Import 2.3.18pre1
[davej-history.git] / drivers / scsi / u14-34f.c
blob211980958484112924eb51a0b325faa806b4b9be
1 /*
2 * u14-34f.c - Low-level driver for UltraStor 14F/34F SCSI host adapters.
4 * 26 Jul 1998 Rev. 4.33 for linux 2.0.35 and 2.1.111
5 * Added command line option (et:[y|n]) to use the existing
6 * translation (returned by scsicam_bios_param) as disk geometry.
7 * The default is et:n, which uses the disk geometry jumpered
8 * on the board.
9 * The default value et:n is compatible with all previous revisions
10 * of this driver.
12 * 28 May 1998 Rev. 4.32 for linux 2.0.33 and 2.1.104
13 * Increased busy timeout from 10 msec. to 200 msec. while
14 * processing interrupts.
16 * 18 May 1998 Rev. 4.31 for linux 2.0.33 and 2.1.102
17 * Improved abort handling during the eh recovery process.
19 * 13 May 1998 Rev. 4.30 for linux 2.0.33 and 2.1.101
20 * The driver is now fully SMP safe, including the
21 * abort and reset routines.
22 * Added command line options (eh:[y|n]) to choose between
23 * new_eh_code and the old scsi code.
24 * If linux version >= 2.1.101 the default is eh:y, while the eh
25 * option is ignored for previous releases and the old scsi code
26 * is used.
28 * 18 Apr 1998 Rev. 4.20 for linux 2.0.33 and 2.1.97
29 * Reworked interrupt handler.
31 * 11 Apr 1998 rev. 4.05 for linux 2.0.33 and 2.1.95
32 * Major reliability improvement: when a batch with overlapping
33 * requests is detected, requests are queued one at a time
34 * eliminating any possible board or drive reordering.
36 * 10 Apr 1998 rev. 4.04 for linux 2.0.33 and 2.1.95
37 * Improved SMP support (if linux version >= 2.1.95).
39 * 9 Apr 1998 rev. 4.03 for linux 2.0.33 and 2.1.94
40 * Performance improvement: when sequential i/o is detected,
41 * always use direct sort instead of reverse sort.
43 * 4 Apr 1998 rev. 4.02 for linux 2.0.33 and 2.1.92
44 * io_port is now unsigned long.
46 * 17 Mar 1998 rev. 4.01 for linux 2.0.33 and 2.1.88
47 * Use new scsi error handling code (if linux version >= 2.1.88).
48 * Use new interrupt code.
50 * 12 Sep 1997 rev. 3.11 for linux 2.0.30 and 2.1.55
51 * Use of udelay inside the wait loops to avoid timeout
52 * problems with fast cpus.
53 * Removed check about useless calls to the interrupt service
54 * routine (reported on SMP systems only).
55 * At initialization time "sorted/unsorted" is displayed instead
56 * of "linked/unlinked" to reinforce the fact that "linking" is
57 * nothing but "elevator sorting" in the actual implementation.
59 * 17 May 1997 rev. 3.10 for linux 2.0.30 and 2.1.38
60 * Use of serial_number_at_timeout in abort and reset processing.
61 * Use of the __initfunc and __initdata macro in setup code.
62 * Minor cleanups in the list_statistics code.
64 * 24 Feb 1997 rev. 3.00 for linux 2.0.29 and 2.1.26
65 * When loading as a module, parameter passing is now supported
66 * both in 2.0 and in 2.1 style.
67 * Fixed data transfer direction for some SCSI opcodes.
68 * Immediate acknowledge to request sense commands.
69 * Linked commands to each disk device are now reordered by elevator
70 * sorting. Rare cases in which reordering of write requests could
71 * cause wrong results are managed.
73 * 18 Jan 1997 rev. 2.60 for linux 2.1.21 and 2.0.28
74 * Added command line options to enable/disable linked commands
75 * (lc:[y|n]), old firmware support (of:[y|n]) and to set the max
76 * queue depth (mq:xx). Default is "u14-34f=lc:n,of:n,mq:8".
77 * Improved command linking.
79 * 8 Jan 1997 rev. 2.50 for linux 2.1.20 and 2.0.27
80 * Added linked command support.
82 * 3 Dec 1996 rev. 2.40 for linux 2.1.14 and 2.0.27
83 * Added queue depth adjustment.
85 * 22 Nov 1996 rev. 2.30 for linux 2.1.12 and 2.0.26
86 * The list of i/o ports to be probed can be overwritten by the
87 * "u14-34f=port0,port1,...." boot command line option.
88 * Scatter/gather lists are now allocated by a number of kmalloc
89 * calls, in order to avoid the previous size limit of 64Kb.
91 * 16 Nov 1996 rev. 2.20 for linux 2.1.10 and 2.0.25
92 * Added multichannel support.
94 * 27 Sep 1996 rev. 2.12 for linux 2.1.0
95 * Portability cleanups (virtual/bus addressing, little/big endian
96 * support).
98 * 09 Jul 1996 rev. 2.11 for linux 2.0.4
99 * "Data over/under-run" no longer implies a redo on all targets.
100 * Number of internal retries is now limited.
102 * 16 Apr 1996 rev. 2.10 for linux 1.3.90
103 * New argument "reset_flags" to the reset routine.
105 * 21 Jul 1995 rev. 2.02 for linux 1.3.11
106 * Fixed Data Transfer Direction for some SCSI commands.
108 * 13 Jun 1995 rev. 2.01 for linux 1.2.10
109 * HAVE_OLD_UX4F_FIRMWARE should be defined for U34F boards when
110 * the firmware prom is not the latest one (28008-006).
112 * 11 Mar 1995 rev. 2.00 for linux 1.2.0
113 * Fixed a bug which prevented media change detection for removable
114 * disk drives.
116 * 23 Feb 1995 rev. 1.18 for linux 1.1.94
117 * Added a check for scsi_register returning NULL.
119 * 11 Feb 1995 rev. 1.17 for linux 1.1.91
120 * U14F qualified to run with 32 sglists.
121 * Now DEBUG_RESET is disabled by default.
123 * 9 Feb 1995 rev. 1.16 for linux 1.1.90
124 * Use host->wish_block instead of host->block.
126 * 8 Feb 1995 rev. 1.15 for linux 1.1.89
127 * Cleared target_time_out counter while performing a reset.
129 * 28 Jan 1995 rev. 1.14 for linux 1.1.86
130 * Added module support.
131 * Log and do a retry when a disk drive returns a target status
132 * different from zero on a recovered error.
133 * Auto detects if U14F boards have an old firmware revision.
134 * Max number of scatter/gather lists set to 16 for all boards
135 * (most installation run fine using 33 sglists, while other
136 * has problems when using more then 16).
138 * 16 Jan 1995 rev. 1.13 for linux 1.1.81
139 * Display a message if check_region detects a port address
140 * already in use.
142 * 15 Dec 1994 rev. 1.12 for linux 1.1.74
143 * The host->block flag is set for all the detected ISA boards.
145 * 30 Nov 1994 rev. 1.11 for linux 1.1.68
146 * Redo i/o on target status CHECK_CONDITION for TYPE_DISK only.
147 * Added optional support for using a single board at a time.
149 * 14 Nov 1994 rev. 1.10 for linux 1.1.63
151 * 28 Oct 1994 rev. 1.09 for linux 1.1.58 Final BETA release.
152 * 16 Jul 1994 rev. 1.00 for linux 1.1.29 Initial ALPHA release.
154 * This driver is a total replacement of the original UltraStor
155 * scsi driver, but it supports ONLY the 14F and 34F boards.
156 * It can be configured in the same kernel in which the original
157 * ultrastor driver is configured to allow the original U24F
158 * support.
160 * Multiple U14F and/or U34F host adapters are supported.
162 * Copyright (C) 1994-1998 Dario Ballabio (dario@milano.europe.dg.com)
164 * Redistribution and use in source and binary forms, with or without
165 * modification, are permitted provided that redistributions of source
166 * code retain the above copyright notice and this comment without
167 * modification.
169 * WARNING: if your 14/34F board has an old firmware revision (see below)
170 * you must change "#undef" into "#define" in the following
171 * statement.
173 #undef HAVE_OLD_UX4F_FIRMWARE
175 * The UltraStor 14F, 24F, and 34F are a family of intelligent, high
176 * performance SCSI-2 host adapters.
177 * Here is the scoop on the various models:
179 * 14F - ISA first-party DMA HA with floppy support and WD1003 emulation.
180 * 24F - EISA Bus Master HA with floppy support and WD1003 emulation.
181 * 34F - VESA Local-Bus Bus Master HA (no WD1003 emulation).
183 * This code has been tested with up to two U14F boards, using both
184 * firmware 28004-005/38004-004 (BIOS rev. 2.00) and the latest firmware
185 * 28004-006/38004-005 (BIOS rev. 2.01).
187 * The latest firmware is required in order to get reliable operations when
188 * clustering is enabled. ENABLE_CLUSTERING provides a performance increase
189 * up to 50% on sequential access.
191 * Since the Scsi_Host_Template structure is shared among all 14F and 34F,
192 * the last setting of use_clustering is in effect for all of these boards.
194 * Here a sample configuration using two U14F boards:
196 U14F0: ISA 0x330, BIOS 0xc8000, IRQ 11, DMA 5, SG 32, MB 16, of:n, lc:y, mq:8.
197 U14F1: ISA 0x340, BIOS 0x00000, IRQ 10, DMA 6, SG 32, MB 16, of:n, lc:y, mq:8.
199 * The boot controller must have its BIOS enabled, while other boards can
200 * have their BIOS disabled, or enabled to an higher address.
201 * Boards are named Ux4F0, Ux4F1..., according to the port address order in
202 * the io_port[] array.
204 * The following facts are based on real testing results (not on
205 * documentation) on the above U14F board.
207 * - The U14F board should be jumpered for bus on time less or equal to 7
208 * microseconds, while the default is 11 microseconds. This is order to
209 * get acceptable performance while using floppy drive and hard disk
210 * together. The jumpering for 7 microseconds is: JP13 pin 15-16,
211 * JP14 pin 7-8 and pin 9-10.
212 * The reduction has a little impact on scsi performance.
214 * - If scsi bus length exceeds 3m., the scsi bus speed needs to be reduced
215 * from 10Mhz to 5Mhz (do this by inserting a jumper on JP13 pin 7-8).
217 * - If U14F on board firmware is older than 28004-006/38004-005,
218 * the U14F board is unable to provide reliable operations if the scsi
219 * request length exceeds 16Kbyte. When this length is exceeded the
220 * behavior is:
221 * - adapter_status equal 0x96 or 0xa3 or 0x93 or 0x94;
222 * - adapter_status equal 0 and target_status equal 2 on for all targets
223 * in the next operation following the reset.
224 * This sequence takes a long time (>3 seconds), so in the meantime
225 * the SD_TIMEOUT in sd.c could expire giving rise to scsi aborts
226 * (SD_TIMEOUT has been increased from 3 to 6 seconds in 1.1.31).
227 * Because of this I had to DISABLE_CLUSTERING and to work around the
228 * bus reset in the interrupt service routine, returning DID_BUS_BUSY
229 * so that the operations are retried without complains from the scsi.c
230 * code.
231 * Any reset of the scsi bus is going to kill tape operations, since
232 * no retry is allowed for tapes. Bus resets are more likely when the
233 * scsi bus is under heavy load.
234 * Requests using scatter/gather have a maximum length of 16 x 1024 bytes
235 * when DISABLE_CLUSTERING is in effect, but unscattered requests could be
236 * larger than 16Kbyte.
238 * The new firmware has fixed all the above problems.
240 * For U34F boards the latest bios prom is 38008-002 (BIOS rev. 2.01),
241 * the latest firmware prom is 28008-006. Older firmware 28008-005 has
242 * problems when using more then 16 scatter/gather lists.
244 * The list of i/o ports to be probed can be totally replaced by the
245 * boot command line option: "u14-34f=port0,port1,port2,...", where the
246 * port0, port1... arguments are ISA/VESA addresses to be probed.
247 * For example using "u14-34f=0x230,0x340", the driver probes only the two
248 * addresses 0x230 and 0x340 in this order; "u14-34f=0" totally disables
249 * this driver.
251 * After the optional list of detection probes, other possible command line
252 * options are:
254 * eh:y use new scsi code (linux 2.2 only);
255 * eh:n use old scsi code;
256 * et:y use disk geometry returned by scsicam_bios_param;
257 * et:n use disk geometry jumpered on the board;
258 * lc:y enables linked commands;
259 * lc:n disables linked commands;
260 * of:y enables old firmware support;
261 * of:n disables old firmware support;
262 * mq:xx set the max queue depth to the value xx (2 <= xx <= 8).
264 * The default value is: "u14-34f=lc:n,of:n,mq:8,et:n".
265 * An example using the list of detection probes could be:
266 * "u14-34f=0x230,0x340,lc:y,of:n,mq:4,eh:n,et:n".
268 * When loading as a module, parameters can be specified as well.
269 * The above example would be (use 1 in place of y and 0 in place of n):
271 * modprobe u14-34f io_port=0x230,0x340 linked_comm=1 have_old_firmware=0 \
272 * max_queue_depth=4 use_new_eh_code=0 ext_tran=0
274 * ----------------------------------------------------------------------------
275 * In this implementation, linked commands are designed to work with any DISK
276 * or CD-ROM, since this linking has only the intent of clustering (time-wise)
277 * and reordering by elevator sorting commands directed to each device,
278 * without any relation with the actual SCSI protocol between the controller
279 * and the device.
280 * If Q is the queue depth reported at boot time for each device (also named
281 * cmds/lun) and Q > 2, whenever there is already an active command to the
282 * device all other commands to the same device (up to Q-1) are kept waiting
283 * in the elevator sorting queue. When the active command completes, the
284 * commands in this queue are sorted by sector address. The sort is chosen
285 * between increasing or decreasing by minimizing the seek distance between
286 * the sector of the commands just completed and the sector of the first
287 * command in the list to be sorted.
288 * Trivial math assures that the unsorted average seek distance when doing
289 * random seeks over S sectors is S/3.
290 * When (Q-1) requests are uniformly distributed over S sectors, the average
291 * distance between two adjacent requests is S/((Q-1) + 1), so the sorted
292 * average seek distance for (Q-1) random requests over S sectors is S/Q.
293 * The elevator sorting hence divides the seek distance by a factor Q/3.
294 * The above pure geometric remarks are valid in all cases and the
295 * driver effectively reduces the seek distance by the predicted factor
296 * when there are Q concurrent read i/o operations on the device, but this
297 * does not necessarily results in a noticeable performance improvement:
298 * your mileage may vary....
300 * Note: command reordering inside a batch of queued commands could cause
301 * wrong results only if there is at least one write request and the
302 * intersection (sector-wise) of all requests is not empty.
303 * When the driver detects a batch including overlapping requests
304 * (a really rare event) strict serial (pid) order is enforced.
305 * ----------------------------------------------------------------------------
307 * The boards are named Ux4F0, Ux4F1,... according to the detection order.
309 * In order to support multiple ISA boards in a reliable way,
310 * the driver sets host->wish_block = TRUE for all ISA boards.
313 #include <linux/version.h>
315 #define LinuxVersionCode(v, p, s) (((v)<<16)+((p)<<8)+(s))
316 #define MAX_INT_PARAM 10
318 #if defined(MODULE)
319 #include <linux/module.h>
321 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,26)
322 MODULE_PARM(io_port, "1-" __MODULE_STRING(MAX_INT_PARAM) "i");
323 MODULE_PARM(linked_comm, "i");
324 MODULE_PARM(have_old_firmware, "i");
325 MODULE_PARM(link_statistics, "i");
326 MODULE_PARM(max_queue_depth, "i");
327 MODULE_PARM(use_new_eh_code, "i");
328 MODULE_PARM(ext_tran, "i");
329 MODULE_AUTHOR("Dario Ballabio");
330 #endif
332 #endif
334 #include <linux/string.h>
335 #include <linux/sched.h>
336 #include <linux/kernel.h>
337 #include <linux/ioport.h>
338 #include <linux/delay.h>
339 #include <asm/io.h>
340 #include <asm/system.h>
341 #include <asm/byteorder.h>
342 #include <linux/proc_fs.h>
343 #include <linux/blk.h>
344 #include "scsi.h"
345 #include "hosts.h"
346 #include "sd.h"
347 #include <asm/dma.h>
348 #include <asm/irq.h>
349 #include "u14-34f.h"
350 #include <linux/stat.h>
351 #include <linux/config.h>
353 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,36)
354 #include <linux/init.h>
355 #else
356 #define __initfunc(A) A
357 #define __initdata
358 #define __init
359 #endif
361 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,101)
362 #include <linux/spinlock.h>
363 #define IRQ_FLAGS
364 #define IRQ_LOCK
365 #define IRQ_LOCK_SAVE
366 #define IRQ_UNLOCK
367 #define IRQ_UNLOCK_RESTORE
368 #define SPIN_FLAGS unsigned long spin_flags;
369 #define SPIN_LOCK spin_lock_irq(&io_request_lock);
370 #define SPIN_LOCK_SAVE spin_lock_irqsave(&io_request_lock, spin_flags);
371 #define SPIN_UNLOCK spin_unlock_irq(&io_request_lock);
372 #define SPIN_UNLOCK_RESTORE \
373 spin_unlock_irqrestore(&io_request_lock, spin_flags);
374 static int use_new_eh_code = TRUE;
375 #else
376 #define IRQ_FLAGS unsigned long irq_flags;
377 #define IRQ_LOCK cli();
378 #define IRQ_LOCK_SAVE do {save_flags(irq_flags); cli();} while (0);
379 #define IRQ_UNLOCK sti();
380 #define IRQ_UNLOCK_RESTORE do {restore_flags(irq_flags);} while (0);
381 #define SPIN_FLAGS
382 #define SPIN_LOCK
383 #define SPIN_LOCK_SAVE
384 #define SPIN_UNLOCK
385 #define SPIN_UNLOCK_RESTORE
386 static int use_new_eh_code = FALSE;
387 #endif
389 struct proc_dir_entry proc_scsi_u14_34f = {
390 PROC_SCSI_U14_34F, 6, "u14_34f",
391 S_IFDIR | S_IRUGO | S_IXUGO, 2
394 /* Values for the PRODUCT_ID ports for the 14/34F */
395 #define PRODUCT_ID1 0x56
396 #define PRODUCT_ID2 0x40 /* NOTE: Only upper nibble is used */
398 /* Subversion values */
399 #define ISA 0
400 #define ESA 1
402 #define OP_HOST_ADAPTER 0x1
403 #define OP_SCSI 0x2
404 #define OP_RESET 0x4
405 #define DTD_SCSI 0x0
406 #define DTD_IN 0x1
407 #define DTD_OUT 0x2
408 #define DTD_NONE 0x3
409 #define HA_CMD_INQUIRY 0x1
410 #define HA_CMD_SELF_DIAG 0x2
411 #define HA_CMD_READ_BUFF 0x3
412 #define HA_CMD_WRITE_BUFF 0x4
414 #undef DEBUG_LINKED_COMMANDS
415 #undef DEBUG_DETECT
416 #undef DEBUG_INTERRUPT
417 #undef DEBUG_RESET
418 #undef DEBUG_GENERATE_ERRORS
419 #undef DEBUG_GENERATE_ABORTS
420 #undef DEBUG_GEOMETRY
422 #define MAX_ISA 3
423 #define MAX_VESA 1
424 #define MAX_EISA 0
425 #define MAX_PCI 0
426 #define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
427 #define MAX_CHANNEL 1
428 #define MAX_LUN 8
429 #define MAX_TARGET 8
430 #define MAX_MAILBOXES 16
431 #define MAX_SGLIST 32
432 #define MAX_SAFE_SGLIST 16
433 #define MAX_INTERNAL_RETRIES 64
434 #define MAX_CMD_PER_LUN 2
435 #define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
437 #define SKIP ULONG_MAX
438 #define FALSE 0
439 #define TRUE 1
440 #define FREE 0
441 #define IN_USE 1
442 #define LOCKED 2
443 #define IN_RESET 3
444 #define IGNORE 4
445 #define READY 5
446 #define ABORTING 6
447 #define NO_DMA 0xff
448 #define MAXLOOP 10000
450 #define REG_LCL_MASK 0
451 #define REG_LCL_INTR 1
452 #define REG_SYS_MASK 2
453 #define REG_SYS_INTR 3
454 #define REG_PRODUCT_ID1 4
455 #define REG_PRODUCT_ID2 5
456 #define REG_CONFIG1 6
457 #define REG_CONFIG2 7
458 #define REG_OGM 8
459 #define REG_ICM 12
460 #define REGION_SIZE 13
461 #define BSY_ASSERTED 0x01
462 #define IRQ_ASSERTED 0x01
463 #define CMD_RESET 0xc0
464 #define CMD_OGM_INTR 0x01
465 #define CMD_CLR_INTR 0x01
466 #define CMD_ENA_INTR 0x81
467 #define ASOK 0x00
468 #define ASST 0x91
470 #define YESNO(a) ((a) ? 'y' : 'n')
471 #define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
473 #define PACKED __attribute__((packed))
475 struct sg_list {
476 unsigned int address; /* Segment Address */
477 unsigned int num_bytes; /* Segment Length */
480 /* MailBox SCSI Command Packet */
481 struct mscp {
482 unsigned char opcode: 3; /* type of command */
483 unsigned char xdir: 2; /* data transfer direction */
484 unsigned char dcn: 1; /* disable disconnect */
485 unsigned char ca: 1; /* use cache (if available) */
486 unsigned char sg: 1; /* scatter/gather operation */
487 unsigned char target: 3; /* SCSI target id */
488 unsigned char channel: 2; /* SCSI channel number */
489 unsigned char lun: 3; /* SCSI logical unit number */
490 unsigned int data_address PACKED; /* transfer data pointer */
491 unsigned int data_len PACKED; /* length in bytes */
492 unsigned int link_address PACKED; /* for linking command chains */
493 unsigned char clink_id; /* identifies command in chain */
494 unsigned char use_sg; /* (if sg is set) 8 bytes per list */
495 unsigned char sense_len;
496 unsigned char scsi_cdbs_len; /* 6, 10, or 12 */
497 unsigned char scsi_cdbs[12]; /* SCSI commands */
498 unsigned char adapter_status; /* non-zero indicates HA error */
499 unsigned char target_status; /* non-zero indicates target error */
500 unsigned int sense_addr PACKED;
501 Scsi_Cmnd *SCpnt;
502 unsigned int index; /* cp index */
503 struct sg_list *sglist;
506 struct hostdata {
507 struct mscp cp[MAX_MAILBOXES]; /* Mailboxes for this board */
508 unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
509 unsigned int last_cp_used; /* Index of last mailbox used */
510 unsigned int iocount; /* Total i/o done for this board */
511 int board_number; /* Number of this board */
512 char board_name[16]; /* Name of this board */
513 char board_id[256]; /* data from INQUIRY on this board */
514 int in_reset; /* True if board is doing a reset */
515 int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
516 int target_redo[MAX_TARGET][MAX_CHANNEL]; /* If TRUE redo i/o on target */
517 unsigned int retries; /* Number of internal retries */
518 unsigned long last_retried_pid; /* Pid of last retried command */
519 unsigned char subversion; /* Bus type, either ISA or ESA */
520 unsigned char heads;
521 unsigned char sectors;
523 /* slot != 0 for the U24F, slot == 0 for both the U14F and U34F */
524 unsigned char slot;
527 static struct Scsi_Host *sh[MAX_BOARDS + 1];
528 static const char *driver_name = "Ux4F";
529 static char sha[MAX_BOARDS];
531 /* Initialize num_boards so that ihdlr can work while detect is in progress */
532 static unsigned int num_boards = MAX_BOARDS;
534 static unsigned long io_port[] __initdata = {
536 /* Space for MAX_INT_PARAM ports usable while loading as a module */
537 SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP, SKIP,
538 SKIP, SKIP,
540 /* Possible ISA/VESA ports */
541 0x330, 0x340, 0x230, 0x240, 0x210, 0x130, 0x140,
543 /* End of list */
547 #define HD(board) ((struct hostdata *) &sh[board]->hostdata)
548 #define BN(board) (HD(board)->board_name)
550 #define SWAP_BYTE(x) ((unsigned long)( \
551 (((unsigned long)(x) & 0x000000ffU) << 24) | \
552 (((unsigned long)(x) & 0x0000ff00U) << 8) | \
553 (((unsigned long)(x) & 0x00ff0000U) >> 8) | \
554 (((unsigned long)(x) & 0xff000000U) >> 24)))
556 #if defined(__BIG_ENDIAN)
557 #define H2DEV(x) SWAP_BYTE(x)
558 #else
559 #define H2DEV(x) (x)
560 #endif
562 #define DEV2H(x) H2DEV(x)
563 #define V2DEV(addr) ((addr) ? H2DEV(virt_to_bus((void *)addr)) : 0)
564 #define DEV2V(addr) ((addr) ? DEV2H(bus_to_virt((unsigned long)addr)) : 0)
566 static void do_interrupt_handler(int, void *, struct pt_regs *);
567 static void flush_dev(Scsi_Device *, unsigned long, unsigned int, unsigned int);
568 static int do_trace = FALSE;
569 static int setup_done = FALSE;
570 static int link_statistics = 0;
571 static int ext_tran = FALSE;
573 #if defined(HAVE_OLD_UX4F_FIRMWARE)
574 static int have_old_firmware = TRUE;
575 #else
576 static int have_old_firmware = FALSE;
577 #endif
579 #if defined(CONFIG_SCSI_U14_34F_LINKED_COMMANDS)
580 static int linked_comm = TRUE;
581 #else
582 static int linked_comm = FALSE;
583 #endif
585 #if defined(CONFIG_SCSI_U14_34F_MAX_TAGS)
586 static int max_queue_depth = CONFIG_SCSI_U14_34F_MAX_TAGS;
587 #else
588 static int max_queue_depth = MAX_CMD_PER_LUN;
589 #endif
591 static void select_queue_depths(struct Scsi_Host *host, Scsi_Device *devlist) {
592 Scsi_Device *dev;
593 int j, ntag = 0, nuntag = 0, tqd, utqd;
594 IRQ_FLAGS
596 IRQ_LOCK_SAVE
597 j = ((struct hostdata *) host->hostdata)->board_number;
599 for(dev = devlist; dev; dev = dev->next) {
601 if (dev->host != host) continue;
603 if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
604 ntag++;
605 else
606 nuntag++;
609 utqd = MAX_CMD_PER_LUN;
611 tqd = (host->can_queue - utqd * nuntag) / (ntag ? ntag : 1);
613 if (tqd > max_queue_depth) tqd = max_queue_depth;
615 if (tqd < MAX_CMD_PER_LUN) tqd = MAX_CMD_PER_LUN;
617 for(dev = devlist; dev; dev = dev->next) {
618 char *tag_suffix = "", *link_suffix = "";
620 if (dev->host != host) continue;
622 if (TLDEV(dev->type) && (dev->tagged_supported || linked_comm))
623 dev->queue_depth = tqd;
624 else
625 dev->queue_depth = utqd;
627 if (TLDEV(dev->type)) {
628 if (linked_comm && dev->queue_depth > 2)
629 link_suffix = ", sorted";
630 else
631 link_suffix = ", unsorted";
634 if (dev->tagged_supported && TLDEV(dev->type) && dev->tagged_queue)
635 tag_suffix = ", tagged";
636 else if (dev->tagged_supported && TLDEV(dev->type))
637 tag_suffix = ", untagged";
639 printk("%s: scsi%d, channel %d, id %d, lun %d, cmds/lun %d%s%s.\n",
640 BN(j), host->host_no, dev->channel, dev->id, dev->lun,
641 dev->queue_depth, link_suffix, tag_suffix);
644 IRQ_UNLOCK_RESTORE
645 return;
648 static inline int wait_on_busy(unsigned long iobase, unsigned int loop) {
650 while (inb(iobase + REG_LCL_INTR) & BSY_ASSERTED) {
651 udelay(1L);
652 if (--loop == 0) return TRUE;
655 return FALSE;
658 static int board_inquiry(unsigned int j) {
659 struct mscp *cpp;
660 unsigned int time, limit = 0;
662 cpp = &HD(j)->cp[0];
663 memset(cpp, 0, sizeof(struct mscp));
664 cpp->opcode = OP_HOST_ADAPTER;
665 cpp->xdir = DTD_IN;
666 cpp->data_address = V2DEV(HD(j)->board_id);
667 cpp->data_len = H2DEV(sizeof(HD(j)->board_id));
668 cpp->scsi_cdbs_len = 6;
669 cpp->scsi_cdbs[0] = HA_CMD_INQUIRY;
671 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
672 printk("%s: board_inquiry, adapter busy.\n", BN(j));
673 return TRUE;
676 HD(j)->cp_stat[0] = IGNORE;
678 /* Clear the interrupt indication */
679 outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
681 /* Store pointer in OGM address bytes */
682 outl(V2DEV(cpp), sh[j]->io_port + REG_OGM);
684 /* Issue OGM interrupt */
685 outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
687 SPIN_UNLOCK
688 IRQ_UNLOCK
689 time = jiffies;
690 while ((jiffies - time) < HZ && limit++ < 20000) udelay(100L);
691 IRQ_LOCK
692 SPIN_LOCK
694 if (cpp->adapter_status || HD(j)->cp_stat[0] != FREE) {
695 HD(j)->cp_stat[0] = FREE;
696 printk("%s: board_inquiry, err 0x%x.\n", BN(j), cpp->adapter_status);
697 return TRUE;
700 return FALSE;
703 static inline int __init port_detect \
704 (unsigned long port_base, unsigned int j, Scsi_Host_Template *tpnt) {
705 unsigned char irq, dma_channel, subversion, i;
706 unsigned char in_byte;
707 char *bus_type, dma_name[16];
709 /* Allowed BIOS base addresses (NULL indicates reserved) */
710 void *bios_segment_table[8] = {
711 NULL,
712 (void *) 0xc4000, (void *) 0xc8000, (void *) 0xcc000, (void *) 0xd0000,
713 (void *) 0xd4000, (void *) 0xd8000, (void *) 0xdc000
716 /* Allowed IRQs */
717 unsigned char interrupt_table[4] = { 15, 14, 11, 10 };
719 /* Allowed DMA channels for ISA (0 indicates reserved) */
720 unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
722 /* Head/sector mappings */
723 struct {
724 unsigned char heads;
725 unsigned char sectors;
726 } mapping_table[4] = {
727 { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 }
730 struct config_1 {
731 unsigned char bios_segment: 3;
732 unsigned char removable_disks_as_fixed: 1;
733 unsigned char interrupt: 2;
734 unsigned char dma_channel: 2;
735 } config_1;
737 struct config_2 {
738 unsigned char ha_scsi_id: 3;
739 unsigned char mapping_mode: 2;
740 unsigned char bios_drive_number: 1;
741 unsigned char tfr_port: 2;
742 } config_2;
744 char name[16];
746 sprintf(name, "%s%d", driver_name, j);
748 if(check_region(port_base, REGION_SIZE)) {
749 printk("%s: address 0x%03lx in use, skipping probe.\n", name, port_base);
750 return FALSE;
753 if (inb(port_base + REG_PRODUCT_ID1) != PRODUCT_ID1) return FALSE;
755 in_byte = inb(port_base + REG_PRODUCT_ID2);
757 if ((in_byte & 0xf0) != PRODUCT_ID2) return FALSE;
759 *(char *)&config_1 = inb(port_base + REG_CONFIG1);
760 *(char *)&config_2 = inb(port_base + REG_CONFIG2);
762 irq = interrupt_table[config_1.interrupt];
763 dma_channel = dma_channel_table[config_1.dma_channel];
764 subversion = (in_byte & 0x0f);
766 /* Board detected, allocate its IRQ */
767 if (request_irq(irq, do_interrupt_handler,
768 SA_INTERRUPT | ((subversion == ESA) ? SA_SHIRQ : 0),
769 driver_name, (void *) &sha[j])) {
770 printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
771 return FALSE;
774 if (subversion == ISA && request_dma(dma_channel, driver_name)) {
775 printk("%s: unable to allocate DMA channel %u, detaching.\n",
776 name, dma_channel);
777 free_irq(irq, &sha[j]);
778 return FALSE;
781 if (have_old_firmware) tpnt->use_clustering = DISABLE_CLUSTERING;
783 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,101)
784 tpnt->use_new_eh_code = use_new_eh_code;
785 #else
786 use_new_eh_code = FALSE;
787 #endif
789 sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
791 if (sh[j] == NULL) {
792 printk("%s: unable to register host, detaching.\n", name);
794 free_irq(irq, &sha[j]);
796 if (subversion == ISA) free_dma(dma_channel);
798 return FALSE;
801 sh[j]->io_port = port_base;
802 sh[j]->unique_id = port_base;
803 sh[j]->n_io_port = REGION_SIZE;
804 sh[j]->base = bios_segment_table[config_1.bios_segment];
805 sh[j]->irq = irq;
806 sh[j]->sg_tablesize = MAX_SGLIST;
807 sh[j]->this_id = config_2.ha_scsi_id;
808 sh[j]->can_queue = MAX_MAILBOXES;
809 sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
810 sh[j]->select_queue_depths = select_queue_depths;
812 #if defined(DEBUG_DETECT)
814 unsigned char sys_mask, lcl_mask;
816 sys_mask = inb(sh[j]->io_port + REG_SYS_MASK);
817 lcl_mask = inb(sh[j]->io_port + REG_LCL_MASK);
818 printk("SYS_MASK 0x%x, LCL_MASK 0x%x.\n", sys_mask, lcl_mask);
820 #endif
822 /* Probably a bogus host scsi id, set it to the dummy value */
823 if (sh[j]->this_id == 0) sh[j]->this_id = -1;
825 /* If BIOS is disabled, force enable interrupts */
826 if (sh[j]->base == 0) outb(CMD_ENA_INTR, sh[j]->io_port + REG_SYS_MASK);
828 /* Register the I/O space that we use */
829 request_region(sh[j]->io_port, sh[j]->n_io_port, driver_name);
831 memset(HD(j), 0, sizeof(struct hostdata));
832 HD(j)->heads = mapping_table[config_2.mapping_mode].heads;
833 HD(j)->sectors = mapping_table[config_2.mapping_mode].sectors;
834 HD(j)->subversion = subversion;
835 HD(j)->board_number = j;
837 if (have_old_firmware) sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
839 if (HD(j)->subversion == ESA) {
840 sh[j]->unchecked_isa_dma = FALSE;
841 sh[j]->dma_channel = NO_DMA;
842 sprintf(BN(j), "U34F%d", j);
843 bus_type = "VESA";
845 else {
846 unsigned long flags;
847 sh[j]->wish_block = TRUE;
848 sh[j]->unchecked_isa_dma = TRUE;
850 flags=claim_dma_lock();
851 disable_dma(dma_channel);
852 clear_dma_ff(dma_channel);
853 set_dma_mode(dma_channel, DMA_MODE_CASCADE);
854 enable_dma(dma_channel);
855 release_dma_lock(flags);
857 sh[j]->dma_channel = dma_channel;
858 sprintf(BN(j), "U14F%d", j);
859 bus_type = "ISA";
862 sh[j]->max_channel = MAX_CHANNEL - 1;
863 sh[j]->max_id = MAX_TARGET;
864 sh[j]->max_lun = MAX_LUN;
866 if (HD(j)->subversion == ISA && !board_inquiry(j)) {
867 HD(j)->board_id[40] = 0;
869 if (strcmp(&HD(j)->board_id[32], "06000600")) {
870 printk("%s: %s.\n", BN(j), &HD(j)->board_id[8]);
871 printk("%s: firmware %s is outdated, FW PROM should be 28004-006.\n",
872 BN(j), &HD(j)->board_id[32]);
873 sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
874 sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
878 if (dma_channel == NO_DMA) sprintf(dma_name, "%s", "BMST");
879 else sprintf(dma_name, "DMA %u", dma_channel);
881 for (i = 0; i < sh[j]->can_queue; i++)
882 if (! ((&HD(j)->cp[i])->sglist = kmalloc(
883 sh[j]->sg_tablesize * sizeof(struct sg_list),
884 (sh[j]->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC))) {
885 printk("%s: kmalloc SGlist failed, mbox %d, detaching.\n", BN(j), i);
886 u14_34f_release(sh[j]);
887 return FALSE;
890 if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
891 max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
893 if (max_queue_depth < MAX_CMD_PER_LUN) max_queue_depth = MAX_CMD_PER_LUN;
895 if (j == 0) {
896 printk("UltraStor 14F/34F: Copyright (C) 1994-1998 Dario Ballabio.\n");
897 printk("%s config options -> of:%c, lc:%c, mq:%d, eh:%c, et:%c.\n",
898 driver_name, YESNO(have_old_firmware), YESNO(linked_comm),
899 max_queue_depth, YESNO(use_new_eh_code), YESNO(ext_tran));
902 printk("%s: %s 0x%03lx, BIOS 0x%05x, IRQ %u, %s, SG %d, MB %d.\n",
903 BN(j), bus_type, (unsigned long)sh[j]->io_port, (int)sh[j]->base,
904 sh[j]->irq, dma_name, sh[j]->sg_tablesize, sh[j]->can_queue);
906 if (sh[j]->max_id > 8 || sh[j]->max_lun > 8)
907 printk("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
908 BN(j), sh[j]->max_id, sh[j]->max_lun);
910 for (i = 0; i <= sh[j]->max_channel; i++)
911 printk("%s: SCSI channel %u enabled, host target ID %d.\n",
912 BN(j), i, sh[j]->this_id);
914 return TRUE;
917 void __init u14_34f_setup(char *str, int *ints)
919 int i, argc = ints[0];
920 char *cur = str, *pc;
922 if (argc > 0) {
924 if (argc > MAX_INT_PARAM) argc = MAX_INT_PARAM;
926 for (i = 0; i < argc; i++) io_port[i] = ints[i + 1];
928 io_port[i] = 0;
929 setup_done = TRUE;
932 while (cur && (pc = strchr(cur, ':'))) {
933 int val = 0, c = *++pc;
935 if (c == 'n' || c == 'N') val = FALSE;
936 else if (c == 'y' || c == 'Y') val = TRUE;
937 else val = (int) simple_strtoul(pc, NULL, 0);
939 if (!strncmp(cur, "lc:", 3)) linked_comm = val;
940 else if (!strncmp(cur, "of:", 3)) have_old_firmware = val;
941 else if (!strncmp(cur, "mq:", 3)) max_queue_depth = val;
942 else if (!strncmp(cur, "ls:", 3)) link_statistics = val;
943 else if (!strncmp(cur, "eh:", 3)) use_new_eh_code = val;
944 else if (!strncmp(cur, "et:", 3)) ext_tran = val;
946 if ((cur = strchr(cur, ','))) ++cur;
949 return;
952 int __init u14_34f_detect(Scsi_Host_Template *tpnt)
954 unsigned int j = 0, k;
955 IRQ_FLAGS
957 IRQ_LOCK_SAVE
958 tpnt->proc_dir = &proc_scsi_u14_34f;
960 #if defined(MODULE)
961 /* io_port could have been modified when loading as a module */
962 if(io_port[0] != SKIP) {
963 setup_done = TRUE;
964 io_port[MAX_INT_PARAM] = 0;
966 #endif
968 for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
970 for (k = 0; io_port[k]; k++) {
972 if (io_port[k] == SKIP) continue;
974 if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt)) j++;
977 num_boards = j;
978 IRQ_UNLOCK_RESTORE
979 return j;
982 static inline void build_sg_list(struct mscp *cpp, Scsi_Cmnd *SCpnt) {
983 unsigned int k, data_len = 0;
984 struct scatterlist *sgpnt;
986 sgpnt = (struct scatterlist *) SCpnt->request_buffer;
988 for (k = 0; k < SCpnt->use_sg; k++) {
989 cpp->sglist[k].address = V2DEV(sgpnt[k].address);
990 cpp->sglist[k].num_bytes = H2DEV(sgpnt[k].length);
991 data_len += sgpnt[k].length;
994 cpp->use_sg = SCpnt->use_sg;
995 cpp->data_address = V2DEV(cpp->sglist);
996 cpp->data_len = H2DEV(data_len);
999 static inline int do_qcomm(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
1000 unsigned int i, j, k;
1001 struct mscp *cpp;
1003 static const unsigned char data_out_cmds[] = {
1004 0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
1005 0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
1006 0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b
1009 static const unsigned char data_none_cmds[] = {
1010 0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
1011 0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
1012 0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5
1015 /* j is the board number */
1016 j = ((struct hostdata *) SCpnt->host->hostdata)->board_number;
1018 if (SCpnt->host_scribble)
1019 panic("%s: qcomm, pid %ld, SCpnt %p already active.\n",
1020 BN(j), SCpnt->pid, SCpnt);
1022 /* i is the mailbox number, look for the first free mailbox
1023 starting from last_cp_used */
1024 i = HD(j)->last_cp_used + 1;
1026 for (k = 0; k < sh[j]->can_queue; k++, i++) {
1028 if (i >= sh[j]->can_queue) i = 0;
1030 if (HD(j)->cp_stat[i] == FREE) {
1031 HD(j)->last_cp_used = i;
1032 break;
1036 if (k == sh[j]->can_queue) {
1037 printk("%s: qcomm, no free mailbox.\n", BN(j));
1038 return 1;
1041 /* Set pointer to control packet structure */
1042 cpp = &HD(j)->cp[i];
1044 memset(cpp, 0, sizeof(struct mscp) - sizeof(struct sg_list *));
1045 SCpnt->scsi_done = done;
1046 cpp->index = i;
1047 SCpnt->host_scribble = (unsigned char *) &cpp->index;
1049 if (do_trace) printk("%s: qcomm, mbox %d, target %d.%d:%d, pid %ld.\n",
1050 BN(j), i, SCpnt->channel, SCpnt->target,
1051 SCpnt->lun, SCpnt->pid);
1053 cpp->xdir = DTD_IN;
1055 for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
1056 if (SCpnt->cmnd[0] == data_out_cmds[k]) {
1057 cpp->xdir = DTD_OUT;
1058 break;
1061 if (cpp->xdir == DTD_IN)
1062 for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
1063 if (SCpnt->cmnd[0] == data_none_cmds[k]) {
1064 cpp->xdir = DTD_NONE;
1065 break;
1068 cpp->opcode = OP_SCSI;
1069 cpp->channel = SCpnt->channel;
1070 cpp->target = SCpnt->target;
1071 cpp->lun = SCpnt->lun;
1072 cpp->SCpnt = SCpnt;
1073 cpp->sense_addr = V2DEV(SCpnt->sense_buffer);
1074 cpp->sense_len = sizeof SCpnt->sense_buffer;
1076 if (SCpnt->use_sg) {
1077 cpp->sg = TRUE;
1078 build_sg_list(cpp, SCpnt);
1080 else {
1081 cpp->data_address = V2DEV(SCpnt->request_buffer);
1082 cpp->data_len = H2DEV(SCpnt->request_bufflen);
1085 cpp->scsi_cdbs_len = SCpnt->cmd_len;
1086 memcpy(cpp->scsi_cdbs, SCpnt->cmnd, cpp->scsi_cdbs_len);
1088 if (linked_comm && SCpnt->device->queue_depth > 2
1089 && TLDEV(SCpnt->device->type)) {
1090 HD(j)->cp_stat[i] = READY;
1091 flush_dev(SCpnt->device, SCpnt->request.sector, j, FALSE);
1092 return 0;
1095 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1096 SCpnt->host_scribble = NULL;
1097 printk("%s: qcomm, target %d.%d:%d, pid %ld, adapter busy.\n",
1098 BN(j), SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid);
1099 return 1;
1102 /* Store pointer in OGM address bytes */
1103 outl(V2DEV(cpp), sh[j]->io_port + REG_OGM);
1105 /* Issue OGM interrupt */
1106 outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
1108 HD(j)->cp_stat[i] = IN_USE;
1109 return 0;
1112 int u14_34f_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *)) {
1113 int rtn;
1114 IRQ_FLAGS
1116 IRQ_LOCK_SAVE
1117 rtn = do_qcomm(SCpnt, done);
1118 IRQ_UNLOCK_RESTORE
1119 return rtn;
1122 static inline int do_old_abort(Scsi_Cmnd *SCarg) {
1123 unsigned int i, j;
1125 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1127 if (SCarg->host_scribble == NULL ||
1128 (SCarg->serial_number_at_timeout &&
1129 (SCarg->serial_number != SCarg->serial_number_at_timeout))) {
1130 printk("%s: abort, target %d.%d:%d, pid %ld inactive.\n",
1131 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1132 return SCSI_ABORT_NOT_RUNNING;
1135 i = *(unsigned int *)SCarg->host_scribble;
1136 printk("%s: abort, mbox %d, target %d.%d:%d, pid %ld.\n",
1137 BN(j), i, SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1139 if (i >= sh[j]->can_queue)
1140 panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
1142 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1143 printk("%s: abort, timeout error.\n", BN(j));
1144 return SCSI_ABORT_ERROR;
1147 if (HD(j)->cp_stat[i] == FREE) {
1148 printk("%s: abort, mbox %d is free.\n", BN(j), i);
1149 return SCSI_ABORT_NOT_RUNNING;
1152 if (HD(j)->cp_stat[i] == IN_USE) {
1153 printk("%s: abort, mbox %d is in use.\n", BN(j), i);
1155 if (SCarg != HD(j)->cp[i].SCpnt)
1156 panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1157 BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
1159 if (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED)
1160 printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
1162 return SCSI_ABORT_SNOOZE;
1165 if (HD(j)->cp_stat[i] == IN_RESET) {
1166 printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
1167 return SCSI_ABORT_ERROR;
1170 if (HD(j)->cp_stat[i] == LOCKED) {
1171 printk("%s: abort, mbox %d is locked.\n", BN(j), i);
1172 return SCSI_ABORT_NOT_RUNNING;
1175 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1176 SCarg->result = DID_ABORT << 16;
1177 SCarg->host_scribble = NULL;
1178 HD(j)->cp_stat[i] = FREE;
1179 printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1180 BN(j), i, SCarg->pid);
1181 SCarg->scsi_done(SCarg);
1182 return SCSI_ABORT_SUCCESS;
1185 panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
1188 int u14_34f_old_abort(Scsi_Cmnd *SCarg) {
1189 int rtn;
1190 IRQ_FLAGS
1192 IRQ_LOCK_SAVE
1193 rtn = do_old_abort(SCarg);
1194 IRQ_UNLOCK_RESTORE
1195 return rtn;
1198 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,101)
1200 static inline int do_abort(Scsi_Cmnd *SCarg) {
1201 unsigned int i, j;
1203 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1205 if (SCarg->host_scribble == NULL) {
1206 printk("%s: abort, target %d.%d:%d, pid %ld inactive.\n",
1207 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1208 return SUCCESS;
1211 i = *(unsigned int *)SCarg->host_scribble;
1212 printk("%s: abort, mbox %d, target %d.%d:%d, pid %ld.\n",
1213 BN(j), i, SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1215 if (i >= sh[j]->can_queue)
1216 panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
1218 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1219 printk("%s: abort, timeout error.\n", BN(j));
1220 return FAILED;
1223 if (HD(j)->cp_stat[i] == FREE) {
1224 printk("%s: abort, mbox %d is free.\n", BN(j), i);
1225 return SUCCESS;
1228 if (HD(j)->cp_stat[i] == IN_USE) {
1229 printk("%s: abort, mbox %d is in use.\n", BN(j), i);
1231 if (SCarg != HD(j)->cp[i].SCpnt)
1232 panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1233 BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
1235 if (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED)
1236 printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
1238 if (SCarg->eh_state == SCSI_STATE_TIMEOUT) {
1239 SCarg->host_scribble = NULL;
1240 HD(j)->cp_stat[i] = FREE;
1241 printk("%s, abort, mbox %d, eh_state timeout, pid %ld.\n",
1242 BN(j), i, SCarg->pid);
1243 return SUCCESS;
1246 return FAILED;
1249 if (HD(j)->cp_stat[i] == IN_RESET) {
1250 printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
1251 return FAILED;
1254 if (HD(j)->cp_stat[i] == LOCKED) {
1255 printk("%s: abort, mbox %d is locked.\n", BN(j), i);
1256 return SUCCESS;
1259 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1260 SCarg->result = DID_ABORT << 16;
1261 SCarg->host_scribble = NULL;
1262 HD(j)->cp_stat[i] = FREE;
1263 printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1264 BN(j), i, SCarg->pid);
1265 SCarg->scsi_done(SCarg);
1266 return SUCCESS;
1269 panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
1272 int u14_34f_abort(Scsi_Cmnd *SCarg) {
1274 return do_abort(SCarg);
1277 #endif /* new_eh_code */
1279 static inline int do_old_reset(Scsi_Cmnd *SCarg) {
1280 unsigned int i, j, time, k, c, limit = 0;
1281 int arg_done = FALSE;
1282 Scsi_Cmnd *SCpnt;
1284 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1285 printk("%s: reset, enter, target %d.%d:%d, pid %ld.\n",
1286 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1288 if (SCarg->host_scribble == NULL)
1289 printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
1291 if (SCarg->serial_number_at_timeout &&
1292 (SCarg->serial_number != SCarg->serial_number_at_timeout)) {
1293 printk("%s: reset, pid %ld, reset not running.\n", BN(j), SCarg->pid);
1294 return SCSI_RESET_NOT_RUNNING;
1297 if (HD(j)->in_reset) {
1298 printk("%s: reset, exit, already in reset.\n", BN(j));
1299 return SCSI_RESET_ERROR;
1302 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1303 printk("%s: reset, exit, timeout error.\n", BN(j));
1304 return SCSI_RESET_ERROR;
1307 HD(j)->retries = 0;
1309 for (c = 0; c <= sh[j]->max_channel; c++)
1310 for (k = 0; k < sh[j]->max_id; k++) {
1311 HD(j)->target_redo[k][c] = TRUE;
1312 HD(j)->target_to[k][c] = 0;
1315 for (i = 0; i < sh[j]->can_queue; i++) {
1317 if (HD(j)->cp_stat[i] == FREE) continue;
1319 if (HD(j)->cp_stat[i] == LOCKED) {
1320 HD(j)->cp_stat[i] = FREE;
1321 printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
1322 continue;
1325 if (!(SCpnt = HD(j)->cp[i].SCpnt))
1326 panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
1328 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1329 HD(j)->cp_stat[i] = ABORTING;
1330 printk("%s: reset, mbox %d aborting, pid %ld.\n",
1331 BN(j), i, SCpnt->pid);
1334 else {
1335 HD(j)->cp_stat[i] = IN_RESET;
1336 printk("%s: reset, mbox %d in reset, pid %ld.\n",
1337 BN(j), i, SCpnt->pid);
1340 if (SCpnt->host_scribble == NULL)
1341 panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
1343 if (*(unsigned int *)SCpnt->host_scribble != i)
1344 panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
1346 if (SCpnt->scsi_done == NULL)
1347 panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
1349 if (SCpnt == SCarg) arg_done = TRUE;
1352 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1353 printk("%s: reset, cannot reset, timeout error.\n", BN(j));
1354 return SCSI_RESET_ERROR;
1357 outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
1358 printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
1360 #if defined(DEBUG_RESET)
1361 do_trace = TRUE;
1362 #endif
1364 HD(j)->in_reset = TRUE;
1365 SPIN_UNLOCK
1366 IRQ_UNLOCK
1367 time = jiffies;
1368 while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
1369 IRQ_LOCK
1370 SPIN_LOCK
1371 printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
1373 for (i = 0; i < sh[j]->can_queue; i++) {
1375 if (HD(j)->cp_stat[i] == IN_RESET) {
1376 SCpnt = HD(j)->cp[i].SCpnt;
1377 SCpnt->result = DID_RESET << 16;
1378 SCpnt->host_scribble = NULL;
1380 /* This mailbox is still waiting for its interrupt */
1381 HD(j)->cp_stat[i] = LOCKED;
1383 printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
1384 BN(j), i, SCpnt->pid);
1387 else if (HD(j)->cp_stat[i] == ABORTING) {
1388 SCpnt = HD(j)->cp[i].SCpnt;
1389 SCpnt->result = DID_RESET << 16;
1390 SCpnt->host_scribble = NULL;
1392 /* This mailbox was never queued to the adapter */
1393 HD(j)->cp_stat[i] = FREE;
1395 printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
1396 BN(j), i, SCpnt->pid);
1399 else
1401 /* Any other mailbox has already been set free by interrupt */
1402 continue;
1404 SCpnt->scsi_done(SCpnt);
1405 IRQ_LOCK
1408 HD(j)->in_reset = FALSE;
1409 do_trace = FALSE;
1411 if (arg_done) {
1412 printk("%s: reset, exit, success.\n", BN(j));
1413 return SCSI_RESET_SUCCESS;
1415 else {
1416 printk("%s: reset, exit, wakeup.\n", BN(j));
1417 return SCSI_RESET_PUNT;
1421 int u14_34f_old_reset(Scsi_Cmnd *SCarg, unsigned int reset_flags) {
1422 int rtn;
1423 IRQ_FLAGS
1425 IRQ_LOCK_SAVE
1426 rtn = do_old_reset(SCarg);
1427 IRQ_UNLOCK_RESTORE
1428 return rtn;
1431 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,1,101)
1433 static inline int do_reset(Scsi_Cmnd *SCarg) {
1434 unsigned int i, j, time, k, c, limit = 0;
1435 int arg_done = FALSE;
1436 Scsi_Cmnd *SCpnt;
1438 j = ((struct hostdata *) SCarg->host->hostdata)->board_number;
1439 printk("%s: reset, enter, target %d.%d:%d, pid %ld.\n",
1440 BN(j), SCarg->channel, SCarg->target, SCarg->lun, SCarg->pid);
1442 if (SCarg->host_scribble == NULL)
1443 printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->pid);
1445 if (HD(j)->in_reset) {
1446 printk("%s: reset, exit, already in reset.\n", BN(j));
1447 return FAILED;
1450 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1451 printk("%s: reset, exit, timeout error.\n", BN(j));
1452 return FAILED;
1455 HD(j)->retries = 0;
1457 for (c = 0; c <= sh[j]->max_channel; c++)
1458 for (k = 0; k < sh[j]->max_id; k++) {
1459 HD(j)->target_redo[k][c] = TRUE;
1460 HD(j)->target_to[k][c] = 0;
1463 for (i = 0; i < sh[j]->can_queue; i++) {
1465 if (HD(j)->cp_stat[i] == FREE) continue;
1467 if (HD(j)->cp_stat[i] == LOCKED) {
1468 HD(j)->cp_stat[i] = FREE;
1469 printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
1470 continue;
1473 if (!(SCpnt = HD(j)->cp[i].SCpnt))
1474 panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
1476 if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1477 HD(j)->cp_stat[i] = ABORTING;
1478 printk("%s: reset, mbox %d aborting, pid %ld.\n",
1479 BN(j), i, SCpnt->pid);
1482 else {
1483 HD(j)->cp_stat[i] = IN_RESET;
1484 printk("%s: reset, mbox %d in reset, pid %ld.\n",
1485 BN(j), i, SCpnt->pid);
1488 if (SCpnt->host_scribble == NULL)
1489 panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
1491 if (*(unsigned int *)SCpnt->host_scribble != i)
1492 panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
1494 if (SCpnt->scsi_done == NULL)
1495 panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
1497 if (SCpnt == SCarg) arg_done = TRUE;
1500 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1501 printk("%s: reset, cannot reset, timeout error.\n", BN(j));
1502 return FAILED;
1505 outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
1506 printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
1508 #if defined(DEBUG_RESET)
1509 do_trace = TRUE;
1510 #endif
1512 HD(j)->in_reset = TRUE;
1513 SPIN_UNLOCK
1514 IRQ_UNLOCK
1515 time = jiffies;
1516 while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
1517 IRQ_LOCK
1518 SPIN_LOCK
1519 printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
1521 for (i = 0; i < sh[j]->can_queue; i++) {
1523 if (HD(j)->cp_stat[i] == IN_RESET) {
1524 SCpnt = HD(j)->cp[i].SCpnt;
1525 SCpnt->result = DID_RESET << 16;
1526 SCpnt->host_scribble = NULL;
1528 /* This mailbox is still waiting for its interrupt */
1529 HD(j)->cp_stat[i] = LOCKED;
1531 printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
1532 BN(j), i, SCpnt->pid);
1535 else if (HD(j)->cp_stat[i] == ABORTING) {
1536 SCpnt = HD(j)->cp[i].SCpnt;
1537 SCpnt->result = DID_RESET << 16;
1538 SCpnt->host_scribble = NULL;
1540 /* This mailbox was never queued to the adapter */
1541 HD(j)->cp_stat[i] = FREE;
1543 printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
1544 BN(j), i, SCpnt->pid);
1547 else
1549 /* Any other mailbox has already been set free by interrupt */
1550 continue;
1552 SCpnt->scsi_done(SCpnt);
1553 IRQ_LOCK
1556 HD(j)->in_reset = FALSE;
1557 do_trace = FALSE;
1559 if (arg_done) printk("%s: reset, exit, pid %ld done.\n", BN(j), SCarg->pid);
1560 else printk("%s: reset, exit.\n", BN(j));
1562 return SUCCESS;
1565 int u14_34f_reset(Scsi_Cmnd *SCarg) {
1567 return do_reset(SCarg);
1570 #endif /* new_eh_code */
1572 int u14_34f_biosparam(Disk *disk, kdev_t dev, int *dkinfo) {
1573 unsigned int j = 0;
1574 int size = disk->capacity;
1576 dkinfo[0] = HD(j)->heads;
1577 dkinfo[1] = HD(j)->sectors;
1578 dkinfo[2] = size / (HD(j)->heads * HD(j)->sectors);
1580 if (ext_tran && (scsicam_bios_param(disk, dev, dkinfo) < 0)) {
1581 dkinfo[0] = 255;
1582 dkinfo[1] = 63;
1583 dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
1586 #if defined (DEBUG_GEOMETRY)
1587 printk ("%s: biosparam, head=%d, sec=%d, cyl=%d.\n", driver_name,
1588 dkinfo[0], dkinfo[1], dkinfo[2]);
1589 #endif
1591 return FALSE;
1594 static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
1595 unsigned int rev) {
1596 unsigned int i, j, k, y;
1597 unsigned long x;
1599 for (i = 0; i < n - 1; i++) {
1600 k = i;
1602 for (j = k + 1; j < n; j++)
1603 if (rev) {
1604 if (sk[j] > sk[k]) k = j;
1606 else {
1607 if (sk[j] < sk[k]) k = j;
1610 if (k != i) {
1611 x = sk[k]; sk[k] = sk[i]; sk[i] = x;
1612 y = da[k]; da[k] = da[i]; da[i] = y;
1616 return;
1619 static inline int reorder(unsigned int j, unsigned long cursec,
1620 unsigned int ihdlr, unsigned int il[], unsigned int n_ready) {
1621 Scsi_Cmnd *SCpnt;
1622 struct mscp *cpp;
1623 unsigned int k, n;
1624 unsigned int rev = FALSE, s = TRUE, r = TRUE;
1625 unsigned int input_only = TRUE, overlap = FALSE;
1626 unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
1627 unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
1628 unsigned long ioseek = 0;
1630 static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
1631 static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
1632 static unsigned int readysorted = 0, revcount = 0;
1633 static unsigned long seeksorted = 0, seeknosort = 0;
1635 if (link_statistics && !(++flushcount % link_statistics))
1636 printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"\
1637 " av %ldK as %ldK.\n", flushcount, batchcount, inputcount,
1638 ovlcount, readycount, readysorted, sortcount, revcount,
1639 seeknosort / (readycount + 1),
1640 seeksorted / (readycount + 1));
1642 if (n_ready <= 1) return FALSE;
1644 for (n = 0; n < n_ready; n++) {
1645 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1647 if (!(cpp->xdir == DTD_IN)) input_only = FALSE;
1649 if (SCpnt->request.sector < minsec) minsec = SCpnt->request.sector;
1650 if (SCpnt->request.sector > maxsec) maxsec = SCpnt->request.sector;
1652 sl[n] = SCpnt->request.sector;
1653 ioseek += SCpnt->request.nr_sectors;
1655 if (!n) continue;
1657 if (sl[n] < sl[n - 1]) s = FALSE;
1658 if (sl[n] > sl[n - 1]) r = FALSE;
1660 if (link_statistics) {
1661 if (sl[n] > sl[n - 1])
1662 seek += sl[n] - sl[n - 1];
1663 else
1664 seek += sl[n - 1] - sl[n];
1669 if (link_statistics) {
1670 if (cursec > sl[0]) seek += cursec - sl[0]; else seek += sl[0] - cursec;
1673 if (cursec > ((maxsec + minsec) / 2)) rev = TRUE;
1675 if (ioseek > ((maxsec - minsec) / 2)) rev = FALSE;
1677 if (!((rev && r) || (!rev && s))) sort(sl, il, n_ready, rev);
1679 if (!input_only) for (n = 0; n < n_ready; n++) {
1680 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1681 ll[n] = SCpnt->request.nr_sectors; pl[n] = SCpnt->pid;
1683 if (!n) continue;
1685 if ((sl[n] == sl[n - 1]) || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
1686 || (rev && ((sl[n] + ll[n]) > sl[n - 1]))) overlap = TRUE;
1689 if (overlap) sort(pl, il, n_ready, FALSE);
1691 if (link_statistics) {
1692 if (cursec > sl[0]) iseek = cursec - sl[0]; else iseek = sl[0] - cursec;
1693 batchcount++; readycount += n_ready, seeknosort += seek / 1024;
1694 if (input_only) inputcount++;
1695 if (overlap) { ovlcount++; seeksorted += iseek / 1024; }
1696 else seeksorted += (iseek + maxsec - minsec) / 1024;
1697 if (rev && !r) { revcount++; readysorted += n_ready; }
1698 if (!rev && !s) { sortcount++; readysorted += n_ready; }
1701 #if defined(DEBUG_LINKED_COMMANDS)
1702 if (link_statistics && (overlap || !(flushcount % link_statistics)))
1703 for (n = 0; n < n_ready; n++) {
1704 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1705 printk("%s %d.%d:%d pid %ld mb %d fc %d nr %d sec %ld ns %ld"\
1706 " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
1707 (ihdlr ? "ihdlr" : "qcomm"), SCpnt->channel, SCpnt->target,
1708 SCpnt->lun, SCpnt->pid, k, flushcount, n_ready,
1709 SCpnt->request.sector, SCpnt->request.nr_sectors, cursec,
1710 YESNO(s), YESNO(r), YESNO(rev), YESNO(input_only),
1711 YESNO(overlap), cpp->xdir);
1713 #endif
1714 return overlap;
1717 static void flush_dev(Scsi_Device *dev, unsigned long cursec, unsigned int j,
1718 unsigned int ihdlr) {
1719 Scsi_Cmnd *SCpnt;
1720 struct mscp *cpp;
1721 unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
1723 for (k = 0; k < sh[j]->can_queue; k++) {
1725 if (HD(j)->cp_stat[k] != READY && HD(j)->cp_stat[k] != IN_USE) continue;
1727 cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1729 if (SCpnt->device != dev) continue;
1731 if (HD(j)->cp_stat[k] == IN_USE) return;
1733 il[n_ready++] = k;
1736 if (reorder(j, cursec, ihdlr, il, n_ready)) n_ready = 1;
1738 for (n = 0; n < n_ready; n++) {
1739 k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1741 if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1742 printk("%s: %s, target %d.%d:%d, pid %ld, mbox %d, adapter"\
1743 " busy, will abort.\n", BN(j), (ihdlr ? "ihdlr" : "qcomm"),
1744 SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid, k);
1745 HD(j)->cp_stat[k] = ABORTING;
1746 continue;
1749 outl(V2DEV(cpp), sh[j]->io_port + REG_OGM);
1750 outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
1751 HD(j)->cp_stat[k] = IN_USE;
1756 static inline void ihdlr(int irq, unsigned int j) {
1757 Scsi_Cmnd *SCpnt;
1758 unsigned int i, k, c, status, tstatus, reg, ret;
1759 struct mscp *spp, *cpp;
1761 if (sh[j]->irq != irq)
1762 panic("%s: ihdlr, irq %d, sh[j]->irq %d.\n", BN(j), irq, sh[j]->irq);
1764 /* Check if this board need to be serviced */
1765 if (!((reg = inb(sh[j]->io_port + REG_SYS_INTR)) & IRQ_ASSERTED)) return;
1767 HD(j)->iocount++;
1769 if (do_trace) printk("%s: ihdlr, enter, irq %d, count %d.\n", BN(j), irq,
1770 HD(j)->iocount);
1772 /* Check if this board is still busy */
1773 if (wait_on_busy(sh[j]->io_port, 20 * MAXLOOP)) {
1774 outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
1775 printk("%s: ihdlr, busy timeout error, irq %d, reg 0x%x, count %d.\n",
1776 BN(j), irq, reg, HD(j)->iocount);
1777 return;
1780 spp = (struct mscp *)DEV2V(ret = inl(sh[j]->io_port + REG_ICM));
1781 cpp = spp;
1783 /* Clear interrupt pending flag */
1784 outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
1786 #if defined(DEBUG_GENERATE_ABORTS)
1787 if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 500) < 3)) return;
1788 #endif
1790 /* Find the mailbox to be serviced on this board */
1791 i = cpp - HD(j)->cp;
1793 if (cpp < HD(j)->cp || cpp >= HD(j)->cp + sh[j]->can_queue
1794 || i >= sh[j]->can_queue)
1795 panic("%s: ihdlr, invalid mscp bus address %p, cp0 %p.\n", BN(j),
1796 (void *)ret, HD(j)->cp);
1798 if (HD(j)->cp_stat[i] == IGNORE) {
1799 HD(j)->cp_stat[i] = FREE;
1800 return;
1802 else if (HD(j)->cp_stat[i] == LOCKED) {
1803 HD(j)->cp_stat[i] = FREE;
1804 printk("%s: ihdlr, mbox %d unlocked, count %d.\n", BN(j), i,
1805 HD(j)->iocount);
1806 return;
1808 else if (HD(j)->cp_stat[i] == FREE) {
1809 printk("%s: ihdlr, mbox %d is free, count %d.\n", BN(j), i,
1810 HD(j)->iocount);
1811 return;
1813 else if (HD(j)->cp_stat[i] == IN_RESET)
1814 printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
1815 else if (HD(j)->cp_stat[i] != IN_USE)
1816 panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
1817 BN(j), i, HD(j)->cp_stat[i]);
1819 HD(j)->cp_stat[i] = FREE;
1820 SCpnt = cpp->SCpnt;
1822 if (SCpnt == NULL) panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
1824 if (SCpnt->host_scribble == NULL)
1825 panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", BN(j), i,
1826 SCpnt->pid, SCpnt);
1828 if (*(unsigned int *)SCpnt->host_scribble != i)
1829 panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
1830 BN(j), i, SCpnt->pid, *(unsigned int *)SCpnt->host_scribble);
1832 if (linked_comm && SCpnt->device->queue_depth > 2
1833 && TLDEV(SCpnt->device->type))
1834 flush_dev(SCpnt->device, SCpnt->request.sector, j, TRUE);
1836 tstatus = status_byte(spp->target_status);
1838 #if defined(DEBUG_GENERATE_ERRORS)
1839 if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 200) < 2))
1840 spp->adapter_status = 0x01;
1841 #endif
1843 switch (spp->adapter_status) {
1844 case ASOK: /* status OK */
1846 /* Forces a reset if a disk drive keeps returning BUSY */
1847 if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
1848 status = DID_ERROR << 16;
1850 /* If there was a bus reset, redo operation on each target */
1851 else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
1852 && HD(j)->target_redo[SCpnt->target][SCpnt->channel])
1853 status = DID_BUS_BUSY << 16;
1855 /* Works around a flaw in scsi.c */
1856 else if (tstatus == CHECK_CONDITION
1857 && SCpnt->device->type == TYPE_DISK
1858 && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
1859 status = DID_BUS_BUSY << 16;
1861 else
1862 status = DID_OK << 16;
1864 if (tstatus == GOOD)
1865 HD(j)->target_redo[SCpnt->target][SCpnt->channel] = FALSE;
1867 if (spp->target_status && SCpnt->device->type == TYPE_DISK)
1868 printk("%s: ihdlr, target %d.%d:%d, pid %ld, "\
1869 "target_status 0x%x, sense key 0x%x.\n", BN(j),
1870 SCpnt->channel, SCpnt->target, SCpnt->lun,
1871 SCpnt->pid, spp->target_status,
1872 SCpnt->sense_buffer[2]);
1874 HD(j)->target_to[SCpnt->target][SCpnt->channel] = 0;
1876 if (HD(j)->last_retried_pid == SCpnt->pid) HD(j)->retries = 0;
1878 break;
1879 case ASST: /* Selection Time Out */
1881 if (HD(j)->target_to[SCpnt->target][SCpnt->channel] > 1)
1882 status = DID_ERROR << 16;
1883 else {
1884 status = DID_TIME_OUT << 16;
1885 HD(j)->target_to[SCpnt->target][SCpnt->channel]++;
1888 break;
1890 /* Perform a limited number of internal retries */
1891 case 0x93: /* Unexpected bus free */
1892 case 0x94: /* Target bus phase sequence failure */
1893 case 0x96: /* Illegal SCSI command */
1894 case 0xa3: /* SCSI bus reset error */
1896 for (c = 0; c <= sh[j]->max_channel; c++)
1897 for (k = 0; k < sh[j]->max_id; k++)
1898 HD(j)->target_redo[k][c] = TRUE;
1901 case 0x92: /* Data over/under-run */
1903 if (SCpnt->device->type != TYPE_TAPE
1904 && HD(j)->retries < MAX_INTERNAL_RETRIES) {
1906 #if defined(DID_SOFT_ERROR)
1907 status = DID_SOFT_ERROR << 16;
1908 #else
1909 status = DID_BUS_BUSY << 16;
1910 #endif
1912 HD(j)->retries++;
1913 HD(j)->last_retried_pid = SCpnt->pid;
1915 else
1916 status = DID_ERROR << 16;
1918 break;
1919 case 0x01: /* Invalid command */
1920 case 0x02: /* Invalid parameters */
1921 case 0x03: /* Invalid data list */
1922 case 0x84: /* SCSI bus abort error */
1923 case 0x9b: /* Auto request sense error */
1924 case 0x9f: /* Unexpected command complete message error */
1925 case 0xff: /* Invalid parameter in the S/G list */
1926 default:
1927 status = DID_ERROR << 16;
1928 break;
1931 SCpnt->result = status | spp->target_status;
1933 #if defined(DEBUG_INTERRUPT)
1934 if (SCpnt->result || do_trace)
1935 #else
1936 if ((spp->adapter_status != ASOK && HD(j)->iocount > 1000) ||
1937 (spp->adapter_status != ASOK &&
1938 spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
1939 do_trace || msg_byte(spp->target_status))
1940 #endif
1941 printk("%s: ihdlr, mbox %2d, err 0x%x:%x,"\
1942 " target %d.%d:%d, pid %ld, reg 0x%x, count %d.\n",
1943 BN(j), i, spp->adapter_status, spp->target_status,
1944 SCpnt->channel, SCpnt->target, SCpnt->lun, SCpnt->pid,
1945 reg, HD(j)->iocount);
1947 /* Set the command state to inactive */
1948 SCpnt->host_scribble = NULL;
1950 SCpnt->scsi_done(SCpnt);
1952 if (do_trace) printk("%s: ihdlr, exit, irq %d, count %d.\n", BN(j), irq,
1953 HD(j)->iocount);
1955 return;
1958 static void do_interrupt_handler(int irq, void *shap, struct pt_regs *regs) {
1959 unsigned int j;
1960 IRQ_FLAGS
1961 SPIN_FLAGS
1963 /* Check if the interrupt must be processed by this handler */
1964 if ((j = (unsigned int)((char *)shap - sha)) >= num_boards) return;
1966 SPIN_LOCK_SAVE
1967 IRQ_LOCK_SAVE
1968 ihdlr(irq, j);
1969 IRQ_UNLOCK_RESTORE
1970 SPIN_UNLOCK_RESTORE
1973 int u14_34f_release(struct Scsi_Host *shpnt) {
1974 unsigned int i, j;
1975 IRQ_FLAGS
1977 IRQ_LOCK_SAVE
1979 for (j = 0; sh[j] != NULL && sh[j] != shpnt; j++);
1981 if (sh[j] == NULL) panic("%s: release, invalid Scsi_Host pointer.\n",
1982 driver_name);
1984 for (i = 0; i < sh[j]->can_queue; i++)
1985 if ((&HD(j)->cp[i])->sglist) kfree((&HD(j)->cp[i])->sglist);
1987 free_irq(sh[j]->irq, &sha[j]);
1989 if (sh[j]->dma_channel != NO_DMA) free_dma(sh[j]->dma_channel);
1991 release_region(sh[j]->io_port, sh[j]->n_io_port);
1992 scsi_unregister(sh[j]);
1993 IRQ_UNLOCK_RESTORE
1994 return FALSE;
1997 #if defined(MODULE)
1998 Scsi_Host_Template driver_template = ULTRASTOR_14_34F;
2000 #include "scsi_module.c"
2001 #endif