1 /*
2  *      u14-34f.c - Low-level driver for UltraStor 14F/34F SCSI host adapters.
3  *
4  *      25 Jun 2002 Rev. 6.70 for linux 2.4.19
5  *        + Fixed endian-ness problem due to bitfields.
6  *
7  *      21 Feb 2002 Rev. 6.52 for linux 2.4.18
8  *        + Backport from rev. 7.22 (use io_request_lock).
9  *
10  *      20 Feb 2002 Rev. 7.22 for linux 2.5.5
11  *        + Remove any reference to virt_to_bus().
12  *        + Fix pio hang while detecting multiple HBAs.
13  *
14  *      01 Jan 2002 Rev. 7.20 for linux 2.5.1
15  *        + Use the dynamic DMA mapping API.
16  *
17  *      19 Dec 2001 Rev. 7.02 for linux 2.5.1
18  *        + Use SCpnt->sc_data_direction if set.
19  *        + Use sglist.page instead of sglist.address.
20  *
21  *      11 Dec 2001 Rev. 7.00 for linux 2.5.1
22  *        + Use host->host_lock instead of io_request_lock.
23  *
24  *       1 May 2001 Rev. 6.05 for linux 2.4.4
25  *        + Fix data transfer direction for opcode SEND_CUE_SHEET (0x5d)
26  *
27  *      25 Jan 2001 Rev. 6.03 for linux 2.4.0
28  *        + "check_region" call replaced by "request_region".
29  *
30  *      22 Nov 2000 Rev. 6.02 for linux 2.4.0-test11
31  *        + Removed old scsi error handling support.
32  *        + The obsolete boot option flag eh:n is silently ignored.
33  *        + Removed error messages while a disk drive is powered up at
34  *          boot time.
35  *        + Improved boot messages: all tagged capable device are
36  *          indicated as "tagged".
37  *
38  *      16 Sep 1999 Rev. 5.11 for linux 2.2.12 and 2.3.18
39  *        + Updated to the new __setup interface for boot command line options.
40  *        + When loaded as a module, accepts the new parameter boot_options
41  *          which value is a string with the same format of the kernel boot
42  *          command line options. A valid example is:
43  *          modprobe u14-34f 'boot_options="0x230,0x340,lc:y,mq:4"'
44  *
45  *      22 Jul 1999 Rev. 5.00 for linux 2.2.10 and 2.3.11
46  *        + Removed pre-2.2 source code compatibility.
47  *
48  *      26 Jul 1998 Rev. 4.33 for linux 2.0.35 and 2.1.111
49  *          Added command line option (et:[y|n]) to use the existing
50  *          translation (returned by scsicam_bios_param) as disk geometry.
51  *          The default is et:n, which uses the disk geometry jumpered
52  *          on the board.
53  *          The default value et:n is compatible with all previous revisions
54  *          of this driver.
55  *
56  *      28 May 1998 Rev. 4.32 for linux 2.0.33 and 2.1.104
57  *          Increased busy timeout from 10 msec. to 200 msec. while
58  *          processing interrupts.
59  *
60  *      18 May 1998 Rev. 4.31 for linux 2.0.33 and 2.1.102
61  *          Improved abort handling during the eh recovery process.
62  *
63  *      13 May 1998 Rev. 4.30 for linux 2.0.33 and 2.1.101
64  *          The driver is now fully SMP safe, including the
65  *          abort and reset routines.
66  *          Added command line options (eh:[y|n]) to choose between
67  *          new_eh_code and the old scsi code.
68  *          If linux version >= 2.1.101 the default is eh:y, while the eh
69  *          option is ignored for previous releases and the old scsi code
70  *          is used.
71  *
72  *      18 Apr 1998 Rev. 4.20 for linux 2.0.33 and 2.1.97
73  *          Reworked interrupt handler.
74  *
75  *      11 Apr 1998 rev. 4.05 for linux 2.0.33 and 2.1.95
76  *          Major reliability improvement: when a batch with overlapping
77  *          requests is detected, requests are queued one at a time
78  *          eliminating any possible board or drive reordering.
79  *
80  *      10 Apr 1998 rev. 4.04 for linux 2.0.33 and 2.1.95
81  *          Improved SMP support (if linux version >= 2.1.95).
82  *
83  *       9 Apr 1998 rev. 4.03 for linux 2.0.33 and 2.1.94
84  *          Performance improvement: when sequential i/o is detected,
85  *          always use direct sort instead of reverse sort.
86  *
87  *       4 Apr 1998 rev. 4.02 for linux 2.0.33 and 2.1.92
88  *          io_port is now unsigned long.
89  *
90  *      17 Mar 1998 rev. 4.01 for linux 2.0.33 and 2.1.88
91  *          Use new scsi error handling code (if linux version >= 2.1.88).
92  *          Use new interrupt code.
93  *
94  *      12 Sep 1997 rev. 3.11 for linux 2.0.30 and 2.1.55
95  *          Use of udelay inside the wait loops to avoid timeout
96  *          problems with fast cpus.
97  *          Removed check about useless calls to the interrupt service
98  *          routine (reported on SMP systems only).
99  *          At initialization time "sorted/unsorted" is displayed instead
100  *          of "linked/unlinked" to reinforce the fact that "linking" is
101  *          nothing but "elevator sorting" in the actual implementation.
102  *
103  *      17 May 1997 rev. 3.10 for linux 2.0.30 and 2.1.38
104  *          Use of serial_number_at_timeout in abort and reset processing.
105  *          Use of the __initfunc and __initdata macro in setup code.
106  *          Minor cleanups in the list_statistics code.
107  *
108  *      24 Feb 1997 rev. 3.00 for linux 2.0.29 and 2.1.26
109  *          When loading as a module, parameter passing is now supported
110  *          both in 2.0 and in 2.1 style.
111  *          Fixed data transfer direction for some SCSI opcodes.
112  *          Immediate acknowledge to request sense commands.
113  *          Linked commands to each disk device are now reordered by elevator
114  *          sorting. Rare cases in which reordering of write requests could
115  *          cause wrong results are managed.
116  *
117  *      18 Jan 1997 rev. 2.60 for linux 2.1.21 and 2.0.28
118  *          Added command line options to enable/disable linked commands
119  *          (lc:[y|n]), old firmware support (of:[y|n]) and to set the max
120  *          queue depth (mq:xx). Default is "u14-34f=lc:n,of:n,mq:8".
121  *          Improved command linking.
122  *
123  *       8 Jan 1997 rev. 2.50 for linux 2.1.20 and 2.0.27
124  *          Added linked command support.
125  *
126  *       3 Dec 1996 rev. 2.40 for linux 2.1.14 and 2.0.27
127  *          Added queue depth adjustment.
128  *
129  *      22 Nov 1996 rev. 2.30 for linux 2.1.12 and 2.0.26
130  *          The list of i/o ports to be probed can be overwritten by the
131  *          "u14-34f=port0,port1,...." boot command line option.
132  *          Scatter/gather lists are now allocated by a number of kmalloc
133  *          calls, in order to avoid the previous size limit of 64Kb.
134  *
135  *      16 Nov 1996 rev. 2.20 for linux 2.1.10 and 2.0.25
136  *          Added multichannel support.
137  *
138  *      27 Sep 1996 rev. 2.12 for linux 2.1.0
139  *          Portability cleanups (virtual/bus addressing, little/big endian
140  *          support).
141  *
142  *      09 Jul 1996 rev. 2.11 for linux 2.0.4
143  *          "Data over/under-run" no longer implies a redo on all targets.
144  *          Number of internal retries is now limited.
145  *
146  *      16 Apr 1996 rev. 2.10 for linux 1.3.90
147  *          New argument "reset_flags" to the reset routine.
148  *
149  *      21 Jul 1995 rev. 2.02 for linux 1.3.11
150  *          Fixed Data Transfer Direction for some SCSI commands.
151  *
152  *      13 Jun 1995 rev. 2.01 for linux 1.2.10
153  *          HAVE_OLD_UX4F_FIRMWARE should be defined for U34F boards when
154  *          the firmware prom is not the latest one (28008-006).
155  *
156  *      11 Mar 1995 rev. 2.00 for linux 1.2.0
157  *          Fixed a bug which prevented media change detection for removable
158  *          disk drives.
159  *
160  *      23 Feb 1995 rev. 1.18 for linux 1.1.94
161  *          Added a check for scsi_register returning NULL.
162  *
163  *      11 Feb 1995 rev. 1.17 for linux 1.1.91
164  *          U14F qualified to run with 32 sglists.
165  *          Now DEBUG_RESET is disabled by default.
166  *
167  *       9 Feb 1995 rev. 1.16 for linux 1.1.90
168  *          Use host->wish_block instead of host->block.
169  *
170  *       8 Feb 1995 rev. 1.15 for linux 1.1.89
171  *          Cleared target_time_out counter while performing a reset.
172  *
173  *      28 Jan 1995 rev. 1.14 for linux 1.1.86
174  *          Added module support.
175  *          Log and do a retry when a disk drive returns a target status
176  *          different from zero on a recovered error.
177  *          Auto detects if U14F boards have an old firmware revision.
178  *          Max number of scatter/gather lists set to 16 for all boards
179  *          (most installation run fine using 33 sglists, while other
180  *          has problems when using more then 16).
181  *
182  *      16 Jan 1995 rev. 1.13 for linux 1.1.81
183  *          Display a message if check_region detects a port address
184  *          already in use.
185  *
186  *      15 Dec 1994 rev. 1.12 for linux 1.1.74
187  *          The host->block flag is set for all the detected ISA boards.
188  *
189  *      30 Nov 1994 rev. 1.11 for linux 1.1.68
190  *          Redo i/o on target status CHECK_CONDITION for TYPE_DISK only.
191  *          Added optional support for using a single board at a time.
192  *
193  *      14 Nov 1994 rev. 1.10 for linux 1.1.63
194  *
195  *      28 Oct 1994 rev. 1.09 for linux 1.1.58  Final BETA release.
196  *      16 Jul 1994 rev. 1.00 for linux 1.1.29  Initial ALPHA release.
197  *
198  *          This driver is a total replacement of the original UltraStor
199  *          scsi driver, but it supports ONLY the 14F and 34F boards.
200  *          It can be configured in the same kernel in which the original
201  *          ultrastor driver is configured to allow the original U24F
202  *          support.
203  *
204  *          Multiple U14F and/or U34F host adapters are supported.
205  *
206  *  Copyright (C) 1994-2002 Dario Ballabio (ballabio_dario@emc.com)
207  *
208  *  Alternate email: dario.ballabio@inwind.it, dario.ballabio@tiscalinet.it
209  *
210  *  Redistribution and use in source and binary forms, with or without
211  *  modification, are permitted provided that redistributions of source
212  *  code retain the above copyright notice and this comment without
213  *  modification.
214  *
215  *      WARNING: if your 14/34F board has an old firmware revision (see below)
216  *               you must change "#undef" into "#define" in the following
217  *               statement.
218  */
219 #undef HAVE_OLD_UX4F_FIRMWARE
220 /*
221  *  The UltraStor 14F, 24F, and 34F are a family of intelligent, high
222  *  performance SCSI-2 host adapters.
223  *  Here is the scoop on the various models:
224  *
225  *  14F - ISA first-party DMA HA with floppy support and WD1003 emulation.
226  *  24F - EISA Bus Master HA with floppy support and WD1003 emulation.
227  *  34F - VESA Local-Bus Bus Master HA (no WD1003 emulation).
228  *
229  *  This code has been tested with up to two U14F boards, using both
230  *  firmware 28004-005/38004-004 (BIOS rev. 2.00) and the latest firmware
231  *  28004-006/38004-005 (BIOS rev. 2.01).
232  *
233  *  The latest firmware is required in order to get reliable operations when
234  *  clustering is enabled. ENABLE_CLUSTERING provides a performance increase
235  *  up to 50% on sequential access.
236  *
237  *  Since the Scsi_Host_Template structure is shared among all 14F and 34F,
238  *  the last setting of use_clustering is in effect for all of these boards.
239  *
240  *  Here a sample configuration using two U14F boards:
241  *
242  U14F0: ISA 0x330, BIOS 0xc8000, IRQ 11, DMA 5, SG 32, MB 16, of:n, lc:y, mq:8.
243  U14F1: ISA 0x340, BIOS 0x00000, IRQ 10, DMA 6, SG 32, MB 16, of:n, lc:y, mq:8.
244  *
245  *  The boot controller must have its BIOS enabled, while other boards can
246  *  have their BIOS disabled, or enabled to an higher address.
247  *  Boards are named Ux4F0, Ux4F1..., according to the port address order in
248  *  the io_port[] array.
249  *
250  *  The following facts are based on real testing results (not on
251  *  documentation) on the above U14F board.
252  *
253  *  - The U14F board should be jumpered for bus on time less or equal to 7
254  *    microseconds, while the default is 11 microseconds. This is order to
255  *    get acceptable performance while using floppy drive and hard disk
256  *    together. The jumpering for 7 microseconds is: JP13 pin 15-16,
257  *    JP14 pin 7-8 and pin 9-10.
258  *    The reduction has a little impact on scsi performance.
259  *
260  *  - If scsi bus length exceeds 3m., the scsi bus speed needs to be reduced
261  *    from 10Mhz to 5Mhz (do this by inserting a jumper on JP13 pin 7-8).
262  *
263  *  - If U14F on board firmware is older than 28004-006/38004-005,
264  *    the U14F board is unable to provide reliable operations if the scsi
265  *    request length exceeds 16Kbyte. When this length is exceeded the
266  *    behavior is:
267  *    - adapter_status equal 0x96 or 0xa3 or 0x93 or 0x94;
268  *    - adapter_status equal 0 and target_status equal 2 on for all targets
269  *      in the next operation following the reset.
270  *    This sequence takes a long time (>3 seconds), so in the meantime
271  *    the SD_TIMEOUT in sd.c could expire giving rise to scsi aborts
272  *    (SD_TIMEOUT has been increased from 3 to 6 seconds in 1.1.31).
273  *    Because of this I had to DISABLE_CLUSTERING and to work around the
274  *    bus reset in the interrupt service routine, returning DID_BUS_BUSY
275  *    so that the operations are retried without complains from the scsi.c
276  *    code.
277  *    Any reset of the scsi bus is going to kill tape operations, since
278  *    no retry is allowed for tapes. Bus resets are more likely when the
279  *    scsi bus is under heavy load.
280  *    Requests using scatter/gather have a maximum length of 16 x 1024 bytes
281  *    when DISABLE_CLUSTERING is in effect, but unscattered requests could be
282  *    larger than 16Kbyte.
283  *
284  *    The new firmware has fixed all the above problems.
285  *
286  *  For U34F boards the latest bios prom is 38008-002 (BIOS rev. 2.01),
287  *  the latest firmware prom is 28008-006. Older firmware 28008-005 has
288  *  problems when using more then 16 scatter/gather lists.
289  *
290  *  The list of i/o ports to be probed can be totally replaced by the
291  *  boot command line option: "u14-34f=port0,port1,port2,...", where the
292  *  port0, port1... arguments are ISA/VESA addresses to be probed.
293  *  For example using "u14-34f=0x230,0x340", the driver probes only the two
294  *  addresses 0x230 and 0x340 in this order; "u14-34f=0" totally disables
295  *  this driver.
296  *
297  *  After the optional list of detection probes, other possible command line
298  *  options are:
299  *
300  *  et:y  use disk geometry returned by scsicam_bios_param;
301  *  et:n  use disk geometry jumpered on the board;
302  *  lc:y  enables linked commands;
303  *  lc:n  disables linked commands;
304  *  of:y  enables old firmware support;
305  *  of:n  disables old firmware support;
306  *  mq:xx set the max queue depth to the value xx (2 <= xx <= 8).
307  *
308  *  The default value is: "u14-34f=lc:n,of:n,mq:8,et:n".
309  *  An example using the list of detection probes could be:
310  *  "u14-34f=0x230,0x340,lc:y,of:n,mq:4,et:n".
311  *
312  *  When loading as a module, parameters can be specified as well.
313  *  The above example would be (use 1 in place of y and 0 in place of n):
314  *
315  *  modprobe u14-34f io_port=0x230,0x340 linked_comm=1 have_old_firmware=0 \
316  *                max_queue_depth=4 ext_tran=0
317  *
318  *  ----------------------------------------------------------------------------
319  *  In this implementation, linked commands are designed to work with any DISK
320  *  or CD-ROM, since this linking has only the intent of clustering (time-wise)
321  *  and reordering by elevator sorting commands directed to each device,
322  *  without any relation with the actual SCSI protocol between the controller
323  *  and the device.
324  *  If Q is the queue depth reported at boot time for each device (also named
325  *  cmds/lun) and Q > 2, whenever there is already an active command to the
326  *  device all other commands to the same device  (up to Q-1) are kept waiting
327  *  in the elevator sorting queue. When the active command completes, the
328  *  commands in this queue are sorted by sector address. The sort is chosen
329  *  between increasing or decreasing by minimizing the seek distance between
330  *  the sector of the commands just completed and the sector of the first
331  *  command in the list to be sorted.
332  *  Trivial math assures that the unsorted average seek distance when doing
333  *  random seeks over S sectors is S/3.
334  *  When (Q-1) requests are uniformly distributed over S sectors, the average
335  *  distance between two adjacent requests is S/((Q-1) + 1), so the sorted
336  *  average seek distance for (Q-1) random requests over S sectors is S/Q.
337  *  The elevator sorting hence divides the seek distance by a factor Q/3.
338  *  The above pure geometric remarks are valid in all cases and the
339  *  driver effectively reduces the seek distance by the predicted factor
340  *  when there are Q concurrent read i/o operations on the device, but this
341  *  does not necessarily results in a noticeable performance improvement:
342  *  your mileage may vary....
343  *
344  *  Note: command reordering inside a batch of queued commands could cause
345  *        wrong results only if there is at least one write request and the
346  *        intersection (sector-wise) of all requests is not empty.
347  *        When the driver detects a batch including overlapping requests
348  *        (a really rare event) strict serial (pid) order is enforced.
349  *  ----------------------------------------------------------------------------
350  *
351  *  The boards are named Ux4F0, Ux4F1,... according to the detection order.
352  *
353  *  In order to support multiple ISA boards in a reliable way,
354  *  the driver sets host->wish_block = TRUE for all ISA boards.
355  */
356 
357 #include <linux/version.h>
358 
359 #ifndef LinuxVersionCode
360 #define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
361 #endif
362 
363 #define MAX_INT_PARAM 10
364 
365 #if defined(MODULE)
366 #include <linux/module.h>
367 
368 MODULE_PARM(boot_options, "s");
369 MODULE_PARM(io_port, "1-" __MODULE_STRING(MAX_INT_PARAM) "i");
370 MODULE_PARM(linked_comm, "i");
371 MODULE_PARM(have_old_firmware, "i");
372 MODULE_PARM(link_statistics, "i");
373 MODULE_PARM(max_queue_depth, "i");
374 MODULE_PARM(ext_tran, "i");
375 MODULE_AUTHOR("Dario Ballabio");
376 
377 #endif
378 
379 #include <linux/string.h>
380 #include <linux/sched.h>
381 #include <linux/kernel.h>
382 #include <linux/ioport.h>
383 #include <linux/delay.h>
384 #include <asm/io.h>
385 #include <asm/system.h>
386 #include <asm/byteorder.h>
387 #include <linux/proc_fs.h>
388 #include <linux/blk.h>
389 #include "scsi.h"
390 #include "hosts.h"
391 #include "sd.h"
392 #include <asm/dma.h>
393 #include <asm/irq.h>
394 #include "u14-34f.h"
395 #include <linux/stat.h>
396 #include <linux/config.h>
397 #include <linux/pci.h>
398 #include <linux/init.h>
399 #include <linux/ctype.h>
400 #include <linux/spinlock.h>
401 
402 #if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
403 #error "Adjust your <asm/byteorder.h> defines"
404 #endif
405 
406 /* Values for the PRODUCT_ID ports for the 14/34F */
407 #define PRODUCT_ID1  0x56
408 #define PRODUCT_ID2  0x40        /* NOTE: Only upper nibble is used */
409 
410 /* Subversion values */
411 #define ISA  0
412 #define ESA 1
413 
414 #define OP_HOST_ADAPTER   0x1
415 #define OP_SCSI           0x2
416 #define OP_RESET          0x4
417 #define DTD_SCSI          0x0
418 #define DTD_IN            0x1
419 #define DTD_OUT           0x2
420 #define DTD_NONE          0x3
421 #define HA_CMD_INQUIRY    0x1
422 #define HA_CMD_SELF_DIAG  0x2
423 #define HA_CMD_READ_BUFF  0x3
424 #define HA_CMD_WRITE_BUFF 0x4
425 
426 #undef  DEBUG_LINKED_COMMANDS
427 #undef  DEBUG_DETECT
428 #undef  DEBUG_INTERRUPT
429 #undef  DEBUG_RESET
430 #undef  DEBUG_GENERATE_ERRORS
431 #undef  DEBUG_GENERATE_ABORTS
432 #undef  DEBUG_GEOMETRY
433 
434 #define MAX_ISA 3
435 #define MAX_VESA 1
436 #define MAX_EISA 0
437 #define MAX_PCI 0
438 #define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
439 #define MAX_CHANNEL 1
440 #define MAX_LUN 8
441 #define MAX_TARGET 8
442 #define MAX_MAILBOXES 16
443 #define MAX_SGLIST 32
444 #define MAX_SAFE_SGLIST 16
445 #define MAX_INTERNAL_RETRIES 64
446 #define MAX_CMD_PER_LUN 2
447 #define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
448 
449 #define SKIP ULONG_MAX
450 #define FALSE 0
451 #define TRUE 1
452 #define FREE 0
453 #define IN_USE   1
454 #define LOCKED   2
455 #define IN_RESET 3
456 #define IGNORE   4
457 #define READY    5
458 #define ABORTING 6
459 #define NO_DMA  0xff
460 #define MAXLOOP  10000
461 
462 #define REG_LCL_MASK      0
463 #define REG_LCL_INTR      1
464 #define REG_SYS_MASK      2
465 #define REG_SYS_INTR      3
466 #define REG_PRODUCT_ID1   4
467 #define REG_PRODUCT_ID2   5
468 #define REG_CONFIG1       6
469 #define REG_CONFIG2       7
470 #define REG_OGM           8
471 #define REG_ICM           12
472 #define REGION_SIZE       13UL
473 #define BSY_ASSERTED      0x01
474 #define IRQ_ASSERTED      0x01
475 #define CMD_RESET         0xc0
476 #define CMD_OGM_INTR      0x01
477 #define CMD_CLR_INTR      0x01
478 #define CMD_ENA_INTR      0x81
479 #define ASOK              0x00
480 #define ASST              0x91
481 
482 #define YESNO(a) ((a) ? 'y' : 'n')
483 #define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
484 
485 #define PACKED          __attribute__((packed))
486 
487 struct sg_list {
488    unsigned int address;                /* Segment Address */
489    unsigned int num_bytes;              /* Segment Length */
490    };
491 
492 /* MailBox SCSI Command Packet */
493 struct mscp {
494 
495 #if defined(__BIG_ENDIAN_BITFIELD)
496    unsigned char sg:1, ca:1, dcn:1, xdir:2, opcode:3;
497    unsigned char lun: 3, channel:2, target:3;
498 #else
499    unsigned char opcode: 3,             /* type of command */
500                  xdir: 2,               /* data transfer direction */
501                  dcn: 1,                /* disable disconnect */
502                  ca: 1,                 /* use cache (if available) */
503                  sg: 1;                 /* scatter/gather operation */
504    unsigned char target: 3,             /* SCSI target id */
505                  channel: 2,            /* SCSI channel number */
506                  lun: 3;                /* SCSI logical unit number */
507 #endif
508 
509    unsigned int data_address PACKED;    /* transfer data pointer */
510    unsigned int data_len PACKED;        /* length in bytes */
511    unsigned int link_address PACKED;    /* for linking command chains */
512    unsigned char clink_id;              /* identifies command in chain */
513    unsigned char use_sg;                /* (if sg is set) 8 bytes per list */
514    unsigned char sense_len;
515    unsigned char cdb_len;               /* 6, 10, or 12 */
516    unsigned char cdb[12];               /* SCSI Command Descriptor Block */
517    unsigned char adapter_status;        /* non-zero indicates HA error */
518    unsigned char target_status;         /* non-zero indicates target error */
519    unsigned int sense_addr PACKED;
520 
521    /* Additional fields begin here. */
522    Scsi_Cmnd *SCpnt;
523    unsigned int cpp_index;              /* cp index */
524 
525    /* All the cp structure is zero filled by queuecommand except the
526       following CP_TAIL_SIZE bytes, initialized by detect */
527    dma_addr_t cp_dma_addr; /* dma handle for this cp structure */
528    struct sg_list *sglist; /* pointer to the allocated SG list */
529    };
530 
531 #define CP_TAIL_SIZE (sizeof(struct sglist *) + sizeof(dma_addr_t))
532 
533 struct hostdata {
534    struct mscp cp[MAX_MAILBOXES];       /* Mailboxes for this board */
535    unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
536    unsigned int last_cp_used;           /* Index of last mailbox used */
537    unsigned int iocount;                /* Total i/o done for this board */
538    int board_number;                    /* Number of this board */
539    char board_name[16];                 /* Name of this board */
540    int in_reset;                        /* True if board is doing a reset */
541    int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
542    int target_redo[MAX_TARGET][MAX_CHANNEL]; /* If TRUE redo i/o on target */
543    unsigned int retries;                /* Number of internal retries */
544    unsigned long last_retried_pid;      /* Pid of last retried command */
545    unsigned char subversion;            /* Bus type, either ISA or ESA */
546    struct pci_dev *pdev;                /* Always NULL */
547    unsigned char heads;
548    unsigned char sectors;
549    char board_id[256];                  /* data from INQUIRY on this board */
550    };
551 
552 static struct Scsi_Host *sh[MAX_BOARDS + 1];
553 static const char *driver_name = "Ux4F";
554 static char sha[MAX_BOARDS];
555 
556 /* Initialize num_boards so that ihdlr can work while detect is in progress */
557 static unsigned int num_boards = MAX_BOARDS;
558 
559 static unsigned long io_port[] = {
560 
561    /* Space for MAX_INT_PARAM ports usable while loading as a module */
562    SKIP,    SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,
563    SKIP,    SKIP,
564 
565    /* Possible ISA/VESA ports */
566    0x330, 0x340, 0x230, 0x240, 0x210, 0x130, 0x140,
567 
568    /* End of list */
569    0x0
570    };
571 
572 #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
573 #define BN(board) (HD(board)->board_name)
574 
575 /* Device is Little Endian */
576 #define H2DEV(x) cpu_to_le32(x)
577 #define DEV2H(x) le32_to_cpu(x)
578 
579 static void do_interrupt_handler(int, void *, struct pt_regs *);
580 static void flush_dev(Scsi_Device *, unsigned long, unsigned int, unsigned int);
581 static int do_trace = FALSE;
582 static int setup_done = FALSE;
583 static int link_statistics;
584 static int ext_tran = FALSE;
585 static char *boot_options;
586 
587 #if defined(HAVE_OLD_UX4F_FIRMWARE)
588 static int have_old_firmware = TRUE;
589 #else
590 static int have_old_firmware = FALSE;
591 #endif
592 
593 #if defined(CONFIG_SCSI_U14_34F_LINKED_COMMANDS)
594 static int linked_comm = TRUE;
595 #else
596 static int linked_comm = FALSE;
597 #endif
598 
599 #if defined(CONFIG_SCSI_U14_34F_MAX_TAGS)
600 static int max_queue_depth = CONFIG_SCSI_U14_34F_MAX_TAGS;
601 #else
602 static int max_queue_depth = MAX_CMD_PER_LUN;
603 #endif
604 
select_queue_depths(struct Scsi_Host * host,Scsi_Device * devlist)605 static void select_queue_depths(struct Scsi_Host *host, Scsi_Device *devlist) {
606    Scsi_Device *dev;
607    int j, ntag = 0, nuntag = 0, tqd, utqd;
608 
609    j = ((struct hostdata *) host->hostdata)->board_number;
610 
611    for(dev = devlist; dev; dev = dev->next) {
612 
613       if (dev->host != host) continue;
614 
615       if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
616          ntag++;
617       else
618          nuntag++;
619       }
620 
621    utqd = MAX_CMD_PER_LUN;
622 
623    tqd = (host->can_queue - utqd * nuntag) / (ntag ? ntag : 1);
624 
625    if (tqd > max_queue_depth) tqd = max_queue_depth;
626 
627    if (tqd < MAX_CMD_PER_LUN) tqd = MAX_CMD_PER_LUN;
628 
629    for(dev = devlist; dev; dev = dev->next) {
630       char *tag_suffix = "", *link_suffix = "";
631 
632       if (dev->host != host) continue;
633 
634       if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
635          dev->queue_depth = tqd;
636       else
637          dev->queue_depth = utqd;
638 
639       if (TLDEV(dev->type)) {
640          if (linked_comm && dev->queue_depth > 2)
641             link_suffix = ", sorted";
642          else
643             link_suffix = ", unsorted";
644          }
645 
646       if (dev->tagged_supported && TLDEV(dev->type) && dev->tagged_queue)
647          tag_suffix = ", soft-tagged";
648       else if (dev->tagged_supported && TLDEV(dev->type))
649          tag_suffix = ", tagged";
650 
651       printk("%s: scsi%d, channel %d, id %d, lun %d, cmds/lun %d%s%s.\n",
652              BN(j), host->host_no, dev->channel, dev->id, dev->lun,
653              dev->queue_depth, link_suffix, tag_suffix);
654       }
655 
656    return;
657 }
658 
wait_on_busy(unsigned long iobase,unsigned int loop)659 static inline int wait_on_busy(unsigned long iobase, unsigned int loop) {
660 
661    while (inb(iobase + REG_LCL_INTR) & BSY_ASSERTED) {
662       udelay(1L);
663       if (--loop == 0) return TRUE;
664       }
665 
666    return FALSE;
667 }
668 
board_inquiry(unsigned int j)669 static int board_inquiry(unsigned int j) {
670    struct mscp *cpp;
671    dma_addr_t id_dma_addr;
672    unsigned int time, limit = 0;
673 
674    id_dma_addr = pci_map_single(HD(j)->pdev, HD(j)->board_id,
675                     sizeof(HD(j)->board_id), PCI_DMA_BIDIRECTIONAL);
676    cpp = &HD(j)->cp[0];
677    cpp->cp_dma_addr = pci_map_single(HD(j)->pdev, cpp, sizeof(struct mscp),
678                                      PCI_DMA_BIDIRECTIONAL);
679    memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
680    cpp->opcode = OP_HOST_ADAPTER;
681    cpp->xdir = DTD_IN;
682    cpp->data_address = H2DEV(id_dma_addr);
683    cpp->data_len = H2DEV(sizeof(HD(j)->board_id));
684    cpp->cdb_len = 6;
685    cpp->cdb[0] = HA_CMD_INQUIRY;
686 
687    if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
688       printk("%s: board_inquiry, adapter busy.\n", BN(j));
689       return TRUE;
690       }
691 
692    HD(j)->cp_stat[0] = IGNORE;
693 
694    /* Clear the interrupt indication */
695    outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
696 
697    /* Store pointer in OGM address bytes */
698    outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
699 
700    /* Issue OGM interrupt */
701    outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
702 
703    spin_unlock_irq(&io_request_lock);
704    time = jiffies;
705    while ((jiffies - time) < HZ && limit++ < 20000) udelay(100L);
706    spin_lock_irq(&io_request_lock);
707 
708    if (cpp->adapter_status || HD(j)->cp_stat[0] != FREE) {
709       HD(j)->cp_stat[0] = FREE;
710       printk("%s: board_inquiry, err 0x%x.\n", BN(j), cpp->adapter_status);
711       return TRUE;
712       }
713 
714    pci_unmap_single(HD(j)->pdev, cpp->cp_dma_addr, sizeof(struct mscp),
715                     PCI_DMA_BIDIRECTIONAL);
716    pci_unmap_single(HD(j)->pdev, id_dma_addr, sizeof(HD(j)->board_id),
717                     PCI_DMA_BIDIRECTIONAL);
718    return FALSE;
719 }
720 
port_detect(unsigned long port_base,unsigned int j,Scsi_Host_Template * tpnt)721 static inline int port_detect \
722       (unsigned long port_base, unsigned int j, Scsi_Host_Template *tpnt) {
723    unsigned char irq, dma_channel, subversion, i;
724    unsigned char in_byte;
725    char *bus_type, dma_name[16];
726 
727    /* Allowed BIOS base addresses (NULL indicates reserved) */
728    unsigned long bios_segment_table[8] = {
729       0,
730       0xc4000, 0xc8000, 0xcc000, 0xd0000,
731       0xd4000, 0xd8000, 0xdc000
732       };
733 
734    /* Allowed IRQs */
735    unsigned char interrupt_table[4] = { 15, 14, 11, 10 };
736 
737    /* Allowed DMA channels for ISA (0 indicates reserved) */
738    unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
739 
740    /* Head/sector mappings */
741    struct {
742       unsigned char heads;
743       unsigned char sectors;
744       } mapping_table[4] = {
745            { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 }
746            };
747 
748    struct config_1 {
749 
750 #if defined(__BIG_ENDIAN_BITFIELD)
751       unsigned char dma_channel: 2, interrupt:2,
752                     removable_disks_as_fixed:1, bios_segment: 3;
753 #else
754       unsigned char bios_segment: 3, removable_disks_as_fixed: 1,
755                     interrupt: 2, dma_channel: 2;
756 #endif
757 
758       } config_1;
759 
760    struct config_2 {
761 
762 #if defined(__BIG_ENDIAN_BITFIELD)
763       unsigned char tfr_port: 2, bios_drive_number: 1,
764                     mapping_mode: 2, ha_scsi_id: 3;
765 #else
766       unsigned char ha_scsi_id: 3, mapping_mode: 2,
767                     bios_drive_number: 1, tfr_port: 2;
768 #endif
769 
770       } config_2;
771 
772    char name[16];
773 
774    sprintf(name, "%s%d", driver_name, j);
775 
776    if(!request_region(port_base, REGION_SIZE, driver_name)) {
777 #if defined(DEBUG_DETECT)
778       printk("%s: address 0x%03lx in use, skipping probe.\n", name, port_base);
779 #endif
780       return FALSE;
781       }
782 
783    if (inb(port_base + REG_PRODUCT_ID1) != PRODUCT_ID1) {
784       release_region(port_base, REGION_SIZE);
785       return FALSE;
786       }
787 
788    in_byte = inb(port_base + REG_PRODUCT_ID2);
789 
790    if ((in_byte & 0xf0) != PRODUCT_ID2) {
791       release_region(port_base, REGION_SIZE);
792       return FALSE;
793       }
794 
795    *(char *)&config_1 = inb(port_base + REG_CONFIG1);
796    *(char *)&config_2 = inb(port_base + REG_CONFIG2);
797 
798    irq = interrupt_table[config_1.interrupt];
799    dma_channel = dma_channel_table[config_1.dma_channel];
800    subversion = (in_byte & 0x0f);
801 
802    /* Board detected, allocate its IRQ */
803    if (request_irq(irq, do_interrupt_handler,
804              SA_INTERRUPT | ((subversion == ESA) ? SA_SHIRQ : 0),
805              driver_name, (void *) &sha[j])) {
806       printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
807       release_region(port_base, REGION_SIZE);
808       return FALSE;
809       }
810 
811    if (subversion == ISA && request_dma(dma_channel, driver_name)) {
812       printk("%s: unable to allocate DMA channel %u, detaching.\n",
813              name, dma_channel);
814       free_irq(irq, &sha[j]);
815       release_region(port_base, REGION_SIZE);
816       return FALSE;
817       }
818 
819    if (have_old_firmware) tpnt->use_clustering = DISABLE_CLUSTERING;
820 
821    sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
822 
823    if (sh[j] == NULL) {
824       printk("%s: unable to register host, detaching.\n", name);
825 
826       free_irq(irq, &sha[j]);
827 
828       if (subversion == ISA) free_dma(dma_channel);
829 
830       release_region(port_base, REGION_SIZE);
831       return FALSE;
832       }
833 
834    sh[j]->io_port = port_base;
835    sh[j]->unique_id = port_base;
836    sh[j]->n_io_port = REGION_SIZE;
837    sh[j]->base = bios_segment_table[config_1.bios_segment];
838    sh[j]->irq = irq;
839    sh[j]->sg_tablesize = MAX_SGLIST;
840    sh[j]->this_id = config_2.ha_scsi_id;
841    sh[j]->can_queue = MAX_MAILBOXES;
842    sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
843    sh[j]->select_queue_depths = select_queue_depths;
844 
845 #if defined(DEBUG_DETECT)
846    {
847    unsigned char sys_mask, lcl_mask;
848 
849    sys_mask = inb(sh[j]->io_port + REG_SYS_MASK);
850    lcl_mask = inb(sh[j]->io_port + REG_LCL_MASK);
851    printk("SYS_MASK 0x%x, LCL_MASK 0x%x.\n", sys_mask, lcl_mask);
852    }
853 #endif
854 
855    /* Probably a bogus host scsi id, set it to the dummy value */
856    if (sh[j]->this_id == 0) sh[j]->this_id = -1;
857 
858    /* If BIOS is disabled, force enable interrupts */
859    if (sh[j]->base == 0) outb(CMD_ENA_INTR, sh[j]->io_port + REG_SYS_MASK);
860 
861    memset(HD(j), 0, sizeof(struct hostdata));
862    HD(j)->heads = mapping_table[config_2.mapping_mode].heads;
863    HD(j)->sectors = mapping_table[config_2.mapping_mode].sectors;
864    HD(j)->subversion = subversion;
865    HD(j)->pdev = NULL;
866    HD(j)->board_number = j;
867 
868    if (have_old_firmware) sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
869 
870    if (HD(j)->subversion == ESA) {
871       sh[j]->unchecked_isa_dma = FALSE;
872       sh[j]->dma_channel = NO_DMA;
873       sprintf(BN(j), "U34F%d", j);
874       bus_type = "VESA";
875       }
876    else {
877       unsigned long flags;
878       scsi_register_blocked_host(sh[j]);
879       sh[j]->unchecked_isa_dma = TRUE;
880 
881       flags=claim_dma_lock();
882       disable_dma(dma_channel);
883       clear_dma_ff(dma_channel);
884       set_dma_mode(dma_channel, DMA_MODE_CASCADE);
885       enable_dma(dma_channel);
886       release_dma_lock(flags);
887 
888       sh[j]->dma_channel = dma_channel;
889       sprintf(BN(j), "U14F%d", j);
890       bus_type = "ISA";
891       }
892 
893    sh[j]->max_channel = MAX_CHANNEL - 1;
894    sh[j]->max_id = MAX_TARGET;
895    sh[j]->max_lun = MAX_LUN;
896 
897    if (HD(j)->subversion == ISA && !board_inquiry(j)) {
898       HD(j)->board_id[40] = 0;
899 
900       if (strcmp(&HD(j)->board_id[32], "06000600")) {
901          printk("%s: %s.\n", BN(j), &HD(j)->board_id[8]);
902          printk("%s: firmware %s is outdated, FW PROM should be 28004-006.\n",
903                 BN(j), &HD(j)->board_id[32]);
904          sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
905          sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
906          }
907       }
908 
909    if (dma_channel == NO_DMA) sprintf(dma_name, "%s", "BMST");
910    else                       sprintf(dma_name, "DMA %u", dma_channel);
911 
912    for (i = 0; i < sh[j]->can_queue; i++)
913       HD(j)->cp[i].cp_dma_addr = pci_map_single(HD(j)->pdev,
914             &HD(j)->cp[i], sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
915 
916    for (i = 0; i < sh[j]->can_queue; i++)
917       if (! ((&HD(j)->cp[i])->sglist = kmalloc(
918             sh[j]->sg_tablesize * sizeof(struct sg_list),
919             (sh[j]->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC))) {
920          printk("%s: kmalloc SGlist failed, mbox %d, detaching.\n", BN(j), i);
921          u14_34f_release(sh[j]);
922          return FALSE;
923          }
924 
925    if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
926        max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
927 
928    if (max_queue_depth < MAX_CMD_PER_LUN) max_queue_depth = MAX_CMD_PER_LUN;
929 
930    if (j == 0) {
931       printk("UltraStor 14F/34F: Copyright (C) 1994-2002 Dario Ballabio.\n");
932       printk("%s config options -> of:%c, lc:%c, mq:%d, et:%c.\n",
933              driver_name, YESNO(have_old_firmware), YESNO(linked_comm),
934              max_queue_depth, YESNO(ext_tran));
935       }
936 
937    printk("%s: %s 0x%03lx, BIOS 0x%05x, IRQ %u, %s, SG %d, MB %d.\n",
938           BN(j), bus_type, (unsigned long)sh[j]->io_port, (int)sh[j]->base,
939           sh[j]->irq, dma_name, sh[j]->sg_tablesize, sh[j]->can_queue);
940 
941    if (sh[j]->max_id > 8 || sh[j]->max_lun > 8)
942       printk("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
943              BN(j), sh[j]->max_id, sh[j]->max_lun);
944 
945    for (i = 0; i <= sh[j]->max_channel; i++)
946       printk("%s: SCSI channel %u enabled, host target ID %d.\n",
947              BN(j), i, sh[j]->this_id);
948 
949    return TRUE;
950 }
951 
internal_setup(char * str,int * ints)952 static void internal_setup(char *str, int *ints) {
953    int i, argc = ints[0];
954    char *cur = str, *pc;
955 
956    if (argc > 0) {
957 
958       if (argc > MAX_INT_PARAM) argc = MAX_INT_PARAM;
959 
960       for (i = 0; i < argc; i++) io_port[i] = ints[i + 1];
961 
962       io_port[i] = 0;
963       setup_done = TRUE;
964       }
965 
966    while (cur && (pc = strchr(cur, ':'))) {
967       int val = 0, c = *++pc;
968 
969       if (c == 'n' || c == 'N') val = FALSE;
970       else if (c == 'y' || c == 'Y') val = TRUE;
971       else val = (int) simple_strtoul(pc, NULL, 0);
972 
973       if (!strncmp(cur, "lc:", 3)) linked_comm = val;
974       else if (!strncmp(cur, "of:", 3)) have_old_firmware = val;
975       else if (!strncmp(cur, "mq:", 3))  max_queue_depth = val;
976       else if (!strncmp(cur, "ls:", 3))  link_statistics = val;
977       else if (!strncmp(cur, "et:", 3))  ext_tran = val;
978 
979       if ((cur = strchr(cur, ','))) ++cur;
980       }
981 
982    return;
983 }
984 
option_setup(char * str)985 static int option_setup(char *str) {
986    int ints[MAX_INT_PARAM];
987    char *cur = str;
988    int i = 1;
989 
990    while (cur && isdigit(*cur) && i <= MAX_INT_PARAM) {
991       ints[i++] = simple_strtoul(cur, NULL, 0);
992 
993       if ((cur = strchr(cur, ',')) != NULL) cur++;
994    }
995 
996    ints[0] = i - 1;
997    internal_setup(cur, ints);
998    return 1;
999 }
1000 
u14_34f_detect(Scsi_Host_Template * tpnt)1001 int u14_34f_detect(Scsi_Host_Template *tpnt) {
1002    unsigned int j = 0, k;
1003 
1004    tpnt->proc_name = "u14-34f";
1005 
1006    if(boot_options) option_setup(boot_options);
1007 
1008 #if defined(MODULE)
1009    /* io_port could have been modified when loading as a module */
1010    if(io_port[0] != SKIP) {
1011       setup_done = TRUE;
1012       io_port[MAX_INT_PARAM] = 0;
1013       }
1014 #endif
1015 
1016    for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
1017 
1018    for (k = 0; io_port[k]; k++) {
1019 
1020       if (io_port[k] == SKIP) continue;
1021 
1022       if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt)) j++;
1023       }
1024 
1025    num_boards = j;
1026    return j;
1027 }
1028 
map_dma(unsigned int i,unsigned int j)1029 static inline void map_dma(unsigned int i, unsigned int j) {
1030    unsigned int data_len = 0;
1031    unsigned int k, count, pci_dir;
1032    struct scatterlist *sgpnt;
1033    struct mscp *cpp;
1034    Scsi_Cmnd *SCpnt;
1035 
1036    cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1037    pci_dir = scsi_to_pci_dma_dir(SCpnt->sc_data_direction);
1038 
1039    if (SCpnt->sense_buffer)
1040       cpp->sense_addr = H2DEV(pci_map_single(HD(j)->pdev, SCpnt->sense_buffer,
1041                            sizeof SCpnt->sense_buffer, PCI_DMA_FROMDEVICE));
1042 
1043    cpp->sense_len = sizeof SCpnt->sense_buffer;
1044 
1045    if (!SCpnt->use_sg) {
1046 
1047       /* If we get here with PCI_DMA_NONE, pci_map_single triggers a BUG() */
1048       if (!SCpnt->request_bufflen) pci_dir = PCI_DMA_BIDIRECTIONAL;
1049 
1050       if (SCpnt->request_buffer)
1051          cpp->data_address = H2DEV(pci_map_single(HD(j)->pdev,
1052                   SCpnt->request_buffer, SCpnt->request_bufflen, pci_dir));
1053 
1054       cpp->data_len = H2DEV(SCpnt->request_bufflen);
1055       return;
1056       }
1057 
1058    sgpnt = (struct scatterlist *) SCpnt->request_buffer;
1059    count = pci_map_sg(HD(j)->pdev, sgpnt, SCpnt->use_sg, pci_dir);
1060 
1061    for (k = 0; k < count; k++) {
1062       cpp->sglist[k].address = H2DEV(sg_dma_address(&sgpnt[k]));
1063       cpp->sglist[k].num_bytes = H2DEV(sg_dma_len(&sgpnt[k]));
1064       data_len += sgpnt[k].length;
1065       }
1066 
1067    cpp->sg = TRUE;
1068    cpp->use_sg = SCpnt->use_sg;
1069    cpp->data_address = H2DEV(pci_map_single(HD(j)->pdev, cpp->sglist,
1070                              SCpnt->use_sg * sizeof(struct sg_list), pci_dir));
1071    cpp->data_len = H2DEV(data_len);
1072 }
1073 
unmap_dma(unsigned int i,unsigned int j)1074 static void unmap_dma(unsigned int i, unsigned int j) {
1075    unsigned int pci_dir;
1076    struct mscp *cpp;
1077    Scsi_Cmnd *SCpnt;
1078 
1079    cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1080    pci_dir = scsi_to_pci_dma_dir(SCpnt->sc_data_direction);
1081 
1082    if (DEV2H(cpp->sense_addr))
1083       pci_unmap_single(HD(j)->pdev, DEV2H(cpp->sense_addr),
1084                        DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
1085 
1086    if (SCpnt->use_sg)
1087       pci_unmap_sg(HD(j)->pdev, SCpnt->request_buffer, SCpnt->use_sg, pci_dir);
1088 
1089    if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
1090 
1091    if (DEV2H(cpp->data_address))
1092       pci_unmap_single(HD(j)->pdev, DEV2H(cpp->data_address),
1093                        DEV2H(cpp->data_len), pci_dir);
1094 }
1095 
sync_dma(unsigned int i,unsigned int j)1096 static void sync_dma(unsigned int i, unsigned int j) {
1097    unsigned int pci_dir;
1098    struct mscp *cpp;
1099    Scsi_Cmnd *SCpnt;
1100 
1101    cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1102    pci_dir = scsi_to_pci_dma_dir(SCpnt->sc_data_direction);
1103 
1104    if (DEV2H(cpp->sense_addr))
1105       pci_dma_sync_single(HD(j)->pdev, DEV2H(cpp->sense_addr),
1106                           DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
1107 
1108    if (SCpnt->use_sg)
1109       pci_dma_sync_sg(HD(j)->pdev, SCpnt->request_buffer,
1110                          SCpnt->use_sg, pci_dir);
1111 
1112    if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
1113 
1114    if (DEV2H(cpp->data_address))
1115       pci_dma_sync_single(HD(j)->pdev, DEV2H(cpp->data_address),
1116                        DEV2H(cpp->data_len), pci_dir);
1117 }
1118 
scsi_to_dev_dir(unsigned int i,unsigned int j)1119 static inline void scsi_to_dev_dir(unsigned int i, unsigned int j) {
1120    unsigned int k;
1121 
1122    static const unsigned char data_out_cmds[] = {
1123       0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
1124       0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
1125       0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b, 0x5d
1126       };
1127 
1128    static const unsigned char data_none_cmds[] = {
1129       0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
1130       0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
1131       0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5, 0x00
1132       };
1133 
1134    struct mscp *cpp;
1135    Scsi_Cmnd *SCpnt;
1136 
1137    cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1138 
1139    if (SCpnt->sc_data_direction == SCSI_DATA_READ) {
1140       cpp->xdir = DTD_IN;
1141       return;
1142       }
1143    else if (SCpnt->sc_data_direction == SCSI_DATA_WRITE) {
1144       cpp->xdir = DTD_OUT;
1145       return;
1146       }
1147    else if (SCpnt->sc_data_direction == SCSI_DATA_NONE) {
1148       cpp->xdir = DTD_NONE;
1149       return;
1150       }
1151 
1152    if (SCpnt->sc_data_direction != SCSI_DATA_UNKNOWN)
1153       panic("%s: qcomm, invalid SCpnt->sc_data_direction.\n", BN(j));
1154 
1155    cpp->xdir = DTD_IN;
1156 
1157    for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
1158       if (SCpnt->cmnd[0] == data_out_cmds[k]) {
1159          cpp->xdir = DTD_OUT;
1160          break;
1161          }
1162 
1163    if (cpp->xdir == DTD_IN)
1164       for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
1165          if (SCpnt->cmnd[0] == data_none_cmds[k]) {
1166             cpp->xdir = DTD_NONE;
1167             break;
1168             }
1169 
1170 }
1171 
do_qcomm(Scsi_Cmnd * SCpnt,void (* done)(Scsi_Cmnd *))1172 static inline int do_qcomm(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
1173    unsigned int i, j, k;
1174    struct mscp *cpp;
1175 
1176    /* j is the board number */
1177    j = ((struct hostdata *) SCpnt->host->hostdata)->board_number;
1178 
1179    if (SCpnt->host_scribble)
1180       panic("%s: qcomm, pid %ld, SCpnt %p already active.\n",
1181             BN(j), SCpnt->pid, SCpnt);
1182 
1183    /* i is the mailbox number, look for the first free mailbox
1184       starting from last_cp_used */
1185    i = HD(j)->last_cp_used + 1;
1186 
1187    for (k = 0; k < sh[j]->can_queue; k++, i++) {
1188 
1189       if (i >= sh[j]->can_queue) i = 0;
1190 
1191       if (HD(j)->cp_stat[i] == FREE) {
1192          HD(j)->last_cp_used = i;
1193          break;
1194          }
1195       }
1196 
1197    if (k == sh[j]->can_queue) {
1198       printk("%s: qcomm, no free mailbox.\n", BN(j));
1199       return 1;
1200       }
1201 
1202    /* Set pointer to control packet structure */
1203    cpp = &HD(j)->cp[i];
1204 
1205    memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
1206    SCpnt->scsi_done = done;
1207    cpp->cpp_index = i;
1208    SCpnt->host_scribble = (unsigned char *) &cpp->cpp_index;
1209 
1210    if (do_trace) printk("%s: qcomm, mbox %d, target %d.%d:%d, pid %ld.\n",
1211                         BN(j), i, SCpnt->channel, SCpnt->target,
1212                         SCpnt->lun, SCpnt->pid);
1213 
1214    cpp->opcode = OP_SCSI;
1215    cpp->channel = SCpnt->channel;
1216    cpp->target = SCpnt->target;
1217    cpp->lun = SCpnt->lun;
1218    cpp->SCpnt = SCpnt;
1219    cpp->cdb_len = SCpnt->cmd_len;
1220    memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
1221 
1222    /* Use data transfer direction SCpnt->sc_data_direction */
1223    scsi_to_dev_dir(i, j);
1224 
1225    /* Map DMA buffers and SG list */
1226    map_dma(i, j);
1227 
1228    if (linked_comm && SCpnt->device->queue_depth > 2
1229                                      && TLDEV(SCpnt->device->type)) {
1230       HD(j)->cp_stat[i] = READY;
1231       flush_dev(SCpnt->device, SCpnt->request.sector, j, FALSE);
1232       return 0;
1233       }
1234 
1235    if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1236       unmap_dma(i, j);
1237       SCpnt->host_scribble = NULL;
1238       printk("%s: qcomm, target %d.%d:%d, pid %ld, adapter busy.\n",
1239              BN(j), SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid);
1240       return 1;
1241       }
1242 
1243    /* Store pointer in OGM address bytes */
1244    outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
1245 
1246    /* Issue OGM interrupt */
1247    outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
1248 
1249    HD(j)->cp_stat[i] = IN_USE;
1250    return 0;
1251 }
1252 
u14_34f_queuecommand(Scsi_Cmnd * SCpnt,void (* done)(Scsi_Cmnd *))1253 int u14_34f_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
1254    int rtn;
1255 
1256    rtn = do_qcomm(SCpnt, done);
1257    return rtn;
1258 }
1259 
do_abort(Scsi_Cmnd * SCarg)1260 static inline int do_abort(Scsi_Cmnd *SCarg) {
1261    unsigned int i, j;
1262 
1263    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1264 
1265    if (SCarg->host_scribble == NULL) {
1266       printk("%s: abort, target %d.%d:%d, pid %ld inactive.\n",
1267              BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1268       return SUCCESS;
1269       }
1270 
1271    i = *(unsigned int *)SCarg->host_scribble;
1272    printk("%s: abort, mbox %d, target %d.%d:%d, pid %ld.\n",
1273           BN(j), i, SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1274 
1275    if (i >= sh[j]->can_queue)
1276       panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
1277 
1278    if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1279       printk("%s: abort, timeout error.\n", BN(j));
1280       return FAILED;
1281       }
1282 
1283    if (HD(j)->cp_stat[i] == FREE) {
1284       printk("%s: abort, mbox %d is free.\n", BN(j), i);
1285       return SUCCESS;
1286       }
1287 
1288    if (HD(j)->cp_stat[i] == IN_USE) {
1289       printk("%s: abort, mbox %d is in use.\n", BN(j), i);
1290 
1291       if (SCarg != HD(j)->cp[i].SCpnt)
1292          panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1293                BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
1294 
1295       if (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED)
1296          printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
1297 
1298       if (SCarg->eh_state == SCSI_STATE_TIMEOUT) {
1299          unmap_dma(i, j);
1300          SCarg->host_scribble = NULL;
1301          HD(j)->cp_stat[i] = FREE;
1302          printk("%s, abort, mbox %d, eh_state timeout, pid %ld.\n",
1303                 BN(j), i, SCarg->pid);
1304          return SUCCESS;
1305          }
1306 
1307       return FAILED;
1308       }
1309 
1310    if (HD(j)->cp_stat[i] == IN_RESET) {
1311       printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
1312       return FAILED;
1313       }
1314 
1315    if (HD(j)->cp_stat[i] == LOCKED) {
1316       printk("%s: abort, mbox %d is locked.\n", BN(j), i);
1317       return SUCCESS;
1318       }
1319 
1320    if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1321       unmap_dma(i, j);
1322       SCarg->result = DID_ABORT << 16;
1323       SCarg->host_scribble = NULL;
1324       HD(j)->cp_stat[i] = FREE;
1325       printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1326              BN(j), i, SCarg->pid);
1327       SCarg->scsi_done(SCarg);
1328       return SUCCESS;
1329       }
1330 
1331    panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
1332 }
1333 
u14_34f_abort(Scsi_Cmnd * SCarg)1334 int u14_34f_abort(Scsi_Cmnd *SCarg) {
1335 
1336    return do_abort(SCarg);
1337 }
1338 
do_reset(Scsi_Cmnd * SCarg)1339 static inline int do_reset(Scsi_Cmnd *SCarg) {
1340    unsigned int i, j, time, k, c, limit = 0;
1341    int arg_done = FALSE;
1342    Scsi_Cmnd *SCpnt;
1343 
1344    j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1345    printk("%s: reset, enter, target %d.%d:%d, pid %ld.\n",
1346           BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1347 
1348    if (SCarg->host_scribble == NULL)
1349       printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
1350 
1351    if (HD(j)->in_reset) {
1352       printk("%s: reset, exit, already in reset.\n", BN(j));
1353       return FAILED;
1354       }
1355 
1356    if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1357       printk("%s: reset, exit, timeout error.\n", BN(j));
1358       return FAILED;
1359       }
1360 
1361    HD(j)->retries = 0;
1362 
1363    for (c = 0; c <= sh[j]->max_channel; c++)
1364       for (k = 0; k < sh[j]->max_id; k++) {
1365          HD(j)->target_redo[k][c] = TRUE;
1366          HD(j)->target_to[k][c] = 0;
1367          }
1368 
1369    for (i = 0; i < sh[j]->can_queue; i++) {
1370 
1371       if (HD(j)->cp_stat[i] == FREE) continue;
1372 
1373       if (HD(j)->cp_stat[i] == LOCKED) {
1374          HD(j)->cp_stat[i] = FREE;
1375          printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
1376          continue;
1377          }
1378 
1379       if (!(SCpnt = HD(j)->cp[i].SCpnt))
1380          panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
1381 
1382       if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1383          HD(j)->cp_stat[i] = ABORTING;
1384          printk("%s: reset, mbox %d aborting, pid %ld.\n",
1385                 BN(j), i, SCpnt->pid);
1386          }
1387 
1388       else {
1389          HD(j)->cp_stat[i] = IN_RESET;
1390          printk("%s: reset, mbox %d in reset, pid %ld.\n",
1391                 BN(j), i, SCpnt->pid);
1392          }
1393 
1394       if (SCpnt->host_scribble == NULL)
1395          panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
1396 
1397       if (*(unsigned int *)SCpnt->host_scribble != i)
1398          panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
1399 
1400       if (SCpnt->scsi_done == NULL)
1401          panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
1402 
1403       if (SCpnt == SCarg) arg_done = TRUE;
1404       }
1405 
1406    if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1407       printk("%s: reset, cannot reset, timeout error.\n", BN(j));
1408       return FAILED;
1409       }
1410 
1411    outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
1412    printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
1413 
1414 #if defined(DEBUG_RESET)
1415    do_trace = TRUE;
1416 #endif
1417 
1418    HD(j)->in_reset = TRUE;
1419 
1420    spin_unlock_irq(&io_request_lock);
1421    time = jiffies;
1422    while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
1423    spin_lock_irq(&io_request_lock);
1424 
1425    printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
1426 
1427    for (i = 0; i < sh[j]->can_queue; i++) {
1428 
1429       if (HD(j)->cp_stat[i] == IN_RESET) {
1430          SCpnt = HD(j)->cp[i].SCpnt;
1431          unmap_dma(i, j);
1432          SCpnt->result = DID_RESET << 16;
1433          SCpnt->host_scribble = NULL;
1434 
1435          /* This mailbox is still waiting for its interrupt */
1436          HD(j)->cp_stat[i] = LOCKED;
1437 
1438          printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
1439                 BN(j), i, SCpnt->pid);
1440          }
1441 
1442       else if (HD(j)->cp_stat[i] == ABORTING) {
1443          SCpnt = HD(j)->cp[i].SCpnt;
1444          unmap_dma(i, j);
1445          SCpnt->result = DID_RESET << 16;
1446          SCpnt->host_scribble = NULL;
1447 
1448          /* This mailbox was never queued to the adapter */
1449          HD(j)->cp_stat[i] = FREE;
1450 
1451          printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
1452                 BN(j), i, SCpnt->pid);
1453          }
1454 
1455       else
1456 
1457          /* Any other mailbox has already been set free by interrupt */
1458          continue;
1459 
1460       SCpnt->scsi_done(SCpnt);
1461       }
1462 
1463    HD(j)->in_reset = FALSE;
1464    do_trace = FALSE;
1465 
1466    if (arg_done) printk("%s: reset, exit, pid %ld done.\n", BN(j), SCarg->pid);
1467    else          printk("%s: reset, exit.\n", BN(j));
1468 
1469    return SUCCESS;
1470 }
1471 
u14_34f_reset(Scsi_Cmnd * SCarg)1472 int u14_34f_reset(Scsi_Cmnd *SCarg) {
1473 
1474    return do_reset(SCarg);
1475 }
1476 
u14_34f_biosparam(Disk * disk,kdev_t dev,int * dkinfo)1477 int u14_34f_biosparam(Disk *disk, kdev_t dev, int *dkinfo) {
1478    unsigned int j = 0;
1479    int size = disk->capacity;
1480 
1481    dkinfo[0] = HD(j)->heads;
1482    dkinfo[1] = HD(j)->sectors;
1483    dkinfo[2] = size / (HD(j)->heads * HD(j)->sectors);
1484 
1485    if (ext_tran && (scsicam_bios_param(disk, dev, dkinfo) < 0)) {
1486       dkinfo[0] = 255;
1487       dkinfo[1] = 63;
1488       dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
1489       }
1490 
1491 #if defined (DEBUG_GEOMETRY)
1492    printk ("%s: biosparam, head=%d, sec=%d, cyl=%d.\n", driver_name,
1493            dkinfo[0], dkinfo[1], dkinfo[2]);
1494 #endif
1495 
1496    return FALSE;
1497 }
1498 
sort(unsigned long sk[],unsigned int da[],unsigned int n,unsigned int rev)1499 static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
1500                  unsigned int rev) {
1501    unsigned int i, j, k, y;
1502    unsigned long x;
1503 
1504    for (i = 0; i < n - 1; i++) {
1505       k = i;
1506 
1507       for (j = k + 1; j < n; j++)
1508          if (rev) {
1509             if (sk[j] > sk[k]) k = j;
1510             }
1511          else {
1512             if (sk[j] < sk[k]) k = j;
1513             }
1514 
1515       if (k != i) {
1516          x = sk[k]; sk[k] = sk[i]; sk[i] = x;
1517          y = da[k]; da[k] = da[i]; da[i] = y;
1518          }
1519       }
1520 
1521    return;
1522    }
1523 
reorder(unsigned int j,unsigned long cursec,unsigned int ihdlr,unsigned int il[],unsigned int n_ready)1524 static inline int reorder(unsigned int j, unsigned long cursec,
1525                  unsigned int ihdlr, unsigned int il[], unsigned int n_ready) {
1526    Scsi_Cmnd *SCpnt;
1527    struct mscp *cpp;
1528    unsigned int k, n;
1529    unsigned int rev = FALSE, s = TRUE, r = TRUE;
1530    unsigned int input_only = TRUE, overlap = FALSE;
1531    unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
1532    unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
1533    unsigned long ioseek = 0;
1534 
1535    static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
1536    static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
1537    static unsigned int readysorted = 0, revcount = 0;
1538    static unsigned long seeksorted = 0, seeknosort = 0;
1539 
1540    if (link_statistics && !(++flushcount % link_statistics))
1541       printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"\
1542              " av %ldK as %ldK.\n", flushcount, batchcount, inputcount,
1543              ovlcount, readycount, readysorted, sortcount, revcount,
1544              seeknosort / (readycount + 1),
1545              seeksorted / (readycount + 1));
1546 
1547    if (n_ready <= 1) return FALSE;
1548 
1549    for (n = 0; n < n_ready; n++) {
1550       k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1551 
1552       if (!(cpp->xdir == DTD_IN)) input_only = FALSE;
1553 
1554       if (SCpnt->request.sector < minsec) minsec = SCpnt->request.sector;
1555       if (SCpnt->request.sector > maxsec) maxsec = SCpnt->request.sector;
1556 
1557       sl[n] = SCpnt->request.sector;
1558       ioseek += SCpnt->request.nr_sectors;
1559 
1560       if (!n) continue;
1561 
1562       if (sl[n] < sl[n - 1]) s = FALSE;
1563       if (sl[n] > sl[n - 1]) r = FALSE;
1564 
1565       if (link_statistics) {
1566          if (sl[n] > sl[n - 1])
1567             seek += sl[n] - sl[n - 1];
1568          else
1569             seek += sl[n - 1] - sl[n];
1570          }
1571 
1572       }
1573 
1574    if (link_statistics) {
1575       if (cursec > sl[0]) seek += cursec - sl[0]; else seek += sl[0] - cursec;
1576       }
1577 
1578    if (cursec > ((maxsec + minsec) / 2)) rev = TRUE;
1579 
1580    if (ioseek > ((maxsec - minsec) / 2)) rev = FALSE;
1581 
1582    if (!((rev && r) || (!rev && s))) sort(sl, il, n_ready, rev);
1583 
1584    if (!input_only) for (n = 0; n < n_ready; n++) {
1585       k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1586       ll[n] = SCpnt->request.nr_sectors; pl[n] = SCpnt->pid;
1587 
1588       if (!n) continue;
1589 
1590       if ((sl[n] == sl[n - 1]) || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
1591           || (rev && ((sl[n] + ll[n]) > sl[n - 1]))) overlap = TRUE;
1592       }
1593 
1594    if (overlap) sort(pl, il, n_ready, FALSE);
1595 
1596    if (link_statistics) {
1597       if (cursec > sl[0]) iseek = cursec - sl[0]; else iseek = sl[0] - cursec;
1598       batchcount++; readycount += n_ready; seeknosort += seek / 1024;
1599       if (input_only) inputcount++;
1600       if (overlap) { ovlcount++; seeksorted += iseek / 1024; }
1601       else seeksorted += (iseek + maxsec - minsec) / 1024;
1602       if (rev && !r)     {  revcount++; readysorted += n_ready; }
1603       if (!rev && !s)    { sortcount++; readysorted += n_ready; }
1604       }
1605 
1606 #if defined(DEBUG_LINKED_COMMANDS)
1607    if (link_statistics && (overlap || !(flushcount % link_statistics)))
1608       for (n = 0; n < n_ready; n++) {
1609          k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1610          printk("%s %d.%d:%d pid %ld mb %d fc %d nr %d sec %ld ns %ld"\
1611                 " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
1612                 (ihdlr ? "ihdlr" : "qcomm"), SCpnt->channel, SCpnt->target,
1613                 SCpnt->lun, SCpnt->pid, k, flushcount, n_ready,
1614                 SCpnt->request.sector, SCpnt->request.nr_sectors, cursec,
1615                 YESNO(s), YESNO(r), YESNO(rev), YESNO(input_only),
1616                 YESNO(overlap), cpp->xdir);
1617          }
1618 #endif
1619    return overlap;
1620 }
1621 
flush_dev(Scsi_Device * dev,unsigned long cursec,unsigned int j,unsigned int ihdlr)1622 static void flush_dev(Scsi_Device *dev, unsigned long cursec, unsigned int j,
1623                       unsigned int ihdlr) {
1624    Scsi_Cmnd *SCpnt;
1625    struct mscp *cpp;
1626    unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
1627 
1628    for (k = 0; k < sh[j]->can_queue; k++) {
1629 
1630       if (HD(j)->cp_stat[k] != READY && HD(j)->cp_stat[k] != IN_USE) continue;
1631 
1632       cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1633 
1634       if (SCpnt->device != dev) continue;
1635 
1636       if (HD(j)->cp_stat[k] == IN_USE) return;
1637 
1638       il[n_ready++] = k;
1639       }
1640 
1641    if (reorder(j, cursec, ihdlr, il, n_ready)) n_ready = 1;
1642 
1643    for (n = 0; n < n_ready; n++) {
1644       k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1645 
1646       if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1647          printk("%s: %s, target %d.%d:%d, pid %ld, mbox %d, adapter"\
1648                 " busy, will abort.\n", BN(j), (ihdlr ? "ihdlr" : "qcomm"),
1649                 SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid, k);
1650          HD(j)->cp_stat[k] = ABORTING;
1651          continue;
1652          }
1653 
1654       outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
1655       outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
1656       HD(j)->cp_stat[k] = IN_USE;
1657       }
1658 
1659 }
1660 
ihdlr(int irq,unsigned int j)1661 static inline void ihdlr(int irq, unsigned int j) {
1662    Scsi_Cmnd *SCpnt;
1663    unsigned int i, k, c, status, tstatus, reg, ret;
1664    struct mscp *spp, *cpp;
1665 
1666    if (sh[j]->irq != irq)
1667        panic("%s: ihdlr, irq %d, sh[j]->irq %d.\n", BN(j), irq, sh[j]->irq);
1668 
1669    /* Check if this board need to be serviced */
1670    if (!((reg = inb(sh[j]->io_port + REG_SYS_INTR)) & IRQ_ASSERTED)) return;
1671 
1672    HD(j)->iocount++;
1673 
1674    if (do_trace) printk("%s: ihdlr, enter, irq %d, count %d.\n", BN(j), irq,
1675                         HD(j)->iocount);
1676 
1677    /* Check if this board is still busy */
1678    if (wait_on_busy(sh[j]->io_port, 20 * MAXLOOP)) {
1679       outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
1680       printk("%s: ihdlr, busy timeout error,  irq %d, reg 0x%x, count %d.\n",
1681              BN(j), irq, reg, HD(j)->iocount);
1682       return;
1683       }
1684 
1685    ret = inl(sh[j]->io_port + REG_ICM);
1686 
1687    /* Clear interrupt pending flag */
1688    outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
1689 
1690    /* Find the mailbox to be serviced on this board */
1691    for (i = 0; i < sh[j]->can_queue; i++)
1692       if (H2DEV(HD(j)->cp[i].cp_dma_addr) == ret) break;
1693 
1694    if (i >= sh[j]->can_queue)
1695       panic("%s: ihdlr, invalid mscp bus address %p, cp0 %p.\n", BN(j),
1696             (void *)ret, (void *)H2DEV(HD(j)->cp[0].cp_dma_addr));
1697 
1698    cpp = &(HD(j)->cp[i]);
1699    spp = cpp;
1700 
1701 #if defined(DEBUG_GENERATE_ABORTS)
1702    if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 500) < 3)) return;
1703 #endif
1704 
1705    if (HD(j)->cp_stat[i] == IGNORE) {
1706       HD(j)->cp_stat[i] = FREE;
1707       return;
1708       }
1709    else if (HD(j)->cp_stat[i] == LOCKED) {
1710       HD(j)->cp_stat[i] = FREE;
1711       printk("%s: ihdlr, mbox %d unlocked, count %d.\n", BN(j), i,
1712              HD(j)->iocount);
1713       return;
1714       }
1715    else if (HD(j)->cp_stat[i] == FREE) {
1716       printk("%s: ihdlr, mbox %d is free, count %d.\n", BN(j), i,
1717              HD(j)->iocount);
1718       return;
1719       }
1720    else if (HD(j)->cp_stat[i] == IN_RESET)
1721       printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
1722    else if (HD(j)->cp_stat[i] != IN_USE)
1723       panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
1724             BN(j), i, HD(j)->cp_stat[i]);
1725 
1726    HD(j)->cp_stat[i] = FREE;
1727    SCpnt = cpp->SCpnt;
1728 
1729    if (SCpnt == NULL) panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
1730 
1731    if (SCpnt->host_scribble == NULL)
1732       panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", BN(j), i,
1733             SCpnt->pid, SCpnt);
1734 
1735    if (*(unsigned int *)SCpnt->host_scribble != i)
1736       panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
1737             BN(j), i, SCpnt->pid, *(unsigned int *)SCpnt->host_scribble);
1738 
1739    sync_dma(i, j);
1740 
1741    if (linked_comm && SCpnt->device->queue_depth > 2
1742                                      && TLDEV(SCpnt->device->type))
1743       flush_dev(SCpnt->device, SCpnt->request.sector, j, TRUE);
1744 
1745    tstatus = status_byte(spp->target_status);
1746 
1747 #if defined(DEBUG_GENERATE_ERRORS)
1748    if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 200) < 2))
1749                                            spp->adapter_status = 0x01;
1750 #endif
1751 
1752    switch (spp->adapter_status) {
1753       case ASOK:     /* status OK */
1754 
1755          /* Forces a reset if a disk drive keeps returning BUSY */
1756          if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
1757             status = DID_ERROR << 16;
1758 
1759          /* If there was a bus reset, redo operation on each target */
1760          else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
1761                   && HD(j)->target_redo[SCpnt->target][SCpnt->channel])
1762             status = DID_BUS_BUSY << 16;
1763 
1764          /* Works around a flaw in scsi.c */
1765          else if (tstatus == CHECK_CONDITION
1766                   && SCpnt->device->type == TYPE_DISK
1767                   && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
1768             status = DID_BUS_BUSY << 16;
1769 
1770          else
1771             status = DID_OK << 16;
1772 
1773          if (tstatus == GOOD)
1774             HD(j)->target_redo[SCpnt->target][SCpnt->channel] = FALSE;
1775 
1776          if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
1777              (!(tstatus == CHECK_CONDITION && HD(j)->iocount <= 1000 &&
1778                (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
1779             printk("%s: ihdlr, target %d.%d:%d, pid %ld, "\
1780                    "target_status 0x%x, sense key 0x%x.\n", BN(j),
1781                    SCpnt->channel, SCpnt->target, SCpnt->lun,
1782                    SCpnt->pid, spp->target_status,
1783                    SCpnt->sense_buffer[2]);
1784 
1785          HD(j)->target_to[SCpnt->target][SCpnt->channel] = 0;
1786 
1787          if (HD(j)->last_retried_pid == SCpnt->pid) HD(j)->retries = 0;
1788 
1789          break;
1790       case ASST:     /* Selection Time Out */
1791 
1792          if (HD(j)->target_to[SCpnt->target][SCpnt->channel] > 1)
1793             status = DID_ERROR << 16;
1794          else {
1795             status = DID_TIME_OUT << 16;
1796             HD(j)->target_to[SCpnt->target][SCpnt->channel]++;
1797             }
1798 
1799          break;
1800 
1801       /* Perform a limited number of internal retries */
1802       case 0x93:     /* Unexpected bus free */
1803       case 0x94:     /* Target bus phase sequence failure */
1804       case 0x96:     /* Illegal SCSI command */
1805       case 0xa3:     /* SCSI bus reset error */
1806 
1807          for (c = 0; c <= sh[j]->max_channel; c++)
1808             for (k = 0; k < sh[j]->max_id; k++)
1809                HD(j)->target_redo[k][c] = TRUE;
1810 
1811 
1812       case 0x92:     /* Data over/under-run */
1813 
1814          if (SCpnt->device->type != TYPE_TAPE
1815              && HD(j)->retries < MAX_INTERNAL_RETRIES) {
1816 
1817 #if defined(DID_SOFT_ERROR)
1818             status = DID_SOFT_ERROR << 16;
1819 #else
1820             status = DID_BUS_BUSY << 16;
1821 #endif
1822 
1823             HD(j)->retries++;
1824             HD(j)->last_retried_pid = SCpnt->pid;
1825             }
1826          else
1827             status = DID_ERROR << 16;
1828 
1829          break;
1830       case 0x01:     /* Invalid command */
1831       case 0x02:     /* Invalid parameters */
1832       case 0x03:     /* Invalid data list */
1833       case 0x84:     /* SCSI bus abort error */
1834       case 0x9b:     /* Auto request sense error */
1835       case 0x9f:     /* Unexpected command complete message error */
1836       case 0xff:     /* Invalid parameter in the S/G list */
1837       default:
1838          status = DID_ERROR << 16;
1839          break;
1840       }
1841 
1842    SCpnt->result = status | spp->target_status;
1843 
1844 #if defined(DEBUG_INTERRUPT)
1845    if (SCpnt->result || do_trace)
1846 #else
1847    if ((spp->adapter_status != ASOK && HD(j)->iocount >  1000) ||
1848        (spp->adapter_status != ASOK &&
1849         spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
1850         do_trace || msg_byte(spp->target_status))
1851 #endif
1852       printk("%s: ihdlr, mbox %2d, err 0x%x:%x,"\
1853              " target %d.%d:%d, pid %ld, reg 0x%x, count %d.\n",
1854              BN(j), i, spp->adapter_status, spp->target_status,
1855              SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid,
1856              reg, HD(j)->iocount);
1857 
1858    unmap_dma(i, j);
1859 
1860    /* Set the command state to inactive */
1861    SCpnt->host_scribble = NULL;
1862 
1863    SCpnt->scsi_done(SCpnt);
1864 
1865    if (do_trace) printk("%s: ihdlr, exit, irq %d, count %d.\n", BN(j), irq,
1866                         HD(j)->iocount);
1867 
1868    return;
1869 }
1870 
do_interrupt_handler(int irq,void * shap,struct pt_regs * regs)1871 static void do_interrupt_handler(int irq, void *shap, struct pt_regs *regs) {
1872    unsigned int j;
1873    unsigned long spin_flags;
1874 
1875    /* Check if the interrupt must be processed by this handler */
1876    if ((j = (unsigned int)((char *)shap - sha)) >= num_boards) return;
1877 
1878    spin_lock_irqsave(&io_request_lock, spin_flags);
1879    ihdlr(irq, j);
1880    spin_unlock_irqrestore(&io_request_lock, spin_flags);
1881 }
1882 
u14_34f_release(struct Scsi_Host * shpnt)1883 int u14_34f_release(struct Scsi_Host *shpnt) {
1884    unsigned int i, j;
1885 
1886    for (j = 0; sh[j] != NULL && sh[j] != shpnt; j++);
1887 
1888    if (sh[j] == NULL) panic("%s: release, invalid Scsi_Host pointer.\n",
1889                             driver_name);
1890 
1891    if(sh[j]->unchecked_isa_dma) scsi_deregister_blocked_host(sh[j]);
1892 
1893    for (i = 0; i < sh[j]->can_queue; i++)
1894       if ((&HD(j)->cp[i])->sglist) kfree((&HD(j)->cp[i])->sglist);
1895 
1896    for (i = 0; i < sh[j]->can_queue; i++)
1897       pci_unmap_single(HD(j)->pdev, HD(j)->cp[i].cp_dma_addr,
1898                      sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
1899 
1900    free_irq(sh[j]->irq, &sha[j]);
1901 
1902    if (sh[j]->dma_channel != NO_DMA) free_dma(sh[j]->dma_channel);
1903 
1904    release_region(sh[j]->io_port, sh[j]->n_io_port);
1905    scsi_unregister(sh[j]);
1906    return FALSE;
1907 }
1908 
1909 static Scsi_Host_Template driver_template = ULTRASTOR_14_34F;
1910 
1911 #include "scsi_module.c"
1912 
1913 #ifndef MODULE
1914 __setup("u14-34f=", option_setup);
1915 #endif /* end MODULE */
1916 MODULE_LICENSE("GPL");
1917