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