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

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