1 /*
2 * Adaptec AIC79xx device driver for Linux.
3 *
4 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic79xx_osm.c#169 $
5 *
6 * --------------------------------------------------------------------------
7 * Copyright (c) 1994-2000 Justin T. Gibbs.
8 * Copyright (c) 1997-1999 Doug Ledford
9 * Copyright (c) 2000-2003 Adaptec Inc.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45 #include "aic79xx_osm.h"
46 #include "aic79xx_inline.h"
47 #include <scsi/scsicam.h>
48
49 /*
50 * Include aiclib.c as part of our
51 * "module dependencies are hard" work around.
52 */
53 #include "aiclib.c"
54
55 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
56 #include <linux/init.h> /* __setup */
57 #endif
58
59 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
60 #include "sd.h" /* For geometry detection */
61 #endif
62
63 #include <linux/mm.h> /* For fetching system memory size */
64
65 /*
66 * Lock protecting manipulation of the ahd softc list.
67 */
68 spinlock_t ahd_list_spinlock;
69
70 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
71 struct proc_dir_entry proc_scsi_aic79xx = {
72 PROC_SCSI_AIC79XX, 7, "aic79xx",
73 S_IFDIR | S_IRUGO | S_IXUGO, 2,
74 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL
75 };
76 #endif
77
78 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
79 /* For dynamic sglist size calculation. */
80 u_int ahd_linux_nseg;
81 #endif
82
83 /*
84 * Bucket size for counting good commands in between bad ones.
85 */
86 #define AHD_LINUX_ERR_THRESH 1000
87
88 /*
89 * Set this to the delay in seconds after SCSI bus reset.
90 * Note, we honor this only for the initial bus reset.
91 * The scsi error recovery code performs its own bus settle
92 * delay handling for error recovery actions.
93 */
94 #ifdef CONFIG_AIC79XX_RESET_DELAY_MS
95 #define AIC79XX_RESET_DELAY CONFIG_AIC79XX_RESET_DELAY_MS
96 #else
97 #define AIC79XX_RESET_DELAY 5000
98 #endif
99
100 /*
101 * To change the default number of tagged transactions allowed per-device,
102 * add a line to the lilo.conf file like:
103 * append="aic79xx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
104 * which will result in the first four devices on the first two
105 * controllers being set to a tagged queue depth of 32.
106 *
107 * The tag_commands is an array of 16 to allow for wide and twin adapters.
108 * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
109 * for channel 1.
110 */
111 typedef struct {
112 uint16_t tag_commands[16]; /* Allow for wide/twin adapters. */
113 } adapter_tag_info_t;
114
115 /*
116 * Modify this as you see fit for your system.
117 *
118 * 0 tagged queuing disabled
119 * 1 <= n <= 253 n == max tags ever dispatched.
120 *
121 * The driver will throttle the number of commands dispatched to a
122 * device if it returns queue full. For devices with a fixed maximum
123 * queue depth, the driver will eventually determine this depth and
124 * lock it in (a console message is printed to indicate that a lock
125 * has occurred). On some devices, queue full is returned for a temporary
126 * resource shortage. These devices will return queue full at varying
127 * depths. The driver will throttle back when the queue fulls occur and
128 * attempt to slowly increase the depth over time as the device recovers
129 * from the resource shortage.
130 *
131 * In this example, the first line will disable tagged queueing for all
132 * the devices on the first probed aic79xx adapter.
133 *
134 * The second line enables tagged queueing with 4 commands/LUN for IDs
135 * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
136 * driver to attempt to use up to 64 tags for ID 1.
137 *
138 * The third line is the same as the first line.
139 *
140 * The fourth line disables tagged queueing for devices 0 and 3. It
141 * enables tagged queueing for the other IDs, with 16 commands/LUN
142 * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
143 * IDs 2, 5-7, and 9-15.
144 */
145
146 /*
147 * NOTE: The below structure is for reference only, the actual structure
148 * to modify in order to change things is just below this comment block.
149 adapter_tag_info_t aic79xx_tag_info[] =
150 {
151 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
152 {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
153 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
154 {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
155 };
156 */
157
158 #ifdef CONFIG_AIC79XX_CMDS_PER_DEVICE
159 #define AIC79XX_CMDS_PER_DEVICE CONFIG_AIC79XX_CMDS_PER_DEVICE
160 #else
161 #define AIC79XX_CMDS_PER_DEVICE AHD_MAX_QUEUE
162 #endif
163
164 #define AIC79XX_CONFIGED_TAG_COMMANDS { \
165 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
166 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
167 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
168 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
169 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
170 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
171 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
172 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE \
173 }
174
175 /*
176 * By default, use the number of commands specified by
177 * the users kernel configuration.
178 */
179 static adapter_tag_info_t aic79xx_tag_info[] =
180 {
181 {AIC79XX_CONFIGED_TAG_COMMANDS},
182 {AIC79XX_CONFIGED_TAG_COMMANDS},
183 {AIC79XX_CONFIGED_TAG_COMMANDS},
184 {AIC79XX_CONFIGED_TAG_COMMANDS},
185 {AIC79XX_CONFIGED_TAG_COMMANDS},
186 {AIC79XX_CONFIGED_TAG_COMMANDS},
187 {AIC79XX_CONFIGED_TAG_COMMANDS},
188 {AIC79XX_CONFIGED_TAG_COMMANDS},
189 {AIC79XX_CONFIGED_TAG_COMMANDS},
190 {AIC79XX_CONFIGED_TAG_COMMANDS},
191 {AIC79XX_CONFIGED_TAG_COMMANDS},
192 {AIC79XX_CONFIGED_TAG_COMMANDS},
193 {AIC79XX_CONFIGED_TAG_COMMANDS},
194 {AIC79XX_CONFIGED_TAG_COMMANDS},
195 {AIC79XX_CONFIGED_TAG_COMMANDS},
196 {AIC79XX_CONFIGED_TAG_COMMANDS}
197 };
198
199 /*
200 * By default, read streaming is disabled. In theory,
201 * read streaming should enhance performance, but early
202 * U320 drive firmware actually performs slower with
203 * read streaming enabled.
204 */
205 #ifdef CONFIG_AIC79XX_ENABLE_RD_STRM
206 #define AIC79XX_CONFIGED_RD_STRM 0xFFFF
207 #else
208 #define AIC79XX_CONFIGED_RD_STRM 0
209 #endif
210
211 static uint16_t aic79xx_rd_strm_info[] =
212 {
213 AIC79XX_CONFIGED_RD_STRM,
214 AIC79XX_CONFIGED_RD_STRM,
215 AIC79XX_CONFIGED_RD_STRM,
216 AIC79XX_CONFIGED_RD_STRM,
217 AIC79XX_CONFIGED_RD_STRM,
218 AIC79XX_CONFIGED_RD_STRM,
219 AIC79XX_CONFIGED_RD_STRM,
220 AIC79XX_CONFIGED_RD_STRM,
221 AIC79XX_CONFIGED_RD_STRM,
222 AIC79XX_CONFIGED_RD_STRM,
223 AIC79XX_CONFIGED_RD_STRM,
224 AIC79XX_CONFIGED_RD_STRM,
225 AIC79XX_CONFIGED_RD_STRM,
226 AIC79XX_CONFIGED_RD_STRM,
227 AIC79XX_CONFIGED_RD_STRM,
228 AIC79XX_CONFIGED_RD_STRM
229 };
230
231 /*
232 * DV option:
233 *
234 * positive value = DV Enabled
235 * zero = DV Disabled
236 * negative value = DV Default for adapter type/seeprom
237 */
238 #ifdef CONFIG_AIC79XX_DV_SETTING
239 #define AIC79XX_CONFIGED_DV CONFIG_AIC79XX_DV_SETTING
240 #else
241 #define AIC79XX_CONFIGED_DV -1
242 #endif
243
244 static int8_t aic79xx_dv_settings[] =
245 {
246 AIC79XX_CONFIGED_DV,
247 AIC79XX_CONFIGED_DV,
248 AIC79XX_CONFIGED_DV,
249 AIC79XX_CONFIGED_DV,
250 AIC79XX_CONFIGED_DV,
251 AIC79XX_CONFIGED_DV,
252 AIC79XX_CONFIGED_DV,
253 AIC79XX_CONFIGED_DV,
254 AIC79XX_CONFIGED_DV,
255 AIC79XX_CONFIGED_DV,
256 AIC79XX_CONFIGED_DV,
257 AIC79XX_CONFIGED_DV,
258 AIC79XX_CONFIGED_DV,
259 AIC79XX_CONFIGED_DV,
260 AIC79XX_CONFIGED_DV,
261 AIC79XX_CONFIGED_DV
262 };
263
264 /*
265 * The I/O cell on the chip is very configurable in respect to its analog
266 * characteristics. Set the defaults here; they can be overriden with
267 * the proper insmod parameters.
268 */
269 struct ahd_linux_iocell_opts
270 {
271 uint8_t precomp;
272 uint8_t slewrate;
273 uint8_t amplitude;
274 };
275 #define AIC79XX_DEFAULT_PRECOMP 0xFF
276 #define AIC79XX_DEFAULT_SLEWRATE 0xFF
277 #define AIC79XX_DEFAULT_AMPLITUDE 0xFF
278 #define AIC79XX_DEFAULT_IOOPTS \
279 { \
280 AIC79XX_DEFAULT_PRECOMP, \
281 AIC79XX_DEFAULT_SLEWRATE, \
282 AIC79XX_DEFAULT_AMPLITUDE \
283 }
284 #define AIC79XX_PRECOMP_INDEX 0
285 #define AIC79XX_SLEWRATE_INDEX 1
286 #define AIC79XX_AMPLITUDE_INDEX 2
287 static struct ahd_linux_iocell_opts aic79xx_iocell_info[] =
288 {
289 AIC79XX_DEFAULT_IOOPTS,
290 AIC79XX_DEFAULT_IOOPTS,
291 AIC79XX_DEFAULT_IOOPTS,
292 AIC79XX_DEFAULT_IOOPTS,
293 AIC79XX_DEFAULT_IOOPTS,
294 AIC79XX_DEFAULT_IOOPTS,
295 AIC79XX_DEFAULT_IOOPTS,
296 AIC79XX_DEFAULT_IOOPTS,
297 AIC79XX_DEFAULT_IOOPTS,
298 AIC79XX_DEFAULT_IOOPTS,
299 AIC79XX_DEFAULT_IOOPTS,
300 AIC79XX_DEFAULT_IOOPTS,
301 AIC79XX_DEFAULT_IOOPTS,
302 AIC79XX_DEFAULT_IOOPTS,
303 AIC79XX_DEFAULT_IOOPTS,
304 AIC79XX_DEFAULT_IOOPTS
305 };
306
307 /*
308 * There should be a specific return value for this in scsi.h, but
309 * it seems that most drivers ignore it.
310 */
311 #define DID_UNDERFLOW DID_ERROR
312
313 void
ahd_print_path(struct ahd_softc * ahd,struct scb * scb)314 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
315 {
316 printk("(scsi%d:%c:%d:%d): ",
317 ahd->platform_data->host->host_no,
318 scb != NULL ? SCB_GET_CHANNEL(ahd, scb) : 'X',
319 scb != NULL ? SCB_GET_TARGET(ahd, scb) : -1,
320 scb != NULL ? SCB_GET_LUN(scb) : -1);
321 }
322
323 /*
324 * XXX - these options apply unilaterally to _all_ adapters
325 * cards in the system. This should be fixed. Exceptions to this
326 * rule are noted in the comments.
327 */
328
329 /*
330 * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This
331 * has no effect on any later resets that might occur due to things like
332 * SCSI bus timeouts.
333 */
334 static uint32_t aic79xx_no_reset;
335
336 /*
337 * Certain PCI motherboards will scan PCI devices from highest to lowest,
338 * others scan from lowest to highest, and they tend to do all kinds of
339 * strange things when they come into contact with PCI bridge chips. The
340 * net result of all this is that the PCI card that is actually used to boot
341 * the machine is very hard to detect. Most motherboards go from lowest
342 * PCI slot number to highest, and the first SCSI controller found is the
343 * one you boot from. The only exceptions to this are when a controller
344 * has its BIOS disabled. So, we by default sort all of our SCSI controllers
345 * from lowest PCI slot number to highest PCI slot number. We also force
346 * all controllers with their BIOS disabled to the end of the list. This
347 * works on *almost* all computers. Where it doesn't work, we have this
348 * option. Setting this option to non-0 will reverse the order of the sort
349 * to highest first, then lowest, but will still leave cards with their BIOS
350 * disabled at the very end. That should fix everyone up unless there are
351 * really strange cirumstances.
352 */
353 static uint32_t aic79xx_reverse_scan;
354
355 /*
356 * Should we force EXTENDED translation on a controller.
357 * 0 == Use whatever is in the SEEPROM or default to off
358 * 1 == Use whatever is in the SEEPROM or default to on
359 */
360 static uint32_t aic79xx_extended;
361
362 /*
363 * PCI bus parity checking of the Adaptec controllers. This is somewhat
364 * dubious at best. To my knowledge, this option has never actually
365 * solved a PCI parity problem, but on certain machines with broken PCI
366 * chipset configurations, it can generate tons of false error messages.
367 * It's included in the driver for completeness.
368 * 0 = Shut off PCI parity check
369 * non-0 = Enable PCI parity check
370 *
371 * NOTE: you can't actually pass -1 on the lilo prompt. So, to set this
372 * variable to -1 you would actually want to simply pass the variable
373 * name without a number. That will invert the 0 which will result in
374 * -1.
375 */
376 static uint32_t aic79xx_pci_parity = ~0;
377
378 /*
379 * There are lots of broken chipsets in the world. Some of them will
380 * violate the PCI spec when we issue byte sized memory writes to our
381 * controller. I/O mapped register access, if allowed by the given
382 * platform, will work in almost all cases.
383 */
384 uint32_t aic79xx_allow_memio = ~0;
385
386 /*
387 * aic79xx_detect() has been run, so register all device arrivals
388 * immediately with the system rather than deferring to the sorted
389 * attachment performed by aic79xx_detect().
390 */
391 int aic79xx_detect_complete;
392
393 /*
394 * So that we can set how long each device is given as a selection timeout.
395 * The table of values goes like this:
396 * 0 - 256ms
397 * 1 - 128ms
398 * 2 - 64ms
399 * 3 - 32ms
400 * We default to 256ms because some older devices need a longer time
401 * to respond to initial selection.
402 */
403 static uint32_t aic79xx_seltime;
404
405 /*
406 * Certain devices do not perform any aging on commands. Should the
407 * device be saturated by commands in one portion of the disk, it is
408 * possible for transactions on far away sectors to never be serviced.
409 * To handle these devices, we can periodically send an ordered tag to
410 * force all outstanding transactions to be serviced prior to a new
411 * transaction.
412 */
413 uint32_t aic79xx_periodic_otag;
414
415 /*
416 * Module information and settable options.
417 */
418 #ifdef MODULE
419 static char *aic79xx = NULL;
420 /*
421 * Just in case someone uses commas to separate items on the insmod
422 * command line, we define a dummy buffer here to avoid having insmod
423 * write wild stuff into our code segment
424 */
425 static char dummy_buffer[60] = "Please don't trounce on me insmod!!\n";
426
427 MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
428 MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
429 #ifdef MODULE_LICENSE
430 MODULE_LICENSE("Dual BSD/GPL");
431 #endif
432 MODULE_PARM(aic79xx, "s");
433 MODULE_PARM_DESC(aic79xx,
434 "period delimited, options string.\n"
435 " verbose Enable verbose/diagnostic logging\n"
436 " allow_memio Allow device registers to be memory mapped\n"
437 " debug Bitmask of debug values to enable\n"
438 " no_reset Supress initial bus resets\n"
439 " extended Enable extended geometry on all controllers\n"
440 " periodic_otag Send an ordered tagged transaction\n"
441 " periodically to prevent tag starvation.\n"
442 " This may be required by some older disk\n"
443 " or drives/RAID arrays.\n"
444 " reverse_scan Sort PCI devices highest Bus/Slot to lowest\n"
445 " tag_info:<tag_str> Set per-target tag depth\n"
446 " global_tag_depth:<int> Global tag depth for all targets on all buses\n"
447 " rd_strm:<rd_strm_masks> Set per-target read streaming setting.\n"
448 " dv:<dv_settings> Set per-controller Domain Validation Setting.\n"
449 " slewrate:<slewrate_list>Set the signal slew rate (0-15).\n"
450 " precomp:<pcomp_list> Set the signal precompensation (0-7).\n"
451 " amplitude:<int> Set the signal amplitude (0-7).\n"
452 " seltime:<int> Selection Timeout:\n"
453 " (0/256ms,1/128ms,2/64ms,3/32ms)\n"
454 "\n"
455 " Sample /etc/modules.conf line:\n"
456 " Enable verbose logging\n"
457 " Set tag depth on Controller 2/Target 2 to 10 tags\n"
458 " Shorten the selection timeout to 128ms\n"
459 "\n"
460 " options aic79xx 'aic79xx=verbose.tag_info:{{}.{}.{..10}}.seltime:1'\n"
461 "\n"
462 " Sample /etc/modules.conf line:\n"
463 " Change Read Streaming for Controller's 2 and 3\n"
464 "\n"
465 " options aic79xx 'aic79xx=rd_strm:{..0xFFF0.0xC0F0}'");
466 #endif
467
468 static void ahd_linux_handle_scsi_status(struct ahd_softc *,
469 struct ahd_linux_device *,
470 struct scb *);
471 static void ahd_linux_queue_cmd_complete(struct ahd_softc *ahd,
472 Scsi_Cmnd *cmd);
473 static void ahd_linux_filter_inquiry(struct ahd_softc *ahd,
474 struct ahd_devinfo *devinfo);
475 static void ahd_linux_dev_timed_unfreeze(u_long arg);
476 static void ahd_linux_sem_timeout(u_long arg);
477 static void ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd);
478 static void ahd_linux_size_nseg(void);
479 static void ahd_linux_thread_run_complete_queue(struct ahd_softc *ahd);
480 static void ahd_linux_start_dv(struct ahd_softc *ahd);
481 static void ahd_linux_dv_timeout(struct scsi_cmnd *cmd);
482 static int ahd_linux_dv_thread(void *data);
483 static void ahd_linux_kill_dv_thread(struct ahd_softc *ahd);
484 static void ahd_linux_dv_target(struct ahd_softc *ahd, u_int target);
485 static void ahd_linux_dv_transition(struct ahd_softc *ahd,
486 struct scsi_cmnd *cmd,
487 struct ahd_devinfo *devinfo,
488 struct ahd_linux_target *targ);
489 static void ahd_linux_dv_fill_cmd(struct ahd_softc *ahd,
490 struct scsi_cmnd *cmd,
491 struct ahd_devinfo *devinfo);
492 static void ahd_linux_dv_inq(struct ahd_softc *ahd,
493 struct scsi_cmnd *cmd,
494 struct ahd_devinfo *devinfo,
495 struct ahd_linux_target *targ,
496 u_int request_length);
497 static void ahd_linux_dv_tur(struct ahd_softc *ahd,
498 struct scsi_cmnd *cmd,
499 struct ahd_devinfo *devinfo);
500 static void ahd_linux_dv_rebd(struct ahd_softc *ahd,
501 struct scsi_cmnd *cmd,
502 struct ahd_devinfo *devinfo,
503 struct ahd_linux_target *targ);
504 static void ahd_linux_dv_web(struct ahd_softc *ahd,
505 struct scsi_cmnd *cmd,
506 struct ahd_devinfo *devinfo,
507 struct ahd_linux_target *targ);
508 static void ahd_linux_dv_reb(struct ahd_softc *ahd,
509 struct scsi_cmnd *cmd,
510 struct ahd_devinfo *devinfo,
511 struct ahd_linux_target *targ);
512 static void ahd_linux_dv_su(struct ahd_softc *ahd,
513 struct scsi_cmnd *cmd,
514 struct ahd_devinfo *devinfo,
515 struct ahd_linux_target *targ);
516 static __inline int
517 ahd_linux_dv_fallback(struct ahd_softc *ahd,
518 struct ahd_devinfo *devinfo);
519 static int ahd_linux_fallback(struct ahd_softc *ahd,
520 struct ahd_devinfo *devinfo);
521 static __inline int ahd_linux_dv_fallback(struct ahd_softc *ahd,
522 struct ahd_devinfo *devinfo);
523 static void ahd_linux_dv_complete(Scsi_Cmnd *cmd);
524 static void ahd_linux_generate_dv_pattern(struct ahd_linux_target *targ);
525 static u_int ahd_linux_user_tagdepth(struct ahd_softc *ahd,
526 struct ahd_devinfo *devinfo);
527 static u_int ahd_linux_user_dv_setting(struct ahd_softc *ahd);
528 static void ahd_linux_setup_user_rd_strm_settings(struct ahd_softc *ahd);
529 static void ahd_linux_device_queue_depth(struct ahd_softc *ahd,
530 struct ahd_linux_device *dev);
531 static struct ahd_linux_target* ahd_linux_alloc_target(struct ahd_softc*,
532 u_int, u_int);
533 static void ahd_linux_free_target(struct ahd_softc*,
534 struct ahd_linux_target*);
535 static struct ahd_linux_device* ahd_linux_alloc_device(struct ahd_softc*,
536 struct ahd_linux_target*,
537 u_int);
538 static void ahd_linux_free_device(struct ahd_softc*,
539 struct ahd_linux_device*);
540 static void ahd_linux_run_device_queue(struct ahd_softc*,
541 struct ahd_linux_device*);
542 static void ahd_linux_setup_tag_info_global(char *p);
543 static aic_option_callback_t ahd_linux_setup_tag_info;
544 static aic_option_callback_t ahd_linux_setup_rd_strm_info;
545 static aic_option_callback_t ahd_linux_setup_dv;
546 static aic_option_callback_t ahd_linux_setup_iocell_info;
547 static int ahd_linux_next_unit(void);
548 static void ahd_runq_tasklet(unsigned long data);
549 static int aic79xx_setup(char *c);
550
551 /****************************** Inlines ***************************************/
552 static __inline void ahd_schedule_completeq(struct ahd_softc *ahd);
553 static __inline void ahd_schedule_runq(struct ahd_softc *ahd);
554 static __inline void ahd_setup_runq_tasklet(struct ahd_softc *ahd);
555 static __inline void ahd_teardown_runq_tasklet(struct ahd_softc *ahd);
556 static __inline struct ahd_linux_device*
557 ahd_linux_get_device(struct ahd_softc *ahd, u_int channel,
558 u_int target, u_int lun, int alloc);
559 static struct ahd_cmd *ahd_linux_run_complete_queue(struct ahd_softc *ahd);
560 static __inline void ahd_linux_check_device_queue(struct ahd_softc *ahd,
561 struct ahd_linux_device *dev);
562 static __inline struct ahd_linux_device *
563 ahd_linux_next_device_to_run(struct ahd_softc *ahd);
564 static __inline void ahd_linux_run_device_queues(struct ahd_softc *ahd);
565 static __inline void ahd_linux_unmap_scb(struct ahd_softc*, struct scb*);
566
567 static __inline int ahd_linux_map_seg(struct ahd_softc *ahd, struct scb *scb,
568 struct ahd_dma_seg *sg,
569 bus_addr_t addr, bus_size_t len);
570
571 static __inline void
ahd_schedule_completeq(struct ahd_softc * ahd)572 ahd_schedule_completeq(struct ahd_softc *ahd)
573 {
574 if ((ahd->platform_data->flags & AHD_RUN_CMPLT_Q_TIMER) == 0) {
575 ahd->platform_data->flags |= AHD_RUN_CMPLT_Q_TIMER;
576 ahd->platform_data->completeq_timer.expires = jiffies;
577 add_timer(&ahd->platform_data->completeq_timer);
578 }
579 }
580
581 /*
582 * Must be called with our lock held.
583 */
584 static __inline void
ahd_schedule_runq(struct ahd_softc * ahd)585 ahd_schedule_runq(struct ahd_softc *ahd)
586 {
587 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
588 tasklet_schedule(&ahd->platform_data->runq_tasklet);
589 #else
590 /*
591 * Tasklets are not available, so run inline.
592 */
593 ahd_runq_tasklet((unsigned long)ahd);
594 #endif
595 }
596
597 static __inline
ahd_setup_runq_tasklet(struct ahd_softc * ahd)598 void ahd_setup_runq_tasklet(struct ahd_softc *ahd)
599 {
600 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
601 tasklet_init(&ahd->platform_data->runq_tasklet, ahd_runq_tasklet,
602 (unsigned long)ahd);
603 #endif
604 }
605
606 static __inline void
ahd_teardown_runq_tasklet(struct ahd_softc * ahd)607 ahd_teardown_runq_tasklet(struct ahd_softc *ahd)
608 {
609 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
610 tasklet_kill(&ahd->platform_data->runq_tasklet);
611 #endif
612 }
613
614 static __inline struct ahd_linux_device*
ahd_linux_get_device(struct ahd_softc * ahd,u_int channel,u_int target,u_int lun,int alloc)615 ahd_linux_get_device(struct ahd_softc *ahd, u_int channel, u_int target,
616 u_int lun, int alloc)
617 {
618 struct ahd_linux_target *targ;
619 struct ahd_linux_device *dev;
620 u_int target_offset;
621
622 target_offset = target;
623 if (channel != 0)
624 target_offset += 8;
625 targ = ahd->platform_data->targets[target_offset];
626 if (targ == NULL) {
627 if (alloc != 0) {
628 targ = ahd_linux_alloc_target(ahd, channel, target);
629 if (targ == NULL)
630 return (NULL);
631 } else
632 return (NULL);
633 }
634 dev = targ->devices[lun];
635 if (dev == NULL && alloc != 0)
636 dev = ahd_linux_alloc_device(ahd, targ, lun);
637 return (dev);
638 }
639
640 #define AHD_LINUX_MAX_RETURNED_ERRORS 4
641 static struct ahd_cmd *
ahd_linux_run_complete_queue(struct ahd_softc * ahd)642 ahd_linux_run_complete_queue(struct ahd_softc *ahd)
643 {
644 struct ahd_cmd *acmd;
645 u_long done_flags;
646 int with_errors;
647
648 with_errors = 0;
649 ahd_done_lock(ahd, &done_flags);
650 while ((acmd = TAILQ_FIRST(&ahd->platform_data->completeq)) != NULL) {
651 Scsi_Cmnd *cmd;
652
653 if (with_errors > AHD_LINUX_MAX_RETURNED_ERRORS) {
654 /*
655 * Linux uses stack recursion to requeue
656 * commands that need to be retried. Avoid
657 * blowing out the stack by "spoon feeding"
658 * commands that completed with error back
659 * the operating system in case they are going
660 * to be retried. "ick"
661 */
662 ahd_schedule_completeq(ahd);
663 break;
664 }
665 TAILQ_REMOVE(&ahd->platform_data->completeq,
666 acmd, acmd_links.tqe);
667 cmd = &acmd_scsi_cmd(acmd);
668 cmd->host_scribble = NULL;
669 if (ahd_cmd_get_transaction_status(cmd) != DID_OK
670 || (cmd->result & 0xFF) != SCSI_STATUS_OK)
671 with_errors++;
672
673 cmd->scsi_done(cmd);
674 }
675 ahd_done_unlock(ahd, &done_flags);
676 return (acmd);
677 }
678
679 static __inline void
ahd_linux_check_device_queue(struct ahd_softc * ahd,struct ahd_linux_device * dev)680 ahd_linux_check_device_queue(struct ahd_softc *ahd,
681 struct ahd_linux_device *dev)
682 {
683 if ((dev->flags & AHD_DEV_FREEZE_TIL_EMPTY) != 0
684 && dev->active == 0) {
685 dev->flags &= ~AHD_DEV_FREEZE_TIL_EMPTY;
686 dev->qfrozen--;
687 }
688
689 if (TAILQ_FIRST(&dev->busyq) == NULL
690 || dev->openings == 0 || dev->qfrozen != 0)
691 return;
692
693 ahd_linux_run_device_queue(ahd, dev);
694 }
695
696 static __inline struct ahd_linux_device *
ahd_linux_next_device_to_run(struct ahd_softc * ahd)697 ahd_linux_next_device_to_run(struct ahd_softc *ahd)
698 {
699
700 if ((ahd->flags & AHD_RESOURCE_SHORTAGE) != 0
701 || (ahd->platform_data->qfrozen != 0
702 && AHD_DV_SIMQ_FROZEN(ahd) == 0))
703 return (NULL);
704 return (TAILQ_FIRST(&ahd->platform_data->device_runq));
705 }
706
707 static __inline void
ahd_linux_run_device_queues(struct ahd_softc * ahd)708 ahd_linux_run_device_queues(struct ahd_softc *ahd)
709 {
710 struct ahd_linux_device *dev;
711
712 while ((dev = ahd_linux_next_device_to_run(ahd)) != NULL) {
713 TAILQ_REMOVE(&ahd->platform_data->device_runq, dev, links);
714 dev->flags &= ~AHD_DEV_ON_RUN_LIST;
715 ahd_linux_check_device_queue(ahd, dev);
716 }
717 }
718
719 static __inline void
ahd_linux_unmap_scb(struct ahd_softc * ahd,struct scb * scb)720 ahd_linux_unmap_scb(struct ahd_softc *ahd, struct scb *scb)
721 {
722 Scsi_Cmnd *cmd;
723 int direction;
724
725 cmd = scb->io_ctx;
726 direction = scsi_to_pci_dma_dir(cmd->sc_data_direction);
727 ahd_sync_sglist(ahd, scb, BUS_DMASYNC_POSTWRITE);
728 if (cmd->use_sg != 0) {
729 struct scatterlist *sg;
730
731 sg = (struct scatterlist *)cmd->request_buffer;
732 pci_unmap_sg(ahd->dev_softc, sg, cmd->use_sg, direction);
733 } else if (cmd->request_bufflen != 0) {
734 pci_unmap_single(ahd->dev_softc,
735 scb->platform_data->buf_busaddr,
736 cmd->request_bufflen, direction);
737 }
738 }
739
740 static __inline int
ahd_linux_map_seg(struct ahd_softc * ahd,struct scb * scb,struct ahd_dma_seg * sg,bus_addr_t addr,bus_size_t len)741 ahd_linux_map_seg(struct ahd_softc *ahd, struct scb *scb,
742 struct ahd_dma_seg *sg, bus_addr_t addr, bus_size_t len)
743 {
744 int consumed;
745
746 if ((scb->sg_count + 1) > AHD_NSEG)
747 panic("Too few segs for dma mapping. "
748 "Increase AHD_NSEG\n");
749
750 consumed = 1;
751 sg->addr = ahd_htole32(addr & 0xFFFFFFFF);
752 scb->platform_data->xfer_len += len;
753
754 if (sizeof(bus_addr_t) > 4
755 && (ahd->flags & AHD_39BIT_ADDRESSING) != 0)
756 len |= (addr >> 8) & AHD_SG_HIGH_ADDR_MASK;
757
758 sg->len = ahd_htole32(len);
759 return (consumed);
760 }
761
762 /******************************** Macros **************************************/
763 #define BUILD_SCSIID(ahd, cmd) \
764 ((((cmd)->device->id << TID_SHIFT) & TID) | (ahd)->our_id)
765
766 /************************ Host template entry points *************************/
767 static int ahd_linux_detect(Scsi_Host_Template *);
768 static const char *ahd_linux_info(struct Scsi_Host *);
769 static int ahd_linux_queue(Scsi_Cmnd *, void (*)(Scsi_Cmnd *));
770 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
771 static int ahd_linux_slave_alloc(Scsi_Device *);
772 static int ahd_linux_slave_configure(Scsi_Device *);
773 static void ahd_linux_slave_destroy(Scsi_Device *);
774 #if defined(__i386__)
775 static int ahd_linux_biosparam(struct scsi_device*,
776 struct block_device*, sector_t, int[]);
777 #endif
778 #else
779 static int ahd_linux_release(struct Scsi_Host *);
780 static void ahd_linux_select_queue_depth(struct Scsi_Host *host,
781 Scsi_Device *scsi_devs);
782 #if defined(__i386__)
783 static int ahd_linux_biosparam(Disk *, kdev_t, int[]);
784 #endif
785 #endif
786 static int ahd_linux_bus_reset(Scsi_Cmnd *);
787 static int ahd_linux_dev_reset(Scsi_Cmnd *);
788 static int ahd_linux_abort(Scsi_Cmnd *);
789
790 /*
791 * Calculate a safe value for AHD_NSEG (as expressed through ahd_linux_nseg).
792 *
793 * In pre-2.5.X...
794 * The midlayer allocates an S/G array dynamically when a command is issued
795 * using SCSI malloc. This array, which is in an OS dependent format that
796 * must later be copied to our private S/G list, is sized to house just the
797 * number of segments needed for the current transfer. Since the code that
798 * sizes the SCSI malloc pool does not take into consideration fragmentation
799 * of the pool, executing transactions numbering just a fraction of our
800 * concurrent transaction limit with SG list lengths aproaching AHC_NSEG will
801 * quickly depleat the SCSI malloc pool of usable space. Unfortunately, the
802 * mid-layer does not properly handle this scsi malloc failures for the S/G
803 * array and the result can be a lockup of the I/O subsystem. We try to size
804 * our S/G list so that it satisfies our drivers allocation requirements in
805 * addition to avoiding fragmentation of the SCSI malloc pool.
806 */
807 static void
ahd_linux_size_nseg(void)808 ahd_linux_size_nseg(void)
809 {
810 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
811 u_int cur_size;
812 u_int best_size;
813
814 /*
815 * The SCSI allocator rounds to the nearest 512 bytes
816 * an cannot allocate across a page boundary. Our algorithm
817 * is to start at 1K of scsi malloc space per-command and
818 * loop through all factors of the PAGE_SIZE and pick the best.
819 */
820 best_size = 0;
821 for (cur_size = 1024; cur_size <= PAGE_SIZE; cur_size *= 2) {
822 u_int nseg;
823
824 nseg = cur_size / sizeof(struct scatterlist);
825 if (nseg < AHD_LINUX_MIN_NSEG)
826 continue;
827
828 if (best_size == 0) {
829 best_size = cur_size;
830 ahd_linux_nseg = nseg;
831 } else {
832 u_int best_rem;
833 u_int cur_rem;
834
835 /*
836 * Compare the traits of the current "best_size"
837 * with the current size to determine if the
838 * current size is a better size.
839 */
840 best_rem = best_size % sizeof(struct scatterlist);
841 cur_rem = cur_size % sizeof(struct scatterlist);
842 if (cur_rem < best_rem) {
843 best_size = cur_size;
844 ahd_linux_nseg = nseg;
845 }
846 }
847 }
848 #endif
849 }
850
851 /*
852 * Try to detect an Adaptec 79XX controller.
853 */
854 static int
ahd_linux_detect(Scsi_Host_Template * template)855 ahd_linux_detect(Scsi_Host_Template *template)
856 {
857 struct ahd_softc *ahd;
858 int found;
859
860 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
861 /*
862 * It is a bug that the upper layer takes
863 * this lock just prior to calling us.
864 */
865 spin_unlock_irq(&io_request_lock);
866 #endif
867
868 /*
869 * Sanity checking of Linux SCSI data structures so
870 * that some of our hacks^H^H^H^H^Hassumptions aren't
871 * violated.
872 */
873 if (offsetof(struct ahd_cmd_internal, end)
874 > offsetof(struct scsi_cmnd, host_scribble)) {
875 printf("ahd_linux_detect: SCSI data structures changed.\n");
876 printf("ahd_linux_detect: Unable to attach\n");
877 return (0);
878 }
879 /*
880 * Determine an appropriate size for our Scatter Gatther lists.
881 */
882 ahd_linux_size_nseg();
883 #ifdef MODULE
884 /*
885 * If we've been passed any parameters, process them now.
886 */
887 if (aic79xx)
888 aic79xx_setup(aic79xx);
889 if (dummy_buffer[0] != 'P')
890 printk(KERN_WARNING
891 "aic79xx: Please read the file /usr/src/linux/drivers/scsi/README.aic79xx\n"
892 "aic79xx: to see the proper way to specify options to the aic79xx module\n"
893 "aic79xx: Specifically, don't use any commas when passing arguments to\n"
894 "aic79xx: insmod or else it might trash certain memory areas.\n");
895 #endif
896
897 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,0)
898 template->proc_name = "aic79xx";
899 #else
900 template->proc_dir = &proc_scsi_aic79xx;
901 #endif
902
903 /*
904 * Initialize our softc list lock prior to
905 * probing for any adapters.
906 */
907 ahd_list_lockinit();
908
909 #ifdef CONFIG_PCI
910 ahd_linux_pci_init();
911 #endif
912
913 /*
914 * Register with the SCSI layer all
915 * controllers we've found.
916 */
917 found = 0;
918 TAILQ_FOREACH(ahd, &ahd_tailq, links) {
919
920 if (ahd_linux_register_host(ahd, template) == 0)
921 found++;
922 }
923 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
924 spin_lock_irq(&io_request_lock);
925 #endif
926 aic79xx_detect_complete++;
927 return (found);
928 }
929
930 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
931 /*
932 * Free the passed in Scsi_Host memory structures prior to unloading the
933 * module.
934 */
935 static int
ahd_linux_release(struct Scsi_Host * host)936 ahd_linux_release(struct Scsi_Host * host)
937 {
938 struct ahd_softc *ahd;
939 u_long l;
940
941 ahd_list_lock(&l);
942 if (host != NULL) {
943
944 /*
945 * We should be able to just perform
946 * the free directly, but check our
947 * list for extra sanity.
948 */
949 ahd = ahd_find_softc(*(struct ahd_softc **)host->hostdata);
950 if (ahd != NULL) {
951 u_long s;
952
953 ahd_lock(ahd, &s);
954 ahd_intr_enable(ahd, FALSE);
955 ahd_unlock(ahd, &s);
956 ahd_free(ahd);
957 }
958 }
959 ahd_list_unlock(&l);
960 return (0);
961 }
962 #endif
963
964 /*
965 * Return a string describing the driver.
966 */
967 static const char *
ahd_linux_info(struct Scsi_Host * host)968 ahd_linux_info(struct Scsi_Host *host)
969 {
970 static char buffer[512];
971 char ahd_info[256];
972 char *bp;
973 struct ahd_softc *ahd;
974
975 bp = &buffer[0];
976 ahd = *(struct ahd_softc **)host->hostdata;
977 memset(bp, 0, sizeof(buffer));
978 strcpy(bp, "Adaptec AIC79XX PCI-X SCSI HBA DRIVER, Rev ");
979 strcat(bp, AIC79XX_DRIVER_VERSION);
980 strcat(bp, "\n");
981 strcat(bp, " <");
982 strcat(bp, ahd->description);
983 strcat(bp, ">\n");
984 strcat(bp, " ");
985 ahd_controller_info(ahd, ahd_info);
986 strcat(bp, ahd_info);
987 strcat(bp, "\n");
988
989 return (bp);
990 }
991
992 /*
993 * Queue an SCB to the controller.
994 */
995 static int
ahd_linux_queue(Scsi_Cmnd * cmd,void (* scsi_done)(Scsi_Cmnd *))996 ahd_linux_queue(Scsi_Cmnd * cmd, void (*scsi_done) (Scsi_Cmnd *))
997 {
998 struct ahd_softc *ahd;
999 struct ahd_linux_device *dev;
1000 u_long flags;
1001
1002 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
1003
1004 /*
1005 * Save the callback on completion function.
1006 */
1007 cmd->scsi_done = scsi_done;
1008
1009 ahd_midlayer_entrypoint_lock(ahd, &flags);
1010
1011 /*
1012 * Close the race of a command that was in the process of
1013 * being queued to us just as our simq was frozen. Let
1014 * DV commands through so long as we are only frozen to
1015 * perform DV.
1016 */
1017 if (ahd->platform_data->qfrozen != 0
1018 && AHD_DV_CMD(cmd) == 0) {
1019
1020 ahd_cmd_set_transaction_status(cmd, CAM_REQUEUE_REQ);
1021 ahd_linux_queue_cmd_complete(ahd, cmd);
1022 ahd_schedule_completeq(ahd);
1023 ahd_midlayer_entrypoint_unlock(ahd, &flags);
1024 return (0);
1025 }
1026 dev = ahd_linux_get_device(ahd, cmd->device->channel,
1027 cmd->device->id, cmd->device->lun,
1028 /*alloc*/TRUE);
1029 if (dev == NULL) {
1030 ahd_cmd_set_transaction_status(cmd, CAM_RESRC_UNAVAIL);
1031 ahd_linux_queue_cmd_complete(ahd, cmd);
1032 ahd_schedule_completeq(ahd);
1033 ahd_midlayer_entrypoint_unlock(ahd, &flags);
1034 printf("%s: aic79xx_linux_queue - Unable to allocate device!\n",
1035 ahd_name(ahd));
1036 return (0);
1037 }
1038 if (cmd->cmd_len > MAX_CDB_LEN)
1039 return (-EINVAL);
1040 cmd->result = CAM_REQ_INPROG << 16;
1041 TAILQ_INSERT_TAIL(&dev->busyq, (struct ahd_cmd *)cmd, acmd_links.tqe);
1042 if ((dev->flags & AHD_DEV_ON_RUN_LIST) == 0) {
1043 TAILQ_INSERT_TAIL(&ahd->platform_data->device_runq, dev, links);
1044 dev->flags |= AHD_DEV_ON_RUN_LIST;
1045 ahd_linux_run_device_queues(ahd);
1046 }
1047 ahd_midlayer_entrypoint_unlock(ahd, &flags);
1048 return (0);
1049 }
1050
1051 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1052 static int
ahd_linux_slave_alloc(Scsi_Device * device)1053 ahd_linux_slave_alloc(Scsi_Device *device)
1054 {
1055 struct ahd_softc *ahd;
1056
1057 ahd = *((struct ahd_softc **)device->host->hostdata);
1058 if (bootverbose)
1059 printf("%s: Slave Alloc %d\n", ahd_name(ahd), device->id);
1060 return (0);
1061 }
1062
1063 static int
ahd_linux_slave_configure(Scsi_Device * device)1064 ahd_linux_slave_configure(Scsi_Device *device)
1065 {
1066 struct ahd_softc *ahd;
1067 struct ahd_linux_device *dev;
1068 u_long flags;
1069
1070 ahd = *((struct ahd_softc **)device->host->hostdata);
1071 if (bootverbose)
1072 printf("%s: Slave Configure %d\n", ahd_name(ahd), device->id);
1073 ahd_midlayer_entrypoint_lock(ahd, &flags);
1074 /*
1075 * Since Linux has attached to the device, configure
1076 * it so we don't free and allocate the device
1077 * structure on every command.
1078 */
1079 dev = ahd_linux_get_device(ahd, device->channel,
1080 device->id, device->lun,
1081 /*alloc*/TRUE);
1082 if (dev != NULL) {
1083 dev->flags &= ~AHD_DEV_UNCONFIGURED;
1084 dev->flags |= AHD_DEV_SLAVE_CONFIGURED;
1085 dev->scsi_device = device;
1086 ahd_linux_device_queue_depth(ahd, dev);
1087 }
1088 ahd_midlayer_entrypoint_unlock(ahd, &flags);
1089 return (0);
1090 }
1091
1092 static void
ahd_linux_slave_destroy(Scsi_Device * device)1093 ahd_linux_slave_destroy(Scsi_Device *device)
1094 {
1095 struct ahd_softc *ahd;
1096 struct ahd_linux_device *dev;
1097 u_long flags;
1098
1099 ahd = *((struct ahd_softc **)device->host->hostdata);
1100 if (bootverbose)
1101 printf("%s: Slave Destroy %d\n", ahd_name(ahd), device->id);
1102 ahd_midlayer_entrypoint_lock(ahd, &flags);
1103 dev = ahd_linux_get_device(ahd, device->channel,
1104 device->id, device->lun,
1105 /*alloc*/FALSE);
1106
1107 /*
1108 * Filter out "silly" deletions of real devices by only
1109 * deleting devices that have had slave_configure()
1110 * called on them. All other devices that have not
1111 * been configured will automatically be deleted by
1112 * the refcounting process.
1113 */
1114 if (dev != NULL
1115 && (dev->flags & AHD_DEV_SLAVE_CONFIGURED) != 0) {
1116 dev->flags |= AHD_DEV_UNCONFIGURED;
1117 if (TAILQ_EMPTY(&dev->busyq)
1118 && dev->active == 0
1119 && (dev->flags & AHD_DEV_TIMER_ACTIVE) == 0)
1120 ahd_linux_free_device(ahd, dev);
1121 }
1122 ahd_midlayer_entrypoint_unlock(ahd, &flags);
1123 }
1124 #else
1125 /*
1126 * Sets the queue depth for each SCSI device hanging
1127 * off the input host adapter.
1128 */
1129 static void
ahd_linux_select_queue_depth(struct Scsi_Host * host,Scsi_Device * scsi_devs)1130 ahd_linux_select_queue_depth(struct Scsi_Host * host,
1131 Scsi_Device * scsi_devs)
1132 {
1133 Scsi_Device *device;
1134 Scsi_Device *ldev;
1135 struct ahd_softc *ahd;
1136 u_long flags;
1137
1138 ahd = *((struct ahd_softc **)host->hostdata);
1139 ahd_lock(ahd, &flags);
1140 for (device = scsi_devs; device != NULL; device = device->next) {
1141
1142 /*
1143 * Watch out for duplicate devices. This works around
1144 * some quirks in how the SCSI scanning code does its
1145 * device management.
1146 */
1147 for (ldev = scsi_devs; ldev != device; ldev = ldev->next) {
1148 if (ldev->host == device->host
1149 && ldev->channel == device->channel
1150 && ldev->id == device->id
1151 && ldev->lun == device->lun)
1152 break;
1153 }
1154 /* Skip duplicate. */
1155 if (ldev != device)
1156 continue;
1157
1158 if (device->host == host) {
1159 struct ahd_linux_device *dev;
1160
1161 /*
1162 * Since Linux has attached to the device, configure
1163 * it so we don't free and allocate the device
1164 * structure on every command.
1165 */
1166 dev = ahd_linux_get_device(ahd, device->channel,
1167 device->id, device->lun,
1168 /*alloc*/TRUE);
1169 if (dev != NULL) {
1170 dev->flags &= ~AHD_DEV_UNCONFIGURED;
1171 dev->scsi_device = device;
1172 ahd_linux_device_queue_depth(ahd, dev);
1173 device->queue_depth = dev->openings
1174 + dev->active;
1175 if ((dev->flags & (AHD_DEV_Q_BASIC
1176 | AHD_DEV_Q_TAGGED)) == 0) {
1177 /*
1178 * We allow the OS to queue 2 untagged
1179 * transactions to us at any time even
1180 * though we can only execute them
1181 * serially on the controller/device.
1182 * This should remove some latency.
1183 */
1184 device->queue_depth = 2;
1185 }
1186 }
1187 }
1188 }
1189 ahd_unlock(ahd, &flags);
1190 }
1191 #endif
1192
1193 #if defined(__i386__)
1194 /*
1195 * Return the disk geometry for the given SCSI device.
1196 */
1197 static int
1198 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
ahd_linux_biosparam(struct scsi_device * sdev,struct block_device * bdev,sector_t capacity,int geom[])1199 ahd_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1200 sector_t capacity, int geom[])
1201 {
1202 uint8_t *bh;
1203 #else
1204 ahd_linux_biosparam(Disk *disk, kdev_t dev, int geom[])
1205 {
1206 struct scsi_device *sdev = disk->device;
1207 u_long capacity = disk->capacity;
1208 struct buffer_head *bh;
1209 #endif
1210 int heads;
1211 int sectors;
1212 int cylinders;
1213 int ret;
1214 int extended;
1215 struct ahd_softc *ahd;
1216
1217 ahd = *((struct ahd_softc **)sdev->host->hostdata);
1218
1219 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1220 bh = scsi_bios_ptable(bdev);
1221 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,17)
1222 bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, block_size(dev));
1223 #else
1224 bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, 1024);
1225 #endif
1226
1227 if (bh) {
1228 ret = scsi_partsize(bh, capacity,
1229 &geom[2], &geom[0], &geom[1]);
1230 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1231 kfree(bh);
1232 #else
1233 brelse(bh);
1234 #endif
1235 if (ret != -1)
1236 return (ret);
1237 }
1238 heads = 64;
1239 sectors = 32;
1240 cylinders = aic_sector_div(capacity, heads, sectors);
1241
1242 if (aic79xx_extended != 0)
1243 extended = 1;
1244 else
1245 extended = (ahd->flags & AHD_EXTENDED_TRANS_A) != 0;
1246 if (extended && cylinders >= 1024) {
1247 heads = 255;
1248 sectors = 63;
1249 cylinders = aic_sector_div(capacity, heads, sectors);
1250 }
1251 geom[0] = heads;
1252 geom[1] = sectors;
1253 geom[2] = cylinders;
1254 return (0);
1255 }
1256 #endif
1257
1258 /*
1259 * Abort the current SCSI command(s).
1260 */
1261 static int
1262 ahd_linux_abort(Scsi_Cmnd *cmd)
1263 {
1264 struct ahd_softc *ahd;
1265 struct ahd_cmd *acmd;
1266 struct ahd_cmd *list_acmd;
1267 struct ahd_linux_device *dev;
1268 struct scb *pending_scb;
1269 u_long s;
1270 u_int saved_scbptr;
1271 u_int active_scbptr;
1272 u_int last_phase;
1273 u_int cdb_byte;
1274 int retval;
1275 int was_paused;
1276 int paused;
1277 int wait;
1278 int disconnected;
1279 ahd_mode_state saved_modes;
1280
1281 pending_scb = NULL;
1282 paused = FALSE;
1283 wait = FALSE;
1284 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
1285 acmd = (struct ahd_cmd *)cmd;
1286
1287 printf("%s:%d:%d:%d: Attempting to abort cmd %p:",
1288 ahd_name(ahd), cmd->device->channel, cmd->device->id,
1289 cmd->device->lun, cmd);
1290 for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
1291 printf(" 0x%x", cmd->cmnd[cdb_byte]);
1292 printf("\n");
1293
1294 /*
1295 * In all versions of Linux, we have to work around
1296 * a major flaw in how the mid-layer is locked down
1297 * if we are to sleep successfully in our error handler
1298 * while allowing our interrupt handler to run. Since
1299 * the midlayer acquires either the io_request_lock or
1300 * our lock prior to calling us, we must use the
1301 * spin_unlock_irq() method for unlocking our lock.
1302 * This will force interrupts to be enabled on the
1303 * current CPU. Since the EH thread should not have
1304 * been running with CPU interrupts disabled other than
1305 * by acquiring either the io_request_lock or our own
1306 * lock, this *should* be safe.
1307 */
1308 ahd_midlayer_entrypoint_lock(ahd, &s);
1309
1310 /*
1311 * First determine if we currently own this command.
1312 * Start by searching the device queue. If not found
1313 * there, check the pending_scb list. If not found
1314 * at all, and the system wanted us to just abort the
1315 * command, return success.
1316 */
1317 dev = ahd_linux_get_device(ahd, cmd->device->channel,
1318 cmd->device->id, cmd->device->lun,
1319 /*alloc*/FALSE);
1320
1321 if (dev == NULL) {
1322 /*
1323 * No target device for this command exists,
1324 * so we must not still own the command.
1325 */
1326 printf("%s:%d:%d:%d: Is not an active device\n",
1327 ahd_name(ahd), cmd->device->channel, cmd->device->id,
1328 cmd->device->lun);
1329 retval = SUCCESS;
1330 goto no_cmd;
1331 }
1332
1333 TAILQ_FOREACH(list_acmd, &dev->busyq, acmd_links.tqe) {
1334 if (list_acmd == acmd)
1335 break;
1336 }
1337
1338 if (list_acmd != NULL) {
1339 printf("%s:%d:%d:%d: Command found on device queue\n",
1340 ahd_name(ahd), cmd->device->channel, cmd->device->id,
1341 cmd->device->lun);
1342 TAILQ_REMOVE(&dev->busyq, list_acmd, acmd_links.tqe);
1343 cmd->result = DID_ABORT << 16;
1344 ahd_linux_queue_cmd_complete(ahd, cmd);
1345 retval = SUCCESS;
1346 goto done;
1347 }
1348
1349 /*
1350 * See if we can find a matching cmd in the pending list.
1351 */
1352 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
1353 if (pending_scb->io_ctx == cmd)
1354 break;
1355 }
1356
1357 if (pending_scb == NULL) {
1358 printf("%s:%d:%d:%d: Command not found\n",
1359 ahd_name(ahd), cmd->device->channel, cmd->device->id,
1360 cmd->device->lun);
1361 goto no_cmd;
1362 }
1363
1364 if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
1365 /*
1366 * We can't queue two recovery actions using the same SCB
1367 */
1368 retval = FAILED;
1369 goto done;
1370 }
1371
1372 /*
1373 * Ensure that the card doesn't do anything
1374 * behind our back. Also make sure that we
1375 * didn't "just" miss an interrupt that would
1376 * affect this cmd.
1377 */
1378 was_paused = ahd_is_paused(ahd);
1379 ahd_pause_and_flushwork(ahd);
1380 paused = TRUE;
1381
1382 if ((pending_scb->flags & SCB_ACTIVE) == 0) {
1383 printf("%s:%d:%d:%d: Command already completed\n",
1384 ahd_name(ahd), cmd->device->channel, cmd->device->id,
1385 cmd->device->lun);
1386 goto no_cmd;
1387 }
1388
1389 printf("%s: At time of recovery, card was %spaused\n",
1390 ahd_name(ahd), was_paused ? "" : "not ");
1391 ahd_dump_card_state(ahd);
1392
1393 disconnected = TRUE;
1394 if (ahd_search_qinfifo(ahd, cmd->device->id, cmd->device->channel + 'A',
1395 cmd->device->lun, SCB_GET_TAG(pending_scb),
1396 ROLE_INITIATOR, CAM_REQ_ABORTED,
1397 SEARCH_COMPLETE) > 0) {
1398 printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
1399 ahd_name(ahd), cmd->device->channel, cmd->device->id,
1400 cmd->device->lun);
1401 retval = SUCCESS;
1402 goto done;
1403 }
1404
1405 saved_modes = ahd_save_modes(ahd);
1406 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
1407 last_phase = ahd_inb(ahd, LASTPHASE);
1408 saved_scbptr = ahd_get_scbptr(ahd);
1409 active_scbptr = saved_scbptr;
1410 if (disconnected && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
1411 struct scb *bus_scb;
1412
1413 bus_scb = ahd_lookup_scb(ahd, active_scbptr);
1414 if (bus_scb == pending_scb)
1415 disconnected = FALSE;
1416 }
1417
1418 /*
1419 * At this point, pending_scb is the scb associated with the
1420 * passed in command. That command is currently active on the
1421 * bus or is in the disconnected state.
1422 */
1423 if (last_phase != P_BUSFREE
1424 && SCB_GET_TAG(pending_scb) == active_scbptr) {
1425
1426 /*
1427 * We're active on the bus, so assert ATN
1428 * and hope that the target responds.
1429 */
1430 pending_scb = ahd_lookup_scb(ahd, active_scbptr);
1431 pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
1432 ahd_outb(ahd, MSG_OUT, HOST_MSG);
1433 ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
1434 printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
1435 ahd_name(ahd), cmd->device->channel,
1436 cmd->device->id, cmd->device->lun);
1437 wait = TRUE;
1438 } else if (disconnected) {
1439
1440 /*
1441 * Actually re-queue this SCB in an attempt
1442 * to select the device before it reconnects.
1443 */
1444 pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
1445 ahd_set_scbptr(ahd, SCB_GET_TAG(pending_scb));
1446 pending_scb->hscb->cdb_len = 0;
1447 pending_scb->hscb->task_attribute = 0;
1448 pending_scb->hscb->task_management = SIU_TASKMGMT_ABORT_TASK;
1449
1450 if ((pending_scb->flags & SCB_PACKETIZED) != 0) {
1451 /*
1452 * Mark the SCB has having an outstanding
1453 * task management function. Should the command
1454 * complete normally before the task management
1455 * function can be sent, the host will be notified
1456 * to abort our requeued SCB.
1457 */
1458 ahd_outb(ahd, SCB_TASK_MANAGEMENT,
1459 pending_scb->hscb->task_management);
1460 } else {
1461 /*
1462 * If non-packetized, set the MK_MESSAGE control
1463 * bit indicating that we desire to send a message.
1464 * We also set the disconnected flag since there is
1465 * no guarantee that our SCB control byte matches
1466 * the version on the card. We don't want the
1467 * sequencer to abort the command thinking an
1468 * unsolicited reselection occurred.
1469 */
1470 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
1471
1472 /*
1473 * The sequencer will never re-reference the
1474 * in-core SCB. To make sure we are notified
1475 * during reslection, set the MK_MESSAGE flag in
1476 * the card's copy of the SCB.
1477 */
1478 ahd_outb(ahd, SCB_CONTROL,
1479 ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
1480 }
1481
1482 /*
1483 * Clear out any entries in the QINFIFO first
1484 * so we are the next SCB for this target
1485 * to run.
1486 */
1487 ahd_search_qinfifo(ahd, cmd->device->id,
1488 cmd->device->channel + 'A', cmd->device->lun,
1489 SCB_LIST_NULL, ROLE_INITIATOR,
1490 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
1491 ahd_qinfifo_requeue_tail(ahd, pending_scb);
1492 ahd_set_scbptr(ahd, saved_scbptr);
1493 ahd_print_path(ahd, pending_scb);
1494 printf("Device is disconnected, re-queuing SCB\n");
1495 wait = TRUE;
1496 } else {
1497 printf("%s:%d:%d:%d: Unable to deliver message\n",
1498 ahd_name(ahd), cmd->device->channel,
1499 cmd->device->id, cmd->device->lun);
1500 retval = FAILED;
1501 goto done;
1502 }
1503
1504 no_cmd:
1505 /*
1506 * Our assumption is that if we don't have the command, no
1507 * recovery action was required, so we return success. Again,
1508 * the semantics of the mid-layer recovery engine are not
1509 * well defined, so this may change in time.
1510 */
1511 retval = SUCCESS;
1512 done:
1513 if (paused)
1514 ahd_unpause(ahd);
1515 if (wait) {
1516 struct timer_list timer;
1517 int ret;
1518
1519 pending_scb->platform_data->flags |= AHD_SCB_UP_EH_SEM;
1520 spin_unlock_irq(&ahd->platform_data->spin_lock);
1521 init_timer(&timer);
1522 timer.data = (u_long)pending_scb;
1523 timer.expires = jiffies + (5 * HZ);
1524 timer.function = ahd_linux_sem_timeout;
1525 add_timer(&timer);
1526 printf("Recovery code sleeping\n");
1527 down(&ahd->platform_data->eh_sem);
1528 printf("Recovery code awake\n");
1529 ret = del_timer_sync(&timer);
1530 if (ret == 0) {
1531 printf("Timer Expired\n");
1532 retval = FAILED;
1533 }
1534 spin_lock_irq(&ahd->platform_data->spin_lock);
1535 }
1536 ahd_schedule_runq(ahd);
1537 ahd_linux_run_complete_queue(ahd);
1538 ahd_midlayer_entrypoint_unlock(ahd, &s);
1539 return (retval);
1540 }
1541
1542
1543 static void
1544 ahd_linux_dev_reset_complete(Scsi_Cmnd *cmd)
1545 {
1546 free(cmd, M_DEVBUF);
1547 }
1548
1549 /*
1550 * Attempt to send a target reset message to the device that timed out.
1551 */
1552 static int
1553 ahd_linux_dev_reset(Scsi_Cmnd *cmd)
1554 {
1555 struct ahd_softc *ahd;
1556 struct scsi_cmnd *recovery_cmd;
1557 struct ahd_linux_device *dev;
1558 struct ahd_initiator_tinfo *tinfo;
1559 struct ahd_tmode_tstate *tstate;
1560 struct scb *scb;
1561 struct hardware_scb *hscb;
1562 u_long s;
1563 struct timer_list timer;
1564 int retval;
1565
1566 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
1567 recovery_cmd = malloc(sizeof(struct scsi_cmnd), M_DEVBUF, M_WAITOK);
1568 memset(recovery_cmd, 0, sizeof(struct scsi_cmnd));
1569 recovery_cmd->device = cmd->device;
1570 recovery_cmd->scsi_done = ahd_linux_dev_reset_complete;
1571 #if AHD_DEBUG
1572 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
1573 printf("%s:%d:%d:%d: Device reset called for cmd %p\n",
1574 ahd_name(ahd), cmd->device->channel, cmd->device->id,
1575 cmd->device->lun, cmd);
1576 #endif
1577 ahd_midlayer_entrypoint_lock(ahd, &s);
1578
1579 dev = ahd_linux_get_device(ahd, cmd->device->channel, cmd->device->id,
1580 cmd->device->lun, /*alloc*/FALSE);
1581 if (dev == NULL) {
1582 ahd_midlayer_entrypoint_unlock(ahd, &s);
1583 return (FAILED);
1584 }
1585 if ((scb = ahd_get_scb(ahd, AHD_NEVER_COL_IDX)) == NULL) {
1586 ahd_midlayer_entrypoint_unlock(ahd, &s);
1587 return (FAILED);
1588 }
1589 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
1590 cmd->device->id, &tstate);
1591 recovery_cmd->result = CAM_REQ_INPROG << 16;
1592 recovery_cmd->host_scribble = (char *)scb;
1593 scb->io_ctx = recovery_cmd;
1594 scb->platform_data->dev = dev;
1595 scb->sg_count = 0;
1596 ahd_set_residual(scb, 0);
1597 ahd_set_sense_residual(scb, 0);
1598 hscb = scb->hscb;
1599 hscb->control = 0;
1600 hscb->scsiid = BUILD_SCSIID(ahd, cmd);
1601 hscb->lun = cmd->device->lun;
1602 hscb->cdb_len = 0;
1603 hscb->task_management = SIU_TASKMGMT_LUN_RESET;
1604 scb->flags |= SCB_DEVICE_RESET|SCB_RECOVERY_SCB|SCB_ACTIVE;
1605 if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
1606 scb->flags |= SCB_PACKETIZED;
1607 } else {
1608 hscb->control |= MK_MESSAGE;
1609 }
1610 dev->openings--;
1611 dev->active++;
1612 dev->commands_issued++;
1613 LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1614 ahd_queue_scb(ahd, scb);
1615
1616 scb->platform_data->flags |= AHD_SCB_UP_EH_SEM;
1617 spin_unlock_irq(&ahd->platform_data->spin_lock);
1618 init_timer(&timer);
1619 timer.data = (u_long)scb;
1620 timer.expires = jiffies + (5 * HZ);
1621 timer.function = ahd_linux_sem_timeout;
1622 add_timer(&timer);
1623 printf("Recovery code sleeping\n");
1624 down(&ahd->platform_data->eh_sem);
1625 printf("Recovery code awake\n");
1626 retval = SUCCESS;
1627 if (del_timer_sync(&timer) == 0) {
1628 printf("Timer Expired\n");
1629 retval = FAILED;
1630 }
1631 spin_lock_irq(&ahd->platform_data->spin_lock);
1632 ahd_schedule_runq(ahd);
1633 ahd_linux_run_complete_queue(ahd);
1634 ahd_midlayer_entrypoint_unlock(ahd, &s);
1635 printf("%s: Device reset returning 0x%x\n", ahd_name(ahd), retval);
1636 return (retval);
1637 }
1638
1639 /*
1640 * Reset the SCSI bus.
1641 */
1642 static int
1643 ahd_linux_bus_reset(Scsi_Cmnd *cmd)
1644 {
1645 struct ahd_softc *ahd;
1646 u_long s;
1647 int found;
1648
1649 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
1650 #ifdef AHD_DEBUG
1651 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
1652 printf("%s: Bus reset called for cmd %p\n",
1653 ahd_name(ahd), cmd);
1654 #endif
1655 ahd_midlayer_entrypoint_lock(ahd, &s);
1656 found = ahd_reset_channel(ahd, cmd->device->channel + 'A',
1657 /*initiate reset*/TRUE);
1658 ahd_linux_run_complete_queue(ahd);
1659 ahd_midlayer_entrypoint_unlock(ahd, &s);
1660
1661 if (bootverbose)
1662 printf("%s: SCSI bus reset delivered. "
1663 "%d SCBs aborted.\n", ahd_name(ahd), found);
1664
1665 return (SUCCESS);
1666 }
1667
1668 Scsi_Host_Template aic79xx_driver_template = {
1669 .module = THIS_MODULE,
1670 .name = "aic79xx",
1671 .proc_info = ahd_linux_proc_info,
1672 .info = ahd_linux_info,
1673 .queuecommand = ahd_linux_queue,
1674 .eh_abort_handler = ahd_linux_abort,
1675 .eh_device_reset_handler = ahd_linux_dev_reset,
1676 .eh_bus_reset_handler = ahd_linux_bus_reset,
1677 #if defined(__i386__)
1678 .bios_param = ahd_linux_biosparam,
1679 #endif
1680 .can_queue = AHD_MAX_QUEUE,
1681 .this_id = -1,
1682 .cmd_per_lun = 2,
1683 .use_clustering = ENABLE_CLUSTERING,
1684 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,7)
1685 /*
1686 * We can only map 16MB per-SG
1687 * so create a sector limit of
1688 * "16MB" in 2K sectors.
1689 */
1690 .max_sectors = 8192,
1691 #endif
1692 #if defined CONFIG_HIGHIO || LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1693 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,10)
1694 /* Assume RedHat Distribution with its different HIGHIO conventions. */
1695 .can_dma_32 = 1,
1696 .single_sg_okay = 1,
1697 #else
1698 .highmem_io = 1,
1699 #endif
1700 #endif
1701 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1702 .slave_alloc = ahd_linux_slave_alloc,
1703 .slave_configure = ahd_linux_slave_configure,
1704 .slave_destroy = ahd_linux_slave_destroy,
1705 #else
1706 .detect = ahd_linux_detect,
1707 .release = ahd_linux_release,
1708 .select_queue_depths = ahd_linux_select_queue_depth,
1709 .use_new_eh_code = 1,
1710 #endif
1711 };
1712
1713 /**************************** Tasklet Handler *********************************/
1714
1715 /*
1716 * In 2.4.X and above, this routine is called from a tasklet,
1717 * so we must re-acquire our lock prior to executing this code.
1718 * In all prior kernels, ahd_schedule_runq() calls this routine
1719 * directly and ahd_schedule_runq() is called with our lock held.
1720 */
1721 static void
1722 ahd_runq_tasklet(unsigned long data)
1723 {
1724 struct ahd_softc* ahd;
1725 struct ahd_linux_device *dev;
1726 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1727 u_long flags;
1728 #endif
1729
1730 ahd = (struct ahd_softc *)data;
1731 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1732 ahd_lock(ahd, &flags);
1733 #endif
1734 while ((dev = ahd_linux_next_device_to_run(ahd)) != NULL) {
1735
1736 TAILQ_REMOVE(&ahd->platform_data->device_runq, dev, links);
1737 dev->flags &= ~AHD_DEV_ON_RUN_LIST;
1738 ahd_linux_check_device_queue(ahd, dev);
1739 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1740 /* Yeild to our interrupt handler */
1741 ahd_unlock(ahd, &flags);
1742 ahd_lock(ahd, &flags);
1743 #endif
1744 }
1745 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1746 ahd_unlock(ahd, &flags);
1747 #endif
1748 }
1749
1750 /******************************** Bus DMA *************************************/
1751 int
1752 ahd_dma_tag_create(struct ahd_softc *ahd, bus_dma_tag_t parent,
1753 bus_size_t alignment, bus_size_t boundary,
1754 bus_addr_t lowaddr, bus_addr_t highaddr,
1755 bus_dma_filter_t *filter, void *filterarg,
1756 bus_size_t maxsize, int nsegments,
1757 bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
1758 {
1759 bus_dma_tag_t dmat;
1760
1761 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
1762 if (dmat == NULL)
1763 return (ENOMEM);
1764
1765 /*
1766 * Linux is very simplistic about DMA memory. For now don't
1767 * maintain all specification information. Once Linux supplies
1768 * better facilities for doing these operations, or the
1769 * needs of this particular driver change, we might need to do
1770 * more here.
1771 */
1772 dmat->alignment = alignment;
1773 dmat->boundary = boundary;
1774 dmat->maxsize = maxsize;
1775 *ret_tag = dmat;
1776 return (0);
1777 }
1778
1779 void
1780 ahd_dma_tag_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat)
1781 {
1782 free(dmat, M_DEVBUF);
1783 }
1784
1785 int
1786 ahd_dmamem_alloc(struct ahd_softc *ahd, bus_dma_tag_t dmat, void** vaddr,
1787 int flags, bus_dmamap_t *mapp)
1788 {
1789 bus_dmamap_t map;
1790
1791 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1792 map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT);
1793 if (map == NULL)
1794 return (ENOMEM);
1795 /*
1796 * Although we can dma data above 4GB, our
1797 * "consistent" memory is below 4GB for
1798 * space efficiency reasons (only need a 4byte
1799 * address). For this reason, we have to reset
1800 * our dma mask when doing allocations.
1801 */
1802 if (ahd->dev_softc != NULL)
1803 ahd_pci_set_dma_mask(ahd->dev_softc, 0xFFFFFFFF);
1804 *vaddr = pci_alloc_consistent(ahd->dev_softc,
1805 dmat->maxsize, &map->bus_addr);
1806 if (ahd->dev_softc != NULL)
1807 ahd_pci_set_dma_mask(ahd->dev_softc,
1808 ahd->platform_data->hw_dma_mask);
1809 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0) */
1810 /*
1811 * At least in 2.2.14, malloc is a slab allocator so all
1812 * allocations are aligned. We assume for these kernel versions
1813 * that all allocations will be bellow 4Gig, physically contiguous,
1814 * and accessible via DMA by the controller.
1815 */
1816 map = NULL; /* No additional information to store */
1817 *vaddr = malloc(dmat->maxsize, M_DEVBUF, M_NOWAIT);
1818 #endif
1819 if (*vaddr == NULL)
1820 return (ENOMEM);
1821 *mapp = map;
1822 return(0);
1823 }
1824
1825 void
1826 ahd_dmamem_free(struct ahd_softc *ahd, bus_dma_tag_t dmat,
1827 void* vaddr, bus_dmamap_t map)
1828 {
1829 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1830 pci_free_consistent(ahd->dev_softc, dmat->maxsize,
1831 vaddr, map->bus_addr);
1832 #else
1833 free(vaddr, M_DEVBUF);
1834 #endif
1835 }
1836
1837 int
1838 ahd_dmamap_load(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map,
1839 void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
1840 void *cb_arg, int flags)
1841 {
1842 /*
1843 * Assume for now that this will only be used during
1844 * initialization and not for per-transaction buffer mapping.
1845 */
1846 bus_dma_segment_t stack_sg;
1847
1848 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1849 stack_sg.ds_addr = map->bus_addr;
1850 #else
1851 #define VIRT_TO_BUS(a) (uint32_t)virt_to_bus((void *)(a))
1852 stack_sg.ds_addr = VIRT_TO_BUS(buf);
1853 #endif
1854 stack_sg.ds_len = dmat->maxsize;
1855 cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
1856 return (0);
1857 }
1858
1859 void
1860 ahd_dmamap_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
1861 {
1862 /*
1863 * The map may is NULL in our < 2.3.X implementation.
1864 */
1865 if (map != NULL)
1866 free(map, M_DEVBUF);
1867 }
1868
1869 int
1870 ahd_dmamap_unload(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
1871 {
1872 /* Nothing to do */
1873 return (0);
1874 }
1875
1876 /********************* Platform Dependent Functions ***************************/
1877 /*
1878 * Compare "left hand" softc with "right hand" softc, returning:
1879 * < 0 - lahd has a lower priority than rahd
1880 * 0 - Softcs are equal
1881 * > 0 - lahd has a higher priority than rahd
1882 */
1883 int
1884 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1885 {
1886 int value;
1887
1888 /*
1889 * Under Linux, cards are ordered as follows:
1890 * 1) PCI devices that are marked as the boot controller.
1891 * 2) PCI devices with BIOS enabled sorted by bus/slot/func.
1892 * 3) All remaining PCI devices sorted by bus/slot/func.
1893 */
1894 #if 0
1895 value = (lahd->flags & AHD_BOOT_CHANNEL)
1896 - (rahd->flags & AHD_BOOT_CHANNEL);
1897 if (value != 0)
1898 /* Controllers set for boot have a *higher* priority */
1899 return (value);
1900 #endif
1901
1902 value = (lahd->flags & AHD_BIOS_ENABLED)
1903 - (rahd->flags & AHD_BIOS_ENABLED);
1904 if (value != 0)
1905 /* Controllers with BIOS enabled have a *higher* priority */
1906 return (value);
1907
1908 /* Still equal. Sort by bus/slot/func. */
1909 if (aic79xx_reverse_scan != 0)
1910 value = ahd_get_pci_bus(lahd->dev_softc)
1911 - ahd_get_pci_bus(rahd->dev_softc);
1912 else
1913 value = ahd_get_pci_bus(rahd->dev_softc)
1914 - ahd_get_pci_bus(lahd->dev_softc);
1915 if (value != 0)
1916 return (value);
1917 if (aic79xx_reverse_scan != 0)
1918 value = ahd_get_pci_slot(lahd->dev_softc)
1919 - ahd_get_pci_slot(rahd->dev_softc);
1920 else
1921 value = ahd_get_pci_slot(rahd->dev_softc)
1922 - ahd_get_pci_slot(lahd->dev_softc);
1923 if (value != 0)
1924 return (value);
1925
1926 value = rahd->channel - lahd->channel;
1927 return (value);
1928 }
1929
1930 static void
1931 ahd_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
1932 {
1933
1934 if ((instance >= 0) && (targ >= 0)
1935 && (instance < NUM_ELEMENTS(aic79xx_tag_info))
1936 && (targ < AHD_NUM_TARGETS)) {
1937 aic79xx_tag_info[instance].tag_commands[targ] = value & 0x1FF;
1938 if (bootverbose)
1939 printf("tag_info[%d:%d] = %d\n", instance, targ, value);
1940 }
1941 }
1942
1943 static void
1944 ahd_linux_setup_rd_strm_info(u_long arg, int instance, int targ, int32_t value)
1945 {
1946 if ((instance >= 0)
1947 && (instance < NUM_ELEMENTS(aic79xx_rd_strm_info))) {
1948 aic79xx_rd_strm_info[instance] = value & 0xFFFF;
1949 if (bootverbose)
1950 printf("rd_strm[%d] = 0x%x\n", instance, value);
1951 }
1952 }
1953
1954 static void
1955 ahd_linux_setup_dv(u_long arg, int instance, int targ, int32_t value)
1956 {
1957 if ((instance >= 0)
1958 && (instance < NUM_ELEMENTS(aic79xx_dv_settings))) {
1959 aic79xx_dv_settings[instance] = value;
1960 if (bootverbose)
1961 printf("dv[%d] = %d\n", instance, value);
1962 }
1963 }
1964
1965 static void
1966 ahd_linux_setup_iocell_info(u_long index, int instance, int targ, int32_t value)
1967 {
1968
1969 if ((instance >= 0)
1970 && (instance < NUM_ELEMENTS(aic79xx_iocell_info))) {
1971 uint8_t *iocell_info;
1972
1973 iocell_info = (uint8_t*)&aic79xx_iocell_info[instance];
1974 iocell_info[index] = value & 0xFFFF;
1975 if (bootverbose)
1976 printf("iocell[%d:%ld] = %d\n", instance, index, value);
1977 }
1978 }
1979
1980 static void
1981 ahd_linux_setup_tag_info_global(char *p)
1982 {
1983 int tags, i, j;
1984
1985 tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
1986 printf("Setting Global Tags= %d\n", tags);
1987
1988 for (i = 0; i < NUM_ELEMENTS(aic79xx_tag_info); i++) {
1989 for (j = 0; j < AHD_NUM_TARGETS; j++) {
1990 aic79xx_tag_info[i].tag_commands[j] = tags;
1991 }
1992 }
1993 }
1994
1995 /*
1996 * Handle Linux boot parameters. This routine allows for assigning a value
1997 * to a parameter with a ':' between the parameter and the value.
1998 * ie. aic79xx=stpwlev:1,extended
1999 */
2000 static int
2001 aic79xx_setup(char *s)
2002 {
2003 int i, n;
2004 char *p;
2005 char *end;
2006
2007 static struct {
2008 const char *name;
2009 uint32_t *flag;
2010 } options[] = {
2011 { "extended", &aic79xx_extended },
2012 { "no_reset", &aic79xx_no_reset },
2013 { "verbose", &aic79xx_verbose },
2014 { "allow_memio", &aic79xx_allow_memio},
2015 #ifdef AHD_DEBUG
2016 { "debug", &ahd_debug },
2017 #endif
2018 { "reverse_scan", &aic79xx_reverse_scan },
2019 { "periodic_otag", &aic79xx_periodic_otag },
2020 { "pci_parity", &aic79xx_pci_parity },
2021 { "seltime", &aic79xx_seltime },
2022 { "tag_info", NULL },
2023 { "global_tag_depth", NULL},
2024 { "rd_strm", NULL },
2025 { "dv", NULL },
2026 { "slewrate", NULL },
2027 { "precomp", NULL },
2028 { "amplitude", NULL },
2029 };
2030
2031 end = strchr(s, '\0');
2032
2033 /*
2034 * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
2035 * will never be 0 in this case.
2036 */
2037 n = 0;
2038
2039 while ((p = strsep(&s, ",.")) != NULL) {
2040 if (*p == '\0')
2041 continue;
2042 for (i = 0; i < NUM_ELEMENTS(options); i++) {
2043
2044 n = strlen(options[i].name);
2045 if (strncmp(options[i].name, p, n) == 0)
2046 break;
2047 }
2048 if (i == NUM_ELEMENTS(options))
2049 continue;
2050
2051 if (strncmp(p, "global_tag_depth", n) == 0) {
2052 ahd_linux_setup_tag_info_global(p + n);
2053 } else if (strncmp(p, "tag_info", n) == 0) {
2054 s = aic_parse_brace_option("tag_info", p + n, end,
2055 2, ahd_linux_setup_tag_info, 0);
2056 } else if (strncmp(p, "rd_strm", n) == 0) {
2057 s = aic_parse_brace_option("rd_strm", p + n, end,
2058 1, ahd_linux_setup_rd_strm_info, 0);
2059 } else if (strncmp(p, "dv", n) == 0) {
2060 s = aic_parse_brace_option("dv", p + n, end, 1,
2061 ahd_linux_setup_dv, 0);
2062 } else if (strncmp(p, "slewrate", n) == 0) {
2063 s = aic_parse_brace_option("slewrate",
2064 p + n, end, 1, ahd_linux_setup_iocell_info,
2065 AIC79XX_SLEWRATE_INDEX);
2066 } else if (strncmp(p, "precomp", n) == 0) {
2067 s = aic_parse_brace_option("precomp",
2068 p + n, end, 1, ahd_linux_setup_iocell_info,
2069 AIC79XX_PRECOMP_INDEX);
2070 } else if (strncmp(p, "amplitude", n) == 0) {
2071 s = aic_parse_brace_option("amplitude",
2072 p + n, end, 1, ahd_linux_setup_iocell_info,
2073 AIC79XX_AMPLITUDE_INDEX);
2074 } else if (p[n] == ':') {
2075 *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
2076 } else if (!strncmp(p, "verbose", n)) {
2077 *(options[i].flag) = 1;
2078 } else {
2079 *(options[i].flag) ^= 0xFFFFFFFF;
2080 }
2081 }
2082 return 1;
2083 }
2084
2085 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,0)
2086 __setup("aic79xx=", aic79xx_setup);
2087 #endif
2088
2089 uint32_t aic79xx_verbose;
2090
2091 int
2092 ahd_linux_register_host(struct ahd_softc *ahd, Scsi_Host_Template *template)
2093 {
2094 char buf[80];
2095 struct Scsi_Host *host;
2096 char *new_name;
2097 u_long s;
2098 u_long target;
2099
2100 template->name = ahd->description;
2101 host = scsi_register(template, sizeof(struct ahd_softc *));
2102 if (host == NULL)
2103 return (ENOMEM);
2104
2105 *((struct ahd_softc **)host->hostdata) = ahd;
2106 ahd_lock(ahd, &s);
2107 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
2108 scsi_assign_lock(host, &ahd->platform_data->spin_lock);
2109 #elif AHD_SCSI_HAS_HOST_LOCK != 0
2110 host->lock = &ahd->platform_data->spin_lock;
2111 #endif
2112 ahd->platform_data->host = host;
2113 host->can_queue = AHD_MAX_QUEUE;
2114 host->cmd_per_lun = 2;
2115 host->sg_tablesize = AHD_NSEG;
2116 host->this_id = ahd->our_id;
2117 host->irq = ahd->platform_data->irq;
2118 host->max_id = (ahd->features & AHD_WIDE) ? 16 : 8;
2119 host->max_lun = AHD_NUM_LUNS;
2120 host->max_channel = 0;
2121 host->sg_tablesize = AHD_NSEG;
2122 ahd_set_unit(ahd, ahd_linux_next_unit());
2123 sprintf(buf, "scsi%d", host->host_no);
2124 new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
2125 if (new_name != NULL) {
2126 strcpy(new_name, buf);
2127 ahd_set_name(ahd, new_name);
2128 }
2129 host->unique_id = ahd->unit;
2130 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,4) && \
2131 LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2132 scsi_set_pci_device(host, ahd->dev_softc);
2133 #endif
2134 ahd_linux_setup_user_rd_strm_settings(ahd);
2135 ahd_linux_initialize_scsi_bus(ahd);
2136 ahd_unlock(ahd, &s);
2137 ahd->platform_data->dv_pid = kernel_thread(ahd_linux_dv_thread, ahd, 0);
2138 ahd_lock(ahd, &s);
2139 if (ahd->platform_data->dv_pid < 0) {
2140 printf("%s: Failed to create DV thread, error= %d\n",
2141 ahd_name(ahd), ahd->platform_data->dv_pid);
2142 return (-ahd->platform_data->dv_pid);
2143 }
2144 /*
2145 * Initially allocate *all* of our linux target objects
2146 * so that the DV thread will scan them all in parallel
2147 * just after driver initialization. Any device that
2148 * does not exist will have its target object destroyed
2149 * by the selection timeout handler. In the case of a
2150 * device that appears after the initial DV scan, async
2151 * negotiation will occur for the first command, and DV
2152 * will comence should that first command be successful.
2153 */
2154 for (target = 0; target < host->max_id; target++) {
2155
2156 /*
2157 * Skip our own ID. Some Compaq/HP storage devices
2158 * have enclosure management devices that respond to
2159 * single bit selection (i.e. selecting ourselves).
2160 * It is expected that either an external application
2161 * or a modified kernel will be used to probe this
2162 * ID if it is appropriate. To accommodate these
2163 * installations, ahc_linux_alloc_target() will allocate
2164 * for our ID if asked to do so.
2165 */
2166 if (target == ahd->our_id)
2167 continue;
2168
2169 ahd_linux_alloc_target(ahd, 0, target);
2170 }
2171 ahd_intr_enable(ahd, TRUE);
2172 ahd_linux_start_dv(ahd);
2173 ahd_unlock(ahd, &s);
2174
2175 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
2176 scsi_add_host(host, &ahd->dev_softc->dev);
2177 #endif
2178 return (0);
2179 }
2180
2181 uint64_t
2182 ahd_linux_get_memsize(void)
2183 {
2184 struct sysinfo si;
2185
2186 si_meminfo(&si);
2187 return ((uint64_t)si.totalram << PAGE_SHIFT);
2188 }
2189
2190 /*
2191 * Find the smallest available unit number to use
2192 * for a new device. We don't just use a static
2193 * count to handle the "repeated hot-(un)plug"
2194 * scenario.
2195 */
2196 static int
2197 ahd_linux_next_unit(void)
2198 {
2199 struct ahd_softc *ahd;
2200 int unit;
2201
2202 unit = 0;
2203 retry:
2204 TAILQ_FOREACH(ahd, &ahd_tailq, links) {
2205 if (ahd->unit == unit) {
2206 unit++;
2207 goto retry;
2208 }
2209 }
2210 return (unit);
2211 }
2212
2213 /*
2214 * Place the SCSI bus into a known state by either resetting it,
2215 * or forcing transfer negotiations on the next command to any
2216 * target.
2217 */
2218 static void
2219 ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd)
2220 {
2221 u_int target_id;
2222 u_int numtarg;
2223
2224 target_id = 0;
2225 numtarg = 0;
2226
2227 if (aic79xx_no_reset != 0)
2228 ahd->flags &= ~AHD_RESET_BUS_A;
2229
2230 if ((ahd->flags & AHD_RESET_BUS_A) != 0)
2231 ahd_reset_channel(ahd, 'A', /*initiate_reset*/TRUE);
2232 else
2233 numtarg = (ahd->features & AHD_WIDE) ? 16 : 8;
2234
2235 /*
2236 * Force negotiation to async for all targets that
2237 * will not see an initial bus reset.
2238 */
2239 for (; target_id < numtarg; target_id++) {
2240 struct ahd_devinfo devinfo;
2241 struct ahd_initiator_tinfo *tinfo;
2242 struct ahd_tmode_tstate *tstate;
2243
2244 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
2245 target_id, &tstate);
2246 ahd_compile_devinfo(&devinfo, ahd->our_id, target_id,
2247 CAM_LUN_WILDCARD, 'A', ROLE_INITIATOR);
2248 ahd_update_neg_request(ahd, &devinfo, tstate,
2249 tinfo, AHD_NEG_ALWAYS);
2250 }
2251 /* Give the bus some time to recover */
2252 if ((ahd->flags & AHD_RESET_BUS_A) != 0) {
2253 ahd_freeze_simq(ahd);
2254 init_timer(&ahd->platform_data->reset_timer);
2255 ahd->platform_data->reset_timer.data = (u_long)ahd;
2256 ahd->platform_data->reset_timer.expires =
2257 jiffies + (AIC79XX_RESET_DELAY * HZ)/1000;
2258 ahd->platform_data->reset_timer.function =
2259 (ahd_linux_callback_t *)ahd_release_simq;
2260 add_timer(&ahd->platform_data->reset_timer);
2261 }
2262 }
2263
2264 int
2265 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
2266 {
2267 ahd->platform_data =
2268 malloc(sizeof(struct ahd_platform_data), M_DEVBUF, M_NOWAIT);
2269 if (ahd->platform_data == NULL)
2270 return (ENOMEM);
2271 memset(ahd->platform_data, 0, sizeof(struct ahd_platform_data));
2272 TAILQ_INIT(&ahd->platform_data->completeq);
2273 TAILQ_INIT(&ahd->platform_data->device_runq);
2274 ahd->platform_data->irq = AHD_LINUX_NOIRQ;
2275 ahd->platform_data->hw_dma_mask = 0xFFFFFFFF;
2276 ahd_lockinit(ahd);
2277 ahd_done_lockinit(ahd);
2278 init_timer(&ahd->platform_data->completeq_timer);
2279 ahd->platform_data->completeq_timer.data = (u_long)ahd;
2280 ahd->platform_data->completeq_timer.function =
2281 (ahd_linux_callback_t *)ahd_linux_thread_run_complete_queue;
2282 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
2283 init_MUTEX_LOCKED(&ahd->platform_data->eh_sem);
2284 init_MUTEX_LOCKED(&ahd->platform_data->dv_sem);
2285 init_MUTEX_LOCKED(&ahd->platform_data->dv_cmd_sem);
2286 #else
2287 ahd->platform_data->eh_sem = MUTEX_LOCKED;
2288 ahd->platform_data->dv_sem = MUTEX_LOCKED;
2289 ahd->platform_data->dv_cmd_sem = MUTEX_LOCKED;
2290 #endif
2291 ahd_setup_runq_tasklet(ahd);
2292 ahd->seltime = (aic79xx_seltime & 0x3) << 4;
2293 return (0);
2294 }
2295
2296 void
2297 ahd_platform_free(struct ahd_softc *ahd)
2298 {
2299 struct ahd_linux_target *targ;
2300 struct ahd_linux_device *dev;
2301 int i, j;
2302
2303 if (ahd->platform_data != NULL) {
2304 del_timer_sync(&ahd->platform_data->completeq_timer);
2305 ahd_linux_kill_dv_thread(ahd);
2306 ahd_teardown_runq_tasklet(ahd);
2307 if (ahd->platform_data->host != NULL) {
2308 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
2309 scsi_remove_host(ahd->platform_data->host);
2310 #endif
2311 scsi_unregister(ahd->platform_data->host);
2312 }
2313
2314 /* destroy all of the device and target objects */
2315 for (i = 0; i < AHD_NUM_TARGETS; i++) {
2316 targ = ahd->platform_data->targets[i];
2317 if (targ != NULL) {
2318 /* Keep target around through the loop. */
2319 targ->refcount++;
2320 for (j = 0; j < AHD_NUM_LUNS; j++) {
2321
2322 if (targ->devices[j] == NULL)
2323 continue;
2324 dev = targ->devices[j];
2325 ahd_linux_free_device(ahd, dev);
2326 }
2327 /*
2328 * Forcibly free the target now that
2329 * all devices are gone.
2330 */
2331 ahd_linux_free_target(ahd, targ);
2332 }
2333 }
2334
2335 if (ahd->platform_data->irq != AHD_LINUX_NOIRQ)
2336 free_irq(ahd->platform_data->irq, ahd);
2337 if (ahd->tags[0] == BUS_SPACE_PIO
2338 && ahd->bshs[0].ioport != 0)
2339 release_region(ahd->bshs[0].ioport, 256);
2340 if (ahd->tags[1] == BUS_SPACE_PIO
2341 && ahd->bshs[1].ioport != 0)
2342 release_region(ahd->bshs[1].ioport, 256);
2343 if (ahd->tags[0] == BUS_SPACE_MEMIO
2344 && ahd->bshs[0].maddr != NULL) {
2345 u_long base_addr;
2346
2347 base_addr = (u_long)ahd->bshs[0].maddr;
2348 base_addr &= PAGE_MASK;
2349 iounmap((void *)base_addr);
2350 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
2351 release_mem_region(ahd->platform_data->mem_busaddr,
2352 0x1000);
2353 #endif
2354 }
2355 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0) && \
2356 LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2357 /*
2358 * In 2.4 we detach from the scsi midlayer before the PCI
2359 * layer invokes our remove callback. No per-instance
2360 * detach is provided, so we must reach inside the PCI
2361 * subsystem's internals and detach our driver manually.
2362 */
2363 if (ahd->dev_softc != NULL)
2364 ahd->dev_softc->driver = NULL;
2365 #endif
2366 free(ahd->platform_data, M_DEVBUF);
2367 }
2368 }
2369
2370 void
2371 ahd_platform_init(struct ahd_softc *ahd)
2372 {
2373 /*
2374 * Lookup and commit any modified IO Cell options.
2375 */
2376 if (ahd->unit < NUM_ELEMENTS(aic79xx_iocell_info)) {
2377 struct ahd_linux_iocell_opts *iocell_opts;
2378
2379 iocell_opts = &aic79xx_iocell_info[ahd->unit];
2380 if (iocell_opts->precomp != AIC79XX_DEFAULT_PRECOMP)
2381 AHD_SET_PRECOMP(ahd, iocell_opts->precomp);
2382 if (iocell_opts->slewrate != AIC79XX_DEFAULT_SLEWRATE)
2383 AHD_SET_SLEWRATE(ahd, iocell_opts->slewrate);
2384 if (iocell_opts->amplitude != AIC79XX_DEFAULT_AMPLITUDE)
2385 AHD_SET_AMPLITUDE(ahd, iocell_opts->amplitude);
2386 }
2387
2388 }
2389
2390 void
2391 ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
2392 {
2393 ahd_platform_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
2394 SCB_GET_CHANNEL(ahd, scb),
2395 SCB_GET_LUN(scb), SCB_LIST_NULL,
2396 ROLE_UNKNOWN, CAM_REQUEUE_REQ);
2397 }
2398
2399 void
2400 ahd_platform_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
2401 ahd_queue_alg alg)
2402 {
2403 struct ahd_linux_device *dev;
2404 int was_queuing;
2405 int now_queuing;
2406
2407 dev = ahd_linux_get_device(ahd, devinfo->channel - 'A',
2408 devinfo->target,
2409 devinfo->lun, /*alloc*/FALSE);
2410 if (dev == NULL)
2411 return;
2412 was_queuing = dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED);
2413 switch (alg) {
2414 default:
2415 case AHD_QUEUE_NONE:
2416 now_queuing = 0;
2417 break;
2418 case AHD_QUEUE_BASIC:
2419 now_queuing = AHD_DEV_Q_BASIC;
2420 break;
2421 case AHD_QUEUE_TAGGED:
2422 now_queuing = AHD_DEV_Q_TAGGED;
2423 break;
2424 }
2425 if ((dev->flags & AHD_DEV_FREEZE_TIL_EMPTY) == 0
2426 && (was_queuing != now_queuing)
2427 && (dev->active != 0)) {
2428 dev->flags |= AHD_DEV_FREEZE_TIL_EMPTY;
2429 dev->qfrozen++;
2430 }
2431
2432 dev->flags &= ~(AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED|AHD_DEV_PERIODIC_OTAG);
2433 if (now_queuing) {
2434 u_int usertags;
2435
2436 usertags = ahd_linux_user_tagdepth(ahd, devinfo);
2437 if (!was_queuing) {
2438 /*
2439 * Start out agressively and allow our
2440 * dynamic queue depth algorithm to take
2441 * care of the rest.
2442 */
2443 dev->maxtags = usertags;
2444 dev->openings = dev->maxtags - dev->active;
2445 }
2446 if (dev->maxtags == 0) {
2447 /*
2448 * Queueing is disabled by the user.
2449 */
2450 dev->openings = 1;
2451 } else if (alg == AHD_QUEUE_TAGGED) {
2452 dev->flags |= AHD_DEV_Q_TAGGED;
2453 if (aic79xx_periodic_otag != 0)
2454 dev->flags |= AHD_DEV_PERIODIC_OTAG;
2455 } else
2456 dev->flags |= AHD_DEV_Q_BASIC;
2457 } else {
2458 /* We can only have one opening. */
2459 dev->maxtags = 0;
2460 dev->openings = 1 - dev->active;
2461 }
2462 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
2463 if (dev->scsi_device != NULL) {
2464 switch ((dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED))) {
2465 case AHD_DEV_Q_BASIC:
2466 scsi_adjust_queue_depth(dev->scsi_device,
2467 MSG_SIMPLE_TASK,
2468 dev->openings + dev->active);
2469 break;
2470 case AHD_DEV_Q_TAGGED:
2471 scsi_adjust_queue_depth(dev->scsi_device,
2472 MSG_ORDERED_TASK,
2473 dev->openings + dev->active);
2474 break;
2475 default:
2476 /*
2477 * We allow the OS to queue 2 untagged transactions to
2478 * us at any time even though we can only execute them
2479 * serially on the controller/device. This should
2480 * remove some latency.
2481 */
2482 scsi_adjust_queue_depth(dev->scsi_device,
2483 /*NON-TAGGED*/0,
2484 /*queue depth*/2);
2485 break;
2486 }
2487 }
2488 #endif
2489 }
2490
2491 int
2492 ahd_platform_abort_scbs(struct ahd_softc *ahd, int target, char channel,
2493 int lun, u_int tag, role_t role, uint32_t status)
2494 {
2495 int targ;
2496 int maxtarg;
2497 int maxlun;
2498 int clun;
2499 int count;
2500
2501 if (tag != SCB_LIST_NULL)
2502 return (0);
2503
2504 targ = 0;
2505 if (target != CAM_TARGET_WILDCARD) {
2506 targ = target;
2507 maxtarg = targ + 1;
2508 } else {
2509 maxtarg = (ahd->features & AHD_WIDE) ? 16 : 8;
2510 }
2511 clun = 0;
2512 if (lun != CAM_LUN_WILDCARD) {
2513 clun = lun;
2514 maxlun = clun + 1;
2515 } else {
2516 maxlun = AHD_NUM_LUNS;
2517 }
2518
2519 count = 0;
2520 for (; targ < maxtarg; targ++) {
2521
2522 for (; clun < maxlun; clun++) {
2523 struct ahd_linux_device *dev;
2524 struct ahd_busyq *busyq;
2525 struct ahd_cmd *acmd;
2526
2527 dev = ahd_linux_get_device(ahd, /*chan*/0, targ,
2528 clun, /*alloc*/FALSE);
2529 if (dev == NULL)
2530 continue;
2531
2532 busyq = &dev->busyq;
2533 while ((acmd = TAILQ_FIRST(busyq)) != NULL) {
2534 Scsi_Cmnd *cmd;
2535
2536 cmd = &acmd_scsi_cmd(acmd);
2537 TAILQ_REMOVE(busyq, acmd,
2538 acmd_links.tqe);
2539 count++;
2540 cmd->result = status << 16;
2541 ahd_linux_queue_cmd_complete(ahd, cmd);
2542 }
2543 }
2544 }
2545
2546 return (count);
2547 }
2548
2549 static void
2550 ahd_linux_thread_run_complete_queue(struct ahd_softc *ahd)
2551 {
2552 u_long flags;
2553
2554 ahd_lock(ahd, &flags);
2555 del_timer(&ahd->platform_data->completeq_timer);
2556 ahd->platform_data->flags &= ~AHD_RUN_CMPLT_Q_TIMER;
2557 ahd_linux_run_complete_queue(ahd);
2558 ahd_unlock(ahd, &flags);
2559 }
2560
2561 static void
2562 ahd_linux_start_dv(struct ahd_softc *ahd)
2563 {
2564
2565 /*
2566 * Freeze the simq and signal ahd_linux_queue to not let any
2567 * more commands through
2568 */
2569 if ((ahd->platform_data->flags & AHD_DV_ACTIVE) == 0) {
2570 #ifdef AHD_DEBUG
2571 if (ahd_debug & AHD_SHOW_DV)
2572 printf("%s: Starting DV\n", ahd_name(ahd));
2573 #endif
2574
2575 ahd->platform_data->flags |= AHD_DV_ACTIVE;
2576 ahd_freeze_simq(ahd);
2577
2578 /* Wake up the DV kthread */
2579 up(&ahd->platform_data->dv_sem);
2580 }
2581 }
2582
2583 static int
2584 ahd_linux_dv_thread(void *data)
2585 {
2586 struct ahd_softc *ahd;
2587 int target;
2588 u_long s;
2589
2590 ahd = (struct ahd_softc *)data;
2591
2592 #ifdef AHD_DEBUG
2593 if (ahd_debug & AHD_SHOW_DV)
2594 printf("In DV Thread\n");
2595 #endif
2596
2597 /*
2598 * Complete thread creation.
2599 */
2600 lock_kernel();
2601 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,60)
2602 /*
2603 * Don't care about any signals.
2604 */
2605 siginitsetinv(¤t->blocked, 0);
2606
2607 daemonize();
2608 sprintf(current->comm, "ahd_dv_%d", ahd->unit);
2609 #else
2610 daemonize("ahd_dv_%d", ahd->unit);
2611 #endif
2612 unlock_kernel();
2613
2614 while (1) {
2615 /*
2616 * Use down_interruptible() rather than down() to
2617 * avoid inclusion in the load average.
2618 */
2619 down_interruptible(&ahd->platform_data->dv_sem);
2620
2621 /* Check to see if we've been signaled to exit */
2622 ahd_lock(ahd, &s);
2623 if ((ahd->platform_data->flags & AHD_DV_SHUTDOWN) != 0) {
2624 ahd_unlock(ahd, &s);
2625 break;
2626 }
2627 ahd_unlock(ahd, &s);
2628
2629 #ifdef AHD_DEBUG
2630 if (ahd_debug & AHD_SHOW_DV)
2631 printf("%s: Beginning Domain Validation\n",
2632 ahd_name(ahd));
2633 #endif
2634
2635 /*
2636 * Wait for any pending commands to drain before proceeding.
2637 */
2638 ahd_lock(ahd, &s);
2639 while (LIST_FIRST(&ahd->pending_scbs) != NULL) {
2640 ahd->platform_data->flags |= AHD_DV_WAIT_SIMQ_EMPTY;
2641 ahd_unlock(ahd, &s);
2642 down_interruptible(&ahd->platform_data->dv_sem);
2643 ahd_lock(ahd, &s);
2644 }
2645
2646 /*
2647 * Wait for the SIMQ to be released so that DV is the
2648 * only reason the queue is frozen.
2649 */
2650 while (AHD_DV_SIMQ_FROZEN(ahd) == 0) {
2651 ahd->platform_data->flags |= AHD_DV_WAIT_SIMQ_RELEASE;
2652 ahd_unlock(ahd, &s);
2653 down_interruptible(&ahd->platform_data->dv_sem);
2654 ahd_lock(ahd, &s);
2655 }
2656 ahd_unlock(ahd, &s);
2657
2658 for (target = 0; target < AHD_NUM_TARGETS; target++)
2659 ahd_linux_dv_target(ahd, target);
2660
2661 ahd_lock(ahd, &s);
2662 ahd->platform_data->flags &= ~AHD_DV_ACTIVE;
2663 ahd_unlock(ahd, &s);
2664
2665 /*
2666 * Release the SIMQ so that normal commands are
2667 * allowed to continue on the bus.
2668 */
2669 ahd_release_simq(ahd);
2670 }
2671 up(&ahd->platform_data->eh_sem);
2672 return (0);
2673 }
2674
2675 static void
2676 ahd_linux_kill_dv_thread(struct ahd_softc *ahd)
2677 {
2678 u_long s;
2679
2680 ahd_lock(ahd, &s);
2681 if (ahd->platform_data->dv_pid != 0) {
2682 ahd->platform_data->flags |= AHD_DV_SHUTDOWN;
2683 ahd_unlock(ahd, &s);
2684 up(&ahd->platform_data->dv_sem);
2685
2686 /*
2687 * Use the eh_sem as an indicator that the
2688 * dv thread is exiting. Note that the dv
2689 * thread must still return after performing
2690 * the up on our semaphore before it has
2691 * completely exited this module. Unfortunately,
2692 * there seems to be no easy way to wait for the
2693 * exit of a thread for which you are not the
2694 * parent (dv threads are parented by init).
2695 * Cross your fingers...
2696 */
2697 down(&ahd->platform_data->eh_sem);
2698
2699 /*
2700 * Mark the dv thread as already dead. This
2701 * avoids attempting to kill it a second time.
2702 * This is necessary because we must kill the
2703 * DV thread before calling ahd_free() in the
2704 * module shutdown case to avoid bogus locking
2705 * in the SCSI mid-layer, but we ahd_free() is
2706 * called without killing the DV thread in the
2707 * instance detach case, so ahd_platform_free()
2708 * calls us again to verify that the DV thread
2709 * is dead.
2710 */
2711 ahd->platform_data->dv_pid = 0;
2712 } else {
2713 ahd_unlock(ahd, &s);
2714 }
2715 }
2716
2717 #define AHD_LINUX_DV_INQ_SHORT_LEN 36
2718 #define AHD_LINUX_DV_INQ_LEN 256
2719 #define AHD_LINUX_DV_TIMEOUT (HZ / 4)
2720
2721 #define AHD_SET_DV_STATE(ahd, targ, newstate) \
2722 ahd_set_dv_state(ahd, targ, newstate, __LINE__)
2723
2724 static __inline void
2725 ahd_set_dv_state(struct ahd_softc *ahd, struct ahd_linux_target *targ,
2726 ahd_dv_state newstate, u_int line)
2727 {
2728 ahd_dv_state oldstate;
2729
2730 oldstate = targ->dv_state;
2731 #ifdef AHD_DEBUG
2732 if (ahd_debug & AHD_SHOW_DV)
2733 printf("%s:%d: Going from state %d to state %d\n",
2734 ahd_name(ahd), line, oldstate, newstate);
2735 #endif
2736
2737 if (oldstate == newstate)
2738 targ->dv_state_retry++;
2739 else
2740 targ->dv_state_retry = 0;
2741 targ->dv_state = newstate;
2742 }
2743
2744 static void
2745 ahd_linux_dv_target(struct ahd_softc *ahd, u_int target_offset)
2746 {
2747 struct ahd_devinfo devinfo;
2748 struct ahd_linux_target *targ;
2749 struct scsi_cmnd *cmd;
2750 struct scsi_device *scsi_dev;
2751 struct scsi_sense_data *sense;
2752 uint8_t *buffer;
2753 u_long s;
2754 u_int timeout;
2755 int echo_size;
2756
2757 sense = NULL;
2758 buffer = NULL;
2759 echo_size = 0;
2760 ahd_lock(ahd, &s);
2761 targ = ahd->platform_data->targets[target_offset];
2762 if (targ == NULL || (targ->flags & AHD_DV_REQUIRED) == 0) {
2763 ahd_unlock(ahd, &s);
2764 return;
2765 }
2766 ahd_compile_devinfo(&devinfo, ahd->our_id, targ->target, /*lun*/0,
2767 targ->channel + 'A', ROLE_INITIATOR);
2768 #ifdef AHD_DEBUG
2769 if (ahd_debug & AHD_SHOW_DV) {
2770 ahd_print_devinfo(ahd, &devinfo);
2771 printf("Performing DV\n");
2772 }
2773 #endif
2774
2775 ahd_unlock(ahd, &s);
2776
2777 cmd = malloc(sizeof(struct scsi_cmnd), M_DEVBUF, M_WAITOK);
2778 scsi_dev = malloc(sizeof(struct scsi_device), M_DEVBUF, M_WAITOK);
2779 scsi_dev->host = ahd->platform_data->host;
2780 scsi_dev->id = devinfo.target;
2781 scsi_dev->lun = devinfo.lun;
2782 scsi_dev->channel = devinfo.channel - 'A';
2783 ahd->platform_data->dv_scsi_dev = scsi_dev;
2784
2785 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_INQ_SHORT_ASYNC);
2786
2787 while (targ->dv_state != AHD_DV_STATE_EXIT) {
2788 timeout = AHD_LINUX_DV_TIMEOUT;
2789 switch (targ->dv_state) {
2790 case AHD_DV_STATE_INQ_SHORT_ASYNC:
2791 case AHD_DV_STATE_INQ_ASYNC:
2792 case AHD_DV_STATE_INQ_ASYNC_VERIFY:
2793 /*
2794 * Set things to async narrow to reduce the
2795 * chance that the INQ will fail.
2796 */
2797 ahd_lock(ahd, &s);
2798 ahd_set_syncrate(ahd, &devinfo, 0, 0, 0,
2799 AHD_TRANS_GOAL, /*paused*/FALSE);
2800 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
2801 AHD_TRANS_GOAL, /*paused*/FALSE);
2802 ahd_unlock(ahd, &s);
2803 timeout = 10 * HZ;
2804 targ->flags &= ~AHD_INQ_VALID;
2805 /* FALLTHROUGH */
2806 case AHD_DV_STATE_INQ_VERIFY:
2807 {
2808 u_int inq_len;
2809
2810 if (targ->dv_state == AHD_DV_STATE_INQ_SHORT_ASYNC)
2811 inq_len = AHD_LINUX_DV_INQ_SHORT_LEN;
2812 else
2813 inq_len = targ->inq_data->additional_length + 5;
2814 ahd_linux_dv_inq(ahd, cmd, &devinfo, targ, inq_len);
2815 break;
2816 }
2817 case AHD_DV_STATE_TUR:
2818 case AHD_DV_STATE_BUSY:
2819 timeout = 5 * HZ;
2820 ahd_linux_dv_tur(ahd, cmd, &devinfo);
2821 break;
2822 case AHD_DV_STATE_REBD:
2823 ahd_linux_dv_rebd(ahd, cmd, &devinfo, targ);
2824 break;
2825 case AHD_DV_STATE_WEB:
2826 ahd_linux_dv_web(ahd, cmd, &devinfo, targ);
2827 break;
2828
2829 case AHD_DV_STATE_REB:
2830 ahd_linux_dv_reb(ahd, cmd, &devinfo, targ);
2831 break;
2832
2833 case AHD_DV_STATE_SU:
2834 ahd_linux_dv_su(ahd, cmd, &devinfo, targ);
2835 timeout = 50 * HZ;
2836 break;
2837
2838 default:
2839 ahd_print_devinfo(ahd, &devinfo);
2840 printf("Unknown DV state %d\n", targ->dv_state);
2841 goto out;
2842 }
2843
2844 /* Queue the command and wait for it to complete */
2845 /* Abuse eh_timeout in the scsi_cmnd struct for our purposes */
2846 init_timer(&cmd->eh_timeout);
2847 #ifdef AHD_DEBUG
2848 if ((ahd_debug & AHD_SHOW_MESSAGES) != 0)
2849 /*
2850 * All of the printfs during negotiation
2851 * really slow down the negotiation.
2852 * Add a bit of time just to be safe.
2853 */
2854 timeout += HZ;
2855 #endif
2856 scsi_add_timer(cmd, timeout, ahd_linux_dv_timeout);
2857 /*
2858 * In 2.5.X, it is assumed that all calls from the
2859 * "midlayer" (which we are emulating) will have the
2860 * ahd host lock held. For other kernels, the
2861 * io_request_lock must be held.
2862 */
2863 #if AHD_SCSI_HAS_HOST_LOCK != 0
2864 ahd_lock(ahd, &s);
2865 #else
2866 spin_lock_irqsave(&io_request_lock, s);
2867 #endif
2868 ahd_linux_queue(cmd, ahd_linux_dv_complete);
2869 #if AHD_SCSI_HAS_HOST_LOCK != 0
2870 ahd_unlock(ahd, &s);
2871 #else
2872 spin_unlock_irqrestore(&io_request_lock, s);
2873 #endif
2874 down_interruptible(&ahd->platform_data->dv_cmd_sem);
2875 /*
2876 * Wait for the SIMQ to be released so that DV is the
2877 * only reason the queue is frozen.
2878 */
2879 ahd_lock(ahd, &s);
2880 while (AHD_DV_SIMQ_FROZEN(ahd) == 0) {
2881 ahd->platform_data->flags |= AHD_DV_WAIT_SIMQ_RELEASE;
2882 ahd_unlock(ahd, &s);
2883 down_interruptible(&ahd->platform_data->dv_sem);
2884 ahd_lock(ahd, &s);
2885 }
2886 ahd_unlock(ahd, &s);
2887
2888 ahd_linux_dv_transition(ahd, cmd, &devinfo, targ);
2889 }
2890
2891 out:
2892 if ((targ->flags & AHD_INQ_VALID) != 0
2893 && ahd_linux_get_device(ahd, devinfo.channel - 'A',
2894 devinfo.target, devinfo.lun,
2895 /*alloc*/FALSE) == NULL) {
2896 /*
2897 * The DV state machine failed to configure this device.
2898 * This is normal if DV is disabled. Since we have inquiry
2899 * data, filter it and use the "optimistic" negotiation
2900 * parameters found in the inquiry string.
2901 */
2902 ahd_linux_filter_inquiry(ahd, &devinfo);
2903 if ((targ->flags & (AHD_BASIC_DV|AHD_ENHANCED_DV)) != 0) {
2904 ahd_print_devinfo(ahd, &devinfo);
2905 printf("DV failed to configure device. "
2906 "Please file a bug report against "
2907 "this driver.\n");
2908 }
2909 }
2910
2911 if (cmd != NULL)
2912 free(cmd, M_DEVBUF);
2913
2914 if (ahd->platform_data->dv_scsi_dev != NULL) {
2915 free(ahd->platform_data->dv_scsi_dev, M_DEVBUF);
2916 ahd->platform_data->dv_scsi_dev = NULL;
2917 }
2918
2919 ahd_lock(ahd, &s);
2920 if (targ->dv_buffer != NULL) {
2921 free(targ->dv_buffer, M_DEVBUF);
2922 targ->dv_buffer = NULL;
2923 }
2924 if (targ->dv_buffer1 != NULL) {
2925 free(targ->dv_buffer1, M_DEVBUF);
2926 targ->dv_buffer1 = NULL;
2927 }
2928 targ->flags &= ~AHD_DV_REQUIRED;
2929 if (targ->refcount == 0)
2930 ahd_linux_free_target(ahd, targ);
2931 ahd_unlock(ahd, &s);
2932 }
2933
2934 static __inline int
2935 ahd_linux_dv_fallback(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
2936 {
2937 u_long s;
2938 int retval;
2939
2940 ahd_lock(ahd, &s);
2941 retval = ahd_linux_fallback(ahd, devinfo);
2942 ahd_unlock(ahd, &s);
2943
2944 return (retval);
2945 }
2946
2947 static void
2948 ahd_linux_dv_transition(struct ahd_softc *ahd, struct scsi_cmnd *cmd,
2949 struct ahd_devinfo *devinfo,
2950 struct ahd_linux_target *targ)
2951 {
2952 u_int32_t status;
2953
2954 status = aic_error_action(cmd, targ->inq_data,
2955 ahd_cmd_get_transaction_status(cmd),
2956 ahd_cmd_get_scsi_status(cmd));
2957
2958
2959 #ifdef AHD_DEBUG
2960 if (ahd_debug & AHD_SHOW_DV) {
2961 ahd_print_devinfo(ahd, devinfo);
2962 printf("Entering ahd_linux_dv_transition, state= %d, "
2963 "status= 0x%x, cmd->result= 0x%x\n", targ->dv_state,
2964 status, cmd->result);
2965 }
2966 #endif
2967
2968 switch (targ->dv_state) {
2969 case AHD_DV_STATE_INQ_SHORT_ASYNC:
2970 case AHD_DV_STATE_INQ_ASYNC:
2971 switch (status & SS_MASK) {
2972 case SS_NOP:
2973 {
2974 AHD_SET_DV_STATE(ahd, targ, targ->dv_state+1);
2975 break;
2976 }
2977 case SS_INQ_REFRESH:
2978 AHD_SET_DV_STATE(ahd, targ,
2979 AHD_DV_STATE_INQ_SHORT_ASYNC);
2980 break;
2981 case SS_TUR:
2982 case SS_RETRY:
2983 AHD_SET_DV_STATE(ahd, targ, targ->dv_state);
2984 if (ahd_cmd_get_transaction_status(cmd)
2985 == CAM_REQUEUE_REQ)
2986 targ->dv_state_retry--;
2987 if ((status & SS_ERRMASK) == EBUSY)
2988 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_BUSY);
2989 if (targ->dv_state_retry < 10)
2990 break;
2991 /* FALLTHROUGH */
2992 default:
2993 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
2994 #ifdef AHD_DEBUG
2995 if (ahd_debug & AHD_SHOW_DV) {
2996 ahd_print_devinfo(ahd, devinfo);
2997 printf("Failed DV inquiry, skipping\n");
2998 }
2999 #endif
3000 break;
3001 }
3002 break;
3003 case AHD_DV_STATE_INQ_ASYNC_VERIFY:
3004 switch (status & SS_MASK) {
3005 case SS_NOP:
3006 {
3007 u_int xportflags;
3008 u_int spi3data;
3009
3010 if (memcmp(targ->inq_data, targ->dv_buffer,
3011 AHD_LINUX_DV_INQ_LEN) != 0) {
3012 /*
3013 * Inquiry data must have changed.
3014 * Try from the top again.
3015 */
3016 AHD_SET_DV_STATE(ahd, targ,
3017 AHD_DV_STATE_INQ_SHORT_ASYNC);
3018 break;
3019 }
3020
3021 AHD_SET_DV_STATE(ahd, targ, targ->dv_state+1);
3022 targ->flags |= AHD_INQ_VALID;
3023 if (ahd_linux_user_dv_setting(ahd) == 0)
3024 break;
3025
3026 xportflags = targ->inq_data->flags;
3027 if ((xportflags & (SID_Sync|SID_WBus16)) == 0)
3028 break;
3029
3030 spi3data = targ->inq_data->spi3data;
3031 switch (spi3data & SID_SPI_CLOCK_DT_ST) {
3032 default:
3033 case SID_SPI_CLOCK_ST:
3034 /* Assume only basic DV is supported. */
3035 targ->flags |= AHD_BASIC_DV;
3036 break;
3037 case SID_SPI_CLOCK_DT:
3038 case SID_SPI_CLOCK_DT_ST:
3039 targ->flags |= AHD_ENHANCED_DV;
3040 break;
3041 }
3042 break;
3043 }
3044 case SS_INQ_REFRESH:
3045 AHD_SET_DV_STATE(ahd, targ,
3046 AHD_DV_STATE_INQ_SHORT_ASYNC);
3047 break;
3048 case SS_TUR:
3049 case SS_RETRY:
3050 AHD_SET_DV_STATE(ahd, targ, targ->dv_state);
3051 if (ahd_cmd_get_transaction_status(cmd)
3052 == CAM_REQUEUE_REQ)
3053 targ->dv_state_retry--;
3054
3055 if ((status & SS_ERRMASK) == EBUSY)
3056 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_BUSY);
3057 if (targ->dv_state_retry < 10)
3058 break;
3059 /* FALLTHROUGH */
3060 default:
3061 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3062 #ifdef AHD_DEBUG
3063 if (ahd_debug & AHD_SHOW_DV) {
3064 ahd_print_devinfo(ahd, devinfo);
3065 printf("Failed DV inquiry, skipping\n");
3066 }
3067 #endif
3068 break;
3069 }
3070 break;
3071 case AHD_DV_STATE_INQ_VERIFY:
3072 switch (status & SS_MASK) {
3073 case SS_NOP:
3074 {
3075
3076 if (memcmp(targ->inq_data, targ->dv_buffer,
3077 AHD_LINUX_DV_INQ_LEN) == 0) {
3078 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3079 break;
3080 }
3081
3082 #ifdef AHD_DEBUG
3083 if (ahd_debug & AHD_SHOW_DV) {
3084 int i;
3085
3086 ahd_print_devinfo(ahd, devinfo);
3087 printf("Inquiry buffer mismatch:");
3088 for (i = 0; i < AHD_LINUX_DV_INQ_LEN; i++) {
3089 if ((i & 0xF) == 0)
3090 printf("\n ");
3091 printf("0x%x:0x0%x ",
3092 ((uint8_t *)targ->inq_data)[i],
3093 targ->dv_buffer[i]);
3094 }
3095 printf("\n");
3096 }
3097 #endif
3098
3099 if (ahd_linux_dv_fallback(ahd, devinfo) != 0) {
3100 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3101 break;
3102 }
3103 /*
3104 * Do not count "falling back"
3105 * against our retries.
3106 */
3107 targ->dv_state_retry = 0;
3108 AHD_SET_DV_STATE(ahd, targ, targ->dv_state);
3109 break;
3110 }
3111 case SS_INQ_REFRESH:
3112 AHD_SET_DV_STATE(ahd, targ,
3113 AHD_DV_STATE_INQ_SHORT_ASYNC);
3114 break;
3115 case SS_TUR:
3116 case SS_RETRY:
3117 AHD_SET_DV_STATE(ahd, targ, targ->dv_state);
3118 if (ahd_cmd_get_transaction_status(cmd)
3119 == CAM_REQUEUE_REQ) {
3120 targ->dv_state_retry--;
3121 } else if ((status & SSQ_FALLBACK) != 0) {
3122 if (ahd_linux_dv_fallback(ahd, devinfo) != 0) {
3123 AHD_SET_DV_STATE(ahd, targ,
3124 AHD_DV_STATE_EXIT);
3125 break;
3126 }
3127 /*
3128 * Do not count "falling back"
3129 * against our retries.
3130 */
3131 targ->dv_state_retry = 0;
3132 } else if ((status & SS_ERRMASK) == EBUSY)
3133 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_BUSY);
3134 if (targ->dv_state_retry < 10)
3135 break;
3136 /* FALLTHROUGH */
3137 default:
3138 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3139 #ifdef AHD_DEBUG
3140 if (ahd_debug & AHD_SHOW_DV) {
3141 ahd_print_devinfo(ahd, devinfo);
3142 printf("Failed DV inquiry, skipping\n");
3143 }
3144 #endif
3145 break;
3146 }
3147 break;
3148
3149 case AHD_DV_STATE_TUR:
3150 switch (status & SS_MASK) {
3151 case SS_NOP:
3152 if ((targ->flags & AHD_BASIC_DV) != 0) {
3153 ahd_linux_filter_inquiry(ahd, devinfo);
3154 AHD_SET_DV_STATE(ahd, targ,
3155 AHD_DV_STATE_INQ_VERIFY);
3156 } else if ((targ->flags & AHD_ENHANCED_DV) != 0) {
3157 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_REBD);
3158 } else {
3159 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3160 }
3161 break;
3162 case SS_RETRY:
3163 case SS_TUR:
3164 if ((status & SS_ERRMASK) == EBUSY) {
3165 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_BUSY);
3166 break;
3167 }
3168 AHD_SET_DV_STATE(ahd, targ, targ->dv_state);
3169 if (ahd_cmd_get_transaction_status(cmd)
3170 == CAM_REQUEUE_REQ) {
3171 targ->dv_state_retry--;
3172 } else if ((status & SSQ_FALLBACK) != 0) {
3173 if (ahd_linux_dv_fallback(ahd, devinfo) != 0) {
3174 AHD_SET_DV_STATE(ahd, targ,
3175 AHD_DV_STATE_EXIT);
3176 break;
3177 }
3178 /*
3179 * Do not count "falling back"
3180 * against our retries.
3181 */
3182 targ->dv_state_retry = 0;
3183 }
3184 if (targ->dv_state_retry >= 10) {
3185 #ifdef AHD_DEBUG
3186 if (ahd_debug & AHD_SHOW_DV) {
3187 ahd_print_devinfo(ahd, devinfo);
3188 printf("DV TUR reties exhausted\n");
3189 }
3190 #endif
3191 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3192 break;
3193 }
3194 if (status & SSQ_DELAY)
3195 scsi_sleep(1 * HZ);
3196
3197 break;
3198 case SS_START:
3199 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_SU);
3200 break;
3201 case SS_INQ_REFRESH:
3202 AHD_SET_DV_STATE(ahd, targ,
3203 AHD_DV_STATE_INQ_SHORT_ASYNC);
3204 break;
3205 default:
3206 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3207 break;
3208 }
3209 break;
3210
3211 case AHD_DV_STATE_REBD:
3212 switch (status & SS_MASK) {
3213 case SS_NOP:
3214 {
3215 uint32_t echo_size;
3216
3217 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_WEB);
3218 echo_size = scsi_3btoul(&targ->dv_buffer[1]);
3219 echo_size &= 0x1FFF;
3220 #ifdef AHD_DEBUG
3221 if (ahd_debug & AHD_SHOW_DV) {
3222 ahd_print_devinfo(ahd, devinfo);
3223 printf("Echo buffer size= %d\n", echo_size);
3224 }
3225 #endif
3226 if (echo_size == 0) {
3227 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3228 break;
3229 }
3230
3231 /* Generate the buffer pattern */
3232 targ->dv_echo_size = echo_size;
3233 ahd_linux_generate_dv_pattern(targ);
3234 /*
3235 * Setup initial negotiation values.
3236 */
3237 ahd_linux_filter_inquiry(ahd, devinfo);
3238 break;
3239 }
3240 case SS_INQ_REFRESH:
3241 AHD_SET_DV_STATE(ahd, targ,
3242 AHD_DV_STATE_INQ_SHORT_ASYNC);
3243 break;
3244 case SS_RETRY:
3245 AHD_SET_DV_STATE(ahd, targ, targ->dv_state);
3246 if (ahd_cmd_get_transaction_status(cmd)
3247 == CAM_REQUEUE_REQ)
3248 targ->dv_state_retry--;
3249 if (targ->dv_state_retry <= 10)
3250 break;
3251 #ifdef AHD_DEBUG
3252 if (ahd_debug & AHD_SHOW_DV) {
3253 ahd_print_devinfo(ahd, devinfo);
3254 printf("DV REBD reties exhausted\n");
3255 }
3256 #endif
3257 /* FALLTHROUGH */
3258 case SS_FATAL:
3259 default:
3260 /*
3261 * Setup initial negotiation values
3262 * and try level 1 DV.
3263 */
3264 ahd_linux_filter_inquiry(ahd, devinfo);
3265 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_INQ_VERIFY);
3266 targ->dv_echo_size = 0;
3267 break;
3268 }
3269 break;
3270
3271 case AHD_DV_STATE_WEB:
3272 switch (status & SS_MASK) {
3273 case SS_NOP:
3274 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_REB);
3275 break;
3276 case SS_INQ_REFRESH:
3277 AHD_SET_DV_STATE(ahd, targ,
3278 AHD_DV_STATE_INQ_SHORT_ASYNC);
3279 break;
3280 case SS_RETRY:
3281 AHD_SET_DV_STATE(ahd, targ, targ->dv_state);
3282 if (ahd_cmd_get_transaction_status(cmd)
3283 == CAM_REQUEUE_REQ) {
3284 targ->dv_state_retry--;
3285 } else if ((status & SSQ_FALLBACK) != 0) {
3286 if (ahd_linux_dv_fallback(ahd, devinfo) != 0) {
3287 AHD_SET_DV_STATE(ahd, targ,
3288 AHD_DV_STATE_EXIT);
3289 break;
3290 }
3291 /*
3292 * Do not count "falling back"
3293 * against our retries.
3294 */
3295 targ->dv_state_retry = 0;
3296 }
3297 if (targ->dv_state_retry <= 10)
3298 break;
3299 /* FALLTHROUGH */
3300 #ifdef AHD_DEBUG
3301 if (ahd_debug & AHD_SHOW_DV) {
3302 ahd_print_devinfo(ahd, devinfo);
3303 printf("DV WEB reties exhausted\n");
3304 }
3305 #endif
3306 default:
3307 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3308 break;
3309 }
3310 break;
3311
3312 case AHD_DV_STATE_REB:
3313 switch (status & SS_MASK) {
3314 case SS_NOP:
3315 if (memcmp(targ->dv_buffer, targ->dv_buffer1,
3316 targ->dv_echo_size) != 0) {
3317 if (ahd_linux_dv_fallback(ahd, devinfo) != 0)
3318 AHD_SET_DV_STATE(ahd, targ,
3319 AHD_DV_STATE_EXIT);
3320 else
3321 AHD_SET_DV_STATE(ahd, targ,
3322 AHD_DV_STATE_WEB);
3323 break;
3324 }
3325
3326 if (targ->dv_buffer != NULL) {
3327 free(targ->dv_buffer, M_DEVBUF);
3328 targ->dv_buffer = NULL;
3329 }
3330 if (targ->dv_buffer1 != NULL) {
3331 free(targ->dv_buffer1, M_DEVBUF);
3332 targ->dv_buffer1 = NULL;
3333 }
3334 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3335 break;
3336 case SS_INQ_REFRESH:
3337 AHD_SET_DV_STATE(ahd, targ,
3338 AHD_DV_STATE_INQ_SHORT_ASYNC);
3339 break;
3340 case SS_RETRY:
3341 AHD_SET_DV_STATE(ahd, targ, targ->dv_state);
3342 if (ahd_cmd_get_transaction_status(cmd)
3343 == CAM_REQUEUE_REQ) {
3344 targ->dv_state_retry--;
3345 } else if ((status & SSQ_FALLBACK) != 0) {
3346 if (ahd_linux_dv_fallback(ahd, devinfo) != 0) {
3347 AHD_SET_DV_STATE(ahd, targ,
3348 AHD_DV_STATE_EXIT);
3349 break;
3350 }
3351 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_WEB);
3352 }
3353 if (targ->dv_state_retry <= 10) {
3354 if ((status & (SSQ_DELAY_RANDOM|SSQ_DELAY))!= 0)
3355 scsi_sleep(ahd->our_id*HZ/10);
3356 break;
3357 }
3358 #ifdef AHD_DEBUG
3359 if (ahd_debug & AHD_SHOW_DV) {
3360 ahd_print_devinfo(ahd, devinfo);
3361 printf("DV REB reties exhausted\n");
3362 }
3363 #endif
3364 /* FALLTHROUGH */
3365 default:
3366 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3367 break;
3368 }
3369 break;
3370
3371 case AHD_DV_STATE_SU:
3372 switch (status & SS_MASK) {
3373 case SS_NOP:
3374 case SS_INQ_REFRESH:
3375 AHD_SET_DV_STATE(ahd, targ,
3376 AHD_DV_STATE_INQ_SHORT_ASYNC);
3377 break;
3378 default:
3379 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3380 break;
3381 }
3382 break;
3383
3384 case AHD_DV_STATE_BUSY:
3385 switch (status & SS_MASK) {
3386 case SS_NOP:
3387 case SS_INQ_REFRESH:
3388 AHD_SET_DV_STATE(ahd, targ,
3389 AHD_DV_STATE_INQ_SHORT_ASYNC);
3390 break;
3391 case SS_TUR:
3392 case SS_RETRY:
3393 AHD_SET_DV_STATE(ahd, targ, targ->dv_state);
3394 if (ahd_cmd_get_transaction_status(cmd)
3395 == CAM_REQUEUE_REQ) {
3396 targ->dv_state_retry--;
3397 } else if (targ->dv_state_retry < 60) {
3398 if ((status & SSQ_DELAY) != 0)
3399 scsi_sleep(1 * HZ);
3400 } else {
3401 #ifdef AHD_DEBUG
3402 if (ahd_debug & AHD_SHOW_DV) {
3403 ahd_print_devinfo(ahd, devinfo);
3404 printf("DV BUSY reties exhausted\n");
3405 }
3406 #endif
3407 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3408 }
3409 break;
3410 default:
3411 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3412 break;
3413 }
3414 break;
3415
3416 default:
3417 printf("%s: Invalid DV completion state %d\n", ahd_name(ahd),
3418 targ->dv_state);
3419 AHD_SET_DV_STATE(ahd, targ, AHD_DV_STATE_EXIT);
3420 break;
3421 }
3422 }
3423
3424 static void
3425 ahd_linux_dv_fill_cmd(struct ahd_softc *ahd, struct scsi_cmnd *cmd,
3426 struct ahd_devinfo *devinfo)
3427 {
3428 memset(cmd, 0, sizeof(struct scsi_cmnd));
3429 cmd->device = ahd->platform_data->dv_scsi_dev;
3430 cmd->scsi_done = ahd_linux_dv_complete;
3431 }
3432
3433 /*
3434 * Synthesize an inquiry command. On the return trip, it'll be
3435 * sniffed and the device transfer settings set for us.
3436 */
3437 static void
3438 ahd_linux_dv_inq(struct ahd_softc *ahd, struct scsi_cmnd *cmd,
3439 struct ahd_devinfo *devinfo, struct ahd_linux_target *targ,
3440 u_int request_length)
3441 {
3442
3443 #ifdef AHD_DEBUG
3444 if (ahd_debug & AHD_SHOW_DV) {
3445 ahd_print_devinfo(ahd, devinfo);
3446 printf("Sending INQ\n");
3447 }
3448 #endif
3449 if (targ->inq_data == NULL)
3450 targ->inq_data = malloc(AHD_LINUX_DV_INQ_LEN,
3451 M_DEVBUF, M_WAITOK);
3452 if (targ->dv_state > AHD_DV_STATE_INQ_ASYNC) {
3453 if (targ->dv_buffer != NULL)
3454 free(targ->dv_buffer, M_DEVBUF);
3455 targ->dv_buffer = malloc(AHD_LINUX_DV_INQ_LEN,
3456 M_DEVBUF, M_WAITOK);
3457 }
3458
3459 ahd_linux_dv_fill_cmd(ahd, cmd, devinfo);
3460 cmd->sc_data_direction = SCSI_DATA_READ;
3461 cmd->cmd_len = 6;
3462 cmd->cmnd[0] = INQUIRY;
3463 cmd->cmnd[4] = request_length;
3464 cmd->request_bufflen = request_length;
3465 if (targ->dv_state > AHD_DV_STATE_INQ_ASYNC)
3466 cmd->request_buffer = targ->dv_buffer;
3467 else
3468 cmd->request_buffer = targ->inq_data;
3469 memset(cmd->request_buffer, 0, AHD_LINUX_DV_INQ_LEN);
3470 }
3471
3472 static void
3473 ahd_linux_dv_tur(struct ahd_softc *ahd, struct scsi_cmnd *cmd,
3474 struct ahd_devinfo *devinfo)
3475 {
3476
3477 #ifdef AHD_DEBUG
3478 if (ahd_debug & AHD_SHOW_DV) {
3479 ahd_print_devinfo(ahd, devinfo);
3480 printf("Sending TUR\n");
3481 }
3482 #endif
3483 /* Do a TUR to clear out any non-fatal transitional state */
3484 ahd_linux_dv_fill_cmd(ahd, cmd, devinfo);
3485 cmd->sc_data_direction = SCSI_DATA_NONE;
3486 cmd->cmd_len = 6;
3487 cmd->cmnd[0] = TEST_UNIT_READY;
3488 }
3489
3490 #define AHD_REBD_LEN 4
3491
3492 static void
3493 ahd_linux_dv_rebd(struct ahd_softc *ahd, struct scsi_cmnd *cmd,
3494 struct ahd_devinfo *devinfo, struct ahd_linux_target *targ)
3495 {
3496
3497 #ifdef AHD_DEBUG
3498 if (ahd_debug & AHD_SHOW_DV) {
3499 ahd_print_devinfo(ahd, devinfo);
3500 printf("Sending REBD\n");
3501 }
3502 #endif
3503 if (targ->dv_buffer != NULL)
3504 free(targ->dv_buffer, M_DEVBUF);
3505 targ->dv_buffer = malloc(AHD_REBD_LEN, M_DEVBUF, M_WAITOK);
3506 ahd_linux_dv_fill_cmd(ahd, cmd, devinfo);
3507 cmd->sc_data_direction = SCSI_DATA_READ;
3508 cmd->cmd_len = 10;
3509 cmd->cmnd[0] = READ_BUFFER;
3510 cmd->cmnd[1] = 0x0b;
3511 scsi_ulto3b(AHD_REBD_LEN, &cmd->cmnd[6]);
3512 cmd->request_bufflen = AHD_REBD_LEN;
3513 cmd->underflow = cmd->request_bufflen;
3514 cmd->request_buffer = targ->dv_buffer;
3515 }
3516
3517 static void
3518 ahd_linux_dv_web(struct ahd_softc *ahd, struct scsi_cmnd *cmd,
3519 struct ahd_devinfo *devinfo, struct ahd_linux_target *targ)
3520 {
3521
3522 #ifdef AHD_DEBUG
3523 if (ahd_debug & AHD_SHOW_DV) {
3524 ahd_print_devinfo(ahd, devinfo);
3525 printf("Sending WEB\n");
3526 }
3527 #endif
3528 ahd_linux_dv_fill_cmd(ahd, cmd, devinfo);
3529 cmd->sc_data_direction = SCSI_DATA_WRITE;
3530 cmd->cmd_len = 10;
3531 cmd->cmnd[0] = WRITE_BUFFER;
3532 cmd->cmnd[1] = 0x0a;
3533 scsi_ulto3b(targ->dv_echo_size, &cmd->cmnd[6]);
3534 cmd->request_bufflen = targ->dv_echo_size;
3535 cmd->underflow = cmd->request_bufflen;
3536 cmd->request_buffer = targ->dv_buffer;
3537 }
3538
3539 static void
3540 ahd_linux_dv_reb(struct ahd_softc *ahd, struct scsi_cmnd *cmd,
3541 struct ahd_devinfo *devinfo, struct ahd_linux_target *targ)
3542 {
3543
3544 #ifdef AHD_DEBUG
3545 if (ahd_debug & AHD_SHOW_DV) {
3546 ahd_print_devinfo(ahd, devinfo);
3547 printf("Sending REB\n");
3548 }
3549 #endif
3550 ahd_linux_dv_fill_cmd(ahd, cmd, devinfo);
3551 cmd->sc_data_direction = SCSI_DATA_READ;
3552 cmd->cmd_len = 10;
3553 cmd->cmnd[0] = READ_BUFFER;
3554 cmd->cmnd[1] = 0x0a;
3555 scsi_ulto3b(targ->dv_echo_size, &cmd->cmnd[6]);
3556 cmd->request_bufflen = targ->dv_echo_size;
3557 cmd->underflow = cmd->request_bufflen;
3558 cmd->request_buffer = targ->dv_buffer1;
3559 }
3560
3561 static void
3562 ahd_linux_dv_su(struct ahd_softc *ahd, struct scsi_cmnd *cmd,
3563 struct ahd_devinfo *devinfo,
3564 struct ahd_linux_target *targ)
3565 {
3566 u_int le;
3567
3568 le = SID_IS_REMOVABLE(targ->inq_data) ? SSS_LOEJ : 0;
3569
3570 #ifdef AHD_DEBUG
3571 if (ahd_debug & AHD_SHOW_DV) {
3572 ahd_print_devinfo(ahd, devinfo);
3573 printf("Sending SU\n");
3574 }
3575 #endif
3576 ahd_linux_dv_fill_cmd(ahd, cmd, devinfo);
3577 cmd->sc_data_direction = SCSI_DATA_NONE;
3578 cmd->cmd_len = 6;
3579 cmd->cmnd[0] = START_STOP_UNIT;
3580 cmd->cmnd[4] = le | SSS_START;
3581 }
3582
3583 static int
3584 ahd_linux_fallback(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3585 {
3586 struct ahd_linux_target *targ;
3587 struct ahd_initiator_tinfo *tinfo;
3588 struct ahd_transinfo *goal;
3589 struct ahd_tmode_tstate *tstate;
3590 u_int width;
3591 u_int period;
3592 u_int offset;
3593 u_int ppr_options;
3594 u_int cur_speed;
3595 u_int wide_speed;
3596 u_int narrow_speed;
3597 u_int fallback_speed;
3598
3599 #ifdef AHD_DEBUG
3600 if (ahd_debug & AHD_SHOW_DV) {
3601 ahd_print_devinfo(ahd, devinfo);
3602 printf("Trying to fallback\n");
3603 }
3604 #endif
3605 targ = ahd->platform_data->targets[devinfo->target_offset];
3606 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel,
3607 devinfo->our_scsiid,
3608 devinfo->target, &tstate);
3609 goal = &tinfo->goal;
3610 width = goal->width;
3611 period = goal->period;
3612 offset = goal->offset;
3613 ppr_options = goal->ppr_options;
3614 if (offset == 0)
3615 period = AHD_ASYNC_XFER_PERIOD;
3616 if (targ->dv_next_narrow_period == 0)
3617 targ->dv_next_narrow_period = MAX(period, AHD_SYNCRATE_ULTRA2);
3618 if (targ->dv_next_wide_period == 0)
3619 targ->dv_next_wide_period = period;
3620 if (targ->dv_max_width == 0)
3621 targ->dv_max_width = width;
3622 if (targ->dv_max_ppr_options == 0)
3623 targ->dv_max_ppr_options = ppr_options;
3624 if (targ->dv_last_ppr_options == 0)
3625 targ->dv_last_ppr_options = ppr_options;
3626
3627 cur_speed = aic_calc_speed(width, period, offset, AHD_SYNCRATE_MIN);
3628 wide_speed = aic_calc_speed(MSG_EXT_WDTR_BUS_16_BIT,
3629 targ->dv_next_wide_period,
3630 MAX_OFFSET, AHD_SYNCRATE_MIN);
3631 narrow_speed = aic_calc_speed(MSG_EXT_WDTR_BUS_8_BIT,
3632 targ->dv_next_narrow_period,
3633 MAX_OFFSET, AHD_SYNCRATE_MIN);
3634 fallback_speed = aic_calc_speed(width, period+1, offset,
3635 AHD_SYNCRATE_MIN);
3636 #ifdef AHD_DEBUG
3637 if (ahd_debug & AHD_SHOW_DV) {
3638 printf("cur_speed= %d, wide_speed= %d, narrow_speed= %d, "
3639 "fallback_speed= %d\n", cur_speed, wide_speed,
3640 narrow_speed, fallback_speed);
3641 }
3642 #endif
3643
3644 if (cur_speed > 160000) {
3645 /*
3646 * Paced/DT/IU_REQ only transfer speeds. All we
3647 * can do is fallback in terms of syncrate.
3648 */
3649 period++;
3650 } else if (cur_speed > 80000) {
3651 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3652 /*
3653 * Try without IU_REQ as it may be confusing
3654 * an expander.
3655 */
3656 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
3657 } else {
3658 /*
3659 * Paced/DT only transfer speeds. All we
3660 * can do is fallback in terms of syncrate.
3661 */
3662 period++;
3663 ppr_options = targ->dv_max_ppr_options;
3664 }
3665 } else if (cur_speed > 3300) {
3666
3667 /*
3668 * In this range we the following
3669 * options ordered from highest to
3670 * lowest desireability:
3671 *
3672 * o Wide/DT
3673 * o Wide/non-DT
3674 * o Narrow at a potentally higher sync rate.
3675 *
3676 * All modes are tested with and without IU_REQ
3677 * set since using IUs may confuse an expander.
3678 */
3679 if ((ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
3680
3681 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
3682 } else if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0) {
3683 /*
3684 * Try going non-DT.
3685 */
3686 ppr_options = targ->dv_max_ppr_options;
3687 ppr_options &= ~MSG_EXT_PPR_DT_REQ;
3688 } else if (targ->dv_last_ppr_options != 0) {
3689 /*
3690 * Try without QAS or any other PPR options.
3691 * We may need a non-PPR message to work with
3692 * an expander. We look at the "last PPR options"
3693 * so we will perform this fallback even if the
3694 * target responded to our PPR negotiation with
3695 * no option bits set.
3696 */
3697 ppr_options = 0;
3698 } else if (width == MSG_EXT_WDTR_BUS_16_BIT) {
3699 /*
3700 * If the next narrow speed is greater than
3701 * the next wide speed, fallback to narrow.
3702 * Otherwise fallback to the next DT/Wide setting.
3703 * The narrow async speed will always be smaller
3704 * than the wide async speed, so handle this case
3705 * specifically.
3706 */
3707 ppr_options = targ->dv_max_ppr_options;
3708 if (narrow_speed > fallback_speed
3709 || period >= AHD_ASYNC_XFER_PERIOD) {
3710 targ->dv_next_wide_period = period+1;
3711 width = MSG_EXT_WDTR_BUS_8_BIT;
3712 period = targ->dv_next_narrow_period;
3713 } else {
3714 period++;
3715 }
3716 } else if ((ahd->features & AHD_WIDE) != 0
3717 && targ->dv_max_width != 0
3718 && wide_speed >= fallback_speed
3719 && (targ->dv_next_wide_period <= AHD_ASYNC_XFER_PERIOD
3720 || period >= AHD_ASYNC_XFER_PERIOD)) {
3721
3722 /*
3723 * We are narrow. Try falling back
3724 * to the next wide speed with
3725 * all supported ppr options set.
3726 */
3727 targ->dv_next_narrow_period = period+1;
3728 width = MSG_EXT_WDTR_BUS_16_BIT;
3729 period = targ->dv_next_wide_period;
3730 ppr_options = targ->dv_max_ppr_options;
3731 } else {
3732 /* Only narrow fallback is allowed. */
3733 period++;
3734 ppr_options = targ->dv_max_ppr_options;
3735 }
3736 } else {
3737 return (-1);
3738 }
3739 offset = MAX_OFFSET;
3740 ahd_find_syncrate(ahd, &period, &ppr_options, AHD_SYNCRATE_PACED);
3741 ahd_set_width(ahd, devinfo, width, AHD_TRANS_GOAL, FALSE);
3742 if (period == 0) {
3743 period = 0;
3744 offset = 0;
3745 ppr_options = 0;
3746 if (width == MSG_EXT_WDTR_BUS_8_BIT)
3747 targ->dv_next_narrow_period = AHD_ASYNC_XFER_PERIOD;
3748 else
3749 targ->dv_next_wide_period = AHD_ASYNC_XFER_PERIOD;
3750 }
3751 ahd_set_syncrate(ahd, devinfo, period, offset,
3752 ppr_options, AHD_TRANS_GOAL, FALSE);
3753 targ->dv_last_ppr_options = ppr_options;
3754 return (0);
3755 }
3756
3757 static void
3758 ahd_linux_dv_timeout(struct scsi_cmnd *cmd)
3759 {
3760 struct ahd_softc *ahd;
3761 struct scb *scb;
3762 u_long flags;
3763
3764 ahd = *((struct ahd_softc **)cmd->device->host->hostdata);
3765 ahd_lock(ahd, &flags);
3766
3767 #ifdef AHD_DEBUG
3768 if (ahd_debug & AHD_SHOW_DV) {
3769 printf("%s: Timeout while doing DV command %x.\n",
3770 ahd_name(ahd), cmd->cmnd[0]);
3771 ahd_dump_card_state(ahd);
3772 }
3773 #endif
3774
3775 /*
3776 * Guard against "done race". No action is
3777 * required if we just completed.
3778 */
3779 if ((scb = (struct scb *)cmd->host_scribble) == NULL) {
3780 ahd_unlock(ahd, &flags);
3781 return;
3782 }
3783
3784 /*
3785 * Command has not completed. Mark this
3786 * SCB as having failing status prior to
3787 * resetting the bus, so we get the correct
3788 * error code.
3789 */
3790 if ((scb->flags & SCB_SENSE) != 0)
3791 ahd_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
3792 else
3793 ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
3794 ahd_reset_channel(ahd, cmd->device->channel + 'A', /*initiate*/TRUE);
3795
3796 /*
3797 * Add a minimal bus settle delay for devices that are slow to
3798 * respond after bus resets.
3799 */
3800 ahd_freeze_simq(ahd);
3801 init_timer(&ahd->platform_data->reset_timer);
3802 ahd->platform_data->reset_timer.data = (u_long)ahd;
3803 ahd->platform_data->reset_timer.expires = jiffies + HZ / 2;
3804 ahd->platform_data->reset_timer.function =
3805 (ahd_linux_callback_t *)ahd_release_simq;
3806 add_timer(&ahd->platform_data->reset_timer);
3807 if (ahd_linux_next_device_to_run(ahd) != NULL)
3808 ahd_schedule_runq(ahd);
3809 ahd_linux_run_complete_queue(ahd);
3810 ahd_unlock(ahd, &flags);
3811 }
3812
3813 static void
3814 ahd_linux_dv_complete(struct scsi_cmnd *cmd)
3815 {
3816 struct ahd_softc *ahd;
3817
3818 ahd = *((struct ahd_softc **)cmd->device->host->hostdata);
3819
3820 /* Delete the DV timer before it goes off! */
3821 scsi_delete_timer(cmd);
3822
3823 #ifdef AHD_DEBUG
3824 if (ahd_debug & AHD_SHOW_DV)
3825 printf("%s:%c:%d: Command completed, status= 0x%x\n",
3826 ahd_name(ahd), cmd->device->channel, cmd->device->id,
3827 cmd->result);
3828 #endif
3829
3830 /* Wake up the state machine */
3831 up(&ahd->platform_data->dv_cmd_sem);
3832 }
3833
3834 static void
3835 ahd_linux_generate_dv_pattern(struct ahd_linux_target *targ)
3836 {
3837 uint16_t b;
3838 u_int i;
3839 u_int j;
3840
3841 if (targ->dv_buffer != NULL)
3842 free(targ->dv_buffer, M_DEVBUF);
3843 targ->dv_buffer = malloc(targ->dv_echo_size, M_DEVBUF, M_WAITOK);
3844 if (targ->dv_buffer1 != NULL)
3845 free(targ->dv_buffer1, M_DEVBUF);
3846 targ->dv_buffer1 = malloc(targ->dv_echo_size, M_DEVBUF, M_WAITOK);
3847
3848 i = 0;
3849
3850 b = 0x0001;
3851 for (j = 0 ; i < targ->dv_echo_size; j++) {
3852 if (j < 32) {
3853 /*
3854 * 32bytes of sequential numbers.
3855 */
3856 targ->dv_buffer[i++] = j & 0xff;
3857 } else if (j < 48) {
3858 /*
3859 * 32bytes of repeating 0x0000, 0xffff.
3860 */
3861 targ->dv_buffer[i++] = (j & 0x02) ? 0xff : 0x00;
3862 } else if (j < 64) {
3863 /*
3864 * 32bytes of repeating 0x5555, 0xaaaa.
3865 */
3866 targ->dv_buffer[i++] = (j & 0x02) ? 0xaa : 0x55;
3867 } else {
3868 /*
3869 * Remaining buffer is filled with a repeating
3870 * patter of:
3871 *
3872 * 0xffff
3873 * ~0x0001 << shifted once in each loop.
3874 */
3875 if (j & 0x02) {
3876 if (j & 0x01) {
3877 targ->dv_buffer[i++] = ~(b >> 8) & 0xff;
3878 b <<= 1;
3879 if (b == 0x0000)
3880 b = 0x0001;
3881 } else {
3882 targ->dv_buffer[i++] = (~b & 0xff);
3883 }
3884 } else {
3885 targ->dv_buffer[i++] = 0xff;
3886 }
3887 }
3888 }
3889 }
3890
3891 static u_int
3892 ahd_linux_user_tagdepth(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
3893 {
3894 static int warned_user;
3895 u_int tags;
3896
3897 tags = 0;
3898 if ((ahd->user_discenable & devinfo->target_mask) != 0) {
3899 if (ahd->unit >= NUM_ELEMENTS(aic79xx_tag_info)) {
3900
3901 if (warned_user == 0) {
3902 printf(KERN_WARNING
3903 "aic79xx: WARNING: Insufficient tag_info instances\n"
3904 "aic79xx: for installed controllers. Using defaults\n"
3905 "aic79xx: Please update the aic79xx_tag_info array in\n"
3906 "aic79xx: the aic79xx_osm.c source file.\n");
3907 warned_user++;
3908 }
3909 tags = AHD_MAX_QUEUE;
3910 } else {
3911 adapter_tag_info_t *tag_info;
3912
3913 tag_info = &aic79xx_tag_info[ahd->unit];
3914 tags = tag_info->tag_commands[devinfo->target_offset];
3915 if (tags > AHD_MAX_QUEUE)
3916 tags = AHD_MAX_QUEUE;
3917 }
3918 }
3919 return (tags);
3920 }
3921
3922 static u_int
3923 ahd_linux_user_dv_setting(struct ahd_softc *ahd)
3924 {
3925 static int warned_user;
3926 int dv;
3927
3928 if (ahd->unit >= NUM_ELEMENTS(aic79xx_dv_settings)) {
3929
3930 if (warned_user == 0) {
3931 printf(KERN_WARNING
3932 "aic79xx: WARNING: Insufficient dv settings instances\n"
3933 "aic79xx: for installed controllers. Using defaults\n"
3934 "aic79xx: Please update the aic79xx_dv_settings array in"
3935 "aic79xx: the aic79xx_osm.c source file.\n");
3936 warned_user++;
3937 }
3938 dv = -1;
3939 } else {
3940
3941 dv = aic79xx_dv_settings[ahd->unit];
3942 }
3943
3944 if (dv < 0) {
3945 /*
3946 * Apply the default.
3947 */
3948 dv = 1;
3949 if (ahd->seep_config != 0)
3950 dv = (ahd->seep_config->bios_control & CFENABLEDV);
3951 }
3952 return (dv);
3953 }
3954
3955 static void
3956 ahd_linux_setup_user_rd_strm_settings(struct ahd_softc *ahd)
3957 {
3958 static int warned_user;
3959 u_int rd_strm_mask;
3960 u_int target_id;
3961
3962 /*
3963 * If we have specific read streaming info for this controller,
3964 * apply it. Otherwise use the defaults.
3965 */
3966 if (ahd->unit >= NUM_ELEMENTS(aic79xx_rd_strm_info)) {
3967
3968 if (warned_user == 0) {
3969
3970 printf(KERN_WARNING
3971 "aic79xx: WARNING: Insufficient rd_strm instances\n"
3972 "aic79xx: for installed controllers. Using defaults\n"
3973 "aic79xx: Please update the aic79xx_rd_strm_info array\n"
3974 "aic79xx: in the aic79xx_osm.c source file.\n");
3975 warned_user++;
3976 }
3977 rd_strm_mask = AIC79XX_CONFIGED_RD_STRM;
3978 } else {
3979
3980 rd_strm_mask = aic79xx_rd_strm_info[ahd->unit];
3981 }
3982 for (target_id = 0; target_id < 16; target_id++) {
3983 struct ahd_devinfo devinfo;
3984 struct ahd_initiator_tinfo *tinfo;
3985 struct ahd_tmode_tstate *tstate;
3986
3987 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
3988 target_id, &tstate);
3989 ahd_compile_devinfo(&devinfo, ahd->our_id, target_id,
3990 CAM_LUN_WILDCARD, 'A', ROLE_INITIATOR);
3991 tinfo->user.ppr_options &= ~MSG_EXT_PPR_RD_STRM;
3992 if ((rd_strm_mask & devinfo.target_mask) != 0)
3993 tinfo->user.ppr_options |= MSG_EXT_PPR_RD_STRM;
3994 }
3995 }
3996
3997 /*
3998 * Determines the queue depth for a given device.
3999 */
4000 static void
4001 ahd_linux_device_queue_depth(struct ahd_softc *ahd,
4002 struct ahd_linux_device *dev)
4003 {
4004 struct ahd_devinfo devinfo;
4005 u_int tags;
4006
4007 ahd_compile_devinfo(&devinfo,
4008 ahd->our_id,
4009 dev->target->target, dev->lun,
4010 dev->target->channel == 0 ? 'A' : 'B',
4011 ROLE_INITIATOR);
4012 tags = ahd_linux_user_tagdepth(ahd, &devinfo);
4013 if (tags != 0
4014 && dev->scsi_device != NULL
4015 && dev->scsi_device->tagged_supported != 0) {
4016
4017 ahd_set_tags(ahd, &devinfo, AHD_QUEUE_TAGGED);
4018 ahd_print_devinfo(ahd, &devinfo);
4019 printf("Tagged Queuing enabled. Depth %d\n", tags);
4020 } else {
4021 ahd_set_tags(ahd, &devinfo, AHD_QUEUE_NONE);
4022 }
4023 }
4024
4025 static void
4026 ahd_linux_run_device_queue(struct ahd_softc *ahd, struct ahd_linux_device *dev)
4027 {
4028 struct ahd_cmd *acmd;
4029 struct scsi_cmnd *cmd;
4030 struct scb *scb;
4031 struct hardware_scb *hscb;
4032 struct ahd_initiator_tinfo *tinfo;
4033 struct ahd_tmode_tstate *tstate;
4034 u_int col_idx;
4035 uint16_t mask;
4036
4037 if ((dev->flags & AHD_DEV_ON_RUN_LIST) != 0)
4038 panic("running device on run list");
4039
4040 while ((acmd = TAILQ_FIRST(&dev->busyq)) != NULL
4041 && dev->openings > 0 && dev->qfrozen == 0) {
4042
4043 /*
4044 * Schedule us to run later. The only reason we are not
4045 * running is because the whole controller Q is frozen.
4046 */
4047 if (ahd->platform_data->qfrozen != 0
4048 && AHD_DV_SIMQ_FROZEN(ahd) == 0) {
4049
4050 TAILQ_INSERT_TAIL(&ahd->platform_data->device_runq,
4051 dev, links);
4052 dev->flags |= AHD_DEV_ON_RUN_LIST;
4053 return;
4054 }
4055
4056 cmd = &acmd_scsi_cmd(acmd);
4057
4058 /*
4059 * Get an scb to use.
4060 */
4061 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
4062 cmd->device->id, &tstate);
4063 if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) == 0
4064 || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
4065 col_idx = AHD_NEVER_COL_IDX;
4066 } else {
4067 col_idx = AHD_BUILD_COL_IDX(cmd->device->id,
4068 cmd->device->lun);
4069 }
4070 if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
4071 TAILQ_INSERT_TAIL(&ahd->platform_data->device_runq,
4072 dev, links);
4073 dev->flags |= AHD_DEV_ON_RUN_LIST;
4074 ahd->flags |= AHD_RESOURCE_SHORTAGE;
4075 return;
4076 }
4077 TAILQ_REMOVE(&dev->busyq, acmd, acmd_links.tqe);
4078 scb->io_ctx = cmd;
4079 scb->platform_data->dev = dev;
4080 hscb = scb->hscb;
4081 cmd->host_scribble = (char *)scb;
4082
4083 /*
4084 * Fill out basics of the HSCB.
4085 */
4086 hscb->control = 0;
4087 hscb->scsiid = BUILD_SCSIID(ahd, cmd);
4088 hscb->lun = cmd->device->lun;
4089 scb->hscb->task_management = 0;
4090 mask = SCB_GET_TARGET_MASK(ahd, scb);
4091
4092 if ((ahd->user_discenable & mask) != 0)
4093 hscb->control |= DISCENB;
4094
4095 if (AHD_DV_CMD(cmd) != 0)
4096 scb->flags |= SCB_SILENT;
4097
4098 if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0)
4099 scb->flags |= SCB_PACKETIZED;
4100
4101 if ((tstate->auto_negotiate & mask) != 0) {
4102 scb->flags |= SCB_AUTO_NEGOTIATE;
4103 scb->hscb->control |= MK_MESSAGE;
4104 }
4105
4106 if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) != 0) {
4107 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
4108 int msg_bytes;
4109 uint8_t tag_msgs[2];
4110
4111 msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
4112 if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
4113 hscb->control |= tag_msgs[0];
4114 if (tag_msgs[0] == MSG_ORDERED_TASK)
4115 dev->commands_since_idle_or_otag = 0;
4116 } else
4117 #endif
4118 if (dev->commands_since_idle_or_otag == AHD_OTAG_THRESH
4119 && (dev->flags & AHD_DEV_Q_TAGGED) != 0) {
4120 hscb->control |= MSG_ORDERED_TASK;
4121 dev->commands_since_idle_or_otag = 0;
4122 } else {
4123 hscb->control |= MSG_SIMPLE_TASK;
4124 }
4125 }
4126
4127 hscb->cdb_len = cmd->cmd_len;
4128 memcpy(hscb->shared_data.idata.cdb, cmd->cmnd, hscb->cdb_len);
4129
4130 scb->sg_count = 0;
4131 ahd_set_residual(scb, 0);
4132 ahd_set_sense_residual(scb, 0);
4133 if (cmd->use_sg != 0) {
4134 void *sg;
4135 struct scatterlist *cur_seg;
4136 u_int nseg;
4137 int dir;
4138
4139 cur_seg = (struct scatterlist *)cmd->request_buffer;
4140 dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
4141 nseg = pci_map_sg(ahd->dev_softc, cur_seg,
4142 cmd->use_sg, dir);
4143 scb->platform_data->xfer_len = 0;
4144 for (sg = scb->sg_list; nseg > 0; nseg--, cur_seg++) {
4145 bus_addr_t addr;
4146 bus_size_t len;
4147
4148 addr = sg_dma_address(cur_seg);
4149 len = sg_dma_len(cur_seg);
4150 scb->platform_data->xfer_len += len;
4151 sg = ahd_sg_setup(ahd, scb, sg, addr, len,
4152 /*last*/nseg == 1);
4153 }
4154 } else if (cmd->request_bufflen != 0) {
4155 void *sg;
4156 bus_addr_t addr;
4157 int dir;
4158
4159 sg = scb->sg_list;
4160 dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
4161 addr = pci_map_single(ahd->dev_softc,
4162 cmd->request_buffer,
4163 cmd->request_bufflen, dir);
4164 scb->platform_data->xfer_len = cmd->request_bufflen;
4165 scb->platform_data->buf_busaddr = addr;
4166 sg = ahd_sg_setup(ahd, scb, sg, addr,
4167 cmd->request_bufflen, /*last*/TRUE);
4168 }
4169
4170 LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
4171 dev->openings--;
4172 dev->active++;
4173 dev->commands_issued++;
4174
4175 /* Update the error counting bucket and dump if needed */
4176 if (dev->target->cmds_since_error) {
4177 dev->target->cmds_since_error++;
4178 if (dev->target->cmds_since_error >
4179 AHD_LINUX_ERR_THRESH)
4180 dev->target->cmds_since_error = 0;
4181 }
4182
4183 if ((dev->flags & AHD_DEV_PERIODIC_OTAG) != 0)
4184 dev->commands_since_idle_or_otag++;
4185 scb->flags |= SCB_ACTIVE;
4186 ahd_queue_scb(ahd, scb);
4187 }
4188 }
4189
4190 /*
4191 * SCSI controller interrupt handler.
4192 */
4193 irqreturn_t
4194 ahd_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
4195 {
4196 struct ahd_softc *ahd;
4197 u_long flags;
4198 int ours;
4199
4200 ahd = (struct ahd_softc *) dev_id;
4201 ahd_lock(ahd, &flags);
4202 ours = ahd_intr(ahd);
4203 if (ahd_linux_next_device_to_run(ahd) != NULL)
4204 ahd_schedule_runq(ahd);
4205 ahd_linux_run_complete_queue(ahd);
4206 ahd_unlock(ahd, &flags);
4207 return IRQ_RETVAL(ours);
4208 }
4209
4210 void
4211 ahd_platform_flushwork(struct ahd_softc *ahd)
4212 {
4213
4214 while (ahd_linux_run_complete_queue(ahd) != NULL)
4215 ;
4216 }
4217
4218 static struct ahd_linux_target*
4219 ahd_linux_alloc_target(struct ahd_softc *ahd, u_int channel, u_int target)
4220 {
4221 struct ahd_linux_target *targ;
4222
4223 targ = malloc(sizeof(*targ), M_DEVBUF, M_NOWAIT);
4224 if (targ == NULL)
4225 return (NULL);
4226 memset(targ, 0, sizeof(*targ));
4227 targ->channel = channel;
4228 targ->target = target;
4229 targ->ahd = ahd;
4230 targ->flags = AHD_DV_REQUIRED;
4231 ahd->platform_data->targets[target] = targ;
4232 return (targ);
4233 }
4234
4235 static void
4236 ahd_linux_free_target(struct ahd_softc *ahd, struct ahd_linux_target *targ)
4237 {
4238 struct ahd_devinfo devinfo;
4239 struct ahd_initiator_tinfo *tinfo;
4240 struct ahd_tmode_tstate *tstate;
4241 u_int our_id;
4242 u_int target_offset;
4243 char channel;
4244
4245 /*
4246 * Force a negotiation to async/narrow on any
4247 * future command to this device unless a bus
4248 * reset occurs between now and that command.
4249 */
4250 channel = 'A' + targ->channel;
4251 our_id = ahd->our_id;
4252 target_offset = targ->target;
4253 tinfo = ahd_fetch_transinfo(ahd, channel, our_id,
4254 targ->target, &tstate);
4255 ahd_compile_devinfo(&devinfo, our_id, targ->target, CAM_LUN_WILDCARD,
4256 channel, ROLE_INITIATOR);
4257 ahd_set_syncrate(ahd, &devinfo, 0, 0, 0,
4258 AHD_TRANS_GOAL, /*paused*/FALSE);
4259 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
4260 AHD_TRANS_GOAL, /*paused*/FALSE);
4261 ahd_update_neg_request(ahd, &devinfo, tstate, tinfo, AHD_NEG_ALWAYS);
4262 ahd->platform_data->targets[target_offset] = NULL;
4263 if (targ->inq_data != NULL)
4264 free(targ->inq_data, M_DEVBUF);
4265 if (targ->dv_buffer != NULL)
4266 free(targ->dv_buffer, M_DEVBUF);
4267 if (targ->dv_buffer1 != NULL)
4268 free(targ->dv_buffer1, M_DEVBUF);
4269 free(targ, M_DEVBUF);
4270 }
4271
4272 static struct ahd_linux_device*
4273 ahd_linux_alloc_device(struct ahd_softc *ahd,
4274 struct ahd_linux_target *targ, u_int lun)
4275 {
4276 struct ahd_linux_device *dev;
4277
4278 dev = malloc(sizeof(*dev), M_DEVBUG, M_NOWAIT);
4279 if (dev == NULL)
4280 return (NULL);
4281 memset(dev, 0, sizeof(*dev));
4282 init_timer(&dev->timer);
4283 TAILQ_INIT(&dev->busyq);
4284 dev->flags = AHD_DEV_UNCONFIGURED;
4285 dev->lun = lun;
4286 dev->target = targ;
4287
4288 /*
4289 * We start out life using untagged
4290 * transactions of which we allow one.
4291 */
4292 dev->openings = 1;
4293
4294 /*
4295 * Set maxtags to 0. This will be changed if we
4296 * later determine that we are dealing with
4297 * a tagged queuing capable device.
4298 */
4299 dev->maxtags = 0;
4300
4301 targ->refcount++;
4302 targ->devices[lun] = dev;
4303 return (dev);
4304 }
4305
4306 static void
4307 ahd_linux_free_device(struct ahd_softc *ahd, struct ahd_linux_device *dev)
4308 {
4309 struct ahd_linux_target *targ;
4310
4311 del_timer(&dev->timer);
4312 targ = dev->target;
4313 targ->devices[dev->lun] = NULL;
4314 free(dev, M_DEVBUF);
4315 targ->refcount--;
4316 if (targ->refcount == 0
4317 && (targ->flags & AHD_DV_REQUIRED) == 0)
4318 ahd_linux_free_target(ahd, targ);
4319 }
4320
4321 void
4322 ahd_send_async(struct ahd_softc *ahd, char channel,
4323 u_int target, u_int lun, ac_code code, void *arg)
4324 {
4325 switch (code) {
4326 case AC_TRANSFER_NEG:
4327 {
4328 char buf[80];
4329 struct ahd_linux_target *targ;
4330 struct info_str info;
4331 struct ahd_initiator_tinfo *tinfo;
4332 struct ahd_tmode_tstate *tstate;
4333
4334 info.buffer = buf;
4335 info.length = sizeof(buf);
4336 info.offset = 0;
4337 info.pos = 0;
4338 tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
4339 target, &tstate);
4340
4341 /*
4342 * Don't bother reporting results while
4343 * negotiations are still pending.
4344 */
4345 if (tinfo->curr.period != tinfo->goal.period
4346 || tinfo->curr.width != tinfo->goal.width
4347 || tinfo->curr.offset != tinfo->goal.offset
4348 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
4349 if (bootverbose == 0)
4350 break;
4351
4352 /*
4353 * Don't bother reporting results that
4354 * are identical to those last reported.
4355 */
4356 targ = ahd->platform_data->targets[target];
4357 if (targ == NULL)
4358 break;
4359 if (tinfo->curr.period == targ->last_tinfo.period
4360 && tinfo->curr.width == targ->last_tinfo.width
4361 && tinfo->curr.offset == targ->last_tinfo.offset
4362 && tinfo->curr.ppr_options == targ->last_tinfo.ppr_options)
4363 if (bootverbose == 0)
4364 break;
4365
4366 targ->last_tinfo.period = tinfo->curr.period;
4367 targ->last_tinfo.width = tinfo->curr.width;
4368 targ->last_tinfo.offset = tinfo->curr.offset;
4369 targ->last_tinfo.ppr_options = tinfo->curr.ppr_options;
4370
4371 printf("(%s:%c:", ahd_name(ahd), channel);
4372 if (target == CAM_TARGET_WILDCARD)
4373 printf("*): ");
4374 else
4375 printf("%d): ", target);
4376 ahd_format_transinfo(&info, &tinfo->curr);
4377 if (info.pos < info.length)
4378 *info.buffer = '\0';
4379 else
4380 buf[info.length - 1] = '\0';
4381 printf("%s", buf);
4382 break;
4383 }
4384 case AC_SENT_BDR:
4385 {
4386 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
4387 WARN_ON(lun != CAM_LUN_WILDCARD);
4388 scsi_report_device_reset(ahd->platform_data->host,
4389 channel - 'A', target);
4390 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
4391 Scsi_Device *scsi_dev;
4392
4393 /*
4394 * Find the SCSI device associated with this
4395 * request and indicate that a UA is expected.
4396 */
4397 for (scsi_dev = ahd->platform_data->host->host_queue;
4398 scsi_dev != NULL; scsi_dev = scsi_dev->next) {
4399 if (channel - 'A' == scsi_dev->channel
4400 && target == scsi_dev->id
4401 && (lun == CAM_LUN_WILDCARD
4402 || lun == scsi_dev->lun)) {
4403 scsi_dev->was_reset = 1;
4404 scsi_dev->expecting_cc_ua = 1;
4405 }
4406 }
4407 #endif
4408 break;
4409 }
4410 case AC_BUS_RESET:
4411 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
4412 if (ahd->platform_data->host != NULL) {
4413 scsi_report_bus_reset(ahd->platform_data->host,
4414 channel - 'A');
4415 }
4416 #endif
4417 break;
4418 default:
4419 panic("ahd_send_async: Unexpected async event");
4420 }
4421 }
4422
4423 /*
4424 * Calls the higher level scsi done function and frees the scb.
4425 */
4426 void
4427 ahd_done(struct ahd_softc *ahd, struct scb *scb)
4428 {
4429 Scsi_Cmnd *cmd;
4430 struct ahd_linux_device *dev;
4431
4432 if ((scb->flags & SCB_ACTIVE) == 0) {
4433 printf("SCB %d done'd twice\n", SCB_GET_TAG(scb));
4434 ahd_dump_card_state(ahd);
4435 panic("Stopping for safety");
4436 }
4437 LIST_REMOVE(scb, pending_links);
4438 cmd = scb->io_ctx;
4439 dev = scb->platform_data->dev;
4440 dev->active--;
4441 dev->openings++;
4442 if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
4443 cmd->result &= ~(CAM_DEV_QFRZN << 16);
4444 dev->qfrozen--;
4445 }
4446 ahd_linux_unmap_scb(ahd, scb);
4447
4448 /*
4449 * Guard against stale sense data.
4450 * The Linux mid-layer assumes that sense
4451 * was retrieved anytime the first byte of
4452 * the sense buffer looks "sane".
4453 */
4454 cmd->sense_buffer[0] = 0;
4455 if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG) {
4456 uint32_t amount_xferred;
4457
4458 amount_xferred =
4459 ahd_get_transfer_length(scb) - ahd_get_residual(scb);
4460 if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
4461 #ifdef AHD_DEBUG
4462 if ((ahd_debug & AHD_SHOW_MISC) != 0) {
4463 ahd_print_path(ahd, scb);
4464 printf("Set CAM_UNCOR_PARITY\n");
4465 }
4466 #endif
4467 ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
4468 } else if (amount_xferred < scb->io_ctx->underflow) {
4469 u_int i;
4470
4471 ahd_print_path(ahd, scb);
4472 printf("CDB:");
4473 for (i = 0; i < scb->io_ctx->cmd_len; i++)
4474 printf(" 0x%x", scb->io_ctx->cmnd[i]);
4475 printf("\n");
4476 ahd_print_path(ahd, scb);
4477 printf("Saw underflow (%ld of %ld bytes). "
4478 "Treated as error\n",
4479 ahd_get_residual(scb),
4480 ahd_get_transfer_length(scb));
4481 ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
4482 } else {
4483 ahd_set_transaction_status(scb, CAM_REQ_CMP);
4484 }
4485 } else if (ahd_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
4486 ahd_linux_handle_scsi_status(ahd, dev, scb);
4487 } else if (ahd_get_transaction_status(scb) == CAM_SEL_TIMEOUT) {
4488 dev->flags |= AHD_DEV_UNCONFIGURED;
4489 if (AHD_DV_CMD(cmd) == FALSE)
4490 dev->target->flags &= ~AHD_DV_REQUIRED;
4491 }
4492 /*
4493 * Start DV for devices that require it assuming the first command
4494 * sent does not result in a selection timeout.
4495 */
4496 if (ahd_get_transaction_status(scb) != CAM_SEL_TIMEOUT
4497 && (dev->target->flags & AHD_DV_REQUIRED) != 0)
4498 ahd_linux_start_dv(ahd);
4499
4500 if (dev->openings == 1
4501 && ahd_get_transaction_status(scb) == CAM_REQ_CMP
4502 && ahd_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
4503 dev->tag_success_count++;
4504 /*
4505 * Some devices deal with temporary internal resource
4506 * shortages by returning queue full. When the queue
4507 * full occurrs, we throttle back. Slowly try to get
4508 * back to our previous queue depth.
4509 */
4510 if ((dev->openings + dev->active) < dev->maxtags
4511 && dev->tag_success_count > AHD_TAG_SUCCESS_INTERVAL) {
4512 dev->tag_success_count = 0;
4513 dev->openings++;
4514 }
4515
4516 if (dev->active == 0)
4517 dev->commands_since_idle_or_otag = 0;
4518
4519 if (TAILQ_EMPTY(&dev->busyq)) {
4520 if ((dev->flags & AHD_DEV_UNCONFIGURED) != 0
4521 && dev->active == 0
4522 && (dev->flags & AHD_DEV_TIMER_ACTIVE) == 0)
4523 ahd_linux_free_device(ahd, dev);
4524 } else if ((dev->flags & AHD_DEV_ON_RUN_LIST) == 0) {
4525 TAILQ_INSERT_TAIL(&ahd->platform_data->device_runq, dev, links);
4526 dev->flags |= AHD_DEV_ON_RUN_LIST;
4527 }
4528
4529 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
4530 printf("Recovery SCB completes\n");
4531 if (ahd_get_transaction_status(scb) == CAM_BDR_SENT
4532 || ahd_get_transaction_status(scb) == CAM_REQ_ABORTED)
4533 ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
4534 if ((scb->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
4535 scb->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
4536 up(&ahd->platform_data->eh_sem);
4537 }
4538 }
4539
4540 ahd_free_scb(ahd, scb);
4541 ahd_linux_queue_cmd_complete(ahd, cmd);
4542
4543 if ((ahd->platform_data->flags & AHD_DV_WAIT_SIMQ_EMPTY) != 0
4544 && LIST_FIRST(&ahd->pending_scbs) == NULL) {
4545 ahd->platform_data->flags &= ~AHD_DV_WAIT_SIMQ_EMPTY;
4546 up(&ahd->platform_data->dv_sem);
4547 }
4548 }
4549
4550 static void
4551 ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
4552 struct ahd_linux_device *dev, struct scb *scb)
4553 {
4554 struct ahd_devinfo devinfo;
4555
4556 ahd_compile_devinfo(&devinfo,
4557 ahd->our_id,
4558 dev->target->target, dev->lun,
4559 dev->target->channel == 0 ? 'A' : 'B',
4560 ROLE_INITIATOR);
4561
4562 /*
4563 * We don't currently trust the mid-layer to
4564 * properly deal with queue full or busy. So,
4565 * when one occurs, we tell the mid-layer to
4566 * unconditionally requeue the command to us
4567 * so that we can retry it ourselves. We also
4568 * implement our own throttling mechanism so
4569 * we don't clobber the device with too many
4570 * commands.
4571 */
4572 switch (ahd_get_scsi_status(scb)) {
4573 default:
4574 break;
4575 case SCSI_STATUS_CHECK_COND:
4576 case SCSI_STATUS_CMD_TERMINATED:
4577 {
4578 Scsi_Cmnd *cmd;
4579
4580 /*
4581 * Copy sense information to the OS's cmd
4582 * structure if it is available.
4583 */
4584 cmd = scb->io_ctx;
4585 if ((scb->flags & (SCB_SENSE|SCB_PKT_SENSE)) != 0) {
4586 struct scsi_status_iu_header *siu;
4587 u_int sense_size;
4588 u_int sense_offset;
4589
4590 if (scb->flags & SCB_SENSE) {
4591 sense_size = MIN(sizeof(struct scsi_sense_data)
4592 - ahd_get_sense_residual(scb),
4593 sizeof(cmd->sense_buffer));
4594 sense_offset = 0;
4595 } else {
4596 /*
4597 * Copy only the sense data into the provided
4598 * buffer.
4599 */
4600 siu = (struct scsi_status_iu_header *)
4601 scb->sense_data;
4602 sense_size = MIN(scsi_4btoul(siu->sense_length),
4603 sizeof(cmd->sense_buffer));
4604 sense_offset = SIU_SENSE_OFFSET(siu);
4605 }
4606
4607 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
4608 memcpy(cmd->sense_buffer,
4609 ahd_get_sense_buf(ahd, scb)
4610 + sense_offset, sense_size);
4611 cmd->result |= (DRIVER_SENSE << 24);
4612
4613 #ifdef AHD_DEBUG
4614 if (ahd_debug & AHD_SHOW_SENSE) {
4615 int i;
4616
4617 printf("Copied %d bytes of sense data at %d:",
4618 sense_size, sense_offset);
4619 for (i = 0; i < sense_size; i++) {
4620 if ((i & 0xF) == 0)
4621 printf("\n");
4622 printf("0x%x ", cmd->sense_buffer[i]);
4623 }
4624 printf("\n");
4625 }
4626 #endif
4627 }
4628 break;
4629 }
4630 case SCSI_STATUS_QUEUE_FULL:
4631 {
4632 /*
4633 * By the time the core driver has returned this
4634 * command, all other commands that were queued
4635 * to us but not the device have been returned.
4636 * This ensures that dev->active is equal to
4637 * the number of commands actually queued to
4638 * the device.
4639 */
4640 dev->tag_success_count = 0;
4641 if (dev->active != 0) {
4642 /*
4643 * Drop our opening count to the number
4644 * of commands currently outstanding.
4645 */
4646 dev->openings = 0;
4647 #ifdef AHD_DEBUG
4648 if ((ahd_debug & AHD_SHOW_QFULL) != 0) {
4649 ahd_print_path(ahd, scb);
4650 printf("Dropping tag count to %d\n",
4651 dev->active);
4652 }
4653 #endif
4654 if (dev->active == dev->tags_on_last_queuefull) {
4655
4656 dev->last_queuefull_same_count++;
4657 /*
4658 * If we repeatedly see a queue full
4659 * at the same queue depth, this
4660 * device has a fixed number of tag
4661 * slots. Lock in this tag depth
4662 * so we stop seeing queue fulls from
4663 * this device.
4664 */
4665 if (dev->last_queuefull_same_count
4666 == AHD_LOCK_TAGS_COUNT) {
4667 dev->maxtags = dev->active;
4668 ahd_print_path(ahd, scb);
4669 printf("Locking max tag count at %d\n",
4670 dev->active);
4671 }
4672 } else {
4673 dev->tags_on_last_queuefull = dev->active;
4674 dev->last_queuefull_same_count = 0;
4675 }
4676 ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
4677 ahd_set_scsi_status(scb, SCSI_STATUS_OK);
4678 ahd_platform_set_tags(ahd, &devinfo,
4679 (dev->flags & AHD_DEV_Q_BASIC)
4680 ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
4681 break;
4682 }
4683 /*
4684 * Drop down to a single opening, and treat this
4685 * as if the target returned BUSY SCSI status.
4686 */
4687 dev->openings = 1;
4688 ahd_platform_set_tags(ahd, &devinfo,
4689 (dev->flags & AHD_DEV_Q_BASIC)
4690 ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
4691 ahd_set_scsi_status(scb, SCSI_STATUS_BUSY);
4692 /* FALLTHROUGH */
4693 }
4694 case SCSI_STATUS_BUSY:
4695 /*
4696 * Set a short timer to defer sending commands for
4697 * a bit since Linux will not delay in this case.
4698 */
4699 if ((dev->flags & AHD_DEV_TIMER_ACTIVE) != 0) {
4700 printf("%s:%c:%d: Device Timer still active during "
4701 "busy processing\n", ahd_name(ahd),
4702 dev->target->channel, dev->target->target);
4703 break;
4704 }
4705 dev->flags |= AHD_DEV_TIMER_ACTIVE;
4706 dev->qfrozen++;
4707 init_timer(&dev->timer);
4708 dev->timer.data = (u_long)dev;
4709 dev->timer.expires = jiffies + (HZ/2);
4710 dev->timer.function = ahd_linux_dev_timed_unfreeze;
4711 add_timer(&dev->timer);
4712 break;
4713 }
4714 }
4715
4716 static void
4717 ahd_linux_queue_cmd_complete(struct ahd_softc *ahd, Scsi_Cmnd *cmd)
4718 {
4719 /*
4720 * Typically, the complete queue has very few entries
4721 * queued to it before the queue is emptied by
4722 * ahd_linux_run_complete_queue, so sorting the entries
4723 * by generation number should be inexpensive.
4724 * We perform the sort so that commands that complete
4725 * with an error are retuned in the order origionally
4726 * queued to the controller so that any subsequent retries
4727 * are performed in order. The underlying ahd routines do
4728 * not guarantee the order that aborted commands will be
4729 * returned to us.
4730 */
4731 struct ahd_completeq *completeq;
4732 struct ahd_cmd *list_cmd;
4733 struct ahd_cmd *acmd;
4734
4735 /*
4736 * Map CAM error codes into Linux Error codes. We
4737 * avoid the conversion so that the DV code has the
4738 * full error information available when making
4739 * state change decisions.
4740 */
4741 if (AHD_DV_CMD(cmd) == FALSE) {
4742 uint32_t status;
4743 u_int new_status;
4744
4745 status = ahd_cmd_get_transaction_status(cmd);
4746 if (status != CAM_REQ_CMP) {
4747 struct ahd_linux_device *dev;
4748 struct ahd_devinfo devinfo;
4749 cam_status cam_status;
4750 uint32_t action;
4751 u_int scsi_status;
4752
4753 dev = ahd_linux_get_device(ahd, cmd->device->channel,
4754 cmd->device->id,
4755 cmd->device->lun,
4756 /*alloc*/FALSE);
4757
4758 if (dev == NULL)
4759 goto no_fallback;
4760
4761 ahd_compile_devinfo(&devinfo,
4762 ahd->our_id,
4763 dev->target->target, dev->lun,
4764 dev->target->channel == 0 ? 'A':'B',
4765 ROLE_INITIATOR);
4766
4767 scsi_status = ahd_cmd_get_scsi_status(cmd);
4768 cam_status = ahd_cmd_get_transaction_status(cmd);
4769 action = aic_error_action(cmd, dev->target->inq_data,
4770 cam_status, scsi_status);
4771 if ((action & SSQ_FALLBACK) != 0) {
4772
4773 /* Update stats */
4774 dev->target->errors_detected++;
4775 if (dev->target->cmds_since_error == 0)
4776 dev->target->cmds_since_error++;
4777 else {
4778 dev->target->cmds_since_error = 0;
4779 ahd_linux_fallback(ahd, &devinfo);
4780 }
4781 }
4782 }
4783 no_fallback:
4784 switch (status) {
4785 case CAM_REQ_INPROG:
4786 case CAM_REQ_CMP:
4787 case CAM_SCSI_STATUS_ERROR:
4788 new_status = DID_OK;
4789 break;
4790 case CAM_REQ_ABORTED:
4791 new_status = DID_ABORT;
4792 break;
4793 case CAM_BUSY:
4794 new_status = DID_BUS_BUSY;
4795 break;
4796 case CAM_REQ_INVALID:
4797 case CAM_PATH_INVALID:
4798 new_status = DID_BAD_TARGET;
4799 break;
4800 case CAM_SEL_TIMEOUT:
4801 new_status = DID_NO_CONNECT;
4802 break;
4803 case CAM_SCSI_BUS_RESET:
4804 case CAM_BDR_SENT:
4805 new_status = DID_RESET;
4806 break;
4807 case CAM_UNCOR_PARITY:
4808 new_status = DID_PARITY;
4809 break;
4810 case CAM_CMD_TIMEOUT:
4811 new_status = DID_TIME_OUT;
4812 break;
4813 case CAM_UA_ABORT:
4814 case CAM_REQ_CMP_ERR:
4815 case CAM_AUTOSENSE_FAIL:
4816 case CAM_NO_HBA:
4817 case CAM_DATA_RUN_ERR:
4818 case CAM_UNEXP_BUSFREE:
4819 case CAM_SEQUENCE_FAIL:
4820 case CAM_CCB_LEN_ERR:
4821 case CAM_PROVIDE_FAIL:
4822 case CAM_REQ_TERMIO:
4823 case CAM_UNREC_HBA_ERROR:
4824 case CAM_REQ_TOO_BIG:
4825 new_status = DID_ERROR;
4826 break;
4827 case CAM_REQUEUE_REQ:
4828 /*
4829 * If we want the request requeued, make sure there
4830 * are sufficent retries. In the old scsi error code,
4831 * we used to be able to specify a result code that
4832 * bypassed the retry count. Now we must use this
4833 * hack. We also "fake" a check condition with
4834 * a sense code of ABORTED COMMAND. This seems to
4835 * evoke a retry even if this command is being sent
4836 * via the eh thread. Ick! Ick! Ick!
4837 */
4838 if (cmd->retries > 0)
4839 cmd->retries--;
4840 new_status = DID_OK;
4841 ahd_cmd_set_scsi_status(cmd, SCSI_STATUS_CHECK_COND);
4842 cmd->result |= (DRIVER_SENSE << 24);
4843 memset(cmd->sense_buffer, 0,
4844 sizeof(cmd->sense_buffer));
4845 cmd->sense_buffer[0] = SSD_ERRCODE_VALID
4846 | SSD_CURRENT_ERROR;
4847 cmd->sense_buffer[2] = SSD_KEY_ABORTED_COMMAND;
4848 break;
4849 default:
4850 /* We should never get here */
4851 new_status = DID_ERROR;
4852 break;
4853 }
4854
4855 ahd_cmd_set_transaction_status(cmd, new_status);
4856 }
4857
4858 completeq = &ahd->platform_data->completeq;
4859 list_cmd = TAILQ_FIRST(completeq);
4860 acmd = (struct ahd_cmd *)cmd;
4861 while (list_cmd != NULL
4862 && acmd_scsi_cmd(list_cmd).serial_number
4863 < acmd_scsi_cmd(acmd).serial_number)
4864 list_cmd = TAILQ_NEXT(list_cmd, acmd_links.tqe);
4865 if (list_cmd != NULL)
4866 TAILQ_INSERT_BEFORE(list_cmd, acmd, acmd_links.tqe);
4867 else
4868 TAILQ_INSERT_TAIL(completeq, acmd, acmd_links.tqe);
4869 }
4870
4871 static void
4872 ahd_linux_filter_inquiry(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
4873 {
4874 struct scsi_inquiry_data *sid;
4875 struct ahd_initiator_tinfo *tinfo;
4876 struct ahd_transinfo *user;
4877 struct ahd_transinfo *goal;
4878 struct ahd_transinfo *curr;
4879 struct ahd_tmode_tstate *tstate;
4880 struct ahd_linux_device *dev;
4881 u_int width;
4882 u_int period;
4883 u_int offset;
4884 u_int ppr_options;
4885 u_int trans_version;
4886 u_int prot_version;
4887
4888 /*
4889 * Determine if this lun actually exists. If so,
4890 * hold on to its corresponding device structure.
4891 * If not, make sure we release the device and
4892 * don't bother processing the rest of this inquiry
4893 * command.
4894 */
4895 dev = ahd_linux_get_device(ahd, devinfo->channel - 'A',
4896 devinfo->target, devinfo->lun,
4897 /*alloc*/TRUE);
4898
4899 sid = (struct scsi_inquiry_data *)dev->target->inq_data;
4900 if (SID_QUAL(sid) == SID_QUAL_LU_CONNECTED) {
4901
4902 dev->flags &= ~AHD_DEV_UNCONFIGURED;
4903 } else {
4904 dev->flags |= AHD_DEV_UNCONFIGURED;
4905 return;
4906 }
4907
4908 /*
4909 * Update our notion of this device's transfer
4910 * negotiation capabilities.
4911 */
4912 tinfo = ahd_fetch_transinfo(ahd, devinfo->channel,
4913 devinfo->our_scsiid,
4914 devinfo->target, &tstate);
4915 user = &tinfo->user;
4916 goal = &tinfo->goal;
4917 curr = &tinfo->curr;
4918 width = user->width;
4919 period = user->period;
4920 offset = user->offset;
4921 ppr_options = user->ppr_options;
4922 trans_version = user->transport_version;
4923 prot_version = MIN(user->protocol_version, SID_ANSI_REV(sid));
4924
4925 /*
4926 * Only attempt SPI3/4 once we've verified that
4927 * the device claims to support SPI3/4 features.
4928 */
4929 if (prot_version < SCSI_REV_2)
4930 trans_version = SID_ANSI_REV(sid);
4931 else
4932 trans_version = SCSI_REV_2;
4933
4934 if ((sid->flags & SID_WBus16) == 0)
4935 width = MSG_EXT_WDTR_BUS_8_BIT;
4936 if ((sid->flags & SID_Sync) == 0) {
4937 period = 0;
4938 offset = 0;
4939 ppr_options = 0;
4940 }
4941 if ((sid->spi3data & SID_SPI_QAS) == 0)
4942 ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
4943 if ((sid->spi3data & SID_SPI_CLOCK_DT) == 0)
4944 ppr_options &= MSG_EXT_PPR_QAS_REQ;
4945 if ((sid->spi3data & SID_SPI_IUS) == 0)
4946 ppr_options &= (MSG_EXT_PPR_DT_REQ
4947 | MSG_EXT_PPR_QAS_REQ);
4948
4949 if (prot_version > SCSI_REV_2
4950 && ppr_options != 0)
4951 trans_version = user->transport_version;
4952
4953 ahd_validate_width(ahd, /*tinfo limit*/NULL, &width, ROLE_UNKNOWN);
4954 ahd_find_syncrate(ahd, &period, &ppr_options, AHD_SYNCRATE_MAX);
4955 ahd_validate_offset(ahd, /*tinfo limit*/NULL, period,
4956 &offset, width, ROLE_UNKNOWN);
4957 if (offset == 0 || period == 0) {
4958 period = 0;
4959 offset = 0;
4960 ppr_options = 0;
4961 }
4962 /* Apply our filtered user settings. */
4963 curr->transport_version = trans_version;
4964 curr->protocol_version = prot_version;
4965 ahd_set_width(ahd, devinfo, width, AHD_TRANS_GOAL, /*paused*/FALSE);
4966 ahd_set_syncrate(ahd, devinfo, period, offset, ppr_options,
4967 AHD_TRANS_GOAL, /*paused*/FALSE);
4968 }
4969
4970 void
4971 ahd_freeze_simq(struct ahd_softc *ahd)
4972 {
4973 ahd->platform_data->qfrozen++;
4974 if (ahd->platform_data->qfrozen == 1) {
4975 scsi_block_requests(ahd->platform_data->host);
4976 ahd_platform_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
4977 CAM_LUN_WILDCARD, SCB_LIST_NULL,
4978 ROLE_INITIATOR, CAM_REQUEUE_REQ);
4979 }
4980 }
4981
4982 void
4983 ahd_release_simq(struct ahd_softc *ahd)
4984 {
4985 u_long s;
4986 int unblock_reqs;
4987
4988 unblock_reqs = 0;
4989 ahd_lock(ahd, &s);
4990 if (ahd->platform_data->qfrozen > 0)
4991 ahd->platform_data->qfrozen--;
4992 if (ahd->platform_data->qfrozen == 0) {
4993 unblock_reqs = 1;
4994 }
4995 if (AHD_DV_SIMQ_FROZEN(ahd)
4996 && ((ahd->platform_data->flags & AHD_DV_WAIT_SIMQ_RELEASE) != 0)) {
4997 ahd->platform_data->flags &= ~AHD_DV_WAIT_SIMQ_RELEASE;
4998 up(&ahd->platform_data->dv_sem);
4999 }
5000 ahd_schedule_runq(ahd);
5001 ahd_unlock(ahd, &s);
5002 /*
5003 * There is still a race here. The mid-layer
5004 * should keep its own freeze count and use
5005 * a bottom half handler to run the queues
5006 * so we can unblock with our own lock held.
5007 */
5008 if (unblock_reqs)
5009 scsi_unblock_requests(ahd->platform_data->host);
5010 }
5011
5012 static void
5013 ahd_linux_sem_timeout(u_long arg)
5014 {
5015 struct scb *scb;
5016 struct ahd_softc *ahd;
5017 u_long s;
5018
5019 scb = (struct scb *)arg;
5020 ahd = scb->ahd_softc;
5021 ahd_lock(ahd, &s);
5022 if ((scb->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
5023 scb->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
5024 up(&ahd->platform_data->eh_sem);
5025 }
5026 ahd_unlock(ahd, &s);
5027 }
5028
5029 static void
5030 ahd_linux_dev_timed_unfreeze(u_long arg)
5031 {
5032 struct ahd_linux_device *dev;
5033 struct ahd_softc *ahd;
5034 u_long s;
5035
5036 dev = (struct ahd_linux_device *)arg;
5037 ahd = dev->target->ahd;
5038 ahd_lock(ahd, &s);
5039 dev->flags &= ~AHD_DEV_TIMER_ACTIVE;
5040 if (dev->qfrozen > 0)
5041 dev->qfrozen--;
5042 if (dev->qfrozen == 0
5043 && (dev->flags & AHD_DEV_ON_RUN_LIST) == 0)
5044 ahd_linux_run_device_queue(ahd, dev);
5045 if ((dev->flags & AHD_DEV_UNCONFIGURED) != 0
5046 && dev->active == 0)
5047 ahd_linux_free_device(ahd, dev);
5048 ahd_unlock(ahd, &s);
5049 }
5050
5051 void
5052 ahd_platform_dump_card_state(struct ahd_softc *ahd)
5053 {
5054 struct ahd_linux_device *dev;
5055 int target;
5056 int maxtarget;
5057 int lun;
5058 int i;
5059
5060 maxtarget = (ahd->features & AHD_WIDE) ? 15 : 7;
5061 for (target = 0; target <=maxtarget; target++) {
5062
5063 for (lun = 0; lun < AHD_NUM_LUNS; lun++) {
5064 struct ahd_cmd *acmd;
5065
5066 dev = ahd_linux_get_device(ahd, 0, target,
5067 lun, /*alloc*/FALSE);
5068 if (dev == NULL)
5069 continue;
5070
5071 printf("DevQ(%d:%d:%d): ", 0, target, lun);
5072 i = 0;
5073 TAILQ_FOREACH(acmd, &dev->busyq, acmd_links.tqe) {
5074 if (i++ > AHD_SCB_MAX)
5075 break;
5076 }
5077 printf("%d waiting\n", i);
5078 }
5079 }
5080 }
5081
5082 static int __init
5083 ahd_linux_init(void)
5084 {
5085 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
5086 return (ahd_linux_detect(&aic79xx_driver_template) ? 0 : -ENODEV);
5087 #else
5088 scsi_register_module(MODULE_SCSI_HA, &aic79xx_driver_template);
5089 if (aic79xx_driver_template.present == 0) {
5090 scsi_unregister_module(MODULE_SCSI_HA,
5091 &aic79xx_driver_template);
5092 return (-ENODEV);
5093 }
5094
5095 return (0);
5096 #endif
5097 }
5098
5099 static void __exit
5100 ahd_linux_exit(void)
5101 {
5102 struct ahd_softc *ahd;
5103 u_long l;
5104
5105 /*
5106 * Shutdown DV threads before going into the SCSI mid-layer.
5107 * This avoids situations where the mid-layer locks the entire
5108 * kernel so that waiting for our DV threads to exit leads
5109 * to deadlock.
5110 */
5111 ahd_list_lock(&l);
5112 TAILQ_FOREACH(ahd, &ahd_tailq, links) {
5113
5114 ahd_linux_kill_dv_thread(ahd);
5115 }
5116 ahd_list_unlock(&l);
5117 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
5118 /*
5119 * In 2.4 we have to unregister from the PCI core _after_
5120 * unregistering from the scsi midlayer to avoid dangling
5121 * references.
5122 */
5123 scsi_unregister_module(MODULE_SCSI_HA, &aic79xx_driver_template);
5124 #endif
5125 ahd_linux_pci_exit();
5126 }
5127
5128 module_init(ahd_linux_init);
5129 module_exit(ahd_linux_exit);
5130