2 * Adaptec AIC79xx device driver for Linux.
4 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic79xx_osm.c#171 $
6 * --------------------------------------------------------------------------
7 * Copyright (c) 1994-2000 Justin T. Gibbs.
8 * Copyright (c) 1997-1999 Doug Ledford
9 * Copyright (c) 2000-2003 Adaptec Inc.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include "aic79xx_osm.h"
46 #include "aic79xx_inline.h"
47 #include <scsi/scsicam.h>
49 static struct scsi_transport_template *ahd_linux_transport_template = NULL;
52 * Include aiclib.c as part of our
53 * "module dependencies are hard" work around.
57 #include <linux/init.h> /* __setup */
58 #include <linux/mm.h> /* For fetching system memory size */
59 #include <linux/blkdev.h> /* For block_size() */
60 #include <linux/delay.h> /* For ssleep/msleep */
63 * Lock protecting manipulation of the ahd softc list.
65 spinlock_t ahd_list_spinlock;
68 * Bucket size for counting good commands in between bad ones.
70 #define AHD_LINUX_ERR_THRESH 1000
73 * Set this to the delay in seconds after SCSI bus reset.
74 * Note, we honor this only for the initial bus reset.
75 * The scsi error recovery code performs its own bus settle
76 * delay handling for error recovery actions.
78 #ifdef CONFIG_AIC79XX_RESET_DELAY_MS
79 #define AIC79XX_RESET_DELAY CONFIG_AIC79XX_RESET_DELAY_MS
81 #define AIC79XX_RESET_DELAY 5000
85 * To change the default number of tagged transactions allowed per-device,
86 * add a line to the lilo.conf file like:
87 * append="aic79xx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
88 * which will result in the first four devices on the first two
89 * controllers being set to a tagged queue depth of 32.
91 * The tag_commands is an array of 16 to allow for wide and twin adapters.
92 * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
96 uint16_t tag_commands[16]; /* Allow for wide/twin adapters. */
100 * Modify this as you see fit for your system.
102 * 0 tagged queuing disabled
103 * 1 <= n <= 253 n == max tags ever dispatched.
105 * The driver will throttle the number of commands dispatched to a
106 * device if it returns queue full. For devices with a fixed maximum
107 * queue depth, the driver will eventually determine this depth and
108 * lock it in (a console message is printed to indicate that a lock
109 * has occurred). On some devices, queue full is returned for a temporary
110 * resource shortage. These devices will return queue full at varying
111 * depths. The driver will throttle back when the queue fulls occur and
112 * attempt to slowly increase the depth over time as the device recovers
113 * from the resource shortage.
115 * In this example, the first line will disable tagged queueing for all
116 * the devices on the first probed aic79xx adapter.
118 * The second line enables tagged queueing with 4 commands/LUN for IDs
119 * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
120 * driver to attempt to use up to 64 tags for ID 1.
122 * The third line is the same as the first line.
124 * The fourth line disables tagged queueing for devices 0 and 3. It
125 * enables tagged queueing for the other IDs, with 16 commands/LUN
126 * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
127 * IDs 2, 5-7, and 9-15.
131 * NOTE: The below structure is for reference only, the actual structure
132 * to modify in order to change things is just below this comment block.
133 adapter_tag_info_t aic79xx_tag_info[] =
135 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
136 {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
137 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
138 {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
142 #ifdef CONFIG_AIC79XX_CMDS_PER_DEVICE
143 #define AIC79XX_CMDS_PER_DEVICE CONFIG_AIC79XX_CMDS_PER_DEVICE
145 #define AIC79XX_CMDS_PER_DEVICE AHD_MAX_QUEUE
148 #define AIC79XX_CONFIGED_TAG_COMMANDS { \
149 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
150 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
151 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
152 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
153 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
154 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
155 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE, \
156 AIC79XX_CMDS_PER_DEVICE, AIC79XX_CMDS_PER_DEVICE \
160 * By default, use the number of commands specified by
161 * the users kernel configuration.
163 static adapter_tag_info_t aic79xx_tag_info[] =
165 {AIC79XX_CONFIGED_TAG_COMMANDS},
166 {AIC79XX_CONFIGED_TAG_COMMANDS},
167 {AIC79XX_CONFIGED_TAG_COMMANDS},
168 {AIC79XX_CONFIGED_TAG_COMMANDS},
169 {AIC79XX_CONFIGED_TAG_COMMANDS},
170 {AIC79XX_CONFIGED_TAG_COMMANDS},
171 {AIC79XX_CONFIGED_TAG_COMMANDS},
172 {AIC79XX_CONFIGED_TAG_COMMANDS},
173 {AIC79XX_CONFIGED_TAG_COMMANDS},
174 {AIC79XX_CONFIGED_TAG_COMMANDS},
175 {AIC79XX_CONFIGED_TAG_COMMANDS},
176 {AIC79XX_CONFIGED_TAG_COMMANDS},
177 {AIC79XX_CONFIGED_TAG_COMMANDS},
178 {AIC79XX_CONFIGED_TAG_COMMANDS},
179 {AIC79XX_CONFIGED_TAG_COMMANDS},
180 {AIC79XX_CONFIGED_TAG_COMMANDS}
184 * The I/O cell on the chip is very configurable in respect to its analog
185 * characteristics. Set the defaults here; they can be overriden with
186 * the proper insmod parameters.
188 struct ahd_linux_iocell_opts
194 #define AIC79XX_DEFAULT_PRECOMP 0xFF
195 #define AIC79XX_DEFAULT_SLEWRATE 0xFF
196 #define AIC79XX_DEFAULT_AMPLITUDE 0xFF
197 #define AIC79XX_DEFAULT_IOOPTS \
199 AIC79XX_DEFAULT_PRECOMP, \
200 AIC79XX_DEFAULT_SLEWRATE, \
201 AIC79XX_DEFAULT_AMPLITUDE \
203 #define AIC79XX_PRECOMP_INDEX 0
204 #define AIC79XX_SLEWRATE_INDEX 1
205 #define AIC79XX_AMPLITUDE_INDEX 2
206 static struct ahd_linux_iocell_opts aic79xx_iocell_info[] =
208 AIC79XX_DEFAULT_IOOPTS,
209 AIC79XX_DEFAULT_IOOPTS,
210 AIC79XX_DEFAULT_IOOPTS,
211 AIC79XX_DEFAULT_IOOPTS,
212 AIC79XX_DEFAULT_IOOPTS,
213 AIC79XX_DEFAULT_IOOPTS,
214 AIC79XX_DEFAULT_IOOPTS,
215 AIC79XX_DEFAULT_IOOPTS,
216 AIC79XX_DEFAULT_IOOPTS,
217 AIC79XX_DEFAULT_IOOPTS,
218 AIC79XX_DEFAULT_IOOPTS,
219 AIC79XX_DEFAULT_IOOPTS,
220 AIC79XX_DEFAULT_IOOPTS,
221 AIC79XX_DEFAULT_IOOPTS,
222 AIC79XX_DEFAULT_IOOPTS,
223 AIC79XX_DEFAULT_IOOPTS
227 * There should be a specific return value for this in scsi.h, but
228 * it seems that most drivers ignore it.
230 #define DID_UNDERFLOW DID_ERROR
233 ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
235 printk("(scsi%d:%c:%d:%d): ",
236 ahd->platform_data->host->host_no,
237 scb != NULL ? SCB_GET_CHANNEL(ahd, scb) : 'X',
238 scb != NULL ? SCB_GET_TARGET(ahd, scb) : -1,
239 scb != NULL ? SCB_GET_LUN(scb) : -1);
243 * XXX - these options apply unilaterally to _all_ adapters
244 * cards in the system. This should be fixed. Exceptions to this
245 * rule are noted in the comments.
249 * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This
250 * has no effect on any later resets that might occur due to things like
253 static uint32_t aic79xx_no_reset;
256 * Certain PCI motherboards will scan PCI devices from highest to lowest,
257 * others scan from lowest to highest, and they tend to do all kinds of
258 * strange things when they come into contact with PCI bridge chips. The
259 * net result of all this is that the PCI card that is actually used to boot
260 * the machine is very hard to detect. Most motherboards go from lowest
261 * PCI slot number to highest, and the first SCSI controller found is the
262 * one you boot from. The only exceptions to this are when a controller
263 * has its BIOS disabled. So, we by default sort all of our SCSI controllers
264 * from lowest PCI slot number to highest PCI slot number. We also force
265 * all controllers with their BIOS disabled to the end of the list. This
266 * works on *almost* all computers. Where it doesn't work, we have this
267 * option. Setting this option to non-0 will reverse the order of the sort
268 * to highest first, then lowest, but will still leave cards with their BIOS
269 * disabled at the very end. That should fix everyone up unless there are
270 * really strange cirumstances.
272 static uint32_t aic79xx_reverse_scan;
275 * Should we force EXTENDED translation on a controller.
276 * 0 == Use whatever is in the SEEPROM or default to off
277 * 1 == Use whatever is in the SEEPROM or default to on
279 static uint32_t aic79xx_extended;
282 * PCI bus parity checking of the Adaptec controllers. This is somewhat
283 * dubious at best. To my knowledge, this option has never actually
284 * solved a PCI parity problem, but on certain machines with broken PCI
285 * chipset configurations, it can generate tons of false error messages.
286 * It's included in the driver for completeness.
287 * 0 = Shut off PCI parity check
288 * non-0 = Enable PCI parity check
290 * NOTE: you can't actually pass -1 on the lilo prompt. So, to set this
291 * variable to -1 you would actually want to simply pass the variable
292 * name without a number. That will invert the 0 which will result in
295 static uint32_t aic79xx_pci_parity = ~0;
298 * There are lots of broken chipsets in the world. Some of them will
299 * violate the PCI spec when we issue byte sized memory writes to our
300 * controller. I/O mapped register access, if allowed by the given
301 * platform, will work in almost all cases.
303 uint32_t aic79xx_allow_memio = ~0;
306 * aic79xx_detect() has been run, so register all device arrivals
307 * immediately with the system rather than deferring to the sorted
308 * attachment performed by aic79xx_detect().
310 int aic79xx_detect_complete;
313 * So that we can set how long each device is given as a selection timeout.
314 * The table of values goes like this:
319 * We default to 256ms because some older devices need a longer time
320 * to respond to initial selection.
322 static uint32_t aic79xx_seltime;
325 * Certain devices do not perform any aging on commands. Should the
326 * device be saturated by commands in one portion of the disk, it is
327 * possible for transactions on far away sectors to never be serviced.
328 * To handle these devices, we can periodically send an ordered tag to
329 * force all outstanding transactions to be serviced prior to a new
332 uint32_t aic79xx_periodic_otag;
335 * Module information and settable options.
337 static char *aic79xx = NULL;
339 MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
340 MODULE_DESCRIPTION("Adaptec Aic790X U320 SCSI Host Bus Adapter driver");
341 MODULE_LICENSE("Dual BSD/GPL");
342 MODULE_VERSION(AIC79XX_DRIVER_VERSION);
343 module_param(aic79xx, charp, 0444);
344 MODULE_PARM_DESC(aic79xx,
345 "period delimited, options string.\n"
346 " verbose Enable verbose/diagnostic logging\n"
347 " allow_memio Allow device registers to be memory mapped\n"
348 " debug Bitmask of debug values to enable\n"
349 " no_reset Supress initial bus resets\n"
350 " extended Enable extended geometry on all controllers\n"
351 " periodic_otag Send an ordered tagged transaction\n"
352 " periodically to prevent tag starvation.\n"
353 " This may be required by some older disk\n"
354 " or drives/RAID arrays.\n"
355 " reverse_scan Sort PCI devices highest Bus/Slot to lowest\n"
356 " tag_info:<tag_str> Set per-target tag depth\n"
357 " global_tag_depth:<int> Global tag depth for all targets on all buses\n"
358 " slewrate:<slewrate_list>Set the signal slew rate (0-15).\n"
359 " precomp:<pcomp_list> Set the signal precompensation (0-7).\n"
360 " amplitude:<int> Set the signal amplitude (0-7).\n"
361 " seltime:<int> Selection Timeout:\n"
362 " (0/256ms,1/128ms,2/64ms,3/32ms)\n"
364 " Sample /etc/modprobe.conf line:\n"
365 " Enable verbose logging\n"
366 " Set tag depth on Controller 2/Target 2 to 10 tags\n"
367 " Shorten the selection timeout to 128ms\n"
369 " options aic79xx 'aic79xx=verbose.tag_info:{{}.{}.{..10}}.seltime:1'\n"
372 static void ahd_linux_handle_scsi_status(struct ahd_softc *,
373 struct scsi_device *,
375 static void ahd_linux_queue_cmd_complete(struct ahd_softc *ahd,
376 struct scsi_cmnd *cmd);
377 static void ahd_linux_sem_timeout(u_long arg);
378 static int ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
379 static void ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd);
380 static u_int ahd_linux_user_tagdepth(struct ahd_softc *ahd,
381 struct ahd_devinfo *devinfo);
382 static void ahd_linux_device_queue_depth(struct scsi_device *);
383 static int ahd_linux_run_command(struct ahd_softc*,
384 struct ahd_linux_device *,
386 static void ahd_linux_setup_tag_info_global(char *p);
387 static aic_option_callback_t ahd_linux_setup_tag_info;
388 static aic_option_callback_t ahd_linux_setup_iocell_info;
389 static int aic79xx_setup(char *c);
390 static int ahd_linux_next_unit(void);
392 /****************************** Inlines ***************************************/
393 static __inline void ahd_linux_unmap_scb(struct ahd_softc*, struct scb*);
396 ahd_linux_unmap_scb(struct ahd_softc *ahd, struct scb *scb)
398 struct scsi_cmnd *cmd;
402 direction = cmd->sc_data_direction;
403 ahd_sync_sglist(ahd, scb, BUS_DMASYNC_POSTWRITE);
404 if (cmd->use_sg != 0) {
405 struct scatterlist *sg;
407 sg = (struct scatterlist *)cmd->request_buffer;
408 pci_unmap_sg(ahd->dev_softc, sg, cmd->use_sg, direction);
409 } else if (cmd->request_bufflen != 0) {
410 pci_unmap_single(ahd->dev_softc,
411 scb->platform_data->buf_busaddr,
412 cmd->request_bufflen, direction);
416 /******************************** Macros **************************************/
417 #define BUILD_SCSIID(ahd, cmd) \
418 ((((cmd)->device->id << TID_SHIFT) & TID) | (ahd)->our_id)
421 * Try to detect an Adaptec 79XX controller.
424 ahd_linux_detect(struct scsi_host_template *template)
426 struct ahd_softc *ahd;
431 * If we've been passed any parameters, process them now.
434 aic79xx_setup(aic79xx);
436 template->proc_name = "aic79xx";
439 * Initialize our softc list lock prior to
440 * probing for any adapters.
445 error = ahd_linux_pci_init();
451 * Register with the SCSI layer all
452 * controllers we've found.
455 TAILQ_FOREACH(ahd, &ahd_tailq, links) {
457 if (ahd_linux_register_host(ahd, template) == 0)
460 aic79xx_detect_complete++;
465 * Return a string describing the driver.
468 ahd_linux_info(struct Scsi_Host *host)
470 static char buffer[512];
473 struct ahd_softc *ahd;
476 ahd = *(struct ahd_softc **)host->hostdata;
477 memset(bp, 0, sizeof(buffer));
478 strcpy(bp, "Adaptec AIC79XX PCI-X SCSI HBA DRIVER, Rev ");
479 strcat(bp, AIC79XX_DRIVER_VERSION);
482 strcat(bp, ahd->description);
485 ahd_controller_info(ahd, ahd_info);
486 strcat(bp, ahd_info);
493 * Queue an SCB to the controller.
496 ahd_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
498 struct ahd_softc *ahd;
499 struct ahd_linux_device *dev = scsi_transport_device_data(cmd->device);
501 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
504 * Close the race of a command that was in the process of
505 * being queued to us just as our simq was frozen. Let
506 * DV commands through so long as we are only frozen to
509 if (ahd->platform_data->qfrozen != 0) {
510 printf("%s: queue frozen\n", ahd_name(ahd));
512 return SCSI_MLQUEUE_HOST_BUSY;
516 * Save the callback on completion function.
518 cmd->scsi_done = scsi_done;
520 cmd->result = CAM_REQ_INPROG << 16;
522 return ahd_linux_run_command(ahd, dev, cmd);
525 static inline struct scsi_target **
526 ahd_linux_target_in_softc(struct scsi_target *starget)
528 struct ahd_softc *ahd =
529 *((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
530 unsigned int target_offset;
532 target_offset = starget->id;
533 if (starget->channel != 0)
536 return &ahd->platform_data->starget[target_offset];
540 ahd_linux_target_alloc(struct scsi_target *starget)
542 struct ahd_softc *ahd =
543 *((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
545 struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
546 struct ahd_linux_target *targ = scsi_transport_target_data(starget);
547 struct ahd_devinfo devinfo;
548 struct ahd_initiator_tinfo *tinfo;
549 struct ahd_tmode_tstate *tstate;
550 char channel = starget->channel + 'A';
552 ahd_lock(ahd, &flags);
554 BUG_ON(*ahd_targp != NULL);
556 *ahd_targp = starget;
557 memset(targ, 0, sizeof(*targ));
559 tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
560 starget->id, &tstate);
561 ahd_compile_devinfo(&devinfo, ahd->our_id, starget->id,
562 CAM_LUN_WILDCARD, channel,
564 spi_min_period(starget) = AHD_SYNCRATE_MAX; /* We can do U320 */
565 if ((ahd->bugs & AHD_PACED_NEGTABLE_BUG) != 0)
566 spi_max_offset(starget) = MAX_OFFSET_PACED_BUG;
568 spi_max_offset(starget) = MAX_OFFSET_PACED;
569 spi_max_width(starget) = ahd->features & AHD_WIDE;
571 ahd_set_syncrate(ahd, &devinfo, 0, 0, 0,
572 AHD_TRANS_GOAL, /*paused*/FALSE);
573 ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
574 AHD_TRANS_GOAL, /*paused*/FALSE);
575 ahd_unlock(ahd, &flags);
581 ahd_linux_target_destroy(struct scsi_target *starget)
583 struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
589 ahd_linux_slave_alloc(struct scsi_device *sdev)
591 struct ahd_softc *ahd =
592 *((struct ahd_softc **)sdev->host->hostdata);
593 struct scsi_target *starget = sdev->sdev_target;
594 struct ahd_linux_target *targ = scsi_transport_target_data(starget);
595 struct ahd_linux_device *dev;
598 printf("%s: Slave Alloc %d\n", ahd_name(ahd), sdev->id);
600 BUG_ON(targ->sdev[sdev->lun] != NULL);
602 dev = scsi_transport_device_data(sdev);
603 memset(dev, 0, sizeof(*dev));
606 * We start out life using untagged
607 * transactions of which we allow one.
612 * Set maxtags to 0. This will be changed if we
613 * later determine that we are dealing with
614 * a tagged queuing capable device.
618 targ->sdev[sdev->lun] = sdev;
624 ahd_linux_slave_configure(struct scsi_device *sdev)
626 struct ahd_softc *ahd;
628 ahd = *((struct ahd_softc **)sdev->host->hostdata);
630 printf("%s: Slave Configure %d\n", ahd_name(ahd), sdev->id);
632 ahd_linux_device_queue_depth(sdev);
634 /* Initial Domain Validation */
635 if (!spi_initial_dv(sdev->sdev_target))
642 ahd_linux_slave_destroy(struct scsi_device *sdev)
644 struct ahd_softc *ahd;
645 struct ahd_linux_device *dev = scsi_transport_device_data(sdev);
646 struct ahd_linux_target *targ = scsi_transport_target_data(sdev->sdev_target);
648 ahd = *((struct ahd_softc **)sdev->host->hostdata);
650 printf("%s: Slave Destroy %d\n", ahd_name(ahd), sdev->id);
654 targ->sdev[sdev->lun] = NULL;
658 #if defined(__i386__)
660 * Return the disk geometry for the given SCSI device.
663 ahd_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
664 sector_t capacity, int geom[])
672 struct ahd_softc *ahd;
674 ahd = *((struct ahd_softc **)sdev->host->hostdata);
676 bh = scsi_bios_ptable(bdev);
678 ret = scsi_partsize(bh, capacity,
679 &geom[2], &geom[0], &geom[1]);
686 cylinders = aic_sector_div(capacity, heads, sectors);
688 if (aic79xx_extended != 0)
691 extended = (ahd->flags & AHD_EXTENDED_TRANS_A) != 0;
692 if (extended && cylinders >= 1024) {
695 cylinders = aic_sector_div(capacity, heads, sectors);
705 * Abort the current SCSI command(s).
708 ahd_linux_abort(struct scsi_cmnd *cmd)
712 error = ahd_linux_queue_recovery_cmd(cmd, SCB_ABORT);
714 printf("aic79xx_abort returns 0x%x\n", error);
719 * Attempt to send a target reset message to the device that timed out.
722 ahd_linux_dev_reset(struct scsi_cmnd *cmd)
726 error = ahd_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
728 printf("aic79xx_dev_reset returns 0x%x\n", error);
733 * Reset the SCSI bus.
736 ahd_linux_bus_reset(struct scsi_cmnd *cmd)
738 struct ahd_softc *ahd;
742 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
744 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
745 printf("%s: Bus reset called for cmd %p\n",
749 found = ahd_reset_channel(ahd, cmd->device->channel + 'A',
750 /*initiate reset*/TRUE);
754 printf("%s: SCSI bus reset delivered. "
755 "%d SCBs aborted.\n", ahd_name(ahd), found);
760 struct scsi_host_template aic79xx_driver_template = {
761 .module = THIS_MODULE,
763 .proc_info = ahd_linux_proc_info,
764 .info = ahd_linux_info,
765 .queuecommand = ahd_linux_queue,
766 .eh_abort_handler = ahd_linux_abort,
767 .eh_device_reset_handler = ahd_linux_dev_reset,
768 .eh_bus_reset_handler = ahd_linux_bus_reset,
769 #if defined(__i386__)
770 .bios_param = ahd_linux_biosparam,
772 .can_queue = AHD_MAX_QUEUE,
775 .use_clustering = ENABLE_CLUSTERING,
776 .slave_alloc = ahd_linux_slave_alloc,
777 .slave_configure = ahd_linux_slave_configure,
778 .slave_destroy = ahd_linux_slave_destroy,
779 .target_alloc = ahd_linux_target_alloc,
780 .target_destroy = ahd_linux_target_destroy,
783 /******************************** Bus DMA *************************************/
785 ahd_dma_tag_create(struct ahd_softc *ahd, bus_dma_tag_t parent,
786 bus_size_t alignment, bus_size_t boundary,
787 dma_addr_t lowaddr, dma_addr_t highaddr,
788 bus_dma_filter_t *filter, void *filterarg,
789 bus_size_t maxsize, int nsegments,
790 bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
794 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
799 * Linux is very simplistic about DMA memory. For now don't
800 * maintain all specification information. Once Linux supplies
801 * better facilities for doing these operations, or the
802 * needs of this particular driver change, we might need to do
805 dmat->alignment = alignment;
806 dmat->boundary = boundary;
807 dmat->maxsize = maxsize;
813 ahd_dma_tag_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat)
815 free(dmat, M_DEVBUF);
819 ahd_dmamem_alloc(struct ahd_softc *ahd, bus_dma_tag_t dmat, void** vaddr,
820 int flags, bus_dmamap_t *mapp)
822 *vaddr = pci_alloc_consistent(ahd->dev_softc,
823 dmat->maxsize, mapp);
830 ahd_dmamem_free(struct ahd_softc *ahd, bus_dma_tag_t dmat,
831 void* vaddr, bus_dmamap_t map)
833 pci_free_consistent(ahd->dev_softc, dmat->maxsize,
838 ahd_dmamap_load(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map,
839 void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
840 void *cb_arg, int flags)
843 * Assume for now that this will only be used during
844 * initialization and not for per-transaction buffer mapping.
846 bus_dma_segment_t stack_sg;
848 stack_sg.ds_addr = map;
849 stack_sg.ds_len = dmat->maxsize;
850 cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
855 ahd_dmamap_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
860 ahd_dmamap_unload(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
866 /********************* Platform Dependent Functions ***************************/
868 * Compare "left hand" softc with "right hand" softc, returning:
869 * < 0 - lahd has a lower priority than rahd
870 * 0 - Softcs are equal
871 * > 0 - lahd has a higher priority than rahd
874 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
879 * Under Linux, cards are ordered as follows:
880 * 1) PCI devices that are marked as the boot controller.
881 * 2) PCI devices with BIOS enabled sorted by bus/slot/func.
882 * 3) All remaining PCI devices sorted by bus/slot/func.
885 value = (lahd->flags & AHD_BOOT_CHANNEL)
886 - (rahd->flags & AHD_BOOT_CHANNEL);
888 /* Controllers set for boot have a *higher* priority */
892 value = (lahd->flags & AHD_BIOS_ENABLED)
893 - (rahd->flags & AHD_BIOS_ENABLED);
895 /* Controllers with BIOS enabled have a *higher* priority */
898 /* Still equal. Sort by bus/slot/func. */
899 if (aic79xx_reverse_scan != 0)
900 value = ahd_get_pci_bus(lahd->dev_softc)
901 - ahd_get_pci_bus(rahd->dev_softc);
903 value = ahd_get_pci_bus(rahd->dev_softc)
904 - ahd_get_pci_bus(lahd->dev_softc);
907 if (aic79xx_reverse_scan != 0)
908 value = ahd_get_pci_slot(lahd->dev_softc)
909 - ahd_get_pci_slot(rahd->dev_softc);
911 value = ahd_get_pci_slot(rahd->dev_softc)
912 - ahd_get_pci_slot(lahd->dev_softc);
916 value = rahd->channel - lahd->channel;
921 ahd_linux_setup_iocell_info(u_long index, int instance, int targ, int32_t value)
925 && (instance < NUM_ELEMENTS(aic79xx_iocell_info))) {
926 uint8_t *iocell_info;
928 iocell_info = (uint8_t*)&aic79xx_iocell_info[instance];
929 iocell_info[index] = value & 0xFFFF;
931 printf("iocell[%d:%ld] = %d\n", instance, index, value);
936 ahd_linux_setup_tag_info_global(char *p)
940 tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
941 printf("Setting Global Tags= %d\n", tags);
943 for (i = 0; i < NUM_ELEMENTS(aic79xx_tag_info); i++) {
944 for (j = 0; j < AHD_NUM_TARGETS; j++) {
945 aic79xx_tag_info[i].tag_commands[j] = tags;
951 ahd_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
954 if ((instance >= 0) && (targ >= 0)
955 && (instance < NUM_ELEMENTS(aic79xx_tag_info))
956 && (targ < AHD_NUM_TARGETS)) {
957 aic79xx_tag_info[instance].tag_commands[targ] = value & 0x1FF;
959 printf("tag_info[%d:%d] = %d\n", instance, targ, value);
964 * Handle Linux boot parameters. This routine allows for assigning a value
965 * to a parameter with a ':' between the parameter and the value.
966 * ie. aic79xx=stpwlev:1,extended
969 aic79xx_setup(char *s)
979 { "extended", &aic79xx_extended },
980 { "no_reset", &aic79xx_no_reset },
981 { "verbose", &aic79xx_verbose },
982 { "allow_memio", &aic79xx_allow_memio},
984 { "debug", &ahd_debug },
986 { "reverse_scan", &aic79xx_reverse_scan },
987 { "periodic_otag", &aic79xx_periodic_otag },
988 { "pci_parity", &aic79xx_pci_parity },
989 { "seltime", &aic79xx_seltime },
990 { "tag_info", NULL },
991 { "global_tag_depth", NULL},
992 { "slewrate", NULL },
994 { "amplitude", NULL },
997 end = strchr(s, '\0');
1000 * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
1001 * will never be 0 in this case.
1005 while ((p = strsep(&s, ",.")) != NULL) {
1008 for (i = 0; i < NUM_ELEMENTS(options); i++) {
1010 n = strlen(options[i].name);
1011 if (strncmp(options[i].name, p, n) == 0)
1014 if (i == NUM_ELEMENTS(options))
1017 if (strncmp(p, "global_tag_depth", n) == 0) {
1018 ahd_linux_setup_tag_info_global(p + n);
1019 } else if (strncmp(p, "tag_info", n) == 0) {
1020 s = aic_parse_brace_option("tag_info", p + n, end,
1021 2, ahd_linux_setup_tag_info, 0);
1022 } else if (strncmp(p, "slewrate", n) == 0) {
1023 s = aic_parse_brace_option("slewrate",
1024 p + n, end, 1, ahd_linux_setup_iocell_info,
1025 AIC79XX_SLEWRATE_INDEX);
1026 } else if (strncmp(p, "precomp", n) == 0) {
1027 s = aic_parse_brace_option("precomp",
1028 p + n, end, 1, ahd_linux_setup_iocell_info,
1029 AIC79XX_PRECOMP_INDEX);
1030 } else if (strncmp(p, "amplitude", n) == 0) {
1031 s = aic_parse_brace_option("amplitude",
1032 p + n, end, 1, ahd_linux_setup_iocell_info,
1033 AIC79XX_AMPLITUDE_INDEX);
1034 } else if (p[n] == ':') {
1035 *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
1036 } else if (!strncmp(p, "verbose", n)) {
1037 *(options[i].flag) = 1;
1039 *(options[i].flag) ^= 0xFFFFFFFF;
1045 __setup("aic79xx=", aic79xx_setup);
1047 uint32_t aic79xx_verbose;
1050 ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *template)
1053 struct Scsi_Host *host;
1057 template->name = ahd->description;
1058 host = scsi_host_alloc(template, sizeof(struct ahd_softc *));
1062 *((struct ahd_softc **)host->hostdata) = ahd;
1064 scsi_assign_lock(host, &ahd->platform_data->spin_lock);
1065 ahd->platform_data->host = host;
1066 host->can_queue = AHD_MAX_QUEUE;
1067 host->cmd_per_lun = 2;
1068 host->sg_tablesize = AHD_NSEG;
1069 host->this_id = ahd->our_id;
1070 host->irq = ahd->platform_data->irq;
1071 host->max_id = (ahd->features & AHD_WIDE) ? 16 : 8;
1072 host->max_lun = AHD_NUM_LUNS;
1073 host->max_channel = 0;
1074 host->sg_tablesize = AHD_NSEG;
1075 ahd_set_unit(ahd, ahd_linux_next_unit());
1076 sprintf(buf, "scsi%d", host->host_no);
1077 new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
1078 if (new_name != NULL) {
1079 strcpy(new_name, buf);
1080 ahd_set_name(ahd, new_name);
1082 host->unique_id = ahd->unit;
1083 ahd_linux_initialize_scsi_bus(ahd);
1084 ahd_intr_enable(ahd, TRUE);
1085 ahd_unlock(ahd, &s);
1087 host->transportt = ahd_linux_transport_template;
1089 scsi_add_host(host, &ahd->dev_softc->dev); /* XXX handle failure */
1090 scsi_scan_host(host);
1095 ahd_linux_get_memsize(void)
1100 return ((uint64_t)si.totalram << PAGE_SHIFT);
1104 * Find the smallest available unit number to use
1105 * for a new device. We don't just use a static
1106 * count to handle the "repeated hot-(un)plug"
1110 ahd_linux_next_unit(void)
1112 struct ahd_softc *ahd;
1117 TAILQ_FOREACH(ahd, &ahd_tailq, links) {
1118 if (ahd->unit == unit) {
1127 * Place the SCSI bus into a known state by either resetting it,
1128 * or forcing transfer negotiations on the next command to any
1132 ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd)
1140 if (aic79xx_no_reset != 0)
1141 ahd->flags &= ~AHD_RESET_BUS_A;
1143 if ((ahd->flags & AHD_RESET_BUS_A) != 0)
1144 ahd_reset_channel(ahd, 'A', /*initiate_reset*/TRUE);
1146 numtarg = (ahd->features & AHD_WIDE) ? 16 : 8;
1149 * Force negotiation to async for all targets that
1150 * will not see an initial bus reset.
1152 for (; target_id < numtarg; target_id++) {
1153 struct ahd_devinfo devinfo;
1154 struct ahd_initiator_tinfo *tinfo;
1155 struct ahd_tmode_tstate *tstate;
1157 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
1158 target_id, &tstate);
1159 ahd_compile_devinfo(&devinfo, ahd->our_id, target_id,
1160 CAM_LUN_WILDCARD, 'A', ROLE_INITIATOR);
1161 ahd_update_neg_request(ahd, &devinfo, tstate,
1162 tinfo, AHD_NEG_ALWAYS);
1164 /* Give the bus some time to recover */
1165 if ((ahd->flags & AHD_RESET_BUS_A) != 0) {
1166 ahd_freeze_simq(ahd);
1167 init_timer(&ahd->platform_data->reset_timer);
1168 ahd->platform_data->reset_timer.data = (u_long)ahd;
1169 ahd->platform_data->reset_timer.expires =
1170 jiffies + (AIC79XX_RESET_DELAY * HZ)/1000;
1171 ahd->platform_data->reset_timer.function =
1172 (ahd_linux_callback_t *)ahd_release_simq;
1173 add_timer(&ahd->platform_data->reset_timer);
1178 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1180 ahd->platform_data =
1181 malloc(sizeof(struct ahd_platform_data), M_DEVBUF, M_NOWAIT);
1182 if (ahd->platform_data == NULL)
1184 memset(ahd->platform_data, 0, sizeof(struct ahd_platform_data));
1185 ahd->platform_data->irq = AHD_LINUX_NOIRQ;
1187 init_MUTEX_LOCKED(&ahd->platform_data->eh_sem);
1188 ahd->seltime = (aic79xx_seltime & 0x3) << 4;
1193 ahd_platform_free(struct ahd_softc *ahd)
1195 struct scsi_target *starget;
1198 if (ahd->platform_data != NULL) {
1199 if (ahd->platform_data->host != NULL) {
1200 scsi_remove_host(ahd->platform_data->host);
1201 scsi_host_put(ahd->platform_data->host);
1204 /* destroy all of the device and target objects */
1205 for (i = 0; i < AHD_NUM_TARGETS; i++) {
1206 starget = ahd->platform_data->starget[i];
1207 if (starget != NULL) {
1208 for (j = 0; j < AHD_NUM_LUNS; j++) {
1209 struct ahd_linux_target *targ =
1210 scsi_transport_target_data(starget);
1211 if (targ->sdev[j] == NULL)
1213 targ->sdev[j] = NULL;
1215 ahd->platform_data->starget[i] = NULL;
1219 if (ahd->platform_data->irq != AHD_LINUX_NOIRQ)
1220 free_irq(ahd->platform_data->irq, ahd);
1221 if (ahd->tags[0] == BUS_SPACE_PIO
1222 && ahd->bshs[0].ioport != 0)
1223 release_region(ahd->bshs[0].ioport, 256);
1224 if (ahd->tags[1] == BUS_SPACE_PIO
1225 && ahd->bshs[1].ioport != 0)
1226 release_region(ahd->bshs[1].ioport, 256);
1227 if (ahd->tags[0] == BUS_SPACE_MEMIO
1228 && ahd->bshs[0].maddr != NULL) {
1229 iounmap(ahd->bshs[0].maddr);
1230 release_mem_region(ahd->platform_data->mem_busaddr,
1233 free(ahd->platform_data, M_DEVBUF);
1238 ahd_platform_init(struct ahd_softc *ahd)
1241 * Lookup and commit any modified IO Cell options.
1243 if (ahd->unit < NUM_ELEMENTS(aic79xx_iocell_info)) {
1244 struct ahd_linux_iocell_opts *iocell_opts;
1246 iocell_opts = &aic79xx_iocell_info[ahd->unit];
1247 if (iocell_opts->precomp != AIC79XX_DEFAULT_PRECOMP)
1248 AHD_SET_PRECOMP(ahd, iocell_opts->precomp);
1249 if (iocell_opts->slewrate != AIC79XX_DEFAULT_SLEWRATE)
1250 AHD_SET_SLEWRATE(ahd, iocell_opts->slewrate);
1251 if (iocell_opts->amplitude != AIC79XX_DEFAULT_AMPLITUDE)
1252 AHD_SET_AMPLITUDE(ahd, iocell_opts->amplitude);
1258 ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
1260 ahd_platform_abort_scbs(ahd, SCB_GET_TARGET(ahd, scb),
1261 SCB_GET_CHANNEL(ahd, scb),
1262 SCB_GET_LUN(scb), SCB_LIST_NULL,
1263 ROLE_UNKNOWN, CAM_REQUEUE_REQ);
1267 ahd_platform_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
1270 struct scsi_target *starget;
1271 struct ahd_linux_target *targ;
1272 struct ahd_linux_device *dev;
1273 struct scsi_device *sdev;
1277 starget = ahd->platform_data->starget[devinfo->target];
1278 targ = scsi_transport_target_data(starget);
1279 BUG_ON(targ == NULL);
1280 sdev = targ->sdev[devinfo->lun];
1284 dev = scsi_transport_device_data(sdev);
1288 was_queuing = dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED);
1291 case AHD_QUEUE_NONE:
1294 case AHD_QUEUE_BASIC:
1295 now_queuing = AHD_DEV_Q_BASIC;
1297 case AHD_QUEUE_TAGGED:
1298 now_queuing = AHD_DEV_Q_TAGGED;
1301 if ((dev->flags & AHD_DEV_FREEZE_TIL_EMPTY) == 0
1302 && (was_queuing != now_queuing)
1303 && (dev->active != 0)) {
1304 dev->flags |= AHD_DEV_FREEZE_TIL_EMPTY;
1308 dev->flags &= ~(AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED|AHD_DEV_PERIODIC_OTAG);
1312 usertags = ahd_linux_user_tagdepth(ahd, devinfo);
1315 * Start out agressively and allow our
1316 * dynamic queue depth algorithm to take
1319 dev->maxtags = usertags;
1320 dev->openings = dev->maxtags - dev->active;
1322 if (dev->maxtags == 0) {
1324 * Queueing is disabled by the user.
1327 } else if (alg == AHD_QUEUE_TAGGED) {
1328 dev->flags |= AHD_DEV_Q_TAGGED;
1329 if (aic79xx_periodic_otag != 0)
1330 dev->flags |= AHD_DEV_PERIODIC_OTAG;
1332 dev->flags |= AHD_DEV_Q_BASIC;
1334 /* We can only have one opening. */
1336 dev->openings = 1 - dev->active;
1339 switch ((dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED))) {
1340 case AHD_DEV_Q_BASIC:
1341 scsi_adjust_queue_depth(sdev,
1343 dev->openings + dev->active);
1345 case AHD_DEV_Q_TAGGED:
1346 scsi_adjust_queue_depth(sdev,
1348 dev->openings + dev->active);
1352 * We allow the OS to queue 2 untagged transactions to
1353 * us at any time even though we can only execute them
1354 * serially on the controller/device. This should
1355 * remove some latency.
1357 scsi_adjust_queue_depth(sdev,
1365 ahd_platform_abort_scbs(struct ahd_softc *ahd, int target, char channel,
1366 int lun, u_int tag, role_t role, uint32_t status)
1372 ahd_linux_user_tagdepth(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
1374 static int warned_user;
1378 if ((ahd->user_discenable & devinfo->target_mask) != 0) {
1379 if (ahd->unit >= NUM_ELEMENTS(aic79xx_tag_info)) {
1381 if (warned_user == 0) {
1383 "aic79xx: WARNING: Insufficient tag_info instances\n"
1384 "aic79xx: for installed controllers. Using defaults\n"
1385 "aic79xx: Please update the aic79xx_tag_info array in\n"
1386 "aic79xx: the aic79xx_osm.c source file.\n");
1389 tags = AHD_MAX_QUEUE;
1391 adapter_tag_info_t *tag_info;
1393 tag_info = &aic79xx_tag_info[ahd->unit];
1394 tags = tag_info->tag_commands[devinfo->target_offset];
1395 if (tags > AHD_MAX_QUEUE)
1396 tags = AHD_MAX_QUEUE;
1403 * Determines the queue depth for a given device.
1406 ahd_linux_device_queue_depth(struct scsi_device *sdev)
1408 struct ahd_devinfo devinfo;
1410 struct ahd_softc *ahd = *((struct ahd_softc **)sdev->host->hostdata);
1412 ahd_compile_devinfo(&devinfo,
1414 sdev->sdev_target->id, sdev->lun,
1415 sdev->sdev_target->channel == 0 ? 'A' : 'B',
1417 tags = ahd_linux_user_tagdepth(ahd, &devinfo);
1418 if (tags != 0 && sdev->tagged_supported != 0) {
1420 ahd_set_tags(ahd, &devinfo, AHD_QUEUE_TAGGED);
1421 ahd_print_devinfo(ahd, &devinfo);
1422 printf("Tagged Queuing enabled. Depth %d\n", tags);
1424 ahd_set_tags(ahd, &devinfo, AHD_QUEUE_NONE);
1429 ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
1430 struct scsi_cmnd *cmd)
1433 struct hardware_scb *hscb;
1434 struct ahd_initiator_tinfo *tinfo;
1435 struct ahd_tmode_tstate *tstate;
1440 * Get an scb to use.
1442 tinfo = ahd_fetch_transinfo(ahd, 'A', ahd->our_id,
1443 cmd->device->id, &tstate);
1444 if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) == 0
1445 || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
1446 col_idx = AHD_NEVER_COL_IDX;
1448 col_idx = AHD_BUILD_COL_IDX(cmd->device->id,
1451 if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
1452 ahd->flags |= AHD_RESOURCE_SHORTAGE;
1453 return SCSI_MLQUEUE_HOST_BUSY;
1457 scb->platform_data->dev = dev;
1459 cmd->host_scribble = (char *)scb;
1462 * Fill out basics of the HSCB.
1465 hscb->scsiid = BUILD_SCSIID(ahd, cmd);
1466 hscb->lun = cmd->device->lun;
1467 scb->hscb->task_management = 0;
1468 mask = SCB_GET_TARGET_MASK(ahd, scb);
1470 if ((ahd->user_discenable & mask) != 0)
1471 hscb->control |= DISCENB;
1473 if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0)
1474 scb->flags |= SCB_PACKETIZED;
1476 if ((tstate->auto_negotiate & mask) != 0) {
1477 scb->flags |= SCB_AUTO_NEGOTIATE;
1478 scb->hscb->control |= MK_MESSAGE;
1481 if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) != 0) {
1483 uint8_t tag_msgs[2];
1485 msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
1486 if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
1487 hscb->control |= tag_msgs[0];
1488 if (tag_msgs[0] == MSG_ORDERED_TASK)
1489 dev->commands_since_idle_or_otag = 0;
1491 if (dev->commands_since_idle_or_otag == AHD_OTAG_THRESH
1492 && (dev->flags & AHD_DEV_Q_TAGGED) != 0) {
1493 hscb->control |= MSG_ORDERED_TASK;
1494 dev->commands_since_idle_or_otag = 0;
1496 hscb->control |= MSG_SIMPLE_TASK;
1500 hscb->cdb_len = cmd->cmd_len;
1501 memcpy(hscb->shared_data.idata.cdb, cmd->cmnd, hscb->cdb_len);
1503 scb->platform_data->xfer_len = 0;
1504 ahd_set_residual(scb, 0);
1505 ahd_set_sense_residual(scb, 0);
1507 if (cmd->use_sg != 0) {
1509 struct scatterlist *cur_seg;
1513 cur_seg = (struct scatterlist *)cmd->request_buffer;
1514 dir = cmd->sc_data_direction;
1515 nseg = pci_map_sg(ahd->dev_softc, cur_seg,
1517 scb->platform_data->xfer_len = 0;
1518 for (sg = scb->sg_list; nseg > 0; nseg--, cur_seg++) {
1522 addr = sg_dma_address(cur_seg);
1523 len = sg_dma_len(cur_seg);
1524 scb->platform_data->xfer_len += len;
1525 sg = ahd_sg_setup(ahd, scb, sg, addr, len,
1528 } else if (cmd->request_bufflen != 0) {
1534 dir = cmd->sc_data_direction;
1535 addr = pci_map_single(ahd->dev_softc,
1536 cmd->request_buffer,
1537 cmd->request_bufflen, dir);
1538 scb->platform_data->xfer_len = cmd->request_bufflen;
1539 scb->platform_data->buf_busaddr = addr;
1540 sg = ahd_sg_setup(ahd, scb, sg, addr,
1541 cmd->request_bufflen, /*last*/TRUE);
1544 LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1547 dev->commands_issued++;
1549 if ((dev->flags & AHD_DEV_PERIODIC_OTAG) != 0)
1550 dev->commands_since_idle_or_otag++;
1551 scb->flags |= SCB_ACTIVE;
1552 ahd_queue_scb(ahd, scb);
1558 * SCSI controller interrupt handler.
1561 ahd_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
1563 struct ahd_softc *ahd;
1567 ahd = (struct ahd_softc *) dev_id;
1568 ahd_lock(ahd, &flags);
1569 ours = ahd_intr(ahd);
1570 ahd_unlock(ahd, &flags);
1571 return IRQ_RETVAL(ours);
1575 ahd_platform_flushwork(struct ahd_softc *ahd)
1581 ahd_send_async(struct ahd_softc *ahd, char channel,
1582 u_int target, u_int lun, ac_code code, void *arg)
1585 case AC_TRANSFER_NEG:
1588 struct scsi_target *starget;
1589 struct ahd_linux_target *targ;
1590 struct info_str info;
1591 struct ahd_initiator_tinfo *tinfo;
1592 struct ahd_tmode_tstate *tstate;
1593 unsigned int target_ppr_options;
1595 BUG_ON(target == CAM_TARGET_WILDCARD);
1598 info.length = sizeof(buf);
1601 tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
1605 * Don't bother reporting results while
1606 * negotiations are still pending.
1608 if (tinfo->curr.period != tinfo->goal.period
1609 || tinfo->curr.width != tinfo->goal.width
1610 || tinfo->curr.offset != tinfo->goal.offset
1611 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
1612 if (bootverbose == 0)
1616 * Don't bother reporting results that
1617 * are identical to those last reported.
1619 starget = ahd->platform_data->starget[target];
1620 targ = scsi_transport_target_data(starget);
1624 target_ppr_options =
1625 (spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
1626 + (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
1627 + (spi_iu(starget) ? MSG_EXT_PPR_IU_REQ : 0)
1628 + (spi_rd_strm(starget) ? MSG_EXT_PPR_RD_STRM : 0)
1629 + (spi_pcomp_en(starget) ? MSG_EXT_PPR_PCOMP_EN : 0)
1630 + (spi_rti(starget) ? MSG_EXT_PPR_RTI : 0)
1631 + (spi_wr_flow(starget) ? MSG_EXT_PPR_WR_FLOW : 0);
1633 if (tinfo->curr.period == spi_period(starget)
1634 && tinfo->curr.width == spi_width(starget)
1635 && tinfo->curr.offset == spi_offset(starget)
1636 && tinfo->curr.ppr_options == target_ppr_options)
1637 if (bootverbose == 0)
1640 spi_period(starget) = tinfo->curr.period;
1641 spi_width(starget) = tinfo->curr.width;
1642 spi_offset(starget) = tinfo->curr.offset;
1643 spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
1644 spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
1645 spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
1646 spi_rd_strm(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_RD_STRM ? 1 : 0;
1647 spi_pcomp_en(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_PCOMP_EN ? 1 : 0;
1648 spi_rti(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_RTI ? 1 : 0;
1649 spi_wr_flow(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_WR_FLOW ? 1 : 0;
1650 spi_display_xfer_agreement(starget);
1655 WARN_ON(lun != CAM_LUN_WILDCARD);
1656 scsi_report_device_reset(ahd->platform_data->host,
1657 channel - 'A', target);
1661 if (ahd->platform_data->host != NULL) {
1662 scsi_report_bus_reset(ahd->platform_data->host,
1667 panic("ahd_send_async: Unexpected async event");
1672 * Calls the higher level scsi done function and frees the scb.
1675 ahd_done(struct ahd_softc *ahd, struct scb *scb)
1677 struct scsi_cmnd *cmd;
1678 struct ahd_linux_device *dev;
1680 if ((scb->flags & SCB_ACTIVE) == 0) {
1681 printf("SCB %d done'd twice\n", SCB_GET_TAG(scb));
1682 ahd_dump_card_state(ahd);
1683 panic("Stopping for safety");
1685 LIST_REMOVE(scb, pending_links);
1687 dev = scb->platform_data->dev;
1690 if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
1691 cmd->result &= ~(CAM_DEV_QFRZN << 16);
1694 ahd_linux_unmap_scb(ahd, scb);
1697 * Guard against stale sense data.
1698 * The Linux mid-layer assumes that sense
1699 * was retrieved anytime the first byte of
1700 * the sense buffer looks "sane".
1702 cmd->sense_buffer[0] = 0;
1703 if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG) {
1704 uint32_t amount_xferred;
1707 ahd_get_transfer_length(scb) - ahd_get_residual(scb);
1708 if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
1710 if ((ahd_debug & AHD_SHOW_MISC) != 0) {
1711 ahd_print_path(ahd, scb);
1712 printf("Set CAM_UNCOR_PARITY\n");
1715 ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
1716 #ifdef AHD_REPORT_UNDERFLOWS
1718 * This code is disabled by default as some
1719 * clients of the SCSI system do not properly
1720 * initialize the underflow parameter. This
1721 * results in spurious termination of commands
1722 * that complete as expected (e.g. underflow is
1723 * allowed as command can return variable amounts
1726 } else if (amount_xferred < scb->io_ctx->underflow) {
1729 ahd_print_path(ahd, scb);
1731 for (i = 0; i < scb->io_ctx->cmd_len; i++)
1732 printf(" 0x%x", scb->io_ctx->cmnd[i]);
1734 ahd_print_path(ahd, scb);
1735 printf("Saw underflow (%ld of %ld bytes). "
1736 "Treated as error\n",
1737 ahd_get_residual(scb),
1738 ahd_get_transfer_length(scb));
1739 ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
1742 ahd_set_transaction_status(scb, CAM_REQ_CMP);
1744 } else if (ahd_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
1745 ahd_linux_handle_scsi_status(ahd, cmd->device, scb);
1748 if (dev->openings == 1
1749 && ahd_get_transaction_status(scb) == CAM_REQ_CMP
1750 && ahd_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
1751 dev->tag_success_count++;
1753 * Some devices deal with temporary internal resource
1754 * shortages by returning queue full. When the queue
1755 * full occurrs, we throttle back. Slowly try to get
1756 * back to our previous queue depth.
1758 if ((dev->openings + dev->active) < dev->maxtags
1759 && dev->tag_success_count > AHD_TAG_SUCCESS_INTERVAL) {
1760 dev->tag_success_count = 0;
1764 if (dev->active == 0)
1765 dev->commands_since_idle_or_otag = 0;
1767 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
1768 printf("Recovery SCB completes\n");
1769 if (ahd_get_transaction_status(scb) == CAM_BDR_SENT
1770 || ahd_get_transaction_status(scb) == CAM_REQ_ABORTED)
1771 ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1772 if ((ahd->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
1773 ahd->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
1774 up(&ahd->platform_data->eh_sem);
1778 ahd_free_scb(ahd, scb);
1779 ahd_linux_queue_cmd_complete(ahd, cmd);
1783 ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
1784 struct scsi_device *sdev, struct scb *scb)
1786 struct ahd_devinfo devinfo;
1787 struct ahd_linux_device *dev = scsi_transport_device_data(sdev);
1789 ahd_compile_devinfo(&devinfo,
1791 sdev->sdev_target->id, sdev->lun,
1792 sdev->sdev_target->channel == 0 ? 'A' : 'B',
1796 * We don't currently trust the mid-layer to
1797 * properly deal with queue full or busy. So,
1798 * when one occurs, we tell the mid-layer to
1799 * unconditionally requeue the command to us
1800 * so that we can retry it ourselves. We also
1801 * implement our own throttling mechanism so
1802 * we don't clobber the device with too many
1805 switch (ahd_get_scsi_status(scb)) {
1808 case SCSI_STATUS_CHECK_COND:
1809 case SCSI_STATUS_CMD_TERMINATED:
1811 struct scsi_cmnd *cmd;
1814 * Copy sense information to the OS's cmd
1815 * structure if it is available.
1818 if ((scb->flags & (SCB_SENSE|SCB_PKT_SENSE)) != 0) {
1819 struct scsi_status_iu_header *siu;
1823 if (scb->flags & SCB_SENSE) {
1824 sense_size = MIN(sizeof(struct scsi_sense_data)
1825 - ahd_get_sense_residual(scb),
1826 sizeof(cmd->sense_buffer));
1830 * Copy only the sense data into the provided
1833 siu = (struct scsi_status_iu_header *)
1835 sense_size = MIN(scsi_4btoul(siu->sense_length),
1836 sizeof(cmd->sense_buffer));
1837 sense_offset = SIU_SENSE_OFFSET(siu);
1840 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
1841 memcpy(cmd->sense_buffer,
1842 ahd_get_sense_buf(ahd, scb)
1843 + sense_offset, sense_size);
1844 cmd->result |= (DRIVER_SENSE << 24);
1847 if (ahd_debug & AHD_SHOW_SENSE) {
1850 printf("Copied %d bytes of sense data at %d:",
1851 sense_size, sense_offset);
1852 for (i = 0; i < sense_size; i++) {
1855 printf("0x%x ", cmd->sense_buffer[i]);
1863 case SCSI_STATUS_QUEUE_FULL:
1865 * By the time the core driver has returned this
1866 * command, all other commands that were queued
1867 * to us but not the device have been returned.
1868 * This ensures that dev->active is equal to
1869 * the number of commands actually queued to
1872 dev->tag_success_count = 0;
1873 if (dev->active != 0) {
1875 * Drop our opening count to the number
1876 * of commands currently outstanding.
1880 if ((ahd_debug & AHD_SHOW_QFULL) != 0) {
1881 ahd_print_path(ahd, scb);
1882 printf("Dropping tag count to %d\n",
1886 if (dev->active == dev->tags_on_last_queuefull) {
1888 dev->last_queuefull_same_count++;
1890 * If we repeatedly see a queue full
1891 * at the same queue depth, this
1892 * device has a fixed number of tag
1893 * slots. Lock in this tag depth
1894 * so we stop seeing queue fulls from
1897 if (dev->last_queuefull_same_count
1898 == AHD_LOCK_TAGS_COUNT) {
1899 dev->maxtags = dev->active;
1900 ahd_print_path(ahd, scb);
1901 printf("Locking max tag count at %d\n",
1905 dev->tags_on_last_queuefull = dev->active;
1906 dev->last_queuefull_same_count = 0;
1908 ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
1909 ahd_set_scsi_status(scb, SCSI_STATUS_OK);
1910 ahd_platform_set_tags(ahd, &devinfo,
1911 (dev->flags & AHD_DEV_Q_BASIC)
1912 ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
1916 * Drop down to a single opening, and treat this
1917 * as if the target returned BUSY SCSI status.
1920 ahd_platform_set_tags(ahd, &devinfo,
1921 (dev->flags & AHD_DEV_Q_BASIC)
1922 ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
1923 ahd_set_scsi_status(scb, SCSI_STATUS_BUSY);
1928 ahd_linux_queue_cmd_complete(struct ahd_softc *ahd, struct scsi_cmnd *cmd)
1931 * Map CAM error codes into Linux Error codes. We
1932 * avoid the conversion so that the DV code has the
1933 * full error information available when making
1934 * state change decisions.
1940 status = ahd_cmd_get_transaction_status(cmd);
1942 case CAM_REQ_INPROG:
1944 case CAM_SCSI_STATUS_ERROR:
1945 new_status = DID_OK;
1947 case CAM_REQ_ABORTED:
1948 new_status = DID_ABORT;
1951 new_status = DID_BUS_BUSY;
1953 case CAM_REQ_INVALID:
1954 case CAM_PATH_INVALID:
1955 new_status = DID_BAD_TARGET;
1957 case CAM_SEL_TIMEOUT:
1958 new_status = DID_NO_CONNECT;
1960 case CAM_SCSI_BUS_RESET:
1962 new_status = DID_RESET;
1964 case CAM_UNCOR_PARITY:
1965 new_status = DID_PARITY;
1967 case CAM_CMD_TIMEOUT:
1968 new_status = DID_TIME_OUT;
1971 case CAM_REQ_CMP_ERR:
1972 case CAM_AUTOSENSE_FAIL:
1974 case CAM_DATA_RUN_ERR:
1975 case CAM_UNEXP_BUSFREE:
1976 case CAM_SEQUENCE_FAIL:
1977 case CAM_CCB_LEN_ERR:
1978 case CAM_PROVIDE_FAIL:
1979 case CAM_REQ_TERMIO:
1980 case CAM_UNREC_HBA_ERROR:
1981 case CAM_REQ_TOO_BIG:
1982 new_status = DID_ERROR;
1984 case CAM_REQUEUE_REQ:
1985 new_status = DID_REQUEUE;
1988 /* We should never get here */
1989 new_status = DID_ERROR;
1993 ahd_cmd_set_transaction_status(cmd, new_status);
1996 cmd->scsi_done(cmd);
2000 ahd_linux_sem_timeout(u_long arg)
2002 struct ahd_softc *ahd;
2005 ahd = (struct ahd_softc *)arg;
2008 if ((ahd->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
2009 ahd->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
2010 up(&ahd->platform_data->eh_sem);
2012 ahd_unlock(ahd, &s);
2016 ahd_freeze_simq(struct ahd_softc *ahd)
2018 ahd->platform_data->qfrozen++;
2019 if (ahd->platform_data->qfrozen == 1) {
2020 scsi_block_requests(ahd->platform_data->host);
2021 ahd_platform_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
2022 CAM_LUN_WILDCARD, SCB_LIST_NULL,
2023 ROLE_INITIATOR, CAM_REQUEUE_REQ);
2028 ahd_release_simq(struct ahd_softc *ahd)
2035 if (ahd->platform_data->qfrozen > 0)
2036 ahd->platform_data->qfrozen--;
2037 if (ahd->platform_data->qfrozen == 0) {
2040 ahd_unlock(ahd, &s);
2042 * There is still a race here. The mid-layer
2043 * should keep its own freeze count and use
2044 * a bottom half handler to run the queues
2045 * so we can unblock with our own lock held.
2048 scsi_unblock_requests(ahd->platform_data->host);
2052 ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
2054 struct ahd_softc *ahd;
2055 struct ahd_linux_device *dev;
2056 struct scb *pending_scb;
2058 u_int active_scbptr;
2067 ahd_mode_state saved_modes;
2072 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
2074 printf("%s:%d:%d:%d: Attempting to queue a%s message:",
2075 ahd_name(ahd), cmd->device->channel,
2076 cmd->device->id, cmd->device->lun,
2077 flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
2080 for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
2081 printf(" 0x%x", cmd->cmnd[cdb_byte]);
2084 spin_lock_irq(&ahd->platform_data->spin_lock);
2087 * First determine if we currently own this command.
2088 * Start by searching the device queue. If not found
2089 * there, check the pending_scb list. If not found
2090 * at all, and the system wanted us to just abort the
2091 * command, return success.
2093 dev = scsi_transport_device_data(cmd->device);
2097 * No target device for this command exists,
2098 * so we must not still own the command.
2100 printf("%s:%d:%d:%d: Is not an active device\n",
2101 ahd_name(ahd), cmd->device->channel, cmd->device->id,
2108 * See if we can find a matching cmd in the pending list.
2110 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
2111 if (pending_scb->io_ctx == cmd)
2115 if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
2117 /* Any SCB for this device will do for a target reset */
2118 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
2119 if (ahd_match_scb(ahd, pending_scb, cmd->device->id,
2120 cmd->device->channel + 'A',
2122 SCB_LIST_NULL, ROLE_INITIATOR) == 0)
2127 if (pending_scb == NULL) {
2128 printf("%s:%d:%d:%d: Command not found\n",
2129 ahd_name(ahd), cmd->device->channel, cmd->device->id,
2134 if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
2136 * We can't queue two recovery actions using the same SCB
2143 * Ensure that the card doesn't do anything
2144 * behind our back. Also make sure that we
2145 * didn't "just" miss an interrupt that would
2148 was_paused = ahd_is_paused(ahd);
2149 ahd_pause_and_flushwork(ahd);
2152 if ((pending_scb->flags & SCB_ACTIVE) == 0) {
2153 printf("%s:%d:%d:%d: Command already completed\n",
2154 ahd_name(ahd), cmd->device->channel, cmd->device->id,
2159 printf("%s: At time of recovery, card was %spaused\n",
2160 ahd_name(ahd), was_paused ? "" : "not ");
2161 ahd_dump_card_state(ahd);
2163 disconnected = TRUE;
2164 if (flag == SCB_ABORT) {
2165 if (ahd_search_qinfifo(ahd, cmd->device->id,
2166 cmd->device->channel + 'A',
2168 pending_scb->hscb->tag,
2169 ROLE_INITIATOR, CAM_REQ_ABORTED,
2170 SEARCH_COMPLETE) > 0) {
2171 printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
2172 ahd_name(ahd), cmd->device->channel,
2173 cmd->device->id, cmd->device->lun);
2177 } else if (ahd_search_qinfifo(ahd, cmd->device->id,
2178 cmd->device->channel + 'A',
2179 cmd->device->lun, pending_scb->hscb->tag,
2180 ROLE_INITIATOR, /*status*/0,
2181 SEARCH_COUNT) > 0) {
2182 disconnected = FALSE;
2185 saved_modes = ahd_save_modes(ahd);
2186 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2187 last_phase = ahd_inb(ahd, LASTPHASE);
2188 saved_scbptr = ahd_get_scbptr(ahd);
2189 active_scbptr = saved_scbptr;
2190 if (disconnected && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
2191 struct scb *bus_scb;
2193 bus_scb = ahd_lookup_scb(ahd, active_scbptr);
2194 if (bus_scb == pending_scb)
2195 disconnected = FALSE;
2196 else if (flag != SCB_ABORT
2197 && ahd_inb(ahd, SAVED_SCSIID) == pending_scb->hscb->scsiid
2198 && ahd_inb(ahd, SAVED_LUN) == SCB_GET_LUN(pending_scb))
2199 disconnected = FALSE;
2203 * At this point, pending_scb is the scb associated with the
2204 * passed in command. That command is currently active on the
2205 * bus or is in the disconnected state.
2207 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
2208 if (last_phase != P_BUSFREE
2209 && (SCB_GET_TAG(pending_scb) == active_scbptr
2210 || (flag == SCB_DEVICE_RESET
2211 && SCSIID_TARGET(ahd, saved_scsiid) == cmd->device->id))) {
2214 * We're active on the bus, so assert ATN
2215 * and hope that the target responds.
2217 pending_scb = ahd_lookup_scb(ahd, active_scbptr);
2218 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2219 ahd_outb(ahd, MSG_OUT, HOST_MSG);
2220 ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
2221 printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
2222 ahd_name(ahd), cmd->device->channel,
2223 cmd->device->id, cmd->device->lun);
2225 } else if (disconnected) {
2228 * Actually re-queue this SCB in an attempt
2229 * to select the device before it reconnects.
2231 pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
2232 ahd_set_scbptr(ahd, SCB_GET_TAG(pending_scb));
2233 pending_scb->hscb->cdb_len = 0;
2234 pending_scb->hscb->task_attribute = 0;
2235 pending_scb->hscb->task_management = SIU_TASKMGMT_ABORT_TASK;
2237 if ((pending_scb->flags & SCB_PACKETIZED) != 0) {
2239 * Mark the SCB has having an outstanding
2240 * task management function. Should the command
2241 * complete normally before the task management
2242 * function can be sent, the host will be notified
2243 * to abort our requeued SCB.
2245 ahd_outb(ahd, SCB_TASK_MANAGEMENT,
2246 pending_scb->hscb->task_management);
2249 * If non-packetized, set the MK_MESSAGE control
2250 * bit indicating that we desire to send a message.
2251 * We also set the disconnected flag since there is
2252 * no guarantee that our SCB control byte matches
2253 * the version on the card. We don't want the
2254 * sequencer to abort the command thinking an
2255 * unsolicited reselection occurred.
2257 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
2260 * The sequencer will never re-reference the
2261 * in-core SCB. To make sure we are notified
2262 * during reslection, set the MK_MESSAGE flag in
2263 * the card's copy of the SCB.
2265 ahd_outb(ahd, SCB_CONTROL,
2266 ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
2270 * Clear out any entries in the QINFIFO first
2271 * so we are the next SCB for this target
2274 ahd_search_qinfifo(ahd, cmd->device->id,
2275 cmd->device->channel + 'A', cmd->device->lun,
2276 SCB_LIST_NULL, ROLE_INITIATOR,
2277 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
2278 ahd_qinfifo_requeue_tail(ahd, pending_scb);
2279 ahd_set_scbptr(ahd, saved_scbptr);
2280 ahd_print_path(ahd, pending_scb);
2281 printf("Device is disconnected, re-queuing SCB\n");
2284 printf("%s:%d:%d:%d: Unable to deliver message\n",
2285 ahd_name(ahd), cmd->device->channel,
2286 cmd->device->id, cmd->device->lun);
2293 * Our assumption is that if we don't have the command, no
2294 * recovery action was required, so we return success. Again,
2295 * the semantics of the mid-layer recovery engine are not
2296 * well defined, so this may change in time.
2303 struct timer_list timer;
2306 ahd->platform_data->flags |= AHD_SCB_UP_EH_SEM;
2307 spin_unlock_irq(&ahd->platform_data->spin_lock);
2309 timer.data = (u_long)ahd;
2310 timer.expires = jiffies + (5 * HZ);
2311 timer.function = ahd_linux_sem_timeout;
2313 printf("Recovery code sleeping\n");
2314 down(&ahd->platform_data->eh_sem);
2315 printf("Recovery code awake\n");
2316 ret = del_timer_sync(&timer);
2318 printf("Timer Expired\n");
2321 spin_lock_irq(&ahd->platform_data->spin_lock);
2323 spin_unlock_irq(&ahd->platform_data->spin_lock);
2327 static void ahd_linux_exit(void);
2329 static void ahd_linux_set_width(struct scsi_target *starget, int width)
2331 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2332 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2333 struct ahd_devinfo devinfo;
2334 unsigned long flags;
2336 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2337 starget->channel + 'A', ROLE_INITIATOR);
2338 ahd_lock(ahd, &flags);
2339 ahd_set_width(ahd, &devinfo, width, AHD_TRANS_GOAL, FALSE);
2340 ahd_unlock(ahd, &flags);
2343 static void ahd_linux_set_period(struct scsi_target *starget, int period)
2345 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2346 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2347 struct ahd_tmode_tstate *tstate;
2348 struct ahd_initiator_tinfo *tinfo
2349 = ahd_fetch_transinfo(ahd,
2350 starget->channel + 'A',
2351 shost->this_id, starget->id, &tstate);
2352 struct ahd_devinfo devinfo;
2353 unsigned int ppr_options = tinfo->goal.ppr_options;
2355 unsigned long flags;
2356 unsigned long offset = tinfo->goal.offset;
2359 if ((ahd_debug & AHD_SHOW_DV) != 0)
2360 printf("%s: set period to %d\n", ahd_name(ahd), period);
2363 offset = MAX_OFFSET;
2368 ppr_options |= MSG_EXT_PPR_DT_REQ;
2370 ppr_options |= MSG_EXT_PPR_IU_REQ;
2373 dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2375 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2376 starget->channel + 'A', ROLE_INITIATOR);
2378 /* all PPR requests apart from QAS require wide transfers */
2379 if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
2380 if (spi_width(starget) == 0)
2381 ppr_options &= MSG_EXT_PPR_QAS_REQ;
2384 ahd_find_syncrate(ahd, &period, &ppr_options,
2385 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2387 ahd_lock(ahd, &flags);
2388 ahd_set_syncrate(ahd, &devinfo, period, offset,
2389 ppr_options, AHD_TRANS_GOAL, FALSE);
2390 ahd_unlock(ahd, &flags);
2393 static void ahd_linux_set_offset(struct scsi_target *starget, int offset)
2395 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2396 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2397 struct ahd_tmode_tstate *tstate;
2398 struct ahd_initiator_tinfo *tinfo
2399 = ahd_fetch_transinfo(ahd,
2400 starget->channel + 'A',
2401 shost->this_id, starget->id, &tstate);
2402 struct ahd_devinfo devinfo;
2403 unsigned int ppr_options = 0;
2404 unsigned int period = 0;
2405 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2406 unsigned long flags;
2409 if ((ahd_debug & AHD_SHOW_DV) != 0)
2410 printf("%s: set offset to %d\n", ahd_name(ahd), offset);
2413 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2414 starget->channel + 'A', ROLE_INITIATOR);
2416 period = tinfo->goal.period;
2417 ppr_options = tinfo->goal.ppr_options;
2418 ahd_find_syncrate(ahd, &period, &ppr_options,
2419 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2422 ahd_lock(ahd, &flags);
2423 ahd_set_syncrate(ahd, &devinfo, period, offset, ppr_options,
2424 AHD_TRANS_GOAL, FALSE);
2425 ahd_unlock(ahd, &flags);
2428 static void ahd_linux_set_dt(struct scsi_target *starget, int dt)
2430 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2431 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2432 struct ahd_tmode_tstate *tstate;
2433 struct ahd_initiator_tinfo *tinfo
2434 = ahd_fetch_transinfo(ahd,
2435 starget->channel + 'A',
2436 shost->this_id, starget->id, &tstate);
2437 struct ahd_devinfo devinfo;
2438 unsigned int ppr_options = tinfo->goal.ppr_options
2439 & ~MSG_EXT_PPR_DT_REQ;
2440 unsigned int period = tinfo->goal.period;
2441 unsigned long flags;
2444 if ((ahd_debug & AHD_SHOW_DV) != 0)
2445 printf("%s: %s DT\n", ahd_name(ahd),
2446 dt ? "enabling" : "disabling");
2449 ppr_options |= MSG_EXT_PPR_DT_REQ;
2451 period = 9; /* at least 12.5ns for DT */
2454 period = 10; /* If resetting DT, period must be >= 25ns */
2455 /* IU is invalid without DT set */
2456 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2458 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2459 starget->channel + 'A', ROLE_INITIATOR);
2460 ahd_find_syncrate(ahd, &period, &ppr_options,
2461 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2463 ahd_lock(ahd, &flags);
2464 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2465 ppr_options, AHD_TRANS_GOAL, FALSE);
2466 ahd_unlock(ahd, &flags);
2469 static void ahd_linux_set_qas(struct scsi_target *starget, int qas)
2471 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2472 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2473 struct ahd_tmode_tstate *tstate;
2474 struct ahd_initiator_tinfo *tinfo
2475 = ahd_fetch_transinfo(ahd,
2476 starget->channel + 'A',
2477 shost->this_id, starget->id, &tstate);
2478 struct ahd_devinfo devinfo;
2479 unsigned int ppr_options = tinfo->goal.ppr_options
2480 & ~MSG_EXT_PPR_QAS_REQ;
2481 unsigned int period = tinfo->goal.period;
2483 unsigned long flags;
2486 if ((ahd_debug & AHD_SHOW_DV) != 0)
2487 printf("%s: %s QAS\n", ahd_name(ahd),
2488 qas ? "enabling" : "disabling");
2492 ppr_options |= MSG_EXT_PPR_QAS_REQ;
2495 dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2497 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2498 starget->channel + 'A', ROLE_INITIATOR);
2499 ahd_find_syncrate(ahd, &period, &ppr_options,
2500 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2502 ahd_lock(ahd, &flags);
2503 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2504 ppr_options, AHD_TRANS_GOAL, FALSE);
2505 ahd_unlock(ahd, &flags);
2508 static void ahd_linux_set_iu(struct scsi_target *starget, int iu)
2510 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2511 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2512 struct ahd_tmode_tstate *tstate;
2513 struct ahd_initiator_tinfo *tinfo
2514 = ahd_fetch_transinfo(ahd,
2515 starget->channel + 'A',
2516 shost->this_id, starget->id, &tstate);
2517 struct ahd_devinfo devinfo;
2518 unsigned int ppr_options = tinfo->goal.ppr_options
2519 & ~MSG_EXT_PPR_IU_REQ;
2520 unsigned int period = tinfo->goal.period;
2522 unsigned long flags;
2525 if ((ahd_debug & AHD_SHOW_DV) != 0)
2526 printf("%s: %s IU\n", ahd_name(ahd),
2527 iu ? "enabling" : "disabling");
2531 ppr_options |= MSG_EXT_PPR_IU_REQ;
2532 ppr_options |= MSG_EXT_PPR_DT_REQ; /* IU requires DT */
2535 dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2537 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2538 starget->channel + 'A', ROLE_INITIATOR);
2539 ahd_find_syncrate(ahd, &period, &ppr_options,
2540 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2542 ahd_lock(ahd, &flags);
2543 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2544 ppr_options, AHD_TRANS_GOAL, FALSE);
2545 ahd_unlock(ahd, &flags);
2548 static void ahd_linux_set_rd_strm(struct scsi_target *starget, int rdstrm)
2550 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2551 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2552 struct ahd_tmode_tstate *tstate;
2553 struct ahd_initiator_tinfo *tinfo
2554 = ahd_fetch_transinfo(ahd,
2555 starget->channel + 'A',
2556 shost->this_id, starget->id, &tstate);
2557 struct ahd_devinfo devinfo;
2558 unsigned int ppr_options = tinfo->goal.ppr_options
2559 & ~MSG_EXT_PPR_RD_STRM;
2560 unsigned int period = tinfo->goal.period;
2561 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2562 unsigned long flags;
2565 if ((ahd_debug & AHD_SHOW_DV) != 0)
2566 printf("%s: %s Read Streaming\n", ahd_name(ahd),
2567 rdstrm ? "enabling" : "disabling");
2571 ppr_options |= MSG_EXT_PPR_RD_STRM;
2573 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2574 starget->channel + 'A', ROLE_INITIATOR);
2575 ahd_find_syncrate(ahd, &period, &ppr_options,
2576 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2578 ahd_lock(ahd, &flags);
2579 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2580 ppr_options, AHD_TRANS_GOAL, FALSE);
2581 ahd_unlock(ahd, &flags);
2584 static void ahd_linux_set_wr_flow(struct scsi_target *starget, int wrflow)
2586 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2587 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2588 struct ahd_tmode_tstate *tstate;
2589 struct ahd_initiator_tinfo *tinfo
2590 = ahd_fetch_transinfo(ahd,
2591 starget->channel + 'A',
2592 shost->this_id, starget->id, &tstate);
2593 struct ahd_devinfo devinfo;
2594 unsigned int ppr_options = tinfo->goal.ppr_options
2595 & ~MSG_EXT_PPR_WR_FLOW;
2596 unsigned int period = tinfo->goal.period;
2597 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2598 unsigned long flags;
2601 if ((ahd_debug & AHD_SHOW_DV) != 0)
2602 printf("%s: %s Write Flow Control\n", ahd_name(ahd),
2603 wrflow ? "enabling" : "disabling");
2607 ppr_options |= MSG_EXT_PPR_WR_FLOW;
2609 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2610 starget->channel + 'A', ROLE_INITIATOR);
2611 ahd_find_syncrate(ahd, &period, &ppr_options,
2612 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2614 ahd_lock(ahd, &flags);
2615 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2616 ppr_options, AHD_TRANS_GOAL, FALSE);
2617 ahd_unlock(ahd, &flags);
2620 static void ahd_linux_set_rti(struct scsi_target *starget, int rti)
2622 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2623 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2624 struct ahd_tmode_tstate *tstate;
2625 struct ahd_initiator_tinfo *tinfo
2626 = ahd_fetch_transinfo(ahd,
2627 starget->channel + 'A',
2628 shost->this_id, starget->id, &tstate);
2629 struct ahd_devinfo devinfo;
2630 unsigned int ppr_options = tinfo->goal.ppr_options
2632 unsigned int period = tinfo->goal.period;
2633 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2634 unsigned long flags;
2636 if ((ahd->features & AHD_RTI) == 0) {
2638 if ((ahd_debug & AHD_SHOW_DV) != 0)
2639 printf("%s: RTI not available\n", ahd_name(ahd));
2645 if ((ahd_debug & AHD_SHOW_DV) != 0)
2646 printf("%s: %s RTI\n", ahd_name(ahd),
2647 rti ? "enabling" : "disabling");
2651 ppr_options |= MSG_EXT_PPR_RTI;
2653 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2654 starget->channel + 'A', ROLE_INITIATOR);
2655 ahd_find_syncrate(ahd, &period, &ppr_options,
2656 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2658 ahd_lock(ahd, &flags);
2659 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2660 ppr_options, AHD_TRANS_GOAL, FALSE);
2661 ahd_unlock(ahd, &flags);
2664 static void ahd_linux_set_pcomp_en(struct scsi_target *starget, int pcomp)
2666 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2667 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2668 struct ahd_tmode_tstate *tstate;
2669 struct ahd_initiator_tinfo *tinfo
2670 = ahd_fetch_transinfo(ahd,
2671 starget->channel + 'A',
2672 shost->this_id, starget->id, &tstate);
2673 struct ahd_devinfo devinfo;
2674 unsigned int ppr_options = tinfo->goal.ppr_options
2675 & ~MSG_EXT_PPR_PCOMP_EN;
2676 unsigned int period = tinfo->goal.period;
2677 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2678 unsigned long flags;
2681 if ((ahd_debug & AHD_SHOW_DV) != 0)
2682 printf("%s: %s Precompensation\n", ahd_name(ahd),
2683 pcomp ? "Enable" : "Disable");
2687 ppr_options |= MSG_EXT_PPR_PCOMP_EN;
2689 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2690 starget->channel + 'A', ROLE_INITIATOR);
2691 ahd_find_syncrate(ahd, &period, &ppr_options,
2692 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2694 ahd_lock(ahd, &flags);
2695 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2696 ppr_options, AHD_TRANS_GOAL, FALSE);
2697 ahd_unlock(ahd, &flags);
2700 static struct spi_function_template ahd_linux_transport_functions = {
2701 .set_offset = ahd_linux_set_offset,
2703 .set_period = ahd_linux_set_period,
2705 .set_width = ahd_linux_set_width,
2707 .set_dt = ahd_linux_set_dt,
2709 .set_iu = ahd_linux_set_iu,
2711 .set_qas = ahd_linux_set_qas,
2713 .set_rd_strm = ahd_linux_set_rd_strm,
2715 .set_wr_flow = ahd_linux_set_wr_flow,
2717 .set_rti = ahd_linux_set_rti,
2719 .set_pcomp_en = ahd_linux_set_pcomp_en,
2726 ahd_linux_init(void)
2728 ahd_linux_transport_template = spi_attach_transport(&ahd_linux_transport_functions);
2729 if (!ahd_linux_transport_template)
2731 scsi_transport_reserve_target(ahd_linux_transport_template,
2732 sizeof(struct ahd_linux_target));
2733 scsi_transport_reserve_device(ahd_linux_transport_template,
2734 sizeof(struct ahd_linux_device));
2735 if (ahd_linux_detect(&aic79xx_driver_template) > 0)
2737 spi_release_transport(ahd_linux_transport_template);
2743 ahd_linux_exit(void)
2745 ahd_linux_pci_exit();
2746 spi_release_transport(ahd_linux_transport_template);
2749 module_init(ahd_linux_init);
2750 module_exit(ahd_linux_exit);