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

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