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_scb
  4. aic7xxx_setup
  5. aic7xxx_loadseq
  6. aic7xxx_delay
  7. rcs_version
  8. aic7xxx_info
  9. aic7xxx_length
  10. aic7xxx_scsirate
  11. aic7xxx_putscb
  12. aic7xxx_getscb
  13. aic7xxx_match_scb
  14. aic7xxx_busy_target
  15. aic7xxx_unbusy_target
  16. aic7xxx_done
  17. aic7xxx_done_aborted_scbs
  18. aic7xxx_add_waiting_scb
  19. aic7xxx_abort_waiting_scb
  20. aic7xxx_reset_device
  21. aic7xxx_reset_current_bus
  22. aic7xxx_reset_channel
  23. aic7xxx_isr
  24. aic7xxx_probe
  25. read_2840_seeprom
  26. read_seeprom
  27. detect_maxscb
  28. aic7xxx_register
  29. aic7xxx_detect
  30. aic7xxx_buildscb
  31. aic7xxx_queue
  32. aic7xxx_abort_reset
  33. aic7xxx_abort
  34. aic7xxx_reset
  35. aic7xxx_biosparam

   1 /*+M*************************************************************************
   2  * Adaptec AIC7xxx device driver for Linux.
   3  *
   4  * Copyright (c) 1994 John Aycock
   5  *   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, or (at your option)
  10  * 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; see the file COPYING.  If not, write to
  19  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20  *
  21  * Sources include the Adaptec 1740 driver (aha1740.c), the Ultrastor 24F
  22  * driver (ultrastor.c), various Linux kernel source, the Adaptec EISA
  23  * config file (!adp7771.cfg), the Adaptec AHA-2740A Series User's Guide,
  24  * the Linux Kernel Hacker's Guide, Writing a SCSI Device Driver for Linux,
  25  * the Adaptec 1542 driver (aha1542.c), the Adaptec EISA overlay file
  26  * (adp7770.ovl), the Adaptec AHA-2740 Series Technical Reference Manual,
  27  * the Adaptec AIC-7770 Data Book, the ANSI SCSI specification, the
  28  * ANSI SCSI-2 specification (draft 10c), ...
  29  *
  30  * ----------------------------------------------------------------
  31  *  Modified to include support for wide and twin bus adapters,
  32  *  DMAing of SCBs, tagged queueing, IRQ sharing, bug fixes,
  33  *  and other rework of the code.
  34  *
  35  *  Parts of this driver are based on the FreeBSD driver by Justin
  36  *  T. Gibbs.
  37  *
  38  *  A Boot time option was also added for not resetting the scsi bus.
  39  *
  40  *    Form:  aic7xxx=extended,no_reset
  41  *
  42  *    -- Daniel M. Eischen, deischen@iworks.InterWorks.org, 04/03/95
  43  *
  44  *  $Id: aic7xxx.c,v 3.0 1996/04/16 09:11:53 deang Exp $
  45  *-M*************************************************************************/
  46 
  47 #ifdef MODULE
  48 #include <linux/module.h>
  49 #endif
  50 
  51 #include <stdarg.h>
  52 #include <asm/io.h>
  53 #include <linux/string.h>
  54 #include <linux/errno.h>
  55 #include <linux/kernel.h>
  56 #include <linux/ioport.h>
  57 #include <linux/bios32.h>
  58 #include <linux/delay.h>
  59 #include <linux/sched.h>
  60 #include <linux/pci.h>
  61 #include <linux/proc_fs.h>
  62 #include <linux/blk.h>
  63 #include "sd.h"
  64 #include "scsi.h"
  65 #include "hosts.h"
  66 #include "aic7xxx.h"
  67 #include "aic7xxx_reg.h"
  68 #include <linux/stat.h>
  69 
  70 #include <linux/config.h>       /* for CONFIG_PCI */
  71 
  72 struct proc_dir_entry proc_scsi_aic7xxx = {
  73     PROC_SCSI_AIC7XXX, 7, "aic7xxx",
  74     S_IFDIR | S_IRUGO | S_IXUGO, 2
  75 };
  76 
  77 #define AIC7XXX_C_VERSION  "$Revision: 3.0 $"
  78 
  79 #define NUMBER(arr)     (sizeof(arr) / sizeof(arr[0]))
  80 #define MIN(a,b)        ((a < b) ? a : b)
  81 #define ALL_TARGETS -1
  82 #ifndef TRUE
  83 #  define TRUE 1
  84 #endif
  85 #ifndef FALSE
  86 #  define FALSE 0
  87 #endif
  88 
  89 /*
  90  * Defines for PCI bus support, testing twin bus support, DMAing of
  91  * SCBs, tagged queueing, commands (SCBs) per lun, and SCSI bus reset
  92  * delay time.
  93  *
  94  *   o PCI bus support - this has been implemented and working since
  95  *     the December 1, 1994 release of this driver. If you don't have
  96  *     a PCI bus, then you can configure your kernel without PCI
  97  *     support because all PCI dependent code is bracketed with
  98  *     "#ifdef CONFIG_PCI ... #endif CONFIG_PCI".
  99  *
 100  *   o Twin bus support - this has been tested and does work.
 101  *
 102  *   o DMAing of SCBs - thanks to Kai Makisara, this now works.
 103  *     This define is now taken out and DMAing of SCBs is always
 104  *     performed (8/12/95 - DE).
 105  *
 106  *   o Tagged queueing - this driver is capable of tagged queueing
 107  *     but I am unsure as to how well the higher level driver implements
 108  *     tagged queueing. Therefore, the maximum commands per lun is
 109  *     set to 2. If you want to implement tagged queueing, ensure
 110  *     this define is not commented out.
 111  *
 112  *   o Sharing IRQs - allowed for sharing of IRQs. This will allow
 113  *     for multiple aic7xxx host adapters sharing the same IRQ, but
 114  *     not for sharing IRQs with other devices. The higher level
 115  *     PCI code and interrupt handling needs to be modified to
 116  *     support this.
 117  *
 118  *   o Commands per lun - If tagged queueing is enabled, then you
 119  *     may want to try increasing AIC7XXX_CMDS_PER_LUN to more
 120  *     than 2.  By default, we limit the SCBs per lun to 2 with
 121  *     or without tagged queueing enabled.  If tagged queueing is
 122  *     disabled, the sequencer will keep the 2nd SCB in the input
 123  *     queue until the first one completes - so it is OK to to have
 124  *     more than 1 SCB queued.  If tagged queueing is enabled, then
 125  *     the sequencer will attempt to send the 2nd SCB to the device
 126  *     while the first SCB is executing and the device is disconnected.
 127  *     For adapters limited to 4 SCBs, you may want to actually
 128  *     decrease the commands per lun to 1, if you often have more
 129  *     than 2 devices active at the same time.  This will allocate
 130  *     1 SCB for each device and ensure that there will always be
 131  *     a free SCB for up to 4 devices active at the same time.
 132  *
 133  *   o 3985 support - The 3985 adapter is much like the 3940, but
 134  *     has three 7870 controllers as opposed to two for the 3940.
 135  *     It will get probed and recognized as three different adapters,
 136  *     but all three controllers share the same bank of 255 SCBs
 137  *     instead of each controller having their own bank (like the
 138  *     controllers on the 3940).  For this reason, it is important
 139  *     that all devices be resident on just one channel of the 3985.
 140  *     In the near future, we'll modify the driver to reserve 1/3
 141  *     of the SCBs for each controller.
 142  *
 143  *  Daniel M. Eischen, deischen@iworks.InterWorks.org, 01/11/96
 144  */
 145 
 146 /* Uncomment this for testing twin bus support. */
 147 #define AIC7XXX_TWIN_SUPPORT
 148 
 149 /* Uncomment this for tagged queueing. */
 150 /* #define AIC7XXX_TAGGED_QUEUEING */
 151 
 152 /* Uncomment this for allowing sharing of IRQs. */
 153 #define AIC7XXX_SHARE_IRQS
 154 
 155 /*
 156  * You can try raising me if tagged queueing is enabled, or lowering
 157  * me if you only have 4 SCBs.
 158  */
 159 #define AIC7XXX_CMDS_PER_LUN 2
 160 
 161 /* Set this to the delay in seconds after SCSI bus reset. */
 162 #define AIC7XXX_RESET_DELAY 15
 163 
 164 /*
 165  * Uncomment the following define for collection of SCSI transfer statistics
 166  * for the /proc filesystem.
 167  *
 168  * NOTE: This does affect performance since it has to maintain statistics.
 169  */
 170 /* #define AIC7XXX_PROC_STATS */
 171 
 172 /*
 173  * For debugging the abort/reset code.
 174  */
 175 /* #define AIC7XXX_DEBUG_ABORT */
 176 
 177 /*
 178  * For general debug messages
 179  */
 180 #define AIC7XXX_DEBUG
 181 
 182 /*
 183  * Controller type and options
 184  */
 185 typedef enum {
 186   AIC_NONE,
 187   AIC_7770,     /* EISA aic7770 on motherboard */
 188   AIC_7771,     /* EISA aic7771 on 274x */
 189   AIC_284x,     /* VLB  aic7770 on 284x, BIOS disabled */
 190   AIC_7850,     /* PCI  aic7850 */
 191   AIC_7855,     /* PCI  aic7855 */
 192   AIC_7870,     /* PCI  aic7870 on motherboard */
 193   AIC_7871,     /* PCI  aic7871 on 294x */
 194   AIC_7872,     /* PCI  aic7872 on 3940 */
 195   AIC_7873,     /* PCI  aic7873 on 3985 */
 196   AIC_7874,     /* PCI  aic7874 on 294x Differential */
 197   AIC_7880,     /* PCI  aic7880 on motherboard */
 198   AIC_7881,     /* PCI  aic7881 on 294x Ultra */
 199   AIC_7882,     /* PCI  aic7882 on 3940 Ultra */
 200   AIC_7883,     /* PCI  aic7883 on 3985 Ultra */
 201   AIC_7884      /* PCI  aic7884 on 294x Ultra Differential */
 202 } aha_type;
 203 
 204 typedef enum {
 205   AIC_777x,     /* AIC-7770 based */
 206   AIC_785x,     /* AIC-7850 based */
 207   AIC_787x,     /* AIC-7870 based */
 208   AIC_788x      /* AIC-7880 based */
 209 } aha_chip_type;
 210 
 211 typedef enum {
 212   AIC_SINGLE,  /* Single Channel */
 213   AIC_TWIN,    /* Twin Channel */
 214   AIC_WIDE     /* Wide Channel */
 215 } aha_bus_type;
 216 
 217 typedef enum {
 218   AIC_UNKNOWN,
 219   AIC_ENABLED,
 220   AIC_DISABLED
 221 } aha_status_type;
 222 
 223 typedef enum {
 224   LIST_HEAD,
 225   LIST_SECOND
 226 } insert_type;
 227 
 228 typedef enum {
 229   ABORT_RESET_INACTIVE,
 230   ABORT_RESET_PENDING,
 231   ABORT_RESET_SUCCESS
 232 } aha_abort_reset_type;
 233 
 234 /*
 235  * Define an array of board names that can be indexed by aha_type.
 236  * Don't forget to change this when changing the types!
 237  */
 238 static const char * board_names[] = {
 239   "<AIC-7xxx Unknown>",         /* AIC_NONE */
 240   "AIC-7770",                   /* AIC_7770 */
 241   "AHA-2740",                   /* AIC_7771 */
 242   "AHA-2840",                   /* AIC_284x */
 243   "AIC-7850",                   /* AIC_7850 */
 244   "AIC-7855",                   /* AIC_7855 */
 245   "AIC-7870",                   /* AIC_7870 */
 246   "AHA-2940",                   /* AIC_7871 */
 247   "AHA-3940",                   /* AIC_7872 */
 248   "AHA-3985",                   /* AIC_7873 */
 249   "AHA-2940 Differential",      /* AIC_7874 */
 250   "AIC-7880 Ultra",             /* AIC_7880 */
 251   "AHA-2940 Ultra",             /* AIC_7881 */
 252   "AHA-3940 Ultra",             /* AIC_7882 */
 253   "AHA-3985 Ultra",             /* AIC_7883 */
 254   "AHA-2940 Ultra Differential" /* AIC_7884 */
 255 };
 256 
 257 /*
 258  * There should be a specific return value for this in scsi.h, but
 259  * it seems that most drivers ignore it.
 260  */
 261 #define DID_UNDERFLOW   DID_ERROR
 262 
 263 /*
 264  *  What we want to do is have the higher level scsi driver requeue
 265  *  the command to us. There is no specific driver status for this
 266  *  condition, but the higher level scsi driver will requeue the
 267  *  command on a DID_BUS_BUSY error.
 268  */
 269 #define DID_RETRY_COMMAND DID_BUS_BUSY
 270 
 271 /*
 272  * EISA/VL-bus stuff
 273  */
 274 #define MINSLOT         1
 275 #define MAXSLOT         15
 276 #define SLOTBASE(x)     ((x) << 12)
 277 #define MAXIRQ          15
 278 
 279 /*
 280  * Standard EISA Host ID regs  (Offset from slot base)
 281  */
 282 #define HID0            0x80   /* 0,1: msb of ID2, 2-7: ID1      */
 283 #define HID1            0x81   /* 0-4: ID3, 5-7: LSB ID2         */
 284 #define HID2            0x82   /* product                        */
 285 #define HID3            0x83   /* firmware revision              */
 286 
 287 /*
 288  * AIC-7770 I/O range to reserve for a card
 289  */
 290 #define MINREG          0xC00
 291 #define MAXREG          0xCBF
 292 
 293 #define INTDEF          0x5C            /* Interrupt Definition Register */
 294 
 295 /*
 296  * Some defines for the HCNTRL register.
 297  */
 298 #define REQ_PAUSE       IRQMS | INTEN | PAUSE
 299 #define UNPAUSE_274X    IRQMS | INTEN
 300 #define UNPAUSE_284X    INTEN
 301 #define UNPAUSE_294X    IRQMS | INTEN
 302 
 303 /*
 304  * AIC-78X0 PCI registers
 305  */
 306 #define CLASS_PROGIF_REVID      0x08
 307 #define         DEVREVID        0x000000FFul
 308 #define         PROGINFC        0x0000FF00ul
 309 #define         SUBCLASS        0x00FF0000ul
 310 #define         BASECLASS       0xFF000000ul
 311 
 312 #define CSIZE_LATTIME           0x0C
 313 #define         CACHESIZE       0x0000003Ful    /* only 5 bits */
 314 #define         LATTIME         0x0000FF00ul
 315 
 316 #define DEVCONFIG               0x40
 317 #define         MPORTMODE       0x00000400ul    /* aic7870 only */
 318 #define         RAMPSM          0x00000200ul    /* aic7870 only */
 319 #define         VOLSENSE        0x00000100ul
 320 #define         SCBRAMSEL       0x00000080ul
 321 #define         MRDCEN          0x00000040ul
 322 #define         EXTSCBTIME      0x00000020ul    /* aic7870 only */
 323 #define         EXTSCBPEN       0x00000010ul    /* aic7870 only */
 324 #define         BERREN          0x00000008ul
 325 #define         DACEN           0x00000004ul
 326 #define         STPWLEVEL       0x00000002ul
 327 #define         DIFACTNEGEN     0x00000001ul    /* aic7870 only */
 328 
 329 /*
 330  *
 331  * Define the format of the SEEPROM registers (16 bits).
 332  *
 333  */
 334 struct seeprom_config {
 335 
 336 /*
 337  * SCSI ID Configuration Flags
 338  */
 339 #define CFXFER          0x0007          /* synchronous transfer rate */
 340 #define CFSYNCH         0x0008          /* enable synchronous transfer */
 341 #define CFDISC          0x0010          /* enable disconnection */
 342 #define CFWIDEB         0x0020          /* wide bus device (wide card) */
 343 /* UNUSED               0x00C0 */
 344 #define CFSTART         0x0100          /* send start unit SCSI command */
 345 #define CFINCBIOS       0x0200          /* include in BIOS scan */
 346 #define CFRNFOUND       0x0400          /* report even if not found */
 347 /* UNUSED               0xF800 */
 348   unsigned short device_flags[16];      /* words 0-15 */
 349 
 350 /*
 351  * BIOS Control Bits
 352  */
 353 #define CFSUPREM        0x0001          /* support all removable drives */
 354 #define CFSUPREMB       0x0002          /* support removable drives for boot only */
 355 #define CFBIOSEN        0x0004          /* BIOS enabled */
 356 /* UNUSED               0x0008 */
 357 #define CFSM2DRV        0x0010          /* support more than two drives */
 358 #define CF284XEXTEND    0x0020          /* extended translation (284x cards) */
 359 /* UNUSED               0x0040 */
 360 #define CFEXTEND        0x0080          /* extended translation enabled */
 361 /* UNUSED               0xFF00 */
 362   unsigned short bios_control;          /* word 16 */
 363 
 364 /*
 365  * Host Adapter Control Bits
 366  */
 367 /* UNUSED               0x0001 */
 368 #define CFULTRAEN       0x0002          /* Ultra SCSI speed enable (Ultra cards) */
 369 #define CF284XSELTO     0x0003          /* Selection timeout (284x cards) */
 370 #define CF284XFIFO      0x000C          /* FIFO Threshold (284x cards) */
 371 #define CFSTERM         0x0004          /* SCSI low byte termination (non-wide cards) */
 372 #define CFWSTERM        0x0008          /* SCSI high byte termination (wide card) */
 373 #define CFSPARITY       0x0010          /* SCSI parity */
 374 #define CF284XSTERM     0x0020          /* SCSI low byte termination (284x cards) */
 375 #define CFRESETB        0x0040          /* reset SCSI bus at IC initialization */
 376 /* UNUSED               0xFF80 */
 377   unsigned short adapter_control;       /* word 17 */
 378 
 379 /*
 380  * Bus Release, Host Adapter ID
 381  */
 382 #define CFSCSIID        0x000F          /* host adapter SCSI ID */
 383 /* UNUSED               0x00F0 */
 384 #define CFBRTIME        0xFF00          /* bus release time */
 385   unsigned short brtime_id;             /* word 18 */
 386 
 387 /*
 388  * Maximum targets
 389  */
 390 #define CFMAXTARG       0x00FF  /* maximum targets */
 391 /* UNUSED               0xFF00 */
 392   unsigned short max_targets;           /* word 19 */
 393 
 394   unsigned short res_1[11];             /* words 20-30 */
 395   unsigned short checksum;              /* word 31 */
 396 };
 397 
 398 /*
 399  * Pause the sequencer and wait for it to actually stop - this
 400  * is important since the sequencer can disable pausing for critical
 401  * sections.
 402  */
 403 #define PAUSE_SEQUENCER(p) \
 404   outb(p->pause, HCNTRL + p->base);                     \
 405   while ((inb(HCNTRL + p->base) & PAUSE) == 0)          \
 406     ;                                                   \
 407 
 408 /*
 409  * Unpause the sequencer. Unremarkable, yet done often enough to
 410  * warrant an easy way to do it.
 411  */
 412 #define UNPAUSE_SEQUENCER(p) \
 413   outb(p->unpause, HCNTRL + p->base)
 414 
 415 /*
 416  * Restart the sequencer program from address zero
 417  */
 418 #define RESTART_SEQUENCER(p) \
 419   do {                                                  \
 420     outb(SEQRESET | FASTMODE, SEQCTL + p->base);        \
 421   } while (inb(SEQADDR0 + p->base) != 0 &&              \
 422            inb(SEQADDR1 + p->base) != 0);               \
 423   UNPAUSE_SEQUENCER(p);
 424 
 425 /*
 426  * If an error occurs during a data transfer phase, run the command
 427  * to completion - it's easier that way - making a note of the error
 428  * condition in this location. This then will modify a DID_OK status
 429  * into an appropriate error for the higher-level SCSI code.
 430  */
 431 #define aic7xxx_error(cmd)      ((cmd)->SCp.Status)
 432 
 433 /*
 434  * Keep track of the targets returned status.
 435  */
 436 #define aic7xxx_status(cmd)     ((cmd)->SCp.sent_command)
 437 
 438 /*
 439  * The position of the SCSI commands scb within the scb array.
 440  */
 441 #define aic7xxx_position(cmd)   ((cmd)->SCp.have_data_in)
 442 
 443 /*
 444  * Since the sequencer code DMAs the scatter-gather structures
 445  * directly from memory, we use this macro to assert that the
 446  * kernel structure hasn't changed.
 447  */
 448 #define SG_STRUCT_CHECK(sg) \
 449   ((char *) &(sg).address - (char *) &(sg) != 0 ||  \
 450    (char *) &(sg).length  - (char *) &(sg) != 8 ||  \
 451    sizeof((sg).address) != 4 ||                   \
 452    sizeof((sg).length)  != 4 ||                   \
 453    sizeof(sg)           != 12)
 454 
 455 /*
 456  * "Static" structures. Note that these are NOT initialized
 457  * to zero inside the kernel - we have to initialize them all
 458  * explicitly.
 459  *
 460  * We support multiple adapter cards per interrupt, but keep a
 461  * linked list of Scsi_Host structures for each IRQ.  On an interrupt,
 462  * use the IRQ as an index into aic7xxx_boards[] to locate the card
 463  * information.
 464  */
 465 static struct Scsi_Host *aic7xxx_boards[MAXIRQ + 1];
 466 
 467 /*
 468  * When we detect and register the card, it is possible to
 469  * have the card raise a spurious interrupt.  Because we need
 470  * to support multiple cards, we cannot tell which card caused
 471  * the spurious interrupt.  And, we might not even have added
 472  * the card info to the linked list at the time the spurious
 473  * interrupt gets raised.  This variable is suppose to keep track
 474  * of when we are registering a card and how many spurious
 475  * interrupts we have encountered.
 476  *
 477  *   0 - do not allow spurious interrupts.
 478  *   1 - allow 1 spurious interrupt
 479  *   2 - have 1 spurious interrupt, do not allow any more.
 480  *
 481  * I've made it an integer instead of a boolean in case we
 482  * want to allow more than one spurious interrupt for debugging
 483  * purposes.  Otherwise, it could just go from true to false to
 484  * true (or something like that).
 485  *
 486  * When the driver detects the cards, we'll set the count to 1
 487  * for each card detection and registration.  After the registration
 488  * of a card completes, we'll set the count back to 0.  So far, it
 489  * seems to be enough to allow a spurious interrupt only during
 490  * card registration; if a spurious interrupt is going to occur,
 491  * this is where it happens.
 492  *
 493  * We should be able to find a way to avoid getting the spurious
 494  * interrupt.  But until we do, we have to keep this ugly code.
 495  */
 496 static int aic7xxx_spurious_count;
 497 
 498 /*
 499  * The driver keeps up to four scb structures per card in memory. Only the
 500  * first 25 bytes of the structure are valid for the hardware, the rest used
 501  * for driver level bookkeeping.
 502  */
 503 
 504 struct aic7xxx_scb {
 505 /* ------------    Begin hardware supported fields    ---------------- */
 506 /* 0*/  unsigned char control;
 507 /* 1*/  unsigned char target_channel_lun;       /* 4/1/3 bits */
 508 /* 2*/  unsigned char target_status;
 509 /* 3*/  unsigned char SG_segment_count;
 510 /* 4*/  unsigned char SG_list_pointer[4] __attribute__ ((packed));
 511 /* 8*/  unsigned char residual_SG_segment_count;
 512 /* 9*/  unsigned char residual_data_count[3];
 513 /*12*/  unsigned char data_pointer[4] __attribute__ ((packed));
 514 /*16*/  unsigned long data_count;
 515 /*20*/  unsigned char SCSI_cmd_pointer[4] __attribute__ ((packed));
 516 /*24*/  unsigned char SCSI_cmd_length;
 517 #define SCB_PIO_TRANSFER_SIZE   25      /*
 518                                          * amount we need to upload/download
 519                                          * via rep in/outsb to perform
 520                                          * a request sense.  The second
 521                                          * RESERVED byte is initialized to
 522                                          * 0 in getscb().
 523                                          */
 524 /*25*/  u_char next_waiting;            /* Used to thread SCBs awaiting selection. */
 525         /*-----------------end of hardware supported fields----------------*/
 526         struct aic7xxx_scb *next;       /* next ptr when in free list */
 527         Scsi_Cmnd          *cmd;        /* Scsi_Cmnd for this scb */
 528 #define SCB_FREE               0x00
 529 #define SCB_ACTIVE             0x01
 530 #define SCB_ABORTED            0x02
 531 #define SCB_DEVICE_RESET       0x04
 532 #define SCB_IMMED              0x08
 533 #define SCB_SENSE              0x10
 534 #define SCB_QUEUED_FOR_DONE    0x40
 535         int                 state;          /* current state of scb */
 536         unsigned int        position;       /* Position in scb array */
 537         struct scatterlist  sg;
 538         struct scatterlist  sense_sg;
 539         unsigned char       sense_cmd[6];   /* Allocate 6 characters for sense command */
 540 };
 541 
 542 static struct {
 543   unsigned char errno;
 544   const char *errmesg;
 545 } hard_error[] = {
 546   { ILLHADDR,  "Illegal Host Access" },
 547   { ILLSADDR,  "Illegal Sequencer Address referenced" },
 548   { ILLOPCODE, "Illegal Opcode in sequencer program" },
 549   { PARERR,    "Sequencer Ram Parity Error" }
 550 };
 551 
 552 static unsigned char
 553 generic_sense[] = { REQUEST_SENSE, 0, 0, 0, 255, 0 };
 554 
 555 /*
 556  * The maximum number of SCBs we could have for ANY type
 557  * of card. DON'T FORGET TO CHANGE THE SCB MASK IN THE
 558  * SEQUENCER CODE IF THIS IS MODIFIED!
 559  */
 560 #define AIC7XXX_MAXSCB  255
 561 
 562 /*
 563  * Define a structure used for each host adapter, only one per IRQ.
 564  */
 565 struct aic7xxx_host {
 566   struct Scsi_Host        *host;             /* pointer to scsi host */
 567   int                      base;             /* card base address */
 568   int                      maxscb;           /* hardware SCBs */
 569   int                      numscb;           /* current number of scbs */
 570   int                      extended;         /* extended xlate? */
 571   aha_type                 type;             /* card type */
 572   aha_chip_type            chip_type;        /* chip base type */
 573   int                      ultra_enabled;    /* Ultra SCSI speed enabled */
 574   int                      chan_num;         /* for 3940/3985, channel number */
 575   aha_bus_type             bus_type;         /* normal/twin/wide bus */
 576   unsigned char            a_scanned;        /* 0 not scanned, 1 scanned */
 577   unsigned char            b_scanned;        /* 0 not scanned, 1 scanned */
 578   unsigned int             isr_count;        /* Interrupt count */
 579   volatile unsigned char   unpause;          /* unpause value for HCNTRL */
 580   volatile unsigned char   pause;            /* pause value for HCNTRL */
 581   volatile unsigned short  needsdtr_copy;    /* default config */
 582   volatile unsigned short  needsdtr;
 583   volatile unsigned short  sdtr_pending;
 584   volatile unsigned short  needwdtr_copy;    /* default config */
 585   volatile unsigned short  needwdtr;
 586   volatile unsigned short  wdtr_pending;
 587   volatile unsigned short  discenable;       /* Targets allowed to disconnect */
 588   struct seeprom_config    seeprom;
 589   int                      have_seeprom;
 590   unsigned char            qcntmask;
 591   struct Scsi_Host        *next;             /* allow for multiple IRQs */
 592   struct aic7xxx_scb       scb_array[AIC7XXX_MAXSCB];  /* active commands */
 593   struct aic7xxx_scb      *free_scb;         /* list of free SCBs */
 594   struct aic7xxx_scb      *aborted_scb;       /* list of aborted SCBs */
 595 #ifdef AIC7XXX_PROC_STATS
 596   /*
 597    * Statistics Kept:
 598    *
 599    * Total Xfers (count for each command that has a data xfer),
 600    * broken down further by reads && writes.
 601    *
 602    * Binned sizes, writes && reads:
 603    *    < 512, 512, 1-2K, 2-4K, 4-8K, 8-16K, 16-32K, 32-64K, 64K-128K, > 128K
 604    *
 605    * Total amounts read/written above 512 bytes (amts under ignored)
 606    */
 607   struct aic7xxx_xferstats {
 608     long xfers;                              /* total xfer count */
 609     long w_total;                            /* total writes */
 610     long w_total512;                         /* 512 byte blocks written */
 611     long w_bins[10];                         /* binned write */
 612     long r_total;                            /* total reads */
 613     long r_total512;                         /* 512 byte blocks read */
 614     long r_bins[10];                         /* binned reads */
 615   } stats[2][16][8];                         /* channel, target, lun */
 616 #endif /* AIC7XXX_PROC_STATS */
 617 };
 618 
 619 struct aic7xxx_host_config {
 620   int              irq;        /* IRQ number */
 621   int              base;       /* I/O base */
 622   int              maxscb;     /* hardware SCBs */
 623   int              unpause;    /* unpause value for HCNTRL */
 624   int              pause;      /* pause value for HCNTRL */
 625   int              scsi_id;    /* host SCSI ID */
 626   int              scsi_id_b;  /* host SCSI ID B channel for twin cards */
 627   int              extended;   /* extended xlate? */
 628   int              ultra_enabled;    /* Ultra SCSI speed enabled */
 629   int              chan_num;   /* for 3940/3985, channel number */
 630   int              use_defaults;   /* Use default wide/sync settings. */
 631   unsigned char    busrtime;   /* bus release time */
 632   unsigned char    bus_speed;  /* bus speed */
 633   unsigned char    qcntmask;
 634   aha_type         type;       /* card type */
 635   aha_chip_type    chip_type;  /* chip base type */
 636   aha_bus_type     bus_type;   /* normal/twin/wide bus */
 637   aha_status_type  bios;       /* BIOS is enabled/disabled */
 638   aha_status_type  parity;     /* bus parity enabled/disabled */
 639   aha_status_type  low_term;   /* bus termination low byte */
 640   aha_status_type  high_term;  /* bus termination high byte (wide cards only) */
 641 };
 642 
 643 /*
 644  * Valid SCSIRATE values. (p. 3-17)
 645  * Provides a mapping of transfer periods in ns to the proper value to
 646  * stick in the scsiscfr reg to use that transfer rate.
 647  */
 648 static struct {
 649   short period;
 650   /* Rates in Ultra mode have bit 8 of sxfr set */
 651 #define         ULTRA_SXFR 0x100
 652   short rate;
 653   const char *english;
 654 } aic7xxx_syncrates[] = {
 655   {  50,  0x100,  "20.0" },
 656   {  62,  0x110,  "16.0" },
 657   {  75,  0x120,  "13.4" },
 658   { 100,  0x140,  "10.0" },
 659   { 100,  0x000,  "10.0" },
 660   { 125,  0x010,  "8.0"  },
 661   { 150,  0x020,  "6.67" },
 662   { 175,  0x030,  "5.7"  },
 663   { 200,  0x040,  "5.0"  },
 664   { 225,  0x050,  "4.4"  },
 665   { 250,  0x060,  "4.0"  },
 666   { 275,  0x070,  "3.6"  }
 667 };
 668 
 669 static int num_aic7xxx_syncrates =
 670     sizeof(aic7xxx_syncrates) / sizeof(aic7xxx_syncrates[0]);
 671 
 672 #ifdef CONFIG_PCI
 673 static int number_of_39xxs = 0;
 674 #endif CONFIG_PCI
 675 
 676 #ifdef AIC7XXX_DEBUG
 677 static void
 678 debug(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 679 {
 680   va_list ap;
 681   char buf[256];
 682 
 683   va_start(ap, fmt);
 684   vsprintf(buf, fmt, ap);
 685   printk(buf);
 686   va_end(ap);
 687 }
 688 
 689 static void
 690 debug_config(struct aic7xxx_host_config *p)
     /* [previous][next][first][last][top][bottom][index][help] */
 691 {
 692   int scsi_conf;
 693   unsigned char brelease;
 694   unsigned char dfthresh;
 695 
 696   static int DFT[] = { 0, 50, 75, 100 };
 697   static int SST[] = { 256, 128, 64, 32 };
 698   static const char *BUSW[] = { "", "-TWIN", "-WIDE" };
 699 
 700   scsi_conf = inb(SCSICONF + p->base);
 701 
 702   /*
 703    * Scale the Data FIFO Threshold and the Bus Release Time; they are
 704    * stored in formats compatible for writing to sequencer registers.
 705    */
 706   dfthresh = p->bus_speed  >> 6;
 707 
 708   if (p->chip_type == AIC_777x)
 709   {
 710     brelease = p->busrtime >> 2;
 711   }
 712   else
 713   {
 714     brelease = p->busrtime;
 715   }
 716   if (brelease == 0)
 717   {
 718     brelease = 2;
 719   }
 720 
 721   switch (p->type)
 722   {
 723     case AIC_7770:
 724     case AIC_7771:
 725       printk("%s%s AT EISA SLOT %d:\n", board_names[p->type], BUSW[p->bus_type],
 726              p->base >> 12);
 727       break;
 728 
 729     case AIC_284x:
 730       printk("%s%s AT VLB SLOT %d:\n", board_names[p->type], BUSW[p->bus_type],
 731              p->base >> 12);
 732       break;
 733 
 734     case AIC_7850:
 735     case AIC_7855:
 736     case AIC_7870:
 737     case AIC_7871:
 738     case AIC_7872:
 739     case AIC_7873:
 740     case AIC_7874:
 741     case AIC_7880:
 742     case AIC_7881:
 743     case AIC_7882:
 744     case AIC_7883:
 745     case AIC_7884:
 746       printk("%s%s (PCI-bus):\n", board_names[p->type], BUSW[p->bus_type]);
 747       break;
 748 
 749     default:
 750       panic("aic7xxx: (debug_config) internal error.\n");
 751   }
 752 
 753   printk("    irq %d\n"
 754          "    bus release time %d bclks\n"
 755          "    data fifo threshold %d%%\n",
 756          p->irq,
 757          brelease,
 758          DFT[dfthresh]);
 759 
 760   printk("    SCSI CHANNEL A:\n"
 761          "        scsi id %d\n"
 762          "        scsi selection timeout %d ms\n"
 763          "        scsi bus reset at power-on %sabled\n",
 764          scsi_conf & 0x07,
 765          SST[(scsi_conf >> 3) & 0x03],
 766          (scsi_conf & 0x40) ? "en" : "dis");
 767 
 768   if ((p->chip_type == AIC_777x) && (p->parity == AIC_UNKNOWN))
 769   {
 770     /*
 771      * Set the parity for 7770 based cards.
 772      */
 773     p->parity = (scsi_conf & 0x20) ? AIC_ENABLED : AIC_DISABLED;
 774   }
 775   if (p->parity != AIC_UNKNOWN)
 776   {
 777     printk("        scsi bus parity %sabled\n",
 778            (p->parity == AIC_ENABLED) ? "en" : "dis");
 779   }
 780 
 781   if ((p->type == AIC_7770) || (p->type == AIC_7771))
 782   {
 783     p->low_term = (scsi_conf & 0x80) ? AIC_ENABLED : AIC_DISABLED;
 784   }
 785   if (p->low_term != AIC_UNKNOWN)
 786   {
 787     printk("        scsi bus termination (low byte) %sabled\n",
 788           (p->low_term == AIC_ENABLED) ? "en" : "dis");
 789   }
 790   if ((p->bus_type == AIC_WIDE) && (p->high_term != AIC_UNKNOWN))
 791   {
 792     printk("        scsi bus termination (high byte) %sabled\n",
 793           (p->high_term == AIC_ENABLED) ? "en" : "dis");
 794   }
 795 }
 796 
 797 #if 0
 798 static void
 799 debug_scb(struct aic7xxx_scb *scb)
     /* [previous][next][first][last][top][bottom][index][help] */
 800 {
 801   printk("control 0x%x, tcl 0x%x, sg_count %d, sg_ptr 0x%x, cmdp 0x%x, cmdlen %d\n",
 802          scb->control, scb->target_channel_lun, scb->SG_segment_count,
 803          (scb->SG_list_pointer[3] << 24) | (scb->SG_list_pointer[2] << 16) |
 804          (scb->SG_list_pointer[1] << 8) | scb->SG_list_pointer[0],
 805          (scb->SCSI_cmd_pointer[3] << 24) | (scb->SCSI_cmd_pointer[2] << 16) |
 806          (scb->SCSI_cmd_pointer[1] << 8) | scb->SCSI_cmd_pointer[0],
 807          scb->SCSI_cmd_length);
 808   printk("reserved 0x%x, target status 0x%x, resid SG count %d, resid data count %d\n",
 809          (scb->RESERVED[1] << 8) | scb->RESERVED[0], scb->target_status,
 810          scb->residual_SG_segment_count, scb->residual_data_count);
 811   printk("data ptr 0x%x, data count %d, next waiting %d\n",
 812          (scb->data_pointer[3] << 24) | (scb->data_pointer[2] << 16) |
 813          (scb->data_pointer[1] << 8) | scb->data_pointer[0],
 814          scb->data_count, scb->next_waiting);
 815   printk("next ptr 0x%lx, Scsi Cmnd 0x%lx, state 0x%x, position %d\n",
 816          (unsigned long) scb->next, (unsigned long) scb->cmd, scb->state,
 817          scb->position);
 818 }
 819 #endif
 820 
 821 #else
 822 #  define debug(fmt, args...)
 823 #  define debug_config(x)
 824 #  define debug_scb(x)
 825 #endif AIC7XXX_DEBUG
 826 
 827 /*
 828  * XXX - these options apply unilaterally to _all_ 274x/284x/294x
 829  *       cards in the system. This should be fixed, but then,
 830  *       does anyone really have more than one in a machine?
 831  */
 832 static unsigned int aic7xxx_extended = 0;    /* extended translation on? */
 833 static unsigned int aic7xxx_no_reset = 0;    /* no resetting of SCSI bus */
 834 
 835 /*+F*************************************************************************
 836  * Function:
 837  *   aic7xxx_setup
 838  *
 839  * Description:
 840  *   Handle Linux boot parameters. This routine allows for assigning a value
 841  *   to a parameter with a ':' between the parameter and the value.
 842  *   ie. aic7xxx=unpause:0x0A,extended
 843  *-F*************************************************************************/
 844 void
 845 aic7xxx_setup(char *s, int *dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 846 {
 847   int   i, n;
 848   char *p;
 849 
 850   static struct {
 851     const char *name;
 852     unsigned int *flag;
 853   } options[] = {
 854     { "extended",    &aic7xxx_extended },
 855     { "no_reset",    &aic7xxx_no_reset },
 856     { NULL,          NULL }
 857   };
 858 
 859   for (p = strtok(s, ","); p; p = strtok(NULL, ","))
 860   {
 861     for (i = 0; options[i].name; i++)
 862     {
 863       n = strlen(options[i].name);
 864       if (!strncmp(options[i].name, p, n))
 865       {
 866         if (p[n] == ':')
 867         {
 868           *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
 869         }
 870         else
 871         {
 872           *(options[i].flag) = !0;
 873         }
 874       }
 875     }
 876   }
 877 }
 878 
 879 /*+F*************************************************************************
 880  * Function:
 881  *   aic7xxx_loadseq
 882  *
 883  * Description:
 884  *   Load the sequencer code into the controller memory.
 885  *-F*************************************************************************/
 886 static void
 887 aic7xxx_loadseq(int base)
     /* [previous][next][first][last][top][bottom][index][help] */
 888 {
 889   static unsigned char seqprog[] = {
 890     /*
 891      * Each sequencer instruction is 29 bits
 892      * long (fill in the excess with zeroes)
 893      * and has to be loaded from least -> most
 894      * significant byte, so this table has the
 895      * byte ordering reversed.
 896      */
 897 #   include "aic7xxx_seq.h"
 898   };
 899 
 900   /*
 901    * When the AIC-7770 is paused (as on chip reset), the
 902    * sequencer address can be altered and a sequencer
 903    * program can be loaded by writing it, byte by byte, to
 904    * the sequencer RAM port - the Adaptec documentation
 905    * recommends using REP OUTSB to do this, hence the inline
 906    * assembly. Since the address autoincrements as we load
 907    * the program, reset it back to zero afterward. Disable
 908    * sequencer RAM parity error detection while loading, and
 909    * make sure the LOADRAM bit is enabled for loading.
 910    */
 911   outb(PERRORDIS | SEQRESET | LOADRAM, SEQCTL + base);
 912 
 913   outsb(SEQRAM + base, seqprog, sizeof(seqprog));
 914 
 915   /*
 916    * WARNING!  This is a magic sequence!  After extensive
 917    * experimentation, it seems that you MUST turn off the
 918    * LOADRAM bit before you play with SEQADDR again, else
 919    * you will end up with parity errors being flagged on
 920    * your sequencer program. (You would also think that
 921    * turning off LOADRAM and setting SEQRESET to reset the
 922    * address to zero would work, but you need to do it twice
 923    * for it to take effect on the address. Timing problem?)
 924    */
 925   do {
 926     /*
 927      * Actually, reset it until
 928      * the address shows up as
 929      * zero just to be safe..
 930      */
 931     outb(SEQRESET | FASTMODE, SEQCTL + base);
 932   } while ((inb(SEQADDR0 + base) != 0) && (inb(SEQADDR1 + base) != 0));
 933 }
 934 
 935 /*+F*************************************************************************
 936  * Function:
 937  *   aic7xxx_delay
 938  *
 939  * Description:
 940  *   Delay for specified amount of time.
 941  *-F*************************************************************************/
 942 static void
 943 aic7xxx_delay(int seconds)
     /* [previous][next][first][last][top][bottom][index][help] */
 944 {
 945   unsigned long i;
 946 
 947   i = jiffies + (seconds * HZ);  /* compute time to stop */
 948 
 949   while (jiffies < i)
 950   {
 951     ;  /* Do nothing! */
 952   }
 953 }
 954 
 955 /*+F*************************************************************************
 956  * Function:
 957  *   rcs_version
 958  *
 959  * Description:
 960  *   Return a string containing just the RCS version number from either
 961  *   an Id or Revision RCS clause.
 962  *-F*************************************************************************/
 963 const char *
 964 rcs_version(const char *version_info)
     /* [previous][next][first][last][top][bottom][index][help] */
 965 {
 966   static char buf[10];
 967   char *bp, *ep;
 968 
 969   bp = NULL;
 970   strcpy(buf, "????");
 971   if (!strncmp(version_info, "$Id: ", 5))
 972   {
 973     if ((bp = strchr(version_info, ' ')) != NULL)
 974     {
 975       bp++;
 976       if ((bp = strchr(bp, ' ')) != NULL)
 977       {
 978         bp++;
 979       }
 980     }
 981   }
 982   else
 983   {
 984     if (!strncmp(version_info, "$Revision: ", 11))
 985     {
 986       if ((bp = strchr(version_info, ' ')) != NULL)
 987       {
 988         bp++;
 989       }
 990     }
 991   }
 992 
 993   if (bp != NULL)
 994   {
 995     if ((ep = strchr(bp, ' ')) != NULL)
 996     {
 997       register int len = ep - bp;
 998 
 999       strncpy(buf, bp, len);
1000       buf[len] = '\0';
1001     }
1002   }
1003 
1004   return buf;
1005 }
1006 
1007 /*+F*************************************************************************
1008  * Function:
1009  *   aic7xxx_info
1010  *
1011  * Description:
1012  *   Return a string describing the driver.
1013  *-F*************************************************************************/
1014 const char *
1015 aic7xxx_info(struct Scsi_Host *notused)
     /* [previous][next][first][last][top][bottom][index][help] */
1016 {
1017   static char buffer[128];
1018 
1019   strcpy(buffer, "Adaptec AHA274x/284x/294x (EISA/VLB/PCI-Fast SCSI) ");
1020   strcat(buffer, rcs_version(AIC7XXX_C_VERSION));
1021   strcat(buffer, "/");
1022   strcat(buffer, rcs_version(AIC7XXX_H_VERSION));
1023   strcat(buffer, "/");
1024   strcat(buffer, rcs_version(AIC7XXX_SEQ_VER));
1025 
1026   return buffer;
1027 }
1028 
1029 /*+F*************************************************************************
1030  * Function:
1031  *   aic7xxx_length
1032  *
1033  * Description:
1034  *   How much data should be transferred for this SCSI command? Stop
1035  *   at segment sg_last if it's a scatter-gather command so we can
1036  *   compute underflow easily.
1037  *-F*************************************************************************/
1038 static unsigned
1039 aic7xxx_length(Scsi_Cmnd *cmd, int sg_last)
     /* [previous][next][first][last][top][bottom][index][help] */
1040 {
1041   int i, segments;
1042   unsigned length;
1043   struct scatterlist *sg;
1044 
1045   segments = cmd->use_sg - sg_last;
1046   sg = (struct scatterlist *) cmd->buffer;
1047 
1048   if (cmd->use_sg)
1049   {
1050     for (i = length = 0; (i < cmd->use_sg) && (i < segments); i++)
1051     {
1052       length += sg[i].length;
1053     }
1054   }
1055   else
1056   {
1057     length = cmd->request_bufflen;
1058   }
1059 
1060   return (length);
1061 }
1062 
1063 /*+F*************************************************************************
1064  * Function:
1065  *   aic7xxx_scsirate
1066  *
1067  * Description:
1068  *   Look up the valid period to SCSIRATE conversion in our table
1069  *-F*************************************************************************/
1070 static void
1071 aic7xxx_scsirate(struct aic7xxx_host *p, unsigned char *scsirate,
     /* [previous][next][first][last][top][bottom][index][help] */
1072                  short period, unsigned char offset,
1073                  int target, char channel)
1074 {
1075   int i;
1076 
1077   for (i = 0; i < num_aic7xxx_syncrates; i++)
1078   {
1079     if ((aic7xxx_syncrates[i].period - period) >= 0)
1080     {
1081       /*
1082        * Watch out for Ultra speeds when ultra is not enabled and
1083        * vice-versa.
1084        */
1085       if (p->ultra_enabled)
1086       {
1087         if (!(aic7xxx_syncrates[i].rate & ULTRA_SXFR))
1088         {
1089           printk ("aic7xxx: Target %d, channel %c, requests %sMHz transfers, "
1090                   "but adapter in Ultra mode can only sync at 7.2MHz or "
1091                   "above.\n", target, channel, aic7xxx_syncrates[i].english);
1092           break;  /* Use asynchronous transfers. */
1093         }
1094       }
1095       else
1096       {
1097         /*
1098          * Check for an Ultra device trying to negotiate an Ultra rate
1099          * on an adapter with Ultra mode disabled.
1100          */
1101         if (aic7xxx_syncrates[i].rate & ULTRA_SXFR)
1102         {
1103           /*
1104            * This should only happen if the driver is the first to negotiate
1105            * and chooses a high rate.  We'll just move down the table until
1106            * we hit a non Ultra speed.
1107            */
1108            continue;
1109         }
1110       }
1111       *scsirate = (aic7xxx_syncrates[i].rate) | (offset & 0x0F);
1112       printk("aic7xxx: Target %d, channel %c, now synchronous at %sMHz, "
1113              "offset(0x%x).\n",
1114              target, channel, aic7xxx_syncrates[i].english, offset);
1115       return;
1116     }
1117   }
1118 
1119   /*
1120    * Default to asynchronous transfer
1121    */
1122   *scsirate = 0;
1123   printk("aic7xxx: Target %d, channel %c, using asynchronous transfers.\n",
1124          target, channel);
1125 }
1126 
1127 /*+F*************************************************************************
1128  * Function:
1129  *   aic7xxx_putscb
1130  *
1131  * Description:
1132  *   Transfer a SCB to the controller.
1133  *-F*************************************************************************/
1134 static inline void
1135 aic7xxx_putscb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
     /* [previous][next][first][last][top][bottom][index][help] */
1136 {
1137   unsigned char curscb;
1138   int base = p->base;
1139 
1140   curscb = inb(SCBPTR + base);
1141   outb(scb->position, SCBPTR + base);
1142   outb(SCBAUTO, SCBCNT + base);
1143 
1144   /*
1145    * By turning on the SCB auto increment, any reference
1146    * to the SCB I/O space postincrements the SCB address
1147    * we're looking at. So turn this on and dump the relevant
1148    * portion of the SCB to the card.
1149    *
1150    * We can do 16bit transfers on all but 284x.
1151    */
1152   if (p->type == AIC_284x)
1153   {
1154     outsb(SCBARRAY + base, scb, SCB_PIO_TRANSFER_SIZE);
1155   }
1156   else
1157   {
1158     outsl(SCBARRAY + base, scb, (SCB_PIO_TRANSFER_SIZE + 3) / 4);
1159   }
1160 
1161   outb(0, SCBCNT + base);
1162   outb(curscb, SCBPTR + base);
1163 }
1164 
1165 /*+F*************************************************************************
1166  * Function:
1167  *   aic7xxx_getscb
1168  *
1169  * Description:
1170  *   Get a SCB from the controller.
1171  *-F*************************************************************************/
1172 static inline void
1173 aic7xxx_getscb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
     /* [previous][next][first][last][top][bottom][index][help] */
1174 {
1175   int base = p->base;
1176 
1177   /*
1178    * This is almost identical to aic7xxx_putscb().
1179    */
1180   outb(SCBAUTO, SCBCNT + base);
1181   insb(SCBARRAY + base, scb, SCB_PIO_TRANSFER_SIZE);
1182   outb(0, SCBCNT + base);
1183 }
1184 
1185 /*+F*************************************************************************
1186  * Function:
1187  *   aic7xxx_match_scb
1188  *
1189  * Description:
1190  *   Checks to see if an scb matches the target/channel as specified.
1191  *   If target is ALL_TARGETS (-1), then we're looking for any device
1192  *   on the specified channel; this happens when a channel is going
1193  *   to be reset and all devices on that channel must be aborted.
1194  *-F*************************************************************************/
1195 static int
1196 aic7xxx_match_scb(struct aic7xxx_scb *scb, int target, char channel)
     /* [previous][next][first][last][top][bottom][index][help] */
1197 {
1198   int targ = (scb->target_channel_lun >> 4) & 0x0F;
1199   char chan = (scb->target_channel_lun & SELBUSB) ? 'B' : 'A';
1200 
1201 #ifdef AIC7XXX_DEBUG_ABORT
1202   printk ("aic7xxx: (match_scb) comparing target/channel %d/%c to scb %d/%c\n",
1203           target, channel, targ, chan);
1204 #endif
1205   if (target == ALL_TARGETS)
1206   {
1207     return (chan == channel);
1208   }
1209   else
1210   {
1211     return ((chan == channel) && (targ == target));
1212   }
1213 }
1214 
1215 /*+F*************************************************************************
1216  * Function:
1217  *   aic7xxx_busy_target
1218  *
1219  * Description:
1220  *   Set the specified target active.
1221  *-F*************************************************************************/
1222 static void
1223 aic7xxx_busy_target(unsigned char target, char channel, int base)
     /* [previous][next][first][last][top][bottom][index][help] */
1224 {
1225   unsigned char active;
1226   unsigned long active_port = ACTIVE_A + base;
1227 
1228   if ((target > 0x07) || (channel == 'B'))
1229   {
1230     /*
1231      * targets on the Second channel or above id 7 store info in byte two
1232      * of ACTIVE
1233      */
1234     active_port++;
1235   }
1236   active = inb(active_port);
1237   active |= (0x01 << (target & 0x07));
1238   outb(active, active_port);
1239 }
1240 
1241 /*+F*************************************************************************
1242  * Function:
1243  *   aic7xxx_unbusy_target
1244  *
1245  * Description:
1246  *   Set the specified target inactive.
1247  *-F*************************************************************************/
1248 static void
1249 aic7xxx_unbusy_target(unsigned char target, char channel, int base)
     /* [previous][next][first][last][top][bottom][index][help] */
1250 {
1251   unsigned char active;
1252   unsigned long active_port = ACTIVE_A + base;
1253 
1254 #ifdef 0
1255   printk ("aic7xxx: (unbusy_target) target/channel %d/%c\n",
1256           target, channel);
1257 #endif
1258   if ((target > 0x07) || (channel == 'B'))
1259   {
1260     /*
1261      * targets on the Second channel or above id 7 store info in byte two
1262      * of ACTIVE
1263      */
1264     active_port++;
1265   }
1266   active = inb(active_port);
1267   active &= ~(0x01 << (target & 0x07));
1268   outb(active, active_port);
1269 }
1270 
1271 /*+F*************************************************************************
1272  * Function:
1273  *   aic7xxx_done
1274  *
1275  * Description:
1276  *   Calls the higher level scsi done function and frees the scb.
1277  *-F*************************************************************************/
1278 static void
1279 aic7xxx_done(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
     /* [previous][next][first][last][top][bottom][index][help] */
1280 {
1281   long flags;
1282   Scsi_Cmnd *cmd = scb->cmd;
1283 
1284 #ifdef 0
1285   printk ("aic7xxx: (done) target/channel %d/%d\n",
1286           cmd->target, cmd->channel);
1287 #endif
1288   /*
1289    * This is a critical section, since we don't want the
1290    * queue routine mucking with the host data.
1291    */
1292   save_flags(flags);
1293   cli();
1294 
1295   /*
1296    * Process the command after marking the scb as free
1297    * and adding it to the free list.
1298    */
1299   scb->state = SCB_FREE;
1300   scb->next = p->free_scb;
1301   p->free_scb = scb;
1302   scb->cmd = NULL;
1303 
1304   restore_flags(flags);
1305   cmd->scsi_done(cmd);
1306 }
1307 
1308 /*+F*************************************************************************
1309  * Function:
1310  *   aic7xxx_done_aborted_scbs
1311  *
1312  * Description:
1313  *   Calls the scsi_done() for the Scsi_Cmnd of each scb in the
1314  *   aborted list, and adds each scb to the free list.
1315  *-F*************************************************************************/
1316 static void
1317 aic7xxx_done_aborted_scbs (struct aic7xxx_host *p)
     /* [previous][next][first][last][top][bottom][index][help] */
1318 {
1319   Scsi_Cmnd *cmd;
1320   struct aic7xxx_scb *scb;
1321   int i;
1322 
1323 #ifdef AIC7XXX_DEBUG_ABORT
1324   int scb_aborted = 0;
1325   printk("aic7xxx: (done_aborted_scbs) calling scsi_done() for aborted scbs\n");
1326 #endif
1327 
1328   for (i = 0; i < p->numscb; i++)
1329   {
1330     scb = &(p->scb_array[i]);
1331     if (scb->state & SCB_QUEUED_FOR_DONE)
1332     {
1333 #ifdef AIC7XXX_DEBUG_ABORT
1334       if (scb_aborted == 0)
1335       {
1336         printk("aic7xxx: (done_aborted_scbs) Aborting scb %d", scb->position);
1337         scb_aborted++;
1338       }
1339       else
1340       {
1341         printk(", %d", scb->position);
1342       }
1343 #endif
1344       /*
1345        * Process the command after marking the scb as free
1346        * and adding it to the free list.
1347        */
1348       scb->state = SCB_FREE;
1349       scb->next = p->free_scb;
1350       p->free_scb = scb;
1351       cmd = scb->cmd;
1352       scb->cmd = NULL;
1353       cmd->scsi_done(cmd);  /* call the done function */
1354     }
1355   }
1356 #ifdef AIC7XXX_DEBUG_ABORT
1357   if (scb_aborted != 0)
1358     printk("\n");
1359 #endif
1360 }
1361 
1362 /*+F*************************************************************************
1363  * Function:
1364  *   aic7xxx_add_waiting_scb
1365  *
1366  * Description:
1367  *   Add this SCB to the head of the "waiting for selection" list.
1368  *-F*************************************************************************/
1369 static void
1370 aic7xxx_add_waiting_scb(u_long              base,
     /* [previous][next][first][last][top][bottom][index][help] */
1371                         struct aic7xxx_scb *scb)
1372 {
1373   unsigned char next;
1374   unsigned char curscb;
1375 
1376   curscb = inb(SCBPTR + base);
1377   next = inb(WAITING_SCBH + base);
1378 
1379   outb(scb->position, SCBPTR + base);
1380   outb(next, SCB_NEXT_WAITING + base);
1381   outb(scb->position, WAITING_SCBH + base);
1382 
1383   outb(curscb, SCBPTR + base);
1384 }
1385 
1386 /*+F*************************************************************************
1387  * Function:
1388  *   aic7xxx_abort_waiting_scb
1389  *
1390  * Description:
1391  *   Manipulate the waiting for selection list and return the
1392  *   scb that follows the one that we remove.
1393  *-F*************************************************************************/
1394 static unsigned char
1395 aic7xxx_abort_waiting_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb,
     /* [previous][next][first][last][top][bottom][index][help] */
1396                           unsigned char prev, unsigned char timedout_scb)
1397 {
1398   unsigned char curscb, next;
1399   int target = (scb->target_channel_lun >> 4) & 0x0F;
1400   char channel = (scb->target_channel_lun & SELBUSB) ? 'B' : 'A';
1401   int base = p->base;
1402 
1403   /*
1404    * Select the SCB we want to abort and pull the next pointer out of it.
1405    */
1406   curscb = inb(SCBPTR + base);
1407   outb(scb->position, SCBPTR + base);
1408   next = inb(SCB_NEXT_WAITING + base);
1409 
1410   /*
1411    * Clear the necessary fields
1412    */
1413   outb(0, SCB_CONTROL + base);
1414   outb(SCB_LIST_NULL, SCB_NEXT_WAITING + base);
1415   aic7xxx_unbusy_target(target, channel, base);
1416 
1417   /*
1418    * Update the waiting list
1419    */
1420   if (prev == SCB_LIST_NULL)
1421   {
1422     /*
1423      * First in the list
1424      */
1425     outb(next, WAITING_SCBH + base);
1426   }
1427   else
1428   {
1429     /*
1430      * Select the scb that pointed to us and update its next pointer.
1431      */
1432     outb(prev, SCBPTR + base);
1433     outb(next, SCB_NEXT_WAITING + base);
1434   }
1435   /*
1436    * Point us back at the original scb position and inform the SCSI
1437    * system that the command has been aborted.
1438    */
1439   outb(curscb, SCBPTR + base);
1440   scb->state |= SCB_ABORTED | SCB_QUEUED_FOR_DONE;
1441   scb->cmd->result = (DID_RESET << 16);
1442 
1443 #ifdef AIC7XXX_DEBUG_ABORT
1444   printk ("aic7xxx: (abort_waiting_scb) target/channel %d/%c, prev %d, "
1445           "to_scb %d, next %d\n", target, channel, prev, timedout_scb, next);
1446 #endif
1447   return (next);
1448 }
1449 
1450 /*+F*************************************************************************
1451  * Function:
1452  *   aic7xxx_reset_device
1453  *
1454  * Description:
1455  *   The device at the given target/channel has been reset.  Abort
1456  *   all active and queued scbs for that target/channel.
1457  *-F*************************************************************************/
1458 static int
1459 aic7xxx_reset_device(struct aic7xxx_host *p, int target, char channel,
     /* [previous][next][first][last][top][bottom][index][help] */
1460                      unsigned char timedout_scb)
1461 {
1462   int base = p->base;
1463   struct aic7xxx_scb *scb;
1464   unsigned char active_scb;
1465   int i = 0;
1466   int found = 0;
1467 
1468   /*
1469    * Restore this when we're done
1470    */
1471   active_scb = inb(SCBPTR + base);
1472 
1473 #ifdef AIC7XXX_DEBUG_ABORT
1474   printk ("aic7xxx: (reset_device) target/channel %d/%c, to_scb %d, "
1475           "active_scb %d\n", target, channel, timedout_scb, active_scb);
1476 #endif
1477   /*
1478    * Search the QINFIFO.
1479    */
1480   {
1481     int saved_queue[AIC7XXX_MAXSCB];
1482     int queued = inb(QINCNT + base);
1483 
1484 #ifdef AIC7XXX_DEBUG_ABORT
1485     if (queued)
1486       printk ("aic7xxx: (reset_device) found %d SCBs in queue\n", queued);
1487 #endif
1488     for (i = 0; i < (queued - found); i++)
1489     {
1490       saved_queue[i] = inb(QINFIFO + base);
1491       scb = &(p->scb_array[saved_queue[i]]);
1492       if (aic7xxx_match_scb(scb, target, channel))
1493       {
1494         /*
1495          * We found an scb that needs to be aborted.
1496          */
1497 #ifdef AIC7XXX_DEBUG_ABORT
1498         printk ("aic7xxx: (reset_device) aborting SCB %d\n", saved_queue[i]);
1499 #endif
1500         scb->state |= SCB_ABORTED | SCB_QUEUED_FOR_DONE;
1501         scb->cmd->result = (DID_RESET << 16);
1502         outb(scb->position, SCBPTR + base);
1503         outb(0, SCBARRAY + base);
1504         i--;
1505         found++;
1506       }
1507     }
1508     /*
1509      * Now put the saved scbs back.
1510      */
1511     for (queued = 0; queued < i; queued++)
1512     {
1513       outb(saved_queue[queued], QINFIFO + base);
1514     }
1515   }
1516 
1517   /*
1518    * Search waiting for selection list.
1519    */
1520   {
1521     unsigned char next, prev;
1522 
1523     next = inb(WAITING_SCBH + base);  /* Start at head of list. */
1524     prev = SCB_LIST_NULL;
1525 
1526 #ifdef AIC7XXX_DEBUG_ABORT
1527   printk ("aic7xxx: (reset_device) Searching waiting SCBs, head %d\n", next);
1528 #endif
1529     while (next != SCB_LIST_NULL)
1530     {
1531       scb = &(p->scb_array[next]);
1532       /*
1533        * Select the SCB.
1534        */
1535       if (aic7xxx_match_scb(scb, target, channel))
1536       {
1537         next = aic7xxx_abort_waiting_scb(p, scb, prev, timedout_scb);
1538         found++;
1539       }
1540       else
1541       {
1542         outb(scb->position, SCBPTR + base);
1543         prev = next;
1544         next = inb(SCB_NEXT_WAITING + base);
1545       }
1546     }
1547   }
1548 
1549   /*
1550    * Go through the entire SCB array now and look for commands for
1551    * for this target that are active.  These are other (most likely
1552    * tagged) commands that were disconnected when the reset occurred.
1553    */
1554   for (i = 0; i < p->numscb; i++)
1555   {
1556     scb = &(p->scb_array[i]);
1557     if ((scb->state & SCB_ACTIVE) && aic7xxx_match_scb(scb, target, channel))
1558     {
1559       /*
1560        * Ensure the target is "free"
1561        */
1562 #ifdef AIC7XXX_DEBUG_ABORT
1563   printk ("aic7xxx: (reset_device) freeing disconnected SCB %d\n", i);
1564 #endif
1565       aic7xxx_unbusy_target(target, channel, base);
1566       outb(scb->position, SCBPTR + base);
1567       outb(0, SCBARRAY + base);
1568       scb->state |= SCB_ABORTED | SCB_QUEUED_FOR_DONE;
1569       scb->cmd->result = (DID_RESET << 16);
1570       found++;
1571     }
1572   }
1573 
1574   outb(active_scb, SCBPTR + base);
1575   return (found);
1576 }
1577 
1578 /*+F*************************************************************************
1579  * Function:
1580  *   aic7xxx_reset_current_bus
1581  *
1582  * Description:
1583  *   Reset the current SCSI bus.
1584  *-F*************************************************************************/
1585 static void
1586 aic7xxx_reset_current_bus(int base)
     /* [previous][next][first][last][top][bottom][index][help] */
1587 {
1588 #ifdef AIC7XXX_DEBUG_ABORT
1589     printk ("aic7xxx: (reset_current_bus)\n");
1590 #endif
1591   outb(SCSIRSTO, SCSISEQ + base);
1592   udelay(1000);
1593   outb(0, SCSISEQ + base);
1594 }
1595 
1596 /*+F*************************************************************************
1597  * Function:
1598  *   aic7xxx_reset_channel
1599  *
1600  * Description:
1601  *   Reset the channel.
1602  *-F*************************************************************************/
1603 static int
1604 aic7xxx_reset_channel(struct aic7xxx_host *p, char channel,
     /* [previous][next][first][last][top][bottom][index][help] */
1605                      unsigned char timedout_scb, int initiate_reset)
1606 {
1607   int base = p->base;
1608   unsigned char sblkctl;
1609   char cur_channel;
1610   unsigned long offset, offset_max;
1611   int found;
1612 
1613 #ifdef AIC7XXX_DEBUG_ABORT
1614   printk ("aic7xxx: (reset_channel) channel %c, to_scb %d\n",
1615           channel, timedout_scb);
1616 #endif
1617   /*
1618    * Clean up all the state information for the
1619    * pending transactions on this bus.
1620    */
1621   found = aic7xxx_reset_device(p, ALL_TARGETS, channel, timedout_scb);
1622 
1623   if (channel == 'B')
1624   {
1625     p->needsdtr |= (p->needsdtr_copy & 0xFF00);
1626     p->sdtr_pending &= 0x00FF;
1627     outb(0, ACTIVE_B + base);
1628     offset = TARG_SCRATCH + base + 8;
1629     offset_max = TARG_SCRATCH + base + 16;
1630   }
1631   else
1632   {
1633     if (p->bus_type == AIC_WIDE)
1634     {
1635       p->needsdtr = p->needsdtr_copy;
1636       p->needwdtr = p->needwdtr_copy;
1637       p->sdtr_pending = 0x0;
1638       p->wdtr_pending = 0x0;
1639       outb(0, ACTIVE_A + base);
1640       outb(0, ACTIVE_B + base);
1641       offset = TARG_SCRATCH + base;
1642       offset_max = TARG_SCRATCH + base + 16;
1643     }
1644     else
1645     {
1646       p->needsdtr |= (p->needsdtr_copy & 0x00FF);
1647       p->sdtr_pending &= 0xFF00;
1648       outb(0, ACTIVE_A + base);
1649       offset = TARG_SCRATCH + base;
1650       offset_max = TARG_SCRATCH + base + 8;
1651     }
1652   }
1653   while (offset < offset_max)
1654   {
1655     /*
1656      * Revert to async/narrow transfers
1657      * until we renegotiate.
1658      */
1659     u_char targ_scratch;
1660     targ_scratch = inb(offset);
1661     targ_scratch &= SXFR;
1662     outb(targ_scratch, offset);
1663     offset++;
1664   }
1665 
1666   /*
1667    * Reset the bus and unpause/restart the controller
1668    */
1669 
1670   /*
1671    * Case 1: Command for another bus is active
1672    */
1673   sblkctl = inb(SBLKCTL + base);
1674   cur_channel = (sblkctl & SELBUSB) ? 'B' : 'A';
1675   if (cur_channel != channel)
1676   {
1677 #ifdef AIC7XXX_DEBUG_ABORT
1678     printk ("aic7xxx: (reset_channel) Stealthily resetting channel %c\n",
1679             channel);
1680 #endif
1681     /*
1682      * Stealthily reset the other bus without upsetting the current bus
1683      */
1684     outb(sblkctl ^ SELBUSB, SBLKCTL + base);
1685     if (initiate_reset)
1686     {
1687       aic7xxx_reset_current_bus(base);
1688     }
1689     outb(sblkctl, SBLKCTL + base);
1690 
1691     UNPAUSE_SEQUENCER(p);
1692   }
1693   /*
1694    * Case 2: A command from this bus is active or we're idle
1695    */
1696   else
1697   {
1698 #ifdef AIC7XXX_DEBUG_ABORT
1699     printk ("aic7xxx: (reset_channel) Resetting current channel %c\n",
1700             channel);
1701 #endif
1702     if (initiate_reset)
1703     {
1704       aic7xxx_reset_current_bus(base);
1705     }
1706     RESTART_SEQUENCER(p);
1707 #ifdef AIC7XXX_DEBUG_ABORT
1708     printk ("aic7xxx: (reset_channel) Channel reset, sequencer restarted\n");
1709 #endif
1710   }
1711 
1712   /*
1713    * Set the time of last reset.
1714    */
1715   p->host->last_reset = jiffies;
1716 
1717   /*
1718    * Now loop through all the SCBs that have been marked for abortion,
1719    * and call the scsi_done routines.
1720    */
1721   aic7xxx_done_aborted_scbs(p);
1722   return found;
1723 }
1724 
1725 /*+F*************************************************************************
1726  * Function:
1727  *   aic7xxx_isr
1728  *
1729  * Description:
1730  *   SCSI controller interrupt handler.
1731  *
1732  *   NOTE: Since we declared this using SA_INTERRUPT, interrupts should
1733  *         be disabled all through this function unless we say otherwise.
1734  *-F*************************************************************************/
1735 static void
1736 aic7xxx_isr(int irq, void *dev_id, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1737 {
1738   int base, intstat;
1739   struct aic7xxx_host *p;
1740   struct aic7xxx_scb *scb;
1741   unsigned char ha_flags;
1742   short         transfer;
1743   unsigned char scsi_id, bus_width;
1744   unsigned char offset, rate, scratch, scratch_offset;
1745   unsigned char max_offset, rej_byte;
1746   unsigned short target_mask;
1747   char channel;
1748   void *addr;
1749   int actual;
1750   int scb_index;
1751   Scsi_Cmnd *cmd;
1752 
1753   p = (struct aic7xxx_host *) aic7xxx_boards[irq]->hostdata;
1754 
1755   /*
1756    * Search for the host with a pending interrupt.  If we can't find
1757    * one, then we've encountered a spurious interrupt.
1758    */
1759   while ((p != NULL) && !(inb(INTSTAT + p->base) & INT_PEND))
1760   {
1761     if (p->next == NULL)
1762     {
1763       p = NULL;
1764     }
1765     else
1766     {
1767       p = (struct aic7xxx_host *) p->next->hostdata;
1768     }
1769   }
1770 
1771   if (p == NULL)
1772   {
1773     if (aic7xxx_spurious_count == 1)
1774     {
1775       aic7xxx_spurious_count = 2;
1776       printk("aic7xxx: (aic7xxx_isr) Encountered spurious interrupt.\n");
1777       return;
1778     }
1779     else
1780     {
1781       /*
1782        * The best we can do is to set p back to head of list and process
1783        * the erroneous interrupt - most likely a BRKADRINT.
1784        */
1785       p = (struct aic7xxx_host *) aic7xxx_boards[irq]->hostdata;
1786     }
1787   }
1788 
1789   /*
1790    * Keep track of interrupts for /proc/scsi
1791    */
1792   p->isr_count++;
1793 
1794   if (!p->a_scanned && (p->isr_count == 1))
1795   {
1796     /*
1797      * We must only have one card at this IRQ and it must have been
1798      * added to the board data before the spurious interrupt occurred.
1799      * It is sufficient that we check isr_count and not the spurious
1800      * interrupt count.
1801      */
1802     printk("aic7xxx: (aic7xxx_isr) Encountered spurious interrupt.\n");
1803     return;
1804   }
1805 
1806   base = p->base;
1807   /*
1808    * Handle all the interrupt sources - especially for SCSI
1809    * interrupts, we won't get a second chance at them.
1810    */
1811   intstat = inb(INTSTAT + base);
1812 
1813   if (intstat & BRKADRINT)
1814   {
1815     int i;
1816     unsigned char errno = inb(ERROR + base);
1817 
1818     printk("aic7xxx: (aic7xxx_isr) BRKADRINT error(0x%x):\n", errno);
1819     for (i = 0; i < NUMBER(hard_error); i++)
1820     {
1821       if (errno & hard_error[i].errno)
1822       {
1823         printk("  %s\n", hard_error[i].errmesg);
1824       }
1825     }
1826     panic("aic7xxx: (aic7xxx_isr) BRKADRINT, error(0x%x) seqaddr(0x%x).\n",
1827           inb(ERROR + base), (inb(SEQADDR1 + base) << 8) | inb(SEQADDR0 + base));
1828   }
1829 
1830   if (intstat & SEQINT)
1831   {
1832     /*
1833      * Although the sequencer is paused immediately on
1834      * a SEQINT, an interrupt for a SCSIINT condition will
1835      * unpaused the sequencer before this point.
1836      */
1837     PAUSE_SEQUENCER(p);
1838 
1839     scsi_id = (inb(SCSIID + base) >> 4) & 0x0F;
1840     scratch_offset = scsi_id;
1841     channel = 'A';
1842     if (inb(SBLKCTL + base) & SELBUSB)
1843     {
1844       channel = 'B';
1845       scratch_offset += 8;
1846     }
1847     target_mask = (0x01 << scratch_offset);
1848 
1849     switch (intstat & SEQINT_MASK)
1850     {
1851       case BAD_PHASE:
1852         panic("aic7xxx: (aic7xxx_isr) Unknown scsi bus phase.\n");
1853         break;
1854 
1855       case SEND_REJECT:
1856         rej_byte = inb(REJBYTE + base);
1857         if ((rej_byte & 0xF0) == 0x20)
1858         {
1859           scb_index = inb(SCBPTR + base);
1860           scb = &(p->scb_array[scb_index]);
1861           printk("aic7xxx: Warning - Tagged message received without identify."
1862                  "Disabling tagged commands for target %d channel %c.\n",
1863                   scsi_id, channel);
1864           scb->cmd->device->tagged_supported = 0;
1865           scb->cmd->device->tagged_queue = 0;
1866         }
1867         else
1868         {
1869           debug("aic7xxx: Warning - Rejecting unknown message (0x%x) received "
1870                 "from target %d channel %c.\n", rej_byte, scsi_id, channel);
1871         }
1872         break;
1873 
1874       case NO_IDENT:
1875         panic("aic7xxx: Target %d, channel %c, did not send an IDENTIFY "
1876               "message. SAVED_TCL(0x%x).\n",
1877               scsi_id, channel, inb(SAVED_TCL + base));
1878         break;
1879 
1880       case NO_MATCH:
1881         printk("aic7xxx: No active SCB for reconnecting target %d, "
1882               "channel %c - Issuing ABORT. SAVED_TCL(0x%x).\n",
1883               scsi_id, channel, inb(SAVED_TCL + base));
1884         aic7xxx_unbusy_target(scsi_id, channel, base);
1885         outb(0, SCBARRAY + base);
1886         outb(CLRSELTIMEO, CLRSINT1 + base);
1887         RESTART_SEQUENCER(p);
1888         break;
1889 
1890       case SDTR_MSG:
1891         /*
1892          * Help the sequencer to translate the negotiated
1893          * transfer rate. Transfer is 1/4 the period
1894          * in ns as is returned by the sync negotiation
1895          * message. So, we must multiply by four.
1896          */
1897         transfer = (inb(ARG_1 + base) << 2);
1898         offset = inb(ACCUM + base);
1899         scratch = inb(TARG_SCRATCH + base + scratch_offset);
1900         /*
1901          * The maximum offset for a wide device is 0x08; for a
1902          * 8-bit bus device the maximum offset is 0x0F.
1903          */
1904         if (scratch & WIDEXFER)
1905         {
1906           max_offset = 0x08;
1907         }
1908         else
1909         {
1910           max_offset = 0x0F;
1911         }
1912         aic7xxx_scsirate(p, &rate, transfer, MIN(offset, max_offset),
1913                          scsi_id, channel);
1914         /*
1915          * Preserve the wide transfer flag.
1916          */
1917         scratch = rate | (scratch & WIDEXFER);
1918         outb(scratch, TARG_SCRATCH + base + scratch_offset);
1919         outb(scratch, SCSIRATE + base);
1920         if ((scratch & 0x0F) == 0)
1921         { /*
1922            * The requested rate was so low that asynchronous transfers
1923            * are faster (not to mention the controller won't support
1924            * them), so we issue a reject to ensure we go to asynchronous
1925            * transfers.
1926            */
1927            outb(SEND_REJ, RETURN_1 + base);
1928         }
1929         else
1930         {
1931           /*
1932            * See if we initiated Sync Negotiation
1933            */
1934           if (p->sdtr_pending & target_mask)
1935           {
1936             /*
1937              * Don't send an SDTR back to the target.
1938              */
1939             outb(0, RETURN_1 + base);
1940           }
1941           else
1942           {
1943             /*
1944              * Send our own SDTR in reply.
1945              */
1946             printk("aic7xxx: Sending SDTR!!\n");
1947             outb(SEND_SDTR, RETURN_1 + base);
1948           }
1949         }
1950         /*
1951          * Clear the flags.
1952          */
1953         p->needsdtr &= ~target_mask;
1954         p->sdtr_pending &= ~target_mask;
1955         break;
1956 
1957       case WDTR_MSG:
1958       {
1959         bus_width = inb(ARG_1 + base);
1960         printk("aic7xxx: Received MSG_WDTR, Target %d, channel %c "
1961                "needwdtr(0x%x).\n", scsi_id, channel, p->needwdtr);
1962         scratch = inb(TARG_SCRATCH + base + scratch_offset);
1963 
1964         if (p->wdtr_pending & target_mask)
1965         {
1966           /*
1967            * Don't send an WDTR back to the target, since we asked first.
1968            */
1969           outb(0, RETURN_1 + base);
1970           switch (bus_width)
1971           {
1972             case BUS_8_BIT:
1973               scratch &= 0x7F;
1974               break;
1975 
1976             case BUS_16_BIT:
1977               printk("aic7xxx: Target %d, channel %c, using 16 bit "
1978                      "transfers.\n", scsi_id, channel);
1979               scratch |= 0x80;
1980               break;
1981 
1982             case BUS_32_BIT:
1983               outb(SEND_REJ, RETURN_1 + base);
1984               printk("aic7xxx: Target %d, channel %c, requesting 32 bit "
1985                      "transfers, rejecting...\n", scsi_id, channel);
1986               break;
1987           }
1988         }
1989         else
1990         {
1991           /*
1992            * Send our own WDTR in reply.
1993            */
1994           printk("aic7xxx: Will send WDTR!!\n");
1995           switch (bus_width)
1996           {
1997             case BUS_8_BIT:
1998               scratch &= 0x7F;
1999               break;
2000 
2001             case BUS_32_BIT:
2002               /*
2003                * Negotiate 16 bits.
2004                */
2005               bus_width = BUS_16_BIT;
2006               /* Yes, we mean to fall thru here. */
2007 
2008             case BUS_16_BIT:
2009               printk("aic7xxx: Target %d, channel %c, using 16 bit "
2010                      "transfers.\n", scsi_id, channel);
2011               scratch |= 0x80;
2012               break;
2013           }
2014           outb(bus_width | SEND_WDTR, RETURN_1 + base);
2015         }
2016         p->needwdtr &= ~target_mask;
2017         p->wdtr_pending &= ~target_mask;
2018         outb(scratch, TARG_SCRATCH + base + scratch_offset);
2019         outb(scratch, SCSIRATE + base);
2020         break;
2021       }
2022 
2023       case REJECT_MSG:
2024       {
2025         /*
2026          * What we care about here is if we had an
2027          * outstanding SDTR or WDTR message for this
2028          * target. If we did, this is a signal that
2029          * the target is refusing negotiation.
2030          */
2031 
2032         scratch = inb(TARG_SCRATCH + base + scratch_offset);
2033 
2034         if (p->wdtr_pending & target_mask)
2035         {
2036           /*
2037            * note 8bit xfers and clear flag
2038            */
2039           scratch &= 0x7F;
2040           p->needwdtr &= ~target_mask;
2041           p->wdtr_pending &= ~target_mask;
2042           printk("aic7xxx: Target %d, channel %c, refusing WIDE negotiation. "
2043                  "Using 8 bit transfers.\n", scsi_id, channel);
2044         }
2045         else
2046         {
2047           if (p->sdtr_pending & target_mask)
2048           {
2049             /*
2050              * note asynch xfers and clear flag
2051              */
2052             scratch &= 0xF0;
2053             p->needsdtr &= ~target_mask;
2054             p->sdtr_pending &= ~target_mask;
2055             printk("aic7xxx: Target %d, channel %c, refusing synchronous "
2056                    "negotiation. Using asynchronous transfers.\n",
2057                    scsi_id, channel);
2058           }
2059           /*
2060            * Otherwise, we ignore it.
2061            */
2062         }
2063         outb(scratch, TARG_SCRATCH + base + scratch_offset);
2064         outb(scratch, SCSIRATE + base);
2065         break;
2066       }
2067 
2068       case BAD_STATUS:
2069         scb_index = inb(SCBPTR + base);
2070         scb = &(p->scb_array[scb_index]);
2071         outb(0, RETURN_1 + base);   /* CHECK_CONDITION may change this */
2072         if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2073         {
2074           printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
2075                  "scb(%d) state(0x%x) cmd(0x%x).\n",
2076                  intstat, scb_index, scb->state, (unsigned int) scb->cmd);
2077         }
2078         else
2079         {
2080           cmd = scb->cmd;
2081           aic7xxx_getscb(p, scb);
2082           aic7xxx_status(cmd) = scb->target_status;
2083 
2084           cmd->result |= scb->target_status;
2085 
2086           switch (status_byte(scb->target_status))
2087           {
2088             case GOOD:
2089               printk("aic7xxx: Interrupted for status of GOOD???\n");
2090               break;
2091 
2092             case CHECK_CONDITION:
2093               if ((aic7xxx_error(cmd) == 0) && !(cmd->flags & WAS_SENSE))
2094               {
2095                 unsigned char tcl;
2096                 unsigned char control;
2097                 void         *req_buf;
2098 
2099                 tcl = scb->target_channel_lun;
2100 
2101                 /*
2102                  * Send a sense command to the requesting target.
2103                  */
2104                 cmd->flags |= WAS_SENSE;
2105                 memcpy((void *) scb->sense_cmd, (void *) generic_sense,
2106                        sizeof(generic_sense));
2107 
2108                 scb->sense_cmd[1] = (cmd->lun << 5);
2109                 scb->sense_cmd[4] = sizeof(cmd->sense_buffer);
2110 
2111                 scb->sense_sg.address = (char *) &cmd->sense_buffer;
2112                 scb->sense_sg.length = sizeof(cmd->sense_buffer);
2113                 req_buf = &scb->sense_sg;
2114                 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
2115                 control = scb->control;
2116 
2117                 memset(scb, 0, SCB_PIO_TRANSFER_SIZE);
2118                 scb->control = control & DISCENB;
2119                 scb->target_channel_lun = tcl;
2120                 addr = scb->sense_cmd;
2121                 scb->SCSI_cmd_length = COMMAND_SIZE(scb->sense_cmd[0]);
2122                 memcpy(scb->SCSI_cmd_pointer, &addr,
2123                        sizeof(scb->SCSI_cmd_pointer));
2124                 scb->SG_segment_count = 1;
2125                 memcpy(scb->SG_list_pointer, &req_buf,
2126                         sizeof(scb->SG_list_pointer));
2127                 scb->data_count = scb->sense_sg.length;
2128                 memcpy(scb->data_pointer, &(scb->sense_sg.address), 4);
2129 
2130                 aic7xxx_putscb(p, scb);
2131                 outb(SCB_LIST_NULL, SCB_NEXT_WAITING + base);
2132                 /*
2133                  * Ensure that the target is "BUSY" so we don't get overlapping
2134                  * commands if we happen to be doing tagged I/O.
2135                  */
2136                 aic7xxx_busy_target(scsi_id, channel, base);
2137 
2138                 aic7xxx_add_waiting_scb(base, scb);
2139                 outb(SEND_SENSE, RETURN_1 + base);
2140               }  /* first time sense, no errors */
2141               else
2142               {
2143                 cmd->flags &= ~ASKED_FOR_SENSE;
2144                 if (aic7xxx_error(cmd) == 0)
2145                 {
2146                   aic7xxx_error(cmd) = DID_RETRY_COMMAND;
2147                 }
2148               }
2149               break;
2150 
2151             case BUSY:
2152               printk("aic7xxx: Target busy.\n");
2153               if (!aic7xxx_error(cmd))
2154               {
2155                 aic7xxx_error(cmd) = DID_BUS_BUSY;
2156               }
2157               break;
2158 
2159             case QUEUE_FULL:
2160               printk("aic7xxx: Queue full.\n");
2161               if (!aic7xxx_error(cmd))
2162               {
2163                 aic7xxx_error(cmd) = DID_RETRY_COMMAND;
2164               }
2165               break;
2166 
2167             default:
2168               printk("aic7xxx: Unexpected target status(0x%x).\n",
2169                      scb->target_status);
2170               if (!aic7xxx_error(cmd))
2171               {
2172                 aic7xxx_error(cmd) = DID_RETRY_COMMAND;
2173               }
2174               break;
2175           }  /* end switch */
2176         }  /* end else of */
2177         break;
2178 
2179       case RESIDUAL:
2180         scb_index = inb(SCBPTR + base);
2181         scb = &(p->scb_array[scb_index]);
2182         if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2183         {
2184           printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
2185                  "scb(%d) state(0x%x) cmd(0x%x).\n",
2186                  intstat, scb_index, scb->state, (unsigned int) scb->cmd);
2187         }
2188         else
2189         {
2190           cmd = scb->cmd;
2191           /*
2192            *  Don't destroy valid residual information with
2193            *  residual coming from a check sense operation.
2194            */
2195           if (!(cmd->flags & WAS_SENSE))
2196           {
2197             /*
2198              *  We had an underflow. At this time, there's only
2199              *  one other driver that bothers to check for this,
2200              *  and cmd->underflow seems to be set rather half-
2201              *  heartedly in the higher-level SCSI code.
2202              */
2203             actual = aic7xxx_length(cmd, scb->residual_SG_segment_count);
2204 
2205             actual -= (inb(SCB_RESID_DCNT2 + base) << 16) |
2206                       (inb(SCB_RESID_DCNT1 + base) <<  8) |
2207                       inb(SCB_RESID_DCNT0 + base);
2208 
2209             if (actual < cmd->underflow)
2210             {
2211               printk("aic7xxx: Target %d underflow - "
2212                      "Wanted (at least) (%u) got(%u) count(%d).\n",
2213                      cmd->target, cmd->underflow, actual,
2214                      inb(SCB_RESID_SGCNT + base));
2215               aic7xxx_error(cmd) = DID_RETRY_COMMAND;
2216               aic7xxx_status(cmd) = scb->target_status;
2217             }
2218           }
2219         }
2220         break;
2221 
2222       case ABORT_TAG:
2223         scb_index = inb(SCBPTR + base);
2224         scb = &(p->scb_array[scb_index]);
2225         if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2226         {
2227           printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
2228                  "scb(%d) state(0x%x) cmd(0x%x)\n",
2229                  intstat, scb_index, scb->state, (unsigned int) scb->cmd);
2230         }
2231         else
2232         {
2233           cmd = scb->cmd;
2234           /*
2235            * We didn't receive a valid tag back from the target
2236            * on a reconnect.
2237            */
2238           printk("aic7xxx: Invalid tag received on target %d, channel %c, "
2239                  "lun %d - Sending ABORT_TAG.\n",
2240                   scsi_id, channel, cmd->lun & 0x07);
2241 
2242           cmd->result = (DID_RETRY_COMMAND << 16);
2243           aic7xxx_done(p, scb);
2244         }
2245         break;
2246 
2247       case AWAITING_MSG:
2248         scb_index = inb(SCBPTR + base);
2249         scb = &(p->scb_array[scb_index]);
2250         if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2251         {
2252           printk("aic7xxx: Referenced SCB not valid during SEQINT(0x%x) "
2253                  "scb(%d) state(0x%x) cmd(0x%x).\n",
2254                  intstat, scb_index, scb->state, (unsigned int) scb->cmd);
2255         }
2256         else
2257         {
2258           /*
2259            * This SCB had a zero length command, informing the sequencer
2260            * that we wanted to send a special message to this target.
2261            * We only do this for BUS_DEVICE_RESET messages currently.
2262            */
2263            if (scb->state & SCB_DEVICE_RESET)
2264            {
2265 #ifdef AIC7XXX_DEBUG_ABORT
2266   printk ("aic7xxx: (isr) sending bus device reset to target %d\n",
2267           scsi_id);
2268 #endif
2269              outb(MSG_BUS_DEVICE_RESET, MSG0 + base);
2270              outb(1, MSG_LEN + base);
2271            }
2272            else
2273            {
2274              panic("aic7xxx: AWAITING_SCB for an SCB that does "
2275                    "not have a waiting message.\n");
2276            }
2277         }
2278         break;
2279 
2280       case IMMEDDONE:
2281         scb_index = inb(SCBPTR + base);
2282         scb = &(p->scb_array[scb_index]);
2283 #ifdef AIC7XXX_DEBUG_ABORT
2284   printk ("aic7xxx: (isr) received IMMEDDONE for target %d, scb %d, state %d\n",
2285           scsi_id, scb_index, scb->state);
2286 #endif
2287         if (scb->state & SCB_DEVICE_RESET)
2288         {
2289           int found;
2290 
2291           /*
2292            * Go back to async/narrow transfers and renegotiate.
2293            */
2294           aic7xxx_unbusy_target(scsi_id, channel, base);
2295           p->needsdtr |= (p->needsdtr_copy & target_mask);
2296           p->needwdtr |= (p->needwdtr_copy & target_mask);
2297           p->sdtr_pending &= ~target_mask;
2298           p->wdtr_pending &= ~target_mask;
2299           scratch = inb(TARG_SCRATCH + base + scratch_offset);
2300           scratch &= SXFR;
2301           outb(scratch, TARG_SCRATCH + base + scratch_offset);
2302           found = aic7xxx_reset_device(p, (int) scsi_id, channel, SCB_LIST_NULL);
2303           aic7xxx_done_aborted_scbs (p);
2304         }
2305         else
2306         {
2307           panic("aic7xxx: Immediate complete for unknown operation.\n");
2308         }
2309         break;
2310 
2311 #if AIC7XXX_NOT_YET
2312       /* XXX Fill these in later */
2313       case MESG_BUFFER_BUSY:
2314         break;
2315       case MSGIN_PHASEMIS:
2316         break;
2317 #endif
2318 
2319       default:               /* unknown */
2320         debug("aic7xxx: SEQINT, INTSTAT(0x%x) SCSISIGI(0x%x).\n",
2321               intstat, inb(SCSISIGI + base));
2322         break;
2323     }
2324 
2325     /*
2326      * Clear the sequencer interrupt and unpause the sequencer.
2327      */
2328     outb(CLRSEQINT, CLRINT + base);
2329     UNPAUSE_SEQUENCER(p);
2330   }
2331 
2332   if (intstat & SCSIINT)
2333   {
2334     int status = inb(SSTAT1 + base);
2335     scsi_id = (inb(SCSIID + base) >> 4) & 0x0F;
2336     channel = 'A';
2337     if (inb(SBLKCTL + base) & SELBUSB)
2338     {
2339       channel = 'B';
2340     }
2341 
2342     scb_index = inb(SCBPTR + base);
2343     scb = &(p->scb_array[scb_index]);
2344     if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2345     {
2346       printk("aic7xxx: No command for SCB (SCSIINT).\n");
2347       /*
2348        * Turn off the interrupt and set status to zero, so that it
2349        * falls through the rest of the SCSIINT code.
2350        */
2351       outb(status, CLRSINT1 + base);
2352       UNPAUSE_SEQUENCER(p);
2353       outb(CLRSCSIINT, CLRINT + base);
2354       scb = NULL;
2355     }
2356     else if (status & SCSIRSTI)
2357     {
2358       PAUSE_SEQUENCER(p);
2359       printk ("aic7xxx: Someone reset channel %c (SCSIINT).\n", channel);
2360       /*
2361        * Go through and abort all commands for the channel, but do not
2362        * reset the channel again.
2363        */
2364       aic7xxx_reset_channel (p, channel, SCB_LIST_NULL, FALSE);
2365     }
2366     else if (status & SCSIPERR)
2367     {
2368       char  *phase;
2369       unsigned char mesg_out = MSG_NOP;
2370       unsigned char lastphase = inb(LASTPHASE + base);
2371 
2372       cmd = scb->cmd;
2373       switch (lastphase)
2374       {
2375         case P_DATAOUT:
2376           phase = "Data-Out";
2377           break;
2378         case P_DATAIN:
2379           phase = "Data-In";
2380           mesg_out = MSG_INITIATOR_DET_ERROR;
2381           break;
2382         case P_COMMAND:
2383           phase = "Command";
2384           break;
2385         case P_MESGOUT:
2386           phase = "Message-Out";
2387           break;
2388         case P_STATUS:
2389           phase = "Status";
2390           mesg_out = MSG_INITIATOR_DET_ERROR;
2391           break;
2392         case P_MESGIN:
2393           phase = "Message-In";
2394           mesg_out = MSG_MSG_PARITY_ERROR;
2395           break;
2396         default:
2397           phase = "unknown";
2398           break;
2399       }
2400 
2401       /*
2402        * A parity error has occurred during a data
2403        * transfer phase. Flag it and continue.
2404        */
2405       printk("aic7xxx: Parity error during phase %s on target %d, "
2406              "channel %d, lun %d.\n", phase,
2407              cmd->target, cmd->channel & 0x01, cmd->lun & 0x07);
2408 
2409       /*
2410        * We've set the hardware to assert ATN if we get a parity
2411        * error on "in" phases, so all we need to do is stuff the
2412        * message buffer with the appropriate message. In phases
2413        * have set mesg_out to something other than MSG_NOP.
2414        */
2415       if (mesg_out != MSG_NOP)
2416       {
2417          outb(mesg_out, MSG0 + base);
2418          outb(1, MSG_LEN + base);
2419          cmd->result = DID_PARITY << 16;
2420       }
2421       else
2422       {
2423          /*
2424           * Should we allow the target to make this decision for us?
2425           */
2426          cmd->result = DID_RETRY_COMMAND << 16;
2427       }
2428       aic7xxx_done(p, scb);
2429     }
2430     else if (status & SELTO)
2431     {
2432       unsigned char waiting;
2433 
2434       cmd = scb->cmd;
2435 
2436      /*
2437        * Hardware selection timer has expired. Turn
2438        * off SCSI selection sequence.
2439        */
2440       outb(ENRSELI, SCSISEQ + base);
2441       cmd->result = (DID_TIME_OUT << 16);
2442       /*
2443        * Clear an pending messages for the timed out
2444        * target and mark the target as free.
2445        */
2446       ha_flags = inb(FLAGS + base);
2447       outb(0, MSG_LEN + base);
2448       aic7xxx_unbusy_target(scsi_id, channel, base);
2449 
2450       outb(0, SCBARRAY + base);
2451 
2452       /*
2453        * Shut off the offending interrupt sources, reset
2454        * the sequencer address to zero and unpause it,
2455        * then call the high-level SCSI completion routine.
2456        *
2457        * WARNING!  This is a magic sequence!  After many
2458        * hours of guesswork, turning off the SCSI interrupts
2459        * in CLRSINT? does NOT clear the SCSIINT bit in
2460        * INTSTAT. By writing to the (undocumented, unused
2461        * according to the AIC-7770 manual) third bit of
2462        * CLRINT, you can clear INTSTAT. But, if you do it
2463        * while the sequencer is paused, you get a BRKADRINT
2464        * with an Illegal Host Address status, so the
2465        * sequencer has to be restarted first.
2466        */
2467       outb(CLRSELTIMEO, CLRSINT1 + base);
2468 
2469       outb(CLRSCSIINT, CLRINT + base);
2470 
2471       /*
2472        * Shift the waiting for selection queue forward
2473        */
2474       waiting = inb(WAITING_SCBH + base);
2475       outb(waiting, SCBPTR + base);
2476       waiting = inb(SCB_NEXT_WAITING + base);
2477       outb(waiting, WAITING_SCBH + base);
2478 
2479       RESTART_SEQUENCER(p);
2480       aic7xxx_done(p, scb);
2481 #if 0
2482 printk("aic7xxx: SELTO SCB(%d) state(0x%x) cmd(0x%x).\n",
2483        scb->position, scb->state, (unsigned int) scb->cmd);
2484 #endif
2485     }
2486     else if (!(status & BUSFREE))
2487     {
2488       /*
2489        * We don't know what's going on. Turn off the
2490        * interrupt source and try to continue.
2491        */
2492       printk("aic7xxx: SSTAT1(0x%x).\n", status);
2493       outb(status, CLRSINT1 + base);
2494       UNPAUSE_SEQUENCER(p);
2495       outb(CLRSCSIINT, CLRINT + base);
2496     }
2497   }
2498 
2499   if (intstat & CMDCMPLT)
2500   {
2501     int complete;
2502 
2503     /*
2504      * The sequencer will continue running when it
2505      * issues this interrupt. There may be >1 commands
2506      * finished, so loop until we've processed them all.
2507      */
2508     do {
2509       complete = inb(QOUTFIFO + base);
2510 
2511       scb = &(p->scb_array[complete]);
2512       if (!(scb->state & SCB_ACTIVE) || (scb->cmd == NULL))
2513       {
2514         printk("aic7xxx: Warning - No command for SCB %d (CMDCMPLT).\n"
2515                "         QOUTCNT(%d) QINCNT(%d) SCB state(0x%x) cmd(0x%x) "
2516                "pos(%d).\n",
2517                complete, inb(QOUTCNT + base), inb(QINCNT + base),
2518                scb->state, (unsigned int) scb->cmd, scb->position);
2519         outb(CLRCMDINT, CLRINT + base);
2520         continue;
2521       }
2522       cmd = scb->cmd;
2523       cmd->result |= (aic7xxx_error(cmd) << 16);
2524       if ((cmd->flags & WAS_SENSE) && !(cmd->flags & ASKED_FOR_SENSE))
2525       {
2526         /*
2527          * Got sense information.
2528          */
2529         cmd->flags &= ASKED_FOR_SENSE;
2530       }
2531 #if 0
2532       printk("aic7xxx: (complete) State(%d) cmd(0x%x) free(0x%x).\n",
2533              scb->state, (unsigned int) scb->cmd, (unsigned int) p->free_scb);
2534 #endif
2535 
2536       /*
2537        * Clear interrupt status before checking
2538        * the output queue again. This eliminates
2539        * a race condition whereby a command could
2540        * complete between the queue poll and the
2541        * interrupt clearing, so notification of the
2542        * command being complete never made it back
2543        * up to the kernel.
2544        */
2545       outb(CLRCMDINT, CLRINT + base);
2546       aic7xxx_done(p, scb);
2547 #if 0
2548   if (scb != &p->scb_array[scb->position])
2549   {
2550     printk("aic7xxx: (complete) Address mismatch, pos(%d).\n", scb->position);
2551   }
2552   printk("aic7xxx: (complete) State(%d) cmd(0x%x) free(0x%x).\n",
2553          scb->state, (unsigned int) scb->cmd, (unsigned int) p->free_scb);
2554 #endif
2555 
2556 #ifdef AIC7XXX_PROC_STATS
2557       /*
2558        * XXX: we should actually know how much actually transferred
2559        * XXX: for each command, but apparently that's too difficult.
2560        */
2561       actual = aic7xxx_length(cmd, 0);
2562       if (!(cmd->flags & WAS_SENSE) && (actual > 0))
2563       {
2564         struct aic7xxx_xferstats *sp;
2565         long *ptr;
2566         int x;
2567 
2568         sp = &p->stats[cmd->channel & 0x01][cmd->target & 0x0F][cmd->lun & 0x07];
2569         sp->xfers++;
2570 
2571         if (cmd->request.cmd == WRITE)
2572         {
2573           sp->w_total++;
2574           sp->w_total512 += (actual >> 9);
2575           ptr = sp->w_bins;
2576         }
2577         else
2578         {
2579           sp->r_total++;
2580           sp->r_total512 += (actual >> 9);
2581           ptr = sp->r_bins;
2582         }
2583         for (x = 9; x <= 17; x++)
2584         {
2585           if (actual < (1 << x))
2586           {
2587             ptr[x - 9]++;
2588             break;
2589           }
2590         }
2591         if (x > 17)
2592         {
2593           ptr[x - 9]++;
2594         }
2595       }
2596 #endif /* AIC7XXX_PROC_STATS */
2597 
2598     } while (inb(QOUTCNT + base) & p->qcntmask);
2599   }
2600 }
2601 
2602 /*+F*************************************************************************
2603  * Function:
2604  *   aic7xxx_probe
2605  *
2606  * Description:
2607  *   Probing for EISA boards: it looks like the first two bytes
2608  *   are a manufacturer code - three characters, five bits each:
2609  *
2610  *               BYTE 0   BYTE 1   BYTE 2   BYTE 3
2611  *              ?1111122 22233333 PPPPPPPP RRRRRRRR
2612  *
2613  *   The characters are baselined off ASCII '@', so add that value
2614  *   to each to get the real ASCII code for it. The next two bytes
2615  *   appear to be a product and revision number, probably vendor-
2616  *   specific. This is what is being searched for at each port,
2617  *   and what should probably correspond to the ID= field in the
2618  *   ECU's .cfg file for the card - if your card is not detected,
2619  *   make sure your signature is listed in the array.
2620  *
2621  *   The fourth byte's lowest bit seems to be an enabled/disabled
2622  *   flag (rest of the bits are reserved?).
2623  *-F*************************************************************************/
2624 static aha_type
2625 aic7xxx_probe(int slot, int base, aha_status_type *bios)
     /* [previous][next][first][last][top][bottom][index][help] */
2626 {
2627   int i;
2628   unsigned char buf[4];
2629 
2630   static struct {
2631     int n;
2632     unsigned char signature[sizeof(buf)];
2633     aha_type type;
2634     int bios_disabled;
2635   } AIC7xxx[] = {
2636     { 4, { 0x04, 0x90, 0x77, 0x71 }, AIC_7771, FALSE }, /* host adapter 274x */
2637     { 4, { 0x04, 0x90, 0x77, 0x70 }, AIC_7770, FALSE }, /* motherboard 7770  */
2638     { 4, { 0x04, 0x90, 0x77, 0x56 }, AIC_284x, FALSE }, /* 284x BIOS enabled */
2639     { 4, { 0x04, 0x90, 0x77, 0x57 }, AIC_284x, TRUE }   /* 284x BIOS disabled */
2640   };
2641 
2642   /*
2643    * The VL-bus cards need to be primed by
2644    * writing before a signature check.
2645    */
2646   for (i = 0; i < sizeof(buf); i++)
2647   {
2648     outb(0x80 + i, base);
2649     buf[i] = inb(base + i);
2650   }
2651 
2652   for (i = 0; i < NUMBER(AIC7xxx); i++)
2653   {
2654     /*
2655      * Signature match on enabled card?
2656      */
2657     if (!memcmp(buf, AIC7xxx[i].signature, AIC7xxx[i].n))
2658     {
2659       if (inb(base + 4) & 1)
2660       {
2661         if (AIC7xxx[i].bios_disabled)
2662         {
2663           *bios = AIC_DISABLED;
2664         }
2665         else
2666         {
2667           *bios = AIC_ENABLED;
2668         }
2669         return (AIC7xxx[i].type);
2670       }
2671 
2672       printk("aic7xxx: Disabled at slot %d, ignored.\n", slot);
2673     }
2674   }
2675 
2676   return (AIC_NONE);
2677 }
2678 
2679 /*+F*************************************************************************
2680  * Function:
2681  *   read_2840_seeprom
2682  *
2683  * Description:
2684  *   Reads the 2840 serial EEPROM and returns 1 if successful and 0 if
2685  *   not successful.
2686  *
2687  *   See read_seeprom (for the 2940) for the instruction set of the 93C46
2688  *   chip.
2689  *
2690  *   The 2840 interface to the 93C46 serial EEPROM is through the
2691  *   STATUS_2840 and SEECTL_2840 registers.  The CS_2840, CK_2840, and
2692  *   DO_2840 bits of the SEECTL_2840 register are connected to the chip
2693  *   select, clock, and data out lines respectively of the serial EEPROM.
2694  *   The DI_2840 bit of the STATUS_2840 is connected to the data in line
2695  *   of the serial EEPROM.  The EEPROM_TF bit of STATUS_2840 register is
2696  *   useful in that it gives us an 800 nsec timer.  After a read from the
2697  *   SEECTL_2840 register the timing flag is cleared and goes high 800 nsec
2698  *   later.
2699  *
2700  *-F*************************************************************************/
2701 static int
2702 read_2840_seeprom(int base, struct seeprom_config *sc)
     /* [previous][next][first][last][top][bottom][index][help] */
2703 {
2704   int i = 0, k = 0;
2705   unsigned char temp;
2706   unsigned short checksum = 0;
2707   unsigned short *seeprom = (unsigned short *) sc;
2708   struct seeprom_cmd {
2709     unsigned char len;
2710     unsigned char bits[3];
2711   };
2712   struct seeprom_cmd seeprom_read = {3, {1, 1, 0}};
2713 
2714 #define CLOCK_PULSE(p) \
2715   while ((inb(STATUS_2840 + base) & EEPROM_TF) == 0)    \
2716   {                                             \
2717     ;  /* Do nothing */                         \
2718   }                                             \
2719   (void) inb(SEECTL_2840 + base);
2720 
2721   /*
2722    * Read the first 32 registers of the seeprom.  For the 2840,
2723    * the 93C46 SEEPROM is a 1024-bit device with 64 16-bit registers
2724    * but only the first 32 are used by Adaptec BIOS.  The loop
2725    * will range from 0 to 31.
2726    */
2727   for (k = 0; k < (sizeof(*sc) / 2); k++)
2728   {
2729     /*
2730      * Send chip select for one clock cycle.
2731      */
2732     outb(CK_2840 | CS_2840, SEECTL_2840 + base);
2733     CLOCK_PULSE(base);
2734 
2735     /*
2736      * Now we're ready to send the read command followed by the
2737      * address of the 16-bit register we want to read.
2738      */
2739     for (i = 0; i < seeprom_read.len; i++)
2740     {
2741       temp = CS_2840 | seeprom_read.bits[i];
2742       outb(temp, SEECTL_2840 + base);
2743       CLOCK_PULSE(base);
2744       temp = temp ^ CK_2840;
2745       outb(temp, SEECTL_2840 + base);
2746       CLOCK_PULSE(base);
2747     }
2748     /*
2749      * Send the 6 bit address (MSB first, LSB last).
2750      */
2751     for (i = 5; i >= 0; i--)
2752     {
2753       temp = k;
2754       temp = (temp >> i) & 1;  /* Mask out all but lower bit. */
2755       temp = CS_2840 | temp;
2756       outb(temp, SEECTL_2840 + base);
2757       CLOCK_PULSE(base);
2758       temp = temp ^ CK_2840;
2759       outb(temp, SEECTL_2840 + base);
2760       CLOCK_PULSE(base);
2761     }
2762 
2763     /*
2764      * Now read the 16 bit register.  An initial 0 precedes the
2765      * register contents which begins with bit 15 (MSB) and ends
2766      * with bit 0 (LSB).  The initial 0 will be shifted off the
2767      * top of our word as we let the loop run from 0 to 16.
2768      */
2769     for (i = 0; i <= 16; i++)
2770     {
2771       temp = CS_2840;
2772       outb(temp, SEECTL_2840 + base);
2773       CLOCK_PULSE(base);
2774       temp = temp ^ CK_2840;
2775       seeprom[k] = (seeprom[k] << 1) | (inb(STATUS_2840 + base) & DI_2840);
2776       outb(temp, SEECTL_2840 + base);
2777       CLOCK_PULSE(base);
2778     }
2779     /*
2780      * The serial EEPROM has a checksum in the last word.  Keep a
2781      * running checksum for all words read except for the last
2782      * word.  We'll verify the checksum after all words have been
2783      * read.
2784      */
2785     if (k < (sizeof(*sc) / 2) - 1)
2786     {
2787       checksum = checksum + seeprom[k];
2788     }
2789 
2790     /*
2791      * Reset the chip select for the next command cycle.
2792      */
2793     outb(0, SEECTL_2840 + base);
2794     CLOCK_PULSE(base);
2795     outb(CK_2840, SEECTL_2840 + base);
2796     CLOCK_PULSE(base);
2797     outb(0, SEECTL_2840 + base);
2798     CLOCK_PULSE(base);
2799   }
2800 
2801 #if 0
2802   printk("Computed checksum 0x%x, checksum read 0x%x\n", checksum, sc->checksum);
2803   printk("Serial EEPROM:");
2804   for (k = 0; k < (sizeof(*sc) / 2); k++)
2805   {
2806     if (((k % 8) == 0) && (k != 0))
2807     {
2808       printk("\n              ");
2809     }
2810     printk(" 0x%x", seeprom[k]);
2811   }
2812   printk("\n");
2813 #endif
2814 
2815   if (checksum != sc->checksum)
2816   {
2817     printk("aic7xxx: SEEPROM checksum error, ignoring SEEPROM settings.\n");
2818     return (0);
2819   }
2820 
2821   return (1);
2822 #undef CLOCK_PULSE
2823 }
2824 
2825 /*+F*************************************************************************
2826  * Function:
2827  *   read_seeprom
2828  *
2829  * Description:
2830  *   Reads the serial EEPROM and returns 1 if successful and 0 if
2831  *   not successful.
2832  *
2833  *   The instruction set of the 93C46 chip is as follows:
2834  *
2835  *               Start  OP
2836  *     Function   Bit  Code  Address    Data     Description
2837  *     -------------------------------------------------------------------
2838  *     READ        1    10   A5 - A0             Reads data stored in memory,
2839  *                                               starting at specified address
2840  *     EWEN        1    00   11XXXX              Write enable must precede
2841  *                                               all programming modes
2842  *     ERASE       1    11   A5 - A0             Erase register A5A4A3A2A1A0
2843  *     WRITE       1    01   A5 - A0   D15 - D0  Writes register
2844  *     ERAL        1    00   10XXXX              Erase all registers
2845  *     WRAL        1    00   01XXXX    D15 - D0  Writes to all registers
2846  *     EWDS        1    00   00XXXX              Disables all programming
2847  *                                               instructions
2848  *     *Note: A value of X for address is a don't care condition.
2849  *
2850  *   The 93C46 has a four wire interface: clock, chip select, data in, and
2851  *   data out.  In order to perform one of the above functions, you need
2852  *   to enable the chip select for a clock period (typically a minimum of
2853  *   1 usec, with the clock high and low a minimum of 750 and 250 nsec
2854  *   respectively.  While the chip select remains high, you can clock in
2855  *   the instructions (above) starting with the start bit, followed by the
2856  *   OP code, Address, and Data (if needed).  For the READ instruction, the
2857  *   requested 16-bit register contents is read from the data out line but
2858  *   is preceded by an initial zero (leading 0, followed by 16-bits, MSB
2859  *   first).  The clock cycling from low to high initiates the next data
2860  *   bit to be sent from the chip.
2861  *
2862  *   The 7870 interface to the 93C46 serial EEPROM is through the SEECTL
2863  *   register.  After successful arbitration for the memory port, the
2864  *   SEECS bit of the SEECTL register is connected to the chip select.
2865  *   The SEECK, SEEDO, and SEEDI are connected to the clock, data out,
2866  *   and data in lines respectively.  The SEERDY bit of SEECTL is useful
2867  *   in that it gives us an 800 nsec timer.  After a write to the SEECTL
2868  *   register, the SEERDY goes high 800 nsec later.  The one exception
2869  *   to this is when we first request access to the memory port.  The
2870  *   SEERDY goes high to signify that access has been granted and, for
2871  *   this case, has no implied timing.
2872  *
2873  *-F*************************************************************************/
2874 static int
2875 read_seeprom(int base, int offset, struct seeprom_config *sc)
     /* [previous][next][first][last][top][bottom][index][help] */
2876 {
2877   int i = 0, k;
2878   unsigned long timeout;
2879   unsigned char temp;
2880   unsigned short checksum = 0;
2881   unsigned short *seeprom = (unsigned short *) sc;
2882   struct seeprom_cmd {
2883     unsigned char len;
2884     unsigned char bits[3];
2885   };
2886   struct seeprom_cmd seeprom_read = {3, {1, 1, 0}};
2887 
2888 #define CLOCK_PULSE(p) \
2889   while ((inb(SEECTL + base) & SEERDY) == 0)    \
2890   {                                             \
2891     ;  /* Do nothing */                         \
2892   }
2893 
2894   /*
2895    * Request access of the memory port.  When access is
2896    * granted, SEERDY will go high.  We use a 1 second
2897    * timeout which should be near 1 second more than
2898    * is needed.  Reason: after the 7870 chip reset, there
2899    * should be no contention.
2900    */
2901   outb(SEEMS, SEECTL + base);
2902   timeout = jiffies + 100;  /* 1 second timeout */
2903   while ((jiffies < timeout) && ((inb(SEECTL + base) & SEERDY) == 0))
2904   {
2905     ; /* Do nothing!  Wait for access to be granted.  */
2906   }
2907   if ((inb(SEECTL + base) & SEERDY) == 0)
2908   {
2909     outb(0, SEECTL + base);
2910     return (0);
2911   }
2912 
2913   /*
2914    * Read the first 32 registers of the seeprom.  For the 7870,
2915    * the 93C46 SEEPROM is a 1024-bit device with 64 16-bit registers
2916    * but only the first 32 are used by Adaptec BIOS.  The loop
2917    * will range from 0 to 31.
2918    */
2919   for (k = 0; k < (sizeof(*sc) / 2); k++)
2920   {
2921     /*
2922      * Send chip select for one clock cycle.
2923      */
2924     outb(SEEMS | SEECK | SEECS, SEECTL + base);
2925     CLOCK_PULSE(base);
2926 
2927     /*
2928      * Now we're ready to send the read command followed by the
2929      * address of the 16-bit register we want to read.
2930      */
2931     for (i = 0; i < seeprom_read.len; i++)
2932     {
2933       temp = SEEMS | SEECS | (seeprom_read.bits[i] << 1);
2934       outb(temp, SEECTL + base);
2935       CLOCK_PULSE(base);
2936       temp = temp ^ SEECK;
2937       outb(temp, SEECTL + base);
2938       CLOCK_PULSE(base);
2939     }
2940     /*
2941      * Send the 6 bit address (MSB first, LSB last).
2942      */
2943     for (i = 5; i >= 0; i--)
2944     {
2945       temp = k + offset;
2946       temp = (temp >> i) & 1;  /* Mask out all but lower bit. */
2947       temp = SEEMS | SEECS | (temp << 1);
2948       outb(temp, SEECTL + base);
2949       CLOCK_PULSE(base);
2950       temp = temp ^ SEECK;
2951       outb(temp, SEECTL + base);
2952       CLOCK_PULSE(base);
2953     }
2954 
2955     /*
2956      * Now read the 16 bit register.  An initial 0 precedes the
2957      * register contents which begins with bit 15 (MSB) and ends
2958      * with bit 0 (LSB).  The initial 0 will be shifted off the
2959      * top of our word as we let the loop run from 0 to 16.
2960      */
2961     for (i = 0; i <= 16; i++)
2962     {
2963       temp = SEEMS | SEECS;
2964       outb(temp, SEECTL + base);
2965       CLOCK_PULSE(base);
2966       temp = temp ^ SEECK;
2967       seeprom[k] = (seeprom[k] << 1) | (inb(SEECTL + base) & SEEDI);
2968       outb(temp, SEECTL + base);
2969       CLOCK_PULSE(base);
2970     }
2971 
2972     /*
2973      * The serial EEPROM has a checksum in the last word.  Keep a
2974      * running checksum for all words read except for the last
2975      * word.  We'll verify the checksum after all words have been
2976      * read.
2977      */
2978     if (k < (sizeof(*sc) / 2) - 1)
2979     {
2980       checksum = checksum + seeprom[k];
2981     }
2982 
2983     /*
2984      * Reset the chip select for the next command cycle.
2985      */
2986     outb(SEEMS, SEECTL + base);
2987     CLOCK_PULSE(base);
2988     outb(SEEMS | SEECK, SEECTL + base);
2989     CLOCK_PULSE(base);
2990     outb(SEEMS, SEECTL + base);
2991     CLOCK_PULSE(base);
2992   }
2993 
2994   /*
2995    * Release access to the memory port and the serial EEPROM.
2996    */
2997   outb(0, SEECTL + base);
2998 
2999 #if 0
3000   printk("Computed checksum 0x%x, checksum read 0x%x\n", checksum, sc->checksum);
3001   printk("Serial EEPROM:");
3002   for (k = 0; k < (sizeof(*sc) / 2); k++)
3003   {
3004     if (((k % 8) == 0) && (k != 0))
3005     {
3006       printk("\n              ");
3007     }
3008     printk(" 0x%x", seeprom[k]);
3009   }
3010   printk("\n");
3011 #endif
3012 
3013   if (checksum != sc->checksum)
3014   {
3015     return (0);
3016   }
3017 
3018   return (1);
3019 #undef CLOCK_PULSE
3020 }
3021 
3022 /*+F*************************************************************************
3023  * Function:
3024  *   detect_maxscb
3025  *
3026  * Description:
3027  *   Detects the maximum number of SCBs for the controller and returns
3028  *   the count and a mask in config (config->maxscbs, config->qcntmask).
3029  *-F*************************************************************************/
3030 static void
3031 detect_maxscb(struct aic7xxx_host_config *config)
     /* [previous][next][first][last][top][bottom][index][help] */
3032 {
3033   unsigned char sblkctl_reg;
3034   int base, i;
3035 
3036   base = config->base;
3037   switch (config->type)
3038   {
3039     case AIC_7770:
3040     case AIC_7771:
3041     case AIC_284x:
3042       /*
3043        * Check for Rev C or E boards. Rev E boards can supposedly have
3044        * more than 4 SCBs, while the Rev C boards are limited to 4 SCBs.
3045        * Until we know how to access more than 4 SCBs for the Rev E chips,
3046        * we limit them, along with the Rev C chips, to 4 SCBs.
3047        *
3048        * The Rev E boards have a read/write autoflush bit in the
3049        * SBLKCTL register, while in the Rev C boards it is read only.
3050        */
3051       sblkctl_reg = inb(SBLKCTL + base) ^ AUTOFLUSHDIS;
3052       outb(sblkctl_reg, SBLKCTL + base);
3053       if (inb(SBLKCTL + base) == sblkctl_reg)
3054       {
3055         /*
3056          * We detected a Rev E board.
3057          */
3058         printk("aic7xxx: %s Rev E and subsequent.\n",
3059                board_names[config->type]);
3060         outb(sblkctl_reg ^ AUTOFLUSHDIS, SBLKCTL + base);
3061       }
3062       else
3063       {
3064         printk("aic7xxx: %s Rev C and previous.\n", board_names[config->type]);
3065       }
3066       break;
3067 
3068     default:
3069       break;
3070   }
3071 
3072   /*
3073    * Walk the SCBs to determine how many there are.
3074    */
3075   i = 1;
3076   outb (0, SCBPTR + base);
3077   outb (0, SCBARRAY + base);
3078 
3079   while (i < AIC7XXX_MAXSCB)
3080   {
3081     outb(i, SCBPTR + base);
3082     outb(i, SCBARRAY + base);
3083     if (inb(SCBARRAY + base) != i)
3084       break;
3085     outb(0, SCBPTR + base);
3086     if (inb(SCBARRAY + base) != 0)
3087       break;
3088 
3089     outb(i, SCBPTR + base);      /* Clear the control byte. */
3090     outb(0, SCBARRAY + base);
3091 
3092     config->qcntmask |= i;       /* Update the count mask. */
3093     i++;
3094   }
3095   outb(i, SCBPTR + base);   /* Ensure we clear the control bytes. */
3096   outb(0, SCBARRAY + base);
3097   outb(0, SCBPTR + base); 
3098   outb(0, SCBARRAY + base);
3099 
3100   config->maxscb = i;
3101   config->qcntmask |= i;
3102 
3103   printk("aic7xxx: Using %d SCB's after checking for SCB memory.\n",
3104          config->maxscb);
3105 
3106 }
3107 
3108 /*+F*************************************************************************
3109  * Function:
3110  *   aic7xxx_register
3111  *
3112  * Description:
3113  *   Register a Adaptec aic7xxx chip SCSI controller with the kernel.
3114  *-F*************************************************************************/
3115 static int
3116 aic7xxx_register(Scsi_Host_Template *template,
     /* [previous][next][first][last][top][bottom][index][help] */
3117                  struct aic7xxx_host_config *config)
3118 {
3119   int i;
3120   unsigned char sblkctl;
3121   int max_targets;
3122   int found = 1, base;
3123   unsigned char target_settings;
3124   unsigned char scsi_conf, host_conf;
3125   int have_seeprom = FALSE;
3126   struct Scsi_Host *host;
3127   struct aic7xxx_host *p;
3128   struct seeprom_config sc;
3129 
3130   base = config->base;
3131 
3132   /*
3133    * Lock out other contenders for our i/o space.
3134    */
3135   request_region(MINREG + base, MAXREG - MINREG, "aic7xxx");
3136 
3137   switch (config->type)
3138   {
3139     case AIC_7770:
3140     case AIC_7771:
3141       /*
3142        * For some 274x boards, we must clear the CHIPRST bit
3143        * and pause the sequencer. For some reason, this makes
3144        * the driver work. For 284x boards, we give it a
3145        * CHIPRST just like the 294x boards.
3146        *
3147        * Use the BIOS settings to determine the interrupt
3148        * trigger type (level or edge) and use this value
3149        * for pausing and unpausing the sequencer.
3150        */
3151       config->unpause = (inb(HCNTRL + base) & IRQMS) | INTEN;
3152       config->pause = config->unpause | PAUSE;
3153       config->extended = aic7xxx_extended;
3154 
3155       outb(config->pause | CHIPRST, HCNTRL + base);
3156       aic7xxx_delay(1);
3157       if (inb(HCNTRL + base) & CHIPRST)
3158       {
3159         printk("aic7xxx: Chip reset not cleared; clearing manually.\n");
3160       }
3161       outb(config->pause, HCNTRL + base);
3162 
3163       /*
3164        * Just to be on the safe side with the 274x, we will re-read the irq
3165        * since there was some issue about resetting the board.
3166        */
3167       config->irq = inb(INTDEF + base) & 0x0F;
3168       if ((config->type == AIC_7771) &&
3169           (inb(HA_274_BIOSCTRL + base) & BIOSMODE) == BIOSDISABLED)
3170       {
3171         config->bios = AIC_DISABLED;
3172         config->use_defaults = TRUE;
3173       }
3174       else
3175       {
3176         host_conf = inb(HOSTCONF + base);
3177         config->bus_speed = host_conf & DFTHRSH;
3178         config->busrtime = (host_conf << 2) & BOFF;
3179       }
3180 
3181       /*
3182        * Setup the FIFO threshold and the bus off time
3183        */
3184       outb(config->bus_speed & DFTHRSH, BUSSPD + base);
3185       outb(config->busrtime, BUSTIME + base);
3186 
3187       /*
3188        * A reminder until this can be detected automatically.
3189        */
3190       printk("aic7xxx: Extended translation %sabled.\n",
3191              config->extended ? "en" : "dis");
3192       break;
3193 
3194     case AIC_284x:
3195       outb(CHIPRST, HCNTRL + base);
3196       config->unpause = UNPAUSE_284X;
3197       config->pause = REQ_PAUSE; /* DWG would like to be like the rest */
3198       aic7xxx_delay(1);
3199       outb(config->pause, HCNTRL + base);
3200 
3201       config->parity = AIC_ENABLED;
3202       config->extended = aic7xxx_extended;
3203       config->irq = inb(INTDEF + base) & 0x0F;
3204       host_conf = inb(HOSTCONF + base);
3205 
3206       printk("aic7xxx: Reading SEEPROM...");
3207       have_seeprom = read_2840_seeprom(base, &sc);
3208       if (!have_seeprom)
3209       {
3210         printk("aic7xxx: Unable to read SEEPROM.\n");
3211       }
3212       else
3213       {
3214         printk("done.\n");
3215         config->extended = ((sc.bios_control & CF284XEXTEND) >> 5);
3216         if (!(sc.bios_control & CFBIOSEN))
3217         {
3218           /*
3219            * The BIOS is disabled; the values left over in scratch
3220            * RAM are still valid.  Do not use defaults as in the
3221            * AIC-7770 case.
3222            */
3223           config->bios = AIC_DISABLED;
3224         }
3225         else
3226         {
3227           config->parity = (sc.adapter_control & CFSPARITY) ?
3228                            AIC_ENABLED : AIC_DISABLED;
3229           config->low_term = (sc.adapter_control & CF284XSTERM) ?
3230                                 AIC_ENABLED : AIC_DISABLED;
3231           /*
3232            * XXX - Adaptec *does* make 284x wide controllers, but the
3233            *       documents do not say where the high byte termination
3234            *       enable bit is located.
3235            */
3236         }
3237       }
3238 
3239       host_conf = inb(HOSTCONF + base);
3240       config->bus_speed = host_conf & DFTHRSH;
3241       config->busrtime = (host_conf << 2) & BOFF;
3242 
3243       /*
3244        * Setup the FIFO threshold and the bus off time
3245        */
3246       outb(config->bus_speed & DFTHRSH, BUSSPD + base);
3247       outb(config->busrtime, BUSTIME + base);
3248 
3249       printk("aic7xxx: Extended translation %sabled.\n",
3250              config->extended ? "en" : "dis");
3251       break;
3252 
3253     case AIC_7850:
3254     case AIC_7855:
3255     case AIC_7870:
3256     case AIC_7871:
3257     case AIC_7872:
3258     case AIC_7873:
3259     case AIC_7874:
3260     case AIC_7880:
3261     case AIC_7881:
3262     case AIC_7882:
3263     case AIC_7883:
3264     case AIC_7884:
3265       outb(CHIPRST, HCNTRL + base);
3266       config->unpause = UNPAUSE_294X;
3267       config->pause = config->unpause | PAUSE;
3268       aic7xxx_delay(1);
3269       outb(config->pause, HCNTRL + base);
3270 
3271       config->extended = aic7xxx_extended;
3272       config->scsi_id = 7;
3273       config->parity = AIC_ENABLED;
3274 
3275       printk("aic7xxx: Reading SEEPROM...");
3276       have_seeprom = read_seeprom(base, config->chan_num * (sizeof(sc) / 2), &sc);
3277       if (!have_seeprom)
3278       {
3279         printk("\naic7xxx: Unable to read SEEPROM; "
3280                "using leftover BIOS values.\n");
3281       }
3282       else
3283       {
3284         printk("done.\n");
3285         if (!(sc.bios_control & CFBIOSEN))
3286         {
3287           /*
3288            * The BIOS is disabled; the values left over in scratch
3289            * RAM are still valid.  Do not use defaults as in the
3290            * AIC-7770 case.
3291            */
3292           config->bios = AIC_DISABLED;
3293         }
3294         else
3295         {
3296           config->extended = ((sc.bios_control & CFEXTEND) >> 7);
3297           config->scsi_id = (sc.brtime_id & CFSCSIID);
3298           config->parity = (sc.adapter_control & CFSPARITY) ?
3299                              AIC_ENABLED : AIC_DISABLED;
3300           config->low_term = (sc.adapter_control & CFSTERM) ?
3301                                AIC_ENABLED : AIC_DISABLED;
3302           config->high_term = (sc.adapter_control & CFWSTERM) ?
3303                                 AIC_ENABLED : AIC_DISABLED;
3304           config->busrtime = ((sc.brtime_id & CFBRTIME) >> 8);
3305           if (((config->type == AIC_7880) || (config->type == AIC_7882) ||
3306                (config->type == AIC_7883) || (config->type == AIC_7884)) &&
3307               (sc.adapter_control & CFULTRAEN))
3308           {
3309             printk ("aic7xxx: Enabling support for Ultra SCSI speed.\n");
3310             config->ultra_enabled = TRUE;
3311           }
3312         }
3313       }
3314 
3315       /*
3316        * XXX - force data fifo threshold to 100%. Why does this
3317        *       need to be done?
3318        *
3319        * We don't know where this is set in the SEEPROM or by the BIOS,
3320        * so we default it to 100%.
3321        */
3322       config->bus_speed = DFTHRSH_100;
3323       scsi_conf = config->scsi_id | config->bus_speed;
3324       if (config->parity == AIC_ENABLED)
3325       {
3326         scsi_conf |= ENSPCHK;
3327       }
3328 
3329       outb(scsi_conf, SCSICONF + base);
3330       outb(config->bus_speed, DSPCISTATUS + base);
3331 
3332       /*
3333        * In case we are a wide card...
3334        */
3335       outb(scsi_conf, (SCSICONF + base + 1));
3336 
3337       printk("aic7xxx: Extended translation %sabled.\n",
3338              config->extended ? "en" : "dis");
3339       break;
3340 
3341     default:
3342       panic("aic7xxx: (aic7xxx_register) Internal error.\n");
3343   }
3344 
3345   detect_maxscb(config);
3346 
3347   if (config->chip_type == AIC_777x)
3348   {
3349     if (config->pause & IRQMS)
3350     {
3351       printk("aic7xxx: Using level sensitive interrupts.\n");
3352     }
3353     else
3354     {
3355       printk("aic7xxx: Using edge triggered interrupts.\n");
3356     }
3357   }
3358 
3359   /*
3360    * Read the bus type from the SBLKCTL register. Set the FLAGS
3361    * register in the sequencer for twin and wide bus cards.
3362    */
3363   sblkctl = inb(SBLKCTL + base);
3364   switch (sblkctl & SELBUS_MASK)
3365   {
3366     case SELNARROW:     /* narrow/normal bus */
3367       config->scsi_id = inb(SCSICONF + base) & 0x07;
3368       config->bus_type = AIC_SINGLE;
3369       outb(SINGLE_BUS, FLAGS + base);
3370       break;
3371 
3372     case SELWIDE:     /* Wide bus */
3373       config->scsi_id = inb(SCSICONF + base + 1) & 0x0F;
3374       config->bus_type = AIC_WIDE;
3375       printk("aic7xxx: Enabling wide channel of %s-Wide.\n",
3376              board_names[config->type]);
3377       outb(WIDE_BUS, FLAGS + base);
3378       break;
3379 
3380     case SELBUSB:     /* Twin bus */
3381       config->scsi_id = inb(SCSICONF + base) & 0x07;
3382 #ifdef AIC7XXX_TWIN_SUPPORT
3383       config->scsi_id_b = inb(SCSICONF + base + 1) & 0x07;
3384       config->bus_type = AIC_TWIN;
3385       printk("aic7xxx: Enabled channel B of %s-Twin.\n",
3386              board_names[config->type]);
3387       outb(TWIN_BUS, FLAGS + base);
3388 #else
3389       config->bus_type = AIC_SINGLE;
3390       printk("aic7xxx: Channel B of %s-Twin will be ignored.\n",
3391              board_names[config->type]);
3392       outb(0, FLAGS + base);
3393 #endif
3394       break;
3395 
3396     default:
3397       printk("aic7xxx: Unsupported type 0x%x, please "
3398              "mail deang@teleport.com\n", inb(SBLKCTL + base));
3399       outb(0, FLAGS + base);
3400       return (0);
3401   }
3402 
3403   /*
3404    * For the 294x cards, clearing DIAGLEDEN and DIAGLEDON, will
3405    * take the card out of diagnostic mode and make the host adapter
3406    * LED follow bus activity (will not always be on).
3407    */
3408   outb(sblkctl & ~(DIAGLEDEN | DIAGLEDON), SBLKCTL + base);
3409 
3410   /*
3411    * The IRQ level in i/o port 4 maps directly onto the real
3412    * IRQ number. If it's ok, register it with the kernel.
3413    *
3414    * NB. the Adaptec documentation says the IRQ number is only
3415    *     in the lower four bits; the ECU information shows the
3416    *     high bit being used as well. Which is correct?
3417    *
3418    * The PCI cards get their interrupt from PCI BIOS.
3419    */
3420   if ((config->chip_type == AIC_777x) && ((config->irq < 9) || (config->irq > 15)))
3421   {
3422     printk("aic7xxx: Host adapter uses unsupported IRQ level, ignoring.\n");
3423     return (0);
3424   }
3425 
3426   /*
3427    * Check the IRQ to see if it is shared by another aic7xxx
3428    * controller. If it is and sharing of IRQs is not defined,
3429    * then return 0 hosts found. If sharing of IRQs is allowed
3430    * or the IRQ is not shared by another host adapter, then
3431    * proceed.
3432    */
3433 #ifndef AIC7XXX_SHARE_IRQS
3434    if (aic7xxx_boards[config->irq] != NULL)
3435    {
3436      printk("aic7xxx: Sharing of IRQ's is not configured.\n");
3437      return (0);
3438    }
3439 #endif
3440 
3441   /*
3442    * Print out debugging information before re-enabling
3443    * the card - a lot of registers on it can't be read
3444    * when the sequencer is active.
3445    */
3446   debug_config(config);
3447 
3448   /*
3449    * Before registry, make sure that the offsets of the
3450    * struct scatterlist are what the sequencer will expect,
3451    * otherwise disable scatter-gather altogether until someone
3452    * can fix it. This is important since the sequencer will
3453    * DMA elements of the SG array in while executing commands.
3454    */
3455   if (template->sg_tablesize != SG_NONE)
3456   {
3457     struct scatterlist sg;
3458 
3459     if (SG_STRUCT_CHECK(sg))
3460     {
3461       printk("aic7xxx: Warning - Kernel scatter-gather structures changed, "
3462              "disabling it.\n");
3463       template->sg_tablesize = SG_NONE;
3464     }
3465   }
3466 
3467   /*
3468    * Register each "host" and fill in the returned Scsi_Host
3469    * structure as best we can. Some of the parameters aren't
3470    * really relevant for bus types beyond ISA, and none of the
3471    * high-level SCSI code looks at it anyway. Why are the fields
3472    * there? Also save the pointer so that we can find the
3473    * information when an IRQ is triggered.
3474    */
3475   host = scsi_register(template, sizeof(struct aic7xxx_host));
3476   host->can_queue = config->maxscb;
3477   host->cmd_per_lun = AIC7XXX_CMDS_PER_LUN;
3478   host->this_id = config->scsi_id;
3479   host->irq = config->irq;
3480   if (config->bus_type == AIC_WIDE)
3481   {
3482     host->max_id = 16;
3483   }
3484   if (config->bus_type == AIC_TWIN)
3485   {
3486     host->max_channel = 1;
3487   }
3488 
3489   p = (struct aic7xxx_host *) host->hostdata;
3490 
3491   p->host = host;
3492   p->isr_count = 0;
3493   p->a_scanned = FALSE;
3494   p->b_scanned = FALSE;
3495   p->base = base;
3496   p->maxscb = config->maxscb;
3497   p->qcntmask = config->qcntmask;
3498   p->numscb = 0;
3499   p->extended = config->extended;
3500   p->type = config->type;
3501   p->chip_type = config->chip_type;
3502   p->ultra_enabled = config->ultra_enabled;
3503   p->chan_num = config->chan_num;
3504   p->bus_type = config->bus_type;
3505   p->have_seeprom = have_seeprom;
3506   p->seeprom = sc;
3507   p->free_scb = NULL;
3508   p->aborted_scb = NULL;
3509   p->next = NULL;
3510 
3511   p->unpause = config->unpause;
3512   p->pause = config->pause;
3513 
3514   if (aic7xxx_boards[config->irq] == NULL)
3515   {
3516     /*
3517      * Warning! This must be done before requesting the irq.  It is
3518      * possible for some boards to raise an interrupt as soon as
3519      * they are enabled.  So when we request the irq from the Linux
3520      * kernel, an interrupt is triggered immediately.  Therefore, we
3521      * must ensure the board data is correctly set before the request.
3522      */
3523     aic7xxx_boards[config->irq] = host;
3524 
3525     /*
3526      * Register IRQ with the kernel.
3527      */
3528     if (request_irq(config->irq, aic7xxx_isr, SA_INTERRUPT, "aic7xxx", NULL))
3529     {
3530       printk("aic7xxx: Couldn't register IRQ %d, ignoring.\n", config->irq);
3531       aic7xxx_boards[config->irq] = NULL;
3532       return (0);
3533     }
3534   }
3535   else
3536   {
3537     /*
3538      * We have found a host adapter sharing an IRQ of a previously
3539      * registered host adapter. Add this host adapter's Scsi_Host
3540      * to the beginning of the linked list of hosts at the same IRQ.
3541      */
3542     p->next = aic7xxx_boards[config->irq];
3543     aic7xxx_boards[config->irq] = host;
3544   }
3545 
3546   /*
3547    * Load the sequencer program, then re-enable the board -
3548    * resetting the AIC-7770 disables it, leaving the lights
3549    * on with nobody home. On the PCI bus you *may* be home,
3550    * but then your mailing address is dynamically assigned
3551    * so no one can find you anyway :-)
3552    */
3553   printk("aic7xxx: Downloading sequencer code...");
3554   aic7xxx_loadseq(base);
3555 
3556   /*
3557    * Set Fast Mode and Enable the board
3558    */
3559   outb(FASTMODE, SEQCTL + base);
3560 
3561   if (p->chip_type == AIC_777x)
3562   {
3563     outb(ENABLE, BCTL + base);
3564   }
3565 
3566   printk("done.\n");
3567 
3568   /*
3569    * Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels
3570    */
3571   if (p->bus_type == AIC_TWIN)
3572   {
3573     /*
3574      * Select Channel B.
3575      */
3576     outb((sblkctl & ~SELBUS_MASK) | SELBUSB, SBLKCTL + base);
3577 
3578     outb(config->scsi_id_b, SCSIID + base);
3579     scsi_conf = inb(SCSICONF + base + 1) & (ENSPCHK | STIMESEL);
3580     outb(scsi_conf | ENSTIMER | ACTNEGEN | STPWEN, SXFRCTL1 + base);
3581     outb(ENSELTIMO | ENSCSIRST | ENSCSIPERR, SIMODE1 + base);
3582     if (p->ultra_enabled)
3583     {
3584       outb(DFON | SPIOEN | ULTRAEN, SXFRCTL0 + base);
3585     }
3586     else
3587     {
3588       outb(DFON | SPIOEN, SXFRCTL0 + base);
3589     }
3590 
3591     /*
3592      * Select Channel A
3593      */
3594     outb((sblkctl & ~SELBUS_MASK) | SELNARROW, SBLKCTL + base);
3595   }
3596   outb(config->scsi_id, SCSIID + base);
3597   scsi_conf = inb(SCSICONF + base) & (ENSPCHK | STIMESEL);
3598   outb(scsi_conf | ENSTIMER | ACTNEGEN | STPWEN, SXFRCTL1 + base);
3599   outb(ENSELTIMO | ENSCSIRST | ENSCSIPERR, SIMODE1 + base);
3600   if (p->ultra_enabled)
3601   {
3602     outb(DFON | SPIOEN | ULTRAEN, SXFRCTL0 + base);
3603   }
3604   else
3605   {
3606     outb(DFON | SPIOEN, SXFRCTL0 + base);
3607   }
3608 
3609   /*
3610    * Look at the information that board initialization or the board
3611    * BIOS has left us. In the lower four bits of each target's
3612    * scratch space any value other than 0 indicates that we should
3613    * initiate synchronous transfers. If it's zero, the user or the
3614    * BIOS has decided to disable synchronous negotiation to that
3615    * target so we don't activate the needsdtr flag.
3616    */
3617   p->needsdtr_copy = 0x0;
3618   p->sdtr_pending = 0x0;
3619   p->needwdtr_copy = 0x0;
3620   p->wdtr_pending = 0x0;
3621   if (p->bus_type == AIC_SINGLE)
3622   {
3623     max_targets = 8;
3624   }
3625   else
3626   {
3627     max_targets = 16;
3628   }
3629 
3630   /*
3631    * Grab the disconnection disable table and invert it for our needs
3632    */
3633   if (have_seeprom)
3634   {
3635     p->discenable = 0x0;
3636   }
3637   else
3638   {
3639     if (config->bios == AIC_DISABLED)
3640     {
3641       printk("aic7xxx : Host adapter BIOS disabled. Using default SCSI "
3642              "device parameters.\n");
3643       p->discenable = 0xFFFF;
3644     }
3645     else
3646     {
3647       p->discenable = ~((inb(DISC_DSB + base + 1) << 8) |
3648           inb(DISC_DSB + base));
3649     }
3650   }
3651 
3652   for (i = 0; i < max_targets; i++)
3653   {
3654     if (have_seeprom)
3655     {
3656       target_settings = ((sc.device_flags[i] & CFXFER) << 4);
3657       if (sc.device_flags[i] & CFSYNCH)
3658       {
3659         p->needsdtr_copy |= (0x01 << i);
3660       }
3661       if (sc.device_flags[i] & CFWIDEB)
3662       {
3663         p->needwdtr_copy |= (0x01 << i);
3664       }
3665       if (sc.device_flags[i] & CFDISC)
3666       {
3667         p->discenable |= (0x01 << i);
3668       }
3669     }
3670     else
3671     {
3672       if (config->use_defaults)
3673       {
3674         target_settings = 0;  /* 10 MHz */
3675         p->needsdtr_copy |= (0x01 << i);
3676         p->needwdtr_copy |= (0x01 << i);
3677       }
3678       else
3679       {
3680         target_settings = inb(TARG_SCRATCH + base + i);
3681         if (target_settings & 0x0F)
3682         {
3683           p->needsdtr_copy |= (0x01 << i);
3684           /*
3685            * Default to asynchronous transfers (0 offset)
3686            */
3687           target_settings &= 0xF0;
3688         }
3689         if (target_settings & 0x80)
3690         {
3691           p->needwdtr_copy |= (0x01 << i);
3692           target_settings &= 0x7F;
3693         }
3694       }
3695     }
3696     outb(target_settings, (TARG_SCRATCH + base + i));
3697   }
3698 
3699   /*
3700    * If we are not wide, forget WDTR. This makes the driver
3701    * work on some cards that don't leave these fields cleared
3702    * when BIOS is not installed.
3703    */
3704   if (p->bus_type != AIC_WIDE)
3705   {
3706     p->needwdtr_copy = 0;
3707   }
3708   p->needsdtr = p->needsdtr_copy;
3709   p->needwdtr = p->needwdtr_copy;
3710 #if 0
3711   printk("NeedSdtr = 0x%x, 0x%x\n", p->needsdtr_copy, p->needsdtr);
3712   printk("NeedWdtr = 0x%x, 0x%x\n", p->needwdtr_copy, p->needwdtr);
3713 #endif
3714 
3715   /*
3716    * For reconnecting targets, the sequencer code needs to
3717    * know how many SCBs it has to search through.
3718    */
3719   outb(config->maxscb, SCBCOUNT + base);
3720 
3721   /*
3722    * 2s compliment of SCBCOUNT
3723    */
3724   i = p->maxscb;
3725   outb(-i & 0xff, COMP_SCBCOUNT + base);
3726 
3727   /*
3728    * Set the QCNT (queue count) mask to deal with broken aic7850s that
3729    * sporadically get garbage in the upper bits of their QCNT registers.
3730    */
3731   outb(config->qcntmask, QCNTMASK + base);
3732 
3733   /*
3734    * Clear the active flags - no targets are busy.
3735    */
3736   outb(0, ACTIVE_A + base);
3737   outb(0, ACTIVE_B + base);
3738 
3739   /*
3740    * We don't have any waiting selections
3741    */
3742   outb(SCB_LIST_NULL, WAITING_SCBH + base);
3743 
3744   /*
3745    * Message out buffer starts empty
3746    */
3747   outb(0, MSG_LEN + base);
3748 
3749   /*
3750    * Reset the SCSI bus. Is this necessary?
3751    *   There may be problems for a warm boot without resetting
3752    *   the SCSI bus. Either BIOS settings in scratch RAM
3753    *   will not get reinitialized, or devices may stay at
3754    *   previous negotiated settings (SDTR and WDTR) while
3755    *   the driver will think that no negotiations have been
3756    *   performed.
3757    *
3758    * Some devices need a long time to "settle" after a SCSI
3759    * bus reset.
3760    */
3761 
3762   if (!aic7xxx_no_reset)
3763   {
3764     printk("aic7xxx: Resetting the SCSI bus...");
3765     if (p->bus_type == AIC_TWIN)
3766     {
3767       /*
3768        * Select Channel B.
3769        */
3770       outb((sblkctl & ~SELBUS_MASK) | SELBUSB, SBLKCTL + base);
3771 
3772       outb(SCSIRSTO, SCSISEQ + base);
3773       udelay(1000);
3774       outb(0, SCSISEQ + base);
3775 
3776       /*
3777        * Select Channel A.
3778        */
3779       outb((sblkctl & ~SELBUS_MASK) | SELNARROW, SBLKCTL + base);
3780     }
3781 
3782     outb(SCSIRSTO, SCSISEQ + base);
3783     udelay(1000);
3784     outb(0, SCSISEQ + base);
3785 
3786     aic7xxx_delay(AIC7XXX_RESET_DELAY);
3787 
3788     printk("done.\n");
3789   }
3790 
3791   /*
3792    * Unpause the sequencer before returning and enable
3793    * interrupts - we shouldn't get any until the first
3794    * command is sent to us by the high-level SCSI code.
3795    */
3796   UNPAUSE_SEQUENCER(p);
3797   return (found);
3798 }
3799 
3800 /*+F*************************************************************************
3801  * Function:
3802  *   aic7xxx_detect
3803  *
3804  * Description:
3805  *   Try to detect and register an Adaptec 7770 or 7870 SCSI controller.
3806  *-F*************************************************************************/
3807 int
3808 aic7xxx_detect(Scsi_Host_Template *template)
     /* [previous][next][first][last][top][bottom][index][help] */
3809 {
3810   int found = 0, slot, base;
3811   unsigned char irq = 0;
3812   int i;
3813   struct aic7xxx_host_config config;
3814 
3815   template->proc_dir = &proc_scsi_aic7xxx;
3816   config.chan_num = 0;
3817 
3818   /*
3819    * Since we may allow sharing of IRQs, it is imperative
3820    * that we "null-out" the aic7xxx_boards array. It is
3821    * not guaranteed to be initialized to 0 (NULL). We use
3822    * a NULL entry to indicate that no prior hosts have
3823    * been found/registered for that IRQ.
3824    */
3825   for (i = 0; i <= MAXIRQ; i++)
3826   {
3827     aic7xxx_boards[i] = NULL;
3828   }
3829 
3830   /*
3831    * Initialize the spurious count to 0.
3832    */
3833   aic7xxx_spurious_count = 0;
3834 
3835   /*
3836    * EISA/VL-bus card signature probe.
3837    */
3838   for (slot = MINSLOT; slot <= MAXSLOT; slot++)
3839   {
3840     base = SLOTBASE(slot) + MINREG;
3841 
3842     if (check_region(MINREG + base, MAXREG - MINREG))
3843     {
3844       /*
3845        * Some other driver has staked a
3846        * claim to this i/o region already.
3847        */
3848       continue;
3849     }
3850 
3851     config.type = aic7xxx_probe(slot, HID0 + base, &(config.bios));
3852     if (config.type != AIC_NONE)
3853     {
3854       /*
3855        * We found a card, allow 1 spurious interrupt.
3856        */
3857       aic7xxx_spurious_count = 1;
3858 
3859       /*
3860        * We "find" a AIC-7770 if we locate the card
3861        * signature and we can set it up and register
3862        * it with the kernel without incident.
3863        */
3864       config.chip_type = AIC_777x;
3865       config.base = base;
3866       config.irq = irq;
3867       config.parity = AIC_ENABLED;
3868       config.low_term = AIC_UNKNOWN;
3869       config.high_term = AIC_UNKNOWN;
3870       config.ultra_enabled = FALSE;
3871       config.extended = aic7xxx_extended;
3872       config.bus_speed = DFTHRSH_100;
3873       config.busrtime = BOFF_60BCLKS;
3874       found += aic7xxx_register(template, &config);
3875 
3876       /*
3877        * Disallow spurious interrupts.
3878        */
3879       aic7xxx_spurious_count = 0;
3880     }
3881   }
3882 
3883 #ifdef CONFIG_PCI
3884   /*
3885    * PCI-bus probe.
3886    */
3887   if (pcibios_present())
3888   {
3889     struct
3890     {
3891       unsigned short vendor_id;
3892       unsigned short device_id;
3893       aha_type       card_type;
3894       aha_chip_type  chip_type;
3895     } const aic7xxx_pci_devices[] = {
3896       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7850, AIC_7850, AIC_785x},
3897       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7855, AIC_7855, AIC_785x},
3898       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7870, AIC_7870, AIC_787x},
3899       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7871, AIC_7871, AIC_787x},
3900       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7872, AIC_7872, AIC_787x},
3901       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7873, AIC_7873, AIC_787x},
3902       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7874, AIC_7874, AIC_787x},
3903       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7880, AIC_7880, AIC_788x},
3904       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7881, AIC_7881, AIC_788x},
3905       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7882, AIC_7882, AIC_788x},
3906       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7883, AIC_7883, AIC_788x},
3907       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7884, AIC_7884, AIC_788x}
3908     };
3909 
3910     int error;
3911     int done = 0;
3912     unsigned int io_port;
3913     unsigned short index = 0;
3914     unsigned char pci_bus, pci_device_fn;
3915     unsigned int  csize_lattime;
3916     unsigned int  class_revid;
3917     unsigned int  devconfig;
3918     char rev_id[] = {'B', 'C', 'D'};
3919 
3920     for (i = 0; i < NUMBER(aic7xxx_pci_devices); i++)
3921     {
3922       done = FALSE;
3923       while (!done)
3924       {
3925         if (pcibios_find_device(aic7xxx_pci_devices[i].vendor_id,
3926                                 aic7xxx_pci_devices[i].device_id,
3927                                 index, &pci_bus, &pci_device_fn))
3928         {
3929           done = TRUE;
3930         }
3931         else  /* Found an Adaptec PCI device. */
3932         {
3933           config.type = aic7xxx_pci_devices[i].card_type;
3934           config.chip_type = aic7xxx_pci_devices[i].chip_type;
3935           config.chan_num = 0;
3936           config.bios = AIC_ENABLED;  /* Assume bios is enabled. */
3937           config.use_defaults = FALSE;
3938           config.busrtime = 40;
3939           switch (config.type)
3940           {
3941             case AIC_7850:
3942             case AIC_7855:
3943               config.bios = AIC_DISABLED;
3944               config.use_defaults = TRUE;
3945               config.bus_speed = DFTHRSH_100;
3946               break;
3947 
3948             case AIC_7872:  /* 3940 */
3949             case AIC_7882:  /* 3940-Ultra */
3950               config.chan_num = number_of_39xxs & 0x1;  /* Has 2 controllers */
3951               number_of_39xxs++;
3952               if (number_of_39xxs == 2)
3953               {
3954                 number_of_39xxs = 0;  /* To be consistent with 3985. */
3955               }
3956               break;
3957 
3958             case AIC_7873:  /* 3985 */
3959             case AIC_7883:  /* 3985-Ultra */
3960               config.chan_num = number_of_39xxs & 0x3;  /* Has 3 controllers */
3961               number_of_39xxs++;
3962               if (number_of_39xxs == 3)
3963               {
3964                 number_of_39xxs = 0;
3965               }
3966               break;
3967 
3968             default:
3969               break;
3970           }
3971 
3972           /*
3973            * Read sundry information from PCI BIOS.
3974            */
3975           error = pcibios_read_config_dword(pci_bus, pci_device_fn,
3976                                             PCI_BASE_ADDRESS_0, &io_port);
3977           error += pcibios_read_config_byte(pci_bus, pci_device_fn,
3978                                             PCI_INTERRUPT_LINE, &irq);
3979 
3980           /*
3981            * Ensure that we are using good values for the PCI burst size
3982            * and latency timer.
3983            */
3984           error += pcibios_read_config_dword(pci_bus, pci_device_fn,
3985                                              CSIZE_LATTIME, &csize_lattime);
3986           if ((csize_lattime & CACHESIZE) == 0)
3987           {
3988             /*
3989              * Default to 8DWDs - what's the PCI define for this?
3990              */
3991             csize_lattime |= 8;
3992           }
3993           if((csize_lattime & LATTIME) == 0)
3994           {
3995             /*
3996              * Default to 64 PCLKS (is this a good value?)
3997              * This may also be available in the SEEPROM??
3998              */
3999             csize_lattime |= (64 << 8);
4000           }
4001           pcibios_write_config_dword(pci_bus, pci_device_fn,
4002                                      CSIZE_LATTIME, csize_lattime);
4003           printk("aic7xxx: BurstLen = %d DWDs, Latency Timer = %d PCLKS\n",
4004                   (int) (csize_lattime & CACHESIZE),
4005                   (csize_lattime >> 8) & 0x000000ff);
4006 
4007           error += pcibios_read_config_dword(pci_bus, pci_device_fn,
4008                                              CLASS_PROGIF_REVID, &class_revid);
4009           if ((class_revid & DEVREVID) < 3)
4010           {
4011             printk("aic7xxx: %s Rev %c.\n", board_names[config.type],
4012                    rev_id[class_revid & DEVREVID]);
4013           }
4014 
4015           error += pcibios_read_config_dword(pci_bus, pci_device_fn,
4016                                              DEVCONFIG, &devconfig);
4017           if (error)
4018           {
4019             panic("aic7xxx: (aic7xxx_detect) Error %d reading PCI registers.\n",
4020                   error);
4021           }
4022 
4023           printk("aic7xxx: devconfig = 0x%x.\n", devconfig);
4024 
4025           /*
4026            * The first bit of PCI_BASE_ADDRESS_0 is always set, so
4027            * we mask it off.
4028            */
4029           base = io_port & 0xfffffffe;
4030 
4031           /*
4032            * I don't think we need to bother with allowing
4033            * spurious interrupts for the 787x/785x, but what
4034            * the hey.
4035            */
4036           aic7xxx_spurious_count = 1;
4037 
4038           config.base = base;
4039           config.irq = irq;
4040           config.parity = AIC_ENABLED;
4041           config.low_term = AIC_UNKNOWN;
4042           config.high_term = AIC_UNKNOWN;
4043           config.extended = aic7xxx_extended;
4044           config.ultra_enabled = FALSE;
4045           if (devconfig & RAMPSM)
4046           {
4047             /*
4048              * External SRAM present.  The probe will walk the SCBs to see
4049              * how much SRAM we have and set the number of SCBs accordingly.
4050              * We have to turn off SCBRAMSEL to access the external SCB
4051              * SRAM.
4052              *
4053              * It seems that early versions of the aic7870 didn't use these
4054              * bits, hence the hack for the 3940 above.  I would guess that
4055              * recent 3940s using later aic7870 or aic7880 chips do actually
4056              * set RAMPSM.
4057              *
4058              * The documentation isn't clear, but it sounds like the value
4059              * written to devconfig must not have RAMPSM set.  The second
4060              * sixteen bits of the register are R/O anyway, so it shouldn't
4061              * affect RAMPSM either way.
4062              */
4063             printk ("aic7xxx: External RAM detected. Enabling RAM access.\n");
4064             devconfig &= ~(RAMPSM | SCBRAMSEL);
4065             pcibios_write_config_dword(pci_bus, pci_device_fn,
4066                                        DEVCONFIG, devconfig);
4067           }
4068           found += aic7xxx_register(template, &config);
4069 
4070           /*
4071            * Disable spurious interrupts.
4072            */
4073           aic7xxx_spurious_count = 0;
4074 
4075           index++;
4076         }  /* Found an Adaptec PCI device. */
4077       }
4078     }
4079   }
4080 #endif CONFIG_PCI
4081 
4082   template->name = aic7xxx_info(NULL);
4083   return (found);
4084 }
4085 
4086 
4087 /*+F*************************************************************************
4088  * Function:
4089  *   aic7xxx_buildscb
4090  *
4091  * Description:
4092  *   Build a SCB.
4093  *-F*************************************************************************/
4094 static void
4095 aic7xxx_buildscb(struct aic7xxx_host *p,
     /* [previous][next][first][last][top][bottom][index][help] */
4096                  Scsi_Cmnd *cmd,
4097                  struct aic7xxx_scb *scb)
4098 {
4099   void *addr;
4100   unsigned short mask;
4101   struct scatterlist *sg;
4102 
4103   /*
4104    * Setup the control byte if we need negotiation and have not
4105    * already requested it.
4106    */
4107 #ifdef AIC7XXX_TAGGED_QUEUEING
4108   if (cmd->device->tagged_supported)
4109   {
4110     if (cmd->device->tagged_queue == 0)
4111     {
4112       printk("aic7xxx: Enabling tagged queuing for target %d, "
4113              "channel %d.\n", cmd->target, cmd->channel);
4114       cmd->device->tagged_queue = 1;
4115       cmd->device->current_tag = 1;  /* enable tagging */
4116     }
4117     cmd->tag = cmd->device->current_tag;
4118     cmd->device->current_tag++;
4119     scb->control |= TAG_ENB;
4120   }
4121 #endif
4122   mask = (0x01 << (cmd->target | (cmd->channel << 3)));
4123   if (p->discenable & mask)
4124   {
4125     scb->control |= DISCENB;
4126   }
4127   if ((p->needwdtr & mask) && !(p->wdtr_pending & mask))
4128   {
4129     p->wdtr_pending |= mask;
4130     scb->control |= NEEDWDTR;
4131 #if 0
4132     printk("aic7xxx: Sending WDTR request to target %d.\n", cmd->target);
4133 #endif
4134   }
4135   else
4136   {
4137     if ((p->needsdtr & mask) && !(p->sdtr_pending & mask))
4138     {
4139       p->sdtr_pending |= mask;
4140       scb->control |= NEEDSDTR;
4141 #if 0
4142       printk("aic7xxx: Sending SDTR request to target %d.\n", cmd->target);
4143 #endif
4144     }
4145   }
4146 
4147 #if 0
4148   printk("aic7xxx: (build_scb) Target %d, cmd(0x%x) size(%u) wdtr(0x%x) "
4149          "mask(0x%x).\n",
4150          cmd->target, cmd->cmnd[0], cmd->cmd_len, p->needwdtr, mask);
4151 #endif
4152   scb->target_channel_lun = ((cmd->target << 4) & 0xF0) |
4153         ((cmd->channel & 0x01) << 3) | (cmd->lun & 0x07);
4154 
4155   /*
4156    * The interpretation of request_buffer and request_bufflen
4157    * changes depending on whether or not use_sg is zero; a
4158    * non-zero use_sg indicates the number of elements in the
4159    * scatter-gather array.
4160    */
4161 
4162   /*
4163    * XXX - this relies on the host data being stored in a
4164    *       little-endian format.
4165    */
4166   addr = cmd->cmnd;
4167   scb->SCSI_cmd_length = cmd->cmd_len;
4168   memcpy(scb->SCSI_cmd_pointer, &addr, sizeof(scb->SCSI_cmd_pointer));
4169 
4170   if (cmd->use_sg)
4171   {
4172     scb->SG_segment_count = cmd->use_sg;
4173     memcpy(scb->SG_list_pointer, &cmd->request_buffer,
4174            sizeof(scb->SG_list_pointer));
4175     memcpy(&sg, &cmd->request_buffer, sizeof(sg));
4176     memcpy(scb->data_pointer, &(sg[0].address), sizeof(scb->data_pointer));
4177     scb->data_count = sg[0].length;
4178 #if 0
4179     debug("aic7xxx: (build_scb) SG segs(%d), length(%u), sg[0].length(%d).\n",
4180            cmd->use_sg, aic7xxx_length(cmd, 0), scb->data_count);
4181 #endif
4182   }
4183   else
4184   {
4185 #if 0
4186   debug("aic7xxx: (build_scb) Creating scatterlist, addr(0x%lx) length(%d).\n",
4187         (unsigned long) cmd->request_buffer, cmd->request_bufflen);
4188 #endif
4189     if (cmd->request_bufflen == 0)
4190     {
4191       /*
4192        * In case the higher level SCSI code ever tries to send a zero
4193        * length command, ensure the SCB indicates no data.  The driver
4194        * will interpret a zero length command as a Bus Device Reset.
4195        */
4196       scb->SG_segment_count = 0;
4197       memset(scb->SG_list_pointer, 0, sizeof(scb->SG_list_pointer));
4198       memset(scb->data_pointer, 0, sizeof(scb->data_pointer));
4199       scb->data_count = 0;
4200     }
4201     else
4202     {
4203       scb->SG_segment_count = 1;
4204       scb->sg.address = (char *) cmd->request_buffer;
4205       scb->sg.length = cmd->request_bufflen;
4206       addr = &scb->sg;
4207       memcpy(scb->SG_list_pointer, &addr, sizeof(scb->SG_list_pointer));
4208       scb->data_count = scb->sg.length;
4209       memcpy(scb->data_pointer, &cmd->request_buffer, sizeof(scb->data_pointer));
4210     }
4211   }
4212 }
4213 
4214 /*+F*************************************************************************
4215  * Function:
4216  *   aic7xxx_queue
4217  *
4218  * Description:
4219  *   Queue a SCB to the controller.
4220  *-F*************************************************************************/
4221 int
4222 aic7xxx_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
     /* [previous][next][first][last][top][bottom][index][help] */
4223 {
4224   long flags;
4225   struct aic7xxx_host *p;
4226   struct aic7xxx_scb *scb;
4227 
4228   p = (struct aic7xxx_host *) cmd->host->hostdata;
4229 
4230   /*
4231    * Check to see if channel was scanned.
4232    */
4233   if (!p->a_scanned && (cmd->channel == 0))
4234   {
4235     printk("aic7xxx: Scanning channel A for devices.\n");
4236     p->a_scanned = TRUE;
4237   }
4238   else
4239   {
4240     if (!p->b_scanned && (cmd->channel == 1))
4241     {
4242       printk("aic7xxx: Scanning channel B for devices.\n");
4243       p->b_scanned = TRUE;
4244     }
4245   }
4246 
4247 #if 0
4248   debug("aic7xxx: (queue) cmd(0x%x) size(%u), target %d, channel %d, lun %d.\n",
4249         cmd->cmnd[0], cmd->cmd_len, cmd->target, cmd->channel,
4250         cmd->lun & 0x07);
4251 #endif
4252 
4253   /*
4254    * This is a critical section, since we don't want the
4255    * interrupt routine mucking with the host data or the
4256    * card. Since the kernel documentation is vague on
4257    * whether or not we are in a cli/sti pair already, save
4258    * the flags to be on the safe side.
4259    */
4260   save_flags(flags);
4261   cli();
4262 
4263   /*
4264    * Find a free slot in the SCB array to load this command
4265    * into. Since can_queue is set to the maximum number of
4266    * SCBs for the card, we should always find one.
4267    *
4268    * First try to find an scb in the free list. If there are
4269    * none in the free list, then check the current number of
4270    * of scbs and take an unused one from the scb array.
4271    */
4272   scb = p->free_scb;
4273   if (scb != NULL)
4274   { /* found one in the free list */
4275     p->free_scb = scb->next;   /* remove and update head of list */
4276     /*
4277      * Warning! For some unknown reason, the scb at the head
4278      * of the free list is not the same address that it should
4279      * be. That's why we set the scb pointer taken by the
4280      * position in the array. The scb at the head of the list
4281      * should match this address, but it doesn't.
4282      */
4283     scb = &(p->scb_array[scb->position]);
4284     scb->control = 0;
4285     scb->state = SCB_ACTIVE;
4286   }
4287   else
4288   {
4289     if (p->numscb >= p->maxscb)
4290     {
4291       panic("aic7xxx: (aic7xxx_queue) Couldn't find a free SCB.\n");
4292     }
4293     else
4294     {
4295       /*
4296        * Initialize the scb within the scb array. The
4297        * position within the array is the position on
4298        * the board that it will be loaded.
4299        */
4300       scb = &(p->scb_array[p->numscb]);
4301       memset(scb, 0, sizeof(*scb));
4302 
4303       scb->position = p->numscb;
4304       p->numscb++;
4305       scb->state = SCB_ACTIVE;
4306     }
4307   }
4308 
4309   scb->cmd = cmd;
4310   aic7xxx_position(cmd) = scb->position;
4311 #if 0
4312   debug_scb(scb);
4313 #endif;
4314 
4315   /*
4316    * Construct the SCB beforehand, so the sequencer is
4317    * paused a minimal amount of time.
4318    */
4319   aic7xxx_buildscb(p, cmd, scb);
4320 
4321 #if 0
4322   if (scb != &p->scb_array[scb->position])
4323   {
4324     printk("aic7xxx: (queue) Address of SCB by position does not match SCB "
4325            "address.\n");
4326   }
4327   printk("aic7xxx: (queue) SCB pos(%d) cmdptr(0x%x) state(%d) freescb(0x%x)\n",
4328          scb->position, (unsigned int) scb->cmd,
4329          scb->state, (unsigned int) p->free_scb);
4330 #endif
4331 
4332   /*
4333    * Make sure the Scsi_Cmnd pointer is saved, the struct it points to
4334    * is set up properly, and the parity error flag is reset, then send
4335    * the SCB to the sequencer and watch the fun begin.
4336    */
4337   cmd->scsi_done = fn;
4338   aic7xxx_error(cmd) = DID_OK;
4339   aic7xxx_status(cmd) = 0;
4340   cmd->result = 0;
4341   memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
4342 
4343   /*
4344    * Pause the sequencer so we can play with its registers -
4345    * wait for it to acknowledge the pause.
4346    *
4347    * XXX - should the interrupts be left on while doing this?
4348    */
4349   PAUSE_SEQUENCER(p);
4350 
4351   /*
4352    * Save the SCB pointer and put our own pointer in - this
4353    * selects one of the four banks of SCB registers. Load
4354    * the SCB, then write its pointer into the queue in FIFO
4355    * and restore the saved SCB pointer.
4356    */
4357   aic7xxx_putscb(p, scb);
4358   outb(scb->position, QINFIFO + p->base);
4359 
4360   UNPAUSE_SEQUENCER(p);
4361 #if 0
4362   printk("aic7xxx: (queue) After - cmd(0x%lx) scb->cmd(0x%lx) pos(%d).\n",
4363          (long) cmd, (long) scb->cmd, scb->position);
4364 #endif;
4365   restore_flags(flags);
4366   return (0);
4367 }
4368 
4369 /*+F*************************************************************************
4370  * Function:
4371  *   aic7xxx_abort_reset
4372  *
4373  * Description:
4374  *   Abort or reset the current SCSI command(s).  If the scb has not
4375  *   previously been aborted, then we attempt to send a BUS_DEVICE_RESET
4376  *   message to the target.  If the scb has previously been unsuccessfully
4377  *   aborted, then we will reset the channel and have all devices renegotiate.
4378  *   Returns an enumerated type that indicates the status of the operation.
4379  *-F*************************************************************************/
4380 static aha_abort_reset_type
4381 aic7xxx_abort_reset(Scsi_Cmnd *cmd, unsigned char errcode)
     /* [previous][next][first][last][top][bottom][index][help] */
4382 {
4383   struct aic7xxx_scb  *scb;
4384   struct aic7xxx_host *p;
4385   long flags;
4386   unsigned char bus_state;
4387   aha_abort_reset_type scb_status = ABORT_RESET_SUCCESS;
4388   int base, found;
4389   char channel;
4390 
4391   p = (struct aic7xxx_host *) cmd->host->hostdata;
4392   scb = &(p->scb_array[aic7xxx_position(cmd)]);
4393   base = p->base;
4394   channel = scb->target_channel_lun & SELBUSB ? 'B': 'A';
4395 
4396   save_flags(flags);
4397   cli();
4398 
4399   if (scb->state & SCB_ACTIVE)
4400   {
4401     /*
4402      * Ensure that the card doesn't do anything
4403      * behind our back.
4404      */
4405     PAUSE_SEQUENCER(p);
4406 
4407     printk ("aic7xxx: (abort_reset) scb state 0x%x, ", scb->state);
4408     bus_state = inb(LASTPHASE + p->base);
4409 
4410     switch (bus_state)
4411     {
4412       case P_DATAOUT:
4413         printk ("Data-Out phase, ");
4414         break;
4415       case P_DATAIN:
4416         printk ("Data-In phase, ");
4417         break;
4418       case P_COMMAND:
4419         printk ("Command phase, ");
4420         break;
4421       case P_MESGOUT:
4422         printk ("Message-Out phase, ");
4423         break;
4424       case P_STATUS:
4425         printk ("Status phase, ");
4426         break;
4427       case P_MESGIN:
4428         printk ("Message-In phase, ");
4429         break;
4430       default:
4431         printk ("while idle, LASTPHASE = 0x%x, ", bus_state);
4432         /*
4433          * We're not in a valid phase, so assume we're idle.
4434          */
4435         bus_state = 0;
4436         break;
4437     }
4438     printk ("SCSISIGI = 0x%x\n", inb (p->base + SCSISIGI));
4439 
4440     /*
4441      * First, determine if we want to do a bus reset or simply a bus device
4442      * reset.  If this is the first time that a transaction has timed out,
4443      * just schedule a bus device reset.  Otherwise, we reset the bus and
4444      * abort all pending I/Os on that bus.
4445      */
4446     if (scb->state & SCB_ABORTED)
4447     {
4448       /*
4449        * Been down this road before.  Do a full bus reset.
4450        */
4451       found = aic7xxx_reset_channel(p, channel, scb->position, TRUE);
4452     }
4453     else
4454     {
4455       unsigned char active_scb, control;
4456       struct aic7xxx_scb *active_scbp;
4457 
4458       /*
4459        * Send a Bus Device Reset Message:
4460        * The target we select to send the message to may be entirely
4461        * different than the target pointed to by the scb that timed
4462        * out.  If the command is in the QINFIFO or the waiting for
4463        * selection list, its not tying up the bus and isn't responsible
4464        * for the delay so we pick off the active command which should
4465        * be the SCB selected by SCBPTR.  If its disconnected or active,
4466        * we device reset the target scbp points to.  Although it may
4467        * be that this target is not responsible for the delay, it may
4468        * may also be that we're timing out on a command that just takes
4469        * too much time, so we try the bus device reset there first.
4470        */
4471       active_scb = inb(SCBPTR + base);
4472       active_scbp = &(p->scb_array[active_scb]);
4473       control = inb(SCB_CONTROL + base);
4474 
4475       /*
4476        * Test to see if scbp is disconnected
4477        */
4478       outb(scb->position, SCBPTR + base);
4479       if (inb(SCB_CONTROL + base) & DISCONNECTED)
4480       {
4481 #ifdef AIC7XXX_DEBUG_ABORT
4482         printk ("aic7xxx: (abort_scb) scb %d is disconnected; "
4483                 "bus device reset message queued.\n", scb->position);
4484 #endif
4485         scb->state |= (SCB_DEVICE_RESET | SCB_ABORTED);
4486         scb->SG_segment_count = 0;
4487         memset(scb->SG_list_pointer, 0, sizeof(scb->SG_list_pointer));
4488         memset(scb->data_pointer, 0, sizeof(scb->data_pointer));
4489         scb->data_count = 0;
4490         aic7xxx_putscb(p, scb);
4491         aic7xxx_add_waiting_scb(base, scb);
4492         aic7xxx_error(scb->cmd) = errcode;
4493         scb_status = ABORT_RESET_PENDING;
4494         outb(active_scb, SCBPTR + base);
4495         UNPAUSE_SEQUENCER(p);
4496       }
4497       else
4498       {
4499         /*
4500          * Is the active SCB really active?
4501          */
4502         if ((active_scbp->state & SCB_ACTIVE) && bus_state)
4503         {
4504           /*
4505            * Load the message buffer and assert attention.
4506            */
4507           active_scbp->state |= (SCB_DEVICE_RESET | SCB_ABORTED);
4508           outb(1, MSG_LEN + base);
4509           outb(MSG_BUS_DEVICE_RESET, MSG0 + base);
4510           outb(bus_state | ATNO, SCSISIGO + base);
4511 #ifdef AIC7XXX_DEBUG_ABORT
4512           printk ("aic7xxx: (abort_scb) asserted ATN - "
4513                   "bus device reset in message buffer.\n");
4514 #endif
4515           if (active_scbp != scb)
4516           {
4517             /*
4518              * XXX - We would like to increment the timeout on scb, but
4519              *       access to that routine is denied because it is hidden
4520              *       in scsi.c.  If we were able to do this, it would give
4521              *       scb a new lease on life.
4522              */
4523             ;
4524           }
4525           aic7xxx_error(scb->cmd) = errcode;
4526           scb_status = ABORT_RESET_PENDING;
4527           /*
4528            * Restore the active SCB and unpause the sequencer.
4529            */
4530           outb(active_scb, SCBPTR + base);
4531 
4532           UNPAUSE_SEQUENCER(p);
4533         }
4534         else
4535         {
4536 #ifdef AIC7XXX_DEBUG_ABORT
4537             printk ("aic7xxx: (abort_scb) no active command.\n");
4538 #endif
4539           /*
4540            * No active command to single out, so reset
4541            * the bus for the timed out target.
4542            */
4543           aic7xxx_reset_channel(p, channel, scb->position, TRUE);
4544         }
4545       }
4546     }
4547   }
4548   else
4549   {
4550     /*
4551      * The scb is not active and must have completed after the timeout
4552      * check in scsi.c and before we check the scb state above.  For
4553      * this case we return SCSI_ABORT_NOT_RUNNING (if abort was called)
4554      * or SCSI_RESET_SUCCESS (if reset was called).
4555      */
4556 #ifdef AIC7XXX_DEBUG_ABORT
4557     printk ("aic7xxx: (abort_reset) called with no active scb, errcode 0x%x\n",
4558             errcode);
4559 #endif
4560     scb_status = ABORT_RESET_INACTIVE;
4561     /*
4562      * According to the comments in scsi.h and Michael Neuffer, if we do not
4563      * have an active command for abort or reset, we should not call the
4564      * command done function.  Unfortunately, this hangs the system for me
4565      * unless we *do* call the done function.
4566      *
4567      * XXX - Revisit this sometime!
4568      */
4569     cmd->result = errcode << 16;
4570     cmd->scsi_done(cmd);
4571   }
4572 
4573   restore_flags(flags);
4574   return (scb_status);
4575 }
4576 
4577 
4578 /*+F*************************************************************************
4579  * Function:
4580  *   aic7xxx_abort
4581  *
4582  * Description:
4583  *   Abort the current SCSI command(s).
4584  *-F*************************************************************************/
4585 int
4586 aic7xxx_abort(Scsi_Cmnd *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
4587 {
4588 #ifdef AIC7XXX_DEBUG_ABORT
4589   printk ("aic7xxx: (abort) target/channel %d/%d\n", cmd->target, cmd->channel);
4590 #endif
4591 
4592   switch (aic7xxx_abort_reset(cmd, DID_ABORT))
4593   {
4594     case ABORT_RESET_INACTIVE:
4595       return (SCSI_ABORT_NOT_RUNNING);
4596       break;
4597     case ABORT_RESET_PENDING:
4598       return (SCSI_ABORT_PENDING);
4599       break;
4600     case ABORT_RESET_SUCCESS:
4601     default:
4602       return (SCSI_ABORT_SUCCESS);
4603       break;
4604   }
4605 }
4606 
4607 /*+F*************************************************************************
4608  * Function:
4609  *   aic7xxx_reset
4610  *
4611  * Description:
4612  *   Resetting the bus always succeeds - is has to, otherwise the
4613  *   kernel will panic! Try a surgical technique - sending a BUS
4614  *   DEVICE RESET message - on the offending target before pulling
4615  *   the SCSI bus reset line.
4616  *-F*************************************************************************/
4617 int
4618 aic7xxx_reset(Scsi_Cmnd *cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
4619 {
4620 #ifdef AIC7XXX_DEBUG_ABORT
4621   printk ("aic7xxx: (reset) target/channel %d/%d\n", cmd->target, cmd->channel);
4622 #endif
4623 
4624   switch (aic7xxx_abort_reset(cmd, DID_RESET))
4625   {
4626     case ABORT_RESET_PENDING:
4627       return (SCSI_RESET_PENDING);
4628       break;
4629     case ABORT_RESET_SUCCESS:
4630       return (SCSI_RESET_BUS_RESET | SCSI_RESET_SUCCESS);
4631       break;
4632     case ABORT_RESET_INACTIVE:
4633     default:
4634       return (SCSI_RESET_SUCCESS);
4635       break;
4636   }
4637 }
4638 
4639 /*+F*************************************************************************
4640  * Function:
4641  *   aic7xxx_biosparam
4642  *
4643  * Description:
4644  *   Return the disk geometry for the given SCSI device.
4645  *-F*************************************************************************/
4646 int
4647 aic7xxx_biosparam(Disk *disk, kdev_t dev, int geom[])
     /* [previous][next][first][last][top][bottom][index][help] */
4648 {
4649   int heads, sectors, cylinders;
4650   struct aic7xxx_host *p;
4651 
4652   p = (struct aic7xxx_host *) disk->device->host->hostdata;
4653 
4654   /*
4655    * XXX - if I could portably find the card's configuration
4656    *       information, then this could be autodetected instead
4657    *       of left to a boot-time switch.
4658    */
4659   heads = 64;
4660   sectors = 32;
4661   cylinders = disk->capacity / (heads * sectors);
4662 
4663   if (p->extended && cylinders > 1024)
4664   {
4665     heads = 255;
4666     sectors = 63;
4667     cylinders = disk->capacity / (255 * 63);
4668   }
4669 
4670   geom[0] = heads;
4671   geom[1] = sectors;
4672   geom[2] = cylinders;
4673 
4674   return (0);
4675 }
4676 
4677 #include "aic7xxx_proc.c"
4678 
4679 #ifdef MODULE
4680 /* Eventually this will go into an include file, but this will be later */
4681 Scsi_Host_Template driver_template = AIC7XXX;
4682 
4683 #include "scsi_module.c"
4684 #endif
4685 
4686 /*
4687  * Overrides for Emacs so that we almost follow Linus's tabbing style.
4688  * Emacs will notice this stuff at the end of the file and automatically
4689  * adjust the settings for this buffer only.  This must remain at the end
4690  * of the file.
4691  * ---------------------------------------------------------------------------
4692  * Local variables:
4693  * c-indent-level: 2
4694  * c-brace-imaginary-offset: 0
4695  * c-brace-offset: -2
4696  * c-argdecl-indent: 2
4697  * c-label-offset: -2
4698  * c-continued-statement-offset: 2
4699  * c-continued-brace-offset: 0
4700  * indent-tabs-mode: nil
4701  * tab-width: 8
4702  * End:
4703  */
4704 

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