blob: ff2a212c6e9cc285f0fada8dd2db1bbdac9c40f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AIC7xxx device driver for Linux.
3 *
4 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.c#235 $
5 *
6 * Copyright (c) 1994 John Aycock
7 * The University of Calgary Department of Computer Science.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * Sources include the Adaptec 1740 driver (aha1740.c), the Ultrastor 24F
24 * driver (ultrastor.c), various Linux kernel source, the Adaptec EISA
25 * config file (!adp7771.cfg), the Adaptec AHA-2740A Series User's Guide,
26 * the Linux Kernel Hacker's Guide, Writing a SCSI Device Driver for Linux,
27 * the Adaptec 1542 driver (aha1542.c), the Adaptec EISA overlay file
28 * (adp7770.ovl), the Adaptec AHA-2740 Series Technical Reference Manual,
29 * the Adaptec AIC-7770 Data Book, the ANSI SCSI specification, the
30 * ANSI SCSI-2 specification (draft 10c), ...
31 *
32 * --------------------------------------------------------------------------
33 *
34 * Modifications by Daniel M. Eischen (deischen@iworks.InterWorks.org):
35 *
36 * Substantially modified to include support for wide and twin bus
37 * adapters, DMAing of SCBs, tagged queueing, IRQ sharing, bug fixes,
38 * SCB paging, and other rework of the code.
39 *
40 * --------------------------------------------------------------------------
41 * Copyright (c) 1994-2000 Justin T. Gibbs.
42 * Copyright (c) 2000-2001 Adaptec Inc.
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions, and the following disclaimer,
50 * without modification.
51 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
52 * substantially similar to the "NO WARRANTY" disclaimer below
53 * ("Disclaimer") and any redistribution must be conditioned upon
54 * including a substantially similar Disclaimer requirement for further
55 * binary redistribution.
56 * 3. Neither the names of the above-listed copyright holders nor the names
57 * of any contributors may be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 *
60 * Alternatively, this software may be distributed under the terms of the
61 * GNU General Public License ("GPL") version 2 as published by the Free
62 * Software Foundation.
63 *
64 * NO WARRANTY
65 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
66 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
67 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
68 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
69 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
73 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
74 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
75 * POSSIBILITY OF SUCH DAMAGES.
76 *
77 *---------------------------------------------------------------------------
78 *
79 * Thanks also go to (in alphabetical order) the following:
80 *
81 * Rory Bolt - Sequencer bug fixes
82 * Jay Estabrook - Initial DEC Alpha support
83 * Doug Ledford - Much needed abort/reset bug fixes
84 * Kai Makisara - DMAing of SCBs
85 *
86 * A Boot time option was also added for not resetting the scsi bus.
87 *
88 * Form: aic7xxx=extended
89 * aic7xxx=no_reset
90 * aic7xxx=verbose
91 *
92 * Daniel M. Eischen, deischen@iworks.InterWorks.org, 1/23/97
93 *
94 * Id: aic7xxx.c,v 4.1 1997/06/12 08:23:42 deang Exp
95 */
96
97/*
98 * Further driver modifications made by Doug Ledford <dledford@redhat.com>
99 *
100 * Copyright (c) 1997-1999 Doug Ledford
101 *
102 * These changes are released under the same licensing terms as the FreeBSD
103 * driver written by Justin Gibbs. Please see his Copyright notice above
104 * for the exact terms and conditions covering my changes as well as the
105 * warranty statement.
106 *
107 * Modifications made to the aic7xxx.c,v 4.1 driver from Dan Eischen include
108 * but are not limited to:
109 *
110 * 1: Import of the latest FreeBSD sequencer code for this driver
111 * 2: Modification of kernel code to accommodate different sequencer semantics
112 * 3: Extensive changes throughout kernel portion of driver to improve
113 * abort/reset processing and error hanndling
114 * 4: Other work contributed by various people on the Internet
115 * 5: Changes to printk information and verbosity selection code
116 * 6: General reliability related changes, especially in IRQ management
117 * 7: Modifications to the default probe/attach order for supported cards
118 * 8: SMP friendliness has been improved
119 *
120 */
121
122#include "aic7xxx_osm.h"
123#include "aic7xxx_inline.h"
124#include <scsi/scsicam.h>
92d161c2005-04-17 16:59:33 -0500125#include <scsi/scsi_transport.h>
126#include <scsi/scsi_transport_spi.h>
127
128static struct scsi_transport_template *ahc_linux_transport_template = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*
131 * Include aiclib.c as part of our
132 * "module dependencies are hard" work around.
133 */
134#include "aiclib.c"
135
136#include <linux/init.h> /* __setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#include <linux/mm.h> /* For fetching system memory size */
138#include <linux/blkdev.h> /* For block_size() */
139#include <linux/delay.h> /* For ssleep/msleep */
140
141/*
142 * Lock protecting manipulation of the ahc softc list.
143 */
144spinlock_t ahc_list_spinlock;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * Set this to the delay in seconds after SCSI bus reset.
148 * Note, we honor this only for the initial bus reset.
149 * The scsi error recovery code performs its own bus settle
150 * delay handling for error recovery actions.
151 */
152#ifdef CONFIG_AIC7XXX_RESET_DELAY_MS
153#define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS
154#else
155#define AIC7XXX_RESET_DELAY 5000
156#endif
157
158/*
159 * Control collection of SCSI transfer statistics for the /proc filesystem.
160 *
161 * NOTE: Do NOT enable this when running on kernels version 1.2.x and below.
162 * NOTE: This does affect performance since it has to maintain statistics.
163 */
164#ifdef CONFIG_AIC7XXX_PROC_STATS
165#define AIC7XXX_PROC_STATS
166#endif
167
168/*
169 * To change the default number of tagged transactions allowed per-device,
170 * add a line to the lilo.conf file like:
171 * append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
172 * which will result in the first four devices on the first two
173 * controllers being set to a tagged queue depth of 32.
174 *
175 * The tag_commands is an array of 16 to allow for wide and twin adapters.
176 * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
177 * for channel 1.
178 */
179typedef struct {
180 uint8_t tag_commands[16]; /* Allow for wide/twin adapters. */
181} adapter_tag_info_t;
182
183/*
184 * Modify this as you see fit for your system.
185 *
186 * 0 tagged queuing disabled
187 * 1 <= n <= 253 n == max tags ever dispatched.
188 *
189 * The driver will throttle the number of commands dispatched to a
190 * device if it returns queue full. For devices with a fixed maximum
191 * queue depth, the driver will eventually determine this depth and
192 * lock it in (a console message is printed to indicate that a lock
193 * has occurred). On some devices, queue full is returned for a temporary
194 * resource shortage. These devices will return queue full at varying
195 * depths. The driver will throttle back when the queue fulls occur and
196 * attempt to slowly increase the depth over time as the device recovers
197 * from the resource shortage.
198 *
199 * In this example, the first line will disable tagged queueing for all
200 * the devices on the first probed aic7xxx adapter.
201 *
202 * The second line enables tagged queueing with 4 commands/LUN for IDs
203 * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
204 * driver to attempt to use up to 64 tags for ID 1.
205 *
206 * The third line is the same as the first line.
207 *
208 * The fourth line disables tagged queueing for devices 0 and 3. It
209 * enables tagged queueing for the other IDs, with 16 commands/LUN
210 * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
211 * IDs 2, 5-7, and 9-15.
212 */
213
214/*
215 * NOTE: The below structure is for reference only, the actual structure
216 * to modify in order to change things is just below this comment block.
217adapter_tag_info_t aic7xxx_tag_info[] =
218{
219 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
220 {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
221 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
222 {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
223};
224*/
225
226#ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
227#define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
228#else
229#define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE
230#endif
231
232#define AIC7XXX_CONFIGED_TAG_COMMANDS { \
233 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
234 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
235 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
236 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
237 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
238 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
239 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
240 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE \
241}
242
243/*
244 * By default, use the number of commands specified by
245 * the users kernel configuration.
246 */
247static adapter_tag_info_t aic7xxx_tag_info[] =
248{
249 {AIC7XXX_CONFIGED_TAG_COMMANDS},
250 {AIC7XXX_CONFIGED_TAG_COMMANDS},
251 {AIC7XXX_CONFIGED_TAG_COMMANDS},
252 {AIC7XXX_CONFIGED_TAG_COMMANDS},
253 {AIC7XXX_CONFIGED_TAG_COMMANDS},
254 {AIC7XXX_CONFIGED_TAG_COMMANDS},
255 {AIC7XXX_CONFIGED_TAG_COMMANDS},
256 {AIC7XXX_CONFIGED_TAG_COMMANDS},
257 {AIC7XXX_CONFIGED_TAG_COMMANDS},
258 {AIC7XXX_CONFIGED_TAG_COMMANDS},
259 {AIC7XXX_CONFIGED_TAG_COMMANDS},
260 {AIC7XXX_CONFIGED_TAG_COMMANDS},
261 {AIC7XXX_CONFIGED_TAG_COMMANDS},
262 {AIC7XXX_CONFIGED_TAG_COMMANDS},
263 {AIC7XXX_CONFIGED_TAG_COMMANDS},
264 {AIC7XXX_CONFIGED_TAG_COMMANDS}
265};
266
267/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * There should be a specific return value for this in scsi.h, but
269 * it seems that most drivers ignore it.
270 */
271#define DID_UNDERFLOW DID_ERROR
272
273void
274ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
275{
276 printk("(scsi%d:%c:%d:%d): ",
277 ahc->platform_data->host->host_no,
278 scb != NULL ? SCB_GET_CHANNEL(ahc, scb) : 'X',
279 scb != NULL ? SCB_GET_TARGET(ahc, scb) : -1,
280 scb != NULL ? SCB_GET_LUN(scb) : -1);
281}
282
283/*
284 * XXX - these options apply unilaterally to _all_ 274x/284x/294x
285 * cards in the system. This should be fixed. Exceptions to this
286 * rule are noted in the comments.
287 */
288
289/*
290 * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This
291 * has no effect on any later resets that might occur due to things like
292 * SCSI bus timeouts.
293 */
294static uint32_t aic7xxx_no_reset;
295
296/*
297 * Certain PCI motherboards will scan PCI devices from highest to lowest,
298 * others scan from lowest to highest, and they tend to do all kinds of
299 * strange things when they come into contact with PCI bridge chips. The
300 * net result of all this is that the PCI card that is actually used to boot
301 * the machine is very hard to detect. Most motherboards go from lowest
302 * PCI slot number to highest, and the first SCSI controller found is the
303 * one you boot from. The only exceptions to this are when a controller
304 * has its BIOS disabled. So, we by default sort all of our SCSI controllers
305 * from lowest PCI slot number to highest PCI slot number. We also force
306 * all controllers with their BIOS disabled to the end of the list. This
307 * works on *almost* all computers. Where it doesn't work, we have this
308 * option. Setting this option to non-0 will reverse the order of the sort
309 * to highest first, then lowest, but will still leave cards with their BIOS
310 * disabled at the very end. That should fix everyone up unless there are
311 * really strange cirumstances.
312 */
313static uint32_t aic7xxx_reverse_scan;
314
315/*
316 * Should we force EXTENDED translation on a controller.
317 * 0 == Use whatever is in the SEEPROM or default to off
318 * 1 == Use whatever is in the SEEPROM or default to on
319 */
320static uint32_t aic7xxx_extended;
321
322/*
323 * PCI bus parity checking of the Adaptec controllers. This is somewhat
324 * dubious at best. To my knowledge, this option has never actually
325 * solved a PCI parity problem, but on certain machines with broken PCI
326 * chipset configurations where stray PCI transactions with bad parity are
327 * the norm rather than the exception, the error messages can be overwelming.
328 * It's included in the driver for completeness.
329 * 0 = Shut off PCI parity check
330 * non-0 = reverse polarity pci parity checking
331 */
332static uint32_t aic7xxx_pci_parity = ~0;
333
334/*
335 * Certain newer motherboards have put new PCI based devices into the
336 * IO spaces that used to typically be occupied by VLB or EISA cards.
337 * This overlap can cause these newer motherboards to lock up when scanned
338 * for older EISA and VLB devices. Setting this option to non-0 will
339 * cause the driver to skip scanning for any VLB or EISA controllers and
340 * only support the PCI controllers. NOTE: this means that if the kernel
341 * os compiled with PCI support disabled, then setting this to non-0
342 * would result in never finding any devices :)
343 */
344#ifndef CONFIG_AIC7XXX_PROBE_EISA_VL
345uint32_t aic7xxx_probe_eisa_vl;
346#else
347uint32_t aic7xxx_probe_eisa_vl = ~0;
348#endif
349
350/*
351 * There are lots of broken chipsets in the world. Some of them will
352 * violate the PCI spec when we issue byte sized memory writes to our
353 * controller. I/O mapped register access, if allowed by the given
354 * platform, will work in almost all cases.
355 */
356uint32_t aic7xxx_allow_memio = ~0;
357
358/*
359 * aic7xxx_detect() has been run, so register all device arrivals
360 * immediately with the system rather than deferring to the sorted
361 * attachment performed by aic7xxx_detect().
362 */
363int aic7xxx_detect_complete;
364
365/*
366 * So that we can set how long each device is given as a selection timeout.
367 * The table of values goes like this:
368 * 0 - 256ms
369 * 1 - 128ms
370 * 2 - 64ms
371 * 3 - 32ms
372 * We default to 256ms because some older devices need a longer time
373 * to respond to initial selection.
374 */
375static uint32_t aic7xxx_seltime;
376
377/*
378 * Certain devices do not perform any aging on commands. Should the
379 * device be saturated by commands in one portion of the disk, it is
380 * possible for transactions on far away sectors to never be serviced.
381 * To handle these devices, we can periodically send an ordered tag to
382 * force all outstanding transactions to be serviced prior to a new
383 * transaction.
384 */
385uint32_t aic7xxx_periodic_otag;
386
387/*
388 * Module information and settable options.
389 */
390static char *aic7xxx = NULL;
391
392MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
393MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
394MODULE_LICENSE("Dual BSD/GPL");
395MODULE_VERSION(AIC7XXX_DRIVER_VERSION);
396module_param(aic7xxx, charp, 0444);
397MODULE_PARM_DESC(aic7xxx,
398"period delimited, options string.\n"
399" verbose Enable verbose/diagnostic logging\n"
400" allow_memio Allow device registers to be memory mapped\n"
401" debug Bitmask of debug values to enable\n"
402" no_probe Toggle EISA/VLB controller probing\n"
403" probe_eisa_vl Toggle EISA/VLB controller probing\n"
404" no_reset Supress initial bus resets\n"
405" extended Enable extended geometry on all controllers\n"
406" periodic_otag Send an ordered tagged transaction\n"
407" periodically to prevent tag starvation.\n"
408" This may be required by some older disk\n"
409" drives or RAID arrays.\n"
410" reverse_scan Sort PCI devices highest Bus/Slot to lowest\n"
411" tag_info:<tag_str> Set per-target tag depth\n"
412" global_tag_depth:<int> Global tag depth for every target\n"
413" on every bus\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414" seltime:<int> Selection Timeout\n"
415" (0/256ms,1/128ms,2/64ms,3/32ms)\n"
416"\n"
417" Sample /etc/modprobe.conf line:\n"
418" Toggle EISA/VLB probing\n"
419" Set tag depth on Controller 1/Target 1 to 10 tags\n"
420" Shorten the selection timeout to 128ms\n"
421"\n"
422" options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n"
423);
424
425static void ahc_linux_handle_scsi_status(struct ahc_softc *,
426 struct ahc_linux_device *,
427 struct scb *);
428static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200429 struct scsi_cmnd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static void ahc_linux_sem_timeout(u_long arg);
431static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
432static void ahc_linux_release_simq(u_long arg);
433static void ahc_linux_dev_timed_unfreeze(u_long arg);
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200434static int ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
438 struct ahc_devinfo *devinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static void ahc_linux_device_queue_depth(struct ahc_softc *ahc,
440 struct ahc_linux_device *dev);
441static struct ahc_linux_target* ahc_linux_alloc_target(struct ahc_softc*,
442 u_int, u_int);
443static void ahc_linux_free_target(struct ahc_softc*,
444 struct ahc_linux_target*);
445static struct ahc_linux_device* ahc_linux_alloc_device(struct ahc_softc*,
446 struct ahc_linux_target*,
447 u_int);
448static void ahc_linux_free_device(struct ahc_softc*,
449 struct ahc_linux_device*);
James Bottomley e4e360c2005-05-16 16:39:38 -0500450static int ahc_linux_run_command(struct ahc_softc*,
451 struct ahc_linux_device *,
452 struct scsi_cmnd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static void ahc_linux_setup_tag_info_global(char *p);
454static aic_option_callback_t ahc_linux_setup_tag_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455static int aic7xxx_setup(char *s);
456static int ahc_linux_next_unit(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457static struct ahc_cmd *ahc_linux_run_complete_queue(struct ahc_softc *ahc);
458
459/********************************* Inlines ************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static __inline struct ahc_linux_device*
461 ahc_linux_get_device(struct ahc_softc *ahc, u_int channel,
462 u_int target, u_int lun, int alloc);
463static __inline void ahc_schedule_completeq(struct ahc_softc *ahc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
465
466static __inline int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
467 struct ahc_dma_seg *sg,
468 dma_addr_t addr, bus_size_t len);
469
470static __inline void
471ahc_schedule_completeq(struct ahc_softc *ahc)
472{
473 if ((ahc->platform_data->flags & AHC_RUN_CMPLT_Q_TIMER) == 0) {
474 ahc->platform_data->flags |= AHC_RUN_CMPLT_Q_TIMER;
475 ahc->platform_data->completeq_timer.expires = jiffies;
476 add_timer(&ahc->platform_data->completeq_timer);
477 }
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480static __inline struct ahc_linux_device*
481ahc_linux_get_device(struct ahc_softc *ahc, u_int channel, u_int target,
482 u_int lun, int alloc)
483{
484 struct ahc_linux_target *targ;
485 struct ahc_linux_device *dev;
486 u_int target_offset;
487
488 target_offset = target;
489 if (channel != 0)
490 target_offset += 8;
491 targ = ahc->platform_data->targets[target_offset];
492 if (targ == NULL) {
493 if (alloc != 0) {
494 targ = ahc_linux_alloc_target(ahc, channel, target);
495 if (targ == NULL)
496 return (NULL);
497 } else
498 return (NULL);
499 }
500 dev = targ->devices[lun];
501 if (dev == NULL && alloc != 0)
502 dev = ahc_linux_alloc_device(ahc, targ, lun);
503 return (dev);
504}
505
506#define AHC_LINUX_MAX_RETURNED_ERRORS 4
507static struct ahc_cmd *
508ahc_linux_run_complete_queue(struct ahc_softc *ahc)
509{
510 struct ahc_cmd *acmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int with_errors;
512
513 with_errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 while ((acmd = TAILQ_FIRST(&ahc->platform_data->completeq)) != NULL) {
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200515 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (with_errors > AHC_LINUX_MAX_RETURNED_ERRORS) {
518 /*
519 * Linux uses stack recursion to requeue
520 * commands that need to be retried. Avoid
521 * blowing out the stack by "spoon feeding"
522 * commands that completed with error back
523 * the operating system in case they are going
524 * to be retried. "ick"
525 */
526 ahc_schedule_completeq(ahc);
527 break;
528 }
529 TAILQ_REMOVE(&ahc->platform_data->completeq,
530 acmd, acmd_links.tqe);
531 cmd = &acmd_scsi_cmd(acmd);
532 cmd->host_scribble = NULL;
533 if (ahc_cmd_get_transaction_status(cmd) != DID_OK
534 || (cmd->result & 0xFF) != SCSI_STATUS_OK)
535 with_errors++;
536
537 cmd->scsi_done(cmd);
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return (acmd);
540}
541
542static __inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
544{
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200545 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 cmd = scb->io_ctx;
548 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
549 if (cmd->use_sg != 0) {
550 struct scatterlist *sg;
551
552 sg = (struct scatterlist *)cmd->request_buffer;
553 pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg,
be7db052005-04-17 15:26:13 -0500554 cmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 } else if (cmd->request_bufflen != 0) {
556 pci_unmap_single(ahc->dev_softc,
557 scb->platform_data->buf_busaddr,
558 cmd->request_bufflen,
be7db052005-04-17 15:26:13 -0500559 cmd->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561}
562
563static __inline int
564ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
565 struct ahc_dma_seg *sg, dma_addr_t addr, bus_size_t len)
566{
567 int consumed;
568
569 if ((scb->sg_count + 1) > AHC_NSEG)
570 panic("Too few segs for dma mapping. "
571 "Increase AHC_NSEG\n");
572
573 consumed = 1;
574 sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
575 scb->platform_data->xfer_len += len;
576
577 if (sizeof(dma_addr_t) > 4
578 && (ahc->flags & AHC_39BIT_ADDRESSING) != 0)
579 len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK;
580
581 sg->len = ahc_htole32(len);
582 return (consumed);
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * Try to detect an Adaptec 7XXX controller.
587 */
588static int
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200589ahc_linux_detect(struct scsi_host_template *template)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct ahc_softc *ahc;
592 int found = 0;
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * Sanity checking of Linux SCSI data structures so
596 * that some of our hacks^H^H^H^H^Hassumptions aren't
597 * violated.
598 */
599 if (offsetof(struct ahc_cmd_internal, end)
600 > offsetof(struct scsi_cmnd, host_scribble)) {
601 printf("ahc_linux_detect: SCSI data structures changed.\n");
602 printf("ahc_linux_detect: Unable to attach\n");
603 return (0);
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
606 * If we've been passed any parameters, process them now.
607 */
608 if (aic7xxx)
609 aic7xxx_setup(aic7xxx);
610
611 template->proc_name = "aic7xxx";
612
613 /*
614 * Initialize our softc list lock prior to
615 * probing for any adapters.
616 */
617 ahc_list_lockinit();
618
619 found = ahc_linux_pci_init();
620 if (!ahc_linux_eisa_init())
621 found++;
622
623 /*
624 * Register with the SCSI layer all
625 * controllers we've found.
626 */
627 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
628
629 if (ahc_linux_register_host(ahc, template) == 0)
630 found++;
631 }
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 aic7xxx_detect_complete++;
634
635 return (found);
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/*
639 * Return a string describing the driver.
640 */
641static const char *
642ahc_linux_info(struct Scsi_Host *host)
643{
644 static char buffer[512];
645 char ahc_info[256];
646 char *bp;
647 struct ahc_softc *ahc;
648
649 bp = &buffer[0];
650 ahc = *(struct ahc_softc **)host->hostdata;
651 memset(bp, 0, sizeof(buffer));
652 strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev ");
653 strcat(bp, AIC7XXX_DRIVER_VERSION);
654 strcat(bp, "\n");
655 strcat(bp, " <");
656 strcat(bp, ahc->description);
657 strcat(bp, ">\n");
658 strcat(bp, " ");
659 ahc_controller_info(ahc, ahc_info);
660 strcat(bp, ahc_info);
661 strcat(bp, "\n");
662
663 return (bp);
664}
665
666/*
667 * Queue an SCB to the controller.
668 */
669static int
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200670ahc_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct ahc_softc *ahc;
673 struct ahc_linux_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
676
677 /*
678 * Save the callback on completion function.
679 */
680 cmd->scsi_done = scsi_done;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * Close the race of a command that was in the process of
684 * being queued to us just as our simq was frozen. Let
685 * DV commands through so long as we are only frozen to
686 * perform DV.
687 */
James Bottomley e4e360c2005-05-16 16:39:38 -0500688 if (ahc->platform_data->qfrozen != 0)
689 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
692 cmd->device->lun, /*alloc*/TRUE);
James Bottomley e4e360c2005-05-16 16:39:38 -0500693 BUG_ON(dev == NULL);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 cmd->result = CAM_REQ_INPROG << 16;
James Bottomley e4e360c2005-05-16 16:39:38 -0500696
697 return ahc_linux_run_command(ahc, dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static int
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200701ahc_linux_slave_alloc(struct scsi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct ahc_softc *ahc;
704
705 ahc = *((struct ahc_softc **)device->host->hostdata);
706 if (bootverbose)
707 printf("%s: Slave Alloc %d\n", ahc_name(ahc), device->id);
708 return (0);
709}
710
711static int
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200712ahc_linux_slave_configure(struct scsi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct ahc_softc *ahc;
715 struct ahc_linux_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 ahc = *((struct ahc_softc **)device->host->hostdata);
718 if (bootverbose)
719 printf("%s: Slave Configure %d\n", ahc_name(ahc), device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * Since Linux has attached to the device, configure
722 * it so we don't free and allocate the device
723 * structure on every command.
724 */
725 dev = ahc_linux_get_device(ahc, device->channel,
726 device->id, device->lun,
727 /*alloc*/TRUE);
728 if (dev != NULL) {
729 dev->flags &= ~AHC_DEV_UNCONFIGURED;
730 dev->scsi_device = device;
731 ahc_linux_device_queue_depth(ahc, dev);
732 }
cb624022005-04-17 18:03:20 -0500733
734 /* Initial Domain Validation */
735 if (!spi_initial_dv(device->sdev_target))
736 spi_dv_device(device);
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return (0);
739}
740
741static void
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200742ahc_linux_slave_destroy(struct scsi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct ahc_softc *ahc;
745 struct ahc_linux_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 ahc = *((struct ahc_softc **)device->host->hostdata);
748 if (bootverbose)
749 printf("%s: Slave Destroy %d\n", ahc_name(ahc), device->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 dev = ahc_linux_get_device(ahc, device->channel,
751 device->id, device->lun,
752 /*alloc*/FALSE);
753 /*
754 * Filter out "silly" deletions of real devices by only
755 * deleting devices that have had slave_configure()
756 * called on them. All other devices that have not
757 * been configured will automatically be deleted by
758 * the refcounting process.
759 */
760 if (dev != NULL
761 && (dev->flags & AHC_DEV_SLAVE_CONFIGURED) != 0) {
762 dev->flags |= AHC_DEV_UNCONFIGURED;
James Bottomley e4e360c2005-05-16 16:39:38 -0500763 if (dev->active == 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0)
765 ahc_linux_free_device(ahc, dev);
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769#if defined(__i386__)
770/*
771 * Return the disk geometry for the given SCSI device.
772 */
773static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
775 sector_t capacity, int geom[])
776{
777 uint8_t *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int heads;
779 int sectors;
780 int cylinders;
781 int ret;
782 int extended;
783 struct ahc_softc *ahc;
784 u_int channel;
785
786 ahc = *((struct ahc_softc **)sdev->host->hostdata);
787 channel = sdev->channel;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 bh = scsi_bios_ptable(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (bh) {
791 ret = scsi_partsize(bh, capacity,
792 &geom[2], &geom[0], &geom[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 kfree(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (ret != -1)
795 return (ret);
796 }
797 heads = 64;
798 sectors = 32;
799 cylinders = aic_sector_div(capacity, heads, sectors);
800
801 if (aic7xxx_extended != 0)
802 extended = 1;
803 else if (channel == 0)
804 extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
805 else
806 extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
807 if (extended && cylinders >= 1024) {
808 heads = 255;
809 sectors = 63;
810 cylinders = aic_sector_div(capacity, heads, sectors);
811 }
812 geom[0] = heads;
813 geom[1] = sectors;
814 geom[2] = cylinders;
815 return (0);
816}
817#endif
818
819/*
820 * Abort the current SCSI command(s).
821 */
822static int
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200823ahc_linux_abort(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 int error;
826
827 error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
828 if (error != 0)
829 printf("aic7xxx_abort returns 0x%x\n", error);
830 return (error);
831}
832
833/*
834 * Attempt to send a target reset message to the device that timed out.
835 */
836static int
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200837ahc_linux_dev_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 int error;
840
841 error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
842 if (error != 0)
843 printf("aic7xxx_dev_reset returns 0x%x\n", error);
844 return (error);
845}
846
847/*
848 * Reset the SCSI bus.
849 */
850static int
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200851ahc_linux_bus_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct ahc_softc *ahc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int found;
855
856 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 found = ahc_reset_channel(ahc, cmd->device->channel + 'A',
858 /*initiate reset*/TRUE);
859 ahc_linux_run_complete_queue(ahc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (bootverbose)
862 printf("%s: SCSI bus reset delivered. "
863 "%d SCBs aborted.\n", ahc_name(ahc), found);
864
865 return SUCCESS;
866}
867
Christoph Hellwig 013791e2005-05-16 18:52:39 +0200868struct scsi_host_template aic7xxx_driver_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 .module = THIS_MODULE,
870 .name = "aic7xxx",
871 .proc_info = ahc_linux_proc_info,
872 .info = ahc_linux_info,
873 .queuecommand = ahc_linux_queue,
874 .eh_abort_handler = ahc_linux_abort,
875 .eh_device_reset_handler = ahc_linux_dev_reset,
876 .eh_bus_reset_handler = ahc_linux_bus_reset,
877#if defined(__i386__)
878 .bios_param = ahc_linux_biosparam,
879#endif
880 .can_queue = AHC_MAX_QUEUE,
881 .this_id = -1,
882 .cmd_per_lun = 2,
883 .use_clustering = ENABLE_CLUSTERING,
884 .slave_alloc = ahc_linux_slave_alloc,
885 .slave_configure = ahc_linux_slave_configure,
886 .slave_destroy = ahc_linux_slave_destroy,
887};
888
889/**************************** Tasklet Handler *********************************/
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891/******************************** Macros **************************************/
892#define BUILD_SCSIID(ahc, cmd) \
893 ((((cmd)->device->id << TID_SHIFT) & TID) \
894 | (((cmd)->device->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \
895 | (((cmd)->device->channel == 0) ? 0 : TWIN_CHNLB))
896
897/******************************** Bus DMA *************************************/
898int
899ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent,
900 bus_size_t alignment, bus_size_t boundary,
901 dma_addr_t lowaddr, dma_addr_t highaddr,
902 bus_dma_filter_t *filter, void *filterarg,
903 bus_size_t maxsize, int nsegments,
904 bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
905{
906 bus_dma_tag_t dmat;
907
908 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
909 if (dmat == NULL)
910 return (ENOMEM);
911
912 /*
913 * Linux is very simplistic about DMA memory. For now don't
914 * maintain all specification information. Once Linux supplies
915 * better facilities for doing these operations, or the
916 * needs of this particular driver change, we might need to do
917 * more here.
918 */
919 dmat->alignment = alignment;
920 dmat->boundary = boundary;
921 dmat->maxsize = maxsize;
922 *ret_tag = dmat;
923 return (0);
924}
925
926void
927ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
928{
929 free(dmat, M_DEVBUF);
930}
931
932int
933ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
934 int flags, bus_dmamap_t *mapp)
935{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *vaddr = pci_alloc_consistent(ahc->dev_softc,
Christoph Hellwig 7dfa0f22005-05-16 18:54:12 +0200937 dmat->maxsize, mapp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (*vaddr == NULL)
Christoph Hellwig 7dfa0f22005-05-16 18:54:12 +0200939 return ENOMEM;
940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943void
944ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
945 void* vaddr, bus_dmamap_t map)
946{
947 pci_free_consistent(ahc->dev_softc, dmat->maxsize,
Christoph Hellwig 7dfa0f22005-05-16 18:54:12 +0200948 vaddr, map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951int
952ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map,
953 void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
954 void *cb_arg, int flags)
955{
956 /*
957 * Assume for now that this will only be used during
958 * initialization and not for per-transaction buffer mapping.
959 */
960 bus_dma_segment_t stack_sg;
961
Christoph Hellwig 7dfa0f22005-05-16 18:54:12 +0200962 stack_sg.ds_addr = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 stack_sg.ds_len = dmat->maxsize;
964 cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
965 return (0);
966}
967
968void
969ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
970{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973int
974ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
975{
976 /* Nothing to do */
977 return (0);
978}
979
980/********************* Platform Dependent Functions ***************************/
981/*
982 * Compare "left hand" softc with "right hand" softc, returning:
983 * < 0 - lahc has a lower priority than rahc
984 * 0 - Softcs are equal
985 * > 0 - lahc has a higher priority than rahc
986 */
987int
988ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
989{
990 int value;
991 int rvalue;
992 int lvalue;
993
994 /*
995 * Under Linux, cards are ordered as follows:
996 * 1) VLB/EISA BIOS enabled devices sorted by BIOS address.
997 * 2) PCI devices with BIOS enabled sorted by bus/slot/func.
998 * 3) All remaining VLB/EISA devices sorted by ioport.
999 * 4) All remaining PCI devices sorted by bus/slot/func.
1000 */
1001 value = (lahc->flags & AHC_BIOS_ENABLED)
1002 - (rahc->flags & AHC_BIOS_ENABLED);
1003 if (value != 0)
1004 /* Controllers with BIOS enabled have a *higher* priority */
1005 return (value);
1006
1007 /*
1008 * Same BIOS setting, now sort based on bus type.
1009 * EISA and VL controllers sort together. EISA/VL
1010 * have higher priority than PCI.
1011 */
1012 rvalue = (rahc->chip & AHC_BUS_MASK);
1013 if (rvalue == AHC_VL)
1014 rvalue = AHC_EISA;
1015 lvalue = (lahc->chip & AHC_BUS_MASK);
1016 if (lvalue == AHC_VL)
1017 lvalue = AHC_EISA;
1018 value = rvalue - lvalue;
1019 if (value != 0)
1020 return (value);
1021
1022 /* Still equal. Sort by BIOS address, ioport, or bus/slot/func. */
1023 switch (rvalue) {
1024#ifdef CONFIG_PCI
1025 case AHC_PCI:
1026 {
1027 char primary_channel;
1028
1029 if (aic7xxx_reverse_scan != 0)
1030 value = ahc_get_pci_bus(lahc->dev_softc)
1031 - ahc_get_pci_bus(rahc->dev_softc);
1032 else
1033 value = ahc_get_pci_bus(rahc->dev_softc)
1034 - ahc_get_pci_bus(lahc->dev_softc);
1035 if (value != 0)
1036 break;
1037 if (aic7xxx_reverse_scan != 0)
1038 value = ahc_get_pci_slot(lahc->dev_softc)
1039 - ahc_get_pci_slot(rahc->dev_softc);
1040 else
1041 value = ahc_get_pci_slot(rahc->dev_softc)
1042 - ahc_get_pci_slot(lahc->dev_softc);
1043 if (value != 0)
1044 break;
1045 /*
1046 * On multi-function devices, the user can choose
1047 * to have function 1 probed before function 0.
1048 * Give whichever channel is the primary channel
1049 * the highest priority.
1050 */
1051 primary_channel = (lahc->flags & AHC_PRIMARY_CHANNEL) + 'A';
1052 value = -1;
1053 if (lahc->channel == primary_channel)
1054 value = 1;
1055 break;
1056 }
1057#endif
1058 case AHC_EISA:
1059 if ((rahc->flags & AHC_BIOS_ENABLED) != 0) {
1060 value = rahc->platform_data->bios_address
1061 - lahc->platform_data->bios_address;
1062 } else {
1063 value = rahc->bsh.ioport
1064 - lahc->bsh.ioport;
1065 }
1066 break;
1067 default:
1068 panic("ahc_softc_sort: invalid bus type");
1069 }
1070 return (value);
1071}
1072
1073static void
1074ahc_linux_setup_tag_info_global(char *p)
1075{
1076 int tags, i, j;
1077
1078 tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
1079 printf("Setting Global Tags= %d\n", tags);
1080
1081 for (i = 0; i < NUM_ELEMENTS(aic7xxx_tag_info); i++) {
1082 for (j = 0; j < AHC_NUM_TARGETS; j++) {
1083 aic7xxx_tag_info[i].tag_commands[j] = tags;
1084 }
1085 }
1086}
1087
1088static void
1089ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
1090{
1091
1092 if ((instance >= 0) && (targ >= 0)
1093 && (instance < NUM_ELEMENTS(aic7xxx_tag_info))
1094 && (targ < AHC_NUM_TARGETS)) {
1095 aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff;
1096 if (bootverbose)
1097 printf("tag_info[%d:%d] = %d\n", instance, targ, value);
1098 }
1099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/*
1102 * Handle Linux boot parameters. This routine allows for assigning a value
1103 * to a parameter with a ':' between the parameter and the value.
1104 * ie. aic7xxx=stpwlev:1,extended
1105 */
1106static int
1107aic7xxx_setup(char *s)
1108{
1109 int i, n;
1110 char *p;
1111 char *end;
1112
1113 static struct {
1114 const char *name;
1115 uint32_t *flag;
1116 } options[] = {
1117 { "extended", &aic7xxx_extended },
1118 { "no_reset", &aic7xxx_no_reset },
1119 { "verbose", &aic7xxx_verbose },
1120 { "allow_memio", &aic7xxx_allow_memio},
1121#ifdef AHC_DEBUG
1122 { "debug", &ahc_debug },
1123#endif
1124 { "reverse_scan", &aic7xxx_reverse_scan },
1125 { "no_probe", &aic7xxx_probe_eisa_vl },
1126 { "probe_eisa_vl", &aic7xxx_probe_eisa_vl },
1127 { "periodic_otag", &aic7xxx_periodic_otag },
1128 { "pci_parity", &aic7xxx_pci_parity },
1129 { "seltime", &aic7xxx_seltime },
1130 { "tag_info", NULL },
1131 { "global_tag_depth", NULL },
1132 { "dv", NULL }
1133 };
1134
1135 end = strchr(s, '\0');
1136
1137 /*
1138 * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
1139 * will never be 0 in this case.
1140 */
1141 n = 0;
1142
1143 while ((p = strsep(&s, ",.")) != NULL) {
1144 if (*p == '\0')
1145 continue;
1146 for (i = 0; i < NUM_ELEMENTS(options); i++) {
1147
1148 n = strlen(options[i].name);
1149 if (strncmp(options[i].name, p, n) == 0)
1150 break;
1151 }
1152 if (i == NUM_ELEMENTS(options))
1153 continue;
1154
1155 if (strncmp(p, "global_tag_depth", n) == 0) {
1156 ahc_linux_setup_tag_info_global(p + n);
1157 } else if (strncmp(p, "tag_info", n) == 0) {
1158 s = aic_parse_brace_option("tag_info", p + n, end,
1159 2, ahc_linux_setup_tag_info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 } else if (p[n] == ':') {
1161 *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
1162 } else if (strncmp(p, "verbose", n) == 0) {
1163 *(options[i].flag) = 1;
1164 } else {
1165 *(options[i].flag) ^= 0xFFFFFFFF;
1166 }
1167 }
1168 return 1;
1169}
1170
1171__setup("aic7xxx=", aic7xxx_setup);
1172
1173uint32_t aic7xxx_verbose;
1174
1175int
Christoph Hellwig 013791e2005-05-16 18:52:39 +02001176ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *template)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 char buf[80];
1179 struct Scsi_Host *host;
1180 char *new_name;
1181 u_long s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 template->name = ahc->description;
1184 host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
1185 if (host == NULL)
1186 return (ENOMEM);
1187
1188 *((struct ahc_softc **)host->hostdata) = ahc;
1189 ahc_lock(ahc, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 scsi_assign_lock(host, &ahc->platform_data->spin_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 ahc->platform_data->host = host;
1192 host->can_queue = AHC_MAX_QUEUE;
1193 host->cmd_per_lun = 2;
1194 /* XXX No way to communicate the ID for multiple channels */
1195 host->this_id = ahc->our_id;
1196 host->irq = ahc->platform_data->irq;
1197 host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
1198 host->max_lun = AHC_NUM_LUNS;
1199 host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
1200 host->sg_tablesize = AHC_NSEG;
1201 ahc_set_unit(ahc, ahc_linux_next_unit());
1202 sprintf(buf, "scsi%d", host->host_no);
1203 new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
1204 if (new_name != NULL) {
1205 strcpy(new_name, buf);
1206 ahc_set_name(ahc, new_name);
1207 }
1208 host->unique_id = ahc->unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 ahc_linux_initialize_scsi_bus(ahc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ahc_intr_enable(ahc, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 ahc_unlock(ahc, &s);
1212
92d161c2005-04-17 16:59:33 -05001213 host->transportt = ahc_linux_transport_template;
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 scsi_add_host(host, (ahc->dev_softc ? &ahc->dev_softc->dev : NULL)); /* XXX handle failure */
1216 scsi_scan_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return (0);
1218}
1219
1220uint64_t
1221ahc_linux_get_memsize(void)
1222{
1223 struct sysinfo si;
1224
1225 si_meminfo(&si);
1226 return ((uint64_t)si.totalram << PAGE_SHIFT);
1227}
1228
1229/*
1230 * Find the smallest available unit number to use
1231 * for a new device. We don't just use a static
1232 * count to handle the "repeated hot-(un)plug"
1233 * scenario.
1234 */
1235static int
1236ahc_linux_next_unit(void)
1237{
1238 struct ahc_softc *ahc;
1239 int unit;
1240
1241 unit = 0;
1242retry:
1243 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
1244 if (ahc->unit == unit) {
1245 unit++;
1246 goto retry;
1247 }
1248 }
1249 return (unit);
1250}
1251
1252/*
1253 * Place the SCSI bus into a known state by either resetting it,
1254 * or forcing transfer negotiations on the next command to any
1255 * target.
1256 */
1257void
1258ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
1259{
1260 int i;
1261 int numtarg;
1262
1263 i = 0;
1264 numtarg = 0;
1265
1266 if (aic7xxx_no_reset != 0)
1267 ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
1268
1269 if ((ahc->flags & AHC_RESET_BUS_A) != 0)
1270 ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
1271 else
1272 numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1273
1274 if ((ahc->features & AHC_TWIN) != 0) {
1275
1276 if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
1277 ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
1278 } else {
1279 if (numtarg == 0)
1280 i = 8;
1281 numtarg += 8;
1282 }
1283 }
1284
1285 /*
1286 * Force negotiation to async for all targets that
1287 * will not see an initial bus reset.
1288 */
1289 for (; i < numtarg; i++) {
1290 struct ahc_devinfo devinfo;
1291 struct ahc_initiator_tinfo *tinfo;
1292 struct ahc_tmode_tstate *tstate;
1293 u_int our_id;
1294 u_int target_id;
1295 char channel;
1296
1297 channel = 'A';
1298 our_id = ahc->our_id;
1299 target_id = i;
1300 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
1301 channel = 'B';
1302 our_id = ahc->our_id_b;
1303 target_id = i % 8;
1304 }
1305 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
1306 target_id, &tstate);
1307 ahc_compile_devinfo(&devinfo, our_id, target_id,
1308 CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
1309 ahc_update_neg_request(ahc, &devinfo, tstate,
1310 tinfo, AHC_NEG_ALWAYS);
1311 }
1312 /* Give the bus some time to recover */
1313 if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
1314 ahc_linux_freeze_simq(ahc);
1315 init_timer(&ahc->platform_data->reset_timer);
1316 ahc->platform_data->reset_timer.data = (u_long)ahc;
1317 ahc->platform_data->reset_timer.expires =
1318 jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000;
1319 ahc->platform_data->reset_timer.function =
1320 ahc_linux_release_simq;
1321 add_timer(&ahc->platform_data->reset_timer);
1322 }
1323}
1324
1325int
1326ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1327{
1328
1329 ahc->platform_data =
1330 malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
1331 if (ahc->platform_data == NULL)
1332 return (ENOMEM);
1333 memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
1334 TAILQ_INIT(&ahc->platform_data->completeq);
1335 TAILQ_INIT(&ahc->platform_data->device_runq);
1336 ahc->platform_data->irq = AHC_LINUX_NOIRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 ahc_lockinit(ahc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 init_timer(&ahc->platform_data->completeq_timer);
1339 ahc->platform_data->completeq_timer.data = (u_long)ahc;
1340 ahc->platform_data->completeq_timer.function =
1341 (ahc_linux_callback_t *)ahc_linux_thread_run_complete_queue;
1342 init_MUTEX_LOCKED(&ahc->platform_data->eh_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
1344 ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
1345 if (aic7xxx_pci_parity == 0)
1346 ahc->flags |= AHC_DISABLE_PCI_PERR;
1347
1348 return (0);
1349}
1350
1351void
1352ahc_platform_free(struct ahc_softc *ahc)
1353{
1354 struct ahc_linux_target *targ;
1355 struct ahc_linux_device *dev;
1356 int i, j;
1357
1358 if (ahc->platform_data != NULL) {
1359 del_timer_sync(&ahc->platform_data->completeq_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (ahc->platform_data->host != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 scsi_remove_host(ahc->platform_data->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 scsi_host_put(ahc->platform_data->host);
1363 }
1364
1365 /* destroy all of the device and target objects */
1366 for (i = 0; i < AHC_NUM_TARGETS; i++) {
1367 targ = ahc->platform_data->targets[i];
1368 if (targ != NULL) {
1369 /* Keep target around through the loop. */
1370 targ->refcount++;
1371 for (j = 0; j < AHC_NUM_LUNS; j++) {
1372
1373 if (targ->devices[j] == NULL)
1374 continue;
1375 dev = targ->devices[j];
1376 ahc_linux_free_device(ahc, dev);
1377 }
1378 /*
1379 * Forcibly free the target now that
1380 * all devices are gone.
1381 */
1382 ahc_linux_free_target(ahc, targ);
1383 }
1384 }
1385
1386 if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
1387 free_irq(ahc->platform_data->irq, ahc);
1388 if (ahc->tag == BUS_SPACE_PIO
1389 && ahc->bsh.ioport != 0)
1390 release_region(ahc->bsh.ioport, 256);
1391 if (ahc->tag == BUS_SPACE_MEMIO
1392 && ahc->bsh.maddr != NULL) {
1393 iounmap(ahc->bsh.maddr);
1394 release_mem_region(ahc->platform_data->mem_busaddr,
1395 0x1000);
1396 }
Christoph Hellwig dedd8312005-05-16 18:52:06 +02001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 free(ahc->platform_data, M_DEVBUF);
1399 }
1400}
1401
1402void
1403ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
1404{
1405 ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1406 SCB_GET_CHANNEL(ahc, scb),
1407 SCB_GET_LUN(scb), SCB_LIST_NULL,
1408 ROLE_UNKNOWN, CAM_REQUEUE_REQ);
1409}
1410
1411void
1412ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1413 ahc_queue_alg alg)
1414{
1415 struct ahc_linux_device *dev;
1416 int was_queuing;
1417 int now_queuing;
1418
1419 dev = ahc_linux_get_device(ahc, devinfo->channel - 'A',
1420 devinfo->target,
1421 devinfo->lun, /*alloc*/FALSE);
1422 if (dev == NULL)
1423 return;
1424 was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
1425 switch (alg) {
1426 default:
1427 case AHC_QUEUE_NONE:
1428 now_queuing = 0;
1429 break;
1430 case AHC_QUEUE_BASIC:
1431 now_queuing = AHC_DEV_Q_BASIC;
1432 break;
1433 case AHC_QUEUE_TAGGED:
1434 now_queuing = AHC_DEV_Q_TAGGED;
1435 break;
1436 }
1437 if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
1438 && (was_queuing != now_queuing)
1439 && (dev->active != 0)) {
1440 dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
1441 dev->qfrozen++;
1442 }
1443
1444 dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
1445 if (now_queuing) {
1446 u_int usertags;
1447
1448 usertags = ahc_linux_user_tagdepth(ahc, devinfo);
1449 if (!was_queuing) {
1450 /*
1451 * Start out agressively and allow our
1452 * dynamic queue depth algorithm to take
1453 * care of the rest.
1454 */
1455 dev->maxtags = usertags;
1456 dev->openings = dev->maxtags - dev->active;
1457 }
1458 if (dev->maxtags == 0) {
1459 /*
1460 * Queueing is disabled by the user.
1461 */
1462 dev->openings = 1;
1463 } else if (alg == AHC_QUEUE_TAGGED) {
1464 dev->flags |= AHC_DEV_Q_TAGGED;
1465 if (aic7xxx_periodic_otag != 0)
1466 dev->flags |= AHC_DEV_PERIODIC_OTAG;
1467 } else
1468 dev->flags |= AHC_DEV_Q_BASIC;
1469 } else {
1470 /* We can only have one opening. */
1471 dev->maxtags = 0;
1472 dev->openings = 1 - dev->active;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (dev->scsi_device != NULL) {
1475 switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
1476 case AHC_DEV_Q_BASIC:
1477 scsi_adjust_queue_depth(dev->scsi_device,
1478 MSG_SIMPLE_TASK,
1479 dev->openings + dev->active);
1480 break;
1481 case AHC_DEV_Q_TAGGED:
1482 scsi_adjust_queue_depth(dev->scsi_device,
1483 MSG_ORDERED_TASK,
1484 dev->openings + dev->active);
1485 break;
1486 default:
1487 /*
1488 * We allow the OS to queue 2 untagged transactions to
1489 * us at any time even though we can only execute them
1490 * serially on the controller/device. This should
1491 * remove some latency.
1492 */
1493 scsi_adjust_queue_depth(dev->scsi_device,
1494 /*NON-TAGGED*/0,
1495 /*queue depth*/2);
1496 break;
1497 }
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501int
1502ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
1503 int lun, u_int tag, role_t role, uint32_t status)
1504{
James Bottomley e4e360c2005-05-16 16:39:38 -05001505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
1508static void
1509ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc)
1510{
1511 u_long flags;
1512
1513 ahc_lock(ahc, &flags);
1514 del_timer(&ahc->platform_data->completeq_timer);
1515 ahc->platform_data->flags &= ~AHC_RUN_CMPLT_Q_TIMER;
1516 ahc_linux_run_complete_queue(ahc);
1517 ahc_unlock(ahc, &flags);
1518}
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520static u_int
1521ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1522{
1523 static int warned_user;
1524 u_int tags;
1525
1526 tags = 0;
1527 if ((ahc->user_discenable & devinfo->target_mask) != 0) {
1528 if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
1529 if (warned_user == 0) {
1530
1531 printf(KERN_WARNING
1532"aic7xxx: WARNING: Insufficient tag_info instances\n"
1533"aic7xxx: for installed controllers. Using defaults\n"
1534"aic7xxx: Please update the aic7xxx_tag_info array in\n"
1535"aic7xxx: the aic7xxx_osm..c source file.\n");
1536 warned_user++;
1537 }
1538 tags = AHC_MAX_QUEUE;
1539 } else {
1540 adapter_tag_info_t *tag_info;
1541
1542 tag_info = &aic7xxx_tag_info[ahc->unit];
1543 tags = tag_info->tag_commands[devinfo->target_offset];
1544 if (tags > AHC_MAX_QUEUE)
1545 tags = AHC_MAX_QUEUE;
1546 }
1547 }
1548 return (tags);
1549}
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551/*
1552 * Determines the queue depth for a given device.
1553 */
1554static void
1555ahc_linux_device_queue_depth(struct ahc_softc *ahc,
1556 struct ahc_linux_device *dev)
1557{
1558 struct ahc_devinfo devinfo;
1559 u_int tags;
1560
1561 ahc_compile_devinfo(&devinfo,
1562 dev->target->channel == 0
1563 ? ahc->our_id : ahc->our_id_b,
1564 dev->target->target, dev->lun,
1565 dev->target->channel == 0 ? 'A' : 'B',
1566 ROLE_INITIATOR);
1567 tags = ahc_linux_user_tagdepth(ahc, &devinfo);
1568 if (tags != 0
1569 && dev->scsi_device != NULL
1570 && dev->scsi_device->tagged_supported != 0) {
1571
1572 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
1573 ahc_print_devinfo(ahc, &devinfo);
1574 printf("Tagged Queuing enabled. Depth %d\n", tags);
1575 } else {
1576 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE);
1577 }
1578}
1579
James Bottomley e4e360c2005-05-16 16:39:38 -05001580static int
1581ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
1582 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct scb *scb;
1585 struct hardware_scb *hscb;
1586 struct ahc_initiator_tinfo *tinfo;
1587 struct ahc_tmode_tstate *tstate;
1588 uint16_t mask;
James Bottomley e4e360c2005-05-16 16:39:38 -05001589 struct scb_tailq *untagged_q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
James Bottomley e4e360c2005-05-16 16:39:38 -05001591 /*
1592 * Schedule us to run later. The only reason we are not
1593 * running is because the whole controller Q is frozen.
1594 */
1595 if (ahc->platform_data->qfrozen != 0)
1596 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
James Bottomley e4e360c2005-05-16 16:39:38 -05001598 /*
1599 * We only allow one untagged transaction
1600 * per target in the initiator role unless
1601 * we are storing a full busy target *lun*
1602 * table in SCB space.
1603 */
1604 if (!blk_rq_tagged(cmd->request)
1605 && (ahc->features & AHC_SCB_BTT) == 0) {
1606 int target_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
James Bottomley e4e360c2005-05-16 16:39:38 -05001608 target_offset = cmd->device->id + cmd->device->channel * 8;
1609 untagged_q = &(ahc->untagged_queues[target_offset]);
1610 if (!TAILQ_EMPTY(untagged_q))
1611 /* if we're already executing an untagged command
1612 * we're busy to another */
1613 return SCSI_MLQUEUE_DEVICE_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
James Bottomley e4e360c2005-05-16 16:39:38 -05001615
1616 /*
1617 * Get an scb to use.
1618 */
1619 if ((scb = ahc_get_scb(ahc)) == NULL) {
1620 ahc->flags |= AHC_RESOURCE_SHORTAGE;
1621 return SCSI_MLQUEUE_HOST_BUSY;
1622 }
1623
1624 scb->io_ctx = cmd;
1625 scb->platform_data->dev = dev;
1626 hscb = scb->hscb;
1627 cmd->host_scribble = (char *)scb;
1628
1629 /*
1630 * Fill out basics of the HSCB.
1631 */
1632 hscb->control = 0;
1633 hscb->scsiid = BUILD_SCSIID(ahc, cmd);
1634 hscb->lun = cmd->device->lun;
1635 mask = SCB_GET_TARGET_MASK(ahc, scb);
1636 tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
1637 SCB_GET_OUR_ID(scb),
1638 SCB_GET_TARGET(ahc, scb), &tstate);
1639 hscb->scsirate = tinfo->scsirate;
1640 hscb->scsioffset = tinfo->curr.offset;
1641 if ((tstate->ultraenb & mask) != 0)
1642 hscb->control |= ULTRAENB;
1643
1644 if ((ahc->user_discenable & mask) != 0)
1645 hscb->control |= DISCENB;
1646
1647 if ((tstate->auto_negotiate & mask) != 0) {
1648 scb->flags |= SCB_AUTO_NEGOTIATE;
1649 scb->hscb->control |= MK_MESSAGE;
1650 }
1651
1652 if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
James Bottomley e4e360c2005-05-16 16:39:38 -05001653 int msg_bytes;
1654 uint8_t tag_msgs[2];
1655
1656 msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
1657 if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
1658 hscb->control |= tag_msgs[0];
1659 if (tag_msgs[0] == MSG_ORDERED_TASK)
1660 dev->commands_since_idle_or_otag = 0;
Christoph Hellwig dedd8312005-05-16 18:52:06 +02001661 } else if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
1662 && (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
James Bottomley e4e360c2005-05-16 16:39:38 -05001663 hscb->control |= MSG_ORDERED_TASK;
1664 dev->commands_since_idle_or_otag = 0;
1665 } else {
1666 hscb->control |= MSG_SIMPLE_TASK;
1667 }
1668 }
1669
1670 hscb->cdb_len = cmd->cmd_len;
1671 if (hscb->cdb_len <= 12) {
1672 memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
1673 } else {
1674 memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
1675 scb->flags |= SCB_CDB32_PTR;
1676 }
1677
1678 scb->platform_data->xfer_len = 0;
1679 ahc_set_residual(scb, 0);
1680 ahc_set_sense_residual(scb, 0);
1681 scb->sg_count = 0;
1682 if (cmd->use_sg != 0) {
1683 struct ahc_dma_seg *sg;
1684 struct scatterlist *cur_seg;
1685 struct scatterlist *end_seg;
1686 int nseg;
1687
1688 cur_seg = (struct scatterlist *)cmd->request_buffer;
1689 nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
1690 cmd->sc_data_direction);
1691 end_seg = cur_seg + nseg;
1692 /* Copy the segments into the SG list. */
1693 sg = scb->sg_list;
1694 /*
1695 * The sg_count may be larger than nseg if
1696 * a transfer crosses a 32bit page.
1697 */
1698 while (cur_seg < end_seg) {
1699 dma_addr_t addr;
1700 bus_size_t len;
1701 int consumed;
1702
1703 addr = sg_dma_address(cur_seg);
1704 len = sg_dma_len(cur_seg);
1705 consumed = ahc_linux_map_seg(ahc, scb,
1706 sg, addr, len);
1707 sg += consumed;
1708 scb->sg_count += consumed;
1709 cur_seg++;
1710 }
1711 sg--;
1712 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1713
1714 /*
1715 * Reset the sg list pointer.
1716 */
1717 scb->hscb->sgptr =
1718 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
1719
1720 /*
1721 * Copy the first SG into the "current"
1722 * data pointer area.
1723 */
1724 scb->hscb->dataptr = scb->sg_list->addr;
1725 scb->hscb->datacnt = scb->sg_list->len;
1726 } else if (cmd->request_bufflen != 0) {
1727 struct ahc_dma_seg *sg;
1728 dma_addr_t addr;
1729
1730 sg = scb->sg_list;
1731 addr = pci_map_single(ahc->dev_softc,
1732 cmd->request_buffer,
1733 cmd->request_bufflen,
1734 cmd->sc_data_direction);
1735 scb->platform_data->buf_busaddr = addr;
1736 scb->sg_count = ahc_linux_map_seg(ahc, scb,
1737 sg, addr,
1738 cmd->request_bufflen);
1739 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1740
1741 /*
1742 * Reset the sg list pointer.
1743 */
1744 scb->hscb->sgptr =
1745 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
1746
1747 /*
1748 * Copy the first SG into the "current"
1749 * data pointer area.
1750 */
1751 scb->hscb->dataptr = sg->addr;
1752 scb->hscb->datacnt = sg->len;
1753 } else {
1754 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
1755 scb->hscb->dataptr = 0;
1756 scb->hscb->datacnt = 0;
1757 scb->sg_count = 0;
1758 }
1759
1760 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1761 dev->openings--;
1762 dev->active++;
1763 dev->commands_issued++;
1764 if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
1765 dev->commands_since_idle_or_otag++;
1766
1767 scb->flags |= SCB_ACTIVE;
1768 if (untagged_q) {
1769 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1770 scb->flags |= SCB_UNTAGGEDQ;
1771 }
1772 ahc_queue_scb(ahc, scb);
1773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
1776/*
1777 * SCSI controller interrupt handler.
1778 */
1779irqreturn_t
1780ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
1781{
1782 struct ahc_softc *ahc;
1783 u_long flags;
1784 int ours;
1785
1786 ahc = (struct ahc_softc *) dev_id;
1787 ahc_lock(ahc, &flags);
1788 ours = ahc_intr(ahc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 ahc_linux_run_complete_queue(ahc);
1790 ahc_unlock(ahc, &flags);
1791 return IRQ_RETVAL(ours);
1792}
1793
1794void
1795ahc_platform_flushwork(struct ahc_softc *ahc)
1796{
1797
1798 while (ahc_linux_run_complete_queue(ahc) != NULL)
1799 ;
1800}
1801
1802static struct ahc_linux_target*
1803ahc_linux_alloc_target(struct ahc_softc *ahc, u_int channel, u_int target)
1804{
1805 struct ahc_linux_target *targ;
1806 u_int target_offset;
1807
1808 target_offset = target;
1809 if (channel != 0)
1810 target_offset += 8;
1811
1812 targ = malloc(sizeof(*targ), M_DEVBUG, M_NOWAIT);
1813 if (targ == NULL)
1814 return (NULL);
1815 memset(targ, 0, sizeof(*targ));
1816 targ->channel = channel;
1817 targ->target = target;
1818 targ->ahc = ahc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 ahc->platform_data->targets[target_offset] = targ;
1820 return (targ);
1821}
1822
1823static void
1824ahc_linux_free_target(struct ahc_softc *ahc, struct ahc_linux_target *targ)
1825{
1826 struct ahc_devinfo devinfo;
1827 struct ahc_initiator_tinfo *tinfo;
1828 struct ahc_tmode_tstate *tstate;
1829 u_int our_id;
1830 u_int target_offset;
1831 char channel;
1832
1833 /*
1834 * Force a negotiation to async/narrow on any
1835 * future command to this device unless a bus
1836 * reset occurs between now and that command.
1837 */
1838 channel = 'A' + targ->channel;
1839 our_id = ahc->our_id;
1840 target_offset = targ->target;
1841 if (targ->channel != 0) {
1842 target_offset += 8;
1843 our_id = ahc->our_id_b;
1844 }
1845 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
1846 targ->target, &tstate);
1847 ahc_compile_devinfo(&devinfo, our_id, targ->target, CAM_LUN_WILDCARD,
1848 channel, ROLE_INITIATOR);
1849 ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
1850 AHC_TRANS_GOAL, /*paused*/FALSE);
1851 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
1852 AHC_TRANS_GOAL, /*paused*/FALSE);
1853 ahc_update_neg_request(ahc, &devinfo, tstate, tinfo, AHC_NEG_ALWAYS);
1854 ahc->platform_data->targets[target_offset] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 free(targ, M_DEVBUF);
1856}
1857
1858static struct ahc_linux_device*
1859ahc_linux_alloc_device(struct ahc_softc *ahc,
1860 struct ahc_linux_target *targ, u_int lun)
1861{
1862 struct ahc_linux_device *dev;
1863
1864 dev = malloc(sizeof(*dev), M_DEVBUG, M_NOWAIT);
1865 if (dev == NULL)
1866 return (NULL);
1867 memset(dev, 0, sizeof(*dev));
1868 init_timer(&dev->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 dev->flags = AHC_DEV_UNCONFIGURED;
1870 dev->lun = lun;
1871 dev->target = targ;
1872
1873 /*
1874 * We start out life using untagged
1875 * transactions of which we allow one.
1876 */
1877 dev->openings = 1;
1878
1879 /*
1880 * Set maxtags to 0. This will be changed if we
1881 * later determine that we are dealing with
1882 * a tagged queuing capable device.
1883 */
1884 dev->maxtags = 0;
1885
1886 targ->refcount++;
1887 targ->devices[lun] = dev;
1888 return (dev);
1889}
1890
1891static void
1892__ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
1893{
1894 struct ahc_linux_target *targ;
1895
1896 targ = dev->target;
1897 targ->devices[dev->lun] = NULL;
1898 free(dev, M_DEVBUF);
1899 targ->refcount--;
cb624022005-04-17 18:03:20 -05001900 if (targ->refcount == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 ahc_linux_free_target(ahc, targ);
1902}
1903
1904static void
1905ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev)
1906{
1907 del_timer_sync(&dev->timer);
1908 __ahc_linux_free_device(ahc, dev);
1909}
1910
1911void
1912ahc_send_async(struct ahc_softc *ahc, char channel,
1913 u_int target, u_int lun, ac_code code, void *arg)
1914{
1915 switch (code) {
1916 case AC_TRANSFER_NEG:
1917 {
1918 char buf[80];
1919 struct ahc_linux_target *targ;
1920 struct info_str info;
1921 struct ahc_initiator_tinfo *tinfo;
1922 struct ahc_tmode_tstate *tstate;
1923 int target_offset;
1924
1925 info.buffer = buf;
1926 info.length = sizeof(buf);
1927 info.offset = 0;
1928 info.pos = 0;
1929 tinfo = ahc_fetch_transinfo(ahc, channel,
1930 channel == 'A' ? ahc->our_id
1931 : ahc->our_id_b,
1932 target, &tstate);
1933
1934 /*
1935 * Don't bother reporting results while
1936 * negotiations are still pending.
1937 */
1938 if (tinfo->curr.period != tinfo->goal.period
1939 || tinfo->curr.width != tinfo->goal.width
1940 || tinfo->curr.offset != tinfo->goal.offset
1941 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
1942 if (bootverbose == 0)
1943 break;
1944
1945 /*
1946 * Don't bother reporting results that
1947 * are identical to those last reported.
1948 */
1949 target_offset = target;
1950 if (channel == 'B')
1951 target_offset += 8;
1952 targ = ahc->platform_data->targets[target_offset];
1953 if (targ == NULL)
1954 break;
1955 if (tinfo->curr.period == targ->last_tinfo.period
1956 && tinfo->curr.width == targ->last_tinfo.width
1957 && tinfo->curr.offset == targ->last_tinfo.offset
1958 && tinfo->curr.ppr_options == targ->last_tinfo.ppr_options)
1959 if (bootverbose == 0)
1960 break;
1961
1962 targ->last_tinfo.period = tinfo->curr.period;
1963 targ->last_tinfo.width = tinfo->curr.width;
1964 targ->last_tinfo.offset = tinfo->curr.offset;
1965 targ->last_tinfo.ppr_options = tinfo->curr.ppr_options;
1966
1967 printf("(%s:%c:", ahc_name(ahc), channel);
1968 if (target == CAM_TARGET_WILDCARD)
1969 printf("*): ");
1970 else
1971 printf("%d): ", target);
1972 ahc_format_transinfo(&info, &tinfo->curr);
1973 if (info.pos < info.length)
1974 *info.buffer = '\0';
1975 else
1976 buf[info.length - 1] = '\0';
1977 printf("%s", buf);
1978 break;
1979 }
1980 case AC_SENT_BDR:
1981 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 WARN_ON(lun != CAM_LUN_WILDCARD);
1983 scsi_report_device_reset(ahc->platform_data->host,
1984 channel - 'A', target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 break;
1986 }
1987 case AC_BUS_RESET:
1988 if (ahc->platform_data->host != NULL) {
1989 scsi_report_bus_reset(ahc->platform_data->host,
1990 channel - 'A');
1991 }
1992 break;
1993 default:
1994 panic("ahc_send_async: Unexpected async event");
1995 }
1996}
1997
1998/*
1999 * Calls the higher level scsi done function and frees the scb.
2000 */
2001void
2002ahc_done(struct ahc_softc *ahc, struct scb *scb)
2003{
Christoph Hellwig 013791e2005-05-16 18:52:39 +02002004 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct ahc_linux_device *dev;
2006
2007 LIST_REMOVE(scb, pending_links);
2008 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
2009 struct scb_tailq *untagged_q;
2010 int target_offset;
2011
2012 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
2013 untagged_q = &(ahc->untagged_queues[target_offset]);
2014 TAILQ_REMOVE(untagged_q, scb, links.tqe);
James Bottomley e4e360c2005-05-16 16:39:38 -05002015 BUG_ON(!TAILQ_EMPTY(untagged_q));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
2018 if ((scb->flags & SCB_ACTIVE) == 0) {
2019 printf("SCB %d done'd twice\n", scb->hscb->tag);
2020 ahc_dump_card_state(ahc);
2021 panic("Stopping for safety");
2022 }
2023 cmd = scb->io_ctx;
2024 dev = scb->platform_data->dev;
2025 dev->active--;
2026 dev->openings++;
2027 if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
2028 cmd->result &= ~(CAM_DEV_QFRZN << 16);
2029 dev->qfrozen--;
2030 }
2031 ahc_linux_unmap_scb(ahc, scb);
2032
2033 /*
2034 * Guard against stale sense data.
2035 * The Linux mid-layer assumes that sense
2036 * was retrieved anytime the first byte of
2037 * the sense buffer looks "sane".
2038 */
2039 cmd->sense_buffer[0] = 0;
2040 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
2041 uint32_t amount_xferred;
2042
2043 amount_xferred =
2044 ahc_get_transfer_length(scb) - ahc_get_residual(scb);
2045 if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
2046#ifdef AHC_DEBUG
2047 if ((ahc_debug & AHC_SHOW_MISC) != 0) {
2048 ahc_print_path(ahc, scb);
2049 printf("Set CAM_UNCOR_PARITY\n");
2050 }
2051#endif
2052 ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
2053#ifdef AHC_REPORT_UNDERFLOWS
2054 /*
2055 * This code is disabled by default as some
2056 * clients of the SCSI system do not properly
2057 * initialize the underflow parameter. This
2058 * results in spurious termination of commands
2059 * that complete as expected (e.g. underflow is
2060 * allowed as command can return variable amounts
2061 * of data.
2062 */
2063 } else if (amount_xferred < scb->io_ctx->underflow) {
2064 u_int i;
2065
2066 ahc_print_path(ahc, scb);
2067 printf("CDB:");
2068 for (i = 0; i < scb->io_ctx->cmd_len; i++)
2069 printf(" 0x%x", scb->io_ctx->cmnd[i]);
2070 printf("\n");
2071 ahc_print_path(ahc, scb);
2072 printf("Saw underflow (%ld of %ld bytes). "
2073 "Treated as error\n",
2074 ahc_get_residual(scb),
2075 ahc_get_transfer_length(scb));
2076 ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
2077#endif
2078 } else {
2079 ahc_set_transaction_status(scb, CAM_REQ_CMP);
2080 }
2081 } else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
2082 ahc_linux_handle_scsi_status(ahc, dev, scb);
2083 } else if (ahc_get_transaction_status(scb) == CAM_SEL_TIMEOUT) {
2084 dev->flags |= AHC_DEV_UNCONFIGURED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 if (dev->openings == 1
2088 && ahc_get_transaction_status(scb) == CAM_REQ_CMP
2089 && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
2090 dev->tag_success_count++;
2091 /*
2092 * Some devices deal with temporary internal resource
2093 * shortages by returning queue full. When the queue
2094 * full occurrs, we throttle back. Slowly try to get
2095 * back to our previous queue depth.
2096 */
2097 if ((dev->openings + dev->active) < dev->maxtags
2098 && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
2099 dev->tag_success_count = 0;
2100 dev->openings++;
2101 }
2102
2103 if (dev->active == 0)
2104 dev->commands_since_idle_or_otag = 0;
2105
James Bottomley e4e360c2005-05-16 16:39:38 -05002106 if ((dev->flags & AHC_DEV_UNCONFIGURED) != 0
2107 && dev->active == 0
2108 && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0)
2109 ahc_linux_free_device(ahc, dev);
2110 else if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links);
2112 dev->flags |= AHC_DEV_ON_RUN_LIST;
2113 }
2114
2115 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
2116 printf("Recovery SCB completes\n");
2117 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
2118 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
2119 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
2120 if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
2121 ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
2122 up(&ahc->platform_data->eh_sem);
2123 }
2124 }
2125
2126 ahc_free_scb(ahc, scb);
2127 ahc_linux_queue_cmd_complete(ahc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130static void
2131ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
2132 struct ahc_linux_device *dev, struct scb *scb)
2133{
2134 struct ahc_devinfo devinfo;
2135
2136 ahc_compile_devinfo(&devinfo,
2137 ahc->our_id,
2138 dev->target->target, dev->lun,
2139 dev->target->channel == 0 ? 'A' : 'B',
2140 ROLE_INITIATOR);
2141
2142 /*
2143 * We don't currently trust the mid-layer to
2144 * properly deal with queue full or busy. So,
2145 * when one occurs, we tell the mid-layer to
2146 * unconditionally requeue the command to us
2147 * so that we can retry it ourselves. We also
2148 * implement our own throttling mechanism so
2149 * we don't clobber the device with too many
2150 * commands.
2151 */
2152 switch (ahc_get_scsi_status(scb)) {
2153 default:
2154 break;
2155 case SCSI_STATUS_CHECK_COND:
2156 case SCSI_STATUS_CMD_TERMINATED:
2157 {
Christoph Hellwig 013791e2005-05-16 18:52:39 +02002158 struct scsi_cmnd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 /*
2161 * Copy sense information to the OS's cmd
2162 * structure if it is available.
2163 */
2164 cmd = scb->io_ctx;
2165 if (scb->flags & SCB_SENSE) {
2166 u_int sense_size;
2167
2168 sense_size = MIN(sizeof(struct scsi_sense_data)
2169 - ahc_get_sense_residual(scb),
2170 sizeof(cmd->sense_buffer));
2171 memcpy(cmd->sense_buffer,
2172 ahc_get_sense_buf(ahc, scb), sense_size);
2173 if (sense_size < sizeof(cmd->sense_buffer))
2174 memset(&cmd->sense_buffer[sense_size], 0,
2175 sizeof(cmd->sense_buffer) - sense_size);
2176 cmd->result |= (DRIVER_SENSE << 24);
2177#ifdef AHC_DEBUG
2178 if (ahc_debug & AHC_SHOW_SENSE) {
2179 int i;
2180
2181 printf("Copied %d bytes of sense data:",
2182 sense_size);
2183 for (i = 0; i < sense_size; i++) {
2184 if ((i & 0xF) == 0)
2185 printf("\n");
2186 printf("0x%x ", cmd->sense_buffer[i]);
2187 }
2188 printf("\n");
2189 }
2190#endif
2191 }
2192 break;
2193 }
2194 case SCSI_STATUS_QUEUE_FULL:
2195 {
2196 /*
2197 * By the time the core driver has returned this
2198 * command, all other commands that were queued
2199 * to us but not the device have been returned.
2200 * This ensures that dev->active is equal to
2201 * the number of commands actually queued to
2202 * the device.
2203 */
2204 dev->tag_success_count = 0;
2205 if (dev->active != 0) {
2206 /*
2207 * Drop our opening count to the number
2208 * of commands currently outstanding.
2209 */
2210 dev->openings = 0;
2211/*
2212 ahc_print_path(ahc, scb);
2213 printf("Dropping tag count to %d\n", dev->active);
2214 */
2215 if (dev->active == dev->tags_on_last_queuefull) {
2216
2217 dev->last_queuefull_same_count++;
2218 /*
2219 * If we repeatedly see a queue full
2220 * at the same queue depth, this
2221 * device has a fixed number of tag
2222 * slots. Lock in this tag depth
2223 * so we stop seeing queue fulls from
2224 * this device.
2225 */
2226 if (dev->last_queuefull_same_count
2227 == AHC_LOCK_TAGS_COUNT) {
2228 dev->maxtags = dev->active;
2229 ahc_print_path(ahc, scb);
2230 printf("Locking max tag count at %d\n",
2231 dev->active);
2232 }
2233 } else {
2234 dev->tags_on_last_queuefull = dev->active;
2235 dev->last_queuefull_same_count = 0;
2236 }
2237 ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
2238 ahc_set_scsi_status(scb, SCSI_STATUS_OK);
2239 ahc_platform_set_tags(ahc, &devinfo,
2240 (dev->flags & AHC_DEV_Q_BASIC)
2241 ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
2242 break;
2243 }
2244 /*
2245 * Drop down to a single opening, and treat this
2246 * as if the target returned BUSY SCSI status.
2247 */
2248 dev->openings = 1;
2249 ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
2250 ahc_platform_set_tags(ahc, &devinfo,
2251 (dev->flags & AHC_DEV_Q_BASIC)
2252 ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
2253 /* FALLTHROUGH */
2254 }
2255 case SCSI_STATUS_BUSY:
2256 {
2257 /*
2258 * Set a short timer to defer sending commands for
2259 * a bit since Linux will not delay in this case.
2260 */
2261 if ((dev->flags & AHC_DEV_TIMER_ACTIVE) != 0) {
2262 printf("%s:%c:%d: Device Timer still active during "
2263 "busy processing\n", ahc_name(ahc),
2264 dev->target->channel, dev->target->target);
2265 break;
2266 }
2267 dev->flags |= AHC_DEV_TIMER_ACTIVE;
2268 dev->qfrozen++;
2269 init_timer(&dev->timer);
2270 dev->timer.data = (u_long)dev;
2271 dev->timer.expires = jiffies + (HZ/2);
2272 dev->timer.function = ahc_linux_dev_timed_unfreeze;
2273 add_timer(&dev->timer);
2274 break;
2275 }
2276 }
2277}
2278
2279static void
Christoph Hellwig 013791e2005-05-16 18:52:39 +02002280ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 /*
2283 * Typically, the complete queue has very few entries
2284 * queued to it before the queue is emptied by
2285 * ahc_linux_run_complete_queue, so sorting the entries
2286 * by generation number should be inexpensive.
2287 * We perform the sort so that commands that complete
2288 * with an error are retuned in the order origionally
2289 * queued to the controller so that any subsequent retries
2290 * are performed in order. The underlying ahc routines do
2291 * not guarantee the order that aborted commands will be
2292 * returned to us.
2293 */
2294 struct ahc_completeq *completeq;
2295 struct ahc_cmd *list_cmd;
2296 struct ahc_cmd *acmd;
2297
2298 /*
2299 * Map CAM error codes into Linux Error codes. We
2300 * avoid the conversion so that the DV code has the
2301 * full error information available when making
2302 * state change decisions.
2303 */
cb624022005-04-17 18:03:20 -05002304 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 u_int new_status;
2306
2307 switch (ahc_cmd_get_transaction_status(cmd)) {
2308 case CAM_REQ_INPROG:
2309 case CAM_REQ_CMP:
2310 case CAM_SCSI_STATUS_ERROR:
2311 new_status = DID_OK;
2312 break;
2313 case CAM_REQ_ABORTED:
2314 new_status = DID_ABORT;
2315 break;
2316 case CAM_BUSY:
2317 new_status = DID_BUS_BUSY;
2318 break;
2319 case CAM_REQ_INVALID:
2320 case CAM_PATH_INVALID:
2321 new_status = DID_BAD_TARGET;
2322 break;
2323 case CAM_SEL_TIMEOUT:
2324 new_status = DID_NO_CONNECT;
2325 break;
2326 case CAM_SCSI_BUS_RESET:
2327 case CAM_BDR_SENT:
2328 new_status = DID_RESET;
2329 break;
2330 case CAM_UNCOR_PARITY:
2331 new_status = DID_PARITY;
2332 break;
2333 case CAM_CMD_TIMEOUT:
2334 new_status = DID_TIME_OUT;
2335 break;
2336 case CAM_UA_ABORT:
2337 case CAM_REQ_CMP_ERR:
2338 case CAM_AUTOSENSE_FAIL:
2339 case CAM_NO_HBA:
2340 case CAM_DATA_RUN_ERR:
2341 case CAM_UNEXP_BUSFREE:
2342 case CAM_SEQUENCE_FAIL:
2343 case CAM_CCB_LEN_ERR:
2344 case CAM_PROVIDE_FAIL:
2345 case CAM_REQ_TERMIO:
2346 case CAM_UNREC_HBA_ERROR:
2347 case CAM_REQ_TOO_BIG:
2348 new_status = DID_ERROR;
2349 break;
2350 case CAM_REQUEUE_REQ:
2351 /*
2352 * If we want the request requeued, make sure there
2353 * are sufficent retries. In the old scsi error code,
2354 * we used to be able to specify a result code that
2355 * bypassed the retry count. Now we must use this
2356 * hack. We also "fake" a check condition with
2357 * a sense code of ABORTED COMMAND. This seems to
2358 * evoke a retry even if this command is being sent
2359 * via the eh thread. Ick! Ick! Ick!
2360 */
2361 if (cmd->retries > 0)
2362 cmd->retries--;
2363 new_status = DID_OK;
2364 ahc_cmd_set_scsi_status(cmd, SCSI_STATUS_CHECK_COND);
2365 cmd->result |= (DRIVER_SENSE << 24);
2366 memset(cmd->sense_buffer, 0,
2367 sizeof(cmd->sense_buffer));
2368 cmd->sense_buffer[0] = SSD_ERRCODE_VALID
2369 | SSD_CURRENT_ERROR;
2370 cmd->sense_buffer[2] = SSD_KEY_ABORTED_COMMAND;
2371 break;
2372 default:
2373 /* We should never get here */
2374 new_status = DID_ERROR;
2375 break;
2376 }
2377
2378 ahc_cmd_set_transaction_status(cmd, new_status);
2379 }
2380
2381 completeq = &ahc->platform_data->completeq;
2382 list_cmd = TAILQ_FIRST(completeq);
2383 acmd = (struct ahc_cmd *)cmd;
2384 while (list_cmd != NULL
2385 && acmd_scsi_cmd(list_cmd).serial_number
2386 < acmd_scsi_cmd(acmd).serial_number)
2387 list_cmd = TAILQ_NEXT(list_cmd, acmd_links.tqe);
2388 if (list_cmd != NULL)
2389 TAILQ_INSERT_BEFORE(list_cmd, acmd, acmd_links.tqe);
2390 else
2391 TAILQ_INSERT_TAIL(completeq, acmd, acmd_links.tqe);
2392}
2393
2394static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395ahc_linux_sem_timeout(u_long arg)
2396{
2397 struct ahc_softc *ahc;
2398 u_long s;
2399
2400 ahc = (struct ahc_softc *)arg;
2401
2402 ahc_lock(ahc, &s);
2403 if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
2404 ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
2405 up(&ahc->platform_data->eh_sem);
2406 }
2407 ahc_unlock(ahc, &s);
2408}
2409
2410static void
2411ahc_linux_freeze_simq(struct ahc_softc *ahc)
2412{
2413 ahc->platform_data->qfrozen++;
2414 if (ahc->platform_data->qfrozen == 1) {
2415 scsi_block_requests(ahc->platform_data->host);
2416
2417 /* XXX What about Twin channels? */
2418 ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
2419 CAM_LUN_WILDCARD, SCB_LIST_NULL,
2420 ROLE_INITIATOR, CAM_REQUEUE_REQ);
2421 }
2422}
2423
2424static void
2425ahc_linux_release_simq(u_long arg)
2426{
2427 struct ahc_softc *ahc;
2428 u_long s;
2429 int unblock_reqs;
2430
2431 ahc = (struct ahc_softc *)arg;
2432
2433 unblock_reqs = 0;
2434 ahc_lock(ahc, &s);
2435 if (ahc->platform_data->qfrozen > 0)
2436 ahc->platform_data->qfrozen--;
2437 if (ahc->platform_data->qfrozen == 0)
2438 unblock_reqs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 ahc_unlock(ahc, &s);
2440 /*
2441 * There is still a race here. The mid-layer
2442 * should keep its own freeze count and use
2443 * a bottom half handler to run the queues
2444 * so we can unblock with our own lock held.
2445 */
2446 if (unblock_reqs)
2447 scsi_unblock_requests(ahc->platform_data->host);
2448}
2449
2450static void
2451ahc_linux_dev_timed_unfreeze(u_long arg)
2452{
2453 struct ahc_linux_device *dev;
2454 struct ahc_softc *ahc;
2455 u_long s;
2456
2457 dev = (struct ahc_linux_device *)arg;
2458 ahc = dev->target->ahc;
2459 ahc_lock(ahc, &s);
2460 dev->flags &= ~AHC_DEV_TIMER_ACTIVE;
2461 if (dev->qfrozen > 0)
2462 dev->qfrozen--;
James Bottomley e4e360c2005-05-16 16:39:38 -05002463 if (dev->active == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 __ahc_linux_free_device(ahc, dev);
2465 ahc_unlock(ahc, &s);
2466}
2467
2468static int
Christoph Hellwig 013791e2005-05-16 18:52:39 +02002469ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 struct ahc_softc *ahc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct ahc_linux_device *dev;
2473 struct scb *pending_scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 u_int saved_scbptr;
2475 u_int active_scb_index;
2476 u_int last_phase;
2477 u_int saved_scsiid;
2478 u_int cdb_byte;
2479 int retval;
2480 int was_paused;
2481 int paused;
2482 int wait;
2483 int disconnected;
2484
2485 pending_scb = NULL;
2486 paused = FALSE;
2487 wait = FALSE;
2488 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 printf("%s:%d:%d:%d: Attempting to queue a%s message\n",
2491 ahc_name(ahc), cmd->device->channel,
2492 cmd->device->id, cmd->device->lun,
2493 flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
2494
2495 printf("CDB:");
2496 for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
2497 printf(" 0x%x", cmd->cmnd[cdb_byte]);
2498 printf("\n");
2499
2500 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 * First determine if we currently own this command.
2502 * Start by searching the device queue. If not found
2503 * there, check the pending_scb list. If not found
2504 * at all, and the system wanted us to just abort the
2505 * command, return success.
2506 */
2507 dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id,
2508 cmd->device->lun, /*alloc*/FALSE);
2509
2510 if (dev == NULL) {
2511 /*
2512 * No target device for this command exists,
2513 * so we must not still own the command.
2514 */
2515 printf("%s:%d:%d:%d: Is not an active device\n",
2516 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2517 cmd->device->lun);
2518 retval = SUCCESS;
2519 goto no_cmd;
2520 }
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
2523 && ahc_search_untagged_queues(ahc, cmd, cmd->device->id,
2524 cmd->device->channel + 'A',
2525 cmd->device->lun,
2526 CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
2527 printf("%s:%d:%d:%d: Command found on untagged queue\n",
2528 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2529 cmd->device->lun);
2530 retval = SUCCESS;
2531 goto done;
2532 }
2533
2534 /*
2535 * See if we can find a matching cmd in the pending list.
2536 */
2537 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2538 if (pending_scb->io_ctx == cmd)
2539 break;
2540 }
2541
2542 if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
2543
2544 /* Any SCB for this device will do for a target reset */
2545 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2546 if (ahc_match_scb(ahc, pending_scb, cmd->device->id,
2547 cmd->device->channel + 'A',
2548 CAM_LUN_WILDCARD,
2549 SCB_LIST_NULL, ROLE_INITIATOR) == 0)
2550 break;
2551 }
2552 }
2553
2554 if (pending_scb == NULL) {
2555 printf("%s:%d:%d:%d: Command not found\n",
2556 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2557 cmd->device->lun);
2558 goto no_cmd;
2559 }
2560
2561 if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
2562 /*
2563 * We can't queue two recovery actions using the same SCB
2564 */
2565 retval = FAILED;
2566 goto done;
2567 }
2568
2569 /*
2570 * Ensure that the card doesn't do anything
2571 * behind our back and that we didn't "just" miss
2572 * an interrupt that would affect this cmd.
2573 */
2574 was_paused = ahc_is_paused(ahc);
2575 ahc_pause_and_flushwork(ahc);
2576 paused = TRUE;
2577
2578 if ((pending_scb->flags & SCB_ACTIVE) == 0) {
2579 printf("%s:%d:%d:%d: Command already completed\n",
2580 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2581 cmd->device->lun);
2582 goto no_cmd;
2583 }
2584
2585 printf("%s: At time of recovery, card was %spaused\n",
2586 ahc_name(ahc), was_paused ? "" : "not ");
2587 ahc_dump_card_state(ahc);
2588
2589 disconnected = TRUE;
2590 if (flag == SCB_ABORT) {
2591 if (ahc_search_qinfifo(ahc, cmd->device->id,
2592 cmd->device->channel + 'A',
2593 cmd->device->lun,
2594 pending_scb->hscb->tag,
2595 ROLE_INITIATOR, CAM_REQ_ABORTED,
2596 SEARCH_COMPLETE) > 0) {
2597 printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
2598 ahc_name(ahc), cmd->device->channel,
2599 cmd->device->id, cmd->device->lun);
2600 retval = SUCCESS;
2601 goto done;
2602 }
2603 } else if (ahc_search_qinfifo(ahc, cmd->device->id,
2604 cmd->device->channel + 'A',
2605 cmd->device->lun, pending_scb->hscb->tag,
2606 ROLE_INITIATOR, /*status*/0,
2607 SEARCH_COUNT) > 0) {
2608 disconnected = FALSE;
2609 }
2610
2611 if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
2612 struct scb *bus_scb;
2613
2614 bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
2615 if (bus_scb == pending_scb)
2616 disconnected = FALSE;
2617 else if (flag != SCB_ABORT
2618 && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
2619 && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb))
2620 disconnected = FALSE;
2621 }
2622
2623 /*
2624 * At this point, pending_scb is the scb associated with the
2625 * passed in command. That command is currently active on the
2626 * bus, is in the disconnected state, or we're hoping to find
2627 * a command for the same target active on the bus to abuse to
2628 * send a BDR. Queue the appropriate message based on which of
2629 * these states we are in.
2630 */
2631 last_phase = ahc_inb(ahc, LASTPHASE);
2632 saved_scbptr = ahc_inb(ahc, SCBPTR);
2633 active_scb_index = ahc_inb(ahc, SCB_TAG);
2634 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
2635 if (last_phase != P_BUSFREE
2636 && (pending_scb->hscb->tag == active_scb_index
2637 || (flag == SCB_DEVICE_RESET
2638 && SCSIID_TARGET(ahc, saved_scsiid) == cmd->device->id))) {
2639
2640 /*
2641 * We're active on the bus, so assert ATN
2642 * and hope that the target responds.
2643 */
2644 pending_scb = ahc_lookup_scb(ahc, active_scb_index);
2645 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2646 ahc_outb(ahc, MSG_OUT, HOST_MSG);
2647 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
2648 printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
2649 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2650 cmd->device->lun);
2651 wait = TRUE;
2652 } else if (disconnected) {
2653
2654 /*
2655 * Actually re-queue this SCB in an attempt
2656 * to select the device before it reconnects.
2657 * In either case (selection or reselection),
2658 * we will now issue the approprate message
2659 * to the timed-out device.
2660 *
2661 * Set the MK_MESSAGE control bit indicating
2662 * that we desire to send a message. We
2663 * also set the disconnected flag since
2664 * in the paging case there is no guarantee
2665 * that our SCB control byte matches the
2666 * version on the card. We don't want the
2667 * sequencer to abort the command thinking
2668 * an unsolicited reselection occurred.
2669 */
2670 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
2671 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2672
2673 /*
2674 * Remove any cached copy of this SCB in the
2675 * disconnected list in preparation for the
2676 * queuing of our abort SCB. We use the
2677 * same element in the SCB, SCB_NEXT, for
2678 * both the qinfifo and the disconnected list.
2679 */
2680 ahc_search_disc_list(ahc, cmd->device->id,
2681 cmd->device->channel + 'A',
2682 cmd->device->lun, pending_scb->hscb->tag,
2683 /*stop_on_first*/TRUE,
2684 /*remove*/TRUE,
2685 /*save_state*/FALSE);
2686
2687 /*
2688 * In the non-paging case, the sequencer will
2689 * never re-reference the in-core SCB.
2690 * To make sure we are notified during
2691 * reslection, set the MK_MESSAGE flag in
2692 * the card's copy of the SCB.
2693 */
2694 if ((ahc->flags & AHC_PAGESCBS) == 0) {
2695 ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
2696 ahc_outb(ahc, SCB_CONTROL,
2697 ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
2698 }
2699
2700 /*
2701 * Clear out any entries in the QINFIFO first
2702 * so we are the next SCB for this target
2703 * to run.
2704 */
2705 ahc_search_qinfifo(ahc, cmd->device->id,
2706 cmd->device->channel + 'A',
2707 cmd->device->lun, SCB_LIST_NULL,
2708 ROLE_INITIATOR, CAM_REQUEUE_REQ,
2709 SEARCH_COMPLETE);
2710 ahc_qinfifo_requeue_tail(ahc, pending_scb);
2711 ahc_outb(ahc, SCBPTR, saved_scbptr);
2712 ahc_print_path(ahc, pending_scb);
2713 printf("Device is disconnected, re-queuing SCB\n");
2714 wait = TRUE;
2715 } else {
2716 printf("%s:%d:%d:%d: Unable to deliver message\n",
2717 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2718 cmd->device->lun);
2719 retval = FAILED;
2720 goto done;
2721 }
2722
2723no_cmd:
2724 /*
2725 * Our assumption is that if we don't have the command, no
2726 * recovery action was required, so we return success. Again,
2727 * the semantics of the mid-layer recovery engine are not
2728 * well defined, so this may change in time.
2729 */
2730 retval = SUCCESS;
2731done:
2732 if (paused)
2733 ahc_unpause(ahc);
2734 if (wait) {
2735 struct timer_list timer;
2736 int ret;
2737
2738 ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE;
2739 spin_unlock_irq(&ahc->platform_data->spin_lock);
2740 init_timer(&timer);
2741 timer.data = (u_long)ahc;
2742 timer.expires = jiffies + (5 * HZ);
2743 timer.function = ahc_linux_sem_timeout;
2744 add_timer(&timer);
2745 printf("Recovery code sleeping\n");
2746 down(&ahc->platform_data->eh_sem);
2747 printf("Recovery code awake\n");
2748 ret = del_timer_sync(&timer);
2749 if (ret == 0) {
2750 printf("Timer Expired\n");
2751 retval = FAILED;
2752 }
2753 spin_lock_irq(&ahc->platform_data->spin_lock);
2754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 ahc_linux_run_complete_queue(ahc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return (retval);
2757}
2758
2759void
2760ahc_platform_dump_card_state(struct ahc_softc *ahc)
2761{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
2764static void ahc_linux_exit(void);
2765
James Bottomley fad01ef2005-05-08 16:00:15 -05002766static void ahc_linux_get_width(struct scsi_target *starget)
2767{
2768 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2769 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2770 struct ahc_tmode_tstate *tstate;
2771 struct ahc_initiator_tinfo *tinfo
2772 = ahc_fetch_transinfo(ahc,
2773 starget->channel + 'A',
2774 shost->this_id, starget->id, &tstate);
2775 spi_width(starget) = tinfo->curr.width;
2776}
2777
2778static void ahc_linux_set_width(struct scsi_target *starget, int width)
2779{
2780 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2781 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2782 struct ahc_devinfo devinfo;
2783 unsigned long flags;
2784
2785 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2786 starget->channel + 'A', ROLE_INITIATOR);
2787 ahc_lock(ahc, &flags);
2788 ahc_set_width(ahc, &devinfo, width, AHC_TRANS_GOAL, FALSE);
2789 ahc_unlock(ahc, &flags);
2790}
2791
92d161c2005-04-17 16:59:33 -05002792static void ahc_linux_get_period(struct scsi_target *starget)
2793{
2794 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2795 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2796 struct ahc_tmode_tstate *tstate;
2797 struct ahc_initiator_tinfo *tinfo
2798 = ahc_fetch_transinfo(ahc,
2799 starget->channel + 'A',
2800 shost->this_id, starget->id, &tstate);
2801 spi_period(starget) = tinfo->curr.period;
2802}
2803
2804static void ahc_linux_set_period(struct scsi_target *starget, int period)
2805{
2806 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2807 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2808 struct ahc_tmode_tstate *tstate;
2809 struct ahc_initiator_tinfo *tinfo
2810 = ahc_fetch_transinfo(ahc,
2811 starget->channel + 'A',
2812 shost->this_id, starget->id, &tstate);
2813 struct ahc_devinfo devinfo;
2814 unsigned int ppr_options = tinfo->curr.ppr_options;
2815 unsigned long flags;
2816 unsigned long offset = tinfo->curr.offset;
2817 struct ahc_syncrate *syncrate;
2818
2819 if (offset == 0)
2820 offset = MAX_OFFSET;
2821
2822 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2823 starget->channel + 'A', ROLE_INITIATOR);
James Bottomley fad01ef2005-05-08 16:00:15 -05002824
2825 /* all PPR requests apart from QAS require wide transfers */
2826 if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
2827 ahc_linux_get_width(starget);
2828 if (spi_width(starget) == 0)
2829 ppr_options &= MSG_EXT_PPR_QAS_REQ;
2830 }
2831
92d161c2005-04-17 16:59:33 -05002832 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
2833 ahc_lock(ahc, &flags);
2834 ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
2835 ppr_options, AHC_TRANS_GOAL, FALSE);
2836 ahc_unlock(ahc, &flags);
2837}
2838
2839static void ahc_linux_get_offset(struct scsi_target *starget)
2840{
2841 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2842 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2843 struct ahc_tmode_tstate *tstate;
2844 struct ahc_initiator_tinfo *tinfo
2845 = ahc_fetch_transinfo(ahc,
2846 starget->channel + 'A',
2847 shost->this_id, starget->id, &tstate);
2848 spi_offset(starget) = tinfo->curr.offset;
2849}
2850
2851static void ahc_linux_set_offset(struct scsi_target *starget, int offset)
2852{
2853 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2854 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2855 struct ahc_tmode_tstate *tstate;
2856 struct ahc_initiator_tinfo *tinfo
2857 = ahc_fetch_transinfo(ahc,
2858 starget->channel + 'A',
2859 shost->this_id, starget->id, &tstate);
2860 struct ahc_devinfo devinfo;
2861 unsigned int ppr_options = 0;
2862 unsigned int period = 0;
2863 unsigned long flags;
2864 struct ahc_syncrate *syncrate = NULL;
2865
2866 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2867 starget->channel + 'A', ROLE_INITIATOR);
2868 if (offset != 0) {
2869 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
2870 period = tinfo->curr.period;
2871 ppr_options = tinfo->curr.ppr_options;
2872 }
2873 ahc_lock(ahc, &flags);
2874 ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
2875 ppr_options, AHC_TRANS_GOAL, FALSE);
2876 ahc_unlock(ahc, &flags);
2877}
2878
92d161c2005-04-17 16:59:33 -05002879static void ahc_linux_get_dt(struct scsi_target *starget)
2880{
2881 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2882 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2883 struct ahc_tmode_tstate *tstate;
2884 struct ahc_initiator_tinfo *tinfo
2885 = ahc_fetch_transinfo(ahc,
2886 starget->channel + 'A',
2887 shost->this_id, starget->id, &tstate);
2888 spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ;
2889}
2890
2891static void ahc_linux_set_dt(struct scsi_target *starget, int dt)
2892{
2893 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2894 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2895 struct ahc_tmode_tstate *tstate;
2896 struct ahc_initiator_tinfo *tinfo
2897 = ahc_fetch_transinfo(ahc,
2898 starget->channel + 'A',
2899 shost->this_id, starget->id, &tstate);
2900 struct ahc_devinfo devinfo;
2901 unsigned int ppr_options = tinfo->curr.ppr_options
2902 & ~MSG_EXT_PPR_DT_REQ;
2903 unsigned int period = tinfo->curr.period;
2904 unsigned long flags;
2905 struct ahc_syncrate *syncrate;
2906
2907 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2908 starget->channel + 'A', ROLE_INITIATOR);
James Bottomley fad01ef2005-05-08 16:00:15 -05002909 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,AHC_SYNCRATE_DT);
92d161c2005-04-17 16:59:33 -05002910 ahc_lock(ahc, &flags);
2911 ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->curr.offset,
2912 ppr_options, AHC_TRANS_GOAL, FALSE);
2913 ahc_unlock(ahc, &flags);
2914}
2915
2916static void ahc_linux_get_qas(struct scsi_target *starget)
2917{
2918 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2919 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2920 struct ahc_tmode_tstate *tstate;
2921 struct ahc_initiator_tinfo *tinfo
2922 = ahc_fetch_transinfo(ahc,
2923 starget->channel + 'A',
2924 shost->this_id, starget->id, &tstate);
2925 spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ;
2926}
2927
2928static void ahc_linux_set_qas(struct scsi_target *starget, int qas)
2929{
2930 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2931 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2932 struct ahc_tmode_tstate *tstate;
2933 struct ahc_initiator_tinfo *tinfo
2934 = ahc_fetch_transinfo(ahc,
2935 starget->channel + 'A',
2936 shost->this_id, starget->id, &tstate);
2937 struct ahc_devinfo devinfo;
2938 unsigned int ppr_options = tinfo->curr.ppr_options
2939 & ~MSG_EXT_PPR_QAS_REQ;
2940 unsigned int period = tinfo->curr.period;
92d161c2005-04-17 16:59:33 -05002941 unsigned long flags;
2942 struct ahc_syncrate *syncrate;
2943
2944 if (qas)
2945 ppr_options |= MSG_EXT_PPR_QAS_REQ;
2946
2947 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2948 starget->channel + 'A', ROLE_INITIATOR);
James Bottomley fad01ef2005-05-08 16:00:15 -05002949 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
92d161c2005-04-17 16:59:33 -05002950 ahc_lock(ahc, &flags);
2951 ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->curr.offset,
2952 ppr_options, AHC_TRANS_GOAL, FALSE);
2953 ahc_unlock(ahc, &flags);
2954}
2955
2956static void ahc_linux_get_iu(struct scsi_target *starget)
2957{
2958 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2959 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2960 struct ahc_tmode_tstate *tstate;
2961 struct ahc_initiator_tinfo *tinfo
2962 = ahc_fetch_transinfo(ahc,
2963 starget->channel + 'A',
2964 shost->this_id, starget->id, &tstate);
2965 spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ;
2966}
2967
2968static void ahc_linux_set_iu(struct scsi_target *starget, int iu)
2969{
2970 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2971 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2972 struct ahc_tmode_tstate *tstate;
2973 struct ahc_initiator_tinfo *tinfo
2974 = ahc_fetch_transinfo(ahc,
2975 starget->channel + 'A',
2976 shost->this_id, starget->id, &tstate);
2977 struct ahc_devinfo devinfo;
2978 unsigned int ppr_options = tinfo->curr.ppr_options
2979 & ~MSG_EXT_PPR_IU_REQ;
2980 unsigned int period = tinfo->curr.period;
92d161c2005-04-17 16:59:33 -05002981 unsigned long flags;
2982 struct ahc_syncrate *syncrate;
2983
2984 if (iu)
2985 ppr_options |= MSG_EXT_PPR_IU_REQ;
2986
2987 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2988 starget->channel + 'A', ROLE_INITIATOR);
James Bottomley fad01ef2005-05-08 16:00:15 -05002989 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
92d161c2005-04-17 16:59:33 -05002990 ahc_lock(ahc, &flags);
2991 ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->curr.offset,
2992 ppr_options, AHC_TRANS_GOAL, FALSE);
2993 ahc_unlock(ahc, &flags);
2994}
2995
2996static struct spi_function_template ahc_linux_transport_functions = {
2997 .get_offset = ahc_linux_get_offset,
2998 .set_offset = ahc_linux_set_offset,
2999 .show_offset = 1,
3000 .get_period = ahc_linux_get_period,
3001 .set_period = ahc_linux_set_period,
3002 .show_period = 1,
3003 .get_width = ahc_linux_get_width,
3004 .set_width = ahc_linux_set_width,
3005 .show_width = 1,
3006 .get_dt = ahc_linux_get_dt,
3007 .set_dt = ahc_linux_set_dt,
3008 .show_dt = 1,
3009 .get_iu = ahc_linux_get_iu,
3010 .set_iu = ahc_linux_set_iu,
3011 .show_iu = 1,
3012 .get_qas = ahc_linux_get_qas,
3013 .set_qas = ahc_linux_set_qas,
3014 .show_qas = 1,
3015};
3016
3017
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019static int __init
3020ahc_linux_init(void)
3021{
92d161c2005-04-17 16:59:33 -05003022 ahc_linux_transport_template = spi_attach_transport(&ahc_linux_transport_functions);
3023 if (!ahc_linux_transport_template)
3024 return -ENODEV;
James Bottomley858eaca162005-04-21 07:35:45 -07003025 if (ahc_linux_detect(&aic7xxx_driver_template))
3026 return 0;
92d161c2005-04-17 16:59:33 -05003027 spi_release_transport(ahc_linux_transport_template);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 ahc_linux_exit();
3029 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
3032static void
3033ahc_linux_exit(void)
3034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 ahc_linux_pci_exit();
3036 ahc_linux_eisa_exit();
92d161c2005-04-17 16:59:33 -05003037 spi_release_transport(ahc_linux_transport_template);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
3040module_init(ahc_linux_init);
3041module_exit(ahc_linux_exit);