xref: /linux/drivers/scsi/aic7xxx/aic79xx_osm.c (revision 9cc9ef28199d9f964cc42e8a0978216bd9c2ba83)
1 /*
2  * Adaptec AIC79xx device driver for Linux.
3  *
4  * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic79xx_osm.c#171 $
5  *
6  * --------------------------------------------------------------------------
7  * Copyright (c) 1994-2000 Justin T. Gibbs.
8  * Copyright (c) 1997-1999 Doug Ledford
9  * Copyright (c) 2000-2003 Adaptec Inc.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44 
45 #include "aic79xx_osm.h"
46 #include "aic79xx_inline.h"
47 #include <scsi/scsicam.h>
48 
49 static struct scsi_transport_template *ahd_linux_transport_template = NULL;
50 
51 #include <linux/init.h>		/* __setup */
52 #include <linux/mm.h>		/* For fetching system memory size */
53 #include <linux/blkdev.h>		/* For block_size() */
54 #include <linux/delay.h>	/* For ssleep/msleep */
55 #include <linux/device.h>
56 #include <linux/slab.h>
57 
58 /*
59  * Bucket size for counting good commands in between bad ones.
60  */
61 #define AHD_LINUX_ERR_THRESH	1000
62 
63 /*
64  * Set this to the delay in seconds after SCSI bus reset.
65  * Note, we honor this only for the initial bus reset.
66  * The scsi error recovery code performs its own bus settle
67  * delay handling for error recovery actions.
68  */
69 #ifdef CONFIG_AIC79XX_RESET_DELAY_MS
70 #define AIC79XX_RESET_DELAY CONFIG_AIC79XX_RESET_DELAY_MS
71 #else
72 #define AIC79XX_RESET_DELAY 5000
73 #endif
74 
75 /*
76  * To change the default number of tagged transactions allowed per-device,
77  * add a line to the lilo.conf file like:
78  * append="aic79xx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
79  * which will result in the first four devices on the first two
80  * controllers being set to a tagged queue depth of 32.
81  *
82  * The tag_commands is an array of 16 to allow for wide and twin adapters.
83  * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
84  * for channel 1.
85  */
86 typedef struct {
87 	uint16_t tag_commands[16];	/* Allow for wide/twin adapters. */
88 } adapter_tag_info_t;
89 
90 /*
91  * Modify this as you see fit for your system.
92  *
93  * 0			tagged queuing disabled
94  * 1 <= n <= 253	n == max tags ever dispatched.
95  *
96  * The driver will throttle the number of commands dispatched to a
97  * device if it returns queue full.  For devices with a fixed maximum
98  * queue depth, the driver will eventually determine this depth and
99  * lock it in (a console message is printed to indicate that a lock
100  * has occurred).  On some devices, queue full is returned for a temporary
101  * resource shortage.  These devices will return queue full at varying
102  * depths.  The driver will throttle back when the queue fulls occur and
103  * attempt to slowly increase the depth over time as the device recovers
104  * from the resource shortage.
105  *
106  * In this example, the first line will disable tagged queueing for all
107  * the devices on the first probed aic79xx adapter.
108  *
109  * The second line enables tagged queueing with 4 commands/LUN for IDs
110  * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
111  * driver to attempt to use up to 64 tags for ID 1.
112  *
113  * The third line is the same as the first line.
114  *
115  * The fourth line disables tagged queueing for devices 0 and 3.  It
116  * enables tagged queueing for the other IDs, with 16 commands/LUN
117  * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
118  * IDs 2, 5-7, and 9-15.
119  */
120 
121 /*
122  * NOTE: The below structure is for reference only, the actual structure
123  *       to modify in order to change things is just below this comment block.
124 adapter_tag_info_t aic79xx_tag_info[] =
125 {
126 	{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
127 	{{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
128 	{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
129 	{{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
130 };
131 */
132 
133 #ifdef CONFIG_AIC79XX_CMDS_PER_DEVICE
134 #define AIC79XX_CMDS_PER_DEVICE CONFIG_AIC79XX_CMDS_PER_DEVICE
135 #else
136 #define AIC79XX_CMDS_PER_DEVICE AHD_MAX_QUEUE
137 #endif
138 
139 #define AIC79XX_CONFIGED_TAG_COMMANDS {					\
140 	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
141 	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
142 	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
143 	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
144 	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
145 	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
146 	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE,		\
147 	AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE		\
148 }
149 
150 /*
151  * By default, use the number of commands specified by
152  * the users kernel configuration.
153  */
154 static adapter_tag_info_t aic79xx_tag_info[] =
155 {
156 	{AIC79XX_CONFIGED_TAG_COMMANDS},
157 	{AIC79XX_CONFIGED_TAG_COMMANDS},
158 	{AIC79XX_CONFIGED_TAG_COMMANDS},
159 	{AIC79XX_CONFIGED_TAG_COMMANDS},
160 	{AIC79XX_CONFIGED_TAG_COMMANDS},
161 	{AIC79XX_CONFIGED_TAG_COMMANDS},
162 	{AIC79XX_CONFIGED_TAG_COMMANDS},
163 	{AIC79XX_CONFIGED_TAG_COMMANDS},
164 	{AIC79XX_CONFIGED_TAG_COMMANDS},
165 	{AIC79XX_CONFIGED_TAG_COMMANDS},
166 	{AIC79XX_CONFIGED_TAG_COMMANDS},
167 	{AIC79XX_CONFIGED_TAG_COMMANDS},
168 	{AIC79XX_CONFIGED_TAG_COMMANDS},
169 	{AIC79XX_CONFIGED_TAG_COMMANDS},
170 	{AIC79XX_CONFIGED_TAG_COMMANDS},
171 	{AIC79XX_CONFIGED_TAG_COMMANDS}
172 };
173 
174 /*
175  * The I/O cell on the chip is very configurable in respect to its analog
176  * characteristics.  Set the defaults here; they can be overriden with
177  * the proper insmod parameters.
178  */
179 struct ahd_linux_iocell_opts
180 {
181 	uint8_t	precomp;
182 	uint8_t	slewrate;
183 	uint8_t amplitude;
184 };
185 #define AIC79XX_DEFAULT_PRECOMP		0xFF
186 #define AIC79XX_DEFAULT_SLEWRATE	0xFF
187 #define AIC79XX_DEFAULT_AMPLITUDE	0xFF
188 #define AIC79XX_DEFAULT_IOOPTS			\
189 {						\
190 	AIC79XX_DEFAULT_PRECOMP,		\
191 	AIC79XX_DEFAULT_SLEWRATE,		\
192 	AIC79XX_DEFAULT_AMPLITUDE		\
193 }
194 #define AIC79XX_PRECOMP_INDEX	0
195 #define AIC79XX_SLEWRATE_INDEX	1
196 #define AIC79XX_AMPLITUDE_INDEX	2
197 static struct ahd_linux_iocell_opts aic79xx_iocell_info[] __ro_after_init =
198 {
199 	AIC79XX_DEFAULT_IOOPTS,
200 	AIC79XX_DEFAULT_IOOPTS,
201 	AIC79XX_DEFAULT_IOOPTS,
202 	AIC79XX_DEFAULT_IOOPTS,
203 	AIC79XX_DEFAULT_IOOPTS,
204 	AIC79XX_DEFAULT_IOOPTS,
205 	AIC79XX_DEFAULT_IOOPTS,
206 	AIC79XX_DEFAULT_IOOPTS,
207 	AIC79XX_DEFAULT_IOOPTS,
208 	AIC79XX_DEFAULT_IOOPTS,
209 	AIC79XX_DEFAULT_IOOPTS,
210 	AIC79XX_DEFAULT_IOOPTS,
211 	AIC79XX_DEFAULT_IOOPTS,
212 	AIC79XX_DEFAULT_IOOPTS,
213 	AIC79XX_DEFAULT_IOOPTS,
214 	AIC79XX_DEFAULT_IOOPTS
215 };
216 
217 /*
218  * There should be a specific return value for this in scsi.h, but
219  * it seems that most drivers ignore it.
220  */
221 #define DID_UNDERFLOW   DID_ERROR
222 
223 void
224 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
225 {
226 	printk("(scsi%d:%c:%d:%d): ",
227 	       ahd->platform_data->host->host_no,
228 	       scb != NULL ? SCB_GET_CHANNEL(ahd, scb) : 'X',
229 	       scb != NULL ? SCB_GET_TARGET(ahd, scb) : -1,
230 	       scb != NULL ? SCB_GET_LUN(scb) : -1);
231 }
232 
233 /*
234  * XXX - these options apply unilaterally to _all_ adapters
235  *       cards in the system.  This should be fixed.  Exceptions to this
236  *       rule are noted in the comments.
237  */
238 
239 /*
240  * Skip the scsi bus reset.  Non 0 make us skip the reset at startup.  This
241  * has no effect on any later resets that might occur due to things like
242  * SCSI bus timeouts.
243  */
244 static uint32_t aic79xx_no_reset;
245 
246 /*
247  * Should we force EXTENDED translation on a controller.
248  *     0 == Use whatever is in the SEEPROM or default to off
249  *     1 == Use whatever is in the SEEPROM or default to on
250  */
251 static uint32_t aic79xx_extended;
252 
253 /*
254  * PCI bus parity checking of the Adaptec controllers.  This is somewhat
255  * dubious at best.  To my knowledge, this option has never actually
256  * solved a PCI parity problem, but on certain machines with broken PCI
257  * chipset configurations, it can generate tons of false error messages.
258  * It's included in the driver for completeness.
259  *   0	   = Shut off PCI parity check
260  *   non-0 = Enable PCI parity check
261  *
262  * NOTE: you can't actually pass -1 on the lilo prompt.  So, to set this
263  * variable to -1 you would actually want to simply pass the variable
264  * name without a number.  That will invert the 0 which will result in
265  * -1.
266  */
267 static uint32_t aic79xx_pci_parity = ~0;
268 
269 /*
270  * There are lots of broken chipsets in the world.  Some of them will
271  * violate the PCI spec when we issue byte sized memory writes to our
272  * controller.  I/O mapped register access, if allowed by the given
273  * platform, will work in almost all cases.
274  */
275 uint32_t aic79xx_allow_memio = ~0;
276 
277 /*
278  * So that we can set how long each device is given as a selection timeout.
279  * The table of values goes like this:
280  *   0 - 256ms
281  *   1 - 128ms
282  *   2 - 64ms
283  *   3 - 32ms
284  * We default to 256ms because some older devices need a longer time
285  * to respond to initial selection.
286  */
287 static uint32_t aic79xx_seltime;
288 
289 /*
290  * Certain devices do not perform any aging on commands.  Should the
291  * device be saturated by commands in one portion of the disk, it is
292  * possible for transactions on far away sectors to never be serviced.
293  * To handle these devices, we can periodically send an ordered tag to
294  * force all outstanding transactions to be serviced prior to a new
295  * transaction.
296  */
297 static uint32_t aic79xx_periodic_otag;
298 
299 /* Some storage boxes are using an LSI chip which has a bug making it
300  * impossible to use aic79xx Rev B chip in 320 speeds.  The following
301  * storage boxes have been reported to be buggy:
302  * EonStor 3U 16-Bay: U16U-G3A3
303  * EonStor 2U 12-Bay: U12U-G3A3
304  * SentinelRAID: 2500F R5 / R6
305  * SentinelRAID: 2500F R1
306  * SentinelRAID: 2500F/1500F
307  * SentinelRAID: 150F
308  *
309  * To get around this LSI bug, you can set your board to 160 mode
310  * or you can enable the SLOWCRC bit.
311  */
312 uint32_t aic79xx_slowcrc;
313 
314 /*
315  * Module information and settable options.
316  */
317 static char *aic79xx = NULL;
318 
319 MODULE_AUTHOR("Maintainer: Hannes Reinecke <hare@suse.de>");
320 MODULE_DESCRIPTION("Adaptec AIC790X U320 SCSI Host Bus Adapter driver");
321 MODULE_LICENSE("Dual BSD/GPL");
322 MODULE_VERSION(AIC79XX_DRIVER_VERSION);
323 module_param(aic79xx, charp, 0444);
324 MODULE_PARM_DESC(aic79xx,
325 "period-delimited options string:\n"
326 "	verbose			Enable verbose/diagnostic logging\n"
327 "	allow_memio		Allow device registers to be memory mapped\n"
328 "	debug			Bitmask of debug values to enable\n"
329 "	no_reset		Suppress initial bus resets\n"
330 "	extended		Enable extended geometry on all controllers\n"
331 "	periodic_otag		Send an ordered tagged transaction\n"
332 "				periodically to prevent tag starvation.\n"
333 "				This may be required by some older disk\n"
334 "				or drives/RAID arrays.\n"
335 "	tag_info:<tag_str>	Set per-target tag depth\n"
336 "	global_tag_depth:<int>	Global tag depth for all targets on all buses\n"
337 "	slewrate:<slewrate_list>Set the signal slew rate (0-15).\n"
338 "	precomp:<pcomp_list>	Set the signal precompensation (0-7).\n"
339 "	amplitude:<int>		Set the signal amplitude (0-7).\n"
340 "	seltime:<int>		Selection Timeout:\n"
341 "				(0/256ms,1/128ms,2/64ms,3/32ms)\n"
342 "	slowcrc			Turn on the SLOWCRC bit (Rev B only)\n"
343 "\n"
344 "	Sample modprobe configuration file:\n"
345 "	#	Enable verbose logging\n"
346 "	#	Set tag depth on Controller 2/Target 2 to 10 tags\n"
347 "	#	Shorten the selection timeout to 128ms\n"
348 "\n"
349 "	options aic79xx 'aic79xx=verbose.tag_info:{{}.{}.{..10}}.seltime:1'\n"
350 );
351 
352 static void ahd_linux_handle_scsi_status(struct ahd_softc *,
353 					 struct scsi_device *,
354 					 struct scb *);
355 static void ahd_linux_queue_cmd_complete(struct ahd_softc *ahd,
356 					 struct scsi_cmnd *cmd);
357 static int ahd_linux_queue_abort_cmd(struct scsi_cmnd *cmd);
358 static void ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd);
359 static u_int ahd_linux_user_tagdepth(struct ahd_softc *ahd,
360 				     struct ahd_devinfo *devinfo);
361 static void ahd_linux_device_queue_depth(struct scsi_device *);
362 static int ahd_linux_run_command(struct ahd_softc*,
363 				 struct ahd_linux_device *,
364 				 struct scsi_cmnd *);
365 static void ahd_linux_setup_tag_info_global(char *p);
366 static int  aic79xx_setup(char *c);
367 static void ahd_freeze_simq(struct ahd_softc *ahd);
368 static void ahd_release_simq(struct ahd_softc *ahd);
369 
370 static int ahd_linux_unit;
371 
372 
373 /************************** OS Utility Wrappers *******************************/
374 void ahd_delay(long);
375 void
376 ahd_delay(long usec)
377 {
378 	/*
379 	 * udelay on Linux can have problems for
380 	 * multi-millisecond waits.  Wait at most
381 	 * 1024us per call.
382 	 */
383 	while (usec > 0) {
384 		udelay(usec % 1024);
385 		usec -= 1024;
386 	}
387 }
388 
389 
390 /***************************** Low Level I/O **********************************/
391 uint8_t ahd_inb(struct ahd_softc * ahd, long port);
392 void ahd_outb(struct ahd_softc * ahd, long port, uint8_t val);
393 void ahd_outw_atomic(struct ahd_softc * ahd,
394 				     long port, uint16_t val);
395 void ahd_outsb(struct ahd_softc * ahd, long port,
396 			       uint8_t *, int count);
397 void ahd_insb(struct ahd_softc * ahd, long port,
398 			       uint8_t *, int count);
399 
400 uint8_t
401 ahd_inb(struct ahd_softc * ahd, long port)
402 {
403 	uint8_t x;
404 
405 	if (ahd->tags[0] == BUS_SPACE_MEMIO) {
406 		x = readb(ahd->bshs[0].maddr + port);
407 	} else {
408 		x = inb(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
409 	}
410 	mb();
411 	return (x);
412 }
413 
414 #if 0 /* unused */
415 static uint16_t
416 ahd_inw_atomic(struct ahd_softc * ahd, long port)
417 {
418 	uint8_t x;
419 
420 	if (ahd->tags[0] == BUS_SPACE_MEMIO) {
421 		x = readw(ahd->bshs[0].maddr + port);
422 	} else {
423 		x = inw(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
424 	}
425 	mb();
426 	return (x);
427 }
428 #endif
429 
430 void
431 ahd_outb(struct ahd_softc * ahd, long port, uint8_t val)
432 {
433 	if (ahd->tags[0] == BUS_SPACE_MEMIO) {
434 		writeb(val, ahd->bshs[0].maddr + port);
435 	} else {
436 		outb(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
437 	}
438 	mb();
439 }
440 
441 void
442 ahd_outw_atomic(struct ahd_softc * ahd, long port, uint16_t val)
443 {
444 	if (ahd->tags[0] == BUS_SPACE_MEMIO) {
445 		writew(val, ahd->bshs[0].maddr + port);
446 	} else {
447 		outw(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
448 	}
449 	mb();
450 }
451 
452 void
453 ahd_outsb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
454 {
455 	int i;
456 
457 	/*
458 	 * There is probably a more efficient way to do this on Linux
459 	 * but we don't use this for anything speed critical and this
460 	 * should work.
461 	 */
462 	for (i = 0; i < count; i++)
463 		ahd_outb(ahd, port, *array++);
464 }
465 
466 void
467 ahd_insb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
468 {
469 	int i;
470 
471 	/*
472 	 * There is probably a more efficient way to do this on Linux
473 	 * but we don't use this for anything speed critical and this
474 	 * should work.
475 	 */
476 	for (i = 0; i < count; i++)
477 		*array++ = ahd_inb(ahd, port);
478 }
479 
480 /******************************* PCI Routines *********************************/
481 uint32_t
482 ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
483 {
484 	switch (width) {
485 	case 1:
486 	{
487 		uint8_t retval;
488 
489 		pci_read_config_byte(pci, reg, &retval);
490 		return (retval);
491 	}
492 	case 2:
493 	{
494 		uint16_t retval;
495 		pci_read_config_word(pci, reg, &retval);
496 		return (retval);
497 	}
498 	case 4:
499 	{
500 		uint32_t retval;
501 		pci_read_config_dword(pci, reg, &retval);
502 		return (retval);
503 	}
504 	default:
505 		panic("ahd_pci_read_config: Read size too big");
506 		/* NOTREACHED */
507 		return (0);
508 	}
509 }
510 
511 void
512 ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
513 {
514 	switch (width) {
515 	case 1:
516 		pci_write_config_byte(pci, reg, value);
517 		break;
518 	case 2:
519 		pci_write_config_word(pci, reg, value);
520 		break;
521 	case 4:
522 		pci_write_config_dword(pci, reg, value);
523 		break;
524 	default:
525 		panic("ahd_pci_write_config: Write size too big");
526 		/* NOTREACHED */
527 	}
528 }
529 
530 /****************************** Inlines ***************************************/
531 static void ahd_linux_unmap_scb(struct ahd_softc*, struct scb*);
532 
533 static void
534 ahd_linux_unmap_scb(struct ahd_softc *ahd, struct scb *scb)
535 {
536 	struct scsi_cmnd *cmd;
537 
538 	cmd = scb->io_ctx;
539 	ahd_sync_sglist(ahd, scb, BUS_DMASYNC_POSTWRITE);
540 	scsi_dma_unmap(cmd);
541 }
542 
543 /******************************** Macros **************************************/
544 static inline unsigned int ahd_build_scsiid(struct ahd_softc *ahd,
545 					    struct scsi_device *sdev)
546 {
547 	return ((sdev_id(sdev) << TID_SHIFT) & TID) | (ahd)->our_id;
548 }
549 
550 /*
551  * Return a string describing the driver.
552  */
553 static const char *
554 ahd_linux_info(struct Scsi_Host *host)
555 {
556 	static char buffer[512];
557 	char	ahd_info[256];
558 	char   *bp;
559 	struct ahd_softc *ahd;
560 
561 	bp = &buffer[0];
562 	ahd = *(struct ahd_softc **)host->hostdata;
563 	memset(bp, 0, sizeof(buffer));
564 	strcpy(bp, "Adaptec AIC79XX PCI-X SCSI HBA DRIVER, Rev " AIC79XX_DRIVER_VERSION "\n"
565 			"        <");
566 	strcat(bp, ahd->description);
567 	strcat(bp, ">\n"
568 			"        ");
569 	ahd_controller_info(ahd, ahd_info);
570 	strcat(bp, ahd_info);
571 
572 	return (bp);
573 }
574 
575 /*
576  * Queue an SCB to the controller.
577  */
578 static int ahd_linux_queue_lck(struct scsi_cmnd *cmd)
579 {
580 	struct	 ahd_softc *ahd;
581 	struct	 ahd_linux_device *dev = scsi_transport_device_data(cmd->device);
582 	int rtn = SCSI_MLQUEUE_HOST_BUSY;
583 
584 	ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
585 
586 	cmd->result = CAM_REQ_INPROG << 16;
587 	rtn = ahd_linux_run_command(ahd, dev, cmd);
588 
589 	return rtn;
590 }
591 
592 static DEF_SCSI_QCMD(ahd_linux_queue)
593 
594 static struct scsi_target **
595 ahd_linux_target_in_softc(struct scsi_target *starget)
596 {
597 	struct	ahd_softc *ahd =
598 		*((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
599 	unsigned int target_offset;
600 
601 	target_offset = starget->id;
602 	if (starget->channel != 0)
603 		target_offset += 8;
604 
605 	return &ahd->platform_data->starget[target_offset];
606 }
607 
608 static int
609 ahd_linux_target_alloc(struct scsi_target *starget)
610 {
611 	struct	ahd_softc *ahd =
612 		*((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
613 	struct seeprom_config *sc = ahd->seep_config;
614 	unsigned long flags;
615 	struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
616 	struct ahd_devinfo devinfo;
617 	struct ahd_initiator_tinfo *tinfo;
618 	struct ahd_tmode_tstate *tstate;
619 	char channel = starget->channel + 'A';
620 
621 	ahd_lock(ahd, &flags);
622 
623 	BUG_ON(*ahd_targp != NULL);
624 
625 	*ahd_targp = starget;
626 
627 	if (sc) {
628 		int flags = sc->device_flags[starget->id];
629 
630 		tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
631 					    starget->id, &tstate);
632 
633 		if ((flags  & CFPACKETIZED) == 0) {
634 			/* don't negotiate packetized (IU) transfers */
635 			spi_max_iu(starget) = 0;
636 		} else {
637 			if ((ahd->features & AHD_RTI) == 0)
638 				spi_rti(starget) = 0;
639 		}
640 
641 		if ((flags & CFQAS) == 0)
642 			spi_max_qas(starget) = 0;
643 
644 		/* Transinfo values have been set to BIOS settings */
645 		spi_max_width(starget) = (flags & CFWIDEB) ? 1 : 0;
646 		spi_min_period(starget) = tinfo->user.period;
647 		spi_max_offset(starget) = tinfo->user.offset;
648 	}
649 
650 	tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
651 				    starget->id, &tstate);
652 	ahd_compile_devinfo(&devinfo, ahd->our_id, starget->id,
653 			    CAM_LUN_WILDCARD, channel,
654 			    ROLE_INITIATOR);
655 	ahd_set_syncrate(ahd, &devinfo, 0, 0, 0,
656 			 AHD_TRANS_GOAL, /*paused*/FALSE);
657 	ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
658 		      AHD_TRANS_GOAL, /*paused*/FALSE);
659 	ahd_unlock(ahd, &flags);
660 
661 	return 0;
662 }
663 
664 static void
665 ahd_linux_target_destroy(struct scsi_target *starget)
666 {
667 	struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
668 
669 	*ahd_targp = NULL;
670 }
671 
672 static int
673 ahd_linux_slave_alloc(struct scsi_device *sdev)
674 {
675 	struct	ahd_softc *ahd =
676 		*((struct ahd_softc **)sdev->host->hostdata);
677 	struct ahd_linux_device *dev;
678 
679 	if (bootverbose)
680 		printk("%s: Slave Alloc %d\n", ahd_name(ahd), sdev->id);
681 
682 	dev = scsi_transport_device_data(sdev);
683 	memset(dev, 0, sizeof(*dev));
684 
685 	/*
686 	 * We start out life using untagged
687 	 * transactions of which we allow one.
688 	 */
689 	dev->openings = 1;
690 
691 	/*
692 	 * Set maxtags to 0.  This will be changed if we
693 	 * later determine that we are dealing with
694 	 * a tagged queuing capable device.
695 	 */
696 	dev->maxtags = 0;
697 
698 	return (0);
699 }
700 
701 static int
702 ahd_linux_slave_configure(struct scsi_device *sdev)
703 {
704 	if (bootverbose)
705 		sdev_printk(KERN_INFO, sdev, "Slave Configure\n");
706 
707 	ahd_linux_device_queue_depth(sdev);
708 
709 	/* Initial Domain Validation */
710 	if (!spi_initial_dv(sdev->sdev_target))
711 		spi_dv_device(sdev);
712 
713 	return 0;
714 }
715 
716 #if defined(__i386__)
717 /*
718  * Return the disk geometry for the given SCSI device.
719  */
720 static int
721 ahd_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
722 		    sector_t capacity, int geom[])
723 {
724 	int	 heads;
725 	int	 sectors;
726 	int	 cylinders;
727 	int	 extended;
728 	struct	 ahd_softc *ahd;
729 
730 	ahd = *((struct ahd_softc **)sdev->host->hostdata);
731 
732 	if (scsi_partsize(bdev, capacity, geom))
733 		return 0;
734 
735 	heads = 64;
736 	sectors = 32;
737 	cylinders = aic_sector_div(capacity, heads, sectors);
738 
739 	if (aic79xx_extended != 0)
740 		extended = 1;
741 	else
742 		extended = (ahd->flags & AHD_EXTENDED_TRANS_A) != 0;
743 	if (extended && cylinders >= 1024) {
744 		heads = 255;
745 		sectors = 63;
746 		cylinders = aic_sector_div(capacity, heads, sectors);
747 	}
748 	geom[0] = heads;
749 	geom[1] = sectors;
750 	geom[2] = cylinders;
751 	return (0);
752 }
753 #endif
754 
755 /*
756  * Abort the current SCSI command(s).
757  */
758 static int
759 ahd_linux_abort(struct scsi_cmnd *cmd)
760 {
761 	return ahd_linux_queue_abort_cmd(cmd);
762 }
763 
764 /*
765  * Attempt to send a target reset message to the device that timed out.
766  */
767 static int
768 ahd_linux_dev_reset(struct scsi_cmnd *cmd)
769 {
770 	struct ahd_softc *ahd;
771 	struct ahd_linux_device *dev;
772 	struct scb *reset_scb;
773 	u_int  cdb_byte;
774 	int    retval = SUCCESS;
775 	struct	ahd_initiator_tinfo *tinfo;
776 	struct	ahd_tmode_tstate *tstate;
777 	unsigned long flags;
778 	DECLARE_COMPLETION_ONSTACK(done);
779 
780 	reset_scb = NULL;
781 
782 	ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
783 
784 	scmd_printk(KERN_INFO, cmd,
785 		    "Attempting to queue a TARGET RESET message:");
786 
787 	printk("CDB:");
788 	for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
789 		printk(" 0x%x", cmd->cmnd[cdb_byte]);
790 	printk("\n");
791 
792 	/*
793 	 * Determine if we currently own this command.
794 	 */
795 	dev = scsi_transport_device_data(cmd->device);
796 
797 	if (dev == NULL) {
798 		/*
799 		 * No target device for this command exists,
800 		 * so we must not still own the command.
801 		 */
802 		scmd_printk(KERN_INFO, cmd, "Is not an active device\n");
803 		return SUCCESS;
804 	}
805 
806 	/*
807 	 * Generate us a new SCB
808 	 */
809 	reset_scb = ahd_get_scb(ahd, AHD_NEVER_COL_IDX);
810 	if (!reset_scb) {
811 		scmd_printk(KERN_INFO, cmd, "No SCB available\n");
812 		return FAILED;
813 	}
814 
815 	tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
816 				    cmd->device->id, &tstate);
817 	reset_scb->io_ctx = cmd;
818 	reset_scb->platform_data->dev = dev;
819 	reset_scb->sg_count = 0;
820 	ahd_set_residual(reset_scb, 0);
821 	ahd_set_sense_residual(reset_scb, 0);
822 	reset_scb->platform_data->xfer_len = 0;
823 	reset_scb->hscb->control = 0;
824 	reset_scb->hscb->scsiid = ahd_build_scsiid(ahd, cmd->device);
825 	reset_scb->hscb->lun = cmd->device->lun;
826 	reset_scb->hscb->cdb_len = 0;
827 	reset_scb->hscb->task_management = SIU_TASKMGMT_LUN_RESET;
828 	reset_scb->flags |= SCB_DEVICE_RESET|SCB_RECOVERY_SCB|SCB_ACTIVE;
829 	if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
830 		reset_scb->flags |= SCB_PACKETIZED;
831 	} else {
832 		reset_scb->hscb->control |= MK_MESSAGE;
833 	}
834 	dev->openings--;
835 	dev->active++;
836 	dev->commands_issued++;
837 
838 	ahd_lock(ahd, &flags);
839 
840 	LIST_INSERT_HEAD(&ahd->pending_scbs, reset_scb, pending_links);
841 	ahd_queue_scb(ahd, reset_scb);
842 
843 	ahd->platform_data->eh_done = &done;
844 	ahd_unlock(ahd, &flags);
845 
846 	printk("%s: Device reset code sleeping\n", ahd_name(ahd));
847 	if (!wait_for_completion_timeout(&done, 5 * HZ)) {
848 		ahd_lock(ahd, &flags);
849 		ahd->platform_data->eh_done = NULL;
850 		ahd_unlock(ahd, &flags);
851 		printk("%s: Device reset timer expired (active %d)\n",
852 		       ahd_name(ahd), dev->active);
853 		retval = FAILED;
854 	}
855 	printk("%s: Device reset returning 0x%x\n", ahd_name(ahd), retval);
856 
857 	return (retval);
858 }
859 
860 /*
861  * Reset the SCSI bus.
862  */
863 static int
864 ahd_linux_bus_reset(struct scsi_cmnd *cmd)
865 {
866 	struct ahd_softc *ahd;
867 	int    found;
868 	unsigned long flags;
869 
870 	ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
871 #ifdef AHD_DEBUG
872 	if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
873 		printk("%s: Bus reset called for cmd %p\n",
874 		       ahd_name(ahd), cmd);
875 #endif
876 	ahd_lock(ahd, &flags);
877 
878 	found = ahd_reset_channel(ahd, scmd_channel(cmd) + 'A',
879 				  /*initiate reset*/TRUE);
880 	ahd_unlock(ahd, &flags);
881 
882 	if (bootverbose)
883 		printk("%s: SCSI bus reset delivered. "
884 		       "%d SCBs aborted.\n", ahd_name(ahd), found);
885 
886 	return (SUCCESS);
887 }
888 
889 struct scsi_host_template aic79xx_driver_template = {
890 	.module			= THIS_MODULE,
891 	.name			= "aic79xx",
892 	.proc_name		= "aic79xx",
893 	.show_info		= ahd_linux_show_info,
894 	.write_info	 	= ahd_proc_write_seeprom,
895 	.info			= ahd_linux_info,
896 	.queuecommand		= ahd_linux_queue,
897 	.eh_abort_handler	= ahd_linux_abort,
898 	.eh_device_reset_handler = ahd_linux_dev_reset,
899 	.eh_bus_reset_handler	= ahd_linux_bus_reset,
900 #if defined(__i386__)
901 	.bios_param		= ahd_linux_biosparam,
902 #endif
903 	.can_queue		= AHD_MAX_QUEUE,
904 	.this_id		= -1,
905 	.max_sectors		= 8192,
906 	.cmd_per_lun		= 2,
907 	.slave_alloc		= ahd_linux_slave_alloc,
908 	.slave_configure	= ahd_linux_slave_configure,
909 	.target_alloc		= ahd_linux_target_alloc,
910 	.target_destroy		= ahd_linux_target_destroy,
911 };
912 
913 /******************************** Bus DMA *************************************/
914 int
915 ahd_dma_tag_create(struct ahd_softc *ahd, bus_dma_tag_t parent,
916 		   bus_size_t alignment, bus_size_t boundary,
917 		   dma_addr_t lowaddr, dma_addr_t highaddr,
918 		   bus_dma_filter_t *filter, void *filterarg,
919 		   bus_size_t maxsize, int nsegments,
920 		   bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
921 {
922 	bus_dma_tag_t dmat;
923 
924 	dmat = kmalloc(sizeof(*dmat), GFP_ATOMIC);
925 	if (dmat == NULL)
926 		return (ENOMEM);
927 
928 	/*
929 	 * Linux is very simplistic about DMA memory.  For now don't
930 	 * maintain all specification information.  Once Linux supplies
931 	 * better facilities for doing these operations, or the
932 	 * needs of this particular driver change, we might need to do
933 	 * more here.
934 	 */
935 	dmat->alignment = alignment;
936 	dmat->boundary = boundary;
937 	dmat->maxsize = maxsize;
938 	*ret_tag = dmat;
939 	return (0);
940 }
941 
942 void
943 ahd_dma_tag_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat)
944 {
945 	kfree(dmat);
946 }
947 
948 int
949 ahd_dmamem_alloc(struct ahd_softc *ahd, bus_dma_tag_t dmat, void** vaddr,
950 		 int flags, bus_dmamap_t *mapp)
951 {
952 	*vaddr = dma_alloc_coherent(&ahd->dev_softc->dev, dmat->maxsize, mapp,
953 				    GFP_ATOMIC);
954 	if (*vaddr == NULL)
955 		return (ENOMEM);
956 	return(0);
957 }
958 
959 void
960 ahd_dmamem_free(struct ahd_softc *ahd, bus_dma_tag_t dmat,
961 		void* vaddr, bus_dmamap_t map)
962 {
963 	dma_free_coherent(&ahd->dev_softc->dev, dmat->maxsize, vaddr, map);
964 }
965 
966 int
967 ahd_dmamap_load(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map,
968 		void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
969 		void *cb_arg, int flags)
970 {
971 	/*
972 	 * Assume for now that this will only be used during
973 	 * initialization and not for per-transaction buffer mapping.
974 	 */
975 	bus_dma_segment_t stack_sg;
976 
977 	stack_sg.ds_addr = map;
978 	stack_sg.ds_len = dmat->maxsize;
979 	cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
980 	return (0);
981 }
982 
983 void
984 ahd_dmamap_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
985 {
986 }
987 
988 int
989 ahd_dmamap_unload(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
990 {
991 	/* Nothing to do */
992 	return (0);
993 }
994 
995 /********************* Platform Dependent Functions ***************************/
996 static void
997 ahd_linux_setup_iocell_info(u_long index, int instance, int targ, int32_t value)
998 {
999 
1000 	if ((instance >= 0)
1001 	 && (instance < ARRAY_SIZE(aic79xx_iocell_info))) {
1002 		uint8_t *iocell_info;
1003 
1004 		iocell_info = (uint8_t*)&aic79xx_iocell_info[instance];
1005 		iocell_info[index] = value & 0xFFFF;
1006 		if (bootverbose)
1007 			printk("iocell[%d:%ld] = %d\n", instance, index, value);
1008 	}
1009 }
1010 
1011 static void
1012 ahd_linux_setup_tag_info_global(char *p)
1013 {
1014 	int tags, i, j;
1015 
1016 	tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
1017 	printk("Setting Global Tags= %d\n", tags);
1018 
1019 	for (i = 0; i < ARRAY_SIZE(aic79xx_tag_info); i++) {
1020 		for (j = 0; j < AHD_NUM_TARGETS; j++) {
1021 			aic79xx_tag_info[i].tag_commands[j] = tags;
1022 		}
1023 	}
1024 }
1025 
1026 static void
1027 ahd_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
1028 {
1029 
1030 	if ((instance >= 0) && (targ >= 0)
1031 	 && (instance < ARRAY_SIZE(aic79xx_tag_info))
1032 	 && (targ < AHD_NUM_TARGETS)) {
1033 		aic79xx_tag_info[instance].tag_commands[targ] = value & 0x1FF;
1034 		if (bootverbose)
1035 			printk("tag_info[%d:%d] = %d\n", instance, targ, value);
1036 	}
1037 }
1038 
1039 static char *
1040 ahd_parse_brace_option(char *opt_name, char *opt_arg, char *end, int depth,
1041 		       void (*callback)(u_long, int, int, int32_t),
1042 		       u_long callback_arg)
1043 {
1044 	char	*tok_end;
1045 	char	*tok_end2;
1046 	int      i;
1047 	int      instance;
1048 	int	 targ;
1049 	int	 done;
1050 	char	 tok_list[] = {'.', ',', '{', '}', '\0'};
1051 
1052 	/* All options use a ':' name/arg separator */
1053 	if (*opt_arg != ':')
1054 		return (opt_arg);
1055 	opt_arg++;
1056 	instance = -1;
1057 	targ = -1;
1058 	done = FALSE;
1059 	/*
1060 	 * Restore separator that may be in
1061 	 * the middle of our option argument.
1062 	 */
1063 	tok_end = strchr(opt_arg, '\0');
1064 	if (tok_end < end)
1065 		*tok_end = ',';
1066 	while (!done) {
1067 		switch (*opt_arg) {
1068 		case '{':
1069 			if (instance == -1) {
1070 				instance = 0;
1071 			} else {
1072 				if (depth > 1) {
1073 					if (targ == -1)
1074 						targ = 0;
1075 				} else {
1076 					printk("Malformed Option %s\n",
1077 					       opt_name);
1078 					done = TRUE;
1079 				}
1080 			}
1081 			opt_arg++;
1082 			break;
1083 		case '}':
1084 			if (targ != -1)
1085 				targ = -1;
1086 			else if (instance != -1)
1087 				instance = -1;
1088 			opt_arg++;
1089 			break;
1090 		case ',':
1091 		case '.':
1092 			if (instance == -1)
1093 				done = TRUE;
1094 			else if (targ >= 0)
1095 				targ++;
1096 			else if (instance >= 0)
1097 				instance++;
1098 			opt_arg++;
1099 			break;
1100 		case '\0':
1101 			done = TRUE;
1102 			break;
1103 		default:
1104 			tok_end = end;
1105 			for (i = 0; tok_list[i]; i++) {
1106 				tok_end2 = strchr(opt_arg, tok_list[i]);
1107 				if ((tok_end2) && (tok_end2 < tok_end))
1108 					tok_end = tok_end2;
1109 			}
1110 			callback(callback_arg, instance, targ,
1111 				 simple_strtol(opt_arg, NULL, 0));
1112 			opt_arg = tok_end;
1113 			break;
1114 		}
1115 	}
1116 	return (opt_arg);
1117 }
1118 
1119 /*
1120  * Handle Linux boot parameters. This routine allows for assigning a value
1121  * to a parameter with a ':' between the parameter and the value.
1122  * ie. aic79xx=stpwlev:1,extended
1123  */
1124 static int
1125 aic79xx_setup(char *s)
1126 {
1127 	int	i, n;
1128 	char   *p;
1129 	char   *end;
1130 
1131 	static const struct {
1132 		const char *name;
1133 		uint32_t *flag;
1134 	} options[] = {
1135 		{ "extended", &aic79xx_extended },
1136 		{ "no_reset", &aic79xx_no_reset },
1137 		{ "verbose", &aic79xx_verbose },
1138 		{ "allow_memio", &aic79xx_allow_memio},
1139 #ifdef AHD_DEBUG
1140 		{ "debug", &ahd_debug },
1141 #endif
1142 		{ "periodic_otag", &aic79xx_periodic_otag },
1143 		{ "pci_parity", &aic79xx_pci_parity },
1144 		{ "seltime", &aic79xx_seltime },
1145 		{ "tag_info", NULL },
1146 		{ "global_tag_depth", NULL},
1147 		{ "slewrate", NULL },
1148 		{ "precomp", NULL },
1149 		{ "amplitude", NULL },
1150 		{ "slowcrc", &aic79xx_slowcrc },
1151 	};
1152 
1153 	end = strchr(s, '\0');
1154 
1155 	/*
1156 	 * XXX ia64 gcc isn't smart enough to know that ARRAY_SIZE
1157 	 * will never be 0 in this case.
1158 	 */
1159 	n = 0;
1160 
1161 	while ((p = strsep(&s, ",.")) != NULL) {
1162 		if (*p == '\0')
1163 			continue;
1164 		for (i = 0; i < ARRAY_SIZE(options); i++) {
1165 
1166 			n = strlen(options[i].name);
1167 			if (strncmp(options[i].name, p, n) == 0)
1168 				break;
1169 		}
1170 		if (i == ARRAY_SIZE(options))
1171 			continue;
1172 
1173 		if (strncmp(p, "global_tag_depth", n) == 0) {
1174 			ahd_linux_setup_tag_info_global(p + n);
1175 		} else if (strncmp(p, "tag_info", n) == 0) {
1176 			s = ahd_parse_brace_option("tag_info", p + n, end,
1177 			    2, ahd_linux_setup_tag_info, 0);
1178 		} else if (strncmp(p, "slewrate", n) == 0) {
1179 			s = ahd_parse_brace_option("slewrate",
1180 			    p + n, end, 1, ahd_linux_setup_iocell_info,
1181 			    AIC79XX_SLEWRATE_INDEX);
1182 		} else if (strncmp(p, "precomp", n) == 0) {
1183 			s = ahd_parse_brace_option("precomp",
1184 			    p + n, end, 1, ahd_linux_setup_iocell_info,
1185 			    AIC79XX_PRECOMP_INDEX);
1186 		} else if (strncmp(p, "amplitude", n) == 0) {
1187 			s = ahd_parse_brace_option("amplitude",
1188 			    p + n, end, 1, ahd_linux_setup_iocell_info,
1189 			    AIC79XX_AMPLITUDE_INDEX);
1190 		} else if (p[n] == ':') {
1191 			*(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
1192 		} else if (!strncmp(p, "verbose", n)) {
1193 			*(options[i].flag) = 1;
1194 		} else {
1195 			*(options[i].flag) ^= 0xFFFFFFFF;
1196 		}
1197 	}
1198 	return 1;
1199 }
1200 
1201 __setup("aic79xx=", aic79xx_setup);
1202 
1203 uint32_t aic79xx_verbose;
1204 
1205 int
1206 ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *template)
1207 {
1208 	char	buf[80];
1209 	struct	Scsi_Host *host;
1210 	char	*new_name;
1211 	u_long	s;
1212 	int	retval;
1213 
1214 	template->name = ahd->description;
1215 	host = scsi_host_alloc(template, sizeof(struct ahd_softc *));
1216 	if (host == NULL)
1217 		return (ENOMEM);
1218 
1219 	*((struct ahd_softc **)host->hostdata) = ahd;
1220 	ahd->platform_data->host = host;
1221 	host->can_queue = AHD_MAX_QUEUE;
1222 	host->cmd_per_lun = 2;
1223 	host->sg_tablesize = AHD_NSEG;
1224 	host->this_id = ahd->our_id;
1225 	host->irq = ahd->platform_data->irq;
1226 	host->max_id = (ahd->features & AHD_WIDE) ? 16 : 8;
1227 	host->max_lun = AHD_NUM_LUNS;
1228 	host->max_channel = 0;
1229 	host->sg_tablesize = AHD_NSEG;
1230 	ahd_lock(ahd, &s);
1231 	ahd_set_unit(ahd, ahd_linux_unit++);
1232 	ahd_unlock(ahd, &s);
1233 	sprintf(buf, "scsi%d", host->host_no);
1234 	new_name = kmalloc(strlen(buf) + 1, GFP_ATOMIC);
1235 	if (new_name != NULL) {
1236 		strcpy(new_name, buf);
1237 		ahd_set_name(ahd, new_name);
1238 	}
1239 	host->unique_id = ahd->unit;
1240 	ahd_linux_initialize_scsi_bus(ahd);
1241 	ahd_intr_enable(ahd, TRUE);
1242 
1243 	host->transportt = ahd_linux_transport_template;
1244 
1245 	retval = scsi_add_host(host, &ahd->dev_softc->dev);
1246 	if (retval) {
1247 		printk(KERN_WARNING "aic79xx: scsi_add_host failed\n");
1248 		scsi_host_put(host);
1249 		return retval;
1250 	}
1251 
1252 	scsi_scan_host(host);
1253 	return 0;
1254 }
1255 
1256 /*
1257  * Place the SCSI bus into a known state by either resetting it,
1258  * or forcing transfer negotiations on the next command to any
1259  * target.
1260  */
1261 static void
1262 ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd)
1263 {
1264 	u_int target_id;
1265 	u_int numtarg;
1266 	unsigned long s;
1267 
1268 	target_id = 0;
1269 	numtarg = 0;
1270 
1271 	if (aic79xx_no_reset != 0)
1272 		ahd->flags &= ~AHD_RESET_BUS_A;
1273 
1274 	if ((ahd->flags & AHD_RESET_BUS_A) != 0)
1275 		ahd_reset_channel(ahd, 'A', /*initiate_reset*/TRUE);
1276 	else
1277 		numtarg = (ahd->features & AHD_WIDE) ? 16 : 8;
1278 
1279 	ahd_lock(ahd, &s);
1280 
1281 	/*
1282 	 * Force negotiation to async for all targets that
1283 	 * will not see an initial bus reset.
1284 	 */
1285 	for (; target_id < numtarg; target_id++) {
1286 		struct ahd_devinfo devinfo;
1287 		struct ahd_initiator_tinfo *tinfo;
1288 		struct ahd_tmode_tstate *tstate;
1289 
1290 		tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
1291 					    target_id, &tstate);
1292 		ahd_compile_devinfo(&devinfo, ahd->our_id, target_id,
1293 				    CAM_LUN_WILDCARD, 'A', ROLE_INITIATOR);
1294 		ahd_update_neg_request(ahd, &devinfo, tstate,
1295 				       tinfo, AHD_NEG_ALWAYS);
1296 	}
1297 	ahd_unlock(ahd, &s);
1298 	/* Give the bus some time to recover */
1299 	if ((ahd->flags & AHD_RESET_BUS_A) != 0) {
1300 		ahd_freeze_simq(ahd);
1301 		msleep(AIC79XX_RESET_DELAY);
1302 		ahd_release_simq(ahd);
1303 	}
1304 }
1305 
1306 int
1307 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1308 {
1309 	ahd->platform_data =
1310 	    kzalloc(sizeof(struct ahd_platform_data), GFP_ATOMIC);
1311 	if (ahd->platform_data == NULL)
1312 		return (ENOMEM);
1313 	ahd->platform_data->irq = AHD_LINUX_NOIRQ;
1314 	ahd_lockinit(ahd);
1315 	ahd->seltime = (aic79xx_seltime & 0x3) << 4;
1316 	return (0);
1317 }
1318 
1319 void
1320 ahd_platform_free(struct ahd_softc *ahd)
1321 {
1322 	struct scsi_target *starget;
1323 	int i;
1324 
1325 	if (ahd->platform_data != NULL) {
1326 		/* destroy all of the device and target objects */
1327 		for (i = 0; i < AHD_NUM_TARGETS; i++) {
1328 			starget = ahd->platform_data->starget[i];
1329 			if (starget != NULL) {
1330 				ahd->platform_data->starget[i] = NULL;
1331 			}
1332 		}
1333 
1334 		if (ahd->platform_data->irq != AHD_LINUX_NOIRQ)
1335 			free_irq(ahd->platform_data->irq, ahd);
1336 		if (ahd->tags[0] == BUS_SPACE_PIO
1337 		 && ahd->bshs[0].ioport != 0)
1338 			release_region(ahd->bshs[0].ioport, 256);
1339 		if (ahd->tags[1] == BUS_SPACE_PIO
1340 		 && ahd->bshs[1].ioport != 0)
1341 			release_region(ahd->bshs[1].ioport, 256);
1342 		if (ahd->tags[0] == BUS_SPACE_MEMIO
1343 		 && ahd->bshs[0].maddr != NULL) {
1344 			iounmap(ahd->bshs[0].maddr);
1345 			release_mem_region(ahd->platform_data->mem_busaddr,
1346 					   0x1000);
1347 		}
1348 		if (ahd->platform_data->host)
1349 			scsi_host_put(ahd->platform_data->host);
1350 
1351 		kfree(ahd->platform_data);
1352 	}
1353 }
1354 
1355 void
1356 ahd_platform_init(struct ahd_softc *ahd)
1357 {
1358 	/*
1359 	 * Lookup and commit any modified IO Cell options.
1360 	 */
1361 	if (ahd->unit < ARRAY_SIZE(aic79xx_iocell_info)) {
1362 		const struct ahd_linux_iocell_opts *iocell_opts;
1363 
1364 		iocell_opts = &aic79xx_iocell_info[ahd->unit];
1365 		if (iocell_opts->precomp != AIC79XX_DEFAULT_PRECOMP)
1366 			AHD_SET_PRECOMP(ahd, iocell_opts->precomp);
1367 		if (iocell_opts->slewrate != AIC79XX_DEFAULT_SLEWRATE)
1368 			AHD_SET_SLEWRATE(ahd, iocell_opts->slewrate);
1369 		if (iocell_opts->amplitude != AIC79XX_DEFAULT_AMPLITUDE)
1370 			AHD_SET_AMPLITUDE(ahd, iocell_opts->amplitude);
1371 	}
1372 
1373 }
1374 
1375 void
1376 ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
1377 {
1378 	ahd_platform_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1379 				SCB_GET_CHANNEL(ahd, scb),
1380 				SCB_GET_LUN(scb), SCB_LIST_NULL,
1381 				ROLE_UNKNOWN, CAM_REQUEUE_REQ);
1382 }
1383 
1384 void
1385 ahd_platform_set_tags(struct ahd_softc *ahd, struct scsi_device *sdev,
1386 		      struct ahd_devinfo *devinfo, ahd_queue_alg alg)
1387 {
1388 	struct ahd_linux_device *dev;
1389 	int was_queuing;
1390 	int now_queuing;
1391 
1392 	if (sdev == NULL)
1393 		return;
1394 
1395 	dev = scsi_transport_device_data(sdev);
1396 
1397 	if (dev == NULL)
1398 		return;
1399 	was_queuing = dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED);
1400 	switch (alg) {
1401 	default:
1402 	case AHD_QUEUE_NONE:
1403 		now_queuing = 0;
1404 		break;
1405 	case AHD_QUEUE_BASIC:
1406 		now_queuing = AHD_DEV_Q_BASIC;
1407 		break;
1408 	case AHD_QUEUE_TAGGED:
1409 		now_queuing = AHD_DEV_Q_TAGGED;
1410 		break;
1411 	}
1412 	if ((dev->flags & AHD_DEV_FREEZE_TIL_EMPTY) == 0
1413 	 && (was_queuing != now_queuing)
1414 	 && (dev->active != 0)) {
1415 		dev->flags |= AHD_DEV_FREEZE_TIL_EMPTY;
1416 		dev->qfrozen++;
1417 	}
1418 
1419 	dev->flags &= ~(AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED|AHD_DEV_PERIODIC_OTAG);
1420 	if (now_queuing) {
1421 		u_int usertags;
1422 
1423 		usertags = ahd_linux_user_tagdepth(ahd, devinfo);
1424 		if (!was_queuing) {
1425 			/*
1426 			 * Start out aggressively and allow our
1427 			 * dynamic queue depth algorithm to take
1428 			 * care of the rest.
1429 			 */
1430 			dev->maxtags = usertags;
1431 			dev->openings = dev->maxtags - dev->active;
1432 		}
1433 		if (dev->maxtags == 0) {
1434 			/*
1435 			 * Queueing is disabled by the user.
1436 			 */
1437 			dev->openings = 1;
1438 		} else if (alg == AHD_QUEUE_TAGGED) {
1439 			dev->flags |= AHD_DEV_Q_TAGGED;
1440 			if (aic79xx_periodic_otag != 0)
1441 				dev->flags |= AHD_DEV_PERIODIC_OTAG;
1442 		} else
1443 			dev->flags |= AHD_DEV_Q_BASIC;
1444 	} else {
1445 		/* We can only have one opening. */
1446 		dev->maxtags = 0;
1447 		dev->openings =  1 - dev->active;
1448 	}
1449 
1450 	switch ((dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED))) {
1451 	case AHD_DEV_Q_BASIC:
1452 	case AHD_DEV_Q_TAGGED:
1453 		scsi_change_queue_depth(sdev,
1454 				dev->openings + dev->active);
1455 		break;
1456 	default:
1457 		/*
1458 		 * We allow the OS to queue 2 untagged transactions to
1459 		 * us at any time even though we can only execute them
1460 		 * serially on the controller/device.  This should
1461 		 * remove some latency.
1462 		 */
1463 		scsi_change_queue_depth(sdev, 1);
1464 		break;
1465 	}
1466 }
1467 
1468 int
1469 ahd_platform_abort_scbs(struct ahd_softc *ahd, int target, char channel,
1470 			int lun, u_int tag, role_t role, uint32_t status)
1471 {
1472 	return 0;
1473 }
1474 
1475 static u_int
1476 ahd_linux_user_tagdepth(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
1477 {
1478 	static int warned_user;
1479 	u_int tags;
1480 
1481 	tags = 0;
1482 	if ((ahd->user_discenable & devinfo->target_mask) != 0) {
1483 		if (ahd->unit >= ARRAY_SIZE(aic79xx_tag_info)) {
1484 
1485 			if (warned_user == 0) {
1486 				printk(KERN_WARNING
1487 "aic79xx: WARNING: Insufficient tag_info instances\n"
1488 "aic79xx: for installed controllers.  Using defaults\n"
1489 "aic79xx: Please update the aic79xx_tag_info array in\n"
1490 "aic79xx: the aic79xx_osm.c source file.\n");
1491 				warned_user++;
1492 			}
1493 			tags = AHD_MAX_QUEUE;
1494 		} else {
1495 			adapter_tag_info_t *tag_info;
1496 
1497 			tag_info = &aic79xx_tag_info[ahd->unit];
1498 			tags = tag_info->tag_commands[devinfo->target_offset];
1499 			if (tags > AHD_MAX_QUEUE)
1500 				tags = AHD_MAX_QUEUE;
1501 		}
1502 	}
1503 	return (tags);
1504 }
1505 
1506 /*
1507  * Determines the queue depth for a given device.
1508  */
1509 static void
1510 ahd_linux_device_queue_depth(struct scsi_device *sdev)
1511 {
1512 	struct	ahd_devinfo devinfo;
1513 	u_int	tags;
1514 	struct ahd_softc *ahd = *((struct ahd_softc **)sdev->host->hostdata);
1515 
1516 	ahd_compile_devinfo(&devinfo,
1517 			    ahd->our_id,
1518 			    sdev->sdev_target->id, sdev->lun,
1519 			    sdev->sdev_target->channel == 0 ? 'A' : 'B',
1520 			    ROLE_INITIATOR);
1521 	tags = ahd_linux_user_tagdepth(ahd, &devinfo);
1522 	if (tags != 0 && sdev->tagged_supported != 0) {
1523 
1524 		ahd_platform_set_tags(ahd, sdev, &devinfo, AHD_QUEUE_TAGGED);
1525 		ahd_send_async(ahd, devinfo.channel, devinfo.target,
1526 			       devinfo.lun, AC_TRANSFER_NEG);
1527 		ahd_print_devinfo(ahd, &devinfo);
1528 		printk("Tagged Queuing enabled.  Depth %d\n", tags);
1529 	} else {
1530 		ahd_platform_set_tags(ahd, sdev, &devinfo, AHD_QUEUE_NONE);
1531 		ahd_send_async(ahd, devinfo.channel, devinfo.target,
1532 			       devinfo.lun, AC_TRANSFER_NEG);
1533 	}
1534 }
1535 
1536 static int
1537 ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
1538 		      struct scsi_cmnd *cmd)
1539 {
1540 	struct	 scb *scb;
1541 	struct	 hardware_scb *hscb;
1542 	struct	 ahd_initiator_tinfo *tinfo;
1543 	struct	 ahd_tmode_tstate *tstate;
1544 	u_int	 col_idx;
1545 	uint16_t mask;
1546 	unsigned long flags;
1547 	int nseg;
1548 
1549 	nseg = scsi_dma_map(cmd);
1550 	if (nseg < 0)
1551 		return SCSI_MLQUEUE_HOST_BUSY;
1552 
1553 	ahd_lock(ahd, &flags);
1554 
1555 	/*
1556 	 * Get an scb to use.
1557 	 */
1558 	tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
1559 				    cmd->device->id, &tstate);
1560 	if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) == 0
1561 	 || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
1562 		col_idx = AHD_NEVER_COL_IDX;
1563 	} else {
1564 		col_idx = AHD_BUILD_COL_IDX(cmd->device->id,
1565 					    cmd->device->lun);
1566 	}
1567 	if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
1568 		ahd->flags |= AHD_RESOURCE_SHORTAGE;
1569 		ahd_unlock(ahd, &flags);
1570 		scsi_dma_unmap(cmd);
1571 		return SCSI_MLQUEUE_HOST_BUSY;
1572 	}
1573 
1574 	scb->io_ctx = cmd;
1575 	scb->platform_data->dev = dev;
1576 	hscb = scb->hscb;
1577 	cmd->host_scribble = (char *)scb;
1578 
1579 	/*
1580 	 * Fill out basics of the HSCB.
1581 	 */
1582 	hscb->control = 0;
1583 	hscb->scsiid = ahd_build_scsiid(ahd, cmd->device);
1584 	hscb->lun = cmd->device->lun;
1585 	scb->hscb->task_management = 0;
1586 	mask = SCB_GET_TARGET_MASK(ahd, scb);
1587 
1588 	if ((ahd->user_discenable & mask) != 0)
1589 		hscb->control |= DISCENB;
1590 
1591 	if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0)
1592 		scb->flags |= SCB_PACKETIZED;
1593 
1594 	if ((tstate->auto_negotiate & mask) != 0) {
1595 		scb->flags |= SCB_AUTO_NEGOTIATE;
1596 		scb->hscb->control |= MK_MESSAGE;
1597 	}
1598 
1599 	if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) != 0) {
1600 		if (dev->commands_since_idle_or_otag == AHD_OTAG_THRESH
1601 		 && (dev->flags & AHD_DEV_Q_TAGGED) != 0) {
1602 			hscb->control |= ORDERED_QUEUE_TAG;
1603 			dev->commands_since_idle_or_otag = 0;
1604 		} else {
1605 			hscb->control |= SIMPLE_QUEUE_TAG;
1606 		}
1607 	}
1608 
1609 	hscb->cdb_len = cmd->cmd_len;
1610 	memcpy(hscb->shared_data.idata.cdb, cmd->cmnd, hscb->cdb_len);
1611 
1612 	scb->platform_data->xfer_len = 0;
1613 	ahd_set_residual(scb, 0);
1614 	ahd_set_sense_residual(scb, 0);
1615 	scb->sg_count = 0;
1616 
1617 	if (nseg > 0) {
1618 		void *sg = scb->sg_list;
1619 		struct scatterlist *cur_seg;
1620 		int i;
1621 
1622 		scb->platform_data->xfer_len = 0;
1623 
1624 		scsi_for_each_sg(cmd, cur_seg, nseg, i) {
1625 			dma_addr_t addr;
1626 			bus_size_t len;
1627 
1628 			addr = sg_dma_address(cur_seg);
1629 			len = sg_dma_len(cur_seg);
1630 			scb->platform_data->xfer_len += len;
1631 			sg = ahd_sg_setup(ahd, scb, sg, addr, len,
1632 					  i == (nseg - 1));
1633 		}
1634 	}
1635 
1636 	LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1637 	dev->openings--;
1638 	dev->active++;
1639 	dev->commands_issued++;
1640 
1641 	if ((dev->flags & AHD_DEV_PERIODIC_OTAG) != 0)
1642 		dev->commands_since_idle_or_otag++;
1643 	scb->flags |= SCB_ACTIVE;
1644 	ahd_queue_scb(ahd, scb);
1645 
1646 	ahd_unlock(ahd, &flags);
1647 
1648 	return 0;
1649 }
1650 
1651 /*
1652  * SCSI controller interrupt handler.
1653  */
1654 irqreturn_t
1655 ahd_linux_isr(int irq, void *dev_id)
1656 {
1657 	struct	ahd_softc *ahd;
1658 	u_long	flags;
1659 	int	ours;
1660 
1661 	ahd = (struct ahd_softc *) dev_id;
1662 	ahd_lock(ahd, &flags);
1663 	ours = ahd_intr(ahd);
1664 	ahd_unlock(ahd, &flags);
1665 	return IRQ_RETVAL(ours);
1666 }
1667 
1668 void
1669 ahd_send_async(struct ahd_softc *ahd, char channel,
1670 	       u_int target, u_int lun, ac_code code)
1671 {
1672 	switch (code) {
1673 	case AC_TRANSFER_NEG:
1674 	{
1675 		struct  scsi_target *starget;
1676 		struct	ahd_initiator_tinfo *tinfo;
1677 		struct	ahd_tmode_tstate *tstate;
1678 		unsigned int target_ppr_options;
1679 
1680 		BUG_ON(target == CAM_TARGET_WILDCARD);
1681 
1682 		tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
1683 					    target, &tstate);
1684 
1685 		/*
1686 		 * Don't bother reporting results while
1687 		 * negotiations are still pending.
1688 		 */
1689 		if (tinfo->curr.period != tinfo->goal.period
1690 		 || tinfo->curr.width != tinfo->goal.width
1691 		 || tinfo->curr.offset != tinfo->goal.offset
1692 		 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
1693 			if (bootverbose == 0)
1694 				break;
1695 
1696 		/*
1697 		 * Don't bother reporting results that
1698 		 * are identical to those last reported.
1699 		 */
1700 		starget = ahd->platform_data->starget[target];
1701 		if (starget == NULL)
1702 			break;
1703 
1704 		target_ppr_options =
1705 			(spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
1706 			+ (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
1707 			+ (spi_iu(starget) ?  MSG_EXT_PPR_IU_REQ : 0)
1708 			+ (spi_rd_strm(starget) ? MSG_EXT_PPR_RD_STRM : 0)
1709 			+ (spi_pcomp_en(starget) ? MSG_EXT_PPR_PCOMP_EN : 0)
1710 			+ (spi_rti(starget) ? MSG_EXT_PPR_RTI : 0)
1711 			+ (spi_wr_flow(starget) ? MSG_EXT_PPR_WR_FLOW : 0)
1712 			+ (spi_hold_mcs(starget) ? MSG_EXT_PPR_HOLD_MCS : 0);
1713 
1714 		if (tinfo->curr.period == spi_period(starget)
1715 		    && tinfo->curr.width == spi_width(starget)
1716 		    && tinfo->curr.offset == spi_offset(starget)
1717 		 && tinfo->curr.ppr_options == target_ppr_options)
1718 			if (bootverbose == 0)
1719 				break;
1720 
1721 		spi_period(starget) = tinfo->curr.period;
1722 		spi_width(starget) = tinfo->curr.width;
1723 		spi_offset(starget) = tinfo->curr.offset;
1724 		spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
1725 		spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
1726 		spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
1727 		spi_rd_strm(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_RD_STRM ? 1 : 0;
1728 		spi_pcomp_en(starget) =  tinfo->curr.ppr_options & MSG_EXT_PPR_PCOMP_EN ? 1 : 0;
1729 		spi_rti(starget) =  tinfo->curr.ppr_options &  MSG_EXT_PPR_RTI ? 1 : 0;
1730 		spi_wr_flow(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_WR_FLOW ? 1 : 0;
1731 		spi_hold_mcs(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_HOLD_MCS ? 1 : 0;
1732 		spi_display_xfer_agreement(starget);
1733 		break;
1734 	}
1735         case AC_SENT_BDR:
1736 	{
1737 		WARN_ON(lun != CAM_LUN_WILDCARD);
1738 		scsi_report_device_reset(ahd->platform_data->host,
1739 					 channel - 'A', target);
1740 		break;
1741 	}
1742         case AC_BUS_RESET:
1743 		if (ahd->platform_data->host != NULL) {
1744 			scsi_report_bus_reset(ahd->platform_data->host,
1745 					      channel - 'A');
1746 		}
1747                 break;
1748         default:
1749                 panic("ahd_send_async: Unexpected async event");
1750         }
1751 }
1752 
1753 /*
1754  * Calls the higher level scsi done function and frees the scb.
1755  */
1756 void
1757 ahd_done(struct ahd_softc *ahd, struct scb *scb)
1758 {
1759 	struct scsi_cmnd *cmd;
1760 	struct	  ahd_linux_device *dev;
1761 
1762 	if ((scb->flags & SCB_ACTIVE) == 0) {
1763 		printk("SCB %d done'd twice\n", SCB_GET_TAG(scb));
1764 		ahd_dump_card_state(ahd);
1765 		panic("Stopping for safety");
1766 	}
1767 	LIST_REMOVE(scb, pending_links);
1768 	cmd = scb->io_ctx;
1769 	dev = scb->platform_data->dev;
1770 	dev->active--;
1771 	dev->openings++;
1772 	if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
1773 		cmd->result &= ~(CAM_DEV_QFRZN << 16);
1774 		dev->qfrozen--;
1775 	}
1776 	ahd_linux_unmap_scb(ahd, scb);
1777 
1778 	/*
1779 	 * Guard against stale sense data.
1780 	 * The Linux mid-layer assumes that sense
1781 	 * was retrieved anytime the first byte of
1782 	 * the sense buffer looks "sane".
1783 	 */
1784 	cmd->sense_buffer[0] = 0;
1785 	if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG) {
1786 #ifdef AHD_REPORT_UNDERFLOWS
1787 		uint32_t amount_xferred;
1788 
1789 		amount_xferred =
1790 		    ahd_get_transfer_length(scb) - ahd_get_residual(scb);
1791 #endif
1792 		if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
1793 #ifdef AHD_DEBUG
1794 			if ((ahd_debug & AHD_SHOW_MISC) != 0) {
1795 				ahd_print_path(ahd, scb);
1796 				printk("Set CAM_UNCOR_PARITY\n");
1797 			}
1798 #endif
1799 			ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
1800 #ifdef AHD_REPORT_UNDERFLOWS
1801 		/*
1802 		 * This code is disabled by default as some
1803 		 * clients of the SCSI system do not properly
1804 		 * initialize the underflow parameter.  This
1805 		 * results in spurious termination of commands
1806 		 * that complete as expected (e.g. underflow is
1807 		 * allowed as command can return variable amounts
1808 		 * of data.
1809 		 */
1810 		} else if (amount_xferred < scb->io_ctx->underflow) {
1811 			u_int i;
1812 
1813 			ahd_print_path(ahd, scb);
1814 			printk("CDB:");
1815 			for (i = 0; i < scb->io_ctx->cmd_len; i++)
1816 				printk(" 0x%x", scb->io_ctx->cmnd[i]);
1817 			printk("\n");
1818 			ahd_print_path(ahd, scb);
1819 			printk("Saw underflow (%ld of %ld bytes). "
1820 			       "Treated as error\n",
1821 				ahd_get_residual(scb),
1822 				ahd_get_transfer_length(scb));
1823 			ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
1824 #endif
1825 		} else {
1826 			ahd_set_transaction_status(scb, CAM_REQ_CMP);
1827 		}
1828 	} else if (ahd_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
1829 		ahd_linux_handle_scsi_status(ahd, cmd->device, scb);
1830 	}
1831 
1832 	if (dev->openings == 1
1833 	 && ahd_get_transaction_status(scb) == CAM_REQ_CMP
1834 	 && ahd_get_scsi_status(scb) != SAM_STAT_TASK_SET_FULL)
1835 		dev->tag_success_count++;
1836 	/*
1837 	 * Some devices deal with temporary internal resource
1838 	 * shortages by returning queue full.  When the queue
1839 	 * full occurrs, we throttle back.  Slowly try to get
1840 	 * back to our previous queue depth.
1841 	 */
1842 	if ((dev->openings + dev->active) < dev->maxtags
1843 	 && dev->tag_success_count > AHD_TAG_SUCCESS_INTERVAL) {
1844 		dev->tag_success_count = 0;
1845 		dev->openings++;
1846 	}
1847 
1848 	if (dev->active == 0)
1849 		dev->commands_since_idle_or_otag = 0;
1850 
1851 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
1852 		printk("Recovery SCB completes\n");
1853 		if (ahd_get_transaction_status(scb) == CAM_BDR_SENT
1854 		 || ahd_get_transaction_status(scb) == CAM_REQ_ABORTED)
1855 			ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1856 
1857 		if (ahd->platform_data->eh_done)
1858 			complete(ahd->platform_data->eh_done);
1859 	}
1860 
1861 	ahd_free_scb(ahd, scb);
1862 	ahd_linux_queue_cmd_complete(ahd, cmd);
1863 }
1864 
1865 static void
1866 ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
1867 			     struct scsi_device *sdev, struct scb *scb)
1868 {
1869 	struct	ahd_devinfo devinfo;
1870 	struct ahd_linux_device *dev = scsi_transport_device_data(sdev);
1871 
1872 	ahd_compile_devinfo(&devinfo,
1873 			    ahd->our_id,
1874 			    sdev->sdev_target->id, sdev->lun,
1875 			    sdev->sdev_target->channel == 0 ? 'A' : 'B',
1876 			    ROLE_INITIATOR);
1877 
1878 	/*
1879 	 * We don't currently trust the mid-layer to
1880 	 * properly deal with queue full or busy.  So,
1881 	 * when one occurs, we tell the mid-layer to
1882 	 * unconditionally requeue the command to us
1883 	 * so that we can retry it ourselves.  We also
1884 	 * implement our own throttling mechanism so
1885 	 * we don't clobber the device with too many
1886 	 * commands.
1887 	 */
1888 	switch (ahd_get_scsi_status(scb)) {
1889 	default:
1890 		break;
1891 	case SAM_STAT_CHECK_CONDITION:
1892 	case SAM_STAT_COMMAND_TERMINATED:
1893 	{
1894 		struct scsi_cmnd *cmd;
1895 
1896 		/*
1897 		 * Copy sense information to the OS's cmd
1898 		 * structure if it is available.
1899 		 */
1900 		cmd = scb->io_ctx;
1901 		if ((scb->flags & (SCB_SENSE|SCB_PKT_SENSE)) != 0) {
1902 			struct scsi_status_iu_header *siu;
1903 			u_int sense_size;
1904 			u_int sense_offset;
1905 
1906 			if (scb->flags & SCB_SENSE) {
1907 				sense_size = min(sizeof(struct scsi_sense_data)
1908 					       - ahd_get_sense_residual(scb),
1909 						 (u_long)SCSI_SENSE_BUFFERSIZE);
1910 				sense_offset = 0;
1911 			} else {
1912 				/*
1913 				 * Copy only the sense data into the provided
1914 				 * buffer.
1915 				 */
1916 				siu = (struct scsi_status_iu_header *)
1917 				    scb->sense_data;
1918 				sense_size = min_t(size_t,
1919 						scsi_4btoul(siu->sense_length),
1920 						SCSI_SENSE_BUFFERSIZE);
1921 				sense_offset = SIU_SENSE_OFFSET(siu);
1922 			}
1923 
1924 			memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1925 			memcpy(cmd->sense_buffer,
1926 			       ahd_get_sense_buf(ahd, scb)
1927 			       + sense_offset, sense_size);
1928 			set_status_byte(cmd, SAM_STAT_CHECK_CONDITION);
1929 
1930 #ifdef AHD_DEBUG
1931 			if (ahd_debug & AHD_SHOW_SENSE) {
1932 				int i;
1933 
1934 				printk("Copied %d bytes of sense data at %d:",
1935 				       sense_size, sense_offset);
1936 				for (i = 0; i < sense_size; i++) {
1937 					if ((i & 0xF) == 0)
1938 						printk("\n");
1939 					printk("0x%x ", cmd->sense_buffer[i]);
1940 				}
1941 				printk("\n");
1942 			}
1943 #endif
1944 		}
1945 		break;
1946 	}
1947 	case SAM_STAT_TASK_SET_FULL:
1948 		/*
1949 		 * By the time the core driver has returned this
1950 		 * command, all other commands that were queued
1951 		 * to us but not the device have been returned.
1952 		 * This ensures that dev->active is equal to
1953 		 * the number of commands actually queued to
1954 		 * the device.
1955 		 */
1956 		dev->tag_success_count = 0;
1957 		if (dev->active != 0) {
1958 			/*
1959 			 * Drop our opening count to the number
1960 			 * of commands currently outstanding.
1961 			 */
1962 			dev->openings = 0;
1963 #ifdef AHD_DEBUG
1964 			if ((ahd_debug & AHD_SHOW_QFULL) != 0) {
1965 				ahd_print_path(ahd, scb);
1966 				printk("Dropping tag count to %d\n",
1967 				       dev->active);
1968 			}
1969 #endif
1970 			if (dev->active == dev->tags_on_last_queuefull) {
1971 
1972 				dev->last_queuefull_same_count++;
1973 				/*
1974 				 * If we repeatedly see a queue full
1975 				 * at the same queue depth, this
1976 				 * device has a fixed number of tag
1977 				 * slots.  Lock in this tag depth
1978 				 * so we stop seeing queue fulls from
1979 				 * this device.
1980 				 */
1981 				if (dev->last_queuefull_same_count
1982 				 == AHD_LOCK_TAGS_COUNT) {
1983 					dev->maxtags = dev->active;
1984 					ahd_print_path(ahd, scb);
1985 					printk("Locking max tag count at %d\n",
1986 					       dev->active);
1987 				}
1988 			} else {
1989 				dev->tags_on_last_queuefull = dev->active;
1990 				dev->last_queuefull_same_count = 0;
1991 			}
1992 			ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
1993 			ahd_set_scsi_status(scb, SAM_STAT_GOOD);
1994 			ahd_platform_set_tags(ahd, sdev, &devinfo,
1995 				     (dev->flags & AHD_DEV_Q_BASIC)
1996 				   ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
1997 			break;
1998 		}
1999 		/*
2000 		 * Drop down to a single opening, and treat this
2001 		 * as if the target returned BUSY SCSI status.
2002 		 */
2003 		dev->openings = 1;
2004 		ahd_platform_set_tags(ahd, sdev, &devinfo,
2005 			     (dev->flags & AHD_DEV_Q_BASIC)
2006 			   ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
2007 		ahd_set_scsi_status(scb, SAM_STAT_BUSY);
2008 	}
2009 }
2010 
2011 static void
2012 ahd_linux_queue_cmd_complete(struct ahd_softc *ahd, struct scsi_cmnd *cmd)
2013 {
2014 	int status;
2015 	int new_status = DID_OK;
2016 	int do_fallback = 0;
2017 	int scsi_status;
2018 	struct scsi_sense_data *sense;
2019 
2020 	/*
2021 	 * Map CAM error codes into Linux Error codes.  We
2022 	 * avoid the conversion so that the DV code has the
2023 	 * full error information available when making
2024 	 * state change decisions.
2025 	 */
2026 
2027 	status = ahd_cmd_get_transaction_status(cmd);
2028 	switch (status) {
2029 	case CAM_REQ_INPROG:
2030 	case CAM_REQ_CMP:
2031 		new_status = DID_OK;
2032 		break;
2033 	case CAM_AUTOSENSE_FAIL:
2034 		new_status = DID_ERROR;
2035 		fallthrough;
2036 	case CAM_SCSI_STATUS_ERROR:
2037 		scsi_status = ahd_cmd_get_scsi_status(cmd);
2038 
2039 		switch(scsi_status) {
2040 		case SAM_STAT_COMMAND_TERMINATED:
2041 		case SAM_STAT_CHECK_CONDITION:
2042 			sense = (struct scsi_sense_data *)
2043 				cmd->sense_buffer;
2044 			if (sense->extra_len >= 5 &&
2045 			    (sense->add_sense_code == 0x47
2046 			     || sense->add_sense_code == 0x48))
2047 				do_fallback = 1;
2048 			break;
2049 		default:
2050 			break;
2051 		}
2052 		break;
2053 	case CAM_REQ_ABORTED:
2054 		new_status = DID_ABORT;
2055 		break;
2056 	case CAM_BUSY:
2057 		new_status = DID_BUS_BUSY;
2058 		break;
2059 	case CAM_REQ_INVALID:
2060 	case CAM_PATH_INVALID:
2061 		new_status = DID_BAD_TARGET;
2062 		break;
2063 	case CAM_SEL_TIMEOUT:
2064 		new_status = DID_NO_CONNECT;
2065 		break;
2066 	case CAM_SCSI_BUS_RESET:
2067 	case CAM_BDR_SENT:
2068 		new_status = DID_RESET;
2069 		break;
2070 	case CAM_UNCOR_PARITY:
2071 		new_status = DID_PARITY;
2072 		do_fallback = 1;
2073 		break;
2074 	case CAM_CMD_TIMEOUT:
2075 		new_status = DID_TIME_OUT;
2076 		do_fallback = 1;
2077 		break;
2078 	case CAM_REQ_CMP_ERR:
2079 	case CAM_UNEXP_BUSFREE:
2080 	case CAM_DATA_RUN_ERR:
2081 		new_status = DID_ERROR;
2082 		do_fallback = 1;
2083 		break;
2084 	case CAM_UA_ABORT:
2085 	case CAM_NO_HBA:
2086 	case CAM_SEQUENCE_FAIL:
2087 	case CAM_CCB_LEN_ERR:
2088 	case CAM_PROVIDE_FAIL:
2089 	case CAM_REQ_TERMIO:
2090 	case CAM_UNREC_HBA_ERROR:
2091 	case CAM_REQ_TOO_BIG:
2092 		new_status = DID_ERROR;
2093 		break;
2094 	case CAM_REQUEUE_REQ:
2095 		new_status = DID_REQUEUE;
2096 		break;
2097 	default:
2098 		/* We should never get here */
2099 		new_status = DID_ERROR;
2100 		break;
2101 	}
2102 
2103 	if (do_fallback) {
2104 		printk("%s: device overrun (status %x) on %d:%d:%d\n",
2105 		       ahd_name(ahd), status, cmd->device->channel,
2106 		       cmd->device->id, (u8)cmd->device->lun);
2107 	}
2108 
2109 	ahd_cmd_set_transaction_status(cmd, new_status);
2110 
2111 	scsi_done(cmd);
2112 }
2113 
2114 static void
2115 ahd_freeze_simq(struct ahd_softc *ahd)
2116 {
2117 	scsi_block_requests(ahd->platform_data->host);
2118 }
2119 
2120 static void
2121 ahd_release_simq(struct ahd_softc *ahd)
2122 {
2123 	scsi_unblock_requests(ahd->platform_data->host);
2124 }
2125 
2126 static int
2127 ahd_linux_queue_abort_cmd(struct scsi_cmnd *cmd)
2128 {
2129 	struct ahd_softc *ahd;
2130 	struct ahd_linux_device *dev;
2131 	struct scb *pending_scb;
2132 	u_int  saved_scbptr;
2133 	u_int  active_scbptr;
2134 	u_int  last_phase;
2135 	u_int  cdb_byte;
2136 	int    retval = SUCCESS;
2137 	int    was_paused;
2138 	int    paused;
2139 	int    wait;
2140 	int    disconnected;
2141 	ahd_mode_state saved_modes;
2142 	unsigned long flags;
2143 
2144 	pending_scb = NULL;
2145 	paused = FALSE;
2146 	wait = FALSE;
2147 	ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
2148 
2149 	scmd_printk(KERN_INFO, cmd,
2150 		    "Attempting to queue an ABORT message:");
2151 
2152 	printk("CDB:");
2153 	for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
2154 		printk(" 0x%x", cmd->cmnd[cdb_byte]);
2155 	printk("\n");
2156 
2157 	ahd_lock(ahd, &flags);
2158 
2159 	/*
2160 	 * First determine if we currently own this command.
2161 	 * Start by searching the device queue.  If not found
2162 	 * there, check the pending_scb list.  If not found
2163 	 * at all, and the system wanted us to just abort the
2164 	 * command, return success.
2165 	 */
2166 	dev = scsi_transport_device_data(cmd->device);
2167 
2168 	if (dev == NULL) {
2169 		/*
2170 		 * No target device for this command exists,
2171 		 * so we must not still own the command.
2172 		 */
2173 		scmd_printk(KERN_INFO, cmd, "Is not an active device\n");
2174 		goto done;
2175 	}
2176 
2177 	/*
2178 	 * See if we can find a matching cmd in the pending list.
2179 	 */
2180 	LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
2181 		if (pending_scb->io_ctx == cmd)
2182 			break;
2183 	}
2184 
2185 	if (pending_scb == NULL) {
2186 		scmd_printk(KERN_INFO, cmd, "Command not found\n");
2187 		goto done;
2188 	}
2189 
2190 	if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
2191 		/*
2192 		 * We can't queue two recovery actions using the same SCB
2193 		 */
2194 		retval = FAILED;
2195 		goto done;
2196 	}
2197 
2198 	/*
2199 	 * Ensure that the card doesn't do anything
2200 	 * behind our back.  Also make sure that we
2201 	 * didn't "just" miss an interrupt that would
2202 	 * affect this cmd.
2203 	 */
2204 	was_paused = ahd_is_paused(ahd);
2205 	ahd_pause_and_flushwork(ahd);
2206 	paused = TRUE;
2207 
2208 	if ((pending_scb->flags & SCB_ACTIVE) == 0) {
2209 		scmd_printk(KERN_INFO, cmd, "Command already completed\n");
2210 		goto done;
2211 	}
2212 
2213 	printk("%s: At time of recovery, card was %spaused\n",
2214 	       ahd_name(ahd), was_paused ? "" : "not ");
2215 	ahd_dump_card_state(ahd);
2216 
2217 	disconnected = TRUE;
2218 	if (ahd_search_qinfifo(ahd, cmd->device->id,
2219 			       cmd->device->channel + 'A',
2220 			       cmd->device->lun,
2221 			       pending_scb->hscb->tag,
2222 			       ROLE_INITIATOR, CAM_REQ_ABORTED,
2223 			       SEARCH_COMPLETE) > 0) {
2224 		printk("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
2225 		       ahd_name(ahd), cmd->device->channel,
2226 		       cmd->device->id, (u8)cmd->device->lun);
2227 		goto done;
2228 	}
2229 
2230 	saved_modes = ahd_save_modes(ahd);
2231 	ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2232 	last_phase = ahd_inb(ahd, LASTPHASE);
2233 	saved_scbptr = ahd_get_scbptr(ahd);
2234 	active_scbptr = saved_scbptr;
2235 	if (disconnected && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
2236 		struct scb *bus_scb;
2237 
2238 		bus_scb = ahd_lookup_scb(ahd, active_scbptr);
2239 		if (bus_scb == pending_scb)
2240 			disconnected = FALSE;
2241 	}
2242 
2243 	/*
2244 	 * At this point, pending_scb is the scb associated with the
2245 	 * passed in command.  That command is currently active on the
2246 	 * bus or is in the disconnected state.
2247 	 */
2248 	ahd_inb(ahd, SAVED_SCSIID);
2249 	if (last_phase != P_BUSFREE
2250 	    && SCB_GET_TAG(pending_scb) == active_scbptr) {
2251 
2252 		/*
2253 		 * We're active on the bus, so assert ATN
2254 		 * and hope that the target responds.
2255 		 */
2256 		pending_scb = ahd_lookup_scb(ahd, active_scbptr);
2257 		pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
2258 		ahd_outb(ahd, MSG_OUT, HOST_MSG);
2259 		ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
2260 		scmd_printk(KERN_INFO, cmd, "Device is active, asserting ATN\n");
2261 		wait = TRUE;
2262 	} else if (disconnected) {
2263 
2264 		/*
2265 		 * Actually re-queue this SCB in an attempt
2266 		 * to select the device before it reconnects.
2267 		 */
2268 		pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
2269 		ahd_set_scbptr(ahd, SCB_GET_TAG(pending_scb));
2270 		pending_scb->hscb->cdb_len = 0;
2271 		pending_scb->hscb->task_attribute = 0;
2272 		pending_scb->hscb->task_management = SIU_TASKMGMT_ABORT_TASK;
2273 
2274 		if ((pending_scb->flags & SCB_PACKETIZED) != 0) {
2275 			/*
2276 			 * Mark the SCB has having an outstanding
2277 			 * task management function.  Should the command
2278 			 * complete normally before the task management
2279 			 * function can be sent, the host will be notified
2280 			 * to abort our requeued SCB.
2281 			 */
2282 			ahd_outb(ahd, SCB_TASK_MANAGEMENT,
2283 				 pending_scb->hscb->task_management);
2284 		} else {
2285 			/*
2286 			 * If non-packetized, set the MK_MESSAGE control
2287 			 * bit indicating that we desire to send a message.
2288 			 * We also set the disconnected flag since there is
2289 			 * no guarantee that our SCB control byte matches
2290 			 * the version on the card.  We don't want the
2291 			 * sequencer to abort the command thinking an
2292 			 * unsolicited reselection occurred.
2293 			 */
2294 			pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
2295 
2296 			/*
2297 			 * The sequencer will never re-reference the
2298 			 * in-core SCB.  To make sure we are notified
2299 			 * during reselection, set the MK_MESSAGE flag in
2300 			 * the card's copy of the SCB.
2301 			 */
2302 			ahd_outb(ahd, SCB_CONTROL,
2303 				 ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
2304 		}
2305 
2306 		/*
2307 		 * Clear out any entries in the QINFIFO first
2308 		 * so we are the next SCB for this target
2309 		 * to run.
2310 		 */
2311 		ahd_search_qinfifo(ahd, cmd->device->id,
2312 				   cmd->device->channel + 'A', cmd->device->lun,
2313 				   SCB_LIST_NULL, ROLE_INITIATOR,
2314 				   CAM_REQUEUE_REQ, SEARCH_COMPLETE);
2315 		ahd_qinfifo_requeue_tail(ahd, pending_scb);
2316 		ahd_set_scbptr(ahd, saved_scbptr);
2317 		ahd_print_path(ahd, pending_scb);
2318 		printk("Device is disconnected, re-queuing SCB\n");
2319 		wait = TRUE;
2320 	} else {
2321 		scmd_printk(KERN_INFO, cmd, "Unable to deliver message\n");
2322 		retval = FAILED;
2323 	}
2324 
2325 
2326 	ahd_restore_modes(ahd, saved_modes);
2327 done:
2328 	if (paused)
2329 		ahd_unpause(ahd);
2330 	if (wait) {
2331 		DECLARE_COMPLETION_ONSTACK(done);
2332 
2333 		ahd->platform_data->eh_done = &done;
2334 		ahd_unlock(ahd, &flags);
2335 
2336 		printk("%s: Recovery code sleeping\n", ahd_name(ahd));
2337 		if (!wait_for_completion_timeout(&done, 5 * HZ)) {
2338 			ahd_lock(ahd, &flags);
2339 			ahd->platform_data->eh_done = NULL;
2340 			ahd_unlock(ahd, &flags);
2341 			printk("%s: Timer Expired (active %d)\n",
2342 			       ahd_name(ahd), dev->active);
2343 			retval = FAILED;
2344 		}
2345 		printk("Recovery code awake\n");
2346 	} else
2347 		ahd_unlock(ahd, &flags);
2348 
2349 	if (retval != SUCCESS)
2350 		printk("%s: Command abort returning 0x%x\n",
2351 		       ahd_name(ahd), retval);
2352 
2353 	return retval;
2354 }
2355 
2356 static void ahd_linux_set_width(struct scsi_target *starget, int width)
2357 {
2358 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2359 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2360 	struct ahd_devinfo devinfo;
2361 	unsigned long flags;
2362 
2363 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2364 			    starget->channel + 'A', ROLE_INITIATOR);
2365 	ahd_lock(ahd, &flags);
2366 	ahd_set_width(ahd, &devinfo, width, AHD_TRANS_GOAL, FALSE);
2367 	ahd_unlock(ahd, &flags);
2368 }
2369 
2370 static void ahd_linux_set_period(struct scsi_target *starget, int period)
2371 {
2372 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2373 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2374 	struct ahd_tmode_tstate *tstate;
2375 	struct ahd_initiator_tinfo *tinfo
2376 		= ahd_fetch_transinfo(ahd,
2377 				      starget->channel + 'A',
2378 				      shost->this_id, starget->id, &tstate);
2379 	struct ahd_devinfo devinfo;
2380 	unsigned int ppr_options = tinfo->goal.ppr_options;
2381 	unsigned int dt;
2382 	unsigned long flags;
2383 	unsigned long offset = tinfo->goal.offset;
2384 
2385 #ifdef AHD_DEBUG
2386 	if ((ahd_debug & AHD_SHOW_DV) != 0)
2387 		printk("%s: set period to %d\n", ahd_name(ahd), period);
2388 #endif
2389 	if (offset == 0)
2390 		offset = MAX_OFFSET;
2391 
2392 	if (period < 8)
2393 		period = 8;
2394 	if (period < 10) {
2395 		if (spi_max_width(starget)) {
2396 			ppr_options |= MSG_EXT_PPR_DT_REQ;
2397 			if (period == 8)
2398 				ppr_options |= MSG_EXT_PPR_IU_REQ;
2399 		} else
2400 			period = 10;
2401 	}
2402 
2403 	dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2404 
2405 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2406 			    starget->channel + 'A', ROLE_INITIATOR);
2407 
2408 	/* all PPR requests apart from QAS require wide transfers */
2409 	if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
2410 		if (spi_width(starget) == 0)
2411 			ppr_options &= MSG_EXT_PPR_QAS_REQ;
2412 	}
2413 
2414 	ahd_find_syncrate(ahd, &period, &ppr_options,
2415 			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2416 
2417 	ahd_lock(ahd, &flags);
2418 	ahd_set_syncrate(ahd, &devinfo, period, offset,
2419 			 ppr_options, AHD_TRANS_GOAL, FALSE);
2420 	ahd_unlock(ahd, &flags);
2421 }
2422 
2423 static void ahd_linux_set_offset(struct scsi_target *starget, int offset)
2424 {
2425 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2426 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2427 	struct ahd_tmode_tstate *tstate;
2428 	struct ahd_initiator_tinfo *tinfo
2429 		= ahd_fetch_transinfo(ahd,
2430 				      starget->channel + 'A',
2431 				      shost->this_id, starget->id, &tstate);
2432 	struct ahd_devinfo devinfo;
2433 	unsigned int ppr_options = 0;
2434 	unsigned int period = 0;
2435 	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2436 	unsigned long flags;
2437 
2438 #ifdef AHD_DEBUG
2439 	if ((ahd_debug & AHD_SHOW_DV) != 0)
2440 		printk("%s: set offset to %d\n", ahd_name(ahd), offset);
2441 #endif
2442 
2443 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2444 			    starget->channel + 'A', ROLE_INITIATOR);
2445 	if (offset != 0) {
2446 		period = tinfo->goal.period;
2447 		ppr_options = tinfo->goal.ppr_options;
2448 		ahd_find_syncrate(ahd, &period, &ppr_options,
2449 				  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2450 	}
2451 
2452 	ahd_lock(ahd, &flags);
2453 	ahd_set_syncrate(ahd, &devinfo, period, offset, ppr_options,
2454 			 AHD_TRANS_GOAL, FALSE);
2455 	ahd_unlock(ahd, &flags);
2456 }
2457 
2458 static void ahd_linux_set_dt(struct scsi_target *starget, int dt)
2459 {
2460 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2461 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2462 	struct ahd_tmode_tstate *tstate;
2463 	struct ahd_initiator_tinfo *tinfo
2464 		= ahd_fetch_transinfo(ahd,
2465 				      starget->channel + 'A',
2466 				      shost->this_id, starget->id, &tstate);
2467 	struct ahd_devinfo devinfo;
2468 	unsigned int ppr_options = tinfo->goal.ppr_options
2469 		& ~MSG_EXT_PPR_DT_REQ;
2470 	unsigned int period = tinfo->goal.period;
2471 	unsigned int width = tinfo->goal.width;
2472 	unsigned long flags;
2473 
2474 #ifdef AHD_DEBUG
2475 	if ((ahd_debug & AHD_SHOW_DV) != 0)
2476 		printk("%s: %s DT\n", ahd_name(ahd),
2477 		       dt ? "enabling" : "disabling");
2478 #endif
2479 	if (dt && spi_max_width(starget)) {
2480 		ppr_options |= MSG_EXT_PPR_DT_REQ;
2481 		if (!width)
2482 			ahd_linux_set_width(starget, 1);
2483 	} else {
2484 		if (period <= 9)
2485 			period = 10; /* If resetting DT, period must be >= 25ns */
2486 		/* IU is invalid without DT set */
2487 		ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2488 	}
2489 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2490 			    starget->channel + 'A', ROLE_INITIATOR);
2491 	ahd_find_syncrate(ahd, &period, &ppr_options,
2492 			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2493 
2494 	ahd_lock(ahd, &flags);
2495 	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2496 			 ppr_options, AHD_TRANS_GOAL, FALSE);
2497 	ahd_unlock(ahd, &flags);
2498 }
2499 
2500 static void ahd_linux_set_qas(struct scsi_target *starget, int qas)
2501 {
2502 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2503 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2504 	struct ahd_tmode_tstate *tstate;
2505 	struct ahd_initiator_tinfo *tinfo
2506 		= ahd_fetch_transinfo(ahd,
2507 				      starget->channel + 'A',
2508 				      shost->this_id, starget->id, &tstate);
2509 	struct ahd_devinfo devinfo;
2510 	unsigned int ppr_options = tinfo->goal.ppr_options
2511 		& ~MSG_EXT_PPR_QAS_REQ;
2512 	unsigned int period = tinfo->goal.period;
2513 	unsigned int dt;
2514 	unsigned long flags;
2515 
2516 #ifdef AHD_DEBUG
2517 	if ((ahd_debug & AHD_SHOW_DV) != 0)
2518 		printk("%s: %s QAS\n", ahd_name(ahd),
2519 		       qas ? "enabling" : "disabling");
2520 #endif
2521 
2522 	if (qas) {
2523 		ppr_options |= MSG_EXT_PPR_QAS_REQ;
2524 	}
2525 
2526 	dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2527 
2528 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2529 			    starget->channel + 'A', ROLE_INITIATOR);
2530 	ahd_find_syncrate(ahd, &period, &ppr_options,
2531 			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2532 
2533 	ahd_lock(ahd, &flags);
2534 	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2535 			 ppr_options, AHD_TRANS_GOAL, FALSE);
2536 	ahd_unlock(ahd, &flags);
2537 }
2538 
2539 static void ahd_linux_set_iu(struct scsi_target *starget, int iu)
2540 {
2541 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2542 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2543 	struct ahd_tmode_tstate *tstate;
2544 	struct ahd_initiator_tinfo *tinfo
2545 		= ahd_fetch_transinfo(ahd,
2546 				      starget->channel + 'A',
2547 				      shost->this_id, starget->id, &tstate);
2548 	struct ahd_devinfo devinfo;
2549 	unsigned int ppr_options = tinfo->goal.ppr_options
2550 		& ~MSG_EXT_PPR_IU_REQ;
2551 	unsigned int period = tinfo->goal.period;
2552 	unsigned int dt;
2553 	unsigned long flags;
2554 
2555 #ifdef AHD_DEBUG
2556 	if ((ahd_debug & AHD_SHOW_DV) != 0)
2557 		printk("%s: %s IU\n", ahd_name(ahd),
2558 		       iu ? "enabling" : "disabling");
2559 #endif
2560 
2561 	if (iu && spi_max_width(starget)) {
2562 		ppr_options |= MSG_EXT_PPR_IU_REQ;
2563 		ppr_options |= MSG_EXT_PPR_DT_REQ; /* IU requires DT */
2564 	}
2565 
2566 	dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2567 
2568 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2569 			    starget->channel + 'A', ROLE_INITIATOR);
2570 	ahd_find_syncrate(ahd, &period, &ppr_options,
2571 			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2572 
2573 	ahd_lock(ahd, &flags);
2574 	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2575 			 ppr_options, AHD_TRANS_GOAL, FALSE);
2576 	ahd_unlock(ahd, &flags);
2577 }
2578 
2579 static void ahd_linux_set_rd_strm(struct scsi_target *starget, int rdstrm)
2580 {
2581 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2582 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2583 	struct ahd_tmode_tstate *tstate;
2584 	struct ahd_initiator_tinfo *tinfo
2585 		= ahd_fetch_transinfo(ahd,
2586 				      starget->channel + 'A',
2587 				      shost->this_id, starget->id, &tstate);
2588 	struct ahd_devinfo devinfo;
2589 	unsigned int ppr_options = tinfo->goal.ppr_options
2590 		& ~MSG_EXT_PPR_RD_STRM;
2591 	unsigned int period = tinfo->goal.period;
2592 	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2593 	unsigned long flags;
2594 
2595 #ifdef AHD_DEBUG
2596 	if ((ahd_debug & AHD_SHOW_DV) != 0)
2597 		printk("%s: %s Read Streaming\n", ahd_name(ahd),
2598 		       rdstrm  ? "enabling" : "disabling");
2599 #endif
2600 
2601 	if (rdstrm && spi_max_width(starget))
2602 		ppr_options |= MSG_EXT_PPR_RD_STRM;
2603 
2604 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2605 			    starget->channel + 'A', ROLE_INITIATOR);
2606 	ahd_find_syncrate(ahd, &period, &ppr_options,
2607 			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2608 
2609 	ahd_lock(ahd, &flags);
2610 	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2611 			 ppr_options, AHD_TRANS_GOAL, FALSE);
2612 	ahd_unlock(ahd, &flags);
2613 }
2614 
2615 static void ahd_linux_set_wr_flow(struct scsi_target *starget, int wrflow)
2616 {
2617 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2618 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2619 	struct ahd_tmode_tstate *tstate;
2620 	struct ahd_initiator_tinfo *tinfo
2621 		= ahd_fetch_transinfo(ahd,
2622 				      starget->channel + 'A',
2623 				      shost->this_id, starget->id, &tstate);
2624 	struct ahd_devinfo devinfo;
2625 	unsigned int ppr_options = tinfo->goal.ppr_options
2626 		& ~MSG_EXT_PPR_WR_FLOW;
2627 	unsigned int period = tinfo->goal.period;
2628 	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2629 	unsigned long flags;
2630 
2631 #ifdef AHD_DEBUG
2632 	if ((ahd_debug & AHD_SHOW_DV) != 0)
2633 		printk("%s: %s Write Flow Control\n", ahd_name(ahd),
2634 		       wrflow ? "enabling" : "disabling");
2635 #endif
2636 
2637 	if (wrflow && spi_max_width(starget))
2638 		ppr_options |= MSG_EXT_PPR_WR_FLOW;
2639 
2640 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2641 			    starget->channel + 'A', ROLE_INITIATOR);
2642 	ahd_find_syncrate(ahd, &period, &ppr_options,
2643 			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2644 
2645 	ahd_lock(ahd, &flags);
2646 	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2647 			 ppr_options, AHD_TRANS_GOAL, FALSE);
2648 	ahd_unlock(ahd, &flags);
2649 }
2650 
2651 static void ahd_linux_set_rti(struct scsi_target *starget, int rti)
2652 {
2653 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2654 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2655 	struct ahd_tmode_tstate *tstate;
2656 	struct ahd_initiator_tinfo *tinfo
2657 		= ahd_fetch_transinfo(ahd,
2658 				      starget->channel + 'A',
2659 				      shost->this_id, starget->id, &tstate);
2660 	struct ahd_devinfo devinfo;
2661 	unsigned int ppr_options = tinfo->goal.ppr_options
2662 		& ~MSG_EXT_PPR_RTI;
2663 	unsigned int period = tinfo->goal.period;
2664 	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2665 	unsigned long flags;
2666 
2667 	if ((ahd->features & AHD_RTI) == 0) {
2668 #ifdef AHD_DEBUG
2669 		if ((ahd_debug & AHD_SHOW_DV) != 0)
2670 			printk("%s: RTI not available\n", ahd_name(ahd));
2671 #endif
2672 		return;
2673 	}
2674 
2675 #ifdef AHD_DEBUG
2676 	if ((ahd_debug & AHD_SHOW_DV) != 0)
2677 		printk("%s: %s RTI\n", ahd_name(ahd),
2678 		       rti ? "enabling" : "disabling");
2679 #endif
2680 
2681 	if (rti && spi_max_width(starget))
2682 		ppr_options |= MSG_EXT_PPR_RTI;
2683 
2684 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2685 			    starget->channel + 'A', ROLE_INITIATOR);
2686 	ahd_find_syncrate(ahd, &period, &ppr_options,
2687 			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2688 
2689 	ahd_lock(ahd, &flags);
2690 	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2691 			 ppr_options, AHD_TRANS_GOAL, FALSE);
2692 	ahd_unlock(ahd, &flags);
2693 }
2694 
2695 static void ahd_linux_set_pcomp_en(struct scsi_target *starget, int pcomp)
2696 {
2697 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2698 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2699 	struct ahd_tmode_tstate *tstate;
2700 	struct ahd_initiator_tinfo *tinfo
2701 		= ahd_fetch_transinfo(ahd,
2702 				      starget->channel + 'A',
2703 				      shost->this_id, starget->id, &tstate);
2704 	struct ahd_devinfo devinfo;
2705 	unsigned int ppr_options = tinfo->goal.ppr_options
2706 		& ~MSG_EXT_PPR_PCOMP_EN;
2707 	unsigned int period = tinfo->goal.period;
2708 	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2709 	unsigned long flags;
2710 
2711 #ifdef AHD_DEBUG
2712 	if ((ahd_debug & AHD_SHOW_DV) != 0)
2713 		printk("%s: %s Precompensation\n", ahd_name(ahd),
2714 		       pcomp ? "Enable" : "Disable");
2715 #endif
2716 
2717 	if (pcomp && spi_max_width(starget)) {
2718 		uint8_t precomp;
2719 
2720 		if (ahd->unit < ARRAY_SIZE(aic79xx_iocell_info)) {
2721 			const struct ahd_linux_iocell_opts *iocell_opts;
2722 
2723 			iocell_opts = &aic79xx_iocell_info[ahd->unit];
2724 			precomp = iocell_opts->precomp;
2725 		} else {
2726 			precomp = AIC79XX_DEFAULT_PRECOMP;
2727 		}
2728 		ppr_options |= MSG_EXT_PPR_PCOMP_EN;
2729 		AHD_SET_PRECOMP(ahd, precomp);
2730 	} else {
2731 		AHD_SET_PRECOMP(ahd, 0);
2732 	}
2733 
2734 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2735 			    starget->channel + 'A', ROLE_INITIATOR);
2736 	ahd_find_syncrate(ahd, &period, &ppr_options,
2737 			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2738 
2739 	ahd_lock(ahd, &flags);
2740 	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2741 			 ppr_options, AHD_TRANS_GOAL, FALSE);
2742 	ahd_unlock(ahd, &flags);
2743 }
2744 
2745 static void ahd_linux_set_hold_mcs(struct scsi_target *starget, int hold)
2746 {
2747 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2748 	struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2749 	struct ahd_tmode_tstate *tstate;
2750 	struct ahd_initiator_tinfo *tinfo
2751 		= ahd_fetch_transinfo(ahd,
2752 				      starget->channel + 'A',
2753 				      shost->this_id, starget->id, &tstate);
2754 	struct ahd_devinfo devinfo;
2755 	unsigned int ppr_options = tinfo->goal.ppr_options
2756 		& ~MSG_EXT_PPR_HOLD_MCS;
2757 	unsigned int period = tinfo->goal.period;
2758 	unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2759 	unsigned long flags;
2760 
2761 	if (hold && spi_max_width(starget))
2762 		ppr_options |= MSG_EXT_PPR_HOLD_MCS;
2763 
2764 	ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2765 			    starget->channel + 'A', ROLE_INITIATOR);
2766 	ahd_find_syncrate(ahd, &period, &ppr_options,
2767 			  dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2768 
2769 	ahd_lock(ahd, &flags);
2770 	ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2771 			 ppr_options, AHD_TRANS_GOAL, FALSE);
2772 	ahd_unlock(ahd, &flags);
2773 }
2774 
2775 static void ahd_linux_get_signalling(struct Scsi_Host *shost)
2776 {
2777 	struct ahd_softc *ahd = *(struct ahd_softc **)shost->hostdata;
2778 	unsigned long flags;
2779 	u8 mode;
2780 
2781 	ahd_lock(ahd, &flags);
2782 	ahd_pause(ahd);
2783 	mode = ahd_inb(ahd, SBLKCTL);
2784 	ahd_unpause(ahd);
2785 	ahd_unlock(ahd, &flags);
2786 
2787 	if (mode & ENAB40)
2788 		spi_signalling(shost) = SPI_SIGNAL_LVD;
2789 	else if (mode & ENAB20)
2790 		spi_signalling(shost) = SPI_SIGNAL_SE;
2791 	else
2792 		spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
2793 }
2794 
2795 static struct spi_function_template ahd_linux_transport_functions = {
2796 	.set_offset	= ahd_linux_set_offset,
2797 	.show_offset	= 1,
2798 	.set_period	= ahd_linux_set_period,
2799 	.show_period	= 1,
2800 	.set_width	= ahd_linux_set_width,
2801 	.show_width	= 1,
2802 	.set_dt		= ahd_linux_set_dt,
2803 	.show_dt	= 1,
2804 	.set_iu		= ahd_linux_set_iu,
2805 	.show_iu	= 1,
2806 	.set_qas	= ahd_linux_set_qas,
2807 	.show_qas	= 1,
2808 	.set_rd_strm	= ahd_linux_set_rd_strm,
2809 	.show_rd_strm	= 1,
2810 	.set_wr_flow	= ahd_linux_set_wr_flow,
2811 	.show_wr_flow	= 1,
2812 	.set_rti	= ahd_linux_set_rti,
2813 	.show_rti	= 1,
2814 	.set_pcomp_en	= ahd_linux_set_pcomp_en,
2815 	.show_pcomp_en	= 1,
2816 	.set_hold_mcs	= ahd_linux_set_hold_mcs,
2817 	.show_hold_mcs	= 1,
2818 	.get_signalling = ahd_linux_get_signalling,
2819 };
2820 
2821 static int __init
2822 ahd_linux_init(void)
2823 {
2824 	int	error = 0;
2825 
2826 	/*
2827 	 * If we've been passed any parameters, process them now.
2828 	 */
2829 	if (aic79xx)
2830 		aic79xx_setup(aic79xx);
2831 
2832 	ahd_linux_transport_template =
2833 		spi_attach_transport(&ahd_linux_transport_functions);
2834 	if (!ahd_linux_transport_template)
2835 		return -ENODEV;
2836 
2837 	scsi_transport_reserve_device(ahd_linux_transport_template,
2838 				      sizeof(struct ahd_linux_device));
2839 
2840 	error = ahd_linux_pci_init();
2841 	if (error)
2842 		spi_release_transport(ahd_linux_transport_template);
2843 	return error;
2844 }
2845 
2846 static void __exit
2847 ahd_linux_exit(void)
2848 {
2849 	ahd_linux_pci_exit();
2850 	spi_release_transport(ahd_linux_transport_template);
2851 }
2852 
2853 module_init(ahd_linux_init);
2854 module_exit(ahd_linux_exit);
2855