ia64/xen-unstable

view xen/drivers/scsi/aic7xxx/aic7xxx_osm.c @ 945:db2e1ea917df

bitkeeper revision 1.596.1.3 (3fb3b41eWUoRU0H8A0jEX5roXjxKkA)

Many files:
Greatly simplified Xen softirqs. They are now only executed in outermost Xen activation; they are never called within an irq context.
author kaf24@scramble.cl.cam.ac.uk
date Thu Nov 13 16:41:02 2003 +0000 (2003-11-13)
parents 8c63ec842aca
children 7a554cbf0f58
line source
1 /*
2 * Adaptec AIC7xxx device driver for Linux.
3 *
4 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.c#103 $
5 *
6 * Copyright (c) 1994 John Aycock
7 * The University of Calgary Department of Computer Science.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Sources include the Adaptec 1740 driver (aha1740.c), the Ultrastor 24F
24 * driver (ultrastor.c), various Linux kernel source, the Adaptec EISA
25 * config file (!adp7771.cfg), the Adaptec AHA-2740A Series User's Guide,
26 * the Linux Kernel Hacker's Guide, Writing a SCSI Device Driver for Linux,
27 * the Adaptec 1542 driver (aha1542.c), the Adaptec EISA overlay file
28 * (adp7770.ovl), the Adaptec AHA-2740 Series Technical Reference Manual,
29 * the Adaptec AIC-7770 Data Book, the ANSI SCSI specification, the
30 * ANSI SCSI-2 specification (draft 10c), ...
31 *
32 * --------------------------------------------------------------------------
33 *
34 * Modifications by Daniel M. Eischen (deischen@iworks.InterWorks.org):
35 *
36 * Substantially modified to include support for wide and twin bus
37 * adapters, DMAing of SCBs, tagged queueing, IRQ sharing, bug fixes,
38 * SCB paging, and other rework of the code.
39 *
40 * --------------------------------------------------------------------------
41 * Copyright (c) 1994-2000 Justin T. Gibbs.
42 * Copyright (c) 2000-2001 Adaptec Inc.
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions, and the following disclaimer,
50 * without modification.
51 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
52 * substantially similar to the "NO WARRANTY" disclaimer below
53 * ("Disclaimer") and any redistribution must be conditioned upon
54 * including a substantially similar Disclaimer requirement for further
55 * binary redistribution.
56 * 3. Neither the names of the above-listed copyright holders nor the names
57 * of any contributors may be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 *
60 * Alternatively, this software may be distributed under the terms of the
61 * GNU General Public License ("GPL") version 2 as published by the Free
62 * Software Foundation.
63 *
64 * NO WARRANTY
65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
66 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
67 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
68 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
69 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
73 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
74 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
75 * POSSIBILITY OF SUCH DAMAGES.
76 *
77 *---------------------------------------------------------------------------
78 *
79 * Thanks also go to (in alphabetical order) the following:
80 *
81 * Rory Bolt - Sequencer bug fixes
82 * Jay Estabrook - Initial DEC Alpha support
83 * Doug Ledford - Much needed abort/reset bug fixes
84 * Kai Makisara - DMAing of SCBs
85 *
86 * A Boot time option was also added for not resetting the scsi bus.
87 *
88 * Form: aic7xxx=extended
89 * aic7xxx=no_reset
90 * aic7xxx=verbose
91 *
92 * Daniel M. Eischen, deischen@iworks.InterWorks.org, 1/23/97
93 *
94 * Id: aic7xxx.c,v 4.1 1997/06/12 08:23:42 deang Exp
95 */
97 /*
98 * Further driver modifications made by Doug Ledford <dledford@redhat.com>
99 *
100 * Copyright (c) 1997-1999 Doug Ledford
101 *
102 * These changes are released under the same licensing terms as the FreeBSD
103 * driver written by Justin Gibbs. Please see his Copyright notice above
104 * for the exact terms and conditions covering my changes as well as the
105 * warranty statement.
106 *
107 * Modifications made to the aic7xxx.c,v 4.1 driver from Dan Eischen include
108 * but are not limited to:
109 *
110 * 1: Import of the latest FreeBSD sequencer code for this driver
111 * 2: Modification of kernel code to accomodate different sequencer semantics
112 * 3: Extensive changes throughout kernel portion of driver to improve
113 * abort/reset processing and error hanndling
114 * 4: Other work contributed by various people on the Internet
115 * 5: Changes to printk information and verbosity selection code
116 * 6: General reliability related changes, especially in IRQ management
117 * 7: Modifications to the default probe/attach order for supported cards
118 * 8: SMP friendliness has been improved
119 *
120 */
122 /*
123 * This is the only file where module.h should
124 * embed module global version info.
125 */
126 //#define AHC_MODVERSION_FILE
128 #include <xeno/lib.h>
129 #include <xeno/string.h>
130 #include "aic7xxx_osm.h"
131 #include "aic7xxx_inline.h"
133 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
134 #include <xeno/init.h> /* __setup */
135 #endif
137 #include "../sd.h" /* For geometry detection */
139 #include <xeno/mm.h> /* For fetching system memory size */
140 #include <xeno/blk.h> /* For block_size() */
142 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
143 /*
144 * Lock protecting manipulation of the ahc softc list.
145 */
146 spinlock_t ahc_list_spinlock;
147 #endif
149 /*
150 * To generate the correct addresses for the controller to issue
151 * on the bus. Originally added for DEC Alpha support.
152 */
153 #define VIRT_TO_BUS(a) (uint32_t)virt_to_bus((void *)(a))
155 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
156 struct proc_dir_entry proc_scsi_aic7xxx = {
157 PROC_SCSI_AIC7XXX, 7, "aic7xxx",
158 S_IFDIR | S_IRUGO | S_IXUGO, 2,
159 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL
160 };
161 #endif
163 /*
164 * Set this to the delay in seconds after SCSI bus reset.
165 * Note, we honor this only for the initial bus reset.
166 * The scsi error recovery code performs its own bus settle
167 * delay handling for error recovery actions.
168 */
169 #ifdef CONFIG_AIC7XXX_RESET_DELAY_MS
170 #define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS
171 #else
172 #define AIC7XXX_RESET_DELAY 500
173 #endif
175 /*
176 * Control collection of SCSI transfer statistics for the /proc filesystem.
177 *
178 * NOTE: Do NOT enable this when running on kernels version 1.2.x and below.
179 * NOTE: This does affect performance since it has to maintain statistics.
180 */
181 #ifdef CONFIG_AIC7XXX_PROC_STATS
182 #define AIC7XXX_PROC_STATS
183 #endif
185 /*
186 * To change the default number of tagged transactions allowed per-device,
187 * add a line to the lilo.conf file like:
188 * append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
189 * which will result in the first four devices on the first two
190 * controllers being set to a tagged queue depth of 32.
191 *
192 * The tag_commands is an array of 16 to allow for wide and twin adapters.
193 * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
194 * for channel 1.
195 */
196 typedef struct {
197 uint8_t tag_commands[16]; /* Allow for wide/twin adapters. */
198 } adapter_tag_info_t;
200 /*
201 * Modify this as you see fit for your system.
202 *
203 * 0 tagged queuing disabled
204 * 1 <= n <= 253 n == max tags ever dispatched.
205 *
206 * The driver will throttle the number of commands dispatched to a
207 * device if it returns queue full. For devices with a fixed maximum
208 * queue depth, the driver will eventually determine this depth and
209 * lock it in (a console message is printed to indicate that a lock
210 * has occurred). On some devices, queue full is returned for a temporary
211 * resource shortage. These devices will return queue full at varying
212 * depths. The driver will throttle back when the queue fulls occur and
213 * attempt to slowly increase the depth over time as the device recovers
214 * from the resource shortage.
215 *
216 * In this example, the first line will disable tagged queueing for all
217 * the devices on the first probed aic7xxx adapter.
218 *
219 * The second line enables tagged queueing with 4 commands/LUN for IDs
220 * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
221 * driver to attempt to use up to 64 tags for ID 1.
222 *
223 * The third line is the same as the first line.
224 *
225 * The fourth line disables tagged queueing for devices 0 and 3. It
226 * enables tagged queueing for the other IDs, with 16 commands/LUN
227 * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
228 * IDs 2, 5-7, and 9-15.
229 */
231 /*
232 * NOTE: The below structure is for reference only, the actual structure
233 * to modify in order to change things is just below this comment block.
234 adapter_tag_info_t aic7xxx_tag_info[] =
235 {
236 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
237 {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
238 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
239 {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
240 };
241 */
243 #ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
244 #define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
245 #else
246 #define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE
247 #endif
249 #define AIC7XXX_CONFIGED_TAG_COMMANDS { \
250 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
251 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
252 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
253 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
254 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
255 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
256 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
257 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE \
258 }
260 /*
261 * By default, use the number of commands specified by
262 * the users kernel configuration.
263 */
264 static adapter_tag_info_t aic7xxx_tag_info[] =
265 {
266 {AIC7XXX_CONFIGED_TAG_COMMANDS},
267 {AIC7XXX_CONFIGED_TAG_COMMANDS},
268 {AIC7XXX_CONFIGED_TAG_COMMANDS},
269 {AIC7XXX_CONFIGED_TAG_COMMANDS},
270 {AIC7XXX_CONFIGED_TAG_COMMANDS},
271 {AIC7XXX_CONFIGED_TAG_COMMANDS},
272 {AIC7XXX_CONFIGED_TAG_COMMANDS},
273 {AIC7XXX_CONFIGED_TAG_COMMANDS},
274 {AIC7XXX_CONFIGED_TAG_COMMANDS},
275 {AIC7XXX_CONFIGED_TAG_COMMANDS},
276 {AIC7XXX_CONFIGED_TAG_COMMANDS},
277 {AIC7XXX_CONFIGED_TAG_COMMANDS},
278 {AIC7XXX_CONFIGED_TAG_COMMANDS},
279 {AIC7XXX_CONFIGED_TAG_COMMANDS},
280 {AIC7XXX_CONFIGED_TAG_COMMANDS},
281 {AIC7XXX_CONFIGED_TAG_COMMANDS}
282 };
284 /*
285 * There should be a specific return value for this in scsi.h, but
286 * it seems that most drivers ignore it.
287 */
288 #define DID_UNDERFLOW DID_ERROR
290 void
291 ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
292 {
293 printf("(scsi%d:%c:%d:%d): ",
294 ahc->platform_data->host->host_no,
295 scb != NULL ? SCB_GET_CHANNEL(ahc, scb) : 'X',
296 scb != NULL ? SCB_GET_TARGET(ahc, scb) : -1,
297 scb != NULL ? SCB_GET_LUN(scb) : -1);
298 }
300 /*
301 * XXX - these options apply unilaterally to _all_ 274x/284x/294x
302 * cards in the system. This should be fixed. Exceptions to this
303 * rule are noted in the comments.
304 */
306 /*
307 * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This
308 * has no effect on any later resets that might occur due to things like
309 * SCSI bus timeouts.
310 */
311 static uint32_t aic7xxx_no_reset;
313 /*
314 * Certain PCI motherboards will scan PCI devices from highest to lowest,
315 * others scan from lowest to highest, and they tend to do all kinds of
316 * strange things when they come into contact with PCI bridge chips. The
317 * net result of all this is that the PCI card that is actually used to boot
318 * the machine is very hard to detect. Most motherboards go from lowest
319 * PCI slot number to highest, and the first SCSI controller found is the
320 * one you boot from. The only exceptions to this are when a controller
321 * has its BIOS disabled. So, we by default sort all of our SCSI controllers
322 * from lowest PCI slot number to highest PCI slot number. We also force
323 * all controllers with their BIOS disabled to the end of the list. This
324 * works on *almost* all computers. Where it doesn't work, we have this
325 * option. Setting this option to non-0 will reverse the order of the sort
326 * to highest first, then lowest, but will still leave cards with their BIOS
327 * disabled at the very end. That should fix everyone up unless there are
328 * really strange cirumstances.
329 */
330 static int aic7xxx_reverse_scan = 0;
332 /*
333 * Should we force EXTENDED translation on a controller.
334 * 0 == Use whatever is in the SEEPROM or default to off
335 * 1 == Use whatever is in the SEEPROM or default to on
336 */
337 static uint32_t aic7xxx_extended = 0;
339 /*
340 * PCI bus parity checking of the Adaptec controllers. This is somewhat
341 * dubious at best. To my knowledge, this option has never actually
342 * solved a PCI parity problem, but on certain machines with broken PCI
343 * chipset configurations, it can generate tons of false error messages.
344 * It's included in the driver for completeness.
345 * 0 = Shut off PCI parity check
346 * -1 = Normal polarity pci parity checking
347 * 1 = reverse polarity pci parity checking
348 *
349 * NOTE: you can't actually pass -1 on the lilo prompt. So, to set this
350 * variable to -1 you would actually want to simply pass the variable
351 * name without a number. That will invert the 0 which will result in
352 * -1.
353 */
354 static int aic7xxx_pci_parity = 0;
356 /*
357 * Certain newer motherboards have put new PCI based devices into the
358 * IO spaces that used to typically be occupied by VLB or EISA cards.
359 * This overlap can cause these newer motherboards to lock up when scanned
360 * for older EISA and VLB devices. Setting this option to non-0 will
361 * cause the driver to skip scanning for any VLB or EISA controllers and
362 * only support the PCI controllers. NOTE: this means that if the kernel
363 * os compiled with PCI support disabled, then setting this to non-0
364 * would result in never finding any devices :)
365 */
366 #ifndef CONFIG_AIC7XXX_PROBE_EISA_VL
367 #define CONFIG_AIC7XXX_PROBE_EISA_VL n
368 #endif
369 #if CONFIG_AIC7XXX_PROBE_EISA_VL == n
370 static int aic7xxx_no_probe = 1;
371 #else
372 static int aic7xxx_no_probe;
373 #endif
375 /*
376 * aic7xxx_detect() has been run, so register all device arrivals
377 * immediately with the system rather than deferring to the sorted
378 * attachment performed by aic7xxx_detect().
379 */
380 int aic7xxx_detect_complete;
382 /*
383 * So that we can set how long each device is given as a selection timeout.
384 * The table of values goes like this:
385 * 0 - 256ms
386 * 1 - 128ms
387 * 2 - 64ms
388 * 3 - 32ms
389 * We default to 256ms because some older devices need a longer time
390 * to respond to initial selection.
391 */
392 static int aic7xxx_seltime = 0x00;
394 /*
395 * Certain devices do not perform any aging on commands. Should the
396 * device be saturated by commands in one portion of the disk, it is
397 * possible for transactions on far away sectors to never be serviced.
398 * To handle these devices, we can periodically send an ordered tag to
399 * force all outstanding transactions to be serviced prior to a new
400 * transaction.
401 */
402 int aic7xxx_periodic_otag;
404 /*
405 * Module information and settable options.
406 */
407 #ifdef MODULE
408 static char *aic7xxx = NULL;
409 /*
410 * Just in case someone uses commas to separate items on the insmod
411 * command line, we define a dummy buffer here to avoid having insmod
412 * write wild stuff into our code segment
413 */
414 static char dummy_buffer[60] = "Please don't trounce on me insmod!!\n";
415 /*
416 MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
417 MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
418 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,10)
419 MODULE_LICENSE("Dual BSD/GPL");
420 #endif
421 MODULE_PARM(aic7xxx, "s");
422 MODULE_PARM_DESC(aic7xxx, "period delimited, options string.
423 verbose Enable verbose/diagnostic logging
424 no_probe Disable EISA/VLB controller probing
425 no_reset Supress initial bus resets
426 extended Enable extended geometry on all controllers
427 periodic_otag Send an ordered tagged transaction periodically
428 to prevent tag starvation. This may be
429 required by some older disk drives/RAID arrays.
430 reverse_scan Sort PCI devices highest Bus/Slot to lowest
431 tag_info:<tag_str> Set per-target tag depth
432 seltime:<int> Selection Timeout(0/256ms,1/128ms,2/64ms,3/32ms)
434 Sample /etc/modules.conf line:
435 Enable verbose logging
436 Disable EISA/VLB probing
437 Set tag depth on Controller 2/Target 2 to 10 tags
438 Shorten the selection timeout to 128ms from its default of 256
440 options aic7xxx='\"verbose.no_probe.tag_info:{{}.{}.{..10}}.seltime:1\"'
441 ");
442 */
443 #endif
445 static void ahc_linux_handle_scsi_status(struct ahc_softc *,
446 struct ahc_linux_device *,
447 struct scb *);
448 static void ahc_linux_filter_command(struct ahc_softc*, Scsi_Cmnd*,
449 struct scb*);
450 //static void ahc_linux_sem_timeout(u_long arg);
451 static void ahc_linux_freeze_sim_queue(struct ahc_softc *ahc);
452 static void ahc_linux_release_sim_queue(u_long arg);
453 static void ahc_linux_dev_timed_unfreeze(u_long arg);
454 static int ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag);
455 static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
456 static void ahc_linux_select_queue_depth(struct Scsi_Host *host,
457 Scsi_Device *scsi_devs);
458 static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
459 struct ahc_devinfo *devinfo);
460 static void ahc_linux_device_queue_depth(struct ahc_softc *ahc,
461 Scsi_Device *device);
462 static struct ahc_linux_target* ahc_linux_alloc_target(struct ahc_softc*,
463 u_int, u_int);
464 static void ahc_linux_free_target(struct ahc_softc*,
465 struct ahc_linux_target*);
466 static struct ahc_linux_device* ahc_linux_alloc_device(struct ahc_softc*,
467 struct ahc_linux_target*,
468 u_int);
469 static void ahc_linux_free_device(struct ahc_softc*,
470 struct ahc_linux_device*);
471 static void ahc_linux_run_device_queue(struct ahc_softc*,
472 struct ahc_linux_device*);
473 static void ahc_linux_setup_tag_info(char *p, char *end);
474 static int ahc_linux_next_unit(void);
475 static void ahc_runq_tasklet(unsigned long data);
476 static int ahc_linux_halt(struct notifier_block *nb, u_long event, void *buf);
478 static __inline struct ahc_linux_device*
479 ahc_linux_get_device(struct ahc_softc *ahc, u_int channel,
480 u_int target, u_int lun, int alloc);
481 static __inline void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
482 Scsi_Cmnd *cmd);
483 static __inline void ahc_linux_run_complete_queue(struct ahc_softc *ahc,
484 struct ahc_cmd *acmd);
485 static __inline void ahc_linux_check_device_queue(struct ahc_softc *ahc,
486 struct ahc_linux_device *dev);
487 static __inline struct ahc_linux_device *
488 ahc_linux_next_device_to_run(struct ahc_softc *ahc);
489 static __inline void ahc_linux_run_device_queues(struct ahc_softc *ahc);
490 static __inline void ahc_linux_sniff_command(struct ahc_softc*, Scsi_Cmnd*,
491 struct scb*);
492 static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
494 static __inline int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
495 struct ahc_dma_seg *sg,
496 bus_addr_t addr, bus_size_t len);
498 static __inline struct ahc_linux_device*
499 ahc_linux_get_device(struct ahc_softc *ahc, u_int channel, u_int target,
500 u_int lun, int alloc)
501 {
502 struct ahc_linux_target *targ;
503 struct ahc_linux_device *dev;
504 u_int target_offset;
506 target_offset = target;
507 if (channel != 0)
508 target_offset += 8;
509 targ = ahc->platform_data->targets[target_offset];
510 if (targ == NULL) {
511 if (alloc != 0) {
512 targ = ahc_linux_alloc_target(ahc, channel, target);
513 if (targ == NULL)
514 return (NULL);
515 } else
516 return (NULL);
517 }
518 dev = targ->devices[lun];
519 if (dev == NULL && alloc != 0)
520 dev = ahc_linux_alloc_device(ahc, targ, lun);
521 return (dev);
522 }
524 static __inline void
525 ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, Scsi_Cmnd *cmd)
526 {
527 /*
528 * Typically, the complete queue has very few entries
529 * queued to it before the queue is emptied by
530 * ahc_linux_run_complete_queue, so sorting the entries
531 * by generation number should be inexpensive.
532 * We perform the sort so that commands that complete
533 * with an error are retuned in the order origionally
534 * queued to the controller so that any subsequent retries
535 * are performed in order. The underlying ahc routines do
536 * not guarantee the order that aborted commands will be
537 * returned to us.
538 */
539 struct ahc_completeq *completeq;
540 struct ahc_cmd *list_cmd;
541 struct ahc_cmd *acmd;
543 /*
544 * If we want the request requeued, make sure there
545 * are sufficent retries. In the old scsi error code,
546 * we used to be able to specify a result code that
547 * bypassed the retry count. Now we must use this
548 * hack.
549 */
550 if (cmd->result == (CAM_REQUEUE_REQ << 16))
551 cmd->retries--;
552 completeq = &ahc->platform_data->completeq;
553 list_cmd = TAILQ_FIRST(completeq);
554 acmd = (struct ahc_cmd *)cmd;
555 while (list_cmd != NULL
556 && acmd_scsi_cmd(list_cmd).serial_number
557 < acmd_scsi_cmd(acmd).serial_number)
558 list_cmd = TAILQ_NEXT(list_cmd, acmd_links.tqe);
559 if (list_cmd != NULL)
560 TAILQ_INSERT_BEFORE(list_cmd, acmd, acmd_links.tqe);
561 else
562 TAILQ_INSERT_TAIL(completeq, acmd, acmd_links.tqe);
563 }
565 static __inline void
566 ahc_linux_run_complete_queue(struct ahc_softc *ahc, struct ahc_cmd *acmd)
567 {
568 u_long done_flags;
570 ahc_done_lock(ahc, &done_flags);
571 while (acmd != NULL) {
572 Scsi_Cmnd *cmd;
574 cmd = &acmd_scsi_cmd(acmd);
575 acmd = TAILQ_NEXT(acmd, acmd_links.tqe);
576 cmd->host_scribble = NULL;
577 cmd->scsi_done(cmd);
578 }
579 ahc_done_unlock(ahc, &done_flags);
580 }
582 static __inline void
583 ahc_linux_check_device_queue(struct ahc_softc *ahc,
584 struct ahc_linux_device *dev)
585 {
586 if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) != 0
587 && dev->active == 0) {
588 dev->flags &= ~AHC_DEV_FREEZE_TIL_EMPTY;
589 dev->qfrozen--;
590 }
592 if (TAILQ_FIRST(&dev->busyq) == NULL
593 || dev->openings == 0 || dev->qfrozen != 0)
594 return;
596 ahc_linux_run_device_queue(ahc, dev);
597 }
599 static __inline struct ahc_linux_device *
600 ahc_linux_next_device_to_run(struct ahc_softc *ahc)
601 {
603 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0
604 || ahc->platform_data->qfrozen != 0)
605 return (NULL);
606 return (TAILQ_FIRST(&ahc->platform_data->device_runq));
607 }
609 static __inline void
610 ahc_linux_run_device_queues(struct ahc_softc *ahc)
611 {
612 struct ahc_linux_device *dev;
614 while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) {
615 TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links);
616 dev->flags &= ~AHC_DEV_ON_RUN_LIST;
617 ahc_linux_check_device_queue(ahc, dev);
618 }
619 }
621 static __inline void
622 ahc_linux_sniff_command(struct ahc_softc *ahc, Scsi_Cmnd *cmd, struct scb *scb)
623 {
624 /*
625 * Determine whether we care to filter
626 * information out of this command. If so,
627 * pass it on to ahc_linux_filter_command() for more
628 * heavy weight processing.
629 */
630 if (cmd->cmnd[0] == INQUIRY)
631 ahc_linux_filter_command(ahc, cmd, scb);
632 }
634 static __inline void
635 ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
636 {
637 Scsi_Cmnd *cmd;
639 cmd = scb->io_ctx;
640 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
641 if (cmd->use_sg != 0) {
642 struct scatterlist *sg;
644 sg = (struct scatterlist *)cmd->request_buffer;
645 pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg,
646 scsi_to_pci_dma_dir(cmd->sc_data_direction));
647 } else if (cmd->request_bufflen != 0) {
648 pci_unmap_single(ahc->dev_softc,
649 scb->platform_data->buf_busaddr,
650 cmd->request_bufflen,
651 scsi_to_pci_dma_dir(cmd->sc_data_direction));
652 }
653 }
655 static __inline int
656 ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
657 struct ahc_dma_seg *sg, bus_addr_t addr, bus_size_t len)
658 {
659 int consumed;
661 if ((scb->sg_count + 1) > AHC_NSEG)
662 panic("Too few segs for dma mapping. "
663 "Increase AHC_NSEG\n");
665 consumed = 1;
666 sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
667 scb->platform_data->xfer_len += len;
668 if (sizeof(bus_addr_t) > 4
669 && (ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
670 /*
671 * Due to DAC restrictions, we can't
672 * cross a 4GB boundary.
673 */
674 if ((addr ^ (addr + len - 1)) & ~0xFFFFFFFF) {
675 struct ahc_dma_seg *next_sg;
676 uint32_t next_len;
678 printf("Crossed Seg\n");
679 if ((scb->sg_count + 2) > AHC_NSEG)
680 panic("Too few segs for dma mapping. "
681 "Increase AHC_NSEG\n");
683 consumed++;
684 next_sg = sg + 1;
685 next_sg->addr = 0;
686 next_len = 0x100000000 - (addr & 0xFFFFFFFF);
687 len -= next_len;
688 next_len |= ((addr >> 8) + 0x1000000) & 0x7F000000;
689 next_sg->len = ahc_htole32(next_len);
690 }
691 len |= (addr >> 8) & 0x7F000000;
692 }
693 sg->len = ahc_htole32(len);
694 return (consumed);
695 }
697 /**************************** Tasklet Handler *********************************/
699 static void
700 ahc_runq_tasklet(unsigned long data)
701 {
702 struct ahc_softc* ahc;
703 struct ahc_linux_device *dev;
704 u_long flags;
706 ahc = (struct ahc_softc *)data;
707 ahc_lock(ahc, &flags);
708 while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) {
710 TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links);
711 dev->flags &= ~AHC_DEV_ON_RUN_LIST;
712 ahc_linux_check_device_queue(ahc, dev);
713 /* Yeild to our interrupt handler */
714 ahc_unlock(ahc, &flags);
715 ahc_lock(ahc, &flags);
716 }
717 ahc_unlock(ahc, &flags);
718 }
720 /************************ Shutdown/halt/reboot hook ***************************/
721 #include <xeno/notifier.h>
722 #include <xeno/reboot.h>
724 #if XENO_KILLED
725 static struct notifier_block ahc_linux_notifier = {
726 ahc_linux_halt, NULL, 0
727 };
728 #endif
730 static int ahc_linux_halt(struct notifier_block *nb, u_long event, void *buf)
731 {
732 struct ahc_softc *ahc;
734 if (event == SYS_DOWN || event == SYS_HALT) {
735 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
736 ahc_shutdown(ahc);
737 }
738 }
739 return (NOTIFY_OK);
740 }
742 /******************************** Macros **************************************/
743 #define BUILD_SCSIID(ahc, cmd) \
744 ((((cmd)->target << TID_SHIFT) & TID) \
745 | (((cmd)->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \
746 | (((cmd)->channel == 0) ? 0 : TWIN_CHNLB))
748 /******************************** Bus DMA *************************************/
749 int
750 ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent,
751 bus_size_t alignment, bus_size_t boundary,
752 bus_addr_t lowaddr, bus_addr_t highaddr,
753 bus_dma_filter_t *filter, void *filterarg,
754 bus_size_t maxsize, int nsegments,
755 bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
756 {
757 bus_dma_tag_t dmat;
759 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
760 if (dmat == NULL)
761 return (ENOMEM);
763 /*
764 * Linux is very simplistic about DMA memory. For now don't
765 * maintain all specification information. Once Linux supplies
766 * better facilities for doing these operations, or the
767 * needs of this particular driver change, we might need to do
768 * more here.
769 */
770 dmat->alignment = alignment;
771 dmat->boundary = boundary;
772 dmat->maxsize = maxsize;
773 *ret_tag = dmat;
774 return (0);
775 }
777 void
778 ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
779 {
780 free(dmat, M_DEVBUF);
781 }
783 int
784 ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
785 int flags, bus_dmamap_t *mapp)
786 {
787 bus_dmamap_t map;
789 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
790 map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT);
791 if (map == NULL)
792 return (ENOMEM);
793 /*
794 * Although we can dma data above 4GB, our
795 * "consistent" memory is below 4GB for
796 * space efficiency reasons (only need a 4byte
797 * address). For this reason, we have to reset
798 * our dma mask when doing allocations.
799 */
800 if (ahc->dev_softc != NULL) {
801 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,3)
802 pci_set_dma_mask(ahc->dev_softc, 0xFFFFFFFF);
803 #else
804 ahc->dev_softc->dma_mask = 0xFFFFFFFF;
805 #endif
806 }
807 *vaddr = pci_alloc_consistent(ahc->dev_softc,
808 dmat->maxsize, &map->bus_addr);
809 if (ahc->dev_softc != NULL) {
810 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,3)
811 pci_set_dma_mask(ahc->dev_softc,
812 ahc->platform_data->hw_dma_mask);
813 #else
814 ahc->dev_softc->dma_mask = ahc->platform_data->hw_dma_mask;
815 #endif
816 }
817 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0) */
818 /*
819 * At least in 2.2.14, malloc is a slab allocator so all
820 * allocations are aligned. We assume for these kernel versions
821 * that all allocations will be bellow 4Gig, physically contiguous,
822 * and accessable via DMA by the controller.
823 */
824 map = NULL; /* No additional information to store */
825 *vaddr = malloc(dmat->maxsize, M_DEVBUF, M_NOWAIT);
826 #endif
827 if (*vaddr == NULL)
828 return (ENOMEM);
829 *mapp = map;
830 return(0);
831 }
833 void
834 ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
835 void* vaddr, bus_dmamap_t map)
836 {
837 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
838 pci_free_consistent(ahc->dev_softc, dmat->maxsize,
839 vaddr, map->bus_addr);
840 #else
841 free(vaddr, M_DEVBUF);
842 #endif
843 }
845 int
846 ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map,
847 void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
848 void *cb_arg, int flags)
849 {
850 /*
851 * Assume for now that this will only be used during
852 * initialization and not for per-transaction buffer mapping.
853 */
854 bus_dma_segment_t stack_sg;
856 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
857 stack_sg.ds_addr = map->bus_addr;
858 #else
859 stack_sg.ds_addr = VIRT_TO_BUS(buf);
860 #endif
861 stack_sg.ds_len = dmat->maxsize;
862 cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
863 return (0);
864 }
866 void
867 ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
868 {
869 /*
870 * The map may is NULL in our < 2.3.X implementation.
871 */
872 if (map != NULL)
873 free(map, M_DEVBUF);
874 }
876 int
877 ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
878 {
879 /* Nothing to do */
880 return (0);
881 }
883 /********************* Platform Dependent Functions ***************************/
884 int
885 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
886 {
887 int value;
888 int rvalue;
889 int lvalue;
891 /*
892 * Under Linux, cards are ordered as follows:
893 * 1) VLB/EISA BIOS enabled devices sorted by BIOS address.
894 * 2) PCI devices with BIOS enabled sorted by bus/slot/func.
895 * 3) All remaining VLB/EISA devices sorted by ioport.
896 * 4) All remaining PCI devices sorted by bus/slot/func.
897 */
898 value = (lahc->flags & AHC_BIOS_ENABLED)
899 - (rahc->flags & AHC_BIOS_ENABLED);
900 if (value != 0)
901 /* Controllers with BIOS enabled have a *higher* priority */
902 return (-value);
904 /*
905 * Same BIOS setting, now sort based on bus type.
906 * EISA and VL controllers sort together. EISA/VL
907 * have higher priority than PCI.
908 */
909 rvalue = (rahc->chip & AHC_BUS_MASK);
910 if (rvalue == AHC_VL)
911 rvalue = AHC_EISA;
912 lvalue = (lahc->chip & AHC_BUS_MASK);
913 if (lvalue == AHC_VL)
914 lvalue = AHC_EISA;
915 value = lvalue - rvalue;
916 if (value != 0)
917 return (value);
919 /* Still equal. Sort by BIOS address, ioport, or bus/slot/func. */
920 switch (rvalue) {
921 case AHC_PCI:
922 {
923 char primary_channel;
925 if (aic7xxx_reverse_scan != 0)
926 value = ahc_get_pci_bus(rahc->dev_softc)
927 - ahc_get_pci_bus(lahc->dev_softc);
928 else
929 value = ahc_get_pci_bus(lahc->dev_softc)
930 - ahc_get_pci_bus(rahc->dev_softc);
931 if (value != 0)
932 break;
933 if (aic7xxx_reverse_scan != 0)
934 value = ahc_get_pci_slot(rahc->dev_softc)
935 - ahc_get_pci_slot(lahc->dev_softc);
936 else
937 value = ahc_get_pci_slot(lahc->dev_softc)
938 - ahc_get_pci_slot(rahc->dev_softc);
939 if (value != 0)
940 break;
941 /*
942 * On multi-function devices, the user can choose
943 * to have function 1 probed before function 0.
944 * Give whichever channel is the primary channel
945 * the lowest priority.
946 */
947 primary_channel = (lahc->flags & AHC_PRIMARY_CHANNEL) + 'A';
948 value = 1;
949 if (lahc->channel == primary_channel)
950 value = -1;
951 break;
952 }
953 case AHC_EISA:
954 if ((rahc->flags & AHC_BIOS_ENABLED) != 0) {
955 value = lahc->platform_data->bios_address
956 - rahc->platform_data->bios_address;
957 } else {
958 value = lahc->bsh.ioport
959 - rahc->bsh.ioport;
960 }
961 break;
962 default:
963 panic("ahc_softc_sort: invalid bus type");
964 }
965 return (value);
966 }
968 static void
969 ahc_linux_setup_tag_info(char *p, char *end)
970 {
971 char *base;
972 char *tok;
973 char *tok_end;
974 char *tok_end2;
975 int i;
976 int instance;
977 int targ;
978 int done;
979 char tok_list[] = {'.', ',', '{', '}', '\0'};
981 if (*p != ':')
982 return;
984 instance = -1;
985 targ = -1;
986 done = FALSE;
987 base = p;
988 /* Forward us just past the ':' */
989 tok = base + 1;
990 tok_end = strchr(tok, '\0');
991 if (tok_end < end)
992 *tok_end = ',';
993 while (!done) {
994 switch (*tok) {
995 case '{':
996 if (instance == -1)
997 instance = 0;
998 else if (targ == -1)
999 targ = 0;
1000 tok++;
1001 break;
1002 case '}':
1003 if (targ != -1)
1004 targ = -1;
1005 else if (instance != -1)
1006 instance = -1;
1007 tok++;
1008 break;
1009 case ',':
1010 case '.':
1011 if (instance == -1)
1012 done = TRUE;
1013 else if (targ >= 0)
1014 targ++;
1015 else if (instance >= 0)
1016 instance++;
1017 if ((targ >= AHC_NUM_TARGETS) ||
1018 (instance >= NUM_ELEMENTS(aic7xxx_tag_info)))
1019 done = TRUE;
1020 tok++;
1021 if (!done) {
1022 base = tok;
1024 break;
1025 case '\0':
1026 done = TRUE;
1027 break;
1028 default:
1029 done = TRUE;
1030 tok_end = strchr(tok, '\0');
1031 for (i = 0; tok_list[i]; i++) {
1032 tok_end2 = strchr(tok, tok_list[i]);
1033 if ((tok_end2) && (tok_end2 < tok_end)) {
1034 tok_end = tok_end2;
1035 done = FALSE;
1038 if ((instance >= 0) && (targ >= 0)
1039 && (instance < NUM_ELEMENTS(aic7xxx_tag_info))
1040 && (targ < AHC_NUM_TARGETS)) {
1041 aic7xxx_tag_info[instance].tag_commands[targ] =
1042 simple_strtoul(tok, NULL, 0) & 0xff;
1044 tok = tok_end;
1045 break;
1048 while ((p != base) && (p != NULL))
1049 p = strtok(NULL, ",.");
1052 /*
1053 * Handle Linux boot parameters. This routine allows for assigning a value
1054 * to a parameter with a ':' between the parameter and the value.
1055 * ie. aic7xxx=stpwlev:1,extended
1056 */
1057 int
1058 aic7xxx_setup(char *s)
1060 int i, n;
1061 char *p;
1062 char *end;
1064 static struct {
1065 const char *name;
1066 uint32_t *flag;
1067 } options[] = {
1068 { "extended", &aic7xxx_extended },
1069 { "no_reset", &aic7xxx_no_reset },
1070 { "verbose", &aic7xxx_verbose },
1071 { "reverse_scan", &aic7xxx_reverse_scan },
1072 { "no_probe", &aic7xxx_no_probe },
1073 { "periodic_otag", &aic7xxx_periodic_otag },
1074 { "pci_parity", &aic7xxx_pci_parity },
1075 { "seltime", &aic7xxx_seltime },
1076 { "tag_info", NULL }
1077 };
1079 end = strchr(s, '\0');
1081 for (p = strtok(s, ",."); p; p = strtok(NULL, ",.")) {
1082 for (i = 0; i < NUM_ELEMENTS(options); i++) {
1083 n = strlen(options[i].name);
1085 if (strncmp(options[i].name, p, n) != 0)
1086 continue;
1088 if (strncmp(p, "tag_info", n) == 0) {
1089 ahc_linux_setup_tag_info(p + n, end);
1090 } else if (p[n] == ':') {
1091 *(options[i].flag) =
1092 simple_strtoul(p + n + 1, NULL, 0);
1093 } else if (!strncmp(p, "verbose", n)) {
1094 *(options[i].flag) = 1;
1095 } else {
1096 *(options[i].flag) = ~(*(options[i].flag));
1098 break;
1101 return 1;
1104 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,0)
1105 __setup("aic7xxx=", aic7xxx_setup);
1106 #endif
1108 int aic7xxx_verbose;
1110 /*
1111 * Try to detect an Adaptec 7XXX controller.
1112 */
1113 int
1114 ahc_linux_detect(Scsi_Host_Template *template)
1116 struct ahc_softc *ahc;
1117 int found;
1119 /*
1120 * It is a bug that the upper layer takes
1121 * this lock just prior to calling us.
1122 */
1123 spin_unlock_irq(&io_request_lock);
1125 /*
1126 * Sanity checking of Linux SCSI data structures so
1127 * that some of our hacks^H^H^H^H^Hassumptions aren't
1128 * violated.
1129 */
1130 if (offsetof(struct ahc_cmd_internal, end)
1131 > offsetof(struct scsi_cmnd, host_scribble)) {
1132 printf("ahc_linux_detect: SCSI data structures changed.\n");
1133 printf("ahc_linux_detect: Unable to attach\n");
1134 return (0);
1136 #ifdef MODULE
1137 /*
1138 * If we've been passed any parameters, process them now.
1139 */
1140 if (aic7xxx)
1141 aic7xxx_setup(aic7xxx);
1142 if (dummy_buffer[0] != 'P')
1143 printf(KERN_WARNING
1144 "aic7xxx: Please read the file /usr/src/xeno/drivers/scsi/README.aic7xxx\n"
1145 "aic7xxx: to see the proper way to specify options to the aic7xxx module\n"
1146 "aic7xxx: Specifically, don't use any commas when passing arguments to\n"
1147 "aic7xxx: insmod or else it might trash certain memory areas.\n");
1148 #endif
1150 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,0)
1151 template->proc_name = "aic7xxx";
1152 #else
1153 template->proc_dir = &proc_scsi_aic7xxx;
1154 #endif
1156 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,7)
1157 /*
1158 * We can only map 16MB per-SG
1159 * so create a sector limit of
1160 * "16MB" in 2K sectors.
1161 */
1162 template->max_sectors = 8192;
1163 #endif
1165 /*
1166 * Initialize our softc list lock prior to
1167 * probing for any adapters.
1168 */
1169 ahc_list_lockinit();
1171 #ifdef CONFIG_PCI
1172 ahc_linux_pci_probe(template);
1173 #endif
1175 if (aic7xxx_no_probe == 0)
1176 aic7770_linux_probe(template);
1178 /*
1179 * Register with the SCSI layer all
1180 * controllers we've found.
1181 */
1182 spin_lock_irq(&io_request_lock);
1183 found = 0;
1184 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
1186 if (ahc_linux_register_host(ahc, template) == 0)
1187 found++;
1189 aic7xxx_detect_complete++;
1190 return (found);
1193 int
1194 ahc_linux_register_host(struct ahc_softc *ahc, Scsi_Host_Template *template)
1196 char buf[80];
1197 struct Scsi_Host *host;
1198 char *new_name;
1199 u_long s;
1202 template->name = ahc->description;
1203 host = scsi_register(template, sizeof(struct ahc_softc *));
1204 if (host == NULL)
1205 return (ENOMEM);
1207 ahc_lock(ahc, &s);
1208 *((struct ahc_softc **)host->hostdata) = ahc;
1209 ahc->platform_data->host = host;
1210 host->can_queue = AHC_MAX_QUEUE;
1211 host->cmd_per_lun = 2;
1212 host->sg_tablesize = AHC_NSEG;
1213 host->select_queue_depths = ahc_linux_select_queue_depth;
1214 /* XXX No way to communicate the ID for multiple channels */
1215 host->this_id = ahc->our_id;
1216 host->irq = ahc->platform_data->irq;
1217 host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
1218 host->max_lun = AHC_NUM_LUNS;
1219 host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
1220 ahc_set_unit(ahc, ahc_linux_next_unit());
1221 sprintf(buf, "scsi%d", host->host_no);
1222 new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
1223 if (new_name != NULL) {
1224 strcpy(new_name, buf);
1225 ahc_set_name(ahc, new_name);
1227 host->unique_id = ahc->unit;
1228 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,4)
1229 scsi_set_pci_device(host, ahc->dev_softc);
1230 #endif
1231 ahc_linux_initialize_scsi_bus(ahc);
1232 ahc_unlock(ahc, &s);
1233 return (0);
1236 uint64_t
1237 ahc_linux_get_memsize()
1239 // struct sysinfo si;
1240 //
1241 // si_meminfo(&si);
1242 // return (si.totalram << PAGE_SHIFT);
1243 printf("JWS: aic7xxx: get_memsize\n");
1244 return 0;
1247 /*
1248 * Find the smallest available unit number to use
1249 * for a new device. We don't just use a static
1250 * count to handle the "repeated hot-(un)plug"
1251 * scenario.
1252 */
1253 static int
1254 ahc_linux_next_unit()
1256 struct ahc_softc *ahc;
1257 int unit;
1259 unit = 0;
1260 retry:
1261 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
1262 if (ahc->unit == unit) {
1263 unit++;
1264 goto retry;
1267 return (unit);
1270 /*
1271 * Place the SCSI bus into a known state by either resetting it,
1272 * or forcing transfer negotiations on the next command to any
1273 * target.
1274 */
1275 void
1276 ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
1278 int i;
1279 int numtarg;
1281 i = 0;
1282 numtarg = 0;
1284 if (aic7xxx_no_reset != 0)
1285 ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
1287 if ((ahc->flags & AHC_RESET_BUS_A) != 0)
1288 ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
1289 else
1290 numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1292 if ((ahc->features & AHC_TWIN) != 0) {
1294 if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
1295 ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
1296 } else {
1297 if (numtarg == 0)
1298 i = 8;
1299 numtarg += 8;
1303 for (; i < numtarg; i++) {
1304 struct ahc_devinfo devinfo;
1305 struct ahc_initiator_tinfo *tinfo;
1306 struct ahc_tmode_tstate *tstate;
1307 u_int our_id;
1308 u_int target_id;
1309 char channel;
1311 channel = 'A';
1312 our_id = ahc->our_id;
1313 target_id = i;
1314 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
1315 channel = 'B';
1316 our_id = ahc->our_id_b;
1317 target_id = i % 8;
1319 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
1320 target_id, &tstate);
1321 tinfo->goal = tinfo->user;
1322 /*
1323 * Don't try negotiations that require PPR messages
1324 * until we successfully retrieve Inquiry data.
1325 */
1326 tinfo->goal.ppr_options = 0;
1327 if (tinfo->goal.transport_version > SCSI_REV_2)
1328 tinfo->goal.transport_version = SCSI_REV_2;
1329 ahc_compile_devinfo(&devinfo, our_id, target_id,
1330 CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
1331 ahc_update_neg_request(ahc, &devinfo, tstate,
1332 tinfo, /*force*/FALSE);
1334 /* Give the bus some time to recover */
1335 if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
1336 // JWS - XEN - DONT USE TIMERS HERE
1337 #if 0
1338 ahc_linux_freeze_sim_queue(ahc);
1339 init_timer(&ahc->platform_data->reset_timer);
1340 ahc->platform_data->reset_timer.data = (u_long)ahc;
1341 ahc->platform_data->reset_timer.expires =
1342 jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000;
1343 ahc->platform_data->reset_timer.function =
1344 ahc_linux_release_sim_queue;
1345 add_timer(&ahc->platform_data->reset_timer);
1346 #else
1347 mdelay(AIC7XXX_RESET_DELAY);
1348 #endif
1352 int
1353 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1355 ahc->platform_data =
1356 malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
1357 if (ahc->platform_data == NULL)
1358 return (ENOMEM);
1359 memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
1360 TAILQ_INIT(&ahc->platform_data->completeq);
1361 TAILQ_INIT(&ahc->platform_data->device_runq);
1362 ahc->platform_data->irq = AHC_LINUX_NOIRQ;
1363 ahc->platform_data->hw_dma_mask = 0xFFFFFFFF;
1364 ahc_lockinit(ahc);
1365 ahc_done_lockinit(ahc);
1366 #if 0
1367 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
1368 init_MUTEX_LOCKED(&ahc->platform_data->eh_sem);
1369 #else
1370 ahc->platform_data->eh_sem = MUTEX_LOCKED;
1371 #endif
1372 #endif
1373 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1374 tasklet_init(&ahc->platform_data->runq_tasklet, ahc_runq_tasklet,
1375 (unsigned long)ahc);
1376 #endif
1377 ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
1378 ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
1379 #if XENO_KILLED
1380 if (TAILQ_EMPTY(&ahc_tailq))
1381 register_reboot_notifier(&ahc_linux_notifier);
1382 #endif
1383 return (0);
1386 void
1387 ahc_platform_free(struct ahc_softc *ahc)
1389 if (ahc->platform_data != NULL) {
1390 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1391 tasklet_kill(&ahc->platform_data->runq_tasklet);
1392 #endif
1393 if (ahc->platform_data->host != NULL)
1394 scsi_unregister(ahc->platform_data->host);
1395 if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
1396 free_irq(ahc->platform_data->irq, ahc);
1397 if (ahc->tag == BUS_SPACE_PIO
1398 && ahc->bsh.ioport != 0)
1399 release_region(ahc->bsh.ioport, 256);
1400 if (ahc->tag == BUS_SPACE_MEMIO
1401 && ahc->bsh.maddr != NULL) {
1402 u_long base_addr;
1404 base_addr = (u_long)ahc->bsh.maddr;
1405 base_addr &= PAGE_MASK;
1406 iounmap((void *)base_addr);
1407 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1408 release_mem_region(ahc->platform_data->mem_busaddr,
1409 0x1000);
1410 #endif
1412 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1413 /* XXX Need an instance detach in the PCI code */
1414 if (ahc->dev_softc != NULL)
1415 ahc->dev_softc->driver = NULL;
1416 #endif
1417 free(ahc->platform_data, M_DEVBUF);
1419 if (TAILQ_EMPTY(&ahc_tailq)) {
1420 #ifdef XENO_KILLED
1421 unregister_reboot_notifier(&ahc_linux_notifier);
1422 #endif
1423 #ifdef CONFIG_PCI
1424 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1425 pci_unregister_driver(&aic7xxx_pci_driver);
1426 #endif
1427 #endif
1431 void
1432 ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
1434 ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1435 SCB_GET_CHANNEL(ahc, scb),
1436 SCB_GET_LUN(scb), SCB_LIST_NULL,
1437 ROLE_UNKNOWN, CAM_REQUEUE_REQ);
1440 void
1441 ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1442 ahc_queue_alg alg)
1444 struct ahc_linux_device *dev;
1445 int was_queuing;
1446 int now_queuing;
1448 dev = ahc_linux_get_device(ahc, devinfo->channel - 'A',
1449 devinfo->target,
1450 devinfo->lun, /*alloc*/FALSE);
1451 if (dev == NULL)
1452 return;
1453 was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
1454 now_queuing = alg != AHC_QUEUE_NONE;
1455 if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
1456 && (was_queuing != now_queuing)
1457 && (dev->active != 0)) {
1458 dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
1459 dev->qfrozen++;
1462 dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
1463 if (now_queuing) {
1464 u_int usertags;
1466 usertags = ahc_linux_user_tagdepth(ahc, devinfo);
1467 if (!was_queuing) {
1468 /*
1469 * Start out agressively and allow our
1470 * dynamic queue depth algorithm to take
1471 * care of the rest.
1472 */
1473 dev->maxtags = usertags;
1474 dev->openings = dev->maxtags - dev->active;
1476 if (alg == AHC_QUEUE_TAGGED) {
1477 dev->flags |= AHC_DEV_Q_TAGGED;
1478 if (aic7xxx_periodic_otag != 0)
1479 dev->flags |= AHC_DEV_PERIODIC_OTAG;
1480 } else
1481 dev->flags |= AHC_DEV_Q_BASIC;
1482 } else {
1483 /* We can only have one opening. */
1484 dev->maxtags = 0;
1485 dev->openings = 1 - dev->active;
1489 int
1490 ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
1491 int lun, u_int tag, role_t role, uint32_t status)
1493 int chan;
1494 int maxchan;
1495 int targ;
1496 int maxtarg;
1497 int clun;
1498 int maxlun;
1499 int count;
1501 if (tag != SCB_LIST_NULL)
1502 return (0);
1504 chan = 0;
1505 if (channel != ALL_CHANNELS) {
1506 chan = channel - 'A';
1507 maxchan = chan + 1;
1508 } else {
1509 maxchan = (ahc->features & AHC_TWIN) ? 2 : 1;
1511 targ = 0;
1512 if (target != CAM_TARGET_WILDCARD) {
1513 targ = target;
1514 maxtarg = targ + 1;
1515 } else {
1516 maxtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1518 clun = 0;
1519 if (lun != CAM_LUN_WILDCARD) {
1520 clun = lun;
1521 maxlun = clun + 1;
1522 } else {
1523 maxlun = AHC_NUM_LUNS;
1526 count = 0;
1527 for (; chan < maxchan; chan++) {
1529 for (; targ < maxtarg; targ++) {
1531 for (; clun < maxlun; clun++) {
1532 struct ahc_linux_device *dev;
1533 struct ahc_busyq *busyq;
1534 struct ahc_cmd *acmd;
1536 dev = ahc_linux_get_device(ahc, chan,
1537 targ, clun,
1538 /*alloc*/FALSE);
1539 if (dev == NULL)
1540 continue;
1542 busyq = &dev->busyq;
1543 while ((acmd = TAILQ_FIRST(busyq)) != NULL) {
1544 Scsi_Cmnd *cmd;
1546 cmd = &acmd_scsi_cmd(acmd);
1547 TAILQ_REMOVE(busyq, acmd,
1548 acmd_links.tqe);
1549 count++;
1550 cmd->result = status << 16;
1551 ahc_linux_queue_cmd_complete(ahc, cmd);
1557 return (count);
1560 /*
1561 * Sets the queue depth for each SCSI device hanging
1562 * off the input host adapter.
1563 */
1564 static void
1565 ahc_linux_select_queue_depth(struct Scsi_Host * host,
1566 Scsi_Device * scsi_devs)
1568 Scsi_Device *device;
1569 struct ahc_softc *ahc;
1570 u_long flags;
1571 int scbnum;
1573 ahc = *((struct ahc_softc **)host->hostdata);
1574 ahc_lock(ahc, &flags);
1575 scbnum = 0;
1576 for (device = scsi_devs; device != NULL; device = device->next) {
1577 if (device->host == host) {
1578 ahc_linux_device_queue_depth(ahc, device);
1579 scbnum += device->queue_depth;
1582 ahc_unlock(ahc, &flags);
1585 static u_int
1586 ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1588 static int warned_user;
1589 u_int tags;
1591 tags = 0;
1592 if ((ahc->user_discenable & devinfo->target_mask) != 0) {
1593 if (warned_user == 0
1594 && ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
1596 printf("aic7xxx: WARNING, insufficient "
1597 "tag_info instances for installed "
1598 "controllers. Using defaults\n");
1599 printf("aic7xxx: Please update the "
1600 "aic7xxx_tag_info array in the "
1601 "aic7xxx.c source file.\n");
1602 tags = AHC_MAX_QUEUE;
1603 warned_user++;
1604 } else {
1605 adapter_tag_info_t *tag_info;
1607 tag_info = &aic7xxx_tag_info[ahc->unit];
1608 tags = tag_info->tag_commands[devinfo->target_offset];
1609 if (tags > AHC_MAX_QUEUE)
1610 tags = AHC_MAX_QUEUE;
1613 return (tags);
1616 /*
1617 * Determines the queue depth for a given device.
1618 */
1619 static void
1620 ahc_linux_device_queue_depth(struct ahc_softc *ahc, Scsi_Device * device)
1622 struct ahc_devinfo devinfo;
1623 u_int tags;
1625 ahc_compile_devinfo(&devinfo,
1626 device->channel == 0 ? ahc->our_id : ahc->our_id_b,
1627 device->id, device->lun,
1628 device->channel == 0 ? 'A' : 'B',
1629 ROLE_INITIATOR);
1630 tags = ahc_linux_user_tagdepth(ahc, &devinfo);
1631 if (tags != 0
1632 && device->tagged_supported != 0) {
1634 device->queue_depth = tags;
1635 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
1636 printf("scsi%d:%c:%d:%d: Tagged Queuing enabled. Depth %d\n",
1637 ahc->platform_data->host->host_no, devinfo.channel,
1638 devinfo.target, devinfo.lun, tags);
1639 } else {
1640 /*
1641 * We allow the OS to queue 2 untagged transactions to
1642 * us at any time even though we can only execute them
1643 * serially on the controller/device. This should remove
1644 * some latency.
1645 */
1646 device->queue_depth = 2;
1650 /*
1651 * Queue an SCB to the controller.
1652 */
1653 int
1654 ahc_linux_queue(Scsi_Cmnd * cmd, void (*scsi_done) (Scsi_Cmnd *))
1656 struct ahc_softc *ahc;
1657 struct ahc_linux_device *dev;
1658 u_long flags;
1660 ahc = *(struct ahc_softc **)cmd->host->hostdata;
1662 /*
1663 * Save the callback on completion function.
1664 */
1665 cmd->scsi_done = scsi_done;
1667 ahc_lock(ahc, &flags);
1668 dev = ahc_linux_get_device(ahc, cmd->channel, cmd->target,
1669 cmd->lun, /*alloc*/TRUE);
1670 if (dev == NULL) {
1671 ahc_unlock(ahc, &flags);
1672 printf("aic7xxx_linux_queue: Unable to allocate device!\n");
1673 return (-ENOMEM);
1675 cmd->result = CAM_REQ_INPROG << 16;
1676 TAILQ_INSERT_TAIL(&dev->busyq, (struct ahc_cmd *)cmd, acmd_links.tqe);
1677 if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) {
1678 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links);
1679 dev->flags |= AHC_DEV_ON_RUN_LIST;
1680 ahc_linux_run_device_queues(ahc);
1682 ahc_unlock(ahc, &flags);
1683 return (0);
1686 static void
1687 ahc_linux_run_device_queue(struct ahc_softc *ahc, struct ahc_linux_device *dev)
1689 struct ahc_cmd *acmd;
1690 struct scsi_cmnd *cmd;
1691 struct scb *scb;
1692 struct hardware_scb *hscb;
1693 struct ahc_initiator_tinfo *tinfo;
1694 struct ahc_tmode_tstate *tstate;
1695 uint16_t mask;
1697 if ((dev->flags & AHC_DEV_ON_RUN_LIST) != 0)
1698 panic("running device on run list");
1700 while ((acmd = TAILQ_FIRST(&dev->busyq)) != NULL
1701 && dev->openings > 0 && dev->qfrozen == 0) {
1703 /*
1704 * Schedule us to run later. The only reason we are not
1705 * running is because the whole controller Q is frozen.
1706 */
1707 if (ahc->platform_data->qfrozen != 0) {
1709 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
1710 dev, links);
1711 dev->flags |= AHC_DEV_ON_RUN_LIST;
1712 return;
1714 /*
1715 * Get an scb to use.
1716 */
1717 if ((scb = ahc_get_scb(ahc)) == NULL) {
1718 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq,
1719 dev, links);
1720 dev->flags |= AHC_DEV_ON_RUN_LIST;
1721 ahc->flags |= AHC_RESOURCE_SHORTAGE;
1722 return;
1724 TAILQ_REMOVE(&dev->busyq, acmd, acmd_links.tqe);
1725 cmd = &acmd_scsi_cmd(acmd);
1726 scb->io_ctx = cmd;
1727 scb->platform_data->dev = dev;
1728 hscb = scb->hscb;
1729 cmd->host_scribble = (char *)scb;
1731 /*
1732 * Fill out basics of the HSCB.
1733 */
1734 hscb->control = 0;
1735 hscb->scsiid = BUILD_SCSIID(ahc, cmd);
1736 hscb->lun = cmd->lun;
1737 mask = SCB_GET_TARGET_MASK(ahc, scb);
1738 tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
1739 SCB_GET_OUR_ID(scb),
1740 SCB_GET_TARGET(ahc, scb), &tstate);
1741 hscb->scsirate = tinfo->scsirate;
1742 hscb->scsioffset = tinfo->curr.offset;
1743 if ((tstate->ultraenb & mask) != 0)
1744 hscb->control |= ULTRAENB;
1746 if ((ahc->user_discenable & mask) != 0)
1747 hscb->control |= DISCENB;
1749 if ((tstate->auto_negotiate & mask) != 0) {
1750 scb->flags |= SCB_AUTO_NEGOTIATE;
1751 scb->hscb->control |= MK_MESSAGE;
1754 if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
1755 if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
1756 && (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
1757 hscb->control |= MSG_ORDERED_TASK;
1758 dev->commands_since_idle_or_otag = 0;
1759 } else {
1760 hscb->control |= MSG_SIMPLE_TASK;
1764 hscb->cdb_len = cmd->cmd_len;
1765 if (hscb->cdb_len <= 12) {
1766 memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
1767 } else {
1768 memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
1769 scb->flags |= SCB_CDB32_PTR;
1772 scb->platform_data->xfer_len = 0;
1773 ahc_set_residual(scb, 0);
1774 ahc_set_sense_residual(scb, 0);
1775 scb->sg_count = 0;
1776 if (cmd->use_sg != 0) {
1777 struct ahc_dma_seg *sg;
1778 struct scatterlist *cur_seg;
1779 struct scatterlist *end_seg;
1780 int nseg;
1782 cur_seg = (struct scatterlist *)cmd->request_buffer;
1783 nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
1784 scsi_to_pci_dma_dir(cmd ->sc_data_direction));
1785 end_seg = cur_seg + nseg;
1786 /* Copy the segments into the SG list. */
1787 sg = scb->sg_list;
1788 /*
1789 * The sg_count may be larger than nseg if
1790 * a transfer crosses a 32bit page.
1791 */
1792 while (cur_seg < end_seg) {
1793 bus_addr_t addr;
1794 bus_size_t len;
1795 int consumed;
1797 addr = sg_dma_address(cur_seg);
1798 len = sg_dma_len(cur_seg);
1799 consumed = ahc_linux_map_seg(ahc, scb,
1800 sg, addr, len);
1801 sg += consumed;
1802 scb->sg_count += consumed;
1803 cur_seg++;
1805 sg--;
1806 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1808 /*
1809 * Reset the sg list pointer.
1810 */
1811 scb->hscb->sgptr =
1812 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
1814 /*
1815 * Copy the first SG into the "current"
1816 * data pointer area.
1817 */
1818 scb->hscb->dataptr = scb->sg_list->addr;
1819 scb->hscb->datacnt = scb->sg_list->len;
1820 } else if (cmd->request_bufflen != 0) {
1821 struct ahc_dma_seg *sg;
1822 bus_addr_t addr;
1824 sg = scb->sg_list;
1825 addr = pci_map_single(ahc->dev_softc,
1826 cmd->request_buffer,
1827 cmd->request_bufflen,
1828 scsi_to_pci_dma_dir(cmd->sc_data_direction));
1829 scb->platform_data->buf_busaddr = addr;
1830 scb->sg_count = ahc_linux_map_seg(ahc, scb,
1831 sg, addr,
1832 cmd->request_bufflen);
1833 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1835 /*
1836 * Reset the sg list pointer.
1837 */
1838 scb->hscb->sgptr =
1839 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
1841 /*
1842 * Copy the first SG into the "current"
1843 * data pointer area.
1844 */
1845 scb->hscb->dataptr = sg->addr;
1846 scb->hscb->datacnt = sg->len;
1847 } else {
1848 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
1849 scb->hscb->dataptr = 0;
1850 scb->hscb->datacnt = 0;
1851 scb->sg_count = 0;
1854 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_PREWRITE);
1855 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1856 dev->openings--;
1857 dev->active++;
1858 dev->commands_issued++;
1859 if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
1860 dev->commands_since_idle_or_otag++;
1862 /*
1863 * We only allow one untagged transaction
1864 * per target in the initiator role unless
1865 * we are storing a full busy target *lun*
1866 * table in SCB space.
1867 */
1868 if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1869 && (ahc->features & AHC_SCB_BTT) == 0) {
1870 struct scb_tailq *untagged_q;
1871 int target_offset;
1873 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1874 untagged_q = &(ahc->untagged_queues[target_offset]);
1875 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1876 scb->flags |= SCB_UNTAGGEDQ;
1877 if (TAILQ_FIRST(untagged_q) != scb)
1878 continue;
1880 scb->flags |= SCB_ACTIVE;
1881 ahc_queue_scb(ahc, scb);
1885 /*
1886 * SCSI controller interrupt handler.
1887 */
1888 void
1889 ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
1891 struct ahc_softc *ahc;
1892 struct ahc_cmd *acmd;
1893 u_long flags;
1894 struct ahc_linux_device *next_dev;
1896 ahc = (struct ahc_softc *) dev_id;
1897 ahc_lock(ahc, &flags);
1898 ahc_intr(ahc);
1899 acmd = TAILQ_FIRST(&ahc->platform_data->completeq);
1900 TAILQ_INIT(&ahc->platform_data->completeq);
1901 next_dev = ahc_linux_next_device_to_run(ahc);
1902 ahc_unlock(ahc, &flags);
1903 if (next_dev) {
1904 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1905 tasklet_schedule(&ahc->platform_data->runq_tasklet);
1906 #else
1907 ahc_runq_tasklet((unsigned long)ahc);
1908 #endif
1910 if (acmd != NULL)
1911 ahc_linux_run_complete_queue(ahc, acmd);
1914 void
1915 ahc_platform_flushwork(struct ahc_softc *ahc)
1917 struct ahc_cmd *acmd;
1919 acmd = TAILQ_FIRST(&ahc->platform_data->completeq);
1920 TAILQ_INIT(&ahc->platform_data->completeq);
1921 if (acmd != NULL)
1922 ahc_linux_run_complete_queue(ahc, acmd);
1925 static struct ahc_linux_target*
1926 ahc_linux_alloc_target(struct ahc_softc *ahc, u_int channel, u_int target)
1928 struct ahc_linux_target *targ;
1929 u_int target_offset;
1931 targ = malloc(sizeof(*targ), M_DEVBUG, M_NOWAIT);
1932 if (targ == NULL)
1933 return (NULL);
1934 memset(targ, 0, sizeof(*targ));
1935 targ->channel = channel;
1936 targ->target = target;
1937 targ->ahc = ahc;
1938 target_offset = target;
1939 if (channel != 0)
1940 target_offset += 8;
1941 ahc->platform_data->targets[target_offset] = targ;
1942 return (targ);
1945 static void
1946 ahc_linux_free_target(struct ahc_softc *ahc, struct ahc_linux_target *targ)
1948 u_int target_offset;
1950 target_offset = targ->target;
1951 if (targ->channel != 0)
1952 target_offset += 8;
1953 ahc->platform_data->targets[target_offset] = NULL;
1954 free(targ, M_DEVBUF);
1957 static struct ahc_linux_device*
1958 ahc_linux_alloc_device(struct ahc_softc *ahc,
1959 struct ahc_linux_target *targ, u_int lun)
1961 struct ahc_linux_device *dev;
1963 dev = malloc(sizeof(*dev), M_DEVBUG, M_NOWAIT);
1964 if (dev == NULL)
1965 return (NULL);
1966 memset(dev, 0, sizeof(*dev));
1967 init_timer(&dev->timer);
1968 TAILQ_INIT(&dev->busyq);
1969 dev->flags = AHC_DEV_UNCONFIGURED;
1970 dev->lun = lun;
1971 dev->target = targ;
1973 /*
1974 * We start out life using untagged
1975 * transactions of which we allow one.
1976 */
1977 dev->openings = 1;
1979 /*
1980 * Set maxtags to 0. This will be changed if we
1981 * later determine that we are dealing with
1982 * a tagged queuing capable device.
1983 */
1984 dev->maxtags = 0;
1986 targ->refcount++;
1987 targ->devices[lun] = dev;
1988 return (dev);
1991 static void
1992 ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
1994 struct ahc_linux_target *targ;
1996 del_timer(&dev->timer);
1997 targ = dev->target;
1998 targ->devices[dev->lun] = NULL;
1999 free(dev, M_DEVBUF);
2000 targ->refcount--;
2001 if (targ->refcount == 0)
2002 ahc_linux_free_target(ahc, targ);
2005 /*
2006 * Return a string describing the driver.
2007 */
2008 const char *
2009 ahc_linux_info(struct Scsi_Host *host)
2011 static char buffer[512];
2012 char ahc_info[256];
2013 char *bp;
2014 struct ahc_softc *ahc;
2016 bp = &buffer[0];
2017 ahc = *(struct ahc_softc **)host->hostdata;
2018 memset(bp, 0, sizeof(buffer));
2019 strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev ");
2020 strcat(bp, AIC7XXX_DRIVER_VERSION);
2021 strcat(bp, "\n");
2022 strcat(bp, " <");
2023 strcat(bp, ahc->description);
2024 strcat(bp, ">\n");
2025 strcat(bp, " ");
2026 ahc_controller_info(ahc, ahc_info);
2027 strcat(bp, ahc_info);
2028 strcat(bp, "\n");
2030 return (bp);
2033 void
2034 ahc_send_async(struct ahc_softc *ahc, char channel,
2035 u_int target, u_int lun, ac_code code, void *arg)
2037 switch (code) {
2038 case AC_TRANSFER_NEG:
2040 char buf[80];
2041 struct ahc_linux_target *targ;
2042 struct info_str info;
2043 struct ahc_initiator_tinfo *tinfo;
2044 struct ahc_tmode_tstate *tstate;
2045 int target_offset;
2047 info.buffer = buf;
2048 info.length = sizeof(buf);
2049 info.offset = 0;
2050 info.pos = 0;
2051 tinfo = ahc_fetch_transinfo(ahc, channel,
2052 channel == 'A' ? ahc->our_id
2053 : ahc->our_id_b,
2054 target, &tstate);
2056 /*
2057 * Don't bother reporting results while
2058 * negotiations are still pending.
2059 */
2060 if (tinfo->curr.period != tinfo->goal.period
2061 || tinfo->curr.width != tinfo->goal.width
2062 || tinfo->curr.offset != tinfo->goal.offset
2063 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
2064 if (bootverbose == 0)
2065 break;
2067 /*
2068 * Don't bother reporting results that
2069 * are identical to those last reported.
2070 */
2071 target_offset = target;
2072 if (channel == 'B')
2073 target_offset += 8;
2074 targ = ahc->platform_data->targets[target_offset];
2075 if (targ == NULL)
2076 break;
2077 if (tinfo->curr.period == targ->last_tinfo.period
2078 && tinfo->curr.width == targ->last_tinfo.width
2079 && tinfo->curr.offset == targ->last_tinfo.offset
2080 && tinfo->curr.ppr_options == targ->last_tinfo.ppr_options)
2081 if (bootverbose == 0)
2082 break;
2084 targ->last_tinfo.period = tinfo->curr.period;
2085 targ->last_tinfo.width = tinfo->curr.width;
2086 targ->last_tinfo.offset = tinfo->curr.offset;
2087 targ->last_tinfo.ppr_options = tinfo->curr.ppr_options;
2089 printf("(%s:%c:", ahc_name(ahc), channel);
2090 if (target == CAM_TARGET_WILDCARD)
2091 printf("*): ");
2092 else
2093 printf("%d): ", target);
2094 ahc_format_transinfo(&info, &tinfo->curr);
2095 if (info.pos < info.length)
2096 *info.buffer = '\0';
2097 else
2098 buf[info.length - 1] = '\0';
2099 printf("%s", buf);
2100 break;
2102 case AC_SENT_BDR:
2103 break;
2104 case AC_BUS_RESET:
2105 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
2106 if (ahc->platform_data->host != NULL) {
2107 scsi_report_bus_reset(ahc->platform_data->host,
2108 channel - 'A');
2110 #endif
2111 break;
2112 default:
2113 panic("ahc_send_async: Unexpected async event");
2117 /*
2118 * Calls the higher level scsi done function and frees the scb.
2119 */
2120 void
2121 ahc_done(struct ahc_softc *ahc, struct scb * scb)
2123 Scsi_Cmnd *cmd;
2124 struct ahc_linux_device *dev;
2126 LIST_REMOVE(scb, pending_links);
2127 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
2128 struct scb_tailq *untagged_q;
2129 int target_offset;
2131 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
2132 untagged_q = &(ahc->untagged_queues[target_offset]);
2133 TAILQ_REMOVE(untagged_q, scb, links.tqe);
2134 ahc_run_untagged_queue(ahc, untagged_q);
2137 if ((scb->flags & SCB_ACTIVE) == 0) {
2138 printf("SCB %d done'd twice\n", scb->hscb->tag);
2139 ahc_dump_card_state(ahc);
2140 panic("Stopping for safety");
2142 cmd = scb->io_ctx;
2143 dev = scb->platform_data->dev;
2144 dev->active--;
2145 dev->openings++;
2146 ahc_linux_unmap_scb(ahc, scb);
2147 if (scb->flags & SCB_SENSE) {
2148 memcpy(cmd->sense_buffer, ahc_get_sense_buf(ahc, scb),
2149 MIN(sizeof(struct scsi_sense_data),
2150 sizeof(cmd->sense_buffer)));
2151 cmd->result |= (DRIVER_SENSE << 24);
2152 } else {
2153 /*
2154 * Guard against stale sense data.
2155 * The Linux mid-layer assumes that sense
2156 * was retrieved anytime the first byte of
2157 * the sense buffer looks "sane".
2158 */
2159 cmd->sense_buffer[0] = 0;
2161 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
2162 uint32_t amount_xferred;
2164 amount_xferred =
2165 ahc_get_transfer_length(scb) - ahc_get_residual(scb);
2166 if (amount_xferred < scb->io_ctx->underflow) {
2167 printf("Saw underflow (%ld of %ld bytes). "
2168 "Treated as error\n",
2169 ahc_get_residual(scb),
2170 ahc_get_transfer_length(scb));
2171 ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
2172 } else {
2173 ahc_set_transaction_status(scb, CAM_REQ_CMP);
2174 ahc_linux_sniff_command(ahc, cmd, scb);
2176 } else if (ahc_get_transaction_status(scb) == DID_OK) {
2177 ahc_linux_handle_scsi_status(ahc, dev, scb);
2178 } else if (ahc_get_transaction_status(scb) == DID_NO_CONNECT) {
2179 /*
2180 * Should a selection timeout kill the device?
2181 * That depends on whether the selection timeout
2182 * is persistent. Since we have no guarantee that
2183 * the mid-layer will issue an inquiry for this device
2184 * again, we can't just kill it off.
2185 dev->flags |= AHC_DEV_UNCONFIGURED;
2186 */
2189 if (dev->openings == 1
2190 && ahc_get_transaction_status(scb) == CAM_REQ_CMP
2191 && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
2192 dev->tag_success_count++;
2193 /*
2194 * Some devices deal with temporary internal resource
2195 * shortages by returning queue full. When the queue
2196 * full occurrs, we throttle back. Slowly try to get
2197 * back to our previous queue depth.
2198 */
2199 if ((dev->openings + dev->active) < dev->maxtags
2200 && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
2201 dev->tag_success_count = 0;
2202 dev->openings++;
2205 if (dev->active == 0)
2206 dev->commands_since_idle_or_otag = 0;
2208 if (TAILQ_EMPTY(&dev->busyq)) {
2209 if ((dev->flags & AHC_DEV_UNCONFIGURED) != 0
2210 && dev->active == 0)
2211 ahc_linux_free_device(ahc, dev);
2212 } else if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) {
2213 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links);
2214 dev->flags |= AHC_DEV_ON_RUN_LIST;
2217 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
2218 printf("Recovery SCB completes\n");
2219 #if 0
2220 up(&ahc->platform_data->eh_sem);
2221 #endif
2224 ahc_free_scb(ahc, scb);
2225 ahc_linux_queue_cmd_complete(ahc, cmd);
2228 static void
2229 ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
2230 struct ahc_linux_device *dev, struct scb *scb)
2232 /*
2233 * We don't currently trust the mid-layer to
2234 * properly deal with queue full or busy. So,
2235 * when one occurs, we tell the mid-layer to
2236 * unconditionally requeue the command to us
2237 * so that we can retry it ourselves. We also
2238 * implement our own throttling mechanism so
2239 * we don't clobber the device with too many
2240 * commands.
2241 */
2242 switch (ahc_get_scsi_status(scb)) {
2243 default:
2244 break;
2245 case SCSI_STATUS_QUEUE_FULL:
2247 /*
2248 * By the time the core driver has returned this
2249 * command, all other commands that were queued
2250 * to us but not the device have been returned.
2251 * This ensures that dev->active is equal to
2252 * the number of commands actually queued to
2253 * the device.
2254 */
2255 dev->tag_success_count = 0;
2256 if (dev->active != 0) {
2257 /*
2258 * Drop our opening count to the number
2259 * of commands currently outstanding.
2260 */
2261 dev->openings = 0;
2262 /*
2263 ahc_print_path(ahc, scb);
2264 printf("Dropping tag count to %d\n", dev->active);
2265 */
2266 if (dev->active == dev->tags_on_last_queuefull) {
2268 dev->last_queuefull_same_count++;
2269 /*
2270 * If we repeatedly see a queue full
2271 * at the same queue depth, this
2272 * device has a fixed number of tag
2273 * slots. Lock in this tag depth
2274 * so we stop seeing queue fulls from
2275 * this device.
2276 */
2277 if (dev->last_queuefull_same_count
2278 == AHC_LOCK_TAGS_COUNT) {
2279 dev->maxtags = dev->active;
2280 ahc_print_path(ahc, scb);
2281 printf("Locking max tag count at %d\n",
2282 dev->active);
2284 } else {
2285 dev->tags_on_last_queuefull = dev->active;
2286 dev->last_queuefull_same_count = 0;
2288 ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
2289 ahc_set_scsi_status(scb, SCSI_STATUS_OK);
2290 break;
2292 /*
2293 * Drop down to a single opening, and treat this
2294 * as if the target return BUSY SCSI status.
2295 */
2296 dev->openings = 1;
2297 /* FALLTHROUGH */
2299 case SCSI_STATUS_BUSY:
2301 /*
2302 * Set a short timer to defer sending commands for
2303 * a bit since Linux will not delay in this case.
2304 */
2305 if ((dev->flags & AHC_DEV_TIMER_ACTIVE) != 0) {
2306 printf("%s:%c:%d: Device Timer still active during "
2307 "busy processing\n", ahc_name(ahc),
2308 dev->target->channel, dev->target->target);
2309 break;
2311 dev->flags |= AHC_DEV_TIMER_ACTIVE;
2312 dev->qfrozen++;
2313 init_timer(&dev->timer);
2314 dev->timer.data = (u_long)dev;
2315 dev->timer.expires = jiffies + (HZ/2);
2316 dev->timer.function = ahc_linux_dev_timed_unfreeze;
2317 add_timer(&dev->timer);
2318 break;
2323 static void
2324 ahc_linux_filter_command(struct ahc_softc *ahc, Scsi_Cmnd *cmd, struct scb *scb)
2326 switch (cmd->cmnd[0]) {
2327 case INQUIRY:
2329 struct ahc_devinfo devinfo;
2330 struct scsi_inquiry *inq;
2331 struct scsi_inquiry_data *sid;
2332 struct ahc_initiator_tinfo *tinfo;
2333 struct ahc_transinfo *user;
2334 struct ahc_transinfo *goal;
2335 struct ahc_transinfo *curr;
2336 struct ahc_tmode_tstate *tstate;
2337 struct ahc_syncrate *syncrate;
2338 struct ahc_linux_device *dev;
2339 u_int scsiid;
2340 u_int maxsync;
2341 int transferred_len;
2342 int minlen;
2343 u_int width;
2344 u_int period;
2345 u_int offset;
2346 u_int ppr_options;
2348 /*
2349 * Validate the command. We only want to filter
2350 * standard inquiry commands, not those querying
2351 * Vital Product Data.
2352 */
2353 inq = (struct scsi_inquiry *)cmd->cmnd;
2354 if ((inq->byte2 & SI_EVPD) != 0
2355 || inq->page_code != 0)
2356 break;
2358 if (cmd->use_sg != 0) {
2359 printf("%s: SG Inquiry response ignored\n",
2360 ahc_name(ahc));
2361 break;
2363 transferred_len = ahc_get_transfer_length(scb)
2364 - ahc_get_residual(scb);
2365 sid = (struct scsi_inquiry_data *)cmd->request_buffer;
2367 /*
2368 * Determine if this lun actually exists. If so,
2369 * hold on to its corresponding device structure.
2370 * If not, make sure we release the device and
2371 * don't bother processing the rest of this inquiry
2372 * command.
2373 */
2374 dev = ahc_linux_get_device(ahc, cmd->channel,
2375 cmd->target, cmd->lun,
2376 /*alloc*/FALSE);
2377 if (transferred_len >= 1
2378 && SID_QUAL(sid) == SID_QUAL_LU_CONNECTED) {
2380 dev->flags &= ~AHC_DEV_UNCONFIGURED;
2381 } else {
2382 dev->flags |= AHC_DEV_UNCONFIGURED;
2383 break;
2386 /*
2387 * Update our notion of this device's transfer
2388 * negotiation capabilities.
2389 */
2390 scsiid = BUILD_SCSIID(ahc, cmd);
2391 ahc_compile_devinfo(&devinfo, SCSIID_OUR_ID(scsiid),
2392 cmd->target, cmd->lun,
2393 SCSIID_CHANNEL(ahc, scsiid),
2394 ROLE_INITIATOR);
2395 tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
2396 devinfo.our_scsiid,
2397 devinfo.target, &tstate);
2398 user = &tinfo->user;
2399 goal = &tinfo->goal;
2400 curr = &tinfo->curr;
2401 width = user->width;
2402 period = user->period;
2403 offset = user->offset;
2404 ppr_options = user->ppr_options;
2405 minlen = offsetof(struct scsi_inquiry_data, version) + 1;
2406 if (transferred_len >= minlen) {
2407 curr->protocol_version = SID_ANSI_REV(sid);
2409 /*
2410 * Only attempt SPI3 once we've verified that
2411 * the device claims to support SPI3 features.
2412 */
2413 if (curr->protocol_version < SCSI_REV_2)
2414 curr->transport_version = SID_ANSI_REV(sid);
2415 else
2416 curr->transport_version = SCSI_REV_2;
2419 minlen = offsetof(struct scsi_inquiry_data, flags) + 1;
2420 if (transferred_len >= minlen
2421 && (sid->additional_length + 4) >= minlen) {
2422 if ((sid->flags & SID_WBus16) == 0)
2423 width = MSG_EXT_WDTR_BUS_8_BIT;
2424 if ((sid->flags & SID_Sync) == 0) {
2425 period = 0;
2426 offset = 0;
2427 ppr_options = 0;
2429 } else {
2430 /* Keep current settings */
2431 break;
2433 minlen = offsetof(struct scsi_inquiry_data, spi3data) + 1;
2434 /*
2435 * This is a kludge to deal with inquiry requests that
2436 * are not large enough for us to pull the spi3/4 bits.
2437 * In this case, we assume that a device that tells us
2438 * they can provide inquiry data that spans the SPI3
2439 * bits and says its SCSI3 can handle a PPR request.
2440 * If the inquiry request has sufficient buffer space to
2441 * cover SPI3 bits, we honor them regardless of reported
2442 * SCSI REV. We also allow any device that has had its
2443 * goal ppr_options set to allow DT speeds to keep that
2444 * option if a short inquiry occurs that would fail the
2445 * normal tests outlined above.
2446 */
2447 if ((sid->additional_length + 4) >= minlen) {
2448 if (transferred_len >= minlen) {
2449 if ((sid->spi3data & SID_SPI_CLOCK_DT) == 0)
2450 ppr_options = 0;
2451 } else if ((goal->ppr_options & MSG_EXT_PPR_DT_REQ)== 0)
2452 ppr_options = 0;
2454 if (curr->protocol_version > SCSI_REV_2)
2455 curr->transport_version = 3;
2456 } else {
2457 ppr_options = 0;
2459 ahc_validate_width(ahc, /*tinfo limit*/NULL, &width,
2460 ROLE_UNKNOWN);
2461 if ((ahc->features & AHC_ULTRA2) != 0)
2462 maxsync = AHC_SYNCRATE_DT;
2463 else if ((ahc->features & AHC_ULTRA) != 0)
2464 maxsync = AHC_SYNCRATE_ULTRA;
2465 else
2466 maxsync = AHC_SYNCRATE_FAST;
2468 syncrate = ahc_find_syncrate(ahc, &period,
2469 &ppr_options, maxsync);
2470 ahc_validate_offset(ahc, /*tinfo limit*/NULL, syncrate,
2471 &offset, width, ROLE_UNKNOWN);
2472 if (offset == 0 || period == 0) {
2473 period = 0;
2474 offset = 0;
2475 ppr_options = 0;
2477 /* Apply our filtered user settings. */
2478 ahc_set_width(ahc, &devinfo, width,
2479 AHC_TRANS_GOAL, /*paused*/FALSE);
2480 ahc_set_syncrate(ahc, &devinfo, syncrate, period,
2481 offset, ppr_options, AHC_TRANS_GOAL,
2482 /*paused*/FALSE);
2483 break;
2485 default:
2486 panic("ahc_linux_filter_command: Unexpected Command type %x\n",
2487 cmd->cmnd[0]);
2488 break;
2492 #if 0
2493 static void
2494 ahc_linux_sem_timeout(u_long arg)
2496 struct semaphore *sem;
2498 sem = (struct semaphore *)arg;
2499 up(sem);
2501 #endif
2503 static void
2504 ahc_linux_freeze_sim_queue(struct ahc_softc *ahc)
2506 ahc->platform_data->qfrozen++;
2507 if (ahc->platform_data->qfrozen == 1)
2508 scsi_block_requests(ahc->platform_data->host);
2511 static void
2512 ahc_linux_release_sim_queue(u_long arg)
2514 struct ahc_softc *ahc;
2515 u_long s;
2516 int unblock_reqs;
2518 ahc = (struct ahc_softc *)arg;
2519 unblock_reqs = 0;
2520 ahc_lock(ahc, &s);
2521 if (ahc->platform_data->qfrozen > 0)
2522 ahc->platform_data->qfrozen--;
2523 if (ahc->platform_data->qfrozen == 0) {
2524 unblock_reqs = 1;
2526 ahc_unlock(ahc, &s);
2527 /*
2528 * There is still a race here. The mid-layer
2529 * should keep its own freeze count and use
2530 * a bottom half handler to run the queues
2531 * so we can unblock with our own lock held.
2532 */
2533 if (unblock_reqs) {
2534 scsi_unblock_requests(ahc->platform_data->host);
2535 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
2536 tasklet_schedule(&ahc->platform_data->runq_tasklet);
2537 #else
2538 ahc_runq_tasklet((unsigned long)ahc);
2539 #endif
2543 static void
2544 ahc_linux_dev_timed_unfreeze(u_long arg)
2546 struct ahc_linux_device *dev;
2547 struct ahc_softc *ahc;
2548 u_long s;
2550 dev = (struct ahc_linux_device *)arg;
2551 ahc = dev->target->ahc;
2552 ahc_lock(ahc, &s);
2553 dev->flags &= ~AHC_DEV_TIMER_ACTIVE;
2554 if (dev->qfrozen > 0)
2555 dev->qfrozen--;
2556 if (dev->qfrozen == 0
2557 && (dev->flags & AHC_DEV_ON_RUN_LIST) == 0)
2558 ahc_linux_run_device_queue(ahc, dev);
2559 ahc_unlock(ahc, &s);
2562 static int
2563 ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag)
2565 struct ahc_softc *ahc;
2566 struct ahc_cmd *acmd;
2567 struct ahc_cmd *list_acmd;
2568 struct ahc_linux_device *dev;
2569 struct scb *pending_scb;
2570 u_long s;
2571 u_int saved_scbptr;
2572 u_int active_scb_index;
2573 u_int last_phase;
2574 int retval;
2575 int paused;
2576 int wait;
2577 int disconnected;
2579 paused = FALSE;
2580 wait = FALSE;
2581 ahc = *(struct ahc_softc **)cmd->host->hostdata;
2582 acmd = (struct ahc_cmd *)cmd;
2584 printf("%s:%d:%d:%d: Attempting to queue a%s message\n",
2585 ahc_name(ahc), cmd->channel, cmd->target, cmd->lun,
2586 flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
2588 /*
2589 * It is a bug that the upper layer takes
2590 * this lock just prior to calling us.
2591 */
2592 spin_unlock_irq(&io_request_lock);
2594 ahc_lock(ahc, &s);
2596 /*
2597 * First determine if we currently own this command.
2598 * Start by searching the device queue. If not found
2599 * there, check the pending_scb list. If not found
2600 * at all, and the system wanted us to just abort the
2601 * command return success.
2602 */
2603 dev = ahc_linux_get_device(ahc, cmd->channel, cmd->target,
2604 cmd->lun, /*alloc*/FALSE);
2606 if (dev == NULL) {
2607 /*
2608 * No target device for this command exists,
2609 * so we must not still own the command.
2610 */
2611 printf("%s:%d:%d:%d: Is not an active device\n",
2612 ahc_name(ahc), cmd->channel, cmd->target, cmd->lun);
2613 retval = SUCCESS;
2614 goto no_cmd;
2617 TAILQ_FOREACH(list_acmd, &dev->busyq, acmd_links.tqe) {
2618 if (list_acmd == acmd)
2619 break;
2622 if (list_acmd != NULL) {
2623 printf("%s:%d:%d:%d: Command found on device queue\n",
2624 ahc_name(ahc), cmd->channel, cmd->target, cmd->lun);
2625 if (flag == SCB_ABORT) {
2626 TAILQ_REMOVE(&dev->busyq, list_acmd, acmd_links.tqe);
2627 cmd->result = DID_ABORT << 16;
2628 ahc_linux_queue_cmd_complete(ahc, cmd);
2629 retval = SUCCESS;
2630 goto done;
2634 if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
2635 && ahc_search_untagged_queues(ahc, cmd, cmd->target,
2636 cmd->channel + 'A', cmd->lun,
2637 CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
2638 printf("%s:%d:%d:%d: Command found on untagged queue\n",
2639 ahc_name(ahc), cmd->channel, cmd->target, cmd->lun);
2640 retval = SUCCESS;
2641 goto done;
2644 /*
2645 * See if we can find a matching cmd in the pending list.
2646 */
2647 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2648 if (pending_scb->io_ctx == cmd)
2649 break;
2652 if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
2654 /* Any SCB for this device will do for a target reset */
2655 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2656 if (ahc_match_scb(ahc, pending_scb, cmd->target,
2657 cmd->channel + 'A', CAM_LUN_WILDCARD,
2658 SCB_LIST_NULL, ROLE_INITIATOR) == 0)
2659 break;
2663 if (pending_scb == NULL) {
2664 printf("%s:%d:%d:%d: Command not found\n",
2665 ahc_name(ahc), cmd->channel, cmd->target, cmd->lun);
2666 goto no_cmd;
2669 if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
2670 /*
2671 * We can't queue two recovery actions using the same SCB
2672 */
2673 retval = FAILED;
2674 goto done;
2677 /*
2678 * Ensure that the card doesn't do anything
2679 * behind our back and that no selections have occurred
2680 * that have not been serviced. Also make sure that we
2681 * didn't "just" miss an interrupt that would
2682 * affect this cmd.
2683 */
2684 ahc->flags |= AHC_ALL_INTERRUPTS;
2685 do {
2686 if (paused)
2687 ahc_unpause(ahc);
2688 ahc_intr(ahc);
2689 ahc_pause(ahc);
2690 paused = TRUE;
2691 ahc_outb(ahc, SCSISEQ, ahc_inb(ahc, SCSISEQ) & ~ENSELO);
2692 ahc_clear_critical_section(ahc);
2693 } while ((ahc_inb(ahc, INTSTAT) & INT_PEND) != 0
2694 || (ahc_inb(ahc, SSTAT0) & (SELDO|SELINGO)));
2695 ahc->flags &= ~AHC_ALL_INTERRUPTS;
2697 ahc_dump_card_state(ahc);
2699 if ((pending_scb->flags & SCB_ACTIVE) == 0) {
2700 printf("%s:%d:%d:%d: Command already completed\n",
2701 ahc_name(ahc), cmd->channel, cmd->target, cmd->lun);
2702 goto no_cmd;
2705 disconnected = TRUE;
2706 if (flag == SCB_ABORT) {
2707 if (ahc_search_qinfifo(ahc, cmd->target, cmd->channel + 'A',
2708 cmd->lun, pending_scb->hscb->tag,
2709 ROLE_INITIATOR, CAM_REQ_ABORTED,
2710 SEARCH_COMPLETE) > 0) {
2711 printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
2712 ahc_name(ahc), cmd->channel, cmd->target,
2713 cmd->lun);
2714 retval = SUCCESS;
2715 goto done;
2717 } else if (ahc_search_qinfifo(ahc, cmd->target, cmd->channel + 'A',
2718 cmd->lun, pending_scb->hscb->tag,
2719 ROLE_INITIATOR, /*status*/0,
2720 SEARCH_COUNT) > 0) {
2721 disconnected = FALSE;
2724 if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & IDENTIFY_SEEN) != 0) {
2725 struct scb *bus_scb;
2727 bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
2728 if (bus_scb == pending_scb)
2729 disconnected = FALSE;
2730 else if (flag != SCB_ABORT
2731 && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
2732 && ahc_inb(ahc, SAVED_LUN) == pending_scb->hscb->lun)
2733 disconnected = FALSE;
2736 /*
2737 * At this point, pending_scb is the scb associated with the
2738 * passed in command. That command is currently active on the
2739 * bus, is in the disconnected state, or we're hoping to find
2740 * a command for the same target active on the bus to abuse to
2741 * send a BDR. Queue the appropriate message based on which of
2742 * these states we are in.
2743 */
2744 last_phase = ahc_inb(ahc, LASTPHASE);
2745 saved_scbptr = ahc_inb(ahc, SCBPTR);
2746 active_scb_index = ahc_inb(ahc, SCB_TAG);
2747 if (last_phase != P_BUSFREE
2748 && (pending_scb->hscb->tag == active_scb_index
2749 || (flag == SCB_DEVICE_RESET
2750 && SCSIID_TARGET(ahc, ahc_inb(ahc, SAVED_SCSIID)) == cmd->target))) {
2752 /*
2753 * We're active on the bus, so assert ATN
2754 * and hope that the target responds.
2755 */
2756 pending_scb = ahc_lookup_scb(ahc, active_scb_index);
2757 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2758 ahc_outb(ahc, MSG_OUT, HOST_MSG);
2759 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
2760 printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
2761 ahc_name(ahc), cmd->channel, cmd->target, cmd->lun);
2762 wait = TRUE;
2763 } else if (disconnected) {
2765 /*
2766 * Actually re-queue this SCB in an attempt
2767 * to select the device before it reconnects.
2768 * In either case (selection or reselection),
2769 * we will now issue the approprate message
2770 * to the timed-out device.
2772 * Set the MK_MESSAGE control bit indicating
2773 * that we desire to send a message. We
2774 * also set the disconnected flag since
2775 * in the paging case there is no guarantee
2776 * that our SCB control byte matches the
2777 * version on the card. We don't want the
2778 * sequencer to abort the command thinking
2779 * an unsolicited reselection occurred.
2780 */
2781 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
2782 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2784 /*
2785 * Remove any cached copy of this SCB in the
2786 * disconnected list in preparation for the
2787 * queuing of our abort SCB. We use the
2788 * same element in the SCB, SCB_NEXT, for
2789 * both the qinfifo and the disconnected list.
2790 */
2791 ahc_search_disc_list(ahc, cmd->target, cmd->channel + 'A',
2792 cmd->lun, pending_scb->hscb->tag,
2793 /*stop_on_first*/TRUE,
2794 /*remove*/TRUE,
2795 /*save_state*/FALSE);
2797 /*
2798 * In the non-paging case, the sequencer will
2799 * never re-reference the in-core SCB.
2800 * To make sure we are notified during
2801 * reslection, set the MK_MESSAGE flag in
2802 * the card's copy of the SCB.
2803 */
2804 if ((ahc->flags & AHC_PAGESCBS) == 0) {
2805 ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
2806 ahc_outb(ahc, SCB_CONTROL,
2807 ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
2810 /*
2811 * Clear out any entries in the QINFIFO first
2812 * so we are the next SCB for this target
2813 * to run.
2814 */
2815 ahc_search_qinfifo(ahc, cmd->target, cmd->channel + 'A',
2816 cmd->lun, SCB_LIST_NULL, ROLE_INITIATOR,
2817 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
2818 ahc_print_path(ahc, pending_scb);
2819 printf("Queuing a recovery SCB\n");
2820 ahc_qinfifo_requeue_tail(ahc, pending_scb);
2821 ahc_outb(ahc, SCBPTR, saved_scbptr);
2822 printf("%s:%d:%d:%d: Device is disconnected, re-queuing SCB\n",
2823 ahc_name(ahc), cmd->channel, cmd->target, cmd->lun);
2824 wait = TRUE;
2825 } else {
2826 printf("%s:%d:%d:%d: Unable to deliver message\n",
2827 ahc_name(ahc), cmd->channel, cmd->target, cmd->lun);
2828 retval = FAILED;
2829 goto done;
2832 no_cmd:
2833 /*
2834 * Our assumption is that if we don't have the command, no
2835 * recovery action was required, so we return success. Again,
2836 * the semantics of the mid-layer recovery engine are not
2837 * well defined, so this may change in time.
2838 */
2839 retval = SUCCESS;
2840 done:
2841 if (paused)
2842 ahc_unpause(ahc);
2843 if (wait) {
2844 // JWS - XEN - err...
2845 printf("JWS - aic7xxx: recovery-wait: doh\n");
2846 retval=FAILED;
2847 /*
2848 struct timer_list timer;
2849 int ret;
2851 ahc_unlock(ahc, &s);
2852 init_timer(&timer);
2853 timer.data = (u_long)&ahc->platform_data->eh_sem;
2854 timer.expires = jiffies + (5 * HZ);
2855 timer.function = ahc_linux_sem_timeout;
2856 add_timer(&timer);
2857 printf("Recovery code sleeping\n");
2858 down(&ahc->platform_data->eh_sem);
2859 printf("Recovery code awake\n");
2860 ret = del_timer(&timer);
2861 if (ret == 0) {
2862 printf("Timer Expired\n");
2863 retval = FAILED;
2865 ahc_lock(ahc, &s);
2866 */
2868 acmd = TAILQ_FIRST(&ahc->platform_data->completeq);
2869 TAILQ_INIT(&ahc->platform_data->completeq);
2870 ahc_unlock(ahc, &s);
2871 if (acmd != NULL)
2872 ahc_linux_run_complete_queue(ahc, acmd);
2873 ahc_runq_tasklet((unsigned long)ahc);
2874 spin_lock_irq(&io_request_lock);
2875 return (retval);
2878 /*
2879 * Abort the current SCSI command(s).
2880 */
2881 int
2882 ahc_linux_abort(Scsi_Cmnd *cmd)
2884 int error;
2886 error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
2887 if (error != 0)
2888 printf("aic7xxx_abort returns 0x%x\n", error);
2889 return (error);
2892 /*
2893 * Attempt to send a target reset message to the device that timed out.
2894 */
2895 int
2896 ahc_linux_dev_reset(Scsi_Cmnd *cmd)
2898 int error;
2900 error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
2901 if (error != 0)
2902 printf("aic7xxx_dev_reset returns 0x%x\n", error);
2903 return (error);
2906 /*
2907 * Reset the SCSI bus.
2908 */
2909 int
2910 ahc_linux_bus_reset(Scsi_Cmnd *cmd)
2912 struct ahc_softc *ahc;
2913 struct ahc_cmd *acmd;
2914 u_long s;
2915 int found;
2917 /*
2918 * It is a bug that the upper layer takes
2919 * this lock just prior to calling us.
2920 */
2921 spin_unlock_irq(&io_request_lock);
2923 ahc = *(struct ahc_softc **)cmd->host->hostdata;
2924 ahc_lock(ahc, &s);
2925 found = ahc_reset_channel(ahc, cmd->channel + 'A',
2926 /*initiate reset*/TRUE);
2927 acmd = TAILQ_FIRST(&ahc->platform_data->completeq);
2928 TAILQ_INIT(&ahc->platform_data->completeq);
2929 ahc_unlock(ahc, &s);
2930 if (bootverbose)
2931 printf("%s: SCSI bus reset delivered. "
2932 "%d SCBs aborted.\n", ahc_name(ahc), found);
2934 if (acmd != NULL)
2935 ahc_linux_run_complete_queue(ahc, acmd);
2937 spin_lock_irq(&io_request_lock);
2938 return SUCCESS;
2941 /*
2942 * Return the disk geometry for the given SCSI device.
2943 */
2944 int
2945 ahc_linux_biosparam(Disk *disk, kdev_t dev, int geom[])
2947 int heads;
2948 int sectors;
2949 int cylinders;
2950 //int ret;
2951 int extended;
2952 struct ahc_softc *ahc;
2953 //struct buffer_head *bh;
2955 ahc = *((struct ahc_softc **)disk->device->host->hostdata);
2956 #if 0
2957 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,17)
2958 bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, block_size(dev));
2959 #else
2960 bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, 1024);
2961 #endif
2963 if (bh) {
2964 ret = scsi_partsize(bh, disk->capacity,
2965 &geom[2], &geom[0], &geom[1]);
2966 brelse(bh);
2967 if (ret != -1)
2968 return (ret);
2970 #endif
2971 heads = 64;
2972 sectors = 32;
2973 cylinders = disk->capacity / (heads * sectors);
2975 if (aic7xxx_extended != 0)
2976 extended = 1;
2977 else if (disk->device->channel == 0)
2978 extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
2979 else
2980 extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
2981 if (extended && cylinders >= 1024) {
2982 heads = 255;
2983 sectors = 63;
2984 cylinders = disk->capacity / (heads * sectors);
2986 geom[0] = heads;
2987 geom[1] = sectors;
2988 geom[2] = cylinders;
2989 return (0);
2992 /*
2993 * Free the passed in Scsi_Host memory structures prior to unloading the
2994 * module.
2995 */
2996 int
2997 ahc_linux_release(struct Scsi_Host * host)
2999 struct ahc_softc *ahc;
3000 u_long l;
3002 ahc_list_lock(&l);
3003 if (host != NULL) {
3005 /*
3006 * We should be able to just perform
3007 * the free directly, but check our
3008 * list for extra sanity.
3009 */
3010 ahc = ahc_find_softc(*(struct ahc_softc **)host->hostdata);
3011 if (ahc != NULL) {
3012 u_long s;
3014 ahc_lock(ahc, &s);
3015 ahc_intr_enable(ahc, FALSE);
3016 ahc_unlock(ahc, &s);
3017 ahc_free(ahc);
3020 ahc_list_unlock(&l);
3021 return (0);
3024 void
3025 ahc_platform_dump_card_state(struct ahc_softc *ahc)
3027 struct ahc_linux_device *dev;
3028 int channel;
3029 int maxchannel;
3030 int target;
3031 int maxtarget;
3032 int lun;
3033 int i;
3035 maxchannel = (ahc->features & AHC_TWIN) ? 1 : 0;
3036 maxtarget = (ahc->features & AHC_WIDE) ? 15 : 7;
3037 for (channel = 0; channel <= maxchannel; channel++) {
3039 for (target = 0; target <=maxtarget; target++) {
3041 for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
3042 struct ahc_cmd *acmd;
3044 dev = ahc_linux_get_device(ahc, channel, target,
3045 lun, /*alloc*/FALSE);
3046 if (dev == NULL)
3047 continue;
3049 printf("DevQ(%d:%d:%d): ",
3050 channel, target, lun);
3051 i = 0;
3052 TAILQ_FOREACH(acmd, &dev->busyq,
3053 acmd_links.tqe) {
3054 if (i++ > AHC_SCB_MAX)
3055 break;
3057 printf("%d waiting\n", i);
3064 #if defined(MODULE) || LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
3065 static Scsi_Host_Template driver_template = AIC7XXX;
3066 Scsi_Host_Template *aic7xxx_driver_template = &driver_template;
3067 #include "../scsi_module.c.inc"
3068 #endif