]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/eata.c
05163cefec12f7f2f806009de76da5d77767a0ed
[linux-2.6-omap-h63xx.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 = 1 for all ISA boards.
478  */
479
480 #include <linux/string.h>
481 #include <linux/kernel.h>
482 #include <linux/ioport.h>
483 #include <linux/delay.h>
484 #include <linux/proc_fs.h>
485 #include <linux/blkdev.h>
486 #include <linux/interrupt.h>
487 #include <linux/stat.h>
488 #include <linux/pci.h>
489 #include <linux/init.h>
490 #include <linux/ctype.h>
491 #include <linux/spinlock.h>
492 #include <linux/dma-mapping.h>
493 #include <asm/byteorder.h>
494 #include <asm/dma.h>
495 #include <asm/io.h>
496 #include <asm/irq.h>
497
498 #include <scsi/scsi.h>
499 #include <scsi/scsi_cmnd.h>
500 #include <scsi/scsi_device.h>
501 #include <scsi/scsi_host.h>
502 #include <scsi/scsi_tcq.h>
503 #include <scsi/scsicam.h>
504
505 static int eata2x_detect(struct scsi_host_template *);
506 static int eata2x_release(struct Scsi_Host *);
507 static int eata2x_queuecommand(struct scsi_cmnd *,
508                                void (*done) (struct scsi_cmnd *));
509 static int eata2x_eh_abort(struct scsi_cmnd *);
510 static int eata2x_eh_host_reset(struct scsi_cmnd *);
511 static int eata2x_bios_param(struct scsi_device *, struct block_device *,
512                              sector_t, int *);
513 static int eata2x_slave_configure(struct scsi_device *);
514
515 static struct scsi_host_template driver_template = {
516         .name = "EATA/DMA 2.0x rev. 8.10.00 ",
517         .detect = eata2x_detect,
518         .release = eata2x_release,
519         .queuecommand = eata2x_queuecommand,
520         .eh_abort_handler = eata2x_eh_abort,
521         .eh_host_reset_handler = eata2x_eh_host_reset,
522         .bios_param = eata2x_bios_param,
523         .slave_configure = eata2x_slave_configure,
524         .this_id = 7,
525         .unchecked_isa_dma = 1,
526         .use_clustering = ENABLE_CLUSTERING,
527         .use_sg_chaining = ENABLE_SG_CHAINING,
528 };
529
530 #if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
531 #error "Adjust your <asm/byteorder.h> defines"
532 #endif
533
534 /* Subversion values */
535 #define ISA  0
536 #define ESA 1
537
538 #undef  FORCE_CONFIG
539
540 #undef  DEBUG_LINKED_COMMANDS
541 #undef  DEBUG_DETECT
542 #undef  DEBUG_PCI_DETECT
543 #undef  DEBUG_INTERRUPT
544 #undef  DEBUG_RESET
545 #undef  DEBUG_GENERATE_ERRORS
546 #undef  DEBUG_GENERATE_ABORTS
547 #undef  DEBUG_GEOMETRY
548
549 #define MAX_ISA 4
550 #define MAX_VESA 0
551 #define MAX_EISA 15
552 #define MAX_PCI 16
553 #define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
554 #define MAX_CHANNEL 4
555 #define MAX_LUN 32
556 #define MAX_TARGET 32
557 #define MAX_MAILBOXES 64
558 #define MAX_SGLIST 64
559 #define MAX_LARGE_SGLIST 122
560 #define MAX_INTERNAL_RETRIES 64
561 #define MAX_CMD_PER_LUN 2
562 #define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
563
564 #define SKIP ULONG_MAX
565 #define FREE 0
566 #define IN_USE   1
567 #define LOCKED   2
568 #define IN_RESET 3
569 #define IGNORE   4
570 #define READY    5
571 #define ABORTING 6
572 #define NO_DMA  0xff
573 #define MAXLOOP  10000
574 #define TAG_DISABLED 0
575 #define TAG_SIMPLE   1
576 #define TAG_ORDERED  2
577
578 #define REG_CMD         7
579 #define REG_STATUS      7
580 #define REG_AUX_STATUS  8
581 #define REG_DATA        0
582 #define REG_DATA2       1
583 #define REG_SEE         6
584 #define REG_LOW         2
585 #define REG_LM          3
586 #define REG_MID         4
587 #define REG_MSB         5
588 #define REGION_SIZE     9UL
589 #define MAX_ISA_ADDR    0x03ff
590 #define MIN_EISA_ADDR   0x1c88
591 #define MAX_EISA_ADDR   0xfc88
592 #define BSY_ASSERTED      0x80
593 #define DRQ_ASSERTED      0x08
594 #define ABSY_ASSERTED     0x01
595 #define IRQ_ASSERTED      0x02
596 #define READ_CONFIG_PIO   0xf0
597 #define SET_CONFIG_PIO    0xf1
598 #define SEND_CP_PIO       0xf2
599 #define RECEIVE_SP_PIO    0xf3
600 #define TRUNCATE_XFR_PIO  0xf4
601 #define RESET_PIO         0xf9
602 #define READ_CONFIG_DMA   0xfd
603 #define SET_CONFIG_DMA    0xfe
604 #define SEND_CP_DMA       0xff
605 #define ASOK              0x00
606 #define ASST              0x01
607
608 #define YESNO(a) ((a) ? 'y' : 'n')
609 #define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
610
611 /* "EATA", in Big Endian format */
612 #define EATA_SIG_BE 0x45415441
613
614 /* Number of valid bytes in the board config structure for EATA 2.0x */
615 #define EATA_2_0A_SIZE 28
616 #define EATA_2_0B_SIZE 30
617 #define EATA_2_0C_SIZE 34
618
619 /* Board info structure */
620 struct eata_info {
621         u_int32_t data_len;     /* Number of valid bytes after this field */
622         u_int32_t sign;         /* ASCII "EATA" signature */
623
624 #if defined(__BIG_ENDIAN_BITFIELD)
625         unchar version  : 4,
626                         : 4;
627         unchar haaval   : 1,
628                ata      : 1,
629                drqvld   : 1,
630                dmasup   : 1,
631                morsup   : 1,
632                trnxfr   : 1,
633                tarsup   : 1,
634                ocsena   : 1;
635 #else
636         unchar          : 4,    /* unused low nibble */
637                 version : 4;    /* EATA version, should be 0x1 */
638         unchar ocsena   : 1,    /* Overlap Command Support Enabled */
639                tarsup   : 1,    /* Target Mode Supported */
640                trnxfr   : 1,    /* Truncate Transfer Cmd NOT Necessary */
641                morsup   : 1,    /* More Supported */
642                dmasup   : 1,    /* DMA Supported */
643                drqvld   : 1,    /* DRQ Index (DRQX) is valid */
644                ata      : 1,    /* This is an ATA device */
645                haaval   : 1;    /* Host Adapter Address Valid */
646 #endif
647
648         ushort cp_pad_len;      /* Number of pad bytes after cp_len */
649         unchar host_addr[4];    /* Host Adapter SCSI ID for channels 3, 2, 1, 0 */
650         u_int32_t cp_len;       /* Number of valid bytes in cp */
651         u_int32_t sp_len;       /* Number of valid bytes in sp */
652         ushort queue_size;      /* Max number of cp that can be queued */
653         ushort unused;
654         ushort scatt_size;      /* Max number of entries in scatter/gather table */
655
656 #if defined(__BIG_ENDIAN_BITFIELD)
657         unchar drqx     : 2,
658                second   : 1,
659                irq_tr   : 1,
660                irq      : 4;
661         unchar sync;
662         unchar          : 4,
663                res1     : 1,
664                large_sg : 1,
665                forcaddr : 1,
666                isaena   : 1;
667         unchar max_chan : 3,
668                max_id   : 5;
669         unchar max_lun;
670         unchar eisa     : 1,
671                pci      : 1,
672                idquest  : 1,
673                m1       : 1,
674                         : 4;
675 #else
676         unchar irq      : 4,    /* Interrupt Request assigned to this controller */
677                irq_tr   : 1,    /* 0 for edge triggered, 1 for level triggered */
678                second   : 1,    /* 1 if this is a secondary (not primary) controller */
679                drqx     : 2;    /* DRQ Index (0=DMA0, 1=DMA7, 2=DMA6, 3=DMA5) */
680         unchar sync;            /* 1 if scsi target id 7...0 is running sync scsi */
681
682         /* Structure extension defined in EATA 2.0B */
683         unchar isaena   : 1,    /* ISA i/o addressing is disabled/enabled */
684                forcaddr : 1,    /* Port address has been forced */
685                large_sg : 1,    /* 1 if large SG lists are supported */
686                res1     : 1,
687                         : 4;
688         unchar max_id   : 5,    /* Max SCSI target ID number */
689                max_chan : 3;    /* Max SCSI channel number on this board */
690
691         /* Structure extension defined in EATA 2.0C */
692         unchar max_lun;         /* Max SCSI LUN number */
693         unchar
694                         : 4,
695                m1       : 1,    /* This is a PCI with an M1 chip installed */
696                idquest  : 1,    /* RAIDNUM returned is questionable */
697                pci      : 1,    /* This board is PCI */
698                eisa     : 1;    /* This board is EISA */
699 #endif
700
701         unchar raidnum;         /* Uniquely identifies this HBA in a system */
702         unchar notused;
703
704         ushort ipad[247];
705 };
706
707 /* Board config structure */
708 struct eata_config {
709         ushort len;             /* Number of bytes following this field */
710
711 #if defined(__BIG_ENDIAN_BITFIELD)
712         unchar          : 4,
713                tarena   : 1,
714                mdpena   : 1,
715                ocena    : 1,
716                edis     : 1;
717 #else
718         unchar edis     : 1,    /* Disable EATA interface after config command */
719                ocena    : 1,    /* Overlapped Commands Enabled */
720                mdpena   : 1,    /* Transfer all Modified Data Pointer Messages */
721                tarena   : 1,    /* Target Mode Enabled for this controller */
722                         : 4;
723 #endif
724         unchar cpad[511];
725 };
726
727 /* Returned status packet structure */
728 struct mssp {
729 #if defined(__BIG_ENDIAN_BITFIELD)
730         unchar eoc      : 1,
731                adapter_status : 7;
732 #else
733         unchar adapter_status : 7,      /* State related to current command */
734                eoc      : 1;            /* End Of Command (1 = command completed) */
735 #endif
736         unchar target_status;   /* SCSI status received after data transfer */
737         unchar unused[2];
738         u_int32_t inv_res_len;  /* Number of bytes not transferred */
739         u_int32_t cpp_index;    /* Index of address set in cp */
740         char mess[12];
741 };
742
743 struct sg_list {
744         unsigned int address;   /* Segment Address */
745         unsigned int num_bytes; /* Segment Length */
746 };
747
748 /* MailBox SCSI Command Packet */
749 struct mscp {
750 #if defined(__BIG_ENDIAN_BITFIELD)
751         unchar din      : 1,
752                dout     : 1,
753                interp   : 1,
754                         : 1,
755                 sg      : 1,
756                 reqsen  :1,
757                 init    : 1,
758                 sreset  : 1;
759         unchar sense_len;
760         unchar unused[3];
761         unchar          : 7,
762                fwnest   : 1;
763         unchar          : 5,
764                hbaci    : 1,
765                iat      : 1,
766                phsunit  : 1;
767         unchar channel  : 3,
768                target   : 5;
769         unchar one      : 1,
770                dispri   : 1,
771                luntar   : 1,
772                lun      : 5;
773 #else
774         unchar sreset   :1,     /* SCSI Bus Reset Signal should be asserted */
775                init     :1,     /* Re-initialize controller and self test */
776                reqsen   :1,     /* Transfer Request Sense Data to addr using DMA */
777                sg       :1,     /* Use Scatter/Gather */
778                         :1,
779                interp   :1,     /* The controller interprets cp, not the target */
780                dout     :1,     /* Direction of Transfer is Out (Host to Target) */
781                din      :1;     /* Direction of Transfer is In (Target to Host) */
782         unchar sense_len;       /* Request Sense Length */
783         unchar unused[3];
784         unchar fwnest   : 1,    /* Send command to a component of an Array Group */
785                         : 7;
786         unchar phsunit  : 1,    /* Send to Target Physical Unit (bypass RAID) */
787                iat      : 1,    /* Inhibit Address Translation */
788                hbaci    : 1,    /* Inhibit HBA Caching for this command */
789                         : 5;
790         unchar target   : 5,    /* SCSI target ID */
791                channel  : 3;    /* SCSI channel number */
792         unchar lun      : 5,    /* SCSI logical unit number */
793                luntar   : 1,    /* This cp is for Target (not LUN) */
794                dispri   : 1,    /* Disconnect Privilege granted */
795                one      : 1;    /* 1 */
796 #endif
797
798         unchar mess[3];         /* Massage to/from Target */
799         unchar cdb[12];         /* Command Descriptor Block */
800         u_int32_t data_len;     /* If sg=0 Data Length, if sg=1 sglist length */
801         u_int32_t cpp_index;    /* Index of address to be returned in sp */
802         u_int32_t data_address; /* If sg=0 Data Address, if sg=1 sglist address */
803         u_int32_t sp_dma_addr;  /* Address where sp is DMA'ed when cp completes */
804         u_int32_t sense_addr;   /* Address where Sense Data is DMA'ed on error */
805
806         /* Additional fields begin here. */
807         struct scsi_cmnd *SCpnt;
808
809         /* All the cp structure is zero filled by queuecommand except the
810            following CP_TAIL_SIZE bytes, initialized by detect */
811         dma_addr_t cp_dma_addr; /* dma handle for this cp structure */
812         struct sg_list *sglist; /* pointer to the allocated SG list */
813 };
814
815 #define CP_TAIL_SIZE (sizeof(struct sglist *) + sizeof(dma_addr_t))
816
817 struct hostdata {
818         struct mscp cp[MAX_MAILBOXES];  /* Mailboxes for this board */
819         unsigned int cp_stat[MAX_MAILBOXES];    /* FREE, IN_USE, LOCKED, IN_RESET */
820         unsigned int last_cp_used;      /* Index of last mailbox used */
821         unsigned int iocount;   /* Total i/o done for this board */
822         int board_number;       /* Number of this board */
823         char board_name[16];    /* Name of this board */
824         int in_reset;           /* True if board is doing a reset */
825         int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
826         int target_redo[MAX_TARGET][MAX_CHANNEL];       /* If 1 redo i/o on target */
827         unsigned int retries;   /* Number of internal retries */
828         unsigned long last_retried_pid; /* Pid of last retried command */
829         unsigned char subversion;       /* Bus type, either ISA or EISA/PCI */
830         unsigned char protocol_rev;     /* EATA 2.0 rev., 'A' or 'B' or 'C' */
831         unsigned char is_pci;   /* 1 is bus type is PCI */
832         struct pci_dev *pdev;   /* pdev for PCI bus, NULL otherwise */
833         struct mssp *sp_cpu_addr;       /* cpu addr for DMA buffer sp */
834         dma_addr_t sp_dma_addr; /* dma handle for DMA buffer sp */
835         struct mssp sp;         /* Local copy of sp buffer */
836 };
837
838 static struct Scsi_Host *sh[MAX_BOARDS];
839 static const char *driver_name = "EATA";
840 static char sha[MAX_BOARDS];
841 static DEFINE_SPINLOCK(driver_lock);
842
843 /* Initialize num_boards so that ihdlr can work while detect is in progress */
844 static unsigned int num_boards = MAX_BOARDS;
845
846 static unsigned long io_port[] = {
847
848         /* Space for MAX_INT_PARAM ports usable while loading as a module */
849         SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
850         SKIP, SKIP,
851
852         /* First ISA */
853         0x1f0,
854
855         /* Space for MAX_PCI ports possibly reported by PCI_BIOS */
856         SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
857         SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
858
859         /* MAX_EISA ports */
860         0x1c88, 0x2c88, 0x3c88, 0x4c88, 0x5c88, 0x6c88, 0x7c88, 0x8c88,
861         0x9c88, 0xac88, 0xbc88, 0xcc88, 0xdc88, 0xec88, 0xfc88,
862
863         /* Other (MAX_ISA - 1) ports */
864         0x170, 0x230, 0x330,
865
866         /* End of list */
867         0x0
868 };
869
870 /* Device is Big Endian */
871 #define H2DEV(x)   cpu_to_be32(x)
872 #define DEV2H(x)   be32_to_cpu(x)
873 #define H2DEV16(x) cpu_to_be16(x)
874 #define DEV2H16(x) be16_to_cpu(x)
875
876 /* But transfer orientation from the 16 bit data register is Little Endian */
877 #define REG2H(x)   le16_to_cpu(x)
878
879 static irqreturn_t do_interrupt_handler(int, void *);
880 static void flush_dev(struct scsi_device *, unsigned long, struct hostdata *,
881                       unsigned int);
882 static int do_trace = 0;
883 static int setup_done = 0;
884 static int link_statistics;
885 static int ext_tran = 0;
886 static int rev_scan = 1;
887
888 #if defined(CONFIG_SCSI_EATA_TAGGED_QUEUE)
889 static int tag_mode = TAG_SIMPLE;
890 #else
891 static int tag_mode = TAG_DISABLED;
892 #endif
893
894 #if defined(CONFIG_SCSI_EATA_LINKED_COMMANDS)
895 static int linked_comm = 1;
896 #else
897 static int linked_comm = 0;
898 #endif
899
900 #if defined(CONFIG_SCSI_EATA_MAX_TAGS)
901 static int max_queue_depth = CONFIG_SCSI_EATA_MAX_TAGS;
902 #else
903 static int max_queue_depth = MAX_CMD_PER_LUN;
904 #endif
905
906 #if defined(CONFIG_ISA)
907 static int isa_probe = 1;
908 #else
909 static int isa_probe = 0;
910 #endif
911
912 #if defined(CONFIG_EISA)
913 static int eisa_probe = 1;
914 #else
915 static int eisa_probe = 0;
916 #endif
917
918 #if defined(CONFIG_PCI)
919 static int pci_probe = 1;
920 #else
921 static int pci_probe = 0;
922 #endif
923
924 #define MAX_INT_PARAM 10
925 #define MAX_BOOT_OPTIONS_SIZE 256
926 static char boot_options[MAX_BOOT_OPTIONS_SIZE];
927
928 #if defined(MODULE)
929 #include <linux/module.h>
930 #include <linux/moduleparam.h>
931
932 module_param_string(eata, boot_options, MAX_BOOT_OPTIONS_SIZE, 0);
933 MODULE_PARM_DESC(eata, " equivalent to the \"eata=...\" kernel boot option."
934                  "            Example: modprobe eata \"eata=0x7410,0x230,lc:y,tm:0,mq:4,ep:n\"");
935 MODULE_AUTHOR("Dario Ballabio");
936 MODULE_LICENSE("GPL");
937 MODULE_DESCRIPTION("EATA/DMA SCSI Driver");
938
939 #endif
940
941 static int eata2x_slave_configure(struct scsi_device *dev)
942 {
943         int tqd, utqd;
944         char *tag_suffix, *link_suffix;
945
946         utqd = MAX_CMD_PER_LUN;
947         tqd = max_queue_depth;
948
949         if (TLDEV(dev->type) && dev->tagged_supported) {
950                 if (tag_mode == TAG_SIMPLE) {
951                         scsi_adjust_queue_depth(dev, MSG_SIMPLE_TAG, tqd);
952                         tag_suffix = ", simple tags";
953                 } else if (tag_mode == TAG_ORDERED) {
954                         scsi_adjust_queue_depth(dev, MSG_ORDERED_TAG, tqd);
955                         tag_suffix = ", ordered tags";
956                 } else {
957                         scsi_adjust_queue_depth(dev, 0, tqd);
958                         tag_suffix = ", no tags";
959                 }
960         } else if (TLDEV(dev->type) && linked_comm) {
961                 scsi_adjust_queue_depth(dev, 0, tqd);
962                 tag_suffix = ", untagged";
963         } else {
964                 scsi_adjust_queue_depth(dev, 0, utqd);
965                 tag_suffix = "";
966         }
967
968         if (TLDEV(dev->type) && linked_comm && dev->queue_depth > 2)
969                 link_suffix = ", sorted";
970         else if (TLDEV(dev->type))
971                 link_suffix = ", unsorted";
972         else
973                 link_suffix = "";
974
975         sdev_printk(KERN_INFO, dev,
976                 "cmds/lun %d%s%s.\n",
977                dev->queue_depth, link_suffix, tag_suffix);
978
979         return 0;
980 }
981
982 static int wait_on_busy(unsigned long iobase, unsigned int loop)
983 {
984         while (inb(iobase + REG_AUX_STATUS) & ABSY_ASSERTED) {
985                 udelay(1L);
986                 if (--loop == 0)
987                         return 1;
988         }
989         return 0;
990 }
991
992 static int do_dma(unsigned long iobase, unsigned long addr, unchar cmd)
993 {
994         unsigned char *byaddr;
995         unsigned long devaddr;
996
997         if (wait_on_busy(iobase, (addr ? MAXLOOP * 100 : MAXLOOP)))
998                 return 1;
999
1000         if (addr) {
1001                 devaddr = H2DEV(addr);
1002                 byaddr = (unsigned char *)&devaddr;
1003                 outb(byaddr[3], iobase + REG_LOW);
1004                 outb(byaddr[2], iobase + REG_LM);
1005                 outb(byaddr[1], iobase + REG_MID);
1006                 outb(byaddr[0], iobase + REG_MSB);
1007         }
1008
1009         outb(cmd, iobase + REG_CMD);
1010         return 0;
1011 }
1012
1013 static int read_pio(unsigned long iobase, ushort * start, ushort * end)
1014 {
1015         unsigned int loop = MAXLOOP;
1016         ushort *p;
1017
1018         for (p = start; p <= end; p++) {
1019                 while (!(inb(iobase + REG_STATUS) & DRQ_ASSERTED)) {
1020                         udelay(1L);
1021                         if (--loop == 0)
1022                                 return 1;
1023                 }
1024                 loop = MAXLOOP;
1025                 *p = REG2H(inw(iobase));
1026         }
1027
1028         return 0;
1029 }
1030
1031 static struct pci_dev *get_pci_dev(unsigned long port_base)
1032 {
1033 #if defined(CONFIG_PCI)
1034         unsigned int addr;
1035         struct pci_dev *dev = NULL;
1036
1037         while ((dev = pci_get_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
1038                 addr = pci_resource_start(dev, 0);
1039
1040 #if defined(DEBUG_PCI_DETECT)
1041                 printk("%s: get_pci_dev, bus %d, devfn 0x%x, addr 0x%x.\n",
1042                        driver_name, dev->bus->number, dev->devfn, addr);
1043 #endif
1044
1045                 /* we are in so much trouble for a pci hotplug system with this driver
1046                  * anyway, so doing this at least lets people unload the driver and not
1047                  * cause memory problems, but in general this is a bad thing to do (this
1048                  * driver needs to be converted to the proper PCI api someday... */
1049                 pci_dev_put(dev);
1050                 if (addr + PCI_BASE_ADDRESS_0 == port_base)
1051                         return dev;
1052         }
1053 #endif                          /* end CONFIG_PCI */
1054         return NULL;
1055 }
1056
1057 static void enable_pci_ports(void)
1058 {
1059 #if defined(CONFIG_PCI)
1060         struct pci_dev *dev = NULL;
1061
1062         while ((dev = pci_get_class(PCI_CLASS_STORAGE_SCSI << 8, dev))) {
1063 #if defined(DEBUG_PCI_DETECT)
1064                 printk("%s: enable_pci_ports, bus %d, devfn 0x%x.\n",
1065                        driver_name, dev->bus->number, dev->devfn);
1066 #endif
1067
1068                 if (pci_enable_device(dev))
1069                         printk
1070                             ("%s: warning, pci_enable_device failed, bus %d devfn 0x%x.\n",
1071                              driver_name, dev->bus->number, dev->devfn);
1072         }
1073
1074 #endif                          /* end CONFIG_PCI */
1075 }
1076
1077 static int port_detect(unsigned long port_base, unsigned int j,
1078                 struct scsi_host_template *tpnt)
1079 {
1080         unsigned char irq, dma_channel, subversion, i, is_pci = 0;
1081         unsigned char protocol_rev;
1082         struct eata_info info;
1083         char *bus_type, dma_name[16];
1084         struct pci_dev *pdev;
1085         /* Allowed DMA channels for ISA (0 indicates reserved) */
1086         unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
1087         struct Scsi_Host *shost;
1088         struct hostdata *ha;
1089         char name[16];
1090
1091         sprintf(name, "%s%d", driver_name, j);
1092
1093         if (!request_region(port_base, REGION_SIZE, driver_name)) {
1094 #if defined(DEBUG_DETECT)
1095                 printk("%s: address 0x%03lx in use, skipping probe.\n", name,
1096                        port_base);
1097 #endif
1098                 goto fail;
1099         }
1100
1101         spin_lock_irq(&driver_lock);
1102
1103         if (do_dma(port_base, 0, READ_CONFIG_PIO)) {
1104 #if defined(DEBUG_DETECT)
1105                 printk("%s: detect, do_dma failed at 0x%03lx.\n", name,
1106                        port_base);
1107 #endif
1108                 goto freelock;
1109         }
1110
1111         /* Read the info structure */
1112         if (read_pio(port_base, (ushort *) & info, (ushort *) & info.ipad[0])) {
1113 #if defined(DEBUG_DETECT)
1114                 printk("%s: detect, read_pio failed at 0x%03lx.\n", name,
1115                        port_base);
1116 #endif
1117                 goto freelock;
1118         }
1119
1120         info.data_len = DEV2H(info.data_len);
1121         info.sign = DEV2H(info.sign);
1122         info.cp_pad_len = DEV2H16(info.cp_pad_len);
1123         info.cp_len = DEV2H(info.cp_len);
1124         info.sp_len = DEV2H(info.sp_len);
1125         info.scatt_size = DEV2H16(info.scatt_size);
1126         info.queue_size = DEV2H16(info.queue_size);
1127
1128         /* Check the controller "EATA" signature */
1129         if (info.sign != EATA_SIG_BE) {
1130 #if defined(DEBUG_DETECT)
1131                 printk("%s: signature 0x%04x discarded.\n", name, info.sign);
1132 #endif
1133                 goto freelock;
1134         }
1135
1136         if (info.data_len < EATA_2_0A_SIZE) {
1137                 printk
1138                     ("%s: config structure size (%d bytes) too short, detaching.\n",
1139                      name, info.data_len);
1140                 goto freelock;
1141         } else if (info.data_len == EATA_2_0A_SIZE)
1142                 protocol_rev = 'A';
1143         else if (info.data_len == EATA_2_0B_SIZE)
1144                 protocol_rev = 'B';
1145         else
1146                 protocol_rev = 'C';
1147
1148         if (protocol_rev != 'A' && info.forcaddr) {
1149                 printk("%s: warning, port address has been forced.\n", name);
1150                 bus_type = "PCI";
1151                 is_pci = 1;
1152                 subversion = ESA;
1153         } else if (port_base > MAX_EISA_ADDR
1154                    || (protocol_rev == 'C' && info.pci)) {
1155                 bus_type = "PCI";
1156                 is_pci = 1;
1157                 subversion = ESA;
1158         } else if (port_base >= MIN_EISA_ADDR
1159                    || (protocol_rev == 'C' && info.eisa)) {
1160                 bus_type = "EISA";
1161                 subversion = ESA;
1162         } else if (protocol_rev == 'C' && !info.eisa && !info.pci) {
1163                 bus_type = "ISA";
1164                 subversion = ISA;
1165         } else if (port_base > MAX_ISA_ADDR) {
1166                 bus_type = "PCI";
1167                 is_pci = 1;
1168                 subversion = ESA;
1169         } else {
1170                 bus_type = "ISA";
1171                 subversion = ISA;
1172         }
1173
1174         if (!info.haaval || info.ata) {
1175                 printk
1176                     ("%s: address 0x%03lx, unusable %s board (%d%d), detaching.\n",
1177                      name, port_base, bus_type, info.haaval, info.ata);
1178                 goto freelock;
1179         }
1180
1181         if (info.drqvld) {
1182                 if (subversion == ESA)
1183                         printk("%s: warning, weird %s board using DMA.\n", name,
1184                                bus_type);
1185
1186                 subversion = ISA;
1187                 dma_channel = dma_channel_table[3 - info.drqx];
1188         } else {
1189                 if (subversion == ISA)
1190                         printk("%s: warning, weird %s board not using DMA.\n",
1191                                name, bus_type);
1192
1193                 subversion = ESA;
1194                 dma_channel = NO_DMA;
1195         }
1196
1197         if (!info.dmasup)
1198                 printk("%s: warning, DMA protocol support not asserted.\n",
1199                        name);
1200
1201         irq = info.irq;
1202
1203         if (subversion == ESA && !info.irq_tr)
1204                 printk
1205                     ("%s: warning, LEVEL triggering is suggested for IRQ %u.\n",
1206                      name, irq);
1207
1208         if (is_pci) {
1209                 pdev = get_pci_dev(port_base);
1210                 if (!pdev)
1211                         printk
1212                             ("%s: warning, failed to get pci_dev structure.\n",
1213                              name);
1214         } else
1215                 pdev = NULL;
1216
1217         if (pdev && (irq != pdev->irq)) {
1218                 printk("%s: IRQ %u mapped to IO-APIC IRQ %u.\n", name, irq,
1219                        pdev->irq);
1220                 irq = pdev->irq;
1221         }
1222
1223         /* Board detected, allocate its IRQ */
1224         if (request_irq(irq, do_interrupt_handler,
1225                         IRQF_DISABLED | ((subversion == ESA) ? IRQF_SHARED : 0),
1226                         driver_name, (void *)&sha[j])) {
1227                 printk("%s: unable to allocate IRQ %u, detaching.\n", name,
1228                        irq);
1229                 goto freelock;
1230         }
1231
1232         if (subversion == ISA && request_dma(dma_channel, driver_name)) {
1233                 printk("%s: unable to allocate DMA channel %u, detaching.\n",
1234                        name, dma_channel);
1235                 goto freeirq;
1236         }
1237 #if defined(FORCE_CONFIG)
1238         {
1239                 struct eata_config *cf;
1240                 dma_addr_t cf_dma_addr;
1241
1242                 cf = pci_alloc_consistent(pdev, sizeof(struct eata_config),
1243                                           &cf_dma_addr);
1244
1245                 if (!cf) {
1246                         printk
1247                             ("%s: config, pci_alloc_consistent failed, detaching.\n",
1248                              name);
1249                         goto freedma;
1250                 }
1251
1252                 /* Set board configuration */
1253                 memset((char *)cf, 0, sizeof(struct eata_config));
1254                 cf->len = (ushort) H2DEV16((ushort) 510);
1255                 cf->ocena = 1;
1256
1257                 if (do_dma(port_base, cf_dma_addr, SET_CONFIG_DMA)) {
1258                         printk
1259                             ("%s: busy timeout sending configuration, detaching.\n",
1260                              name);
1261                         pci_free_consistent(pdev, sizeof(struct eata_config),
1262                                             cf, cf_dma_addr);
1263                         goto freedma;
1264                 }
1265
1266         }
1267 #endif
1268
1269         spin_unlock_irq(&driver_lock);
1270         sh[j] = shost = scsi_register(tpnt, sizeof(struct hostdata));
1271         spin_lock_irq(&driver_lock);
1272
1273         if (shost == NULL) {
1274                 printk("%s: unable to register host, detaching.\n", name);
1275                 goto freedma;
1276         }
1277
1278         shost->io_port = port_base;
1279         shost->unique_id = port_base;
1280         shost->n_io_port = REGION_SIZE;
1281         shost->dma_channel = dma_channel;
1282         shost->irq = irq;
1283         shost->sg_tablesize = (ushort) info.scatt_size;
1284         shost->this_id = (ushort) info.host_addr[3];
1285         shost->can_queue = (ushort) info.queue_size;
1286         shost->cmd_per_lun = MAX_CMD_PER_LUN;
1287
1288         ha = (struct hostdata *)shost->hostdata;
1289         
1290         memset(ha, 0, sizeof(struct hostdata));
1291         ha->subversion = subversion;
1292         ha->protocol_rev = protocol_rev;
1293         ha->is_pci = is_pci;
1294         ha->pdev = pdev;
1295         ha->board_number = j;
1296
1297         if (ha->subversion == ESA)
1298                 shost->unchecked_isa_dma = 0;
1299         else {
1300                 unsigned long flags;
1301                 shost->unchecked_isa_dma = 1;
1302
1303                 flags = claim_dma_lock();
1304                 disable_dma(dma_channel);
1305                 clear_dma_ff(dma_channel);
1306                 set_dma_mode(dma_channel, DMA_MODE_CASCADE);
1307                 enable_dma(dma_channel);
1308                 release_dma_lock(flags);
1309
1310         }
1311
1312         strcpy(ha->board_name, name);
1313
1314         /* DPT PM2012 does not allow to detect sg_tablesize correctly */
1315         if (shost->sg_tablesize > MAX_SGLIST || shost->sg_tablesize < 2) {
1316                 printk("%s: detect, wrong n. of SG lists %d, fixed.\n",
1317                        ha->board_name, shost->sg_tablesize);
1318                 shost->sg_tablesize = MAX_SGLIST;
1319         }
1320
1321         /* DPT PM2012 does not allow to detect can_queue correctly */
1322         if (shost->can_queue > MAX_MAILBOXES || shost->can_queue < 2) {
1323                 printk("%s: detect, wrong n. of mbox %d, fixed.\n",
1324                        ha->board_name, shost->can_queue);
1325                 shost->can_queue = MAX_MAILBOXES;
1326         }
1327
1328         if (protocol_rev != 'A') {
1329                 if (info.max_chan > 0 && info.max_chan < MAX_CHANNEL)
1330                         shost->max_channel = info.max_chan;
1331
1332                 if (info.max_id > 7 && info.max_id < MAX_TARGET)
1333                         shost->max_id = info.max_id + 1;
1334
1335                 if (info.large_sg && shost->sg_tablesize == MAX_SGLIST)
1336                         shost->sg_tablesize = MAX_LARGE_SGLIST;
1337         }
1338
1339         if (protocol_rev == 'C') {
1340                 if (info.max_lun > 7 && info.max_lun < MAX_LUN)
1341                         shost->max_lun = info.max_lun + 1;
1342         }
1343
1344         if (dma_channel == NO_DMA)
1345                 sprintf(dma_name, "%s", "BMST");
1346         else
1347                 sprintf(dma_name, "DMA %u", dma_channel);
1348
1349         spin_unlock_irq(&driver_lock);
1350
1351         for (i = 0; i < shost->can_queue; i++)
1352                 ha->cp[i].cp_dma_addr = pci_map_single(ha->pdev,
1353                                                           &ha->cp[i],
1354                                                           sizeof(struct mscp),
1355                                                           PCI_DMA_BIDIRECTIONAL);
1356
1357         for (i = 0; i < shost->can_queue; i++) {
1358                 size_t sz = shost->sg_tablesize *sizeof(struct sg_list);
1359                 gfp_t gfp_mask = (shost->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC;
1360                 ha->cp[i].sglist = kmalloc(sz, gfp_mask);
1361                 if (!ha->cp[i].sglist) {
1362                         printk
1363                             ("%s: kmalloc SGlist failed, mbox %d, detaching.\n",
1364                              ha->board_name, i);
1365                         goto release;
1366                 }
1367         }
1368
1369         if (!(ha->sp_cpu_addr = pci_alloc_consistent(ha->pdev,
1370                                                         sizeof(struct mssp),
1371                                                         &ha->sp_dma_addr))) {
1372                 printk("%s: pci_alloc_consistent failed, detaching.\n", ha->board_name);
1373                 goto release;
1374         }
1375
1376         if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
1377                 max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
1378
1379         if (max_queue_depth < MAX_CMD_PER_LUN)
1380                 max_queue_depth = MAX_CMD_PER_LUN;
1381
1382         if (tag_mode != TAG_DISABLED && tag_mode != TAG_SIMPLE)
1383                 tag_mode = TAG_ORDERED;
1384
1385         if (j == 0) {
1386                 printk
1387                     ("EATA/DMA 2.0x: Copyright (C) 1994-2003 Dario Ballabio.\n");
1388                 printk
1389                     ("%s config options -> tm:%d, lc:%c, mq:%d, rs:%c, et:%c, "
1390                      "ip:%c, ep:%c, pp:%c.\n", driver_name, tag_mode,
1391                      YESNO(linked_comm), max_queue_depth, YESNO(rev_scan),
1392                      YESNO(ext_tran), YESNO(isa_probe), YESNO(eisa_probe),
1393                      YESNO(pci_probe));
1394         }
1395
1396         printk("%s: 2.0%c, %s 0x%03lx, IRQ %u, %s, SG %d, MB %d.\n",
1397                ha->board_name, ha->protocol_rev, bus_type,
1398                (unsigned long)shost->io_port, shost->irq, dma_name,
1399                shost->sg_tablesize, shost->can_queue);
1400
1401         if (shost->max_id > 8 || shost->max_lun > 8)
1402                 printk
1403                     ("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
1404                      ha->board_name, shost->max_id, shost->max_lun);
1405
1406         for (i = 0; i <= shost->max_channel; i++)
1407                 printk("%s: SCSI channel %u enabled, host target ID %d.\n",
1408                        ha->board_name, i, info.host_addr[3 - i]);
1409
1410 #if defined(DEBUG_DETECT)
1411         printk("%s: Vers. 0x%x, ocs %u, tar %u, trnxfr %u, more %u, SYNC 0x%x, "
1412                "sec. %u, infol %d, cpl %d spl %d.\n", name, info.version,
1413                info.ocsena, info.tarsup, info.trnxfr, info.morsup, info.sync,
1414                info.second, info.data_len, info.cp_len, info.sp_len);
1415
1416         if (protocol_rev == 'B' || protocol_rev == 'C')
1417                 printk("%s: isaena %u, forcaddr %u, max_id %u, max_chan %u, "
1418                        "large_sg %u, res1 %u.\n", name, info.isaena,
1419                        info.forcaddr, info.max_id, info.max_chan, info.large_sg,
1420                        info.res1);
1421
1422         if (protocol_rev == 'C')
1423                 printk("%s: max_lun %u, m1 %u, idquest %u, pci %u, eisa %u, "
1424                        "raidnum %u.\n", name, info.max_lun, info.m1,
1425                        info.idquest, info.pci, info.eisa, info.raidnum);
1426 #endif
1427
1428         if (ha->pdev) {
1429                 pci_set_master(ha->pdev);
1430                 if (pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK))
1431                         printk("%s: warning, pci_set_dma_mask failed.\n",
1432                                ha->board_name);
1433         }
1434
1435         return 1;
1436
1437       freedma:
1438         if (subversion == ISA)
1439                 free_dma(dma_channel);
1440       freeirq:
1441         free_irq(irq, &sha[j]);
1442       freelock:
1443         spin_unlock_irq(&driver_lock);
1444         release_region(port_base, REGION_SIZE);
1445       fail:
1446         return 0;
1447
1448       release:
1449         eata2x_release(shost);
1450         return 0;
1451 }
1452
1453 static void internal_setup(char *str, int *ints)
1454 {
1455         int i, argc = ints[0];
1456         char *cur = str, *pc;
1457
1458         if (argc > 0) {
1459                 if (argc > MAX_INT_PARAM)
1460                         argc = MAX_INT_PARAM;
1461
1462                 for (i = 0; i < argc; i++)
1463                         io_port[i] = ints[i + 1];
1464
1465                 io_port[i] = 0;
1466                 setup_done = 1;
1467         }
1468
1469         while (cur && (pc = strchr(cur, ':'))) {
1470                 int val = 0, c = *++pc;
1471
1472                 if (c == 'n' || c == 'N')
1473                         val = 0;
1474                 else if (c == 'y' || c == 'Y')
1475                         val = 1;
1476                 else
1477                         val = (int)simple_strtoul(pc, NULL, 0);
1478
1479                 if (!strncmp(cur, "lc:", 3))
1480                         linked_comm = val;
1481                 else if (!strncmp(cur, "tm:", 3))
1482                         tag_mode = val;
1483                 else if (!strncmp(cur, "tc:", 3))
1484                         tag_mode = val;
1485                 else if (!strncmp(cur, "mq:", 3))
1486                         max_queue_depth = val;
1487                 else if (!strncmp(cur, "ls:", 3))
1488                         link_statistics = val;
1489                 else if (!strncmp(cur, "et:", 3))
1490                         ext_tran = val;
1491                 else if (!strncmp(cur, "rs:", 3))
1492                         rev_scan = val;
1493                 else if (!strncmp(cur, "ip:", 3))
1494                         isa_probe = val;
1495                 else if (!strncmp(cur, "ep:", 3))
1496                         eisa_probe = val;
1497                 else if (!strncmp(cur, "pp:", 3))
1498                         pci_probe = val;
1499
1500                 if ((cur = strchr(cur, ',')))
1501                         ++cur;
1502         }
1503
1504         return;
1505 }
1506
1507 static int option_setup(char *str)
1508 {
1509         int ints[MAX_INT_PARAM];
1510         char *cur = str;
1511         int i = 1;
1512
1513         while (cur && isdigit(*cur) && i <= MAX_INT_PARAM) {
1514                 ints[i++] = simple_strtoul(cur, NULL, 0);
1515
1516                 if ((cur = strchr(cur, ',')) != NULL)
1517                         cur++;
1518         }
1519
1520         ints[0] = i - 1;
1521         internal_setup(cur, ints);
1522         return 1;
1523 }
1524
1525 static void add_pci_ports(void)
1526 {
1527 #if defined(CONFIG_PCI)
1528         unsigned int addr, k;
1529         struct pci_dev *dev = NULL;
1530
1531         for (k = 0; k < MAX_PCI; k++) {
1532
1533                 if (!(dev = pci_get_class(PCI_CLASS_STORAGE_SCSI << 8, dev)))
1534                         break;
1535
1536                 if (pci_enable_device(dev)) {
1537 #if defined(DEBUG_PCI_DETECT)
1538                         printk
1539                             ("%s: detect, bus %d, devfn 0x%x, pci_enable_device failed.\n",
1540                              driver_name, dev->bus->number, dev->devfn);
1541 #endif
1542
1543                         continue;
1544                 }
1545
1546                 addr = pci_resource_start(dev, 0);
1547
1548 #if defined(DEBUG_PCI_DETECT)
1549                 printk("%s: detect, seq. %d, bus %d, devfn 0x%x, addr 0x%x.\n",
1550                        driver_name, k, dev->bus->number, dev->devfn, addr);
1551 #endif
1552
1553                 /* Order addresses according to rev_scan value */
1554                 io_port[MAX_INT_PARAM + (rev_scan ? (MAX_PCI - k) : (1 + k))] =
1555                     addr + PCI_BASE_ADDRESS_0;
1556         }
1557
1558         pci_dev_put(dev);
1559 #endif                          /* end CONFIG_PCI */
1560 }
1561
1562 static int eata2x_detect(struct scsi_host_template *tpnt)
1563 {
1564         unsigned int j = 0, k;
1565
1566         tpnt->proc_name = "eata2x";
1567
1568         if (strlen(boot_options))
1569                 option_setup(boot_options);
1570
1571 #if defined(MODULE)
1572         /* io_port could have been modified when loading as a module */
1573         if (io_port[0] != SKIP) {
1574                 setup_done = 1;
1575                 io_port[MAX_INT_PARAM] = 0;
1576         }
1577 #endif
1578
1579         for (k = MAX_INT_PARAM; io_port[k]; k++)
1580                 if (io_port[k] == SKIP)
1581                         continue;
1582                 else if (io_port[k] <= MAX_ISA_ADDR) {
1583                         if (!isa_probe)
1584                                 io_port[k] = SKIP;
1585                 } else if (io_port[k] >= MIN_EISA_ADDR
1586                            && io_port[k] <= MAX_EISA_ADDR) {
1587                         if (!eisa_probe)
1588                                 io_port[k] = SKIP;
1589                 }
1590
1591         if (pci_probe) {
1592                 if (!setup_done)
1593                         add_pci_ports();
1594                 else
1595                         enable_pci_ports();
1596         }
1597
1598         for (k = 0; io_port[k]; k++) {
1599
1600                 if (io_port[k] == SKIP)
1601                         continue;
1602
1603                 if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt))
1604                         j++;
1605         }
1606
1607         num_boards = j;
1608         return j;
1609 }
1610
1611 static void map_dma(unsigned int i, struct hostdata *ha)
1612 {
1613         unsigned int k, pci_dir;
1614         int count;
1615         struct scatterlist *sg;
1616         struct mscp *cpp;
1617         struct scsi_cmnd *SCpnt;
1618
1619         cpp = &ha->cp[i];
1620         SCpnt = cpp->SCpnt;
1621         pci_dir = SCpnt->sc_data_direction;
1622
1623         if (SCpnt->sense_buffer)
1624                 cpp->sense_addr =
1625                     H2DEV(pci_map_single(ha->pdev, SCpnt->sense_buffer,
1626                            SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE));
1627
1628         cpp->sense_len = SCSI_SENSE_BUFFERSIZE;
1629
1630         count = scsi_dma_map(SCpnt);
1631         BUG_ON(count < 0);
1632         scsi_for_each_sg(SCpnt, sg, count, k) {
1633                 cpp->sglist[k].address = H2DEV(sg_dma_address(sg));
1634                 cpp->sglist[k].num_bytes = H2DEV(sg_dma_len(sg));
1635         }
1636
1637         cpp->sg = 1;
1638         cpp->data_address = H2DEV(pci_map_single(ha->pdev, cpp->sglist,
1639                                                  scsi_sg_count(SCpnt) *
1640                                                  sizeof(struct sg_list),
1641                                                  pci_dir));
1642         cpp->data_len = H2DEV((scsi_sg_count(SCpnt) * sizeof(struct sg_list)));
1643 }
1644
1645 static void unmap_dma(unsigned int i, struct hostdata *ha)
1646 {
1647         unsigned int pci_dir;
1648         struct mscp *cpp;
1649         struct scsi_cmnd *SCpnt;
1650
1651         cpp = &ha->cp[i];
1652         SCpnt = cpp->SCpnt;
1653         pci_dir = SCpnt->sc_data_direction;
1654
1655         if (DEV2H(cpp->sense_addr))
1656                 pci_unmap_single(ha->pdev, DEV2H(cpp->sense_addr),
1657                                  DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
1658
1659         scsi_dma_unmap(SCpnt);
1660
1661         if (!DEV2H(cpp->data_len))
1662                 pci_dir = PCI_DMA_BIDIRECTIONAL;
1663
1664         if (DEV2H(cpp->data_address))
1665                 pci_unmap_single(ha->pdev, DEV2H(cpp->data_address),
1666                                  DEV2H(cpp->data_len), pci_dir);
1667 }
1668
1669 static void sync_dma(unsigned int i, struct hostdata *ha)
1670 {
1671         unsigned int pci_dir;
1672         struct mscp *cpp;
1673         struct scsi_cmnd *SCpnt;
1674
1675         cpp = &ha->cp[i];
1676         SCpnt = cpp->SCpnt;
1677         pci_dir = SCpnt->sc_data_direction;
1678
1679         if (DEV2H(cpp->sense_addr))
1680                 pci_dma_sync_single_for_cpu(ha->pdev, DEV2H(cpp->sense_addr),
1681                                             DEV2H(cpp->sense_len),
1682                                             PCI_DMA_FROMDEVICE);
1683
1684         if (scsi_sg_count(SCpnt))
1685                 pci_dma_sync_sg_for_cpu(ha->pdev, scsi_sglist(SCpnt),
1686                                         scsi_sg_count(SCpnt), pci_dir);
1687
1688         if (!DEV2H(cpp->data_len))
1689                 pci_dir = PCI_DMA_BIDIRECTIONAL;
1690
1691         if (DEV2H(cpp->data_address))
1692                 pci_dma_sync_single_for_cpu(ha->pdev,
1693                                             DEV2H(cpp->data_address),
1694                                             DEV2H(cpp->data_len), pci_dir);
1695 }
1696
1697 static void scsi_to_dev_dir(unsigned int i, struct hostdata *ha)
1698 {
1699         unsigned int k;
1700
1701         static const unsigned char data_out_cmds[] = {
1702                 0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
1703                 0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
1704                 0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b, 0x5d
1705         };
1706
1707         static const unsigned char data_none_cmds[] = {
1708                 0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
1709                 0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
1710                 0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5, 0x00
1711         };
1712
1713         struct mscp *cpp;
1714         struct scsi_cmnd *SCpnt;
1715
1716         cpp = &ha->cp[i];
1717         SCpnt = cpp->SCpnt;
1718
1719         if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
1720                 cpp->din = 1;
1721                 cpp->dout = 0;
1722                 return;
1723         } else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
1724                 cpp->din = 0;
1725                 cpp->dout = 1;
1726                 return;
1727         } else if (SCpnt->sc_data_direction == DMA_NONE) {
1728                 cpp->din = 0;
1729                 cpp->dout = 0;
1730                 return;
1731         }
1732
1733         if (SCpnt->sc_data_direction != DMA_BIDIRECTIONAL)
1734                 panic("%s: qcomm, invalid SCpnt->sc_data_direction.\n",
1735                                 ha->board_name);
1736
1737         for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
1738                 if (SCpnt->cmnd[0] == data_out_cmds[k]) {
1739                         cpp->dout = 1;
1740                         break;
1741                 }
1742
1743         if ((cpp->din = !cpp->dout))
1744                 for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
1745                         if (SCpnt->cmnd[0] == data_none_cmds[k]) {
1746                                 cpp->din = 0;
1747                                 break;
1748                         }
1749
1750 }
1751
1752 static int eata2x_queuecommand(struct scsi_cmnd *SCpnt,
1753                                void (*done) (struct scsi_cmnd *))
1754 {
1755         struct Scsi_Host *shost = SCpnt->device->host;
1756         struct hostdata *ha = (struct hostdata *)shost->hostdata;
1757         unsigned int i, k;
1758         struct mscp *cpp;
1759
1760         if (SCpnt->host_scribble)
1761                 panic("%s: qcomm, pid %ld, SCpnt %p already active.\n",
1762                       ha->board_name, SCpnt->serial_number, SCpnt);
1763
1764         /* i is the mailbox number, look for the first free mailbox
1765            starting from last_cp_used */
1766         i = ha->last_cp_used + 1;
1767
1768         for (k = 0; k < shost->can_queue; k++, i++) {
1769                 if (i >= shost->can_queue)
1770                         i = 0;
1771                 if (ha->cp_stat[i] == FREE) {
1772                         ha->last_cp_used = i;
1773                         break;
1774                 }
1775         }
1776
1777         if (k == shost->can_queue) {
1778                 printk("%s: qcomm, no free mailbox.\n", ha->board_name);
1779                 return 1;
1780         }
1781
1782         /* Set pointer to control packet structure */
1783         cpp = &ha->cp[i];
1784
1785         memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
1786
1787         /* Set pointer to status packet structure, Big Endian format */
1788         cpp->sp_dma_addr = H2DEV(ha->sp_dma_addr);
1789
1790         SCpnt->scsi_done = done;
1791         cpp->cpp_index = i;
1792         SCpnt->host_scribble = (unsigned char *)&cpp->cpp_index;
1793
1794         if (do_trace)
1795                 scmd_printk(KERN_INFO, SCpnt,
1796                         "qcomm, mbox %d, pid %ld.\n", i, SCpnt->serial_number);
1797
1798         cpp->reqsen = 1;
1799         cpp->dispri = 1;
1800 #if 0
1801         if (SCpnt->device->type == TYPE_TAPE)
1802                 cpp->hbaci = 1;
1803 #endif
1804         cpp->one = 1;
1805         cpp->channel = SCpnt->device->channel;
1806         cpp->target = SCpnt->device->id;
1807         cpp->lun = SCpnt->device->lun;
1808         cpp->SCpnt = SCpnt;
1809         memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
1810
1811         /* Use data transfer direction SCpnt->sc_data_direction */
1812         scsi_to_dev_dir(i, ha);
1813
1814         /* Map DMA buffers and SG list */
1815         map_dma(i, ha);
1816
1817         if (linked_comm && SCpnt->device->queue_depth > 2
1818             && TLDEV(SCpnt->device->type)) {
1819                 ha->cp_stat[i] = READY;
1820                 flush_dev(SCpnt->device, SCpnt->request->sector, ha, 0);
1821                 return 0;
1822         }
1823
1824         /* Send control packet to the board */
1825         if (do_dma(shost->io_port, cpp->cp_dma_addr, SEND_CP_DMA)) {
1826                 unmap_dma(i, ha);
1827                 SCpnt->host_scribble = NULL;
1828                 scmd_printk(KERN_INFO, SCpnt,
1829                         "qcomm, pid %ld, adapter busy.\n", SCpnt->serial_number);
1830                 return 1;
1831         }
1832
1833         ha->cp_stat[i] = IN_USE;
1834         return 0;
1835 }
1836
1837 static int eata2x_eh_abort(struct scsi_cmnd *SCarg)
1838 {
1839         struct Scsi_Host *shost = SCarg->device->host;
1840         struct hostdata *ha = (struct hostdata *)shost->hostdata;
1841         unsigned int i;
1842
1843         if (SCarg->host_scribble == NULL) {
1844                 scmd_printk(KERN_INFO, SCarg,
1845                         "abort, pid %ld inactive.\n", SCarg->serial_number);
1846                 return SUCCESS;
1847         }
1848
1849         i = *(unsigned int *)SCarg->host_scribble;
1850         scmd_printk(KERN_WARNING, SCarg,
1851                 "abort, mbox %d, pid %ld.\n", i, SCarg->serial_number);
1852
1853         if (i >= shost->can_queue)
1854                 panic("%s: abort, invalid SCarg->host_scribble.\n", ha->board_name);
1855
1856         if (wait_on_busy(shost->io_port, MAXLOOP)) {
1857                 printk("%s: abort, timeout error.\n", ha->board_name);
1858                 return FAILED;
1859         }
1860
1861         if (ha->cp_stat[i] == FREE) {
1862                 printk("%s: abort, mbox %d is free.\n", ha->board_name, i);
1863                 return SUCCESS;
1864         }
1865
1866         if (ha->cp_stat[i] == IN_USE) {
1867                 printk("%s: abort, mbox %d is in use.\n", ha->board_name, i);
1868
1869                 if (SCarg != ha->cp[i].SCpnt)
1870                         panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1871                               ha->board_name, i, SCarg, ha->cp[i].SCpnt);
1872
1873                 if (inb(shost->io_port + REG_AUX_STATUS) & IRQ_ASSERTED)
1874                         printk("%s: abort, mbox %d, interrupt pending.\n",
1875                                ha->board_name, i);
1876
1877                 return FAILED;
1878         }
1879
1880         if (ha->cp_stat[i] == IN_RESET) {
1881                 printk("%s: abort, mbox %d is in reset.\n", ha->board_name, i);
1882                 return FAILED;
1883         }
1884
1885         if (ha->cp_stat[i] == LOCKED) {
1886                 printk("%s: abort, mbox %d is locked.\n", ha->board_name, i);
1887                 return SUCCESS;
1888         }
1889
1890         if (ha->cp_stat[i] == READY || ha->cp_stat[i] == ABORTING) {
1891                 unmap_dma(i, ha);
1892                 SCarg->result = DID_ABORT << 16;
1893                 SCarg->host_scribble = NULL;
1894                 ha->cp_stat[i] = FREE;
1895                 printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1896                        ha->board_name, i, SCarg->serial_number);
1897                 SCarg->scsi_done(SCarg);
1898                 return SUCCESS;
1899         }
1900
1901         panic("%s: abort, mbox %d, invalid cp_stat.\n", ha->board_name, i);
1902 }
1903
1904 static int eata2x_eh_host_reset(struct scsi_cmnd *SCarg)
1905 {
1906         unsigned int i, time, k, c, limit = 0;
1907         int arg_done = 0;
1908         struct scsi_cmnd *SCpnt;
1909         struct Scsi_Host *shost = SCarg->device->host;
1910         struct hostdata *ha = (struct hostdata *)shost->hostdata;
1911
1912         scmd_printk(KERN_INFO, SCarg,
1913                 "reset, enter, pid %ld.\n", SCarg->serial_number);
1914
1915         spin_lock_irq(shost->host_lock);
1916
1917         if (SCarg->host_scribble == NULL)
1918                 printk("%s: reset, pid %ld inactive.\n", ha->board_name, SCarg->serial_number);
1919
1920         if (ha->in_reset) {
1921                 printk("%s: reset, exit, already in reset.\n", ha->board_name);
1922                 spin_unlock_irq(shost->host_lock);
1923                 return FAILED;
1924         }
1925
1926         if (wait_on_busy(shost->io_port, MAXLOOP)) {
1927                 printk("%s: reset, exit, timeout error.\n", ha->board_name);
1928                 spin_unlock_irq(shost->host_lock);
1929                 return FAILED;
1930         }
1931
1932         ha->retries = 0;
1933
1934         for (c = 0; c <= shost->max_channel; c++)
1935                 for (k = 0; k < shost->max_id; k++) {
1936                         ha->target_redo[k][c] = 1;
1937                         ha->target_to[k][c] = 0;
1938                 }
1939
1940         for (i = 0; i < shost->can_queue; i++) {
1941
1942                 if (ha->cp_stat[i] == FREE)
1943                         continue;
1944
1945                 if (ha->cp_stat[i] == LOCKED) {
1946                         ha->cp_stat[i] = FREE;
1947                         printk("%s: reset, locked mbox %d forced free.\n",
1948                                ha->board_name, i);
1949                         continue;
1950                 }
1951
1952                 if (!(SCpnt = ha->cp[i].SCpnt))
1953                         panic("%s: reset, mbox %d, SCpnt == NULL.\n", ha->board_name, i);
1954
1955                 if (ha->cp_stat[i] == READY || ha->cp_stat[i] == ABORTING) {
1956                         ha->cp_stat[i] = ABORTING;
1957                         printk("%s: reset, mbox %d aborting, pid %ld.\n",
1958                                ha->board_name, i, SCpnt->serial_number);
1959                 }
1960
1961                 else {
1962                         ha->cp_stat[i] = IN_RESET;
1963                         printk("%s: reset, mbox %d in reset, pid %ld.\n",
1964                                ha->board_name, i, SCpnt->serial_number);
1965                 }
1966
1967                 if (SCpnt->host_scribble == NULL)
1968                         panic("%s: reset, mbox %d, garbled SCpnt.\n", ha->board_name, i);
1969
1970                 if (*(unsigned int *)SCpnt->host_scribble != i)
1971                         panic("%s: reset, mbox %d, index mismatch.\n", ha->board_name, i);
1972
1973                 if (SCpnt->scsi_done == NULL)
1974                         panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n",
1975                               ha->board_name, i);
1976
1977                 if (SCpnt == SCarg)
1978                         arg_done = 1;
1979         }
1980
1981         if (do_dma(shost->io_port, 0, RESET_PIO)) {
1982                 printk("%s: reset, cannot reset, timeout error.\n", ha->board_name);
1983                 spin_unlock_irq(shost->host_lock);
1984                 return FAILED;
1985         }
1986
1987         printk("%s: reset, board reset done, enabling interrupts.\n", ha->board_name);
1988
1989 #if defined(DEBUG_RESET)
1990         do_trace = 1;
1991 #endif
1992
1993         ha->in_reset = 1;
1994
1995         spin_unlock_irq(shost->host_lock);
1996
1997         /* FIXME: use a sleep instead */
1998         time = jiffies;
1999         while ((jiffies - time) < (10 * HZ) && limit++ < 200000)
2000                 udelay(100L);
2001
2002         spin_lock_irq(shost->host_lock);
2003
2004         printk("%s: reset, interrupts disabled, loops %d.\n", ha->board_name, limit);
2005
2006         for (i = 0; i < shost->can_queue; i++) {
2007
2008                 if (ha->cp_stat[i] == IN_RESET) {
2009                         SCpnt = ha->cp[i].SCpnt;
2010                         unmap_dma(i, ha);
2011                         SCpnt->result = DID_RESET << 16;
2012                         SCpnt->host_scribble = NULL;
2013
2014                         /* This mailbox is still waiting for its interrupt */
2015                         ha->cp_stat[i] = LOCKED;
2016
2017                         printk
2018                             ("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
2019                              ha->board_name, i, SCpnt->serial_number);
2020                 }
2021
2022                 else if (ha->cp_stat[i] == ABORTING) {
2023                         SCpnt = ha->cp[i].SCpnt;
2024                         unmap_dma(i, ha);
2025                         SCpnt->result = DID_RESET << 16;
2026                         SCpnt->host_scribble = NULL;
2027
2028                         /* This mailbox was never queued to the adapter */
2029                         ha->cp_stat[i] = FREE;
2030
2031                         printk
2032                             ("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
2033                              ha->board_name, i, SCpnt->serial_number);
2034                 }
2035
2036                 else
2037                         /* Any other mailbox has already been set free by interrupt */
2038                         continue;
2039
2040                 SCpnt->scsi_done(SCpnt);
2041         }
2042
2043         ha->in_reset = 0;
2044         do_trace = 0;
2045
2046         if (arg_done)
2047                 printk("%s: reset, exit, pid %ld done.\n", ha->board_name, SCarg->serial_number);
2048         else
2049                 printk("%s: reset, exit.\n", ha->board_name);
2050
2051         spin_unlock_irq(shost->host_lock);
2052         return SUCCESS;
2053 }
2054
2055 int eata2x_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2056                       sector_t capacity, int *dkinfo)
2057 {
2058         unsigned int size = capacity;
2059
2060         if (ext_tran || (scsicam_bios_param(bdev, capacity, dkinfo) < 0)) {
2061                 dkinfo[0] = 255;
2062                 dkinfo[1] = 63;
2063                 dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
2064         }
2065 #if defined (DEBUG_GEOMETRY)
2066         printk("%s: bios_param, head=%d, sec=%d, cyl=%d.\n", driver_name,
2067                dkinfo[0], dkinfo[1], dkinfo[2]);
2068 #endif
2069
2070         return 0;
2071 }
2072
2073 static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
2074                  unsigned int rev)
2075 {
2076         unsigned int i, j, k, y;
2077         unsigned long x;
2078
2079         for (i = 0; i < n - 1; i++) {
2080                 k = i;
2081
2082                 for (j = k + 1; j < n; j++)
2083                         if (rev) {
2084                                 if (sk[j] > sk[k])
2085                                         k = j;
2086                         } else {
2087                                 if (sk[j] < sk[k])
2088                                         k = j;
2089                         }
2090
2091                 if (k != i) {
2092                         x = sk[k];
2093                         sk[k] = sk[i];
2094                         sk[i] = x;
2095                         y = da[k];
2096                         da[k] = da[i];
2097                         da[i] = y;
2098                 }
2099         }
2100
2101         return;
2102 }
2103
2104 static int reorder(struct hostdata *ha, unsigned long cursec,
2105                    unsigned int ihdlr, unsigned int il[], unsigned int n_ready)
2106 {
2107         struct scsi_cmnd *SCpnt;
2108         struct mscp *cpp;
2109         unsigned int k, n;
2110         unsigned int rev = 0, s = 1, r = 1;
2111         unsigned int input_only = 1, overlap = 0;
2112         unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
2113         unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
2114         unsigned long ioseek = 0;
2115
2116         static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
2117         static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
2118         static unsigned int readysorted = 0, revcount = 0;
2119         static unsigned long seeksorted = 0, seeknosort = 0;
2120
2121         if (link_statistics && !(++flushcount % link_statistics))
2122                 printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"
2123                        " av %ldK as %ldK.\n", flushcount, batchcount,
2124                        inputcount, ovlcount, readycount, readysorted, sortcount,
2125                        revcount, seeknosort / (readycount + 1),
2126                        seeksorted / (readycount + 1));
2127
2128         if (n_ready <= 1)
2129                 return 0;
2130
2131         for (n = 0; n < n_ready; n++) {
2132                 k = il[n];
2133                 cpp = &ha->cp[k];
2134                 SCpnt = cpp->SCpnt;
2135
2136                 if (!cpp->din)
2137                         input_only = 0;
2138
2139                 if (SCpnt->request->sector < minsec)
2140                         minsec = SCpnt->request->sector;
2141                 if (SCpnt->request->sector > maxsec)
2142                         maxsec = SCpnt->request->sector;
2143
2144                 sl[n] = SCpnt->request->sector;
2145                 ioseek += SCpnt->request->nr_sectors;
2146
2147                 if (!n)
2148                         continue;
2149
2150                 if (sl[n] < sl[n - 1])
2151                         s = 0;
2152                 if (sl[n] > sl[n - 1])
2153                         r = 0;
2154
2155                 if (link_statistics) {
2156                         if (sl[n] > sl[n - 1])
2157                                 seek += sl[n] - sl[n - 1];
2158                         else
2159                                 seek += sl[n - 1] - sl[n];
2160                 }
2161
2162         }
2163
2164         if (link_statistics) {
2165                 if (cursec > sl[0])
2166                         seek += cursec - sl[0];
2167                 else
2168                         seek += sl[0] - cursec;
2169         }
2170
2171         if (cursec > ((maxsec + minsec) / 2))
2172                 rev = 1;
2173
2174         if (ioseek > ((maxsec - minsec) / 2))
2175                 rev = 0;
2176
2177         if (!((rev && r) || (!rev && s)))
2178                 sort(sl, il, n_ready, rev);
2179
2180         if (!input_only)
2181                 for (n = 0; n < n_ready; n++) {
2182                         k = il[n];
2183                         cpp = &ha->cp[k];
2184                         SCpnt = cpp->SCpnt;
2185                         ll[n] = SCpnt->request->nr_sectors;
2186                         pl[n] = SCpnt->serial_number;
2187
2188                         if (!n)
2189                                 continue;
2190
2191                         if ((sl[n] == sl[n - 1])
2192                             || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
2193                             || (rev && ((sl[n] + ll[n]) > sl[n - 1])))
2194                                 overlap = 1;
2195                 }
2196
2197         if (overlap)
2198                 sort(pl, il, n_ready, 0);
2199
2200         if (link_statistics) {
2201                 if (cursec > sl[0])
2202                         iseek = cursec - sl[0];
2203                 else
2204                         iseek = sl[0] - cursec;
2205                 batchcount++;
2206                 readycount += n_ready;
2207                 seeknosort += seek / 1024;
2208                 if (input_only)
2209                         inputcount++;
2210                 if (overlap) {
2211                         ovlcount++;
2212                         seeksorted += iseek / 1024;
2213                 } else
2214                         seeksorted += (iseek + maxsec - minsec) / 1024;
2215                 if (rev && !r) {
2216                         revcount++;
2217                         readysorted += n_ready;
2218                 }
2219                 if (!rev && !s) {
2220                         sortcount++;
2221                         readysorted += n_ready;
2222                 }
2223         }
2224 #if defined(DEBUG_LINKED_COMMANDS)
2225         if (link_statistics && (overlap || !(flushcount % link_statistics)))
2226                 for (n = 0; n < n_ready; n++) {
2227                         k = il[n];
2228                         cpp = &ha->cp[k];
2229                         SCpnt = cpp->SCpnt;
2230                         scmd_printk(KERN_INFO, SCpnt,
2231                             "%s pid %ld mb %d fc %d nr %d sec %ld ns %ld"
2232                              " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
2233                              (ihdlr ? "ihdlr" : "qcomm"),
2234                              SCpnt->serial_number, k, flushcount,
2235                              n_ready, SCpnt->request->sector,
2236                              SCpnt->request->nr_sectors, cursec, YESNO(s),
2237                              YESNO(r), YESNO(rev), YESNO(input_only),
2238                              YESNO(overlap), cpp->din);
2239                 }
2240 #endif
2241         return overlap;
2242 }
2243
2244 static void flush_dev(struct scsi_device *dev, unsigned long cursec,
2245                       struct hostdata *ha, unsigned int ihdlr)
2246 {
2247         struct scsi_cmnd *SCpnt;
2248         struct mscp *cpp;
2249         unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
2250
2251         for (k = 0; k < dev->host->can_queue; k++) {
2252
2253                 if (ha->cp_stat[k] != READY && ha->cp_stat[k] != IN_USE)
2254                         continue;
2255
2256                 cpp = &ha->cp[k];
2257                 SCpnt = cpp->SCpnt;
2258
2259                 if (SCpnt->device != dev)
2260                         continue;
2261
2262                 if (ha->cp_stat[k] == IN_USE)
2263                         return;
2264
2265                 il[n_ready++] = k;
2266         }
2267
2268         if (reorder(ha, cursec, ihdlr, il, n_ready))
2269                 n_ready = 1;
2270
2271         for (n = 0; n < n_ready; n++) {
2272                 k = il[n];
2273                 cpp = &ha->cp[k];
2274                 SCpnt = cpp->SCpnt;
2275
2276                 if (do_dma(dev->host->io_port, cpp->cp_dma_addr, SEND_CP_DMA)) {
2277                         scmd_printk(KERN_INFO, SCpnt,
2278                             "%s, pid %ld, mbox %d, adapter"
2279                              " busy, will abort.\n",
2280                              (ihdlr ? "ihdlr" : "qcomm"),
2281                              SCpnt->serial_number, k);
2282                         ha->cp_stat[k] = ABORTING;
2283                         continue;
2284                 }
2285
2286                 ha->cp_stat[k] = IN_USE;
2287         }
2288 }
2289
2290 static irqreturn_t ihdlr(int irq, struct Scsi_Host *shost)
2291 {
2292         struct scsi_cmnd *SCpnt;
2293         unsigned int i, k, c, status, tstatus, reg;
2294         struct mssp *spp;
2295         struct mscp *cpp;
2296         struct hostdata *ha = (struct hostdata *)shost->hostdata;
2297
2298         if (shost->irq != irq)
2299                 panic("%s: ihdlr, irq %d, shost->irq %d.\n", ha->board_name, irq,
2300                       shost->irq);
2301
2302         /* Check if this board need to be serviced */
2303         if (!(inb(shost->io_port + REG_AUX_STATUS) & IRQ_ASSERTED))
2304                 goto none;
2305
2306         ha->iocount++;
2307
2308         if (do_trace)
2309                 printk("%s: ihdlr, enter, irq %d, count %d.\n", ha->board_name, irq,
2310                        ha->iocount);
2311
2312         /* Check if this board is still busy */
2313         if (wait_on_busy(shost->io_port, 20 * MAXLOOP)) {
2314                 reg = inb(shost->io_port + REG_STATUS);
2315                 printk
2316                     ("%s: ihdlr, busy timeout error,  irq %d, reg 0x%x, count %d.\n",
2317                      ha->board_name, irq, reg, ha->iocount);
2318                 goto none;
2319         }
2320
2321         spp = &ha->sp;
2322
2323         /* Make a local copy just before clearing the interrupt indication */
2324         memcpy(spp, ha->sp_cpu_addr, sizeof(struct mssp));
2325
2326         /* Clear the completion flag and cp pointer on the dynamic copy of sp */
2327         memset(ha->sp_cpu_addr, 0, sizeof(struct mssp));
2328
2329         /* Read the status register to clear the interrupt indication */
2330         reg = inb(shost->io_port + REG_STATUS);
2331
2332 #if defined (DEBUG_INTERRUPT)
2333         {
2334                 unsigned char *bytesp;
2335                 int cnt;
2336                 bytesp = (unsigned char *)spp;
2337                 if (ha->iocount < 200) {
2338                         printk("sp[] =");
2339                         for (cnt = 0; cnt < 15; cnt++)
2340                                 printk(" 0x%x", bytesp[cnt]);
2341                         printk("\n");
2342                 }
2343         }
2344 #endif
2345
2346         /* Reject any sp with supspect data */
2347         if (spp->eoc == 0 && ha->iocount > 1)
2348                 printk
2349                     ("%s: ihdlr, spp->eoc == 0, irq %d, reg 0x%x, count %d.\n",
2350                      ha->board_name, irq, reg, ha->iocount);
2351         if (spp->cpp_index < 0 || spp->cpp_index >= shost->can_queue)
2352                 printk
2353                     ("%s: ihdlr, bad spp->cpp_index %d, irq %d, reg 0x%x, count %d.\n",
2354                      ha->board_name, spp->cpp_index, irq, reg, ha->iocount);
2355         if (spp->eoc == 0 || spp->cpp_index < 0
2356             || spp->cpp_index >= shost->can_queue)
2357                 goto handled;
2358
2359         /* Find the mailbox to be serviced on this board */
2360         i = spp->cpp_index;
2361
2362         cpp = &(ha->cp[i]);
2363
2364 #if defined(DEBUG_GENERATE_ABORTS)
2365         if ((ha->iocount > 500) && ((ha->iocount % 500) < 3))
2366                 goto handled;
2367 #endif
2368
2369         if (ha->cp_stat[i] == IGNORE) {
2370                 ha->cp_stat[i] = FREE;
2371                 goto handled;
2372         } else if (ha->cp_stat[i] == LOCKED) {
2373                 ha->cp_stat[i] = FREE;
2374                 printk("%s: ihdlr, mbox %d unlocked, count %d.\n", ha->board_name, i,
2375                        ha->iocount);
2376                 goto handled;
2377         } else if (ha->cp_stat[i] == FREE) {
2378                 printk("%s: ihdlr, mbox %d is free, count %d.\n", ha->board_name, i,
2379                        ha->iocount);
2380                 goto handled;
2381         } else if (ha->cp_stat[i] == IN_RESET)
2382                 printk("%s: ihdlr, mbox %d is in reset.\n", ha->board_name, i);
2383         else if (ha->cp_stat[i] != IN_USE)
2384                 panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
2385                       ha->board_name, i, ha->cp_stat[i]);
2386
2387         ha->cp_stat[i] = FREE;
2388         SCpnt = cpp->SCpnt;
2389
2390         if (SCpnt == NULL)
2391                 panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", ha->board_name, i);
2392
2393         if (SCpnt->host_scribble == NULL)
2394                 panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", ha->board_name,
2395                       i, SCpnt->serial_number, SCpnt);
2396
2397         if (*(unsigned int *)SCpnt->host_scribble != i)
2398                 panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
2399                       ha->board_name, i, SCpnt->serial_number,
2400                       *(unsigned int *)SCpnt->host_scribble);
2401
2402         sync_dma(i, ha);
2403
2404         if (linked_comm && SCpnt->device->queue_depth > 2
2405             && TLDEV(SCpnt->device->type))
2406                 flush_dev(SCpnt->device, SCpnt->request->sector, ha, 1);
2407
2408         tstatus = status_byte(spp->target_status);
2409
2410 #if defined(DEBUG_GENERATE_ERRORS)
2411         if ((ha->iocount > 500) && ((ha->iocount % 200) < 2))
2412                 spp->adapter_status = 0x01;
2413 #endif
2414
2415         switch (spp->adapter_status) {
2416         case ASOK:              /* status OK */
2417
2418                 /* Forces a reset if a disk drive keeps returning BUSY */
2419                 if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
2420                         status = DID_ERROR << 16;
2421
2422                 /* If there was a bus reset, redo operation on each target */
2423                 else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
2424                          && ha->target_redo[SCpnt->device->id][SCpnt->
2425                                                                   device->
2426                                                                   channel])
2427                         status = DID_BUS_BUSY << 16;
2428
2429                 /* Works around a flaw in scsi.c */
2430                 else if (tstatus == CHECK_CONDITION
2431                          && SCpnt->device->type == TYPE_DISK
2432                          && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
2433                         status = DID_BUS_BUSY << 16;
2434
2435                 else
2436                         status = DID_OK << 16;
2437
2438                 if (tstatus == GOOD)
2439                         ha->target_redo[SCpnt->device->id][SCpnt->device->
2440                                                               channel] = 0;
2441
2442                 if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
2443                     (!(tstatus == CHECK_CONDITION && ha->iocount <= 1000 &&
2444                        (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
2445                         printk("%s: ihdlr, target %d.%d:%d, pid %ld, "
2446                                "target_status 0x%x, sense key 0x%x.\n",
2447                                ha->board_name,
2448                                SCpnt->device->channel, SCpnt->device->id,
2449                                SCpnt->device->lun, SCpnt->serial_number,
2450                                spp->target_status, SCpnt->sense_buffer[2]);
2451
2452                 ha->target_to[SCpnt->device->id][SCpnt->device->channel] = 0;
2453
2454                 if (ha->last_retried_pid == SCpnt->serial_number)
2455                         ha->retries = 0;
2456
2457                 break;
2458         case ASST:              /* Selection Time Out */
2459         case 0x02:              /* Command Time Out   */
2460
2461                 if (ha->target_to[SCpnt->device->id][SCpnt->device->channel] > 1)
2462                         status = DID_ERROR << 16;
2463                 else {
2464                         status = DID_TIME_OUT << 16;
2465                         ha->target_to[SCpnt->device->id][SCpnt->device->
2466                                                             channel]++;
2467                 }
2468
2469                 break;
2470
2471                 /* Perform a limited number of internal retries */
2472         case 0x03:              /* SCSI Bus Reset Received */
2473         case 0x04:              /* Initial Controller Power-up */
2474
2475                 for (c = 0; c <= shost->max_channel; c++)
2476                         for (k = 0; k < shost->max_id; k++)
2477                                 ha->target_redo[k][c] = 1;
2478
2479                 if (SCpnt->device->type != TYPE_TAPE
2480                     && ha->retries < MAX_INTERNAL_RETRIES) {
2481
2482 #if defined(DID_SOFT_ERROR)
2483                         status = DID_SOFT_ERROR << 16;
2484 #else
2485                         status = DID_BUS_BUSY << 16;
2486 #endif
2487
2488                         ha->retries++;
2489                         ha->last_retried_pid = SCpnt->serial_number;
2490                 } else
2491                         status = DID_ERROR << 16;
2492
2493                 break;
2494         case 0x05:              /* Unexpected Bus Phase */
2495         case 0x06:              /* Unexpected Bus Free */
2496         case 0x07:              /* Bus Parity Error */
2497         case 0x08:              /* SCSI Hung */
2498         case 0x09:              /* Unexpected Message Reject */
2499         case 0x0a:              /* SCSI Bus Reset Stuck */
2500         case 0x0b:              /* Auto Request-Sense Failed */
2501         case 0x0c:              /* Controller Ram Parity Error */
2502         default:
2503                 status = DID_ERROR << 16;
2504                 break;
2505         }
2506
2507         SCpnt->result = status | spp->target_status;
2508
2509 #if defined(DEBUG_INTERRUPT)
2510         if (SCpnt->result || do_trace)
2511 #else
2512         if ((spp->adapter_status != ASOK && ha->iocount > 1000) ||
2513             (spp->adapter_status != ASOK &&
2514              spp->adapter_status != ASST && ha->iocount <= 1000) ||
2515             do_trace || msg_byte(spp->target_status))
2516 #endif
2517                 scmd_printk(KERN_INFO, SCpnt, "ihdlr, mbox %2d, err 0x%x:%x,"
2518                        " pid %ld, reg 0x%x, count %d.\n",
2519                        i, spp->adapter_status, spp->target_status,
2520                        SCpnt->serial_number, reg, ha->iocount);
2521
2522         unmap_dma(i, ha);
2523
2524         /* Set the command state to inactive */
2525         SCpnt->host_scribble = NULL;
2526
2527         SCpnt->scsi_done(SCpnt);
2528
2529         if (do_trace)
2530                 printk("%s: ihdlr, exit, irq %d, count %d.\n", ha->board_name,
2531                                 irq, ha->iocount);
2532
2533       handled:
2534         return IRQ_HANDLED;
2535       none:
2536         return IRQ_NONE;
2537 }
2538
2539 static irqreturn_t do_interrupt_handler(int irq, void *shap)
2540 {
2541         struct Scsi_Host *shost;
2542         unsigned int j;
2543         unsigned long spin_flags;
2544         irqreturn_t ret;
2545
2546         /* Check if the interrupt must be processed by this handler */
2547         if ((j = (unsigned int)((char *)shap - sha)) >= num_boards)
2548                 return IRQ_NONE;
2549         shost = sh[j];
2550
2551         spin_lock_irqsave(shost->host_lock, spin_flags);
2552         ret = ihdlr(irq, shost);
2553         spin_unlock_irqrestore(shost->host_lock, spin_flags);
2554         return ret;
2555 }
2556
2557 static int eata2x_release(struct Scsi_Host *shost)
2558 {
2559         struct hostdata *ha = (struct hostdata *)shost->hostdata;
2560         unsigned int i;
2561
2562         for (i = 0; i < shost->can_queue; i++)
2563                 kfree((&ha->cp[i])->sglist);
2564
2565         for (i = 0; i < shost->can_queue; i++)
2566                 pci_unmap_single(ha->pdev, ha->cp[i].cp_dma_addr,
2567                                  sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
2568
2569         if (ha->sp_cpu_addr)
2570                 pci_free_consistent(ha->pdev, sizeof(struct mssp),
2571                                     ha->sp_cpu_addr, ha->sp_dma_addr);
2572
2573         free_irq(shost->irq, &sha[ha->board_number]);
2574
2575         if (shost->dma_channel != NO_DMA)
2576                 free_dma(shost->dma_channel);
2577
2578         release_region(shost->io_port, shost->n_io_port);
2579         scsi_unregister(shost);
2580         return 0;
2581 }
2582
2583 #include "scsi_module.c"
2584
2585 #ifndef MODULE
2586 __setup("eata=", option_setup);
2587 #endif                          /* end MODULE */