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