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

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