Annotation of Gnu-Mach/linux/src/drivers/scsi/aic7xxx.c, revision 1.1.1.2

1.1       root        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:  *
                     32:  *  Modifications by Daniel M. Eischen ([email protected]):
                     33:  *
                     34:  *  Substantially modified to include support for wide and twin bus
                     35:  *  adapters, DMAing of SCBs, tagged queueing, IRQ sharing, bug fixes,
                     36:  *  SCB paging, and other rework of the code.
                     37:  *
                     38:  *  Parts of this driver were also based on the FreeBSD driver by
                     39:  *  Justin T. Gibbs.  His copyright follows:
                     40:  *
1.1.1.2 ! root       41:  * --------------------------------------------------------------------------
1.1       root       42:  * Copyright (c) 1994-1997 Justin Gibbs.
                     43:  * All rights reserved.
                     44:  *
                     45:  * Redistribution and use in source and binary forms, with or without
                     46:  * modification, are permitted provided that the following conditions
                     47:  * are met:
                     48:  * 1. Redistributions of source code must retain the above copyright
                     49:  *    notice, this list of conditions, and the following disclaimer,
                     50:  *    without modification, immediately at the beginning of the file.
                     51:  * 2. Redistributions in binary form must reproduce the above copyright
                     52:  *    notice, this list of conditions and the following disclaimer in the
                     53:  *    documentation and/or other materials provided with the distribution.
                     54:  * 3. The name of the author may not be used to endorse or promote products
                     55:  *    derived from this software without specific prior written permission.
                     56:  *
1.1.1.2 ! root       57:  * Where this Software is combined with software released under the terms of
        !            58:  * the GNU Public License ("GPL") and the terms of the GPL would require the
1.1       root       59:  * combined work to also be released under the terms of the GPL, the terms
                     60:  * and conditions of this License will apply in addition to those of the
                     61:  * GPL with the exception of any terms or conditions of this License that
                     62:  * conflict with, or are expressly prohibited by, the GPL.
                     63:  *
                     64:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     65:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     66:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     67:  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
                     68:  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     69:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     70:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     71:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     72:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     73:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     74:  * SUCH DAMAGE.
                     75:  *
1.1.1.2 ! root       76:  *      $Id: aic7xxx.c,v 1.1.4.1 2004/01/16 22:41:26 roland Exp $
1.1       root       77:  *---------------------------------------------------------------------------
                     78:  *
                     79:  *  Thanks also go to (in alphabetical order) the following:
                     80:  *
                     81:  *    Rory Bolt     - Sequencer bug fixes
                     82:  *    Jay Estabrook - Initial DEC Alpha support
                     83:  *    Doug Ledford  - Much needed abort/reset bug fixes
                     84:  *    Kai Makisara  - DMAing of SCBs
                     85:  *
                     86:  *  A Boot time option was also added for not resetting the scsi bus.
                     87:  *
                     88:  *    Form:  aic7xxx=extended
                     89:  *           aic7xxx=no_reset
                     90:  *           aic7xxx=ultra
                     91:  *           aic7xxx=irq_trigger:[0,1]  # 0 edge, 1 level
                     92:  *           aic7xxx=verbose
                     93:  *
                     94:  *  Daniel M. Eischen, [email protected], 1/23/97
                     95:  *
                     96:  *-M*************************************************************************/
                     97: 
                     98: /*+M**************************************************************************
                     99:  *
                    100:  * Further driver modifications made by Doug Ledford <[email protected]>
                    101:  *
                    102:  * Copyright (c) 1997-1998 Doug Ledford
                    103:  *
                    104:  * These changes are released under the same licensing terms as the FreeBSD
                    105:  * driver written by Justin Gibbs.  Please see his Copyright notice above
                    106:  * for the exact terms and conditions covering my changes as well as the
                    107:  * warranty statement.
                    108:  *
                    109:  * Modifications made to the aic7xxx.c,v 4.1 driver from Dan Eischen include
                    110:  * but are not limited to:
                    111:  *
                    112:  *  1: Import of the latest FreeBSD sequencer code for this driver
                    113:  *  2: Modification of kernel code to accomodate different sequencer semantics
                    114:  *  3: Extensive changes throughout kernel portion of driver to improve
                    115:  *     abort/reset processing and error hanndling
                    116:  *  4: Other work contributed by various people on the Internet
                    117:  *  5: Changes to printk information and verbosity selection code
                    118:  *  6: General reliability related changes, especially in IRQ management
                    119:  *  7: Modifications to the default probe/attach order for supported cards
                    120:  *  8: SMP friendliness has been improved
                    121:  *
                    122:  * Overall, this driver represents a significant departure from the official
                    123:  * aic7xxx driver released by Dan Eischen in two ways.  First, in the code
                    124:  * itself.  A diff between the two version of the driver is now a several
                    125:  * thousand line diff.  Second, in approach to solving the same problem.  The
                    126:  * problem is importing the FreeBSD aic7xxx driver code to linux can be a
                    127:  * difficult and time consuming process, that also can be error prone.  Dan
                    128:  * Eischen's official driver uses the approach that the linux and FreeBSD
                    129:  * drivers should be as identical as possible.  To that end, his next version
                    130:  * of this driver will be using a mid-layer code library that he is developing
                    131:  * to moderate communications between the linux mid-level SCSI code and the
                    132:  * low level FreeBSD driver.  He intends to be able to essentially drop the
                    133:  * FreeBSD driver into the linux kernel with only a few minor tweaks to some
                    134:  * include files and the like and get things working, making for fast easy
                    135:  * imports of the FreeBSD code into linux.
                    136:  *
                    137:  * I disagree with Dan's approach.  Not that I don't think his way of doing
                    138:  * things would be nice, easy to maintain, and create a more uniform driver
                    139:  * between FreeBSD and Linux.  I have no objection to those issues.  My
                    140:  * disagreement is on the needed functionality.  There simply are certain
                    141:  * things that are done differently in FreeBSD than linux that will cause
                    142:  * problems for this driver regardless of any middle ware Dan implements.
                    143:  * The biggest example of this at the moment is interrupt semantics.  Linux
                    144:  * doesn't provide the same protection techniques as FreeBSD does, nor can
                    145:  * they be easily implemented in any middle ware code since they would truly
                    146:  * belong in the kernel proper and would effect all drivers.  For the time
1.1.1.2 ! root      147:  * being, I see issues such as these as major stumbling blocks to the
1.1       root      148:  * reliability of code based upon such middle ware.  Therefore, I choose to
                    149:  * use a different approach to importing the FreeBSD code that doesn't
                    150:  * involve any middle ware type code.  My approach is to import the sequencer
                    151:  * code from FreeBSD wholesale.  Then, to only make changes in the kernel
                    152:  * portion of the driver as they are needed for the new sequencer semantics.
                    153:  * In this way, the portion of the driver that speaks to the rest of the
                    154:  * linux kernel is fairly static and can be changed/modified to solve
                    155:  * any problems one might encounter without concern for the FreeBSD driver.
                    156:  *
                    157:  * Note: If time and experience should prove me wrong that the middle ware
                    158:  * code Dan writes is reliable in its operation, then I'll retract my above
                    159:  * statements.  But, for those that don't know, I'm from Missouri (in the US)
                    160:  * and our state motto is "The Show-Me State".  Well, before I will put
                    161:  * faith into it, you'll have to show me that it works :)
                    162:  *
                    163:  *_M*************************************************************************/
                    164: 
                    165: /*
                    166:  * The next three defines are user configurable.  These should be the only
                    167:  * defines a user might need to get in here and change.  There are other
                    168:  * defines buried deeper in the code, but those really shouldn't need touched
                    169:  * under normal conditions.
                    170:  */
                    171: 
                    172: /*
                    173:  * AIC7XXX_FAKE_NEGOTIATION_CMDS
                    174:  *   We now have two distinctly different methods of device negotiation
                    175:  *   in this code.  The two methods are selected by either defining or not
                    176:  *   defining this option.  The difference is as follows:
                    177:  *
                    178:  *   With AIC7XXX_FAKE_NEGOTIATION_CMDS not set (commented out)
                    179:  *     When the driver is in need of issuing a negotiation command for any
                    180:  *     given device, it will add the negotiation message on to part of a
                    181:  *     regular SCSI command for the device.  In the process, if the device
                    182:  *     is configured for and using tagged queueing, then the code will
                    183:  *     also issue that single command as a non-tagged command, attach the
                    184:  *     negotiation message to that one command, and use a temporary
                    185:  *     queue depth of one to keep the untagged and tagged commands from
                    186:  *     overlapping.
                    187:  *       Pros: This doesn't use any extra SCB structures, it's simple, it
                    188:  *         works most of the time (if not all of the time now), and
                    189:  *         since we get the device capability info frmo the INQUIRY data
                    190:  *         now, shouldn't cause any problems.
                    191:  *       Cons: When we need to send a negotiation command to a device, we
                    192:  *         must use a command that is being sent to LUN 0 of the device.
                    193:  *         If we try sending one to high LUN numbers, then some devices
                    194:  *         get noticeably upset.  Since we have to wait for a command with
                    195:  *         LUN == 0 to come along, we may not be able to renegotiate when
                    196:  *         we want if the user is actually using say LUN 1 of a CD Changer
                    197:  *         instead of using LUN 0 for an extended period of time.
                    198:  *
                    199:  *   With AIC7XXX_FAKE_NEGOTIATION_CMDS defined
                    200:  *     When we need to negotiate with a device, instead of attaching our
                    201:  *     negotiation message to an existing command, we insert our own
                    202:  *     fictional Scsi_Cmnd into the chain that has the negotiation message
                    203:  *     attached to it.  We send this one command as untagged regardless
                    204:  *     of the device type, and we fiddle with the queue depth the same as
                    205:  *     we would with the option unset to avoid overlapping commands.  The
                    206:  *     primary difference between this and the unset option is that the
                    207:  *     negotiation message is no longer attached to a specific command,
                    208:  *     instead it is its own command and is merely triggered by a
                    209:  *     combination of both A) We need to negotiate and B) The mid level
                    210:  *     SCSI code has sent us a command.  We still don't do any negotiation
                    211:  *     unless there is a valid SCSI command to be processed.
                    212:  *       Pros: This fixes the problem above in the Cons section.  Since we
                    213:  *         issue our own fake command, we can set the LUN to 0 regardless
                    214:  *         of what the LUN is in the real command.  It also means that if
                    215:  *         the device get's nasty over negotiation issues, it won't be
                    216:  *         showing up on a regular command, so we won't get any SENSE buffer
                    217:  *         data or STATUS_BYTE returns to the mid level code that are caused
                    218:  *         by snits in the negotiation code.
                    219:  *       Cons: We add more code, and more complexity.  This means more ways
                    220:  *         in which things could break.  It means a larger driver.  It means
                    221:  *         more resource consumption for the fake commands.  However, the
                    222:  *         biggest problem is this.  Take a system where there is a CD-ROM
                    223:  *         on the SCSI bus.  Someone has a CD in the CD-ROM and is using it.
                    224:  *         For some reason the SCSI bus gets reset.  We don't touch the
                    225:  *         CD-ROM again for quite a period of time (so we don't renegotiate
                    226:  *         after the reset until we do touch the CD-ROM again).  In the
                    227:  *         time while we aren't using the CD-ROM, the current disc is
                    228:  *         removed and a new one put in.  When we go to check that disc, we
                    229:  *         will first have to renegotiate.  In so doing, we issue our fake
                    230:  *         SCSI command, which happens to be TEST_UNIT_READY.  The CD-ROM
                    231:  *         negotiates with us, then responds to our fake command with a
                    232:  *         CHECK_CONDITION status.  We REQUEST_SENSE from the CD-ROM, it
                    233:  *         then sends the SENSE data to our fake command to tell it that
                    234:  *         it has been through a disc change.  There, now we've cleared out
                    235:  *         the SENSE data along with our negotiation command, and when the
                    236:  *         real command executes, it won't pick up that the CD was changed.
                    237:  *         That's the biggest Con to this approach.  In the future, I could
                    238:  *         probably code around this problem though, so this option is still
                    239:  *         viable.
                    240:  *
                    241:  *  So, which command style should you use?  I would appreciate it if people
                    242:  *  could try out both types.  I want to know about any cases where one
                    243:  *  method works and the other doesn't.  If one method works on significantly
                    244:  *  more systems than another, then it will become the default.  If the second
                    245:  *  option turns out to work best, then I'll find a way to work around that
                    246:  *  big con I listed.
                    247:  *
                    248:  *  -- July 7, 02:33
                    249:  *    OK...I just added some code that should make the Con listed for the
                    250:  *    fake commands a non issue now.  However, it needs testing.  For now,
                    251:  *    I'm going to make the default to use the fake commands, we'll see how
                    252:  *    it goes.
                    253:  */
1.1.1.2 ! root      254: 
1.1       root      255: #define AIC7XXX_FAKE_NEGOTIATION_CMDS
                    256: 
                    257: /*
                    258:  * AIC7XXX_STRICT_PCI_SETUP
                    259:  *   Should we assume the PCI config options on our controllers are set with
                    260:  *   sane and proper values, or should we be anal about our PCI config
                    261:  *   registers and force them to what we want?  The main advantage to
                    262:  *   defining this option is on non-Intel hardware where the BIOS may not
                    263:  *   have been run to set things up, or if you have one of the BIOSless
                    264:  *   Adaptec controllers, such as a 2910, that don't get set up by the
                    265:  *   BIOS.  However, keep in mind that we really do set the most important
                    266:  *   items in the driver regardless of this setting, this only controls some
                    267:  *   of the more esoteric PCI options on these cards.  In that sense, I
                    268:  *   would default to leaving this off.  However, if people wish to try
                    269:  *   things both ways, that would also help me to know if there are some
                    270:  *   machines where it works one way but not another.
                    271:  *
                    272:  *   -- July 7, 17:09
                    273:  *     OK...I need this on my machine for testing, so the default is to
                    274:  *     leave it defined.
                    275:  *
                    276:  *   -- July 7, 18:49
                    277:  *     I needed it for testing, but it didn't make any difference, so back
                    278:  *     off she goes.
                    279:  *
                    280:  *   -- July 16, 23:04
                    281:  *     I turned it back on to try and compensate for the 2.1.x PCI code
                    282:  *     which no longer relies solely on the BIOS and now tries to set
                    283:  *     things itself.
                    284:  */
                    285: 
                    286: #define AIC7XXX_STRICT_PCI_SETUP
                    287: 
                    288: /*
                    289:  * AIC7XXX_VERBOSE_DEBUGGING
                    290:  *   This option enables a lot of extra printk();s in the code, surrounded
                    291:  *   by if (aic7xxx_verbose ...) statements.  Executing all of those if
                    292:  *   statements and the extra checks can get to where it actually does have
                    293:  *   an impact on CPU usage and such, as well as code size.  Disabling this
                    294:  *   define will keep some of those from becoming part of the code.
                    295:  *
                    296:  *   NOTE:  Currently, this option has no real effect, I will be adding the
                    297:  *   various #ifdef's in the code later when I've decided a section is
                    298:  *   complete and no longer needs debugging.  OK...a lot of things are now
                    299:  *   surrounded by this define, so turning this off does have an impact.
                    300:  */
1.1.1.2 ! root      301: 
1.1       root      302: /*
                    303:  * #define AIC7XXX_VERBOSE_DEBUGGING
                    304:  */
1.1.1.2 ! root      305: 
1.1       root      306: #if defined(MODULE) || defined(PCMCIA)
                    307: #include <linux/module.h>
                    308: #endif
                    309: 
                    310: #if defined(PCMCIA)
                    311: #  undef MODULE
                    312: #endif
                    313: 
                    314: #include <stdarg.h>
                    315: #include <asm/io.h>
                    316: #include <asm/irq.h>
                    317: #include <asm/byteorder.h>
                    318: #include <linux/version.h>
                    319: #include <linux/string.h>
                    320: #include <linux/errno.h>
                    321: #include <linux/kernel.h>
                    322: #include <linux/ioport.h>
                    323: #include <linux/delay.h>
                    324: #include <linux/sched.h>
                    325: #include <linux/pci.h>
                    326: #include <linux/proc_fs.h>
                    327: #include <linux/blk.h>
                    328: #include <linux/tqueue.h>
                    329: #include <linux/tasks.h>
                    330: #include "sd.h"
                    331: #include "scsi.h"
                    332: #include "hosts.h"
                    333: #include "aic7xxx.h"
                    334: 
                    335: #include "aic7xxx/sequencer.h"
                    336: #include "aic7xxx/scsi_message.h"
                    337: #include "aic7xxx_reg.h"
1.1.1.2 ! root      338: #include <scsi/scsicam.h>
1.1       root      339: 
                    340: #include <linux/stat.h>
                    341: #include <linux/malloc.h>        /* for kmalloc() */
                    342: 
                    343: #include <linux/config.h>        /* for CONFIG_PCI */
                    344: 
                    345: /*
                    346:  * To generate the correct addresses for the controller to issue
                    347:  * on the bus.  Originally added for DEC Alpha support.
                    348:  */
                    349: #define VIRT_TO_BUS(a) (unsigned int)virt_to_bus((void *)(a))
                    350: 
                    351: struct proc_dir_entry proc_scsi_aic7xxx = {
                    352:     PROC_SCSI_AIC7XXX, 7, "aic7xxx",
                    353:     S_IFDIR | S_IRUGO | S_IXUGO, 2,
                    354:     0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL
                    355: };
                    356: 
1.1.1.2 ! root      357: #define AIC7XXX_C_VERSION  "5.1.13"
1.1       root      358: 
                    359: #define NUMBER(arr)     (sizeof(arr) / sizeof(arr[0]))
                    360: #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
                    361: #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
                    362: #define ALL_TARGETS -1
                    363: #define ALL_CHANNELS -1
                    364: #define ALL_LUNS -1
                    365: #define MAX_TARGETS  16
                    366: #define MAX_LUNS     8
                    367: #ifndef TRUE
                    368: #  define TRUE 1
                    369: #endif
                    370: #ifndef FALSE
                    371: #  define FALSE 0
                    372: #endif
                    373: 
                    374: #ifndef KERNEL_VERSION
                    375: #  define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
                    376: #endif
                    377: 
                    378: /*
                    379:  * We need the bios32.h file if we are kernel version 2.1.92 or less.  The
                    380:  * full set of pci_* changes wasn't in place until 2.1.93
                    381:  */
                    382: 
                    383: #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,1,92)
                    384: #  if defined(__sparc_v9__) || defined(__powerpc__)
                    385: #    error "PPC and Sparc platforms are only support under 2.1.92 and above"
                    386: #  endif
                    387: #  include <linux/bios32.h>
                    388: #endif
                    389: 
                    390: #if defined(__powerpc__)
                    391: #  define MMAPIO
                    392: #  ifdef mb
                    393: #    undef mb
                    394: #  endif
                    395: #  define mb() \
                    396:      __asm__ __volatile__("eieio" ::: "memory")
                    397: #elif defined(__i386__)
                    398: #  define MMAPIO
                    399: #  ifdef mb
                    400: #    undef mb
                    401: #  endif
                    402: #  define mb() \
                    403:      __asm__ __volatile__("lock ; addl $0,0(%%esp)": : :"memory")
                    404: #elif defined(__alpha__)
                    405: #  ifdef mb
                    406: #    undef mb
                    407: #  endif
                    408: #  define mb() \
                    409:      __asm__ __volatile__("mb": : :"memory")
                    410: #endif
                    411: 
                    412: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,0)
                    413: #  include <asm/spinlock.h>
                    414: #  include <linux/smp.h>
                    415: #  define cpuid smp_processor_id()
                    416: #  if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
                    417: #    define DRIVER_LOCK_INIT \
                    418:        spin_lock_init(&p->spin_lock);
                    419: #    define DRIVER_LOCK \
                    420:        if(!p->cpu_lock_count[cpuid]) { \
                    421:          spin_lock_irqsave(&p->spin_lock, cpu_flags); \
                    422:          p->cpu_lock_count[cpuid]++; \
                    423:        } else { \
                    424:          p->cpu_lock_count[cpuid]++; \
                    425:        }
                    426: #    define DRIVER_UNLOCK \
                    427:        if(--p->cpu_lock_count[cpuid] == 0) \
                    428:          spin_unlock_irqrestore(&p->spin_lock, cpu_flags);
                    429: #  else
                    430: #    define DRIVER_LOCK_INIT
                    431: #    define DRIVER_LOCK
                    432: #    define DRIVER_UNLOCK
                    433: #  endif
                    434: #else
                    435: #  define cpuid 0
                    436: #  define DRIVER_LOCK_INIT
                    437: #  define DRIVER_LOCK \
                    438:        save_flags(cpu_flags); \
                    439:        cli();
                    440: #  define DRIVER_UNLOCK \
                    441:        restore_flags(cpu_flags);
                    442: #  define le32_to_cpu(x) (x)
                    443: #  define cpu_to_le32(x) (x)
                    444: #endif
                    445: 
                    446: /*
                    447:  * You can try raising me if tagged queueing is enabled, or lowering
                    448:  * me if you only have 4 SCBs.
                    449:  */
1.1.1.2 ! root      450: #ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
        !           451: #define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
        !           452: #else
        !           453: #define AIC7XXX_CMDS_PER_DEVICE 8
1.1       root      454: #endif
                    455: 
                    456: /* Set this to the delay in seconds after SCSI bus reset. */
                    457: #ifdef CONFIG_AIC7XXX_RESET_DELAY
                    458: #define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY
                    459: #else
                    460: #define AIC7XXX_RESET_DELAY 5
                    461: #endif
                    462: 
                    463: /*
                    464:  * Control collection of SCSI transfer statistics for the /proc filesystem.
                    465:  *
                    466:  * NOTE: Do NOT enable this when running on kernels version 1.2.x and below.
                    467:  * NOTE: This does affect performance since it has to maintain statistics.
                    468:  */
                    469: #ifdef CONFIG_AIC7XXX_PROC_STATS
                    470: #define AIC7XXX_PROC_STATS
                    471: #endif
                    472: 
                    473: /*
                    474:  * NOTE: Uncommenting the define below no longer has any effect, the
                    475:  *       tagged queue value array is always active now.  I've added
                    476:  *       a setup option to set this particular array and I'm hoping
                    477:  *       insmod will be smart enough to set it properly as well.  It's
                    478:  *       by use of this array that a person can enable tagged queueing.
                    479:  *       The DEFAULT_TAG_COMMANDS define has been changed to disable
                    480:  *       tagged queueing by default, so if your devices can handle tagged
                    481:  *       queueing you will need to add a line to their lilo.conf file like:
                    482:  *       append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
                    483:  *       which will result in the first four devices on the first two
                    484:  *       controllers being set to a tagged queue depth of 32.
                    485:  *
                    486:  * Set this for defining the number of tagged commands on a device
                    487:  * by device, and controller by controller basis.  The first set
                    488:  * of tagged commands will be used for the first detected aic7xxx
                    489:  * controller, the second set will be used for the second detected
                    490:  * aic7xxx controller, and so on.  These values will *only* be used
                    491:  * for targets that are tagged queueing capable; these values will
                    492:  * be ignored in all other cases.  The tag_commands is an array of
                    493:  * 16 to allow for wide and twin adapters.  Twin adapters will use
                    494:  * indexes 0-7 for channel 0, and indexes 8-15 for channel 1.
                    495:  *
                    496:  * *** Determining commands per LUN ***
1.1.1.2 ! root      497:  *
        !           498:  * When AIC7XXX_CMDS_PER_DEVICE is not defined, the driver will use its
1.1       root      499:  * own algorithm to determine the commands/LUN.  If SCB paging is
                    500:  * enabled, which is always now, the default is 8 commands per lun
                    501:  * that indicates it supports tagged queueing.  All non-tagged devices
                    502:  * use an internal queue depth of 3, with no more than one of those
                    503:  * three commands active at one time.
                    504:  */
                    505: /* #define AIC7XXX_TAGGED_QUEUEING_BY_DEVICE */
                    506: 
                    507: typedef struct
                    508: {
                    509:   unsigned char tag_commands[16];   /* Allow for wide/twin adapters. */
                    510: } adapter_tag_info_t;
                    511: 
                    512: /*
                    513:  * Make a define that will tell the driver not to use tagged queueing
                    514:  * by default.
                    515:  */
1.1.1.2 ! root      516: #ifdef CONFIG_AIC7XXX_TCQ_ON_BY_DEFAULT
        !           517: #define DEFAULT_TAG_COMMANDS {0, 0, 0, 0, 0, 0, 0, 0,\
        !           518:                               0, 0, 0, 0, 0, 0, 0, 0}
        !           519: #else
1.1       root      520: #define DEFAULT_TAG_COMMANDS {255, 255, 255, 255, 255, 255, 255, 255,\
                    521:                               255, 255, 255, 255, 255, 255, 255, 255}
1.1.1.2 ! root      522: #endif
1.1       root      523: 
                    524: /*
                    525:  * Modify this as you see fit for your system.  By setting tag_commands
                    526:  * to 0, the driver will use it's own algorithm for determining the
                    527:  * number of commands to use (see above).  When 255, the driver will
                    528:  * not enable tagged queueing for that particular device.  When positive
                    529:  * (> 0) and (< 255) the values in the array are used for the queue_depth.
                    530:  * Note that the maximum value for an entry is 254, but you're insane if
                    531:  * you try to use that many commands on one device.
                    532:  *
                    533:  * In this example, the first line will disable tagged queueing for all
                    534:  * the devices on the first probed aic7xxx adapter.
                    535:  *
                    536:  * The second line enables tagged queueing with 4 commands/LUN for IDs
                    537:  * (1, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
                    538:  * driver to use its own algorithm for ID 1.
                    539:  *
                    540:  * The third line is the same as the first line.
                    541:  *
                    542:  * The fourth line disables tagged queueing for devices 0 and 3.  It
                    543:  * enables tagged queueing for the other IDs, with 16 commands/LUN
                    544:  * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
                    545:  * IDs 2, 5-7, and 9-15.
                    546:  */
                    547: 
                    548: /*
                    549:  * NOTE: The below structure is for reference only, the actual structure
                    550:  *       to modify in order to change things is located around line
                    551:  *       number 1305
                    552: adapter_tag_info_t aic7xxx_tag_info[] =
                    553: {
                    554:   {DEFAULT_TAG_COMMANDS},
                    555:   {{4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 255, 4, 4, 4}},
                    556:   {DEFAULT_TAG_COMMANDS},
                    557:   {{255, 16, 4, 255, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
                    558: };
                    559: */
                    560: 
1.1.1.2 ! root      561: static adapter_tag_info_t aic7xxx_tag_info[] =
        !           562: {
        !           563:   {DEFAULT_TAG_COMMANDS},
        !           564:   {DEFAULT_TAG_COMMANDS},
        !           565:   {DEFAULT_TAG_COMMANDS},
        !           566:   {DEFAULT_TAG_COMMANDS},
        !           567:   {DEFAULT_TAG_COMMANDS},
        !           568:   {DEFAULT_TAG_COMMANDS},
        !           569:   {DEFAULT_TAG_COMMANDS},
        !           570:   {DEFAULT_TAG_COMMANDS},
        !           571:   {DEFAULT_TAG_COMMANDS},
        !           572:   {DEFAULT_TAG_COMMANDS},
        !           573:   {DEFAULT_TAG_COMMANDS},
        !           574:   {DEFAULT_TAG_COMMANDS},
        !           575:   {DEFAULT_TAG_COMMANDS},
        !           576:   {DEFAULT_TAG_COMMANDS},
        !           577:   {DEFAULT_TAG_COMMANDS},
        !           578:   {DEFAULT_TAG_COMMANDS}
        !           579: };
        !           580: 
        !           581: 
1.1       root      582: /*
                    583:  * Define an array of board names that can be indexed by aha_type.
                    584:  * Don't forget to change this when changing the types!
                    585:  */
                    586: static const char *board_names[] = {
                    587:   "AIC-7xxx Unknown",                                   /* AIC_NONE */
                    588:   "Adaptec AIC-7810 Hardware RAID Controller",          /* AIC_7810 */
                    589:   "Adaptec AIC-7770 SCSI host adapter",                 /* AIC_7770 */
                    590:   "Adaptec AHA-274X SCSI host adapter",                 /* AIC_7771 */
                    591:   "Adaptec AHA-284X SCSI host adapter",                 /* AIC_284x */
                    592:   "Adaptec AIC-7850 SCSI host adapter",                 /* AIC_7850 */
                    593:   "Adaptec AIC-7855 SCSI host adapter",                 /* AIC_7855 */
                    594:   "Adaptec AIC-7860 Ultra SCSI host adapter",           /* AIC_7860 */
                    595:   "Adaptec AHA-2940A Ultra SCSI host adapter",          /* AIC_7861 */
                    596:   "Adaptec AIC-7870 SCSI host adapter",                 /* AIC_7870 */
                    597:   "Adaptec AHA-294X SCSI host adapter",                 /* AIC_7871 */
                    598:   "Adaptec AHA-394X SCSI host adapter",                 /* AIC_7872 */
                    599:   "Adaptec AHA-398X SCSI host adapter",                 /* AIC_7873 */
                    600:   "Adaptec AHA-2944 SCSI host adapter",                 /* AIC_7874 */
                    601:   "Adaptec AIC-7880 Ultra SCSI host adapter",           /* AIC_7880 */
                    602:   "Adaptec AHA-294X Ultra SCSI host adapter",           /* AIC_7881 */
                    603:   "Adaptec AHA-394X Ultra SCSI host adapter",           /* AIC_7882 */
                    604:   "Adaptec AHA-398X Ultra SCSI host adapter",           /* AIC_7883 */
                    605:   "Adaptec AHA-2944 Ultra SCSI host adapter",           /* AIC_7884 */
                    606:   "Adaptec AIC-7895 Ultra SCSI host adapter",           /* AIC_7895 */
                    607:   "Adaptec AIC-7890/1 Ultra2 SCSI host adapter",        /* AIC_7890 */
1.1.1.2 ! root      608:   "Adaptec AHA-293X Ultra2 SCSI host adapter",          /* AIC_7890 */
1.1       root      609:   "Adaptec AHA-294X Ultra2 SCSI host adapter",          /* AIC_7890 */
                    610:   "Adaptec AIC-7896/7 Ultra2 SCSI host adapter",        /* AIC_7896 */
1.1.1.2 ! root      611:   "Adaptec AHA-394X Ultra2 SCSI host adapter",          /* AIC_7897 */
        !           612:   "Adaptec AHA-395X Ultra2 SCSI host adapter",          /* AIC_7897 */
        !           613:   "Adaptec PCMCIA SCSI controller",                     /* card bus stuff */
        !           614:   "Adaptec AIC-7892 Ultra 160/m SCSI host adapter",     /* AIC_7892 */
        !           615:   "Adaptec AIC-7899 Ultra 160/m SCSI host adapter",     /* AIC_7899 */
1.1       root      616: };
                    617: 
                    618: /*
                    619:  * There should be a specific return value for this in scsi.h, but
                    620:  * it seems that most drivers ignore it.
                    621:  */
                    622: #define DID_UNDERFLOW   DID_ERROR
                    623: 
                    624: /*
                    625:  *  What we want to do is have the higher level scsi driver requeue
                    626:  *  the command to us. There is no specific driver status for this
                    627:  *  condition, but the higher level scsi driver will requeue the
                    628:  *  command on a DID_BUS_BUSY error.
                    629:  *
                    630:  *  Upon further inspection and testing, it seems that DID_BUS_BUSY
                    631:  *  will *always* retry the command.  We can get into an infinite loop
                    632:  *  if this happens when we really want some sort of counter that
                    633:  *  will automatically abort/reset the command after so many retries.
                    634:  *  Using DID_ERROR will do just that.  (Made by a suggestion by
                    635:  *  Doug Ledford 8/1/96)
                    636:  */
                    637: #define DID_RETRY_COMMAND DID_ERROR
                    638: 
                    639: #define HSCSIID        0x07
                    640: #define SCSI_RESET     0x040
                    641: 
                    642: /*
                    643:  * EISA/VL-bus stuff
                    644:  */
                    645: #define MINSLOT                1
                    646: #define MAXSLOT                15
                    647: #define SLOTBASE(x)        ((x) << 12)
                    648: #define BASE_TO_SLOT(x) ((x) >> 12)
                    649: 
                    650: /*
                    651:  * Standard EISA Host ID regs  (Offset from slot base)
                    652:  */
                    653: #define AHC_HID0              0x80   /* 0,1: msb of ID2, 2-7: ID1      */
                    654: #define AHC_HID1              0x81   /* 0-4: ID3, 5-7: LSB ID2         */
                    655: #define AHC_HID2              0x82   /* product                        */
                    656: #define AHC_HID3              0x83   /* firmware revision              */
                    657: 
                    658: /*
                    659:  * AIC-7770 I/O range to reserve for a card
                    660:  */
                    661: #define MINREG                0xC00
                    662: #define MAXREG                0xCBF
                    663: 
                    664: #define INTDEF                0x5C      /* Interrupt Definition Register */
                    665: 
                    666: /*
                    667:  * AIC-78X0 PCI registers
                    668:  */
                    669: #define        CLASS_PROGIF_REVID        0x08
                    670: #define                DEVREVID        0x000000FFul
                    671: #define                PROGINFC        0x0000FF00ul
                    672: #define                SUBCLASS        0x00FF0000ul
                    673: #define                BASECLASS        0xFF000000ul
                    674: 
                    675: #define        CSIZE_LATTIME                0x0C
                    676: #define                CACHESIZE        0x0000003Ful        /* only 5 bits */
                    677: #define                LATTIME                0x0000FF00ul
                    678: 
                    679: #define        DEVCONFIG                0x40
                    680: #define                SCBSIZE32        0x00010000ul        /* aic789X only */
                    681: #define                MPORTMODE        0x00000400ul        /* aic7870 only */
                    682: #define                RAMPSM           0x00000200ul        /* aic7870 only */
                    683: #define                RAMPSM_ULTRA2    0x00000004
                    684: #define                VOLSENSE         0x00000100ul
                    685: #define                SCBRAMSEL        0x00000080ul
                    686: #define                SCBRAMSEL_ULTRA2 0x00000008
                    687: #define                MRDCEN           0x00000040ul
                    688: #define                EXTSCBTIME       0x00000020ul        /* aic7870 only */
                    689: #define                EXTSCBPEN        0x00000010ul        /* aic7870 only */
                    690: #define                BERREN           0x00000008ul
                    691: #define                DACEN            0x00000004ul
                    692: #define                STPWLEVEL        0x00000002ul
                    693: #define                DIFACTNEGEN      0x00000001ul        /* aic7870 only */
                    694: 
                    695: #define        SCAMCTL                  0x1a                /* Ultra2 only  */
                    696: #define        CCSCBBADDR               0xf0                /* aic7895/6/7  */
                    697: 
                    698: /*
                    699:  * Define the different types of SEEPROMs on aic7xxx adapters
                    700:  * and make it also represent the address size used in accessing
                    701:  * its registers.  The 93C46 chips have 1024 bits organized into
                    702:  * 64 16-bit words, while the 93C56 chips have 2048 bits organized
                    703:  * into 128 16-bit words.  The C46 chips use 6 bits to address
                    704:  * each word, while the C56 and C66 (4096 bits) use 8 bits to
                    705:  * address each word.
                    706:  */
                    707: typedef enum {C46 = 6, C56_66 = 8} seeprom_chip_type;
                    708: 
                    709: /*
                    710:  *
                    711:  * Define the format of the SEEPROM registers (16 bits).
                    712:  *
                    713:  */
                    714: struct seeprom_config {
                    715: 
                    716: /*
                    717:  * SCSI ID Configuration Flags
                    718:  */
                    719: #define CFXFER                0x0007      /* synchronous transfer rate */
                    720: #define CFSYNCH               0x0008      /* enable synchronous transfer */
                    721: #define CFDISC                0x0010      /* enable disconnection */
                    722: #define CFWIDEB               0x0020      /* wide bus device (wide card) */
                    723: #define CFSYNCHISULTRA        0x0040      /* CFSYNC is an ultra offset */
                    724: #define CFNEWULTRAFORMAT      0x0080      /* Use the Ultra2 SEEPROM format */
                    725: #define CFSTART               0x0100      /* send start unit SCSI command */
                    726: #define CFINCBIOS             0x0200      /* include in BIOS scan */
                    727: #define CFRNFOUND             0x0400      /* report even if not found */
                    728: #define CFMULTILUN            0x0800      /* probe mult luns in BIOS scan */
                    729: #define CFWBCACHEYES          0x4000      /* Enable W-Behind Cache on drive */
                    730: #define CFWBCACHENC           0xc000      /* Don't change W-Behind Cache */
                    731: /* UNUSED                0x3000 */
                    732:   unsigned short device_flags[16];        /* words 0-15 */
                    733: 
                    734: /*
                    735:  * BIOS Control Bits
                    736:  */
                    737: #define CFSUPREM        0x0001  /* support all removable drives */
                    738: #define CFSUPREMB       0x0002  /* support removable drives for boot only */
                    739: #define CFBIOSEN        0x0004  /* BIOS enabled */
                    740: /* UNUSED                0x0008 */
                    741: #define CFSM2DRV        0x0010  /* support more than two drives */
                    742: #define CF284XEXTEND    0x0020  /* extended translation (284x cards) */
                    743: /* UNUSED                0x0040 */
                    744: #define CFEXTEND        0x0080  /* extended translation enabled */
                    745: /* UNUSED                0xFF00 */
                    746:   unsigned short bios_control;  /* word 16 */
                    747: 
                    748: /*
                    749:  * Host Adapter Control Bits
                    750:  */
                    751: #define CFAUTOTERM      0x0001  /* Perform Auto termination */
                    752: #define CFULTRAEN       0x0002  /* Ultra SCSI speed enable (Ultra cards) */
                    753: #define CF284XSELTO     0x0003  /* Selection timeout (284x cards) */
                    754: #define CF284XFIFO      0x000C  /* FIFO Threshold (284x cards) */
                    755: #define CFSTERM         0x0004  /* SCSI low byte termination */
                    756: #define CFWSTERM        0x0008  /* SCSI high byte termination (wide card) */
                    757: #define CFSPARITY       0x0010  /* SCSI parity */
                    758: #define CF284XSTERM     0x0020  /* SCSI low byte termination (284x cards) */
                    759: #define CFRESETB        0x0040  /* reset SCSI bus at boot */
                    760: #define CFBPRIMARY      0x0100  /* Channel B primary on 7895 chipsets */
                    761: #define CFSEAUTOTERM    0x0400  /* aic7890 Perform SE Auto Term */
                    762: #define CFLVDSTERM      0x0800  /* aic7890 LVD Termination */
                    763: /* UNUSED                0xF280 */
                    764:   unsigned short adapter_control;        /* word 17 */
                    765: 
                    766: /*
                    767:  * Bus Release, Host Adapter ID
                    768:  */
                    769: #define CFSCSIID        0x000F                /* host adapter SCSI ID */
                    770: /* UNUSED                0x00F0 */
                    771: #define CFBRTIME        0xFF00                /* bus release time */
                    772:   unsigned short brtime_id;                /* word 18 */
                    773: 
                    774: /*
                    775:  * Maximum targets
                    776:  */
                    777: #define CFMAXTARG        0x00FF        /* maximum targets */
                    778: /* UNUSED                0xFF00 */
                    779:   unsigned short max_targets;                /* word 19 */
                    780: 
                    781:   unsigned short res_1[11];                /* words 20-30 */
                    782:   unsigned short checksum;                /* word 31 */
                    783: };
                    784: 
                    785: #define SELBUS_MASK                0x0a
                    786: #define         SELNARROW        0x00
                    787: #define         SELBUSB                0x08
                    788: #define SINGLE_BUS                0x00
                    789: 
                    790: #define SCB_TARGET(scb)         \
                    791:        (((scb)->hscb->target_channel_lun & TID) >> 4)
                    792: #define SCB_LUN(scb)            \
                    793:        ((scb)->hscb->target_channel_lun & LID)
                    794: #define SCB_IS_SCSIBUS_B(scb)   \
                    795:        (((scb)->hscb->target_channel_lun & SELBUSB) != 0)
                    796: 
                    797: /*
                    798:  * If an error occurs during a data transfer phase, run the command
                    799:  * to completion - it's easier that way - making a note of the error
                    800:  * condition in this location. This then will modify a DID_OK status
                    801:  * into an appropriate error for the higher-level SCSI code.
                    802:  */
                    803: #define aic7xxx_error(cmd)        ((cmd)->SCp.Status)
                    804: 
                    805: /*
                    806:  * Keep track of the targets returned status.
                    807:  */
                    808: #define aic7xxx_status(cmd)        ((cmd)->SCp.sent_command)
                    809: 
                    810: /*
                    811:  * The position of the SCSI commands scb within the scb array.
                    812:  */
                    813: #define aic7xxx_position(cmd)        ((cmd)->SCp.have_data_in)
                    814: 
                    815: /*
                    816:  * So we can keep track of our host structs
                    817:  */
                    818: static struct aic7xxx_host *first_aic7xxx = NULL;
                    819: 
                    820: /*
                    821:  * As of Linux 2.1, the mid-level SCSI code uses virtual addresses
                    822:  * in the scatter-gather lists.  We need to convert the virtual
                    823:  * addresses to physical addresses.
                    824:  */
                    825: struct hw_scatterlist {
                    826:   unsigned int address;
                    827:   unsigned int length;
                    828: };
                    829: 
                    830: /*
                    831:  * Maximum number of SG segments these cards can support.
                    832:  */
                    833: #define        AIC7XXX_MAX_SG 128
                    834: 
                    835: /*
                    836:  * The maximum number of SCBs we could have for ANY type
                    837:  * of card. DON'T FORGET TO CHANGE THE SCB MASK IN THE
                    838:  * SEQUENCER CODE IF THIS IS MODIFIED!
                    839:  */
                    840: #define AIC7XXX_MAXSCB        255
                    841: 
                    842: 
                    843: struct aic7xxx_hwscb {
                    844: /* ------------    Begin hardware supported fields    ---------------- */
                    845: /* 0*/  unsigned char control;
                    846: /* 1*/  unsigned char target_channel_lun;       /* 4/1/3 bits */
                    847: /* 2*/  unsigned char target_status;
                    848: /* 3*/  unsigned char SG_segment_count;
                    849: /* 4*/  unsigned int  SG_list_pointer;
                    850: /* 8*/  unsigned char residual_SG_segment_count;
                    851: /* 9*/  unsigned char residual_data_count[3];
                    852: /*12*/  unsigned int  data_pointer;
                    853: /*16*/  unsigned int  data_count;
                    854: /*20*/  unsigned int  SCSI_cmd_pointer;
                    855: /*24*/  unsigned char SCSI_cmd_length;
                    856: /*25*/  unsigned char tag;          /* Index into our kernel SCB array.
                    857:                                      * Also used as the tag for tagged I/O
                    858:                                      */
                    859: #define SCB_PIO_TRANSFER_SIZE  26   /* amount we need to upload/download
                    860:                                      * via PIO to initialize a transaction.
                    861:                                      */
                    862: /*26*/  unsigned char next;         /* Used to thread SCBs awaiting selection
                    863:                                      * or disconnected down in the sequencer.
                    864:                                      */
                    865: /*27*/  unsigned char prev;
                    866: /*28*/  unsigned int pad;           /*
                    867:                                      * Unused by the kernel, but we require
                    868:                                      * the padding so that the array of
                    869:                                      * hardware SCBs is alligned on 32 byte
                    870:                                      * boundaries so the sequencer can index
                    871:                                      */
                    872: };
                    873: 
                    874: typedef enum {
                    875:         SCB_FREE                = 0x0000,
                    876:         SCB_WAITINGQ            = 0x0002,
                    877:         SCB_ACTIVE              = 0x0004,
                    878:         SCB_SENSE               = 0x0008,
                    879:         SCB_ABORT               = 0x0010,
                    880:         SCB_DEVICE_RESET        = 0x0020,
                    881:         SCB_RESET               = 0x0040,
                    882:         SCB_RECOVERY_SCB        = 0x0080,
                    883:         SCB_WAS_BUSY            = 0x0100,
                    884:         SCB_MSGOUT_SENT         = 0x0200,
                    885:         SCB_MSGOUT_SDTR         = 0x0400,
                    886:         SCB_MSGOUT_WDTR         = 0x0800,
1.1.1.2 ! root      887:         SCB_MSGOUT_BITS         = SCB_MSGOUT_SENT |
1.1       root      888:                                   SCB_MSGOUT_SDTR |
                    889:                                   SCB_MSGOUT_WDTR,
                    890:         SCB_QUEUED_ABORT        = 0x1000,
                    891:         SCB_QUEUED_FOR_DONE     = 0x2000
                    892: } scb_flag_type;
                    893: 
                    894: typedef enum {
                    895:         AHC_FNONE                 = 0x00000000,
                    896:         AHC_PAGESCBS              = 0x00000001,
                    897:         AHC_CHANNEL_B_PRIMARY     = 0x00000002,
                    898:         AHC_USEDEFAULTS           = 0x00000004,
                    899:         AHC_INDIRECT_PAGING       = 0x00000008,
                    900:         AHC_CHNLB                 = 0x00000020,
                    901:         AHC_CHNLC                 = 0x00000040,
                    902:         AHC_EXTEND_TRANS_A        = 0x00000100,
                    903:         AHC_EXTEND_TRANS_B        = 0x00000200,
                    904:         AHC_TERM_ENB_A            = 0x00000400,
                    905:         AHC_TERM_ENB_SE_LOW       = 0x00000400,
                    906:         AHC_TERM_ENB_B            = 0x00000800,
                    907:         AHC_TERM_ENB_SE_HIGH      = 0x00000800,
                    908:         AHC_HANDLING_REQINITS     = 0x00001000,
                    909:         AHC_TARGETMODE            = 0x00002000,
                    910:         AHC_NEWEEPROM_FMT         = 0x00004000,
                    911:  /*
                    912:   *  Here ends the FreeBSD defined flags and here begins the linux defined
                    913:   *  flags.  NOTE: I did not preserve the old flag name during this change
                    914:   *  specifically to force me to evaluate what flags were being used properly
                    915:   *  and what flags weren't.  This way, I could clean up the flag usage on
                    916:   *  a use by use basis.  Doug Ledford
                    917:   */
1.1.1.2 ! root      918:         AHC_RESET_DELAY           = 0x00080000,
1.1       root      919:         AHC_A_SCANNED             = 0x00100000,
                    920:         AHC_B_SCANNED             = 0x00200000,
                    921:         AHC_MULTI_CHANNEL         = 0x00400000,
                    922:         AHC_BIOS_ENABLED          = 0x00800000,
                    923:         AHC_SEEPROM_FOUND         = 0x01000000,
                    924:         AHC_TERM_ENB_LVD          = 0x02000000,
                    925:         AHC_ABORT_PENDING         = 0x04000000,
                    926:         AHC_RESET_PENDING         = 0x08000000,
                    927: #define AHC_IN_ISR_BIT              28
                    928:         AHC_IN_ISR                = 0x10000000,
                    929:         AHC_IN_ABORT              = 0x20000000,
                    930:         AHC_IN_RESET              = 0x40000000,
                    931:         AHC_EXTERNAL_SRAM         = 0x80000000
                    932: } ahc_flag_type;
                    933: 
                    934: typedef enum {
                    935:   AHC_NONE             = 0x0000,
                    936:   AHC_CHIPID_MASK      = 0x00ff,
                    937:   AHC_AIC7770          = 0x0001,
                    938:   AHC_AIC7850          = 0x0002,
                    939:   AHC_AIC7860          = 0x0003,
                    940:   AHC_AIC7870          = 0x0004,
                    941:   AHC_AIC7880          = 0x0005,
                    942:   AHC_AIC7890          = 0x0006,
                    943:   AHC_AIC7895          = 0x0007,
                    944:   AHC_AIC7896          = 0x0008,
1.1.1.2 ! root      945:   AHC_AIC7892          = 0x0009,
        !           946:   AHC_AIC7899          = 0x000a,
1.1       root      947:   AHC_VL               = 0x0100,
                    948:   AHC_EISA             = 0x0200,
                    949:   AHC_PCI              = 0x0400,
                    950: } ahc_chip;
                    951: 
                    952: typedef enum {
                    953:   AHC_FENONE           = 0x0000,
                    954:   AHC_ULTRA            = 0x0001,
                    955:   AHC_ULTRA2           = 0x0002,
                    956:   AHC_WIDE             = 0x0004,
                    957:   AHC_TWIN             = 0x0008,
                    958:   AHC_MORE_SRAM        = 0x0010,
                    959:   AHC_CMD_CHAN         = 0x0020,
                    960:   AHC_QUEUE_REGS       = 0x0040,
                    961:   AHC_SG_PRELOAD       = 0x0080,
                    962:   AHC_SPIOCAP          = 0x0100,
1.1.1.2 ! root      963:   AHC_ULTRA160         = 0x0200,
1.1       root      964:   AHC_AIC7770_FE       = AHC_FENONE,
                    965:   AHC_AIC7850_FE       = AHC_SPIOCAP,
                    966:   AHC_AIC7860_FE       = AHC_ULTRA|AHC_SPIOCAP,
                    967:   AHC_AIC7870_FE       = AHC_FENONE,
                    968:   AHC_AIC7880_FE       = AHC_ULTRA,
                    969:   AHC_AIC7890_FE       = AHC_MORE_SRAM|AHC_CMD_CHAN|AHC_ULTRA2|
                    970:                          AHC_QUEUE_REGS|AHC_SG_PRELOAD,
                    971:   AHC_AIC7895_FE       = AHC_MORE_SRAM|AHC_CMD_CHAN|AHC_ULTRA,
                    972:   AHC_AIC7896_FE       = AHC_AIC7890_FE,
1.1.1.2 ! root      973:   AHC_AIC7892_FE       = AHC_AIC7890_FE|AHC_ULTRA160,
        !           974:   AHC_AIC7899_FE       = AHC_AIC7890_FE|AHC_ULTRA160,
1.1       root      975: } ahc_feature;
                    976: 
                    977: struct aic7xxx_scb {
                    978:         struct aic7xxx_hwscb  *hscb;          /* corresponding hardware scb */
                    979:         Scsi_Cmnd             *cmd;              /* Scsi_Cmnd for this scb */
                    980:         struct aic7xxx_scb    *q_next;        /* next scb in queue */
                    981:         volatile scb_flag_type flags;         /* current state of scb */
                    982:         struct hw_scatterlist *sg_list;       /* SG list in adapter format */
                    983:         unsigned char          tag_action;
                    984:         unsigned char          sg_count;
                    985:         unsigned char          sense_cmd[6];  /*
                    986:                                                * Allocate 6 characters for
                    987:                                                * sense command.
                    988:                                                */
                    989:         unsigned int           sg_length; /* We init this during buildscb so we
                    990:                                            * don't have to calculate anything
                    991:                                            * during underflow/overflow/stat code
                    992:                                            */
1.1.1.2 ! root      993:         void                  *kmalloc_ptr;
1.1       root      994: };
                    995: 
                    996: /*
                    997:  * Define a linked list of SCBs.
                    998:  */
                    999: typedef struct {
                   1000:   struct aic7xxx_scb *head;
                   1001:   struct aic7xxx_scb *tail;
                   1002: } scb_queue_type;
                   1003: 
                   1004: static struct {
                   1005:   unsigned char errno;
                   1006:   const char *errmesg;
                   1007: } hard_error[] = {
                   1008:   { ILLHADDR,  "Illegal Host Access" },
                   1009:   { ILLSADDR,  "Illegal Sequencer Address referenced" },
                   1010:   { ILLOPCODE, "Illegal Opcode in sequencer program" },
                   1011:   { SQPARERR,  "Sequencer Ram Parity Error" },
                   1012:   { DPARERR,   "Data-Path Ram Parity Error" },
                   1013:   { MPARERR,   "Scratch Ram/SCB Array Ram Parity Error" },
                   1014:   { PCIERRSTAT,"PCI Error detected" },
                   1015:   { CIOPARERR, "CIOBUS Parity Error" }
                   1016: };
                   1017: 
                   1018: static unsigned char
                   1019: generic_sense[] = { REQUEST_SENSE, 0, 0, 0, 255, 0 };
                   1020: 
                   1021: typedef struct {
                   1022:   scb_queue_type free_scbs;        /*
                   1023:                                     * SCBs assigned to free slot on
                   1024:                                     * card (no paging required)
                   1025:                                     */
                   1026:   struct aic7xxx_scb   *scb_array[AIC7XXX_MAXSCB];
                   1027:   struct aic7xxx_hwscb *hscbs;
                   1028:   unsigned char  numscbs;          /* current number of scbs */
                   1029:   unsigned char  maxhscbs;         /* hardware scbs */
                   1030:   unsigned char  maxscbs;          /* max scbs including pageable scbs */
                   1031:   void          *hscb_kmalloc_ptr;
                   1032: } scb_data_type;
                   1033: 
                   1034: struct target_cmd {
                   1035:   unsigned char mesg_bytes[4];
                   1036:   unsigned char command[28];
                   1037: };
                   1038: 
                   1039: #define AHC_TRANS_CUR    0x0001
                   1040: #define AHC_TRANS_ACTIVE 0x0002
                   1041: #define AHC_TRANS_GOAL   0x0004
                   1042: #define AHC_TRANS_USER   0x0008
                   1043: #define AHC_TRANS_QUITE  0x0010
                   1044: typedef struct {
                   1045:   unsigned char cur_width;
                   1046:   unsigned char goal_width;
                   1047:   unsigned char cur_period;
                   1048:   unsigned char goal_period;
                   1049:   unsigned char cur_offset;
                   1050:   unsigned char goal_offset;
                   1051:   unsigned char user_width;
                   1052:   unsigned char user_period;
                   1053:   unsigned char user_offset;
                   1054: } transinfo_type;
                   1055: 
                   1056: /*
                   1057:  * Define a structure used for each host adapter.  Note, in order to avoid
                   1058:  * problems with architectures I can't test on (because I don't have one,
                   1059:  * such as the Alpha based systems) which happen to give faults for
                   1060:  * non-aligned memory accesses, care was taken to align this structure
                   1061:  * in a way that gauranteed all accesses larger than 8 bits were aligned
                   1062:  * on the appropriate boundary.  It's also organized to try and be more
                   1063:  * cache line efficient.  Be careful when changing this lest you might hurt
                   1064:  * overall performance and bring down the wrath of the masses.
                   1065:  */
                   1066: struct aic7xxx_host {
                   1067:   /*
                   1068:    *  This is the first 64 bytes in the host struct
                   1069:    */
                   1070: 
1.1.1.2 ! root     1071:   /*
        !          1072:    * We are grouping things here....first, items that get either read or
        !          1073:    * written with nearly every interrupt
        !          1074:    */
1.1       root     1075:   volatile ahc_flag_type   flags;
                   1076:   ahc_feature              features;         /* chip features */
1.1.1.2 ! root     1077:   unsigned long            base;             /* card base address */
        !          1078:   volatile unsigned char  *maddr;            /* memory mapped address */
1.1       root     1079:   unsigned long            isr_count;        /* Interrupt count */
                   1080:   unsigned long            spurious_int;
1.1.1.2 ! root     1081:   scb_data_type           *scb_data;
        !          1082:   volatile unsigned short  needsdtr;
        !          1083:   volatile unsigned short  sdtr_pending;
        !          1084:   volatile unsigned short  needwdtr;
        !          1085:   volatile unsigned short  wdtr_pending;
        !          1086:   struct aic7xxx_cmd_queue {
        !          1087:     Scsi_Cmnd *head;
        !          1088:     Scsi_Cmnd *tail;
        !          1089:   } completeq;
        !          1090: 
        !          1091:   /*
        !          1092:    * Things read/written on nearly every entry into aic7xxx_queue()
        !          1093:    */
        !          1094:   volatile scb_queue_type  waiting_scbs;
1.1       root     1095:   unsigned short           discenable;       /* Targets allowed to disconnect */
                   1096:   unsigned short           tagenable;        /* Targets using tagged I/O */
                   1097:   unsigned short           orderedtag;       /* Ordered Q tags allowed */
                   1098:   unsigned char            unpause;          /* unpause value for HCNTRL */
                   1099:   unsigned char            pause;            /* pause value for HCNTRL */
                   1100:   volatile unsigned char   qoutfifonext;
1.1.1.2 ! root     1101:   volatile unsigned char   activescbs;       /* active scbs */
        !          1102:   volatile unsigned char   max_activescbs;
1.1       root     1103:   volatile unsigned char   qinfifonext;
                   1104: 
                   1105: #define  DEVICE_PRESENT                 0x01
                   1106: #define  BUS_DEVICE_RESET_PENDING       0x02
1.1.1.2 ! root     1107: #define  DEVICE_RESET_DELAY             0x04
1.1       root     1108: #define  DEVICE_PRINT_SDTR              0x08
                   1109: #define  DEVICE_PRINT_WDTR              0x10
1.1.1.2 ! root     1110: #define  DEVICE_WAS_BUSY                0x20
1.1       root     1111: #define  DEVICE_SCANNED                 0x80
                   1112:   volatile unsigned char   dev_flags[MAX_TARGETS];
                   1113:   volatile unsigned char   dev_active_cmds[MAX_TARGETS];
                   1114:   volatile unsigned char   dev_temp_queue_depth[MAX_TARGETS];
                   1115:   unsigned char            dev_commands_sent[MAX_TARGETS];
                   1116: 
1.1.1.2 ! root     1117:   unsigned int             dev_timer_active; /* Which devs have a timer set */
        !          1118:   struct timer_list        dev_timer;
        !          1119:   unsigned long            dev_expires[MAX_TARGETS];
1.1       root     1120: 
1.1.1.2 ! root     1121: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,0)
        !          1122:   spinlock_t               spin_lock;
        !          1123:   volatile unsigned char   cpu_lock_count[NR_CPUS];
        !          1124: #endif
1.1       root     1125: 
                   1126: 
1.1.1.2 ! root     1127: #ifdef AIC7XXX_FAKE_NEGOTIATION_CMDS
        !          1128:   Scsi_Cmnd               *dev_wdtr_cmnd[MAX_TARGETS];
        !          1129:   Scsi_Cmnd               *dev_sdtr_cmnd[MAX_TARGETS];
        !          1130: #endif
1.1       root     1131: 
                   1132:   unsigned char            dev_last_queue_full[MAX_TARGETS];
                   1133:   unsigned char            dev_last_queue_full_count[MAX_TARGETS];
                   1134:   unsigned char            dev_max_queue_depth[MAX_TARGETS];
                   1135: 
                   1136:   volatile scb_queue_type  delayed_scbs[MAX_TARGETS];
                   1137: 
                   1138: 
                   1139:   unsigned char            msg_buf[9];       /* The message for the target */
                   1140:   unsigned char            msg_type;
                   1141: #define MSG_TYPE_NONE              0x00
                   1142: #define MSG_TYPE_INITIATOR_MSGOUT  0x01
                   1143: #define MSG_TYPE_INITIATOR_MSGIN   0x02
                   1144:   unsigned char            msg_len;          /* Length of message */
                   1145:   unsigned char            msg_index;        /* Index into msg_buf array */
                   1146:   transinfo_type           transinfo[MAX_TARGETS];
                   1147: 
                   1148: 
                   1149:   /*
                   1150:    * We put the less frequently used host structure items after the more
                   1151:    * frequently used items to try and ease the burden on the cache subsystem.
                   1152:    * These entries are not *commonly* accessed, whereas the preceding entries
                   1153:    * are accessed very often.  The only exceptions are the qinfifo, qoutfifo,
                   1154:    * and untagged_scbs array.  But, they are often accessed only once and each
                   1155:    * access into these arrays is likely to blow a cache line, so they are put
                   1156:    * down here so we can minimize the number of cache lines required to hold
                   1157:    * the preceeding entries.
                   1158:    */
                   1159: 
                   1160:   volatile unsigned char   untagged_scbs[256];
                   1161:   volatile unsigned char   qoutfifo[256];
                   1162:   volatile unsigned char   qinfifo[256];
                   1163:   unsigned int             irq;              /* IRQ for this adapter */
                   1164:   int                      instance;         /* aic7xxx instance number */
                   1165:   int                      scsi_id;          /* host adapter SCSI ID */
                   1166:   int                      scsi_id_b;        /* channel B for twin adapters */
                   1167:   unsigned int             bios_address;
                   1168:   int                      board_name_index;
                   1169:   unsigned short           needsdtr_copy;    /* default config */
                   1170:   unsigned short           needwdtr_copy;    /* default config */
                   1171:   unsigned short           ultraenb;         /* Ultra mode target list */
                   1172:   unsigned short           bios_control;     /* bios control - SEEPROM */
                   1173:   unsigned short           adapter_control;  /* adapter control - SEEPROM */
                   1174: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   1175:   struct pci_dev          *pdev;
                   1176: #endif
                   1177:   unsigned char            pci_bus;
                   1178:   unsigned char            pci_device_fn;
                   1179:   struct seeprom_config    sc;
                   1180:   unsigned short           sc_type;
                   1181:   unsigned short           sc_size;
1.1.1.2 ! root     1182:   struct aic7xxx_host     *next;             /* allow for multiple IRQs */
        !          1183:   struct Scsi_Host        *host;             /* pointer to scsi host */
        !          1184:   int                      host_no;          /* SCSI host number */
        !          1185:   unsigned long            mbase;            /* I/O memory address */
        !          1186:   ahc_chip                 chip;             /* chip type */
1.1       root     1187: 
                   1188:   /*
                   1189:    * Statistics Kept:
                   1190:    *
                   1191:    * Total Xfers (count for each command that has a data xfer),
                   1192:    * broken down further by reads && writes.
                   1193:    *
                   1194:    * Binned sizes, writes && reads:
                   1195:    *    < 512, 512, 1-2K, 2-4K, 4-8K, 8-16K, 16-32K, 32-64K, 64K-128K, > 128K
                   1196:    *
                   1197:    * Total amounts read/written above 512 bytes (amts under ignored)
                   1198:    *
                   1199:    * NOTE: Enabling this feature is likely to cause a noticeable performance
                   1200:    * decrease as the accesses into the stats structures blows apart multiple
1.1.1.2 ! root     1201:    * cache lines and is CPU time consuming.
        !          1202:    *
        !          1203:    * NOTE: Since it doesn't really buy us much, but consumes *tons* of RAM
        !          1204:    * and blows apart all sorts of cache lines, I modified this so that we
        !          1205:    * no longer look at the LUN.  All LUNs now go into the same bin on each
        !          1206:    * device for stats purposes.
1.1       root     1207:    */
                   1208:   struct aic7xxx_xferstats {
1.1.1.2 ! root     1209:     long w_total;                          /* total writes */
        !          1210:     long r_total;                          /* total reads */
1.1       root     1211: #ifdef AIC7XXX_PROC_STATS
1.1.1.2 ! root     1212:     long w_bins[8];                       /* binned write */
        !          1213:     long r_bins[8];                       /* binned reads */
1.1       root     1214: #endif /* AIC7XXX_PROC_STATS */
1.1.1.2 ! root     1215:   } stats[MAX_TARGETS];                    /* [(channel << 3)|target] */
        !          1216: 
        !          1217: #if 0
        !          1218:   struct target_cmd       *targetcmds;
        !          1219:   unsigned int             num_targetcmds;
        !          1220: #endif
        !          1221: 
1.1       root     1222: };
                   1223: 
                   1224: /*
                   1225:  * Valid SCSIRATE values. (p. 3-17)
                   1226:  * Provides a mapping of transfer periods in ns/4 to the proper value to
                   1227:  * stick in the SCSIRATE reg to use that transfer rate.
                   1228:  */
                   1229: #define AHC_SYNCRATE_ULTRA2 0
                   1230: #define AHC_SYNCRATE_ULTRA  2
                   1231: #define AHC_SYNCRATE_FAST   5
                   1232: static struct aic7xxx_syncrate {
                   1233:   /* Rates in Ultra mode have bit 8 of sxfr set */
                   1234: #define                ULTRA_SXFR 0x100
                   1235:   int sxfr_ultra2;
                   1236:   int sxfr;
                   1237:   unsigned char period;
                   1238:   const char *rate[2];
                   1239: } aic7xxx_syncrates[] = {
                   1240:   { 0x13,  0x000,  10,  {"40.0", "80.0"} },
                   1241:   { 0x14,  0x000,  11,  {"33.0", "66.6"} },
                   1242:   { 0x15,  0x100,  12,  {"20.0", "40.0"} },
                   1243:   { 0x16,  0x110,  15,  {"16.0", "32.0"} },
                   1244:   { 0x17,  0x120,  18,  {"13.4", "26.8"} },
                   1245:   { 0x18,  0x000,  25,  {"10.0", "20.0"} },
                   1246:   { 0x19,  0x010,  31,  {"8.0",  "16.0"} },
                   1247:   { 0x1a,  0x020,  37,  {"6.67", "13.3"} },
                   1248:   { 0x1b,  0x030,  43,  {"5.7",  "11.4"} },
                   1249:   { 0x10,  0x040,  50,  {"5.0",  "10.0"} },
                   1250:   { 0x00,  0x050,  56,  {"4.4",  "8.8" } },
                   1251:   { 0x00,  0x060,  62,  {"4.0",  "8.0" } },
                   1252:   { 0x00,  0x070,  68,  {"3.6",  "7.2" } },
                   1253:   { 0x00,  0x000,  0,   {NULL, NULL}   },
                   1254: };
                   1255: 
                   1256: #define CTL_OF_SCB(scb) (((scb->hscb)->target_channel_lun >> 3) & 0x1),  \
                   1257:                         (((scb->hscb)->target_channel_lun >> 4) & 0xf), \
                   1258:                         ((scb->hscb)->target_channel_lun & 0x07)
                   1259: 
                   1260: #define CTL_OF_CMD(cmd) ((cmd->channel) & 0x01),  \
                   1261:                         ((cmd->target) & 0x0f), \
                   1262:                         ((cmd->lun) & 0x07)
                   1263: 
                   1264: #define TARGET_INDEX(cmd)  ((cmd)->target | ((cmd)->channel << 3))
                   1265: 
                   1266: /*
                   1267:  * A nice little define to make doing our printks a little easier
                   1268:  */
                   1269: 
                   1270: #define WARN_LEAD KERN_WARNING "(scsi%d:%d:%d:%d) "
                   1271: #define INFO_LEAD KERN_INFO "(scsi%d:%d:%d:%d) "
                   1272: 
                   1273: /*
                   1274:  * XXX - these options apply unilaterally to _all_ 274x/284x/294x
                   1275:  *       cards in the system.  This should be fixed.  Exceptions to this
                   1276:  *       rule are noted in the comments.
                   1277:  */
                   1278: 
                   1279: 
                   1280: /*
                   1281:  * Skip the scsi bus reset.  Non 0 make us skip the reset at startup.  This
                   1282:  * has no effect on any later resets that might occur due to things like
                   1283:  * SCSI bus timeouts.
                   1284:  */
                   1285: static unsigned int aic7xxx_no_reset = 0;
                   1286: /*
                   1287:  * Certain PCI motherboards will scan PCI devices from highest to lowest,
                   1288:  * others scan from lowest to highest, and they tend to do all kinds of
                   1289:  * strange things when they come into contact with PCI bridge chips.  The
                   1290:  * net result of all this is that the PCI card that is actually used to boot
                   1291:  * the machine is very hard to detect.  Most motherboards go from lowest
                   1292:  * PCI slot number to highest, and the first SCSI controller found is the
                   1293:  * one you boot from.  The only exceptions to this are when a controller
                   1294:  * has its BIOS disabled.  So, we by default sort all of our SCSI controllers
                   1295:  * from lowest PCI slot number to highest PCI slot number.  We also force
                   1296:  * all controllers with their BIOS disabled to the end of the list.  This
                   1297:  * works on *almost* all computers.  Where it doesn't work, we have this
                   1298:  * option.  Setting this option to non-0 will reverse the order of the sort
                   1299:  * to highest first, then lowest, but will still leave cards with their BIOS
                   1300:  * disabled at the very end.  That should fix everyone up unless there are
                   1301:  * really strange cirumstances.
                   1302:  */
                   1303: static int aic7xxx_reverse_scan = 0;
                   1304: /*
1.1.1.2 ! root     1305:  * Should we force EXTENDED translation on a controller.
        !          1306:  *     0 == Use whatever is in the SEEPROM or default to off
        !          1307:  *     1 == Use whatever is in the SEEPROM or default to on
1.1       root     1308:  */
                   1309: static unsigned int aic7xxx_extended = 0;
                   1310: /*
                   1311:  * The IRQ trigger method used on EISA controllers. Does not effect PCI cards.
                   1312:  *   -1 = Use detected settings.
                   1313:  *    0 = Force Edge triggered mode.
                   1314:  *    1 = Force Level triggered mode.
                   1315:  */
                   1316: static int aic7xxx_irq_trigger = -1;
                   1317: /*
                   1318:  * This variable is used to override the termination settings on a controller.
                   1319:  * This should not be used under normal conditions.  However, in the case
                   1320:  * that a controller does not have a readable SEEPROM (so that we can't
                   1321:  * read the SEEPROM settings directly) and that a controller has a buggered
1.1.1.2 ! root     1322:  * version of the cable detection logic, this can be used to force the
1.1       root     1323:  * correct termination.  It is preferable to use the manual termination
                   1324:  * settings in the BIOS if possible, but some motherboard controllers store
                   1325:  * those settings in a format we can't read.  In other cases, auto term
                   1326:  * should also work, but the chipset was put together with no auto term
                   1327:  * logic (common on motherboard controllers).  In those cases, we have
                   1328:  * 32 bits here to work with.  That's good for 8 controllers/channels.  The
                   1329:  * bits are organized as 4 bits per channel, with scsi0 getting the lowest
                   1330:  * 4 bits in the int.  A 1 in a bit position indicates the termination setting
                   1331:  * that corresponds to that bit should be enabled, a 0 is disabled.
                   1332:  * It looks something like this:
                   1333:  *
                   1334:  *    0x0f =  1111-Single Ended Low Byte Termination on/off
                   1335:  *            ||\-Single Ended High Byte Termination on/off
                   1336:  *            |\-LVD Low Byte Termination on/off
                   1337:  *            \-LVD High Byte Termination on/off
                   1338:  *
                   1339:  * For non-Ultra2 controllers, the upper 2 bits are not important.  So, to
                   1340:  * enable both high byte and low byte termination on scsi0, I would need to
                   1341:  * make sure that the override_term variable was set to 0x03 (bits 0011).
                   1342:  * To make sure that all termination is enabled on an Ultra2 controller at
                   1343:  * scsi2 and only high byte termination on scsi1 and high and low byte
                   1344:  * termination on scsi0, I would set override_term=0xf23 (bits 1111 0010 0011)
                   1345:  *
                   1346:  * For the most part, users should never have to use this, that's why I
                   1347:  * left it fairly cryptic instead of easy to understand.  If you need it,
                   1348:  * most likely someone will be telling you what your's needs to be set to.
                   1349:  */
                   1350: static int aic7xxx_override_term = -1;
                   1351: /*
                   1352:  * Certain motherboard chipset controllers tend to screw
                   1353:  * up the polarity of the term enable output pin.  Use this variable
                   1354:  * to force the correct polarity for your system.  This is a bitfield variable
                   1355:  * similar to the previous one, but this one has one bit per channel instead
                   1356:  * of four.
                   1357:  *    0 = Force the setting to active low.
                   1358:  *    1 = Force setting to active high.
                   1359:  * Most Adaptec cards are active high, several motherboards are active low.
                   1360:  * To force a 2940 card at SCSI 0 to active high and a motherboard 7895
                   1361:  * controller at scsi1 and scsi2 to active low, and a 2910 card at scsi3
                   1362:  * to active high, you would need to set stpwlev=0x9 (bits 1001).
                   1363:  *
                   1364:  * People shouldn't need to use this, but if you are experiencing lots of
                   1365:  * SCSI timeout problems, this may help.  There is one sure way to test what
                   1366:  * this option needs to be.  Using a boot floppy to boot the system, configure
                   1367:  * your system to enable all SCSI termination (in the Adaptec SCSI BIOS) and
                   1368:  * if needed then also pass a value to override_term to make sure that the
                   1369:  * driver is enabling SCSI termination, then set this variable to either 0
                   1370:  * or 1.  When the driver boots, make sure there are *NO* SCSI cables
                   1371:  * connected to your controller.  If it finds and inits the controller
                   1372:  * without problem, then the setting you passed to stpwlev was correct.  If
                   1373:  * the driver goes into a reset loop and hangs the system, then you need the
                   1374:  * other setting for this variable.  If neither setting lets the machine
                   1375:  * boot then you have definite termination problems that may not be fixable.
                   1376:  */
                   1377: static int aic7xxx_stpwlev = -1;
                   1378: /*
                   1379:  * Set this to non-0 in order to force the driver to panic the kernel
                   1380:  * and print out debugging info on a SCSI abort or reset cycle.
                   1381:  */
                   1382: static int aic7xxx_panic_on_abort = 0;
                   1383: /*
                   1384:  * PCI bus parity checking of the Adaptec controllers.  This is somewhat
                   1385:  * dubious at best.  To my knowledge, this option has never actually
                   1386:  * solved a PCI parity problem, but on certain machines with broken PCI
                   1387:  * chipset configurations, it can generate tons of false error messages.
                   1388:  * It's included in the driver for completeness.
                   1389:  *   0 = Shut off PCI parity check
                   1390:  *  -1 = Normal polarity pci parity checking
                   1391:  *   1 = reverse polarity pci parity checking
                   1392:  *
                   1393:  * NOTE: you can't actually pass -1 on the lilo prompt.  So, to set this
                   1394:  * variable to -1 you would actually want to simply pass the variable
                   1395:  * name without a number.  That will invert the 0 which will result in
                   1396:  * -1.
                   1397:  */
                   1398: static int aic7xxx_pci_parity = 0;
                   1399: /*
                   1400:  * Set this to any non-0 value to cause us to dump the contents of all
                   1401:  * the card's registers in a hex dump format tailored to each model of
                   1402:  * controller.
1.1.1.2 ! root     1403:  *
1.1       root     1404:  * NOTE: THE CONTROLLER IS LEFT IN AN UNUSEABLE STATE BY THIS OPTION.
                   1405:  *       YOU CANNOT BOOT UP WITH THIS OPTION, IT IS FOR DEBUGGING PURPOSES
                   1406:  *       ONLY
                   1407:  */
                   1408: static int aic7xxx_dump_card = 0;
                   1409: /*
                   1410:  * Set this to a non-0 value to make us dump out the 32 bit instruction
                   1411:  * registers on the card after completing the sequencer download.  This
                   1412:  * allows the actual sequencer download to be verified.  It is possible
                   1413:  * to use this option and still boot up and run your system.  This is
                   1414:  * only intended for debugging purposes.
                   1415:  */
                   1416: static int aic7xxx_dump_sequencer = 0;
                   1417: /*
                   1418:  * Certain newer motherboards have put new PCI based devices into the
                   1419:  * IO spaces that used to typically be occupied by VLB or EISA cards.
                   1420:  * This overlap can cause these newer motherboards to lock up when scanned
                   1421:  * for older EISA and VLB devices.  Setting this option to non-0 will
                   1422:  * cause the driver to skip scanning for any VLB or EISA controllers and
                   1423:  * only support the PCI controllers.  NOTE: this means that if the kernel
                   1424:  * os compiled with PCI support disabled, then setting this to non-0
                   1425:  * would result in never finding any devices :)
                   1426:  */
                   1427: static int aic7xxx_no_probe = 0;
                   1428: 
                   1429: /*
                   1430:  * So that insmod can find the variable and make it point to something
                   1431:  */
                   1432: #ifdef MODULE
                   1433: static char * aic7xxx = NULL;
                   1434: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,18)
                   1435: MODULE_PARM(aic7xxx, "s");
                   1436: #endif
                   1437: 
                   1438: /*
                   1439:  * Just in case someone uses commas to separate items on the insmod
                   1440:  * command line, we define a dummy buffer here to avoid having insmod
                   1441:  * write wild stuff into our code segment
                   1442:  */
                   1443: static char dummy_buffer[60] = "Please don't trounce on me insmod!!\n";
                   1444: 
                   1445: #endif
                   1446: 
                   1447: #define VERBOSE_NORMAL         0x0000
                   1448: #define VERBOSE_NEGOTIATION    0x0001
                   1449: #define VERBOSE_SEQINT         0x0002
                   1450: #define VERBOSE_SCSIINT        0x0004
                   1451: #define VERBOSE_PROBE          0x0008
                   1452: #define VERBOSE_PROBE2         0x0010
                   1453: #define VERBOSE_NEGOTIATION2   0x0020
                   1454: #define VERBOSE_MINOR_ERROR    0x0040
                   1455: #define VERBOSE_TRACING        0x0080
                   1456: #define VERBOSE_ABORT          0x0f00
                   1457: #define VERBOSE_ABORT_MID      0x0100
                   1458: #define VERBOSE_ABORT_FIND     0x0200
                   1459: #define VERBOSE_ABORT_PROCESS  0x0400
                   1460: #define VERBOSE_ABORT_RETURN   0x0800
                   1461: #define VERBOSE_RESET          0xf000
                   1462: #define VERBOSE_RESET_MID      0x1000
                   1463: #define VERBOSE_RESET_FIND     0x2000
                   1464: #define VERBOSE_RESET_PROCESS  0x4000
                   1465: #define VERBOSE_RESET_RETURN   0x8000
                   1466: static int aic7xxx_verbose = VERBOSE_NORMAL | VERBOSE_NEGOTIATION |
                   1467:            VERBOSE_PROBE;                     /* verbose messages */
                   1468: 
                   1469: 
                   1470: /****************************************************************************
                   1471:  *
                   1472:  * We're going to start putting in function declarations so that order of
                   1473:  * functions is no longer important.  As needed, they are added here.
                   1474:  *
                   1475:  ***************************************************************************/
                   1476: 
                   1477: static void aic7xxx_panic_abort(struct aic7xxx_host *p, Scsi_Cmnd *cmd);
                   1478: static void aic7xxx_print_card(struct aic7xxx_host *p);
                   1479: static void aic7xxx_print_scratch_ram(struct aic7xxx_host *p);
                   1480: static void aic7xxx_print_sequencer(struct aic7xxx_host *p, int downloaded);
                   1481: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   1482: static void aic7xxx_check_scbs(struct aic7xxx_host *p, char *buffer);
                   1483: #endif
                   1484: 
                   1485: /****************************************************************************
                   1486:  *
                   1487:  * These functions are now used.  They happen to be wrapped in useless
                   1488:  * inb/outb port read/writes around the real reads and writes because it
                   1489:  * seems that certain very fast CPUs have a problem dealing with us when
                   1490:  * going at full speed.
                   1491:  *
                   1492:  ***************************************************************************/
                   1493: 
                   1494: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
                   1495: static inline void
                   1496: mdelay(int milliseconds)
                   1497: {
                   1498:   int i;
                   1499: 
                   1500:   for(i=0; i<milliseconds; i++)
                   1501:     udelay(1000);
                   1502: }
1.1.1.2 ! root     1503: 
        !          1504: static inline int
        !          1505: time_after_eq(unsigned long a, unsigned long b)
        !          1506: {
        !          1507:   return((long)((a) - (b)) >= 0L);
        !          1508: }
        !          1509: 
        !          1510: static inline int
        !          1511: timer_pending(struct timer_list *timer)
        !          1512: {
        !          1513:   return( timer->prev != NULL );
        !          1514: }
        !          1515: 
        !          1516: #define PCI_DEVICE_ID_ADAPTEC_1480A 0x6075
        !          1517: 
1.1       root     1518: #endif
                   1519: 
                   1520: static inline unsigned char
                   1521: aic_inb(struct aic7xxx_host *p, long port)
                   1522: {
                   1523: #ifdef MMAPIO
                   1524:   unsigned char x;
                   1525:   if(p->maddr)
                   1526:   {
                   1527:     x = p->maddr[port];
                   1528:   }
                   1529:   else
                   1530:   {
                   1531:     x = inb(p->base + port);
                   1532:   }
                   1533:   mb();
                   1534:   return(x);
                   1535: #else
                   1536:   return(inb(p->base + port));
                   1537: #endif
                   1538: }
                   1539: 
                   1540: static inline void
                   1541: aic_outb(struct aic7xxx_host *p, unsigned char val, long port)
                   1542: {
                   1543: #ifdef MMAPIO
                   1544:   if(p->maddr)
                   1545:   {
                   1546:     p->maddr[port] = val;
                   1547:   }
                   1548:   else
                   1549:   {
                   1550:     outb(val, p->base + port);
                   1551:   }
                   1552:   mb();
                   1553: #else
                   1554:   outb(val, p->base + port);
                   1555: #endif
                   1556: }
                   1557: 
                   1558: /*+F*************************************************************************
                   1559:  * Function:
                   1560:  *   aic7xxx_setup
                   1561:  *
                   1562:  * Description:
                   1563:  *   Handle Linux boot parameters. This routine allows for assigning a value
                   1564:  *   to a parameter with a ':' between the parameter and the value.
                   1565:  *   ie. aic7xxx=unpause:0x0A,extended
                   1566:  *-F*************************************************************************/
                   1567: void
                   1568: aic7xxx_setup(char *s, int *dummy)
                   1569: {
                   1570:   int   i, n;
                   1571:   char *p;
                   1572:   char *end;
                   1573: 
                   1574:   static struct {
                   1575:     const char *name;
                   1576:     unsigned int *flag;
                   1577:   } options[] = {
                   1578:     { "extended",    &aic7xxx_extended },
                   1579:     { "no_reset",    &aic7xxx_no_reset },
                   1580:     { "irq_trigger", &aic7xxx_irq_trigger },
                   1581:     { "verbose",     &aic7xxx_verbose },
                   1582:     { "reverse_scan",&aic7xxx_reverse_scan },
                   1583:     { "override_term", &aic7xxx_override_term },
                   1584:     { "stpwlev", &aic7xxx_stpwlev },
                   1585:     { "no_probe", &aic7xxx_no_probe },
                   1586:     { "panic_on_abort", &aic7xxx_panic_on_abort },
                   1587:     { "pci_parity", &aic7xxx_pci_parity },
                   1588:     { "dump_card", &aic7xxx_dump_card },
                   1589:     { "dump_sequencer", &aic7xxx_dump_sequencer },
                   1590:     { "tag_info",    NULL }
                   1591:   };
                   1592: 
                   1593:   end = strchr(s, '\0');
                   1594: 
                   1595:   for (p = strtok(s, ",."); p; p = strtok(NULL, ",."))
                   1596:   {
                   1597:     for (i = 0; i < NUMBER(options); i++)
                   1598:     {
                   1599:       n = strlen(options[i].name);
                   1600:       if (!strncmp(options[i].name, p, n))
                   1601:       {
                   1602:         if (!strncmp(p, "tag_info", n))
                   1603:         {
                   1604:           if (p[n] == ':')
                   1605:           {
                   1606:             char *base;
                   1607:             char *tok, *tok_end, *tok_end2;
                   1608:             char tok_list[] = { '.', ',', '{', '}', '\0' };
                   1609:             int i, instance = -1, device = -1;
                   1610:             unsigned char done = FALSE;
                   1611: 
                   1612:             base = p;
                   1613:             tok = base + n + 1;  /* Forward us just past the ':' */
                   1614:             tok_end = strchr(tok, '\0');
                   1615:             if (tok_end < end)
                   1616:               *tok_end = ',';
                   1617:             while(!done)
                   1618:             {
                   1619:               switch(*tok)
                   1620:               {
                   1621:                 case '{':
                   1622:                   if (instance == -1)
                   1623:                     instance = 0;
                   1624:                   else if (device == -1)
                   1625:                     device = 0;
                   1626:                   tok++;
                   1627:                   break;
                   1628:                 case '}':
                   1629:                   if (device != -1)
                   1630:                     device = -1;
                   1631:                   else if (instance != -1)
                   1632:                     instance = -1;
                   1633:                   tok++;
                   1634:                   break;
                   1635:                 case ',':
                   1636:                 case '.':
                   1637:                   if (instance == -1)
                   1638:                     done = TRUE;
                   1639:                   else if (device >= 0)
                   1640:                     device++;
                   1641:                   else if (instance >= 0)
                   1642:                     instance++;
1.1.1.2 ! root     1643:                   if ( (device >= MAX_TARGETS) ||
1.1       root     1644:                        (instance >= NUMBER(aic7xxx_tag_info)) )
                   1645:                     done = TRUE;
                   1646:                   tok++;
                   1647:                   if (!done)
                   1648:                   {
                   1649:                     base = tok;
                   1650:                   }
                   1651:                   break;
                   1652:                 case '\0':
                   1653:                   done = TRUE;
                   1654:                   break;
                   1655:                 default:
                   1656:                   done = TRUE;
                   1657:                   tok_end = strchr(tok, '\0');
                   1658:                   for(i=0; tok_list[i]; i++)
                   1659:                   {
                   1660:                     tok_end2 = strchr(tok, tok_list[i]);
                   1661:                     if ( (tok_end2) && (tok_end2 < tok_end) )
                   1662:                     {
                   1663:                       tok_end = tok_end2;
                   1664:                       done = FALSE;
                   1665:                     }
                   1666:                   }
                   1667:                   if ( (instance >= 0) && (device >= 0) &&
                   1668:                        (instance < NUMBER(aic7xxx_tag_info)) &&
                   1669:                        (device < MAX_TARGETS) )
                   1670:                     aic7xxx_tag_info[instance].tag_commands[device] =
                   1671:                       simple_strtoul(tok, NULL, 0) & 0xff;
                   1672:                   tok = tok_end;
                   1673:                   break;
                   1674:               }
                   1675:             }
                   1676:             while((p != base) && (p != NULL))
                   1677:               p = strtok(NULL, ",.");
                   1678:           }
                   1679:         }
                   1680:         else if (p[n] == ':')
                   1681:         {
                   1682:           *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
                   1683:         }
                   1684:         else if (!strncmp(p, "verbose", n))
                   1685:         {
                   1686:           *(options[i].flag) = 0xff09;
                   1687:         }
                   1688:         else
                   1689:         {
                   1690:           *(options[i].flag) = ~(*(options[i].flag));
                   1691:         }
                   1692:       }
                   1693:     }
                   1694:   }
                   1695: }
                   1696: 
                   1697: /*+F*************************************************************************
                   1698:  * Function:
                   1699:  *   pause_sequencer
                   1700:  *
                   1701:  * Description:
                   1702:  *   Pause the sequencer and wait for it to actually stop - this
                   1703:  *   is important since the sequencer can disable pausing for critical
                   1704:  *   sections.
                   1705:  *-F*************************************************************************/
                   1706: static inline void
                   1707: pause_sequencer(struct aic7xxx_host *p)
                   1708: {
                   1709:   aic_outb(p, p->pause, HCNTRL);
                   1710:   while ((aic_inb(p, HCNTRL) & PAUSE) == 0)
                   1711:   {
                   1712:     ;
                   1713:   }
                   1714: }
                   1715: 
                   1716: /*+F*************************************************************************
                   1717:  * Function:
                   1718:  *   unpause_sequencer
                   1719:  *
                   1720:  * Description:
                   1721:  *   Unpause the sequencer. Unremarkable, yet done often enough to
                   1722:  *   warrant an easy way to do it.
                   1723:  *-F*************************************************************************/
                   1724: static inline void
                   1725: unpause_sequencer(struct aic7xxx_host *p, int unpause_always)
                   1726: {
                   1727:   if (unpause_always ||
                   1728:       ( !(aic_inb(p, INTSTAT) & (SCSIINT | SEQINT | BRKADRINT)) &&
                   1729:         !(p->flags & AHC_HANDLING_REQINITS) ) )
                   1730:   {
                   1731:     aic_outb(p, p->unpause, HCNTRL);
                   1732:   }
                   1733: }
                   1734: 
                   1735: /*+F*************************************************************************
                   1736:  * Function:
                   1737:  *   restart_sequencer
                   1738:  *
                   1739:  * Description:
                   1740:  *   Restart the sequencer program from address zero.  This assumes
                   1741:  *   that the sequencer is already paused.
                   1742:  *-F*************************************************************************/
                   1743: static inline void
                   1744: restart_sequencer(struct aic7xxx_host *p)
                   1745: {
                   1746:   aic_outb(p, 0, SEQADDR0);
                   1747:   aic_outb(p, 0, SEQADDR1);
                   1748:   aic_outb(p, FASTMODE, SEQCTL);
                   1749: }
                   1750: 
                   1751: /*
                   1752:  * We include the aic7xxx_seq.c file here so that the other defines have
                   1753:  * already been made, and so that it comes before the code that actually
                   1754:  * downloads the instructions (since we don't typically use function
                   1755:  * prototype, our code has to be ordered that way, it's a left-over from
                   1756:  * the original driver days.....I should fix it some time DL).
                   1757:  */
                   1758: #include "aic7xxx_seq.c"
                   1759: 
                   1760: /*+F*************************************************************************
                   1761:  * Function:
                   1762:  *   aic7xxx_check_patch
                   1763:  *
                   1764:  * Description:
                   1765:  *   See if the next patch to download should be downloaded.
                   1766:  *-F*************************************************************************/
                   1767: static int
                   1768: aic7xxx_check_patch(struct aic7xxx_host *p,
                   1769:   struct sequencer_patch **start_patch, int start_instr, int *skip_addr)
                   1770: {
                   1771:   struct sequencer_patch *cur_patch;
                   1772:   struct sequencer_patch *last_patch;
                   1773:   int num_patches;
                   1774: 
                   1775:   num_patches = sizeof(sequencer_patches)/sizeof(struct sequencer_patch);
                   1776:   last_patch = &sequencer_patches[num_patches];
                   1777:   cur_patch = *start_patch;
                   1778: 
                   1779:   while ((cur_patch < last_patch) && (start_instr == cur_patch->begin))
                   1780:   {
                   1781:     if (cur_patch->patch_func(p) == 0)
                   1782:     {
                   1783:       /*
                   1784:        * Start rejecting code.
                   1785:        */
                   1786:       *skip_addr = start_instr + cur_patch->skip_instr;
                   1787:       cur_patch += cur_patch->skip_patch;
                   1788:     }
                   1789:     else
                   1790:     {
                   1791:       /*
                   1792:        * Found an OK patch.  Advance the patch pointer to the next patch
                   1793:        * and wait for our instruction pointer to get here.
                   1794:        */
                   1795:       cur_patch++;
                   1796:     }
                   1797:   }
                   1798: 
                   1799:   *start_patch = cur_patch;
                   1800:   if (start_instr < *skip_addr)
                   1801:     /*
                   1802:      * Still skipping
                   1803:      */
                   1804:     return (0);
                   1805:   return(1);
                   1806: }
                   1807: 
                   1808: 
                   1809: /*+F*************************************************************************
                   1810:  * Function:
                   1811:  *   aic7xxx_download_instr
                   1812:  *
                   1813:  * Description:
                   1814:  *   Find the next patch to download.
                   1815:  *-F*************************************************************************/
                   1816: static void
                   1817: aic7xxx_download_instr(struct aic7xxx_host *p, int instrptr,
                   1818:   unsigned char *dconsts)
                   1819: {
                   1820:   union ins_formats instr;
                   1821:   struct ins_format1 *fmt1_ins;
                   1822:   struct ins_format3 *fmt3_ins;
                   1823:   unsigned char opcode;
                   1824: 
                   1825:   instr = *(union ins_formats*) &seqprog[instrptr * 4];
                   1826: 
                   1827:   instr.integer = le32_to_cpu(instr.integer);
1.1.1.2 ! root     1828: 
1.1       root     1829:   fmt1_ins = &instr.format1;
                   1830:   fmt3_ins = NULL;
                   1831: 
                   1832:   /* Pull the opcode */
                   1833:   opcode = instr.format1.opcode;
                   1834:   switch (opcode)
                   1835:   {
                   1836:     case AIC_OP_JMP:
                   1837:     case AIC_OP_JC:
                   1838:     case AIC_OP_JNC:
                   1839:     case AIC_OP_CALL:
                   1840:     case AIC_OP_JNE:
                   1841:     case AIC_OP_JNZ:
                   1842:     case AIC_OP_JE:
                   1843:     case AIC_OP_JZ:
                   1844:     {
                   1845:       struct sequencer_patch *cur_patch;
                   1846:       int address_offset;
                   1847:       unsigned int address;
                   1848:       int skip_addr;
                   1849:       int i;
                   1850: 
                   1851:       fmt3_ins = &instr.format3;
                   1852:       address_offset = 0;
                   1853:       address = fmt3_ins->address;
                   1854:       cur_patch = sequencer_patches;
                   1855:       skip_addr = 0;
                   1856: 
                   1857:       for (i = 0; i < address;)
                   1858:       {
                   1859:         aic7xxx_check_patch(p, &cur_patch, i, &skip_addr);
                   1860:         if (skip_addr > i)
                   1861:         {
                   1862:           int end_addr;
                   1863: 
                   1864:           end_addr = MIN(address, skip_addr);
                   1865:           address_offset += end_addr - i;
                   1866:           i = skip_addr;
                   1867:         }
                   1868:         else
                   1869:         {
                   1870:           i++;
                   1871:         }
                   1872:       }
                   1873:       address -= address_offset;
                   1874:       fmt3_ins->address = address;
                   1875:       /* Fall Through to the next code section */
                   1876:     }
                   1877:     case AIC_OP_OR:
                   1878:     case AIC_OP_AND:
                   1879:     case AIC_OP_XOR:
                   1880:     case AIC_OP_ADD:
                   1881:     case AIC_OP_ADC:
                   1882:     case AIC_OP_BMOV:
                   1883:       if (fmt1_ins->parity != 0)
                   1884:       {
                   1885:         fmt1_ins->immediate = dconsts[fmt1_ins->immediate];
                   1886:       }
                   1887:       fmt1_ins->parity = 0;
                   1888:       /* Fall Through to the next code section */
                   1889:     case AIC_OP_ROL:
                   1890:       if ((p->features & AHC_ULTRA2) != 0)
                   1891:       {
                   1892:         int i, count;
                   1893: 
                   1894:         /* Calculate odd parity for the instruction */
                   1895:         for ( i=0, count=0; i < 31; i++)
                   1896:         {
                   1897:           unsigned int mask;
                   1898: 
                   1899:           mask = 0x01 << i;
                   1900:           if ((instr.integer & mask) != 0)
                   1901:             count++;
                   1902:         }
                   1903:         if (!(count & 0x01))
                   1904:           instr.format1.parity = 1;
                   1905:       }
                   1906:       else
                   1907:       {
                   1908:         if (fmt3_ins != NULL)
                   1909:         {
                   1910:           instr.integer =  fmt3_ins->immediate |
                   1911:                           (fmt3_ins->source << 8) |
                   1912:                           (fmt3_ins->address << 16) |
                   1913:                           (fmt3_ins->opcode << 25);
                   1914:         }
                   1915:         else
                   1916:         {
                   1917:           instr.integer =  fmt1_ins->immediate |
                   1918:                           (fmt1_ins->source << 8) |
                   1919:                           (fmt1_ins->destination << 16) |
                   1920:                           (fmt1_ins->ret << 24) |
                   1921:                           (fmt1_ins->opcode << 25);
                   1922:         }
                   1923:       }
                   1924:       aic_outb(p, (instr.integer & 0xff), SEQRAM);
                   1925:       aic_outb(p, ((instr.integer >> 8) & 0xff), SEQRAM);
                   1926:       aic_outb(p, ((instr.integer >> 16) & 0xff), SEQRAM);
                   1927:       aic_outb(p, ((instr.integer >> 24) & 0xff), SEQRAM);
                   1928:       break;
                   1929: 
                   1930:     default:
                   1931:       panic("aic7xxx: Unknown opcode encountered in sequencer program.");
                   1932:       break;
                   1933:   }
                   1934: }
                   1935: 
                   1936: 
                   1937: /*+F*************************************************************************
                   1938:  * Function:
                   1939:  *   aic7xxx_loadseq
                   1940:  *
                   1941:  * Description:
                   1942:  *   Load the sequencer code into the controller memory.
                   1943:  *-F*************************************************************************/
                   1944: static void
                   1945: aic7xxx_loadseq(struct aic7xxx_host *p)
                   1946: {
                   1947:   struct sequencer_patch *cur_patch;
                   1948:   int i;
                   1949:   int downloaded;
                   1950:   int skip_addr;
                   1951:   unsigned char download_consts[4] = {0, 0, 0, 0};
                   1952: 
                   1953:   if (aic7xxx_verbose & VERBOSE_PROBE)
                   1954:   {
                   1955:     printk(KERN_INFO "(scsi%d) Downloading sequencer code...", p->host_no);
                   1956:   }
1.1.1.2 ! root     1957: #if 0
1.1       root     1958:   download_consts[TMODE_NUMCMDS] = p->num_targetcmds;
1.1.1.2 ! root     1959: #endif
        !          1960:   download_consts[TMODE_NUMCMDS] = 0;
1.1       root     1961:   cur_patch = &sequencer_patches[0];
                   1962:   downloaded = 0;
                   1963:   skip_addr = 0;
                   1964: 
                   1965:   aic_outb(p, PERRORDIS|LOADRAM|FAILDIS|FASTMODE, SEQCTL);
                   1966:   aic_outb(p, 0, SEQADDR0);
                   1967:   aic_outb(p, 0, SEQADDR1);
                   1968: 
                   1969:   for (i = 0; i < sizeof(seqprog) / 4;  i++)
                   1970:   {
                   1971:     if (aic7xxx_check_patch(p, &cur_patch, i, &skip_addr) == 0)
                   1972:     {
                   1973:       /* Skip this instruction for this configuration. */
                   1974:       continue;
                   1975:     }
                   1976:     aic7xxx_download_instr(p, i, &download_consts[0]);
                   1977:     downloaded++;
                   1978:   }
                   1979: 
                   1980:   aic_outb(p, 0, SEQADDR0);
                   1981:   aic_outb(p, 0, SEQADDR1);
                   1982:   aic_outb(p, FASTMODE | FAILDIS, SEQCTL);
                   1983:   unpause_sequencer(p, TRUE);
                   1984:   mdelay(1);
                   1985:   pause_sequencer(p);
                   1986:   aic_outb(p, FASTMODE, SEQCTL);
                   1987:   if (aic7xxx_verbose & VERBOSE_PROBE)
                   1988:   {
                   1989:     printk(" %d instructions downloaded\n", downloaded);
                   1990:   }
                   1991:   if (aic7xxx_dump_sequencer)
                   1992:     aic7xxx_print_sequencer(p, downloaded);
                   1993: }
                   1994: 
                   1995: /*+F*************************************************************************
                   1996:  * Function:
                   1997:  *   aic7xxx_print_sequencer
                   1998:  *
                   1999:  * Description:
                   2000:  *   Print the contents of the sequencer memory to the screen.
                   2001:  *-F*************************************************************************/
                   2002: static void
                   2003: aic7xxx_print_sequencer(struct aic7xxx_host *p, int downloaded)
                   2004: {
                   2005:   int i, k, temp;
1.1.1.2 ! root     2006: 
1.1       root     2007:   aic_outb(p, PERRORDIS|LOADRAM|FAILDIS|FASTMODE, SEQCTL);
                   2008:   aic_outb(p, 0, SEQADDR0);
                   2009:   aic_outb(p, 0, SEQADDR1);
                   2010: 
                   2011:   k = 0;
                   2012:   for (i=0; i < downloaded; i++)
                   2013:   {
                   2014:     if ( k == 0 )
                   2015:       printk("%03x: ", i);
                   2016:     temp = aic_inb(p, SEQRAM);
                   2017:     temp |= (aic_inb(p, SEQRAM) << 8);
                   2018:     temp |= (aic_inb(p, SEQRAM) << 16);
                   2019:     temp |= (aic_inb(p, SEQRAM) << 24);
                   2020:     printk("%08x", temp);
                   2021:     if ( ++k == 8 )
                   2022:     {
                   2023:       printk("\n");
                   2024:       k = 0;
                   2025:     }
                   2026:     else
                   2027:       printk(" ");
                   2028:   }
                   2029:   aic_outb(p, 0, SEQADDR0);
                   2030:   aic_outb(p, 0, SEQADDR1);
                   2031:   aic_outb(p, FASTMODE | FAILDIS, SEQCTL);
                   2032:   unpause_sequencer(p, TRUE);
                   2033:   mdelay(1);
                   2034:   pause_sequencer(p);
                   2035:   aic_outb(p, FASTMODE, SEQCTL);
                   2036:   printk("\n");
                   2037: }
                   2038: 
                   2039: /*+F*************************************************************************
                   2040:  * Function:
                   2041:  *   aic7xxx_delay
                   2042:  *
                   2043:  * Description:
                   2044:  *   Delay for specified amount of time.  We use mdelay because the timer
                   2045:  *   interrupt is not guaranteed to be enabled.  This will cause an
                   2046:  *   infinite loop since jiffies (clock ticks) is not updated.
                   2047:  *-F*************************************************************************/
                   2048: static void
                   2049: aic7xxx_delay(int seconds)
                   2050: {
                   2051:   mdelay(seconds * 1000);
                   2052: }
                   2053: 
                   2054: /*+F*************************************************************************
                   2055:  * Function:
                   2056:  *   aic7xxx_info
                   2057:  *
                   2058:  * Description:
                   2059:  *   Return a string describing the driver.
                   2060:  *-F*************************************************************************/
                   2061: const char *
                   2062: aic7xxx_info(struct Scsi_Host *dooh)
                   2063: {
                   2064:   static char buffer[256];
                   2065:   char *bp;
                   2066:   struct aic7xxx_host *p;
                   2067: 
                   2068:   bp = &buffer[0];
                   2069:   p = (struct aic7xxx_host *)dooh->hostdata;
                   2070:   memset(bp, 0, sizeof(buffer));
                   2071:   strcpy(bp, "Adaptec AHA274x/284x/294x (EISA/VLB/PCI-Fast SCSI) ");
                   2072:   strcat(bp, AIC7XXX_C_VERSION);
                   2073:   strcat(bp, "/");
                   2074:   strcat(bp, AIC7XXX_H_VERSION);
                   2075:   strcat(bp, "\n");
                   2076:   strcat(bp, "       <");
                   2077:   strcat(bp, board_names[p->board_name_index]);
                   2078:   strcat(bp, ">");
                   2079: 
                   2080:   return(bp);
                   2081: }
                   2082: 
                   2083: /*+F*************************************************************************
                   2084:  * Function:
                   2085:  *   aic7xxx_find_syncrate
                   2086:  *
                   2087:  * Description:
                   2088:  *   Look up the valid period to SCSIRATE conversion in our table
                   2089:  *-F*************************************************************************/
                   2090: static struct aic7xxx_syncrate *
                   2091: aic7xxx_find_syncrate(struct aic7xxx_host *p, unsigned int *period,
                   2092:   unsigned int maxsync)
                   2093: {
                   2094:   struct aic7xxx_syncrate *syncrate;
                   2095: 
                   2096:   syncrate = &aic7xxx_syncrates[maxsync];
                   2097:   while ( (syncrate->rate[0] != NULL) &&
                   2098:          (!(p->features & AHC_ULTRA2) || syncrate->sxfr_ultra2) )
                   2099:   {
                   2100:     if ( *period <= syncrate->period )
                   2101:     {
                   2102:       /*
                   2103:        * When responding to a target that requests sync, the requested rate
                   2104:        * may fall between two rates that we can output, but still be a rate
                   2105:        * that we can receive.  Because of this, we want to respond with the
                   2106:        * same rate that it sent to us even if the persiod we use to send
                   2107:        * data to it is lower.  Only lower the response period if we must.
                   2108:        */
                   2109:       if(syncrate == &aic7xxx_syncrates[maxsync])
                   2110:       {
                   2111:         *period = syncrate->period;
                   2112:       }
                   2113:       break;
                   2114:     }
                   2115:     syncrate++;
                   2116:   }
                   2117:   if ( (*period == 0) || (syncrate->rate[0] == NULL) ||
                   2118:        ((p->features & AHC_ULTRA2) && (syncrate->sxfr_ultra2 == 0)) )
                   2119:   {
                   2120:     /*
                   2121:      * Use async transfers for this target
                   2122:      */
                   2123:     *period = 0;
                   2124:     syncrate = NULL;
                   2125:   }
                   2126:   return (syncrate);
                   2127: }
                   2128: 
                   2129: 
                   2130: /*+F*************************************************************************
                   2131:  * Function:
                   2132:  *   aic7xxx_find_period
                   2133:  *
                   2134:  * Description:
                   2135:  *   Look up the valid SCSIRATE to period conversion in our table
                   2136:  *-F*************************************************************************/
                   2137: static unsigned int
                   2138: aic7xxx_find_period(struct aic7xxx_host *p, unsigned int scsirate,
                   2139:   unsigned int maxsync)
                   2140: {
                   2141:   struct aic7xxx_syncrate *syncrate;
                   2142: 
                   2143:   if ((p->features & AHC_ULTRA2) != 0)
                   2144:   {
                   2145:     scsirate &= SXFR_ULTRA2;
                   2146:   }
                   2147:   else
                   2148:   {
                   2149:     scsirate &= SXFR;
                   2150:   }
                   2151: 
                   2152:   syncrate = &aic7xxx_syncrates[maxsync];
                   2153:   while (syncrate->rate[0] != NULL)
                   2154:   {
                   2155:     if ((p->features & AHC_ULTRA2) != 0)
                   2156:     {
                   2157:       if (syncrate->sxfr_ultra2 == 0)
                   2158:         break;
                   2159:       else if (scsirate == syncrate->sxfr_ultra2)
                   2160:         return (syncrate->period);
                   2161:     }
                   2162:     else if (scsirate == (syncrate->sxfr & ~ULTRA_SXFR))
                   2163:     {
                   2164:       return (syncrate->period);
                   2165:     }
                   2166:     syncrate++;
                   2167:   }
                   2168:   return (0); /* async */
                   2169: }
                   2170: 
                   2171: /*+F*************************************************************************
                   2172:  * Function:
                   2173:  *   aic7xxx_validate_offset
                   2174:  *
                   2175:  * Description:
                   2176:  *   Set a valid offset value for a particular card in use and transfer
                   2177:  *   settings in use.
                   2178:  *-F*************************************************************************/
                   2179: static void
                   2180: aic7xxx_validate_offset(struct aic7xxx_host *p,
                   2181:   struct aic7xxx_syncrate *syncrate, unsigned int *offset, int wide)
                   2182: {
                   2183:   unsigned int maxoffset;
                   2184: 
                   2185:   /* Limit offset to what the card (and device) can do */
                   2186:   if (syncrate == NULL)
                   2187:   {
                   2188:     maxoffset = 0;
                   2189:   }
                   2190:   else if (p->features & AHC_ULTRA2)
                   2191:   {
                   2192:     maxoffset = MAX_OFFSET_ULTRA2;
                   2193:   }
                   2194:   else
                   2195:   {
                   2196:     if (wide)
                   2197:       maxoffset = MAX_OFFSET_16BIT;
                   2198:     else
                   2199:       maxoffset = MAX_OFFSET_8BIT;
                   2200:   }
                   2201:   *offset = MIN(*offset, maxoffset);
                   2202: }
                   2203: 
                   2204: /*+F*************************************************************************
                   2205:  * Function:
                   2206:  *   aic7xxx_set_syncrate
                   2207:  *
                   2208:  * Description:
                   2209:  *   Set the actual syncrate down in the card and in our host structs
                   2210:  *-F*************************************************************************/
                   2211: static void
                   2212: aic7xxx_set_syncrate(struct aic7xxx_host *p, struct aic7xxx_syncrate *syncrate,
                   2213:     int target, int channel, unsigned int period, unsigned int offset,
                   2214:     unsigned int type)
                   2215: {
                   2216:   unsigned char tindex;
                   2217:   unsigned short target_mask;
                   2218:   unsigned char lun;
                   2219:   unsigned int old_period, old_offset;
                   2220: 
                   2221:   tindex = target | (channel << 3);
                   2222:   target_mask = 0x01 << tindex;
                   2223:   lun = aic_inb(p, SCB_TCL) & 0x07;
                   2224: 
                   2225:   if (syncrate == NULL)
                   2226:   {
                   2227:     period = 0;
                   2228:     offset = 0;
                   2229:   }
                   2230: 
                   2231:   old_period = p->transinfo[tindex].cur_period;
                   2232:   old_offset = p->transinfo[tindex].cur_offset;
                   2233: 
1.1.1.2 ! root     2234: 
1.1       root     2235:   if (type & AHC_TRANS_CUR)
                   2236:   {
                   2237:     unsigned int scsirate;
                   2238: 
                   2239:     scsirate = aic_inb(p, TARG_SCSIRATE + tindex);
                   2240:     if (p->features & AHC_ULTRA2)
                   2241:     {
                   2242:       scsirate &= ~SXFR_ULTRA2;
                   2243:       if (syncrate != NULL)
                   2244:       {
                   2245:         scsirate |= syncrate->sxfr_ultra2;
                   2246:       }
                   2247:       if (type & AHC_TRANS_ACTIVE)
                   2248:       {
                   2249:         aic_outb(p, offset, SCSIOFFSET);
                   2250:       }
                   2251:       aic_outb(p, offset, TARG_OFFSET + tindex);
                   2252:     }
                   2253:     else /* Not an Ultra2 controller */
                   2254:     {
                   2255:       scsirate &= ~(SXFR|SOFS);
                   2256:       p->ultraenb &= ~target_mask;
                   2257:       if (syncrate != NULL)
                   2258:       {
                   2259:         if (syncrate->sxfr & ULTRA_SXFR)
                   2260:         {
                   2261:           p->ultraenb |= target_mask;
                   2262:         }
                   2263:         scsirate |= (syncrate->sxfr & SXFR);
                   2264:         scsirate |= (offset & SOFS);
                   2265:       }
                   2266:       if (type & AHC_TRANS_ACTIVE)
                   2267:       {
                   2268:         unsigned char sxfrctl0;
                   2269: 
                   2270:         sxfrctl0 = aic_inb(p, SXFRCTL0);
                   2271:         sxfrctl0 &= ~FAST20;
                   2272:         if (p->ultraenb & target_mask)
                   2273:           sxfrctl0 |= FAST20;
                   2274:         aic_outb(p, sxfrctl0, SXFRCTL0);
                   2275:       }
                   2276:       aic_outb(p, p->ultraenb & 0xff, ULTRA_ENB);
                   2277:       aic_outb(p, (p->ultraenb >> 8) & 0xff, ULTRA_ENB + 1 );
                   2278:     }
                   2279:     if (type & AHC_TRANS_ACTIVE)
                   2280:     {
                   2281:       aic_outb(p, scsirate, SCSIRATE);
                   2282:     }
                   2283:     aic_outb(p, scsirate, TARG_SCSIRATE + tindex);
                   2284:     p->transinfo[tindex].cur_period = period;
                   2285:     p->transinfo[tindex].cur_offset = offset;
                   2286:     if ( !(type & AHC_TRANS_QUITE) &&
                   2287:          (aic7xxx_verbose & VERBOSE_NEGOTIATION) &&
                   2288:          (p->dev_flags[tindex] & DEVICE_PRINT_SDTR) )
                   2289:     {
                   2290:       if (offset)
                   2291:       {
                   2292:         int rate_mod = (scsirate & WIDEXFER) ? 1 : 0;
1.1.1.2 ! root     2293: 
1.1       root     2294:         printk(INFO_LEAD "Synchronous at %s Mbyte/sec, "
                   2295:                "offset %d.\n", p->host_no, channel, target, lun,
                   2296:                syncrate->rate[rate_mod], offset);
                   2297:       }
                   2298:       else
                   2299:       {
                   2300:         printk(INFO_LEAD "Using asynchronous transfers.\n",
                   2301:                p->host_no, channel, target, lun);
                   2302:       }
                   2303:       p->dev_flags[tindex] &= ~DEVICE_PRINT_SDTR;
                   2304:     }
                   2305:   }
                   2306: 
                   2307:   if (type & AHC_TRANS_GOAL)
                   2308:   {
                   2309:     p->transinfo[tindex].goal_period = period;
                   2310:     p->transinfo[tindex].goal_offset = offset;
                   2311:   }
                   2312: 
                   2313:   if (type & AHC_TRANS_USER)
                   2314:   {
                   2315:     p->transinfo[tindex].user_period = period;
                   2316:     p->transinfo[tindex].user_offset = offset;
                   2317:   }
                   2318: }
                   2319: 
                   2320: /*+F*************************************************************************
                   2321:  * Function:
                   2322:  *   aic7xxx_set_width
                   2323:  *
                   2324:  * Description:
                   2325:  *   Set the actual width down in the card and in our host structs
                   2326:  *-F*************************************************************************/
                   2327: static void
                   2328: aic7xxx_set_width(struct aic7xxx_host *p, int target, int channel, int lun,
                   2329:     unsigned int width, unsigned int type)
                   2330: {
                   2331:   unsigned char tindex;
                   2332:   unsigned short target_mask;
                   2333:   unsigned int old_width, new_offset;
                   2334: 
                   2335:   tindex = target | (channel << 3);
                   2336:   target_mask = 1 << tindex;
1.1.1.2 ! root     2337: 
1.1       root     2338:   old_width = p->transinfo[tindex].cur_width;
                   2339: 
                   2340:   if (p->features & AHC_ULTRA2)
                   2341:     new_offset = MAX_OFFSET_ULTRA2;
                   2342:   else if (width == MSG_EXT_WDTR_BUS_16_BIT)
                   2343:     new_offset = MAX_OFFSET_16BIT;
                   2344:   else
                   2345:     new_offset = MAX_OFFSET_8BIT;
1.1.1.2 ! root     2346: 
        !          2347:   if (type & AHC_TRANS_CUR)
1.1       root     2348:   {
                   2349:     unsigned char scsirate;
                   2350: 
                   2351:     scsirate = aic_inb(p, TARG_SCSIRATE + tindex);
                   2352: 
                   2353:     scsirate &= ~WIDEXFER;
                   2354:     if (width == MSG_EXT_WDTR_BUS_16_BIT)
                   2355:       scsirate |= WIDEXFER;
                   2356: 
                   2357:     aic_outb(p, scsirate, TARG_SCSIRATE + tindex);
                   2358: 
                   2359:     if (type & AHC_TRANS_ACTIVE)
                   2360:       aic_outb(p, scsirate, SCSIRATE);
                   2361: 
                   2362:     p->transinfo[tindex].cur_width = width;
                   2363: 
1.1.1.2 ! root     2364:     if ((aic7xxx_verbose & VERBOSE_NEGOTIATION2) &&
1.1       root     2365:         (p->dev_flags[tindex] & DEVICE_PRINT_WDTR))
                   2366:     {
                   2367:       printk(INFO_LEAD "Using %s transfers\n", p->host_no, channel, target,
                   2368:         lun, (scsirate & WIDEXFER) ? "Wide(16bit)" : "Narrow(8bit)" );
                   2369:       p->dev_flags[tindex] &= ~DEVICE_PRINT_WDTR;
                   2370:     }
                   2371:   }
                   2372: 
                   2373:   if (type & AHC_TRANS_GOAL)
                   2374:     p->transinfo[tindex].goal_width = width;
                   2375:   if (type & AHC_TRANS_USER)
                   2376:     p->transinfo[tindex].user_width = width;
                   2377: 
                   2378:   /*
                   2379:    * Having just set the width, the SDTR should come next, and we need a valid
                   2380:    * offset for the SDTR.  So, we make sure we put a valid one in here now as
                   2381:    * the goal_offset.
                   2382:    */
                   2383:   if (p->transinfo[tindex].goal_offset)
                   2384:     p->transinfo[tindex].goal_offset = new_offset;
                   2385: 
                   2386: }
1.1.1.2 ! root     2387: 
1.1       root     2388: /*+F*************************************************************************
                   2389:  * Function:
                   2390:  *   scbq_init
                   2391:  *
                   2392:  * Description:
                   2393:  *   SCB queue initialization.
                   2394:  *
                   2395:  *-F*************************************************************************/
                   2396: static void
                   2397: scbq_init(volatile scb_queue_type *queue)
                   2398: {
                   2399:   queue->head = NULL;
                   2400:   queue->tail = NULL;
                   2401: }
                   2402: 
                   2403: /*+F*************************************************************************
                   2404:  * Function:
                   2405:  *   scbq_insert_head
                   2406:  *
                   2407:  * Description:
                   2408:  *   Add an SCB to the head of the list.
                   2409:  *
                   2410:  *-F*************************************************************************/
                   2411: static inline void
                   2412: scbq_insert_head(volatile scb_queue_type *queue, struct aic7xxx_scb *scb)
                   2413: {
                   2414: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
                   2415:   unsigned long cpu_flags;
                   2416: #endif
                   2417: 
                   2418:   DRIVER_LOCK
                   2419:   scb->q_next = queue->head;
                   2420:   queue->head = scb;
                   2421:   if (queue->tail == NULL)       /* If list was empty, update tail. */
                   2422:     queue->tail = queue->head;
                   2423:   DRIVER_UNLOCK
                   2424: }
                   2425: 
                   2426: /*+F*************************************************************************
                   2427:  * Function:
                   2428:  *   scbq_remove_head
                   2429:  *
                   2430:  * Description:
                   2431:  *   Remove an SCB from the head of the list.
                   2432:  *
                   2433:  *-F*************************************************************************/
                   2434: static inline struct aic7xxx_scb *
                   2435: scbq_remove_head(volatile scb_queue_type *queue)
                   2436: {
                   2437:   struct aic7xxx_scb * scbp;
                   2438: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
                   2439:   unsigned long cpu_flags;
                   2440: #endif
                   2441: 
                   2442:   DRIVER_LOCK
                   2443:   scbp = queue->head;
                   2444:   if (queue->head != NULL)
                   2445:     queue->head = queue->head->q_next;
                   2446:   if (queue->head == NULL)       /* If list is now empty, update tail. */
                   2447:     queue->tail = NULL;
                   2448:   DRIVER_UNLOCK
                   2449:   return(scbp);
                   2450: }
                   2451: 
                   2452: /*+F*************************************************************************
                   2453:  * Function:
                   2454:  *   scbq_remove
                   2455:  *
                   2456:  * Description:
                   2457:  *   Removes an SCB from the list.
                   2458:  *
                   2459:  *-F*************************************************************************/
                   2460: static inline void
                   2461: scbq_remove(volatile scb_queue_type *queue, struct aic7xxx_scb *scb)
                   2462: {
                   2463: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
                   2464:   unsigned long cpu_flags;
                   2465: #endif
                   2466: 
                   2467:   DRIVER_LOCK
                   2468:   if (queue->head == scb)
                   2469:   {
                   2470:     /* At beginning of queue, remove from head. */
                   2471:     scbq_remove_head(queue);
                   2472:   }
                   2473:   else
                   2474:   {
                   2475:     struct aic7xxx_scb *curscb = queue->head;
                   2476: 
                   2477:     /*
                   2478:      * Search until the next scb is the one we're looking for, or
                   2479:      * we run out of queue.
                   2480:      */
                   2481:     while ((curscb != NULL) && (curscb->q_next != scb))
                   2482:     {
                   2483:       curscb = curscb->q_next;
                   2484:     }
                   2485:     if (curscb != NULL)
                   2486:     {
                   2487:       /* Found it. */
                   2488:       curscb->q_next = scb->q_next;
                   2489:       if (scb->q_next == NULL)
                   2490:       {
                   2491:         /* Update the tail when removing the tail. */
                   2492:         queue->tail = curscb;
                   2493:       }
                   2494:     }
                   2495:   }
                   2496:   DRIVER_UNLOCK
                   2497: }
                   2498: 
                   2499: /*+F*************************************************************************
                   2500:  * Function:
                   2501:  *   scbq_insert_tail
                   2502:  *
                   2503:  * Description:
                   2504:  *   Add an SCB at the tail of the list.
                   2505:  *
                   2506:  *-F*************************************************************************/
                   2507: static inline void
                   2508: scbq_insert_tail(volatile scb_queue_type *queue, struct aic7xxx_scb *scb)
                   2509: {
                   2510: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
                   2511:   unsigned long cpu_flags;
                   2512: #endif
                   2513: 
                   2514:   DRIVER_LOCK
                   2515:   scb->q_next = NULL;
                   2516:   if (queue->tail != NULL)       /* Add the scb at the end of the list. */
                   2517:     queue->tail->q_next = scb;
                   2518:   queue->tail = scb;             /* Update the tail. */
                   2519:   if (queue->head == NULL)       /* If list was empty, update head. */
                   2520:     queue->head = queue->tail;
                   2521:   DRIVER_UNLOCK
                   2522: }
                   2523: 
                   2524: /*+F*************************************************************************
                   2525:  * Function:
                   2526:  *   aic7xxx_match_scb
                   2527:  *
                   2528:  * Description:
                   2529:  *   Checks to see if an scb matches the target/channel as specified.
                   2530:  *   If target is ALL_TARGETS (-1), then we're looking for any device
                   2531:  *   on the specified channel; this happens when a channel is going
                   2532:  *   to be reset and all devices on that channel must be aborted.
                   2533:  *-F*************************************************************************/
                   2534: static int
                   2535: aic7xxx_match_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb,
                   2536:     int target, int channel, int lun, unsigned char tag)
                   2537: {
                   2538:   int targ = (scb->hscb->target_channel_lun >> 4) & 0x0F;
                   2539:   int chan = (scb->hscb->target_channel_lun >> 3) & 0x01;
                   2540:   int slun = scb->hscb->target_channel_lun & 0x07;
                   2541:   int match;
                   2542: 
                   2543:   match = ((chan == channel) || (channel == ALL_CHANNELS));
                   2544:   if (match != 0)
                   2545:     match = ((targ == target) || (target == ALL_TARGETS));
                   2546:   if (match != 0)
                   2547:     match = ((lun == slun) || (lun == ALL_LUNS));
                   2548:   if (match != 0)
                   2549:     match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL));
                   2550: 
                   2551:   if (aic7xxx_verbose & (VERBOSE_ABORT_PROCESS | VERBOSE_RESET_PROCESS))
                   2552:   {
                   2553:     printk(KERN_INFO "(scsi%d:%d:%d:%d:tag%d) %s search criteria"
                   2554:       " (scsi%d:%d:%d:%d:tag%d)\n", p->host_no, CTL_OF_SCB(scb),
                   2555:       scb->hscb->tag, (match) ? "matches" : "doesn't match",
                   2556:       p->host_no, channel, target, lun, tag);
                   2557:   }
                   2558: 
                   2559:   return (match);
                   2560: }
                   2561: 
                   2562: /*+F*************************************************************************
                   2563:  * Function:
                   2564:  *   aic7xxx_add_curscb_to_free_list
                   2565:  *
                   2566:  * Description:
                   2567:  *   Adds the current scb (in SCBPTR) to the list of free SCBs.
                   2568:  *-F*************************************************************************/
                   2569: static void
                   2570: aic7xxx_add_curscb_to_free_list(struct aic7xxx_host *p)
                   2571: {
                   2572:   /*
                   2573:    * Invalidate the tag so that aic7xxx_find_scb doesn't think
                   2574:    * it's active
                   2575:    */
                   2576:   aic_outb(p, SCB_LIST_NULL, SCB_TAG);
                   2577:   aic_outb(p, 0, SCB_CONTROL);
                   2578: 
                   2579:   aic_outb(p, aic_inb(p, FREE_SCBH), SCB_NEXT);
                   2580:   aic_outb(p, aic_inb(p, SCBPTR), FREE_SCBH);
                   2581: }
                   2582: 
                   2583: /*+F*************************************************************************
                   2584:  * Function:
                   2585:  *   aic7xxx_rem_scb_from_disc_list
                   2586:  *
                   2587:  * Description:
                   2588:  *   Removes the current SCB from the disconnected list and adds it
                   2589:  *   to the free list.
                   2590:  *-F*************************************************************************/
                   2591: static unsigned char
                   2592: aic7xxx_rem_scb_from_disc_list(struct aic7xxx_host *p, unsigned char scbptr)
                   2593: {
                   2594:   unsigned char next;
                   2595:   unsigned char prev;
                   2596: 
                   2597:   aic_outb(p, scbptr, SCBPTR);
                   2598:   next = aic_inb(p, SCB_NEXT);
                   2599:   prev = aic_inb(p, SCB_PREV);
                   2600:   aic7xxx_add_curscb_to_free_list(p);
                   2601: 
                   2602:   if (prev != SCB_LIST_NULL)
                   2603:   {
                   2604:     aic_outb(p, prev, SCBPTR);
                   2605:     aic_outb(p, next, SCB_NEXT);
                   2606:   }
                   2607:   else
                   2608:   {
                   2609:     aic_outb(p, next, DISCONNECTED_SCBH);
                   2610:   }
                   2611: 
                   2612:   if (next != SCB_LIST_NULL)
                   2613:   {
                   2614:     aic_outb(p, next, SCBPTR);
                   2615:     aic_outb(p, prev, SCB_PREV);
                   2616:   }
                   2617:   return next;
                   2618: }
                   2619: 
                   2620: /*+F*************************************************************************
                   2621:  * Function:
                   2622:  *   aic7xxx_busy_target
                   2623:  *
                   2624:  * Description:
                   2625:  *   Set the specified target busy.
                   2626:  *-F*************************************************************************/
                   2627: static inline void
                   2628: aic7xxx_busy_target(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                   2629: {
                   2630:   p->untagged_scbs[scb->hscb->target_channel_lun] = scb->hscb->tag;
                   2631: }
                   2632: 
                   2633: /*+F*************************************************************************
                   2634:  * Function:
                   2635:  *   aic7xxx_index_busy_target
                   2636:  *
                   2637:  * Description:
                   2638:  *   Returns the index of the busy target, and optionally sets the
                   2639:  *   target inactive.
                   2640:  *-F*************************************************************************/
                   2641: static inline unsigned char
                   2642: aic7xxx_index_busy_target(struct aic7xxx_host *p, unsigned char tcl,
                   2643:     int unbusy)
                   2644: {
                   2645:   unsigned char busy_scbid;
                   2646: 
                   2647:   busy_scbid = p->untagged_scbs[tcl];
                   2648:   if (unbusy)
                   2649:   {
                   2650:     p->untagged_scbs[tcl] = SCB_LIST_NULL;
                   2651:   }
                   2652:   return (busy_scbid);
                   2653: }
                   2654: 
                   2655: /*+F*************************************************************************
                   2656:  * Function:
                   2657:  *   aic7xxx_find_scb
                   2658:  *
                   2659:  * Description:
                   2660:  *   Look through the SCB array of the card and attempt to find the
                   2661:  *   hardware SCB that corresponds to the passed in SCB.  Return
                   2662:  *   SCB_LIST_NULL if unsuccessful.  This routine assumes that the
                   2663:  *   card is already paused.
                   2664:  *-F*************************************************************************/
                   2665: static unsigned char
                   2666: aic7xxx_find_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                   2667: {
                   2668:   unsigned char saved_scbptr;
                   2669:   unsigned char curindex;
                   2670: 
                   2671:   saved_scbptr = aic_inb(p, SCBPTR);
                   2672:   curindex = 0;
                   2673:   for (curindex = 0; curindex < p->scb_data->maxhscbs; curindex++)
                   2674:   {
                   2675:     aic_outb(p, curindex, SCBPTR);
                   2676:     if (aic_inb(p, SCB_TAG) == scb->hscb->tag)
                   2677:     {
                   2678:       break;
                   2679:     }
                   2680:   }
                   2681:   aic_outb(p, saved_scbptr, SCBPTR);
                   2682:   if (curindex >= p->scb_data->maxhscbs)
                   2683:   {
                   2684:     curindex = SCB_LIST_NULL;
                   2685:   }
                   2686: 
                   2687:   return (curindex);
                   2688: }
                   2689: 
                   2690: /*+F*************************************************************************
                   2691:  * Function:
                   2692:  *   aic7xxx_allocate_scb
                   2693:  *
                   2694:  * Description:
                   2695:  *   Get an SCB from the free list or by allocating a new one.
                   2696:  *-F*************************************************************************/
                   2697: static int
                   2698: aic7xxx_allocate_scb(struct aic7xxx_host *p)
                   2699: {
                   2700:   struct aic7xxx_scb   *scbp = NULL;
                   2701:   int scb_size = sizeof(struct aic7xxx_scb) +
                   2702:                  sizeof (struct hw_scatterlist) * AIC7XXX_MAX_SG;
                   2703:   int i;
                   2704:   int step = PAGE_SIZE / 1024;
                   2705:   unsigned long scb_count = 0;
                   2706:   struct hw_scatterlist *hsgp;
                   2707:   struct aic7xxx_scb *scb_ap;
                   2708:   unsigned long temp;
                   2709: 
                   2710: 
                   2711:   if (p->scb_data->numscbs < p->scb_data->maxscbs)
                   2712:   {
                   2713:     /*
                   2714:      * Calculate the optimal number of SCBs to allocate.
                   2715:      *
                   2716:      * NOTE: This formula works because the sizeof(sg_array) is always
                   2717:      * 1024.  Therefore, scb_size * i would always be > PAGE_SIZE *
                   2718:      * (i/step).  The (i-1) allows the left hand side of the equation
                   2719:      * to grow into the right hand side to a point of near perfect
                   2720:      * efficiency since scb_size * (i -1) is growing slightly faster
                   2721:      * than the right hand side.  If the number of SG array elements
                   2722:      * is changed, this function may not be near so efficient any more.
                   2723:      */
                   2724:     for ( i=step;; i *= 2 )
                   2725:     {
                   2726:       if ( (scb_size * (i-1)) >= ( (PAGE_SIZE * (i/step)) - 64 ) )
                   2727:       {
                   2728:         i /= 2;
                   2729:         break;
                   2730:       }
                   2731:     }
                   2732:     scb_count = MIN( (i-1), p->scb_data->maxscbs - p->scb_data->numscbs);
                   2733:     scb_ap = (struct aic7xxx_scb *)kmalloc(scb_size * scb_count, GFP_ATOMIC);
                   2734:     if (scb_ap != NULL)
                   2735:     {
                   2736: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   2737:       if (aic7xxx_verbose > 0xffff)
                   2738:       {
                   2739:         if (p->scb_data->numscbs == 0)
                   2740:           printk(INFO_LEAD "Allocating initial %ld SCB structures.\n",
                   2741:             p->host_no, -1, -1, -1, scb_count);
                   2742:         else
                   2743:           printk(INFO_LEAD "Allocating %ld additional SCB structures.\n",
                   2744:             p->host_no, -1, -1, -1, scb_count);
                   2745:       }
                   2746: #endif
                   2747:       memset(scb_ap, 0, scb_count * scb_size);
                   2748:       temp = (unsigned long) &scb_ap[scb_count];
                   2749:       temp += 1023;
                   2750:       temp &= ~1023;
                   2751:       hsgp = (struct hw_scatterlist *)temp;
                   2752:       for (i=0; i < scb_count; i++)
                   2753:       {
                   2754:         scbp = &scb_ap[i];
                   2755:         scbp->hscb = &p->scb_data->hscbs[p->scb_data->numscbs];
                   2756:         scbp->sg_list = &hsgp[i * AIC7XXX_MAX_SG];
                   2757:         memset(scbp->hscb, 0, sizeof(struct aic7xxx_hwscb));
                   2758:         scbp->hscb->tag = p->scb_data->numscbs;
                   2759:         /*
                   2760:          * Place in the scb array; never is removed
                   2761:          */
                   2762:         p->scb_data->scb_array[p->scb_data->numscbs++] = scbp;
                   2763:         scbq_insert_head(&p->scb_data->free_scbs, scbp);
                   2764:       }
                   2765:       scbp->kmalloc_ptr = scb_ap;
                   2766:     }
                   2767:     else
                   2768:     {
                   2769:       return(0);
                   2770:     }
                   2771:   }
                   2772:   return(scb_count);
                   2773: }
                   2774: 
                   2775: /*+F*************************************************************************
                   2776:  * Function:
                   2777:  *   aic7xxx_queue_cmd_complete
                   2778:  *
                   2779:  * Description:
                   2780:  *   Due to race conditions present in the SCSI subsystem, it is easier
                   2781:  *   to queue completed commands, then call scsi_done() on them when
                   2782:  *   we're finished.  This function queues the completed commands.
                   2783:  *-F*************************************************************************/
                   2784: static void
                   2785: aic7xxx_queue_cmd_complete(struct aic7xxx_host *p, Scsi_Cmnd *cmd)
                   2786: {
                   2787:   cmd->host_scribble = (char *)p->completeq.head;
                   2788:   p->completeq.head = cmd;
                   2789: }
                   2790: 
                   2791: /*+F*************************************************************************
                   2792:  * Function:
                   2793:  *   aic7xxx_done_cmds_complete
                   2794:  *
                   2795:  * Description:
                   2796:  *   Process the completed command queue.
                   2797:  *-F*************************************************************************/
                   2798: static void
                   2799: aic7xxx_done_cmds_complete(struct aic7xxx_host *p)
                   2800: {
                   2801:   Scsi_Cmnd *cmd;
                   2802: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1.1.1.2 ! root     2803:   unsigned long cpu_flags = 0;
        !          2804: #endif
        !          2805: 
1.1       root     2806:   DRIVER_LOCK
                   2807:   while (p->completeq.head != NULL)
                   2808:   {
                   2809:     cmd = p->completeq.head;
                   2810:     p->completeq.head = (Scsi_Cmnd *)cmd->host_scribble;
                   2811:     cmd->host_scribble = NULL;
                   2812:     cmd->scsi_done(cmd);
                   2813:   }
                   2814:   DRIVER_UNLOCK
                   2815: }
                   2816: 
                   2817: /*+F*************************************************************************
                   2818:  * Function:
                   2819:  *   aic7xxx_free_scb
                   2820:  *
                   2821:  * Description:
                   2822:  *   Free the scb and insert into the free scb list.
                   2823:  *-F*************************************************************************/
                   2824: static void
                   2825: aic7xxx_free_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                   2826: {
                   2827: 
                   2828:   scb->flags = SCB_FREE;
                   2829:   scb->cmd = NULL;
                   2830:   scb->sg_count = 0;
                   2831:   scb->sg_length = 0;
                   2832:   scb->tag_action = 0;
                   2833:   scb->hscb->control = 0;
                   2834:   scb->hscb->target_status = 0;
                   2835:   scb->hscb->target_channel_lun = SCB_LIST_NULL;
                   2836: 
                   2837:   scbq_insert_head(&p->scb_data->free_scbs, scb);
                   2838: }
                   2839: 
                   2840: /*+F*************************************************************************
                   2841:  * Function:
                   2842:  *   aic7xxx_done
                   2843:  *
                   2844:  * Description:
                   2845:  *   Calls the higher level scsi done function and frees the scb.
                   2846:  *-F*************************************************************************/
                   2847: static void
                   2848: aic7xxx_done(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                   2849: {
                   2850:   Scsi_Cmnd *cmd = scb->cmd;
                   2851:   int tindex = TARGET_INDEX(cmd);
                   2852:   struct aic7xxx_scb *scbp;
                   2853:   unsigned char queue_depth;
                   2854: 
                   2855:   if (scb->flags & SCB_RECOVERY_SCB)
                   2856:   {
                   2857:     p->flags &= ~AHC_ABORT_PENDING;
                   2858:   }
                   2859:   if (scb->flags & SCB_RESET)
                   2860:   {
                   2861:       cmd->result = (DID_RESET << 16) | (cmd->result & 0xffff);
                   2862:   }
                   2863:   else if (scb->flags & SCB_ABORT)
                   2864:   {
                   2865:       cmd->result = (DID_RESET << 16) | (cmd->result & 0xffff);
                   2866:   }
                   2867:   else if (!(p->dev_flags[tindex] & DEVICE_SCANNED))
                   2868:   {
                   2869:     if ( (cmd->cmnd[0] == INQUIRY) && (cmd->result == DID_OK) )
                   2870:     {
                   2871:       char *buffer;
1.1.1.2 ! root     2872: 
        !          2873:       p->dev_flags[tindex] |= DEVICE_PRESENT;
1.1       root     2874:       if(cmd->use_sg)
                   2875:       {
                   2876:         struct scatterlist *sg;
                   2877: 
                   2878:         sg = (struct scatterlist *)cmd->request_buffer;
                   2879:         buffer = (char *)sg[0].address;
                   2880:       }
                   2881:       else
                   2882:       {
                   2883:         buffer = (char *)cmd->request_buffer;
                   2884:       }
                   2885: #define WIDE_INQUIRY_BITS 0x60
                   2886: #define SYNC_INQUIRY_BITS 0x10
                   2887:       if ( (buffer[7] & WIDE_INQUIRY_BITS) &&
                   2888:            (p->features & AHC_WIDE) )
                   2889:       {
                   2890:         p->needwdtr |= (1<<tindex);
                   2891:         p->needwdtr_copy |= (1<<tindex);
                   2892:         if ( (p->flags & AHC_SEEPROM_FOUND) &&
                   2893:              (p->transinfo[tindex].user_width != MSG_EXT_WDTR_BUS_16_BIT) )
                   2894:           p->transinfo[tindex].goal_width = MSG_EXT_WDTR_BUS_8_BIT;
                   2895:         else
                   2896:           p->transinfo[tindex].goal_width = MSG_EXT_WDTR_BUS_16_BIT;
                   2897:       }
                   2898:       else
                   2899:       {
                   2900:         p->needwdtr &= ~(1<<tindex);
                   2901:         p->needwdtr_copy &= ~(1<<tindex);
                   2902:         pause_sequencer(p);
                   2903:         aic7xxx_set_width(p, cmd->target, cmd->channel, cmd->lun,
                   2904:                           MSG_EXT_WDTR_BUS_8_BIT, (AHC_TRANS_ACTIVE |
                   2905:                                                    AHC_TRANS_GOAL |
                   2906:                                                    AHC_TRANS_CUR) );
                   2907:         unpause_sequencer(p, FALSE);
                   2908:       }
                   2909:       if (buffer[7] & SYNC_INQUIRY_BITS)
                   2910:       {
                   2911:         p->needsdtr |= (1<<tindex);
                   2912:         p->needsdtr_copy |= (1<<tindex);
                   2913: 
                   2914:         if (p->flags & AHC_SEEPROM_FOUND)
1.1.1.2 ! root     2915:         {
1.1       root     2916:           p->transinfo[tindex].goal_period = p->transinfo[tindex].user_period;
1.1.1.2 ! root     2917:           p->transinfo[tindex].goal_offset = p->transinfo[tindex].user_offset;
        !          2918:         }
1.1       root     2919:         else
1.1.1.2 ! root     2920:         {
        !          2921:           if (p->features & AHC_ULTRA2)
        !          2922:           {
        !          2923:             p->transinfo[tindex].goal_period =
        !          2924:               aic7xxx_syncrates[AHC_SYNCRATE_ULTRA2].period;
        !          2925:           }
        !          2926:           else if (p->features & AHC_ULTRA)
        !          2927:           {
        !          2928:             p->transinfo[tindex].goal_period =
        !          2929:               aic7xxx_syncrates[AHC_SYNCRATE_ULTRA].period;
        !          2930:           }
        !          2931:           else
        !          2932:           {
        !          2933:             p->transinfo[tindex].goal_period =
        !          2934:               aic7xxx_syncrates[AHC_SYNCRATE_FAST].period;
        !          2935:           }
        !          2936:           if (p->features & AHC_ULTRA2)
        !          2937:             p->transinfo[tindex].goal_offset = MAX_OFFSET_ULTRA2;
        !          2938:           else if (p->transinfo[tindex].goal_width == MSG_EXT_WDTR_BUS_16_BIT)
        !          2939:             p->transinfo[tindex].goal_offset = MAX_OFFSET_16BIT;
        !          2940:           else
        !          2941:             p->transinfo[tindex].goal_offset = MAX_OFFSET_8BIT;
        !          2942:         }
1.1       root     2943:       }
                   2944:       else
                   2945:       {
                   2946:         p->needsdtr &= ~(1<<tindex);
                   2947:         p->needsdtr_copy &= ~(1<<tindex);
                   2948:         p->transinfo[tindex].goal_period = 0;
                   2949:         p->transinfo[tindex].goal_offset = 0;
                   2950:       }
                   2951:       p->dev_flags[tindex] |= DEVICE_SCANNED;
                   2952:       p->dev_flags[tindex] |= DEVICE_PRINT_WDTR | DEVICE_PRINT_SDTR;
                   2953: #undef WIDE_INQUIRY_BITS
                   2954: #undef SYNC_INQUIRY_BITS
                   2955:     }
                   2956:   }
                   2957:   else if ((scb->flags & (SCB_MSGOUT_WDTR | SCB_MSGOUT_SDTR)) != 0)
                   2958:   {
                   2959:     unsigned short mask;
                   2960:     int message_error = FALSE;
                   2961: 
                   2962:     mask = 0x01 << tindex;
1.1.1.2 ! root     2963: 
1.1       root     2964:     /*
                   2965:      * Check to see if we get an invalid message or a message error
                   2966:      * after failing to negotiate a wide or sync transfer message.
                   2967:      */
1.1.1.2 ! root     2968:     if ((scb->flags & SCB_SENSE) &&
1.1       root     2969:           ((scb->cmd->sense_buffer[12] == 0x43) ||  /* INVALID_MESSAGE */
                   2970:           (scb->cmd->sense_buffer[12] == 0x49))) /* MESSAGE_ERROR  */
                   2971:     {
                   2972:       message_error = TRUE;
                   2973:     }
                   2974: 
                   2975:     if (scb->flags & SCB_MSGOUT_WDTR)
                   2976:     {
                   2977:       p->wdtr_pending &= ~mask;
                   2978:       if (message_error)
                   2979:       {
                   2980:         if ( (aic7xxx_verbose & VERBOSE_NEGOTIATION2) &&
                   2981:              (p->dev_flags[tindex] & DEVICE_PRINT_WDTR) )
                   2982:         {
                   2983:           printk(INFO_LEAD "Device failed to complete Wide Negotiation "
                   2984:             "processing and\n", p->host_no, CTL_OF_SCB(scb));
                   2985:           printk(INFO_LEAD "returned a sense error code for invalid message, "
                   2986:             "disabling future\n", p->host_no, CTL_OF_SCB(scb));
                   2987:           printk(INFO_LEAD "Wide negotiation to this device.\n", p->host_no,
                   2988:             CTL_OF_SCB(scb));
                   2989:           p->dev_flags[tindex] &= ~DEVICE_PRINT_WDTR;
                   2990:         }
                   2991:         p->needwdtr &= ~mask;
                   2992:         p->needwdtr_copy &= ~mask;
                   2993:       }
                   2994:     }
                   2995:     if (scb->flags & SCB_MSGOUT_SDTR)
                   2996:     {
                   2997:       p->sdtr_pending &= ~mask;
                   2998:       if (message_error)
                   2999:       {
                   3000:         if ( (aic7xxx_verbose & VERBOSE_NEGOTIATION2) &&
                   3001:              (p->dev_flags[tindex] & DEVICE_PRINT_SDTR) )
                   3002:         {
                   3003:           printk(INFO_LEAD "Device failed to complete Sync Negotiation "
                   3004:             "processing and\n", p->host_no, CTL_OF_SCB(scb));
                   3005:           printk(INFO_LEAD "returned a sense error code for invalid message, "
                   3006:             "disabling future\n", p->host_no, CTL_OF_SCB(scb));
                   3007:           printk(INFO_LEAD "Sync negotiation to this device.\n", p->host_no,
                   3008:             CTL_OF_SCB(scb));
                   3009:           p->dev_flags[tindex] &= ~DEVICE_PRINT_SDTR;
                   3010:         }
                   3011:         p->needsdtr &= ~mask;
                   3012:         p->needsdtr_copy &= ~mask;
                   3013:       }
                   3014:     }
                   3015:   }
                   3016:   queue_depth = p->dev_temp_queue_depth[tindex];
                   3017:   if (queue_depth >= p->dev_active_cmds[tindex])
                   3018:   {
                   3019:     scbp = scbq_remove_head(&p->delayed_scbs[tindex]);
                   3020:     if (scbp)
                   3021:     {
                   3022:       if (queue_depth == 1)
                   3023:       {
                   3024:         /*
                   3025:          * Give extra preference to untagged devices, such as CD-R devices
                   3026:          * This makes it more likely that a drive *won't* stuff up while
                   3027:          * waiting on data at a critical time, such as CD-R writing and
                   3028:          * audio CD ripping operations.  Should also benefit tape drives.
                   3029:          */
                   3030:         scbq_insert_head(&p->waiting_scbs, scbp);
                   3031:       }
                   3032:       else
                   3033:       {
                   3034:         scbq_insert_tail(&p->waiting_scbs, scbp);
                   3035:       }
                   3036: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   3037:       if (aic7xxx_verbose > 0xffff)
                   3038:         printk(INFO_LEAD "Moving SCB from delayed to waiting queue.\n",
                   3039:                p->host_no, CTL_OF_SCB(scbp));
                   3040: #endif
                   3041:       if (queue_depth > p->dev_active_cmds[tindex])
                   3042:       {
                   3043:         scbp = scbq_remove_head(&p->delayed_scbs[tindex]);
                   3044:         if (scbp)
                   3045:           scbq_insert_tail(&p->waiting_scbs, scbp);
                   3046:       }
                   3047:     }
                   3048:   }
                   3049:   if ( !(scb->tag_action) && (p->tagenable & (1<<tindex)) )
                   3050:   {
                   3051:     p->dev_temp_queue_depth[tindex] = p->dev_max_queue_depth[tindex];
                   3052:   }
                   3053:   p->dev_active_cmds[tindex]--;
                   3054:   p->activescbs--;
                   3055: 
                   3056:   /*
                   3057:    * If this was an untagged I/O, unbusy the target so the sequencer won't
                   3058:    * mistake things later
                   3059:    */
                   3060:   if (aic7xxx_index_busy_target(p, scb->hscb->target_channel_lun, FALSE) ==
                   3061:       scb->hscb->tag)
                   3062:   {
                   3063:     aic7xxx_index_busy_target(p, scb->hscb->target_channel_lun, TRUE);
                   3064:   }
                   3065: 
                   3066:   {
                   3067:     int actual;
                   3068: 
                   3069:     /*
                   3070:      * XXX: we should actually know how much actually transferred
                   3071:      * XXX: for each command, but apparently that's too difficult.
1.1.1.2 ! root     3072:      *
1.1       root     3073:      * We set a lower limit of 512 bytes on the transfer length.  We
                   3074:      * ignore anything less than this because we don't have a real
                   3075:      * reason to count it.  Read/Writes to tapes are usually about 20K
                   3076:      * and disks are a minimum of 512 bytes unless you want to count
                   3077:      * non-read/write commands (such as TEST_UNIT_READY) which we don't
                   3078:      */
                   3079:     actual = scb->sg_length;
                   3080:     if ((actual >= 512) && (((cmd->result >> 16) & 0xf) == DID_OK))
                   3081:     {
                   3082:       struct aic7xxx_xferstats *sp;
                   3083: #ifdef AIC7XXX_PROC_STATS
                   3084:       long *ptr;
                   3085:       int x;
                   3086: #endif /* AIC7XXX_PROC_STATS */
                   3087: 
1.1.1.2 ! root     3088:       sp = &p->stats[TARGET_INDEX(cmd)];
1.1       root     3089: 
                   3090:       /*
                   3091:        * For block devices, cmd->request.cmd is always == either READ or
                   3092:        * WRITE.  For character devices, this isn't always set properly, so
                   3093:        * we check data_cmnd[0].  This catches the conditions for st.c, but
                   3094:        * I'm still not sure if request.cmd is valid for sg devices.
                   3095:        */
                   3096:       if ( (cmd->request.cmd == WRITE) || (cmd->data_cmnd[0] == WRITE_6) ||
                   3097:            (cmd->data_cmnd[0] == WRITE_FILEMARKS) )
                   3098:       {
                   3099:         sp->w_total++;
1.1.1.2 ! root     3100: #ifdef AIC7XXX_VERBOSE_DEBUGGING
        !          3101:         if ( (sp->w_total > 16) && (aic7xxx_verbose > 0xffff) )
        !          3102:           aic7xxx_verbose &= 0xffff;
        !          3103: #endif
1.1       root     3104: #ifdef AIC7XXX_PROC_STATS
                   3105:         ptr = sp->w_bins;
                   3106: #endif /* AIC7XXX_PROC_STATS */
                   3107:       }
                   3108:       else
                   3109:       {
                   3110:         sp->r_total++;
1.1.1.2 ! root     3111: #ifdef AIC7XXX_VERBOSE_DEBUGGING
        !          3112:         if ( (sp->r_total > 16) && (aic7xxx_verbose > 0xffff) )
        !          3113:           aic7xxx_verbose &= 0xffff;
        !          3114: #endif
1.1       root     3115: #ifdef AIC7XXX_PROC_STATS
                   3116:         ptr = sp->r_bins;
                   3117: #endif /* AIC7XXX_PROC_STATS */
                   3118:       }
                   3119: #ifdef AIC7XXX_PROC_STATS
1.1.1.2 ! root     3120:       x = -10;
        !          3121:       while(actual)
1.1       root     3122:       {
1.1.1.2 ! root     3123:         actual >>= 1;
        !          3124:         x++;
1.1       root     3125:       }
1.1.1.2 ! root     3126:       if (x < 0)
1.1       root     3127:       {
1.1.1.2 ! root     3128:         ptr[0]++;
        !          3129:       }
        !          3130:       else if (x > 7)
        !          3131:       {
        !          3132:         ptr[7]++;
        !          3133:       }
        !          3134:       else
        !          3135:       {
        !          3136:         ptr[x]++;
1.1       root     3137:       }
                   3138: #endif /* AIC7XXX_PROC_STATS */
                   3139:     }
                   3140:   }
                   3141:   aic7xxx_free_scb(p, scb);
                   3142:   aic7xxx_queue_cmd_complete(p, cmd);
                   3143: 
                   3144: }
                   3145: 
                   3146: /*+F*************************************************************************
                   3147:  * Function:
                   3148:  *   aic7xxx_run_done_queue
                   3149:  *
                   3150:  * Description:
                   3151:  *   Calls the aic7xxx_done() for the Scsi_Cmnd of each scb in the
                   3152:  *   aborted list, and adds each scb to the free list.  If complete
                   3153:  *   is TRUE, we also process the commands complete list.
                   3154:  *-F*************************************************************************/
                   3155: static void
                   3156: aic7xxx_run_done_queue(struct aic7xxx_host *p, /*complete*/ int complete)
                   3157: {
                   3158:   struct aic7xxx_scb *scb;
                   3159:   int i, found = 0;
                   3160: 
                   3161:   for (i = 0; i < p->scb_data->numscbs; i++)
                   3162:   {
                   3163:     scb = p->scb_data->scb_array[i];
                   3164:     if (scb->flags & SCB_QUEUED_FOR_DONE)
                   3165:     {
                   3166:       if (aic7xxx_verbose & (VERBOSE_ABORT_PROCESS | VERBOSE_RESET_PROCESS))
                   3167:         printk(INFO_LEAD "Aborting scb %d\n",
                   3168:              p->host_no, CTL_OF_SCB(scb), scb->hscb->tag);
                   3169:       found++;
                   3170:       aic7xxx_done(p, scb);
                   3171:     }
                   3172:   }
                   3173:   if (aic7xxx_verbose & (VERBOSE_ABORT_RETURN | VERBOSE_RESET_RETURN))
                   3174:   {
                   3175:     printk(INFO_LEAD "%d commands found and queued for "
                   3176:         "completion.\n", p->host_no, -1, -1, -1, found);
                   3177:   }
                   3178:   if (complete)
                   3179:   {
                   3180:     aic7xxx_done_cmds_complete(p);
                   3181:   }
                   3182: }
                   3183: 
                   3184: /*+F*************************************************************************
                   3185:  * Function:
                   3186:  *   aic7xxx_abort_waiting_scb
                   3187:  *
                   3188:  * Description:
                   3189:  *   Manipulate the waiting for selection list and return the
                   3190:  *   scb that follows the one that we remove.
                   3191:  *-F*************************************************************************/
                   3192: static unsigned char
                   3193: aic7xxx_abort_waiting_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb,
                   3194:     unsigned char scbpos, unsigned char prev)
                   3195: {
                   3196:   unsigned char curscb, next;
                   3197: 
                   3198:   /*
                   3199:    * Select the SCB we want to abort and pull the next pointer out of it.
                   3200:    */
                   3201:   curscb = aic_inb(p, SCBPTR);
                   3202:   aic_outb(p, scbpos, SCBPTR);
                   3203:   next = aic_inb(p, SCB_NEXT);
                   3204: 
                   3205:   aic7xxx_add_curscb_to_free_list(p);
                   3206: 
                   3207:   /*
                   3208:    * Update the waiting list
                   3209:    */
                   3210:   if (prev == SCB_LIST_NULL)
                   3211:   {
                   3212:     /*
                   3213:      * First in the list
                   3214:      */
                   3215:     aic_outb(p, next, WAITING_SCBH);
                   3216:   }
                   3217:   else
                   3218:   {
                   3219:     /*
                   3220:      * Select the scb that pointed to us and update its next pointer.
                   3221:      */
                   3222:     aic_outb(p, prev, SCBPTR);
                   3223:     aic_outb(p, next, SCB_NEXT);
                   3224:   }
                   3225:   /*
                   3226:    * Point us back at the original scb position and inform the SCSI
                   3227:    * system that the command has been aborted.
                   3228:    */
                   3229:   aic_outb(p, curscb, SCBPTR);
                   3230:   return (next);
                   3231: }
                   3232: 
                   3233: /*+F*************************************************************************
                   3234:  * Function:
                   3235:  *   aic7xxx_search_qinfifo
                   3236:  *
                   3237:  * Description:
                   3238:  *   Search the queue-in FIFO for matching SCBs and conditionally
                   3239:  *   requeue.  Returns the number of matching SCBs.
                   3240:  *-F*************************************************************************/
                   3241: static int
                   3242: aic7xxx_search_qinfifo(struct aic7xxx_host *p, int target, int channel,
                   3243:     int lun, unsigned char tag, int flags, int requeue,
                   3244:     volatile scb_queue_type *queue)
                   3245: {
                   3246:   int      found;
                   3247:   unsigned char qinpos, qintail;
                   3248:   struct aic7xxx_scb *scbp;
                   3249: 
                   3250:   found = 0;
                   3251:   qinpos = aic_inb(p, QINPOS);
                   3252:   qintail = p->qinfifonext;
                   3253: 
                   3254:   p->qinfifonext = qinpos;
                   3255: 
                   3256:   while (qinpos != qintail)
                   3257:   {
                   3258:     scbp = p->scb_data->scb_array[p->qinfifo[qinpos++]];
                   3259:     if (aic7xxx_match_scb(p, scbp, target, channel, lun, tag))
                   3260:     {
                   3261:        /*
                   3262:         * We found an scb that needs to be removed.
                   3263:         */
                   3264:        if (requeue && (queue != NULL))
                   3265:        {
                   3266:          if (scbp->flags & SCB_WAITINGQ)
                   3267:          {
                   3268:            scbq_remove(queue, scbp);
                   3269:            scbq_remove(&p->waiting_scbs, scbp);
                   3270:            scbq_remove(&p->delayed_scbs[TARGET_INDEX(scbp->cmd)], scbp);
                   3271:            p->dev_active_cmds[TARGET_INDEX(scbp->cmd)]++;
                   3272:            p->activescbs++;
                   3273:          }
                   3274:          scbq_insert_tail(queue, scbp);
                   3275:          p->dev_active_cmds[TARGET_INDEX(scbp->cmd)]--;
                   3276:          p->activescbs--;
                   3277:          scbp->flags |= SCB_WAITINGQ;
                   3278:          if ( !(scbp->tag_action & TAG_ENB) )
                   3279:          {
                   3280:            aic7xxx_index_busy_target(p, scbp->hscb->target_channel_lun,
                   3281:              TRUE);
                   3282:          }
                   3283:        }
                   3284:        else if (requeue)
                   3285:        {
                   3286:          p->qinfifo[p->qinfifonext++] = scbp->hscb->tag;
                   3287:        }
                   3288:        else
                   3289:        {
                   3290:         /*
                   3291:          * Preserve any SCB_RECOVERY_SCB flags on this scb then set the
                   3292:          * flags we were called with, presumeably so aic7xxx_run_done_queue
                   3293:          * can find this scb
                   3294:          */
                   3295:          scbp->flags = flags | (scbp->flags & SCB_RECOVERY_SCB);
                   3296:          if (aic7xxx_index_busy_target(p, scbp->hscb->target_channel_lun,
                   3297:                                        FALSE) == scbp->hscb->tag)
                   3298:          {
                   3299:            aic7xxx_index_busy_target(p, scbp->hscb->target_channel_lun,
                   3300:              TRUE);
                   3301:          }
                   3302:        }
                   3303:        found++;
                   3304:     }
                   3305:     else
                   3306:     {
                   3307:       p->qinfifo[p->qinfifonext++] = scbp->hscb->tag;
                   3308:     }
                   3309:   }
                   3310:   /*
                   3311:    * Now that we've done the work, clear out any left over commands in the
                   3312:    * qinfifo and update the KERNEL_QINPOS down on the card.
                   3313:    *
                   3314:    *  NOTE: This routine expect the sequencer to already be paused when
                   3315:    *        it is run....make sure it's that way!
                   3316:    */
                   3317:   qinpos = p->qinfifonext;
                   3318:   while(qinpos != qintail)
                   3319:   {
                   3320:     p->qinfifo[qinpos++] = SCB_LIST_NULL;
                   3321:   }
                   3322:   if (p->features & AHC_QUEUE_REGS)
                   3323:     aic_outb(p, p->qinfifonext, HNSCB_QOFF);
                   3324:   else
                   3325:     aic_outb(p, p->qinfifonext, KERNEL_QINPOS);
                   3326: 
                   3327:   return (found);
                   3328: }
                   3329: 
                   3330: /*+F*************************************************************************
                   3331:  * Function:
                   3332:  *   aic7xxx_scb_on_qoutfifo
                   3333:  *
                   3334:  * Description:
                   3335:  *   Is the scb that was passed to us currently on the qoutfifo?
                   3336:  *-F*************************************************************************/
                   3337: static int
                   3338: aic7xxx_scb_on_qoutfifo(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                   3339: {
                   3340:   int i=0;
                   3341: 
                   3342:   while(p->qoutfifo[(p->qoutfifonext + i) & 0xff ] != SCB_LIST_NULL)
                   3343:   {
                   3344:     if(p->qoutfifo[(p->qoutfifonext + i) & 0xff ] == scb->hscb->tag)
                   3345:       return TRUE;
                   3346:     else
                   3347:       i++;
                   3348:   }
                   3349:   return FALSE;
                   3350: }
                   3351: 
                   3352: 
                   3353: /*+F*************************************************************************
                   3354:  * Function:
                   3355:  *   aic7xxx_reset_device
                   3356:  *
                   3357:  * Description:
                   3358:  *   The device at the given target/channel has been reset.  Abort
                   3359:  *   all active and queued scbs for that target/channel.  This function
                   3360:  *   need not worry about linked next pointers because if was a MSG_ABORT_TAG
                   3361:  *   then we had a tagged command (no linked next), if it was MSG_ABORT or
                   3362:  *   MSG_BUS_DEV_RESET then the device won't know about any commands any more
                   3363:  *   and no busy commands will exist, and if it was a bus reset, then nothing
                   3364:  *   knows about any linked next commands any more.  In all cases, we don't
                   3365:  *   need to worry about the linked next or busy scb, we just need to clear
                   3366:  *   them.
                   3367:  *-F*************************************************************************/
                   3368: static void
                   3369: aic7xxx_reset_device(struct aic7xxx_host *p, int target, int channel,
                   3370:                      int lun, unsigned char tag)
                   3371: {
                   3372:   struct aic7xxx_scb *scbp;
                   3373:   unsigned char active_scb, tcl;
                   3374:   int i = 0, j, init_lists = FALSE;
                   3375: 
                   3376:   /*
                   3377:    * Restore this when we're done
                   3378:    */
                   3379:   active_scb = aic_inb(p, SCBPTR);
                   3380: 
                   3381:   if (aic7xxx_verbose & (VERBOSE_RESET_PROCESS | VERBOSE_ABORT_PROCESS))
                   3382:     printk(INFO_LEAD "Reset device, active_scb %d\n",
                   3383:          p->host_no, channel, target, lun, active_scb);
                   3384:   /*
                   3385:    * Deal with the busy target and linked next issues.
                   3386:    */
                   3387:   {
                   3388:     int min_target, max_target;
                   3389:     struct aic7xxx_scb *scbp, *prev_scbp;
                   3390: 
                   3391:     /* Make all targets 'relative' to bus A. */
                   3392:     if (target == ALL_TARGETS)
                   3393:     {
                   3394:       switch (channel)
                   3395:       {
                   3396:         case 0:
                   3397:                  min_target = 0;
                   3398:                  max_target = (p->features & AHC_WIDE) ? 15 : 7;
                   3399:                  break;
                   3400:         case 1:
                   3401:                  min_target = 8;
                   3402:                  max_target = 15;
                   3403:                  break;
                   3404:         case ALL_CHANNELS:
                   3405:         default:
                   3406:                  min_target = 0;
                   3407:                  max_target = (p->features & (AHC_TWIN|AHC_WIDE)) ? 15 : 7;
                   3408:                  break;
                   3409:       }
                   3410:     }
                   3411:     else
1.1.1.2 ! root     3412:     {
1.1       root     3413:       min_target = target | (channel << 3);
                   3414:       max_target = min_target;
                   3415:     }
                   3416: 
                   3417: 
                   3418:     for (i = min_target; i <= max_target; i++)
                   3419:     {
1.1.1.2 ! root     3420:       if ( i == p->scsi_id )
        !          3421:       {
        !          3422:         continue;
        !          3423:       }
1.1       root     3424:       if (aic7xxx_verbose & (VERBOSE_ABORT_PROCESS | VERBOSE_RESET_PROCESS))
                   3425:         printk(INFO_LEAD "Cleaning up status information "
                   3426:           "and delayed_scbs.\n", p->host_no, channel, i, lun);
                   3427:       p->dev_flags[i] &= ~BUS_DEVICE_RESET_PENDING;
                   3428:       if ( tag == SCB_LIST_NULL )
                   3429:       {
1.1.1.2 ! root     3430:         p->dev_flags[i] |= DEVICE_PRINT_WDTR | DEVICE_PRINT_SDTR |
        !          3431:                            DEVICE_RESET_DELAY;
        !          3432:         p->dev_expires[i] = jiffies + (4 * HZ);
        !          3433:         p->dev_timer_active |= (0x01 << i);
1.1       root     3434:         p->dev_last_queue_full_count[i] = 0;
                   3435:         p->dev_last_queue_full[i] = 0;
                   3436:         p->dev_temp_queue_depth[i] =
                   3437:           p->dev_max_queue_depth[i];
                   3438:       }
                   3439:       for(j=0; j<MAX_LUNS; j++)
                   3440:       {
                   3441:         if (channel == 1)
                   3442:           tcl = ((i << 4) & 0x70) | (channel << 3) | j;
                   3443:         else
                   3444:           tcl = (i << 4) | (channel << 3) | j;
                   3445:         if ( (aic7xxx_index_busy_target(p, tcl, FALSE) == tag) ||
                   3446:              (tag == SCB_LIST_NULL) )
                   3447:           aic7xxx_index_busy_target(p, tcl, /* unbusy */ TRUE);
                   3448:       }
1.1.1.2 ! root     3449:       j = 0;
        !          3450:       prev_scbp = NULL;
1.1       root     3451:       scbp = p->delayed_scbs[i].head;
                   3452:       while ( (scbp != NULL) && (j++ <= (p->scb_data->numscbs + 1)) )
                   3453:       {
                   3454:         prev_scbp = scbp;
                   3455:         scbp = scbp->q_next;
                   3456:         if ( prev_scbp == scbp )
                   3457:         {
                   3458:           if (aic7xxx_verbose & (VERBOSE_ABORT | VERBOSE_RESET))
                   3459:             printk(WARN_LEAD "Yikes!! scb->q_next == scb "
                   3460:               "in the delayed_scbs queue!\n", p->host_no, channel, i, lun);
                   3461:           scbp = NULL;
                   3462:           prev_scbp->q_next = NULL;
                   3463:           p->delayed_scbs[i].tail = prev_scbp;
                   3464:         }
                   3465:         if (aic7xxx_match_scb(p, prev_scbp, target, channel, lun, tag))
                   3466:         {
                   3467:           scbq_remove(&p->delayed_scbs[i], prev_scbp);
                   3468:           if (prev_scbp->flags & SCB_WAITINGQ)
                   3469:           {
                   3470:             p->dev_active_cmds[i]++;
                   3471:             p->activescbs++;
                   3472:           }
                   3473:           prev_scbp->flags &= ~(SCB_ACTIVE | SCB_WAITINGQ);
                   3474:           prev_scbp->flags |= SCB_RESET | SCB_QUEUED_FOR_DONE;
                   3475:         }
                   3476:       }
                   3477:       if ( j > (p->scb_data->maxscbs + 1) )
                   3478:       {
                   3479:         if (aic7xxx_verbose & (VERBOSE_ABORT | VERBOSE_RESET))
                   3480:           printk(WARN_LEAD "Yikes!! There's a loop in the "
                   3481:             "delayed_scbs queue!\n", p->host_no, channel, i, lun);
                   3482:         scbq_init(&p->delayed_scbs[i]);
                   3483:       }
1.1.1.2 ! root     3484:       if ( !(p->dev_timer_active & (0x01 << MAX_TARGETS)) ||
        !          3485:             time_after_eq(p->dev_timer.expires, p->dev_expires[i]) )
1.1       root     3486:       {
1.1.1.2 ! root     3487:         del_timer(&p->dev_timer);
        !          3488:         p->dev_timer.expires = p->dev_expires[i];
        !          3489:         add_timer(&p->dev_timer);
        !          3490:         p->dev_timer_active |= (0x01 << MAX_TARGETS);
1.1       root     3491:       }
                   3492:     }
                   3493:   }
                   3494: 
                   3495:   if (aic7xxx_verbose & (VERBOSE_ABORT_PROCESS | VERBOSE_RESET_PROCESS))
                   3496:     printk(INFO_LEAD "Cleaning QINFIFO.\n", p->host_no, channel, target, lun );
                   3497:   aic7xxx_search_qinfifo(p, target, channel, lun, tag,
                   3498:       SCB_RESET | SCB_QUEUED_FOR_DONE, /* requeue */ FALSE, NULL);
                   3499: 
                   3500: /*
                   3501:  *  Search the waiting_scbs queue for matches, this catches any SCB_QUEUED
                   3502:  *  ABORT/RESET commands.
                   3503:  */
                   3504:   if (aic7xxx_verbose & (VERBOSE_ABORT_PROCESS | VERBOSE_RESET_PROCESS))
                   3505:     printk(INFO_LEAD "Cleaning waiting_scbs.\n", p->host_no, channel,
                   3506:       target, lun );
                   3507:   {
                   3508:     struct aic7xxx_scb *scbp, *prev_scbp;
                   3509: 
1.1.1.2 ! root     3510:     j = 0;
        !          3511:     prev_scbp = NULL;
1.1       root     3512:     scbp = p->waiting_scbs.head;
                   3513:     while ( (scbp != NULL) && (j++ <= (p->scb_data->numscbs + 1)) )
                   3514:     {
                   3515:       prev_scbp = scbp;
                   3516:       scbp = scbp->q_next;
                   3517:       if ( prev_scbp == scbp )
                   3518:       {
                   3519:         if (aic7xxx_verbose & (VERBOSE_ABORT | VERBOSE_RESET))
                   3520:           printk(WARN_LEAD "Yikes!! scb->q_next == scb "
                   3521:             "in the waiting_scbs queue!\n", p->host_no, CTL_OF_SCB(scbp));
                   3522:         scbp = NULL;
                   3523:         prev_scbp->q_next = NULL;
                   3524:         p->waiting_scbs.tail = prev_scbp;
                   3525:       }
                   3526:       if (aic7xxx_match_scb(p, prev_scbp, target, channel, lun, tag))
                   3527:       {
                   3528:         scbq_remove(&p->waiting_scbs, prev_scbp);
                   3529:         if (prev_scbp->flags & SCB_WAITINGQ)
                   3530:         {
                   3531:           p->dev_active_cmds[TARGET_INDEX(prev_scbp->cmd)]++;
                   3532:           p->activescbs++;
                   3533:         }
                   3534:         prev_scbp->flags &= ~(SCB_ACTIVE | SCB_WAITINGQ);
                   3535:         prev_scbp->flags |= SCB_RESET | SCB_QUEUED_FOR_DONE;
                   3536:       }
                   3537:     }
                   3538:     if ( j > (p->scb_data->maxscbs + 1) )
                   3539:     {
                   3540:       if (aic7xxx_verbose & (VERBOSE_ABORT | VERBOSE_RESET))
                   3541:         printk(WARN_LEAD "Yikes!! There's a loop in the "
                   3542:           "waiting_scbs queue!\n", p->host_no, channel, target, lun);
                   3543:       scbq_init(&p->waiting_scbs);
                   3544:     }
                   3545:   }
                   3546: 
                   3547: 
                   3548:   /*
                   3549:    * Search waiting for selection list.
                   3550:    */
                   3551:   if (aic7xxx_verbose & (VERBOSE_ABORT_PROCESS | VERBOSE_RESET_PROCESS))
                   3552:     printk(INFO_LEAD "Cleaning waiting for selection "
                   3553:       "list.\n", p->host_no, channel, target, lun);
                   3554:   {
                   3555:     unsigned char next, prev, scb_index;
                   3556: 
                   3557:     next = aic_inb(p, WAITING_SCBH);  /* Start at head of list. */
                   3558:     prev = SCB_LIST_NULL;
                   3559:     j = 0;
                   3560:     while ( (next != SCB_LIST_NULL) && (j++ <= (p->scb_data->maxscbs + 1)) )
                   3561:     {
                   3562:       aic_outb(p, next, SCBPTR);
                   3563:       scb_index = aic_inb(p, SCB_TAG);
                   3564:       if (scb_index >= p->scb_data->numscbs)
                   3565:       {
                   3566:        /*
                   3567:         * No aic7xxx_verbose check here.....we want to see this since it
                   3568:         * means either the kernel driver or the sequencer screwed things up
                   3569:         */
                   3570:         printk(WARN_LEAD "Waiting List inconsistency; SCB index=%d, "
                   3571:           "numscbs=%d\n", p->host_no, channel, target, lun, scb_index,
                   3572:           p->scb_data->numscbs);
                   3573:         next = aic_inb(p, SCB_NEXT);
                   3574:         aic7xxx_add_curscb_to_free_list(p);
                   3575:       }
                   3576:       else
                   3577:       {
                   3578:         scbp = p->scb_data->scb_array[scb_index];
                   3579:         if (aic7xxx_match_scb(p, scbp, target, channel, lun, tag))
                   3580:         {
                   3581:           next = aic7xxx_abort_waiting_scb(p, scbp, next, prev);
                   3582:           if (scbp->flags & SCB_WAITINGQ)
                   3583:           {
                   3584:             p->dev_active_cmds[TARGET_INDEX(scbp->cmd)]++;
                   3585:             p->activescbs++;
                   3586:           }
                   3587:           scbp->flags &= ~(SCB_ACTIVE | SCB_WAITINGQ);
                   3588:           scbp->flags |= SCB_RESET | SCB_QUEUED_FOR_DONE;
                   3589:           if (prev == SCB_LIST_NULL)
                   3590:           {
                   3591:             /*
                   3592:              * This is either the first scb on the waiting list, or we
                   3593:              * have already yanked the first and haven't left any behind.
                   3594:              * Either way, we need to turn off the selection hardware if
                   3595:              * it isn't already off.
                   3596:              */
                   3597:             aic_outb(p, aic_inb(p, SCSISEQ) & ~ENSELO, SCSISEQ);
                   3598:             aic_outb(p, CLRSELTIMEO, CLRSINT1);
                   3599:           }
                   3600:         }
                   3601:         else
                   3602:         {
                   3603:           prev = next;
                   3604:           next = aic_inb(p, SCB_NEXT);
                   3605:         }
                   3606:       }
                   3607:     }
                   3608:     if ( j > (p->scb_data->maxscbs + 1) )
                   3609:     {
                   3610:       printk(WARN_LEAD "Yikes!!  There is a loop in the waiting for "
                   3611:         "selection list!\n", p->host_no, channel, target, lun);
                   3612:       init_lists = TRUE;
                   3613:     }
                   3614:   }
                   3615: 
                   3616:   /*
                   3617:    * Go through disconnected list and remove any entries we have queued
                   3618:    * for completion, zeroing their control byte too.
                   3619:    */
                   3620:   if (aic7xxx_verbose & (VERBOSE_ABORT_PROCESS | VERBOSE_RESET_PROCESS))
                   3621:     printk(INFO_LEAD "Cleaning disconnected scbs "
                   3622:       "list.\n", p->host_no, channel, target, lun);
1.1.1.2 ! root     3623:   if (p->flags & AHC_PAGESCBS)
1.1       root     3624:   {
                   3625:     unsigned char next, prev, scb_index;
                   3626: 
                   3627:     next = aic_inb(p, DISCONNECTED_SCBH);
                   3628:     prev = SCB_LIST_NULL;
                   3629:     j = 0;
                   3630:     while ( (next != SCB_LIST_NULL) && (j++ <= (p->scb_data->maxscbs + 1)) )
                   3631:     {
                   3632:       aic_outb(p, next, SCBPTR);
                   3633:       scb_index = aic_inb(p, SCB_TAG);
                   3634:       if (scb_index > p->scb_data->numscbs)
                   3635:       {
                   3636:         printk(WARN_LEAD "Disconnected List inconsistency; SCB index=%d, "
                   3637:           "numscbs=%d\n", p->host_no, channel, target, lun, scb_index,
                   3638:           p->scb_data->numscbs);
                   3639:         next = aic7xxx_rem_scb_from_disc_list(p, next);
                   3640:       }
                   3641:       else
                   3642:       {
                   3643:         scbp = p->scb_data->scb_array[scb_index];
                   3644:         if (aic7xxx_match_scb(p, scbp, target, channel, lun, tag))
                   3645:         {
                   3646:           next = aic7xxx_rem_scb_from_disc_list(p, next);
                   3647:           if (scbp->flags & SCB_WAITINGQ)
                   3648:           {
                   3649:             p->dev_active_cmds[TARGET_INDEX(scbp->cmd)]++;
                   3650:             p->activescbs++;
                   3651:           }
                   3652:           scbp->flags &= ~(SCB_ACTIVE | SCB_WAITINGQ);
                   3653:           scbp->flags |= SCB_RESET | SCB_QUEUED_FOR_DONE;
                   3654:           scbp->hscb->control = 0;
                   3655:         }
                   3656:         else
                   3657:         {
                   3658:           prev = next;
                   3659:           next = aic_inb(p, SCB_NEXT);
                   3660:         }
                   3661:       }
                   3662:     }
                   3663:     if ( j > (p->scb_data->maxscbs + 1) )
                   3664:     {
                   3665:       printk(WARN_LEAD "Yikes!!  There is a loop in the disconnected list!\n",
                   3666:         p->host_no, channel, target, lun);
                   3667:       init_lists = TRUE;
                   3668:     }
                   3669:   }
                   3670: 
                   3671:   /*
                   3672:    * Walk the free list making sure no entries on the free list have
                   3673:    * a valid SCB_TAG value or SCB_CONTROL byte.
                   3674:    */
1.1.1.2 ! root     3675:   if (p->flags & AHC_PAGESCBS)
1.1       root     3676:   {
                   3677:     unsigned char next;
                   3678: 
                   3679:     j = 0;
                   3680:     next = aic_inb(p, FREE_SCBH);
                   3681:     if ( (next >= p->scb_data->maxhscbs) && (next != SCB_LIST_NULL) )
                   3682:     {
                   3683:       printk(WARN_LEAD "Bogus FREE_SCBH!.\n", p->host_no, channel,
                   3684:         target, lun);
                   3685:       init_lists = TRUE;
                   3686:       next = SCB_LIST_NULL;
                   3687:     }
                   3688:     while ( (next != SCB_LIST_NULL) && (j++ <= (p->scb_data->maxscbs + 1)) )
                   3689:     {
                   3690:       aic_outb(p, next, SCBPTR);
                   3691:       if (aic_inb(p, SCB_TAG) < p->scb_data->numscbs)
                   3692:       {
                   3693:         printk(WARN_LEAD "Free list inconsistency!.\n", p->host_no, channel,
                   3694:           target, lun);
                   3695:         init_lists = TRUE;
                   3696:         next = SCB_LIST_NULL;
                   3697:       }
                   3698:       else
                   3699:       {
                   3700:         aic_outb(p, SCB_LIST_NULL, SCB_TAG);
                   3701:         aic_outb(p, 0, SCB_CONTROL);
                   3702:         next = aic_inb(p, SCB_NEXT);
                   3703:       }
                   3704:     }
                   3705:     if ( j > (p->scb_data->maxscbs + 1) )
                   3706:     {
                   3707:       printk(WARN_LEAD "Yikes!!  There is a loop in the free list!\n",
                   3708:         p->host_no, channel, target, lun);
                   3709:       init_lists = TRUE;
                   3710:     }
                   3711:   }
                   3712: 
                   3713:   /*
                   3714:    * Go through the hardware SCB array looking for commands that
                   3715:    * were active but not on any list.
                   3716:    */
                   3717:   if (init_lists)
                   3718:   {
                   3719:     aic_outb(p, SCB_LIST_NULL, FREE_SCBH);
                   3720:     aic_outb(p, SCB_LIST_NULL, WAITING_SCBH);
                   3721:     aic_outb(p, SCB_LIST_NULL, DISCONNECTED_SCBH);
                   3722:   }
                   3723:   for (i = p->scb_data->maxhscbs - 1; i >= 0; i--)
                   3724:   {
                   3725:     unsigned char scbid;
                   3726: 
                   3727:     aic_outb(p, i, SCBPTR);
                   3728:     if (init_lists)
                   3729:     {
                   3730:       aic_outb(p, SCB_LIST_NULL, SCB_TAG);
                   3731:       aic_outb(p, SCB_LIST_NULL, SCB_NEXT);
                   3732:       aic_outb(p, SCB_LIST_NULL, SCB_PREV);
                   3733:       aic_outb(p, 0, SCB_CONTROL);
                   3734:       aic7xxx_add_curscb_to_free_list(p);
                   3735:     }
                   3736:     else
                   3737:     {
                   3738:       scbid = aic_inb(p, SCB_TAG);
                   3739:       if (scbid < p->scb_data->numscbs)
                   3740:       {
                   3741:         scbp = p->scb_data->scb_array[scbid];
                   3742:         if (aic7xxx_match_scb(p, scbp, target, channel, lun, tag))
                   3743:         {
                   3744:           aic_outb(p, 0, SCB_CONTROL);
                   3745:           aic_outb(p, SCB_LIST_NULL, SCB_TAG);
                   3746:           aic7xxx_add_curscb_to_free_list(p);
                   3747:         }
                   3748:       }
                   3749:     }
                   3750:   }
                   3751: 
                   3752:   /*
                   3753:    * Go through the entire SCB array now and look for commands for
                   3754:    * for this target that are stillactive.  These are other (most likely
                   3755:    * tagged) commands that were disconnected when the reset occurred.
                   3756:    * Any commands we find here we know this about, it wasn't on any queue,
                   3757:    * it wasn't in the qinfifo, it wasn't in the disconnected or waiting
                   3758:    * lists, so it really must have been a paged out SCB.  In that case,
                   3759:    * we shouldn't need to bother with updating any counters, just mark
                   3760:    * the correct flags and go on.
                   3761:    */
                   3762:   for (i = 0; i < p->scb_data->numscbs; i++)
                   3763:   {
                   3764:     scbp = p->scb_data->scb_array[i];
                   3765:     if ((scbp->flags & SCB_ACTIVE) &&
                   3766:         aic7xxx_match_scb(p, scbp, target, channel, lun, tag) &&
                   3767:         !aic7xxx_scb_on_qoutfifo(p, scbp))
                   3768:     {
                   3769:       if (scbp->flags & SCB_WAITINGQ)
                   3770:       {
                   3771:         scbq_remove(&p->waiting_scbs, scbp);
                   3772:         scbq_remove(&p->delayed_scbs[TARGET_INDEX(scbp->cmd)], scbp);
                   3773:         p->dev_active_cmds[TARGET_INDEX(scbp->cmd)]++;
                   3774:         p->activescbs++;
                   3775:       }
                   3776:       scbp->flags |= SCB_RESET | SCB_QUEUED_FOR_DONE;
                   3777:       scbp->flags &= ~(SCB_ACTIVE | SCB_WAITINGQ);
                   3778:     }
                   3779:   }
                   3780: 
                   3781:   aic_outb(p, active_scb, SCBPTR);
                   3782: }
                   3783: 
                   3784: 
                   3785: /*+F*************************************************************************
                   3786:  * Function:
                   3787:  *   aic7xxx_clear_intstat
                   3788:  *
                   3789:  * Description:
                   3790:  *   Clears the interrupt status.
                   3791:  *-F*************************************************************************/
                   3792: static void
                   3793: aic7xxx_clear_intstat(struct aic7xxx_host *p)
                   3794: {
                   3795:   /* Clear any interrupt conditions this may have caused. */
                   3796:   aic_outb(p, CLRSELDO | CLRSELDI | CLRSELINGO, CLRSINT0);
                   3797:   aic_outb(p, CLRSELTIMEO | CLRATNO | CLRSCSIRSTI | CLRBUSFREE | CLRSCSIPERR |
                   3798:        CLRPHASECHG | CLRREQINIT, CLRSINT1);
                   3799:   aic_outb(p, CLRSCSIINT | CLRSEQINT | CLRBRKADRINT | CLRPARERR, CLRINT);
                   3800: }
                   3801: 
                   3802: /*+F*************************************************************************
                   3803:  * Function:
                   3804:  *   aic7xxx_reset_current_bus
                   3805:  *
                   3806:  * Description:
                   3807:  *   Reset the current SCSI bus.
                   3808:  *-F*************************************************************************/
                   3809: static void
                   3810: aic7xxx_reset_current_bus(struct aic7xxx_host *p)
                   3811: {
                   3812: 
                   3813:   /* Disable reset interrupts. */
                   3814:   aic_outb(p, aic_inb(p, SIMODE1) & ~ENSCSIRST, SIMODE1);
                   3815: 
                   3816:   /* Turn off the bus' current operations, after all, we shouldn't have any
                   3817:    * valid commands left to cause a RSELI and SELO once we've tossed the
                   3818:    * bus away with this reset, so we might as well shut down the sequencer
                   3819:    * until the bus is restarted as oppossed to saving the current settings
                   3820:    * and restoring them (which makes no sense to me). */
                   3821: 
                   3822:   /* Turn on the bus reset. */
                   3823:   aic_outb(p, aic_inb(p, SCSISEQ) | SCSIRSTO, SCSISEQ);
                   3824:   while ( (aic_inb(p, SCSISEQ) & SCSIRSTO) == 0)
                   3825:     mdelay(5);
                   3826: 
                   3827:   mdelay(10);
                   3828: 
                   3829:   /* Turn off the bus reset. */
                   3830:   aic_outb(p, 0, SCSISEQ);
                   3831:   mdelay(5);
                   3832: 
                   3833:   aic7xxx_clear_intstat(p);
                   3834:   /* Re-enable reset interrupts. */
                   3835:   aic_outb(p, aic_inb(p, SIMODE1) | ENSCSIRST, SIMODE1);
                   3836: 
                   3837: }
                   3838: 
                   3839: /*+F*************************************************************************
                   3840:  * Function:
                   3841:  *   aic7xxx_reset_channel
                   3842:  *
                   3843:  * Description:
                   3844:  *   Reset the channel.
                   3845:  *-F*************************************************************************/
                   3846: static void
                   3847: aic7xxx_reset_channel(struct aic7xxx_host *p, int channel, int initiate_reset)
                   3848: {
                   3849:   unsigned long offset_min, offset_max;
                   3850:   unsigned char sblkctl;
                   3851:   int cur_channel;
                   3852: 
                   3853:   if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   3854:     printk(INFO_LEAD "Reset channel called, %s initiate reset.\n",
                   3855:       p->host_no, channel, -1, -1, (initiate_reset==TRUE) ? "will" : "won't" );
                   3856: 
                   3857: 
                   3858:   if (channel == 1)
                   3859:   {
                   3860:     p->needsdtr |= (p->needsdtr_copy & 0xFF00);
                   3861:     p->sdtr_pending &= 0x00FF;
                   3862:     offset_min = 8;
                   3863:     offset_max = 16;
                   3864:   }
                   3865:   else
                   3866:   {
                   3867:     if (p->features & AHC_WIDE)
                   3868:     {
                   3869:       p->needsdtr = p->needsdtr_copy;
                   3870:       p->needwdtr = p->needwdtr_copy;
                   3871:       p->sdtr_pending = 0x0;
                   3872:       p->wdtr_pending = 0x0;
                   3873:       offset_min = 0;
                   3874:       offset_max = 16;
                   3875:     }
                   3876:     else
                   3877:     {
                   3878:       /* Channel A */
                   3879:       p->needsdtr |= (p->needsdtr_copy & 0x00FF);
                   3880:       p->sdtr_pending &= 0xFF00;
                   3881:       offset_min = 0;
                   3882:       offset_max = 8;
                   3883:     }
                   3884:   }
                   3885: 
                   3886:   while (offset_min < offset_max)
                   3887:   {
                   3888:     /*
                   3889:      * Revert to async/narrow transfers until we renegotiate.
                   3890:      */
                   3891:     aic_outb(p, 0, TARG_SCSIRATE + offset_min);
                   3892:     if (p->features & AHC_ULTRA2)
                   3893:     {
                   3894:       aic_outb(p, 0, TARG_OFFSET + offset_min);
                   3895:     }
                   3896:     offset_min++;
                   3897:   }
                   3898: 
                   3899:   /*
                   3900:    * Reset the bus and unpause/restart the controller
                   3901:    */
                   3902:   sblkctl = aic_inb(p, SBLKCTL);
                   3903:   if ( (p->chip & AHC_CHIPID_MASK) == AHC_AIC7770 )
                   3904:     cur_channel = (sblkctl & SELBUSB) >> 3;
                   3905:   else
                   3906:     cur_channel = 0;
                   3907:   if ( (cur_channel != channel) && (p->features & AHC_TWIN) )
                   3908:   {
                   3909:     /*
                   3910:      * Case 1: Command for another bus is active
                   3911:      */
                   3912:     if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   3913:       printk(INFO_LEAD "Stealthily resetting idle channel.\n", p->host_no,
                   3914:         channel, -1, -1);
                   3915:     /*
                   3916:      * Stealthily reset the other bus without upsetting the current bus.
                   3917:      */
                   3918:     aic_outb(p, sblkctl ^ SELBUSB, SBLKCTL);
                   3919:     aic_outb(p, aic_inb(p, SIMODE1) & ~ENBUSFREE, SIMODE1);
                   3920:     if (initiate_reset)
                   3921:     {
                   3922:       aic7xxx_reset_current_bus(p);
                   3923:     }
                   3924:     aic_outb(p, aic_inb(p, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP), SCSISEQ);
                   3925:     aic7xxx_clear_intstat(p);
                   3926:     aic_outb(p, sblkctl, SBLKCTL);
                   3927:   }
                   3928:   else
                   3929:   {
                   3930:     /*
                   3931:      * Case 2: A command from this bus is active or we're idle.
                   3932:      */
                   3933:     if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   3934:       printk(INFO_LEAD "Resetting currently active channel.\n", p->host_no,
                   3935:         channel, -1, -1);
                   3936:     aic_outb(p, aic_inb(p, SIMODE1) & ~(ENBUSFREE|ENREQINIT),
                   3937:       SIMODE1);
                   3938:     p->flags &= ~AHC_HANDLING_REQINITS;
                   3939:     p->msg_type = MSG_TYPE_NONE;
                   3940:     p->msg_len = 0;
                   3941:     if (initiate_reset)
                   3942:     {
                   3943:       aic7xxx_reset_current_bus(p);
                   3944:     }
                   3945:     aic_outb(p, aic_inb(p, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP), SCSISEQ);
                   3946:     aic7xxx_clear_intstat(p);
                   3947:   }
                   3948:   if (aic7xxx_verbose & VERBOSE_RESET_RETURN)
                   3949:     printk(INFO_LEAD "Channel reset\n", p->host_no, channel, -1, -1);
                   3950:   /*
                   3951:    * Clean up all the state information for the pending transactions
                   3952:    * on this bus.
                   3953:    */
                   3954:   aic7xxx_reset_device(p, ALL_TARGETS, channel, ALL_LUNS, SCB_LIST_NULL);
                   3955: 
                   3956:   if ( !(p->features & AHC_TWIN) )
                   3957:   {
                   3958:     restart_sequencer(p);
                   3959:   }
                   3960: 
                   3961:   return;
                   3962: }
                   3963: 
                   3964: /*+F*************************************************************************
                   3965:  * Function:
                   3966:  *   aic7xxx_run_waiting_queues
                   3967:  *
                   3968:  * Description:
                   3969:  *   Scan the awaiting_scbs queue downloading and starting as many
                   3970:  *   scbs as we can.
                   3971:  *-F*************************************************************************/
                   3972: static void
                   3973: aic7xxx_run_waiting_queues(struct aic7xxx_host *p)
                   3974: {
                   3975:   struct aic7xxx_scb *scb;
                   3976:   int tindex;
                   3977:   int sent;
                   3978: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
                   3979:   unsigned long cpu_flags = 0;
                   3980: #endif
                   3981: 
                   3982: 
                   3983:   if (p->waiting_scbs.head == NULL)
                   3984:     return;
                   3985: 
                   3986:   sent = 0;
                   3987: 
                   3988:   /*
                   3989:    * First handle SCBs that are waiting but have been assigned a slot.
                   3990:    */
                   3991:   DRIVER_LOCK
                   3992:   while ((scb = scbq_remove_head(&p->waiting_scbs)) != NULL)
                   3993:   {
                   3994:     tindex = TARGET_INDEX(scb->cmd);
                   3995:     if ( !scb->tag_action && (p->tagenable & (1<<tindex)) )
                   3996:     {
                   3997:       p->dev_temp_queue_depth[tindex] = 1;
                   3998:     }
                   3999:     if ( (p->dev_active_cmds[tindex] >=
                   4000:           p->dev_temp_queue_depth[tindex]) ||
1.1.1.2 ! root     4001:          (p->dev_flags[tindex] & (DEVICE_RESET_DELAY|DEVICE_WAS_BUSY)) ||
        !          4002:          (p->flags & AHC_RESET_DELAY) )
1.1       root     4003:     {
                   4004:       scbq_insert_tail(&p->delayed_scbs[tindex], scb);
                   4005:     }
                   4006:     else
                   4007:     {
                   4008:         scb->flags &= ~SCB_WAITINGQ;
                   4009:         p->dev_active_cmds[tindex]++;
                   4010:         p->activescbs++;
                   4011:         if ( !(scb->tag_action) )
                   4012:         {
                   4013:           aic7xxx_busy_target(p, scb);
                   4014:         }
                   4015:         p->qinfifo[p->qinfifonext++] = scb->hscb->tag;
                   4016:         sent++;
                   4017:     }
                   4018:   }
                   4019:   if (sent)
                   4020:   {
                   4021:     if (p->features & AHC_QUEUE_REGS)
                   4022:       aic_outb(p, p->qinfifonext, HNSCB_QOFF);
                   4023:     else
                   4024:     {
                   4025:       pause_sequencer(p);
                   4026:       aic_outb(p, p->qinfifonext, KERNEL_QINPOS);
                   4027:       unpause_sequencer(p, FALSE);
                   4028:     }
                   4029:     if (p->activescbs > p->max_activescbs)
                   4030:       p->max_activescbs = p->activescbs;
                   4031:   }
                   4032:   DRIVER_UNLOCK
                   4033: }
                   4034: 
                   4035: #ifdef CONFIG_PCI
                   4036: 
                   4037: #define  DPE 0x80
                   4038: #define  SSE 0x40
                   4039: #define  RMA 0x20
                   4040: #define  RTA 0x10
                   4041: #define  STA 0x08
                   4042: #define  DPR 0x01
                   4043: 
                   4044: /*+F*************************************************************************
                   4045:  * Function:
                   4046:  *   aic7xxx_pci_intr
                   4047:  *
                   4048:  * Description:
                   4049:  *   Check the scsi card for PCI errors and clear the interrupt
                   4050:  *
                   4051:  *   NOTE: If you don't have this function and a 2940 card encounters
                   4052:  *         a PCI error condition, the machine will end up locked as the
                   4053:  *         interrupt handler gets slammed with non-stop PCI error interrupts
                   4054:  *-F*************************************************************************/
                   4055: static void
                   4056: aic7xxx_pci_intr(struct aic7xxx_host *p)
                   4057: {
                   4058:   unsigned char status1;
                   4059: 
                   4060: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   4061:   pci_read_config_byte(p->pdev, PCI_STATUS + 1, &status1);
                   4062: #else
                   4063:   pcibios_read_config_byte(p->pci_bus, p->pci_device_fn,
                   4064:                            PCI_STATUS + 1, &status1);
                   4065: #endif
                   4066: 
                   4067:   if ( (status1 & DPE) && (aic7xxx_verbose & VERBOSE_MINOR_ERROR) )
                   4068:     printk(WARN_LEAD "Data Parity Error during PCI address or PCI write"
                   4069:       "phase.\n", p->host_no, -1, -1, -1);
                   4070:   if ( (status1 & SSE) && (aic7xxx_verbose & VERBOSE_MINOR_ERROR) )
                   4071:     printk(WARN_LEAD "Signal System Error Detected\n", p->host_no,
                   4072:       -1, -1, -1);
                   4073:   if ( (status1 & RMA) && (aic7xxx_verbose & VERBOSE_MINOR_ERROR) )
                   4074:     printk(WARN_LEAD "Received a PCI Master Abort\n", p->host_no,
                   4075:       -1, -1, -1);
                   4076:   if ( (status1 & RTA) && (aic7xxx_verbose & VERBOSE_MINOR_ERROR) )
                   4077:     printk(WARN_LEAD "Received a PCI Target Abort\n", p->host_no,
                   4078:       -1, -1, -1);
                   4079:   if ( (status1 & STA) && (aic7xxx_verbose & VERBOSE_MINOR_ERROR) )
                   4080:     printk(WARN_LEAD "Signaled a PCI Target Abort\n", p->host_no,
                   4081:       -1, -1, -1);
                   4082:   if ( (status1 & DPR) && (aic7xxx_verbose & VERBOSE_MINOR_ERROR) )
                   4083:     printk(WARN_LEAD "Data Parity Error has been reported via PCI pin "
                   4084:       "PERR#\n", p->host_no, -1, -1, -1);
1.1.1.2 ! root     4085: 
1.1       root     4086: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   4087:   pci_write_config_byte(p->pdev, PCI_STATUS + 1, status1);
                   4088: #else
                   4089:   pcibios_write_config_byte(p->pci_bus, p->pci_device_fn,
                   4090:                             PCI_STATUS + 1, status1);
                   4091: #endif
                   4092:   if (status1 & (DPR|RMA|RTA))
                   4093:     aic_outb(p,  CLRPARERR, CLRINT);
                   4094: 
                   4095:   if ( (aic7xxx_panic_on_abort) && (p->spurious_int > 500) )
                   4096:     aic7xxx_panic_abort(p, NULL);
                   4097: 
                   4098: }
                   4099: #endif /* CONFIG_PCI */
                   4100: 
                   4101: /*+F*************************************************************************
                   4102:  * Function:
                   4103:  *   aic7xxx_timer
                   4104:  *
                   4105:  * Description:
                   4106:  *   Take expired extries off of delayed queues and place on waiting queue
                   4107:  *   then run waiting queue to start commands.
                   4108:  ***************************************************************************/
                   4109: static void
                   4110: aic7xxx_timer(struct aic7xxx_host *p)
                   4111: {
                   4112:   int i, j;
                   4113:   unsigned long cpu_flags = 0;
                   4114:   struct aic7xxx_scb *scb;
                   4115: 
1.1.1.2 ! root     4116: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1.1       root     4117:   DRIVER_LOCK
                   4118: #else
                   4119:   spin_lock_irqsave(&io_request_lock, cpu_flags);
                   4120: #endif
1.1.1.2 ! root     4121:   p->dev_timer_active &= ~(0x01 << MAX_TARGETS);
        !          4122:   if ( (p->dev_timer_active & (0x01 << p->scsi_id)) &&
        !          4123:        time_after_eq(jiffies, p->dev_expires[p->scsi_id]) )
        !          4124:   {
        !          4125:     p->flags &= ~AHC_RESET_DELAY;
        !          4126:     p->dev_timer_active &= ~(0x01 << p->scsi_id);
        !          4127:   }
1.1       root     4128:   for(i=0; i<MAX_TARGETS; i++)
                   4129:   {
1.1.1.2 ! root     4130:     if ( (i != p->scsi_id) &&
        !          4131:          (p->dev_timer_active & (0x01 << i)) &&
        !          4132:          time_after_eq(jiffies, p->dev_expires[i]) )
1.1       root     4133:     {
1.1.1.2 ! root     4134:       p->dev_timer_active &= ~(0x01 << i);
        !          4135:       p->dev_flags[i] &= ~(DEVICE_RESET_DELAY|DEVICE_WAS_BUSY);
1.1       root     4136:       p->dev_temp_queue_depth[i] =  p->dev_max_queue_depth[i];
                   4137:       j = 0;
                   4138:       while ( ((scb = scbq_remove_head(&p->delayed_scbs[i])) != NULL) &&
                   4139:               (j++ < p->scb_data->numscbs) )
                   4140:       {
                   4141:         scbq_insert_tail(&p->waiting_scbs, scb);
                   4142:       }
                   4143:       if (j == p->scb_data->numscbs)
                   4144:       {
                   4145:         printk(INFO_LEAD "timer: Yikes, loop in delayed_scbs list.\n",
                   4146:           p->host_no, 0, i, -1);
                   4147:         scbq_init(&p->delayed_scbs[i]);
                   4148:         scbq_init(&p->waiting_scbs);
                   4149:         /*
                   4150:          * Well, things are screwed now, wait for a reset to clean the junk
                   4151:          * out.
                   4152:          */
                   4153:       }
                   4154:     }
1.1.1.2 ! root     4155:     else if ( p->dev_timer_active & (0x01 << i) )
        !          4156:     {
        !          4157:       if ( p->dev_timer_active & (0x01 << MAX_TARGETS) )
        !          4158:       {
        !          4159:         if ( time_after_eq(p->dev_timer.expires, p->dev_expires[i]) )
        !          4160:         {
        !          4161:           p->dev_timer.expires = p->dev_expires[i];
        !          4162:         }
        !          4163:       }
        !          4164:       else
        !          4165:       {
        !          4166:         p->dev_timer.expires = p->dev_expires[i];
        !          4167:         p->dev_timer_active |= (0x01 << MAX_TARGETS);
        !          4168:       }
        !          4169:     }
        !          4170:   }
        !          4171:   if ( p->dev_timer_active & (0x01 << MAX_TARGETS) )
        !          4172:   {
        !          4173:     add_timer(&p->dev_timer);
1.1       root     4174:   }
1.1.1.2 ! root     4175: 
1.1       root     4176:   aic7xxx_run_waiting_queues(p);
1.1.1.2 ! root     4177: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
1.1       root     4178:   DRIVER_UNLOCK
                   4179: #else
                   4180:   spin_unlock_irqrestore(&io_request_lock, cpu_flags);
                   4181: #endif
                   4182: }
                   4183: 
                   4184: /*+F*************************************************************************
                   4185:  * Function:
                   4186:  *   aic7xxx_construct_sdtr
                   4187:  *
                   4188:  * Description:
                   4189:  *   Constucts a synchronous data transfer message in the message
                   4190:  *   buffer on the sequencer.
                   4191:  *-F*************************************************************************/
                   4192: static void
                   4193: aic7xxx_construct_sdtr(struct aic7xxx_host *p, unsigned char period,
                   4194:         unsigned char offset)
                   4195: {
                   4196:   p->msg_buf[p->msg_index++] = MSG_EXTENDED;
                   4197:   p->msg_buf[p->msg_index++] = MSG_EXT_SDTR_LEN;
                   4198:   p->msg_buf[p->msg_index++] = MSG_EXT_SDTR;
                   4199:   p->msg_buf[p->msg_index++] = period;
                   4200:   p->msg_buf[p->msg_index++] = offset;
                   4201:   p->msg_len += 5;
                   4202: }
                   4203: 
                   4204: /*+F*************************************************************************
                   4205:  * Function:
                   4206:  *   aic7xxx_construct_wdtr
                   4207:  *
                   4208:  * Description:
                   4209:  *   Constucts a wide data transfer message in the message buffer
                   4210:  *   on the sequencer.
                   4211:  *-F*************************************************************************/
                   4212: static void
                   4213: aic7xxx_construct_wdtr(struct aic7xxx_host *p, unsigned char bus_width)
                   4214: {
                   4215:   p->msg_buf[p->msg_index++] = MSG_EXTENDED;
                   4216:   p->msg_buf[p->msg_index++] = MSG_EXT_WDTR_LEN;
                   4217:   p->msg_buf[p->msg_index++] = MSG_EXT_WDTR;
                   4218:   p->msg_buf[p->msg_index++] = bus_width;
                   4219:   p->msg_len += 4;
                   4220: }
                   4221: 
                   4222: /*+F*************************************************************************
                   4223:  * Function:
                   4224:  *   aic7xxx_calc_residual
                   4225:  *
                   4226:  * Description:
                   4227:  *   Calculate the residual data not yet transferred.
                   4228:  *-F*************************************************************************/
                   4229: static void
                   4230: aic7xxx_calculate_residual (struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                   4231: {
                   4232:   struct aic7xxx_hwscb *hscb;
                   4233:   Scsi_Cmnd *cmd;
                   4234:   int actual, i;
                   4235: 
                   4236:   cmd = scb->cmd;
                   4237:   hscb = scb->hscb;
                   4238: 
                   4239:   /*
                   4240:    *  Don't destroy valid residual information with
                   4241:    *  residual coming from a check sense operation.
                   4242:    */
                   4243:   if (((scb->hscb->control & DISCONNECTED) == 0) &&
                   4244:       (scb->flags & SCB_SENSE) == 0)
                   4245:   {
                   4246:     /*
                   4247:      *  We had an underflow. At this time, there's only
                   4248:      *  one other driver that bothers to check for this,
                   4249:      *  and cmd->underflow seems to be set rather half-
                   4250:      *  heartedly in the higher-level SCSI code.
                   4251:      */
                   4252:     actual = scb->sg_length;
                   4253:     for (i=1; i < hscb->residual_SG_segment_count; i++)
                   4254:     {
                   4255:       actual -= scb->sg_list[scb->sg_count - i].length;
                   4256:     }
                   4257:     actual -= (hscb->residual_data_count[2] << 16) |
                   4258:               (hscb->residual_data_count[1] <<  8) |
                   4259:               hscb->residual_data_count[0];
                   4260: 
                   4261:     if (actual < cmd->underflow)
                   4262:     {
                   4263:       if (aic7xxx_verbose & VERBOSE_MINOR_ERROR)
                   4264:         printk(INFO_LEAD "Underflow - Wanted %u, %s %u, residual SG "
                   4265:           "count %d.\n", p->host_no, CTL_OF_SCB(scb), cmd->underflow,
                   4266:           (cmd->request.cmd == WRITE) ? "wrote" : "read", actual,
                   4267:           hscb->residual_SG_segment_count);
                   4268:       aic7xxx_error(cmd) = DID_RETRY_COMMAND;
                   4269:       aic7xxx_status(cmd) = hscb->target_status;
                   4270:     }
                   4271:   }
                   4272: 
                   4273:   /*
                   4274:    * Clean out the residual information in the SCB for the
                   4275:    * next consumer.
                   4276:    */
                   4277:   hscb->residual_data_count[2] = 0;
                   4278:   hscb->residual_data_count[1] = 0;
                   4279:   hscb->residual_data_count[0] = 0;
                   4280:   hscb->residual_SG_segment_count = 0;
                   4281: }
                   4282: 
                   4283: /*+F*************************************************************************
                   4284:  * Function:
                   4285:  *   aic7xxx_handle_device_reset
                   4286:  *
                   4287:  * Description:
                   4288:  *   Interrupt handler for sequencer interrupts (SEQINT).
                   4289:  *-F*************************************************************************/
                   4290: static void
                   4291: aic7xxx_handle_device_reset(struct aic7xxx_host *p, int target, int channel)
                   4292: {
                   4293:   unsigned short targ_mask;
                   4294:   unsigned char tindex = target;
                   4295: 
                   4296:   tindex |= ((channel & 0x01) << 3);
                   4297: 
                   4298:   targ_mask = (0x01 << tindex);
                   4299:   /*
                   4300:    * Go back to async/narrow transfers and renegotiate.
                   4301:    */
                   4302:   p->needsdtr |= (p->needsdtr_copy & targ_mask);
                   4303:   p->needwdtr |= (p->needwdtr_copy & targ_mask);
                   4304:   p->sdtr_pending &= ~targ_mask;
                   4305:   p->wdtr_pending &= ~targ_mask;
                   4306:   aic_outb(p, 0, TARG_SCSIRATE + tindex);
                   4307:   if (p->features & AHC_ULTRA2)
                   4308:     aic_outb(p, 0, TARG_OFFSET + tindex);
                   4309:   aic7xxx_reset_device(p, target, channel, ALL_LUNS, SCB_LIST_NULL);
                   4310:   if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   4311:     printk(INFO_LEAD "Bus Device Reset delivered.\n", p->host_no, channel,
                   4312:       target, -1);
                   4313:   aic7xxx_run_done_queue(p, /*complete*/ FALSE);
                   4314: }
                   4315: 
                   4316: /*+F*************************************************************************
                   4317:  * Function:
                   4318:  *   aic7xxx_handle_seqint
                   4319:  *
                   4320:  * Description:
                   4321:  *   Interrupt handler for sequencer interrupts (SEQINT).
                   4322:  *-F*************************************************************************/
                   4323: static void
                   4324: aic7xxx_handle_seqint(struct aic7xxx_host *p, unsigned char intstat)
                   4325: {
                   4326:   struct aic7xxx_scb *scb;
                   4327:   unsigned short target_mask;
                   4328:   unsigned char target, lun, tindex;
                   4329:   unsigned char queue_flag = FALSE;
                   4330:   char channel;
                   4331: 
                   4332:   target = ((aic_inb(p, SAVED_TCL) >> 4) & 0x0f);
                   4333:   if ( (p->chip & AHC_CHIPID_MASK) == AHC_AIC7770 )
                   4334:     channel = (aic_inb(p, SBLKCTL) & SELBUSB) >> 3;
                   4335:   else
                   4336:     channel = 0;
                   4337:   tindex = target + (channel << 3);
                   4338:   lun = aic_inb(p, SAVED_TCL) & 0x07;
                   4339:   target_mask = (0x01 << tindex);
                   4340: 
                   4341:   /*
                   4342:    * Go ahead and clear the SEQINT now, that avoids any interrupt race
                   4343:    * conditions later on in case we enable some other interrupt.
                   4344:    */
                   4345:   aic_outb(p, CLRSEQINT, CLRINT);
                   4346:   switch (intstat & SEQINT_MASK)
                   4347:   {
                   4348:     case NO_MATCH:
                   4349:       {
                   4350:         aic_outb(p, aic_inb(p, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP),
                   4351:                  SCSISEQ);
                   4352:         printk(WARN_LEAD "No active SCB for reconnecting target - Issuing "
                   4353:                "BUS DEVICE RESET.\n", p->host_no, channel, target, lun);
                   4354:         printk(WARN_LEAD "      SAVED_TCL=0x%x, ARG_1=0x%x, SEQADDR=0x%x\n",
                   4355:                p->host_no, channel, target, lun,
                   4356:                aic_inb(p, SAVED_TCL), aic_inb(p, ARG_1),
                   4357:                (aic_inb(p, SEQADDR1) << 8) | aic_inb(p, SEQADDR0));
                   4358:       }
                   4359:       break;
                   4360: 
                   4361:     case SEND_REJECT:
                   4362:       {
                   4363:         if (aic7xxx_verbose & VERBOSE_MINOR_ERROR)
                   4364:           printk(INFO_LEAD "Rejecting unknown message (0x%x) received from "
                   4365:             "target, SEQ_FLAGS=0x%x\n", p->host_no, channel, target, lun,
                   4366:             aic_inb(p, ACCUM), aic_inb(p, SEQ_FLAGS));
                   4367:       }
                   4368:       break;
                   4369: 
                   4370:     case NO_IDENT:
                   4371:       {
                   4372:         /*
                   4373:          * The reconnecting target either did not send an identify
                   4374:          * message, or did, but we didn't find an SCB to match and
                   4375:          * before it could respond to our ATN/abort, it hit a dataphase.
                   4376:          * The only safe thing to do is to blow it away with a bus
                   4377:          * reset.
                   4378:          */
                   4379:         if (aic7xxx_verbose & (VERBOSE_SEQINT | VERBOSE_RESET_MID))
                   4380:           printk(INFO_LEAD "Target did not send an IDENTIFY message; "
                   4381:             "LASTPHASE 0x%x, SAVED_TCL 0x%x\n", p->host_no, channel, target,
                   4382:             lun, aic_inb(p, LASTPHASE), aic_inb(p, SAVED_TCL));
                   4383: 
                   4384:         aic7xxx_reset_channel(p, channel, /*initiate reset*/ TRUE);
                   4385:         aic7xxx_run_done_queue(p, FALSE);
                   4386: 
                   4387:       }
                   4388:       break;
                   4389: 
                   4390:     case BAD_PHASE:
                   4391:       if (aic_inb(p, LASTPHASE) == P_BUSFREE)
                   4392:       {
                   4393:         if (aic7xxx_verbose & VERBOSE_SEQINT)
                   4394:           printk(INFO_LEAD "Missed busfree.\n", p->host_no, channel,
                   4395:             target, lun);
                   4396:         restart_sequencer(p);
                   4397:       }
                   4398:       else
                   4399:       {
                   4400:         if (aic7xxx_verbose & VERBOSE_SEQINT)
                   4401:           printk(INFO_LEAD "Unknown scsi bus phase, continuing\n", p->host_no,
                   4402:             channel, target, lun);
                   4403:       }
                   4404:       break;
                   4405: 
                   4406:     case EXTENDED_MSG:
                   4407:       {
                   4408:         p->msg_type = MSG_TYPE_INITIATOR_MSGIN;
                   4409:         p->msg_len = 0;
                   4410:         p->msg_index = 0;
                   4411: 
                   4412: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   4413:         if (aic7xxx_verbose > 0xffff)
                   4414:           printk(INFO_LEAD "Enabling REQINITs for MSG_IN\n", p->host_no,
                   4415:                  channel, target, lun);
                   4416: #endif
                   4417: 
1.1.1.2 ! root     4418:        /*
1.1       root     4419:         * To actually receive the message, simply turn on
                   4420:         * REQINIT interrupts and let our interrupt handler
                   4421:         * do the rest (REQINIT should already be true).
                   4422:         */
                   4423:         p->flags |= AHC_HANDLING_REQINITS;
                   4424:         aic_outb(p, aic_inb(p, SIMODE1) | ENREQINIT, SIMODE1);
                   4425: 
                   4426:        /*
                   4427:         * We don't want the sequencer unpaused yet so we return early
                   4428:         */
                   4429:         return;
                   4430:       }
                   4431: 
                   4432:     case REJECT_MSG:
                   4433:       {
                   4434:         /*
                   4435:          * What we care about here is if we had an outstanding SDTR
                   4436:          * or WDTR message for this target. If we did, this is a
                   4437:          * signal that the target is refusing negotiation.
                   4438:          */
                   4439:         unsigned char scb_index;
                   4440:         unsigned char last_msg;
                   4441: 
                   4442:         scb_index = aic_inb(p, SCB_TAG);
                   4443:         scb = p->scb_data->scb_array[scb_index];
                   4444:         last_msg = aic_inb(p, LAST_MSG);
                   4445: 
                   4446:         if ( (last_msg == MSG_IDENTIFYFLAG) &&
                   4447:              (scb->tag_action) &&
                   4448:             !(scb->flags & SCB_MSGOUT_BITS) )
                   4449:         {
1.1.1.2 ! root     4450:           if (scb->tag_action == MSG_ORDERED_Q_TAG)
1.1       root     4451:           {
                   4452:             /*
                   4453:              * OK...the device seems able to accept tagged commands, but
                   4454:              * not ordered tag commands, only simple tag commands.  So, we
                   4455:              * disable ordered tag commands and go on with life just like
                   4456:              * normal.
                   4457:              */
                   4458:             p->orderedtag &= ~target_mask;
                   4459:             scb->tag_action = MSG_SIMPLE_Q_TAG;
                   4460:             scb->hscb->control &= ~SCB_TAG_TYPE;
                   4461:             scb->hscb->control |= MSG_SIMPLE_Q_TAG;
                   4462:             aic_outb(p, scb->hscb->control, SCB_CONTROL);
                   4463:             /*
                   4464:              * OK..we set the tag type to simple tag command, now we re-assert
                   4465:              * ATNO and hope this will take us into the identify phase again
                   4466:              * so we can resend the tag type and info to the device.
                   4467:              */
                   4468:             aic_outb(p, MSG_IDENTIFYFLAG, MSG_OUT);
                   4469:             aic_outb(p, aic_inb(p, SCSISIGI) | ATNO, SCSISIGO);
                   4470:           }
1.1.1.2 ! root     4471:           else if (scb->tag_action == MSG_SIMPLE_Q_TAG)
1.1       root     4472:           {
                   4473:             unsigned char i, reset = 0;
                   4474:             struct aic7xxx_scb *scbp;
                   4475:             int old_verbose;
                   4476:             /*
                   4477:              * Hmmmm....the device is flaking out on tagged commands.  The
                   4478:              * bad thing is that we already have tagged commands enabled in
                   4479:              * the device struct in the mid level code.  We also have a queue
                   4480:              * set according to the tagged queue depth.  Gonna have to live
                   4481:              * with it by controlling our queue depth internally and making
                   4482:              * sure we don't set the tagged command flag any more.
                   4483:              */
                   4484:             p->tagenable &= ~target_mask;
                   4485:             p->orderedtag &= ~target_mask;
                   4486:             p->dev_max_queue_depth[tindex] =
                   4487:                p->dev_temp_queue_depth[tindex] = 1;
                   4488:             /*
                   4489:              * We set this command up as a bus device reset.  However, we have
                   4490:              * to clear the tag type as it's causing us problems.  We shouldnt
                   4491:              * have to worry about any other commands being active, since if
                   4492:              * the device is refusing tagged commands, this should be the
                   4493:              * first tagged command sent to the device, however, we do have
                   4494:              * to worry about any other tagged commands that may already be
                   4495:              * in the qinfifo.  The easiest way to do this, is to issue a BDR,
                   4496:              * send all the commands back to the mid level code, then let them
                   4497:              * come back and get rebuilt as untagged commands.
                   4498:              */
                   4499:             scb->tag_action = 0;
                   4500:             scb->hscb->control &= ~(TAG_ENB | SCB_TAG_TYPE);
                   4501:             aic_outb(p,  scb->hscb->control, SCB_CONTROL);
                   4502: 
                   4503:             old_verbose = aic7xxx_verbose;
                   4504:             aic7xxx_verbose &= ~(VERBOSE_RESET|VERBOSE_ABORT);
                   4505:             for (i=0; i!=p->scb_data->numscbs; i++)
                   4506:             {
                   4507:               scbp = p->scb_data->scb_array[i];
                   4508:               if ((scbp->flags & SCB_ACTIVE) && (scbp != scb))
                   4509:               {
                   4510:                 if (aic7xxx_match_scb(p, scbp, target, channel, lun, i))
                   4511:                 {
                   4512:                   aic7xxx_reset_device(p, target, channel, lun, i);
                   4513:                   reset++;
                   4514:                 }
                   4515:                 aic7xxx_run_done_queue(p, FALSE);
                   4516:               }
                   4517:             }
                   4518:             aic7xxx_verbose = old_verbose;
                   4519:             /*
                   4520:              * Wait until after the for loop to set the busy index since
                   4521:              * aic7xxx_reset_device will clear the busy index during its
                   4522:              * operation.
                   4523:              */
                   4524:             aic7xxx_busy_target(p, scb);
                   4525:             printk(INFO_LEAD "Device is refusing tagged commands, using "
                   4526:               "untagged I/O.\n", p->host_no, channel, target, lun);
                   4527:             aic_outb(p, MSG_IDENTIFYFLAG, MSG_OUT);
                   4528:             aic_outb(p, aic_inb(p, SCSISIGI) | ATNO, SCSISIGO);
                   4529:           }
                   4530:         }
                   4531:         else if (scb->flags & SCB_MSGOUT_WDTR)
                   4532:         {
                   4533:           /*
                   4534:            * note 8bit xfers and clear flag
                   4535:            */
                   4536:           p->needwdtr &= ~target_mask;
                   4537:           p->needwdtr_copy &= ~target_mask;
                   4538:           p->wdtr_pending &= ~target_mask;
                   4539:           scb->flags &= ~SCB_MSGOUT_BITS;
                   4540:           aic7xxx_set_width(p, target, channel, lun, MSG_EXT_WDTR_BUS_8_BIT,
                   4541:             (AHC_TRANS_ACTIVE|AHC_TRANS_GOAL|AHC_TRANS_CUR));
                   4542:           aic7xxx_set_syncrate(p, NULL, target, channel, 0, 0,
                   4543:                                AHC_TRANS_ACTIVE|AHC_TRANS_CUR|AHC_TRANS_QUITE);
                   4544:           if ( (p->needsdtr_copy & target_mask) &&
                   4545:                !(p->sdtr_pending & target_mask) )
                   4546:           {
                   4547:             p->sdtr_pending |= target_mask;
                   4548:             scb->flags |= SCB_MSGOUT_SDTR;
                   4549:             aic_outb(p, HOST_MSG, MSG_OUT);
                   4550:             aic_outb(p, aic_inb(p, SCSISIGO) | ATNO, SCSISIGO);
                   4551:           }
                   4552:         }
                   4553:         else if (scb->flags & SCB_MSGOUT_SDTR)
                   4554:         {
                   4555:          /*
                   4556:           * note asynch xfers and clear flag
                   4557:           */
                   4558:           p->needsdtr &= ~target_mask;
                   4559:           p->needsdtr_copy &= ~target_mask;
                   4560:           p->sdtr_pending &= ~target_mask;
                   4561:           scb->flags &= ~SCB_MSGOUT_SDTR;
                   4562:           aic7xxx_set_syncrate(p, NULL, target, channel, 0, 0,
                   4563:             (AHC_TRANS_CUR|AHC_TRANS_ACTIVE|AHC_TRANS_GOAL));
                   4564:         }
                   4565:         else if (aic7xxx_verbose & VERBOSE_SEQINT)
                   4566:         {
                   4567:           /*
                   4568:            * Otherwise, we ignore it.
                   4569:            */
                   4570:           printk(INFO_LEAD "Received MESSAGE_REJECT for unknown cause.  "
                   4571:             "Ignoring.\n", p->host_no, channel, target, lun);
                   4572:         }
                   4573:       }
                   4574:       break;
                   4575: 
                   4576:     case BAD_STATUS:
                   4577:       {
                   4578:         unsigned char scb_index;
                   4579:         struct aic7xxx_hwscb *hscb;
                   4580:         Scsi_Cmnd *cmd;
                   4581: 
                   4582:         /* The sequencer will notify us when a command has an error that
                   4583:          * would be of interest to the kernel.  This allows us to leave
                   4584:          * the sequencer running in the common case of command completes
                   4585:          * without error.  The sequencer will have DMA'd the SCB back
                   4586:          * up to us, so we can reference the drivers SCB array.
                   4587:          *
                   4588:          * Set the default return value to 0 indicating not to send
                   4589:          * sense.  The sense code will change this if needed and this
                   4590:          * reduces code duplication.
                   4591:          */
                   4592:         aic_outb(p, 0, RETURN_1);
                   4593:         scb_index = aic_inb(p, SCB_TAG);
                   4594:         if (scb_index > p->scb_data->numscbs)
                   4595:         {
                   4596:           printk(WARN_LEAD "Invalid SCB during SEQINT 0x%02x, SCB_TAG %d.\n",
                   4597:             p->host_no, channel, target, lun, intstat, scb_index);
                   4598:           break;
                   4599:         }
                   4600:         scb = p->scb_data->scb_array[scb_index];
                   4601:         hscb = scb->hscb;
                   4602: 
                   4603:         if (!(scb->flags & SCB_ACTIVE) || (scb->cmd == NULL))
                   4604:         {
                   4605:           printk(WARN_LEAD "Invalid SCB during SEQINT 0x%x, scb %d, flags 0x%x,"
                   4606:             " cmd 0x%lx.\n", p->host_no, channel, target, lun, intstat,
                   4607:             scb_index, scb->flags, (unsigned long) scb->cmd);
                   4608:         }
                   4609:         else
                   4610:         {
                   4611:           cmd = scb->cmd;
                   4612:           hscb->target_status = aic_inb(p, SCB_TARGET_STATUS);
                   4613:           aic7xxx_status(cmd) = hscb->target_status;
                   4614: 
                   4615:           cmd->result = hscb->target_status;
                   4616: 
                   4617:           switch (status_byte(hscb->target_status))
                   4618:           {
                   4619:             case GOOD:
                   4620:               if (aic7xxx_verbose & VERBOSE_SEQINT)
                   4621:                 printk(INFO_LEAD "Interrupted for status of GOOD???\n",
                   4622:                   p->host_no, CTL_OF_SCB(scb));
                   4623:               break;
                   4624: 
                   4625:             case COMMAND_TERMINATED:
                   4626:             case CHECK_CONDITION:
                   4627:               if ( !(scb->flags & SCB_SENSE) )
                   4628:               {
                   4629:                 /*
                   4630:                  * XXX - How do we save the residual (if there is one).
                   4631:                  */
                   4632:                 if ( hscb->residual_SG_segment_count != 0 )
                   4633:                   aic7xxx_calculate_residual(p, scb);
                   4634: 
                   4635:                 /*
                   4636:                    * Send a sense command to the requesting target.
                   4637:                  * XXX - revisit this and get rid of the memcopys.
                   4638:                    */
                   4639:                 memcpy(&scb->sense_cmd[0], &generic_sense[0],
                   4640:                        sizeof(generic_sense));
                   4641: 
                   4642:                 scb->sense_cmd[1] = (cmd->lun << 5);
                   4643:                 scb->sense_cmd[4] = sizeof(cmd->sense_buffer);
                   4644: 
1.1.1.2 ! root     4645:                 scb->sg_list[0].address =
1.1       root     4646:                   cpu_to_le32(VIRT_TO_BUS(&cmd->sense_buffer[0]));
1.1.1.2 ! root     4647:                 scb->sg_list[0].length =
1.1       root     4648:                   cpu_to_le32(sizeof(cmd->sense_buffer));
                   4649: 
                   4650:                 /*
                   4651:                  * XXX - We should allow disconnection, but can't as it
                   4652:                  * might allow overlapped tagged commands.
                   4653:                  */
                   4654:                   /* hscb->control &= DISCENB; */
                   4655:                 hscb->control = 0;
                   4656:                 hscb->target_status = 0;
1.1.1.2 ! root     4657:                 hscb->SG_list_pointer =
1.1       root     4658:                   cpu_to_le32(VIRT_TO_BUS(&scb->sg_list[0]));
                   4659:                 hscb->data_pointer = scb->sg_list[0].address;
                   4660:                 hscb->data_count = scb->sg_list[0].length;
1.1.1.2 ! root     4661:                 hscb->SCSI_cmd_pointer =
1.1       root     4662:                   cpu_to_le32(VIRT_TO_BUS(&scb->sense_cmd[0]));
                   4663:                 hscb->SCSI_cmd_length = COMMAND_SIZE(scb->sense_cmd[0]);
                   4664:                 hscb->residual_SG_segment_count = 0;
                   4665:                 hscb->residual_data_count[0] = 0;
                   4666:                 hscb->residual_data_count[1] = 0;
                   4667:                 hscb->residual_data_count[2] = 0;
                   4668: 
                   4669:                 scb->sg_count = hscb->SG_segment_count = 1;
                   4670:                 scb->sg_length = sizeof(cmd->sense_buffer);
                   4671:                 scb->tag_action = 0;
                   4672:                 /*
                   4673:                  * This problem could be caused if the target has lost power
                   4674:                  * or found some other way to loose the negotiation settings,
                   4675:                  * so if needed, we'll re-negotiate while doing the sense cmd.
                   4676:                  * However, if this SCB already was attempting to negotiate,
                   4677:                  * then we assume this isn't the problem and skip this part.
                   4678:                  */
                   4679: #ifdef AIC7XXX_FAKE_NEGOTIATION_CMDS
                   4680:                 if ( (scb->cmd->cmnd[0] != TEST_UNIT_READY) &&
                   4681:                      (p->dev_flags[tindex] & DEVICE_SCANNED) &&
1.1.1.2 ! root     4682:                      !(p->wdtr_pending & target_mask) &&
1.1       root     4683:                      !(p->sdtr_pending & target_mask) )
                   4684:                 {
                   4685:                   p->needwdtr |= (p->needwdtr_copy & target_mask);
                   4686:                   p->needsdtr |= (p->needsdtr_copy & target_mask);
                   4687:                 }
                   4688:                 else if ( (scb->cmd == p->dev_wdtr_cmnd[tindex]) ||
                   4689:                           (scb->cmd == p->dev_sdtr_cmnd[tindex]) )
                   4690:                 {
                   4691:                   /*
                   4692:                    * This is already a negotiation command, so we must have
                   4693:                    * already done either WDTR or SDTR (or maybe both).  So
                   4694:                    * we simply check sdtr_pending and needsdtr to see if we
                   4695:                    * should throw out SDTR on this command.
                   4696:                    *
                   4697:                    * Note: Don't check the needsdtr_copy here, instead just
                   4698:                    * check to see if WDTR wiped out our SDTR and set needsdtr.
                   4699:                    * Even if WDTR did wipe out SDTR and set needsdtr, if
                   4700:                    * parse_msg() then turned around and started our SDTR
                   4701:                    * in back to back fasion, then conclusion of that should
                   4702:                    * have negated any needsdtr setting.  That's why we only
                   4703:                    * check needsdtr and sdtr_pending.
                   4704:                    */
                   4705:                   scb->flags &= ~SCB_MSGOUT_BITS;
                   4706:                   if ( (scb->cmd == p->dev_wdtr_cmnd[tindex]) &&
                   4707:                        !(p->sdtr_pending & target_mask) &&
                   4708:                        (p->needsdtr & target_mask) )
                   4709:                   {
                   4710:                     p->sdtr_pending |= target_mask;
                   4711:                     hscb->control |= MK_MESSAGE;
                   4712:                     scb->flags |= SCB_MSGOUT_SDTR;
                   4713:                   }
                   4714: 
                   4715:                   /*
                   4716:                    * This is the important part though.  We are getting sense
                   4717:                    * info back from this device.  It's going into a fake
                   4718:                    * command.  We need to put that into the real command
                   4719:                    * instead so that the mid level SCSI code can act upon it.
                   4720:                    * So, when we set up these fake commands, the next pointer
                   4721:                    * is used to point to the real command.  Use that to change
                   4722:                    * the address of our sense_buffer[] to the real command.
                   4723:                    * However, don't do this if the real command is also a
                   4724:                    * TEST_UNIT_READY as it will most likely pull down its own
                   4725:                    * SENSE information anyway.
                   4726:                    */
                   4727:                   if (cmd->next->cmnd[0] != TEST_UNIT_READY)
                   4728:                   {
1.1.1.2 ! root     4729:                     scb->sg_list[0].address =
1.1       root     4730:                       cpu_to_le32(VIRT_TO_BUS(&cmd->next->sense_buffer[0]));
                   4731:                     hscb->data_pointer = scb->sg_list[0].address;
                   4732:                   }
                   4733:                 }
                   4734: #else
                   4735:                 if (  (scb->cmd->cmnd[0] != TEST_UNIT_READY) &&
1.1.1.2 ! root     4736:                      !(scb->flags & SCB_MSGOUT_BITS) &&
1.1       root     4737:                       (scb->cmd->lun == 0) &&
                   4738:                       (p->dev_flags[TARGET_INDEX(scb->cmd)] & DEVICE_SCANNED) )
                   4739:                 {
                   4740:                   if ( (p->needwdtr_copy & target_mask) &&
                   4741:                       !(p->wdtr_pending & target_mask) &&
                   4742:                       !(p->sdtr_pending & target_mask) )
                   4743:                   {
                   4744:                     p->needwdtr |= target_mask;
                   4745:                     p->wdtr_pending |= target_mask;
                   4746:                     hscb->control |= MK_MESSAGE;
                   4747:                     scb->flags |= SCB_MSGOUT_WDTR;
                   4748:                   }
                   4749:                   if ( p->needsdtr_copy & target_mask )
                   4750:                   {
                   4751:                     p->needsdtr |= target_mask;
                   4752:                     if ( !(p->wdtr_pending & target_mask) &&
                   4753:                          !(p->sdtr_pending & target_mask) )
                   4754:                     {
                   4755:                       p->sdtr_pending |= target_mask;
                   4756:                       hscb->control |= MK_MESSAGE;
                   4757:                       scb->flags |= SCB_MSGOUT_SDTR;
                   4758:                     }
                   4759:                   }
                   4760:                 }
                   4761:                 else
                   4762:                   scb->flags &= ~SCB_MSGOUT_BITS;
                   4763: #endif /* AIC7XXX_FAKE_NEGOTIATION_CMDS */
                   4764:                 scb->flags |= SCB_SENSE;
                   4765:                 /*
                   4766:                  * Ensure the target is busy since this will be an
                   4767:                  * an untagged request.
                   4768:                  */
                   4769: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   4770:                 if (aic7xxx_verbose > 0xffff)
                   4771:                 {
                   4772:                   if (scb->flags & SCB_MSGOUT_BITS)
                   4773:                     printk(INFO_LEAD "Requesting SENSE with %s\n", p->host_no,
                   4774:                            CTL_OF_SCB(scb), (scb->flags & SCB_MSGOUT_SDTR) ?
                   4775:                            "SDTR" : "WDTR");
                   4776:                   else
                   4777:                     printk(INFO_LEAD "Requesting SENSE, no MSG\n", p->host_no,
                   4778:                            CTL_OF_SCB(scb));
                   4779:                 }
                   4780: #endif
                   4781:                 aic7xxx_busy_target(p, scb);
                   4782:                 aic_outb(p, SEND_SENSE, RETURN_1);
                   4783:                 aic7xxx_error(cmd) = DID_OK;
                   4784:                 break;
                   4785:               }  /* first time sense, no errors */
                   4786:               aic7xxx_error(cmd) = DID_OK;
                   4787:               scb->flags &= ~SCB_SENSE;
                   4788:               break;
                   4789: 
                   4790:             case QUEUE_FULL:
                   4791:               queue_flag = TRUE;    /* Mark that this is a QUEUE_FULL and */
                   4792:             case BUSY:              /* drop through to here */
                   4793:             {
                   4794:               struct aic7xxx_scb *next_scbp, *prev_scbp;
                   4795:               unsigned char active_hscb, next_hscb, prev_hscb, scb_index;
                   4796:               /*
                   4797:                * We have to look three places for queued commands:
                   4798:                *  1: QINFIFO
                   4799:                *  2: p->waiting_scbs queue
                   4800:                *  3: WAITING_SCBS list on card (for commands that are started
                   4801:                *     but haven't yet made it to the device)
                   4802:                */
                   4803:               aic7xxx_search_qinfifo(p, target, channel, lun,
                   4804:                 SCB_LIST_NULL, 0, TRUE,
                   4805:                 &p->delayed_scbs[tindex]);
                   4806:               next_scbp = p->waiting_scbs.head;
                   4807:               while ( next_scbp != NULL )
                   4808:               {
                   4809:                 prev_scbp = next_scbp;
                   4810:                 next_scbp = next_scbp->q_next;
                   4811:                 if ( aic7xxx_match_scb(p, prev_scbp, target, channel, lun,
                   4812:                      SCB_LIST_NULL) )
                   4813:                 {
                   4814:                   scbq_remove(&p->waiting_scbs, prev_scbp);
                   4815:                   scbq_insert_tail(&p->delayed_scbs[tindex],
                   4816:                     prev_scbp);
                   4817:                 }
                   4818:               }
                   4819:               next_scbp = NULL;
                   4820:               active_hscb = aic_inb(p, SCBPTR);
                   4821:               prev_hscb = next_hscb = scb_index = SCB_LIST_NULL;
                   4822:               next_hscb = aic_inb(p, WAITING_SCBH);
                   4823:               while (next_hscb != SCB_LIST_NULL)
                   4824:               {
                   4825:                 aic_outb(p, next_hscb, SCBPTR);
                   4826:                 scb_index = aic_inb(p, SCB_TAG);
                   4827:                 if (scb_index < p->scb_data->numscbs)
                   4828:                 {
                   4829:                   next_scbp = p->scb_data->scb_array[scb_index];
                   4830:                   if (aic7xxx_match_scb(p, next_scbp, target, channel, lun,
                   4831:                       SCB_LIST_NULL) )
                   4832:                   {
                   4833:                     if (next_scbp->flags & SCB_WAITINGQ)
                   4834:                     {
                   4835:                       p->dev_active_cmds[tindex]++;
                   4836:                       p->activescbs--;
                   4837:                       scbq_remove(&p->delayed_scbs[tindex], next_scbp);
                   4838:                       scbq_remove(&p->waiting_scbs, next_scbp);
                   4839:                     }
                   4840:                     scbq_insert_head(&p->delayed_scbs[tindex],
                   4841:                       next_scbp);
                   4842:                     next_scbp->flags |= SCB_WAITINGQ;
                   4843:                     p->dev_active_cmds[tindex]--;
                   4844:                     p->activescbs--;
                   4845:                     next_hscb = aic_inb(p, SCB_NEXT);
                   4846:                     aic_outb(p, 0, SCB_CONTROL);
                   4847:                     aic_outb(p, SCB_LIST_NULL, SCB_TAG);
                   4848:                     aic7xxx_add_curscb_to_free_list(p);
                   4849:                     if (prev_hscb == SCB_LIST_NULL)
                   4850:                     {
                   4851:                       /* We were first on the list,
                   4852:                        * so we kill the selection
                   4853:                        * hardware.  Let the sequencer
                   4854:                        * re-init the hardware itself
                   4855:                        */
                   4856:                       aic_outb(p, aic_inb(p, SCSISEQ) & ~ENSELO, SCSISEQ);
                   4857:                       aic_outb(p, CLRSELTIMEO, CLRSINT1);
                   4858:                       aic_outb(p, next_hscb, WAITING_SCBH);
                   4859:                     }
                   4860:                     else
                   4861:                     {
                   4862:                       aic_outb(p, prev_hscb, SCBPTR);
                   4863:                       aic_outb(p, next_hscb, SCB_NEXT);
                   4864:                     }
                   4865:                   }
                   4866:                   else
                   4867:                   {
                   4868:                     prev_hscb = next_hscb;
                   4869:                     next_hscb = aic_inb(p, SCB_NEXT);
                   4870:                   }
                   4871:                 } /* scb_index >= p->scb_data->numscbs */
                   4872:               }
                   4873:               aic_outb(p, active_hscb, SCBPTR);
                   4874:               if (scb->flags & SCB_WAITINGQ)
                   4875:               {
                   4876:                 scbq_remove(&p->delayed_scbs[tindex], scb);
                   4877:                 scbq_remove(&p->waiting_scbs, scb);
                   4878:                 p->dev_active_cmds[tindex]++;
                   4879:                 p->activescbs++;
                   4880:               }
                   4881:               scbq_insert_head(&p->delayed_scbs[tindex], scb);
                   4882:               p->dev_active_cmds[tindex]--;
                   4883:               p->activescbs--;
                   4884:               scb->flags |= SCB_WAITINGQ | SCB_WAS_BUSY;
1.1.1.2 ! root     4885: 
        !          4886:               if ( !(p->dev_timer_active & (0x01 << tindex)) )
1.1       root     4887:               {
1.1.1.2 ! root     4888:                 p->dev_timer_active |= (0x01 << tindex);
1.1       root     4889:                 if ( p->dev_active_cmds[tindex] )
                   4890:                 {
1.1.1.2 ! root     4891:                   p->dev_expires[tindex] = jiffies + HZ;
1.1       root     4892:                 }
                   4893:                 else
                   4894:                 {
1.1.1.2 ! root     4895:                   p->dev_expires[tindex] = jiffies + (HZ / 10);
        !          4896:                 }
        !          4897:                 if ( !(p->dev_timer_active & (0x01 << MAX_TARGETS)) )
        !          4898:                 {
        !          4899:                   p->dev_timer.expires = p->dev_expires[tindex];
        !          4900:                   p->dev_timer_active |= (0x01 << MAX_TARGETS);
        !          4901:                   add_timer(&p->dev_timer);
        !          4902:                 }
        !          4903:                 else if ( time_after_eq(p->dev_timer.expires,
        !          4904:                                         p->dev_expires[tindex]) )
        !          4905:                 {
        !          4906:                   del_timer(&p->dev_timer);
        !          4907:                   p->dev_timer.expires = p->dev_expires[tindex];
        !          4908:                   add_timer(&p->dev_timer);
1.1       root     4909:                 }
                   4910:               }
                   4911: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   4912:               if (aic7xxx_verbose & VERBOSE_MINOR_ERROR)
                   4913:               {
                   4914:                 if (queue_flag)
                   4915:                   printk(INFO_LEAD "Queue full received; queue depth %d, "
                   4916:                     "active %d\n", p->host_no, CTL_OF_SCB(scb),
                   4917:                     p->dev_max_queue_depth[tindex],
                   4918:                     p->dev_active_cmds[tindex]);
                   4919:                 else
                   4920:                   printk(INFO_LEAD "Target busy\n", p->host_no, CTL_OF_SCB(scb));
                   4921: 
                   4922:               }
                   4923: #endif
                   4924:               if (queue_flag)
                   4925:               {
1.1.1.2 ! root     4926:                 p->dev_temp_queue_depth[tindex] =
1.1       root     4927:                   p->dev_active_cmds[tindex];
                   4928:                 if ( p->dev_last_queue_full[tindex] !=
                   4929:                      p->dev_active_cmds[tindex] )
                   4930:                 {
1.1.1.2 ! root     4931:                   p->dev_last_queue_full[tindex] =
1.1       root     4932:                       p->dev_active_cmds[tindex];
                   4933:                   p->dev_last_queue_full_count[tindex] = 0;
                   4934:                 }
                   4935:                 else
                   4936:                 {
                   4937:                   p->dev_last_queue_full_count[tindex]++;
                   4938:                 }
                   4939:                 if ( (p->dev_last_queue_full_count[tindex] > 14) &&
                   4940:                      (p->dev_active_cmds[tindex] > 4) )
                   4941:                 {
                   4942:                   if (aic7xxx_verbose & VERBOSE_NEGOTIATION2)
                   4943:                     printk(INFO_LEAD "Queue depth reduced to %d\n", p->host_no,
                   4944:                       CTL_OF_SCB(scb), p->dev_active_cmds[tindex]);
1.1.1.2 ! root     4945:                   p->dev_max_queue_depth[tindex] =
1.1       root     4946:                       p->dev_active_cmds[tindex];
                   4947:                   p->dev_last_queue_full[tindex] = 0;
                   4948:                   p->dev_last_queue_full_count[tindex] = 0;
                   4949:                 }
1.1.1.2 ! root     4950:                 else
        !          4951:                 {
        !          4952:                   p->dev_flags[tindex] |= DEVICE_WAS_BUSY;
        !          4953:                 }
1.1       root     4954:               }
                   4955:               break;
                   4956:             }
1.1.1.2 ! root     4957: 
1.1       root     4958:             default:
                   4959:               if (aic7xxx_verbose & VERBOSE_SEQINT)
                   4960:                 printk(INFO_LEAD "Unexpected target status 0x%x.\n", p->host_no,
                   4961:                      CTL_OF_SCB(scb), scb->hscb->target_status);
                   4962:               if (!aic7xxx_error(cmd))
                   4963:               {
                   4964:                 aic7xxx_error(cmd) = DID_RETRY_COMMAND;
                   4965:               }
                   4966:               break;
                   4967:           }  /* end switch */
                   4968:         }  /* end else of */
                   4969:       }
                   4970:       break;
                   4971: 
                   4972:     case AWAITING_MSG:
                   4973:       {
                   4974:         unsigned char scb_index, msg_out;
                   4975: 
                   4976:         scb_index = aic_inb(p, SCB_TAG);
                   4977:         msg_out = aic_inb(p, MSG_OUT);
                   4978:         scb = p->scb_data->scb_array[scb_index];
                   4979:         p->msg_index = p->msg_len = 0;
                   4980:         /*
                   4981:          * This SCB had a MK_MESSAGE set in its control byte informing
                   4982:          * the sequencer that we wanted to send a special message to
                   4983:          * this target.
                   4984:          */
                   4985: 
                   4986:         if ( !(scb->flags & SCB_DEVICE_RESET) &&
                   4987:               (aic_inb(p, MSG_OUT) == MSG_IDENTIFYFLAG) &&
                   4988:               (scb->hscb->control & TAG_ENB) )
                   4989:         {
                   4990:           p->msg_buf[p->msg_index++] = scb->tag_action;
                   4991:           p->msg_buf[p->msg_index++] = scb->hscb->tag;
                   4992:           p->msg_len += 2;
                   4993:         }
                   4994: 
                   4995:         if (scb->flags & SCB_DEVICE_RESET)
                   4996:         {
                   4997:           p->msg_buf[p->msg_index++] = MSG_BUS_DEV_RESET;
                   4998:           p->msg_len++;
                   4999:           if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   5000:             printk(INFO_LEAD "Bus device reset mailed.\n",
                   5001:                  p->host_no, CTL_OF_SCB(scb));
                   5002:         }
                   5003:         else if (scb->flags & SCB_ABORT)
                   5004:         {
                   5005:           if (scb->tag_action)
                   5006:           {
                   5007:             p->msg_buf[p->msg_index++] = MSG_ABORT_TAG;
                   5008:           }
                   5009:           else
                   5010:           {
                   5011:             p->msg_buf[p->msg_index++] = MSG_ABORT;
                   5012:           }
                   5013:           p->msg_len++;
                   5014:           if (aic7xxx_verbose & VERBOSE_ABORT_PROCESS)
                   5015:             printk(INFO_LEAD "Abort message mailed.\n", p->host_no,
                   5016:               CTL_OF_SCB(scb));
                   5017:         }
                   5018:         else if (scb->flags & SCB_MSGOUT_WDTR)
                   5019:         {
                   5020: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   5021:           if (aic7xxx_verbose > 0xffff)
                   5022:             printk(INFO_LEAD "Sending WDTR message.\n", p->host_no,
                   5023:                    CTL_OF_SCB(scb));
                   5024: #endif
                   5025:           aic7xxx_construct_wdtr(p,
                   5026:             p->transinfo[TARGET_INDEX(scb->cmd)].goal_width);
                   5027:         }
                   5028:         else if (scb->flags & SCB_MSGOUT_SDTR)
                   5029:         {
                   5030:           unsigned int max_sync, period;
                   5031:           /*
                   5032:            * We need to set an accurate goal_offset instead of
                   5033:            * the ridiculously high one we default to.  We should
1.1.1.2 ! root     5034:            * now know if we are wide.  Plus, the WDTR code will
1.1       root     5035:            * set our goal_offset for us as well.
                   5036:            */
                   5037:           if (p->transinfo[tindex].goal_offset)
                   5038:           {
                   5039:             if (p->features & AHC_ULTRA2)
                   5040:               p->transinfo[tindex].goal_offset = MAX_OFFSET_ULTRA2;
                   5041:             else if (p->transinfo[tindex].cur_width == MSG_EXT_WDTR_BUS_16_BIT)
                   5042:               p->transinfo[tindex].goal_offset = MAX_OFFSET_16BIT;
                   5043:             else
                   5044:               p->transinfo[tindex].goal_offset = MAX_OFFSET_8BIT;
                   5045:           }
                   5046:           /*
                   5047:            * Now that the device is selected, use the bits in SBLKCTL and
                   5048:            * SSTAT2 to determine the max sync rate for this device.
                   5049:            */
                   5050:           if (p->features & AHC_ULTRA2)
                   5051:           {
                   5052:             if ( (aic_inb(p, SBLKCTL) & ENAB40) &&
                   5053:                 !(aic_inb(p, SSTAT2) & EXP_ACTIVE) )
                   5054:             {
                   5055:               max_sync = AHC_SYNCRATE_ULTRA2;
                   5056:             }
                   5057:             else
                   5058:             {
                   5059:               max_sync = AHC_SYNCRATE_ULTRA;
                   5060:             }
                   5061:           }
                   5062:           else if (p->features & AHC_ULTRA)
                   5063:           {
                   5064:             max_sync = AHC_SYNCRATE_ULTRA;
                   5065:           }
                   5066:           else
                   5067:           {
                   5068:             max_sync = AHC_SYNCRATE_FAST;
                   5069:           }
                   5070:           period = p->transinfo[tindex].goal_period;
                   5071:           aic7xxx_find_syncrate(p, &period, max_sync);
                   5072: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   5073:           if (aic7xxx_verbose > 0xffff)
                   5074:             printk(INFO_LEAD "Sending SDTR %d/%d message.\n", p->host_no,
                   5075:                    CTL_OF_SCB(scb),
                   5076:                    p->transinfo[tindex].goal_period,
                   5077:                    p->transinfo[tindex].goal_offset);
                   5078: #endif
                   5079:           aic7xxx_construct_sdtr(p, period,
                   5080:             p->transinfo[tindex].goal_offset);
                   5081:         }
1.1.1.2 ! root     5082:         else
1.1       root     5083:         {
                   5084:           sti();
                   5085:           panic("aic7xxx: AWAITING_MSG for an SCB that does "
                   5086:                 "not have a waiting message.\n");
                   5087:         }
                   5088:         /*
                   5089:          * We've set everything up to send our message, now to actually do
                   5090:          * so we need to enable reqinit interrupts and let the interrupt
                   5091:          * handler do the rest.  We don't want to unpause the sequencer yet
                   5092:          * though so we'll return early.  We also have to make sure that
                   5093:          * we clear the SEQINT *BEFORE* we set the REQINIT handler active
                   5094:          * or else it's possible on VLB cards to loose the first REQINIT
                   5095:          * interrupt.  Edge triggered EISA cards could also loose this
                   5096:          * interrupt, although PCI and level triggered cards should not
                   5097:          * have this problem since they continually interrupt the kernel
                   5098:          * until we take care of the situation.
                   5099:          */
                   5100:         scb->flags |= SCB_MSGOUT_SENT;
                   5101:         p->msg_index = 0;
                   5102:         p->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
                   5103:         p->flags |= AHC_HANDLING_REQINITS;
                   5104:         aic_outb(p, aic_inb(p, SIMODE1) | ENREQINIT, SIMODE1);
                   5105:         return;
                   5106:       }
                   5107:       break;
                   5108: 
                   5109:     case DATA_OVERRUN:
                   5110:       {
                   5111:         unsigned char scb_index = aic_inb(p, SCB_TAG);
                   5112:         unsigned char lastphase = aic_inb(p, LASTPHASE);
                   5113:         unsigned int i;
                   5114: 
                   5115:         scb = (p->scb_data->scb_array[scb_index]);
                   5116:         /*
                   5117:          * XXX - What do we really want to do on an overrun?  The
                   5118:          *       mid-level SCSI code should handle this, but for now,
                   5119:          *       we'll just indicate that the command should retried.
1.1.1.2 ! root     5120:          *    If we retrieved sense info on this target, then the
1.1       root     5121:          *    base SENSE info should have been saved prior to the
                   5122:          *    overrun error.  In that case, we return DID_OK and let
                   5123:          *    the mid level code pick up on the sense info.  Otherwise
                   5124:          *    we return DID_ERROR so the command will get retried.
                   5125:          */
                   5126:         if ( !(scb->flags & SCB_SENSE) )
                   5127:         {
                   5128:           printk(WARN_LEAD "Data overrun detected in %s phase, tag %d;\n",
1.1.1.2 ! root     5129:             p->host_no, CTL_OF_SCB(scb),
1.1       root     5130:             (lastphase == P_DATAIN) ? "Data-In" : "Data-Out", scb->hscb->tag);
                   5131:           printk(KERN_WARNING "  %s seen Data Phase. Length=%d, NumSGs=%d.\n",
                   5132:             (aic_inb(p, SEQ_FLAGS) & DPHASE) ? "Have" : "Haven't",
                   5133:             scb->sg_length, scb->sg_count);
                   5134:           for (i = 0; i < scb->sg_count; i++)
                   5135:           {
                   5136:             printk(KERN_WARNING "     sg[%d] - Addr 0x%x : Length %d\n",
1.1.1.2 ! root     5137:                  i,
1.1       root     5138:                  le32_to_cpu(scb->sg_list[i].address),
                   5139:                  le32_to_cpu(scb->sg_list[i].length) );
                   5140:           }
                   5141:           aic7xxx_error(scb->cmd) = DID_ERROR;
                   5142:         }
                   5143:         else
                   5144:           printk(INFO_LEAD "Data Overrun during SEND_SENSE operation.\n",
                   5145:             p->host_no, CTL_OF_SCB(scb));
                   5146:       }
                   5147:       break;
                   5148: 
1.1.1.2 ! root     5149: #if AIC7XXX_NOT_YET
1.1       root     5150:     case TRACEPOINT:
                   5151:       {
                   5152:         printk(INFO_LEAD "Tracepoint #1 reached.\n", p->host_no, channel,
                   5153:           target, lun);
                   5154:       }
                   5155:       break;
                   5156: 
                   5157:     case TRACEPOINT2:
                   5158:       {
                   5159:         printk(INFO_LEAD "Tracepoint #2 reached.\n", p->host_no, channel,
                   5160:           target, lun);
                   5161:       }
                   5162:       break;
                   5163: 
                   5164:     /* XXX Fill these in later */
                   5165:     case MSG_BUFFER_BUSY:
                   5166:       printk("aic7xxx: Message buffer busy.\n");
                   5167:       break;
                   5168:     case MSGIN_PHASEMIS:
                   5169:       printk("aic7xxx: Message-in phasemis.\n");
                   5170:       break;
                   5171: #endif
                   5172: 
                   5173:     default:                   /* unknown */
                   5174:       printk(WARN_LEAD "Unknown SEQINT, INTSTAT 0x%x, SCSISIGI 0x%x.\n",
                   5175:              p->host_no, channel, target, lun, intstat,
                   5176:              aic_inb(p, SCSISIGI));
                   5177:       break;
                   5178:   }
                   5179: 
                   5180:   /*
                   5181:    * Clear the sequencer interrupt and unpause the sequencer.
                   5182:    */
                   5183:   unpause_sequencer(p, /* unpause always */ TRUE);
                   5184: }
                   5185: 
                   5186: /*+F*************************************************************************
                   5187:  * Function:
                   5188:  *   aic7xxx_parse_msg
                   5189:  *
                   5190:  * Description:
                   5191:  *   Parses incoming messages into actions on behalf of
                   5192:  *   aic7xxx_handle_reqinit
                   5193:  *_F*************************************************************************/
                   5194: static int
                   5195: aic7xxx_parse_msg(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                   5196: {
                   5197:   int reject, reply, done;
                   5198:   unsigned char target_scsirate, tindex;
                   5199:   unsigned short target_mask;
                   5200:   unsigned char target, channel, lun;
                   5201: 
                   5202:   target = scb->cmd->target;
                   5203:   channel = scb->cmd->channel;
                   5204:   lun = scb->cmd->lun;
                   5205:   reply = reject = done = FALSE;
                   5206:   tindex = TARGET_INDEX(scb->cmd);
                   5207:   target_scsirate = aic_inb(p, TARG_SCSIRATE + tindex);
                   5208:   target_mask = (0x01 << tindex);
                   5209: 
                   5210:   /*
                   5211:    * Parse as much of the message as is availible,
                   5212:    * rejecting it if we don't support it.  When
                   5213:    * the entire message is availible and has been
                   5214:    * handled, return TRUE indicating that we have
                   5215:    * parsed an entire message.
                   5216:    */
                   5217: 
                   5218:   if (p->msg_buf[0] != MSG_EXTENDED)
                   5219:   {
                   5220:     reject = TRUE;
                   5221:   }
                   5222: 
                   5223:   /*
                   5224:    * Just accept the length byte outright and perform
                   5225:    * more checking once we know the message type.
                   5226:    */
                   5227: 
                   5228:   if ( !reject && (p->msg_len > 2) )
                   5229:   {
                   5230:     switch(p->msg_buf[2])
                   5231:     {
                   5232:       case MSG_EXT_SDTR:
                   5233:       {
                   5234:         unsigned int period, offset;
                   5235:         unsigned char maxsync, saved_offset;
                   5236:         struct aic7xxx_syncrate *syncrate;
1.1.1.2 ! root     5237: 
1.1       root     5238:         if (p->msg_buf[1] != MSG_EXT_SDTR_LEN)
                   5239:         {
                   5240:           reject = TRUE;
                   5241:           break;
                   5242:         }
                   5243: 
                   5244:         if (p->msg_len < (MSG_EXT_SDTR_LEN + 2))
                   5245:         {
                   5246:           break;
                   5247:         }
                   5248: 
                   5249:         period = p->msg_buf[3];
                   5250:         saved_offset = offset = p->msg_buf[4];
                   5251: 
                   5252:         if (p->features & AHC_ULTRA2)
                   5253:         {
                   5254:           if ( (aic_inb(p, SBLKCTL) & ENAB40) &&
                   5255:                !(aic_inb(p, SSTAT2) & EXP_ACTIVE) )
                   5256:           {
                   5257:             maxsync = AHC_SYNCRATE_ULTRA2;
                   5258:           }
                   5259:           else
                   5260:           {
                   5261:             maxsync = AHC_SYNCRATE_ULTRA;
                   5262:           }
                   5263:         }
                   5264:         else if (p->features & AHC_ULTRA)
                   5265:         {
                   5266:           maxsync = AHC_SYNCRATE_ULTRA;
                   5267:         }
                   5268:         else
                   5269:         {
                   5270:           maxsync = AHC_SYNCRATE_FAST;
                   5271:         }
1.1.1.2 ! root     5272:         /*
        !          5273:          * We might have a device that is starting negotiation with us
        !          5274:          * before we can start up negotiation with it....be prepared to
        !          5275:          * have a device ask for a higher speed then we want to give it
        !          5276:          * in that case
        !          5277:          */
        !          5278:         if ( (scb->flags & (SCB_MSGOUT_SENT|SCB_MSGOUT_SDTR)) !=
        !          5279:              (SCB_MSGOUT_SENT|SCB_MSGOUT_SDTR) )
1.1       root     5280:         {
1.1.1.2 ! root     5281:           if (!(p->dev_flags[tindex] & DEVICE_SCANNED))
        !          5282:           {
        !          5283:             /*
        !          5284:              * Not only is the device starting this up, but it also hasn't
        !          5285:              * been scanned yet, so this would likely be our TUR or our
        !          5286:              * INQUIRY command at scan time, so we need to use the
        !          5287:              * settings from the SEEPROM if they existed.  Of course, even
        !          5288:              * if we didn't find a SEEPROM, we stuffed default values into
        !          5289:              * the user settings anyway, so use those in all cases.
        !          5290:              */
        !          5291:             p->transinfo[tindex].goal_period =
        !          5292:               p->transinfo[tindex].user_period;
        !          5293:             p->transinfo[tindex].goal_offset =
        !          5294:               p->transinfo[tindex].user_offset;
        !          5295:             p->needsdtr_copy |= target_mask;
        !          5296:           }
        !          5297:           if ( !p->transinfo[tindex].goal_offset )
        !          5298:             period = 255;
        !          5299:           if ( p->transinfo[tindex].goal_period > period )
        !          5300:             period = p->transinfo[tindex].goal_period;
1.1       root     5301:         }
1.1.1.2 ! root     5302: 
1.1       root     5303:         syncrate = aic7xxx_find_syncrate(p, &period, maxsync);
                   5304:         aic7xxx_validate_offset(p, syncrate, &offset,
                   5305:                                 target_scsirate & WIDEXFER);
                   5306:         aic7xxx_set_syncrate(p, syncrate, target, channel, period,
                   5307:                              offset, AHC_TRANS_ACTIVE|AHC_TRANS_CUR);
                   5308: 
1.1.1.2 ! root     5309:         /*
        !          5310:          * Did we drop to async?  If so, are we sending a reply?  If we are,
        !          5311:          * then we have to make sure that the reply value reflects the proper
        !          5312:          * settings so we need to set the goal values according to what
        !          5313:          * we need to send.
        !          5314:          */
        !          5315:         if ( (offset == 0) || (offset != saved_offset) ||
        !          5316:              ((scb->flags & (SCB_MSGOUT_SENT|SCB_MSGOUT_SDTR)) !=
        !          5317:               (SCB_MSGOUT_SENT|SCB_MSGOUT_SDTR) ) )
1.1       root     5318:         {
                   5319:           aic7xxx_set_syncrate(p, syncrate, target, channel, period,
1.1.1.2 ! root     5320:                                offset, AHC_TRANS_GOAL|AHC_TRANS_QUITE);
        !          5321:           if ( offset == 0 )
        !          5322:           {
        !          5323:             p->needsdtr_copy &= ~target_mask;
        !          5324:           }
1.1       root     5325:         }
1.1.1.2 ! root     5326: 
1.1       root     5327:         /*
                   5328:          * Did we start this, if not, or if we went to low and had to
                   5329:          * go async, then send an SDTR back to the target
                   5330:          */
                   5331:         p->needsdtr &= ~target_mask;
                   5332:         p->sdtr_pending &= ~target_mask;
                   5333:         if ( ((scb->flags & (SCB_MSGOUT_SENT|SCB_MSGOUT_SDTR)) ==
                   5334:               (SCB_MSGOUT_SENT|SCB_MSGOUT_SDTR)) &&
                   5335:              (offset == saved_offset) )
                   5336:         {
                   5337:           scb->flags &= ~SCB_MSGOUT_BITS;
                   5338:         }
                   5339:         else
                   5340:         {
                   5341:           scb->flags &= ~SCB_MSGOUT_BITS;
                   5342:           scb->flags |= SCB_MSGOUT_SDTR;
                   5343:           aic_outb(p, HOST_MSG, MSG_OUT);
                   5344:           aic_outb(p, aic_inb(p, SCSISIGO) | ATNO, SCSISIGO);
                   5345:         }
                   5346:         done = TRUE;
                   5347:         break;
                   5348:       }
                   5349:       case MSG_EXT_WDTR:
                   5350:       {
                   5351:         unsigned char bus_width;
1.1.1.2 ! root     5352: 
1.1       root     5353:         if (p->msg_buf[1] != MSG_EXT_WDTR_LEN)
                   5354:         {
                   5355:           reject = TRUE;
                   5356:           break;
                   5357:         }
                   5358: 
                   5359:         if (p->msg_len < (MSG_EXT_WDTR_LEN + 2))
                   5360:         {
                   5361:           break;
                   5362:         }
                   5363: 
                   5364:         bus_width = p->msg_buf[3];
                   5365:         if ( (scb->flags & (SCB_MSGOUT_SENT|SCB_MSGOUT_WDTR)) ==
                   5366:              (SCB_MSGOUT_SENT|SCB_MSGOUT_WDTR) )
                   5367:         {
                   5368:           switch(bus_width)
                   5369:           {
                   5370:             default:
                   5371:             {
                   5372:               reject = TRUE;
                   5373:               if ( (aic7xxx_verbose & VERBOSE_NEGOTIATION2) &&
                   5374:                    ((p->dev_flags[tindex] & DEVICE_PRINT_WDTR) ||
                   5375:                     (aic7xxx_verbose > 0xffff)) )
                   5376:               {
                   5377:                 printk(INFO_LEAD "Requesting %d bit transfers, rejecting.\n",
                   5378:                   p->host_no, CTL_OF_SCB(scb), 8 * (0x01 << bus_width));
                   5379:                 p->dev_flags[tindex] &= ~DEVICE_PRINT_WDTR;
                   5380:               }
                   5381:             } /* We fall through on purpose */
                   5382:             case MSG_EXT_WDTR_BUS_8_BIT:
                   5383:             {
                   5384:               bus_width = MSG_EXT_WDTR_BUS_8_BIT;
                   5385:               p->needwdtr_copy &= ~target_mask;
                   5386:               break;
                   5387:             }
                   5388:             case MSG_EXT_WDTR_BUS_16_BIT:
                   5389:             {
                   5390:               break;
                   5391:             }
                   5392:           }
                   5393:           scb->flags &= ~SCB_MSGOUT_BITS;
                   5394:           p->wdtr_pending &= ~target_mask;
                   5395:           p->needwdtr &= ~target_mask;
                   5396:         }
                   5397:         else
                   5398:         {
                   5399:           scb->flags &= ~SCB_MSGOUT_BITS;
                   5400:           scb->flags |= SCB_MSGOUT_WDTR;
                   5401:           reply = TRUE;
                   5402:           if ( !(p->dev_flags[tindex] & DEVICE_SCANNED) )
                   5403:           {
1.1.1.2 ! root     5404:             /*
1.1       root     5405:              * Well, we now know the WDTR and SYNC caps of this device since
                   5406:              * it contacted us first, mark it as such and copy the user stuff
                   5407:              * over to the goal stuff.
                   5408:              */
                   5409:             p->transinfo[tindex].goal_period =
                   5410:               p->transinfo[tindex].user_period;
                   5411:             p->transinfo[tindex].goal_offset =
                   5412:               p->transinfo[tindex].user_offset;
1.1.1.2 ! root     5413:             p->transinfo[tindex].goal_width =
        !          5414:               p->transinfo[tindex].user_width;
1.1       root     5415:             p->needwdtr_copy |= target_mask;
                   5416:             p->needsdtr_copy |= target_mask;
                   5417:           }
                   5418:           switch(bus_width)
                   5419:           {
                   5420:             default:
                   5421:             {
                   5422:               if ( (p->features & AHC_WIDE) &&
                   5423:                    (p->transinfo[tindex].goal_width ==
                   5424:                     MSG_EXT_WDTR_BUS_16_BIT) )
                   5425:               {
                   5426:                 bus_width = MSG_EXT_WDTR_BUS_16_BIT;
                   5427:                 break;
                   5428:               }
                   5429:             } /* Fall through if we aren't a wide card */
                   5430:             case MSG_EXT_WDTR_BUS_8_BIT:
                   5431:             {
                   5432:               p->needwdtr_copy &= ~target_mask;
                   5433:               bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1.1.1.2 ! root     5434:               aic7xxx_set_width(p, target, channel, lun, bus_width,
        !          5435:                                 AHC_TRANS_GOAL|AHC_TRANS_QUITE);
1.1       root     5436:               break;
                   5437:             }
                   5438:           }
                   5439:           p->needwdtr &= ~target_mask;
                   5440:           p->wdtr_pending &= ~target_mask;
                   5441:           aic_outb(p, HOST_MSG, MSG_OUT);
                   5442:           aic_outb(p, aic_inb(p, SCSISIGO) | ATNO, SCSISIGO);
                   5443:         }
                   5444:         aic7xxx_set_width(p, target, channel, lun, bus_width,
                   5445:                           AHC_TRANS_ACTIVE|AHC_TRANS_CUR);
1.1.1.2 ! root     5446: 
1.1       root     5447:         /*
                   5448:          * By virtue of the SCSI spec, a WDTR message negates any existing
                   5449:          * SDTR negotiations.  So, even if needsdtr isn't marked for this
                   5450:          * device, we still have to do a new SDTR message if the device
                   5451:          * supports SDTR at all.  Therefore, we check needsdtr_copy instead
                   5452:          * of needstr.
                   5453:          */
                   5454:         aic7xxx_set_syncrate(p, NULL, target, channel, 0, 0,
                   5455:                              AHC_TRANS_ACTIVE|AHC_TRANS_CUR|AHC_TRANS_QUITE);
                   5456:         if ( (p->needsdtr_copy & target_mask) &&
                   5457:             !(p->sdtr_pending & target_mask))
                   5458:         {
                   5459:           p->needsdtr |= target_mask;
                   5460:           if ( !reject && !reply )
                   5461:           {
                   5462:             scb->flags &= ~SCB_MSGOUT_WDTR;
                   5463:             if (p->transinfo[tindex].goal_period)
                   5464:             {
                   5465:               p->sdtr_pending |= target_mask;
                   5466:               scb->flags |= SCB_MSGOUT_SDTR;
                   5467:               aic_outb(p, HOST_MSG, MSG_OUT);
                   5468:               aic_outb(p, aic_inb(p, SCSISIGO) | ATNO, SCSISIGO);
                   5469:             }
                   5470:           }
                   5471:         }
                   5472:         done = TRUE;
                   5473:         break;
                   5474:       }
                   5475:       default:
                   5476:       {
                   5477:         reject = TRUE;
                   5478:         break;
                   5479:       }
                   5480:     } /* end of switch(p->msg_type) */
                   5481:   } /* end of if (!reject && (p->msg_len > 2)) */
                   5482: 
                   5483:   if (reject)
                   5484:   {
                   5485:     aic_outb(p, MSG_MESSAGE_REJECT, MSG_OUT);
                   5486:     aic_outb(p, aic_inb(p, SCSISIGO) | ATNO, SCSISIGO);
                   5487:     done = TRUE;
                   5488:   }
                   5489:   return(done);
                   5490: }
                   5491: 
                   5492: 
                   5493: /*+F*************************************************************************
                   5494:  * Function:
                   5495:  *   aic7xxx_handle_reqinit
                   5496:  *
                   5497:  * Description:
                   5498:  *   Interrupt handler for REQINIT interrupts (used to transfer messages to
                   5499:  *    and from devices).
                   5500:  *_F*************************************************************************/
                   5501: static void
                   5502: aic7xxx_handle_reqinit(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                   5503: {
                   5504:   unsigned char lastbyte;
                   5505:   unsigned char phasemis;
                   5506:   int done = FALSE;
                   5507: 
                   5508:   switch(p->msg_type)
                   5509:   {
                   5510:     case MSG_TYPE_INITIATOR_MSGOUT:
                   5511:       {
                   5512:         if (p->msg_len == 0)
                   5513:           panic("aic7xxx: REQINIT with no active message!\n");
                   5514: 
                   5515:         lastbyte = (p->msg_index == (p->msg_len - 1));
                   5516:         phasemis = ( aic_inb(p, SCSISIGI) & PHASE_MASK) != P_MESGOUT;
                   5517: 
                   5518:         if (lastbyte || phasemis)
                   5519:         {
                   5520:           /* Time to end the message */
                   5521:           p->msg_len = 0;
                   5522:           p->msg_type = MSG_TYPE_NONE;
                   5523:           /*
                   5524:            * NOTE-TO-MYSELF: If you clear the REQINIT after you
                   5525:            * disable REQINITs, then cases of REJECT_MSG stop working
                   5526:            * and hang the bus
                   5527:            */
                   5528:           aic_outb(p, aic_inb(p, SIMODE1) & ~ENREQINIT, SIMODE1);
                   5529:           aic_outb(p, CLRSCSIINT, CLRINT);
                   5530:           p->flags &= ~AHC_HANDLING_REQINITS;
                   5531: 
                   5532:           if (phasemis == 0)
                   5533:           {
                   5534:             aic_outb(p, p->msg_buf[p->msg_index], SINDEX);
                   5535:             aic_outb(p, 0, RETURN_1);
                   5536: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   5537:             if (aic7xxx_verbose > 0xffff)
                   5538:               printk(INFO_LEAD "Completed sending of REQINIT message.\n",
                   5539:                      p->host_no, CTL_OF_SCB(scb));
                   5540: #endif
                   5541:           }
                   5542:           else
                   5543:           {
                   5544:             aic_outb(p, MSGOUT_PHASEMIS, RETURN_1);
                   5545: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   5546:             if (aic7xxx_verbose > 0xffff)
                   5547:               printk(INFO_LEAD "PHASEMIS while sending REQINIT message.\n",
                   5548:                      p->host_no, CTL_OF_SCB(scb));
                   5549: #endif
                   5550:           }
                   5551:           unpause_sequencer(p, TRUE);
                   5552:         }
                   5553:         else
                   5554:         {
                   5555:           /*
                   5556:            * Present the byte on the bus (clearing REQINIT) but don't
                   5557:            * unpause the sequencer.
                   5558:            */
                   5559:           aic_outb(p, CLRREQINIT, CLRSINT1);
                   5560:           aic_outb(p, CLRSCSIINT, CLRINT);
                   5561:           aic_outb(p,  p->msg_buf[p->msg_index++], SCSIDATL);
                   5562:         }
                   5563:         break;
                   5564:       }
                   5565:     case MSG_TYPE_INITIATOR_MSGIN:
                   5566:       {
                   5567:         phasemis = ( aic_inb(p, SCSISIGI) & PHASE_MASK ) != P_MESGIN;
                   5568: 
                   5569:         if (phasemis == 0)
                   5570:         {
                   5571:           p->msg_len++;
                   5572:           /* Pull the byte in without acking it */
                   5573:           p->msg_buf[p->msg_index] = aic_inb(p, SCSIBUSL);
                   5574:           done = aic7xxx_parse_msg(p, scb);
                   5575:           /* Ack the byte */
                   5576:           aic_outb(p, CLRREQINIT, CLRSINT1);
                   5577:           aic_outb(p, CLRSCSIINT, CLRINT);
                   5578:           aic_inb(p, SCSIDATL);
                   5579:           p->msg_index++;
                   5580:         }
                   5581:         if (phasemis || done)
                   5582:         {
                   5583: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   5584:           if (aic7xxx_verbose > 0xffff)
                   5585:           {
                   5586:             if (phasemis)
                   5587:               printk(INFO_LEAD "PHASEMIS while receiving REQINIT message.\n",
                   5588:                      p->host_no, CTL_OF_SCB(scb));
                   5589:             else
                   5590:               printk(INFO_LEAD "Completed receipt of REQINIT message.\n",
                   5591:                      p->host_no, CTL_OF_SCB(scb));
                   5592:           }
                   5593: #endif
                   5594:           /* Time to end our message session */
                   5595:           p->msg_len = 0;
                   5596:           p->msg_type = MSG_TYPE_NONE;
                   5597:           aic_outb(p, aic_inb(p, SIMODE1) & ~ENREQINIT, SIMODE1);
                   5598:           aic_outb(p, CLRSCSIINT, CLRINT);
                   5599:           p->flags &= ~AHC_HANDLING_REQINITS;
                   5600:           unpause_sequencer(p, TRUE);
                   5601:         }
                   5602:         break;
                   5603:       }
                   5604:     default:
                   5605:       {
                   5606:         panic("aic7xxx: Unknown REQINIT message type.\n");
                   5607:         break;
                   5608:       }
                   5609:   } /* End of switch(p->msg_type) */
                   5610: }
                   5611: 
                   5612: /*+F*************************************************************************
                   5613:  * Function:
                   5614:  *   aic7xxx_handle_scsiint
                   5615:  *
                   5616:  * Description:
                   5617:  *   Interrupt handler for SCSI interrupts (SCSIINT).
                   5618:  *-F*************************************************************************/
                   5619: static void
                   5620: aic7xxx_handle_scsiint(struct aic7xxx_host *p, unsigned char intstat)
                   5621: {
                   5622:   unsigned char scb_index;
                   5623:   unsigned char status;
                   5624:   struct aic7xxx_scb *scb;
                   5625: 
                   5626:   scb_index = aic_inb(p, SCB_TAG);
                   5627:   status = aic_inb(p, SSTAT1);
                   5628: 
                   5629:   if (scb_index < p->scb_data->numscbs)
                   5630:   {
                   5631:     scb = p->scb_data->scb_array[scb_index];
                   5632:     if ((scb->flags & SCB_ACTIVE) == 0)
                   5633:     {
                   5634:       scb = NULL;
                   5635:     }
                   5636:   }
                   5637:   else
                   5638:   {
                   5639:     scb = NULL;
                   5640:   }
                   5641: 
                   5642: 
                   5643:   if ((status & SCSIRSTI) != 0)
                   5644:   {
                   5645:     int channel;
                   5646: 
                   5647:     if ( (p->chip & AHC_CHIPID_MASK) == AHC_AIC7770 )
                   5648:       channel = (aic_inb(p, SBLKCTL) & SELBUSB) >> 3;
                   5649:     else
                   5650:       channel = 0;
                   5651: 
                   5652:     if (aic7xxx_verbose & VERBOSE_RESET)
                   5653:       printk(WARN_LEAD "Someone else reset the channel!!\n",
                   5654:            p->host_no, channel, -1, -1);
                   5655:     /*
                   5656:      * Go through and abort all commands for the channel, but do not
                   5657:      * reset the channel again.
                   5658:      */
                   5659:     aic7xxx_reset_channel(p, channel, /* Initiate Reset */ FALSE);
                   5660:     aic7xxx_run_done_queue(p, FALSE);
                   5661:     scb = NULL;
                   5662:   }
                   5663:   else if ( ((status & BUSFREE) != 0) && ((status & SELTO) == 0) )
                   5664:   {
                   5665:     /*
                   5666:      * First look at what phase we were last in.  If it's message-out,
                   5667:      * chances are pretty good that the bus free was in response to
                   5668:      * one of our abort requests.
                   5669:      */
                   5670:     unsigned char lastphase = aic_inb(p, LASTPHASE);
                   5671:     unsigned char saved_tcl = aic_inb(p, SAVED_TCL);
                   5672:     unsigned char target = (saved_tcl >> 4) & 0x0F;
                   5673:     int channel;
                   5674:     int printerror = TRUE;
                   5675: 
                   5676:     if ( (p->chip & AHC_CHIPID_MASK) == AHC_AIC7770 )
                   5677:       channel = (aic_inb(p, SBLKCTL) & SELBUSB) >> 3;
                   5678:     else
                   5679:       channel = 0;
                   5680: 
                   5681:     aic_outb(p, aic_inb(p, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP),
                   5682:              SCSISEQ);
                   5683:     if (lastphase == P_MESGOUT)
                   5684:     {
                   5685:       unsigned char message;
                   5686: 
                   5687:       message = aic_inb(p, SINDEX);
                   5688: 
                   5689:       if ((message == MSG_ABORT) || (message == MSG_ABORT_TAG))
                   5690:       {
                   5691:         if (aic7xxx_verbose & VERBOSE_ABORT_PROCESS)
                   5692:           printk(INFO_LEAD "SCB %d abort delivered.\n", p->host_no,
                   5693:             CTL_OF_SCB(scb), scb->hscb->tag);
                   5694:         aic7xxx_reset_device(p, target, channel, ALL_LUNS,
                   5695:                 (message == MSG_ABORT) ? SCB_LIST_NULL : scb->hscb->tag );
                   5696:         aic7xxx_run_done_queue(p, FALSE);
                   5697:         scb = NULL;
                   5698:         printerror = 0;
                   5699:       }
                   5700:       else if (message == MSG_BUS_DEV_RESET)
                   5701:       {
                   5702:         aic7xxx_handle_device_reset(p, target, channel);
                   5703:         scb = NULL;
                   5704:         printerror = 0;
                   5705:       }
                   5706:     }
                   5707:     if (printerror != 0)
                   5708:     {
                   5709:       if (scb != NULL)
                   5710:       {
                   5711:         unsigned char tag;
                   5712: 
                   5713:         if ((scb->hscb->control & TAG_ENB) != 0)
                   5714:         {
                   5715:           tag = scb->hscb->tag;
                   5716:         }
                   5717:         else
                   5718:         {
                   5719:           tag = SCB_LIST_NULL;
                   5720:         }
                   5721:         aic7xxx_reset_device(p, target, channel, ALL_LUNS, tag);
                   5722:         aic7xxx_run_done_queue(p, FALSE);
                   5723:       }
                   5724:       printk(INFO_LEAD "Unexpected busfree, LASTPHASE = 0x%x, "
                   5725:              "SEQADDR = 0x%x\n", p->host_no, channel, target, -1, lastphase,
                   5726:              (aic_inb(p, SEQADDR1) << 8) | aic_inb(p, SEQADDR0));
                   5727:       scb = NULL;
                   5728:     }
                   5729:     aic_outb(p, MSG_NOOP, MSG_OUT);
                   5730:     aic_outb(p, aic_inb(p, SIMODE1) & ~(ENBUSFREE|ENREQINIT),
                   5731:       SIMODE1);
                   5732:     p->flags &= ~AHC_HANDLING_REQINITS;
                   5733:     aic_outb(p, CLRBUSFREE, CLRSINT1);
                   5734:     aic_outb(p, CLRSCSIINT, CLRINT);
                   5735:     restart_sequencer(p);
                   5736:     unpause_sequencer(p, TRUE);
                   5737:   }
                   5738:   else if ((status & SELTO) != 0)
                   5739:   {
                   5740:     unsigned char scbptr;
                   5741:     unsigned char nextscb;
                   5742:     Scsi_Cmnd *cmd;
                   5743: 
                   5744:     scbptr = aic_inb(p, WAITING_SCBH);
                   5745:     if (scbptr > p->scb_data->maxhscbs)
                   5746:     {
                   5747:       /*
                   5748:        * I'm still trying to track down exactly how this happens, but until
                   5749:        * I find it, this code will make sure we aren't passing bogus values
                   5750:        * into the SCBPTR register, even if that register will just wrap
                   5751:        * things around, we still don't like having out of range variables.
                   5752:        *
                   5753:        * NOTE: Don't check the aic7xxx_verbose variable, I want this message
                   5754:        * to always be displayed.
                   5755:        */
                   5756:       printk(INFO_LEAD "Invalid WAITING_SCBH value %d, improvising.\n",
                   5757:              p->host_no, -1, -1, -1, scbptr);
                   5758:       if (p->scb_data->maxhscbs > 4)
                   5759:         scbptr &= (p->scb_data->maxhscbs - 1);
                   5760:       else
                   5761:         scbptr &= 0x03;
                   5762:     }
                   5763:     aic_outb(p, scbptr, SCBPTR);
                   5764:     scb_index = aic_inb(p, SCB_TAG);
                   5765: 
                   5766:     scb = NULL;
                   5767:     if (scb_index < p->scb_data->numscbs)
                   5768:     {
                   5769:       scb = p->scb_data->scb_array[scb_index];
                   5770:       if ((scb->flags & SCB_ACTIVE) == 0)
                   5771:       {
                   5772:         scb = NULL;
                   5773:       }
                   5774:     }
                   5775:     if (scb == NULL)
                   5776:     {
                   5777:       printk(WARN_LEAD "Referenced SCB %d not valid during SELTO.\n",
                   5778:              p->host_no, -1, -1, -1, scb_index);
                   5779:       printk(KERN_WARNING "        SCSISEQ = 0x%x SEQADDR = 0x%x SSTAT0 = 0x%x "
                   5780:              "SSTAT1 = 0x%x\n", aic_inb(p, SCSISEQ),
                   5781:              aic_inb(p, SEQADDR0) | (aic_inb(p, SEQADDR1) << 8),
                   5782:              aic_inb(p, SSTAT0), aic_inb(p, SSTAT1));
                   5783:       if (aic7xxx_panic_on_abort)
                   5784:         aic7xxx_panic_abort(p, NULL);
                   5785:     }
                   5786:     else
                   5787:     {
                   5788:       cmd = scb->cmd;
                   5789:       cmd->result = (DID_TIME_OUT << 16);
                   5790: 
                   5791:       /*
                   5792:        * Clear out this hardware SCB
                   5793:        */
                   5794:       aic_outb(p, 0, SCB_CONTROL);
                   5795: 
                   5796:       /*
                   5797:        * Clear out a few values in the card that are in an undetermined
                   5798:        * state.
                   5799:        */
                   5800:       aic_outb(p, MSG_NOOP, MSG_OUT);
                   5801: 
                   5802:       /*
                   5803:        * Shift the waiting for selection queue forward
                   5804:        */
                   5805:       nextscb = aic_inb(p, SCB_NEXT);
                   5806:       aic_outb(p, nextscb, WAITING_SCBH);
                   5807: 
                   5808:       /*
                   5809:        * Put this SCB back on the free list.
                   5810:        */
                   5811:       aic7xxx_add_curscb_to_free_list(p);
                   5812: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   5813:       if (aic7xxx_verbose > 0xffff)
                   5814:         printk(INFO_LEAD "Selection Timeout.\n", p->host_no, CTL_OF_SCB(scb));
                   5815: #endif
1.1.1.2 ! root     5816:       if (scb->flags & SCB_QUEUED_ABORT)
1.1       root     5817:       {
1.1.1.2 ! root     5818:         /*
        !          5819:          * We know that this particular SCB had to be the queued abort since
        !          5820:          * the disconnected SCB would have gotten a reconnect instead.
        !          5821:          * What we need to do then is to let the command timeout again so
        !          5822:          * we get a reset since this abort just failed.
        !          5823:          */
1.1       root     5824:         cmd->result = 0;
                   5825:         scb = NULL;
                   5826:       }
                   5827:     }
                   5828:     /*
                   5829:      * Restarting the sequencer will stop the selection and make sure devices
                   5830:      * are allowed to reselect in.
                   5831:      */
                   5832:     aic_outb(p, 0, SCSISEQ);
                   5833:     aic_outb(p, aic_inb(p, SIMODE1) & ~(ENREQINIT|ENBUSFREE), SIMODE1);
                   5834:     p->flags &= ~AHC_HANDLING_REQINITS;
                   5835:     aic_outb(p, CLRSELTIMEO | CLRBUSFREE, CLRSINT1);
                   5836:     aic_outb(p, CLRSCSIINT, CLRINT);
                   5837:     restart_sequencer(p);
                   5838:     unpause_sequencer(p, TRUE);
                   5839:   }
                   5840:   else if (scb == NULL)
                   5841:   {
                   5842:     printk(WARN_LEAD "aic7xxx_isr - referenced scb not valid "
                   5843:            "during scsiint 0x%x scb(%d)\n"
                   5844:            "      SIMODE0 0x%x, SIMODE1 0x%x, SSTAT0 0x%x, SEQADDR 0x%x\n",
                   5845:            p->host_no, -1, -1, -1, status, scb_index, aic_inb(p, SIMODE0),
                   5846:            aic_inb(p, SIMODE1), aic_inb(p, SSTAT0),
                   5847:            (aic_inb(p, SEQADDR1) << 8) | aic_inb(p, SEQADDR0));
                   5848:     /*
                   5849:      * Turn off the interrupt and set status to zero, so that it
                   5850:      * falls through the rest of the SCSIINT code.
                   5851:      */
                   5852:     aic_outb(p, status, CLRSINT1);
                   5853:     aic_outb(p, CLRSCSIINT, CLRINT);
                   5854:     unpause_sequencer(p, /* unpause always */ TRUE);
                   5855:     scb = NULL;
                   5856:   }
                   5857:   else if (status & SCSIPERR)
                   5858:   {
                   5859:     /*
                   5860:      * Determine the bus phase and queue an appropriate message.
                   5861:      */
                   5862:     char  *phase;
                   5863:     Scsi_Cmnd *cmd;
                   5864:     unsigned char mesg_out = MSG_NOOP;
                   5865:     unsigned char lastphase = aic_inb(p, LASTPHASE);
                   5866: 
                   5867:     cmd = scb->cmd;
                   5868:     switch (lastphase)
                   5869:     {
                   5870:       case P_DATAOUT:
                   5871:         phase = "Data-Out";
                   5872:         break;
                   5873:       case P_DATAIN:
                   5874:         phase = "Data-In";
                   5875:         mesg_out = MSG_INITIATOR_DET_ERR;
                   5876:         break;
                   5877:       case P_COMMAND:
                   5878:         phase = "Command";
                   5879:         break;
                   5880:       case P_MESGOUT:
                   5881:         phase = "Message-Out";
                   5882:         break;
                   5883:       case P_STATUS:
                   5884:         phase = "Status";
                   5885:         mesg_out = MSG_INITIATOR_DET_ERR;
                   5886:         break;
                   5887:       case P_MESGIN:
                   5888:         phase = "Message-In";
                   5889:         mesg_out = MSG_PARITY_ERROR;
                   5890:         break;
                   5891:       default:
                   5892:         phase = "unknown";
                   5893:         break;
                   5894:     }
                   5895: 
                   5896:     /*
                   5897:      * A parity error has occurred during a data
                   5898:      * transfer phase. Flag it and continue.
                   5899:      */
                   5900:     printk(WARN_LEAD "Parity error during %s phase.\n",
                   5901:            p->host_no, CTL_OF_SCB(scb), phase);
                   5902: 
                   5903:     /*
                   5904:      * We've set the hardware to assert ATN if we get a parity
                   5905:      * error on "in" phases, so all we need to do is stuff the
                   5906:      * message buffer with the appropriate message.  "In" phases
                   5907:      * have set mesg_out to something other than MSG_NOP.
                   5908:      */
                   5909:     if (mesg_out != MSG_NOOP)
                   5910:     {
                   5911:       aic_outb(p, mesg_out, MSG_OUT);
                   5912:       scb = NULL;
                   5913:     }
                   5914:     aic_outb(p, CLRSCSIPERR, CLRSINT1);
                   5915:     aic_outb(p, CLRSCSIINT, CLRINT);
                   5916:     unpause_sequencer(p, /* unpause_always */ TRUE);
                   5917:   }
                   5918:   else if ( (status & REQINIT) &&
                   5919:             (p->flags & AHC_HANDLING_REQINITS) )
                   5920:   {
                   5921: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   5922:     if (aic7xxx_verbose > 0xffff)
                   5923:       printk(INFO_LEAD "Handling REQINIT, SSTAT1=0x%x.\n", p->host_no,
                   5924:              CTL_OF_SCB(scb), aic_inb(p, SSTAT1));
                   5925: #endif
                   5926:     aic7xxx_handle_reqinit(p, scb);
                   5927:     return;
                   5928:   }
                   5929:   else
                   5930:   {
                   5931:     /*
                   5932:      * We don't know what's going on. Turn off the
                   5933:      * interrupt source and try to continue.
                   5934:      */
                   5935:     if (aic7xxx_verbose & VERBOSE_SCSIINT)
                   5936:       printk(INFO_LEAD "Unknown SCSIINT status, SSTAT1(0x%x).\n",
                   5937:         p->host_no, -1, -1, -1, status);
                   5938:     aic_outb(p, status, CLRSINT1);
                   5939:     aic_outb(p, CLRSCSIINT, CLRINT);
                   5940:     unpause_sequencer(p, /* unpause always */ TRUE);
                   5941:     scb = NULL;
                   5942:   }
                   5943:   if (scb != NULL)
                   5944:   {
                   5945:     aic7xxx_done(p, scb);
                   5946:   }
                   5947: }
                   5948: 
                   5949: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   5950: static void
                   5951: aic7xxx_check_scbs(struct aic7xxx_host *p, char *buffer)
                   5952: {
                   5953:   unsigned char saved_scbptr, free_scbh, dis_scbh, wait_scbh, temp;
                   5954:   int i, bogus, lost;
                   5955:   static unsigned char scb_status[AIC7XXX_MAXSCB];
                   5956: 
                   5957: #define SCB_NO_LIST 0
                   5958: #define SCB_FREE_LIST 1
                   5959: #define SCB_WAITING_LIST 2
                   5960: #define SCB_DISCONNECTED_LIST 4
                   5961: #define SCB_CURRENTLY_ACTIVE 8
                   5962: 
                   5963:   /*
                   5964:    * Note, these checks will fail on a regular basis once the machine moves
                   5965:    * beyond the bus scan phase.  The problem is race conditions concerning
                   5966:    * the scbs and where they are linked in.  When you have 30 or so commands
                   5967:    * outstanding on the bus, and run this twice with every interrupt, the
                   5968:    * chances get pretty good that you'll catch the sequencer with an SCB
                   5969:    * only partially linked in.  Therefore, once we pass the scan phase
                   5970:    * of the bus, we really should disable this function.
                   5971:    */
                   5972:   bogus = FALSE;
                   5973:   memset(&scb_status[0], 0, sizeof(scb_status));
                   5974:   pause_sequencer(p);
                   5975:   saved_scbptr = aic_inb(p, SCBPTR);
                   5976:   if (saved_scbptr >= p->scb_data->maxhscbs)
                   5977:   {
                   5978:     printk("Bogus SCBPTR %d\n", saved_scbptr);
                   5979:     bogus = TRUE;
                   5980:   }
                   5981:   scb_status[saved_scbptr] = SCB_CURRENTLY_ACTIVE;
                   5982:   free_scbh = aic_inb(p, FREE_SCBH);
                   5983:   if ( (free_scbh != SCB_LIST_NULL) &&
                   5984:        (free_scbh >= p->scb_data->maxhscbs) )
                   5985:   {
                   5986:     printk("Bogus FREE_SCBH %d\n", free_scbh);
                   5987:     bogus = TRUE;
                   5988:   }
                   5989:   else
                   5990:   {
                   5991:     temp = free_scbh;
                   5992:     while( (temp != SCB_LIST_NULL) && (temp < p->scb_data->maxhscbs) )
                   5993:     {
                   5994:       if(scb_status[temp] & 0x07)
                   5995:       {
                   5996:         printk("HSCB %d on multiple lists, status 0x%02x", temp,
                   5997:                scb_status[temp] | SCB_FREE_LIST);
                   5998:         bogus = TRUE;
                   5999:       }
                   6000:       scb_status[temp] |= SCB_FREE_LIST;
                   6001:       aic_outb(p, temp, SCBPTR);
                   6002:       temp = aic_inb(p, SCB_NEXT);
                   6003:     }
                   6004:   }
                   6005: 
                   6006:   dis_scbh = aic_inb(p, DISCONNECTED_SCBH);
                   6007:   if ( (dis_scbh != SCB_LIST_NULL) &&
                   6008:        (dis_scbh >= p->scb_data->maxhscbs) )
                   6009:   {
                   6010:     printk("Bogus DISCONNECTED_SCBH %d\n", dis_scbh);
                   6011:     bogus = TRUE;
                   6012:   }
                   6013:   else
                   6014:   {
                   6015:     temp = dis_scbh;
                   6016:     while( (temp != SCB_LIST_NULL) && (temp < p->scb_data->maxhscbs) )
                   6017:     {
                   6018:       if(scb_status[temp] & 0x07)
                   6019:       {
                   6020:         printk("HSCB %d on multiple lists, status 0x%02x", temp,
                   6021:                scb_status[temp] | SCB_DISCONNECTED_LIST);
                   6022:         bogus = TRUE;
                   6023:       }
                   6024:       scb_status[temp] |= SCB_DISCONNECTED_LIST;
                   6025:       aic_outb(p, temp, SCBPTR);
                   6026:       temp = aic_inb(p, SCB_NEXT);
                   6027:     }
                   6028:   }
1.1.1.2 ! root     6029: 
1.1       root     6030:   wait_scbh = aic_inb(p, WAITING_SCBH);
                   6031:   if ( (wait_scbh != SCB_LIST_NULL) &&
                   6032:        (wait_scbh >= p->scb_data->maxhscbs) )
                   6033:   {
                   6034:     printk("Bogus WAITING_SCBH %d\n", wait_scbh);
                   6035:     bogus = TRUE;
                   6036:   }
                   6037:   else
                   6038:   {
                   6039:     temp = wait_scbh;
                   6040:     while( (temp != SCB_LIST_NULL) && (temp < p->scb_data->maxhscbs) )
                   6041:     {
                   6042:       if(scb_status[temp] & 0x07)
                   6043:       {
                   6044:         printk("HSCB %d on multiple lists, status 0x%02x", temp,
                   6045:                scb_status[temp] | SCB_WAITING_LIST);
                   6046:         bogus = TRUE;
                   6047:       }
                   6048:       scb_status[temp] |= SCB_WAITING_LIST;
                   6049:       aic_outb(p, temp, SCBPTR);
                   6050:       temp = aic_inb(p, SCB_NEXT);
                   6051:     }
                   6052:   }
                   6053: 
                   6054:   lost=0;
                   6055:   for(i=0; i < p->scb_data->maxhscbs; i++)
                   6056:   {
                   6057:     aic_outb(p, i, SCBPTR);
                   6058:     temp = aic_inb(p, SCB_NEXT);
                   6059:     if ( ((temp != SCB_LIST_NULL) &&
                   6060:           (temp >= p->scb_data->maxhscbs)) )
                   6061:     {
                   6062:       printk("HSCB %d bad, SCB_NEXT invalid(%d).\n", i, temp);
                   6063:       bogus = TRUE;
                   6064:     }
                   6065:     if ( temp == i )
                   6066:     {
                   6067:       printk("HSCB %d bad, SCB_NEXT points to self.\n", i);
                   6068:       bogus = TRUE;
                   6069:     }
                   6070:     temp = aic_inb(p, SCB_PREV);
                   6071:     if ((temp != SCB_LIST_NULL) &&
                   6072:         (temp >= p->scb_data->maxhscbs))
                   6073:     {
                   6074:       printk("HSCB %d bad, SCB_PREV invalid(%d).\n", i, temp);
                   6075:       bogus = TRUE;
                   6076:     }
                   6077:     if (scb_status[i] == 0)
                   6078:       lost++;
                   6079:     if (lost > 1)
                   6080:     {
                   6081:       printk("Too many lost scbs.\n");
                   6082:       bogus=TRUE;
                   6083:     }
                   6084:   }
                   6085:   aic_outb(p, saved_scbptr, SCBPTR);
                   6086:   unpause_sequencer(p, FALSE);
                   6087:   if (bogus)
                   6088:   {
                   6089:     printk("Bogus parameters found in card SCB array structures.\n");
                   6090:     printk("%s\n", buffer);
                   6091:     aic7xxx_panic_abort(p, NULL);
                   6092:   }
                   6093:   return;
                   6094: }
                   6095: #endif
                   6096: 
                   6097: /*+F*************************************************************************
                   6098:  * Function:
                   6099:  *   aic7xxx_isr
                   6100:  *
                   6101:  * Description:
                   6102:  *   SCSI controller interrupt handler.
                   6103:  *-F*************************************************************************/
                   6104: static void
                   6105: aic7xxx_isr(int irq, void *dev_id, struct pt_regs *regs)
                   6106: {
                   6107:   struct aic7xxx_host *p;
                   6108:   unsigned char intstat;
                   6109: 
                   6110:   p = (struct aic7xxx_host *)dev_id;
                   6111: 
                   6112:   /*
                   6113:    * Just a few sanity checks.  Make sure that we have an int pending.
                   6114:    * Also, if PCI, then we are going to check for a PCI bus error status
                   6115:    * should we get too many spurious interrupts.
                   6116:    */
                   6117:   if (!((intstat = aic_inb(p, INTSTAT)) & INT_PEND))
                   6118:   {
                   6119: #ifdef CONFIG_PCI
                   6120:     if ( (p->chip & AHC_PCI) && (p->spurious_int > 500) &&
                   6121:         !(p->flags & AHC_HANDLING_REQINITS) )
                   6122:     {
                   6123:       if ( aic_inb(p, ERROR) & PCIERRSTAT )
                   6124:       {
                   6125:         aic7xxx_pci_intr(p);
                   6126:       }
                   6127:       p->spurious_int = 0;
                   6128:     }
                   6129:     else if ( !(p->flags & AHC_HANDLING_REQINITS) )
                   6130:     {
                   6131:       p->spurious_int++;
                   6132:     }
                   6133: #endif
                   6134:     return;
                   6135:   }
                   6136: 
                   6137:   p->spurious_int = 0;
                   6138: 
                   6139:   /*
                   6140:    * Keep track of interrupts for /proc/scsi
                   6141:    */
                   6142:   p->isr_count++;
                   6143: 
                   6144: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   6145:   if ( (p->isr_count < 16) && (aic7xxx_verbose > 0xffff) &&
                   6146:        (aic7xxx_panic_on_abort) && (p->flags & AHC_PAGESCBS) )
                   6147:     aic7xxx_check_scbs(p, "Bogus settings at start of interrupt.");
                   6148: #endif
                   6149: 
                   6150:   /*
                   6151:    * Handle all the interrupt sources - especially for SCSI
                   6152:    * interrupts, we won't get a second chance at them.
                   6153:    */
                   6154:   if (intstat & CMDCMPLT)
                   6155:   {
                   6156:     struct aic7xxx_scb *scb = NULL;
                   6157:     Scsi_Cmnd *cmd;
                   6158:     unsigned char scb_index;
                   6159: 
                   6160: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   6161:     if(aic7xxx_verbose > 0xffff)
                   6162:       printk(INFO_LEAD "Command Complete Int.\n", p->host_no, -1, -1, -1);
                   6163: #endif
1.1.1.2 ! root     6164: 
1.1       root     6165:     /*
                   6166:      * Clear interrupt status before running the completion loop.
                   6167:      * This eliminates a race condition whereby a command could
                   6168:      * complete between the last check of qoutfifo and the
                   6169:      * CLRCMDINT statement.  This would result in us thinking the
                   6170:      * qoutfifo was empty when it wasn't, and in actuality be a lost
                   6171:      * completion interrupt.  With multiple devices or tagged queueing
                   6172:      * this could be very bad if we caught all but the last completion
                   6173:      * and no more are imediately sent.
                   6174:      */
                   6175:     aic_outb(p, CLRCMDINT, CLRINT);
                   6176:     /*
                   6177:      * The sequencer will continue running when it
                   6178:      * issues this interrupt. There may be >1 commands
                   6179:      * finished, so loop until we've processed them all.
                   6180:      */
                   6181: 
                   6182:     while (p->qoutfifo[p->qoutfifonext] != SCB_LIST_NULL)
                   6183:     {
                   6184:       scb_index = p->qoutfifo[p->qoutfifonext];
                   6185:       p->qoutfifo[p->qoutfifonext++] = SCB_LIST_NULL;
                   6186:       if ( scb_index >= p->scb_data->numscbs )
                   6187:         scb = NULL;
                   6188:       else
                   6189:         scb = p->scb_data->scb_array[scb_index];
                   6190:       if (scb == NULL)
                   6191:       {
                   6192:         printk(WARN_LEAD "CMDCMPLT with invalid SCB index %d\n", p->host_no,
                   6193:           -1, -1, -1, scb_index);
                   6194:         continue;
                   6195:       }
                   6196:       else if (!(scb->flags & SCB_ACTIVE) || (scb->cmd == NULL))
                   6197:       {
                   6198:         printk(WARN_LEAD "CMDCMPLT without command for SCB %d, SCB flags "
                   6199:           "0x%x, cmd 0x%lx\n", p->host_no, -1, -1, -1, scb_index, scb->flags,
                   6200:           (unsigned long) scb->cmd);
                   6201:         continue;
                   6202:       }
                   6203:       else if (scb->flags & SCB_QUEUED_ABORT)
                   6204:       {
                   6205:         pause_sequencer(p);
                   6206:         if ( ((aic_inb(p, LASTPHASE) & PHASE_MASK) != P_BUSFREE) &&
                   6207:              (aic_inb(p, SCB_TAG) == scb->hscb->tag) )
                   6208:         {
                   6209:           unpause_sequencer(p, FALSE);
                   6210:           continue;
                   6211:         }
                   6212:         aic7xxx_reset_device(p, scb->cmd->target, scb->cmd->channel,
                   6213:           scb->cmd->lun, scb->hscb->tag);
                   6214:         scb->flags &= ~(SCB_QUEUED_FOR_DONE | SCB_RESET | SCB_ABORT |
                   6215:           SCB_QUEUED_ABORT);
                   6216:         unpause_sequencer(p, FALSE);
                   6217:       }
                   6218:       else if (scb->flags & SCB_ABORT)
                   6219:       {
                   6220:         /*
                   6221:          * We started to abort this, but it completed on us, let it
                   6222:          * through as successful
                   6223:          */
                   6224:         scb->flags &= ~(SCB_ABORT|SCB_RESET);
                   6225:       }
                   6226:       switch (status_byte(scb->hscb->target_status))
                   6227:       {
                   6228:         case QUEUE_FULL:
                   6229:         case BUSY:
                   6230:           scb->hscb->target_status = 0;
                   6231:           scb->cmd->result = 0;
                   6232:           aic7xxx_error(scb->cmd) = DID_OK;
                   6233:           break;
                   6234:         default:
                   6235:           cmd = scb->cmd;
                   6236:           if (scb->hscb->residual_SG_segment_count != 0)
                   6237:           {
                   6238:             aic7xxx_calculate_residual(p, scb);
                   6239:           }
                   6240:           cmd->result |= (aic7xxx_error(cmd) << 16);
                   6241:           aic7xxx_done(p, scb);
                   6242:           break;
1.1.1.2 ! root     6243:       }
1.1       root     6244:     }
                   6245:   }
                   6246: 
                   6247:   if (intstat & BRKADRINT)
                   6248:   {
                   6249:     int i;
                   6250:     unsigned char errno = aic_inb(p, ERROR);
                   6251: 
                   6252:     printk(KERN_ERR "(scsi%d) BRKADRINT error(0x%x):\n", p->host_no, errno);
                   6253:     for (i = 0; i < NUMBER(hard_error); i++)
                   6254:     {
                   6255:       if (errno & hard_error[i].errno)
                   6256:       {
                   6257:         printk(KERN_ERR "  %s\n", hard_error[i].errmesg);
                   6258:       }
                   6259:     }
                   6260:     printk(KERN_ERR "(scsi%d)   SEQADDR=0x%x\n", p->host_no,
                   6261:       (((aic_inb(p, SEQADDR1) << 8) & 0x100) | aic_inb(p, SEQADDR0)));
                   6262:     if (aic7xxx_panic_on_abort)
                   6263:       aic7xxx_panic_abort(p, NULL);
                   6264: #ifdef CONFIG_PCI
                   6265:     if (errno & PCIERRSTAT)
                   6266:       aic7xxx_pci_intr(p);
                   6267: #endif
                   6268:     if (errno & (SQPARERR | ILLOPCODE | ILLSADDR))
                   6269:     {
                   6270:       sti();
                   6271:       panic("aic7xxx: unrecoverable BRKADRINT.\n");
                   6272:     }
                   6273:     if (errno & ILLHADDR)
                   6274:     {
                   6275:       printk(KERN_ERR "(scsi%d) BUG! Driver accessed chip without first "
                   6276:              "pausing controller!\n", p->host_no);
                   6277:     }
                   6278: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   6279:     if (errno & DPARERR)
                   6280:     {
                   6281:       if (aic_inb(p, DMAPARAMS) & DIRECTION)
                   6282:         printk("(scsi%d) while DMAing SCB from host to card.\n", p->host_no);
                   6283:       else
                   6284:         printk("(scsi%d) while DMAing SCB from card to host.\n", p->host_no);
                   6285:     }
                   6286: #endif
                   6287:     aic_outb(p, CLRPARERR | CLRBRKADRINT, CLRINT);
                   6288:     unpause_sequencer(p, FALSE);
                   6289:   }
                   6290: 
                   6291:   if (intstat & SEQINT)
                   6292:   {
                   6293:     aic7xxx_handle_seqint(p, intstat);
                   6294:   }
                   6295: 
                   6296:   if (intstat & SCSIINT)
                   6297:   {
                   6298:     aic7xxx_handle_scsiint(p, intstat);
                   6299:   }
                   6300: 
                   6301: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   6302:   if ( (p->isr_count < 16) && (aic7xxx_verbose > 0xffff) &&
                   6303:        (aic7xxx_panic_on_abort) && (p->flags & AHC_PAGESCBS) )
                   6304:     aic7xxx_check_scbs(p, "Bogus settings at end of interrupt.");
                   6305: #endif
                   6306: 
                   6307: }
                   6308: 
                   6309: /*+F*************************************************************************
                   6310:  * Function:
                   6311:  *   do_aic7xxx_isr
                   6312:  *
                   6313:  * Description:
                   6314:  *   This is a gross hack to solve a problem in linux kernels 2.1.85 and
                   6315:  *   above.  Please, children, do not try this at home, and if you ever see
                   6316:  *   anything like it, please inform the Gross Hack Police immediately
                   6317:  *-F*************************************************************************/
                   6318: static void
                   6319: do_aic7xxx_isr(int irq, void *dev_id, struct pt_regs *regs)
                   6320: {
                   6321:   unsigned long cpu_flags;
                   6322:   struct aic7xxx_host *p;
1.1.1.2 ! root     6323: 
1.1       root     6324:   p = (struct aic7xxx_host *)dev_id;
                   6325:   if(!p)
                   6326:     return;
                   6327: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,95)
                   6328:   spin_lock_irqsave(&io_request_lock, cpu_flags);
                   6329:   if(test_and_set_bit(AHC_IN_ISR_BIT, &p->flags))
                   6330:   {
                   6331:     return;
                   6332:   }
                   6333:   do
                   6334:   {
                   6335:     aic7xxx_isr(irq, dev_id, regs);
                   6336:   } while ( (aic_inb(p, INTSTAT) & INT_PEND) );
                   6337:   aic7xxx_done_cmds_complete(p);
                   6338:   aic7xxx_run_waiting_queues(p);
                   6339:   clear_bit(AHC_IN_ISR_BIT, &p->flags);
                   6340:   spin_unlock_irqrestore(&io_request_lock, cpu_flags);
                   6341: #else
                   6342:   if(set_bit(AHC_IN_ISR_BIT, (int *)&p->flags))
                   6343:   {
                   6344:     return;
                   6345:   }
                   6346:   DRIVER_LOCK
                   6347:   do
                   6348:   {
                   6349:     aic7xxx_isr(irq, dev_id, regs);
                   6350:   } while ( (aic_inb(p, INTSTAT) & INT_PEND) );
                   6351:   DRIVER_UNLOCK
                   6352:   aic7xxx_done_cmds_complete(p);
                   6353:   aic7xxx_run_waiting_queues(p);
                   6354:   clear_bit(AHC_IN_ISR_BIT, (int *)&p->flags);
                   6355: #endif
                   6356: }
                   6357: 
                   6358: /*+F*************************************************************************
                   6359:  * Function:
                   6360:  *   aic7xxx_device_queue_depth
                   6361:  *
                   6362:  * Description:
                   6363:  *   Determines the queue depth for a given device.  There are two ways
                   6364:  *   a queue depth can be obtained for a tagged queueing device.  One
                   6365:  *   way is the default queue depth which is determined by whether
1.1.1.2 ! root     6366:  *   AIC7XXX_CMDS_PER_DEVICE is defined.  If it is defined, then it is used
1.1       root     6367:  *   as the default queue depth.  Otherwise, we use either 4 or 8 as the
                   6368:  *   default queue depth (dependent on the number of hardware SCBs).
                   6369:  *   The other way we determine queue depth is through the use of the
                   6370:  *   aic7xxx_tag_info array which is enabled by defining
                   6371:  *   AIC7XXX_TAGGED_QUEUEING_BY_DEVICE.  This array can be initialized
                   6372:  *   with queue depths for individual devices.  It also allows tagged
                   6373:  *   queueing to be [en|dis]abled for a specific adapter.
                   6374:  *-F*************************************************************************/
                   6375: static void
                   6376: aic7xxx_device_queue_depth(struct aic7xxx_host *p, Scsi_Device *device)
                   6377: {
                   6378:   int default_depth = 3;
                   6379:   unsigned char tindex;
                   6380:   unsigned short target_mask;
                   6381: 
                   6382:   tindex = device->id | (device->channel << 3);
                   6383:   target_mask = (1 << tindex);
                   6384: 
                   6385:   device->queue_depth = default_depth;
                   6386:   p->dev_temp_queue_depth[tindex] = 1;
                   6387:   p->dev_max_queue_depth[tindex] = 1;
                   6388:   p->tagenable &= ~target_mask;
                   6389: 
                   6390:   if (device->tagged_supported)
                   6391:   {
                   6392:     int tag_enabled = TRUE;
                   6393: 
1.1.1.2 ! root     6394:     default_depth = AIC7XXX_CMDS_PER_DEVICE;
        !          6395: 
1.1       root     6396:     if (!(p->discenable & target_mask))
                   6397:     {
                   6398:       if (aic7xxx_verbose & VERBOSE_NEGOTIATION2)
                   6399:         printk(INFO_LEAD "Disconnection disabled, unable to "
                   6400:              "enable tagged queueing.\n",
                   6401:              p->host_no, device->channel, device->id, device->lun);
                   6402:     }
                   6403:     else
                   6404:     {
                   6405:       if (p->instance >= NUMBER(aic7xxx_tag_info))
                   6406:       {
                   6407:         static int print_warning = TRUE;
                   6408:         if(print_warning)
                   6409:         {
                   6410:           printk(KERN_INFO "aic7xxx: WARNING, insufficient tag_info instances for"
                   6411:                            " installed controllers.\n");
                   6412:           printk(KERN_INFO "aic7xxx: Please update the aic7xxx_tag_info array in"
                   6413:                            " the aic7xxx.c source file.\n");
                   6414:           print_warning = FALSE;
                   6415:         }
                   6416:         device->queue_depth = default_depth;
                   6417:       }
                   6418:       else
                   6419:       {
                   6420: 
                   6421:         if (aic7xxx_tag_info[p->instance].tag_commands[tindex] == 255)
                   6422:         {
                   6423:           tag_enabled = FALSE;
                   6424:           device->queue_depth = 3;  /* Tagged queueing is disabled. */
                   6425:         }
                   6426:         else if (aic7xxx_tag_info[p->instance].tag_commands[tindex] == 0)
                   6427:         {
                   6428:           device->queue_depth = default_depth;
                   6429:         }
                   6430:         else
                   6431:         {
                   6432:           device->queue_depth =
                   6433:             aic7xxx_tag_info[p->instance].tag_commands[tindex];
                   6434:         }
                   6435:       }
                   6436:       if ((device->tagged_queue == 0) && tag_enabled)
                   6437:       {
                   6438:         if (aic7xxx_verbose & VERBOSE_NEGOTIATION2)
                   6439:         {
                   6440:               printk(INFO_LEAD "Enabled tagged queuing, queue depth %d.\n",
                   6441:                 p->host_no, device->channel, device->id,
                   6442:                 device->lun, device->queue_depth);
                   6443:         }
                   6444:         p->dev_max_queue_depth[tindex] = device->queue_depth;
                   6445:         p->dev_temp_queue_depth[tindex] = device->queue_depth;
                   6446:         p->tagenable |= target_mask;
                   6447:         p->orderedtag |= target_mask;
                   6448:         device->tagged_queue = 1;
                   6449:         device->current_tag = SCB_LIST_NULL;
                   6450:       }
                   6451:     }
                   6452:   }
                   6453: }
                   6454: 
                   6455: /*+F*************************************************************************
                   6456:  * Function:
                   6457:  *   aic7xxx_select_queue_depth
                   6458:  *
                   6459:  * Description:
                   6460:  *   Sets the queue depth for each SCSI device hanging off the input
                   6461:  *   host adapter.  We use a queue depth of 2 for devices that do not
                   6462:  *   support tagged queueing.  If AIC7XXX_CMDS_PER_LUN is defined, we
                   6463:  *   use that for tagged queueing devices; otherwise we use our own
                   6464:  *   algorithm for determining the queue depth based on the maximum
                   6465:  *   SCBs for the controller.
                   6466:  *-F*************************************************************************/
                   6467: static void
                   6468: aic7xxx_select_queue_depth(struct Scsi_Host *host,
                   6469:     Scsi_Device *scsi_devs)
                   6470: {
                   6471:   Scsi_Device *device;
                   6472:   struct aic7xxx_host *p = (struct aic7xxx_host *) host->hostdata;
                   6473:   int scbnum;
                   6474: 
                   6475:   scbnum = 0;
                   6476:   for (device = scsi_devs; device != NULL; device = device->next)
                   6477:   {
                   6478:     if (device->host == host)
                   6479:     {
                   6480:       aic7xxx_device_queue_depth(p, device);
                   6481:       scbnum += device->queue_depth;
                   6482:     }
                   6483:   }
                   6484:   while (scbnum > p->scb_data->numscbs)
                   6485:   {
                   6486:     /*
                   6487:      * Pre-allocate the needed SCBs to get around the possibility of having
                   6488:      * to allocate some when memory is more or less exhausted and we need
                   6489:      * the SCB in order to perform a swap operation (possible deadlock)
                   6490:      */
                   6491:     if ( aic7xxx_allocate_scb(p) == 0 )
                   6492:       return;
                   6493:   }
                   6494: }
                   6495: 
                   6496: /*+F*************************************************************************
                   6497:  * Function:
                   6498:  *   aic7xxx_probe
                   6499:  *
                   6500:  * Description:
                   6501:  *   Probing for EISA boards: it looks like the first two bytes
                   6502:  *   are a manufacturer code - three characters, five bits each:
                   6503:  *
                   6504:  *               BYTE 0   BYTE 1   BYTE 2   BYTE 3
                   6505:  *              ?1111122 22233333 PPPPPPPP RRRRRRRR
                   6506:  *
                   6507:  *   The characters are baselined off ASCII '@', so add that value
                   6508:  *   to each to get the real ASCII code for it. The next two bytes
                   6509:  *   appear to be a product and revision number, probably vendor-
                   6510:  *   specific. This is what is being searched for at each port,
                   6511:  *   and what should probably correspond to the ID= field in the
                   6512:  *   ECU's .cfg file for the card - if your card is not detected,
                   6513:  *   make sure your signature is listed in the array.
                   6514:  *
                   6515:  *   The fourth byte's lowest bit seems to be an enabled/disabled
                   6516:  *   flag (rest of the bits are reserved?).
                   6517:  *
                   6518:  * NOTE:  This function is only needed on Intel and Alpha platforms,
                   6519:  *   the other platforms we support don't have EISA/VLB busses.  So,
                   6520:  *   we #ifdef this entire function to avoid compiler warnings about
                   6521:  *   an unused function.
                   6522:  *-F*************************************************************************/
                   6523: #if defined(__i386__) || defined(__alpha__)
                   6524: static int
                   6525: aic7xxx_probe(int slot, int base, ahc_flag_type *flags)
                   6526: {
                   6527:   int i;
                   6528:   unsigned char buf[4];
                   6529: 
                   6530:   static struct {
                   6531:     int n;
                   6532:     unsigned char signature[sizeof(buf)];
                   6533:     ahc_chip type;
                   6534:     int bios_disabled;
                   6535:   } AIC7xxx[] = {
                   6536:     { 4, { 0x04, 0x90, 0x77, 0x70 },
                   6537:       AHC_AIC7770|AHC_EISA, FALSE },  /* mb 7770  */
                   6538:     { 4, { 0x04, 0x90, 0x77, 0x71 },
                   6539:       AHC_AIC7770|AHC_EISA, FALSE }, /* host adapter 274x */
                   6540:     { 4, { 0x04, 0x90, 0x77, 0x56 },
                   6541:       AHC_AIC7770|AHC_VL, FALSE }, /* 284x BIOS enabled */
                   6542:     { 4, { 0x04, 0x90, 0x77, 0x57 },
                   6543:       AHC_AIC7770|AHC_VL, TRUE }   /* 284x BIOS disabled */
                   6544:   };
                   6545: 
                   6546:   /*
                   6547:    * The VL-bus cards need to be primed by
                   6548:    * writing before a signature check.
                   6549:    */
                   6550:   for (i = 0; i < sizeof(buf); i++)
                   6551:   {
                   6552:     outb(0x80 + i, base);
                   6553:     buf[i] = inb(base + i);
                   6554:   }
                   6555: 
                   6556:   for (i = 0; i < NUMBER(AIC7xxx); i++)
                   6557:   {
                   6558:     /*
                   6559:      * Signature match on enabled card?
                   6560:      */
                   6561:     if (!memcmp(buf, AIC7xxx[i].signature, AIC7xxx[i].n))
                   6562:     {
                   6563:       if (inb(base + 4) & 1)
                   6564:       {
                   6565:         if (AIC7xxx[i].bios_disabled)
                   6566:         {
                   6567:           *flags |= AHC_USEDEFAULTS;
                   6568:         }
                   6569:         else
                   6570:         {
                   6571:           *flags |= AHC_BIOS_ENABLED;
                   6572:         }
                   6573:         return (i);
                   6574:       }
                   6575: 
                   6576:       printk("aic7xxx: <Adaptec 7770 SCSI Host Adapter> "
                   6577:              "disabled at slot %d, ignored.\n", slot);
                   6578:     }
                   6579:   }
                   6580: 
                   6581:   return (-1);
                   6582: }
                   6583: #endif /* (__i386__) || (__alpha__) */
                   6584: 
                   6585: 
                   6586: /*+F*************************************************************************
                   6587:  * Function:
                   6588:  *   read_2840_seeprom
                   6589:  *
                   6590:  * Description:
                   6591:  *   Reads the 2840 serial EEPROM and returns 1 if successful and 0 if
                   6592:  *   not successful.
                   6593:  *
                   6594:  *   See read_seeprom (for the 2940) for the instruction set of the 93C46
                   6595:  *   chip.
                   6596:  *
                   6597:  *   The 2840 interface to the 93C46 serial EEPROM is through the
                   6598:  *   STATUS_2840 and SEECTL_2840 registers.  The CS_2840, CK_2840, and
                   6599:  *   DO_2840 bits of the SEECTL_2840 register are connected to the chip
                   6600:  *   select, clock, and data out lines respectively of the serial EEPROM.
                   6601:  *   The DI_2840 bit of the STATUS_2840 is connected to the data in line
                   6602:  *   of the serial EEPROM.  The EEPROM_TF bit of STATUS_2840 register is
                   6603:  *   useful in that it gives us an 800 nsec timer.  After a read from the
                   6604:  *   SEECTL_2840 register the timing flag is cleared and goes high 800 nsec
                   6605:  *   later.
                   6606:  *-F*************************************************************************/
                   6607: static int
                   6608: read_284x_seeprom(struct aic7xxx_host *p, struct seeprom_config *sc)
                   6609: {
                   6610:   int i = 0, k = 0;
                   6611:   unsigned char temp;
                   6612:   unsigned short checksum = 0;
                   6613:   unsigned short *seeprom = (unsigned short *) sc;
                   6614:   struct seeprom_cmd {
                   6615:     unsigned char len;
                   6616:     unsigned char bits[3];
                   6617:   };
                   6618:   struct seeprom_cmd seeprom_read = {3, {1, 1, 0}};
                   6619: 
                   6620: #define CLOCK_PULSE(p) \
                   6621:   while ((aic_inb(p, STATUS_2840) & EEPROM_TF) == 0)        \
                   6622:   {                                                \
                   6623:     ;  /* Do nothing */                                \
                   6624:   }                                                \
                   6625:   (void) aic_inb(p, SEECTL_2840);
                   6626: 
                   6627:   /*
                   6628:    * Read the first 32 registers of the seeprom.  For the 2840,
                   6629:    * the 93C46 SEEPROM is a 1024-bit device with 64 16-bit registers
                   6630:    * but only the first 32 are used by Adaptec BIOS.  The loop
                   6631:    * will range from 0 to 31.
                   6632:    */
                   6633:   for (k = 0; k < (sizeof(*sc) / 2); k++)
                   6634:   {
                   6635:     /*
                   6636:      * Send chip select for one clock cycle.
                   6637:      */
                   6638:     aic_outb(p, CK_2840 | CS_2840, SEECTL_2840);
                   6639:     CLOCK_PULSE(p);
                   6640: 
                   6641:     /*
                   6642:      * Now we're ready to send the read command followed by the
                   6643:      * address of the 16-bit register we want to read.
                   6644:      */
                   6645:     for (i = 0; i < seeprom_read.len; i++)
                   6646:     {
                   6647:       temp = CS_2840 | seeprom_read.bits[i];
                   6648:       aic_outb(p, temp, SEECTL_2840);
                   6649:       CLOCK_PULSE(p);
                   6650:       temp = temp ^ CK_2840;
                   6651:       aic_outb(p, temp, SEECTL_2840);
                   6652:       CLOCK_PULSE(p);
                   6653:     }
                   6654:     /*
                   6655:      * Send the 6 bit address (MSB first, LSB last).
                   6656:      */
                   6657:     for (i = 5; i >= 0; i--)
                   6658:     {
                   6659:       temp = k;
                   6660:       temp = (temp >> i) & 1;  /* Mask out all but lower bit. */
                   6661:       temp = CS_2840 | temp;
                   6662:       aic_outb(p, temp, SEECTL_2840);
                   6663:       CLOCK_PULSE(p);
                   6664:       temp = temp ^ CK_2840;
                   6665:       aic_outb(p, temp, SEECTL_2840);
                   6666:       CLOCK_PULSE(p);
                   6667:     }
                   6668: 
                   6669:     /*
                   6670:      * Now read the 16 bit register.  An initial 0 precedes the
                   6671:      * register contents which begins with bit 15 (MSB) and ends
                   6672:      * with bit 0 (LSB).  The initial 0 will be shifted off the
                   6673:      * top of our word as we let the loop run from 0 to 16.
                   6674:      */
                   6675:     for (i = 0; i <= 16; i++)
                   6676:     {
                   6677:       temp = CS_2840;
                   6678:       aic_outb(p, temp, SEECTL_2840);
                   6679:       CLOCK_PULSE(p);
                   6680:       temp = temp ^ CK_2840;
                   6681:       seeprom[k] = (seeprom[k] << 1) | (aic_inb(p, STATUS_2840) & DI_2840);
                   6682:       aic_outb(p, temp, SEECTL_2840);
                   6683:       CLOCK_PULSE(p);
                   6684:     }
                   6685:     /*
                   6686:      * The serial EEPROM has a checksum in the last word.  Keep a
                   6687:      * running checksum for all words read except for the last
                   6688:      * word.  We'll verify the checksum after all words have been
                   6689:      * read.
                   6690:      */
                   6691:     if (k < (sizeof(*sc) / 2) - 1)
                   6692:     {
                   6693:       checksum = checksum + seeprom[k];
                   6694:     }
                   6695: 
                   6696:     /*
                   6697:      * Reset the chip select for the next command cycle.
                   6698:      */
                   6699:     aic_outb(p, 0, SEECTL_2840);
                   6700:     CLOCK_PULSE(p);
                   6701:     aic_outb(p, CK_2840, SEECTL_2840);
                   6702:     CLOCK_PULSE(p);
                   6703:     aic_outb(p, 0, SEECTL_2840);
                   6704:     CLOCK_PULSE(p);
                   6705:   }
                   6706: 
                   6707: #if 0
                   6708:   printk("Computed checksum 0x%x, checksum read 0x%x\n", checksum, sc->checksum);
                   6709:   printk("Serial EEPROM:");
                   6710:   for (k = 0; k < (sizeof(*sc) / 2); k++)
                   6711:   {
                   6712:     if (((k % 8) == 0) && (k != 0))
                   6713:     {
                   6714:       printk("\n              ");
                   6715:     }
                   6716:     printk(" 0x%x", seeprom[k]);
                   6717:   }
                   6718:   printk("\n");
                   6719: #endif
                   6720: 
                   6721:   if (checksum != sc->checksum)
                   6722:   {
                   6723:     printk("aic7xxx: SEEPROM checksum error, ignoring SEEPROM settings.\n");
                   6724:     return (0);
                   6725:   }
                   6726: 
                   6727:   return (1);
                   6728: #undef CLOCK_PULSE
                   6729: }
                   6730: 
                   6731: /*+F*************************************************************************
                   6732:  * Function:
                   6733:  *   acquire_seeprom
                   6734:  *
                   6735:  * Description:
                   6736:  *   Acquires access to the memory port on PCI controllers.
                   6737:  *-F*************************************************************************/
                   6738: static int
                   6739: acquire_seeprom(struct aic7xxx_host *p)
                   6740: {
                   6741:   int wait;
                   6742: 
                   6743:   /*
                   6744:    * Request access of the memory port.  When access is
                   6745:    * granted, SEERDY will go high.  We use a 1 second
                   6746:    * timeout which should be near 1 second more than
                   6747:    * is needed.  Reason: after the 7870 chip reset, there
                   6748:    * should be no contention.
                   6749:    */
                   6750:   aic_outb(p, SEEMS, SEECTL);
                   6751:   wait = 1000;  /* 1000 msec = 1 second */
                   6752:   while ((wait > 0) && ((aic_inb(p, SEECTL) & SEERDY) == 0))
                   6753:   {
                   6754:     wait--;
                   6755:     mdelay(1);  /* 1 msec */
                   6756:   }
                   6757:   if ((aic_inb(p, SEECTL) & SEERDY) == 0)
                   6758:   {
                   6759:     aic_outb(p, 0, SEECTL);
                   6760:     return (0);
                   6761:   }
                   6762:   return (1);
                   6763: }
                   6764: 
                   6765: /*+F*************************************************************************
                   6766:  * Function:
                   6767:  *   release_seeprom
                   6768:  *
                   6769:  * Description:
                   6770:  *   Releases access to the memory port on PCI controllers.
                   6771:  *-F*************************************************************************/
                   6772: static void
                   6773: release_seeprom(struct aic7xxx_host *p)
                   6774: {
                   6775:   aic_outb(p, 0, SEECTL);
                   6776: }
                   6777: 
                   6778: /*+F*************************************************************************
                   6779:  * Function:
                   6780:  *   read_seeprom
                   6781:  *
                   6782:  * Description:
                   6783:  *   Reads the serial EEPROM and returns 1 if successful and 0 if
                   6784:  *   not successful.
                   6785:  *
                   6786:  *   The instruction set of the 93C46/56/66 chips is as follows:
                   6787:  *
                   6788:  *               Start  OP
                   6789:  *     Function   Bit  Code  Address    Data     Description
                   6790:  *     -------------------------------------------------------------------
                   6791:  *     READ        1    10   A5 - A0             Reads data stored in memory,
                   6792:  *                                               starting at specified address
                   6793:  *     EWEN        1    00   11XXXX              Write enable must precede
                   6794:  *                                               all programming modes
                   6795:  *     ERASE       1    11   A5 - A0             Erase register A5A4A3A2A1A0
                   6796:  *     WRITE       1    01   A5 - A0   D15 - D0  Writes register
                   6797:  *     ERAL        1    00   10XXXX              Erase all registers
                   6798:  *     WRAL        1    00   01XXXX    D15 - D0  Writes to all registers
                   6799:  *     EWDS        1    00   00XXXX              Disables all programming
                   6800:  *                                               instructions
                   6801:  *     *Note: A value of X for address is a don't care condition.
                   6802:  *     *Note: The 93C56 and 93C66 have 8 address bits.
1.1.1.2 ! root     6803:  *
1.1       root     6804:  *
                   6805:  *   The 93C46 has a four wire interface: clock, chip select, data in, and
                   6806:  *   data out.  In order to perform one of the above functions, you need
                   6807:  *   to enable the chip select for a clock period (typically a minimum of
                   6808:  *   1 usec, with the clock high and low a minimum of 750 and 250 nsec
                   6809:  *   respectively.  While the chip select remains high, you can clock in
                   6810:  *   the instructions (above) starting with the start bit, followed by the
                   6811:  *   OP code, Address, and Data (if needed).  For the READ instruction, the
                   6812:  *   requested 16-bit register contents is read from the data out line but
                   6813:  *   is preceded by an initial zero (leading 0, followed by 16-bits, MSB
                   6814:  *   first).  The clock cycling from low to high initiates the next data
                   6815:  *   bit to be sent from the chip.
                   6816:  *
                   6817:  *   The 78xx interface to the 93C46 serial EEPROM is through the SEECTL
                   6818:  *   register.  After successful arbitration for the memory port, the
                   6819:  *   SEECS bit of the SEECTL register is connected to the chip select.
                   6820:  *   The SEECK, SEEDO, and SEEDI are connected to the clock, data out,
                   6821:  *   and data in lines respectively.  The SEERDY bit of SEECTL is useful
                   6822:  *   in that it gives us an 800 nsec timer.  After a write to the SEECTL
                   6823:  *   register, the SEERDY goes high 800 nsec later.  The one exception
                   6824:  *   to this is when we first request access to the memory port.  The
                   6825:  *   SEERDY goes high to signify that access has been granted and, for
                   6826:  *   this case, has no implied timing.
                   6827:  *-F*************************************************************************/
                   6828: static int
1.1.1.2 ! root     6829: read_seeprom(struct aic7xxx_host *p, int offset,
1.1       root     6830:     unsigned short *scarray, unsigned int len, seeprom_chip_type chip)
                   6831: {
                   6832:   int i = 0, k;
                   6833:   unsigned char temp;
                   6834:   unsigned short checksum = 0;
                   6835:   struct seeprom_cmd {
                   6836:     unsigned char len;
                   6837:     unsigned char bits[3];
                   6838:   };
                   6839:   struct seeprom_cmd seeprom_read = {3, {1, 1, 0}};
                   6840: 
                   6841: #define CLOCK_PULSE(p) \
                   6842:   while ((aic_inb(p, SEECTL) & SEERDY) == 0)        \
                   6843:   {                                                \
                   6844:     ;  /* Do nothing */                                \
                   6845:   }
                   6846: 
                   6847:   /*
                   6848:    * Request access of the memory port.
                   6849:    */
                   6850:   if (acquire_seeprom(p) == 0)
                   6851:   {
                   6852:     return (0);
                   6853:   }
                   6854: 
                   6855:   /*
                   6856:    * Read 'len' registers of the seeprom.  For the 7870, the 93C46
                   6857:    * SEEPROM is a 1024-bit device with 64 16-bit registers but only
                   6858:    * the first 32 are used by Adaptec BIOS.  Some adapters use the
                   6859:    * 93C56 SEEPROM which is a 2048-bit device.  The loop will range
                   6860:    * from 0 to 'len' - 1.
                   6861:    */
                   6862:   for (k = 0; k < len; k++)
                   6863:   {
                   6864:     /*
                   6865:      * Send chip select for one clock cycle.
                   6866:      */
                   6867:     aic_outb(p, SEEMS | SEECK | SEECS, SEECTL);
                   6868:     CLOCK_PULSE(p);
                   6869: 
                   6870:     /*
                   6871:      * Now we're ready to send the read command followed by the
                   6872:      * address of the 16-bit register we want to read.
                   6873:      */
                   6874:     for (i = 0; i < seeprom_read.len; i++)
                   6875:     {
                   6876:       temp = SEEMS | SEECS | (seeprom_read.bits[i] << 1);
                   6877:       aic_outb(p, temp, SEECTL);
                   6878:       CLOCK_PULSE(p);
                   6879:       temp = temp ^ SEECK;
                   6880:       aic_outb(p, temp, SEECTL);
                   6881:       CLOCK_PULSE(p);
                   6882:     }
                   6883:     /*
                   6884:      * Send the 6 or 8 bit address (MSB first, LSB last).
                   6885:      */
                   6886:     for (i = ((int) chip - 1); i >= 0; i--)
                   6887:     {
                   6888:       temp = k + offset;
                   6889:       temp = (temp >> i) & 1;  /* Mask out all but lower bit. */
                   6890:       temp = SEEMS | SEECS | (temp << 1);
                   6891:       aic_outb(p, temp, SEECTL);
                   6892:       CLOCK_PULSE(p);
                   6893:       temp = temp ^ SEECK;
                   6894:       aic_outb(p, temp, SEECTL);
                   6895:       CLOCK_PULSE(p);
                   6896:     }
                   6897: 
                   6898:     /*
                   6899:      * Now read the 16 bit register.  An initial 0 precedes the
                   6900:      * register contents which begins with bit 15 (MSB) and ends
                   6901:      * with bit 0 (LSB).  The initial 0 will be shifted off the
                   6902:      * top of our word as we let the loop run from 0 to 16.
                   6903:      */
                   6904:     for (i = 0; i <= 16; i++)
                   6905:     {
                   6906:       temp = SEEMS | SEECS;
                   6907:       aic_outb(p, temp, SEECTL);
                   6908:       CLOCK_PULSE(p);
                   6909:       temp = temp ^ SEECK;
                   6910:       scarray[k] = (scarray[k] << 1) | (aic_inb(p, SEECTL) & SEEDI);
                   6911:       aic_outb(p, temp, SEECTL);
                   6912:       CLOCK_PULSE(p);
                   6913:     }
                   6914: 
                   6915:     /*
                   6916:      * The serial EEPROM should have a checksum in the last word.
                   6917:      * Keep a running checksum for all words read except for the
                   6918:      * last word.  We'll verify the checksum after all words have
                   6919:      * been read.
                   6920:      */
                   6921:     if (k < (len - 1))
                   6922:     {
                   6923:       checksum = checksum + scarray[k];
                   6924:     }
                   6925: 
                   6926:     /*
                   6927:      * Reset the chip select for the next command cycle.
                   6928:      */
                   6929:     aic_outb(p, SEEMS, SEECTL);
                   6930:     CLOCK_PULSE(p);
                   6931:     aic_outb(p, SEEMS | SEECK, SEECTL);
                   6932:     CLOCK_PULSE(p);
                   6933:     aic_outb(p, SEEMS, SEECTL);
                   6934:     CLOCK_PULSE(p);
                   6935:   }
                   6936: 
                   6937:   /*
                   6938:    * Release access to the memory port and the serial EEPROM.
                   6939:    */
                   6940:   release_seeprom(p);
                   6941: 
                   6942: #if 0
                   6943:   printk("Computed checksum 0x%x, checksum read 0x%x\n",
                   6944:          checksum, scarray[len - 1]);
                   6945:   printk("Serial EEPROM:");
                   6946:   for (k = 0; k < len; k++)
                   6947:   {
                   6948:     if (((k % 8) == 0) && (k != 0))
                   6949:     {
                   6950:       printk("\n              ");
                   6951:     }
                   6952:     printk(" 0x%x", scarray[k]);
                   6953:   }
                   6954:   printk("\n");
                   6955: #endif
1.1.1.2 ! root     6956:   if ( (checksum != scarray[len - 1]) || (checksum == 0) )
1.1       root     6957:   {
                   6958:     return (0);
                   6959:   }
                   6960: 
                   6961:   return (1);
                   6962: #undef CLOCK_PULSE
                   6963: }
                   6964: 
                   6965: /*+F*************************************************************************
                   6966:  * Function:
                   6967:  *   write_brdctl
                   6968:  *
                   6969:  * Description:
                   6970:  *   Writes a value to the BRDCTL register.
                   6971:  *-F*************************************************************************/
                   6972: static void
                   6973: write_brdctl(struct aic7xxx_host *p, unsigned char value)
                   6974: {
                   6975:   unsigned char brdctl;
                   6976: 
                   6977:   if ((p->chip & AHC_CHIPID_MASK) == AHC_AIC7895)
                   6978:   {
                   6979:     brdctl = BRDSTB;
                   6980:     if (p->flags & AHC_CHNLB)
                   6981:       brdctl |= BRDCS;
                   6982:   }
                   6983:   else if (p->features & AHC_ULTRA2)
                   6984:     brdctl = 0;
                   6985:   else
                   6986:     brdctl = BRDSTB | BRDCS;
                   6987:   aic_outb(p, brdctl, BRDCTL);
                   6988:   udelay(1);
                   6989:   brdctl |= value;
                   6990:   aic_outb(p, brdctl, BRDCTL);
                   6991:   udelay(1);
                   6992:   if (p->features & AHC_ULTRA2)
                   6993:     brdctl |= BRDSTB_ULTRA2;
                   6994:   else
                   6995:     brdctl &= ~BRDSTB;
                   6996:   aic_outb(p, brdctl, BRDCTL);
                   6997:   udelay(1);
                   6998:   if (p->features & AHC_ULTRA2)
                   6999:     brdctl = 0;
                   7000:   else
                   7001:     brdctl &= ~BRDCS;
                   7002:   aic_outb(p, brdctl, BRDCTL);
                   7003:   udelay(1);
                   7004: }
                   7005: 
                   7006: /*+F*************************************************************************
                   7007:  * Function:
                   7008:  *   read_brdctl
                   7009:  *
                   7010:  * Description:
                   7011:  *   Reads the BRDCTL register.
                   7012:  *-F*************************************************************************/
                   7013: static unsigned char
                   7014: read_brdctl(struct aic7xxx_host *p)
                   7015: {
                   7016:   unsigned char brdctl, value;
                   7017: 
                   7018:   if ((p->chip & AHC_CHIPID_MASK) == AHC_AIC7895)
                   7019:   {
                   7020:     brdctl = BRDRW;
                   7021:     if (p->flags & AHC_CHNLB)
                   7022:       brdctl |= BRDCS;
                   7023:   }
                   7024:   else if (p->features & AHC_ULTRA2)
                   7025:     brdctl = BRDRW_ULTRA2;
                   7026:   else
                   7027:     brdctl = BRDRW | BRDCS;
                   7028:   aic_outb(p, brdctl, BRDCTL);
                   7029:   udelay(1);
                   7030:   value = aic_inb(p, BRDCTL);
                   7031:   aic_outb(p, 0, BRDCTL);
                   7032:   udelay(1);
                   7033:   return (value);
                   7034: }
                   7035: 
                   7036: /*+F*************************************************************************
                   7037:  * Function:
                   7038:  *   aic785x_cable_detect
                   7039:  *
                   7040:  * Description:
                   7041:  *   Detect the cables that are present on aic785x class controller chips
                   7042:  *-F*************************************************************************/
                   7043: static void
                   7044: aic785x_cable_detect(struct aic7xxx_host *p, int *int_50,
                   7045:     int *ext_present, int *eeprom)
                   7046: {
                   7047:   unsigned char brdctl;
                   7048: 
                   7049:   aic_outb(p, BRDRW | BRDCS, BRDCTL);
                   7050:   udelay(1);
                   7051:   aic_outb(p, 0, BRDCTL);
                   7052:   udelay(1);
                   7053:   brdctl = aic_inb(p, BRDCTL);
                   7054:   udelay(1);
                   7055:   *int_50 = !(brdctl & BRDDAT5);
                   7056:   *ext_present = !(brdctl & BRDDAT6);
                   7057:   *eeprom = (aic_inb(p, SPIOCAP) & EEPROM);
                   7058: }
                   7059: 
                   7060: /*+F*************************************************************************
                   7061:  * Function:
                   7062:  *   aic787x_cable_detect
                   7063:  *
                   7064:  * Description:
                   7065:  *   Detect the cables that are present on aic787x class controller chips
                   7066:  *
                   7067:  * NOTE: This functions assumes the SEEPROM will have already been aquired
                   7068:  *       prior to invocation of this function.
                   7069:  *-F*************************************************************************/
                   7070: static void
                   7071: aic787x_cable_detect(struct aic7xxx_host *p, int *int_50, int *int_68,
                   7072:     int *ext_present, int *eeprom)
                   7073: {
                   7074:   unsigned char brdctl;
                   7075: 
                   7076:   /*
                   7077:    * First read the status of our cables.  Set the rom bank to
                   7078:    * 0 since the bank setting serves as a multiplexor for the
                   7079:    * cable detection logic.  BRDDAT5 controls the bank switch.
                   7080:    */
                   7081:   write_brdctl(p, 0);
                   7082: 
                   7083:   /*
                   7084:    * Now we read the state of the two internal connectors.  BRDDAT6
                   7085:    * is internal 50, BRDDAT7 is internal 68.  For each, the cable is
                   7086:    * present if the bit is 0
                   7087:    */
                   7088:   brdctl = read_brdctl(p);
                   7089:   *int_50 = !(brdctl & BRDDAT6);
                   7090:   *int_68 = !(brdctl & BRDDAT7);
                   7091: 
                   7092:   /*
                   7093:    * Set the bank bit in brdctl and then read the external cable state
                   7094:    * and the EEPROM status
                   7095:    */
                   7096:   write_brdctl(p, BRDDAT5);
                   7097:   brdctl = read_brdctl(p);
                   7098: 
                   7099:   *ext_present = !(brdctl & BRDDAT6);
                   7100:   *eeprom = !(brdctl & BRDDAT7);
                   7101: 
                   7102:   /*
                   7103:    * We're done, the calling function will release the SEEPROM for us
                   7104:    */
                   7105: }
                   7106: 
                   7107: /*+F*************************************************************************
                   7108:  * Function:
                   7109:  *   aic787x_ultra2_term_detect
                   7110:  *
                   7111:  * Description:
                   7112:  *   Detect the termination settings present on ultra2 class controllers
                   7113:  *
                   7114:  * NOTE: This functions assumes the SEEPROM will have already been aquired
                   7115:  *       prior to invocation of this function.
                   7116:  *-F*************************************************************************/
                   7117: static void
                   7118: aic7xxx_ultra2_term_detect(struct aic7xxx_host *p, int *enableSE_low,
                   7119:                            int *enableSE_high, int *enableLVD_low,
                   7120:                            int *enableLVD_high, int *eprom_present)
                   7121: {
                   7122:   unsigned char brdctl;
                   7123: 
                   7124:   brdctl = read_brdctl(p);
                   7125: 
                   7126:   *eprom_present  = (brdctl & BRDDAT7);
                   7127:   *enableSE_high  = (brdctl & BRDDAT6);
                   7128:   *enableSE_low   = (brdctl & BRDDAT5);
                   7129:   *enableLVD_high = (brdctl & BRDDAT4);
                   7130:   *enableLVD_low  = (brdctl & BRDDAT3);
                   7131: }
                   7132: 
                   7133: /*+F*************************************************************************
                   7134:  * Function:
                   7135:  *   configure_termination
                   7136:  *
                   7137:  * Description:
                   7138:  *   Configures the termination settings on PCI adapters that have
                   7139:  *   SEEPROMs available.
                   7140:  *-F*************************************************************************/
                   7141: static void
                   7142: configure_termination(struct aic7xxx_host *p)
                   7143: {
                   7144:   int internal50_present = 0;
                   7145:   int internal68_present = 0;
                   7146:   int external_present = 0;
                   7147:   int eprom_present = 0;
                   7148:   int enableSE_low = 0;
                   7149:   int enableSE_high = 0;
                   7150:   int enableLVD_low = 0;
                   7151:   int enableLVD_high = 0;
                   7152:   unsigned char brddat = 0;
                   7153:   unsigned char max_target = 0;
                   7154:   unsigned char sxfrctl1 = aic_inb(p, SXFRCTL1);
                   7155: 
                   7156:   if (acquire_seeprom(p))
                   7157:   {
                   7158:     if (p->features & (AHC_WIDE|AHC_TWIN))
                   7159:       max_target = 16;
                   7160:     else
                   7161:       max_target = 8;
                   7162:     aic_outb(p, SEEMS | SEECS, SEECTL);
                   7163:     sxfrctl1 &= ~STPWEN;
                   7164:     if ( (p->adapter_control & CFAUTOTERM) ||
                   7165:          (p->features & AHC_ULTRA2) )
                   7166:     {
                   7167:       if ( (p->adapter_control & CFAUTOTERM) && !(p->features & AHC_ULTRA2) )
                   7168:       {
                   7169:         printk(KERN_INFO "(scsi%d) Warning - detected auto-termination\n",
                   7170:                p->host_no);
                   7171:         printk(KERN_INFO "(scsi%d) Please verify driver detected settings are "
                   7172:           "correct.\n", p->host_no);
                   7173:         printk(KERN_INFO "(scsi%d) If not, then please properly set the device "
                   7174:           "termination\n", p->host_no);
                   7175:         printk(KERN_INFO "(scsi%d) in the Adaptec SCSI BIOS by hitting CTRL-A "
                   7176:           "when prompted\n", p->host_no);
                   7177:         printk(KERN_INFO "(scsi%d) during machine bootup.\n", p->host_no);
                   7178:       }
                   7179:       /* Configure auto termination. */
                   7180: 
                   7181:       if (p->features & AHC_ULTRA2)
                   7182:       {
                   7183:         if (aic7xxx_override_term == -1)
                   7184:           aic7xxx_ultra2_term_detect(p, &enableSE_low, &enableSE_high,
                   7185:                                      &enableLVD_low, &enableLVD_high,
                   7186:                                      &eprom_present);
                   7187:         if (!(p->adapter_control & CFSEAUTOTERM))
                   7188:         {
                   7189:           enableSE_low = (p->adapter_control & CFSTERM);
                   7190:           enableSE_high = (p->adapter_control & CFWSTERM);
                   7191:         }
                   7192:         if (!(p->adapter_control & CFAUTOTERM))
                   7193:         {
                   7194:           enableLVD_low = enableLVD_high = (p->adapter_control & CFLVDSTERM);
                   7195:         }
                   7196:         internal50_present = 0;
                   7197:         internal68_present = 1;
                   7198:         external_present = 1;
                   7199:       }
                   7200:       else if ( (p->chip & AHC_CHIPID_MASK) >= AHC_AIC7870 )
                   7201:       {
                   7202:         aic787x_cable_detect(p, &internal50_present, &internal68_present,
                   7203:           &external_present, &eprom_present);
                   7204:       }
                   7205:       else
                   7206:       {
                   7207:         aic785x_cable_detect(p, &internal50_present, &external_present,
                   7208:           &eprom_present);
                   7209:       }
1.1.1.2 ! root     7210: 
1.1       root     7211:       if (max_target <= 8)
                   7212:         internal68_present = 0;
                   7213: 
                   7214:       if ( !(p->features & AHC_ULTRA2) )
                   7215:       {
                   7216:         if (max_target > 8)
                   7217:         {
                   7218:           printk(KERN_INFO "(scsi%d) Cables present (Int-50 %s, Int-68 %s, "
                   7219:                  "Ext-68 %s)\n", p->host_no,
                   7220:                  internal50_present ? "YES" : "NO",
                   7221:                  internal68_present ? "YES" : "NO",
                   7222:                  external_present ? "YES" : "NO");
                   7223:         }
                   7224:         else
                   7225:         {
                   7226:           printk(KERN_INFO "(scsi%d) Cables present (Int-50 %s, Ext-50 %s)\n",
                   7227:                  p->host_no,
                   7228:                  internal50_present ? "YES" : "NO",
                   7229:                  external_present ? "YES" : "NO");
                   7230:         }
                   7231:       }
                   7232:       if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7233:         printk(KERN_INFO "(scsi%d) EEPROM %s present.\n", p->host_no,
                   7234:              eprom_present ? "is" : "is not");
                   7235: 
                   7236:       /*
                   7237:        * Now set the termination based on what we found.  BRDDAT6
                   7238:        * controls wide termination enable.
                   7239:        * Flash Enable = BRDDAT7
                   7240:        * SE High Term Enable = BRDDAT6
                   7241:        * SE Low Term Enable = BRDDAT5 (7890)
                   7242:        * LVD High Term Enable = BRDDAT4 (7890)
                   7243:        */
                   7244:       if ( !(p->features & AHC_ULTRA2) &&
                   7245:            (internal50_present && internal68_present && external_present) )
                   7246:       {
                   7247:         printk(KERN_INFO "(scsi%d) Illegal cable configuration!!  Only two\n",
                   7248:                p->host_no);
                   7249:         printk(KERN_INFO "(scsi%d) connectors on the SCSI controller may be "
                   7250:                "in use at a time!\n", p->host_no);
                   7251:         /*
                   7252:          * Force termination (low and high byte) on.  This is safer than
                   7253:          * leaving it completely off, especially since this message comes
                   7254:          * most often from motherboard controllers that don't even have 3
                   7255:          * connectors, but instead are failing the cable detection.
                   7256:          */
                   7257:         internal50_present = external_present = 0;
                   7258:         enableSE_high = enableSE_low = 1;
                   7259:       }
                   7260: 
                   7261:       if ((max_target > 8) &&
                   7262:           ((external_present == 0) || (internal68_present == 0) ||
                   7263:            (enableSE_high != 0)))
                   7264:       {
                   7265:         brddat |= BRDDAT6;
                   7266:         p->flags |= AHC_TERM_ENB_SE_HIGH;
                   7267:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7268:           printk(KERN_INFO "(scsi%d) SE High byte termination Enabled\n",
                   7269:                  p->host_no);
                   7270:       }
                   7271: 
                   7272:       if ( (((internal50_present ? 1 : 0) +
                   7273:              (internal68_present ? 1 : 0) +
                   7274:              (external_present   ? 1 : 0)) <= 1) ||
                   7275:            (enableSE_low != 0) )
                   7276:       {
                   7277:         if (p->features & AHC_ULTRA2)
                   7278:           brddat |= BRDDAT5;
                   7279:         else
                   7280:           sxfrctl1 |= STPWEN;
                   7281:         p->flags |= AHC_TERM_ENB_SE_LOW;
                   7282:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7283:           printk(KERN_INFO "(scsi%d) SE Low byte termination Enabled\n",
                   7284:                  p->host_no);
                   7285:       }
                   7286: 
                   7287:       if (enableLVD_low != 0)
                   7288:       {
                   7289:         sxfrctl1 |= STPWEN;
                   7290:         p->flags |= AHC_TERM_ENB_LVD;
                   7291:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7292:           printk(KERN_INFO "(scsi%d) LVD Low byte termination Enabled\n",
                   7293:                  p->host_no);
                   7294:       }
1.1.1.2 ! root     7295: 
1.1       root     7296:       if (enableLVD_high != 0)
                   7297:       {
                   7298:         brddat |= BRDDAT4;
                   7299:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7300:           printk(KERN_INFO "(scsi%d) LVD High byte termination Enabled\n",
                   7301:                  p->host_no);
                   7302:       }
                   7303:     }
                   7304:     else
                   7305:     {
                   7306:       if (p->adapter_control & CFSTERM)
                   7307:       {
                   7308:         if (p->features & AHC_ULTRA2)
                   7309:           brddat |= BRDDAT5;
                   7310:         else
                   7311:           sxfrctl1 |= STPWEN;
                   7312:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7313:           printk(KERN_INFO "(scsi%d) SE Low byte termination Enabled\n",
                   7314:                  p->host_no);
                   7315:       }
                   7316: 
                   7317:       if (p->adapter_control & CFWSTERM)
                   7318:       {
                   7319:         brddat |= BRDDAT6;
                   7320:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7321:           printk(KERN_INFO "(scsi%d) SE High byte termination Enabled\n",
                   7322:                  p->host_no);
                   7323:       }
                   7324:     }
                   7325:     write_brdctl(p, brddat);
                   7326:     release_seeprom(p);
                   7327:     aic_outb(p, sxfrctl1, SXFRCTL1);
                   7328:   }
                   7329: }
                   7330: 
                   7331: /*+F*************************************************************************
                   7332:  * Function:
                   7333:  *   detect_maxscb
                   7334:  *
                   7335:  * Description:
                   7336:  *   Detects the maximum number of SCBs for the controller and returns
                   7337:  *   the count and a mask in p (p->maxscbs, p->qcntmask).
                   7338:  *-F*************************************************************************/
                   7339: static void
                   7340: detect_maxscb(struct aic7xxx_host *p)
                   7341: {
                   7342:   int i;
                   7343: 
                   7344:   /*
                   7345:    * It's possible that we've already done this for multichannel
                   7346:    * adapters.
                   7347:    */
                   7348:   if (p->scb_data->maxhscbs == 0)
                   7349:   {
                   7350:     /*
                   7351:      * We haven't initialized the SCB settings yet.  Walk the SCBs to
                   7352:      * determince how many there are.
                   7353:      */
                   7354:     aic_outb(p, 0, FREE_SCBH);
                   7355: 
                   7356:     for (i = 0; i < AIC7XXX_MAXSCB; i++)
                   7357:     {
                   7358:       aic_outb(p, i, SCBPTR);
                   7359:       aic_outb(p, i, SCB_CONTROL);
                   7360:       if (aic_inb(p, SCB_CONTROL) != i)
                   7361:         break;
                   7362:       aic_outb(p, 0, SCBPTR);
                   7363:       if (aic_inb(p, SCB_CONTROL) != 0)
                   7364:         break;
                   7365: 
                   7366:       aic_outb(p, i, SCBPTR);
                   7367:       aic_outb(p, 0, SCB_CONTROL);   /* Clear the control byte. */
                   7368:       aic_outb(p, i + 1, SCB_NEXT);  /* Set the next pointer. */
                   7369:       aic_outb(p, i - 1, SCB_PREV);  /* Set the prev pointer. */
                   7370:       aic_outb(p, SCB_LIST_NULL, SCB_TAG);  /* Make the tag invalid. */
                   7371:       aic_outb(p, SCB_LIST_NULL, SCB_BUSYTARGETS);  /* no busy untagged */
                   7372:       aic_outb(p, SCB_LIST_NULL, SCB_BUSYTARGETS+1);/* targets active yet */
                   7373:       aic_outb(p, SCB_LIST_NULL, SCB_BUSYTARGETS+2);
                   7374:       aic_outb(p, SCB_LIST_NULL, SCB_BUSYTARGETS+3);
                   7375:     }
                   7376: 
                   7377:     /* Make sure the last SCB terminates the free list. */
                   7378:     aic_outb(p, i - 1, SCBPTR);
                   7379:     aic_outb(p, SCB_LIST_NULL, SCB_NEXT);
                   7380: 
                   7381:     /* Ensure we clear the first (0) SCBs control byte. */
                   7382:     aic_outb(p, 0, SCBPTR);
                   7383:     aic_outb(p, 0, SCB_CONTROL);
                   7384: 
                   7385:     p->scb_data->maxhscbs = i;
                   7386:     /*
                   7387:      * Use direct indexing instead for speed
                   7388:      */
                   7389:     if ( i == AIC7XXX_MAXSCB )
                   7390:       p->flags &= ~AHC_PAGESCBS;
                   7391:   }
                   7392: 
                   7393: }
                   7394: 
                   7395: /*+F*************************************************************************
                   7396:  * Function:
                   7397:  *   aic7xxx_register
                   7398:  *
                   7399:  * Description:
                   7400:  *   Register a Adaptec aic7xxx chip SCSI controller with the kernel.
                   7401:  *-F*************************************************************************/
                   7402: static int
                   7403: aic7xxx_register(Scsi_Host_Template *template, struct aic7xxx_host *p,
                   7404:   int reset_delay)
                   7405: {
                   7406:   int i, result;
                   7407:   int max_targets;
                   7408:   int found = 1;
                   7409:   unsigned char term, scsi_conf;
                   7410:   struct Scsi_Host *host;
                   7411: 
                   7412:   /*
                   7413:    * Lock out other contenders for our i/o space.
                   7414:    */
                   7415:   request_region(p->base, MAXREG - MINREG, "aic7xxx");
                   7416: 
                   7417: 
                   7418:   host = p->host;
                   7419: 
                   7420:   p->scb_data->maxscbs = AIC7XXX_MAXSCB;
                   7421:   host->can_queue = AIC7XXX_MAXSCB;
                   7422:   host->cmd_per_lun = 3;
                   7423:   host->sg_tablesize = AIC7XXX_MAX_SG;
                   7424:   host->select_queue_depths = aic7xxx_select_queue_depth;
                   7425:   host->this_id = p->scsi_id;
                   7426:   host->io_port = p->base;
                   7427:   host->n_io_port = 0xFF;
                   7428:   host->base = (unsigned char *) p->mbase;
                   7429:   host->irq = p->irq;
                   7430:   if (p->features & AHC_WIDE)
                   7431:   {
                   7432:     host->max_id = 16;
                   7433:   }
                   7434:   if (p->features & AHC_TWIN)
                   7435:   {
                   7436:     host->max_channel = 1;
                   7437:   }
                   7438: 
                   7439:   p->host = host;
                   7440:   p->host_no = host->host_no;
                   7441:   host->unique_id = p->instance;
                   7442:   p->isr_count = 0;
                   7443:   p->next = NULL;
                   7444:   p->completeq.head = NULL;
                   7445:   p->completeq.tail = NULL;
                   7446:   scbq_init(&p->scb_data->free_scbs);
                   7447:   scbq_init(&p->waiting_scbs);
1.1.1.2 ! root     7448:   init_timer(&p->dev_timer);
        !          7449:   p->dev_timer.data = (unsigned long)p;
        !          7450:   p->dev_timer.function = (void *)aic7xxx_timer;
        !          7451:   p->dev_timer_active = 0;
1.1       root     7452: 
                   7453:   for (i = 0; i < NUMBER(p->untagged_scbs); i++)
                   7454:   {
                   7455:     p->untagged_scbs[i] = SCB_LIST_NULL;
                   7456:     p->qinfifo[i] = SCB_LIST_NULL;
                   7457:     p->qoutfifo[i] = SCB_LIST_NULL;
                   7458:   }
                   7459:   /*
                   7460:    * We currently have no commands of any type
                   7461:    */
                   7462:   p->qinfifonext = 0;
                   7463:   p->qoutfifonext = 0;
                   7464: 
                   7465:   for (i = 0; i < MAX_TARGETS; i++)
                   7466:   {
                   7467:     p->dev_commands_sent[i] = 0;
                   7468:     p->dev_flags[i] = 0;
                   7469:     p->dev_active_cmds[i] = 0;
                   7470:     p->dev_last_queue_full[i] = 0;
                   7471:     p->dev_last_queue_full_count[i] = 0;
                   7472:     p->dev_max_queue_depth[i] = 1;
                   7473:     p->dev_temp_queue_depth[i] = 1;
1.1.1.2 ! root     7474:     p->dev_expires[i] = 0;
1.1       root     7475:     scbq_init(&p->delayed_scbs[i]);
                   7476:   }
                   7477: 
                   7478:   printk(KERN_INFO "(scsi%d) <%s> found at ", p->host_no,
                   7479:     board_names[p->board_name_index]);
                   7480:   switch(p->chip)
                   7481:   {
                   7482:     case (AHC_AIC7770|AHC_EISA):
                   7483:       printk("EISA slot %d\n", p->pci_device_fn);
                   7484:       break;
                   7485:     case (AHC_AIC7770|AHC_VL):
                   7486:       printk("VLB slot %d\n", p->pci_device_fn);
                   7487:       break;
                   7488:     default:
                   7489:       printk("PCI %d/%d\n", PCI_SLOT(p->pci_device_fn),
                   7490:         PCI_FUNC(p->pci_device_fn));
                   7491:       break;
                   7492:   }
                   7493:   if (p->features & AHC_TWIN)
                   7494:   {
                   7495:     printk(KERN_INFO "(scsi%d) Twin Channel, A SCSI ID %d, B SCSI ID %d, ",
                   7496:            p->host_no, p->scsi_id, p->scsi_id_b);
                   7497:   }
                   7498:   else
                   7499:   {
                   7500:     char *channel;
                   7501: 
                   7502:     channel = "";
                   7503: 
                   7504:     if ((p->flags & AHC_MULTI_CHANNEL) != 0)
                   7505:     {
                   7506:       channel = " A";
                   7507: 
                   7508:       if ( (p->flags & (AHC_CHNLB|AHC_CHNLC)) != 0 )
                   7509:       {
                   7510:         channel = (p->flags & AHC_CHNLB) ? " B" : " C";
                   7511:       }
                   7512:     }
                   7513:     if (p->features & AHC_WIDE)
                   7514:     {
                   7515:       printk(KERN_INFO "(scsi%d) Wide ", p->host_no);
                   7516:     }
                   7517:     else
                   7518:     {
                   7519:       printk(KERN_INFO "(scsi%d) Narrow ", p->host_no);
                   7520:     }
                   7521:     printk("Channel%s, SCSI ID=%d, ", channel, p->scsi_id);
                   7522:   }
                   7523:   aic_outb(p, 0, SEQ_FLAGS);
                   7524: 
                   7525:   detect_maxscb(p);
1.1.1.2 ! root     7526: 
        !          7527: 
1.1       root     7528:   printk("%d/%d SCBs\n", p->scb_data->maxhscbs, p->scb_data->maxscbs);
                   7529:   if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7530:   {
                   7531:     printk(KERN_INFO "(scsi%d) BIOS %sabled, IO Port 0x%lx, IRQ %d\n",
                   7532:       p->host_no, (p->flags & AHC_BIOS_ENABLED) ? "en" : "dis",
                   7533:       p->base, p->irq);
                   7534:     printk(KERN_INFO "(scsi%d) IO Memory at 0x%lx, MMAP Memory at 0x%lx\n",
                   7535:       p->host_no, p->mbase, (unsigned long)p->maddr);
                   7536:   }
                   7537: 
                   7538: #ifdef CONFIG_PCI
                   7539:   /*
                   7540:    * Now that we know our instance number, we can set the flags we need to
                   7541:    * force termination if need be.
                   7542:    */
                   7543:   if (aic7xxx_stpwlev != -1)
                   7544:   {
                   7545:     /*
                   7546:      * This option only applies to PCI controllers.
                   7547:      */
                   7548:     if ( (p->chip & ~AHC_CHIPID_MASK) == AHC_PCI)
                   7549:     {
                   7550:       unsigned char devconfig;
                   7551: 
                   7552: #if LINUX_KERNEL_VERSION > KERNEL_VERSION(2,1,92)
                   7553:       pci_read_config_byte(p->pdev, DEVCONFIG, &devconfig);
                   7554: #else
                   7555:       pcibios_read_config_byte(p->pci_bus, p->pci_device_fn,
                   7556:                                DEVCONFIG, &devconfig);
                   7557: #endif
                   7558:       if ( (aic7xxx_stpwlev >> p->instance) & 0x01 )
                   7559:       {
                   7560:         devconfig |= 0x02;
                   7561:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7562:           printk("(scsi%d) Force setting STPWLEV bit\n", p->host_no);
                   7563:       }
                   7564:       else
                   7565:       {
                   7566:         devconfig &= ~0x02;
                   7567:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7568:           printk("(scsi%d) Force clearing STPWLEV bit\n", p->host_no);
                   7569:       }
                   7570: #if LINUX_KERNEL_VERSION > KERNEL_VERSION(2,1,92)
                   7571:       pci_write_config_byte(p->pdev, DEVCONFIG, devconfig);
                   7572: #else
                   7573:       pcibios_write_config_byte(p->pci_bus, p->pci_device_fn,
                   7574:                                 DEVCONFIG, devconfig);
                   7575: #endif
                   7576:     }
                   7577:   }
                   7578: #endif
                   7579: 
                   7580:   /*
                   7581:    * That took care of devconfig and stpwlev, now for the actual termination
                   7582:    * settings.
                   7583:    */
                   7584:   if (aic7xxx_override_term != -1)
                   7585:   {
                   7586:     /*
                   7587:      * Again, this only applies to PCI controllers.  We don't have problems
                   7588:      * with the termination on 274x controllers to the best of my knowledge.
                   7589:      */
                   7590:     if ( (p->chip & ~AHC_CHIPID_MASK) == AHC_PCI)
                   7591:     {
                   7592:       unsigned char term_override;
                   7593: 
                   7594:       term_override = ( (aic7xxx_override_term >> (p->instance * 4)) & 0x0f);
1.1.1.2 ! root     7595:       p->adapter_control &=
1.1       root     7596:         ~(CFSTERM|CFWSTERM|CFLVDSTERM|CFAUTOTERM|CFSEAUTOTERM);
                   7597:       if ( (p->features & AHC_ULTRA2) && (term_override & 0x0c) )
                   7598:       {
                   7599:         p->adapter_control |= CFLVDSTERM;
                   7600:       }
                   7601:       if (term_override & 0x02)
                   7602:       {
                   7603:         p->adapter_control |= CFWSTERM;
                   7604:       }
                   7605:       if (term_override & 0x01)
                   7606:       {
                   7607:         p->adapter_control |= CFSTERM;
                   7608:       }
                   7609:     }
                   7610:   }
                   7611: 
                   7612:   if ( (p->flags & AHC_SEEPROM_FOUND) || (aic7xxx_override_term != -1) )
                   7613:   {
                   7614:     if (p->features & AHC_SPIOCAP)
                   7615:     {
                   7616:       if ( aic_inb(p, SPIOCAP) & SSPIOCPS )
                   7617:       /*
                   7618:        * Update the settings in sxfrctl1 to match the termination
                   7619:        * settings.
                   7620:        */
                   7621:         configure_termination(p);
                   7622:     }
                   7623:     else if ((p->chip & AHC_CHIPID_MASK) >= AHC_AIC7870)
                   7624:     {
                   7625:       configure_termination(p);
                   7626:     }
                   7627:   }
                   7628: 
                   7629:   /*
                   7630:    * Clear out any possible pending interrupts.
                   7631:    */
                   7632:   aic7xxx_clear_intstat(p);
                   7633: 
                   7634:   /*
                   7635:    * Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels
                   7636:    */
                   7637:   if (p->features & AHC_TWIN)
                   7638:   {
                   7639:     /* Select channel B */
                   7640:     aic_outb(p, aic_inb(p, SBLKCTL) | SELBUSB, SBLKCTL);
                   7641: 
                   7642:     term = ((p->flags & AHC_TERM_ENB_B) != 0) ? STPWEN : 0;
                   7643:     aic_outb(p, p->scsi_id_b, SCSIID);
                   7644:     scsi_conf = aic_inb(p, SCSICONF + 1);
                   7645:     aic_outb(p, DFON | SPIOEN, SXFRCTL0);
1.1.1.2 ! root     7646:     aic_outb(p, (scsi_conf & ENSPCHK) | STIMESEL | term |
1.1       root     7647:          ENSTIMER | ACTNEGEN, SXFRCTL1);
                   7648:     aic_outb(p, 0, SIMODE0);
                   7649:     aic_outb(p, ENSELTIMO | ENSCSIRST | ENSCSIPERR, SIMODE1);
                   7650:     aic_outb(p, 0, SCSIRATE);
                   7651: 
                   7652:     /* Select channel A */
                   7653:     aic_outb(p, aic_inb(p, SBLKCTL) & ~SELBUSB, SBLKCTL);
                   7654:   }
                   7655: 
                   7656:   term = ((p->flags & AHC_TERM_ENB_SE_LOW) != 0) ? STPWEN : 0;
                   7657:   if (p->features & AHC_ULTRA2)
                   7658:     aic_outb(p, p->scsi_id, SCSIID_ULTRA2);
                   7659:   else
                   7660:     aic_outb(p, p->scsi_id, SCSIID);
                   7661:   scsi_conf = aic_inb(p, SCSICONF);
                   7662:   aic_outb(p, DFON | SPIOEN, SXFRCTL0);
1.1.1.2 ! root     7663:   aic_outb(p, (scsi_conf & ENSPCHK) | STIMESEL | term |
1.1       root     7664:        ENSTIMER | ACTNEGEN, SXFRCTL1);
                   7665:   aic_outb(p, 0, SIMODE0);
                   7666:   aic_outb(p, ENSELTIMO | ENSCSIRST | ENSCSIPERR, SIMODE1);
                   7667:   aic_outb(p, 0, SCSIRATE);
                   7668:   if ( p->features & AHC_ULTRA2)
                   7669:     aic_outb(p, 0, SCSIOFFSET);
                   7670: 
                   7671:   /*
                   7672:    * Look at the information that board initialization or the board
                   7673:    * BIOS has left us. In the lower four bits of each target's
                   7674:    * scratch space any value other than 0 indicates that we should
                   7675:    * initiate synchronous transfers. If it's zero, the user or the
                   7676:    * BIOS has decided to disable synchronous negotiation to that
                   7677:    * target so we don't activate the needsdtr flag.
                   7678:    */
                   7679:   if ((p->features & (AHC_TWIN|AHC_WIDE)) == 0)
                   7680:   {
                   7681:     max_targets = 8;
                   7682:   }
                   7683:   else
                   7684:   {
                   7685:     max_targets = 16;
                   7686:   }
                   7687: 
                   7688:   if (!(aic7xxx_no_reset))
                   7689:   {
                   7690:     /*
                   7691:      * If we reset the bus, then clear the transfer settings, else leave
                   7692:      * them be
                   7693:      */
                   7694:     for (i = 0; i < max_targets; i++)
                   7695:     {
                   7696:       aic_outb(p, 0, TARG_SCSIRATE + i);
                   7697:       if (p->features & AHC_ULTRA2)
                   7698:       {
                   7699:         aic_outb(p, 0, TARG_OFFSET + i);
                   7700:       }
                   7701:       p->transinfo[i].cur_offset = 0;
                   7702:       p->transinfo[i].cur_period = 0;
                   7703:       p->transinfo[i].cur_width = MSG_EXT_WDTR_BUS_8_BIT;
                   7704:     }
                   7705: 
                   7706:     /*
                   7707:      * If we reset the bus, then clear the transfer settings, else leave
                   7708:      * them be.
                   7709:      */
                   7710:     aic_outb(p, 0, ULTRA_ENB);
                   7711:     aic_outb(p, 0, ULTRA_ENB + 1);
                   7712:     p->ultraenb = 0;
                   7713:   }
                   7714: 
                   7715:   /*
                   7716:    * Allocate enough hardware scbs to handle the maximum number of
                   7717:    * concurrent transactions we can have.  We have to make sure that
                   7718:    * the allocated memory is contiguous memory.  The Linux kmalloc
                   7719:    * routine should only allocate contiguous memory, but note that
                   7720:    * this could be a problem if kmalloc() is changed.
                   7721:    */
                   7722:   {
                   7723:     size_t array_size;
                   7724:     unsigned int hscb_physaddr;
                   7725:     unsigned long temp;
                   7726: 
                   7727:     array_size = p->scb_data->maxscbs * sizeof(struct aic7xxx_hwscb);
                   7728:     if (p->scb_data->hscbs == NULL)
                   7729:     {
                   7730:       /*
                   7731:        * A little padding so we can align thing the way we want
                   7732:        */
                   7733:       p->scb_data->hscbs = kmalloc(array_size + 0x1f, GFP_ATOMIC);
                   7734:     }
                   7735:     if (p->scb_data->hscbs == NULL)
                   7736:     {
                   7737:       printk("(scsi%d) Unable to allocate hardware SCB array; "
                   7738:              "failing detection.\n", p->host_no);
                   7739:       p->irq = 0;
                   7740:       return(0);
                   7741:     }
                   7742:     /*
                   7743:      * Save the actual kmalloc buffer pointer off, then align our
                   7744:      * buffer to a 32 byte boundary
                   7745:      */
                   7746:     p->scb_data->hscb_kmalloc_ptr = p->scb_data->hscbs;
                   7747:     temp = (unsigned long)p->scb_data->hscbs;
                   7748:     temp += 0x1f;
                   7749:     temp &= ~0x1f;
                   7750:     p->scb_data->hscbs = (struct aic7xxx_hwscb *)temp;
                   7751:     /* At least the control byte of each SCB needs to be 0. */
                   7752:     memset(p->scb_data->hscbs, 0, array_size);
                   7753: 
                   7754:     /* Tell the sequencer where it can find the hardware SCB array. */
                   7755:     hscb_physaddr = VIRT_TO_BUS(p->scb_data->hscbs);
                   7756:     aic_outb(p, hscb_physaddr & 0xFF, HSCB_ADDR);
                   7757:     aic_outb(p, (hscb_physaddr >> 8) & 0xFF, HSCB_ADDR + 1);
                   7758:     aic_outb(p, (hscb_physaddr >> 16) & 0xFF, HSCB_ADDR + 2);
                   7759:     aic_outb(p, (hscb_physaddr >> 24) & 0xFF, HSCB_ADDR + 3);
                   7760: 
                   7761:     /* Set up the fifo areas at the same time */
                   7762:     hscb_physaddr = VIRT_TO_BUS(&p->untagged_scbs[0]);
                   7763:     aic_outb(p, hscb_physaddr & 0xFF, SCBID_ADDR);
                   7764:     aic_outb(p, (hscb_physaddr >> 8) & 0xFF, SCBID_ADDR + 1);
                   7765:     aic_outb(p, (hscb_physaddr >> 16) & 0xFF, SCBID_ADDR + 2);
                   7766:     aic_outb(p, (hscb_physaddr >> 24) & 0xFF, SCBID_ADDR + 3);
                   7767:   }
                   7768: 
                   7769:   /* The Q-FIFOs we just set up are all empty */
                   7770:   aic_outb(p, 0, QINPOS);
                   7771:   aic_outb(p, 0, KERNEL_QINPOS);
                   7772:   aic_outb(p, 0, QOUTPOS);
                   7773: 
                   7774:   if(p->features & AHC_QUEUE_REGS)
                   7775:   {
                   7776:     aic_outb(p, SCB_QSIZE_256, QOFF_CTLSTA);
                   7777:     aic_outb(p, 0, SDSCB_QOFF);
                   7778:     aic_outb(p, 0, SNSCB_QOFF);
                   7779:     aic_outb(p, 0, HNSCB_QOFF);
                   7780:   }
                   7781: 
                   7782:   /*
                   7783:    * We don't have any waiting selections or disconnected SCBs.
                   7784:    */
                   7785:   aic_outb(p, SCB_LIST_NULL, WAITING_SCBH);
                   7786:   aic_outb(p, SCB_LIST_NULL, DISCONNECTED_SCBH);
                   7787: 
                   7788:   /*
                   7789:    * Message out buffer starts empty
                   7790:    */
                   7791:   aic_outb(p, MSG_NOOP, MSG_OUT);
                   7792:   aic_outb(p, MSG_NOOP, LAST_MSG);
                   7793: 
                   7794:   /*
                   7795:    * Set all the other asundry items that haven't been set yet.
                   7796:    * This includes just dumping init values to a lot of registers simply
                   7797:    * to make sure they've been touched and are ready for use parity wise
                   7798:    * speaking.
                   7799:    */
                   7800:   aic_outb(p, 0, TMODE_CMDADDR);
                   7801:   aic_outb(p, 0, TMODE_CMDADDR + 1);
                   7802:   aic_outb(p, 0, TMODE_CMDADDR + 2);
                   7803:   aic_outb(p, 0, TMODE_CMDADDR + 3);
                   7804:   aic_outb(p, 0, TMODE_CMDADDR_NEXT);
                   7805: 
                   7806:   /*
                   7807:    * Link us into the list of valid hosts
                   7808:    */
                   7809:   p->next = first_aic7xxx;
                   7810:   first_aic7xxx = p;
                   7811: 
                   7812:   /*
                   7813:    * Clear out any possible pending interrupts, again.
                   7814:    */
                   7815:   aic7xxx_clear_intstat(p);
                   7816: 
                   7817:   /*
                   7818:    * Allocate the first set of scbs for this controller.  This is to stream-
                   7819:    * line code elsewhere in the driver.  If we have to check for the existence
                   7820:    * of scbs in certain code sections, it slows things down.  However, as
                   7821:    * soon as we register the IRQ for this card, we could get an interrupt that
                   7822:    * includes possibly the SCSI_RSTI interrupt.  If we catch that interrupt
                   7823:    * then we are likely to segfault if we don't have at least one chunk of
                   7824:    * SCBs allocated or add checks all through the reset code to make sure
                   7825:    * that the SCBs have been allocated which is an invalid running condition
                   7826:    * and therefore I think it's preferable to simply pre-allocate the first
                   7827:    * chunk of SCBs.
                   7828:    */
                   7829:   aic7xxx_allocate_scb(p);
                   7830: 
                   7831:   /*
                   7832:    * Load the sequencer program, then re-enable the board -
                   7833:    * resetting the AIC-7770 disables it, leaving the lights
                   7834:    * on with nobody home.
                   7835:    */
                   7836:   aic7xxx_loadseq(p);
                   7837: 
1.1.1.2 ! root     7838:   /*
        !          7839:    * Make sure the AUTOFLUSHDIS bit is *not* set in the SBLKCTL register
        !          7840:    */
        !          7841:   aic_outb(p, aic_inb(p, SBLKCTL) & ~AUTOFLUSHDIS, SBLKCTL);
        !          7842: 
1.1       root     7843:   if ( (p->chip & AHC_CHIPID_MASK) == AHC_AIC7770 )
                   7844:   {
                   7845:     aic_outb(p, ENABLE, BCTL);  /* Enable the boards BUS drivers. */
                   7846:   }
                   7847: 
                   7848:   if ( !(aic7xxx_no_reset) )
                   7849:   {
                   7850:     if (p->features & AHC_TWIN)
                   7851:     {
                   7852:       if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7853:         printk(KERN_INFO "(scsi%d) Resetting channel B\n", p->host_no);
                   7854:       aic_outb(p, aic_inb(p, SBLKCTL) | SELBUSB, SBLKCTL);
                   7855:       aic7xxx_reset_current_bus(p);
                   7856:       aic_outb(p, aic_inb(p, SBLKCTL) & ~SELBUSB, SBLKCTL);
                   7857:     }
                   7858:     /* Reset SCSI bus A. */
                   7859:     if (aic7xxx_verbose & VERBOSE_PROBE2)
                   7860:     {  /* In case we are a 3940, 3985, or 7895, print the right channel */
                   7861:       char *channel = "";
                   7862:       if (p->flags & AHC_MULTI_CHANNEL)
                   7863:       {
                   7864:         channel = " A";
                   7865:         if (p->flags & (AHC_CHNLB|AHC_CHNLC))
                   7866:           channel = (p->flags & AHC_CHNLB) ? " B" : " C";
                   7867:       }
                   7868:       printk(KERN_INFO "(scsi%d) Resetting channel%s\n", p->host_no, channel);
                   7869:     }
1.1.1.2 ! root     7870: 
1.1       root     7871:     /*
                   7872:      * Some of the new Ultra2 chipsets need a longer delay after a chip
                   7873:      * reset than just the init setup creates, so we have to delay here
                   7874:      * before we go into a reset in order to make the chips happy.
                   7875:      */
                   7876:     if (p->features & AHC_ULTRA2)
                   7877:       mdelay(250);
                   7878:     aic7xxx_reset_current_bus(p);
                   7879: 
                   7880:     /*
                   7881:      * Delay for the reset delay.
                   7882:      */
                   7883:     if (!reset_delay)
                   7884:       aic7xxx_delay(AIC7XXX_RESET_DELAY);
                   7885:   }
                   7886:   else
                   7887:   {
                   7888:     if (!reset_delay)
                   7889:     {
                   7890:       printk(KERN_INFO "(scsi%d) Not resetting SCSI bus.  Note: Don't use "
                   7891:              "the no_reset\n", p->host_no);
                   7892:       printk(KERN_INFO "(scsi%d) option unless you have a verifiable need "
                   7893:              "for it.\n", p->host_no);
                   7894:       printk(KERN_INFO "(scsi%d) The no_reset option is known to break some "
                   7895:              "systems,\n", p->host_no);
                   7896:       printk(KERN_INFO "(scsi%d) and is not supported by the driver author\n",
                   7897:              p->host_no);
                   7898:       aic7xxx_delay(AIC7XXX_RESET_DELAY);
                   7899:     }
                   7900:   }
1.1.1.2 ! root     7901: 
1.1       root     7902:   /*
                   7903:    * Register IRQ with the kernel.  Only allow sharing IRQs with
                   7904:    * PCI devices.
                   7905:    */
                   7906:   if (!(p->chip & AHC_PCI))
                   7907:   {
                   7908:     result = (request_irq(p->irq, do_aic7xxx_isr, 0, "aic7xxx", p));
                   7909:   }
                   7910:   else
                   7911:   {
                   7912:     result = (request_irq(p->irq, do_aic7xxx_isr, SA_SHIRQ,
                   7913:               "aic7xxx", p));
                   7914:     if (result < 0)
                   7915:     {
                   7916:       result = (request_irq(p->irq, do_aic7xxx_isr, SA_INTERRUPT | SA_SHIRQ,
                   7917:               "aic7xxx", p));
                   7918:     }
                   7919:   }
                   7920:   if (result < 0)
                   7921:   {
                   7922:     printk(KERN_WARNING "(scsi%d) Couldn't register IRQ %d, ignoring "
                   7923:            "controller.\n", p->host_no, p->irq);
                   7924:     p->irq = 0;
                   7925:     return (0);
                   7926:   }
                   7927: 
                   7928:   unpause_sequencer(p, /* unpause_always */ TRUE);
                   7929: 
                   7930:   return (found);
                   7931: }
                   7932: 
                   7933: /*+F*************************************************************************
                   7934:  * Function:
                   7935:  *   aic7xxx_chip_reset
                   7936:  *
                   7937:  * Description:
                   7938:  *   Perform a chip reset on the aic7xxx SCSI controller.  The controller
                   7939:  *   is paused upon return.
                   7940:  *-F*************************************************************************/
                   7941: int
                   7942: aic7xxx_chip_reset(struct aic7xxx_host *p)
                   7943: {
                   7944:   unsigned char sblkctl;
                   7945:   int wait;
                   7946: 
                   7947:   /*
                   7948:    * For some 274x boards, we must clear the CHIPRST bit and pause
                   7949:    * the sequencer. For some reason, this makes the driver work.
                   7950:    */
                   7951:   aic_outb(p, PAUSE | CHIPRST, HCNTRL);
                   7952: 
                   7953:   /*
                   7954:    * In the future, we may call this function as a last resort for
                   7955:    * error handling.  Let's be nice and not do any unecessary delays.
                   7956:    */
                   7957:   wait = 1000;  /* 1 second (1000 * 1 msec) */
                   7958:   while (--wait && !(aic_inb(p, HCNTRL) & CHIPRSTACK))
                   7959:   {
                   7960:     mdelay(1);  /* 1 msec */
                   7961:   }
                   7962: 
                   7963:   pause_sequencer(p);
                   7964: 
                   7965:   sblkctl = aic_inb(p, SBLKCTL) & (SELBUSB|SELWIDE);
                   7966:   if (p->chip & AHC_PCI)
                   7967:     sblkctl &= ~SELBUSB;
                   7968:   switch( sblkctl )
                   7969:   {
                   7970:     case 0:  /* normal narrow card */
                   7971:       break;
                   7972:     case 2:  /* Wide card */
                   7973:       p->features |= AHC_WIDE;
                   7974:       break;
                   7975:     case 8:  /* Twin card */
                   7976:       p->features |= AHC_TWIN;
                   7977:       p->flags |= AHC_MULTI_CHANNEL;
                   7978:       break;
                   7979:     default: /* hmmm...we don't know what this is */
                   7980:       printk(KERN_WARNING "aic7xxx: Unsupported adapter type %d, ignoring.\n",
                   7981:         aic_inb(p, SBLKCTL) & 0x0a);
                   7982:       return(-1);
                   7983:   }
                   7984:   return(0);
                   7985: }
                   7986: 
                   7987: /*+F*************************************************************************
                   7988:  * Function:
                   7989:  *   aic7xxx_alloc
                   7990:  *
                   7991:  * Description:
                   7992:  *   Allocate and initialize a host structure.  Returns NULL upon error
                   7993:  *   and a pointer to a aic7xxx_host struct upon success.
                   7994:  *-F*************************************************************************/
                   7995: static struct aic7xxx_host *
                   7996: aic7xxx_alloc(Scsi_Host_Template *sht, struct aic7xxx_host *temp)
                   7997: {
                   7998:   struct aic7xxx_host *p = NULL;
                   7999:   struct Scsi_Host *host;
                   8000:   int i;
                   8001: 
                   8002:   /*
                   8003:    * Allocate a storage area by registering us with the mid-level
                   8004:    * SCSI layer.
                   8005:    */
                   8006:   host = scsi_register(sht, sizeof(struct aic7xxx_host));
                   8007: 
                   8008:   if (host != NULL)
                   8009:   {
                   8010:     p = (struct aic7xxx_host *) host->hostdata;
                   8011:     memset(p, 0, sizeof(struct aic7xxx_host));
                   8012:     *p = *temp;
                   8013:     p->host = host;
                   8014: 
                   8015:     p->scb_data = kmalloc(sizeof(scb_data_type), GFP_ATOMIC);
                   8016:     if (p->scb_data != NULL)
                   8017:     {
                   8018:       memset(p->scb_data, 0, sizeof(scb_data_type));
                   8019:       scbq_init (&p->scb_data->free_scbs);
                   8020:     }
                   8021:     else
                   8022:     {
                   8023:       /*
                   8024:        * For some reason we don't have enough memory.  Free the
                   8025:        * allocated memory for the aic7xxx_host struct, and return NULL.
                   8026:        */
                   8027:       release_region(p->base, MAXREG - MINREG);
                   8028:       scsi_unregister(host);
                   8029:       return(NULL);
                   8030:     }
                   8031:     p->host_no = host->host_no;
                   8032:     p->tagenable = 0;
                   8033:     p->orderedtag = 0;
                   8034:     for (i=0; i<MAX_TARGETS; i++)
                   8035:     {
                   8036:       p->transinfo[i].goal_period = 0;
                   8037:       p->transinfo[i].goal_offset = 0;
                   8038:       p->transinfo[i].goal_width = MSG_EXT_WDTR_BUS_8_BIT;
                   8039:     }
                   8040:     DRIVER_LOCK_INIT
                   8041:   }
                   8042:   return (p);
                   8043: }
                   8044: 
                   8045: /*+F*************************************************************************
                   8046:  * Function:
                   8047:  *   aic7xxx_free
                   8048:  *
                   8049:  * Description:
                   8050:  *   Frees and releases all resources associated with an instance of
                   8051:  *   the driver (struct aic7xxx_host *).
                   8052:  *-F*************************************************************************/
                   8053: static void
                   8054: aic7xxx_free(struct aic7xxx_host *p)
                   8055: {
                   8056:   int i;
                   8057: 
                   8058:   /*
                   8059:    * Free the allocated hardware SCB space.
                   8060:    */
                   8061:   if (p->scb_data != NULL)
                   8062:   {
                   8063:     if (p->scb_data->hscbs != NULL)
                   8064:     {
                   8065:       kfree(p->scb_data->hscb_kmalloc_ptr);
                   8066:       p->scb_data->hscbs = p->scb_data->hscb_kmalloc_ptr = NULL;
                   8067:     }
                   8068:     /*
                   8069:      * Free the driver SCBs.  These were allocated on an as-need
                   8070:      * basis.  We allocated these in groups depending on how many
                   8071:      * we could fit into a given amount of RAM.  The tail SCB for
                   8072:      * these allocations has a pointer to the alloced area.
                   8073:      */
                   8074:     for (i = 0; i < p->scb_data->numscbs; i++)
                   8075:     {
                   8076:       if (p->scb_data->scb_array[i]->kmalloc_ptr != NULL)
                   8077:         kfree(p->scb_data->scb_array[i]->kmalloc_ptr);
                   8078:       p->scb_data->scb_array[i] = NULL;
                   8079:     }
1.1.1.2 ! root     8080: 
1.1       root     8081:     /*
                   8082:      * Free the SCB data area.
                   8083:      */
                   8084:     kfree(p->scb_data);
                   8085:   }
                   8086: 
                   8087:   /*
                   8088:    * Free any alloced Scsi_Cmnd structures that might be around for
                   8089:    * negotiation purposes....
                   8090:    */
                   8091:   for (i = 0; i < MAX_TARGETS; i++)
                   8092:   {
                   8093:     if(p->dev_wdtr_cmnd[i])
                   8094:       kfree(p->dev_wdtr_cmnd[i]);
                   8095:     if(p->dev_sdtr_cmnd[i])
                   8096:       kfree(p->dev_sdtr_cmnd[i]);
                   8097:   }
                   8098: 
                   8099: }
                   8100: 
                   8101: /*+F*************************************************************************
                   8102:  * Function:
                   8103:  *   aic7xxx_load_seeprom
                   8104:  *
                   8105:  * Description:
                   8106:  *   Load the seeprom and configure adapter and target settings.
                   8107:  *   Returns 1 if the load was successful and 0 otherwise.
                   8108:  *-F*************************************************************************/
                   8109: static void
                   8110: aic7xxx_load_seeprom(struct aic7xxx_host *p, unsigned char *sxfrctl1)
                   8111: {
                   8112:   int have_seeprom = 0;
                   8113:   int i, max_targets, mask;
                   8114:   unsigned char scsirate, scsi_conf;
                   8115:   unsigned short scarray[128];
                   8116:   struct seeprom_config *sc = (struct seeprom_config *) scarray;
                   8117: 
                   8118:   if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8119:   {
                   8120:     printk(KERN_INFO "aic7xxx: Loading serial EEPROM...");
                   8121:   }
                   8122:   switch (p->chip)
                   8123:   {
                   8124:     case (AHC_AIC7770|AHC_EISA):  /* None of these adapters have seeproms. */
                   8125:       if (aic_inb(p, SCSICONF) & TERM_ENB)
                   8126:         p->flags |= AHC_TERM_ENB_A;
                   8127:       if ( (p->features & AHC_TWIN) && (aic_inb(p, SCSICONF + 1) & TERM_ENB) )
                   8128:         p->flags |= AHC_TERM_ENB_B;
1.1.1.2 ! root     8129:       aic_outb(p, 0, DISC_DSB);
        !          8130:       aic_outb(p, 0, DISC_DSB + 1);
1.1       root     8131:       break;
                   8132: 
                   8133:     case (AHC_AIC7770|AHC_VL):
                   8134:       have_seeprom = read_284x_seeprom(p, (struct seeprom_config *) scarray);
                   8135:       break;
                   8136: 
                   8137:     default:
                   8138:       have_seeprom = read_seeprom(p, (p->flags & (AHC_CHNLB|AHC_CHNLC)),
                   8139:                                   scarray, p->sc_size, p->sc_type);
                   8140:       if (!have_seeprom)
                   8141:       {
                   8142:         if(p->sc_type == C46)
                   8143:           have_seeprom = read_seeprom(p, (p->flags & (AHC_CHNLB|AHC_CHNLC)),
                   8144:                                       scarray, p->sc_size, C56_66);
                   8145:         else
                   8146:           have_seeprom = read_seeprom(p, (p->flags & (AHC_CHNLB|AHC_CHNLC)),
                   8147:                                       scarray, p->sc_size, C46);
                   8148:       }
1.1.1.2 ! root     8149:       if (!have_seeprom)
        !          8150:       {
        !          8151:         p->sc_size = 128;
        !          8152:         have_seeprom = read_seeprom(p, (p->flags & (AHC_CHNLB|AHC_CHNLC)),
        !          8153:                                     scarray, p->sc_size, p->sc_type);
        !          8154:         if (!have_seeprom)
        !          8155:         {
        !          8156:           if(p->sc_type == C46)
        !          8157:             have_seeprom = read_seeprom(p, (p->flags & (AHC_CHNLB|AHC_CHNLC)),
        !          8158:                                         scarray, p->sc_size, C56_66);
        !          8159:           else
        !          8160:             have_seeprom = read_seeprom(p, (p->flags & (AHC_CHNLB|AHC_CHNLC)),
        !          8161:                                         scarray, p->sc_size, C46);
        !          8162:         }
        !          8163:       }
1.1       root     8164:       break;
                   8165:   }
                   8166: 
                   8167:   if (!have_seeprom)
                   8168:   {
                   8169:     if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8170:     {
                   8171:       printk("\naic7xxx: No SEEPROM available.\n");
                   8172:     }
                   8173:     p->flags |= AHC_NEWEEPROM_FMT;
                   8174:     if (aic_inb(p, SCSISEQ) == 0)
                   8175:     {
                   8176:       p->flags |= AHC_USEDEFAULTS;
                   8177:       p->flags &= ~AHC_BIOS_ENABLED;
                   8178:       p->scsi_id = p->scsi_id_b = 7;
                   8179:       *sxfrctl1 |= STPWEN;
                   8180:       if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8181:       {
                   8182:         printk("aic7xxx: Using default values.\n");
                   8183:       }
                   8184:     }
                   8185:     else if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8186:     {
                   8187:       printk("aic7xxx: Using leftover BIOS values.\n");
                   8188:     }
1.1.1.2 ! root     8189:     if ( ((p->chip & ~AHC_CHIPID_MASK) == AHC_PCI) && (*sxfrctl1 & STPWEN) )
1.1       root     8190:     {
                   8191:       p->flags |= AHC_TERM_ENB_SE_LOW | AHC_TERM_ENB_SE_HIGH;
                   8192:       sc->adapter_control &= ~CFAUTOTERM;
                   8193:       sc->adapter_control |= CFSTERM | CFWSTERM | CFLVDSTERM;
                   8194:     }
1.1.1.2 ! root     8195:     if (aic7xxx_extended)
        !          8196:       p->flags |= (AHC_EXTEND_TRANS_A | AHC_EXTEND_TRANS_B);
        !          8197:     else
        !          8198:       p->flags &= ~(AHC_EXTEND_TRANS_A | AHC_EXTEND_TRANS_B);
1.1       root     8199:   }
                   8200:   else
                   8201:   {
                   8202:     if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8203:     {
                   8204:       printk("done\n");
                   8205:     }
                   8206: 
                   8207:     /*
                   8208:      * Note things in our flags
                   8209:      */
                   8210:     p->flags |= AHC_SEEPROM_FOUND;
                   8211: 
                   8212:     /*
                   8213:      * Update the settings in sxfrctl1 to match the termination settings.
                   8214:      */
                   8215:     *sxfrctl1 = 0;
                   8216: 
                   8217:     /*
                   8218:      * Get our SCSI ID from the SEEPROM setting...
                   8219:      */
                   8220:     p->scsi_id = (sc->brtime_id & CFSCSIID);
                   8221: 
                   8222:     /*
                   8223:      * First process the settings that are different between the VLB
                   8224:      * and PCI adapter seeproms.
                   8225:      */
                   8226:     if ((p->chip & AHC_CHIPID_MASK) == AHC_AIC7770)
                   8227:     {
                   8228:       /* VLB adapter seeproms */
                   8229:       if (sc->bios_control & CF284XEXTEND)
                   8230:         p->flags |= AHC_EXTEND_TRANS_A;
                   8231: 
                   8232:       if (sc->adapter_control & CF284XSTERM)
                   8233:       {
                   8234:         *sxfrctl1 |= STPWEN;
                   8235:         p->flags |= AHC_TERM_ENB_SE_LOW | AHC_TERM_ENB_SE_HIGH;
                   8236:       }
                   8237:     }
                   8238:     else
                   8239:     {
                   8240:       /* PCI adapter seeproms */
                   8241:       if (sc->bios_control & CFEXTEND)
                   8242:         p->flags |= AHC_EXTEND_TRANS_A;
                   8243:       if (sc->bios_control & CFBIOSEN)
                   8244:         p->flags |= AHC_BIOS_ENABLED;
                   8245:       else
                   8246:         p->flags &= ~AHC_BIOS_ENABLED;
                   8247: 
                   8248:       if (sc->adapter_control & CFSTERM)
                   8249:       {
                   8250:         *sxfrctl1 |= STPWEN;
                   8251:         p->flags |= AHC_TERM_ENB_SE_LOW | AHC_TERM_ENB_SE_HIGH;
                   8252:       }
                   8253:     }
                   8254:     p->sc = *sc;
                   8255:   }
                   8256: 
                   8257:   p->discenable = 0;
1.1.1.2 ! root     8258: 
1.1       root     8259:   /*
                   8260:    * Limit to 16 targets just in case.  The 2842 for one is known to
                   8261:    * blow the max_targets setting, future cards might also.
                   8262:    */
                   8263:   max_targets = MIN(sc->max_targets & CFMAXTARG,
                   8264:                    ((p->features & (AHC_TWIN | AHC_WIDE)) ? 16 : 8));
                   8265: 
                   8266:   if (have_seeprom)
                   8267:   {
                   8268:     for (i = 0; i < max_targets; i++)
                   8269:     {
                   8270:       if( ((p->features & AHC_ULTRA) &&
                   8271:           !(sc->adapter_control & CFULTRAEN) &&
                   8272:            (sc->device_flags[i] & CFSYNCHISULTRA)) ||
                   8273:           (sc->device_flags[i] & CFNEWULTRAFORMAT) )
                   8274:       {
                   8275:         p->flags |= AHC_NEWEEPROM_FMT;
                   8276:         break;
                   8277:       }
                   8278:     }
                   8279:   }
                   8280: 
                   8281:   for (i = 0; i < max_targets; i++)
                   8282:   {
                   8283:     mask = (0x01 << i);
                   8284:     if (!have_seeprom)
                   8285:     {
1.1.1.2 ! root     8286:       if (aic_inb(p, SCSISEQ) != 0)
1.1       root     8287:       {
                   8288:         /*
                   8289:          * OK...the BIOS set things up and left behind the settings we need.
                   8290:          * Just make our sc->device_flags[i] entry match what the card has
                   8291:          * set for this device.
                   8292:          */
1.1.1.2 ! root     8293:         p->discenable =
1.1       root     8294:           ~(aic_inb(p, DISC_DSB) | (aic_inb(p, DISC_DSB + 1) << 8) );
                   8295:         p->ultraenb =
                   8296:           (aic_inb(p, ULTRA_ENB) | (aic_inb(p, ULTRA_ENB + 1) << 8) );
                   8297:         sc->device_flags[i] = (p->discenable & mask) ? CFDISC : 0;
                   8298:         if (aic_inb(p, TARG_SCSIRATE + i) & WIDEXFER)
                   8299:           sc->device_flags[i] |= CFWIDEB;
                   8300:         if (p->features & AHC_ULTRA2)
                   8301:         {
                   8302:           if (aic_inb(p, TARG_OFFSET + i))
                   8303:           {
                   8304:             sc->device_flags[i] |= CFSYNCH;
                   8305:             sc->device_flags[i] |= (aic_inb(p, TARG_SCSIRATE + i) & 0x07);
                   8306:             if ( (aic_inb(p, TARG_SCSIRATE + i) & 0x18) == 0x18 )
                   8307:               sc->device_flags[i] |= CFSYNCHISULTRA;
                   8308:           }
                   8309:         }
                   8310:         else
                   8311:         {
                   8312:           if (aic_inb(p, TARG_SCSIRATE + i) & ~WIDEXFER)
                   8313:           {
                   8314:             sc->device_flags[i] |= CFSYNCH;
                   8315:             if (p->features & AHC_ULTRA)
                   8316:               sc->device_flags[i] |= ((p->ultraenb & mask) ?
                   8317:                                       CFSYNCHISULTRA : 0);
                   8318:           }
                   8319:         }
                   8320:       }
                   8321:       else
                   8322:       {
                   8323:         /*
                   8324:          * Assume the BIOS has NOT been run on this card and nothing between
                   8325:          * the card and the devices is configured yet.
                   8326:          */
                   8327:         sc->device_flags[i] = CFDISC;
                   8328:         if (p->features & AHC_WIDE)
                   8329:           sc->device_flags[i] |= CFWIDEB;
                   8330:         if (p->features & AHC_ULTRA2)
                   8331:           sc->device_flags[i] |= 3;
                   8332:         else if (p->features & AHC_ULTRA)
                   8333:           sc->device_flags[i] |= CFSYNCHISULTRA;
                   8334:         sc->device_flags[i] |= CFSYNCH;
                   8335:         aic_outb(p, 0, TARG_SCSIRATE + i);
                   8336:         if (p->features & AHC_ULTRA2)
                   8337:           aic_outb(p, 0, TARG_OFFSET + i);
                   8338:       }
                   8339:     }
                   8340:     if (sc->device_flags[i] & CFDISC)
                   8341:     {
                   8342:       p->discenable |= mask;
                   8343:     }
                   8344:     if (p->flags & AHC_NEWEEPROM_FMT)
                   8345:     {
1.1.1.2 ! root     8346:       if ( (sc->device_flags[i] & CFNEWULTRAFORMAT) &&
        !          8347:           !(p->features & AHC_ULTRA2) )
1.1       root     8348:       {
1.1.1.2 ! root     8349:         /*
        !          8350:          * I know of two different Ultra BIOSes that do this differently.
        !          8351:          * One on the Gigabyte 6BXU mb that wants flags[i] & CFXFER to
        !          8352:          * be == to 0x03 and SYNCISULTRA to be true to mean 40MByte/s
        !          8353:          * while on the IBM Netfinity 5000 they want the same thing
        !          8354:          * to be something else, while flags[i] & CFXFER == 0x03 and
        !          8355:          * SYNCISULTRA false should be 40MByte/s.  So, we set both to
        !          8356:          * 40MByte/s and the lower speeds be damned.  People will have
        !          8357:          * to select around the conversely mapped lower speeds in order
        !          8358:          * to select lower speeds on these boards.
        !          8359:          */
        !          8360:         if ((sc->device_flags[i] & (CFXFER)) == 0x03)
1.1       root     8361:         {
                   8362:           sc->device_flags[i] &= ~CFXFER;
                   8363:           sc->device_flags[i] |= CFSYNCHISULTRA;
                   8364:         }
                   8365:       }
1.1.1.2 ! root     8366:       if (sc->device_flags[i] & CFSYNCHISULTRA)
        !          8367:       {
        !          8368:         p->ultraenb |= mask;
        !          8369:       }
1.1       root     8370:     }
                   8371:     else if (sc->adapter_control & CFULTRAEN)
                   8372:     {
                   8373:       p->ultraenb |= mask;
                   8374:     }
                   8375:     if ( (sc->device_flags[i] & CFSYNCH) == 0)
                   8376:     {
                   8377:       sc->device_flags[i] &= ~CFXFER;
                   8378:       p->ultraenb &= ~mask;
                   8379:       p->transinfo[i].user_offset = 0;
                   8380:       p->transinfo[i].user_period = 0;
                   8381:       p->transinfo[i].cur_offset = 0;
                   8382:       p->transinfo[i].cur_period = 0;
                   8383:       p->needsdtr_copy &= ~mask;
                   8384:     }
                   8385:     else
                   8386:     {
                   8387:       if (p->features & AHC_ULTRA2)
                   8388:       {
                   8389:         p->transinfo[i].user_offset = MAX_OFFSET_ULTRA2;
                   8390:         p->transinfo[i].cur_offset = aic_inb(p, TARG_OFFSET + i);
                   8391:         scsirate = (sc->device_flags[i] & CFXFER) |
                   8392:                    ((p->ultraenb & mask) ? 0x18 : 0x10);
                   8393:         p->transinfo[i].user_period = aic7xxx_find_period(p, scsirate,
                   8394:                                        AHC_SYNCRATE_ULTRA2);
                   8395:         p->transinfo[i].cur_period = aic7xxx_find_period(p,
                   8396:                                        aic_inb(p, TARG_SCSIRATE + i),
                   8397:                                        AHC_SYNCRATE_ULTRA2);
                   8398:       }
                   8399:       else
                   8400:       {
                   8401:         scsirate = (sc->device_flags[i] & CFXFER) << 4;
                   8402:         if (sc->device_flags[i] & CFWIDEB)
                   8403:           p->transinfo[i].user_offset = MAX_OFFSET_16BIT;
                   8404:         else
                   8405:           p->transinfo[i].user_offset = MAX_OFFSET_8BIT;
                   8406:         if (p->features & AHC_ULTRA)
                   8407:         {
                   8408:           short ultraenb;
                   8409:           ultraenb = aic_inb(p, ULTRA_ENB) |
                   8410:             (aic_inb(p, ULTRA_ENB + 1) << 8);
                   8411:           p->transinfo[i].user_period = aic7xxx_find_period(p,
                   8412:                                           scsirate,
                   8413:                                           (p->ultraenb & mask) ?
                   8414:                                           AHC_SYNCRATE_ULTRA :
                   8415:                                           AHC_SYNCRATE_FAST);
                   8416:           p->transinfo[i].cur_period = aic7xxx_find_period(p,
                   8417:                                          aic_inb(p, TARG_SCSIRATE + i),
1.1.1.2 ! root     8418:                                          (ultraenb & mask) ?
1.1       root     8419:                                          AHC_SYNCRATE_ULTRA :
                   8420:                                          AHC_SYNCRATE_FAST);
                   8421:         }
                   8422:         else
                   8423:           p->transinfo[i].user_period = aic7xxx_find_period(p,
                   8424:                                           scsirate, AHC_SYNCRATE_FAST);
                   8425:       }
                   8426:       p->needsdtr_copy |= mask;
                   8427:     }
                   8428:     if ( (sc->device_flags[i] & CFWIDEB) && (p->features & AHC_WIDE) )
                   8429:     {
                   8430:       p->transinfo[i].user_width = MSG_EXT_WDTR_BUS_16_BIT;
                   8431:       p->needwdtr_copy |= mask;
                   8432:     }
                   8433:     else
                   8434:     {
                   8435:       p->transinfo[i].user_width = MSG_EXT_WDTR_BUS_8_BIT;
                   8436:       p->needwdtr_copy &= ~mask;
                   8437:     }
                   8438:     p->transinfo[i].cur_width =
                   8439:       (aic_inb(p, TARG_SCSIRATE + i) & WIDEXFER) ?
                   8440:       MSG_EXT_WDTR_BUS_16_BIT : MSG_EXT_WDTR_BUS_8_BIT;
                   8441:   }
                   8442:   aic_outb(p, ~(p->discenable & 0xFF), DISC_DSB);
                   8443:   aic_outb(p, ~((p->discenable >> 8) & 0xFF), DISC_DSB + 1);
                   8444:   p->needwdtr = p->needwdtr_copy;
                   8445:   p->needsdtr = p->needsdtr_copy;
                   8446:   p->wdtr_pending = p->sdtr_pending = 0;
                   8447: 
                   8448:   /*
                   8449:    * We set the p->ultraenb from the SEEPROM to begin with, but now we make
                   8450:    * it match what is already down in the card.  If we are doing a reset
                   8451:    * on the card then this will get put back to a default state anyway.
                   8452:    * This allows us to not have to pre-emptively negotiate when using the
                   8453:    * no_reset option.
                   8454:    */
                   8455:   if (p->features & AHC_ULTRA)
                   8456:     p->ultraenb = aic_inb(p, ULTRA_ENB) | (aic_inb(p, ULTRA_ENB + 1) << 8);
                   8457: 
1.1.1.2 ! root     8458: 
1.1       root     8459:   scsi_conf = (p->scsi_id & HSCSIID);
                   8460: 
                   8461:   if(have_seeprom)
                   8462:   {
                   8463:     p->adapter_control = sc->adapter_control;
                   8464:     p->bios_control = sc->bios_control;
                   8465: 
                   8466:     switch (p->chip & AHC_CHIPID_MASK)
                   8467:     {
                   8468:       case AHC_AIC7895:
                   8469:       case AHC_AIC7896:
                   8470:         if (p->adapter_control & CFBPRIMARY)
                   8471:           p->flags |= AHC_CHANNEL_B_PRIMARY;
                   8472:       default:
                   8473:         break;
                   8474:     }
                   8475: 
                   8476:     if (sc->adapter_control & CFSPARITY)
                   8477:       scsi_conf |= ENSPCHK;
                   8478:   }
                   8479:   else
                   8480:   {
                   8481:     scsi_conf |= ENSPCHK | RESET_SCSI;
                   8482:   }
                   8483: 
                   8484:   /*
                   8485:    * Only set the SCSICONF and SCSICONF + 1 registers if we are a PCI card.
                   8486:    * The 2842 and 2742 cards already have these registers set and we don't
                   8487:    * want to muck with them since we don't set all the bits they do.
                   8488:    */
                   8489:   if ( (p->chip & ~AHC_CHIPID_MASK) == AHC_PCI )
                   8490:   {
                   8491:     /* Set the host ID */
                   8492:     aic_outb(p, scsi_conf, SCSICONF);
                   8493:     /* In case we are a wide card */
                   8494:     aic_outb(p, p->scsi_id, SCSICONF + 1);
                   8495:   }
                   8496: }
                   8497: 
                   8498: /*+F*************************************************************************
                   8499:  * Function:
                   8500:  *   aic7xxx_detect
                   8501:  *
                   8502:  * Description:
                   8503:  *   Try to detect and register an Adaptec 7770 or 7870 SCSI controller.
                   8504:  *
                   8505:  * XXX - This should really be called aic7xxx_probe().  A sequence of
                   8506:  *       probe(), attach()/detach(), and init() makes more sense than
                   8507:  *       one do-it-all function.  This may be useful when (and if) the
                   8508:  *       mid-level SCSI code is overhauled.
                   8509:  *-F*************************************************************************/
                   8510: int
                   8511: aic7xxx_detect(Scsi_Host_Template *template)
                   8512: {
                   8513:   struct aic7xxx_host *temp_p = NULL;
                   8514:   struct aic7xxx_host *current_p = NULL;
                   8515:   struct aic7xxx_host *list_p = NULL;
                   8516:   int found = 0;
                   8517: #if defined(__i386__) || defined(__alpha__)
                   8518:   ahc_flag_type flags = 0;
                   8519:   int type;
                   8520: #endif
                   8521:   unsigned char sxfrctl1;
                   8522: #if defined(__i386__) || defined(__alpha__)
                   8523:   unsigned char hcntrl, hostconf;
                   8524:   unsigned int slot, base;
                   8525: #endif
                   8526: 
                   8527: #ifdef MODULE
                   8528:   /*
                   8529:    * If we are called as a module, the aic7xxx pointer may not be null
                   8530:    * and it would point to our bootup string, just like on the lilo
                   8531:    * command line.  IF not NULL, then process this config string with
                   8532:    * aic7xxx_setup
                   8533:    */
                   8534:   if(aic7xxx)
                   8535:     aic7xxx_setup(aic7xxx, NULL);
                   8536:   if(dummy_buffer[0] != 'P')
                   8537:     printk(KERN_WARNING "aic7xxx: Please read the file /usr/src/linux/drivers"
                   8538:       "/scsi/README.aic7xxx\n"
                   8539:       "aic7xxx: to see the proper way to specify options to the aic7xxx "
                   8540:       "module\n"
                   8541:       "aic7xxx: Specifically, don't use any commas when passing arguments to\n"
                   8542:       "aic7xxx: insmod or else it might trash certain memory areas.\n");
                   8543: #endif
                   8544: 
                   8545:   template->proc_dir = &proc_scsi_aic7xxx;
                   8546:   template->sg_tablesize = AIC7XXX_MAX_SG;
                   8547: 
                   8548: 
                   8549: #if defined(__i386__) || defined(__alpha__)
                   8550:   /*
                   8551:    * EISA/VL-bus card signature probe.
                   8552:    */
                   8553:   slot = MINSLOT;
                   8554:   while ( (slot <= MAXSLOT) && !(aic7xxx_no_probe) )
                   8555:   {
                   8556:     base = SLOTBASE(slot) + MINREG;
                   8557: 
                   8558:     if (check_region(base, MAXREG - MINREG))
                   8559:     {
                   8560:       /*
                   8561:        * Some other driver has staked a
                   8562:        * claim to this i/o region already.
                   8563:        */
                   8564:       slot++;
                   8565:       continue; /* back to the beginning of the for loop */
                   8566:     }
                   8567:     flags = 0;
                   8568:     type = aic7xxx_probe(slot, base + AHC_HID0, &flags);
                   8569:     if (type == -1)
                   8570:     {
                   8571:       slot++;
                   8572:       continue;
                   8573:     }
                   8574:     temp_p = kmalloc(sizeof(struct aic7xxx_host), GFP_ATOMIC);
                   8575:     if (temp_p == NULL)
                   8576:     {
                   8577:       printk(KERN_WARNING "aic7xxx: Unable to allocate device space.\n");
                   8578:       slot++;
                   8579:       continue; /* back to the beginning of the while loop */
                   8580:     }
                   8581:     /*
                   8582:      * Pause the card preserving the IRQ type.  Allow the operator
                   8583:      * to override the IRQ trigger.
                   8584:      */
                   8585:     if (aic7xxx_irq_trigger == 1)
                   8586:       hcntrl = IRQMS;  /* Level */
                   8587:     else if (aic7xxx_irq_trigger == 0)
                   8588:       hcntrl = 0;  /* Edge */
                   8589:     else
                   8590:       hcntrl = inb(base + HCNTRL) & IRQMS;  /* Default */
                   8591:     memset(temp_p, 0, sizeof(struct aic7xxx_host));
                   8592:     temp_p->unpause = hcntrl | INTEN;
                   8593:     temp_p->pause = hcntrl | PAUSE | INTEN;
                   8594:     temp_p->base = base;
                   8595:     temp_p->mbase = 0;
                   8596:     temp_p->maddr = 0;
                   8597:     temp_p->pci_bus = 0;
                   8598:     temp_p->pci_device_fn = slot;
                   8599:     aic_outb(temp_p, hcntrl | PAUSE, HCNTRL);
                   8600:     while( (aic_inb(temp_p, HCNTRL) & PAUSE) == 0 ) ;
                   8601:     if (aic7xxx_chip_reset(temp_p) == -1)
                   8602:       temp_p->irq = 0;
                   8603:     else
                   8604:       temp_p->irq = aic_inb(temp_p, INTDEF) & 0x0F;
                   8605:     temp_p->flags |= AHC_PAGESCBS;
                   8606: 
                   8607:     switch (temp_p->irq)
                   8608:     {
                   8609:       case 9:
                   8610:       case 10:
                   8611:       case 11:
                   8612:       case 12:
                   8613:       case 14:
                   8614:       case 15:
                   8615:         break;
                   8616: 
                   8617:       default:
                   8618:         printk(KERN_WARNING "aic7xxx: Host adapter uses unsupported IRQ "
                   8619:           "level %d, ignoring.\n", temp_p->irq);
                   8620:         kfree(temp_p);
                   8621:         slot++;
                   8622:         continue; /* back to the beginning of the while loop */
                   8623:     }
                   8624: 
                   8625:     /*
                   8626:      * We are commited now, everything has been checked and this card
                   8627:      * has been found, now we just set it up
                   8628:      */
                   8629: 
                   8630:     /*
                   8631:      * Insert our new struct into the list at the end
                   8632:      */
                   8633:     if (list_p == NULL)
                   8634:     {
                   8635:       list_p = current_p = temp_p;
                   8636:     }
                   8637:     else
                   8638:     {
                   8639:       current_p = list_p;
                   8640:       while (current_p->next != NULL)
                   8641:         current_p = current_p->next;
                   8642:       current_p->next = temp_p;
                   8643:     }
                   8644: 
                   8645:     switch (type)
                   8646:     {
                   8647:       case 0:
                   8648:         temp_p->board_name_index = 2;
                   8649:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8650:           printk("aic7xxx: <%s> at EISA %d\n",
                   8651:                board_names[2], slot);
                   8652:         /* FALLTHROUGH */
                   8653:       case 1:
                   8654:       {
                   8655:         temp_p->chip = AHC_AIC7770 | AHC_EISA;
                   8656:         temp_p->features |= AHC_AIC7770_FE;
                   8657:         temp_p->bios_control = aic_inb(temp_p, HA_274_BIOSCTRL);
                   8658: 
                   8659:         /*
                   8660:          * Get the primary channel information.  Right now we don't
                   8661:          * do anything with this, but someday we will be able to inform
                   8662:          * the mid-level SCSI code which channel is primary.
                   8663:          */
                   8664:         if (temp_p->board_name_index == 0)
                   8665:         {
                   8666:           temp_p->board_name_index = 3;
                   8667:           if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8668:             printk("aic7xxx: <%s> at EISA %d\n",
                   8669:                  board_names[3], slot);
                   8670:         }
                   8671:         if (temp_p->bios_control & CHANNEL_B_PRIMARY)
                   8672:         {
                   8673:           temp_p->flags |= AHC_CHANNEL_B_PRIMARY;
                   8674:         }
                   8675: 
                   8676:         if ((temp_p->bios_control & BIOSMODE) == BIOSDISABLED)
                   8677:         {
                   8678:           temp_p->flags &= ~AHC_BIOS_ENABLED;
                   8679:         }
                   8680:         else
                   8681:         {
                   8682:           temp_p->flags &= ~AHC_USEDEFAULTS;
                   8683:           temp_p->flags |= AHC_BIOS_ENABLED;
                   8684:           if ( (temp_p->bios_control & 0x20) == 0 )
                   8685:           {
                   8686:             temp_p->bios_address = 0xcc000;
                   8687:             temp_p->bios_address += (0x4000 * (temp_p->bios_control & 0x07));
                   8688:           }
                   8689:           else
                   8690:           {
                   8691:             temp_p->bios_address = 0xd0000;
                   8692:             temp_p->bios_address += (0x8000 * (temp_p->bios_control & 0x06));
                   8693:           }
                   8694:         }
                   8695:         temp_p->adapter_control = aic_inb(temp_p, SCSICONF) << 8;
                   8696:         temp_p->adapter_control |= aic_inb(temp_p, SCSICONF + 1);
                   8697:         if (temp_p->features & AHC_WIDE)
                   8698:         {
                   8699:           temp_p->scsi_id = temp_p->adapter_control & HWSCSIID;
                   8700:           temp_p->scsi_id_b = temp_p->scsi_id;
                   8701:         }
                   8702:         else
                   8703:         {
                   8704:           temp_p->scsi_id = (temp_p->adapter_control >> 8) & HSCSIID;
                   8705:           temp_p->scsi_id_b = temp_p->adapter_control & HSCSIID;
                   8706:         }
                   8707:         aic7xxx_load_seeprom(temp_p, &sxfrctl1);
                   8708:         break;
                   8709:       }
                   8710: 
                   8711:       case 2:
                   8712:       case 3:
                   8713:         temp_p->chip = AHC_AIC7770 | AHC_VL;
                   8714:         temp_p->features |= AHC_AIC7770_FE;
                   8715:         if (type == 2)
                   8716:           temp_p->flags |= AHC_BIOS_ENABLED;
                   8717:         else
                   8718:           temp_p->flags &= ~AHC_BIOS_ENABLED;
                   8719:         if (aic_inb(temp_p, SCSICONF) & TERM_ENB)
                   8720:           sxfrctl1 = STPWEN;
                   8721:         aic7xxx_load_seeprom(temp_p, &sxfrctl1);
                   8722:         temp_p->board_name_index = 4;
                   8723:         if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8724:           printk("aic7xxx: <%s> at VLB %d\n",
                   8725:                board_names[2], slot);
                   8726:         switch( aic_inb(temp_p, STATUS_2840) & BIOS_SEL )
                   8727:         {
                   8728:           case 0x00:
                   8729:             temp_p->bios_address = 0xe0000;
                   8730:             break;
                   8731:           case 0x20:
                   8732:             temp_p->bios_address = 0xc8000;
                   8733:             break;
                   8734:           case 0x40:
                   8735:             temp_p->bios_address = 0xd0000;
                   8736:             break;
                   8737:           case 0x60:
                   8738:             temp_p->bios_address = 0xd8000;
                   8739:             break;
                   8740:           default:
                   8741:             break; /* can't get here */
                   8742:         }
                   8743:         break;
                   8744: 
                   8745:       default:  /* Won't get here. */
                   8746:         break;
                   8747:     }
                   8748:     if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8749:     {
                   8750:       printk(KERN_INFO "aic7xxx: BIOS %sabled, IO Port 0x%lx, IRQ %d (%s)\n",
                   8751:         (temp_p->flags & AHC_USEDEFAULTS) ? "dis" : "en", temp_p->base,
                   8752:         temp_p->irq,
                   8753:         (temp_p->pause & IRQMS) ? "level sensitive" : "edge triggered");
                   8754:       printk(KERN_INFO "aic7xxx: Extended translation %sabled.\n",
                   8755:              (temp_p->flags & AHC_EXTEND_TRANS_A) ? "en" : "dis");
                   8756:     }
                   8757: 
                   8758:     /*
                   8759:      * Set the FIFO threshold and the bus off time.
                   8760:      */
                   8761:     hostconf = aic_inb(temp_p, HOSTCONF);
                   8762:     aic_outb(temp_p, hostconf & DFTHRSH, BUSSPD);
                   8763:     aic_outb(temp_p, (hostconf << 2) & BOFF, BUSTIME);
                   8764:     slot++;
                   8765:     found++;
                   8766:   }
                   8767: 
                   8768: #endif /* defined(__i386__) || defined(__alpha__) */
                   8769: 
                   8770: #ifdef CONFIG_PCI
                   8771:   /*
                   8772:    * PCI-bus probe.
                   8773:    */
                   8774: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   8775:   if (pci_present())
                   8776: #else
                   8777:   if (pcibios_present())
                   8778: #endif
                   8779:   {
                   8780:     struct
                   8781:     {
                   8782:       unsigned short      vendor_id;
                   8783:       unsigned short      device_id;
                   8784:       ahc_chip            chip;
                   8785:       ahc_flag_type       flags;
                   8786:       ahc_feature         features;
                   8787:       int                 board_name_index;
                   8788:       unsigned short      seeprom_size;
                   8789:       unsigned short      seeprom_type;
                   8790:     } const aic_pdevs[] = {
                   8791:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7810, AHC_NONE,
                   8792:        AHC_FNONE, AHC_FENONE,                                1,
                   8793:        32, C46 },
                   8794:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7850, AHC_AIC7850,
                   8795:        AHC_PAGESCBS, AHC_AIC7850_FE,                         5,
                   8796:        32, C46 },
                   8797:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7855, AHC_AIC7850,
                   8798:        AHC_PAGESCBS, AHC_AIC7850_FE,                         6,
                   8799:        32, C46 },
1.1.1.2 ! root     8800:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7821, AHC_AIC7860,
        !          8801:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8802:        AHC_AIC7860_FE,                                       7,
        !          8803:        32, C46 },
        !          8804:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_3860, AHC_AIC7860,
        !          8805:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8806:        AHC_AIC7860_FE,                                       7,
        !          8807:        32, C46 },
1.1       root     8808:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7860, AHC_AIC7860,
                   8809:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
                   8810:        AHC_AIC7860_FE,                                       7,
                   8811:        32, C46 },
                   8812:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7861, AHC_AIC7860,
                   8813:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
                   8814:        AHC_AIC7860_FE,                                       8,
                   8815:        32, C46 },
                   8816:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7870, AHC_AIC7870,
                   8817:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7870_FE,      9,
                   8818:        32, C46 },
                   8819:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7871, AHC_AIC7870,
                   8820:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7870_FE,     10,
                   8821:        32, C46 },
                   8822:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7872, AHC_AIC7870,
                   8823:        AHC_PAGESCBS | AHC_BIOS_ENABLED | AHC_MULTI_CHANNEL,
                   8824:        AHC_AIC7870_FE,                                      11,
                   8825:        32, C56_66 },
                   8826:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7873, AHC_AIC7870,
                   8827:        AHC_PAGESCBS | AHC_BIOS_ENABLED | AHC_MULTI_CHANNEL,
                   8828:        AHC_AIC7870_FE,                                      12,
                   8829:        32, C56_66 },
                   8830:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7874, AHC_AIC7870,
                   8831:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7870_FE,     13,
                   8832:        32, C46 },
                   8833:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7880, AHC_AIC7880,
                   8834:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7880_FE,     14,
                   8835:        32, C46 },
                   8836:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7881, AHC_AIC7880,
                   8837:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7880_FE,     15,
                   8838:        32, C46 },
                   8839:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7882, AHC_AIC7880,
                   8840:        AHC_PAGESCBS | AHC_BIOS_ENABLED | AHC_MULTI_CHANNEL,
                   8841:        AHC_AIC7880_FE,                                      16,
                   8842:        32, C56_66 },
                   8843:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7883, AHC_AIC7880,
                   8844:        AHC_PAGESCBS | AHC_BIOS_ENABLED | AHC_MULTI_CHANNEL,
                   8845:        AHC_AIC7880_FE,                                      17,
                   8846:        32, C56_66 },
                   8847:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7884, AHC_AIC7880,
                   8848:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7880_FE,     18,
                   8849:        32, C46 },
1.1.1.2 ! root     8850:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7885, AHC_AIC7880,
        !          8851:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7880_FE,     18,
        !          8852:        32, C46 },
        !          8853:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7886, AHC_AIC7880,
        !          8854:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7880_FE,     18,
        !          8855:        32, C46 },
        !          8856:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7887, AHC_AIC7880,
        !          8857:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7880_FE,     18,
        !          8858:        32, C46 },
        !          8859:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7888, AHC_AIC7880,
        !          8860:        AHC_PAGESCBS | AHC_BIOS_ENABLED, AHC_AIC7880_FE,     18,
        !          8861:        32, C46 },
1.1       root     8862:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_7895, AHC_AIC7895,
                   8863:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED | AHC_MULTI_CHANNEL,
                   8864:        AHC_AIC7895_FE,                                      19,
                   8865:        32, C56_66 },
                   8866:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7890, AHC_AIC7890,
                   8867:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
                   8868:        AHC_AIC7890_FE,                                      20,
                   8869:        32, C46 },
1.1.1.2 ! root     8870:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7890B, AHC_AIC7890,
        !          8871:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8872:        AHC_AIC7890_FE,                                      20,
        !          8873:        32, C46 },
        !          8874:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_2930U2, AHC_AIC7890,
1.1       root     8875:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
                   8876:        AHC_AIC7890_FE,                                      21,
                   8877:        32, C46 },
1.1.1.2 ! root     8878:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_2940U2, AHC_AIC7890,
        !          8879:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8880:        AHC_AIC7890_FE,                                      22,
        !          8881:        32, C46 },
1.1       root     8882:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7896, AHC_AIC7896,
                   8883:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED | AHC_MULTI_CHANNEL,
1.1.1.2 ! root     8884:        AHC_AIC7896_FE,                                      23,
1.1       root     8885:        32, C56_66 },
                   8886:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_3940U2, AHC_AIC7896,
                   8887:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED | AHC_MULTI_CHANNEL,
1.1.1.2 ! root     8888:        AHC_AIC7896_FE,                                      24,
        !          8889:        32, C56_66 },
        !          8890:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_3950U2D, AHC_AIC7896,
        !          8891:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED | AHC_MULTI_CHANNEL,
        !          8892:        AHC_AIC7896_FE,                                      25,
        !          8893:        32, C56_66 },
        !          8894:       {PCI_VENDOR_ID_ADAPTEC, PCI_DEVICE_ID_ADAPTEC_1480A, AHC_AIC7860,
        !          8895:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8896:        AHC_AIC7860_FE,                                      26,
        !          8897:        32, C46 },
        !          8898:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7892A, AHC_AIC7892,
        !          8899:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8900:        AHC_AIC7892_FE,                                      27,
        !          8901:        32, C46 },
        !          8902:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7892B, AHC_AIC7892,
        !          8903:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8904:        AHC_AIC7892_FE,                                      27,
        !          8905:        32, C46 },
        !          8906:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7892D, AHC_AIC7892,
        !          8907:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8908:        AHC_AIC7892_FE,                                      27,
        !          8909:        32, C46 },
        !          8910:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7892P, AHC_AIC7892,
        !          8911:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8912:        AHC_AIC7892_FE,                                      27,
        !          8913:        32, C46 },
        !          8914:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7899A, AHC_AIC7899,
        !          8915:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8916:        AHC_AIC7899_FE,                                      28,
        !          8917:        32, C56_66 },
        !          8918:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7899B, AHC_AIC7899,
        !          8919:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8920:        AHC_AIC7899_FE,                                      28,
        !          8921:        32, C56_66 },
        !          8922:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7899D, AHC_AIC7899,
        !          8923:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8924:        AHC_AIC7899_FE,                                      28,
        !          8925:        32, C56_66 },
        !          8926:       {PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_7899P, AHC_AIC7899,
        !          8927:        AHC_PAGESCBS | AHC_NEWEEPROM_FMT | AHC_BIOS_ENABLED,
        !          8928:        AHC_AIC7899_FE,                                      28,
1.1       root     8929:        32, C56_66 },
                   8930:     };
                   8931: 
                   8932:     unsigned short command;
                   8933:     unsigned int  devconfig, i, oldverbose;
                   8934: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   8935:     struct pci_dev *pdev = NULL;
                   8936: #else
                   8937:     int index;
                   8938:     unsigned int piobase, mmapbase;
                   8939:     unsigned char pci_bus, pci_devfn, pci_irq;
                   8940: #endif
                   8941: 
                   8942:     for (i = 0; i < NUMBER(aic_pdevs); i++)
                   8943:     {
                   8944: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   8945:       pdev = NULL;
                   8946:       while ((pdev = pci_find_device(aic_pdevs[i].vendor_id,
                   8947:                                      aic_pdevs[i].device_id,
                   8948:                                      pdev)))
                   8949: #else
                   8950:       index = 0;
                   8951:       while (!(pcibios_find_device(aic_pdevs[i].vendor_id,
                   8952:                                    aic_pdevs[i].device_id,
                   8953:                                    index++, &pci_bus, &pci_devfn)) )
                   8954: #endif
                   8955:       {
                   8956:         if ( i == 0 ) /* We found one, but it's the 7810 RAID cont. */
                   8957:         {
                   8958:           if (aic7xxx_verbose & (VERBOSE_PROBE|VERBOSE_PROBE2))
                   8959:           {
                   8960:             printk(KERN_INFO "aic7xxx: The 7810 RAID controller is not "
                   8961:               "supported by\n");
                   8962:             printk(KERN_INFO "         this driver, we are ignoring it.\n");
                   8963:           }
                   8964:         }
                   8965:         else if ( (temp_p = kmalloc(sizeof(struct aic7xxx_host),
                   8966:                                     GFP_ATOMIC)) != NULL )
                   8967:         {
                   8968:           memset(temp_p, 0, sizeof(struct aic7xxx_host));
                   8969:           temp_p->chip = aic_pdevs[i].chip | AHC_PCI;
                   8970:           temp_p->flags = aic_pdevs[i].flags;
                   8971:           temp_p->features = aic_pdevs[i].features;
                   8972:           temp_p->board_name_index = aic_pdevs[i].board_name_index;
                   8973:           temp_p->sc_size = aic_pdevs[i].seeprom_size;
                   8974:           temp_p->sc_type = aic_pdevs[i].seeprom_type;
                   8975: 
                   8976:           /*
                   8977:            * Read sundry information from PCI BIOS.
                   8978:            */
                   8979: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   8980:           temp_p->irq = pdev->irq;
                   8981:           temp_p->pdev = pdev;
                   8982:           temp_p->pci_bus = pdev->bus->number;
                   8983:           temp_p->pci_device_fn = pdev->devfn;
                   8984:           temp_p->base = pdev->base_address[0];
                   8985:           temp_p->mbase = pdev->base_address[1];
                   8986:           if (aic7xxx_verbose & VERBOSE_PROBE2)
1.1.1.2 ! root     8987:             printk("aic7xxx: <%s> at PCI %d/%d\n",
1.1       root     8988:               board_names[aic_pdevs[i].board_name_index],
                   8989:               PCI_SLOT(temp_p->pdev->devfn),
                   8990:               PCI_FUNC(temp_p->pdev->devfn));
                   8991:           pci_read_config_word(pdev, PCI_COMMAND, &command);
                   8992:           if (aic7xxx_verbose & VERBOSE_PROBE2)
                   8993:           {
                   8994:             printk("aic7xxx: Initial PCI_COMMAND value was 0x%x\n",
                   8995:               (int)command);
                   8996:           }
                   8997: #ifdef AIC7XXX_STRICT_PCI_SETUP
                   8998:           command |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY |
                   8999:             PCI_COMMAND_INVALIDATE | PCI_COMMAND_MASTER |
                   9000:             PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
                   9001: #else
                   9002:           command |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
                   9003: #endif
                   9004:           if (aic7xxx_pci_parity == 0)
                   9005:             command &= ~(PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
                   9006:           pci_write_config_word(pdev, PCI_COMMAND, command);
                   9007: #ifdef AIC7XXX_STRICT_PCI_SETUP
                   9008:           pci_read_config_dword(pdev, DEVCONFIG, &devconfig);
                   9009:           if (aic7xxx_verbose & VERBOSE_PROBE2)
                   9010:           {
                   9011:             printk("aic7xxx: Initial DEVCONFIG value was 0x%x\n", devconfig);
                   9012:           }
                   9013:           devconfig |= 0x80000000;
                   9014:           if ((aic7xxx_pci_parity == 0) || (aic7xxx_pci_parity == -1))
                   9015:           {
                   9016:             devconfig &= ~(0x00000008);
                   9017:           }
                   9018:           else
                   9019:           {
                   9020:             devconfig |= 0x00000008;
                   9021:           }
                   9022:           pci_write_config_dword(pdev, DEVCONFIG, devconfig);
                   9023: #endif /* AIC7XXX_STRICT_PCI_SETUP */
                   9024: #else  /* LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92) */
                   9025:           temp_p->pci_bus = pci_bus;
                   9026:           temp_p->pci_device_fn = pci_devfn;
                   9027:           if (aic7xxx_verbose & VERBOSE_PROBE2)
1.1.1.2 ! root     9028:             printk("aic7xxx: <%s> at PCI %d/%d\n",
1.1       root     9029:               board_names[aic_pdevs[i].board_name_index],
                   9030:               PCI_SLOT(temp_p->pci_device_fn),
                   9031:               PCI_FUNC(temp_p->pci_device_fn));
                   9032:           pcibios_read_config_byte(pci_bus, pci_devfn, PCI_INTERRUPT_LINE,
                   9033:             &pci_irq);
                   9034:           temp_p->irq = pci_irq;
                   9035:           pcibios_read_config_dword(pci_bus, pci_devfn, PCI_BASE_ADDRESS_0,
                   9036:             &piobase);
                   9037:           temp_p->base = piobase;
                   9038:           pcibios_read_config_dword(pci_bus, pci_devfn, PCI_BASE_ADDRESS_1,
                   9039:             &mmapbase);
                   9040:           temp_p->mbase = mmapbase;
                   9041:           pcibios_read_config_word(pci_bus, pci_devfn, PCI_COMMAND, &command);
                   9042:           if (aic7xxx_verbose & VERBOSE_PROBE2)
                   9043:           {
                   9044:             printk("aic7xxx: Initial PCI_COMMAND value was 0x%x\n",
                   9045:               (int)command);
                   9046:           }
                   9047: #ifdef AIC7XXX_STRICT_PCI_SETUP
                   9048:           command |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY |
                   9049:             PCI_COMMAND_INVALIDATE | PCI_COMMAND_MASTER |
                   9050:             PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
                   9051: #else
                   9052:           command |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
                   9053: #endif
                   9054:           if (aic7xxx_pci_parity == 0)
                   9055:             command &= ~(PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
                   9056:           pcibios_write_config_word(pci_bus, pci_devfn, PCI_COMMAND, command);
                   9057: #ifdef AIC7XXX_STRICT_PCI_SETUP
                   9058:           pcibios_read_config_dword(pci_bus, pci_devfn, DEVCONFIG, &devconfig);
                   9059:           if (aic7xxx_verbose & VERBOSE_PROBE2)
                   9060:           {
                   9061:             printk("aic7xxx: Initial DEVCONFIG value was 0x%x\n", devconfig);
                   9062:           }
                   9063:           devconfig |= 0x80000000;
                   9064:           if ((aic7xxx_pci_parity == 0) || (aic7xxx_pci_parity == -1))
                   9065:           {
                   9066:             devconfig &= ~(0x00000008);
                   9067:           }
                   9068:           else
                   9069:           {
                   9070:             devconfig |= 0x00000008;
                   9071:           }
                   9072:           pcibios_write_config_dword(pci_bus, pci_devfn, DEVCONFIG, devconfig);
                   9073: #endif /* AIC7XXX_STRICT_PCI_SETUP */
                   9074: #endif /* LINUIX_VERSION_CODE > KERNEL_VERSION(2,1,92) */
                   9075: 
                   9076:           /*
                   9077:            * The first bit (LSB) of PCI_BASE_ADDRESS_0 is always set, so
                   9078:            * we mask it off.
                   9079:            */
                   9080:           temp_p->base &= PCI_BASE_ADDRESS_IO_MASK;
                   9081:           temp_p->mbase &= PCI_BASE_ADDRESS_MEM_MASK;
                   9082:           temp_p->unpause = INTEN;
                   9083:           temp_p->pause = temp_p->unpause | PAUSE;
1.1.1.2 ! root     9084:           if ( ((temp_p->base == 0) &&
        !          9085:                 (temp_p->mbase == 0)) ||
        !          9086:                (temp_p->irq == 0) )
        !          9087:           {
        !          9088:             printk("aic7xxx: <%s> at PCI %d/%d\n",
        !          9089:               board_names[aic_pdevs[i].board_name_index],
        !          9090:               PCI_SLOT(temp_p->pci_device_fn),
        !          9091:               PCI_FUNC(temp_p->pci_device_fn));
        !          9092:             printk("aic7xxx: Controller disabled by BIOS, ignoring.\n");
        !          9093:             kfree(temp_p);
        !          9094:             temp_p = NULL;
        !          9095:             continue;
        !          9096:           }
1.1       root     9097: 
                   9098: #ifdef MMAPIO
1.1.1.2 ! root     9099:           {
        !          9100:             unsigned long page_offset, base;
        !          9101: 
        !          9102:             base = temp_p->mbase & PAGE_MASK;
        !          9103:             page_offset = temp_p->mbase - base;
1.1       root     9104: #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
1.1.1.2 ! root     9105:             temp_p->maddr = ioremap_nocache(base, page_offset + 256);
1.1       root     9106: #else
1.1.1.2 ! root     9107:             temp_p->maddr = vremap(base, page_offset + 256);
1.1       root     9108: #endif
1.1.1.2 ! root     9109:             if(temp_p->maddr)
        !          9110:             {
        !          9111:               temp_p->maddr += page_offset;
        !          9112:               /*
        !          9113:                * We need to check the I/O with the MMAPed address.  Some machines
        !          9114:                * simply fail to work with MMAPed I/O and certain controllers.
        !          9115:                */
        !          9116:               if(aic_inb(temp_p, HCNTRL) == 0xff)
        !          9117:               {
        !          9118:                 /*
        !          9119:                  * OK.....we failed our test....go back to programmed I/O
        !          9120:                  */
        !          9121:                 printk(KERN_INFO "aic7xxx: <%s> at PCI %d/%d\n",
        !          9122:                   board_names[aic_pdevs[i].board_name_index],
        !          9123:                   PCI_SLOT(temp_p->pci_device_fn),
        !          9124:                   PCI_FUNC(temp_p->pci_device_fn));
        !          9125:                 printk(KERN_INFO "aic7xxx: MMAPed I/O failed, reverting to "
        !          9126:                                  "Programmed I/O.\n");
        !          9127: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,0)
        !          9128:                 iounmap((void *) (((unsigned long) temp_p->maddr) & PAGE_MASK));
        !          9129: #else
        !          9130:                 vfree((void *) (((unsigned long) temp_p->maddr) & PAGE_MASK));
        !          9131: #endif
        !          9132:                 temp_p->maddr = 0;
        !          9133:               }
        !          9134:             }
1.1       root     9135:           }
                   9136: #endif
                   9137: 
1.1.1.2 ! root     9138:           /*
        !          9139:            * We HAVE to make sure the first pause_sequencer() and all other
        !          9140:            * subsequent I/O that isn't PCI config space I/O takes place
        !          9141:            * after the MMAPed I/O region is configured and tested.  The
        !          9142:            * problem is the PowerPC architecture that doesn't support
        !          9143:            * programmed I/O at all, so we have to have the MMAP I/O set up
        !          9144:            * for this pause to even work on those machines.
        !          9145:            */
1.1       root     9146:           pause_sequencer(temp_p);
                   9147: 
                   9148:           /*
                   9149:            * Clear out any pending PCI error status messages.  Also set
                   9150:            * verbose to 0 so that we don't emit strange PCI error messages
                   9151:            * while cleaning out the current status bits.
                   9152:            */
                   9153:           oldverbose = aic7xxx_verbose;
                   9154:           aic7xxx_verbose = 0;
                   9155:           aic7xxx_pci_intr(temp_p);
                   9156:           aic7xxx_verbose = oldverbose;
                   9157: 
                   9158:           temp_p->bios_address = 0;
                   9159: 
                   9160:           /*
                   9161:            * Remember how the card was setup in case there is no seeprom.
                   9162:            */
                   9163:           if (temp_p->features & AHC_ULTRA2)
                   9164:             temp_p->scsi_id = aic_inb(temp_p, SCSIID_ULTRA2) & OID;
                   9165:           else
                   9166:             temp_p->scsi_id = aic_inb(temp_p, SCSIID) & OID;
                   9167:           /*
                   9168:            * Get current termination setting
                   9169:            */
                   9170:           sxfrctl1 = aic_inb(temp_p, SXFRCTL1) & STPWEN;
                   9171: 
                   9172:           if (aic7xxx_chip_reset(temp_p) == -1)
                   9173:           {
                   9174:             kfree(temp_p);
                   9175:             temp_p = NULL;
                   9176:             continue;
                   9177:           }
                   9178: 
                   9179:           /*
1.1.1.2 ! root     9180:            * We need to set the CHNL? assignments before loading the SEEPROM
        !          9181:            * The 3940 and 3985 cards (original stuff, not any of the later
        !          9182:            * stuff) are 7870 and 7880 class chips.  The Ultra2 stuff falls
        !          9183:            * under 7896 and 7897.  The 7895 is in a class by itself :)
1.1       root     9184:            */
1.1.1.2 ! root     9185:           switch (temp_p->chip & AHC_CHIPID_MASK)
1.1       root     9186:           {
1.1.1.2 ! root     9187:             case AHC_AIC7870: /* 3840 / 3985 */
        !          9188:             case AHC_AIC7880: /* 3840 UW / 3985 UW */
        !          9189:               if(temp_p->flags & AHC_MULTI_CHANNEL)
1.1       root     9190:               {
1.1.1.2 ! root     9191:                 switch(PCI_SLOT(temp_p->pci_device_fn))
        !          9192:                 {
        !          9193:                   case 5:
        !          9194:                     temp_p->flags |= AHC_CHNLB;
        !          9195:                     break;
        !          9196:                   case 8:
        !          9197:                     temp_p->flags |= AHC_CHNLB;
        !          9198:                     break;
        !          9199:                   case 12:
        !          9200:                     temp_p->flags |= AHC_CHNLC;
        !          9201:                     break;
        !          9202:                   default:
        !          9203:                     break;
        !          9204:                 }
1.1       root     9205:               }
                   9206:               break;
                   9207: 
1.1.1.2 ! root     9208:             case AHC_AIC7895: /* 7895 */
        !          9209:             case AHC_AIC7896: /* 7896/7 */
1.1       root     9210: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   9211:               if (PCI_FUNC(temp_p->pdev->devfn) != 0)
                   9212:               {
                   9213:                 temp_p->flags |= AHC_CHNLB;
                   9214:               }
                   9215:               /*
                   9216:                * The 7895 is the only chipset that sets the SCBSIZE32 param
                   9217:                * in the DEVCONFIG register.  The Ultra2 chipsets use
                   9218:                * the DSCOMMAND0 register instead.
                   9219:                */
                   9220:               if ((temp_p->chip & AHC_CHIPID_MASK) == AHC_AIC7895)
                   9221:               {
                   9222:                 pci_read_config_dword(pdev, DEVCONFIG, &devconfig);
                   9223:                 devconfig |= SCBSIZE32;
                   9224:                 pci_write_config_dword(pdev, DEVCONFIG, devconfig);
                   9225:               }
                   9226: #else
                   9227:               if (PCI_FUNC(temp_p->pci_device_fn) != 0)
                   9228:               {
                   9229:                 temp_p->flags |= AHC_CHNLB;
                   9230:               }
                   9231:               /*
                   9232:                * The 7895 is the only chipset that sets the SCBSIZE32 param
                   9233:                * in the DEVCONFIG register.  The Ultra2 chipsets use
                   9234:                * the DSCOMMAND0 register instead.
                   9235:                */
                   9236:               if ((temp_p->chip & AHC_CHIPID_MASK) == AHC_AIC7895)
                   9237:               {
                   9238:                 pcibios_read_config_dword(pci_bus, pci_devfn, DEVCONFIG,
                   9239:                   &devconfig);
                   9240:                 devconfig |= SCBSIZE32;
                   9241:                 pcibios_write_config_dword(pci_bus, pci_devfn, DEVCONFIG,
                   9242:                   devconfig);
                   9243:               }
                   9244: #endif
                   9245:               break;
                   9246:             default:
                   9247:               break;
                   9248:           }
                   9249: 
                   9250:           /*
                   9251:            * Loading of the SEEPROM needs to come after we've set the flags
                   9252:            * to indicate possible CHNLB and CHNLC assigments.  Otherwise,
                   9253:            * on 394x and 398x cards we'll end up reading the wrong settings
                   9254:            * for channels B and C
                   9255:            */
                   9256:           switch (temp_p->chip & AHC_CHIPID_MASK)
                   9257:           {
                   9258:             case AHC_AIC7890:
                   9259:             case AHC_AIC7896:
                   9260:               aic_outb(temp_p, 0, SCAMCTL);
                   9261:               /*
                   9262:                * We used to set DPARCKEN in this register, but after talking
                   9263:                * to a tech from Adaptec, I found out they don't use that
                   9264:                * particular bit in their own register settings, and when you
                   9265:                * combine that with the fact that I determined that we were
                   9266:                * seeing Data-Path Parity Errors on things we shouldn't see
                   9267:                * them on, I think there is a bug in the silicon and the way
                   9268:                * to work around it is to disable this particular check.  Also
                   9269:                * This bug only showed up on certain commands, so it seems to
                   9270:                * be pattern related or some such.  The commands we would
                   9271:                * typically send as a linux TEST_UNIT_READY or INQUIRY command
                   9272:                * could cause it to be triggered, while regular commands that
                   9273:                * actually made reasonable use of the SG array capabilities
                   9274:                * seemed not to cause the problem.
                   9275:                */
                   9276:               /*
                   9277:               aic_outb(temp_p, aic_inb(temp_p, DSCOMMAND0) |
                   9278:                                CACHETHEN | DPARCKEN | MPARCKEN |
                   9279:                                USCBSIZE32 | CIOPARCKEN,
                   9280:                                DSCOMMAND0);
                   9281:                */
                   9282:               aic_outb(temp_p, (aic_inb(temp_p, DSCOMMAND0) |
                   9283:                                 CACHETHEN | MPARCKEN | USCBSIZE32 |
                   9284:                                 CIOPARCKEN) & ~DPARCKEN, DSCOMMAND0);
                   9285:               aic7xxx_load_seeprom(temp_p, &sxfrctl1);
                   9286:               break;
                   9287:             case AHC_AIC7850:
                   9288:             case AHC_AIC7860:
                   9289:               /*
                   9290:                * Set the DSCOMMAND0 register on these cards different from
                   9291:                * on the 789x cards.  Also, read the SEEPROM as well.
                   9292:                */
                   9293:               aic_outb(temp_p, (aic_inb(temp_p, DSCOMMAND0) |
                   9294:                                 CACHETHEN | MPARCKEN) & ~DPARCKEN,
                   9295:                        DSCOMMAND0);
1.1.1.2 ! root     9296:               /* FALLTHROUGH */
        !          9297:             default:
1.1       root     9298:               aic7xxx_load_seeprom(temp_p, &sxfrctl1);
                   9299:               break;
                   9300:             case AHC_AIC7880:
                   9301:               /*
1.1.1.2 ! root     9302:                * Check the rev of the chipset before we change DSCOMMAND0
1.1       root     9303:                */
                   9304: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   9305:               pci_read_config_dword(pdev, DEVCONFIG, &devconfig);
                   9306: #else
                   9307:               pcibios_read_config_dword(pci_bus, pci_devfn, DEVCONFIG,
                   9308:                                         &devconfig);
                   9309: #endif
                   9310:               if ((devconfig & 0xff) >= 1)
                   9311:               {
                   9312:                 aic_outb(temp_p, (aic_inb(temp_p, DSCOMMAND0) |
                   9313:                                   CACHETHEN | MPARCKEN) & ~DPARCKEN,
                   9314:                          DSCOMMAND0);
                   9315:               }
                   9316:               aic7xxx_load_seeprom(temp_p, &sxfrctl1);
                   9317:               break;
                   9318:           }
1.1.1.2 ! root     9319: 
1.1       root     9320: 
                   9321:           /*
                   9322:            * and then we need another switch based on the type in order to
                   9323:            * make sure the channel B primary flag is set properly on 7895
                   9324:            * controllers....Arrrgggghhh!!!  We also have to catch the fact
                   9325:            * that when you disable the BIOS on the 7895 on the Intel DK440LX
                   9326:            * motherboard, and possibly others, it only sets the BIOS disabled
                   9327:            * bit on the A channel...I think I'm starting to lean towards
                   9328:            * going postal....
                   9329:            */
                   9330:           switch(temp_p->chip & AHC_CHIPID_MASK)
                   9331:           {
                   9332:             case AHC_AIC7895:
                   9333:             case AHC_AIC7896:
                   9334:               current_p = list_p;
                   9335:               while(current_p != NULL)
                   9336:               {
                   9337:                 if ( (current_p->pci_bus == temp_p->pci_bus) &&
                   9338:                      (PCI_SLOT(current_p->pci_device_fn) ==
                   9339:                       PCI_SLOT(temp_p->pci_device_fn)) )
                   9340:                 {
                   9341:                   if ( PCI_FUNC(current_p->pci_device_fn) == 0 )
                   9342:                   {
1.1.1.2 ! root     9343:                     temp_p->flags |=
1.1       root     9344:                       (current_p->flags & AHC_CHANNEL_B_PRIMARY);
                   9345:                     temp_p->flags &= ~(AHC_BIOS_ENABLED|AHC_USEDEFAULTS);
                   9346:                     temp_p->flags |=
                   9347:                       (current_p->flags & (AHC_BIOS_ENABLED|AHC_USEDEFAULTS));
                   9348:                   }
                   9349:                   else
                   9350:                   {
                   9351:                     current_p->flags |=
                   9352:                       (temp_p->flags & AHC_CHANNEL_B_PRIMARY);
                   9353:                     current_p->flags &= ~(AHC_BIOS_ENABLED|AHC_USEDEFAULTS);
                   9354:                     current_p->flags |=
                   9355:                       (temp_p->flags & (AHC_BIOS_ENABLED|AHC_USEDEFAULTS));
                   9356:                   }
                   9357:                 }
                   9358:                 current_p = current_p->next;
                   9359:               }
                   9360:               break;
                   9361:             default:
                   9362:               break;
                   9363:           }
                   9364: 
                   9365:           /*
1.1.1.2 ! root     9366:            * We only support external SCB RAM on the 7895/6/7 chipsets.
        !          9367:            * We could support it on the 7890/1 easy enough, but I don't
        !          9368:            * know of any 7890/1 based cards that have it.  I do know
        !          9369:            * of 7895/6/7 cards that have it and they work properly.
1.1       root     9370:            */
                   9371:           switch(temp_p->chip & AHC_CHIPID_MASK)
                   9372:           {
                   9373:             default:
                   9374:               break;
                   9375:             case AHC_AIC7895:
                   9376:             case AHC_AIC7896:
                   9377: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   9378:               pci_read_config_dword(pdev, DEVCONFIG, &devconfig);
                   9379: #else
                   9380:               pcibios_read_config_dword(pci_bus, pci_devfn, DEVCONFIG,
                   9381:                                         &devconfig);
                   9382: #endif
                   9383:               if (temp_p->features & AHC_ULTRA2)
                   9384:               {
                   9385:                 if (aic_inb(temp_p, DSCOMMAND0) & RAMPSM_ULTRA2)
                   9386:                 {
                   9387:                   aic_outb(temp_p,
                   9388:                            aic_inb(temp_p, DSCOMMAND0) & ~SCBRAMSEL_ULTRA2,
                   9389:                            DSCOMMAND0);
                   9390:                   temp_p->flags |= AHC_EXTERNAL_SRAM;
                   9391:                   devconfig |= EXTSCBPEN;
                   9392:                 }
                   9393:               }
                   9394:               else if (devconfig & RAMPSM)
                   9395:               {
                   9396:                 devconfig &= ~SCBRAMSEL;
                   9397:                 devconfig |= EXTSCBPEN;
                   9398:                 temp_p->flags |= AHC_EXTERNAL_SRAM;
                   9399:               }
                   9400: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   9401:               pci_write_config_dword(pdev, DEVCONFIG, devconfig);
                   9402: #else
                   9403:               pcibios_write_config_dword(pci_bus, pci_devfn, DEVCONFIG,
                   9404:                                          devconfig);
                   9405: #endif
                   9406:               if ( (temp_p->flags & AHC_EXTERNAL_SRAM) &&
                   9407:                    (temp_p->flags & AHC_CHNLB) )
                   9408:                 aic_outb(temp_p, 1, CCSCBBADDR);
                   9409:               break;
                   9410:           }
                   9411: 
                   9412:           /*
                   9413:            * Take the LED out of diagnostic mode
                   9414:            */
1.1.1.2 ! root     9415:           aic_outb(temp_p,
1.1       root     9416:             (aic_inb(temp_p, SBLKCTL) & ~(DIAGLEDEN | DIAGLEDON)),
                   9417:             SBLKCTL);
                   9418: 
                   9419:           /*
                   9420:            * We don't know where this is set in the SEEPROM or by the
                   9421:            * BIOS, so we default to 100%.  On Ultra2 controllers, use 75%
                   9422:            * instead.
                   9423:            */
                   9424:           if (temp_p->features & AHC_ULTRA2)
                   9425:           {
                   9426:             aic_outb(temp_p, RD_DFTHRSH_75 | WR_DFTHRSH_75, DFF_THRSH);
                   9427:           }
                   9428:           else
                   9429:           {
                   9430:             aic_outb(temp_p, DFTHRSH_100, DSPCISTATUS);
                   9431:           }
                   9432: 
                   9433:           if ( list_p == NULL )
                   9434:           {
                   9435:             list_p = current_p = temp_p;
                   9436:           }
                   9437:           else
                   9438:           {
                   9439:             current_p = list_p;
                   9440:             while(current_p->next != NULL)
                   9441:               current_p = current_p->next;
                   9442:             current_p->next = temp_p;
                   9443:           }
                   9444:           temp_p->next = NULL;
                   9445:           found++;
                   9446:         }  /* Found an Adaptec PCI device. */
                   9447:         else /* Well, we found one, but we couldn't get any memory */
                   9448:         {
1.1.1.2 ! root     9449:           printk("aic7xxx: Found <%s>\n",
1.1       root     9450:             board_names[aic_pdevs[i].board_name_index]);
                   9451:           printk(KERN_INFO "aic7xxx: Unable to allocate device memory, "
                   9452:             "skipping.\n");
                   9453:         }
                   9454:       } /* while(pdev=....) */
                   9455:     } /* for PCI_DEVICES */
                   9456:   } /* PCI BIOS present */
                   9457: #endif CONFIG_PCI
                   9458:   /*
                   9459:    * Now, we re-order the probed devices by BIOS address and BUS class.
                   9460:    * In general, we follow this algorithm to make the adapters show up
                   9461:    * in the same order under linux that the computer finds them.
                   9462:    *  1: All VLB/EISA cards with BIOS_ENABLED first, according to BIOS
                   9463:    *     address, going from lowest to highest.
                   9464:    *  2: All PCI controllers with BIOS_ENABLED next, according to BIOS
                   9465:    *     address, going from lowest to highest.
                   9466:    *  3: Remaining VLB/EISA controllers going in slot order.
                   9467:    *  4: Remaining PCI controllers, going in PCI device order (reversable)
                   9468:    */
                   9469: 
                   9470:   {
                   9471:     struct aic7xxx_host *sort_list[4] = { NULL, NULL, NULL, NULL };
                   9472:     struct aic7xxx_host *vlb, *pci;
                   9473:     struct aic7xxx_host *prev_p;
                   9474:     struct aic7xxx_host *p;
                   9475:     unsigned char left;
                   9476: 
                   9477:     prev_p = vlb = pci = NULL;
                   9478: 
                   9479:     temp_p = list_p;
                   9480:     while (temp_p != NULL)
                   9481:     {
                   9482:       switch(temp_p->chip & ~AHC_CHIPID_MASK)
                   9483:       {
                   9484:         case AHC_EISA:
                   9485:         case AHC_VL:
                   9486:         {
                   9487:           p = temp_p;
                   9488:           if (p->flags & AHC_BIOS_ENABLED)
                   9489:             vlb = sort_list[0];
                   9490:           else
                   9491:             vlb = sort_list[2];
                   9492: 
                   9493:           if (vlb == NULL)
                   9494:           {
                   9495:             vlb = temp_p;
                   9496:             temp_p = temp_p->next;
                   9497:             vlb->next = NULL;
                   9498:           }
                   9499:           else
                   9500:           {
                   9501:             current_p = vlb;
                   9502:             prev_p = NULL;
                   9503:             while ( (current_p != NULL) &&
                   9504:                     (current_p->bios_address < temp_p->bios_address))
                   9505:             {
                   9506:               prev_p = current_p;
                   9507:               current_p = current_p->next;
                   9508:             }
                   9509:             if (prev_p != NULL)
                   9510:             {
                   9511:               prev_p->next = temp_p;
                   9512:               temp_p = temp_p->next;
                   9513:               prev_p->next->next = current_p;
                   9514:             }
                   9515:             else
                   9516:             {
                   9517:               vlb = temp_p;
                   9518:               temp_p = temp_p->next;
                   9519:               vlb->next = current_p;
                   9520:             }
                   9521:           }
1.1.1.2 ! root     9522: 
1.1       root     9523:           if (p->flags & AHC_BIOS_ENABLED)
                   9524:             sort_list[0] = vlb;
                   9525:           else
                   9526:             sort_list[2] = vlb;
1.1.1.2 ! root     9527: 
1.1       root     9528:           break;
                   9529:         }
                   9530:         default:  /* All PCI controllers fall through to default */
                   9531:         {
                   9532: 
                   9533:           p = temp_p;
1.1.1.2 ! root     9534:           if (p->flags & AHC_BIOS_ENABLED)
1.1       root     9535:             pci = sort_list[1];
                   9536:           else
                   9537:             pci = sort_list[3];
                   9538: 
                   9539:           if (pci == NULL)
                   9540:           {
                   9541:             pci = temp_p;
                   9542:             temp_p = temp_p->next;
                   9543:             pci->next = NULL;
                   9544:           }
                   9545:           else
                   9546:           {
                   9547:             current_p = pci;
                   9548:             prev_p = NULL;
                   9549:             if (!aic7xxx_reverse_scan)
                   9550:             {
                   9551:               while ( (current_p != NULL) &&
                   9552:                       ( (PCI_SLOT(current_p->pci_device_fn) |
1.1.1.2 ! root     9553:                         (current_p->pci_bus << 8)) <
1.1       root     9554:                         (PCI_SLOT(temp_p->pci_device_fn) |
                   9555:                         (temp_p->pci_bus << 8)) ) )
                   9556:               {
                   9557:                 prev_p = current_p;
                   9558:                 current_p = current_p->next;
                   9559:               }
                   9560:             }
                   9561:             else
                   9562:             {
                   9563:               while ( (current_p != NULL) &&
                   9564:                       ( (PCI_SLOT(current_p->pci_device_fn) |
1.1.1.2 ! root     9565:                         (current_p->pci_bus << 8)) >
1.1       root     9566:                         (PCI_SLOT(temp_p->pci_device_fn) |
                   9567:                         (temp_p->pci_bus << 8)) ) )
                   9568:               {
                   9569:                 prev_p = current_p;
                   9570:                 current_p = current_p->next;
                   9571:               }
                   9572:             }
                   9573:             /*
                   9574:              * Are we dealing with a 7985 where we need to sort the
                   9575:              * channels as well, if so, the bios_address values should
                   9576:              * be the same
                   9577:              */
                   9578:             if ( (current_p) && (temp_p->flags & AHC_MULTI_CHANNEL) &&
                   9579:                  (temp_p->pci_bus == current_p->pci_bus) &&
                   9580:                  (PCI_SLOT(temp_p->pci_device_fn) ==
                   9581:                   PCI_SLOT(current_p->pci_device_fn)) )
                   9582:             {
                   9583:               if (temp_p->flags & AHC_CHNLB)
                   9584:               {
                   9585:                 if ( !(temp_p->flags & AHC_CHANNEL_B_PRIMARY) )
                   9586:                 {
                   9587:                   prev_p = current_p;
                   9588:                   current_p = current_p->next;
                   9589:                 }
                   9590:               }
                   9591:               else
                   9592:               {
                   9593:                 if (temp_p->flags & AHC_CHANNEL_B_PRIMARY)
                   9594:                 {
                   9595:                   prev_p = current_p;
                   9596:                   current_p = current_p->next;
                   9597:                 }
                   9598:               }
                   9599:             }
                   9600:             if (prev_p != NULL)
                   9601:             {
                   9602:               prev_p->next = temp_p;
                   9603:               temp_p = temp_p->next;
                   9604:               prev_p->next->next = current_p;
                   9605:             }
                   9606:             else
                   9607:             {
                   9608:               pci = temp_p;
                   9609:               temp_p = temp_p->next;
                   9610:               pci->next = current_p;
                   9611:             }
                   9612:           }
                   9613: 
                   9614:           if (p->flags & AHC_BIOS_ENABLED)
                   9615:             sort_list[1] = pci;
                   9616:           else
                   9617:             sort_list[3] = pci;
                   9618: 
                   9619:           break;
                   9620:         }
                   9621:       }  /* End of switch(temp_p->type) */
                   9622:     } /* End of while (temp_p != NULL) */
                   9623:     /*
                   9624:      * At this point, the cards have been broken into 4 sorted lists, now
                   9625:      * we run through the lists in order and register each controller
                   9626:      */
                   9627:     {
                   9628:       int i;
1.1.1.2 ! root     9629: 
1.1       root     9630:       left = found;
                   9631:       for (i=0; i<NUMBER(sort_list); i++)
                   9632:       {
                   9633:         temp_p = sort_list[i];
                   9634:         while(temp_p != NULL)
                   9635:         {
                   9636:           template->name = board_names[temp_p->board_name_index];
                   9637:           p = aic7xxx_alloc(template, temp_p);
                   9638:           if (p != NULL)
                   9639:           {
                   9640:             p->instance = found - left;
                   9641:             if (aic7xxx_register(template, p, (--left)) == 0)
                   9642:             {
                   9643:               found--;
                   9644:               aic7xxx_release(p->host);
                   9645:               scsi_unregister(p->host);
                   9646:             }
                   9647:             else if (aic7xxx_dump_card)
                   9648:             {
                   9649:               pause_sequencer(p);
                   9650:               aic7xxx_print_card(p);
                   9651:               aic7xxx_print_scratch_ram(p);
                   9652:               unpause_sequencer(p, TRUE);
                   9653:             }
                   9654:           }
                   9655:           current_p = temp_p;
                   9656:           temp_p = (struct aic7xxx_host *)temp_p->next;
                   9657:           kfree(current_p);
                   9658:         }
                   9659:       }
                   9660:     }
                   9661:   }
                   9662:   return (found);
                   9663: }
                   9664: 
                   9665: #ifdef AIC7XXX_FAKE_NEGOTIATION_CMDS
                   9666: 
                   9667: /*+F*************************************************************************
                   9668:  * Function:
                   9669:  *   aic7xxx_negotiation_complete
                   9670:  *
                   9671:  * Description:
                   9672:  *   Handle completion events for our Negotiation commands.  Clear out the
                   9673:  *   struct and get it ready for its next use.
                   9674:  *-F*************************************************************************/
                   9675: static void
                   9676: aic7xxx_negotiation_complete(Scsi_Cmnd *cmd)
                   9677: {
                   9678:   return;
                   9679: }
                   9680: 
                   9681: /*+F*************************************************************************
                   9682:  * Function:
                   9683:  *   aic7xxx_build_negotiation_command
                   9684:  *
                   9685:  * Description:
                   9686:  *   Build a Scsi_Cmnd structure to perform negotiation with or else send
                   9687:  *   a pre-built command specifically for this purpose.
                   9688:  *-F*************************************************************************/
                   9689: static void
                   9690: aic7xxx_build_negotiation_cmnd(struct aic7xxx_host *p, Scsi_Cmnd *old_cmd,
                   9691:   int tindex)
                   9692: {
                   9693: 
                   9694:   if ( (p->needwdtr & (1<<tindex)) && !(p->wdtr_pending & (1<<tindex)) )
                   9695:   {
                   9696:     if(p->dev_wdtr_cmnd[tindex] == NULL)
                   9697:     {
                   9698:       Scsi_Cmnd *cmd;
                   9699: 
                   9700:       if (!(p->dev_wdtr_cmnd[tindex] = kmalloc(sizeof(Scsi_Cmnd), GFP_ATOMIC)) )
                   9701:       {
                   9702:         return;
                   9703:       }
                   9704:       cmd = p->dev_wdtr_cmnd[tindex];
                   9705:       memset(cmd, 0, sizeof(Scsi_Cmnd));
                   9706:       memcpy(cmd, old_cmd, sizeof(Scsi_Cmnd));
                   9707:       memset(&cmd->cmnd[0], 0, sizeof(cmd->cmnd));
                   9708:       memset(&cmd->data_cmnd[0], 0, sizeof(cmd->data_cmnd));
                   9709:       cmd->lun = 0;
                   9710:       cmd->request_bufflen = 0;
                   9711:       cmd->request_buffer = NULL;
                   9712:       cmd->use_sg = cmd->old_use_sg = cmd->sglist_len = 0;
                   9713:       cmd->bufflen = 0;
                   9714:       cmd->buffer = NULL;
                   9715:       cmd->underflow = 0;
                   9716:       cmd->cmd_len = 6;
                   9717:     }
                   9718:     /*
                   9719:      * Before sending this thing out, we also amke the cmd->next pointer
                   9720:      * point to the real command so we can stuff any possible SENSE data
                   9721:      * intp the real command instead of this fake command.  This has to be
                   9722:      * done each time the command is built, not just the first time, hence
                   9723:      * it's outside of the above if()...
                   9724:      */
                   9725:     p->dev_wdtr_cmnd[tindex]->next = old_cmd;
1.1.1.2 ! root     9726:     aic7xxx_queue(p->dev_wdtr_cmnd[tindex],
1.1       root     9727:                   aic7xxx_negotiation_complete);
                   9728:   }
                   9729:   else if ( (p->needsdtr & (1<<tindex)) && !(p->sdtr_pending & (1<<tindex)) &&
                   9730:             !(p->wdtr_pending & (1<<tindex)) )
                   9731:   {
                   9732:     if(p->dev_sdtr_cmnd[tindex] == NULL)
                   9733:     {
                   9734:       Scsi_Cmnd *cmd;
                   9735: 
                   9736:       if (!(p->dev_sdtr_cmnd[tindex] = kmalloc(sizeof(Scsi_Cmnd), GFP_ATOMIC)) )
                   9737:       {
                   9738:         return;
                   9739:       }
                   9740:       cmd = p->dev_sdtr_cmnd[tindex];
                   9741:       memset(cmd, 0, sizeof(Scsi_Cmnd));
                   9742:       memcpy(cmd, old_cmd, sizeof(Scsi_Cmnd));
                   9743:       memset(&cmd->cmnd[0], 0, sizeof(cmd->cmnd));
                   9744:       memset(&cmd->data_cmnd[0], 0, sizeof(cmd->data_cmnd));
                   9745:       cmd->lun = 0;
                   9746:       cmd->request_bufflen = 0;
                   9747:       cmd->request_buffer = NULL;
                   9748:       cmd->use_sg = cmd->old_use_sg = cmd->sglist_len = 0;
                   9749:       cmd->bufflen = 0;
                   9750:       cmd->buffer = NULL;
                   9751:       cmd->underflow = 0;
                   9752:       cmd->cmd_len = 6;
                   9753:     }
                   9754:     /*
                   9755:      * Before sending this thing out, we also amke the cmd->next pointer
                   9756:      * point to the real command so we can stuff any possible SENSE data
                   9757:      * intp the real command instead of this fake command.  This has to be
                   9758:      * done each time the command is built, not just the first time, hence
                   9759:      * it's outside of the above if()...
                   9760:      */
                   9761:     p->dev_sdtr_cmnd[tindex]->next = old_cmd;
1.1.1.2 ! root     9762:     aic7xxx_queue(p->dev_sdtr_cmnd[tindex],
1.1       root     9763:                   aic7xxx_negotiation_complete);
                   9764:   }
                   9765: }
                   9766: 
                   9767: #endif
                   9768: 
                   9769: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   9770: /*+F*************************************************************************
                   9771:  * Function:
                   9772:  *   aic7xxx_print_scb
                   9773:  *
                   9774:  * Description:
                   9775:  *   Dump the byte codes for an about to be sent SCB.
                   9776:  *-F*************************************************************************/
                   9777: static void
                   9778: aic7xxx_print_scb(struct aic7xxx_host *p, struct aic7xxx_scb *scb)
                   9779: {
                   9780:   int i;
1.1.1.2 ! root     9781:   unsigned char *x;
1.1       root     9782: 
                   9783:   x = (unsigned char *)&scb->hscb->control;
                   9784: 
                   9785:   for(i=0; i<32; i++)
                   9786:   {
                   9787:     printk("%02x ", x[i]);
                   9788:   }
                   9789:   printk("\n");
                   9790: }
                   9791: #endif
                   9792: 
                   9793: /*+F*************************************************************************
                   9794:  * Function:
                   9795:  *   aic7xxx_buildscb
                   9796:  *
                   9797:  * Description:
                   9798:  *   Build a SCB.
                   9799:  *-F*************************************************************************/
                   9800: static void
                   9801: aic7xxx_buildscb(struct aic7xxx_host *p, Scsi_Cmnd *cmd,
                   9802:     struct aic7xxx_scb *scb)
                   9803: {
                   9804:   unsigned short mask;
                   9805:   struct aic7xxx_hwscb *hscb;
                   9806: 
                   9807:   mask = (0x01 << TARGET_INDEX(cmd));
                   9808:   hscb = scb->hscb;
                   9809: 
                   9810:   /*
                   9811:    * Setup the control byte if we need negotiation and have not
                   9812:    * already requested it.
                   9813:    */
                   9814:   hscb->control = 0;
                   9815:   scb->tag_action = 0;
                   9816:   if (p->discenable & mask)
                   9817:   {
                   9818:     hscb->control |= DISCENB;
                   9819:     if (p->tagenable & mask)
                   9820:     {
                   9821:       cmd->tag = hscb->tag;
                   9822:       p->dev_commands_sent[TARGET_INDEX(cmd)]++;
                   9823:       if (p->dev_commands_sent[TARGET_INDEX(cmd)] < 200)
                   9824:       {
                   9825:         hscb->control |= MSG_SIMPLE_Q_TAG;
                   9826:         scb->tag_action = MSG_SIMPLE_Q_TAG;
                   9827:       }
                   9828:       else
                   9829:       {
                   9830:         if (p->orderedtag & mask)
                   9831:         {
                   9832:           hscb->control |= MSG_ORDERED_Q_TAG;
                   9833:           scb->tag_action = MSG_ORDERED_Q_TAG;
                   9834:         }
                   9835:         else
                   9836:         {
                   9837:           hscb->control |= MSG_SIMPLE_Q_TAG;
                   9838:           scb->tag_action = MSG_SIMPLE_Q_TAG;
                   9839:         }
                   9840:         p->dev_commands_sent[TARGET_INDEX(cmd)] = 0;
                   9841:       }
                   9842:     }
                   9843:   }
                   9844:   if (p->dev_flags[TARGET_INDEX(cmd)] & DEVICE_SCANNED)
                   9845:   {
                   9846: #ifdef AIC7XXX_FAKE_NEGOTIATION_CMDS
                   9847:     if ( (p->needwdtr & mask) && !(p->wdtr_pending & mask) )
                   9848:     {
                   9849:       if (cmd == p->dev_wdtr_cmnd[TARGET_INDEX(cmd)])
                   9850:       {
                   9851:         p->wdtr_pending |= mask;
                   9852:         scb->flags |= SCB_MSGOUT_WDTR;
                   9853:         hscb->control &= DISCENB;
                   9854:         hscb->control |= MK_MESSAGE;
                   9855:         scb->tag_action = 0;
                   9856:       }
                   9857:       else
                   9858:       {
                   9859:         aic7xxx_build_negotiation_cmnd(p, cmd, TARGET_INDEX(cmd));
                   9860:       }
                   9861:     }
                   9862:     else if ( (p->needsdtr & mask) && !(p->sdtr_pending & mask) &&
                   9863:               !(p->wdtr_pending & mask) )
                   9864:     {
                   9865:       if (cmd == p->dev_sdtr_cmnd[TARGET_INDEX(cmd)])
                   9866:       {
                   9867:         p->sdtr_pending |= mask;
                   9868:         scb->flags |= SCB_MSGOUT_SDTR;
                   9869:         hscb->control &= DISCENB;
                   9870:         hscb->control |= MK_MESSAGE;
                   9871:         scb->tag_action = 0;
                   9872:       }
                   9873:       else if (cmd != p->dev_wdtr_cmnd[TARGET_INDEX(cmd)])
                   9874:       {
                   9875:         aic7xxx_build_negotiation_cmnd(p, cmd, TARGET_INDEX(cmd));
                   9876:       }
                   9877:     }
                   9878: #else
                   9879:     if ( (p->needwdtr & mask) && !(p->wdtr_pending & mask) &&
                   9880:          !(p->sdtr_pending & mask) && (cmd->lun == 0) )
                   9881:     {
                   9882:       p->wdtr_pending |= mask;
                   9883:       scb->flags |= SCB_MSGOUT_WDTR;
                   9884:       hscb->control &= DISCENB;
                   9885:       hscb->control |= MK_MESSAGE;
                   9886:       scb->tag_action = 0;
                   9887: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   9888:       if (aic7xxx_verbose > 0xffff)
                   9889:         printk(INFO_LEAD "Building WDTR command.\n", p->host_no,
                   9890:                CTL_OF_CMD(cmd));
                   9891: #endif
                   9892:     }
                   9893:     else if ( (p->needsdtr & mask) && !(p->wdtr_pending & mask) &&
                   9894:               !(p->sdtr_pending & mask) && (cmd->lun == 0) )
                   9895:     {
                   9896:       p->sdtr_pending |= mask;
                   9897:       scb->flags |= SCB_MSGOUT_SDTR;
                   9898:       hscb->control &= DISCENB;
                   9899:       hscb->control |= MK_MESSAGE;
                   9900:       scb->tag_action = 0;
                   9901: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   9902:       if (aic7xxx_verbose > 0xffff)
                   9903:         printk(INFO_LEAD "Building SDTR command.\n", p->host_no,
                   9904:                CTL_OF_CMD(cmd));
                   9905: #endif
                   9906:     }
                   9907: #endif
                   9908:   }
                   9909:   hscb->target_channel_lun = ((cmd->target << 4) & 0xF0) |
                   9910:         ((cmd->channel & 0x01) << 3) | (cmd->lun & 0x07);
                   9911: 
                   9912:   /*
                   9913:    * The interpretation of request_buffer and request_bufflen
                   9914:    * changes depending on whether or not use_sg is zero; a
                   9915:    * non-zero use_sg indicates the number of elements in the
                   9916:    * scatter-gather array.
                   9917:    */
                   9918: 
                   9919:   /*
                   9920:    * XXX - this relies on the host data being stored in a
                   9921:    *       little-endian format.
                   9922:    */
                   9923:   hscb->SCSI_cmd_length = cmd->cmd_len;
                   9924:   hscb->SCSI_cmd_pointer = cpu_to_le32(VIRT_TO_BUS(cmd->cmnd));
                   9925: 
                   9926:   if (cmd->use_sg)
                   9927:   {
                   9928:     struct scatterlist *sg;  /* Must be mid-level SCSI code scatterlist */
                   9929: 
                   9930:     /*
                   9931:      * We must build an SG list in adapter format, as the kernel's SG list
                   9932:      * cannot be used directly because of data field size (__alpha__)
                   9933:      * differences and the kernel SG list uses virtual addresses where
                   9934:      * we need physical addresses.
                   9935:      */
                   9936:     int i;
                   9937: 
                   9938:     sg = (struct scatterlist *)cmd->request_buffer;
                   9939:     scb->sg_length = 0;
                   9940:     /*
                   9941:      * Copy the segments into the SG array.  NOTE!!! - We used to
                   9942:      * have the first entry both in the data_pointer area and the first
                   9943:      * SG element.  That has changed somewhat.  We still have the first
                   9944:      * entry in both places, but now we download the address of
                   9945:      * scb->sg_list[1] instead of 0 to the sg pointer in the hscb.
                   9946:      */
                   9947:     for (i = 0; i < cmd->use_sg; i++)
                   9948:     {
                   9949:       scb->sg_list[i].address = cpu_to_le32(VIRT_TO_BUS(sg[i].address));
                   9950:       scb->sg_list[i].length = cpu_to_le32(sg[i].length);
                   9951:       scb->sg_length += sg[i].length;
                   9952:     }
                   9953:     /* Copy the first SG into the data pointer area. */
                   9954:     hscb->data_pointer = scb->sg_list[0].address;
                   9955:     hscb->data_count = scb->sg_list[0].length;
                   9956:     scb->sg_count = cmd->use_sg;
                   9957:     hscb->SG_segment_count = cmd->use_sg;
                   9958:     hscb->SG_list_pointer = cpu_to_le32(VIRT_TO_BUS(&scb->sg_list[1]));
                   9959: 
                   9960:   }
                   9961:   else
                   9962:   {
                   9963:     if (cmd->request_bufflen)
                   9964:     {
                   9965:       scb->sg_count = 1;
                   9966:       scb->sg_list[0].address = cpu_to_le32(VIRT_TO_BUS(cmd->request_buffer));
                   9967:       scb->sg_list[0].length = cpu_to_le32(cmd->request_bufflen);
                   9968:       scb->sg_length = cmd->request_bufflen;
                   9969:       hscb->SG_segment_count = 1;
                   9970:       hscb->SG_list_pointer = cpu_to_le32(VIRT_TO_BUS(&scb->sg_list[0]));
                   9971:       hscb->data_count = scb->sg_list[0].length;
                   9972:       hscb->data_pointer = scb->sg_list[0].address;
                   9973:     }
                   9974:     else
                   9975:     {
                   9976:       scb->sg_count = 0;
                   9977:       scb->sg_length = 0;
                   9978:       hscb->SG_segment_count = 0;
                   9979:       hscb->SG_list_pointer = 0;
                   9980:       hscb->data_count = 0;
                   9981:       hscb->data_pointer = 0;
                   9982:     }
                   9983:   }
                   9984: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   9985:   if((cmd->cmnd[0] == TEST_UNIT_READY) && (aic7xxx_verbose & VERBOSE_PROBE2))
                   9986:   {
                   9987:     aic7xxx_print_scb(p, scb);
                   9988:   }
                   9989: #endif
                   9990: }
                   9991: 
                   9992: /*+F*************************************************************************
                   9993:  * Function:
                   9994:  *   aic7xxx_queue
                   9995:  *
                   9996:  * Description:
                   9997:  *   Queue a SCB to the controller.
                   9998:  *-F*************************************************************************/
                   9999: int
                   10000: aic7xxx_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
                   10001: {
                   10002:   struct aic7xxx_host *p;
                   10003:   struct aic7xxx_scb *scb;
                   10004: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   10005:   int tindex = TARGET_INDEX(cmd);
                   10006: #endif
                   10007: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
                   10008:   unsigned long cpu_flags = 0;
                   10009: #endif
                   10010: 
                   10011:   p = (struct aic7xxx_host *) cmd->host->hostdata;
                   10012:   /*
                   10013:    * Check to see if channel was scanned.
                   10014:    */
1.1.1.2 ! root     10015: 
1.1       root     10016: #ifdef AIC7XXX_VERBOSE_DEBUGGING
                   10017:   if (!(p->flags & AHC_A_SCANNED) && (cmd->channel == 0))
                   10018:   {
                   10019:     if (aic7xxx_verbose & VERBOSE_PROBE2)
                   10020:       printk(INFO_LEAD "Scanning channel for devices.\n",
                   10021:         p->host_no, 0, -1, -1);
                   10022:     p->flags |= AHC_A_SCANNED;
                   10023:   }
                   10024:   else
                   10025:   {
                   10026:     if (!(p->flags & AHC_B_SCANNED) && (cmd->channel == 1))
                   10027:     {
                   10028:       if (aic7xxx_verbose & VERBOSE_PROBE2)
                   10029:         printk(INFO_LEAD "Scanning channel for devices.\n",
                   10030:           p->host_no, 1, -1, -1);
                   10031:       p->flags |= AHC_B_SCANNED;
                   10032:     }
                   10033:   }
                   10034: 
                   10035:   if (p->dev_active_cmds[tindex] > (cmd->device->queue_depth + 1))
                   10036:   {
                   10037:     printk(WARN_LEAD "Commands queued exceeds queue "
                   10038:            "depth, active=%d\n",
1.1.1.2 ! root     10039:            p->host_no, CTL_OF_CMD(cmd),
1.1       root     10040:            p->dev_active_cmds[tindex]);
                   10041:     if ( p->dev_active_cmds[tindex] > 220 )
                   10042:       p->dev_active_cmds[tindex] = 0;
                   10043:   }
                   10044: #endif
                   10045: 
                   10046:   scb = scbq_remove_head(&p->scb_data->free_scbs);
                   10047:   if (scb == NULL)
                   10048:   {
                   10049:     DRIVER_LOCK
                   10050:     aic7xxx_allocate_scb(p);
                   10051:     DRIVER_UNLOCK
                   10052:     scb = scbq_remove_head(&p->scb_data->free_scbs);
                   10053:   }
                   10054:   if (scb == NULL)
                   10055:   {
                   10056:     printk(WARN_LEAD "Couldn't get a free SCB.\n", p->host_no,
                   10057:            CTL_OF_CMD(cmd));
                   10058:     cmd->result = (DID_BUS_BUSY << 16);
                   10059:     DRIVER_LOCK
                   10060:     aic7xxx_queue_cmd_complete(p, cmd);
                   10061:     DRIVER_UNLOCK
                   10062:     return 0;
                   10063:   }
                   10064:   else
                   10065:   {
                   10066:     scb->cmd = cmd;
                   10067:     aic7xxx_position(cmd) = scb->hscb->tag;
                   10068: 
                   10069:     /*
                   10070:      * Construct the SCB beforehand, so the sequencer is
                   10071:      * paused a minimal amount of time.
                   10072:      */
                   10073:     aic7xxx_buildscb(p, cmd, scb);
                   10074: 
                   10075:     /*
                   10076:      * Make sure the Scsi_Cmnd pointer is saved, the struct it points to
                   10077:      * is set up properly, and the parity error flag is reset, then send
                   10078:      * the SCB to the sequencer and watch the fun begin.
                   10079:      */
                   10080:     cmd->scsi_done = fn;
                   10081:     cmd->result = DID_OK;
                   10082:     memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
                   10083:     aic7xxx_error(cmd) = DID_OK;
                   10084:     aic7xxx_status(cmd) = 0;
                   10085:     cmd->host_scribble = NULL;
                   10086: 
                   10087:     scb->flags |= SCB_ACTIVE | SCB_WAITINGQ;
                   10088: 
                   10089:     DRIVER_LOCK
                   10090:     scbq_insert_tail(&p->waiting_scbs, scb);
                   10091:     if ( (p->flags & (AHC_IN_ISR | AHC_IN_ABORT | AHC_IN_RESET)) == 0)
                   10092:     {
                   10093:       aic7xxx_run_waiting_queues(p);
                   10094:     }
                   10095:     DRIVER_UNLOCK
                   10096:   }
                   10097:   return (0);
                   10098: }
                   10099: 
                   10100: /*+F*************************************************************************
                   10101:  * Function:
                   10102:  *   aic7xxx_bus_device_reset
                   10103:  *
                   10104:  * Description:
                   10105:  *   Abort or reset the current SCSI command(s).  If the scb has not
                   10106:  *   previously been aborted, then we attempt to send a BUS_DEVICE_RESET
                   10107:  *   message to the target.  If the scb has previously been unsuccessfully
                   10108:  *   aborted, then we will reset the channel and have all devices renegotiate.
                   10109:  *   Returns an enumerated type that indicates the status of the operation.
                   10110:  *-F*************************************************************************/
                   10111: static int
                   10112: aic7xxx_bus_device_reset(struct aic7xxx_host *p, Scsi_Cmnd *cmd)
                   10113: {
                   10114:   struct aic7xxx_scb   *scb;
                   10115:   struct aic7xxx_hwscb *hscb;
                   10116:   int result = -1;
                   10117:   int channel;
                   10118:   unsigned char saved_scbptr, lastphase;
                   10119:   unsigned char hscb_index;
                   10120:   int disconnected;
                   10121: 
                   10122:   scb = (p->scb_data->scb_array[aic7xxx_position(cmd)]);
                   10123:   hscb = scb->hscb;
                   10124: 
                   10125:   lastphase = aic_inb(p, LASTPHASE);
                   10126:   if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   10127:   {
                   10128:     printk(INFO_LEAD "Bus Device reset, scb flags 0x%x, ",
                   10129:          p->host_no, CTL_OF_SCB(scb), scb->flags);
                   10130:     switch (lastphase)
                   10131:     {
                   10132:       case P_DATAOUT:
                   10133:         printk("Data-Out phase\n");
                   10134:         break;
                   10135:       case P_DATAIN:
                   10136:         printk("Data-In phase\n");
                   10137:         break;
                   10138:       case P_COMMAND:
                   10139:         printk("Command phase\n");
                   10140:         break;
                   10141:       case P_MESGOUT:
                   10142:         printk("Message-Out phase\n");
                   10143:         break;
                   10144:       case P_STATUS:
                   10145:         printk("Status phase\n");
                   10146:         break;
                   10147:       case P_MESGIN:
                   10148:         printk("Message-In phase\n");
                   10149:         break;
                   10150:       default:
                   10151:       /*
                   10152:        * We're not in a valid phase, so assume we're idle.
                   10153:        */
                   10154:         printk("while idle, LASTPHASE = 0x%x\n", lastphase);
                   10155:         break;
                   10156:     }
                   10157:     printk(INFO_LEAD "SCSISIGI 0x%x, SEQADDR 0x%x, SSTAT0 0x%x, SSTAT1 "
                   10158:          "0x%x\n", p->host_no, CTL_OF_SCB(scb),
                   10159:          aic_inb(p, SCSISIGI),
                   10160:          aic_inb(p, SEQADDR0) | (aic_inb(p, SEQADDR1) << 8),
                   10161:          aic_inb(p, SSTAT0), aic_inb(p, SSTAT1));
                   10162:   }
                   10163: 
                   10164:   channel = cmd->channel;
                   10165: 
                   10166:     /*
                   10167:      * Send a Device Reset Message:
                   10168:      * The target that is holding up the bus may not be the same as
                   10169:      * the one that triggered this timeout (different commands have
                   10170:      * different timeout lengths).  Our strategy here is to queue an
                   10171:      * abort message to the timed out target if it is disconnected.
                   10172:      * Otherwise, if we have an active target we stuff the message buffer
                   10173:      * with an abort message and assert ATN in the hopes that the target
                   10174:      * will let go of the bus and go to the mesgout phase.  If this
                   10175:      * fails, we'll get another timeout a few seconds later which will
                   10176:      * attempt a bus reset.
                   10177:      */
                   10178:   saved_scbptr = aic_inb(p, SCBPTR);
                   10179:   disconnected = FALSE;
                   10180: 
                   10181:   if (lastphase != P_BUSFREE)
                   10182:   {
                   10183:     if (aic_inb(p, SCB_TAG) >= p->scb_data->numscbs)
                   10184:     {
                   10185:       printk(WARN_LEAD "Invalid SCB ID %d is active, "
                   10186:              "SCB flags = 0x%x.\n", p->host_no,
                   10187:             CTL_OF_CMD(cmd), scb->hscb->tag, scb->flags);
                   10188:       return(SCSI_RESET_ERROR);
                   10189:     }
                   10190:     if (scb->hscb->tag == aic_inb(p, SCB_TAG))
1.1.1.2 ! root     10191:     {
1.1       root     10192:       if ( (lastphase != P_MESGOUT) && (lastphase != P_MESGIN) )
                   10193:       {
                   10194:         if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   10195:           printk(INFO_LEAD "Device reset message in "
                   10196:                 "message buffer\n", p->host_no, CTL_OF_SCB(scb));
                   10197:         scb->flags |= SCB_RESET | SCB_DEVICE_RESET;
                   10198:         aic7xxx_error(scb->cmd) = DID_RESET;
1.1.1.2 ! root     10199:         p->dev_flags[TARGET_INDEX(scb->cmd)] |=
1.1       root     10200:                 BUS_DEVICE_RESET_PENDING;
                   10201:         /* Send the abort message to the active SCB. */
                   10202:         aic_outb(p, HOST_MSG, MSG_OUT);
                   10203:         aic_outb(p, lastphase | ATNO, SCSISIGO);
                   10204:         return(SCSI_RESET_PENDING);
                   10205:       }
                   10206:       else
                   10207:       {
                   10208:         /* We want to send out the message, but it could screw an already */
                   10209:         /* in place and being used message.  Instead, we return an error  */
                   10210:         /* to try and start the bus reset phase since this command is     */
                   10211:         /* probably hung (aborts failed, and now reset is failing).  We   */
                   10212:         /* also make sure to set BUS_DEVICE_RESET_PENDING so we won't try */
                   10213:         /* any more on this device, but instead will escalate to a bus or */
                   10214:         /* host reset (additionally, we won't try to abort any more).     */
                   10215:         printk(WARN_LEAD "Device reset, Message buffer "
                   10216:                 "in use\n", p->host_no, CTL_OF_SCB(scb));
                   10217:         scb->flags |= SCB_RESET | SCB_DEVICE_RESET;
                   10218:         aic7xxx_error(scb->cmd) = DID_RESET;
1.1.1.2 ! root     10219:         p->dev_flags[TARGET_INDEX(scb->cmd)] |=
1.1       root     10220:                 BUS_DEVICE_RESET_PENDING;
                   10221:         return(SCSI_RESET_ERROR);
                   10222:       }
                   10223:     }
                   10224:   } /* if (last_phase != P_BUSFREE).....indicates we are idle and can work */
                   10225:   hscb_index = aic7xxx_find_scb(p, scb);
                   10226:   if (hscb_index == SCB_LIST_NULL)
                   10227:   {
                   10228:     disconnected = (aic7xxx_scb_on_qoutfifo(p, scb)) ? FALSE : TRUE;
                   10229:   }
                   10230:   else
                   10231:   {
                   10232:     aic_outb(p, hscb_index, SCBPTR);
                   10233:     if (aic_inb(p, SCB_CONTROL) & DISCONNECTED)
                   10234:     {
                   10235:       disconnected = TRUE;
                   10236:     }
                   10237:   }
                   10238:   if (disconnected)
                   10239:   {
                   10240:         /*
                   10241:          * Simply set the MK_MESSAGE flag and the SEQINT handler will do
                   10242:          * the rest on a reconnect.
                   10243:          */
                   10244:     scb->hscb->control |= MK_MESSAGE;
                   10245:     scb->flags |= SCB_RESET | SCB_DEVICE_RESET;
1.1.1.2 ! root     10246:     p->dev_flags[TARGET_INDEX(scb->cmd)] |=
1.1       root     10247:         BUS_DEVICE_RESET_PENDING;
                   10248:     if (hscb_index != SCB_LIST_NULL)
                   10249:     {
                   10250:       unsigned char scb_control;
                   10251: 
                   10252:       aic_outb(p, hscb_index, SCBPTR);
                   10253:       scb_control = aic_inb(p, SCB_CONTROL);
                   10254:       aic_outb(p, scb_control | MK_MESSAGE, SCB_CONTROL);
                   10255:     }
                   10256:         /*
                   10257:          * Actually requeue this SCB in case we can select the
                   10258:          * device before it reconnects.  If the transaction we
                   10259:          * want to abort is not tagged, then this will be the only
                   10260:          * outstanding command and we can simply shove it on the
                   10261:          * qoutfifo and be done.  If it is tagged, then it goes right
                   10262:          * in with all the others, no problem :)  We need to add it
                   10263:          * to the qinfifo and let the sequencer know it is there.
                   10264:          * Now, the only problem left to deal with is, *IF* this
                   10265:          * command completes, in spite of the MK_MESSAGE bit in the
                   10266:          * control byte, then we need to pick that up in the interrupt
                   10267:          * routine and clean things up.  This *shouldn't* ever happen.
                   10268:          */
                   10269:     if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   10270:       printk(INFO_LEAD "Queueing device reset "
                   10271:            "command.\n", p->host_no, CTL_OF_SCB(scb));
                   10272:     p->qinfifo[p->qinfifonext++] = scb->hscb->tag;
                   10273:     if (p->features & AHC_QUEUE_REGS)
                   10274:       aic_outb(p, p->qinfifonext, HNSCB_QOFF);
                   10275:     else
                   10276:       aic_outb(p, p->qinfifonext, KERNEL_QINPOS);
                   10277:     scb->flags |= SCB_QUEUED_ABORT;
                   10278:     result = SCSI_RESET_PENDING;
                   10279:   }
                   10280:   else if (result == -1)
                   10281:   {
                   10282:     result = SCSI_RESET_ERROR;
                   10283:   }
                   10284:   aic_outb(p, saved_scbptr, SCBPTR);
                   10285:   return (result);
                   10286: }
                   10287: 
                   10288: 
                   10289: /*+F*************************************************************************
                   10290:  * Function:
                   10291:  *   aic7xxx_panic_abort
                   10292:  *
                   10293:  * Description:
                   10294:  *   Abort the current SCSI command(s).
                   10295:  *-F*************************************************************************/
                   10296: void
                   10297: aic7xxx_panic_abort(struct aic7xxx_host *p, Scsi_Cmnd *cmd)
                   10298: {
                   10299: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
                   10300:   int i, mask, found, need_tag;
                   10301:   struct aic7xxx_scb *scb;
                   10302:   unsigned char qinpos, hscbp;
                   10303: 
                   10304:   found = FALSE;
                   10305: #endif
                   10306: 
                   10307:   printk("aic7xxx driver version %s/%s\n", AIC7XXX_C_VERSION,
                   10308:          UTS_RELEASE);
                   10309:   printk("Controller type:\n    %s\n", board_names[p->board_name_index]);
                   10310:   printk("p->flags=0x%x, p->chip=0x%x, p->features=0x%x, "
                   10311:          "sequencer %s paused\n",
                   10312:      p->flags, p->chip, p->features,
                   10313:     (aic_inb(p, HCNTRL) & PAUSE) ? "is" : "isn't" );
                   10314:   pause_sequencer(p);
                   10315:   disable_irq(p->irq);
                   10316:   aic7xxx_print_card(p);
                   10317:   aic7xxx_print_scratch_ram(p);
                   10318: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
                   10319:   for(i=0; i<MAX_TARGETS; i++)
                   10320:   {
                   10321:     if(p->dev_flags[i] & DEVICE_PRESENT)
                   10322:     {
                   10323:       mask = (0x01 << i);
                   10324:       printk(INFO_LEAD "dev_flags=0x%x, WDTR:%c/%c/%c, SDTR:%c/%c/%c,"
1.1.1.2 ! root     10325:              " q_depth=%d:%d\n",
1.1       root     10326:         p->host_no, 0, i, 0, p->dev_flags[i],
                   10327:         (p->wdtr_pending & mask) ? 'Y' : 'N',
                   10328:         (p->needwdtr & mask) ? 'Y' : 'N',
                   10329:         (p->needwdtr_copy & mask) ? 'Y' : 'N',
                   10330:         (p->sdtr_pending & mask) ? 'Y' : 'N',
                   10331:         (p->needsdtr & mask) ? 'Y' : 'N',
                   10332:         (p->needsdtr_copy & mask) ? 'Y' : 'N',
                   10333:         p->dev_active_cmds[i],
1.1.1.2 ! root     10334:         p->dev_max_queue_depth[i] );
1.1       root     10335:       printk(INFO_LEAD "targ_scsirate=0x%x", p->host_no, 0, i, 0,
                   10336:              aic_inb(p, TARG_SCSIRATE + i));
                   10337:       if (p->features & AHC_ULTRA2)
                   10338:         printk(", targ_offset=%d", aic_inb(p, TARG_OFFSET + i));
                   10339:       printk("\n");
                   10340:     }
                   10341:   }
                   10342:   /*
                   10343:    * Search for this command and see if we can't track it down, it's the
                   10344:    * one causing the timeout.  Print out this command first, then all other
                   10345:    * active commands afterwords.
                   10346:    */
                   10347:   need_tag = -1;
                   10348:   if ( cmd )
                   10349:   {
                   10350:     scb = p->scb_data->scb_array[aic7xxx_position(cmd)];
                   10351:     if ( (scb->flags & SCB_ACTIVE) && (scb->cmd == cmd) )
                   10352:     {
                   10353:       printk("Timed out command is scb #%d:\n", scb->hscb->tag);
                   10354:       printk("Tag%d: flags=0x%x, control=0x%x, TCL=0x%x, %s\n", scb->hscb->tag,
                   10355:              scb->flags, scb->hscb->control, scb->hscb->target_channel_lun,
                   10356:              (scb->flags & SCB_WAITINGQ) ? "WAITINGQ" : "Sent" );
                   10357:       need_tag = scb->hscb->tag;
                   10358:       if (scb->flags & SCB_WAITINGQ) found=TRUE;
                   10359:     }
                   10360:   }
                   10361:   printk("QINFIFO: (TAG) ");
                   10362:   qinpos = aic_inb(p, QINPOS);
                   10363:   while ( qinpos != p->qinfifonext )
                   10364:   {
                   10365:     if (p->qinfifo[qinpos] == need_tag)
                   10366:       found=TRUE;
                   10367:     printk("%d ", p->qinfifo[qinpos++]);
1.1.1.2 ! root     10368:   }
1.1       root     10369:   printk("\n");
                   10370:   printk("Current SCB: (SCBPTR/TAG/CONTROL) %d/%d/0x%x\n", aic_inb(p, SCBPTR),
                   10371:          aic_inb(p, SCB_TAG), aic_inb(p, SCB_CONTROL) );
                   10372:   if (aic_inb(p, SCB_TAG) == need_tag)  found=TRUE;
                   10373:   printk("WAITING_SCBS: (SCBPTR/TAG/CONTROL) %d->",
                   10374:          hscbp = aic_inb(p, WAITING_SCBH));
                   10375:   while (hscbp != SCB_LIST_NULL)
                   10376:   {
                   10377:     aic_outb(p, hscbp, SCBPTR);
                   10378:     printk("%d/%d/0x%x ", hscbp, aic_inb(p, SCB_TAG), aic_inb(p, SCB_CONTROL));
                   10379:     hscbp = aic_inb(p, SCB_NEXT);
                   10380:     if (aic_inb(p, SCB_TAG) == need_tag)  found=TRUE;
                   10381:   }
                   10382:   printk("\n");
                   10383:   printk("DISCONNECTED_SCBS: (SCBPTR/TAG/CONTROL) %d->",
                   10384:          hscbp = aic_inb(p, DISCONNECTED_SCBH));
                   10385:   while (hscbp != SCB_LIST_NULL)
                   10386:   {
                   10387:     aic_outb(p, hscbp, SCBPTR);
                   10388:     printk("%d/%d/0x%x ", hscbp, aic_inb(p, SCB_TAG), aic_inb(p, SCB_CONTROL));
                   10389:     hscbp = aic_inb(p, SCB_NEXT);
                   10390:     if (aic_inb(p, SCB_TAG) == need_tag)  found=TRUE;
                   10391:   }
                   10392:   printk("\n");
                   10393:   printk("FREE_SCBS: (SCBPTR/TAG/CONTROL) %d->",
                   10394:          hscbp = aic_inb(p, FREE_SCBH));
                   10395:   while (hscbp != SCB_LIST_NULL)
                   10396:   {
                   10397:     aic_outb(p, hscbp, SCBPTR);
                   10398:     printk("%d/%d/0x%x ", hscbp, aic_inb(p, SCB_TAG), aic_inb(p, SCB_CONTROL));
                   10399:     hscbp = aic_inb(p, SCB_NEXT);
                   10400:   }
                   10401:   printk("\n");
                   10402: 
                   10403:   if (found == FALSE)
                   10404:   {
                   10405:     /*
                   10406:      * We haven't found the offending SCB yet, and it should be around
                   10407:      * somewhere, so go look for it in the cards SCBs.
                   10408:      */
                   10409:     printk("SCBPTR CONTROL TAG PREV NEXT\n");
                   10410:     for(i=0; i<p->scb_data->maxhscbs; i++)
                   10411:     {
                   10412:       aic_outb(p, i, SCBPTR);
                   10413:       printk("   %3d      %02x  %02x   %02x   %02x\n", i,
                   10414:              aic_inb(p, SCB_CONTROL), aic_inb(p, SCB_TAG),
                   10415:              aic_inb(p, SCB_PREV), aic_inb(p, SCB_NEXT));
                   10416:     }
                   10417:   }
1.1.1.2 ! root     10418: 
1.1       root     10419: 
                   10420:   for (i=0; i < p->scb_data->numscbs; i++)
                   10421:   {
                   10422:     scb = p->scb_data->scb_array[i];
                   10423:     if ( (scb->flags & SCB_ACTIVE) && (scb->cmd != cmd) )
                   10424:     {
                   10425:       printk("Tag%d: flags=0x%x, control=0x%x, TCL=0x%x, %s\n", scb->hscb->tag,
                   10426:              scb->flags, scb->hscb->control, scb->hscb->target_channel_lun,
                   10427:              (scb->flags & SCB_WAITINGQ) ? "WAITINGQ" : "Sent" );
                   10428:     }
                   10429:   }
                   10430: #endif
                   10431:   sti();
                   10432:   for(;;) barrier();
                   10433: }
                   10434: 
                   10435: /*+F*************************************************************************
                   10436:  * Function:
                   10437:  *   aic7xxx_abort
                   10438:  *
                   10439:  * Description:
                   10440:  *   Abort the current SCSI command(s).
                   10441:  *-F*************************************************************************/
                   10442: int
                   10443: aic7xxx_abort(Scsi_Cmnd *cmd)
                   10444: {
                   10445:   struct aic7xxx_scb  *scb = NULL;
                   10446:   struct aic7xxx_host *p;
                   10447:   int    result, found=0;
                   10448:   unsigned char tmp_char, saved_hscbptr, next_hscbptr, prev_hscbptr;
                   10449: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
                   10450:   unsigned long cpu_flags = 0;
                   10451: #endif
                   10452:   Scsi_Cmnd *cmd_next, *cmd_prev;
                   10453: 
                   10454:   p = (struct aic7xxx_host *) cmd->host->hostdata;
                   10455:   scb = (p->scb_data->scb_array[aic7xxx_position(cmd)]);
                   10456: 
                   10457:   /*
                   10458:    * I added a new config option to the driver: "panic_on_abort" that will
                   10459:    * cause the driver to panic and the machine to stop on the first abort
                   10460:    * or reset call into the driver.  At that point, it prints out a lot of
                   10461:    * usefull information for me which I can then use to try and debug the
                   10462:    * problem.  Simply enable the boot time prompt in order to activate this
                   10463:    * code.
                   10464:    */
                   10465:   if (aic7xxx_panic_on_abort)
                   10466:     aic7xxx_panic_abort(p, cmd);
                   10467: 
                   10468:   DRIVER_LOCK
                   10469: 
                   10470: /*
                   10471:  *  Run the isr to grab any command in the QOUTFIFO and any other misc.
                   10472:  *  assundry tasks.  This should also set up the bh handler if there is
                   10473:  *  anything to be done, but it won't run until we are done here since
                   10474:  *  we are following a straight code path without entering the scheduler
                   10475:  *  code.
                   10476:  */
                   10477: 
                   10478:   pause_sequencer(p);
                   10479:   while ( (aic_inb(p, INTSTAT) & INT_PEND) && !(p->flags & AHC_IN_ISR))
                   10480:   {
                   10481:     aic7xxx_isr(p->irq, p, (void *)NULL);
                   10482:     pause_sequencer(p);
                   10483:     aic7xxx_done_cmds_complete(p);
                   10484:   }
                   10485: 
                   10486:   if ((scb == NULL) || (cmd->serial_number != cmd->serial_number_at_timeout))
                   10487:                       /*  Totally bogus cmd since it points beyond our  */
                   10488:   {                   /*  valid SCB range or doesn't even match it's own*/
                   10489:                       /*  timeout serial number.                        */
                   10490:     if (aic7xxx_verbose & VERBOSE_ABORT_MID)
                   10491:       printk(INFO_LEAD "Abort called with bogus Scsi_Cmnd "
                   10492:         "pointer.\n", p->host_no, CTL_OF_CMD(cmd));
                   10493:     unpause_sequencer(p, FALSE);
                   10494:     DRIVER_UNLOCK
                   10495:     return(SCSI_ABORT_NOT_RUNNING);
                   10496:   }
                   10497:   if (scb->cmd != cmd)  /*  Hmmm...either this SCB is currently free with a */
                   10498:   {                     /*  NULL cmd pointer (NULLed out when freed) or it  */
                   10499:                         /*  has already been recycled for another command   */
                   10500:                         /*  Either way, this SCB has nothing to do with this*/
                   10501:                         /*  command and we need to deal with cmd without    */
                   10502:                         /*  touching the SCB.                               */
                   10503:                         /*  The theory here is to return a value that will  */
                   10504:                         /*  make the queued for complete command actually   */
                   10505:                         /*  finish successfully, or to indicate that we     */
                   10506:                         /*  don't have this cmd any more and the mid level  */
                   10507:                         /*  code needs to find it.                          */
                   10508:     cmd_next = p->completeq.head;
                   10509:     cmd_prev = NULL;
1.1.1.2 ! root     10510:     while (cmd_next != NULL)
1.1       root     10511:     {
1.1.1.2 ! root     10512:       if (cmd_next == cmd)
1.1       root     10513:       {
                   10514:         if (aic7xxx_verbose & VERBOSE_ABORT_PROCESS)
                   10515:           printk(INFO_LEAD "Abort called for command "
                   10516:           "on completeq, completing.\n", p->host_no, CTL_OF_CMD(cmd));
                   10517:         if ( cmd_prev == NULL )
                   10518:           p->completeq.head = (Scsi_Cmnd *)cmd_next->host_scribble;
                   10519:         else
                   10520:           cmd_prev->host_scribble = cmd_next->host_scribble;
                   10521:         cmd_next->scsi_done(cmd_next);
                   10522:         unpause_sequencer(p, FALSE);
                   10523:         DRIVER_UNLOCK
                   10524:         return(SCSI_ABORT_NOT_RUNNING); /* It's already back as a successful
                   10525:                                          * completion */
1.1.1.2 ! root     10526:       }
1.1       root     10527:       cmd_prev = cmd_next;
                   10528:       cmd_next = (Scsi_Cmnd *)cmd_next->host_scribble;
                   10529:     }
                   10530:     if (aic7xxx_verbose & VERBOSE_ABORT_MID)
                   10531:       printk(INFO_LEAD "Abort called for already completed"
                   10532:         " command.\n", p->host_no, CTL_OF_CMD(cmd));
                   10533:     unpause_sequencer(p, FALSE);
                   10534:     DRIVER_UNLOCK
                   10535:     return(SCSI_ABORT_NOT_RUNNING);
                   10536:   }
1.1.1.2 ! root     10537: 
1.1       root     10538: /*   At this point we know the following:
                   10539:  *     the SCB pointer is valid
                   10540:  *     the command pointer passed in to us and the scb->cmd pointer match
                   10541:  *     this then means that the command we need to abort is the same as the
                   10542:  *     command held by the scb pointer and is a valid abort request.
                   10543:  *   Now, we just have to figure out what to do from here.  Current plan is:
                   10544:  *     if we have already been here on this command, escalate to a reset
                   10545:  *     if scb is on waiting list or QINFIFO, send it back as aborted, but
                   10546:  *       we also need to be aware of the possibility that we could be using
                   10547:  *       a faked negotiation command that is holding this command up,  if
                   10548:  *       so we need to take care of that command instead, which means we
                   10549:  *       would then treat this one like it was sitting around disconnected
                   10550:  *       instead.
                   10551:  *     if scb is on WAITING_SCB list in sequencer, free scb and send back
                   10552:  *     if scb is disconnected and not completed, abort with abort message
                   10553:  *     if scb is currently running, then it may be causing the bus to hang
                   10554:  *       so we want a return value that indicates a reset would be appropriate
                   10555:  *       if the command does not finish shortly
                   10556:  *     if scb is already complete but not on completeq, we're screwed because
                   10557:  *       this can't happen (except if the command is in the QOUTFIFO, in which
                   10558:  *       case we would like it to complete successfully instead of having to
                   10559:  *       to be re-done)
                   10560:  *   All other scenarios already dealt with by previous code.
                   10561:  */
                   10562: 
                   10563:   if ( scb->flags & (SCB_ABORT | SCB_RESET | SCB_QUEUED_ABORT) )
                   10564:   {
                   10565:     if (aic7xxx_verbose & VERBOSE_ABORT_PROCESS)
                   10566:       printk(INFO_LEAD "SCB aborted once already, "
                   10567:         "escalating.\n", p->host_no, CTL_OF_SCB(scb));
                   10568:     unpause_sequencer(p, FALSE);
                   10569:     DRIVER_UNLOCK
                   10570:     return(SCSI_ABORT_SNOOZE);
                   10571:   }
1.1.1.2 ! root     10572:   if ( (p->flags & (AHC_RESET_PENDING | AHC_ABORT_PENDING)) ||
        !          10573:           (p->dev_flags[TARGET_INDEX(scb->cmd)] &
1.1       root     10574:            BUS_DEVICE_RESET_PENDING) )
                   10575:   {
                   10576:     if (aic7xxx_verbose & VERBOSE_ABORT_PROCESS)
                   10577:       printk(INFO_LEAD "Reset/Abort pending for this "
                   10578:         "device, not wasting our time.\n", p->host_no, CTL_OF_SCB(scb));
                   10579:     unpause_sequencer(p, FALSE);
                   10580:     DRIVER_UNLOCK
                   10581:     return(SCSI_ABORT_PENDING);
                   10582:   }
                   10583: 
                   10584:   found = 0;
                   10585:   p->flags |= AHC_IN_ABORT;
                   10586:   if (aic7xxx_verbose & VERBOSE_ABORT)
                   10587:     printk(INFO_LEAD "Aborting scb %d, flags 0x%x\n",
                   10588:          p->host_no, CTL_OF_SCB(scb), scb->hscb->tag, scb->flags);
                   10589: 
                   10590: /*
                   10591:  *   First, let's check to see if the currently running command is our target
                   10592:  *    since if it is, the return is fairly easy and quick since we don't want
                   10593:  *    to touch the command in case it might complete, but we do want a timeout
                   10594:  *    in case it's actually hung, so we really do nothing, but tell the mid
                   10595:  *    level code to reset the timeout.
                   10596:  */
                   10597: 
                   10598:   if ( scb->hscb->tag == aic_inb(p, SCB_TAG) )
                   10599:   {
                   10600:    /*
                   10601:     *  Check to see if the sequencer is just sitting on this command, or
                   10602:     *   if it's actively being run.
                   10603:     */
                   10604:     result = aic_inb(p, LASTPHASE);
                   10605:     switch (result)
                   10606:     {
                   10607:       case P_DATAOUT:    /*    For any of these cases, we can assume we are */
                   10608:       case P_DATAIN:     /*    an active command and act according.  For    */
                   10609:       case P_COMMAND:    /*    anything else we are going to fall on through*/
                   10610:       case P_STATUS:     /*    The SCSI_ABORT_SNOOZE will give us two abort */
                   10611:       case P_MESGOUT:    /*    chances to finish and then escalate to a     */
                   10612:       case P_MESGIN:     /*    reset call                                   */
                   10613:         if (aic7xxx_verbose & VERBOSE_ABORT_PROCESS)
                   10614:           printk(INFO_LEAD "SCB is currently active.  "
                   10615:                 "Waiting on completion.\n", p->host_no, CTL_OF_SCB(scb));
                   10616:         unpause_sequencer(p, FALSE);
                   10617:         p->flags &= ~AHC_IN_ABORT;
                   10618:         scb->flags |= SCB_RECOVERY_SCB; /*  Note the fact that we've been  */
                   10619:         p->flags |= AHC_ABORT_PENDING;  /*  here so we will know not to    */
                   10620:         DRIVER_UNLOCK                   /*  muck with other SCBs if this   */
                   10621:         return(SCSI_ABORT_PENDING);     /*  one doesn't complete and clear */
                   10622:         break;                          /*  out.                           */
                   10623:       default:
                   10624:         break;
                   10625:     }
                   10626:   }
                   10627: 
                   10628:   if ((found == 0) && (scb->flags & SCB_WAITINGQ))
                   10629:   {
                   10630:     int tindex = TARGET_INDEX(cmd);
                   10631: #ifdef AIC7XXX_FAKE_NEGOTIATION_CMDS
                   10632:     unsigned short mask;
                   10633: 
                   10634:     mask = (1 << tindex);
                   10635: 
                   10636:     if (p->wdtr_pending & mask)
                   10637:     {
                   10638:       if (p->dev_wdtr_cmnd[tindex]->next != cmd)
                   10639:         found = 1;
                   10640:       else
                   10641:         found = 0;
                   10642:     }
                   10643:     else if (p->sdtr_pending & mask)
                   10644:     {
                   10645:       if (p->dev_sdtr_cmnd[tindex]->next != cmd)
                   10646:         found = 1;
                   10647:       else
                   10648:         found = 0;
                   10649:     }
                   10650:     else
                   10651:     {
                   10652:       found = 1;
                   10653:     }
                   10654:     if (found == 0)
                   10655:     {
                   10656:       /*
                   10657:        * OK..this means the command we are currently getting an abort
                   10658:        * for has an outstanding negotiation command in front of it.
                   10659:        * We don't really have a way to tie back into the negotiation
                   10660:        * commands, so we just send this back as pending, then it
                   10661:        * will get reset in 2 seconds.
                   10662:        */
                   10663:       unpause_sequencer(p, TRUE);
                   10664:       scb->flags |= SCB_ABORT;
                   10665:       DRIVER_UNLOCK
                   10666:       return(SCSI_ABORT_PENDING);
                   10667:     }
                   10668: #endif
1.1.1.2 ! root     10669:     if (aic7xxx_verbose & VERBOSE_ABORT_PROCESS)
1.1       root     10670:       printk(INFO_LEAD "SCB found on waiting list and "
                   10671:           "aborted.\n", p->host_no, CTL_OF_SCB(scb));
                   10672:     scbq_remove(&p->waiting_scbs, scb);
                   10673:     scbq_remove(&p->delayed_scbs[tindex], scb);
                   10674:     p->dev_active_cmds[tindex]++;
                   10675:     p->activescbs++;
                   10676:     scb->flags &= ~(SCB_WAITINGQ | SCB_ACTIVE);
                   10677:     scb->flags |= SCB_ABORT | SCB_QUEUED_FOR_DONE;
                   10678:     found = 1;
                   10679:   }
                   10680: 
                   10681: /*
                   10682:  *  We just checked the waiting_q, now for the QINFIFO
                   10683:  */
                   10684:   if ( found == 0 )
                   10685:   {
1.1.1.2 ! root     10686:     if ( ((found = aic7xxx_search_qinfifo(p, cmd->target,
1.1       root     10687:                      cmd->channel,
                   10688:                      cmd->lun, scb->hscb->tag, SCB_ABORT | SCB_QUEUED_FOR_DONE,
                   10689:                      FALSE, NULL)) != 0) &&
                   10690:                     (aic7xxx_verbose & VERBOSE_ABORT_PROCESS))
                   10691:       printk(INFO_LEAD "SCB found in QINFIFO and "
                   10692:         "aborted.\n", p->host_no, CTL_OF_SCB(scb));
                   10693:   }
                   10694: 
                   10695: /*
                   10696:  *  QINFIFO, waitingq, completeq done.  Next, check WAITING_SCB list in card
                   10697:  */
                   10698: 
                   10699:   if ( found == 0 )
                   10700:   {
                   10701:     unsigned char scb_next_ptr;
                   10702:     prev_hscbptr = SCB_LIST_NULL;
                   10703:     saved_hscbptr = aic_inb(p, SCBPTR);
                   10704:     next_hscbptr = aic_inb(p, WAITING_SCBH);
                   10705:     while ( next_hscbptr != SCB_LIST_NULL )
                   10706:     {
                   10707:       aic_outb(p,  next_hscbptr, SCBPTR );
                   10708:       if ( scb->hscb->tag == aic_inb(p, SCB_TAG) )
                   10709:       {
                   10710:         found = 1;
                   10711:         if (aic7xxx_verbose & VERBOSE_ABORT_PROCESS)
                   10712:           printk(INFO_LEAD "SCB found on hardware waiting"
                   10713:             " list and aborted.\n", p->host_no, CTL_OF_SCB(scb));
                   10714:         if ( prev_hscbptr == SCB_LIST_NULL )
                   10715:         {
                   10716:             aic_outb(p, aic_inb(p, SCB_NEXT), WAITING_SCBH);
                   10717:             /* stop the selection since we just
                   10718:              * grabbed the scb out from under the
                   10719:              * card
                   10720:              */
                   10721:             aic_outb(p, aic_inb(p, SCSISEQ) & ~ENSELO, SCSISEQ);
                   10722:             aic_outb(p, CLRSELTIMEO, CLRSINT1);
                   10723:         }
                   10724:         else
                   10725:         {
                   10726:             scb_next_ptr = aic_inb(p, SCB_NEXT);
                   10727:             aic_outb(p, prev_hscbptr, SCBPTR);
                   10728:             aic_outb(p, scb_next_ptr, SCB_NEXT);
                   10729:             aic_outb(p, next_hscbptr, SCBPTR);
                   10730:         }
                   10731:         aic_outb(p, SCB_LIST_NULL, SCB_TAG);
                   10732:         aic_outb(p, 0, SCB_CONTROL);
                   10733:         aic7xxx_add_curscb_to_free_list(p);
                   10734:         scb->flags = SCB_ABORT | SCB_QUEUED_FOR_DONE;
                   10735:         break;
                   10736:       }
                   10737:       prev_hscbptr = next_hscbptr;
                   10738:       next_hscbptr = aic_inb(p, SCB_NEXT);
                   10739:     }
                   10740:     aic_outb(p,  saved_hscbptr, SCBPTR );
                   10741:   }
1.1.1.2 ! root     10742: 
1.1       root     10743: /*
                   10744:  *  Hmmm...completeq, QOUTFIFO, QINFIFO, WAITING_SCBH, waitingq all checked.
                   10745:  *  OK...the sequencer's paused, interrupts are off, and we haven't found the
                   10746:  *  command anyplace where it could be easily aborted.  Time for the hard
                   10747:  *  work.  We also know the command is valid.  This essentially means the
                   10748:  *  command is disconnected, or connected but not into any phases yet, which
                   10749:  *  we know due to the tests we ran earlier on the current active scb phase.
                   10750:  *  At this point we can queue the abort tag and go on with life.
                   10751:  */
                   10752: 
                   10753:   if ( found == 0 )
                   10754:   {
                   10755:     p->flags |= AHC_ABORT_PENDING;
                   10756:     scb->flags |= SCB_QUEUED_ABORT | SCB_ABORT | SCB_RECOVERY_SCB;
                   10757:     scb->hscb->control |= MK_MESSAGE;
                   10758:     result=aic7xxx_find_scb(p, scb);
1.1.1.2 ! root     10759:     if ( result != SCB_LIST_NULL )
1.1       root     10760:     {
                   10761:       saved_hscbptr = aic_inb(p, SCBPTR);
                   10762:       aic_outb(p, result, SCBPTR);
                   10763:       tmp_char = aic_inb(p, SCB_CONTROL);
                   10764:       aic_outb(p,  tmp_char | MK_MESSAGE, SCB_CONTROL);
                   10765:       aic_outb(p, saved_hscbptr, SCBPTR);
                   10766:     }
                   10767:     if (aic7xxx_verbose & VERBOSE_ABORT_PROCESS)
                   10768:       printk(INFO_LEAD "SCB disconnected.  Queueing Abort"
                   10769:         " SCB.\n", p->host_no, CTL_OF_SCB(scb));
                   10770:     p->qinfifo[p->qinfifonext++] = scb->hscb->tag;
                   10771:     if (p->features & AHC_QUEUE_REGS)
                   10772:       aic_outb(p, p->qinfifonext, HNSCB_QOFF);
                   10773:     else
                   10774:       aic_outb(p, p->qinfifonext, KERNEL_QINPOS);
                   10775:   }
                   10776:   if (found)
                   10777:   {
                   10778:     aic7xxx_run_done_queue(p, TRUE);
                   10779:     aic7xxx_run_waiting_queues(p);
                   10780:   }
                   10781:   p->flags &= ~AHC_IN_ABORT;
                   10782:   unpause_sequencer(p, FALSE);
                   10783:   DRIVER_UNLOCK
                   10784: 
                   10785: /*
                   10786:  *  On the return value.  If we found the command and aborted it, then we know
                   10787:  *  it's already sent back and there is no reason for a further timeout, so
                   10788:  *  we use SCSI_ABORT_SUCCESS.  On the queued abort side, we aren't so certain
                   10789:  *  there hasn't been a bus hang or something that might keep the abort from
                   10790:  *  from completing.  Therefore, we use SCSI_ABORT_PENDING.  The first time this
                   10791:  *  is passed back, the timeout on the command gets extended, the second time
                   10792:  *  we pass this back, the mid level SCSI code calls our reset function, which
                   10793:  *  would shake loose a hung bus.
                   10794:  */
                   10795:   if ( found != 0 )
                   10796:     return(SCSI_ABORT_SUCCESS);
                   10797:   else
1.1.1.2 ! root     10798:     return(SCSI_ABORT_PENDING);
1.1       root     10799: }
                   10800: 
                   10801: 
                   10802: /*+F*************************************************************************
                   10803:  * Function:
                   10804:  *   aic7xxx_reset
                   10805:  *
                   10806:  * Description:
                   10807:  *   Resetting the bus always succeeds - is has to, otherwise the
                   10808:  *   kernel will panic! Try a surgical technique - sending a BUS
                   10809:  *   DEVICE RESET message - on the offending target before pulling
                   10810:  *   the SCSI bus reset line.
                   10811:  *-F*************************************************************************/
                   10812: int
                   10813: aic7xxx_reset(Scsi_Cmnd *cmd, unsigned int flags)
                   10814: {
                   10815:   struct aic7xxx_scb *scb = NULL;
                   10816:   struct aic7xxx_host *p;
                   10817:   int    tindex;
                   10818:   int    result = -1;
                   10819: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,95)
                   10820:   unsigned long cpu_flags = 0;
                   10821: #endif
                   10822: #define DEVICE_RESET 0x01
                   10823: #define BUS_RESET    0x02
                   10824: #define HOST_RESET   0x04
                   10825: #define FAIL         0x08
                   10826: #define RESET_DELAY  0x10
                   10827:   int        action;
                   10828:   Scsi_Cmnd *cmd_prev, *cmd_next;
                   10829: 
                   10830: 
                   10831:   if ( cmd == NULL )
                   10832:   {
                   10833:     printk(KERN_WARNING "(scsi?:?:?:?) Reset called with NULL Scsi_Cmnd "
                   10834:       "pointer, failing.\n");
                   10835:     return(SCSI_RESET_SNOOZE);
                   10836:   }
                   10837: 
                   10838:   p = (struct aic7xxx_host *) cmd->host->hostdata;
                   10839:   scb = (p->scb_data->scb_array[aic7xxx_position(cmd)]);
                   10840:   tindex = TARGET_INDEX(cmd);
                   10841: 
                   10842:   /*
                   10843:    * I added a new config option to the driver: "panic_on_abort" that will
                   10844:    * cause the driver to panic and the machine to stop on the first abort
                   10845:    * or reset call into the driver.  At that point, it prints out a lot of
                   10846:    * usefull information for me which I can then use to try and debug the
                   10847:    * problem.  Simply enable the boot time prompt in order to activate this
                   10848:    * code.
                   10849:    */
                   10850:   if (aic7xxx_panic_on_abort)
                   10851:     aic7xxx_panic_abort(p, cmd);
                   10852: 
                   10853:   DRIVER_LOCK
                   10854: 
                   10855:   pause_sequencer(p);
                   10856:   while ( (aic_inb(p, INTSTAT) & INT_PEND) && !(p->flags & AHC_IN_ISR))
                   10857:   {
                   10858:     aic7xxx_isr(p->irq, p, (void *)NULL );
                   10859:     pause_sequencer(p);
                   10860:     aic7xxx_done_cmds_complete(p);
                   10861:   }
                   10862: 
                   10863:   if (scb == NULL)
                   10864:   {
                   10865:     if (aic7xxx_verbose & VERBOSE_RESET_MID)
                   10866:       printk(INFO_LEAD "Reset called with bogus Scsi_Cmnd"
                   10867:            "->SCB mapping, improvising.\n", p->host_no, CTL_OF_CMD(cmd));
                   10868:     if ( flags & SCSI_RESET_SUGGEST_HOST_RESET )
                   10869:     {
                   10870:       action = HOST_RESET;
                   10871:     }
                   10872:     else
                   10873:     {
                   10874:       action = BUS_RESET;
                   10875:     }
                   10876:   }
1.1.1.2 ! root     10877:   else if (scb->cmd != cmd)
1.1       root     10878:   {
                   10879:     if (aic7xxx_verbose & VERBOSE_RESET_MID)
                   10880:     printk(INFO_LEAD "Reset called with recycled SCB "
                   10881:         "for cmd.\n", p->host_no, CTL_OF_CMD(cmd));
                   10882:     cmd_prev = NULL;
                   10883:     cmd_next = p->completeq.head;
                   10884:     while ( cmd_next != NULL )
                   10885:     {
                   10886:       if (cmd_next == cmd)
                   10887:       {
                   10888:         if (aic7xxx_verbose & VERBOSE_RESET_RETURN)
                   10889:           printk(INFO_LEAD "Reset, found cmd on completeq"
                   10890:           ", completing.\n", p->host_no, CTL_OF_CMD(cmd));
                   10891:         unpause_sequencer(p, FALSE);
                   10892:         DRIVER_UNLOCK
                   10893:         return(SCSI_RESET_NOT_RUNNING);
                   10894:       }
                   10895:       cmd_prev = cmd_next;
                   10896:       cmd_next = (Scsi_Cmnd *)cmd_next->host_scribble;
                   10897:     }
                   10898:     if ( !(flags & SCSI_RESET_SYNCHRONOUS) )
                   10899:     {
                   10900:       if (aic7xxx_verbose & VERBOSE_RESET_RETURN)
                   10901:         printk(INFO_LEAD "Reset, cmd not found,"
                   10902:           " failing.\n", p->host_no, CTL_OF_CMD(cmd));
                   10903:       unpause_sequencer(p, FALSE);
                   10904:       DRIVER_UNLOCK
                   10905:       return(SCSI_RESET_NOT_RUNNING);
                   10906:     }
                   10907:     else
                   10908:     {
                   10909:       if (aic7xxx_verbose & VERBOSE_RESET_MID)
                   10910:         printk(INFO_LEAD "Reset called, no scb, "
                   10911:           "flags 0x%x\n", p->host_no, CTL_OF_CMD(cmd), flags);
                   10912:       scb = NULL;
                   10913:       action = HOST_RESET;
                   10914:     }
                   10915:   }
                   10916:   else
                   10917:   {
                   10918:     if (aic7xxx_verbose & VERBOSE_RESET_MID)
                   10919:       printk(INFO_LEAD "Reset called, scb %d, flags "
                   10920:         "0x%x\n", p->host_no, CTL_OF_SCB(scb), scb->hscb->tag, scb->flags);
                   10921:     if ( aic7xxx_scb_on_qoutfifo(p, scb) )
                   10922:     {
                   10923:       if(aic7xxx_verbose & VERBOSE_RESET_RETURN)
                   10924:         printk(INFO_LEAD "SCB on qoutfifo, returning.\n", p->host_no,
                   10925:           CTL_OF_SCB(scb));
                   10926:       unpause_sequencer(p, FALSE);
                   10927:       DRIVER_UNLOCK
                   10928:       return(SCSI_RESET_NOT_RUNNING);
                   10929:     }
                   10930:     if ( flags & SCSI_RESET_SUGGEST_HOST_RESET )
                   10931:     {
                   10932:       action = HOST_RESET;
                   10933:     }
                   10934:     else if ( flags & SCSI_RESET_SUGGEST_BUS_RESET )
                   10935:     {
                   10936:       action = BUS_RESET;
                   10937:     }
1.1.1.2 ! root     10938:     else
1.1       root     10939:     {
                   10940:       action = DEVICE_RESET;
                   10941:     }
                   10942:   }
1.1.1.2 ! root     10943:   if ( (action & DEVICE_RESET) &&
1.1       root     10944:         (p->dev_flags[tindex] & BUS_DEVICE_RESET_PENDING) )
                   10945:   {
                   10946:     if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   10947:       printk(INFO_LEAD "Bus device reset already sent to "
                   10948:         "device, escalating.\n", p->host_no, CTL_OF_CMD(cmd));
                   10949:     action = BUS_RESET;
                   10950:   }
                   10951:   if ( (action & DEVICE_RESET) &&
                   10952:        (scb->flags & SCB_QUEUED_ABORT) )
                   10953:   {
                   10954:     if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   10955:     {
                   10956:       printk(INFO_LEAD "Have already attempted to reach "
                   10957:         "device with queued\n", p->host_no, CTL_OF_CMD(cmd));
                   10958:       printk(INFO_LEAD "message, will escalate to bus "
                   10959:         "reset.\n", p->host_no, CTL_OF_CMD(cmd));
                   10960:     }
                   10961:     action = BUS_RESET;
                   10962:   }
1.1.1.2 ! root     10963:   if ( (action & DEVICE_RESET) &&
1.1       root     10964:        (p->flags & (AHC_RESET_PENDING | AHC_ABORT_PENDING)) )
                   10965:   {
                   10966:     if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   10967:      printk(INFO_LEAD "Bus device reset stupid when "
                   10968:         "other action has failed.\n", p->host_no, CTL_OF_CMD(cmd));
                   10969:     action = BUS_RESET;
                   10970:   }
                   10971:   if ( (action & BUS_RESET) && !(p->features & AHC_TWIN) )
                   10972:   {
                   10973:     action = HOST_RESET;
                   10974:   }
1.1.1.2 ! root     10975:   if ( (p->dev_flags[tindex] & DEVICE_RESET_DELAY) &&
1.1       root     10976:        !(action & (HOST_RESET | BUS_RESET)))
                   10977:   {
                   10978:     if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   10979:     {
                   10980:       printk(INFO_LEAD "Reset called too soon after last "
                   10981:         "reset without requesting\n", p->host_no, CTL_OF_CMD(cmd));
                   10982:       printk(INFO_LEAD "bus or host reset, escalating.\n", p->host_no,
                   10983:         CTL_OF_CMD(cmd));
                   10984:     }
                   10985:     action = BUS_RESET;
                   10986:   }
1.1.1.2 ! root     10987:   if ( (p->flags & AHC_RESET_DELAY) &&
        !          10988:        (action & (HOST_RESET | BUS_RESET)) )
1.1       root     10989:   {
                   10990:     if (aic7xxx_verbose & VERBOSE_RESET_PROCESS)
                   10991:       printk(INFO_LEAD "Reset called too soon after "
                   10992:         "last bus reset, delaying.\n", p->host_no, CTL_OF_CMD(cmd));
                   10993:     action = RESET_DELAY;
                   10994:   }
                   10995: /*
                   10996:  *  By this point, we want to already know what we are going to do and
                   10997:  *  only have the following code implement our course of action.
                   10998:  */
                   10999:   switch (action)
                   11000:   {
                   11001:     case RESET_DELAY:
                   11002:       unpause_sequencer(p, FALSE);
                   11003:       DRIVER_UNLOCK
                   11004:       return(SCSI_RESET_PENDING);
                   11005:       break;
                   11006:     case FAIL:
                   11007:       unpause_sequencer(p, FALSE);
                   11008:       DRIVER_UNLOCK
                   11009:       return(SCSI_RESET_ERROR);
                   11010:       break;
                   11011:     case DEVICE_RESET:
                   11012:       p->flags |= AHC_IN_RESET;
                   11013:       result = aic7xxx_bus_device_reset(p, cmd);
                   11014:       aic7xxx_run_done_queue(p, TRUE);
                   11015:       /*  We can't rely on run_waiting_queues to unpause the sequencer for
                   11016:        *  PCI based controllers since we use AAP */
                   11017:       aic7xxx_run_waiting_queues(p);
                   11018:       unpause_sequencer(p, FALSE);
                   11019:       p->flags &= ~AHC_IN_RESET;
                   11020:       DRIVER_UNLOCK
                   11021:       return(result);
                   11022:       break;
                   11023:     case BUS_RESET:
                   11024:     case HOST_RESET:
                   11025:     default:
1.1.1.2 ! root     11026:       p->flags |= AHC_IN_RESET | AHC_RESET_DELAY;
        !          11027:       p->dev_expires[p->scsi_id] = jiffies + (3 * HZ);
        !          11028:       p->dev_timer_active |= (0x01 << p->scsi_id);
        !          11029:       if ( !(p->dev_timer_active & (0x01 << MAX_TARGETS)) ||
        !          11030:             time_after_eq(p->dev_timer.expires, p->dev_expires[p->scsi_id]) )
        !          11031:       {
        !          11032:         del_timer(&p->dev_timer);
        !          11033:         p->dev_timer.expires = p->dev_expires[p->scsi_id];
        !          11034:         add_timer(&p->dev_timer);
        !          11035:         p->dev_timer_active |= (0x01 << MAX_TARGETS);
        !          11036:       }
1.1       root     11037:       aic7xxx_reset_channel(p, cmd->channel, TRUE);
                   11038:       if ( (p->features & AHC_TWIN) && (action & HOST_RESET) )
                   11039:       {
                   11040:         aic7xxx_reset_channel(p, cmd->channel ^ 0x01, TRUE);
                   11041:         restart_sequencer(p);
                   11042:       }
                   11043:       if (action != HOST_RESET)
                   11044:         result = SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET;
                   11045:       else
                   11046:       {
                   11047:         result = SCSI_RESET_SUCCESS | SCSI_RESET_HOST_RESET;
                   11048:         aic_outb(p,  aic_inb(p, SIMODE1) & ~(ENREQINIT|ENBUSFREE),
                   11049:           SIMODE1);
                   11050:         aic7xxx_clear_intstat(p);
                   11051:         p->flags &= ~AHC_HANDLING_REQINITS;
                   11052:         p->msg_type = MSG_TYPE_NONE;
                   11053:         p->msg_index = 0;
                   11054:         p->msg_len = 0;
                   11055:       }
                   11056:       aic7xxx_run_done_queue(p, TRUE);
1.1.1.2 ! root     11057:       /*
        !          11058:        * If this a SCSI_RESET_SYNCHRONOUS then the command we were given is
        !          11059:        * in need of being re-started, so send it on through to aic7xxx_queue
        !          11060:        * and let it set until the delay is over.  This keeps it from dying
        !          11061:        * entirely and avoids getting a bogus dead command back through the
        !          11062:        * mid-level code due to too many retries.
        !          11063:        */
        !          11064: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,132)
        !          11065:       if ( flags & SCSI_RESET_SYNCHRONOUS )
        !          11066:       {
        !          11067:         cmd->result = DID_BUS_BUSY << 16;
        !          11068:         cmd->done(cmd);
        !          11069:       }
        !          11070: #endif
1.1       root     11071:       p->flags &= ~AHC_IN_RESET;
1.1.1.2 ! root     11072:       /*
        !          11073:        * We can't rely on run_waiting_queues to unpause the sequencer for
        !          11074:        * PCI based controllers since we use AAP.  NOTE: this also sets
        !          11075:        * the timer for the one command we might have queued in the case
        !          11076:        * of a synch reset.
        !          11077:        */
1.1       root     11078:       aic7xxx_run_waiting_queues(p);
                   11079:       unpause_sequencer(p, FALSE);
                   11080:       DRIVER_UNLOCK
                   11081:       return(result);
                   11082:       break;
                   11083:   }
                   11084: }
                   11085: 
                   11086: /*+F*************************************************************************
                   11087:  * Function:
                   11088:  *   aic7xxx_biosparam
                   11089:  *
                   11090:  * Description:
                   11091:  *   Return the disk geometry for the given SCSI device.
                   11092:  *-F*************************************************************************/
                   11093: int
                   11094: aic7xxx_biosparam(Disk *disk, kdev_t dev, int geom[])
                   11095: {
1.1.1.2 ! root     11096:   int heads, sectors, cylinders, ret;
1.1       root     11097:   struct aic7xxx_host *p;
1.1.1.2 ! root     11098:   struct buffer_head *bh;
1.1       root     11099: 
                   11100:   p = (struct aic7xxx_host *) disk->device->host->hostdata;
1.1.1.2 ! root     11101:   bh = bread(MKDEV(MAJOR(dev), MINOR(dev)&~0xf), 0, 1024);
        !          11102: 
        !          11103:   if ( bh )
        !          11104:   {
        !          11105:     ret = scsi_partsize(bh, disk->capacity, &geom[2], &geom[0], &geom[1]);
        !          11106:     brelse(bh);
        !          11107:     if ( ret != -1 )
        !          11108:       return(ret);
        !          11109:   }
1.1       root     11110: 
                   11111:   heads = 64;
                   11112:   sectors = 32;
                   11113:   cylinders = disk->capacity / (heads * sectors);
                   11114: 
                   11115:   if ((p->flags & AHC_EXTEND_TRANS_A) && (cylinders > 1024))
                   11116:   {
                   11117:     heads = 255;
                   11118:     sectors = 63;
                   11119:     cylinders = disk->capacity / (heads * sectors);
                   11120:   }
                   11121: 
                   11122:   geom[0] = heads;
                   11123:   geom[1] = sectors;
                   11124:   geom[2] = cylinders;
                   11125: 
                   11126:   return (0);
                   11127: }
                   11128: 
                   11129: /*+F*************************************************************************
                   11130:  * Function:
                   11131:  *   aic7xxx_release
                   11132:  *
                   11133:  * Description:
                   11134:  *   Free the passed in Scsi_Host memory structures prior to unloading the
                   11135:  *   module.
                   11136:  *-F*************************************************************************/
                   11137: int
                   11138: aic7xxx_release(struct Scsi_Host *host)
                   11139: {
                   11140:   struct aic7xxx_host *p = (struct aic7xxx_host *) host->hostdata;
                   11141:   struct aic7xxx_host *next, *prev;
                   11142: 
                   11143:   if(p->irq)
                   11144:     free_irq(p->irq, p);
                   11145:   release_region(p->base, MAXREG - MINREG);
                   11146: #ifdef MMAPIO
                   11147:   if(p->maddr)
                   11148:   {
                   11149: #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
                   11150:     vfree((void *) (((unsigned long) p->maddr) & PAGE_MASK));
                   11151: #else
                   11152:     iounmap((void *) (((unsigned long) p->maddr) & PAGE_MASK));
                   11153: #endif
                   11154:   }
                   11155: #endif /* MMAPIO */
                   11156:   prev = NULL;
                   11157:   next = first_aic7xxx;
                   11158:   while(next != NULL)
                   11159:   {
                   11160:     if(next == p)
                   11161:     {
                   11162:       if(prev == NULL)
                   11163:         first_aic7xxx = next->next;
                   11164:       else
                   11165:         prev->next = next->next;
                   11166:     }
                   11167:     else
                   11168:     {
                   11169:       prev = next;
                   11170:     }
                   11171:     next = next->next;
                   11172:   }
                   11173:   aic7xxx_free(p);
                   11174:   return(0);
                   11175: }
                   11176: 
                   11177: /*+F*************************************************************************
                   11178:  * Function:
                   11179:  *   aic7xxx_print_card
                   11180:  *
                   11181:  * Description:
                   11182:  *   Print out all of the control registers on the card
                   11183:  *
                   11184:  *   NOTE: This function is not yet safe for use on the VLB and EISA
                   11185:  *   controllers, so it isn't used on those controllers at all.
                   11186:  *-F*************************************************************************/
                   11187: static void
                   11188: aic7xxx_print_card(struct aic7xxx_host *p)
                   11189: {
                   11190:   int i, j, k, chip;
                   11191:   static struct register_ranges {
                   11192:     int num_ranges;
                   11193:     int range_val[32];
                   11194:   } cards_ds[] = {
                   11195:     { 0, {0,} }, /* none */
                   11196:     {10, {0x00, 0x05, 0x08, 0x11, 0x18, 0x19, 0x1f, 0x1f, 0x60, 0x60, /*7771*/
                   11197:           0x62, 0x66, 0x80, 0x8e, 0x90, 0x95, 0x97, 0x97, 0x9b, 0x9f} },
                   11198:     { 9, {0x00, 0x05, 0x08, 0x11, 0x18, 0x1f, 0x60, 0x60, 0x62, 0x66, /*7850*/
                   11199:           0x80, 0x8e, 0x90, 0x95, 0x97, 0x97, 0x9a, 0x9f} },
                   11200:     { 9, {0x00, 0x05, 0x08, 0x11, 0x18, 0x1f, 0x60, 0x60, 0x62, 0x66, /*7860*/
                   11201:           0x80, 0x8e, 0x90, 0x95, 0x97, 0x97, 0x9a, 0x9f} },
                   11202:     {10, {0x00, 0x05, 0x08, 0x11, 0x18, 0x19, 0x1c, 0x1f, 0x60, 0x60, /*7870*/
                   11203:           0x62, 0x66, 0x80, 0x8e, 0x90, 0x95, 0x97, 0x97, 0x9a, 0x9f} },
                   11204:     {10, {0x00, 0x05, 0x08, 0x11, 0x18, 0x1a, 0x1c, 0x1f, 0x60, 0x60, /*7880*/
                   11205:           0x62, 0x66, 0x80, 0x8e, 0x90, 0x95, 0x97, 0x97, 0x9a, 0x9f} },
                   11206:     {16, {0x00, 0x05, 0x08, 0x11, 0x18, 0x1f, 0x60, 0x60, 0x62, 0x66, /*7890*/
                   11207:           0x84, 0x8e, 0x90, 0x95, 0x97, 0x97, 0x9a, 0x9a, 0x9f, 0x9f,
                   11208:           0xe0, 0xf1, 0xf4, 0xf4, 0xf6, 0xf6, 0xf8, 0xf8, 0xfa, 0xfc,
                   11209:           0xfe, 0xff} },
                   11210:     {12, {0x00, 0x05, 0x08, 0x11, 0x18, 0x19, 0x1b, 0x1f, 0x60, 0x60, /*7895*/
                   11211:           0x62, 0x66, 0x80, 0x8e, 0x90, 0x95, 0x97, 0x97, 0x9a, 0x9a,
                   11212:           0x9f, 0x9f, 0xe0, 0xf1} },
                   11213:     {16, {0x00, 0x05, 0x08, 0x11, 0x18, 0x1f, 0x60, 0x60, 0x62, 0x66, /*7896*/
                   11214:           0x84, 0x8e, 0x90, 0x95, 0x97, 0x97, 0x9a, 0x9a, 0x9f, 0x9f,
                   11215:           0xe0, 0xf1, 0xf4, 0xf4, 0xf6, 0xf6, 0xf8, 0xf8, 0xfa, 0xfc,
                   11216:           0xfe, 0xff} },
                   11217:   };
                   11218: #ifdef CONFIG_PCI
                   11219:   static struct register_ranges cards_ns[] = {
                   11220:     { 0, {0,} }, /* none */
                   11221:     { 0, {0,} }, /* 7771 */
                   11222:     { 7, {0x04, 0x08, 0x0c, 0x0e, 0x10, 0x17, 0x28, 0x2b, 0x30, 0x33,
                   11223:           0x3c, 0x41, 0x43, 0x47} },
                   11224:     { 7, {0x04, 0x08, 0x0c, 0x0e, 0x10, 0x17, 0x28, 0x2b, 0x30, 0x33,
                   11225:           0x3c, 0x41, 0x43, 0x47} },
                   11226:     { 5, {0x04, 0x08, 0x0c, 0x0e, 0x10, 0x17, 0x30, 0x33, 0x3c, 0x41} },
                   11227:     { 5, {0x04, 0x08, 0x0c, 0x0e, 0x10, 0x17, 0x30, 0x34, 0x3c, 0x47} },
                   11228:     { 5, {0x04, 0x08, 0x0c, 0x1b, 0x30, 0x34, 0x3c, 0x43, 0xdc, 0xe3} },
                   11229:     { 6, {0x04, 0x08, 0x0c, 0x0e, 0x10, 0x17, 0x30, 0x34, 0x3c, 0x47,
                   11230:           0xdc, 0xe3} },
                   11231:     { 6, {0x04, 0x08, 0x0c, 0x1b, 0x30, 0x34, 0x3c, 0x43, 0xdc, 0xe3,
                   11232:           0xff, 0xff} }
                   11233:   };
                   11234: #endif
                   11235:   chip = p->chip & AHC_CHIPID_MASK;
                   11236:   /*
                   11237:    * Let's run through the PCI space first....
                   11238:    */
                   11239:   printk("%s at ",
                   11240:          board_names[p->board_name_index]);
                   11241:   switch(p->chip & ~AHC_CHIPID_MASK)
                   11242:   {
                   11243:     case AHC_VL:
                   11244:       printk("VLB Slot %d.\n", p->pci_device_fn);
                   11245:       break;
                   11246:     case AHC_EISA:
                   11247:       printk("EISA Slot %d.\n", p->pci_device_fn);
                   11248:       break;
                   11249:     case AHC_PCI:
                   11250:     default:
                   11251:       printk("PCI %d/%d.\n", PCI_SLOT(p->pci_device_fn),
                   11252:              PCI_FUNC(p->pci_device_fn));
                   11253:       break;
                   11254:   }
                   11255: 
                   11256: #ifdef CONFIG_PCI
                   11257:   {
                   11258:     unsigned char temp;
1.1.1.2 ! root     11259: 
1.1       root     11260:     printk("PCI Dump:\n");
                   11261:     k=0;
                   11262:     for(i=0; i<cards_ns[chip].num_ranges; i++)
                   11263:     {
                   11264:       for(j  = cards_ns[chip].range_val[ i * 2 ];
                   11265:           j <= cards_ns[chip].range_val[ i * 2 + 1 ] ;
                   11266:           j++)
                   11267:       {
                   11268: #if LINUX_VERSION_CODE > KERNEL_VERSION(2,1,92)
                   11269:         pci_read_config_byte(p->pdev, j, &temp);
                   11270: #else
                   11271:         pcibios_read_config_byte(p->pci_bus, p->pci_device_fn, j, &temp);
                   11272: #endif
                   11273:         printk("%02x:%02x ", j, temp);
                   11274:         if(++k == 13)
                   11275:         {
                   11276:           printk("\n");
                   11277:           k = 0;
                   11278:         }
                   11279:       }
                   11280:     }
                   11281:   }
                   11282:   if(k != 0)
                   11283:     printk("\n");
                   11284: #endif /* CONFIG_PCI */
                   11285: 
                   11286:   /*
                   11287:    * Now the registers on the card....
                   11288:    */
                   11289:   printk("Card Dump:\n");
                   11290:   k = 0;
                   11291:   for(i=0; i<cards_ds[chip].num_ranges; i++)
                   11292:   {
                   11293:     for(j  = cards_ds[chip].range_val[ i * 2 ];
                   11294:         j <= cards_ds[chip].range_val[ i * 2 + 1 ] ;
                   11295:         j++)
                   11296:     {
                   11297:       printk("%02x:%02x ", j, aic_inb(p, j));
                   11298:       if(++k == 13)
                   11299:       {
                   11300:         printk("\n");
                   11301:         k=0;
                   11302:       }
                   11303:     }
                   11304:   }
                   11305:   if(k != 0)
                   11306:     printk("\n");
                   11307:   if (p->flags & AHC_SEEPROM_FOUND)
                   11308:   {
                   11309:     unsigned short *sc1;
                   11310:     sc1 = (unsigned short *)&p->sc;
1.1.1.2 ! root     11311: 
1.1       root     11312:     printk("SEEPROM dump.\n");
                   11313:     for(i=1; i<=32; i++)
                   11314:     {
                   11315:       printk("0x%04x", sc1[i-1]);
                   11316:       if ( (i % 8) == 0 )
                   11317:         printk("\n");
                   11318:       else
                   11319:         printk("  ");
                   11320:     }
                   11321:   }
                   11322: 
                   11323:   /*
                   11324:    * If this was an Ultra2 controller, then we just hosed the card in terms
                   11325:    * of the QUEUE REGS.  This function is only called at init time or by
                   11326:    * the panic_abort function, so it's safe to assume a generic init time
                   11327:    * setting here
                   11328:    */
                   11329: 
                   11330:   if(p->features & AHC_QUEUE_REGS)
                   11331:   {
                   11332:     aic_outb(p, 0, SDSCB_QOFF);
                   11333:     aic_outb(p, 0, SNSCB_QOFF);
                   11334:     aic_outb(p, 0, HNSCB_QOFF);
                   11335:   }
                   11336: 
                   11337: }
                   11338: 
                   11339: /*+F*************************************************************************
                   11340:  * Function:
                   11341:  *   aic7xxx_print_scratch_ram
                   11342:  *
                   11343:  * Description:
                   11344:  *   Print out the scratch RAM values on the card.
                   11345:  *-F*************************************************************************/
                   11346: static void
                   11347: aic7xxx_print_scratch_ram(struct aic7xxx_host *p)
                   11348: {
                   11349:   int i, k;
                   11350: 
                   11351:   k = 0;
                   11352:   printk("Scratch RAM:\n");
                   11353:   for(i = SRAM_BASE; i < SEQCTL; i++)
                   11354:   {
                   11355:     printk("%02x:%02x ", i, aic_inb(p, i));
                   11356:     if(++k == 13)
                   11357:     {
                   11358:       printk("\n");
                   11359:       k=0;
                   11360:     }
                   11361:   }
                   11362:   if (p->features & AHC_MORE_SRAM)
                   11363:   {
                   11364:     for(i = TARG_OFFSET; i < 0x80; i++)
                   11365:     {
                   11366:       printk("%02x:%02x ", i, aic_inb(p, i));
                   11367:       if(++k == 13)
                   11368:       {
                   11369:         printk("\n");
                   11370:         k=0;
                   11371:       }
                   11372:     }
                   11373:   }
                   11374:   printk("\n");
                   11375: }
                   11376: 
                   11377: 
                   11378: #include "aic7xxx_proc.c"
                   11379: 
                   11380: #ifdef MODULE
                   11381: /* Eventually this will go into an include file, but this will be later */
                   11382: Scsi_Host_Template driver_template = AIC7XXX;
                   11383: 
                   11384: #include "scsi_module.c"
                   11385: #endif
                   11386: 
                   11387: /*
                   11388:  * Overrides for Emacs so that we almost follow Linus's tabbing style.
                   11389:  * Emacs will notice this stuff at the end of the file and automatically
                   11390:  * adjust the settings for this buffer only.  This must remain at the end
                   11391:  * of the file.
                   11392:  * ---------------------------------------------------------------------------
                   11393:  * Local variables:
                   11394:  * c-indent-level: 2
                   11395:  * c-brace-imaginary-offset: 0
                   11396:  * c-brace-offset: -2
                   11397:  * c-argdecl-indent: 2
                   11398:  * c-label-offset: -2
                   11399:  * c-continued-statement-offset: 2
                   11400:  * c-continued-brace-offset: 0
                   11401:  * indent-tabs-mode: nil
                   11402:  * tab-width: 8
                   11403:  * End:
                   11404:  */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.