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

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