make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / scsi / eata.c
1 /*
2  *      eata.c - Low-level driver for EATA/DMA SCSI host adapters.
3  *
4  *      25 Jun 2002 Rev. 6.70 for linux 2.4.19
5  *        + This release is the first one tested on a Big Endian platform:
6  *          fixed endian-ness problem due to bitfields;
7  *          fixed endian-ness problem in read_pio.
8  *        + Added new options for selectively probing ISA, EISA and PCI bus:
9  *
10  *          Boot option   Parameter name    Default according to
11  *
12  *          ip:[y|n]      isa_probe=[1|0]   CONFIG_ISA  defined
13  *          ep:[y|n]      eisa_probe=[1|0]  CONFIG_EISA defined
14  *          pp:[y|n]      pci_probe=[1|0]   CONFIG_PCI  defined
15  *
16  *          The default action is to perform probing if the corrisponding
17  *          bus is configured and to skip probing otherwise.
18  *
19  *        + If pci_probe is in effect and a list of I/O  ports is specified
20  *          as parameter or boot option, pci_enable_device() is performed
21  *          on all pci devices matching PCI_CLASS_STORAGE_SCSI.
22  *
23  *      21 Feb 2002 Rev. 6.52 for linux 2.4.18
24  *        + Backport from rev. 7.22 (use io_request_lock).
25  *
26  *      20 Feb 2002 Rev. 7.22 for linux 2.5.5
27  *        + Remove any reference to virt_to_bus().
28  *        + Fix pio hang while detecting multiple HBAs.
29  *        + Fixed a board detection bug: in a system with
30  *          multiple ISA/EISA boards, all but the first one
31  *          were erroneously detected as PCI.
32  *
33  *      01 Jan 2002 Rev. 7.20 for linux 2.5.1
34  *        + Use the dynamic DMA mapping API.
35  *
36  *      19 Dec 2001 Rev. 7.02 for linux 2.5.1
37  *        + Use SCpnt->sc_data_direction if set.
38  *        + Use sglist.page instead of sglist.address.
39  *
40  *      11 Dec 2001 Rev. 7.00 for linux 2.5.1
41  *        + Use host->host_lock instead of io_request_lock.
42  *
43  *       1 May 2001 Rev. 6.05 for linux 2.4.4
44  *        + Clean up all pci related routines.
45  *        + Fix data transfer direction for opcode SEND_CUE_SHEET (0x5d)
46  *
47  *      30 Jan 2001 Rev. 6.04 for linux 2.4.1
48  *        + Call pci_resource_start after pci_enable_device.
49  *
50  *      25 Jan 2001 Rev. 6.03 for linux 2.4.0
51  *        + "check_region" call replaced by "request_region".
52  *
53  *      22 Nov 2000 Rev. 6.02 for linux 2.4.0-test11
54  *        + Return code checked when calling pci_enable_device.
55  *        + Removed old scsi error handling support.
56  *        + The obsolete boot option flag eh:n is silently ignored.
57  *        + Removed error messages while a disk drive is powered up at
58  *          boot time.
59  *        + Improved boot messages: all tagged capable device are
60  *          indicated as "tagged" or "soft-tagged" :
61  *          - "soft-tagged"  means that the driver is trying to do its
62  *            own tagging (i.e. the tc:y option is in effect);
63  *          - "tagged" means that the device supports tagged commands,
64  *            but the driver lets the HBA be responsible for tagging
65  *            support.
66  *
67  *      16 Sep 1999 Rev. 5.11 for linux 2.2.12 and 2.3.18
68  *        + Updated to the new __setup interface for boot command line options.
69  *        + When loaded as a module, accepts the new parameter boot_options
70  *          which value is a string with the same format of the kernel boot
71  *          command line options. A valid example is:
72  *          modprobe eata 'boot_options="0x7410,0x230,lc:y,tc:n,mq:4"'
73  *
74  *       9 Sep 1999 Rev. 5.10 for linux 2.2.12 and 2.3.17
75  *        + 64bit cleanup for Linux/Alpha platform support
76  *          (contribution from H.J. Lu).
77  *
78  *      22 Jul 1999 Rev. 5.00 for linux 2.2.10 and 2.3.11
79  *        + Removed pre-2.2 source code compatibility.
80  *        + Added call to pci_set_master.
81  *
82  *      26 Jul 1998 Rev. 4.33 for linux 2.0.35 and 2.1.111
83  *        + Added command line option (rs:[y|n]) to reverse the scan order
84  *          of PCI boards. The default is rs:y, which reverses the BIOS order
85  *          while registering PCI boards. The default value rs:y generates
86  *          the same order of all previous revisions of this driver.
87  *          Pls. note that "BIOS order" might have been reversed itself
88  *          after the 2.1.9x PCI modifications in the linux kernel.
89  *          The rs value is ignored when the explicit list of addresses
90  *          is used by the "eata=port0,port1,..." command line option.
91  *        + Added command line option (et:[y|n]) to force use of extended
92  *          translation (255 heads, 63 sectors) as disk geometry.
93  *          The default is et:n, which uses the disk geometry returned
94  *          by scsicam_bios_param. The default value et:n is compatible with
95  *          all previous revisions of this driver.
96  *
97  *      28 May 1998 Rev. 4.32 for linux 2.0.33 and 2.1.104
98  *          Increased busy timeout from 10 msec. to 200 msec. while
99  *          processing interrupts.
100  *
101  *      16 May 1998 Rev. 4.31 for linux 2.0.33 and 2.1.102
102  *          Improved abort handling during the eh recovery process.
103  *
104  *      13 May 1998 Rev. 4.30 for linux 2.0.33 and 2.1.101
105  *          The driver is now fully SMP safe, including the
106  *          abort and reset routines.
107  *          Added command line options (eh:[y|n]) to choose between
108  *          new_eh_code and the old scsi code.
109  *          If linux version >= 2.1.101 the default is eh:y, while the eh
110  *          option is ignored for previous releases and the old scsi code
111  *          is used.
112  *
113  *      18 Apr 1998 Rev. 4.20 for linux 2.0.33 and 2.1.97
114  *          Reworked interrupt handler.
115  *
116  *      11 Apr 1998 rev. 4.05 for linux 2.0.33 and 2.1.95
117  *          Major reliability improvement: when a batch with overlapping
118  *          requests is detected, requests are queued one at a time
119  *          eliminating any possible board or drive reordering.
120  *
121  *      10 Apr 1998 rev. 4.04 for linux 2.0.33 and 2.1.95
122  *          Improved SMP support (if linux version >= 2.1.95).
123  *
124  *       9 Apr 1998 rev. 4.03 for linux 2.0.33 and 2.1.94
125  *          Added support for new PCI code and IO-APIC remapping of irqs.
126  *          Performance improvement: when sequential i/o is detected,
127  *          always use direct sort instead of reverse sort.
128  *
129  *       4 Apr 1998 rev. 4.02 for linux 2.0.33 and 2.1.92
130  *          io_port is now unsigned long.
131  *
132  *      17 Mar 1998 rev. 4.01 for linux 2.0.33 and 2.1.88
133  *          Use new scsi error handling code (if linux version >= 2.1.88).
134  *          Use new interrupt code.
135  *
136  *      12 Sep 1997 rev. 3.11 for linux 2.0.30 and 2.1.55
137  *          Use of udelay inside the wait loops to avoid timeout
138  *          problems with fast cpus.
139  *          Removed check about useless calls to the interrupt service
140  *          routine (reported on SMP systems only).
141  *          At initialization time "sorted/unsorted" is displayed instead
142  *          of "linked/unlinked" to reinforce the fact that "linking" is
143  *          nothing but "elevator sorting" in the actual implementation.
144  *
145  *      17 May 1997 rev. 3.10 for linux 2.0.30 and 2.1.38
146  *          Use of serial_number_at_timeout in abort and reset processing.
147  *          Use of the __initfunc and __initdata macro in setup code.
148  *          Minor cleanups in the list_statistics code.
149  *          Increased controller busy timeout in order to better support
150  *          slow SCSI devices.
151  *
152  *      24 Feb 1997 rev. 3.00 for linux 2.0.29 and 2.1.26
153  *          When loading as a module, parameter passing is now supported
154  *          both in 2.0 and in 2.1 style.
155  *          Fixed data transfer direction for some SCSI opcodes.
156  *          Immediate acknowledge to request sense commands.
157  *          Linked commands to each disk device are now reordered by elevator
158  *          sorting. Rare cases in which reordering of write requests could
159  *          cause wrong results are managed.
160  *          Fixed spurious timeouts caused by long simple queue tag sequences.
161  *          New command line option (tm:[0-3]) to choose the type of tags:
162  *          0 -> mixed (default); 1 -> simple; 2 -> head; 3 -> ordered.
163  *
164  *      18 Jan 1997 rev. 2.60 for linux 2.1.21 and 2.0.28
165  *          Added command line options to enable/disable linked commands
166  *          (lc:[y|n]), tagged commands (tc:[y|n]) and to set the max queue
167  *          depth (mq:xx). Default is "eata=lc:n,tc:n,mq:16".
168  *          Improved command linking.
169  *          Documented how to setup RAID-0 with DPT SmartRAID boards.
170  *
171  *       8 Jan 1997 rev. 2.50 for linux 2.1.20 and 2.0.27
172  *          Added linked command support.
173  *          Improved detection of PCI boards using ISA base addresses.
174  *
175  *       3 Dec 1996 rev. 2.40 for linux 2.1.14 and 2.0.27
176  *          Added support for tagged commands and queue depth adjustment.
177  *
178  *      22 Nov 1996 rev. 2.30 for linux 2.1.12 and 2.0.26
179  *          When CONFIG_PCI is defined, BIOS32 is used to include in the
180  *          list of i/o ports to be probed all the PCI SCSI controllers.
181  *          The list of i/o ports to be probed can be overwritten by the
182  *          "eata=port0,port1,...." boot command line option.
183  *          Scatter/gather lists are now allocated by a number of kmalloc
184  *          calls, in order to avoid the previous size limit of 64Kb.
185  *
186  *      16 Nov 1996 rev. 2.20 for linux 2.1.10 and 2.0.25
187  *          Added support for EATA 2.0C, PCI, multichannel and wide SCSI.
188  *
189  *      27 Sep 1996 rev. 2.12 for linux 2.1.0
190  *          Portability cleanups (virtual/bus addressing, little/big endian
191  *          support).
192  *
193  *      09 Jul 1996 rev. 2.11 for linux 2.0.4
194  *          Number of internal retries is now limited.
195  *
196  *      16 Apr 1996 rev. 2.10 for linux 1.3.90
197  *          New argument "reset_flags" to the reset routine.
198  *
199  *       6 Jul 1995 rev. 2.01 for linux 1.3.7
200  *          Update required by the new /proc/scsi support.
201  *
202  *      11 Mar 1995 rev. 2.00 for linux 1.2.0
203  *          Fixed a bug which prevented media change detection for removable
204  *          disk drives.
205  *
206  *      23 Feb 1995 rev. 1.18 for linux 1.1.94
207  *          Added a check for scsi_register returning NULL.
208  *
209  *      11 Feb 1995 rev. 1.17 for linux 1.1.91
210  *          Now DEBUG_RESET is disabled by default.
211  *          Register a board even if it does not assert DMA protocol support
212  *          (DPT SK2011B does not report correctly the dmasup bit).
213  *
214  *       9 Feb 1995 rev. 1.16 for linux 1.1.90
215  *          Use host->wish_block instead of host->block.
216  *          New list of Data Out SCSI commands.
217  *
218  *       8 Feb 1995 rev. 1.15 for linux 1.1.89
219  *          Cleared target_time_out counter while performing a reset.
220  *          All external symbols renamed to avoid possible name conflicts.
221  *
222  *      28 Jan 1995 rev. 1.14 for linux 1.1.86
223  *          Added module support.
224  *          Log and do a retry when a disk drive returns a target status
225  *          different from zero on a recovered error.
226  *
227  *      24 Jan 1995 rev. 1.13 for linux 1.1.85
228  *          Use optimized board configuration, with a measured performance
229  *          increase in the range 10%-20% on i/o throughput.
230  *
231  *      16 Jan 1995 rev. 1.12 for linux 1.1.81
232  *          Fix mscp structure comments (no functional change).
233  *          Display a message if check_region detects a port address
234  *          already in use.
235  *
236  *      17 Dec 1994 rev. 1.11 for linux 1.1.74
237  *          Use the scsicam_bios_param routine. This allows an easy
238  *          migration path from disk partition tables created using
239  *          different SCSI drivers and non optimal disk geometry.
240  *
241  *      15 Dec 1994 rev. 1.10 for linux 1.1.74
242  *          Added support for ISA EATA boards (DPT PM2011, DPT PM2021).
243  *          The host->block flag is set for all the detected ISA boards.
244  *          The detect routine no longer enforces LEVEL triggering
245  *          for EISA boards, it just prints a warning message.
246  *
247  *      30 Nov 1994 rev. 1.09 for linux 1.1.68
248  *          Redo i/o on target status CHECK_CONDITION for TYPE_DISK only.
249  *          Added optional support for using a single board at a time.
250  *
251  *      18 Nov 1994 rev. 1.08 for linux 1.1.64
252  *          Forces sg_tablesize = 64 and can_queue = 64 if these
253  *          values are not correctly detected (DPT PM2012).
254  *
255  *      14 Nov 1994 rev. 1.07 for linux 1.1.63  Final BETA release.
256  *      04 Aug 1994 rev. 1.00 for linux 1.1.39  First BETA release.
257  *
258  *
259  *          This driver is based on the CAM (Common Access Method Committee)
260  *          EATA (Enhanced AT Bus Attachment) rev. 2.0A, using DMA protocol.
261  *
262  *  Copyright (C) 1994-2002 Dario Ballabio (ballabio_dario@emc.com)
263  *
264  *  Alternate email: dario.ballabio@inwind.it, dario.ballabio@tiscalinet.it
265  *
266  *  Redistribution and use in source and binary forms, with or without
267  *  modification, are permitted provided that redistributions of source
268  *  code retain the above copyright notice and this comment without
269  *  modification.
270  *
271  */
272
273 /*
274  *
275  *  Here is a brief description of the DPT SCSI host adapters.
276  *  All these boards provide an EATA/DMA compatible programming interface
277  *  and are fully supported by this driver in any configuration, including
278  *  multiple SCSI channels:
279  *
280  *  PM2011B/9X -  Entry Level ISA
281  *  PM2021A/9X -  High Performance ISA
282  *  PM2012A       Old EISA
283  *  PM2012B       Old EISA
284  *  PM2022A/9X -  Entry Level EISA
285  *  PM2122A/9X -  High Performance EISA
286  *  PM2322A/9X -  Extra High Performance EISA
287  *  PM3021     -  SmartRAID Adapter for ISA
288  *  PM3222     -  SmartRAID Adapter for EISA (PM3222W is 16-bit wide SCSI)
289  *  PM3224     -  SmartRAID Adapter for PCI  (PM3224W is 16-bit wide SCSI)
290  *  PM33340UW  -  SmartRAID Adapter for PCI  ultra wide multichannel
291  *
292  *  The above list is just an indication: as a matter of fact all DPT
293  *  boards using the EATA/DMA protocol are supported by this driver,
294  *  since they use exactely the same programming interface.
295  *
296  *  The DPT PM2001 provides only the EATA/PIO interface and hence is not
297  *  supported by this driver.
298  *
299  *  This code has been tested with up to 3 Distributed Processing Technology
300  *  PM2122A/9X (DPT SCSI BIOS v002.D1, firmware v05E.0) EISA controllers,
301  *  in any combination of private and shared IRQ.
302  *  PCI support has been tested using up to 2 DPT PM3224W (DPT SCSI BIOS
303  *  v003.D0, firmware v07G.0).
304  *
305  *  DPT SmartRAID boards support "Hardware Array" - a group of disk drives
306  *  which are all members of the same RAID-0, RAID-1 or RAID-5 array implemented
307  *  in host adapter hardware. Hardware Arrays are fully compatible with this
308  *  driver, since they look to it as a single disk drive.
309  *
310  *  WARNING: to create a RAID-0 "Hardware Array" you must select "Other Unix"
311  *  as the current OS in the DPTMGR "Initial System Installation" menu.
312  *  Otherwise RAID-0 is generated as an "Array Group" (i.e. software RAID-0),
313  *  which is not supported by the actual SCSI subsystem.
314  *  To get the "Array Group" functionality, the Linux MD driver must be used
315  *  instead of the DPT "Array Group" feature.
316  *
317  *  Multiple ISA, EISA and PCI boards can be configured in the same system.
318  *  It is suggested to put all the EISA boards on the same IRQ level, all
319  *  the PCI  boards on another IRQ level, while ISA boards cannot share
320  *  interrupts.
321  *
322  *  If you configure multiple boards on the same IRQ, the interrupt must
323  *  be _level_ triggered (not _edge_ triggered).
324  *
325  *  This driver detects EATA boards by probes at fixed port addresses,
326  *  so no BIOS32 or PCI BIOS support is required.
327  *  The suggested way to detect a generic EATA PCI board is to force on it
328  *  any unused EISA address, even if there are other controllers on the EISA
329  *  bus, or even if you system has no EISA bus at all.
330  *  Do not force any ISA address on EATA PCI boards.
331  *
332  *  If PCI bios support is configured into the kernel, BIOS32 is used to
333  *  include in the list of i/o ports to be probed all the PCI SCSI controllers.
334  *
335  *  Due to a DPT BIOS "feature", it might not be possible to force an EISA
336  *  address on more than a single DPT PCI board, so in this case you have to
337  *  let the PCI BIOS assign the addresses.
338  *
339  *  The sequence of detection probes is:
340  *
341  *  - ISA 0x1F0;
342  *  - PCI SCSI controllers (only if BIOS32 is available);
343  *  - EISA/PCI 0x1C88 through 0xFC88 (corresponding to EISA slots 1 to 15);
344  *  - ISA  0x170, 0x230, 0x330.
345  *
346  *  The above list of detection probes can be totally replaced by the
347  *  boot command line option: "eata=port0,port1,port2,...", where the
348  *  port0, port1... arguments are ISA/EISA/PCI addresses to be probed.
349  *  For example using "eata=0x7410,0x7450,0x230", the driver probes
350  *  only the two PCI addresses 0x7410 and 0x7450 and the ISA address 0x230,
351  *  in this order; "eata=0" totally disables this driver.
352  *
353  *  After the optional list of detection probes, other possible command line
354  *  options are:
355  *
356  *  et:y  force use of extended translation (255 heads, 63 sectors);
357  *  et:n  use disk geometry detected by scsicam_bios_param;
358  *  rs:y  reverse scan order while detecting PCI boards;
359  *  rs:n  use BIOS order while detecting PCI boards;
360  *  lc:y  enables linked commands;
361  *  lc:n  disables linked commands;
362  *  tc:y  enables tagged commands;
363  *  tc:n  disables tagged commands;
364  *  tm:0  use head/simple/ordered queue tag sequences;
365  *  tm:1  use only simple queue tags;
366  *  tm:2  use only head of queue tags;
367  *  tm:3  use only ordered queue tags;
368  *  mq:xx set the max queue depth to the value xx (2 <= xx <= 32).
369  *
370  *  The default value is: "eata=lc:n,tc:n,mq:16,tm:0,et:n,rs:n".
371  *  An example using the list of detection probes could be:
372  *  "eata=0x7410,0x230,lc:y,tc:n,mq:4,et:n".
373  *
374  *  When loading as a module, parameters can be specified as well.
375  *  The above example would be (use 1 in place of y and 0 in place of n):
376  *
377  *  modprobe eata io_port=0x7410,0x230 linked_comm=1 tagged_comm=0 \
378  *                max_queue_depth=4 tag_mode=0 \
379  *                ext_tran=0 rev_scan=1
380  *
381  *  ----------------------------------------------------------------------------
382  *  In this implementation, linked commands are designed to work with any DISK
383  *  or CD-ROM, since this linking has only the intent of clustering (time-wise)
384  *  and reordering by elevator sorting commands directed to each device,
385  *  without any relation with the actual SCSI protocol between the controller
386  *  and the device.
387  *  If Q is the queue depth reported at boot time for each device (also named
388  *  cmds/lun) and Q > 2, whenever there is already an active command to the
389  *  device all other commands to the same device  (up to Q-1) are kept waiting
390  *  in the elevator sorting queue. When the active command completes, the
391  *  commands in this queue are sorted by sector address. The sort is chosen
392  *  between increasing or decreasing by minimizing the seek distance between
393  *  the sector of the commands just completed and the sector of the first
394  *  command in the list to be sorted.
395  *  Trivial math assures that the unsorted average seek distance when doing
396  *  random seeks over S sectors is S/3.
397  *  When (Q-1) requests are uniformly distributed over S sectors, the average
398  *  distance between two adjacent requests is S/((Q-1) + 1), so the sorted
399  *  average seek distance for (Q-1) random requests over S sectors is S/Q.
400  *  The elevator sorting hence divides the seek distance by a factor Q/3.
401  *  The above pure geometric remarks are valid in all cases and the
402  *  driver effectively reduces the seek distance by the predicted factor
403  *  when there are Q concurrent read i/o operations on the device, but this
404  *  does not necessarily results in a noticeable performance improvement:
405  *  your mileage may vary....
406  *
407  *  Note: command reordering inside a batch of queued commands could cause
408  *        wrong results only if there is at least one write request and the
409  *        intersection (sector-wise) of all requests is not empty.
410  *        When the driver detects a batch including overlapping requests
411  *        (a really rare event) strict serial (pid) order is enforced.
412  *  ----------------------------------------------------------------------------
413  *  The extended translation option (et:y) is useful when using large physical
414  *  disks/arrays. It could also be useful when switching between Adaptec boards
415  *  and DPT boards without reformatting the disk.
416  *  When a boot disk is partitioned with extended translation, in order to
417  *  be able to boot it with a DPT board is could be necessary to add to
418  *  lilo.conf additional commands as in the following example:
419  *
420  *  fix-table
421  *  disk=/dev/sda bios=0x80 sectors=63 heads=128 cylindres=546
422  *
423  *  where the above geometry should be replaced with the one reported at
424  *  power up by the DPT controller.
425  *  ----------------------------------------------------------------------------
426  *
427  *  The boards are named EATA0, EATA1,... according to the detection order.
428  *
429  *  In order to support multiple ISA boards in a reliable way,
430  *  the driver sets host->wish_block = TRUE for all ISA boards.
431  */
432
433 #include <linux/version.h>
434
435 #ifndef LinuxVersionCode
436 #define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
437 #endif
438
439 #define MAX_INT_PARAM 10
440
441 #if defined(MODULE)
442 #include <linux/module.h>
443
444 MODULE_PARM(boot_options, "s");
445 MODULE_PARM(io_port, "1-" __MODULE_STRING(MAX_INT_PARAM) "i");
446 MODULE_PARM(linked_comm, "i");
447 MODULE_PARM(tagged_comm, "i");
448 MODULE_PARM(link_statistics, "i");
449 MODULE_PARM(max_queue_depth, "i");
450 MODULE_PARM(tag_mode, "i");
451 MODULE_PARM(ext_tran, "i");
452 MODULE_PARM(rev_scan, "i");
453 MODULE_PARM(isa_probe, "i");
454 MODULE_PARM(eisa_probe, "i");
455 MODULE_PARM(pci_probe, "i");
456 MODULE_AUTHOR("Dario Ballabio");
457
458 #endif
459
460 #include <linux/string.h>
461 #include <linux/sched.h>
462 #include <linux/kernel.h>
463 #include <linux/ioport.h>
464 #include <linux/delay.h>
465 #include <asm/io.h>
466 #include <asm/system.h>
467 #include <asm/byteorder.h>
468 #include <linux/proc_fs.h>
469 #include <linux/blk.h>
470 #include "scsi.h"
471 #include "hosts.h"
472 #include "sd.h"
473 #include <asm/dma.h>
474 #include <asm/irq.h>
475 #include "eata.h"
476 #include <linux/stat.h>
477 #include <linux/config.h>
478 #include <linux/pci.h>
479 #include <linux/init.h>
480 #include <linux/ctype.h>
481 #include <linux/spinlock.h>
482
483 #if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
484 #error "Adjust your <asm/byteorder.h> defines"
485 #endif
486
487 /* Subversion values */
488 #define ISA  0
489 #define ESA 1
490
491 #undef  FORCE_CONFIG
492
493 #undef  DEBUG_LINKED_COMMANDS
494 #undef  DEBUG_DETECT
495 #undef  DEBUG_PCI_DETECT
496 #undef  DEBUG_INTERRUPT
497 #undef  DEBUG_RESET
498 #undef  DEBUG_GENERATE_ERRORS
499 #undef  DEBUG_GENERATE_ABORTS
500 #undef  DEBUG_GEOMETRY
501
502 #define MAX_ISA 4
503 #define MAX_VESA 0
504 #define MAX_EISA 15
505 #define MAX_PCI 16
506 #define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
507 #define MAX_CHANNEL 4
508 #define MAX_LUN 32
509 #define MAX_TARGET 32
510 #define MAX_MAILBOXES 64
511 #define MAX_SGLIST 64
512 #define MAX_LARGE_SGLIST 122
513 #define MAX_INTERNAL_RETRIES 64
514 #define MAX_CMD_PER_LUN 2
515 #define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
516
517 #define SKIP ULONG_MAX
518 #define FALSE 0
519 #define TRUE 1
520 #define FREE 0
521 #define IN_USE   1
522 #define LOCKED   2
523 #define IN_RESET 3
524 #define IGNORE   4
525 #define READY    5
526 #define ABORTING 6
527 #define NO_DMA  0xff
528 #define MAXLOOP  10000
529 #define TAG_MIXED    0
530 #define TAG_SIMPLE   1
531 #define TAG_HEAD     2
532 #define TAG_ORDERED  3
533
534 #define REG_CMD         7
535 #define REG_STATUS      7
536 #define REG_AUX_STATUS  8
537 #define REG_DATA        0
538 #define REG_DATA2       1
539 #define REG_SEE         6
540 #define REG_LOW         2
541 #define REG_LM          3
542 #define REG_MID         4
543 #define REG_MSB         5
544 #define REGION_SIZE     9UL
545 #define MAX_ISA_ADDR    0x03ff
546 #define MIN_EISA_ADDR   0x1c88
547 #define MAX_EISA_ADDR   0xfc88
548 #define BSY_ASSERTED      0x80
549 #define DRQ_ASSERTED      0x08
550 #define ABSY_ASSERTED     0x01
551 #define IRQ_ASSERTED      0x02
552 #define READ_CONFIG_PIO   0xf0
553 #define SET_CONFIG_PIO    0xf1
554 #define SEND_CP_PIO       0xf2
555 #define RECEIVE_SP_PIO    0xf3
556 #define TRUNCATE_XFR_PIO  0xf4
557 #define RESET_PIO         0xf9
558 #define READ_CONFIG_DMA   0xfd
559 #define SET_CONFIG_DMA    0xfe
560 #define SEND_CP_DMA       0xff
561 #define ASOK              0x00
562 #define ASST              0x01
563
564 #define YESNO(a) ((a) ? 'y' : 'n')
565 #define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
566
567 /* "EATA", in Big Endian format */
568 #define EATA_SIG_BE 0x45415441
569
570 /* Number of valid bytes in the board config structure for EATA 2.0x */
571 #define EATA_2_0A_SIZE 28
572 #define EATA_2_0B_SIZE 30
573 #define EATA_2_0C_SIZE 34
574
575 /* Board info structure */
576 struct eata_info {
577    u_int32_t data_len;  /* Number of valid bytes after this field */
578    u_int32_t sign;      /* ASCII "EATA" signature */
579
580 #if defined(__BIG_ENDIAN_BITFIELD)
581    unchar version:4, :4;
582    unchar  haaval:1, ata:1, drqvld:1, dmasup:1, morsup:1, trnxfr:1, tarsup:1,
583            ocsena:1;
584 #else
585    unchar        :4,    /* unused low nibble */
586           version:4;    /* EATA version, should be 0x1 */
587    unchar  ocsena:1,    /* Overlap Command Support Enabled */
588            tarsup:1,    /* Target Mode Supported */
589            trnxfr:1,    /* Truncate Transfer Cmd NOT Necessary */
590            morsup:1,    /* More Supported */
591            dmasup:1,    /* DMA Supported */
592            drqvld:1,    /* DRQ Index (DRQX) is valid */
593               ata:1,    /* This is an ATA device */
594            haaval:1;    /* Host Adapter Address Valid */
595 #endif
596
597    ushort cp_pad_len;   /* Number of pad bytes after cp_len */
598    unchar host_addr[4]; /* Host Adapter SCSI ID for channels 3, 2, 1, 0 */
599    u_int32_t cp_len;    /* Number of valid bytes in cp */
600    u_int32_t sp_len;    /* Number of valid bytes in sp */
601    ushort queue_size;   /* Max number of cp that can be queued */
602    ushort unused;
603    ushort scatt_size;   /* Max number of entries in scatter/gather table */
604
605 #if defined(__BIG_ENDIAN_BITFIELD)
606    unchar    drqx:2, second:1, irq_tr:1, irq:4;
607    unchar  sync;
608    unchar         :4, res1:1, large_sg:1, forcaddr:1, isaena:1;
609    unchar max_chan:3, max_id:5;
610    unchar   max_lun;
611    unchar     eisa:1, pci:1, idquest:1, m1:1, :4;
612 #else
613    unchar     irq:4,    /* Interrupt Request assigned to this controller */
614            irq_tr:1,    /* 0 for edge triggered, 1 for level triggered */
615            second:1,    /* 1 if this is a secondary (not primary) controller */
616              drqx:2;    /* DRQ Index (0=DMA0, 1=DMA7, 2=DMA6, 3=DMA5) */
617    unchar  sync;        /* 1 if scsi target id 7...0 is running sync scsi */
618
619    /* Structure extension defined in EATA 2.0B */
620    unchar  isaena:1,    /* ISA i/o addressing is disabled/enabled */
621          forcaddr:1,    /* Port address has been forced */
622          large_sg:1,    /* 1 if large SG lists are supported */
623              res1:1,
624                  :4;
625    unchar  max_id:5,    /* Max SCSI target ID number */
626          max_chan:3;    /* Max SCSI channel number on this board */
627
628    /* Structure extension defined in EATA 2.0C */
629    unchar   max_lun;    /* Max SCSI LUN number */
630    unchar        :4,
631                m1:1,    /* This is a PCI with an M1 chip installed */
632           idquest:1,    /* RAIDNUM returned is questionable */
633               pci:1,    /* This board is PCI */
634              eisa:1;    /* This board is EISA */
635 #endif
636
637    unchar   raidnum;    /* Uniquely identifies this HBA in a system */
638    unchar   notused;
639
640    ushort ipad[247];
641    };
642
643 /* Board config structure */
644 struct eata_config {
645    ushort len;          /* Number of bytes following this field */
646
647 #if defined(__BIG_ENDIAN_BITFIELD)
648    unchar     :4, tarena:1, mdpena:1, ocena:1, edis:1;
649 #else
650    unchar edis:1,       /* Disable EATA interface after config command */
651          ocena:1,       /* Overlapped Commands Enabled */
652         mdpena:1,       /* Transfer all Modified Data Pointer Messages */
653         tarena:1,       /* Target Mode Enabled for this controller */
654               :4;
655 #endif
656
657    unchar cpad[511];
658    };
659
660 /* Returned status packet structure */
661 struct mssp {
662
663 #if defined(__BIG_ENDIAN_BITFIELD)
664    unchar            eoc:1, adapter_status:7;
665 #else
666    unchar adapter_status:7,    /* State related to current command */
667                      eoc:1;    /* End Of Command (1 = command completed) */
668 #endif
669
670    unchar target_status;       /* SCSI status received after data transfer */
671    unchar unused[2];
672    u_int32_t inv_res_len;      /* Number of bytes not transferred */
673    u_int32_t cpp_index;        /* Index of address set in cp */
674    char mess[12];
675    };
676
677 struct sg_list {
678    unsigned int address;                /* Segment Address */
679    unsigned int num_bytes;              /* Segment Length */
680    };
681
682 /* MailBox SCSI Command Packet */
683 struct mscp {
684
685 #if defined(__BIG_ENDIAN_BITFIELD)
686    unchar     din:1, dout:1, interp:1, :1, sg:1, reqsen:1, init:1, sreset:1;
687    unchar sense_len;
688    unchar unused[3];
689    unchar        :7, fwnest:1;
690    unchar        :5, hbaci:1, iat:1, phsunit:1;
691    unchar channel:3, target:5;
692    unchar     one:1, dispri:1, luntar:1, lun:5;
693 #else
694    unchar  sreset:1,     /* SCSI Bus Reset Signal should be asserted */
695              init:1,     /* Re-initialize controller and self test */
696            reqsen:1,     /* Transfer Request Sense Data to addr using DMA */
697                sg:1,     /* Use Scatter/Gather */
698                  :1,
699            interp:1,     /* The controller interprets cp, not the target */
700              dout:1,     /* Direction of Transfer is Out (Host to Target) */
701               din:1;     /* Direction of Transfer is In (Target to Host) */
702    unchar sense_len;     /* Request Sense Length */
703    unchar unused[3];
704    unchar  fwnest:1,     /* Send command to a component of an Array Group */
705                  :7;
706    unchar phsunit:1,     /* Send to Target Physical Unit (bypass RAID) */
707               iat:1,     /* Inhibit Address Translation */
708             hbaci:1,     /* Inhibit HBA Caching for this command */
709                  :5;
710    unchar  target:5,     /* SCSI target ID */
711           channel:3;     /* SCSI channel number */
712    unchar     lun:5,     /* SCSI logical unit number */
713            luntar:1,     /* This cp is for Target (not LUN) */
714            dispri:1,     /* Disconnect Privilege granted */
715               one:1;     /* 1 */
716 #endif
717
718    unchar mess[3];       /* Massage to/from Target */
719    unchar cdb[12];       /* Command Descriptor Block */
720    u_int32_t data_len;   /* If sg=0 Data Length, if sg=1 sglist length */
721    u_int32_t cpp_index;  /* Index of address to be returned in sp */
722    u_int32_t data_address; /* If sg=0 Data Address, if sg=1 sglist address */
723    u_int32_t sp_dma_addr;  /* Address where sp is DMA'ed when cp completes */
724    u_int32_t sense_addr; /* Address where Sense Data is DMA'ed on error */
725
726    /* Additional fields begin here. */
727    Scsi_Cmnd *SCpnt;
728
729    /* All the cp structure is zero filled by queuecommand except the
730       following CP_TAIL_SIZE bytes, initialized by detect */
731    dma_addr_t cp_dma_addr; /* dma handle for this cp structure */
732    struct sg_list *sglist; /* pointer to the allocated SG list */
733    };
734
735 #define CP_TAIL_SIZE (sizeof(struct sglist *) + sizeof(dma_addr_t))
736
737 struct hostdata {
738    struct mscp cp[MAX_MAILBOXES];       /* Mailboxes for this board */
739    unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
740    unsigned int last_cp_used;           /* Index of last mailbox used */
741    unsigned int iocount;                /* Total i/o done for this board */
742    int board_number;                    /* Number of this board */
743    char board_name[16];                 /* Name of this board */
744    int in_reset;                        /* True if board is doing a reset */
745    int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
746    int target_redo[MAX_TARGET][MAX_CHANNEL]; /* If TRUE redo i/o on target */
747    unsigned int retries;                /* Number of internal retries */
748    unsigned long last_retried_pid;      /* Pid of last retried command */
749    unsigned char subversion;            /* Bus type, either ISA or EISA/PCI */
750    unsigned char protocol_rev;          /* EATA 2.0 rev., 'A' or 'B' or 'C' */
751    unsigned char is_pci;                /* TRUE is bus type is PCI */
752    struct pci_dev *pdev;                /* pdev for PCI bus, NULL otherwise */
753    struct mssp *sp_cpu_addr;            /* cpu addr for DMA buffer sp */
754    dma_addr_t sp_dma_addr;              /* dma handle for DMA buffer sp */
755    struct mssp sp;                      /* Local copy of sp buffer */
756    };
757
758 static struct Scsi_Host *sh[MAX_BOARDS + 1];
759 static const char *driver_name = "EATA";
760 static char sha[MAX_BOARDS];
761
762 /* Initialize num_boards so that ihdlr can work while detect is in progress */
763 static unsigned int num_boards = MAX_BOARDS;
764
765 static unsigned long io_port[] = {
766
767    /* Space for MAX_INT_PARAM ports usable while loading as a module */
768    SKIP,    SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,
769    SKIP,    SKIP,
770
771    /* First ISA */
772    0x1f0,
773
774    /* Space for MAX_PCI ports possibly reported by PCI_BIOS */
775    SKIP,    SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,
776    SKIP,    SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,
777
778    /* MAX_EISA ports */
779    0x1c88, 0x2c88, 0x3c88, 0x4c88, 0x5c88, 0x6c88, 0x7c88, 0x8c88,
780    0x9c88, 0xac88, 0xbc88, 0xcc88, 0xdc88, 0xec88, 0xfc88,
781
782    /* Other (MAX_ISA - 1) ports */
783    0x170,  0x230,  0x330,
784
785    /* End of list */
786    0x0
787    };
788
789 #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
790 #define BN(board) (HD(board)->board_name)
791
792 /* Device is Big Endian */
793 #define H2DEV(x)   cpu_to_be32(x)
794 #define DEV2H(x)   be32_to_cpu(x)
795 #define H2DEV16(x) cpu_to_be16(x)
796 #define DEV2H16(x) be16_to_cpu(x)
797
798 /* But transfer orientation from the 16 bit data register is Little Endian */
799 #define REG2H(x)   le16_to_cpu(x)
800
801 static void do_interrupt_handler(int, void *, struct pt_regs *);
802 static void flush_dev(Scsi_Device *, unsigned long, unsigned int, unsigned int);
803 static int do_trace = FALSE;
804 static int setup_done = FALSE;
805 static int link_statistics;
806 static int tag_mode = TAG_MIXED;
807 static int ext_tran = FALSE;
808 static int rev_scan = TRUE;
809 static char *boot_options;
810
811 #if defined(CONFIG_SCSI_EATA_TAGGED_QUEUE)
812 static int tagged_comm = TRUE;
813 #else
814 static int tagged_comm = FALSE;
815 #endif
816
817 #if defined(CONFIG_SCSI_EATA_LINKED_COMMANDS)
818 static int linked_comm = TRUE;
819 #else
820 static int linked_comm = FALSE;
821 #endif
822
823 #if defined(CONFIG_SCSI_EATA_MAX_TAGS)
824 static int max_queue_depth = CONFIG_SCSI_EATA_MAX_TAGS;
825 #else
826 static int max_queue_depth = MAX_CMD_PER_LUN;
827 #endif
828
829 #if defined(CONFIG_ISA)
830 static int isa_probe = TRUE;
831 #else
832 static int isa_probe = FALSE;
833 #endif
834
835 #if defined(CONFIG_EISA)
836 static int eisa_probe = TRUE;
837 #else
838 static int eisa_probe = FALSE;
839 #endif
840
841 #if defined(CONFIG_PCI)
842 static int pci_probe = TRUE;
843 #else
844 static int pci_probe = FALSE;
845 #endif
846
847 static void select_queue_depths(struct Scsi_Host *host, Scsi_Device *devlist) {
848    Scsi_Device *dev;
849    int j, ntag = 0, nuntag = 0, tqd, utqd;
850
851    j = ((struct hostdata *) host->hostdata)->board_number;
852
853    for(dev = devlist; dev; dev = dev->next) {
854
855       if (dev->host != host) continue;
856
857       if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
858          ntag++;
859       else
860          nuntag++;
861       }
862
863    utqd = MAX_CMD_PER_LUN;
864
865    tqd = (host->can_queue - utqd * nuntag) / (ntag ? ntag : 1);
866
867    if (tqd > max_queue_depth) tqd = max_queue_depth;
868
869    if (tqd < MAX_CMD_PER_LUN) tqd = MAX_CMD_PER_LUN;
870
871    for(dev = devlist; dev; dev = dev->next) {
872       char *tag_suffix = "", *link_suffix = "";
873
874       if (dev->host != host) continue;
875
876       if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
877          dev->queue_depth = tqd;
878       else
879          dev->queue_depth = utqd;
880
881       if (TLDEV(dev->type)) {
882          if (linked_comm && dev->queue_depth > 2)
883             link_suffix = ", sorted";
884          else
885             link_suffix = ", unsorted";
886          }
887
888       if (tagged_comm && dev->tagged_supported && TLDEV(dev->type)) {
889          dev->tagged_queue = 1;
890          dev->current_tag = 1;
891          }
892
893       if (dev->tagged_supported && TLDEV(dev->type) && dev->tagged_queue)
894          tag_suffix = ", soft-tagged";
895       else if (dev->tagged_supported && TLDEV(dev->type))
896          tag_suffix = ", tagged";
897
898       printk("%s: scsi%d, channel %d, id %d, lun %d, cmds/lun %d%s%s.\n",
899              BN(j), host->host_no, dev->channel, dev->id, dev->lun,
900              dev->queue_depth, link_suffix, tag_suffix);
901       }
902
903    return;
904 }
905
906 static inline int wait_on_busy(unsigned long iobase, unsigned int loop) {
907
908    while (inb(iobase + REG_AUX_STATUS) & ABSY_ASSERTED) {
909       udelay(1L);
910       if (--loop == 0) return TRUE;
911       }
912
913    return FALSE;
914 }
915
916 static inline int do_dma(unsigned long iobase, unsigned long addr, unchar cmd) {
917    unsigned char *byaddr;
918    unsigned long devaddr;
919
920    if (wait_on_busy(iobase, (addr ? MAXLOOP * 100 : MAXLOOP))) return TRUE;
921
922    if (addr) {
923       devaddr = H2DEV(addr);
924       byaddr = (unsigned char *) &devaddr;
925       outb(byaddr[3], iobase + REG_LOW);
926       outb(byaddr[2], iobase + REG_LM);
927       outb(byaddr[1], iobase + REG_MID);
928       outb(byaddr[0], iobase + REG_MSB);
929       }
930
931    outb(cmd, iobase + REG_CMD);
932    return FALSE;
933 }
934
935 static inline int read_pio(unsigned long iobase, ushort *start, ushort *end) {
936    unsigned int loop = MAXLOOP;
937    ushort *p;
938
939    for (p = start; p <= end; p++) {
940
941       while (!(inb(iobase + REG_STATUS) & DRQ_ASSERTED)) {
942          udelay(1L);
943          if (--loop == 0) return TRUE;
944          }
945
946       loop = MAXLOOP;
947       *p = REG2H(inw(iobase));
948       }
949
950    return FALSE;
951 }
952
953 static inline struct pci_dev *get_pci_dev(unsigned long port_base) {
954
955 #if defined(CONFIG_PCI)
956
957    unsigned int addr;
958    struct pci_dev *dev = NULL;
959
960    if (!pci_present()) return NULL;
961
962    while((dev = pci_find_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
963       addr = pci_resource_start (dev, 0);
964
965 #if defined(DEBUG_PCI_DETECT)
966       printk("%s: get_pci_dev, bus %d, devfn 0x%x, addr 0x%x.\n",
967              driver_name, dev->bus->number, dev->devfn, addr);
968 #endif
969
970       if (addr + PCI_BASE_ADDRESS_0 == port_base) return dev;
971       }
972
973 #endif /* end CONFIG_PCI */
974
975    return NULL;
976 }
977
978 static void enable_pci_ports(void) {
979
980 #if defined(CONFIG_PCI)
981
982    struct pci_dev *dev = NULL;
983
984    if (!pci_present()) return;
985
986    while((dev = pci_find_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
987
988 #if defined(DEBUG_PCI_DETECT)
989       printk("%s: enable_pci_ports, bus %d, devfn 0x%x.\n",
990              driver_name, dev->bus->number, dev->devfn);
991 #endif
992
993       if (pci_enable_device (dev))
994          printk("%s: warning, pci_enable_device failed, bus %d devfn 0x%x.\n",
995                 driver_name, dev->bus->number, dev->devfn);
996       }
997
998 #endif /* end CONFIG_PCI */
999 }
1000
1001 static inline int port_detect \
1002       (unsigned long port_base, unsigned int j, Scsi_Host_Template *tpnt) {
1003    unsigned char irq, dma_channel, subversion, i, is_pci = FALSE;
1004    unsigned char protocol_rev;
1005    struct eata_info info;
1006    char *bus_type, dma_name[16], tag_type;
1007    struct pci_dev *pdev;
1008
1009    /* Allowed DMA channels for ISA (0 indicates reserved) */
1010    unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
1011
1012    char name[16];
1013
1014    sprintf(name, "%s%d", driver_name, j);
1015
1016    if(!request_region(port_base, REGION_SIZE, driver_name)) {
1017 #if defined(DEBUG_DETECT)
1018       printk("%s: address 0x%03lx in use, skipping probe.\n", name, port_base);
1019 #endif
1020       return FALSE;
1021       }
1022
1023    if (do_dma(port_base, 0, READ_CONFIG_PIO)) {
1024 #if defined(DEBUG_DETECT)
1025       printk("%s: detect, do_dma failed at 0x%03lx.\n", name, port_base);
1026 #endif
1027       release_region(port_base, REGION_SIZE);
1028       return FALSE;
1029       }
1030
1031    /* Read the info structure */
1032    if (read_pio(port_base, (ushort *)&info, (ushort *)&info.ipad[0])) {
1033 #if defined(DEBUG_DETECT)
1034       printk("%s: detect, read_pio failed at 0x%03lx.\n", name, port_base);
1035 #endif
1036       release_region(port_base, REGION_SIZE);
1037       return FALSE;
1038       }
1039
1040    info.data_len = DEV2H(info.data_len);
1041    info.sign = DEV2H(info.sign);
1042    info.cp_pad_len = DEV2H16(info.cp_pad_len);
1043    info.cp_len = DEV2H(info.cp_len);
1044    info.sp_len = DEV2H(info.sp_len);
1045    info.scatt_size = DEV2H16(info.scatt_size);
1046    info.queue_size = DEV2H16(info.queue_size);
1047
1048    /* Check the controller "EATA" signature */
1049    if (info.sign != EATA_SIG_BE) {
1050 #if defined(DEBUG_DETECT)
1051       printk("%s: signature 0x%04x discarded.\n", name, info.sign);
1052 #endif
1053       release_region(port_base, REGION_SIZE);
1054       return FALSE;
1055       }
1056
1057    if (info.data_len < EATA_2_0A_SIZE) {
1058       printk("%s: config structure size (%d bytes) too short, detaching.\n",
1059              name, info.data_len);
1060       release_region(port_base, REGION_SIZE);
1061       return FALSE;
1062       }
1063    else if (info.data_len == EATA_2_0A_SIZE)
1064       protocol_rev = 'A';
1065    else if (info.data_len == EATA_2_0B_SIZE)
1066       protocol_rev = 'B';
1067    else
1068       protocol_rev = 'C';
1069
1070    if (protocol_rev != 'A' && info.forcaddr) {
1071       printk("%s: warning, port address has been forced.\n", name);
1072       bus_type = "PCI";
1073       is_pci = TRUE;
1074       subversion = ESA;
1075       }
1076    else if (port_base > MAX_EISA_ADDR || (protocol_rev == 'C' && info.pci)) {
1077       bus_type = "PCI";
1078       is_pci = TRUE;
1079       subversion = ESA;
1080       }
1081    else if (port_base >= MIN_EISA_ADDR || (protocol_rev == 'C' && info.eisa)) {
1082       bus_type = "EISA";
1083       subversion = ESA;
1084       }
1085    else if (protocol_rev == 'C' && !info.eisa && !info.pci) {
1086       bus_type = "ISA";
1087       subversion = ISA;
1088       }
1089    else if (port_base > MAX_ISA_ADDR) {
1090       bus_type = "PCI";
1091       is_pci = TRUE;
1092       subversion = ESA;
1093       }
1094    else {
1095       bus_type = "ISA";
1096       subversion = ISA;
1097       }
1098
1099    if (!info.haaval || info.ata) {
1100       printk("%s: address 0x%03lx, unusable %s board (%d%d), detaching.\n",
1101              name, port_base, bus_type, info.haaval, info.ata);
1102       release_region(port_base, REGION_SIZE);
1103       return FALSE;
1104       }
1105
1106    if (info.drqvld) {
1107
1108       if (subversion ==  ESA)
1109          printk("%s: warning, weird %s board using DMA.\n", name, bus_type);
1110
1111       subversion = ISA;
1112       dma_channel = dma_channel_table[3 - info.drqx];
1113       }
1114    else {
1115
1116       if (subversion ==  ISA)
1117          printk("%s: warning, weird %s board not using DMA.\n", name, bus_type);
1118
1119       subversion = ESA;
1120       dma_channel = NO_DMA;
1121       }
1122
1123    if (!info.dmasup)
1124       printk("%s: warning, DMA protocol support not asserted.\n", name);
1125
1126    irq = info.irq;
1127
1128    if (subversion == ESA && !info.irq_tr)
1129       printk("%s: warning, LEVEL triggering is suggested for IRQ %u.\n",
1130              name, irq);
1131
1132    if (is_pci) {
1133       pdev = get_pci_dev(port_base);
1134       if (!pdev)
1135          printk("%s: warning, failed to get pci_dev structure.\n", name);
1136       }
1137    else
1138       pdev = NULL;
1139
1140    if (pdev && (irq != pdev->irq)) {
1141       printk("%s: IRQ %u mapped to IO-APIC IRQ %u.\n", name, irq, pdev->irq);
1142       irq = pdev->irq;
1143       }
1144
1145    /* Board detected, allocate its IRQ */
1146    if (request_irq(irq, do_interrupt_handler,
1147              SA_INTERRUPT | ((subversion == ESA) ? SA_SHIRQ : 0),
1148              driver_name, (void *) &sha[j])) {
1149       printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
1150       release_region(port_base, REGION_SIZE);
1151       return FALSE;
1152       }
1153
1154    if (subversion == ISA && request_dma(dma_channel, driver_name)) {
1155       printk("%s: unable to allocate DMA channel %u, detaching.\n",
1156              name, dma_channel);
1157       free_irq(irq, &sha[j]);
1158       release_region(port_base, REGION_SIZE);
1159       return FALSE;
1160       }
1161
1162 #if defined(FORCE_CONFIG)
1163    {
1164    struct eata_config *cf;
1165    dma_addr_t cf_dma_addr;
1166
1167    cf = pci_alloc_consistent(pdev, sizeof(struct eata_config), &cf_dma_addr);
1168
1169    if (!cf) {
1170       printk("%s: config, pci_alloc_consistent failed, detaching.\n", name);
1171       release_region(port_base, REGION_SIZE);
1172       return FALSE;
1173       }
1174
1175    /* Set board configuration */
1176    memset((char *)cf, 0, sizeof(struct eata_config));
1177    cf->len = (ushort) H2DEV16((ushort)510);
1178    cf->ocena = TRUE;
1179
1180    if (do_dma(port_base, cf_dma_addr, SET_CONFIG_DMA)) {
1181       printk("%s: busy timeout sending configuration, detaching.\n", name);
1182       pci_free_consistent(pdev, sizeof(struct eata_config), cf, cf_dma_addr);
1183       release_region(port_base, REGION_SIZE);
1184       return FALSE;
1185       }
1186
1187    }
1188 #endif
1189
1190    sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
1191
1192    if (sh[j] == NULL) {
1193       printk("%s: unable to register host, detaching.\n", name);
1194
1195       free_irq(irq, &sha[j]);
1196
1197       if (subversion == ISA) free_dma(dma_channel);
1198
1199       release_region(port_base, REGION_SIZE);
1200       return FALSE;
1201       }
1202
1203    sh[j]->io_port = port_base;
1204    sh[j]->unique_id = port_base;
1205    sh[j]->n_io_port = REGION_SIZE;
1206    sh[j]->dma_channel = dma_channel;
1207    sh[j]->irq = irq;
1208    sh[j]->sg_tablesize = (ushort) info.scatt_size;
1209    sh[j]->this_id = (ushort) info.host_addr[3];
1210    sh[j]->can_queue = (ushort) info.queue_size;
1211    sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
1212    sh[j]->select_queue_depths = select_queue_depths;
1213    memset(HD(j), 0, sizeof(struct hostdata));
1214    HD(j)->subversion = subversion;
1215    HD(j)->protocol_rev = protocol_rev;
1216    HD(j)->is_pci = is_pci;
1217    HD(j)->pdev = pdev;
1218    HD(j)->board_number = j;
1219
1220    if (HD(j)->subversion == ESA)
1221       sh[j]->unchecked_isa_dma = FALSE;
1222    else {
1223       unsigned long flags;
1224       scsi_register_blocked_host(sh[j]);
1225       sh[j]->unchecked_isa_dma = TRUE;
1226
1227       flags=claim_dma_lock();
1228       disable_dma(dma_channel);
1229       clear_dma_ff(dma_channel);
1230       set_dma_mode(dma_channel, DMA_MODE_CASCADE);
1231       enable_dma(dma_channel);
1232       release_dma_lock(flags);
1233
1234       }
1235
1236    strcpy(BN(j), name);
1237
1238    /* DPT PM2012 does not allow to detect sg_tablesize correctly */
1239    if (sh[j]->sg_tablesize > MAX_SGLIST || sh[j]->sg_tablesize < 2) {
1240       printk("%s: detect, wrong n. of SG lists %d, fixed.\n",
1241              BN(j), sh[j]->sg_tablesize);
1242       sh[j]->sg_tablesize = MAX_SGLIST;
1243       }
1244
1245    /* DPT PM2012 does not allow to detect can_queue correctly */
1246    if (sh[j]->can_queue > MAX_MAILBOXES || sh[j]->can_queue  < 2) {
1247       printk("%s: detect, wrong n. of mbox %d, fixed.\n",
1248              BN(j), sh[j]->can_queue);
1249       sh[j]->can_queue = MAX_MAILBOXES;
1250       }
1251
1252    if (protocol_rev != 'A') {
1253
1254       if (info.max_chan > 0 && info.max_chan < MAX_CHANNEL)
1255          sh[j]->max_channel = info.max_chan;
1256
1257       if (info.max_id > 7 && info.max_id < MAX_TARGET)
1258          sh[j]->max_id = info.max_id + 1;
1259
1260       if (info.large_sg && sh[j]->sg_tablesize == MAX_SGLIST)
1261          sh[j]->sg_tablesize = MAX_LARGE_SGLIST;
1262       }
1263
1264    if (protocol_rev == 'C') {
1265
1266       if (info.max_lun > 7 && info.max_lun < MAX_LUN)
1267          sh[j]->max_lun = info.max_lun + 1;
1268       }
1269
1270    if (dma_channel == NO_DMA) sprintf(dma_name, "%s", "BMST");
1271    else                       sprintf(dma_name, "DMA %u", dma_channel);
1272
1273    for (i = 0; i < sh[j]->can_queue; i++)
1274       HD(j)->cp[i].cp_dma_addr = pci_map_single(HD(j)->pdev,
1275             &HD(j)->cp[i], sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
1276
1277    for (i = 0; i < sh[j]->can_queue; i++)
1278       if (! ((&HD(j)->cp[i])->sglist = kmalloc(
1279             sh[j]->sg_tablesize * sizeof(struct sg_list),
1280             (sh[j]->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC))) {
1281          printk("%s: kmalloc SGlist failed, mbox %d, detaching.\n", BN(j), i);
1282          eata2x_release(sh[j]);
1283          return FALSE;
1284          }
1285
1286    if (! (HD(j)->sp_cpu_addr = pci_alloc_consistent(HD(j)->pdev,
1287          sizeof(struct mssp), &HD(j)->sp_dma_addr))) {
1288       printk("%s: pci_alloc_consistent failed, detaching.\n", BN(j));
1289       eata2x_release(sh[j]);
1290       return FALSE;
1291       }
1292
1293    if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
1294        max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
1295
1296    if (max_queue_depth < MAX_CMD_PER_LUN) max_queue_depth = MAX_CMD_PER_LUN;
1297
1298    if (tagged_comm) {
1299       if      (tag_mode == TAG_SIMPLE)  tag_type = '1';
1300       else if (tag_mode == TAG_HEAD)    tag_type = '2';
1301       else if (tag_mode == TAG_ORDERED) tag_type = '3';
1302       else                              tag_type = 'y';
1303       }
1304    else                                 tag_type = 'n';
1305
1306    if (j == 0) {
1307       printk("EATA/DMA 2.0x: Copyright (C) 1994-2002 Dario Ballabio.\n");
1308       printk("%s config options -> tc:%c, lc:%c, mq:%d, rs:%c, et:%c, "\
1309              "ip:%c, ep:%c, pp:%c.\n", driver_name, tag_type,
1310              YESNO(linked_comm), max_queue_depth, YESNO(rev_scan),
1311              YESNO(ext_tran), YESNO(isa_probe), YESNO(eisa_probe),
1312              YESNO(pci_probe));
1313       }
1314
1315    printk("%s: 2.0%c, %s 0x%03lx, IRQ %u, %s, SG %d, MB %d.\n",
1316           BN(j), HD(j)->protocol_rev, bus_type, (unsigned long)sh[j]->io_port,
1317           sh[j]->irq, dma_name, sh[j]->sg_tablesize, sh[j]->can_queue);
1318
1319    if (sh[j]->max_id > 8 || sh[j]->max_lun > 8)
1320       printk("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
1321              BN(j), sh[j]->max_id, sh[j]->max_lun);
1322
1323    for (i = 0; i <= sh[j]->max_channel; i++)
1324       printk("%s: SCSI channel %u enabled, host target ID %d.\n",
1325              BN(j), i, info.host_addr[3 - i]);
1326
1327 #if defined(DEBUG_DETECT)
1328    printk("%s: Vers. 0x%x, ocs %u, tar %u, trnxfr %u, more %u, SYNC 0x%x, "\
1329           "sec. %u, infol %d, cpl %d spl %d.\n", name, info.version,
1330           info.ocsena, info.tarsup, info.trnxfr, info.morsup, info.sync,
1331           info.second, info.data_len, info.cp_len,
1332           info.sp_len);
1333
1334    if (protocol_rev == 'B' || protocol_rev == 'C')
1335       printk("%s: isaena %u, forcaddr %u, max_id %u, max_chan %u, "\
1336              "large_sg %u, res1 %u.\n", name, info.isaena, info.forcaddr,
1337              info.max_id, info.max_chan, info.large_sg, info.res1);
1338
1339    if (protocol_rev == 'C')
1340       printk("%s: max_lun %u, m1 %u, idquest %u, pci %u, eisa %u, "\
1341              "raidnum %u.\n", name, info.max_lun, info.m1, info.idquest,
1342              info.pci, info.eisa, info.raidnum);
1343 #endif
1344
1345    if (HD(j)->pdev) {
1346       pci_set_master(HD(j)->pdev);
1347       if (pci_set_dma_mask(HD(j)->pdev, 0xffffffff))
1348          printk("%s: warning, pci_set_dma_mask failed.\n", BN(j));
1349       }
1350
1351    return TRUE;
1352 }
1353
1354 static void internal_setup(char *str, int *ints) {
1355    int i, argc = ints[0];
1356    char *cur = str, *pc;
1357
1358    if (argc > 0) {
1359
1360       if (argc > MAX_INT_PARAM) argc = MAX_INT_PARAM;
1361
1362       for (i = 0; i < argc; i++) io_port[i] = ints[i + 1];
1363
1364       io_port[i] = 0;
1365       setup_done = TRUE;
1366       }
1367
1368    while (cur && (pc = strchr(cur, ':'))) {
1369       int val = 0, c = *++pc;
1370
1371       if (c == 'n' || c == 'N') val = FALSE;
1372       else if (c == 'y' || c == 'Y') val = TRUE;
1373       else val = (int) simple_strtoul(pc, NULL, 0);
1374
1375       if (!strncmp(cur, "lc:", 3)) linked_comm = val;
1376       else if (!strncmp(cur, "tc:", 3)) tagged_comm = val;
1377       else if (!strncmp(cur, "tm:", 3)) tag_mode = val;
1378       else if (!strncmp(cur, "mq:", 3))  max_queue_depth = val;
1379       else if (!strncmp(cur, "ls:", 3))  link_statistics = val;
1380       else if (!strncmp(cur, "et:", 3))  ext_tran = val;
1381       else if (!strncmp(cur, "rs:", 3))  rev_scan = val;
1382       else if (!strncmp(cur, "ip:", 3))  isa_probe = val;
1383       else if (!strncmp(cur, "ep:", 3))  eisa_probe = val;
1384       else if (!strncmp(cur, "pp:", 3))  pci_probe = val;
1385
1386       if ((cur = strchr(cur, ','))) ++cur;
1387       }
1388
1389    return;
1390 }
1391
1392 static int option_setup(char *str) {
1393    int ints[MAX_INT_PARAM];
1394    char *cur = str;
1395    int i = 1;
1396
1397    while (cur && isdigit(*cur) && i <= MAX_INT_PARAM) {
1398       ints[i++] = simple_strtoul(cur, NULL, 0);
1399
1400       if ((cur = strchr(cur, ',')) != NULL) cur++;
1401    }
1402
1403    ints[0] = i - 1;
1404    internal_setup(cur, ints);
1405    return 1;
1406 }
1407
1408 static void add_pci_ports(void) {
1409
1410 #if defined(CONFIG_PCI)
1411
1412    unsigned int addr, k;
1413
1414    struct pci_dev *dev = NULL;
1415
1416    if (!pci_present()) return;
1417
1418    for (k = 0; k < MAX_PCI; k++) {
1419
1420       if (!(dev = pci_find_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) break;
1421
1422       if (pci_enable_device (dev)) {
1423
1424 #if defined(DEBUG_PCI_DETECT)
1425          printk("%s: detect, bus %d, devfn 0x%x, pci_enable_device failed.\n",
1426                 driver_name, dev->bus->number, dev->devfn);
1427 #endif
1428
1429          continue;
1430          }
1431
1432       addr = pci_resource_start (dev, 0);
1433
1434 #if defined(DEBUG_PCI_DETECT)
1435       printk("%s: detect, seq. %d, bus %d, devfn 0x%x, addr 0x%x.\n",
1436              driver_name, k, dev->bus->number, dev->devfn, addr);
1437 #endif
1438
1439       /* Order addresses according to rev_scan value */
1440       io_port[MAX_INT_PARAM + (rev_scan ? (MAX_PCI - k) : (1 + k))] =
1441              addr + PCI_BASE_ADDRESS_0;
1442       }
1443
1444 #endif /* end CONFIG_PCI */
1445
1446    return;
1447 }
1448
1449 int eata2x_detect(Scsi_Host_Template *tpnt) {
1450    unsigned int j = 0, k;
1451
1452    tpnt->proc_name = "eata2x";
1453
1454    if(boot_options) option_setup(boot_options);
1455
1456 #if defined(MODULE)
1457    /* io_port could have been modified when loading as a module */
1458    if(io_port[0] != SKIP) {
1459       setup_done = TRUE;
1460       io_port[MAX_INT_PARAM] = 0;
1461       }
1462 #endif
1463
1464    for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
1465
1466    for (k = MAX_INT_PARAM; io_port[k]; k++)
1467       if (io_port[k] == SKIP) continue;
1468       else if (io_port[k] <= MAX_ISA_ADDR) {
1469          if (!isa_probe) io_port[k] = SKIP;
1470          }
1471       else if (io_port[k] >= MIN_EISA_ADDR && io_port[k] <= MAX_EISA_ADDR) {
1472          if (!eisa_probe) io_port[k] = SKIP;
1473          }
1474
1475    if (pci_probe) {
1476       if (!setup_done) add_pci_ports();
1477       else          enable_pci_ports();
1478       }
1479
1480    for (k = 0; io_port[k]; k++) {
1481
1482       if (io_port[k] == SKIP) continue;
1483
1484       if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt)) j++;
1485       }
1486
1487    num_boards = j;
1488    return j;
1489 }
1490
1491 static inline void map_dma(unsigned int i, unsigned int j) {
1492    unsigned int k, count, pci_dir;
1493    struct scatterlist *sgpnt;
1494    struct mscp *cpp;
1495    Scsi_Cmnd *SCpnt;
1496
1497    cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1498    pci_dir = scsi_to_pci_dma_dir(SCpnt->sc_data_direction);
1499
1500    if (SCpnt->sense_buffer)
1501       cpp->sense_addr = H2DEV(pci_map_single(HD(j)->pdev, SCpnt->sense_buffer,
1502                            sizeof SCpnt->sense_buffer, PCI_DMA_FROMDEVICE));
1503
1504    cpp->sense_len = sizeof SCpnt->sense_buffer;
1505
1506    if (!SCpnt->use_sg) {
1507
1508       /* If we get here with PCI_DMA_NONE, pci_map_single triggers a BUG() */
1509       if (!SCpnt->request_bufflen) pci_dir = PCI_DMA_BIDIRECTIONAL;
1510
1511       if (SCpnt->request_buffer)
1512          cpp->data_address = H2DEV(pci_map_single(HD(j)->pdev,
1513                   SCpnt->request_buffer, SCpnt->request_bufflen, pci_dir));
1514
1515       cpp->data_len = H2DEV(SCpnt->request_bufflen);
1516       return;
1517       }
1518
1519    sgpnt = (struct scatterlist *) SCpnt->request_buffer;
1520    count = pci_map_sg(HD(j)->pdev, sgpnt, SCpnt->use_sg, pci_dir);
1521
1522    for (k = 0; k < count; k++) {
1523       cpp->sglist[k].address = H2DEV(sg_dma_address(&sgpnt[k]));
1524       cpp->sglist[k].num_bytes = H2DEV(sg_dma_len(&sgpnt[k]));
1525       }
1526
1527    cpp->sg = TRUE;
1528    cpp->data_address = H2DEV(pci_map_single(HD(j)->pdev, cpp->sglist,
1529                              SCpnt->use_sg * sizeof(struct sg_list), pci_dir));
1530    cpp->data_len = H2DEV((SCpnt->use_sg * sizeof(struct sg_list)));
1531 }
1532
1533 static void unmap_dma(unsigned int i, unsigned int j) {
1534    unsigned int pci_dir;
1535    struct mscp *cpp;
1536    Scsi_Cmnd *SCpnt;
1537
1538    cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1539    pci_dir = scsi_to_pci_dma_dir(SCpnt->sc_data_direction);
1540
1541    if (DEV2H(cpp->sense_addr))
1542       pci_unmap_single(HD(j)->pdev, DEV2H(cpp->sense_addr),
1543                        DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
1544
1545    if (SCpnt->use_sg)
1546       pci_unmap_sg(HD(j)->pdev, SCpnt->request_buffer, SCpnt->use_sg, pci_dir);
1547
1548    if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
1549
1550    if (DEV2H(cpp->data_address))
1551       pci_unmap_single(HD(j)->pdev, DEV2H(cpp->data_address),
1552                        DEV2H(cpp->data_len), pci_dir);
1553 }
1554
1555 static void sync_dma(unsigned int i, unsigned int j) {
1556    unsigned int pci_dir;
1557    struct mscp *cpp;
1558    Scsi_Cmnd *SCpnt;
1559
1560    cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1561    pci_dir = scsi_to_pci_dma_dir(SCpnt->sc_data_direction);
1562
1563    if (DEV2H(cpp->sense_addr))
1564       pci_dma_sync_single(HD(j)->pdev, DEV2H(cpp->sense_addr),
1565                           DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
1566
1567    if (SCpnt->use_sg)
1568       pci_dma_sync_sg(HD(j)->pdev, SCpnt->request_buffer,
1569                          SCpnt->use_sg, pci_dir);
1570
1571    if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
1572
1573    if (DEV2H(cpp->data_address))
1574       pci_dma_sync_single(HD(j)->pdev, DEV2H(cpp->data_address),
1575                        DEV2H(cpp->data_len), pci_dir);
1576 }
1577
1578 static inline void scsi_to_dev_dir(unsigned int i, unsigned int j) {
1579    unsigned int k;
1580
1581    static const unsigned char data_out_cmds[] = {
1582       0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
1583       0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
1584       0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b, 0x5d
1585       };
1586
1587    static const unsigned char data_none_cmds[] = {
1588       0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
1589       0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
1590       0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5, 0x00
1591       };
1592
1593    struct mscp *cpp;
1594    Scsi_Cmnd *SCpnt;
1595
1596    cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1597
1598    if (SCpnt->sc_data_direction == SCSI_DATA_READ) {
1599       cpp->din  = TRUE;
1600       cpp->dout = FALSE;
1601       return;
1602       }
1603    else if (SCpnt->sc_data_direction == SCSI_DATA_WRITE) {
1604       cpp->din  = FALSE;
1605       cpp->dout = TRUE;
1606       return;
1607       }
1608    else if (SCpnt->sc_data_direction == SCSI_DATA_NONE) {
1609       cpp->din  = FALSE;
1610       cpp->dout = FALSE;
1611       return;
1612       }
1613
1614    if (SCpnt->sc_data_direction != SCSI_DATA_UNKNOWN)
1615       panic("%s: qcomm, invalid SCpnt->sc_data_direction.\n", BN(j));
1616
1617    for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
1618       if (SCpnt->cmnd[0] == data_out_cmds[k]) {
1619          cpp->dout = TRUE;
1620          break;
1621          }
1622
1623    if ((cpp->din = !cpp->dout))
1624       for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
1625          if (SCpnt->cmnd[0] == data_none_cmds[k]) {
1626             cpp->din = FALSE;
1627             break;
1628             }
1629
1630 }
1631
1632 static inline int do_qcomm(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
1633    unsigned int i, j, k;
1634    struct mscp *cpp;
1635
1636    /* j is the board number */
1637    j = ((struct hostdata *) SCpnt->host->hostdata)->board_number;
1638
1639    if (SCpnt->host_scribble)
1640       panic("%s: qcomm, pid %ld, SCpnt %p already active.\n",
1641             BN(j), SCpnt->pid, SCpnt);
1642
1643    /* i is the mailbox number, look for the first free mailbox
1644       starting from last_cp_used */
1645    i = HD(j)->last_cp_used + 1;
1646
1647    for (k = 0; k < sh[j]->can_queue; k++, i++) {
1648
1649       if (i >= sh[j]->can_queue) i = 0;
1650
1651       if (HD(j)->cp_stat[i] == FREE) {
1652          HD(j)->last_cp_used = i;
1653          break;
1654          }
1655       }
1656
1657    if (k == sh[j]->can_queue) {
1658       printk("%s: qcomm, no free mailbox.\n", BN(j));
1659       return 1;
1660       }
1661
1662    /* Set pointer to control packet structure */
1663    cpp = &HD(j)->cp[i];
1664
1665    memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
1666
1667    /* Set pointer to status packet structure, Big Endian format */
1668    cpp->sp_dma_addr = H2DEV(HD(j)->sp_dma_addr);
1669
1670    SCpnt->scsi_done = done;
1671    cpp->cpp_index = i;
1672    SCpnt->host_scribble = (unsigned char *) &cpp->cpp_index;
1673
1674    if (do_trace) printk("%s: qcomm, mbox %d, target %d.%d:%d, pid %ld.\n",
1675                         BN(j), i, SCpnt->channel, SCpnt->target,
1676                         SCpnt->lun, SCpnt->pid);
1677
1678    cpp->reqsen = TRUE;
1679    cpp->dispri = TRUE;
1680 #if 0
1681    if (SCpnt->device->type == TYPE_TAPE) cpp->hbaci = TRUE;
1682 #endif
1683    cpp->one = TRUE;
1684    cpp->channel = SCpnt->channel;
1685    cpp->target = SCpnt->target;
1686    cpp->lun = SCpnt->lun;
1687    cpp->SCpnt = SCpnt;
1688    memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
1689
1690    /* Use data transfer direction SCpnt->sc_data_direction */
1691    scsi_to_dev_dir(i, j);
1692
1693    /* Map DMA buffers and SG list */
1694    map_dma(i, j);
1695
1696    if (SCpnt->device->tagged_queue) {
1697
1698       if (HD(j)->target_redo[SCpnt->target][SCpnt->channel] ||
1699             HD(j)->target_to[SCpnt->target][SCpnt->channel])
1700          cpp->mess[0] = ORDERED_QUEUE_TAG;
1701       else if (tag_mode == TAG_SIMPLE)  cpp->mess[0] = SIMPLE_QUEUE_TAG;
1702       else if (tag_mode == TAG_HEAD)    cpp->mess[0] = HEAD_OF_QUEUE_TAG;
1703       else if (tag_mode == TAG_ORDERED) cpp->mess[0] = ORDERED_QUEUE_TAG;
1704       else if (SCpnt->device->current_tag == 0)
1705          cpp->mess[0] = ORDERED_QUEUE_TAG;
1706       else if (SCpnt->device->current_tag == 1)
1707          cpp->mess[0] = HEAD_OF_QUEUE_TAG;
1708       else
1709          cpp->mess[0] = SIMPLE_QUEUE_TAG;
1710
1711       cpp->mess[1] = SCpnt->device->current_tag++;
1712       }
1713
1714    if (linked_comm && SCpnt->device->queue_depth > 2
1715                                      && TLDEV(SCpnt->device->type)) {
1716       HD(j)->cp_stat[i] = READY;
1717       flush_dev(SCpnt->device, SCpnt->request.sector, j, FALSE);
1718       return 0;
1719       }
1720
1721    /* Send control packet to the board */
1722    if (do_dma(sh[j]->io_port, cpp->cp_dma_addr, SEND_CP_DMA)) {
1723       unmap_dma(i, j);
1724       SCpnt->host_scribble = NULL;
1725       printk("%s: qcomm, target %d.%d:%d, pid %ld, adapter busy.\n",
1726              BN(j), SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid);
1727       return 1;
1728       }
1729
1730    HD(j)->cp_stat[i] = IN_USE;
1731    return 0;
1732 }
1733
1734 int eata2x_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
1735    int rtn;
1736
1737    rtn = do_qcomm(SCpnt, done);
1738    return rtn;
1739 }
1740
1741 static inline int do_abort(Scsi_Cmnd *SCarg) {
1742    unsigned int i, j;
1743
1744    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1745
1746    if (SCarg->host_scribble == NULL) {
1747       printk("%s: abort, target %d.%d:%d, pid %ld inactive.\n",
1748              BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1749       return SUCCESS;
1750       }
1751
1752    i = *(unsigned int *)SCarg->host_scribble;
1753    printk("%s: abort, mbox %d, target %d.%d:%d, pid %ld.\n",
1754           BN(j), i, SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1755
1756    if (i >= sh[j]->can_queue)
1757       panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
1758
1759    if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1760       printk("%s: abort, timeout error.\n", BN(j));
1761       return FAILED;
1762       }
1763
1764    if (HD(j)->cp_stat[i] == FREE) {
1765       printk("%s: abort, mbox %d is free.\n", BN(j), i);
1766       return SUCCESS;
1767       }
1768
1769    if (HD(j)->cp_stat[i] == IN_USE) {
1770       printk("%s: abort, mbox %d is in use.\n", BN(j), i);
1771
1772       if (SCarg != HD(j)->cp[i].SCpnt)
1773          panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1774                BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
1775
1776       if (inb(sh[j]->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)
1777          printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
1778
1779       if (SCarg->eh_state == SCSI_STATE_TIMEOUT) {
1780          unmap_dma(i, j);
1781          SCarg->host_scribble = NULL;
1782          HD(j)->cp_stat[i] = FREE;
1783          printk("%s, abort, mbox %d, eh_state timeout, pid %ld.\n",
1784                 BN(j), i, SCarg->pid);
1785          return SUCCESS;
1786          }
1787
1788       return FAILED;
1789       }
1790
1791    if (HD(j)->cp_stat[i] == IN_RESET) {
1792       printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
1793       return FAILED;
1794       }
1795
1796    if (HD(j)->cp_stat[i] == LOCKED) {
1797       printk("%s: abort, mbox %d is locked.\n", BN(j), i);
1798       return SUCCESS;
1799       }
1800
1801    if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1802       unmap_dma(i, j);
1803       SCarg->result = DID_ABORT << 16;
1804       SCarg->host_scribble = NULL;
1805       HD(j)->cp_stat[i] = FREE;
1806       printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1807              BN(j), i, SCarg->pid);
1808       SCarg->scsi_done(SCarg);
1809       return SUCCESS;
1810       }
1811
1812    panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
1813 }
1814
1815 int eata2x_abort(Scsi_Cmnd *SCarg) {
1816
1817    return do_abort(SCarg);
1818 }
1819
1820 static inline int do_reset(Scsi_Cmnd *SCarg) {
1821    unsigned int i, j, time, k, c, limit = 0;
1822    int arg_done = FALSE;
1823    Scsi_Cmnd *SCpnt;
1824
1825    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1826    printk("%s: reset, enter, target %d.%d:%d, pid %ld.\n",
1827           BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1828
1829    if (SCarg->host_scribble == NULL)
1830       printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
1831
1832    if (HD(j)->in_reset) {
1833       printk("%s: reset, exit, already in reset.\n", BN(j));
1834       return FAILED;
1835       }
1836
1837    if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1838       printk("%s: reset, exit, timeout error.\n", BN(j));
1839       return FAILED;
1840       }
1841
1842    HD(j)->retries = 0;
1843
1844    for (c = 0; c <= sh[j]->max_channel; c++)
1845       for (k = 0; k < sh[j]->max_id; k++) {
1846          HD(j)->target_redo[k][c] = TRUE;
1847          HD(j)->target_to[k][c] = 0;
1848          }
1849
1850    for (i = 0; i < sh[j]->can_queue; i++) {
1851
1852       if (HD(j)->cp_stat[i] == FREE) continue;
1853
1854       if (HD(j)->cp_stat[i] == LOCKED) {
1855          HD(j)->cp_stat[i] = FREE;
1856          printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
1857          continue;
1858          }
1859
1860       if (!(SCpnt = HD(j)->cp[i].SCpnt))
1861          panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
1862
1863       if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1864          HD(j)->cp_stat[i] = ABORTING;
1865          printk("%s: reset, mbox %d aborting, pid %ld.\n",
1866                 BN(j), i, SCpnt->pid);
1867          }
1868
1869       else {
1870          HD(j)->cp_stat[i] = IN_RESET;
1871          printk("%s: reset, mbox %d in reset, pid %ld.\n",
1872                 BN(j), i, SCpnt->pid);
1873          }
1874
1875       if (SCpnt->host_scribble == NULL)
1876          panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
1877
1878       if (*(unsigned int *)SCpnt->host_scribble != i)
1879          panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
1880
1881       if (SCpnt->scsi_done == NULL)
1882          panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
1883
1884       if (SCpnt == SCarg) arg_done = TRUE;
1885       }
1886
1887    if (do_dma(sh[j]->io_port, 0, RESET_PIO)) {
1888       printk("%s: reset, cannot reset, timeout error.\n", BN(j));
1889       return FAILED;
1890       }
1891
1892    printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
1893
1894 #if defined(DEBUG_RESET)
1895    do_trace = TRUE;
1896 #endif
1897
1898    HD(j)->in_reset = TRUE;
1899
1900    spin_unlock_irq(&io_request_lock);
1901    time = jiffies;
1902    while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
1903    spin_lock_irq(&io_request_lock);
1904
1905    printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
1906
1907    for (i = 0; i < sh[j]->can_queue; i++) {
1908
1909       if (HD(j)->cp_stat[i] == IN_RESET) {
1910          SCpnt = HD(j)->cp[i].SCpnt;
1911          unmap_dma(i, j);
1912          SCpnt->result = DID_RESET << 16;
1913          SCpnt->host_scribble = NULL;
1914
1915          /* This mailbox is still waiting for its interrupt */
1916          HD(j)->cp_stat[i] = LOCKED;
1917
1918          printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
1919                 BN(j), i, SCpnt->pid);
1920          }
1921
1922       else if (HD(j)->cp_stat[i] == ABORTING) {
1923          SCpnt = HD(j)->cp[i].SCpnt;
1924          unmap_dma(i, j);
1925          SCpnt->result = DID_RESET << 16;
1926          SCpnt->host_scribble = NULL;
1927
1928          /* This mailbox was never queued to the adapter */
1929          HD(j)->cp_stat[i] = FREE;
1930
1931          printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
1932                 BN(j), i, SCpnt->pid);
1933          }
1934
1935       else
1936
1937          /* Any other mailbox has already been set free by interrupt */
1938          continue;
1939
1940       SCpnt->scsi_done(SCpnt);
1941       }
1942
1943    HD(j)->in_reset = FALSE;
1944    do_trace = FALSE;
1945
1946    if (arg_done) printk("%s: reset, exit, pid %ld done.\n", BN(j), SCarg->pid);
1947    else          printk("%s: reset, exit.\n", BN(j));
1948
1949    return SUCCESS;
1950 }
1951
1952 int eata2x_reset(Scsi_Cmnd *SCarg) {
1953
1954    return do_reset(SCarg);
1955 }
1956
1957 int eata2x_biosparam(Disk *disk, kdev_t dev, int *dkinfo) {
1958    int size = disk->capacity;
1959
1960    if (ext_tran || (scsicam_bios_param(disk, dev, dkinfo) < 0)) {
1961       dkinfo[0] = 255;
1962       dkinfo[1] = 63;
1963       dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
1964       }
1965
1966 #if defined (DEBUG_GEOMETRY)
1967    printk ("%s: biosparam, head=%d, sec=%d, cyl=%d.\n", driver_name,
1968            dkinfo[0], dkinfo[1], dkinfo[2]);
1969 #endif
1970
1971    return FALSE;
1972 }
1973
1974 static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
1975                  unsigned int rev) {
1976    unsigned int i, j, k, y;
1977    unsigned long x;
1978
1979    for (i = 0; i < n - 1; i++) {
1980       k = i;
1981
1982       for (j = k + 1; j < n; j++)
1983          if (rev) {
1984             if (sk[j] > sk[k]) k = j;
1985             }
1986          else {
1987             if (sk[j] < sk[k]) k = j;
1988             }
1989
1990       if (k != i) {
1991          x = sk[k]; sk[k] = sk[i]; sk[i] = x;
1992          y = da[k]; da[k] = da[i]; da[i] = y;
1993          }
1994       }
1995
1996    return;
1997    }
1998
1999 static inline int reorder(unsigned int j, unsigned long cursec,
2000                  unsigned int ihdlr, unsigned int il[], unsigned int n_ready) {
2001    Scsi_Cmnd *SCpnt;
2002    struct mscp *cpp;
2003    unsigned int k, n;
2004    unsigned int rev = FALSE, s = TRUE, r = TRUE;
2005    unsigned int input_only = TRUE, overlap = FALSE;
2006    unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
2007    unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
2008    unsigned long ioseek = 0;
2009
2010    static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
2011    static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
2012    static unsigned int readysorted = 0, revcount = 0;
2013    static unsigned long seeksorted = 0, seeknosort = 0;
2014
2015    if (link_statistics && !(++flushcount % link_statistics))
2016       printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"\
2017              " av %ldK as %ldK.\n", flushcount, batchcount, inputcount,
2018              ovlcount, readycount, readysorted, sortcount, revcount,
2019              seeknosort / (readycount + 1),
2020              seeksorted / (readycount + 1));
2021
2022    if (n_ready <= 1) return FALSE;
2023
2024    for (n = 0; n < n_ready; n++) {
2025       k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
2026
2027       if (!cpp->din) input_only = FALSE;
2028
2029       if (SCpnt->request.sector < minsec) minsec = SCpnt->request.sector;
2030       if (SCpnt->request.sector > maxsec) maxsec = SCpnt->request.sector;
2031
2032       sl[n] = SCpnt->request.sector;
2033       ioseek += SCpnt->request.nr_sectors;
2034
2035       if (!n) continue;
2036
2037       if (sl[n] < sl[n - 1]) s = FALSE;
2038       if (sl[n] > sl[n - 1]) r = FALSE;
2039
2040       if (link_statistics) {
2041          if (sl[n] > sl[n - 1])
2042             seek += sl[n] - sl[n - 1];
2043          else
2044             seek += sl[n - 1] - sl[n];
2045          }
2046
2047       }
2048
2049    if (link_statistics) {
2050       if (cursec > sl[0]) seek += cursec - sl[0]; else seek += sl[0] - cursec;
2051       }
2052
2053    if (cursec > ((maxsec + minsec) / 2)) rev = TRUE;
2054
2055    if (ioseek > ((maxsec - minsec) / 2)) rev = FALSE;
2056
2057    if (!((rev && r) || (!rev && s))) sort(sl, il, n_ready, rev);
2058
2059    if (!input_only) for (n = 0; n < n_ready; n++) {
2060       k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
2061       ll[n] = SCpnt->request.nr_sectors; pl[n] = SCpnt->pid;
2062
2063       if (!n) continue;
2064
2065       if ((sl[n] == sl[n - 1]) || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
2066           || (rev && ((sl[n] + ll[n]) > sl[n - 1]))) overlap = TRUE;
2067       }
2068
2069    if (overlap) sort(pl, il, n_ready, FALSE);
2070
2071    if (link_statistics) {
2072       if (cursec > sl[0]) iseek = cursec - sl[0]; else iseek = sl[0] - cursec;
2073       batchcount++; readycount += n_ready; seeknosort += seek / 1024;
2074       if (input_only) inputcount++;
2075       if (overlap) { ovlcount++; seeksorted += iseek / 1024; }
2076       else seeksorted += (iseek + maxsec - minsec) / 1024;
2077       if (rev && !r)     {  revcount++; readysorted += n_ready; }
2078       if (!rev && !s)    { sortcount++; readysorted += n_ready; }
2079       }
2080
2081 #if defined(DEBUG_LINKED_COMMANDS)
2082    if (link_statistics && (overlap || !(flushcount % link_statistics)))
2083       for (n = 0; n < n_ready; n++) {
2084          k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
2085          printk("%s %d.%d:%d pid %ld mb %d fc %d nr %d sec %ld ns %ld"\
2086                 " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
2087                 (ihdlr ? "ihdlr" : "qcomm"), SCpnt->channel, SCpnt->target,
2088                 SCpnt->lun, SCpnt->pid, k, flushcount, n_ready,
2089                 SCpnt->request.sector, SCpnt->request.nr_sectors, cursec,
2090                 YESNO(s), YESNO(r), YESNO(rev), YESNO(input_only),
2091                 YESNO(overlap), cpp->din);
2092          }
2093 #endif
2094    return overlap;
2095 }
2096
2097 static void flush_dev(Scsi_Device *dev, unsigned long cursec, unsigned int j,
2098                       unsigned int ihdlr) {
2099    Scsi_Cmnd *SCpnt;
2100    struct mscp *cpp;
2101    unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
2102
2103    for (k = 0; k < sh[j]->can_queue; k++) {
2104
2105       if (HD(j)->cp_stat[k] != READY && HD(j)->cp_stat[k] != IN_USE) continue;
2106
2107       cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
2108
2109       if (SCpnt->device != dev) continue;
2110
2111       if (HD(j)->cp_stat[k] == IN_USE) return;
2112
2113       il[n_ready++] = k;
2114       }
2115
2116    if (reorder(j, cursec, ihdlr, il, n_ready)) n_ready = 1;
2117
2118    for (n = 0; n < n_ready; n++) {
2119       k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
2120
2121       if (do_dma(sh[j]->io_port, cpp->cp_dma_addr, SEND_CP_DMA)) {
2122          printk("%s: %s, target %d.%d:%d, pid %ld, mbox %d, adapter"\
2123                 " busy, will abort.\n", BN(j), (ihdlr ? "ihdlr" : "qcomm"),
2124                 SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid, k);
2125          HD(j)->cp_stat[k] = ABORTING;
2126          continue;
2127          }
2128
2129       HD(j)->cp_stat[k] = IN_USE;
2130       }
2131
2132 }
2133
2134 static inline void ihdlr(int irq, unsigned int j) {
2135    Scsi_Cmnd *SCpnt;
2136    unsigned int i, k, c, status, tstatus, reg;
2137    struct mssp *spp;
2138    struct mscp *cpp;
2139
2140    if (sh[j]->irq != irq)
2141        panic("%s: ihdlr, irq %d, sh[j]->irq %d.\n", BN(j), irq, sh[j]->irq);
2142
2143    /* Check if this board need to be serviced */
2144    if (!(inb(sh[j]->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)) return;
2145
2146    HD(j)->iocount++;
2147
2148    if (do_trace) printk("%s: ihdlr, enter, irq %d, count %d.\n", BN(j), irq,
2149                         HD(j)->iocount);
2150
2151    /* Check if this board is still busy */
2152    if (wait_on_busy(sh[j]->io_port, 20 * MAXLOOP)) {
2153       reg = inb(sh[j]->io_port + REG_STATUS);
2154       printk("%s: ihdlr, busy timeout error,  irq %d, reg 0x%x, count %d.\n",
2155              BN(j), irq, reg, HD(j)->iocount);
2156       return;
2157       }
2158
2159    spp  = &HD(j)->sp;
2160
2161    /* Make a local copy just before clearing the interrupt indication */
2162    memcpy(spp, HD(j)->sp_cpu_addr, sizeof(struct mssp));
2163
2164    /* Clear the completion flag and cp pointer on the dynamic copy of sp */
2165    memset(HD(j)->sp_cpu_addr, 0, sizeof(struct mssp));
2166
2167    /* Read the status register to clear the interrupt indication */
2168    reg = inb(sh[j]->io_port + REG_STATUS);
2169
2170 #if defined (DEBUG_INTERRUPT)
2171    {
2172    unsigned char *bytesp;
2173    int cnt;
2174    bytesp= (unsigned char *) spp;
2175    if (HD(j)->iocount < 200) {
2176       printk("sp[] =");
2177       for (cnt=0; cnt < 15; cnt++) printk(" 0x%x", bytesp[cnt]);
2178       printk("\n");
2179       }
2180    }
2181 #endif
2182
2183    /* Reject any sp with supspect data */
2184    if (spp->eoc == FALSE && HD(j)->iocount > 1)
2185       printk("%s: ihdlr, spp->eoc == FALSE, irq %d, reg 0x%x, count %d.\n",
2186              BN(j), irq, reg, HD(j)->iocount);
2187    if (spp->cpp_index < 0 || spp->cpp_index >= sh[j]->can_queue)
2188       printk("%s: ihdlr, bad spp->cpp_index %d, irq %d, reg 0x%x, count %d.\n",
2189              BN(j), spp->cpp_index, irq, reg, HD(j)->iocount);
2190    if (spp->eoc == FALSE || spp->cpp_index < 0
2191                          || spp->cpp_index >= sh[j]->can_queue) return;
2192
2193    /* Find the mailbox to be serviced on this board */
2194    i = spp->cpp_index;
2195
2196    cpp = &(HD(j)->cp[i]);
2197
2198 #if defined(DEBUG_GENERATE_ABORTS)
2199    if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 500) < 3)) return;
2200 #endif
2201
2202    if (HD(j)->cp_stat[i] == IGNORE) {
2203       HD(j)->cp_stat[i] = FREE;
2204       return;
2205       }
2206    else if (HD(j)->cp_stat[i] == LOCKED) {
2207       HD(j)->cp_stat[i] = FREE;
2208       printk("%s: ihdlr, mbox %d unlocked, count %d.\n", BN(j), i,
2209              HD(j)->iocount);
2210       return;
2211       }
2212    else if (HD(j)->cp_stat[i] == FREE) {
2213       printk("%s: ihdlr, mbox %d is free, count %d.\n", BN(j), i,
2214              HD(j)->iocount);
2215       return;
2216       }
2217    else if (HD(j)->cp_stat[i] == IN_RESET)
2218       printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
2219    else if (HD(j)->cp_stat[i] != IN_USE)
2220       panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
2221             BN(j), i, HD(j)->cp_stat[i]);
2222
2223    HD(j)->cp_stat[i] = FREE;
2224    SCpnt = cpp->SCpnt;
2225
2226    if (SCpnt == NULL) panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
2227
2228    if (SCpnt->host_scribble == NULL)
2229       panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", BN(j), i,
2230             SCpnt->pid, SCpnt);
2231
2232    if (*(unsigned int *)SCpnt->host_scribble != i)
2233       panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
2234             BN(j), i, SCpnt->pid, *(unsigned int *)SCpnt->host_scribble);
2235
2236    sync_dma(i, j);
2237
2238    if (linked_comm && SCpnt->device->queue_depth > 2
2239                                      && TLDEV(SCpnt->device->type))
2240       flush_dev(SCpnt->device, SCpnt->request.sector, j, TRUE);
2241
2242    tstatus = status_byte(spp->target_status);
2243
2244 #if defined(DEBUG_GENERATE_ERRORS)
2245    if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 200) < 2))
2246                                            spp->adapter_status = 0x01;
2247 #endif
2248
2249    switch (spp->adapter_status) {
2250       case ASOK:     /* status OK */
2251
2252          /* Forces a reset if a disk drive keeps returning BUSY */
2253          if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
2254             status = DID_ERROR << 16;
2255
2256          /* If there was a bus reset, redo operation on each target */
2257          else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
2258                   && HD(j)->target_redo[SCpnt->target][SCpnt->channel])
2259             status = DID_BUS_BUSY << 16;
2260
2261          /* Works around a flaw in scsi.c */
2262          else if (tstatus == CHECK_CONDITION
2263                   && SCpnt->device->type == TYPE_DISK
2264                   && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
2265             status = DID_BUS_BUSY << 16;
2266
2267          else
2268             status = DID_OK << 16;
2269
2270          if (tstatus == GOOD)
2271             HD(j)->target_redo[SCpnt->target][SCpnt->channel] = FALSE;
2272
2273          if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
2274              (!(tstatus == CHECK_CONDITION && HD(j)->iocount <= 1000 &&
2275                (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
2276             printk("%s: ihdlr, target %d.%d:%d, pid %ld, "\
2277                    "target_status 0x%x, sense key 0x%x.\n", BN(j),
2278                    SCpnt->channel, SCpnt->target, SCpnt->lun,
2279                    SCpnt->pid, spp->target_status,
2280                    SCpnt->sense_buffer[2]);
2281
2282          HD(j)->target_to[SCpnt->target][SCpnt->channel] = 0;
2283
2284          if (HD(j)->last_retried_pid == SCpnt->pid) HD(j)->retries = 0;
2285
2286          break;
2287       case ASST:     /* Selection Time Out */
2288       case 0x02:     /* Command Time Out   */
2289
2290          if (HD(j)->target_to[SCpnt->target][SCpnt->channel] > 1)
2291             status = DID_ERROR << 16;
2292          else {
2293             status = DID_TIME_OUT << 16;
2294             HD(j)->target_to[SCpnt->target][SCpnt->channel]++;
2295             }
2296
2297          break;
2298
2299       /* Perform a limited number of internal retries */
2300       case 0x03:     /* SCSI Bus Reset Received */
2301       case 0x04:     /* Initial Controller Power-up */
2302
2303          for (c = 0; c <= sh[j]->max_channel; c++)
2304             for (k = 0; k < sh[j]->max_id; k++)
2305                HD(j)->target_redo[k][c] = TRUE;
2306
2307          if (SCpnt->device->type != TYPE_TAPE
2308              && HD(j)->retries < MAX_INTERNAL_RETRIES) {
2309
2310 #if defined(DID_SOFT_ERROR)
2311             status = DID_SOFT_ERROR << 16;
2312 #else
2313             status = DID_BUS_BUSY << 16;
2314 #endif
2315
2316             HD(j)->retries++;
2317             HD(j)->last_retried_pid = SCpnt->pid;
2318             }
2319          else
2320             status = DID_ERROR << 16;
2321
2322          break;
2323       case 0x05:     /* Unexpected Bus Phase */
2324       case 0x06:     /* Unexpected Bus Free */
2325       case 0x07:     /* Bus Parity Error */
2326       case 0x08:     /* SCSI Hung */
2327       case 0x09:     /* Unexpected Message Reject */
2328       case 0x0a:     /* SCSI Bus Reset Stuck */
2329       case 0x0b:     /* Auto Request-Sense Failed */
2330       case 0x0c:     /* Controller Ram Parity Error */
2331       default:
2332          status = DID_ERROR << 16;
2333          break;
2334       }
2335
2336    SCpnt->result = status | spp->target_status;
2337
2338 #if defined(DEBUG_INTERRUPT)
2339    if (SCpnt->result || do_trace)
2340 #else
2341    if ((spp->adapter_status != ASOK && HD(j)->iocount >  1000) ||
2342        (spp->adapter_status != ASOK &&
2343         spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
2344         do_trace || msg_byte(spp->target_status))
2345 #endif
2346       printk("%s: ihdlr, mbox %2d, err 0x%x:%x,"\
2347              " target %d.%d:%d, pid %ld, reg 0x%x, count %d.\n",
2348              BN(j), i, spp->adapter_status, spp->target_status,
2349              SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid,
2350              reg, HD(j)->iocount);
2351
2352    unmap_dma(i, j);
2353
2354    /* Set the command state to inactive */
2355    SCpnt->host_scribble = NULL;
2356
2357    SCpnt->scsi_done(SCpnt);
2358
2359    if (do_trace) printk("%s: ihdlr, exit, irq %d, count %d.\n", BN(j), irq,
2360                         HD(j)->iocount);
2361
2362    return;
2363 }
2364
2365 static void do_interrupt_handler(int irq, void *shap, struct pt_regs *regs) {
2366    unsigned int j;
2367    unsigned long spin_flags;
2368
2369    /* Check if the interrupt must be processed by this handler */
2370    if ((j = (unsigned int)((char *)shap - sha)) >= num_boards) return;
2371
2372    spin_lock_irqsave(&io_request_lock, spin_flags);
2373    ihdlr(irq, j);
2374    spin_unlock_irqrestore(&io_request_lock, spin_flags);
2375 }
2376
2377 int eata2x_release(struct Scsi_Host *shpnt) {
2378    unsigned int i, j;
2379
2380    for (j = 0; sh[j] != NULL && sh[j] != shpnt; j++);
2381
2382    if (sh[j] == NULL) panic("%s: release, invalid Scsi_Host pointer.\n",
2383                             driver_name);
2384
2385    if(sh[j]->unchecked_isa_dma) scsi_deregister_blocked_host(sh[j]);
2386
2387    for (i = 0; i < sh[j]->can_queue; i++)
2388       if ((&HD(j)->cp[i])->sglist) kfree((&HD(j)->cp[i])->sglist);
2389
2390    for (i = 0; i < sh[j]->can_queue; i++)
2391       pci_unmap_single(HD(j)->pdev, HD(j)->cp[i].cp_dma_addr,
2392                      sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
2393
2394    if (HD(j)->sp_cpu_addr)
2395       pci_free_consistent(HD(j)->pdev, sizeof(struct mssp),
2396                           HD(j)->sp_cpu_addr, HD(j)->sp_dma_addr);
2397
2398    free_irq(sh[j]->irq, &sha[j]);
2399
2400    if (sh[j]->dma_channel != NO_DMA) free_dma(sh[j]->dma_channel);
2401
2402    release_region(sh[j]->io_port, sh[j]->n_io_port);
2403    scsi_unregister(sh[j]);
2404    return FALSE;
2405 }
2406
2407 static Scsi_Host_Template driver_template = EATA;
2408
2409 #include "scsi_module.c"
2410
2411 #ifndef MODULE
2412 __setup("eata=", option_setup);
2413 #endif /* end MODULE */
2414 MODULE_LICENSE("GPL");