root/drivers/scsi/aha274x.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. aha274x_setup
  5. aha274x_getscb
  6. aha274x_length
  7. aha274x_sg_check
  8. aha274x_to_scsirate
  9. aha274x_isr
  10. aha274x_probe
  11. aha274x_type
  12. aha274x_loadram
  13. aha274x_register
  14. aha274x_detect
  15. aha274x_info
  16. aha274x_command
  17. aha274x_buildscb
  18. aha274x_putscb
  19. aha274x_queue
  20. aha274x_kill
  21. aha274x_abort
  22. aha274x_reset
  23. aha274x_biosparam

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

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