xref: /OK3568_Linux_fs/kernel/drivers/scsi/aic7xxx/aic7xxx_osm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Adaptec AIC7xxx device driver for Linux.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.c#235 $
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (c) 1994 John Aycock
7*4882a593Smuzhiyun  *   The University of Calgary Department of Computer Science.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
11*4882a593Smuzhiyun  * the Free Software Foundation; either version 2, or (at your option)
12*4882a593Smuzhiyun  * any later version.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful,
15*4882a593Smuzhiyun  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*4882a593Smuzhiyun  * GNU General Public License for more details.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
20*4882a593Smuzhiyun  * along with this program; see the file COPYING.  If not, write to
21*4882a593Smuzhiyun  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Sources include the Adaptec 1740 driver (aha1740.c), the Ultrastor 24F
24*4882a593Smuzhiyun  * driver (ultrastor.c), various Linux kernel source, the Adaptec EISA
25*4882a593Smuzhiyun  * config file (!adp7771.cfg), the Adaptec AHA-2740A Series User's Guide,
26*4882a593Smuzhiyun  * the Linux Kernel Hacker's Guide, Writing a SCSI Device Driver for Linux,
27*4882a593Smuzhiyun  * the Adaptec 1542 driver (aha1542.c), the Adaptec EISA overlay file
28*4882a593Smuzhiyun  * (adp7770.ovl), the Adaptec AHA-2740 Series Technical Reference Manual,
29*4882a593Smuzhiyun  * the Adaptec AIC-7770 Data Book, the ANSI SCSI specification, the
30*4882a593Smuzhiyun  * ANSI SCSI-2 specification (draft 10c), ...
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * --------------------------------------------------------------------------
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  *  Modifications by Daniel M. Eischen (deischen@iworks.InterWorks.org):
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  *  Substantially modified to include support for wide and twin bus
37*4882a593Smuzhiyun  *  adapters, DMAing of SCBs, tagged queueing, IRQ sharing, bug fixes,
38*4882a593Smuzhiyun  *  SCB paging, and other rework of the code.
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * --------------------------------------------------------------------------
41*4882a593Smuzhiyun  * Copyright (c) 1994-2000 Justin T. Gibbs.
42*4882a593Smuzhiyun  * Copyright (c) 2000-2001 Adaptec Inc.
43*4882a593Smuzhiyun  * All rights reserved.
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
46*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
47*4882a593Smuzhiyun  * are met:
48*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
49*4882a593Smuzhiyun  *    notice, this list of conditions, and the following disclaimer,
50*4882a593Smuzhiyun  *    without modification.
51*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
52*4882a593Smuzhiyun  *    substantially similar to the "NO WARRANTY" disclaimer below
53*4882a593Smuzhiyun  *    ("Disclaimer") and any redistribution must be conditioned upon
54*4882a593Smuzhiyun  *    including a substantially similar Disclaimer requirement for further
55*4882a593Smuzhiyun  *    binary redistribution.
56*4882a593Smuzhiyun  * 3. Neither the names of the above-listed copyright holders nor the names
57*4882a593Smuzhiyun  *    of any contributors may be used to endorse or promote products derived
58*4882a593Smuzhiyun  *    from this software without specific prior written permission.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * Alternatively, this software may be distributed under the terms of the
61*4882a593Smuzhiyun  * GNU General Public License ("GPL") version 2 as published by the Free
62*4882a593Smuzhiyun  * Software Foundation.
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * NO WARRANTY
65*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
66*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
67*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
68*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
69*4882a593Smuzhiyun  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70*4882a593Smuzhiyun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71*4882a593Smuzhiyun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72*4882a593Smuzhiyun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
73*4882a593Smuzhiyun  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
74*4882a593Smuzhiyun  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
75*4882a593Smuzhiyun  * POSSIBILITY OF SUCH DAMAGES.
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  *---------------------------------------------------------------------------
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  *  Thanks also go to (in alphabetical order) the following:
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  *    Rory Bolt     - Sequencer bug fixes
82*4882a593Smuzhiyun  *    Jay Estabrook - Initial DEC Alpha support
83*4882a593Smuzhiyun  *    Doug Ledford  - Much needed abort/reset bug fixes
84*4882a593Smuzhiyun  *    Kai Makisara  - DMAing of SCBs
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  *  A Boot time option was also added for not resetting the scsi bus.
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  *    Form:  aic7xxx=extended
89*4882a593Smuzhiyun  *           aic7xxx=no_reset
90*4882a593Smuzhiyun  *           aic7xxx=verbose
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  *  Daniel M. Eischen, deischen@iworks.InterWorks.org, 1/23/97
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  *  Id: aic7xxx.c,v 4.1 1997/06/12 08:23:42 deang Exp
95*4882a593Smuzhiyun  */
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun  * Further driver modifications made by Doug Ledford <dledford@redhat.com>
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  * Copyright (c) 1997-1999 Doug Ledford
101*4882a593Smuzhiyun  *
102*4882a593Smuzhiyun  * These changes are released under the same licensing terms as the FreeBSD
103*4882a593Smuzhiyun  * driver written by Justin Gibbs.  Please see his Copyright notice above
104*4882a593Smuzhiyun  * for the exact terms and conditions covering my changes as well as the
105*4882a593Smuzhiyun  * warranty statement.
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * Modifications made to the aic7xxx.c,v 4.1 driver from Dan Eischen include
108*4882a593Smuzhiyun  * but are not limited to:
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  *  1: Import of the latest FreeBSD sequencer code for this driver
111*4882a593Smuzhiyun  *  2: Modification of kernel code to accommodate different sequencer semantics
112*4882a593Smuzhiyun  *  3: Extensive changes throughout kernel portion of driver to improve
113*4882a593Smuzhiyun  *     abort/reset processing and error hanndling
114*4882a593Smuzhiyun  *  4: Other work contributed by various people on the Internet
115*4882a593Smuzhiyun  *  5: Changes to printk information and verbosity selection code
116*4882a593Smuzhiyun  *  6: General reliability related changes, especially in IRQ management
117*4882a593Smuzhiyun  *  7: Modifications to the default probe/attach order for supported cards
118*4882a593Smuzhiyun  *  8: SMP friendliness has been improved
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  */
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun #include "aic7xxx_osm.h"
123*4882a593Smuzhiyun #include "aic7xxx_inline.h"
124*4882a593Smuzhiyun #include <scsi/scsicam.h>
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun static struct scsi_transport_template *ahc_linux_transport_template = NULL;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun #include <linux/init.h>		/* __setup */
129*4882a593Smuzhiyun #include <linux/mm.h>		/* For fetching system memory size */
130*4882a593Smuzhiyun #include <linux/blkdev.h>		/* For block_size() */
131*4882a593Smuzhiyun #include <linux/delay.h>	/* For ssleep/msleep */
132*4882a593Smuzhiyun #include <linux/slab.h>
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun /*
136*4882a593Smuzhiyun  * Set this to the delay in seconds after SCSI bus reset.
137*4882a593Smuzhiyun  * Note, we honor this only for the initial bus reset.
138*4882a593Smuzhiyun  * The scsi error recovery code performs its own bus settle
139*4882a593Smuzhiyun  * delay handling for error recovery actions.
140*4882a593Smuzhiyun  */
141*4882a593Smuzhiyun #ifdef CONFIG_AIC7XXX_RESET_DELAY_MS
142*4882a593Smuzhiyun #define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS
143*4882a593Smuzhiyun #else
144*4882a593Smuzhiyun #define AIC7XXX_RESET_DELAY 5000
145*4882a593Smuzhiyun #endif
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun  * To change the default number of tagged transactions allowed per-device,
149*4882a593Smuzhiyun  * add a line to the lilo.conf file like:
150*4882a593Smuzhiyun  * append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
151*4882a593Smuzhiyun  * which will result in the first four devices on the first two
152*4882a593Smuzhiyun  * controllers being set to a tagged queue depth of 32.
153*4882a593Smuzhiyun  *
154*4882a593Smuzhiyun  * The tag_commands is an array of 16 to allow for wide and twin adapters.
155*4882a593Smuzhiyun  * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
156*4882a593Smuzhiyun  * for channel 1.
157*4882a593Smuzhiyun  */
158*4882a593Smuzhiyun typedef struct {
159*4882a593Smuzhiyun 	uint8_t tag_commands[16];	/* Allow for wide/twin adapters. */
160*4882a593Smuzhiyun } adapter_tag_info_t;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun /*
163*4882a593Smuzhiyun  * Modify this as you see fit for your system.
164*4882a593Smuzhiyun  *
165*4882a593Smuzhiyun  * 0			tagged queuing disabled
166*4882a593Smuzhiyun  * 1 <= n <= 253	n == max tags ever dispatched.
167*4882a593Smuzhiyun  *
168*4882a593Smuzhiyun  * The driver will throttle the number of commands dispatched to a
169*4882a593Smuzhiyun  * device if it returns queue full.  For devices with a fixed maximum
170*4882a593Smuzhiyun  * queue depth, the driver will eventually determine this depth and
171*4882a593Smuzhiyun  * lock it in (a console message is printed to indicate that a lock
172*4882a593Smuzhiyun  * has occurred).  On some devices, queue full is returned for a temporary
173*4882a593Smuzhiyun  * resource shortage.  These devices will return queue full at varying
174*4882a593Smuzhiyun  * depths.  The driver will throttle back when the queue fulls occur and
175*4882a593Smuzhiyun  * attempt to slowly increase the depth over time as the device recovers
176*4882a593Smuzhiyun  * from the resource shortage.
177*4882a593Smuzhiyun  *
178*4882a593Smuzhiyun  * In this example, the first line will disable tagged queueing for all
179*4882a593Smuzhiyun  * the devices on the first probed aic7xxx adapter.
180*4882a593Smuzhiyun  *
181*4882a593Smuzhiyun  * The second line enables tagged queueing with 4 commands/LUN for IDs
182*4882a593Smuzhiyun  * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
183*4882a593Smuzhiyun  * driver to attempt to use up to 64 tags for ID 1.
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  * The third line is the same as the first line.
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  * The fourth line disables tagged queueing for devices 0 and 3.  It
188*4882a593Smuzhiyun  * enables tagged queueing for the other IDs, with 16 commands/LUN
189*4882a593Smuzhiyun  * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
190*4882a593Smuzhiyun  * IDs 2, 5-7, and 9-15.
191*4882a593Smuzhiyun  */
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun  * NOTE: The below structure is for reference only, the actual structure
195*4882a593Smuzhiyun  *       to modify in order to change things is just below this comment block.
196*4882a593Smuzhiyun adapter_tag_info_t aic7xxx_tag_info[] =
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
199*4882a593Smuzhiyun 	{{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
200*4882a593Smuzhiyun 	{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
201*4882a593Smuzhiyun 	{{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
202*4882a593Smuzhiyun };
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun #ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
206*4882a593Smuzhiyun #define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
207*4882a593Smuzhiyun #else
208*4882a593Smuzhiyun #define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE
209*4882a593Smuzhiyun #endif
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun #define AIC7XXX_CONFIGED_TAG_COMMANDS {					\
212*4882a593Smuzhiyun 	AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,		\
213*4882a593Smuzhiyun 	AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,		\
214*4882a593Smuzhiyun 	AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,		\
215*4882a593Smuzhiyun 	AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,		\
216*4882a593Smuzhiyun 	AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,		\
217*4882a593Smuzhiyun 	AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,		\
218*4882a593Smuzhiyun 	AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,		\
219*4882a593Smuzhiyun 	AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE		\
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun /*
223*4882a593Smuzhiyun  * By default, use the number of commands specified by
224*4882a593Smuzhiyun  * the users kernel configuration.
225*4882a593Smuzhiyun  */
226*4882a593Smuzhiyun static adapter_tag_info_t aic7xxx_tag_info[] =
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
229*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
230*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
231*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
232*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
233*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
234*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
235*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
236*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
237*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
238*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
239*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
240*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
241*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
242*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS},
243*4882a593Smuzhiyun 	{AIC7XXX_CONFIGED_TAG_COMMANDS}
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /*
247*4882a593Smuzhiyun  * There should be a specific return value for this in scsi.h, but
248*4882a593Smuzhiyun  * it seems that most drivers ignore it.
249*4882a593Smuzhiyun  */
250*4882a593Smuzhiyun #define DID_UNDERFLOW   DID_ERROR
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun void
ahc_print_path(struct ahc_softc * ahc,struct scb * scb)253*4882a593Smuzhiyun ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	printk("(scsi%d:%c:%d:%d): ",
256*4882a593Smuzhiyun 	       ahc->platform_data->host->host_no,
257*4882a593Smuzhiyun 	       scb != NULL ? SCB_GET_CHANNEL(ahc, scb) : 'X',
258*4882a593Smuzhiyun 	       scb != NULL ? SCB_GET_TARGET(ahc, scb) : -1,
259*4882a593Smuzhiyun 	       scb != NULL ? SCB_GET_LUN(scb) : -1);
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /*
263*4882a593Smuzhiyun  * XXX - these options apply unilaterally to _all_ 274x/284x/294x
264*4882a593Smuzhiyun  *       cards in the system.  This should be fixed.  Exceptions to this
265*4882a593Smuzhiyun  *       rule are noted in the comments.
266*4882a593Smuzhiyun  */
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun /*
269*4882a593Smuzhiyun  * Skip the scsi bus reset.  Non 0 make us skip the reset at startup.  This
270*4882a593Smuzhiyun  * has no effect on any later resets that might occur due to things like
271*4882a593Smuzhiyun  * SCSI bus timeouts.
272*4882a593Smuzhiyun  */
273*4882a593Smuzhiyun static uint32_t aic7xxx_no_reset;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun /*
276*4882a593Smuzhiyun  * Should we force EXTENDED translation on a controller.
277*4882a593Smuzhiyun  *     0 == Use whatever is in the SEEPROM or default to off
278*4882a593Smuzhiyun  *     1 == Use whatever is in the SEEPROM or default to on
279*4882a593Smuzhiyun  */
280*4882a593Smuzhiyun static uint32_t aic7xxx_extended;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun /*
283*4882a593Smuzhiyun  * PCI bus parity checking of the Adaptec controllers.  This is somewhat
284*4882a593Smuzhiyun  * dubious at best.  To my knowledge, this option has never actually
285*4882a593Smuzhiyun  * solved a PCI parity problem, but on certain machines with broken PCI
286*4882a593Smuzhiyun  * chipset configurations where stray PCI transactions with bad parity are
287*4882a593Smuzhiyun  * the norm rather than the exception, the error messages can be overwhelming.
288*4882a593Smuzhiyun  * It's included in the driver for completeness.
289*4882a593Smuzhiyun  *   0	   = Shut off PCI parity check
290*4882a593Smuzhiyun  *   non-0 = reverse polarity pci parity checking
291*4882a593Smuzhiyun  */
292*4882a593Smuzhiyun static uint32_t aic7xxx_pci_parity = ~0;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun  * There are lots of broken chipsets in the world.  Some of them will
296*4882a593Smuzhiyun  * violate the PCI spec when we issue byte sized memory writes to our
297*4882a593Smuzhiyun  * controller.  I/O mapped register access, if allowed by the given
298*4882a593Smuzhiyun  * platform, will work in almost all cases.
299*4882a593Smuzhiyun  */
300*4882a593Smuzhiyun uint32_t aic7xxx_allow_memio = ~0;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun /*
303*4882a593Smuzhiyun  * So that we can set how long each device is given as a selection timeout.
304*4882a593Smuzhiyun  * The table of values goes like this:
305*4882a593Smuzhiyun  *   0 - 256ms
306*4882a593Smuzhiyun  *   1 - 128ms
307*4882a593Smuzhiyun  *   2 - 64ms
308*4882a593Smuzhiyun  *   3 - 32ms
309*4882a593Smuzhiyun  * We default to 256ms because some older devices need a longer time
310*4882a593Smuzhiyun  * to respond to initial selection.
311*4882a593Smuzhiyun  */
312*4882a593Smuzhiyun static uint32_t aic7xxx_seltime;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun /*
315*4882a593Smuzhiyun  * Certain devices do not perform any aging on commands.  Should the
316*4882a593Smuzhiyun  * device be saturated by commands in one portion of the disk, it is
317*4882a593Smuzhiyun  * possible for transactions on far away sectors to never be serviced.
318*4882a593Smuzhiyun  * To handle these devices, we can periodically send an ordered tag to
319*4882a593Smuzhiyun  * force all outstanding transactions to be serviced prior to a new
320*4882a593Smuzhiyun  * transaction.
321*4882a593Smuzhiyun  */
322*4882a593Smuzhiyun static uint32_t aic7xxx_periodic_otag;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun /*
325*4882a593Smuzhiyun  * Module information and settable options.
326*4882a593Smuzhiyun  */
327*4882a593Smuzhiyun static char *aic7xxx = NULL;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun MODULE_AUTHOR("Maintainer: Hannes Reinecke <hare@suse.de>");
330*4882a593Smuzhiyun MODULE_DESCRIPTION("Adaptec AIC77XX/78XX SCSI Host Bus Adapter driver");
331*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
332*4882a593Smuzhiyun MODULE_VERSION(AIC7XXX_DRIVER_VERSION);
333*4882a593Smuzhiyun module_param(aic7xxx, charp, 0444);
334*4882a593Smuzhiyun MODULE_PARM_DESC(aic7xxx,
335*4882a593Smuzhiyun "period-delimited options string:\n"
336*4882a593Smuzhiyun "	verbose			Enable verbose/diagnostic logging\n"
337*4882a593Smuzhiyun "	allow_memio		Allow device registers to be memory mapped\n"
338*4882a593Smuzhiyun "	debug			Bitmask of debug values to enable\n"
339*4882a593Smuzhiyun "	no_probe		Toggle EISA/VLB controller probing\n"
340*4882a593Smuzhiyun "	probe_eisa_vl		Toggle EISA/VLB controller probing\n"
341*4882a593Smuzhiyun "	no_reset		Suppress initial bus resets\n"
342*4882a593Smuzhiyun "	extended		Enable extended geometry on all controllers\n"
343*4882a593Smuzhiyun "	periodic_otag		Send an ordered tagged transaction\n"
344*4882a593Smuzhiyun "				periodically to prevent tag starvation.\n"
345*4882a593Smuzhiyun "				This may be required by some older disk\n"
346*4882a593Smuzhiyun "				drives or RAID arrays.\n"
347*4882a593Smuzhiyun "	tag_info:<tag_str>	Set per-target tag depth\n"
348*4882a593Smuzhiyun "	global_tag_depth:<int>	Global tag depth for every target\n"
349*4882a593Smuzhiyun "				on every bus\n"
350*4882a593Smuzhiyun "	seltime:<int>		Selection Timeout\n"
351*4882a593Smuzhiyun "				(0/256ms,1/128ms,2/64ms,3/32ms)\n"
352*4882a593Smuzhiyun "\n"
353*4882a593Smuzhiyun "	Sample modprobe configuration file:\n"
354*4882a593Smuzhiyun "	#	Toggle EISA/VLB probing\n"
355*4882a593Smuzhiyun "	#	Set tag depth on Controller 1/Target 1 to 10 tags\n"
356*4882a593Smuzhiyun "	#	Shorten the selection timeout to 128ms\n"
357*4882a593Smuzhiyun "\n"
358*4882a593Smuzhiyun "	options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n"
359*4882a593Smuzhiyun );
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun static void ahc_linux_handle_scsi_status(struct ahc_softc *,
362*4882a593Smuzhiyun 					 struct scsi_device *,
363*4882a593Smuzhiyun 					 struct scb *);
364*4882a593Smuzhiyun static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
365*4882a593Smuzhiyun 					 struct scsi_cmnd *cmd);
366*4882a593Smuzhiyun static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
367*4882a593Smuzhiyun static void ahc_linux_release_simq(struct ahc_softc *ahc);
368*4882a593Smuzhiyun static int  ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
369*4882a593Smuzhiyun static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
370*4882a593Smuzhiyun static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
371*4882a593Smuzhiyun 				     struct ahc_devinfo *devinfo);
372*4882a593Smuzhiyun static void ahc_linux_device_queue_depth(struct scsi_device *);
373*4882a593Smuzhiyun static int ahc_linux_run_command(struct ahc_softc*,
374*4882a593Smuzhiyun 				 struct ahc_linux_device *,
375*4882a593Smuzhiyun 				 struct scsi_cmnd *);
376*4882a593Smuzhiyun static void ahc_linux_setup_tag_info_global(char *p);
377*4882a593Smuzhiyun static int  aic7xxx_setup(char *s);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun static int ahc_linux_unit;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun /************************** OS Utility Wrappers *******************************/
383*4882a593Smuzhiyun void
ahc_delay(long usec)384*4882a593Smuzhiyun ahc_delay(long usec)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	/*
387*4882a593Smuzhiyun 	 * udelay on Linux can have problems for
388*4882a593Smuzhiyun 	 * multi-millisecond waits.  Wait at most
389*4882a593Smuzhiyun 	 * 1024us per call.
390*4882a593Smuzhiyun 	 */
391*4882a593Smuzhiyun 	while (usec > 0) {
392*4882a593Smuzhiyun 		udelay(usec % 1024);
393*4882a593Smuzhiyun 		usec -= 1024;
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun /***************************** Low Level I/O **********************************/
398*4882a593Smuzhiyun uint8_t
ahc_inb(struct ahc_softc * ahc,long port)399*4882a593Smuzhiyun ahc_inb(struct ahc_softc * ahc, long port)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	uint8_t x;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	if (ahc->tag == BUS_SPACE_MEMIO) {
404*4882a593Smuzhiyun 		x = readb(ahc->bsh.maddr + port);
405*4882a593Smuzhiyun 	} else {
406*4882a593Smuzhiyun 		x = inb(ahc->bsh.ioport + port);
407*4882a593Smuzhiyun 	}
408*4882a593Smuzhiyun 	mb();
409*4882a593Smuzhiyun 	return (x);
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun void
ahc_outb(struct ahc_softc * ahc,long port,uint8_t val)413*4882a593Smuzhiyun ahc_outb(struct ahc_softc * ahc, long port, uint8_t val)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	if (ahc->tag == BUS_SPACE_MEMIO) {
416*4882a593Smuzhiyun 		writeb(val, ahc->bsh.maddr + port);
417*4882a593Smuzhiyun 	} else {
418*4882a593Smuzhiyun 		outb(val, ahc->bsh.ioport + port);
419*4882a593Smuzhiyun 	}
420*4882a593Smuzhiyun 	mb();
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun void
ahc_outsb(struct ahc_softc * ahc,long port,uint8_t * array,int count)424*4882a593Smuzhiyun ahc_outsb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	int i;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	/*
429*4882a593Smuzhiyun 	 * There is probably a more efficient way to do this on Linux
430*4882a593Smuzhiyun 	 * but we don't use this for anything speed critical and this
431*4882a593Smuzhiyun 	 * should work.
432*4882a593Smuzhiyun 	 */
433*4882a593Smuzhiyun 	for (i = 0; i < count; i++)
434*4882a593Smuzhiyun 		ahc_outb(ahc, port, *array++);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun void
ahc_insb(struct ahc_softc * ahc,long port,uint8_t * array,int count)438*4882a593Smuzhiyun ahc_insb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
439*4882a593Smuzhiyun {
440*4882a593Smuzhiyun 	int i;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	/*
443*4882a593Smuzhiyun 	 * There is probably a more efficient way to do this on Linux
444*4882a593Smuzhiyun 	 * but we don't use this for anything speed critical and this
445*4882a593Smuzhiyun 	 * should work.
446*4882a593Smuzhiyun 	 */
447*4882a593Smuzhiyun 	for (i = 0; i < count; i++)
448*4882a593Smuzhiyun 		*array++ = ahc_inb(ahc, port);
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun /********************************* Inlines ************************************/
452*4882a593Smuzhiyun static void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun static int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
455*4882a593Smuzhiyun 		 		      struct ahc_dma_seg *sg,
456*4882a593Smuzhiyun 				      dma_addr_t addr, bus_size_t len);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun static void
ahc_linux_unmap_scb(struct ahc_softc * ahc,struct scb * scb)459*4882a593Smuzhiyun ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
460*4882a593Smuzhiyun {
461*4882a593Smuzhiyun 	struct scsi_cmnd *cmd;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	cmd = scb->io_ctx;
464*4882a593Smuzhiyun 	ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	scsi_dma_unmap(cmd);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun static int
ahc_linux_map_seg(struct ahc_softc * ahc,struct scb * scb,struct ahc_dma_seg * sg,dma_addr_t addr,bus_size_t len)470*4882a593Smuzhiyun ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
471*4882a593Smuzhiyun 		  struct ahc_dma_seg *sg, dma_addr_t addr, bus_size_t len)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	int	 consumed;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	if ((scb->sg_count + 1) > AHC_NSEG)
476*4882a593Smuzhiyun 		panic("Too few segs for dma mapping.  "
477*4882a593Smuzhiyun 		      "Increase AHC_NSEG\n");
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	consumed = 1;
480*4882a593Smuzhiyun 	sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
481*4882a593Smuzhiyun 	scb->platform_data->xfer_len += len;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	if (sizeof(dma_addr_t) > 4
484*4882a593Smuzhiyun 	 && (ahc->flags & AHC_39BIT_ADDRESSING) != 0)
485*4882a593Smuzhiyun 		len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	sg->len = ahc_htole32(len);
488*4882a593Smuzhiyun 	return (consumed);
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun /*
492*4882a593Smuzhiyun  * Return a string describing the driver.
493*4882a593Smuzhiyun  */
494*4882a593Smuzhiyun static const char *
ahc_linux_info(struct Scsi_Host * host)495*4882a593Smuzhiyun ahc_linux_info(struct Scsi_Host *host)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun 	static char buffer[512];
498*4882a593Smuzhiyun 	char	ahc_info[256];
499*4882a593Smuzhiyun 	char   *bp;
500*4882a593Smuzhiyun 	struct ahc_softc *ahc;
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	bp = &buffer[0];
503*4882a593Smuzhiyun 	ahc = *(struct ahc_softc **)host->hostdata;
504*4882a593Smuzhiyun 	memset(bp, 0, sizeof(buffer));
505*4882a593Smuzhiyun 	strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev " AIC7XXX_DRIVER_VERSION "\n"
506*4882a593Smuzhiyun 			"        <");
507*4882a593Smuzhiyun 	strcat(bp, ahc->description);
508*4882a593Smuzhiyun 	strcat(bp, ">\n"
509*4882a593Smuzhiyun 			"        ");
510*4882a593Smuzhiyun 	ahc_controller_info(ahc, ahc_info);
511*4882a593Smuzhiyun 	strcat(bp, ahc_info);
512*4882a593Smuzhiyun 	strcat(bp, "\n");
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	return (bp);
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun /*
518*4882a593Smuzhiyun  * Queue an SCB to the controller.
519*4882a593Smuzhiyun  */
520*4882a593Smuzhiyun static int
ahc_linux_queue_lck(struct scsi_cmnd * cmd,void (* scsi_done)(struct scsi_cmnd *))521*4882a593Smuzhiyun ahc_linux_queue_lck(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	struct	 ahc_softc *ahc;
524*4882a593Smuzhiyun 	struct	 ahc_linux_device *dev = scsi_transport_device_data(cmd->device);
525*4882a593Smuzhiyun 	int rtn = SCSI_MLQUEUE_HOST_BUSY;
526*4882a593Smuzhiyun 	unsigned long flags;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
531*4882a593Smuzhiyun 	if (ahc->platform_data->qfrozen == 0) {
532*4882a593Smuzhiyun 		cmd->scsi_done = scsi_done;
533*4882a593Smuzhiyun 		cmd->result = CAM_REQ_INPROG << 16;
534*4882a593Smuzhiyun 		rtn = ahc_linux_run_command(ahc, dev, cmd);
535*4882a593Smuzhiyun 	}
536*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	return rtn;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun 
DEF_SCSI_QCMD(ahc_linux_queue)541*4882a593Smuzhiyun static DEF_SCSI_QCMD(ahc_linux_queue)
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun static inline struct scsi_target **
544*4882a593Smuzhiyun ahc_linux_target_in_softc(struct scsi_target *starget)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	struct	ahc_softc *ahc =
547*4882a593Smuzhiyun 		*((struct ahc_softc **)dev_to_shost(&starget->dev)->hostdata);
548*4882a593Smuzhiyun 	unsigned int target_offset;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	target_offset = starget->id;
551*4882a593Smuzhiyun 	if (starget->channel != 0)
552*4882a593Smuzhiyun 		target_offset += 8;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	return &ahc->platform_data->starget[target_offset];
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun static int
ahc_linux_target_alloc(struct scsi_target * starget)558*4882a593Smuzhiyun ahc_linux_target_alloc(struct scsi_target *starget)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	struct	ahc_softc *ahc =
561*4882a593Smuzhiyun 		*((struct ahc_softc **)dev_to_shost(&starget->dev)->hostdata);
562*4882a593Smuzhiyun 	struct seeprom_config *sc = ahc->seep_config;
563*4882a593Smuzhiyun 	unsigned long flags;
564*4882a593Smuzhiyun 	struct scsi_target **ahc_targp = ahc_linux_target_in_softc(starget);
565*4882a593Smuzhiyun 	unsigned short scsirate;
566*4882a593Smuzhiyun 	struct ahc_devinfo devinfo;
567*4882a593Smuzhiyun 	char channel = starget->channel + 'A';
568*4882a593Smuzhiyun 	unsigned int our_id = ahc->our_id;
569*4882a593Smuzhiyun 	unsigned int target_offset;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	target_offset = starget->id;
572*4882a593Smuzhiyun 	if (starget->channel != 0)
573*4882a593Smuzhiyun 		target_offset += 8;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	if (starget->channel)
576*4882a593Smuzhiyun 		our_id = ahc->our_id_b;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	BUG_ON(*ahc_targp != NULL);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	*ahc_targp = starget;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	if (sc) {
585*4882a593Smuzhiyun 		int maxsync = AHC_SYNCRATE_DT;
586*4882a593Smuzhiyun 		int ultra = 0;
587*4882a593Smuzhiyun 		int flags = sc->device_flags[target_offset];
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 		if (ahc->flags & AHC_NEWEEPROM_FMT) {
590*4882a593Smuzhiyun 		    if (flags & CFSYNCHISULTRA)
591*4882a593Smuzhiyun 			ultra = 1;
592*4882a593Smuzhiyun 		} else if (flags & CFULTRAEN)
593*4882a593Smuzhiyun 			ultra = 1;
594*4882a593Smuzhiyun 		/* AIC nutcase; 10MHz appears as ultra = 1, CFXFER = 0x04
595*4882a593Smuzhiyun 		 * change it to ultra=0, CFXFER = 0 */
596*4882a593Smuzhiyun 		if(ultra && (flags & CFXFER) == 0x04) {
597*4882a593Smuzhiyun 			ultra = 0;
598*4882a593Smuzhiyun 			flags &= ~CFXFER;
599*4882a593Smuzhiyun 		}
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 		if ((ahc->features & AHC_ULTRA2) != 0) {
602*4882a593Smuzhiyun 			scsirate = (flags & CFXFER) | (ultra ? 0x8 : 0);
603*4882a593Smuzhiyun 		} else {
604*4882a593Smuzhiyun 			scsirate = (flags & CFXFER) << 4;
605*4882a593Smuzhiyun 			maxsync = ultra ? AHC_SYNCRATE_ULTRA :
606*4882a593Smuzhiyun 				AHC_SYNCRATE_FAST;
607*4882a593Smuzhiyun 		}
608*4882a593Smuzhiyun 		spi_max_width(starget) = (flags & CFWIDEB) ? 1 : 0;
609*4882a593Smuzhiyun 		if (!(flags & CFSYNCH))
610*4882a593Smuzhiyun 			spi_max_offset(starget) = 0;
611*4882a593Smuzhiyun 		spi_min_period(starget) =
612*4882a593Smuzhiyun 			ahc_find_period(ahc, scsirate, maxsync);
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 	ahc_compile_devinfo(&devinfo, our_id, starget->id,
615*4882a593Smuzhiyun 			    CAM_LUN_WILDCARD, channel,
616*4882a593Smuzhiyun 			    ROLE_INITIATOR);
617*4882a593Smuzhiyun 	ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
618*4882a593Smuzhiyun 			 AHC_TRANS_GOAL, /*paused*/FALSE);
619*4882a593Smuzhiyun 	ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
620*4882a593Smuzhiyun 		      AHC_TRANS_GOAL, /*paused*/FALSE);
621*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	return 0;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun static void
ahc_linux_target_destroy(struct scsi_target * starget)627*4882a593Smuzhiyun ahc_linux_target_destroy(struct scsi_target *starget)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	struct scsi_target **ahc_targp = ahc_linux_target_in_softc(starget);
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	*ahc_targp = NULL;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun static int
ahc_linux_slave_alloc(struct scsi_device * sdev)635*4882a593Smuzhiyun ahc_linux_slave_alloc(struct scsi_device *sdev)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	struct	ahc_softc *ahc =
638*4882a593Smuzhiyun 		*((struct ahc_softc **)sdev->host->hostdata);
639*4882a593Smuzhiyun 	struct scsi_target *starget = sdev->sdev_target;
640*4882a593Smuzhiyun 	struct ahc_linux_device *dev;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	if (bootverbose)
643*4882a593Smuzhiyun 		printk("%s: Slave Alloc %d\n", ahc_name(ahc), sdev->id);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	dev = scsi_transport_device_data(sdev);
646*4882a593Smuzhiyun 	memset(dev, 0, sizeof(*dev));
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	/*
649*4882a593Smuzhiyun 	 * We start out life using untagged
650*4882a593Smuzhiyun 	 * transactions of which we allow one.
651*4882a593Smuzhiyun 	 */
652*4882a593Smuzhiyun 	dev->openings = 1;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	/*
655*4882a593Smuzhiyun 	 * Set maxtags to 0.  This will be changed if we
656*4882a593Smuzhiyun 	 * later determine that we are dealing with
657*4882a593Smuzhiyun 	 * a tagged queuing capable device.
658*4882a593Smuzhiyun 	 */
659*4882a593Smuzhiyun 	dev->maxtags = 0;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	spi_period(starget) = 0;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	return 0;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun static int
ahc_linux_slave_configure(struct scsi_device * sdev)667*4882a593Smuzhiyun ahc_linux_slave_configure(struct scsi_device *sdev)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun 	if (bootverbose)
670*4882a593Smuzhiyun 		sdev_printk(KERN_INFO, sdev, "Slave Configure\n");
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	ahc_linux_device_queue_depth(sdev);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	/* Initial Domain Validation */
675*4882a593Smuzhiyun 	if (!spi_initial_dv(sdev->sdev_target))
676*4882a593Smuzhiyun 		spi_dv_device(sdev);
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	return 0;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun #if defined(__i386__)
682*4882a593Smuzhiyun /*
683*4882a593Smuzhiyun  * Return the disk geometry for the given SCSI device.
684*4882a593Smuzhiyun  */
685*4882a593Smuzhiyun static int
ahc_linux_biosparam(struct scsi_device * sdev,struct block_device * bdev,sector_t capacity,int geom[])686*4882a593Smuzhiyun ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
687*4882a593Smuzhiyun 		    sector_t capacity, int geom[])
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun 	int	 heads;
690*4882a593Smuzhiyun 	int	 sectors;
691*4882a593Smuzhiyun 	int	 cylinders;
692*4882a593Smuzhiyun 	int	 extended;
693*4882a593Smuzhiyun 	struct	 ahc_softc *ahc;
694*4882a593Smuzhiyun 	u_int	 channel;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	ahc = *((struct ahc_softc **)sdev->host->hostdata);
697*4882a593Smuzhiyun 	channel = sdev_channel(sdev);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	if (scsi_partsize(bdev, capacity, geom))
700*4882a593Smuzhiyun 		return 0;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	heads = 64;
703*4882a593Smuzhiyun 	sectors = 32;
704*4882a593Smuzhiyun 	cylinders = aic_sector_div(capacity, heads, sectors);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	if (aic7xxx_extended != 0)
707*4882a593Smuzhiyun 		extended = 1;
708*4882a593Smuzhiyun 	else if (channel == 0)
709*4882a593Smuzhiyun 		extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
710*4882a593Smuzhiyun 	else
711*4882a593Smuzhiyun 		extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
712*4882a593Smuzhiyun 	if (extended && cylinders >= 1024) {
713*4882a593Smuzhiyun 		heads = 255;
714*4882a593Smuzhiyun 		sectors = 63;
715*4882a593Smuzhiyun 		cylinders = aic_sector_div(capacity, heads, sectors);
716*4882a593Smuzhiyun 	}
717*4882a593Smuzhiyun 	geom[0] = heads;
718*4882a593Smuzhiyun 	geom[1] = sectors;
719*4882a593Smuzhiyun 	geom[2] = cylinders;
720*4882a593Smuzhiyun 	return (0);
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun #endif
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun /*
725*4882a593Smuzhiyun  * Abort the current SCSI command(s).
726*4882a593Smuzhiyun  */
727*4882a593Smuzhiyun static int
ahc_linux_abort(struct scsi_cmnd * cmd)728*4882a593Smuzhiyun ahc_linux_abort(struct scsi_cmnd *cmd)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun 	int error;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
733*4882a593Smuzhiyun 	if (error != SUCCESS)
734*4882a593Smuzhiyun 		printk("aic7xxx_abort returns 0x%x\n", error);
735*4882a593Smuzhiyun 	return (error);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun /*
739*4882a593Smuzhiyun  * Attempt to send a target reset message to the device that timed out.
740*4882a593Smuzhiyun  */
741*4882a593Smuzhiyun static int
ahc_linux_dev_reset(struct scsi_cmnd * cmd)742*4882a593Smuzhiyun ahc_linux_dev_reset(struct scsi_cmnd *cmd)
743*4882a593Smuzhiyun {
744*4882a593Smuzhiyun 	int error;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
747*4882a593Smuzhiyun 	if (error != SUCCESS)
748*4882a593Smuzhiyun 		printk("aic7xxx_dev_reset returns 0x%x\n", error);
749*4882a593Smuzhiyun 	return (error);
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun /*
753*4882a593Smuzhiyun  * Reset the SCSI bus.
754*4882a593Smuzhiyun  */
755*4882a593Smuzhiyun static int
ahc_linux_bus_reset(struct scsi_cmnd * cmd)756*4882a593Smuzhiyun ahc_linux_bus_reset(struct scsi_cmnd *cmd)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun 	struct ahc_softc *ahc;
759*4882a593Smuzhiyun 	int    found;
760*4882a593Smuzhiyun 	unsigned long flags;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
765*4882a593Smuzhiyun 	found = ahc_reset_channel(ahc, scmd_channel(cmd) + 'A',
766*4882a593Smuzhiyun 				  /*initiate reset*/TRUE);
767*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	if (bootverbose)
770*4882a593Smuzhiyun 		printk("%s: SCSI bus reset delivered. "
771*4882a593Smuzhiyun 		       "%d SCBs aborted.\n", ahc_name(ahc), found);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	return SUCCESS;
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun struct scsi_host_template aic7xxx_driver_template = {
777*4882a593Smuzhiyun 	.module			= THIS_MODULE,
778*4882a593Smuzhiyun 	.name			= "aic7xxx",
779*4882a593Smuzhiyun 	.proc_name		= "aic7xxx",
780*4882a593Smuzhiyun 	.show_info		= ahc_linux_show_info,
781*4882a593Smuzhiyun 	.write_info		= ahc_proc_write_seeprom,
782*4882a593Smuzhiyun 	.info			= ahc_linux_info,
783*4882a593Smuzhiyun 	.queuecommand		= ahc_linux_queue,
784*4882a593Smuzhiyun 	.eh_abort_handler	= ahc_linux_abort,
785*4882a593Smuzhiyun 	.eh_device_reset_handler = ahc_linux_dev_reset,
786*4882a593Smuzhiyun 	.eh_bus_reset_handler	= ahc_linux_bus_reset,
787*4882a593Smuzhiyun #if defined(__i386__)
788*4882a593Smuzhiyun 	.bios_param		= ahc_linux_biosparam,
789*4882a593Smuzhiyun #endif
790*4882a593Smuzhiyun 	.can_queue		= AHC_MAX_QUEUE,
791*4882a593Smuzhiyun 	.this_id		= -1,
792*4882a593Smuzhiyun 	.max_sectors		= 8192,
793*4882a593Smuzhiyun 	.cmd_per_lun		= 2,
794*4882a593Smuzhiyun 	.slave_alloc		= ahc_linux_slave_alloc,
795*4882a593Smuzhiyun 	.slave_configure	= ahc_linux_slave_configure,
796*4882a593Smuzhiyun 	.target_alloc		= ahc_linux_target_alloc,
797*4882a593Smuzhiyun 	.target_destroy		= ahc_linux_target_destroy,
798*4882a593Smuzhiyun };
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun /**************************** Tasklet Handler *********************************/
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun /******************************** Macros **************************************/
803*4882a593Smuzhiyun #define BUILD_SCSIID(ahc, cmd)						    \
804*4882a593Smuzhiyun 	((((cmd)->device->id << TID_SHIFT) & TID)			    \
805*4882a593Smuzhiyun 	| (((cmd)->device->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \
806*4882a593Smuzhiyun 	| (((cmd)->device->channel == 0) ? 0 : TWIN_CHNLB))
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun /******************************** Bus DMA *************************************/
809*4882a593Smuzhiyun int
ahc_dma_tag_create(struct ahc_softc * ahc,bus_dma_tag_t parent,bus_size_t alignment,bus_size_t boundary,dma_addr_t lowaddr,dma_addr_t highaddr,bus_dma_filter_t * filter,void * filterarg,bus_size_t maxsize,int nsegments,bus_size_t maxsegsz,int flags,bus_dma_tag_t * ret_tag)810*4882a593Smuzhiyun ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent,
811*4882a593Smuzhiyun 		   bus_size_t alignment, bus_size_t boundary,
812*4882a593Smuzhiyun 		   dma_addr_t lowaddr, dma_addr_t highaddr,
813*4882a593Smuzhiyun 		   bus_dma_filter_t *filter, void *filterarg,
814*4882a593Smuzhiyun 		   bus_size_t maxsize, int nsegments,
815*4882a593Smuzhiyun 		   bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
816*4882a593Smuzhiyun {
817*4882a593Smuzhiyun 	bus_dma_tag_t dmat;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	dmat = kmalloc(sizeof(*dmat), GFP_ATOMIC);
820*4882a593Smuzhiyun 	if (dmat == NULL)
821*4882a593Smuzhiyun 		return (ENOMEM);
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	/*
824*4882a593Smuzhiyun 	 * Linux is very simplistic about DMA memory.  For now don't
825*4882a593Smuzhiyun 	 * maintain all specification information.  Once Linux supplies
826*4882a593Smuzhiyun 	 * better facilities for doing these operations, or the
827*4882a593Smuzhiyun 	 * needs of this particular driver change, we might need to do
828*4882a593Smuzhiyun 	 * more here.
829*4882a593Smuzhiyun 	 */
830*4882a593Smuzhiyun 	dmat->alignment = alignment;
831*4882a593Smuzhiyun 	dmat->boundary = boundary;
832*4882a593Smuzhiyun 	dmat->maxsize = maxsize;
833*4882a593Smuzhiyun 	*ret_tag = dmat;
834*4882a593Smuzhiyun 	return (0);
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun void
ahc_dma_tag_destroy(struct ahc_softc * ahc,bus_dma_tag_t dmat)838*4882a593Smuzhiyun ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
839*4882a593Smuzhiyun {
840*4882a593Smuzhiyun 	kfree(dmat);
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun int
ahc_dmamem_alloc(struct ahc_softc * ahc,bus_dma_tag_t dmat,void ** vaddr,int flags,bus_dmamap_t * mapp)844*4882a593Smuzhiyun ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
845*4882a593Smuzhiyun 		 int flags, bus_dmamap_t *mapp)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun 	/* XXX: check if we really need the GFP_ATOMIC and unwind this mess! */
848*4882a593Smuzhiyun 	*vaddr = dma_alloc_coherent(ahc->dev, dmat->maxsize, mapp, GFP_ATOMIC);
849*4882a593Smuzhiyun 	if (*vaddr == NULL)
850*4882a593Smuzhiyun 		return ENOMEM;
851*4882a593Smuzhiyun 	return 0;
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun void
ahc_dmamem_free(struct ahc_softc * ahc,bus_dma_tag_t dmat,void * vaddr,bus_dmamap_t map)855*4882a593Smuzhiyun ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
856*4882a593Smuzhiyun 		void* vaddr, bus_dmamap_t map)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun 	dma_free_coherent(ahc->dev, dmat->maxsize, vaddr, map);
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun int
ahc_dmamap_load(struct ahc_softc * ahc,bus_dma_tag_t dmat,bus_dmamap_t map,void * buf,bus_size_t buflen,bus_dmamap_callback_t * cb,void * cb_arg,int flags)862*4882a593Smuzhiyun ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map,
863*4882a593Smuzhiyun 		void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
864*4882a593Smuzhiyun 		void *cb_arg, int flags)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun 	/*
867*4882a593Smuzhiyun 	 * Assume for now that this will only be used during
868*4882a593Smuzhiyun 	 * initialization and not for per-transaction buffer mapping.
869*4882a593Smuzhiyun 	 */
870*4882a593Smuzhiyun 	bus_dma_segment_t stack_sg;
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	stack_sg.ds_addr = map;
873*4882a593Smuzhiyun 	stack_sg.ds_len = dmat->maxsize;
874*4882a593Smuzhiyun 	cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
875*4882a593Smuzhiyun 	return (0);
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun void
ahc_dmamap_destroy(struct ahc_softc * ahc,bus_dma_tag_t dmat,bus_dmamap_t map)879*4882a593Smuzhiyun ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun int
ahc_dmamap_unload(struct ahc_softc * ahc,bus_dma_tag_t dmat,bus_dmamap_t map)884*4882a593Smuzhiyun ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun 	/* Nothing to do */
887*4882a593Smuzhiyun 	return (0);
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun static void
ahc_linux_setup_tag_info_global(char * p)891*4882a593Smuzhiyun ahc_linux_setup_tag_info_global(char *p)
892*4882a593Smuzhiyun {
893*4882a593Smuzhiyun 	int tags, i, j;
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
896*4882a593Smuzhiyun 	printk("Setting Global Tags= %d\n", tags);
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(aic7xxx_tag_info); i++) {
899*4882a593Smuzhiyun 		for (j = 0; j < AHC_NUM_TARGETS; j++) {
900*4882a593Smuzhiyun 			aic7xxx_tag_info[i].tag_commands[j] = tags;
901*4882a593Smuzhiyun 		}
902*4882a593Smuzhiyun 	}
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun static void
ahc_linux_setup_tag_info(u_long arg,int instance,int targ,int32_t value)906*4882a593Smuzhiyun ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
907*4882a593Smuzhiyun {
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	if ((instance >= 0) && (targ >= 0)
910*4882a593Smuzhiyun 	 && (instance < ARRAY_SIZE(aic7xxx_tag_info))
911*4882a593Smuzhiyun 	 && (targ < AHC_NUM_TARGETS)) {
912*4882a593Smuzhiyun 		aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff;
913*4882a593Smuzhiyun 		if (bootverbose)
914*4882a593Smuzhiyun 			printk("tag_info[%d:%d] = %d\n", instance, targ, value);
915*4882a593Smuzhiyun 	}
916*4882a593Smuzhiyun }
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun static char *
ahc_parse_brace_option(char * opt_name,char * opt_arg,char * end,int depth,void (* callback)(u_long,int,int,int32_t),u_long callback_arg)919*4882a593Smuzhiyun ahc_parse_brace_option(char *opt_name, char *opt_arg, char *end, int depth,
920*4882a593Smuzhiyun 		       void (*callback)(u_long, int, int, int32_t),
921*4882a593Smuzhiyun 		       u_long callback_arg)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun 	char	*tok_end;
924*4882a593Smuzhiyun 	char	*tok_end2;
925*4882a593Smuzhiyun 	int      i;
926*4882a593Smuzhiyun 	int      instance;
927*4882a593Smuzhiyun 	int	 targ;
928*4882a593Smuzhiyun 	int	 done;
929*4882a593Smuzhiyun 	char	 tok_list[] = {'.', ',', '{', '}', '\0'};
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	/* All options use a ':' name/arg separator */
932*4882a593Smuzhiyun 	if (*opt_arg != ':')
933*4882a593Smuzhiyun 		return (opt_arg);
934*4882a593Smuzhiyun 	opt_arg++;
935*4882a593Smuzhiyun 	instance = -1;
936*4882a593Smuzhiyun 	targ = -1;
937*4882a593Smuzhiyun 	done = FALSE;
938*4882a593Smuzhiyun 	/*
939*4882a593Smuzhiyun 	 * Restore separator that may be in
940*4882a593Smuzhiyun 	 * the middle of our option argument.
941*4882a593Smuzhiyun 	 */
942*4882a593Smuzhiyun 	tok_end = strchr(opt_arg, '\0');
943*4882a593Smuzhiyun 	if (tok_end < end)
944*4882a593Smuzhiyun 		*tok_end = ',';
945*4882a593Smuzhiyun 	while (!done) {
946*4882a593Smuzhiyun 		switch (*opt_arg) {
947*4882a593Smuzhiyun 		case '{':
948*4882a593Smuzhiyun 			if (instance == -1) {
949*4882a593Smuzhiyun 				instance = 0;
950*4882a593Smuzhiyun 			} else {
951*4882a593Smuzhiyun 				if (depth > 1) {
952*4882a593Smuzhiyun 					if (targ == -1)
953*4882a593Smuzhiyun 						targ = 0;
954*4882a593Smuzhiyun 				} else {
955*4882a593Smuzhiyun 					printk("Malformed Option %s\n",
956*4882a593Smuzhiyun 					       opt_name);
957*4882a593Smuzhiyun 					done = TRUE;
958*4882a593Smuzhiyun 				}
959*4882a593Smuzhiyun 			}
960*4882a593Smuzhiyun 			opt_arg++;
961*4882a593Smuzhiyun 			break;
962*4882a593Smuzhiyun 		case '}':
963*4882a593Smuzhiyun 			if (targ != -1)
964*4882a593Smuzhiyun 				targ = -1;
965*4882a593Smuzhiyun 			else if (instance != -1)
966*4882a593Smuzhiyun 				instance = -1;
967*4882a593Smuzhiyun 			opt_arg++;
968*4882a593Smuzhiyun 			break;
969*4882a593Smuzhiyun 		case ',':
970*4882a593Smuzhiyun 		case '.':
971*4882a593Smuzhiyun 			if (instance == -1)
972*4882a593Smuzhiyun 				done = TRUE;
973*4882a593Smuzhiyun 			else if (targ >= 0)
974*4882a593Smuzhiyun 				targ++;
975*4882a593Smuzhiyun 			else if (instance >= 0)
976*4882a593Smuzhiyun 				instance++;
977*4882a593Smuzhiyun 			opt_arg++;
978*4882a593Smuzhiyun 			break;
979*4882a593Smuzhiyun 		case '\0':
980*4882a593Smuzhiyun 			done = TRUE;
981*4882a593Smuzhiyun 			break;
982*4882a593Smuzhiyun 		default:
983*4882a593Smuzhiyun 			tok_end = end;
984*4882a593Smuzhiyun 			for (i = 0; tok_list[i]; i++) {
985*4882a593Smuzhiyun 				tok_end2 = strchr(opt_arg, tok_list[i]);
986*4882a593Smuzhiyun 				if ((tok_end2) && (tok_end2 < tok_end))
987*4882a593Smuzhiyun 					tok_end = tok_end2;
988*4882a593Smuzhiyun 			}
989*4882a593Smuzhiyun 			callback(callback_arg, instance, targ,
990*4882a593Smuzhiyun 				 simple_strtol(opt_arg, NULL, 0));
991*4882a593Smuzhiyun 			opt_arg = tok_end;
992*4882a593Smuzhiyun 			break;
993*4882a593Smuzhiyun 		}
994*4882a593Smuzhiyun 	}
995*4882a593Smuzhiyun 	return (opt_arg);
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun /*
999*4882a593Smuzhiyun  * Handle Linux boot parameters. This routine allows for assigning a value
1000*4882a593Smuzhiyun  * to a parameter with a ':' between the parameter and the value.
1001*4882a593Smuzhiyun  * ie. aic7xxx=stpwlev:1,extended
1002*4882a593Smuzhiyun  */
1003*4882a593Smuzhiyun static int
aic7xxx_setup(char * s)1004*4882a593Smuzhiyun aic7xxx_setup(char *s)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun 	int	i, n;
1007*4882a593Smuzhiyun 	char   *p;
1008*4882a593Smuzhiyun 	char   *end;
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	static const struct {
1011*4882a593Smuzhiyun 		const char *name;
1012*4882a593Smuzhiyun 		uint32_t *flag;
1013*4882a593Smuzhiyun 	} options[] = {
1014*4882a593Smuzhiyun 		{ "extended", &aic7xxx_extended },
1015*4882a593Smuzhiyun 		{ "no_reset", &aic7xxx_no_reset },
1016*4882a593Smuzhiyun 		{ "verbose", &aic7xxx_verbose },
1017*4882a593Smuzhiyun 		{ "allow_memio", &aic7xxx_allow_memio},
1018*4882a593Smuzhiyun #ifdef AHC_DEBUG
1019*4882a593Smuzhiyun 		{ "debug", &ahc_debug },
1020*4882a593Smuzhiyun #endif
1021*4882a593Smuzhiyun 		{ "periodic_otag", &aic7xxx_periodic_otag },
1022*4882a593Smuzhiyun 		{ "pci_parity", &aic7xxx_pci_parity },
1023*4882a593Smuzhiyun 		{ "seltime", &aic7xxx_seltime },
1024*4882a593Smuzhiyun 		{ "tag_info", NULL },
1025*4882a593Smuzhiyun 		{ "global_tag_depth", NULL },
1026*4882a593Smuzhiyun 		{ "dv", NULL }
1027*4882a593Smuzhiyun 	};
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	end = strchr(s, '\0');
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	/*
1032*4882a593Smuzhiyun 	 * XXX ia64 gcc isn't smart enough to know that ARRAY_SIZE
1033*4882a593Smuzhiyun 	 * will never be 0 in this case.
1034*4882a593Smuzhiyun 	 */
1035*4882a593Smuzhiyun 	n = 0;
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	while ((p = strsep(&s, ",.")) != NULL) {
1038*4882a593Smuzhiyun 		if (*p == '\0')
1039*4882a593Smuzhiyun 			continue;
1040*4882a593Smuzhiyun 		for (i = 0; i < ARRAY_SIZE(options); i++) {
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 			n = strlen(options[i].name);
1043*4882a593Smuzhiyun 			if (strncmp(options[i].name, p, n) == 0)
1044*4882a593Smuzhiyun 				break;
1045*4882a593Smuzhiyun 		}
1046*4882a593Smuzhiyun 		if (i == ARRAY_SIZE(options))
1047*4882a593Smuzhiyun 			continue;
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 		if (strncmp(p, "global_tag_depth", n) == 0) {
1050*4882a593Smuzhiyun 			ahc_linux_setup_tag_info_global(p + n);
1051*4882a593Smuzhiyun 		} else if (strncmp(p, "tag_info", n) == 0) {
1052*4882a593Smuzhiyun 			s = ahc_parse_brace_option("tag_info", p + n, end,
1053*4882a593Smuzhiyun 			    2, ahc_linux_setup_tag_info, 0);
1054*4882a593Smuzhiyun 		} else if (p[n] == ':') {
1055*4882a593Smuzhiyun 			*(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
1056*4882a593Smuzhiyun 		} else if (strncmp(p, "verbose", n) == 0) {
1057*4882a593Smuzhiyun 			*(options[i].flag) = 1;
1058*4882a593Smuzhiyun 		} else {
1059*4882a593Smuzhiyun 			*(options[i].flag) ^= 0xFFFFFFFF;
1060*4882a593Smuzhiyun 		}
1061*4882a593Smuzhiyun 	}
1062*4882a593Smuzhiyun 	return 1;
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun __setup("aic7xxx=", aic7xxx_setup);
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun uint32_t aic7xxx_verbose;
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun int
ahc_linux_register_host(struct ahc_softc * ahc,struct scsi_host_template * template)1070*4882a593Smuzhiyun ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *template)
1071*4882a593Smuzhiyun {
1072*4882a593Smuzhiyun 	char	buf[80];
1073*4882a593Smuzhiyun 	struct	Scsi_Host *host;
1074*4882a593Smuzhiyun 	char	*new_name;
1075*4882a593Smuzhiyun 	u_long	s;
1076*4882a593Smuzhiyun 	int	retval;
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun 	template->name = ahc->description;
1079*4882a593Smuzhiyun 	host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
1080*4882a593Smuzhiyun 	if (host == NULL)
1081*4882a593Smuzhiyun 		return (ENOMEM);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	*((struct ahc_softc **)host->hostdata) = ahc;
1084*4882a593Smuzhiyun 	ahc->platform_data->host = host;
1085*4882a593Smuzhiyun 	host->can_queue = AHC_MAX_QUEUE;
1086*4882a593Smuzhiyun 	host->cmd_per_lun = 2;
1087*4882a593Smuzhiyun 	/* XXX No way to communicate the ID for multiple channels */
1088*4882a593Smuzhiyun 	host->this_id = ahc->our_id;
1089*4882a593Smuzhiyun 	host->irq = ahc->platform_data->irq;
1090*4882a593Smuzhiyun 	host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
1091*4882a593Smuzhiyun 	host->max_lun = AHC_NUM_LUNS;
1092*4882a593Smuzhiyun 	host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
1093*4882a593Smuzhiyun 	host->sg_tablesize = AHC_NSEG;
1094*4882a593Smuzhiyun 	ahc_lock(ahc, &s);
1095*4882a593Smuzhiyun 	ahc_set_unit(ahc, ahc_linux_unit++);
1096*4882a593Smuzhiyun 	ahc_unlock(ahc, &s);
1097*4882a593Smuzhiyun 	sprintf(buf, "scsi%d", host->host_no);
1098*4882a593Smuzhiyun 	new_name = kmalloc(strlen(buf) + 1, GFP_ATOMIC);
1099*4882a593Smuzhiyun 	if (new_name != NULL) {
1100*4882a593Smuzhiyun 		strcpy(new_name, buf);
1101*4882a593Smuzhiyun 		ahc_set_name(ahc, new_name);
1102*4882a593Smuzhiyun 	}
1103*4882a593Smuzhiyun 	host->unique_id = ahc->unit;
1104*4882a593Smuzhiyun 	ahc_linux_initialize_scsi_bus(ahc);
1105*4882a593Smuzhiyun 	ahc_intr_enable(ahc, TRUE);
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	host->transportt = ahc_linux_transport_template;
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	retval = scsi_add_host(host, ahc->dev);
1110*4882a593Smuzhiyun 	if (retval) {
1111*4882a593Smuzhiyun 		printk(KERN_WARNING "aic7xxx: scsi_add_host failed\n");
1112*4882a593Smuzhiyun 		scsi_host_put(host);
1113*4882a593Smuzhiyun 		return retval;
1114*4882a593Smuzhiyun 	}
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	scsi_scan_host(host);
1117*4882a593Smuzhiyun 	return 0;
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun /*
1121*4882a593Smuzhiyun  * Place the SCSI bus into a known state by either resetting it,
1122*4882a593Smuzhiyun  * or forcing transfer negotiations on the next command to any
1123*4882a593Smuzhiyun  * target.
1124*4882a593Smuzhiyun  */
1125*4882a593Smuzhiyun static void
ahc_linux_initialize_scsi_bus(struct ahc_softc * ahc)1126*4882a593Smuzhiyun ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
1127*4882a593Smuzhiyun {
1128*4882a593Smuzhiyun 	int i;
1129*4882a593Smuzhiyun 	int numtarg;
1130*4882a593Smuzhiyun 	unsigned long s;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	i = 0;
1133*4882a593Smuzhiyun 	numtarg = 0;
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	ahc_lock(ahc, &s);
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	if (aic7xxx_no_reset != 0)
1138*4882a593Smuzhiyun 		ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	if ((ahc->flags & AHC_RESET_BUS_A) != 0)
1141*4882a593Smuzhiyun 		ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
1142*4882a593Smuzhiyun 	else
1143*4882a593Smuzhiyun 		numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	if ((ahc->features & AHC_TWIN) != 0) {
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 		if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
1148*4882a593Smuzhiyun 			ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
1149*4882a593Smuzhiyun 		} else {
1150*4882a593Smuzhiyun 			if (numtarg == 0)
1151*4882a593Smuzhiyun 				i = 8;
1152*4882a593Smuzhiyun 			numtarg += 8;
1153*4882a593Smuzhiyun 		}
1154*4882a593Smuzhiyun 	}
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	/*
1157*4882a593Smuzhiyun 	 * Force negotiation to async for all targets that
1158*4882a593Smuzhiyun 	 * will not see an initial bus reset.
1159*4882a593Smuzhiyun 	 */
1160*4882a593Smuzhiyun 	for (; i < numtarg; i++) {
1161*4882a593Smuzhiyun 		struct ahc_devinfo devinfo;
1162*4882a593Smuzhiyun 		struct ahc_initiator_tinfo *tinfo;
1163*4882a593Smuzhiyun 		struct ahc_tmode_tstate *tstate;
1164*4882a593Smuzhiyun 		u_int our_id;
1165*4882a593Smuzhiyun 		u_int target_id;
1166*4882a593Smuzhiyun 		char channel;
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 		channel = 'A';
1169*4882a593Smuzhiyun 		our_id = ahc->our_id;
1170*4882a593Smuzhiyun 		target_id = i;
1171*4882a593Smuzhiyun 		if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
1172*4882a593Smuzhiyun 			channel = 'B';
1173*4882a593Smuzhiyun 			our_id = ahc->our_id_b;
1174*4882a593Smuzhiyun 			target_id = i % 8;
1175*4882a593Smuzhiyun 		}
1176*4882a593Smuzhiyun 		tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
1177*4882a593Smuzhiyun 					    target_id, &tstate);
1178*4882a593Smuzhiyun 		ahc_compile_devinfo(&devinfo, our_id, target_id,
1179*4882a593Smuzhiyun 				    CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
1180*4882a593Smuzhiyun 		ahc_update_neg_request(ahc, &devinfo, tstate,
1181*4882a593Smuzhiyun 				       tinfo, AHC_NEG_ALWAYS);
1182*4882a593Smuzhiyun 	}
1183*4882a593Smuzhiyun 	ahc_unlock(ahc, &s);
1184*4882a593Smuzhiyun 	/* Give the bus some time to recover */
1185*4882a593Smuzhiyun 	if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
1186*4882a593Smuzhiyun 		ahc_linux_freeze_simq(ahc);
1187*4882a593Smuzhiyun 		msleep(AIC7XXX_RESET_DELAY);
1188*4882a593Smuzhiyun 		ahc_linux_release_simq(ahc);
1189*4882a593Smuzhiyun 	}
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun int
ahc_platform_alloc(struct ahc_softc * ahc,void * platform_arg)1193*4882a593Smuzhiyun ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1194*4882a593Smuzhiyun {
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	ahc->platform_data =
1197*4882a593Smuzhiyun 	    kzalloc(sizeof(struct ahc_platform_data), GFP_ATOMIC);
1198*4882a593Smuzhiyun 	if (ahc->platform_data == NULL)
1199*4882a593Smuzhiyun 		return (ENOMEM);
1200*4882a593Smuzhiyun 	ahc->platform_data->irq = AHC_LINUX_NOIRQ;
1201*4882a593Smuzhiyun 	ahc_lockinit(ahc);
1202*4882a593Smuzhiyun 	ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
1203*4882a593Smuzhiyun 	ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
1204*4882a593Smuzhiyun 	if (aic7xxx_pci_parity == 0)
1205*4882a593Smuzhiyun 		ahc->flags |= AHC_DISABLE_PCI_PERR;
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	return (0);
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun void
ahc_platform_free(struct ahc_softc * ahc)1211*4882a593Smuzhiyun ahc_platform_free(struct ahc_softc *ahc)
1212*4882a593Smuzhiyun {
1213*4882a593Smuzhiyun 	struct scsi_target *starget;
1214*4882a593Smuzhiyun 	int i;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	if (ahc->platform_data != NULL) {
1217*4882a593Smuzhiyun 		/* destroy all of the device and target objects */
1218*4882a593Smuzhiyun 		for (i = 0; i < AHC_NUM_TARGETS; i++) {
1219*4882a593Smuzhiyun 			starget = ahc->platform_data->starget[i];
1220*4882a593Smuzhiyun 			if (starget != NULL) {
1221*4882a593Smuzhiyun 				ahc->platform_data->starget[i] = NULL;
1222*4882a593Smuzhiyun  			}
1223*4882a593Smuzhiyun  		}
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 		if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
1226*4882a593Smuzhiyun 			free_irq(ahc->platform_data->irq, ahc);
1227*4882a593Smuzhiyun 		if (ahc->tag == BUS_SPACE_PIO
1228*4882a593Smuzhiyun 		 && ahc->bsh.ioport != 0)
1229*4882a593Smuzhiyun 			release_region(ahc->bsh.ioport, 256);
1230*4882a593Smuzhiyun 		if (ahc->tag == BUS_SPACE_MEMIO
1231*4882a593Smuzhiyun 		 && ahc->bsh.maddr != NULL) {
1232*4882a593Smuzhiyun 			iounmap(ahc->bsh.maddr);
1233*4882a593Smuzhiyun 			release_mem_region(ahc->platform_data->mem_busaddr,
1234*4882a593Smuzhiyun 					   0x1000);
1235*4882a593Smuzhiyun 		}
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 		if (ahc->platform_data->host)
1238*4882a593Smuzhiyun 			scsi_host_put(ahc->platform_data->host);
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 		kfree(ahc->platform_data);
1241*4882a593Smuzhiyun 	}
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun void
ahc_platform_freeze_devq(struct ahc_softc * ahc,struct scb * scb)1245*4882a593Smuzhiyun ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
1246*4882a593Smuzhiyun {
1247*4882a593Smuzhiyun 	ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1248*4882a593Smuzhiyun 				SCB_GET_CHANNEL(ahc, scb),
1249*4882a593Smuzhiyun 				SCB_GET_LUN(scb), SCB_LIST_NULL,
1250*4882a593Smuzhiyun 				ROLE_UNKNOWN, CAM_REQUEUE_REQ);
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun void
ahc_platform_set_tags(struct ahc_softc * ahc,struct scsi_device * sdev,struct ahc_devinfo * devinfo,ahc_queue_alg alg)1254*4882a593Smuzhiyun ahc_platform_set_tags(struct ahc_softc *ahc, struct scsi_device *sdev,
1255*4882a593Smuzhiyun 		      struct ahc_devinfo *devinfo, ahc_queue_alg alg)
1256*4882a593Smuzhiyun {
1257*4882a593Smuzhiyun 	struct ahc_linux_device *dev;
1258*4882a593Smuzhiyun 	int was_queuing;
1259*4882a593Smuzhiyun 	int now_queuing;
1260*4882a593Smuzhiyun 
1261*4882a593Smuzhiyun 	if (sdev == NULL)
1262*4882a593Smuzhiyun 		return;
1263*4882a593Smuzhiyun 	dev = scsi_transport_device_data(sdev);
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
1266*4882a593Smuzhiyun 	switch (alg) {
1267*4882a593Smuzhiyun 	default:
1268*4882a593Smuzhiyun 	case AHC_QUEUE_NONE:
1269*4882a593Smuzhiyun 		now_queuing = 0;
1270*4882a593Smuzhiyun 		break;
1271*4882a593Smuzhiyun 	case AHC_QUEUE_BASIC:
1272*4882a593Smuzhiyun 		now_queuing = AHC_DEV_Q_BASIC;
1273*4882a593Smuzhiyun 		break;
1274*4882a593Smuzhiyun 	case AHC_QUEUE_TAGGED:
1275*4882a593Smuzhiyun 		now_queuing = AHC_DEV_Q_TAGGED;
1276*4882a593Smuzhiyun 		break;
1277*4882a593Smuzhiyun 	}
1278*4882a593Smuzhiyun 	if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
1279*4882a593Smuzhiyun 	 && (was_queuing != now_queuing)
1280*4882a593Smuzhiyun 	 && (dev->active != 0)) {
1281*4882a593Smuzhiyun 		dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
1282*4882a593Smuzhiyun 		dev->qfrozen++;
1283*4882a593Smuzhiyun 	}
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
1286*4882a593Smuzhiyun 	if (now_queuing) {
1287*4882a593Smuzhiyun 		u_int usertags;
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 		usertags = ahc_linux_user_tagdepth(ahc, devinfo);
1290*4882a593Smuzhiyun 		if (!was_queuing) {
1291*4882a593Smuzhiyun 			/*
1292*4882a593Smuzhiyun 			 * Start out aggressively and allow our
1293*4882a593Smuzhiyun 			 * dynamic queue depth algorithm to take
1294*4882a593Smuzhiyun 			 * care of the rest.
1295*4882a593Smuzhiyun 			 */
1296*4882a593Smuzhiyun 			dev->maxtags = usertags;
1297*4882a593Smuzhiyun 			dev->openings = dev->maxtags - dev->active;
1298*4882a593Smuzhiyun 		}
1299*4882a593Smuzhiyun 		if (dev->maxtags == 0) {
1300*4882a593Smuzhiyun 			/*
1301*4882a593Smuzhiyun 			 * Queueing is disabled by the user.
1302*4882a593Smuzhiyun 			 */
1303*4882a593Smuzhiyun 			dev->openings = 1;
1304*4882a593Smuzhiyun 		} else if (alg == AHC_QUEUE_TAGGED) {
1305*4882a593Smuzhiyun 			dev->flags |= AHC_DEV_Q_TAGGED;
1306*4882a593Smuzhiyun 			if (aic7xxx_periodic_otag != 0)
1307*4882a593Smuzhiyun 				dev->flags |= AHC_DEV_PERIODIC_OTAG;
1308*4882a593Smuzhiyun 		} else
1309*4882a593Smuzhiyun 			dev->flags |= AHC_DEV_Q_BASIC;
1310*4882a593Smuzhiyun 	} else {
1311*4882a593Smuzhiyun 		/* We can only have one opening. */
1312*4882a593Smuzhiyun 		dev->maxtags = 0;
1313*4882a593Smuzhiyun 		dev->openings =  1 - dev->active;
1314*4882a593Smuzhiyun 	}
1315*4882a593Smuzhiyun 	switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
1316*4882a593Smuzhiyun 	case AHC_DEV_Q_BASIC:
1317*4882a593Smuzhiyun 	case AHC_DEV_Q_TAGGED:
1318*4882a593Smuzhiyun 		scsi_change_queue_depth(sdev,
1319*4882a593Smuzhiyun 				dev->openings + dev->active);
1320*4882a593Smuzhiyun 		break;
1321*4882a593Smuzhiyun 	default:
1322*4882a593Smuzhiyun 		/*
1323*4882a593Smuzhiyun 		 * We allow the OS to queue 2 untagged transactions to
1324*4882a593Smuzhiyun 		 * us at any time even though we can only execute them
1325*4882a593Smuzhiyun 		 * serially on the controller/device.  This should
1326*4882a593Smuzhiyun 		 * remove some latency.
1327*4882a593Smuzhiyun 		 */
1328*4882a593Smuzhiyun 		scsi_change_queue_depth(sdev, 2);
1329*4882a593Smuzhiyun 		break;
1330*4882a593Smuzhiyun 	}
1331*4882a593Smuzhiyun }
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun int
ahc_platform_abort_scbs(struct ahc_softc * ahc,int target,char channel,int lun,u_int tag,role_t role,uint32_t status)1334*4882a593Smuzhiyun ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
1335*4882a593Smuzhiyun 			int lun, u_int tag, role_t role, uint32_t status)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun 	return 0;
1338*4882a593Smuzhiyun }
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun static u_int
ahc_linux_user_tagdepth(struct ahc_softc * ahc,struct ahc_devinfo * devinfo)1341*4882a593Smuzhiyun ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1342*4882a593Smuzhiyun {
1343*4882a593Smuzhiyun 	static int warned_user;
1344*4882a593Smuzhiyun 	u_int tags;
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	tags = 0;
1347*4882a593Smuzhiyun 	if ((ahc->user_discenable & devinfo->target_mask) != 0) {
1348*4882a593Smuzhiyun 		if (ahc->unit >= ARRAY_SIZE(aic7xxx_tag_info)) {
1349*4882a593Smuzhiyun 			if (warned_user == 0) {
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 				printk(KERN_WARNING
1352*4882a593Smuzhiyun "aic7xxx: WARNING: Insufficient tag_info instances\n"
1353*4882a593Smuzhiyun "aic7xxx: for installed controllers. Using defaults\n"
1354*4882a593Smuzhiyun "aic7xxx: Please update the aic7xxx_tag_info array in\n"
1355*4882a593Smuzhiyun "aic7xxx: the aic7xxx_osm..c source file.\n");
1356*4882a593Smuzhiyun 				warned_user++;
1357*4882a593Smuzhiyun 			}
1358*4882a593Smuzhiyun 			tags = AHC_MAX_QUEUE;
1359*4882a593Smuzhiyun 		} else {
1360*4882a593Smuzhiyun 			adapter_tag_info_t *tag_info;
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 			tag_info = &aic7xxx_tag_info[ahc->unit];
1363*4882a593Smuzhiyun 			tags = tag_info->tag_commands[devinfo->target_offset];
1364*4882a593Smuzhiyun 			if (tags > AHC_MAX_QUEUE)
1365*4882a593Smuzhiyun 				tags = AHC_MAX_QUEUE;
1366*4882a593Smuzhiyun 		}
1367*4882a593Smuzhiyun 	}
1368*4882a593Smuzhiyun 	return (tags);
1369*4882a593Smuzhiyun }
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun /*
1372*4882a593Smuzhiyun  * Determines the queue depth for a given device.
1373*4882a593Smuzhiyun  */
1374*4882a593Smuzhiyun static void
ahc_linux_device_queue_depth(struct scsi_device * sdev)1375*4882a593Smuzhiyun ahc_linux_device_queue_depth(struct scsi_device *sdev)
1376*4882a593Smuzhiyun {
1377*4882a593Smuzhiyun 	struct	ahc_devinfo devinfo;
1378*4882a593Smuzhiyun 	u_int	tags;
1379*4882a593Smuzhiyun 	struct ahc_softc *ahc = *((struct ahc_softc **)sdev->host->hostdata);
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	ahc_compile_devinfo(&devinfo,
1382*4882a593Smuzhiyun 			    sdev->sdev_target->channel == 0
1383*4882a593Smuzhiyun 			  ? ahc->our_id : ahc->our_id_b,
1384*4882a593Smuzhiyun 			    sdev->sdev_target->id, sdev->lun,
1385*4882a593Smuzhiyun 			    sdev->sdev_target->channel == 0 ? 'A' : 'B',
1386*4882a593Smuzhiyun 			    ROLE_INITIATOR);
1387*4882a593Smuzhiyun 	tags = ahc_linux_user_tagdepth(ahc, &devinfo);
1388*4882a593Smuzhiyun 	if (tags != 0 && sdev->tagged_supported != 0) {
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun 		ahc_platform_set_tags(ahc, sdev, &devinfo, AHC_QUEUE_TAGGED);
1391*4882a593Smuzhiyun 		ahc_send_async(ahc, devinfo.channel, devinfo.target,
1392*4882a593Smuzhiyun 			       devinfo.lun, AC_TRANSFER_NEG);
1393*4882a593Smuzhiyun 		ahc_print_devinfo(ahc, &devinfo);
1394*4882a593Smuzhiyun 		printk("Tagged Queuing enabled.  Depth %d\n", tags);
1395*4882a593Smuzhiyun 	} else {
1396*4882a593Smuzhiyun 		ahc_platform_set_tags(ahc, sdev, &devinfo, AHC_QUEUE_NONE);
1397*4882a593Smuzhiyun 		ahc_send_async(ahc, devinfo.channel, devinfo.target,
1398*4882a593Smuzhiyun 			       devinfo.lun, AC_TRANSFER_NEG);
1399*4882a593Smuzhiyun 	}
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun static int
ahc_linux_run_command(struct ahc_softc * ahc,struct ahc_linux_device * dev,struct scsi_cmnd * cmd)1403*4882a593Smuzhiyun ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
1404*4882a593Smuzhiyun 		      struct scsi_cmnd *cmd)
1405*4882a593Smuzhiyun {
1406*4882a593Smuzhiyun 	struct	 scb *scb;
1407*4882a593Smuzhiyun 	struct	 hardware_scb *hscb;
1408*4882a593Smuzhiyun 	struct	 ahc_initiator_tinfo *tinfo;
1409*4882a593Smuzhiyun 	struct	 ahc_tmode_tstate *tstate;
1410*4882a593Smuzhiyun 	uint16_t mask;
1411*4882a593Smuzhiyun 	struct scb_tailq *untagged_q = NULL;
1412*4882a593Smuzhiyun 	int nseg;
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun 	/*
1415*4882a593Smuzhiyun 	 * Schedule us to run later.  The only reason we are not
1416*4882a593Smuzhiyun 	 * running is because the whole controller Q is frozen.
1417*4882a593Smuzhiyun 	 */
1418*4882a593Smuzhiyun 	if (ahc->platform_data->qfrozen != 0)
1419*4882a593Smuzhiyun 		return SCSI_MLQUEUE_HOST_BUSY;
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	/*
1422*4882a593Smuzhiyun 	 * We only allow one untagged transaction
1423*4882a593Smuzhiyun 	 * per target in the initiator role unless
1424*4882a593Smuzhiyun 	 * we are storing a full busy target *lun*
1425*4882a593Smuzhiyun 	 * table in SCB space.
1426*4882a593Smuzhiyun 	 */
1427*4882a593Smuzhiyun 	if (!(cmd->flags & SCMD_TAGGED)
1428*4882a593Smuzhiyun 	    && (ahc->features & AHC_SCB_BTT) == 0) {
1429*4882a593Smuzhiyun 		int target_offset;
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 		target_offset = cmd->device->id + cmd->device->channel * 8;
1432*4882a593Smuzhiyun 		untagged_q = &(ahc->untagged_queues[target_offset]);
1433*4882a593Smuzhiyun 		if (!TAILQ_EMPTY(untagged_q))
1434*4882a593Smuzhiyun 			/* if we're already executing an untagged command
1435*4882a593Smuzhiyun 			 * we're busy to another */
1436*4882a593Smuzhiyun 			return SCSI_MLQUEUE_DEVICE_BUSY;
1437*4882a593Smuzhiyun 	}
1438*4882a593Smuzhiyun 
1439*4882a593Smuzhiyun 	nseg = scsi_dma_map(cmd);
1440*4882a593Smuzhiyun 	if (nseg < 0)
1441*4882a593Smuzhiyun 		return SCSI_MLQUEUE_HOST_BUSY;
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 	/*
1444*4882a593Smuzhiyun 	 * Get an scb to use.
1445*4882a593Smuzhiyun 	 */
1446*4882a593Smuzhiyun 	scb = ahc_get_scb(ahc);
1447*4882a593Smuzhiyun 	if (!scb) {
1448*4882a593Smuzhiyun 		scsi_dma_unmap(cmd);
1449*4882a593Smuzhiyun 		return SCSI_MLQUEUE_HOST_BUSY;
1450*4882a593Smuzhiyun 	}
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 	scb->io_ctx = cmd;
1453*4882a593Smuzhiyun 	scb->platform_data->dev = dev;
1454*4882a593Smuzhiyun 	hscb = scb->hscb;
1455*4882a593Smuzhiyun 	cmd->host_scribble = (char *)scb;
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	/*
1458*4882a593Smuzhiyun 	 * Fill out basics of the HSCB.
1459*4882a593Smuzhiyun 	 */
1460*4882a593Smuzhiyun 	hscb->control = 0;
1461*4882a593Smuzhiyun 	hscb->scsiid = BUILD_SCSIID(ahc, cmd);
1462*4882a593Smuzhiyun 	hscb->lun = cmd->device->lun;
1463*4882a593Smuzhiyun 	mask = SCB_GET_TARGET_MASK(ahc, scb);
1464*4882a593Smuzhiyun 	tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
1465*4882a593Smuzhiyun 				    SCB_GET_OUR_ID(scb),
1466*4882a593Smuzhiyun 				    SCB_GET_TARGET(ahc, scb), &tstate);
1467*4882a593Smuzhiyun 	hscb->scsirate = tinfo->scsirate;
1468*4882a593Smuzhiyun 	hscb->scsioffset = tinfo->curr.offset;
1469*4882a593Smuzhiyun 	if ((tstate->ultraenb & mask) != 0)
1470*4882a593Smuzhiyun 		hscb->control |= ULTRAENB;
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 	if ((ahc->user_discenable & mask) != 0)
1473*4882a593Smuzhiyun 		hscb->control |= DISCENB;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	if ((tstate->auto_negotiate & mask) != 0) {
1476*4882a593Smuzhiyun 		scb->flags |= SCB_AUTO_NEGOTIATE;
1477*4882a593Smuzhiyun 		scb->hscb->control |= MK_MESSAGE;
1478*4882a593Smuzhiyun 	}
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
1481*4882a593Smuzhiyun 		if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
1482*4882a593Smuzhiyun 				&& (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
1483*4882a593Smuzhiyun 			hscb->control |= MSG_ORDERED_TASK;
1484*4882a593Smuzhiyun 			dev->commands_since_idle_or_otag = 0;
1485*4882a593Smuzhiyun 		} else {
1486*4882a593Smuzhiyun 			hscb->control |= MSG_SIMPLE_TASK;
1487*4882a593Smuzhiyun 		}
1488*4882a593Smuzhiyun 	}
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	hscb->cdb_len = cmd->cmd_len;
1491*4882a593Smuzhiyun 	if (hscb->cdb_len <= 12) {
1492*4882a593Smuzhiyun 		memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
1493*4882a593Smuzhiyun 	} else {
1494*4882a593Smuzhiyun 		memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
1495*4882a593Smuzhiyun 		scb->flags |= SCB_CDB32_PTR;
1496*4882a593Smuzhiyun 	}
1497*4882a593Smuzhiyun 
1498*4882a593Smuzhiyun 	scb->platform_data->xfer_len = 0;
1499*4882a593Smuzhiyun 	ahc_set_residual(scb, 0);
1500*4882a593Smuzhiyun 	ahc_set_sense_residual(scb, 0);
1501*4882a593Smuzhiyun 	scb->sg_count = 0;
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	if (nseg > 0) {
1504*4882a593Smuzhiyun 		struct	ahc_dma_seg *sg;
1505*4882a593Smuzhiyun 		struct	scatterlist *cur_seg;
1506*4882a593Smuzhiyun 		int i;
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 		/* Copy the segments into the SG list. */
1509*4882a593Smuzhiyun 		sg = scb->sg_list;
1510*4882a593Smuzhiyun 		/*
1511*4882a593Smuzhiyun 		 * The sg_count may be larger than nseg if
1512*4882a593Smuzhiyun 		 * a transfer crosses a 32bit page.
1513*4882a593Smuzhiyun 		 */
1514*4882a593Smuzhiyun 		scsi_for_each_sg(cmd, cur_seg, nseg, i) {
1515*4882a593Smuzhiyun 			dma_addr_t addr;
1516*4882a593Smuzhiyun 			bus_size_t len;
1517*4882a593Smuzhiyun 			int consumed;
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 			addr = sg_dma_address(cur_seg);
1520*4882a593Smuzhiyun 			len = sg_dma_len(cur_seg);
1521*4882a593Smuzhiyun 			consumed = ahc_linux_map_seg(ahc, scb,
1522*4882a593Smuzhiyun 						     sg, addr, len);
1523*4882a593Smuzhiyun 			sg += consumed;
1524*4882a593Smuzhiyun 			scb->sg_count += consumed;
1525*4882a593Smuzhiyun 		}
1526*4882a593Smuzhiyun 		sg--;
1527*4882a593Smuzhiyun 		sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1528*4882a593Smuzhiyun 
1529*4882a593Smuzhiyun 		/*
1530*4882a593Smuzhiyun 		 * Reset the sg list pointer.
1531*4882a593Smuzhiyun 		 */
1532*4882a593Smuzhiyun 		scb->hscb->sgptr =
1533*4882a593Smuzhiyun 			ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
1534*4882a593Smuzhiyun 
1535*4882a593Smuzhiyun 		/*
1536*4882a593Smuzhiyun 		 * Copy the first SG into the "current"
1537*4882a593Smuzhiyun 		 * data pointer area.
1538*4882a593Smuzhiyun 		 */
1539*4882a593Smuzhiyun 		scb->hscb->dataptr = scb->sg_list->addr;
1540*4882a593Smuzhiyun 		scb->hscb->datacnt = scb->sg_list->len;
1541*4882a593Smuzhiyun 	} else {
1542*4882a593Smuzhiyun 		scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
1543*4882a593Smuzhiyun 		scb->hscb->dataptr = 0;
1544*4882a593Smuzhiyun 		scb->hscb->datacnt = 0;
1545*4882a593Smuzhiyun 		scb->sg_count = 0;
1546*4882a593Smuzhiyun 	}
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1549*4882a593Smuzhiyun 	dev->openings--;
1550*4882a593Smuzhiyun 	dev->active++;
1551*4882a593Smuzhiyun 	dev->commands_issued++;
1552*4882a593Smuzhiyun 	if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
1553*4882a593Smuzhiyun 		dev->commands_since_idle_or_otag++;
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	scb->flags |= SCB_ACTIVE;
1556*4882a593Smuzhiyun 	if (untagged_q) {
1557*4882a593Smuzhiyun 		TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1558*4882a593Smuzhiyun 		scb->flags |= SCB_UNTAGGEDQ;
1559*4882a593Smuzhiyun 	}
1560*4882a593Smuzhiyun 	ahc_queue_scb(ahc, scb);
1561*4882a593Smuzhiyun 	return 0;
1562*4882a593Smuzhiyun }
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun /*
1565*4882a593Smuzhiyun  * SCSI controller interrupt handler.
1566*4882a593Smuzhiyun  */
1567*4882a593Smuzhiyun irqreturn_t
ahc_linux_isr(int irq,void * dev_id)1568*4882a593Smuzhiyun ahc_linux_isr(int irq, void *dev_id)
1569*4882a593Smuzhiyun {
1570*4882a593Smuzhiyun 	struct	ahc_softc *ahc;
1571*4882a593Smuzhiyun 	u_long	flags;
1572*4882a593Smuzhiyun 	int	ours;
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	ahc = (struct ahc_softc *) dev_id;
1575*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
1576*4882a593Smuzhiyun 	ours = ahc_intr(ahc);
1577*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
1578*4882a593Smuzhiyun 	return IRQ_RETVAL(ours);
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun void
ahc_platform_flushwork(struct ahc_softc * ahc)1582*4882a593Smuzhiyun ahc_platform_flushwork(struct ahc_softc *ahc)
1583*4882a593Smuzhiyun {
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun }
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun void
ahc_send_async(struct ahc_softc * ahc,char channel,u_int target,u_int lun,ac_code code)1588*4882a593Smuzhiyun ahc_send_async(struct ahc_softc *ahc, char channel,
1589*4882a593Smuzhiyun 	       u_int target, u_int lun, ac_code code)
1590*4882a593Smuzhiyun {
1591*4882a593Smuzhiyun 	switch (code) {
1592*4882a593Smuzhiyun 	case AC_TRANSFER_NEG:
1593*4882a593Smuzhiyun 	{
1594*4882a593Smuzhiyun 		struct	scsi_target *starget;
1595*4882a593Smuzhiyun 		struct	ahc_initiator_tinfo *tinfo;
1596*4882a593Smuzhiyun 		struct	ahc_tmode_tstate *tstate;
1597*4882a593Smuzhiyun 		int	target_offset;
1598*4882a593Smuzhiyun 		unsigned int target_ppr_options;
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun 		BUG_ON(target == CAM_TARGET_WILDCARD);
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 		tinfo = ahc_fetch_transinfo(ahc, channel,
1603*4882a593Smuzhiyun 						channel == 'A' ? ahc->our_id
1604*4882a593Smuzhiyun 							       : ahc->our_id_b,
1605*4882a593Smuzhiyun 						target, &tstate);
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 		/*
1608*4882a593Smuzhiyun 		 * Don't bother reporting results while
1609*4882a593Smuzhiyun 		 * negotiations are still pending.
1610*4882a593Smuzhiyun 		 */
1611*4882a593Smuzhiyun 		if (tinfo->curr.period != tinfo->goal.period
1612*4882a593Smuzhiyun 		 || tinfo->curr.width != tinfo->goal.width
1613*4882a593Smuzhiyun 		 || tinfo->curr.offset != tinfo->goal.offset
1614*4882a593Smuzhiyun 		 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
1615*4882a593Smuzhiyun 			if (bootverbose == 0)
1616*4882a593Smuzhiyun 				break;
1617*4882a593Smuzhiyun 
1618*4882a593Smuzhiyun 		/*
1619*4882a593Smuzhiyun 		 * Don't bother reporting results that
1620*4882a593Smuzhiyun 		 * are identical to those last reported.
1621*4882a593Smuzhiyun 		 */
1622*4882a593Smuzhiyun 		target_offset = target;
1623*4882a593Smuzhiyun 		if (channel == 'B')
1624*4882a593Smuzhiyun 			target_offset += 8;
1625*4882a593Smuzhiyun 		starget = ahc->platform_data->starget[target_offset];
1626*4882a593Smuzhiyun 		if (starget == NULL)
1627*4882a593Smuzhiyun 			break;
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun 		target_ppr_options =
1630*4882a593Smuzhiyun 			(spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
1631*4882a593Smuzhiyun 			+ (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
1632*4882a593Smuzhiyun 			+ (spi_iu(starget) ?  MSG_EXT_PPR_IU_REQ : 0);
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun 		if (tinfo->curr.period == spi_period(starget)
1635*4882a593Smuzhiyun 		    && tinfo->curr.width == spi_width(starget)
1636*4882a593Smuzhiyun 		    && tinfo->curr.offset == spi_offset(starget)
1637*4882a593Smuzhiyun 		 && tinfo->curr.ppr_options == target_ppr_options)
1638*4882a593Smuzhiyun 			if (bootverbose == 0)
1639*4882a593Smuzhiyun 				break;
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 		spi_period(starget) = tinfo->curr.period;
1642*4882a593Smuzhiyun 		spi_width(starget) = tinfo->curr.width;
1643*4882a593Smuzhiyun 		spi_offset(starget) = tinfo->curr.offset;
1644*4882a593Smuzhiyun 		spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
1645*4882a593Smuzhiyun 		spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
1646*4882a593Smuzhiyun 		spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
1647*4882a593Smuzhiyun 		spi_display_xfer_agreement(starget);
1648*4882a593Smuzhiyun 		break;
1649*4882a593Smuzhiyun 	}
1650*4882a593Smuzhiyun         case AC_SENT_BDR:
1651*4882a593Smuzhiyun 	{
1652*4882a593Smuzhiyun 		WARN_ON(lun != CAM_LUN_WILDCARD);
1653*4882a593Smuzhiyun 		scsi_report_device_reset(ahc->platform_data->host,
1654*4882a593Smuzhiyun 					 channel - 'A', target);
1655*4882a593Smuzhiyun 		break;
1656*4882a593Smuzhiyun 	}
1657*4882a593Smuzhiyun         case AC_BUS_RESET:
1658*4882a593Smuzhiyun 		if (ahc->platform_data->host != NULL) {
1659*4882a593Smuzhiyun 			scsi_report_bus_reset(ahc->platform_data->host,
1660*4882a593Smuzhiyun 					      channel - 'A');
1661*4882a593Smuzhiyun 		}
1662*4882a593Smuzhiyun                 break;
1663*4882a593Smuzhiyun         default:
1664*4882a593Smuzhiyun                 panic("ahc_send_async: Unexpected async event");
1665*4882a593Smuzhiyun         }
1666*4882a593Smuzhiyun }
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun /*
1669*4882a593Smuzhiyun  * Calls the higher level scsi done function and frees the scb.
1670*4882a593Smuzhiyun  */
1671*4882a593Smuzhiyun void
ahc_done(struct ahc_softc * ahc,struct scb * scb)1672*4882a593Smuzhiyun ahc_done(struct ahc_softc *ahc, struct scb *scb)
1673*4882a593Smuzhiyun {
1674*4882a593Smuzhiyun 	struct scsi_cmnd *cmd;
1675*4882a593Smuzhiyun 	struct	   ahc_linux_device *dev;
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 	LIST_REMOVE(scb, pending_links);
1678*4882a593Smuzhiyun 	if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
1679*4882a593Smuzhiyun 		struct scb_tailq *untagged_q;
1680*4882a593Smuzhiyun 		int target_offset;
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1683*4882a593Smuzhiyun 		untagged_q = &(ahc->untagged_queues[target_offset]);
1684*4882a593Smuzhiyun 		TAILQ_REMOVE(untagged_q, scb, links.tqe);
1685*4882a593Smuzhiyun 		BUG_ON(!TAILQ_EMPTY(untagged_q));
1686*4882a593Smuzhiyun 	} else if ((scb->flags & SCB_ACTIVE) == 0) {
1687*4882a593Smuzhiyun 		/*
1688*4882a593Smuzhiyun 		 * Transactions aborted from the untagged queue may
1689*4882a593Smuzhiyun 		 * not have been dispatched to the controller, so
1690*4882a593Smuzhiyun 		 * only check the SCB_ACTIVE flag for tagged transactions.
1691*4882a593Smuzhiyun 		 */
1692*4882a593Smuzhiyun 		printk("SCB %d done'd twice\n", scb->hscb->tag);
1693*4882a593Smuzhiyun 		ahc_dump_card_state(ahc);
1694*4882a593Smuzhiyun 		panic("Stopping for safety");
1695*4882a593Smuzhiyun 	}
1696*4882a593Smuzhiyun 	cmd = scb->io_ctx;
1697*4882a593Smuzhiyun 	dev = scb->platform_data->dev;
1698*4882a593Smuzhiyun 	dev->active--;
1699*4882a593Smuzhiyun 	dev->openings++;
1700*4882a593Smuzhiyun 	if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
1701*4882a593Smuzhiyun 		cmd->result &= ~(CAM_DEV_QFRZN << 16);
1702*4882a593Smuzhiyun 		dev->qfrozen--;
1703*4882a593Smuzhiyun 	}
1704*4882a593Smuzhiyun 	ahc_linux_unmap_scb(ahc, scb);
1705*4882a593Smuzhiyun 
1706*4882a593Smuzhiyun 	/*
1707*4882a593Smuzhiyun 	 * Guard against stale sense data.
1708*4882a593Smuzhiyun 	 * The Linux mid-layer assumes that sense
1709*4882a593Smuzhiyun 	 * was retrieved anytime the first byte of
1710*4882a593Smuzhiyun 	 * the sense buffer looks "sane".
1711*4882a593Smuzhiyun 	 */
1712*4882a593Smuzhiyun 	cmd->sense_buffer[0] = 0;
1713*4882a593Smuzhiyun 	if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
1714*4882a593Smuzhiyun #ifdef AHC_REPORT_UNDERFLOWS
1715*4882a593Smuzhiyun 		uint32_t amount_xferred;
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun 		amount_xferred =
1718*4882a593Smuzhiyun 		    ahc_get_transfer_length(scb) - ahc_get_residual(scb);
1719*4882a593Smuzhiyun #endif
1720*4882a593Smuzhiyun 		if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
1721*4882a593Smuzhiyun #ifdef AHC_DEBUG
1722*4882a593Smuzhiyun 			if ((ahc_debug & AHC_SHOW_MISC) != 0) {
1723*4882a593Smuzhiyun 				ahc_print_path(ahc, scb);
1724*4882a593Smuzhiyun 				printk("Set CAM_UNCOR_PARITY\n");
1725*4882a593Smuzhiyun 			}
1726*4882a593Smuzhiyun #endif
1727*4882a593Smuzhiyun 			ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
1728*4882a593Smuzhiyun #ifdef AHC_REPORT_UNDERFLOWS
1729*4882a593Smuzhiyun 		/*
1730*4882a593Smuzhiyun 		 * This code is disabled by default as some
1731*4882a593Smuzhiyun 		 * clients of the SCSI system do not properly
1732*4882a593Smuzhiyun 		 * initialize the underflow parameter.  This
1733*4882a593Smuzhiyun 		 * results in spurious termination of commands
1734*4882a593Smuzhiyun 		 * that complete as expected (e.g. underflow is
1735*4882a593Smuzhiyun 		 * allowed as command can return variable amounts
1736*4882a593Smuzhiyun 		 * of data.
1737*4882a593Smuzhiyun 		 */
1738*4882a593Smuzhiyun 		} else if (amount_xferred < scb->io_ctx->underflow) {
1739*4882a593Smuzhiyun 			u_int i;
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun 			ahc_print_path(ahc, scb);
1742*4882a593Smuzhiyun 			printk("CDB:");
1743*4882a593Smuzhiyun 			for (i = 0; i < scb->io_ctx->cmd_len; i++)
1744*4882a593Smuzhiyun 				printk(" 0x%x", scb->io_ctx->cmnd[i]);
1745*4882a593Smuzhiyun 			printk("\n");
1746*4882a593Smuzhiyun 			ahc_print_path(ahc, scb);
1747*4882a593Smuzhiyun 			printk("Saw underflow (%ld of %ld bytes). "
1748*4882a593Smuzhiyun 			       "Treated as error\n",
1749*4882a593Smuzhiyun 				ahc_get_residual(scb),
1750*4882a593Smuzhiyun 				ahc_get_transfer_length(scb));
1751*4882a593Smuzhiyun 			ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
1752*4882a593Smuzhiyun #endif
1753*4882a593Smuzhiyun 		} else {
1754*4882a593Smuzhiyun 			ahc_set_transaction_status(scb, CAM_REQ_CMP);
1755*4882a593Smuzhiyun 		}
1756*4882a593Smuzhiyun 	} else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
1757*4882a593Smuzhiyun 		ahc_linux_handle_scsi_status(ahc, cmd->device, scb);
1758*4882a593Smuzhiyun 	}
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 	if (dev->openings == 1
1761*4882a593Smuzhiyun 	 && ahc_get_transaction_status(scb) == CAM_REQ_CMP
1762*4882a593Smuzhiyun 	 && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
1763*4882a593Smuzhiyun 		dev->tag_success_count++;
1764*4882a593Smuzhiyun 	/*
1765*4882a593Smuzhiyun 	 * Some devices deal with temporary internal resource
1766*4882a593Smuzhiyun 	 * shortages by returning queue full.  When the queue
1767*4882a593Smuzhiyun 	 * full occurrs, we throttle back.  Slowly try to get
1768*4882a593Smuzhiyun 	 * back to our previous queue depth.
1769*4882a593Smuzhiyun 	 */
1770*4882a593Smuzhiyun 	if ((dev->openings + dev->active) < dev->maxtags
1771*4882a593Smuzhiyun 	 && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
1772*4882a593Smuzhiyun 		dev->tag_success_count = 0;
1773*4882a593Smuzhiyun 		dev->openings++;
1774*4882a593Smuzhiyun 	}
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	if (dev->active == 0)
1777*4882a593Smuzhiyun 		dev->commands_since_idle_or_otag = 0;
1778*4882a593Smuzhiyun 
1779*4882a593Smuzhiyun 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
1780*4882a593Smuzhiyun 		printk("Recovery SCB completes\n");
1781*4882a593Smuzhiyun 		if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
1782*4882a593Smuzhiyun 		 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
1783*4882a593Smuzhiyun 			ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1784*4882a593Smuzhiyun 
1785*4882a593Smuzhiyun 		if (ahc->platform_data->eh_done)
1786*4882a593Smuzhiyun 			complete(ahc->platform_data->eh_done);
1787*4882a593Smuzhiyun 	}
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	ahc_free_scb(ahc, scb);
1790*4882a593Smuzhiyun 	ahc_linux_queue_cmd_complete(ahc, cmd);
1791*4882a593Smuzhiyun }
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun static void
ahc_linux_handle_scsi_status(struct ahc_softc * ahc,struct scsi_device * sdev,struct scb * scb)1794*4882a593Smuzhiyun ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
1795*4882a593Smuzhiyun 			     struct scsi_device *sdev, struct scb *scb)
1796*4882a593Smuzhiyun {
1797*4882a593Smuzhiyun 	struct	ahc_devinfo devinfo;
1798*4882a593Smuzhiyun 	struct ahc_linux_device *dev = scsi_transport_device_data(sdev);
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun 	ahc_compile_devinfo(&devinfo,
1801*4882a593Smuzhiyun 			    ahc->our_id,
1802*4882a593Smuzhiyun 			    sdev->sdev_target->id, sdev->lun,
1803*4882a593Smuzhiyun 			    sdev->sdev_target->channel == 0 ? 'A' : 'B',
1804*4882a593Smuzhiyun 			    ROLE_INITIATOR);
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	/*
1807*4882a593Smuzhiyun 	 * We don't currently trust the mid-layer to
1808*4882a593Smuzhiyun 	 * properly deal with queue full or busy.  So,
1809*4882a593Smuzhiyun 	 * when one occurs, we tell the mid-layer to
1810*4882a593Smuzhiyun 	 * unconditionally requeue the command to us
1811*4882a593Smuzhiyun 	 * so that we can retry it ourselves.  We also
1812*4882a593Smuzhiyun 	 * implement our own throttling mechanism so
1813*4882a593Smuzhiyun 	 * we don't clobber the device with too many
1814*4882a593Smuzhiyun 	 * commands.
1815*4882a593Smuzhiyun 	 */
1816*4882a593Smuzhiyun 	switch (ahc_get_scsi_status(scb)) {
1817*4882a593Smuzhiyun 	default:
1818*4882a593Smuzhiyun 		break;
1819*4882a593Smuzhiyun 	case SCSI_STATUS_CHECK_COND:
1820*4882a593Smuzhiyun 	case SCSI_STATUS_CMD_TERMINATED:
1821*4882a593Smuzhiyun 	{
1822*4882a593Smuzhiyun 		struct scsi_cmnd *cmd;
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 		/*
1825*4882a593Smuzhiyun 		 * Copy sense information to the OS's cmd
1826*4882a593Smuzhiyun 		 * structure if it is available.
1827*4882a593Smuzhiyun 		 */
1828*4882a593Smuzhiyun 		cmd = scb->io_ctx;
1829*4882a593Smuzhiyun 		if (scb->flags & SCB_SENSE) {
1830*4882a593Smuzhiyun 			u_int sense_size;
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun 			sense_size = min(sizeof(struct scsi_sense_data)
1833*4882a593Smuzhiyun 				       - ahc_get_sense_residual(scb),
1834*4882a593Smuzhiyun 					 (u_long)SCSI_SENSE_BUFFERSIZE);
1835*4882a593Smuzhiyun 			memcpy(cmd->sense_buffer,
1836*4882a593Smuzhiyun 			       ahc_get_sense_buf(ahc, scb), sense_size);
1837*4882a593Smuzhiyun 			if (sense_size < SCSI_SENSE_BUFFERSIZE)
1838*4882a593Smuzhiyun 				memset(&cmd->sense_buffer[sense_size], 0,
1839*4882a593Smuzhiyun 				       SCSI_SENSE_BUFFERSIZE - sense_size);
1840*4882a593Smuzhiyun 			cmd->result |= (DRIVER_SENSE << 24);
1841*4882a593Smuzhiyun #ifdef AHC_DEBUG
1842*4882a593Smuzhiyun 			if (ahc_debug & AHC_SHOW_SENSE) {
1843*4882a593Smuzhiyun 				int i;
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun 				printk("Copied %d bytes of sense data:",
1846*4882a593Smuzhiyun 				       sense_size);
1847*4882a593Smuzhiyun 				for (i = 0; i < sense_size; i++) {
1848*4882a593Smuzhiyun 					if ((i & 0xF) == 0)
1849*4882a593Smuzhiyun 						printk("\n");
1850*4882a593Smuzhiyun 					printk("0x%x ", cmd->sense_buffer[i]);
1851*4882a593Smuzhiyun 				}
1852*4882a593Smuzhiyun 				printk("\n");
1853*4882a593Smuzhiyun 			}
1854*4882a593Smuzhiyun #endif
1855*4882a593Smuzhiyun 		}
1856*4882a593Smuzhiyun 		break;
1857*4882a593Smuzhiyun 	}
1858*4882a593Smuzhiyun 	case SCSI_STATUS_QUEUE_FULL:
1859*4882a593Smuzhiyun 	{
1860*4882a593Smuzhiyun 		/*
1861*4882a593Smuzhiyun 		 * By the time the core driver has returned this
1862*4882a593Smuzhiyun 		 * command, all other commands that were queued
1863*4882a593Smuzhiyun 		 * to us but not the device have been returned.
1864*4882a593Smuzhiyun 		 * This ensures that dev->active is equal to
1865*4882a593Smuzhiyun 		 * the number of commands actually queued to
1866*4882a593Smuzhiyun 		 * the device.
1867*4882a593Smuzhiyun 		 */
1868*4882a593Smuzhiyun 		dev->tag_success_count = 0;
1869*4882a593Smuzhiyun 		if (dev->active != 0) {
1870*4882a593Smuzhiyun 			/*
1871*4882a593Smuzhiyun 			 * Drop our opening count to the number
1872*4882a593Smuzhiyun 			 * of commands currently outstanding.
1873*4882a593Smuzhiyun 			 */
1874*4882a593Smuzhiyun 			dev->openings = 0;
1875*4882a593Smuzhiyun /*
1876*4882a593Smuzhiyun 			ahc_print_path(ahc, scb);
1877*4882a593Smuzhiyun 			printk("Dropping tag count to %d\n", dev->active);
1878*4882a593Smuzhiyun  */
1879*4882a593Smuzhiyun 			if (dev->active == dev->tags_on_last_queuefull) {
1880*4882a593Smuzhiyun 
1881*4882a593Smuzhiyun 				dev->last_queuefull_same_count++;
1882*4882a593Smuzhiyun 				/*
1883*4882a593Smuzhiyun 				 * If we repeatedly see a queue full
1884*4882a593Smuzhiyun 				 * at the same queue depth, this
1885*4882a593Smuzhiyun 				 * device has a fixed number of tag
1886*4882a593Smuzhiyun 				 * slots.  Lock in this tag depth
1887*4882a593Smuzhiyun 				 * so we stop seeing queue fulls from
1888*4882a593Smuzhiyun 				 * this device.
1889*4882a593Smuzhiyun 				 */
1890*4882a593Smuzhiyun 				if (dev->last_queuefull_same_count
1891*4882a593Smuzhiyun 				 == AHC_LOCK_TAGS_COUNT) {
1892*4882a593Smuzhiyun 					dev->maxtags = dev->active;
1893*4882a593Smuzhiyun 					ahc_print_path(ahc, scb);
1894*4882a593Smuzhiyun 					printk("Locking max tag count at %d\n",
1895*4882a593Smuzhiyun 					       dev->active);
1896*4882a593Smuzhiyun 				}
1897*4882a593Smuzhiyun 			} else {
1898*4882a593Smuzhiyun 				dev->tags_on_last_queuefull = dev->active;
1899*4882a593Smuzhiyun 				dev->last_queuefull_same_count = 0;
1900*4882a593Smuzhiyun 			}
1901*4882a593Smuzhiyun 			ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
1902*4882a593Smuzhiyun 			ahc_set_scsi_status(scb, SCSI_STATUS_OK);
1903*4882a593Smuzhiyun 			ahc_platform_set_tags(ahc, sdev, &devinfo,
1904*4882a593Smuzhiyun 				     (dev->flags & AHC_DEV_Q_BASIC)
1905*4882a593Smuzhiyun 				   ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
1906*4882a593Smuzhiyun 			break;
1907*4882a593Smuzhiyun 		}
1908*4882a593Smuzhiyun 		/*
1909*4882a593Smuzhiyun 		 * Drop down to a single opening, and treat this
1910*4882a593Smuzhiyun 		 * as if the target returned BUSY SCSI status.
1911*4882a593Smuzhiyun 		 */
1912*4882a593Smuzhiyun 		dev->openings = 1;
1913*4882a593Smuzhiyun 		ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
1914*4882a593Smuzhiyun 		ahc_platform_set_tags(ahc, sdev, &devinfo,
1915*4882a593Smuzhiyun 			     (dev->flags & AHC_DEV_Q_BASIC)
1916*4882a593Smuzhiyun 			   ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
1917*4882a593Smuzhiyun 		break;
1918*4882a593Smuzhiyun 	}
1919*4882a593Smuzhiyun 	}
1920*4882a593Smuzhiyun }
1921*4882a593Smuzhiyun 
1922*4882a593Smuzhiyun static void
ahc_linux_queue_cmd_complete(struct ahc_softc * ahc,struct scsi_cmnd * cmd)1923*4882a593Smuzhiyun ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, struct scsi_cmnd *cmd)
1924*4882a593Smuzhiyun {
1925*4882a593Smuzhiyun 	/*
1926*4882a593Smuzhiyun 	 * Map CAM error codes into Linux Error codes.  We
1927*4882a593Smuzhiyun 	 * avoid the conversion so that the DV code has the
1928*4882a593Smuzhiyun 	 * full error information available when making
1929*4882a593Smuzhiyun 	 * state change decisions.
1930*4882a593Smuzhiyun 	 */
1931*4882a593Smuzhiyun 	{
1932*4882a593Smuzhiyun 		u_int new_status;
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun 		switch (ahc_cmd_get_transaction_status(cmd)) {
1935*4882a593Smuzhiyun 		case CAM_REQ_INPROG:
1936*4882a593Smuzhiyun 		case CAM_REQ_CMP:
1937*4882a593Smuzhiyun 		case CAM_SCSI_STATUS_ERROR:
1938*4882a593Smuzhiyun 			new_status = DID_OK;
1939*4882a593Smuzhiyun 			break;
1940*4882a593Smuzhiyun 		case CAM_REQ_ABORTED:
1941*4882a593Smuzhiyun 			new_status = DID_ABORT;
1942*4882a593Smuzhiyun 			break;
1943*4882a593Smuzhiyun 		case CAM_BUSY:
1944*4882a593Smuzhiyun 			new_status = DID_BUS_BUSY;
1945*4882a593Smuzhiyun 			break;
1946*4882a593Smuzhiyun 		case CAM_REQ_INVALID:
1947*4882a593Smuzhiyun 		case CAM_PATH_INVALID:
1948*4882a593Smuzhiyun 			new_status = DID_BAD_TARGET;
1949*4882a593Smuzhiyun 			break;
1950*4882a593Smuzhiyun 		case CAM_SEL_TIMEOUT:
1951*4882a593Smuzhiyun 			new_status = DID_NO_CONNECT;
1952*4882a593Smuzhiyun 			break;
1953*4882a593Smuzhiyun 		case CAM_SCSI_BUS_RESET:
1954*4882a593Smuzhiyun 		case CAM_BDR_SENT:
1955*4882a593Smuzhiyun 			new_status = DID_RESET;
1956*4882a593Smuzhiyun 			break;
1957*4882a593Smuzhiyun 		case CAM_UNCOR_PARITY:
1958*4882a593Smuzhiyun 			new_status = DID_PARITY;
1959*4882a593Smuzhiyun 			break;
1960*4882a593Smuzhiyun 		case CAM_CMD_TIMEOUT:
1961*4882a593Smuzhiyun 			new_status = DID_TIME_OUT;
1962*4882a593Smuzhiyun 			break;
1963*4882a593Smuzhiyun 		case CAM_UA_ABORT:
1964*4882a593Smuzhiyun 		case CAM_REQ_CMP_ERR:
1965*4882a593Smuzhiyun 		case CAM_AUTOSENSE_FAIL:
1966*4882a593Smuzhiyun 		case CAM_NO_HBA:
1967*4882a593Smuzhiyun 		case CAM_DATA_RUN_ERR:
1968*4882a593Smuzhiyun 		case CAM_UNEXP_BUSFREE:
1969*4882a593Smuzhiyun 		case CAM_SEQUENCE_FAIL:
1970*4882a593Smuzhiyun 		case CAM_CCB_LEN_ERR:
1971*4882a593Smuzhiyun 		case CAM_PROVIDE_FAIL:
1972*4882a593Smuzhiyun 		case CAM_REQ_TERMIO:
1973*4882a593Smuzhiyun 		case CAM_UNREC_HBA_ERROR:
1974*4882a593Smuzhiyun 		case CAM_REQ_TOO_BIG:
1975*4882a593Smuzhiyun 			new_status = DID_ERROR;
1976*4882a593Smuzhiyun 			break;
1977*4882a593Smuzhiyun 		case CAM_REQUEUE_REQ:
1978*4882a593Smuzhiyun 			new_status = DID_REQUEUE;
1979*4882a593Smuzhiyun 			break;
1980*4882a593Smuzhiyun 		default:
1981*4882a593Smuzhiyun 			/* We should never get here */
1982*4882a593Smuzhiyun 			new_status = DID_ERROR;
1983*4882a593Smuzhiyun 			break;
1984*4882a593Smuzhiyun 		}
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun 		ahc_cmd_set_transaction_status(cmd, new_status);
1987*4882a593Smuzhiyun 	}
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	cmd->scsi_done(cmd);
1990*4882a593Smuzhiyun }
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun static void
ahc_linux_freeze_simq(struct ahc_softc * ahc)1993*4882a593Smuzhiyun ahc_linux_freeze_simq(struct ahc_softc *ahc)
1994*4882a593Smuzhiyun {
1995*4882a593Smuzhiyun 	unsigned long s;
1996*4882a593Smuzhiyun 
1997*4882a593Smuzhiyun 	ahc_lock(ahc, &s);
1998*4882a593Smuzhiyun 	ahc->platform_data->qfrozen++;
1999*4882a593Smuzhiyun 	if (ahc->platform_data->qfrozen == 1) {
2000*4882a593Smuzhiyun 		scsi_block_requests(ahc->platform_data->host);
2001*4882a593Smuzhiyun 
2002*4882a593Smuzhiyun 		/* XXX What about Twin channels? */
2003*4882a593Smuzhiyun 		ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
2004*4882a593Smuzhiyun 					CAM_LUN_WILDCARD, SCB_LIST_NULL,
2005*4882a593Smuzhiyun 					ROLE_INITIATOR, CAM_REQUEUE_REQ);
2006*4882a593Smuzhiyun 	}
2007*4882a593Smuzhiyun 	ahc_unlock(ahc, &s);
2008*4882a593Smuzhiyun }
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun static void
ahc_linux_release_simq(struct ahc_softc * ahc)2011*4882a593Smuzhiyun ahc_linux_release_simq(struct ahc_softc *ahc)
2012*4882a593Smuzhiyun {
2013*4882a593Smuzhiyun 	u_long s;
2014*4882a593Smuzhiyun 	int    unblock_reqs;
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	unblock_reqs = 0;
2017*4882a593Smuzhiyun 	ahc_lock(ahc, &s);
2018*4882a593Smuzhiyun 	if (ahc->platform_data->qfrozen > 0)
2019*4882a593Smuzhiyun 		ahc->platform_data->qfrozen--;
2020*4882a593Smuzhiyun 	if (ahc->platform_data->qfrozen == 0)
2021*4882a593Smuzhiyun 		unblock_reqs = 1;
2022*4882a593Smuzhiyun 	ahc_unlock(ahc, &s);
2023*4882a593Smuzhiyun 	/*
2024*4882a593Smuzhiyun 	 * There is still a race here.  The mid-layer
2025*4882a593Smuzhiyun 	 * should keep its own freeze count and use
2026*4882a593Smuzhiyun 	 * a bottom half handler to run the queues
2027*4882a593Smuzhiyun 	 * so we can unblock with our own lock held.
2028*4882a593Smuzhiyun 	 */
2029*4882a593Smuzhiyun 	if (unblock_reqs)
2030*4882a593Smuzhiyun 		scsi_unblock_requests(ahc->platform_data->host);
2031*4882a593Smuzhiyun }
2032*4882a593Smuzhiyun 
2033*4882a593Smuzhiyun static int
ahc_linux_queue_recovery_cmd(struct scsi_cmnd * cmd,scb_flag flag)2034*4882a593Smuzhiyun ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
2035*4882a593Smuzhiyun {
2036*4882a593Smuzhiyun 	struct ahc_softc *ahc;
2037*4882a593Smuzhiyun 	struct ahc_linux_device *dev;
2038*4882a593Smuzhiyun 	struct scb *pending_scb;
2039*4882a593Smuzhiyun 	u_int  saved_scbptr;
2040*4882a593Smuzhiyun 	u_int  active_scb_index;
2041*4882a593Smuzhiyun 	u_int  last_phase;
2042*4882a593Smuzhiyun 	u_int  saved_scsiid;
2043*4882a593Smuzhiyun 	u_int  cdb_byte;
2044*4882a593Smuzhiyun 	int    retval;
2045*4882a593Smuzhiyun 	int    was_paused;
2046*4882a593Smuzhiyun 	int    paused;
2047*4882a593Smuzhiyun 	int    wait;
2048*4882a593Smuzhiyun 	int    disconnected;
2049*4882a593Smuzhiyun 	unsigned long flags;
2050*4882a593Smuzhiyun 
2051*4882a593Smuzhiyun 	pending_scb = NULL;
2052*4882a593Smuzhiyun 	paused = FALSE;
2053*4882a593Smuzhiyun 	wait = FALSE;
2054*4882a593Smuzhiyun 	ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
2055*4882a593Smuzhiyun 
2056*4882a593Smuzhiyun 	scmd_printk(KERN_INFO, cmd, "Attempting to queue a%s message\n",
2057*4882a593Smuzhiyun 	       flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun 	printk("CDB:");
2060*4882a593Smuzhiyun 	for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
2061*4882a593Smuzhiyun 		printk(" 0x%x", cmd->cmnd[cdb_byte]);
2062*4882a593Smuzhiyun 	printk("\n");
2063*4882a593Smuzhiyun 
2064*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
2065*4882a593Smuzhiyun 
2066*4882a593Smuzhiyun 	/*
2067*4882a593Smuzhiyun 	 * First determine if we currently own this command.
2068*4882a593Smuzhiyun 	 * Start by searching the device queue.  If not found
2069*4882a593Smuzhiyun 	 * there, check the pending_scb list.  If not found
2070*4882a593Smuzhiyun 	 * at all, and the system wanted us to just abort the
2071*4882a593Smuzhiyun 	 * command, return success.
2072*4882a593Smuzhiyun 	 */
2073*4882a593Smuzhiyun 	dev = scsi_transport_device_data(cmd->device);
2074*4882a593Smuzhiyun 
2075*4882a593Smuzhiyun 	if (dev == NULL) {
2076*4882a593Smuzhiyun 		/*
2077*4882a593Smuzhiyun 		 * No target device for this command exists,
2078*4882a593Smuzhiyun 		 * so we must not still own the command.
2079*4882a593Smuzhiyun 		 */
2080*4882a593Smuzhiyun 		printk("%s:%d:%d:%d: Is not an active device\n",
2081*4882a593Smuzhiyun 		       ahc_name(ahc), cmd->device->channel, cmd->device->id,
2082*4882a593Smuzhiyun 		       (u8)cmd->device->lun);
2083*4882a593Smuzhiyun 		retval = SUCCESS;
2084*4882a593Smuzhiyun 		goto no_cmd;
2085*4882a593Smuzhiyun 	}
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun 	if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
2088*4882a593Smuzhiyun 	 && ahc_search_untagged_queues(ahc, cmd, cmd->device->id,
2089*4882a593Smuzhiyun 				       cmd->device->channel + 'A',
2090*4882a593Smuzhiyun 				       (u8)cmd->device->lun,
2091*4882a593Smuzhiyun 				       CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
2092*4882a593Smuzhiyun 		printk("%s:%d:%d:%d: Command found on untagged queue\n",
2093*4882a593Smuzhiyun 		       ahc_name(ahc), cmd->device->channel, cmd->device->id,
2094*4882a593Smuzhiyun 		       (u8)cmd->device->lun);
2095*4882a593Smuzhiyun 		retval = SUCCESS;
2096*4882a593Smuzhiyun 		goto done;
2097*4882a593Smuzhiyun 	}
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun 	/*
2100*4882a593Smuzhiyun 	 * See if we can find a matching cmd in the pending list.
2101*4882a593Smuzhiyun 	 */
2102*4882a593Smuzhiyun 	LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2103*4882a593Smuzhiyun 		if (pending_scb->io_ctx == cmd)
2104*4882a593Smuzhiyun 			break;
2105*4882a593Smuzhiyun 	}
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun 	if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
2108*4882a593Smuzhiyun 
2109*4882a593Smuzhiyun 		/* Any SCB for this device will do for a target reset */
2110*4882a593Smuzhiyun 		LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2111*4882a593Smuzhiyun 		  	if (ahc_match_scb(ahc, pending_scb, scmd_id(cmd),
2112*4882a593Smuzhiyun 					  scmd_channel(cmd) + 'A',
2113*4882a593Smuzhiyun 					  CAM_LUN_WILDCARD,
2114*4882a593Smuzhiyun 					  SCB_LIST_NULL, ROLE_INITIATOR))
2115*4882a593Smuzhiyun 				break;
2116*4882a593Smuzhiyun 		}
2117*4882a593Smuzhiyun 	}
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun 	if (pending_scb == NULL) {
2120*4882a593Smuzhiyun 		scmd_printk(KERN_INFO, cmd, "Command not found\n");
2121*4882a593Smuzhiyun 		goto no_cmd;
2122*4882a593Smuzhiyun 	}
2123*4882a593Smuzhiyun 
2124*4882a593Smuzhiyun 	if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
2125*4882a593Smuzhiyun 		/*
2126*4882a593Smuzhiyun 		 * We can't queue two recovery actions using the same SCB
2127*4882a593Smuzhiyun 		 */
2128*4882a593Smuzhiyun 		retval = FAILED;
2129*4882a593Smuzhiyun 		goto  done;
2130*4882a593Smuzhiyun 	}
2131*4882a593Smuzhiyun 
2132*4882a593Smuzhiyun 	/*
2133*4882a593Smuzhiyun 	 * Ensure that the card doesn't do anything
2134*4882a593Smuzhiyun 	 * behind our back and that we didn't "just" miss
2135*4882a593Smuzhiyun 	 * an interrupt that would affect this cmd.
2136*4882a593Smuzhiyun 	 */
2137*4882a593Smuzhiyun 	was_paused = ahc_is_paused(ahc);
2138*4882a593Smuzhiyun 	ahc_pause_and_flushwork(ahc);
2139*4882a593Smuzhiyun 	paused = TRUE;
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun 	if ((pending_scb->flags & SCB_ACTIVE) == 0) {
2142*4882a593Smuzhiyun 		scmd_printk(KERN_INFO, cmd, "Command already completed\n");
2143*4882a593Smuzhiyun 		goto no_cmd;
2144*4882a593Smuzhiyun 	}
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	printk("%s: At time of recovery, card was %spaused\n",
2147*4882a593Smuzhiyun 	       ahc_name(ahc), was_paused ? "" : "not ");
2148*4882a593Smuzhiyun 	ahc_dump_card_state(ahc);
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 	disconnected = TRUE;
2151*4882a593Smuzhiyun 	if (flag == SCB_ABORT) {
2152*4882a593Smuzhiyun 		if (ahc_search_qinfifo(ahc, cmd->device->id,
2153*4882a593Smuzhiyun 				       cmd->device->channel + 'A',
2154*4882a593Smuzhiyun 				       cmd->device->lun,
2155*4882a593Smuzhiyun 				       pending_scb->hscb->tag,
2156*4882a593Smuzhiyun 				       ROLE_INITIATOR, CAM_REQ_ABORTED,
2157*4882a593Smuzhiyun 				       SEARCH_COMPLETE) > 0) {
2158*4882a593Smuzhiyun 			printk("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
2159*4882a593Smuzhiyun 			       ahc_name(ahc), cmd->device->channel,
2160*4882a593Smuzhiyun 			       cmd->device->id, (u8)cmd->device->lun);
2161*4882a593Smuzhiyun 			retval = SUCCESS;
2162*4882a593Smuzhiyun 			goto done;
2163*4882a593Smuzhiyun 		}
2164*4882a593Smuzhiyun 	} else if (ahc_search_qinfifo(ahc, cmd->device->id,
2165*4882a593Smuzhiyun 				      cmd->device->channel + 'A',
2166*4882a593Smuzhiyun 				      cmd->device->lun,
2167*4882a593Smuzhiyun 				      pending_scb->hscb->tag,
2168*4882a593Smuzhiyun 				      ROLE_INITIATOR, /*status*/0,
2169*4882a593Smuzhiyun 				      SEARCH_COUNT) > 0) {
2170*4882a593Smuzhiyun 		disconnected = FALSE;
2171*4882a593Smuzhiyun 	}
2172*4882a593Smuzhiyun 
2173*4882a593Smuzhiyun 	if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
2174*4882a593Smuzhiyun 		struct scb *bus_scb;
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun 		bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
2177*4882a593Smuzhiyun 		if (bus_scb == pending_scb)
2178*4882a593Smuzhiyun 			disconnected = FALSE;
2179*4882a593Smuzhiyun 		else if (flag != SCB_ABORT
2180*4882a593Smuzhiyun 		      && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
2181*4882a593Smuzhiyun 		      && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb))
2182*4882a593Smuzhiyun 			disconnected = FALSE;
2183*4882a593Smuzhiyun 	}
2184*4882a593Smuzhiyun 
2185*4882a593Smuzhiyun 	/*
2186*4882a593Smuzhiyun 	 * At this point, pending_scb is the scb associated with the
2187*4882a593Smuzhiyun 	 * passed in command.  That command is currently active on the
2188*4882a593Smuzhiyun 	 * bus, is in the disconnected state, or we're hoping to find
2189*4882a593Smuzhiyun 	 * a command for the same target active on the bus to abuse to
2190*4882a593Smuzhiyun 	 * send a BDR.  Queue the appropriate message based on which of
2191*4882a593Smuzhiyun 	 * these states we are in.
2192*4882a593Smuzhiyun 	 */
2193*4882a593Smuzhiyun 	last_phase = ahc_inb(ahc, LASTPHASE);
2194*4882a593Smuzhiyun 	saved_scbptr = ahc_inb(ahc, SCBPTR);
2195*4882a593Smuzhiyun 	active_scb_index = ahc_inb(ahc, SCB_TAG);
2196*4882a593Smuzhiyun 	saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
2197*4882a593Smuzhiyun 	if (last_phase != P_BUSFREE
2198*4882a593Smuzhiyun 	 && (pending_scb->hscb->tag == active_scb_index
2199*4882a593Smuzhiyun 	  || (flag == SCB_DEVICE_RESET
2200*4882a593Smuzhiyun 	   && SCSIID_TARGET(ahc, saved_scsiid) == scmd_id(cmd)))) {
2201*4882a593Smuzhiyun 
2202*4882a593Smuzhiyun 		/*
2203*4882a593Smuzhiyun 		 * We're active on the bus, so assert ATN
2204*4882a593Smuzhiyun 		 * and hope that the target responds.
2205*4882a593Smuzhiyun 		 */
2206*4882a593Smuzhiyun 		pending_scb = ahc_lookup_scb(ahc, active_scb_index);
2207*4882a593Smuzhiyun 		pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2208*4882a593Smuzhiyun 		ahc_outb(ahc, MSG_OUT, HOST_MSG);
2209*4882a593Smuzhiyun 		ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
2210*4882a593Smuzhiyun 		scmd_printk(KERN_INFO, cmd, "Device is active, asserting ATN\n");
2211*4882a593Smuzhiyun 		wait = TRUE;
2212*4882a593Smuzhiyun 	} else if (disconnected) {
2213*4882a593Smuzhiyun 
2214*4882a593Smuzhiyun 		/*
2215*4882a593Smuzhiyun 		 * Actually re-queue this SCB in an attempt
2216*4882a593Smuzhiyun 		 * to select the device before it reconnects.
2217*4882a593Smuzhiyun 		 * In either case (selection or reselection),
2218*4882a593Smuzhiyun 		 * we will now issue the approprate message
2219*4882a593Smuzhiyun 		 * to the timed-out device.
2220*4882a593Smuzhiyun 		 *
2221*4882a593Smuzhiyun 		 * Set the MK_MESSAGE control bit indicating
2222*4882a593Smuzhiyun 		 * that we desire to send a message.  We
2223*4882a593Smuzhiyun 		 * also set the disconnected flag since
2224*4882a593Smuzhiyun 		 * in the paging case there is no guarantee
2225*4882a593Smuzhiyun 		 * that our SCB control byte matches the
2226*4882a593Smuzhiyun 		 * version on the card.  We don't want the
2227*4882a593Smuzhiyun 		 * sequencer to abort the command thinking
2228*4882a593Smuzhiyun 		 * an unsolicited reselection occurred.
2229*4882a593Smuzhiyun 		 */
2230*4882a593Smuzhiyun 		pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
2231*4882a593Smuzhiyun 		pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2232*4882a593Smuzhiyun 
2233*4882a593Smuzhiyun 		/*
2234*4882a593Smuzhiyun 		 * Remove any cached copy of this SCB in the
2235*4882a593Smuzhiyun 		 * disconnected list in preparation for the
2236*4882a593Smuzhiyun 		 * queuing of our abort SCB.  We use the
2237*4882a593Smuzhiyun 		 * same element in the SCB, SCB_NEXT, for
2238*4882a593Smuzhiyun 		 * both the qinfifo and the disconnected list.
2239*4882a593Smuzhiyun 		 */
2240*4882a593Smuzhiyun 		ahc_search_disc_list(ahc, cmd->device->id,
2241*4882a593Smuzhiyun 				     cmd->device->channel + 'A',
2242*4882a593Smuzhiyun 				     cmd->device->lun, pending_scb->hscb->tag,
2243*4882a593Smuzhiyun 				     /*stop_on_first*/TRUE,
2244*4882a593Smuzhiyun 				     /*remove*/TRUE,
2245*4882a593Smuzhiyun 				     /*save_state*/FALSE);
2246*4882a593Smuzhiyun 
2247*4882a593Smuzhiyun 		/*
2248*4882a593Smuzhiyun 		 * In the non-paging case, the sequencer will
2249*4882a593Smuzhiyun 		 * never re-reference the in-core SCB.
2250*4882a593Smuzhiyun 		 * To make sure we are notified during
2251*4882a593Smuzhiyun 		 * reselection, set the MK_MESSAGE flag in
2252*4882a593Smuzhiyun 		 * the card's copy of the SCB.
2253*4882a593Smuzhiyun 		 */
2254*4882a593Smuzhiyun 		if ((ahc->flags & AHC_PAGESCBS) == 0) {
2255*4882a593Smuzhiyun 			ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
2256*4882a593Smuzhiyun 			ahc_outb(ahc, SCB_CONTROL,
2257*4882a593Smuzhiyun 				 ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
2258*4882a593Smuzhiyun 		}
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun 		/*
2261*4882a593Smuzhiyun 		 * Clear out any entries in the QINFIFO first
2262*4882a593Smuzhiyun 		 * so we are the next SCB for this target
2263*4882a593Smuzhiyun 		 * to run.
2264*4882a593Smuzhiyun 		 */
2265*4882a593Smuzhiyun 		ahc_search_qinfifo(ahc, cmd->device->id,
2266*4882a593Smuzhiyun 				   cmd->device->channel + 'A',
2267*4882a593Smuzhiyun 				   cmd->device->lun, SCB_LIST_NULL,
2268*4882a593Smuzhiyun 				   ROLE_INITIATOR, CAM_REQUEUE_REQ,
2269*4882a593Smuzhiyun 				   SEARCH_COMPLETE);
2270*4882a593Smuzhiyun 		ahc_qinfifo_requeue_tail(ahc, pending_scb);
2271*4882a593Smuzhiyun 		ahc_outb(ahc, SCBPTR, saved_scbptr);
2272*4882a593Smuzhiyun 		ahc_print_path(ahc, pending_scb);
2273*4882a593Smuzhiyun 		printk("Device is disconnected, re-queuing SCB\n");
2274*4882a593Smuzhiyun 		wait = TRUE;
2275*4882a593Smuzhiyun 	} else {
2276*4882a593Smuzhiyun 		scmd_printk(KERN_INFO, cmd, "Unable to deliver message\n");
2277*4882a593Smuzhiyun 		retval = FAILED;
2278*4882a593Smuzhiyun 		goto done;
2279*4882a593Smuzhiyun 	}
2280*4882a593Smuzhiyun 
2281*4882a593Smuzhiyun no_cmd:
2282*4882a593Smuzhiyun 	/*
2283*4882a593Smuzhiyun 	 * Our assumption is that if we don't have the command, no
2284*4882a593Smuzhiyun 	 * recovery action was required, so we return success.  Again,
2285*4882a593Smuzhiyun 	 * the semantics of the mid-layer recovery engine are not
2286*4882a593Smuzhiyun 	 * well defined, so this may change in time.
2287*4882a593Smuzhiyun 	 */
2288*4882a593Smuzhiyun 	retval = SUCCESS;
2289*4882a593Smuzhiyun done:
2290*4882a593Smuzhiyun 	if (paused)
2291*4882a593Smuzhiyun 		ahc_unpause(ahc);
2292*4882a593Smuzhiyun 	if (wait) {
2293*4882a593Smuzhiyun 		DECLARE_COMPLETION_ONSTACK(done);
2294*4882a593Smuzhiyun 
2295*4882a593Smuzhiyun 		ahc->platform_data->eh_done = &done;
2296*4882a593Smuzhiyun 		ahc_unlock(ahc, &flags);
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun 		printk("Recovery code sleeping\n");
2299*4882a593Smuzhiyun 		if (!wait_for_completion_timeout(&done, 5 * HZ)) {
2300*4882a593Smuzhiyun 			ahc_lock(ahc, &flags);
2301*4882a593Smuzhiyun 			ahc->platform_data->eh_done = NULL;
2302*4882a593Smuzhiyun 			ahc_unlock(ahc, &flags);
2303*4882a593Smuzhiyun 
2304*4882a593Smuzhiyun 			printk("Timer Expired\n");
2305*4882a593Smuzhiyun 			retval = FAILED;
2306*4882a593Smuzhiyun 		}
2307*4882a593Smuzhiyun 		printk("Recovery code awake\n");
2308*4882a593Smuzhiyun 	} else
2309*4882a593Smuzhiyun 		ahc_unlock(ahc, &flags);
2310*4882a593Smuzhiyun 	return (retval);
2311*4882a593Smuzhiyun }
2312*4882a593Smuzhiyun 
ahc_linux_set_width(struct scsi_target * starget,int width)2313*4882a593Smuzhiyun static void ahc_linux_set_width(struct scsi_target *starget, int width)
2314*4882a593Smuzhiyun {
2315*4882a593Smuzhiyun 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2316*4882a593Smuzhiyun 	struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2317*4882a593Smuzhiyun 	struct ahc_devinfo devinfo;
2318*4882a593Smuzhiyun 	unsigned long flags;
2319*4882a593Smuzhiyun 
2320*4882a593Smuzhiyun 	ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2321*4882a593Smuzhiyun 			    starget->channel + 'A', ROLE_INITIATOR);
2322*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
2323*4882a593Smuzhiyun 	ahc_set_width(ahc, &devinfo, width, AHC_TRANS_GOAL, FALSE);
2324*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
2325*4882a593Smuzhiyun }
2326*4882a593Smuzhiyun 
ahc_linux_set_period(struct scsi_target * starget,int period)2327*4882a593Smuzhiyun static void ahc_linux_set_period(struct scsi_target *starget, int period)
2328*4882a593Smuzhiyun {
2329*4882a593Smuzhiyun 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2330*4882a593Smuzhiyun 	struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2331*4882a593Smuzhiyun 	struct ahc_tmode_tstate *tstate;
2332*4882a593Smuzhiyun 	struct ahc_initiator_tinfo *tinfo
2333*4882a593Smuzhiyun 		= ahc_fetch_transinfo(ahc,
2334*4882a593Smuzhiyun 				      starget->channel + 'A',
2335*4882a593Smuzhiyun 				      shost->this_id, starget->id, &tstate);
2336*4882a593Smuzhiyun 	struct ahc_devinfo devinfo;
2337*4882a593Smuzhiyun 	unsigned int ppr_options = tinfo->goal.ppr_options;
2338*4882a593Smuzhiyun 	unsigned long flags;
2339*4882a593Smuzhiyun 	unsigned long offset = tinfo->goal.offset;
2340*4882a593Smuzhiyun 	const struct ahc_syncrate *syncrate;
2341*4882a593Smuzhiyun 
2342*4882a593Smuzhiyun 	if (offset == 0)
2343*4882a593Smuzhiyun 		offset = MAX_OFFSET;
2344*4882a593Smuzhiyun 
2345*4882a593Smuzhiyun 	if (period < 9)
2346*4882a593Smuzhiyun 		period = 9;	/* 12.5ns is our minimum */
2347*4882a593Smuzhiyun 	if (period == 9) {
2348*4882a593Smuzhiyun 		if (spi_max_width(starget))
2349*4882a593Smuzhiyun 			ppr_options |= MSG_EXT_PPR_DT_REQ;
2350*4882a593Smuzhiyun 		else
2351*4882a593Smuzhiyun 			/* need wide for DT and need DT for 12.5 ns */
2352*4882a593Smuzhiyun 			period = 10;
2353*4882a593Smuzhiyun 	}
2354*4882a593Smuzhiyun 
2355*4882a593Smuzhiyun 	ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2356*4882a593Smuzhiyun 			    starget->channel + 'A', ROLE_INITIATOR);
2357*4882a593Smuzhiyun 
2358*4882a593Smuzhiyun 	/* all PPR requests apart from QAS require wide transfers */
2359*4882a593Smuzhiyun 	if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
2360*4882a593Smuzhiyun 		if (spi_width(starget) == 0)
2361*4882a593Smuzhiyun 			ppr_options &= MSG_EXT_PPR_QAS_REQ;
2362*4882a593Smuzhiyun 	}
2363*4882a593Smuzhiyun 
2364*4882a593Smuzhiyun 	syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
2365*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
2366*4882a593Smuzhiyun 	ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
2367*4882a593Smuzhiyun 			 ppr_options, AHC_TRANS_GOAL, FALSE);
2368*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
2369*4882a593Smuzhiyun }
2370*4882a593Smuzhiyun 
ahc_linux_set_offset(struct scsi_target * starget,int offset)2371*4882a593Smuzhiyun static void ahc_linux_set_offset(struct scsi_target *starget, int offset)
2372*4882a593Smuzhiyun {
2373*4882a593Smuzhiyun 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2374*4882a593Smuzhiyun 	struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2375*4882a593Smuzhiyun 	struct ahc_tmode_tstate *tstate;
2376*4882a593Smuzhiyun 	struct ahc_initiator_tinfo *tinfo
2377*4882a593Smuzhiyun 		= ahc_fetch_transinfo(ahc,
2378*4882a593Smuzhiyun 				      starget->channel + 'A',
2379*4882a593Smuzhiyun 				      shost->this_id, starget->id, &tstate);
2380*4882a593Smuzhiyun 	struct ahc_devinfo devinfo;
2381*4882a593Smuzhiyun 	unsigned int ppr_options = 0;
2382*4882a593Smuzhiyun 	unsigned int period = 0;
2383*4882a593Smuzhiyun 	unsigned long flags;
2384*4882a593Smuzhiyun 	const struct ahc_syncrate *syncrate = NULL;
2385*4882a593Smuzhiyun 
2386*4882a593Smuzhiyun 	ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2387*4882a593Smuzhiyun 			    starget->channel + 'A', ROLE_INITIATOR);
2388*4882a593Smuzhiyun 	if (offset != 0) {
2389*4882a593Smuzhiyun 		syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
2390*4882a593Smuzhiyun 		period = tinfo->goal.period;
2391*4882a593Smuzhiyun 		ppr_options = tinfo->goal.ppr_options;
2392*4882a593Smuzhiyun 	}
2393*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
2394*4882a593Smuzhiyun 	ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
2395*4882a593Smuzhiyun 			 ppr_options, AHC_TRANS_GOAL, FALSE);
2396*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
2397*4882a593Smuzhiyun }
2398*4882a593Smuzhiyun 
ahc_linux_set_dt(struct scsi_target * starget,int dt)2399*4882a593Smuzhiyun static void ahc_linux_set_dt(struct scsi_target *starget, int dt)
2400*4882a593Smuzhiyun {
2401*4882a593Smuzhiyun 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2402*4882a593Smuzhiyun 	struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2403*4882a593Smuzhiyun 	struct ahc_tmode_tstate *tstate;
2404*4882a593Smuzhiyun 	struct ahc_initiator_tinfo *tinfo
2405*4882a593Smuzhiyun 		= ahc_fetch_transinfo(ahc,
2406*4882a593Smuzhiyun 				      starget->channel + 'A',
2407*4882a593Smuzhiyun 				      shost->this_id, starget->id, &tstate);
2408*4882a593Smuzhiyun 	struct ahc_devinfo devinfo;
2409*4882a593Smuzhiyun 	unsigned int ppr_options = tinfo->goal.ppr_options
2410*4882a593Smuzhiyun 		& ~MSG_EXT_PPR_DT_REQ;
2411*4882a593Smuzhiyun 	unsigned int period = tinfo->goal.period;
2412*4882a593Smuzhiyun 	unsigned int width = tinfo->goal.width;
2413*4882a593Smuzhiyun 	unsigned long flags;
2414*4882a593Smuzhiyun 	const struct ahc_syncrate *syncrate;
2415*4882a593Smuzhiyun 
2416*4882a593Smuzhiyun 	if (dt && spi_max_width(starget)) {
2417*4882a593Smuzhiyun 		ppr_options |= MSG_EXT_PPR_DT_REQ;
2418*4882a593Smuzhiyun 		if (!width)
2419*4882a593Smuzhiyun 			ahc_linux_set_width(starget, 1);
2420*4882a593Smuzhiyun 	} else if (period == 9)
2421*4882a593Smuzhiyun 		period = 10;	/* if resetting DT, period must be >= 25ns */
2422*4882a593Smuzhiyun 
2423*4882a593Smuzhiyun 	ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2424*4882a593Smuzhiyun 			    starget->channel + 'A', ROLE_INITIATOR);
2425*4882a593Smuzhiyun 	syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,AHC_SYNCRATE_DT);
2426*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
2427*4882a593Smuzhiyun 	ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
2428*4882a593Smuzhiyun 			 ppr_options, AHC_TRANS_GOAL, FALSE);
2429*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
2430*4882a593Smuzhiyun }
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun #if 0
2433*4882a593Smuzhiyun /* FIXME: This code claims to support IU and QAS.  However, the actual
2434*4882a593Smuzhiyun  * sequencer code and aic7xxx_core have no support for these parameters and
2435*4882a593Smuzhiyun  * will get into a bad state if they're negotiated.  Do not enable this
2436*4882a593Smuzhiyun  * unless you know what you're doing */
2437*4882a593Smuzhiyun static void ahc_linux_set_qas(struct scsi_target *starget, int qas)
2438*4882a593Smuzhiyun {
2439*4882a593Smuzhiyun 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2440*4882a593Smuzhiyun 	struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2441*4882a593Smuzhiyun 	struct ahc_tmode_tstate *tstate;
2442*4882a593Smuzhiyun 	struct ahc_initiator_tinfo *tinfo
2443*4882a593Smuzhiyun 		= ahc_fetch_transinfo(ahc,
2444*4882a593Smuzhiyun 				      starget->channel + 'A',
2445*4882a593Smuzhiyun 				      shost->this_id, starget->id, &tstate);
2446*4882a593Smuzhiyun 	struct ahc_devinfo devinfo;
2447*4882a593Smuzhiyun 	unsigned int ppr_options = tinfo->goal.ppr_options
2448*4882a593Smuzhiyun 		& ~MSG_EXT_PPR_QAS_REQ;
2449*4882a593Smuzhiyun 	unsigned int period = tinfo->goal.period;
2450*4882a593Smuzhiyun 	unsigned long flags;
2451*4882a593Smuzhiyun 	struct ahc_syncrate *syncrate;
2452*4882a593Smuzhiyun 
2453*4882a593Smuzhiyun 	if (qas)
2454*4882a593Smuzhiyun 		ppr_options |= MSG_EXT_PPR_QAS_REQ;
2455*4882a593Smuzhiyun 
2456*4882a593Smuzhiyun 	ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2457*4882a593Smuzhiyun 			    starget->channel + 'A', ROLE_INITIATOR);
2458*4882a593Smuzhiyun 	syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
2459*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
2460*4882a593Smuzhiyun 	ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
2461*4882a593Smuzhiyun 			 ppr_options, AHC_TRANS_GOAL, FALSE);
2462*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
2463*4882a593Smuzhiyun }
2464*4882a593Smuzhiyun 
2465*4882a593Smuzhiyun static void ahc_linux_set_iu(struct scsi_target *starget, int iu)
2466*4882a593Smuzhiyun {
2467*4882a593Smuzhiyun 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2468*4882a593Smuzhiyun 	struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2469*4882a593Smuzhiyun 	struct ahc_tmode_tstate *tstate;
2470*4882a593Smuzhiyun 	struct ahc_initiator_tinfo *tinfo
2471*4882a593Smuzhiyun 		= ahc_fetch_transinfo(ahc,
2472*4882a593Smuzhiyun 				      starget->channel + 'A',
2473*4882a593Smuzhiyun 				      shost->this_id, starget->id, &tstate);
2474*4882a593Smuzhiyun 	struct ahc_devinfo devinfo;
2475*4882a593Smuzhiyun 	unsigned int ppr_options = tinfo->goal.ppr_options
2476*4882a593Smuzhiyun 		& ~MSG_EXT_PPR_IU_REQ;
2477*4882a593Smuzhiyun 	unsigned int period = tinfo->goal.period;
2478*4882a593Smuzhiyun 	unsigned long flags;
2479*4882a593Smuzhiyun 	struct ahc_syncrate *syncrate;
2480*4882a593Smuzhiyun 
2481*4882a593Smuzhiyun 	if (iu)
2482*4882a593Smuzhiyun 		ppr_options |= MSG_EXT_PPR_IU_REQ;
2483*4882a593Smuzhiyun 
2484*4882a593Smuzhiyun 	ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2485*4882a593Smuzhiyun 			    starget->channel + 'A', ROLE_INITIATOR);
2486*4882a593Smuzhiyun 	syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
2487*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
2488*4882a593Smuzhiyun 	ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
2489*4882a593Smuzhiyun 			 ppr_options, AHC_TRANS_GOAL, FALSE);
2490*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
2491*4882a593Smuzhiyun }
2492*4882a593Smuzhiyun #endif
2493*4882a593Smuzhiyun 
ahc_linux_get_signalling(struct Scsi_Host * shost)2494*4882a593Smuzhiyun static void ahc_linux_get_signalling(struct Scsi_Host *shost)
2495*4882a593Smuzhiyun {
2496*4882a593Smuzhiyun 	struct ahc_softc *ahc = *(struct ahc_softc **)shost->hostdata;
2497*4882a593Smuzhiyun 	unsigned long flags;
2498*4882a593Smuzhiyun 	u8 mode;
2499*4882a593Smuzhiyun 
2500*4882a593Smuzhiyun 	if (!(ahc->features & AHC_ULTRA2)) {
2501*4882a593Smuzhiyun 		/* non-LVD chipset, may not have SBLKCTL reg */
2502*4882a593Smuzhiyun 		spi_signalling(shost) =
2503*4882a593Smuzhiyun 			ahc->features & AHC_HVD ?
2504*4882a593Smuzhiyun 			SPI_SIGNAL_HVD :
2505*4882a593Smuzhiyun 			SPI_SIGNAL_SE;
2506*4882a593Smuzhiyun 		return;
2507*4882a593Smuzhiyun 	}
2508*4882a593Smuzhiyun 
2509*4882a593Smuzhiyun 	ahc_lock(ahc, &flags);
2510*4882a593Smuzhiyun 	ahc_pause(ahc);
2511*4882a593Smuzhiyun 	mode = ahc_inb(ahc, SBLKCTL);
2512*4882a593Smuzhiyun 	ahc_unpause(ahc);
2513*4882a593Smuzhiyun 	ahc_unlock(ahc, &flags);
2514*4882a593Smuzhiyun 
2515*4882a593Smuzhiyun 	if (mode & ENAB40)
2516*4882a593Smuzhiyun 		spi_signalling(shost) = SPI_SIGNAL_LVD;
2517*4882a593Smuzhiyun 	else if (mode & ENAB20)
2518*4882a593Smuzhiyun 		spi_signalling(shost) = SPI_SIGNAL_SE;
2519*4882a593Smuzhiyun 	else
2520*4882a593Smuzhiyun 		spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
2521*4882a593Smuzhiyun }
2522*4882a593Smuzhiyun 
2523*4882a593Smuzhiyun static struct spi_function_template ahc_linux_transport_functions = {
2524*4882a593Smuzhiyun 	.set_offset	= ahc_linux_set_offset,
2525*4882a593Smuzhiyun 	.show_offset	= 1,
2526*4882a593Smuzhiyun 	.set_period	= ahc_linux_set_period,
2527*4882a593Smuzhiyun 	.show_period	= 1,
2528*4882a593Smuzhiyun 	.set_width	= ahc_linux_set_width,
2529*4882a593Smuzhiyun 	.show_width	= 1,
2530*4882a593Smuzhiyun 	.set_dt		= ahc_linux_set_dt,
2531*4882a593Smuzhiyun 	.show_dt	= 1,
2532*4882a593Smuzhiyun #if 0
2533*4882a593Smuzhiyun 	.set_iu		= ahc_linux_set_iu,
2534*4882a593Smuzhiyun 	.show_iu	= 1,
2535*4882a593Smuzhiyun 	.set_qas	= ahc_linux_set_qas,
2536*4882a593Smuzhiyun 	.show_qas	= 1,
2537*4882a593Smuzhiyun #endif
2538*4882a593Smuzhiyun 	.get_signalling	= ahc_linux_get_signalling,
2539*4882a593Smuzhiyun };
2540*4882a593Smuzhiyun 
2541*4882a593Smuzhiyun 
2542*4882a593Smuzhiyun 
2543*4882a593Smuzhiyun static int __init
ahc_linux_init(void)2544*4882a593Smuzhiyun ahc_linux_init(void)
2545*4882a593Smuzhiyun {
2546*4882a593Smuzhiyun 	/*
2547*4882a593Smuzhiyun 	 * If we've been passed any parameters, process them now.
2548*4882a593Smuzhiyun 	 */
2549*4882a593Smuzhiyun 	if (aic7xxx)
2550*4882a593Smuzhiyun 		aic7xxx_setup(aic7xxx);
2551*4882a593Smuzhiyun 
2552*4882a593Smuzhiyun 	ahc_linux_transport_template =
2553*4882a593Smuzhiyun 		spi_attach_transport(&ahc_linux_transport_functions);
2554*4882a593Smuzhiyun 	if (!ahc_linux_transport_template)
2555*4882a593Smuzhiyun 		return -ENODEV;
2556*4882a593Smuzhiyun 
2557*4882a593Smuzhiyun 	scsi_transport_reserve_device(ahc_linux_transport_template,
2558*4882a593Smuzhiyun 				      sizeof(struct ahc_linux_device));
2559*4882a593Smuzhiyun 
2560*4882a593Smuzhiyun 	ahc_linux_pci_init();
2561*4882a593Smuzhiyun 	ahc_linux_eisa_init();
2562*4882a593Smuzhiyun 	return 0;
2563*4882a593Smuzhiyun }
2564*4882a593Smuzhiyun 
2565*4882a593Smuzhiyun static void
ahc_linux_exit(void)2566*4882a593Smuzhiyun ahc_linux_exit(void)
2567*4882a593Smuzhiyun {
2568*4882a593Smuzhiyun 	ahc_linux_pci_exit();
2569*4882a593Smuzhiyun 	ahc_linux_eisa_exit();
2570*4882a593Smuzhiyun 	spi_release_transport(ahc_linux_transport_template);
2571*4882a593Smuzhiyun }
2572*4882a593Smuzhiyun 
2573*4882a593Smuzhiyun module_init(ahc_linux_init);
2574*4882a593Smuzhiyun module_exit(ahc_linux_exit);
2575