root/drivers/scsi/aha1542.c

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

DEFINITIONS

This source file includes following definitions.
  1. aha1542_stat
  2. aha1542_out
  3. aha1542_in
  4. makecode
  5. aha1542_test_port
  6. aha1542_info
  7. aha1542_intr_handle
  8. aha1542_queuecommand
  9. internal_done
  10. aha1542_command
  11. setup_mailboxes
  12. aha1542_getconfig
  13. aha1542_mbenable
  14. aha1542_query
  15. aha1542_detect
  16. aha1542_restart
  17. aha1542_abort
  18. aha1542_reset
  19. aha1542_biosparam

   1 /* $Id: aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $
   2  *  linux/kernel/aha1542.c
   3  *
   4  *  Copyright (C) 1992  Tommy Thorn
   5  *  Copyright (C) 1993, 1994 Eric Youngdale
   6  *
   7  *  Modified by Eric Youngdale
   8  *        Use request_irq and request_dma to help prevent unexpected conflicts
   9  *        Set up on-board DMA controller, such that we do not have to
  10  *        have the bios enabled to use the aha1542.
  11  *  Modified by David Gentzel
  12  *        Don't call request_dma if dma mask is 0 (for BusLogic BT-445S VL-Bus controller).
  13  */
  14 
  15 #include <linux/kernel.h>
  16 #include <linux/head.h>
  17 #include <linux/types.h>
  18 #include <linux/string.h>
  19 #include <linux/ioport.h>
  20 #include <linux/config.h>
  21 
  22 #include <linux/sched.h>
  23 #include <asm/dma.h>
  24 
  25 #include <asm/system.h>
  26 #include <asm/io.h>
  27 #include "../block/blk.h"
  28 #include "scsi.h"
  29 #include "hosts.h"
  30 
  31 #include "aha1542.h"
  32 
  33 #ifdef DEBUG
  34 #define DEB(x) x
  35 #else
  36 #define DEB(x)
  37 #endif
  38 /*
  39 static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $";
  40 */
  41 
  42 /* The adaptec can be configured for quite a number of addresses, but
  43 I generally do not want the card poking around at random.  We allow
  44 two addresses - this allows people to use the Adaptec with a Midi
  45 card, which also used 0x330 */
  46 
  47 static unsigned int bases[]={0x330, 0x334};
  48 
  49 /* The DMA-Controller.  We need to fool with this because we want to 
  50    be able to use the aha1542 without having to have the bios enabled */
  51 #define DMA_MODE_REG    0xd6
  52 #define DMA_MASK_REG    0xd4
  53 #define CASCADE         0xc0
  54 
  55 #define BIOS_TRANSLATION_1632 0  /* Used by some old 1542A boards */
  56 #define BIOS_TRANSLATION_6432 1 /* Default case these days */
  57 #define BIOS_TRANSLATION_25563 2 /* Big disk case */
  58 
  59 struct aha1542_hostdata{
  60         /* This will effectively start both of them at the first mailbox */
  61         int bios_translation;   /* Mapping bios uses - for compatibility */
  62         int aha1542_last_mbi_used;
  63         int aha1542_last_mbo_used;
  64         Scsi_Cmnd * SCint[AHA1542_MAILBOXES];
  65         struct mailbox mb[2*AHA1542_MAILBOXES];
  66         struct ccb ccb[AHA1542_MAILBOXES];
  67 };
  68 
  69 #define HOSTDATA(host) ((struct aha1542_hostdata *) &host->hostdata)
  70 
  71 static struct Scsi_Host * aha_host[7] = {NULL,};  /* One for each IRQ level (9-15) */
  72 
  73 
  74 
  75 
  76 #define WAITnexttimeout 3000000
  77 
  78 static void setup_mailboxes(int base_io, struct Scsi_Host * shpnt);
  79 static int aha1542_restart(struct Scsi_Host * shost);
  80 
  81 #define aha1542_intr_reset(base)  outb(IRST, CONTROL(base))
  82 
  83 #define WAIT(port, mask, allof, noneof)                                 \
  84  { register WAITbits;                                                   \
  85    register WAITtimeout = WAITnexttimeout;                              \
  86    while (1) {                                                          \
  87      WAITbits = inb(port) & (mask);                                     \
  88      if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
  89        break;                                                           \
  90      if (--WAITtimeout == 0) goto fail;                                 \
  91    }                                                                    \
  92  }
  93 
  94 static void aha1542_stat(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96 /*    int s = inb(STATUS), i = inb(INTRFLAGS);
  97   printk("status=%x intrflags=%x\n", s, i, WAITnexttimeout-WAITtimeout); */
  98 }
  99 
 100 /* This is a bit complicated, but we need to make sure that an interrupt
 101    routine does not send something out while we are in the middle of this.
 102    Fortunately, it is only at boot time that multi-byte messages
 103    are ever sent. */
 104 static int aha1542_out(unsigned int base, unchar *cmdp, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106   if(len == 1) {
 107     while(1==1){
 108         WAIT(STATUS(base), CDF, 0, CDF);
 109         cli();
 110         if(inb(STATUS(base)) & CDF) {sti(); continue;}
 111         outb(*cmdp, DATA(base));
 112         sti();
 113         return 0;
 114       }
 115   } else {
 116     cli();
 117     while (len--)
 118       {
 119         WAIT(STATUS(base), CDF, 0, CDF);
 120         outb(*cmdp++, DATA(base));
 121       }
 122     sti();
 123   }
 124     return 0;
 125   fail:
 126   sti();
 127     printk("aha1542_out failed(%d): ", len+1); aha1542_stat();
 128     return 1;
 129 }
 130 
 131 /* Only used at boot time, so we do not need to worry about latency as much
 132    here */
 133 static int aha1542_in(unsigned int base, unchar *cmdp, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 134 {
 135     cli();
 136     while (len--)
 137       {
 138           WAIT(STATUS(base), DF, DF, 0);
 139           *cmdp++ = inb(DATA(base));
 140       }
 141     sti();
 142     return 0;
 143   fail:
 144     sti();
 145     printk("aha1542_in failed(%d): ", len+1); aha1542_stat();
 146     return 1;
 147 }
 148 
 149 static int makecode(unsigned hosterr, unsigned scsierr)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151     switch (hosterr) {
 152       case 0x0:
 153       case 0xa: /* Linked command complete without error and linked normally */
 154       case 0xb: /* Linked command complete without error, interrupt generated */
 155         hosterr = 0;
 156         break;
 157 
 158       case 0x11: /* Selection time out-The initiator selection or target
 159                     reselection was not complete within the SCSI Time out period */
 160         hosterr = DID_TIME_OUT;
 161         break;
 162 
 163       case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
 164                     thean was allocated by the Data Length field or the sum of the
 165                     Scatter / Gather Data Length fields. */
 166 
 167       case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
 168 
 169       case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
 170                     invalid. This usually indicates a software failure. */
 171 
 172       case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
 173                     This usually indicates a software failure. */
 174 
 175       case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
 176                     of linked CCB's does not specify the same logical unit number as
 177                     the first. */
 178       case 0x18: /* Invalid Target Direction received from Host-The direction of a
 179                     Target Mode CCB was invalid. */
 180 
 181       case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
 182                     received to service data transfer between the same target LUN
 183                     and initiator SCSI ID in the same direction. */
 184 
 185       case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
 186                     length segment or invalid segment list boundaries was received.
 187                     A CCB parameter was invalid. */
 188         DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
 189         hosterr = DID_ERROR; /* Couldn't find any better */
 190         break;
 191 
 192       case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
 193                     phase sequence was requested by the target. The host adapter
 194                     will generate a SCSI Reset Condition, notifying the host with
 195                     a SCRD interrupt */
 196         hosterr = DID_RESET;
 197         break;
 198       default:
 199         printk("makecode: unknown hoststatus %x\n", hosterr);
 200         break;
 201     }
 202     return scsierr|(hosterr << 16);
 203 }
 204 
 205 static int aha1542_test_port(int bse, struct Scsi_Host * shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 206 {
 207     int i;
 208     unchar inquiry_cmd[] = {CMD_INQUIRY };
 209     unchar inquiry_result[4];
 210     unchar *cmdp;
 211     int len;
 212     volatile int debug = 0;
 213     
 214     /* Quick and dirty test for presence of the card. */
 215     if(inb(STATUS(bse)) == 0xff) return 0;
 216 
 217     /* Reset the adapter. I ought to make a hard reset, but it's not really nessesary */
 218     
 219     /*  DEB(printk("aha1542_test_port called \n")); */
 220 
 221     /* In case some other card was probing here, reset interrupts */
 222     aha1542_intr_reset(bse);     /* reset interrupts, so they don't block */    
 223     outb(SRST|IRST/*|SCRST*/, CONTROL(bse));
 224 
 225     i = jiffies + 2;
 226     while (i>jiffies); /* Wait a little bit for things to settle down. */
 227     
 228     debug = 1;
 229     /* Expect INIT and IDLE, any of the others are bad */
 230     WAIT(STATUS(bse), STATMASK, INIT|IDLE, STST|DIAGF|INVDCMD|DF|CDF);
 231     
 232     debug = 2;
 233     /* Shouldn't have generated any interrupts during reset */
 234     if (inb(INTRFLAGS(bse))&INTRMASK) goto fail;
 235 
 236 
 237     /* Perform a host adapter inquiry instead so we do not need to set
 238        up the mailboxes ahead of time */
 239 
 240     aha1542_out(bse, inquiry_cmd, 1);
 241 
 242     debug = 3;
 243     len = 4;
 244     cmdp = &inquiry_result[0];
 245 
 246     while (len--)
 247       {
 248           WAIT(STATUS(bse), DF, DF, 0);
 249           *cmdp++ = inb(DATA(bse));
 250       }
 251     
 252     debug = 8;
 253     /* Reading port should reset DF */
 254     if (inb(STATUS(bse)) & DF) goto fail;
 255     
 256     debug = 9;
 257     /* When HACC, command is completed, and we're though testing */
 258     WAIT(INTRFLAGS(bse), HACC, HACC, 0);
 259     /* now initialize adapter */
 260     
 261     debug = 10;
 262     /* Clear interrupts */
 263     outb(IRST, CONTROL(bse));
 264     
 265     debug = 11;
 266 
 267     return debug;                               /* 1 = ok */
 268   fail:
 269     return 0;                                   /* 0 = not ok */
 270 }
 271 
 272 static const char aha_ident[] = "Adaptec 1542";
 273 
 274 /* What's this little function for? */
 275 const char *aha1542_info(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 276 {
 277     return aha_ident;
 278 }
 279 
 280 /* A "high" level interrupt handler */
 281 static void aha1542_intr_handle(int foo)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283     void (*my_done)(Scsi_Cmnd *) = NULL;
 284     int errstatus, mbi, mbo, mbistatus;
 285     int number_serviced;
 286     struct Scsi_Host * shost;
 287     Scsi_Cmnd * SCtmp;
 288     int irqno, * irqp, flag;
 289     int needs_restart;
 290     struct mailbox * mb;
 291     struct ccb  *ccb;
 292 
 293     irqp = (int *) foo;
 294     irqp -= 2;  /* Magic - this is only required for slow interrupt handlers */
 295     irqno = *irqp;
 296 
 297     shost = aha_host[irqno - 9];
 298     mb = HOSTDATA(shost)->mb;
 299     ccb = HOSTDATA(shost)->ccb;
 300 
 301     if(!shost) panic("Splunge!");
 302 
 303 #ifdef DEBUG
 304     {
 305     flag = inb(INTRFLAGS(shost->io_port));
 306     printk("aha1542_intr_handle: ");
 307     if (!(flag&ANYINTR)) printk("no interrupt?");
 308     if (flag&MBIF) printk("MBIF ");
 309     if (flag&MBOA) printk("MBOF ");
 310     if (flag&HACC) printk("HACC ");
 311     if (flag&SCRD) printk("SCRD ");
 312     printk("status %02x\n", inb(STATUS(shost->io_port)));
 313   };
 314 #endif
 315     number_serviced = 0;
 316     needs_restart = 0;
 317 
 318     while(1==1){
 319       flag = inb(INTRFLAGS(shost->io_port));
 320 
 321       /* Check for unusual interrupts.  If any of these happen, we should
 322          probably do something special, but for now just printing a message
 323          is sufficient.  A SCSI reset detected is something that we really
 324          need to deal with in some way. */
 325       if (flag & ~MBIF) {
 326         if (flag&MBOA) printk("MBOF ");
 327         if (flag&HACC) printk("HACC ");
 328         if (flag&SCRD) {
 329           needs_restart = 1;
 330           printk("SCRD ");
 331         }
 332       }
 333 
 334       aha1542_intr_reset(shost->io_port);
 335 
 336       cli();
 337       mbi = HOSTDATA(shost)->aha1542_last_mbi_used + 1;
 338       if (mbi >= 2*AHA1542_MAILBOXES) mbi = AHA1542_MAILBOXES;
 339       
 340       do{
 341         if(mb[mbi].status != 0) break;
 342         mbi++;
 343         if (mbi >= 2*AHA1542_MAILBOXES) mbi = AHA1542_MAILBOXES;
 344       } while (mbi != HOSTDATA(shost)->aha1542_last_mbi_used);
 345       
 346       if(mb[mbi].status == 0){
 347         sti();
 348         /* Hmm, no mail.  Must have read it the last time around */
 349         if (!number_serviced && !needs_restart)
 350           printk("aha1542.c: interrupt received, but no mail.\n");
 351         /* We detected a reset.  Restart all pending commands for
 352            devices that use the hard reset option */
 353         if(needs_restart) aha1542_restart(shost);
 354         return;
 355       };
 356 
 357       mbo = (scsi2int(mb[mbi].ccbptr) - ((unsigned int) &ccb[0])) / sizeof(struct ccb);
 358       mbistatus = mb[mbi].status;
 359       mb[mbi].status = 0;
 360       HOSTDATA(shost)->aha1542_last_mbi_used = mbi;
 361       sti();
 362       
 363 #ifdef DEBUG
 364       {
 365         if (ccb[mbo].tarstat|ccb[mbo].hastat)
 366           printk("aha1542_command: returning %x (status %d)\n", 
 367                  ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
 368       };
 369 #endif
 370 
 371       if(mbistatus == 3) continue; /* Aborted command not found */
 372 
 373 #ifdef DEBUG
 374       printk("...done %d %d\n",mbo, mbi);
 375 #endif
 376       
 377       SCtmp = HOSTDATA(shost)->SCint[mbo];
 378 
 379       if (!SCtmp || !SCtmp->scsi_done) {
 380         printk("aha1542_intr_handle: Unexpected interrupt\n");
 381         printk("tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat, 
 382                ccb[mbo].hastat, ccb[mbo].idlun, mbo);
 383         return;
 384       }
 385       
 386       my_done = SCtmp->scsi_done;
 387       if (SCtmp->host_scribble) scsi_free(SCtmp->host_scribble, 512);
 388       
 389       /* Fetch the sense data, and tuck it away, in the required slot.  The
 390          Adaptec automatically fetches it, and there is no guarantee that
 391          we will still have it in the cdb when we come back */
 392       if (ccb[mbo].tarstat == 2)
 393         memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen], 
 394                sizeof(SCtmp->sense_buffer));
 395       
 396       
 397       /* is there mail :-) */
 398       
 399       /* more error checking left out here */
 400       if (mbistatus != 1)
 401         /* This is surely wrong, but I don't know what's right */
 402         errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
 403       else
 404         errstatus = 0;
 405       
 406 #ifdef DEBUG
 407       if(errstatus) printk("(aha1542 error:%x %x %x) ",errstatus, 
 408                            ccb[mbo].hastat, ccb[mbo].tarstat);
 409 #endif
 410 
 411       if (ccb[mbo].tarstat == 2) {
 412 #ifdef DEBUG
 413         int i;
 414 #endif
 415         DEB(printk("aha1542_intr_handle: sense:"));
 416 #ifdef DEBUG
 417         for (i = 0; i < 12; i++)
 418           printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen+i]);
 419         printk("\n");
 420 #endif
 421         /*
 422           DEB(printk("aha1542_intr_handle: buf:"));
 423           for (i = 0; i < bufflen; i++)
 424           printk("%02x ", ((unchar *)buff)[i]);
 425           printk("\n");
 426           */
 427       }
 428       DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
 429       SCtmp->result = errstatus;
 430       HOSTDATA(shost)->SCint[mbo] = NULL;  /* This effectively frees up the mailbox slot, as
 431                              far as queuecommand is concerned */
 432       my_done(SCtmp);
 433       number_serviced++;
 434     };
 435 }
 436 
 437 int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
 438 {
 439     unchar ahacmd = CMD_START_SCSI;
 440     unchar direction;
 441     unchar *cmd = (unchar *) SCpnt->cmnd;
 442     unchar target = SCpnt->target;
 443     unchar lun = SCpnt->lun;
 444     void *buff = SCpnt->request_buffer;
 445     int bufflen = SCpnt->request_bufflen;
 446     int mbo;
 447     struct mailbox * mb;
 448     struct ccb  *ccb;
 449 
 450     DEB(int i);
 451 
 452     mb = HOSTDATA(SCpnt->host)->mb;
 453     ccb = HOSTDATA(SCpnt->host)->ccb;
 454 
 455     DEB(if (target > 1) {
 456       SCpnt->result = DID_TIME_OUT << 16;
 457       done(SCpnt); return 0;});
 458     
 459     if(*cmd == REQUEST_SENSE){
 460 #ifndef DEBUG
 461       if (bufflen != sizeof(SCpnt->sense_buffer)) {
 462         printk("Wrong buffer length supplied for request sense (%d)\n",bufflen);
 463       };
 464 #endif
 465       SCpnt->result = 0;
 466       done(SCpnt); 
 467       return 0;
 468     };
 469 
 470 #ifdef DEBUG
 471     if (*cmd == READ_10 || *cmd == WRITE_10)
 472       i = xscsi2int(cmd+2);
 473     else if (*cmd == READ_6 || *cmd == WRITE_6)
 474       i = scsi2int(cmd+2);
 475     else
 476       i = -1;
 477     if (done)
 478       printk("aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
 479     else
 480       printk("aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
 481     aha1542_stat();
 482     printk("aha1542_queuecommand: dumping scsi cmd:");
 483     for (i = 0; i < (COMMAND_SIZE(*cmd)); i++) printk("%02x ", cmd[i]);
 484     printk("\n");
 485     if (*cmd == WRITE_10 || *cmd == WRITE_6)
 486       return 0; /* we are still testing, so *don't* write */
 487 #endif
 488 /* Use the outgoing mailboxes in a round-robin fashion, because this
 489    is how the host adapter will scan for them */
 490 
 491     cli();
 492     mbo = HOSTDATA(SCpnt->host)->aha1542_last_mbo_used + 1;
 493     if (mbo >= AHA1542_MAILBOXES) mbo = 0;
 494 
 495     do{
 496       if(mb[mbo].status == 0 && HOSTDATA(SCpnt->host)->SCint[mbo] == NULL)
 497         break;
 498       mbo++;
 499       if (mbo >= AHA1542_MAILBOXES) mbo = 0;
 500     } while (mbo != HOSTDATA(SCpnt->host)->aha1542_last_mbo_used);
 501 
 502     if(mb[mbo].status || HOSTDATA(SCpnt->host)->SCint[mbo])
 503       panic("Unable to find empty mailbox for aha1542.\n");
 504 
 505     HOSTDATA(SCpnt->host)->SCint[mbo] = SCpnt;  /* This will effectively prevent someone else from
 506                             screwing with this cdb. */
 507 
 508     HOSTDATA(SCpnt->host)->aha1542_last_mbo_used = mbo;    
 509     sti();
 510 
 511 #ifdef DEBUG
 512     printk("Sending command (%d %x)...",mbo, done);
 513 #endif
 514 
 515     any2scsi(mb[mbo].ccbptr, &ccb[mbo]); /* This gets trashed for some reason*/
 516 
 517     memset(&ccb[mbo], 0, sizeof(struct ccb));
 518 
 519     ccb[mbo].cdblen = COMMAND_SIZE(*cmd);     /* SCSI Command Descriptor Block Length */
 520 
 521     direction = 0;
 522     if (*cmd == READ_10 || *cmd == READ_6)
 523         direction = 8;
 524     else if (*cmd == WRITE_10 || *cmd == WRITE_6)
 525         direction = 16;
 526 
 527     memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
 528 
 529     if (SCpnt->use_sg) {
 530       struct scatterlist * sgpnt;
 531       struct chain * cptr;
 532 #ifdef DEBUG
 533       unsigned char * ptr;
 534 #endif
 535       int i;
 536       ccb[mbo].op = 2;        /* SCSI Initiator Command  w/scatter-gather*/
 537       SCpnt->host_scribble = (unsigned char *) scsi_malloc(512);
 538       sgpnt = (struct scatterlist *) SCpnt->request_buffer;
 539       cptr = (struct chain *) SCpnt->host_scribble; 
 540       if (cptr == NULL) panic("aha1542.c: unable to allocate DMA memory\n");
 541       for(i=0; i<SCpnt->use_sg; i++) {
 542         if(sgpnt[i].length == 0 || SCpnt->use_sg > 16 || 
 543            (((int)sgpnt[i].address) & 1) || (sgpnt[i].length & 1)){
 544           unsigned char * ptr;
 545           printk("Bad segment list supplied to aha1542.c (%d, %d)\n",SCpnt->use_sg,i);
 546           for(i=0;i<SCpnt->use_sg;i++){
 547             printk("%d: %x %x %d\n",i,(unsigned int) sgpnt[i].address, (unsigned int) sgpnt[i].alt_address,
 548                    sgpnt[i].length);
 549           };
 550           printk("cptr %x: ",(unsigned int) cptr);
 551           ptr = (unsigned char *) &cptr[i];
 552           for(i=0;i<18;i++) printk("%02x ", ptr[i]);
 553           panic("Foooooooood fight!");
 554         };
 555         any2scsi(cptr[i].dataptr, sgpnt[i].address);
 556         if(((unsigned  int) sgpnt[i].address) & 0xff000000) goto baddma;
 557         any2scsi(cptr[i].datalen, sgpnt[i].length);
 558       };
 559       any2scsi(ccb[mbo].datalen, SCpnt->use_sg * sizeof(struct chain));
 560       if(((unsigned int) buff & 0xff000000)) goto baddma;
 561       any2scsi(ccb[mbo].dataptr, cptr);
 562 #ifdef DEBUG
 563       printk("cptr %x: ",cptr);
 564       ptr = (unsigned char *) cptr;
 565       for(i=0;i<18;i++) printk("%02x ", ptr[i]);
 566 #endif
 567     } else {
 568       ccb[mbo].op = 0;        /* SCSI Initiator Command */
 569       SCpnt->host_scribble = NULL;
 570       any2scsi(ccb[mbo].datalen, bufflen);
 571       any2scsi(ccb[mbo].dataptr, buff);
 572     };
 573     ccb[mbo].idlun = (target&7)<<5 | direction | (lun & 7); /*SCSI Target Id*/
 574     ccb[mbo].rsalen = 12;
 575     ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
 576     ccb[mbo].commlinkid = 0;
 577 
 578 #ifdef DEBUGd
 579     { int i;
 580     printk("aha1542_command: sending.. ");
 581     for (i = 0; i < sizeof(ccb[mbo])-10; i++)
 582       printk("%02x ", ((unchar *)&ccb[mbo])[i]);
 583     };
 584 #endif
 585     
 586     if (done) {
 587         DEB(printk("aha1542_queuecommand: now waiting for interrupt "); aha1542_stat());
 588         SCpnt->scsi_done = done;
 589         mb[mbo].status = 1;
 590         aha1542_out(SCpnt->host->io_port, &ahacmd, 1);          /* start scsi command */
 591         DEB(aha1542_stat());
 592     }
 593     else
 594       printk("aha1542_queuecommand: done can't be NULL\n");
 595     
 596     return 0;
 597  baddma:
 598     panic("Buffer at address  > 16Mb used for 1542B");
 599 }
 600 
 601 static void internal_done(Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 602 {
 603         SCpnt->SCp.Status++;
 604 }
 605 
 606 int aha1542_command(Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 607 {
 608     DEB(printk("aha1542_command: ..calling aha1542_queuecommand\n"));
 609 
 610     aha1542_queuecommand(SCpnt, internal_done);
 611 
 612     SCpnt->SCp.Status = 0;
 613     while (!SCpnt->SCp.Status);
 614     return SCpnt->result;
 615 }
 616 
 617 /* Initialize mailboxes */
 618 static void setup_mailboxes(int bse, struct Scsi_Host * shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 619 {
 620     int i;
 621     struct mailbox * mb;
 622     struct ccb  *ccb;
 623 
 624     unchar cmd[5] = {CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
 625 
 626     mb = HOSTDATA(shpnt)->mb;
 627     ccb = HOSTDATA(shpnt)->ccb;
 628 
 629     for(i=0; i<AHA1542_MAILBOXES; i++){
 630       mb[i].status = mb[AHA1542_MAILBOXES+i].status = 0;
 631       any2scsi(mb[i].ccbptr, &ccb[i]);
 632     };
 633     aha1542_intr_reset(bse);     /* reset interrupts, so they don't block */    
 634     any2scsi((cmd+2), mb);
 635     aha1542_out(bse, cmd, 5);
 636     WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
 637     while (0) {
 638       fail:
 639         printk("aha1542_detect: failed setting up mailboxes\n");
 640     }
 641     aha1542_intr_reset(bse);
 642 }
 643 
 644 static int aha1542_getconfig(int base_io, unsigned char * irq_level, unsigned char * dma_chan)
     /* [previous][next][first][last][top][bottom][index][help] */
 645 {
 646   unchar inquiry_cmd[] = {CMD_RETCONF };
 647   unchar inquiry_result[3];
 648   int i;
 649   i = inb(STATUS(base_io));
 650   if (i & DF) {
 651     i = inb(DATA(base_io));
 652   };
 653   aha1542_out(base_io, inquiry_cmd, 1);
 654   aha1542_in(base_io, inquiry_result, 3);
 655   WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
 656   while (0) {
 657   fail:
 658     printk("aha1542_detect: query board settings\n");
 659   }
 660   aha1542_intr_reset(base_io);
 661   switch(inquiry_result[0]){
 662   case 0x80:
 663     *dma_chan = 7;
 664     break;
 665   case 0x40:
 666     *dma_chan = 6;
 667     break;
 668   case 0x20:
 669     *dma_chan = 5;
 670     break;
 671   case 0x01:
 672     printk("DMA priority 0 not available for Adaptec driver\n");
 673     return -1;
 674   case 0:
 675     /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
 676        Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
 677     *dma_chan = 0xFF;
 678     break;
 679   default:
 680     printk("Unable to determine Adaptec DMA priority.  Disabling board\n");
 681     return -1;
 682   };
 683   switch(inquiry_result[1]){
 684   case 0x40:
 685     *irq_level = 15;
 686     break;
 687   case 0x20:
 688     *irq_level = 14;
 689     break;
 690   case 0x8:
 691     *irq_level = 12;
 692     break;
 693   case 0x4:
 694     *irq_level = 11;
 695     break;
 696   case 0x2:
 697     *irq_level = 10;
 698     break;
 699   case 0x1:
 700     *irq_level = 9;
 701     break;
 702   default:
 703     printk("Unable to determine Adaptec IRQ level.  Disabling board\n");
 704     return -1;
 705   };
 706   return 0;
 707 }
 708 
 709 /* This function should only be called for 1542C boards - we can detect
 710    the special firmware settings and unlock the board */
 711 
 712 static int aha1542_mbenable(int base)
     /* [previous][next][first][last][top][bottom][index][help] */
 713 {
 714   static unchar mbenable_cmd[3];
 715   static unchar mbenable_result[2];
 716   int retval;
 717   
 718   retval = BIOS_TRANSLATION_6432;
 719 
 720   mbenable_cmd[0]=CMD_EXTBIOS;
 721   aha1542_out(base,mbenable_cmd,1);
 722   aha1542_in(base,mbenable_result,2);
 723   WAIT(INTRFLAGS(base),INTRMASK,HACC,0);
 724   aha1542_intr_reset(base);
 725   
 726   if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
 727      mbenable_cmd[0]=CMD_MBENABLE;
 728      mbenable_cmd[1]=0;
 729      mbenable_cmd[2]=mbenable_result[1];
 730      if(mbenable_result[1] & 1) retval = BIOS_TRANSLATION_25563;
 731      aha1542_out(base,mbenable_cmd,3);
 732      WAIT(INTRFLAGS(base),INTRMASK,HACC,0);
 733   };
 734   while(0) {
 735 fail:
 736     printk("aha1542_mbenable: Mailbox init failed\n");
 737   }
 738 aha1542_intr_reset(base);
 739 return retval;
 740 }
 741 
 742 /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
 743 static int aha1542_query(int base_io, int * transl)
     /* [previous][next][first][last][top][bottom][index][help] */
 744 {
 745   unchar inquiry_cmd[] = {CMD_INQUIRY };
 746   unchar inquiry_result[4];
 747   int i;
 748   i = inb(STATUS(base_io));
 749   if (i & DF) {
 750     i = inb(DATA(base_io));
 751   };
 752   aha1542_out(base_io, inquiry_cmd, 1);
 753   aha1542_in(base_io, inquiry_result, 4);
 754   WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
 755   while (0) {
 756   fail:
 757     printk("aha1542_detect: query card type\n");
 758   }
 759   aha1542_intr_reset(base_io);
 760 
 761   *transl = BIOS_TRANSLATION_6432; /* Default case */
 762 
 763 /* For an AHA1740 series board, we ignore the board since there is a
 764    hardware bug which can lead to wrong blocks being returned if the board
 765    is operating in the 1542 emulation mode.  Since there is an extended mode
 766    driver, we simply ignore the board and let the 1740 driver pick it up.
 767 */
 768 
 769   if (inquiry_result[0] == 0x43) {
 770     printk("aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
 771     return 1;
 772   };
 773 
 774   /* 1542C returns 0x44, 1542CF returns 0x45 */
 775   if (inquiry_result[0] == 0x44 || inquiry_result[0] == 0x45)
 776     { /* Detect 1542C  */
 777       *transl = aha1542_mbenable(base_io);
 778     };
 779   return 0;
 780 }
 781 
 782 
 783 /* return non-zero on detection */
 784 int aha1542_detect(int hostnum)
     /* [previous][next][first][last][top][bottom][index][help] */
 785 {
 786     unsigned char dma_chan;
 787     unsigned char irq_level;
 788     unsigned int base_io;
 789     int trans;
 790     struct Scsi_Host * shpnt = NULL;
 791     int count = 0;
 792     int indx;
 793 
 794     DEB(printk("aha1542_detect: \n"));
 795     
 796     for(indx = 0; indx < sizeof(bases)/sizeof(bases[0]); indx++)
 797             if(!check_region(bases[indx], 4)) { 
 798                     shpnt = scsi_register(hostnum,
 799                                           sizeof(struct aha1542_hostdata));
 800 
 801                     if(!aha1542_test_port(bases[indx], shpnt)) goto unregister;
 802 
 803 
 804                     base_io = bases[indx];
 805                     
 806                     /* Set the Bus on/off-times as not to ruin floppy performance */
 807             {
 808                     unchar oncmd[] = {CMD_BUSON_TIME, 7};
 809                     unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
 810                     
 811                     aha1542_intr_reset(base_io);
 812                     aha1542_out(base_io, oncmd, 2);
 813                     WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
 814                     aha1542_intr_reset(base_io);
 815                     aha1542_out(base_io, offcmd, 2);
 816                     WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
 817                     while (0) {
 818                     fail:
 819                             printk("aha1542_detect: setting bus on/off-time failed\n");
 820                     }
 821                     aha1542_intr_reset(base_io);
 822             }
 823                     if(aha1542_query(base_io, &trans))  goto unregister;
 824                     
 825                     if (aha1542_getconfig(base_io, &irq_level, &dma_chan) == -1)  goto unregister;
 826                     
 827                     printk("Configuring Adaptec at IO:%x, IRQ %d",base_io, irq_level);
 828                     if (dma_chan != 0xFF)
 829                             printk(", DMA priority %d", dma_chan);
 830                     printk("\n");
 831                     
 832                     DEB(aha1542_stat());
 833                     setup_mailboxes(base_io, shpnt);
 834                     
 835                     DEB(aha1542_stat());
 836                     
 837                     DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
 838                     cli();
 839                     if (request_irq(irq_level,aha1542_intr_handle)) {
 840                             printk("Unable to allocate IRQ for adaptec controller.\n");
 841                             goto unregister;
 842                     }
 843                     
 844                     if (dma_chan != 0xFF) {
 845                             if (request_dma(dma_chan)) {
 846                                     printk("Unable to allocate DMA channel for Adaptec.\n");
 847                                     free_irq(irq_level);
 848                                     goto unregister;
 849                             }
 850                             
 851                             if (dma_chan >= 5) {
 852                                     outb((dma_chan - 4) | CASCADE, DMA_MODE_REG);
 853                                     outb(dma_chan - 4, DMA_MASK_REG);
 854                             }
 855                     }
 856                     aha_host[irq_level - 9] = shpnt;
 857                     shpnt->io_port = base_io;
 858                     shpnt->dma_channel = dma_chan;
 859                     shpnt->irq = irq_level;
 860                     HOSTDATA(shpnt)->bios_translation  = trans;
 861                     if(trans == 2) 
 862                       printk("aha1542.c: Using extended bios translation\n");
 863                     HOSTDATA(shpnt)->aha1542_last_mbi_used  = (2*AHA1542_MAILBOXES - 1);
 864                     HOSTDATA(shpnt)->aha1542_last_mbo_used  = (AHA1542_MAILBOXES - 1);
 865                     memset(HOSTDATA(shpnt)->SCint, 0, sizeof(HOSTDATA(shpnt)->SCint));
 866                     sti();
 867 #if 0
 868                     DEB(printk(" *** READ CAPACITY ***\n"));
 869                     
 870             {
 871                     unchar buf[8];
 872                     static unchar cmd[] = {     READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 873                     int i;
 874                     
 875                     for (i = 0; i < sizeof(buf); ++i) buf[i] = 0x87;
 876                     for (i = 0; i < 2; ++i)
 877                             if (!aha1542_command(i, cmd, buf, sizeof(buf))) {
 878                                     printk("aha_detect: LU %d sector_size %d device_size %d\n",
 879                                            i, xscsi2int(buf+4), xscsi2int(buf));
 880                             }
 881             }
 882                     
 883                     DEB(printk(" *** NOW RUNNING MY OWN TEST *** \n"));
 884                     
 885                     for (i = 0; i < 4; ++i)
 886                     {
 887                             unsigned char cmd[10];
 888                             static buffer[512];
 889                             
 890                             cmd[0] = READ_10;
 891                             cmd[1] = 0;
 892                             xany2scsi(cmd+2, i);
 893                             cmd[6] = 0;
 894                             cmd[7] = 0;
 895                             cmd[8] = 1;
 896                             cmd[9] = 0;
 897                             aha1542_command(0, cmd, buffer, 512);
 898                     }
 899 #endif    
 900                     snarf_region(bases[indx], 4);  /* Register the IO ports that we use */
 901                     count++;
 902                     continue;
 903             unregister:
 904                     scsi_unregister(shpnt, sizeof(struct aha1542_hostdata));
 905                     continue;
 906                     
 907             };
 908     return count;
 909 }
 910 
 911 static int aha1542_restart(struct Scsi_Host * shost)
     /* [previous][next][first][last][top][bottom][index][help] */
 912 {
 913   int i;
 914   int count = 0;
 915 #if 0
 916   unchar ahacmd = CMD_START_SCSI;
 917 #endif
 918 
 919   for(i=0; i< AHA1542_MAILBOXES; i++)
 920    if(HOSTDATA(shost)->SCint[i] && 
 921       !(HOSTDATA(shost)->SCint[i]->device->soft_reset))
 922      {
 923 #if 0
 924         HOSTDATA(shost)->mb[i].status = 1; /* Indicate ready to restart... */
 925 #endif
 926         count++;
 927      }     
 928 
 929   printk("Potential to restart %d stalled commands...\n", count);
 930 #if 0
 931   /* start scsi command */
 932   if (count) aha1542_out(shost->io_port, &ahacmd, 1);
 933 #endif
 934   return 0;
 935 }
 936 
 937 /* The abort command does not leave the device in a clean state where
 938    it is available to be used again.  Until this gets worked out, we will
 939    leave it commented out.  */
 940 
 941 int aha1542_abort(Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 942 {
 943 #if 0
 944   int intval[3];
 945   unchar ahacmd = CMD_START_SCSI;
 946   struct mailbox * mb;
 947   int mbi, mbo, i;
 948 
 949   printk("In aha1542_abort: %x %x\n",
 950          inb(STATUS(SCpnt->host->io_port)),
 951          inb(INTRFLAGS(SCpnt->host->io_port)));
 952 
 953   cli();
 954   mb = HOSTDATA(SCpnt->host)->mb;
 955   mbi = HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
 956   if (mbi >= 2*AHA1542_MAILBOXES) mbi = AHA1542_MAILBOXES;
 957   
 958   do{
 959     if(mb[mbi].status != 0) break;
 960     mbi++;
 961     if (mbi >= 2*AHA1542_MAILBOXES) mbi = AHA1542_MAILBOXES;
 962   } while (mbi != HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
 963   sti();
 964 
 965   if(mb[mbi].status) {
 966     printk("Lost interrupt discovered on irq %d - attempting to recover\n", 
 967            SCpnt->host->irq);
 968     intval[0] = SCpnt->host->irq;
 969     aha1542_intr_handle((int) &intval[2]);
 970     return 0;
 971   }
 972 
 973   /* OK, no lost interrupt.  Try looking to see how many pending commands
 974      we think we have. */
 975 
 976   for(i=0; i< AHA1542_MAILBOXES; i++)
 977    if(HOSTDATA(SCpnt->host)->SCint[i])
 978      {
 979        if(HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
 980          printk("Timed out command pending for %4.4x\n", SCpnt->request.dev);
 981          if (HOSTDATA(SCpnt->host)->mb[i].status) {
 982            printk("OGMB still full - restarting\n");
 983            aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
 984          };
 985        } else
 986          printk("Other pending command %4.4x\n", SCpnt->request.dev);
 987      }
 988 
 989 #endif
 990 
 991     DEB(printk("aha1542_abort\n"));
 992 #if 0
 993     cli();
 994     for(mbo = 0; mbo < AHA1542_MAILBOXES; mbo++)
 995       if (SCpnt == HOSTDATA(SCpnt->host)->SCint[mbo]){
 996         mb[mbo].status = 2;  /* Abort command */
 997         aha1542_out(SCpnt->host->io_port, &ahacmd, 1); /* start scsi command */
 998         sti();
 999         break;
1000       };
1001 #endif
1002     return SCSI_ABORT_SNOOZE;
1003 }
1004 
1005 /* We do not implement a reset function here, but the upper level code
1006    assumes that it will get some kind of response for the command in
1007    SCpnt.  We must oblige, or the command will hang the scsi system.
1008    For a first go, we assume that the 1542 notifies us with all of the
1009    pending commands (it does implement soft reset, after all). */
1010 
1011 int aha1542_reset(Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1012 {
1013     unchar ahacmd = CMD_START_SCSI;
1014     int i;
1015 
1016     DEB(printk("aha1542_reset called\n"));
1017 #if 0
1018     /* This does a scsi reset for all devices on the bus */
1019     outb(SCRST, CONTROL(SCpnt->host->io_port));
1020 #else
1021     /* This does a selective reset of just the one device */
1022     /* First locate the ccb for this command */
1023     for(i=0; i< AHA1542_MAILBOXES; i++)
1024       if(HOSTDATA(SCpnt->host)->SCint[i] == SCpnt)
1025         {
1026           HOSTDATA(SCpnt->host)->ccb[i].op = 0x81;  /* BUS DEVICE RESET */
1027           /* Now tell the 1542 to flush all pending commands for this target */
1028           aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
1029 
1030           /* Here is the tricky part.  What to do next.  Do we get an interrupt
1031              for the commands that we aborted with the specified target, or
1032              do we generate this on our own?  Try it without first and see
1033              what happens */
1034           printk("Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
1035 
1036           /* If the first does not work, then try the second.  I think the
1037              first option is more likely to be correct. Free the command
1038              block for all commands running on this target... */
1039 #if 1
1040           for(i=0; i< AHA1542_MAILBOXES; i++)
1041             if(HOSTDATA(SCpnt->host)->SCint[i] &&
1042                HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target)
1043             {
1044               Scsi_Cmnd * SCtmp;
1045               SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
1046               SCtmp->result = DID_RESET << 16;
1047               if (SCtmp->host_scribble) scsi_free(SCtmp->host_scribble, 512);
1048               printk("Sending DID_RESET for target %d\n", SCpnt->target);
1049               SCtmp->scsi_done(SCpnt);
1050               
1051               HOSTDATA(SCpnt->host)->SCint[i] = NULL;
1052             }
1053           return SCSI_RESET_SUCCESS;
1054 #else
1055           return SCSI_RESET_PENDING;
1056 #endif
1057         }
1058 
1059 #endif
1060     /* No active command at this time, so this means that each time we got
1061        some kind of response the last time through.  Tell the mid-level code
1062        to request sense information in order to decide what to do next. */
1063     return SCSI_RESET_PUNT;
1064 }
1065 
1066 #ifdef CONFIG_BLK_DEV_SD
1067 #include "sd.h"
1068 #endif
1069 
1070 int aha1542_biosparam(int size, int dev, int * ip)
     /* [previous][next][first][last][top][bottom][index][help] */
1071 {
1072   int translation_algorithm;
1073 #ifdef CONFIG_BLK_DEV_SD
1074   Scsi_Device *disk;
1075 
1076   disk = rscsi_disks[MINOR(dev) >> 4].device;
1077   translation_algorithm = HOSTDATA(disk->host)->bios_translation;
1078   /* Should this be > 1024, or >= 1024?  Enquiring minds want to know. */
1079   if((size>>11) > 1024 && translation_algorithm == 2) {
1080     /* Please verify that this is the same as what DOS returns */
1081     ip[0] = 255;
1082     ip[1] = 63;
1083     ip[2] = size /255/63;
1084   } else {
1085     ip[0] = 64;
1086     ip[1] = 32;
1087     ip[2] = size >> 11;
1088   };
1089 /*  if (ip[2] >= 1024) ip[2] = 1024; */
1090 #endif
1091   return 0;
1092 }

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