Import 2.3.18pre1
[davej-history.git] / drivers / scsi / eata.c
blob985453a0c5e5a45149360972f9d1b1b4f703cc6c
1 /*
2 * eata.c - Low-level driver for EATA/DMA SCSI host adapters.
4 * 26 Jul 1998 Rev. 4.33 for linux 2.0.35 and 2.1.111
5 * + Added command line option (rs:[y|n]) to reverse the scan order
6 * of PCI boards. The default is rs:y, which reverses the BIOS order
7 * while registering PCI boards. The default value rs:y generates
8 * the same order of all previous revisions of this driver.
9 * Pls. note that "BIOS order" might have been reversed itself
10 * after the 2.1.9x PCI modifications in the linux kernel.
11 * The rs value is ignored when the explicit list of addresses
12 * is used by the "eata=port0,port1,..." command line option.
13 * + Added command line option (et:[y|n]) to force use of extended
14 * translation (255 heads, 63 sectors) as disk geometry.
15 * The default is et:n, which uses the disk geometry returned
16 * by scsicam_bios_param. The default value et:n is compatible with
17 * all previous revisions of this driver.
19 * 28 May 1998 Rev. 4.32 for linux 2.0.33 and 2.1.104
20 * Increased busy timeout from 10 msec. to 200 msec. while
21 * processing interrupts.
23 * 16 May 1998 Rev. 4.31 for linux 2.0.33 and 2.1.102
24 * Improved abort handling during the eh recovery process.
26 * 13 May 1998 Rev. 4.30 for linux 2.0.33 and 2.1.101
27 * The driver is now fully SMP safe, including the
28 * abort and reset routines.
29 * Added command line options (eh:[y|n]) to choose between
30 * new_eh_code and the old scsi code.
31 * If linux version >= 2.1.101 the default is eh:y, while the eh
32 * option is ignored for previous releases and the old scsi code
33 * is used.
35 * 18 Apr 1998 Rev. 4.20 for linux 2.0.33 and 2.1.97
36 * Reworked interrupt handler.
38 * 11 Apr 1998 rev. 4.05 for linux 2.0.33 and 2.1.95
39 * Major reliability improvement: when a batch with overlapping
40 * requests is detected, requests are queued one at a time
41 * eliminating any possible board or drive reordering.
43 * 10 Apr 1998 rev. 4.04 for linux 2.0.33 and 2.1.95
44 * Improved SMP support (if linux version >= 2.1.95).
46 * 9 Apr 1998 rev. 4.03 for linux 2.0.33 and 2.1.94
47 * Added support for new PCI code and IO-APIC remapping of irqs.
48 * Performance improvement: when sequential i/o is detected,
49 * always use direct sort instead of reverse sort.
51 * 4 Apr 1998 rev. 4.02 for linux 2.0.33 and 2.1.92
52 * io_port is now unsigned long.
54 * 17 Mar 1998 rev. 4.01 for linux 2.0.33 and 2.1.88
55 * Use new scsi error handling code (if linux version >= 2.1.88).
56 * Use new interrupt code.
58 * 12 Sep 1997 rev. 3.11 for linux 2.0.30 and 2.1.55
59 * Use of udelay inside the wait loops to avoid timeout
60 * problems with fast cpus.
61 * Removed check about useless calls to the interrupt service
62 * routine (reported on SMP systems only).
63 * At initialization time "sorted/unsorted" is displayed instead
64 * of "linked/unlinked" to reinforce the fact that "linking" is
65 * nothing but "elevator sorting" in the actual implementation.
67 * 17 May 1997 rev. 3.10 for linux 2.0.30 and 2.1.38
68 * Use of serial_number_at_timeout in abort and reset processing.
69 * Use of the __initfunc and __initdata macro in setup code.
70 * Minor cleanups in the list_statistics code.
71 * Increased controller busy timeout in order to better support
72 * slow SCSI devices.
74 * 24 Feb 1997 rev. 3.00 for linux 2.0.29 and 2.1.26
75 * When loading as a module, parameter passing is now supported
76 * both in 2.0 and in 2.1 style.
77 * Fixed data transfer direction for some SCSI opcodes.
78 * Immediate acknowledge to request sense commands.
79 * Linked commands to each disk device are now reordered by elevator
80 * sorting. Rare cases in which reordering of write requests could
81 * cause wrong results are managed.
82 * Fixed spurious timeouts caused by long simple queue tag sequences.
83 * New command line option (tm:[0-3]) to choose the type of tags:
84 * 0 -> mixed (default); 1 -> simple; 2 -> head; 3 -> ordered.
86 * 18 Jan 1997 rev. 2.60 for linux 2.1.21 and 2.0.28
87 * Added command line options to enable/disable linked commands
88 * (lc:[y|n]), tagged commands (tc:[y|n]) and to set the max queue
89 * depth (mq:xx). Default is "eata=lc:n,tc:n,mq:16".
90 * Improved command linking.
91 * Documented how to setup RAID-0 with DPT SmartRAID boards.
93 * 8 Jan 1997 rev. 2.50 for linux 2.1.20 and 2.0.27
94 * Added linked command support.
95 * Improved detection of PCI boards using ISA base addresses.
97 * 3 Dec 1996 rev. 2.40 for linux 2.1.14 and 2.0.27
98 * Added support for tagged commands and queue depth adjustment.
100 * 22 Nov 1996 rev. 2.30 for linux 2.1.12 and 2.0.26
101 * When CONFIG_PCI is defined, BIOS32 is used to include in the
102 * list of i/o ports to be probed all the PCI SCSI controllers.
103 * The list of i/o ports to be probed can be overwritten by the
104 * "eata=port0,port1,...." boot command line option.
105 * Scatter/gather lists are now allocated by a number of kmalloc
106 * calls, in order to avoid the previous size limit of 64Kb.
108 * 16 Nov 1996 rev. 2.20 for linux 2.1.10 and 2.0.25
109 * Added support for EATA 2.0C, PCI, multichannel and wide SCSI.
111 * 27 Sep 1996 rev. 2.12 for linux 2.1.0
112 * Portability cleanups (virtual/bus addressing, little/big endian
113 * support).
115 * 09 Jul 1996 rev. 2.11 for linux 2.0.4
116 * Number of internal retries is now limited.
118 * 16 Apr 1996 rev. 2.10 for linux 1.3.90
119 * New argument "reset_flags" to the reset routine.
121 * 6 Jul 1995 rev. 2.01 for linux 1.3.7
122 * Update required by the new /proc/scsi support.
124 * 11 Mar 1995 rev. 2.00 for linux 1.2.0
125 * Fixed a bug which prevented media change detection for removable
126 * disk drives.
128 * 23 Feb 1995 rev. 1.18 for linux 1.1.94
129 * Added a check for scsi_register returning NULL.
131 * 11 Feb 1995 rev. 1.17 for linux 1.1.91
132 * Now DEBUG_RESET is disabled by default.
133 * Register a board even if it does not assert DMA protocol support
134 * (DPT SK2011B does not report correctly the dmasup bit).
136 * 9 Feb 1995 rev. 1.16 for linux 1.1.90
137 * Use host->wish_block instead of host->block.
138 * New list of Data Out SCSI commands.
140 * 8 Feb 1995 rev. 1.15 for linux 1.1.89
141 * Cleared target_time_out counter while performing a reset.
142 * All external symbols renamed to avoid possible name conflicts.
144 * 28 Jan 1995 rev. 1.14 for linux 1.1.86
145 * Added module support.
146 * Log and do a retry when a disk drive returns a target status
147 * different from zero on a recovered error.
149 * 24 Jan 1995 rev. 1.13 for linux 1.1.85
150 * Use optimized board configuration, with a measured performance
151 * increase in the range 10%-20% on i/o throughput.
153 * 16 Jan 1995 rev. 1.12 for linux 1.1.81
154 * Fix mscp structure comments (no functional change).
155 * Display a message if check_region detects a port address
156 * already in use.
158 * 17 Dec 1994 rev. 1.11 for linux 1.1.74
159 * Use the scsicam_bios_param routine. This allows an easy
160 * migration path from disk partition tables created using
161 * different SCSI drivers and non optimal disk geometry.
163 * 15 Dec 1994 rev. 1.10 for linux 1.1.74
164 * Added support for ISA EATA boards (DPT PM2011, DPT PM2021).
165 * The host->block flag is set for all the detected ISA boards.
166 * The detect routine no longer enforces LEVEL triggering
167 * for EISA boards, it just prints a warning message.
169 * 30 Nov 1994 rev. 1.09 for linux 1.1.68
170 * Redo i/o on target status CHECK_CONDITION for TYPE_DISK only.
171 * Added optional support for using a single board at a time.
173 * 18 Nov 1994 rev. 1.08 for linux 1.1.64
174 * Forces sg_tablesize = 64 and can_queue = 64 if these
175 * values are not correctly detected (DPT PM2012).
177 * 14 Nov 1994 rev. 1.07 for linux 1.1.63 Final BETA release.
178 * 04 Aug 1994 rev. 1.00 for linux 1.1.39 First BETA release.
181 * This driver is based on the CAM (Common Access Method Committee)
182 * EATA (Enhanced AT Bus Attachment) rev. 2.0A, using DMA protocol.
184 * Copyright (C) 1994-1998 Dario Ballabio (dario@milano.europe.dg.com)
186 * Redistribution and use in source and binary forms, with or without
187 * modification, are permitted provided that redistributions of source
188 * code retain the above copyright notice and this comment without
189 * modification.
195 * Here is a brief description of the DPT SCSI host adapters.
196 * All these boards provide an EATA/DMA compatible programming interface
197 * and are fully supported by this driver in any configuration, including
198 * multiple SCSI channels:
200 * PM2011B/9X - Entry Level ISA
201 * PM2021A/9X - High Performance ISA
202 * PM2012A Old EISA
203 * PM2012B Old EISA
204 * PM2022A/9X - Entry Level EISA
205 * PM2122A/9X - High Performance EISA
206 * PM2322A/9X - Extra High Performance EISA
207 * PM3021 - SmartRAID Adapter for ISA
208 * PM3222 - SmartRAID Adapter for EISA (PM3222W is 16-bit wide SCSI)
209 * PM3224 - SmartRAID Adapter for PCI (PM3224W is 16-bit wide SCSI)
211 * The above list is just an indication: as a matter of fact all DPT
212 * boards using the EATA/DMA protocol are supported by this driver,
213 * since they use exactely the same programming interface.
215 * The DPT PM2001 provides only the EATA/PIO interface and hence is not
216 * supported by this driver.
218 * This code has been tested with up to 3 Distributed Processing Technology
219 * PM2122A/9X (DPT SCSI BIOS v002.D1, firmware v05E.0) EISA controllers,
220 * in any combination of private and shared IRQ.
221 * PCI support has been tested using up to 2 DPT PM3224W (DPT SCSI BIOS
222 * v003.D0, firmware v07G.0).
224 * DPT SmartRAID boards support "Hardware Array" - a group of disk drives
225 * which are all members of the same RAID-0, RAID-1 or RAID-5 array implemented
226 * in host adapter hardware. Hardware Arrays are fully compatible with this
227 * driver, since they look to it as a single disk drive.
229 * WARNING: to create a RAID-0 "Hardware Array" you must select "Other Unix"
230 * as the current OS in the DPTMGR "Initial System Installation" menu.
231 * Otherwise RAID-0 is generated as an "Array Group" (i.e. software RAID-0),
232 * which is not supported by the actual SCSI subsystem.
233 * To get the "Array Group" functionality, the Linux MD driver must be used
234 * instead of the DPT "Array Group" feature.
236 * Multiple ISA, EISA and PCI boards can be configured in the same system.
237 * It is suggested to put all the EISA boards on the same IRQ level, all
238 * the PCI boards on another IRQ level, while ISA boards cannot share
239 * interrupts.
241 * If you configure multiple boards on the same IRQ, the interrupt must
242 * be _level_ triggered (not _edge_ triggered).
244 * This driver detects EATA boards by probes at fixed port addresses,
245 * so no BIOS32 or PCI BIOS support is required.
246 * The suggested way to detect a generic EATA PCI board is to force on it
247 * any unused EISA address, even if there are other controllers on the EISA
248 * bus, or even if you system has no EISA bus at all.
249 * Do not force any ISA address on EATA PCI boards.
251 * If PCI bios support is configured into the kernel, BIOS32 is used to
252 * include in the list of i/o ports to be probed all the PCI SCSI controllers.
254 * Due to a DPT BIOS "feature", it might not be possible to force an EISA
255 * address on more then a single DPT PCI board, so in this case you have to
256 * let the PCI BIOS assign the addresses.
258 * The sequence of detection probes is:
260 * - ISA 0x1F0;
261 * - PCI SCSI controllers (only if BIOS32 is available);
262 * - EISA/PCI 0x1C88 through 0xFC88 (corresponding to EISA slots 1 to 15);
263 * - ISA 0x170, 0x230, 0x330.
265 * The above list of detection probes can be totally replaced by the
266 * boot command line option: "eata=port0,port1,port2,...", where the
267 * port0, port1... arguments are ISA/EISA/PCI addresses to be probed.
268 * For example using "eata=0x7410,0x7450,0x230", the driver probes
269 * only the two PCI addresses 0x7410 and 0x7450 and the ISA address 0x230,
270 * in this order; "eata=0" totally disables this driver.
272 * After the optional list of detection probes, other possible command line
273 * options are:
275 * eh:y use new scsi code (linux 2.2 only);
276 * eh:n use old scsi code;
277 * et:y force use of extended translation (255 heads, 63 sectors);
278 * et:n use disk geometry detected by scsicam_bios_param;
279 * rs:y reverse scan order while detecting PCI boards;
280 * rs:n use BIOS order while detecting PCI boards;
281 * lc:y enables linked commands;
282 * lc:n disables linked commands;
283 * tc:y enables tagged commands;
284 * tc:n disables tagged commands;
285 * tm:0 use head/simple/ordered queue tag sequences;
286 * tm:1 use only simple queue tags;
287 * tm:2 use only head of queue tags;
288 * tm:3 use only ordered queue tags;
289 * mq:xx set the max queue depth to the value xx (2 <= xx <= 32).
291 * The default value is: "eata=lc:n,tc:n,mq:16,tm:0,et:n,rs:n".
292 * An example using the list of detection probes could be:
293 * "eata=0x7410,0x230,lc:y,tc:n,mq:4,eh:n,et:n".
295 * When loading as a module, parameters can be specified as well.
296 * The above example would be (use 1 in place of y and 0 in place of n):
298 * modprobe eata io_port=0x7410,0x230 linked_comm=1 tagged_comm=0 \
299 * max_queue_depth=4 tag_mode=0 use_new_eh_code=0 \
300 * ext_tran=0 rev_scan=1
302 * ----------------------------------------------------------------------------
303 * In this implementation, linked commands are designed to work with any DISK
304 * or CD-ROM, since this linking has only the intent of clustering (time-wise)
305 * and reordering by elevator sorting commands directed to each device,
306 * without any relation with the actual SCSI protocol between the controller
307 * and the device.
308 * If Q is the queue depth reported at boot time for each device (also named
309 * cmds/lun) and Q > 2, whenever there is already an active command to the
310 * device all other commands to the same device (up to Q-1) are kept waiting
311 * in the elevator sorting queue. When the active command completes, the
312 * commands in this queue are sorted by sector address. The sort is chosen
313 * between increasing or decreasing by minimizing the seek distance between
314 * the sector of the commands just completed and the sector of the first
315 * command in the list to be sorted.
316 * Trivial math assures that the unsorted average seek distance when doing
317 * random seeks over S sectors is S/3.
318 * When (Q-1) requests are uniformly distributed over S sectors, the average
319 * distance between two adjacent requests is S/((Q-1) + 1), so the sorted
320 * average seek distance for (Q-1) random requests over S sectors is S/Q.
321 * The elevator sorting hence divides the seek distance by a factor Q/3.
322 * The above pure geometric remarks are valid in all cases and the
323 * driver effectively reduces the seek distance by the predicted factor
324 * when there are Q concurrent read i/o operations on the device, but this
325 * does not necessarily results in a noticeable performance improvement:
326 * your mileage may vary....
328 * Note: command reordering inside a batch of queued commands could cause
329 * wrong results only if there is at least one write request and the
330 * intersection (sector-wise) of all requests is not empty.
331 * When the driver detects a batch including overlapping requests
332 * (a really rare event) strict serial (pid) order is enforced.
333 * ----------------------------------------------------------------------------
334 * The extended translation option (et:y) is useful when using large physical
335 * disks/arrays. It could also be useful when switching between Adaptec boards
336 * and DPT boards without reformatting the disk.
337 * When a boot disk is partitioned with extended translation, in order to
338 * be able to boot it with a DPT board is could be necessary to add to
339 * lilo.conf additional commands as in the following example:
341 * fix-table
342 * disk=/dev/sda bios=0x80 sectors=63 heads=128 cylindres=546
344 * where the above geometry should be replaced with the one reported at
345 * power up by the DPT controller.
346 * ----------------------------------------------------------------------------
348 * The boards are named EATA0, EATA1,... according to the detection order.
350 * In order to support multiple ISA boards in a reliable way,
351 * the driver sets host->wish_block = TRUE for all ISA boards.
354 #include <linux/version.h>
356 #define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
357 #define MAX_INT_PARAM 10
359 #if defined(MODULE)
360 #include <linux/module.h>
362 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,26)
363 MODULE_PARM(io_port, "1-" __MODULE_STRING(MAX_INT_PARAM) "i");
364 MODULE_PARM(linked_comm, "i");
365 MODULE_PARM(tagged_comm, "i");
366 MODULE_PARM(link_statistics, "i");
367 MODULE_PARM(max_queue_depth, "i");
368 MODULE_PARM(tag_mode, "i");
369 MODULE_PARM(use_new_eh_code, "i");
370 MODULE_PARM(ext_tran, "i");
371 MODULE_PARM(rev_scan, "i");
372 MODULE_AUTHOR("Dario Ballabio");
373 #endif
375 #endif
377 #include <linux/string.h>
378 #include <linux/sched.h>
379 #include <linux/kernel.h>
380 #include <linux/ioport.h>
381 #include <linux/delay.h>
382 #include <asm/io.h>
383 #include <asm/system.h>
384 #include <asm/byteorder.h>
385 #include <linux/proc_fs.h>
386 #include <linux/blk.h>
387 #include "scsi.h"
388 #include "hosts.h"
389 #include "sd.h"
390 #include <asm/dma.h>
391 #include <asm/irq.h>
392 #include "eata.h"
393 #include <linux/stat.h>
394 #include <linux/config.h>
395 #include <linux/pci.h>
397 #if LINUX_VERSION_CODE < LinuxVersionCode(2,1,93)
398 #include <linux/bios32.h>
399 #endif
401 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,36)
402 #include <linux/init.h>
403 #else
404 #define __initfunc(A) A
405 #define __initdata
406 #define __init
407 #endif
409 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,101)
410 #include <linux/spinlock.h>
411 #define IRQ_FLAGS
412 #define IRQ_LOCK
413 #define IRQ_LOCK_SAVE
414 #define IRQ_UNLOCK
415 #define IRQ_UNLOCK_RESTORE
416 #define SPIN_FLAGS unsigned long spin_flags;
417 #define SPIN_LOCK spin_lock_irq(&io_request_lock);
418 #define SPIN_LOCK_SAVE spin_lock_irqsave(&io_request_lock, spin_flags);
419 #define SPIN_UNLOCK spin_unlock_irq(&io_request_lock);
420 #define SPIN_UNLOCK_RESTORE \
421 spin_unlock_irqrestore(&io_request_lock, spin_flags);
422 static int use_new_eh_code = TRUE;
423 #else
424 #define IRQ_FLAGS unsigned long irq_flags;
425 #define IRQ_LOCK cli();
426 #define IRQ_LOCK_SAVE do {save_flags(irq_flags); cli();} while (0);
427 #define IRQ_UNLOCK sti();
428 #define IRQ_UNLOCK_RESTORE do {restore_flags(irq_flags);} while (0);
429 #define SPIN_FLAGS
430 #define SPIN_LOCK
431 #define SPIN_LOCK_SAVE
432 #define SPIN_UNLOCK
433 #define SPIN_UNLOCK_RESTORE
434 static int use_new_eh_code = FALSE;
435 #endif
437 struct proc_dir_entry proc_scsi_eata2x = {
438 PROC_SCSI_EATA2X, 6, "eata2x",
439 S_IFDIR | S_IRUGO | S_IXUGO, 2
442 /* Subversion values */
443 #define ISA 0
444 #define ESA 1
446 #undef FORCE_CONFIG
448 #undef DEBUG_LINKED_COMMANDS
449 #undef DEBUG_DETECT
450 #undef DEBUG_PCI_DETECT
451 #undef DEBUG_INTERRUPT
452 #undef DEBUG_RESET
453 #undef DEBUG_GENERATE_ERRORS
454 #undef DEBUG_GENERATE_ABORTS
455 #undef DEBUG_GEOMETRY
457 #define MAX_ISA 4
458 #define MAX_VESA 0
459 #define MAX_EISA 15
460 #define MAX_PCI 16
461 #define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
462 #define MAX_CHANNEL 4
463 #define MAX_LUN 32
464 #define MAX_TARGET 32
465 #define MAX_MAILBOXES 64
466 #define MAX_SGLIST 64
467 #define MAX_LARGE_SGLIST 122
468 #define MAX_INTERNAL_RETRIES 64
469 #define MAX_CMD_PER_LUN 2
470 #define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
472 #define SKIP ULONG_MAX
473 #define FALSE 0
474 #define TRUE 1
475 #define FREE 0
476 #define IN_USE 1
477 #define LOCKED 2
478 #define IN_RESET 3
479 #define IGNORE 4
480 #define READY 5
481 #define ABORTING 6
482 #define NO_DMA 0xff
483 #define MAXLOOP 10000
484 #define TAG_MIXED 0
485 #define TAG_SIMPLE 1
486 #define TAG_HEAD 2
487 #define TAG_ORDERED 3
489 #define REG_CMD 7
490 #define REG_STATUS 7
491 #define REG_AUX_STATUS 8
492 #define REG_DATA 0
493 #define REG_DATA2 1
494 #define REG_SEE 6
495 #define REG_LOW 2
496 #define REG_LM 3
497 #define REG_MID 4
498 #define REG_MSB 5
499 #define REGION_SIZE 9
500 #define MAX_ISA_ADDR 0x03ff
501 #define MIN_EISA_ADDR 0x1c88
502 #define MAX_EISA_ADDR 0xfc88
503 #define BSY_ASSERTED 0x80
504 #define DRQ_ASSERTED 0x08
505 #define ABSY_ASSERTED 0x01
506 #define IRQ_ASSERTED 0x02
507 #define READ_CONFIG_PIO 0xf0
508 #define SET_CONFIG_PIO 0xf1
509 #define SEND_CP_PIO 0xf2
510 #define RECEIVE_SP_PIO 0xf3
511 #define TRUNCATE_XFR_PIO 0xf4
512 #define RESET_PIO 0xf9
513 #define READ_CONFIG_DMA 0xfd
514 #define SET_CONFIG_DMA 0xfe
515 #define SEND_CP_DMA 0xff
516 #define ASOK 0x00
517 #define ASST 0x01
519 #define YESNO(a) ((a) ? 'y' : 'n')
520 #define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
522 /* "EATA", in Big Endian format */
523 #define EATA_SIGNATURE 0x41544145
525 /* Number of valid bytes in the board config structure for EATA 2.0x */
526 #define EATA_2_0A_SIZE 28
527 #define EATA_2_0B_SIZE 30
528 #define EATA_2_0C_SIZE 34
530 /* Board info structure */
531 struct eata_info {
532 ulong data_len; /* Number of valid bytes after this field */
533 ulong sign; /* ASCII "EATA" signature */
534 unchar :4, /* unused low nibble */
535 version:4; /* EATA version, should be 0x1 */
536 unchar ocsena:1, /* Overlap Command Support Enabled */
537 tarsup:1, /* Target Mode Supported */
538 trnxfr:1, /* Truncate Transfer Cmd NOT Necessary */
539 morsup:1, /* More Supported */
540 dmasup:1, /* DMA Supported */
541 drqvld:1, /* DRQ Index (DRQX) is valid */
542 ata:1, /* This is an ATA device */
543 haaval:1; /* Host Adapter Address Valid */
544 ushort cp_pad_len; /* Number of pad bytes after cp_len */
545 unchar host_addr[4]; /* Host Adapter SCSI ID for channels 3, 2, 1, 0 */
546 ulong cp_len; /* Number of valid bytes in cp */
547 ulong sp_len; /* Number of valid bytes in sp */
548 ushort queue_size; /* Max number of cp that can be queued */
549 ushort unused;
550 ushort scatt_size; /* Max number of entries in scatter/gather table */
551 unchar irq:4, /* Interrupt Request assigned to this controller */
552 irq_tr:1, /* 0 for edge triggered, 1 for level triggered */
553 second:1, /* 1 if this is a secondary (not primary) controller */
554 drqx:2; /* DRQ Index (0=DMA0, 1=DMA7, 2=DMA6, 3=DMA5) */
555 unchar sync; /* 1 if scsi target id 7...0 is running sync scsi */
557 /* Structure extension defined in EATA 2.0B */
558 unchar isaena:1, /* ISA i/o addressing is disabled/enabled */
559 forcaddr:1, /* Port address has been forced */
560 large_sg:1, /* 1 if large SG lists are supported */
561 res1:1,
563 unchar max_id:5, /* Max SCSI target ID number */
564 max_chan:3; /* Max SCSI channel number on this board */
566 /* Structure extension defined in EATA 2.0C */
567 unchar max_lun; /* Max SCSI LUN number */
568 unchar :4,
569 m1:1, /* This is a PCI with an M1 chip installed */
570 idquest:1, /* RAIDNUM returned is questionable */
571 pci:1, /* This board is PCI */
572 eisa:1; /* This board is EISA */
573 unchar raidnum; /* Uniquely identifies this HBA in a system */
574 unchar notused;
576 ushort ipad[247];
579 /* Board config structure */
580 struct eata_config {
581 ushort len; /* Number of bytes following this field */
582 unchar edis:1, /* Disable EATA interface after config command */
583 ocena:1, /* Overlapped Commands Enabled */
584 mdpena:1, /* Transfer all Modified Data Pointer Messages */
585 tarena:1, /* Target Mode Enabled for this controller */
587 unchar cpad[511];
590 /* Returned status packet structure */
591 struct mssp {
592 unchar adapter_status:7, /* State related to current command */
593 eoc:1; /* End Of Command (1 = command completed) */
594 unchar target_status; /* SCSI status received after data transfer */
595 unchar unused[2];
596 ulong inv_res_len; /* Number of bytes not transferred */
597 struct mscp *cpp; /* Address set in cp */
598 char mess[12];
601 struct sg_list {
602 unsigned int address; /* Segment Address */
603 unsigned int num_bytes; /* Segment Length */
606 /* MailBox SCSI Command Packet */
607 struct mscp {
608 unchar sreset:1, /* SCSI Bus Reset Signal should be asserted */
609 init:1, /* Re-initialize controller and self test */
610 reqsen:1, /* Transfer Request Sense Data to addr using DMA */
611 sg:1, /* Use Scatter/Gather */
613 interp:1, /* The controller interprets cp, not the target */
614 dout:1, /* Direction of Transfer is Out (Host to Target) */
615 din:1; /* Direction of Transfer is In (Target to Host) */
616 unchar sense_len; /* Request Sense Length */
617 unchar unused[3];
618 unchar fwnest:1, /* Send command to a component of an Array Group */
620 unchar phsunit:1, /* Send to Target Physical Unit (bypass RAID) */
621 iat:1, /* Inhibit Address Translation */
622 hbaci:1, /* Inhibit HBA Caching for this command */
624 unchar target:5, /* SCSI target ID */
625 channel:3; /* SCSI channel number */
626 unchar lun:5, /* SCSI logical unit number */
627 luntar:1, /* This cp is for Target (not LUN) */
628 dispri:1, /* Disconnect Privilege granted */
629 one:1; /* 1 */
630 unchar mess[3]; /* Massage to/from Target */
631 unchar cdb[12]; /* Command Descriptor Block */
632 ulong data_len; /* If sg=0 Data Length, if sg=1 sglist length */
633 struct mscp *cpp; /* Address to be returned in sp */
634 ulong data_address; /* If sg=0 Data Address, if sg=1 sglist address */
635 ulong sp_addr; /* Address where sp is DMA'ed when cp completes */
636 ulong sense_addr; /* Address where Sense Data is DMA'ed on error */
637 Scsi_Cmnd *SCpnt;
638 unsigned int index; /* cp index */
639 struct sg_list *sglist;
642 struct hostdata {
643 struct mscp cp[MAX_MAILBOXES]; /* Mailboxes for this board */
644 unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
645 unsigned int last_cp_used; /* Index of last mailbox used */
646 unsigned int iocount; /* Total i/o done for this board */
647 int board_number; /* Number of this board */
648 char board_name[16]; /* Name of this board */
649 char board_id[256]; /* data from INQUIRY on this board */
650 int in_reset; /* True if board is doing a reset */
651 int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
652 int target_redo[MAX_TARGET][MAX_CHANNEL]; /* If TRUE redo i/o on target */
653 unsigned int retries; /* Number of internal retries */
654 unsigned long last_retried_pid; /* Pid of last retried command */
655 unsigned char subversion; /* Bus type, either ISA or EISA/PCI */
656 unsigned char protocol_rev; /* EATA 2.0 rev., 'A' or 'B' or 'C' */
657 struct mssp sp[2]; /* Returned status for this board */
660 static struct Scsi_Host *sh[MAX_BOARDS + 1];
661 static const char *driver_name = "EATA";
662 static char sha[MAX_BOARDS];
664 /* Initialize num_boards so that ihdlr can work while detect is in progress */
665 static unsigned int num_boards = MAX_BOARDS;
667 static unsigned long io_port[] __initdata = {
669 /* Space for MAX_INT_PARAM ports usable while loading as a module */
670 SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
671 SKIP, SKIP,
673 /* First ISA */
674 0x1f0,
676 /* Space for MAX_PCI ports possibly reported by PCI_BIOS */
677 SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
678 SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
680 /* MAX_EISA ports */
681 0x1c88, 0x2c88, 0x3c88, 0x4c88, 0x5c88, 0x6c88, 0x7c88, 0x8c88,
682 0x9c88, 0xac88, 0xbc88, 0xcc88, 0xdc88, 0xec88, 0xfc88,
684 /* Other (MAX_ISA - 1) ports */
685 0x170, 0x230, 0x330,
687 /* End of list */
691 #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
692 #define BN(board) (HD(board)->board_name)
694 #define H2DEV(x) htonl(x)
695 #define DEV2H(x) H2DEV(x)
696 #define V2DEV(addr) ((addr) ? H2DEV(virt_to_bus((void *)addr)) : 0)
697 #define DEV2V(addr) ((addr) ? DEV2H(bus_to_virt((unsigned long)addr)) : 0)
699 static void do_interrupt_handler(int, void *, struct pt_regs *);
700 static void flush_dev(Scsi_Device *, unsigned long, unsigned int, unsigned int);
701 static int do_trace = FALSE;
702 static int setup_done = FALSE;
703 static int link_statistics = 0;
704 static int tag_mode = TAG_MIXED;
705 static int ext_tran = FALSE;
706 static int rev_scan = TRUE;
708 #if defined(CONFIG_SCSI_EATA_TAGGED_QUEUE)
709 static int tagged_comm = TRUE;
710 #else
711 static int tagged_comm = FALSE;
712 #endif
714 #if defined(CONFIG_SCSI_EATA_LINKED_COMMANDS)
715 static int linked_comm = TRUE;
716 #else
717 static int linked_comm = FALSE;
718 #endif
720 #if defined(CONFIG_SCSI_EATA_MAX_TAGS)
721 static int max_queue_depth = CONFIG_SCSI_EATA_MAX_TAGS;
722 #else
723 static int max_queue_depth = MAX_CMD_PER_LUN;
724 #endif
726 static void select_queue_depths(struct Scsi_Host *host, Scsi_Device *devlist) {
727 Scsi_Device *dev;
728 int j, ntag = 0, nuntag = 0, tqd, utqd;
729 IRQ_FLAGS
731 IRQ_LOCK_SAVE
732 j = ((struct hostdata *) host->hostdata)->board_number;
734 for(dev = devlist; dev; dev = dev->next) {
736 if (dev->host != host) continue;
738 if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
739 ntag++;
740 else
741 nuntag++;
744 utqd = MAX_CMD_PER_LUN;
746 tqd = (host->can_queue - utqd * nuntag) / (ntag ? ntag : 1);
748 if (tqd > max_queue_depth) tqd = max_queue_depth;
750 if (tqd < MAX_CMD_PER_LUN) tqd = MAX_CMD_PER_LUN;
752 for(dev = devlist; dev; dev = dev->next) {
753 char *tag_suffix = "", *link_suffix = "";
755 if (dev->host != host) continue;
757 if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
758 dev->queue_depth = tqd;
759 else
760 dev->queue_depth = utqd;
762 if (TLDEV(dev->type)) {
763 if (linked_comm && dev->queue_depth > 2)
764 link_suffix = ", sorted";
765 else
766 link_suffix = ", unsorted";
769 if (tagged_comm && dev->tagged_supported && TLDEV(dev->type)) {
770 dev->tagged_queue = 1;
771 dev->current_tag = 1;
774 if (dev->tagged_supported && TLDEV(dev->type) && dev->tagged_queue)
775 tag_suffix = ", tagged";
776 else if (dev->tagged_supported && TLDEV(dev->type))
777 tag_suffix = ", untagged";
779 printk("%s: scsi%d, channel %d, id %d, lun %d, cmds/lun %d%s%s.\n",
780 BN(j), host->host_no, dev->channel, dev->id, dev->lun,
781 dev->queue_depth, link_suffix, tag_suffix);
784 IRQ_UNLOCK_RESTORE
785 return;
788 static inline int wait_on_busy(unsigned long iobase, unsigned int loop) {
790 while (inb(iobase + REG_AUX_STATUS) & ABSY_ASSERTED) {
791 udelay(1L);
792 if (--loop == 0) return TRUE;
795 return FALSE;
798 static inline int do_dma(unsigned long iobase, unsigned int addr, unchar cmd) {
800 if (wait_on_busy(iobase, (addr ? MAXLOOP * 100 : MAXLOOP))) return TRUE;
802 if ((addr = V2DEV(addr))) {
803 outb((char) (addr >> 24), iobase + REG_LOW);
804 outb((char) (addr >> 16), iobase + REG_LM);
805 outb((char) (addr >> 8), iobase + REG_MID);
806 outb((char) addr, iobase + REG_MSB);
809 outb(cmd, iobase + REG_CMD);
810 return FALSE;
813 static inline int read_pio(unsigned long iobase, ushort *start, ushort *end) {
814 unsigned int loop = MAXLOOP;
815 ushort *p;
817 for (p = start; p <= end; p++) {
819 while (!(inb(iobase + REG_STATUS) & DRQ_ASSERTED)) {
820 udelay(1L);
821 if (--loop == 0) return TRUE;
824 loop = MAXLOOP;
825 *p = inw(iobase);
828 return FALSE;
831 static inline int __init
832 get_pci_irq(unsigned long port_base, unsigned char *apic_irq){
834 #if defined(CONFIG_PCI)
836 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
838 unsigned int addr;
839 struct pci_dev *dev = NULL;
841 if (!pci_present()) return FALSE;
843 while((dev = pci_find_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
845 if (pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &addr)) continue;
847 #if defined(DEBUG_PCI_DETECT)
848 printk("%s: get_pci_irq, bus %d, devfn 0x%x, addr 0x%x, apic_irq %u.\n",
849 driver_name, dev->bus->number, dev->devfn, addr, dev->irq);
850 #endif
852 if ((addr & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_IO)
853 continue;
855 if ((addr & PCI_BASE_ADDRESS_IO_MASK) + PCI_BASE_ADDRESS_0 == port_base) {
856 *apic_irq = dev->irq;
857 return TRUE;
862 #endif /* end new style PCI code */
864 #endif /* end CONFIG_PCI */
866 return FALSE;
869 static inline int __init port_detect \
870 (unsigned long port_base, unsigned int j, Scsi_Host_Template *tpnt){
871 unsigned char irq, dma_channel, subversion, i;
872 unsigned char protocol_rev, apic_irq;
873 struct eata_info info;
874 char *bus_type, dma_name[16], tag_type;
876 /* Allowed DMA channels for ISA (0 indicates reserved) */
877 unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
879 char name[16];
881 sprintf(name, "%s%d", driver_name, j);
883 if(check_region(port_base, REGION_SIZE)) {
884 printk("%s: address 0x%03lx in use, skipping probe.\n", name, port_base);
885 return FALSE;
888 if (do_dma(port_base, 0, READ_CONFIG_PIO)) return FALSE;
890 /* Read the info structure */
891 if (read_pio(port_base, (ushort *)&info, (ushort *)&info.ipad[0]))
892 return FALSE;
894 /* Check the controller "EATA" signature */
895 if (info.sign != EATA_SIGNATURE) return FALSE;
897 if (DEV2H(info.data_len) < EATA_2_0A_SIZE) {
898 printk("%s: config structure size (%ld bytes) too short, detaching.\n",
899 name, DEV2H(info.data_len));
900 return FALSE;
902 else if (DEV2H(info.data_len) == EATA_2_0A_SIZE)
903 protocol_rev = 'A';
904 else if (DEV2H(info.data_len) == EATA_2_0B_SIZE)
905 protocol_rev = 'B';
906 else
907 protocol_rev = 'C';
909 if (!setup_done && j > 0 && j <= MAX_PCI) {
910 bus_type = "PCI";
911 subversion = ESA;
913 else if (port_base > MAX_EISA_ADDR || (protocol_rev == 'C' && info.pci)) {
914 bus_type = "PCI";
915 subversion = ESA;
917 else if (port_base >= MIN_EISA_ADDR || (protocol_rev == 'C' && info.eisa)) {
918 bus_type = "EISA";
919 subversion = ESA;
921 else if (protocol_rev == 'C' && !info.eisa && !info.pci) {
922 bus_type = "ISA";
923 subversion = ISA;
925 else if (port_base > MAX_ISA_ADDR) {
926 bus_type = "PCI";
927 subversion = ESA;
929 else {
930 bus_type = "ISA";
931 subversion = ISA;
934 if (!info.haaval || info.ata) {
935 printk("%s: address 0x%03lx, unusable %s board (%d%d), detaching.\n",
936 name, port_base, bus_type, info.haaval, info.ata);
937 return FALSE;
940 if (info.drqvld) {
942 if (subversion == ESA)
943 printk("%s: warning, weird %s board using DMA.\n", name, bus_type);
945 subversion = ISA;
946 dma_channel = dma_channel_table[3 - info.drqx];
948 else {
950 if (subversion == ISA)
951 printk("%s: warning, weird %s board not using DMA.\n", name, bus_type);
953 subversion = ESA;
954 dma_channel = NO_DMA;
957 if (!info.dmasup)
958 printk("%s: warning, DMA protocol support not asserted.\n", name);
960 irq = info.irq;
962 if (subversion == ESA && !info.irq_tr)
963 printk("%s: warning, LEVEL triggering is suggested for IRQ %u.\n",
964 name, irq);
966 if (get_pci_irq(port_base, &apic_irq) && (irq != apic_irq)) {
967 printk("%s: IRQ %u mapped to IO-APIC IRQ %u.\n", name, irq, apic_irq);
968 irq = apic_irq;
971 /* Board detected, allocate its IRQ */
972 if (request_irq(irq, do_interrupt_handler,
973 SA_INTERRUPT | ((subversion == ESA) ? SA_SHIRQ : 0),
974 driver_name, (void *) &sha[j])) {
975 printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
976 return FALSE;
979 if (subversion == ISA && request_dma(dma_channel, driver_name)) {
980 printk("%s: unable to allocate DMA channel %u, detaching.\n",
981 name, dma_channel);
982 free_irq(irq, &sha[j]);
983 return FALSE;
986 #if defined(FORCE_CONFIG)
988 struct eata_config config;
990 /* Set board configuration */
991 memset((char *)&config, 0, sizeof(struct eata_config));
992 config.len = (ushort) htons((ushort)510);
993 config.ocena = TRUE;
995 if (do_dma(port_base, (unsigned int)&config, SET_CONFIG_DMA)) {
996 printk("%s: busy timeout sending configuration, detaching.\n", name);
997 return FALSE;
1000 #endif
1002 sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
1004 if (sh[j] == NULL) {
1005 printk("%s: unable to register host, detaching.\n", name);
1007 free_irq(irq, &sha[j]);
1009 if (subversion == ISA) free_dma(dma_channel);
1011 return FALSE;
1014 sh[j]->io_port = port_base;
1015 sh[j]->unique_id = port_base;
1016 sh[j]->n_io_port = REGION_SIZE;
1017 sh[j]->dma_channel = dma_channel;
1018 sh[j]->irq = irq;
1019 sh[j]->sg_tablesize = (ushort) ntohs(info.scatt_size);
1020 sh[j]->this_id = (ushort) info.host_addr[3];
1021 sh[j]->can_queue = (ushort) ntohs(info.queue_size);
1022 sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
1023 sh[j]->select_queue_depths = select_queue_depths;
1025 /* Register the I/O space that we use */
1026 request_region(sh[j]->io_port, sh[j]->n_io_port, driver_name);
1028 memset(HD(j), 0, sizeof(struct hostdata));
1029 HD(j)->subversion = subversion;
1030 HD(j)->protocol_rev = protocol_rev;
1031 HD(j)->board_number = j;
1033 if (HD(j)->subversion == ESA)
1034 sh[j]->unchecked_isa_dma = FALSE;
1035 else {
1036 unsigned long flags;
1037 sh[j]->wish_block = TRUE;
1038 sh[j]->unchecked_isa_dma = TRUE;
1040 flags=claim_dma_lock();
1041 disable_dma(dma_channel);
1042 clear_dma_ff(dma_channel);
1043 set_dma_mode(dma_channel, DMA_MODE_CASCADE);
1044 enable_dma(dma_channel);
1045 release_dma_lock(flags);
1049 strcpy(BN(j), name);
1051 /* DPT PM2012 does not allow to detect sg_tablesize correctly */
1052 if (sh[j]->sg_tablesize > MAX_SGLIST || sh[j]->sg_tablesize < 2) {
1053 printk("%s: detect, wrong n. of SG lists %d, fixed.\n",
1054 BN(j), sh[j]->sg_tablesize);
1055 sh[j]->sg_tablesize = MAX_SGLIST;
1058 /* DPT PM2012 does not allow to detect can_queue correctly */
1059 if (sh[j]->can_queue > MAX_MAILBOXES || sh[j]->can_queue < 2) {
1060 printk("%s: detect, wrong n. of mbox %d, fixed.\n",
1061 BN(j), sh[j]->can_queue);
1062 sh[j]->can_queue = MAX_MAILBOXES;
1065 if (protocol_rev != 'A') {
1067 if (info.max_chan > 0 && info.max_chan < MAX_CHANNEL)
1068 sh[j]->max_channel = info.max_chan;
1070 if (info.max_id > 7 && info.max_id < MAX_TARGET)
1071 sh[j]->max_id = info.max_id + 1;
1073 if (info.large_sg && sh[j]->sg_tablesize == MAX_SGLIST)
1074 sh[j]->sg_tablesize = MAX_LARGE_SGLIST;
1077 if (protocol_rev == 'C') {
1079 if (info.max_lun > 7 && info.max_lun < MAX_LUN)
1080 sh[j]->max_lun = info.max_lun + 1;
1083 if (dma_channel == NO_DMA) sprintf(dma_name, "%s", "BMST");
1084 else sprintf(dma_name, "DMA %u", dma_channel);
1086 for (i = 0; i < sh[j]->can_queue; i++)
1087 if (! ((&HD(j)->cp[i])->sglist = kmalloc(
1088 sh[j]->sg_tablesize * sizeof(struct sg_list),
1089 (sh[j]->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC))) {
1090 printk("%s: kmalloc SGlist failed, mbox %d, detaching.\n", BN(j), i);
1091 eata2x_release(sh[j]);
1092 return FALSE;
1095 if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
1096 max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
1098 if (max_queue_depth < MAX_CMD_PER_LUN) max_queue_depth = MAX_CMD_PER_LUN;
1100 if (tagged_comm) {
1101 if (tag_mode == TAG_SIMPLE) tag_type = '1';
1102 else if (tag_mode == TAG_HEAD) tag_type = '2';
1103 else if (tag_mode == TAG_ORDERED) tag_type = '3';
1104 else tag_type = 'y';
1106 else tag_type = 'n';
1108 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,101)
1109 sh[j]->hostt->use_new_eh_code = use_new_eh_code;
1110 #else
1111 use_new_eh_code = FALSE;
1112 #endif
1114 if (j == 0) {
1115 printk("EATA/DMA 2.0x: Copyright (C) 1994-1998 Dario Ballabio.\n");
1116 printk("%s config options -> tc:%c, lc:%c, mq:%d, eh:%c, rs:%c, et:%c.\n",
1117 driver_name, tag_type, YESNO(linked_comm), max_queue_depth,
1118 YESNO(use_new_eh_code), YESNO(rev_scan), YESNO(ext_tran));
1121 printk("%s: 2.0%c, %s 0x%03lx, IRQ %u, %s, SG %d, MB %d.\n",
1122 BN(j), HD(j)->protocol_rev, bus_type, (unsigned long)sh[j]->io_port,
1123 sh[j]->irq, dma_name, sh[j]->sg_tablesize, sh[j]->can_queue);
1125 if (sh[j]->max_id > 8 || sh[j]->max_lun > 8)
1126 printk("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
1127 BN(j), sh[j]->max_id, sh[j]->max_lun);
1129 for (i = 0; i <= sh[j]->max_channel; i++)
1130 printk("%s: SCSI channel %u enabled, host target ID %d.\n",
1131 BN(j), i, info.host_addr[3 - i]);
1133 #if defined(DEBUG_DETECT)
1134 printk("%s: Vers. 0x%x, ocs %u, tar %u, trnxfr %u, more %u, SYNC 0x%x, "\
1135 "sec. %u, infol %ld, cpl %ld spl %ld.\n", name, info.version,
1136 info.ocsena, info.tarsup, info.trnxfr, info.morsup, info.sync,
1137 info.second, DEV2H(info.data_len), DEV2H(info.cp_len),
1138 DEV2H(info.sp_len));
1140 if (protocol_rev == 'B' || protocol_rev == 'C')
1141 printk("%s: isaena %u, forcaddr %u, max_id %u, max_chan %u, "\
1142 "large_sg %u, res1 %u.\n", name, info.isaena, info.forcaddr,
1143 info.max_id, info.max_chan, info.large_sg, info.res1);
1145 if (protocol_rev == 'C')
1146 printk("%s: max_lun %u, m1 %u, idquest %u, pci %u, eisa %u, "\
1147 "raidnum %u.\n", name, info.max_lun, info.m1, info.idquest,
1148 info.pci, info.eisa, info.raidnum);
1149 #endif
1151 return TRUE;
1154 void __init eata2x_setup(char *str, int *ints){
1155 int i, argc = ints[0];
1156 char *cur = str, *pc;
1158 if (argc > 0) {
1160 if (argc > MAX_INT_PARAM) argc = MAX_INT_PARAM;
1162 for (i = 0; i < argc; i++) io_port[i] = ints[i + 1];
1164 io_port[i] = 0;
1165 setup_done = TRUE;
1168 while (cur && (pc = strchr(cur, ':'))) {
1169 int val = 0, c = *++pc;
1171 if (c == 'n' || c == 'N') val = FALSE;
1172 else if (c == 'y' || c == 'Y') val = TRUE;
1173 else val = (int) simple_strtoul(pc, NULL, 0);
1175 if (!strncmp(cur, "lc:", 3)) linked_comm = val;
1176 else if (!strncmp(cur, "tc:", 3)) tagged_comm = val;
1177 else if (!strncmp(cur, "tm:", 3)) tag_mode = val;
1178 else if (!strncmp(cur, "mq:", 3)) max_queue_depth = val;
1179 else if (!strncmp(cur, "ls:", 3)) link_statistics = val;
1180 else if (!strncmp(cur, "eh:", 3)) use_new_eh_code = val;
1181 else if (!strncmp(cur, "et:", 3)) ext_tran = val;
1182 else if (!strncmp(cur, "rs:", 3)) rev_scan = val;
1184 if ((cur = strchr(cur, ','))) ++cur;
1187 return;
1190 static void __init add_pci_ports(void){
1192 #if defined(CONFIG_PCI)
1194 unsigned int addr, k;
1196 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,93)
1198 struct pci_dev *dev = NULL;
1200 if (!pci_present()) return;
1202 for (k = 0; k < MAX_PCI; k++) {
1204 if (!(dev = pci_find_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) break;
1206 if (pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &addr)) continue;
1208 #if defined(DEBUG_PCI_DETECT)
1209 printk("%s: detect, seq. %d, bus %d, devfn 0x%x, addr 0x%x.\n",
1210 driver_name, k, dev->bus->number, dev->devfn, addr);
1211 #endif
1213 if ((addr & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_IO)
1214 continue;
1216 /* Order addresses according to rev_scan value */
1217 io_port[MAX_INT_PARAM + (rev_scan ? (MAX_PCI - k) : (1 + k))] =
1218 (addr & PCI_BASE_ADDRESS_IO_MASK) + PCI_BASE_ADDRESS_0;
1221 #else /* else old style PCI code */
1223 unsigned short i = 0;
1224 unsigned char bus, devfn;
1226 if (!pcibios_present()) return;
1228 for (k = 0; k < MAX_PCI; k++) {
1230 if (pcibios_find_class(PCI_CLASS_STORAGE_SCSI << 8, i++, &bus, &devfn)
1231 != PCIBIOS_SUCCESSFUL) break;
1233 if (pcibios_read_config_dword(bus, devfn, PCI_BASE_ADDRESS_0, &addr)
1234 != PCIBIOS_SUCCESSFUL) continue;
1236 #if defined(DEBUG_PCI_DETECT)
1237 printk("%s: detect, seq. %d, bus %d, devfn 0x%x, addr 0x%x.\n",
1238 driver_name, k, bus, devfn, addr);
1239 #endif
1241 if ((addr & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_IO)
1242 continue;
1244 /* Order addresses according to rev_scan value */
1245 io_port[MAX_INT_PARAM + (rev_scan ? (MAX_PCI - k) : (1 + k))] =
1246 (addr & PCI_BASE_ADDRESS_IO_MASK) + PCI_BASE_ADDRESS_0;
1249 #endif /* end old style PCI code */
1251 #endif /* end CONFIG_PCI */
1253 return;
1256 int __init eata2x_detect(Scsi_Host_Template *tpnt){
1257 unsigned int j = 0, k;
1258 IRQ_FLAGS
1260 IRQ_LOCK_SAVE
1261 tpnt->proc_dir = &proc_scsi_eata2x;
1263 #if defined(MODULE)
1264 /* io_port could have been modified when loading as a module */
1265 if(io_port[0] != SKIP) {
1266 setup_done = TRUE;
1267 io_port[MAX_INT_PARAM] = 0;
1269 #endif
1271 for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
1273 if (!setup_done) add_pci_ports();
1275 for (k = 0; io_port[k]; k++) {
1277 if (io_port[k] == SKIP) continue;
1279 if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt)) j++;
1282 num_boards = j;
1283 IRQ_UNLOCK_RESTORE
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 cpp->cpp = cpp;
1357 SCpnt->scsi_done = done;
1358 cpp->index = i;
1359 SCpnt->host_scribble = (unsigned char *) &cpp->index;
1361 if (do_trace) printk("%s: qcomm, mbox %d, target %d.%d:%d, pid %ld.\n",
1362 BN(j), i, SCpnt->channel, SCpnt->target,
1363 SCpnt->lun, SCpnt->pid);
1365 for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
1366 if (SCpnt->cmnd[0] == data_out_cmds[k]) {
1367 cpp->dout = TRUE;
1368 break;
1371 if ((cpp->din = !cpp->dout))
1372 for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
1373 if (SCpnt->cmnd[0] == data_none_cmds[k]) {
1374 cpp->din = FALSE;
1375 break;
1378 cpp->reqsen = TRUE;
1379 cpp->dispri = TRUE;
1380 #if 0
1381 if (SCpnt->device->type == TYPE_TAPE) cpp->hbaci = TRUE;
1382 #endif
1383 cpp->one = TRUE;
1384 cpp->channel = SCpnt->channel;
1385 cpp->target = SCpnt->target;
1386 cpp->lun = SCpnt->lun;
1387 cpp->SCpnt = SCpnt;
1388 cpp->sense_addr = V2DEV(SCpnt->sense_buffer);
1389 cpp->sense_len = sizeof SCpnt->sense_buffer;
1391 if (SCpnt->device->tagged_queue) {
1393 if (HD(j)->target_redo[SCpnt->target][SCpnt->channel] ||
1394 HD(j)->target_to[SCpnt->target][SCpnt->channel])
1395 cpp->mess[0] = ORDERED_QUEUE_TAG;
1396 else if (tag_mode == TAG_SIMPLE) cpp->mess[0] = SIMPLE_QUEUE_TAG;
1397 else if (tag_mode == TAG_HEAD) cpp->mess[0] = HEAD_OF_QUEUE_TAG;
1398 else if (tag_mode == TAG_ORDERED) cpp->mess[0] = ORDERED_QUEUE_TAG;
1399 else if (SCpnt->device->current_tag == 0)
1400 cpp->mess[0] = ORDERED_QUEUE_TAG;
1401 else if (SCpnt->device->current_tag == 1)
1402 cpp->mess[0] = HEAD_OF_QUEUE_TAG;
1403 else
1404 cpp->mess[0] = SIMPLE_QUEUE_TAG;
1406 cpp->mess[1] = SCpnt->device->current_tag++;
1409 if (SCpnt->use_sg) {
1410 cpp->sg = TRUE;
1411 build_sg_list(cpp, SCpnt);
1413 else {
1414 cpp->data_address = V2DEV(SCpnt->request_buffer);
1415 cpp->data_len = H2DEV(SCpnt->request_bufflen);
1418 memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
1420 if (linked_comm && SCpnt->device->queue_depth > 2
1421 && TLDEV(SCpnt->device->type)) {
1422 HD(j)->cp_stat[i] = READY;
1423 flush_dev(SCpnt->device, SCpnt->request.sector, j, FALSE);
1424 return 0;
1427 /* Send control packet to the board */
1428 if (do_dma(sh[j]->io_port, (unsigned int) cpp, SEND_CP_DMA)) {
1429 SCpnt->host_scribble = NULL;
1430 printk("%s: qcomm, target %d.%d:%d, pid %ld, adapter busy.\n",
1431 BN(j), SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid);
1432 return 1;
1435 HD(j)->cp_stat[i] = IN_USE;
1436 return 0;
1439 int eata2x_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
1440 int rtn;
1441 IRQ_FLAGS
1443 IRQ_LOCK_SAVE
1444 rtn = do_qcomm(SCpnt, done);
1445 IRQ_UNLOCK_RESTORE
1446 return rtn;
1449 static inline int do_old_abort(Scsi_Cmnd *SCarg) {
1450 unsigned int i, j;
1452 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1454 if (SCarg->host_scribble == NULL ||
1455 (SCarg->serial_number_at_timeout &&
1456 (SCarg->serial_number != SCarg->serial_number_at_timeout))) {
1457 printk("%s: abort, target %d.%d:%d, pid %ld inactive.\n",
1458 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1459 return SCSI_ABORT_NOT_RUNNING;
1462 i = *(unsigned int *)SCarg->host_scribble;
1463 printk("%s: abort, mbox %d, target %d.%d:%d, pid %ld.\n",
1464 BN(j), i, SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1466 if (i >= sh[j]->can_queue)
1467 panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
1469 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1470 printk("%s: abort, timeout error.\n", BN(j));
1471 return SCSI_ABORT_ERROR;
1474 if (HD(j)->cp_stat[i] == FREE) {
1475 printk("%s: abort, mbox %d is free.\n", BN(j), i);
1476 return SCSI_ABORT_NOT_RUNNING;
1479 if (HD(j)->cp_stat[i] == IN_USE) {
1480 printk("%s: abort, mbox %d is in use.\n", BN(j), i);
1482 if (SCarg != HD(j)->cp[i].SCpnt)
1483 panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1484 BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
1486 if (inb(sh[j]->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)
1487 printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
1489 return SCSI_ABORT_SNOOZE;
1492 if (HD(j)->cp_stat[i] == IN_RESET) {
1493 printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
1494 return SCSI_ABORT_ERROR;
1497 if (HD(j)->cp_stat[i] == LOCKED) {
1498 printk("%s: abort, mbox %d is locked.\n", BN(j), i);
1499 return SCSI_ABORT_NOT_RUNNING;
1502 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1503 SCarg->result = DID_ABORT << 16;
1504 SCarg->host_scribble = NULL;
1505 HD(j)->cp_stat[i] = FREE;
1506 printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1507 BN(j), i, SCarg->pid);
1508 SCarg->scsi_done(SCarg);
1509 return SCSI_ABORT_SUCCESS;
1512 panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
1515 int eata2x_old_abort(Scsi_Cmnd *SCarg) {
1516 int rtn;
1517 IRQ_FLAGS
1519 IRQ_LOCK_SAVE
1520 rtn = do_old_abort(SCarg);
1521 IRQ_UNLOCK_RESTORE
1522 return rtn;
1525 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,101)
1527 static inline int do_abort(Scsi_Cmnd *SCarg) {
1528 unsigned int i, j;
1530 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1532 if (SCarg->host_scribble == NULL) {
1533 printk("%s: abort, target %d.%d:%d, pid %ld inactive.\n",
1534 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1535 return SUCCESS;
1538 i = *(unsigned int *)SCarg->host_scribble;
1539 printk("%s: abort, mbox %d, target %d.%d:%d, pid %ld.\n",
1540 BN(j), i, SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1542 if (i >= sh[j]->can_queue)
1543 panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
1545 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1546 printk("%s: abort, timeout error.\n", BN(j));
1547 return FAILED;
1550 if (HD(j)->cp_stat[i] == FREE) {
1551 printk("%s: abort, mbox %d is free.\n", BN(j), i);
1552 return SUCCESS;
1555 if (HD(j)->cp_stat[i] == IN_USE) {
1556 printk("%s: abort, mbox %d is in use.\n", BN(j), i);
1558 if (SCarg != HD(j)->cp[i].SCpnt)
1559 panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1560 BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
1562 if (inb(sh[j]->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)
1563 printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
1565 if (SCarg->eh_state == SCSI_STATE_TIMEOUT) {
1566 SCarg->host_scribble = NULL;
1567 HD(j)->cp_stat[i] = FREE;
1568 printk("%s, abort, mbox %d, eh_state timeout, pid %ld.\n",
1569 BN(j), i, SCarg->pid);
1570 return SUCCESS;
1573 return FAILED;
1576 if (HD(j)->cp_stat[i] == IN_RESET) {
1577 printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
1578 return FAILED;
1581 if (HD(j)->cp_stat[i] == LOCKED) {
1582 printk("%s: abort, mbox %d is locked.\n", BN(j), i);
1583 return SUCCESS;
1586 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1587 SCarg->result = DID_ABORT << 16;
1588 SCarg->host_scribble = NULL;
1589 HD(j)->cp_stat[i] = FREE;
1590 printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1591 BN(j), i, SCarg->pid);
1592 SCarg->scsi_done(SCarg);
1593 return SUCCESS;
1596 panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
1599 int eata2x_abort(Scsi_Cmnd *SCarg) {
1601 return do_abort(SCarg);
1604 #endif /* new_eh_code */
1606 static inline int do_old_reset(Scsi_Cmnd *SCarg) {
1607 unsigned int i, j, time, k, c, limit = 0;
1608 int arg_done = FALSE;
1609 Scsi_Cmnd *SCpnt;
1611 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1612 printk("%s: reset, enter, target %d.%d:%d, pid %ld.\n",
1613 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1615 if (SCarg->host_scribble == NULL)
1616 printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
1618 if (SCarg->serial_number_at_timeout &&
1619 (SCarg->serial_number != SCarg->serial_number_at_timeout)) {
1620 printk("%s: reset, pid %ld, reset not running.\n", BN(j), SCarg->pid);
1621 return SCSI_RESET_NOT_RUNNING;
1624 if (HD(j)->in_reset) {
1625 printk("%s: reset, exit, already in reset.\n", BN(j));
1626 return SCSI_RESET_ERROR;
1629 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1630 printk("%s: reset, exit, timeout error.\n", BN(j));
1631 return SCSI_RESET_ERROR;
1634 HD(j)->retries = 0;
1636 for (c = 0; c <= sh[j]->max_channel; c++)
1637 for (k = 0; k < sh[j]->max_id; k++) {
1638 HD(j)->target_redo[k][c] = TRUE;
1639 HD(j)->target_to[k][c] = 0;
1642 for (i = 0; i < sh[j]->can_queue; i++) {
1644 if (HD(j)->cp_stat[i] == FREE) continue;
1646 if (HD(j)->cp_stat[i] == LOCKED) {
1647 HD(j)->cp_stat[i] = FREE;
1648 printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
1649 continue;
1652 if (!(SCpnt = HD(j)->cp[i].SCpnt))
1653 panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
1655 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1656 HD(j)->cp_stat[i] = ABORTING;
1657 printk("%s: reset, mbox %d aborting, pid %ld.\n",
1658 BN(j), i, SCpnt->pid);
1661 else {
1662 HD(j)->cp_stat[i] = IN_RESET;
1663 printk("%s: reset, mbox %d in reset, pid %ld.\n",
1664 BN(j), i, SCpnt->pid);
1667 if (SCpnt->host_scribble == NULL)
1668 panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
1670 if (*(unsigned int *)SCpnt->host_scribble != i)
1671 panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
1673 if (SCpnt->scsi_done == NULL)
1674 panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
1676 if (SCpnt == SCarg) arg_done = TRUE;
1679 if (do_dma(sh[j]->io_port, 0, RESET_PIO)) {
1680 printk("%s: reset, cannot reset, timeout error.\n", BN(j));
1681 return SCSI_RESET_ERROR;
1684 printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
1686 #if defined(DEBUG_RESET)
1687 do_trace = TRUE;
1688 #endif
1690 HD(j)->in_reset = TRUE;
1691 SPIN_UNLOCK
1692 IRQ_UNLOCK
1693 time = jiffies;
1694 while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
1695 IRQ_LOCK
1696 SPIN_LOCK
1697 printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
1699 for (i = 0; i < sh[j]->can_queue; i++) {
1701 if (HD(j)->cp_stat[i] == IN_RESET) {
1702 SCpnt = HD(j)->cp[i].SCpnt;
1703 SCpnt->result = DID_RESET << 16;
1704 SCpnt->host_scribble = NULL;
1706 /* This mailbox is still waiting for its interrupt */
1707 HD(j)->cp_stat[i] = LOCKED;
1709 printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
1710 BN(j), i, SCpnt->pid);
1713 else if (HD(j)->cp_stat[i] == ABORTING) {
1714 SCpnt = HD(j)->cp[i].SCpnt;
1715 SCpnt->result = DID_RESET << 16;
1716 SCpnt->host_scribble = NULL;
1718 /* This mailbox was never queued to the adapter */
1719 HD(j)->cp_stat[i] = FREE;
1721 printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
1722 BN(j), i, SCpnt->pid);
1725 else
1727 /* Any other mailbox has already been set free by interrupt */
1728 continue;
1730 SCpnt->scsi_done(SCpnt);
1731 IRQ_LOCK
1734 HD(j)->in_reset = FALSE;
1735 do_trace = FALSE;
1737 if (arg_done) {
1738 printk("%s: reset, exit, success.\n", BN(j));
1739 return SCSI_RESET_SUCCESS;
1741 else {
1742 printk("%s: reset, exit, wakeup.\n", BN(j));
1743 return SCSI_RESET_PUNT;
1747 int eata2x_old_reset(Scsi_Cmnd *SCarg, unsigned int reset_flags) {
1748 int rtn;
1749 IRQ_FLAGS
1751 IRQ_LOCK_SAVE
1752 rtn = do_old_reset(SCarg);
1753 IRQ_UNLOCK_RESTORE
1754 return rtn;
1757 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,101)
1759 static inline int do_reset(Scsi_Cmnd *SCarg) {
1760 unsigned int i, j, time, k, c, limit = 0;
1761 int arg_done = FALSE;
1762 Scsi_Cmnd *SCpnt;
1764 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1765 printk("%s: reset, enter, target %d.%d:%d, pid %ld.\n",
1766 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1768 if (SCarg->host_scribble == NULL)
1769 printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
1771 if (HD(j)->in_reset) {
1772 printk("%s: reset, exit, already in reset.\n", BN(j));
1773 return FAILED;
1776 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1777 printk("%s: reset, exit, timeout error.\n", BN(j));
1778 return FAILED;
1781 HD(j)->retries = 0;
1783 for (c = 0; c <= sh[j]->max_channel; c++)
1784 for (k = 0; k < sh[j]->max_id; k++) {
1785 HD(j)->target_redo[k][c] = TRUE;
1786 HD(j)->target_to[k][c] = 0;
1789 for (i = 0; i < sh[j]->can_queue; i++) {
1791 if (HD(j)->cp_stat[i] == FREE) continue;
1793 if (HD(j)->cp_stat[i] == LOCKED) {
1794 HD(j)->cp_stat[i] = FREE;
1795 printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
1796 continue;
1799 if (!(SCpnt = HD(j)->cp[i].SCpnt))
1800 panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
1802 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1803 HD(j)->cp_stat[i] = ABORTING;
1804 printk("%s: reset, mbox %d aborting, pid %ld.\n",
1805 BN(j), i, SCpnt->pid);
1808 else {
1809 HD(j)->cp_stat[i] = IN_RESET;
1810 printk("%s: reset, mbox %d in reset, pid %ld.\n",
1811 BN(j), i, SCpnt->pid);
1814 if (SCpnt->host_scribble == NULL)
1815 panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
1817 if (*(unsigned int *)SCpnt->host_scribble != i)
1818 panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
1820 if (SCpnt->scsi_done == NULL)
1821 panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
1823 if (SCpnt == SCarg) arg_done = TRUE;
1826 if (do_dma(sh[j]->io_port, 0, RESET_PIO)) {
1827 printk("%s: reset, cannot reset, timeout error.\n", BN(j));
1828 return FAILED;
1831 printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
1833 #if defined(DEBUG_RESET)
1834 do_trace = TRUE;
1835 #endif
1837 HD(j)->in_reset = TRUE;
1838 SPIN_UNLOCK
1839 IRQ_UNLOCK
1840 time = jiffies;
1841 while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
1842 IRQ_LOCK
1843 SPIN_LOCK
1844 printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
1846 for (i = 0; i < sh[j]->can_queue; i++) {
1848 if (HD(j)->cp_stat[i] == IN_RESET) {
1849 SCpnt = HD(j)->cp[i].SCpnt;
1850 SCpnt->result = DID_RESET << 16;
1851 SCpnt->host_scribble = NULL;
1853 /* This mailbox is still waiting for its interrupt */
1854 HD(j)->cp_stat[i] = LOCKED;
1856 printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
1857 BN(j), i, SCpnt->pid);
1860 else if (HD(j)->cp_stat[i] == ABORTING) {
1861 SCpnt = HD(j)->cp[i].SCpnt;
1862 SCpnt->result = DID_RESET << 16;
1863 SCpnt->host_scribble = NULL;
1865 /* This mailbox was never queued to the adapter */
1866 HD(j)->cp_stat[i] = FREE;
1868 printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
1869 BN(j), i, SCpnt->pid);
1872 else
1874 /* Any other mailbox has already been set free by interrupt */
1875 continue;
1877 SCpnt->scsi_done(SCpnt);
1878 IRQ_LOCK
1881 HD(j)->in_reset = FALSE;
1882 do_trace = FALSE;
1884 if (arg_done) printk("%s: reset, exit, pid %ld done.\n", BN(j), SCarg->pid);
1885 else printk("%s: reset, exit.\n", BN(j));
1887 return SUCCESS;
1890 int eata2x_reset(Scsi_Cmnd *SCarg) {
1892 return do_reset(SCarg);
1895 #endif /* new_eh_code */
1897 int eata2x_biosparam(Disk *disk, kdev_t dev, int *dkinfo) {
1898 int size = disk->capacity;
1900 if (ext_tran || (scsicam_bios_param(disk, dev, dkinfo) < 0)) {
1901 dkinfo[0] = 255;
1902 dkinfo[1] = 63;
1903 dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
1906 #if defined (DEBUG_GEOMETRY)
1907 printk ("%s: biosparam, head=%d, sec=%d, cyl=%d.\n", driver_name,
1908 dkinfo[0], dkinfo[1], dkinfo[2]);
1909 #endif
1911 return FALSE;
1914 static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
1915 unsigned int rev) {
1916 unsigned int i, j, k, y;
1917 unsigned long x;
1919 for (i = 0; i < n - 1; i++) {
1920 k = i;
1922 for (j = k + 1; j < n; j++)
1923 if (rev) {
1924 if (sk[j] > sk[k]) k = j;
1926 else {
1927 if (sk[j] < sk[k]) k = j;
1930 if (k != i) {
1931 x = sk[k]; sk[k] = sk[i]; sk[i] = x;
1932 y = da[k]; da[k] = da[i]; da[i] = y;
1936 return;
1939 static inline int reorder(unsigned int j, unsigned long cursec,
1940 unsigned int ihdlr, unsigned int il[], unsigned int n_ready) {
1941 Scsi_Cmnd *SCpnt;
1942 struct mscp *cpp;
1943 unsigned int k, n;
1944 unsigned int rev = FALSE, s = TRUE, r = TRUE;
1945 unsigned int input_only = TRUE, overlap = FALSE;
1946 unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
1947 unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
1948 unsigned long ioseek = 0;
1950 static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
1951 static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
1952 static unsigned int readysorted = 0, revcount = 0;
1953 static unsigned long seeksorted = 0, seeknosort = 0;
1955 if (link_statistics && !(++flushcount % link_statistics))
1956 printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"\
1957 " av %ldK as %ldK.\n", flushcount, batchcount, inputcount,
1958 ovlcount, readycount, readysorted, sortcount, revcount,
1959 seeknosort / (readycount + 1),
1960 seeksorted / (readycount + 1));
1962 if (n_ready <= 1) return FALSE;
1964 for (n = 0; n < n_ready; n++) {
1965 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1967 if (!cpp->din) input_only = FALSE;
1969 if (SCpnt->request.sector < minsec) minsec = SCpnt->request.sector;
1970 if (SCpnt->request.sector > maxsec) maxsec = SCpnt->request.sector;
1972 sl[n] = SCpnt->request.sector;
1973 ioseek += SCpnt->request.nr_sectors;
1975 if (!n) continue;
1977 if (sl[n] < sl[n - 1]) s = FALSE;
1978 if (sl[n] > sl[n - 1]) r = FALSE;
1980 if (link_statistics) {
1981 if (sl[n] > sl[n - 1])
1982 seek += sl[n] - sl[n - 1];
1983 else
1984 seek += sl[n - 1] - sl[n];
1989 if (link_statistics) {
1990 if (cursec > sl[0]) seek += cursec - sl[0]; else seek += sl[0] - cursec;
1993 if (cursec > ((maxsec + minsec) / 2)) rev = TRUE;
1995 if (ioseek > ((maxsec - minsec) / 2)) rev = FALSE;
1997 if (!((rev && r) || (!rev && s))) sort(sl, il, n_ready, rev);
1999 if (!input_only) for (n = 0; n < n_ready; n++) {
2000 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
2001 ll[n] = SCpnt->request.nr_sectors; pl[n] = SCpnt->pid;
2003 if (!n) continue;
2005 if ((sl[n] == sl[n - 1]) || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
2006 || (rev && ((sl[n] + ll[n]) > sl[n - 1]))) overlap = TRUE;
2009 if (overlap) sort(pl, il, n_ready, FALSE);
2011 if (link_statistics) {
2012 if (cursec > sl[0]) iseek = cursec - sl[0]; else iseek = sl[0] - cursec;
2013 batchcount++; readycount += n_ready, seeknosort += seek / 1024;
2014 if (input_only) inputcount++;
2015 if (overlap) { ovlcount++; seeksorted += iseek / 1024; }
2016 else seeksorted += (iseek + maxsec - minsec) / 1024;
2017 if (rev && !r) { revcount++; readysorted += n_ready; }
2018 if (!rev && !s) { sortcount++; readysorted += n_ready; }
2021 #if defined(DEBUG_LINKED_COMMANDS)
2022 if (link_statistics && (overlap || !(flushcount % link_statistics)))
2023 for (n = 0; n < n_ready; n++) {
2024 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
2025 printk("%s %d.%d:%d pid %ld mb %d fc %d nr %d sec %ld ns %ld"\
2026 " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
2027 (ihdlr ? "ihdlr" : "qcomm"), SCpnt->channel, SCpnt->target,
2028 SCpnt->lun, SCpnt->pid, k, flushcount, n_ready,
2029 SCpnt->request.sector, SCpnt->request.nr_sectors, cursec,
2030 YESNO(s), YESNO(r), YESNO(rev), YESNO(input_only),
2031 YESNO(overlap), cpp->din);
2033 #endif
2034 return overlap;
2037 static void flush_dev(Scsi_Device *dev, unsigned long cursec, unsigned int j,
2038 unsigned int ihdlr) {
2039 Scsi_Cmnd *SCpnt;
2040 struct mscp *cpp;
2041 unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
2043 for (k = 0; k < sh[j]->can_queue; k++) {
2045 if (HD(j)->cp_stat[k] != READY && HD(j)->cp_stat[k] != IN_USE) continue;
2047 cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
2049 if (SCpnt->device != dev) continue;
2051 if (HD(j)->cp_stat[k] == IN_USE) return;
2053 il[n_ready++] = k;
2056 if (reorder(j, cursec, ihdlr, il, n_ready)) n_ready = 1;
2058 for (n = 0; n < n_ready; n++) {
2059 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
2061 if (do_dma(sh[j]->io_port, (unsigned int) cpp, SEND_CP_DMA)) {
2062 printk("%s: %s, target %d.%d:%d, pid %ld, mbox %d, adapter"\
2063 " busy, will abort.\n", BN(j), (ihdlr ? "ihdlr" : "qcomm"),
2064 SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid, k);
2065 HD(j)->cp_stat[k] = ABORTING;
2066 continue;
2069 HD(j)->cp_stat[k] = IN_USE;
2074 static inline void ihdlr(int irq, unsigned int j) {
2075 Scsi_Cmnd *SCpnt;
2076 unsigned int i, k, c, status, tstatus, reg;
2077 struct mssp *dspp, *spp;
2078 struct mscp *cpp;
2080 if (sh[j]->irq != irq)
2081 panic("%s: ihdlr, irq %d, sh[j]->irq %d.\n", BN(j), irq, sh[j]->irq);
2083 /* Check if this board need to be serviced */
2084 if (!(inb(sh[j]->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)) return;
2086 HD(j)->iocount++;
2088 if (do_trace) printk("%s: ihdlr, enter, irq %d, count %d.\n", BN(j), irq,
2089 HD(j)->iocount);
2091 /* Check if this board is still busy */
2092 if (wait_on_busy(sh[j]->io_port, 20 * MAXLOOP)) {
2093 reg = inb(sh[j]->io_port + REG_STATUS);
2094 printk("%s: ihdlr, busy timeout error, irq %d, reg 0x%x, count %d.\n",
2095 BN(j), irq, reg, HD(j)->iocount);
2096 return;
2099 dspp = &HD(j)->sp[0];
2100 spp = &HD(j)->sp[1];
2102 /* Make a local copy just before clearing the interrupt indication */
2103 memcpy(spp, dspp, sizeof(struct mssp));
2105 /* Clear the completion flag and cp pointer on the dynamic copy of sp */
2106 memset(dspp, 0, sizeof(struct mssp));
2108 /* Read the status register to clear the interrupt indication */
2109 reg = inb(sh[j]->io_port + REG_STATUS);
2111 /* Reject any sp with supspect data */
2112 if (spp->eoc == FALSE)
2113 printk("%s: ihdlr, spp->eoc == FALSE, irq %d, reg 0x%x, count %d.\n",
2114 BN(j), irq, reg, HD(j)->iocount);
2115 if (spp->cpp == NULL)
2116 printk("%s: ihdlr, spp->cpp == NULL, irq %d, reg 0x%x, count %d.\n",
2117 BN(j), irq, reg, HD(j)->iocount);
2118 if (spp->eoc == FALSE || spp->cpp == NULL) return;
2120 cpp = spp->cpp;
2122 #if defined(DEBUG_GENERATE_ABORTS)
2123 if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 500) < 3)) return;
2124 #endif
2126 /* Find the mailbox to be serviced on this board */
2127 i = cpp - HD(j)->cp;
2129 if (cpp < HD(j)->cp || cpp >= HD(j)->cp + sh[j]->can_queue
2130 || i >= sh[j]->can_queue)
2131 panic("%s: ihdlr, invalid mscp bus address %p, cp0 %p.\n", BN(j),
2132 cpp, HD(j)->cp);
2134 if (HD(j)->cp_stat[i] == IGNORE) {
2135 HD(j)->cp_stat[i] = FREE;
2136 return;
2138 else if (HD(j)->cp_stat[i] == LOCKED) {
2139 HD(j)->cp_stat[i] = FREE;
2140 printk("%s: ihdlr, mbox %d unlocked, count %d.\n", BN(j), i,
2141 HD(j)->iocount);
2142 return;
2144 else if (HD(j)->cp_stat[i] == FREE) {
2145 printk("%s: ihdlr, mbox %d is free, count %d.\n", BN(j), i,
2146 HD(j)->iocount);
2147 return;
2149 else if (HD(j)->cp_stat[i] == IN_RESET)
2150 printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
2151 else if (HD(j)->cp_stat[i] != IN_USE)
2152 panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
2153 BN(j), i, HD(j)->cp_stat[i]);
2155 HD(j)->cp_stat[i] = FREE;
2156 SCpnt = cpp->SCpnt;
2158 if (SCpnt == NULL) panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
2160 if (SCpnt->host_scribble == NULL)
2161 panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", BN(j), i,
2162 SCpnt->pid, SCpnt);
2164 if (*(unsigned int *)SCpnt->host_scribble != i)
2165 panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
2166 BN(j), i, SCpnt->pid, *(unsigned int *)SCpnt->host_scribble);
2168 if (linked_comm && SCpnt->device->queue_depth > 2
2169 && TLDEV(SCpnt->device->type))
2170 flush_dev(SCpnt->device, SCpnt->request.sector, j, TRUE);
2172 tstatus = status_byte(spp->target_status);
2174 #if defined(DEBUG_GENERATE_ERRORS)
2175 if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 200) < 2))
2176 spp->adapter_status = 0x01;
2177 #endif
2179 switch (spp->adapter_status) {
2180 case ASOK: /* status OK */
2182 /* Forces a reset if a disk drive keeps returning BUSY */
2183 if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
2184 status = DID_ERROR << 16;
2186 /* If there was a bus reset, redo operation on each target */
2187 else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
2188 && HD(j)->target_redo[SCpnt->target][SCpnt->channel])
2189 status = DID_BUS_BUSY << 16;
2191 /* Works around a flaw in scsi.c */
2192 else if (tstatus == CHECK_CONDITION
2193 && SCpnt->device->type == TYPE_DISK
2194 && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
2195 status = DID_BUS_BUSY << 16;
2197 else
2198 status = DID_OK << 16;
2200 if (tstatus == GOOD)
2201 HD(j)->target_redo[SCpnt->target][SCpnt->channel] = FALSE;
2203 if (spp->target_status && SCpnt->device->type == TYPE_DISK)
2204 printk("%s: ihdlr, target %d.%d:%d, pid %ld, "\
2205 "target_status 0x%x, sense key 0x%x.\n", BN(j),
2206 SCpnt->channel, SCpnt->target, SCpnt->lun,
2207 SCpnt->pid, spp->target_status,
2208 SCpnt->sense_buffer[2]);
2210 HD(j)->target_to[SCpnt->target][SCpnt->channel] = 0;
2212 if (HD(j)->last_retried_pid == SCpnt->pid) HD(j)->retries = 0;
2214 break;
2215 case ASST: /* Selection Time Out */
2216 case 0x02: /* Command Time Out */
2218 if (HD(j)->target_to[SCpnt->target][SCpnt->channel] > 1)
2219 status = DID_ERROR << 16;
2220 else {
2221 status = DID_TIME_OUT << 16;
2222 HD(j)->target_to[SCpnt->target][SCpnt->channel]++;
2225 break;
2227 /* Perform a limited number of internal retries */
2228 case 0x03: /* SCSI Bus Reset Received */
2229 case 0x04: /* Initial Controller Power-up */
2231 for (c = 0; c <= sh[j]->max_channel; c++)
2232 for (k = 0; k < sh[j]->max_id; k++)
2233 HD(j)->target_redo[k][c] = TRUE;
2235 if (SCpnt->device->type != TYPE_TAPE
2236 && HD(j)->retries < MAX_INTERNAL_RETRIES) {
2238 #if defined(DID_SOFT_ERROR)
2239 status = DID_SOFT_ERROR << 16;
2240 #else
2241 status = DID_BUS_BUSY << 16;
2242 #endif
2243 HD(j)->retries++;
2244 HD(j)->last_retried_pid = SCpnt->pid;
2246 else
2247 status = DID_ERROR << 16;
2249 break;
2250 case 0x05: /* Unexpected Bus Phase */
2251 case 0x06: /* Unexpected Bus Free */
2252 case 0x07: /* Bus Parity Error */
2253 case 0x08: /* SCSI Hung */
2254 case 0x09: /* Unexpected Message Reject */
2255 case 0x0a: /* SCSI Bus Reset Stuck */
2256 case 0x0b: /* Auto Request-Sense Failed */
2257 case 0x0c: /* Controller Ram Parity Error */
2258 default:
2259 status = DID_ERROR << 16;
2260 break;
2263 SCpnt->result = status | spp->target_status;
2265 #if defined(DEBUG_INTERRUPT)
2266 if (SCpnt->result || do_trace)
2267 #else
2268 if ((spp->adapter_status != ASOK && HD(j)->iocount > 1000) ||
2269 (spp->adapter_status != ASOK &&
2270 spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
2271 do_trace || msg_byte(spp->target_status))
2272 #endif
2273 printk("%s: ihdlr, mbox %2d, err 0x%x:%x,"\
2274 " target %d.%d:%d, pid %ld, reg 0x%x, count %d.\n",
2275 BN(j), i, spp->adapter_status, spp->target_status,
2276 SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid,
2277 reg, HD(j)->iocount);
2279 /* Set the command state to inactive */
2280 SCpnt->host_scribble = NULL;
2282 SCpnt->scsi_done(SCpnt);
2284 if (do_trace) printk("%s: ihdlr, exit, irq %d, count %d.\n", BN(j), irq,
2285 HD(j)->iocount);
2287 return;
2290 static void do_interrupt_handler(int irq, void *shap, struct pt_regs *regs) {
2291 unsigned int j;
2292 IRQ_FLAGS
2293 SPIN_FLAGS
2295 /* Check if the interrupt must be processed by this handler */
2296 if ((j = (unsigned int)((char *)shap - sha)) >= num_boards) return;
2298 SPIN_LOCK_SAVE
2299 IRQ_LOCK_SAVE
2300 ihdlr(irq, j);
2301 IRQ_UNLOCK_RESTORE
2302 SPIN_UNLOCK_RESTORE
2305 int eata2x_release(struct Scsi_Host *shpnt) {
2306 unsigned int i, j;
2307 IRQ_FLAGS
2309 IRQ_LOCK_SAVE
2311 for (j = 0; sh[j] != NULL && sh[j] != shpnt; j++);
2313 if (sh[j] == NULL) panic("%s: release, invalid Scsi_Host pointer.\n",
2314 driver_name);
2316 for (i = 0; i < sh[j]->can_queue; i++)
2317 if ((&HD(j)->cp[i])->sglist) kfree((&HD(j)->cp[i])->sglist);
2319 free_irq(sh[j]->irq, &sha[j]);
2321 if (sh[j]->dma_channel != NO_DMA) free_dma(sh[j]->dma_channel);
2323 release_region(sh[j]->io_port, sh[j]->n_io_port);
2324 scsi_unregister(sh[j]);
2325 IRQ_UNLOCK_RESTORE
2326 return FALSE;
2329 #if defined(MODULE)
2330 Scsi_Host_Template driver_template = EATA;
2332 #include "scsi_module.c"
2333 #endif