File: /usr/src/linux/drivers/scsi/eata.c

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