[SCSI] zfcp: fix handling of port boxed and lun boxed fsf states
[deliverable/linux.git] / drivers / scsi / aic7xxx / aic7xxx_osm.c
CommitLineData
1da177e4
LT
1/*
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>
92d161c3 125
126static struct scsi_transport_template *ahc_linux_transport_template = NULL;
1da177e4
LT
127
128/*
129 * Include aiclib.c as part of our
130 * "module dependencies are hard" work around.
131 */
132#include "aiclib.c"
133
134#include <linux/init.h> /* __setup */
1da177e4
LT
135#include <linux/mm.h> /* For fetching system memory size */
136#include <linux/blkdev.h> /* For block_size() */
137#include <linux/delay.h> /* For ssleep/msleep */
138
139/*
140 * Lock protecting manipulation of the ahc softc list.
141 */
142spinlock_t ahc_list_spinlock;
143
1da177e4
LT
144/*
145 * Set this to the delay in seconds after SCSI bus reset.
146 * Note, we honor this only for the initial bus reset.
147 * The scsi error recovery code performs its own bus settle
148 * delay handling for error recovery actions.
149 */
150#ifdef CONFIG_AIC7XXX_RESET_DELAY_MS
151#define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS
152#else
153#define AIC7XXX_RESET_DELAY 5000
154#endif
155
156/*
157 * Control collection of SCSI transfer statistics for the /proc filesystem.
158 *
159 * NOTE: Do NOT enable this when running on kernels version 1.2.x and below.
160 * NOTE: This does affect performance since it has to maintain statistics.
161 */
162#ifdef CONFIG_AIC7XXX_PROC_STATS
163#define AIC7XXX_PROC_STATS
164#endif
165
166/*
167 * To change the default number of tagged transactions allowed per-device,
168 * add a line to the lilo.conf file like:
169 * append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}"
170 * which will result in the first four devices on the first two
171 * controllers being set to a tagged queue depth of 32.
172 *
173 * The tag_commands is an array of 16 to allow for wide and twin adapters.
174 * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15
175 * for channel 1.
176 */
177typedef struct {
178 uint8_t tag_commands[16]; /* Allow for wide/twin adapters. */
179} adapter_tag_info_t;
180
181/*
182 * Modify this as you see fit for your system.
183 *
184 * 0 tagged queuing disabled
185 * 1 <= n <= 253 n == max tags ever dispatched.
186 *
187 * The driver will throttle the number of commands dispatched to a
188 * device if it returns queue full. For devices with a fixed maximum
189 * queue depth, the driver will eventually determine this depth and
190 * lock it in (a console message is printed to indicate that a lock
191 * has occurred). On some devices, queue full is returned for a temporary
192 * resource shortage. These devices will return queue full at varying
193 * depths. The driver will throttle back when the queue fulls occur and
194 * attempt to slowly increase the depth over time as the device recovers
195 * from the resource shortage.
196 *
197 * In this example, the first line will disable tagged queueing for all
198 * the devices on the first probed aic7xxx adapter.
199 *
200 * The second line enables tagged queueing with 4 commands/LUN for IDs
201 * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the
202 * driver to attempt to use up to 64 tags for ID 1.
203 *
204 * The third line is the same as the first line.
205 *
206 * The fourth line disables tagged queueing for devices 0 and 3. It
207 * enables tagged queueing for the other IDs, with 16 commands/LUN
208 * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for
209 * IDs 2, 5-7, and 9-15.
210 */
211
212/*
213 * NOTE: The below structure is for reference only, the actual structure
214 * to modify in order to change things is just below this comment block.
215adapter_tag_info_t aic7xxx_tag_info[] =
216{
217 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
218 {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}},
219 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
220 {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}}
221};
222*/
223
224#ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE
225#define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE
226#else
227#define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE
228#endif
229
230#define AIC7XXX_CONFIGED_TAG_COMMANDS { \
231 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
232 AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \
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}
240
241/*
242 * By default, use the number of commands specified by
243 * the users kernel configuration.
244 */
245static adapter_tag_info_t aic7xxx_tag_info[] =
246{
247 {AIC7XXX_CONFIGED_TAG_COMMANDS},
248 {AIC7XXX_CONFIGED_TAG_COMMANDS},
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};
264
1da177e4
LT
265/*
266 * There should be a specific return value for this in scsi.h, but
267 * it seems that most drivers ignore it.
268 */
269#define DID_UNDERFLOW DID_ERROR
270
271void
272ahc_print_path(struct ahc_softc *ahc, struct scb *scb)
273{
274 printk("(scsi%d:%c:%d:%d): ",
275 ahc->platform_data->host->host_no,
276 scb != NULL ? SCB_GET_CHANNEL(ahc, scb) : 'X',
277 scb != NULL ? SCB_GET_TARGET(ahc, scb) : -1,
278 scb != NULL ? SCB_GET_LUN(scb) : -1);
279}
280
281/*
282 * XXX - these options apply unilaterally to _all_ 274x/284x/294x
283 * cards in the system. This should be fixed. Exceptions to this
284 * rule are noted in the comments.
285 */
286
287/*
288 * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This
289 * has no effect on any later resets that might occur due to things like
290 * SCSI bus timeouts.
291 */
292static uint32_t aic7xxx_no_reset;
293
294/*
295 * Certain PCI motherboards will scan PCI devices from highest to lowest,
296 * others scan from lowest to highest, and they tend to do all kinds of
297 * strange things when they come into contact with PCI bridge chips. The
298 * net result of all this is that the PCI card that is actually used to boot
299 * the machine is very hard to detect. Most motherboards go from lowest
300 * PCI slot number to highest, and the first SCSI controller found is the
301 * one you boot from. The only exceptions to this are when a controller
302 * has its BIOS disabled. So, we by default sort all of our SCSI controllers
303 * from lowest PCI slot number to highest PCI slot number. We also force
304 * all controllers with their BIOS disabled to the end of the list. This
305 * works on *almost* all computers. Where it doesn't work, we have this
306 * option. Setting this option to non-0 will reverse the order of the sort
307 * to highest first, then lowest, but will still leave cards with their BIOS
308 * disabled at the very end. That should fix everyone up unless there are
309 * really strange cirumstances.
310 */
311static uint32_t aic7xxx_reverse_scan;
312
313/*
314 * Should we force EXTENDED translation on a controller.
315 * 0 == Use whatever is in the SEEPROM or default to off
316 * 1 == Use whatever is in the SEEPROM or default to on
317 */
318static uint32_t aic7xxx_extended;
319
320/*
321 * PCI bus parity checking of the Adaptec controllers. This is somewhat
322 * dubious at best. To my knowledge, this option has never actually
323 * solved a PCI parity problem, but on certain machines with broken PCI
324 * chipset configurations where stray PCI transactions with bad parity are
325 * the norm rather than the exception, the error messages can be overwelming.
326 * It's included in the driver for completeness.
327 * 0 = Shut off PCI parity check
328 * non-0 = reverse polarity pci parity checking
329 */
330static uint32_t aic7xxx_pci_parity = ~0;
331
1da177e4
LT
332/*
333 * There are lots of broken chipsets in the world. Some of them will
334 * violate the PCI spec when we issue byte sized memory writes to our
335 * controller. I/O mapped register access, if allowed by the given
336 * platform, will work in almost all cases.
337 */
338uint32_t aic7xxx_allow_memio = ~0;
339
340/*
341 * aic7xxx_detect() has been run, so register all device arrivals
342 * immediately with the system rather than deferring to the sorted
343 * attachment performed by aic7xxx_detect().
344 */
345int aic7xxx_detect_complete;
346
347/*
348 * So that we can set how long each device is given as a selection timeout.
349 * The table of values goes like this:
350 * 0 - 256ms
351 * 1 - 128ms
352 * 2 - 64ms
353 * 3 - 32ms
354 * We default to 256ms because some older devices need a longer time
355 * to respond to initial selection.
356 */
357static uint32_t aic7xxx_seltime;
358
359/*
360 * Certain devices do not perform any aging on commands. Should the
361 * device be saturated by commands in one portion of the disk, it is
362 * possible for transactions on far away sectors to never be serviced.
363 * To handle these devices, we can periodically send an ordered tag to
364 * force all outstanding transactions to be serviced prior to a new
365 * transaction.
366 */
367uint32_t aic7xxx_periodic_otag;
368
369/*
370 * Module information and settable options.
371 */
372static char *aic7xxx = NULL;
373
374MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>");
375MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver");
376MODULE_LICENSE("Dual BSD/GPL");
377MODULE_VERSION(AIC7XXX_DRIVER_VERSION);
378module_param(aic7xxx, charp, 0444);
379MODULE_PARM_DESC(aic7xxx,
380"period delimited, options string.\n"
381" verbose Enable verbose/diagnostic logging\n"
382" allow_memio Allow device registers to be memory mapped\n"
383" debug Bitmask of debug values to enable\n"
384" no_probe Toggle EISA/VLB controller probing\n"
385" probe_eisa_vl Toggle EISA/VLB controller probing\n"
386" no_reset Supress initial bus resets\n"
387" extended Enable extended geometry on all controllers\n"
388" periodic_otag Send an ordered tagged transaction\n"
389" periodically to prevent tag starvation.\n"
390" This may be required by some older disk\n"
391" drives or RAID arrays.\n"
392" reverse_scan Sort PCI devices highest Bus/Slot to lowest\n"
393" tag_info:<tag_str> Set per-target tag depth\n"
394" global_tag_depth:<int> Global tag depth for every target\n"
395" on every bus\n"
1da177e4
LT
396" seltime:<int> Selection Timeout\n"
397" (0/256ms,1/128ms,2/64ms,3/32ms)\n"
398"\n"
399" Sample /etc/modprobe.conf line:\n"
400" Toggle EISA/VLB probing\n"
401" Set tag depth on Controller 1/Target 1 to 10 tags\n"
402" Shorten the selection timeout to 128ms\n"
403"\n"
404" options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n"
405);
406
407static void ahc_linux_handle_scsi_status(struct ahc_softc *,
b1abb4d6 408 struct scsi_device *,
1da177e4
LT
409 struct scb *);
410static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc,
013791ee 411 struct scsi_cmnd *cmd);
1da177e4
LT
412static void ahc_linux_sem_timeout(u_long arg);
413static void ahc_linux_freeze_simq(struct ahc_softc *ahc);
414static void ahc_linux_release_simq(u_long arg);
013791ee 415static int ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag);
1da177e4 416static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc);
1da177e4
LT
417static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc,
418 struct ahc_devinfo *devinfo);
b1abb4d6 419static void ahc_linux_device_queue_depth(struct scsi_device *);
e4e360c3
JB
420static int ahc_linux_run_command(struct ahc_softc*,
421 struct ahc_linux_device *,
422 struct scsi_cmnd *);
1da177e4
LT
423static void ahc_linux_setup_tag_info_global(char *p);
424static aic_option_callback_t ahc_linux_setup_tag_info;
1da177e4
LT
425static int aic7xxx_setup(char *s);
426static int ahc_linux_next_unit(void);
1da177e4
LT
427
428/********************************* Inlines ************************************/
1da177e4
LT
429static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*);
430
431static __inline int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
432 struct ahc_dma_seg *sg,
433 dma_addr_t addr, bus_size_t len);
434
1da177e4
LT
435static __inline void
436ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb)
437{
013791ee 438 struct scsi_cmnd *cmd;
1da177e4
LT
439
440 cmd = scb->io_ctx;
441 ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE);
442 if (cmd->use_sg != 0) {
443 struct scatterlist *sg;
444
445 sg = (struct scatterlist *)cmd->request_buffer;
446 pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg,
be7db055 447 cmd->sc_data_direction);
1da177e4
LT
448 } else if (cmd->request_bufflen != 0) {
449 pci_unmap_single(ahc->dev_softc,
450 scb->platform_data->buf_busaddr,
451 cmd->request_bufflen,
be7db055 452 cmd->sc_data_direction);
1da177e4
LT
453 }
454}
455
456static __inline int
457ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb,
458 struct ahc_dma_seg *sg, dma_addr_t addr, bus_size_t len)
459{
460 int consumed;
461
462 if ((scb->sg_count + 1) > AHC_NSEG)
463 panic("Too few segs for dma mapping. "
464 "Increase AHC_NSEG\n");
465
466 consumed = 1;
467 sg->addr = ahc_htole32(addr & 0xFFFFFFFF);
468 scb->platform_data->xfer_len += len;
469
470 if (sizeof(dma_addr_t) > 4
471 && (ahc->flags & AHC_39BIT_ADDRESSING) != 0)
472 len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK;
473
474 sg->len = ahc_htole32(len);
475 return (consumed);
476}
477
1da177e4
LT
478/*
479 * Try to detect an Adaptec 7XXX controller.
480 */
481static int
013791ee 482ahc_linux_detect(struct scsi_host_template *template)
1da177e4
LT
483{
484 struct ahc_softc *ahc;
485 int found = 0;
486
1da177e4
LT
487 /*
488 * If we've been passed any parameters, process them now.
489 */
490 if (aic7xxx)
491 aic7xxx_setup(aic7xxx);
492
493 template->proc_name = "aic7xxx";
494
495 /*
496 * Initialize our softc list lock prior to
497 * probing for any adapters.
498 */
499 ahc_list_lockinit();
500
501 found = ahc_linux_pci_init();
502 if (!ahc_linux_eisa_init())
503 found++;
504
505 /*
506 * Register with the SCSI layer all
507 * controllers we've found.
508 */
509 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
510
511 if (ahc_linux_register_host(ahc, template) == 0)
512 found++;
513 }
514
1da177e4
LT
515 aic7xxx_detect_complete++;
516
517 return (found);
518}
519
1da177e4
LT
520/*
521 * Return a string describing the driver.
522 */
523static const char *
524ahc_linux_info(struct Scsi_Host *host)
525{
526 static char buffer[512];
527 char ahc_info[256];
528 char *bp;
529 struct ahc_softc *ahc;
530
531 bp = &buffer[0];
532 ahc = *(struct ahc_softc **)host->hostdata;
533 memset(bp, 0, sizeof(buffer));
534 strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev ");
535 strcat(bp, AIC7XXX_DRIVER_VERSION);
536 strcat(bp, "\n");
537 strcat(bp, " <");
538 strcat(bp, ahc->description);
539 strcat(bp, ">\n");
540 strcat(bp, " ");
541 ahc_controller_info(ahc, ahc_info);
542 strcat(bp, ahc_info);
543 strcat(bp, "\n");
544
545 return (bp);
546}
547
548/*
549 * Queue an SCB to the controller.
550 */
551static int
013791ee 552ahc_linux_queue(struct scsi_cmnd * cmd, void (*scsi_done) (struct scsi_cmnd *))
1da177e4
LT
553{
554 struct ahc_softc *ahc;
b1abb4d6 555 struct ahc_linux_device *dev = scsi_transport_device_data(cmd->device);
1da177e4
LT
556
557 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
558
559 /*
560 * Save the callback on completion function.
561 */
562 cmd->scsi_done = scsi_done;
563
1da177e4
LT
564 /*
565 * Close the race of a command that was in the process of
566 * being queued to us just as our simq was frozen. Let
567 * DV commands through so long as we are only frozen to
568 * perform DV.
569 */
e4e360c3
JB
570 if (ahc->platform_data->qfrozen != 0)
571 return SCSI_MLQUEUE_HOST_BUSY;
1da177e4 572
1da177e4 573 cmd->result = CAM_REQ_INPROG << 16;
e4e360c3
JB
574
575 return ahc_linux_run_command(ahc, dev, cmd);
1da177e4
LT
576}
577
b1abb4d6
JB
578static inline struct scsi_target **
579ahc_linux_target_in_softc(struct scsi_target *starget)
1da177e4 580{
b1abb4d6
JB
581 struct ahc_softc *ahc =
582 *((struct ahc_softc **)dev_to_shost(&starget->dev)->hostdata);
fb3089df 583 unsigned int target_offset;
b1abb4d6
JB
584
585 target_offset = starget->id;
586 if (starget->channel != 0)
587 target_offset += 8;
588
589 return &ahc->platform_data->starget[target_offset];
590}
591
592static int
593ahc_linux_target_alloc(struct scsi_target *starget)
594{
595 struct ahc_softc *ahc =
596 *((struct ahc_softc **)dev_to_shost(&starget->dev)->hostdata);
597 struct seeprom_config *sc = ahc->seep_config;
fb3089df 598 unsigned long flags;
b1abb4d6
JB
599 struct scsi_target **ahc_targp = ahc_linux_target_in_softc(starget);
600 struct ahc_linux_target *targ = scsi_transport_target_data(starget);
601 unsigned short scsirate;
602 struct ahc_devinfo devinfo;
603 struct ahc_initiator_tinfo *tinfo;
604 struct ahc_tmode_tstate *tstate;
605 char channel = starget->channel + 'A';
606 unsigned int our_id = ahc->our_id;
607 unsigned int target_offset;
c7525233
JB
608
609 target_offset = starget->id;
610 if (starget->channel != 0)
611 target_offset += 8;
b1abb4d6
JB
612
613 if (starget->channel)
614 our_id = ahc->our_id_b;
1da177e4 615
fb3089df 616 ahc_lock(ahc, &flags);
fb3089df 617
b1abb4d6 618 BUG_ON(*ahc_targp != NULL);
c7525233 619
b1abb4d6
JB
620 *ahc_targp = starget;
621 memset(targ, 0, sizeof(*targ));
c7525233 622
b1abb4d6
JB
623 if (sc) {
624 if ((ahc->features & AHC_ULTRA2) != 0) {
625 scsirate = sc->device_flags[target_offset] & CFXFER;
626 } else {
627 scsirate = (sc->device_flags[target_offset] & CFXFER) << 4;
628 if (sc->device_flags[target_offset] & CFSYNCH)
629 scsirate |= SOFS;
c7525233 630 }
b1abb4d6
JB
631 if (sc->device_flags[target_offset] & CFWIDEB) {
632 scsirate |= WIDEXFER;
633 spi_max_width(starget) = 1;
634 } else
635 spi_max_width(starget) = 0;
636 spi_min_period(starget) =
637 ahc_find_period(ahc, scsirate, AHC_SYNCRATE_DT);
638 tinfo = ahc_fetch_transinfo(ahc, channel, ahc->our_id,
639 starget->id, &tstate);
c7525233 640 }
b1abb4d6
JB
641 ahc_compile_devinfo(&devinfo, our_id, starget->id,
642 CAM_LUN_WILDCARD, channel,
643 ROLE_INITIATOR);
644 ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0,
645 AHC_TRANS_GOAL, /*paused*/FALSE);
646 ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
647 AHC_TRANS_GOAL, /*paused*/FALSE);
fb3089df 648 ahc_unlock(ahc, &flags);
b1abb4d6
JB
649
650 return 0;
651}
652
653static void
654ahc_linux_target_destroy(struct scsi_target *starget)
655{
656 struct scsi_target **ahc_targp = ahc_linux_target_in_softc(starget);
657
658 *ahc_targp = NULL;
659}
660
661static int
662ahc_linux_slave_alloc(struct scsi_device *sdev)
663{
664 struct ahc_softc *ahc =
665 *((struct ahc_softc **)sdev->host->hostdata);
666 struct scsi_target *starget = sdev->sdev_target;
667 struct ahc_linux_target *targ = scsi_transport_target_data(starget);
668 struct ahc_linux_device *dev;
669
670 if (bootverbose)
671 printf("%s: Slave Alloc %d\n", ahc_name(ahc), sdev->id);
672
673 BUG_ON(targ->sdev[sdev->lun] != NULL);
674
675 dev = scsi_transport_device_data(sdev);
676 memset(dev, 0, sizeof(*dev));
677
678 /*
679 * We start out life using untagged
680 * transactions of which we allow one.
681 */
682 dev->openings = 1;
683
684 /*
685 * Set maxtags to 0. This will be changed if we
686 * later determine that we are dealing with
687 * a tagged queuing capable device.
688 */
689 dev->maxtags = 0;
690
691 targ->sdev[sdev->lun] = sdev;
692
693 return 0;
1da177e4
LT
694}
695
696static int
b1abb4d6 697ahc_linux_slave_configure(struct scsi_device *sdev)
1da177e4
LT
698{
699 struct ahc_softc *ahc;
1da177e4 700
b1abb4d6 701 ahc = *((struct ahc_softc **)sdev->host->hostdata);
c7525233 702
1da177e4 703 if (bootverbose)
b1abb4d6 704 printf("%s: Slave Configure %d\n", ahc_name(ahc), sdev->id);
c7525233 705
b1abb4d6 706 ahc_linux_device_queue_depth(sdev);
cb624029 707
708 /* Initial Domain Validation */
b1abb4d6
JB
709 if (!spi_initial_dv(sdev->sdev_target))
710 spi_dv_device(sdev);
cb624029 711
c7525233 712 return 0;
1da177e4
LT
713}
714
715static void
b1abb4d6 716ahc_linux_slave_destroy(struct scsi_device *sdev)
1da177e4
LT
717{
718 struct ahc_softc *ahc;
b1abb4d6
JB
719 struct ahc_linux_device *dev = scsi_transport_device_data(sdev);
720 struct ahc_linux_target *targ = scsi_transport_target_data(sdev->sdev_target);
1da177e4 721
b1abb4d6 722 ahc = *((struct ahc_softc **)sdev->host->hostdata);
1da177e4 723 if (bootverbose)
b1abb4d6 724 printf("%s: Slave Destroy %d\n", ahc_name(ahc), sdev->id);
c7525233
JB
725
726 BUG_ON(dev->active);
727
b1abb4d6 728 targ->sdev[sdev->lun] = NULL;
1da177e4 729}
1da177e4
LT
730
731#if defined(__i386__)
732/*
733 * Return the disk geometry for the given SCSI device.
734 */
735static int
1da177e4
LT
736ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev,
737 sector_t capacity, int geom[])
738{
739 uint8_t *bh;
1da177e4
LT
740 int heads;
741 int sectors;
742 int cylinders;
743 int ret;
744 int extended;
745 struct ahc_softc *ahc;
746 u_int channel;
747
748 ahc = *((struct ahc_softc **)sdev->host->hostdata);
749 channel = sdev->channel;
750
1da177e4 751 bh = scsi_bios_ptable(bdev);
1da177e4
LT
752 if (bh) {
753 ret = scsi_partsize(bh, capacity,
754 &geom[2], &geom[0], &geom[1]);
1da177e4 755 kfree(bh);
1da177e4
LT
756 if (ret != -1)
757 return (ret);
758 }
759 heads = 64;
760 sectors = 32;
761 cylinders = aic_sector_div(capacity, heads, sectors);
762
763 if (aic7xxx_extended != 0)
764 extended = 1;
765 else if (channel == 0)
766 extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0;
767 else
768 extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0;
769 if (extended && cylinders >= 1024) {
770 heads = 255;
771 sectors = 63;
772 cylinders = aic_sector_div(capacity, heads, sectors);
773 }
774 geom[0] = heads;
775 geom[1] = sectors;
776 geom[2] = cylinders;
777 return (0);
778}
779#endif
780
781/*
782 * Abort the current SCSI command(s).
783 */
784static int
013791ee 785ahc_linux_abort(struct scsi_cmnd *cmd)
1da177e4
LT
786{
787 int error;
788
789 error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT);
790 if (error != 0)
791 printf("aic7xxx_abort returns 0x%x\n", error);
792 return (error);
793}
794
795/*
796 * Attempt to send a target reset message to the device that timed out.
797 */
798static int
013791ee 799ahc_linux_dev_reset(struct scsi_cmnd *cmd)
1da177e4
LT
800{
801 int error;
802
803 error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET);
804 if (error != 0)
805 printf("aic7xxx_dev_reset returns 0x%x\n", error);
806 return (error);
807}
808
809/*
810 * Reset the SCSI bus.
811 */
812static int
013791ee 813ahc_linux_bus_reset(struct scsi_cmnd *cmd)
1da177e4
LT
814{
815 struct ahc_softc *ahc;
1da177e4
LT
816 int found;
817
818 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
1da177e4
LT
819 found = ahc_reset_channel(ahc, cmd->device->channel + 'A',
820 /*initiate reset*/TRUE);
1da177e4
LT
821
822 if (bootverbose)
823 printf("%s: SCSI bus reset delivered. "
824 "%d SCBs aborted.\n", ahc_name(ahc), found);
825
826 return SUCCESS;
827}
828
013791ee 829struct scsi_host_template aic7xxx_driver_template = {
1da177e4
LT
830 .module = THIS_MODULE,
831 .name = "aic7xxx",
832 .proc_info = ahc_linux_proc_info,
833 .info = ahc_linux_info,
834 .queuecommand = ahc_linux_queue,
835 .eh_abort_handler = ahc_linux_abort,
836 .eh_device_reset_handler = ahc_linux_dev_reset,
837 .eh_bus_reset_handler = ahc_linux_bus_reset,
838#if defined(__i386__)
839 .bios_param = ahc_linux_biosparam,
840#endif
841 .can_queue = AHC_MAX_QUEUE,
842 .this_id = -1,
843 .cmd_per_lun = 2,
844 .use_clustering = ENABLE_CLUSTERING,
845 .slave_alloc = ahc_linux_slave_alloc,
846 .slave_configure = ahc_linux_slave_configure,
847 .slave_destroy = ahc_linux_slave_destroy,
b1abb4d6
JB
848 .target_alloc = ahc_linux_target_alloc,
849 .target_destroy = ahc_linux_target_destroy,
1da177e4
LT
850};
851
852/**************************** Tasklet Handler *********************************/
853
1da177e4
LT
854/******************************** Macros **************************************/
855#define BUILD_SCSIID(ahc, cmd) \
856 ((((cmd)->device->id << TID_SHIFT) & TID) \
857 | (((cmd)->device->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \
858 | (((cmd)->device->channel == 0) ? 0 : TWIN_CHNLB))
859
860/******************************** Bus DMA *************************************/
861int
862ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent,
863 bus_size_t alignment, bus_size_t boundary,
864 dma_addr_t lowaddr, dma_addr_t highaddr,
865 bus_dma_filter_t *filter, void *filterarg,
866 bus_size_t maxsize, int nsegments,
867 bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag)
868{
869 bus_dma_tag_t dmat;
870
871 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
872 if (dmat == NULL)
873 return (ENOMEM);
874
875 /*
876 * Linux is very simplistic about DMA memory. For now don't
877 * maintain all specification information. Once Linux supplies
878 * better facilities for doing these operations, or the
879 * needs of this particular driver change, we might need to do
880 * more here.
881 */
882 dmat->alignment = alignment;
883 dmat->boundary = boundary;
884 dmat->maxsize = maxsize;
885 *ret_tag = dmat;
886 return (0);
887}
888
889void
890ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat)
891{
892 free(dmat, M_DEVBUF);
893}
894
895int
896ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr,
897 int flags, bus_dmamap_t *mapp)
898{
1da177e4 899 *vaddr = pci_alloc_consistent(ahc->dev_softc,
7dfa0f26 900 dmat->maxsize, mapp);
1da177e4 901 if (*vaddr == NULL)
7dfa0f26
CH
902 return ENOMEM;
903 return 0;
1da177e4
LT
904}
905
906void
907ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat,
908 void* vaddr, bus_dmamap_t map)
909{
910 pci_free_consistent(ahc->dev_softc, dmat->maxsize,
7dfa0f26 911 vaddr, map);
1da177e4
LT
912}
913
914int
915ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map,
916 void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb,
917 void *cb_arg, int flags)
918{
919 /*
920 * Assume for now that this will only be used during
921 * initialization and not for per-transaction buffer mapping.
922 */
923 bus_dma_segment_t stack_sg;
924
7dfa0f26 925 stack_sg.ds_addr = map;
1da177e4
LT
926 stack_sg.ds_len = dmat->maxsize;
927 cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0);
928 return (0);
929}
930
931void
932ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
933{
1da177e4
LT
934}
935
936int
937ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map)
938{
939 /* Nothing to do */
940 return (0);
941}
942
943/********************* Platform Dependent Functions ***************************/
944/*
945 * Compare "left hand" softc with "right hand" softc, returning:
946 * < 0 - lahc has a lower priority than rahc
947 * 0 - Softcs are equal
948 * > 0 - lahc has a higher priority than rahc
949 */
950int
951ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
952{
953 int value;
954 int rvalue;
955 int lvalue;
956
957 /*
958 * Under Linux, cards are ordered as follows:
959 * 1) VLB/EISA BIOS enabled devices sorted by BIOS address.
960 * 2) PCI devices with BIOS enabled sorted by bus/slot/func.
961 * 3) All remaining VLB/EISA devices sorted by ioport.
962 * 4) All remaining PCI devices sorted by bus/slot/func.
963 */
964 value = (lahc->flags & AHC_BIOS_ENABLED)
965 - (rahc->flags & AHC_BIOS_ENABLED);
966 if (value != 0)
967 /* Controllers with BIOS enabled have a *higher* priority */
968 return (value);
969
970 /*
971 * Same BIOS setting, now sort based on bus type.
972 * EISA and VL controllers sort together. EISA/VL
973 * have higher priority than PCI.
974 */
975 rvalue = (rahc->chip & AHC_BUS_MASK);
976 if (rvalue == AHC_VL)
977 rvalue = AHC_EISA;
978 lvalue = (lahc->chip & AHC_BUS_MASK);
979 if (lvalue == AHC_VL)
980 lvalue = AHC_EISA;
981 value = rvalue - lvalue;
982 if (value != 0)
983 return (value);
984
985 /* Still equal. Sort by BIOS address, ioport, or bus/slot/func. */
986 switch (rvalue) {
987#ifdef CONFIG_PCI
988 case AHC_PCI:
989 {
990 char primary_channel;
991
992 if (aic7xxx_reverse_scan != 0)
993 value = ahc_get_pci_bus(lahc->dev_softc)
994 - ahc_get_pci_bus(rahc->dev_softc);
995 else
996 value = ahc_get_pci_bus(rahc->dev_softc)
997 - ahc_get_pci_bus(lahc->dev_softc);
998 if (value != 0)
999 break;
1000 if (aic7xxx_reverse_scan != 0)
1001 value = ahc_get_pci_slot(lahc->dev_softc)
1002 - ahc_get_pci_slot(rahc->dev_softc);
1003 else
1004 value = ahc_get_pci_slot(rahc->dev_softc)
1005 - ahc_get_pci_slot(lahc->dev_softc);
1006 if (value != 0)
1007 break;
1008 /*
1009 * On multi-function devices, the user can choose
1010 * to have function 1 probed before function 0.
1011 * Give whichever channel is the primary channel
1012 * the highest priority.
1013 */
1014 primary_channel = (lahc->flags & AHC_PRIMARY_CHANNEL) + 'A';
1015 value = -1;
1016 if (lahc->channel == primary_channel)
1017 value = 1;
1018 break;
1019 }
1020#endif
1021 case AHC_EISA:
1022 if ((rahc->flags & AHC_BIOS_ENABLED) != 0) {
1023 value = rahc->platform_data->bios_address
1024 - lahc->platform_data->bios_address;
1025 } else {
1026 value = rahc->bsh.ioport
1027 - lahc->bsh.ioport;
1028 }
1029 break;
1030 default:
1031 panic("ahc_softc_sort: invalid bus type");
1032 }
1033 return (value);
1034}
1035
1036static void
1037ahc_linux_setup_tag_info_global(char *p)
1038{
1039 int tags, i, j;
1040
1041 tags = simple_strtoul(p + 1, NULL, 0) & 0xff;
1042 printf("Setting Global Tags= %d\n", tags);
1043
1044 for (i = 0; i < NUM_ELEMENTS(aic7xxx_tag_info); i++) {
1045 for (j = 0; j < AHC_NUM_TARGETS; j++) {
1046 aic7xxx_tag_info[i].tag_commands[j] = tags;
1047 }
1048 }
1049}
1050
1051static void
1052ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value)
1053{
1054
1055 if ((instance >= 0) && (targ >= 0)
1056 && (instance < NUM_ELEMENTS(aic7xxx_tag_info))
1057 && (targ < AHC_NUM_TARGETS)) {
1058 aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff;
1059 if (bootverbose)
1060 printf("tag_info[%d:%d] = %d\n", instance, targ, value);
1061 }
1062}
1063
1da177e4
LT
1064/*
1065 * Handle Linux boot parameters. This routine allows for assigning a value
1066 * to a parameter with a ':' between the parameter and the value.
1067 * ie. aic7xxx=stpwlev:1,extended
1068 */
1069static int
1070aic7xxx_setup(char *s)
1071{
1072 int i, n;
1073 char *p;
1074 char *end;
1075
1076 static struct {
1077 const char *name;
1078 uint32_t *flag;
1079 } options[] = {
1080 { "extended", &aic7xxx_extended },
1081 { "no_reset", &aic7xxx_no_reset },
1082 { "verbose", &aic7xxx_verbose },
1083 { "allow_memio", &aic7xxx_allow_memio},
1084#ifdef AHC_DEBUG
1085 { "debug", &ahc_debug },
1086#endif
1087 { "reverse_scan", &aic7xxx_reverse_scan },
1da177e4
LT
1088 { "periodic_otag", &aic7xxx_periodic_otag },
1089 { "pci_parity", &aic7xxx_pci_parity },
1090 { "seltime", &aic7xxx_seltime },
1091 { "tag_info", NULL },
1092 { "global_tag_depth", NULL },
1093 { "dv", NULL }
1094 };
1095
1096 end = strchr(s, '\0');
1097
1098 /*
1099 * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS
1100 * will never be 0 in this case.
1101 */
1102 n = 0;
1103
1104 while ((p = strsep(&s, ",.")) != NULL) {
1105 if (*p == '\0')
1106 continue;
1107 for (i = 0; i < NUM_ELEMENTS(options); i++) {
1108
1109 n = strlen(options[i].name);
1110 if (strncmp(options[i].name, p, n) == 0)
1111 break;
1112 }
1113 if (i == NUM_ELEMENTS(options))
1114 continue;
1115
1116 if (strncmp(p, "global_tag_depth", n) == 0) {
1117 ahc_linux_setup_tag_info_global(p + n);
1118 } else if (strncmp(p, "tag_info", n) == 0) {
1119 s = aic_parse_brace_option("tag_info", p + n, end,
1120 2, ahc_linux_setup_tag_info, 0);
1da177e4
LT
1121 } else if (p[n] == ':') {
1122 *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0);
1123 } else if (strncmp(p, "verbose", n) == 0) {
1124 *(options[i].flag) = 1;
1125 } else {
1126 *(options[i].flag) ^= 0xFFFFFFFF;
1127 }
1128 }
1129 return 1;
1130}
1131
1132__setup("aic7xxx=", aic7xxx_setup);
1133
1134uint32_t aic7xxx_verbose;
1135
1136int
013791ee 1137ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *template)
1da177e4
LT
1138{
1139 char buf[80];
1140 struct Scsi_Host *host;
1141 char *new_name;
1142 u_long s;
1da177e4
LT
1143
1144 template->name = ahc->description;
1145 host = scsi_host_alloc(template, sizeof(struct ahc_softc *));
1146 if (host == NULL)
1147 return (ENOMEM);
1148
1149 *((struct ahc_softc **)host->hostdata) = ahc;
1150 ahc_lock(ahc, &s);
1da177e4 1151 scsi_assign_lock(host, &ahc->platform_data->spin_lock);
1da177e4
LT
1152 ahc->platform_data->host = host;
1153 host->can_queue = AHC_MAX_QUEUE;
1154 host->cmd_per_lun = 2;
1155 /* XXX No way to communicate the ID for multiple channels */
1156 host->this_id = ahc->our_id;
1157 host->irq = ahc->platform_data->irq;
1158 host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8;
1159 host->max_lun = AHC_NUM_LUNS;
1160 host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0;
1161 host->sg_tablesize = AHC_NSEG;
1162 ahc_set_unit(ahc, ahc_linux_next_unit());
1163 sprintf(buf, "scsi%d", host->host_no);
1164 new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
1165 if (new_name != NULL) {
1166 strcpy(new_name, buf);
1167 ahc_set_name(ahc, new_name);
1168 }
1169 host->unique_id = ahc->unit;
1da177e4 1170 ahc_linux_initialize_scsi_bus(ahc);
1da177e4 1171 ahc_intr_enable(ahc, TRUE);
1da177e4
LT
1172 ahc_unlock(ahc, &s);
1173
92d161c3 1174 host->transportt = ahc_linux_transport_template;
1175
1da177e4
LT
1176 scsi_add_host(host, (ahc->dev_softc ? &ahc->dev_softc->dev : NULL)); /* XXX handle failure */
1177 scsi_scan_host(host);
1da177e4
LT
1178 return (0);
1179}
1180
1181uint64_t
1182ahc_linux_get_memsize(void)
1183{
1184 struct sysinfo si;
1185
1186 si_meminfo(&si);
1187 return ((uint64_t)si.totalram << PAGE_SHIFT);
1188}
1189
1190/*
1191 * Find the smallest available unit number to use
1192 * for a new device. We don't just use a static
1193 * count to handle the "repeated hot-(un)plug"
1194 * scenario.
1195 */
1196static int
1197ahc_linux_next_unit(void)
1198{
1199 struct ahc_softc *ahc;
1200 int unit;
1201
1202 unit = 0;
1203retry:
1204 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
1205 if (ahc->unit == unit) {
1206 unit++;
1207 goto retry;
1208 }
1209 }
1210 return (unit);
1211}
1212
1213/*
1214 * Place the SCSI bus into a known state by either resetting it,
1215 * or forcing transfer negotiations on the next command to any
1216 * target.
1217 */
1218void
1219ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc)
1220{
1221 int i;
1222 int numtarg;
1223
1224 i = 0;
1225 numtarg = 0;
1226
1227 if (aic7xxx_no_reset != 0)
1228 ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B);
1229
1230 if ((ahc->flags & AHC_RESET_BUS_A) != 0)
1231 ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE);
1232 else
1233 numtarg = (ahc->features & AHC_WIDE) ? 16 : 8;
1234
1235 if ((ahc->features & AHC_TWIN) != 0) {
1236
1237 if ((ahc->flags & AHC_RESET_BUS_B) != 0) {
1238 ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE);
1239 } else {
1240 if (numtarg == 0)
1241 i = 8;
1242 numtarg += 8;
1243 }
1244 }
1245
1246 /*
1247 * Force negotiation to async for all targets that
1248 * will not see an initial bus reset.
1249 */
1250 for (; i < numtarg; i++) {
1251 struct ahc_devinfo devinfo;
1252 struct ahc_initiator_tinfo *tinfo;
1253 struct ahc_tmode_tstate *tstate;
1254 u_int our_id;
1255 u_int target_id;
1256 char channel;
1257
1258 channel = 'A';
1259 our_id = ahc->our_id;
1260 target_id = i;
1261 if (i > 7 && (ahc->features & AHC_TWIN) != 0) {
1262 channel = 'B';
1263 our_id = ahc->our_id_b;
1264 target_id = i % 8;
1265 }
1266 tinfo = ahc_fetch_transinfo(ahc, channel, our_id,
1267 target_id, &tstate);
1268 ahc_compile_devinfo(&devinfo, our_id, target_id,
1269 CAM_LUN_WILDCARD, channel, ROLE_INITIATOR);
1270 ahc_update_neg_request(ahc, &devinfo, tstate,
1271 tinfo, AHC_NEG_ALWAYS);
1272 }
1273 /* Give the bus some time to recover */
1274 if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) {
1275 ahc_linux_freeze_simq(ahc);
1276 init_timer(&ahc->platform_data->reset_timer);
1277 ahc->platform_data->reset_timer.data = (u_long)ahc;
1278 ahc->platform_data->reset_timer.expires =
1279 jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000;
1280 ahc->platform_data->reset_timer.function =
1281 ahc_linux_release_simq;
1282 add_timer(&ahc->platform_data->reset_timer);
1283 }
1284}
1285
1286int
1287ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1288{
1289
1290 ahc->platform_data =
1291 malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT);
1292 if (ahc->platform_data == NULL)
1293 return (ENOMEM);
1294 memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data));
1da177e4 1295 ahc->platform_data->irq = AHC_LINUX_NOIRQ;
1da177e4 1296 ahc_lockinit(ahc);
1da177e4 1297 init_MUTEX_LOCKED(&ahc->platform_data->eh_sem);
1da177e4
LT
1298 ahc->seltime = (aic7xxx_seltime & 0x3) << 4;
1299 ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4;
1300 if (aic7xxx_pci_parity == 0)
1301 ahc->flags |= AHC_DISABLE_PCI_PERR;
1302
1303 return (0);
1304}
1305
1306void
1307ahc_platform_free(struct ahc_softc *ahc)
1308{
b1abb4d6 1309 struct scsi_target *starget;
1da177e4
LT
1310 int i, j;
1311
1312 if (ahc->platform_data != NULL) {
1da177e4 1313 if (ahc->platform_data->host != NULL) {
1da177e4 1314 scsi_remove_host(ahc->platform_data->host);
1da177e4
LT
1315 scsi_host_put(ahc->platform_data->host);
1316 }
1317
1318 /* destroy all of the device and target objects */
1319 for (i = 0; i < AHC_NUM_TARGETS; i++) {
b1abb4d6
JB
1320 starget = ahc->platform_data->starget[i];
1321 if (starget != NULL) {
1da177e4 1322 for (j = 0; j < AHC_NUM_LUNS; j++) {
b1abb4d6
JB
1323 struct ahc_linux_target *targ =
1324 scsi_transport_target_data(starget);
1da177e4 1325
b1abb4d6 1326 if (targ->sdev[j] == NULL)
1da177e4 1327 continue;
b1abb4d6 1328 targ->sdev[j] = NULL;
1da177e4 1329 }
b1abb4d6 1330 ahc->platform_data->starget[i] = NULL;
1da177e4
LT
1331 }
1332 }
1333
1334 if (ahc->platform_data->irq != AHC_LINUX_NOIRQ)
1335 free_irq(ahc->platform_data->irq, ahc);
1336 if (ahc->tag == BUS_SPACE_PIO
1337 && ahc->bsh.ioport != 0)
1338 release_region(ahc->bsh.ioport, 256);
1339 if (ahc->tag == BUS_SPACE_MEMIO
1340 && ahc->bsh.maddr != NULL) {
1341 iounmap(ahc->bsh.maddr);
1342 release_mem_region(ahc->platform_data->mem_busaddr,
1343 0x1000);
1344 }
dedd8310 1345
1da177e4
LT
1346 free(ahc->platform_data, M_DEVBUF);
1347 }
1348}
1349
1350void
1351ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb)
1352{
1353 ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb),
1354 SCB_GET_CHANNEL(ahc, scb),
1355 SCB_GET_LUN(scb), SCB_LIST_NULL,
1356 ROLE_UNKNOWN, CAM_REQUEUE_REQ);
1357}
1358
1359void
1360ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
1361 ahc_queue_alg alg)
1362{
b1abb4d6
JB
1363 struct scsi_target *starget;
1364 struct ahc_linux_target *targ;
1da177e4 1365 struct ahc_linux_device *dev;
b1abb4d6
JB
1366 struct scsi_device *sdev;
1367 u_int target_offset;
1da177e4
LT
1368 int was_queuing;
1369 int now_queuing;
1370
b1abb4d6
JB
1371 target_offset = devinfo->target;
1372 if (devinfo->channel != 'A')
1373 target_offset += 8;
1374 starget = ahc->platform_data->starget[target_offset];
1375 targ = scsi_transport_target_data(starget);
1376 BUG_ON(targ == NULL);
1377 sdev = targ->sdev[devinfo->lun];
1378 if (sdev == NULL)
1da177e4 1379 return;
b1abb4d6
JB
1380 dev = scsi_transport_device_data(sdev);
1381
1da177e4
LT
1382 was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED);
1383 switch (alg) {
1384 default:
1385 case AHC_QUEUE_NONE:
1386 now_queuing = 0;
1387 break;
1388 case AHC_QUEUE_BASIC:
1389 now_queuing = AHC_DEV_Q_BASIC;
1390 break;
1391 case AHC_QUEUE_TAGGED:
1392 now_queuing = AHC_DEV_Q_TAGGED;
1393 break;
1394 }
1395 if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0
1396 && (was_queuing != now_queuing)
1397 && (dev->active != 0)) {
1398 dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY;
1399 dev->qfrozen++;
1400 }
1401
1402 dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG);
1403 if (now_queuing) {
1404 u_int usertags;
1405
1406 usertags = ahc_linux_user_tagdepth(ahc, devinfo);
1407 if (!was_queuing) {
1408 /*
1409 * Start out agressively and allow our
1410 * dynamic queue depth algorithm to take
1411 * care of the rest.
1412 */
1413 dev->maxtags = usertags;
1414 dev->openings = dev->maxtags - dev->active;
1415 }
1416 if (dev->maxtags == 0) {
1417 /*
1418 * Queueing is disabled by the user.
1419 */
1420 dev->openings = 1;
1421 } else if (alg == AHC_QUEUE_TAGGED) {
1422 dev->flags |= AHC_DEV_Q_TAGGED;
1423 if (aic7xxx_periodic_otag != 0)
1424 dev->flags |= AHC_DEV_PERIODIC_OTAG;
1425 } else
1426 dev->flags |= AHC_DEV_Q_BASIC;
1427 } else {
1428 /* We can only have one opening. */
1429 dev->maxtags = 0;
1430 dev->openings = 1 - dev->active;
1431 }
b1abb4d6
JB
1432 switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) {
1433 case AHC_DEV_Q_BASIC:
1434 scsi_adjust_queue_depth(sdev,
1435 MSG_SIMPLE_TASK,
1436 dev->openings + dev->active);
1437 break;
1438 case AHC_DEV_Q_TAGGED:
1439 scsi_adjust_queue_depth(sdev,
1440 MSG_ORDERED_TASK,
1441 dev->openings + dev->active);
1442 break;
1443 default:
1444 /*
1445 * We allow the OS to queue 2 untagged transactions to
1446 * us at any time even though we can only execute them
1447 * serially on the controller/device. This should
1448 * remove some latency.
1449 */
1450 scsi_adjust_queue_depth(sdev,
1451 /*NON-TAGGED*/0,
1452 /*queue depth*/2);
1453 break;
1da177e4 1454 }
1da177e4
LT
1455}
1456
1457int
1458ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel,
1459 int lun, u_int tag, role_t role, uint32_t status)
1460{
e4e360c3 1461 return 0;
1da177e4
LT
1462}
1463
cb624029 1464static u_int
1465ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
1da177e4 1466{
cb624029 1467 static int warned_user;
1468 u_int tags;
1da177e4 1469
cb624029 1470 tags = 0;
1471 if ((ahc->user_discenable & devinfo->target_mask) != 0) {
1472 if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) {
1473 if (warned_user == 0) {
1da177e4 1474
cb624029 1475 printf(KERN_WARNING
1476"aic7xxx: WARNING: Insufficient tag_info instances\n"
1477"aic7xxx: for installed controllers. Using defaults\n"
1478"aic7xxx: Please update the aic7xxx_tag_info array in\n"
1479"aic7xxx: the aic7xxx_osm..c source file.\n");
1480 warned_user++;
1481 }
1482 tags = AHC_MAX_QUEUE;
1483 } else {
1484 adapter_tag_info_t *tag_info;
1da177e4 1485
cb624029 1486 tag_info = &aic7xxx_tag_info[ahc->unit];
1487 tags = tag_info->tag_commands[devinfo->target_offset];
1488 if (tags > AHC_MAX_QUEUE)
1489 tags = AHC_MAX_QUEUE;
1490 }
1da177e4 1491 }
cb624029 1492 return (tags);
1da177e4
LT
1493}
1494
cb624029 1495/*
1496 * Determines the queue depth for a given device.
1497 */
1da177e4 1498static void
b1abb4d6 1499ahc_linux_device_queue_depth(struct scsi_device *sdev)
1da177e4 1500{
cb624029 1501 struct ahc_devinfo devinfo;
1502 u_int tags;
b1abb4d6 1503 struct ahc_softc *ahc = *((struct ahc_softc **)sdev->host->hostdata);
1da177e4 1504
cb624029 1505 ahc_compile_devinfo(&devinfo,
b1abb4d6 1506 sdev->sdev_target->channel == 0
cb624029 1507 ? ahc->our_id : ahc->our_id_b,
b1abb4d6
JB
1508 sdev->sdev_target->id, sdev->lun,
1509 sdev->sdev_target->channel == 0 ? 'A' : 'B',
cb624029 1510 ROLE_INITIATOR);
1511 tags = ahc_linux_user_tagdepth(ahc, &devinfo);
b1abb4d6 1512 if (tags != 0 && sdev->tagged_supported != 0) {
1da177e4 1513
cb624029 1514 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED);
1515 ahc_print_devinfo(ahc, &devinfo);
1516 printf("Tagged Queuing enabled. Depth %d\n", tags);
1da177e4 1517 } else {
cb624029 1518 ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE);
1da177e4
LT
1519 }
1520}
1521
e4e360c3
JB
1522static int
1523ahc_linux_run_command(struct ahc_softc *ahc, struct ahc_linux_device *dev,
1524 struct scsi_cmnd *cmd)
1da177e4 1525{
cb624029 1526 struct scb *scb;
1527 struct hardware_scb *hscb;
1528 struct ahc_initiator_tinfo *tinfo;
1529 struct ahc_tmode_tstate *tstate;
1530 uint16_t mask;
e4e360c3 1531 struct scb_tailq *untagged_q = NULL;
1da177e4 1532
e4e360c3
JB
1533 /*
1534 * Schedule us to run later. The only reason we are not
1535 * running is because the whole controller Q is frozen.
1536 */
1537 if (ahc->platform_data->qfrozen != 0)
1538 return SCSI_MLQUEUE_HOST_BUSY;
1da177e4 1539
e4e360c3
JB
1540 /*
1541 * We only allow one untagged transaction
1542 * per target in the initiator role unless
1543 * we are storing a full busy target *lun*
1544 * table in SCB space.
1545 */
1546 if (!blk_rq_tagged(cmd->request)
1547 && (ahc->features & AHC_SCB_BTT) == 0) {
1548 int target_offset;
1da177e4 1549
e4e360c3
JB
1550 target_offset = cmd->device->id + cmd->device->channel * 8;
1551 untagged_q = &(ahc->untagged_queues[target_offset]);
1552 if (!TAILQ_EMPTY(untagged_q))
1553 /* if we're already executing an untagged command
1554 * we're busy to another */
1555 return SCSI_MLQUEUE_DEVICE_BUSY;
1556 }
1557
1558 /*
1559 * Get an scb to use.
1560 */
8eb37942
CH
1561 scb = ahc_get_scb(ahc);
1562 if (!scb)
1563 return SCSI_MLQUEUE_HOST_BUSY;
1da177e4 1564
e4e360c3
JB
1565 scb->io_ctx = cmd;
1566 scb->platform_data->dev = dev;
1567 hscb = scb->hscb;
1568 cmd->host_scribble = (char *)scb;
1da177e4 1569
e4e360c3
JB
1570 /*
1571 * Fill out basics of the HSCB.
1572 */
1573 hscb->control = 0;
1574 hscb->scsiid = BUILD_SCSIID(ahc, cmd);
1575 hscb->lun = cmd->device->lun;
1576 mask = SCB_GET_TARGET_MASK(ahc, scb);
1577 tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb),
1578 SCB_GET_OUR_ID(scb),
1579 SCB_GET_TARGET(ahc, scb), &tstate);
1580 hscb->scsirate = tinfo->scsirate;
1581 hscb->scsioffset = tinfo->curr.offset;
1582 if ((tstate->ultraenb & mask) != 0)
1583 hscb->control |= ULTRAENB;
1584
1585 if ((ahc->user_discenable & mask) != 0)
1586 hscb->control |= DISCENB;
1587
1588 if ((tstate->auto_negotiate & mask) != 0) {
1589 scb->flags |= SCB_AUTO_NEGOTIATE;
1590 scb->hscb->control |= MK_MESSAGE;
1591 }
1592
1593 if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) {
e4e360c3
JB
1594 int msg_bytes;
1595 uint8_t tag_msgs[2];
1596
1597 msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs);
1598 if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) {
1599 hscb->control |= tag_msgs[0];
1600 if (tag_msgs[0] == MSG_ORDERED_TASK)
cb624029 1601 dev->commands_since_idle_or_otag = 0;
dedd8310
CH
1602 } else if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH
1603 && (dev->flags & AHC_DEV_Q_TAGGED) != 0) {
e4e360c3
JB
1604 hscb->control |= MSG_ORDERED_TASK;
1605 dev->commands_since_idle_or_otag = 0;
cb624029 1606 } else {
e4e360c3 1607 hscb->control |= MSG_SIMPLE_TASK;
cb624029 1608 }
e4e360c3 1609 }
1da177e4 1610
e4e360c3
JB
1611 hscb->cdb_len = cmd->cmd_len;
1612 if (hscb->cdb_len <= 12) {
1613 memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len);
1614 } else {
1615 memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len);
1616 scb->flags |= SCB_CDB32_PTR;
1617 }
1da177e4 1618
e4e360c3
JB
1619 scb->platform_data->xfer_len = 0;
1620 ahc_set_residual(scb, 0);
1621 ahc_set_sense_residual(scb, 0);
1622 scb->sg_count = 0;
1623 if (cmd->use_sg != 0) {
1624 struct ahc_dma_seg *sg;
1625 struct scatterlist *cur_seg;
1626 struct scatterlist *end_seg;
1627 int nseg;
1628
1629 cur_seg = (struct scatterlist *)cmd->request_buffer;
1630 nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg,
1631 cmd->sc_data_direction);
1632 end_seg = cur_seg + nseg;
1633 /* Copy the segments into the SG list. */
1634 sg = scb->sg_list;
1635 /*
1636 * The sg_count may be larger than nseg if
1637 * a transfer crosses a 32bit page.
1638 */
1639 while (cur_seg < end_seg) {
1da177e4 1640 dma_addr_t addr;
e4e360c3
JB
1641 bus_size_t len;
1642 int consumed;
1643
1644 addr = sg_dma_address(cur_seg);
1645 len = sg_dma_len(cur_seg);
1646 consumed = ahc_linux_map_seg(ahc, scb,
1647 sg, addr, len);
1648 sg += consumed;
1649 scb->sg_count += consumed;
1650 cur_seg++;
1da177e4 1651 }
e4e360c3
JB
1652 sg--;
1653 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1da177e4 1654
e4e360c3
JB
1655 /*
1656 * Reset the sg list pointer.
1657 */
1658 scb->hscb->sgptr =
1659 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
1660
1661 /*
1662 * Copy the first SG into the "current"
1663 * data pointer area.
1664 */
1665 scb->hscb->dataptr = scb->sg_list->addr;
1666 scb->hscb->datacnt = scb->sg_list->len;
1667 } else if (cmd->request_bufflen != 0) {
1668 struct ahc_dma_seg *sg;
1669 dma_addr_t addr;
1670
1671 sg = scb->sg_list;
1672 addr = pci_map_single(ahc->dev_softc,
1673 cmd->request_buffer,
1674 cmd->request_bufflen,
1675 cmd->sc_data_direction);
1676 scb->platform_data->buf_busaddr = addr;
1677 scb->sg_count = ahc_linux_map_seg(ahc, scb,
1678 sg, addr,
1679 cmd->request_bufflen);
1680 sg->len |= ahc_htole32(AHC_DMA_LAST_SEG);
1da177e4
LT
1681
1682 /*
e4e360c3 1683 * Reset the sg list pointer.
1da177e4 1684 */
e4e360c3
JB
1685 scb->hscb->sgptr =
1686 ahc_htole32(scb->sg_list_phys | SG_FULL_RESID);
1687
1688 /*
1689 * Copy the first SG into the "current"
1690 * data pointer area.
1691 */
1692 scb->hscb->dataptr = sg->addr;
1693 scb->hscb->datacnt = sg->len;
1694 } else {
1695 scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL);
1696 scb->hscb->dataptr = 0;
1697 scb->hscb->datacnt = 0;
1698 scb->sg_count = 0;
1699 }
1700
1701 LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1702 dev->openings--;
1703 dev->active++;
1704 dev->commands_issued++;
1705 if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0)
1706 dev->commands_since_idle_or_otag++;
1707
1708 scb->flags |= SCB_ACTIVE;
1709 if (untagged_q) {
1710 TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1711 scb->flags |= SCB_UNTAGGEDQ;
1da177e4 1712 }
e4e360c3
JB
1713 ahc_queue_scb(ahc, scb);
1714 return 0;
1da177e4
LT
1715}
1716
1717/*
1718 * SCSI controller interrupt handler.
1719 */
1720irqreturn_t
1721ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs)
1722{
1723 struct ahc_softc *ahc;
1724 u_long flags;
1725 int ours;
1726
1727 ahc = (struct ahc_softc *) dev_id;
1728 ahc_lock(ahc, &flags);
1729 ours = ahc_intr(ahc);
1da177e4
LT
1730 ahc_unlock(ahc, &flags);
1731 return IRQ_RETVAL(ours);
1732}
1733
1734void
1735ahc_platform_flushwork(struct ahc_softc *ahc)
1736{
1737
1da177e4
LT
1738}
1739
1da177e4
LT
1740void
1741ahc_send_async(struct ahc_softc *ahc, char channel,
1742 u_int target, u_int lun, ac_code code, void *arg)
1743{
1744 switch (code) {
1745 case AC_TRANSFER_NEG:
1746 {
1747 char buf[80];
b1abb4d6 1748 struct scsi_target *starget;
1da177e4
LT
1749 struct ahc_linux_target *targ;
1750 struct info_str info;
1751 struct ahc_initiator_tinfo *tinfo;
1752 struct ahc_tmode_tstate *tstate;
1753 int target_offset;
b1abb4d6
JB
1754 unsigned int target_ppr_options;
1755
1756 BUG_ON(target == CAM_TARGET_WILDCARD);
1da177e4
LT
1757
1758 info.buffer = buf;
1759 info.length = sizeof(buf);
1760 info.offset = 0;
1761 info.pos = 0;
1762 tinfo = ahc_fetch_transinfo(ahc, channel,
1763 channel == 'A' ? ahc->our_id
1764 : ahc->our_id_b,
1765 target, &tstate);
1766
1767 /*
1768 * Don't bother reporting results while
1769 * negotiations are still pending.
1770 */
1771 if (tinfo->curr.period != tinfo->goal.period
1772 || tinfo->curr.width != tinfo->goal.width
1773 || tinfo->curr.offset != tinfo->goal.offset
1774 || tinfo->curr.ppr_options != tinfo->goal.ppr_options)
1775 if (bootverbose == 0)
1776 break;
1777
1778 /*
1779 * Don't bother reporting results that
1780 * are identical to those last reported.
1781 */
1782 target_offset = target;
1783 if (channel == 'B')
1784 target_offset += 8;
b1abb4d6
JB
1785 starget = ahc->platform_data->starget[target_offset];
1786 targ = scsi_transport_target_data(starget);
1da177e4
LT
1787 if (targ == NULL)
1788 break;
b1abb4d6
JB
1789
1790 target_ppr_options =
1791 (spi_dt(starget) ? MSG_EXT_PPR_DT_REQ : 0)
1792 + (spi_qas(starget) ? MSG_EXT_PPR_QAS_REQ : 0)
1793 + (spi_iu(starget) ? MSG_EXT_PPR_IU_REQ : 0);
1794
1795 if (tinfo->curr.period == spi_period(starget)
1796 && tinfo->curr.width == spi_width(starget)
1797 && tinfo->curr.offset == spi_offset(starget)
1798 && tinfo->curr.ppr_options == target_ppr_options)
1da177e4
LT
1799 if (bootverbose == 0)
1800 break;
1801
b1abb4d6
JB
1802 spi_period(starget) = tinfo->curr.period;
1803 spi_width(starget) = tinfo->curr.width;
1804 spi_offset(starget) = tinfo->curr.offset;
1805 spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ;
1806 spi_qas(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ;
1807 spi_iu(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ;
1808 spi_display_xfer_agreement(starget);
1da177e4
LT
1809 break;
1810 }
1811 case AC_SENT_BDR:
1812 {
1da177e4
LT
1813 WARN_ON(lun != CAM_LUN_WILDCARD);
1814 scsi_report_device_reset(ahc->platform_data->host,
1815 channel - 'A', target);
1da177e4
LT
1816 break;
1817 }
1818 case AC_BUS_RESET:
1819 if (ahc->platform_data->host != NULL) {
1820 scsi_report_bus_reset(ahc->platform_data->host,
1821 channel - 'A');
1822 }
1823 break;
1824 default:
1825 panic("ahc_send_async: Unexpected async event");
1826 }
1827}
1828
1829/*
1830 * Calls the higher level scsi done function and frees the scb.
1831 */
1832void
1833ahc_done(struct ahc_softc *ahc, struct scb *scb)
1834{
013791ee 1835 struct scsi_cmnd *cmd;
1da177e4
LT
1836 struct ahc_linux_device *dev;
1837
1838 LIST_REMOVE(scb, pending_links);
1839 if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
1840 struct scb_tailq *untagged_q;
1841 int target_offset;
1842
1843 target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1844 untagged_q = &(ahc->untagged_queues[target_offset]);
1845 TAILQ_REMOVE(untagged_q, scb, links.tqe);
e4e360c3 1846 BUG_ON(!TAILQ_EMPTY(untagged_q));
1da177e4
LT
1847 }
1848
1849 if ((scb->flags & SCB_ACTIVE) == 0) {
1850 printf("SCB %d done'd twice\n", scb->hscb->tag);
1851 ahc_dump_card_state(ahc);
1852 panic("Stopping for safety");
1853 }
1854 cmd = scb->io_ctx;
1855 dev = scb->platform_data->dev;
1856 dev->active--;
1857 dev->openings++;
1858 if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) {
1859 cmd->result &= ~(CAM_DEV_QFRZN << 16);
1860 dev->qfrozen--;
1861 }
1862 ahc_linux_unmap_scb(ahc, scb);
1863
1864 /*
1865 * Guard against stale sense data.
1866 * The Linux mid-layer assumes that sense
1867 * was retrieved anytime the first byte of
1868 * the sense buffer looks "sane".
1869 */
1870 cmd->sense_buffer[0] = 0;
1871 if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) {
1872 uint32_t amount_xferred;
1873
1874 amount_xferred =
1875 ahc_get_transfer_length(scb) - ahc_get_residual(scb);
1876 if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) {
1877#ifdef AHC_DEBUG
1878 if ((ahc_debug & AHC_SHOW_MISC) != 0) {
1879 ahc_print_path(ahc, scb);
1880 printf("Set CAM_UNCOR_PARITY\n");
1881 }
1882#endif
1883 ahc_set_transaction_status(scb, CAM_UNCOR_PARITY);
1884#ifdef AHC_REPORT_UNDERFLOWS
1885 /*
1886 * This code is disabled by default as some
1887 * clients of the SCSI system do not properly
1888 * initialize the underflow parameter. This
1889 * results in spurious termination of commands
1890 * that complete as expected (e.g. underflow is
1891 * allowed as command can return variable amounts
1892 * of data.
1893 */
1894 } else if (amount_xferred < scb->io_ctx->underflow) {
1895 u_int i;
1896
1897 ahc_print_path(ahc, scb);
1898 printf("CDB:");
1899 for (i = 0; i < scb->io_ctx->cmd_len; i++)
1900 printf(" 0x%x", scb->io_ctx->cmnd[i]);
1901 printf("\n");
1902 ahc_print_path(ahc, scb);
1903 printf("Saw underflow (%ld of %ld bytes). "
1904 "Treated as error\n",
1905 ahc_get_residual(scb),
1906 ahc_get_transfer_length(scb));
1907 ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
1908#endif
1909 } else {
1910 ahc_set_transaction_status(scb, CAM_REQ_CMP);
1911 }
1912 } else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) {
b1abb4d6 1913 ahc_linux_handle_scsi_status(ahc, cmd->device, scb);
1da177e4 1914 }
1da177e4
LT
1915
1916 if (dev->openings == 1
1917 && ahc_get_transaction_status(scb) == CAM_REQ_CMP
1918 && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL)
1919 dev->tag_success_count++;
1920 /*
1921 * Some devices deal with temporary internal resource
1922 * shortages by returning queue full. When the queue
1923 * full occurrs, we throttle back. Slowly try to get
1924 * back to our previous queue depth.
1925 */
1926 if ((dev->openings + dev->active) < dev->maxtags
1927 && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) {
1928 dev->tag_success_count = 0;
1929 dev->openings++;
1930 }
1931
1932 if (dev->active == 0)
1933 dev->commands_since_idle_or_otag = 0;
1934
1da177e4
LT
1935 if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
1936 printf("Recovery SCB completes\n");
1937 if (ahc_get_transaction_status(scb) == CAM_BDR_SENT
1938 || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED)
1939 ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT);
1940 if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
1941 ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
1942 up(&ahc->platform_data->eh_sem);
1943 }
1944 }
1945
1946 ahc_free_scb(ahc, scb);
1947 ahc_linux_queue_cmd_complete(ahc, cmd);
1da177e4
LT
1948}
1949
1950static void
1951ahc_linux_handle_scsi_status(struct ahc_softc *ahc,
b1abb4d6 1952 struct scsi_device *sdev, struct scb *scb)
1da177e4
LT
1953{
1954 struct ahc_devinfo devinfo;
b1abb4d6 1955 struct ahc_linux_device *dev = scsi_transport_device_data(sdev);
1da177e4
LT
1956
1957 ahc_compile_devinfo(&devinfo,
1958 ahc->our_id,
b1abb4d6
JB
1959 sdev->sdev_target->id, sdev->lun,
1960 sdev->sdev_target->channel == 0 ? 'A' : 'B',
1da177e4
LT
1961 ROLE_INITIATOR);
1962
1963 /*
1964 * We don't currently trust the mid-layer to
1965 * properly deal with queue full or busy. So,
1966 * when one occurs, we tell the mid-layer to
1967 * unconditionally requeue the command to us
1968 * so that we can retry it ourselves. We also
1969 * implement our own throttling mechanism so
1970 * we don't clobber the device with too many
1971 * commands.
1972 */
1973 switch (ahc_get_scsi_status(scb)) {
1974 default:
1975 break;
1976 case SCSI_STATUS_CHECK_COND:
1977 case SCSI_STATUS_CMD_TERMINATED:
1978 {
013791ee 1979 struct scsi_cmnd *cmd;
1da177e4
LT
1980
1981 /*
1982 * Copy sense information to the OS's cmd
1983 * structure if it is available.
1984 */
1985 cmd = scb->io_ctx;
1986 if (scb->flags & SCB_SENSE) {
1987 u_int sense_size;
1988
1989 sense_size = MIN(sizeof(struct scsi_sense_data)
1990 - ahc_get_sense_residual(scb),
1991 sizeof(cmd->sense_buffer));
1992 memcpy(cmd->sense_buffer,
1993 ahc_get_sense_buf(ahc, scb), sense_size);
1994 if (sense_size < sizeof(cmd->sense_buffer))
1995 memset(&cmd->sense_buffer[sense_size], 0,
1996 sizeof(cmd->sense_buffer) - sense_size);
1997 cmd->result |= (DRIVER_SENSE << 24);
1998#ifdef AHC_DEBUG
1999 if (ahc_debug & AHC_SHOW_SENSE) {
2000 int i;
2001
2002 printf("Copied %d bytes of sense data:",
2003 sense_size);
2004 for (i = 0; i < sense_size; i++) {
2005 if ((i & 0xF) == 0)
2006 printf("\n");
2007 printf("0x%x ", cmd->sense_buffer[i]);
2008 }
2009 printf("\n");
2010 }
2011#endif
2012 }
2013 break;
2014 }
2015 case SCSI_STATUS_QUEUE_FULL:
2016 {
2017 /*
2018 * By the time the core driver has returned this
2019 * command, all other commands that were queued
2020 * to us but not the device have been returned.
2021 * This ensures that dev->active is equal to
2022 * the number of commands actually queued to
2023 * the device.
2024 */
2025 dev->tag_success_count = 0;
2026 if (dev->active != 0) {
2027 /*
2028 * Drop our opening count to the number
2029 * of commands currently outstanding.
2030 */
2031 dev->openings = 0;
2032/*
2033 ahc_print_path(ahc, scb);
2034 printf("Dropping tag count to %d\n", dev->active);
2035 */
2036 if (dev->active == dev->tags_on_last_queuefull) {
2037
2038 dev->last_queuefull_same_count++;
2039 /*
2040 * If we repeatedly see a queue full
2041 * at the same queue depth, this
2042 * device has a fixed number of tag
2043 * slots. Lock in this tag depth
2044 * so we stop seeing queue fulls from
2045 * this device.
2046 */
2047 if (dev->last_queuefull_same_count
2048 == AHC_LOCK_TAGS_COUNT) {
2049 dev->maxtags = dev->active;
2050 ahc_print_path(ahc, scb);
2051 printf("Locking max tag count at %d\n",
2052 dev->active);
2053 }
2054 } else {
2055 dev->tags_on_last_queuefull = dev->active;
2056 dev->last_queuefull_same_count = 0;
2057 }
2058 ahc_set_transaction_status(scb, CAM_REQUEUE_REQ);
2059 ahc_set_scsi_status(scb, SCSI_STATUS_OK);
2060 ahc_platform_set_tags(ahc, &devinfo,
2061 (dev->flags & AHC_DEV_Q_BASIC)
2062 ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
2063 break;
2064 }
2065 /*
2066 * Drop down to a single opening, and treat this
2067 * as if the target returned BUSY SCSI status.
2068 */
2069 dev->openings = 1;
2070 ahc_set_scsi_status(scb, SCSI_STATUS_BUSY);
2071 ahc_platform_set_tags(ahc, &devinfo,
2072 (dev->flags & AHC_DEV_Q_BASIC)
2073 ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED);
1da177e4
LT
2074 break;
2075 }
2076 }
2077}
2078
2079static void
013791ee 2080ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, struct scsi_cmnd *cmd)
1da177e4 2081{
1da177e4
LT
2082 /*
2083 * Map CAM error codes into Linux Error codes. We
2084 * avoid the conversion so that the DV code has the
2085 * full error information available when making
2086 * state change decisions.
2087 */
cb624029 2088 {
1da177e4
LT
2089 u_int new_status;
2090
2091 switch (ahc_cmd_get_transaction_status(cmd)) {
2092 case CAM_REQ_INPROG:
2093 case CAM_REQ_CMP:
2094 case CAM_SCSI_STATUS_ERROR:
2095 new_status = DID_OK;
2096 break;
2097 case CAM_REQ_ABORTED:
2098 new_status = DID_ABORT;
2099 break;
2100 case CAM_BUSY:
2101 new_status = DID_BUS_BUSY;
2102 break;
2103 case CAM_REQ_INVALID:
2104 case CAM_PATH_INVALID:
2105 new_status = DID_BAD_TARGET;
2106 break;
2107 case CAM_SEL_TIMEOUT:
2108 new_status = DID_NO_CONNECT;
2109 break;
2110 case CAM_SCSI_BUS_RESET:
2111 case CAM_BDR_SENT:
2112 new_status = DID_RESET;
2113 break;
2114 case CAM_UNCOR_PARITY:
2115 new_status = DID_PARITY;
2116 break;
2117 case CAM_CMD_TIMEOUT:
2118 new_status = DID_TIME_OUT;
2119 break;
2120 case CAM_UA_ABORT:
2121 case CAM_REQ_CMP_ERR:
2122 case CAM_AUTOSENSE_FAIL:
2123 case CAM_NO_HBA:
2124 case CAM_DATA_RUN_ERR:
2125 case CAM_UNEXP_BUSFREE:
2126 case CAM_SEQUENCE_FAIL:
2127 case CAM_CCB_LEN_ERR:
2128 case CAM_PROVIDE_FAIL:
2129 case CAM_REQ_TERMIO:
2130 case CAM_UNREC_HBA_ERROR:
2131 case CAM_REQ_TOO_BIG:
2132 new_status = DID_ERROR;
2133 break;
2134 case CAM_REQUEUE_REQ:
8e45ebcc 2135 new_status = DID_REQUEUE;
1da177e4
LT
2136 break;
2137 default:
2138 /* We should never get here */
2139 new_status = DID_ERROR;
2140 break;
2141 }
2142
2143 ahc_cmd_set_transaction_status(cmd, new_status);
2144 }
2145
8e45ebcc 2146 cmd->scsi_done(cmd);
1da177e4
LT
2147}
2148
1da177e4
LT
2149static void
2150ahc_linux_sem_timeout(u_long arg)
2151{
2152 struct ahc_softc *ahc;
2153 u_long s;
2154
2155 ahc = (struct ahc_softc *)arg;
2156
2157 ahc_lock(ahc, &s);
2158 if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) {
2159 ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE;
2160 up(&ahc->platform_data->eh_sem);
2161 }
2162 ahc_unlock(ahc, &s);
2163}
2164
2165static void
2166ahc_linux_freeze_simq(struct ahc_softc *ahc)
2167{
2168 ahc->platform_data->qfrozen++;
2169 if (ahc->platform_data->qfrozen == 1) {
2170 scsi_block_requests(ahc->platform_data->host);
2171
2172 /* XXX What about Twin channels? */
2173 ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
2174 CAM_LUN_WILDCARD, SCB_LIST_NULL,
2175 ROLE_INITIATOR, CAM_REQUEUE_REQ);
2176 }
2177}
2178
2179static void
2180ahc_linux_release_simq(u_long arg)
2181{
2182 struct ahc_softc *ahc;
2183 u_long s;
2184 int unblock_reqs;
2185
2186 ahc = (struct ahc_softc *)arg;
2187
2188 unblock_reqs = 0;
2189 ahc_lock(ahc, &s);
2190 if (ahc->platform_data->qfrozen > 0)
2191 ahc->platform_data->qfrozen--;
2192 if (ahc->platform_data->qfrozen == 0)
2193 unblock_reqs = 1;
1da177e4
LT
2194 ahc_unlock(ahc, &s);
2195 /*
2196 * There is still a race here. The mid-layer
2197 * should keep its own freeze count and use
2198 * a bottom half handler to run the queues
2199 * so we can unblock with our own lock held.
2200 */
2201 if (unblock_reqs)
2202 scsi_unblock_requests(ahc->platform_data->host);
2203}
2204
1da177e4 2205static int
013791ee 2206ahc_linux_queue_recovery_cmd(struct scsi_cmnd *cmd, scb_flag flag)
1da177e4
LT
2207{
2208 struct ahc_softc *ahc;
1da177e4
LT
2209 struct ahc_linux_device *dev;
2210 struct scb *pending_scb;
1da177e4
LT
2211 u_int saved_scbptr;
2212 u_int active_scb_index;
2213 u_int last_phase;
2214 u_int saved_scsiid;
2215 u_int cdb_byte;
2216 int retval;
2217 int was_paused;
2218 int paused;
2219 int wait;
2220 int disconnected;
2221
2222 pending_scb = NULL;
2223 paused = FALSE;
2224 wait = FALSE;
2225 ahc = *(struct ahc_softc **)cmd->device->host->hostdata;
1da177e4
LT
2226
2227 printf("%s:%d:%d:%d: Attempting to queue a%s message\n",
2228 ahc_name(ahc), cmd->device->channel,
2229 cmd->device->id, cmd->device->lun,
2230 flag == SCB_ABORT ? "n ABORT" : " TARGET RESET");
2231
2232 printf("CDB:");
2233 for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++)
2234 printf(" 0x%x", cmd->cmnd[cdb_byte]);
2235 printf("\n");
2236
1da177e4
LT
2237 /*
2238 * First determine if we currently own this command.
2239 * Start by searching the device queue. If not found
2240 * there, check the pending_scb list. If not found
2241 * at all, and the system wanted us to just abort the
2242 * command, return success.
2243 */
b1abb4d6 2244 dev = scsi_transport_device_data(cmd->device);
1da177e4
LT
2245
2246 if (dev == NULL) {
2247 /*
2248 * No target device for this command exists,
2249 * so we must not still own the command.
2250 */
2251 printf("%s:%d:%d:%d: Is not an active device\n",
2252 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2253 cmd->device->lun);
2254 retval = SUCCESS;
2255 goto no_cmd;
2256 }
2257
1da177e4
LT
2258 if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0
2259 && ahc_search_untagged_queues(ahc, cmd, cmd->device->id,
2260 cmd->device->channel + 'A',
2261 cmd->device->lun,
2262 CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) {
2263 printf("%s:%d:%d:%d: Command found on untagged queue\n",
2264 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2265 cmd->device->lun);
2266 retval = SUCCESS;
2267 goto done;
2268 }
2269
2270 /*
2271 * See if we can find a matching cmd in the pending list.
2272 */
2273 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2274 if (pending_scb->io_ctx == cmd)
2275 break;
2276 }
2277
2278 if (pending_scb == NULL && flag == SCB_DEVICE_RESET) {
2279
2280 /* Any SCB for this device will do for a target reset */
2281 LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
2282 if (ahc_match_scb(ahc, pending_scb, cmd->device->id,
2283 cmd->device->channel + 'A',
2284 CAM_LUN_WILDCARD,
2285 SCB_LIST_NULL, ROLE_INITIATOR) == 0)
2286 break;
2287 }
2288 }
2289
2290 if (pending_scb == NULL) {
2291 printf("%s:%d:%d:%d: Command not found\n",
2292 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2293 cmd->device->lun);
2294 goto no_cmd;
2295 }
2296
2297 if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) {
2298 /*
2299 * We can't queue two recovery actions using the same SCB
2300 */
2301 retval = FAILED;
2302 goto done;
2303 }
2304
2305 /*
2306 * Ensure that the card doesn't do anything
2307 * behind our back and that we didn't "just" miss
2308 * an interrupt that would affect this cmd.
2309 */
2310 was_paused = ahc_is_paused(ahc);
2311 ahc_pause_and_flushwork(ahc);
2312 paused = TRUE;
2313
2314 if ((pending_scb->flags & SCB_ACTIVE) == 0) {
2315 printf("%s:%d:%d:%d: Command already completed\n",
2316 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2317 cmd->device->lun);
2318 goto no_cmd;
2319 }
2320
2321 printf("%s: At time of recovery, card was %spaused\n",
2322 ahc_name(ahc), was_paused ? "" : "not ");
2323 ahc_dump_card_state(ahc);
2324
2325 disconnected = TRUE;
2326 if (flag == SCB_ABORT) {
2327 if (ahc_search_qinfifo(ahc, cmd->device->id,
2328 cmd->device->channel + 'A',
2329 cmd->device->lun,
2330 pending_scb->hscb->tag,
2331 ROLE_INITIATOR, CAM_REQ_ABORTED,
2332 SEARCH_COMPLETE) > 0) {
2333 printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n",
2334 ahc_name(ahc), cmd->device->channel,
2335 cmd->device->id, cmd->device->lun);
2336 retval = SUCCESS;
2337 goto done;
2338 }
2339 } else if (ahc_search_qinfifo(ahc, cmd->device->id,
2340 cmd->device->channel + 'A',
2341 cmd->device->lun, pending_scb->hscb->tag,
2342 ROLE_INITIATOR, /*status*/0,
2343 SEARCH_COUNT) > 0) {
2344 disconnected = FALSE;
2345 }
2346
2347 if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) {
2348 struct scb *bus_scb;
2349
2350 bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG));
2351 if (bus_scb == pending_scb)
2352 disconnected = FALSE;
2353 else if (flag != SCB_ABORT
2354 && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid
2355 && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb))
2356 disconnected = FALSE;
2357 }
2358
2359 /*
2360 * At this point, pending_scb is the scb associated with the
2361 * passed in command. That command is currently active on the
2362 * bus, is in the disconnected state, or we're hoping to find
2363 * a command for the same target active on the bus to abuse to
2364 * send a BDR. Queue the appropriate message based on which of
2365 * these states we are in.
2366 */
2367 last_phase = ahc_inb(ahc, LASTPHASE);
2368 saved_scbptr = ahc_inb(ahc, SCBPTR);
2369 active_scb_index = ahc_inb(ahc, SCB_TAG);
2370 saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
2371 if (last_phase != P_BUSFREE
2372 && (pending_scb->hscb->tag == active_scb_index
2373 || (flag == SCB_DEVICE_RESET
2374 && SCSIID_TARGET(ahc, saved_scsiid) == cmd->device->id))) {
2375
2376 /*
2377 * We're active on the bus, so assert ATN
2378 * and hope that the target responds.
2379 */
2380 pending_scb = ahc_lookup_scb(ahc, active_scb_index);
2381 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2382 ahc_outb(ahc, MSG_OUT, HOST_MSG);
2383 ahc_outb(ahc, SCSISIGO, last_phase|ATNO);
2384 printf("%s:%d:%d:%d: Device is active, asserting ATN\n",
2385 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2386 cmd->device->lun);
2387 wait = TRUE;
2388 } else if (disconnected) {
2389
2390 /*
2391 * Actually re-queue this SCB in an attempt
2392 * to select the device before it reconnects.
2393 * In either case (selection or reselection),
2394 * we will now issue the approprate message
2395 * to the timed-out device.
2396 *
2397 * Set the MK_MESSAGE control bit indicating
2398 * that we desire to send a message. We
2399 * also set the disconnected flag since
2400 * in the paging case there is no guarantee
2401 * that our SCB control byte matches the
2402 * version on the card. We don't want the
2403 * sequencer to abort the command thinking
2404 * an unsolicited reselection occurred.
2405 */
2406 pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED;
2407 pending_scb->flags |= SCB_RECOVERY_SCB|flag;
2408
2409 /*
2410 * Remove any cached copy of this SCB in the
2411 * disconnected list in preparation for the
2412 * queuing of our abort SCB. We use the
2413 * same element in the SCB, SCB_NEXT, for
2414 * both the qinfifo and the disconnected list.
2415 */
2416 ahc_search_disc_list(ahc, cmd->device->id,
2417 cmd->device->channel + 'A',
2418 cmd->device->lun, pending_scb->hscb->tag,
2419 /*stop_on_first*/TRUE,
2420 /*remove*/TRUE,
2421 /*save_state*/FALSE);
2422
2423 /*
2424 * In the non-paging case, the sequencer will
2425 * never re-reference the in-core SCB.
2426 * To make sure we are notified during
2427 * reslection, set the MK_MESSAGE flag in
2428 * the card's copy of the SCB.
2429 */
2430 if ((ahc->flags & AHC_PAGESCBS) == 0) {
2431 ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag);
2432 ahc_outb(ahc, SCB_CONTROL,
2433 ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE);
2434 }
2435
2436 /*
2437 * Clear out any entries in the QINFIFO first
2438 * so we are the next SCB for this target
2439 * to run.
2440 */
2441 ahc_search_qinfifo(ahc, cmd->device->id,
2442 cmd->device->channel + 'A',
2443 cmd->device->lun, SCB_LIST_NULL,
2444 ROLE_INITIATOR, CAM_REQUEUE_REQ,
2445 SEARCH_COMPLETE);
2446 ahc_qinfifo_requeue_tail(ahc, pending_scb);
2447 ahc_outb(ahc, SCBPTR, saved_scbptr);
2448 ahc_print_path(ahc, pending_scb);
2449 printf("Device is disconnected, re-queuing SCB\n");
2450 wait = TRUE;
2451 } else {
2452 printf("%s:%d:%d:%d: Unable to deliver message\n",
2453 ahc_name(ahc), cmd->device->channel, cmd->device->id,
2454 cmd->device->lun);
2455 retval = FAILED;
2456 goto done;
2457 }
2458
2459no_cmd:
2460 /*
2461 * Our assumption is that if we don't have the command, no
2462 * recovery action was required, so we return success. Again,
2463 * the semantics of the mid-layer recovery engine are not
2464 * well defined, so this may change in time.
2465 */
2466 retval = SUCCESS;
2467done:
2468 if (paused)
2469 ahc_unpause(ahc);
2470 if (wait) {
2471 struct timer_list timer;
2472 int ret;
2473
2474 ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE;
2475 spin_unlock_irq(&ahc->platform_data->spin_lock);
2476 init_timer(&timer);
2477 timer.data = (u_long)ahc;
2478 timer.expires = jiffies + (5 * HZ);
2479 timer.function = ahc_linux_sem_timeout;
2480 add_timer(&timer);
2481 printf("Recovery code sleeping\n");
2482 down(&ahc->platform_data->eh_sem);
2483 printf("Recovery code awake\n");
2484 ret = del_timer_sync(&timer);
2485 if (ret == 0) {
2486 printf("Timer Expired\n");
2487 retval = FAILED;
2488 }
2489 spin_lock_irq(&ahc->platform_data->spin_lock);
2490 }
1da177e4
LT
2491 return (retval);
2492}
2493
2494void
2495ahc_platform_dump_card_state(struct ahc_softc *ahc)
2496{
1da177e4
LT
2497}
2498
2499static void ahc_linux_exit(void);
2500
fad01ef8
JB
2501static void ahc_linux_set_width(struct scsi_target *starget, int width)
2502{
2503 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2504 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2505 struct ahc_devinfo devinfo;
2506 unsigned long flags;
2507
2508 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2509 starget->channel + 'A', ROLE_INITIATOR);
2510 ahc_lock(ahc, &flags);
2511 ahc_set_width(ahc, &devinfo, width, AHC_TRANS_GOAL, FALSE);
2512 ahc_unlock(ahc, &flags);
2513}
2514
92d161c3 2515static void ahc_linux_set_period(struct scsi_target *starget, int period)
2516{
2517 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2518 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2519 struct ahc_tmode_tstate *tstate;
2520 struct ahc_initiator_tinfo *tinfo
2521 = ahc_fetch_transinfo(ahc,
2522 starget->channel + 'A',
2523 shost->this_id, starget->id, &tstate);
2524 struct ahc_devinfo devinfo;
597487b9 2525 unsigned int ppr_options = tinfo->goal.ppr_options;
92d161c3 2526 unsigned long flags;
597487b9 2527 unsigned long offset = tinfo->goal.offset;
92d161c3 2528 struct ahc_syncrate *syncrate;
2529
2530 if (offset == 0)
2531 offset = MAX_OFFSET;
2532
2bf2c568
JB
2533 if (period < 9)
2534 period = 9; /* 12.5ns is our minimum */
2535 if (period == 9)
2536 ppr_options |= MSG_EXT_PPR_DT_REQ;
2537
92d161c3 2538 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2539 starget->channel + 'A', ROLE_INITIATOR);
fad01ef8
JB
2540
2541 /* all PPR requests apart from QAS require wide transfers */
2542 if (ppr_options & ~MSG_EXT_PPR_QAS_REQ) {
fad01ef8
JB
2543 if (spi_width(starget) == 0)
2544 ppr_options &= MSG_EXT_PPR_QAS_REQ;
2545 }
2546
92d161c3 2547 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
2548 ahc_lock(ahc, &flags);
2549 ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
2550 ppr_options, AHC_TRANS_GOAL, FALSE);
2551 ahc_unlock(ahc, &flags);
2552}
2553
92d161c3 2554static void ahc_linux_set_offset(struct scsi_target *starget, int offset)
2555{
2556 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2557 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2558 struct ahc_tmode_tstate *tstate;
2559 struct ahc_initiator_tinfo *tinfo
2560 = ahc_fetch_transinfo(ahc,
2561 starget->channel + 'A',
2562 shost->this_id, starget->id, &tstate);
2563 struct ahc_devinfo devinfo;
2564 unsigned int ppr_options = 0;
2565 unsigned int period = 0;
2566 unsigned long flags;
2567 struct ahc_syncrate *syncrate = NULL;
2568
2569 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2570 starget->channel + 'A', ROLE_INITIATOR);
2571 if (offset != 0) {
2572 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
597487b9
JB
2573 period = tinfo->goal.period;
2574 ppr_options = tinfo->goal.ppr_options;
92d161c3 2575 }
2576 ahc_lock(ahc, &flags);
2577 ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset,
2578 ppr_options, AHC_TRANS_GOAL, FALSE);
2579 ahc_unlock(ahc, &flags);
2580}
2581
92d161c3 2582static void ahc_linux_set_dt(struct scsi_target *starget, int dt)
2583{
2584 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2585 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2586 struct ahc_tmode_tstate *tstate;
2587 struct ahc_initiator_tinfo *tinfo
2588 = ahc_fetch_transinfo(ahc,
2589 starget->channel + 'A',
2590 shost->this_id, starget->id, &tstate);
2591 struct ahc_devinfo devinfo;
597487b9 2592 unsigned int ppr_options = tinfo->goal.ppr_options
92d161c3 2593 & ~MSG_EXT_PPR_DT_REQ;
597487b9 2594 unsigned int period = tinfo->goal.period;
92d161c3 2595 unsigned long flags;
2596 struct ahc_syncrate *syncrate;
2597
2bf2c568
JB
2598 if (dt) {
2599 period = 9; /* 12.5ns is the only period valid for DT */
2600 ppr_options |= MSG_EXT_PPR_DT_REQ;
2601 } else if (period == 9)
2602 period = 10; /* if resetting DT, period must be >= 25ns */
2603
92d161c3 2604 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2605 starget->channel + 'A', ROLE_INITIATOR);
fad01ef8 2606 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options,AHC_SYNCRATE_DT);
92d161c3 2607 ahc_lock(ahc, &flags);
597487b9 2608 ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
92d161c3 2609 ppr_options, AHC_TRANS_GOAL, FALSE);
2610 ahc_unlock(ahc, &flags);
2611}
2612
92d161c3 2613static void ahc_linux_set_qas(struct scsi_target *starget, int qas)
2614{
2615 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2616 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2617 struct ahc_tmode_tstate *tstate;
2618 struct ahc_initiator_tinfo *tinfo
2619 = ahc_fetch_transinfo(ahc,
2620 starget->channel + 'A',
2621 shost->this_id, starget->id, &tstate);
2622 struct ahc_devinfo devinfo;
597487b9 2623 unsigned int ppr_options = tinfo->goal.ppr_options
92d161c3 2624 & ~MSG_EXT_PPR_QAS_REQ;
597487b9 2625 unsigned int period = tinfo->goal.period;
92d161c3 2626 unsigned long flags;
2627 struct ahc_syncrate *syncrate;
2628
2629 if (qas)
2630 ppr_options |= MSG_EXT_PPR_QAS_REQ;
2631
2632 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2633 starget->channel + 'A', ROLE_INITIATOR);
fad01ef8 2634 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
92d161c3 2635 ahc_lock(ahc, &flags);
597487b9 2636 ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
92d161c3 2637 ppr_options, AHC_TRANS_GOAL, FALSE);
2638 ahc_unlock(ahc, &flags);
2639}
2640
92d161c3 2641static void ahc_linux_set_iu(struct scsi_target *starget, int iu)
2642{
2643 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2644 struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata);
2645 struct ahc_tmode_tstate *tstate;
2646 struct ahc_initiator_tinfo *tinfo
2647 = ahc_fetch_transinfo(ahc,
2648 starget->channel + 'A',
2649 shost->this_id, starget->id, &tstate);
2650 struct ahc_devinfo devinfo;
597487b9 2651 unsigned int ppr_options = tinfo->goal.ppr_options
92d161c3 2652 & ~MSG_EXT_PPR_IU_REQ;
597487b9 2653 unsigned int period = tinfo->goal.period;
92d161c3 2654 unsigned long flags;
2655 struct ahc_syncrate *syncrate;
2656
2657 if (iu)
2658 ppr_options |= MSG_EXT_PPR_IU_REQ;
2659
2660 ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0,
2661 starget->channel + 'A', ROLE_INITIATOR);
fad01ef8 2662 syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT);
92d161c3 2663 ahc_lock(ahc, &flags);
597487b9 2664 ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->goal.offset,
92d161c3 2665 ppr_options, AHC_TRANS_GOAL, FALSE);
2666 ahc_unlock(ahc, &flags);
2667}
2668
2669static struct spi_function_template ahc_linux_transport_functions = {
92d161c3 2670 .set_offset = ahc_linux_set_offset,
2671 .show_offset = 1,
92d161c3 2672 .set_period = ahc_linux_set_period,
2673 .show_period = 1,
92d161c3 2674 .set_width = ahc_linux_set_width,
2675 .show_width = 1,
92d161c3 2676 .set_dt = ahc_linux_set_dt,
2677 .show_dt = 1,
92d161c3 2678 .set_iu = ahc_linux_set_iu,
2679 .show_iu = 1,
92d161c3 2680 .set_qas = ahc_linux_set_qas,
2681 .show_qas = 1,
2682};
2683
2684
2685
1da177e4
LT
2686static int __init
2687ahc_linux_init(void)
2688{
92d161c3 2689 ahc_linux_transport_template = spi_attach_transport(&ahc_linux_transport_functions);
2690 if (!ahc_linux_transport_template)
2691 return -ENODEV;
b1abb4d6
JB
2692 scsi_transport_reserve_target(ahc_linux_transport_template,
2693 sizeof(struct ahc_linux_target));
2694 scsi_transport_reserve_device(ahc_linux_transport_template,
2695 sizeof(struct ahc_linux_device));
858eaca1
JB
2696 if (ahc_linux_detect(&aic7xxx_driver_template))
2697 return 0;
92d161c3 2698 spi_release_transport(ahc_linux_transport_template);
1da177e4
LT
2699 ahc_linux_exit();
2700 return -ENODEV;
1da177e4
LT
2701}
2702
2703static void
2704ahc_linux_exit(void)
2705{
1da177e4
LT
2706 ahc_linux_pci_exit();
2707 ahc_linux_eisa_exit();
92d161c3 2708 spi_release_transport(ahc_linux_transport_template);
1da177e4
LT
2709}
2710
2711module_init(ahc_linux_init);
2712module_exit(ahc_linux_exit);
This page took 0.153448 seconds and 5 git commands to generate.