root/drivers/scsi/aic7xxx.c

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

DEFINITIONS

This source file includes following definitions.
  1. debug
  2. debug_config
  3. debug_rate
  4. aic7xxx_setup
  5. aic7xxx_getscb
  6. aic7xxx_length
  7. aic7xxx_sg_check
  8. aic7xxx_to_scsirate
  9. aic7xxx_isr
  10. aic7xxx_probe
  11. aic7xxx_type
  12. aic7xxx_loadram
  13. aha274x_config
  14. aha284x_config
  15. aha294x_config
  16. aic7xxx_register
  17. aic7xxx_detect
  18. aic7xxx_info
  19. aic7xxx_buildscb
  20. aic7xxx_putscb
  21. aic7xxx_queue
  22. aic7xxx_kill
  23. aic7xxx_abort
  24. aic7xxx_reset
  25. aic7xxx_biosparam

   1 /*
   2  *  @(#)aic7xxx.c 1.34 94/11/30 jda
   3  *
   4  *  Adaptec 274x/284x/294x device driver for Linux.
   5  *  Copyright (c) 1994 The University of Calgary Department of Computer Science.
   6  *  
   7  *  This program is free software; you can redistribute it and/or modify
   8  *  it under the terms of the GNU General Public License as published by
   9  *  the Free Software Foundation; either version 2 of the License, or
  10  *  (at your option) any later version.
  11  *  
  12  *  This program is distributed in the hope that it will be useful,
  13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  *  GNU General Public License for more details.
  16  *  
  17  *  You should have received a copy of the GNU General Public License
  18  *  along with this program; if not, write to the Free Software
  19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20  *
  21  *  Sources include the Adaptec 1740 driver (aha1740.c), the
  22  *  Ultrastor 24F driver (ultrastor.c), various Linux kernel
  23  *  source, the Adaptec EISA config file (!adp7771.cfg), the
  24  *  Adaptec AHA-2740A Series User's Guide, the Linux Kernel
  25  *  Hacker's Guide, Writing a SCSI Device Driver for Linux,
  26  *  the Adaptec 1542 driver (aha1542.c), the Adaptec EISA
  27  *  overlay file (adp7770.ovl), the Adaptec AHA-2740 Series
  28  *  Technical Reference Manual, the Adaptec AIC-7770 Data
  29  *  Book, the ANSI SCSI specification, the ANSI SCSI-2
  30  *  specification (draft 10c), ...
  31  *
  32  *  On a twin-bus adapter card, channel B is ignored.  Rationale:
  33  *  it would greatly complicate the sequencer and host driver code,
  34  *  and both busses are multiplexed on to the EISA bus anyway.  So
  35  *  I don't really see any technical advantage to supporting both.
  36  *
  37  *  As well, multiple adapter card using the same IRQ level are
  38  *  not supported.  It doesn't make sense to configure the cards
  39  *  this way from a performance standpoint.  Not to mention that
  40  *  the kernel would have to support two devices per registered IRQ.
  41  */
  42 
  43 #include <stdarg.h>
  44 #include <asm/io.h>
  45 #include <linux/string.h>
  46 #include <linux/kernel.h>
  47 #include <linux/ioport.h>
  48 #include <linux/bios32.h>
  49 #include <linux/delay.h>
  50 #include <linux/pci.h>
  51 
  52 #include "../block/blk.h"
  53 #include "sd.h"
  54 #include "scsi.h"
  55 #include "hosts.h"
  56 #include "aic7xxx.h"
  57 
  58 /*
  59  *  There should be a specific return value for this in scsi.h, but
  60  *  it seems that most drivers ignore it.
  61  */
  62 #define DID_UNDERFLOW   DID_ERROR
  63 
  64 /* EISA/VL-bus stuff */
  65 
  66 #define MINSLOT         1
  67 #define MAXSLOT         15
  68 #define SLOTBASE(x)     ((x) << 12)
  69 
  70 #define MAXIRQ          15
  71 
  72 /* AIC-7770 offset definitions */
  73 
  74 #define O_MINREG(x)     ((x) + 0xc00)           /* i/o range to reserve */
  75 #define O_MAXREG(x)     ((x) + 0xcbf)
  76 
  77 #define O_SCSISEQ(x)    ((x) + 0xc00)           /* scsi sequence control */
  78 #define O_SCSISIGI(x)   ((x) + 0xc03)           /* scsi control signal read */
  79 #define O_SCSISIGO(x)   ((x) + 0xc03)           /* scsi control signal write */
  80 #define O_SCSIID(x)     ((x) + 0xc05)           /* scsi id */
  81 #define O_SSTAT0(x)     ((x) + 0xc0b)           /* scsi status register 0 */
  82 #define O_CLRSINT1(x)   ((x) + 0xc0c)           /* clear scsi interrupt 1 */
  83 #define O_SSTAT1(x)     ((x) + 0xc0c)           /* scsi status register 1 */
  84 #define O_SELID(x)      ((x) + 0xc19)           /* [re]selection id */
  85 #define O_SBLKCTL(x)    ((x) + 0xc1f)           /* scsi block control */
  86 #define O_SEQCTL(x)     ((x) + 0xc60)           /* sequencer control */
  87 #define O_SEQRAM(x)     ((x) + 0xc61)           /* sequencer ram data */
  88 #define O_SEQADDR(x)    ((x) + 0xc62)           /* sequencer address (W) */
  89 #define O_BIDx(x)       ((x) + 0xc80)           /* board id */
  90 #define O_BCTL(x)       ((x) + 0xc84)           /* board control */
  91 #define O_HCNTRL(x)     ((x) + 0xc87)           /* host control */
  92 #define O_SCBPTR(x)     ((x) + 0xc90)           /* scb pointer */
  93 #define O_INTSTAT(x)    ((x) + 0xc91)           /* interrupt status */
  94 #define O_ERROR(x)      ((x) + 0xc92)           /* hard error */
  95 #define O_CLRINT(x)     ((x) + 0xc92)           /* clear interrupt status */
  96 #define O_SCBCNT(x)     ((x) + 0xc9a)           /* scb auto increment */
  97 #define O_QINFIFO(x)    ((x) + 0xc9b)           /* queue in fifo */
  98 #define O_QINCNT(x)     ((x) + 0xc9c)           /* queue in count */
  99 #define O_QOUTFIFO(x)   ((x) + 0xc9d)           /* queue out fifo */
 100 #define O_QOUTCNT(x)    ((x) + 0xc9e)           /* queue out count */
 101 #define O_SCBARRAY(x)   ((x) + 0xca0)           /* scb array start */
 102 
 103 /* AIC-7870-only definitions */
 104 
 105 #define O_DSPCISTATUS(x) ((x) + 0xc86)          /* ??? */
 106 
 107 /* host adapter offset definitions */
 108 
 109 #define HA_REJBYTE(x)   ((x) + 0xc31)           /* 1st message in byte */
 110 #define HA_MSG_FLAGS(x) ((x) + 0xc35)           /* outgoing message flag */
 111 #define HA_MSG_LEN(x)   ((x) + 0xc36)           /* outgoing message length */
 112 #define HA_MSG_START(x) ((x) + 0xc37)           /* outgoing message body */
 113 #define HA_ARG_1(x)     ((x) + 0xc4c)           /* sdtr <-> rate parameters */
 114 #define HA_ARG_2(x)     ((x) + 0xc4d)
 115 #define HA_RETURN_1(x)  ((x) + 0xc4c)
 116 #define HA_RETURN_2(x)  ((x) + 0xc4d)
 117 #define HA_SIGSTATE(x)  ((x) + 0xc4e)           /* value in SCSISIGO */
 118 #define HA_NEEDSDTR(x)  ((x) + 0xc4f)           /* synchronous negotiation? */
 119 #define HA_SCBCOUNT(x)  ((x) + 0xc56)           /* number of hardware SCBs */
 120 
 121 #define HA_SCSICONF(x)  ((x) + 0xc5a)           /* SCSI config register */
 122 #define HA_INTDEF(x)    ((x) + 0xc5c)           /* interrupt def'n register */
 123 #define HA_HOSTCONF(x)  ((x) + 0xc5d)           /* host config def'n register */
 124 
 125 /* debugging code */
 126 
 127 /*
 128 #define AIC7XXX_DEBUG
 129 */
 130 
 131 /*
 132  *  If a parity error occurs during a data transfer phase, run the
 133  *  command to completion - it's easier that way - making a note
 134  *  of the error condition in this location.  This then will modify
 135  *  a DID_OK status into a DID_PARITY one for the higher-level SCSI
 136  *  code.
 137  */
 138 #define aic7xxx_parity(cmd)     ((cmd)->SCp.Status)
 139 
 140 /*
 141  *  Since the sequencer code DMAs the scatter-gather structures
 142  *  directly from memory, we use this macro to assert that the
 143  *  kernel structure hasn't changed.
 144  */
 145 #define SG_STRUCT_CHECK(sg) \
 146         ((char *)&(sg).address - (char *)&(sg) != 0 ||  \
 147          (char *)&(sg).length  - (char *)&(sg) != 8 ||  \
 148          sizeof((sg).address) != 4 ||                   \
 149          sizeof((sg).length)  != 4 ||                   \
 150          sizeof(sg)           != 12)
 151 
 152 /*
 153  *  "Static" structures.  Note that these are NOT initialized
 154  *  to zero inside the kernel - we have to initialize them all
 155  *  explicitly.
 156  *
 157  *  We support a maximum of one adapter card per IRQ level (see the
 158  *  rationale for this above).  On an interrupt, use the IRQ as an
 159  *  index into aic7xxx_boards[] to locate the card information.
 160  */
 161 static struct Scsi_Host *aic7xxx_boards[MAXIRQ + 1];
 162 
 163 /*
 164  *  The maximum number of SCBs we could have for ANY type
 165  *  of card.  DON'T FORGET TO CHANGE THE SCB MASK IN THE
 166  *  SEQUENCER CODE IF THIS IS MODIFIED!
 167  */
 168 #define AIC7XXX_MAXSCB  16
 169 
 170 struct aic7xxx_host {
 171         int base;                                       /* card base address */
 172         int maxscb;                                     /* hardware SCBs */
 173         int startup;                                    /* intr type check */
 174         int extended;                                   /* extended xlate? */
 175         volatile int unpause;                           /* value for HCNTRL */
 176         volatile Scsi_Cmnd *SCB_array[AIC7XXX_MAXSCB];  /* active commands */
 177 };
 178 
 179 struct aic7xxx_host_config {
 180         int irq;                                        /* IRQ number */
 181         int base;                                       /* I/O base */
 182         int maxscb;                                     /* hardware SCBs */
 183         int unpause;                                    /* value for HCNTRL */
 184         int scsi_id;                                    /* host SCSI id */
 185         int extended;                                   /* extended xlate? */
 186 };
 187 
 188 struct aic7xxx_scb {
 189         unsigned char control;
 190         unsigned char target_channel_lun;               /* 4/1/3 bits */
 191         unsigned char SG_segment_count;
 192         unsigned char SG_list_pointer[4];
 193         unsigned char SCSI_cmd_pointer[4];
 194         unsigned char SCSI_cmd_length;
 195         unsigned char RESERVED[2];                      /* must be zero */
 196         unsigned char target_status;
 197         unsigned char residual_data_count[3];
 198         unsigned char residual_SG_segment_count;
 199         unsigned char data_pointer[4];
 200         unsigned char data_count[3];
 201 #if 0
 202         /*
 203          *  No real point in transferring this to the
 204          *  SCB registers.
 205          */
 206         unsigned char RESERVED[6];
 207 #endif
 208 };
 209 
 210 /*
 211  *  NB.  This table MUST be ordered shortest period first.
 212  */
 213 static struct {
 214         short period;
 215         short rate;
 216         char *english;
 217 } aic7xxx_synctab[] = {
 218         100,    0,      "10.0",
 219         125,    1,      "8.0",
 220         150,    2,      "6.67",
 221         175,    3,      "5.7",
 222         200,    4,      "5.0",
 223         225,    5,      "4.4",
 224         250,    6,      "4.0",
 225         275,    7,      "3.6"
 226 };
 227 
 228 static int aic7xxx_synctab_max =
 229         sizeof(aic7xxx_synctab) / sizeof(aic7xxx_synctab[0]);
 230 
 231 enum aha_type {
 232         T_NONE,
 233         T_274X,
 234         T_284X,
 235         T_294X,
 236         T_MAX
 237 };
 238 
 239 #ifdef AIC7XXX_DEBUG
 240 
 241         extern int vsprintf(char *, const char *, va_list);
 242 
 243         static
 244         void debug(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 245         {
 246                 va_list ap;
 247                 char buf[256];
 248 
 249                 va_start(ap, fmt);
 250                   vsprintf(buf, fmt, ap);
 251                   printk(buf);
 252                 va_end(ap);
 253         }
 254 
 255         static
 256         void debug_config(enum aha_type type, struct aic7xxx_host_config *p)
     /* [previous][next][first][last][top][bottom][index][help] */
 257         {
 258                 int ioport2, ioport3;
 259 
 260                 static char *BRT[T_MAX][16] = {
 261                         { },                                    /* T_NONE */
 262                         {
 263                                 "2",   "???", "???", "12",      /* T_274X */
 264                                 "???", "???", "???", "28",
 265                                 "???", "???", "???", "44",
 266                                 "???", "???", "???", "60"
 267                         },
 268                         {
 269                                 "2",  "4",  "8",  "12",         /* T_284X */
 270                                 "16", "20", "24", "28",
 271                                 "32", "36", "40", "44",
 272                                 "48", "52", "56", "60"
 273                         },
 274                         {
 275                                 "???", "???", "???", "???",     /* T_294X */
 276                                 "???", "???", "???", "???",
 277                                 "???", "???", "???", "???",
 278                                 "???", "???", "???", "???"
 279                         }
 280                 };
 281                 static int DFT[4] = {
 282                         0, 50, 75, 100
 283                 };
 284                 static int SST[4] = {
 285                         256, 128, 64, 32
 286                 };
 287 
 288                 ioport2 = inb(HA_HOSTCONF(p->base));
 289                 ioport3 = inb(HA_SCSICONF(p->base));
 290 
 291                 switch (type) {
 292                     case T_274X:
 293                         printk("AHA274X AT EISA SLOT %d:\n", p->base >> 12);
 294                         break;
 295                     case T_284X:
 296                         printk("AHA284X AT SLOT %d:\n", p->base >> 12);
 297                         break;
 298                     case T_294X:
 299                         printk("AHA294X (PCI-bus):\n");
 300                         break;
 301                     default:
 302                         panic("aic7xxx debug_config: internal error\n");
 303                 }
 304 
 305                 printk("    irq %d\n"
 306                        "    bus release time %s bclks\n"
 307                        "    data fifo threshold %d%%\n",
 308                        p->irq,
 309                        BRT[type][(ioport2 >> 2) & 0xf],
 310                        DFT[(ioport2 >> 6) & 0x3]);
 311 
 312                 printk("    SCSI CHANNEL A:\n"
 313                        "        scsi id %d\n"
 314                        "        scsi bus parity check %sabled\n"
 315                        "        scsi selection timeout %d ms\n"
 316                        "        scsi bus reset at power-on %sabled\n",
 317                        ioport3 & 0x7,
 318                        (ioport3 & 0x20) ? "en" : "dis",
 319                        SST[(ioport3 >> 3) & 0x3],
 320                        (ioport3 & 0x40) ? "en" : "dis");
 321 
 322                 if (type == T_274X) {
 323                         printk("        scsi bus termination %sabled\n",
 324                                (ioport3 & 0x80) ? "en" : "dis");
 325                 }
 326         }
 327 
 328         static
 329         void debug_rate(int base, int rate)
     /* [previous][next][first][last][top][bottom][index][help] */
 330         {
 331                 int target = inb(O_SCSIID(base)) >> 4;
 332 
 333                 if (rate) {
 334                         printk("aic7xxx: target %d now synchronous at %sMb/s\n",
 335                                target,
 336                                aic7xxx_synctab[(rate >> 4) & 0x7].english);
 337                 } else {
 338                         printk("aic7xxx: target %d using asynchronous mode\n",
 339                                target);
 340                 }
 341         }
 342 
 343 #else
 344 
 345 #       define debug(fmt, args...)
 346 #       define debug_config(x)
 347 #       define debug_rate(x,y)
 348 
 349 #endif AIC7XXX_DEBUG
 350 
 351 /*
 352  *  XXX - these options apply unilaterally to _all_ 274x/284x/294x
 353  *        cards in the system.  This should be fixed, but then,
 354  *        does anyone really have more than one in a machine?
 355  */
 356 static int aic7xxx_extended = 0;                /* extended translation on? */
 357 
 358 void aic7xxx_setup(char *s, int *dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 359 {
 360         int i;
 361         char *p;
 362 
 363         static struct {
 364                 char *name;
 365                 int *flag;
 366         } options[] = {
 367                 "extended",     &aic7xxx_extended,
 368                 NULL
 369         };
 370 
 371         for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
 372                 for (i = 0; options[i].name; i++)
 373                         if (!strcmp(options[i].name, p))
 374                                 *(options[i].flag) = !0;
 375         }
 376 }
 377 
 378 static
 379 void aic7xxx_getscb(int base, struct aic7xxx_scb *scb)
     /* [previous][next][first][last][top][bottom][index][help] */
 380 {
 381         /*
 382          *  This is almost identical to aic7xxx_putscb().
 383          */
 384         outb(0x80, O_SCBCNT(base));     /* SCBAUTO */
 385 
 386         asm volatile("cld\n\t"
 387                      "rep\n\t"
 388                      "insb"
 389                      : /* no output */
 390                      :"D" (scb), "c" (sizeof(*scb)), "d" (O_SCBARRAY(base))
 391                      :"di", "cx", "dx");
 392 
 393         outb(0, O_SCBCNT(base));
 394 }
 395 
 396 /*
 397  *  How much data should be transferred for this SCSI command?  Stop
 398  *  at segment sg_last if it's a scatter-gather command so we can
 399  *  compute underflow easily.
 400  */
 401 static
 402 unsigned aic7xxx_length(Scsi_Cmnd *cmd, int sg_last)
     /* [previous][next][first][last][top][bottom][index][help] */
 403 {
 404         int i, segments;
 405         unsigned length;
 406         struct scatterlist *sg;
 407 
 408         segments = cmd->use_sg - sg_last;
 409         sg = (struct scatterlist *)cmd->buffer;
 410 
 411         if (cmd->use_sg) {
 412                 for (i = length = 0;
 413                      i < cmd->use_sg && i < segments;
 414                      i++)
 415                 {
 416                         length += sg[i].length;
 417                 }
 418         } else
 419                 length = cmd->request_bufflen;
 420 
 421         return(length);
 422 }
 423 
 424 static
 425 void aic7xxx_sg_check(Scsi_Cmnd *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 426 {
 427         int i;
 428         struct scatterlist *sg = (struct scatterlist *)cmd->buffer;
 429 
 430         if (cmd->use_sg) {
 431                 for (i = 0; i < cmd->use_sg; i++)
 432                         if ((unsigned)sg[i].length > 0xffff)
 433                                 panic("aic7xxx_sg_check: s/g segment > 64k\n");
 434         }
 435 }
 436 
 437 static
 438 void aic7xxx_to_scsirate(unsigned char *rate,
     /* [previous][next][first][last][top][bottom][index][help] */
 439                          unsigned char transfer,
 440                          unsigned char offset)
 441 {
 442         int i;
 443 
 444         transfer *= 4;
 445 
 446         for (i = 0; i < aic7xxx_synctab_max-1; i++) {
 447 
 448                 if (transfer == aic7xxx_synctab[i].period) {
 449                         *rate = (aic7xxx_synctab[i].rate << 4) | (offset & 0xf);
 450                         return;
 451                 }
 452 
 453                 if (transfer > aic7xxx_synctab[i].period &&
 454                     transfer < aic7xxx_synctab[i+1].period)
 455                 {
 456                         *rate = (aic7xxx_synctab[i+1].rate << 4) |
 457                                 (offset & 0xf);
 458                         return;
 459                 }
 460         }
 461         *rate = 0;
 462 }
 463 
 464 /*
 465  *  Pause the sequencer and wait for it to actually stop - this
 466  *  is important since the sequencer can disable pausing for critical
 467  *  sections.
 468  */
 469 #define PAUSE_SEQUENCER(p)      \
 470         do {                                                            \
 471                 outb(0xe, O_HCNTRL(p->base));   /* IRQMS|PAUSE|INTEN */ \
 472                                                                         \
 473                 while ((inb(O_HCNTRL(p->base)) & 0x4) == 0)             \
 474                         ;                                               \
 475         } while (0)
 476 
 477 /*
 478  *  Unpause the sequencer.  Unremarkable, yet done often enough to
 479  *  warrant an easy way to do it.
 480  */
 481 #define UNPAUSE_SEQUENCER(p)    \
 482         outb(p->unpause, O_HCNTRL(p->base))     /* IRQMS|INTEN */
 483 
 484 /*
 485  *  See comments in aic7xxx_loadram() wrt this.
 486  */
 487 #define RESTART_SEQUENCER(p)    \
 488         do {                                                    \
 489                 do {                                            \
 490                         outb(0x2, O_SEQCTL(p->base));           \
 491                                                                 \
 492                 } while (inb(O_SEQADDR(p->base)) != 0 &&        \
 493                          inb(O_SEQADDR(p->base) + 1) != 0);     \
 494                                                                 \
 495                 UNPAUSE_SEQUENCER(p);                           \
 496         } while (0)
 497 
 498 /*
 499  *  Since we declared this using SA_INTERRUPT, interrupts should
 500  *  be disabled all through this function unless we say otherwise.
 501  */
 502 static
 503 void aic7xxx_isr(int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 504 {
 505         int base, intstat;
 506         struct aic7xxx_host *p;
 507         
 508         p = (struct aic7xxx_host *)aic7xxx_boards[irq]->hostdata;
 509         base = p->base;
 510 
 511         /*
 512          *  Check the startup flag - if no commands have been queued,
 513          *  we probably have the interrupt type set wrong.  Reverse
 514          *  the stored value and the active one in the host control
 515          *  register.
 516          */
 517         if (p->startup) {
 518                 p->unpause ^= 0x8;
 519                 outb(inb(O_HCNTRL(p->base)) ^ 0x8, O_HCNTRL(p->base));
 520                 return;
 521         }
 522 
 523         /*
 524          *  Handle all the interrupt sources - especially for SCSI
 525          *  interrupts, we won't get a second chance at them.
 526          */
 527         intstat = inb(O_INTSTAT(base));
 528 
 529         if (intstat & 0x8) {                            /* BRKADRINT */
 530 
 531                 panic("aic7xxx_isr: brkadrint, error = 0x%x, seqaddr = 0x%x\n",
 532                       inb(O_ERROR(base)), inw(O_SEQADDR(base)));
 533         }
 534 
 535         if (intstat & 0x4) {                            /* SCSIINT */
 536 
 537                 int scbptr = inb(O_SCBPTR(base));
 538                 int status = inb(O_SSTAT1(base));
 539                 Scsi_Cmnd *cmd;
 540 
 541                 cmd = (Scsi_Cmnd *)p->SCB_array[scbptr];
 542                 if (!cmd) {
 543                         printk("aic7xxx_isr: no command for scb (scsiint)\n");
 544                         /*
 545                          *  Turn off the interrupt and set status
 546                          *  to zero, so that it falls through the
 547                          *  reset of the SCSIINT code.
 548                          */
 549                         outb(status, O_CLRSINT1(base));
 550                         UNPAUSE_SEQUENCER(p);
 551                         outb(0x4, O_CLRINT(base));      /* undocumented */
 552                         status = 0;
 553                 }
 554                 p->SCB_array[scbptr] = NULL;
 555 
 556                 /*
 557                  *  Only the SCSI Status 1 register has information
 558                  *  about exceptional conditions that we'd have a
 559                  *  SCSIINT about; anything in SSTAT0 will be handled
 560                  *  by the sequencer.  Note that there can be multiple
 561                  *  bits set.
 562                  */
 563                 if (status & 0x80) {                    /* SELTO */
 564                         /*
 565                          *  Hardware selection timer has expired.  Turn
 566                          *  off SCSI selection sequence.
 567                          */
 568                         outb(0, O_SCSISEQ(base));
 569                         cmd->result = DID_TIME_OUT << 16;
 570 
 571                         /*
 572                          *  If there's an active message, it belongs to the
 573                          *  command that is getting punted - remove it.
 574                          */
 575                         outb(0, HA_MSG_FLAGS(base));
 576 
 577                         /*
 578                          *  Shut off the offending interrupt sources, reset
 579                          *  the sequencer address to zero and unpause it,
 580                          *  then call the high-level SCSI completion routine.
 581                          *
 582                          *  WARNING!  This is a magic sequence!  After many
 583                          *  hours of guesswork, turning off the SCSI interrupts
 584                          *  in CLRSINT? does NOT clear the SCSIINT bit in
 585                          *  INTSTAT.  By writing to the (undocumented, unused
 586                          *  according to the AIC-7770 manual) third bit of
 587                          *  CLRINT, you can clear INTSTAT.  But, if you do it
 588                          *  while the sequencer is paused, you get a BRKADRINT
 589                          *  with an Illegal Host Address status, so the
 590                          *  sequencer has to be restarted first.
 591                          */
 592                         outb(0x80, O_CLRSINT1(base));   /* CLRSELTIMO */
 593                         RESTART_SEQUENCER(p);
 594 
 595                         outb(0x4, O_CLRINT(base));      /* undocumented */
 596                         cmd->scsi_done(cmd);
 597                 }
 598 
 599                 if (status & 0x4) {                     /* SCSIPERR */
 600                         /*
 601                          *  A parity error has occurred during a data
 602                          *  transfer phase.  Flag it and continue.
 603                          */
 604                         printk("aic7xxx: parity error on target %d, lun %d\n",
 605                                cmd->target,
 606                                cmd->lun);
 607                         aic7xxx_parity(cmd) = DID_PARITY;
 608 
 609                         /*
 610                          *  Clear interrupt and resume as above.
 611                          */
 612                         outb(0x4, O_CLRSINT1(base));    /* CLRSCSIPERR */
 613                         UNPAUSE_SEQUENCER(p);
 614 
 615                         outb(0x4, O_CLRINT(base));      /* undocumented */
 616                 }
 617 
 618                 if ((status & (0x8|0x4)) == 0 && status) {
 619                         /*
 620                          *  We don't know what's going on.  Turn off the
 621                          *  interrupt source and try to continue.
 622                          */
 623                         printk("aic7xxx_isr: sstat1 = 0x%x\n", status);
 624                         outb(status, O_CLRSINT1(base));
 625                         UNPAUSE_SEQUENCER(p);
 626                         outb(0x4, O_CLRINT(base));      /* undocumented */
 627                 }
 628         }
 629 
 630         if (intstat & 0x2) {                            /* CMDCMPLT */
 631 
 632                 int complete, old_scbptr;
 633                 struct aic7xxx_scb scb;
 634                 unsigned actual;
 635                 Scsi_Cmnd *cmd;
 636 
 637                 /*
 638                  *  The sequencer will continue running when it
 639                  *  issues this interrupt.  There may be >1 commands
 640                  *  finished, so loop until we've processed them all.
 641                  */
 642                 do {
 643                         complete = inb(O_QOUTFIFO(base));
 644 
 645                         cmd = (Scsi_Cmnd *)p->SCB_array[complete];
 646                         if (!cmd) {
 647                                 printk("aic7xxx warning: "
 648                                        "no command for scb (cmdcmplt)\n");
 649                                 continue;
 650                         }
 651                         p->SCB_array[complete] = NULL;
 652                         
 653                         PAUSE_SEQUENCER(p);
 654 
 655                         /*
 656                          *  After pausing the sequencer (and waiting
 657                          *  for it to stop), save its SCB pointer, then
 658                          *  write in our completed one and read the SCB
 659                          *  registers.  Afterwards, restore the saved
 660                          *  pointer, unpause the sequencer and call the
 661                          *  higher-level completion function - unpause
 662                          *  first since we have no idea how long done()
 663                          *  will take.
 664                          */
 665                         old_scbptr = inb(O_SCBPTR(base));
 666                         outb(complete, O_SCBPTR(base));
 667 
 668                         aic7xxx_getscb(base, &scb);
 669                         outb(old_scbptr, O_SCBPTR(base));
 670 
 671                         UNPAUSE_SEQUENCER(p);
 672 
 673                         cmd->result = scb.target_status |
 674                                      (aic7xxx_parity(cmd) << 16);
 675 
 676                         /*
 677                          *  Did we underflow?  At this time, there's only
 678                          *  one other driver that bothers to check for this,
 679                          *  and cmd->underflow seems to be set rather half-
 680                          *  heartedly in the higher-level SCSI code.
 681                          */
 682                         actual = aic7xxx_length(cmd,
 683                                                 scb.residual_SG_segment_count);
 684 
 685                         actual -= ((scb.residual_data_count[2] << 16) |
 686                                    (scb.residual_data_count[1] <<  8) |
 687                                    (scb.residual_data_count[0]));
 688 
 689                         if (actual < cmd->underflow) {
 690                                 printk("aic7xxx: target %d underflow - "
 691                                        "wanted (at least) %u, got %u\n",
 692                                        cmd->target, cmd->underflow, actual);
 693 
 694                                 cmd->result = scb.target_status |
 695                                              (DID_UNDERFLOW << 16);
 696                         }
 697 
 698                         cmd->scsi_done(cmd);
 699 
 700                         /*
 701                          *  Clear interrupt status before checking
 702                          *  the output queue again.  This eliminates
 703                          *  a race condition whereby a command could
 704                          *  complete between the queue poll and the
 705                          *  interrupt clearing, so notification of the
 706                          *  command being complete never made it back
 707                          *  up to the kernel.
 708                          */
 709                         outb(0x2, O_CLRINT(base));      /* CLRCMDINT */
 710 
 711                 } while (inb(O_QOUTCNT(base)));
 712         }
 713 
 714         if (intstat & 0x1) {                            /* SEQINT */
 715 
 716                 unsigned char transfer, offset, rate;
 717 
 718                 /*
 719                  *  Although the sequencer is paused immediately on
 720                  *  a SEQINT, an interrupt for a SCSIINT or a CMDCMPLT
 721                  *  condition will have unpaused the sequencer before
 722                  *  this point.
 723                  */
 724                 PAUSE_SEQUENCER(p);
 725 
 726                 switch (intstat & 0xf0) {
 727                     case 0x00:
 728                         panic("aic7xxx_isr: unknown scsi bus phase\n");
 729                     case 0x10:
 730                         debug("aic7xxx_isr warning: "
 731                               "issuing message reject, 1st byte 0x%x\n",
 732                               inb(HA_REJBYTE(base)));
 733                         break;
 734                     case 0x20:
 735                         panic("aic7xxx_isr: reconnecting target %d "
 736                               "didn't issue IDENTIFY message\n",
 737                               (inb(O_SELID(base)) >> 4) & 0xf);
 738                     case 0x30:
 739                         debug("aic7xxx_isr: sequencer couldn't find match "
 740                               "for reconnecting target %d - issuing ABORT\n",
 741                               (inb(O_SELID(base)) >> 4) & 0xf);
 742                         break;
 743                     case 0x40:
 744                         transfer = inb(HA_ARG_1(base));
 745                         offset = inb(HA_ARG_2(base));
 746                         aic7xxx_to_scsirate(&rate, transfer, offset);
 747                         outb(rate, HA_RETURN_1(base));
 748                         debug_rate(base, rate);
 749                         break;
 750                     default:
 751                         debug("aic7xxx_isr: seqint, "
 752                               "intstat = 0x%x, scsisigi = 0x%x\n",
 753                               intstat, inb(O_SCSISIGI(base)));
 754                         break;
 755                 }
 756 
 757                 outb(0x1, O_CLRINT(base));              /* CLRSEQINT */
 758                 UNPAUSE_SEQUENCER(p);
 759         }
 760 }
 761 
 762 /*
 763  *  Probing for EISA boards: it looks like the first two bytes
 764  *  are a manufacturer code - three characters, five bits each:
 765  *
 766  *               BYTE 0   BYTE 1   BYTE 2   BYTE 3
 767  *              ?1111122 22233333 PPPPPPPP RRRRRRRR
 768  *
 769  *  The characters are baselined off ASCII '@', so add that value
 770  *  to each to get the real ASCII code for it.  The next two bytes
 771  *  appear to be a product and revision number, probably vendor-
 772  *  specific.  This is what is being searched for at each port,
 773  *  and what should probably correspond to the ID= field in the
 774  *  ECU's .cfg file for the card - if your card is not detected,
 775  *  make sure your signature is listed in the array.
 776  *
 777  *  The fourth byte's lowest bit seems to be an enabled/disabled
 778  *  flag (rest of the bits are reserved?).
 779  */
 780 
 781 static
 782 enum aha_type aic7xxx_probe(int slot, int s_base)
     /* [previous][next][first][last][top][bottom][index][help] */
 783 {
 784         int i;
 785         unsigned char buf[4];
 786 
 787         static struct {
 788                 int n;
 789                 unsigned char signature[sizeof(buf)];
 790                 enum aha_type type;
 791         } S[] = {
 792                 4, { 0x04, 0x90, 0x77, 0x71 }, T_274X,  /* host adapter 274x */
 793                 4, { 0x04, 0x90, 0x77, 0x70 }, T_274X,  /* motherboard 274x  */
 794                 4, { 0x04, 0x90, 0x77, 0x56 }, T_284X,  /* 284x, BIOS enabled */
 795         };
 796 
 797         for (i = 0; i < sizeof(buf); i++) {
 798                 /*
 799                  *  The VL-bus cards need to be primed by
 800                  *  writing before a signature check.
 801                  */
 802                 outb(0x80 + i, s_base);
 803                 buf[i] = inb(s_base + i);
 804         }
 805 
 806         for (i = 0; i < sizeof(S)/sizeof(S[0]); i++) {
 807                 if (!memcmp(buf, S[i].signature, S[i].n)) {
 808                         /*
 809                          *  Signature match on enabled card?
 810                          */
 811                         if (inb(s_base + 4) & 1)
 812                                 return(S[i].type);
 813                         printk("aic7xxx disabled at slot %d, ignored\n", slot);
 814                 }
 815         }
 816         return(T_NONE);
 817 }
 818 
 819 /*
 820  *  Return ' ' for plain 274x, 'T' for twin-channel, 'W' for
 821  *  wide channel, '?' for anything else.
 822  */
 823 
 824 static
 825 char aic7xxx_type(int base)
     /* [previous][next][first][last][top][bottom][index][help] */
 826 {
 827         /*
 828          *  AIC-7770/7870s can be wired so that, on chip reset,
 829          *  the SCSI Block Control register indicates how many
 830          *  busses the chip is configured for.  The two high bits
 831          *  set indicate a 294x.
 832          */
 833         switch (inb(O_SBLKCTL(base))) {
 834             case 0:
 835             case 0xc0:
 836                 return(' ');
 837             case 2:
 838             case 0xc2:
 839                 return('W');
 840             case 8:
 841                 return('T');
 842             default:
 843                 printk("aic7xxx has unknown bus configuration\n");
 844                 return('?');
 845         }
 846 }
 847 
 848 static
 849 void aic7xxx_loadram(int base)
     /* [previous][next][first][last][top][bottom][index][help] */
 850 {
 851         static unsigned char seqprog[] = {
 852                 /*
 853                  *  Each sequencer instruction is 29 bits
 854                  *  long (fill in the excess with zeroes)
 855                  *  and has to be loaded from least -> most
 856                  *  significant byte, so this table has the
 857                  *  byte ordering reversed.
 858                  */
 859 #               include "aic7xxx_seq.h"
 860         };
 861 
 862         /*
 863          *  When the AIC-7770 is paused (as on chip reset), the
 864          *  sequencer address can be altered and a sequencer
 865          *  program can be loaded by writing it, byte by byte, to
 866          *  the sequencer RAM port - the Adaptec documentation
 867          *  recommends using REP OUTSB to do this, hence the inline
 868          *  assembly.  Since the address autoincrements as we load
 869          *  the program, reset it back to zero afterward.  Disable
 870          *  sequencer RAM parity error detection while loading, and
 871          *  make sure the LOADRAM bit is enabled for loading.
 872          */
 873         outb(0x83, O_SEQCTL(base));     /* PERRORDIS|SEQRESET|LOADRAM */
 874 
 875         asm volatile("cld\n\t"
 876                      "rep\n\t"
 877                      "outsb"
 878                      : /* no output */
 879                      :"S" (seqprog), "c" (sizeof(seqprog)), "d" (O_SEQRAM(base))
 880                      :"si", "cx", "dx");
 881 
 882         /*
 883          *  WARNING!  This is a magic sequence!  After extensive
 884          *  experimentation, it seems that you MUST turn off the
 885          *  LOADRAM bit before you play with SEQADDR again, else
 886          *  you will end up with parity errors being flagged on
 887          *  your sequencer program.  (You would also think that
 888          *  turning off LOADRAM and setting SEQRESET to reset the
 889          *  address to zero would work, but you need to do it twice
 890          *  for it to take effect on the address.  Timing problem?)
 891          */
 892         outb(0, O_SEQCTL(base));
 893         do {
 894                 /*
 895                  *  Actually, reset it until
 896                  *  the address shows up as
 897                  *  zero just to be safe..
 898                  */
 899                 outb(0x2, O_SEQCTL(base));      /* SEQRESET */
 900 
 901         } while (inb(O_SEQADDR(base)) != 0 && inb(O_SEQADDR(base) + 1) != 0);
 902 }
 903 
 904 static
 905 void aha274x_config(struct aic7xxx_host_config *p, va_list ap)
     /* [previous][next][first][last][top][bottom][index][help] */
 906 {
 907         int base = va_arg(ap, int);
 908 
 909         /*
 910          *  Give the AIC-7770 a reset - reading the 274x's registers
 911          *  returns zeroes unless you do.  This forces a pause of the
 912          *  Sequencer.
 913          */
 914         outb(1, O_HCNTRL(base));        /* CHIPRST */
 915 
 916         p->base = base;
 917         p->irq = inb(HA_INTDEF(base)) & 0xf;
 918         p->scsi_id = inb(HA_SCSICONF(base)) & 0x7;
 919 
 920         /*
 921          *  This value for HCNTRL may be changed in the ISR if we
 922          *  catch a spurious interrupt right away.
 923          */
 924         p->unpause = 0xa;
 925 
 926         /*
 927          *  XXX - these are values that I don't know how to query
 928          *        the hardware for.  Apparently some revision E
 929          *        '7770s can have more SCBs, and I don't know how
 930          *        to get the "extended translation" flag from the
 931          *        EISA data area.
 932          */
 933         p->maxscb = 4;
 934         p->extended = aic7xxx_extended;
 935 
 936         /*
 937          *  A reminder until this can be detected automatically.
 938          */
 939         printk("aha274x: extended translation %sabled\n",
 940                 p->extended ? "en" : "dis");
 941 }
 942 
 943 static
 944 void aha284x_config(struct aic7xxx_host_config *p, va_list ap)
     /* [previous][next][first][last][top][bottom][index][help] */
 945 {
 946         int base = va_arg(ap, int);
 947 
 948         /*
 949          *  Give the AIC-7770 a reset - this forces a pause of the
 950          *  Sequencer and returns everything to default values.
 951          */
 952         outb(1, O_HCNTRL(base));        /* CHIPRST */
 953 
 954         p->base = base;
 955         p->unpause = 0x2;
 956         p->irq = inb(HA_INTDEF(base)) & 0xf;
 957         p->scsi_id = inb(HA_SCSICONF(base)) & 0x7;
 958 
 959         /*
 960          *  XXX - these are values that I don't know how to query
 961          *        the hardware for.  Apparently some revision E
 962          *        '7770s can have more SCBs, and I don't know how
 963          *        to get the "extended translation" flag from the
 964          *        onboard memory.
 965          */
 966         p->maxscb = 4;
 967         p->extended = aic7xxx_extended;
 968 
 969         /*
 970          *  A reminder until this can be detected automatically.
 971          */
 972         printk("aha284x: extended translation %sabled\n",
 973                 p->extended ? "en" : "dis");
 974 }
 975 
 976 static
 977 void aha294x_config(struct aic7xxx_host_config *p, va_list ap)
     /* [previous][next][first][last][top][bottom][index][help] */
 978 {
 979         int error;
 980         unsigned long io_port;
 981         unsigned char bus, device_fn, irq;
 982 
 983         bus = va_arg(ap, unsigned char);
 984         device_fn = va_arg(ap, unsigned char);
 985 
 986         /*
 987          *  Read esundry information from PCI BIOS.
 988          */
 989         error = pcibios_read_config_dword(bus,
 990                                           device_fn,
 991                                           PCI_BASE_ADDRESS_0,
 992                                           &io_port);
 993         if (error) {
 994                 panic("aha294x_config: error %s reading i/o port\n",
 995                       pcibios_strerror(error));
 996         }
 997 
 998         error = pcibios_read_config_byte(bus,
 999                                          device_fn,
1000                                          PCI_INTERRUPT_LINE,
1001                                          &irq);
1002         if (error) {
1003                 panic("aha294x_config: error %s reading irq\n",
1004                       pcibios_strerror(error));
1005         }
1006 
1007         /*
1008          *  Make the base I/O register look like EISA and VL-bus.
1009          */
1010         p->base = io_port - 0xc01;
1011 
1012         /*
1013          *  Give the AIC-7870 a reset - this forces a pause of the
1014          *  Sequencer and returns everything to default values.
1015          */
1016         outb(1, O_HCNTRL(p->base));     /* CHIPRST */
1017 
1018         p->irq = irq;
1019         p->maxscb = 16;
1020         p->unpause = 0xa;
1021 
1022         /*
1023          *  XXX - these are values that I don't know how to query
1024          *        the hardware for, so for now the SCSI host ID is
1025          *        hardwired to 7, and the "extended translation"
1026          *        flag is taken from boot-time flags.
1027          */
1028         p->scsi_id = 7;
1029         p->extended = aic7xxx_extended;
1030 
1031         /*
1032          *  XXX - force data fifo threshold to 100%.  Why does this
1033          *        need to be done?
1034          */
1035 #       define  DFTHRESH        3
1036 
1037         outb(DFTHRESH << 6, O_DSPCISTATUS(p->base));
1038         outb(p->scsi_id | (DFTHRESH << 6), HA_SCSICONF(p->base));
1039 
1040         /*
1041          *  A reminder until this can be detected automatically.
1042          */
1043         printk("aha294x: extended translation %sabled\n",
1044                 p->extended ? "en" : "dis");
1045 }
1046 
1047 static
1048 int aic7xxx_register(Scsi_Host_Template *template, enum aha_type type, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
1049 {
1050         va_list ap;
1051         int i, base;
1052         struct Scsi_Host *host;
1053         struct aic7xxx_host *p;
1054         struct aic7xxx_host_config config;
1055 
1056         va_start(ap, type);
1057         
1058         switch (type) {
1059             case T_274X:
1060                 aha274x_config(&config, ap);
1061                 break;
1062             case T_284X:
1063                 aha284x_config(&config, ap);
1064                 break;
1065             case T_294X:
1066                 aha294x_config(&config, ap);
1067                 break;
1068             default:
1069                 panic("aic7xxx_register: internal error\n");
1070         }
1071         va_end(ap);
1072 
1073         base = config.base;
1074 
1075         /*
1076          *  The IRQ level in i/o port 4 maps directly onto the real
1077          *  IRQ number.  If it's ok, register it with the kernel.
1078          *
1079          *  NB. the Adaptec documentation says the IRQ number is only
1080          *      in the lower four bits; the ECU information shows the
1081          *      high bit being used as well.  Which is correct?
1082          */
1083         if (config.irq < 9 || config.irq > 15) {
1084                 printk("aic7xxx uses unsupported IRQ level, ignoring\n");
1085                 return(0);
1086         }
1087         
1088         /*
1089          *  Lock out other contenders for our i/o space.
1090          */
1091         snarf_region(O_MINREG(base), O_MAXREG(base)-O_MINREG(base));
1092 
1093         /*
1094          *  Any card-type-specific adjustments before we register
1095          *  the scsi host(s).
1096          */
1097         switch (aic7xxx_type(base)) {
1098             case 'T':
1099                 printk("aic7xxx warning: ignoring channel B of 274x-twin\n");
1100                 break;
1101             case ' ':
1102                 break;
1103             default:
1104                 printk("aic7xxx is an unsupported type, ignoring\n");
1105                 return(0);
1106         }
1107 
1108         /*
1109          *  Before registry, make sure that the offsets of the
1110          *  struct scatterlist are what the sequencer will expect,
1111          *  otherwise disable scatter-gather altogether until someone
1112          *  can fix it.  This is important since the sequencer will
1113          *  DMA elements of the SG array in while executing commands.
1114          */
1115         if (template->sg_tablesize != SG_NONE) {
1116                 struct scatterlist sg;
1117 
1118                 if (SG_STRUCT_CHECK(sg)) {
1119                         printk("aic7xxx warning: kernel scatter-gather "
1120                                "structures changed, disabling it\n");
1121                         template->sg_tablesize = SG_NONE;
1122                 }
1123         }
1124         
1125         /*
1126          *  Register each "host" and fill in the returned Scsi_Host
1127          *  structure as best we can.  Some of the parameters aren't
1128          *  really relevant for bus types beyond ISA, and none of the
1129          *  high-level SCSI code looks at it anyway.. why are the fields
1130          *  there?  Also save the pointer so that we can find the
1131          *  information when an IRQ is triggered.
1132          */
1133         host = scsi_register(template, sizeof(struct aic7xxx_host));
1134         host->can_queue = config.maxscb;
1135         host->this_id = config.scsi_id;
1136         host->irq = config.irq;
1137 
1138         aic7xxx_boards[config.irq] = host;
1139         
1140         p = (struct aic7xxx_host *)host->hostdata;
1141         for (i = 0; i < AIC7XXX_MAXSCB; i++)
1142                 p->SCB_array[i] = NULL;
1143 
1144         p->base = config.base;
1145         p->maxscb = config.maxscb;
1146         p->extended = config.extended;
1147 
1148         /*
1149          *  The interrupt trigger is different depending
1150          *  on whether the card is EISA or VL-bus - sometimes.
1151          *  The startup variable will be cleared once the first
1152          *  command is queued, and is checked in the isr to
1153          *  try and detect when the interrupt type is set
1154          *  incorrectly, triggering an interrupt immediately.
1155          *  This is now just set on a per-card-type basis.
1156          */
1157         p->unpause = config.unpause;
1158         p->startup = !0;
1159 
1160         /*
1161          *  Register IRQ with the kernel _after_ the host information
1162          *  is set up, in case we take an interrupt right away, due to
1163          *  the interrupt type being set wrong.
1164          */
1165         if (request_irq(config.irq, aic7xxx_isr, SA_INTERRUPT, "aic7xxx")) {
1166                 printk("aic7xxx couldn't register irq %d, ignoring\n",
1167                        config.irq);
1168                 return(0);
1169         }
1170 
1171         /*
1172          *  Print out debugging information before re-enabling
1173          *  the card - a lot of registers on it can't be read
1174          *  when the sequencer is active.
1175          */
1176 #ifdef AIC7XXX_DEBUG
1177         debug_config(type, &config);
1178 #endif
1179 
1180         /*
1181          *  Load the sequencer program, then re-enable the board -
1182          *  resetting the AIC-7770 disables it, leaving the lights
1183          *  on with nobody home.  On the PCI bus you *may* be home,
1184          *  but then your mailing address is dynamically assigned
1185          *  so no one can find you anyway :-)
1186          */
1187         aic7xxx_loadram(base);
1188         if (type != T_294X)
1189                 outb(1, O_BCTL(base));  /* ENABLE */
1190 
1191         /*
1192          *  Set the host adapter registers to indicate that synchronous
1193          *  negotiation should be attempted the first time the targets
1194          *  are communicated with.  Also initialize the active message
1195          *  flag to indicate that there is no message.
1196          */
1197         outb(0xff, HA_NEEDSDTR(base));
1198         outb(0, HA_MSG_FLAGS(base));
1199 
1200         /*
1201          *  For reconnecting targets, the sequencer code needs to
1202          *  know how many SCBs it has to search through.
1203          */
1204         outb(config.maxscb, HA_SCBCOUNT(base));
1205 
1206         /*
1207          *  Unpause the sequencer before returning and enable
1208          *  interrupts - we shouldn't get any until the first
1209          *  command is sent to us by the high-level SCSI code.
1210          */
1211         UNPAUSE_SEQUENCER(p);
1212         return(1);
1213 }
1214 
1215 int aic7xxx_detect(Scsi_Host_Template *template)
     /* [previous][next][first][last][top][bottom][index][help] */
1216 {
1217         enum aha_type type;
1218         int found = 0, slot, base;
1219 
1220         /*
1221          *  EISA/VL-bus card signature probe.
1222          */
1223         for (slot = MINSLOT; slot <= MAXSLOT; slot++) {
1224 
1225                 base = SLOTBASE(slot);
1226                 
1227                 if (check_region(O_MINREG(base),
1228                                  O_MAXREG(base)-O_MINREG(base)))
1229                 {
1230                         /*
1231                          *  Some other driver has staked a
1232                          *  claim to this i/o region already.
1233                          */
1234                         continue;
1235                 }
1236 
1237                 type = aic7xxx_probe(slot, O_BIDx(base));
1238 
1239                 if (type != T_NONE) {
1240                         /*
1241                          *  We "find" a 274x if we locate the card
1242                          *  signature and we can set it up and register
1243                          *  it with the kernel without incident.
1244                          */
1245                         found += aic7xxx_register(template, type, base);
1246                 }
1247         }
1248 
1249         /*
1250          *  PCI-bus probe.
1251          */
1252         if (pcibios_present()) {
1253                 int index = 0;
1254                 unsigned char bus, device_fn;
1255 
1256                 while (!pcibios_find_device(PCI_VENDOR_ID_ADAPTEC,
1257                                             PCI_DEVICE_ID_ADAPTEC_2940,
1258                                             index,
1259                                             &bus,
1260                                             &device_fn))
1261                 {
1262                         found += aic7xxx_register(template, T_294X,
1263                                                   bus, device_fn);
1264                         index += 1;
1265                 }
1266         }
1267 
1268         template->name = (char *)aic7xxx_info(NULL);
1269         return(found);
1270 }
1271 
1272 const char *aic7xxx_info(struct Scsi_Host *notused)
     /* [previous][next][first][last][top][bottom][index][help] */
1273 {
1274         return("Adaptec AHA274x/284x/294x (EISA/VL-bus/PCI -> Fast SCSI) "
1275                AIC7XXX_SEQ_VERSION "/"
1276                AIC7XXX_H_VERSION "/"
1277                "1.34");
1278 }
1279 
1280 static
1281 void aic7xxx_buildscb(struct aic7xxx_host *p,
     /* [previous][next][first][last][top][bottom][index][help] */
1282                       Scsi_Cmnd *cmd,
1283                       struct aic7xxx_scb *scb)
1284 {
1285         void *addr;
1286         unsigned length;
1287 
1288         memset(scb, 0, sizeof(*scb));
1289 
1290         /*
1291          *  NB. channel selection (bit 3) is always zero.
1292          */
1293         scb->target_channel_lun = ((cmd->target << 4) & 0xf0) |
1294                                    (cmd->lun & 0x7);
1295 
1296         /*
1297          *  The interpretation of request_buffer and request_bufflen
1298          *  changes depending on whether or not use_sg is zero; a
1299          *  non-zero use_sg indicates the number of elements in the
1300          *  scatter-gather array.
1301          *
1302          *  The AIC-7770 can't support transfers of any sort larger
1303          *  than 2^24 (three-byte count) without backflips.  For what
1304          *  the kernel is doing, this shouldn't occur.  I hope.
1305          */
1306         length = aic7xxx_length(cmd, 0);
1307 
1308         /*
1309          *  The sequencer code cannot yet handle scatter-gather segments
1310          *  larger than 64k (two-byte length).  The 1.1.x kernels, however,
1311          *  have a four-byte length field in the struct scatterlist, so
1312          *  make sure we don't exceed 64k on these kernels for now.
1313          */
1314         aic7xxx_sg_check(cmd);
1315 
1316         if (length > 0xffffff) {
1317                 panic("aic7xxx_buildscb: can't transfer > 2^24 - 1 bytes\n");
1318         }
1319 
1320         /*
1321          *  XXX - this relies on the host data being stored in a
1322          *        little-endian format.
1323          */
1324         addr = cmd->cmnd;
1325         scb->SCSI_cmd_length = cmd->cmd_len;
1326         memcpy(scb->SCSI_cmd_pointer, &addr, sizeof(scb->SCSI_cmd_pointer));
1327 
1328         if (cmd->use_sg) {
1329 #if 0
1330                 debug("aic7xxx_buildscb: SG used, %d segments, length %u\n",
1331                       cmd->use_sg,
1332                       length);
1333 #endif
1334                 scb->SG_segment_count = cmd->use_sg;
1335                 memcpy(scb->SG_list_pointer,
1336                        &cmd->request_buffer,
1337                        sizeof(scb->SG_list_pointer));
1338         } else {
1339                 scb->SG_segment_count = 0;
1340                 memcpy(scb->data_pointer,
1341                        &cmd->request_buffer,
1342                        sizeof(scb->data_pointer));
1343                 memcpy(scb->data_count,
1344                        &cmd->request_bufflen,
1345                        sizeof(scb->data_count));
1346         }
1347 }
1348 
1349 static
1350 void aic7xxx_putscb(int base, struct aic7xxx_scb *scb)
     /* [previous][next][first][last][top][bottom][index][help] */
1351 {
1352         /*
1353          *  By turning on the SCB auto increment, any reference
1354          *  to the SCB I/O space postincrements the SCB address
1355          *  we're looking at.  So turn this on and dump the relevant
1356          *  portion of the SCB to the card.
1357          */
1358         outb(0x80, O_SCBCNT(base));     /* SCBAUTO */
1359 
1360         asm volatile("cld\n\t"
1361                      "rep\n\t"
1362                      "outsb"
1363                      : /* no output */
1364                      :"S" (scb), "c" (sizeof(*scb)), "d" (O_SCBARRAY(base))
1365                      :"si", "cx", "dx");
1366 
1367         outb(0, O_SCBCNT(base));
1368 }
1369 
1370 int aic7xxx_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
1371 {
1372         long flags;
1373         int empty, old_scbptr;
1374         struct aic7xxx_host *p;
1375         struct aic7xxx_scb scb;
1376 
1377 #if 0
1378         debug("aic7xxx_queue: cmd 0x%x (size %u), target %d, lun %d\n",
1379               cmd->cmnd[0],
1380               cmd->cmd_len,
1381               cmd->target,
1382               cmd->lun);
1383 #endif
1384 
1385         p = (struct aic7xxx_host *)cmd->host->hostdata;
1386 
1387         /*
1388          *  Construct the SCB beforehand, so the sequencer is
1389          *  paused a minimal amount of time.
1390          */
1391         aic7xxx_buildscb(p, cmd, &scb);
1392 
1393         /*
1394          *  Clear the startup flag - we can now legitimately
1395          *  expect interrupts.
1396          */
1397         p->startup = 0;
1398 
1399         /*
1400          *  This is a critical section, since we don't want the
1401          *  interrupt routine mucking with the host data or the
1402          *  card.  Since the kernel documentation is vague on
1403          *  whether or not we are in a cli/sti pair already, save
1404          *  the flags to be on the safe side.
1405          */
1406         save_flags(flags);
1407         cli();
1408 
1409         /*
1410          *  Find a free slot in the SCB array to load this command
1411          *  into.  Since can_queue is set to the maximum number of
1412          *  SCBs for the card, we should always find one.
1413          */
1414         for (empty = 0; empty < p->maxscb; empty++)
1415                 if (!p->SCB_array[empty])
1416                         break;
1417         if (empty == p->maxscb)
1418                 panic("aic7xxx_queue: couldn't find a free scb\n");
1419 
1420         /*
1421          *  Pause the sequencer so we can play with its registers -
1422          *  wait for it to acknowledge the pause.
1423          *
1424          *  XXX - should the interrupts be left on while doing this?
1425          */
1426         PAUSE_SEQUENCER(p);
1427 
1428         /*
1429          *  Save the SCB pointer and put our own pointer in - this
1430          *  selects one of the four banks of SCB registers.  Load
1431          *  the SCB, then write its pointer into the queue in FIFO
1432          *  and restore the saved SCB pointer.
1433          */
1434         old_scbptr = inb(O_SCBPTR(p->base));
1435         outb(empty, O_SCBPTR(p->base));
1436         
1437         aic7xxx_putscb(p->base, &scb);
1438 
1439         outb(empty, O_QINFIFO(p->base));
1440         outb(old_scbptr, O_SCBPTR(p->base));
1441 
1442         /*
1443          *  Make sure the Scsi_Cmnd pointer is saved, the struct it
1444          *  points to is set up properly, and the parity error flag
1445          *  is reset, then unpause the sequencer and watch the fun
1446          *  begin.
1447          */
1448         cmd->scsi_done = fn;
1449         p->SCB_array[empty] = cmd;
1450         aic7xxx_parity(cmd) = DID_OK;
1451 
1452         UNPAUSE_SEQUENCER(p);
1453 
1454         restore_flags(flags);
1455         return(0);
1456 }
1457 
1458 /* return values from aic7xxx_kill */
1459 
1460 enum k_state {
1461         k_ok,                           /* scb found and message sent */
1462         k_busy,                         /* message already present */
1463         k_absent,                       /* couldn't locate scb */
1464         k_disconnect,                   /* scb found, but disconnected */
1465 };
1466 
1467 /*
1468  *  This must be called with interrupts disabled - it's going to
1469  *  be messing around with the host data, and an interrupt being
1470  *  fielded in the middle could get ugly.
1471  *
1472  *  Since so much of the abort and reset code is shared, this
1473  *  function performs more magic than it really should.  If the
1474  *  command completes ok, then it will call scsi_done with the
1475  *  result code passed in.  The unpause parameter controls whether
1476  *  or not the sequencer gets unpaused - the reset function, for
1477  *  instance, may want to do something more aggressive.
1478  *
1479  *  Note that the command is checked for in our SCB_array first
1480  *  before the sequencer is paused, so if k_absent is returned,
1481  *  then the sequencer is NOT paused.
1482  */
1483 
1484 static
1485 enum k_state aic7xxx_kill(Scsi_Cmnd *cmd, unsigned char message,
     /* [previous][next][first][last][top][bottom][index][help] */
1486                           unsigned int result, int unpause)
1487 {
1488         struct aic7xxx_host *p;
1489         int i, scb, found, queued;
1490         unsigned char scbsave[AIC7XXX_MAXSCB];
1491 
1492         p = (struct aic7xxx_host *)cmd->host->hostdata;
1493 
1494         /*
1495          *  If we can't find the command, assume it just completed
1496          *  and shrug it away.
1497          */
1498         for (scb = 0; scb < p->maxscb; scb++)
1499                 if (p->SCB_array[scb] == cmd)
1500                         break;
1501 
1502         if (scb == p->maxscb)
1503                 return(k_absent);
1504 
1505         PAUSE_SEQUENCER(p);
1506 
1507         /*
1508          *  This is the best case, really.  Check to see if the
1509          *  command is still in the sequencer's input queue.  If
1510          *  so, simply remove it.  Reload the queue afterward.
1511          */
1512         queued = inb(O_QINCNT(p->base));
1513         
1514         for (i = found = 0; i < queued; i++) {
1515                 scbsave[i] = inb(O_QINFIFO(p->base));
1516 
1517                 if (scbsave[i] == scb) {
1518                         found = 1;
1519                         i -= 1;
1520                 }
1521         }
1522 
1523         queued -= found;
1524         for (i = 0; i < queued; i++)
1525                 outb(scbsave[i], O_QINFIFO(p->base));
1526 
1527         if (found)
1528                 goto complete;
1529 
1530         /*
1531          *  Check the current SCB bank.  If it's not the one belonging
1532          *  to the command we want to kill, assume that the command
1533          *  is disconnected.  It's rather a pain to force a reconnect
1534          *  and send a message to the target, so we abdicate responsibility
1535          *  in this case.
1536          */
1537         if (inb(O_SCBPTR(p->base)) != scb) {
1538                 if (unpause)
1539                         UNPAUSE_SEQUENCER(p);
1540                 return(k_disconnect);
1541         }
1542 
1543         /*
1544          *  Presumably at this point our target command is active.  Check
1545          *  to see if there's a message already in effect.  If not, place
1546          *  our message in and assert ATN so the target goes into MESSAGE
1547          *  OUT phase.
1548          */
1549         if (inb(HA_MSG_FLAGS(p->base)) & 0x80) {
1550                 if (unpause)
1551                         UNPAUSE_SEQUENCER(p);
1552                 return(k_busy);
1553         }
1554 
1555         outb(0x80, HA_MSG_FLAGS(p->base));              /* active message */
1556         outb(1, HA_MSG_LEN(p->base));                   /* length = 1 */
1557         outb(message, HA_MSG_START(p->base));           /* message body */
1558 
1559         /*
1560          *  Assert ATN.  Use the value of SCSISIGO saved by the
1561          *  sequencer code so we don't alter its contents radically
1562          *  in the middle of something critical.
1563          */
1564         outb(inb(HA_SIGSTATE(p->base)) | 0x10, O_SCSISIGO(p->base));
1565 
1566         /*
1567          *  The command has been killed.  Do the bookkeeping, unpause
1568          *  the sequencer, and notify the higher-level SCSI code.
1569          */
1570 complete:
1571         p->SCB_array[scb] = NULL;
1572         if (unpause)
1573                 UNPAUSE_SEQUENCER(p);
1574 
1575         cmd->result = result << 16;
1576         cmd->scsi_done(cmd);
1577         return(k_ok);
1578 }
1579 
1580 int aic7xxx_abort(Scsi_Cmnd *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1581 {
1582         int rv;
1583         long flags;
1584 
1585         save_flags(flags);
1586         cli();
1587 
1588         switch (aic7xxx_kill(cmd, ABORT, DID_ABORT, !0)) {
1589             case k_ok:          rv = SCSI_ABORT_SUCCESS;        break;
1590             case k_busy:        rv = SCSI_ABORT_BUSY;           break;
1591             case k_absent:      rv = SCSI_ABORT_NOT_RUNNING;    break;
1592             case k_disconnect:  rv = SCSI_ABORT_SNOOZE;         break;
1593             default:
1594                 panic("aic7xxx_do_abort: internal error\n");
1595         }
1596 
1597         restore_flags(flags);
1598         return(rv);
1599 }
1600 
1601 /*
1602  *  Resetting the bus always succeeds - is has to, otherwise the
1603  *  kernel will panic!  Try a surgical technique - sending a BUS
1604  *  DEVICE RESET message - on the offending target before pulling
1605  *  the SCSI bus reset line.
1606  */
1607 
1608 int aic7xxx_reset(Scsi_Cmnd *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1609 {
1610         int i;
1611         long flags;
1612         Scsi_Cmnd *reset;
1613         struct aic7xxx_host *p;
1614 
1615         p = (struct aic7xxx_host *)cmd->host->hostdata;
1616         save_flags(flags);
1617         cli();
1618 
1619         switch (aic7xxx_kill(cmd, BUS_DEVICE_RESET, DID_RESET, 0)) {
1620 
1621             case k_ok:
1622                 /*
1623                  *  The RESET message was sent to the target
1624                  *  with no problems.  Flag that target as
1625                  *  needing a SDTR negotiation on the next
1626                  *  connection and restart the sequencer.
1627                  */
1628                 outb((1 << cmd->target), HA_NEEDSDTR(p->base));
1629                 UNPAUSE_SEQUENCER(p);
1630                 break;
1631 
1632             case k_absent:
1633                 /*
1634                  *  The sequencer will not be paused if aic7xxx_kill()
1635                  *  couldn't find the command.
1636                  */
1637                 PAUSE_SEQUENCER(p);
1638                 /* falls through */
1639 
1640             case k_busy:
1641             case k_disconnect:
1642                 /*
1643                  *  Do a hard reset of the SCSI bus.  According to the
1644                  *  SCSI-2 draft specification, reset has to be asserted
1645                  *  for at least 25us.  I'm invoking the kernel delay
1646                  *  function for 30us since I'm not totally trusting of
1647                  *  the busy loop timing.
1648                  *
1649                  *  XXX - I'm not convinced this works.  I tried resetting
1650                  *        the bus before, trying to get the devices on the
1651                  *        bus to revert to asynchronous transfer, and it
1652                  *        never seemed to work.
1653                  */
1654                 debug("aic7xxx: attempting to reset scsi bus and card\n");
1655 
1656                 outb(1, O_SCSISEQ(p->base));            /* SCSIRSTO */
1657                 udelay(30);
1658                 outb(0, O_SCSISEQ(p->base));            /* !SCSIRSTO */
1659 
1660                 outb(0xff, HA_NEEDSDTR(p->base));
1661                 UNPAUSE_SEQUENCER(p);
1662 
1663                 /*
1664                  *  Locate the command and return a "reset" status
1665                  *  for it.  This is not completely correct and will
1666                  *  probably return to haunt me later.
1667                  */
1668                 for (i = 0; i < p->maxscb; i++) {
1669                         if (cmd == p->SCB_array[i]) {
1670                                 reset = (Scsi_Cmnd *)p->SCB_array[i];
1671                                 p->SCB_array[i] = NULL;
1672                                 reset->result = DID_RESET << 16;
1673                                 reset->scsi_done(reset);
1674                                 break;
1675                         }
1676                 }
1677                 break;
1678 
1679             default:
1680                 panic("aic7xxx_reset: internal error\n");
1681         }
1682 
1683         restore_flags(flags);
1684         return(SCSI_RESET_SUCCESS);
1685 }
1686 
1687 int aic7xxx_biosparam(Disk *disk, int devno, int geom[])
     /* [previous][next][first][last][top][bottom][index][help] */
1688 {
1689         int heads, sectors, cylinders;
1690         struct aic7xxx_host *p;
1691         
1692         p = (struct aic7xxx_host *)disk->device->host->hostdata;
1693 
1694         /*
1695          *  XXX - if I could portably find the card's configuration
1696          *        information, then this could be autodetected instead
1697          *        of left to a boot-time switch.
1698          */
1699         heads = 64;
1700         sectors = 32;
1701         cylinders = disk->capacity / (heads * sectors);
1702 
1703         if (p->extended && cylinders > 1024) {
1704                 heads = 255;
1705                 sectors = 63;
1706                 cylinders = disk->capacity / (255 * 63);
1707         }
1708 
1709         geom[0] = heads;
1710         geom[1] = sectors;
1711         geom[2] = cylinders;
1712 
1713         return(0);
1714 }
1715 

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