blob: 031c6aaa5ca571456d1d4f8f3e0eabad56ef725e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AIC7xxx device driver for Linux.
3 *
4 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.c#235 $
5 *
6 * Copyright (c) 1994 John Aycock
7 * The University of Calgary Department of Computer Science.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Sources include the Adaptec 1740 driver (aha1740.c), the Ultrastor 24F
24 * driver (ultrastor.c), various Linux kernel source, the Adaptec EISA
25 * config file (!adp7771.cfg), the Adaptec AHA-2740A Series User's Guide,
26 * the Linux Kernel Hacker's Guide, Writing a SCSI Device Driver for Linux,
27 * the Adaptec 1542 driver (aha1542.c), the Adaptec EISA overlay file
28 * (adp7770.ovl), the Adaptec AHA-2740 Series Technical Reference Manual,
29 * the Adaptec AIC-7770 Data Book, the ANSI SCSI specification, the
30 * ANSI SCSI-2 specification (draft 10c), ...
31 *
32 * --------------------------------------------------------------------------
33 *
34 * Modifications by Daniel M. Eischen (deischen@iworks.InterWorks.org):
35 *
36 * Substantially modified to include support for wide and twin bus
37 * adapters, DMAing of SCBs, tagged queueing, IRQ sharing, bug fixes,
38 * SCB paging, and other rework of the code.
39 *
40 * --------------------------------------------------------------------------
41 * Copyright (c) 1994-2000 Justin T. Gibbs.
42 * Copyright (c) 2000-2001 Adaptec Inc.
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions, and the following disclaimer,
50 * without modification.
51 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
52 * substantially similar to the "NO WARRANTY" disclaimer below
53 * ("Disclaimer") and any redistribution must be conditioned upon
54 * including a substantially similar Disclaimer requirement for further
55 * binary redistribution.
56 * 3. Neither the names of the above-listed copyright holders nor the names
57 * of any contributors may be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 *
60 * Alternatively, this software may be distributed under the terms of the
61 * GNU General Public License ("GPL") version 2 as published by the Free
62 * Software Foundation.
63 *
64 * NO WARRANTY
65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
66 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
67 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
68 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
69 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
73 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
74 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
75 * POSSIBILITY OF SUCH DAMAGES.
76 *
77 *---------------------------------------------------------------------------
78 *
79 * Thanks also go to (in alphabetical order) the following:
80 *
81 * Rory Bolt - Sequencer bug fixes
82 * Jay Estabrook - Initial DEC Alpha support
83 * Doug Ledford - Much needed abort/reset bug fixes
84 * Kai Makisara - DMAing of SCBs
85 *
86 * A Boot time option was also added for not resetting the scsi bus.
87 *
88 * Form: aic7xxx=extended
89 * aic7xxx=no_reset
90 * aic7xxx=verbose
91 *
92 * Daniel M. Eischen, deischen@iworks.InterWorks.org, 1/23/97
93 *
94 * Id: aic7xxx.c,v 4.1 1997/06/12 08:23:42 deang Exp
95 */
96
97/*
98 * Further driver modifications made by Doug Ledford <dledford@redhat.com>
99 *
100 * Copyright (c) 1997-1999 Doug Ledford
101 *
102 * These changes are released under the same licensing terms as the FreeBSD
103 * driver written by Justin Gibbs. Please see his Copyright notice above
104 * for the exact terms and conditions covering my changes as well as the
105 * warranty statement.
106 *
107 * Modifications made to the aic7xxx.c,v 4.1 driver from Dan Eischen include
108 * but are not limited to:
109 *
110 * 1: Import of the latest FreeBSD sequencer code for this driver
111 * 2: Modification of kernel code to accommodate different sequencer semantics
112 * 3: Extensive changes throughout kernel portion of driver to improve
113 * abort/reset processing and error hanndling
114 * 4: Other work contributed by various people on the Internet
115 * 5: Changes to printk information and verbosity selection code
116 * 6: General reliability related changes, especially in IRQ management
117 * 7: Modifications to the default probe/attach order for supported cards
118 * 8: SMP friendliness has been improved
119 *
120 */
121
122#include "aic7xxx_osm.h"
123#include "aic7xxx_inline.h"
124#include <scsi/scsicam.h>
125
126/*
127 * Include aiclib.c as part of our
128 * "module dependencies are hard" work around.
129 */
130#include "aiclib.c"
131
132#include <linux/init.h> /* __setup */
133
134#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
135#include "sd.h" /* For geometry detection */
136#endif
137
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 */
141
142/*
143 * Lock protecting manipulation of the ahc softc list.
144 */
145spinlock_t ahc_list_spinlock;
146
147#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
148/* For dynamic sglist size calculation. */
149u_int ahc_linux_nseg;
150#endif
151
152/*
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.
157 */
158#ifdef CONFIG_AIC7XXX_RESET_DELAY_MS
159#define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS
160#else
161#define AIC7XXX_RESET_DELAY 5000
162#endif
163
164/*
165 * Control collection of SCSI transfer statistics for the /proc filesystem.
166 *
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.
169 */
170#ifdef CONFIG_AIC7XXX_PROC_STATS
171#define AIC7XXX_PROC_STATS
172#endif
173
174/*
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.
180 *
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
183 * for channel 1.
184 */
185typedef struct {
186 uint8_t tag_commands[16]; /* Allow for wide/twin adapters. */
187} adapter_tag_info_t;
188
189/*
190 * Modify this as you see fit for your system.
191 *
192 * 0 tagged queuing disabled
193 * 1 <= n <= 253 n == max tags ever dispatched.
194 *
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.
204 *
205 * In this example, the first line will disable tagged queueing for all
206 * the devices on the first probed aic7xxx adapter.
207 *
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.
211 *
212 * The third line is the same as the first line.
213 *
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.
218 */
219
220/*
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.
223adapter_tag_info_t aic7xxx_tag_info[] =
224{
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}}
229};
230*/
231
232#ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
233#define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
234#else
235#define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE
236#endif
237
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 \
247}
248
249/*
250 * By default, use the number of commands specified by
251 * the users kernel configuration.
252 */
253static adapter_tag_info_t aic7xxx_tag_info[] =
254{
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}
271};
272
273/*
274 * DV option:
275 *
276 * positive value = DV Enabled
277 * zero = DV Disabled
278 * negative value = DV Default for adapter type/seeprom
279 */
280#ifdef CONFIG_AIC7XXX_DV_SETTING
281#define AIC7XXX_CONFIGED_DV CONFIG_AIC7XXX_DV_SETTING
282#else
283#define AIC7XXX_CONFIGED_DV -1
284#endif
285
286static int8_t aic7xxx_dv_settings[] =
287{
288 AIC7XXX_CONFIGED_DV,
289 AIC7XXX_CONFIGED_DV,
290 AIC7XXX_CONFIGED_DV,
291 AIC7XXX_CONFIGED_DV,
292 AIC7XXX_CONFIGED_DV,
293 AIC7XXX_CONFIGED_DV,
294 AIC7XXX_CONFIGED_DV,
295 AIC7XXX_CONFIGED_DV,
296 AIC7XXX_CONFIGED_DV,
297 AIC7XXX_CONFIGED_DV,
298 AIC7XXX_CONFIGED_DV,
299 AIC7XXX_CONFIGED_DV,
300 AIC7XXX_CONFIGED_DV,
301 AIC7XXX_CONFIGED_DV,
302 AIC7XXX_CONFIGED_DV,
303 AIC7XXX_CONFIGED_DV
304};
305
306/*
307 * There should be a specific return value for this in scsi.h, but
308 * it seems that most drivers ignore it.
309 */
310#define DID_UNDERFLOW DID_ERROR
311
312void
313ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
314{
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);
320}
321
322/*
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.
326 */
327
328/*
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
331 * SCSI bus timeouts.
332 */
333static uint32_t aic7xxx_no_reset;
334
335/*
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.
351 */
352static uint32_t aic7xxx_reverse_scan;
353
354/*
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
358 */
359static uint32_t aic7xxx_extended;
360
361/*
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
370 */
371static uint32_t aic7xxx_pci_parity = ~0;
372
373/*
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 :)
382 */
383#ifndef CONFIG_AIC7XXX_PROBE_EISA_VL
384uint32_t aic7xxx_probe_eisa_vl;
385#else
386uint32_t aic7xxx_probe_eisa_vl = ~0;
387#endif
388
389/*
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.
394 */
395uint32_t aic7xxx_allow_memio = ~0;
396
397/*
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().
401 */
402int aic7xxx_detect_complete;
403
404/*
405 * So that we can set how long each device is given as a selection timeout.
406 * The table of values goes like this:
407 * 0 - 256ms
408 * 1 - 128ms
409 * 2 - 64ms
410 * 3 - 32ms
411 * We default to 256ms because some older devices need a longer time
412 * to respond to initial selection.
413 */
414static uint32_t aic7xxx_seltime;
415
416/*
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
422 * transaction.
423 */
424uint32_t aic7xxx_periodic_otag;
425
426/*
427 * Module information and settable options.
428 */
429static char *aic7xxx = NULL;
430
431MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
432MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
433MODULE_LICENSE("Dual BSD/GPL");
434MODULE_VERSION(AIC7XXX_DRIVER_VERSION);
435module_param(aic7xxx, charp, 0444);
436MODULE_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"
452" on every bus\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"
456"\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"
461"\n"
462" options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n"
463);
464
465static void ahc_linux_handle_scsi_status(struct ahc_softc *,
466 struct ahc_linux_device *,
467 struct scb *);
468static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
469 Scsi_Cmnd *cmd);
470static void ahc_linux_filter_inquiry(struct ahc_softc*, struct ahc_devinfo*);
471static void ahc_linux_sem_timeout(u_long arg);
472static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
473static void ahc_linux_release_simq(u_long arg);
474static void ahc_linux_dev_timed_unfreeze(u_long arg);
475static int ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag);
476static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
477static void ahc_linux_size_nseg(void);
478static void ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc);
479static void ahc_linux_start_dv(struct ahc_softc *ahc);
480static void ahc_linux_dv_timeout(struct scsi_cmnd *cmd);
481static int ahc_linux_dv_thread(void *data);
482static void ahc_linux_kill_dv_thread(struct ahc_softc *ahc);
483static void ahc_linux_dv_target(struct ahc_softc *ahc, u_int target);
484static 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);
488static void ahc_linux_dv_fill_cmd(struct ahc_softc *ahc,
489 struct scsi_cmnd *cmd,
490 struct ahc_devinfo *devinfo);
491static 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);
496static void ahc_linux_dv_tur(struct ahc_softc *ahc,
497 struct scsi_cmnd *cmd,
498 struct ahc_devinfo *devinfo);
499static 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);
503static 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);
507static 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);
511static 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);
515static int ahc_linux_fallback(struct ahc_softc *ahc,
516 struct ahc_devinfo *devinfo);
517static void ahc_linux_dv_complete(Scsi_Cmnd *cmd);
518static void ahc_linux_generate_dv_pattern(struct ahc_linux_target *targ);
519static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
520 struct ahc_devinfo *devinfo);
521static u_int ahc_linux_user_dv_setting(struct ahc_softc *ahc);
522static void ahc_linux_device_queue_depth(struct ahc_softc *ahc,
523 struct ahc_linux_device *dev);
524static struct ahc_linux_target* ahc_linux_alloc_target(struct ahc_softc*,
525 u_int, u_int);
526static void ahc_linux_free_target(struct ahc_softc*,
527 struct ahc_linux_target*);
528static struct ahc_linux_device* ahc_linux_alloc_device(struct ahc_softc*,
529 struct ahc_linux_target*,
530 u_int);
531static void ahc_linux_free_device(struct ahc_softc*,
532 struct ahc_linux_device*);
533static void ahc_linux_run_device_queue(struct ahc_softc*,
534 struct ahc_linux_device*);
535static void ahc_linux_setup_tag_info_global(char *p);
536static aic_option_callback_t ahc_linux_setup_tag_info;
537static aic_option_callback_t ahc_linux_setup_dv;
538static int aic7xxx_setup(char *s);
539static int ahc_linux_next_unit(void);
540static void ahc_runq_tasklet(unsigned long data);
541static struct ahc_cmd *ahc_linux_run_complete_queue(struct ahc_softc *ahc);
542
543/********************************* Inlines ************************************/
544static __inline void ahc_schedule_runq(struct ahc_softc *ahc);
545static __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);
548static __inline void ahc_schedule_completeq(struct ahc_softc *ahc);
549static __inline void ahc_linux_check_device_queue(struct ahc_softc *ahc,
550 struct ahc_linux_device *dev);
551static __inline struct ahc_linux_device *
552 ahc_linux_next_device_to_run(struct ahc_softc *ahc);
553static __inline void ahc_linux_run_device_queues(struct ahc_softc *ahc);
554static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
555
556static __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);
559
560static __inline void
561ahc_schedule_completeq(struct ahc_softc *ahc)
562{
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);
567 }
568}
569
570/*
571 * Must be called with our lock held.
572 */
573static __inline void
574ahc_schedule_runq(struct ahc_softc *ahc)
575{
576 tasklet_schedule(&ahc->platform_data->runq_tasklet);
577}
578
579static __inline struct ahc_linux_device*
580ahc_linux_get_device(struct ahc_softc *ahc, u_int channel, u_int target,
581 u_int lun, int alloc)
582{
583 struct ahc_linux_target *targ;
584 struct ahc_linux_device *dev;
585 u_int target_offset;
586
587 target_offset = target;
588 if (channel != 0)
589 target_offset += 8;
590 targ = ahc->platform_data->targets[target_offset];
591 if (targ == NULL) {
592 if (alloc != 0) {
593 targ = ahc_linux_alloc_target(ahc, channel, target);
594 if (targ == NULL)
595 return (NULL);
596 } else
597 return (NULL);
598 }
599 dev = targ->devices[lun];
600 if (dev == NULL && alloc != 0)
601 dev = ahc_linux_alloc_device(ahc, targ, lun);
602 return (dev);
603}
604
605#define AHC_LINUX_MAX_RETURNED_ERRORS 4
606static struct ahc_cmd *
607ahc_linux_run_complete_queue(struct ahc_softc *ahc)
608{
609 struct ahc_cmd *acmd;
610 u_long done_flags;
611 int with_errors;
612
613 with_errors = 0;
614 ahc_done_lock(ahc, &done_flags);
615 while ((acmd = TAILQ_FIRST(&ahc->platform_data->completeq)) != NULL) {
616 Scsi_Cmnd *cmd;
617
618 if (with_errors > AHC_LINUX_MAX_RETURNED_ERRORS) {
619 /*
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"
626 */
627 ahc_schedule_completeq(ahc);
628 break;
629 }
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)
636 with_errors++;
637
638 cmd->scsi_done(cmd);
639 }
640 ahc_done_unlock(ahc, &done_flags);
641 return (acmd);
642}
643
644static __inline void
645ahc_linux_check_device_queue(struct ahc_softc *ahc,
646 struct ahc_linux_device *dev)
647{
648 if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) != 0
649 && dev->active == 0) {
650 dev->flags &= ~AHC_DEV_FREEZE_TIL_EMPTY;
651 dev->qfrozen--;
652 }
653
654 if (TAILQ_FIRST(&dev->busyq) == NULL
655 || dev->openings == 0 || dev->qfrozen != 0)
656 return;
657
658 ahc_linux_run_device_queue(ahc, dev);
659}
660
661static __inline struct ahc_linux_device *
662ahc_linux_next_device_to_run(struct ahc_softc *ahc)
663{
664
665 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0
666 || (ahc->platform_data->qfrozen != 0
667 && AHC_DV_SIMQ_FROZEN(ahc) == 0))
668 return (NULL);
669 return (TAILQ_FIRST(&ahc->platform_data->device_runq));
670}
671
672static __inline void
673ahc_linux_run_device_queues(struct ahc_softc *ahc)
674{
675 struct ahc_linux_device *dev;
676
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);
681 }
682}
683
684static __inline void
685ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
686{
687 Scsi_Cmnd *cmd;
688
689 cmd = scb->io_ctx;
690 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
691 if (cmd->use_sg != 0) {
692 struct scatterlist *sg;
693
694 sg = (struct scatterlist *)cmd->request_buffer;
695 pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg,
696 scsi_to_pci_dma_dir(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 scsi_to_pci_dma_dir(cmd->sc_data_direction));
702 }
703}
704
705static __inline int
706ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
707 struct ahc_dma_seg *sg, dma_addr_t addr, bus_size_t len)
708{
709 int consumed;
710
711 if ((scb->sg_count + 1) > AHC_NSEG)
712 panic("Too few segs for dma mapping. "
713 "Increase AHC_NSEG\n");
714
715 consumed = 1;
716 sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
717 scb->platform_data->xfer_len += len;
718
719 if (sizeof(dma_addr_t) > 4
720 && (ahc->flags & AHC_39BIT_ADDRESSING) != 0)
721 len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK;
722
723 sg->len = ahc_htole32(len);
724 return (consumed);
725}
726
727/************************ Host template entry points *************************/
728static int ahc_linux_detect(Scsi_Host_Template *);
729static int ahc_linux_queue(Scsi_Cmnd *, void (*)(Scsi_Cmnd *));
730static const char *ahc_linux_info(struct Scsi_Host *);
731#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
732static int ahc_linux_slave_alloc(Scsi_Device *);
733static int ahc_linux_slave_configure(Scsi_Device *);
734static void ahc_linux_slave_destroy(Scsi_Device *);
735#if defined(__i386__)
736static int ahc_linux_biosparam(struct scsi_device*,
737 struct block_device*,
738 sector_t, int[]);
739#endif
740#else
741static int ahc_linux_release(struct Scsi_Host *);
742static void ahc_linux_select_queue_depth(struct Scsi_Host *host,
743 Scsi_Device *scsi_devs);
744#if defined(__i386__)
745static int ahc_linux_biosparam(Disk *, kdev_t, int[]);
746#endif
747#endif
748static int ahc_linux_bus_reset(Scsi_Cmnd *);
749static int ahc_linux_dev_reset(Scsi_Cmnd *);
750static int ahc_linux_abort(Scsi_Cmnd *);
751
752/*
753 * Calculate a safe value for AHC_NSEG (as expressed through ahc_linux_nseg).
754 *
755 * In pre-2.5.X...
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.
768 */
769static void
770ahc_linux_size_nseg(void)
771{
772#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
773 u_int cur_size;
774 u_int best_size;
775
776 /*
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.
781 */
782 best_size = 0;
783 for (cur_size = 1024; cur_size <= PAGE_SIZE; cur_size *= 2) {
784 u_int nseg;
785
786 nseg = cur_size / sizeof(struct scatterlist);
787 if (nseg < AHC_LINUX_MIN_NSEG)
788 continue;
789
790 if (best_size == 0) {
791 best_size = cur_size;
792 ahc_linux_nseg = nseg;
793 } else {
794 u_int best_rem;
795 u_int cur_rem;
796
797 /*
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.
801 */
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;
807 }
808 }
809 }
810#endif
811}
812
813/*
814 * Try to detect an Adaptec 7XXX controller.
815 */
816static int
817ahc_linux_detect(Scsi_Host_Template *template)
818{
819 struct ahc_softc *ahc;
820 int found = 0;
821
822#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
823 /*
824 * It is a bug that the upper layer takes
825 * this lock just prior to calling us.
826 */
827 spin_unlock_irq(&io_request_lock);
828#endif
829
830 /*
831 * Sanity checking of Linux SCSI data structures so
832 * that some of our hacks^H^H^H^H^Hassumptions aren't
833 * violated.
834 */
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");
839 return (0);
840 }
841 ahc_linux_size_nseg();
842 /*
843 * If we've been passed any parameters, process them now.
844 */
845 if (aic7xxx)
846 aic7xxx_setup(aic7xxx);
847
848 template->proc_name = "aic7xxx";
849
850 /*
851 * Initialize our softc list lock prior to
852 * probing for any adapters.
853 */
854 ahc_list_lockinit();
855
856 found = ahc_linux_pci_init();
857 if (!ahc_linux_eisa_init())
858 found++;
859
860 /*
861 * Register with the SCSI layer all
862 * controllers we've found.
863 */
864 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
865
866 if (ahc_linux_register_host(ahc, template) == 0)
867 found++;
868 }
869
870#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
871 spin_lock_irq(&io_request_lock);
872#endif
873 aic7xxx_detect_complete++;
874
875 return (found);
876}
877
878#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
879/*
880 * Free the passed in Scsi_Host memory structures prior to unloading the
881 * module.
882 */
883int
884ahc_linux_release(struct Scsi_Host * host)
885{
886 struct ahc_softc *ahc;
887 u_long l;
888
889 ahc_list_lock(&l);
890 if (host != NULL) {
891
892 /*
893 * We should be able to just perform
894 * the free directly, but check our
895 * list for extra sanity.
896 */
897 ahc = ahc_find_softc(*(struct ahc_softc **)host->hostdata);
898 if (ahc != NULL) {
899 u_long s;
900
901 ahc_lock(ahc, &s);
902 ahc_intr_enable(ahc, FALSE);
903 ahc_unlock(ahc, &s);
904 ahc_free(ahc);
905 }
906 }
907 ahc_list_unlock(&l);
908 return (0);
909}
910#endif
911
912/*
913 * Return a string describing the driver.
914 */
915static const char *
916ahc_linux_info(struct Scsi_Host *host)
917{
918 static char buffer[512];
919 char ahc_info[256];
920 char *bp;
921 struct ahc_softc *ahc;
922
923 bp = &buffer[0];
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);
928 strcat(bp, "\n");
929 strcat(bp, " <");
930 strcat(bp, ahc->description);
931 strcat(bp, ">\n");
932 strcat(bp, " ");
933 ahc_controller_info(ahc, ahc_info);
934 strcat(bp, ahc_info);
935 strcat(bp, "\n");
936
937 return (bp);
938}
939
940/*
941 * Queue an SCB to the controller.
942 */
943static int
944ahc_linux_queue(Scsi_Cmnd * cmd, void (*scsi_done) (Scsi_Cmnd *))
945{
946 struct ahc_softc *ahc;
947 struct ahc_linux_device *dev;
948 u_long flags;
949
950 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
951
952 /*
953 * Save the callback on completion function.
954 */
955 cmd->scsi_done = scsi_done;
956
957 ahc_midlayer_entrypoint_lock(ahc, &flags);
958
959 /*
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
963 * perform DV.
964 */
965 if (ahc->platform_data->qfrozen != 0
966 && AHC_DV_CMD(cmd) == 0) {
967
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);
972 return (0);
973 }
974 dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
975 cmd->device->lun, /*alloc*/TRUE);
976 if (dev == NULL) {
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",
982 ahc_name(ahc));
983 return (0);
984 }
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);
991 }
992 ahc_midlayer_entrypoint_unlock(ahc, &flags);
993 return (0);
994}
995
996#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
997static int
998ahc_linux_slave_alloc(Scsi_Device *device)
999{
1000 struct ahc_softc *ahc;
1001
1002 ahc = *((struct ahc_softc **)device->host->hostdata);
1003 if (bootverbose)
1004 printf("%s: Slave Alloc %d\n", ahc_name(ahc), device->id);
1005 return (0);
1006}
1007
1008static int
1009ahc_linux_slave_configure(Scsi_Device *device)
1010{
1011 struct ahc_softc *ahc;
1012 struct ahc_linux_device *dev;
1013 u_long flags;
1014
1015 ahc = *((struct ahc_softc **)device->host->hostdata);
1016 if (bootverbose)
1017 printf("%s: Slave Configure %d\n", ahc_name(ahc), device->id);
1018 ahc_midlayer_entrypoint_lock(ahc, &flags);
1019 /*
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.
1023 */
1024 dev = ahc_linux_get_device(ahc, device->channel,
1025 device->id, device->lun,
1026 /*alloc*/TRUE);
1027 if (dev != NULL) {
1028 dev->flags &= ~AHC_DEV_UNCONFIGURED;
1029 dev->scsi_device = device;
1030 ahc_linux_device_queue_depth(ahc, dev);
1031 }
1032 ahc_midlayer_entrypoint_unlock(ahc, &flags);
1033 return (0);
1034}
1035
1036static void
1037ahc_linux_slave_destroy(Scsi_Device *device)
1038{
1039 struct ahc_softc *ahc;
1040 struct ahc_linux_device *dev;
1041 u_long flags;
1042
1043 ahc = *((struct ahc_softc **)device->host->hostdata);
1044 if (bootverbose)
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,
1049 /*alloc*/FALSE);
1050 /*
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.
1056 */
1057 if (dev != NULL
1058 && (dev->flags & AHC_DEV_SLAVE_CONFIGURED) != 0) {
1059 dev->flags |= AHC_DEV_UNCONFIGURED;
1060 if (TAILQ_EMPTY(&dev->busyq)
1061 && dev->active == 0
1062 && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0)
1063 ahc_linux_free_device(ahc, dev);
1064 }
1065 ahc_midlayer_entrypoint_unlock(ahc, &flags);
1066}
1067#else
1068/*
1069 * Sets the queue depth for each SCSI device hanging
1070 * off the input host adapter.
1071 */
1072static void
1073ahc_linux_select_queue_depth(struct Scsi_Host *host, Scsi_Device *scsi_devs)
1074{
1075 Scsi_Device *device;
1076 Scsi_Device *ldev;
1077 struct ahc_softc *ahc;
1078 u_long flags;
1079
1080 ahc = *((struct ahc_softc **)host->hostdata);
1081 ahc_lock(ahc, &flags);
1082 for (device = scsi_devs; device != NULL; device = device->next) {
1083
1084 /*
1085 * Watch out for duplicate devices. This works around
1086 * some quirks in how the SCSI scanning code does its
1087 * device management.
1088 */
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)
1094 break;
1095 }
1096 /* Skip duplicate. */
1097 if (ldev != device)
1098 continue;
1099
1100 if (device->host == host) {
1101 struct ahc_linux_device *dev;
1102
1103 /*
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.
1107 */
1108 dev = ahc_linux_get_device(ahc, device->channel,
1109 device->id, device->lun,
1110 /*alloc*/TRUE);
1111 if (dev != NULL) {
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
1116 + dev->active;
1117 if ((dev->flags & (AHC_DEV_Q_BASIC
1118 | AHC_DEV_Q_TAGGED)) == 0) {
1119 /*
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.
1125 */
1126 device->queue_depth = 2;
1127 }
1128 }
1129 }
1130 }
1131 ahc_unlock(ahc, &flags);
1132}
1133#endif
1134
1135#if defined(__i386__)
1136/*
1137 * Return the disk geometry for the given SCSI device.
1138 */
1139static int
1140#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1141ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1142 sector_t capacity, int geom[])
1143{
1144 uint8_t *bh;
1145#else
1146ahc_linux_biosparam(Disk *disk, kdev_t dev, int geom[])
1147{
1148 struct scsi_device *sdev = disk->device;
1149 u_long capacity = disk->capacity;
1150 struct buffer_head *bh;
1151#endif
1152 int heads;
1153 int sectors;
1154 int cylinders;
1155 int ret;
1156 int extended;
1157 struct ahc_softc *ahc;
1158 u_int channel;
1159
1160 ahc = *((struct ahc_softc **)sdev->host->hostdata);
1161 channel = sdev->channel;
1162
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));
1167#else
1168 bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, 1024);
1169#endif
1170
1171 if (bh) {
1172 ret = scsi_partsize(bh, capacity,
1173 &geom[2], &geom[0], &geom[1]);
1174#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1175 kfree(bh);
1176#else
1177 brelse(bh);
1178#endif
1179 if (ret != -1)
1180 return (ret);
1181 }
1182 heads = 64;
1183 sectors = 32;
1184 cylinders = aic_sector_div(capacity, heads, sectors);
1185
1186 if (aic7xxx_extended != 0)
1187 extended = 1;
1188 else if (channel == 0)
1189 extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
1190 else
1191 extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
1192 if (extended && cylinders >= 1024) {
1193 heads = 255;
1194 sectors = 63;
1195 cylinders = aic_sector_div(capacity, heads, sectors);
1196 }
1197 geom[0] = heads;
1198 geom[1] = sectors;
1199 geom[2] = cylinders;
1200 return (0);
1201}
1202#endif
1203
1204/*
1205 * Abort the current SCSI command(s).
1206 */
1207static int
1208ahc_linux_abort(Scsi_Cmnd *cmd)
1209{
1210 int error;
1211
1212 error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
1213 if (error != 0)
1214 printf("aic7xxx_abort returns 0x%x\n", error);
1215 return (error);
1216}
1217
1218/*
1219 * Attempt to send a target reset message to the device that timed out.
1220 */
1221static int
1222ahc_linux_dev_reset(Scsi_Cmnd *cmd)
1223{
1224 int error;
1225
1226 error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
1227 if (error != 0)
1228 printf("aic7xxx_dev_reset returns 0x%x\n", error);
1229 return (error);
1230}
1231
1232/*
1233 * Reset the SCSI bus.
1234 */
1235static int
1236ahc_linux_bus_reset(Scsi_Cmnd *cmd)
1237{
1238 struct ahc_softc *ahc;
1239 u_long s;
1240 int found;
1241
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);
1248
1249 if (bootverbose)
1250 printf("%s: SCSI bus reset delivered. "
1251 "%d SCBs aborted.\n", ahc_name(ahc), found);
1252
1253 return SUCCESS;
1254}
1255
1256Scsi_Host_Template aic7xxx_driver_template = {
1257 .module = THIS_MODULE,
1258 .name = "aic7xxx",
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,
1267#endif
1268 .can_queue = AHC_MAX_QUEUE,
1269 .this_id = -1,
1270 .cmd_per_lun = 2,
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,
1275};
1276
1277/**************************** Tasklet Handler *********************************/
1278
1279/*
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.
1284 */
1285static void
1286ahc_runq_tasklet(unsigned long data)
1287{
1288 struct ahc_softc* ahc;
1289 struct ahc_linux_device *dev;
1290 u_long flags;
1291
1292 ahc = (struct ahc_softc *)data;
1293 ahc_lock(ahc, &flags);
1294 while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) {
1295
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);
1302 }
1303 ahc_unlock(ahc, &flags);
1304}
1305
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))
1311
1312/******************************** Bus DMA *************************************/
1313int
1314ahc_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)
1320{
1321 bus_dma_tag_t dmat;
1322
1323 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
1324 if (dmat == NULL)
1325 return (ENOMEM);
1326
1327 /*
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
1332 * more here.
1333 */
1334 dmat->alignment = alignment;
1335 dmat->boundary = boundary;
1336 dmat->maxsize = maxsize;
1337 *ret_tag = dmat;
1338 return (0);
1339}
1340
1341void
1342ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
1343{
1344 free(dmat, M_DEVBUF);
1345}
1346
1347int
1348ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
1349 int flags, bus_dmamap_t *mapp)
1350{
1351 bus_dmamap_t map;
1352
1353 map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT);
1354 if (map == NULL)
1355 return (ENOMEM);
1356 /*
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.
1362 */
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");
1366 kfree(map);
1367 return (ENODEV);
1368 }
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");
1375 kfree(map);
1376 return (ENODEV);
1377 }
1378 if (*vaddr == NULL)
1379 return (ENOMEM);
1380 *mapp = map;
1381 return(0);
1382}
1383
1384void
1385ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
1386 void* vaddr, bus_dmamap_t map)
1387{
1388 pci_free_consistent(ahc->dev_softc, dmat->maxsize,
1389 vaddr, map->bus_addr);
1390}
1391
1392int
1393ahc_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)
1396{
1397 /*
1398 * Assume for now that this will only be used during
1399 * initialization and not for per-transaction buffer mapping.
1400 */
1401 bus_dma_segment_t stack_sg;
1402
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);
1406 return (0);
1407}
1408
1409void
1410ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
1411{
1412 /*
1413 * The map may is NULL in our < 2.3.X implementation.
1414 * Now it's 2.6.5, but just in case...
1415 */
1416 BUG_ON(map == NULL);
1417 free(map, M_DEVBUF);
1418}
1419
1420int
1421ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
1422{
1423 /* Nothing to do */
1424 return (0);
1425}
1426
1427/********************* Platform Dependent Functions ***************************/
1428/*
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
1433 */
1434int
1435ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1436{
1437 int value;
1438 int rvalue;
1439 int lvalue;
1440
1441 /*
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.
1447 */
1448 value = (lahc->flags & AHC_BIOS_ENABLED)
1449 - (rahc->flags & AHC_BIOS_ENABLED);
1450 if (value != 0)
1451 /* Controllers with BIOS enabled have a *higher* priority */
1452 return (value);
1453
1454 /*
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.
1458 */
1459 rvalue = (rahc->chip & AHC_BUS_MASK);
1460 if (rvalue == AHC_VL)
1461 rvalue = AHC_EISA;
1462 lvalue = (lahc->chip & AHC_BUS_MASK);
1463 if (lvalue == AHC_VL)
1464 lvalue = AHC_EISA;
1465 value = rvalue - lvalue;
1466 if (value != 0)
1467 return (value);
1468
1469 /* Still equal. Sort by BIOS address, ioport, or bus/slot/func. */
1470 switch (rvalue) {
1471#ifdef CONFIG_PCI
1472 case AHC_PCI:
1473 {
1474 char primary_channel;
1475
1476 if (aic7xxx_reverse_scan != 0)
1477 value = ahc_get_pci_bus(lahc->dev_softc)
1478 - ahc_get_pci_bus(rahc->dev_softc);
1479 else
1480 value = ahc_get_pci_bus(rahc->dev_softc)
1481 - ahc_get_pci_bus(lahc->dev_softc);
1482 if (value != 0)
1483 break;
1484 if (aic7xxx_reverse_scan != 0)
1485 value = ahc_get_pci_slot(lahc->dev_softc)
1486 - ahc_get_pci_slot(rahc->dev_softc);
1487 else
1488 value = ahc_get_pci_slot(rahc->dev_softc)
1489 - ahc_get_pci_slot(lahc->dev_softc);
1490 if (value != 0)
1491 break;
1492 /*
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.
1497 */
1498 primary_channel = (lahc->flags & AHC_PRIMARY_CHANNEL) + 'A';
1499 value = -1;
1500 if (lahc->channel == primary_channel)
1501 value = 1;
1502 break;
1503 }
1504#endif
1505 case AHC_EISA:
1506 if ((rahc->flags & AHC_BIOS_ENABLED) != 0) {
1507 value = rahc->platform_data->bios_address
1508 - lahc->platform_data->bios_address;
1509 } else {
1510 value = rahc->bsh.ioport
1511 - lahc->bsh.ioport;
1512 }
1513 break;
1514 default:
1515 panic("ahc_softc_sort: invalid bus type");
1516 }
1517 return (value);
1518}
1519
1520static void
1521ahc_linux_setup_tag_info_global(char *p)
1522{
1523 int tags, i, j;
1524
1525 tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
1526 printf("Setting Global Tags= %d\n", tags);
1527
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;
1531 }
1532 }
1533}
1534
1535static void
1536ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
1537{
1538
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;
1543 if (bootverbose)
1544 printf("tag_info[%d:%d] = %d\n", instance, targ, value);
1545 }
1546}
1547
1548static void
1549ahc_linux_setup_dv(u_long arg, int instance, int targ, int32_t value)
1550{
1551
1552 if ((instance >= 0)
1553 && (instance < NUM_ELEMENTS(aic7xxx_dv_settings))) {
1554 aic7xxx_dv_settings[instance] = value;
1555 if (bootverbose)
1556 printf("dv[%d] = %d\n", instance, value);
1557 }
1558}
1559
1560/*
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
1564 */
1565static int
1566aic7xxx_setup(char *s)
1567{
1568 int i, n;
1569 char *p;
1570 char *end;
1571
1572 static struct {
1573 const char *name;
1574 uint32_t *flag;
1575 } options[] = {
1576 { "extended", &aic7xxx_extended },
1577 { "no_reset", &aic7xxx_no_reset },
1578 { "verbose", &aic7xxx_verbose },
1579 { "allow_memio", &aic7xxx_allow_memio},
1580#ifdef AHC_DEBUG
1581 { "debug", &ahc_debug },
1582#endif
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 },
1591 { "dv", NULL }
1592 };
1593
1594 end = strchr(s, '\0');
1595
1596 /*
1597 * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
1598 * will never be 0 in this case.
1599 */
1600 n = 0;
1601
1602 while ((p = strsep(&s, ",.")) != NULL) {
1603 if (*p == '\0')
1604 continue;
1605 for (i = 0; i < NUM_ELEMENTS(options); i++) {
1606
1607 n = strlen(options[i].name);
1608 if (strncmp(options[i].name, p, n) == 0)
1609 break;
1610 }
1611 if (i == NUM_ELEMENTS(options))
1612 continue;
1613
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;
1626 } else {
1627 *(options[i].flag) ^= 0xFFFFFFFF;
1628 }
1629 }
1630 return 1;
1631}
1632
1633__setup("aic7xxx=", aic7xxx_setup);
1634
1635uint32_t aic7xxx_verbose;
1636
1637int
1638ahc_linux_register_host(struct ahc_softc *ahc, Scsi_Host_Template *template)
1639{
1640 char buf[80];
1641 struct Scsi_Host *host;
1642 char *new_name;
1643 u_long s;
1644 u_int targ_offset;
1645
1646 template->name = ahc->description;
1647 host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
1648 if (host == NULL)
1649 return (ENOMEM);
1650
1651 *((struct ahc_softc **)host->hostdata) = ahc;
1652 ahc_lock(ahc, &s);
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;
1657#endif
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);
1674 }
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);
1678#endif
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);
1682 ahc_lock(ahc, &s);
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);
1687 }
1688 /*
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.
1697 */
1698 for (targ_offset = 0;
1699 targ_offset < host->max_id * (host->max_channel + 1);
1700 targ_offset++) {
1701 u_int channel;
1702 u_int target;
1703
1704 channel = 0;
1705 target = targ_offset;
1706 if (target > 7
1707 && (ahc->features & AHC_TWIN) != 0) {
1708 channel = 1;
1709 target &= 0x7;
1710 }
1711 /*
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.
1720 */
1721 if ((channel == 0 && target == ahc->our_id)
1722 || (channel == 1 && target == ahc->our_id_b))
1723 continue;
1724
1725 ahc_linux_alloc_target(ahc, channel, target);
1726 }
1727 ahc_intr_enable(ahc, TRUE);
1728 ahc_linux_start_dv(ahc);
1729 ahc_unlock(ahc, &s);
1730
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);
1734#endif
1735 return (0);
1736}
1737
1738uint64_t
1739ahc_linux_get_memsize(void)
1740{
1741 struct sysinfo si;
1742
1743 si_meminfo(&si);
1744 return ((uint64_t)si.totalram << PAGE_SHIFT);
1745}
1746
1747/*
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"
1751 * scenario.
1752 */
1753static int
1754ahc_linux_next_unit(void)
1755{
1756 struct ahc_softc *ahc;
1757 int unit;
1758
1759 unit = 0;
1760retry:
1761 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
1762 if (ahc->unit == unit) {
1763 unit++;
1764 goto retry;
1765 }
1766 }
1767 return (unit);
1768}
1769
1770/*
1771 * Place the SCSI bus into a known state by either resetting it,
1772 * or forcing transfer negotiations on the next command to any
1773 * target.
1774 */
1775void
1776ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
1777{
1778 int i;
1779 int numtarg;
1780
1781 i = 0;
1782 numtarg = 0;
1783
1784 if (aic7xxx_no_reset != 0)
1785 ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
1786
1787 if ((ahc->flags & AHC_RESET_BUS_A) != 0)
1788 ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
1789 else
1790 numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1791
1792 if ((ahc->features & AHC_TWIN) != 0) {
1793
1794 if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
1795 ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
1796 } else {
1797 if (numtarg == 0)
1798 i = 8;
1799 numtarg += 8;
1800 }
1801 }
1802
1803 /*
1804 * Force negotiation to async for all targets that
1805 * will not see an initial bus reset.
1806 */
1807 for (; i < numtarg; i++) {
1808 struct ahc_devinfo devinfo;
1809 struct ahc_initiator_tinfo *tinfo;
1810 struct ahc_tmode_tstate *tstate;
1811 u_int our_id;
1812 u_int target_id;
1813 char channel;
1814
1815 channel = 'A';
1816 our_id = ahc->our_id;
1817 target_id = i;
1818 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
1819 channel = 'B';
1820 our_id = ahc->our_id_b;
1821 target_id = i % 8;
1822 }
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);
1829 }
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);
1840 }
1841}
1842
1843int
1844ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1845{
1846
1847 ahc->platform_data =
1848 malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
1849 if (ahc->platform_data == NULL)
1850 return (ENOMEM);
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;
1856 ahc_lockinit(ahc);
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;
1871
1872 return (0);
1873}
1874
1875void
1876ahc_platform_free(struct ahc_softc *ahc)
1877{
1878 struct ahc_linux_target *targ;
1879 struct ahc_linux_device *dev;
1880 int i, j;
1881
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);
1889#endif
1890 scsi_host_put(ahc->platform_data->host);
1891 }
1892
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];
1896 if (targ != NULL) {
1897 /* Keep target around through the loop. */
1898 targ->refcount++;
1899 for (j = 0; j < AHC_NUM_LUNS; j++) {
1900
1901 if (targ->devices[j] == NULL)
1902 continue;
1903 dev = targ->devices[j];
1904 ahc_linux_free_device(ahc, dev);
1905 }
1906 /*
1907 * Forcibly free the target now that
1908 * all devices are gone.
1909 */
1910 ahc_linux_free_target(ahc, targ);
1911 }
1912 }
1913
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,
1923 0x1000);
1924 }
1925#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1926 /*
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.
1931 */
1932 if (ahc->dev_softc != NULL)
1933 ahc->dev_softc->driver = NULL;
1934#endif
1935 free(ahc->platform_data, M_DEVBUF);
1936 }
1937}
1938
1939void
1940ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
1941{
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);
1946}
1947
1948void
1949ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1950 ahc_queue_alg alg)
1951{
1952 struct ahc_linux_device *dev;
1953 int was_queuing;
1954 int now_queuing;
1955
1956 dev = ahc_linux_get_device(ahc, devinfo->channel - 'A',
1957 devinfo->target,
1958 devinfo->lun, /*alloc*/FALSE);
1959 if (dev == NULL)
1960 return;
1961 was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
1962 switch (alg) {
1963 default:
1964 case AHC_QUEUE_NONE:
1965 now_queuing = 0;
1966 break;
1967 case AHC_QUEUE_BASIC:
1968 now_queuing = AHC_DEV_Q_BASIC;
1969 break;
1970 case AHC_QUEUE_TAGGED:
1971 now_queuing = AHC_DEV_Q_TAGGED;
1972 break;
1973 }
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;
1978 dev->qfrozen++;
1979 }
1980
1981 dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
1982 if (now_queuing) {
1983 u_int usertags;
1984
1985 usertags = ahc_linux_user_tagdepth(ahc, devinfo);
1986 if (!was_queuing) {
1987 /*
1988 * Start out agressively and allow our
1989 * dynamic queue depth algorithm to take
1990 * care of the rest.
1991 */
1992 dev->maxtags = usertags;
1993 dev->openings = dev->maxtags - dev->active;
1994 }
1995 if (dev->maxtags == 0) {
1996 /*
1997 * Queueing is disabled by the user.
1998 */
1999 dev->openings = 1;
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;
2004 } else
2005 dev->flags |= AHC_DEV_Q_BASIC;
2006 } else {
2007 /* We can only have one opening. */
2008 dev->maxtags = 0;
2009 dev->openings = 1 - dev->active;
2010 }
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,
2016 MSG_SIMPLE_TASK,
2017 dev->openings + dev->active);
2018 break;
2019 case AHC_DEV_Q_TAGGED:
2020 scsi_adjust_queue_depth(dev->scsi_device,
2021 MSG_ORDERED_TASK,
2022 dev->openings + dev->active);
2023 break;
2024 default:
2025 /*
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.
2030 */
2031 scsi_adjust_queue_depth(dev->scsi_device,
2032 /*NON-TAGGED*/0,
2033 /*queue depth*/2);
2034 break;
2035 }
2036 }
2037#endif
2038}
2039
2040int
2041ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
2042 int lun, u_int tag, role_t role, uint32_t status)
2043{
2044 int chan;
2045 int maxchan;
2046 int targ;
2047 int maxtarg;
2048 int clun;
2049 int maxlun;
2050 int count;
2051
2052 if (tag != SCB_LIST_NULL)
2053 return (0);
2054
2055 chan = 0;
2056 if (channel != ALL_CHANNELS) {
2057 chan = channel - 'A';
2058 maxchan = chan + 1;
2059 } else {
2060 maxchan = (ahc->features & AHC_TWIN) ? 2 : 1;
2061 }
2062 targ = 0;
2063 if (target != CAM_TARGET_WILDCARD) {
2064 targ = target;
2065 maxtarg = targ + 1;
2066 } else {
2067 maxtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
2068 }
2069 clun = 0;
2070 if (lun != CAM_LUN_WILDCARD) {
2071 clun = lun;
2072 maxlun = clun + 1;
2073 } else {
2074 maxlun = AHC_NUM_LUNS;
2075 }
2076
2077 count = 0;
2078 for (; chan < maxchan; chan++) {
2079
2080 for (; targ < maxtarg; targ++) {
2081
2082 for (; clun < maxlun; clun++) {
2083 struct ahc_linux_device *dev;
2084 struct ahc_busyq *busyq;
2085 struct ahc_cmd *acmd;
2086
2087 dev = ahc_linux_get_device(ahc, chan,
2088 targ, clun,
2089 /*alloc*/FALSE);
2090 if (dev == NULL)
2091 continue;
2092
2093 busyq = &dev->busyq;
2094 while ((acmd = TAILQ_FIRST(busyq)) != NULL) {
2095 Scsi_Cmnd *cmd;
2096
2097 cmd = &acmd_scsi_cmd(acmd);
2098 TAILQ_REMOVE(busyq, acmd,
2099 acmd_links.tqe);
2100 count++;
2101 cmd->result = status << 16;
2102 ahc_linux_queue_cmd_complete(ahc, cmd);
2103 }
2104 }
2105 }
2106 }
2107
2108 return (count);
2109}
2110
2111static void
2112ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc)
2113{
2114 u_long flags;
2115
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);
2121}
2122
2123static void
2124ahc_linux_start_dv(struct ahc_softc *ahc)
2125{
2126
2127 /*
2128 * Freeze the simq and signal ahc_linux_queue to not let any
2129 * more commands through.
2130 */
2131 if ((ahc->platform_data->flags & AHC_DV_ACTIVE) == 0) {
2132#ifdef AHC_DEBUG
2133 if (ahc_debug & AHC_SHOW_DV)
2134 printf("%s: Waking DV thread\n", ahc_name(ahc));
2135#endif
2136
2137 ahc->platform_data->flags |= AHC_DV_ACTIVE;
2138 ahc_linux_freeze_simq(ahc);
2139
2140 /* Wake up the DV kthread */
2141 up(&ahc->platform_data->dv_sem);
2142 }
2143}
2144
2145static void
2146ahc_linux_kill_dv_thread(struct ahc_softc *ahc)
2147{
2148 u_long s;
2149
2150 ahc_lock(ahc, &s);
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);
2155
2156 /*
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...
2166 */
2167 down(&ahc->platform_data->eh_sem);
2168
2169 /*
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
2179 * is dead.
2180 */
2181 ahc->platform_data->dv_pid = 0;
2182 } else {
2183 ahc_unlock(ahc, &s);
2184 }
2185}
2186
2187static int
2188ahc_linux_dv_thread(void *data)
2189{
2190 struct ahc_softc *ahc;
2191 int target;
2192 u_long s;
2193
2194 ahc = (struct ahc_softc *)data;
2195
2196#ifdef AHC_DEBUG
2197 if (ahc_debug & AHC_SHOW_DV)
2198 printf("Launching DV Thread\n");
2199#endif
2200
2201 /*
2202 * Complete thread creation.
2203 */
2204 lock_kernel();
2205#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2206 /*
2207 * Don't care about any signals.
2208 */
2209 siginitsetinv(&current->blocked, 0);
2210
2211 daemonize();
2212 sprintf(current->comm, "ahc_dv_%d", ahc->unit);
2213#else
2214 daemonize("ahc_dv_%d", ahc->unit);
2215 current->flags |= PF_FREEZE;
2216#endif
2217 unlock_kernel();
2218
2219 while (1) {
2220 /*
2221 * Use down_interruptible() rather than down() to
2222 * avoid inclusion in the load average.
2223 */
2224 down_interruptible(&ahc->platform_data->dv_sem);
2225
2226 /* Check to see if we've been signaled to exit */
2227 ahc_lock(ahc, &s);
2228 if ((ahc->platform_data->flags & AHC_DV_SHUTDOWN) != 0) {
2229 ahc_unlock(ahc, &s);
2230 break;
2231 }
2232 ahc_unlock(ahc, &s);
2233
2234#ifdef AHC_DEBUG
2235 if (ahc_debug & AHC_SHOW_DV)
2236 printf("%s: Beginning Domain Validation\n",
2237 ahc_name(ahc));
2238#endif
2239
2240 /*
2241 * Wait for any pending commands to drain before proceeding.
2242 */
2243 ahc_lock(ahc, &s);
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);
2248 ahc_lock(ahc, &s);
2249 }
2250
2251 /*
2252 * Wait for the SIMQ to be released so that DV is the
2253 * only reason the queue is frozen.
2254 */
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);
2259 ahc_lock(ahc, &s);
2260 }
2261 ahc_unlock(ahc, &s);
2262
2263 for (target = 0; target < AHC_NUM_TARGETS; target++)
2264 ahc_linux_dv_target(ahc, target);
2265
2266 ahc_lock(ahc, &s);
2267 ahc->platform_data->flags &= ~AHC_DV_ACTIVE;
2268 ahc_unlock(ahc, &s);
2269
2270 /*
2271 * Release the SIMQ so that normal commands are
2272 * allowed to continue on the bus.
2273 */
2274 ahc_linux_release_simq((u_long)ahc);
2275 }
2276 up(&ahc->platform_data->eh_sem);
2277 return (0);
2278}
2279
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)
2283
2284#define AHC_SET_DV_STATE(ahc, targ, newstate) \
2285 ahc_set_dv_state(ahc, targ, newstate, __LINE__)
2286
2287static __inline void
2288ahc_set_dv_state(struct ahc_softc *ahc, struct ahc_linux_target *targ,
2289 ahc_dv_state newstate, u_int line)
2290{
2291 ahc_dv_state oldstate;
2292
2293 oldstate = targ->dv_state;
2294#ifdef AHC_DEBUG
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);
2298#endif
2299
2300 if (oldstate == newstate)
2301 targ->dv_state_retry++;
2302 else
2303 targ->dv_state_retry = 0;
2304 targ->dv_state = newstate;
2305}
2306
2307static void
2308ahc_linux_dv_target(struct ahc_softc *ahc, u_int target_offset)
2309{
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;
2315 uint8_t *buffer;
2316 u_long s;
2317 u_int timeout;
2318 int echo_size;
2319
2320 sense = NULL;
2321 buffer = NULL;
2322 echo_size = 0;
2323 ahc_lock(ahc, &s);
2324 targ = ahc->platform_data->targets[target_offset];
2325 if (targ == NULL || (targ->flags & AHC_DV_REQUIRED) == 0) {
2326 ahc_unlock(ahc, &s);
2327 return;
2328 }
2329 ahc_compile_devinfo(&devinfo,
2330 targ->channel == 0 ? ahc->our_id : ahc->our_id_b,
2331 targ->target, /*lun*/0, targ->channel + 'A',
2332 ROLE_INITIATOR);
2333#ifdef AHC_DEBUG
2334 if (ahc_debug & AHC_SHOW_DV) {
2335 ahc_print_devinfo(ahc, &devinfo);
2336 printf("Performing DV\n");
2337 }
2338#endif
2339
2340 ahc_unlock(ahc, &s);
2341
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;
2349
2350 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_INQ_SHORT_ASYNC);
2351
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:
2358 /*
2359 * Set things to async narrow to reduce the
2360 * chance that the INQ will fail.
2361 */
2362 ahc_lock(ahc, &s);
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);
2368 timeout = 10 * HZ;
2369 targ->flags &= ~AHC_INQ_VALID;
2370 /* FALLTHROUGH */
2371 case AHC_DV_STATE_INQ_VERIFY:
2372 {
2373 u_int inq_len;
2374
2375 if (targ->dv_state == AHC_DV_STATE_INQ_SHORT_ASYNC)
2376 inq_len = AHC_LINUX_DV_INQ_SHORT_LEN;
2377 else
2378 inq_len = targ->inq_data->additional_length + 5;
2379 ahc_linux_dv_inq(ahc, cmd, &devinfo, targ, inq_len);
2380 break;
2381 }
2382 case AHC_DV_STATE_TUR:
2383 case AHC_DV_STATE_BUSY:
2384 timeout = 5 * HZ;
2385 ahc_linux_dv_tur(ahc, cmd, &devinfo);
2386 break;
2387 case AHC_DV_STATE_REBD:
2388 ahc_linux_dv_rebd(ahc, cmd, &devinfo, targ);
2389 break;
2390 case AHC_DV_STATE_WEB:
2391 ahc_linux_dv_web(ahc, cmd, &devinfo, targ);
2392 break;
2393
2394 case AHC_DV_STATE_REB:
2395 ahc_linux_dv_reb(ahc, cmd, &devinfo, targ);
2396 break;
2397
2398 case AHC_DV_STATE_SU:
2399 ahc_linux_dv_su(ahc, cmd, &devinfo, targ);
2400 timeout = 50 * HZ;
2401 break;
2402
2403 default:
2404 ahc_print_devinfo(ahc, &devinfo);
2405 printf("Unknown DV state %d\n", targ->dv_state);
2406 goto out;
2407 }
2408
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);
2412#ifdef AHC_DEBUG
2413 if ((ahc_debug & AHC_SHOW_MESSAGES) != 0)
2414 /*
2415 * All of the printfs during negotiation
2416 * really slow down the negotiation.
2417 * Add a bit of time just to be safe.
2418 */
2419 timeout += HZ;
2420#endif
2421 scsi_add_timer(cmd, timeout, ahc_linux_dv_timeout);
2422 /*
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.
2427 */
2428#if AHC_SCSI_HAS_HOST_LOCK != 0
2429 ahc_lock(ahc, &s);
2430#else
2431 spin_lock_irqsave(&io_request_lock, s);
2432#endif
2433 ahc_linux_queue(cmd, ahc_linux_dv_complete);
2434#if AHC_SCSI_HAS_HOST_LOCK != 0
2435 ahc_unlock(ahc, &s);
2436#else
2437 spin_unlock_irqrestore(&io_request_lock, s);
2438#endif
2439 down_interruptible(&ahc->platform_data->dv_cmd_sem);
2440 /*
2441 * Wait for the SIMQ to be released so that DV is the
2442 * only reason the queue is frozen.
2443 */
2444 ahc_lock(ahc, &s);
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);
2449 ahc_lock(ahc, &s);
2450 }
2451 ahc_unlock(ahc, &s);
2452
2453 ahc_linux_dv_transition(ahc, cmd, &devinfo, targ);
2454 }
2455
2456out:
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) {
2461 /*
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.
2466 */
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 "
2472 "this driver.\n");
2473 }
2474 }
2475
2476 if (cmd != NULL)
2477 free(cmd, M_DEVBUF);
2478
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;
2482 }
2483
2484 ahc_lock(ahc, &s);
2485 if (targ->dv_buffer != NULL) {
2486 free(targ->dv_buffer, M_DEVBUF);
2487 targ->dv_buffer = NULL;
2488 }
2489 if (targ->dv_buffer1 != NULL) {
2490 free(targ->dv_buffer1, M_DEVBUF);
2491 targ->dv_buffer1 = NULL;
2492 }
2493 targ->flags &= ~AHC_DV_REQUIRED;
2494 if (targ->refcount == 0)
2495 ahc_linux_free_target(ahc, targ);
2496 ahc_unlock(ahc, &s);
2497}
2498
2499static void
2500ahc_linux_dv_transition(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
2501 struct ahc_devinfo *devinfo,
2502 struct ahc_linux_target *targ)
2503{
2504 u_int32_t status;
2505
2506 status = aic_error_action(cmd, targ->inq_data,
2507 ahc_cmd_get_transaction_status(cmd),
2508 ahc_cmd_get_scsi_status(cmd));
2509
2510#ifdef AHC_DEBUG
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);
2516 }
2517#endif
2518
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) {
2523 case SS_NOP:
2524 {
2525 AHC_SET_DV_STATE(ahc, targ, targ->dv_state+1);
2526 break;
2527 }
2528 case SS_INQ_REFRESH:
2529 AHC_SET_DV_STATE(ahc, targ,
2530 AHC_DV_STATE_INQ_SHORT_ASYNC);
2531 break;
2532 case SS_TUR:
2533 case SS_RETRY:
2534 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2535 if (ahc_cmd_get_transaction_status(cmd)
2536 == CAM_REQUEUE_REQ)
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)
2541 break;
2542 /* FALLTHROUGH */
2543 default:
2544 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2545#ifdef AHC_DEBUG
2546 if (ahc_debug & AHC_SHOW_DV) {
2547 ahc_print_devinfo(ahc, devinfo);
2548 printf("Failed DV inquiry, skipping\n");
2549 }
2550#endif
2551 break;
2552 }
2553 break;
2554 case AHC_DV_STATE_INQ_ASYNC_VERIFY:
2555 switch (status & SS_MASK) {
2556 case SS_NOP:
2557 {
2558 u_int xportflags;
2559 u_int spi3data;
2560
2561 if (memcmp(targ->inq_data, targ->dv_buffer,
2562 AHC_LINUX_DV_INQ_LEN) != 0) {
2563 /*
2564 * Inquiry data must have changed.
2565 * Try from the top again.
2566 */
2567 AHC_SET_DV_STATE(ahc, targ,
2568 AHC_DV_STATE_INQ_SHORT_ASYNC);
2569 break;
2570 }
2571
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)
2575 break;
2576
2577 xportflags = targ->inq_data->flags;
2578 if ((xportflags & (SID_Sync|SID_WBus16)) == 0)
2579 break;
2580
2581 spi3data = targ->inq_data->spi3data;
2582 switch (spi3data & SID_SPI_CLOCK_DT_ST) {
2583 default:
2584 case SID_SPI_CLOCK_ST:
2585 /* Assume only basic DV is supported. */
2586 targ->flags |= AHC_BASIC_DV;
2587 break;
2588 case SID_SPI_CLOCK_DT:
2589 case SID_SPI_CLOCK_DT_ST:
2590 targ->flags |= AHC_ENHANCED_DV;
2591 break;
2592 }
2593 break;
2594 }
2595 case SS_INQ_REFRESH:
2596 AHC_SET_DV_STATE(ahc, targ,
2597 AHC_DV_STATE_INQ_SHORT_ASYNC);
2598 break;
2599 case SS_TUR:
2600 case SS_RETRY:
2601 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2602 if (ahc_cmd_get_transaction_status(cmd)
2603 == CAM_REQUEUE_REQ)
2604 targ->dv_state_retry--;
2605
2606 if ((status & SS_ERRMASK) == EBUSY)
2607 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2608 if (targ->dv_state_retry < 10)
2609 break;
2610 /* FALLTHROUGH */
2611 default:
2612 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2613#ifdef AHC_DEBUG
2614 if (ahc_debug & AHC_SHOW_DV) {
2615 ahc_print_devinfo(ahc, devinfo);
2616 printf("Failed DV inquiry, skipping\n");
2617 }
2618#endif
2619 break;
2620 }
2621 break;
2622 case AHC_DV_STATE_INQ_VERIFY:
2623 switch (status & SS_MASK) {
2624 case SS_NOP:
2625 {
2626
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);
2630 break;
2631 }
2632#ifdef AHC_DEBUG
2633 if (ahc_debug & AHC_SHOW_DV) {
2634 int i;
2635
2636 ahc_print_devinfo(ahc, devinfo);
2637 printf("Inquiry buffer mismatch:");
2638 for (i = 0; i < AHC_LINUX_DV_INQ_LEN; i++) {
2639 if ((i & 0xF) == 0)
2640 printf("\n ");
2641 printf("0x%x:0x0%x ",
2642 ((uint8_t *)targ->inq_data)[i],
2643 targ->dv_buffer[i]);
2644 }
2645 printf("\n");
2646 }
2647#endif
2648
2649 if (ahc_linux_fallback(ahc, devinfo) != 0) {
2650 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2651 break;
2652 }
2653 /*
2654 * Do not count "falling back"
2655 * against our retries.
2656 */
2657 targ->dv_state_retry = 0;
2658 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2659 break;
2660 }
2661 case SS_INQ_REFRESH:
2662 AHC_SET_DV_STATE(ahc, targ,
2663 AHC_DV_STATE_INQ_SHORT_ASYNC);
2664 break;
2665 case SS_TUR:
2666 case SS_RETRY:
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,
2674 AHC_DV_STATE_EXIT);
2675 break;
2676 }
2677 /*
2678 * Do not count "falling back"
2679 * against our retries.
2680 */
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)
2685 break;
2686 /* FALLTHROUGH */
2687 default:
2688 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2689#ifdef AHC_DEBUG
2690 if (ahc_debug & AHC_SHOW_DV) {
2691 ahc_print_devinfo(ahc, devinfo);
2692 printf("Failed DV inquiry, skipping\n");
2693 }
2694#endif
2695 break;
2696 }
2697 break;
2698
2699 case AHC_DV_STATE_TUR:
2700 switch (status & SS_MASK) {
2701 case SS_NOP:
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);
2708 } else {
2709 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2710 }
2711 break;
2712 case SS_RETRY:
2713 case SS_TUR:
2714 if ((status & SS_ERRMASK) == EBUSY) {
2715 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_BUSY);
2716 break;
2717 }
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,
2725 AHC_DV_STATE_EXIT);
2726 break;
2727 }
2728 /*
2729 * Do not count "falling back"
2730 * against our retries.
2731 */
2732 targ->dv_state_retry = 0;
2733 }
2734 if (targ->dv_state_retry >= 10) {
2735#ifdef AHC_DEBUG
2736 if (ahc_debug & AHC_SHOW_DV) {
2737 ahc_print_devinfo(ahc, devinfo);
2738 printf("DV TUR reties exhausted\n");
2739 }
2740#endif
2741 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2742 break;
2743 }
2744 if (status & SSQ_DELAY)
2745 ssleep(1);
2746
2747 break;
2748 case SS_START:
2749 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_SU);
2750 break;
2751 case SS_INQ_REFRESH:
2752 AHC_SET_DV_STATE(ahc, targ,
2753 AHC_DV_STATE_INQ_SHORT_ASYNC);
2754 break;
2755 default:
2756 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2757 break;
2758 }
2759 break;
2760
2761 case AHC_DV_STATE_REBD:
2762 switch (status & SS_MASK) {
2763 case SS_NOP:
2764 {
2765 uint32_t echo_size;
2766
2767 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_WEB);
2768 echo_size = scsi_3btoul(&targ->dv_buffer[1]);
2769 echo_size &= 0x1FFF;
2770#ifdef AHC_DEBUG
2771 if (ahc_debug & AHC_SHOW_DV) {
2772 ahc_print_devinfo(ahc, devinfo);
2773 printf("Echo buffer size= %d\n", echo_size);
2774 }
2775#endif
2776 if (echo_size == 0) {
2777 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2778 break;
2779 }
2780
2781 /* Generate the buffer pattern */
2782 targ->dv_echo_size = echo_size;
2783 ahc_linux_generate_dv_pattern(targ);
2784 /*
2785 * Setup initial negotiation values.
2786 */
2787 ahc_linux_filter_inquiry(ahc, devinfo);
2788 break;
2789 }
2790 case SS_INQ_REFRESH:
2791 AHC_SET_DV_STATE(ahc, targ,
2792 AHC_DV_STATE_INQ_SHORT_ASYNC);
2793 break;
2794 case SS_RETRY:
2795 AHC_SET_DV_STATE(ahc, targ, targ->dv_state);
2796 if (ahc_cmd_get_transaction_status(cmd)
2797 == CAM_REQUEUE_REQ)
2798 targ->dv_state_retry--;
2799 if (targ->dv_state_retry <= 10)
2800 break;
2801#ifdef AHC_DEBUG
2802 if (ahc_debug & AHC_SHOW_DV) {
2803 ahc_print_devinfo(ahc, devinfo);
2804 printf("DV REBD reties exhausted\n");
2805 }
2806#endif
2807 /* FALLTHROUGH */
2808 case SS_FATAL:
2809 default:
2810 /*
2811 * Setup initial negotiation values
2812 * and try level 1 DV.
2813 */
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;
2817 break;
2818 }
2819 break;
2820
2821 case AHC_DV_STATE_WEB:
2822 switch (status & SS_MASK) {
2823 case SS_NOP:
2824 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_REB);
2825 break;
2826 case SS_INQ_REFRESH:
2827 AHC_SET_DV_STATE(ahc, targ,
2828 AHC_DV_STATE_INQ_SHORT_ASYNC);
2829 break;
2830 case SS_RETRY:
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,
2838 AHC_DV_STATE_EXIT);
2839 break;
2840 }
2841 /*
2842 * Do not count "falling back"
2843 * against our retries.
2844 */
2845 targ->dv_state_retry = 0;
2846 }
2847 if (targ->dv_state_retry <= 10)
2848 break;
2849 /* FALLTHROUGH */
2850#ifdef AHC_DEBUG
2851 if (ahc_debug & AHC_SHOW_DV) {
2852 ahc_print_devinfo(ahc, devinfo);
2853 printf("DV WEB reties exhausted\n");
2854 }
2855#endif
2856 default:
2857 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2858 break;
2859 }
2860 break;
2861
2862 case AHC_DV_STATE_REB:
2863 switch (status & SS_MASK) {
2864 case SS_NOP:
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,
2869 AHC_DV_STATE_EXIT);
2870 else
2871 AHC_SET_DV_STATE(ahc, targ,
2872 AHC_DV_STATE_WEB);
2873 break;
2874 }
2875
2876 if (targ->dv_buffer != NULL) {
2877 free(targ->dv_buffer, M_DEVBUF);
2878 targ->dv_buffer = NULL;
2879 }
2880 if (targ->dv_buffer1 != NULL) {
2881 free(targ->dv_buffer1, M_DEVBUF);
2882 targ->dv_buffer1 = NULL;
2883 }
2884 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2885 break;
2886 case SS_INQ_REFRESH:
2887 AHC_SET_DV_STATE(ahc, targ,
2888 AHC_DV_STATE_INQ_SHORT_ASYNC);
2889 break;
2890 case SS_RETRY:
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,
2898 AHC_DV_STATE_EXIT);
2899 break;
2900 }
2901 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_WEB);
2902 }
2903 if (targ->dv_state_retry <= 10) {
2904 if ((status & (SSQ_DELAY_RANDOM|SSQ_DELAY))!= 0)
2905 msleep(ahc->our_id*1000/10);
2906 break;
2907 }
2908#ifdef AHC_DEBUG
2909 if (ahc_debug & AHC_SHOW_DV) {
2910 ahc_print_devinfo(ahc, devinfo);
2911 printf("DV REB reties exhausted\n");
2912 }
2913#endif
2914 /* FALLTHROUGH */
2915 default:
2916 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2917 break;
2918 }
2919 break;
2920
2921 case AHC_DV_STATE_SU:
2922 switch (status & SS_MASK) {
2923 case SS_NOP:
2924 case SS_INQ_REFRESH:
2925 AHC_SET_DV_STATE(ahc, targ,
2926 AHC_DV_STATE_INQ_SHORT_ASYNC);
2927 break;
2928 default:
2929 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2930 break;
2931 }
2932 break;
2933
2934 case AHC_DV_STATE_BUSY:
2935 switch (status & SS_MASK) {
2936 case SS_NOP:
2937 case SS_INQ_REFRESH:
2938 AHC_SET_DV_STATE(ahc, targ,
2939 AHC_DV_STATE_INQ_SHORT_ASYNC);
2940 break;
2941 case SS_TUR:
2942 case SS_RETRY:
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)
2949 ssleep(1);
2950 } else {
2951#ifdef AHC_DEBUG
2952 if (ahc_debug & AHC_SHOW_DV) {
2953 ahc_print_devinfo(ahc, devinfo);
2954 printf("DV BUSY reties exhausted\n");
2955 }
2956#endif
2957 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2958 }
2959 break;
2960 default:
2961 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2962 break;
2963 }
2964 break;
2965
2966 default:
2967 printf("%s: Invalid DV completion state %d\n", ahc_name(ahc),
2968 targ->dv_state);
2969 AHC_SET_DV_STATE(ahc, targ, AHC_DV_STATE_EXIT);
2970 break;
2971 }
2972}
2973
2974static void
2975ahc_linux_dv_fill_cmd(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
2976 struct ahc_devinfo *devinfo)
2977{
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;
2981}
2982
2983/*
2984 * Synthesize an inquiry command. On the return trip, it'll be
2985 * sniffed and the device transfer settings set for us.
2986 */
2987static void
2988ahc_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)
2991{
2992
2993#ifdef AHC_DEBUG
2994 if (ahc_debug & AHC_SHOW_DV) {
2995 ahc_print_devinfo(ahc, devinfo);
2996 printf("Sending INQ\n");
2997 }
2998#endif
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);
3007 }
3008
3009 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3010 cmd->sc_data_direction = SCSI_DATA_READ;
3011 cmd->cmd_len = 6;
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;
3017 else
3018 cmd->request_buffer = targ->inq_data;
3019 memset(cmd->request_buffer, 0, AHC_LINUX_DV_INQ_LEN);
3020}
3021
3022static void
3023ahc_linux_dv_tur(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3024 struct ahc_devinfo *devinfo)
3025{
3026
3027#ifdef AHC_DEBUG
3028 if (ahc_debug & AHC_SHOW_DV) {
3029 ahc_print_devinfo(ahc, devinfo);
3030 printf("Sending TUR\n");
3031 }
3032#endif
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 = SCSI_DATA_NONE;
3036 cmd->cmd_len = 6;
3037 cmd->cmnd[0] = TEST_UNIT_READY;
3038}
3039
3040#define AHC_REBD_LEN 4
3041
3042static void
3043ahc_linux_dv_rebd(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3044 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ)
3045{
3046
3047#ifdef AHC_DEBUG
3048 if (ahc_debug & AHC_SHOW_DV) {
3049 ahc_print_devinfo(ahc, devinfo);
3050 printf("Sending REBD\n");
3051 }
3052#endif
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 = SCSI_DATA_READ;
3058 cmd->cmd_len = 10;
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;
3065}
3066
3067static void
3068ahc_linux_dv_web(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3069 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ)
3070{
3071
3072#ifdef AHC_DEBUG
3073 if (ahc_debug & AHC_SHOW_DV) {
3074 ahc_print_devinfo(ahc, devinfo);
3075 printf("Sending WEB\n");
3076 }
3077#endif
3078 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3079 cmd->sc_data_direction = SCSI_DATA_WRITE;
3080 cmd->cmd_len = 10;
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;
3087}
3088
3089static void
3090ahc_linux_dv_reb(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3091 struct ahc_devinfo *devinfo, struct ahc_linux_target *targ)
3092{
3093
3094#ifdef AHC_DEBUG
3095 if (ahc_debug & AHC_SHOW_DV) {
3096 ahc_print_devinfo(ahc, devinfo);
3097 printf("Sending REB\n");
3098 }
3099#endif
3100 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3101 cmd->sc_data_direction = SCSI_DATA_READ;
3102 cmd->cmd_len = 10;
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;
3109}
3110
3111static void
3112ahc_linux_dv_su(struct ahc_softc *ahc, struct scsi_cmnd *cmd,
3113 struct ahc_devinfo *devinfo,
3114 struct ahc_linux_target *targ)
3115{
3116 u_int le;
3117
3118 le = SID_IS_REMOVABLE(targ->inq_data) ? SSS_LOEJ : 0;
3119
3120#ifdef AHC_DEBUG
3121 if (ahc_debug & AHC_SHOW_DV) {
3122 ahc_print_devinfo(ahc, devinfo);
3123 printf("Sending SU\n");
3124 }
3125#endif
3126 ahc_linux_dv_fill_cmd(ahc, cmd, devinfo);
3127 cmd->sc_data_direction = SCSI_DATA_NONE;
3128 cmd->cmd_len = 6;
3129 cmd->cmnd[0] = START_STOP_UNIT;
3130 cmd->cmnd[4] = le | SSS_START;
3131}
3132
3133static int
3134ahc_linux_fallback(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3135{
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;
3141 u_long s;
3142 u_int width;
3143 u_int period;
3144 u_int offset;
3145 u_int ppr_options;
3146 u_int cur_speed;
3147 u_int wide_speed;
3148 u_int narrow_speed;
3149 u_int fallback_speed;
3150
3151#ifdef AHC_DEBUG
3152 if (ahc_debug & AHC_SHOW_DV) {
3153 ahc_print_devinfo(ahc, devinfo);
3154 printf("Trying to fallback\n");
3155 }
3156#endif
3157 ahc_lock(ahc, &s);
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;
3167 if (offset == 0)
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;
3179
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,
3183 MAX_OFFSET,
3184 AHC_SYNCRATE_MIN);
3185 narrow_speed = aic_calc_speed(MSG_EXT_WDTR_BUS_8_BIT,
3186 targ->dv_next_narrow_period,
3187 MAX_OFFSET,
3188 AHC_SYNCRATE_MIN);
3189 fallback_speed = aic_calc_speed(width, period+1, offset,
3190 AHC_SYNCRATE_MIN);
3191#ifdef AHC_DEBUG
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);
3196 }
3197#endif
3198
3199 if (cur_speed > 160000) {
3200 /*
3201 * Paced/DT/IU_REQ only transfer speeds. All we
3202 * can do is fallback in terms of syncrate.
3203 */
3204 period++;
3205 } else if (cur_speed > 80000) {
3206 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3207 /*
3208 * Try without IU_REQ as it may be confusing
3209 * an expander.
3210 */
3211 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
3212 } else {
3213 /*
3214 * Paced/DT only transfer speeds. All we
3215 * can do is fallback in terms of syncrate.
3216 */
3217 period++;
3218 ppr_options = targ->dv_max_ppr_options;
3219 }
3220 } else if (cur_speed > 3300) {
3221
3222 /*
3223 * In this range we the following
3224 * options ordered from highest to
3225 * lowest desireability:
3226 *
3227 * o Wide/DT
3228 * o Wide/non-DT
3229 * o Narrow at a potentally higher sync rate.
3230 *
3231 * All modes are tested with and without IU_REQ
3232 * set since using IUs may confuse an expander.
3233 */
3234 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3235
3236 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
3237 } else if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
3238 /*
3239 * Try going non-DT.
3240 */
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) {
3244 /*
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.
3251 */
3252 ppr_options = 0;
3253 } else if (width == MSG_EXT_WDTR_BUS_16_BIT) {
3254 /*
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
3260 * specifically.
3261 */
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;
3268 } else {
3269 period++;
3270 }
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)) {
3276
3277 /*
3278 * We are narrow. Try falling back
3279 * to the next wide speed with
3280 * all supported ppr options set.
3281 */
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;
3286 } else {
3287 /* Only narrow fallback is allowed. */
3288 period++;
3289 ppr_options = targ->dv_max_ppr_options;
3290 }
3291 } else {
3292 ahc_unlock(ahc, &s);
3293 return (-1);
3294 }
3295 offset = MAX_OFFSET;
3296 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,
3297 AHC_SYNCRATE_DT);
3298 ahc_set_width(ahc, devinfo, width, AHC_TRANS_GOAL, FALSE);
3299 if (period == 0) {
3300 period = 0;
3301 offset = 0;
3302 ppr_options = 0;
3303 if (width == MSG_EXT_WDTR_BUS_8_BIT)
3304 targ->dv_next_narrow_period = AHC_ASYNC_XFER_PERIOD;
3305 else
3306 targ->dv_next_wide_period = AHC_ASYNC_XFER_PERIOD;
3307 }
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);
3312 return (0);
3313}
3314
3315static void
3316ahc_linux_dv_timeout(struct scsi_cmnd *cmd)
3317{
3318 struct ahc_softc *ahc;
3319 struct scb *scb;
3320 u_long flags;
3321
3322 ahc = *((struct ahc_softc **)cmd->device->host->hostdata);
3323 ahc_lock(ahc, &flags);
3324
3325#ifdef AHC_DEBUG
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);
3330 }
3331#endif
3332
3333 /*
3334 * Guard against "done race". No action is
3335 * required if we just completed.
3336 */
3337 if ((scb = (struct scb *)cmd->host_scribble) == NULL) {
3338 ahc_unlock(ahc, &flags);
3339 return;
3340 }
3341
3342 /*
3343 * Command has not completed. Mark this
3344 * SCB as having failing status prior to
3345 * resetting the bus, so we get the correct
3346 * error code.
3347 */
3348 if ((scb->flags & SCB_SENSE) != 0)
3349 ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
3350 else
3351 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
3352 ahc_reset_channel(ahc, cmd->device->channel + 'A', /*initiate*/TRUE);
3353
3354 /*
3355 * Add a minimal bus settle delay for devices that are slow to
3356 * respond after bus resets.
3357 */
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);
3369}
3370
3371static void
3372ahc_linux_dv_complete(struct scsi_cmnd *cmd)
3373{
3374 struct ahc_softc *ahc;
3375
3376 ahc = *((struct ahc_softc **)cmd->device->host->hostdata);
3377
3378 /* Delete the DV timer before it goes off! */
3379 scsi_delete_timer(cmd);
3380
3381#ifdef AHC_DEBUG
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);
3386#endif
3387
3388 /* Wake up the state machine */
3389 up(&ahc->platform_data->dv_cmd_sem);
3390}
3391
3392static void
3393ahc_linux_generate_dv_pattern(struct ahc_linux_target *targ)
3394{
3395 uint16_t b;
3396 u_int i;
3397 u_int j;
3398
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);
3405
3406 i = 0;
3407 b = 0x0001;
3408 for (j = 0 ; i < targ->dv_echo_size; j++) {
3409 if (j < 32) {
3410 /*
3411 * 32bytes of sequential numbers.
3412 */
3413 targ->dv_buffer[i++] = j & 0xff;
3414 } else if (j < 48) {
3415 /*
3416 * 32bytes of repeating 0x0000, 0xffff.
3417 */
3418 targ->dv_buffer[i++] = (j & 0x02) ? 0xff : 0x00;
3419 } else if (j < 64) {
3420 /*
3421 * 32bytes of repeating 0x5555, 0xaaaa.
3422 */
3423 targ->dv_buffer[i++] = (j & 0x02) ? 0xaa : 0x55;
3424 } else {
3425 /*
3426 * Remaining buffer is filled with a repeating
3427 * patter of:
3428 *
3429 * 0xffff
3430 * ~0x0001 << shifted once in each loop.
3431 */
3432 if (j & 0x02) {
3433 if (j & 0x01) {
3434 targ->dv_buffer[i++] = ~(b >> 8) & 0xff;
3435 b <<= 1;
3436 if (b == 0x0000)
3437 b = 0x0001;
3438 } else {
3439 targ->dv_buffer[i++] = (~b & 0xff);
3440 }
3441 } else {
3442 targ->dv_buffer[i++] = 0xff;
3443 }
3444 }
3445 }
3446}
3447
3448static u_int
3449ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
3450{
3451 static int warned_user;
3452 u_int tags;
3453
3454 tags = 0;
3455 if ((ahc->user_discenable & devinfo->target_mask) != 0) {
3456 if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
3457 if (warned_user == 0) {
3458
3459 printf(KERN_WARNING
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");
3464 warned_user++;
3465 }
3466 tags = AHC_MAX_QUEUE;
3467 } else {
3468 adapter_tag_info_t *tag_info;
3469
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;
3474 }
3475 }
3476 return (tags);
3477}
3478
3479static u_int
3480ahc_linux_user_dv_setting(struct ahc_softc *ahc)
3481{
3482 static int warned_user;
3483 int dv;
3484
3485 if (ahc->unit >= NUM_ELEMENTS(aic7xxx_dv_settings)) {
3486 if (warned_user == 0) {
3487
3488 printf(KERN_WARNING
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");
3493 warned_user++;
3494 }
3495 dv = -1;
3496 } else {
3497
3498 dv = aic7xxx_dv_settings[ahc->unit];
3499 }
3500
3501 if (dv < 0) {
3502 u_long s;
3503
3504 /*
3505 * Apply the default.
3506 */
3507 /*
3508 * XXX - Enable DV on non-U160 controllers once it
3509 * has been tested there.
3510 */
3511 ahc_lock(ahc, &s);
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);
3517 }
3518 return (dv);
3519}
3520
3521/*
3522 * Determines the queue depth for a given device.
3523 */
3524static void
3525ahc_linux_device_queue_depth(struct ahc_softc *ahc,
3526 struct ahc_linux_device *dev)
3527{
3528 struct ahc_devinfo devinfo;
3529 u_int tags;
3530
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',
3536 ROLE_INITIATOR);
3537 tags = ahc_linux_user_tagdepth(ahc, &devinfo);
3538 if (tags != 0
3539 && dev->scsi_device != NULL
3540 && dev->scsi_device->tagged_supported != 0) {
3541
3542 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
3543 ahc_print_devinfo(ahc, &devinfo);
3544 printf("Tagged Queuing enabled. Depth %d\n", tags);
3545 } else {
3546 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE);
3547 }
3548}
3549
3550static void
3551ahc_linux_run_device_queue(struct ahc_softc *ahc, struct ahc_linux_device *dev)
3552{
3553 struct ahc_cmd *acmd;
3554 struct scsi_cmnd *cmd;
3555 struct scb *scb;
3556 struct hardware_scb *hscb;
3557 struct ahc_initiator_tinfo *tinfo;
3558 struct ahc_tmode_tstate *tstate;
3559 uint16_t mask;
3560
3561 if ((dev->flags & AHC_DEV_ON_RUN_LIST) != 0)
3562 panic("running device on run list");
3563
3564 while ((acmd = TAILQ_FIRST(&dev->busyq)) != NULL
3565 && dev->openings > 0 && dev->qfrozen == 0) {
3566
3567 /*
3568 * Schedule us to run later. The only reason we are not
3569 * running is because the whole controller Q is frozen.
3570 */
3571 if (ahc->platform_data->qfrozen != 0
3572 && AHC_DV_SIMQ_FROZEN(ahc) == 0) {
3573 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
3574 dev, links);
3575 dev->flags |= AHC_DEV_ON_RUN_LIST;
3576 return;
3577 }
3578 /*
3579 * Get an scb to use.
3580 */
3581 if ((scb = ahc_get_scb(ahc)) == NULL) {
3582 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
3583 dev, links);
3584 dev->flags |= AHC_DEV_ON_RUN_LIST;
3585 ahc->flags |= AHC_RESOURCE_SHORTAGE;
3586 return;
3587 }
3588 TAILQ_REMOVE(&dev->busyq, acmd, acmd_links.tqe);
3589 cmd = &acmd_scsi_cmd(acmd);
3590 scb->io_ctx = cmd;
3591 scb->platform_data->dev = dev;
3592 hscb = scb->hscb;
3593 cmd->host_scribble = (char *)scb;
3594
3595 /*
3596 * Fill out basics of the HSCB.
3597 */
3598 hscb->control = 0;
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;
3609
3610 if ((ahc->user_discenable & mask) != 0)
3611 hscb->control |= DISCENB;
3612
3613 if (AHC_DV_CMD(cmd) != 0)
3614 scb->flags |= SCB_SILENT;
3615
3616 if ((tstate->auto_negotiate & mask) != 0) {
3617 scb->flags |= SCB_AUTO_NEGOTIATE;
3618 scb->hscb->control |= MK_MESSAGE;
3619 }
3620
3621 if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
3622#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3623 int msg_bytes;
3624 uint8_t tag_msgs[2];
3625
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;
3631 } else
3632#endif
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;
3637 } else {
3638 hscb->control |= MSG_SIMPLE_TASK;
3639 }
3640 }
3641
3642 hscb->cdb_len = cmd->cmd_len;
3643 if (hscb->cdb_len <= 12) {
3644 memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
3645 } else {
3646 memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
3647 scb->flags |= SCB_CDB32_PTR;
3648 }
3649
3650 scb->platform_data->xfer_len = 0;
3651 ahc_set_residual(scb, 0);
3652 ahc_set_sense_residual(scb, 0);
3653 scb->sg_count = 0;
3654 if (cmd->use_sg != 0) {
3655 struct ahc_dma_seg *sg;
3656 struct scatterlist *cur_seg;
3657 struct scatterlist *end_seg;
3658 int nseg;
3659
3660 cur_seg = (struct scatterlist *)cmd->request_buffer;
3661 nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
3662 scsi_to_pci_dma_dir(cmd->sc_data_direction));
3663 end_seg = cur_seg + nseg;
3664 /* Copy the segments into the SG list. */
3665 sg = scb->sg_list;
3666 /*
3667 * The sg_count may be larger than nseg if
3668 * a transfer crosses a 32bit page.
3669 */
3670 while (cur_seg < end_seg) {
3671 dma_addr_t addr;
3672 bus_size_t len;
3673 int consumed;
3674
3675 addr = sg_dma_address(cur_seg);
3676 len = sg_dma_len(cur_seg);
3677 consumed = ahc_linux_map_seg(ahc, scb,
3678 sg, addr, len);
3679 sg += consumed;
3680 scb->sg_count += consumed;
3681 cur_seg++;
3682 }
3683 sg--;
3684 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
3685
3686 /*
3687 * Reset the sg list pointer.
3688 */
3689 scb->hscb->sgptr =
3690 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
3691
3692 /*
3693 * Copy the first SG into the "current"
3694 * data pointer area.
3695 */
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;
3700 dma_addr_t addr;
3701
3702 sg = scb->sg_list;
3703 addr = pci_map_single(ahc->dev_softc,
3704 cmd->request_buffer,
3705 cmd->request_bufflen,
3706 scsi_to_pci_dma_dir(cmd->sc_data_direction));
3707 scb->platform_data->buf_busaddr = addr;
3708 scb->sg_count = ahc_linux_map_seg(ahc, scb,
3709 sg, addr,
3710 cmd->request_bufflen);
3711 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
3712
3713 /*
3714 * Reset the sg list pointer.
3715 */
3716 scb->hscb->sgptr =
3717 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
3718
3719 /*
3720 * Copy the first SG into the "current"
3721 * data pointer area.
3722 */
3723 scb->hscb->dataptr = sg->addr;
3724 scb->hscb->datacnt = sg->len;
3725 } else {
3726 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
3727 scb->hscb->dataptr = 0;
3728 scb->hscb->datacnt = 0;
3729 scb->sg_count = 0;
3730 }
3731
3732 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_PREWRITE);
3733 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
3734 dev->openings--;
3735 dev->active++;
3736 dev->commands_issued++;
3737 if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
3738 dev->commands_since_idle_or_otag++;
3739
3740 /*
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.
3745 */
3746 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
3747 && (ahc->features & AHC_SCB_BTT) == 0) {
3748 struct scb_tailq *untagged_q;
3749 int target_offset;
3750
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)
3756 continue;
3757 }
3758 scb->flags |= SCB_ACTIVE;
3759 ahc_queue_scb(ahc, scb);
3760 }
3761}
3762
3763/*
3764 * SCSI controller interrupt handler.
3765 */
3766irqreturn_t
3767ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
3768{
3769 struct ahc_softc *ahc;
3770 u_long flags;
3771 int ours;
3772
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);
3781}
3782
3783void
3784ahc_platform_flushwork(struct ahc_softc *ahc)
3785{
3786
3787 while (ahc_linux_run_complete_queue(ahc) != NULL)
3788 ;
3789}
3790
3791static struct ahc_linux_target*
3792ahc_linux_alloc_target(struct ahc_softc *ahc, u_int channel, u_int target)
3793{
3794 struct ahc_linux_target *targ;
3795 u_int target_offset;
3796
3797 target_offset = target;
3798 if (channel != 0)
3799 target_offset += 8;
3800
3801 targ = malloc(sizeof(*targ), M_DEVBUG, M_NOWAIT);
3802 if (targ == NULL)
3803 return (NULL);
3804 memset(targ, 0, sizeof(*targ));
3805 targ->channel = channel;
3806 targ->target = target;
3807 targ->ahc = ahc;
3808 targ->flags = AHC_DV_REQUIRED;
3809 ahc->platform_data->targets[target_offset] = targ;
3810 return (targ);
3811}
3812
3813static void
3814ahc_linux_free_target(struct ahc_softc *ahc, struct ahc_linux_target *targ)
3815{
3816 struct ahc_devinfo devinfo;
3817 struct ahc_initiator_tinfo *tinfo;
3818 struct ahc_tmode_tstate *tstate;
3819 u_int our_id;
3820 u_int target_offset;
3821 char channel;
3822
3823 /*
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.
3827 */
3828 channel = 'A' + targ->channel;
3829 our_id = ahc->our_id;
3830 target_offset = targ->target;
3831 if (targ->channel != 0) {
3832 target_offset += 8;
3833 our_id = ahc->our_id_b;
3834 }
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);
3852}
3853
3854static struct ahc_linux_device*
3855ahc_linux_alloc_device(struct ahc_softc *ahc,
3856 struct ahc_linux_target *targ, u_int lun)
3857{
3858 struct ahc_linux_device *dev;
3859
3860 dev = malloc(sizeof(*dev), M_DEVBUG, M_NOWAIT);
3861 if (dev == NULL)
3862 return (NULL);
3863 memset(dev, 0, sizeof(*dev));
3864 init_timer(&dev->timer);
3865 TAILQ_INIT(&dev->busyq);
3866 dev->flags = AHC_DEV_UNCONFIGURED;
3867 dev->lun = lun;
3868 dev->target = targ;
3869
3870 /*
3871 * We start out life using untagged
3872 * transactions of which we allow one.
3873 */
3874 dev->openings = 1;
3875
3876 /*
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.
3880 */
3881 dev->maxtags = 0;
3882
3883 targ->refcount++;
3884 targ->devices[lun] = dev;
3885 return (dev);
3886}
3887
3888static void
3889__ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
3890{
3891 struct ahc_linux_target *targ;
3892
3893 targ = dev->target;
3894 targ->devices[dev->lun] = NULL;
3895 free(dev, M_DEVBUF);
3896 targ->refcount--;
3897 if (targ->refcount == 0
3898 && (targ->flags & AHC_DV_REQUIRED) == 0)
3899 ahc_linux_free_target(ahc, targ);
3900}
3901
3902static void
3903ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
3904{
3905 del_timer_sync(&dev->timer);
3906 __ahc_linux_free_device(ahc, dev);
3907}
3908
3909void
3910ahc_send_async(struct ahc_softc *ahc, char channel,
3911 u_int target, u_int lun, ac_code code, void *arg)
3912{
3913 switch (code) {
3914 case AC_TRANSFER_NEG:
3915 {
3916 char buf[80];
3917 struct ahc_linux_target *targ;
3918 struct info_str info;
3919 struct ahc_initiator_tinfo *tinfo;
3920 struct ahc_tmode_tstate *tstate;
3921 int target_offset;
3922
3923 info.buffer = buf;
3924 info.length = sizeof(buf);
3925 info.offset = 0;
3926 info.pos = 0;
3927 tinfo = ahc_fetch_transinfo(ahc, channel,
3928 channel == 'A' ? ahc->our_id
3929 : ahc->our_id_b,
3930 target, &tstate);
3931
3932 /*
3933 * Don't bother reporting results while
3934 * negotiations are still pending.
3935 */
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)
3941 break;
3942
3943 /*
3944 * Don't bother reporting results that
3945 * are identical to those last reported.
3946 */
3947 target_offset = target;
3948 if (channel == 'B')
3949 target_offset += 8;
3950 targ = ahc->platform_data->targets[target_offset];
3951 if (targ == NULL)
3952 break;
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)
3958 break;
3959
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;
3964
3965 printf("(%s:%c:", ahc_name(ahc), channel);
3966 if (target == CAM_TARGET_WILDCARD)
3967 printf("*): ");
3968 else
3969 printf("%d): ", target);
3970 ahc_format_transinfo(&info, &tinfo->curr);
3971 if (info.pos < info.length)
3972 *info.buffer = '\0';
3973 else
3974 buf[info.length - 1] = '\0';
3975 printf("%s", buf);
3976 break;
3977 }
3978 case AC_SENT_BDR:
3979 {
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);
3984#else
3985 Scsi_Device *scsi_dev;
3986
3987 /*
3988 * Find the SCSI device associated with this
3989 * request and indicate that a UA is expected.
3990 */
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;
3999 }
4000 }
4001#endif
4002 break;
4003 }
4004 case AC_BUS_RESET:
4005 if (ahc->platform_data->host != NULL) {
4006 scsi_report_bus_reset(ahc->platform_data->host,
4007 channel - 'A');
4008 }
4009 break;
4010 default:
4011 panic("ahc_send_async: Unexpected async event");
4012 }
4013}
4014
4015/*
4016 * Calls the higher level scsi done function and frees the scb.
4017 */
4018void
4019ahc_done(struct ahc_softc *ahc, struct scb *scb)
4020{
4021 Scsi_Cmnd *cmd;
4022 struct ahc_linux_device *dev;
4023
4024 LIST_REMOVE(scb, pending_links);
4025 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
4026 struct scb_tailq *untagged_q;
4027 int target_offset;
4028
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);
4033 }
4034
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");
4039 }
4040 cmd = scb->io_ctx;
4041 dev = scb->platform_data->dev;
4042 dev->active--;
4043 dev->openings++;
4044 if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
4045 cmd->result &= ~(CAM_DEV_QFRZN << 16);
4046 dev->qfrozen--;
4047 }
4048 ahc_linux_unmap_scb(ahc, scb);
4049
4050 /*
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".
4055 */
4056 cmd->sense_buffer[0] = 0;
4057 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
4058 uint32_t amount_xferred;
4059
4060 amount_xferred =
4061 ahc_get_transfer_length(scb) - ahc_get_residual(scb);
4062 if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
4063#ifdef AHC_DEBUG
4064 if ((ahc_debug & AHC_SHOW_MISC) != 0) {
4065 ahc_print_path(ahc, scb);
4066 printf("Set CAM_UNCOR_PARITY\n");
4067 }
4068#endif
4069 ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
4070#ifdef AHC_REPORT_UNDERFLOWS
4071 /*
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
4078 * of data.
4079 */
4080 } else if (amount_xferred < scb->io_ctx->underflow) {
4081 u_int i;
4082
4083 ahc_print_path(ahc, scb);
4084 printf("CDB:");
4085 for (i = 0; i < scb->io_ctx->cmd_len; i++)
4086 printf(" 0x%x", scb->io_ctx->cmnd[i]);
4087 printf("\n");
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);
4094#endif
4095 } else {
4096 ahc_set_transaction_status(scb, CAM_REQ_CMP);
4097 }
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;
4104 }
4105 /*
4106 * Start DV for devices that require it assuming the first command
4107 * sent does not result in a selection timeout.
4108 */
4109 if (ahc_get_transaction_status(scb) != CAM_SEL_TIMEOUT
4110 && (dev->target->flags & AHC_DV_REQUIRED) != 0)
4111 ahc_linux_start_dv(ahc);
4112
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++;
4117 /*
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.
4122 */
4123 if ((dev->openings + dev->active) < dev->maxtags
4124 && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
4125 dev->tag_success_count = 0;
4126 dev->openings++;
4127 }
4128
4129 if (dev->active == 0)
4130 dev->commands_since_idle_or_otag = 0;
4131
4132 if (TAILQ_EMPTY(&dev->busyq)) {
4133 if ((dev->flags & AHC_DEV_UNCONFIGURED) != 0
4134 && dev->active == 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;
4140 }
4141
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);
4150 }
4151 }
4152
4153 ahc_free_scb(ahc, scb);
4154 ahc_linux_queue_cmd_complete(ahc, cmd);
4155
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);
4160 }
4161
4162}
4163
4164static void
4165ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
4166 struct ahc_linux_device *dev, struct scb *scb)
4167{
4168 struct ahc_devinfo devinfo;
4169
4170 ahc_compile_devinfo(&devinfo,
4171 ahc->our_id,
4172 dev->target->target, dev->lun,
4173 dev->target->channel == 0 ? 'A' : 'B',
4174 ROLE_INITIATOR);
4175
4176 /*
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
4184 * commands.
4185 */
4186 switch (ahc_get_scsi_status(scb)) {
4187 default:
4188 break;
4189 case SCSI_STATUS_CHECK_COND:
4190 case SCSI_STATUS_CMD_TERMINATED:
4191 {
4192 Scsi_Cmnd *cmd;
4193
4194 /*
4195 * Copy sense information to the OS's cmd
4196 * structure if it is available.
4197 */
4198 cmd = scb->io_ctx;
4199 if (scb->flags & SCB_SENSE) {
4200 u_int sense_size;
4201
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);
4211#ifdef AHC_DEBUG
4212 if (ahc_debug & AHC_SHOW_SENSE) {
4213 int i;
4214
4215 printf("Copied %d bytes of sense data:",
4216 sense_size);
4217 for (i = 0; i < sense_size; i++) {
4218 if ((i & 0xF) == 0)
4219 printf("\n");
4220 printf("0x%x ", cmd->sense_buffer[i]);
4221 }
4222 printf("\n");
4223 }
4224#endif
4225 }
4226 break;
4227 }
4228 case SCSI_STATUS_QUEUE_FULL:
4229 {
4230 /*
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
4236 * the device.
4237 */
4238 dev->tag_success_count = 0;
4239 if (dev->active != 0) {
4240 /*
4241 * Drop our opening count to the number
4242 * of commands currently outstanding.
4243 */
4244 dev->openings = 0;
4245/*
4246 ahc_print_path(ahc, scb);
4247 printf("Dropping tag count to %d\n", dev->active);
4248 */
4249 if (dev->active == dev->tags_on_last_queuefull) {
4250
4251 dev->last_queuefull_same_count++;
4252 /*
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
4258 * this device.
4259 */
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",
4265 dev->active);
4266 }
4267 } else {
4268 dev->tags_on_last_queuefull = dev->active;
4269 dev->last_queuefull_same_count = 0;
4270 }
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);
4276 break;
4277 }
4278 /*
4279 * Drop down to a single opening, and treat this
4280 * as if the target returned BUSY SCSI status.
4281 */
4282 dev->openings = 1;
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);
4287 /* FALLTHROUGH */
4288 }
4289 case SCSI_STATUS_BUSY:
4290 {
4291 /*
4292 * Set a short timer to defer sending commands for
4293 * a bit since Linux will not delay in this case.
4294 */
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);
4299 break;
4300 }
4301 dev->flags |= AHC_DEV_TIMER_ACTIVE;
4302 dev->qfrozen++;
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);
4308 break;
4309 }
4310 }
4311}
4312
4313static void
4314ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, Scsi_Cmnd *cmd)
4315{
4316 /*
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
4326 * returned to us.
4327 */
4328 struct ahc_completeq *completeq;
4329 struct ahc_cmd *list_cmd;
4330 struct ahc_cmd *acmd;
4331
4332 /*
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.
4337 */
4338 if (AHC_DV_CMD(cmd) == FALSE) {
4339 u_int new_status;
4340
4341 switch (ahc_cmd_get_transaction_status(cmd)) {
4342 case CAM_REQ_INPROG:
4343 case CAM_REQ_CMP:
4344 case CAM_SCSI_STATUS_ERROR:
4345 new_status = DID_OK;
4346 break;
4347 case CAM_REQ_ABORTED:
4348 new_status = DID_ABORT;
4349 break;
4350 case CAM_BUSY:
4351 new_status = DID_BUS_BUSY;
4352 break;
4353 case CAM_REQ_INVALID:
4354 case CAM_PATH_INVALID:
4355 new_status = DID_BAD_TARGET;
4356 break;
4357 case CAM_SEL_TIMEOUT:
4358 new_status = DID_NO_CONNECT;
4359 break;
4360 case CAM_SCSI_BUS_RESET:
4361 case CAM_BDR_SENT:
4362 new_status = DID_RESET;
4363 break;
4364 case CAM_UNCOR_PARITY:
4365 new_status = DID_PARITY;
4366 break;
4367 case CAM_CMD_TIMEOUT:
4368 new_status = DID_TIME_OUT;
4369 break;
4370 case CAM_UA_ABORT:
4371 case CAM_REQ_CMP_ERR:
4372 case CAM_AUTOSENSE_FAIL:
4373 case CAM_NO_HBA:
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;
4383 break;
4384 case CAM_REQUEUE_REQ:
4385 /*
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!
4394 */
4395 if (cmd->retries > 0)
4396 cmd->retries--;
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;
4405 break;
4406 default:
4407 /* We should never get here */
4408 new_status = DID_ERROR;
4409 break;
4410 }
4411
4412 ahc_cmd_set_transaction_status(cmd, new_status);
4413 }
4414
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);
4424 else
4425 TAILQ_INSERT_TAIL(completeq, acmd, acmd_links.tqe);
4426}
4427
4428static void
4429ahc_linux_filter_inquiry(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
4430{
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;
4439 u_int maxsync;
4440 u_int width;
4441 u_int period;
4442 u_int offset;
4443 u_int ppr_options;
4444 u_int trans_version;
4445 u_int prot_version;
4446
4447 /*
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
4452 * command.
4453 */
4454 dev = ahc_linux_get_device(ahc, devinfo->channel - 'A',
4455 devinfo->target, devinfo->lun,
4456 /*alloc*/TRUE);
4457
4458 sid = (struct scsi_inquiry_data *)dev->target->inq_data;
4459 if (SID_QUAL(sid) == SID_QUAL_LU_CONNECTED) {
4460
4461 dev->flags &= ~AHC_DEV_UNCONFIGURED;
4462 } else {
4463 dev->flags |= AHC_DEV_UNCONFIGURED;
4464 return;
4465 }
4466
4467 /*
4468 * Update our notion of this device's transfer
4469 * negotiation capabilities.
4470 */
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));
4483
4484 /*
4485 * Only attempt SPI3/4 once we've verified that
4486 * the device claims to support SPI3/4 features.
4487 */
4488 if (prot_version < SCSI_REV_2)
4489 trans_version = SID_ANSI_REV(sid);
4490 else
4491 trans_version = SCSI_REV_2;
4492
4493 if ((sid->flags & SID_WBus16) == 0)
4494 width = MSG_EXT_WDTR_BUS_8_BIT;
4495 if ((sid->flags & SID_Sync) == 0) {
4496 period = 0;
4497 offset = 0;
4498 ppr_options = 0;
4499 }
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);
4507
4508 if (prot_version > SCSI_REV_2
4509 && ppr_options != 0)
4510 trans_version = user->transport_version;
4511
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;
4517 else
4518 maxsync = AHC_SYNCRATE_FAST;
4519
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) {
4524 period = 0;
4525 offset = 0;
4526 ppr_options = 0;
4527 }
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,
4534 /*paused*/FALSE);
4535}
4536
4537static void
4538ahc_linux_sem_timeout(u_long arg)
4539{
4540 struct ahc_softc *ahc;
4541 u_long s;
4542
4543 ahc = (struct ahc_softc *)arg;
4544
4545 ahc_lock(ahc, &s);
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);
4549 }
4550 ahc_unlock(ahc, &s);
4551}
4552
4553static void
4554ahc_linux_freeze_simq(struct ahc_softc *ahc)
4555{
4556 ahc->platform_data->qfrozen++;
4557 if (ahc->platform_data->qfrozen == 1) {
4558 scsi_block_requests(ahc->platform_data->host);
4559
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);
4564 }
4565}
4566
4567static void
4568ahc_linux_release_simq(u_long arg)
4569{
4570 struct ahc_softc *ahc;
4571 u_long s;
4572 int unblock_reqs;
4573
4574 ahc = (struct ahc_softc *)arg;
4575
4576 unblock_reqs = 0;
4577 ahc_lock(ahc, &s);
4578 if (ahc->platform_data->qfrozen > 0)
4579 ahc->platform_data->qfrozen--;
4580 if (ahc->platform_data->qfrozen == 0)
4581 unblock_reqs = 1;
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);
4586 }
4587 ahc_schedule_runq(ahc);
4588 ahc_unlock(ahc, &s);
4589 /*
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.
4594 */
4595 if (unblock_reqs)
4596 scsi_unblock_requests(ahc->platform_data->host);
4597}
4598
4599static void
4600ahc_linux_dev_timed_unfreeze(u_long arg)
4601{
4602 struct ahc_linux_device *dev;
4603 struct ahc_softc *ahc;
4604 u_long s;
4605
4606 dev = (struct ahc_linux_device *)arg;
4607 ahc = dev->target->ahc;
4608 ahc_lock(ahc, &s);
4609 dev->flags &= ~AHC_DEV_TIMER_ACTIVE;
4610 if (dev->qfrozen > 0)
4611 dev->qfrozen--;
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);
4619}
4620
4621static int
4622ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag)
4623{
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;
4629 u_long s;
4630 u_int saved_scbptr;
4631 u_int active_scb_index;
4632 u_int last_phase;
4633 u_int saved_scsiid;
4634 u_int cdb_byte;
4635 int retval;
4636 int was_paused;
4637 int paused;
4638 int wait;
4639 int disconnected;
4640
4641 pending_scb = NULL;
4642 paused = FALSE;
4643 wait = FALSE;
4644 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
4645 acmd = (struct ahc_cmd *)cmd;
4646
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");
4651
4652 printf("CDB:");
4653 for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
4654 printf(" 0x%x", cmd->cmnd[cdb_byte]);
4655 printf("\n");
4656
4657 /*
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.
4670 */
4671 ahc_midlayer_entrypoint_lock(ahc, &s);
4672
4673 /*
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.
4679 */
4680 dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
4681 cmd->device->lun, /*alloc*/FALSE);
4682
4683 if (dev == NULL) {
4684 /*
4685 * No target device for this command exists,
4686 * so we must not still own the command.
4687 */
4688 printf("%s:%d:%d:%d: Is not an active device\n",
4689 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4690 cmd->device->lun);
4691 retval = SUCCESS;
4692 goto no_cmd;
4693 }
4694
4695 TAILQ_FOREACH(list_acmd, &dev->busyq, acmd_links.tqe) {
4696 if (list_acmd == acmd)
4697 break;
4698 }
4699
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,
4703 cmd->device->lun);
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);
4708 retval = SUCCESS;
4709 goto done;
4710 }
4711 }
4712
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',
4716 cmd->device->lun,
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,
4720 cmd->device->lun);
4721 retval = SUCCESS;
4722 goto done;
4723 }
4724
4725 /*
4726 * See if we can find a matching cmd in the pending list.
4727 */
4728 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
4729 if (pending_scb->io_ctx == cmd)
4730 break;
4731 }
4732
4733 if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
4734
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',
4739 CAM_LUN_WILDCARD,
4740 SCB_LIST_NULL, ROLE_INITIATOR) == 0)
4741 break;
4742 }
4743 }
4744
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,
4748 cmd->device->lun);
4749 goto no_cmd;
4750 }
4751
4752 if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
4753 /*
4754 * We can't queue two recovery actions using the same SCB
4755 */
4756 retval = FAILED;
4757 goto done;
4758 }
4759
4760 /*
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.
4764 */
4765 was_paused = ahc_is_paused(ahc);
4766 ahc_pause_and_flushwork(ahc);
4767 paused = TRUE;
4768
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,
4772 cmd->device->lun);
4773 goto no_cmd;
4774 }
4775
4776 printf("%s: At time of recovery, card was %spaused\n",
4777 ahc_name(ahc), was_paused ? "" : "not ");
4778 ahc_dump_card_state(ahc);
4779
4780 disconnected = TRUE;
4781 if (flag == SCB_ABORT) {
4782 if (ahc_search_qinfifo(ahc, cmd->device->id,
4783 cmd->device->channel + 'A',
4784 cmd->device->lun,
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);
4791 retval = SUCCESS;
4792 goto done;
4793 }
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;
4800 }
4801
4802 if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
4803 struct scb *bus_scb;
4804
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;
4812 }
4813
4814 /*
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.
4821 */
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))) {
4830
4831 /*
4832 * We're active on the bus, so assert ATN
4833 * and hope that the target responds.
4834 */
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,
4841 cmd->device->lun);
4842 wait = TRUE;
4843 } else if (disconnected) {
4844
4845 /*
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.
4851 *
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.
4860 */
4861 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
4862 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
4863
4864 /*
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.
4870 */
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,
4875 /*remove*/TRUE,
4876 /*save_state*/FALSE);
4877
4878 /*
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.
4884 */
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);
4889 }
4890
4891 /*
4892 * Clear out any entries in the QINFIFO first
4893 * so we are the next SCB for this target
4894 * to run.
4895 */
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,
4900 SEARCH_COMPLETE);
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");
4905 wait = TRUE;
4906 } else {
4907 printf("%s:%d:%d:%d: Unable to deliver message\n",
4908 ahc_name(ahc), cmd->device->channel, cmd->device->id,
4909 cmd->device->lun);
4910 retval = FAILED;
4911 goto done;
4912 }
4913
4914no_cmd:
4915 /*
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.
4920 */
4921 retval = SUCCESS;
4922done:
4923 if (paused)
4924 ahc_unpause(ahc);
4925 if (wait) {
4926 struct timer_list timer;
4927 int ret;
4928
4929 ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE;
4930 spin_unlock_irq(&ahc->platform_data->spin_lock);
4931 init_timer(&timer);
4932 timer.data = (u_long)ahc;
4933 timer.expires = jiffies + (5 * HZ);
4934 timer.function = ahc_linux_sem_timeout;
4935 add_timer(&timer);
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);
4940 if (ret == 0) {
4941 printf("Timer Expired\n");
4942 retval = FAILED;
4943 }
4944 spin_lock_irq(&ahc->platform_data->spin_lock);
4945 }
4946 ahc_schedule_runq(ahc);
4947 ahc_linux_run_complete_queue(ahc);
4948 ahc_midlayer_entrypoint_unlock(ahc, &s);
4949 return (retval);
4950}
4951
4952void
4953ahc_platform_dump_card_state(struct ahc_softc *ahc)
4954{
4955 struct ahc_linux_device *dev;
4956 int channel;
4957 int maxchannel;
4958 int target;
4959 int maxtarget;
4960 int lun;
4961 int i;
4962
4963 maxchannel = (ahc->features & AHC_TWIN) ? 1 : 0;
4964 maxtarget = (ahc->features & AHC_WIDE) ? 15 : 7;
4965 for (channel = 0; channel <= maxchannel; channel++) {
4966
4967 for (target = 0; target <=maxtarget; target++) {
4968
4969 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
4970 struct ahc_cmd *acmd;
4971
4972 dev = ahc_linux_get_device(ahc, channel, target,
4973 lun, /*alloc*/FALSE);
4974 if (dev == NULL)
4975 continue;
4976
4977 printf("DevQ(%d:%d:%d): ",
4978 channel, target, lun);
4979 i = 0;
4980 TAILQ_FOREACH(acmd, &dev->busyq,
4981 acmd_links.tqe) {
4982 if (i++ > AHC_SCB_MAX)
4983 break;
4984 }
4985 printf("%d waiting\n", i);
4986 }
4987 }
4988 }
4989}
4990
4991static void ahc_linux_exit(void);
4992
4993static int __init
4994ahc_linux_init(void)
4995{
4996#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
4997 int rc = ahc_linux_detect(&aic7xxx_driver_template);
4998 if (rc)
4999 return rc;
5000 ahc_linux_exit();
5001 return -ENODEV;
5002#else
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);
5007 return (-ENODEV);
5008 }
5009
5010 return (0);
5011#endif
5012}
5013
5014static void
5015ahc_linux_exit(void)
5016{
5017 struct ahc_softc *ahc;
5018
5019 /*
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
5023 * to deadlock.
5024 */
5025 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
5026
5027 ahc_linux_kill_dv_thread(ahc);
5028 }
5029
5030#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
5031 /*
5032 * In 2.4 we have to unregister from the PCI core _after_
5033 * unregistering from the scsi midlayer to avoid dangling
5034 * references.
5035 */
5036 scsi_unregister_module(MODULE_SCSI_HA, &aic7xxx_driver_template);
5037#endif
5038 ahc_linux_pci_exit();
5039 ahc_linux_eisa_exit();
5040}
5041
5042module_init(ahc_linux_init);
5043module_exit(ahc_linux_exit);