root/drivers/scsi/u14-34f.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. wait_on_busy
  2. board_inquiry
  3. port_detect
  4. u14_34f_detect
  5. build_sg_list
  6. u14_34f_queuecommand
  7. u14_34f_abort
  8. u14_34f_reset
  9. u14_34f_biosparam
  10. u14_34f_interrupt_handler

   1 /*
   2  *      u14-34f.c - Low-level driver for UltraStor 14F/34F SCSI host adapters.
   3  *
   4  *      13 Jun 1995 rev. 2.01 for linux 1.2.10
   5  *         HAVE_OLD_UX4F_FIRMWARE should be defined for U34F boards when
   6  *         the firmware prom is not the lastest one (28008-006).
   7  *
   8  *      11 Mar 1995 rev. 2.00 for linux 1.2.0
   9  *          Fixed a bug which prevented media change detection for removable
  10  *          disk drives.
  11  *
  12  *      23 Feb 1995 rev. 1.18 for linux 1.1.94
  13  *          Added a check for scsi_register returning NULL.
  14  *
  15  *      11 Feb 1995 rev. 1.17 for linux 1.1.91
  16  *          U14F qualified to run with 32 sglists.
  17  *          Now DEBUG_RESET is disabled by default.
  18  *
  19  *       9 Feb 1995 rev. 1.16 for linux 1.1.90
  20  *          Use host->wish_block instead of host->block.
  21  *
  22  *       8 Feb 1995 rev. 1.15 for linux 1.1.89
  23  *          Cleared target_time_out counter while performing a reset.
  24  *
  25  *      28 Jan 1995 rev. 1.14 for linux 1.1.86
  26  *          Added module support.
  27  *          Log and do a retry when a disk drive returns a target status
  28  *          different from zero on a recovered error.
  29  *          Auto detects if U14F boards have an old firmware revision.
  30  *          Max number of scatter/gather lists set to 16 for all boards
  31  *          (most installation run fine using 33 sglists, while other
  32  *          has problems when using more then 16).
  33  *
  34  *      16 Jan 1995 rev. 1.13 for linux 1.1.81
  35  *          Display a message if check_region detects a port address
  36  *          already in use.
  37  *
  38  *      15 Dec 1994 rev. 1.12 for linux 1.1.74
  39  *          The host->block flag is set for all the detected ISA boards.
  40  *
  41  *      30 Nov 1994 rev. 1.11 for linux 1.1.68
  42  *          Redo i/o on target status CHECK_CONDITION for TYPE_DISK only.
  43  *          Added optional support for using a single board at a time.
  44  *
  45  *      14 Nov 1994 rev. 1.10 for linux 1.1.63
  46  *
  47  *      28 Oct 1994 rev. 1.09 for linux 1.1.58  Final BETA release.
  48  *      16 Jul 1994 rev. 1.00 for linux 1.1.29  Initial ALPHA release.
  49  *
  50  *          This driver is a total replacement of the original UltraStor 
  51  *          scsi driver, but it supports ONLY the 14F and 34F boards.
  52  *          It can be configured in the same kernel in which the original
  53  *          ultrastor driver is configured to allow the original U24F
  54  *          support.
  55  * 
  56  *          Multiple U14F and/or U34F host adapters are supported.
  57  *
  58  *      Copyright (C) 1994, 1995 Dario Ballabio (dario@milano.europe.dg.com)
  59  *
  60  *      WARNING: if your 14/34F board has an old firmware revision (see below)
  61  *               you must change "#undef" into "#define" in the following
  62  *               statement.
  63  */
  64 #undef HAVE_OLD_UX4F_FIRMWARE
  65 /*
  66  *  The UltraStor 14F, 24F, and 34F are a family of intelligent, high
  67  *  performance SCSI-2 host adapters.
  68  *  Here is the scoop on the various models:
  69  *
  70  *  14F - ISA first-party DMA HA with floppy support and WD1003 emulation.
  71  *  24F - EISA Bus Master HA with floppy support and WD1003 emulation.
  72  *  34F - VESA Local-Bus Bus Master HA (no WD1003 emulation).
  73  *
  74  *  This code has been tested with up to two U14F boards, using both 
  75  *  firmware 28004-005/38004-004 (BIOS rev. 2.00) and the latest firmware
  76  *  28004-006/38004-005 (BIOS rev. 2.01). 
  77  *
  78  *  The latest firmware is required in order to get reliable operations when 
  79  *  clustering is enabled. ENABLE_CLUSTERING provides a performance increase
  80  *  up to 50% on sequential access.
  81  *
  82  *  Since the Scsi_Host_Template structure is shared among all 14F and 34F,
  83  *  the last setting of use_clustering is in effect for all of these boards.
  84  *
  85  *  Here a sample configuration using two U14F boards:
  86  *
  87  U14F0: PORT 0x330, BIOS 0xc8000, IRQ 11, DMA 5, SG 32, Mbox 16, CmdLun 2, C1.
  88  U14F1: PORT 0x340, BIOS 0x00000, IRQ 10, DMA 6, SG 32, Mbox 16, CmdLun 2, C1.
  89  *
  90  *  The boot controller must have its BIOS enabled, while other boards can
  91  *  have their BIOS disabled, or enabled to an higher address.
  92  *  Boards are named Ux4F0, Ux4F1..., according to the port address order in
  93  *  the io_port[] array.
  94  *  
  95  *  The following facts are based on real testing results (not on
  96  *  documentation) on the above U14F board.
  97  *  
  98  *  - The U14F board should be jumpered for bus on time less or equal to 7 
  99  *    microseconds, while the default is 11 microseconds. This is order to 
 100  *    get acceptable performance while using floppy drive and hard disk 
 101  *    together. The jumpering for 7 microseconds is: JP13 pin 15-16, 
 102  *    JP14 pin 7-8 and pin 9-10.
 103  *    The reduction has a little impact on scsi performance.
 104  *  
 105  *  - If scsi bus length exceeds 3m., the scsi bus speed needs to be reduced
 106  *    from 10Mhz to 5Mhz (do this by inserting a jumper on JP13 pin 7-8).
 107  *
 108  *  - If U14F on board firmware is older than 28004-006/38004-005,
 109  *    the U14F board is unable to provide reliable operations if the scsi 
 110  *    request length exceeds 16Kbyte. When this length is exceeded the
 111  *    behavior is: 
 112  *    - adapter_status equal 0x96 or 0xa3 or 0x93 or 0x94;
 113  *    - adapter_status equal 0 and target_status equal 2 on for all targets
 114  *      in the next operation following the reset.
 115  *    This sequence takes a long time (>3 seconds), so in the meantime
 116  *    the SD_TIMEOUT in sd.c could expire giving rise to scsi aborts
 117  *    (SD_TIMEOUT has been increased from 3 to 6 seconds in 1.1.31).
 118  *    Because of this I had to DISABLE_CLUSTERING and to work around the
 119  *    bus reset in the interrupt service routine, returning DID_BUS_BUSY
 120  *    so that the operations are retried without complains from the scsi.c
 121  *    code.
 122  *    Any reset of the scsi bus is going to kill tape operations, since
 123  *    no retry is allowed for tapes. Bus resets are more likely when the
 124  *    scsi bus is under heavy load.
 125  *    Requests using scatter/gather have a maximum length of 16 x 1024 bytes 
 126  *    when DISABLE_CLUSTERING is in effect, but unscattered requests could be
 127  *    larger than 16Kbyte.
 128  *
 129  *    The new firmware has fixed all the above problems.
 130  *
 131  *  For U34F boards the latest bios prom is 38008-002 (BIOS rev. 2.01),
 132  *  the latest firmware prom is 28008-006. Older firmware 28008-005 has
 133  *  problems when using more then 16 scatter/gather lists.
 134  *
 135  *  In order to support multiple ISA boards in a reliable way,
 136  *  the driver sets host->wish_block = TRUE for all ISA boards.
 137  */
 138 
 139 #if defined(MODULE)
 140 #include <linux/module.h>
 141 #include <linux/version.h>
 142 #endif
 143 
 144 #include <linux/string.h>
 145 #include <linux/sched.h>
 146 #include <linux/kernel.h>
 147 #include <linux/ioport.h>
 148 #include <asm/io.h>
 149 #include <asm/system.h>
 150 #include "../block/blk.h"
 151 #include "scsi.h"
 152 #include "hosts.h"
 153 #include "sd.h"
 154 #include <asm/dma.h>
 155 #include <asm/irq.h>
 156 #include "u14-34f.h"
 157 
 158 /* Values for the PRODUCT_ID ports for the 14/34F */
 159 #define PRODUCT_ID1  0x56
 160 #define PRODUCT_ID2  0x40        /* NOTE: Only upper nibble is used */
 161 
 162 /* Subversion values */
 163 #define ISA  0
 164 #define ESA 1
 165 
 166 #define OP_HOST_ADAPTER   0x1
 167 #define OP_SCSI           0x2
 168 #define OP_RESET          0x4
 169 #define DTD_SCSI          0x0
 170 #define DTD_IN            0x1
 171 #define DTD_OUT           0x2
 172 #define DTD_NONE          0x3
 173 #define HA_CMD_INQUIRY    0x1
 174 #define HA_CMD_SELF_DIAG  0x2
 175 #define HA_CMD_READ_BUFF  0x3
 176 #define HA_CMD_WRITE_BUFF 0x4
 177 
 178 #undef  DEBUG_DETECT
 179 #undef  DEBUG_INTERRUPT
 180 #undef  DEBUG_STATISTICS
 181 #undef  DEBUG_RESET
 182 
 183 #define MAX_TARGET 8
 184 #define MAX_IRQ 16
 185 #define MAX_BOARDS 4
 186 #define MAX_MAILBOXES 16
 187 #define MAX_SGLIST 32
 188 #define MAX_SAFE_SGLIST 16
 189 #define MAX_CMD_PER_LUN 2
 190 
 191 #define FALSE 0
 192 #define TRUE 1
 193 #define FREE 0
 194 #define IN_USE   1
 195 #define LOCKED   2
 196 #define IN_RESET 3
 197 #define IGNORE   4
 198 #define NO_IRQ  0xff
 199 #define NO_DMA  0xff
 200 #define MAXLOOP 200000
 201 
 202 #define REG_LCL_MASK      0
 203 #define REG_LCL_INTR      1
 204 #define REG_SYS_MASK      2
 205 #define REG_SYS_INTR      3
 206 #define REG_PRODUCT_ID1   4
 207 #define REG_PRODUCT_ID2   5
 208 #define REG_CONFIG1       6
 209 #define REG_CONFIG2       7
 210 #define REG_OGM           8
 211 #define REG_ICM           12
 212 #define REGION_SIZE       13
 213 #define BSY_ASSERTED      0x01
 214 #define IRQ_ASSERTED      0x01
 215 #define CMD_RESET         0xc0
 216 #define CMD_OGM_INTR      0x01
 217 #define CMD_CLR_INTR      0x01
 218 #define CMD_ENA_INTR      0x81
 219 #define ASOK              0x00
 220 #define ASST              0x91
 221 
 222 #define PACKED          __attribute__((packed))
 223 
 224 /* MailBox SCSI Command Packet */
 225 struct mscp {
 226    unsigned char opcode: 3;             /* type of command */
 227    unsigned char xdir: 2;               /* data transfer direction */
 228    unsigned char dcn: 1;                /* disable disconnect */
 229    unsigned char ca: 1;                 /* use cache (if available) */
 230    unsigned char sg: 1;                 /* scatter/gather operation */
 231    unsigned char target: 3;             /* target SCSI id */
 232    unsigned char ch_no: 2;              /* SCSI channel (always 0 for 14f) */
 233    unsigned char lun: 3;                /* logical unit number */
 234    unsigned int data_address PACKED;    /* transfer data pointer */
 235    unsigned int data_len PACKED;        /* length in bytes */
 236    unsigned int command_link PACKED;    /* for linking command chains */
 237    unsigned char scsi_command_link_id;  /* identifies command in chain */
 238    unsigned char use_sg;                /* (if sg is set) 8 bytes per list */
 239    unsigned char sense_len;
 240    unsigned char scsi_cdbs_len;         /* 6, 10, or 12 */
 241    unsigned char scsi_cdbs[12];         /* SCSI commands */
 242    unsigned char adapter_status;        /* non-zero indicates HA error */
 243    unsigned char target_status;         /* non-zero indicates target error */
 244    unsigned int sense_addr PACKED;
 245 
 246    Scsi_Cmnd *SCpnt;
 247 
 248    struct sg_list {
 249       unsigned int address;     /* Segment Address */
 250       unsigned int num_bytes;   /* Segment Length */
 251       } sglist[MAX_SGLIST];
 252 
 253    unsigned int index;   /* cp index */
 254    };
 255 
 256 struct hostdata {
 257    struct mscp cp[MAX_MAILBOXES];       /* Mailboxes for this board */
 258    unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
 259    unsigned int last_cp_used;           /* Index of last mailbox used */
 260    unsigned int iocount;                /* Total i/o done for this board */
 261    unsigned int multicount;             /* Total ... in second ihdlr loop */
 262    int board_number;                    /* Number of this board */
 263    char board_name[16];                 /* Name of this board */
 264    char board_id[256];                  /* data from INQUIRY on this board */
 265    int in_reset;                        /* True if board is doing a reset */
 266    int target_time_out[MAX_TARGET];     /* N. of timeout errors on target */
 267    int target_reset[MAX_TARGET];        /* If TRUE redo operation on target */
 268    unsigned char subversion;            /* Bus type, either ISA or ESA */
 269    unsigned char heads;
 270    unsigned char sectors;
 271 
 272    /* slot != 0 for the U24F, slot == 0 for both the U14F and U34F */
 273    unsigned char slot;
 274    };
 275 
 276 static struct Scsi_Host * sh[MAX_BOARDS + 1];
 277 static char* driver_name = "Ux4F";
 278 static unsigned int irqlist[MAX_IRQ], calls[MAX_IRQ];
 279 
 280 #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
 281 #define BN(board) (HD(board)->board_name)
 282 
 283 static void u14_34f_interrupt_handler(int, struct pt_regs *);
 284 static int do_trace = FALSE;
 285 
 286 static inline unchar wait_on_busy(ushort iobase) {
     /* [previous][next][first][last][top][bottom][index][help] */
 287    unsigned int loop = MAXLOOP;
 288 
 289    while (inb(iobase + REG_LCL_INTR) & BSY_ASSERTED)
 290       if (--loop == 0) return TRUE;
 291 
 292    return FALSE;
 293 }
 294 
 295 static int board_inquiry(unsigned int j) {
     /* [previous][next][first][last][top][bottom][index][help] */
 296    struct mscp *cpp;
 297    unsigned int time, limit = 0;
 298 
 299    cpp = &HD(j)->cp[0];
 300    memset(cpp, 0, sizeof(struct mscp));
 301    cpp->opcode = OP_HOST_ADAPTER;
 302    cpp->xdir = DTD_IN;
 303    cpp->data_address = (unsigned int) HD(j)->board_id;
 304    cpp->data_len = sizeof(HD(j)->board_id);
 305    cpp->scsi_cdbs_len = 6;
 306    cpp->scsi_cdbs[0] = HA_CMD_INQUIRY;
 307 
 308    if (wait_on_busy(sh[j]->io_port)) {
 309       printk("%s: board_inquiry, adapter busy.\n", BN(j));
 310       return TRUE;
 311       }
 312 
 313    HD(j)->cp_stat[0] = IGNORE;
 314 
 315    /* Clear the interrupt indication */
 316    outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
 317 
 318    /* Store pointer in OGM address bytes */
 319    outl((unsigned int)cpp, sh[j]->io_port + REG_OGM);
 320 
 321    /* Issue OGM interrupt */
 322    outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
 323 
 324    sti();
 325    time = jiffies;
 326    while (jiffies < (time + 100) && limit++ < 100000000);
 327    cli();
 328 
 329    if (cpp->adapter_status || HD(j)->cp_stat[0] != FREE) {
 330       HD(j)->cp_stat[0] = FREE;
 331       printk("%s: board_inquiry, err 0x%x.\n", BN(j), cpp->adapter_status);
 332       return TRUE;
 333       }
 334 
 335    return FALSE;
 336 }
 337 
 338 static inline int port_detect(ushort *port_base, unsigned int j, 
     /* [previous][next][first][last][top][bottom][index][help] */
 339                               Scsi_Host_Template * tpnt) {
 340    unsigned char irq, dma_channel, subversion;
 341    unsigned char in_byte;
 342 
 343    /* Allowed BIOS base addresses (NULL indicates reserved) */
 344    void *bios_segment_table[8] = { 
 345       NULL, 
 346       (void *) 0xc4000, (void *) 0xc8000, (void *) 0xcc000, (void *) 0xd0000,
 347       (void *) 0xd4000, (void *) 0xd8000, (void *) 0xdc000
 348       };
 349    
 350    /* Allowed IRQs */
 351    unsigned char interrupt_table[4] = { 15, 14, 11, 10 };
 352    
 353    /* Allowed DMA channels for ISA (0 indicates reserved) */
 354    unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
 355    
 356    /* Head/sector mappings */
 357    struct {
 358       unsigned char heads;
 359       unsigned char sectors;
 360       } mapping_table[4] = { 
 361            { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 }
 362            };
 363 
 364    struct config_1 {
 365       unsigned char bios_segment: 3;
 366       unsigned char removable_disks_as_fixed: 1;
 367       unsigned char interrupt: 2;
 368       unsigned char dma_channel: 2;
 369       } config_1;
 370 
 371    struct config_2 {
 372       unsigned char ha_scsi_id: 3;
 373       unsigned char mapping_mode: 2;
 374       unsigned char bios_drive_number: 1;
 375       unsigned char tfr_port: 2;
 376       } config_2;
 377 
 378    char name[16];
 379 
 380    sprintf(name, "%s%d", driver_name, j);
 381 
 382    if(check_region(*port_base, REGION_SIZE)) {
 383       printk("%s: address 0x%03x in use, skipping probe.\n", 
 384              name, *port_base);
 385       return FALSE;
 386       }
 387 
 388    if (inb(*port_base + REG_PRODUCT_ID1) != PRODUCT_ID1) return FALSE;
 389 
 390    in_byte = inb(*port_base + REG_PRODUCT_ID2);
 391 
 392    if ((in_byte & 0xf0) != PRODUCT_ID2) return FALSE;
 393 
 394    *(char *)&config_1 = inb(*port_base + REG_CONFIG1);
 395    *(char *)&config_2 = inb(*port_base + REG_CONFIG2);
 396 
 397    irq = interrupt_table[config_1.interrupt];
 398    dma_channel = dma_channel_table[config_1.dma_channel];
 399    subversion = (in_byte & 0x0f);
 400 
 401    /* Board detected, allocate its IRQ if not already done */
 402    if ((irq >= MAX_IRQ) || ((irqlist[irq] == NO_IRQ) && request_irq
 403        (irq, u14_34f_interrupt_handler, SA_INTERRUPT, driver_name))) {
 404       printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
 405       return FALSE;
 406       }
 407 
 408    if (subversion == ISA && request_dma(dma_channel, driver_name)) {
 409       printk("%s: unable to allocate DMA channel %u, detaching.\n",
 410              name, dma_channel);
 411       free_irq(irq);
 412       return FALSE;
 413       }
 414 
 415    sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
 416 
 417    if (sh[j] == NULL) {
 418       printk("%s: unable to register host, detaching.\n", name);
 419 
 420       if (irqlist[irq] == NO_IRQ) free_irq(irq);
 421 
 422       if (subversion == ISA) free_dma(dma_channel);
 423 
 424       return FALSE;
 425       }
 426 
 427    sh[j]->io_port = *port_base;
 428    sh[j]->n_io_port = REGION_SIZE;
 429    sh[j]->base = bios_segment_table[config_1.bios_segment];
 430    sh[j]->irq = irq;
 431    sh[j]->sg_tablesize = MAX_SGLIST;
 432    sh[j]->this_id = config_2.ha_scsi_id;
 433    sh[j]->can_queue = MAX_MAILBOXES;
 434    sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
 435 
 436 #if defined(DEBUG_DETECT)
 437    {
 438    unsigned char sys_mask, lcl_mask;
 439 
 440    sys_mask = inb(sh[j]->io_port + REG_SYS_MASK);
 441    lcl_mask = inb(sh[j]->io_port + REG_LCL_MASK);
 442    printk("SYS_MASK 0x%x, LCL_MASK 0x%x.\n", sys_mask, lcl_mask);
 443    }
 444 #endif
 445 
 446    /* If BIOS is disabled, force enable interrupts */
 447    if (sh[j]->base == 0) outb(CMD_ENA_INTR, sh[j]->io_port + REG_SYS_MASK);
 448 
 449    /* Register the I/O space that we use */
 450    request_region(sh[j]->io_port, REGION_SIZE, driver_name);
 451 
 452    memset(HD(j), 0, sizeof(struct hostdata));
 453    HD(j)->heads = mapping_table[config_2.mapping_mode].heads;
 454    HD(j)->sectors = mapping_table[config_2.mapping_mode].sectors;
 455    HD(j)->subversion = subversion;
 456    HD(j)->board_number = j;
 457    irqlist[irq] = j;
 458 
 459    if (HD(j)->subversion == ESA) {
 460 
 461 #if defined (HAVE_OLD_UX4F_FIRMWARE)
 462       sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
 463 #endif
 464 
 465       sh[j]->dma_channel = NO_DMA;
 466       sh[j]->unchecked_isa_dma = FALSE;
 467       sprintf(BN(j), "U34F%d", j);
 468       }
 469    else {
 470       sh[j]->wish_block = TRUE;
 471 
 472 #if defined (HAVE_OLD_UX4F_FIRMWARE)
 473       sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
 474       sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
 475 #endif
 476 
 477       sh[j]->dma_channel = dma_channel;
 478       sh[j]->unchecked_isa_dma = TRUE;
 479       sprintf(BN(j), "U14F%d", j);
 480       disable_dma(dma_channel);
 481       clear_dma_ff(dma_channel);
 482       set_dma_mode(dma_channel, DMA_MODE_CASCADE);
 483       enable_dma(dma_channel);
 484       }
 485 
 486    if (HD(j)->subversion == ISA && !board_inquiry(j)) {
 487       HD(j)->board_id[40] = 0;
 488 
 489       if (strcmp(&HD(j)->board_id[32], "06000600")) {
 490          printk("%s: %s.\n", BN(j), &HD(j)->board_id[8]);
 491          printk("%s: firmware %s is outdated, FW PROM should be 28004-006.\n",
 492                 BN(j), &HD(j)->board_id[32]);
 493          sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
 494          sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
 495          }
 496       }
 497 
 498    printk("%s: PORT 0x%03x, BIOS 0x%05x, IRQ %u, DMA %u, SG %d, "\
 499           "Mbox %d, CmdLun %d, C%d.\n", BN(j), sh[j]->io_port, 
 500           (int)sh[j]->base, sh[j]->irq, 
 501           sh[j]->dma_channel, sh[j]->sg_tablesize, 
 502           sh[j]->can_queue, sh[j]->cmd_per_lun,
 503           sh[j]->hostt->use_clustering);
 504    return TRUE;
 505 }
 506 
 507 int u14_34f_detect (Scsi_Host_Template * tpnt) {
     /* [previous][next][first][last][top][bottom][index][help] */
 508    unsigned int j = 0, k, flags;
 509 
 510    ushort io_port[] = {
 511       0x330, 0x340, 0x230, 0x240, 0x210, 0x130, 0x140, 0x0
 512       };
 513 
 514    ushort *port_base = io_port;
 515 
 516    save_flags(flags);
 517    cli();
 518 
 519    for (k = 0; k < MAX_IRQ; k++) {
 520       irqlist[k] = NO_IRQ;
 521       calls[k] = 0;
 522       }
 523 
 524    for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
 525 
 526    while (*port_base) {
 527 
 528       if (j < MAX_BOARDS && port_detect(port_base, j, tpnt)) j++;
 529 
 530       port_base++;
 531       }
 532 
 533    if (j > 0) 
 534       printk("UltraStor 14F/34F: Copyright (C) 1994, 1995 Dario Ballabio.\n");
 535 
 536    restore_flags(flags);
 537    return j;
 538 }
 539 
 540 static inline void build_sg_list(struct mscp *cpp, Scsi_Cmnd *SCpnt) {
     /* [previous][next][first][last][top][bottom][index][help] */
 541    unsigned int k, data_len = 0;
 542    struct scatterlist * sgpnt;
 543 
 544    sgpnt = (struct scatterlist *) SCpnt->request_buffer;
 545 
 546    for (k = 0; k < SCpnt->use_sg; k++) {
 547       cpp->sglist[k].address = (unsigned int) sgpnt[k].address;
 548       cpp->sglist[k].num_bytes = sgpnt[k].length;
 549       data_len += sgpnt[k].length;
 550       }
 551 
 552    cpp->use_sg = SCpnt->use_sg;
 553    cpp->data_address = (unsigned int) cpp->sglist;
 554    cpp->data_len = data_len;
 555 }
 556 
 557 int u14_34f_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
     /* [previous][next][first][last][top][bottom][index][help] */
 558    unsigned int i, j, k, flags;
 559    struct mscp *cpp;
 560 
 561    save_flags(flags);
 562    cli();
 563    /* j is the board number */
 564    j = ((struct hostdata *) SCpnt->host->hostdata)->board_number;
 565 
 566    if (!done) panic("%s: qcomm, pid %ld, null done.\n", BN(j), SCpnt->pid);
 567 
 568    /* i is the mailbox number, look for the first free mailbox 
 569       starting from last_cp_used */
 570    i = HD(j)->last_cp_used + 1;
 571 
 572    for (k = 0; k < sh[j]->can_queue; k++, i++) {
 573 
 574       if (i >= sh[j]->can_queue) i = 0;
 575 
 576       if (HD(j)->cp_stat[i] == FREE) {
 577          HD(j)->last_cp_used = i;
 578          break;
 579          }
 580       }
 581 
 582    if (k == sh[j]->can_queue) {
 583       printk("%s: qcomm, no free mailbox, resetting.\n", BN(j));
 584 
 585       if (HD(j)->in_reset) 
 586          printk("%s: qcomm, already in reset.\n", BN(j));
 587       else if (u14_34f_reset(SCpnt) == SCSI_RESET_SUCCESS) 
 588          panic("%s: qcomm, SCSI_RESET_SUCCESS.\n", BN(j));
 589 
 590       SCpnt->result = DID_BUS_BUSY << 16; 
 591       SCpnt->host_scribble = NULL;
 592       printk("%s: qcomm, pid %ld, DID_BUS_BUSY, done.\n", BN(j), SCpnt->pid);
 593       restore_flags(flags);
 594       done(SCpnt);    
 595       return 0;
 596       }
 597 
 598    /* Set pointer to control packet structure */
 599    cpp = &HD(j)->cp[i];
 600 
 601    memset(cpp, 0, sizeof(struct mscp));
 602    SCpnt->scsi_done = done;
 603    cpp->index = i;
 604    SCpnt->host_scribble = (unsigned char *) &cpp->index;
 605 
 606    if (do_trace) printk("%s: qcomm, mbox %d, target %d, pid %ld.\n",
 607                         BN(j), i, SCpnt->target, SCpnt->pid);
 608 
 609    cpp->opcode = OP_SCSI;
 610    cpp->xdir = DTD_SCSI;
 611    cpp->target = SCpnt->target;
 612    cpp->lun = SCpnt->lun;
 613    cpp->SCpnt = SCpnt;
 614    cpp->sense_addr = (unsigned int) SCpnt->sense_buffer;
 615    cpp->sense_len = sizeof SCpnt->sense_buffer;
 616 
 617    if (SCpnt->use_sg) {
 618       cpp->sg = TRUE;
 619       build_sg_list(cpp, SCpnt);
 620       }
 621    else {
 622       cpp->data_address = (unsigned int)SCpnt->request_buffer;
 623       cpp->data_len = SCpnt->request_bufflen;
 624       }
 625 
 626    cpp->scsi_cdbs_len = SCpnt->cmd_len;
 627    memcpy(cpp->scsi_cdbs, SCpnt->cmnd, cpp->scsi_cdbs_len);
 628 
 629    if (wait_on_busy(sh[j]->io_port)) {
 630       SCpnt->result = DID_ERROR << 16;
 631       SCpnt->host_scribble = NULL;
 632       printk("%s: qcomm, target %d, pid %ld, adapter busy, DID_ERROR, done.\n", 
 633              BN(j), SCpnt->target, SCpnt->pid);
 634       restore_flags(flags);
 635       done(SCpnt);
 636       return 0;
 637       }
 638 
 639    /* Store pointer in OGM address bytes */
 640    outl((unsigned int)cpp, sh[j]->io_port + REG_OGM);
 641 
 642    /* Issue OGM interrupt */
 643    outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
 644 
 645    HD(j)->cp_stat[i] = IN_USE;
 646    restore_flags(flags);
 647    return 0;
 648 }
 649 
 650 int u14_34f_abort(Scsi_Cmnd *SCarg) {
     /* [previous][next][first][last][top][bottom][index][help] */
 651    unsigned int i, j, flags;
 652 
 653    save_flags(flags);
 654    cli();
 655    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
 656 
 657    if (SCarg->host_scribble == NULL) {
 658       printk("%s: abort, target %d, pid %ld inactive.\n",
 659              BN(j), SCarg->target, SCarg->pid);
 660       return SCSI_ABORT_NOT_RUNNING;
 661       }
 662 
 663    i = *(unsigned int *)SCarg->host_scribble;
 664    printk("%s: abort, mbox %d, target %d, pid %ld.\n",
 665           BN(j), i, SCarg->target, SCarg->pid);
 666 
 667    if (i >= sh[j]->can_queue)
 668       panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
 669 
 670    if (wait_on_busy(sh[j]->io_port)) {
 671       printk("%s: abort, timeout error.\n", BN(j));
 672       restore_flags(flags);
 673       return SCSI_ABORT_ERROR;
 674       }
 675 
 676    if (HD(j)->cp_stat[i] == FREE) {
 677       printk("%s: abort, mbox %d is free.\n", BN(j), i);
 678       restore_flags(flags);
 679       return SCSI_ABORT_NOT_RUNNING;
 680       }
 681 
 682    if (HD(j)->cp_stat[i] == IN_USE) {
 683       printk("%s: abort, mbox %d is in use.\n", BN(j), i);
 684 
 685       if (SCarg != HD(j)->cp[i].SCpnt)
 686          panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
 687                BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
 688 
 689       restore_flags(flags);
 690       return SCSI_ABORT_SNOOZE;
 691       }
 692 
 693    if (HD(j)->cp_stat[i] == IN_RESET) {
 694       printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
 695       restore_flags(flags);
 696       return SCSI_ABORT_ERROR;
 697       }
 698 
 699    if (HD(j)->cp_stat[i] == LOCKED) {
 700       printk("%s: abort, mbox %d is locked.\n", BN(j), i);
 701       restore_flags(flags);
 702       return SCSI_ABORT_NOT_RUNNING;
 703       }
 704    else
 705       panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
 706 }
 707 
 708 int u14_34f_reset(Scsi_Cmnd * SCarg) {
     /* [previous][next][first][last][top][bottom][index][help] */
 709    unsigned int i, j, flags, time, k, limit = 0;
 710    int arg_done = FALSE;
 711    Scsi_Cmnd *SCpnt;
 712 
 713    save_flags(flags);
 714    cli();
 715    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
 716    printk("%s: reset, enter, target %d, pid %ld.\n", 
 717           BN(j), SCarg->target, SCarg->pid);
 718 
 719    if (SCarg->host_scribble == NULL)
 720       printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
 721 
 722    if (HD(j)->in_reset) {
 723       printk("%s: reset, exit, already in reset.\n", BN(j));
 724       restore_flags(flags);
 725       return SCSI_RESET_ERROR;
 726       }
 727 
 728    if (wait_on_busy(sh[j]->io_port)) {
 729       printk("%s: reset, exit, timeout error.\n", BN(j));
 730       restore_flags(flags);
 731       return SCSI_RESET_ERROR;
 732       }
 733 
 734    for (k = 0; k < MAX_TARGET; k++) HD(j)->target_reset[k] = TRUE;
 735 
 736    for (k = 0; k < MAX_TARGET; k++) HD(j)->target_time_out[k] = 0;
 737 
 738    for (i = 0; i < sh[j]->can_queue; i++) {
 739 
 740       if (HD(j)->cp_stat[i] == FREE) continue;
 741 
 742       if (HD(j)->cp_stat[i] == LOCKED) {
 743          HD(j)->cp_stat[i] = FREE;
 744          printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
 745          continue;
 746          }
 747 
 748       SCpnt = HD(j)->cp[i].SCpnt;
 749       HD(j)->cp_stat[i] = IN_RESET;
 750       printk("%s: reset, mbox %d in reset, pid %ld.\n",
 751              BN(j), i, SCpnt->pid);
 752 
 753       if (SCpnt == NULL)
 754          panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
 755 
 756       if (SCpnt->host_scribble == NULL)
 757          panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
 758 
 759       if (*(unsigned int *)SCpnt->host_scribble != i) 
 760          panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
 761 
 762       if (SCpnt->scsi_done == NULL) 
 763          panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
 764 
 765       if (SCpnt == SCarg) arg_done = TRUE;
 766       }
 767 
 768    if (wait_on_busy(sh[j]->io_port)) {
 769       printk("%s: reset, cannot reset, timeout error.\n", BN(j));
 770       restore_flags(flags);
 771       return SCSI_RESET_ERROR;
 772       }
 773 
 774    outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
 775    printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
 776 
 777 #if defined (DEBUG_RESET)
 778    do_trace = TRUE;
 779 #endif
 780 
 781    HD(j)->in_reset = TRUE;
 782    sti();
 783    time = jiffies;
 784    while (jiffies < (time + 100) && limit++ < 100000000);
 785    cli();
 786    printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
 787 
 788    for (i = 0; i < sh[j]->can_queue; i++) {
 789 
 790       /* Skip mailboxes already set free by interrupt */
 791       if (HD(j)->cp_stat[i] != IN_RESET) continue;
 792 
 793       SCpnt = HD(j)->cp[i].SCpnt;
 794       SCpnt->result = DID_RESET << 16;
 795       SCpnt->host_scribble = NULL;
 796 
 797       /* This mailbox is still waiting for its interrupt */
 798       HD(j)->cp_stat[i] = LOCKED;
 799 
 800       printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
 801              BN(j), i, SCpnt->pid);
 802       restore_flags(flags);
 803       SCpnt->scsi_done(SCpnt);
 804       cli();
 805       }
 806 
 807    HD(j)->in_reset = FALSE;
 808    do_trace = FALSE;
 809    restore_flags(flags);
 810 
 811    if (arg_done) {
 812       printk("%s: reset, exit, success.\n", BN(j));
 813       return SCSI_RESET_SUCCESS;
 814       }
 815    else {
 816       printk("%s: reset, exit, wakeup.\n", BN(j));
 817       return SCSI_RESET_PUNT;
 818       }
 819 }
 820 
 821 int u14_34f_biosparam(Disk * disk, int dev, int * dkinfo) {
     /* [previous][next][first][last][top][bottom][index][help] */
 822    unsigned int j = 0;
 823    int size = disk->capacity;
 824 
 825    dkinfo[0] = HD(j)->heads;
 826    dkinfo[1] = HD(j)->sectors;
 827    dkinfo[2] = size / (HD(j)->heads * HD(j)->sectors);
 828    return 0;
 829 }
 830 
 831 static void u14_34f_interrupt_handler(int irq, struct pt_regs * regs) {
     /* [previous][next][first][last][top][bottom][index][help] */
 832    Scsi_Cmnd *SCpnt;
 833    unsigned int i, j, k, flags, status, tstatus, loops, total_loops = 0;
 834    struct mscp *spp;
 835 
 836    save_flags(flags);
 837    cli();
 838 
 839    if (irqlist[irq] == NO_IRQ) {
 840       printk("%s, ihdlr, irq %d, unexpected interrupt.\n", driver_name, irq);
 841       restore_flags(flags);
 842       return;
 843       }
 844 
 845    if (do_trace) printk("%s: ihdlr, enter, irq %d, calls %d.\n", 
 846                         driver_name, irq, calls[irq]);
 847 
 848    /* Service all the boards configured on this irq */
 849    for (j = 0; sh[j] != NULL; j++) {
 850 
 851       if (sh[j]->irq != irq) continue;
 852 
 853       loops = 0;
 854 
 855       /* Loop until all interrupts for a board are serviced */
 856       while (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED) {
 857          total_loops++;
 858          loops++;
 859 
 860          if (do_trace) printk("%s: ihdlr, start service, count %d.\n",
 861                               BN(j), HD(j)->iocount);
 862 
 863          spp = (struct mscp *)inl(sh[j]->io_port + REG_ICM);
 864 
 865          /* Clear interrupt pending flag */
 866          outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
 867 
 868          i = spp - HD(j)->cp;
 869 
 870          if (i >= sh[j]->can_queue)
 871             panic("%s: ihdlr, invalid mscp address.\n", BN(j));
 872 
 873          if (HD(j)->cp_stat[i] == IGNORE) {
 874             HD(j)->cp_stat[i] = FREE;
 875             continue;
 876             }
 877          else if (HD(j)->cp_stat[i] == LOCKED) {
 878             HD(j)->cp_stat[i] = FREE;
 879             printk("%s: ihdlr, mbox %d unlocked, count %d.\n",
 880                    BN(j), i, HD(j)->iocount);
 881             continue;
 882             }
 883          else if (HD(j)->cp_stat[i] == FREE) {
 884             printk("%s: ihdlr, mbox %d is free, count %d.\n", 
 885                    BN(j), i, HD(j)->iocount);
 886             continue;
 887             }
 888          else if (HD(j)->cp_stat[i] == IN_RESET)
 889             printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
 890          else if (HD(j)->cp_stat[i] != IN_USE) 
 891             panic("%s: ihdlr, mbox %d, invalid cp_stat.\n", BN(j), i);
 892 
 893          HD(j)->cp_stat[i] = FREE;
 894          SCpnt = spp->SCpnt;
 895 
 896          if (SCpnt == NULL) 
 897             panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
 898 
 899          if (SCpnt->host_scribble == NULL) 
 900             panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n",
 901                   BN(j), i, SCpnt->pid, SCpnt);
 902 
 903          if (*(unsigned int *)SCpnt->host_scribble != i) 
 904             panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d,"\
 905                   " irq %d.\n", BN(j), i, SCpnt->pid, 
 906                   *(unsigned int *)SCpnt->host_scribble, irq);
 907 
 908          tstatus = status_byte(spp->target_status);
 909 
 910          switch (spp->adapter_status) {
 911             case ASOK:     /* status OK */
 912 
 913                /* Forces a reset if a disk drive keeps returning BUSY */
 914                if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE) 
 915                   status = DID_ERROR << 16;
 916 
 917                /* If there was a bus reset, redo operation on each target */
 918                else if (tstatus != GOOD
 919                         && SCpnt->device->type == TYPE_DISK
 920                         && HD(j)->target_reset[SCpnt->target])
 921                   status = DID_BUS_BUSY << 16;
 922 
 923                /* Works around a flaw in scsi.c */
 924                else if (tstatus == CHECK_CONDITION
 925                         && SCpnt->device->type == TYPE_DISK
 926                         && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
 927                   status = DID_BUS_BUSY << 16;
 928 
 929                else
 930                   status = DID_OK << 16;
 931 
 932                if (tstatus == GOOD)
 933                   HD(j)->target_reset[SCpnt->target] = FALSE;
 934 
 935                if (spp->target_status && SCpnt->device->type == TYPE_DISK)
 936                   printk("%s: ihdlr, target %d:%d, pid %ld, target_status "\
 937                          "0x%x, sense key 0x%x.\n", BN(j), 
 938                          SCpnt->target, SCpnt->lun, SCpnt->pid,
 939                          spp->target_status, SCpnt->sense_buffer[2]);
 940 
 941                HD(j)->target_time_out[SCpnt->target] = 0;
 942 
 943                break;
 944             case ASST:     /* Selection Time Out */
 945 
 946                if (HD(j)->target_time_out[SCpnt->target] > 1)
 947                   status = DID_ERROR << 16;
 948                else {
 949                   status = DID_TIME_OUT << 16;
 950                   HD(j)->target_time_out[SCpnt->target]++;
 951                   }
 952 
 953                break;
 954             case 0x92:     /* Data over/under-run */
 955             case 0x93:     /* Unexpected bus free */
 956             case 0x94:     /* Target bus phase sequence failure */
 957             case 0x96:     /* Illegal SCSI command */
 958             case 0xa3:     /* SCSI bus reset error */
 959 
 960                if (SCpnt->device->type != TYPE_TAPE)
 961                   status = DID_BUS_BUSY << 16;
 962                else
 963                   status = DID_ERROR << 16;
 964 
 965                for (k = 0; k < MAX_TARGET; k++) 
 966                   HD(j)->target_reset[k] = TRUE;
 967 
 968                break;
 969             case 0x01:     /* Invalid command */
 970             case 0x02:     /* Invalid parameters */
 971             case 0x03:     /* Invalid data list */
 972             case 0x84:     /* SCSI bus abort error */
 973             case 0x9b:     /* Auto request sense error */
 974             case 0x9f:     /* Unexpected command complete message error */
 975             case 0xff:     /* Invalid parameter in the S/G list */
 976             default:
 977                status = DID_ERROR << 16;
 978                break;
 979             }
 980 
 981          SCpnt->result = status | spp->target_status;
 982          HD(j)->iocount++;
 983 
 984          if (loops > 1) HD(j)->multicount++;
 985 
 986 #if defined (DEBUG_INTERRUPT)
 987          if (SCpnt->result || do_trace) 
 988 #else
 989          if ((spp->adapter_status != ASOK && HD(j)->iocount >  1000) ||
 990              (spp->adapter_status != ASOK && 
 991               spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
 992              do_trace)
 993 #endif
 994             printk("%s: ihdlr, mbox %d, err 0x%x:%x,"\
 995                    " target %d:%d, pid %ld, count %d.\n",
 996                    BN(j), i, spp->adapter_status, spp->target_status,
 997                    SCpnt->target, SCpnt->lun, SCpnt->pid, HD(j)->iocount);
 998 
 999          /* Set the command state to inactive */
1000          SCpnt->host_scribble = NULL;
1001 
1002          restore_flags(flags);
1003          SCpnt->scsi_done(SCpnt);
1004          cli();
1005 
1006          }   /* Multiple command loop */
1007 
1008       }   /* Boards loop */
1009 
1010    calls[irq]++;
1011 
1012    if (total_loops == 0) 
1013      printk("%s: ihdlr, irq %d, no command completed, calls %d.\n",
1014             driver_name, irq, calls[irq]);
1015 
1016    if (do_trace) printk("%s: ihdlr, exit, irq %d, calls %d.\n",
1017                         driver_name, irq, calls[irq]);
1018 
1019 #if defined (DEBUG_STATISTICS)
1020    if ((calls[irq] % 100000) == 10000)
1021       for (j = 0; sh[j] != NULL; j++)
1022          printk("%s: ihdlr, calls %d, count %d, multi %d.\n", BN(j),
1023                 calls[(sh[j]->irq)], HD(j)->iocount, HD(j)->multicount);
1024 #endif
1025 
1026    restore_flags(flags);
1027    return;
1028 }
1029 
1030 #if defined(MODULE)
1031 Scsi_Host_Template driver_template = ULTRASTOR_14_34F;
1032 
1033 #include "scsi_module.c"
1034 #endif

/* [previous][next][first][last][top][bottom][index][help] */