root/drivers/scsi/ultrastor.c

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

DEFINITIONS

This source file includes following definitions.
  1. find_and_clear_bit_16
  2. xchgb
  3. log_ultrastor_abort
  4. ultrastor_14f_detect
  5. ARRAY_SIZE
  6. request_irq
  7. request_dma
  8. ultrastor_24f_detect
  9. ultrastor_detect
  10. ultrastor_info
  11. build_sg_list
  12. ultrastor_queuecommand
  13. ultrastor_abort
  14. ultrastor_reset
  15. ultrastor_biosparam
  16. ultrastor_interrupt

   1 /*
   2  *      ultrastor.c     Copyright (C) 1992 David B. Gentzel
   3  *      Low-level SCSI driver for UltraStor 14F, 24F, and 34F
   4  *      by David B. Gentzel, Whitfield Software Services, Carnegie, PA
   5  *          (gentzel@nova.enet.dec.com)
   6  *  scatter/gather added by Scott Taylor (n217cg@tamuts.tamu.edu)
   7  *  24F and multiple command support by John F. Carr (jfc@athena.mit.edu)
   8  *    John's work modified by Caleb Epstein (cae@jpmorgan.com) and 
   9  *    Eric Youngdale (ericy@cais.com).
  10  *      Thanks to UltraStor for providing the necessary documentation
  11  */
  12 
  13 /*
  14  * TODO:
  15  *      1. Find out why scatter/gather is limited to 16 requests per command.
  16  *         This is fixed, at least on the 24F, as of version 1.12 - CAE.
  17  *      2. Look at command linking (mscp.command_link and
  18  *         mscp.command_link_id).  (Does not work with many disks, 
  19  *                              and no performance increase.  ERY).
  20  *      3. Allow multiple adapters.
  21  */
  22 
  23 /*
  24  * NOTES:
  25  *    The UltraStor 14F, 24F, and 34F are a family of intelligent, high
  26  *    performance SCSI-2 host adapters.  They all support command queueing
  27  *    and scatter/gather I/O.  Some of them can also emulate the standard
  28  *    WD1003 interface for use with OS's which don't support SCSI.  Here
  29  *    is the scoop on the various models:
  30  *      14F - ISA first-party DMA HA with floppy support and WD1003 emulation.
  31  *      14N - ISA HA with floppy support.  I think that this is a non-DMA
  32  *            HA.  Nothing further known.
  33  *      24F - EISA Bus Master HA with floppy support and WD1003 emulation.
  34  *      34F - VL-Bus Bus Master HA with floppy support (no WD1003 emulation).
  35  *
  36  *    The 14F, 24F, and 34F are supported by this driver.
  37  *
  38  *    Places flagged with a triple question-mark are things which are either
  39  *    unfinished, questionable, or wrong.
  40  */
  41 
  42 /* Changes from version 1.11 alpha to 1.12
  43  *
  44  * Increased the size of the scatter-gather list to 33 entries for
  45  * the 24F adapter (it was 16).  I don't have the specs for the 14F
  46  * or the 34F, so they may support larger s-g lists as well.
  47  *
  48  * Caleb Epstein <cae@jpmorgan.com>
  49  */
  50 
  51 /* Changes from version 1.9 to 1.11
  52  *
  53  * Patches to bring this driver up to speed with the default kernel
  54  * driver which supports only the 14F and 34F adapters.  This version
  55  * should compile cleanly into 0.99.13, 0.99.12 and probably 0.99.11.
  56  *
  57  * Fixes from Eric Youngdale to fix a few possible race conditions and
  58  * several problems with bit testing operations (insufficient
  59  * parentheses).
  60  *
  61  * Removed the ultrastor_abort() and ultrastor_reset() functions
  62  * (enclosed them in #if 0 / #endif).  These functions, at least on
  63  * the 24F, cause the SCSI bus to do odd things and generally lead to
  64  * kernel panics and machine hangs.  This is like the Adaptec code.
  65  *
  66  * Use check/snarf_region for 14f, 34f to avoid I/O space address conflicts.
  67  */
  68 
  69 /* Changes from version 1.8 to version 1.9
  70  *
  71  *  0.99.11 patches (cae@jpmorgan.com) */
  72 
  73 /* Changes from version 1.7 to version 1.8
  74  *
  75  * Better error reporting.
  76  */
  77 
  78 /* Changes from version 1.6 to version 1.7
  79  *
  80  * Removed CSIR command code.
  81  *
  82  * Better race condition avoidance (xchgb function added).
  83  *
  84  * Set ICM and OGM status to zero at probe (24F)
  85  *
  86  * reset sends soft reset to UltraStor adapter
  87  *
  88  * reset adapter if adapter interrupts with an invalid MSCP address
  89  *
  90  * handle aborted command interrupt (24F)
  91  *
  92  */
  93 
  94 /* Changes from version 1.5 to version 1.6:
  95  *
  96  * Read MSCP address from ICM _before_ clearing the interrupt flag.
  97  * This fixes a race condition.
  98  */
  99 
 100 /* Changes from version 1.4 to version 1.5:
 101  *
 102  * Abort now calls done when multiple commands are enabled.
 103  *
 104  * Clear busy when aborted command finishes, not when abort is called.
 105  *
 106  * More debugging messages for aborts.
 107  */
 108 
 109 /* Changes from version 1.3 to version 1.4:
 110  *
 111  * Enable automatic request of sense data on error (requires newer version
 112  * of scsi.c to be useful).
 113  *
 114  * Fix PORT_OVERRIDE for 14F.
 115  *
 116  * Fix abort and reset to work properly (config.aborted wasn't cleared
 117  * after it was tested, so after a command abort no further commands would
 118  * work).
 119  *
 120  * Boot time test to enable SCSI bus reset (defaults to not allowing reset).
 121  *
 122  * Fix test for OGM busy -- the busy bit is in different places on the 24F.
 123  *
 124  * Release ICM slot by clearing first byte on 24F.
 125  */
 126 
 127 #include <linux/stddef.h>
 128 #include <linux/string.h>
 129 #include <linux/sched.h>
 130 #include <linux/kernel.h>
 131 #include <linux/ioport.h>
 132 
 133 #include <asm/io.h>
 134 #include <asm/bitops.h>
 135 #include <asm/system.h>
 136 #include <asm/dma.h>
 137 
 138 #define ULTRASTOR_PRIVATE       /* Get the private stuff from ultrastor.h */
 139 #include "../block/blk.h"
 140 #include "scsi.h"
 141 #include "hosts.h"
 142 #include "ultrastor.h"
 143 #include "sd.h"
 144 
 145 #define FALSE 0
 146 #define TRUE 1
 147 
 148 #ifndef ULTRASTOR_DEBUG
 149 #define ULTRASTOR_DEBUG (UD_ABORT|UD_CSIR|UD_RESET)
 150 #endif
 151 
 152 #define VERSION "1.12"
 153 
 154 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr)[0])
 155 
 156 #define PACKED          __attribute__((packed))
 157 #define ALIGNED(x)      __attribute__((aligned(x)))
 158 
 159 
 160 /* The 14F uses an array of 4-byte ints for its scatter/gather list.
 161    The data can be unaligned, but need not be.  It's easier to give
 162    the list normal alignment since it doesn't need to fit into a
 163    packed structure.  */
 164 
 165 typedef struct {
 166   unsigned int address;
 167   unsigned int num_bytes;
 168 } ultrastor_sg_list;
 169 
 170 
 171 /* MailBox SCSI Command Packet.  Basic command structure for communicating
 172    with controller. */
 173 struct mscp {
 174   unsigned char opcode: 3;              /* type of command */
 175   unsigned char xdir: 2;                /* data transfer direction */
 176   unsigned char dcn: 1;         /* disable disconnect */
 177   unsigned char ca: 1;          /* use cache (if available) */
 178   unsigned char sg: 1;          /* scatter/gather operation */
 179   unsigned char target_id: 3;           /* target SCSI id */
 180   unsigned char ch_no: 2;               /* SCSI channel (always 0 for 14f) */
 181   unsigned char lun: 3;         /* logical unit number */
 182   unsigned int transfer_data PACKED;    /* transfer data pointer */
 183   unsigned int transfer_data_length PACKED;     /* length in bytes */
 184   unsigned int command_link PACKED;     /* for linking command chains */
 185   unsigned char scsi_command_link_id;   /* identifies command in chain */
 186   unsigned char number_of_sg_list;      /* (if sg is set) 8 bytes per list */
 187   unsigned char length_of_sense_byte;
 188   unsigned char length_of_scsi_cdbs;    /* 6, 10, or 12 */
 189   unsigned char scsi_cdbs[12];  /* SCSI commands */
 190   unsigned char adapter_status; /* non-zero indicates HA error */
 191   unsigned char target_status;  /* non-zero indicates target error */
 192   unsigned int sense_data PACKED;
 193   /* The following fields are for software only.  They are included in
 194      the MSCP structure because they are associated with SCSI requests.  */
 195   void (*done)(Scsi_Cmnd *);
 196   Scsi_Cmnd *SCint;
 197   ultrastor_sg_list sglist[ULTRASTOR_24F_MAX_SG]; /* use larger size for 24F */
 198 };
 199 
 200 
 201 /* Port addresses (relative to the base address) */
 202 #define U14F_PRODUCT_ID(port) ((port) + 0x4)
 203 #define CONFIG(port) ((port) + 0x6)
 204 
 205 /* Port addresses relative to the doorbell base address.  */
 206 #define LCL_DOORBELL_MASK(port) ((port) + 0x0)
 207 #define LCL_DOORBELL_INTR(port) ((port) + 0x1)
 208 #define SYS_DOORBELL_MASK(port) ((port) + 0x2)
 209 #define SYS_DOORBELL_INTR(port) ((port) + 0x3)
 210 
 211 
 212 /* Used to store configuration info read from config i/o registers.  Most of
 213    this is not used yet, but might as well save it.
 214    
 215    This structure also holds port addresses that are not at the same offset
 216    on the 14F and 24F.
 217    
 218    This structure holds all data that must be duplicated to support multiple
 219    adapters.  */
 220 
 221 static struct ultrastor_config
 222 {
 223   unsigned short port_address;          /* base address of card */
 224   unsigned short doorbell_address;      /* base address of doorbell CSRs */
 225   unsigned short ogm_address;           /* base address of OGM */
 226   unsigned short icm_address;           /* base address of ICM */
 227   const void *bios_segment;
 228   unsigned char interrupt: 4;
 229   unsigned char dma_channel: 3;
 230   unsigned char bios_drive_number: 1;
 231   unsigned char heads;
 232   unsigned char sectors;
 233   unsigned char ha_scsi_id: 3;
 234   unsigned char subversion: 4;
 235   unsigned char revision;
 236   /* The slot number is used to distinguish the 24F (slot != 0) from
 237      the 14F and 34F (slot == 0). */
 238   unsigned char slot;
 239 
 240 #ifdef PRINT_U24F_VERSION
 241   volatile int csir_done;
 242 #endif
 243 
 244   /* A pool of MSCP structures for this adapter, and a bitmask of
 245      busy structures.  (If ULTRASTOR_14F_MAX_CMDS == 1, a 1 byte
 246      busy flag is used instead.)  */
 247 
 248 #if ULTRASTOR_MAX_CMDS == 1
 249   unsigned char mscp_busy;
 250 #else
 251   unsigned short mscp_free;
 252 #endif
 253   volatile unsigned char aborted[ULTRASTOR_MAX_CMDS];
 254   struct mscp mscp[ULTRASTOR_MAX_CMDS];
 255 } config = {0};
 256 
 257 /* Set this to 1 to reset the SCSI bus on error.  */
 258 int ultrastor_bus_reset = 0;
 259 
 260 
 261 /* Allowed BIOS base addresses (NULL indicates reserved) */
 262 static const void *const bios_segment_table[8] = {
 263   NULL,      (void *)0xC4000, (void *)0xC8000, (void *)0xCC000,
 264   (void *)0xD0000, (void *)0xD4000, (void *)0xD8000, (void *)0xDC000,
 265 };
 266 
 267 /* Allowed IRQs for 14f */
 268 static const unsigned char interrupt_table_14f[4] = { 15, 14, 11, 10 };
 269 
 270 /* Allowed DMA channels for 14f (0 indicates reserved) */
 271 static const unsigned char dma_channel_table_14f[4] = { 5, 6, 7, 0 };
 272 
 273 /* Head/sector mappings allowed by 14f */
 274 static const struct {
 275   unsigned char heads;
 276   unsigned char sectors;
 277 } mapping_table[4] = { { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 } };
 278 
 279 #ifndef PORT_OVERRIDE
 280 /* ??? A probe of address 0x310 screws up NE2000 cards */
 281 static const unsigned short ultrastor_ports_14f[] = {
 282   0x330, 0x340, /*0x310,*/ 0x230, 0x240, 0x210, 0x130, 0x140,
 283 };
 284 #endif
 285 
 286 static void ultrastor_interrupt(int cpl);
 287 static inline void build_sg_list(struct mscp *, Scsi_Cmnd *SCpnt);
 288 
 289 
 290 static inline int find_and_clear_bit_16(unsigned short *field)
     /* [previous][next][first][last][top][bottom][index][help] */
 291 {
 292   int rv;
 293   unsigned long flags;
 294 
 295   save_flags(flags);
 296   cli();
 297   if (*field == 0) panic("No free mscp");
 298   asm("xorl %0,%0\n0:\tbsfw %1,%w0\n\tbtr %0,%1\n\tjnc 0b"
 299       : "=&r" (rv), "=m" (*field) : "1" (*field));
 300   restore_flags(flags);
 301   return rv;
 302 }
 303 
 304 /* This has been re-implemented with the help of Richard Earnshaw,
 305    <rwe@pegasus.esprit.ec.org> and works with gcc-2.5.8 and gcc-2.6.0.
 306    The instability noted by jfc below appears to be a bug in
 307    gcc-2.5.x when compiling w/o optimization.  --Caleb
 308 
 309    This asm is fragile: it doesn't work without the casts and it may
 310    not work without optimization.  Maybe I should add a swap builtin
 311    to gcc.  --jfc  */
 312 static inline unsigned char xchgb(unsigned char reg,
     /* [previous][next][first][last][top][bottom][index][help] */
 313                                   volatile unsigned char *mem)
 314 {
 315   __asm__ ("xchgb %0,%1" : "=q" (reg), "=m" (*mem) : "0" (reg));
 316   return reg;
 317 }
 318 
 319 #if ULTRASTOR_DEBUG & (UD_COMMAND | UD_ABORT)
 320 
 321 static void log_ultrastor_abort(register struct ultrastor_config *config,
     /* [previous][next][first][last][top][bottom][index][help] */
 322                                 int command)
 323 {
 324   static char fmt[80] = "abort %d (%x); MSCP free pool: %x;";
 325   register int i;
 326   int flags;
 327   save_flags(flags);
 328   cli();
 329 
 330   for (i = 0; i < ULTRASTOR_MAX_CMDS; i++)
 331     {
 332       fmt[20 + i*2] = ' ';
 333       if (! (config->mscp_free & (1 << i)))
 334         fmt[21 + i*2] = '0' + config->mscp[i].target_id;
 335       else
 336         fmt[21 + i*2] = '-';
 337     }
 338   fmt[20 + ULTRASTOR_MAX_CMDS * 2] = '\n';
 339   fmt[21 + ULTRASTOR_MAX_CMDS * 2] = 0;
 340   printk(fmt, command, &config->mscp[command], config->mscp_free);
 341   restore_flags(flags);
 342 }
 343 #endif
 344 
 345 static int ultrastor_14f_detect(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 346 {
 347     size_t i;
 348     unsigned char in_byte, version_byte = 0;
 349     struct config_1 {
 350       unsigned char bios_segment: 3;
 351       unsigned char removable_disks_as_fixed: 1;
 352       unsigned char interrupt: 2;
 353     unsigned char dma_channel: 2;
 354     } config_1;
 355     struct config_2 {
 356       unsigned char ha_scsi_id: 3;
 357       unsigned char mapping_mode: 2;
 358       unsigned char bios_drive_number: 1;
 359       unsigned char tfr_port: 2;
 360     } config_2;
 361 
 362 #if (ULTRASTOR_DEBUG & UD_DETECT)
 363     printk("US14F: detect: called\n");
 364 #endif
 365 
 366     /* If a 24F has already been configured, don't look for a 14F.  */
 367     if (config.bios_segment)
 368         return FALSE;
 369 
 370 #ifdef PORT_OVERRIDE
 371     if(check_region(PORT_OVERRIDE, 0xc)) {
 372       printk("Ultrastor I/O space already in use\n");
 373       return FALSE;
 374     };
 375     config.port_address = PORT_OVERRIDE;
 376 #else
 377     for (i = 0; i < ARRAY_SIZE(ultrastor_ports_14f); i++) {
 378       if(check_region(ultrastor_ports_14f[i], 0x0c)) continue;
 379       config.port_address = ultrastor_ports_14f[i];
 380 #endif
 381 
 382 #if (ULTRASTOR_DEBUG & UD_DETECT)
 383         printk("US14F: detect: testing port address %03X\n", config.port_address);
 384 #endif
 385 
 386         in_byte = inb(U14F_PRODUCT_ID(config.port_address));
 387         if (in_byte != US14F_PRODUCT_ID_0) {
 388 #if (ULTRASTOR_DEBUG & UD_DETECT)
 389 # ifdef PORT_OVERRIDE
 390             printk("US14F: detect: wrong product ID 0 - %02X\n", in_byte);
 391 # else
 392             printk("US14F: detect: no adapter at port %03X\n", config.port_address);
 393 # endif
 394 #endif
 395 #ifdef PORT_OVERRIDE
 396             return FALSE;
 397 #else
 398             continue;
 399 #endif
 400         }
 401         in_byte = inb(U14F_PRODUCT_ID(config.port_address) + 1);
 402         /* Only upper nibble is significant for Product ID 1 */
 403         if ((in_byte & 0xF0) != US14F_PRODUCT_ID_1) {
 404 #if (ULTRASTOR_DEBUG & UD_DETECT)
 405 # ifdef PORT_OVERRIDE
 406             printk("US14F: detect: wrong product ID 1 - %02X\n", in_byte);
 407 # else
 408             printk("US14F: detect: no adapter at port %03X\n", config.port_address);
 409 # endif
 410 #endif
 411 #ifdef PORT_OVERRIDE
 412             return FALSE;
 413 #else
 414             continue;
 415 #endif
 416         }
 417         version_byte = in_byte;
 418 #ifndef PORT_OVERRIDE
 419         break;
 420     }
 421     if (i == ARRAY_SIZE(ultrastor_ports_14f)) {
     /* [previous][next][first][last][top][bottom][index][help] */
 422 # if (ULTRASTOR_DEBUG & UD_DETECT)
 423         printk("US14F: detect: no port address found!\n");
 424 # endif
 425         return FALSE;
 426     }
 427 #endif
 428 
 429 #if (ULTRASTOR_DEBUG & UD_DETECT)
 430     printk("US14F: detect: adapter found at port address %03X\n",
 431            config.port_address);
 432 #endif
 433 
 434     /* Set local doorbell mask to disallow bus reset unless
 435        ultrastor_bus_reset is true.  */
 436     outb(ultrastor_bus_reset ? 0xc2 : 0x82, LCL_DOORBELL_MASK(config.port_address));
 437 
 438     /* All above tests passed, must be the right thing.  Get some useful
 439        info. */
 440 
 441     snarf_region(config.port_address, 0x0c); /* Register the I/O space that we use */
 442 
 443     *(char *)&config_1 = inb(CONFIG(config.port_address + 0));
 444     *(char *)&config_2 = inb(CONFIG(config.port_address + 1));
 445     config.bios_segment = bios_segment_table[config_1.bios_segment];
 446     config.doorbell_address = config.port_address;
 447     config.ogm_address = config.port_address + 0x8;
 448     config.icm_address = config.port_address + 0xC;
 449     config.interrupt = interrupt_table_14f[config_1.interrupt];
 450     config.ha_scsi_id = config_2.ha_scsi_id;
 451     config.heads = mapping_table[config_2.mapping_mode].heads;
 452     config.sectors = mapping_table[config_2.mapping_mode].sectors;
 453     config.bios_drive_number = config_2.bios_drive_number;
 454     config.subversion = (version_byte & 0x0F);
 455     if (config.subversion == U34F)
 456         config.dma_channel = 0;
 457     else
 458         config.dma_channel = dma_channel_table_14f[config_1.dma_channel];
 459 
 460     if (!config.bios_segment) {
 461 #if (ULTRASTOR_DEBUG & UD_DETECT)
 462         printk("US14F: detect: not detected.\n");
 463 #endif
 464         return FALSE;
 465     }
 466 
 467     /* Final consistency check, verify previous info. */
 468     if (config.subversion != U34F)
 469         if (!config.dma_channel || !(config_2.tfr_port & 0x2)) {
 470 #if (ULTRASTOR_DEBUG & UD_DETECT)
 471             printk("US14F: detect: consistency check failed\n");
 472 #endif
 473             return FALSE;
 474         }
 475 
 476     /* If we were TRULY paranoid, we could issue a host adapter inquiry
 477        command here and verify the data returned.  But frankly, I'm
 478        exhausted! */
 479 
 480     /* Finally!  Now I'm satisfied... */
 481 #if (ULTRASTOR_DEBUG & UD_DETECT)
 482     printk("US14F: detect: detect succeeded\n"
 483            "  Port address: %03X\n"
 484            "  BIOS segment: %05X\n"
 485            "  Interrupt: %u\n"
 486            "  DMA channel: %u\n"
 487            "  H/A SCSI ID: %u\n"
 488            "  Subversion: %u\n",
 489            config.port_address, config.bios_segment, config.interrupt,
 490            config.dma_channel, config.ha_scsi_id, config.subversion);
 491 #endif
 492     tpnt->this_id = config.ha_scsi_id;
 493     tpnt->unchecked_isa_dma = (config.subversion != U34F);
 494 
 495 #if ULTRASTOR_MAX_CMDS > 1
 496     config.mscp_free = ~0;
 497 #endif
 498 
 499     if (request_irq(config.interrupt, ultrastor_interrupt, 0, "Ultrastor")) {
     /* [previous][next][first][last][top][bottom][index][help] */
 500         printk("Unable to allocate IRQ%u for UltraStor controller.\n",
 501                config.interrupt);
 502         return FALSE;
 503     }
 504     if (config.dma_channel && request_dma(config.dma_channel,"Ultrastor")) {
     /* [previous][next][first][last][top][bottom][index][help] */
 505         printk("Unable to allocate DMA channel %u for UltraStor controller.\n",
 506                config.dma_channel);
 507         free_irq(config.interrupt);
 508         return FALSE;
 509     }
 510     tpnt->sg_tablesize = ULTRASTOR_14F_MAX_SG;
 511     printk("UltraStor driver version" VERSION ".  Using %d SG lists.\n",
 512            ULTRASTOR_14F_MAX_SG);
 513 
 514     return TRUE;
 515 }
 516 
 517 static int ultrastor_24f_detect(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 518 {
 519   register int i;
 520   struct Scsi_Host * shpnt = NULL;
 521 
 522 #if (ULTRASTOR_DEBUG & UD_DETECT)
 523   printk("US24F: detect");
 524 #endif
 525 
 526   /* probe each EISA slot at slot address C80 */
 527   for (i = 1; i < 15; i++)
 528     {
 529       unsigned char config_1, config_2;
 530       unsigned short addr = (i << 12) | ULTRASTOR_24F_PORT;
 531 
 532       if (inb(addr) != US24F_PRODUCT_ID_0 &&
 533           inb(addr+1) != US24F_PRODUCT_ID_1 &&
 534           inb(addr+2) != US24F_PRODUCT_ID_2)
 535         continue;
 536 
 537       config.revision = inb(addr+3);
 538       config.slot = i;
 539       if (! (inb(addr+4) & 1))
 540         {
 541 #if (ULTRASTOR_DEBUG & UD_DETECT)
 542           printk("U24F: found disabled card in slot %u\n", i);
 543 #endif
 544           continue;
 545         }
 546 #if (ULTRASTOR_DEBUG & UD_DETECT)
 547       printk("U24F: found card in slot %u\n", i);
 548 #endif
 549       config_1 = inb(addr + 5);
 550       config.bios_segment = bios_segment_table[config_1 & 7];
 551       switch(config_1 >> 4)
 552         {
 553         case 1:
 554           config.interrupt = 15;
 555           break;
 556         case 2:
 557           config.interrupt = 14;
 558           break;
 559         case 4:
 560           config.interrupt = 11;
 561           break;
 562         case 8:
 563           config.interrupt = 10;
 564           break;
 565         default:
 566           printk("U24F: invalid IRQ\n");
 567           return FALSE;
 568         }
 569       if (request_irq(config.interrupt, ultrastor_interrupt, 0, "Ultrastor"))
 570         {
 571           printk("Unable to allocate IRQ%u for UltraStor controller.\n",
 572                  config.interrupt);
 573           return FALSE;
 574         }
 575       /* BIOS addr set */
 576       /* base port set */
 577       config.port_address = addr;
 578       config.doorbell_address = addr + 12;
 579       config.ogm_address = addr + 0x17;
 580       config.icm_address = addr + 0x1C;
 581       config_2 = inb(addr + 7);
 582       config.ha_scsi_id = config_2 & 7;
 583       config.heads = mapping_table[(config_2 >> 3) & 3].heads;
 584       config.sectors = mapping_table[(config_2 >> 3) & 3].sectors;
 585 #if (ULTRASTOR_DEBUG & UD_DETECT)
 586       printk("US24F: detect: detect succeeded\n"
 587              "  Port address: %03X\n"
 588              "  BIOS segment: %05X\n"
 589              "  Interrupt: %u\n"
 590              "  H/A SCSI ID: %u\n",
 591              config.port_address, config.bios_segment,
 592              config.interrupt, config.ha_scsi_id);
 593 #endif
 594       tpnt->this_id = config.ha_scsi_id;
 595       tpnt->unchecked_isa_dma = 0;
 596       tpnt->sg_tablesize = ULTRASTOR_24F_MAX_SG;
 597 
 598       shpnt = scsi_register(tpnt, 0);
 599       shpnt->irq = config.interrupt;
 600       shpnt->dma_channel = config.dma_channel;
 601       shpnt->io_port = config.port_address;
 602 
 603 #if ULTRASTOR_MAX_CMDS > 1
 604       config.mscp_free = ~0;
 605 #endif
 606       /* Mark ICM and OGM free */
 607       outb(0, addr + 0x16);
 608       outb(0, addr + 0x1B);
 609 
 610       /* Set local doorbell mask to disallow bus reset unless
 611          ultrastor_bus_reset is true.  */
 612       outb(ultrastor_bus_reset ? 0xc2 : 0x82, LCL_DOORBELL_MASK(addr+12));
 613       outb(0x02, SYS_DOORBELL_MASK(addr+12));
 614       printk("UltraStor driver version " VERSION ".  Using %d SG lists.\n",
 615              tpnt->sg_tablesize);
 616       return TRUE;
 617     }
 618   return FALSE;
 619 }
 620 
 621 int ultrastor_detect(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 622 {
 623   return ultrastor_14f_detect(tpnt) || ultrastor_24f_detect(tpnt);
 624 }
 625 
 626 const char *ultrastor_info(struct Scsi_Host * shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 627 {
 628     static char buf[64];
 629 
 630     if (config.slot)
 631       sprintf(buf, "UltraStor 24F SCSI @ Slot %u IRQ%u\n",
 632               config.slot, config.interrupt);
 633     else if (config.subversion)
 634       sprintf(buf, "UltraStor 34F SCSI @ Port %03X BIOS %05X IRQ%u\n",
 635               config.port_address, (int)config.bios_segment,
 636               config.interrupt);
 637     else
 638       sprintf(buf, "UltraStor 14F SCSI @ Port %03X BIOS %05X IRQ%u DMA%u\n",
 639               config.port_address, (int)config.bios_segment,
 640               config.interrupt, config.dma_channel);
 641     return buf;
 642 }
 643 
 644 static inline void build_sg_list(register struct mscp *mscp, Scsi_Cmnd *SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 645 {
 646         struct scatterlist *sl;
 647         long transfer_length = 0;
 648         int i, max;
 649 
 650         sl = (struct scatterlist *) SCpnt->request_buffer;
 651         max = SCpnt->use_sg;
 652         for (i = 0; i < max; i++) {
 653                 mscp->sglist[i].address = (unsigned int)sl[i].address;
 654                 mscp->sglist[i].num_bytes = sl[i].length;
 655                 transfer_length += sl[i].length;
 656         }
 657         mscp->number_of_sg_list = max;
 658         mscp->transfer_data = (unsigned int)mscp->sglist;
 659         /* ??? May not be necessary.  Docs are unclear as to whether transfer
 660            length field is ignored or whether it should be set to the total
 661            number of bytes of the transfer.  */
 662         mscp->transfer_data_length = transfer_length;
 663 }
 664 
 665 int ultrastor_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
 666 {
 667     register struct mscp *my_mscp;
 668 #if ULTRASTOR_MAX_CMDS > 1
 669     int mscp_index;
 670 #endif
 671     unsigned int status;
 672     int flags;
 673 
 674     /* Next test is for debugging; "can't happen" */
 675     if ((config.mscp_free & ((1U << ULTRASTOR_MAX_CMDS) - 1)) == 0)
 676         panic("ultrastor_queuecommand: no free MSCP\n");
 677     mscp_index = find_and_clear_bit_16(&config.mscp_free);
 678 
 679     /* Has the command been aborted?  */
 680     if (xchgb(0xff, &config.aborted[mscp_index]) != 0)
 681       {
 682         status = DID_ABORT << 16;
 683         goto aborted;
 684       }
 685 
 686     my_mscp = &config.mscp[mscp_index];
 687 
 688 #if 1
 689     /* This way is faster.  */
 690     *(unsigned char *)my_mscp = OP_SCSI | (DTD_SCSI << 3);
 691 #else
 692     my_mscp->opcode = OP_SCSI;
 693     my_mscp->xdir = DTD_SCSI;
 694     my_mscp->dcn = FALSE;
 695 #endif
 696     /* Tape drives don't work properly if the cache is used.  The SCSI
 697        READ command for a tape doesn't have a block offset, and the adapter
 698        incorrectly assumes that all reads from the tape read the same
 699        blocks.  Results will depend on read buffer size and other disk
 700        activity. 
 701 
 702        ???  Which other device types should never use the cache?   */
 703     my_mscp->ca = SCpnt->device->type != TYPE_TAPE;
 704     my_mscp->target_id = SCpnt->target;
 705     my_mscp->ch_no = 0;
 706     my_mscp->lun = SCpnt->lun;
 707     if (SCpnt->use_sg) {
 708         /* Set scatter/gather flag in SCSI command packet */
 709         my_mscp->sg = TRUE;
 710         build_sg_list(my_mscp, SCpnt);
 711     } else {
 712         /* Unset scatter/gather flag in SCSI command packet */
 713         my_mscp->sg = FALSE;
 714         my_mscp->transfer_data = (unsigned int)SCpnt->request_buffer;
 715         my_mscp->transfer_data_length = SCpnt->request_bufflen;
 716     }
 717     my_mscp->command_link = 0;          /*???*/
 718     my_mscp->scsi_command_link_id = 0;  /*???*/
 719     my_mscp->length_of_sense_byte = sizeof SCpnt->sense_buffer;
 720     my_mscp->length_of_scsi_cdbs = SCpnt->cmd_len;
 721     memcpy(my_mscp->scsi_cdbs, SCpnt->cmnd, my_mscp->length_of_scsi_cdbs);
 722     my_mscp->adapter_status = 0;
 723     my_mscp->target_status = 0;
 724     my_mscp->sense_data = (unsigned int)&SCpnt->sense_buffer;
 725     my_mscp->done = done;
 726     my_mscp->SCint = SCpnt;
 727     SCpnt->host_scribble = (unsigned char *)my_mscp;
 728 
 729     /* Find free OGM slot.  On 24F, look for OGM status byte == 0.
 730        On 14F and 34F, wait for local interrupt pending flag to clear.  */
 731 
 732   retry:
 733     if (config.slot)
 734         while (inb(config.ogm_address - 1) != 0 &&
 735                config.aborted[mscp_index] == 0xff);
 736 
 737     /* else??? */
 738 
 739     while ((inb(LCL_DOORBELL_INTR(config.doorbell_address)) & 
 740             (config.slot ? 2 : 1)) 
 741            && config.aborted[mscp_index] == 0xff);
 742 
 743     /* To avoid race conditions, make the code to write to the adapter
 744        atomic.  This simplifies the abort code.  */
 745 
 746     save_flags(flags);
 747     cli();
 748 
 749     if (inb(LCL_DOORBELL_INTR(config.doorbell_address)) &
 750         (config.slot ? 2 : 1))
 751       {
 752       restore_flags(flags);
 753       goto retry;
 754       }
 755 
 756     status = xchgb(0, &config.aborted[mscp_index]);
 757     if (status != 0xff) {
 758         restore_flags(flags);
 759 
 760 #if ULTRASTOR_DEBUG & (UD_COMMAND | UD_ABORT)
 761         printk("USx4F: queuecommand: aborted\n");
 762 #if ULTRASTOR_MAX_CMDS > 1
 763         log_ultrastor_abort(&config, mscp_index);
 764 #endif
 765 #endif
 766         status <<= 16;
 767 
 768       aborted:
 769         set_bit(mscp_index, &config.mscp_free);
 770         /* If the driver queues commands, call the done proc here.  Otherwise
 771            return an error.  */
 772 #if ULTRASTOR_MAX_CMDS > 1
 773         SCpnt->result = status;
 774         done(SCpnt);
 775         return 0;
 776 #else
 777         return status;
 778 #endif
 779     }
 780 
 781     /* Store pointer in OGM address bytes */
 782     outl((unsigned int)my_mscp, config.ogm_address);
 783 
 784     /* Issue OGM interrupt */
 785     if (config.slot) {
 786         /* Write OGM command register on 24F */
 787         outb(1, config.ogm_address - 1);
 788         outb(0x2, LCL_DOORBELL_INTR(config.doorbell_address));
 789     } else {
 790         outb(0x1, LCL_DOORBELL_INTR(config.doorbell_address));
 791     }
 792 
 793     restore_flags(flags);
 794 
 795 #if (ULTRASTOR_DEBUG & UD_COMMAND)
 796     printk("USx4F: queuecommand: returning\n");
 797 #endif
 798 
 799     return 0;
 800 }
 801 
 802 /* This code must deal with 2 cases:
 803 
 804    1. The command has not been written to the OGM.  In this case, set
 805    the abort flag and return.
 806 
 807    2. The command has been written to the OGM and is stuck somewhere in
 808    the adapter.
 809 
 810    2a.  On a 24F, ask the adapter to abort the command.  It will interrupt
 811    when it does.
 812 
 813    2b.  Call the command's done procedure.
 814 
 815  */
 816 
 817 int ultrastor_abort(Scsi_Cmnd *SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 818 {
 819 #if ULTRASTOR_DEBUG & UD_ABORT
 820     char out[108];
 821     unsigned char icm_status = 0, ogm_status = 0;
 822     unsigned int icm_addr = 0, ogm_addr = 0;
 823 #endif
 824     unsigned int mscp_index;
 825     unsigned char old_aborted;
 826     void (*done)(Scsi_Cmnd *);
 827 
 828     if(config.slot) 
 829       return SCSI_ABORT_SNOOZE;  /* Do not attempt an abort for the 24f */
 830 
 831     /* Simple consistency checking */
 832     if(!SCpnt->host_scribble)
 833       return SCSI_ABORT_NOT_RUNNING;
 834 
 835     mscp_index = ((struct mscp *)SCpnt->host_scribble) - config.mscp;
 836     if (mscp_index >= ULTRASTOR_MAX_CMDS)
 837         panic("Ux4F aborting invalid MSCP");
 838 
 839 #if ULTRASTOR_DEBUG & UD_ABORT
 840     if (config.slot)
 841       {
 842         int port0 = (config.slot << 12) | 0xc80;
 843         int i;
 844         int flags;
 845         save_flags(flags);
 846         cli();
 847         strcpy(out, "OGM %d:%x ICM %d:%x ports:  ");
 848         for (i = 0; i < 16; i++)
 849           {
 850             unsigned char p = inb(port0 + i);
 851             out[28 + i * 3] = "0123456789abcdef"[p >> 4];
 852             out[29 + i * 3] = "0123456789abcdef"[p & 15];
 853             out[30 + i * 3] = ' ';
 854           }
 855         out[28 + i * 3] = '\n';
 856         out[29 + i * 3] = 0;
 857         ogm_status = inb(port0 + 22);
 858         ogm_addr = inl(port0 + 23);
 859         icm_status = inb(port0 + 27);
 860         icm_addr = inl(port0 + 28);
 861         restore_flags(flags);
 862       }
 863 
 864     /* First check to see if an interrupt is pending.  I suspect the SiS
 865        chipset loses interrupts.  (I also suspect is mangles data, but
 866        one bug at a time... */
 867     if (config.slot ? inb(config.icm_address - 1) == 2 :
 868         (inb(SYS_DOORBELL_INTR(config.doorbell_address)) & 1))
 869       {
 870         int flags;
 871         save_flags(flags);
 872         printk("Ux4F: abort while completed command pending\n");
 873         restore_flags(flags);
 874         cli();
 875         ultrastor_interrupt(0);
 876         restore_flags(flags);
 877         return SCSI_ABORT_SUCCESS;  /* FIXME - is this correct? -ERY */
 878       }
 879 #endif
 880 
 881     old_aborted = xchgb(DID_ABORT, &config.aborted[mscp_index]);
 882 
 883     /* aborted == 0xff is the signal that queuecommand has not yet sent
 884        the command.  It will notice the new abort flag and fail.  */
 885     if (old_aborted == 0xff)
 886         return SCSI_ABORT_SUCCESS;
 887 
 888     /* On 24F, send an abort MSCP request.  The adapter will interrupt
 889        and the interrupt handler will call done.  */
 890     if (config.slot && inb(config.ogm_address - 1) == 0)
 891       {
 892         int flags;
 893 
 894         save_flags(flags);
 895         cli();
 896         outl((int)&config.mscp[mscp_index], config.ogm_address);
 897         inb(0xc80);     /* delay */
 898         outb(0x80, config.ogm_address - 1);
 899         outb(0x2, LCL_DOORBELL_INTR(config.doorbell_address));
 900 #if ULTRASTOR_DEBUG & UD_ABORT
 901         log_ultrastor_abort(&config, mscp_index);
 902         printk(out, ogm_status, ogm_addr, icm_status, icm_addr);
 903 #endif
 904         restore_flags(flags);
 905         return SCSI_ABORT_PENDING;
 906       }
 907 
 908 #if ULTRASTOR_DEBUG & UD_ABORT
 909     log_ultrastor_abort(&config, mscp_index);
 910 #endif
 911 
 912     /* Can't request a graceful abort.  Either this is not a 24F or
 913        the OGM is busy.  Don't free the command -- the adapter might
 914        still be using it.  Setting SCint = 0 causes the interrupt
 915        handler to ignore the command.  */
 916 
 917     /* FIXME - devices that implement soft resets will still be running
 918        the command after a bus reset.  We would probably rather leave
 919        the command in the queue.  The upper level code will automatically
 920        leave the command in the active state instead of requeueing it. ERY */
 921 
 922 #if ULTRASTOR_DEBUG & UD_ABORT
 923     if (config.mscp[mscp_index].SCint != SCpnt)
 924         printk("abort: command mismatch, %p != %p\n",
 925                config.mscp[mscp_index].SCint, SCpnt);
 926 #endif
 927     if (config.mscp[mscp_index].SCint == 0)
 928         return SCSI_ABORT_NOT_RUNNING;
 929 
 930     if (config.mscp[mscp_index].SCint != SCpnt) panic("Bad abort");
 931     config.mscp[mscp_index].SCint = 0;
 932     done = config.mscp[mscp_index].done;
 933     config.mscp[mscp_index].done = 0;
 934     SCpnt->result = DID_ABORT << 16;
 935     /* I worry about reentrancy in scsi.c  */
 936     done(SCpnt);
 937 
 938     /* Need to set a timeout here in case command never completes.  */
 939     return SCSI_ABORT_SUCCESS;
 940 }
 941 
 942 int ultrastor_reset(Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 943 {
 944     int flags;
 945     register int i;
 946 #if (ULTRASTOR_DEBUG & UD_RESET)
 947     printk("US14F: reset: called\n");
 948 #endif
 949 
 950     if(config.slot)
 951       return SCSI_RESET_PUNT;  /* Do not attempt a reset for the 24f */
 952 
 953     save_flags(flags);
 954     cli();
 955 
 956     /* Reset the adapter and SCSI bus.  The SCSI bus reset can be
 957        inhibited by clearing ultrastor_bus_reset before probe.  */
 958     outb(0xc0, LCL_DOORBELL_INTR(config.doorbell_address));
 959     if (config.slot)
 960       {
 961         outb(0, config.ogm_address - 1);
 962         outb(0, config.icm_address - 1);
 963       }
 964 
 965 #if ULTRASTOR_MAX_CMDS == 1
 966     if (config.mscp_busy && config.mscp->done && config.mscp->SCint)
 967       {
 968         config.mscp->SCint->result = DID_RESET << 16;
 969         config.mscp->done(config.mscp->SCint);
 970       }
 971     config.mscp->SCint = 0;
 972 #else
 973     for (i = 0; i < ULTRASTOR_MAX_CMDS; i++)
 974       {
 975         if (! (config.mscp_free & (1 << i)) &&
 976             config.mscp[i].done && config.mscp[i].SCint)
 977           {
 978             config.mscp[i].SCint->result = DID_RESET << 16;
 979             config.mscp[i].done(config.mscp[i].SCint);
 980             config.mscp[i].done = 0;
 981           }
 982         config.mscp[i].SCint = 0;
 983       }
 984 #endif
 985 
 986     /* FIXME - if the device implements soft resets, then the command
 987        will still be running.  ERY */
 988 
 989     memset((unsigned char *)config.aborted, 0, sizeof config.aborted);
 990 #if ULTRASTOR_MAX_CMDS == 1
 991     config.mscp_busy = 0;
 992 #else
 993     config.mscp_free = ~0;
 994 #endif
 995 
 996     restore_flags(flags);
 997     return SCSI_RESET_SUCCESS;
 998 
 999 }
1000 
1001 int ultrastor_biosparam(Disk * disk, int dev, int * dkinfo)
     /* [previous][next][first][last][top][bottom][index][help] */
1002 {
1003     int size = disk->capacity;
1004     unsigned int s = config.heads * config.sectors;
1005 
1006     dkinfo[0] = config.heads;
1007     dkinfo[1] = config.sectors;
1008     dkinfo[2] = size / s;       /* Ignore partial cylinders */
1009 #if 0
1010     if (dkinfo[2] > 1024)
1011         dkinfo[2] = 1024;
1012 #endif
1013     return 0;
1014 }
1015 
1016 static void ultrastor_interrupt(int cpl)
     /* [previous][next][first][last][top][bottom][index][help] */
1017 {
1018     unsigned int status;
1019 #if ULTRASTOR_MAX_CMDS > 1
1020     unsigned int mscp_index;
1021 #endif
1022     register struct mscp *mscp;
1023     void (*done)(Scsi_Cmnd *);
1024     Scsi_Cmnd *SCtmp;
1025 
1026 #if ULTRASTOR_MAX_CMDS == 1
1027     mscp = &config.mscp[0];
1028 #else
1029     mscp = (struct mscp *)inl(config.icm_address);
1030     mscp_index = mscp - config.mscp;
1031     if (mscp_index >= ULTRASTOR_MAX_CMDS) {
1032         printk("Ux4F interrupt: bad MSCP address %x\n", (unsigned int) mscp);
1033         /* A command has been lost.  Reset and report an error
1034            for all commands.  */
1035         ultrastor_reset(NULL);
1036         return;
1037     }
1038 #endif
1039 
1040     /* Clean ICM slot (set ICMINT bit to 0) */
1041     if (config.slot) {
1042         unsigned char icm_status = inb(config.icm_address - 1);
1043 #if ULTRASTOR_DEBUG & (UD_INTERRUPT|UD_ERROR|UD_ABORT)
1044         if (icm_status != 1 && icm_status != 2)
1045             printk("US24F: ICM status %x for MSCP %d (%x)\n", icm_status,
1046                    mscp_index, (unsigned int) mscp);
1047 #endif
1048         /* The manual says clear interrupt then write 0 to ICM status.
1049            This seems backwards, but I'll do it anyway.  --jfc */
1050         outb(2, SYS_DOORBELL_INTR(config.doorbell_address));
1051         outb(0, config.icm_address - 1);
1052         if (icm_status == 4) {
1053             printk("UltraStor abort command failed\n");
1054             return;
1055         }
1056         if (icm_status == 3) {
1057             void (*done)(Scsi_Cmnd *) = mscp->done;
1058             if (done) {
1059                 mscp->done = 0;
1060                 mscp->SCint->result = DID_ABORT << 16;
1061                 done(mscp->SCint);
1062             }
1063             return;
1064         }
1065     } else {
1066         outb(1, SYS_DOORBELL_INTR(config.doorbell_address));
1067     }
1068 
1069     SCtmp = mscp->SCint;
1070     mscp->SCint = NULL;
1071 
1072     if (SCtmp == 0)
1073       {
1074 #if ULTRASTOR_DEBUG & (UD_ABORT|UD_INTERRUPT)
1075         printk("MSCP %d (%x): no command\n", mscp_index, (unsigned int) mscp);
1076 #endif  
1077 #if ULTRASTOR_MAX_CMDS == 1
1078         config.mscp_busy = FALSE;
1079 #else
1080         set_bit(mscp_index, &config.mscp_free);
1081 #endif
1082         config.aborted[mscp_index] = 0;
1083         return;
1084       }
1085 
1086     /* Save done locally and zero before calling.  This is needed as
1087        once we call done, we may get another command queued before this
1088        interrupt service routine can return. */
1089     done = mscp->done;
1090     mscp->done = 0;
1091 
1092     /* Let the higher levels know that we're done */
1093     switch (mscp->adapter_status)
1094       {
1095       case 0:
1096         status = DID_OK << 16;
1097         break;
1098       case 0x01:        /* invalid command */
1099       case 0x02:        /* invalid parameters */
1100       case 0x03:        /* invalid data list */
1101       default:
1102         status = DID_ERROR << 16;
1103         break;
1104       case 0x84:        /* SCSI bus abort */
1105         status = DID_ABORT << 16;
1106         break;
1107       case 0x91:
1108         status = DID_TIME_OUT << 16;
1109         break;
1110       }
1111 
1112     SCtmp->result = status | mscp->target_status;
1113 
1114     SCtmp->host_scribble = 0;
1115 
1116     /* Free up mscp block for next command */
1117 #if ULTRASTOR_MAX_CMDS == 1
1118     config.mscp_busy = FALSE;
1119 #else
1120     set_bit(mscp_index, &config.mscp_free);
1121 #endif
1122 
1123 #if ULTRASTOR_DEBUG & (UD_ABORT|UD_INTERRUPT)
1124     if (config.aborted[mscp_index])
1125         printk("Ux4 interrupt: MSCP %d (%x) aborted = %d\n",
1126                mscp_index, (unsigned int) mscp, config.aborted[mscp_index]);
1127 #endif
1128     config.aborted[mscp_index] = 0;
1129 
1130     if (done)
1131         done(SCtmp);
1132     else
1133         printk("US14F: interrupt: unexpected interrupt\n");
1134 
1135     if (config.slot ? inb(config.icm_address - 1) : (inb(SYS_DOORBELL_INTR(config.doorbell_address)) & 1))
1136       printk("Ux4F: multiple commands completed\n");
1137 
1138 #if (ULTRASTOR_DEBUG & UD_INTERRUPT)
1139     printk("USx4F: interrupt: returning\n");
1140 #endif
1141 }

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