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