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 <linux/proc_fs.h>
 151 #include "../block/blk.h"
 152 #include "scsi.h"
 153 #include "hosts.h"
 154 #include "sd.h"
 155 #include <asm/dma.h>
 156 #include <asm/irq.h>
 157 #include "u14-34f.h"
 158 
 159 /* Values for the PRODUCT_ID ports for the 14/34F */
 160 #define PRODUCT_ID1  0x56
 161 #define PRODUCT_ID2  0x40        /* NOTE: Only upper nibble is used */
 162 
 163 /* Subversion values */
 164 #define ISA  0
 165 #define ESA 1
 166 
 167 #define OP_HOST_ADAPTER   0x1
 168 #define OP_SCSI           0x2
 169 #define OP_RESET          0x4
 170 #define DTD_SCSI          0x0
 171 #define DTD_IN            0x1
 172 #define DTD_OUT           0x2
 173 #define DTD_NONE          0x3
 174 #define HA_CMD_INQUIRY    0x1
 175 #define HA_CMD_SELF_DIAG  0x2
 176 #define HA_CMD_READ_BUFF  0x3
 177 #define HA_CMD_WRITE_BUFF 0x4
 178 
 179 #undef  DEBUG_DETECT
 180 #undef  DEBUG_INTERRUPT
 181 #undef  DEBUG_STATISTICS
 182 #undef  DEBUG_RESET
 183 
 184 #define MAX_TARGET 8
 185 #define MAX_IRQ 16
 186 #define MAX_BOARDS 4
 187 #define MAX_MAILBOXES 16
 188 #define MAX_SGLIST 32
 189 #define MAX_SAFE_SGLIST 16
 190 #define MAX_CMD_PER_LUN 2
 191 
 192 #define FALSE 0
 193 #define TRUE 1
 194 #define FREE 0
 195 #define IN_USE   1
 196 #define LOCKED   2
 197 #define IN_RESET 3
 198 #define IGNORE   4
 199 #define NO_IRQ  0xff
 200 #define NO_DMA  0xff
 201 #define MAXLOOP 200000
 202 
 203 #define REG_LCL_MASK      0
 204 #define REG_LCL_INTR      1
 205 #define REG_SYS_MASK      2
 206 #define REG_SYS_INTR      3
 207 #define REG_PRODUCT_ID1   4
 208 #define REG_PRODUCT_ID2   5
 209 #define REG_CONFIG1       6
 210 #define REG_CONFIG2       7
 211 #define REG_OGM           8
 212 #define REG_ICM           12
 213 #define REGION_SIZE       13
 214 #define BSY_ASSERTED      0x01
 215 #define IRQ_ASSERTED      0x01
 216 #define CMD_RESET         0xc0
 217 #define CMD_OGM_INTR      0x01
 218 #define CMD_CLR_INTR      0x01
 219 #define CMD_ENA_INTR      0x81
 220 #define ASOK              0x00
 221 #define ASST              0x91
 222 
 223 #define PACKED          __attribute__((packed))
 224 
 225 /* MailBox SCSI Command Packet */
 226 struct mscp {
 227    unsigned char opcode: 3;             /* type of command */
 228    unsigned char xdir: 2;               /* data transfer direction */
 229    unsigned char dcn: 1;                /* disable disconnect */
 230    unsigned char ca: 1;                 /* use cache (if available) */
 231    unsigned char sg: 1;                 /* scatter/gather operation */
 232    unsigned char target: 3;             /* target SCSI id */
 233    unsigned char ch_no: 2;              /* SCSI channel (always 0 for 14f) */
 234    unsigned char lun: 3;                /* logical unit number */
 235    unsigned int data_address PACKED;    /* transfer data pointer */
 236    unsigned int data_len PACKED;        /* length in bytes */
 237    unsigned int command_link PACKED;    /* for linking command chains */
 238    unsigned char scsi_command_link_id;  /* identifies command in chain */
 239    unsigned char use_sg;                /* (if sg is set) 8 bytes per list */
 240    unsigned char sense_len;
 241    unsigned char scsi_cdbs_len;         /* 6, 10, or 12 */
 242    unsigned char scsi_cdbs[12];         /* SCSI commands */
 243    unsigned char adapter_status;        /* non-zero indicates HA error */
 244    unsigned char target_status;         /* non-zero indicates target error */
 245    unsigned int sense_addr PACKED;
 246 
 247    Scsi_Cmnd *SCpnt;
 248 
 249    struct sg_list {
 250       unsigned int address;     /* Segment Address */
 251       unsigned int num_bytes;   /* Segment Length */
 252       } sglist[MAX_SGLIST];
 253 
 254    unsigned int index;   /* cp index */
 255    };
 256 
 257 struct hostdata {
 258    struct mscp cp[MAX_MAILBOXES];       /* Mailboxes for this board */
 259    unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
 260    unsigned int last_cp_used;           /* Index of last mailbox used */
 261    unsigned int iocount;                /* Total i/o done for this board */
 262    unsigned int multicount;             /* Total ... in second ihdlr loop */
 263    int board_number;                    /* Number of this board */
 264    char board_name[16];                 /* Name of this board */
 265    char board_id[256];                  /* data from INQUIRY on this board */
 266    int in_reset;                        /* True if board is doing a reset */
 267    int target_time_out[MAX_TARGET];     /* N. of timeout errors on target */
 268    int target_reset[MAX_TARGET];        /* If TRUE redo operation on target */
 269    unsigned char subversion;            /* Bus type, either ISA or ESA */
 270    unsigned char heads;
 271    unsigned char sectors;
 272 
 273    /* slot != 0 for the U24F, slot == 0 for both the U14F and U34F */
 274    unsigned char slot;
 275    };
 276 
 277 static struct Scsi_Host * sh[MAX_BOARDS + 1];
 278 static const char* driver_name = "Ux4F";
 279 static unsigned int irqlist[MAX_IRQ], calls[MAX_IRQ];
 280 
 281 #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
 282 #define BN(board) (HD(board)->board_name)
 283 
 284 static void u14_34f_interrupt_handler(int, struct pt_regs *);
 285 static int do_trace = FALSE;
 286 
 287 static inline unchar wait_on_busy(ushort iobase) {
     /* [previous][next][first][last][top][bottom][index][help] */
 288    unsigned int loop = MAXLOOP;
 289 
 290    while (inb(iobase + REG_LCL_INTR) & BSY_ASSERTED)
 291       if (--loop == 0) return TRUE;
 292 
 293    return FALSE;
 294 }
 295 
 296 static int board_inquiry(unsigned int j) {
     /* [previous][next][first][last][top][bottom][index][help] */
 297    struct mscp *cpp;
 298    unsigned int time, limit = 0;
 299 
 300    cpp = &HD(j)->cp[0];
 301    memset(cpp, 0, sizeof(struct mscp));
 302    cpp->opcode = OP_HOST_ADAPTER;
 303    cpp->xdir = DTD_IN;
 304    cpp->data_address = (unsigned int) HD(j)->board_id;
 305    cpp->data_len = sizeof(HD(j)->board_id);
 306    cpp->scsi_cdbs_len = 6;
 307    cpp->scsi_cdbs[0] = HA_CMD_INQUIRY;
 308 
 309    if (wait_on_busy(sh[j]->io_port)) {
 310       printk("%s: board_inquiry, adapter busy.\n", BN(j));
 311       return TRUE;
 312       }
 313 
 314    HD(j)->cp_stat[0] = IGNORE;
 315 
 316    /* Clear the interrupt indication */
 317    outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
 318 
 319    /* Store pointer in OGM address bytes */
 320    outl((unsigned int)cpp, sh[j]->io_port + REG_OGM);
 321 
 322    /* Issue OGM interrupt */
 323    outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
 324 
 325    sti();
 326    time = jiffies;
 327    while (jiffies < (time + 100) && limit++ < 100000000);
 328    cli();
 329 
 330    if (cpp->adapter_status || HD(j)->cp_stat[0] != FREE) {
 331       HD(j)->cp_stat[0] = FREE;
 332       printk("%s: board_inquiry, err 0x%x.\n", BN(j), cpp->adapter_status);
 333       return TRUE;
 334       }
 335 
 336    return FALSE;
 337 }
 338 
 339 static inline int port_detect(ushort *port_base, unsigned int j, 
     /* [previous][next][first][last][top][bottom][index][help] */
 340                               Scsi_Host_Template * tpnt) {
 341    unsigned char irq, dma_channel, subversion;
 342    unsigned char in_byte;
 343 
 344    /* Allowed BIOS base addresses (NULL indicates reserved) */
 345    void *bios_segment_table[8] = { 
 346       NULL, 
 347       (void *) 0xc4000, (void *) 0xc8000, (void *) 0xcc000, (void *) 0xd0000,
 348       (void *) 0xd4000, (void *) 0xd8000, (void *) 0xdc000
 349       };
 350    
 351    /* Allowed IRQs */
 352    unsigned char interrupt_table[4] = { 15, 14, 11, 10 };
 353    
 354    /* Allowed DMA channels for ISA (0 indicates reserved) */
 355    unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
 356    
 357    /* Head/sector mappings */
 358    struct {
 359       unsigned char heads;
 360       unsigned char sectors;
 361       } mapping_table[4] = { 
 362            { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 }
 363            };
 364 
 365    struct config_1 {
 366       unsigned char bios_segment: 3;
 367       unsigned char removable_disks_as_fixed: 1;
 368       unsigned char interrupt: 2;
 369       unsigned char dma_channel: 2;
 370       } config_1;
 371 
 372    struct config_2 {
 373       unsigned char ha_scsi_id: 3;
 374       unsigned char mapping_mode: 2;
 375       unsigned char bios_drive_number: 1;
 376       unsigned char tfr_port: 2;
 377       } config_2;
 378 
 379    char name[16];
 380 
 381    sprintf(name, "%s%d", driver_name, j);
 382 
 383    if(check_region(*port_base, REGION_SIZE)) {
 384       printk("%s: address 0x%03x in use, skipping probe.\n", 
 385              name, *port_base);
 386       return FALSE;
 387       }
 388 
 389    if (inb(*port_base + REG_PRODUCT_ID1) != PRODUCT_ID1) return FALSE;
 390 
 391    in_byte = inb(*port_base + REG_PRODUCT_ID2);
 392 
 393    if ((in_byte & 0xf0) != PRODUCT_ID2) return FALSE;
 394 
 395    *(char *)&config_1 = inb(*port_base + REG_CONFIG1);
 396    *(char *)&config_2 = inb(*port_base + REG_CONFIG2);
 397 
 398    irq = interrupt_table[config_1.interrupt];
 399    dma_channel = dma_channel_table[config_1.dma_channel];
 400    subversion = (in_byte & 0x0f);
 401 
 402    /* Board detected, allocate its IRQ if not already done */
 403    if ((irq >= MAX_IRQ) || ((irqlist[irq] == NO_IRQ) && request_irq
 404        (irq, u14_34f_interrupt_handler, SA_INTERRUPT, driver_name))) {
 405       printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
 406       return FALSE;
 407       }
 408 
 409    if (subversion == ISA && request_dma(dma_channel, driver_name)) {
 410       printk("%s: unable to allocate DMA channel %u, detaching.\n",
 411              name, dma_channel);
 412       free_irq(irq);
 413       return FALSE;
 414       }
 415 
 416    sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
 417 
 418    if (sh[j] == NULL) {
 419       printk("%s: unable to register host, detaching.\n", name);
 420 
 421       if (irqlist[irq] == NO_IRQ) free_irq(irq);
 422 
 423       if (subversion == ISA) free_dma(dma_channel);
 424 
 425       return FALSE;
 426       }
 427 
 428    sh[j]->io_port = *port_base;
 429    sh[j]->n_io_port = REGION_SIZE;
 430    sh[j]->base = bios_segment_table[config_1.bios_segment];
 431    sh[j]->irq = irq;
 432    sh[j]->sg_tablesize = MAX_SGLIST;
 433    sh[j]->this_id = config_2.ha_scsi_id;
 434    sh[j]->can_queue = MAX_MAILBOXES;
 435    sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
 436 
 437 #if defined(DEBUG_DETECT)
 438    {
 439    unsigned char sys_mask, lcl_mask;
 440 
 441    sys_mask = inb(sh[j]->io_port + REG_SYS_MASK);
 442    lcl_mask = inb(sh[j]->io_port + REG_LCL_MASK);
 443    printk("SYS_MASK 0x%x, LCL_MASK 0x%x.\n", sys_mask, lcl_mask);
 444    }
 445 #endif
 446 
 447    /* If BIOS is disabled, force enable interrupts */
 448    if (sh[j]->base == 0) outb(CMD_ENA_INTR, sh[j]->io_port + REG_SYS_MASK);
 449 
 450    /* Register the I/O space that we use */
 451    request_region(sh[j]->io_port, REGION_SIZE, driver_name);
 452 
 453    memset(HD(j), 0, sizeof(struct hostdata));
 454    HD(j)->heads = mapping_table[config_2.mapping_mode].heads;
 455    HD(j)->sectors = mapping_table[config_2.mapping_mode].sectors;
 456    HD(j)->subversion = subversion;
 457    HD(j)->board_number = j;
 458    irqlist[irq] = j;
 459 
 460    if (HD(j)->subversion == ESA) {
 461 
 462 #if defined (HAVE_OLD_UX4F_FIRMWARE)
 463       sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
 464 #endif
 465 
 466       sh[j]->dma_channel = NO_DMA;
 467       sh[j]->unchecked_isa_dma = FALSE;
 468       sprintf(BN(j), "U34F%d", j);
 469       }
 470    else {
 471       sh[j]->wish_block = TRUE;
 472 
 473 #if defined (HAVE_OLD_UX4F_FIRMWARE)
 474       sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
 475       sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
 476 #endif
 477 
 478       sh[j]->dma_channel = dma_channel;
 479       sh[j]->unchecked_isa_dma = TRUE;
 480       sprintf(BN(j), "U14F%d", j);
 481       disable_dma(dma_channel);
 482       clear_dma_ff(dma_channel);
 483       set_dma_mode(dma_channel, DMA_MODE_CASCADE);
 484       enable_dma(dma_channel);
 485       }
 486 
 487    if (HD(j)->subversion == ISA && !board_inquiry(j)) {
 488       HD(j)->board_id[40] = 0;
 489 
 490       if (strcmp(&HD(j)->board_id[32], "06000600")) {
 491          printk("%s: %s.\n", BN(j), &HD(j)->board_id[8]);
 492          printk("%s: firmware %s is outdated, FW PROM should be 28004-006.\n",
 493                 BN(j), &HD(j)->board_id[32]);
 494          sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
 495          sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
 496          }
 497       }
 498 
 499    printk("%s: PORT 0x%03x, BIOS 0x%05x, IRQ %u, DMA %u, SG %d, "\
 500           "Mbox %d, CmdLun %d, C%d.\n", BN(j), sh[j]->io_port, 
 501           (int)sh[j]->base, sh[j]->irq, 
 502           sh[j]->dma_channel, sh[j]->sg_tablesize, 
 503           sh[j]->can_queue, sh[j]->cmd_per_lun,
 504           sh[j]->hostt->use_clustering);
 505    return TRUE;
 506 }
 507 
 508 int u14_34f_detect (Scsi_Host_Template * tpnt) {
     /* [previous][next][first][last][top][bottom][index][help] */
 509    unsigned int j = 0, k, flags;
 510 
 511    ushort io_port[] = {
 512       0x330, 0x340, 0x230, 0x240, 0x210, 0x130, 0x140, 0x0
 513       };
 514 
 515    ushort *port_base = io_port;
 516 
 517    save_flags(flags);
 518    cli();
 519 
 520    for (k = 0; k < MAX_IRQ; k++) {
 521       irqlist[k] = NO_IRQ;
 522       calls[k] = 0;
 523       }
 524 
 525    for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
 526 
 527    while (*port_base) {
 528 
 529       if (j < MAX_BOARDS && port_detect(port_base, j, tpnt)) j++;
 530 
 531       port_base++;
 532       }
 533 
 534    if (j > 0) 
 535       printk("UltraStor 14F/34F: Copyright (C) 1994, 1995 Dario Ballabio.\n");
 536 
 537    restore_flags(flags);
 538    return j;
 539 }
 540 
 541 static inline void build_sg_list(struct mscp *cpp, Scsi_Cmnd *SCpnt) {
     /* [previous][next][first][last][top][bottom][index][help] */
 542    unsigned int k, data_len = 0;
 543    struct scatterlist * sgpnt;
 544 
 545    sgpnt = (struct scatterlist *) SCpnt->request_buffer;
 546 
 547    for (k = 0; k < SCpnt->use_sg; k++) {
 548       cpp->sglist[k].address = (unsigned int) sgpnt[k].address;
 549       cpp->sglist[k].num_bytes = sgpnt[k].length;
 550       data_len += sgpnt[k].length;
 551       }
 552 
 553    cpp->use_sg = SCpnt->use_sg;
 554    cpp->data_address = (unsigned int) cpp->sglist;
 555    cpp->data_len = data_len;
 556 }
 557 
 558 int u14_34f_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
     /* [previous][next][first][last][top][bottom][index][help] */
 559    unsigned int i, j, k, flags;
 560    struct mscp *cpp;
 561 
 562    save_flags(flags);
 563    cli();
 564    /* j is the board number */
 565    j = ((struct hostdata *) SCpnt->host->hostdata)->board_number;
 566 
 567    if (!done) panic("%s: qcomm, pid %ld, null done.\n", BN(j), SCpnt->pid);
 568 
 569    /* i is the mailbox number, look for the first free mailbox 
 570       starting from last_cp_used */
 571    i = HD(j)->last_cp_used + 1;
 572 
 573    for (k = 0; k < sh[j]->can_queue; k++, i++) {
 574 
 575       if (i >= sh[j]->can_queue) i = 0;
 576 
 577       if (HD(j)->cp_stat[i] == FREE) {
 578          HD(j)->last_cp_used = i;
 579          break;
 580          }
 581       }
 582 
 583    if (k == sh[j]->can_queue) {
 584       printk("%s: qcomm, no free mailbox, resetting.\n", BN(j));
 585 
 586       if (HD(j)->in_reset) 
 587          printk("%s: qcomm, already in reset.\n", BN(j));
 588       else if (u14_34f_reset(SCpnt) == SCSI_RESET_SUCCESS) 
 589          panic("%s: qcomm, SCSI_RESET_SUCCESS.\n", BN(j));
 590 
 591       SCpnt->result = DID_BUS_BUSY << 16; 
 592       SCpnt->host_scribble = NULL;
 593       printk("%s: qcomm, pid %ld, DID_BUS_BUSY, done.\n", BN(j), SCpnt->pid);
 594       restore_flags(flags);
 595       done(SCpnt);    
 596       return 0;
 597       }
 598 
 599    /* Set pointer to control packet structure */
 600    cpp = &HD(j)->cp[i];
 601 
 602    memset(cpp, 0, sizeof(struct mscp));
 603    SCpnt->scsi_done = done;
 604    cpp->index = i;
 605    SCpnt->host_scribble = (unsigned char *) &cpp->index;
 606 
 607    if (do_trace) printk("%s: qcomm, mbox %d, target %d, pid %ld.\n",
 608                         BN(j), i, SCpnt->target, SCpnt->pid);
 609 
 610    cpp->opcode = OP_SCSI;
 611    cpp->xdir = DTD_SCSI;
 612    cpp->target = SCpnt->target;
 613    cpp->lun = SCpnt->lun;
 614    cpp->SCpnt = SCpnt;
 615    cpp->sense_addr = (unsigned int) SCpnt->sense_buffer;
 616    cpp->sense_len = sizeof SCpnt->sense_buffer;
 617 
 618    if (SCpnt->use_sg) {
 619       cpp->sg = TRUE;
 620       build_sg_list(cpp, SCpnt);
 621       }
 622    else {
 623       cpp->data_address = (unsigned int)SCpnt->request_buffer;
 624       cpp->data_len = SCpnt->request_bufflen;
 625       }
 626 
 627    cpp->scsi_cdbs_len = SCpnt->cmd_len;
 628    memcpy(cpp->scsi_cdbs, SCpnt->cmnd, cpp->scsi_cdbs_len);
 629 
 630    if (wait_on_busy(sh[j]->io_port)) {
 631       SCpnt->result = DID_ERROR << 16;
 632       SCpnt->host_scribble = NULL;
 633       printk("%s: qcomm, target %d, pid %ld, adapter busy, DID_ERROR, done.\n", 
 634              BN(j), SCpnt->target, SCpnt->pid);
 635       restore_flags(flags);
 636       done(SCpnt);
 637       return 0;
 638       }
 639 
 640    /* Store pointer in OGM address bytes */
 641    outl((unsigned int)cpp, sh[j]->io_port + REG_OGM);
 642 
 643    /* Issue OGM interrupt */
 644    outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
 645 
 646    HD(j)->cp_stat[i] = IN_USE;
 647    restore_flags(flags);
 648    return 0;
 649 }
 650 
 651 int u14_34f_abort(Scsi_Cmnd *SCarg) {
     /* [previous][next][first][last][top][bottom][index][help] */
 652    unsigned int i, j, flags;
 653 
 654    save_flags(flags);
 655    cli();
 656    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
 657 
 658    if (SCarg->host_scribble == NULL) {
 659       printk("%s: abort, target %d, pid %ld inactive.\n",
 660              BN(j), SCarg->target, SCarg->pid);
 661       restore_flags(flags);
 662       return SCSI_ABORT_NOT_RUNNING;
 663       }
 664 
 665    i = *(unsigned int *)SCarg->host_scribble;
 666    printk("%s: abort, mbox %d, target %d, pid %ld.\n",
 667           BN(j), i, SCarg->target, SCarg->pid);
 668 
 669    if (i >= sh[j]->can_queue)
 670       panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
 671 
 672    if (wait_on_busy(sh[j]->io_port)) {
 673       printk("%s: abort, timeout error.\n", BN(j));
 674       restore_flags(flags);
 675       return SCSI_ABORT_ERROR;
 676       }
 677 
 678    if (HD(j)->cp_stat[i] == FREE) {
 679       printk("%s: abort, mbox %d is free.\n", BN(j), i);
 680       restore_flags(flags);
 681       return SCSI_ABORT_NOT_RUNNING;
 682       }
 683 
 684    if (HD(j)->cp_stat[i] == IN_USE) {
 685       printk("%s: abort, mbox %d is in use.\n", BN(j), i);
 686 
 687       if (SCarg != HD(j)->cp[i].SCpnt)
 688          panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
 689                BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
 690 
 691       restore_flags(flags);
 692       return SCSI_ABORT_SNOOZE;
 693       }
 694 
 695    if (HD(j)->cp_stat[i] == IN_RESET) {
 696       printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
 697       restore_flags(flags);
 698       return SCSI_ABORT_ERROR;
 699       }
 700 
 701    if (HD(j)->cp_stat[i] == LOCKED) {
 702       printk("%s: abort, mbox %d is locked.\n", BN(j), i);
 703       restore_flags(flags);
 704       return SCSI_ABORT_NOT_RUNNING;
 705       }
 706    restore_flags(flags);
 707    panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
 708 }
 709 
 710 int u14_34f_reset(Scsi_Cmnd * SCarg) {
     /* [previous][next][first][last][top][bottom][index][help] */
 711    unsigned int i, j, flags, time, k, limit = 0;
 712    int arg_done = FALSE;
 713    Scsi_Cmnd *SCpnt;
 714 
 715    save_flags(flags);
 716    cli();
 717    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
 718    printk("%s: reset, enter, target %d, pid %ld.\n", 
 719           BN(j), SCarg->target, SCarg->pid);
 720 
 721    if (SCarg->host_scribble == NULL)
 722       printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
 723 
 724    if (HD(j)->in_reset) {
 725       printk("%s: reset, exit, already in reset.\n", BN(j));
 726       restore_flags(flags);
 727       return SCSI_RESET_ERROR;
 728       }
 729 
 730    if (wait_on_busy(sh[j]->io_port)) {
 731       printk("%s: reset, exit, timeout error.\n", BN(j));
 732       restore_flags(flags);
 733       return SCSI_RESET_ERROR;
 734       }
 735 
 736    for (k = 0; k < MAX_TARGET; k++) HD(j)->target_reset[k] = TRUE;
 737 
 738    for (k = 0; k < MAX_TARGET; k++) HD(j)->target_time_out[k] = 0;
 739 
 740    for (i = 0; i < sh[j]->can_queue; i++) {
 741 
 742       if (HD(j)->cp_stat[i] == FREE) continue;
 743 
 744       if (HD(j)->cp_stat[i] == LOCKED) {
 745          HD(j)->cp_stat[i] = FREE;
 746          printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
 747          continue;
 748          }
 749 
 750       SCpnt = HD(j)->cp[i].SCpnt;
 751       HD(j)->cp_stat[i] = IN_RESET;
 752       printk("%s: reset, mbox %d in reset, pid %ld.\n",
 753              BN(j), i, SCpnt->pid);
 754 
 755       if (SCpnt == NULL)
 756          panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
 757 
 758       if (SCpnt->host_scribble == NULL)
 759          panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
 760 
 761       if (*(unsigned int *)SCpnt->host_scribble != i) 
 762          panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
 763 
 764       if (SCpnt->scsi_done == NULL) 
 765          panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
 766 
 767       if (SCpnt == SCarg) arg_done = TRUE;
 768       }
 769 
 770    if (wait_on_busy(sh[j]->io_port)) {
 771       printk("%s: reset, cannot reset, timeout error.\n", BN(j));
 772       restore_flags(flags);
 773       return SCSI_RESET_ERROR;
 774       }
 775 
 776    outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
 777    printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
 778 
 779 #if defined (DEBUG_RESET)
 780    do_trace = TRUE;
 781 #endif
 782 
 783    HD(j)->in_reset = TRUE;
 784    sti();
 785    time = jiffies;
 786    while (jiffies < (time + 100) && limit++ < 100000000);
 787    cli();
 788    printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
 789 
 790    for (i = 0; i < sh[j]->can_queue; i++) {
 791 
 792       /* Skip mailboxes already set free by interrupt */
 793       if (HD(j)->cp_stat[i] != IN_RESET) continue;
 794 
 795       SCpnt = HD(j)->cp[i].SCpnt;
 796       SCpnt->result = DID_RESET << 16;
 797       SCpnt->host_scribble = NULL;
 798 
 799       /* This mailbox is still waiting for its interrupt */
 800       HD(j)->cp_stat[i] = LOCKED;
 801 
 802       printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
 803              BN(j), i, SCpnt->pid);
 804       restore_flags(flags);
 805       SCpnt->scsi_done(SCpnt);
 806       cli();
 807       }
 808 
 809    HD(j)->in_reset = FALSE;
 810    do_trace = FALSE;
 811    restore_flags(flags);
 812 
 813    if (arg_done) {
 814       printk("%s: reset, exit, success.\n", BN(j));
 815       return SCSI_RESET_SUCCESS;
 816       }
 817    else {
 818       printk("%s: reset, exit, wakeup.\n", BN(j));
 819       return SCSI_RESET_PUNT;
 820       }
 821 }
 822 
 823 int u14_34f_biosparam(Disk * disk, int dev, int * dkinfo) {
     /* [previous][next][first][last][top][bottom][index][help] */
 824    unsigned int j = 0;
 825    int size = disk->capacity;
 826 
 827    dkinfo[0] = HD(j)->heads;
 828    dkinfo[1] = HD(j)->sectors;
 829    dkinfo[2] = size / (HD(j)->heads * HD(j)->sectors);
 830    return 0;
 831 }
 832 
 833 static void u14_34f_interrupt_handler(int irq, struct pt_regs * regs) {
     /* [previous][next][first][last][top][bottom][index][help] */
 834    Scsi_Cmnd *SCpnt;
 835    unsigned int i, j, k, flags, status, tstatus, loops, total_loops = 0;
 836    struct mscp *spp;
 837 
 838    save_flags(flags);
 839    cli();
 840 
 841    if (irqlist[irq] == NO_IRQ) {
 842       printk("%s, ihdlr, irq %d, unexpected interrupt.\n", driver_name, irq);
 843       restore_flags(flags);
 844       return;
 845       }
 846 
 847    if (do_trace) printk("%s: ihdlr, enter, irq %d, calls %d.\n", 
 848                         driver_name, irq, calls[irq]);
 849 
 850    /* Service all the boards configured on this irq */
 851    for (j = 0; sh[j] != NULL; j++) {
 852 
 853       if (sh[j]->irq != irq) continue;
 854 
 855       loops = 0;
 856 
 857       /* Loop until all interrupts for a board are serviced */
 858       while (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED) {
 859          total_loops++;
 860          loops++;
 861 
 862          if (do_trace) printk("%s: ihdlr, start service, count %d.\n",
 863                               BN(j), HD(j)->iocount);
 864 
 865          spp = (struct mscp *)inl(sh[j]->io_port + REG_ICM);
 866 
 867          /* Clear interrupt pending flag */
 868          outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
 869 
 870          i = spp - HD(j)->cp;
 871 
 872          if (i >= sh[j]->can_queue)
 873             panic("%s: ihdlr, invalid mscp address.\n", BN(j));
 874 
 875          if (HD(j)->cp_stat[i] == IGNORE) {
 876             HD(j)->cp_stat[i] = FREE;
 877             continue;
 878             }
 879          else if (HD(j)->cp_stat[i] == LOCKED) {
 880             HD(j)->cp_stat[i] = FREE;
 881             printk("%s: ihdlr, mbox %d unlocked, count %d.\n",
 882                    BN(j), i, HD(j)->iocount);
 883             continue;
 884             }
 885          else if (HD(j)->cp_stat[i] == FREE) {
 886             printk("%s: ihdlr, mbox %d is free, count %d.\n", 
 887                    BN(j), i, HD(j)->iocount);
 888             continue;
 889             }
 890          else if (HD(j)->cp_stat[i] == IN_RESET)
 891             printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
 892          else if (HD(j)->cp_stat[i] != IN_USE) 
 893             panic("%s: ihdlr, mbox %d, invalid cp_stat.\n", BN(j), i);
 894 
 895          HD(j)->cp_stat[i] = FREE;
 896          SCpnt = spp->SCpnt;
 897 
 898          if (SCpnt == NULL) 
 899             panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
 900 
 901          if (SCpnt->host_scribble == NULL) 
 902             panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n",
 903                   BN(j), i, SCpnt->pid, SCpnt);
 904 
 905          if (*(unsigned int *)SCpnt->host_scribble != i) 
 906             panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d,"\
 907                   " irq %d.\n", BN(j), i, SCpnt->pid, 
 908                   *(unsigned int *)SCpnt->host_scribble, irq);
 909 
 910          tstatus = status_byte(spp->target_status);
 911 
 912          switch (spp->adapter_status) {
 913             case ASOK:     /* status OK */
 914 
 915                /* Forces a reset if a disk drive keeps returning BUSY */
 916                if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE) 
 917                   status = DID_ERROR << 16;
 918 
 919                /* If there was a bus reset, redo operation on each target */
 920                else if (tstatus != GOOD
 921                         && SCpnt->device->type == TYPE_DISK
 922                         && HD(j)->target_reset[SCpnt->target])
 923                   status = DID_BUS_BUSY << 16;
 924 
 925                /* Works around a flaw in scsi.c */
 926                else if (tstatus == CHECK_CONDITION
 927                         && SCpnt->device->type == TYPE_DISK
 928                         && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
 929                   status = DID_BUS_BUSY << 16;
 930 
 931                else
 932                   status = DID_OK << 16;
 933 
 934                if (tstatus == GOOD)
 935                   HD(j)->target_reset[SCpnt->target] = FALSE;
 936 
 937                if (spp->target_status && SCpnt->device->type == TYPE_DISK)
 938                   printk("%s: ihdlr, target %d:%d, pid %ld, target_status "\
 939                          "0x%x, sense key 0x%x.\n", BN(j), 
 940                          SCpnt->target, SCpnt->lun, SCpnt->pid,
 941                          spp->target_status, SCpnt->sense_buffer[2]);
 942 
 943                HD(j)->target_time_out[SCpnt->target] = 0;
 944 
 945                break;
 946             case ASST:     /* Selection Time Out */
 947 
 948                if (HD(j)->target_time_out[SCpnt->target] > 1)
 949                   status = DID_ERROR << 16;
 950                else {
 951                   status = DID_TIME_OUT << 16;
 952                   HD(j)->target_time_out[SCpnt->target]++;
 953                   }
 954 
 955                break;
 956             case 0x92:     /* Data over/under-run */
 957             case 0x93:     /* Unexpected bus free */
 958             case 0x94:     /* Target bus phase sequence failure */
 959             case 0x96:     /* Illegal SCSI command */
 960             case 0xa3:     /* SCSI bus reset error */
 961 
 962                if (SCpnt->device->type != TYPE_TAPE)
 963                   status = DID_BUS_BUSY << 16;
 964                else
 965                   status = DID_ERROR << 16;
 966 
 967                for (k = 0; k < MAX_TARGET; k++) 
 968                   HD(j)->target_reset[k] = TRUE;
 969 
 970                break;
 971             case 0x01:     /* Invalid command */
 972             case 0x02:     /* Invalid parameters */
 973             case 0x03:     /* Invalid data list */
 974             case 0x84:     /* SCSI bus abort error */
 975             case 0x9b:     /* Auto request sense error */
 976             case 0x9f:     /* Unexpected command complete message error */
 977             case 0xff:     /* Invalid parameter in the S/G list */
 978             default:
 979                status = DID_ERROR << 16;
 980                break;
 981             }
 982 
 983          SCpnt->result = status | spp->target_status;
 984          HD(j)->iocount++;
 985 
 986          if (loops > 1) HD(j)->multicount++;
 987 
 988 #if defined (DEBUG_INTERRUPT)
 989          if (SCpnt->result || do_trace) 
 990 #else
 991          if ((spp->adapter_status != ASOK && HD(j)->iocount >  1000) ||
 992              (spp->adapter_status != ASOK && 
 993               spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
 994              do_trace)
 995 #endif
 996             printk("%s: ihdlr, mbox %d, err 0x%x:%x,"\
 997                    " target %d:%d, pid %ld, count %d.\n",
 998                    BN(j), i, spp->adapter_status, spp->target_status,
 999                    SCpnt->target, SCpnt->lun, SCpnt->pid, HD(j)->iocount);
1000 
1001          /* Set the command state to inactive */
1002          SCpnt->host_scribble = NULL;
1003 
1004          restore_flags(flags);
1005          SCpnt->scsi_done(SCpnt);
1006          cli();
1007 
1008          }   /* Multiple command loop */
1009 
1010       }   /* Boards loop */
1011 
1012    calls[irq]++;
1013 
1014    if (total_loops == 0) 
1015      printk("%s: ihdlr, irq %d, no command completed, calls %d.\n",
1016             driver_name, irq, calls[irq]);
1017 
1018    if (do_trace) printk("%s: ihdlr, exit, irq %d, calls %d.\n",
1019                         driver_name, irq, calls[irq]);
1020 
1021 #if defined (DEBUG_STATISTICS)
1022    if ((calls[irq] % 100000) == 10000)
1023       for (j = 0; sh[j] != NULL; j++)
1024          printk("%s: ihdlr, calls %d, count %d, multi %d.\n", BN(j),
1025                 calls[(sh[j]->irq)], HD(j)->iocount, HD(j)->multicount);
1026 #endif
1027 
1028    restore_flags(flags);
1029    return;
1030 }
1031 
1032 #if defined(MODULE)
1033 Scsi_Host_Template driver_template = ULTRASTOR_14_34F;
1034 
1035 #include "scsi_module.c"
1036 #endif

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