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       return SCSI_ABORT_NOT_RUNNING;
 662       }
 663 
 664    i = *(unsigned int *)SCarg->host_scribble;
 665    printk("%s: abort, mbox %d, target %d, pid %ld.\n",
 666           BN(j), i, SCarg->target, SCarg->pid);
 667 
 668    if (i >= sh[j]->can_queue)
 669       panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
 670 
 671    if (wait_on_busy(sh[j]->io_port)) {
 672       printk("%s: abort, timeout error.\n", BN(j));
 673       restore_flags(flags);
 674       return SCSI_ABORT_ERROR;
 675       }
 676 
 677    if (HD(j)->cp_stat[i] == FREE) {
 678       printk("%s: abort, mbox %d is free.\n", BN(j), i);
 679       restore_flags(flags);
 680       return SCSI_ABORT_NOT_RUNNING;
 681       }
 682 
 683    if (HD(j)->cp_stat[i] == IN_USE) {
 684       printk("%s: abort, mbox %d is in use.\n", BN(j), i);
 685 
 686       if (SCarg != HD(j)->cp[i].SCpnt)
 687          panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
 688                BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
 689 
 690       restore_flags(flags);
 691       return SCSI_ABORT_SNOOZE;
 692       }
 693 
 694    if (HD(j)->cp_stat[i] == IN_RESET) {
 695       printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
 696       restore_flags(flags);
 697       return SCSI_ABORT_ERROR;
 698       }
 699 
 700    if (HD(j)->cp_stat[i] == LOCKED) {
 701       printk("%s: abort, mbox %d is locked.\n", BN(j), i);
 702       restore_flags(flags);
 703       return SCSI_ABORT_NOT_RUNNING;
 704       }
 705    else
 706       panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
 707 }
 708 
 709 int u14_34f_reset(Scsi_Cmnd * SCarg) {
     /* [previous][next][first][last][top][bottom][index][help] */
 710    unsigned int i, j, flags, time, k, limit = 0;
 711    int arg_done = FALSE;
 712    Scsi_Cmnd *SCpnt;
 713 
 714    save_flags(flags);
 715    cli();
 716    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
 717    printk("%s: reset, enter, target %d, pid %ld.\n", 
 718           BN(j), SCarg->target, SCarg->pid);
 719 
 720    if (SCarg->host_scribble == NULL)
 721       printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
 722 
 723    if (HD(j)->in_reset) {
 724       printk("%s: reset, exit, already in reset.\n", BN(j));
 725       restore_flags(flags);
 726       return SCSI_RESET_ERROR;
 727       }
 728 
 729    if (wait_on_busy(sh[j]->io_port)) {
 730       printk("%s: reset, exit, timeout error.\n", BN(j));
 731       restore_flags(flags);
 732       return SCSI_RESET_ERROR;
 733       }
 734 
 735    for (k = 0; k < MAX_TARGET; k++) HD(j)->target_reset[k] = TRUE;
 736 
 737    for (k = 0; k < MAX_TARGET; k++) HD(j)->target_time_out[k] = 0;
 738 
 739    for (i = 0; i < sh[j]->can_queue; i++) {
 740 
 741       if (HD(j)->cp_stat[i] == FREE) continue;
 742 
 743       if (HD(j)->cp_stat[i] == LOCKED) {
 744          HD(j)->cp_stat[i] = FREE;
 745          printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
 746          continue;
 747          }
 748 
 749       SCpnt = HD(j)->cp[i].SCpnt;
 750       HD(j)->cp_stat[i] = IN_RESET;
 751       printk("%s: reset, mbox %d in reset, pid %ld.\n",
 752              BN(j), i, SCpnt->pid);
 753 
 754       if (SCpnt == NULL)
 755          panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
 756 
 757       if (SCpnt->host_scribble == NULL)
 758          panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
 759 
 760       if (*(unsigned int *)SCpnt->host_scribble != i) 
 761          panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
 762 
 763       if (SCpnt->scsi_done == NULL) 
 764          panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
 765 
 766       if (SCpnt == SCarg) arg_done = TRUE;
 767       }
 768 
 769    if (wait_on_busy(sh[j]->io_port)) {
 770       printk("%s: reset, cannot reset, timeout error.\n", BN(j));
 771       restore_flags(flags);
 772       return SCSI_RESET_ERROR;
 773       }
 774 
 775    outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
 776    printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
 777 
 778 #if defined (DEBUG_RESET)
 779    do_trace = TRUE;
 780 #endif
 781 
 782    HD(j)->in_reset = TRUE;
 783    sti();
 784    time = jiffies;
 785    while (jiffies < (time + 100) && limit++ < 100000000);
 786    cli();
 787    printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
 788 
 789    for (i = 0; i < sh[j]->can_queue; i++) {
 790 
 791       /* Skip mailboxes already set free by interrupt */
 792       if (HD(j)->cp_stat[i] != IN_RESET) continue;
 793 
 794       SCpnt = HD(j)->cp[i].SCpnt;
 795       SCpnt->result = DID_RESET << 16;
 796       SCpnt->host_scribble = NULL;
 797 
 798       /* This mailbox is still waiting for its interrupt */
 799       HD(j)->cp_stat[i] = LOCKED;
 800 
 801       printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
 802              BN(j), i, SCpnt->pid);
 803       restore_flags(flags);
 804       SCpnt->scsi_done(SCpnt);
 805       cli();
 806       }
 807 
 808    HD(j)->in_reset = FALSE;
 809    do_trace = FALSE;
 810    restore_flags(flags);
 811 
 812    if (arg_done) {
 813       printk("%s: reset, exit, success.\n", BN(j));
 814       return SCSI_RESET_SUCCESS;
 815       }
 816    else {
 817       printk("%s: reset, exit, wakeup.\n", BN(j));
 818       return SCSI_RESET_PUNT;
 819       }
 820 }
 821 
 822 int u14_34f_biosparam(Disk * disk, int dev, int * dkinfo) {
     /* [previous][next][first][last][top][bottom][index][help] */
 823    unsigned int j = 0;
 824    int size = disk->capacity;
 825 
 826    dkinfo[0] = HD(j)->heads;
 827    dkinfo[1] = HD(j)->sectors;
 828    dkinfo[2] = size / (HD(j)->heads * HD(j)->sectors);
 829    return 0;
 830 }
 831 
 832 static void u14_34f_interrupt_handler(int irq, struct pt_regs * regs) {
     /* [previous][next][first][last][top][bottom][index][help] */
 833    Scsi_Cmnd *SCpnt;
 834    unsigned int i, j, k, flags, status, tstatus, loops, total_loops = 0;
 835    struct mscp *spp;
 836 
 837    save_flags(flags);
 838    cli();
 839 
 840    if (irqlist[irq] == NO_IRQ) {
 841       printk("%s, ihdlr, irq %d, unexpected interrupt.\n", driver_name, irq);
 842       restore_flags(flags);
 843       return;
 844       }
 845 
 846    if (do_trace) printk("%s: ihdlr, enter, irq %d, calls %d.\n", 
 847                         driver_name, irq, calls[irq]);
 848 
 849    /* Service all the boards configured on this irq */
 850    for (j = 0; sh[j] != NULL; j++) {
 851 
 852       if (sh[j]->irq != irq) continue;
 853 
 854       loops = 0;
 855 
 856       /* Loop until all interrupts for a board are serviced */
 857       while (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED) {
 858          total_loops++;
 859          loops++;
 860 
 861          if (do_trace) printk("%s: ihdlr, start service, count %d.\n",
 862                               BN(j), HD(j)->iocount);
 863 
 864          spp = (struct mscp *)inl(sh[j]->io_port + REG_ICM);
 865 
 866          /* Clear interrupt pending flag */
 867          outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
 868 
 869          i = spp - HD(j)->cp;
 870 
 871          if (i >= sh[j]->can_queue)
 872             panic("%s: ihdlr, invalid mscp address.\n", BN(j));
 873 
 874          if (HD(j)->cp_stat[i] == IGNORE) {
 875             HD(j)->cp_stat[i] = FREE;
 876             continue;
 877             }
 878          else if (HD(j)->cp_stat[i] == LOCKED) {
 879             HD(j)->cp_stat[i] = FREE;
 880             printk("%s: ihdlr, mbox %d unlocked, count %d.\n",
 881                    BN(j), i, HD(j)->iocount);
 882             continue;
 883             }
 884          else if (HD(j)->cp_stat[i] == FREE) {
 885             printk("%s: ihdlr, mbox %d is free, count %d.\n", 
 886                    BN(j), i, HD(j)->iocount);
 887             continue;
 888             }
 889          else if (HD(j)->cp_stat[i] == IN_RESET)
 890             printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
 891          else if (HD(j)->cp_stat[i] != IN_USE) 
 892             panic("%s: ihdlr, mbox %d, invalid cp_stat.\n", BN(j), i);
 893 
 894          HD(j)->cp_stat[i] = FREE;
 895          SCpnt = spp->SCpnt;
 896 
 897          if (SCpnt == NULL) 
 898             panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
 899 
 900          if (SCpnt->host_scribble == NULL) 
 901             panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n",
 902                   BN(j), i, SCpnt->pid, SCpnt);
 903 
 904          if (*(unsigned int *)SCpnt->host_scribble != i) 
 905             panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d,"\
 906                   " irq %d.\n", BN(j), i, SCpnt->pid, 
 907                   *(unsigned int *)SCpnt->host_scribble, irq);
 908 
 909          tstatus = status_byte(spp->target_status);
 910 
 911          switch (spp->adapter_status) {
 912             case ASOK:     /* status OK */
 913 
 914                /* Forces a reset if a disk drive keeps returning BUSY */
 915                if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE) 
 916                   status = DID_ERROR << 16;
 917 
 918                /* If there was a bus reset, redo operation on each target */
 919                else if (tstatus != GOOD
 920                         && SCpnt->device->type == TYPE_DISK
 921                         && HD(j)->target_reset[SCpnt->target])
 922                   status = DID_BUS_BUSY << 16;
 923 
 924                /* Works around a flaw in scsi.c */
 925                else if (tstatus == CHECK_CONDITION
 926                         && SCpnt->device->type == TYPE_DISK
 927                         && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
 928                   status = DID_BUS_BUSY << 16;
 929 
 930                else
 931                   status = DID_OK << 16;
 932 
 933                if (tstatus == GOOD)
 934                   HD(j)->target_reset[SCpnt->target] = FALSE;
 935 
 936                if (spp->target_status && SCpnt->device->type == TYPE_DISK)
 937                   printk("%s: ihdlr, target %d:%d, pid %ld, target_status "\
 938                          "0x%x, sense key 0x%x.\n", BN(j), 
 939                          SCpnt->target, SCpnt->lun, SCpnt->pid,
 940                          spp->target_status, SCpnt->sense_buffer[2]);
 941 
 942                HD(j)->target_time_out[SCpnt->target] = 0;
 943 
 944                break;
 945             case ASST:     /* Selection Time Out */
 946 
 947                if (HD(j)->target_time_out[SCpnt->target] > 1)
 948                   status = DID_ERROR << 16;
 949                else {
 950                   status = DID_TIME_OUT << 16;
 951                   HD(j)->target_time_out[SCpnt->target]++;
 952                   }
 953 
 954                break;
 955             case 0x92:     /* Data over/under-run */
 956             case 0x93:     /* Unexpected bus free */
 957             case 0x94:     /* Target bus phase sequence failure */
 958             case 0x96:     /* Illegal SCSI command */
 959             case 0xa3:     /* SCSI bus reset error */
 960 
 961                if (SCpnt->device->type != TYPE_TAPE)
 962                   status = DID_BUS_BUSY << 16;
 963                else
 964                   status = DID_ERROR << 16;
 965 
 966                for (k = 0; k < MAX_TARGET; k++) 
 967                   HD(j)->target_reset[k] = TRUE;
 968 
 969                break;
 970             case 0x01:     /* Invalid command */
 971             case 0x02:     /* Invalid parameters */
 972             case 0x03:     /* Invalid data list */
 973             case 0x84:     /* SCSI bus abort error */
 974             case 0x9b:     /* Auto request sense error */
 975             case 0x9f:     /* Unexpected command complete message error */
 976             case 0xff:     /* Invalid parameter in the S/G list */
 977             default:
 978                status = DID_ERROR << 16;
 979                break;
 980             }
 981 
 982          SCpnt->result = status | spp->target_status;
 983          HD(j)->iocount++;
 984 
 985          if (loops > 1) HD(j)->multicount++;
 986 
 987 #if defined (DEBUG_INTERRUPT)
 988          if (SCpnt->result || do_trace) 
 989 #else
 990          if ((spp->adapter_status != ASOK && HD(j)->iocount >  1000) ||
 991              (spp->adapter_status != ASOK && 
 992               spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
 993              do_trace)
 994 #endif
 995             printk("%s: ihdlr, mbox %d, err 0x%x:%x,"\
 996                    " target %d:%d, pid %ld, count %d.\n",
 997                    BN(j), i, spp->adapter_status, spp->target_status,
 998                    SCpnt->target, SCpnt->lun, SCpnt->pid, HD(j)->iocount);
 999 
1000          /* Set the command state to inactive */
1001          SCpnt->host_scribble = NULL;
1002 
1003          restore_flags(flags);
1004          SCpnt->scsi_done(SCpnt);
1005          cli();
1006 
1007          }   /* Multiple command loop */
1008 
1009       }   /* Boards loop */
1010 
1011    calls[irq]++;
1012 
1013    if (total_loops == 0) 
1014      printk("%s: ihdlr, irq %d, no command completed, calls %d.\n",
1015             driver_name, irq, calls[irq]);
1016 
1017    if (do_trace) printk("%s: ihdlr, exit, irq %d, calls %d.\n",
1018                         driver_name, irq, calls[irq]);
1019 
1020 #if defined (DEBUG_STATISTICS)
1021    if ((calls[irq] % 100000) == 10000)
1022       for (j = 0; sh[j] != NULL; j++)
1023          printk("%s: ihdlr, calls %d, count %d, multi %d.\n", BN(j),
1024                 calls[(sh[j]->irq)], HD(j)->iocount, HD(j)->multicount);
1025 #endif
1026 
1027    restore_flags(flags);
1028    return;
1029 }
1030 
1031 #if defined(MODULE)
1032 Scsi_Host_Template driver_template = ULTRASTOR_14_34F;
1033 
1034 #include "scsi_module.c"
1035 #endif

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