• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/scsi/
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#include <linux/string.h>
270#include <linux/kernel.h>
271#include <linux/ioport.h>
272#include <linux/delay.h>
273#include <asm/io.h>
274#include <asm/system.h>
275#include <asm/byteorder.h>
276#include <linux/proc_fs.h>
277#include <linux/blkdev.h>
278#include <linux/interrupt.h>
279#include <linux/stat.h>
280#include <linux/pci.h>
281#include <linux/init.h>
282#include <linux/ctype.h>
283#include <linux/spinlock.h>
284#include <linux/slab.h>
285#include <asm/dma.h>
286#include <asm/irq.h>
287
288#include <scsi/scsi.h>
289#include <scsi/scsi_cmnd.h>
290#include <scsi/scsi_device.h>
291#include <scsi/scsi_host.h>
292#include <scsi/scsi_tcq.h>
293#include <scsi/scsicam.h>
294
295static int u14_34f_detect(struct scsi_host_template *);
296static int u14_34f_release(struct Scsi_Host *);
297static int u14_34f_queuecommand(struct scsi_cmnd *, void (*done)(struct scsi_cmnd *));
298static int u14_34f_eh_abort(struct scsi_cmnd *);
299static int u14_34f_eh_host_reset(struct scsi_cmnd *);
300static int u14_34f_bios_param(struct scsi_device *, struct block_device *,
301                              sector_t, int *);
302static int u14_34f_slave_configure(struct scsi_device *);
303
304static struct scsi_host_template driver_template = {
305                .name                    = "UltraStor 14F/34F rev. 8.10.00 ",
306                .detect                  = u14_34f_detect,
307                .release                 = u14_34f_release,
308                .queuecommand            = u14_34f_queuecommand,
309                .eh_abort_handler        = u14_34f_eh_abort,
310                .eh_host_reset_handler   = u14_34f_eh_host_reset,
311                .bios_param              = u14_34f_bios_param,
312                .slave_configure         = u14_34f_slave_configure,
313                .this_id                 = 7,
314                .unchecked_isa_dma       = 1,
315                .use_clustering          = ENABLE_CLUSTERING,
316                };
317
318#if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
319#error "Adjust your <asm/byteorder.h> defines"
320#endif
321
322/* Values for the PRODUCT_ID ports for the 14/34F */
323#define PRODUCT_ID1  0x56
324#define PRODUCT_ID2  0x40        /* NOTE: Only upper nibble is used */
325
326/* Subversion values */
327#define ISA  0
328#define ESA 1
329
330#define OP_HOST_ADAPTER   0x1
331#define OP_SCSI           0x2
332#define OP_RESET          0x4
333#define DTD_SCSI          0x0
334#define DTD_IN            0x1
335#define DTD_OUT           0x2
336#define DTD_NONE          0x3
337#define HA_CMD_INQUIRY    0x1
338#define HA_CMD_SELF_DIAG  0x2
339#define HA_CMD_READ_BUFF  0x3
340#define HA_CMD_WRITE_BUFF 0x4
341
342#undef  DEBUG_LINKED_COMMANDS
343#undef  DEBUG_DETECT
344#undef  DEBUG_INTERRUPT
345#undef  DEBUG_RESET
346#undef  DEBUG_GENERATE_ERRORS
347#undef  DEBUG_GENERATE_ABORTS
348#undef  DEBUG_GEOMETRY
349
350#define MAX_ISA 3
351#define MAX_VESA 1
352#define MAX_EISA 0
353#define MAX_PCI 0
354#define MAX_BOARDS (MAX_ISA + MAX_VESA + MAX_EISA + MAX_PCI)
355#define MAX_CHANNEL 1
356#define MAX_LUN 8
357#define MAX_TARGET 8
358#define MAX_MAILBOXES 16
359#define MAX_SGLIST 32
360#define MAX_SAFE_SGLIST 16
361#define MAX_INTERNAL_RETRIES 64
362#define MAX_CMD_PER_LUN 2
363#define MAX_TAGGED_CMD_PER_LUN (MAX_MAILBOXES - MAX_CMD_PER_LUN)
364
365#define SKIP ULONG_MAX
366#define FALSE 0
367#define TRUE 1
368#define FREE 0
369#define IN_USE   1
370#define LOCKED   2
371#define IN_RESET 3
372#define IGNORE   4
373#define READY    5
374#define ABORTING 6
375#define NO_DMA  0xff
376#define MAXLOOP  10000
377#define TAG_DISABLED 0
378#define TAG_SIMPLE   1
379#define TAG_ORDERED  2
380
381#define REG_LCL_MASK      0
382#define REG_LCL_INTR      1
383#define REG_SYS_MASK      2
384#define REG_SYS_INTR      3
385#define REG_PRODUCT_ID1   4
386#define REG_PRODUCT_ID2   5
387#define REG_CONFIG1       6
388#define REG_CONFIG2       7
389#define REG_OGM           8
390#define REG_ICM           12
391#define REGION_SIZE       13UL
392#define BSY_ASSERTED      0x01
393#define IRQ_ASSERTED      0x01
394#define CMD_RESET         0xc0
395#define CMD_OGM_INTR      0x01
396#define CMD_CLR_INTR      0x01
397#define CMD_ENA_INTR      0x81
398#define ASOK              0x00
399#define ASST              0x91
400
401#define YESNO(a) ((a) ? 'y' : 'n')
402#define TLDEV(type) ((type) == TYPE_DISK || (type) == TYPE_ROM)
403
404#define PACKED          __attribute__((packed))
405
406struct sg_list {
407   unsigned int address;                /* Segment Address */
408   unsigned int num_bytes;              /* Segment Length */
409   };
410
411/* MailBox SCSI Command Packet */
412struct mscp {
413
414#if defined(__BIG_ENDIAN_BITFIELD)
415   unsigned char sg:1, ca:1, dcn:1, xdir:2, opcode:3;
416   unsigned char lun: 3, channel:2, target:3;
417#else
418   unsigned char opcode: 3,             /* type of command */
419                 xdir: 2,               /* data transfer direction */
420                 dcn: 1,                /* disable disconnect */
421                 ca: 1,                 /* use cache (if available) */
422                 sg: 1;                 /* scatter/gather operation */
423   unsigned char target: 3,             /* SCSI target id */
424                 channel: 2,            /* SCSI channel number */
425                 lun: 3;                /* SCSI logical unit number */
426#endif
427
428   unsigned int data_address PACKED;    /* transfer data pointer */
429   unsigned int data_len PACKED;        /* length in bytes */
430   unsigned int link_address PACKED;    /* for linking command chains */
431   unsigned char clink_id;              /* identifies command in chain */
432   unsigned char use_sg;                /* (if sg is set) 8 bytes per list */
433   unsigned char sense_len;
434   unsigned char cdb_len;               /* 6, 10, or 12 */
435   unsigned char cdb[12];               /* SCSI Command Descriptor Block */
436   unsigned char adapter_status;        /* non-zero indicates HA error */
437   unsigned char target_status;         /* non-zero indicates target error */
438   unsigned int sense_addr PACKED;
439
440   /* Additional fields begin here. */
441   struct scsi_cmnd *SCpnt;
442   unsigned int cpp_index;              /* cp index */
443
444   /* All the cp structure is zero filled by queuecommand except the
445      following CP_TAIL_SIZE bytes, initialized by detect */
446   dma_addr_t cp_dma_addr; /* dma handle for this cp structure */
447   struct sg_list *sglist; /* pointer to the allocated SG list */
448   };
449
450#define CP_TAIL_SIZE (sizeof(struct sglist *) + sizeof(dma_addr_t))
451
452struct hostdata {
453   struct mscp cp[MAX_MAILBOXES];       /* Mailboxes for this board */
454   unsigned int cp_stat[MAX_MAILBOXES]; /* FREE, IN_USE, LOCKED, IN_RESET */
455   unsigned int last_cp_used;           /* Index of last mailbox used */
456   unsigned int iocount;                /* Total i/o done for this board */
457   int board_number;                    /* Number of this board */
458   char board_name[16];                 /* Name of this board */
459   int in_reset;                        /* True if board is doing a reset */
460   int target_to[MAX_TARGET][MAX_CHANNEL]; /* N. of timeout errors on target */
461   int target_redo[MAX_TARGET][MAX_CHANNEL]; /* If TRUE redo i/o on target */
462   unsigned int retries;                /* Number of internal retries */
463   unsigned long last_retried_pid;      /* Pid of last retried command */
464   unsigned char subversion;            /* Bus type, either ISA or ESA */
465   struct pci_dev *pdev;                /* Always NULL */
466   unsigned char heads;
467   unsigned char sectors;
468   char board_id[256];                  /* data from INQUIRY on this board */
469   };
470
471static struct Scsi_Host *sh[MAX_BOARDS + 1];
472static const char *driver_name = "Ux4F";
473static char sha[MAX_BOARDS];
474static DEFINE_SPINLOCK(driver_lock);
475
476/* Initialize num_boards so that ihdlr can work while detect is in progress */
477static unsigned int num_boards = MAX_BOARDS;
478
479static unsigned long io_port[] = {
480
481   /* Space for MAX_INT_PARAM ports usable while loading as a module */
482   SKIP,    SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,   SKIP,
483   SKIP,    SKIP,
484
485   /* Possible ISA/VESA ports */
486   0x330, 0x340, 0x230, 0x240, 0x210, 0x130, 0x140,
487
488   /* End of list */
489   0x0
490   };
491
492#define HD(board) ((struct hostdata *) &sh[board]->hostdata)
493#define BN(board) (HD(board)->board_name)
494
495/* Device is Little Endian */
496#define H2DEV(x) cpu_to_le32(x)
497#define DEV2H(x) le32_to_cpu(x)
498
499static irqreturn_t do_interrupt_handler(int, void *);
500static void flush_dev(struct scsi_device *, unsigned long, unsigned int, unsigned int);
501static int do_trace = FALSE;
502static int setup_done = FALSE;
503static int link_statistics;
504static int ext_tran = FALSE;
505
506#if defined(HAVE_OLD_UX4F_FIRMWARE)
507static int have_old_firmware = TRUE;
508#else
509static int have_old_firmware = FALSE;
510#endif
511
512#if defined(CONFIG_SCSI_U14_34F_TAGGED_QUEUE)
513static int tag_mode = TAG_SIMPLE;
514#else
515static int tag_mode = TAG_DISABLED;
516#endif
517
518#if defined(CONFIG_SCSI_U14_34F_LINKED_COMMANDS)
519static int linked_comm = TRUE;
520#else
521static int linked_comm = FALSE;
522#endif
523
524#if defined(CONFIG_SCSI_U14_34F_MAX_TAGS)
525static int max_queue_depth = CONFIG_SCSI_U14_34F_MAX_TAGS;
526#else
527static int max_queue_depth = MAX_CMD_PER_LUN;
528#endif
529
530#define MAX_INT_PARAM 10
531#define MAX_BOOT_OPTIONS_SIZE 256
532static char boot_options[MAX_BOOT_OPTIONS_SIZE];
533
534#if defined(MODULE)
535#include <linux/module.h>
536#include <linux/moduleparam.h>
537
538module_param_string(u14_34f, boot_options, MAX_BOOT_OPTIONS_SIZE, 0);
539MODULE_PARM_DESC(u14_34f, " equivalent to the \"u14-34f=...\" kernel boot " \
540"option." \
541"      Example: modprobe u14-34f \"u14_34f=0x340,0x330,lc:y,tm:0,mq:4\"");
542MODULE_AUTHOR("Dario Ballabio");
543MODULE_LICENSE("GPL");
544MODULE_DESCRIPTION("UltraStor 14F/34F SCSI Driver");
545
546#endif
547
548static int u14_34f_slave_configure(struct scsi_device *dev) {
549   int j, tqd, utqd;
550   char *tag_suffix, *link_suffix;
551   struct Scsi_Host *host = dev->host;
552
553   j = ((struct hostdata *) host->hostdata)->board_number;
554
555   utqd = MAX_CMD_PER_LUN;
556   tqd = max_queue_depth;
557
558   if (TLDEV(dev->type) && dev->tagged_supported)
559
560      if (tag_mode == TAG_SIMPLE) {
561         scsi_adjust_queue_depth(dev, MSG_SIMPLE_TAG, tqd);
562         tag_suffix = ", simple tags";
563         }
564      else if (tag_mode == TAG_ORDERED) {
565         scsi_adjust_queue_depth(dev, MSG_ORDERED_TAG, tqd);
566         tag_suffix = ", ordered tags";
567         }
568      else {
569         scsi_adjust_queue_depth(dev, 0, tqd);
570         tag_suffix = ", no tags";
571         }
572
573   else if (TLDEV(dev->type) && linked_comm) {
574      scsi_adjust_queue_depth(dev, 0, tqd);
575      tag_suffix = ", untagged";
576      }
577
578   else {
579      scsi_adjust_queue_depth(dev, 0, utqd);
580      tag_suffix = "";
581      }
582
583   if (TLDEV(dev->type) && linked_comm && dev->queue_depth > 2)
584      link_suffix = ", sorted";
585   else if (TLDEV(dev->type))
586      link_suffix = ", unsorted";
587   else
588      link_suffix = "";
589
590   sdev_printk(KERN_INFO, dev, "cmds/lun %d%s%s.\n",
591          dev->queue_depth, link_suffix, tag_suffix);
592
593   return FALSE;
594}
595
596static int wait_on_busy(unsigned long iobase, unsigned int loop) {
597
598   while (inb(iobase + REG_LCL_INTR) & BSY_ASSERTED) {
599      udelay(1L);
600      if (--loop == 0) return TRUE;
601      }
602
603   return FALSE;
604}
605
606static int board_inquiry(unsigned int j) {
607   struct mscp *cpp;
608   dma_addr_t id_dma_addr;
609   unsigned int limit = 0;
610   unsigned long time;
611
612   id_dma_addr = pci_map_single(HD(j)->pdev, HD(j)->board_id,
613                    sizeof(HD(j)->board_id), PCI_DMA_BIDIRECTIONAL);
614   cpp = &HD(j)->cp[0];
615   cpp->cp_dma_addr = pci_map_single(HD(j)->pdev, cpp, sizeof(struct mscp),
616                                     PCI_DMA_BIDIRECTIONAL);
617   memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
618   cpp->opcode = OP_HOST_ADAPTER;
619   cpp->xdir = DTD_IN;
620   cpp->data_address = H2DEV(id_dma_addr);
621   cpp->data_len = H2DEV(sizeof(HD(j)->board_id));
622   cpp->cdb_len = 6;
623   cpp->cdb[0] = HA_CMD_INQUIRY;
624
625   if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
626      printk("%s: board_inquiry, adapter busy.\n", BN(j));
627      return TRUE;
628      }
629
630   HD(j)->cp_stat[0] = IGNORE;
631
632   /* Clear the interrupt indication */
633   outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
634
635   /* Store pointer in OGM address bytes */
636   outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
637
638   /* Issue OGM interrupt */
639   outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
640
641   spin_unlock_irq(&driver_lock);
642   time = jiffies;
643   while ((jiffies - time) < HZ && limit++ < 20000) udelay(100L);
644   spin_lock_irq(&driver_lock);
645
646   if (cpp->adapter_status || HD(j)->cp_stat[0] != FREE) {
647      HD(j)->cp_stat[0] = FREE;
648      printk("%s: board_inquiry, err 0x%x.\n", BN(j), cpp->adapter_status);
649      return TRUE;
650      }
651
652   pci_unmap_single(HD(j)->pdev, cpp->cp_dma_addr, sizeof(struct mscp),
653                    PCI_DMA_BIDIRECTIONAL);
654   pci_unmap_single(HD(j)->pdev, id_dma_addr, sizeof(HD(j)->board_id),
655                    PCI_DMA_BIDIRECTIONAL);
656   return FALSE;
657}
658
659static int port_detect \
660      (unsigned long port_base, unsigned int j, struct scsi_host_template *tpnt) {
661   unsigned char irq, dma_channel, subversion, i;
662   unsigned char in_byte;
663   char *bus_type, dma_name[16];
664
665   /* Allowed BIOS base addresses (NULL indicates reserved) */
666   unsigned long bios_segment_table[8] = {
667      0,
668      0xc4000, 0xc8000, 0xcc000, 0xd0000,
669      0xd4000, 0xd8000, 0xdc000
670      };
671
672   /* Allowed IRQs */
673   unsigned char interrupt_table[4] = { 15, 14, 11, 10 };
674
675   /* Allowed DMA channels for ISA (0 indicates reserved) */
676   unsigned char dma_channel_table[4] = { 5, 6, 7, 0 };
677
678   /* Head/sector mappings */
679   struct {
680      unsigned char heads;
681      unsigned char sectors;
682      } mapping_table[4] = {
683           { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 }
684           };
685
686   struct config_1 {
687
688#if defined(__BIG_ENDIAN_BITFIELD)
689      unsigned char dma_channel: 2, interrupt:2,
690                    removable_disks_as_fixed:1, bios_segment: 3;
691#else
692      unsigned char bios_segment: 3, removable_disks_as_fixed: 1,
693                    interrupt: 2, dma_channel: 2;
694#endif
695
696      } config_1;
697
698   struct config_2 {
699
700#if defined(__BIG_ENDIAN_BITFIELD)
701      unsigned char tfr_port: 2, bios_drive_number: 1,
702                    mapping_mode: 2, ha_scsi_id: 3;
703#else
704      unsigned char ha_scsi_id: 3, mapping_mode: 2,
705                    bios_drive_number: 1, tfr_port: 2;
706#endif
707
708      } config_2;
709
710   char name[16];
711
712   sprintf(name, "%s%d", driver_name, j);
713
714   if (!request_region(port_base, REGION_SIZE, driver_name)) {
715#if defined(DEBUG_DETECT)
716      printk("%s: address 0x%03lx in use, skipping probe.\n", name, port_base);
717#endif
718      goto fail;
719      }
720
721   spin_lock_irq(&driver_lock);
722
723   if (inb(port_base + REG_PRODUCT_ID1) != PRODUCT_ID1) goto freelock;
724
725   in_byte = inb(port_base + REG_PRODUCT_ID2);
726
727   if ((in_byte & 0xf0) != PRODUCT_ID2) goto freelock;
728
729   *(char *)&config_1 = inb(port_base + REG_CONFIG1);
730   *(char *)&config_2 = inb(port_base + REG_CONFIG2);
731
732   irq = interrupt_table[config_1.interrupt];
733   dma_channel = dma_channel_table[config_1.dma_channel];
734   subversion = (in_byte & 0x0f);
735
736   /* Board detected, allocate its IRQ */
737   if (request_irq(irq, do_interrupt_handler,
738             IRQF_DISABLED | ((subversion == ESA) ? IRQF_SHARED : 0),
739             driver_name, (void *) &sha[j])) {
740      printk("%s: unable to allocate IRQ %u, detaching.\n", name, irq);
741      goto freelock;
742      }
743
744   if (subversion == ISA && request_dma(dma_channel, driver_name)) {
745      printk("%s: unable to allocate DMA channel %u, detaching.\n",
746             name, dma_channel);
747      goto freeirq;
748      }
749
750   if (have_old_firmware) tpnt->use_clustering = DISABLE_CLUSTERING;
751
752   spin_unlock_irq(&driver_lock);
753   sh[j] = scsi_register(tpnt, sizeof(struct hostdata));
754   spin_lock_irq(&driver_lock);
755
756   if (sh[j] == NULL) {
757      printk("%s: unable to register host, detaching.\n", name);
758      goto freedma;
759      }
760
761   sh[j]->io_port = port_base;
762   sh[j]->unique_id = port_base;
763   sh[j]->n_io_port = REGION_SIZE;
764   sh[j]->base = bios_segment_table[config_1.bios_segment];
765   sh[j]->irq = irq;
766   sh[j]->sg_tablesize = MAX_SGLIST;
767   sh[j]->this_id = config_2.ha_scsi_id;
768   sh[j]->can_queue = MAX_MAILBOXES;
769   sh[j]->cmd_per_lun = MAX_CMD_PER_LUN;
770
771#if defined(DEBUG_DETECT)
772   {
773   unsigned char sys_mask, lcl_mask;
774
775   sys_mask = inb(sh[j]->io_port + REG_SYS_MASK);
776   lcl_mask = inb(sh[j]->io_port + REG_LCL_MASK);
777   printk("SYS_MASK 0x%x, LCL_MASK 0x%x.\n", sys_mask, lcl_mask);
778   }
779#endif
780
781   /* Probably a bogus host scsi id, set it to the dummy value */
782   if (sh[j]->this_id == 0) sh[j]->this_id = -1;
783
784   /* If BIOS is disabled, force enable interrupts */
785   if (sh[j]->base == 0) outb(CMD_ENA_INTR, sh[j]->io_port + REG_SYS_MASK);
786
787   memset(HD(j), 0, sizeof(struct hostdata));
788   HD(j)->heads = mapping_table[config_2.mapping_mode].heads;
789   HD(j)->sectors = mapping_table[config_2.mapping_mode].sectors;
790   HD(j)->subversion = subversion;
791   HD(j)->pdev = NULL;
792   HD(j)->board_number = j;
793
794   if (have_old_firmware) sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
795
796   if (HD(j)->subversion == ESA) {
797      sh[j]->unchecked_isa_dma = FALSE;
798      sh[j]->dma_channel = NO_DMA;
799      sprintf(BN(j), "U34F%d", j);
800      bus_type = "VESA";
801      }
802   else {
803      unsigned long flags;
804      sh[j]->unchecked_isa_dma = TRUE;
805
806      flags=claim_dma_lock();
807      disable_dma(dma_channel);
808      clear_dma_ff(dma_channel);
809      set_dma_mode(dma_channel, DMA_MODE_CASCADE);
810      enable_dma(dma_channel);
811      release_dma_lock(flags);
812
813      sh[j]->dma_channel = dma_channel;
814      sprintf(BN(j), "U14F%d", j);
815      bus_type = "ISA";
816      }
817
818   sh[j]->max_channel = MAX_CHANNEL - 1;
819   sh[j]->max_id = MAX_TARGET;
820   sh[j]->max_lun = MAX_LUN;
821
822   if (HD(j)->subversion == ISA && !board_inquiry(j)) {
823      HD(j)->board_id[40] = 0;
824
825      if (strcmp(&HD(j)->board_id[32], "06000600")) {
826         printk("%s: %s.\n", BN(j), &HD(j)->board_id[8]);
827         printk("%s: firmware %s is outdated, FW PROM should be 28004-006.\n",
828                BN(j), &HD(j)->board_id[32]);
829         sh[j]->hostt->use_clustering = DISABLE_CLUSTERING;
830         sh[j]->sg_tablesize = MAX_SAFE_SGLIST;
831         }
832      }
833
834   if (dma_channel == NO_DMA) sprintf(dma_name, "%s", "BMST");
835   else                       sprintf(dma_name, "DMA %u", dma_channel);
836
837   spin_unlock_irq(&driver_lock);
838
839   for (i = 0; i < sh[j]->can_queue; i++)
840      HD(j)->cp[i].cp_dma_addr = pci_map_single(HD(j)->pdev,
841            &HD(j)->cp[i], sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
842
843   for (i = 0; i < sh[j]->can_queue; i++)
844      if (! ((&HD(j)->cp[i])->sglist = kmalloc(
845            sh[j]->sg_tablesize * sizeof(struct sg_list),
846            (sh[j]->unchecked_isa_dma ? GFP_DMA : 0) | GFP_ATOMIC))) {
847         printk("%s: kmalloc SGlist failed, mbox %d, detaching.\n", BN(j), i);
848         goto release;
849         }
850
851   if (max_queue_depth > MAX_TAGGED_CMD_PER_LUN)
852       max_queue_depth = MAX_TAGGED_CMD_PER_LUN;
853
854   if (max_queue_depth < MAX_CMD_PER_LUN) max_queue_depth = MAX_CMD_PER_LUN;
855
856   if (tag_mode != TAG_DISABLED && tag_mode != TAG_SIMPLE)
857      tag_mode = TAG_ORDERED;
858
859   if (j == 0) {
860      printk("UltraStor 14F/34F: Copyright (C) 1994-2003 Dario Ballabio.\n");
861      printk("%s config options -> of:%c, tm:%d, lc:%c, mq:%d, et:%c.\n",
862             driver_name, YESNO(have_old_firmware), tag_mode,
863             YESNO(linked_comm), max_queue_depth, YESNO(ext_tran));
864      }
865
866   printk("%s: %s 0x%03lx, BIOS 0x%05x, IRQ %u, %s, SG %d, MB %d.\n",
867          BN(j), bus_type, (unsigned long)sh[j]->io_port, (int)sh[j]->base,
868          sh[j]->irq, dma_name, sh[j]->sg_tablesize, sh[j]->can_queue);
869
870   if (sh[j]->max_id > 8 || sh[j]->max_lun > 8)
871      printk("%s: wide SCSI support enabled, max_id %u, max_lun %u.\n",
872             BN(j), sh[j]->max_id, sh[j]->max_lun);
873
874   for (i = 0; i <= sh[j]->max_channel; i++)
875      printk("%s: SCSI channel %u enabled, host target ID %d.\n",
876             BN(j), i, sh[j]->this_id);
877
878   return TRUE;
879
880freedma:
881   if (subversion == ISA) free_dma(dma_channel);
882freeirq:
883   free_irq(irq, &sha[j]);
884freelock:
885   spin_unlock_irq(&driver_lock);
886   release_region(port_base, REGION_SIZE);
887fail:
888   return FALSE;
889
890release:
891   u14_34f_release(sh[j]);
892   return FALSE;
893}
894
895static void internal_setup(char *str, int *ints) {
896   int i, argc = ints[0];
897   char *cur = str, *pc;
898
899   if (argc > 0) {
900
901      if (argc > MAX_INT_PARAM) argc = MAX_INT_PARAM;
902
903      for (i = 0; i < argc; i++) io_port[i] = ints[i + 1];
904
905      io_port[i] = 0;
906      setup_done = TRUE;
907      }
908
909   while (cur && (pc = strchr(cur, ':'))) {
910      int val = 0, c = *++pc;
911
912      if (c == 'n' || c == 'N') val = FALSE;
913      else if (c == 'y' || c == 'Y') val = TRUE;
914      else val = (int) simple_strtoul(pc, NULL, 0);
915
916      if (!strncmp(cur, "lc:", 3)) linked_comm = val;
917      else if (!strncmp(cur, "of:", 3)) have_old_firmware = val;
918      else if (!strncmp(cur, "tm:", 3)) tag_mode = val;
919      else if (!strncmp(cur, "tc:", 3)) tag_mode = val;
920      else if (!strncmp(cur, "mq:", 3))  max_queue_depth = val;
921      else if (!strncmp(cur, "ls:", 3))  link_statistics = val;
922      else if (!strncmp(cur, "et:", 3))  ext_tran = val;
923
924      if ((cur = strchr(cur, ','))) ++cur;
925      }
926
927   return;
928}
929
930static int option_setup(char *str) {
931   int ints[MAX_INT_PARAM];
932   char *cur = str;
933   int i = 1;
934
935   while (cur && isdigit(*cur) && i < MAX_INT_PARAM) {
936      ints[i++] = simple_strtoul(cur, NULL, 0);
937
938      if ((cur = strchr(cur, ',')) != NULL) cur++;
939   }
940
941   ints[0] = i - 1;
942   internal_setup(cur, ints);
943   return 1;
944}
945
946static int u14_34f_detect(struct scsi_host_template *tpnt) {
947   unsigned int j = 0, k;
948
949   tpnt->proc_name = "u14-34f";
950
951   if(strlen(boot_options)) option_setup(boot_options);
952
953#if defined(MODULE)
954   /* io_port could have been modified when loading as a module */
955   if(io_port[0] != SKIP) {
956      setup_done = TRUE;
957      io_port[MAX_INT_PARAM] = 0;
958      }
959#endif
960
961   for (k = 0; k < MAX_BOARDS + 1; k++) sh[k] = NULL;
962
963   for (k = 0; io_port[k]; k++) {
964
965      if (io_port[k] == SKIP) continue;
966
967      if (j < MAX_BOARDS && port_detect(io_port[k], j, tpnt)) j++;
968      }
969
970   num_boards = j;
971   return j;
972}
973
974static void map_dma(unsigned int i, unsigned int j) {
975   unsigned int data_len = 0;
976   unsigned int k, pci_dir;
977   int count;
978   struct scatterlist *sg;
979   struct mscp *cpp;
980   struct scsi_cmnd *SCpnt;
981
982   cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
983   pci_dir = SCpnt->sc_data_direction;
984
985   if (SCpnt->sense_buffer)
986      cpp->sense_addr = H2DEV(pci_map_single(HD(j)->pdev, SCpnt->sense_buffer,
987                           SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE));
988
989   cpp->sense_len = SCSI_SENSE_BUFFERSIZE;
990
991   if (scsi_bufflen(SCpnt)) {
992	   count = scsi_dma_map(SCpnt);
993	   BUG_ON(count < 0);
994
995	   scsi_for_each_sg(SCpnt, sg, count, k) {
996		   cpp->sglist[k].address = H2DEV(sg_dma_address(sg));
997		   cpp->sglist[k].num_bytes = H2DEV(sg_dma_len(sg));
998		   data_len += sg->length;
999	   }
1000
1001	   cpp->sg = TRUE;
1002	   cpp->use_sg = scsi_sg_count(SCpnt);
1003	   cpp->data_address =
1004		   H2DEV(pci_map_single(HD(j)->pdev, cpp->sglist,
1005					cpp->use_sg * sizeof(struct sg_list),
1006					pci_dir));
1007	   cpp->data_len = H2DEV(data_len);
1008
1009   } else {
1010	   pci_dir = PCI_DMA_BIDIRECTIONAL;
1011	   cpp->data_len = H2DEV(scsi_bufflen(SCpnt));
1012   }
1013}
1014
1015static void unmap_dma(unsigned int i, unsigned int j) {
1016   unsigned int pci_dir;
1017   struct mscp *cpp;
1018   struct scsi_cmnd *SCpnt;
1019
1020   cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1021   pci_dir = SCpnt->sc_data_direction;
1022
1023   if (DEV2H(cpp->sense_addr))
1024      pci_unmap_single(HD(j)->pdev, DEV2H(cpp->sense_addr),
1025                       DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
1026
1027   scsi_dma_unmap(SCpnt);
1028
1029   if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
1030
1031   if (DEV2H(cpp->data_address))
1032      pci_unmap_single(HD(j)->pdev, DEV2H(cpp->data_address),
1033                       DEV2H(cpp->data_len), pci_dir);
1034}
1035
1036static void sync_dma(unsigned int i, unsigned int j) {
1037   unsigned int pci_dir;
1038   struct mscp *cpp;
1039   struct scsi_cmnd *SCpnt;
1040
1041   cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1042   pci_dir = SCpnt->sc_data_direction;
1043
1044   if (DEV2H(cpp->sense_addr))
1045      pci_dma_sync_single_for_cpu(HD(j)->pdev, DEV2H(cpp->sense_addr),
1046                          DEV2H(cpp->sense_len), PCI_DMA_FROMDEVICE);
1047
1048   if (scsi_sg_count(SCpnt))
1049	   pci_dma_sync_sg_for_cpu(HD(j)->pdev, scsi_sglist(SCpnt),
1050				   scsi_sg_count(SCpnt), pci_dir);
1051
1052   if (!DEV2H(cpp->data_len)) pci_dir = PCI_DMA_BIDIRECTIONAL;
1053
1054   if (DEV2H(cpp->data_address))
1055      pci_dma_sync_single_for_cpu(HD(j)->pdev, DEV2H(cpp->data_address),
1056                       DEV2H(cpp->data_len), pci_dir);
1057}
1058
1059static void scsi_to_dev_dir(unsigned int i, unsigned int j) {
1060   unsigned int k;
1061
1062   static const unsigned char data_out_cmds[] = {
1063      0x0a, 0x2a, 0x15, 0x55, 0x04, 0x07, 0x18, 0x1d, 0x24, 0x2e,
1064      0x30, 0x31, 0x32, 0x38, 0x39, 0x3a, 0x3b, 0x3d, 0x3f, 0x40,
1065      0x41, 0x4c, 0xaa, 0xae, 0xb0, 0xb1, 0xb2, 0xb6, 0xea, 0x1b, 0x5d
1066      };
1067
1068   static const unsigned char data_none_cmds[] = {
1069      0x01, 0x0b, 0x10, 0x11, 0x13, 0x16, 0x17, 0x19, 0x2b, 0x1e,
1070      0x2c, 0xac, 0x2f, 0xaf, 0x33, 0xb3, 0x35, 0x36, 0x45, 0x47,
1071      0x48, 0x49, 0xa9, 0x4b, 0xa5, 0xa6, 0xb5, 0x00
1072      };
1073
1074   struct mscp *cpp;
1075   struct scsi_cmnd *SCpnt;
1076
1077   cpp = &HD(j)->cp[i]; SCpnt = cpp->SCpnt;
1078
1079   if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
1080      cpp->xdir = DTD_IN;
1081      return;
1082      }
1083   else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
1084      cpp->xdir = DTD_OUT;
1085      return;
1086      }
1087   else if (SCpnt->sc_data_direction == DMA_NONE) {
1088      cpp->xdir = DTD_NONE;
1089      return;
1090      }
1091
1092   if (SCpnt->sc_data_direction != DMA_BIDIRECTIONAL)
1093      panic("%s: qcomm, invalid SCpnt->sc_data_direction.\n", BN(j));
1094
1095   cpp->xdir = DTD_IN;
1096
1097   for (k = 0; k < ARRAY_SIZE(data_out_cmds); k++)
1098      if (SCpnt->cmnd[0] == data_out_cmds[k]) {
1099         cpp->xdir = DTD_OUT;
1100         break;
1101         }
1102
1103   if (cpp->xdir == DTD_IN)
1104      for (k = 0; k < ARRAY_SIZE(data_none_cmds); k++)
1105         if (SCpnt->cmnd[0] == data_none_cmds[k]) {
1106            cpp->xdir = DTD_NONE;
1107            break;
1108            }
1109
1110}
1111
1112static int u14_34f_queuecommand(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *)) {
1113   unsigned int i, j, k;
1114   struct mscp *cpp;
1115
1116   /* j is the board number */
1117   j = ((struct hostdata *) SCpnt->device->host->hostdata)->board_number;
1118
1119   if (SCpnt->host_scribble)
1120      panic("%s: qcomm, pid %ld, SCpnt %p already active.\n",
1121            BN(j), SCpnt->serial_number, SCpnt);
1122
1123   /* i is the mailbox number, look for the first free mailbox
1124      starting from last_cp_used */
1125   i = HD(j)->last_cp_used + 1;
1126
1127   for (k = 0; k < sh[j]->can_queue; k++, i++) {
1128
1129      if (i >= sh[j]->can_queue) i = 0;
1130
1131      if (HD(j)->cp_stat[i] == FREE) {
1132         HD(j)->last_cp_used = i;
1133         break;
1134         }
1135      }
1136
1137   if (k == sh[j]->can_queue) {
1138      printk("%s: qcomm, no free mailbox.\n", BN(j));
1139      return 1;
1140      }
1141
1142   /* Set pointer to control packet structure */
1143   cpp = &HD(j)->cp[i];
1144
1145   memset(cpp, 0, sizeof(struct mscp) - CP_TAIL_SIZE);
1146   SCpnt->scsi_done = done;
1147   cpp->cpp_index = i;
1148   SCpnt->host_scribble = (unsigned char *) &cpp->cpp_index;
1149
1150   if (do_trace) printk("%s: qcomm, mbox %d, target %d.%d:%d, pid %ld.\n",
1151                        BN(j), i, SCpnt->device->channel, SCpnt->device->id,
1152                        SCpnt->device->lun, SCpnt->serial_number);
1153
1154   cpp->opcode = OP_SCSI;
1155   cpp->channel = SCpnt->device->channel;
1156   cpp->target = SCpnt->device->id;
1157   cpp->lun = SCpnt->device->lun;
1158   cpp->SCpnt = SCpnt;
1159   cpp->cdb_len = SCpnt->cmd_len;
1160   memcpy(cpp->cdb, SCpnt->cmnd, SCpnt->cmd_len);
1161
1162   /* Use data transfer direction SCpnt->sc_data_direction */
1163   scsi_to_dev_dir(i, j);
1164
1165   /* Map DMA buffers and SG list */
1166   map_dma(i, j);
1167
1168   if (linked_comm && SCpnt->device->queue_depth > 2
1169                                     && TLDEV(SCpnt->device->type)) {
1170      HD(j)->cp_stat[i] = READY;
1171      flush_dev(SCpnt->device, blk_rq_pos(SCpnt->request), j, FALSE);
1172      return 0;
1173      }
1174
1175   if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1176      unmap_dma(i, j);
1177      SCpnt->host_scribble = NULL;
1178      scmd_printk(KERN_INFO, SCpnt,
1179      		"qcomm, pid %ld, adapter busy.\n", SCpnt->serial_number);
1180      return 1;
1181      }
1182
1183   /* Store pointer in OGM address bytes */
1184   outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
1185
1186   /* Issue OGM interrupt */
1187   outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
1188
1189   HD(j)->cp_stat[i] = IN_USE;
1190   return 0;
1191}
1192
1193static int u14_34f_eh_abort(struct scsi_cmnd *SCarg) {
1194   unsigned int i, j;
1195
1196   j = ((struct hostdata *) SCarg->device->host->hostdata)->board_number;
1197
1198   if (SCarg->host_scribble == NULL) {
1199      scmd_printk(KERN_INFO, SCarg, "abort, pid %ld inactive.\n",
1200             SCarg->serial_number);
1201      return SUCCESS;
1202      }
1203
1204   i = *(unsigned int *)SCarg->host_scribble;
1205   scmd_printk(KERN_INFO, SCarg, "abort, mbox %d, pid %ld.\n",
1206	       i, SCarg->serial_number);
1207
1208   if (i >= sh[j]->can_queue)
1209      panic("%s: abort, invalid SCarg->host_scribble.\n", BN(j));
1210
1211   if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1212      printk("%s: abort, timeout error.\n", BN(j));
1213      return FAILED;
1214      }
1215
1216   if (HD(j)->cp_stat[i] == FREE) {
1217      printk("%s: abort, mbox %d is free.\n", BN(j), i);
1218      return SUCCESS;
1219      }
1220
1221   if (HD(j)->cp_stat[i] == IN_USE) {
1222      printk("%s: abort, mbox %d is in use.\n", BN(j), i);
1223
1224      if (SCarg != HD(j)->cp[i].SCpnt)
1225         panic("%s: abort, mbox %d, SCarg %p, cp SCpnt %p.\n",
1226               BN(j), i, SCarg, HD(j)->cp[i].SCpnt);
1227
1228      if (inb(sh[j]->io_port + REG_SYS_INTR) & IRQ_ASSERTED)
1229         printk("%s: abort, mbox %d, interrupt pending.\n", BN(j), i);
1230
1231      return FAILED;
1232      }
1233
1234   if (HD(j)->cp_stat[i] == IN_RESET) {
1235      printk("%s: abort, mbox %d is in reset.\n", BN(j), i);
1236      return FAILED;
1237      }
1238
1239   if (HD(j)->cp_stat[i] == LOCKED) {
1240      printk("%s: abort, mbox %d is locked.\n", BN(j), i);
1241      return SUCCESS;
1242      }
1243
1244   if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1245      unmap_dma(i, j);
1246      SCarg->result = DID_ABORT << 16;
1247      SCarg->host_scribble = NULL;
1248      HD(j)->cp_stat[i] = FREE;
1249      printk("%s, abort, mbox %d ready, DID_ABORT, pid %ld done.\n",
1250             BN(j), i, SCarg->serial_number);
1251      SCarg->scsi_done(SCarg);
1252      return SUCCESS;
1253      }
1254
1255   panic("%s: abort, mbox %d, invalid cp_stat.\n", BN(j), i);
1256}
1257
1258static int u14_34f_eh_host_reset(struct scsi_cmnd *SCarg) {
1259   unsigned int i, j, k, c, limit = 0;
1260   unsigned long time;
1261   int arg_done = FALSE;
1262   struct scsi_cmnd *SCpnt;
1263
1264   j = ((struct hostdata *) SCarg->device->host->hostdata)->board_number;
1265   scmd_printk(KERN_INFO, SCarg, "reset, enter, pid %ld.\n", SCarg->serial_number);
1266
1267   spin_lock_irq(sh[j]->host_lock);
1268
1269   if (SCarg->host_scribble == NULL)
1270      printk("%s: reset, pid %ld inactive.\n", BN(j), SCarg->serial_number);
1271
1272   if (HD(j)->in_reset) {
1273      printk("%s: reset, exit, already in reset.\n", BN(j));
1274      spin_unlock_irq(sh[j]->host_lock);
1275      return FAILED;
1276      }
1277
1278   if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1279      printk("%s: reset, exit, timeout error.\n", BN(j));
1280      spin_unlock_irq(sh[j]->host_lock);
1281      return FAILED;
1282      }
1283
1284   HD(j)->retries = 0;
1285
1286   for (c = 0; c <= sh[j]->max_channel; c++)
1287      for (k = 0; k < sh[j]->max_id; k++) {
1288         HD(j)->target_redo[k][c] = TRUE;
1289         HD(j)->target_to[k][c] = 0;
1290         }
1291
1292   for (i = 0; i < sh[j]->can_queue; i++) {
1293
1294      if (HD(j)->cp_stat[i] == FREE) continue;
1295
1296      if (HD(j)->cp_stat[i] == LOCKED) {
1297         HD(j)->cp_stat[i] = FREE;
1298         printk("%s: reset, locked mbox %d forced free.\n", BN(j), i);
1299         continue;
1300         }
1301
1302      if (!(SCpnt = HD(j)->cp[i].SCpnt))
1303         panic("%s: reset, mbox %d, SCpnt == NULL.\n", BN(j), i);
1304
1305      if (HD(j)->cp_stat[i] == READY || HD(j)->cp_stat[i] == ABORTING) {
1306         HD(j)->cp_stat[i] = ABORTING;
1307         printk("%s: reset, mbox %d aborting, pid %ld.\n",
1308                BN(j), i, SCpnt->serial_number);
1309         }
1310
1311      else {
1312         HD(j)->cp_stat[i] = IN_RESET;
1313         printk("%s: reset, mbox %d in reset, pid %ld.\n",
1314                BN(j), i, SCpnt->serial_number);
1315         }
1316
1317      if (SCpnt->host_scribble == NULL)
1318         panic("%s: reset, mbox %d, garbled SCpnt.\n", BN(j), i);
1319
1320      if (*(unsigned int *)SCpnt->host_scribble != i)
1321         panic("%s: reset, mbox %d, index mismatch.\n", BN(j), i);
1322
1323      if (SCpnt->scsi_done == NULL)
1324         panic("%s: reset, mbox %d, SCpnt->scsi_done == NULL.\n", BN(j), i);
1325
1326      if (SCpnt == SCarg) arg_done = TRUE;
1327      }
1328
1329   if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1330      printk("%s: reset, cannot reset, timeout error.\n", BN(j));
1331      spin_unlock_irq(sh[j]->host_lock);
1332      return FAILED;
1333      }
1334
1335   outb(CMD_RESET, sh[j]->io_port + REG_LCL_INTR);
1336   printk("%s: reset, board reset done, enabling interrupts.\n", BN(j));
1337
1338#if defined(DEBUG_RESET)
1339   do_trace = TRUE;
1340#endif
1341
1342   HD(j)->in_reset = TRUE;
1343
1344   spin_unlock_irq(sh[j]->host_lock);
1345   time = jiffies;
1346   while ((jiffies - time) < (10 * HZ) && limit++ < 200000) udelay(100L);
1347   spin_lock_irq(sh[j]->host_lock);
1348
1349   printk("%s: reset, interrupts disabled, loops %d.\n", BN(j), limit);
1350
1351   for (i = 0; i < sh[j]->can_queue; i++) {
1352
1353      if (HD(j)->cp_stat[i] == IN_RESET) {
1354         SCpnt = HD(j)->cp[i].SCpnt;
1355         unmap_dma(i, j);
1356         SCpnt->result = DID_RESET << 16;
1357         SCpnt->host_scribble = NULL;
1358
1359         /* This mailbox is still waiting for its interrupt */
1360         HD(j)->cp_stat[i] = LOCKED;
1361
1362         printk("%s, reset, mbox %d locked, DID_RESET, pid %ld done.\n",
1363                BN(j), i, SCpnt->serial_number);
1364         }
1365
1366      else if (HD(j)->cp_stat[i] == ABORTING) {
1367         SCpnt = HD(j)->cp[i].SCpnt;
1368         unmap_dma(i, j);
1369         SCpnt->result = DID_RESET << 16;
1370         SCpnt->host_scribble = NULL;
1371
1372         /* This mailbox was never queued to the adapter */
1373         HD(j)->cp_stat[i] = FREE;
1374
1375         printk("%s, reset, mbox %d aborting, DID_RESET, pid %ld done.\n",
1376                BN(j), i, SCpnt->serial_number);
1377         }
1378
1379      else
1380
1381         /* Any other mailbox has already been set free by interrupt */
1382         continue;
1383
1384      SCpnt->scsi_done(SCpnt);
1385      }
1386
1387   HD(j)->in_reset = FALSE;
1388   do_trace = FALSE;
1389
1390   if (arg_done) printk("%s: reset, exit, pid %ld done.\n", BN(j), SCarg->serial_number);
1391   else          printk("%s: reset, exit.\n", BN(j));
1392
1393   spin_unlock_irq(sh[j]->host_lock);
1394   return SUCCESS;
1395}
1396
1397static int u14_34f_bios_param(struct scsi_device *disk,
1398                 struct block_device *bdev, sector_t capacity, int *dkinfo) {
1399   unsigned int j = 0;
1400   unsigned int size = capacity;
1401
1402   dkinfo[0] = HD(j)->heads;
1403   dkinfo[1] = HD(j)->sectors;
1404   dkinfo[2] = size / (HD(j)->heads * HD(j)->sectors);
1405
1406   if (ext_tran && (scsicam_bios_param(bdev, capacity, dkinfo) < 0)) {
1407      dkinfo[0] = 255;
1408      dkinfo[1] = 63;
1409      dkinfo[2] = size / (dkinfo[0] * dkinfo[1]);
1410      }
1411
1412#if defined(DEBUG_GEOMETRY)
1413   printk ("%s: bios_param, head=%d, sec=%d, cyl=%d.\n", driver_name,
1414           dkinfo[0], dkinfo[1], dkinfo[2]);
1415#endif
1416
1417   return FALSE;
1418}
1419
1420static void sort(unsigned long sk[], unsigned int da[], unsigned int n,
1421                 unsigned int rev) {
1422   unsigned int i, j, k, y;
1423   unsigned long x;
1424
1425   for (i = 0; i < n - 1; i++) {
1426      k = i;
1427
1428      for (j = k + 1; j < n; j++)
1429         if (rev) {
1430            if (sk[j] > sk[k]) k = j;
1431            }
1432         else {
1433            if (sk[j] < sk[k]) k = j;
1434            }
1435
1436      if (k != i) {
1437         x = sk[k]; sk[k] = sk[i]; sk[i] = x;
1438         y = da[k]; da[k] = da[i]; da[i] = y;
1439         }
1440      }
1441
1442   return;
1443   }
1444
1445static int reorder(unsigned int j, unsigned long cursec,
1446                 unsigned int ihdlr, unsigned int il[], unsigned int n_ready) {
1447   struct scsi_cmnd *SCpnt;
1448   struct mscp *cpp;
1449   unsigned int k, n;
1450   unsigned int rev = FALSE, s = TRUE, r = TRUE;
1451   unsigned int input_only = TRUE, overlap = FALSE;
1452   unsigned long sl[n_ready], pl[n_ready], ll[n_ready];
1453   unsigned long maxsec = 0, minsec = ULONG_MAX, seek = 0, iseek = 0;
1454   unsigned long ioseek = 0;
1455
1456   static unsigned int flushcount = 0, batchcount = 0, sortcount = 0;
1457   static unsigned int readycount = 0, ovlcount = 0, inputcount = 0;
1458   static unsigned int readysorted = 0, revcount = 0;
1459   static unsigned long seeksorted = 0, seeknosort = 0;
1460
1461   if (link_statistics && !(++flushcount % link_statistics))
1462      printk("fc %d bc %d ic %d oc %d rc %d rs %d sc %d re %d"\
1463             " av %ldK as %ldK.\n", flushcount, batchcount, inputcount,
1464             ovlcount, readycount, readysorted, sortcount, revcount,
1465             seeknosort / (readycount + 1),
1466             seeksorted / (readycount + 1));
1467
1468   if (n_ready <= 1) return FALSE;
1469
1470   for (n = 0; n < n_ready; n++) {
1471      k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1472
1473      if (!(cpp->xdir == DTD_IN)) input_only = FALSE;
1474
1475      if (blk_rq_pos(SCpnt->request) < minsec)
1476	 minsec = blk_rq_pos(SCpnt->request);
1477      if (blk_rq_pos(SCpnt->request) > maxsec)
1478	 maxsec = blk_rq_pos(SCpnt->request);
1479
1480      sl[n] = blk_rq_pos(SCpnt->request);
1481      ioseek += blk_rq_sectors(SCpnt->request);
1482
1483      if (!n) continue;
1484
1485      if (sl[n] < sl[n - 1]) s = FALSE;
1486      if (sl[n] > sl[n - 1]) r = FALSE;
1487
1488      if (link_statistics) {
1489         if (sl[n] > sl[n - 1])
1490            seek += sl[n] - sl[n - 1];
1491         else
1492            seek += sl[n - 1] - sl[n];
1493         }
1494
1495      }
1496
1497   if (link_statistics) {
1498      if (cursec > sl[0]) seek += cursec - sl[0]; else seek += sl[0] - cursec;
1499      }
1500
1501   if (cursec > ((maxsec + minsec) / 2)) rev = TRUE;
1502
1503   if (ioseek > ((maxsec - minsec) / 2)) rev = FALSE;
1504
1505   if (!((rev && r) || (!rev && s))) sort(sl, il, n_ready, rev);
1506
1507   if (!input_only) for (n = 0; n < n_ready; n++) {
1508      k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1509      ll[n] = blk_rq_sectors(SCpnt->request); pl[n] = SCpnt->serial_number;
1510
1511      if (!n) continue;
1512
1513      if ((sl[n] == sl[n - 1]) || (!rev && ((sl[n - 1] + ll[n - 1]) > sl[n]))
1514          || (rev && ((sl[n] + ll[n]) > sl[n - 1]))) overlap = TRUE;
1515      }
1516
1517   if (overlap) sort(pl, il, n_ready, FALSE);
1518
1519   if (link_statistics) {
1520      if (cursec > sl[0]) iseek = cursec - sl[0]; else iseek = sl[0] - cursec;
1521      batchcount++; readycount += n_ready; seeknosort += seek / 1024;
1522      if (input_only) inputcount++;
1523      if (overlap) { ovlcount++; seeksorted += iseek / 1024; }
1524      else seeksorted += (iseek + maxsec - minsec) / 1024;
1525      if (rev && !r)     {  revcount++; readysorted += n_ready; }
1526      if (!rev && !s)    { sortcount++; readysorted += n_ready; }
1527      }
1528
1529#if defined(DEBUG_LINKED_COMMANDS)
1530   if (link_statistics && (overlap || !(flushcount % link_statistics)))
1531      for (n = 0; n < n_ready; n++) {
1532         k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1533         printk("%s %d.%d:%d pid %ld mb %d fc %d nr %d sec %ld ns %u"\
1534                " cur %ld s:%c r:%c rev:%c in:%c ov:%c xd %d.\n",
1535                (ihdlr ? "ihdlr" : "qcomm"), SCpnt->channel, SCpnt->target,
1536                SCpnt->lun, SCpnt->serial_number, k, flushcount, n_ready,
1537                blk_rq_pos(SCpnt->request), blk_rq_sectors(SCpnt->request),
1538		cursec, YESNO(s), YESNO(r), YESNO(rev), YESNO(input_only),
1539                YESNO(overlap), cpp->xdir);
1540         }
1541#endif
1542   return overlap;
1543}
1544
1545static void flush_dev(struct scsi_device *dev, unsigned long cursec, unsigned int j,
1546                      unsigned int ihdlr) {
1547   struct scsi_cmnd *SCpnt;
1548   struct mscp *cpp;
1549   unsigned int k, n, n_ready = 0, il[MAX_MAILBOXES];
1550
1551   for (k = 0; k < sh[j]->can_queue; k++) {
1552
1553      if (HD(j)->cp_stat[k] != READY && HD(j)->cp_stat[k] != IN_USE) continue;
1554
1555      cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1556
1557      if (SCpnt->device != dev) continue;
1558
1559      if (HD(j)->cp_stat[k] == IN_USE) return;
1560
1561      il[n_ready++] = k;
1562      }
1563
1564   if (reorder(j, cursec, ihdlr, il, n_ready)) n_ready = 1;
1565
1566   for (n = 0; n < n_ready; n++) {
1567      k = il[n]; cpp = &HD(j)->cp[k]; SCpnt = cpp->SCpnt;
1568
1569      if (wait_on_busy(sh[j]->io_port, MAXLOOP)) {
1570         scmd_printk(KERN_INFO, SCpnt,
1571	 	"%s, pid %ld, mbox %d, adapter"
1572                " busy, will abort.\n", (ihdlr ? "ihdlr" : "qcomm"),
1573                SCpnt->serial_number, k);
1574         HD(j)->cp_stat[k] = ABORTING;
1575         continue;
1576         }
1577
1578      outl(H2DEV(cpp->cp_dma_addr), sh[j]->io_port + REG_OGM);
1579      outb(CMD_OGM_INTR, sh[j]->io_port + REG_LCL_INTR);
1580      HD(j)->cp_stat[k] = IN_USE;
1581      }
1582
1583}
1584
1585static irqreturn_t ihdlr(unsigned int j)
1586{
1587   struct scsi_cmnd *SCpnt;
1588   unsigned int i, k, c, status, tstatus, reg, ret;
1589   struct mscp *spp, *cpp;
1590   int irq = sh[j]->irq;
1591
1592   /* Check if this board need to be serviced */
1593   if (!((reg = inb(sh[j]->io_port + REG_SYS_INTR)) & IRQ_ASSERTED)) goto none;
1594
1595   HD(j)->iocount++;
1596
1597   if (do_trace) printk("%s: ihdlr, enter, irq %d, count %d.\n", BN(j), irq,
1598                        HD(j)->iocount);
1599
1600   /* Check if this board is still busy */
1601   if (wait_on_busy(sh[j]->io_port, 20 * MAXLOOP)) {
1602      outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
1603      printk("%s: ihdlr, busy timeout error,  irq %d, reg 0x%x, count %d.\n",
1604             BN(j), irq, reg, HD(j)->iocount);
1605      goto none;
1606      }
1607
1608   ret = inl(sh[j]->io_port + REG_ICM);
1609
1610   /* Clear interrupt pending flag */
1611   outb(CMD_CLR_INTR, sh[j]->io_port + REG_SYS_INTR);
1612
1613   /* Find the mailbox to be serviced on this board */
1614   for (i = 0; i < sh[j]->can_queue; i++)
1615      if (H2DEV(HD(j)->cp[i].cp_dma_addr) == ret) break;
1616
1617   if (i >= sh[j]->can_queue)
1618      panic("%s: ihdlr, invalid mscp bus address %p, cp0 %p.\n", BN(j),
1619            (void *)ret, (void *)H2DEV(HD(j)->cp[0].cp_dma_addr));
1620
1621   cpp = &(HD(j)->cp[i]);
1622   spp = cpp;
1623
1624#if defined(DEBUG_GENERATE_ABORTS)
1625   if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 500) < 3)) goto handled;
1626#endif
1627
1628   if (HD(j)->cp_stat[i] == IGNORE) {
1629      HD(j)->cp_stat[i] = FREE;
1630      goto handled;
1631      }
1632   else if (HD(j)->cp_stat[i] == LOCKED) {
1633      HD(j)->cp_stat[i] = FREE;
1634      printk("%s: ihdlr, mbox %d unlocked, count %d.\n", BN(j), i,
1635             HD(j)->iocount);
1636      goto handled;
1637      }
1638   else if (HD(j)->cp_stat[i] == FREE) {
1639      printk("%s: ihdlr, mbox %d is free, count %d.\n", BN(j), i,
1640             HD(j)->iocount);
1641      goto handled;
1642      }
1643   else if (HD(j)->cp_stat[i] == IN_RESET)
1644      printk("%s: ihdlr, mbox %d is in reset.\n", BN(j), i);
1645   else if (HD(j)->cp_stat[i] != IN_USE)
1646      panic("%s: ihdlr, mbox %d, invalid cp_stat: %d.\n",
1647            BN(j), i, HD(j)->cp_stat[i]);
1648
1649   HD(j)->cp_stat[i] = FREE;
1650   SCpnt = cpp->SCpnt;
1651
1652   if (SCpnt == NULL) panic("%s: ihdlr, mbox %d, SCpnt == NULL.\n", BN(j), i);
1653
1654   if (SCpnt->host_scribble == NULL)
1655      panic("%s: ihdlr, mbox %d, pid %ld, SCpnt %p garbled.\n", BN(j), i,
1656            SCpnt->serial_number, SCpnt);
1657
1658   if (*(unsigned int *)SCpnt->host_scribble != i)
1659      panic("%s: ihdlr, mbox %d, pid %ld, index mismatch %d.\n",
1660            BN(j), i, SCpnt->serial_number, *(unsigned int *)SCpnt->host_scribble);
1661
1662   sync_dma(i, j);
1663
1664   if (linked_comm && SCpnt->device->queue_depth > 2
1665                                     && TLDEV(SCpnt->device->type))
1666      flush_dev(SCpnt->device, blk_rq_pos(SCpnt->request), j, TRUE);
1667
1668   tstatus = status_byte(spp->target_status);
1669
1670#if defined(DEBUG_GENERATE_ERRORS)
1671   if ((HD(j)->iocount > 500) && ((HD(j)->iocount % 200) < 2))
1672                                           spp->adapter_status = 0x01;
1673#endif
1674
1675   switch (spp->adapter_status) {
1676      case ASOK:     /* status OK */
1677
1678         /* Forces a reset if a disk drive keeps returning BUSY */
1679         if (tstatus == BUSY && SCpnt->device->type != TYPE_TAPE)
1680            status = DID_ERROR << 16;
1681
1682         /* If there was a bus reset, redo operation on each target */
1683         else if (tstatus != GOOD && SCpnt->device->type == TYPE_DISK
1684                  && HD(j)->target_redo[scmd_id(SCpnt)][scmd_channel(SCpnt)])
1685            status = DID_BUS_BUSY << 16;
1686
1687         /* Works around a flaw in scsi.c */
1688         else if (tstatus == CHECK_CONDITION
1689                  && SCpnt->device->type == TYPE_DISK
1690                  && (SCpnt->sense_buffer[2] & 0xf) == RECOVERED_ERROR)
1691            status = DID_BUS_BUSY << 16;
1692
1693         else
1694            status = DID_OK << 16;
1695
1696         if (tstatus == GOOD)
1697            HD(j)->target_redo[scmd_id(SCpnt)][scmd_channel(SCpnt)] = FALSE;
1698
1699         if (spp->target_status && SCpnt->device->type == TYPE_DISK &&
1700             (!(tstatus == CHECK_CONDITION && HD(j)->iocount <= 1000 &&
1701               (SCpnt->sense_buffer[2] & 0xf) == NOT_READY)))
1702            scmd_printk(KERN_INFO, SCpnt,
1703	    	"ihdlr, pid %ld, target_status 0x%x, sense key 0x%x.\n",
1704                   SCpnt->serial_number, spp->target_status,
1705                   SCpnt->sense_buffer[2]);
1706
1707         HD(j)->target_to[scmd_id(SCpnt)][scmd_channel(SCpnt)] = 0;
1708
1709         if (HD(j)->last_retried_pid == SCpnt->serial_number) HD(j)->retries = 0;
1710
1711         break;
1712      case ASST:     /* Selection Time Out */
1713
1714         if (HD(j)->target_to[scmd_id(SCpnt)][scmd_channel(SCpnt)] > 1)
1715            status = DID_ERROR << 16;
1716         else {
1717            status = DID_TIME_OUT << 16;
1718            HD(j)->target_to[scmd_id(SCpnt)][scmd_channel(SCpnt)]++;
1719            }
1720
1721         break;
1722
1723      /* Perform a limited number of internal retries */
1724      case 0x93:     /* Unexpected bus free */
1725      case 0x94:     /* Target bus phase sequence failure */
1726      case 0x96:     /* Illegal SCSI command */
1727      case 0xa3:     /* SCSI bus reset error */
1728
1729         for (c = 0; c <= sh[j]->max_channel; c++)
1730            for (k = 0; k < sh[j]->max_id; k++)
1731               HD(j)->target_redo[k][c] = TRUE;
1732
1733
1734      case 0x92:     /* Data over/under-run */
1735
1736         if (SCpnt->device->type != TYPE_TAPE
1737             && HD(j)->retries < MAX_INTERNAL_RETRIES) {
1738
1739#if defined(DID_SOFT_ERROR)
1740            status = DID_SOFT_ERROR << 16;
1741#else
1742            status = DID_BUS_BUSY << 16;
1743#endif
1744
1745            HD(j)->retries++;
1746            HD(j)->last_retried_pid = SCpnt->serial_number;
1747            }
1748         else
1749            status = DID_ERROR << 16;
1750
1751         break;
1752      case 0x01:     /* Invalid command */
1753      case 0x02:     /* Invalid parameters */
1754      case 0x03:     /* Invalid data list */
1755      case 0x84:     /* SCSI bus abort error */
1756      case 0x9b:     /* Auto request sense error */
1757      case 0x9f:     /* Unexpected command complete message error */
1758      case 0xff:     /* Invalid parameter in the S/G list */
1759      default:
1760         status = DID_ERROR << 16;
1761         break;
1762      }
1763
1764   SCpnt->result = status | spp->target_status;
1765
1766#if defined(DEBUG_INTERRUPT)
1767   if (SCpnt->result || do_trace)
1768#else
1769   if ((spp->adapter_status != ASOK && HD(j)->iocount >  1000) ||
1770       (spp->adapter_status != ASOK &&
1771        spp->adapter_status != ASST && HD(j)->iocount <= 1000) ||
1772        do_trace || msg_byte(spp->target_status))
1773#endif
1774      scmd_printk(KERN_INFO, SCpnt, "ihdlr, mbox %2d, err 0x%x:%x,"\
1775             " pid %ld, reg 0x%x, count %d.\n",
1776             i, spp->adapter_status, spp->target_status, SCpnt->serial_number,
1777             reg, HD(j)->iocount);
1778
1779   unmap_dma(i, j);
1780
1781   /* Set the command state to inactive */
1782   SCpnt->host_scribble = NULL;
1783
1784   SCpnt->scsi_done(SCpnt);
1785
1786   if (do_trace) printk("%s: ihdlr, exit, irq %d, count %d.\n", BN(j), irq,
1787                        HD(j)->iocount);
1788
1789handled:
1790   return IRQ_HANDLED;
1791none:
1792   return IRQ_NONE;
1793}
1794
1795static irqreturn_t do_interrupt_handler(int irq, void *shap) {
1796   unsigned int j;
1797   unsigned long spin_flags;
1798   irqreturn_t ret;
1799
1800   /* Check if the interrupt must be processed by this handler */
1801   if ((j = (unsigned int)((char *)shap - sha)) >= num_boards) return IRQ_NONE;
1802
1803   spin_lock_irqsave(sh[j]->host_lock, spin_flags);
1804   ret = ihdlr(j);
1805   spin_unlock_irqrestore(sh[j]->host_lock, spin_flags);
1806   return ret;
1807}
1808
1809static int u14_34f_release(struct Scsi_Host *shpnt) {
1810   unsigned int i, j;
1811
1812   for (j = 0; sh[j] != NULL && sh[j] != shpnt; j++);
1813
1814   if (sh[j] == NULL)
1815      panic("%s: release, invalid Scsi_Host pointer.\n", driver_name);
1816
1817   for (i = 0; i < sh[j]->can_queue; i++)
1818      kfree((&HD(j)->cp[i])->sglist);
1819
1820   for (i = 0; i < sh[j]->can_queue; i++)
1821      pci_unmap_single(HD(j)->pdev, HD(j)->cp[i].cp_dma_addr,
1822                     sizeof(struct mscp), PCI_DMA_BIDIRECTIONAL);
1823
1824   free_irq(sh[j]->irq, &sha[j]);
1825
1826   if (sh[j]->dma_channel != NO_DMA)
1827      free_dma(sh[j]->dma_channel);
1828
1829   release_region(sh[j]->io_port, sh[j]->n_io_port);
1830   scsi_unregister(sh[j]);
1831   return FALSE;
1832}
1833
1834#include "scsi_module.c"
1835
1836#ifndef MODULE
1837__setup("u14-34f=", option_setup);
1838#endif /* end MODULE */
1839