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

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