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