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. buslogic_in_fast
  7. makecode
  8. test_port
  9. buslogic_info
  10. buslogic_interrupt
  11. buslogic_queuecommand
  12. internal_done
  13. buslogic_command
  14. setup_mailboxes
  15. getconfig
  16. get_translation
  17. buslogic_query
  18. buslogic_detect
  19. restart
  20. buslogic_abort
  21. buslogic_reset
  22. buslogic_biosparam

   1 /*
   2  *      buslogic.c      (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] which
   9  *      is Copyright (C) 1992 Tommy Thorn.  Much has been reworked, but most of
  10  *      basic structure and substantial chunks of code still remain.
  11  */
  12 
  13 /*
  14  * TODO:
  15  *      1. Cleanup error handling & reporting.
  16  *      2. Find out why scatter/gather is limited to 16 requests per command.
  17  *      3. Add multiple outstanding requests.
  18  *      4. See if we can make good use of having more than one command per lun.
  19  *      5. Test/improve/fix abort & reset functions.
  20  *      6. Look at command linking.
  21  *      7. Allow multiple boards to share an IRQ if the bus allows (e.g. EISA).
  22  */
  23 
  24 /*
  25  * NOTES:
  26  *    BusLogic (formerly BusTek) manufactures an extensive family of
  27  *    intelligent, high performance SCSI-2 host adapters.  They all support
  28  *    command queueing and scatter/gather I/O.  Most importantly, they all
  29  *    support identical programming interfaces, so a single driver can be used
  30  *    for all boards.
  31  *
  32  *    Actually, they all support TWO identical programming interfaces!  They
  33  *    have an Adaptec 154x compatible interface (complete with 24 bit
  34  *    addresses) as well as a "native" 32 bit interface.  As such, the Linux
  35  *    aha1542 driver can be used to drive them, but with less than optimal
  36  *    performance (at least for the EISA, VESA, and MCA boards).
  37  *
  38  *    Here is the scoop on the various models:
  39  *      BT-542B - ISA first-party DMA with floppy support.
  40  *      BT-545S - 542B + FAST SCSI and active termination.
  41  *      BT-545D - 545S + differential termination.
  42  *      BT-445S - VESA bus-master FAST SCSI with active termination and floppy
  43  *                support.
  44  *      BT-640A - MCA bus-master with floppy support.
  45  *      BT-646S - 640A + FAST SCSI and active termination.
  46  *      BT-646D - 646S + differential termination.
  47  *      BT-742A - EISA bus-master with floppy support.
  48  *      BT-747S - 742A + FAST SCSI, active termination, and 2.88M floppy.
  49  *      BT-747D - 747S + differential termination.
  50  *      BT-757S - 747S + WIDE SCSI.
  51  *      BT-757D - 747D + WIDE SCSI.
  52  *      BT-946C - PCI bus-master FAST SCSI. (??? Nothing else known.)
  53  *
  54  *    Should you require further information on any of these boards, BusLogic
  55  *    can be reached at (408)492-9090.
  56  *
  57  *    This driver SHOULD support all of these boards.  It has only been tested
  58  *    with a 747S and 445S.
  59  *
  60  *    Places flagged with a triple question-mark are things which are either
  61  *    unfinished, questionable, or wrong.
  62  */
  63 
  64 #include <linux/string.h>
  65 #include <linux/sched.h>
  66 #include <linux/kernel.h>
  67 #include <linux/head.h>
  68 #include <linux/types.h>
  69 #include <linux/ioport.h>
  70 #include <linux/delay.h>
  71 
  72 #include <asm/io.h>
  73 #include <asm/system.h>
  74 #include <asm/dma.h>
  75 
  76 #include "../block/blk.h"
  77 #include "scsi.h"
  78 #include "hosts.h"
  79 #include "sd.h"
  80 #define BUSLOGIC_PRIVATE_H      /* Get the "private" stuff */
  81 #include "buslogic.h"
  82 
  83 #ifndef BUSLOGIC_DEBUG
  84 # define BUSLOGIC_DEBUG UD_ABORT
  85 #endif
  86 
  87 #define BUSLOGIC_VERSION "1.00"
  88 
  89 /* Not a random value - if this is too large, the system hangs for a long time
  90    waiting for something to happen if a board is not installed. */
  91 #define WAITNEXTTIMEOUT 3000000
  92 
  93 /* This is for the scsi_malloc call in buslogic_queuecommand. */
  94 /* ??? I'd up this to 4096, but would we be in danger of using up the
  95    scsi_malloc memory pool? */
  96 /* This could be a concern, I guess.  It may be possible to fix things so that
  97    the table generated in sd.c is compatible with the low-level code, but
  98    don't hold your breath.  -ERY */
  99 #define BUSLOGIC_SG_MALLOC 512
 100 
 101 /* Since the SG list is malloced, we have to limit the length. */
 102 #define BUSLOGIC_MAX_SG (BUSLOGIC_SG_MALLOC / sizeof (struct chain))
 103 
 104 /* The DMA-Controller.  We need to fool with this because we want to be able to
 105    use an ISA BusLogic without having to have the BIOS enabled. */
 106 #define DMA_MODE_REG 0xD6
 107 #define DMA_MASK_REG 0xD4
 108 #define CASCADE 0xC0
 109 
 110 #define BUSLOGIC_MAILBOXES 16   /* ??? Arbitrary? */
 111 
 112 /* BusLogic boards can be configured for quite a number of port addresses (six
 113    to be exact), but I generally do not want the driver poking around at
 114    random.  We allow two port addresses - this allows people to use a BusLogic
 115    with a MIDI card, which frequently also uses 0x330.  If different port
 116    addresses are needed (e.g. to install more than two cards), you must define
 117    BUSLOGIC_PORT_OVERRIDE to be a list of the addresses which will be checked.
 118    This can also be used to resolve a conflict if the port-probing at a
 119    standard port causes problems with another board. */
 120 static const unsigned int bases[] = {
 121 #ifdef BUSLOGIC_PORT_OVERRIDE
 122     BUSLOGIC_PORT_OVERRIDE
 123 #else
 124     0x330, 0x334, /* 0x130, 0x134, 0x230, 0x234 */
 125 #endif
 126 };
 127 
 128 #define BIOS_TRANSLATION_DEFAULT 0      /* Default case */
 129 #define BIOS_TRANSLATION_BIG 1          /* Big disk (> 1G) case */
 130 
 131 struct hostdata {
 132     unsigned char bus_type;
 133     int bios_translation;       /* Mapping bios uses - for compatibility */
 134     size_t last_mbi_used;
 135     size_t last_mbo_used;
 136     Scsi_Cmnd *sc[BUSLOGIC_MAILBOXES];
 137     struct mailbox mb[2 * BUSLOGIC_MAILBOXES];
 138     struct ccb ccbs[BUSLOGIC_MAILBOXES];
 139 };
 140 
 141 #define HOSTDATA(host) ((struct hostdata *)&(host)->hostdata)
 142 
 143 /* One for each IRQ level (9-15), although 13 will never be used. */
 144 static struct Scsi_Host *host[7] = { NULL, };
 145 
 146 static int setup_mailboxes(unsigned int base, struct Scsi_Host *shpnt);
 147 static int restart(struct Scsi_Host *shpnt);
 148 
 149 #define INTR_RESET(base) outb(RINT, CONTROL(base))
 150 
 151 #define buslogic_printk buslogic_prefix(),printk
 152 
 153 #define CHECK(cond) if (cond) ; else goto fail
 154 
 155 #define WAIT(port, mask, allof, noneof) \
 156     CHECK(wait(port, mask, allof, noneof, WAITNEXTTIMEOUT, FALSE))
 157 #define WAIT_WHILE(port, mask) WAIT(port, mask, 0, mask)
 158 #define WAIT_UNTIL(port, mask) WAIT(port, mask, mask, 0)
 159 #define WAIT_FAST(port, mask, allof, noneof) \
 160     CHECK(wait(port, mask, allof, noneof, 100, TRUE))
 161 #define WAIT_WHILE_FAST(port, mask) WAIT_FAST(port, mask, 0, mask)
 162 #define WAIT_UNTIL_FAST(port, mask) WAIT_FAST(port, mask, mask, 0)
 163 
 164 /* If delay != 0, we use the udelay call to regulate the amount of time we
 165    wait. */
 166 static __inline__ int wait(unsigned short port, unsigned char mask,
     /* [previous][next][first][last][top][bottom][index][help] */
 167                            unsigned char allof, unsigned char noneof,
 168                            unsigned int timeout, int delay)
 169 {
 170     int bits;
 171 
 172     for (;;) {
 173         bits = inb(port) & mask;
 174         if ((bits & allof) == allof && (bits & noneof) == 0)
 175             return TRUE;
 176         if (delay)
 177             udelay(1000);
 178         if (--timeout == 0)
 179             return FALSE;
 180     }
 181 }
 182 
 183 static void buslogic_prefix(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 184 {
 185     printk("BusLogic SCSI: ");
 186 }
 187 
 188 #if BUSLOGIC_DEBUG
 189 static void buslogic_stat(unsigned int base)
     /* [previous][next][first][last][top][bottom][index][help] */
 190 {
 191     int s = inb(STATUS(base)), i = inb(INTERRUPT(base));
 192 
 193     printk("status=%02X intrflags=%02X\n", s, i);
 194 }
 195 #else
 196 # define buslogic_stat(base)
 197 #endif
 198 
 199 /* This is a bit complicated, but we need to make sure that an interrupt
 200    routine does not send something out while we are in the middle of this.
 201    Fortunately, it is only at boot time that multi-byte messages are ever
 202    sent. */
 203 static int buslogic_out(unsigned int base, const unsigned char *cmdp,
     /* [previous][next][first][last][top][bottom][index][help] */
 204                         size_t len)
 205 {
 206     if (len == 1) {
 207         for (;;) {
 208             WAIT_WHILE(STATUS(base), CPRBSY);
 209             cli();
 210             if (!(inb(STATUS(base)) & CPRBSY)) {
 211                 outb(*cmdp, COMMAND_PARAMETER(base));
 212                 sti();
 213                 return FALSE;
 214             }
 215             sti();
 216         }
 217     } else {
 218         cli();
 219         while (len--) {
 220             WAIT_WHILE(STATUS(base), CPRBSY);
 221             outb(*cmdp++, COMMAND_PARAMETER(base));
 222         }
 223         sti();
 224     }
 225     return FALSE;
 226   fail:
 227     sti();
 228     buslogic_printk("buslogic_out failed(%u): ", len + 1);
 229     buslogic_stat(base);
 230     return TRUE;
 231 }
 232 
 233 /* Only used at boot time, so we do not need to worry about latency as much
 234    here. */
 235 static int buslogic_in(unsigned int base, unsigned char *cmdp, size_t len)
     /* [previous][next][first][last][top][bottom][index][help] */
 236 {
 237     cli();
 238     while (len--) {
 239         WAIT_UNTIL(STATUS(base), DIRRDY);
 240         *cmdp++ = inb(DATA_IN(base));
 241     }
 242     sti();
 243     return FALSE;
 244   fail:
 245     sti();
 246     buslogic_printk("buslogic_in failed(%u): ", len + 1);
 247     buslogic_stat(base);
 248     return TRUE;
 249 }
 250 
 251 #if 0
 252 /* Similar to buslogic_in, except that we wait a very short period of time.
 253    We use this if we know the board is alive and awake, but we are not sure
 254    whether the board will respond the the command we are about to send. */
 255 static int buslogic_in_fast(unsigned int base, unsigned char *cmdp, size_t len)
     /* [previous][next][first][last][top][bottom][index][help] */
 256 {
 257     cli();
 258     while (len--) {
 259         WAIT_UNTIL_FAST(STATUS(base), DIRRDY);
 260         *cmdp++ = inb(DATA_IN(base));
 261     }
 262     sti();
 263     return FALSE;
 264   fail:
 265     sti();
 266     return TRUE;
 267 }
 268 #endif
 269 
 270 static unsigned int makecode(unsigned int hosterr, unsigned int scsierr)
     /* [previous][next][first][last][top][bottom][index][help] */
 271 {
 272     switch (hosterr) {
 273       case 0x00:        /* Normal completion. */
 274       case 0x0A:        /* Linked command complete without error and linked
 275                            normally. */
 276       case 0x0B:        /* Linked command complete without error, interrupt
 277                            generated. */
 278         hosterr = DID_OK;
 279         break;
 280 
 281       case 0x11:        /* Selection time out: the initiator selection or
 282                            target reselection was not complete within the SCSI
 283                            Time out period. */
 284         hosterr = DID_TIME_OUT;
 285         break;
 286 
 287       case 0x14:        /* Target bus phase sequence failure - An invalid bus
 288                            phase or bus phase sequence was requested by the
 289                            target.  The host adapter will generate a SCSI
 290                            Reset Condition, notifying the host with a RSTS
 291                            interrupt. */
 292         hosterr = DID_RESET;
 293         break;
 294 
 295       case 0x12:        /* Data overrun/underrun: the target attempted to
 296                            transfer more data than was allocated by the Data
 297                            Length field or the sum of the Scatter/Gather Data
 298                            Length fields. */
 299       case 0x13:        /* Unexpected bus free - The target dropped the SCSI
 300                            BSY at an unexpected time. */
 301       case 0x15:        /* MBO command was not 00, 01, or 02 - The first byte
 302                            of the MB was invalid.  This usually indicates a
 303                            software failure. */
 304       case 0x16:        /* Invalid CCB Operation Code - The first byte of the
 305                            CCB was invalid.  This usually indicates a software
 306                            failure. */
 307       case 0x17:        /* Linked CCB does not have the same LUN - A
 308                            subsequent CCB of a set of linked CCB's does not
 309                            specify the same logical unit number as the
 310                            first. */
 311       case 0x18:        /* Invalid Target Direction received from Host - The
 312                            direction of a Target Mode CCB was invalid. */
 313       case 0x19:        /* Duplicate CCB Received in Target Mode - More than
 314                            once CCB was received to service data transfer
 315                            between the same target LUN and initiator SCSI ID
 316                            in the same direction. */
 317       case 0x1A:        /* Invalid CCB or Segment List Parameter - A segment
 318                            list with a zero length segment or invalid segment
 319                            list boundaries was received.  A CCB parameter was
 320                            invalid. */
 321       case 0x1B:        /* Auto request sense failed. */
 322       case 0x1C:        /* SCSI-2 tagged queueing message was rejected by the
 323                            target. */
 324       case 0x20:        /* The host adapter hardware failed. */
 325       case 0x21:        /* The target did not respond to SCSI ATN and the host
 326                            adapter consequently issued a SCSI bus reset to
 327                            clear up the failure. */
 328       case 0x22:        /* The host adapter asserted a SCSI bus reset. */
 329       case 0x23:        /* Other SCSI devices asserted a SCSI bus reset. */
 330 #if BUSLOGIC_DEBUG
 331         buslogic_printk("%X %X\n", hosterr, scsierr);
 332 #endif
 333         hosterr = DID_ERROR;    /* ??? Couldn't find any better. */
 334         break;
 335 
 336       default:
 337         buslogic_printk("makecode: unknown hoststatus %X\n", hosterr);
 338         break;
 339     }
 340     return (hosterr << 16) | scsierr;
 341 }
 342 
 343 static int test_port(unsigned int base, struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 344 {
 345     unsigned int i;
 346     unsigned char inquiry_cmd[] = { CMD_INQUIRY };
 347     unsigned char inquiry_result[4];
 348     unsigned char *cmdp;
 349     int len;
 350     volatile int debug = 0;
 351 
 352     /* Quick and dirty test for presence of the card. */
 353     if (inb(STATUS(base)) == 0xFF)
 354         return TRUE;
 355 
 356     /* Reset the adapter.  I ought to make a hard reset, but it's not really
 357        necessary. */
 358 
 359 #if BUSLOGIC_DEBUG
 360     buslogic_printk("test_port called\n");
 361 #endif
 362 
 363     /* In case some other card was probing here, reset interrupts. */
 364     INTR_RESET(base);   /* reset interrupts, so they don't block */
 365 
 366     outb(RSOFT | RINT/* | RSBUS*/, CONTROL(base));
 367 
 368     /* Wait a little bit for things to settle down. */
 369     i = jiffies + 2;
 370     while (i > jiffies);
 371 
 372     debug = 1;
 373     /* Expect INREQ and HARDY, any of the others are bad. */
 374     WAIT(STATUS(base), STATMASK, INREQ | HARDY,
 375          DACT | DFAIL | CMDINV | DIRRDY | CPRBSY);
 376 
 377     debug = 2;
 378     /* Shouldn't have generated any interrupts during reset. */
 379     if (inb(INTERRUPT(base)) & INTRMASK)
 380         goto fail;
 381 
 382     /* Perform a host adapter inquiry instead so we do not need to set up the
 383        mailboxes ahead of time. */
 384     buslogic_out(base, inquiry_cmd, 1);
 385 
 386     debug = 3;
 387     len = 4;
 388     cmdp = &inquiry_result[0];
 389     while (len--) {
 390         WAIT(STATUS(base), DIRRDY, DIRRDY, 0);
 391         *cmdp++ = inb(DATA_IN(base));
 392     }
 393 
 394     debug = 4;
 395     /* Reading port should reset DIRRDY. */
 396     if (inb(STATUS(base)) & DIRRDY)
 397         goto fail;
 398 
 399     debug = 5;
 400     /* When CMDC, command is completed, and we're though testing. */
 401     WAIT_UNTIL(INTERRUPT(base), CMDC);
 402 
 403     /* now initialize adapter. */
 404 
 405     debug = 6;
 406     /* Clear interrupts. */
 407     outb(RINT, CONTROL(base));
 408 
 409     debug = 7;
 410 
 411     return FALSE;                               /* 0 = ok */
 412   fail:
 413     return TRUE;                                /* 1 = not ok */
 414 }
 415 
 416 const char *buslogic_info(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 417 {
 418     return "BusLogic SCSI Driver version " BUSLOGIC_VERSION;
 419 }
 420 
 421 /* A "high" level interrupt handler. */
 422 static void buslogic_interrupt(int junk)
     /* [previous][next][first][last][top][bottom][index][help] */
 423 {
 424     void (*my_done)(Scsi_Cmnd *) = NULL;
 425     int errstatus, mbistatus = MBX_NOT_IN_USE, number_serviced, found;
 426     size_t mbi, mbo = 0;
 427     struct Scsi_Host *shpnt;
 428     Scsi_Cmnd *sctmp;
 429     int irqno, base, flag;
 430     int needs_restart;
 431     struct mailbox *mb;
 432     struct ccb *ccb;
 433 
 434     /* Magic - this -2 is only required for slow interrupt handlers */
 435     irqno = ((int *)junk)[-2];
 436 
 437     shpnt = host[irqno - 9];
 438     if (!shpnt)
 439         panic("buslogic.c: NULL SCSI host entry");
 440 
 441     mb = HOSTDATA(shpnt)->mb;
 442     ccb = HOSTDATA(shpnt)->ccbs;
 443     base = shpnt->io_port;
 444 
 445 #if BUSLOGIC_DEBUG
 446     flag = inb(INTERRUPT(base));
 447 
 448     buslogic_printk("buslogic_interrupt: ");
 449     if (!(flag & INTV))
 450         printk("no interrupt? ");
 451     if (flag & IMBL)
 452         printk("IMBL ");
 453     if (flag & MBOR)
 454         printk("MBOR ");
 455     if (flag & CMDC)
 456         printk("CMDC ");
 457     if (flag & RSTS)
 458         printk("RSTS ");
 459     printk("status %02X\n", inb(STATUS(base)));
 460 #endif
 461 
 462     number_serviced = 0;
 463     needs_restart = 0;
 464 
 465     for (;;) {
 466         flag = inb(INTERRUPT(base));
 467 
 468         /* Check for unusual interrupts.  If any of these happen, we should
 469            probably do something special, but for now just printing a message
 470            is sufficient.  A SCSI reset detected is something that we really
 471            need to deal with in some way. */
 472         if (flag & ~IMBL) {
 473             if (flag & MBOR)
 474                 printk("MBOR ");
 475             if (flag & CMDC)
 476                 printk("CMDC ");
 477             if (flag & RSTS) {
 478                 needs_restart = 1;
 479                 printk("RSTS ");
 480             }
 481         }
 482 
 483         INTR_RESET(base);
 484 
 485         cli();
 486 
 487         mbi = HOSTDATA(shpnt)->last_mbi_used + 1;
 488         if (mbi >= 2 * BUSLOGIC_MAILBOXES)
 489             mbi = BUSLOGIC_MAILBOXES;
 490 
 491         /* I use the "found" variable as I like to keep cli/sti pairs at the
 492            same block level.  Debugging dropped sti's is no fun... */
 493 
 494         found = FALSE;
 495         do {
 496             if (mb[mbi].status != MBX_NOT_IN_USE) {
 497                 found = TRUE;
 498                 break;
 499             }
 500             mbi++;
 501             if (mbi >= 2 * BUSLOGIC_MAILBOXES)
 502                 mbi = BUSLOGIC_MAILBOXES;
 503         } while (mbi != HOSTDATA(shpnt)->last_mbi_used);
 504 
 505         if (found) {
 506             mbo = (struct ccb *)mb[mbi].ccbptr - ccb;
 507             mbistatus = mb[mbi].status;
 508             mb[mbi].status = MBX_NOT_IN_USE;
 509             HOSTDATA(shpnt)->last_mbi_used = mbi;
 510         }
 511 
 512         sti();
 513 
 514         if (!found) {
 515             /* Hmm, no mail.  Must have read it the last time around. */
 516             if (!number_serviced && !needs_restart)
 517                 buslogic_printk("interrupt received, but no mail.\n");
 518             /* We detected a reset.  Restart all pending commands for devices
 519                that use the hard reset option. */
 520             if (needs_restart)
 521                 restart(shpnt);
 522             return;
 523         }
 524 
 525 #if BUSLOGIC_DEBUG
 526         if (ccb[mbo].tarstat || ccb[mbo].hastat)
 527             buslogic_printk("buslogic_interrupt: returning %08X (status %d)\n",
 528                             ((int)ccb[mbo].hastat << 16) | ccb[mbo].tarstat,
 529                             mb[mbi].status);
 530 #endif
 531 
 532         if (mbistatus == MBX_COMPLETION_NOT_FOUND)
 533             continue;
 534 
 535 #if BUSLOGIC_DEBUG
 536         buslogic_printk("...done %u %u\n", mbo, mbi);
 537 #endif
 538 
 539         sctmp = HOSTDATA(shpnt)->sc[mbo];
 540 
 541         if (!sctmp || !sctmp->scsi_done) {
 542             buslogic_printk("buslogic_interrupt: Unexpected interrupt\n");
 543             buslogic_printk("tarstat=%02X, hastat=%02X id=%d lun=%d ccb#=%d\n",
 544                             ccb[mbo].tarstat, ccb[mbo].hastat,
 545                             ccb[mbo].id, ccb[mbo].lun, mbo);
 546             return;
 547         }
 548 
 549         my_done = sctmp->scsi_done;
 550         if (sctmp->host_scribble)
 551             scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
 552 
 553 #if 0   /* ??? */
 554         /* Fetch the sense data, and tuck it away, in the required slot.  The
 555            BusLogic automatically fetches it, and there is no guarantee that we
 556            will still have it in the cdb when we come back. */
 557         if (ccb[mbo].tarstat == 2)      /* ??? */
 558             memcpy(sctmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
 559                    sizeof sctmp->sense_buffer);
 560 #endif
 561 
 562         /* ??? more error checking left out here */
 563         if (mbistatus != MBX_COMPLETION_OK)
 564             /* ??? This is surely wrong, but I don't know what's right. */
 565             errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
 566         else
 567             errstatus = 0;
 568 
 569 #if BUSLOGIC_DEBUG
 570         if (errstatus)
 571             buslogic_printk("error: %08X %04X %04X\n",
 572                             errstatus, ccb[mbo].hastat, ccb[mbo].tarstat);
 573 
 574         if (status_byte(ccb[mbo].tarstat) == CHECK_CONDITION) {
 575             size_t i;
 576 
 577             buslogic_printk("buslogic_interrupt: sense:");
 578             for (i = 0; i < sizeof sctmp->sense_buffer; i++)
 579                 printk(" %02X", sctmp->sense_buffer[i]);
 580             printk("\n");
 581         }
 582 
 583         if (errstatus)
 584             buslogic_printk("buslogic_interrupt: returning %08X\n", errstatus);
 585 #endif
 586 
 587         sctmp->result = errstatus;
 588         HOSTDATA(shpnt)->sc[mbo] = NULL;        /* This effectively frees up
 589                                                    the mailbox slot, as far as
 590                                                    queuecommand is
 591                                                    concerned. */
 592         my_done(sctmp);
 593         number_serviced++;
 594     }
 595 }
 596 
 597 int buslogic_queuecommand(Scsi_Cmnd *scpnt, void (*done)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
 598 {
 599     static const unsigned char buscmd[] = { CMD_START_SCSI };
 600     unsigned char direction;
 601     unsigned char *cmd = (unsigned char *)scpnt->cmnd;
 602     unsigned char target = scpnt->target;
 603     unsigned char lun = scpnt->lun;
 604     void *buff = scpnt->request_buffer;
 605     int bufflen = scpnt->request_bufflen;
 606     int mbo;
 607     struct mailbox *mb;
 608     struct ccb *ccb;
 609 
 610 #if BUSLOGIC_DEBUG
 611     if (target > 1) {
 612         scpnt->result = DID_TIME_OUT << 16;
 613         done(scpnt);
 614         return 0;
 615     }
 616 #endif
 617 
 618     if (*cmd == REQUEST_SENSE) {
 619 #ifndef DEBUG
 620         if (bufflen != sizeof scpnt->sense_buffer) {
 621             buslogic_printk("Wrong buffer length supplied for request sense"
 622                             " (%d)\n",
 623                             bufflen);
 624         }
 625 #endif
 626         scpnt->result = 0;
 627         done(scpnt);
 628         return 0;
 629     }
 630 
 631 #if BUSLOGIC_DEBUG
 632     {
 633         int i;
 634 
 635         if (*cmd == READ_10 || *cmd == WRITE_10
 636             || *cmd == READ_6 || *cmd == WRITE_6)
 637             i = *(int *)(cmd + 2);
 638         else
 639             i = -1;
 640         buslogic_printk("buslogic_queuecommand:"
 641                         " dev %d cmd %02X pos %d len %d ",
 642                         target, *cmd, i, bufflen);
 643         buslogic_stat(scpnt->host->io_port);
 644         buslogic_printk("buslogic_queuecommand: dumping scsi cmd:");
 645         for (i = 0; i < (COMMAND_SIZE(*cmd)); 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(scpnt->host)->mb;
 654     ccb = HOSTDATA(scpnt->host)->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     cli();
 660 
 661     mbo = HOSTDATA(scpnt->host)->last_mbo_used + 1;
 662     if (mbo >= BUSLOGIC_MAILBOXES)
 663         mbo = 0;
 664 
 665     do {
 666         if (mb[mbo].status == MBX_NOT_IN_USE
 667             && HOSTDATA(scpnt->host)->sc[mbo] == NULL)
 668             break;
 669         mbo++;
 670         if (mbo >= BUSLOGIC_MAILBOXES)
 671             mbo = 0;
 672     } while (mbo != HOSTDATA(scpnt->host)->last_mbo_used);
 673 
 674     if (mb[mbo].status != MBX_NOT_IN_USE
 675         || HOSTDATA(scpnt->host)->sc[mbo]) {
 676         /* ??? Instead of panicing, should we enable OMBR interrupts and
 677            sleep until we get one? */
 678         panic("buslogic.c: unable to find empty mailbox");
 679     }
 680 
 681     HOSTDATA(scpnt->host)->sc[mbo] = scpnt;     /* This will effectively
 682                                                    prevent someone else from
 683                                                    screwing with this cdb. */
 684 
 685     HOSTDATA(scpnt->host)->last_mbo_used = mbo;
 686 
 687     sti();
 688 
 689 #if BUSLOGIC_DEBUG
 690     buslogic_printk("sending command (%d %08X)...", mbo, done);
 691 #endif
 692 
 693     /* This gets trashed for some reason */
 694     mb[mbo].ccbptr = &ccb[mbo];
 695 
 696     memset(&ccb[mbo], 0, sizeof (struct ccb));
 697 
 698     ccb[mbo].cdblen = COMMAND_SIZE(*cmd);       /* SCSI Command Descriptor
 699                                                    Block Length */
 700 
 701     direction = 0;
 702     if (*cmd == READ_10 || *cmd == READ_6)
 703         direction = 8;
 704     else if (*cmd == WRITE_10 || *cmd == WRITE_6)
 705         direction = 16;
 706 
 707     memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
 708 
 709     if (scpnt->use_sg) {
 710         struct scatterlist *sgpnt;
 711         struct chain *cptr;
 712         size_t i;
 713 
 714         ccb[mbo].op = CCB_OP_INIT_SG;   /* SCSI Initiator Command
 715                                            w/scatter-gather */
 716         scpnt->host_scribble
 717             = (unsigned char *)scsi_malloc(BUSLOGIC_SG_MALLOC);
 718         if (scpnt->host_scribble == NULL)
 719             panic("buslogic.c: unable to allocate DMA memory");
 720         sgpnt = (struct scatterlist *)scpnt->request_buffer;
 721         cptr = (struct chain *)scpnt->host_scribble;
 722         if (scpnt->use_sg > scpnt->host->sg_tablesize) {
 723             buslogic_printk("buslogic_queuecommand: bad segment list,"
 724                             " %d > %d\n",
 725                             scpnt->use_sg, scpnt->host->sg_tablesize);
 726             panic("buslogic.c: bad segment list");
 727         }
 728         for (i = 0; i < scpnt->use_sg; i++) {
 729             cptr[i].dataptr = sgpnt[i].address;
 730             cptr[i].datalen = sgpnt[i].length;
 731         }
 732         ccb[mbo].datalen = scpnt->use_sg * sizeof (struct chain);
 733         ccb[mbo].dataptr = cptr;
 734 #if BUSLOGIC_DEBUG
 735         {
 736             unsigned char *ptr;
 737 
 738             buslogic_printk("cptr %08X:", cptr);
 739             ptr = (unsigned char *)cptr;
 740             for (i = 0; i < 18; i++)
 741                 printk(" %02X", ptr[i]);
 742             printk("\n");
 743         }
 744 #endif
 745     } else {
 746         ccb[mbo].op = CCB_OP_INIT;      /* SCSI Initiator Command */
 747         scpnt->host_scribble = NULL;
 748         ccb[mbo].datalen = bufflen;
 749         ccb[mbo].dataptr = buff;
 750     }
 751     ccb[mbo].id = target;
 752     ccb[mbo].lun = lun;
 753     ccb[mbo].dir = direction;
 754     ccb[mbo].rsalen = sizeof scpnt->sense_buffer;
 755     ccb[mbo].senseptr = scpnt->sense_buffer;
 756     ccb[mbo].linkptr = NULL;
 757     ccb[mbo].commlinkid = 0;
 758 
 759 #if BUSLOGIC_DEBUG
 760     {
 761         size_t i;
 762 
 763         buslogic_printk("buslogic_queuecommand: sending...");
 764         for (i = 0; i < sizeof ccb[mbo] - 10; i++)
 765             printk(" %02X", ((unsigned char *)&ccb[mbo])[i]);
 766         printk("\n");
 767     }
 768 #endif
 769 
 770     if (done) {
 771 #if BUSLOGIC_DEBUG
 772         buslogic_printk("buslogic_queuecommand: now waiting for interrupt: ");
 773         buslogic_stat(scpnt->host->io_port);
 774 #endif
 775         scpnt->scsi_done = done;
 776         mb[mbo].status = MBX_ACTION_START;
 777         /* start scsi command */
 778         buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
 779 #if BUSLOGIC_DEBUG
 780         buslogic_stat(scpnt->host->io_port);
 781 #endif
 782     } else
 783         buslogic_printk("buslogic_queuecommand: done can't be NULL\n");
 784 
 785     return 0;
 786 }
 787 
 788 #if 0
 789 static void internal_done(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 790 {
 791     scpnt->SCp.Status++;
 792 }
 793 
 794 int buslogic_command(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 795 {
 796 #if BUSLOGIC_DEBUG
 797     buslogic_printk("buslogic_command: ..calling buslogic_queuecommand\n");
 798 #endif
 799 
 800     buslogic_queuecommand(scpnt, internal_done);
 801 
 802     scpnt->SCp.Status = 0;
 803     while (!scpnt->SCp.Status)
 804         continue;
 805     return scpnt->result;
 806 }
 807 #endif
 808 
 809 /* Initialize mailboxes. */
 810 static int setup_mailboxes(unsigned int base, struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 811 {
 812     size_t i;
 813     int ok = FALSE;             /* Innocent until proven guilty... */
 814     struct mailbox *mb = HOSTDATA(shpnt)->mb;
 815     struct ccb *ccb = HOSTDATA(shpnt)->ccbs;
 816     struct {
 817         unsigned char cmd, count;
 818         void *base PACKED;
 819     } cmd = { CMD_INITEXTMB, BUSLOGIC_MAILBOXES, mb };
 820 
 821     for (i = 0; i < BUSLOGIC_MAILBOXES; i++) {
 822         mb[i].status = mb[BUSLOGIC_MAILBOXES + i].status = MBX_NOT_IN_USE;
 823         mb[i].ccbptr = &ccb[i];
 824     }
 825     INTR_RESET(base);   /* reset interrupts, so they don't block */
 826 
 827     /* If this fails, this must be an Adaptec board */
 828     if (buslogic_out(base, (unsigned char *)&cmd, sizeof cmd))
 829         goto must_be_adaptec;
 830 
 831     /* Wait until host adapter is done messing around, and then check to see
 832        if the command was accepted.  If it failed, this must be an Adaptec
 833        board. */
 834     WAIT_UNTIL(STATUS(base), HARDY);
 835     if (inb(STATUS(base)) & CMDINV)
 836         goto must_be_adaptec;
 837 
 838     WAIT_UNTIL(INTERRUPT(base), CMDC);
 839     while (0) {
 840       fail:
 841         buslogic_printk("buslogic_detect: failed setting up mailboxes\n");
 842     }
 843     ok = TRUE;
 844   must_be_adaptec:
 845     INTR_RESET(base);
 846     if (!ok)
 847         printk("- must be Adaptec\n");  /* So that the adaptec detect looks
 848                                            clean */
 849     return ok;
 850 }
 851 
 852 static int getconfig(unsigned int base, unsigned char *irq,
     /* [previous][next][first][last][top][bottom][index][help] */
 853                      unsigned char *dma, unsigned char *id,
 854                      unsigned char *bus_type, unsigned short *max_sg)
 855 {
 856     unsigned char inquiry_cmd[2];
 857     unsigned char inquiry_result[4];
 858     int i;
 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     buslogic_in(base, inquiry_result, 3);
 866     WAIT_UNTIL(INTERRUPT(base), CMDC);
 867     INTR_RESET(base);
 868     /* Defer using the DMA value until we know the bus type. */
 869     *dma = inquiry_result[0];
 870     switch (inquiry_result[1]) {
 871       case 0x01:
 872         *irq = 9;
 873         break;
 874       case 0x02:
 875         *irq = 10;
 876         break;
 877       case 0x04:
 878         *irq = 11;
 879         break;
 880       case 0x08:
 881         *irq = 12;
 882         break;
 883       case 0x20:
 884         *irq = 14;
 885         break;
 886       case 0x40:
 887         *irq = 15;
 888         break;
 889       default:
 890         buslogic_printk("Unable to determine BusLogic IRQ level."
 891                         "  Disabling board.\n");
 892         return TRUE;
 893     }
 894     *id = inquiry_result[2] & 0x7;
 895 
 896     inquiry_cmd[0] = CMD_INQEXTSETUP;
 897     inquiry_cmd[1] = 4;
 898     if (buslogic_out(base, inquiry_cmd, 2)
 899         || buslogic_in(base, inquiry_result, 4))
 900         return TRUE;
 901     WAIT_UNTIL(INTERRUPT(base), CMDC);
 902     INTR_RESET(base);
 903 
 904 #ifdef BUSLOGIC_BUS_TYPE_OVERRIDE
 905     *bus_type = BUS_TYPE_OVERRIDE;
 906 #else
 907     *bus_type = inquiry_result[0];
 908 #endif
 909     CHECK(*bus_type == 'A' || *bus_type == 'E' || *bus_type == 'M');
 910 #ifdef BUSLOGIC_BUS_TYPE_OVERRIDE
 911     if (inquiry_result[0] != BUS_TYPE_OVERRIDE)
 912         buslogic_printk("Overriding bus type %c with %c\n",
 913                         inquiry_result[0], BUS_TYPE_OVERRIDE);
 914 #endif
 915     *max_sg = (inquiry_result[3] << 8) | inquiry_result[2];
 916 
 917     /* We only need a DMA channel for ISA boards.  Some other types of boards
 918        (such as the 747S) have an option to report a DMA channel even though
 919        none is used (for compatibility with Adaptec drivers which require a
 920        DMA channel).  We ignore this. */
 921     if (*bus_type == 'A')
 922         switch (*dma) {
 923           case 0:       /* This indicates that no DMA channel is used. */
 924             *dma = 0;
 925             break;
 926           case 0x20:
 927             *dma = 5;
 928             break;
 929           case 0x40:
 930             *dma = 6;
 931             break;
 932           case 0x80:
 933             *dma = 7;
 934             break;
 935           default:
 936             buslogic_printk("Unable to determine BusLogic DMA channel."
 937                             "  Disabling board.\n");
 938             return TRUE;
 939         }
 940     else
 941         *dma = 0;
 942 
 943     while (0) {
 944       fail:
 945         buslogic_printk("buslogic_detect: query board settings\n");
 946         return TRUE;
 947     }
 948 
 949     return FALSE;
 950 }
 951 
 952 static int get_translation(unsigned int base)
     /* [previous][next][first][last][top][bottom][index][help] */
 953 {
 954     /* ??? Unlike UltraStor, I see no way of determining whether > 1G mapping
 955        has been enabled.  However, it appears that BusLogic uses a mapping
 956        scheme which varies with the disk size when > 1G mapping is enabled.
 957        For disks <= 1G, this mapping is the same regardless of the setting of
 958        > 1G mapping.  Therefore, we should be safe in always assuming that > 1G
 959        mapping has been enabled. */
 960     return BIOS_TRANSLATION_BIG;
 961 }
 962 
 963 /* Query the board to find out the model. */
 964 static int buslogic_query(unsigned int base, int *trans)
     /* [previous][next][first][last][top][bottom][index][help] */
 965 {
 966     static const unsigned char inquiry_cmd[] = { CMD_INQUIRY };
 967     unsigned char inquiry_result[4];
 968     int i;
 969 
 970     i = inb(STATUS(base));
 971     if (i & DIRRDY)
 972         i = inb(DATA_IN(base));
 973     buslogic_out(base, inquiry_cmd, sizeof inquiry_cmd);
 974     buslogic_in(base, inquiry_result, 4);
 975     WAIT_UNTIL(INTERRUPT(base), CMDC);
 976     INTR_RESET(base);
 977 
 978 #if 1   /* ??? Temporary */
 979     buslogic_printk("Inquiry Bytes: %02X %02X %02X %02X\n",
 980                     inquiry_result[0], inquiry_result[1],
 981                     inquiry_result[2], inquiry_result[3]);
 982 #endif
 983 
 984     while (0) {
 985       fail:
 986         buslogic_printk("buslogic_query: query board settings\n");
 987         return TRUE;
 988     }
 989 
 990     *trans = get_translation(base);
 991 
 992     return FALSE;
 993 }
 994 
 995 /* return non-zero on detection */
 996 int buslogic_detect(Scsi_Host_Template *tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 997 {
 998     unsigned char dma;
 999     unsigned char irq;
1000     unsigned int base = 0;
1001     unsigned char id;
1002     unsigned char bus_type;
1003     unsigned short max_sg;
1004     int trans;
1005     struct Scsi_Host *shpnt = NULL;
1006     int count = 0;
1007     int indx;
1008 
1009 #if BUSLOGIC_DEBUG
1010     buslogic_printk("buslogic_detect:\n");
1011 #endif
1012 
1013     for (indx = 0; indx < ARRAY_SIZE(bases); indx++)
1014         if (!check_region(bases[indx], 3)) {
1015             shpnt = scsi_register(tpnt, sizeof (struct hostdata));
1016 
1017             base = bases[indx];
1018 
1019             if (test_port(base, shpnt))
1020                 goto unregister;
1021 
1022             /* Set the Bus on/off-times as not to ruin floppy performance. */
1023             {
1024                 /* The default ON/OFF times for BusLogic adapters is 7/4. */
1025                 static const unsigned char oncmd[] = { CMD_BUSON_TIME, 7 };
1026                 static const unsigned char offcmd[] = { CMD_BUSOFF_TIME, 5 };
1027 
1028                 INTR_RESET(base);
1029                 buslogic_out(base, oncmd, sizeof oncmd);
1030                 WAIT_UNTIL(INTERRUPT(base), CMDC);
1031                 /* CMD_BUSOFF_TIME is a noop for EISA boards, but as there is
1032                    no way to to differentiate EISA from VESA we send it
1033                    unconditionally. */
1034                 INTR_RESET(base);
1035                 buslogic_out(base, offcmd, sizeof offcmd);
1036                 WAIT_UNTIL(INTERRUPT(base), CMDC);
1037                 while (0) {
1038                   fail:
1039                     buslogic_printk("buslogic_detect:"
1040                                     " setting bus on/off-time failed\n");
1041                 }
1042                 INTR_RESET(base);
1043             }
1044 
1045             if (buslogic_query(base, &trans))
1046                 goto unregister;
1047 
1048             if (getconfig(base, &irq, &dma, &id, &bus_type, &max_sg))
1049                 goto unregister;
1050 
1051 #if BUSLOGIC_DEBUG
1052             buslogic_stat(base);
1053 #endif
1054             /* Here is where we tell the men from the boys (i.e. an Adaptec
1055                will fail in setup_mailboxes, the men will not :-) */
1056             if (!setup_mailboxes(base, shpnt))
1057                 goto unregister;
1058 
1059             printk("Configuring BusLogic %s HA at port 0x%03X, IRQ %u",
1060                    (bus_type == 'A' ? "ISA"
1061                     : (bus_type == 'E' ? "EISA/VESA" : "MCA")),
1062                    base, irq);
1063             if (dma != 0)
1064                 printk(", DMA %u", dma);
1065             printk(", ID %u\n", id);
1066 
1067 #if BUSLOGIC_DEBUG
1068             buslogic_stat(base);
1069 #endif
1070 
1071 #if BUSLOGIC_DEBUG
1072             buslogic_printk("buslogic_detect: enable interrupt channel %d\n",
1073                             irq);
1074 #endif
1075 
1076             cli();
1077             if (request_irq(irq, buslogic_interrupt, 0, "buslogic")) {
1078                 buslogic_printk("Unable to allocate IRQ for "
1079                                 "BusLogic controller.\n");
1080                 sti();
1081                 goto unregister;
1082             }
1083 
1084             if (dma) {
1085                 if (request_dma(dma,"buslogic")) {
1086                     buslogic_printk("Unable to allocate DMA channel for "
1087                                     "BusLogic controller.\n");
1088                     free_irq(irq);
1089                     sti();
1090                     goto unregister;
1091                 }
1092 
1093                 if (dma >= 5) {
1094                     outb((dma - 4) | CASCADE, DMA_MODE_REG);
1095                     outb(dma - 4, DMA_MASK_REG);
1096                 }
1097             }
1098 
1099             host[irq - 9] = shpnt;
1100             shpnt->this_id = id;
1101 #ifdef CONFIG_NO_BUGGY_BUSLOGIC
1102             /* Only type 'A' (AT/ISA) bus adapters use unchecked DMA. */
1103             shpnt->unchecked_isa_dma = (bus_type == 'A');
1104 #else
1105             /* Bugs in the firmware of the 445S with >16M.  This does not seem
1106                to affect Revision E boards with firmware 3.37. */
1107             shpnt->unchecked_isa_dma = 1;
1108 #endif
1109             shpnt->sg_tablesize = max_sg;
1110             if (shpnt->sg_tablesize > BUSLOGIC_MAX_SG)
1111                 shpnt->sg_tablesize = BUSLOGIC_MAX_SG;
1112             /* ??? If we can dynamically allocate the mailbox arrays, I'll
1113                probably bump up this number. */
1114             shpnt->hostt->can_queue = BUSLOGIC_MAILBOXES;
1115             /* No known way to determine BIOS base address, but we don't
1116                care since we don't use it anyway. */
1117             shpnt->base = NULL;
1118             shpnt->io_port = base;
1119             shpnt->dma_channel = dma;
1120             shpnt->irq = irq;
1121             HOSTDATA(shpnt)->bios_translation = trans;
1122             if (trans == BIOS_TRANSLATION_BIG)
1123                 buslogic_printk("Using extended bios translation.\n");
1124             HOSTDATA(shpnt)->last_mbi_used = 2 * BUSLOGIC_MAILBOXES - 1;
1125             HOSTDATA(shpnt)->last_mbo_used = BUSLOGIC_MAILBOXES - 1;
1126             memset(HOSTDATA(shpnt)->sc, 0, sizeof HOSTDATA(shpnt)->sc);
1127             sti();
1128 
1129 #if 0
1130             {
1131                 unsigned char buf[8];
1132                 unsigned char cmd[]
1133                     = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1134                 size_t i;
1135 
1136 #if BUSLOGIC_DEBUG
1137                 buslogic_printk("*** READ CAPACITY ***\n");
1138 #endif
1139                 for (i = 0; i < sizeof buf; i++)
1140                     buf[i] = 0x87;
1141                 for (i = 0; i < 2; i++)
1142                     if (!buslogic_command(i, cmd, buf, sizeof buf)) {
1143                         buslogic_printk("buslogic_detect: LU %u "
1144                                         "sector_size %d device_size %d\n",
1145                                         i, *(int *)(buf + 4), *(int *)buf);
1146                     }
1147 
1148 #if BUSLOGIC_DEBUG
1149                 buslogic_printk("*** NOW RUNNING MY OWN TEST ***\n");
1150 #endif
1151                 for (i = 0; i < 4; i++) {
1152                     static buffer[512];
1153 
1154                     cmd[0] = READ_10;
1155                     cmd[1] = 0;
1156                     xany2scsi(cmd + 2, i);
1157                     cmd[6] = 0;
1158                     cmd[7] = 0;
1159                     cmd[8] = 1;
1160                     cmd[9] = 0;
1161                     buslogic_command(0, cmd, buffer, sizeof buffer);
1162                 }
1163             }
1164 #endif
1165 
1166             snarf_region(bases[indx], 3);       /* Register the IO ports that
1167                                                    we use */
1168             count++;
1169             continue;
1170           unregister:
1171             scsi_unregister(shpnt);
1172         }
1173     return count;
1174 }
1175 
1176 static int restart(struct Scsi_Host *shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1177 {
1178     unsigned int i;
1179     unsigned int count = 0;
1180 #if 0
1181     static const unsigned char buscmd[] = { CMD_START_SCSI };
1182 #endif
1183 
1184     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1185         if (HOSTDATA(shpnt)->sc[i]
1186             && !HOSTDATA(shpnt)->sc[i]->device->soft_reset) {
1187 #if 0
1188             HOSTDATA(shpnt)->mb[i].status
1189                 = MBX_ACTION_START;     /* Indicate ready to restart... */
1190 #endif
1191             count++;
1192         }
1193 
1194     buslogic_printk("Potential to restart %d stalled commands...\n", count);
1195 #if 0
1196     /* start scsi command */
1197     if (count)
1198         buslogic_out(shpnt->host->io_port, buscmd, sizeof buscmd);
1199 #endif
1200     return 0;
1201 }
1202 
1203 /* ??? The abort command for the aha1542 does not leave the device in a clean
1204    state where it is available to be used again.  As it is not clear whether
1205    the same problem exists with BusLogic boards, we will enable this and see
1206    if it works. */
1207 int buslogic_abort(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1208 {
1209 #if 1
1210     static const unsigned char buscmd[] = { CMD_START_SCSI };
1211     struct mailbox *mb;
1212     size_t mbi, mbo;
1213     unsigned int i;
1214 
1215     buslogic_printk("buslogic_abort: %X %X\n",
1216                     inb(STATUS(scpnt->host->io_port)),
1217                     inb(INTERRUPT(scpnt->host->io_port)));
1218 
1219     cli();
1220     mb = HOSTDATA(scpnt->host)->mb;
1221     mbi = HOSTDATA(scpnt->host)->last_mbi_used + 1;
1222     if (mbi >= 2 * BUSLOGIC_MAILBOXES)
1223         mbi = BUSLOGIC_MAILBOXES;
1224 
1225     do {
1226         if (mb[mbi].status != MBX_NOT_IN_USE)
1227             break;
1228         mbi++;
1229         if (mbi >= 2 * BUSLOGIC_MAILBOXES)
1230             mbi = BUSLOGIC_MAILBOXES;
1231     } while (mbi != HOSTDATA(scpnt->host)->last_mbi_used);
1232     sti();
1233 
1234     if (mb[mbi].status != MBX_NOT_IN_USE) {
1235         buslogic_printk("Lost interrupt discovered on irq %d"
1236                         " - attempting to recover\n",
1237                         scpnt->host->irq);
1238         {
1239             int intval[3];
1240 
1241             intval[0] = scpnt->host->irq;
1242             buslogic_interrupt((int)&intval[2]);
1243             return SCSI_ABORT_SUCCESS;
1244         }
1245     }
1246 
1247     /* OK, no lost interrupt.  Try looking to see how many pending commands we
1248        think we have. */
1249     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1250         if (HOSTDATA(scpnt->host)->sc[i]) {
1251             if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
1252                 buslogic_printk("Timed out command pending for %4.4X\n",
1253                                 scpnt->request.dev);
1254                 if (HOSTDATA(scpnt->host)->mb[i].status != MBX_NOT_IN_USE) {
1255                     buslogic_printk("OGMB still full - restarting\n");
1256                     buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
1257                 }
1258             } else
1259                 buslogic_printk("Other pending command %4.4X\n",
1260                                 scpnt->request.dev);
1261         }
1262 #endif
1263 
1264 #if (BUSLOGIC_DEBUG & BD_ABORT)
1265     buslogic_printk("buslogic_abort\n");
1266 #endif
1267 
1268 #if 1
1269     /* This section of code should be used carefully - some devices cannot
1270        abort a command, and this merely makes it worse. */
1271     cli();
1272     for (mbo = 0; mbo < BUSLOGIC_MAILBOXES; mbo++)
1273         if (scpnt == HOSTDATA(scpnt->host)->sc[mbo]) {
1274             mb[mbo].status = MBX_ACTION_ABORT;
1275             buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
1276             break;
1277         }
1278     sti();
1279 #endif
1280 
1281     return SCSI_ABORT_SNOOZE;
1282 }
1283 
1284 /* We do not implement a reset function here, but the upper level code assumes
1285    that it will get some kind of response for the command in scpnt.  We must
1286    oblige, or the command will hang the SCSI system.  For a first go, we assume
1287    that the BusLogic notifies us with all of the pending commands (it does
1288    implement soft reset, after all). */
1289 int buslogic_reset(Scsi_Cmnd *scpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1290 {
1291     static const unsigned char buscmd[] = { CMD_START_SCSI };
1292     unsigned int i;
1293 
1294 #if BUSLOGIC_DEBUG
1295     buslogic_printk("buslogic_reset\n");
1296 #endif
1297 #if 0
1298     /* This does a scsi reset for all devices on the bus. */
1299     outb(RSBUS, CONTROL(scpnt->host->io_port));
1300 #else
1301     /* This does a selective reset of just the one device. */
1302     /* First locate the ccb for this command. */
1303     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1304         if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
1305             HOSTDATA(scpnt->host)->ccbs[i].op = 0x81;   /* ??? BUS DEVICE
1306                                                            RESET */
1307 
1308             /* Now tell the BusLogic to flush all pending commands for this
1309                target. */
1310             buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
1311 
1312             /* Here is the tricky part.  What to do next.  Do we get an
1313                interrupt for the commands that we aborted with the specified
1314                target, or do we generate this on our own?  Try it without first
1315                and see what happens. */
1316             buslogic_printk("Sent BUS DEVICE RESET to target %d\n",
1317                             scpnt->target);
1318 
1319             /* If the first does not work, then try the second.  I think the
1320                first option is more likely to be correct.  Free the command
1321                block for all commands running on this target... */
1322 #if 1
1323             for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
1324                 if (HOSTDATA(scpnt->host)->sc[i]
1325                     && HOSTDATA(scpnt->host)->sc[i]->target == scpnt->target) {
1326                     Scsi_Cmnd *sctmp = HOSTDATA(scpnt->host)->sc[i];
1327 
1328                     sctmp->result = DID_RESET << 16;
1329                     if (sctmp->host_scribble)
1330                         scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
1331                     printk("Sending DID_RESET for target %d\n", scpnt->target);
1332                     sctmp->scsi_done(scpnt);
1333 
1334                     HOSTDATA(scpnt->host)->sc[i] = NULL;
1335                     HOSTDATA(scpnt->host)->mb[i].status = MBX_NOT_IN_USE;
1336                 }
1337             return SCSI_RESET_SUCCESS;
1338 #else
1339             return SCSI_RESET_PENDING;
1340 #endif
1341         }
1342 #endif
1343     /* No active command at this time, so this means that each time we got some
1344        kind of response the last time through.  Tell the mid-level code to
1345        request sense information in order to decide what to do next. */
1346     return SCSI_RESET_PUNT;
1347 }
1348 
1349 int buslogic_biosparam(Disk *disk, int dev, int *ip)
     /* [previous][next][first][last][top][bottom][index][help] */
1350 {
1351     /* ??? This truncates.  Should we round up to next MB? */
1352     unsigned int mb = disk->capacity >> 11;
1353 
1354     /* ip[0] == heads, ip[1] == sectors, ip[2] == cylinders */
1355     if (HOSTDATA(disk->device->host)->bios_translation == BIOS_TRANSLATION_BIG
1356         && mb > 1024) {
1357         if (mb > 4096) {
1358             ip[0] = 256;
1359             ip[1] = 64;
1360             ip[2] = mb >> 3;
1361 /*          if (ip[2] > 1024)
1362                 ip[2] = 1024; */
1363         } else if (mb > 2048) {
1364             ip[0] = 256;
1365             ip[1] = 32;
1366             ip[2] = mb >> 2;
1367         } else {
1368             ip[0] = 128;
1369             ip[1] = 32;
1370             ip[2] = mb >> 1;
1371         }
1372     } else {
1373         ip[0] = 64;
1374         ip[1] = 32;
1375         ip[2] = mb;
1376 /*      if (ip[2] > 1024)
1377             ip[2] = 1024; */
1378     }
1379     return 0;
1380 }

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