Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / scsi / eata.c
blob9436e6a6519adbe247a4d3f6b80c6fd72f985fb1
1 /*
2 * eata.c - Low-level driver for EATA/DMA SCSI host adapters.
4 * 22 Nov 2000 Rev. 6.02 for linux 2.4.0-test11
5 * + Return code checked when calling pci_enable_device.
6 * + Removed old scsi error handling support.
7 * + The obsolete boot option flag eh:n is silently ignored.
8 * + Removed error messages while a disk drive is powered up at
9 * boot time.
10 * + Improved boot messages: all tagged capable device are
11 * indicated as "tagged" or "soft-tagged" :
12 * - "soft-tagged" means that the driver is trying to do its
13 * own tagging (i.e. the tc:y option is in effect);
14 * - "tagged" means that the device supports tagged commands,
15 * but the driver lets the HBA be responsible for tagging
16 * support.
18 * 16 Sep 1999 Rev. 5.11 for linux 2.2.12 and 2.3.18
19 * + Updated to the new __setup interface for boot command line options.
20 * + When loaded as a module, accepts the new parameter boot_options
21 * which value is a string with the same format of the kernel boot
22 * command line options. A valid example is:
23 * modprobe eata boot_options=\"0x7410,0x230,lc:y,tc:n,mq:4\"
25 * 9 Sep 1999 Rev. 5.10 for linux 2.2.12 and 2.3.17
26 * + 64bit cleanup for Linux/Alpha platform support
27 * (contribution from H.J. Lu).
29 * 22 Jul 1999 Rev. 5.00 for linux 2.2.10 and 2.3.11
30 * + Removed pre-2.2 source code compatibility.
31 * + Added call to pci_set_master.
33 * 26 Jul 1998 Rev. 4.33 for linux 2.0.35 and 2.1.111
34 * + Added command line option (rs:[y|n]) to reverse the scan order
35 * of PCI boards. The default is rs:y, which reverses the BIOS order
36 * while registering PCI boards. The default value rs:y generates
37 * the same order of all previous revisions of this driver.
38 * Pls. note that "BIOS order" might have been reversed itself
39 * after the 2.1.9x PCI modifications in the linux kernel.
40 * The rs value is ignored when the explicit list of addresses
41 * is used by the "eata=port0,port1,..." command line option.
42 * + Added command line option (et:[y|n]) to force use of extended
43 * translation (255 heads, 63 sectors) as disk geometry.
44 * The default is et:n, which uses the disk geometry returned
45 * by scsicam_bios_param. The default value et:n is compatible with
46 * all previous revisions of this driver.
48 * 28 May 1998 Rev. 4.32 for linux 2.0.33 and 2.1.104
49 * Increased busy timeout from 10 msec. to 200 msec. while
50 * processing interrupts.
52 * 16 May 1998 Rev. 4.31 for linux 2.0.33 and 2.1.102
53 * Improved abort handling during the eh recovery process.
55 * 13 May 1998 Rev. 4.30 for linux 2.0.33 and 2.1.101
56 * The driver is now fully SMP safe, including the
57 * abort and reset routines.
58 * Added command line options (eh:[y|n]) to choose between
59 * new_eh_code and the old scsi code.
60 * If linux version >= 2.1.101 the default is eh:y, while the eh
61 * option is ignored for previous releases and the old scsi code
62 * is used.
64 * 18 Apr 1998 Rev. 4.20 for linux 2.0.33 and 2.1.97
65 * Reworked interrupt handler.
67 * 11 Apr 1998 rev. 4.05 for linux 2.0.33 and 2.1.95
68 * Major reliability improvement: when a batch with overlapping
69 * requests is detected, requests are queued one at a time
70 * eliminating any possible board or drive reordering.
72 * 10 Apr 1998 rev. 4.04 for linux 2.0.33 and 2.1.95
73 * Improved SMP support (if linux version >= 2.1.95).
75 * 9 Apr 1998 rev. 4.03 for linux 2.0.33 and 2.1.94
76 * Added support for new PCI code and IO-APIC remapping of irqs.
77 * Performance improvement: when sequential i/o is detected,
78 * always use direct sort instead of reverse sort.
80 * 4 Apr 1998 rev. 4.02 for linux 2.0.33 and 2.1.92
81 * io_port is now unsigned long.
83 * 17 Mar 1998 rev. 4.01 for linux 2.0.33 and 2.1.88
84 * Use new scsi error handling code (if linux version >= 2.1.88).
85 * Use new interrupt code.
87 * 12 Sep 1997 rev. 3.11 for linux 2.0.30 and 2.1.55
88 * Use of udelay inside the wait loops to avoid timeout
89 * problems with fast cpus.
90 * Removed check about useless calls to the interrupt service
91 * routine (reported on SMP systems only).
92 * At initialization time "sorted/unsorted" is displayed instead
93 * of "linked/unlinked" to reinforce the fact that "linking" is
94 * nothing but "elevator sorting" in the actual implementation.
96 * 17 May 1997 rev. 3.10 for linux 2.0.30 and 2.1.38
97 * Use of serial_number_at_timeout in abort and reset processing.
98 * Use of the __initfunc and __initdata macro in setup code.
99 * Minor cleanups in the list_statistics code.
100 * Increased controller busy timeout in order to better support
101 * slow SCSI devices.
103 * 24 Feb 1997 rev. 3.00 for linux 2.0.29 and 2.1.26
104 * When loading as a module, parameter passing is now supported
105 * both in 2.0 and in 2.1 style.
106 * Fixed data transfer direction for some SCSI opcodes.
107 * Immediate acknowledge to request sense commands.
108 * Linked commands to each disk device are now reordered by elevator
109 * sorting. Rare cases in which reordering of write requests could
110 * cause wrong results are managed.
111 * Fixed spurious timeouts caused by long simple queue tag sequences.
112 * New command line option (tm:[0-3]) to choose the type of tags:
113 * 0 -> mixed (default); 1 -> simple; 2 -> head; 3 -> ordered.
115 * 18 Jan 1997 rev. 2.60 for linux 2.1.21 and 2.0.28
116 * Added command line options to enable/disable linked commands
117 * (lc:[y|n]), tagged commands (tc:[y|n]) and to set the max queue
118 * depth (mq:xx). Default is "eata=lc:n,tc:n,mq:16".
119 * Improved command linking.
120 * Documented how to setup RAID-0 with DPT SmartRAID boards.
122 * 8 Jan 1997 rev. 2.50 for linux 2.1.20 and 2.0.27
123 * Added linked command support.
124 * Improved detection of PCI boards using ISA base addresses.
126 * 3 Dec 1996 rev. 2.40 for linux 2.1.14 and 2.0.27
127 * Added support for tagged commands and queue depth adjustment.
129 * 22 Nov 1996 rev. 2.30 for linux 2.1.12 and 2.0.26
130 * When CONFIG_PCI is defined, BIOS32 is used to include in the
131 * list of i/o ports to be probed all the PCI SCSI controllers.
132 * The list of i/o ports to be probed can be overwritten by the
133 * "eata=port0,port1,...." boot command line option.
134 * Scatter/gather lists are now allocated by a number of kmalloc
135 * calls, in order to avoid the previous size limit of 64Kb.
137 * 16 Nov 1996 rev. 2.20 for linux 2.1.10 and 2.0.25
138 * Added support for EATA 2.0C, PCI, multichannel and wide SCSI.
140 * 27 Sep 1996 rev. 2.12 for linux 2.1.0
141 * Portability cleanups (virtual/bus addressing, little/big endian
142 * support).
144 * 09 Jul 1996 rev. 2.11 for linux 2.0.4
145 * Number of internal retries is now limited.
147 * 16 Apr 1996 rev. 2.10 for linux 1.3.90
148 * New argument "reset_flags" to the reset routine.
150 * 6 Jul 1995 rev. 2.01 for linux 1.3.7
151 * Update required by the new /proc/scsi support.
153 * 11 Mar 1995 rev. 2.00 for linux 1.2.0
154 * Fixed a bug which prevented media change detection for removable
155 * disk drives.
157 * 23 Feb 1995 rev. 1.18 for linux 1.1.94
158 * Added a check for scsi_register returning NULL.
160 * 11 Feb 1995 rev. 1.17 for linux 1.1.91
161 * Now DEBUG_RESET is disabled by default.
162 * Register a board even if it does not assert DMA protocol support
163 * (DPT SK2011B does not report correctly the dmasup bit).
165 * 9 Feb 1995 rev. 1.16 for linux 1.1.90
166 * Use host->wish_block instead of host->block.
167 * New list of Data Out SCSI commands.
169 * 8 Feb 1995 rev. 1.15 for linux 1.1.89
170 * Cleared target_time_out counter while performing a reset.
171 * All external symbols renamed to avoid possible name conflicts.
173 * 28 Jan 1995 rev. 1.14 for linux 1.1.86
174 * Added module support.
175 * Log and do a retry when a disk drive returns a target status
176 * different from zero on a recovered error.
178 * 24 Jan 1995 rev. 1.13 for linux 1.1.85
179 * Use optimized board configuration, with a measured performance
180 * increase in the range 10%-20% on i/o throughput.
182 * 16 Jan 1995 rev. 1.12 for linux 1.1.81
183 * Fix mscp structure comments (no functional change).
184 * Display a message if check_region detects a port address
185 * already in use.
187 * 17 Dec 1994 rev. 1.11 for linux 1.1.74
188 * Use the scsicam_bios_param routine. This allows an easy
189 * migration path from disk partition tables created using
190 * different SCSI drivers and non optimal disk geometry.
192 * 15 Dec 1994 rev. 1.10 for linux 1.1.74
193 * Added support for ISA EATA boards (DPT PM2011, DPT PM2021).
194 * The host->block flag is set for all the detected ISA boards.
195 * The detect routine no longer enforces LEVEL triggering
196 * for EISA boards, it just prints a warning message.
198 * 30 Nov 1994 rev. 1.09 for linux 1.1.68
199 * Redo i/o on target status CHECK_CONDITION for TYPE_DISK only.
200 * Added optional support for using a single board at a time.
202 * 18 Nov 1994 rev. 1.08 for linux 1.1.64
203 * Forces sg_tablesize = 64 and can_queue = 64 if these
204 * values are not correctly detected (DPT PM2012).
206 * 14 Nov 1994 rev. 1.07 for linux 1.1.63 Final BETA release.
207 * 04 Aug 1994 rev. 1.00 for linux 1.1.39 First BETA release.
210 * This driver is based on the CAM (Common Access Method Committee)
211 * EATA (Enhanced AT Bus Attachment) rev. 2.0A, using DMA protocol.
213 * Copyright (C) 1994-2000 Dario Ballabio (ballabio_dario@emc.com)
215 * Alternate email: dario.ballabio@inwind.it, dario.ballabio@tiscalinet.it
217 * Redistribution and use in source and binary forms, with or without
218 * modification, are permitted provided that redistributions of source
219 * code retain the above copyright notice and this comment without
220 * modification.
226 * Here is a brief description of the DPT SCSI host adapters.
227 * All these boards provide an EATA/DMA compatible programming interface
228 * and are fully supported by this driver in any configuration, including
229 * multiple SCSI channels:
231 * PM2011B/9X - Entry Level ISA
232 * PM2021A/9X - High Performance ISA
233 * PM2012A Old EISA
234 * PM2012B Old EISA
235 * PM2022A/9X - Entry Level EISA
236 * PM2122A/9X - High Performance EISA
237 * PM2322A/9X - Extra High Performance EISA
238 * PM3021 - SmartRAID Adapter for ISA
239 * PM3222 - SmartRAID Adapter for EISA (PM3222W is 16-bit wide SCSI)
240 * PM3224 - SmartRAID Adapter for PCI (PM3224W is 16-bit wide SCSI)
241 * PM33340UW - SmartRAID Adapter for PCI ultra wide multichannel
243 * The above list is just an indication: as a matter of fact all DPT
244 * boards using the EATA/DMA protocol are supported by this driver,
245 * since they use exactely the same programming interface.
247 * The DPT PM2001 provides only the EATA/PIO interface and hence is not
248 * supported by this driver.
250 * This code has been tested with up to 3 Distributed Processing Technology
251 * PM2122A/9X (DPT SCSI BIOS v002.D1, firmware v05E.0) EISA controllers,
252 * in any combination of private and shared IRQ.
253 * PCI support has been tested using up to 2 DPT PM3224W (DPT SCSI BIOS
254 * v003.D0, firmware v07G.0).
256 * DPT SmartRAID boards support "Hardware Array" - a group of disk drives
257 * which are all members of the same RAID-0, RAID-1 or RAID-5 array implemented
258 * in host adapter hardware. Hardware Arrays are fully compatible with this
259 * driver, since they look to it as a single disk drive.
261 * WARNING: to create a RAID-0 "Hardware Array" you must select "Other Unix"
262 * as the current OS in the DPTMGR "Initial System Installation" menu.
263 * Otherwise RAID-0 is generated as an "Array Group" (i.e. software RAID-0),
264 * which is not supported by the actual SCSI subsystem.
265 * To get the "Array Group" functionality, the Linux MD driver must be used
266 * instead of the DPT "Array Group" feature.
268 * Multiple ISA, EISA and PCI boards can be configured in the same system.
269 * It is suggested to put all the EISA boards on the same IRQ level, all
270 * the PCI boards on another IRQ level, while ISA boards cannot share
271 * interrupts.
273 * If you configure multiple boards on the same IRQ, the interrupt must
274 * be _level_ triggered (not _edge_ triggered).
276 * This driver detects EATA boards by probes at fixed port addresses,
277 * so no BIOS32 or PCI BIOS support is required.
278 * The suggested way to detect a generic EATA PCI board is to force on it
279 * any unused EISA address, even if there are other controllers on the EISA
280 * bus, or even if you system has no EISA bus at all.
281 * Do not force any ISA address on EATA PCI boards.
283 * If PCI bios support is configured into the kernel, BIOS32 is used to
284 * include in the list of i/o ports to be probed all the PCI SCSI controllers.
286 * Due to a DPT BIOS "feature", it might not be possible to force an EISA
287 * address on more then a single DPT PCI board, so in this case you have to
288 * let the PCI BIOS assign the addresses.
290 * The sequence of detection probes is:
292 * - ISA 0x1F0;
293 * - PCI SCSI controllers (only if BIOS32 is available);
294 * - EISA/PCI 0x1C88 through 0xFC88 (corresponding to EISA slots 1 to 15);
295 * - ISA 0x170, 0x230, 0x330.
297 * The above list of detection probes can be totally replaced by the
298 * boot command line option: "eata=port0,port1,port2,...", where the
299 * port0, port1... arguments are ISA/EISA/PCI addresses to be probed.
300 * For example using "eata=0x7410,0x7450,0x230", the driver probes
301 * only the two PCI addresses 0x7410 and 0x7450 and the ISA address 0x230,
302 * in this order; "eata=0" totally disables this driver.
304 * After the optional list of detection probes, other possible command line
305 * options are:
307 * et:y force use of extended translation (255 heads, 63 sectors);
308 * et:n use disk geometry detected by scsicam_bios_param;
309 * rs:y reverse scan order while detecting PCI boards;
310 * rs:n use BIOS order while detecting PCI boards;
311 * lc:y enables linked commands;
312 * lc:n disables linked commands;
313 * tc:y enables tagged commands;
314 * tc:n disables tagged commands;
315 * tm:0 use head/simple/ordered queue tag sequences;
316 * tm:1 use only simple queue tags;
317 * tm:2 use only head of queue tags;
318 * tm:3 use only ordered queue tags;
319 * mq:xx set the max queue depth to the value xx (2 <= xx <= 32).
321 * The default value is: "eata=lc:n,tc:n,mq:16,tm:0,et:n,rs:n".
322 * An example using the list of detection probes could be:
323 * "eata=0x7410,0x230,lc:y,tc:n,mq:4,et:n".
325 * When loading as a module, parameters can be specified as well.
326 * The above example would be (use 1 in place of y and 0 in place of n):
328 * modprobe eata io_port=0x7410,0x230 linked_comm=1 tagged_comm=0 \
329 * max_queue_depth=4 tag_mode=0 \
330 * ext_tran=0 rev_scan=1
332 * ----------------------------------------------------------------------------
333 * In this implementation, linked commands are designed to work with any DISK
334 * or CD-ROM, since this linking has only the intent of clustering (time-wise)
335 * and reordering by elevator sorting commands directed to each device,
336 * without any relation with the actual SCSI protocol between the controller
337 * and the device.
338 * If Q is the queue depth reported at boot time for each device (also named
339 * cmds/lun) and Q > 2, whenever there is already an active command to the
340 * device all other commands to the same device (up to Q-1) are kept waiting
341 * in the elevator sorting queue. When the active command completes, the
342 * commands in this queue are sorted by sector address. The sort is chosen
343 * between increasing or decreasing by minimizing the seek distance between
344 * the sector of the commands just completed and the sector of the first
345 * command in the list to be sorted.
346 * Trivial math assures that the unsorted average seek distance when doing
347 * random seeks over S sectors is S/3.
348 * When (Q-1) requests are uniformly distributed over S sectors, the average
349 * distance between two adjacent requests is S/((Q-1) + 1), so the sorted
350 * average seek distance for (Q-1) random requests over S sectors is S/Q.
351 * The elevator sorting hence divides the seek distance by a factor Q/3.
352 * The above pure geometric remarks are valid in all cases and the
353 * driver effectively reduces the seek distance by the predicted factor
354 * when there are Q concurrent read i/o operations on the device, but this
355 * does not necessarily results in a noticeable performance improvement:
356 * your mileage may vary....
358 * Note: command reordering inside a batch of queued commands could cause
359 * wrong results only if there is at least one write request and the
360 * intersection (sector-wise) of all requests is not empty.
361 * When the driver detects a batch including overlapping requests
362 * (a really rare event) strict serial (pid) order is enforced.
363 * ----------------------------------------------------------------------------
364 * The extended translation option (et:y) is useful when using large physical
365 * disks/arrays. It could also be useful when switching between Adaptec boards
366 * and DPT boards without reformatting the disk.
367 * When a boot disk is partitioned with extended translation, in order to
368 * be able to boot it with a DPT board is could be necessary to add to
369 * lilo.conf additional commands as in the following example:
371 * fix-table
372 * disk=/dev/sda bios=0x80 sectors=63 heads=128 cylindres=546
374 * where the above geometry should be replaced with the one reported at
375 * power up by the DPT controller.
376 * ----------------------------------------------------------------------------
378 * The boards are named EATA0, EATA1,... according to the detection order.
380 * In order to support multiple ISA boards in a reliable way,
381 * the driver sets host->wish_block = TRUE for all ISA boards.
384 #include <linux/version.h>
386 #ifndef LinuxVersionCode
387 #define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
388 #endif
390 #define MAX_INT_PARAM 10
392 #if defined(MODULE)
393 #include <linux/module.h>
395 MODULE_PARM(boot_options, "s");
396 MODULE_PARM(io_port, "1-" __MODULE_STRING(MAX_INT_PARAM) "i");
397 MODULE_PARM(linked_comm, "i");
398 MODULE_PARM(tagged_comm, "i");
399 MODULE_PARM(link_statistics, "i");
400 MODULE_PARM(max_queue_depth, "i");
401 MODULE_PARM(tag_mode, "i");
402 MODULE_PARM(ext_tran, "i");
403 MODULE_PARM(rev_scan, "i");
404 MODULE_AUTHOR("Dario Ballabio");
406 #endif
408 #include <linux/string.h>
409 #include <linux/sched.h>
410 #include <linux/kernel.h>
411 #include <linux/ioport.h>
412 #include <linux/delay.h>
413 #include <asm/io.h>
414 #include <asm/system.h>
415 #include <asm/byteorder.h>
416 #include <linux/proc_fs.h>
417 #include <linux/blk.h>
418 #include "scsi.h"
419 #include "hosts.h"
420 #include "sd.h"
421 #include <asm/dma.h>
422 #include <asm/irq.h>
423 #include "eata.h"
424 #include <linux/stat.h>
425 #include <linux/config.h>
426 #include <linux/pci.h>
427 #include <linux/init.h>
428 #include <linux/ctype.h>
429 #include <linux/spinlock.h>
431 #define SPIN_FLAGS unsigned long spin_flags;
432 #define SPIN_LOCK spin_lock_irq(&io_request_lock);
433 #define SPIN_LOCK_SAVE spin_lock_irqsave(&io_request_lock, spin_flags);
434 #define SPIN_UNLOCK spin_unlock_irq(&io_request_lock);
435 #define SPIN_UNLOCK_RESTORE \
436 spin_unlock_irqrestore(&io_request_lock, spin_flags);
438 /* Subversion values */
439 #define ISA 0
440 #define ESA 1
442 #undef FORCE_CONFIG
444 #undef DEBUG_LINKED_COMMANDS
445 #undef DEBUG_DETECT
446 #undef DEBUG_PCI_DETECT
447 #undef DEBUG_INTERRUPT
448 #undef DEBUG_RESET
449 #undef DEBUG_GENERATE_ERRORS
450 #undef DEBUG_GENERATE_ABORTS
451 #undef DEBUG_GEOMETRY
453 #define MAX_ISA 4
454 #define MAX_VESA 0
455 #define MAX_EISA 15
456 #define MAX_PCI 16
457 #define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
458 #define MAX_CHANNEL 4
459 #define MAX_LUN 32
460 #define MAX_TARGET 32
461 #define MAX_MAILBOXES 64
462 #define MAX_SGLIST 64
463 #define MAX_LARGE_SGLIST 122
464 #define MAX_INTERNAL_RETRIES 64
465 #define MAX_CMD_PER_LUN 2
466 #define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
468 #define SKIP ULONG_MAX
469 #define FALSE 0
470 #define TRUE 1
471 #define FREE 0
472 #define IN_USE 1
473 #define LOCKED 2
474 #define IN_RESET 3
475 #define IGNORE 4
476 #define READY 5
477 #define ABORTING 6
478 #define NO_DMA 0xff
479 #define MAXLOOP 10000
480 #define TAG_MIXED 0
481 #define TAG_SIMPLE 1
482 #define TAG_HEAD 2
483 #define TAG_ORDERED 3
485 #define REG_CMD 7
486 #define REG_STATUS 7
487 #define REG_AUX_STATUS 8
488 #define REG_DATA 0
489 #define REG_DATA2 1
490 #define REG_SEE 6
491 #define REG_LOW 2
492 #define REG_LM 3
493 #define REG_MID 4
494 #define REG_MSB 5
495 #define REGION_SIZE 9
496 #define MAX_ISA_ADDR 0x03ff
497 #define MIN_EISA_ADDR 0x1c88
498 #define MAX_EISA_ADDR 0xfc88
499 #define BSY_ASSERTED 0x80
500 #define DRQ_ASSERTED 0x08
501 #define ABSY_ASSERTED 0x01
502 #define IRQ_ASSERTED 0x02
503 #define READ_CONFIG_PIO 0xf0
504 #define SET_CONFIG_PIO 0xf1
505 #define SEND_CP_PIO 0xf2
506 #define RECEIVE_SP_PIO 0xf3
507 #define TRUNCATE_XFR_PIO 0xf4
508 #define RESET_PIO 0xf9
509 #define READ_CONFIG_DMA 0xfd
510 #define SET_CONFIG_DMA 0xfe
511 #define SEND_CP_DMA 0xff
512 #define ASOK 0x00
513 #define ASST 0x01
515 #define YESNO(a) ((a) ? 'y' : 'n')
516 #define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
518 /* "EATA", in Big Endian format */
519 #define EATA_SIGNATURE 0x41544145
521 /* Number of valid bytes in the board config structure for EATA 2.0x */
522 #define EATA_2_0A_SIZE 28
523 #define EATA_2_0B_SIZE 30
524 #define EATA_2_0C_SIZE 34
526 /* Board info structure */
527 struct eata_info {
528 u_int32_t data_len; /* Number of valid bytes after this field */
529 u_int32_t sign; /* ASCII "EATA" signature */
530 unchar :4, /* unused low nibble */
531 version:4; /* EATA version, should be 0x1 */
532 unchar ocsena:1, /* Overlap Command Support Enabled */
533 tarsup:1, /* Target Mode Supported */
534 trnxfr:1, /* Truncate Transfer Cmd NOT Necessary */
535 morsup:1, /* More Supported */
536 dmasup:1, /* DMA Supported */
537 drqvld:1, /* DRQ Index (DRQX) is valid */
538 ata:1, /* This is an ATA device */
539 haaval:1; /* Host Adapter Address Valid */
540 ushort cp_pad_len; /* Number of pad bytes after cp_len */
541 unchar host_addr[4]; /* Host Adapter SCSI ID for channels 3, 2, 1, 0 */
542 u_int32_t cp_len; /* Number of valid bytes in cp */
543 u_int32_t sp_len; /* Number of valid bytes in sp */
544 ushort queue_size; /* Max number of cp that can be queued */
545 ushort unused;
546 ushort scatt_size; /* Max number of entries in scatter/gather table */
547 unchar irq:4, /* Interrupt Request assigned to this controller */
548 irq_tr:1, /* 0 for edge triggered, 1 for level triggered */
549 second:1, /* 1 if this is a secondary (not primary) controller */
550 drqx:2; /* DRQ Index (0=DMA0, 1=DMA7, 2=DMA6, 3=DMA5) */
551 unchar sync; /* 1 if scsi target id 7...0 is running sync scsi */
553 /* Structure extension defined in EATA 2.0B */
554 unchar isaena:1, /* ISA i/o addressing is disabled/enabled */
555 forcaddr:1, /* Port address has been forced */
556 large_sg:1, /* 1 if large SG lists are supported */
557 res1:1,
559 unchar max_id:5, /* Max SCSI target ID number */
560 max_chan:3; /* Max SCSI channel number on this board */
562 /* Structure extension defined in EATA 2.0C */
563 unchar max_lun; /* Max SCSI LUN number */
564 unchar :4,
565 m1:1, /* This is a PCI with an M1 chip installed */
566 idquest:1, /* RAIDNUM returned is questionable */
567 pci:1, /* This board is PCI */
568 eisa:1; /* This board is EISA */
569 unchar raidnum; /* Uniquely identifies this HBA in a system */
570 unchar notused;
572 ushort ipad[247];
575 /* Board config structure */
576 struct eata_config {
577 ushort len; /* Number of bytes following this field */
578 unchar edis:1, /* Disable EATA interface after config command */
579 ocena:1, /* Overlapped Commands Enabled */
580 mdpena:1, /* Transfer all Modified Data Pointer Messages */
581 tarena:1, /* Target Mode Enabled for this controller */
583 unchar cpad[511];
586 /* Returned status packet structure */
587 struct mssp {
588 unchar adapter_status:7, /* State related to current command */
589 eoc:1; /* End Of Command (1 = command completed) */
590 unchar target_status; /* SCSI status received after data transfer */
591 unchar unused[2];
592 u_int32_t inv_res_len; /* Number of bytes not transferred */
593 u_int32_t cpp_index; /* Index of address set in cp */
594 char mess[12];
597 struct sg_list {
598 unsigned int address; /* Segment Address */
599 unsigned int num_bytes; /* Segment Length */
602 /* MailBox SCSI Command Packet */
603 struct mscp {
604 unchar sreset:1, /* SCSI Bus Reset Signal should be asserted */
605 init:1, /* Re-initialize controller and self test */
606 reqsen:1, /* Transfer Request Sense Data to addr using DMA */
607 sg:1, /* Use Scatter/Gather */
609 interp:1, /* The controller interprets cp, not the target */
610 dout:1, /* Direction of Transfer is Out (Host to Target) */
611 din:1; /* Direction of Transfer is In (Target to Host) */
612 unchar sense_len; /* Request Sense Length */
613 unchar unused[3];
614 unchar fwnest:1, /* Send command to a component of an Array Group */
616 unchar phsunit:1, /* Send to Target Physical Unit (bypass RAID) */
617 iat:1, /* Inhibit Address Translation */
618 hbaci:1, /* Inhibit HBA Caching for this command */
620 unchar target:5, /* SCSI target ID */
621 channel:3; /* SCSI channel number */
622 unchar lun:5, /* SCSI logical unit number */
623 luntar:1, /* This cp is for Target (not LUN) */
624 dispri:1, /* Disconnect Privilege granted */
625 one:1; /* 1 */
626 unchar mess[3]; /* Massage to/from Target */
627 unchar cdb[12]; /* Command Descriptor Block */
628 u_int32_t data_len; /* If sg=0 Data Length, if sg=1 sglist length */
629 u_int32_t cpp_index; /* Index of address to be returned in sp */
630 u_int32_t data_address; /* If sg=0 Data Address, if sg=1 sglist address */
631 u_int32_t sp_addr; /* Address where sp is DMA'ed when cp completes */
632 u_int32_t sense_addr; /* Address where Sense Data is DMA'ed on error */
633 /* Additional fields begin here. */
634 Scsi_Cmnd *SCpnt;
635 struct sg_list *sglist;
638 struct hostdata {
639 struct mscp cp[MAX_MAILBOXES]; /* Mailboxes for this board */
640 unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
641 unsigned int last_cp_used; /* Index of last mailbox used */
642 unsigned int iocount; /* Total i/o done for this board */
643 int board_number; /* Number of this board */
644 char board_name[16]; /* Name of this board */
645 char board_id[256]; /* data from INQUIRY on this board */
646 int in_reset; /* True if board is doing a reset */
647 int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
648 int target_redo[MAX_TARGET][MAX_CHANNEL]; /* If TRUE redo i/o on target */
649 unsigned int retries; /* Number of internal retries */
650 unsigned long last_retried_pid; /* Pid of last retried command */
651 unsigned char subversion; /* Bus type, either ISA or EISA/PCI */
652 unsigned char protocol_rev; /* EATA 2.0 rev., 'A' or 'B' or 'C' */
653 struct mssp sp[2]; /* Returned status for this board */
656 static struct Scsi_Host *sh[MAX_BOARDS + 1];
657 static const char *driver_name = "EATA";
658 static char sha[MAX_BOARDS];
660 /* Initialize num_boards so that ihdlr can work while detect is in progress */
661 static unsigned int num_boards = MAX_BOARDS;
663 static unsigned long io_port[] = {
665 /* Space for MAX_INT_PARAM ports usable while loading as a module */
666 SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
667 SKIP, SKIP,
669 /* First ISA */
670 0x1f0,
672 /* Space for MAX_PCI ports possibly reported by PCI_BIOS */
673 SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
674 SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
676 /* MAX_EISA ports */
677 0x1c88, 0x2c88, 0x3c88, 0x4c88, 0x5c88, 0x6c88, 0x7c88, 0x8c88,
678 0x9c88, 0xac88, 0xbc88, 0xcc88, 0xdc88, 0xec88, 0xfc88,
680 /* Other (MAX_ISA - 1) ports */
681 0x170, 0x230, 0x330,
683 /* End of list */
687 #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
688 #define BN(board) (HD(board)->board_name)
690 #define H2DEV(x) htonl(x)
691 #define DEV2H(x) H2DEV(x)
692 #define V2DEV(addr) ((addr) ? H2DEV(virt_to_bus((void *)addr)) : 0)
693 #define DEV2V(addr) ((addr) ? DEV2H(bus_to_virt((unsigned long)addr)) : 0)
695 static void do_interrupt_handler(int, void *, struct pt_regs *);
696 static void flush_dev(Scsi_Device *, unsigned long, unsigned int, unsigned int);
697 static int do_trace = FALSE;
698 static int setup_done = FALSE;
699 static int link_statistics;
700 static int tag_mode = TAG_MIXED;
701 static int ext_tran = FALSE;
702 static int rev_scan = TRUE;
703 static char *boot_options;
705 #if defined(CONFIG_SCSI_EATA_TAGGED_QUEUE)
706 static int tagged_comm = TRUE;
707 #else
708 static int tagged_comm = FALSE;
709 #endif
711 #if defined(CONFIG_SCSI_EATA_LINKED_COMMANDS)
712 static int linked_comm = TRUE;
713 #else
714 static int linked_comm = FALSE;
715 #endif
717 #if defined(CONFIG_SCSI_EATA_MAX_TAGS)
718 static int max_queue_depth = CONFIG_SCSI_EATA_MAX_TAGS;
719 #else
720 static int max_queue_depth = MAX_CMD_PER_LUN;
721 #endif
723 static void select_queue_depths(struct Scsi_Host *host, Scsi_Device *devlist) {
724 Scsi_Device *dev;
725 int j, ntag = 0, nuntag = 0, tqd, utqd;
727 j = ((struct hostdata *) host->hostdata)->board_number;
729 for(dev = devlist; dev; dev = dev->next) {
731 if (dev->host != host) continue;
733 if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
734 ntag++;
735 else
736 nuntag++;
739 utqd = MAX_CMD_PER_LUN;
741 tqd = (host->can_queue - utqd * nuntag) / (ntag ? ntag : 1);
743 if (tqd > max_queue_depth) tqd = max_queue_depth;
745 if (tqd < MAX_CMD_PER_LUN) tqd = MAX_CMD_PER_LUN;
747 for(dev = devlist; dev; dev = dev->next) {
748 char *tag_suffix = "", *link_suffix = "";
750 if (dev->host != host) continue;
752 if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
753 dev->queue_depth = tqd;
754 else
755 dev->queue_depth = utqd;
757 if (TLDEV(dev->type)) {
758 if (linked_comm && dev->queue_depth > 2)
759 link_suffix = ", sorted";
760 else
761 link_suffix = ", unsorted";
764 if (tagged_comm && dev->tagged_supported && TLDEV(dev->type)) {
765 dev->tagged_queue = 1;
766 dev->current_tag = 1;
769 if (dev->tagged_supported && TLDEV(dev->type) && dev->tagged_queue)
770 tag_suffix = ", soft-tagged";
771 else if (dev->tagged_supported && TLDEV(dev->type))
772 tag_suffix = ", tagged";
774 printk("%s: scsi%d, channel %d, id %d, lun %d, cmds/lun %d%s%s.\n",
775 BN(j), host->host_no, dev->channel, dev->id, dev->lun,
776 dev->queue_depth, link_suffix, tag_suffix);
779 return;
782 static inline int wait_on_busy(unsigned long iobase, unsigned int loop) {
784 while (inb(iobase + REG_AUX_STATUS) & ABSY_ASSERTED) {
785 udelay(1L);
786 if (--loop == 0) return TRUE;
789 return FALSE;
792 static inline int do_dma(unsigned long iobase, unsigned long addr, unchar cmd) {
794 if (wait_on_busy(iobase, (addr ? MAXLOOP * 100 : MAXLOOP))) return TRUE;
796 if ((addr = V2DEV(addr))) {
797 outb((char) (addr >> 24), iobase + REG_LOW);
798 outb((char) (addr >> 16), iobase + REG_LM);
799 outb((char) (addr >> 8), iobase + REG_MID);
800 outb((char) addr, iobase + REG_MSB);
803 outb(cmd, iobase + REG_CMD);
804 return FALSE;
807 static inline int read_pio(unsigned long iobase, ushort *start, ushort *end) {
808 unsigned int loop = MAXLOOP;
809 ushort *p;
811 for (p = start; p <= end; p++) {
813 while (!(inb(iobase + REG_STATUS) & DRQ_ASSERTED)) {
814 udelay(1L);
815 if (--loop == 0) return TRUE;
818 loop = MAXLOOP;
819 *p = inw(iobase);
822 return FALSE;
825 static inline void tune_pci_port(unsigned long port_base) {
827 #if defined(CONFIG_PCI)
829 unsigned int addr, k;
830 struct pci_dev *dev = NULL;
832 if (!pci_present()) return;
834 for (k = 0; k < MAX_PCI; k++) {
836 if (!(dev = pci_find_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) break;
838 addr = pci_resource_start (dev, 0);
840 if (pci_enable_device (dev)) continue;
842 #if defined(DEBUG_PCI_DETECT)
843 printk("%s: tune_pci_port, bus %d, devfn 0x%x, addr 0x%x.\n",
844 driver_name, dev->bus->number, dev->devfn, addr);
845 #endif
847 if ((addr & PCI_BASE_ADDRESS_IO_MASK) + PCI_BASE_ADDRESS_0 == port_base) {
848 pci_set_master(dev);
849 return;
854 #endif /* end CONFIG_PCI */
856 return;
859 static inline int
860 get_pci_irq(unsigned long port_base, unsigned char *apic_irq) {
862 #if defined(CONFIG_PCI)
864 unsigned int addr;
865 struct pci_dev *dev = NULL;
867 if (!pci_present()) return FALSE;
869 while((dev = pci_find_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
871 if (pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &addr)) continue;
873 #if defined(DEBUG_PCI_DETECT)
874 printk("%s: get_pci_irq, bus %d, devfn 0x%x, addr 0x%x, apic_irq %u.\n",
875 driver_name, dev->bus->number, dev->devfn, addr, dev->irq);
876 #endif
878 if ((addr & PCI_BASE_ADDRESS_IO_MASK) + PCI_BASE_ADDRESS_0 == port_base) {
879 *apic_irq = dev->irq;
880 return TRUE;
885 #endif /* end CONFIG_PCI */
887 return FALSE;
890 static inline int port_detect \
891 (unsigned long port_base, unsigned int j, Scsi_Host_Template *tpnt) {
892 unsigned char irq, dma_channel, subversion, i;
893 unsigned char protocol_rev, apic_irq;
894 struct eata_info info;
895 char *bus_type, dma_name[16], tag_type;
897 /* Allowed DMA channels for ISA (0 indicates reserved) */
898 unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
900 char name[16];
902 sprintf(name, "%s%d", driver_name, j);
904 if(check_region(port_base, REGION_SIZE)) {
905 #if defined(DEBUG_DETECT)
906 printk("%s: address 0x%03lx in use, skipping probe.\n", name, port_base);
907 #endif
908 return FALSE;
911 if (do_dma(port_base, 0, READ_CONFIG_PIO)) return FALSE;
913 /* Read the info structure */
914 if (read_pio(port_base, (ushort *)&info, (ushort *)&info.ipad[0]))
915 return FALSE;
917 /* Check the controller "EATA" signature */
918 if (info.sign != EATA_SIGNATURE) return FALSE;
920 if (DEV2H(info.data_len) < EATA_2_0A_SIZE) {
921 printk("%s: config structure size (%d bytes) too short, detaching.\n",
922 name, DEV2H(info.data_len));
923 return FALSE;
925 else if (DEV2H(info.data_len) == EATA_2_0A_SIZE)
926 protocol_rev = 'A';
927 else if (DEV2H(info.data_len) == EATA_2_0B_SIZE)
928 protocol_rev = 'B';
929 else
930 protocol_rev = 'C';
932 if (!setup_done && j > 0 && j <= MAX_PCI) {
933 bus_type = "PCI";
934 subversion = ESA;
936 else if (port_base > MAX_EISA_ADDR || (protocol_rev == 'C' && info.pci)) {
937 bus_type = "PCI";
938 subversion = ESA;
940 else if (port_base >= MIN_EISA_ADDR || (protocol_rev == 'C' && info.eisa)) {
941 bus_type = "EISA";
942 subversion = ESA;
944 else if (protocol_rev == 'C' && !info.eisa && !info.pci) {
945 bus_type = "ISA";
946 subversion = ISA;
948 else if (port_base > MAX_ISA_ADDR) {
949 bus_type = "PCI";
950 subversion = ESA;
952 else {
953 bus_type = "ISA";
954 subversion = ISA;
957 if (!info.haaval || info.ata) {
958 printk("%s: address 0x%03lx, unusable %s board (%d%d), detaching.\n",
959 name, port_base, bus_type, info.haaval, info.ata);
960 return FALSE;
963 if (info.drqvld) {
965 if (subversion == ESA)
966 printk("%s: warning, weird %s board using DMA.\n", name, bus_type);
968 subversion = ISA;
969 dma_channel = dma_channel_table[3 - info.drqx];
971 else {
973 if (subversion == ISA)
974 printk("%s: warning, weird %s board not using DMA.\n", name, bus_type);
976 subversion = ESA;
977 dma_channel = NO_DMA;
980 if (!info.dmasup)
981 printk("%s: warning, DMA protocol support not asserted.\n", name);
983 irq = info.irq;
985 if (subversion == ESA && !info.irq_tr)
986 printk("%s: warning, LEVEL triggering is suggested for IRQ %u.\n",
987 name, irq);
989 if (get_pci_irq(port_base, &apic_irq) && (irq != apic_irq)) {
990 printk("%s: IRQ %u mapped to IO-APIC IRQ %u.\n", name, irq, apic_irq);
991 irq = apic_irq;
994 /* Board detected, allocate its IRQ */
995 if (request_irq(irq, do_interrupt_handler,
996 SA_INTERRUPT | ((subversion == ESA) ? SA_SHIRQ : 0),
997 driver_name, (void *) &sha[j])) {
998 printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
999 return FALSE;
1002 if (subversion == ISA && request_dma(dma_channel, driver_name)) {
1003 printk("%s: unable to allocate DMA channel %u, detaching.\n",
1004 name, dma_channel);
1005 free_irq(irq, &sha[j]);
1006 return FALSE;
1009 #if defined(FORCE_CONFIG)
1011 struct eata_config config;
1013 /* Set board configuration */
1014 memset((char *)&config, 0, sizeof(struct eata_config));
1015 config.len = (ushort) htons((ushort)510);
1016 config.ocena = TRUE;
1018 if (do_dma(port_base, (unsigned long)&config, SET_CONFIG_DMA)) {
1019 printk("%s: busy timeout sending configuration, detaching.\n", name);
1020 return FALSE;
1023 #endif
1025 sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
1027 if (sh[j] == NULL) {
1028 printk("%s: unable to register host, detaching.\n", name);
1030 free_irq(irq, &sha[j]);
1032 if (subversion == ISA) free_dma(dma_channel);
1034 return FALSE;
1037 sh[j]->io_port = port_base;
1038 sh[j]->unique_id = port_base;
1039 sh[j]->n_io_port = REGION_SIZE;
1040 sh[j]->dma_channel = dma_channel;
1041 sh[j]->irq = irq;
1042 sh[j]->sg_tablesize = (ushort) ntohs(info.scatt_size);
1043 sh[j]->this_id = (ushort) info.host_addr[3];
1044 sh[j]->can_queue = (ushort) ntohs(info.queue_size);
1045 sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
1046 sh[j]->select_queue_depths = select_queue_depths;
1048 /* Register the I/O space that we use */
1049 request_region(sh[j]->io_port, sh[j]->n_io_port, driver_name);
1051 memset(HD(j), 0, sizeof(struct hostdata));
1052 HD(j)->subversion = subversion;
1053 HD(j)->protocol_rev = protocol_rev;
1054 HD(j)->board_number = j;
1056 if (HD(j)->subversion == ESA)
1057 sh[j]->unchecked_isa_dma = FALSE;
1058 else {
1059 unsigned long flags;
1060 scsi_register_blocked_host(sh[j]);
1061 sh[j]->unchecked_isa_dma = TRUE;
1063 flags=claim_dma_lock();
1064 disable_dma(dma_channel);
1065 clear_dma_ff(dma_channel);
1066 set_dma_mode(dma_channel, DMA_MODE_CASCADE);
1067 enable_dma(dma_channel);
1068 release_dma_lock(flags);
1072 strcpy(BN(j), name);
1074 /* DPT PM2012 does not allow to detect sg_tablesize correctly */
1075 if (sh[j]->sg_tablesize > MAX_SGLIST || sh[j]->sg_tablesize < 2) {
1076 printk("%s: detect, wrong n. of SG lists %d, fixed.\n",
1077 BN(j), sh[j]->sg_tablesize);
1078 sh[j]->sg_tablesize = MAX_SGLIST;
1081 /* DPT PM2012 does not allow to detect can_queue correctly */
1082 if (sh[j]->can_queue > MAX_MAILBOXES || sh[j]->can_queue < 2) {
1083 printk("%s: detect, wrong n. of mbox %d, fixed.\n",
1084 BN(j), sh[j]->can_queue);
1085 sh[j]->can_queue = MAX_MAILBOXES;
1088 if (protocol_rev != 'A') {
1090 if (info.max_chan > 0 && info.max_chan < MAX_CHANNEL)
1091 sh[j]->max_channel = info.max_chan;
1093 if (info.max_id > 7 && info.max_id < MAX_TARGET)
1094 sh[j]->max_id = info.max_id + 1;
1096 if (info.large_sg && sh[j]->sg_tablesize == MAX_SGLIST)
1097 sh[j]->sg_tablesize = MAX_LARGE_SGLIST;
1100 if (protocol_rev == 'C') {
1102 if (info.max_lun > 7 && info.max_lun < MAX_LUN)
1103 sh[j]->max_lun = info.max_lun + 1;
1106 if (dma_channel == NO_DMA) sprintf(dma_name, "%s", "BMST");
1107 else sprintf(dma_name, "DMA %u", dma_channel);
1109 for (i = 0; i < sh[j]->can_queue; i++)
1110 if (! ((&HD(j)->cp[i])->sglist = kmalloc(
1111 sh[j]->sg_tablesize * sizeof(struct sg_list),
1112 (sh[j]->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC))) {
1113 printk("%s: kmalloc SGlist failed, mbox %d, detaching.\n", BN(j), i);
1114 eata2x_release(sh[j]);
1115 return FALSE;
1118 if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
1119 max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
1121 if (max_queue_depth < MAX_CMD_PER_LUN) max_queue_depth = MAX_CMD_PER_LUN;
1123 if (tagged_comm) {
1124 if (tag_mode == TAG_SIMPLE) tag_type = '1';
1125 else if (tag_mode == TAG_HEAD) tag_type = '2';
1126 else if (tag_mode == TAG_ORDERED) tag_type = '3';
1127 else tag_type = 'y';
1129 else tag_type = 'n';
1131 if (j == 0) {
1132 printk("EATA/DMA 2.0x: Copyright (C) 1994-2000 Dario Ballabio.\n");
1133 printk("%s config options -> tc:%c, lc:%c, mq:%d, rs:%c, et:%c.\n",
1134 driver_name, tag_type, YESNO(linked_comm), max_queue_depth,
1135 YESNO(rev_scan), YESNO(ext_tran));
1138 printk("%s: 2.0%c, %s 0x%03lx, IRQ %u, %s, SG %d, MB %d.\n",
1139 BN(j), HD(j)->protocol_rev, bus_type, (unsigned long)sh[j]->io_port,
1140 sh[j]->irq, dma_name, sh[j]->sg_tablesize, sh[j]->can_queue);
1142 if (sh[j]->max_id > 8 || sh[j]->max_lun > 8)
1143 printk("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
1144 BN(j), sh[j]->max_id, sh[j]->max_lun);
1146 for (i = 0; i <= sh[j]->max_channel; i++)
1147 printk("%s: SCSI channel %u enabled, host target ID %d.\n",
1148 BN(j), i, info.host_addr[3 - i]);
1150 #if defined(DEBUG_DETECT)
1151 printk("%s: Vers. 0x%x, ocs %u, tar %u, trnxfr %u, more %u, SYNC 0x%x, "\
1152 "sec. %u, infol %ld, cpl %ld spl %ld.\n", name, info.version,
1153 info.ocsena, info.tarsup, info.trnxfr, info.morsup, info.sync,
1154 info.second, DEV2H(info.data_len), DEV2H(info.cp_len),
1155 DEV2H(info.sp_len));
1157 if (protocol_rev == 'B' || protocol_rev == 'C')
1158 printk("%s: isaena %u, forcaddr %u, max_id %u, max_chan %u, "\
1159 "large_sg %u, res1 %u.\n", name, info.isaena, info.forcaddr,
1160 info.max_id, info.max_chan, info.large_sg, info.res1);
1162 if (protocol_rev == 'C')
1163 printk("%s: max_lun %u, m1 %u, idquest %u, pci %u, eisa %u, "\
1164 "raidnum %u.\n", name, info.max_lun, info.m1, info.idquest,
1165 info.pci, info.eisa, info.raidnum);
1166 #endif
1168 tune_pci_port(sh[j]->io_port);
1169 return TRUE;
1172 static void internal_setup(char *str, int *ints) {
1173 int i, argc = ints[0];
1174 char *cur = str, *pc;
1176 if (argc > 0) {
1178 if (argc > MAX_INT_PARAM) argc = MAX_INT_PARAM;
1180 for (i = 0; i < argc; i++) io_port[i] = ints[i + 1];
1182 io_port[i] = 0;
1183 setup_done = TRUE;
1186 while (cur && (pc = strchr(cur, ':'))) {
1187 int val = 0, c = *++pc;
1189 if (c == 'n' || c == 'N') val = FALSE;
1190 else if (c == 'y' || c == 'Y') val = TRUE;
1191 else val = (int) simple_strtoul(pc, NULL, 0);
1193 if (!strncmp(cur, "lc:", 3)) linked_comm = val;
1194 else if (!strncmp(cur, "tc:", 3)) tagged_comm = val;
1195 else if (!strncmp(cur, "tm:", 3)) tag_mode = val;
1196 else if (!strncmp(cur, "mq:", 3)) max_queue_depth = val;
1197 else if (!strncmp(cur, "ls:", 3)) link_statistics = val;
1198 else if (!strncmp(cur, "et:", 3)) ext_tran = val;
1199 else if (!strncmp(cur, "rs:", 3)) rev_scan = val;
1201 if ((cur = strchr(cur, ','))) ++cur;
1204 return;
1207 static int option_setup(char *str) {
1208 int ints[MAX_INT_PARAM];
1209 char *cur = str;
1210 int i = 1;
1212 while (cur && isdigit(*cur) && i <= MAX_INT_PARAM) {
1213 ints[i++] = simple_strtoul(cur, NULL, 0);
1215 if ((cur = strchr(cur, ',')) != NULL) cur++;
1218 ints[0] = i - 1;
1219 internal_setup(cur, ints);
1220 return 1;
1223 static void add_pci_ports(void) {
1225 #if defined(CONFIG_PCI)
1227 unsigned int addr, k;
1229 struct pci_dev *dev = NULL;
1231 if (!pci_present()) return;
1233 for (k = 0; k < MAX_PCI; k++) {
1235 if (!(dev = pci_find_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) break;
1237 if (pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &addr)) continue;
1239 #if defined(DEBUG_PCI_DETECT)
1240 printk("%s: detect, seq. %d, bus %d, devfn 0x%x, addr 0x%x.\n",
1241 driver_name, k, dev->bus->number, dev->devfn, addr);
1242 #endif
1244 if ((addr & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_IO)
1245 continue;
1247 /* Order addresses according to rev_scan value */
1248 io_port[MAX_INT_PARAM + (rev_scan ? (MAX_PCI - k) : (1 + k))] =
1249 (addr & PCI_BASE_ADDRESS_IO_MASK) + PCI_BASE_ADDRESS_0;
1252 #endif /* end CONFIG_PCI */
1254 return;
1257 int eata2x_detect(Scsi_Host_Template *tpnt) {
1258 unsigned int j = 0, k;
1260 tpnt->proc_name = "eata2x";
1262 if(boot_options) option_setup(boot_options);
1264 #if defined(MODULE)
1265 /* io_port could have been modified when loading as a module */
1266 if(io_port[0] != SKIP) {
1267 setup_done = TRUE;
1268 io_port[MAX_INT_PARAM] = 0;
1270 #endif
1272 for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
1274 if (!setup_done) add_pci_ports();
1276 for (k = 0; io_port[k]; k++) {
1278 if (io_port[k] == SKIP) continue;
1280 if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt)) j++;
1283 num_boards = j;
1284 return j;
1287 static inline void build_sg_list(struct mscp *cpp, Scsi_Cmnd *SCpnt) {
1288 unsigned int k;
1289 struct scatterlist *sgpnt;
1291 sgpnt = (struct scatterlist *) SCpnt->request_buffer;
1293 for (k = 0; k < SCpnt->use_sg; k++) {
1294 cpp->sglist[k].address = V2DEV(sgpnt[k].address);
1295 cpp->sglist[k].num_bytes = H2DEV(sgpnt[k].length);
1298 cpp->data_address = V2DEV(cpp->sglist);
1299 cpp->data_len = H2DEV((SCpnt->use_sg * sizeof(struct sg_list)));
1302 static inline int do_qcomm(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
1303 unsigned int i, j, k;
1304 struct mscp *cpp;
1305 struct mssp *spp;
1307 static const unsigned char data_out_cmds[] = {
1308 0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
1309 0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
1310 0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b
1313 static const unsigned char data_none_cmds[] = {
1314 0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
1315 0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
1316 0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5
1319 /* j is the board number */
1320 j = ((struct hostdata *) SCpnt->host->hostdata)->board_number;
1322 if (SCpnt->host_scribble)
1323 panic("%s: qcomm, pid %ld, SCpnt %p already active.\n",
1324 BN(j), SCpnt->pid, SCpnt);
1326 /* i is the mailbox number, look for the first free mailbox
1327 starting from last_cp_used */
1328 i = HD(j)->last_cp_used + 1;
1330 for (k = 0; k < sh[j]->can_queue; k++, i++) {
1332 if (i >= sh[j]->can_queue) i = 0;
1334 if (HD(j)->cp_stat[i] == FREE) {
1335 HD(j)->last_cp_used = i;
1336 break;
1340 if (k == sh[j]->can_queue) {
1341 printk("%s: qcomm, no free mailbox.\n", BN(j));
1342 return 1;
1345 /* Set pointer to control packet structure */
1346 cpp = &HD(j)->cp[i];
1348 memset(cpp, 0, sizeof(struct mscp) - sizeof(struct sg_list *));
1350 /* Set pointer to status packet structure */
1351 spp = &HD(j)->sp[0];
1353 /* The EATA protocol uses Big Endian format */
1354 cpp->sp_addr = V2DEV(spp);
1356 SCpnt->scsi_done = done;
1357 cpp->cpp_index = i;
1358 SCpnt->host_scribble = (unsigned char *) &cpp->cpp_index;
1360 if (do_trace) printk("%s: qcomm, mbox %d, target %d.%d:%d, pid %ld.\n",
1361 BN(j), i, SCpnt->channel, SCpnt->target,
1362 SCpnt->lun, SCpnt->pid);
1364 for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
1365 if (SCpnt->cmnd[0] == data_out_cmds[k]) {
1366 cpp->dout = TRUE;
1367 break;
1370 if ((cpp->din = !cpp->dout))
1371 for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
1372 if (SCpnt->cmnd[0] == data_none_cmds[k]) {
1373 cpp->din = FALSE;
1374 break;
1377 cpp->reqsen = TRUE;
1378 cpp->dispri = TRUE;
1379 #if 0
1380 if (SCpnt->device->type == TYPE_TAPE) cpp->hbaci = TRUE;
1381 #endif
1382 cpp->one = TRUE;
1383 cpp->channel = SCpnt->channel;
1384 cpp->target = SCpnt->target;
1385 cpp->lun = SCpnt->lun;
1386 cpp->SCpnt = SCpnt;
1387 cpp->sense_addr = V2DEV(SCpnt->sense_buffer);
1388 cpp->sense_len = sizeof SCpnt->sense_buffer;
1390 if (SCpnt->device->tagged_queue) {
1392 if (HD(j)->target_redo[SCpnt->target][SCpnt->channel] ||
1393 HD(j)->target_to[SCpnt->target][SCpnt->channel])
1394 cpp->mess[0] = ORDERED_QUEUE_TAG;
1395 else if (tag_mode == TAG_SIMPLE) cpp->mess[0] = SIMPLE_QUEUE_TAG;
1396 else if (tag_mode == TAG_HEAD) cpp->mess[0] = HEAD_OF_QUEUE_TAG;
1397 else if (tag_mode == TAG_ORDERED) cpp->mess[0] = ORDERED_QUEUE_TAG;
1398 else if (SCpnt->device->current_tag == 0)
1399 cpp->mess[0] = ORDERED_QUEUE_TAG;
1400 else if (SCpnt->device->current_tag == 1)
1401 cpp->mess[0] = HEAD_OF_QUEUE_TAG;
1402 else
1403 cpp->mess[0] = SIMPLE_QUEUE_TAG;
1405 cpp->mess[1] = SCpnt->device->current_tag++;
1408 if (SCpnt->use_sg) {
1409 cpp->sg = TRUE;
1410 build_sg_list(cpp, SCpnt);
1412 else {
1413 cpp->data_address = V2DEV(SCpnt->request_buffer);
1414 cpp->data_len = H2DEV(SCpnt->request_bufflen);
1417 memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
1419 if (linked_comm && SCpnt->device->queue_depth > 2
1420 && TLDEV(SCpnt->device->type)) {
1421 HD(j)->cp_stat[i] = READY;
1422 flush_dev(SCpnt->device, SCpnt->request.sector, j, FALSE);
1423 return 0;
1426 /* Send control packet to the board */
1427 if (do_dma(sh[j]->io_port, (unsigned long) cpp, SEND_CP_DMA)) {
1428 SCpnt->host_scribble = NULL;
1429 printk("%s: qcomm, target %d.%d:%d, pid %ld, adapter busy.\n",
1430 BN(j), SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid);
1431 return 1;
1434 HD(j)->cp_stat[i] = IN_USE;
1435 return 0;
1438 int eata2x_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
1439 int rtn;
1441 rtn = do_qcomm(SCpnt, done);
1442 return rtn;
1445 static inline int do_abort(Scsi_Cmnd *SCarg) {
1446 unsigned int i, j;
1448 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1450 if (SCarg->host_scribble == NULL) {
1451 printk("%s: abort, target %d.%d:%d, pid %ld inactive.\n",
1452 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1453 return SUCCESS;
1456 i = *(unsigned int *)SCarg->host_scribble;
1457 printk("%s: abort, mbox %d, target %d.%d:%d, pid %ld.\n",
1458 BN(j), i, SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1460 if (i >= sh[j]->can_queue)
1461 panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
1463 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1464 printk("%s: abort, timeout error.\n", BN(j));
1465 return FAILED;
1468 if (HD(j)->cp_stat[i] == FREE) {
1469 printk("%s: abort, mbox %d is free.\n", BN(j), i);
1470 return SUCCESS;
1473 if (HD(j)->cp_stat[i] == IN_USE) {
1474 printk("%s: abort, mbox %d is in use.\n", BN(j), i);
1476 if (SCarg != HD(j)->cp[i].SCpnt)
1477 panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1478 BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
1480 if (inb(sh[j]->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)
1481 printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
1483 if (SCarg->eh_state == SCSI_STATE_TIMEOUT) {
1484 SCarg->host_scribble = NULL;
1485 HD(j)->cp_stat[i] = FREE;
1486 printk("%s, abort, mbox %d, eh_state timeout, pid %ld.\n",
1487 BN(j), i, SCarg->pid);
1488 return SUCCESS;
1491 return FAILED;
1494 if (HD(j)->cp_stat[i] == IN_RESET) {
1495 printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
1496 return FAILED;
1499 if (HD(j)->cp_stat[i] == LOCKED) {
1500 printk("%s: abort, mbox %d is locked.\n", BN(j), i);
1501 return SUCCESS;
1504 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1505 SCarg->result = DID_ABORT << 16;
1506 SCarg->host_scribble = NULL;
1507 HD(j)->cp_stat[i] = FREE;
1508 printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1509 BN(j), i, SCarg->pid);
1510 SCarg->scsi_done(SCarg);
1511 return SUCCESS;
1514 panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
1517 int eata2x_abort(Scsi_Cmnd *SCarg) {
1519 return do_abort(SCarg);
1522 static inline int do_reset(Scsi_Cmnd *SCarg) {
1523 unsigned int i, j, time, k, c, limit = 0;
1524 int arg_done = FALSE;
1525 Scsi_Cmnd *SCpnt;
1527 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1528 printk("%s: reset, enter, target %d.%d:%d, pid %ld.\n",
1529 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1531 if (SCarg->host_scribble == NULL)
1532 printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
1534 if (HD(j)->in_reset) {
1535 printk("%s: reset, exit, already in reset.\n", BN(j));
1536 return FAILED;
1539 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1540 printk("%s: reset, exit, timeout error.\n", BN(j));
1541 return FAILED;
1544 HD(j)->retries = 0;
1546 for (c = 0; c <= sh[j]->max_channel; c++)
1547 for (k = 0; k < sh[j]->max_id; k++) {
1548 HD(j)->target_redo[k][c] = TRUE;
1549 HD(j)->target_to[k][c] = 0;
1552 for (i = 0; i < sh[j]->can_queue; i++) {
1554 if (HD(j)->cp_stat[i] == FREE) continue;
1556 if (HD(j)->cp_stat[i] == LOCKED) {
1557 HD(j)->cp_stat[i] = FREE;
1558 printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
1559 continue;
1562 if (!(SCpnt = HD(j)->cp[i].SCpnt))
1563 panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
1565 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1566 HD(j)->cp_stat[i] = ABORTING;
1567 printk("%s: reset, mbox %d aborting, pid %ld.\n",
1568 BN(j), i, SCpnt->pid);
1571 else {
1572 HD(j)->cp_stat[i] = IN_RESET;
1573 printk("%s: reset, mbox %d in reset, pid %ld.\n",
1574 BN(j), i, SCpnt->pid);
1577 if (SCpnt->host_scribble == NULL)
1578 panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
1580 if (*(unsigned int *)SCpnt->host_scribble != i)
1581 panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
1583 if (SCpnt->scsi_done == NULL)
1584 panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
1586 if (SCpnt == SCarg) arg_done = TRUE;
1589 if (do_dma(sh[j]->io_port, 0, RESET_PIO)) {
1590 printk("%s: reset, cannot reset, timeout error.\n", BN(j));
1591 return FAILED;
1594 printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
1596 #if defined(DEBUG_RESET)
1597 do_trace = TRUE;
1598 #endif
1600 HD(j)->in_reset = TRUE;
1601 SPIN_UNLOCK
1602 time = jiffies;
1603 while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
1604 SPIN_LOCK
1605 printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
1607 for (i = 0; i < sh[j]->can_queue; i++) {
1609 if (HD(j)->cp_stat[i] == IN_RESET) {
1610 SCpnt = HD(j)->cp[i].SCpnt;
1611 SCpnt->result = DID_RESET << 16;
1612 SCpnt->host_scribble = NULL;
1614 /* This mailbox is still waiting for its interrupt */
1615 HD(j)->cp_stat[i] = LOCKED;
1617 printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
1618 BN(j), i, SCpnt->pid);
1621 else if (HD(j)->cp_stat[i] == ABORTING) {
1622 SCpnt = HD(j)->cp[i].SCpnt;
1623 SCpnt->result = DID_RESET << 16;
1624 SCpnt->host_scribble = NULL;
1626 /* This mailbox was never queued to the adapter */
1627 HD(j)->cp_stat[i] = FREE;
1629 printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
1630 BN(j), i, SCpnt->pid);
1633 else
1635 /* Any other mailbox has already been set free by interrupt */
1636 continue;
1638 SCpnt->scsi_done(SCpnt);
1641 HD(j)->in_reset = FALSE;
1642 do_trace = FALSE;
1644 if (arg_done) printk("%s: reset, exit, pid %ld done.\n", BN(j), SCarg->pid);
1645 else printk("%s: reset, exit.\n", BN(j));
1647 return SUCCESS;
1650 int eata2x_reset(Scsi_Cmnd *SCarg) {
1652 return do_reset(SCarg);
1655 int eata2x_biosparam(Disk *disk, kdev_t dev, int *dkinfo) {
1656 int size = disk->capacity;
1658 if (ext_tran || (scsicam_bios_param(disk, dev, dkinfo) < 0)) {
1659 dkinfo[0] = 255;
1660 dkinfo[1] = 63;
1661 dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
1664 #if defined (DEBUG_GEOMETRY)
1665 printk ("%s: biosparam, head=%d, sec=%d, cyl=%d.\n", driver_name,
1666 dkinfo[0], dkinfo[1], dkinfo[2]);
1667 #endif
1669 return FALSE;
1672 static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
1673 unsigned int rev) {
1674 unsigned int i, j, k, y;
1675 unsigned long x;
1677 for (i = 0; i < n - 1; i++) {
1678 k = i;
1680 for (j = k + 1; j < n; j++)
1681 if (rev) {
1682 if (sk[j] > sk[k]) k = j;
1684 else {
1685 if (sk[j] < sk[k]) k = j;
1688 if (k != i) {
1689 x = sk[k]; sk[k] = sk[i]; sk[i] = x;
1690 y = da[k]; da[k] = da[i]; da[i] = y;
1694 return;
1697 static inline int reorder(unsigned int j, unsigned long cursec,
1698 unsigned int ihdlr, unsigned int il[], unsigned int n_ready) {
1699 Scsi_Cmnd *SCpnt;
1700 struct mscp *cpp;
1701 unsigned int k, n;
1702 unsigned int rev = FALSE, s = TRUE, r = TRUE;
1703 unsigned int input_only = TRUE, overlap = FALSE;
1704 unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
1705 unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
1706 unsigned long ioseek = 0;
1708 static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
1709 static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
1710 static unsigned int readysorted = 0, revcount = 0;
1711 static unsigned long seeksorted = 0, seeknosort = 0;
1713 if (link_statistics && !(++flushcount % link_statistics))
1714 printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"\
1715 " av %ldK as %ldK.\n", flushcount, batchcount, inputcount,
1716 ovlcount, readycount, readysorted, sortcount, revcount,
1717 seeknosort / (readycount + 1),
1718 seeksorted / (readycount + 1));
1720 if (n_ready <= 1) return FALSE;
1722 for (n = 0; n < n_ready; n++) {
1723 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1725 if (!cpp->din) input_only = FALSE;
1727 if (SCpnt->request.sector < minsec) minsec = SCpnt->request.sector;
1728 if (SCpnt->request.sector > maxsec) maxsec = SCpnt->request.sector;
1730 sl[n] = SCpnt->request.sector;
1731 ioseek += SCpnt->request.nr_sectors;
1733 if (!n) continue;
1735 if (sl[n] < sl[n - 1]) s = FALSE;
1736 if (sl[n] > sl[n - 1]) r = FALSE;
1738 if (link_statistics) {
1739 if (sl[n] > sl[n - 1])
1740 seek += sl[n] - sl[n - 1];
1741 else
1742 seek += sl[n - 1] - sl[n];
1747 if (link_statistics) {
1748 if (cursec > sl[0]) seek += cursec - sl[0]; else seek += sl[0] - cursec;
1751 if (cursec > ((maxsec + minsec) / 2)) rev = TRUE;
1753 if (ioseek > ((maxsec - minsec) / 2)) rev = FALSE;
1755 if (!((rev && r) || (!rev && s))) sort(sl, il, n_ready, rev);
1757 if (!input_only) for (n = 0; n < n_ready; n++) {
1758 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1759 ll[n] = SCpnt->request.nr_sectors; pl[n] = SCpnt->pid;
1761 if (!n) continue;
1763 if ((sl[n] == sl[n - 1]) || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
1764 || (rev && ((sl[n] + ll[n]) > sl[n - 1]))) overlap = TRUE;
1767 if (overlap) sort(pl, il, n_ready, FALSE);
1769 if (link_statistics) {
1770 if (cursec > sl[0]) iseek = cursec - sl[0]; else iseek = sl[0] - cursec;
1771 batchcount++; readycount += n_ready; seeknosort += seek / 1024;
1772 if (input_only) inputcount++;
1773 if (overlap) { ovlcount++; seeksorted += iseek / 1024; }
1774 else seeksorted += (iseek + maxsec - minsec) / 1024;
1775 if (rev && !r) { revcount++; readysorted += n_ready; }
1776 if (!rev && !s) { sortcount++; readysorted += n_ready; }
1779 #if defined(DEBUG_LINKED_COMMANDS)
1780 if (link_statistics && (overlap || !(flushcount % link_statistics)))
1781 for (n = 0; n < n_ready; n++) {
1782 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1783 printk("%s %d.%d:%d pid %ld mb %d fc %d nr %d sec %ld ns %ld"\
1784 " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
1785 (ihdlr ? "ihdlr" : "qcomm"), SCpnt->channel, SCpnt->target,
1786 SCpnt->lun, SCpnt->pid, k, flushcount, n_ready,
1787 SCpnt->request.sector, SCpnt->request.nr_sectors, cursec,
1788 YESNO(s), YESNO(r), YESNO(rev), YESNO(input_only),
1789 YESNO(overlap), cpp->din);
1791 #endif
1792 return overlap;
1795 static void flush_dev(Scsi_Device *dev, unsigned long cursec, unsigned int j,
1796 unsigned int ihdlr) {
1797 Scsi_Cmnd *SCpnt;
1798 struct mscp *cpp;
1799 unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
1801 for (k = 0; k < sh[j]->can_queue; k++) {
1803 if (HD(j)->cp_stat[k] != READY && HD(j)->cp_stat[k] != IN_USE) continue;
1805 cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1807 if (SCpnt->device != dev) continue;
1809 if (HD(j)->cp_stat[k] == IN_USE) return;
1811 il[n_ready++] = k;
1814 if (reorder(j, cursec, ihdlr, il, n_ready)) n_ready = 1;
1816 for (n = 0; n < n_ready; n++) {
1817 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1819 if (do_dma(sh[j]->io_port, (unsigned long) cpp, SEND_CP_DMA)) {
1820 printk("%s: %s, target %d.%d:%d, pid %ld, mbox %d, adapter"\
1821 " busy, will abort.\n", BN(j), (ihdlr ? "ihdlr" : "qcomm"),
1822 SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid, k);
1823 HD(j)->cp_stat[k] = ABORTING;
1824 continue;
1827 HD(j)->cp_stat[k] = IN_USE;
1832 static inline void ihdlr(int irq, unsigned int j) {
1833 Scsi_Cmnd *SCpnt;
1834 unsigned int i, k, c, status, tstatus, reg;
1835 struct mssp *dspp, *spp;
1836 struct mscp *cpp;
1838 if (sh[j]->irq != irq)
1839 panic("%s: ihdlr, irq %d, sh[j]->irq %d.\n", BN(j), irq, sh[j]->irq);
1841 /* Check if this board need to be serviced */
1842 if (!(inb(sh[j]->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)) return;
1844 HD(j)->iocount++;
1846 if (do_trace) printk("%s: ihdlr, enter, irq %d, count %d.\n", BN(j), irq,
1847 HD(j)->iocount);
1849 /* Check if this board is still busy */
1850 if (wait_on_busy(sh[j]->io_port, 20 * MAXLOOP)) {
1851 reg = inb(sh[j]->io_port + REG_STATUS);
1852 printk("%s: ihdlr, busy timeout error, irq %d, reg 0x%x, count %d.\n",
1853 BN(j), irq, reg, HD(j)->iocount);
1854 return;
1857 dspp = &HD(j)->sp[0];
1858 spp = &HD(j)->sp[1];
1860 /* Make a local copy just before clearing the interrupt indication */
1861 memcpy(spp, dspp, sizeof(struct mssp));
1863 /* Clear the completion flag and cp pointer on the dynamic copy of sp */
1864 memset(dspp, 0, sizeof(struct mssp));
1866 /* Read the status register to clear the interrupt indication */
1867 reg = inb(sh[j]->io_port + REG_STATUS);
1869 /* Reject any sp with supspect data */
1870 if (spp->eoc == FALSE)
1871 printk("%s: ihdlr, spp->eoc == FALSE, irq %d, reg 0x%x, count %d.\n",
1872 BN(j), irq, reg, HD(j)->iocount);
1873 if (spp->cpp_index < 0 || spp->cpp_index >= sh[j]->can_queue)
1874 printk("%s: ihdlr, bad spp->cpp_index %d, irq %d, reg 0x%x, count %d.\n",
1875 BN(j), spp->cpp_index, irq, reg, HD(j)->iocount);
1876 if (spp->eoc == FALSE || spp->cpp_index < 0
1877 || spp->cpp_index >= sh[j]->can_queue) return;
1879 /* Find the mailbox to be serviced on this board */
1880 i = spp->cpp_index;
1882 cpp = &(HD(j)->cp[i]);
1884 #if defined(DEBUG_GENERATE_ABORTS)
1885 if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 500) < 3)) return;
1886 #endif
1888 if (HD(j)->cp_stat[i] == IGNORE) {
1889 HD(j)->cp_stat[i] = FREE;
1890 return;
1892 else if (HD(j)->cp_stat[i] == LOCKED) {
1893 HD(j)->cp_stat[i] = FREE;
1894 printk("%s: ihdlr, mbox %d unlocked, count %d.\n", BN(j), i,
1895 HD(j)->iocount);
1896 return;
1898 else if (HD(j)->cp_stat[i] == FREE) {
1899 printk("%s: ihdlr, mbox %d is free, count %d.\n", BN(j), i,
1900 HD(j)->iocount);
1901 return;
1903 else if (HD(j)->cp_stat[i] == IN_RESET)
1904 printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
1905 else if (HD(j)->cp_stat[i] != IN_USE)
1906 panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
1907 BN(j), i, HD(j)->cp_stat[i]);
1909 HD(j)->cp_stat[i] = FREE;
1910 SCpnt = cpp->SCpnt;
1912 if (SCpnt == NULL) panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
1914 if (SCpnt->host_scribble == NULL)
1915 panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", BN(j), i,
1916 SCpnt->pid, SCpnt);
1918 if (*(unsigned int *)SCpnt->host_scribble != i)
1919 panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
1920 BN(j), i, SCpnt->pid, *(unsigned int *)SCpnt->host_scribble);
1922 if (linked_comm && SCpnt->device->queue_depth > 2
1923 && TLDEV(SCpnt->device->type))
1924 flush_dev(SCpnt->device, SCpnt->request.sector, j, TRUE);
1926 tstatus = status_byte(spp->target_status);
1928 #if defined(DEBUG_GENERATE_ERRORS)
1929 if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 200) < 2))
1930 spp->adapter_status = 0x01;
1931 #endif
1933 switch (spp->adapter_status) {
1934 case ASOK: /* status OK */
1936 /* Forces a reset if a disk drive keeps returning BUSY */
1937 if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
1938 status = DID_ERROR << 16;
1940 /* If there was a bus reset, redo operation on each target */
1941 else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
1942 && HD(j)->target_redo[SCpnt->target][SCpnt->channel])
1943 status = DID_BUS_BUSY << 16;
1945 /* Works around a flaw in scsi.c */
1946 else if (tstatus == CHECK_CONDITION
1947 && SCpnt->device->type == TYPE_DISK
1948 && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
1949 status = DID_BUS_BUSY << 16;
1951 else
1952 status = DID_OK << 16;
1954 if (tstatus == GOOD)
1955 HD(j)->target_redo[SCpnt->target][SCpnt->channel] = FALSE;
1957 if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
1958 (!(tstatus == CHECK_CONDITION && HD(j)->iocount <= 1000 &&
1959 (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
1960 printk("%s: ihdlr, target %d.%d:%d, pid %ld, "\
1961 "target_status 0x%x, sense key 0x%x.\n", BN(j),
1962 SCpnt->channel, SCpnt->target, SCpnt->lun,
1963 SCpnt->pid, spp->target_status,
1964 SCpnt->sense_buffer[2]);
1966 HD(j)->target_to[SCpnt->target][SCpnt->channel] = 0;
1968 if (HD(j)->last_retried_pid == SCpnt->pid) HD(j)->retries = 0;
1970 break;
1971 case ASST: /* Selection Time Out */
1972 case 0x02: /* Command Time Out */
1974 if (HD(j)->target_to[SCpnt->target][SCpnt->channel] > 1)
1975 status = DID_ERROR << 16;
1976 else {
1977 status = DID_TIME_OUT << 16;
1978 HD(j)->target_to[SCpnt->target][SCpnt->channel]++;
1981 break;
1983 /* Perform a limited number of internal retries */
1984 case 0x03: /* SCSI Bus Reset Received */
1985 case 0x04: /* Initial Controller Power-up */
1987 for (c = 0; c <= sh[j]->max_channel; c++)
1988 for (k = 0; k < sh[j]->max_id; k++)
1989 HD(j)->target_redo[k][c] = TRUE;
1991 if (SCpnt->device->type != TYPE_TAPE
1992 && HD(j)->retries < MAX_INTERNAL_RETRIES) {
1994 #if defined(DID_SOFT_ERROR)
1995 status = DID_SOFT_ERROR << 16;
1996 #else
1997 status = DID_BUS_BUSY << 16;
1998 #endif
1999 HD(j)->retries++;
2000 HD(j)->last_retried_pid = SCpnt->pid;
2002 else
2003 status = DID_ERROR << 16;
2005 break;
2006 case 0x05: /* Unexpected Bus Phase */
2007 case 0x06: /* Unexpected Bus Free */
2008 case 0x07: /* Bus Parity Error */
2009 case 0x08: /* SCSI Hung */
2010 case 0x09: /* Unexpected Message Reject */
2011 case 0x0a: /* SCSI Bus Reset Stuck */
2012 case 0x0b: /* Auto Request-Sense Failed */
2013 case 0x0c: /* Controller Ram Parity Error */
2014 default:
2015 status = DID_ERROR << 16;
2016 break;
2019 SCpnt->result = status | spp->target_status;
2021 #if defined(DEBUG_INTERRUPT)
2022 if (SCpnt->result || do_trace)
2023 #else
2024 if ((spp->adapter_status != ASOK && HD(j)->iocount > 1000) ||
2025 (spp->adapter_status != ASOK &&
2026 spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
2027 do_trace || msg_byte(spp->target_status))
2028 #endif
2029 printk("%s: ihdlr, mbox %2d, err 0x%x:%x,"\
2030 " target %d.%d:%d, pid %ld, reg 0x%x, count %d.\n",
2031 BN(j), i, spp->adapter_status, spp->target_status,
2032 SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid,
2033 reg, HD(j)->iocount);
2035 /* Set the command state to inactive */
2036 SCpnt->host_scribble = NULL;
2038 SCpnt->scsi_done(SCpnt);
2040 if (do_trace) printk("%s: ihdlr, exit, irq %d, count %d.\n", BN(j), irq,
2041 HD(j)->iocount);
2043 return;
2046 static void do_interrupt_handler(int irq, void *shap, struct pt_regs *regs) {
2047 unsigned int j;
2048 SPIN_FLAGS
2050 /* Check if the interrupt must be processed by this handler */
2051 if ((j = (unsigned int)((char *)shap - sha)) >= num_boards) return;
2053 SPIN_LOCK_SAVE
2054 ihdlr(irq, j);
2055 SPIN_UNLOCK_RESTORE
2058 int eata2x_release(struct Scsi_Host *shpnt) {
2059 unsigned int i, j;
2061 for (j = 0; sh[j] != NULL && sh[j] != shpnt; j++);
2063 if (sh[j] == NULL) panic("%s: release, invalid Scsi_Host pointer.\n",
2064 driver_name);
2066 if( sh[j]->unchecked_isa_dma ) {
2067 scsi_deregister_blocked_host(sh[j]);
2070 for (i = 0; i < sh[j]->can_queue; i++)
2071 if ((&HD(j)->cp[i])->sglist) kfree((&HD(j)->cp[i])->sglist);
2073 free_irq(sh[j]->irq, &sha[j]);
2075 if (sh[j]->dma_channel != NO_DMA) free_dma(sh[j]->dma_channel);
2077 release_region(sh[j]->io_port, sh[j]->n_io_port);
2078 scsi_unregister(sh[j]);
2079 return FALSE;
2082 static Scsi_Host_Template driver_template = EATA;
2084 #include "scsi_module.c"
2086 #ifndef MODULE
2087 __setup("eata=", option_setup);
2088 #endif /* end MODULE */