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