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