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 #include <linux/init.h> /* __setup */
134 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
135 #include "sd.h" /* For geometry detection */
138 #include <linux/mm.h> /* For fetching system memory size */
139 #include <linux/blkdev.h> /* For block_size() */
140 #include <linux/delay.h> /* For ssleep/msleep */
143 * Lock protecting manipulation of the ahc softc list.
145 spinlock_t ahc_list_spinlock;
147 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
148 /* For dynamic sglist size calculation. */
149 u_int ahc_linux_nseg;
153 * Set this to the delay in seconds after SCSI bus reset.
154 * Note, we honor this only for the initial bus reset.
155 * The scsi error recovery code performs its own bus settle
156 * delay handling for error recovery actions.
158 #ifdef CONFIG_AIC7XXX_RESET_DELAY_MS
159 #define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS
161 #define AIC7XXX_RESET_DELAY 5000
165 * Control collection of SCSI transfer statistics for the /proc filesystem.
167 * NOTE: Do NOT enable this when running on kernels version 1.2.x and below.
168 * NOTE: This does affect performance since it has to maintain statistics.
170 #ifdef CONFIG_AIC7XXX_PROC_STATS
171 #define AIC7XXX_PROC_STATS
175 * To change the default number of tagged transactions allowed per-device,
176 * add a line to the lilo.conf file like:
177 * append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
178 * which will result in the first four devices on the first two
179 * controllers being set to a tagged queue depth of 32.
181 * The tag_commands is an array of 16 to allow for wide and twin adapters.
182 * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
186 uint8_t tag_commands[16]; /* Allow for wide/twin adapters. */
187 } adapter_tag_info_t;
190 * Modify this as you see fit for your system.
192 * 0 tagged queuing disabled
193 * 1 <= n <= 253 n == max tags ever dispatched.
195 * The driver will throttle the number of commands dispatched to a
196 * device if it returns queue full. For devices with a fixed maximum
197 * queue depth, the driver will eventually determine this depth and
198 * lock it in (a console message is printed to indicate that a lock
199 * has occurred). On some devices, queue full is returned for a temporary
200 * resource shortage. These devices will return queue full at varying
201 * depths. The driver will throttle back when the queue fulls occur and
202 * attempt to slowly increase the depth over time as the device recovers
203 * from the resource shortage.
205 * In this example, the first line will disable tagged queueing for all
206 * the devices on the first probed aic7xxx adapter.
208 * The second line enables tagged queueing with 4 commands/LUN for IDs
209 * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
210 * driver to attempt to use up to 64 tags for ID 1.
212 * The third line is the same as the first line.
214 * The fourth line disables tagged queueing for devices 0 and 3. It
215 * enables tagged queueing for the other IDs, with 16 commands/LUN
216 * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
217 * IDs 2, 5-7, and 9-15.
221 * NOTE: The below structure is for reference only, the actual structure
222 * to modify in order to change things is just below this comment block.
223 adapter_tag_info_t aic7xxx_tag_info[] =
225 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
226 {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
227 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
228 {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
232 #ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
233 #define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
235 #define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE
238 #define AIC7XXX_CONFIGED_TAG_COMMANDS { \
239 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
240 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
241 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
242 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
243 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
244 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
245 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
246 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE \
250 * By default, use the number of commands specified by
251 * the users kernel configuration.
253 static adapter_tag_info_t aic7xxx_tag_info[] =
255 {AIC7XXX_CONFIGED_TAG_COMMANDS},
256 {AIC7XXX_CONFIGED_TAG_COMMANDS},
257 {AIC7XXX_CONFIGED_TAG_COMMANDS},
258 {AIC7XXX_CONFIGED_TAG_COMMANDS},
259 {AIC7XXX_CONFIGED_TAG_COMMANDS},
260 {AIC7XXX_CONFIGED_TAG_COMMANDS},
261 {AIC7XXX_CONFIGED_TAG_COMMANDS},
262 {AIC7XXX_CONFIGED_TAG_COMMANDS},
263 {AIC7XXX_CONFIGED_TAG_COMMANDS},
264 {AIC7XXX_CONFIGED_TAG_COMMANDS},
265 {AIC7XXX_CONFIGED_TAG_COMMANDS},
266 {AIC7XXX_CONFIGED_TAG_COMMANDS},
267 {AIC7XXX_CONFIGED_TAG_COMMANDS},
268 {AIC7XXX_CONFIGED_TAG_COMMANDS},
269 {AIC7XXX_CONFIGED_TAG_COMMANDS},
270 {AIC7XXX_CONFIGED_TAG_COMMANDS}
276 * positive value = DV Enabled
278 * negative value = DV Default for adapter type/seeprom
280 #ifdef CONFIG_AIC7XXX_DV_SETTING
281 #define AIC7XXX_CONFIGED_DV CONFIG_AIC7XXX_DV_SETTING
283 #define AIC7XXX_CONFIGED_DV -1
286 static int8_t aic7xxx_dv_settings[] =
307 * There should be a specific return value for this in scsi.h, but
308 * it seems that most drivers ignore it.
310 #define DID_UNDERFLOW DID_ERROR
313 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
315 printk("(scsi%d:%c:%d:%d): ",
316 ahc->platform_data->host->host_no,
317 scb != NULL ? SCB_GET_CHANNEL(ahc, scb) : 'X',
318 scb != NULL ? SCB_GET_TARGET(ahc, scb) : -1,
319 scb != NULL ? SCB_GET_LUN(scb) : -1);
323 * XXX - these options apply unilaterally to _all_ 274x/284x/294x
324 * cards in the system. This should be fixed. Exceptions to this
325 * rule are noted in the comments.
329 * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This
330 * has no effect on any later resets that might occur due to things like
333 static uint32_t aic7xxx_no_reset;
336 * Certain PCI motherboards will scan PCI devices from highest to lowest,
337 * others scan from lowest to highest, and they tend to do all kinds of
338 * strange things when they come into contact with PCI bridge chips. The
339 * net result of all this is that the PCI card that is actually used to boot
340 * the machine is very hard to detect. Most motherboards go from lowest
341 * PCI slot number to highest, and the first SCSI controller found is the
342 * one you boot from. The only exceptions to this are when a controller
343 * has its BIOS disabled. So, we by default sort all of our SCSI controllers
344 * from lowest PCI slot number to highest PCI slot number. We also force
345 * all controllers with their BIOS disabled to the end of the list. This
346 * works on *almost* all computers. Where it doesn't work, we have this
347 * option. Setting this option to non-0 will reverse the order of the sort
348 * to highest first, then lowest, but will still leave cards with their BIOS
349 * disabled at the very end. That should fix everyone up unless there are
350 * really strange cirumstances.
352 static uint32_t aic7xxx_reverse_scan;
355 * Should we force EXTENDED translation on a controller.
356 * 0 == Use whatever is in the SEEPROM or default to off
357 * 1 == Use whatever is in the SEEPROM or default to on
359 static uint32_t aic7xxx_extended;
362 * PCI bus parity checking of the Adaptec controllers. This is somewhat
363 * dubious at best. To my knowledge, this option has never actually
364 * solved a PCI parity problem, but on certain machines with broken PCI
365 * chipset configurations where stray PCI transactions with bad parity are
366 * the norm rather than the exception, the error messages can be overwelming.
367 * It's included in the driver for completeness.
368 * 0 = Shut off PCI parity check
369 * non-0 = reverse polarity pci parity checking
371 static uint32_t aic7xxx_pci_parity = ~0;
374 * Certain newer motherboards have put new PCI based devices into the
375 * IO spaces that used to typically be occupied by VLB or EISA cards.
376 * This overlap can cause these newer motherboards to lock up when scanned
377 * for older EISA and VLB devices. Setting this option to non-0 will
378 * cause the driver to skip scanning for any VLB or EISA controllers and
379 * only support the PCI controllers. NOTE: this means that if the kernel
380 * os compiled with PCI support disabled, then setting this to non-0
381 * would result in never finding any devices :)
383 #ifndef CONFIG_AIC7XXX_PROBE_EISA_VL
384 uint32_t aic7xxx_probe_eisa_vl;
386 uint32_t aic7xxx_probe_eisa_vl = ~0;
390 * There are lots of broken chipsets in the world. Some of them will
391 * violate the PCI spec when we issue byte sized memory writes to our
392 * controller. I/O mapped register access, if allowed by the given
393 * platform, will work in almost all cases.
395 uint32_t aic7xxx_allow_memio = ~0;
398 * aic7xxx_detect() has been run, so register all device arrivals
399 * immediately with the system rather than deferring to the sorted
400 * attachment performed by aic7xxx_detect().
402 int aic7xxx_detect_complete;
405 * So that we can set how long each device is given as a selection timeout.
406 * The table of values goes like this:
411 * We default to 256ms because some older devices need a longer time
412 * to respond to initial selection.
414 static uint32_t aic7xxx_seltime;
417 * Certain devices do not perform any aging on commands. Should the
418 * device be saturated by commands in one portion of the disk, it is
419 * possible for transactions on far away sectors to never be serviced.
420 * To handle these devices, we can periodically send an ordered tag to
421 * force all outstanding transactions to be serviced prior to a new
424 uint32_t aic7xxx_periodic_otag;
427 * Module information and settable options.
429 static char *aic7xxx = NULL;
431 MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
432 MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
433 MODULE_LICENSE("Dual BSD/GPL");
434 MODULE_VERSION(AIC7XXX_DRIVER_VERSION);
435 module_param(aic7xxx, charp, 0444);
436 MODULE_PARM_DESC(aic7xxx,
437 "period delimited, options string.\n"
438 " verbose Enable verbose/diagnostic logging\n"
439 " allow_memio Allow device registers to be memory mapped\n"
440 " debug Bitmask of debug values to enable\n"
441 " no_probe Toggle EISA/VLB controller probing\n"
442 " probe_eisa_vl Toggle EISA/VLB controller probing\n"
443 " no_reset Supress initial bus resets\n"
444 " extended Enable extended geometry on all controllers\n"
445 " periodic_otag Send an ordered tagged transaction\n"
446 " periodically to prevent tag starvation.\n"
447 " This may be required by some older disk\n"
448 " drives or RAID arrays.\n"
449 " reverse_scan Sort PCI devices highest Bus/Slot to lowest\n"
450 " tag_info:<tag_str> Set per-target tag depth\n"
451 " global_tag_depth:<int> Global tag depth for every target\n"
453 " dv:<dv_settings> Set per-controller Domain Validation Setting.\n"
454 " seltime:<int> Selection Timeout\n"
455 " (0/256ms,1/128ms,2/64ms,3/32ms)\n"
457 " Sample /etc/modprobe.conf line:\n"
458 " Toggle EISA/VLB probing\n"
459 " Set tag depth on Controller 1/Target 1 to 10 tags\n"
460 " Shorten the selection timeout to 128ms\n"
462 " options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n"
465 static void ahc_linux_handle_scsi_status(struct ahc_softc *,
466 struct ahc_linux_device *,
468 static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
470 static void ahc_linux_filter_inquiry(struct ahc_softc*, struct ahc_devinfo*);
471 static void ahc_linux_sem_timeout(u_long arg);
472 static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
473 static void ahc_linux_release_simq(u_long arg);
474 static void ahc_linux_dev_timed_unfreeze(u_long arg);
475 static int ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag);
476 static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
477 static void ahc_linux_size_nseg(void);
478 static void ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc);
479 static void ahc_linux_start_dv(struct ahc_softc *ahc);
480 static void ahc_linux_dv_timeout(struct scsi_cmnd *cmd);
481 static int ahc_linux_dv_thread(void *data);
482 static void ahc_linux_kill_dv_thread(struct ahc_softc *ahc);
483 static void ahc_linux_dv_target(struct ahc_softc *ahc, u_int target);
484 static void ahc_linux_dv_transition(struct ahc_softc *ahc,
485 struct scsi_cmnd *cmd,
486 struct ahc_devinfo *devinfo,
487 struct ahc_linux_target *targ);
488 static void ahc_linux_dv_fill_cmd(struct ahc_softc *ahc,
489 struct scsi_cmnd *cmd,
490 struct ahc_devinfo *devinfo);
491 static void ahc_linux_dv_inq(struct ahc_softc *ahc,
492 struct scsi_cmnd *cmd,
493 struct ahc_devinfo *devinfo,
494 struct ahc_linux_target *targ,
495 u_int request_length);
496 static void ahc_linux_dv_tur(struct ahc_softc *ahc,
497 struct scsi_cmnd *cmd,
498 struct ahc_devinfo *devinfo);
499 static void ahc_linux_dv_rebd(struct ahc_softc *ahc,
500 struct scsi_cmnd *cmd,
501 struct ahc_devinfo *devinfo,
502 struct ahc_linux_target *targ);
503 static void ahc_linux_dv_web(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_reb(struct ahc_softc *ahc,
508 struct scsi_cmnd *cmd,
509 struct ahc_devinfo *devinfo,
510 struct ahc_linux_target *targ);
511 static void ahc_linux_dv_su(struct ahc_softc *ahc,
512 struct scsi_cmnd *cmd,
513 struct ahc_devinfo *devinfo,
514 struct ahc_linux_target *targ);
515 static int ahc_linux_fallback(struct ahc_softc *ahc,
516 struct ahc_devinfo *devinfo);
517 static void ahc_linux_dv_complete(Scsi_Cmnd *cmd);
518 static void ahc_linux_generate_dv_pattern(struct ahc_linux_target *targ);
519 static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
520 struct ahc_devinfo *devinfo);
521 static u_int ahc_linux_user_dv_setting(struct ahc_softc *ahc);
522 static void ahc_linux_device_queue_depth(struct ahc_softc *ahc,
523 struct ahc_linux_device *dev);
524 static struct ahc_linux_target* ahc_linux_alloc_target(struct ahc_softc*,
526 static void ahc_linux_free_target(struct ahc_softc*,
527 struct ahc_linux_target*);
528 static struct ahc_linux_device* ahc_linux_alloc_device(struct ahc_softc*,
529 struct ahc_linux_target*,
531 static void ahc_linux_free_device(struct ahc_softc*,
532 struct ahc_linux_device*);
533 static void ahc_linux_run_device_queue(struct ahc_softc*,
534 struct ahc_linux_device*);
535 static void ahc_linux_setup_tag_info_global(char *p);
536 static aic_option_callback_t ahc_linux_setup_tag_info;
537 static aic_option_callback_t ahc_linux_setup_dv;
538 static int aic7xxx_setup(char *s);
539 static int ahc_linux_next_unit(void);
540 static void ahc_runq_tasklet(unsigned long data);
541 static struct ahc_cmd *ahc_linux_run_complete_queue(struct ahc_softc *ahc);
543 /********************************* Inlines ************************************/
544 static __inline void ahc_schedule_runq(struct ahc_softc *ahc);
545 static __inline struct ahc_linux_device*
546 ahc_linux_get_device(struct ahc_softc *ahc, u_int channel,
547 u_int target, u_int lun, int alloc);
548 static __inline void ahc_schedule_completeq(struct ahc_softc *ahc);
549 static __inline void ahc_linux_check_device_queue(struct ahc_softc *ahc,
550 struct ahc_linux_device *dev);
551 static __inline struct ahc_linux_device *
552 ahc_linux_next_device_to_run(struct ahc_softc *ahc);
553 static __inline void ahc_linux_run_device_queues(struct ahc_softc *ahc);
554 static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
556 static __inline int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
557 struct ahc_dma_seg *sg,
558 dma_addr_t addr, bus_size_t len);
561 ahc_schedule_completeq(struct ahc_softc *ahc)
563 if ((ahc->platform_data->flags & AHC_RUN_CMPLT_Q_TIMER) == 0) {
564 ahc->platform_data->flags |= AHC_RUN_CMPLT_Q_TIMER;
565 ahc->platform_data->completeq_timer.expires = jiffies;
566 add_timer(&ahc->platform_data->completeq_timer);
571 * Must be called with our lock held.
574 ahc_schedule_runq(struct ahc_softc *ahc)
576 tasklet_schedule(&ahc->platform_data->runq_tasklet);
579 static __inline struct ahc_linux_device*
580 ahc_linux_get_device(struct ahc_softc *ahc, u_int channel, u_int target,
581 u_int lun, int alloc)
583 struct ahc_linux_target *targ;
584 struct ahc_linux_device *dev;
587 target_offset = target;
590 targ = ahc->platform_data->targets[target_offset];
593 targ = ahc_linux_alloc_target(ahc, channel, target);
599 dev = targ->devices[lun];
600 if (dev == NULL && alloc != 0)
601 dev = ahc_linux_alloc_device(ahc, targ, lun);
605 #define AHC_LINUX_MAX_RETURNED_ERRORS 4
606 static struct ahc_cmd *
607 ahc_linux_run_complete_queue(struct ahc_softc *ahc)
609 struct ahc_cmd *acmd;
614 ahc_done_lock(ahc, &done_flags);
615 while ((acmd = TAILQ_FIRST(&ahc->platform_data->completeq)) != NULL) {
618 if (with_errors > AHC_LINUX_MAX_RETURNED_ERRORS) {
620 * Linux uses stack recursion to requeue
621 * commands that need to be retried. Avoid
622 * blowing out the stack by "spoon feeding"
623 * commands that completed with error back
624 * the operating system in case they are going
625 * to be retried. "ick"
627 ahc_schedule_completeq(ahc);
630 TAILQ_REMOVE(&ahc->platform_data->completeq,
631 acmd, acmd_links.tqe);
632 cmd = &acmd_scsi_cmd(acmd);
633 cmd->host_scribble = NULL;
634 if (ahc_cmd_get_transaction_status(cmd) != DID_OK
635 || (cmd->result & 0xFF) != SCSI_STATUS_OK)
640 ahc_done_unlock(ahc, &done_flags);
645 ahc_linux_check_device_queue(struct ahc_softc *ahc,
646 struct ahc_linux_device *dev)
648 if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) != 0
649 && dev->active == 0) {
650 dev->flags &= ~AHC_DEV_FREEZE_TIL_EMPTY;
654 if (TAILQ_FIRST(&dev->busyq) == NULL
655 || dev->openings == 0 || dev->qfrozen != 0)
658 ahc_linux_run_device_queue(ahc, dev);
661 static __inline struct ahc_linux_device *
662 ahc_linux_next_device_to_run(struct ahc_softc *ahc)
665 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0
666 || (ahc->platform_data->qfrozen != 0
667 && AHC_DV_SIMQ_FROZEN(ahc) == 0))
669 return (TAILQ_FIRST(&ahc->platform_data->device_runq));
673 ahc_linux_run_device_queues(struct ahc_softc *ahc)
675 struct ahc_linux_device *dev;
677 while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) {
678 TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links);
679 dev->flags &= ~AHC_DEV_ON_RUN_LIST;
680 ahc_linux_check_device_queue(ahc, dev);
685 ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
690 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
691 if (cmd->use_sg != 0) {
692 struct scatterlist *sg;
694 sg = (struct scatterlist *)cmd->request_buffer;
695 pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg,
696 cmd->sc_data_direction);
697 } else if (cmd->request_bufflen != 0) {
698 pci_unmap_single(ahc->dev_softc,
699 scb->platform_data->buf_busaddr,
700 cmd->request_bufflen,
701 cmd->sc_data_direction);
706 ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
707 struct ahc_dma_seg *sg, dma_addr_t addr, bus_size_t len)
711 if ((scb->sg_count + 1) > AHC_NSEG)
712 panic("Too few segs for dma mapping. "
713 "Increase AHC_NSEG\n");
716 sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
717 scb->platform_data->xfer_len += len;
719 if (sizeof(dma_addr_t) > 4
720 && (ahc->flags & AHC_39BIT_ADDRESSING) != 0)
721 len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK;
723 sg->len = ahc_htole32(len);
727 /************************ Host template entry points *************************/
728 static int ahc_linux_detect(Scsi_Host_Template *);
729 static int ahc_linux_queue(Scsi_Cmnd *, void (*)(Scsi_Cmnd *));
730 static const char *ahc_linux_info(struct Scsi_Host *);
731 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
732 static int ahc_linux_slave_alloc(Scsi_Device *);
733 static int ahc_linux_slave_configure(Scsi_Device *);
734 static void ahc_linux_slave_destroy(Scsi_Device *);
735 #if defined(__i386__)
736 static int ahc_linux_biosparam(struct scsi_device*,
737 struct block_device*,
741 static int ahc_linux_release(struct Scsi_Host *);
742 static void ahc_linux_select_queue_depth(struct Scsi_Host *host,
743 Scsi_Device *scsi_devs);
744 #if defined(__i386__)
745 static int ahc_linux_biosparam(Disk *, kdev_t, int[]);
748 static int ahc_linux_bus_reset(Scsi_Cmnd *);
749 static int ahc_linux_dev_reset(Scsi_Cmnd *);
750 static int ahc_linux_abort(Scsi_Cmnd *);
753 * Calculate a safe value for AHC_NSEG (as expressed through ahc_linux_nseg).
756 * The midlayer allocates an S/G array dynamically when a command is issued
757 * using SCSI malloc. This array, which is in an OS dependent format that
758 * must later be copied to our private S/G list, is sized to house just the
759 * number of segments needed for the current transfer. Since the code that
760 * sizes the SCSI malloc pool does not take into consideration fragmentation
761 * of the pool, executing transactions numbering just a fraction of our
762 * concurrent transaction limit with list lengths aproaching AHC_NSEG will
763 * quickly depleat the SCSI malloc pool of usable space. Unfortunately, the
764 * mid-layer does not properly handle this scsi malloc failures for the S/G
765 * array and the result can be a lockup of the I/O subsystem. We try to size
766 * our S/G list so that it satisfies our drivers allocation requirements in
767 * addition to avoiding fragmentation of the SCSI malloc pool.
770 ahc_linux_size_nseg(void)
772 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
777 * The SCSI allocator rounds to the nearest 512 bytes
778 * an cannot allocate across a page boundary. Our algorithm
779 * is to start at 1K of scsi malloc space per-command and
780 * loop through all factors of the PAGE_SIZE and pick the best.
783 for (cur_size = 1024; cur_size <= PAGE_SIZE; cur_size *= 2) {
786 nseg = cur_size / sizeof(struct scatterlist);
787 if (nseg < AHC_LINUX_MIN_NSEG)
790 if (best_size == 0) {
791 best_size = cur_size;
792 ahc_linux_nseg = nseg;
798 * Compare the traits of the current "best_size"
799 * with the current size to determine if the
800 * current size is a better size.
802 best_rem = best_size % sizeof(struct scatterlist);
803 cur_rem = cur_size % sizeof(struct scatterlist);
804 if (cur_rem < best_rem) {
805 best_size = cur_size;
806 ahc_linux_nseg = nseg;
814 * Try to detect an Adaptec 7XXX controller.
817 ahc_linux_detect(Scsi_Host_Template *template)
819 struct ahc_softc *ahc;
822 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
824 * It is a bug that the upper layer takes
825 * this lock just prior to calling us.
827 spin_unlock_irq(&io_request_lock);
831 * Sanity checking of Linux SCSI data structures so
832 * that some of our hacks^H^H^H^H^Hassumptions aren't
835 if (offsetof(struct ahc_cmd_internal, end)
836 > offsetof(struct scsi_cmnd, host_scribble)) {
837 printf("ahc_linux_detect: SCSI data structures changed.\n");
838 printf("ahc_linux_detect: Unable to attach\n");
841 ahc_linux_size_nseg();
843 * If we've been passed any parameters, process them now.
846 aic7xxx_setup(aic7xxx);
848 template->proc_name = "aic7xxx";
851 * Initialize our softc list lock prior to
852 * probing for any adapters.
856 found = ahc_linux_pci_init();
857 if (!ahc_linux_eisa_init())
861 * Register with the SCSI layer all
862 * controllers we've found.
864 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
866 if (ahc_linux_register_host(ahc, template) == 0)
870 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
871 spin_lock_irq(&io_request_lock);
873 aic7xxx_detect_complete++;
878 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
880 * Free the passed in Scsi_Host memory structures prior to unloading the
884 ahc_linux_release(struct Scsi_Host * host)
886 struct ahc_softc *ahc;
893 * We should be able to just perform
894 * the free directly, but check our
895 * list for extra sanity.
897 ahc = ahc_find_softc(*(struct ahc_softc **)host->hostdata);
902 ahc_intr_enable(ahc, FALSE);
913 * Return a string describing the driver.
916 ahc_linux_info(struct Scsi_Host *host)
918 static char buffer[512];
921 struct ahc_softc *ahc;
924 ahc = *(struct ahc_softc **)host->hostdata;
925 memset(bp, 0, sizeof(buffer));
926 strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev ");
927 strcat(bp, AIC7XXX_DRIVER_VERSION);
930 strcat(bp, ahc->description);
933 ahc_controller_info(ahc, ahc_info);
934 strcat(bp, ahc_info);
941 * Queue an SCB to the controller.
944 ahc_linux_queue(Scsi_Cmnd * cmd, void (*scsi_done) (Scsi_Cmnd *))
946 struct ahc_softc *ahc;
947 struct ahc_linux_device *dev;
950 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
953 * Save the callback on completion function.
955 cmd->scsi_done = scsi_done;
957 ahc_midlayer_entrypoint_lock(ahc, &flags);
960 * Close the race of a command that was in the process of
961 * being queued to us just as our simq was frozen. Let
962 * DV commands through so long as we are only frozen to
965 if (ahc->platform_data->qfrozen != 0
966 && AHC_DV_CMD(cmd) == 0) {
968 ahc_cmd_set_transaction_status(cmd, CAM_REQUEUE_REQ);
969 ahc_linux_queue_cmd_complete(ahc, cmd);
970 ahc_schedule_completeq(ahc);
971 ahc_midlayer_entrypoint_unlock(ahc, &flags);
974 dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
975 cmd->device->lun, /*alloc*/TRUE);
977 ahc_cmd_set_transaction_status(cmd, CAM_RESRC_UNAVAIL);
978 ahc_linux_queue_cmd_complete(ahc, cmd);
979 ahc_schedule_completeq(ahc);
980 ahc_midlayer_entrypoint_unlock(ahc, &flags);
981 printf("%s: aic7xxx_linux_queue - Unable to allocate device!\n",
985 cmd->result = CAM_REQ_INPROG << 16;
986 TAILQ_INSERT_TAIL(&dev->busyq, (struct ahc_cmd *)cmd, acmd_links.tqe);
987 if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) {
988 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links);
989 dev->flags |= AHC_DEV_ON_RUN_LIST;
990 ahc_linux_run_device_queues(ahc);
992 ahc_midlayer_entrypoint_unlock(ahc, &flags);
996 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
998 ahc_linux_slave_alloc(Scsi_Device *device)
1000 struct ahc_softc *ahc;
1002 ahc = *((struct ahc_softc **)device->host->hostdata);
1004 printf("%s: Slave Alloc %d\n", ahc_name(ahc), device->id);
1009 ahc_linux_slave_configure(Scsi_Device *device)
1011 struct ahc_softc *ahc;
1012 struct ahc_linux_device *dev;
1015 ahc = *((struct ahc_softc **)device->host->hostdata);
1017 printf("%s: Slave Configure %d\n", ahc_name(ahc), device->id);
1018 ahc_midlayer_entrypoint_lock(ahc, &flags);
1020 * Since Linux has attached to the device, configure
1021 * it so we don't free and allocate the device
1022 * structure on every command.
1024 dev = ahc_linux_get_device(ahc, device->channel,
1025 device->id, device->lun,
1028 dev->flags &= ~AHC_DEV_UNCONFIGURED;
1029 dev->scsi_device = device;
1030 ahc_linux_device_queue_depth(ahc, dev);
1032 ahc_midlayer_entrypoint_unlock(ahc, &flags);
1037 ahc_linux_slave_destroy(Scsi_Device *device)
1039 struct ahc_softc *ahc;
1040 struct ahc_linux_device *dev;
1043 ahc = *((struct ahc_softc **)device->host->hostdata);
1045 printf("%s: Slave Destroy %d\n", ahc_name(ahc), device->id);
1046 ahc_midlayer_entrypoint_lock(ahc, &flags);
1047 dev = ahc_linux_get_device(ahc, device->channel,
1048 device->id, device->lun,
1051 * Filter out "silly" deletions of real devices by only
1052 * deleting devices that have had slave_configure()
1053 * called on them. All other devices that have not
1054 * been configured will automatically be deleted by
1055 * the refcounting process.
1058 && (dev->flags & AHC_DEV_SLAVE_CONFIGURED) != 0) {
1059 dev->flags |= AHC_DEV_UNCONFIGURED;
1060 if (TAILQ_EMPTY(&dev->busyq)
1062 && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0)
1063 ahc_linux_free_device(ahc, dev);
1065 ahc_midlayer_entrypoint_unlock(ahc, &flags);
1069 * Sets the queue depth for each SCSI device hanging
1070 * off the input host adapter.
1073 ahc_linux_select_queue_depth(struct Scsi_Host *host, Scsi_Device *scsi_devs)
1075 Scsi_Device *device;
1077 struct ahc_softc *ahc;
1080 ahc = *((struct ahc_softc **)host->hostdata);
1081 ahc_lock(ahc, &flags);
1082 for (device = scsi_devs; device != NULL; device = device->next) {
1085 * Watch out for duplicate devices. This works around
1086 * some quirks in how the SCSI scanning code does its
1087 * device management.
1089 for (ldev = scsi_devs; ldev != device; ldev = ldev->next) {
1090 if (ldev->host == device->host
1091 && ldev->channel == device->channel
1092 && ldev->id == device->id
1093 && ldev->lun == device->lun)
1096 /* Skip duplicate. */
1100 if (device->host == host) {
1101 struct ahc_linux_device *dev;
1104 * Since Linux has attached to the device, configure
1105 * it so we don't free and allocate the device
1106 * structure on every command.
1108 dev = ahc_linux_get_device(ahc, device->channel,
1109 device->id, device->lun,
1112 dev->flags &= ~AHC_DEV_UNCONFIGURED;
1113 dev->scsi_device = device;
1114 ahc_linux_device_queue_depth(ahc, dev);
1115 device->queue_depth = dev->openings
1117 if ((dev->flags & (AHC_DEV_Q_BASIC
1118 | AHC_DEV_Q_TAGGED)) == 0) {
1120 * We allow the OS to queue 2 untagged
1121 * transactions to us at any time even
1122 * though we can only execute them
1123 * serially on the controller/device.
1124 * This should remove some latency.
1126 device->queue_depth = 2;
1131 ahc_unlock(ahc, &flags);
1135 #if defined(__i386__)
1137 * Return the disk geometry for the given SCSI device.
1140 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1141 ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1142 sector_t capacity, int geom[])
1146 ahc_linux_biosparam(Disk *disk, kdev_t dev, int geom[])
1148 struct scsi_device *sdev = disk->device;
1149 u_long capacity = disk->capacity;
1150 struct buffer_head *bh;
1157 struct ahc_softc *ahc;
1160 ahc = *((struct ahc_softc **)sdev->host->hostdata);
1161 channel = sdev->channel;
1163 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1164 bh = scsi_bios_ptable(bdev);
1165 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,17)
1166 bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, block_size(dev));
1168 bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, 1024);
1172 ret = scsi_partsize(bh, capacity,
1173 &geom[2], &geom[0], &geom[1]);
1174 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1184 cylinders = aic_sector_div(capacity, heads, sectors);
1186 if (aic7xxx_extended != 0)
1188 else if (channel == 0)
1189 extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
1191 extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
1192 if (extended && cylinders >= 1024) {
1195 cylinders = aic_sector_div(capacity, heads, sectors);
1199 geom[2] = cylinders;
1205 * Abort the current SCSI command(s).
1208 ahc_linux_abort(Scsi_Cmnd *cmd)
1212 error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
1214 printf("aic7xxx_abort returns 0x%x\n", error);
1219 * Attempt to send a target reset message to the device that timed out.
1222 ahc_linux_dev_reset(Scsi_Cmnd *cmd)
1226 error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
1228 printf("aic7xxx_dev_reset returns 0x%x\n", error);
1233 * Reset the SCSI bus.
1236 ahc_linux_bus_reset(Scsi_Cmnd *cmd)
1238 struct ahc_softc *ahc;
1242 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
1243 ahc_midlayer_entrypoint_lock(ahc, &s);
1244 found = ahc_reset_channel(ahc, cmd->device->channel + 'A',
1245 /*initiate reset*/TRUE);
1246 ahc_linux_run_complete_queue(ahc);
1247 ahc_midlayer_entrypoint_unlock(ahc, &s);
1250 printf("%s: SCSI bus reset delivered. "
1251 "%d SCBs aborted.\n", ahc_name(ahc), found);
1256 Scsi_Host_Template aic7xxx_driver_template = {
1257 .module = THIS_MODULE,
1259 .proc_info = ahc_linux_proc_info,
1260 .info = ahc_linux_info,
1261 .queuecommand = ahc_linux_queue,
1262 .eh_abort_handler = ahc_linux_abort,
1263 .eh_device_reset_handler = ahc_linux_dev_reset,
1264 .eh_bus_reset_handler = ahc_linux_bus_reset,
1265 #if defined(__i386__)
1266 .bios_param = ahc_linux_biosparam,
1268 .can_queue = AHC_MAX_QUEUE,
1271 .use_clustering = ENABLE_CLUSTERING,
1272 .slave_alloc = ahc_linux_slave_alloc,
1273 .slave_configure = ahc_linux_slave_configure,
1274 .slave_destroy = ahc_linux_slave_destroy,
1277 /**************************** Tasklet Handler *********************************/
1280 * In 2.4.X and above, this routine is called from a tasklet,
1281 * so we must re-acquire our lock prior to executing this code.
1282 * In all prior kernels, ahc_schedule_runq() calls this routine
1283 * directly and ahc_schedule_runq() is called with our lock held.
1286 ahc_runq_tasklet(unsigned long data)
1288 struct ahc_softc* ahc;
1289 struct ahc_linux_device *dev;
1292 ahc = (struct ahc_softc *)data;
1293 ahc_lock(ahc, &flags);
1294 while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) {
1296 TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links);
1297 dev->flags &= ~AHC_DEV_ON_RUN_LIST;
1298 ahc_linux_check_device_queue(ahc, dev);
1299 /* Yeild to our interrupt handler */
1300 ahc_unlock(ahc, &flags);
1301 ahc_lock(ahc, &flags);
1303 ahc_unlock(ahc, &flags);
1306 /******************************** Macros **************************************/
1307 #define BUILD_SCSIID(ahc, cmd) \
1308 ((((cmd)->device->id << TID_SHIFT) & TID) \
1309 | (((cmd)->device->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \
1310 | (((cmd)->device->channel == 0) ? 0 : TWIN_CHNLB))
1312 /******************************** Bus DMA *************************************/
1314 ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent,
1315 bus_size_t alignment, bus_size_t boundary,
1316 dma_addr_t lowaddr, dma_addr_t highaddr,
1317 bus_dma_filter_t *filter, void *filterarg,
1318 bus_size_t maxsize, int nsegments,
1319 bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
1323 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
1328 * Linux is very simplistic about DMA memory. For now don't
1329 * maintain all specification information. Once Linux supplies
1330 * better facilities for doing these operations, or the
1331 * needs of this particular driver change, we might need to do
1334 dmat->alignment = alignment;
1335 dmat->boundary = boundary;
1336 dmat->maxsize = maxsize;
1342 ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
1344 free(dmat, M_DEVBUF);
1348 ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
1349 int flags, bus_dmamap_t *mapp)
1353 map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT);
1357 * Although we can dma data above 4GB, our
1358 * "consistent" memory is below 4GB for
1359 * space efficiency reasons (only need a 4byte
1360 * address). For this reason, we have to reset
1361 * our dma mask when doing allocations.
1363 if (ahc->dev_softc != NULL)
1364 if (pci_set_dma_mask(ahc->dev_softc, 0xFFFFFFFF)) {
1365 printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
1369 *vaddr = pci_alloc_consistent(ahc->dev_softc,
1370 dmat->maxsize, &map->bus_addr);
1371 if (ahc->dev_softc != NULL)
1372 if (pci_set_dma_mask(ahc->dev_softc,
1373 ahc->platform_data->hw_dma_mask)) {
1374 printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
1385 ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
1386 void* vaddr, bus_dmamap_t map)
1388 pci_free_consistent(ahc->dev_softc, dmat->maxsize,
1389 vaddr, map->bus_addr);
1393 ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map,
1394 void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
1395 void *cb_arg, int flags)
1398 * Assume for now that this will only be used during
1399 * initialization and not for per-transaction buffer mapping.
1401 bus_dma_segment_t stack_sg;
1403 stack_sg.ds_addr = map->bus_addr;
1404 stack_sg.ds_len = dmat->maxsize;
1405 cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
1410 ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
1413 * The map may is NULL in our < 2.3.X implementation.
1414 * Now it's 2.6.5, but just in case...
1416 BUG_ON(map == NULL);
1417 free(map, M_DEVBUF);
1421 ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
1427 /********************* Platform Dependent Functions ***************************/
1429 * Compare "left hand" softc with "right hand" softc, returning:
1430 * < 0 - lahc has a lower priority than rahc
1431 * 0 - Softcs are equal
1432 * > 0 - lahc has a higher priority than rahc
1435 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1442 * Under Linux, cards are ordered as follows:
1443 * 1) VLB/EISA BIOS enabled devices sorted by BIOS address.
1444 * 2) PCI devices with BIOS enabled sorted by bus/slot/func.
1445 * 3) All remaining VLB/EISA devices sorted by ioport.
1446 * 4) All remaining PCI devices sorted by bus/slot/func.
1448 value = (lahc->flags & AHC_BIOS_ENABLED)
1449 - (rahc->flags & AHC_BIOS_ENABLED);
1451 /* Controllers with BIOS enabled have a *higher* priority */
1455 * Same BIOS setting, now sort based on bus type.
1456 * EISA and VL controllers sort together. EISA/VL
1457 * have higher priority than PCI.
1459 rvalue = (rahc->chip & AHC_BUS_MASK);
1460 if (rvalue == AHC_VL)
1462 lvalue = (lahc->chip & AHC_BUS_MASK);
1463 if (lvalue == AHC_VL)
1465 value = rvalue - lvalue;
1469 /* Still equal. Sort by BIOS address, ioport, or bus/slot/func. */
1474 char primary_channel;
1476 if (aic7xxx_reverse_scan != 0)
1477 value = ahc_get_pci_bus(lahc->dev_softc)
1478 - ahc_get_pci_bus(rahc->dev_softc);
1480 value = ahc_get_pci_bus(rahc->dev_softc)
1481 - ahc_get_pci_bus(lahc->dev_softc);
1484 if (aic7xxx_reverse_scan != 0)
1485 value = ahc_get_pci_slot(lahc->dev_softc)
1486 - ahc_get_pci_slot(rahc->dev_softc);
1488 value = ahc_get_pci_slot(rahc->dev_softc)
1489 - ahc_get_pci_slot(lahc->dev_softc);
1493 * On multi-function devices, the user can choose
1494 * to have function 1 probed before function 0.
1495 * Give whichever channel is the primary channel
1496 * the highest priority.
1498 primary_channel = (lahc->flags & AHC_PRIMARY_CHANNEL) + 'A';
1500 if (lahc->channel == primary_channel)
1506 if ((rahc->flags & AHC_BIOS_ENABLED) != 0) {
1507 value = rahc->platform_data->bios_address
1508 - lahc->platform_data->bios_address;
1510 value = rahc->bsh.ioport
1515 panic("ahc_softc_sort: invalid bus type");
1521 ahc_linux_setup_tag_info_global(char *p)
1525 tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
1526 printf("Setting Global Tags= %d\n", tags);
1528 for (i = 0; i < NUM_ELEMENTS(aic7xxx_tag_info); i++) {
1529 for (j = 0; j < AHC_NUM_TARGETS; j++) {
1530 aic7xxx_tag_info[i].tag_commands[j] = tags;
1536 ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
1539 if ((instance >= 0) && (targ >= 0)
1540 && (instance < NUM_ELEMENTS(aic7xxx_tag_info))
1541 && (targ < AHC_NUM_TARGETS)) {
1542 aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff;
1544 printf("tag_info[%d:%d] = %d\n", instance, targ, value);
1549 ahc_linux_setup_dv(u_long arg, int instance, int targ, int32_t value)
1553 && (instance < NUM_ELEMENTS(aic7xxx_dv_settings))) {
1554 aic7xxx_dv_settings[instance] = value;
1556 printf("dv[%d] = %d\n", instance, value);
1561 * Handle Linux boot parameters. This routine allows for assigning a value
1562 * to a parameter with a ':' between the parameter and the value.
1563 * ie. aic7xxx=stpwlev:1,extended
1566 aic7xxx_setup(char *s)
1576 { "extended", &aic7xxx_extended },
1577 { "no_reset", &aic7xxx_no_reset },
1578 { "verbose", &aic7xxx_verbose },
1579 { "allow_memio", &aic7xxx_allow_memio},
1581 { "debug", &ahc_debug },
1583 { "reverse_scan", &aic7xxx_reverse_scan },
1584 { "no_probe", &aic7xxx_probe_eisa_vl },
1585 { "probe_eisa_vl", &aic7xxx_probe_eisa_vl },
1586 { "periodic_otag", &aic7xxx_periodic_otag },
1587 { "pci_parity", &aic7xxx_pci_parity },
1588 { "seltime", &aic7xxx_seltime },
1589 { "tag_info", NULL },
1590 { "global_tag_depth", NULL },
1594 end = strchr(s, '\0');
1597 * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
1598 * will never be 0 in this case.
1602 while ((p = strsep(&s, ",.")) != NULL) {
1605 for (i = 0; i < NUM_ELEMENTS(options); i++) {
1607 n = strlen(options[i].name);
1608 if (strncmp(options[i].name, p, n) == 0)
1611 if (i == NUM_ELEMENTS(options))
1614 if (strncmp(p, "global_tag_depth", n) == 0) {
1615 ahc_linux_setup_tag_info_global(p + n);
1616 } else if (strncmp(p, "tag_info", n) == 0) {
1617 s = aic_parse_brace_option("tag_info", p + n, end,
1618 2, ahc_linux_setup_tag_info, 0);
1619 } else if (strncmp(p, "dv", n) == 0) {
1620 s = aic_parse_brace_option("dv", p + n, end, 1,
1621 ahc_linux_setup_dv, 0);
1622 } else if (p[n] == ':') {
1623 *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
1624 } else if (strncmp(p, "verbose", n) == 0) {
1625 *(options[i].flag) = 1;
1627 *(options[i].flag) ^= 0xFFFFFFFF;
1633 __setup("aic7xxx=", aic7xxx_setup);
1635 uint32_t aic7xxx_verbose;
1638 ahc_linux_register_host(struct ahc_softc *ahc, Scsi_Host_Template *template)
1641 struct Scsi_Host *host;
1646 template->name = ahc->description;
1647 host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
1651 *((struct ahc_softc **)host->hostdata) = ahc;
1653 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1654 scsi_assign_lock(host, &ahc->platform_data->spin_lock);
1655 #elif AHC_SCSI_HAS_HOST_LOCK != 0
1656 host->lock = &ahc->platform_data->spin_lock;
1658 ahc->platform_data->host = host;
1659 host->can_queue = AHC_MAX_QUEUE;
1660 host->cmd_per_lun = 2;
1661 /* XXX No way to communicate the ID for multiple channels */
1662 host->this_id = ahc->our_id;
1663 host->irq = ahc->platform_data->irq;
1664 host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
1665 host->max_lun = AHC_NUM_LUNS;
1666 host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
1667 host->sg_tablesize = AHC_NSEG;
1668 ahc_set_unit(ahc, ahc_linux_next_unit());
1669 sprintf(buf, "scsi%d", host->host_no);
1670 new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
1671 if (new_name != NULL) {
1672 strcpy(new_name, buf);
1673 ahc_set_name(ahc, new_name);
1675 host->unique_id = ahc->unit;
1676 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1677 scsi_set_pci_device(host, ahc->dev_softc);
1679 ahc_linux_initialize_scsi_bus(ahc);
1680 ahc_unlock(ahc, &s);
1681 ahc->platform_data->dv_pid = kernel_thread(ahc_linux_dv_thread, ahc, 0);
1683 if (ahc->platform_data->dv_pid < 0) {
1684 printf("%s: Failed to create DV thread, error= %d\n",
1685 ahc_name(ahc), ahc->platform_data->dv_pid);
1686 return (-ahc->platform_data->dv_pid);
1689 * Initially allocate *all* of our linux target objects
1690 * so that the DV thread will scan them all in parallel
1691 * just after driver initialization. Any device that
1692 * does not exist will have its target object destroyed
1693 * by the selection timeout handler. In the case of a
1694 * device that appears after the initial DV scan, async
1695 * negotiation will occur for the first command, and DV
1696 * will comence should that first command be successful.
1698 for (targ_offset = 0;
1699 targ_offset < host->max_id * (host->max_channel + 1);
1705 target = targ_offset;
1707 && (ahc->features & AHC_TWIN) != 0) {
1712 * Skip our own ID. Some Compaq/HP storage devices
1713 * have enclosure management devices that respond to
1714 * single bit selection (i.e. selecting ourselves).
1715 * It is expected that either an external application
1716 * or a modified kernel will be used to probe this
1717 * ID if it is appropriate. To accommodate these
1718 * installations, ahc_linux_alloc_target() will allocate
1719 * for our ID if asked to do so.
1721 if ((channel == 0 && target == ahc->our_id)
1722 || (channel == 1 && target == ahc->our_id_b))
1725 ahc_linux_alloc_target(ahc, channel, target);
1727 ahc_intr_enable(ahc, TRUE);
1728 ahc_linux_start_dv(ahc);
1729 ahc_unlock(ahc, &s);
1731 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1732 scsi_add_host(host, (ahc->dev_softc ? &ahc->dev_softc->dev : NULL)); /* XXX handle failure */
1733 scsi_scan_host(host);
1739 ahc_linux_get_memsize(void)
1744 return ((uint64_t)si.totalram << PAGE_SHIFT);
1748 * Find the smallest available unit number to use
1749 * for a new device. We don't just use a static
1750 * count to handle the "repeated hot-(un)plug"
1754 ahc_linux_next_unit(void)
1756 struct ahc_softc *ahc;
1761 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
1762 if (ahc->unit == unit) {
1771 * Place the SCSI bus into a known state by either resetting it,
1772 * or forcing transfer negotiations on the next command to any
1776 ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
1784 if (aic7xxx_no_reset != 0)
1785 ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
1787 if ((ahc->flags & AHC_RESET_BUS_A) != 0)
1788 ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
1790 numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1792 if ((ahc->features & AHC_TWIN) != 0) {
1794 if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
1795 ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
1804 * Force negotiation to async for all targets that
1805 * will not see an initial bus reset.
1807 for (; i < numtarg; i++) {
1808 struct ahc_devinfo devinfo;
1809 struct ahc_initiator_tinfo *tinfo;
1810 struct ahc_tmode_tstate *tstate;
1816 our_id = ahc->our_id;
1818 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
1820 our_id = ahc->our_id_b;
1823 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
1824 target_id, &tstate);
1825 ahc_compile_devinfo(&devinfo, our_id, target_id,
1826 CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
1827 ahc_update_neg_request(ahc, &devinfo, tstate,
1828 tinfo, AHC_NEG_ALWAYS);
1830 /* Give the bus some time to recover */
1831 if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
1832 ahc_linux_freeze_simq(ahc);
1833 init_timer(&ahc->platform_data->reset_timer);
1834 ahc->platform_data->reset_timer.data = (u_long)ahc;
1835 ahc->platform_data->reset_timer.expires =
1836 jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000;
1837 ahc->platform_data->reset_timer.function =
1838 ahc_linux_release_simq;
1839 add_timer(&ahc->platform_data->reset_timer);
1844 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1847 ahc->platform_data =
1848 malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
1849 if (ahc->platform_data == NULL)
1851 memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
1852 TAILQ_INIT(&ahc->platform_data->completeq);
1853 TAILQ_INIT(&ahc->platform_data->device_runq);
1854 ahc->platform_data->irq = AHC_LINUX_NOIRQ;
1855 ahc->platform_data->hw_dma_mask = 0xFFFFFFFF;
1857 ahc_done_lockinit(ahc);
1858 init_timer(&ahc->platform_data->completeq_timer);
1859 ahc->platform_data->completeq_timer.data = (u_long)ahc;
1860 ahc->platform_data->completeq_timer.function =
1861 (ahc_linux_callback_t *)ahc_linux_thread_run_complete_queue;
1862 init_MUTEX_LOCKED(&ahc->platform_data->eh_sem);
1863 init_MUTEX_LOCKED(&ahc->platform_data->dv_sem);
1864 init_MUTEX_LOCKED(&ahc->platform_data->dv_cmd_sem);
1865 tasklet_init(&ahc->platform_data->runq_tasklet, ahc_runq_tasklet,
1866 (unsigned long)ahc);
1867 ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
1868 ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
1869 if (aic7xxx_pci_parity == 0)
1870 ahc->flags |= AHC_DISABLE_PCI_PERR;
1876 ahc_platform_free(struct ahc_softc *ahc)
1878 struct ahc_linux_target *targ;
1879 struct ahc_linux_device *dev;
1882 if (ahc->platform_data != NULL) {
1883 del_timer_sync(&ahc->platform_data->completeq_timer);
1884 ahc_linux_kill_dv_thread(ahc);
1885 tasklet_kill(&ahc->platform_data->runq_tasklet);
1886 if (ahc->platform_data->host != NULL) {
1887 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1888 scsi_remove_host(ahc->platform_data->host);
1890 scsi_host_put(ahc->platform_data->host);
1893 /* destroy all of the device and target objects */
1894 for (i = 0; i < AHC_NUM_TARGETS; i++) {
1895 targ = ahc->platform_data->targets[i];
1897 /* Keep target around through the loop. */
1899 for (j = 0; j < AHC_NUM_LUNS; j++) {
1901 if (targ->devices[j] == NULL)
1903 dev = targ->devices[j];
1904 ahc_linux_free_device(ahc, dev);
1907 * Forcibly free the target now that
1908 * all devices are gone.
1910 ahc_linux_free_target(ahc, targ);
1914 if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
1915 free_irq(ahc->platform_data->irq, ahc);
1916 if (ahc->tag == BUS_SPACE_PIO
1917 && ahc->bsh.ioport != 0)
1918 release_region(ahc->bsh.ioport, 256);
1919 if (ahc->tag == BUS_SPACE_MEMIO
1920 && ahc->bsh.maddr != NULL) {
1921 iounmap(ahc->bsh.maddr);
1922 release_mem_region(ahc->platform_data->mem_busaddr,
1925 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1927 * In 2.4 we detach from the scsi midlayer before the PCI
1928 * layer invokes our remove callback. No per-instance
1929 * detach is provided, so we must reach inside the PCI
1930 * subsystem's internals and detach our driver manually.
1932 if (ahc->dev_softc != NULL)
1933 ahc->dev_softc->driver = NULL;
1935 free(ahc->platform_data, M_DEVBUF);
1940 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
1942 ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1943 SCB_GET_CHANNEL(ahc, scb),
1944 SCB_GET_LUN(scb), SCB_LIST_NULL,
1945 ROLE_UNKNOWN, CAM_REQUEUE_REQ);
1949 ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1952 struct ahc_linux_device *dev;
1956 dev = ahc_linux_get_device(ahc, devinfo->channel - 'A',
1958 devinfo->lun, /*alloc*/FALSE);
1961 was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
1964 case AHC_QUEUE_NONE:
1967 case AHC_QUEUE_BASIC:
1968 now_queuing = AHC_DEV_Q_BASIC;
1970 case AHC_QUEUE_TAGGED:
1971 now_queuing = AHC_DEV_Q_TAGGED;
1974 if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
1975 && (was_queuing != now_queuing)
1976 && (dev->active != 0)) {
1977 dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
1981 dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
1985 usertags = ahc_linux_user_tagdepth(ahc, devinfo);
1988 * Start out agressively and allow our
1989 * dynamic queue depth algorithm to take
1992 dev->maxtags = usertags;
1993 dev->openings = dev->maxtags - dev->active;
1995 if (dev->maxtags == 0) {
1997 * Queueing is disabled by the user.
2000 } else if (alg == AHC_QUEUE_TAGGED) {
2001 dev->flags |= AHC_DEV_Q_TAGGED;
2002 if (aic7xxx_periodic_otag != 0)
2003 dev->flags |= AHC_DEV_PERIODIC_OTAG;
2005 dev->flags |= AHC_DEV_Q_BASIC;
2007 /* We can only have one opening. */
2009 dev->openings = 1 - dev->active;
2011 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
2012 if (dev->scsi_device != NULL) {
2013 switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
2014 case AHC_DEV_Q_BASIC:
2015 scsi_adjust_queue_depth(dev->scsi_device,
2017 dev->openings + dev->active);
2019 case AHC_DEV_Q_TAGGED:
2020 scsi_adjust_queue_depth(dev->scsi_device,
2022 dev->openings + dev->active);
2026 * We allow the OS to queue 2 untagged transactions to
2027 * us at any time even though we can only execute them
2028 * serially on the controller/device. This should
2029 * remove some latency.
2031 scsi_adjust_queue_depth(dev->scsi_device,
2041 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
2042 int lun, u_int tag, role_t role, uint32_t status)
2052 if (tag != SCB_LIST_NULL)
2056 if (channel != ALL_CHANNELS) {
2057 chan = channel - 'A';
2060 maxchan = (ahc->features & AHC_TWIN) ? 2 : 1;
2063 if (target != CAM_TARGET_WILDCARD) {
2067 maxtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
2070 if (lun != CAM_LUN_WILDCARD) {
2074 maxlun = AHC_NUM_LUNS;
2078 for (; chan < maxchan; chan++) {
2080 for (; targ < maxtarg; targ++) {
2082 for (; clun < maxlun; clun++) {
2083 struct ahc_linux_device *dev;
2084 struct ahc_busyq *busyq;
2085 struct ahc_cmd *acmd;
2087 dev = ahc_linux_get_device(ahc, chan,
2093 busyq = &dev->busyq;
2094 while ((acmd = TAILQ_FIRST(busyq)) != NULL) {
2097 cmd = &acmd_scsi_cmd(acmd);
2098 TAILQ_REMOVE(busyq, acmd,
2101 cmd->result = status << 16;
2102 ahc_linux_queue_cmd_complete(ahc, cmd);
2112 ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc)
2116 ahc_lock(ahc, &flags);
2117 del_timer(&ahc->platform_data->completeq_timer);
2118 ahc->platform_data->flags &= ~AHC_RUN_CMPLT_Q_TIMER;
2119 ahc_linux_run_complete_queue(ahc);
2120 ahc_unlock(ahc, &flags);
2124 ahc_linux_start_dv(struct ahc_softc *ahc)
2128 * Freeze the simq and signal ahc_linux_queue to not let any
2129 * more commands through.
2131 if ((ahc->platform_data->flags & AHC_DV_ACTIVE) == 0) {
2133 if (ahc_debug & AHC_SHOW_DV)
2134 printf("%s: Waking DV thread\n", ahc_name(ahc));
2137 ahc->platform_data->flags |= AHC_DV_ACTIVE;
2138 ahc_linux_freeze_simq(ahc);
2140 /* Wake up the DV kthread */
2141 up(&ahc->platform_data->dv_sem);
2146 ahc_linux_kill_dv_thread(struct ahc_softc *ahc)
2151 if (ahc->platform_data->dv_pid != 0) {
2152 ahc->platform_data->flags |= AHC_DV_SHUTDOWN;
2153 ahc_unlock(ahc, &s);
2154 up(&ahc->platform_data->dv_sem);
2157 * Use the eh_sem as an indicator that the
2158 * dv thread is exiting. Note that the dv
2159 * thread must still return after performing
2160 * the up on our semaphore before it has
2161 * completely exited this module. Unfortunately,
2162 * there seems to be no easy way to wait for the
2163 * exit of a thread for which you are not the
2164 * parent (dv threads are parented by init).
2165 * Cross your fingers...
2167 down(&ahc->platform_data->eh_sem);
2170 * Mark the dv thread as already dead. This
2171 * avoids attempting to kill it a second time.
2172 * This is necessary because we must kill the
2173 * DV thread before calling ahc_free() in the
2174 * module shutdown case to avoid bogus locking
2175 * in the SCSI mid-layer, but we ahc_free() is
2176 * called without killing the DV thread in the
2177 * instance detach case, so ahc_platform_free()
2178 * calls us again to verify that the DV thread
2181 ahc->platform_data->dv_pid = 0;
2183 ahc_unlock(ahc, &s);
2188 ahc_linux_dv_thread(void *data)
2190 struct ahc_softc *ahc;
2194 ahc = (struct ahc_softc *)data;
2197 if (ahc_debug & AHC_SHOW_DV)
2198 printf("Launching DV Thread\n");
2202 * Complete thread creation.
2205 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2207 * Don't care about any signals.
2209 siginitsetinv(¤t->blocked, 0);
2212 sprintf(current->comm, "ahc_dv_%d", ahc->unit);
2214 daemonize("ahc_dv_%d", ahc->unit);
2215 current->flags |= PF_FREEZE;
2221 * Use down_interruptible() rather than down() to
2222 * avoid inclusion in the load average.
2224 down_interruptible(&ahc->platform_data->dv_sem);
2226 /* Check to see if we've been signaled to exit */
2228 if ((ahc->platform_data->flags & AHC_DV_SHUTDOWN) != 0) {
2229 ahc_unlock(ahc, &s);
2232 ahc_unlock(ahc, &s);
2235 if (ahc_debug & AHC_SHOW_DV)
2236 printf("%s: Beginning Domain Validation\n",
2241 * Wait for any pending commands to drain before proceeding.
2244 while (LIST_FIRST(&ahc->pending_scbs) != NULL) {
2245 ahc->platform_data->flags |= AHC_DV_WAIT_SIMQ_EMPTY;
2246 ahc_unlock(ahc, &s);
2247 down_interruptible(&ahc->platform_data->dv_sem);
2252 * Wait for the SIMQ to be released so that DV is the
2253 * only reason the queue is frozen.
2255 while (AHC_DV_SIMQ_FROZEN(ahc) == 0) {
2256 ahc->platform_data->flags |= AHC_DV_WAIT_SIMQ_RELEASE;
2257 ahc_unlock(ahc, &s);
2258 down_interruptible(&ahc->platform_data->dv_sem);
2261 ahc_unlock(ahc, &s);
2263 for (target = 0; target < AHC_NUM_TARGETS; target++)
2264 ahc_linux_dv_target(ahc, target);
2267 ahc->platform_data->flags &= ~AHC_DV_ACTIVE;
2268 ahc_unlock(ahc, &s);
2271 * Release the SIMQ so that normal commands are
2272 * allowed to continue on the bus.
2274 ahc_linux_release_simq((u_long)ahc);
2276 up(&ahc->platform_data->eh_sem);
2280 #define AHC_LINUX_DV_INQ_SHORT_LEN 36
2281 #define AHC_LINUX_DV_INQ_LEN 256
2282 #define AHC_LINUX_DV_TIMEOUT (HZ / 4)
2284 #define AHC_SET_DV_STATE(ahc, targ, newstate) \
2285 ahc_set_dv_state(ahc, targ, newstate, __LINE__)
2287 static __inline void
2288 ahc_set_dv_state(struct ahc_softc *ahc, struct ahc_linux_target *targ,
2289 ahc_dv_state newstate, u_int line)
2291 ahc_dv_state oldstate;
2293 oldstate = targ->dv_state;
2295 if (ahc_debug & AHC_SHOW_DV)
2296 printf("%s:%d: Going from state %d to state %d\n",
2297 ahc_name(ahc), line, oldstate, newstate);
2300 if (oldstate == newstate)
2301 targ->dv_state_retry++;
2303 targ->dv_state_retry = 0;
2304 targ->dv_state = newstate;
2308 ahc_linux_dv_target(struct ahc_softc *ahc, u_int target_offset)
2310 struct ahc_devinfo devinfo;
2311 struct ahc_linux_target *targ;
2312 struct scsi_cmnd *cmd;
2313 struct scsi_device *scsi_dev;
2314 struct scsi_sense_data *sense;
2324 targ = ahc->platform_data->targets[target_offset];
2325 if (targ == NULL || (targ->flags & AHC_DV_REQUIRED) == 0) {
2326 ahc_unlock(ahc, &s);
2329 ahc_compile_devinfo(&devinfo,
2330 targ->channel == 0 ? ahc->our_id : ahc->our_id_b,
2331 targ->target, /*lun*/0, targ->channel + 'A',
2334 if (ahc_debug & AHC_SHOW_DV) {
2335 ahc_print_devinfo(ahc, &devinfo);
2336 printf("Performing DV\n");
2340 ahc_unlock(ahc, &s);
2342 cmd = malloc(sizeof(struct scsi_cmnd), M_DEVBUF, M_WAITOK);
2343 scsi_dev = malloc(sizeof(struct scsi_device), M_DEVBUF, M_WAITOK);
2344 scsi_dev->host = ahc->platform_data->host;
2345 scsi_dev->id = devinfo.target;
2346 scsi_dev->lun = devinfo.lun;
2347 scsi_dev->channel = devinfo.channel - 'A';
2348 ahc->platform_data->dv_scsi_dev = scsi_dev;
2350 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_INQ_SHORT_ASYNC);
2352 while (targ->dv_state != AHC_DV_STATE_EXIT) {
2353 timeout = AHC_LINUX_DV_TIMEOUT;
2354 switch (targ->dv_state) {
2355 case AHC_DV_STATE_INQ_SHORT_ASYNC:
2356 case AHC_DV_STATE_INQ_ASYNC:
2357 case AHC_DV_STATE_INQ_ASYNC_VERIFY:
2359 * Set things to async narrow to reduce the
2360 * chance that the INQ will fail.
2363 ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
2364 AHC_TRANS_GOAL, /*paused*/FALSE);
2365 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
2366 AHC_TRANS_GOAL, /*paused*/FALSE);
2367 ahc_unlock(ahc, &s);
2369 targ->flags &= ~AHC_INQ_VALID;
2371 case AHC_DV_STATE_INQ_VERIFY:
2375 if (targ->dv_state == AHC_DV_STATE_INQ_SHORT_ASYNC)
2376 inq_len = AHC_LINUX_DV_INQ_SHORT_LEN;
2378 inq_len = targ->inq_data->additional_length + 5;
2379 ahc_linux_dv_inq(ahc, cmd, &devinfo, targ, inq_len);
2382 case AHC_DV_STATE_TUR:
2383 case AHC_DV_STATE_BUSY:
2385 ahc_linux_dv_tur(ahc, cmd, &devinfo);
2387 case AHC_DV_STATE_REBD:
2388 ahc_linux_dv_rebd(ahc, cmd, &devinfo, targ);
2390 case AHC_DV_STATE_WEB:
2391 ahc_linux_dv_web(ahc, cmd, &devinfo, targ);
2394 case AHC_DV_STATE_REB:
2395 ahc_linux_dv_reb(ahc, cmd, &devinfo, targ);
2398 case AHC_DV_STATE_SU:
2399 ahc_linux_dv_su(ahc, cmd, &devinfo, targ);
2404 ahc_print_devinfo(ahc, &devinfo);
2405 printf("Unknown DV state %d\n", targ->dv_state);
2409 /* Queue the command and wait for it to complete */
2410 /* Abuse eh_timeout in the scsi_cmnd struct for our purposes */
2411 init_timer(&cmd->eh_timeout);
2413 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2415 * All of the printfs during negotiation
2416 * really slow down the negotiation.
2417 * Add a bit of time just to be safe.
2421 scsi_add_timer(cmd, timeout, ahc_linux_dv_timeout);
2423 * In 2.5.X, it is assumed that all calls from the
2424 * "midlayer" (which we are emulating) will have the
2425 * ahc host lock held. For other kernels, the
2426 * io_request_lock must be held.
2428 #if AHC_SCSI_HAS_HOST_LOCK != 0
2431 spin_lock_irqsave(&io_request_lock, s);
2433 ahc_linux_queue(cmd, ahc_linux_dv_complete);
2434 #if AHC_SCSI_HAS_HOST_LOCK != 0
2435 ahc_unlock(ahc, &s);
2437 spin_unlock_irqrestore(&io_request_lock, s);
2439 down_interruptible(&ahc->platform_data->dv_cmd_sem);
2441 * Wait for the SIMQ to be released so that DV is the
2442 * only reason the queue is frozen.
2445 while (AHC_DV_SIMQ_FROZEN(ahc) == 0) {
2446 ahc->platform_data->flags |= AHC_DV_WAIT_SIMQ_RELEASE;
2447 ahc_unlock(ahc, &s);
2448 down_interruptible(&ahc->platform_data->dv_sem);
2451 ahc_unlock(ahc, &s);
2453 ahc_linux_dv_transition(ahc, cmd, &devinfo, targ);
2457 if ((targ->flags & AHC_INQ_VALID) != 0
2458 && ahc_linux_get_device(ahc, devinfo.channel - 'A',
2459 devinfo.target, devinfo.lun,
2460 /*alloc*/FALSE) == NULL) {
2462 * The DV state machine failed to configure this device.
2463 * This is normal if DV is disabled. Since we have inquiry
2464 * data, filter it and use the "optimistic" negotiation
2465 * parameters found in the inquiry string.
2467 ahc_linux_filter_inquiry(ahc, &devinfo);
2468 if ((targ->flags & (AHC_BASIC_DV|AHC_ENHANCED_DV)) != 0) {
2469 ahc_print_devinfo(ahc, &devinfo);
2470 printf("DV failed to configure device. "
2471 "Please file a bug report against "
2477 free(cmd, M_DEVBUF);
2479 if (ahc->platform_data->dv_scsi_dev != NULL) {
2480 free(ahc->platform_data->dv_scsi_dev, M_DEVBUF);
2481 ahc->platform_data->dv_scsi_dev = NULL;
2485 if (targ->dv_buffer != NULL) {
2486 free(targ->dv_buffer, M_DEVBUF);
2487 targ->dv_buffer = NULL;
2489 if (targ->dv_buffer1 != NULL) {
2490 free(targ->dv_buffer1, M_DEVBUF);
2491 targ->dv_buffer1 = NULL;
2493 targ->flags &= ~AHC_DV_REQUIRED;
2494 if (targ->refcount == 0)
2495 ahc_linux_free_target(ahc, targ);
2496 ahc_unlock(ahc, &s);
2500 ahc_linux_dv_transition(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
2501 struct ahc_devinfo *devinfo,
2502 struct ahc_linux_target *targ)
2506 status = aic_error_action(cmd, targ->inq_data,
2507 ahc_cmd_get_transaction_status(cmd),
2508 ahc_cmd_get_scsi_status(cmd));
2511 if (ahc_debug & AHC_SHOW_DV) {
2512 ahc_print_devinfo(ahc, devinfo);
2513 printf("Entering ahc_linux_dv_transition, state= %d, "
2514 "status= 0x%x, cmd->result= 0x%x\n", targ->dv_state,
2515 status, cmd->result);
2519 switch (targ->dv_state) {
2520 case AHC_DV_STATE_INQ_SHORT_ASYNC:
2521 case AHC_DV_STATE_INQ_ASYNC:
2522 switch (status & SS_MASK) {
2525 AHC_SET_DV_STATE(ahc, targ, targ->dv_state+1);
2528 case SS_INQ_REFRESH:
2529 AHC_SET_DV_STATE(ahc, targ,
2530 AHC_DV_STATE_INQ_SHORT_ASYNC);
2534 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2535 if (ahc_cmd_get_transaction_status(cmd)
2537 targ->dv_state_retry--;
2538 if ((status & SS_ERRMASK) == EBUSY)
2539 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2540 if (targ->dv_state_retry < 10)
2544 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2546 if (ahc_debug & AHC_SHOW_DV) {
2547 ahc_print_devinfo(ahc, devinfo);
2548 printf("Failed DV inquiry, skipping\n");
2554 case AHC_DV_STATE_INQ_ASYNC_VERIFY:
2555 switch (status & SS_MASK) {
2561 if (memcmp(targ->inq_data, targ->dv_buffer,
2562 AHC_LINUX_DV_INQ_LEN) != 0) {
2564 * Inquiry data must have changed.
2565 * Try from the top again.
2567 AHC_SET_DV_STATE(ahc, targ,
2568 AHC_DV_STATE_INQ_SHORT_ASYNC);
2572 AHC_SET_DV_STATE(ahc, targ, targ->dv_state+1);
2573 targ->flags |= AHC_INQ_VALID;
2574 if (ahc_linux_user_dv_setting(ahc) == 0)
2577 xportflags = targ->inq_data->flags;
2578 if ((xportflags & (SID_Sync|SID_WBus16)) == 0)
2581 spi3data = targ->inq_data->spi3data;
2582 switch (spi3data & SID_SPI_CLOCK_DT_ST) {
2584 case SID_SPI_CLOCK_ST:
2585 /* Assume only basic DV is supported. */
2586 targ->flags |= AHC_BASIC_DV;
2588 case SID_SPI_CLOCK_DT:
2589 case SID_SPI_CLOCK_DT_ST:
2590 targ->flags |= AHC_ENHANCED_DV;
2595 case SS_INQ_REFRESH:
2596 AHC_SET_DV_STATE(ahc, targ,
2597 AHC_DV_STATE_INQ_SHORT_ASYNC);
2601 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2602 if (ahc_cmd_get_transaction_status(cmd)
2604 targ->dv_state_retry--;
2606 if ((status & SS_ERRMASK) == EBUSY)
2607 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2608 if (targ->dv_state_retry < 10)
2612 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2614 if (ahc_debug & AHC_SHOW_DV) {
2615 ahc_print_devinfo(ahc, devinfo);
2616 printf("Failed DV inquiry, skipping\n");
2622 case AHC_DV_STATE_INQ_VERIFY:
2623 switch (status & SS_MASK) {
2627 if (memcmp(targ->inq_data, targ->dv_buffer,
2628 AHC_LINUX_DV_INQ_LEN) == 0) {
2629 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2633 if (ahc_debug & AHC_SHOW_DV) {
2636 ahc_print_devinfo(ahc, devinfo);
2637 printf("Inquiry buffer mismatch:");
2638 for (i = 0; i < AHC_LINUX_DV_INQ_LEN; i++) {
2641 printf("0x%x:0x0%x ",
2642 ((uint8_t *)targ->inq_data)[i],
2643 targ->dv_buffer[i]);
2649 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2650 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2654 * Do not count "falling back"
2655 * against our retries.
2657 targ->dv_state_retry = 0;
2658 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2661 case SS_INQ_REFRESH:
2662 AHC_SET_DV_STATE(ahc, targ,
2663 AHC_DV_STATE_INQ_SHORT_ASYNC);
2667 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2668 if (ahc_cmd_get_transaction_status(cmd)
2669 == CAM_REQUEUE_REQ) {
2670 targ->dv_state_retry--;
2671 } else if ((status & SSQ_FALLBACK) != 0) {
2672 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2673 AHC_SET_DV_STATE(ahc, targ,
2678 * Do not count "falling back"
2679 * against our retries.
2681 targ->dv_state_retry = 0;
2682 } else if ((status & SS_ERRMASK) == EBUSY)
2683 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2684 if (targ->dv_state_retry < 10)
2688 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2690 if (ahc_debug & AHC_SHOW_DV) {
2691 ahc_print_devinfo(ahc, devinfo);
2692 printf("Failed DV inquiry, skipping\n");
2699 case AHC_DV_STATE_TUR:
2700 switch (status & SS_MASK) {
2702 if ((targ->flags & AHC_BASIC_DV) != 0) {
2703 ahc_linux_filter_inquiry(ahc, devinfo);
2704 AHC_SET_DV_STATE(ahc, targ,
2705 AHC_DV_STATE_INQ_VERIFY);
2706 } else if ((targ->flags & AHC_ENHANCED_DV) != 0) {
2707 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_REBD);
2709 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2714 if ((status & SS_ERRMASK) == EBUSY) {
2715 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2718 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2719 if (ahc_cmd_get_transaction_status(cmd)
2720 == CAM_REQUEUE_REQ) {
2721 targ->dv_state_retry--;
2722 } else if ((status & SSQ_FALLBACK) != 0) {
2723 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2724 AHC_SET_DV_STATE(ahc, targ,
2729 * Do not count "falling back"
2730 * against our retries.
2732 targ->dv_state_retry = 0;
2734 if (targ->dv_state_retry >= 10) {
2736 if (ahc_debug & AHC_SHOW_DV) {
2737 ahc_print_devinfo(ahc, devinfo);
2738 printf("DV TUR reties exhausted\n");
2741 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2744 if (status & SSQ_DELAY)
2749 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_SU);
2751 case SS_INQ_REFRESH:
2752 AHC_SET_DV_STATE(ahc, targ,
2753 AHC_DV_STATE_INQ_SHORT_ASYNC);
2756 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2761 case AHC_DV_STATE_REBD:
2762 switch (status & SS_MASK) {
2767 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_WEB);
2768 echo_size = scsi_3btoul(&targ->dv_buffer[1]);
2769 echo_size &= 0x1FFF;
2771 if (ahc_debug & AHC_SHOW_DV) {
2772 ahc_print_devinfo(ahc, devinfo);
2773 printf("Echo buffer size= %d\n", echo_size);
2776 if (echo_size == 0) {
2777 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2781 /* Generate the buffer pattern */
2782 targ->dv_echo_size = echo_size;
2783 ahc_linux_generate_dv_pattern(targ);
2785 * Setup initial negotiation values.
2787 ahc_linux_filter_inquiry(ahc, devinfo);
2790 case SS_INQ_REFRESH:
2791 AHC_SET_DV_STATE(ahc, targ,
2792 AHC_DV_STATE_INQ_SHORT_ASYNC);
2795 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2796 if (ahc_cmd_get_transaction_status(cmd)
2798 targ->dv_state_retry--;
2799 if (targ->dv_state_retry <= 10)
2802 if (ahc_debug & AHC_SHOW_DV) {
2803 ahc_print_devinfo(ahc, devinfo);
2804 printf("DV REBD reties exhausted\n");
2811 * Setup initial negotiation values
2812 * and try level 1 DV.
2814 ahc_linux_filter_inquiry(ahc, devinfo);
2815 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_INQ_VERIFY);
2816 targ->dv_echo_size = 0;
2821 case AHC_DV_STATE_WEB:
2822 switch (status & SS_MASK) {
2824 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_REB);
2826 case SS_INQ_REFRESH:
2827 AHC_SET_DV_STATE(ahc, targ,
2828 AHC_DV_STATE_INQ_SHORT_ASYNC);
2831 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2832 if (ahc_cmd_get_transaction_status(cmd)
2833 == CAM_REQUEUE_REQ) {
2834 targ->dv_state_retry--;
2835 } else if ((status & SSQ_FALLBACK) != 0) {
2836 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2837 AHC_SET_DV_STATE(ahc, targ,
2842 * Do not count "falling back"
2843 * against our retries.
2845 targ->dv_state_retry = 0;
2847 if (targ->dv_state_retry <= 10)
2851 if (ahc_debug & AHC_SHOW_DV) {
2852 ahc_print_devinfo(ahc, devinfo);
2853 printf("DV WEB reties exhausted\n");
2857 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2862 case AHC_DV_STATE_REB:
2863 switch (status & SS_MASK) {
2865 if (memcmp(targ->dv_buffer, targ->dv_buffer1,
2866 targ->dv_echo_size) != 0) {
2867 if (ahc_linux_fallback(ahc, devinfo) != 0)
2868 AHC_SET_DV_STATE(ahc, targ,
2871 AHC_SET_DV_STATE(ahc, targ,
2876 if (targ->dv_buffer != NULL) {
2877 free(targ->dv_buffer, M_DEVBUF);
2878 targ->dv_buffer = NULL;
2880 if (targ->dv_buffer1 != NULL) {
2881 free(targ->dv_buffer1, M_DEVBUF);
2882 targ->dv_buffer1 = NULL;
2884 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2886 case SS_INQ_REFRESH:
2887 AHC_SET_DV_STATE(ahc, targ,
2888 AHC_DV_STATE_INQ_SHORT_ASYNC);
2891 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2892 if (ahc_cmd_get_transaction_status(cmd)
2893 == CAM_REQUEUE_REQ) {
2894 targ->dv_state_retry--;
2895 } else if ((status & SSQ_FALLBACK) != 0) {
2896 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2897 AHC_SET_DV_STATE(ahc, targ,
2901 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_WEB);
2903 if (targ->dv_state_retry <= 10) {
2904 if ((status & (SSQ_DELAY_RANDOM|SSQ_DELAY))!= 0)
2905 msleep(ahc->our_id*1000/10);
2909 if (ahc_debug & AHC_SHOW_DV) {
2910 ahc_print_devinfo(ahc, devinfo);
2911 printf("DV REB reties exhausted\n");
2916 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2921 case AHC_DV_STATE_SU:
2922 switch (status & SS_MASK) {
2924 case SS_INQ_REFRESH:
2925 AHC_SET_DV_STATE(ahc, targ,
2926 AHC_DV_STATE_INQ_SHORT_ASYNC);
2929 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2934 case AHC_DV_STATE_BUSY:
2935 switch (status & SS_MASK) {
2937 case SS_INQ_REFRESH:
2938 AHC_SET_DV_STATE(ahc, targ,
2939 AHC_DV_STATE_INQ_SHORT_ASYNC);
2943 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2944 if (ahc_cmd_get_transaction_status(cmd)
2945 == CAM_REQUEUE_REQ) {
2946 targ->dv_state_retry--;
2947 } else if (targ->dv_state_retry < 60) {
2948 if ((status & SSQ_DELAY) != 0)
2952 if (ahc_debug & AHC_SHOW_DV) {
2953 ahc_print_devinfo(ahc, devinfo);
2954 printf("DV BUSY reties exhausted\n");
2957 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2961 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2967 printf("%s: Invalid DV completion state %d\n", ahc_name(ahc),
2969 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2975 ahc_linux_dv_fill_cmd(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
2976 struct ahc_devinfo *devinfo)
2978 memset(cmd, 0, sizeof(struct scsi_cmnd));
2979 cmd->device = ahc->platform_data->dv_scsi_dev;
2980 cmd->scsi_done = ahc_linux_dv_complete;
2984 * Synthesize an inquiry command. On the return trip, it'll be
2985 * sniffed and the device transfer settings set for us.
2988 ahc_linux_dv_inq(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
2989 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ,
2990 u_int request_length)
2994 if (ahc_debug & AHC_SHOW_DV) {
2995 ahc_print_devinfo(ahc, devinfo);
2996 printf("Sending INQ\n");
2999 if (targ->inq_data == NULL)
3000 targ->inq_data = malloc(AHC_LINUX_DV_INQ_LEN,
3001 M_DEVBUF, M_WAITOK);
3002 if (targ->dv_state > AHC_DV_STATE_INQ_ASYNC) {
3003 if (targ->dv_buffer != NULL)
3004 free(targ->dv_buffer, M_DEVBUF);
3005 targ->dv_buffer = malloc(AHC_LINUX_DV_INQ_LEN,
3006 M_DEVBUF, M_WAITOK);
3009 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3010 cmd->sc_data_direction = DMA_FROM_DEVICE;
3012 cmd->cmnd[0] = INQUIRY;
3013 cmd->cmnd[4] = request_length;
3014 cmd->request_bufflen = request_length;
3015 if (targ->dv_state > AHC_DV_STATE_INQ_ASYNC)
3016 cmd->request_buffer = targ->dv_buffer;
3018 cmd->request_buffer = targ->inq_data;
3019 memset(cmd->request_buffer, 0, AHC_LINUX_DV_INQ_LEN);
3023 ahc_linux_dv_tur(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3024 struct ahc_devinfo *devinfo)
3028 if (ahc_debug & AHC_SHOW_DV) {
3029 ahc_print_devinfo(ahc, devinfo);
3030 printf("Sending TUR\n");
3033 /* Do a TUR to clear out any non-fatal transitional state */
3034 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3035 cmd->sc_data_direction = DMA_NONE;
3037 cmd->cmnd[0] = TEST_UNIT_READY;
3040 #define AHC_REBD_LEN 4
3043 ahc_linux_dv_rebd(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3044 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ)
3048 if (ahc_debug & AHC_SHOW_DV) {
3049 ahc_print_devinfo(ahc, devinfo);
3050 printf("Sending REBD\n");
3053 if (targ->dv_buffer != NULL)
3054 free(targ->dv_buffer, M_DEVBUF);
3055 targ->dv_buffer = malloc(AHC_REBD_LEN, M_DEVBUF, M_WAITOK);
3056 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3057 cmd->sc_data_direction = DMA_FROM_DEVICE;
3059 cmd->cmnd[0] = READ_BUFFER;
3060 cmd->cmnd[1] = 0x0b;
3061 scsi_ulto3b(AHC_REBD_LEN, &cmd->cmnd[6]);
3062 cmd->request_bufflen = AHC_REBD_LEN;
3063 cmd->underflow = cmd->request_bufflen;
3064 cmd->request_buffer = targ->dv_buffer;
3068 ahc_linux_dv_web(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3069 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ)
3073 if (ahc_debug & AHC_SHOW_DV) {
3074 ahc_print_devinfo(ahc, devinfo);
3075 printf("Sending WEB\n");
3078 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3079 cmd->sc_data_direction = DMA_TO_DEVICE;
3081 cmd->cmnd[0] = WRITE_BUFFER;
3082 cmd->cmnd[1] = 0x0a;
3083 scsi_ulto3b(targ->dv_echo_size, &cmd->cmnd[6]);
3084 cmd->request_bufflen = targ->dv_echo_size;
3085 cmd->underflow = cmd->request_bufflen;
3086 cmd->request_buffer = targ->dv_buffer;
3090 ahc_linux_dv_reb(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3091 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ)
3095 if (ahc_debug & AHC_SHOW_DV) {
3096 ahc_print_devinfo(ahc, devinfo);
3097 printf("Sending REB\n");
3100 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3101 cmd->sc_data_direction = DMA_FROM_DEVICE;
3103 cmd->cmnd[0] = READ_BUFFER;
3104 cmd->cmnd[1] = 0x0a;
3105 scsi_ulto3b(targ->dv_echo_size, &cmd->cmnd[6]);
3106 cmd->request_bufflen = targ->dv_echo_size;
3107 cmd->underflow = cmd->request_bufflen;
3108 cmd->request_buffer = targ->dv_buffer1;
3112 ahc_linux_dv_su(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3113 struct ahc_devinfo *devinfo,
3114 struct ahc_linux_target *targ)
3118 le = SID_IS_REMOVABLE(targ->inq_data) ? SSS_LOEJ : 0;
3121 if (ahc_debug & AHC_SHOW_DV) {
3122 ahc_print_devinfo(ahc, devinfo);
3123 printf("Sending SU\n");
3126 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3127 cmd->sc_data_direction = DMA_NONE;
3129 cmd->cmnd[0] = START_STOP_UNIT;
3130 cmd->cmnd[4] = le | SSS_START;
3134 ahc_linux_fallback(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3136 struct ahc_linux_target *targ;
3137 struct ahc_initiator_tinfo *tinfo;
3138 struct ahc_transinfo *goal;
3139 struct ahc_tmode_tstate *tstate;
3140 struct ahc_syncrate *syncrate;
3149 u_int fallback_speed;
3152 if (ahc_debug & AHC_SHOW_DV) {
3153 ahc_print_devinfo(ahc, devinfo);
3154 printf("Trying to fallback\n");
3158 targ = ahc->platform_data->targets[devinfo->target_offset];
3159 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
3160 devinfo->our_scsiid,
3161 devinfo->target, &tstate);
3162 goal = &tinfo->goal;
3163 width = goal->width;
3164 period = goal->period;
3165 offset = goal->offset;
3166 ppr_options = goal->ppr_options;
3168 period = AHC_ASYNC_XFER_PERIOD;
3169 if (targ->dv_next_narrow_period == 0)
3170 targ->dv_next_narrow_period = MAX(period, AHC_SYNCRATE_ULTRA2);
3171 if (targ->dv_next_wide_period == 0)
3172 targ->dv_next_wide_period = period;
3173 if (targ->dv_max_width == 0)
3174 targ->dv_max_width = width;
3175 if (targ->dv_max_ppr_options == 0)
3176 targ->dv_max_ppr_options = ppr_options;
3177 if (targ->dv_last_ppr_options == 0)
3178 targ->dv_last_ppr_options = ppr_options;
3180 cur_speed = aic_calc_speed(width, period, offset, AHC_SYNCRATE_MIN);
3181 wide_speed = aic_calc_speed(MSG_EXT_WDTR_BUS_16_BIT,
3182 targ->dv_next_wide_period,
3185 narrow_speed = aic_calc_speed(MSG_EXT_WDTR_BUS_8_BIT,
3186 targ->dv_next_narrow_period,
3189 fallback_speed = aic_calc_speed(width, period+1, offset,
3192 if (ahc_debug & AHC_SHOW_DV) {
3193 printf("cur_speed= %d, wide_speed= %d, narrow_speed= %d, "
3194 "fallback_speed= %d\n", cur_speed, wide_speed,
3195 narrow_speed, fallback_speed);
3199 if (cur_speed > 160000) {
3201 * Paced/DT/IU_REQ only transfer speeds. All we
3202 * can do is fallback in terms of syncrate.
3205 } else if (cur_speed > 80000) {
3206 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3208 * Try without IU_REQ as it may be confusing
3211 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
3214 * Paced/DT only transfer speeds. All we
3215 * can do is fallback in terms of syncrate.
3218 ppr_options = targ->dv_max_ppr_options;
3220 } else if (cur_speed > 3300) {
3223 * In this range we the following
3224 * options ordered from highest to
3225 * lowest desireability:
3229 * o Narrow at a potentally higher sync rate.
3231 * All modes are tested with and without IU_REQ
3232 * set since using IUs may confuse an expander.
3234 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3236 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
3237 } else if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
3241 ppr_options = targ->dv_max_ppr_options;
3242 ppr_options &= ~MSG_EXT_PPR_DT_REQ;
3243 } else if (targ->dv_last_ppr_options != 0) {
3245 * Try without QAS or any other PPR options.
3246 * We may need a non-PPR message to work with
3247 * an expander. We look at the "last PPR options"
3248 * so we will perform this fallback even if the
3249 * target responded to our PPR negotiation with
3250 * no option bits set.
3253 } else if (width == MSG_EXT_WDTR_BUS_16_BIT) {
3255 * If the next narrow speed is greater than
3256 * the next wide speed, fallback to narrow.
3257 * Otherwise fallback to the next DT/Wide setting.
3258 * The narrow async speed will always be smaller
3259 * than the wide async speed, so handle this case
3262 ppr_options = targ->dv_max_ppr_options;
3263 if (narrow_speed > fallback_speed
3264 || period >= AHC_ASYNC_XFER_PERIOD) {
3265 targ->dv_next_wide_period = period+1;
3266 width = MSG_EXT_WDTR_BUS_8_BIT;
3267 period = targ->dv_next_narrow_period;
3271 } else if ((ahc->features & AHC_WIDE) != 0
3272 && targ->dv_max_width != 0
3273 && wide_speed >= fallback_speed
3274 && (targ->dv_next_wide_period <= AHC_ASYNC_XFER_PERIOD
3275 || period >= AHC_ASYNC_XFER_PERIOD)) {
3278 * We are narrow. Try falling back
3279 * to the next wide speed with
3280 * all supported ppr options set.
3282 targ->dv_next_narrow_period = period+1;
3283 width = MSG_EXT_WDTR_BUS_16_BIT;
3284 period = targ->dv_next_wide_period;
3285 ppr_options = targ->dv_max_ppr_options;
3287 /* Only narrow fallback is allowed. */
3289 ppr_options = targ->dv_max_ppr_options;
3292 ahc_unlock(ahc, &s);
3295 offset = MAX_OFFSET;
3296 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,
3298 ahc_set_width(ahc, devinfo, width, AHC_TRANS_GOAL, FALSE);
3303 if (width == MSG_EXT_WDTR_BUS_8_BIT)
3304 targ->dv_next_narrow_period = AHC_ASYNC_XFER_PERIOD;
3306 targ->dv_next_wide_period = AHC_ASYNC_XFER_PERIOD;
3308 ahc_set_syncrate(ahc, devinfo, syncrate, period, offset,
3309 ppr_options, AHC_TRANS_GOAL, FALSE);
3310 targ->dv_last_ppr_options = ppr_options;
3311 ahc_unlock(ahc, &s);
3316 ahc_linux_dv_timeout(struct scsi_cmnd *cmd)
3318 struct ahc_softc *ahc;
3322 ahc = *((struct ahc_softc **)cmd->device->host->hostdata);
3323 ahc_lock(ahc, &flags);
3326 if (ahc_debug & AHC_SHOW_DV) {
3327 printf("%s: Timeout while doing DV command %x.\n",
3328 ahc_name(ahc), cmd->cmnd[0]);
3329 ahc_dump_card_state(ahc);
3334 * Guard against "done race". No action is
3335 * required if we just completed.
3337 if ((scb = (struct scb *)cmd->host_scribble) == NULL) {
3338 ahc_unlock(ahc, &flags);
3343 * Command has not completed. Mark this
3344 * SCB as having failing status prior to
3345 * resetting the bus, so we get the correct
3348 if ((scb->flags & SCB_SENSE) != 0)
3349 ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
3351 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
3352 ahc_reset_channel(ahc, cmd->device->channel + 'A', /*initiate*/TRUE);
3355 * Add a minimal bus settle delay for devices that are slow to
3356 * respond after bus resets.
3358 ahc_linux_freeze_simq(ahc);
3359 init_timer(&ahc->platform_data->reset_timer);
3360 ahc->platform_data->reset_timer.data = (u_long)ahc;
3361 ahc->platform_data->reset_timer.expires = jiffies + HZ / 2;
3362 ahc->platform_data->reset_timer.function =
3363 (ahc_linux_callback_t *)ahc_linux_release_simq;
3364 add_timer(&ahc->platform_data->reset_timer);
3365 if (ahc_linux_next_device_to_run(ahc) != NULL)
3366 ahc_schedule_runq(ahc);
3367 ahc_linux_run_complete_queue(ahc);
3368 ahc_unlock(ahc, &flags);
3372 ahc_linux_dv_complete(struct scsi_cmnd *cmd)
3374 struct ahc_softc *ahc;
3376 ahc = *((struct ahc_softc **)cmd->device->host->hostdata);
3378 /* Delete the DV timer before it goes off! */
3379 scsi_delete_timer(cmd);
3382 if (ahc_debug & AHC_SHOW_DV)
3383 printf("%s:%d:%d: Command completed, status= 0x%x\n",
3384 ahc_name(ahc), cmd->device->channel,
3385 cmd->device->id, cmd->result);
3388 /* Wake up the state machine */
3389 up(&ahc->platform_data->dv_cmd_sem);
3393 ahc_linux_generate_dv_pattern(struct ahc_linux_target *targ)
3399 if (targ->dv_buffer != NULL)
3400 free(targ->dv_buffer, M_DEVBUF);
3401 targ->dv_buffer = malloc(targ->dv_echo_size, M_DEVBUF, M_WAITOK);
3402 if (targ->dv_buffer1 != NULL)
3403 free(targ->dv_buffer1, M_DEVBUF);
3404 targ->dv_buffer1 = malloc(targ->dv_echo_size, M_DEVBUF, M_WAITOK);
3408 for (j = 0 ; i < targ->dv_echo_size; j++) {
3411 * 32bytes of sequential numbers.
3413 targ->dv_buffer[i++] = j & 0xff;
3414 } else if (j < 48) {
3416 * 32bytes of repeating 0x0000, 0xffff.
3418 targ->dv_buffer[i++] = (j & 0x02) ? 0xff : 0x00;
3419 } else if (j < 64) {
3421 * 32bytes of repeating 0x5555, 0xaaaa.
3423 targ->dv_buffer[i++] = (j & 0x02) ? 0xaa : 0x55;
3426 * Remaining buffer is filled with a repeating
3430 * ~0x0001 << shifted once in each loop.
3434 targ->dv_buffer[i++] = ~(b >> 8) & 0xff;
3439 targ->dv_buffer[i++] = (~b & 0xff);
3442 targ->dv_buffer[i++] = 0xff;
3449 ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3451 static int warned_user;
3455 if ((ahc->user_discenable & devinfo->target_mask) != 0) {
3456 if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
3457 if (warned_user == 0) {
3460 "aic7xxx: WARNING: Insufficient tag_info instances\n"
3461 "aic7xxx: for installed controllers. Using defaults\n"
3462 "aic7xxx: Please update the aic7xxx_tag_info array in\n"
3463 "aic7xxx: the aic7xxx_osm..c source file.\n");
3466 tags = AHC_MAX_QUEUE;
3468 adapter_tag_info_t *tag_info;
3470 tag_info = &aic7xxx_tag_info[ahc->unit];
3471 tags = tag_info->tag_commands[devinfo->target_offset];
3472 if (tags > AHC_MAX_QUEUE)
3473 tags = AHC_MAX_QUEUE;
3480 ahc_linux_user_dv_setting(struct ahc_softc *ahc)
3482 static int warned_user;
3485 if (ahc->unit >= NUM_ELEMENTS(aic7xxx_dv_settings)) {
3486 if (warned_user == 0) {
3489 "aic7xxx: WARNING: Insufficient dv settings instances\n"
3490 "aic7xxx: for installed controllers. Using defaults\n"
3491 "aic7xxx: Please update the aic7xxx_dv_settings array\n"
3492 "aic7xxx: in the aic7xxx_osm.c source file.\n");
3498 dv = aic7xxx_dv_settings[ahc->unit];
3505 * Apply the default.
3508 * XXX - Enable DV on non-U160 controllers once it
3509 * has been tested there.
3512 dv = (ahc->features & AHC_DT);
3513 if (ahc->seep_config != 0
3514 && ahc->seep_config->signature >= CFSIGNATURE2)
3515 dv = (ahc->seep_config->adapter_control & CFENABLEDV);
3516 ahc_unlock(ahc, &s);
3522 * Determines the queue depth for a given device.
3525 ahc_linux_device_queue_depth(struct ahc_softc *ahc,
3526 struct ahc_linux_device *dev)
3528 struct ahc_devinfo devinfo;
3531 ahc_compile_devinfo(&devinfo,
3532 dev->target->channel == 0
3533 ? ahc->our_id : ahc->our_id_b,
3534 dev->target->target, dev->lun,
3535 dev->target->channel == 0 ? 'A' : 'B',
3537 tags = ahc_linux_user_tagdepth(ahc, &devinfo);
3539 && dev->scsi_device != NULL
3540 && dev->scsi_device->tagged_supported != 0) {
3542 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
3543 ahc_print_devinfo(ahc, &devinfo);
3544 printf("Tagged Queuing enabled. Depth %d\n", tags);
3546 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE);
3551 ahc_linux_run_device_queue(struct ahc_softc *ahc, struct ahc_linux_device *dev)
3553 struct ahc_cmd *acmd;
3554 struct scsi_cmnd *cmd;
3556 struct hardware_scb *hscb;
3557 struct ahc_initiator_tinfo *tinfo;
3558 struct ahc_tmode_tstate *tstate;
3561 if ((dev->flags & AHC_DEV_ON_RUN_LIST) != 0)
3562 panic("running device on run list");
3564 while ((acmd = TAILQ_FIRST(&dev->busyq)) != NULL
3565 && dev->openings > 0 && dev->qfrozen == 0) {
3568 * Schedule us to run later. The only reason we are not
3569 * running is because the whole controller Q is frozen.
3571 if (ahc->platform_data->qfrozen != 0
3572 && AHC_DV_SIMQ_FROZEN(ahc) == 0) {
3573 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
3575 dev->flags |= AHC_DEV_ON_RUN_LIST;
3579 * Get an scb to use.
3581 if ((scb = ahc_get_scb(ahc)) == NULL) {
3582 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
3584 dev->flags |= AHC_DEV_ON_RUN_LIST;
3585 ahc->flags |= AHC_RESOURCE_SHORTAGE;
3588 TAILQ_REMOVE(&dev->busyq, acmd, acmd_links.tqe);
3589 cmd = &acmd_scsi_cmd(acmd);
3591 scb->platform_data->dev = dev;
3593 cmd->host_scribble = (char *)scb;
3596 * Fill out basics of the HSCB.
3599 hscb->scsiid = BUILD_SCSIID(ahc, cmd);
3600 hscb->lun = cmd->device->lun;
3601 mask = SCB_GET_TARGET_MASK(ahc, scb);
3602 tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
3603 SCB_GET_OUR_ID(scb),
3604 SCB_GET_TARGET(ahc, scb), &tstate);
3605 hscb->scsirate = tinfo->scsirate;
3606 hscb->scsioffset = tinfo->curr.offset;
3607 if ((tstate->ultraenb & mask) != 0)
3608 hscb->control |= ULTRAENB;
3610 if ((ahc->user_discenable & mask) != 0)
3611 hscb->control |= DISCENB;
3613 if (AHC_DV_CMD(cmd) != 0)
3614 scb->flags |= SCB_SILENT;
3616 if ((tstate->auto_negotiate & mask) != 0) {
3617 scb->flags |= SCB_AUTO_NEGOTIATE;
3618 scb->hscb->control |= MK_MESSAGE;
3621 if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
3622 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3624 uint8_t tag_msgs[2];
3626 msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
3627 if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
3628 hscb->control |= tag_msgs[0];
3629 if (tag_msgs[0] == MSG_ORDERED_TASK)
3630 dev->commands_since_idle_or_otag = 0;
3633 if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
3634 && (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
3635 hscb->control |= MSG_ORDERED_TASK;
3636 dev->commands_since_idle_or_otag = 0;
3638 hscb->control |= MSG_SIMPLE_TASK;
3642 hscb->cdb_len = cmd->cmd_len;
3643 if (hscb->cdb_len <= 12) {
3644 memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
3646 memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
3647 scb->flags |= SCB_CDB32_PTR;
3650 scb->platform_data->xfer_len = 0;
3651 ahc_set_residual(scb, 0);
3652 ahc_set_sense_residual(scb, 0);
3654 if (cmd->use_sg != 0) {
3655 struct ahc_dma_seg *sg;
3656 struct scatterlist *cur_seg;
3657 struct scatterlist *end_seg;
3660 cur_seg = (struct scatterlist *)cmd->request_buffer;
3661 nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
3662 cmd->sc_data_direction);
3663 end_seg = cur_seg + nseg;
3664 /* Copy the segments into the SG list. */
3667 * The sg_count may be larger than nseg if
3668 * a transfer crosses a 32bit page.
3670 while (cur_seg < end_seg) {
3675 addr = sg_dma_address(cur_seg);
3676 len = sg_dma_len(cur_seg);
3677 consumed = ahc_linux_map_seg(ahc, scb,
3680 scb->sg_count += consumed;
3684 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
3687 * Reset the sg list pointer.
3690 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
3693 * Copy the first SG into the "current"
3694 * data pointer area.
3696 scb->hscb->dataptr = scb->sg_list->addr;
3697 scb->hscb->datacnt = scb->sg_list->len;
3698 } else if (cmd->request_bufflen != 0) {
3699 struct ahc_dma_seg *sg;
3703 addr = pci_map_single(ahc->dev_softc,
3704 cmd->request_buffer,
3705 cmd->request_bufflen,
3706 cmd->sc_data_direction);
3707 scb->platform_data->buf_busaddr = addr;
3708 scb->sg_count = ahc_linux_map_seg(ahc, scb,
3710 cmd->request_bufflen);
3711 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
3714 * Reset the sg list pointer.
3717 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
3720 * Copy the first SG into the "current"
3721 * data pointer area.
3723 scb->hscb->dataptr = sg->addr;
3724 scb->hscb->datacnt = sg->len;
3726 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
3727 scb->hscb->dataptr = 0;
3728 scb->hscb->datacnt = 0;
3732 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_PREWRITE);
3733 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
3736 dev->commands_issued++;
3737 if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
3738 dev->commands_since_idle_or_otag++;
3741 * We only allow one untagged transaction
3742 * per target in the initiator role unless
3743 * we are storing a full busy target *lun*
3744 * table in SCB space.
3746 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
3747 && (ahc->features & AHC_SCB_BTT) == 0) {
3748 struct scb_tailq *untagged_q;
3751 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
3752 untagged_q = &(ahc->untagged_queues[target_offset]);
3753 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
3754 scb->flags |= SCB_UNTAGGEDQ;
3755 if (TAILQ_FIRST(untagged_q) != scb)
3758 scb->flags |= SCB_ACTIVE;
3759 ahc_queue_scb(ahc, scb);
3764 * SCSI controller interrupt handler.
3767 ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
3769 struct ahc_softc *ahc;
3773 ahc = (struct ahc_softc *) dev_id;
3774 ahc_lock(ahc, &flags);
3775 ours = ahc_intr(ahc);
3776 if (ahc_linux_next_device_to_run(ahc) != NULL)
3777 ahc_schedule_runq(ahc);
3778 ahc_linux_run_complete_queue(ahc);
3779 ahc_unlock(ahc, &flags);
3780 return IRQ_RETVAL(ours);
3784 ahc_platform_flushwork(struct ahc_softc *ahc)
3787 while (ahc_linux_run_complete_queue(ahc) != NULL)
3791 static struct ahc_linux_target*
3792 ahc_linux_alloc_target(struct ahc_softc *ahc, u_int channel, u_int target)
3794 struct ahc_linux_target *targ;
3795 u_int target_offset;
3797 target_offset = target;
3801 targ = malloc(sizeof(*targ), M_DEVBUG, M_NOWAIT);
3804 memset(targ, 0, sizeof(*targ));
3805 targ->channel = channel;
3806 targ->target = target;
3808 targ->flags = AHC_DV_REQUIRED;
3809 ahc->platform_data->targets[target_offset] = targ;
3814 ahc_linux_free_target(struct ahc_softc *ahc, struct ahc_linux_target *targ)
3816 struct ahc_devinfo devinfo;
3817 struct ahc_initiator_tinfo *tinfo;
3818 struct ahc_tmode_tstate *tstate;
3820 u_int target_offset;
3824 * Force a negotiation to async/narrow on any
3825 * future command to this device unless a bus
3826 * reset occurs between now and that command.
3828 channel = 'A' + targ->channel;
3829 our_id = ahc->our_id;
3830 target_offset = targ->target;
3831 if (targ->channel != 0) {
3833 our_id = ahc->our_id_b;
3835 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
3836 targ->target, &tstate);
3837 ahc_compile_devinfo(&devinfo, our_id, targ->target, CAM_LUN_WILDCARD,
3838 channel, ROLE_INITIATOR);
3839 ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
3840 AHC_TRANS_GOAL, /*paused*/FALSE);
3841 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
3842 AHC_TRANS_GOAL, /*paused*/FALSE);
3843 ahc_update_neg_request(ahc, &devinfo, tstate, tinfo, AHC_NEG_ALWAYS);
3844 ahc->platform_data->targets[target_offset] = NULL;
3845 if (targ->inq_data != NULL)
3846 free(targ->inq_data, M_DEVBUF);
3847 if (targ->dv_buffer != NULL)
3848 free(targ->dv_buffer, M_DEVBUF);
3849 if (targ->dv_buffer1 != NULL)
3850 free(targ->dv_buffer1, M_DEVBUF);
3851 free(targ, M_DEVBUF);
3854 static struct ahc_linux_device*
3855 ahc_linux_alloc_device(struct ahc_softc *ahc,
3856 struct ahc_linux_target *targ, u_int lun)
3858 struct ahc_linux_device *dev;
3860 dev = malloc(sizeof(*dev), M_DEVBUG, M_NOWAIT);
3863 memset(dev, 0, sizeof(*dev));
3864 init_timer(&dev->timer);
3865 TAILQ_INIT(&dev->busyq);
3866 dev->flags = AHC_DEV_UNCONFIGURED;
3871 * We start out life using untagged
3872 * transactions of which we allow one.
3877 * Set maxtags to 0. This will be changed if we
3878 * later determine that we are dealing with
3879 * a tagged queuing capable device.
3884 targ->devices[lun] = dev;
3889 __ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
3891 struct ahc_linux_target *targ;
3894 targ->devices[dev->lun] = NULL;
3895 free(dev, M_DEVBUF);
3897 if (targ->refcount == 0
3898 && (targ->flags & AHC_DV_REQUIRED) == 0)
3899 ahc_linux_free_target(ahc, targ);
3903 ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
3905 del_timer_sync(&dev->timer);
3906 __ahc_linux_free_device(ahc, dev);
3910 ahc_send_async(struct ahc_softc *ahc, char channel,
3911 u_int target, u_int lun, ac_code code, void *arg)
3914 case AC_TRANSFER_NEG:
3917 struct ahc_linux_target *targ;
3918 struct info_str info;
3919 struct ahc_initiator_tinfo *tinfo;
3920 struct ahc_tmode_tstate *tstate;
3924 info.length = sizeof(buf);
3927 tinfo = ahc_fetch_transinfo(ahc, channel,
3928 channel == 'A' ? ahc->our_id
3933 * Don't bother reporting results while
3934 * negotiations are still pending.
3936 if (tinfo->curr.period != tinfo->goal.period
3937 || tinfo->curr.width != tinfo->goal.width
3938 || tinfo->curr.offset != tinfo->goal.offset
3939 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
3940 if (bootverbose == 0)
3944 * Don't bother reporting results that
3945 * are identical to those last reported.
3947 target_offset = target;
3950 targ = ahc->platform_data->targets[target_offset];
3953 if (tinfo->curr.period == targ->last_tinfo.period
3954 && tinfo->curr.width == targ->last_tinfo.width
3955 && tinfo->curr.offset == targ->last_tinfo.offset
3956 && tinfo->curr.ppr_options == targ->last_tinfo.ppr_options)
3957 if (bootverbose == 0)
3960 targ->last_tinfo.period = tinfo->curr.period;
3961 targ->last_tinfo.width = tinfo->curr.width;
3962 targ->last_tinfo.offset = tinfo->curr.offset;
3963 targ->last_tinfo.ppr_options = tinfo->curr.ppr_options;
3965 printf("(%s:%c:", ahc_name(ahc), channel);
3966 if (target == CAM_TARGET_WILDCARD)
3969 printf("%d): ", target);
3970 ahc_format_transinfo(&info, &tinfo->curr);
3971 if (info.pos < info.length)
3972 *info.buffer = '\0';
3974 buf[info.length - 1] = '\0';
3980 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3981 WARN_ON(lun != CAM_LUN_WILDCARD);
3982 scsi_report_device_reset(ahc->platform_data->host,
3983 channel - 'A', target);
3985 Scsi_Device *scsi_dev;
3988 * Find the SCSI device associated with this
3989 * request and indicate that a UA is expected.
3991 for (scsi_dev = ahc->platform_data->host->host_queue;
3992 scsi_dev != NULL; scsi_dev = scsi_dev->next) {
3993 if (channel - 'A' == scsi_dev->channel
3994 && target == scsi_dev->id
3995 && (lun == CAM_LUN_WILDCARD
3996 || lun == scsi_dev->lun)) {
3997 scsi_dev->was_reset = 1;
3998 scsi_dev->expecting_cc_ua = 1;
4005 if (ahc->platform_data->host != NULL) {
4006 scsi_report_bus_reset(ahc->platform_data->host,
4011 panic("ahc_send_async: Unexpected async event");
4016 * Calls the higher level scsi done function and frees the scb.
4019 ahc_done(struct ahc_softc *ahc, struct scb *scb)
4022 struct ahc_linux_device *dev;
4024 LIST_REMOVE(scb, pending_links);
4025 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
4026 struct scb_tailq *untagged_q;
4029 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
4030 untagged_q = &(ahc->untagged_queues[target_offset]);
4031 TAILQ_REMOVE(untagged_q, scb, links.tqe);
4032 ahc_run_untagged_queue(ahc, untagged_q);
4035 if ((scb->flags & SCB_ACTIVE) == 0) {
4036 printf("SCB %d done'd twice\n", scb->hscb->tag);
4037 ahc_dump_card_state(ahc);
4038 panic("Stopping for safety");
4041 dev = scb->platform_data->dev;
4044 if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
4045 cmd->result &= ~(CAM_DEV_QFRZN << 16);
4048 ahc_linux_unmap_scb(ahc, scb);
4051 * Guard against stale sense data.
4052 * The Linux mid-layer assumes that sense
4053 * was retrieved anytime the first byte of
4054 * the sense buffer looks "sane".
4056 cmd->sense_buffer[0] = 0;
4057 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
4058 uint32_t amount_xferred;
4061 ahc_get_transfer_length(scb) - ahc_get_residual(scb);
4062 if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
4064 if ((ahc_debug & AHC_SHOW_MISC) != 0) {
4065 ahc_print_path(ahc, scb);
4066 printf("Set CAM_UNCOR_PARITY\n");
4069 ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
4070 #ifdef AHC_REPORT_UNDERFLOWS
4072 * This code is disabled by default as some
4073 * clients of the SCSI system do not properly
4074 * initialize the underflow parameter. This
4075 * results in spurious termination of commands
4076 * that complete as expected (e.g. underflow is
4077 * allowed as command can return variable amounts
4080 } else if (amount_xferred < scb->io_ctx->underflow) {
4083 ahc_print_path(ahc, scb);
4085 for (i = 0; i < scb->io_ctx->cmd_len; i++)
4086 printf(" 0x%x", scb->io_ctx->cmnd[i]);
4088 ahc_print_path(ahc, scb);
4089 printf("Saw underflow (%ld of %ld bytes). "
4090 "Treated as error\n",
4091 ahc_get_residual(scb),
4092 ahc_get_transfer_length(scb));
4093 ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
4096 ahc_set_transaction_status(scb, CAM_REQ_CMP);
4098 } else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
4099 ahc_linux_handle_scsi_status(ahc, dev, scb);
4100 } else if (ahc_get_transaction_status(scb) == CAM_SEL_TIMEOUT) {
4101 dev->flags |= AHC_DEV_UNCONFIGURED;
4102 if (AHC_DV_CMD(cmd) == FALSE)
4103 dev->target->flags &= ~AHC_DV_REQUIRED;
4106 * Start DV for devices that require it assuming the first command
4107 * sent does not result in a selection timeout.
4109 if (ahc_get_transaction_status(scb) != CAM_SEL_TIMEOUT
4110 && (dev->target->flags & AHC_DV_REQUIRED) != 0)
4111 ahc_linux_start_dv(ahc);
4113 if (dev->openings == 1
4114 && ahc_get_transaction_status(scb) == CAM_REQ_CMP
4115 && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
4116 dev->tag_success_count++;
4118 * Some devices deal with temporary internal resource
4119 * shortages by returning queue full. When the queue
4120 * full occurrs, we throttle back. Slowly try to get
4121 * back to our previous queue depth.
4123 if ((dev->openings + dev->active) < dev->maxtags
4124 && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
4125 dev->tag_success_count = 0;
4129 if (dev->active == 0)
4130 dev->commands_since_idle_or_otag = 0;
4132 if (TAILQ_EMPTY(&dev->busyq)) {
4133 if ((dev->flags & AHC_DEV_UNCONFIGURED) != 0
4135 && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0)
4136 ahc_linux_free_device(ahc, dev);
4137 } else if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) {
4138 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links);
4139 dev->flags |= AHC_DEV_ON_RUN_LIST;
4142 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
4143 printf("Recovery SCB completes\n");
4144 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
4145 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
4146 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
4147 if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
4148 ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
4149 up(&ahc->platform_data->eh_sem);
4153 ahc_free_scb(ahc, scb);
4154 ahc_linux_queue_cmd_complete(ahc, cmd);
4156 if ((ahc->platform_data->flags & AHC_DV_WAIT_SIMQ_EMPTY) != 0
4157 && LIST_FIRST(&ahc->pending_scbs) == NULL) {
4158 ahc->platform_data->flags &= ~AHC_DV_WAIT_SIMQ_EMPTY;
4159 up(&ahc->platform_data->dv_sem);
4165 ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
4166 struct ahc_linux_device *dev, struct scb *scb)
4168 struct ahc_devinfo devinfo;
4170 ahc_compile_devinfo(&devinfo,
4172 dev->target->target, dev->lun,
4173 dev->target->channel == 0 ? 'A' : 'B',
4177 * We don't currently trust the mid-layer to
4178 * properly deal with queue full or busy. So,
4179 * when one occurs, we tell the mid-layer to
4180 * unconditionally requeue the command to us
4181 * so that we can retry it ourselves. We also
4182 * implement our own throttling mechanism so
4183 * we don't clobber the device with too many
4186 switch (ahc_get_scsi_status(scb)) {
4189 case SCSI_STATUS_CHECK_COND:
4190 case SCSI_STATUS_CMD_TERMINATED:
4195 * Copy sense information to the OS's cmd
4196 * structure if it is available.
4199 if (scb->flags & SCB_SENSE) {
4202 sense_size = MIN(sizeof(struct scsi_sense_data)
4203 - ahc_get_sense_residual(scb),
4204 sizeof(cmd->sense_buffer));
4205 memcpy(cmd->sense_buffer,
4206 ahc_get_sense_buf(ahc, scb), sense_size);
4207 if (sense_size < sizeof(cmd->sense_buffer))
4208 memset(&cmd->sense_buffer[sense_size], 0,
4209 sizeof(cmd->sense_buffer) - sense_size);
4210 cmd->result |= (DRIVER_SENSE << 24);
4212 if (ahc_debug & AHC_SHOW_SENSE) {
4215 printf("Copied %d bytes of sense data:",
4217 for (i = 0; i < sense_size; i++) {
4220 printf("0x%x ", cmd->sense_buffer[i]);
4228 case SCSI_STATUS_QUEUE_FULL:
4231 * By the time the core driver has returned this
4232 * command, all other commands that were queued
4233 * to us but not the device have been returned.
4234 * This ensures that dev->active is equal to
4235 * the number of commands actually queued to
4238 dev->tag_success_count = 0;
4239 if (dev->active != 0) {
4241 * Drop our opening count to the number
4242 * of commands currently outstanding.
4246 ahc_print_path(ahc, scb);
4247 printf("Dropping tag count to %d\n", dev->active);
4249 if (dev->active == dev->tags_on_last_queuefull) {
4251 dev->last_queuefull_same_count++;
4253 * If we repeatedly see a queue full
4254 * at the same queue depth, this
4255 * device has a fixed number of tag
4256 * slots. Lock in this tag depth
4257 * so we stop seeing queue fulls from
4260 if (dev->last_queuefull_same_count
4261 == AHC_LOCK_TAGS_COUNT) {
4262 dev->maxtags = dev->active;
4263 ahc_print_path(ahc, scb);
4264 printf("Locking max tag count at %d\n",
4268 dev->tags_on_last_queuefull = dev->active;
4269 dev->last_queuefull_same_count = 0;
4271 ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
4272 ahc_set_scsi_status(scb, SCSI_STATUS_OK);
4273 ahc_platform_set_tags(ahc, &devinfo,
4274 (dev->flags & AHC_DEV_Q_BASIC)
4275 ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
4279 * Drop down to a single opening, and treat this
4280 * as if the target returned BUSY SCSI status.
4283 ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
4284 ahc_platform_set_tags(ahc, &devinfo,
4285 (dev->flags & AHC_DEV_Q_BASIC)
4286 ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
4289 case SCSI_STATUS_BUSY:
4292 * Set a short timer to defer sending commands for
4293 * a bit since Linux will not delay in this case.
4295 if ((dev->flags & AHC_DEV_TIMER_ACTIVE) != 0) {
4296 printf("%s:%c:%d: Device Timer still active during "
4297 "busy processing\n", ahc_name(ahc),
4298 dev->target->channel, dev->target->target);
4301 dev->flags |= AHC_DEV_TIMER_ACTIVE;
4303 init_timer(&dev->timer);
4304 dev->timer.data = (u_long)dev;
4305 dev->timer.expires = jiffies + (HZ/2);
4306 dev->timer.function = ahc_linux_dev_timed_unfreeze;
4307 add_timer(&dev->timer);
4314 ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, Scsi_Cmnd *cmd)
4317 * Typically, the complete queue has very few entries
4318 * queued to it before the queue is emptied by
4319 * ahc_linux_run_complete_queue, so sorting the entries
4320 * by generation number should be inexpensive.
4321 * We perform the sort so that commands that complete
4322 * with an error are retuned in the order origionally
4323 * queued to the controller so that any subsequent retries
4324 * are performed in order. The underlying ahc routines do
4325 * not guarantee the order that aborted commands will be
4328 struct ahc_completeq *completeq;
4329 struct ahc_cmd *list_cmd;
4330 struct ahc_cmd *acmd;
4333 * Map CAM error codes into Linux Error codes. We
4334 * avoid the conversion so that the DV code has the
4335 * full error information available when making
4336 * state change decisions.
4338 if (AHC_DV_CMD(cmd) == FALSE) {
4341 switch (ahc_cmd_get_transaction_status(cmd)) {
4342 case CAM_REQ_INPROG:
4344 case CAM_SCSI_STATUS_ERROR:
4345 new_status = DID_OK;
4347 case CAM_REQ_ABORTED:
4348 new_status = DID_ABORT;
4351 new_status = DID_BUS_BUSY;
4353 case CAM_REQ_INVALID:
4354 case CAM_PATH_INVALID:
4355 new_status = DID_BAD_TARGET;
4357 case CAM_SEL_TIMEOUT:
4358 new_status = DID_NO_CONNECT;
4360 case CAM_SCSI_BUS_RESET:
4362 new_status = DID_RESET;
4364 case CAM_UNCOR_PARITY:
4365 new_status = DID_PARITY;
4367 case CAM_CMD_TIMEOUT:
4368 new_status = DID_TIME_OUT;
4371 case CAM_REQ_CMP_ERR:
4372 case CAM_AUTOSENSE_FAIL:
4374 case CAM_DATA_RUN_ERR:
4375 case CAM_UNEXP_BUSFREE:
4376 case CAM_SEQUENCE_FAIL:
4377 case CAM_CCB_LEN_ERR:
4378 case CAM_PROVIDE_FAIL:
4379 case CAM_REQ_TERMIO:
4380 case CAM_UNREC_HBA_ERROR:
4381 case CAM_REQ_TOO_BIG:
4382 new_status = DID_ERROR;
4384 case CAM_REQUEUE_REQ:
4386 * If we want the request requeued, make sure there
4387 * are sufficent retries. In the old scsi error code,
4388 * we used to be able to specify a result code that
4389 * bypassed the retry count. Now we must use this
4390 * hack. We also "fake" a check condition with
4391 * a sense code of ABORTED COMMAND. This seems to
4392 * evoke a retry even if this command is being sent
4393 * via the eh thread. Ick! Ick! Ick!
4395 if (cmd->retries > 0)
4397 new_status = DID_OK;
4398 ahc_cmd_set_scsi_status(cmd, SCSI_STATUS_CHECK_COND);
4399 cmd->result |= (DRIVER_SENSE << 24);
4400 memset(cmd->sense_buffer, 0,
4401 sizeof(cmd->sense_buffer));
4402 cmd->sense_buffer[0] = SSD_ERRCODE_VALID
4403 | SSD_CURRENT_ERROR;
4404 cmd->sense_buffer[2] = SSD_KEY_ABORTED_COMMAND;
4407 /* We should never get here */
4408 new_status = DID_ERROR;
4412 ahc_cmd_set_transaction_status(cmd, new_status);
4415 completeq = &ahc->platform_data->completeq;
4416 list_cmd = TAILQ_FIRST(completeq);
4417 acmd = (struct ahc_cmd *)cmd;
4418 while (list_cmd != NULL
4419 && acmd_scsi_cmd(list_cmd).serial_number
4420 < acmd_scsi_cmd(acmd).serial_number)
4421 list_cmd = TAILQ_NEXT(list_cmd, acmd_links.tqe);
4422 if (list_cmd != NULL)
4423 TAILQ_INSERT_BEFORE(list_cmd, acmd, acmd_links.tqe);
4425 TAILQ_INSERT_TAIL(completeq, acmd, acmd_links.tqe);
4429 ahc_linux_filter_inquiry(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
4431 struct scsi_inquiry_data *sid;
4432 struct ahc_initiator_tinfo *tinfo;
4433 struct ahc_transinfo *user;
4434 struct ahc_transinfo *goal;
4435 struct ahc_transinfo *curr;
4436 struct ahc_tmode_tstate *tstate;
4437 struct ahc_syncrate *syncrate;
4438 struct ahc_linux_device *dev;
4444 u_int trans_version;
4448 * Determine if this lun actually exists. If so,
4449 * hold on to its corresponding device structure.
4450 * If not, make sure we release the device and
4451 * don't bother processing the rest of this inquiry
4454 dev = ahc_linux_get_device(ahc, devinfo->channel - 'A',
4455 devinfo->target, devinfo->lun,
4458 sid = (struct scsi_inquiry_data *)dev->target->inq_data;
4459 if (SID_QUAL(sid) == SID_QUAL_LU_CONNECTED) {
4461 dev->flags &= ~AHC_DEV_UNCONFIGURED;
4463 dev->flags |= AHC_DEV_UNCONFIGURED;
4468 * Update our notion of this device's transfer
4469 * negotiation capabilities.
4471 tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
4472 devinfo->our_scsiid,
4473 devinfo->target, &tstate);
4474 user = &tinfo->user;
4475 goal = &tinfo->goal;
4476 curr = &tinfo->curr;
4477 width = user->width;
4478 period = user->period;
4479 offset = user->offset;
4480 ppr_options = user->ppr_options;
4481 trans_version = user->transport_version;
4482 prot_version = MIN(user->protocol_version, SID_ANSI_REV(sid));
4485 * Only attempt SPI3/4 once we've verified that
4486 * the device claims to support SPI3/4 features.
4488 if (prot_version < SCSI_REV_2)
4489 trans_version = SID_ANSI_REV(sid);
4491 trans_version = SCSI_REV_2;
4493 if ((sid->flags & SID_WBus16) == 0)
4494 width = MSG_EXT_WDTR_BUS_8_BIT;
4495 if ((sid->flags & SID_Sync) == 0) {
4500 if ((sid->spi3data & SID_SPI_QAS) == 0)
4501 ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
4502 if ((sid->spi3data & SID_SPI_CLOCK_DT) == 0)
4503 ppr_options &= MSG_EXT_PPR_QAS_REQ;
4504 if ((sid->spi3data & SID_SPI_IUS) == 0)
4505 ppr_options &= (MSG_EXT_PPR_DT_REQ
4506 | MSG_EXT_PPR_QAS_REQ);
4508 if (prot_version > SCSI_REV_2
4509 && ppr_options != 0)
4510 trans_version = user->transport_version;
4512 ahc_validate_width(ahc, /*tinfo limit*/NULL, &width, ROLE_UNKNOWN);
4513 if ((ahc->features & AHC_ULTRA2) != 0)
4514 maxsync = AHC_SYNCRATE_DT;
4515 else if ((ahc->features & AHC_ULTRA) != 0)
4516 maxsync = AHC_SYNCRATE_ULTRA;
4518 maxsync = AHC_SYNCRATE_FAST;
4520 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, maxsync);
4521 ahc_validate_offset(ahc, /*tinfo limit*/NULL, syncrate,
4522 &offset, width, ROLE_UNKNOWN);
4523 if (offset == 0 || period == 0) {
4528 /* Apply our filtered user settings. */
4529 curr->transport_version = trans_version;
4530 curr->protocol_version = prot_version;
4531 ahc_set_width(ahc, devinfo, width, AHC_TRANS_GOAL, /*paused*/FALSE);
4532 ahc_set_syncrate(ahc, devinfo, syncrate, period,
4533 offset, ppr_options, AHC_TRANS_GOAL,
4538 ahc_linux_sem_timeout(u_long arg)
4540 struct ahc_softc *ahc;
4543 ahc = (struct ahc_softc *)arg;
4546 if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
4547 ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
4548 up(&ahc->platform_data->eh_sem);
4550 ahc_unlock(ahc, &s);
4554 ahc_linux_freeze_simq(struct ahc_softc *ahc)
4556 ahc->platform_data->qfrozen++;
4557 if (ahc->platform_data->qfrozen == 1) {
4558 scsi_block_requests(ahc->platform_data->host);
4560 /* XXX What about Twin channels? */
4561 ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
4562 CAM_LUN_WILDCARD, SCB_LIST_NULL,
4563 ROLE_INITIATOR, CAM_REQUEUE_REQ);
4568 ahc_linux_release_simq(u_long arg)
4570 struct ahc_softc *ahc;
4574 ahc = (struct ahc_softc *)arg;
4578 if (ahc->platform_data->qfrozen > 0)
4579 ahc->platform_data->qfrozen--;
4580 if (ahc->platform_data->qfrozen == 0)
4582 if (AHC_DV_SIMQ_FROZEN(ahc)
4583 && ((ahc->platform_data->flags & AHC_DV_WAIT_SIMQ_RELEASE) != 0)) {
4584 ahc->platform_data->flags &= ~AHC_DV_WAIT_SIMQ_RELEASE;
4585 up(&ahc->platform_data->dv_sem);
4587 ahc_schedule_runq(ahc);
4588 ahc_unlock(ahc, &s);
4590 * There is still a race here. The mid-layer
4591 * should keep its own freeze count and use
4592 * a bottom half handler to run the queues
4593 * so we can unblock with our own lock held.
4596 scsi_unblock_requests(ahc->platform_data->host);
4600 ahc_linux_dev_timed_unfreeze(u_long arg)
4602 struct ahc_linux_device *dev;
4603 struct ahc_softc *ahc;
4606 dev = (struct ahc_linux_device *)arg;
4607 ahc = dev->target->ahc;
4609 dev->flags &= ~AHC_DEV_TIMER_ACTIVE;
4610 if (dev->qfrozen > 0)
4612 if (dev->qfrozen == 0
4613 && (dev->flags & AHC_DEV_ON_RUN_LIST) == 0)
4614 ahc_linux_run_device_queue(ahc, dev);
4615 if (TAILQ_EMPTY(&dev->busyq)
4616 && dev->active == 0)
4617 __ahc_linux_free_device(ahc, dev);
4618 ahc_unlock(ahc, &s);
4622 ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag)
4624 struct ahc_softc *ahc;
4625 struct ahc_cmd *acmd;
4626 struct ahc_cmd *list_acmd;
4627 struct ahc_linux_device *dev;
4628 struct scb *pending_scb;
4631 u_int active_scb_index;
4644 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
4645 acmd = (struct ahc_cmd *)cmd;
4647 printf("%s:%d:%d:%d: Attempting to queue a%s message\n",
4648 ahc_name(ahc), cmd->device->channel,
4649 cmd->device->id, cmd->device->lun,
4650 flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
4653 for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
4654 printf(" 0x%x", cmd->cmnd[cdb_byte]);
4658 * In all versions of Linux, we have to work around
4659 * a major flaw in how the mid-layer is locked down
4660 * if we are to sleep successfully in our error handler
4661 * while allowing our interrupt handler to run. Since
4662 * the midlayer acquires either the io_request_lock or
4663 * our lock prior to calling us, we must use the
4664 * spin_unlock_irq() method for unlocking our lock.
4665 * This will force interrupts to be enabled on the
4666 * current CPU. Since the EH thread should not have
4667 * been running with CPU interrupts disabled other than
4668 * by acquiring either the io_request_lock or our own
4669 * lock, this *should* be safe.
4671 ahc_midlayer_entrypoint_lock(ahc, &s);
4674 * First determine if we currently own this command.
4675 * Start by searching the device queue. If not found
4676 * there, check the pending_scb list. If not found
4677 * at all, and the system wanted us to just abort the
4678 * command, return success.
4680 dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
4681 cmd->device->lun, /*alloc*/FALSE);
4685 * No target device for this command exists,
4686 * so we must not still own the command.
4688 printf("%s:%d:%d:%d: Is not an active device\n",
4689 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4695 TAILQ_FOREACH(list_acmd, &dev->busyq, acmd_links.tqe) {
4696 if (list_acmd == acmd)
4700 if (list_acmd != NULL) {
4701 printf("%s:%d:%d:%d: Command found on device queue\n",
4702 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4704 if (flag == SCB_ABORT) {
4705 TAILQ_REMOVE(&dev->busyq, list_acmd, acmd_links.tqe);
4706 cmd->result = DID_ABORT << 16;
4707 ahc_linux_queue_cmd_complete(ahc, cmd);
4713 if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
4714 && ahc_search_untagged_queues(ahc, cmd, cmd->device->id,
4715 cmd->device->channel + 'A',
4717 CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
4718 printf("%s:%d:%d:%d: Command found on untagged queue\n",
4719 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4726 * See if we can find a matching cmd in the pending list.
4728 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
4729 if (pending_scb->io_ctx == cmd)
4733 if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
4735 /* Any SCB for this device will do for a target reset */
4736 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
4737 if (ahc_match_scb(ahc, pending_scb, cmd->device->id,
4738 cmd->device->channel + 'A',
4740 SCB_LIST_NULL, ROLE_INITIATOR) == 0)
4745 if (pending_scb == NULL) {
4746 printf("%s:%d:%d:%d: Command not found\n",
4747 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4752 if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
4754 * We can't queue two recovery actions using the same SCB
4761 * Ensure that the card doesn't do anything
4762 * behind our back and that we didn't "just" miss
4763 * an interrupt that would affect this cmd.
4765 was_paused = ahc_is_paused(ahc);
4766 ahc_pause_and_flushwork(ahc);
4769 if ((pending_scb->flags & SCB_ACTIVE) == 0) {
4770 printf("%s:%d:%d:%d: Command already completed\n",
4771 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4776 printf("%s: At time of recovery, card was %spaused\n",
4777 ahc_name(ahc), was_paused ? "" : "not ");
4778 ahc_dump_card_state(ahc);
4780 disconnected = TRUE;
4781 if (flag == SCB_ABORT) {
4782 if (ahc_search_qinfifo(ahc, cmd->device->id,
4783 cmd->device->channel + 'A',
4785 pending_scb->hscb->tag,
4786 ROLE_INITIATOR, CAM_REQ_ABORTED,
4787 SEARCH_COMPLETE) > 0) {
4788 printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
4789 ahc_name(ahc), cmd->device->channel,
4790 cmd->device->id, cmd->device->lun);
4794 } else if (ahc_search_qinfifo(ahc, cmd->device->id,
4795 cmd->device->channel + 'A',
4796 cmd->device->lun, pending_scb->hscb->tag,
4797 ROLE_INITIATOR, /*status*/0,
4798 SEARCH_COUNT) > 0) {
4799 disconnected = FALSE;
4802 if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
4803 struct scb *bus_scb;
4805 bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
4806 if (bus_scb == pending_scb)
4807 disconnected = FALSE;
4808 else if (flag != SCB_ABORT
4809 && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
4810 && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb))
4811 disconnected = FALSE;
4815 * At this point, pending_scb is the scb associated with the
4816 * passed in command. That command is currently active on the
4817 * bus, is in the disconnected state, or we're hoping to find
4818 * a command for the same target active on the bus to abuse to
4819 * send a BDR. Queue the appropriate message based on which of
4820 * these states we are in.
4822 last_phase = ahc_inb(ahc, LASTPHASE);
4823 saved_scbptr = ahc_inb(ahc, SCBPTR);
4824 active_scb_index = ahc_inb(ahc, SCB_TAG);
4825 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
4826 if (last_phase != P_BUSFREE
4827 && (pending_scb->hscb->tag == active_scb_index
4828 || (flag == SCB_DEVICE_RESET
4829 && SCSIID_TARGET(ahc, saved_scsiid) == cmd->device->id))) {
4832 * We're active on the bus, so assert ATN
4833 * and hope that the target responds.
4835 pending_scb = ahc_lookup_scb(ahc, active_scb_index);
4836 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
4837 ahc_outb(ahc, MSG_OUT, HOST_MSG);
4838 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
4839 printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
4840 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4843 } else if (disconnected) {
4846 * Actually re-queue this SCB in an attempt
4847 * to select the device before it reconnects.
4848 * In either case (selection or reselection),
4849 * we will now issue the approprate message
4850 * to the timed-out device.
4852 * Set the MK_MESSAGE control bit indicating
4853 * that we desire to send a message. We
4854 * also set the disconnected flag since
4855 * in the paging case there is no guarantee
4856 * that our SCB control byte matches the
4857 * version on the card. We don't want the
4858 * sequencer to abort the command thinking
4859 * an unsolicited reselection occurred.
4861 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
4862 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
4865 * Remove any cached copy of this SCB in the
4866 * disconnected list in preparation for the
4867 * queuing of our abort SCB. We use the
4868 * same element in the SCB, SCB_NEXT, for
4869 * both the qinfifo and the disconnected list.
4871 ahc_search_disc_list(ahc, cmd->device->id,
4872 cmd->device->channel + 'A',
4873 cmd->device->lun, pending_scb->hscb->tag,
4874 /*stop_on_first*/TRUE,
4876 /*save_state*/FALSE);
4879 * In the non-paging case, the sequencer will
4880 * never re-reference the in-core SCB.
4881 * To make sure we are notified during
4882 * reslection, set the MK_MESSAGE flag in
4883 * the card's copy of the SCB.
4885 if ((ahc->flags & AHC_PAGESCBS) == 0) {
4886 ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
4887 ahc_outb(ahc, SCB_CONTROL,
4888 ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
4892 * Clear out any entries in the QINFIFO first
4893 * so we are the next SCB for this target
4896 ahc_search_qinfifo(ahc, cmd->device->id,
4897 cmd->device->channel + 'A',
4898 cmd->device->lun, SCB_LIST_NULL,
4899 ROLE_INITIATOR, CAM_REQUEUE_REQ,
4901 ahc_qinfifo_requeue_tail(ahc, pending_scb);
4902 ahc_outb(ahc, SCBPTR, saved_scbptr);
4903 ahc_print_path(ahc, pending_scb);
4904 printf("Device is disconnected, re-queuing SCB\n");
4907 printf("%s:%d:%d:%d: Unable to deliver message\n",
4908 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4916 * Our assumption is that if we don't have the command, no
4917 * recovery action was required, so we return success. Again,
4918 * the semantics of the mid-layer recovery engine are not
4919 * well defined, so this may change in time.
4926 struct timer_list timer;
4929 ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE;
4930 spin_unlock_irq(&ahc->platform_data->spin_lock);
4932 timer.data = (u_long)ahc;
4933 timer.expires = jiffies + (5 * HZ);
4934 timer.function = ahc_linux_sem_timeout;
4936 printf("Recovery code sleeping\n");
4937 down(&ahc->platform_data->eh_sem);
4938 printf("Recovery code awake\n");
4939 ret = del_timer_sync(&timer);
4941 printf("Timer Expired\n");
4944 spin_lock_irq(&ahc->platform_data->spin_lock);
4946 ahc_schedule_runq(ahc);
4947 ahc_linux_run_complete_queue(ahc);
4948 ahc_midlayer_entrypoint_unlock(ahc, &s);
4953 ahc_platform_dump_card_state(struct ahc_softc *ahc)
4955 struct ahc_linux_device *dev;
4963 maxchannel = (ahc->features & AHC_TWIN) ? 1 : 0;
4964 maxtarget = (ahc->features & AHC_WIDE) ? 15 : 7;
4965 for (channel = 0; channel <= maxchannel; channel++) {
4967 for (target = 0; target <=maxtarget; target++) {
4969 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
4970 struct ahc_cmd *acmd;
4972 dev = ahc_linux_get_device(ahc, channel, target,
4973 lun, /*alloc*/FALSE);
4977 printf("DevQ(%d:%d:%d): ",
4978 channel, target, lun);
4980 TAILQ_FOREACH(acmd, &dev->busyq,
4982 if (i++ > AHC_SCB_MAX)
4985 printf("%d waiting\n", i);
4991 static void ahc_linux_exit(void);
4994 ahc_linux_init(void)
4996 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
4997 int rc = ahc_linux_detect(&aic7xxx_driver_template);
5003 scsi_register_module(MODULE_SCSI_HA, &aic7xxx_driver_template);
5004 if (aic7xxx_driver_template.present == 0) {
5005 scsi_unregister_module(MODULE_SCSI_HA,
5006 &aic7xxx_driver_template);
5015 ahc_linux_exit(void)
5017 struct ahc_softc *ahc;
5020 * Shutdown DV threads before going into the SCSI mid-layer.
5021 * This avoids situations where the mid-layer locks the entire
5022 * kernel so that waiting for our DV threads to exit leads
5025 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
5027 ahc_linux_kill_dv_thread(ahc);
5030 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
5032 * In 2.4 we have to unregister from the PCI core _after_
5033 * unregistering from the scsi midlayer to avoid dangling
5036 scsi_unregister_module(MODULE_SCSI_HA, &aic7xxx_driver_template);
5038 ahc_linux_pci_exit();
5039 ahc_linux_eisa_exit();
5042 module_init(ahc_linux_init);
5043 module_exit(ahc_linux_exit);