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

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