root/drivers/scsi/buslogic.c

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

DEFINITIONS

This source file includes following definitions.
  1. wait
  2. buslogic_prefix
  3. buslogic_stat
  4. buslogic_out
  5. buslogic_in
  6. makecode
  7. buslogic_info
  8. buslogic_interrupt
  9. buslogic_queuecommand
  10. internal_done
  11. buslogic_command
  12. setup_mailboxes
  13. getconfig
  14. buslogic_query
  15. buslogic_detect
  16. restart
  17. buslogic_abort
  18. buslogic_reset
  19. buslogic_biosparam
  20. buslogic_setup

   1 /*
   2  *      buslogic.c      Copyright (C) 1993, 1994 David B. Gentzel
   3  *      Low-level scsi driver for BusLogic adapters
   4  *      by David B. Gentzel, Whitfield Software Services, Carnegie, PA
   5  *          (gentzel@nova.enet.dec.com)
   6  *      Thanks to BusLogic for providing the necessary documentation
   7  *
   8  *      The original version of this driver was derived from aha1542.[ch],
   9  *      which is Copyright (C) 1992 Tommy Thorn.  Much has been reworked, but
  10  *      most of basic structure and substantial chunks of code still remain.
  11  *
  12  *      Furthermore, many subsequent fixes and improvements to the aha1542
  13  *      driver have been folded back into this driver.  These changes to
  14  *      aha1542.[ch] are Copyright (C) 1993, 1994 Eric Youngdale.
  15  *
  16  *      Thanks to the following individuals who have made contributions (of
  17  *      (code, information, support, or testing) to this driver:
  18  *              Eric Youngdale          Leonard Zubkoff
  19  *              Tomas Hurka             Andrew Walker
  20  */
  21 
  22 /*
  23  * TODO:
  24  *      1. Clean up error handling & reporting.
  25  *      2. Find out why scatter/gather is limited to 16 requests per command.
  26  *      3. Test/improve/fix abort & reset functions.
  27  *      4. Look at command linking.
  28  *      5. Allow multiple boards to share an IRQ if the bus allows (EISA, MCA,
  29  *         and PCI).
  30  *      6. Avoid using the 445S workaround for board revs >= D.
  31  */
  32 
  33 /*
  34  * NOTES:
  35  *    BusLogic (formerly BusTek) manufactures an extensive family of
  36  *    intelligent, high performance SCSI-2 host adapters.  They all support
  37  *    command queueing and scatter/gather I/O.  Most importantly, they all
  38  *    support identical programming interfaces, so a single driver can be used
  39  *    for all boards.
  40  *
  41  *    Actually, they all support TWO identical programming interfaces!  They
  42  *    have an Adaptec 154x compatible interface (complete with 24 bit
  43  *    addresses) as well as a "native" 32 bit interface.  As such, the Linux
  44  *    aha1542 driver can be used to drive them, but with less than optimal
  45  *    performance (at least for the EISA, VESA, and MCA boards).
  46  *
  47  *    Here is the scoop on the various models:
  48  *      BT-542B - ISA first-party DMA with floppy support.
  49  *      BT-545S - 542B + FAST SCSI and active termination.
  50  *      BT-545D - 545S + differential termination.
  51  *      BT-640A - MCA bus-master with floppy support.
  52  *      BT-646S - 640A + FAST SCSI and active termination.
  53  *      BT-646D - 646S + differential termination.
  54  *      BT-742A - EISA bus-master with floppy support.
  55  *      BT-747S - 742A + FAST SCSI, active termination, and 2.88M floppy.
  56  *      BT-747D - 747S + differential termination.
  57  *      BT-757S - 747S + WIDE SCSI.
  58  *      BT-757D - 747D + WIDE SCSI.
  59  *      BT-445S - VESA bus-master FAST SCSI with active termination
  60  *                and floppy support.
  61  *      BT-445C - 445S + enhanced BIOS & firmware options.
  62  *      BT-946C - PCI bus-master FAST SCSI.
  63  *      BT-956C - PCI bus-master FAST/WIDE SCSI.
  64  *
  65  *    ??? I believe other boards besides the 445 now have a "C" model, but I
  66  *    have no facts on them.
  67  *
  68  *    This driver SHOULD support all of these boards.  It has only been tested
  69  *    with a 747S, 445S, 946C, and 956C; there is no PCI-specific support as
  70  *    yet.
  71  *
  72  *    Should you require further information on any of these boards, BusLogic
  73  *    can be reached at (408)492-9090.  Their BBS # is (408)492-1984 (maybe BBS
  74  *    stands for "Big Brother System"?).
  75  *
  76  *    Places flagged with a triple question-mark are things which are either
  77  *    unfinished, questionable, or wrong.
  78  */
  79 
  80 #ifdef MODULE
  81 #include <linux/module.h>
  82 #endif
  83 
  84 #include <linux/string.h>
  85 #include <linux/sched.h>
  86 #include <linux/kernel.h>
  87 #include <linux/head.h>
  88 #include <linux/types.h>
  89 #include <linux/ioport.h>
  90 #include <linux/delay.h>
  91 #include <linux/config.h>
  92 
  93 #include <asm/io.h>
  94 #include <asm/system.h>
  95 #include <asm/dma.h>
  96 
  97 #include "../block/blk.h"
  98 #include "scsi.h"
  99 #include "hosts.h"
 100 #include "sd.h"
 101 #define BUSLOGIC_PRIVATE_H      /* Get the "private" stuff */
 102 #include "buslogic.h"
 103 
 104 #ifndef BUSLOGIC_DEBUG
 105 # define BUSLOGIC_DEBUG 0
 106 #endif
 107 
 108 /* ??? Until kmalloc actually implements GFP_DMA, we can't depend on it... */
 109 #undef GFP_DMA
 110 
 111 /* If different port addresses are needed (e.g. to install more than two
 112    cards), you must define BUSLOGIC_PORT_OVERRIDE to be a comma-separated list
 113    of the addresses which will be checked.  This can also be used to resolve a
 114    conflict if the port-probing at a standard port causes problems with
 115    another board. */
 116 /* #define BUSLOGIC_PORT_OVERRIDE 0x330, 0x334, 0x130, 0x134, 0x230, 0x234 */
 117 
 118 /* Define this to be either BIOS_TRANSLATION_DEFAULT or BIOS_TRANSLATION_BIG
 119    if you wish to bypass the test for this, which uses an undocumented port.
 120    The test is believed to fail on at least some AMI BusLogic clones. */
 121 /* #define BIOS_TRANSLATION_OVERRIDE BIOS_TRANSLATION_BIG */
 122 
 123 #define BUSLOGIC_VERSION "1.15"
 124 
 125 /* Not a random value - if this is too large, the system hangs for a long time
 126    waiting for something to happen if a board is not installed. */
 127 /* ??? I don't really like this as it will wait longer on slow machines.
 128    Perhaps we should base this on the loops_per_second "Bogomips" value? */
 129 #define WAITNEXTTIMEOUT 3000000
 130 
 131 /* This is for the scsi_malloc call in buslogic_queuecommand. */
 132 /* ??? I'd up this to 4096, but would we be in danger of using up the
 133    scsi_malloc memory pool? */
 134 /* This could be a concern, I guess.  It may be possible to fix things so that
 135    the table generated in sd.c is compatible with the low-level code, but
 136    don't hold your breath.  -ERY */
 137 #define BUSLOGIC_SG_MALLOC 512
 138 
 139 /* Since the SG list is malloced, we have to limit the length. */
 140 #define BUSLOGIC_MAX_SG (BUSLOGIC_SG_MALLOC / sizeof (struct chain))
 141 
 142 /* Since the host adapters have room to buffer 32 commands internally, there
 143    is some virtue in setting BUSLOGIC_MAILBOXES to 32.  The maximum value
 144    appears to be 255, since the Count parameter to the Initialize Extended
 145    Mailbox command is limited to one byte. */
 146 #define BUSLOGIC_MAILBOXES 32
 147 
 148 #define BUSLOGIC_CMDLUN 4       /* Arbitrary, but seems to work well. */
 149 
 150 /* BusLogic boards can be configured for quite a number of port addresses (six
 151    to be exact), but I generally do not want the driver poking around at
 152    random.  We allow two port addresses - this allows people to use a BusLogic
 153    with a MIDI card, which frequently also uses 0x330.
 154 
 155    This can also be overridden on the command line to the kernel, via LILO or
 156    LOADLIN. */
 157 static unsigned short bases[7] = {
 158 #ifdef BUSLOGIC_PORT_OVERRIDE
 159     BUSLOGIC_PORT_OVERRIDE,
 160 #else
 161     0x330, 0x334, /* 0x130, 0x134, 0x230, 0x234, */
 162 #endif
 163     0
 164 };
 165 
 166 #define BIOS_TRANSLATION_DEFAULT 0      /* Default case */
 167 #define BIOS_TRANSLATION_BIG 1          /* Big disk (> 1G) case */
 168 
 169 struct hostdata {
 170     unsigned int bus_type;
 171     unsigned int bios_translation: 1;   /* BIOS mapping (for compatibility) */
 172     int last_mbi_used;
 173     int last_mbo_used;
 174     char model[7];
 175     char firmware_rev[6];
 176     Scsi_Cmnd *sc[BUSLOGIC_MAILBOXES];
 177     struct mailbox mb[2 * BUSLOGIC_MAILBOXES];
 178     struct ccb ccbs[BUSLOGIC_MAILBOXES];
 179 };
 180 
 181 #define HOSTDATA(host) ((struct hostdata *)&(host)->hostdata)
 182 
 183 /* One for each IRQ level (9-15), although 13 will never be used. */
 184 static struct Scsi_Host *host[7] = { NULL, };
 185 
 186 static int setup_mailboxes(unsigned int base, struct Scsi_Host *shpnt);
 187 static int restart(struct Scsi_Host *shpnt);
 188 
 189 #define INTR_RESET(base) outb(RINT, CONTROL(base))
 190 
 191 #define buslogic_printk buslogic_prefix(__PRETTY_FUNCTION__),printk
 192 
 193 #if defined(MODULE) && !defined(GFP_DMA)
 194 # define CHECK_DMA_ADDR(isa, addr, badstmt) \
 195     do { if ((isa) && (addr) > (void *)ISA_DMA_THRESHOLD) badstmt; } while (0)
 196 #else
 197 # define CHECK_DMA_ADDR(isa, addr, badstmt)
 198 #endif
 199 
 200 #define CHECK(cond) if (cond) ; else goto fail
 201 
 202 #define WAIT(port, allof, noneof) \
 203     CHECK(wait(port, allof, noneof, WAITNEXTTIMEOUT, FALSE))
 204 #define WAIT_WHILE(port, mask) WAIT(port, 0, mask)
 205 #define WAIT_UNTIL(port, mask) WAIT(port, mask, 0)
 206 #define WAIT_FAST(port, allof, noneof) \
 207     CHECK(wait(port, allof, noneof, 100, TRUE))
 208 #define WAIT_WHILE_FAST(port, mask) WAIT_FAST(port, 0, mask)
 209 #define WAIT_UNTIL_FAST(port, mask) WAIT_FAST(port, mask, 0)
 210 
 211 /* If delay != 0, we use the udelay call to regulate the amount of time we
 212    wait.
 213 
 214    This is inline as it is always called with constant arguments and hence
 215    will be very well optimized. */
 216 static __inline__ int wait(unsigned short port,
     /* [previous][next][first][last][top][bottom][index][help] */
 217                            unsigned char allof, unsigned char noneof,
 218                            unsigned int timeout, int delay)
 219 {
 220     int bits;
 221 
 222     for (;;) {
 223         bits = inb(port);
 224         if ((bits & allof) == allof && (bits & noneof) == 0)
 225             return TRUE;
 226         if (delay)
 227             udelay(1000);
 228         if (--timeout == 0)
 229             return FALSE;
 230     }
 231 }
 232 
 233 static void buslogic_prefix(const char *func)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235     printk("BusLogic SCSI: %s: ", func);
 236 }
 237 
 238 static void buslogic_stat(unsigned int base)
     /* [previous][next][first][last][top][bottom][index][help] */
 239 {
 240     int s = inb(STATUS(base)), i = inb(INTERRUPT(base));
 241 
 242     buslogic_printk("status=%02X intrflags=%02X\n", s, i);
 243 }
 244 
 245 /* This is a bit complicated, but we need to make sure that an interrupt
 246    routine does not send something out while we are in the middle of this.
 247    Fortunately, it is only at boot time that multi-byte messages are ever
 248    sent. */
 249 static int buslogic_out(unsigned int base, const unsigned char *cmdp,
     /* [previous][next][first][last][top][bottom][index][help] */
 250                         size_t len)
 251 {
 252     unsigned long flags = 0;
 253     
 254     if (len == 1) {
 255         for (;;) {
 256             WAIT_WHILE(STATUS(base), CPRBSY);
 257             save_flags(flags);
 258             cli();
 259             if (!(inb(STATUS(base)) & CPRBSY)) {
 260                 outb(*cmdp, COMMAND_PARAMETER(base));
 261                 restore_flags(flags);
 262                 return FALSE;
 263             }
 264             restore_flags(flags);
 265         }
 266     } else {
 267         save_flags(flags);
 268         cli();
 269         while (len--) {
 270             WAIT_WHILE(STATUS(base), CPRBSY);
 271             outb(*cmdp++, COMMAND_PARAMETER(base));
 272         }
 273         restore_flags(flags);
 274     }
 275     return FALSE;
 276   fail:
 277     restore_flags(flags);
 278     buslogic_printk("failed(%u): ", len + 1);
 279     buslogic_stat(base);
 280     return TRUE;
 281 }
 282 
 283 /* Only used at boot time, so we do not need to worry about latency as much
 284    here.  This waits a very short period of time.  We use this if we are not
 285    sure whether the board will respond to the command we just sent. */
 286 static int buslogic_in(unsigned int base, unsigned char *cmdp, size_t len)
     /* [previous][next][first][last][top][bottom][index][help] */
 287 {
 288     unsigned long flags;
 289     
 290     save_flags(flags);
 291     cli();
 292     while (len--) {
 293         WAIT_UNTIL_FAST(STATUS(base), DIRRDY);
 294         *cmdp++ = inb(DATA_IN(base));
 295     }
 296     restore_flags(flags);
 297     return FALSE;
 298   fail:
 299     restore_flags(flags);
 300 #if (BUSLOGIC_DEBUG & BD_IO)
 301     buslogic_printk("failed(%u): ", len + 1);
 302     buslogic_stat(base);
 303 #endif
 304     return TRUE;
 305 }
 306 
 307 static unsigned int makecode(unsigned int haerr, unsigned int scsierr)
     /* [previous][next][first][last][top][bottom][index][help] */
 308 {
 309     unsigned int hosterr;
 310     const char *errstr = NULL;
 311 #if (BUSLOGIC_DEBUG & BD_ERRORS) && defined(CONFIG_SCSI_CONSTANTS)
 312     static const char *const buslogic_status[] = {
 313     /* 00 */    "Command completed normally",
 314     /* 01-07 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL,
 315     /* 08-09 */ NULL, NULL,
 316     /* 0A */    "Linked command completed normally",
 317     /* 0B */    "Linked command completed normally, interrupt generated",
 318     /* 0C-0F */ NULL, NULL, NULL, NULL,
 319     /* 10 */    NULL,
 320     /* 11 */    "Selection timed out",
 321     /* 12 */    "Data overrun/underrun",
 322     /* 13 */    "Unexpected bus free",
 323     /* 14 */    "Target bus phase sequence failure",
 324     /* 15 */    "First byte of outgoing MB was invalid",
 325     /* 16 */    "Invalid CCB Operation Code",
 326     /* 17 */    "Linked CCB does not have the same LUN",
 327     /* 18 */    "Invalid Target Direction received from Host",
 328     /* 19 */    "Duplicate CCB Received in Target Mode",
 329     /* 1A */    "Invalid CCB or Segment List Parameter",
 330     /* 1B */    "Auto request sense failed",
 331     /* 1C */    "SCSI-2 tagged queueing message was rejected by the target",
 332     /* 1D-1F */ NULL, NULL, NULL,
 333     /* 20 */    "Host adapter hardware failure",
 334     /* 21 */    "Target did not respond to SCSI ATN and the HA SCSI bus reset",
 335     /* 22 */    "Host adapter asserted a SCSI bus reset",
 336     /* 23 */    "Other SCSI devices asserted a SCSI bus reset",
 337     };
 338 #endif
 339 
 340     switch (haerr) {
 341       case 0x00:        /* Normal completion. */
 342       case 0x0A:        /* Linked command complete without error and linked
 343                            normally. */
 344       case 0x0B:        /* Linked command complete without error, interrupt
 345                            generated. */
 346         hosterr = DID_OK;
 347         break;
 348 
 349       case 0x11:        /* Selection time out: the initiator selection or
 350                            target reselection was not complete within the SCSI
 351                            time out period. */
 352         hosterr = DID_TIME_OUT;
 353         break;
 354 
 355       case 0x14:        /* Target bus phase sequence failure - An invalid bus
 356                            phase or bus phase sequence was requested by the
 357                            target.  The host adapter will generate a SCSI
 358                            Reset Condition, notifying the host with a RSTS
 359                            interrupt. */
 360       case 0x21:        /* The target did not respond to SCSI ATN and the host
 361                            adapter consequently issued a SCSI bus reset to
 362                            clear up the failure. */
 363       case 0x22:        /* The host adapter asserted a SCSI bus reset. */
 364         hosterr = DID_RESET;
 365         break;
 366 
 367       case 0x12:        /* Data overrun/underrun: the target attempted to
 368                            transfer more data than was allocated by the Data
 369                            Length field or the sum of the Scatter/Gather Data
 370                            Length fields. */
 371       case 0x13:        /* Unexpected bus free - The target dropped the SCSI
 372                            BSY at an unexpected time. */
 373       case 0x15:        /* MBO command was not 00, 01, or 02 - The first byte
 374                            of the MB was invalid.  This usually indicates a
 375                            software failure. */
 376       case 0x16:        /* Invalid CCB Operation Code - The first byte of the
 377                            CCB was invalid.  This usually indicates a software
 378                            failure. */
 379       case 0x17:        /* Linked CCB does not have the same LUN - A
 380                            subsequent CCB of a set of linked CCB's does not
 381                            specify the same logical unit number as the
 382                            first. */
 383       case 0x18:        /* Invalid Target Direction received from Host - The
 384                            direction of a Target Mode CCB was invalid. */
 385       case 0x19:        /* Duplicate CCB Received in Target Mode - More than
 386                            once CCB was received to service data transfer
 387                            between the same target LUN and initiator SCSI ID
 388                            in the same direction. */
 389       case 0x1A:        /* Invalid CCB or Segment List Parameter - A segment
 390                            list with a zero length segment or invalid segment
 391                            list boundaries was received.  A CCB parameter was
 392                            invalid. */
 393       case 0x1B:        /* Auto request sense failed. */
 394       case 0x1C:        /* SCSI-2 tagged queueing message was rejected by the
 395                            target. */
 396       case 0x20:        /* The host adapter hardware failed. */
 397       case 0x23:        /* Other SCSI devices asserted a SCSI bus reset. */
 398         hosterr = DID_ERROR;    /* ??? Couldn't find any better. */
 399         break;
 400 
 401       default:
 402 #ifndef CONFIG_SCSI_CONSTANTS
 403         errstr = "unknown hoststatus";
 404 #endif
 405         hosterr = DID_ERROR;
 406         break;
 407     }
 408 #if (BUSLOGIC_DEBUG & BD_ERRORS)
 409 # ifdef CONFIG_SCSI_CONSTANTS
 410     if (hosterr != DID_OK) {
 411         if (haerr < ARRAY_SIZE(buslogic_status))
 412             errstr = buslogic_status[haerr];
 413         if (errstr == NULL)
 414             errstr = "unknown hoststatus";
 415     }
 416 # else
 417     if (hosterr == DID_ERROR)
 418         errstr = "";
 419 # endif
 420 #endif
 421     if (errstr != NULL)
 422         buslogic_printk("%s (%02X)\n", errstr, haerr);
 423     return (hosterr << 16) | scsierr;
 424 }
 425 
 426 /* ??? this should really be "const struct Scsi_Host *" */
 427 const char *buslogic_info(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 428 {
 429     return "BusLogic SCSI driver " BUSLOGIC_VERSION;
 430 }
 431 
 432 /*
 433   This is a major rewrite of the interrupt handler to support the newer
 434   and faster PCI cards.  While the previous interrupt handler was supposed
 435   to handle multiple incoming becoming available mailboxes during the same
 436   interrupt, my testing showed that in practice only a single mailbox was
 437   ever made available.  With the 946C and 956C, multiple incoming mailboxes
 438   being ready for processing during a single interrupt occurs much more
 439   frequently, and so care must be taken to avoid race conditions managing
 440   the Host Adapter Interrupt Register, which can lead to lost interrupts.
 441 
 442   Leonard N. Zubkoff, 23-Mar-95
 443 */
 444 
 445 static void buslogic_interrupt(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 446 {
 447     int mbi, saved_mbo[BUSLOGIC_MAILBOXES];
 448     int base, interrupt_flags, found, i;
 449     struct Scsi_Host *shpnt;
 450     Scsi_Cmnd *sctmp;
 451     struct mailbox *mb;
 452     struct ccb *ccb;
 453 
 454     shpnt = host[irq - 9];
 455     if (shpnt == NULL)
 456       panic("buslogic_interrupt: NULL SCSI host entry");
 457 
 458     mb = HOSTDATA(shpnt)->mb;
 459     ccb = HOSTDATA(shpnt)->ccbs;
 460     base = shpnt->io_port;
 461 
 462     /*
 463       This interrupt handler is now specified to use the SA_INTERRUPT
 464       protocol, so interrupts are inhibited on entry until explicitly
 465       allowed again.  Read the Host Adapter Interrupt Register, and
 466       complain if there is no pending interrupt being signaled.
 467     */
 468 
 469     interrupt_flags = inb(INTERRUPT(base));
 470 
 471     if (!(interrupt_flags & INTV))
 472       buslogic_printk("interrupt received, but INTV not set\n");
 473 
 474     /*
 475       Reset the Host Adapter Interrupt Register.  It appears to be
 476       important that this is only done once per interrupt to avoid
 477       losing interrupts under heavy loads.
 478     */
 479 
 480     INTR_RESET(base);
 481 
 482     if (interrupt_flags & RSTS)
 483       {
 484         restart(shpnt);
 485         return;
 486       }
 487 
 488     /*
 489       With interrupts still inhibited, scan through the incoming mailboxes
 490       in strict round robin fashion saving the status information and
 491       then freeing the mailbox.  A second pass over the completed commands
 492       will be made separately to complete their processing.
 493     */
 494 
 495     mbi = HOSTDATA(shpnt)->last_mbi_used + 1;
 496     if (mbi >= 2*BUSLOGIC_MAILBOXES)
 497       mbi = BUSLOGIC_MAILBOXES;
 498 
 499     found = 0;
 500 
 501     while (mb[mbi].status != MBX_NOT_IN_USE && found < BUSLOGIC_MAILBOXES)
 502       {
 503         int mbo = (struct ccb *)mb[mbi].ccbptr - ccb;
 504 
 505         sctmp = HOSTDATA(shpnt)->sc[mbo];
 506 
 507         /*
 508           If sctmp has become NULL, higher level code must have aborted
 509           this operation and called the necessary completion routine.
 510         */
 511 
 512         if (sctmp != NULL && mb[mbi].status != MBX_COMPLETION_NOT_FOUND)
 513           {
 514             int result = 0;
 515 
 516             saved_mbo[found++] = mbo;
 517 
 518             if (mb[mbi].status != MBX_COMPLETION_OK)
 519               result = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
 520 
 521             sctmp->result = result;
 522 
 523             mb[mbi].status = MBX_NOT_IN_USE;
 524           }
 525 
 526         HOSTDATA(shpnt)->last_mbi_used = mbi;
 527 
 528         if (++mbi >= 2*BUSLOGIC_MAILBOXES)
 529           mbi = BUSLOGIC_MAILBOXES;
 530       }
 531 
 532     /*
 533       With interrupts no longer inhibited, iterate over the completed
 534       commands freeing resources and calling the completion routines.
 535       Since we exit upon completion of this loop, there is no need to
 536       inhibit interrupts before exit, as this will be handled by the
 537       fast interrupt assembly code we return to.
 538     */
 539 
 540     sti();
 541 
 542     for (i = 0; i < found; i++)
 543       {
 544         int mbo = saved_mbo[i];
 545         sctmp = HOSTDATA(shpnt)->sc[mbo];
 546         if (sctmp == NULL) continue;
 547         /*
 548           First, free any storage allocated for a scatter/gather
 549           data segment list.
 550         */
 551         if (sctmp->host_scribble)
 552           scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
 553         /*
 554           Next, mark the SCSI Command as completed so it may be reused
 555           for another command by buslogic_queuecommand.  This also signals
 556           to buslogic_reset that the command is no longer active.
 557         */
 558         HOSTDATA(shpnt)->sc[mbo] = NULL;
 559         /*
 560           Finally, call the SCSI command completion handler.
 561         */
 562         sctmp->scsi_done(sctmp);
 563       }
 564 }
 565 
 566 
 567 /* ??? Why does queuecommand return a value?  scsi.c never looks at it... */
 568 int buslogic_queuecommand(Scsi_Cmnd *scpnt, void (*done)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
 569 {
 570     static const unsigned char buscmd[] = { CMD_START_SCSI };
 571     unsigned char direction;
 572     unsigned char *cmd = (unsigned char *)scpnt->cmnd;
 573     unsigned char target = scpnt->target;
 574     unsigned char lun = scpnt->lun;
 575     void *buff = scpnt->request_buffer;
 576     int bufflen = scpnt->request_bufflen;
 577     int mbo;
 578     unsigned long flags;
 579     struct Scsi_Host *shpnt = scpnt->host;
 580     struct mailbox *mb = HOSTDATA(shpnt)->mb;
 581     struct ccb *ccb;
 582 
 583 
 584 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 585     if (target > 1) {
 586         scpnt->result = DID_TIME_OUT << 16;
 587         done(scpnt);
 588         return 0;
 589     }
 590 #endif
 591 
 592     if (*cmd == REQUEST_SENSE) {
 593 #if (BUSLOGIC_DEBUG & (BD_COMMAND | BD_ERRORS))
 594         if (bufflen != sizeof scpnt->sense_buffer) {
 595             buslogic_printk("wrong buffer length supplied for request sense"
 596                             " (%d).\n",
 597                             bufflen);
 598         }
 599 #endif
 600         scpnt->result = 0;
 601         done(scpnt);
 602         return 0;
 603     }
 604 
 605 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 606     {
 607         int i;
 608 
 609         if (*cmd == READ_10 || *cmd == WRITE_10
 610             || *cmd == READ_6 || *cmd == WRITE_6)
 611             i = *(int *)(cmd + 2);
 612         else
 613             i = -1;
 614         buslogic_printk("dev %d cmd %02X pos %d len %d ",
 615                         target, *cmd, i, bufflen);
 616         buslogic_stat(shpnt->io_port);
 617         buslogic_printk("dumping scsi cmd:");
 618         for (i = 0; i < scpnt->cmd_len; i++)
 619             printk(" %02X", cmd[i]);
 620         printk("\n");
 621         if (*cmd == WRITE_10 || *cmd == WRITE_6)
 622             return 0;   /* we are still testing, so *don't* write */
 623     }
 624 #endif
 625 
 626     /* Use the outgoing mailboxes in a round-robin fashion, because this
 627        is how the host adapter will scan for them. */
 628 
 629     save_flags(flags);
 630     cli();
 631 
 632     mbo = HOSTDATA(shpnt)->last_mbo_used + 1;
 633     if (mbo >= BUSLOGIC_MAILBOXES)
 634         mbo = 0;
 635 
 636     do {
 637         if (mb[mbo].status == MBX_NOT_IN_USE
 638             && HOSTDATA(shpnt)->sc[mbo] == NULL)
 639             break;
 640         mbo++;
 641         if (mbo >= BUSLOGIC_MAILBOXES)
 642             mbo = 0;
 643     } while (mbo != HOSTDATA(shpnt)->last_mbo_used);
 644 
 645     if (mb[mbo].status != MBX_NOT_IN_USE || HOSTDATA(shpnt)->sc[mbo]) {
 646         /* ??? Instead of failing, should we enable OMBR interrupts and sleep
 647            until we get one? */
 648         restore_flags(flags);
 649         buslogic_printk("unable to find empty mailbox.\n");
 650         goto fail;
 651     }
 652 
 653     HOSTDATA(shpnt)->sc[mbo] = scpnt;           /* This will effectively
 654                                                    prevent someone else from
 655                                                    screwing with this cdb. */
 656 
 657     HOSTDATA(shpnt)->last_mbo_used = mbo;
 658 
 659     restore_flags(flags);
 660 
 661 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 662     buslogic_printk("sending command (%d %08X)...", mbo, done);
 663 #endif
 664 
 665     ccb = &HOSTDATA(shpnt)->ccbs[mbo];
 666 
 667     /* This gets trashed for some reason */
 668     mb[mbo].ccbptr = ccb;
 669 
 670     memset(ccb, 0, sizeof (struct ccb));
 671 
 672     ccb->cdblen = scpnt->cmd_len;               /* SCSI Command Descriptor
 673                                                    Block Length */
 674 
 675     direction = 0;
 676     if (*cmd == READ_10 || *cmd == READ_6)
 677         direction = 8;
 678     else if (*cmd == WRITE_10 || *cmd == WRITE_6)
 679         direction = 16;
 680 
 681     memcpy(ccb->cdb, cmd, ccb->cdblen);
 682 
 683     if (scpnt->use_sg) {
 684         struct scatterlist *sgpnt;
 685         struct chain *cptr;
 686         size_t i;
 687 
 688         ccb->op = CCB_OP_INIT_SG;       /* SCSI Initiator Command
 689                                            w/scatter-gather */
 690         scpnt->host_scribble
 691             = (unsigned char *)scsi_malloc(BUSLOGIC_SG_MALLOC);
 692         if (scpnt->host_scribble == NULL) {
 693             buslogic_printk("unable to allocate DMA memory.\n");
 694             goto fail;
 695         }
 696         sgpnt = (struct scatterlist *)scpnt->request_buffer;
 697         cptr = (struct chain *)scpnt->host_scribble;
 698         if (scpnt->use_sg > shpnt->sg_tablesize) {
 699             buslogic_printk("bad segment list, %d > %d.\n",
 700                             scpnt->use_sg, shpnt->sg_tablesize);
 701             goto fail;
 702         }
 703         for (i = 0; i < scpnt->use_sg; i++) {
 704             CHECK_DMA_ADDR(shpnt->unchecked_isa_dma, sgpnt[i].address,
 705                            goto baddma);
 706             cptr[i].dataptr = sgpnt[i].address;
 707             cptr[i].datalen = sgpnt[i].length;
 708         }
 709         ccb->datalen = scpnt->use_sg * sizeof (struct chain);
 710         ccb->dataptr = cptr;
 711 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 712         {
 713             unsigned char *ptr;
 714 
 715             buslogic_printk("cptr %08X:", cptr);
 716             ptr = (unsigned char *)cptr;
 717             for (i = 0; i < 18; i++)
 718                 printk(" %02X", ptr[i]);
 719             printk("\n");
 720         }
 721 #endif
 722     } else {
 723         ccb->op = CCB_OP_INIT;  /* SCSI Initiator Command */
 724         scpnt->host_scribble = NULL;
 725         CHECK_DMA_ADDR(shpnt->unchecked_isa_dma, buff, goto baddma);
 726         ccb->datalen = bufflen;
 727         ccb->dataptr = buff;
 728     }
 729     ccb->id = target;
 730     ccb->lun = lun;
 731     ccb->dir = direction;
 732     ccb->rsalen = sizeof scpnt->sense_buffer;
 733     ccb->senseptr = scpnt->sense_buffer;
 734     /* ccbcontrol, commlinkid, and linkptr are 0 due to above memset. */
 735 
 736 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 737     {
 738         size_t i;
 739 
 740         buslogic_printk("sending...");
 741         for (i = 0; i < sizeof(struct ccb) - 10; i++)
 742             printk(" %02X", ((unsigned char *)ccb)[i]);
 743         printk("\n");
 744     }
 745 #endif
 746 
 747     if (done) {
 748 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 749         buslogic_printk("now waiting for interrupt: ");
 750         buslogic_stat(shpnt->io_port);
 751 #endif
 752         scpnt->scsi_done = done;
 753         mb[mbo].status = MBX_ACTION_START;
 754         /* start scsi command */
 755         buslogic_out(shpnt->io_port, buscmd, sizeof buscmd);
 756 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 757         buslogic_stat(shpnt->io_port);
 758 #endif
 759     } else
 760         buslogic_printk("done can't be NULL.\n");
 761 
 762     while (0) {
 763 #if defined(MODULE) && !defined(GFP_DMA)
 764       baddma:
 765         buslogic_printk("address > 16MB used for ISA HA.\n");
 766 #endif
 767       fail:
 768         scpnt->result = DID_ERROR << 16;
 769         done(scpnt);
 770     }
 771 
 772     return 0;
 773 }
 774 
 775 #if 0
 776 static void internal_done(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 777 {
 778     scpnt->SCp.Status++;
 779 }
 780 
 781 int buslogic_command(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 782 {
 783 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 784     buslogic_printk("calling buslogic_queuecommand.\n");
 785 #endif
 786 
 787     buslogic_queuecommand(scpnt, internal_done);
 788 
 789     scpnt->SCp.Status = 0;
 790     while (!scpnt->SCp.Status)
 791         continue;
 792     return scpnt->result;
 793 }
 794 #endif
 795 
 796 /* Initialize mailboxes. */
 797 static int setup_mailboxes(unsigned int base, struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 798 {
 799     size_t i;
 800     int ok = FALSE;             /* Innocent until proven guilty... */
 801     struct mailbox *mb = HOSTDATA(shpnt)->mb;
 802     struct ccb *ccb = HOSTDATA(shpnt)->ccbs;
 803     struct {
 804         unsigned char cmd, count;
 805         void *base PACKED;
 806     } cmd = { CMD_INITEXTMB, BUSLOGIC_MAILBOXES, mb };
 807 
 808     for (i = 0; i < BUSLOGIC_MAILBOXES; i++) {
 809         mb[i].status = mb[BUSLOGIC_MAILBOXES + i].status = MBX_NOT_IN_USE;
 810         mb[i].ccbptr = &ccb[i];
 811     }
 812     INTR_RESET(base);   /* reset interrupts, so they don't block */
 813 
 814     if (buslogic_out(base, (unsigned char *)&cmd, sizeof cmd))
 815         goto fail;
 816     WAIT_UNTIL(INTERRUPT(base), CMDC);
 817 
 818     ok = TRUE;
 819 
 820     while (0) {
 821       fail:
 822         buslogic_printk("failed setting up mailboxes.\n");
 823     }
 824 
 825     INTR_RESET(base);
 826 
 827     return !ok;
 828 }
 829 
 830 static int getconfig(unsigned int base, unsigned char *irq,
     /* [previous][next][first][last][top][bottom][index][help] */
 831                      unsigned char *dma, unsigned char *id,
 832                      char *bus_type, unsigned short *max_sg,
 833                      const unsigned char **bios)
 834 {
 835     unsigned char inquiry_cmd[2];
 836     unsigned char inquiry_result[4];
 837     int i;
 838 
 839 #if (BUSLOGIC_DEBUG & BD_DETECT)
 840     buslogic_printk("called\n");
 841 #endif
 842 
 843     i = inb(STATUS(base));
 844     if (i & DIRRDY)
 845         i = inb(DATA_IN(base));
 846     inquiry_cmd[0] = CMD_RETCONF;
 847     buslogic_out(base, inquiry_cmd, 1);
 848     if (buslogic_in(base, inquiry_result, 3))
 849         goto fail;
 850     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
 851     INTR_RESET(base);
 852     /* Defer using the DMA value until we know the bus type. */
 853     *dma = inquiry_result[0];
 854     switch (inquiry_result[1]) {
 855       case 0x01:
 856         *irq = 9;
 857         break;
 858       case 0x02:
 859         *irq = 10;
 860         break;
 861       case 0x04:
 862         *irq = 11;
 863         break;
 864       case 0x08:
 865         *irq = 12;
 866         break;
 867       case 0x20:
 868         *irq = 14;
 869         break;
 870       case 0x40:
 871         *irq = 15;
 872         break;
 873       default:
 874         buslogic_printk("unable to determine BusLogic IRQ level, "
 875                         " disabling board.\n");
 876         goto fail;
 877     }
 878     *id = inquiry_result[2] & 0x7;
 879 
 880     /* I expected Adaptec boards to fail on this, but it doesn't happen... */
 881     inquiry_cmd[0] = CMD_INQEXTSETUP;
 882     inquiry_cmd[1] = 4;
 883     if (buslogic_out(base, inquiry_cmd, 2))
 884         goto fail;
 885     if (buslogic_in(base, inquiry_result, inquiry_cmd[1]))
 886         goto fail;
 887     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
 888     if (inb(STATUS(base)) & CMDINV)
 889         goto fail;
 890     INTR_RESET(base);
 891 
 892     *bus_type = inquiry_result[0];
 893     CHECK(*bus_type == 'A' || *bus_type == 'E' || *bus_type == 'M');
 894 
 895     *bios = (const unsigned char *)((unsigned int)inquiry_result[1] << 12);
 896 
 897     *max_sg = (inquiry_result[3] << 8) | inquiry_result[2];
 898 
 899     /* We only need a DMA channel for ISA boards.  Some other types of boards
 900        (such as the 747S) have an option to report a DMA channel even though
 901        none is used (for compatibility with Adaptec drivers which require a
 902        DMA channel).  We ignore this. */
 903     if (*bus_type == 'A')
 904         switch (*dma) {
 905           case 0:       /* This indicates that no DMA channel is used. */
 906             *dma = 0;
 907             break;
 908           case 0x20:
 909             *dma = 5;
 910             break;
 911           case 0x40:
 912             *dma = 6;
 913             break;
 914           case 0x80:
 915             *dma = 7;
 916             break;
 917           default:
 918             buslogic_printk("unable to determine BusLogic DMA channel,"
 919                             " disabling board.\n");
 920             goto fail;
 921         }
 922     else
 923         *dma = 0;
 924 
 925     while (0) {
 926       fail:
 927 #if (BUSLOGIC_DEBUG & BD_DETECT)
 928         buslogic_printk("query board settings\n");
 929 #endif
 930         return TRUE;
 931     }
 932 
 933     return FALSE;
 934 }
 935 
 936 /* Query the board.  This acts both as part of the detection sequence and as a
 937    means to get necessary configuration information. */
 938 static int buslogic_query(unsigned int base, unsigned char *trans,
     /* [previous][next][first][last][top][bottom][index][help] */
 939                           unsigned char *irq, unsigned char *dma,
 940                           unsigned char *id, char *bus_type,
 941                           unsigned short *max_sg, const unsigned char **bios,
 942                           char *model, char *firmware_rev)
 943 {
 944     unsigned char inquiry_cmd[2];
 945     unsigned char inquiry_result[6];
 946     unsigned char geo;
 947     unsigned int i;
 948 
 949 #if (BUSLOGIC_DEBUG & BD_DETECT)
 950     buslogic_printk("called\n");
 951 #endif
 952 
 953     /* Quick and dirty test for presence of the card. */
 954     if (inb(STATUS(base)) == 0xFF)
 955         goto fail;
 956 
 957     /* Check the GEOMETRY port early for quick bailout on Adaptec boards. */
 958     geo = inb(GEOMETRY(base));
 959 #if (BUSLOGIC_DEBUG & BD_DETECT)
 960     buslogic_printk("geometry bits: %02X\n", geo);
 961 #endif
 962     /* Here is where we tell the men from the boys (i.e. Adaptec's don't
 963        support the GEOMETRY port, the men do :-) */
 964     if (geo == 0xFF)
 965         goto fail;
 966 
 967     /* In case some other card was probing here, reset interrupts. */
 968     INTR_RESET(base);
 969 
 970     /* Reset the adapter.  I ought to make a hard reset, but it's not really
 971        necessary. */
 972     outb(RSOFT | RINT/* | RSBUS*/, CONTROL(base));
 973 
 974     /* Wait a little bit for things to settle down. */
 975     i = jiffies + 2;
 976     while (i > jiffies);
 977 
 978     /* Expect INREQ and HARDY, any of the others are bad. */
 979     WAIT(STATUS(base), INREQ | HARDY, DACT | DFAIL | CMDINV | DIRRDY | CPRBSY);
 980 
 981     /* Shouldn't have generated any interrupts during reset. */
 982     if (inb(INTERRUPT(base)) & INTRMASK)
 983         goto fail;
 984 
 985     /* Getting the BusLogic firmware revision level is a bit tricky.  We get
 986        the first two digits (d.d) from CMD_INQUIRY and then use two undocumented
 987        commands to get the remaining digit and letter (d.ddl as in 3.31C). */
 988 
 989     inquiry_cmd[0] = CMD_INQUIRY;
 990     buslogic_out(base, inquiry_cmd, 1);
 991     if (buslogic_in(base, inquiry_result, 4))
 992         goto fail;
 993     /* Reading port should reset DIRRDY. */
 994     if (inb(STATUS(base)) & DIRRDY)
 995         goto fail;
 996     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
 997     INTR_RESET(base);
 998     firmware_rev[0] = inquiry_result[2];
 999     firmware_rev[1] = '.';
1000     firmware_rev[2] = inquiry_result[3];
1001     firmware_rev[3] = '\0';
1002 #if 0
1003     buslogic_printk("inquiry bytes: %02X(%c) %02X(%c)\n",
1004                     inquiry_result[0], inquiry_result[0],
1005                     inquiry_result[1], inquiry_result[1]);
1006 #endif
1007 
1008     if (getconfig(base, irq, dma, id, bus_type, max_sg, bios))
1009         goto fail;
1010 
1011     /* Set up defaults */
1012 #ifdef BIOS_TRANSLATION_OVERRIDE
1013     *trans = BIOS_TRANSLATION_OVERRIDE;
1014 #else
1015     *trans = BIOS_TRANSLATION_DEFAULT;
1016 #endif
1017     model[0] = '\0';
1018     model[6] = 0;
1019 
1020     /* ??? Begin undocumented command use.
1021        These may not be supported by clones. */
1022 
1023     do {
1024         /* ??? It appears as though AMI BusLogic clones don't implement this
1025            feature.  As an experiment, if we read a 00 we ignore the GEO_GT_1GB
1026            bit and skip all further undocumented commands. */
1027         if (geo == 0x00)
1028             break;
1029 #ifndef BIOS_TRANSLATION_OVERRIDE
1030         *trans = ((geo & GEO_GT_1GB)
1031                   ? BIOS_TRANSLATION_BIG : BIOS_TRANSLATION_DEFAULT);
1032 #endif
1033 
1034         inquiry_cmd[0] = CMD_VER_NO_LAST;
1035         buslogic_out(base, inquiry_cmd, 1);
1036         if (buslogic_in(base, inquiry_result, 1))
1037             break;
1038         WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
1039         INTR_RESET(base);
1040         firmware_rev[3] = inquiry_result[0];
1041         firmware_rev[4] = '\0';
1042 
1043         inquiry_cmd[0] = CMD_VER_NO_LETTER;
1044         buslogic_out(base, inquiry_cmd, 1);
1045         if (buslogic_in(base, inquiry_result, 1))
1046             break;
1047         WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
1048         INTR_RESET(base);
1049         firmware_rev[4] = inquiry_result[0];
1050         firmware_rev[5] = '\0';
1051 
1052         /* Use undocumented command to get model number and revision. */
1053 
1054         inquiry_cmd[0] = CMD_RET_MODEL_NO;
1055         inquiry_cmd[1] = 6;
1056         buslogic_out(base, inquiry_cmd, 2);
1057         if (buslogic_in(base, inquiry_result, inquiry_cmd[1]))
1058             break;
1059         WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
1060         INTR_RESET(base);
1061         memcpy(model, inquiry_result, 5);
1062         model[5] = '\0';
1063         model[6] = inquiry_result[5];
1064     } while (0);
1065 
1066     /* ??? End undocumented command use. */
1067 
1068     /* bus_type from getconfig doesn't differentiate between EISA/VESA.  We
1069        override using the model number here. */
1070     switch (*bus_type) {
1071       case 'E':
1072         switch (model[0]) {
1073           case '4':
1074             *bus_type = 'V';
1075             break;
1076           case '9':
1077             *bus_type = 'P';
1078             break;
1079           case '7':
1080             break;
1081           default:
1082             *bus_type = 'X';
1083             break;
1084         }
1085         break;
1086       default:
1087         break;
1088     }
1089 
1090     while (0) {
1091       fail:
1092 #if (BUSLOGIC_DEBUG & BD_DETECT)
1093         buslogic_printk("query board settings\n");
1094 #endif
1095         return TRUE;
1096     }
1097 
1098     return FALSE;
1099 }
1100 
1101 /* return non-zero on detection */
1102 int buslogic_detect(Scsi_Host_Template *tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1103 {
1104     unsigned char dma;
1105     unsigned char irq;
1106     unsigned int base;
1107     unsigned char id;
1108     char bus_type;
1109     unsigned short max_sg;
1110     unsigned char bios_translation;
1111     unsigned long flags;
1112     const unsigned char *bios;
1113     char *model;
1114     char *firmware_rev;
1115     struct Scsi_Host *shpnt;
1116     size_t indx;
1117     int unchecked_isa_dma;
1118     int count = 0;
1119 
1120 #if (BUSLOGIC_DEBUG & BD_DETECT)
1121     buslogic_printk("called\n");
1122 #endif
1123 
1124     tpnt->can_queue = BUSLOGIC_MAILBOXES;
1125     for (indx = 0; bases[indx] != 0; indx++)
1126         if (!check_region(bases[indx], 4)) {
1127             shpnt = scsi_register(tpnt, sizeof (struct hostdata));
1128 
1129             base = bases[indx];
1130 
1131             model = HOSTDATA(shpnt)->model;
1132             firmware_rev = HOSTDATA(shpnt)->firmware_rev;
1133             if (buslogic_query(base, &bios_translation, &irq, &dma, &id,
1134                                &bus_type, &max_sg, &bios, model, firmware_rev))
1135                 goto unregister;
1136 
1137 #if (BUSLOGIC_DEBUG & BD_DETECT)
1138             buslogic_stat(base);
1139 #endif
1140 
1141             /* Only type 'A' (AT/ISA) bus adapters use unchecked DMA. */
1142             unchecked_isa_dma = (bus_type == 'A');
1143 #ifndef CONFIG_NO_BUGGY_BUSLOGIC
1144             /* There is a hardware bug in the BT-445S prior to revision D.
1145                When the BIOS is enabled and you have more than 16MB of memory,
1146                the card mishandles memory transfers over 16MB which (if viewed
1147                as a 24-bit address) overlap with the BIOS address space.  For
1148                example if you have the BIOS located at physical address
1149                0xDC000 and a DMA transfer from the card to RAM starts at
1150                physical address 0x10DC000 then the transfer is messed up.  To
1151                be more precise every fourth byte of the transfer is messed up.
1152                (This analysis courtesy of Tomas Hurka, author of the NeXTSTEP
1153                BusLogic driver.) */
1154 
1155             if (bus_type == 'V'                             /* 445 */
1156                 && firmware_rev[0] <= '3'                   /* S */
1157                 && bios != NULL) {                          /* BIOS enabled */
1158 #if 1
1159                 /* Now that LNZ's forbidden_addr stuff is in the higher level
1160                    scsi code, we can use this instead. */
1161                 /* Avoid addresses which "mirror" the BIOS for DMA. */
1162                 shpnt->forbidden_addr = (unsigned long)bios;
1163                 shpnt->forbidden_size = 16 * 1024;
1164 #else
1165                 /* Use double-buffering. */
1166                 unchecked_isa_dma = TRUE;
1167 #endif
1168             }
1169 #endif
1170 
1171             CHECK_DMA_ADDR(unchecked_isa_dma, shpnt, goto unregister);
1172 
1173             if (setup_mailboxes(base, shpnt))
1174                 goto unregister;
1175 
1176             /* Set the Bus on/off-times as not to ruin floppy performance.
1177                CMD_BUSOFF_TIME is a noop for EISA boards (and possibly
1178                others???). */
1179             if (bus_type != 'E' && bus_type != 'P') {
1180                 /* The default ON/OFF times for BusLogic adapters is 7/4. */
1181                 static const unsigned char oncmd[] = { CMD_BUSON_TIME, 7 };
1182                 static const unsigned char offcmd[] = { CMD_BUSOFF_TIME, 5 };
1183 
1184                 INTR_RESET(base);
1185                 buslogic_out(base, oncmd, sizeof oncmd);
1186                 WAIT_UNTIL(INTERRUPT(base), CMDC);
1187                 INTR_RESET(base);
1188                 buslogic_out(base, offcmd, sizeof offcmd);
1189                 WAIT_UNTIL(INTERRUPT(base), CMDC);
1190                 while (0) {
1191                   fail:
1192                     buslogic_printk("setting bus on/off-time failed.\n");
1193                 }
1194                 INTR_RESET(base);
1195             }
1196 
1197             buslogic_printk("configuring %s HA at port 0x%03X, IRQ %u",
1198                             (bus_type == 'A' ? "ISA"
1199                              : (bus_type == 'E' ? "EISA"
1200                                 : (bus_type == 'M' ? "MCA"
1201                                    : (bus_type == 'P' ? "PCI"
1202                                       : (bus_type == 'V' ? "VESA"
1203                                          : (bus_type == 'X' ? "EISA/VESA/PCI"
1204                                             : "Unknown")))))),
1205                             base, irq);
1206             if (bios != NULL)
1207                 printk(", BIOS 0x%05X", (unsigned int)bios);
1208             if (dma != 0)
1209                 printk(", DMA %u", dma);
1210             printk(", ID %u\n", id);
1211             buslogic_printk("Model Number: %s",
1212                             (model[0] ? model : "Unknown"));
1213             if (model[0])
1214                 printk(" (revision %d)", model[6]);
1215             printk("\n");
1216             buslogic_printk("firmware revision: %s\n", firmware_rev);
1217 
1218 #if (BUSLOGIC_DEBUG & BD_DETECT)
1219             buslogic_stat(base);
1220 #endif
1221 
1222 #if (BUSLOGIC_DEBUG & BD_DETECT)
1223             buslogic_printk("enable interrupt channel %d.\n", irq);
1224 #endif
1225 
1226             save_flags(flags);
1227             cli();
1228             if (request_irq(irq, buslogic_interrupt,
1229                             SA_INTERRUPT, "buslogic")) {
1230                 buslogic_printk("unable to allocate IRQ for "
1231                                 "BusLogic controller.\n");
1232                 restore_flags(flags);
1233                 goto unregister;
1234             }
1235 
1236             if (dma) {
1237                 if (request_dma(dma, "buslogic")) {
1238                     buslogic_printk("unable to allocate DMA channel for "
1239                                     "BusLogic controller.\n");
1240                     free_irq(irq);
1241                     restore_flags(flags);
1242                     goto unregister;
1243                 }
1244 
1245                 /* The DMA-Controller.  We need to fool with this because we
1246                    want to be able to use an ISA BusLogic without having to
1247                    have the BIOS enabled. */
1248                 set_dma_mode(dma, DMA_MODE_CASCADE);
1249                 enable_dma(dma);
1250             }
1251 
1252             host[irq - 9] = shpnt;
1253             shpnt->this_id = id;
1254             shpnt->unchecked_isa_dma = unchecked_isa_dma;
1255             /* Have to keep cmd_per_lun at 1 for ISA machines otherwise lots
1256                of memory gets sucked up for bounce buffers.  */
1257             shpnt->cmd_per_lun = (unchecked_isa_dma ? 1 : BUSLOGIC_CMDLUN);
1258             shpnt->sg_tablesize = max_sg;
1259             if (shpnt->sg_tablesize > BUSLOGIC_MAX_SG)
1260                 shpnt->sg_tablesize = BUSLOGIC_MAX_SG;
1261             /* ??? shpnt->base should really be "const unsigned char *"... */
1262             shpnt->base = (unsigned char *)bios;
1263             shpnt->io_port = base;
1264             shpnt->n_io_port = 4;       /* Number of bytes of I/O space used */
1265             shpnt->dma_channel = dma;
1266             shpnt->irq = irq;
1267             HOSTDATA(shpnt)->bios_translation = bios_translation;
1268             if (bios_translation == BIOS_TRANSLATION_BIG)
1269                 buslogic_printk("using extended bios translation.\n");
1270             HOSTDATA(shpnt)->last_mbi_used = 2 * BUSLOGIC_MAILBOXES - 1;
1271             HOSTDATA(shpnt)->last_mbo_used = BUSLOGIC_MAILBOXES - 1;
1272             memset(HOSTDATA(shpnt)->sc, 0, sizeof HOSTDATA(shpnt)->sc);
1273             restore_flags(flags);
1274 
1275 #if 0
1276             {
1277                 unsigned char buf[8];
1278                 unsigned char cmd[]
1279                     = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1280                 size_t i;
1281 
1282 #if (BUSLOGIC_DEBUG & BD_DETECT)
1283                 buslogic_printk("*** READ CAPACITY ***\n");
1284 #endif
1285                 for (i = 0; i < sizeof buf; i++)
1286                     buf[i] = 0x87;
1287                 for (i = 0; i < 2; i++)
1288                     if (!buslogic_command(i, cmd, buf, sizeof buf)) {
1289                         buslogic_printk("LU %u sector_size %d device_size %d\n",
1290                                         i, *(int *)(buf + 4), *(int *)buf);
1291                     }
1292 
1293 #if (BUSLOGIC_DEBUG & BD_DETECT)
1294                 buslogic_printk("*** NOW RUNNING MY OWN TEST ***\n");
1295 #endif
1296                 for (i = 0; i < 4; i++) {
1297                     static buffer[512];
1298 
1299                     cmd[0] = READ_10;
1300                     cmd[1] = 0;
1301                     xany2scsi(cmd + 2, i);
1302                     cmd[6] = 0;
1303                     cmd[7] = 0;
1304                     cmd[8] = 1;
1305                     cmd[9] = 0;
1306                     buslogic_command(0, cmd, buffer, sizeof buffer);
1307                 }
1308             }
1309 #endif
1310 
1311             request_region(bases[indx], 4,"buslogic");
1312             /* Register the IO ports that we use */
1313             count++;
1314             continue;
1315           unregister:
1316             scsi_unregister(shpnt);
1317         }
1318     return count;
1319 }
1320 
1321 static int restart(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1322 {
1323     unsigned int i;
1324     unsigned int count = 0;
1325 #if 0
1326     static const unsigned char buscmd[] = { CMD_START_SCSI };
1327 #endif
1328 
1329     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1330         if (HOSTDATA(shpnt)->sc[i]
1331             && !HOSTDATA(shpnt)->sc[i]->device->soft_reset) {
1332 #if 0
1333             HOSTDATA(shpnt)->mb[i].status
1334                 = MBX_ACTION_START;     /* Indicate ready to restart... */
1335 #endif
1336             count++;
1337         }
1338 
1339     buslogic_printk("potential to restart %d stalled commands...\n", count);
1340 #if 0
1341     /* start scsi command */
1342     if (count)
1343         buslogic_out(shpnt->host->io_port, buscmd, sizeof buscmd);
1344 #endif
1345     return 0;
1346 }
1347 
1348 /* ??? The abort command for the aha1542 does not leave the device in a clean
1349    state where it is available to be used again.  As it is not clear whether
1350    the same problem exists with BusLogic boards, we will enable this and see
1351    if it works. */
1352 int buslogic_abort(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1353 {
1354 #if 1
1355     static const unsigned char buscmd[] = { CMD_START_SCSI };
1356     struct mailbox *mb;
1357     int mbi, mbo, last_mbi;
1358     unsigned long flags;
1359     unsigned int i;
1360 
1361     buslogic_printk("%X %X\n",
1362                     inb(STATUS(scpnt->host->io_port)),
1363                     inb(INTERRUPT(scpnt->host->io_port)));
1364 
1365     save_flags(flags);
1366     cli();
1367     mb = HOSTDATA(scpnt->host)->mb;
1368     last_mbi = HOSTDATA(scpnt->host)->last_mbi_used;
1369     mbi = last_mbi + 1;
1370     if (mbi >= 2 * BUSLOGIC_MAILBOXES)
1371         mbi = BUSLOGIC_MAILBOXES;
1372 
1373     do {
1374         if (mb[mbi].status != MBX_NOT_IN_USE)
1375             break;
1376         last_mbi = mbi;
1377         mbi++;
1378         if (mbi >= 2 * BUSLOGIC_MAILBOXES)
1379             mbi = BUSLOGIC_MAILBOXES;
1380     } while (mbi != HOSTDATA(scpnt->host)->last_mbi_used);
1381 
1382     if (mb[mbi].status != MBX_NOT_IN_USE) {
1383         buslogic_printk("lost interrupt discovered on irq %d, "
1384                         " - attempting to recover...\n",
1385                         scpnt->host->irq);
1386         HOSTDATA(scpnt->host)->last_mbi_used = last_mbi;
1387         buslogic_interrupt(scpnt->host->irq, NULL);
1388         restore_flags(flags);
1389         return SCSI_ABORT_SUCCESS;
1390     }
1391     restore_flags(flags);
1392 
1393     /* OK, no lost interrupt.  Try looking to see how many pending commands we
1394        think we have. */
1395     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1396         if (HOSTDATA(scpnt->host)->sc[i]) {
1397             if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
1398                 buslogic_printk("timed out command pending for %4.4X.\n",
1399                                 scpnt->request.dev);
1400                 if (HOSTDATA(scpnt->host)->mb[i].status != MBX_NOT_IN_USE) {
1401                     buslogic_printk("OGMB still full - restarting...\n");
1402                     buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
1403                 }
1404             } else
1405                 buslogic_printk("other pending command: %4.4X\n",
1406                                 scpnt->request.dev);
1407         }
1408 #endif
1409 
1410 #if (BUSLOGIC_DEBUG & BD_ABORT)
1411     buslogic_printk("called\n");
1412 #endif
1413 
1414 #if 1
1415     /* This section of code should be used carefully - some devices cannot
1416        abort a command, and this merely makes it worse. */
1417     save_flags(flags);
1418     cli();
1419     for (mbo = 0; mbo < BUSLOGIC_MAILBOXES; mbo++)
1420         if (scpnt == HOSTDATA(scpnt->host)->sc[mbo]) {
1421             mb[mbo].status = MBX_ACTION_ABORT;
1422             buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
1423             break;
1424         }
1425     restore_flags(flags);
1426 #endif
1427 
1428     return SCSI_ABORT_SNOOZE;
1429 }
1430 
1431 /* We do not implement a reset function here, but the upper level code assumes
1432    that it will get some kind of response for the command in scpnt.  We must
1433    oblige, or the command will hang the SCSI system.  For a first go, we assume
1434    that the BusLogic notifies us with all of the pending commands (it does
1435    implement soft reset, after all). */
1436 int buslogic_reset(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1437 {
1438     static const unsigned char buscmd[] = { CMD_START_SCSI };
1439     unsigned int i;
1440 
1441 #if (BUSLOGIC_DEBUG & BD_RESET)
1442     buslogic_printk("called\n");
1443 #endif
1444 #if 0
1445     /* This does a scsi reset for all devices on the bus. */
1446     outb(RSBUS, CONTROL(scpnt->host->io_port));
1447 #else
1448     /* This does a selective reset of just the one device. */
1449     /* First locate the ccb for this command. */
1450     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1451         if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
1452             HOSTDATA(scpnt->host)->ccbs[i].op = CCB_OP_BUS_RESET;
1453 
1454             /* Now tell the BusLogic to flush all pending commands for this
1455                target. */
1456             buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
1457 
1458             /* Here is the tricky part.  What to do next.  Do we get an
1459                interrupt for the commands that we aborted with the specified
1460                target, or do we generate this on our own?  Try it without first
1461                and see what happens. */
1462             buslogic_printk("sent BUS DEVICE RESET to target %d.\n",
1463                             scpnt->target);
1464 
1465             /* If the first does not work, then try the second.  I think the
1466                first option is more likely to be correct.  Free the command
1467                block for all commands running on this target... */
1468 #if 1
1469             for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1470                 if (HOSTDATA(scpnt->host)->sc[i]
1471                     && HOSTDATA(scpnt->host)->sc[i]->target == scpnt->target) {
1472                     Scsi_Cmnd *sctmp = HOSTDATA(scpnt->host)->sc[i];
1473 
1474                     sctmp->result = DID_RESET << 16;
1475                     if (sctmp->host_scribble)
1476                         scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
1477                     buslogic_printk("sending DID_RESET for target %d.\n",
1478                                     scpnt->target);
1479                     sctmp->scsi_done(scpnt);
1480 
1481                     HOSTDATA(scpnt->host)->sc[i] = NULL;
1482                     HOSTDATA(scpnt->host)->mb[i].status = MBX_NOT_IN_USE;
1483                 }
1484             return SCSI_RESET_SUCCESS;
1485 #else
1486             return SCSI_RESET_PENDING;
1487 #endif
1488         }
1489 #endif
1490     /* No active command at this time, so this means that each time we got some
1491        kind of response the last time through.  Tell the mid-level code to
1492        request sense information in order to decide what to do next. */
1493     return SCSI_RESET_PUNT;
1494 }
1495 
1496 /* ??? This is probably not correct for series "C" boards.  I believe these
1497    support separate mappings for each disk.  We would need to issue a
1498    CMD_READ_FW_LOCAL_RAM command to check for the particular drive being
1499    queried.  Note that series "C" boards can be differentiated by having
1500    HOSTDATA(disk->device->host)->firmware_rev[0] >= '4'. */
1501 int buslogic_biosparam(Disk *disk, int dev, int *ip)
     /* [previous][next][first][last][top][bottom][index][help] */
1502 {
1503     unsigned int size = disk->capacity;
1504 
1505     /* ip[0] == heads, ip[1] == sectors, ip[2] == cylinders */
1506     if (HOSTDATA(disk->device->host)->bios_translation == BIOS_TRANSLATION_BIG
1507         && size >= 0x200000) {          /* 1GB */
1508         if (size >= 0x400000) {         /* 2GB */
1509 #if 0   /* ??? Used in earlier kernels, but disagrees with BusLogic info. */
1510             if (mb >= 0x800000) {       /* 4GB */
1511                 ip[0] = 256;
1512                 ip[1] = 64;
1513             } else {
1514                 ip[0] = 256;
1515                 ip[1] = 32;
1516             }
1517 #else
1518             ip[0] = 256;
1519             ip[1] = 64;
1520 #endif
1521         } else {
1522             ip[0] = 128;
1523             ip[1] = 32;
1524         }
1525     } else {
1526         ip[0] = 64;
1527         ip[1] = 32;
1528     }
1529     ip[2] = size / (ip[0] * ip[1]);
1530 /*    if (ip[2] > 1024)
1531         ip[2] = 1024; */
1532     return 0;
1533 }
1534 
1535 /* called from init/main.c */
1536 void buslogic_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
1537 {
1538     static const unsigned short valid_bases[]
1539         = { 0x130, 0x134, 0x230, 0x234, 0x330, 0x334 };
1540     static size_t setup_idx = 0;
1541     size_t i;
1542 
1543     if (setup_idx >= ARRAY_SIZE(bases) - 1) {
1544         buslogic_printk("called too many times.  Bad LILO params?\n");
1545         return;
1546     }
1547     if (ints[0] != 1) {
1548         buslogic_printk("malformed command line.\n");
1549         buslogic_printk("usage: buslogic=<portbase>\n");
1550         return;
1551     }
1552     for (i = 0; i < ARRAY_SIZE(valid_bases); i++)
1553         if (valid_bases[i] == ints[1]) {
1554             bases[setup_idx++] = ints[1];
1555             bases[setup_idx] = 0;
1556             return;
1557         }
1558     buslogic_printk("invalid base 0x%X specified.\n", ints[i]);
1559 }
1560 
1561 #ifdef MODULE
1562 /* Eventually this will go into an include file, but that's later... */
1563 Scsi_Host_Template driver_template = BUSLOGIC;
1564 
1565 # include "scsi_module.c"
1566 #endif

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