2 * Adaptec AIC7xxx device driver for Linux.
4 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.c#235 $
6 * Copyright (c) 1994 John Aycock
7 * The University of Calgary Department of Computer Science.
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)
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.
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.
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), ...
32 * --------------------------------------------------------------------------
34 * Modifications by Daniel M. Eischen (deischen@iworks.InterWorks.org):
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.
40 * --------------------------------------------------------------------------
41 * Copyright (c) 1994-2000 Justin T. Gibbs.
42 * Copyright (c) 2000-2001 Adaptec Inc.
43 * All rights reserved.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
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.
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.
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.
77 *---------------------------------------------------------------------------
79 * Thanks also go to (in alphabetical order) the following:
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
86 * A Boot time option was also added for not resetting the scsi bus.
88 * Form: aic7xxx=extended
92 * Daniel M. Eischen, deischen@iworks.InterWorks.org, 1/23/97
94 * Id: aic7xxx.c,v 4.1 1997/06/12 08:23:42 deang Exp
98 * Further driver modifications made by Doug Ledford <dledford@redhat.com>
100 * Copyright (c) 1997-1999 Doug Ledford
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.
107 * Modifications made to the aic7xxx.c,v 4.1 driver from Dan Eischen include
108 * but are not limited to:
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
122 #include "aic7xxx_osm.h"
123 #include "aic7xxx_inline.h"
124 #include <scsi/scsicam.h>
127 * Include aiclib.c as part of our
128 * "module dependencies are hard" work around.
132 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
133 #include <linux/init.h> /* __setup */
137 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
138 #include "sd.h" /* For geometry detection */
141 #include <linux/mm.h> /* For fetching system memory size */
142 #include <linux/blkdev.h> /* For block_size() */
145 * Lock protecting manipulation of the ahc softc list.
147 spinlock_t ahc_list_spinlock;
149 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
150 /* For dynamic sglist size calculation. */
151 u_int ahc_linux_nseg;
154 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
155 struct proc_dir_entry proc_scsi_aic7xxx = {
156 PROC_SCSI_AIC7XXX, 7, "aic7xxx",
157 S_IFDIR | S_IRUGO | S_IXUGO, 2,
158 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL
163 * Set this to the delay in seconds after SCSI bus reset.
164 * Note, we honor this only for the initial bus reset.
165 * The scsi error recovery code performs its own bus settle
166 * delay handling for error recovery actions.
168 #ifdef CONFIG_AIC7XXX_RESET_DELAY_MS
169 #define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS
171 #define AIC7XXX_RESET_DELAY 5000
175 * Control collection of SCSI transfer statistics for the /proc filesystem.
177 * NOTE: Do NOT enable this when running on kernels version 1.2.x and below.
178 * NOTE: This does affect performance since it has to maintain statistics.
180 #ifdef CONFIG_AIC7XXX_PROC_STATS
181 #define AIC7XXX_PROC_STATS
185 * To change the default number of tagged transactions allowed per-device,
186 * add a line to the lilo.conf file like:
187 * append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
188 * which will result in the first four devices on the first two
189 * controllers being set to a tagged queue depth of 32.
191 * The tag_commands is an array of 16 to allow for wide and twin adapters.
192 * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
196 uint8_t tag_commands[16]; /* Allow for wide/twin adapters. */
197 } adapter_tag_info_t;
200 * Modify this as you see fit for your system.
202 * 0 tagged queuing disabled
203 * 1 <= n <= 253 n == max tags ever dispatched.
205 * The driver will throttle the number of commands dispatched to a
206 * device if it returns queue full. For devices with a fixed maximum
207 * queue depth, the driver will eventually determine this depth and
208 * lock it in (a console message is printed to indicate that a lock
209 * has occurred). On some devices, queue full is returned for a temporary
210 * resource shortage. These devices will return queue full at varying
211 * depths. The driver will throttle back when the queue fulls occur and
212 * attempt to slowly increase the depth over time as the device recovers
213 * from the resource shortage.
215 * In this example, the first line will disable tagged queueing for all
216 * the devices on the first probed aic7xxx adapter.
218 * The second line enables tagged queueing with 4 commands/LUN for IDs
219 * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
220 * driver to attempt to use up to 64 tags for ID 1.
222 * The third line is the same as the first line.
224 * The fourth line disables tagged queueing for devices 0 and 3. It
225 * enables tagged queueing for the other IDs, with 16 commands/LUN
226 * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
227 * IDs 2, 5-7, and 9-15.
231 * NOTE: The below structure is for reference only, the actual structure
232 * to modify in order to change things is just below this comment block.
233 adapter_tag_info_t aic7xxx_tag_info[] =
235 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
236 {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
237 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
238 {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
242 #ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
243 #define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
245 #define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE
248 #define AIC7XXX_CONFIGED_TAG_COMMANDS { \
249 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
250 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
251 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
252 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
253 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
254 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
255 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
256 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE \
260 * By default, use the number of commands specified by
261 * the users kernel configuration.
263 static adapter_tag_info_t aic7xxx_tag_info[] =
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 {AIC7XXX_CONFIGED_TAG_COMMANDS},
276 {AIC7XXX_CONFIGED_TAG_COMMANDS},
277 {AIC7XXX_CONFIGED_TAG_COMMANDS},
278 {AIC7XXX_CONFIGED_TAG_COMMANDS},
279 {AIC7XXX_CONFIGED_TAG_COMMANDS},
280 {AIC7XXX_CONFIGED_TAG_COMMANDS}
286 * positive value = DV Enabled
288 * negative value = DV Default for adapter type/seeprom
290 #ifdef CONFIG_AIC7XXX_DV_SETTING
291 #define AIC7XXX_CONFIGED_DV CONFIG_AIC7XXX_DV_SETTING
293 #define AIC7XXX_CONFIGED_DV -1
296 static int8_t aic7xxx_dv_settings[] =
317 * There should be a specific return value for this in scsi.h, but
318 * it seems that most drivers ignore it.
320 #define DID_UNDERFLOW DID_ERROR
323 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
325 printk("(scsi%d:%c:%d:%d): ",
326 ahc->platform_data->host->host_no,
327 scb != NULL ? SCB_GET_CHANNEL(ahc, scb) : 'X',
328 scb != NULL ? SCB_GET_TARGET(ahc, scb) : -1,
329 scb != NULL ? SCB_GET_LUN(scb) : -1);
333 * XXX - these options apply unilaterally to _all_ 274x/284x/294x
334 * cards in the system. This should be fixed. Exceptions to this
335 * rule are noted in the comments.
339 * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This
340 * has no effect on any later resets that might occur due to things like
343 static uint32_t aic7xxx_no_reset;
346 * Certain PCI motherboards will scan PCI devices from highest to lowest,
347 * others scan from lowest to highest, and they tend to do all kinds of
348 * strange things when they come into contact with PCI bridge chips. The
349 * net result of all this is that the PCI card that is actually used to boot
350 * the machine is very hard to detect. Most motherboards go from lowest
351 * PCI slot number to highest, and the first SCSI controller found is the
352 * one you boot from. The only exceptions to this are when a controller
353 * has its BIOS disabled. So, we by default sort all of our SCSI controllers
354 * from lowest PCI slot number to highest PCI slot number. We also force
355 * all controllers with their BIOS disabled to the end of the list. This
356 * works on *almost* all computers. Where it doesn't work, we have this
357 * option. Setting this option to non-0 will reverse the order of the sort
358 * to highest first, then lowest, but will still leave cards with their BIOS
359 * disabled at the very end. That should fix everyone up unless there are
360 * really strange cirumstances.
362 static uint32_t aic7xxx_reverse_scan;
365 * Should we force EXTENDED translation on a controller.
366 * 0 == Use whatever is in the SEEPROM or default to off
367 * 1 == Use whatever is in the SEEPROM or default to on
369 static uint32_t aic7xxx_extended;
372 * PCI bus parity checking of the Adaptec controllers. This is somewhat
373 * dubious at best. To my knowledge, this option has never actually
374 * solved a PCI parity problem, but on certain machines with broken PCI
375 * chipset configurations where stray PCI transactions with bad parity are
376 * the norm rather than the exception, the error messages can be overwelming.
377 * It's included in the driver for completeness.
378 * 0 = Shut off PCI parity check
379 * non-0 = reverse polarity pci parity checking
381 static uint32_t aic7xxx_pci_parity = ~0;
384 * Certain newer motherboards have put new PCI based devices into the
385 * IO spaces that used to typically be occupied by VLB or EISA cards.
386 * This overlap can cause these newer motherboards to lock up when scanned
387 * for older EISA and VLB devices. Setting this option to non-0 will
388 * cause the driver to skip scanning for any VLB or EISA controllers and
389 * only support the PCI controllers. NOTE: this means that if the kernel
390 * os compiled with PCI support disabled, then setting this to non-0
391 * would result in never finding any devices :)
393 #ifndef CONFIG_AIC7XXX_PROBE_EISA_VL
394 uint32_t aic7xxx_probe_eisa_vl;
396 uint32_t aic7xxx_probe_eisa_vl = ~0;
400 * There are lots of broken chipsets in the world. Some of them will
401 * violate the PCI spec when we issue byte sized memory writes to our
402 * controller. I/O mapped register access, if allowed by the given
403 * platform, will work in almost all cases.
405 uint32_t aic7xxx_allow_memio = ~0;
408 * aic7xxx_detect() has been run, so register all device arrivals
409 * immediately with the system rather than deferring to the sorted
410 * attachment performed by aic7xxx_detect().
412 int aic7xxx_detect_complete;
415 * So that we can set how long each device is given as a selection timeout.
416 * The table of values goes like this:
421 * We default to 256ms because some older devices need a longer time
422 * to respond to initial selection.
424 static uint32_t aic7xxx_seltime;
427 * Certain devices do not perform any aging on commands. Should the
428 * device be saturated by commands in one portion of the disk, it is
429 * possible for transactions on far away sectors to never be serviced.
430 * To handle these devices, we can periodically send an ordered tag to
431 * force all outstanding transactions to be serviced prior to a new
434 uint32_t aic7xxx_periodic_otag;
437 * Module information and settable options.
440 static char *aic7xxx = NULL;
442 * Just in case someone uses commas to separate items on the insmod
443 * command line, we define a dummy buffer here to avoid having insmod
444 * write wild stuff into our code segment
446 static char dummy_buffer[60] = "Please don't trounce on me insmod!!\n";
448 MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
449 MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
450 #ifdef MODULE_LICENSE
451 MODULE_LICENSE("Dual BSD/GPL");
453 MODULE_PARM(aic7xxx, "s");
454 MODULE_PARM_DESC(aic7xxx,
455 "period delimited, options string.\n"
456 " verbose Enable verbose/diagnostic logging\n"
457 " allow_memio Allow device registers to be memory mapped\n"
458 " debug Bitmask of debug values to enable\n"
459 " no_probe Toggle EISA/VLB controller probing\n"
460 " probe_eisa_vl Toggle EISA/VLB controller probing\n"
461 " no_reset Supress initial bus resets\n"
462 " extended Enable extended geometry on all controllers\n"
463 " periodic_otag Send an ordered tagged transaction\n"
464 " periodically to prevent tag starvation.\n"
465 " This may be required by some older disk\n"
466 " drives or RAID arrays.\n"
467 " reverse_scan Sort PCI devices highest Bus/Slot to lowest\n"
468 " tag_info:<tag_str> Set per-target tag depth\n"
469 " global_tag_depth:<int> Global tag depth for every target\n"
471 " dv:<dv_settings> Set per-controller Domain Validation Setting.\n"
472 " seltime:<int> Selection Timeout\n"
473 " (0/256ms,1/128ms,2/64ms,3/32ms)\n"
475 " Sample /etc/modprobe.conf line:\n"
476 " Toggle EISA/VLB probing\n"
477 " Set tag depth on Controller 1/Target 1 to 10 tags\n"
478 " Shorten the selection timeout to 128ms\n"
480 " options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n"
484 static void ahc_linux_handle_scsi_status(struct ahc_softc *,
485 struct ahc_linux_device *,
487 static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
489 static void ahc_linux_filter_inquiry(struct ahc_softc*, struct ahc_devinfo*);
490 static void ahc_linux_sem_timeout(u_long arg);
491 static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
492 static void ahc_linux_release_simq(u_long arg);
493 static void ahc_linux_dev_timed_unfreeze(u_long arg);
494 static int ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag);
495 static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
496 static void ahc_linux_size_nseg(void);
497 static void ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc);
498 static void ahc_linux_start_dv(struct ahc_softc *ahc);
499 static void ahc_linux_dv_timeout(struct scsi_cmnd *cmd);
500 static int ahc_linux_dv_thread(void *data);
501 static void ahc_linux_kill_dv_thread(struct ahc_softc *ahc);
502 static void ahc_linux_dv_target(struct ahc_softc *ahc, u_int target);
503 static void ahc_linux_dv_transition(struct ahc_softc *ahc,
504 struct scsi_cmnd *cmd,
505 struct ahc_devinfo *devinfo,
506 struct ahc_linux_target *targ);
507 static void ahc_linux_dv_fill_cmd(struct ahc_softc *ahc,
508 struct scsi_cmnd *cmd,
509 struct ahc_devinfo *devinfo);
510 static void ahc_linux_dv_inq(struct ahc_softc *ahc,
511 struct scsi_cmnd *cmd,
512 struct ahc_devinfo *devinfo,
513 struct ahc_linux_target *targ,
514 u_int request_length);
515 static void ahc_linux_dv_tur(struct ahc_softc *ahc,
516 struct scsi_cmnd *cmd,
517 struct ahc_devinfo *devinfo);
518 static void ahc_linux_dv_rebd(struct ahc_softc *ahc,
519 struct scsi_cmnd *cmd,
520 struct ahc_devinfo *devinfo,
521 struct ahc_linux_target *targ);
522 static void ahc_linux_dv_web(struct ahc_softc *ahc,
523 struct scsi_cmnd *cmd,
524 struct ahc_devinfo *devinfo,
525 struct ahc_linux_target *targ);
526 static void ahc_linux_dv_reb(struct ahc_softc *ahc,
527 struct scsi_cmnd *cmd,
528 struct ahc_devinfo *devinfo,
529 struct ahc_linux_target *targ);
530 static void ahc_linux_dv_su(struct ahc_softc *ahc,
531 struct scsi_cmnd *cmd,
532 struct ahc_devinfo *devinfo,
533 struct ahc_linux_target *targ);
534 static int ahc_linux_fallback(struct ahc_softc *ahc,
535 struct ahc_devinfo *devinfo);
536 static void ahc_linux_dv_complete(Scsi_Cmnd *cmd);
537 static void ahc_linux_generate_dv_pattern(struct ahc_linux_target *targ);
538 static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
539 struct ahc_devinfo *devinfo);
540 static u_int ahc_linux_user_dv_setting(struct ahc_softc *ahc);
541 static void ahc_linux_device_queue_depth(struct ahc_softc *ahc,
542 struct ahc_linux_device *dev);
543 static struct ahc_linux_target* ahc_linux_alloc_target(struct ahc_softc*,
545 static void ahc_linux_free_target(struct ahc_softc*,
546 struct ahc_linux_target*);
547 static struct ahc_linux_device* ahc_linux_alloc_device(struct ahc_softc*,
548 struct ahc_linux_target*,
550 static void ahc_linux_free_device(struct ahc_softc*,
551 struct ahc_linux_device*);
552 static void ahc_linux_run_device_queue(struct ahc_softc*,
553 struct ahc_linux_device*);
554 static void ahc_linux_setup_tag_info_global(char *p);
555 static aic_option_callback_t ahc_linux_setup_tag_info;
556 static aic_option_callback_t ahc_linux_setup_dv;
557 static int aic7xxx_setup(char *s);
558 static int ahc_linux_next_unit(void);
559 static void ahc_runq_tasklet(unsigned long data);
560 static struct ahc_cmd *ahc_linux_run_complete_queue(struct ahc_softc *ahc);
562 /********************************* Inlines ************************************/
563 static __inline void ahc_schedule_runq(struct ahc_softc *ahc);
564 static __inline struct ahc_linux_device*
565 ahc_linux_get_device(struct ahc_softc *ahc, u_int channel,
566 u_int target, u_int lun, int alloc);
567 static __inline void ahc_schedule_completeq(struct ahc_softc *ahc);
568 static __inline void ahc_linux_check_device_queue(struct ahc_softc *ahc,
569 struct ahc_linux_device *dev);
570 static __inline struct ahc_linux_device *
571 ahc_linux_next_device_to_run(struct ahc_softc *ahc);
572 static __inline void ahc_linux_run_device_queues(struct ahc_softc *ahc);
573 static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
575 static __inline int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
576 struct ahc_dma_seg *sg,
577 bus_addr_t addr, bus_size_t len);
580 ahc_schedule_completeq(struct ahc_softc *ahc)
582 if ((ahc->platform_data->flags & AHC_RUN_CMPLT_Q_TIMER) == 0) {
583 ahc->platform_data->flags |= AHC_RUN_CMPLT_Q_TIMER;
584 ahc->platform_data->completeq_timer.expires = jiffies;
585 add_timer(&ahc->platform_data->completeq_timer);
590 * Must be called with our lock held.
593 ahc_schedule_runq(struct ahc_softc *ahc)
595 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
596 tasklet_schedule(&ahc->platform_data->runq_tasklet);
599 * Tasklets are not available, so run inline.
601 ahc_runq_tasklet((unsigned long)ahc);
605 static __inline struct ahc_linux_device*
606 ahc_linux_get_device(struct ahc_softc *ahc, u_int channel, u_int target,
607 u_int lun, int alloc)
609 struct ahc_linux_target *targ;
610 struct ahc_linux_device *dev;
613 target_offset = target;
616 targ = ahc->platform_data->targets[target_offset];
619 targ = ahc_linux_alloc_target(ahc, channel, target);
625 dev = targ->devices[lun];
626 if (dev == NULL && alloc != 0)
627 dev = ahc_linux_alloc_device(ahc, targ, lun);
631 #define AHC_LINUX_MAX_RETURNED_ERRORS 4
632 static struct ahc_cmd *
633 ahc_linux_run_complete_queue(struct ahc_softc *ahc)
635 struct ahc_cmd *acmd;
640 ahc_done_lock(ahc, &done_flags);
641 while ((acmd = TAILQ_FIRST(&ahc->platform_data->completeq)) != NULL) {
644 if (with_errors > AHC_LINUX_MAX_RETURNED_ERRORS) {
646 * Linux uses stack recursion to requeue
647 * commands that need to be retried. Avoid
648 * blowing out the stack by "spoon feeding"
649 * commands that completed with error back
650 * the operating system in case they are going
651 * to be retried. "ick"
653 ahc_schedule_completeq(ahc);
656 TAILQ_REMOVE(&ahc->platform_data->completeq,
657 acmd, acmd_links.tqe);
658 cmd = &acmd_scsi_cmd(acmd);
659 cmd->host_scribble = NULL;
660 if (ahc_cmd_get_transaction_status(cmd) != DID_OK
661 || (cmd->result & 0xFF) != SCSI_STATUS_OK)
666 ahc_done_unlock(ahc, &done_flags);
671 ahc_linux_check_device_queue(struct ahc_softc *ahc,
672 struct ahc_linux_device *dev)
674 if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) != 0
675 && dev->active == 0) {
676 dev->flags &= ~AHC_DEV_FREEZE_TIL_EMPTY;
680 if (TAILQ_FIRST(&dev->busyq) == NULL
681 || dev->openings == 0 || dev->qfrozen != 0)
684 ahc_linux_run_device_queue(ahc, dev);
687 static __inline struct ahc_linux_device *
688 ahc_linux_next_device_to_run(struct ahc_softc *ahc)
691 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0
692 || (ahc->platform_data->qfrozen != 0
693 && AHC_DV_SIMQ_FROZEN(ahc) == 0))
695 return (TAILQ_FIRST(&ahc->platform_data->device_runq));
699 ahc_linux_run_device_queues(struct ahc_softc *ahc)
701 struct ahc_linux_device *dev;
703 while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) {
704 TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links);
705 dev->flags &= ~AHC_DEV_ON_RUN_LIST;
706 ahc_linux_check_device_queue(ahc, dev);
711 ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
716 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
717 if (cmd->use_sg != 0) {
718 struct scatterlist *sg;
720 sg = (struct scatterlist *)cmd->request_buffer;
721 pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg,
722 scsi_to_pci_dma_dir(cmd->sc_data_direction));
723 } else if (cmd->request_bufflen != 0) {
724 pci_unmap_single(ahc->dev_softc,
725 scb->platform_data->buf_busaddr,
726 cmd->request_bufflen,
727 scsi_to_pci_dma_dir(cmd->sc_data_direction));
732 ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
733 struct ahc_dma_seg *sg, bus_addr_t addr, bus_size_t len)
737 if ((scb->sg_count + 1) > AHC_NSEG)
738 panic("Too few segs for dma mapping. "
739 "Increase AHC_NSEG\n");
742 sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
743 scb->platform_data->xfer_len += len;
745 if (sizeof(bus_addr_t) > 4
746 && (ahc->flags & AHC_39BIT_ADDRESSING) != 0)
747 len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK;
749 sg->len = ahc_htole32(len);
753 /************************ Host template entry points *************************/
754 static int ahc_linux_detect(Scsi_Host_Template *);
755 static int ahc_linux_queue(Scsi_Cmnd *, void (*)(Scsi_Cmnd *));
756 static const char *ahc_linux_info(struct Scsi_Host *);
757 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
758 static int ahc_linux_slave_alloc(Scsi_Device *);
759 static int ahc_linux_slave_configure(Scsi_Device *);
760 static void ahc_linux_slave_destroy(Scsi_Device *);
761 #if defined(__i386__)
762 static int ahc_linux_biosparam(struct scsi_device*,
763 struct block_device*,
767 static int ahc_linux_release(struct Scsi_Host *);
768 static void ahc_linux_select_queue_depth(struct Scsi_Host *host,
769 Scsi_Device *scsi_devs);
770 #if defined(__i386__)
771 static int ahc_linux_biosparam(Disk *, kdev_t, int[]);
774 static int ahc_linux_bus_reset(Scsi_Cmnd *);
775 static int ahc_linux_dev_reset(Scsi_Cmnd *);
776 static int ahc_linux_abort(Scsi_Cmnd *);
779 * Calculate a safe value for AHC_NSEG (as expressed through ahc_linux_nseg).
782 * The midlayer allocates an S/G array dynamically when a command is issued
783 * using SCSI malloc. This array, which is in an OS dependent format that
784 * must later be copied to our private S/G list, is sized to house just the
785 * number of segments needed for the current transfer. Since the code that
786 * sizes the SCSI malloc pool does not take into consideration fragmentation
787 * of the pool, executing transactions numbering just a fraction of our
788 * concurrent transaction limit with list lengths aproaching AHC_NSEG will
789 * quickly depleat the SCSI malloc pool of usable space. Unfortunately, the
790 * mid-layer does not properly handle this scsi malloc failures for the S/G
791 * array and the result can be a lockup of the I/O subsystem. We try to size
792 * our S/G list so that it satisfies our drivers allocation requirements in
793 * addition to avoiding fragmentation of the SCSI malloc pool.
796 ahc_linux_size_nseg(void)
798 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
803 * The SCSI allocator rounds to the nearest 512 bytes
804 * an cannot allocate across a page boundary. Our algorithm
805 * is to start at 1K of scsi malloc space per-command and
806 * loop through all factors of the PAGE_SIZE and pick the best.
809 for (cur_size = 1024; cur_size <= PAGE_SIZE; cur_size *= 2) {
812 nseg = cur_size / sizeof(struct scatterlist);
813 if (nseg < AHC_LINUX_MIN_NSEG)
816 if (best_size == 0) {
817 best_size = cur_size;
818 ahc_linux_nseg = nseg;
824 * Compare the traits of the current "best_size"
825 * with the current size to determine if the
826 * current size is a better size.
828 best_rem = best_size % sizeof(struct scatterlist);
829 cur_rem = cur_size % sizeof(struct scatterlist);
830 if (cur_rem < best_rem) {
831 best_size = cur_size;
832 ahc_linux_nseg = nseg;
840 * Try to detect an Adaptec 7XXX controller.
843 ahc_linux_detect(Scsi_Host_Template *template)
845 struct ahc_softc *ahc;
847 int eisa_err, pci_err;
849 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
851 * It is a bug that the upper layer takes
852 * this lock just prior to calling us.
854 spin_unlock_irq(&io_request_lock);
858 * Sanity checking of Linux SCSI data structures so
859 * that some of our hacks^H^H^H^H^Hassumptions aren't
862 if (offsetof(struct ahc_cmd_internal, end)
863 > offsetof(struct scsi_cmnd, host_scribble)) {
864 printf("ahc_linux_detect: SCSI data structures changed.\n");
865 printf("ahc_linux_detect: Unable to attach\n");
868 ahc_linux_size_nseg();
871 * If we've been passed any parameters, process them now.
874 aic7xxx_setup(aic7xxx);
875 if (dummy_buffer[0] != 'P')
877 "aic7xxx: Please read the file /usr/src/linux/drivers/scsi/README.aic7xxx\n"
878 "aic7xxx: to see the proper way to specify options to the aic7xxx module\n"
879 "aic7xxx: Specifically, don't use any commas when passing arguments to\n"
880 "aic7xxx: insmod or else it might trash certain memory areas.\n");
883 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,0)
884 template->proc_name = "aic7xxx";
886 template->proc_dir = &proc_scsi_aic7xxx;
890 * Initialize our softc list lock prior to
891 * probing for any adapters.
895 pci_err = ahc_linux_pci_init();
896 eisa_err = ahc_linux_eisa_init();
898 if(pci_err && eisa_err)
903 * Register with the SCSI layer all
904 * controllers we've found.
906 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
908 if (ahc_linux_register_host(ahc, template) == 0)
913 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
914 spin_lock_irq(&io_request_lock);
916 aic7xxx_detect_complete++;
921 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
923 * Free the passed in Scsi_Host memory structures prior to unloading the
927 ahc_linux_release(struct Scsi_Host * host)
929 struct ahc_softc *ahc;
936 * We should be able to just perform
937 * the free directly, but check our
938 * list for extra sanity.
940 ahc = ahc_find_softc(*(struct ahc_softc **)host->hostdata);
945 ahc_intr_enable(ahc, FALSE);
956 * Return a string describing the driver.
959 ahc_linux_info(struct Scsi_Host *host)
961 static char buffer[512];
964 struct ahc_softc *ahc;
967 ahc = *(struct ahc_softc **)host->hostdata;
968 memset(bp, 0, sizeof(buffer));
969 strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev ");
970 strcat(bp, AIC7XXX_DRIVER_VERSION);
973 strcat(bp, ahc->description);
976 ahc_controller_info(ahc, ahc_info);
977 strcat(bp, ahc_info);
984 * Queue an SCB to the controller.
987 ahc_linux_queue(Scsi_Cmnd * cmd, void (*scsi_done) (Scsi_Cmnd *))
989 struct ahc_softc *ahc;
990 struct ahc_linux_device *dev;
993 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
996 * Save the callback on completion function.
998 cmd->scsi_done = scsi_done;
1000 ahc_midlayer_entrypoint_lock(ahc, &flags);
1003 * Close the race of a command that was in the process of
1004 * being queued to us just as our simq was frozen. Let
1005 * DV commands through so long as we are only frozen to
1008 if (ahc->platform_data->qfrozen != 0
1009 && AHC_DV_CMD(cmd) == 0) {
1011 ahc_cmd_set_transaction_status(cmd, CAM_REQUEUE_REQ);
1012 ahc_linux_queue_cmd_complete(ahc, cmd);
1013 ahc_schedule_completeq(ahc);
1014 ahc_midlayer_entrypoint_unlock(ahc, &flags);
1017 dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
1018 cmd->device->lun, /*alloc*/TRUE);
1020 ahc_cmd_set_transaction_status(cmd, CAM_RESRC_UNAVAIL);
1021 ahc_linux_queue_cmd_complete(ahc, cmd);
1022 ahc_schedule_completeq(ahc);
1023 ahc_midlayer_entrypoint_unlock(ahc, &flags);
1024 printf("%s: aic7xxx_linux_queue - Unable to allocate device!\n",
1028 cmd->result = CAM_REQ_INPROG << 16;
1029 TAILQ_INSERT_TAIL(&dev->busyq, (struct ahc_cmd *)cmd, acmd_links.tqe);
1030 if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) {
1031 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links);
1032 dev->flags |= AHC_DEV_ON_RUN_LIST;
1033 ahc_linux_run_device_queues(ahc);
1035 ahc_midlayer_entrypoint_unlock(ahc, &flags);
1039 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1041 ahc_linux_slave_alloc(Scsi_Device *device)
1043 struct ahc_softc *ahc;
1045 ahc = *((struct ahc_softc **)device->host->hostdata);
1047 printf("%s: Slave Alloc %d\n", ahc_name(ahc), device->id);
1052 ahc_linux_slave_configure(Scsi_Device *device)
1054 struct ahc_softc *ahc;
1055 struct ahc_linux_device *dev;
1058 ahc = *((struct ahc_softc **)device->host->hostdata);
1060 printf("%s: Slave Configure %d\n", ahc_name(ahc), device->id);
1061 ahc_midlayer_entrypoint_lock(ahc, &flags);
1063 * Since Linux has attached to the device, configure
1064 * it so we don't free and allocate the device
1065 * structure on every command.
1067 dev = ahc_linux_get_device(ahc, device->channel,
1068 device->id, device->lun,
1071 dev->flags &= ~AHC_DEV_UNCONFIGURED;
1072 dev->scsi_device = device;
1073 ahc_linux_device_queue_depth(ahc, dev);
1075 ahc_midlayer_entrypoint_unlock(ahc, &flags);
1080 ahc_linux_slave_destroy(Scsi_Device *device)
1082 struct ahc_softc *ahc;
1083 struct ahc_linux_device *dev;
1086 ahc = *((struct ahc_softc **)device->host->hostdata);
1088 printf("%s: Slave Destroy %d\n", ahc_name(ahc), device->id);
1089 ahc_midlayer_entrypoint_lock(ahc, &flags);
1090 dev = ahc_linux_get_device(ahc, device->channel,
1091 device->id, device->lun,
1094 * Filter out "silly" deletions of real devices by only
1095 * deleting devices that have had slave_configure()
1096 * called on them. All other devices that have not
1097 * been configured will automatically be deleted by
1098 * the refcounting process.
1101 && (dev->flags & AHC_DEV_SLAVE_CONFIGURED) != 0) {
1102 dev->flags |= AHC_DEV_UNCONFIGURED;
1103 if (TAILQ_EMPTY(&dev->busyq)
1105 && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0)
1106 ahc_linux_free_device(ahc, dev);
1108 ahc_midlayer_entrypoint_unlock(ahc, &flags);
1112 * Sets the queue depth for each SCSI device hanging
1113 * off the input host adapter.
1116 ahc_linux_select_queue_depth(struct Scsi_Host *host, Scsi_Device *scsi_devs)
1118 Scsi_Device *device;
1120 struct ahc_softc *ahc;
1123 ahc = *((struct ahc_softc **)host->hostdata);
1124 ahc_lock(ahc, &flags);
1125 for (device = scsi_devs; device != NULL; device = device->next) {
1128 * Watch out for duplicate devices. This works around
1129 * some quirks in how the SCSI scanning code does its
1130 * device management.
1132 for (ldev = scsi_devs; ldev != device; ldev = ldev->next) {
1133 if (ldev->host == device->host
1134 && ldev->channel == device->channel
1135 && ldev->id == device->id
1136 && ldev->lun == device->lun)
1139 /* Skip duplicate. */
1143 if (device->host == host) {
1144 struct ahc_linux_device *dev;
1147 * Since Linux has attached to the device, configure
1148 * it so we don't free and allocate the device
1149 * structure on every command.
1151 dev = ahc_linux_get_device(ahc, device->channel,
1152 device->id, device->lun,
1155 dev->flags &= ~AHC_DEV_UNCONFIGURED;
1156 dev->scsi_device = device;
1157 ahc_linux_device_queue_depth(ahc, dev);
1158 device->queue_depth = dev->openings
1160 if ((dev->flags & (AHC_DEV_Q_BASIC
1161 | AHC_DEV_Q_TAGGED)) == 0) {
1163 * We allow the OS to queue 2 untagged
1164 * transactions to us at any time even
1165 * though we can only execute them
1166 * serially on the controller/device.
1167 * This should remove some latency.
1169 device->queue_depth = 2;
1174 ahc_unlock(ahc, &flags);
1178 #if defined(__i386__)
1180 * Return the disk geometry for the given SCSI device.
1183 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1184 ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1185 sector_t capacity, int geom[])
1189 ahc_linux_biosparam(Disk *disk, kdev_t dev, int geom[])
1191 struct scsi_device *sdev = disk->device;
1192 u_long capacity = disk->capacity;
1193 struct buffer_head *bh;
1200 struct ahc_softc *ahc;
1203 ahc = *((struct ahc_softc **)sdev->host->hostdata);
1204 channel = sdev->channel;
1206 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1207 bh = scsi_bios_ptable(bdev);
1208 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,17)
1209 bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, block_size(dev));
1211 bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, 1024);
1215 ret = scsi_partsize(bh, capacity,
1216 &geom[2], &geom[0], &geom[1]);
1217 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1227 cylinders = aic_sector_div(capacity, heads, sectors);
1229 if (aic7xxx_extended != 0)
1231 else if (channel == 0)
1232 extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
1234 extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
1235 if (extended && cylinders >= 1024) {
1238 cylinders = aic_sector_div(capacity, heads, sectors);
1242 geom[2] = cylinders;
1248 * Abort the current SCSI command(s).
1251 ahc_linux_abort(Scsi_Cmnd *cmd)
1255 error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
1257 printf("aic7xxx_abort returns 0x%x\n", error);
1262 * Attempt to send a target reset message to the device that timed out.
1265 ahc_linux_dev_reset(Scsi_Cmnd *cmd)
1269 error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
1271 printf("aic7xxx_dev_reset returns 0x%x\n", error);
1276 * Reset the SCSI bus.
1279 ahc_linux_bus_reset(Scsi_Cmnd *cmd)
1281 struct ahc_softc *ahc;
1285 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
1286 ahc_midlayer_entrypoint_lock(ahc, &s);
1287 found = ahc_reset_channel(ahc, cmd->device->channel + 'A',
1288 /*initiate reset*/TRUE);
1289 ahc_linux_run_complete_queue(ahc);
1290 ahc_midlayer_entrypoint_unlock(ahc, &s);
1293 printf("%s: SCSI bus reset delivered. "
1294 "%d SCBs aborted.\n", ahc_name(ahc), found);
1299 Scsi_Host_Template aic7xxx_driver_template = {
1300 .module = THIS_MODULE,
1302 .proc_info = ahc_linux_proc_info,
1303 .info = ahc_linux_info,
1304 .queuecommand = ahc_linux_queue,
1305 .eh_abort_handler = ahc_linux_abort,
1306 .eh_device_reset_handler = ahc_linux_dev_reset,
1307 .eh_bus_reset_handler = ahc_linux_bus_reset,
1308 #if defined(__i386__)
1309 .bios_param = ahc_linux_biosparam,
1311 .can_queue = AHC_MAX_QUEUE,
1314 .use_clustering = ENABLE_CLUSTERING,
1315 .slave_alloc = ahc_linux_slave_alloc,
1316 .slave_configure = ahc_linux_slave_configure,
1317 .slave_destroy = ahc_linux_slave_destroy,
1320 /**************************** Tasklet Handler *********************************/
1323 * In 2.4.X and above, this routine is called from a tasklet,
1324 * so we must re-acquire our lock prior to executing this code.
1325 * In all prior kernels, ahc_schedule_runq() calls this routine
1326 * directly and ahc_schedule_runq() is called with our lock held.
1329 ahc_runq_tasklet(unsigned long data)
1331 struct ahc_softc* ahc;
1332 struct ahc_linux_device *dev;
1333 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1337 ahc = (struct ahc_softc *)data;
1338 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1339 ahc_lock(ahc, &flags);
1341 while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) {
1343 TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links);
1344 dev->flags &= ~AHC_DEV_ON_RUN_LIST;
1345 ahc_linux_check_device_queue(ahc, dev);
1346 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1347 /* Yeild to our interrupt handler */
1348 ahc_unlock(ahc, &flags);
1349 ahc_lock(ahc, &flags);
1352 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1353 ahc_unlock(ahc, &flags);
1357 /******************************** Macros **************************************/
1358 #define BUILD_SCSIID(ahc, cmd) \
1359 ((((cmd)->device->id << TID_SHIFT) & TID) \
1360 | (((cmd)->device->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \
1361 | (((cmd)->device->channel == 0) ? 0 : TWIN_CHNLB))
1363 /******************************** Bus DMA *************************************/
1365 ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent,
1366 bus_size_t alignment, bus_size_t boundary,
1367 bus_addr_t lowaddr, bus_addr_t highaddr,
1368 bus_dma_filter_t *filter, void *filterarg,
1369 bus_size_t maxsize, int nsegments,
1370 bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
1374 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
1379 * Linux is very simplistic about DMA memory. For now don't
1380 * maintain all specification information. Once Linux supplies
1381 * better facilities for doing these operations, or the
1382 * needs of this particular driver change, we might need to do
1385 dmat->alignment = alignment;
1386 dmat->boundary = boundary;
1387 dmat->maxsize = maxsize;
1393 ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
1395 free(dmat, M_DEVBUF);
1399 ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
1400 int flags, bus_dmamap_t *mapp)
1404 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1405 map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT);
1409 * Although we can dma data above 4GB, our
1410 * "consistent" memory is below 4GB for
1411 * space efficiency reasons (only need a 4byte
1412 * address). For this reason, we have to reset
1413 * our dma mask when doing allocations.
1415 if (ahc->dev_softc != NULL)
1416 if (ahc_pci_set_dma_mask(ahc->dev_softc, 0xFFFFFFFF)) {
1417 printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
1420 *vaddr = pci_alloc_consistent(ahc->dev_softc,
1421 dmat->maxsize, &map->bus_addr);
1422 if (ahc->dev_softc != NULL)
1423 if (ahc_pci_set_dma_mask(ahc->dev_softc,
1424 ahc->platform_data->hw_dma_mask)) {
1425 printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
1428 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0) */
1430 * At least in 2.2.14, malloc is a slab allocator so all
1431 * allocations are aligned. We assume for these kernel versions
1432 * that all allocations will be bellow 4Gig, physically contiguous,
1433 * and accessible via DMA by the controller.
1435 map = NULL; /* No additional information to store */
1436 *vaddr = malloc(dmat->maxsize, M_DEVBUF, M_NOWAIT);
1445 ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
1446 void* vaddr, bus_dmamap_t map)
1448 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1449 pci_free_consistent(ahc->dev_softc, dmat->maxsize,
1450 vaddr, map->bus_addr);
1452 free(vaddr, M_DEVBUF);
1457 ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map,
1458 void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
1459 void *cb_arg, int flags)
1462 * Assume for now that this will only be used during
1463 * initialization and not for per-transaction buffer mapping.
1465 bus_dma_segment_t stack_sg;
1467 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1468 stack_sg.ds_addr = map->bus_addr;
1470 #define VIRT_TO_BUS(a) (uint32_t)virt_to_bus((void *)(a))
1471 stack_sg.ds_addr = VIRT_TO_BUS(buf);
1473 stack_sg.ds_len = dmat->maxsize;
1474 cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
1479 ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
1482 * The map may is NULL in our < 2.3.X implementation.
1485 free(map, M_DEVBUF);
1489 ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
1495 /********************* Platform Dependent Functions ***************************/
1497 * Compare "left hand" softc with "right hand" softc, returning:
1498 * < 0 - lahc has a lower priority than rahc
1499 * 0 - Softcs are equal
1500 * > 0 - lahc has a higher priority than rahc
1503 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1510 * Under Linux, cards are ordered as follows:
1511 * 1) VLB/EISA BIOS enabled devices sorted by BIOS address.
1512 * 2) PCI devices with BIOS enabled sorted by bus/slot/func.
1513 * 3) All remaining VLB/EISA devices sorted by ioport.
1514 * 4) All remaining PCI devices sorted by bus/slot/func.
1516 value = (lahc->flags & AHC_BIOS_ENABLED)
1517 - (rahc->flags & AHC_BIOS_ENABLED);
1519 /* Controllers with BIOS enabled have a *higher* priority */
1523 * Same BIOS setting, now sort based on bus type.
1524 * EISA and VL controllers sort together. EISA/VL
1525 * have higher priority than PCI.
1527 rvalue = (rahc->chip & AHC_BUS_MASK);
1528 if (rvalue == AHC_VL)
1530 lvalue = (lahc->chip & AHC_BUS_MASK);
1531 if (lvalue == AHC_VL)
1533 value = rvalue - lvalue;
1537 /* Still equal. Sort by BIOS address, ioport, or bus/slot/func. */
1542 char primary_channel;
1544 if (aic7xxx_reverse_scan != 0)
1545 value = ahc_get_pci_bus(lahc->dev_softc)
1546 - ahc_get_pci_bus(rahc->dev_softc);
1548 value = ahc_get_pci_bus(rahc->dev_softc)
1549 - ahc_get_pci_bus(lahc->dev_softc);
1552 if (aic7xxx_reverse_scan != 0)
1553 value = ahc_get_pci_slot(lahc->dev_softc)
1554 - ahc_get_pci_slot(rahc->dev_softc);
1556 value = ahc_get_pci_slot(rahc->dev_softc)
1557 - ahc_get_pci_slot(lahc->dev_softc);
1561 * On multi-function devices, the user can choose
1562 * to have function 1 probed before function 0.
1563 * Give whichever channel is the primary channel
1564 * the highest priority.
1566 primary_channel = (lahc->flags & AHC_PRIMARY_CHANNEL) + 'A';
1568 if (lahc->channel == primary_channel)
1574 if ((rahc->flags & AHC_BIOS_ENABLED) != 0) {
1575 value = rahc->platform_data->bios_address
1576 - lahc->platform_data->bios_address;
1578 value = rahc->bsh.ioport
1583 panic("ahc_softc_sort: invalid bus type");
1589 ahc_linux_setup_tag_info_global(char *p)
1593 tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
1594 printf("Setting Global Tags= %d\n", tags);
1596 for (i = 0; i < NUM_ELEMENTS(aic7xxx_tag_info); i++) {
1597 for (j = 0; j < AHC_NUM_TARGETS; j++) {
1598 aic7xxx_tag_info[i].tag_commands[j] = tags;
1604 ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
1607 if ((instance >= 0) && (targ >= 0)
1608 && (instance < NUM_ELEMENTS(aic7xxx_tag_info))
1609 && (targ < AHC_NUM_TARGETS)) {
1610 aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff;
1612 printf("tag_info[%d:%d] = %d\n", instance, targ, value);
1617 ahc_linux_setup_dv(u_long arg, int instance, int targ, int32_t value)
1621 && (instance < NUM_ELEMENTS(aic7xxx_dv_settings))) {
1622 aic7xxx_dv_settings[instance] = value;
1624 printf("dv[%d] = %d\n", instance, value);
1629 * Handle Linux boot parameters. This routine allows for assigning a value
1630 * to a parameter with a ':' between the parameter and the value.
1631 * ie. aic7xxx=stpwlev:1,extended
1634 aic7xxx_setup(char *s)
1644 { "extended", &aic7xxx_extended },
1645 { "no_reset", &aic7xxx_no_reset },
1646 { "verbose", &aic7xxx_verbose },
1647 { "allow_memio", &aic7xxx_allow_memio},
1649 { "debug", &ahc_debug },
1651 { "reverse_scan", &aic7xxx_reverse_scan },
1652 { "no_probe", &aic7xxx_probe_eisa_vl },
1653 { "probe_eisa_vl", &aic7xxx_probe_eisa_vl },
1654 { "periodic_otag", &aic7xxx_periodic_otag },
1655 { "pci_parity", &aic7xxx_pci_parity },
1656 { "seltime", &aic7xxx_seltime },
1657 { "tag_info", NULL },
1658 { "global_tag_depth", NULL },
1662 end = strchr(s, '\0');
1665 * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
1666 * will never be 0 in this case.
1670 while ((p = strsep(&s, ",.")) != NULL) {
1673 for (i = 0; i < NUM_ELEMENTS(options); i++) {
1675 n = strlen(options[i].name);
1676 if (strncmp(options[i].name, p, n) == 0)
1679 if (i == NUM_ELEMENTS(options))
1682 if (strncmp(p, "global_tag_depth", n) == 0) {
1683 ahc_linux_setup_tag_info_global(p + n);
1684 } else if (strncmp(p, "tag_info", n) == 0) {
1685 s = aic_parse_brace_option("tag_info", p + n, end,
1686 2, ahc_linux_setup_tag_info, 0);
1687 } else if (strncmp(p, "dv", n) == 0) {
1688 s = aic_parse_brace_option("dv", p + n, end, 1,
1689 ahc_linux_setup_dv, 0);
1690 } else if (p[n] == ':') {
1691 *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
1692 } else if (strncmp(p, "verbose", n) == 0) {
1693 *(options[i].flag) = 1;
1695 *(options[i].flag) ^= 0xFFFFFFFF;
1701 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,0)
1702 __setup("aic7xxx=", aic7xxx_setup);
1705 uint32_t aic7xxx_verbose;
1708 ahc_linux_register_host(struct ahc_softc *ahc, Scsi_Host_Template *template)
1711 struct Scsi_Host *host;
1716 template->name = ahc->description;
1717 host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
1721 *((struct ahc_softc **)host->hostdata) = ahc;
1723 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1724 scsi_assign_lock(host, &ahc->platform_data->spin_lock);
1725 #elif AHC_SCSI_HAS_HOST_LOCK != 0
1726 host->lock = &ahc->platform_data->spin_lock;
1728 ahc->platform_data->host = host;
1729 host->can_queue = AHC_MAX_QUEUE;
1730 host->cmd_per_lun = 2;
1731 /* XXX No way to communicate the ID for multiple channels */
1732 host->this_id = ahc->our_id;
1733 host->irq = ahc->platform_data->irq;
1734 host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
1735 host->max_lun = AHC_NUM_LUNS;
1736 host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
1737 host->sg_tablesize = AHC_NSEG;
1738 ahc_set_unit(ahc, ahc_linux_next_unit());
1739 sprintf(buf, "scsi%d", host->host_no);
1740 new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
1741 if (new_name != NULL) {
1742 strcpy(new_name, buf);
1743 ahc_set_name(ahc, new_name);
1745 host->unique_id = ahc->unit;
1746 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,4) && \
1747 LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1748 scsi_set_pci_device(host, ahc->dev_softc);
1750 ahc_linux_initialize_scsi_bus(ahc);
1751 ahc_unlock(ahc, &s);
1752 ahc->platform_data->dv_pid = kernel_thread(ahc_linux_dv_thread, ahc, 0);
1754 if (ahc->platform_data->dv_pid < 0) {
1755 printf("%s: Failed to create DV thread, error= %d\n",
1756 ahc_name(ahc), ahc->platform_data->dv_pid);
1757 return (-ahc->platform_data->dv_pid);
1760 * Initially allocate *all* of our linux target objects
1761 * so that the DV thread will scan them all in parallel
1762 * just after driver initialization. Any device that
1763 * does not exist will have its target object destroyed
1764 * by the selection timeout handler. In the case of a
1765 * device that appears after the initial DV scan, async
1766 * negotiation will occur for the first command, and DV
1767 * will comence should that first command be successful.
1769 for (targ_offset = 0;
1770 targ_offset < host->max_id * (host->max_channel + 1);
1776 target = targ_offset;
1778 && (ahc->features & AHC_TWIN) != 0) {
1783 * Skip our own ID. Some Compaq/HP storage devices
1784 * have enclosure management devices that respond to
1785 * single bit selection (i.e. selecting ourselves).
1786 * It is expected that either an external application
1787 * or a modified kernel will be used to probe this
1788 * ID if it is appropriate. To accommodate these
1789 * installations, ahc_linux_alloc_target() will allocate
1790 * for our ID if asked to do so.
1792 if ((channel == 0 && target == ahc->our_id)
1793 || (channel == 1 && target == ahc->our_id_b))
1796 ahc_linux_alloc_target(ahc, channel, target);
1798 ahc_intr_enable(ahc, TRUE);
1799 ahc_linux_start_dv(ahc);
1800 ahc_unlock(ahc, &s);
1802 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1803 scsi_add_host(host, (ahc->dev_softc ? &ahc->dev_softc->dev : NULL)); /* XXX handle failure */
1804 scsi_scan_host(host);
1810 ahc_linux_get_memsize(void)
1815 return ((uint64_t)si.totalram << PAGE_SHIFT);
1819 * Find the smallest available unit number to use
1820 * for a new device. We don't just use a static
1821 * count to handle the "repeated hot-(un)plug"
1825 ahc_linux_next_unit(void)
1827 struct ahc_softc *ahc;
1832 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
1833 if (ahc->unit == unit) {
1842 * Place the SCSI bus into a known state by either resetting it,
1843 * or forcing transfer negotiations on the next command to any
1847 ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
1855 if (aic7xxx_no_reset != 0)
1856 ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
1858 if ((ahc->flags & AHC_RESET_BUS_A) != 0)
1859 ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
1861 numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1863 if ((ahc->features & AHC_TWIN) != 0) {
1865 if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
1866 ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
1875 * Force negotiation to async for all targets that
1876 * will not see an initial bus reset.
1878 for (; i < numtarg; i++) {
1879 struct ahc_devinfo devinfo;
1880 struct ahc_initiator_tinfo *tinfo;
1881 struct ahc_tmode_tstate *tstate;
1887 our_id = ahc->our_id;
1889 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
1891 our_id = ahc->our_id_b;
1894 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
1895 target_id, &tstate);
1896 ahc_compile_devinfo(&devinfo, our_id, target_id,
1897 CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
1898 ahc_update_neg_request(ahc, &devinfo, tstate,
1899 tinfo, AHC_NEG_ALWAYS);
1901 /* Give the bus some time to recover */
1902 if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
1903 ahc_linux_freeze_simq(ahc);
1904 init_timer(&ahc->platform_data->reset_timer);
1905 ahc->platform_data->reset_timer.data = (u_long)ahc;
1906 ahc->platform_data->reset_timer.expires =
1907 jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000;
1908 ahc->platform_data->reset_timer.function =
1909 ahc_linux_release_simq;
1910 add_timer(&ahc->platform_data->reset_timer);
1915 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1918 ahc->platform_data =
1919 malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
1920 if (ahc->platform_data == NULL)
1922 memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
1923 TAILQ_INIT(&ahc->platform_data->completeq);
1924 TAILQ_INIT(&ahc->platform_data->device_runq);
1925 ahc->platform_data->irq = AHC_LINUX_NOIRQ;
1926 ahc->platform_data->hw_dma_mask = 0xFFFFFFFF;
1928 ahc_done_lockinit(ahc);
1929 init_timer(&ahc->platform_data->completeq_timer);
1930 ahc->platform_data->completeq_timer.data = (u_long)ahc;
1931 ahc->platform_data->completeq_timer.function =
1932 (ahc_linux_callback_t *)ahc_linux_thread_run_complete_queue;
1933 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1934 init_MUTEX_LOCKED(&ahc->platform_data->eh_sem);
1935 init_MUTEX_LOCKED(&ahc->platform_data->dv_sem);
1936 init_MUTEX_LOCKED(&ahc->platform_data->dv_cmd_sem);
1938 ahc->platform_data->eh_sem = MUTEX_LOCKED;
1939 ahc->platform_data->dv_sem = MUTEX_LOCKED;
1940 ahc->platform_data->dv_cmd_sem = MUTEX_LOCKED;
1942 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1943 tasklet_init(&ahc->platform_data->runq_tasklet, ahc_runq_tasklet,
1944 (unsigned long)ahc);
1946 ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
1947 ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
1948 if (aic7xxx_pci_parity == 0)
1949 ahc->flags |= AHC_DISABLE_PCI_PERR;
1955 ahc_platform_free(struct ahc_softc *ahc)
1957 struct ahc_linux_target *targ;
1958 struct ahc_linux_device *dev;
1961 if (ahc->platform_data != NULL) {
1962 del_timer_sync(&ahc->platform_data->completeq_timer);
1963 ahc_linux_kill_dv_thread(ahc);
1964 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1965 tasklet_kill(&ahc->platform_data->runq_tasklet);
1967 if (ahc->platform_data->host != NULL) {
1968 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1969 scsi_remove_host(ahc->platform_data->host);
1971 scsi_host_put(ahc->platform_data->host);
1974 /* destroy all of the device and target objects */
1975 for (i = 0; i < AHC_NUM_TARGETS; i++) {
1976 targ = ahc->platform_data->targets[i];
1978 /* Keep target around through the loop. */
1980 for (j = 0; j < AHC_NUM_LUNS; j++) {
1982 if (targ->devices[j] == NULL)
1984 dev = targ->devices[j];
1985 ahc_linux_free_device(ahc, dev);
1988 * Forcibly free the target now that
1989 * all devices are gone.
1991 ahc_linux_free_target(ahc, targ);
1995 if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
1996 free_irq(ahc->platform_data->irq, ahc);
1997 if (ahc->tag == BUS_SPACE_PIO
1998 && ahc->bsh.ioport != 0)
1999 release_region(ahc->bsh.ioport, 256);
2000 if (ahc->tag == BUS_SPACE_MEMIO
2001 && ahc->bsh.maddr != NULL) {
2004 base_addr = (u_long)ahc->bsh.maddr;
2005 base_addr &= PAGE_MASK;
2006 iounmap((void *)base_addr);
2007 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
2008 release_mem_region(ahc->platform_data->mem_busaddr,
2012 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0) && \
2013 LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2015 * In 2.4 we detach from the scsi midlayer before the PCI
2016 * layer invokes our remove callback. No per-instance
2017 * detach is provided, so we must reach inside the PCI
2018 * subsystem's internals and detach our driver manually.
2020 if (ahc->dev_softc != NULL)
2021 ahc->dev_softc->driver = NULL;
2023 free(ahc->platform_data, M_DEVBUF);
2028 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
2030 ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
2031 SCB_GET_CHANNEL(ahc, scb),
2032 SCB_GET_LUN(scb), SCB_LIST_NULL,
2033 ROLE_UNKNOWN, CAM_REQUEUE_REQ);
2037 ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
2040 struct ahc_linux_device *dev;
2044 dev = ahc_linux_get_device(ahc, devinfo->channel - 'A',
2046 devinfo->lun, /*alloc*/FALSE);
2049 was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
2052 case AHC_QUEUE_NONE:
2055 case AHC_QUEUE_BASIC:
2056 now_queuing = AHC_DEV_Q_BASIC;
2058 case AHC_QUEUE_TAGGED:
2059 now_queuing = AHC_DEV_Q_TAGGED;
2062 if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
2063 && (was_queuing != now_queuing)
2064 && (dev->active != 0)) {
2065 dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
2069 dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
2073 usertags = ahc_linux_user_tagdepth(ahc, devinfo);
2076 * Start out agressively and allow our
2077 * dynamic queue depth algorithm to take
2080 dev->maxtags = usertags;
2081 dev->openings = dev->maxtags - dev->active;
2083 if (dev->maxtags == 0) {
2085 * Queueing is disabled by the user.
2088 } else if (alg == AHC_QUEUE_TAGGED) {
2089 dev->flags |= AHC_DEV_Q_TAGGED;
2090 if (aic7xxx_periodic_otag != 0)
2091 dev->flags |= AHC_DEV_PERIODIC_OTAG;
2093 dev->flags |= AHC_DEV_Q_BASIC;
2095 /* We can only have one opening. */
2097 dev->openings = 1 - dev->active;
2099 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
2100 if (dev->scsi_device != NULL) {
2101 switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
2102 case AHC_DEV_Q_BASIC:
2103 scsi_adjust_queue_depth(dev->scsi_device,
2105 dev->openings + dev->active);
2107 case AHC_DEV_Q_TAGGED:
2108 scsi_adjust_queue_depth(dev->scsi_device,
2110 dev->openings + dev->active);
2114 * We allow the OS to queue 2 untagged transactions to
2115 * us at any time even though we can only execute them
2116 * serially on the controller/device. This should
2117 * remove some latency.
2119 scsi_adjust_queue_depth(dev->scsi_device,
2129 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
2130 int lun, u_int tag, role_t role, uint32_t status)
2140 if (tag != SCB_LIST_NULL)
2144 if (channel != ALL_CHANNELS) {
2145 chan = channel - 'A';
2148 maxchan = (ahc->features & AHC_TWIN) ? 2 : 1;
2151 if (target != CAM_TARGET_WILDCARD) {
2155 maxtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
2158 if (lun != CAM_LUN_WILDCARD) {
2162 maxlun = AHC_NUM_LUNS;
2166 for (; chan < maxchan; chan++) {
2168 for (; targ < maxtarg; targ++) {
2170 for (; clun < maxlun; clun++) {
2171 struct ahc_linux_device *dev;
2172 struct ahc_busyq *busyq;
2173 struct ahc_cmd *acmd;
2175 dev = ahc_linux_get_device(ahc, chan,
2181 busyq = &dev->busyq;
2182 while ((acmd = TAILQ_FIRST(busyq)) != NULL) {
2185 cmd = &acmd_scsi_cmd(acmd);
2186 TAILQ_REMOVE(busyq, acmd,
2189 cmd->result = status << 16;
2190 ahc_linux_queue_cmd_complete(ahc, cmd);
2200 ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc)
2204 ahc_lock(ahc, &flags);
2205 del_timer(&ahc->platform_data->completeq_timer);
2206 ahc->platform_data->flags &= ~AHC_RUN_CMPLT_Q_TIMER;
2207 ahc_linux_run_complete_queue(ahc);
2208 ahc_unlock(ahc, &flags);
2212 ahc_linux_start_dv(struct ahc_softc *ahc)
2216 * Freeze the simq and signal ahc_linux_queue to not let any
2217 * more commands through.
2219 if ((ahc->platform_data->flags & AHC_DV_ACTIVE) == 0) {
2221 if (ahc_debug & AHC_SHOW_DV)
2222 printf("%s: Waking DV thread\n", ahc_name(ahc));
2225 ahc->platform_data->flags |= AHC_DV_ACTIVE;
2226 ahc_linux_freeze_simq(ahc);
2228 /* Wake up the DV kthread */
2229 up(&ahc->platform_data->dv_sem);
2234 ahc_linux_kill_dv_thread(struct ahc_softc *ahc)
2239 if (ahc->platform_data->dv_pid != 0) {
2240 ahc->platform_data->flags |= AHC_DV_SHUTDOWN;
2241 ahc_unlock(ahc, &s);
2242 up(&ahc->platform_data->dv_sem);
2245 * Use the eh_sem as an indicator that the
2246 * dv thread is exiting. Note that the dv
2247 * thread must still return after performing
2248 * the up on our semaphore before it has
2249 * completely exited this module. Unfortunately,
2250 * there seems to be no easy way to wait for the
2251 * exit of a thread for which you are not the
2252 * parent (dv threads are parented by init).
2253 * Cross your fingers...
2255 down(&ahc->platform_data->eh_sem);
2258 * Mark the dv thread as already dead. This
2259 * avoids attempting to kill it a second time.
2260 * This is necessary because we must kill the
2261 * DV thread before calling ahc_free() in the
2262 * module shutdown case to avoid bogus locking
2263 * in the SCSI mid-layer, but we ahc_free() is
2264 * called without killing the DV thread in the
2265 * instance detach case, so ahc_platform_free()
2266 * calls us again to verify that the DV thread
2269 ahc->platform_data->dv_pid = 0;
2271 ahc_unlock(ahc, &s);
2276 ahc_linux_dv_thread(void *data)
2278 struct ahc_softc *ahc;
2282 ahc = (struct ahc_softc *)data;
2285 if (ahc_debug & AHC_SHOW_DV)
2286 printf("Launching DV Thread\n");
2290 * Complete thread creation.
2293 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2295 * Don't care about any signals.
2297 siginitsetinv(¤t->blocked, 0);
2300 sprintf(current->comm, "ahc_dv_%d", ahc->unit);
2302 daemonize("ahc_dv_%d", ahc->unit);
2303 current->flags |= PF_FREEZE;
2309 * Use down_interruptible() rather than down() to
2310 * avoid inclusion in the load average.
2312 down_interruptible(&ahc->platform_data->dv_sem);
2314 /* Check to see if we've been signaled to exit */
2316 if ((ahc->platform_data->flags & AHC_DV_SHUTDOWN) != 0) {
2317 ahc_unlock(ahc, &s);
2320 ahc_unlock(ahc, &s);
2323 if (ahc_debug & AHC_SHOW_DV)
2324 printf("%s: Beginning Domain Validation\n",
2329 * Wait for any pending commands to drain before proceeding.
2332 while (LIST_FIRST(&ahc->pending_scbs) != NULL) {
2333 ahc->platform_data->flags |= AHC_DV_WAIT_SIMQ_EMPTY;
2334 ahc_unlock(ahc, &s);
2335 down_interruptible(&ahc->platform_data->dv_sem);
2340 * Wait for the SIMQ to be released so that DV is the
2341 * only reason the queue is frozen.
2343 while (AHC_DV_SIMQ_FROZEN(ahc) == 0) {
2344 ahc->platform_data->flags |= AHC_DV_WAIT_SIMQ_RELEASE;
2345 ahc_unlock(ahc, &s);
2346 down_interruptible(&ahc->platform_data->dv_sem);
2349 ahc_unlock(ahc, &s);
2351 for (target = 0; target < AHC_NUM_TARGETS; target++)
2352 ahc_linux_dv_target(ahc, target);
2355 ahc->platform_data->flags &= ~AHC_DV_ACTIVE;
2356 ahc_unlock(ahc, &s);
2359 * Release the SIMQ so that normal commands are
2360 * allowed to continue on the bus.
2362 ahc_linux_release_simq((u_long)ahc);
2364 up(&ahc->platform_data->eh_sem);
2368 #define AHC_LINUX_DV_INQ_SHORT_LEN 36
2369 #define AHC_LINUX_DV_INQ_LEN 256
2370 #define AHC_LINUX_DV_TIMEOUT (HZ / 4)
2372 #define AHC_SET_DV_STATE(ahc, targ, newstate) \
2373 ahc_set_dv_state(ahc, targ, newstate, __LINE__)
2375 static __inline void
2376 ahc_set_dv_state(struct ahc_softc *ahc, struct ahc_linux_target *targ,
2377 ahc_dv_state newstate, u_int line)
2379 ahc_dv_state oldstate;
2381 oldstate = targ->dv_state;
2383 if (ahc_debug & AHC_SHOW_DV)
2384 printf("%s:%d: Going from state %d to state %d\n",
2385 ahc_name(ahc), line, oldstate, newstate);
2388 if (oldstate == newstate)
2389 targ->dv_state_retry++;
2391 targ->dv_state_retry = 0;
2392 targ->dv_state = newstate;
2396 ahc_linux_dv_target(struct ahc_softc *ahc, u_int target_offset)
2398 struct ahc_devinfo devinfo;
2399 struct ahc_linux_target *targ;
2400 struct scsi_cmnd *cmd;
2401 struct scsi_device *scsi_dev;
2402 struct scsi_sense_data *sense;
2412 targ = ahc->platform_data->targets[target_offset];
2413 if (targ == NULL || (targ->flags & AHC_DV_REQUIRED) == 0) {
2414 ahc_unlock(ahc, &s);
2417 ahc_compile_devinfo(&devinfo,
2418 targ->channel == 0 ? ahc->our_id : ahc->our_id_b,
2419 targ->target, /*lun*/0, targ->channel + 'A',
2422 if (ahc_debug & AHC_SHOW_DV) {
2423 ahc_print_devinfo(ahc, &devinfo);
2424 printf("Performing DV\n");
2428 ahc_unlock(ahc, &s);
2430 cmd = malloc(sizeof(struct scsi_cmnd), M_DEVBUF, M_WAITOK);
2431 scsi_dev = malloc(sizeof(struct scsi_device), M_DEVBUF, M_WAITOK);
2432 scsi_dev->host = ahc->platform_data->host;
2433 scsi_dev->id = devinfo.target;
2434 scsi_dev->lun = devinfo.lun;
2435 scsi_dev->channel = devinfo.channel - 'A';
2436 ahc->platform_data->dv_scsi_dev = scsi_dev;
2438 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_INQ_SHORT_ASYNC);
2440 while (targ->dv_state != AHC_DV_STATE_EXIT) {
2441 timeout = AHC_LINUX_DV_TIMEOUT;
2442 switch (targ->dv_state) {
2443 case AHC_DV_STATE_INQ_SHORT_ASYNC:
2444 case AHC_DV_STATE_INQ_ASYNC:
2445 case AHC_DV_STATE_INQ_ASYNC_VERIFY:
2447 * Set things to async narrow to reduce the
2448 * chance that the INQ will fail.
2451 ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
2452 AHC_TRANS_GOAL, /*paused*/FALSE);
2453 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
2454 AHC_TRANS_GOAL, /*paused*/FALSE);
2455 ahc_unlock(ahc, &s);
2457 targ->flags &= ~AHC_INQ_VALID;
2459 case AHC_DV_STATE_INQ_VERIFY:
2463 if (targ->dv_state == AHC_DV_STATE_INQ_SHORT_ASYNC)
2464 inq_len = AHC_LINUX_DV_INQ_SHORT_LEN;
2466 inq_len = targ->inq_data->additional_length + 5;
2467 ahc_linux_dv_inq(ahc, cmd, &devinfo, targ, inq_len);
2470 case AHC_DV_STATE_TUR:
2471 case AHC_DV_STATE_BUSY:
2473 ahc_linux_dv_tur(ahc, cmd, &devinfo);
2475 case AHC_DV_STATE_REBD:
2476 ahc_linux_dv_rebd(ahc, cmd, &devinfo, targ);
2478 case AHC_DV_STATE_WEB:
2479 ahc_linux_dv_web(ahc, cmd, &devinfo, targ);
2482 case AHC_DV_STATE_REB:
2483 ahc_linux_dv_reb(ahc, cmd, &devinfo, targ);
2486 case AHC_DV_STATE_SU:
2487 ahc_linux_dv_su(ahc, cmd, &devinfo, targ);
2492 ahc_print_devinfo(ahc, &devinfo);
2493 printf("Unknown DV state %d\n", targ->dv_state);
2497 /* Queue the command and wait for it to complete */
2498 /* Abuse eh_timeout in the scsi_cmnd struct for our purposes */
2499 init_timer(&cmd->eh_timeout);
2501 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2503 * All of the printfs during negotiation
2504 * really slow down the negotiation.
2505 * Add a bit of time just to be safe.
2509 scsi_add_timer(cmd, timeout, ahc_linux_dv_timeout);
2511 * In 2.5.X, it is assumed that all calls from the
2512 * "midlayer" (which we are emulating) will have the
2513 * ahc host lock held. For other kernels, the
2514 * io_request_lock must be held.
2516 #if AHC_SCSI_HAS_HOST_LOCK != 0
2519 spin_lock_irqsave(&io_request_lock, s);
2521 ahc_linux_queue(cmd, ahc_linux_dv_complete);
2522 #if AHC_SCSI_HAS_HOST_LOCK != 0
2523 ahc_unlock(ahc, &s);
2525 spin_unlock_irqrestore(&io_request_lock, s);
2527 down_interruptible(&ahc->platform_data->dv_cmd_sem);
2529 * Wait for the SIMQ to be released so that DV is the
2530 * only reason the queue is frozen.
2533 while (AHC_DV_SIMQ_FROZEN(ahc) == 0) {
2534 ahc->platform_data->flags |= AHC_DV_WAIT_SIMQ_RELEASE;
2535 ahc_unlock(ahc, &s);
2536 down_interruptible(&ahc->platform_data->dv_sem);
2539 ahc_unlock(ahc, &s);
2541 ahc_linux_dv_transition(ahc, cmd, &devinfo, targ);
2545 if ((targ->flags & AHC_INQ_VALID) != 0
2546 && ahc_linux_get_device(ahc, devinfo.channel - 'A',
2547 devinfo.target, devinfo.lun,
2548 /*alloc*/FALSE) == NULL) {
2550 * The DV state machine failed to configure this device.
2551 * This is normal if DV is disabled. Since we have inquiry
2552 * data, filter it and use the "optimistic" negotiation
2553 * parameters found in the inquiry string.
2555 ahc_linux_filter_inquiry(ahc, &devinfo);
2556 if ((targ->flags & (AHC_BASIC_DV|AHC_ENHANCED_DV)) != 0) {
2557 ahc_print_devinfo(ahc, &devinfo);
2558 printf("DV failed to configure device. "
2559 "Please file a bug report against "
2565 free(cmd, M_DEVBUF);
2567 if (ahc->platform_data->dv_scsi_dev != NULL) {
2568 free(ahc->platform_data->dv_scsi_dev, M_DEVBUF);
2569 ahc->platform_data->dv_scsi_dev = NULL;
2573 if (targ->dv_buffer != NULL) {
2574 free(targ->dv_buffer, M_DEVBUF);
2575 targ->dv_buffer = NULL;
2577 if (targ->dv_buffer1 != NULL) {
2578 free(targ->dv_buffer1, M_DEVBUF);
2579 targ->dv_buffer1 = NULL;
2581 targ->flags &= ~AHC_DV_REQUIRED;
2582 if (targ->refcount == 0)
2583 ahc_linux_free_target(ahc, targ);
2584 ahc_unlock(ahc, &s);
2588 ahc_linux_dv_transition(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
2589 struct ahc_devinfo *devinfo,
2590 struct ahc_linux_target *targ)
2594 status = aic_error_action(cmd, targ->inq_data,
2595 ahc_cmd_get_transaction_status(cmd),
2596 ahc_cmd_get_scsi_status(cmd));
2599 if (ahc_debug & AHC_SHOW_DV) {
2600 ahc_print_devinfo(ahc, devinfo);
2601 printf("Entering ahc_linux_dv_transition, state= %d, "
2602 "status= 0x%x, cmd->result= 0x%x\n", targ->dv_state,
2603 status, cmd->result);
2607 switch (targ->dv_state) {
2608 case AHC_DV_STATE_INQ_SHORT_ASYNC:
2609 case AHC_DV_STATE_INQ_ASYNC:
2610 switch (status & SS_MASK) {
2613 AHC_SET_DV_STATE(ahc, targ, targ->dv_state+1);
2616 case SS_INQ_REFRESH:
2617 AHC_SET_DV_STATE(ahc, targ,
2618 AHC_DV_STATE_INQ_SHORT_ASYNC);
2622 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2623 if (ahc_cmd_get_transaction_status(cmd)
2625 targ->dv_state_retry--;
2626 if ((status & SS_ERRMASK) == EBUSY)
2627 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2628 if (targ->dv_state_retry < 10)
2632 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2634 if (ahc_debug & AHC_SHOW_DV) {
2635 ahc_print_devinfo(ahc, devinfo);
2636 printf("Failed DV inquiry, skipping\n");
2642 case AHC_DV_STATE_INQ_ASYNC_VERIFY:
2643 switch (status & SS_MASK) {
2649 if (memcmp(targ->inq_data, targ->dv_buffer,
2650 AHC_LINUX_DV_INQ_LEN) != 0) {
2652 * Inquiry data must have changed.
2653 * Try from the top again.
2655 AHC_SET_DV_STATE(ahc, targ,
2656 AHC_DV_STATE_INQ_SHORT_ASYNC);
2660 AHC_SET_DV_STATE(ahc, targ, targ->dv_state+1);
2661 targ->flags |= AHC_INQ_VALID;
2662 if (ahc_linux_user_dv_setting(ahc) == 0)
2665 xportflags = targ->inq_data->flags;
2666 if ((xportflags & (SID_Sync|SID_WBus16)) == 0)
2669 spi3data = targ->inq_data->spi3data;
2670 switch (spi3data & SID_SPI_CLOCK_DT_ST) {
2672 case SID_SPI_CLOCK_ST:
2673 /* Assume only basic DV is supported. */
2674 targ->flags |= AHC_BASIC_DV;
2676 case SID_SPI_CLOCK_DT:
2677 case SID_SPI_CLOCK_DT_ST:
2678 targ->flags |= AHC_ENHANCED_DV;
2683 case SS_INQ_REFRESH:
2684 AHC_SET_DV_STATE(ahc, targ,
2685 AHC_DV_STATE_INQ_SHORT_ASYNC);
2689 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2690 if (ahc_cmd_get_transaction_status(cmd)
2692 targ->dv_state_retry--;
2694 if ((status & SS_ERRMASK) == EBUSY)
2695 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2696 if (targ->dv_state_retry < 10)
2700 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2702 if (ahc_debug & AHC_SHOW_DV) {
2703 ahc_print_devinfo(ahc, devinfo);
2704 printf("Failed DV inquiry, skipping\n");
2710 case AHC_DV_STATE_INQ_VERIFY:
2711 switch (status & SS_MASK) {
2715 if (memcmp(targ->inq_data, targ->dv_buffer,
2716 AHC_LINUX_DV_INQ_LEN) == 0) {
2717 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2721 if (ahc_debug & AHC_SHOW_DV) {
2724 ahc_print_devinfo(ahc, devinfo);
2725 printf("Inquiry buffer mismatch:");
2726 for (i = 0; i < AHC_LINUX_DV_INQ_LEN; i++) {
2729 printf("0x%x:0x0%x ",
2730 ((uint8_t *)targ->inq_data)[i],
2731 targ->dv_buffer[i]);
2737 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2738 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2742 * Do not count "falling back"
2743 * against our retries.
2745 targ->dv_state_retry = 0;
2746 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2749 case SS_INQ_REFRESH:
2750 AHC_SET_DV_STATE(ahc, targ,
2751 AHC_DV_STATE_INQ_SHORT_ASYNC);
2755 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2756 if (ahc_cmd_get_transaction_status(cmd)
2757 == CAM_REQUEUE_REQ) {
2758 targ->dv_state_retry--;
2759 } else if ((status & SSQ_FALLBACK) != 0) {
2760 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2761 AHC_SET_DV_STATE(ahc, targ,
2766 * Do not count "falling back"
2767 * against our retries.
2769 targ->dv_state_retry = 0;
2770 } else if ((status & SS_ERRMASK) == EBUSY)
2771 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2772 if (targ->dv_state_retry < 10)
2776 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2778 if (ahc_debug & AHC_SHOW_DV) {
2779 ahc_print_devinfo(ahc, devinfo);
2780 printf("Failed DV inquiry, skipping\n");
2787 case AHC_DV_STATE_TUR:
2788 switch (status & SS_MASK) {
2790 if ((targ->flags & AHC_BASIC_DV) != 0) {
2791 ahc_linux_filter_inquiry(ahc, devinfo);
2792 AHC_SET_DV_STATE(ahc, targ,
2793 AHC_DV_STATE_INQ_VERIFY);
2794 } else if ((targ->flags & AHC_ENHANCED_DV) != 0) {
2795 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_REBD);
2797 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2802 if ((status & SS_ERRMASK) == EBUSY) {
2803 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2806 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2807 if (ahc_cmd_get_transaction_status(cmd)
2808 == CAM_REQUEUE_REQ) {
2809 targ->dv_state_retry--;
2810 } else if ((status & SSQ_FALLBACK) != 0) {
2811 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2812 AHC_SET_DV_STATE(ahc, targ,
2817 * Do not count "falling back"
2818 * against our retries.
2820 targ->dv_state_retry = 0;
2822 if (targ->dv_state_retry >= 10) {
2824 if (ahc_debug & AHC_SHOW_DV) {
2825 ahc_print_devinfo(ahc, devinfo);
2826 printf("DV TUR reties exhausted\n");
2829 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2832 if (status & SSQ_DELAY)
2837 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_SU);
2839 case SS_INQ_REFRESH:
2840 AHC_SET_DV_STATE(ahc, targ,
2841 AHC_DV_STATE_INQ_SHORT_ASYNC);
2844 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2849 case AHC_DV_STATE_REBD:
2850 switch (status & SS_MASK) {
2855 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_WEB);
2856 echo_size = scsi_3btoul(&targ->dv_buffer[1]);
2857 echo_size &= 0x1FFF;
2859 if (ahc_debug & AHC_SHOW_DV) {
2860 ahc_print_devinfo(ahc, devinfo);
2861 printf("Echo buffer size= %d\n", echo_size);
2864 if (echo_size == 0) {
2865 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2869 /* Generate the buffer pattern */
2870 targ->dv_echo_size = echo_size;
2871 ahc_linux_generate_dv_pattern(targ);
2873 * Setup initial negotiation values.
2875 ahc_linux_filter_inquiry(ahc, devinfo);
2878 case SS_INQ_REFRESH:
2879 AHC_SET_DV_STATE(ahc, targ,
2880 AHC_DV_STATE_INQ_SHORT_ASYNC);
2883 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2884 if (ahc_cmd_get_transaction_status(cmd)
2886 targ->dv_state_retry--;
2887 if (targ->dv_state_retry <= 10)
2890 if (ahc_debug & AHC_SHOW_DV) {
2891 ahc_print_devinfo(ahc, devinfo);
2892 printf("DV REBD reties exhausted\n");
2899 * Setup initial negotiation values
2900 * and try level 1 DV.
2902 ahc_linux_filter_inquiry(ahc, devinfo);
2903 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_INQ_VERIFY);
2904 targ->dv_echo_size = 0;
2909 case AHC_DV_STATE_WEB:
2910 switch (status & SS_MASK) {
2912 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_REB);
2914 case SS_INQ_REFRESH:
2915 AHC_SET_DV_STATE(ahc, targ,
2916 AHC_DV_STATE_INQ_SHORT_ASYNC);
2919 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2920 if (ahc_cmd_get_transaction_status(cmd)
2921 == CAM_REQUEUE_REQ) {
2922 targ->dv_state_retry--;
2923 } else if ((status & SSQ_FALLBACK) != 0) {
2924 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2925 AHC_SET_DV_STATE(ahc, targ,
2930 * Do not count "falling back"
2931 * against our retries.
2933 targ->dv_state_retry = 0;
2935 if (targ->dv_state_retry <= 10)
2939 if (ahc_debug & AHC_SHOW_DV) {
2940 ahc_print_devinfo(ahc, devinfo);
2941 printf("DV WEB reties exhausted\n");
2945 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2950 case AHC_DV_STATE_REB:
2951 switch (status & SS_MASK) {
2953 if (memcmp(targ->dv_buffer, targ->dv_buffer1,
2954 targ->dv_echo_size) != 0) {
2955 if (ahc_linux_fallback(ahc, devinfo) != 0)
2956 AHC_SET_DV_STATE(ahc, targ,
2959 AHC_SET_DV_STATE(ahc, targ,
2964 if (targ->dv_buffer != NULL) {
2965 free(targ->dv_buffer, M_DEVBUF);
2966 targ->dv_buffer = NULL;
2968 if (targ->dv_buffer1 != NULL) {
2969 free(targ->dv_buffer1, M_DEVBUF);
2970 targ->dv_buffer1 = NULL;
2972 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2974 case SS_INQ_REFRESH:
2975 AHC_SET_DV_STATE(ahc, targ,
2976 AHC_DV_STATE_INQ_SHORT_ASYNC);
2979 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2980 if (ahc_cmd_get_transaction_status(cmd)
2981 == CAM_REQUEUE_REQ) {
2982 targ->dv_state_retry--;
2983 } else if ((status & SSQ_FALLBACK) != 0) {
2984 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2985 AHC_SET_DV_STATE(ahc, targ,
2989 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_WEB);
2991 if (targ->dv_state_retry <= 10) {
2992 if ((status & (SSQ_DELAY_RANDOM|SSQ_DELAY))!= 0)
2993 scsi_sleep(ahc->our_id*HZ/10);
2997 if (ahc_debug & AHC_SHOW_DV) {
2998 ahc_print_devinfo(ahc, devinfo);
2999 printf("DV REB reties exhausted\n");
3004 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
3009 case AHC_DV_STATE_SU:
3010 switch (status & SS_MASK) {
3012 case SS_INQ_REFRESH:
3013 AHC_SET_DV_STATE(ahc, targ,
3014 AHC_DV_STATE_INQ_SHORT_ASYNC);
3017 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
3022 case AHC_DV_STATE_BUSY:
3023 switch (status & SS_MASK) {
3025 case SS_INQ_REFRESH:
3026 AHC_SET_DV_STATE(ahc, targ,
3027 AHC_DV_STATE_INQ_SHORT_ASYNC);
3031 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
3032 if (ahc_cmd_get_transaction_status(cmd)
3033 == CAM_REQUEUE_REQ) {
3034 targ->dv_state_retry--;
3035 } else if (targ->dv_state_retry < 60) {
3036 if ((status & SSQ_DELAY) != 0)
3040 if (ahc_debug & AHC_SHOW_DV) {
3041 ahc_print_devinfo(ahc, devinfo);
3042 printf("DV BUSY reties exhausted\n");
3045 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
3049 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
3055 printf("%s: Invalid DV completion state %d\n", ahc_name(ahc),
3057 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
3063 ahc_linux_dv_fill_cmd(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3064 struct ahc_devinfo *devinfo)
3066 memset(cmd, 0, sizeof(struct scsi_cmnd));
3067 cmd->device = ahc->platform_data->dv_scsi_dev;
3068 cmd->scsi_done = ahc_linux_dv_complete;
3072 * Synthesize an inquiry command. On the return trip, it'll be
3073 * sniffed and the device transfer settings set for us.
3076 ahc_linux_dv_inq(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3077 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ,
3078 u_int request_length)
3082 if (ahc_debug & AHC_SHOW_DV) {
3083 ahc_print_devinfo(ahc, devinfo);
3084 printf("Sending INQ\n");
3087 if (targ->inq_data == NULL)
3088 targ->inq_data = malloc(AHC_LINUX_DV_INQ_LEN,
3089 M_DEVBUF, M_WAITOK);
3090 if (targ->dv_state > AHC_DV_STATE_INQ_ASYNC) {
3091 if (targ->dv_buffer != NULL)
3092 free(targ->dv_buffer, M_DEVBUF);
3093 targ->dv_buffer = malloc(AHC_LINUX_DV_INQ_LEN,
3094 M_DEVBUF, M_WAITOK);
3097 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3098 cmd->sc_data_direction = SCSI_DATA_READ;
3100 cmd->cmnd[0] = INQUIRY;
3101 cmd->cmnd[4] = request_length;
3102 cmd->request_bufflen = request_length;
3103 if (targ->dv_state > AHC_DV_STATE_INQ_ASYNC)
3104 cmd->request_buffer = targ->dv_buffer;
3106 cmd->request_buffer = targ->inq_data;
3107 memset(cmd->request_buffer, 0, AHC_LINUX_DV_INQ_LEN);
3111 ahc_linux_dv_tur(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3112 struct ahc_devinfo *devinfo)
3116 if (ahc_debug & AHC_SHOW_DV) {
3117 ahc_print_devinfo(ahc, devinfo);
3118 printf("Sending TUR\n");
3121 /* Do a TUR to clear out any non-fatal transitional state */
3122 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3123 cmd->sc_data_direction = SCSI_DATA_NONE;
3125 cmd->cmnd[0] = TEST_UNIT_READY;
3128 #define AHC_REBD_LEN 4
3131 ahc_linux_dv_rebd(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3132 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ)
3136 if (ahc_debug & AHC_SHOW_DV) {
3137 ahc_print_devinfo(ahc, devinfo);
3138 printf("Sending REBD\n");
3141 if (targ->dv_buffer != NULL)
3142 free(targ->dv_buffer, M_DEVBUF);
3143 targ->dv_buffer = malloc(AHC_REBD_LEN, M_DEVBUF, M_WAITOK);
3144 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3145 cmd->sc_data_direction = SCSI_DATA_READ;
3147 cmd->cmnd[0] = READ_BUFFER;
3148 cmd->cmnd[1] = 0x0b;
3149 scsi_ulto3b(AHC_REBD_LEN, &cmd->cmnd[6]);
3150 cmd->request_bufflen = AHC_REBD_LEN;
3151 cmd->underflow = cmd->request_bufflen;
3152 cmd->request_buffer = targ->dv_buffer;
3156 ahc_linux_dv_web(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3157 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ)
3161 if (ahc_debug & AHC_SHOW_DV) {
3162 ahc_print_devinfo(ahc, devinfo);
3163 printf("Sending WEB\n");
3166 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3167 cmd->sc_data_direction = SCSI_DATA_WRITE;
3169 cmd->cmnd[0] = WRITE_BUFFER;
3170 cmd->cmnd[1] = 0x0a;
3171 scsi_ulto3b(targ->dv_echo_size, &cmd->cmnd[6]);
3172 cmd->request_bufflen = targ->dv_echo_size;
3173 cmd->underflow = cmd->request_bufflen;
3174 cmd->request_buffer = targ->dv_buffer;
3178 ahc_linux_dv_reb(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3179 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ)
3183 if (ahc_debug & AHC_SHOW_DV) {
3184 ahc_print_devinfo(ahc, devinfo);
3185 printf("Sending REB\n");
3188 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3189 cmd->sc_data_direction = SCSI_DATA_READ;
3191 cmd->cmnd[0] = READ_BUFFER;
3192 cmd->cmnd[1] = 0x0a;
3193 scsi_ulto3b(targ->dv_echo_size, &cmd->cmnd[6]);
3194 cmd->request_bufflen = targ->dv_echo_size;
3195 cmd->underflow = cmd->request_bufflen;
3196 cmd->request_buffer = targ->dv_buffer1;
3200 ahc_linux_dv_su(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3201 struct ahc_devinfo *devinfo,
3202 struct ahc_linux_target *targ)
3206 le = SID_IS_REMOVABLE(targ->inq_data) ? SSS_LOEJ : 0;
3209 if (ahc_debug & AHC_SHOW_DV) {
3210 ahc_print_devinfo(ahc, devinfo);
3211 printf("Sending SU\n");
3214 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3215 cmd->sc_data_direction = SCSI_DATA_NONE;
3217 cmd->cmnd[0] = START_STOP_UNIT;
3218 cmd->cmnd[4] = le | SSS_START;
3222 ahc_linux_fallback(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3224 struct ahc_linux_target *targ;
3225 struct ahc_initiator_tinfo *tinfo;
3226 struct ahc_transinfo *goal;
3227 struct ahc_tmode_tstate *tstate;
3228 struct ahc_syncrate *syncrate;
3237 u_int fallback_speed;
3240 if (ahc_debug & AHC_SHOW_DV) {
3241 ahc_print_devinfo(ahc, devinfo);
3242 printf("Trying to fallback\n");
3246 targ = ahc->platform_data->targets[devinfo->target_offset];
3247 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
3248 devinfo->our_scsiid,
3249 devinfo->target, &tstate);
3250 goal = &tinfo->goal;
3251 width = goal->width;
3252 period = goal->period;
3253 offset = goal->offset;
3254 ppr_options = goal->ppr_options;
3256 period = AHC_ASYNC_XFER_PERIOD;
3257 if (targ->dv_next_narrow_period == 0)
3258 targ->dv_next_narrow_period = MAX(period, AHC_SYNCRATE_ULTRA2);
3259 if (targ->dv_next_wide_period == 0)
3260 targ->dv_next_wide_period = period;
3261 if (targ->dv_max_width == 0)
3262 targ->dv_max_width = width;
3263 if (targ->dv_max_ppr_options == 0)
3264 targ->dv_max_ppr_options = ppr_options;
3265 if (targ->dv_last_ppr_options == 0)
3266 targ->dv_last_ppr_options = ppr_options;
3268 cur_speed = aic_calc_speed(width, period, offset, AHC_SYNCRATE_MIN);
3269 wide_speed = aic_calc_speed(MSG_EXT_WDTR_BUS_16_BIT,
3270 targ->dv_next_wide_period,
3273 narrow_speed = aic_calc_speed(MSG_EXT_WDTR_BUS_8_BIT,
3274 targ->dv_next_narrow_period,
3277 fallback_speed = aic_calc_speed(width, period+1, offset,
3280 if (ahc_debug & AHC_SHOW_DV) {
3281 printf("cur_speed= %d, wide_speed= %d, narrow_speed= %d, "
3282 "fallback_speed= %d\n", cur_speed, wide_speed,
3283 narrow_speed, fallback_speed);
3287 if (cur_speed > 160000) {
3289 * Paced/DT/IU_REQ only transfer speeds. All we
3290 * can do is fallback in terms of syncrate.
3293 } else if (cur_speed > 80000) {
3294 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3296 * Try without IU_REQ as it may be confusing
3299 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
3302 * Paced/DT only transfer speeds. All we
3303 * can do is fallback in terms of syncrate.
3306 ppr_options = targ->dv_max_ppr_options;
3308 } else if (cur_speed > 3300) {
3311 * In this range we the following
3312 * options ordered from highest to
3313 * lowest desireability:
3317 * o Narrow at a potentally higher sync rate.
3319 * All modes are tested with and without IU_REQ
3320 * set since using IUs may confuse an expander.
3322 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3324 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
3325 } else if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
3329 ppr_options = targ->dv_max_ppr_options;
3330 ppr_options &= ~MSG_EXT_PPR_DT_REQ;
3331 } else if (targ->dv_last_ppr_options != 0) {
3333 * Try without QAS or any other PPR options.
3334 * We may need a non-PPR message to work with
3335 * an expander. We look at the "last PPR options"
3336 * so we will perform this fallback even if the
3337 * target responded to our PPR negotiation with
3338 * no option bits set.
3341 } else if (width == MSG_EXT_WDTR_BUS_16_BIT) {
3343 * If the next narrow speed is greater than
3344 * the next wide speed, fallback to narrow.
3345 * Otherwise fallback to the next DT/Wide setting.
3346 * The narrow async speed will always be smaller
3347 * than the wide async speed, so handle this case
3350 ppr_options = targ->dv_max_ppr_options;
3351 if (narrow_speed > fallback_speed
3352 || period >= AHC_ASYNC_XFER_PERIOD) {
3353 targ->dv_next_wide_period = period+1;
3354 width = MSG_EXT_WDTR_BUS_8_BIT;
3355 period = targ->dv_next_narrow_period;
3359 } else if ((ahc->features & AHC_WIDE) != 0
3360 && targ->dv_max_width != 0
3361 && wide_speed >= fallback_speed
3362 && (targ->dv_next_wide_period <= AHC_ASYNC_XFER_PERIOD
3363 || period >= AHC_ASYNC_XFER_PERIOD)) {
3366 * We are narrow. Try falling back
3367 * to the next wide speed with
3368 * all supported ppr options set.
3370 targ->dv_next_narrow_period = period+1;
3371 width = MSG_EXT_WDTR_BUS_16_BIT;
3372 period = targ->dv_next_wide_period;
3373 ppr_options = targ->dv_max_ppr_options;
3375 /* Only narrow fallback is allowed. */
3377 ppr_options = targ->dv_max_ppr_options;
3380 ahc_unlock(ahc, &s);
3383 offset = MAX_OFFSET;
3384 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,
3386 ahc_set_width(ahc, devinfo, width, AHC_TRANS_GOAL, FALSE);
3391 if (width == MSG_EXT_WDTR_BUS_8_BIT)
3392 targ->dv_next_narrow_period = AHC_ASYNC_XFER_PERIOD;
3394 targ->dv_next_wide_period = AHC_ASYNC_XFER_PERIOD;
3396 ahc_set_syncrate(ahc, devinfo, syncrate, period, offset,
3397 ppr_options, AHC_TRANS_GOAL, FALSE);
3398 targ->dv_last_ppr_options = ppr_options;
3399 ahc_unlock(ahc, &s);
3404 ahc_linux_dv_timeout(struct scsi_cmnd *cmd)
3406 struct ahc_softc *ahc;
3410 ahc = *((struct ahc_softc **)cmd->device->host->hostdata);
3411 ahc_lock(ahc, &flags);
3414 if (ahc_debug & AHC_SHOW_DV) {
3415 printf("%s: Timeout while doing DV command %x.\n",
3416 ahc_name(ahc), cmd->cmnd[0]);
3417 ahc_dump_card_state(ahc);
3422 * Guard against "done race". No action is
3423 * required if we just completed.
3425 if ((scb = (struct scb *)cmd->host_scribble) == NULL) {
3426 ahc_unlock(ahc, &flags);
3431 * Command has not completed. Mark this
3432 * SCB as having failing status prior to
3433 * resetting the bus, so we get the correct
3436 if ((scb->flags & SCB_SENSE) != 0)
3437 ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
3439 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
3440 ahc_reset_channel(ahc, cmd->device->channel + 'A', /*initiate*/TRUE);
3443 * Add a minimal bus settle delay for devices that are slow to
3444 * respond after bus resets.
3446 ahc_linux_freeze_simq(ahc);
3447 init_timer(&ahc->platform_data->reset_timer);
3448 ahc->platform_data->reset_timer.data = (u_long)ahc;
3449 ahc->platform_data->reset_timer.expires = jiffies + HZ / 2;
3450 ahc->platform_data->reset_timer.function =
3451 (ahc_linux_callback_t *)ahc_linux_release_simq;
3452 add_timer(&ahc->platform_data->reset_timer);
3453 if (ahc_linux_next_device_to_run(ahc) != NULL)
3454 ahc_schedule_runq(ahc);
3455 ahc_linux_run_complete_queue(ahc);
3456 ahc_unlock(ahc, &flags);
3460 ahc_linux_dv_complete(struct scsi_cmnd *cmd)
3462 struct ahc_softc *ahc;
3464 ahc = *((struct ahc_softc **)cmd->device->host->hostdata);
3466 /* Delete the DV timer before it goes off! */
3467 scsi_delete_timer(cmd);
3470 if (ahc_debug & AHC_SHOW_DV)
3471 printf("%s:%d:%d: Command completed, status= 0x%x\n",
3472 ahc_name(ahc), cmd->device->channel,
3473 cmd->device->id, cmd->result);
3476 /* Wake up the state machine */
3477 up(&ahc->platform_data->dv_cmd_sem);
3481 ahc_linux_generate_dv_pattern(struct ahc_linux_target *targ)
3487 if (targ->dv_buffer != NULL)
3488 free(targ->dv_buffer, M_DEVBUF);
3489 targ->dv_buffer = malloc(targ->dv_echo_size, M_DEVBUF, M_WAITOK);
3490 if (targ->dv_buffer1 != NULL)
3491 free(targ->dv_buffer1, M_DEVBUF);
3492 targ->dv_buffer1 = malloc(targ->dv_echo_size, M_DEVBUF, M_WAITOK);
3496 for (j = 0 ; i < targ->dv_echo_size; j++) {
3499 * 32bytes of sequential numbers.
3501 targ->dv_buffer[i++] = j & 0xff;
3502 } else if (j < 48) {
3504 * 32bytes of repeating 0x0000, 0xffff.
3506 targ->dv_buffer[i++] = (j & 0x02) ? 0xff : 0x00;
3507 } else if (j < 64) {
3509 * 32bytes of repeating 0x5555, 0xaaaa.
3511 targ->dv_buffer[i++] = (j & 0x02) ? 0xaa : 0x55;
3514 * Remaining buffer is filled with a repeating
3518 * ~0x0001 << shifted once in each loop.
3522 targ->dv_buffer[i++] = ~(b >> 8) & 0xff;
3527 targ->dv_buffer[i++] = (~b & 0xff);
3530 targ->dv_buffer[i++] = 0xff;
3537 ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3539 static int warned_user;
3543 if ((ahc->user_discenable & devinfo->target_mask) != 0) {
3544 if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
3545 if (warned_user == 0) {
3548 "aic7xxx: WARNING: Insufficient tag_info instances\n"
3549 "aic7xxx: for installed controllers. Using defaults\n"
3550 "aic7xxx: Please update the aic7xxx_tag_info array in\n"
3551 "aic7xxx: the aic7xxx_osm..c source file.\n");
3554 tags = AHC_MAX_QUEUE;
3556 adapter_tag_info_t *tag_info;
3558 tag_info = &aic7xxx_tag_info[ahc->unit];
3559 tags = tag_info->tag_commands[devinfo->target_offset];
3560 if (tags > AHC_MAX_QUEUE)
3561 tags = AHC_MAX_QUEUE;
3568 ahc_linux_user_dv_setting(struct ahc_softc *ahc)
3570 static int warned_user;
3573 if (ahc->unit >= NUM_ELEMENTS(aic7xxx_dv_settings)) {
3574 if (warned_user == 0) {
3577 "aic7xxx: WARNING: Insufficient dv settings instances\n"
3578 "aic7xxx: for installed controllers. Using defaults\n"
3579 "aic7xxx: Please update the aic7xxx_dv_settings array\n"
3580 "aic7xxx: in the aic7xxx_osm.c source file.\n");
3586 dv = aic7xxx_dv_settings[ahc->unit];
3593 * Apply the default.
3596 * XXX - Enable DV on non-U160 controllers once it
3597 * has been tested there.
3600 dv = (ahc->features & AHC_DT);
3601 if (ahc->seep_config != 0
3602 && ahc->seep_config->signature >= CFSIGNATURE2)
3603 dv = (ahc->seep_config->adapter_control & CFENABLEDV);
3604 ahc_unlock(ahc, &s);
3610 * Determines the queue depth for a given device.
3613 ahc_linux_device_queue_depth(struct ahc_softc *ahc,
3614 struct ahc_linux_device *dev)
3616 struct ahc_devinfo devinfo;
3619 ahc_compile_devinfo(&devinfo,
3620 dev->target->channel == 0
3621 ? ahc->our_id : ahc->our_id_b,
3622 dev->target->target, dev->lun,
3623 dev->target->channel == 0 ? 'A' : 'B',
3625 tags = ahc_linux_user_tagdepth(ahc, &devinfo);
3627 && dev->scsi_device != NULL
3628 && dev->scsi_device->tagged_supported != 0) {
3630 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
3631 ahc_print_devinfo(ahc, &devinfo);
3632 printf("Tagged Queuing enabled. Depth %d\n", tags);
3634 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE);
3639 ahc_linux_run_device_queue(struct ahc_softc *ahc, struct ahc_linux_device *dev)
3641 struct ahc_cmd *acmd;
3642 struct scsi_cmnd *cmd;
3644 struct hardware_scb *hscb;
3645 struct ahc_initiator_tinfo *tinfo;
3646 struct ahc_tmode_tstate *tstate;
3649 if ((dev->flags & AHC_DEV_ON_RUN_LIST) != 0)
3650 panic("running device on run list");
3652 while ((acmd = TAILQ_FIRST(&dev->busyq)) != NULL
3653 && dev->openings > 0 && dev->qfrozen == 0) {
3656 * Schedule us to run later. The only reason we are not
3657 * running is because the whole controller Q is frozen.
3659 if (ahc->platform_data->qfrozen != 0
3660 && AHC_DV_SIMQ_FROZEN(ahc) == 0) {
3661 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
3663 dev->flags |= AHC_DEV_ON_RUN_LIST;
3667 * Get an scb to use.
3669 if ((scb = ahc_get_scb(ahc)) == NULL) {
3670 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
3672 dev->flags |= AHC_DEV_ON_RUN_LIST;
3673 ahc->flags |= AHC_RESOURCE_SHORTAGE;
3676 TAILQ_REMOVE(&dev->busyq, acmd, acmd_links.tqe);
3677 cmd = &acmd_scsi_cmd(acmd);
3679 scb->platform_data->dev = dev;
3681 cmd->host_scribble = (char *)scb;
3684 * Fill out basics of the HSCB.
3687 hscb->scsiid = BUILD_SCSIID(ahc, cmd);
3688 hscb->lun = cmd->device->lun;
3689 mask = SCB_GET_TARGET_MASK(ahc, scb);
3690 tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
3691 SCB_GET_OUR_ID(scb),
3692 SCB_GET_TARGET(ahc, scb), &tstate);
3693 hscb->scsirate = tinfo->scsirate;
3694 hscb->scsioffset = tinfo->curr.offset;
3695 if ((tstate->ultraenb & mask) != 0)
3696 hscb->control |= ULTRAENB;
3698 if ((ahc->user_discenable & mask) != 0)
3699 hscb->control |= DISCENB;
3701 if (AHC_DV_CMD(cmd) != 0)
3702 scb->flags |= SCB_SILENT;
3704 if ((tstate->auto_negotiate & mask) != 0) {
3705 scb->flags |= SCB_AUTO_NEGOTIATE;
3706 scb->hscb->control |= MK_MESSAGE;
3709 if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
3710 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3712 uint8_t tag_msgs[2];
3714 msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
3715 if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
3716 hscb->control |= tag_msgs[0];
3717 if (tag_msgs[0] == MSG_ORDERED_TASK)
3718 dev->commands_since_idle_or_otag = 0;
3721 if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
3722 && (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
3723 hscb->control |= MSG_ORDERED_TASK;
3724 dev->commands_since_idle_or_otag = 0;
3726 hscb->control |= MSG_SIMPLE_TASK;
3730 hscb->cdb_len = cmd->cmd_len;
3731 if (hscb->cdb_len <= 12) {
3732 memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
3734 memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
3735 scb->flags |= SCB_CDB32_PTR;
3738 scb->platform_data->xfer_len = 0;
3739 ahc_set_residual(scb, 0);
3740 ahc_set_sense_residual(scb, 0);
3742 if (cmd->use_sg != 0) {
3743 struct ahc_dma_seg *sg;
3744 struct scatterlist *cur_seg;
3745 struct scatterlist *end_seg;
3748 cur_seg = (struct scatterlist *)cmd->request_buffer;
3749 nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
3750 scsi_to_pci_dma_dir(cmd->sc_data_direction));
3751 end_seg = cur_seg + nseg;
3752 /* Copy the segments into the SG list. */
3755 * The sg_count may be larger than nseg if
3756 * a transfer crosses a 32bit page.
3758 while (cur_seg < end_seg) {
3763 addr = sg_dma_address(cur_seg);
3764 len = sg_dma_len(cur_seg);
3765 consumed = ahc_linux_map_seg(ahc, scb,
3768 scb->sg_count += consumed;
3772 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
3775 * Reset the sg list pointer.
3778 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
3781 * Copy the first SG into the "current"
3782 * data pointer area.
3784 scb->hscb->dataptr = scb->sg_list->addr;
3785 scb->hscb->datacnt = scb->sg_list->len;
3786 } else if (cmd->request_bufflen != 0) {
3787 struct ahc_dma_seg *sg;
3791 addr = pci_map_single(ahc->dev_softc,
3792 cmd->request_buffer,
3793 cmd->request_bufflen,
3794 scsi_to_pci_dma_dir(cmd->sc_data_direction));
3795 scb->platform_data->buf_busaddr = addr;
3796 scb->sg_count = ahc_linux_map_seg(ahc, scb,
3798 cmd->request_bufflen);
3799 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
3802 * Reset the sg list pointer.
3805 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
3808 * Copy the first SG into the "current"
3809 * data pointer area.
3811 scb->hscb->dataptr = sg->addr;
3812 scb->hscb->datacnt = sg->len;
3814 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
3815 scb->hscb->dataptr = 0;
3816 scb->hscb->datacnt = 0;
3820 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_PREWRITE);
3821 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
3824 dev->commands_issued++;
3825 if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
3826 dev->commands_since_idle_or_otag++;
3829 * We only allow one untagged transaction
3830 * per target in the initiator role unless
3831 * we are storing a full busy target *lun*
3832 * table in SCB space.
3834 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
3835 && (ahc->features & AHC_SCB_BTT) == 0) {
3836 struct scb_tailq *untagged_q;
3839 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
3840 untagged_q = &(ahc->untagged_queues[target_offset]);
3841 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
3842 scb->flags |= SCB_UNTAGGEDQ;
3843 if (TAILQ_FIRST(untagged_q) != scb)
3846 scb->flags |= SCB_ACTIVE;
3847 ahc_queue_scb(ahc, scb);
3852 * SCSI controller interrupt handler.
3855 ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
3857 struct ahc_softc *ahc;
3861 ahc = (struct ahc_softc *) dev_id;
3862 ahc_lock(ahc, &flags);
3863 ours = ahc_intr(ahc);
3864 if (ahc_linux_next_device_to_run(ahc) != NULL)
3865 ahc_schedule_runq(ahc);
3866 ahc_linux_run_complete_queue(ahc);
3867 ahc_unlock(ahc, &flags);
3868 return IRQ_RETVAL(ours);
3872 ahc_platform_flushwork(struct ahc_softc *ahc)
3875 while (ahc_linux_run_complete_queue(ahc) != NULL)
3879 static struct ahc_linux_target*
3880 ahc_linux_alloc_target(struct ahc_softc *ahc, u_int channel, u_int target)
3882 struct ahc_linux_target *targ;
3883 u_int target_offset;
3885 target_offset = target;
3889 targ = malloc(sizeof(*targ), M_DEVBUG, M_NOWAIT);
3892 memset(targ, 0, sizeof(*targ));
3893 targ->channel = channel;
3894 targ->target = target;
3896 targ->flags = AHC_DV_REQUIRED;
3897 ahc->platform_data->targets[target_offset] = targ;
3902 ahc_linux_free_target(struct ahc_softc *ahc, struct ahc_linux_target *targ)
3904 struct ahc_devinfo devinfo;
3905 struct ahc_initiator_tinfo *tinfo;
3906 struct ahc_tmode_tstate *tstate;
3908 u_int target_offset;
3912 * Force a negotiation to async/narrow on any
3913 * future command to this device unless a bus
3914 * reset occurs between now and that command.
3916 channel = 'A' + targ->channel;
3917 our_id = ahc->our_id;
3918 target_offset = targ->target;
3919 if (targ->channel != 0) {
3921 our_id = ahc->our_id_b;
3923 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
3924 targ->target, &tstate);
3925 ahc_compile_devinfo(&devinfo, our_id, targ->target, CAM_LUN_WILDCARD,
3926 channel, ROLE_INITIATOR);
3927 ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
3928 AHC_TRANS_GOAL, /*paused*/FALSE);
3929 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
3930 AHC_TRANS_GOAL, /*paused*/FALSE);
3931 ahc_update_neg_request(ahc, &devinfo, tstate, tinfo, AHC_NEG_ALWAYS);
3932 ahc->platform_data->targets[target_offset] = NULL;
3933 if (targ->inq_data != NULL)
3934 free(targ->inq_data, M_DEVBUF);
3935 if (targ->dv_buffer != NULL)
3936 free(targ->dv_buffer, M_DEVBUF);
3937 if (targ->dv_buffer1 != NULL)
3938 free(targ->dv_buffer1, M_DEVBUF);
3939 free(targ, M_DEVBUF);
3942 static struct ahc_linux_device*
3943 ahc_linux_alloc_device(struct ahc_softc *ahc,
3944 struct ahc_linux_target *targ, u_int lun)
3946 struct ahc_linux_device *dev;
3948 dev = malloc(sizeof(*dev), M_DEVBUG, M_NOWAIT);
3951 memset(dev, 0, sizeof(*dev));
3952 init_timer(&dev->timer);
3953 TAILQ_INIT(&dev->busyq);
3954 dev->flags = AHC_DEV_UNCONFIGURED;
3959 * We start out life using untagged
3960 * transactions of which we allow one.
3965 * Set maxtags to 0. This will be changed if we
3966 * later determine that we are dealing with
3967 * a tagged queuing capable device.
3972 targ->devices[lun] = dev;
3977 __ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
3979 struct ahc_linux_target *targ;
3982 targ->devices[dev->lun] = NULL;
3983 free(dev, M_DEVBUF);
3985 if (targ->refcount == 0
3986 && (targ->flags & AHC_DV_REQUIRED) == 0)
3987 ahc_linux_free_target(ahc, targ);
3991 ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
3993 del_timer_sync(&dev->timer);
3994 __ahc_linux_free_device(ahc, dev);
3998 ahc_send_async(struct ahc_softc *ahc, char channel,
3999 u_int target, u_int lun, ac_code code, void *arg)
4002 case AC_TRANSFER_NEG:
4005 struct ahc_linux_target *targ;
4006 struct info_str info;
4007 struct ahc_initiator_tinfo *tinfo;
4008 struct ahc_tmode_tstate *tstate;
4012 info.length = sizeof(buf);
4015 tinfo = ahc_fetch_transinfo(ahc, channel,
4016 channel == 'A' ? ahc->our_id
4021 * Don't bother reporting results while
4022 * negotiations are still pending.
4024 if (tinfo->curr.period != tinfo->goal.period
4025 || tinfo->curr.width != tinfo->goal.width
4026 || tinfo->curr.offset != tinfo->goal.offset
4027 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
4028 if (bootverbose == 0)
4032 * Don't bother reporting results that
4033 * are identical to those last reported.
4035 target_offset = target;
4038 targ = ahc->platform_data->targets[target_offset];
4041 if (tinfo->curr.period == targ->last_tinfo.period
4042 && tinfo->curr.width == targ->last_tinfo.width
4043 && tinfo->curr.offset == targ->last_tinfo.offset
4044 && tinfo->curr.ppr_options == targ->last_tinfo.ppr_options)
4045 if (bootverbose == 0)
4048 targ->last_tinfo.period = tinfo->curr.period;
4049 targ->last_tinfo.width = tinfo->curr.width;
4050 targ->last_tinfo.offset = tinfo->curr.offset;
4051 targ->last_tinfo.ppr_options = tinfo->curr.ppr_options;
4053 printf("(%s:%c:", ahc_name(ahc), channel);
4054 if (target == CAM_TARGET_WILDCARD)
4057 printf("%d): ", target);
4058 ahc_format_transinfo(&info, &tinfo->curr);
4059 if (info.pos < info.length)
4060 *info.buffer = '\0';
4062 buf[info.length - 1] = '\0';
4068 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
4069 WARN_ON(lun != CAM_LUN_WILDCARD);
4070 scsi_report_device_reset(ahc->platform_data->host,
4071 channel - 'A', target);
4072 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
4073 Scsi_Device *scsi_dev;
4076 * Find the SCSI device associated with this
4077 * request and indicate that a UA is expected.
4079 for (scsi_dev = ahc->platform_data->host->host_queue;
4080 scsi_dev != NULL; scsi_dev = scsi_dev->next) {
4081 if (channel - 'A' == scsi_dev->channel
4082 && target == scsi_dev->id
4083 && (lun == CAM_LUN_WILDCARD
4084 || lun == scsi_dev->lun)) {
4085 scsi_dev->was_reset = 1;
4086 scsi_dev->expecting_cc_ua = 1;
4093 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
4094 if (ahc->platform_data->host != NULL) {
4095 scsi_report_bus_reset(ahc->platform_data->host,
4101 panic("ahc_send_async: Unexpected async event");
4106 * Calls the higher level scsi done function and frees the scb.
4109 ahc_done(struct ahc_softc *ahc, struct scb *scb)
4112 struct ahc_linux_device *dev;
4114 LIST_REMOVE(scb, pending_links);
4115 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
4116 struct scb_tailq *untagged_q;
4119 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
4120 untagged_q = &(ahc->untagged_queues[target_offset]);
4121 TAILQ_REMOVE(untagged_q, scb, links.tqe);
4122 ahc_run_untagged_queue(ahc, untagged_q);
4125 if ((scb->flags & SCB_ACTIVE) == 0) {
4126 printf("SCB %d done'd twice\n", scb->hscb->tag);
4127 ahc_dump_card_state(ahc);
4128 panic("Stopping for safety");
4131 dev = scb->platform_data->dev;
4134 if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
4135 cmd->result &= ~(CAM_DEV_QFRZN << 16);
4138 ahc_linux_unmap_scb(ahc, scb);
4141 * Guard against stale sense data.
4142 * The Linux mid-layer assumes that sense
4143 * was retrieved anytime the first byte of
4144 * the sense buffer looks "sane".
4146 cmd->sense_buffer[0] = 0;
4147 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
4148 uint32_t amount_xferred;
4151 ahc_get_transfer_length(scb) - ahc_get_residual(scb);
4152 if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
4154 if ((ahc_debug & AHC_SHOW_MISC) != 0) {
4155 ahc_print_path(ahc, scb);
4156 printf("Set CAM_UNCOR_PARITY\n");
4159 ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
4160 #ifdef AHC_REPORT_UNDERFLOWS
4162 * This code is disabled by default as some
4163 * clients of the SCSI system do not properly
4164 * initialize the underflow parameter. This
4165 * results in spurious termination of commands
4166 * that complete as expected (e.g. underflow is
4167 * allowed as command can return variable amounts
4170 } else if (amount_xferred < scb->io_ctx->underflow) {
4173 ahc_print_path(ahc, scb);
4175 for (i = 0; i < scb->io_ctx->cmd_len; i++)
4176 printf(" 0x%x", scb->io_ctx->cmnd[i]);
4178 ahc_print_path(ahc, scb);
4179 printf("Saw underflow (%ld of %ld bytes). "
4180 "Treated as error\n",
4181 ahc_get_residual(scb),
4182 ahc_get_transfer_length(scb));
4183 ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
4186 ahc_set_transaction_status(scb, CAM_REQ_CMP);
4188 } else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
4189 ahc_linux_handle_scsi_status(ahc, dev, scb);
4190 } else if (ahc_get_transaction_status(scb) == CAM_SEL_TIMEOUT) {
4191 dev->flags |= AHC_DEV_UNCONFIGURED;
4192 if (AHC_DV_CMD(cmd) == FALSE)
4193 dev->target->flags &= ~AHC_DV_REQUIRED;
4196 * Start DV for devices that require it assuming the first command
4197 * sent does not result in a selection timeout.
4199 if (ahc_get_transaction_status(scb) != CAM_SEL_TIMEOUT
4200 && (dev->target->flags & AHC_DV_REQUIRED) != 0)
4201 ahc_linux_start_dv(ahc);
4203 if (dev->openings == 1
4204 && ahc_get_transaction_status(scb) == CAM_REQ_CMP
4205 && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
4206 dev->tag_success_count++;
4208 * Some devices deal with temporary internal resource
4209 * shortages by returning queue full. When the queue
4210 * full occurrs, we throttle back. Slowly try to get
4211 * back to our previous queue depth.
4213 if ((dev->openings + dev->active) < dev->maxtags
4214 && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
4215 dev->tag_success_count = 0;
4219 if (dev->active == 0)
4220 dev->commands_since_idle_or_otag = 0;
4222 if (TAILQ_EMPTY(&dev->busyq)) {
4223 if ((dev->flags & AHC_DEV_UNCONFIGURED) != 0
4225 && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0)
4226 ahc_linux_free_device(ahc, dev);
4227 } else if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) {
4228 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links);
4229 dev->flags |= AHC_DEV_ON_RUN_LIST;
4232 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
4233 printf("Recovery SCB completes\n");
4234 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
4235 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
4236 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
4237 if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
4238 ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
4239 up(&ahc->platform_data->eh_sem);
4243 ahc_free_scb(ahc, scb);
4244 ahc_linux_queue_cmd_complete(ahc, cmd);
4246 if ((ahc->platform_data->flags & AHC_DV_WAIT_SIMQ_EMPTY) != 0
4247 && LIST_FIRST(&ahc->pending_scbs) == NULL) {
4248 ahc->platform_data->flags &= ~AHC_DV_WAIT_SIMQ_EMPTY;
4249 up(&ahc->platform_data->dv_sem);
4255 ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
4256 struct ahc_linux_device *dev, struct scb *scb)
4258 struct ahc_devinfo devinfo;
4260 ahc_compile_devinfo(&devinfo,
4262 dev->target->target, dev->lun,
4263 dev->target->channel == 0 ? 'A' : 'B',
4267 * We don't currently trust the mid-layer to
4268 * properly deal with queue full or busy. So,
4269 * when one occurs, we tell the mid-layer to
4270 * unconditionally requeue the command to us
4271 * so that we can retry it ourselves. We also
4272 * implement our own throttling mechanism so
4273 * we don't clobber the device with too many
4276 switch (ahc_get_scsi_status(scb)) {
4279 case SCSI_STATUS_CHECK_COND:
4280 case SCSI_STATUS_CMD_TERMINATED:
4285 * Copy sense information to the OS's cmd
4286 * structure if it is available.
4289 if (scb->flags & SCB_SENSE) {
4292 sense_size = MIN(sizeof(struct scsi_sense_data)
4293 - ahc_get_sense_residual(scb),
4294 sizeof(cmd->sense_buffer));
4295 memcpy(cmd->sense_buffer,
4296 ahc_get_sense_buf(ahc, scb), sense_size);
4297 if (sense_size < sizeof(cmd->sense_buffer))
4298 memset(&cmd->sense_buffer[sense_size], 0,
4299 sizeof(cmd->sense_buffer) - sense_size);
4300 cmd->result |= (DRIVER_SENSE << 24);
4302 if (ahc_debug & AHC_SHOW_SENSE) {
4305 printf("Copied %d bytes of sense data:",
4307 for (i = 0; i < sense_size; i++) {
4310 printf("0x%x ", cmd->sense_buffer[i]);
4318 case SCSI_STATUS_QUEUE_FULL:
4321 * By the time the core driver has returned this
4322 * command, all other commands that were queued
4323 * to us but not the device have been returned.
4324 * This ensures that dev->active is equal to
4325 * the number of commands actually queued to
4328 dev->tag_success_count = 0;
4329 if (dev->active != 0) {
4331 * Drop our opening count to the number
4332 * of commands currently outstanding.
4336 ahc_print_path(ahc, scb);
4337 printf("Dropping tag count to %d\n", dev->active);
4339 if (dev->active == dev->tags_on_last_queuefull) {
4341 dev->last_queuefull_same_count++;
4343 * If we repeatedly see a queue full
4344 * at the same queue depth, this
4345 * device has a fixed number of tag
4346 * slots. Lock in this tag depth
4347 * so we stop seeing queue fulls from
4350 if (dev->last_queuefull_same_count
4351 == AHC_LOCK_TAGS_COUNT) {
4352 dev->maxtags = dev->active;
4353 ahc_print_path(ahc, scb);
4354 printf("Locking max tag count at %d\n",
4358 dev->tags_on_last_queuefull = dev->active;
4359 dev->last_queuefull_same_count = 0;
4361 ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
4362 ahc_set_scsi_status(scb, SCSI_STATUS_OK);
4363 ahc_platform_set_tags(ahc, &devinfo,
4364 (dev->flags & AHC_DEV_Q_BASIC)
4365 ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
4369 * Drop down to a single opening, and treat this
4370 * as if the target returned BUSY SCSI status.
4373 ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
4374 ahc_platform_set_tags(ahc, &devinfo,
4375 (dev->flags & AHC_DEV_Q_BASIC)
4376 ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
4379 case SCSI_STATUS_BUSY:
4382 * Set a short timer to defer sending commands for
4383 * a bit since Linux will not delay in this case.
4385 if ((dev->flags & AHC_DEV_TIMER_ACTIVE) != 0) {
4386 printf("%s:%c:%d: Device Timer still active during "
4387 "busy processing\n", ahc_name(ahc),
4388 dev->target->channel, dev->target->target);
4391 dev->flags |= AHC_DEV_TIMER_ACTIVE;
4393 init_timer(&dev->timer);
4394 dev->timer.data = (u_long)dev;
4395 dev->timer.expires = jiffies + (HZ/2);
4396 dev->timer.function = ahc_linux_dev_timed_unfreeze;
4397 add_timer(&dev->timer);
4404 ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, Scsi_Cmnd *cmd)
4407 * Typically, the complete queue has very few entries
4408 * queued to it before the queue is emptied by
4409 * ahc_linux_run_complete_queue, so sorting the entries
4410 * by generation number should be inexpensive.
4411 * We perform the sort so that commands that complete
4412 * with an error are retuned in the order origionally
4413 * queued to the controller so that any subsequent retries
4414 * are performed in order. The underlying ahc routines do
4415 * not guarantee the order that aborted commands will be
4418 struct ahc_completeq *completeq;
4419 struct ahc_cmd *list_cmd;
4420 struct ahc_cmd *acmd;
4423 * Map CAM error codes into Linux Error codes. We
4424 * avoid the conversion so that the DV code has the
4425 * full error information available when making
4426 * state change decisions.
4428 if (AHC_DV_CMD(cmd) == FALSE) {
4431 switch (ahc_cmd_get_transaction_status(cmd)) {
4432 case CAM_REQ_INPROG:
4434 case CAM_SCSI_STATUS_ERROR:
4435 new_status = DID_OK;
4437 case CAM_REQ_ABORTED:
4438 new_status = DID_ABORT;
4441 new_status = DID_BUS_BUSY;
4443 case CAM_REQ_INVALID:
4444 case CAM_PATH_INVALID:
4445 new_status = DID_BAD_TARGET;
4447 case CAM_SEL_TIMEOUT:
4448 new_status = DID_NO_CONNECT;
4450 case CAM_SCSI_BUS_RESET:
4452 new_status = DID_RESET;
4454 case CAM_UNCOR_PARITY:
4455 new_status = DID_PARITY;
4457 case CAM_CMD_TIMEOUT:
4458 new_status = DID_TIME_OUT;
4461 case CAM_REQ_CMP_ERR:
4462 case CAM_AUTOSENSE_FAIL:
4464 case CAM_DATA_RUN_ERR:
4465 case CAM_UNEXP_BUSFREE:
4466 case CAM_SEQUENCE_FAIL:
4467 case CAM_CCB_LEN_ERR:
4468 case CAM_PROVIDE_FAIL:
4469 case CAM_REQ_TERMIO:
4470 case CAM_UNREC_HBA_ERROR:
4471 case CAM_REQ_TOO_BIG:
4472 new_status = DID_ERROR;
4474 case CAM_REQUEUE_REQ:
4476 * If we want the request requeued, make sure there
4477 * are sufficent retries. In the old scsi error code,
4478 * we used to be able to specify a result code that
4479 * bypassed the retry count. Now we must use this
4480 * hack. We also "fake" a check condition with
4481 * a sense code of ABORTED COMMAND. This seems to
4482 * evoke a retry even if this command is being sent
4483 * via the eh thread. Ick! Ick! Ick!
4485 if (cmd->retries > 0)
4487 new_status = DID_OK;
4488 ahc_cmd_set_scsi_status(cmd, SCSI_STATUS_CHECK_COND);
4489 cmd->result |= (DRIVER_SENSE << 24);
4490 memset(cmd->sense_buffer, 0,
4491 sizeof(cmd->sense_buffer));
4492 cmd->sense_buffer[0] = SSD_ERRCODE_VALID
4493 | SSD_CURRENT_ERROR;
4494 cmd->sense_buffer[2] = SSD_KEY_ABORTED_COMMAND;
4497 /* We should never get here */
4498 new_status = DID_ERROR;
4502 ahc_cmd_set_transaction_status(cmd, new_status);
4505 completeq = &ahc->platform_data->completeq;
4506 list_cmd = TAILQ_FIRST(completeq);
4507 acmd = (struct ahc_cmd *)cmd;
4508 while (list_cmd != NULL
4509 && acmd_scsi_cmd(list_cmd).serial_number
4510 < acmd_scsi_cmd(acmd).serial_number)
4511 list_cmd = TAILQ_NEXT(list_cmd, acmd_links.tqe);
4512 if (list_cmd != NULL)
4513 TAILQ_INSERT_BEFORE(list_cmd, acmd, acmd_links.tqe);
4515 TAILQ_INSERT_TAIL(completeq, acmd, acmd_links.tqe);
4519 ahc_linux_filter_inquiry(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
4521 struct scsi_inquiry_data *sid;
4522 struct ahc_initiator_tinfo *tinfo;
4523 struct ahc_transinfo *user;
4524 struct ahc_transinfo *goal;
4525 struct ahc_transinfo *curr;
4526 struct ahc_tmode_tstate *tstate;
4527 struct ahc_syncrate *syncrate;
4528 struct ahc_linux_device *dev;
4534 u_int trans_version;
4538 * Determine if this lun actually exists. If so,
4539 * hold on to its corresponding device structure.
4540 * If not, make sure we release the device and
4541 * don't bother processing the rest of this inquiry
4544 dev = ahc_linux_get_device(ahc, devinfo->channel - 'A',
4545 devinfo->target, devinfo->lun,
4548 sid = (struct scsi_inquiry_data *)dev->target->inq_data;
4549 if (SID_QUAL(sid) == SID_QUAL_LU_CONNECTED) {
4551 dev->flags &= ~AHC_DEV_UNCONFIGURED;
4553 dev->flags |= AHC_DEV_UNCONFIGURED;
4558 * Update our notion of this device's transfer
4559 * negotiation capabilities.
4561 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
4562 devinfo->our_scsiid,
4563 devinfo->target, &tstate);
4564 user = &tinfo->user;
4565 goal = &tinfo->goal;
4566 curr = &tinfo->curr;
4567 width = user->width;
4568 period = user->period;
4569 offset = user->offset;
4570 ppr_options = user->ppr_options;
4571 trans_version = user->transport_version;
4572 prot_version = MIN(user->protocol_version, SID_ANSI_REV(sid));
4575 * Only attempt SPI3/4 once we've verified that
4576 * the device claims to support SPI3/4 features.
4578 if (prot_version < SCSI_REV_2)
4579 trans_version = SID_ANSI_REV(sid);
4581 trans_version = SCSI_REV_2;
4583 if ((sid->flags & SID_WBus16) == 0)
4584 width = MSG_EXT_WDTR_BUS_8_BIT;
4585 if ((sid->flags & SID_Sync) == 0) {
4590 if ((sid->spi3data & SID_SPI_QAS) == 0)
4591 ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
4592 if ((sid->spi3data & SID_SPI_CLOCK_DT) == 0)
4593 ppr_options &= MSG_EXT_PPR_QAS_REQ;
4594 if ((sid->spi3data & SID_SPI_IUS) == 0)
4595 ppr_options &= (MSG_EXT_PPR_DT_REQ
4596 | MSG_EXT_PPR_QAS_REQ);
4598 if (prot_version > SCSI_REV_2
4599 && ppr_options != 0)
4600 trans_version = user->transport_version;
4602 ahc_validate_width(ahc, /*tinfo limit*/NULL, &width, ROLE_UNKNOWN);
4603 if ((ahc->features & AHC_ULTRA2) != 0)
4604 maxsync = AHC_SYNCRATE_DT;
4605 else if ((ahc->features & AHC_ULTRA) != 0)
4606 maxsync = AHC_SYNCRATE_ULTRA;
4608 maxsync = AHC_SYNCRATE_FAST;
4610 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, maxsync);
4611 ahc_validate_offset(ahc, /*tinfo limit*/NULL, syncrate,
4612 &offset, width, ROLE_UNKNOWN);
4613 if (offset == 0 || period == 0) {
4618 /* Apply our filtered user settings. */
4619 curr->transport_version = trans_version;
4620 curr->protocol_version = prot_version;
4621 ahc_set_width(ahc, devinfo, width, AHC_TRANS_GOAL, /*paused*/FALSE);
4622 ahc_set_syncrate(ahc, devinfo, syncrate, period,
4623 offset, ppr_options, AHC_TRANS_GOAL,
4628 ahc_linux_sem_timeout(u_long arg)
4630 struct ahc_softc *ahc;
4633 ahc = (struct ahc_softc *)arg;
4636 if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
4637 ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
4638 up(&ahc->platform_data->eh_sem);
4640 ahc_unlock(ahc, &s);
4644 ahc_linux_freeze_simq(struct ahc_softc *ahc)
4646 ahc->platform_data->qfrozen++;
4647 if (ahc->platform_data->qfrozen == 1) {
4648 scsi_block_requests(ahc->platform_data->host);
4650 /* XXX What about Twin channels? */
4651 ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
4652 CAM_LUN_WILDCARD, SCB_LIST_NULL,
4653 ROLE_INITIATOR, CAM_REQUEUE_REQ);
4658 ahc_linux_release_simq(u_long arg)
4660 struct ahc_softc *ahc;
4664 ahc = (struct ahc_softc *)arg;
4668 if (ahc->platform_data->qfrozen > 0)
4669 ahc->platform_data->qfrozen--;
4670 if (ahc->platform_data->qfrozen == 0)
4672 if (AHC_DV_SIMQ_FROZEN(ahc)
4673 && ((ahc->platform_data->flags & AHC_DV_WAIT_SIMQ_RELEASE) != 0)) {
4674 ahc->platform_data->flags &= ~AHC_DV_WAIT_SIMQ_RELEASE;
4675 up(&ahc->platform_data->dv_sem);
4677 ahc_schedule_runq(ahc);
4678 ahc_unlock(ahc, &s);
4680 * There is still a race here. The mid-layer
4681 * should keep its own freeze count and use
4682 * a bottom half handler to run the queues
4683 * so we can unblock with our own lock held.
4686 scsi_unblock_requests(ahc->platform_data->host);
4690 ahc_linux_dev_timed_unfreeze(u_long arg)
4692 struct ahc_linux_device *dev;
4693 struct ahc_softc *ahc;
4696 dev = (struct ahc_linux_device *)arg;
4697 ahc = dev->target->ahc;
4699 dev->flags &= ~AHC_DEV_TIMER_ACTIVE;
4700 if (dev->qfrozen > 0)
4702 if (dev->qfrozen == 0
4703 && (dev->flags & AHC_DEV_ON_RUN_LIST) == 0)
4704 ahc_linux_run_device_queue(ahc, dev);
4705 if (TAILQ_EMPTY(&dev->busyq)
4706 && dev->active == 0)
4707 __ahc_linux_free_device(ahc, dev);
4708 ahc_unlock(ahc, &s);
4712 ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag)
4714 struct ahc_softc *ahc;
4715 struct ahc_cmd *acmd;
4716 struct ahc_cmd *list_acmd;
4717 struct ahc_linux_device *dev;
4718 struct scb *pending_scb;
4721 u_int active_scb_index;
4734 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
4735 acmd = (struct ahc_cmd *)cmd;
4737 printf("%s:%d:%d:%d: Attempting to queue a%s message\n",
4738 ahc_name(ahc), cmd->device->channel,
4739 cmd->device->id, cmd->device->lun,
4740 flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
4743 for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
4744 printf(" 0x%x", cmd->cmnd[cdb_byte]);
4748 * In all versions of Linux, we have to work around
4749 * a major flaw in how the mid-layer is locked down
4750 * if we are to sleep successfully in our error handler
4751 * while allowing our interrupt handler to run. Since
4752 * the midlayer acquires either the io_request_lock or
4753 * our lock prior to calling us, we must use the
4754 * spin_unlock_irq() method for unlocking our lock.
4755 * This will force interrupts to be enabled on the
4756 * current CPU. Since the EH thread should not have
4757 * been running with CPU interrupts disabled other than
4758 * by acquiring either the io_request_lock or our own
4759 * lock, this *should* be safe.
4761 ahc_midlayer_entrypoint_lock(ahc, &s);
4764 * First determine if we currently own this command.
4765 * Start by searching the device queue. If not found
4766 * there, check the pending_scb list. If not found
4767 * at all, and the system wanted us to just abort the
4768 * command, return success.
4770 dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
4771 cmd->device->lun, /*alloc*/FALSE);
4775 * No target device for this command exists,
4776 * so we must not still own the command.
4778 printf("%s:%d:%d:%d: Is not an active device\n",
4779 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4785 TAILQ_FOREACH(list_acmd, &dev->busyq, acmd_links.tqe) {
4786 if (list_acmd == acmd)
4790 if (list_acmd != NULL) {
4791 printf("%s:%d:%d:%d: Command found on device queue\n",
4792 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4794 if (flag == SCB_ABORT) {
4795 TAILQ_REMOVE(&dev->busyq, list_acmd, acmd_links.tqe);
4796 cmd->result = DID_ABORT << 16;
4797 ahc_linux_queue_cmd_complete(ahc, cmd);
4803 if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
4804 && ahc_search_untagged_queues(ahc, cmd, cmd->device->id,
4805 cmd->device->channel + 'A',
4807 CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
4808 printf("%s:%d:%d:%d: Command found on untagged queue\n",
4809 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4816 * See if we can find a matching cmd in the pending list.
4818 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
4819 if (pending_scb->io_ctx == cmd)
4823 if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
4825 /* Any SCB for this device will do for a target reset */
4826 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
4827 if (ahc_match_scb(ahc, pending_scb, cmd->device->id,
4828 cmd->device->channel + 'A',
4830 SCB_LIST_NULL, ROLE_INITIATOR) == 0)
4835 if (pending_scb == NULL) {
4836 printf("%s:%d:%d:%d: Command not found\n",
4837 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4842 if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
4844 * We can't queue two recovery actions using the same SCB
4851 * Ensure that the card doesn't do anything
4852 * behind our back and that we didn't "just" miss
4853 * an interrupt that would affect this cmd.
4855 was_paused = ahc_is_paused(ahc);
4856 ahc_pause_and_flushwork(ahc);
4859 if ((pending_scb->flags & SCB_ACTIVE) == 0) {
4860 printf("%s:%d:%d:%d: Command already completed\n",
4861 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4866 printf("%s: At time of recovery, card was %spaused\n",
4867 ahc_name(ahc), was_paused ? "" : "not ");
4868 ahc_dump_card_state(ahc);
4870 disconnected = TRUE;
4871 if (flag == SCB_ABORT) {
4872 if (ahc_search_qinfifo(ahc, cmd->device->id,
4873 cmd->device->channel + 'A',
4875 pending_scb->hscb->tag,
4876 ROLE_INITIATOR, CAM_REQ_ABORTED,
4877 SEARCH_COMPLETE) > 0) {
4878 printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
4879 ahc_name(ahc), cmd->device->channel,
4880 cmd->device->id, cmd->device->lun);
4884 } else if (ahc_search_qinfifo(ahc, cmd->device->id,
4885 cmd->device->channel + 'A',
4886 cmd->device->lun, pending_scb->hscb->tag,
4887 ROLE_INITIATOR, /*status*/0,
4888 SEARCH_COUNT) > 0) {
4889 disconnected = FALSE;
4892 if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
4893 struct scb *bus_scb;
4895 bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
4896 if (bus_scb == pending_scb)
4897 disconnected = FALSE;
4898 else if (flag != SCB_ABORT
4899 && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
4900 && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb))
4901 disconnected = FALSE;
4905 * At this point, pending_scb is the scb associated with the
4906 * passed in command. That command is currently active on the
4907 * bus, is in the disconnected state, or we're hoping to find
4908 * a command for the same target active on the bus to abuse to
4909 * send a BDR. Queue the appropriate message based on which of
4910 * these states we are in.
4912 last_phase = ahc_inb(ahc, LASTPHASE);
4913 saved_scbptr = ahc_inb(ahc, SCBPTR);
4914 active_scb_index = ahc_inb(ahc, SCB_TAG);
4915 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
4916 if (last_phase != P_BUSFREE
4917 && (pending_scb->hscb->tag == active_scb_index
4918 || (flag == SCB_DEVICE_RESET
4919 && SCSIID_TARGET(ahc, saved_scsiid) == cmd->device->id))) {
4922 * We're active on the bus, so assert ATN
4923 * and hope that the target responds.
4925 pending_scb = ahc_lookup_scb(ahc, active_scb_index);
4926 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
4927 ahc_outb(ahc, MSG_OUT, HOST_MSG);
4928 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
4929 printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
4930 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4933 } else if (disconnected) {
4936 * Actually re-queue this SCB in an attempt
4937 * to select the device before it reconnects.
4938 * In either case (selection or reselection),
4939 * we will now issue the approprate message
4940 * to the timed-out device.
4942 * Set the MK_MESSAGE control bit indicating
4943 * that we desire to send a message. We
4944 * also set the disconnected flag since
4945 * in the paging case there is no guarantee
4946 * that our SCB control byte matches the
4947 * version on the card. We don't want the
4948 * sequencer to abort the command thinking
4949 * an unsolicited reselection occurred.
4951 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
4952 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
4955 * Remove any cached copy of this SCB in the
4956 * disconnected list in preparation for the
4957 * queuing of our abort SCB. We use the
4958 * same element in the SCB, SCB_NEXT, for
4959 * both the qinfifo and the disconnected list.
4961 ahc_search_disc_list(ahc, cmd->device->id,
4962 cmd->device->channel + 'A',
4963 cmd->device->lun, pending_scb->hscb->tag,
4964 /*stop_on_first*/TRUE,
4966 /*save_state*/FALSE);
4969 * In the non-paging case, the sequencer will
4970 * never re-reference the in-core SCB.
4971 * To make sure we are notified during
4972 * reslection, set the MK_MESSAGE flag in
4973 * the card's copy of the SCB.
4975 if ((ahc->flags & AHC_PAGESCBS) == 0) {
4976 ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
4977 ahc_outb(ahc, SCB_CONTROL,
4978 ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
4982 * Clear out any entries in the QINFIFO first
4983 * so we are the next SCB for this target
4986 ahc_search_qinfifo(ahc, cmd->device->id,
4987 cmd->device->channel + 'A',
4988 cmd->device->lun, SCB_LIST_NULL,
4989 ROLE_INITIATOR, CAM_REQUEUE_REQ,
4991 ahc_qinfifo_requeue_tail(ahc, pending_scb);
4992 ahc_outb(ahc, SCBPTR, saved_scbptr);
4993 ahc_print_path(ahc, pending_scb);
4994 printf("Device is disconnected, re-queuing SCB\n");
4997 printf("%s:%d:%d:%d: Unable to deliver message\n",
4998 ahc_name(ahc), cmd->device->channel, cmd->device->id,
5006 * Our assumption is that if we don't have the command, no
5007 * recovery action was required, so we return success. Again,
5008 * the semantics of the mid-layer recovery engine are not
5009 * well defined, so this may change in time.
5016 struct timer_list timer;
5019 ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE;
5020 spin_unlock_irq(&ahc->platform_data->spin_lock);
5022 timer.data = (u_long)ahc;
5023 timer.expires = jiffies + (5 * HZ);
5024 timer.function = ahc_linux_sem_timeout;
5026 printf("Recovery code sleeping\n");
5027 down(&ahc->platform_data->eh_sem);
5028 printf("Recovery code awake\n");
5029 ret = del_timer_sync(&timer);
5031 printf("Timer Expired\n");
5034 spin_lock_irq(&ahc->platform_data->spin_lock);
5036 ahc_schedule_runq(ahc);
5037 ahc_linux_run_complete_queue(ahc);
5038 ahc_midlayer_entrypoint_unlock(ahc, &s);
5043 ahc_platform_dump_card_state(struct ahc_softc *ahc)
5045 struct ahc_linux_device *dev;
5053 maxchannel = (ahc->features & AHC_TWIN) ? 1 : 0;
5054 maxtarget = (ahc->features & AHC_WIDE) ? 15 : 7;
5055 for (channel = 0; channel <= maxchannel; channel++) {
5057 for (target = 0; target <=maxtarget; target++) {
5059 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
5060 struct ahc_cmd *acmd;
5062 dev = ahc_linux_get_device(ahc, channel, target,
5063 lun, /*alloc*/FALSE);
5067 printf("DevQ(%d:%d:%d): ",
5068 channel, target, lun);
5070 TAILQ_FOREACH(acmd, &dev->busyq,
5072 if (i++ > AHC_SCB_MAX)
5075 printf("%d waiting\n", i);
5081 static void ahc_linux_exit(void);
5084 ahc_linux_init(void)
5086 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
5087 int rc = ahc_linux_detect(&aic7xxx_driver_template);
5093 scsi_register_module(MODULE_SCSI_HA, &aic7xxx_driver_template);
5094 if (aic7xxx_driver_template.present == 0) {
5095 scsi_unregister_module(MODULE_SCSI_HA,
5096 &aic7xxx_driver_template);
5105 ahc_linux_exit(void)
5107 struct ahc_softc *ahc;
5111 * Shutdown DV threads before going into the SCSI mid-layer.
5112 * This avoids situations where the mid-layer locks the entire
5113 * kernel so that waiting for our DV threads to exit leads
5117 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
5119 ahc_linux_kill_dv_thread(ahc);
5121 ahc_list_unlock(&l);
5123 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
5125 * In 2.4 we have to unregister from the PCI core _after_
5126 * unregistering from the scsi midlayer to avoid dangling
5129 scsi_unregister_module(MODULE_SCSI_HA, &aic7xxx_driver_template);
5131 ahc_linux_pci_exit();
5132 ahc_linux_eisa_exit();
5135 module_init(ahc_linux_init);
5136 module_exit(ahc_linux_exit);