Automatic merge of rsync://www.parisc-linux.org/~jejb/git/scsi-for-linus-2.6.git
[linux-2.6] / drivers / scsi / aic7xxx / aic7xxx_osm.c
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 #include <scsi/scsi_transport.h>
126 #include <scsi/scsi_transport_spi.h>
127
128 static struct scsi_transport_template *ahc_linux_transport_template = NULL;
129
130 /*
131  * Include aiclib.c as part of our
132  * "module dependencies are hard" work around.
133  */
134 #include "aiclib.c"
135
136 #include <linux/init.h>         /* __setup */
137
138 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
139 #include "sd.h"                 /* For geometry detection */
140 #endif
141
142 #include <linux/mm.h>           /* For fetching system memory size */
143 #include <linux/blkdev.h>               /* For block_size() */
144 #include <linux/delay.h>        /* For ssleep/msleep */
145
146 /*
147  * Lock protecting manipulation of the ahc softc list.
148  */
149 spinlock_t ahc_list_spinlock;
150
151 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
152 /* For dynamic sglist size calculation. */
153 u_int ahc_linux_nseg;
154 #endif
155
156 /*
157  * Set this to the delay in seconds after SCSI bus reset.
158  * Note, we honor this only for the initial bus reset.
159  * The scsi error recovery code performs its own bus settle
160  * delay handling for error recovery actions.
161  */
162 #ifdef CONFIG_AIC7XXX_RESET_DELAY_MS
163 #define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS
164 #else
165 #define AIC7XXX_RESET_DELAY 5000
166 #endif
167
168 /*
169  * Control collection of SCSI transfer statistics for the /proc filesystem.
170  *
171  * NOTE: Do NOT enable this when running on kernels version 1.2.x and below.
172  * NOTE: This does affect performance since it has to maintain statistics.
173  */
174 #ifdef CONFIG_AIC7XXX_PROC_STATS
175 #define AIC7XXX_PROC_STATS
176 #endif
177
178 /*
179  * To change the default number of tagged transactions allowed per-device,
180  * add a line to the lilo.conf file like:
181  * append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
182  * which will result in the first four devices on the first two
183  * controllers being set to a tagged queue depth of 32.
184  *
185  * The tag_commands is an array of 16 to allow for wide and twin adapters.
186  * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
187  * for channel 1.
188  */
189 typedef struct {
190         uint8_t tag_commands[16];       /* Allow for wide/twin adapters. */
191 } adapter_tag_info_t;
192
193 /*
194  * Modify this as you see fit for your system.
195  *
196  * 0                    tagged queuing disabled
197  * 1 <= n <= 253        n == max tags ever dispatched.
198  *
199  * The driver will throttle the number of commands dispatched to a
200  * device if it returns queue full.  For devices with a fixed maximum
201  * queue depth, the driver will eventually determine this depth and
202  * lock it in (a console message is printed to indicate that a lock
203  * has occurred).  On some devices, queue full is returned for a temporary
204  * resource shortage.  These devices will return queue full at varying
205  * depths.  The driver will throttle back when the queue fulls occur and
206  * attempt to slowly increase the depth over time as the device recovers
207  * from the resource shortage.
208  *
209  * In this example, the first line will disable tagged queueing for all
210  * the devices on the first probed aic7xxx adapter.
211  *
212  * The second line enables tagged queueing with 4 commands/LUN for IDs
213  * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
214  * driver to attempt to use up to 64 tags for ID 1.
215  *
216  * The third line is the same as the first line.
217  *
218  * The fourth line disables tagged queueing for devices 0 and 3.  It
219  * enables tagged queueing for the other IDs, with 16 commands/LUN
220  * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
221  * IDs 2, 5-7, and 9-15.
222  */
223
224 /*
225  * NOTE: The below structure is for reference only, the actual structure
226  *       to modify in order to change things is just below this comment block.
227 adapter_tag_info_t aic7xxx_tag_info[] =
228 {
229         {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
230         {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
231         {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
232         {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
233 };
234 */
235
236 #ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
237 #define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
238 #else
239 #define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE
240 #endif
241
242 #define AIC7XXX_CONFIGED_TAG_COMMANDS {                                 \
243         AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,               \
244         AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,               \
245         AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,               \
246         AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,               \
247         AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,               \
248         AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,               \
249         AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE,               \
250         AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE                \
251 }
252
253 /*
254  * By default, use the number of commands specified by
255  * the users kernel configuration.
256  */
257 static adapter_tag_info_t aic7xxx_tag_info[] =
258 {
259         {AIC7XXX_CONFIGED_TAG_COMMANDS},
260         {AIC7XXX_CONFIGED_TAG_COMMANDS},
261         {AIC7XXX_CONFIGED_TAG_COMMANDS},
262         {AIC7XXX_CONFIGED_TAG_COMMANDS},
263         {AIC7XXX_CONFIGED_TAG_COMMANDS},
264         {AIC7XXX_CONFIGED_TAG_COMMANDS},
265         {AIC7XXX_CONFIGED_TAG_COMMANDS},
266         {AIC7XXX_CONFIGED_TAG_COMMANDS},
267         {AIC7XXX_CONFIGED_TAG_COMMANDS},
268         {AIC7XXX_CONFIGED_TAG_COMMANDS},
269         {AIC7XXX_CONFIGED_TAG_COMMANDS},
270         {AIC7XXX_CONFIGED_TAG_COMMANDS},
271         {AIC7XXX_CONFIGED_TAG_COMMANDS},
272         {AIC7XXX_CONFIGED_TAG_COMMANDS},
273         {AIC7XXX_CONFIGED_TAG_COMMANDS},
274         {AIC7XXX_CONFIGED_TAG_COMMANDS}
275 };
276
277 /*
278  * There should be a specific return value for this in scsi.h, but
279  * it seems that most drivers ignore it.
280  */
281 #define DID_UNDERFLOW   DID_ERROR
282
283 void
284 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
285 {
286         printk("(scsi%d:%c:%d:%d): ",
287                ahc->platform_data->host->host_no,
288                scb != NULL ? SCB_GET_CHANNEL(ahc, scb) : 'X',
289                scb != NULL ? SCB_GET_TARGET(ahc, scb) : -1,
290                scb != NULL ? SCB_GET_LUN(scb) : -1);
291 }
292
293 /*
294  * XXX - these options apply unilaterally to _all_ 274x/284x/294x
295  *       cards in the system.  This should be fixed.  Exceptions to this
296  *       rule are noted in the comments.
297  */
298
299 /*
300  * Skip the scsi bus reset.  Non 0 make us skip the reset at startup.  This
301  * has no effect on any later resets that might occur due to things like
302  * SCSI bus timeouts.
303  */
304 static uint32_t aic7xxx_no_reset;
305
306 /*
307  * Certain PCI motherboards will scan PCI devices from highest to lowest,
308  * others scan from lowest to highest, and they tend to do all kinds of
309  * strange things when they come into contact with PCI bridge chips.  The
310  * net result of all this is that the PCI card that is actually used to boot
311  * the machine is very hard to detect.  Most motherboards go from lowest
312  * PCI slot number to highest, and the first SCSI controller found is the
313  * one you boot from.  The only exceptions to this are when a controller
314  * has its BIOS disabled.  So, we by default sort all of our SCSI controllers
315  * from lowest PCI slot number to highest PCI slot number.  We also force
316  * all controllers with their BIOS disabled to the end of the list.  This
317  * works on *almost* all computers.  Where it doesn't work, we have this
318  * option.  Setting this option to non-0 will reverse the order of the sort
319  * to highest first, then lowest, but will still leave cards with their BIOS
320  * disabled at the very end.  That should fix everyone up unless there are
321  * really strange cirumstances.
322  */
323 static uint32_t aic7xxx_reverse_scan;
324
325 /*
326  * Should we force EXTENDED translation on a controller.
327  *     0 == Use whatever is in the SEEPROM or default to off
328  *     1 == Use whatever is in the SEEPROM or default to on
329  */
330 static uint32_t aic7xxx_extended;
331
332 /*
333  * PCI bus parity checking of the Adaptec controllers.  This is somewhat
334  * dubious at best.  To my knowledge, this option has never actually
335  * solved a PCI parity problem, but on certain machines with broken PCI
336  * chipset configurations where stray PCI transactions with bad parity are
337  * the norm rather than the exception, the error messages can be overwelming.
338  * It's included in the driver for completeness.
339  *   0     = Shut off PCI parity check
340  *   non-0 = reverse polarity pci parity checking
341  */
342 static uint32_t aic7xxx_pci_parity = ~0;
343
344 /*
345  * Certain newer motherboards have put new PCI based devices into the
346  * IO spaces that used to typically be occupied by VLB or EISA cards.
347  * This overlap can cause these newer motherboards to lock up when scanned
348  * for older EISA and VLB devices.  Setting this option to non-0 will
349  * cause the driver to skip scanning for any VLB or EISA controllers and
350  * only support the PCI controllers.  NOTE: this means that if the kernel
351  * os compiled with PCI support disabled, then setting this to non-0
352  * would result in never finding any devices :)
353  */
354 #ifndef CONFIG_AIC7XXX_PROBE_EISA_VL
355 uint32_t aic7xxx_probe_eisa_vl;
356 #else
357 uint32_t aic7xxx_probe_eisa_vl = ~0;
358 #endif
359
360 /*
361  * There are lots of broken chipsets in the world.  Some of them will
362  * violate the PCI spec when we issue byte sized memory writes to our
363  * controller.  I/O mapped register access, if allowed by the given
364  * platform, will work in almost all cases.
365  */
366 uint32_t aic7xxx_allow_memio = ~0;
367
368 /*
369  * aic7xxx_detect() has been run, so register all device arrivals
370  * immediately with the system rather than deferring to the sorted
371  * attachment performed by aic7xxx_detect().
372  */
373 int aic7xxx_detect_complete;
374
375 /*
376  * So that we can set how long each device is given as a selection timeout.
377  * The table of values goes like this:
378  *   0 - 256ms
379  *   1 - 128ms
380  *   2 - 64ms
381  *   3 - 32ms
382  * We default to 256ms because some older devices need a longer time
383  * to respond to initial selection.
384  */
385 static uint32_t aic7xxx_seltime;
386
387 /*
388  * Certain devices do not perform any aging on commands.  Should the
389  * device be saturated by commands in one portion of the disk, it is
390  * possible for transactions on far away sectors to never be serviced.
391  * To handle these devices, we can periodically send an ordered tag to
392  * force all outstanding transactions to be serviced prior to a new
393  * transaction.
394  */
395 uint32_t aic7xxx_periodic_otag;
396
397 /*
398  * Module information and settable options.
399  */
400 static char *aic7xxx = NULL;
401
402 MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
403 MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
404 MODULE_LICENSE("Dual BSD/GPL");
405 MODULE_VERSION(AIC7XXX_DRIVER_VERSION);
406 module_param(aic7xxx, charp, 0444);
407 MODULE_PARM_DESC(aic7xxx,
408 "period delimited, options string.\n"
409 "       verbose                 Enable verbose/diagnostic logging\n"
410 "       allow_memio             Allow device registers to be memory mapped\n"
411 "       debug                   Bitmask of debug values to enable\n"
412 "       no_probe                Toggle EISA/VLB controller probing\n"
413 "       probe_eisa_vl           Toggle EISA/VLB controller probing\n"
414 "       no_reset                Supress initial bus resets\n"
415 "       extended                Enable extended geometry on all controllers\n"
416 "       periodic_otag           Send an ordered tagged transaction\n"
417 "                               periodically to prevent tag starvation.\n"
418 "                               This may be required by some older disk\n"
419 "                               drives or RAID arrays.\n"
420 "       reverse_scan            Sort PCI devices highest Bus/Slot to lowest\n"
421 "       tag_info:<tag_str>      Set per-target tag depth\n"
422 "       global_tag_depth:<int>  Global tag depth for every target\n"
423 "                               on every bus\n"
424 "       seltime:<int>           Selection Timeout\n"
425 "                               (0/256ms,1/128ms,2/64ms,3/32ms)\n"
426 "\n"
427 "       Sample /etc/modprobe.conf line:\n"
428 "               Toggle EISA/VLB probing\n"
429 "               Set tag depth on Controller 1/Target 1 to 10 tags\n"
430 "               Shorten the selection timeout to 128ms\n"
431 "\n"
432 "       options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n"
433 );
434
435 static void ahc_linux_handle_scsi_status(struct ahc_softc *,
436                                          struct ahc_linux_device *,
437                                          struct scb *);
438 static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
439                                          Scsi_Cmnd *cmd);
440 static void ahc_linux_sem_timeout(u_long arg);
441 static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
442 static void ahc_linux_release_simq(u_long arg);
443 static void ahc_linux_dev_timed_unfreeze(u_long arg);
444 static int  ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag);
445 static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
446 static void ahc_linux_size_nseg(void);
447 static void ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc);
448 static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
449                                      struct ahc_devinfo *devinfo);
450 static void ahc_linux_device_queue_depth(struct ahc_softc *ahc,
451                                          struct ahc_linux_device *dev);
452 static struct ahc_linux_target* ahc_linux_alloc_target(struct ahc_softc*,
453                                                        u_int, u_int);
454 static void                     ahc_linux_free_target(struct ahc_softc*,
455                                                       struct ahc_linux_target*);
456 static struct ahc_linux_device* ahc_linux_alloc_device(struct ahc_softc*,
457                                                        struct ahc_linux_target*,
458                                                        u_int);
459 static void                     ahc_linux_free_device(struct ahc_softc*,
460                                                       struct ahc_linux_device*);
461 static void ahc_linux_run_device_queue(struct ahc_softc*,
462                                        struct ahc_linux_device*);
463 static void ahc_linux_setup_tag_info_global(char *p);
464 static aic_option_callback_t ahc_linux_setup_tag_info;
465 static int  aic7xxx_setup(char *s);
466 static int  ahc_linux_next_unit(void);
467 static void ahc_runq_tasklet(unsigned long data);
468 static struct ahc_cmd *ahc_linux_run_complete_queue(struct ahc_softc *ahc);
469
470 /********************************* Inlines ************************************/
471 static __inline void ahc_schedule_runq(struct ahc_softc *ahc);
472 static __inline struct ahc_linux_device*
473                      ahc_linux_get_device(struct ahc_softc *ahc, u_int channel,
474                                           u_int target, u_int lun, int alloc);
475 static __inline void ahc_schedule_completeq(struct ahc_softc *ahc);
476 static __inline void ahc_linux_check_device_queue(struct ahc_softc *ahc,
477                                                   struct ahc_linux_device *dev);
478 static __inline struct ahc_linux_device *
479                      ahc_linux_next_device_to_run(struct ahc_softc *ahc);
480 static __inline void ahc_linux_run_device_queues(struct ahc_softc *ahc);
481 static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
482
483 static __inline int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
484                                       struct ahc_dma_seg *sg,
485                                       dma_addr_t addr, bus_size_t len);
486
487 static __inline void
488 ahc_schedule_completeq(struct ahc_softc *ahc)
489 {
490         if ((ahc->platform_data->flags & AHC_RUN_CMPLT_Q_TIMER) == 0) {
491                 ahc->platform_data->flags |= AHC_RUN_CMPLT_Q_TIMER;
492                 ahc->platform_data->completeq_timer.expires = jiffies;
493                 add_timer(&ahc->platform_data->completeq_timer);
494         }
495 }
496
497 /*
498  * Must be called with our lock held.
499  */
500 static __inline void
501 ahc_schedule_runq(struct ahc_softc *ahc)
502 {
503         tasklet_schedule(&ahc->platform_data->runq_tasklet);
504 }
505
506 static __inline struct ahc_linux_device*
507 ahc_linux_get_device(struct ahc_softc *ahc, u_int channel, u_int target,
508                      u_int lun, int alloc)
509 {
510         struct ahc_linux_target *targ;
511         struct ahc_linux_device *dev;
512         u_int target_offset;
513
514         target_offset = target;
515         if (channel != 0)
516                 target_offset += 8;
517         targ = ahc->platform_data->targets[target_offset];
518         if (targ == NULL) {
519                 if (alloc != 0) {
520                         targ = ahc_linux_alloc_target(ahc, channel, target);
521                         if (targ == NULL)
522                                 return (NULL);
523                 } else
524                         return (NULL);
525         }
526         dev = targ->devices[lun];
527         if (dev == NULL && alloc != 0)
528                 dev = ahc_linux_alloc_device(ahc, targ, lun);
529         return (dev);
530 }
531
532 #define AHC_LINUX_MAX_RETURNED_ERRORS 4
533 static struct ahc_cmd *
534 ahc_linux_run_complete_queue(struct ahc_softc *ahc)
535 {
536         struct  ahc_cmd *acmd;
537         u_long  done_flags;
538         int     with_errors;
539
540         with_errors = 0;
541         ahc_done_lock(ahc, &done_flags);
542         while ((acmd = TAILQ_FIRST(&ahc->platform_data->completeq)) != NULL) {
543                 Scsi_Cmnd *cmd;
544
545                 if (with_errors > AHC_LINUX_MAX_RETURNED_ERRORS) {
546                         /*
547                          * Linux uses stack recursion to requeue
548                          * commands that need to be retried.  Avoid
549                          * blowing out the stack by "spoon feeding"
550                          * commands that completed with error back
551                          * the operating system in case they are going
552                          * to be retried. "ick"
553                          */
554                         ahc_schedule_completeq(ahc);
555                         break;
556                 }
557                 TAILQ_REMOVE(&ahc->platform_data->completeq,
558                              acmd, acmd_links.tqe);
559                 cmd = &acmd_scsi_cmd(acmd);
560                 cmd->host_scribble = NULL;
561                 if (ahc_cmd_get_transaction_status(cmd) != DID_OK
562                  || (cmd->result & 0xFF) != SCSI_STATUS_OK)
563                         with_errors++;
564
565                 cmd->scsi_done(cmd);
566         }
567         ahc_done_unlock(ahc, &done_flags);
568         return (acmd);
569 }
570
571 static __inline void
572 ahc_linux_check_device_queue(struct ahc_softc *ahc,
573                              struct ahc_linux_device *dev)
574 {
575         if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) != 0
576          && dev->active == 0) {
577                 dev->flags &= ~AHC_DEV_FREEZE_TIL_EMPTY;
578                 dev->qfrozen--;
579         }
580
581         if (TAILQ_FIRST(&dev->busyq) == NULL
582          || dev->openings == 0 || dev->qfrozen != 0)
583                 return;
584
585         ahc_linux_run_device_queue(ahc, dev);
586 }
587
588 static __inline struct ahc_linux_device *
589 ahc_linux_next_device_to_run(struct ahc_softc *ahc)
590 {
591         
592         if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0
593             || (ahc->platform_data->qfrozen != 0))
594                 return (NULL);
595         return (TAILQ_FIRST(&ahc->platform_data->device_runq));
596 }
597
598 static __inline void
599 ahc_linux_run_device_queues(struct ahc_softc *ahc)
600 {
601         struct ahc_linux_device *dev;
602
603         while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) {
604                 TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links);
605                 dev->flags &= ~AHC_DEV_ON_RUN_LIST;
606                 ahc_linux_check_device_queue(ahc, dev);
607         }
608 }
609
610 static __inline void
611 ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
612 {
613         Scsi_Cmnd *cmd;
614
615         cmd = scb->io_ctx;
616         ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
617         if (cmd->use_sg != 0) {
618                 struct scatterlist *sg;
619
620                 sg = (struct scatterlist *)cmd->request_buffer;
621                 pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg,
622                              cmd->sc_data_direction);
623         } else if (cmd->request_bufflen != 0) {
624                 pci_unmap_single(ahc->dev_softc,
625                                  scb->platform_data->buf_busaddr,
626                                  cmd->request_bufflen,
627                                  cmd->sc_data_direction);
628         }
629 }
630
631 static __inline int
632 ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
633                   struct ahc_dma_seg *sg, dma_addr_t addr, bus_size_t len)
634 {
635         int      consumed;
636
637         if ((scb->sg_count + 1) > AHC_NSEG)
638                 panic("Too few segs for dma mapping.  "
639                       "Increase AHC_NSEG\n");
640
641         consumed = 1;
642         sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
643         scb->platform_data->xfer_len += len;
644
645         if (sizeof(dma_addr_t) > 4
646          && (ahc->flags & AHC_39BIT_ADDRESSING) != 0)
647                 len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK;
648
649         sg->len = ahc_htole32(len);
650         return (consumed);
651 }
652
653 /************************  Host template entry points *************************/
654 static int         ahc_linux_detect(Scsi_Host_Template *);
655 static int         ahc_linux_queue(Scsi_Cmnd *, void (*)(Scsi_Cmnd *));
656 static const char *ahc_linux_info(struct Scsi_Host *);
657 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
658 static int         ahc_linux_slave_alloc(Scsi_Device *);
659 static int         ahc_linux_slave_configure(Scsi_Device *);
660 static void        ahc_linux_slave_destroy(Scsi_Device *);
661 #if defined(__i386__)
662 static int         ahc_linux_biosparam(struct scsi_device*,
663                                        struct block_device*,
664                                        sector_t, int[]);
665 #endif
666 #else
667 static int         ahc_linux_release(struct Scsi_Host *);
668 static void        ahc_linux_select_queue_depth(struct Scsi_Host *host,
669                                                 Scsi_Device *scsi_devs);
670 #if defined(__i386__)
671 static int         ahc_linux_biosparam(Disk *, kdev_t, int[]);
672 #endif
673 #endif
674 static int         ahc_linux_bus_reset(Scsi_Cmnd *);
675 static int         ahc_linux_dev_reset(Scsi_Cmnd *);
676 static int         ahc_linux_abort(Scsi_Cmnd *);
677
678 /*
679  * Calculate a safe value for AHC_NSEG (as expressed through ahc_linux_nseg).
680  *
681  * In pre-2.5.X...
682  * The midlayer allocates an S/G array dynamically when a command is issued
683  * using SCSI malloc.  This array, which is in an OS dependent format that
684  * must later be copied to our private S/G list, is sized to house just the
685  * number of segments needed for the current transfer.  Since the code that
686  * sizes the SCSI malloc pool does not take into consideration fragmentation
687  * of the pool, executing transactions numbering just a fraction of our
688  * concurrent transaction limit with list lengths aproaching AHC_NSEG will
689  * quickly depleat the SCSI malloc pool of usable space.  Unfortunately, the
690  * mid-layer does not properly handle this scsi malloc failures for the S/G
691  * array and the result can be a lockup of the I/O subsystem.  We try to size
692  * our S/G list so that it satisfies our drivers allocation requirements in
693  * addition to avoiding fragmentation of the SCSI malloc pool.
694  */
695 static void
696 ahc_linux_size_nseg(void)
697 {
698 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
699         u_int cur_size;
700         u_int best_size;
701
702         /*
703          * The SCSI allocator rounds to the nearest 512 bytes
704          * an cannot allocate across a page boundary.  Our algorithm
705          * is to start at 1K of scsi malloc space per-command and
706          * loop through all factors of the PAGE_SIZE and pick the best.
707          */
708         best_size = 0;
709         for (cur_size = 1024; cur_size <= PAGE_SIZE; cur_size *= 2) {
710                 u_int nseg;
711
712                 nseg = cur_size / sizeof(struct scatterlist);
713                 if (nseg < AHC_LINUX_MIN_NSEG)
714                         continue;
715
716                 if (best_size == 0) {
717                         best_size = cur_size;
718                         ahc_linux_nseg = nseg;
719                 } else {
720                         u_int best_rem;
721                         u_int cur_rem;
722
723                         /*
724                          * Compare the traits of the current "best_size"
725                          * with the current size to determine if the
726                          * current size is a better size.
727                          */
728                         best_rem = best_size % sizeof(struct scatterlist);
729                         cur_rem = cur_size % sizeof(struct scatterlist);
730                         if (cur_rem < best_rem) {
731                                 best_size = cur_size;
732                                 ahc_linux_nseg = nseg;
733                         }
734                 }
735         }
736 #endif
737 }
738
739 /*
740  * Try to detect an Adaptec 7XXX controller.
741  */
742 static int
743 ahc_linux_detect(Scsi_Host_Template *template)
744 {
745         struct  ahc_softc *ahc;
746         int     found = 0;
747
748 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
749         /*
750          * It is a bug that the upper layer takes
751          * this lock just prior to calling us.
752          */
753         spin_unlock_irq(&io_request_lock);
754 #endif
755
756         /*
757          * Sanity checking of Linux SCSI data structures so
758          * that some of our hacks^H^H^H^H^Hassumptions aren't
759          * violated.
760          */
761         if (offsetof(struct ahc_cmd_internal, end)
762           > offsetof(struct scsi_cmnd, host_scribble)) {
763                 printf("ahc_linux_detect: SCSI data structures changed.\n");
764                 printf("ahc_linux_detect: Unable to attach\n");
765                 return (0);
766         }
767         ahc_linux_size_nseg();
768         /*
769          * If we've been passed any parameters, process them now.
770          */
771         if (aic7xxx)
772                 aic7xxx_setup(aic7xxx);
773
774         template->proc_name = "aic7xxx";
775
776         /*
777          * Initialize our softc list lock prior to
778          * probing for any adapters.
779          */
780         ahc_list_lockinit();
781
782         found = ahc_linux_pci_init();
783         if (!ahc_linux_eisa_init())
784                 found++;
785         
786         /*
787          * Register with the SCSI layer all
788          * controllers we've found.
789          */
790         TAILQ_FOREACH(ahc, &ahc_tailq, links) {
791
792                 if (ahc_linux_register_host(ahc, template) == 0)
793                         found++;
794         }
795
796 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
797         spin_lock_irq(&io_request_lock);
798 #endif
799         aic7xxx_detect_complete++;
800
801         return (found);
802 }
803
804 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
805 /*
806  * Free the passed in Scsi_Host memory structures prior to unloading the
807  * module.
808  */
809 int
810 ahc_linux_release(struct Scsi_Host * host)
811 {
812         struct ahc_softc *ahc;
813         u_long l;
814
815         ahc_list_lock(&l);
816         if (host != NULL) {
817
818                 /*
819                  * We should be able to just perform
820                  * the free directly, but check our
821                  * list for extra sanity.
822                  */
823                 ahc = ahc_find_softc(*(struct ahc_softc **)host->hostdata);
824                 if (ahc != NULL) {
825                         u_long s;
826
827                         ahc_lock(ahc, &s);
828                         ahc_intr_enable(ahc, FALSE);
829                         ahc_unlock(ahc, &s);
830                         ahc_free(ahc);
831                 }
832         }
833         ahc_list_unlock(&l);
834         return (0);
835 }
836 #endif
837
838 /*
839  * Return a string describing the driver.
840  */
841 static const char *
842 ahc_linux_info(struct Scsi_Host *host)
843 {
844         static char buffer[512];
845         char    ahc_info[256];
846         char   *bp;
847         struct ahc_softc *ahc;
848
849         bp = &buffer[0];
850         ahc = *(struct ahc_softc **)host->hostdata;
851         memset(bp, 0, sizeof(buffer));
852         strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev ");
853         strcat(bp, AIC7XXX_DRIVER_VERSION);
854         strcat(bp, "\n");
855         strcat(bp, "        <");
856         strcat(bp, ahc->description);
857         strcat(bp, ">\n");
858         strcat(bp, "        ");
859         ahc_controller_info(ahc, ahc_info);
860         strcat(bp, ahc_info);
861         strcat(bp, "\n");
862
863         return (bp);
864 }
865
866 /*
867  * Queue an SCB to the controller.
868  */
869 static int
870 ahc_linux_queue(Scsi_Cmnd * cmd, void (*scsi_done) (Scsi_Cmnd *))
871 {
872         struct   ahc_softc *ahc;
873         struct   ahc_linux_device *dev;
874         u_long   flags;
875
876         ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
877
878         /*
879          * Save the callback on completion function.
880          */
881         cmd->scsi_done = scsi_done;
882
883         ahc_midlayer_entrypoint_lock(ahc, &flags);
884
885         /*
886          * Close the race of a command that was in the process of
887          * being queued to us just as our simq was frozen.  Let
888          * DV commands through so long as we are only frozen to
889          * perform DV.
890          */
891         if (ahc->platform_data->qfrozen != 0) {
892
893                 ahc_cmd_set_transaction_status(cmd, CAM_REQUEUE_REQ);
894                 ahc_linux_queue_cmd_complete(ahc, cmd);
895                 ahc_schedule_completeq(ahc);
896                 ahc_midlayer_entrypoint_unlock(ahc, &flags);
897                 return (0);
898         }
899         dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
900                                    cmd->device->lun, /*alloc*/TRUE);
901         if (dev == NULL) {
902                 ahc_cmd_set_transaction_status(cmd, CAM_RESRC_UNAVAIL);
903                 ahc_linux_queue_cmd_complete(ahc, cmd);
904                 ahc_schedule_completeq(ahc);
905                 ahc_midlayer_entrypoint_unlock(ahc, &flags);
906                 printf("%s: aic7xxx_linux_queue - Unable to allocate device!\n",
907                        ahc_name(ahc));
908                 return (0);
909         }
910         cmd->result = CAM_REQ_INPROG << 16;
911         TAILQ_INSERT_TAIL(&dev->busyq, (struct ahc_cmd *)cmd, acmd_links.tqe);
912         if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) {
913                 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links);
914                 dev->flags |= AHC_DEV_ON_RUN_LIST;
915                 ahc_linux_run_device_queues(ahc);
916         }
917         ahc_midlayer_entrypoint_unlock(ahc, &flags);
918         return (0);
919 }
920
921 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
922 static int
923 ahc_linux_slave_alloc(Scsi_Device *device)
924 {
925         struct  ahc_softc *ahc;
926
927         ahc = *((struct ahc_softc **)device->host->hostdata);
928         if (bootverbose)
929                 printf("%s: Slave Alloc %d\n", ahc_name(ahc), device->id);
930         return (0);
931 }
932
933 static int
934 ahc_linux_slave_configure(Scsi_Device *device)
935 {
936         struct  ahc_softc *ahc;
937         struct  ahc_linux_device *dev;
938         u_long  flags;
939
940         ahc = *((struct ahc_softc **)device->host->hostdata);
941         if (bootverbose)
942                 printf("%s: Slave Configure %d\n", ahc_name(ahc), device->id);
943         ahc_midlayer_entrypoint_lock(ahc, &flags);
944         /*
945          * Since Linux has attached to the device, configure
946          * it so we don't free and allocate the device
947          * structure on every command.
948          */
949         dev = ahc_linux_get_device(ahc, device->channel,
950                                    device->id, device->lun,
951                                    /*alloc*/TRUE);
952         if (dev != NULL) {
953                 dev->flags &= ~AHC_DEV_UNCONFIGURED;
954                 dev->scsi_device = device;
955                 ahc_linux_device_queue_depth(ahc, dev);
956         }
957         ahc_midlayer_entrypoint_unlock(ahc, &flags);
958
959         /* Initial Domain Validation */
960         if (!spi_initial_dv(device->sdev_target))
961                 spi_dv_device(device);
962
963         return (0);
964 }
965
966 static void
967 ahc_linux_slave_destroy(Scsi_Device *device)
968 {
969         struct  ahc_softc *ahc;
970         struct  ahc_linux_device *dev;
971         u_long  flags;
972
973         ahc = *((struct ahc_softc **)device->host->hostdata);
974         if (bootverbose)
975                 printf("%s: Slave Destroy %d\n", ahc_name(ahc), device->id);
976         ahc_midlayer_entrypoint_lock(ahc, &flags);
977         dev = ahc_linux_get_device(ahc, device->channel,
978                                    device->id, device->lun,
979                                            /*alloc*/FALSE);
980         /*
981          * Filter out "silly" deletions of real devices by only
982          * deleting devices that have had slave_configure()
983          * called on them.  All other devices that have not
984          * been configured will automatically be deleted by
985          * the refcounting process.
986          */
987         if (dev != NULL
988          && (dev->flags & AHC_DEV_SLAVE_CONFIGURED) != 0) {
989                 dev->flags |= AHC_DEV_UNCONFIGURED;
990                 if (TAILQ_EMPTY(&dev->busyq)
991                  && dev->active == 0
992                  && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0)
993                         ahc_linux_free_device(ahc, dev);
994         }
995         ahc_midlayer_entrypoint_unlock(ahc, &flags);
996 }
997 #else
998 /*
999  * Sets the queue depth for each SCSI device hanging
1000  * off the input host adapter.
1001  */
1002 static void
1003 ahc_linux_select_queue_depth(struct Scsi_Host *host, Scsi_Device *scsi_devs)
1004 {
1005         Scsi_Device *device;
1006         Scsi_Device *ldev;
1007         struct  ahc_softc *ahc;
1008         u_long  flags;
1009
1010         ahc = *((struct ahc_softc **)host->hostdata);
1011         ahc_lock(ahc, &flags);
1012         for (device = scsi_devs; device != NULL; device = device->next) {
1013
1014                 /*
1015                  * Watch out for duplicate devices.  This works around
1016                  * some quirks in how the SCSI scanning code does its
1017                  * device management.
1018                  */
1019                 for (ldev = scsi_devs; ldev != device; ldev = ldev->next) {
1020                         if (ldev->host == device->host
1021                          && ldev->channel == device->channel
1022                          && ldev->id == device->id
1023                          && ldev->lun == device->lun)
1024                                 break;
1025                 }
1026                 /* Skip duplicate. */
1027                 if (ldev != device)
1028                         continue;
1029
1030                 if (device->host == host) {
1031                         struct   ahc_linux_device *dev;
1032
1033                         /*
1034                          * Since Linux has attached to the device, configure
1035                          * it so we don't free and allocate the device
1036                          * structure on every command.
1037                          */
1038                         dev = ahc_linux_get_device(ahc, device->channel,
1039                                                    device->id, device->lun,
1040                                                    /*alloc*/TRUE);
1041                         if (dev != NULL) {
1042                                 dev->flags &= ~AHC_DEV_UNCONFIGURED;
1043                                 dev->scsi_device = device;
1044                                 ahc_linux_device_queue_depth(ahc, dev);
1045                                 device->queue_depth = dev->openings
1046                                                     + dev->active;
1047                                 if ((dev->flags & (AHC_DEV_Q_BASIC
1048                                                 | AHC_DEV_Q_TAGGED)) == 0) {
1049                                         /*
1050                                          * We allow the OS to queue 2 untagged
1051                                          * transactions to us at any time even
1052                                          * though we can only execute them
1053                                          * serially on the controller/device.
1054                                          * This should remove some latency.
1055                                          */
1056                                         device->queue_depth = 2;
1057                                 }
1058                         }
1059                 }
1060         }
1061         ahc_unlock(ahc, &flags);
1062 }
1063 #endif
1064
1065 #if defined(__i386__)
1066 /*
1067  * Return the disk geometry for the given SCSI device.
1068  */
1069 static int
1070 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1071 ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1072                     sector_t capacity, int geom[])
1073 {
1074         uint8_t *bh;
1075 #else
1076 ahc_linux_biosparam(Disk *disk, kdev_t dev, int geom[])
1077 {
1078         struct  scsi_device *sdev = disk->device;
1079         u_long  capacity = disk->capacity;
1080         struct  buffer_head *bh;
1081 #endif
1082         int      heads;
1083         int      sectors;
1084         int      cylinders;
1085         int      ret;
1086         int      extended;
1087         struct   ahc_softc *ahc;
1088         u_int    channel;
1089
1090         ahc = *((struct ahc_softc **)sdev->host->hostdata);
1091         channel = sdev->channel;
1092
1093 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1094         bh = scsi_bios_ptable(bdev);
1095 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,17)
1096         bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, block_size(dev));
1097 #else
1098         bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, 1024);
1099 #endif
1100
1101         if (bh) {
1102                 ret = scsi_partsize(bh, capacity,
1103                                     &geom[2], &geom[0], &geom[1]);
1104 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1105                 kfree(bh);
1106 #else
1107                 brelse(bh);
1108 #endif
1109                 if (ret != -1)
1110                         return (ret);
1111         }
1112         heads = 64;
1113         sectors = 32;
1114         cylinders = aic_sector_div(capacity, heads, sectors);
1115
1116         if (aic7xxx_extended != 0)
1117                 extended = 1;
1118         else if (channel == 0)
1119                 extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
1120         else
1121                 extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
1122         if (extended && cylinders >= 1024) {
1123                 heads = 255;
1124                 sectors = 63;
1125                 cylinders = aic_sector_div(capacity, heads, sectors);
1126         }
1127         geom[0] = heads;
1128         geom[1] = sectors;
1129         geom[2] = cylinders;
1130         return (0);
1131 }
1132 #endif
1133
1134 /*
1135  * Abort the current SCSI command(s).
1136  */
1137 static int
1138 ahc_linux_abort(Scsi_Cmnd *cmd)
1139 {
1140         int error;
1141
1142         error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
1143         if (error != 0)
1144                 printf("aic7xxx_abort returns 0x%x\n", error);
1145         return (error);
1146 }
1147
1148 /*
1149  * Attempt to send a target reset message to the device that timed out.
1150  */
1151 static int
1152 ahc_linux_dev_reset(Scsi_Cmnd *cmd)
1153 {
1154         int error;
1155
1156         error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
1157         if (error != 0)
1158                 printf("aic7xxx_dev_reset returns 0x%x\n", error);
1159         return (error);
1160 }
1161
1162 /*
1163  * Reset the SCSI bus.
1164  */
1165 static int
1166 ahc_linux_bus_reset(Scsi_Cmnd *cmd)
1167 {
1168         struct ahc_softc *ahc;
1169         u_long s;
1170         int    found;
1171
1172         ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
1173         ahc_midlayer_entrypoint_lock(ahc, &s);
1174         found = ahc_reset_channel(ahc, cmd->device->channel + 'A',
1175                                   /*initiate reset*/TRUE);
1176         ahc_linux_run_complete_queue(ahc);
1177         ahc_midlayer_entrypoint_unlock(ahc, &s);
1178
1179         if (bootverbose)
1180                 printf("%s: SCSI bus reset delivered. "
1181                        "%d SCBs aborted.\n", ahc_name(ahc), found);
1182
1183         return SUCCESS;
1184 }
1185
1186 Scsi_Host_Template aic7xxx_driver_template = {
1187         .module                 = THIS_MODULE,
1188         .name                   = "aic7xxx",
1189         .proc_info              = ahc_linux_proc_info,
1190         .info                   = ahc_linux_info,
1191         .queuecommand           = ahc_linux_queue,
1192         .eh_abort_handler       = ahc_linux_abort,
1193         .eh_device_reset_handler = ahc_linux_dev_reset,
1194         .eh_bus_reset_handler   = ahc_linux_bus_reset,
1195 #if defined(__i386__)
1196         .bios_param             = ahc_linux_biosparam,
1197 #endif
1198         .can_queue              = AHC_MAX_QUEUE,
1199         .this_id                = -1,
1200         .cmd_per_lun            = 2,
1201         .use_clustering         = ENABLE_CLUSTERING,
1202         .slave_alloc            = ahc_linux_slave_alloc,
1203         .slave_configure        = ahc_linux_slave_configure,
1204         .slave_destroy          = ahc_linux_slave_destroy,
1205 };
1206
1207 /**************************** Tasklet Handler *********************************/
1208
1209 /*
1210  * In 2.4.X and above, this routine is called from a tasklet,
1211  * so we must re-acquire our lock prior to executing this code.
1212  * In all prior kernels, ahc_schedule_runq() calls this routine
1213  * directly and ahc_schedule_runq() is called with our lock held.
1214  */
1215 static void
1216 ahc_runq_tasklet(unsigned long data)
1217 {
1218         struct ahc_softc* ahc;
1219         struct ahc_linux_device *dev;
1220         u_long flags;
1221
1222         ahc = (struct ahc_softc *)data;
1223         ahc_lock(ahc, &flags);
1224         while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) {
1225         
1226                 TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links);
1227                 dev->flags &= ~AHC_DEV_ON_RUN_LIST;
1228                 ahc_linux_check_device_queue(ahc, dev);
1229                 /* Yeild to our interrupt handler */
1230                 ahc_unlock(ahc, &flags);
1231                 ahc_lock(ahc, &flags);
1232         }
1233         ahc_unlock(ahc, &flags);
1234 }
1235
1236 /******************************** Macros **************************************/
1237 #define BUILD_SCSIID(ahc, cmd)                                              \
1238         ((((cmd)->device->id << TID_SHIFT) & TID)                           \
1239         | (((cmd)->device->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \
1240         | (((cmd)->device->channel == 0) ? 0 : TWIN_CHNLB))
1241
1242 /******************************** Bus DMA *************************************/
1243 int
1244 ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent,
1245                    bus_size_t alignment, bus_size_t boundary,
1246                    dma_addr_t lowaddr, dma_addr_t highaddr,
1247                    bus_dma_filter_t *filter, void *filterarg,
1248                    bus_size_t maxsize, int nsegments,
1249                    bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
1250 {
1251         bus_dma_tag_t dmat;
1252
1253         dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
1254         if (dmat == NULL)
1255                 return (ENOMEM);
1256
1257         /*
1258          * Linux is very simplistic about DMA memory.  For now don't
1259          * maintain all specification information.  Once Linux supplies
1260          * better facilities for doing these operations, or the
1261          * needs of this particular driver change, we might need to do
1262          * more here.
1263          */
1264         dmat->alignment = alignment;
1265         dmat->boundary = boundary;
1266         dmat->maxsize = maxsize;
1267         *ret_tag = dmat;
1268         return (0);
1269 }
1270
1271 void
1272 ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
1273 {
1274         free(dmat, M_DEVBUF);
1275 }
1276
1277 int
1278 ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
1279                  int flags, bus_dmamap_t *mapp)
1280 {
1281         bus_dmamap_t map;
1282
1283         map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT);
1284         if (map == NULL)
1285                 return (ENOMEM);
1286         /*
1287          * Although we can dma data above 4GB, our
1288          * "consistent" memory is below 4GB for
1289          * space efficiency reasons (only need a 4byte
1290          * address).  For this reason, we have to reset
1291          * our dma mask when doing allocations.
1292          */
1293         if (ahc->dev_softc != NULL)
1294                 if (pci_set_dma_mask(ahc->dev_softc, 0xFFFFFFFF)) {
1295                         printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
1296                         kfree(map);
1297                         return (ENODEV);
1298                 }
1299         *vaddr = pci_alloc_consistent(ahc->dev_softc,
1300                                       dmat->maxsize, &map->bus_addr);
1301         if (ahc->dev_softc != NULL)
1302                 if (pci_set_dma_mask(ahc->dev_softc,
1303                                      ahc->platform_data->hw_dma_mask)) {
1304                         printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
1305                         kfree(map);
1306                         return (ENODEV);
1307                 }
1308         if (*vaddr == NULL)
1309                 return (ENOMEM);
1310         *mapp = map;
1311         return(0);
1312 }
1313
1314 void
1315 ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
1316                 void* vaddr, bus_dmamap_t map)
1317 {
1318         pci_free_consistent(ahc->dev_softc, dmat->maxsize,
1319                             vaddr, map->bus_addr);
1320 }
1321
1322 int
1323 ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map,
1324                 void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
1325                 void *cb_arg, int flags)
1326 {
1327         /*
1328          * Assume for now that this will only be used during
1329          * initialization and not for per-transaction buffer mapping.
1330          */
1331         bus_dma_segment_t stack_sg;
1332
1333         stack_sg.ds_addr = map->bus_addr;
1334         stack_sg.ds_len = dmat->maxsize;
1335         cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
1336         return (0);
1337 }
1338
1339 void
1340 ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
1341 {
1342         /*
1343          * The map may is NULL in our < 2.3.X implementation.
1344          * Now it's 2.6.5, but just in case...
1345          */
1346         BUG_ON(map == NULL);
1347         free(map, M_DEVBUF);
1348 }
1349
1350 int
1351 ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
1352 {
1353         /* Nothing to do */
1354         return (0);
1355 }
1356
1357 /********************* Platform Dependent Functions ***************************/
1358 /*
1359  * Compare "left hand" softc with "right hand" softc, returning:
1360  * < 0 - lahc has a lower priority than rahc
1361  *   0 - Softcs are equal
1362  * > 0 - lahc has a higher priority than rahc
1363  */
1364 int
1365 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1366 {
1367         int     value;
1368         int     rvalue;
1369         int     lvalue;
1370
1371         /*
1372          * Under Linux, cards are ordered as follows:
1373          *      1) VLB/EISA BIOS enabled devices sorted by BIOS address.
1374          *      2) PCI devices with BIOS enabled sorted by bus/slot/func.
1375          *      3) All remaining VLB/EISA devices sorted by ioport.
1376          *      4) All remaining PCI devices sorted by bus/slot/func.
1377          */
1378         value = (lahc->flags & AHC_BIOS_ENABLED)
1379               - (rahc->flags & AHC_BIOS_ENABLED);
1380         if (value != 0)
1381                 /* Controllers with BIOS enabled have a *higher* priority */
1382                 return (value);
1383
1384         /*
1385          * Same BIOS setting, now sort based on bus type.
1386          * EISA and VL controllers sort together.  EISA/VL
1387          * have higher priority than PCI.
1388          */
1389         rvalue = (rahc->chip & AHC_BUS_MASK);
1390         if (rvalue == AHC_VL)
1391                 rvalue = AHC_EISA;
1392         lvalue = (lahc->chip & AHC_BUS_MASK);
1393         if (lvalue == AHC_VL)
1394                 lvalue = AHC_EISA;
1395         value = rvalue - lvalue;
1396         if (value != 0)
1397                 return (value);
1398
1399         /* Still equal.  Sort by BIOS address, ioport, or bus/slot/func. */
1400         switch (rvalue) {
1401 #ifdef CONFIG_PCI
1402         case AHC_PCI:
1403         {
1404                 char primary_channel;
1405
1406                 if (aic7xxx_reverse_scan != 0)
1407                         value = ahc_get_pci_bus(lahc->dev_softc)
1408                               - ahc_get_pci_bus(rahc->dev_softc);
1409                 else
1410                         value = ahc_get_pci_bus(rahc->dev_softc)
1411                               - ahc_get_pci_bus(lahc->dev_softc);
1412                 if (value != 0)
1413                         break;
1414                 if (aic7xxx_reverse_scan != 0)
1415                         value = ahc_get_pci_slot(lahc->dev_softc)
1416                               - ahc_get_pci_slot(rahc->dev_softc);
1417                 else
1418                         value = ahc_get_pci_slot(rahc->dev_softc)
1419                               - ahc_get_pci_slot(lahc->dev_softc);
1420                 if (value != 0)
1421                         break;
1422                 /*
1423                  * On multi-function devices, the user can choose
1424                  * to have function 1 probed before function 0.
1425                  * Give whichever channel is the primary channel
1426                  * the highest priority.
1427                  */
1428                 primary_channel = (lahc->flags & AHC_PRIMARY_CHANNEL) + 'A';
1429                 value = -1;
1430                 if (lahc->channel == primary_channel)
1431                         value = 1;
1432                 break;
1433         }
1434 #endif
1435         case AHC_EISA:
1436                 if ((rahc->flags & AHC_BIOS_ENABLED) != 0) {
1437                         value = rahc->platform_data->bios_address
1438                               - lahc->platform_data->bios_address; 
1439                 } else {
1440                         value = rahc->bsh.ioport
1441                               - lahc->bsh.ioport; 
1442                 }
1443                 break;
1444         default:
1445                 panic("ahc_softc_sort: invalid bus type");
1446         }
1447         return (value);
1448 }
1449
1450 static void
1451 ahc_linux_setup_tag_info_global(char *p)
1452 {
1453         int tags, i, j;
1454
1455         tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
1456         printf("Setting Global Tags= %d\n", tags);
1457
1458         for (i = 0; i < NUM_ELEMENTS(aic7xxx_tag_info); i++) {
1459                 for (j = 0; j < AHC_NUM_TARGETS; j++) {
1460                         aic7xxx_tag_info[i].tag_commands[j] = tags;
1461                 }
1462         }
1463 }
1464
1465 static void
1466 ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
1467 {
1468
1469         if ((instance >= 0) && (targ >= 0)
1470          && (instance < NUM_ELEMENTS(aic7xxx_tag_info))
1471          && (targ < AHC_NUM_TARGETS)) {
1472                 aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff;
1473                 if (bootverbose)
1474                         printf("tag_info[%d:%d] = %d\n", instance, targ, value);
1475         }
1476 }
1477
1478 /*
1479  * Handle Linux boot parameters. This routine allows for assigning a value
1480  * to a parameter with a ':' between the parameter and the value.
1481  * ie. aic7xxx=stpwlev:1,extended
1482  */
1483 static int
1484 aic7xxx_setup(char *s)
1485 {
1486         int     i, n;
1487         char   *p;
1488         char   *end;
1489
1490         static struct {
1491                 const char *name;
1492                 uint32_t *flag;
1493         } options[] = {
1494                 { "extended", &aic7xxx_extended },
1495                 { "no_reset", &aic7xxx_no_reset },
1496                 { "verbose", &aic7xxx_verbose },
1497                 { "allow_memio", &aic7xxx_allow_memio},
1498 #ifdef AHC_DEBUG
1499                 { "debug", &ahc_debug },
1500 #endif
1501                 { "reverse_scan", &aic7xxx_reverse_scan },
1502                 { "no_probe", &aic7xxx_probe_eisa_vl },
1503                 { "probe_eisa_vl", &aic7xxx_probe_eisa_vl },
1504                 { "periodic_otag", &aic7xxx_periodic_otag },
1505                 { "pci_parity", &aic7xxx_pci_parity },
1506                 { "seltime", &aic7xxx_seltime },
1507                 { "tag_info", NULL },
1508                 { "global_tag_depth", NULL },
1509                 { "dv", NULL }
1510         };
1511
1512         end = strchr(s, '\0');
1513
1514         /*
1515          * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
1516          * will never be 0 in this case.
1517          */
1518         n = 0;
1519
1520         while ((p = strsep(&s, ",.")) != NULL) {
1521                 if (*p == '\0')
1522                         continue;
1523                 for (i = 0; i < NUM_ELEMENTS(options); i++) {
1524
1525                         n = strlen(options[i].name);
1526                         if (strncmp(options[i].name, p, n) == 0)
1527                                 break;
1528                 }
1529                 if (i == NUM_ELEMENTS(options))
1530                         continue;
1531
1532                 if (strncmp(p, "global_tag_depth", n) == 0) {
1533                         ahc_linux_setup_tag_info_global(p + n);
1534                 } else if (strncmp(p, "tag_info", n) == 0) {
1535                         s = aic_parse_brace_option("tag_info", p + n, end,
1536                             2, ahc_linux_setup_tag_info, 0);
1537                 } else if (p[n] == ':') {
1538                         *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
1539                 } else if (strncmp(p, "verbose", n) == 0) {
1540                         *(options[i].flag) = 1;
1541                 } else {
1542                         *(options[i].flag) ^= 0xFFFFFFFF;
1543                 }
1544         }
1545         return 1;
1546 }
1547
1548 __setup("aic7xxx=", aic7xxx_setup);
1549
1550 uint32_t aic7xxx_verbose;
1551
1552 int
1553 ahc_linux_register_host(struct ahc_softc *ahc, Scsi_Host_Template *template)
1554 {
1555         char     buf[80];
1556         struct   Scsi_Host *host;
1557         char    *new_name;
1558         u_long   s;
1559
1560         template->name = ahc->description;
1561         host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
1562         if (host == NULL)
1563                 return (ENOMEM);
1564
1565         *((struct ahc_softc **)host->hostdata) = ahc;
1566         ahc_lock(ahc, &s);
1567 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1568         scsi_assign_lock(host, &ahc->platform_data->spin_lock);
1569 #elif AHC_SCSI_HAS_HOST_LOCK != 0
1570         host->lock = &ahc->platform_data->spin_lock;
1571 #endif
1572         ahc->platform_data->host = host;
1573         host->can_queue = AHC_MAX_QUEUE;
1574         host->cmd_per_lun = 2;
1575         /* XXX No way to communicate the ID for multiple channels */
1576         host->this_id = ahc->our_id;
1577         host->irq = ahc->platform_data->irq;
1578         host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
1579         host->max_lun = AHC_NUM_LUNS;
1580         host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
1581         host->sg_tablesize = AHC_NSEG;
1582         ahc_set_unit(ahc, ahc_linux_next_unit());
1583         sprintf(buf, "scsi%d", host->host_no);
1584         new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
1585         if (new_name != NULL) {
1586                 strcpy(new_name, buf);
1587                 ahc_set_name(ahc, new_name);
1588         }
1589         host->unique_id = ahc->unit;
1590 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1591         scsi_set_pci_device(host, ahc->dev_softc);
1592 #endif
1593         ahc_linux_initialize_scsi_bus(ahc);
1594         ahc_intr_enable(ahc, TRUE);
1595         ahc_unlock(ahc, &s);
1596
1597         host->transportt = ahc_linux_transport_template;
1598
1599 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1600         scsi_add_host(host, (ahc->dev_softc ? &ahc->dev_softc->dev : NULL)); /* XXX handle failure */
1601         scsi_scan_host(host);
1602 #endif
1603         return (0);
1604 }
1605
1606 uint64_t
1607 ahc_linux_get_memsize(void)
1608 {
1609         struct sysinfo si;
1610
1611         si_meminfo(&si);
1612         return ((uint64_t)si.totalram << PAGE_SHIFT);
1613 }
1614
1615 /*
1616  * Find the smallest available unit number to use
1617  * for a new device.  We don't just use a static
1618  * count to handle the "repeated hot-(un)plug"
1619  * scenario.
1620  */
1621 static int
1622 ahc_linux_next_unit(void)
1623 {
1624         struct ahc_softc *ahc;
1625         int unit;
1626
1627         unit = 0;
1628 retry:
1629         TAILQ_FOREACH(ahc, &ahc_tailq, links) {
1630                 if (ahc->unit == unit) {
1631                         unit++;
1632                         goto retry;
1633                 }
1634         }
1635         return (unit);
1636 }
1637
1638 /*
1639  * Place the SCSI bus into a known state by either resetting it,
1640  * or forcing transfer negotiations on the next command to any
1641  * target.
1642  */
1643 void
1644 ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
1645 {
1646         int i;
1647         int numtarg;
1648
1649         i = 0;
1650         numtarg = 0;
1651
1652         if (aic7xxx_no_reset != 0)
1653                 ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
1654
1655         if ((ahc->flags & AHC_RESET_BUS_A) != 0)
1656                 ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
1657         else
1658                 numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1659
1660         if ((ahc->features & AHC_TWIN) != 0) {
1661
1662                 if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
1663                         ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
1664                 } else {
1665                         if (numtarg == 0)
1666                                 i = 8;
1667                         numtarg += 8;
1668                 }
1669         }
1670
1671         /*
1672          * Force negotiation to async for all targets that
1673          * will not see an initial bus reset.
1674          */
1675         for (; i < numtarg; i++) {
1676                 struct ahc_devinfo devinfo;
1677                 struct ahc_initiator_tinfo *tinfo;
1678                 struct ahc_tmode_tstate *tstate;
1679                 u_int our_id;
1680                 u_int target_id;
1681                 char channel;
1682
1683                 channel = 'A';
1684                 our_id = ahc->our_id;
1685                 target_id = i;
1686                 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
1687                         channel = 'B';
1688                         our_id = ahc->our_id_b;
1689                         target_id = i % 8;
1690                 }
1691                 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
1692                                             target_id, &tstate);
1693                 ahc_compile_devinfo(&devinfo, our_id, target_id,
1694                                     CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
1695                 ahc_update_neg_request(ahc, &devinfo, tstate,
1696                                        tinfo, AHC_NEG_ALWAYS);
1697         }
1698         /* Give the bus some time to recover */
1699         if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
1700                 ahc_linux_freeze_simq(ahc);
1701                 init_timer(&ahc->platform_data->reset_timer);
1702                 ahc->platform_data->reset_timer.data = (u_long)ahc;
1703                 ahc->platform_data->reset_timer.expires =
1704                     jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000;
1705                 ahc->platform_data->reset_timer.function =
1706                     ahc_linux_release_simq;
1707                 add_timer(&ahc->platform_data->reset_timer);
1708         }
1709 }
1710
1711 int
1712 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1713 {
1714
1715         ahc->platform_data =
1716             malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
1717         if (ahc->platform_data == NULL)
1718                 return (ENOMEM);
1719         memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
1720         TAILQ_INIT(&ahc->platform_data->completeq);
1721         TAILQ_INIT(&ahc->platform_data->device_runq);
1722         ahc->platform_data->irq = AHC_LINUX_NOIRQ;
1723         ahc->platform_data->hw_dma_mask = 0xFFFFFFFF;
1724         ahc_lockinit(ahc);
1725         ahc_done_lockinit(ahc);
1726         init_timer(&ahc->platform_data->completeq_timer);
1727         ahc->platform_data->completeq_timer.data = (u_long)ahc;
1728         ahc->platform_data->completeq_timer.function =
1729             (ahc_linux_callback_t *)ahc_linux_thread_run_complete_queue;
1730         init_MUTEX_LOCKED(&ahc->platform_data->eh_sem);
1731         tasklet_init(&ahc->platform_data->runq_tasklet, ahc_runq_tasklet,
1732                      (unsigned long)ahc);
1733         ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
1734         ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
1735         if (aic7xxx_pci_parity == 0)
1736                 ahc->flags |= AHC_DISABLE_PCI_PERR;
1737
1738         return (0);
1739 }
1740
1741 void
1742 ahc_platform_free(struct ahc_softc *ahc)
1743 {
1744         struct ahc_linux_target *targ;
1745         struct ahc_linux_device *dev;
1746         int i, j;
1747
1748         if (ahc->platform_data != NULL) {
1749                 del_timer_sync(&ahc->platform_data->completeq_timer);
1750                 tasklet_kill(&ahc->platform_data->runq_tasklet);
1751                 if (ahc->platform_data->host != NULL) {
1752 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1753                         scsi_remove_host(ahc->platform_data->host);
1754 #endif
1755                         scsi_host_put(ahc->platform_data->host);
1756                 }
1757
1758                 /* destroy all of the device and target objects */
1759                 for (i = 0; i < AHC_NUM_TARGETS; i++) {
1760                         targ = ahc->platform_data->targets[i];
1761                         if (targ != NULL) {
1762                                 /* Keep target around through the loop. */
1763                                 targ->refcount++;
1764                                 for (j = 0; j < AHC_NUM_LUNS; j++) {
1765
1766                                         if (targ->devices[j] == NULL)
1767                                                 continue;
1768                                         dev = targ->devices[j];
1769                                         ahc_linux_free_device(ahc, dev);
1770                                 }
1771                                 /*
1772                                  * Forcibly free the target now that
1773                                  * all devices are gone.
1774                                  */
1775                                 ahc_linux_free_target(ahc, targ);
1776                         }
1777                 }
1778
1779                 if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
1780                         free_irq(ahc->platform_data->irq, ahc);
1781                 if (ahc->tag == BUS_SPACE_PIO
1782                  && ahc->bsh.ioport != 0)
1783                         release_region(ahc->bsh.ioport, 256);
1784                 if (ahc->tag == BUS_SPACE_MEMIO
1785                  && ahc->bsh.maddr != NULL) {
1786                         iounmap(ahc->bsh.maddr);
1787                         release_mem_region(ahc->platform_data->mem_busaddr,
1788                                            0x1000);
1789                 }
1790 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1791                 /*
1792                  * In 2.4 we detach from the scsi midlayer before the PCI
1793                  * layer invokes our remove callback.  No per-instance
1794                  * detach is provided, so we must reach inside the PCI
1795                  * subsystem's internals and detach our driver manually.
1796                  */
1797                 if (ahc->dev_softc != NULL)
1798                         ahc->dev_softc->driver = NULL;
1799 #endif
1800                 free(ahc->platform_data, M_DEVBUF);
1801         }
1802 }
1803
1804 void
1805 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
1806 {
1807         ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1808                                 SCB_GET_CHANNEL(ahc, scb),
1809                                 SCB_GET_LUN(scb), SCB_LIST_NULL,
1810                                 ROLE_UNKNOWN, CAM_REQUEUE_REQ);
1811 }
1812
1813 void
1814 ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1815                       ahc_queue_alg alg)
1816 {
1817         struct ahc_linux_device *dev;
1818         int was_queuing;
1819         int now_queuing;
1820
1821         dev = ahc_linux_get_device(ahc, devinfo->channel - 'A',
1822                                    devinfo->target,
1823                                    devinfo->lun, /*alloc*/FALSE);
1824         if (dev == NULL)
1825                 return;
1826         was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
1827         switch (alg) {
1828         default:
1829         case AHC_QUEUE_NONE:
1830                 now_queuing = 0;
1831                 break; 
1832         case AHC_QUEUE_BASIC:
1833                 now_queuing = AHC_DEV_Q_BASIC;
1834                 break;
1835         case AHC_QUEUE_TAGGED:
1836                 now_queuing = AHC_DEV_Q_TAGGED;
1837                 break;
1838         }
1839         if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
1840          && (was_queuing != now_queuing)
1841          && (dev->active != 0)) {
1842                 dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
1843                 dev->qfrozen++;
1844         }
1845
1846         dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
1847         if (now_queuing) {
1848                 u_int usertags;
1849
1850                 usertags = ahc_linux_user_tagdepth(ahc, devinfo);
1851                 if (!was_queuing) {
1852                         /*
1853                          * Start out agressively and allow our
1854                          * dynamic queue depth algorithm to take
1855                          * care of the rest.
1856                          */
1857                         dev->maxtags = usertags;
1858                         dev->openings = dev->maxtags - dev->active;
1859                 }
1860                 if (dev->maxtags == 0) {
1861                         /*
1862                          * Queueing is disabled by the user.
1863                          */
1864                         dev->openings = 1;
1865                 } else if (alg == AHC_QUEUE_TAGGED) {
1866                         dev->flags |= AHC_DEV_Q_TAGGED;
1867                         if (aic7xxx_periodic_otag != 0)
1868                                 dev->flags |= AHC_DEV_PERIODIC_OTAG;
1869                 } else
1870                         dev->flags |= AHC_DEV_Q_BASIC;
1871         } else {
1872                 /* We can only have one opening. */
1873                 dev->maxtags = 0;
1874                 dev->openings =  1 - dev->active;
1875         }
1876 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1877         if (dev->scsi_device != NULL) {
1878                 switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
1879                 case AHC_DEV_Q_BASIC:
1880                         scsi_adjust_queue_depth(dev->scsi_device,
1881                                                 MSG_SIMPLE_TASK,
1882                                                 dev->openings + dev->active);
1883                         break;
1884                 case AHC_DEV_Q_TAGGED:
1885                         scsi_adjust_queue_depth(dev->scsi_device,
1886                                                 MSG_ORDERED_TASK,
1887                                                 dev->openings + dev->active);
1888                         break;
1889                 default:
1890                         /*
1891                          * We allow the OS to queue 2 untagged transactions to
1892                          * us at any time even though we can only execute them
1893                          * serially on the controller/device.  This should
1894                          * remove some latency.
1895                          */
1896                         scsi_adjust_queue_depth(dev->scsi_device,
1897                                                 /*NON-TAGGED*/0,
1898                                                 /*queue depth*/2);
1899                         break;
1900                 }
1901         }
1902 #endif
1903 }
1904
1905 int
1906 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
1907                         int lun, u_int tag, role_t role, uint32_t status)
1908 {
1909         int chan;
1910         int maxchan;
1911         int targ;
1912         int maxtarg;
1913         int clun;
1914         int maxlun;
1915         int count;
1916
1917         if (tag != SCB_LIST_NULL)
1918                 return (0);
1919
1920         chan = 0;
1921         if (channel != ALL_CHANNELS) {
1922                 chan = channel - 'A';
1923                 maxchan = chan + 1;
1924         } else {
1925                 maxchan = (ahc->features & AHC_TWIN) ? 2 : 1;
1926         }
1927         targ = 0;
1928         if (target != CAM_TARGET_WILDCARD) {
1929                 targ = target;
1930                 maxtarg = targ + 1;
1931         } else {
1932                 maxtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1933         }
1934         clun = 0;
1935         if (lun != CAM_LUN_WILDCARD) {
1936                 clun = lun;
1937                 maxlun = clun + 1;
1938         } else {
1939                 maxlun = AHC_NUM_LUNS;
1940         }
1941
1942         count = 0;
1943         for (; chan < maxchan; chan++) {
1944
1945                 for (; targ < maxtarg; targ++) {
1946
1947                         for (; clun < maxlun; clun++) {
1948                                 struct ahc_linux_device *dev;
1949                                 struct ahc_busyq *busyq;
1950                                 struct ahc_cmd *acmd;
1951
1952                                 dev = ahc_linux_get_device(ahc, chan,
1953                                                            targ, clun,
1954                                                            /*alloc*/FALSE);
1955                                 if (dev == NULL)
1956                                         continue;
1957
1958                                 busyq = &dev->busyq;
1959                                 while ((acmd = TAILQ_FIRST(busyq)) != NULL) {
1960                                         Scsi_Cmnd *cmd;
1961
1962                                         cmd = &acmd_scsi_cmd(acmd);
1963                                         TAILQ_REMOVE(busyq, acmd,
1964                                                      acmd_links.tqe);
1965                                         count++;
1966                                         cmd->result = status << 16;
1967                                         ahc_linux_queue_cmd_complete(ahc, cmd);
1968                                 }
1969                         }
1970                 }
1971         }
1972
1973         return (count);
1974 }
1975
1976 static void
1977 ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc)
1978 {
1979         u_long flags;
1980
1981         ahc_lock(ahc, &flags);
1982         del_timer(&ahc->platform_data->completeq_timer);
1983         ahc->platform_data->flags &= ~AHC_RUN_CMPLT_Q_TIMER;
1984         ahc_linux_run_complete_queue(ahc);
1985         ahc_unlock(ahc, &flags);
1986 }
1987
1988 static u_int
1989 ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1990 {
1991         static int warned_user;
1992         u_int tags;
1993
1994         tags = 0;
1995         if ((ahc->user_discenable & devinfo->target_mask) != 0) {
1996                 if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
1997                         if (warned_user == 0) {
1998
1999                                 printf(KERN_WARNING
2000 "aic7xxx: WARNING: Insufficient tag_info instances\n"
2001 "aic7xxx: for installed controllers. Using defaults\n"
2002 "aic7xxx: Please update the aic7xxx_tag_info array in\n"
2003 "aic7xxx: the aic7xxx_osm..c source file.\n");
2004                                 warned_user++;
2005                         }
2006                         tags = AHC_MAX_QUEUE;
2007                 } else {
2008                         adapter_tag_info_t *tag_info;
2009
2010                         tag_info = &aic7xxx_tag_info[ahc->unit];
2011                         tags = tag_info->tag_commands[devinfo->target_offset];
2012                         if (tags > AHC_MAX_QUEUE)
2013                                 tags = AHC_MAX_QUEUE;
2014                 }
2015         }
2016         return (tags);
2017 }
2018
2019 /*
2020  * Determines the queue depth for a given device.
2021  */
2022 static void
2023 ahc_linux_device_queue_depth(struct ahc_softc *ahc,
2024                              struct ahc_linux_device *dev)
2025 {
2026         struct  ahc_devinfo devinfo;
2027         u_int   tags;
2028
2029         ahc_compile_devinfo(&devinfo,
2030                             dev->target->channel == 0
2031                           ? ahc->our_id : ahc->our_id_b,
2032                             dev->target->target, dev->lun,
2033                             dev->target->channel == 0 ? 'A' : 'B',
2034                             ROLE_INITIATOR);
2035         tags = ahc_linux_user_tagdepth(ahc, &devinfo);
2036         if (tags != 0
2037          && dev->scsi_device != NULL
2038          && dev->scsi_device->tagged_supported != 0) {
2039
2040                 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
2041                 ahc_print_devinfo(ahc, &devinfo);
2042                 printf("Tagged Queuing enabled.  Depth %d\n", tags);
2043         } else {
2044                 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE);
2045         }
2046 }
2047
2048 static void
2049 ahc_linux_run_device_queue(struct ahc_softc *ahc, struct ahc_linux_device *dev)
2050 {
2051         struct   ahc_cmd *acmd;
2052         struct   scsi_cmnd *cmd;
2053         struct   scb *scb;
2054         struct   hardware_scb *hscb;
2055         struct   ahc_initiator_tinfo *tinfo;
2056         struct   ahc_tmode_tstate *tstate;
2057         uint16_t mask;
2058
2059         if ((dev->flags & AHC_DEV_ON_RUN_LIST) != 0)
2060                 panic("running device on run list");
2061
2062         while ((acmd = TAILQ_FIRST(&dev->busyq)) != NULL
2063             && dev->openings > 0 && dev->qfrozen == 0) {
2064
2065                 /*
2066                  * Schedule us to run later.  The only reason we are not
2067                  * running is because the whole controller Q is frozen.
2068                  */
2069                 if (ahc->platform_data->qfrozen != 0) {
2070                         TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
2071                                           dev, links);
2072                         dev->flags |= AHC_DEV_ON_RUN_LIST;
2073                         return;
2074                 }
2075                 /*
2076                  * Get an scb to use.
2077                  */
2078                 if ((scb = ahc_get_scb(ahc)) == NULL) {
2079                         TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
2080                                          dev, links);
2081                         dev->flags |= AHC_DEV_ON_RUN_LIST;
2082                         ahc->flags |= AHC_RESOURCE_SHORTAGE;
2083                         return;
2084                 }
2085                 TAILQ_REMOVE(&dev->busyq, acmd, acmd_links.tqe);
2086                 cmd = &acmd_scsi_cmd(acmd);
2087                 scb->io_ctx = cmd;
2088                 scb->platform_data->dev = dev;
2089                 hscb = scb->hscb;
2090                 cmd->host_scribble = (char *)scb;
2091
2092                 /*
2093                  * Fill out basics of the HSCB.
2094                  */
2095                 hscb->control = 0;
2096                 hscb->scsiid = BUILD_SCSIID(ahc, cmd);
2097                 hscb->lun = cmd->device->lun;
2098                 mask = SCB_GET_TARGET_MASK(ahc, scb);
2099                 tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
2100                                             SCB_GET_OUR_ID(scb),
2101                                             SCB_GET_TARGET(ahc, scb), &tstate);
2102                 hscb->scsirate = tinfo->scsirate;
2103                 hscb->scsioffset = tinfo->curr.offset;
2104                 if ((tstate->ultraenb & mask) != 0)
2105                         hscb->control |= ULTRAENB;
2106
2107                 if ((ahc->user_discenable & mask) != 0)
2108                         hscb->control |= DISCENB;
2109
2110                 if ((tstate->auto_negotiate & mask) != 0) {
2111                         scb->flags |= SCB_AUTO_NEGOTIATE;
2112                         scb->hscb->control |= MK_MESSAGE;
2113                 }
2114
2115                 if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
2116 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
2117                         int     msg_bytes;
2118                         uint8_t tag_msgs[2];
2119
2120                         msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
2121                         if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
2122                                 hscb->control |= tag_msgs[0];
2123                                 if (tag_msgs[0] == MSG_ORDERED_TASK)
2124                                         dev->commands_since_idle_or_otag = 0;
2125                         } else
2126 #endif
2127                         if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
2128                          && (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
2129                                 hscb->control |= MSG_ORDERED_TASK;
2130                                 dev->commands_since_idle_or_otag = 0;
2131                         } else {
2132                                 hscb->control |= MSG_SIMPLE_TASK;
2133                         }
2134                 }
2135
2136                 hscb->cdb_len = cmd->cmd_len;
2137                 if (hscb->cdb_len <= 12) {
2138                         memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
2139                 } else {
2140                         memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
2141                         scb->flags |= SCB_CDB32_PTR;
2142                 }
2143
2144                 scb->platform_data->xfer_len = 0;
2145                 ahc_set_residual(scb, 0);
2146                 ahc_set_sense_residual(scb, 0);
2147                 scb->sg_count = 0;
2148                 if (cmd->use_sg != 0) {
2149                         struct  ahc_dma_seg *sg;
2150                         struct  scatterlist *cur_seg;
2151                         struct  scatterlist *end_seg;
2152                         int     nseg;
2153
2154                         cur_seg = (struct scatterlist *)cmd->request_buffer;
2155                         nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
2156                             cmd->sc_data_direction);
2157                         end_seg = cur_seg + nseg;
2158                         /* Copy the segments into the SG list. */
2159                         sg = scb->sg_list;
2160                         /*
2161                          * The sg_count may be larger than nseg if
2162                          * a transfer crosses a 32bit page.
2163                          */ 
2164                         while (cur_seg < end_seg) {
2165                                 dma_addr_t addr;
2166                                 bus_size_t len;
2167                                 int consumed;
2168
2169                                 addr = sg_dma_address(cur_seg);
2170                                 len = sg_dma_len(cur_seg);
2171                                 consumed = ahc_linux_map_seg(ahc, scb,
2172                                                              sg, addr, len);
2173                                 sg += consumed;
2174                                 scb->sg_count += consumed;
2175                                 cur_seg++;
2176                         }
2177                         sg--;
2178                         sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
2179
2180                         /*
2181                          * Reset the sg list pointer.
2182                          */
2183                         scb->hscb->sgptr =
2184                             ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
2185
2186                         /*
2187                          * Copy the first SG into the "current"
2188                          * data pointer area.
2189                          */
2190                         scb->hscb->dataptr = scb->sg_list->addr;
2191                         scb->hscb->datacnt = scb->sg_list->len;
2192                 } else if (cmd->request_bufflen != 0) {
2193                         struct   ahc_dma_seg *sg;
2194                         dma_addr_t addr;
2195
2196                         sg = scb->sg_list;
2197                         addr = pci_map_single(ahc->dev_softc,
2198                                cmd->request_buffer,
2199                                cmd->request_bufflen,
2200                                cmd->sc_data_direction);
2201                         scb->platform_data->buf_busaddr = addr;
2202                         scb->sg_count = ahc_linux_map_seg(ahc, scb,
2203                                                           sg, addr,
2204                                                           cmd->request_bufflen);
2205                         sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
2206
2207                         /*
2208                          * Reset the sg list pointer.
2209                          */
2210                         scb->hscb->sgptr =
2211                             ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
2212
2213                         /*
2214                          * Copy the first SG into the "current"
2215                          * data pointer area.
2216                          */
2217                         scb->hscb->dataptr = sg->addr;
2218                         scb->hscb->datacnt = sg->len;
2219                 } else {
2220                         scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
2221                         scb->hscb->dataptr = 0;
2222                         scb->hscb->datacnt = 0;
2223                         scb->sg_count = 0;
2224                 }
2225
2226                 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_PREWRITE);
2227                 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
2228                 dev->openings--;
2229                 dev->active++;
2230                 dev->commands_issued++;
2231                 if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
2232                         dev->commands_since_idle_or_otag++;
2233
2234                 /*
2235                  * We only allow one untagged transaction
2236                  * per target in the initiator role unless
2237                  * we are storing a full busy target *lun*
2238                  * table in SCB space.
2239                  */
2240                 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
2241                  && (ahc->features & AHC_SCB_BTT) == 0) {
2242                         struct scb_tailq *untagged_q;
2243                         int target_offset;
2244
2245                         target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
2246                         untagged_q = &(ahc->untagged_queues[target_offset]);
2247                         TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
2248                         scb->flags |= SCB_UNTAGGEDQ;
2249                         if (TAILQ_FIRST(untagged_q) != scb)
2250                                 continue;
2251                 }
2252                 scb->flags |= SCB_ACTIVE;
2253                 ahc_queue_scb(ahc, scb);
2254         }
2255 }
2256
2257 /*
2258  * SCSI controller interrupt handler.
2259  */
2260 irqreturn_t
2261 ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
2262 {
2263         struct  ahc_softc *ahc;
2264         u_long  flags;
2265         int     ours;
2266
2267         ahc = (struct ahc_softc *) dev_id;
2268         ahc_lock(ahc, &flags); 
2269         ours = ahc_intr(ahc);
2270         if (ahc_linux_next_device_to_run(ahc) != NULL)
2271                 ahc_schedule_runq(ahc);
2272         ahc_linux_run_complete_queue(ahc);
2273         ahc_unlock(ahc, &flags);
2274         return IRQ_RETVAL(ours);
2275 }
2276
2277 void
2278 ahc_platform_flushwork(struct ahc_softc *ahc)
2279 {
2280
2281         while (ahc_linux_run_complete_queue(ahc) != NULL)
2282                 ;
2283 }
2284
2285 static struct ahc_linux_target*
2286 ahc_linux_alloc_target(struct ahc_softc *ahc, u_int channel, u_int target)
2287 {
2288         struct ahc_linux_target *targ;
2289         u_int target_offset;
2290
2291         target_offset = target;
2292         if (channel != 0)
2293                 target_offset += 8;
2294
2295         targ = malloc(sizeof(*targ), M_DEVBUG, M_NOWAIT);
2296         if (targ == NULL)
2297                 return (NULL);
2298         memset(targ, 0, sizeof(*targ));
2299         targ->channel = channel;
2300         targ->target = target;
2301         targ->ahc = ahc;
2302         ahc->platform_data->targets[target_offset] = targ;
2303         return (targ);
2304 }
2305
2306 static void
2307 ahc_linux_free_target(struct ahc_softc *ahc, struct ahc_linux_target *targ)
2308 {
2309         struct ahc_devinfo devinfo;
2310         struct ahc_initiator_tinfo *tinfo;
2311         struct ahc_tmode_tstate *tstate;
2312         u_int our_id;
2313         u_int target_offset;
2314         char channel;
2315
2316         /*
2317          * Force a negotiation to async/narrow on any
2318          * future command to this device unless a bus
2319          * reset occurs between now and that command.
2320          */
2321         channel = 'A' + targ->channel;
2322         our_id = ahc->our_id;
2323         target_offset = targ->target;
2324         if (targ->channel != 0) {
2325                 target_offset += 8;
2326                 our_id = ahc->our_id_b;
2327         }
2328         tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
2329                                     targ->target, &tstate);
2330         ahc_compile_devinfo(&devinfo, our_id, targ->target, CAM_LUN_WILDCARD,
2331                             channel, ROLE_INITIATOR);
2332         ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
2333                          AHC_TRANS_GOAL, /*paused*/FALSE);
2334         ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
2335                       AHC_TRANS_GOAL, /*paused*/FALSE);
2336         ahc_update_neg_request(ahc, &devinfo, tstate, tinfo, AHC_NEG_ALWAYS);
2337         ahc->platform_data->targets[target_offset] = NULL;
2338         free(targ, M_DEVBUF);
2339 }
2340
2341 static struct ahc_linux_device*
2342 ahc_linux_alloc_device(struct ahc_softc *ahc,
2343                  struct ahc_linux_target *targ, u_int lun)
2344 {
2345         struct ahc_linux_device *dev;
2346
2347         dev = malloc(sizeof(*dev), M_DEVBUG, M_NOWAIT);
2348         if (dev == NULL)
2349                 return (NULL);
2350         memset(dev, 0, sizeof(*dev));
2351         init_timer(&dev->timer);
2352         TAILQ_INIT(&dev->busyq);
2353         dev->flags = AHC_DEV_UNCONFIGURED;
2354         dev->lun = lun;
2355         dev->target = targ;
2356
2357         /*
2358          * We start out life using untagged
2359          * transactions of which we allow one.
2360          */
2361         dev->openings = 1;
2362
2363         /*
2364          * Set maxtags to 0.  This will be changed if we
2365          * later determine that we are dealing with
2366          * a tagged queuing capable device.
2367          */
2368         dev->maxtags = 0;
2369         
2370         targ->refcount++;
2371         targ->devices[lun] = dev;
2372         return (dev);
2373 }
2374
2375 static void
2376 __ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
2377 {
2378         struct ahc_linux_target *targ;
2379
2380         targ = dev->target;
2381         targ->devices[dev->lun] = NULL;
2382         free(dev, M_DEVBUF);
2383         targ->refcount--;
2384         if (targ->refcount == 0)
2385                 ahc_linux_free_target(ahc, targ);
2386 }
2387
2388 static void
2389 ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
2390 {
2391         del_timer_sync(&dev->timer);
2392         __ahc_linux_free_device(ahc, dev);
2393 }
2394
2395 void
2396 ahc_send_async(struct ahc_softc *ahc, char channel,
2397                u_int target, u_int lun, ac_code code, void *arg)
2398 {
2399         switch (code) {
2400         case AC_TRANSFER_NEG:
2401         {
2402                 char    buf[80];
2403                 struct  ahc_linux_target *targ;
2404                 struct  info_str info;
2405                 struct  ahc_initiator_tinfo *tinfo;
2406                 struct  ahc_tmode_tstate *tstate;
2407                 int     target_offset;
2408
2409                 info.buffer = buf;
2410                 info.length = sizeof(buf);
2411                 info.offset = 0;
2412                 info.pos = 0;
2413                 tinfo = ahc_fetch_transinfo(ahc, channel,
2414                                                 channel == 'A' ? ahc->our_id
2415                                                                : ahc->our_id_b,
2416                                                 target, &tstate);
2417
2418                 /*
2419                  * Don't bother reporting results while
2420                  * negotiations are still pending.
2421                  */
2422                 if (tinfo->curr.period != tinfo->goal.period
2423                  || tinfo->curr.width != tinfo->goal.width
2424                  || tinfo->curr.offset != tinfo->goal.offset
2425                  || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
2426                         if (bootverbose == 0)
2427                                 break;
2428
2429                 /*
2430                  * Don't bother reporting results that
2431                  * are identical to those last reported.
2432                  */
2433                 target_offset = target;
2434                 if (channel == 'B')
2435                         target_offset += 8;
2436                 targ = ahc->platform_data->targets[target_offset];
2437                 if (targ == NULL)
2438                         break;
2439                 if (tinfo->curr.period == targ->last_tinfo.period
2440                  && tinfo->curr.width == targ->last_tinfo.width
2441                  && tinfo->curr.offset == targ->last_tinfo.offset
2442                  && tinfo->curr.ppr_options == targ->last_tinfo.ppr_options)
2443                         if (bootverbose == 0)
2444                                 break;
2445
2446                 targ->last_tinfo.period = tinfo->curr.period;
2447                 targ->last_tinfo.width = tinfo->curr.width;
2448                 targ->last_tinfo.offset = tinfo->curr.offset;
2449                 targ->last_tinfo.ppr_options = tinfo->curr.ppr_options;
2450
2451                 printf("(%s:%c:", ahc_name(ahc), channel);
2452                 if (target == CAM_TARGET_WILDCARD)
2453                         printf("*): ");
2454                 else
2455                         printf("%d): ", target);
2456                 ahc_format_transinfo(&info, &tinfo->curr);
2457                 if (info.pos < info.length)
2458                         *info.buffer = '\0';
2459                 else
2460                         buf[info.length - 1] = '\0';
2461                 printf("%s", buf);
2462                 break;
2463         }
2464         case AC_SENT_BDR:
2465         {
2466 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
2467                 WARN_ON(lun != CAM_LUN_WILDCARD);
2468                 scsi_report_device_reset(ahc->platform_data->host,
2469                                          channel - 'A', target);
2470 #else
2471                 Scsi_Device *scsi_dev;
2472
2473                 /*
2474                  * Find the SCSI device associated with this
2475                  * request and indicate that a UA is expected.
2476                  */
2477                 for (scsi_dev = ahc->platform_data->host->host_queue;
2478                      scsi_dev != NULL; scsi_dev = scsi_dev->next) {
2479                         if (channel - 'A' == scsi_dev->channel
2480                          && target == scsi_dev->id
2481                          && (lun == CAM_LUN_WILDCARD
2482                           || lun == scsi_dev->lun)) {
2483                                 scsi_dev->was_reset = 1;
2484                                 scsi_dev->expecting_cc_ua = 1;
2485                         }
2486                 }
2487 #endif
2488                 break;
2489         }
2490         case AC_BUS_RESET:
2491                 if (ahc->platform_data->host != NULL) {
2492                         scsi_report_bus_reset(ahc->platform_data->host,
2493                                               channel - 'A');
2494                 }
2495                 break;
2496         default:
2497                 panic("ahc_send_async: Unexpected async event");
2498         }
2499 }
2500
2501 /*
2502  * Calls the higher level scsi done function and frees the scb.
2503  */
2504 void
2505 ahc_done(struct ahc_softc *ahc, struct scb *scb)
2506 {
2507         Scsi_Cmnd *cmd;
2508         struct     ahc_linux_device *dev;
2509
2510         LIST_REMOVE(scb, pending_links);
2511         if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
2512                 struct scb_tailq *untagged_q;
2513                 int target_offset;
2514
2515                 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
2516                 untagged_q = &(ahc->untagged_queues[target_offset]);
2517                 TAILQ_REMOVE(untagged_q, scb, links.tqe);
2518                 ahc_run_untagged_queue(ahc, untagged_q);
2519         }
2520
2521         if ((scb->flags & SCB_ACTIVE) == 0) {
2522                 printf("SCB %d done'd twice\n", scb->hscb->tag);
2523                 ahc_dump_card_state(ahc);
2524                 panic("Stopping for safety");
2525         }
2526         cmd = scb->io_ctx;
2527         dev = scb->platform_data->dev;
2528         dev->active--;
2529         dev->openings++;
2530         if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
2531                 cmd->result &= ~(CAM_DEV_QFRZN << 16);
2532                 dev->qfrozen--;
2533         }
2534         ahc_linux_unmap_scb(ahc, scb);
2535
2536         /*
2537          * Guard against stale sense data.
2538          * The Linux mid-layer assumes that sense
2539          * was retrieved anytime the first byte of
2540          * the sense buffer looks "sane".
2541          */
2542         cmd->sense_buffer[0] = 0;
2543         if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
2544                 uint32_t amount_xferred;
2545
2546                 amount_xferred =
2547                     ahc_get_transfer_length(scb) - ahc_get_residual(scb);
2548                 if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
2549 #ifdef AHC_DEBUG
2550                         if ((ahc_debug & AHC_SHOW_MISC) != 0) {
2551                                 ahc_print_path(ahc, scb);
2552                                 printf("Set CAM_UNCOR_PARITY\n");
2553                         }
2554 #endif
2555                         ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
2556 #ifdef AHC_REPORT_UNDERFLOWS
2557                 /*
2558                  * This code is disabled by default as some
2559                  * clients of the SCSI system do not properly
2560                  * initialize the underflow parameter.  This
2561                  * results in spurious termination of commands
2562                  * that complete as expected (e.g. underflow is
2563                  * allowed as command can return variable amounts
2564                  * of data.
2565                  */
2566                 } else if (amount_xferred < scb->io_ctx->underflow) {
2567                         u_int i;
2568
2569                         ahc_print_path(ahc, scb);
2570                         printf("CDB:");
2571                         for (i = 0; i < scb->io_ctx->cmd_len; i++)
2572                                 printf(" 0x%x", scb->io_ctx->cmnd[i]);
2573                         printf("\n");
2574                         ahc_print_path(ahc, scb);
2575                         printf("Saw underflow (%ld of %ld bytes). "
2576                                "Treated as error\n",
2577                                 ahc_get_residual(scb),
2578                                 ahc_get_transfer_length(scb));
2579                         ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
2580 #endif
2581                 } else {
2582                         ahc_set_transaction_status(scb, CAM_REQ_CMP);
2583                 }
2584         } else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
2585                 ahc_linux_handle_scsi_status(ahc, dev, scb);
2586         } else if (ahc_get_transaction_status(scb) == CAM_SEL_TIMEOUT) {
2587                 dev->flags |= AHC_DEV_UNCONFIGURED;
2588         }
2589
2590         if (dev->openings == 1
2591          && ahc_get_transaction_status(scb) == CAM_REQ_CMP
2592          && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
2593                 dev->tag_success_count++;
2594         /*
2595          * Some devices deal with temporary internal resource
2596          * shortages by returning queue full.  When the queue
2597          * full occurrs, we throttle back.  Slowly try to get
2598          * back to our previous queue depth.
2599          */
2600         if ((dev->openings + dev->active) < dev->maxtags
2601          && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
2602                 dev->tag_success_count = 0;
2603                 dev->openings++;
2604         }
2605
2606         if (dev->active == 0)
2607                 dev->commands_since_idle_or_otag = 0;
2608
2609         if (TAILQ_EMPTY(&dev->busyq)) {
2610                 if ((dev->flags & AHC_DEV_UNCONFIGURED) != 0
2611                  && dev->active == 0
2612                  && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0)
2613                         ahc_linux_free_device(ahc, dev);
2614         } else if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) {
2615                 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links);
2616                 dev->flags |= AHC_DEV_ON_RUN_LIST;
2617         }
2618
2619         if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
2620                 printf("Recovery SCB completes\n");
2621                 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
2622                  || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
2623                         ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
2624                 if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
2625                         ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
2626                         up(&ahc->platform_data->eh_sem);
2627                 }
2628         }
2629
2630         ahc_free_scb(ahc, scb);
2631         ahc_linux_queue_cmd_complete(ahc, cmd);
2632 }
2633
2634 static void
2635 ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
2636                              struct ahc_linux_device *dev, struct scb *scb)
2637 {
2638         struct  ahc_devinfo devinfo;
2639
2640         ahc_compile_devinfo(&devinfo,
2641                             ahc->our_id,
2642                             dev->target->target, dev->lun,
2643                             dev->target->channel == 0 ? 'A' : 'B',
2644                             ROLE_INITIATOR);
2645         
2646         /*
2647          * We don't currently trust the mid-layer to
2648          * properly deal with queue full or busy.  So,
2649          * when one occurs, we tell the mid-layer to
2650          * unconditionally requeue the command to us
2651          * so that we can retry it ourselves.  We also
2652          * implement our own throttling mechanism so
2653          * we don't clobber the device with too many
2654          * commands.
2655          */
2656         switch (ahc_get_scsi_status(scb)) {
2657         default:
2658                 break;
2659         case SCSI_STATUS_CHECK_COND:
2660         case SCSI_STATUS_CMD_TERMINATED:
2661         {
2662                 Scsi_Cmnd *cmd;
2663
2664                 /*
2665                  * Copy sense information to the OS's cmd
2666                  * structure if it is available.
2667                  */
2668                 cmd = scb->io_ctx;
2669                 if (scb->flags & SCB_SENSE) {
2670                         u_int sense_size;
2671
2672                         sense_size = MIN(sizeof(struct scsi_sense_data)
2673                                        - ahc_get_sense_residual(scb),
2674                                          sizeof(cmd->sense_buffer));
2675                         memcpy(cmd->sense_buffer,
2676                                ahc_get_sense_buf(ahc, scb), sense_size);
2677                         if (sense_size < sizeof(cmd->sense_buffer))
2678                                 memset(&cmd->sense_buffer[sense_size], 0,
2679                                        sizeof(cmd->sense_buffer) - sense_size);
2680                         cmd->result |= (DRIVER_SENSE << 24);
2681 #ifdef AHC_DEBUG
2682                         if (ahc_debug & AHC_SHOW_SENSE) {
2683                                 int i;
2684
2685                                 printf("Copied %d bytes of sense data:",
2686                                        sense_size);
2687                                 for (i = 0; i < sense_size; i++) {
2688                                         if ((i & 0xF) == 0)
2689                                                 printf("\n");
2690                                         printf("0x%x ", cmd->sense_buffer[i]);
2691                                 }
2692                                 printf("\n");
2693                         }
2694 #endif
2695                 }
2696                 break;
2697         }
2698         case SCSI_STATUS_QUEUE_FULL:
2699         {
2700                 /*
2701                  * By the time the core driver has returned this
2702                  * command, all other commands that were queued
2703                  * to us but not the device have been returned.
2704                  * This ensures that dev->active is equal to
2705                  * the number of commands actually queued to
2706                  * the device.
2707                  */
2708                 dev->tag_success_count = 0;
2709                 if (dev->active != 0) {
2710                         /*
2711                          * Drop our opening count to the number
2712                          * of commands currently outstanding.
2713                          */
2714                         dev->openings = 0;
2715 /*
2716                         ahc_print_path(ahc, scb);
2717                         printf("Dropping tag count to %d\n", dev->active);
2718  */
2719                         if (dev->active == dev->tags_on_last_queuefull) {
2720
2721                                 dev->last_queuefull_same_count++;
2722                                 /*
2723                                  * If we repeatedly see a queue full
2724                                  * at the same queue depth, this
2725                                  * device has a fixed number of tag
2726                                  * slots.  Lock in this tag depth
2727                                  * so we stop seeing queue fulls from
2728                                  * this device.
2729                                  */
2730                                 if (dev->last_queuefull_same_count
2731                                  == AHC_LOCK_TAGS_COUNT) {
2732                                         dev->maxtags = dev->active;
2733                                         ahc_print_path(ahc, scb);
2734                                         printf("Locking max tag count at %d\n",
2735                                                dev->active);
2736                                 }
2737                         } else {
2738                                 dev->tags_on_last_queuefull = dev->active;
2739                                 dev->last_queuefull_same_count = 0;
2740                         }
2741                         ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
2742                         ahc_set_scsi_status(scb, SCSI_STATUS_OK);
2743                         ahc_platform_set_tags(ahc, &devinfo,
2744                                      (dev->flags & AHC_DEV_Q_BASIC)
2745                                    ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
2746                         break;
2747                 }
2748                 /*
2749                  * Drop down to a single opening, and treat this
2750                  * as if the target returned BUSY SCSI status.
2751                  */
2752                 dev->openings = 1;
2753                 ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
2754                 ahc_platform_set_tags(ahc, &devinfo,
2755                              (dev->flags & AHC_DEV_Q_BASIC)
2756                            ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
2757                 /* FALLTHROUGH */
2758         }
2759         case SCSI_STATUS_BUSY:
2760         {
2761                 /*
2762                  * Set a short timer to defer sending commands for
2763                  * a bit since Linux will not delay in this case.
2764                  */
2765                 if ((dev->flags & AHC_DEV_TIMER_ACTIVE) != 0) {
2766                         printf("%s:%c:%d: Device Timer still active during "
2767                                "busy processing\n", ahc_name(ahc),
2768                                 dev->target->channel, dev->target->target);
2769                         break;
2770                 }
2771                 dev->flags |= AHC_DEV_TIMER_ACTIVE;
2772                 dev->qfrozen++;
2773                 init_timer(&dev->timer);
2774                 dev->timer.data = (u_long)dev;
2775                 dev->timer.expires = jiffies + (HZ/2);
2776                 dev->timer.function = ahc_linux_dev_timed_unfreeze;
2777                 add_timer(&dev->timer);
2778                 break;
2779         }
2780         }
2781 }
2782
2783 static void
2784 ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, Scsi_Cmnd *cmd)
2785 {
2786         /*
2787          * Typically, the complete queue has very few entries
2788          * queued to it before the queue is emptied by
2789          * ahc_linux_run_complete_queue, so sorting the entries
2790          * by generation number should be inexpensive.
2791          * We perform the sort so that commands that complete
2792          * with an error are retuned in the order origionally
2793          * queued to the controller so that any subsequent retries
2794          * are performed in order.  The underlying ahc routines do
2795          * not guarantee the order that aborted commands will be
2796          * returned to us.
2797          */
2798         struct ahc_completeq *completeq;
2799         struct ahc_cmd *list_cmd;
2800         struct ahc_cmd *acmd;
2801
2802         /*
2803          * Map CAM error codes into Linux Error codes.  We
2804          * avoid the conversion so that the DV code has the
2805          * full error information available when making
2806          * state change decisions.
2807          */
2808         {
2809                 u_int new_status;
2810
2811                 switch (ahc_cmd_get_transaction_status(cmd)) {
2812                 case CAM_REQ_INPROG:
2813                 case CAM_REQ_CMP:
2814                 case CAM_SCSI_STATUS_ERROR:
2815                         new_status = DID_OK;
2816                         break;
2817                 case CAM_REQ_ABORTED:
2818                         new_status = DID_ABORT;
2819                         break;
2820                 case CAM_BUSY:
2821                         new_status = DID_BUS_BUSY;
2822                         break;
2823                 case CAM_REQ_INVALID:
2824                 case CAM_PATH_INVALID:
2825                         new_status = DID_BAD_TARGET;
2826                         break;
2827                 case CAM_SEL_TIMEOUT:
2828                         new_status = DID_NO_CONNECT;
2829                         break;
2830                 case CAM_SCSI_BUS_RESET:
2831                 case CAM_BDR_SENT:
2832                         new_status = DID_RESET;
2833                         break;
2834                 case CAM_UNCOR_PARITY:
2835                         new_status = DID_PARITY;
2836                         break;
2837                 case CAM_CMD_TIMEOUT:
2838                         new_status = DID_TIME_OUT;
2839                         break;
2840                 case CAM_UA_ABORT:
2841                 case CAM_REQ_CMP_ERR:
2842                 case CAM_AUTOSENSE_FAIL:
2843                 case CAM_NO_HBA:
2844                 case CAM_DATA_RUN_ERR:
2845                 case CAM_UNEXP_BUSFREE:
2846                 case CAM_SEQUENCE_FAIL:
2847                 case CAM_CCB_LEN_ERR:
2848                 case CAM_PROVIDE_FAIL:
2849                 case CAM_REQ_TERMIO:
2850                 case CAM_UNREC_HBA_ERROR:
2851                 case CAM_REQ_TOO_BIG:
2852                         new_status = DID_ERROR;
2853                         break;
2854                 case CAM_REQUEUE_REQ:
2855                         /*
2856                          * If we want the request requeued, make sure there
2857                          * are sufficent retries.  In the old scsi error code,
2858                          * we used to be able to specify a result code that
2859                          * bypassed the retry count.  Now we must use this
2860                          * hack.  We also "fake" a check condition with
2861                          * a sense code of ABORTED COMMAND.  This seems to
2862                          * evoke a retry even if this command is being sent
2863                          * via the eh thread.  Ick!  Ick!  Ick!
2864                          */
2865                         if (cmd->retries > 0)
2866                                 cmd->retries--;
2867                         new_status = DID_OK;
2868                         ahc_cmd_set_scsi_status(cmd, SCSI_STATUS_CHECK_COND);
2869                         cmd->result |= (DRIVER_SENSE << 24);
2870                         memset(cmd->sense_buffer, 0,
2871                                sizeof(cmd->sense_buffer));
2872                         cmd->sense_buffer[0] = SSD_ERRCODE_VALID
2873                                              | SSD_CURRENT_ERROR;
2874                         cmd->sense_buffer[2] = SSD_KEY_ABORTED_COMMAND;
2875                         break;
2876                 default:
2877                         /* We should never get here */
2878                         new_status = DID_ERROR;
2879                         break;
2880                 }
2881
2882                 ahc_cmd_set_transaction_status(cmd, new_status);
2883         }
2884
2885         completeq = &ahc->platform_data->completeq;
2886         list_cmd = TAILQ_FIRST(completeq);
2887         acmd = (struct ahc_cmd *)cmd;
2888         while (list_cmd != NULL
2889             && acmd_scsi_cmd(list_cmd).serial_number
2890              < acmd_scsi_cmd(acmd).serial_number)
2891                 list_cmd = TAILQ_NEXT(list_cmd, acmd_links.tqe);
2892         if (list_cmd != NULL)
2893                 TAILQ_INSERT_BEFORE(list_cmd, acmd, acmd_links.tqe);
2894         else
2895                 TAILQ_INSERT_TAIL(completeq, acmd, acmd_links.tqe);
2896 }
2897
2898 static void
2899 ahc_linux_sem_timeout(u_long arg)
2900 {
2901         struct  ahc_softc *ahc;
2902         u_long  s;
2903
2904         ahc = (struct ahc_softc *)arg;
2905
2906         ahc_lock(ahc, &s);
2907         if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
2908                 ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
2909                 up(&ahc->platform_data->eh_sem);
2910         }
2911         ahc_unlock(ahc, &s);
2912 }
2913
2914 static void
2915 ahc_linux_freeze_simq(struct ahc_softc *ahc)
2916 {
2917         ahc->platform_data->qfrozen++;
2918         if (ahc->platform_data->qfrozen == 1) {
2919                 scsi_block_requests(ahc->platform_data->host);
2920
2921                 /* XXX What about Twin channels? */
2922                 ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
2923                                         CAM_LUN_WILDCARD, SCB_LIST_NULL,
2924                                         ROLE_INITIATOR, CAM_REQUEUE_REQ);
2925         }
2926 }
2927
2928 static void
2929 ahc_linux_release_simq(u_long arg)
2930 {
2931         struct ahc_softc *ahc;
2932         u_long s;
2933         int    unblock_reqs;
2934
2935         ahc = (struct ahc_softc *)arg;
2936
2937         unblock_reqs = 0;
2938         ahc_lock(ahc, &s);
2939         if (ahc->platform_data->qfrozen > 0)
2940                 ahc->platform_data->qfrozen--;
2941         if (ahc->platform_data->qfrozen == 0)
2942                 unblock_reqs = 1;
2943         ahc_schedule_runq(ahc);
2944         ahc_unlock(ahc, &s);
2945         /*
2946          * There is still a race here.  The mid-layer
2947          * should keep its own freeze count and use
2948          * a bottom half handler to run the queues
2949          * so we can unblock with our own lock held.
2950          */
2951         if (unblock_reqs)
2952                 scsi_unblock_requests(ahc->platform_data->host);
2953 }
2954
2955 static void
2956 ahc_linux_dev_timed_unfreeze(u_long arg)
2957 {
2958         struct ahc_linux_device *dev;
2959         struct ahc_softc *ahc;
2960         u_long s;
2961
2962         dev = (struct ahc_linux_device *)arg;
2963         ahc = dev->target->ahc;
2964         ahc_lock(ahc, &s);
2965         dev->flags &= ~AHC_DEV_TIMER_ACTIVE;
2966         if (dev->qfrozen > 0)
2967                 dev->qfrozen--;
2968         if (dev->qfrozen == 0
2969          && (dev->flags & AHC_DEV_ON_RUN_LIST) == 0)
2970                 ahc_linux_run_device_queue(ahc, dev);
2971         if (TAILQ_EMPTY(&dev->busyq)
2972          && dev->active == 0)
2973                 __ahc_linux_free_device(ahc, dev);
2974         ahc_unlock(ahc, &s);
2975 }
2976
2977 static int
2978 ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag)
2979 {
2980         struct ahc_softc *ahc;
2981         struct ahc_cmd *acmd;
2982         struct ahc_cmd *list_acmd;
2983         struct ahc_linux_device *dev;
2984         struct scb *pending_scb;
2985         u_long s;
2986         u_int  saved_scbptr;
2987         u_int  active_scb_index;
2988         u_int  last_phase;
2989         u_int  saved_scsiid;
2990         u_int  cdb_byte;
2991         int    retval;
2992         int    was_paused;
2993         int    paused;
2994         int    wait;
2995         int    disconnected;
2996
2997         pending_scb = NULL;
2998         paused = FALSE;
2999         wait = FALSE;
3000         ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
3001         acmd = (struct ahc_cmd *)cmd;
3002
3003         printf("%s:%d:%d:%d: Attempting to queue a%s message\n",
3004                ahc_name(ahc), cmd->device->channel,
3005                cmd->device->id, cmd->device->lun,
3006                flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
3007
3008         printf("CDB:");
3009         for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
3010                 printf(" 0x%x", cmd->cmnd[cdb_byte]);
3011         printf("\n");
3012
3013         /*
3014          * In all versions of Linux, we have to work around
3015          * a major flaw in how the mid-layer is locked down
3016          * if we are to sleep successfully in our error handler
3017          * while allowing our interrupt handler to run.  Since
3018          * the midlayer acquires either the io_request_lock or
3019          * our lock prior to calling us, we must use the
3020          * spin_unlock_irq() method for unlocking our lock.
3021          * This will force interrupts to be enabled on the
3022          * current CPU.  Since the EH thread should not have
3023          * been running with CPU interrupts disabled other than
3024          * by acquiring either the io_request_lock or our own
3025          * lock, this *should* be safe.
3026          */
3027         ahc_midlayer_entrypoint_lock(ahc, &s);
3028
3029         /*
3030          * First determine if we currently own this command.
3031          * Start by searching the device queue.  If not found
3032          * there, check the pending_scb list.  If not found
3033          * at all, and the system wanted us to just abort the
3034          * command, return success.
3035          */
3036         dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
3037                                    cmd->device->lun, /*alloc*/FALSE);
3038
3039         if (dev == NULL) {
3040                 /*
3041                  * No target device for this command exists,
3042                  * so we must not still own the command.
3043                  */
3044                 printf("%s:%d:%d:%d: Is not an active device\n",
3045                        ahc_name(ahc), cmd->device->channel, cmd->device->id,
3046                        cmd->device->lun);
3047                 retval = SUCCESS;
3048                 goto no_cmd;
3049         }
3050
3051         TAILQ_FOREACH(list_acmd, &dev->busyq, acmd_links.tqe) {
3052                 if (list_acmd == acmd)
3053                         break;
3054         }
3055
3056         if (list_acmd != NULL) {
3057                 printf("%s:%d:%d:%d: Command found on device queue\n",
3058                        ahc_name(ahc), cmd->device->channel, cmd->device->id,
3059                        cmd->device->lun);
3060                 if (flag == SCB_ABORT) {
3061                         TAILQ_REMOVE(&dev->busyq, list_acmd, acmd_links.tqe);
3062                         cmd->result = DID_ABORT << 16;
3063                         ahc_linux_queue_cmd_complete(ahc, cmd);
3064                         retval = SUCCESS;
3065                         goto done;
3066                 }
3067         }
3068
3069         if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
3070          && ahc_search_untagged_queues(ahc, cmd, cmd->device->id,
3071                                        cmd->device->channel + 'A',
3072                                        cmd->device->lun,
3073                                        CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
3074                 printf("%s:%d:%d:%d: Command found on untagged queue\n",
3075                        ahc_name(ahc), cmd->device->channel, cmd->device->id,
3076                        cmd->device->lun);
3077                 retval = SUCCESS;
3078                 goto done;
3079         }
3080
3081         /*
3082          * See if we can find a matching cmd in the pending list.
3083          */
3084         LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
3085                 if (pending_scb->io_ctx == cmd)
3086                         break;
3087         }
3088
3089         if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
3090
3091                 /* Any SCB for this device will do for a target reset */
3092                 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
3093                         if (ahc_match_scb(ahc, pending_scb, cmd->device->id,
3094                                           cmd->device->channel + 'A',
3095                                           CAM_LUN_WILDCARD,
3096                                           SCB_LIST_NULL, ROLE_INITIATOR) == 0)
3097                                 break;
3098                 }
3099         }
3100
3101         if (pending_scb == NULL) {
3102                 printf("%s:%d:%d:%d: Command not found\n",
3103                        ahc_name(ahc), cmd->device->channel, cmd->device->id,
3104                        cmd->device->lun);
3105                 goto no_cmd;
3106         }
3107
3108         if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
3109                 /*
3110                  * We can't queue two recovery actions using the same SCB
3111                  */
3112                 retval = FAILED;
3113                 goto  done;
3114         }
3115
3116         /*
3117          * Ensure that the card doesn't do anything
3118          * behind our back and that we didn't "just" miss
3119          * an interrupt that would affect this cmd.
3120          */
3121         was_paused = ahc_is_paused(ahc);
3122         ahc_pause_and_flushwork(ahc);
3123         paused = TRUE;
3124
3125         if ((pending_scb->flags & SCB_ACTIVE) == 0) {
3126                 printf("%s:%d:%d:%d: Command already completed\n",
3127                        ahc_name(ahc), cmd->device->channel, cmd->device->id,
3128                        cmd->device->lun);
3129                 goto no_cmd;
3130         }
3131
3132         printf("%s: At time of recovery, card was %spaused\n",
3133                ahc_name(ahc), was_paused ? "" : "not ");
3134         ahc_dump_card_state(ahc);
3135
3136         disconnected = TRUE;
3137         if (flag == SCB_ABORT) {
3138                 if (ahc_search_qinfifo(ahc, cmd->device->id,
3139                                        cmd->device->channel + 'A',
3140                                        cmd->device->lun,
3141                                        pending_scb->hscb->tag,
3142                                        ROLE_INITIATOR, CAM_REQ_ABORTED,
3143                                        SEARCH_COMPLETE) > 0) {
3144                         printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
3145                                ahc_name(ahc), cmd->device->channel,
3146                                         cmd->device->id, cmd->device->lun);
3147                         retval = SUCCESS;
3148                         goto done;
3149                 }
3150         } else if (ahc_search_qinfifo(ahc, cmd->device->id,
3151                                       cmd->device->channel + 'A',
3152                                       cmd->device->lun, pending_scb->hscb->tag,
3153                                       ROLE_INITIATOR, /*status*/0,
3154                                       SEARCH_COUNT) > 0) {
3155                 disconnected = FALSE;
3156         }
3157
3158         if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
3159                 struct scb *bus_scb;
3160
3161                 bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
3162                 if (bus_scb == pending_scb)
3163                         disconnected = FALSE;
3164                 else if (flag != SCB_ABORT
3165                       && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
3166                       && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb))
3167                         disconnected = FALSE;
3168         }
3169
3170         /*
3171          * At this point, pending_scb is the scb associated with the
3172          * passed in command.  That command is currently active on the
3173          * bus, is in the disconnected state, or we're hoping to find
3174          * a command for the same target active on the bus to abuse to
3175          * send a BDR.  Queue the appropriate message based on which of
3176          * these states we are in.
3177          */
3178         last_phase = ahc_inb(ahc, LASTPHASE);
3179         saved_scbptr = ahc_inb(ahc, SCBPTR);
3180         active_scb_index = ahc_inb(ahc, SCB_TAG);
3181         saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
3182         if (last_phase != P_BUSFREE
3183          && (pending_scb->hscb->tag == active_scb_index
3184           || (flag == SCB_DEVICE_RESET
3185            && SCSIID_TARGET(ahc, saved_scsiid) == cmd->device->id))) {
3186
3187                 /*
3188                  * We're active on the bus, so assert ATN
3189                  * and hope that the target responds.
3190                  */
3191                 pending_scb = ahc_lookup_scb(ahc, active_scb_index);
3192                 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
3193                 ahc_outb(ahc, MSG_OUT, HOST_MSG);
3194                 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
3195                 printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
3196                        ahc_name(ahc), cmd->device->channel, cmd->device->id,
3197                        cmd->device->lun);
3198                 wait = TRUE;
3199         } else if (disconnected) {
3200
3201                 /*
3202                  * Actually re-queue this SCB in an attempt
3203                  * to select the device before it reconnects.
3204                  * In either case (selection or reselection),
3205                  * we will now issue the approprate message
3206                  * to the timed-out device.
3207                  *
3208                  * Set the MK_MESSAGE control bit indicating
3209                  * that we desire to send a message.  We
3210                  * also set the disconnected flag since
3211                  * in the paging case there is no guarantee
3212                  * that our SCB control byte matches the
3213                  * version on the card.  We don't want the
3214                  * sequencer to abort the command thinking
3215                  * an unsolicited reselection occurred.
3216                  */
3217                 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
3218                 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
3219
3220                 /*
3221                  * Remove any cached copy of this SCB in the
3222                  * disconnected list in preparation for the
3223                  * queuing of our abort SCB.  We use the
3224                  * same element in the SCB, SCB_NEXT, for
3225                  * both the qinfifo and the disconnected list.
3226                  */
3227                 ahc_search_disc_list(ahc, cmd->device->id,
3228                                      cmd->device->channel + 'A',
3229                                      cmd->device->lun, pending_scb->hscb->tag,
3230                                      /*stop_on_first*/TRUE,
3231                                      /*remove*/TRUE,
3232                                      /*save_state*/FALSE);
3233
3234                 /*
3235                  * In the non-paging case, the sequencer will
3236                  * never re-reference the in-core SCB.
3237                  * To make sure we are notified during
3238                  * reslection, set the MK_MESSAGE flag in
3239                  * the card's copy of the SCB.
3240                  */
3241                 if ((ahc->flags & AHC_PAGESCBS) == 0) {
3242                         ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
3243                         ahc_outb(ahc, SCB_CONTROL,
3244                                  ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
3245                 }
3246
3247                 /*
3248                  * Clear out any entries in the QINFIFO first
3249                  * so we are the next SCB for this target
3250                  * to run.
3251                  */
3252                 ahc_search_qinfifo(ahc, cmd->device->id,
3253                                    cmd->device->channel + 'A',
3254                                    cmd->device->lun, SCB_LIST_NULL,
3255                                    ROLE_INITIATOR, CAM_REQUEUE_REQ,
3256                                    SEARCH_COMPLETE);
3257                 ahc_qinfifo_requeue_tail(ahc, pending_scb);
3258                 ahc_outb(ahc, SCBPTR, saved_scbptr);
3259                 ahc_print_path(ahc, pending_scb);
3260                 printf("Device is disconnected, re-queuing SCB\n");
3261                 wait = TRUE;
3262         } else {
3263                 printf("%s:%d:%d:%d: Unable to deliver message\n",
3264                        ahc_name(ahc), cmd->device->channel, cmd->device->id,
3265                        cmd->device->lun);
3266                 retval = FAILED;
3267                 goto done;
3268         }
3269
3270 no_cmd:
3271         /*
3272          * Our assumption is that if we don't have the command, no
3273          * recovery action was required, so we return success.  Again,
3274          * the semantics of the mid-layer recovery engine are not
3275          * well defined, so this may change in time.
3276          */
3277         retval = SUCCESS;
3278 done:
3279         if (paused)
3280                 ahc_unpause(ahc);
3281         if (wait) {
3282                 struct timer_list timer;
3283                 int ret;
3284
3285                 ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE;
3286                 spin_unlock_irq(&ahc->platform_data->spin_lock);
3287                 init_timer(&timer);
3288                 timer.data = (u_long)ahc;
3289                 timer.expires = jiffies + (5 * HZ);
3290                 timer.function = ahc_linux_sem_timeout;
3291                 add_timer(&timer);
3292                 printf("Recovery code sleeping\n");
3293                 down(&ahc->platform_data->eh_sem);
3294                 printf("Recovery code awake\n");
3295                 ret = del_timer_sync(&timer);
3296                 if (ret == 0) {
3297                         printf("Timer Expired\n");
3298                         retval = FAILED;
3299                 }
3300                 spin_lock_irq(&ahc->platform_data->spin_lock);
3301         }
3302         ahc_schedule_runq(ahc);
3303         ahc_linux_run_complete_queue(ahc);
3304         ahc_midlayer_entrypoint_unlock(ahc, &s);
3305         return (retval);
3306 }
3307
3308 void
3309 ahc_platform_dump_card_state(struct ahc_softc *ahc)
3310 {
3311         struct ahc_linux_device *dev;
3312         int channel;
3313         int maxchannel;
3314         int target;
3315         int maxtarget;
3316         int lun;
3317         int i;
3318
3319         maxchannel = (ahc->features & AHC_TWIN) ? 1 : 0;
3320         maxtarget = (ahc->features & AHC_WIDE) ? 15 : 7;
3321         for (channel = 0; channel <= maxchannel; channel++) {
3322
3323                 for (target = 0; target <=maxtarget; target++) {
3324
3325                         for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
3326                                 struct ahc_cmd *acmd;
3327
3328                                 dev = ahc_linux_get_device(ahc, channel, target,
3329                                                            lun, /*alloc*/FALSE);
3330                                 if (dev == NULL)
3331                                         continue;
3332
3333                                 printf("DevQ(%d:%d:%d): ",
3334                                        channel, target, lun);
3335                                 i = 0;
3336                                 TAILQ_FOREACH(acmd, &dev->busyq,
3337                                               acmd_links.tqe) {
3338                                         if (i++ > AHC_SCB_MAX)
3339                                                 break;
3340                                 }
3341                                 printf("%d waiting\n", i);
3342                         }
3343                 }
3344         }
3345 }
3346
3347 static void ahc_linux_exit(void);
3348
3349 static void ahc_linux_get_period(struct scsi_target *starget)
3350 {
3351         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3352         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3353         struct ahc_tmode_tstate *tstate;
3354         struct ahc_initiator_tinfo *tinfo 
3355                 = ahc_fetch_transinfo(ahc,
3356                                       starget->channel + 'A',
3357                                       shost->this_id, starget->id, &tstate);
3358         spi_period(starget) = tinfo->curr.period;
3359 }
3360
3361 static void ahc_linux_set_period(struct scsi_target *starget, int period)
3362 {
3363         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3364         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3365         struct ahc_tmode_tstate *tstate;
3366         struct ahc_initiator_tinfo *tinfo 
3367                 = ahc_fetch_transinfo(ahc,
3368                                       starget->channel + 'A',
3369                                       shost->this_id, starget->id, &tstate);
3370         struct ahc_devinfo devinfo;
3371         unsigned int ppr_options = tinfo->curr.ppr_options;
3372         unsigned long flags;
3373         unsigned long offset = tinfo->curr.offset;
3374         struct ahc_syncrate *syncrate;
3375
3376         if (offset == 0)
3377                 offset = MAX_OFFSET;
3378
3379         ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
3380                             starget->channel + 'A', ROLE_INITIATOR);
3381         syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
3382         ahc_lock(ahc, &flags);
3383         ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
3384                          ppr_options, AHC_TRANS_GOAL, FALSE);
3385         ahc_unlock(ahc, &flags);
3386 }
3387
3388 static void ahc_linux_get_offset(struct scsi_target *starget)
3389 {
3390         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3391         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3392         struct ahc_tmode_tstate *tstate;
3393         struct ahc_initiator_tinfo *tinfo 
3394                 = ahc_fetch_transinfo(ahc,
3395                                       starget->channel + 'A',
3396                                       shost->this_id, starget->id, &tstate);
3397         spi_offset(starget) = tinfo->curr.offset;
3398 }
3399
3400 static void ahc_linux_set_offset(struct scsi_target *starget, int offset)
3401 {
3402         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3403         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3404         struct ahc_tmode_tstate *tstate;
3405         struct ahc_initiator_tinfo *tinfo 
3406                 = ahc_fetch_transinfo(ahc,
3407                                       starget->channel + 'A',
3408                                       shost->this_id, starget->id, &tstate);
3409         struct ahc_devinfo devinfo;
3410         unsigned int ppr_options = 0;
3411         unsigned int period = 0;
3412         unsigned long flags;
3413         struct ahc_syncrate *syncrate = NULL;
3414
3415         ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
3416                             starget->channel + 'A', ROLE_INITIATOR);
3417         if (offset != 0) {
3418                 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
3419                 period = tinfo->curr.period;
3420                 ppr_options = tinfo->curr.ppr_options;
3421         }
3422         ahc_lock(ahc, &flags);
3423         ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
3424                          ppr_options, AHC_TRANS_GOAL, FALSE);
3425         ahc_unlock(ahc, &flags);
3426 }
3427
3428 static void ahc_linux_get_width(struct scsi_target *starget)
3429 {
3430         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3431         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3432         struct ahc_tmode_tstate *tstate;
3433         struct ahc_initiator_tinfo *tinfo 
3434                 = ahc_fetch_transinfo(ahc,
3435                                       starget->channel + 'A',
3436                                       shost->this_id, starget->id, &tstate);
3437         spi_width(starget) = tinfo->curr.width;
3438 }
3439
3440 static void ahc_linux_set_width(struct scsi_target *starget, int width)
3441 {
3442         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3443         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3444         struct ahc_devinfo devinfo;
3445         unsigned long flags;
3446
3447         ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
3448                             starget->channel + 'A', ROLE_INITIATOR);
3449         ahc_lock(ahc, &flags);
3450         ahc_set_width(ahc, &devinfo, width, AHC_TRANS_GOAL, FALSE);
3451         ahc_unlock(ahc, &flags);
3452 }
3453
3454 static void ahc_linux_get_dt(struct scsi_target *starget)
3455 {
3456         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3457         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3458         struct ahc_tmode_tstate *tstate;
3459         struct ahc_initiator_tinfo *tinfo 
3460                 = ahc_fetch_transinfo(ahc,
3461                                       starget->channel + 'A',
3462                                       shost->this_id, starget->id, &tstate);
3463         spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ;
3464 }
3465
3466 static void ahc_linux_set_dt(struct scsi_target *starget, int dt)
3467 {
3468         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3469         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3470         struct ahc_tmode_tstate *tstate;
3471         struct ahc_initiator_tinfo *tinfo 
3472                 = ahc_fetch_transinfo(ahc,
3473                                       starget->channel + 'A',
3474                                       shost->this_id, starget->id, &tstate);
3475         struct ahc_devinfo devinfo;
3476         unsigned int ppr_options = tinfo->curr.ppr_options
3477                 & ~MSG_EXT_PPR_DT_REQ;
3478         unsigned int period = tinfo->curr.period;
3479         unsigned long flags;
3480         struct ahc_syncrate *syncrate;
3481
3482         ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
3483                             starget->channel + 'A', ROLE_INITIATOR);
3484         syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,
3485                                      dt ? AHC_SYNCRATE_DT : AHC_SYNCRATE_ULTRA2);
3486         ahc_lock(ahc, &flags);
3487         ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->curr.offset,
3488                          ppr_options, AHC_TRANS_GOAL, FALSE);
3489         ahc_unlock(ahc, &flags);
3490 }
3491
3492 static void ahc_linux_get_qas(struct scsi_target *starget)
3493 {
3494         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3495         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3496         struct ahc_tmode_tstate *tstate;
3497         struct ahc_initiator_tinfo *tinfo 
3498                 = ahc_fetch_transinfo(ahc,
3499                                       starget->channel + 'A',
3500                                       shost->this_id, starget->id, &tstate);
3501         spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ;
3502 }
3503
3504 static void ahc_linux_set_qas(struct scsi_target *starget, int qas)
3505 {
3506         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3507         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3508         struct ahc_tmode_tstate *tstate;
3509         struct ahc_initiator_tinfo *tinfo 
3510                 = ahc_fetch_transinfo(ahc,
3511                                       starget->channel + 'A',
3512                                       shost->this_id, starget->id, &tstate);
3513         struct ahc_devinfo devinfo;
3514         unsigned int ppr_options = tinfo->curr.ppr_options
3515                 & ~MSG_EXT_PPR_QAS_REQ;
3516         unsigned int period = tinfo->curr.period;
3517         unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
3518         unsigned long flags;
3519         struct ahc_syncrate *syncrate;
3520
3521         if (qas)
3522                 ppr_options |= MSG_EXT_PPR_QAS_REQ;
3523
3524         ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
3525                             starget->channel + 'A', ROLE_INITIATOR);
3526         syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,
3527                                      dt ? AHC_SYNCRATE_DT : AHC_SYNCRATE_ULTRA2);
3528         ahc_lock(ahc, &flags);
3529         ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->curr.offset,
3530                          ppr_options, AHC_TRANS_GOAL, FALSE);
3531         ahc_unlock(ahc, &flags);
3532 }
3533
3534 static void ahc_linux_get_iu(struct scsi_target *starget)
3535 {
3536         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3537         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3538         struct ahc_tmode_tstate *tstate;
3539         struct ahc_initiator_tinfo *tinfo 
3540                 = ahc_fetch_transinfo(ahc,
3541                                       starget->channel + 'A',
3542                                       shost->this_id, starget->id, &tstate);
3543         spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ;
3544 }
3545
3546 static void ahc_linux_set_iu(struct scsi_target *starget, int iu)
3547 {
3548         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
3549         struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
3550         struct ahc_tmode_tstate *tstate;
3551         struct ahc_initiator_tinfo *tinfo 
3552                 = ahc_fetch_transinfo(ahc,
3553                                       starget->channel + 'A',
3554                                       shost->this_id, starget->id, &tstate);
3555         struct ahc_devinfo devinfo;
3556         unsigned int ppr_options = tinfo->curr.ppr_options
3557                 & ~MSG_EXT_PPR_IU_REQ;
3558         unsigned int period = tinfo->curr.period;
3559         unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
3560         unsigned long flags;
3561         struct ahc_syncrate *syncrate;
3562
3563         if (iu)
3564                 ppr_options |= MSG_EXT_PPR_IU_REQ;
3565
3566         ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
3567                             starget->channel + 'A', ROLE_INITIATOR);
3568         syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,
3569                                      dt ? AHC_SYNCRATE_DT : AHC_SYNCRATE_ULTRA2);
3570         ahc_lock(ahc, &flags);
3571         ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->curr.offset,
3572                          ppr_options, AHC_TRANS_GOAL, FALSE);
3573         ahc_unlock(ahc, &flags);
3574 }
3575
3576 static struct spi_function_template ahc_linux_transport_functions = {
3577         .get_offset     = ahc_linux_get_offset,
3578         .set_offset     = ahc_linux_set_offset,
3579         .show_offset    = 1,
3580         .get_period     = ahc_linux_get_period,
3581         .set_period     = ahc_linux_set_period,
3582         .show_period    = 1,
3583         .get_width      = ahc_linux_get_width,
3584         .set_width      = ahc_linux_set_width,
3585         .show_width     = 1,
3586         .get_dt         = ahc_linux_get_dt,
3587         .set_dt         = ahc_linux_set_dt,
3588         .show_dt        = 1,
3589         .get_iu         = ahc_linux_get_iu,
3590         .set_iu         = ahc_linux_set_iu,
3591         .show_iu        = 1,
3592         .get_qas        = ahc_linux_get_qas,
3593         .set_qas        = ahc_linux_set_qas,
3594         .show_qas       = 1,
3595 };
3596
3597
3598
3599 static int __init
3600 ahc_linux_init(void)
3601 {
3602 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3603         ahc_linux_transport_template = spi_attach_transport(&ahc_linux_transport_functions);
3604         if (!ahc_linux_transport_template)
3605                 return -ENODEV;
3606         if (ahc_linux_detect(&aic7xxx_driver_template))
3607                 return 0;
3608         spi_release_transport(ahc_linux_transport_template);
3609         ahc_linux_exit();
3610         return -ENODEV;
3611 #else
3612         scsi_register_module(MODULE_SCSI_HA, &aic7xxx_driver_template);
3613         if (aic7xxx_driver_template.present == 0) {
3614                 scsi_unregister_module(MODULE_SCSI_HA,
3615                                        &aic7xxx_driver_template);
3616                 return (-ENODEV);
3617         }
3618
3619         return (0);
3620 #endif
3621 }
3622
3623 static void
3624 ahc_linux_exit(void)
3625 {
3626 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
3627         /*
3628          * In 2.4 we have to unregister from the PCI core _after_
3629          * unregistering from the scsi midlayer to avoid dangling
3630          * references.
3631          */
3632         scsi_unregister_module(MODULE_SCSI_HA, &aic7xxx_driver_template);
3633 #endif
3634         ahc_linux_pci_exit();
3635         ahc_linux_eisa_exit();
3636         spi_release_transport(ahc_linux_transport_template);
3637 }
3638
3639 module_init(ahc_linux_init);
3640 module_exit(ahc_linux_exit);