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 junk)
     /* [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 irqno, base, flag;
 434     int needs_restart;
 435     struct mailbox *mb;
 436     struct ccb *ccb;
 437 
 438     /* Magic - this -2 is only required for slow interrupt handlers */
 439     irqno = ((int *)junk)[-2];
 440 
 441     shpnt = host[irqno - 9];
 442     if (!shpnt)
 443         panic("buslogic_interrupt: NULL SCSI host entry");
 444 
 445     mb = HOSTDATA(shpnt)->mb;
 446     ccb = HOSTDATA(shpnt)->ccbs;
 447     base = shpnt->io_port;
 448 
 449 #if (BUSLOGIC_DEBUG & BD_INTERRUPT)
 450     flag = inb(INTERRUPT(base));
 451 
 452     buslogic_printk("");
 453     if (!(flag & INTV))
 454         printk("no interrupt? ");
 455     if (flag & IMBL)
 456         printk("IMBL ");
 457     if (flag & MBOR)
 458         printk("MBOR ");
 459     if (flag & CMDC)
 460         printk("CMDC ");
 461     if (flag & RSTS)
 462         printk("RSTS ");
 463     printk("status %02X\n", inb(STATUS(base)));
 464 #endif
 465 
 466     number_serviced = 0;
 467     needs_restart = 0;
 468 
 469     for (;;) {
 470         flag = inb(INTERRUPT(base));
 471 
 472         /* Check for unusual interrupts.  If any of these happen, we should
 473            probably do something special, but for now just printing a message
 474            is sufficient.  A SCSI reset detected is something that we really
 475            need to deal with in some way. */
 476         if (flag & (MBOR | CMDC | RSTS)) {
 477             buslogic_printk("unusual flag:");
 478             if (flag & MBOR)
 479                 printk(" MBOR");
 480             if (flag & CMDC)
 481                 printk(" CMDC");
 482             if (flag & RSTS) {
 483                 needs_restart = 1;
 484                 printk(" RSTS");
 485             }
 486             printk("\n");
 487         }
 488 
 489         INTR_RESET(base);
 490 
 491         save_flags(flags);
 492         cli();
 493 
 494         mbi = HOSTDATA(shpnt)->last_mbi_used + 1;
 495         if (mbi >= 2 * BUSLOGIC_MAILBOXES)
 496             mbi = BUSLOGIC_MAILBOXES;
 497 
 498         /* I use the "found" variable as I like to keep cli/sti pairs at the
 499            same block level.  Debugging dropped sti's is no fun... */
 500 
 501         found = FALSE;
 502         do {
 503             if (mb[mbi].status != MBX_NOT_IN_USE) {
 504                 found = TRUE;
 505                 break;
 506             }
 507             mbi++;
 508             if (mbi >= 2 * BUSLOGIC_MAILBOXES)
 509                 mbi = BUSLOGIC_MAILBOXES;
 510         } while (mbi != HOSTDATA(shpnt)->last_mbi_used);
 511 
 512         if (found) {
 513             mbo = (struct ccb *)mb[mbi].ccbptr - ccb;
 514             mbistatus = mb[mbi].status;
 515             mb[mbi].status = MBX_NOT_IN_USE;
 516             HOSTDATA(shpnt)->last_mbi_used = mbi;
 517         }
 518 
 519         restore_flags(flags);
 520 
 521         if (!found) {
 522             /* Hmm, no mail.  Must have read it the last time around. */
 523             if (!number_serviced && !needs_restart)
 524                 buslogic_printk("interrupt received, but no mail.\n");
 525             /* We detected a reset.  Restart all pending commands for devices
 526                that use the hard reset option. */
 527             if (needs_restart)
 528                 restart(shpnt);
 529             return;
 530         }
 531 
 532 #if (BUSLOGIC_DEBUG & BD_INTERRUPT)
 533         if (ccb[mbo].tarstat || ccb[mbo].hastat)
 534             buslogic_printk("returning %08X (status %d).\n",
 535                             ((int)ccb[mbo].hastat << 16) | ccb[mbo].tarstat,
 536                             mb[mbi].status);
 537 #endif
 538 
 539         if (mbistatus == MBX_COMPLETION_NOT_FOUND)
 540             continue;
 541 
 542 #if (BUSLOGIC_DEBUG & BD_INTERRUPT)
 543         buslogic_printk("...done %u %u\n", mbo, mbi);
 544 #endif
 545 
 546         sctmp = HOSTDATA(shpnt)->sc[mbo];
 547 
 548         if (!sctmp || !sctmp->scsi_done) {
 549             buslogic_printk("unexpected interrupt.\n");
 550             buslogic_printk("tarstat=%02X, hastat=%02X id=%d lun=%d ccb#=%u\n",
 551                             ccb[mbo].tarstat, ccb[mbo].hastat,
 552                             ccb[mbo].id, ccb[mbo].lun, mbo);
 553             return;
 554         }
 555 
 556         my_done = sctmp->scsi_done;
 557         if (sctmp->host_scribble)
 558             scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
 559 
 560         /* ??? more error checking left out here */
 561         if (mbistatus != MBX_COMPLETION_OK) {
 562             /* ??? This is surely wrong, but I don't know what's right. */
 563             errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
 564         } else
 565             errstatus = 0;
 566 
 567 #if (BUSLOGIC_DEBUG & BD_INTERRUPT)
 568         if (errstatus)
 569             buslogic_printk("error: %04X %04X\n",
 570                             ccb[mbo].hastat, ccb[mbo].tarstat);
 571 
 572         if (status_byte(ccb[mbo].tarstat) == CHECK_CONDITION) {
 573             size_t i;
 574 
 575             buslogic_printk("sense:");
 576             for (i = 0; i < sizeof sctmp->sense_buffer; i++)
 577                 printk(" %02X", sctmp->sense_buffer[i]);
 578             printk("\n");
 579         }
 580 
 581         if (errstatus)
 582             buslogic_printk("returning %08X.\n", errstatus);
 583 #endif
 584 
 585         sctmp->result = errstatus;
 586         HOSTDATA(shpnt)->sc[mbo] = NULL;        /* This effectively frees up
 587                                                    the mailbox slot, as far as
 588                                                    queuecommand is
 589                                                    concerned. */
 590         my_done(sctmp);
 591         number_serviced++;
 592     }
 593 }
 594 
 595 /* ??? Why does queuecommand return a value?  scsi.c never looks at it... */
 596 int buslogic_queuecommand(Scsi_Cmnd *scpnt, void (*done)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
 597 {
 598     static const unsigned char buscmd[] = { CMD_START_SCSI };
 599     unsigned char direction;
 600     unsigned char *cmd = (unsigned char *)scpnt->cmnd;
 601     unsigned char target = scpnt->target;
 602     unsigned char lun = scpnt->lun;
 603     void *buff = scpnt->request_buffer;
 604     int bufflen = scpnt->request_bufflen;
 605     int mbo;
 606     unsigned long flags;
 607     struct mailbox *mb;
 608     struct ccb *ccb;
 609     struct Scsi_Host *shpnt = scpnt->host;
 610 
 611 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 612     if (target > 1) {
 613         scpnt->result = DID_TIME_OUT << 16;
 614         done(scpnt);
 615         return 0;
 616     }
 617 #endif
 618 
 619     if (*cmd == REQUEST_SENSE) {
 620 #if (BUSLOGIC_DEBUG & (BD_COMMAND | BD_ERRORS))
 621         if (bufflen != sizeof scpnt->sense_buffer) {
 622             buslogic_printk("wrong buffer length supplied for request sense"
 623                             " (%d).\n",
 624                             bufflen);
 625         }
 626 #endif
 627         scpnt->result = 0;
 628         done(scpnt);
 629         return 0;
 630     }
 631 
 632 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 633     {
 634         int i;
 635 
 636         if (*cmd == READ_10 || *cmd == WRITE_10
 637             || *cmd == READ_6 || *cmd == WRITE_6)
 638             i = *(int *)(cmd + 2);
 639         else
 640             i = -1;
 641         buslogic_printk("dev %d cmd %02X pos %d len %d ",
 642                         target, *cmd, i, bufflen);
 643         buslogic_stat(shpnt->io_port);
 644         buslogic_printk("dumping scsi cmd:");
 645         for (i = 0; i < scpnt->cmd_len; i++)
 646             printk(" %02X", cmd[i]);
 647         printk("\n");
 648         if (*cmd == WRITE_10 || *cmd == WRITE_6)
 649             return 0;   /* we are still testing, so *don't* write */
 650     }
 651 #endif
 652 
 653     mb = HOSTDATA(shpnt)->mb;
 654     ccb = HOSTDATA(shpnt)->ccbs;
 655 
 656     /* Use the outgoing mailboxes in a round-robin fashion, because this
 657        is how the host adapter will scan for them. */
 658 
 659     save_flags(flags);
 660     cli();
 661 
 662     mbo = HOSTDATA(shpnt)->last_mbo_used + 1;
 663     if (mbo >= BUSLOGIC_MAILBOXES)
 664         mbo = 0;
 665 
 666     do {
 667         if (mb[mbo].status == MBX_NOT_IN_USE
 668             && HOSTDATA(shpnt)->sc[mbo] == NULL)
 669             break;
 670         mbo++;
 671         if (mbo >= BUSLOGIC_MAILBOXES)
 672             mbo = 0;
 673     } while (mbo != HOSTDATA(shpnt)->last_mbo_used);
 674 
 675     if (mb[mbo].status != MBX_NOT_IN_USE || HOSTDATA(shpnt)->sc[mbo]) {
 676         /* ??? Instead of failing, should we enable OMBR interrupts and sleep
 677            until we get one? */
 678         restore_flags(flags);
 679         buslogic_printk("unable to find empty mailbox.\n");
 680         goto fail;
 681     }
 682 
 683     HOSTDATA(shpnt)->sc[mbo] = scpnt;           /* This will effectively
 684                                                    prevent someone else from
 685                                                    screwing with this cdb. */
 686 
 687     HOSTDATA(shpnt)->last_mbo_used = mbo;
 688 
 689     restore_flags(flags);
 690 
 691 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 692     buslogic_printk("sending command (%d %08X)...", mbo, done);
 693 #endif
 694 
 695     /* This gets trashed for some reason */
 696     mb[mbo].ccbptr = &ccb[mbo];
 697 
 698     memset(&ccb[mbo], 0, sizeof (struct ccb));
 699 
 700     ccb[mbo].cdblen = scpnt->cmd_len;           /* SCSI Command Descriptor
 701                                                    Block Length */
 702 
 703     direction = 0;
 704     if (*cmd == READ_10 || *cmd == READ_6)
 705         direction = 8;
 706     else if (*cmd == WRITE_10 || *cmd == WRITE_6)
 707         direction = 16;
 708 
 709     memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
 710 
 711     if (scpnt->use_sg) {
 712         struct scatterlist *sgpnt;
 713         struct chain *cptr;
 714         size_t i;
 715 
 716         ccb[mbo].op = CCB_OP_INIT_SG;   /* SCSI Initiator Command
 717                                            w/scatter-gather */
 718         scpnt->host_scribble
 719             = (unsigned char *)scsi_malloc(BUSLOGIC_SG_MALLOC);
 720         if (scpnt->host_scribble == NULL) {
 721             buslogic_printk("unable to allocate DMA memory.\n");
 722             goto fail;
 723         }
 724         sgpnt = (struct scatterlist *)scpnt->request_buffer;
 725         cptr = (struct chain *)scpnt->host_scribble;
 726         if (scpnt->use_sg > shpnt->sg_tablesize) {
 727             buslogic_printk("bad segment list, %d > %d.\n",
 728                             scpnt->use_sg, shpnt->sg_tablesize);
 729             goto fail;
 730         }
 731         for (i = 0; i < scpnt->use_sg; i++) {
 732             CHECK_DMA_ADDR(shpnt->unchecked_isa_dma, sgpnt[i].address,
 733                            goto baddma);
 734             cptr[i].dataptr = sgpnt[i].address;
 735             cptr[i].datalen = sgpnt[i].length;
 736         }
 737         ccb[mbo].datalen = scpnt->use_sg * sizeof (struct chain);
 738         ccb[mbo].dataptr = cptr;
 739 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 740         {
 741             unsigned char *ptr;
 742 
 743             buslogic_printk("cptr %08X:", cptr);
 744             ptr = (unsigned char *)cptr;
 745             for (i = 0; i < 18; i++)
 746                 printk(" %02X", ptr[i]);
 747             printk("\n");
 748         }
 749 #endif
 750     } else {
 751         ccb[mbo].op = CCB_OP_INIT;      /* SCSI Initiator Command */
 752         scpnt->host_scribble = NULL;
 753         CHECK_DMA_ADDR(shpnt->unchecked_isa_dma, buff, goto baddma);
 754         ccb[mbo].datalen = bufflen;
 755         ccb[mbo].dataptr = buff;
 756     }
 757     ccb[mbo].id = target;
 758     ccb[mbo].lun = lun;
 759     ccb[mbo].dir = direction;
 760     ccb[mbo].rsalen = sizeof scpnt->sense_buffer;
 761     ccb[mbo].senseptr = scpnt->sense_buffer;
 762     ccb[mbo].linkptr = NULL;
 763     ccb[mbo].commlinkid = 0;
 764 
 765 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 766     {
 767         size_t i;
 768 
 769         buslogic_printk("sending...");
 770         for (i = 0; i < sizeof ccb[mbo] - 10; i++)
 771             printk(" %02X", ((unsigned char *)&ccb[mbo])[i]);
 772         printk("\n");
 773     }
 774 #endif
 775 
 776     if (done) {
 777 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 778         buslogic_printk("now waiting for interrupt: ");
 779         buslogic_stat(shpnt->io_port);
 780 #endif
 781         scpnt->scsi_done = done;
 782         mb[mbo].status = MBX_ACTION_START;
 783         /* start scsi command */
 784         buslogic_out(shpnt->io_port, buscmd, sizeof buscmd);
 785 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 786         buslogic_stat(shpnt->io_port);
 787 #endif
 788     } else
 789         buslogic_printk("done can't be NULL.\n");
 790 
 791     while (0) {
 792 #if defined(MODULE) && !defined(GFP_DMA)
 793       baddma:
 794         buslogic_printk("address > 16MB used for ISA HA.\n");
 795 #endif
 796       fail:
 797         scpnt->result = DID_ERROR << 16;
 798         done(scpnt);
 799     }
 800 
 801     return 0;
 802 }
 803 
 804 #if 0
 805 static void internal_done(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 806 {
 807     scpnt->SCp.Status++;
 808 }
 809 
 810 int buslogic_command(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 811 {
 812 #if (BUSLOGIC_DEBUG & BD_COMMAND)
 813     buslogic_printk("calling buslogic_queuecommand.\n");
 814 #endif
 815 
 816     buslogic_queuecommand(scpnt, internal_done);
 817 
 818     scpnt->SCp.Status = 0;
 819     while (!scpnt->SCp.Status)
 820         continue;
 821     return scpnt->result;
 822 }
 823 #endif
 824 
 825 /* Initialize mailboxes. */
 826 static int setup_mailboxes(unsigned int base, struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 827 {
 828     size_t i;
 829     int ok = FALSE;             /* Innocent until proven guilty... */
 830     struct mailbox *mb = HOSTDATA(shpnt)->mb;
 831     struct ccb *ccb = HOSTDATA(shpnt)->ccbs;
 832     struct {
 833         unsigned char cmd, count;
 834         void *base PACKED;
 835     } cmd = { CMD_INITEXTMB, BUSLOGIC_MAILBOXES, mb };
 836 
 837     for (i = 0; i < BUSLOGIC_MAILBOXES; i++) {
 838         mb[i].status = mb[BUSLOGIC_MAILBOXES + i].status = MBX_NOT_IN_USE;
 839         mb[i].ccbptr = &ccb[i];
 840     }
 841     INTR_RESET(base);   /* reset interrupts, so they don't block */
 842 
 843     if (buslogic_out(base, (unsigned char *)&cmd, sizeof cmd))
 844         goto fail;
 845     WAIT_UNTIL(INTERRUPT(base), CMDC);
 846 
 847     ok = TRUE;
 848 
 849     while (0) {
 850       fail:
 851         buslogic_printk("failed setting up mailboxes.\n");
 852     }
 853 
 854     INTR_RESET(base);
 855 
 856     return !ok;
 857 }
 858 
 859 static int getconfig(unsigned int base, unsigned char *irq,
     /* [previous][next][first][last][top][bottom][index][help] */
 860                      unsigned char *dma, unsigned char *id,
 861                      char *bus_type, unsigned short *max_sg,
 862                      const unsigned char **bios)
 863 {
 864     unsigned char inquiry_cmd[2];
 865     unsigned char inquiry_result[4];
 866     int i;
 867 
 868 #if (BUSLOGIC_DEBUG & BD_DETECT)
 869     buslogic_printk("called\n");
 870 #endif
 871 
 872     i = inb(STATUS(base));
 873     if (i & DIRRDY)
 874         i = inb(DATA_IN(base));
 875     inquiry_cmd[0] = CMD_RETCONF;
 876     buslogic_out(base, inquiry_cmd, 1);
 877     if (buslogic_in(base, inquiry_result, 3))
 878         goto fail;
 879     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
 880     INTR_RESET(base);
 881     /* Defer using the DMA value until we know the bus type. */
 882     *dma = inquiry_result[0];
 883     switch (inquiry_result[1]) {
 884       case 0x01:
 885         *irq = 9;
 886         break;
 887       case 0x02:
 888         *irq = 10;
 889         break;
 890       case 0x04:
 891         *irq = 11;
 892         break;
 893       case 0x08:
 894         *irq = 12;
 895         break;
 896       case 0x20:
 897         *irq = 14;
 898         break;
 899       case 0x40:
 900         *irq = 15;
 901         break;
 902       default:
 903         buslogic_printk("unable to determine BusLogic IRQ level, "
 904                         " disabling board.\n");
 905         goto fail;
 906     }
 907     *id = inquiry_result[2] & 0x7;
 908 
 909     /* I expected Adaptec boards to fail on this, but it doesn't happen... */
 910     inquiry_cmd[0] = CMD_INQEXTSETUP;
 911     inquiry_cmd[1] = 4;
 912     if (buslogic_out(base, inquiry_cmd, 2))
 913         goto fail;
 914     if (buslogic_in(base, inquiry_result, inquiry_cmd[1]))
 915         goto fail;
 916     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
 917     if (inb(STATUS(base)) & CMDINV)
 918         goto fail;
 919     INTR_RESET(base);
 920 
 921     *bus_type = inquiry_result[0];
 922     CHECK(*bus_type == 'A' || *bus_type == 'E' || *bus_type == 'M');
 923 
 924     *bios = (const unsigned char *)((unsigned int)inquiry_result[1] << 12);
 925 
 926     *max_sg = (inquiry_result[3] << 8) | inquiry_result[2];
 927 
 928     /* We only need a DMA channel for ISA boards.  Some other types of boards
 929        (such as the 747S) have an option to report a DMA channel even though
 930        none is used (for compatibility with Adaptec drivers which require a
 931        DMA channel).  We ignore this. */
 932     if (*bus_type == 'A')
 933         switch (*dma) {
 934           case 0:       /* This indicates that no DMA channel is used. */
 935             *dma = 0;
 936             break;
 937           case 0x20:
 938             *dma = 5;
 939             break;
 940           case 0x40:
 941             *dma = 6;
 942             break;
 943           case 0x80:
 944             *dma = 7;
 945             break;
 946           default:
 947             buslogic_printk("unable to determine BusLogic DMA channel,"
 948                             " disabling board.\n");
 949             goto fail;
 950         }
 951     else
 952         *dma = 0;
 953 
 954     while (0) {
 955       fail:
 956 #if (BUSLOGIC_DEBUG & BD_DETECT)
 957         buslogic_printk("query board settings\n");
 958 #endif
 959         return TRUE;
 960     }
 961 
 962     return FALSE;
 963 }
 964 
 965 /* Query the board.  This acts both as part of the detection sequence and as a
 966    means to get necessary configuration information. */
 967 static int buslogic_query(unsigned int base, unsigned char *trans,
     /* [previous][next][first][last][top][bottom][index][help] */
 968                           unsigned char *irq, unsigned char *dma,
 969                           unsigned char *id, char *bus_type,
 970                           unsigned short *max_sg, const unsigned char **bios,
 971                           char *model, char *firmware_rev)
 972 {
 973     unsigned char inquiry_cmd[2];
 974     unsigned char inquiry_result[6];
 975     unsigned char geo;
 976     unsigned int i;
 977 
 978 #if (BUSLOGIC_DEBUG & BD_DETECT)
 979     buslogic_printk("called\n");
 980 #endif
 981 
 982     /* Quick and dirty test for presence of the card. */
 983     if (inb(STATUS(base)) == 0xFF)
 984         goto fail;
 985 
 986     /* Check the GEOMETRY port early for quick bailout on Adaptec boards. */
 987     geo = inb(GEOMETRY(base));
 988 #if (BUSLOGIC_DEBUG & BD_DETECT)
 989     buslogic_printk("geometry bits: %02X\n", geo);
 990 #endif
 991     /* Here is where we tell the men from the boys (i.e. Adaptec's don't
 992        support the GEOMETRY port, the men do :-) */
 993     if (geo == 0xFF)
 994         goto fail;
 995 
 996     /* In case some other card was probing here, reset interrupts. */
 997     INTR_RESET(base);
 998 
 999     /* Reset the adapter.  I ought to make a hard reset, but it's not really
1000        necessary. */
1001     outb(RSOFT | RINT/* | RSBUS*/, CONTROL(base));
1002 
1003     /* Wait a little bit for things to settle down. */
1004     i = jiffies + 2;
1005     while (i > jiffies);
1006 
1007     /* Expect INREQ and HARDY, any of the others are bad. */
1008     WAIT(STATUS(base), INREQ | HARDY, DACT | DFAIL | CMDINV | DIRRDY | CPRBSY);
1009 
1010     /* Shouldn't have generated any interrupts during reset. */
1011     if (inb(INTERRUPT(base)) & INTRMASK)
1012         goto fail;
1013 
1014     /* Getting the BusLogic firmware revision level is a bit tricky.  We get
1015        the first two digits (d.d) from CMD_INQUIRY and then use two undocumented
1016        commands to get the remaining digit and letter (d.ddl as in 3.31C). */
1017 
1018     inquiry_cmd[0] = CMD_INQUIRY;
1019     buslogic_out(base, inquiry_cmd, 1);
1020     if (buslogic_in(base, inquiry_result, 4))
1021         goto fail;
1022     /* Reading port should reset DIRRDY. */
1023     if (inb(STATUS(base)) & DIRRDY)
1024         goto fail;
1025     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
1026     INTR_RESET(base);
1027     firmware_rev[0] = inquiry_result[2];
1028     firmware_rev[1] = '.';
1029     firmware_rev[2] = inquiry_result[3];
1030     firmware_rev[3] = '\0';
1031 #if 0
1032     buslogic_printk("inquiry bytes: %02X(%c) %02X(%c)\n",
1033                     inquiry_result[0], inquiry_result[0],
1034                     inquiry_result[1], inquiry_result[1]);
1035 #endif
1036 
1037     if (getconfig(base, irq, dma, id, bus_type, max_sg, bios))
1038         goto fail;
1039 
1040     /* Set up defaults */
1041 #ifdef BIOS_TRANSLATION_OVERRIDE
1042     *trans = BIOS_TRANSLATION_OVERRIDE;
1043 #else
1044     *trans = BIOS_TRANSLATION_DEFAULT;
1045 #endif
1046     model[0] = '\0';
1047     model[6] = 0;
1048 
1049     /* ??? Begin undocumented command use.
1050        These may not be supported by clones. */
1051 
1052     do {
1053         /* ??? It appears as though AMI BusLogic clones don't implement this
1054            feature.  As an experiment, if we read a 00 we ignore the GEO_GT_1GB
1055            bit and skip all further undocumented commands. */
1056         if (geo == 0x00)
1057             break;
1058 #ifndef BIOS_TRANSLATION_OVERRIDE
1059         *trans = ((geo & GEO_GT_1GB)
1060                   ? BIOS_TRANSLATION_BIG : BIOS_TRANSLATION_DEFAULT);
1061 #endif
1062 
1063         inquiry_cmd[0] = CMD_VER_NO_LAST;
1064         buslogic_out(base, inquiry_cmd, 1);
1065         if (buslogic_in(base, inquiry_result, 1))
1066             break;
1067         WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
1068         INTR_RESET(base);
1069         firmware_rev[3] = inquiry_result[0];
1070         firmware_rev[4] = '\0';
1071 
1072         inquiry_cmd[0] = CMD_VER_NO_LETTER;
1073         buslogic_out(base, inquiry_cmd, 1);
1074         if (buslogic_in(base, inquiry_result, 1))
1075             break;
1076         WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
1077         INTR_RESET(base);
1078         firmware_rev[4] = inquiry_result[0];
1079         firmware_rev[5] = '\0';
1080 
1081         /* Use undocumented command to get model number and revision. */
1082 
1083         inquiry_cmd[0] = CMD_RET_MODEL_NO;
1084         inquiry_cmd[1] = 6;
1085         buslogic_out(base, inquiry_cmd, 2);
1086         if (buslogic_in(base, inquiry_result, inquiry_cmd[1]))
1087             break;
1088         WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
1089         INTR_RESET(base);
1090         memcpy(model, inquiry_result, 5);
1091         model[5] = '\0';
1092         model[6] = inquiry_result[5];
1093     } while (0);
1094 
1095     /* ??? End undocumented command use. */
1096 
1097     /* bus_type from getconfig doesn't differentiate between EISA/VESA.  We
1098        override using the model number here. */
1099     switch (*bus_type) {
1100       case 'E':
1101         switch (model[0]) {
1102           case '4':
1103             *bus_type = 'V';
1104             break;
1105           case '9':
1106             *bus_type = 'P';
1107             break;
1108           case '7':
1109             break;
1110           default:
1111             *bus_type = 'X';
1112             break;
1113         }
1114         break;
1115       default:
1116         break;
1117     }
1118 
1119     while (0) {
1120       fail:
1121 #if (BUSLOGIC_DEBUG & BD_DETECT)
1122         buslogic_printk("query board settings\n");
1123 #endif
1124         return TRUE;
1125     }
1126 
1127     return FALSE;
1128 }
1129 
1130 /* return non-zero on detection */
1131 int buslogic_detect(Scsi_Host_Template *tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1132 {
1133     unsigned char dma;
1134     unsigned char irq;
1135     unsigned int base;
1136     unsigned char id;
1137     char bus_type;
1138     unsigned short max_sg;
1139     unsigned char bios_translation;
1140     unsigned long flags;
1141     const unsigned char *bios;
1142     char *model;
1143     char *firmware_rev;
1144     struct Scsi_Host *shpnt;
1145     size_t indx;
1146     int unchecked_isa_dma;
1147     int count = 0;
1148 
1149 #if (BUSLOGIC_DEBUG & BD_DETECT)
1150     buslogic_printk("called\n");
1151 #endif
1152 
1153     tpnt->can_queue = BUSLOGIC_MAILBOXES;
1154     for (indx = 0; bases[indx] != 0; indx++)
1155         if (!check_region(bases[indx], 4)) {
1156             shpnt = scsi_register(tpnt, sizeof (struct hostdata));
1157 
1158             base = bases[indx];
1159 
1160             model = HOSTDATA(shpnt)->model;
1161             firmware_rev = HOSTDATA(shpnt)->firmware_rev;
1162             if (buslogic_query(base, &bios_translation, &irq, &dma, &id,
1163                                &bus_type, &max_sg, &bios, model, firmware_rev))
1164                 goto unregister;
1165 
1166 #if (BUSLOGIC_DEBUG & BD_DETECT)
1167             buslogic_stat(base);
1168 #endif
1169 
1170             /* Only type 'A' (AT/ISA) bus adapters use unchecked DMA. */
1171             unchecked_isa_dma = (bus_type == 'A');
1172 #ifndef CONFIG_NO_BUGGY_BUSLOGIC
1173             /* There is a hardware bug in the BT-445S prior to revision D.
1174                When the BIOS is enabled and you have more than 16MB of memory,
1175                the card mishandles memory transfers over 16MB which (if viewed
1176                as a 24-bit address) overlap with the BIOS address space.  For
1177                example if you have the BIOS located at physical address
1178                0xDC000 and a DMA transfer from the card to RAM starts at
1179                physical address 0x10DC000 then the transfer is messed up.  To
1180                be more precise every fourth byte of the transfer is messed up.
1181                (This analysis courtesy of Tomas Hurka, author of the NeXTSTEP
1182                BusLogic driver.) */
1183 
1184             if (bus_type == 'V'                             /* 445 */
1185                 && firmware_rev[0] <= '3'                   /* S */
1186                 && bios != NULL) {                          /* BIOS enabled */
1187 #if 1
1188                 /* Now that LNZ's forbidden_addr stuff is in the higher level
1189                    scsi code, we can use this instead. */
1190                 /* Avoid addresses which "mirror" the BIOS for DMA. */
1191                 shpnt->forbidden_addr = (unsigned long)bios;
1192                 shpnt->forbidden_size = 16 * 1024;
1193 #else
1194                 /* Use double-buffering. */
1195                 unchecked_isa_dma = TRUE;
1196 #endif
1197             }
1198 #endif
1199 
1200             CHECK_DMA_ADDR(unchecked_isa_dma, shpnt, goto unregister);
1201 
1202             if (setup_mailboxes(base, shpnt))
1203                 goto unregister;
1204 
1205             /* Set the Bus on/off-times as not to ruin floppy performance.
1206                CMD_BUSOFF_TIME is a noop for EISA boards (and possibly
1207                others???). */
1208             if (bus_type != 'E' && bus_type != 'P') {
1209                 /* The default ON/OFF times for BusLogic adapters is 7/4. */
1210                 static const unsigned char oncmd[] = { CMD_BUSON_TIME, 7 };
1211                 static const unsigned char offcmd[] = { CMD_BUSOFF_TIME, 5 };
1212 
1213                 INTR_RESET(base);
1214                 buslogic_out(base, oncmd, sizeof oncmd);
1215                 WAIT_UNTIL(INTERRUPT(base), CMDC);
1216                 INTR_RESET(base);
1217                 buslogic_out(base, offcmd, sizeof offcmd);
1218                 WAIT_UNTIL(INTERRUPT(base), CMDC);
1219                 while (0) {
1220                   fail:
1221                     buslogic_printk("setting bus on/off-time failed.\n");
1222                 }
1223                 INTR_RESET(base);
1224             }
1225 
1226             buslogic_printk("configuring %s HA at port 0x%03X, IRQ %u",
1227                             (bus_type == 'A' ? "ISA"
1228                              : (bus_type == 'E' ? "EISA"
1229                                 : (bus_type == 'M' ? "MCA"
1230                                    : (bus_type == 'P' ? "PCI"
1231                                       : (bus_type == 'V' ? "VESA"
1232                                          : (bus_type == 'X' ? "EISA/VESA/PCI"
1233                                             : "Unknown")))))),
1234                             base, irq);
1235             if (bios != NULL)
1236                 printk(", BIOS 0x%05X", (unsigned int)bios);
1237             if (dma != 0)
1238                 printk(", DMA %u", dma);
1239             printk(", ID %u\n", id);
1240             buslogic_printk("Model Number: %s",
1241                             (model[0] ? model : "Unknown"));
1242             if (model[0])
1243                 printk(" (revision %d)", model[6]);
1244             printk("\n");
1245             buslogic_printk("firmware revision: %s\n", firmware_rev);
1246 
1247 #if (BUSLOGIC_DEBUG & BD_DETECT)
1248             buslogic_stat(base);
1249 #endif
1250 
1251 #if (BUSLOGIC_DEBUG & BD_DETECT)
1252             buslogic_printk("enable interrupt channel %d.\n", irq);
1253 #endif
1254 
1255             save_flags(flags);
1256             cli();
1257             if (request_irq(irq, buslogic_interrupt, 0, "buslogic")) {
1258                 buslogic_printk("unable to allocate IRQ for "
1259                                 "BusLogic controller.\n");
1260                 restore_flags(flags);
1261                 goto unregister;
1262             }
1263 
1264             if (dma) {
1265                 if (request_dma(dma, "buslogic")) {
1266                     buslogic_printk("unable to allocate DMA channel for "
1267                                     "BusLogic controller.\n");
1268                     free_irq(irq);
1269                     restore_flags(flags);
1270                     goto unregister;
1271                 }
1272 
1273                 /* The DMA-Controller.  We need to fool with this because we
1274                    want to be able to use an ISA BusLogic without having to
1275                    have the BIOS enabled. */
1276                 set_dma_mode(dma, DMA_MODE_CASCADE);
1277                 enable_dma(dma);
1278             }
1279 
1280             host[irq - 9] = shpnt;
1281             shpnt->this_id = id;
1282             shpnt->unchecked_isa_dma = unchecked_isa_dma;
1283             /* Have to keep cmd_per_lun at 1 for ISA machines otherwise lots
1284                of memory gets sucked up for bounce buffers.  */
1285             shpnt->cmd_per_lun = (unchecked_isa_dma ? 1 : BUSLOGIC_CMDLUN);
1286             shpnt->sg_tablesize = max_sg;
1287             if (shpnt->sg_tablesize > BUSLOGIC_MAX_SG)
1288                 shpnt->sg_tablesize = BUSLOGIC_MAX_SG;
1289             /* ??? shpnt->base should really be "const unsigned char *"... */
1290             shpnt->base = (unsigned char *)bios;
1291             shpnt->io_port = base;
1292             shpnt->n_io_port = 4;       /* Number of bytes of I/O space used */
1293             shpnt->dma_channel = dma;
1294             shpnt->irq = irq;
1295             HOSTDATA(shpnt)->bios_translation = bios_translation;
1296             if (bios_translation == BIOS_TRANSLATION_BIG)
1297                 buslogic_printk("using extended bios translation.\n");
1298             HOSTDATA(shpnt)->last_mbi_used = 2 * BUSLOGIC_MAILBOXES - 1;
1299             HOSTDATA(shpnt)->last_mbo_used = BUSLOGIC_MAILBOXES - 1;
1300             memset(HOSTDATA(shpnt)->sc, 0, sizeof HOSTDATA(shpnt)->sc);
1301             restore_flags(flags);
1302 
1303 #if 0
1304             {
1305                 unsigned char buf[8];
1306                 unsigned char cmd[]
1307                     = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1308                 size_t i;
1309 
1310 #if (BUSLOGIC_DEBUG & BD_DETECT)
1311                 buslogic_printk("*** READ CAPACITY ***\n");
1312 #endif
1313                 for (i = 0; i < sizeof buf; i++)
1314                     buf[i] = 0x87;
1315                 for (i = 0; i < 2; i++)
1316                     if (!buslogic_command(i, cmd, buf, sizeof buf)) {
1317                         buslogic_printk("LU %u sector_size %d device_size %d\n",
1318                                         i, *(int *)(buf + 4), *(int *)buf);
1319                     }
1320 
1321 #if (BUSLOGIC_DEBUG & BD_DETECT)
1322                 buslogic_printk("*** NOW RUNNING MY OWN TEST ***\n");
1323 #endif
1324                 for (i = 0; i < 4; i++) {
1325                     static buffer[512];
1326 
1327                     cmd[0] = READ_10;
1328                     cmd[1] = 0;
1329                     xany2scsi(cmd + 2, i);
1330                     cmd[6] = 0;
1331                     cmd[7] = 0;
1332                     cmd[8] = 1;
1333                     cmd[9] = 0;
1334                     buslogic_command(0, cmd, buffer, sizeof buffer);
1335                 }
1336             }
1337 #endif
1338 
1339             request_region(bases[indx], 4,"buslogic");
1340             /* Register the IO ports that we use */
1341             count++;
1342             continue;
1343           unregister:
1344             scsi_unregister(shpnt);
1345         }
1346     return count;
1347 }
1348 
1349 static int restart(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1350 {
1351     unsigned int i;
1352     unsigned int count = 0;
1353 #if 0
1354     static const unsigned char buscmd[] = { CMD_START_SCSI };
1355 #endif
1356 
1357     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1358         if (HOSTDATA(shpnt)->sc[i]
1359             && !HOSTDATA(shpnt)->sc[i]->device->soft_reset) {
1360 #if 0
1361             HOSTDATA(shpnt)->mb[i].status
1362                 = MBX_ACTION_START;     /* Indicate ready to restart... */
1363 #endif
1364             count++;
1365         }
1366 
1367     buslogic_printk("potential to restart %d stalled commands...\n", count);
1368 #if 0
1369     /* start scsi command */
1370     if (count)
1371         buslogic_out(shpnt->host->io_port, buscmd, sizeof buscmd);
1372 #endif
1373     return 0;
1374 }
1375 
1376 /* ??? The abort command for the aha1542 does not leave the device in a clean
1377    state where it is available to be used again.  As it is not clear whether
1378    the same problem exists with BusLogic boards, we will enable this and see
1379    if it works. */
1380 int buslogic_abort(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1381 {
1382 #if 1
1383     static const unsigned char buscmd[] = { CMD_START_SCSI };
1384     struct mailbox *mb;
1385     size_t mbi, mbo;
1386     unsigned long flags;
1387     unsigned int i;
1388 
1389     buslogic_printk("%X %X\n",
1390                     inb(STATUS(scpnt->host->io_port)),
1391                     inb(INTERRUPT(scpnt->host->io_port)));
1392 
1393     save_flags(flags);
1394     cli();
1395     mb = HOSTDATA(scpnt->host)->mb;
1396     mbi = HOSTDATA(scpnt->host)->last_mbi_used + 1;
1397     if (mbi >= 2 * BUSLOGIC_MAILBOXES)
1398         mbi = BUSLOGIC_MAILBOXES;
1399 
1400     do {
1401         if (mb[mbi].status != MBX_NOT_IN_USE)
1402             break;
1403         mbi++;
1404         if (mbi >= 2 * BUSLOGIC_MAILBOXES)
1405             mbi = BUSLOGIC_MAILBOXES;
1406     } while (mbi != HOSTDATA(scpnt->host)->last_mbi_used);
1407     restore_flags(flags);
1408 
1409     if (mb[mbi].status != MBX_NOT_IN_USE) {
1410         buslogic_printk("lost interrupt discovered on irq %d"
1411                         " - attempting to recover...\n",
1412                         scpnt->host->irq);
1413         {
1414             int intval[3];
1415 
1416             intval[0] = scpnt->host->irq;
1417             buslogic_interrupt((int)&intval[2]);
1418             return SCSI_ABORT_SUCCESS;
1419         }
1420     }
1421 
1422     /* OK, no lost interrupt.  Try looking to see how many pending commands we
1423        think we have. */
1424     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1425         if (HOSTDATA(scpnt->host)->sc[i]) {
1426             if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
1427                 buslogic_printk("timed out command pending for %4.4X.\n",
1428                                 scpnt->request.dev);
1429                 if (HOSTDATA(scpnt->host)->mb[i].status != MBX_NOT_IN_USE) {
1430                     buslogic_printk("OGMB still full - restarting...\n");
1431                     buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
1432                 }
1433             } else
1434                 buslogic_printk("other pending command: %4.4X\n",
1435                                 scpnt->request.dev);
1436         }
1437 #endif
1438 
1439 #if (BUSLOGIC_DEBUG & BD_ABORT)
1440     buslogic_printk("called\n");
1441 #endif
1442 
1443 #if 1
1444     /* This section of code should be used carefully - some devices cannot
1445        abort a command, and this merely makes it worse. */
1446     save_flags(flags);
1447     cli();
1448     for (mbo = 0; mbo < BUSLOGIC_MAILBOXES; mbo++)
1449         if (scpnt == HOSTDATA(scpnt->host)->sc[mbo]) {
1450             mb[mbo].status = MBX_ACTION_ABORT;
1451             buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
1452             break;
1453         }
1454     restore_flags(flags);
1455 #endif
1456 
1457     return SCSI_ABORT_SNOOZE;
1458 }
1459 
1460 /* We do not implement a reset function here, but the upper level code assumes
1461    that it will get some kind of response for the command in scpnt.  We must
1462    oblige, or the command will hang the SCSI system.  For a first go, we assume
1463    that the BusLogic notifies us with all of the pending commands (it does
1464    implement soft reset, after all). */
1465 int buslogic_reset(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1466 {
1467     static const unsigned char buscmd[] = { CMD_START_SCSI };
1468     unsigned int i;
1469 
1470 #if (BUSLOGIC_DEBUG & BD_RESET)
1471     buslogic_printk("called\n");
1472 #endif
1473 #if 0
1474     /* This does a scsi reset for all devices on the bus. */
1475     outb(RSBUS, CONTROL(scpnt->host->io_port));
1476 #else
1477     /* This does a selective reset of just the one device. */
1478     /* First locate the ccb for this command. */
1479     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1480         if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
1481             HOSTDATA(scpnt->host)->ccbs[i].op = CCB_OP_BUS_RESET;
1482 
1483             /* Now tell the BusLogic to flush all pending commands for this
1484                target. */
1485             buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
1486 
1487             /* Here is the tricky part.  What to do next.  Do we get an
1488                interrupt for the commands that we aborted with the specified
1489                target, or do we generate this on our own?  Try it without first
1490                and see what happens. */
1491             buslogic_printk("sent BUS DEVICE RESET to target %d.\n",
1492                             scpnt->target);
1493 
1494             /* If the first does not work, then try the second.  I think the
1495                first option is more likely to be correct.  Free the command
1496                block for all commands running on this target... */
1497 #if 1
1498             for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1499                 if (HOSTDATA(scpnt->host)->sc[i]
1500                     && HOSTDATA(scpnt->host)->sc[i]->target == scpnt->target) {
1501                     Scsi_Cmnd *sctmp = HOSTDATA(scpnt->host)->sc[i];
1502 
1503                     sctmp->result = DID_RESET << 16;
1504                     if (sctmp->host_scribble)
1505                         scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
1506                     buslogic_printk("sending DID_RESET for target %d.\n",
1507                                     scpnt->target);
1508                     sctmp->scsi_done(scpnt);
1509 
1510                     HOSTDATA(scpnt->host)->sc[i] = NULL;
1511                     HOSTDATA(scpnt->host)->mb[i].status = MBX_NOT_IN_USE;
1512                 }
1513             return SCSI_RESET_SUCCESS;
1514 #else
1515             return SCSI_RESET_PENDING;
1516 #endif
1517         }
1518 #endif
1519     /* No active command at this time, so this means that each time we got some
1520        kind of response the last time through.  Tell the mid-level code to
1521        request sense information in order to decide what to do next. */
1522     return SCSI_RESET_PUNT;
1523 }
1524 
1525 /* ??? This is probably not correct for series "C" boards.  I believe these
1526    support separate mappings for each disk.  We would need to issue a
1527    CMD_READ_FW_LOCAL_RAM command to check for the particular drive being
1528    queried.  Note that series "C" boards can be differentiated by having
1529    HOSTDATA(disk->device->host)->firmware_rev[0] >= '4'. */
1530 int buslogic_biosparam(Disk *disk, int dev, int *ip)
     /* [previous][next][first][last][top][bottom][index][help] */
1531 {
1532     unsigned int size = disk->capacity;
1533 
1534     /* ip[0] == heads, ip[1] == sectors, ip[2] == cylinders */
1535     if (HOSTDATA(disk->device->host)->bios_translation == BIOS_TRANSLATION_BIG
1536         && size >= 0x200000) {          /* 1GB */
1537         if (size >= 0x400000) {         /* 2GB */
1538 #if 0   /* ??? Used in earlier kernels, but disagrees with BusLogic info. */
1539             if (mb >= 0x800000) {       /* 4GB */
1540                 ip[0] = 256;
1541                 ip[1] = 64;
1542             } else {
1543                 ip[0] = 256;
1544                 ip[1] = 32;
1545             }
1546 #else
1547             ip[0] = 256;
1548             ip[1] = 64;
1549 #endif
1550         } else {
1551             ip[0] = 128;
1552             ip[1] = 32;
1553         }
1554     } else {
1555         ip[0] = 64;
1556         ip[1] = 32;
1557     }
1558     ip[2] = size / (ip[0] * ip[1]);
1559 /*    if (ip[2] > 1024)
1560         ip[2] = 1024; */
1561     return 0;
1562 }
1563 
1564 /* called from init/main.c */
1565 void buslogic_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
1566 {
1567     static const unsigned short valid_bases[]
1568         = { 0x130, 0x134, 0x230, 0x234, 0x330, 0x334 };
1569     static size_t setup_idx = 0;
1570     size_t i;
1571 
1572     if (setup_idx >= ARRAY_SIZE(bases) - 1) {
1573         buslogic_printk("called too many times.  Bad LILO params?\n");
1574         return;
1575     }
1576     if (ints[0] != 1) {
1577         buslogic_printk("malformed command line.\n");
1578         buslogic_printk("usage: buslogic=<portbase>\n");
1579         return;
1580     }
1581     for (i = 0; i < ARRAY_SIZE(valid_bases); i++)
1582         if (valid_bases[i] == ints[1]) {
1583             bases[setup_idx++] = ints[1];
1584             bases[setup_idx] = 0;
1585             return;
1586         }
1587     buslogic_printk("invalid base 0x%X specified.\n", ints[i]);
1588 }
1589 
1590 #ifdef MODULE
1591 /* Eventually this will go into an include file, but that's later... */
1592 Scsi_Host_Template driver_template = BUSLOGIC;
1593 
1594 # include "scsi_module.c"
1595 #endif

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