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