blob: 40f32bb239723a4132ace21a777838fd0a8b1130 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AIC79xx device driver for Linux.
3 *
4 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic79xx_osm.c#171 $
5 *
6 * --------------------------------------------------------------------------
7 * Copyright (c) 1994-2000 Justin T. Gibbs.
8 * Copyright (c) 1997-1999 Doug Ledford
9 * Copyright (c) 2000-2003 Adaptec Inc.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45#include "aic79xx_osm.h"
46#include "aic79xx_inline.h"
47#include <scsi/scsicam.h>
48
Hannes Reinecke73a25462005-07-22 16:44:04 +020049static struct scsi_transport_template *ahd_linux_transport_template = NULL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Include aiclib.c as part of our
53 * "module dependencies are hard" work around.
54 */
55#include "aiclib.c"
56
57#include <linux/init.h> /* __setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/mm.h> /* For fetching system memory size */
Hannes Reinecke73a25462005-07-22 16:44:04 +020059#include <linux/blkdev.h> /* For block_size() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/delay.h> /* For ssleep/msleep */
61
62/*
63 * Lock protecting manipulation of the ahd softc list.
64 */
65spinlock_t ahd_list_spinlock;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Bucket size for counting good commands in between bad ones.
69 */
70#define AHD_LINUX_ERR_THRESH 1000
71
72/*
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.
77 */
78#ifdef CONFIG_AIC79XX_RESET_DELAY_MS
79#define AIC79XX_RESET_DELAY CONFIG_AIC79XX_RESET_DELAY_MS
80#else
81#define AIC79XX_RESET_DELAY 5000
82#endif
83
84/*
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.
90 *
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
93 * for channel 1.
94 */
95typedef struct {
96 uint16_t tag_commands[16]; /* Allow for wide/twin adapters. */
97} adapter_tag_info_t;
98
99/*
100 * Modify this as you see fit for your system.
101 *
102 * 0 tagged queuing disabled
103 * 1 <= n <= 253 n == max tags ever dispatched.
104 *
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.
114 *
115 * In this example, the first line will disable tagged queueing for all
116 * the devices on the first probed aic79xx adapter.
117 *
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.
121 *
122 * The third line is the same as the first line.
123 *
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.
128 */
129
130/*
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.
133adapter_tag_info_t aic79xx_tag_info[] =
134{
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}}
139};
140*/
141
142#ifdef CONFIG_AIC79XX_CMDS_PER_DEVICE
143#define AIC79XX_CMDS_PER_DEVICE CONFIG_AIC79XX_CMDS_PER_DEVICE
144#else
145#define AIC79XX_CMDS_PER_DEVICE AHD_MAX_QUEUE
146#endif
147
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 \
157}
158
159/*
160 * By default, use the number of commands specified by
161 * the users kernel configuration.
162 */
163static adapter_tag_info_t aic79xx_tag_info[] =
164{
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}
181};
182
183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * 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.
187 */
188struct ahd_linux_iocell_opts
189{
190 uint8_t precomp;
191 uint8_t slewrate;
192 uint8_t amplitude;
193};
194#define AIC79XX_DEFAULT_PRECOMP 0xFF
195#define AIC79XX_DEFAULT_SLEWRATE 0xFF
196#define AIC79XX_DEFAULT_AMPLITUDE 0xFF
197#define AIC79XX_DEFAULT_IOOPTS \
198{ \
199 AIC79XX_DEFAULT_PRECOMP, \
200 AIC79XX_DEFAULT_SLEWRATE, \
201 AIC79XX_DEFAULT_AMPLITUDE \
202}
203#define AIC79XX_PRECOMP_INDEX 0
204#define AIC79XX_SLEWRATE_INDEX 1
205#define AIC79XX_AMPLITUDE_INDEX 2
206static struct ahd_linux_iocell_opts aic79xx_iocell_info[] =
207{
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
224};
225
226/*
227 * There should be a specific return value for this in scsi.h, but
228 * it seems that most drivers ignore it.
229 */
230#define DID_UNDERFLOW DID_ERROR
231
232void
233ahd_print_path(struct ahd_softc *ahd, struct scb *scb)
234{
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);
240}
241
242/*
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.
246 */
247
248/*
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
251 * SCSI bus timeouts.
252 */
253static uint32_t aic79xx_no_reset;
254
255/*
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.
271 */
272static uint32_t aic79xx_reverse_scan;
273
274/*
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
278 */
279static uint32_t aic79xx_extended;
280
281/*
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
289 *
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
293 * -1.
294 */
295static uint32_t aic79xx_pci_parity = ~0;
296
297/*
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.
302 */
303uint32_t aic79xx_allow_memio = ~0;
304
305/*
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().
309 */
310int aic79xx_detect_complete;
311
312/*
313 * So that we can set how long each device is given as a selection timeout.
314 * The table of values goes like this:
315 * 0 - 256ms
316 * 1 - 128ms
317 * 2 - 64ms
318 * 3 - 32ms
319 * We default to 256ms because some older devices need a longer time
320 * to respond to initial selection.
321 */
322static uint32_t aic79xx_seltime;
323
324/*
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
330 * transaction.
331 */
332uint32_t aic79xx_periodic_otag;
333
334/*
335 * Module information and settable options.
336 */
337static char *aic79xx = NULL;
338
339MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
340MODULE_DESCRIPTION("Adaptec Aic790X U320 SCSI Host Bus Adapter driver");
341MODULE_LICENSE("Dual BSD/GPL");
342MODULE_VERSION(AIC79XX_DRIVER_VERSION);
Hannes Reinecke73a25462005-07-22 16:44:04 +0200343module_param(aic79xx, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344MODULE_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"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358" 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"
363"\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"
368"\n"
369" options aic79xx 'aic79xx=verbose.tag_info:{{}.{}.{..10}}.seltime:1'\n"
Hannes Reinecke73a25462005-07-22 16:44:04 +0200370"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372static void ahd_linux_handle_scsi_status(struct ahd_softc *,
Hannes Reinecke73a25462005-07-22 16:44:04 +0200373 struct scsi_device *,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct scb *);
375static void ahd_linux_queue_cmd_complete(struct ahd_softc *ahd,
Hannes Reinecke73a25462005-07-22 16:44:04 +0200376 struct scsi_cmnd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static void ahd_linux_sem_timeout(u_long arg);
Hannes Reinecke73a25462005-07-22 16:44:04 +0200378static int ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static void ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static u_int ahd_linux_user_tagdepth(struct ahd_softc *ahd,
381 struct ahd_devinfo *devinfo);
Hannes Reinecke73a25462005-07-22 16:44:04 +0200382static void ahd_linux_device_queue_depth(struct scsi_device *);
Hannes Reinecke60a13212005-07-22 16:42:28 +0200383static int ahd_linux_run_command(struct ahd_softc*,
Hannes Reinecke73a25462005-07-22 16:44:04 +0200384 struct ahd_linux_device *,
Hannes Reinecke60a13212005-07-22 16:42:28 +0200385 struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static void ahd_linux_setup_tag_info_global(char *p);
387static aic_option_callback_t ahd_linux_setup_tag_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static aic_option_callback_t ahd_linux_setup_iocell_info;
Hannes Reinecke73a25462005-07-22 16:44:04 +0200389static int aic79xx_setup(char *c);
390static int ahd_linux_next_unit(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392/****************************** Inlines ***************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393static __inline void ahd_linux_unmap_scb(struct ahd_softc*, struct scb*);
394
395static __inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396ahd_linux_unmap_scb(struct ahd_softc *ahd, struct scb *scb)
397{
Hannes Reinecke73a25462005-07-22 16:44:04 +0200398 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int direction;
400
401 cmd = scb->io_ctx;
be7db052005-04-17 15:26:13 -0500402 direction = cmd->sc_data_direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 ahd_sync_sglist(ahd, scb, BUS_DMASYNC_POSTWRITE);
404 if (cmd->use_sg != 0) {
405 struct scatterlist *sg;
406
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);
413 }
414}
415
416/******************************** Macros **************************************/
417#define BUILD_SCSIID(ahd, cmd) \
418 ((((cmd)->device->id << TID_SHIFT) & TID) | (ahd)->our_id)
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
421 * Try to detect an Adaptec 79XX controller.
422 */
423static int
Hannes Reinecke73a25462005-07-22 16:44:04 +0200424ahd_linux_detect(struct scsi_host_template *template)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct ahd_softc *ahd;
427 int found;
428 int error = 0;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * If we've been passed any parameters, process them now.
432 */
433 if (aic79xx)
434 aic79xx_setup(aic79xx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 template->proc_name = "aic79xx";
437
438 /*
439 * Initialize our softc list lock prior to
440 * probing for any adapters.
441 */
442 ahd_list_lockinit();
443
444#ifdef CONFIG_PCI
445 error = ahd_linux_pci_init();
446 if (error)
447 return error;
448#endif
449
450 /*
451 * Register with the SCSI layer all
452 * controllers we've found.
453 */
454 found = 0;
455 TAILQ_FOREACH(ahd, &ahd_tailq, links) {
456
457 if (ahd_linux_register_host(ahd, template) == 0)
458 found++;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 aic79xx_detect_complete++;
Hannes Reinecke73a25462005-07-22 16:44:04 +0200461 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
465 * Return a string describing the driver.
466 */
467static const char *
468ahd_linux_info(struct Scsi_Host *host)
469{
470 static char buffer[512];
471 char ahd_info[256];
472 char *bp;
473 struct ahd_softc *ahd;
474
475 bp = &buffer[0];
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);
480 strcat(bp, "\n");
481 strcat(bp, " <");
482 strcat(bp, ahd->description);
483 strcat(bp, ">\n");
484 strcat(bp, " ");
485 ahd_controller_info(ahd, ahd_info);
486 strcat(bp, ahd_info);
487 strcat(bp, "\n");
488
489 return (bp);
490}
491
492/*
493 * Queue an SCB to the controller.
494 */
495static int
Hannes Reinecke73a25462005-07-22 16:44:04 +0200496ahd_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct ahd_softc *ahd;
Hannes Reinecke73a25462005-07-22 16:44:04 +0200499 struct ahd_linux_device *dev = scsi_transport_device_data(cmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
502
503 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * 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
507 * perform DV.
508 */
Hannes Reinecke73a25462005-07-22 16:44:04 +0200509 if (ahd->platform_data->qfrozen != 0) {
Hannes Reinecke60a13212005-07-22 16:42:28 +0200510 printf("%s: queue frozen\n", ahd_name(ahd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Hannes Reinecke60a13212005-07-22 16:42:28 +0200512 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Hannes Reinecke60a13212005-07-22 16:42:28 +0200514
515 /*
516 * Save the callback on completion function.
517 */
518 cmd->scsi_done = scsi_done;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 cmd->result = CAM_REQ_INPROG << 16;
Hannes Reinecke60a13212005-07-22 16:42:28 +0200521
522 return ahd_linux_run_command(ahd, dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Hannes Reinecke73a25462005-07-22 16:44:04 +0200525static inline struct scsi_target **
526ahd_linux_target_in_softc(struct scsi_target *starget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Hannes Reinecke73a25462005-07-22 16:44:04 +0200528 struct ahd_softc *ahd =
529 *((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
530 unsigned int target_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Hannes Reinecke73a25462005-07-22 16:44:04 +0200532 target_offset = starget->id;
533 if (starget->channel != 0)
534 target_offset += 8;
535
536 return &ahd->platform_data->starget[target_offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static int
Hannes Reinecke73a25462005-07-22 16:44:04 +0200540ahd_linux_target_alloc(struct scsi_target *starget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Hannes Reinecke73a25462005-07-22 16:44:04 +0200542 struct ahd_softc *ahd =
543 *((struct ahd_softc **)dev_to_shost(&starget->dev)->hostdata);
544 unsigned long flags;
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';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Hannes Reinecke73a25462005-07-22 16:44:04 +0200552 ahd_lock(ahd, &flags);
553
554 BUG_ON(*ahd_targp != NULL);
555
556 *ahd_targp = starget;
557 memset(targ, 0, sizeof(*targ));
558
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,
563 ROLE_INITIATOR);
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;
567 else
568 spi_max_offset(starget) = MAX_OFFSET_PACED;
569 spi_max_width(starget) = ahd->features & AHD_WIDE;
570
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);
576
577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580static void
Hannes Reinecke73a25462005-07-22 16:44:04 +0200581ahd_linux_target_destroy(struct scsi_target *starget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Hannes Reinecke73a25462005-07-22 16:44:04 +0200583 struct scsi_target **ahd_targp = ahd_linux_target_in_softc(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Hannes Reinecke73a25462005-07-22 16:44:04 +0200585 *ahd_targp = NULL;
586}
587
588static int
589ahd_linux_slave_alloc(struct scsi_device *sdev)
590{
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;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (bootverbose)
Hannes Reinecke73a25462005-07-22 16:44:04 +0200598 printf("%s: Slave Alloc %d\n", ahd_name(ahd), sdev->id);
599
600 BUG_ON(targ->sdev[sdev->lun] != NULL);
601
602 dev = scsi_transport_device_data(sdev);
603 memset(dev, 0, sizeof(*dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /*
Hannes Reinecke73a25462005-07-22 16:44:04 +0200606 * We start out life using untagged
607 * transactions of which we allow one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Hannes Reinecke73a25462005-07-22 16:44:04 +0200609 dev->openings = 1;
610
611 /*
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.
615 */
616 dev->maxtags = 0;
617
618 targ->sdev[sdev->lun] = sdev;
619
620 return (0);
621}
622
623static int
624ahd_linux_slave_configure(struct scsi_device *sdev)
625{
626 struct ahd_softc *ahd;
627
628 ahd = *((struct ahd_softc **)sdev->host->hostdata);
629 if (bootverbose)
630 printf("%s: Slave Configure %d\n", ahd_name(ahd), sdev->id);
631
632 ahd_linux_device_queue_depth(sdev);
633
634 /* Initial Domain Validation */
635 if (!spi_initial_dv(sdev->sdev_target))
636 spi_dv_device(sdev);
637
638 return 0;
639}
640
641static void
642ahd_linux_slave_destroy(struct scsi_device *sdev)
643{
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);
647
648 ahd = *((struct ahd_softc **)sdev->host->hostdata);
649 if (bootverbose)
650 printf("%s: Slave Destroy %d\n", ahd_name(ahd), sdev->id);
651
652 BUG_ON(dev->active);
653
654 targ->sdev[sdev->lun] = NULL;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658#if defined(__i386__)
659/*
660 * Return the disk geometry for the given SCSI device.
661 */
662static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663ahd_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
664 sector_t capacity, int geom[])
665{
666 uint8_t *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int heads;
668 int sectors;
669 int cylinders;
670 int ret;
671 int extended;
672 struct ahd_softc *ahd;
673
674 ahd = *((struct ahd_softc **)sdev->host->hostdata);
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 bh = scsi_bios_ptable(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (bh) {
678 ret = scsi_partsize(bh, capacity,
679 &geom[2], &geom[0], &geom[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 kfree(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (ret != -1)
682 return (ret);
683 }
684 heads = 64;
685 sectors = 32;
686 cylinders = aic_sector_div(capacity, heads, sectors);
687
688 if (aic79xx_extended != 0)
689 extended = 1;
690 else
691 extended = (ahd->flags & AHD_EXTENDED_TRANS_A) != 0;
692 if (extended && cylinders >= 1024) {
693 heads = 255;
694 sectors = 63;
695 cylinders = aic_sector_div(capacity, heads, sectors);
696 }
697 geom[0] = heads;
698 geom[1] = sectors;
699 geom[2] = cylinders;
700 return (0);
701}
702#endif
703
704/*
705 * Abort the current SCSI command(s).
706 */
707static int
Hannes Reinecke73a25462005-07-22 16:44:04 +0200708ahd_linux_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Hannes Reinecke73a25462005-07-22 16:44:04 +0200710 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Hannes Reinecke73a25462005-07-22 16:44:04 +0200712 error = ahd_linux_queue_recovery_cmd(cmd, SCB_ABORT);
713 if (error != 0)
714 printf("aic79xx_abort returns 0x%x\n", error);
715 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/*
719 * Attempt to send a target reset message to the device that timed out.
720 */
721static int
Hannes Reinecke73a25462005-07-22 16:44:04 +0200722ahd_linux_dev_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Hannes Reinecke73a25462005-07-22 16:44:04 +0200724 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Hannes Reinecke73a25462005-07-22 16:44:04 +0200726 error = ahd_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
727 if (error != 0)
728 printf("aic79xx_dev_reset returns 0x%x\n", error);
729 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732/*
733 * Reset the SCSI bus.
734 */
735static int
Hannes Reinecke73a25462005-07-22 16:44:04 +0200736ahd_linux_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct ahd_softc *ahd;
739 u_long s;
740 int found;
741
742 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
743#ifdef AHD_DEBUG
744 if ((ahd_debug & AHD_SHOW_RECOVERY) != 0)
745 printf("%s: Bus reset called for cmd %p\n",
746 ahd_name(ahd), cmd);
747#endif
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400748 ahd_lock(ahd, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 found = ahd_reset_channel(ahd, cmd->device->channel + 'A',
750 /*initiate reset*/TRUE);
Jeff Garzik 68b3aa72005-05-28 07:56:31 -0400751 ahd_unlock(ahd, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (bootverbose)
754 printf("%s: SCSI bus reset delivered. "
755 "%d SCBs aborted.\n", ahd_name(ahd), found);
756
757 return (SUCCESS);
758}
759
Hannes Reinecke73a25462005-07-22 16:44:04 +0200760struct scsi_host_template aic79xx_driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 .module = THIS_MODULE,
762 .name = "aic79xx",
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,
771#endif
772 .can_queue = AHD_MAX_QUEUE,
773 .this_id = -1,
774 .cmd_per_lun = 2,
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,
Hannes Reinecke73a25462005-07-22 16:44:04 +0200779 .target_alloc = ahd_linux_target_alloc,
780 .target_destroy = ahd_linux_target_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781};
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/******************************** Bus DMA *************************************/
784int
785ahd_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)
791{
792 bus_dma_tag_t dmat;
793
794 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
795 if (dmat == NULL)
796 return (ENOMEM);
797
798 /*
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
803 * more here.
804 */
805 dmat->alignment = alignment;
806 dmat->boundary = boundary;
807 dmat->maxsize = maxsize;
808 *ret_tag = dmat;
809 return (0);
810}
811
812void
813ahd_dma_tag_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat)
814{
815 free(dmat, M_DEVBUF);
816}
817
818int
819ahd_dmamem_alloc(struct ahd_softc *ahd, bus_dma_tag_t dmat, void** vaddr,
820 int flags, bus_dmamap_t *mapp)
821{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *vaddr = pci_alloc_consistent(ahd->dev_softc,
Hannes Reinecke73a25462005-07-22 16:44:04 +0200823 dmat->maxsize, mapp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (*vaddr == NULL)
825 return (ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return(0);
827}
828
829void
830ahd_dmamem_free(struct ahd_softc *ahd, bus_dma_tag_t dmat,
831 void* vaddr, bus_dmamap_t map)
832{
833 pci_free_consistent(ahd->dev_softc, dmat->maxsize,
Hannes Reinecke73a25462005-07-22 16:44:04 +0200834 vaddr, map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837int
838ahd_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)
841{
842 /*
843 * Assume for now that this will only be used during
844 * initialization and not for per-transaction buffer mapping.
845 */
846 bus_dma_segment_t stack_sg;
847
Hannes Reinecke73a25462005-07-22 16:44:04 +0200848 stack_sg.ds_addr = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 stack_sg.ds_len = dmat->maxsize;
850 cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
851 return (0);
852}
853
854void
855ahd_dmamap_destroy(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
856{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859int
860ahd_dmamap_unload(struct ahd_softc *ahd, bus_dma_tag_t dmat, bus_dmamap_t map)
861{
862 /* Nothing to do */
863 return (0);
864}
865
866/********************* Platform Dependent Functions ***************************/
867/*
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
872 */
873int
874ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
875{
876 int value;
877
878 /*
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.
883 */
884#if 0
885 value = (lahd->flags & AHD_BOOT_CHANNEL)
886 - (rahd->flags & AHD_BOOT_CHANNEL);
887 if (value != 0)
888 /* Controllers set for boot have a *higher* priority */
889 return (value);
890#endif
891
892 value = (lahd->flags & AHD_BIOS_ENABLED)
893 - (rahd->flags & AHD_BIOS_ENABLED);
894 if (value != 0)
895 /* Controllers with BIOS enabled have a *higher* priority */
896 return (value);
897
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);
902 else
903 value = ahd_get_pci_bus(rahd->dev_softc)
904 - ahd_get_pci_bus(lahd->dev_softc);
905 if (value != 0)
906 return (value);
907 if (aic79xx_reverse_scan != 0)
908 value = ahd_get_pci_slot(lahd->dev_softc)
909 - ahd_get_pci_slot(rahd->dev_softc);
910 else
911 value = ahd_get_pci_slot(rahd->dev_softc)
912 - ahd_get_pci_slot(lahd->dev_softc);
913 if (value != 0)
914 return (value);
915
916 value = rahd->channel - lahd->channel;
917 return (value);
918}
919
920static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921ahd_linux_setup_iocell_info(u_long index, int instance, int targ, int32_t value)
922{
923
924 if ((instance >= 0)
925 && (instance < NUM_ELEMENTS(aic79xx_iocell_info))) {
926 uint8_t *iocell_info;
927
928 iocell_info = (uint8_t*)&aic79xx_iocell_info[instance];
929 iocell_info[index] = value & 0xFFFF;
930 if (bootverbose)
931 printf("iocell[%d:%ld] = %d\n", instance, index, value);
932 }
933}
934
935static void
936ahd_linux_setup_tag_info_global(char *p)
937{
938 int tags, i, j;
939
940 tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
941 printf("Setting Global Tags= %d\n", tags);
942
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;
946 }
947 }
948}
949
Hannes Reinecke73a25462005-07-22 16:44:04 +0200950static void
951ahd_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
952{
953
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;
958 if (bootverbose)
959 printf("tag_info[%d:%d] = %d\n", instance, targ, value);
960 }
961}
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963/*
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
967 */
968static int
969aic79xx_setup(char *s)
970{
971 int i, n;
972 char *p;
973 char *end;
974
975 static struct {
976 const char *name;
977 uint32_t *flag;
978 } options[] = {
979 { "extended", &aic79xx_extended },
980 { "no_reset", &aic79xx_no_reset },
981 { "verbose", &aic79xx_verbose },
982 { "allow_memio", &aic79xx_allow_memio},
983#ifdef AHD_DEBUG
984 { "debug", &ahd_debug },
985#endif
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},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 { "slewrate", NULL },
993 { "precomp", NULL },
994 { "amplitude", NULL },
995 };
996
997 end = strchr(s, '\0');
998
999 /*
1000 * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
1001 * will never be 0 in this case.
1002 */
1003 n = 0;
1004
1005 while ((p = strsep(&s, ",.")) != NULL) {
1006 if (*p == '\0')
1007 continue;
1008 for (i = 0; i < NUM_ELEMENTS(options); i++) {
1009
1010 n = strlen(options[i].name);
1011 if (strncmp(options[i].name, p, n) == 0)
1012 break;
1013 }
1014 if (i == NUM_ELEMENTS(options))
1015 continue;
1016
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } 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;
1038 } else {
1039 *(options[i].flag) ^= 0xFFFFFFFF;
1040 }
1041 }
1042 return 1;
1043}
1044
1045__setup("aic79xx=", aic79xx_setup);
1046
1047uint32_t aic79xx_verbose;
1048
1049int
Hannes Reinecke73a25462005-07-22 16:44:04 +02001050ahd_linux_register_host(struct ahd_softc *ahd, struct scsi_host_template *template)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 char buf[80];
1053 struct Scsi_Host *host;
1054 char *new_name;
1055 u_long s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 template->name = ahd->description;
1058 host = scsi_host_alloc(template, sizeof(struct ahd_softc *));
1059 if (host == NULL)
1060 return (ENOMEM);
1061
1062 *((struct ahd_softc **)host->hostdata) = ahd;
1063 ahd_lock(ahd, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 scsi_assign_lock(host, &ahd->platform_data->spin_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 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);
1081 }
1082 host->unique_id = ahd->unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ahd_linux_initialize_scsi_bus(ahd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 ahd_intr_enable(ahd, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ahd_unlock(ahd, &s);
1086
Hannes Reinecke73a25462005-07-22 16:44:04 +02001087 host->transportt = ahd_linux_transport_template;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 scsi_add_host(host, &ahd->dev_softc->dev); /* XXX handle failure */
1090 scsi_scan_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return (0);
1092}
1093
1094uint64_t
1095ahd_linux_get_memsize(void)
1096{
1097 struct sysinfo si;
1098
1099 si_meminfo(&si);
1100 return ((uint64_t)si.totalram << PAGE_SHIFT);
1101}
1102
1103/*
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"
1107 * scenario.
1108 */
1109static int
1110ahd_linux_next_unit(void)
1111{
1112 struct ahd_softc *ahd;
1113 int unit;
1114
1115 unit = 0;
1116retry:
1117 TAILQ_FOREACH(ahd, &ahd_tailq, links) {
1118 if (ahd->unit == unit) {
1119 unit++;
1120 goto retry;
1121 }
1122 }
1123 return (unit);
1124}
1125
1126/*
1127 * Place the SCSI bus into a known state by either resetting it,
1128 * or forcing transfer negotiations on the next command to any
1129 * target.
1130 */
1131static void
1132ahd_linux_initialize_scsi_bus(struct ahd_softc *ahd)
1133{
1134 u_int target_id;
1135 u_int numtarg;
1136
1137 target_id = 0;
1138 numtarg = 0;
1139
1140 if (aic79xx_no_reset != 0)
1141 ahd->flags &= ~AHD_RESET_BUS_A;
1142
1143 if ((ahd->flags & AHD_RESET_BUS_A) != 0)
1144 ahd_reset_channel(ahd, 'A', /*initiate_reset*/TRUE);
1145 else
1146 numtarg = (ahd->features & AHD_WIDE) ? 16 : 8;
1147
1148 /*
1149 * Force negotiation to async for all targets that
1150 * will not see an initial bus reset.
1151 */
1152 for (; target_id < numtarg; target_id++) {
1153 struct ahd_devinfo devinfo;
1154 struct ahd_initiator_tinfo *tinfo;
1155 struct ahd_tmode_tstate *tstate;
1156
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);
1163 }
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);
1174 }
1175}
1176
1177int
1178ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1179{
1180 ahd->platform_data =
1181 malloc(sizeof(struct ahd_platform_data), M_DEVBUF, M_NOWAIT);
1182 if (ahd->platform_data == NULL)
1183 return (ENOMEM);
1184 memset(ahd->platform_data, 0, sizeof(struct ahd_platform_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ahd->platform_data->irq = AHD_LINUX_NOIRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ahd_lockinit(ahd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 init_MUTEX_LOCKED(&ahd->platform_data->eh_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 ahd->seltime = (aic79xx_seltime & 0x3) << 4;
1189 return (0);
1190}
1191
1192void
1193ahd_platform_free(struct ahd_softc *ahd)
1194{
Hannes Reinecke73a25462005-07-22 16:44:04 +02001195 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int i, j;
1197
1198 if (ahd->platform_data != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (ahd->platform_data->host != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 scsi_remove_host(ahd->platform_data->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 scsi_host_put(ahd->platform_data->host);
1202 }
1203
1204 /* destroy all of the device and target objects */
1205 for (i = 0; i < AHD_NUM_TARGETS; i++) {
Hannes Reinecke73a25462005-07-22 16:44:04 +02001206 starget = ahd->platform_data->starget[i];
1207 if (starget != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 for (j = 0; j < AHD_NUM_LUNS; j++) {
Hannes Reinecke73a25462005-07-22 16:44:04 +02001209 struct ahd_linux_target *targ =
1210 scsi_transport_target_data(starget);
1211 if (targ->sdev[j] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 continue;
Hannes Reinecke73a25462005-07-22 16:44:04 +02001213 targ->sdev[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Hannes Reinecke73a25462005-07-22 16:44:04 +02001215 ahd->platform_data->starget[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 }
1218
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,
1231 0x1000);
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 free(ahd->platform_data, M_DEVBUF);
1234 }
1235}
1236
1237void
1238ahd_platform_init(struct ahd_softc *ahd)
1239{
1240 /*
1241 * Lookup and commit any modified IO Cell options.
1242 */
1243 if (ahd->unit < NUM_ELEMENTS(aic79xx_iocell_info)) {
1244 struct ahd_linux_iocell_opts *iocell_opts;
1245
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);
1253 }
1254
1255}
1256
1257void
1258ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb)
1259{
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);
1264}
1265
1266void
1267ahd_platform_set_tags(struct ahd_softc *ahd, struct ahd_devinfo *devinfo,
1268 ahd_queue_alg alg)
1269{
Hannes Reinecke73a25462005-07-22 16:44:04 +02001270 struct scsi_target *starget;
1271 struct ahd_linux_target *targ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 struct ahd_linux_device *dev;
Hannes Reinecke73a25462005-07-22 16:44:04 +02001273 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int was_queuing;
1275 int now_queuing;
1276
Hannes Reinecke73a25462005-07-22 16:44:04 +02001277 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];
1281 if (sdev == NULL)
1282 return;
1283
1284 dev = scsi_transport_device_data(sdev);
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (dev == NULL)
1287 return;
1288 was_queuing = dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED);
1289 switch (alg) {
1290 default:
1291 case AHD_QUEUE_NONE:
1292 now_queuing = 0;
1293 break;
1294 case AHD_QUEUE_BASIC:
1295 now_queuing = AHD_DEV_Q_BASIC;
1296 break;
1297 case AHD_QUEUE_TAGGED:
1298 now_queuing = AHD_DEV_Q_TAGGED;
1299 break;
1300 }
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;
1305 dev->qfrozen++;
1306 }
1307
1308 dev->flags &= ~(AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED|AHD_DEV_PERIODIC_OTAG);
1309 if (now_queuing) {
1310 u_int usertags;
1311
1312 usertags = ahd_linux_user_tagdepth(ahd, devinfo);
1313 if (!was_queuing) {
1314 /*
1315 * Start out agressively and allow our
1316 * dynamic queue depth algorithm to take
1317 * care of the rest.
1318 */
1319 dev->maxtags = usertags;
1320 dev->openings = dev->maxtags - dev->active;
1321 }
1322 if (dev->maxtags == 0) {
1323 /*
1324 * Queueing is disabled by the user.
1325 */
1326 dev->openings = 1;
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;
1331 } else
1332 dev->flags |= AHD_DEV_Q_BASIC;
1333 } else {
1334 /* We can only have one opening. */
1335 dev->maxtags = 0;
1336 dev->openings = 1 - dev->active;
1337 }
Hannes Reinecke60a13212005-07-22 16:42:28 +02001338
Hannes Reinecke73a25462005-07-22 16:44:04 +02001339 switch ((dev->flags & (AHD_DEV_Q_BASIC|AHD_DEV_Q_TAGGED))) {
1340 case AHD_DEV_Q_BASIC:
1341 scsi_adjust_queue_depth(sdev,
1342 MSG_SIMPLE_TASK,
1343 dev->openings + dev->active);
1344 break;
1345 case AHD_DEV_Q_TAGGED:
1346 scsi_adjust_queue_depth(sdev,
1347 MSG_ORDERED_TASK,
1348 dev->openings + dev->active);
1349 break;
1350 default:
1351 /*
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.
1356 */
1357 scsi_adjust_queue_depth(sdev,
1358 /*NON-TAGGED*/0,
1359 /*queue depth*/2);
1360 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364int
1365ahd_platform_abort_scbs(struct ahd_softc *ahd, int target, char channel,
1366 int lun, u_int tag, role_t role, uint32_t status)
1367{
Hannes Reinecke60a13212005-07-22 16:42:28 +02001368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371static u_int
1372ahd_linux_user_tagdepth(struct ahd_softc *ahd, struct ahd_devinfo *devinfo)
1373{
1374 static int warned_user;
1375 u_int tags;
1376
1377 tags = 0;
1378 if ((ahd->user_discenable & devinfo->target_mask) != 0) {
1379 if (ahd->unit >= NUM_ELEMENTS(aic79xx_tag_info)) {
1380
1381 if (warned_user == 0) {
1382 printf(KERN_WARNING
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");
1387 warned_user++;
1388 }
1389 tags = AHD_MAX_QUEUE;
1390 } else {
1391 adapter_tag_info_t *tag_info;
1392
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;
1397 }
1398 }
1399 return (tags);
1400}
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402/*
1403 * Determines the queue depth for a given device.
1404 */
1405static void
Hannes Reinecke73a25462005-07-22 16:44:04 +02001406ahd_linux_device_queue_depth(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 struct ahd_devinfo devinfo;
1409 u_int tags;
Hannes Reinecke73a25462005-07-22 16:44:04 +02001410 struct ahd_softc *ahd = *((struct ahd_softc **)sdev->host->hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 ahd_compile_devinfo(&devinfo,
1413 ahd->our_id,
Hannes Reinecke73a25462005-07-22 16:44:04 +02001414 sdev->sdev_target->id, sdev->lun,
1415 sdev->sdev_target->channel == 0 ? 'A' : 'B',
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 ROLE_INITIATOR);
1417 tags = ahd_linux_user_tagdepth(ahd, &devinfo);
Hannes Reinecke73a25462005-07-22 16:44:04 +02001418 if (tags != 0 && sdev->tagged_supported != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 ahd_set_tags(ahd, &devinfo, AHD_QUEUE_TAGGED);
1421 ahd_print_devinfo(ahd, &devinfo);
1422 printf("Tagged Queuing enabled. Depth %d\n", tags);
1423 } else {
1424 ahd_set_tags(ahd, &devinfo, AHD_QUEUE_NONE);
1425 }
1426}
1427
Hannes Reinecke60a13212005-07-22 16:42:28 +02001428static int
1429ahd_linux_run_command(struct ahd_softc *ahd, struct ahd_linux_device *dev,
1430 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 struct scb *scb;
1433 struct hardware_scb *hscb;
1434 struct ahd_initiator_tinfo *tinfo;
1435 struct ahd_tmode_tstate *tstate;
1436 u_int col_idx;
1437 uint16_t mask;
1438
Hannes Reinecke60a13212005-07-22 16:42:28 +02001439 /*
1440 * Get an scb to use.
1441 */
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;
1447 } else {
1448 col_idx = AHD_BUILD_COL_IDX(cmd->device->id,
1449 cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Hannes Reinecke60a13212005-07-22 16:42:28 +02001451 if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
1452 ahd->flags |= AHD_RESOURCE_SHORTAGE;
1453 return SCSI_MLQUEUE_HOST_BUSY;
1454 }
1455
1456 scb->io_ctx = cmd;
1457 scb->platform_data->dev = dev;
1458 hscb = scb->hscb;
1459 cmd->host_scribble = (char *)scb;
1460
1461 /*
1462 * Fill out basics of the HSCB.
1463 */
1464 hscb->control = 0;
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);
1469
1470 if ((ahd->user_discenable & mask) != 0)
1471 hscb->control |= DISCENB;
1472
Hannes Reinecke60a13212005-07-22 16:42:28 +02001473 if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0)
1474 scb->flags |= SCB_PACKETIZED;
1475
1476 if ((tstate->auto_negotiate & mask) != 0) {
1477 scb->flags |= SCB_AUTO_NEGOTIATE;
1478 scb->hscb->control |= MK_MESSAGE;
1479 }
1480
1481 if ((dev->flags & (AHD_DEV_Q_TAGGED|AHD_DEV_Q_BASIC)) != 0) {
1482 int msg_bytes;
1483 uint8_t tag_msgs[2];
1484
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;
1490 } else
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;
1495 } else {
1496 hscb->control |= MSG_SIMPLE_TASK;
1497 }
1498 }
1499
1500 hscb->cdb_len = cmd->cmd_len;
1501 memcpy(hscb->shared_data.idata.cdb, cmd->cmnd, hscb->cdb_len);
1502
Hannes Reinecke73a25462005-07-22 16:44:04 +02001503 scb->platform_data->xfer_len = 0;
Hannes Reinecke60a13212005-07-22 16:42:28 +02001504 ahd_set_residual(scb, 0);
1505 ahd_set_sense_residual(scb, 0);
Hannes Reinecke73a25462005-07-22 16:44:04 +02001506 scb->sg_count = 0;
Hannes Reinecke60a13212005-07-22 16:42:28 +02001507 if (cmd->use_sg != 0) {
1508 void *sg;
1509 struct scatterlist *cur_seg;
1510 u_int nseg;
1511 int dir;
1512
1513 cur_seg = (struct scatterlist *)cmd->request_buffer;
1514 dir = cmd->sc_data_direction;
1515 nseg = pci_map_sg(ahd->dev_softc, cur_seg,
1516 cmd->use_sg, dir);
1517 scb->platform_data->xfer_len = 0;
1518 for (sg = scb->sg_list; nseg > 0; nseg--, cur_seg++) {
1519 dma_addr_t addr;
1520 bus_size_t len;
1521
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,
1526 /*last*/nseg == 1);
1527 }
1528 } else if (cmd->request_bufflen != 0) {
1529 void *sg;
1530 dma_addr_t addr;
1531 int dir;
1532
1533 sg = scb->sg_list;
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);
1542 }
1543
1544 LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1545 dev->openings--;
1546 dev->active++;
1547 dev->commands_issued++;
1548
Hannes Reinecke60a13212005-07-22 16:42:28 +02001549 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);
1553
1554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557/*
1558 * SCSI controller interrupt handler.
1559 */
1560irqreturn_t
1561ahd_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
1562{
1563 struct ahd_softc *ahd;
1564 u_long flags;
1565 int ours;
1566
1567 ahd = (struct ahd_softc *) dev_id;
1568 ahd_lock(ahd, &flags);
1569 ours = ahd_intr(ahd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 ahd_unlock(ahd, &flags);
1571 return IRQ_RETVAL(ours);
1572}
1573
1574void
1575ahd_platform_flushwork(struct ahd_softc *ahd)
1576{
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580void
1581ahd_send_async(struct ahd_softc *ahd, char channel,
1582 u_int target, u_int lun, ac_code code, void *arg)
1583{
1584 switch (code) {
1585 case AC_TRANSFER_NEG:
1586 {
1587 char buf[80];
Hannes Reinecke73a25462005-07-22 16:44:04 +02001588 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 struct ahd_linux_target *targ;
1590 struct info_str info;
1591 struct ahd_initiator_tinfo *tinfo;
1592 struct ahd_tmode_tstate *tstate;
Hannes Reinecke73a25462005-07-22 16:44:04 +02001593 unsigned int target_ppr_options;
1594
1595 BUG_ON(target == CAM_TARGET_WILDCARD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 info.buffer = buf;
1598 info.length = sizeof(buf);
1599 info.offset = 0;
1600 info.pos = 0;
1601 tinfo = ahd_fetch_transinfo(ahd, channel, ahd->our_id,
1602 target, &tstate);
1603
1604 /*
1605 * Don't bother reporting results while
1606 * negotiations are still pending.
1607 */
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)
1613 break;
1614
1615 /*
1616 * Don't bother reporting results that
1617 * are identical to those last reported.
1618 */
Hannes Reinecke73a25462005-07-22 16:44:04 +02001619 starget = ahd->platform_data->starget[target];
1620 targ = scsi_transport_target_data(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (targ == NULL)
1622 break;
Hannes Reinecke73a25462005-07-22 16:44:04 +02001623
1624 target_ppr_options =
1625 (spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
1626 + (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
James Bottomley3f40d7d2005-08-03 13:25:10 -05001627 + (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)
James Bottomley88ff29a2005-08-03 15:59:04 -05001631 + (spi_wr_flow(starget) ? MSG_EXT_PPR_WR_FLOW : 0)
1632 + (spi_hold_mcs(starget) ? MSG_EXT_PPR_HOLD_MCS : 0);
Hannes Reinecke73a25462005-07-22 16:44:04 +02001633
1634 if (tinfo->curr.period == spi_period(starget)
1635 && tinfo->curr.width == spi_width(starget)
1636 && tinfo->curr.offset == spi_offset(starget)
1637 && tinfo->curr.ppr_options == target_ppr_options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (bootverbose == 0)
1639 break;
1640
Hannes Reinecke73a25462005-07-22 16:44:04 +02001641 spi_period(starget) = tinfo->curr.period;
1642 spi_width(starget) = tinfo->curr.width;
1643 spi_offset(starget) = tinfo->curr.offset;
Hannes Reineckea4b53a12005-08-01 09:52:56 +02001644 spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ ? 1 : 0;
1645 spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ ? 1 : 0;
1646 spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ ? 1 : 0;
James Bottomley3f40d7d2005-08-03 13:25:10 -05001647 spi_rd_strm(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_RD_STRM ? 1 : 0;
1648 spi_pcomp_en(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_PCOMP_EN ? 1 : 0;
1649 spi_rti(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_RTI ? 1 : 0;
1650 spi_wr_flow(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_WR_FLOW ? 1 : 0;
James Bottomley88ff29a2005-08-03 15:59:04 -05001651 spi_hold_mcs(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_HOLD_MCS ? 1 : 0;
Hannes Reinecke73a25462005-07-22 16:44:04 +02001652 spi_display_xfer_agreement(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 }
1655 case AC_SENT_BDR:
1656 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 WARN_ON(lun != CAM_LUN_WILDCARD);
1658 scsi_report_device_reset(ahd->platform_data->host,
1659 channel - 'A', target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661 }
1662 case AC_BUS_RESET:
1663 if (ahd->platform_data->host != NULL) {
1664 scsi_report_bus_reset(ahd->platform_data->host,
1665 channel - 'A');
1666 }
1667 break;
1668 default:
1669 panic("ahd_send_async: Unexpected async event");
1670 }
1671}
1672
1673/*
1674 * Calls the higher level scsi done function and frees the scb.
1675 */
1676void
1677ahd_done(struct ahd_softc *ahd, struct scb *scb)
1678{
Hannes Reinecke73a25462005-07-22 16:44:04 +02001679 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct ahd_linux_device *dev;
1681
1682 if ((scb->flags & SCB_ACTIVE) == 0) {
1683 printf("SCB %d done'd twice\n", SCB_GET_TAG(scb));
1684 ahd_dump_card_state(ahd);
1685 panic("Stopping for safety");
1686 }
1687 LIST_REMOVE(scb, pending_links);
1688 cmd = scb->io_ctx;
1689 dev = scb->platform_data->dev;
1690 dev->active--;
1691 dev->openings++;
1692 if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
1693 cmd->result &= ~(CAM_DEV_QFRZN << 16);
1694 dev->qfrozen--;
1695 }
1696 ahd_linux_unmap_scb(ahd, scb);
1697
1698 /*
1699 * Guard against stale sense data.
1700 * The Linux mid-layer assumes that sense
1701 * was retrieved anytime the first byte of
1702 * the sense buffer looks "sane".
1703 */
1704 cmd->sense_buffer[0] = 0;
1705 if (ahd_get_transaction_status(scb) == CAM_REQ_INPROG) {
1706 uint32_t amount_xferred;
1707
1708 amount_xferred =
1709 ahd_get_transfer_length(scb) - ahd_get_residual(scb);
1710 if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
1711#ifdef AHD_DEBUG
1712 if ((ahd_debug & AHD_SHOW_MISC) != 0) {
1713 ahd_print_path(ahd, scb);
1714 printf("Set CAM_UNCOR_PARITY\n");
1715 }
1716#endif
1717 ahd_set_transaction_status(scb, CAM_UNCOR_PARITY);
1718#ifdef AHD_REPORT_UNDERFLOWS
1719 /*
1720 * This code is disabled by default as some
1721 * clients of the SCSI system do not properly
1722 * initialize the underflow parameter. This
1723 * results in spurious termination of commands
1724 * that complete as expected (e.g. underflow is
1725 * allowed as command can return variable amounts
1726 * of data.
1727 */
1728 } else if (amount_xferred < scb->io_ctx->underflow) {
1729 u_int i;
1730
1731 ahd_print_path(ahd, scb);
1732 printf("CDB:");
1733 for (i = 0; i < scb->io_ctx->cmd_len; i++)
1734 printf(" 0x%x", scb->io_ctx->cmnd[i]);
1735 printf("\n");
1736 ahd_print_path(ahd, scb);
1737 printf("Saw underflow (%ld of %ld bytes). "
1738 "Treated as error\n",
1739 ahd_get_residual(scb),
1740 ahd_get_transfer_length(scb));
1741 ahd_set_transaction_status(scb, CAM_DATA_RUN_ERR);
1742#endif
1743 } else {
1744 ahd_set_transaction_status(scb, CAM_REQ_CMP);
1745 }
1746 } else if (ahd_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
Hannes Reinecke73a25462005-07-22 16:44:04 +02001747 ahd_linux_handle_scsi_status(ahd, cmd->device, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 if (dev->openings == 1
1751 && ahd_get_transaction_status(scb) == CAM_REQ_CMP
1752 && ahd_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
1753 dev->tag_success_count++;
1754 /*
1755 * Some devices deal with temporary internal resource
1756 * shortages by returning queue full. When the queue
1757 * full occurrs, we throttle back. Slowly try to get
1758 * back to our previous queue depth.
1759 */
1760 if ((dev->openings + dev->active) < dev->maxtags
1761 && dev->tag_success_count > AHD_TAG_SUCCESS_INTERVAL) {
1762 dev->tag_success_count = 0;
1763 dev->openings++;
1764 }
1765
1766 if (dev->active == 0)
1767 dev->commands_since_idle_or_otag = 0;
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
1770 printf("Recovery SCB completes\n");
1771 if (ahd_get_transaction_status(scb) == CAM_BDR_SENT
1772 || ahd_get_transaction_status(scb) == CAM_REQ_ABORTED)
1773 ahd_set_transaction_status(scb, CAM_CMD_TIMEOUT);
Hannes Reinecke73a25462005-07-22 16:44:04 +02001774 if ((ahd->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
1775 ahd->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 up(&ahd->platform_data->eh_sem);
1777 }
1778 }
1779
1780 ahd_free_scb(ahd, scb);
1781 ahd_linux_queue_cmd_complete(ahd, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
1784static void
1785ahd_linux_handle_scsi_status(struct ahd_softc *ahd,
Hannes Reinecke73a25462005-07-22 16:44:04 +02001786 struct scsi_device *sdev, struct scb *scb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 struct ahd_devinfo devinfo;
Hannes Reinecke73a25462005-07-22 16:44:04 +02001789 struct ahd_linux_device *dev = scsi_transport_device_data(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 ahd_compile_devinfo(&devinfo,
1792 ahd->our_id,
Hannes Reinecke73a25462005-07-22 16:44:04 +02001793 sdev->sdev_target->id, sdev->lun,
1794 sdev->sdev_target->channel == 0 ? 'A' : 'B',
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 ROLE_INITIATOR);
1796
1797 /*
1798 * We don't currently trust the mid-layer to
1799 * properly deal with queue full or busy. So,
1800 * when one occurs, we tell the mid-layer to
1801 * unconditionally requeue the command to us
1802 * so that we can retry it ourselves. We also
1803 * implement our own throttling mechanism so
1804 * we don't clobber the device with too many
1805 * commands.
1806 */
1807 switch (ahd_get_scsi_status(scb)) {
1808 default:
1809 break;
1810 case SCSI_STATUS_CHECK_COND:
1811 case SCSI_STATUS_CMD_TERMINATED:
1812 {
Hannes Reinecke73a25462005-07-22 16:44:04 +02001813 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 /*
1816 * Copy sense information to the OS's cmd
1817 * structure if it is available.
1818 */
1819 cmd = scb->io_ctx;
1820 if ((scb->flags & (SCB_SENSE|SCB_PKT_SENSE)) != 0) {
1821 struct scsi_status_iu_header *siu;
1822 u_int sense_size;
1823 u_int sense_offset;
1824
1825 if (scb->flags & SCB_SENSE) {
1826 sense_size = MIN(sizeof(struct scsi_sense_data)
1827 - ahd_get_sense_residual(scb),
1828 sizeof(cmd->sense_buffer));
1829 sense_offset = 0;
1830 } else {
1831 /*
1832 * Copy only the sense data into the provided
1833 * buffer.
1834 */
1835 siu = (struct scsi_status_iu_header *)
1836 scb->sense_data;
1837 sense_size = MIN(scsi_4btoul(siu->sense_length),
1838 sizeof(cmd->sense_buffer));
1839 sense_offset = SIU_SENSE_OFFSET(siu);
1840 }
1841
1842 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
1843 memcpy(cmd->sense_buffer,
1844 ahd_get_sense_buf(ahd, scb)
1845 + sense_offset, sense_size);
1846 cmd->result |= (DRIVER_SENSE << 24);
1847
1848#ifdef AHD_DEBUG
1849 if (ahd_debug & AHD_SHOW_SENSE) {
1850 int i;
1851
1852 printf("Copied %d bytes of sense data at %d:",
1853 sense_size, sense_offset);
1854 for (i = 0; i < sense_size; i++) {
1855 if ((i & 0xF) == 0)
1856 printf("\n");
1857 printf("0x%x ", cmd->sense_buffer[i]);
1858 }
1859 printf("\n");
1860 }
1861#endif
1862 }
1863 break;
1864 }
1865 case SCSI_STATUS_QUEUE_FULL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 /*
1867 * By the time the core driver has returned this
1868 * command, all other commands that were queued
1869 * to us but not the device have been returned.
1870 * This ensures that dev->active is equal to
1871 * the number of commands actually queued to
1872 * the device.
1873 */
1874 dev->tag_success_count = 0;
1875 if (dev->active != 0) {
1876 /*
1877 * Drop our opening count to the number
1878 * of commands currently outstanding.
1879 */
1880 dev->openings = 0;
1881#ifdef AHD_DEBUG
1882 if ((ahd_debug & AHD_SHOW_QFULL) != 0) {
1883 ahd_print_path(ahd, scb);
1884 printf("Dropping tag count to %d\n",
1885 dev->active);
1886 }
1887#endif
1888 if (dev->active == dev->tags_on_last_queuefull) {
1889
1890 dev->last_queuefull_same_count++;
1891 /*
1892 * If we repeatedly see a queue full
1893 * at the same queue depth, this
1894 * device has a fixed number of tag
1895 * slots. Lock in this tag depth
1896 * so we stop seeing queue fulls from
1897 * this device.
1898 */
1899 if (dev->last_queuefull_same_count
1900 == AHD_LOCK_TAGS_COUNT) {
1901 dev->maxtags = dev->active;
1902 ahd_print_path(ahd, scb);
1903 printf("Locking max tag count at %d\n",
1904 dev->active);
1905 }
1906 } else {
1907 dev->tags_on_last_queuefull = dev->active;
1908 dev->last_queuefull_same_count = 0;
1909 }
1910 ahd_set_transaction_status(scb, CAM_REQUEUE_REQ);
1911 ahd_set_scsi_status(scb, SCSI_STATUS_OK);
1912 ahd_platform_set_tags(ahd, &devinfo,
1913 (dev->flags & AHD_DEV_Q_BASIC)
1914 ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
1915 break;
1916 }
1917 /*
1918 * Drop down to a single opening, and treat this
1919 * as if the target returned BUSY SCSI status.
1920 */
1921 dev->openings = 1;
1922 ahd_platform_set_tags(ahd, &devinfo,
1923 (dev->flags & AHD_DEV_Q_BASIC)
1924 ? AHD_QUEUE_BASIC : AHD_QUEUE_TAGGED);
1925 ahd_set_scsi_status(scb, SCSI_STATUS_BUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927}
1928
1929static void
Hannes Reinecke73a25462005-07-22 16:44:04 +02001930ahd_linux_queue_cmd_complete(struct ahd_softc *ahd, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 * Map CAM error codes into Linux Error codes. We
1934 * avoid the conversion so that the DV code has the
1935 * full error information available when making
1936 * state change decisions.
1937 */
Hannes Reinecke73a25462005-07-22 16:44:04 +02001938 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 uint32_t status;
1940 u_int new_status;
1941
1942 status = ahd_cmd_get_transaction_status(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 switch (status) {
1944 case CAM_REQ_INPROG:
1945 case CAM_REQ_CMP:
1946 case CAM_SCSI_STATUS_ERROR:
1947 new_status = DID_OK;
1948 break;
1949 case CAM_REQ_ABORTED:
1950 new_status = DID_ABORT;
1951 break;
1952 case CAM_BUSY:
1953 new_status = DID_BUS_BUSY;
1954 break;
1955 case CAM_REQ_INVALID:
1956 case CAM_PATH_INVALID:
1957 new_status = DID_BAD_TARGET;
1958 break;
1959 case CAM_SEL_TIMEOUT:
1960 new_status = DID_NO_CONNECT;
1961 break;
1962 case CAM_SCSI_BUS_RESET:
1963 case CAM_BDR_SENT:
1964 new_status = DID_RESET;
1965 break;
1966 case CAM_UNCOR_PARITY:
1967 new_status = DID_PARITY;
1968 break;
1969 case CAM_CMD_TIMEOUT:
1970 new_status = DID_TIME_OUT;
1971 break;
1972 case CAM_UA_ABORT:
1973 case CAM_REQ_CMP_ERR:
1974 case CAM_AUTOSENSE_FAIL:
1975 case CAM_NO_HBA:
1976 case CAM_DATA_RUN_ERR:
1977 case CAM_UNEXP_BUSFREE:
1978 case CAM_SEQUENCE_FAIL:
1979 case CAM_CCB_LEN_ERR:
1980 case CAM_PROVIDE_FAIL:
1981 case CAM_REQ_TERMIO:
1982 case CAM_UNREC_HBA_ERROR:
1983 case CAM_REQ_TOO_BIG:
1984 new_status = DID_ERROR;
1985 break;
1986 case CAM_REQUEUE_REQ:
Hannes Reinecke73a25462005-07-22 16:44:04 +02001987 new_status = DID_REQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 break;
1989 default:
1990 /* We should never get here */
1991 new_status = DID_ERROR;
1992 break;
1993 }
1994
1995 ahd_cmd_set_transaction_status(cmd, new_status);
1996 }
1997
Hannes Reinecke73a25462005-07-22 16:44:04 +02001998 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
2001static void
Hannes Reinecke73a25462005-07-22 16:44:04 +02002002ahd_linux_sem_timeout(u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Hannes Reinecke73a25462005-07-22 16:44:04 +02002004 struct ahd_softc *ahd;
2005 u_long s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Hannes Reinecke73a25462005-07-22 16:44:04 +02002007 ahd = (struct ahd_softc *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Hannes Reinecke73a25462005-07-22 16:44:04 +02002009 ahd_lock(ahd, &s);
2010 if ((ahd->platform_data->flags & AHD_SCB_UP_EH_SEM) != 0) {
2011 ahd->platform_data->flags &= ~AHD_SCB_UP_EH_SEM;
2012 up(&ahd->platform_data->eh_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Hannes Reinecke73a25462005-07-22 16:44:04 +02002014 ahd_unlock(ahd, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
2017void
2018ahd_freeze_simq(struct ahd_softc *ahd)
2019{
2020 ahd->platform_data->qfrozen++;
2021 if (ahd->platform_data->qfrozen == 1) {
2022 scsi_block_requests(ahd->platform_data->host);
2023 ahd_platform_abort_scbs(ahd, CAM_TARGET_WILDCARD, ALL_CHANNELS,
2024 CAM_LUN_WILDCARD, SCB_LIST_NULL,
2025 ROLE_INITIATOR, CAM_REQUEUE_REQ);
2026 }
2027}
2028
2029void
2030ahd_release_simq(struct ahd_softc *ahd)
2031{
2032 u_long s;
2033 int unblock_reqs;
2034
2035 unblock_reqs = 0;
2036 ahd_lock(ahd, &s);
2037 if (ahd->platform_data->qfrozen > 0)
2038 ahd->platform_data->qfrozen--;
2039 if (ahd->platform_data->qfrozen == 0) {
2040 unblock_reqs = 1;
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 ahd_unlock(ahd, &s);
2043 /*
2044 * There is still a race here. The mid-layer
2045 * should keep its own freeze count and use
2046 * a bottom half handler to run the queues
2047 * so we can unblock with our own lock held.
2048 */
2049 if (unblock_reqs)
2050 scsi_unblock_requests(ahd->platform_data->host);
2051}
2052
Hannes Reinecke73a25462005-07-22 16:44:04 +02002053static int
2054ahd_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct ahd_softc *ahd;
Hannes Reinecke73a25462005-07-22 16:44:04 +02002057 struct ahd_linux_device *dev;
2058 struct scb *pending_scb;
2059 u_int saved_scbptr;
2060 u_int active_scbptr;
2061 u_int last_phase;
2062 u_int saved_scsiid;
2063 u_int cdb_byte;
2064 int retval;
2065 int was_paused;
2066 int paused;
2067 int wait;
2068 int disconnected;
2069 ahd_mode_state saved_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Hannes Reinecke73a25462005-07-22 16:44:04 +02002071 pending_scb = NULL;
2072 paused = FALSE;
2073 wait = FALSE;
2074 ahd = *(struct ahd_softc **)cmd->device->host->hostdata;
2075
2076 printf("%s:%d:%d:%d: Attempting to queue a%s message:",
2077 ahd_name(ahd), cmd->device->channel,
2078 cmd->device->id, cmd->device->lun,
2079 flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
2080
2081 printf("CDB:");
2082 for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
2083 printf(" 0x%x", cmd->cmnd[cdb_byte]);
2084 printf("\n");
2085
2086 spin_lock_irq(&ahd->platform_data->spin_lock);
2087
2088 /*
2089 * First determine if we currently own this command.
2090 * Start by searching the device queue. If not found
2091 * there, check the pending_scb list. If not found
2092 * at all, and the system wanted us to just abort the
2093 * command, return success.
2094 */
2095 dev = scsi_transport_device_data(cmd->device);
2096
2097 if (dev == NULL) {
2098 /*
2099 * No target device for this command exists,
2100 * so we must not still own the command.
2101 */
2102 printf("%s:%d:%d:%d: Is not an active device\n",
2103 ahd_name(ahd), cmd->device->channel, cmd->device->id,
2104 cmd->device->lun);
2105 retval = SUCCESS;
2106 goto no_cmd;
2107 }
2108
2109 /*
2110 * See if we can find a matching cmd in the pending list.
2111 */
2112 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
2113 if (pending_scb->io_ctx == cmd)
2114 break;
2115 }
2116
2117 if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
2118
2119 /* Any SCB for this device will do for a target reset */
2120 LIST_FOREACH(pending_scb, &ahd->pending_scbs, pending_links) {
2121 if (ahd_match_scb(ahd, pending_scb, cmd->device->id,
2122 cmd->device->channel + 'A',
2123 CAM_LUN_WILDCARD,
2124 SCB_LIST_NULL, ROLE_INITIATOR) == 0)
2125 break;
2126 }
2127 }
2128
2129 if (pending_scb == NULL) {
2130 printf("%s:%d:%d:%d: Command not found\n",
2131 ahd_name(ahd), cmd->device->channel, cmd->device->id,
2132 cmd->device->lun);
2133 goto no_cmd;
2134 }
2135
2136 if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
2137 /*
2138 * We can't queue two recovery actions using the same SCB
2139 */
2140 retval = FAILED;
2141 goto done;
2142 }
2143
2144 /*
2145 * Ensure that the card doesn't do anything
2146 * behind our back. Also make sure that we
2147 * didn't "just" miss an interrupt that would
2148 * affect this cmd.
2149 */
2150 was_paused = ahd_is_paused(ahd);
2151 ahd_pause_and_flushwork(ahd);
2152 paused = TRUE;
2153
2154 if ((pending_scb->flags & SCB_ACTIVE) == 0) {
2155 printf("%s:%d:%d:%d: Command already completed\n",
2156 ahd_name(ahd), cmd->device->channel, cmd->device->id,
2157 cmd->device->lun);
2158 goto no_cmd;
2159 }
2160
2161 printf("%s: At time of recovery, card was %spaused\n",
2162 ahd_name(ahd), was_paused ? "" : "not ");
2163 ahd_dump_card_state(ahd);
2164
2165 disconnected = TRUE;
2166 if (flag == SCB_ABORT) {
2167 if (ahd_search_qinfifo(ahd, cmd->device->id,
2168 cmd->device->channel + 'A',
2169 cmd->device->lun,
2170 pending_scb->hscb->tag,
2171 ROLE_INITIATOR, CAM_REQ_ABORTED,
2172 SEARCH_COMPLETE) > 0) {
2173 printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
2174 ahd_name(ahd), cmd->device->channel,
2175 cmd->device->id, cmd->device->lun);
2176 retval = SUCCESS;
2177 goto done;
2178 }
2179 } else if (ahd_search_qinfifo(ahd, cmd->device->id,
2180 cmd->device->channel + 'A',
2181 cmd->device->lun, pending_scb->hscb->tag,
2182 ROLE_INITIATOR, /*status*/0,
2183 SEARCH_COUNT) > 0) {
2184 disconnected = FALSE;
2185 }
2186
2187 saved_modes = ahd_save_modes(ahd);
2188 ahd_set_modes(ahd, AHD_MODE_SCSI, AHD_MODE_SCSI);
2189 last_phase = ahd_inb(ahd, LASTPHASE);
2190 saved_scbptr = ahd_get_scbptr(ahd);
2191 active_scbptr = saved_scbptr;
2192 if (disconnected && (ahd_inb(ahd, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
2193 struct scb *bus_scb;
2194
2195 bus_scb = ahd_lookup_scb(ahd, active_scbptr);
2196 if (bus_scb == pending_scb)
2197 disconnected = FALSE;
2198 else if (flag != SCB_ABORT
2199 && ahd_inb(ahd, SAVED_SCSIID) == pending_scb->hscb->scsiid
2200 && ahd_inb(ahd, SAVED_LUN) == SCB_GET_LUN(pending_scb))
2201 disconnected = FALSE;
2202 }
2203
2204 /*
2205 * At this point, pending_scb is the scb associated with the
2206 * passed in command. That command is currently active on the
2207 * bus or is in the disconnected state.
2208 */
2209 saved_scsiid = ahd_inb(ahd, SAVED_SCSIID);
2210 if (last_phase != P_BUSFREE
2211 && (SCB_GET_TAG(pending_scb) == active_scbptr
2212 || (flag == SCB_DEVICE_RESET
2213 && SCSIID_TARGET(ahd, saved_scsiid) == cmd->device->id))) {
2214
2215 /*
2216 * We're active on the bus, so assert ATN
2217 * and hope that the target responds.
2218 */
2219 pending_scb = ahd_lookup_scb(ahd, active_scbptr);
2220 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2221 ahd_outb(ahd, MSG_OUT, HOST_MSG);
2222 ahd_outb(ahd, SCSISIGO, last_phase|ATNO);
2223 printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
2224 ahd_name(ahd), cmd->device->channel,
2225 cmd->device->id, cmd->device->lun);
2226 wait = TRUE;
2227 } else if (disconnected) {
2228
2229 /*
2230 * Actually re-queue this SCB in an attempt
2231 * to select the device before it reconnects.
2232 */
2233 pending_scb->flags |= SCB_RECOVERY_SCB|SCB_ABORT;
2234 ahd_set_scbptr(ahd, SCB_GET_TAG(pending_scb));
2235 pending_scb->hscb->cdb_len = 0;
2236 pending_scb->hscb->task_attribute = 0;
2237 pending_scb->hscb->task_management = SIU_TASKMGMT_ABORT_TASK;
2238
2239 if ((pending_scb->flags & SCB_PACKETIZED) != 0) {
2240 /*
2241 * Mark the SCB has having an outstanding
2242 * task management function. Should the command
2243 * complete normally before the task management
2244 * function can be sent, the host will be notified
2245 * to abort our requeued SCB.
2246 */
2247 ahd_outb(ahd, SCB_TASK_MANAGEMENT,
2248 pending_scb->hscb->task_management);
2249 } else {
2250 /*
2251 * If non-packetized, set the MK_MESSAGE control
2252 * bit indicating that we desire to send a message.
2253 * We also set the disconnected flag since there is
2254 * no guarantee that our SCB control byte matches
2255 * the version on the card. We don't want the
2256 * sequencer to abort the command thinking an
2257 * unsolicited reselection occurred.
2258 */
2259 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
2260
2261 /*
2262 * The sequencer will never re-reference the
2263 * in-core SCB. To make sure we are notified
2264 * during reslection, set the MK_MESSAGE flag in
2265 * the card's copy of the SCB.
2266 */
2267 ahd_outb(ahd, SCB_CONTROL,
2268 ahd_inb(ahd, SCB_CONTROL)|MK_MESSAGE);
2269 }
2270
2271 /*
2272 * Clear out any entries in the QINFIFO first
2273 * so we are the next SCB for this target
2274 * to run.
2275 */
2276 ahd_search_qinfifo(ahd, cmd->device->id,
2277 cmd->device->channel + 'A', cmd->device->lun,
2278 SCB_LIST_NULL, ROLE_INITIATOR,
2279 CAM_REQUEUE_REQ, SEARCH_COMPLETE);
2280 ahd_qinfifo_requeue_tail(ahd, pending_scb);
2281 ahd_set_scbptr(ahd, saved_scbptr);
2282 ahd_print_path(ahd, pending_scb);
2283 printf("Device is disconnected, re-queuing SCB\n");
2284 wait = TRUE;
2285 } else {
2286 printf("%s:%d:%d:%d: Unable to deliver message\n",
2287 ahd_name(ahd), cmd->device->channel,
2288 cmd->device->id, cmd->device->lun);
2289 retval = FAILED;
2290 goto done;
2291 }
2292
2293no_cmd:
2294 /*
2295 * Our assumption is that if we don't have the command, no
2296 * recovery action was required, so we return success. Again,
2297 * the semantics of the mid-layer recovery engine are not
2298 * well defined, so this may change in time.
2299 */
2300 retval = SUCCESS;
2301done:
2302 if (paused)
2303 ahd_unpause(ahd);
2304 if (wait) {
2305 struct timer_list timer;
2306 int ret;
2307
2308 ahd->platform_data->flags |= AHD_SCB_UP_EH_SEM;
2309 spin_unlock_irq(&ahd->platform_data->spin_lock);
2310 init_timer(&timer);
2311 timer.data = (u_long)ahd;
2312 timer.expires = jiffies + (5 * HZ);
2313 timer.function = ahd_linux_sem_timeout;
2314 add_timer(&timer);
2315 printf("Recovery code sleeping\n");
2316 down(&ahd->platform_data->eh_sem);
2317 printf("Recovery code awake\n");
2318 ret = del_timer_sync(&timer);
2319 if (ret == 0) {
2320 printf("Timer Expired\n");
2321 retval = FAILED;
2322 }
2323 spin_lock_irq(&ahd->platform_data->spin_lock);
2324 }
2325 spin_unlock_irq(&ahd->platform_data->spin_lock);
2326 return (retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Hannes Reinecke73a25462005-07-22 16:44:04 +02002329static void ahd_linux_exit(void);
2330
2331static void ahd_linux_set_width(struct scsi_target *starget, int width)
2332{
2333 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2334 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2335 struct ahd_devinfo devinfo;
2336 unsigned long flags;
2337
2338 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2339 starget->channel + 'A', ROLE_INITIATOR);
2340 ahd_lock(ahd, &flags);
2341 ahd_set_width(ahd, &devinfo, width, AHD_TRANS_GOAL, FALSE);
2342 ahd_unlock(ahd, &flags);
2343}
2344
2345static void ahd_linux_set_period(struct scsi_target *starget, int period)
2346{
2347 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2348 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2349 struct ahd_tmode_tstate *tstate;
2350 struct ahd_initiator_tinfo *tinfo
2351 = ahd_fetch_transinfo(ahd,
2352 starget->channel + 'A',
2353 shost->this_id, starget->id, &tstate);
2354 struct ahd_devinfo devinfo;
2355 unsigned int ppr_options = tinfo->goal.ppr_options;
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002356 unsigned int dt;
Hannes Reinecke73a25462005-07-22 16:44:04 +02002357 unsigned long flags;
2358 unsigned long offset = tinfo->goal.offset;
2359
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002360#ifdef AHD_DEBUG
2361 if ((ahd_debug & AHD_SHOW_DV) != 0)
2362 printf("%s: set period to %d\n", ahd_name(ahd), period);
2363#endif
Hannes Reinecke73a25462005-07-22 16:44:04 +02002364 if (offset == 0)
2365 offset = MAX_OFFSET;
2366
2367 if (period < 8)
2368 period = 8;
2369 if (period < 10) {
2370 ppr_options |= MSG_EXT_PPR_DT_REQ;
2371 if (period == 8)
2372 ppr_options |= MSG_EXT_PPR_IU_REQ;
2373 }
2374
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002375 dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2376
Hannes Reinecke73a25462005-07-22 16:44:04 +02002377 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2378 starget->channel + 'A', ROLE_INITIATOR);
2379
2380 /* all PPR requests apart from QAS require wide transfers */
2381 if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
2382 if (spi_width(starget) == 0)
2383 ppr_options &= MSG_EXT_PPR_QAS_REQ;
2384 }
2385
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002386 ahd_find_syncrate(ahd, &period, &ppr_options,
2387 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2388
Hannes Reinecke73a25462005-07-22 16:44:04 +02002389 ahd_lock(ahd, &flags);
2390 ahd_set_syncrate(ahd, &devinfo, period, offset,
2391 ppr_options, AHD_TRANS_GOAL, FALSE);
2392 ahd_unlock(ahd, &flags);
2393}
2394
2395static void ahd_linux_set_offset(struct scsi_target *starget, int offset)
2396{
2397 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2398 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2399 struct ahd_tmode_tstate *tstate;
2400 struct ahd_initiator_tinfo *tinfo
2401 = ahd_fetch_transinfo(ahd,
2402 starget->channel + 'A',
2403 shost->this_id, starget->id, &tstate);
2404 struct ahd_devinfo devinfo;
2405 unsigned int ppr_options = 0;
2406 unsigned int period = 0;
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002407 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
Hannes Reinecke73a25462005-07-22 16:44:04 +02002408 unsigned long flags;
2409
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002410#ifdef AHD_DEBUG
2411 if ((ahd_debug & AHD_SHOW_DV) != 0)
2412 printf("%s: set offset to %d\n", ahd_name(ahd), offset);
2413#endif
2414
Hannes Reinecke73a25462005-07-22 16:44:04 +02002415 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2416 starget->channel + 'A', ROLE_INITIATOR);
2417 if (offset != 0) {
2418 period = tinfo->goal.period;
2419 ppr_options = tinfo->goal.ppr_options;
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002420 ahd_find_syncrate(ahd, &period, &ppr_options,
2421 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
Hannes Reinecke73a25462005-07-22 16:44:04 +02002422 }
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002423
Hannes Reinecke73a25462005-07-22 16:44:04 +02002424 ahd_lock(ahd, &flags);
2425 ahd_set_syncrate(ahd, &devinfo, period, offset, ppr_options,
2426 AHD_TRANS_GOAL, FALSE);
2427 ahd_unlock(ahd, &flags);
2428}
2429
2430static void ahd_linux_set_dt(struct scsi_target *starget, int dt)
2431{
2432 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2433 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2434 struct ahd_tmode_tstate *tstate;
2435 struct ahd_initiator_tinfo *tinfo
2436 = ahd_fetch_transinfo(ahd,
2437 starget->channel + 'A',
2438 shost->this_id, starget->id, &tstate);
2439 struct ahd_devinfo devinfo;
2440 unsigned int ppr_options = tinfo->goal.ppr_options
2441 & ~MSG_EXT_PPR_DT_REQ;
2442 unsigned int period = tinfo->goal.period;
Hannes Reinecke52b5cfb2005-08-04 09:16:59 +02002443 unsigned int width = tinfo->goal.width;
Hannes Reinecke73a25462005-07-22 16:44:04 +02002444 unsigned long flags;
2445
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002446#ifdef AHD_DEBUG
2447 if ((ahd_debug & AHD_SHOW_DV) != 0)
2448 printf("%s: %s DT\n", ahd_name(ahd),
2449 dt ? "enabling" : "disabling");
2450#endif
Hannes Reinecke73a25462005-07-22 16:44:04 +02002451 if (dt) {
2452 ppr_options |= MSG_EXT_PPR_DT_REQ;
Hannes Reinecke52b5cfb2005-08-04 09:16:59 +02002453 if (!width)
2454 ahd_linux_set_width(starget, 1);
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002455 } else {
2456 if (period <= 9)
2457 period = 10; /* If resetting DT, period must be >= 25ns */
2458 /* IU is invalid without DT set */
2459 ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2460 }
Hannes Reinecke73a25462005-07-22 16:44:04 +02002461 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2462 starget->channel + 'A', ROLE_INITIATOR);
2463 ahd_find_syncrate(ahd, &period, &ppr_options,
2464 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002465
Hannes Reinecke73a25462005-07-22 16:44:04 +02002466 ahd_lock(ahd, &flags);
2467 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2468 ppr_options, AHD_TRANS_GOAL, FALSE);
2469 ahd_unlock(ahd, &flags);
2470}
2471
2472static void ahd_linux_set_qas(struct scsi_target *starget, int qas)
2473{
2474 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2475 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2476 struct ahd_tmode_tstate *tstate;
2477 struct ahd_initiator_tinfo *tinfo
2478 = ahd_fetch_transinfo(ahd,
2479 starget->channel + 'A',
2480 shost->this_id, starget->id, &tstate);
2481 struct ahd_devinfo devinfo;
2482 unsigned int ppr_options = tinfo->goal.ppr_options
2483 & ~MSG_EXT_PPR_QAS_REQ;
2484 unsigned int period = tinfo->goal.period;
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002485 unsigned int dt;
Hannes Reinecke73a25462005-07-22 16:44:04 +02002486 unsigned long flags;
2487
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002488#ifdef AHD_DEBUG
2489 if ((ahd_debug & AHD_SHOW_DV) != 0)
2490 printf("%s: %s QAS\n", ahd_name(ahd),
2491 qas ? "enabling" : "disabling");
2492#endif
2493
2494 if (qas) {
2495 ppr_options |= MSG_EXT_PPR_QAS_REQ;
2496 }
2497
2498 dt = ppr_options & MSG_EXT_PPR_DT_REQ;
Hannes Reinecke73a25462005-07-22 16:44:04 +02002499
2500 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2501 starget->channel + 'A', ROLE_INITIATOR);
2502 ahd_find_syncrate(ahd, &period, &ppr_options,
2503 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002504
Hannes Reinecke73a25462005-07-22 16:44:04 +02002505 ahd_lock(ahd, &flags);
2506 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2507 ppr_options, AHD_TRANS_GOAL, FALSE);
2508 ahd_unlock(ahd, &flags);
2509}
2510
2511static void ahd_linux_set_iu(struct scsi_target *starget, int iu)
2512{
2513 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2514 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2515 struct ahd_tmode_tstate *tstate;
2516 struct ahd_initiator_tinfo *tinfo
2517 = ahd_fetch_transinfo(ahd,
2518 starget->channel + 'A',
2519 shost->this_id, starget->id, &tstate);
2520 struct ahd_devinfo devinfo;
2521 unsigned int ppr_options = tinfo->goal.ppr_options
2522 & ~MSG_EXT_PPR_IU_REQ;
2523 unsigned int period = tinfo->goal.period;
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002524 unsigned int dt;
Hannes Reinecke73a25462005-07-22 16:44:04 +02002525 unsigned long flags;
2526
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002527#ifdef AHD_DEBUG
2528 if ((ahd_debug & AHD_SHOW_DV) != 0)
2529 printf("%s: %s IU\n", ahd_name(ahd),
2530 iu ? "enabling" : "disabling");
2531#endif
2532
2533 if (iu) {
Hannes Reinecke73a25462005-07-22 16:44:04 +02002534 ppr_options |= MSG_EXT_PPR_IU_REQ;
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002535 ppr_options |= MSG_EXT_PPR_DT_REQ; /* IU requires DT */
2536 }
2537
2538 dt = ppr_options & MSG_EXT_PPR_DT_REQ;
Hannes Reinecke73a25462005-07-22 16:44:04 +02002539
2540 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2541 starget->channel + 'A', ROLE_INITIATOR);
2542 ahd_find_syncrate(ahd, &period, &ppr_options,
2543 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002544
Hannes Reinecke73a25462005-07-22 16:44:04 +02002545 ahd_lock(ahd, &flags);
2546 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2547 ppr_options, AHD_TRANS_GOAL, FALSE);
2548 ahd_unlock(ahd, &flags);
2549}
2550
2551static void ahd_linux_set_rd_strm(struct scsi_target *starget, int rdstrm)
2552{
2553 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2554 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2555 struct ahd_tmode_tstate *tstate;
2556 struct ahd_initiator_tinfo *tinfo
2557 = ahd_fetch_transinfo(ahd,
2558 starget->channel + 'A',
2559 shost->this_id, starget->id, &tstate);
2560 struct ahd_devinfo devinfo;
2561 unsigned int ppr_options = tinfo->goal.ppr_options
2562 & ~MSG_EXT_PPR_RD_STRM;
2563 unsigned int period = tinfo->goal.period;
2564 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2565 unsigned long flags;
2566
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002567#ifdef AHD_DEBUG
2568 if ((ahd_debug & AHD_SHOW_DV) != 0)
2569 printf("%s: %s Read Streaming\n", ahd_name(ahd),
2570 rdstrm ? "enabling" : "disabling");
2571#endif
2572
Hannes Reinecke73a25462005-07-22 16:44:04 +02002573 if (rdstrm)
2574 ppr_options |= MSG_EXT_PPR_RD_STRM;
2575
2576 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2577 starget->channel + 'A', ROLE_INITIATOR);
2578 ahd_find_syncrate(ahd, &period, &ppr_options,
2579 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002580
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002581 ahd_lock(ahd, &flags);
2582 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2583 ppr_options, AHD_TRANS_GOAL, FALSE);
2584 ahd_unlock(ahd, &flags);
2585}
2586
2587static void ahd_linux_set_wr_flow(struct scsi_target *starget, int wrflow)
2588{
2589 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2590 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2591 struct ahd_tmode_tstate *tstate;
2592 struct ahd_initiator_tinfo *tinfo
2593 = ahd_fetch_transinfo(ahd,
2594 starget->channel + 'A',
2595 shost->this_id, starget->id, &tstate);
2596 struct ahd_devinfo devinfo;
2597 unsigned int ppr_options = tinfo->goal.ppr_options
2598 & ~MSG_EXT_PPR_WR_FLOW;
2599 unsigned int period = tinfo->goal.period;
2600 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2601 unsigned long flags;
2602
2603#ifdef AHD_DEBUG
2604 if ((ahd_debug & AHD_SHOW_DV) != 0)
2605 printf("%s: %s Write Flow Control\n", ahd_name(ahd),
2606 wrflow ? "enabling" : "disabling");
2607#endif
2608
2609 if (wrflow)
2610 ppr_options |= MSG_EXT_PPR_WR_FLOW;
2611
2612 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2613 starget->channel + 'A', ROLE_INITIATOR);
2614 ahd_find_syncrate(ahd, &period, &ppr_options,
2615 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2616
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002617 ahd_lock(ahd, &flags);
2618 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2619 ppr_options, AHD_TRANS_GOAL, FALSE);
2620 ahd_unlock(ahd, &flags);
2621}
2622
2623static void ahd_linux_set_rti(struct scsi_target *starget, int rti)
2624{
2625 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2626 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2627 struct ahd_tmode_tstate *tstate;
2628 struct ahd_initiator_tinfo *tinfo
2629 = ahd_fetch_transinfo(ahd,
2630 starget->channel + 'A',
2631 shost->this_id, starget->id, &tstate);
2632 struct ahd_devinfo devinfo;
2633 unsigned int ppr_options = tinfo->goal.ppr_options
2634 & ~MSG_EXT_PPR_RTI;
2635 unsigned int period = tinfo->goal.period;
2636 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2637 unsigned long flags;
2638
2639 if ((ahd->features & AHD_RTI) == 0) {
2640#ifdef AHD_DEBUG
2641 if ((ahd_debug & AHD_SHOW_DV) != 0)
2642 printf("%s: RTI not available\n", ahd_name(ahd));
2643#endif
2644 return;
2645 }
2646
2647#ifdef AHD_DEBUG
2648 if ((ahd_debug & AHD_SHOW_DV) != 0)
2649 printf("%s: %s RTI\n", ahd_name(ahd),
2650 rti ? "enabling" : "disabling");
2651#endif
2652
2653 if (rti)
2654 ppr_options |= MSG_EXT_PPR_RTI;
2655
2656 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2657 starget->channel + 'A', ROLE_INITIATOR);
2658 ahd_find_syncrate(ahd, &period, &ppr_options,
2659 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2660
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002661 ahd_lock(ahd, &flags);
2662 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2663 ppr_options, AHD_TRANS_GOAL, FALSE);
2664 ahd_unlock(ahd, &flags);
2665}
2666
2667static void ahd_linux_set_pcomp_en(struct scsi_target *starget, int pcomp)
2668{
2669 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2670 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2671 struct ahd_tmode_tstate *tstate;
2672 struct ahd_initiator_tinfo *tinfo
2673 = ahd_fetch_transinfo(ahd,
2674 starget->channel + 'A',
2675 shost->this_id, starget->id, &tstate);
2676 struct ahd_devinfo devinfo;
2677 unsigned int ppr_options = tinfo->goal.ppr_options
2678 & ~MSG_EXT_PPR_PCOMP_EN;
2679 unsigned int period = tinfo->goal.period;
2680 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2681 unsigned long flags;
2682
2683#ifdef AHD_DEBUG
2684 if ((ahd_debug & AHD_SHOW_DV) != 0)
2685 printf("%s: %s Precompensation\n", ahd_name(ahd),
2686 pcomp ? "Enable" : "Disable");
2687#endif
2688
2689 if (pcomp)
2690 ppr_options |= MSG_EXT_PPR_PCOMP_EN;
2691
2692 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2693 starget->channel + 'A', ROLE_INITIATOR);
2694 ahd_find_syncrate(ahd, &period, &ppr_options,
2695 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2696
Hannes Reinecke73a25462005-07-22 16:44:04 +02002697 ahd_lock(ahd, &flags);
2698 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2699 ppr_options, AHD_TRANS_GOAL, FALSE);
2700 ahd_unlock(ahd, &flags);
2701}
2702
James Bottomley88ff29a2005-08-03 15:59:04 -05002703static void ahd_linux_set_hold_mcs(struct scsi_target *starget, int hold)
2704{
2705 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2706 struct ahd_softc *ahd = *((struct ahd_softc **)shost->hostdata);
2707 struct ahd_tmode_tstate *tstate;
2708 struct ahd_initiator_tinfo *tinfo
2709 = ahd_fetch_transinfo(ahd,
2710 starget->channel + 'A',
2711 shost->this_id, starget->id, &tstate);
2712 struct ahd_devinfo devinfo;
2713 unsigned int ppr_options = tinfo->goal.ppr_options
2714 & ~MSG_EXT_PPR_HOLD_MCS;
2715 unsigned int period = tinfo->goal.period;
2716 unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ;
2717 unsigned long flags;
2718
2719 if (hold)
2720 ppr_options |= MSG_EXT_PPR_HOLD_MCS;
2721
2722 ahd_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2723 starget->channel + 'A', ROLE_INITIATOR);
2724 ahd_find_syncrate(ahd, &period, &ppr_options,
2725 dt ? AHD_SYNCRATE_MAX : AHD_SYNCRATE_ULTRA2);
2726
2727 ahd_lock(ahd, &flags);
2728 ahd_set_syncrate(ahd, &devinfo, period, tinfo->goal.offset,
2729 ppr_options, AHD_TRANS_GOAL, FALSE);
2730 ahd_unlock(ahd, &flags);
2731}
2732
2733
2734
Hannes Reinecke73a25462005-07-22 16:44:04 +02002735static struct spi_function_template ahd_linux_transport_functions = {
2736 .set_offset = ahd_linux_set_offset,
2737 .show_offset = 1,
2738 .set_period = ahd_linux_set_period,
2739 .show_period = 1,
2740 .set_width = ahd_linux_set_width,
2741 .show_width = 1,
2742 .set_dt = ahd_linux_set_dt,
2743 .show_dt = 1,
2744 .set_iu = ahd_linux_set_iu,
2745 .show_iu = 1,
2746 .set_qas = ahd_linux_set_qas,
2747 .show_qas = 1,
2748 .set_rd_strm = ahd_linux_set_rd_strm,
2749 .show_rd_strm = 1,
Hannes Reineckea4b53a12005-08-01 09:52:56 +02002750 .set_wr_flow = ahd_linux_set_wr_flow,
2751 .show_wr_flow = 1,
2752 .set_rti = ahd_linux_set_rti,
2753 .show_rti = 1,
2754 .set_pcomp_en = ahd_linux_set_pcomp_en,
2755 .show_pcomp_en = 1,
James Bottomley88ff29a2005-08-03 15:59:04 -05002756 .set_hold_mcs = ahd_linux_set_hold_mcs,
2757 .show_hold_mcs = 1,
Hannes Reinecke73a25462005-07-22 16:44:04 +02002758};
2759
2760
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762static int __init
2763ahd_linux_init(void)
2764{
Hannes Reinecke73a25462005-07-22 16:44:04 +02002765 ahd_linux_transport_template = spi_attach_transport(&ahd_linux_transport_functions);
2766 if (!ahd_linux_transport_template)
2767 return -ENODEV;
2768 scsi_transport_reserve_target(ahd_linux_transport_template,
2769 sizeof(struct ahd_linux_target));
2770 scsi_transport_reserve_device(ahd_linux_transport_template,
2771 sizeof(struct ahd_linux_device));
2772 if (ahd_linux_detect(&aic79xx_driver_template) > 0)
2773 return 0;
2774 spi_release_transport(ahd_linux_transport_template);
2775 ahd_linux_exit();
2776 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
2779static void __exit
2780ahd_linux_exit(void)
2781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 ahd_linux_pci_exit();
Hannes Reinecke73a25462005-07-22 16:44:04 +02002783 spi_release_transport(ahd_linux_transport_template);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786module_init(ahd_linux_init);
2787module_exit(ahd_linux_exit);