Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[pandora-kernel.git] / drivers / scsi / qlogicisp.c
1 /*
2  * QLogic ISP1020 Intelligent SCSI Processor Driver (PCI)
3  * Written by Erik H. Moe, ehm@cris.com
4  * Copyright 1995, Erik H. Moe
5  * Copyright 1996, 1997  Michael A. Griffith <grif@acm.org>
6  * Copyright 2000, Jayson C. Vantuyl <vantuyl@csc.smsu.edu>
7  *             and Bryon W. Roche    <bryon@csc.smsu.edu>
8  *
9  * 64-bit addressing added by Kanoj Sarcar <kanoj@sgi.com>
10  *                         and Leo Dagum    <dagum@sgi.com>
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation; either version 2, or (at your option) any
15  * later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  */
22
23 #include <linux/blkdev.h>
24 #include <linux/config.h>
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/ioport.h>
28 #include <linux/sched.h>
29 #include <linux/types.h>
30 #include <linux/pci.h>
31 #include <linux/delay.h>
32 #include <linux/unistd.h>
33 #include <linux/spinlock.h>
34 #include <linux/interrupt.h>
35 #include <asm/io.h>
36 #include <asm/irq.h>
37 #include <asm/byteorder.h>
38 #include "scsi.h"
39 #include <scsi/scsi_host.h>
40
41 /*
42  * With the qlogic interface, every queue slot can hold a SCSI
43  * command with up to 4 scatter/gather entries.  If we need more
44  * than 4 entries, continuation entries can be used that hold
45  * another 7 entries each.  Unlike for other drivers, this means
46  * that the maximum number of scatter/gather entries we can
47  * support at any given time is a function of the number of queue
48  * slots available.  That is, host->can_queue and host->sg_tablesize
49  * are dynamic and _not_ independent.  This all works fine because
50  * requests are queued serially and the scatter/gather limit is
51  * determined for each queue request anew.
52  */
53 #define QLOGICISP_REQ_QUEUE_LEN 63      /* must be power of two - 1 */
54 #define QLOGICISP_MAX_SG(ql)    (4 + ((ql) > 0) ? 7*((ql) - 1) : 0)
55
56 /* Configuration section *****************************************************/
57
58 /* Set the following macro to 1 to reload the ISP1020's firmware.  This is
59    the latest firmware provided by QLogic.  This may be an earlier/later
60    revision than supplied by your board. */
61
62 #define RELOAD_FIRMWARE         1
63
64 /* Set the following macro to 1 to reload the ISP1020's defaults from nvram.
65    If you are not sure of your settings, leave this alone, the driver will
66    use a set of 'safe' defaults */
67
68 #define USE_NVRAM_DEFAULTS      0
69
70 /*  Macros used for debugging */
71
72 #define DEBUG_ISP1020           0
73 #define DEBUG_ISP1020_INTR      0
74 #define DEBUG_ISP1020_SETUP     0
75 #define TRACE_ISP               0
76
77 #define DEFAULT_LOOP_COUNT      1000000
78
79 /* End Configuration section *************************************************/
80
81 #include <linux/module.h>
82
83 #if TRACE_ISP
84
85 # define TRACE_BUF_LEN  (32*1024)
86
87 struct {
88         u_long          next;
89         struct {
90                 u_long          time;
91                 u_int           index;
92                 u_int           addr;
93                 u_char *        name;
94         } buf[TRACE_BUF_LEN];
95 } trace;
96
97 #define TRACE(w, i, a)                                          \
98 {                                                               \
99         unsigned long flags;                                    \
100                                                                 \
101         trace.buf[trace.next].name  = (w);                      \
102         trace.buf[trace.next].time  = jiffies;                  \
103         trace.buf[trace.next].index = (i);                      \
104         trace.buf[trace.next].addr  = (long) (a);               \
105         trace.next = (trace.next + 1) & (TRACE_BUF_LEN - 1);    \
106 }
107
108 #else
109 # define TRACE(w, i, a)
110 #endif
111
112 #if DEBUG_ISP1020
113 #define ENTER(x)        printk("isp1020 : entering %s()\n", x);
114 #define LEAVE(x)        printk("isp1020 : leaving %s()\n", x);
115 #define DEBUG(x)        x
116 #else
117 #define ENTER(x)
118 #define LEAVE(x)
119 #define DEBUG(x)
120 #endif /* DEBUG_ISP1020 */
121
122 #if DEBUG_ISP1020_INTR
123 #define ENTER_INTR(x)   printk("isp1020 : entering %s()\n", x);
124 #define LEAVE_INTR(x)   printk("isp1020 : leaving %s()\n", x);
125 #define DEBUG_INTR(x)   x
126 #else
127 #define ENTER_INTR(x)
128 #define LEAVE_INTR(x)
129 #define DEBUG_INTR(x)
130 #endif /* DEBUG ISP1020_INTR */
131
132 #define ISP1020_REV_ID  1
133
134 #define MAX_TARGETS     16
135 #define MAX_LUNS        8
136
137 /* host configuration and control registers */
138 #define HOST_HCCR       0xc0    /* host command and control */
139
140 /* pci bus interface registers */
141 #define PCI_ID_LOW      0x00    /* vendor id */
142 #define PCI_ID_HIGH     0x02    /* device id */
143 #define ISP_CFG0        0x04    /* configuration register #0 */
144 #define  ISP_CFG0_HWMSK  0x000f /* Hardware revision mask */
145 #define  ISP_CFG0_1020   0x0001 /* ISP1020 */
146 #define  ISP_CFG0_1020A  0x0002 /* ISP1020A */
147 #define  ISP_CFG0_1040   0x0003 /* ISP1040 */
148 #define  ISP_CFG0_1040A  0x0004 /* ISP1040A */
149 #define  ISP_CFG0_1040B  0x0005 /* ISP1040B */
150 #define  ISP_CFG0_1040C  0x0006 /* ISP1040C */
151 #define ISP_CFG1        0x06    /* configuration register #1 */
152 #define  ISP_CFG1_F128   0x0040 /* 128-byte FIFO threshold */
153 #define  ISP_CFG1_F64    0x0030 /* 128-byte FIFO threshold */
154 #define  ISP_CFG1_F32    0x0020 /* 128-byte FIFO threshold */
155 #define  ISP_CFG1_F16    0x0010 /* 128-byte FIFO threshold */
156 #define  ISP_CFG1_BENAB  0x0004 /* Global Bus burst enable */
157 #define  ISP_CFG1_SXP    0x0001 /* SXP register select */
158 #define PCI_INTF_CTL    0x08    /* pci interface control */
159 #define PCI_INTF_STS    0x0a    /* pci interface status */
160 #define PCI_SEMAPHORE   0x0c    /* pci semaphore */
161 #define PCI_NVRAM       0x0e    /* pci nvram interface */
162 #define CDMA_CONF       0x20    /* Command DMA Config */
163 #define DDMA_CONF       0x40    /* Data DMA Config */
164 #define  DMA_CONF_SENAB  0x0008 /* SXP to DMA Data enable */
165 #define  DMA_CONF_RIRQ   0x0004 /* RISC interrupt enable */
166 #define  DMA_CONF_BENAB  0x0002 /* Bus burst enable */
167 #define  DMA_CONF_DIR    0x0001 /* DMA direction (0=fifo->host 1=host->fifo) */
168
169 /* mailbox registers */
170 #define MBOX0           0x70    /* mailbox 0 */
171 #define MBOX1           0x72    /* mailbox 1 */
172 #define MBOX2           0x74    /* mailbox 2 */
173 #define MBOX3           0x76    /* mailbox 3 */
174 #define MBOX4           0x78    /* mailbox 4 */
175 #define MBOX5           0x7a    /* mailbox 5 */
176 #define MBOX6           0x7c    /* mailbox 6 */
177 #define MBOX7           0x7e    /* mailbox 7 */
178
179 /* mailbox command complete status codes */
180 #define MBOX_COMMAND_COMPLETE           0x4000
181 #define INVALID_COMMAND                 0x4001
182 #define HOST_INTERFACE_ERROR            0x4002
183 #define TEST_FAILED                     0x4003
184 #define COMMAND_ERROR                   0x4005
185 #define COMMAND_PARAM_ERROR             0x4006
186
187 /* async event status codes */
188 #define ASYNC_SCSI_BUS_RESET            0x8001
189 #define SYSTEM_ERROR                    0x8002
190 #define REQUEST_TRANSFER_ERROR          0x8003
191 #define RESPONSE_TRANSFER_ERROR         0x8004
192 #define REQUEST_QUEUE_WAKEUP            0x8005
193 #define EXECUTION_TIMEOUT_RESET         0x8006
194
195 #ifdef CONFIG_QL_ISP_A64
196 #define IOCB_SEGS                       2
197 #define CONTINUATION_SEGS               5
198 #define MAX_CONTINUATION_ENTRIES        254
199 #else
200 #define IOCB_SEGS                       4
201 #define CONTINUATION_SEGS               7
202 #endif /* CONFIG_QL_ISP_A64 */
203
204 struct Entry_header {
205         u_char  entry_type;
206         u_char  entry_cnt;
207         u_char  sys_def_1;
208         u_char  flags;
209 };
210
211 /* entry header type commands */
212 #ifdef CONFIG_QL_ISP_A64
213 #define ENTRY_COMMAND           9
214 #define ENTRY_CONTINUATION      0xa
215 #else
216 #define ENTRY_COMMAND           1
217 #define ENTRY_CONTINUATION      2
218 #endif /* CONFIG_QL_ISP_A64 */
219
220 #define ENTRY_STATUS            3
221 #define ENTRY_MARKER            4
222 #define ENTRY_EXTENDED_COMMAND  5
223
224 /* entry header flag definitions */
225 #define EFLAG_CONTINUATION      1
226 #define EFLAG_BUSY              2
227 #define EFLAG_BAD_HEADER        4
228 #define EFLAG_BAD_PAYLOAD       8
229
230 struct dataseg {
231         u_int                   d_base;
232 #ifdef CONFIG_QL_ISP_A64
233         u_int                   d_base_hi;
234 #endif
235         u_int                   d_count;
236 };
237
238 struct Command_Entry {
239         struct Entry_header     hdr;
240         u_int                   handle;
241         u_char                  target_lun;
242         u_char                  target_id;
243         u_short                 cdb_length;
244         u_short                 control_flags;
245         u_short                 rsvd;
246         u_short                 time_out;
247         u_short                 segment_cnt;
248         u_char                  cdb[12];
249 #ifdef CONFIG_QL_ISP_A64
250         u_int                   rsvd1;
251         u_int                   rsvd2;
252 #endif
253         struct dataseg          dataseg[IOCB_SEGS];
254 };
255
256 /* command entry control flag definitions */
257 #define CFLAG_NODISC            0x01
258 #define CFLAG_HEAD_TAG          0x02
259 #define CFLAG_ORDERED_TAG       0x04
260 #define CFLAG_SIMPLE_TAG        0x08
261 #define CFLAG_TAR_RTN           0x10
262 #define CFLAG_READ              0x20
263 #define CFLAG_WRITE             0x40
264
265 struct Ext_Command_Entry {
266         struct Entry_header     hdr;
267         u_int                   handle;
268         u_char                  target_lun;
269         u_char                  target_id;
270         u_short                 cdb_length;
271         u_short                 control_flags;
272         u_short                 rsvd;
273         u_short                 time_out;
274         u_short                 segment_cnt;
275         u_char                  cdb[44];
276 };
277
278 struct Continuation_Entry {
279         struct Entry_header     hdr;
280 #ifndef CONFIG_QL_ISP_A64
281         u_int                   reserved;
282 #endif
283         struct dataseg          dataseg[CONTINUATION_SEGS];
284 };
285
286 struct Marker_Entry {
287         struct Entry_header     hdr;
288         u_int                   reserved;
289         u_char                  target_lun;
290         u_char                  target_id;
291         u_char                  modifier;
292         u_char                  rsvd;
293         u_char                  rsvds[52];
294 };
295
296 /* marker entry modifier definitions */
297 #define SYNC_DEVICE     0
298 #define SYNC_TARGET     1
299 #define SYNC_ALL        2
300
301 struct Status_Entry {
302         struct Entry_header     hdr;
303         u_int                   handle;
304         u_short                 scsi_status;
305         u_short                 completion_status;
306         u_short                 state_flags;
307         u_short                 status_flags;
308         u_short                 time;
309         u_short                 req_sense_len;
310         u_int                   residual;
311         u_char                  rsvd[8];
312         u_char                  req_sense_data[32];
313 };
314
315 /* status entry completion status definitions */
316 #define CS_COMPLETE                     0x0000
317 #define CS_INCOMPLETE                   0x0001
318 #define CS_DMA_ERROR                    0x0002
319 #define CS_TRANSPORT_ERROR              0x0003
320 #define CS_RESET_OCCURRED               0x0004
321 #define CS_ABORTED                      0x0005
322 #define CS_TIMEOUT                      0x0006
323 #define CS_DATA_OVERRUN                 0x0007
324 #define CS_COMMAND_OVERRUN              0x0008
325 #define CS_STATUS_OVERRUN               0x0009
326 #define CS_BAD_MESSAGE                  0x000a
327 #define CS_NO_MESSAGE_OUT               0x000b
328 #define CS_EXT_ID_FAILED                0x000c
329 #define CS_IDE_MSG_FAILED               0x000d
330 #define CS_ABORT_MSG_FAILED             0x000e
331 #define CS_REJECT_MSG_FAILED            0x000f
332 #define CS_NOP_MSG_FAILED               0x0010
333 #define CS_PARITY_ERROR_MSG_FAILED      0x0011
334 #define CS_DEVICE_RESET_MSG_FAILED      0x0012
335 #define CS_ID_MSG_FAILED                0x0013
336 #define CS_UNEXP_BUS_FREE               0x0014
337 #define CS_DATA_UNDERRUN                0x0015
338
339 /* status entry state flag definitions */
340 #define SF_GOT_BUS                      0x0100
341 #define SF_GOT_TARGET                   0x0200
342 #define SF_SENT_CDB                     0x0400
343 #define SF_TRANSFERRED_DATA             0x0800
344 #define SF_GOT_STATUS                   0x1000
345 #define SF_GOT_SENSE                    0x2000
346
347 /* status entry status flag definitions */
348 #define STF_DISCONNECT                  0x0001
349 #define STF_SYNCHRONOUS                 0x0002
350 #define STF_PARITY_ERROR                0x0004
351 #define STF_BUS_RESET                   0x0008
352 #define STF_DEVICE_RESET                0x0010
353 #define STF_ABORTED                     0x0020
354 #define STF_TIMEOUT                     0x0040
355 #define STF_NEGOTIATION                 0x0080
356
357 /* interface control commands */
358 #define ISP_RESET                       0x0001
359 #define ISP_EN_INT                      0x0002
360 #define ISP_EN_RISC                     0x0004
361
362 /* host control commands */
363 #define HCCR_NOP                        0x0000
364 #define HCCR_RESET                      0x1000
365 #define HCCR_PAUSE                      0x2000
366 #define HCCR_RELEASE                    0x3000
367 #define HCCR_SINGLE_STEP                0x4000
368 #define HCCR_SET_HOST_INTR              0x5000
369 #define HCCR_CLEAR_HOST_INTR            0x6000
370 #define HCCR_CLEAR_RISC_INTR            0x7000
371 #define HCCR_BP_ENABLE                  0x8000
372 #define HCCR_BIOS_DISABLE               0x9000
373 #define HCCR_TEST_MODE                  0xf000
374
375 #define RISC_BUSY                       0x0004
376
377 /* mailbox commands */
378 #define MBOX_NO_OP                      0x0000
379 #define MBOX_LOAD_RAM                   0x0001
380 #define MBOX_EXEC_FIRMWARE              0x0002
381 #define MBOX_DUMP_RAM                   0x0003
382 #define MBOX_WRITE_RAM_WORD             0x0004
383 #define MBOX_READ_RAM_WORD              0x0005
384 #define MBOX_MAILBOX_REG_TEST           0x0006
385 #define MBOX_VERIFY_CHECKSUM            0x0007
386 #define MBOX_ABOUT_FIRMWARE             0x0008
387 #define MBOX_CHECK_FIRMWARE             0x000e
388 #define MBOX_INIT_REQ_QUEUE             0x0010
389 #define MBOX_INIT_RES_QUEUE             0x0011
390 #define MBOX_EXECUTE_IOCB               0x0012
391 #define MBOX_WAKE_UP                    0x0013
392 #define MBOX_STOP_FIRMWARE              0x0014
393 #define MBOX_ABORT                      0x0015
394 #define MBOX_ABORT_DEVICE               0x0016
395 #define MBOX_ABORT_TARGET               0x0017
396 #define MBOX_BUS_RESET                  0x0018
397 #define MBOX_STOP_QUEUE                 0x0019
398 #define MBOX_START_QUEUE                0x001a
399 #define MBOX_SINGLE_STEP_QUEUE          0x001b
400 #define MBOX_ABORT_QUEUE                0x001c
401 #define MBOX_GET_DEV_QUEUE_STATUS       0x001d
402 #define MBOX_GET_FIRMWARE_STATUS        0x001f
403 #define MBOX_GET_INIT_SCSI_ID           0x0020
404 #define MBOX_GET_SELECT_TIMEOUT         0x0021
405 #define MBOX_GET_RETRY_COUNT            0x0022
406 #define MBOX_GET_TAG_AGE_LIMIT          0x0023
407 #define MBOX_GET_CLOCK_RATE             0x0024
408 #define MBOX_GET_ACT_NEG_STATE          0x0025
409 #define MBOX_GET_ASYNC_DATA_SETUP_TIME  0x0026
410 #define MBOX_GET_PCI_PARAMS             0x0027
411 #define MBOX_GET_TARGET_PARAMS          0x0028
412 #define MBOX_GET_DEV_QUEUE_PARAMS       0x0029
413 #define MBOX_SET_INIT_SCSI_ID           0x0030
414 #define MBOX_SET_SELECT_TIMEOUT         0x0031
415 #define MBOX_SET_RETRY_COUNT            0x0032
416 #define MBOX_SET_TAG_AGE_LIMIT          0x0033
417 #define MBOX_SET_CLOCK_RATE             0x0034
418 #define MBOX_SET_ACTIVE_NEG_STATE       0x0035
419 #define MBOX_SET_ASYNC_DATA_SETUP_TIME  0x0036
420 #define MBOX_SET_PCI_CONTROL_PARAMS     0x0037
421 #define MBOX_SET_TARGET_PARAMS          0x0038
422 #define MBOX_SET_DEV_QUEUE_PARAMS       0x0039
423 #define MBOX_RETURN_BIOS_BLOCK_ADDR     0x0040
424 #define MBOX_WRITE_FOUR_RAM_WORDS       0x0041
425 #define MBOX_EXEC_BIOS_IOCB             0x0042
426
427 #ifdef CONFIG_QL_ISP_A64
428 #define MBOX_CMD_INIT_REQUEST_QUEUE_64      0x0052
429 #define MBOX_CMD_INIT_RESPONSE_QUEUE_64     0x0053
430 #endif /* CONFIG_QL_ISP_A64 */
431
432 #include "qlogicisp_asm.c"
433
434 #define PACKB(a, b)                     (((a)<<4)|(b))
435
436 static const u_char mbox_param[] = {
437         PACKB(1, 1),    /* MBOX_NO_OP */
438         PACKB(5, 5),    /* MBOX_LOAD_RAM */
439         PACKB(2, 0),    /* MBOX_EXEC_FIRMWARE */
440         PACKB(5, 5),    /* MBOX_DUMP_RAM */
441         PACKB(3, 3),    /* MBOX_WRITE_RAM_WORD */
442         PACKB(2, 3),    /* MBOX_READ_RAM_WORD */
443         PACKB(6, 6),    /* MBOX_MAILBOX_REG_TEST */
444         PACKB(2, 3),    /* MBOX_VERIFY_CHECKSUM */
445         PACKB(1, 3),    /* MBOX_ABOUT_FIRMWARE */
446         PACKB(0, 0),    /* 0x0009 */
447         PACKB(0, 0),    /* 0x000a */
448         PACKB(0, 0),    /* 0x000b */
449         PACKB(0, 0),    /* 0x000c */
450         PACKB(0, 0),    /* 0x000d */
451         PACKB(1, 2),    /* MBOX_CHECK_FIRMWARE */
452         PACKB(0, 0),    /* 0x000f */
453         PACKB(5, 5),    /* MBOX_INIT_REQ_QUEUE */
454         PACKB(6, 6),    /* MBOX_INIT_RES_QUEUE */
455         PACKB(4, 4),    /* MBOX_EXECUTE_IOCB */
456         PACKB(2, 2),    /* MBOX_WAKE_UP */
457         PACKB(1, 6),    /* MBOX_STOP_FIRMWARE */
458         PACKB(4, 4),    /* MBOX_ABORT */
459         PACKB(2, 2),    /* MBOX_ABORT_DEVICE */
460         PACKB(3, 3),    /* MBOX_ABORT_TARGET */
461         PACKB(2, 2),    /* MBOX_BUS_RESET */
462         PACKB(2, 3),    /* MBOX_STOP_QUEUE */
463         PACKB(2, 3),    /* MBOX_START_QUEUE */
464         PACKB(2, 3),    /* MBOX_SINGLE_STEP_QUEUE */
465         PACKB(2, 3),    /* MBOX_ABORT_QUEUE */
466         PACKB(2, 4),    /* MBOX_GET_DEV_QUEUE_STATUS */
467         PACKB(0, 0),    /* 0x001e */
468         PACKB(1, 3),    /* MBOX_GET_FIRMWARE_STATUS */
469         PACKB(1, 2),    /* MBOX_GET_INIT_SCSI_ID */
470         PACKB(1, 2),    /* MBOX_GET_SELECT_TIMEOUT */
471         PACKB(1, 3),    /* MBOX_GET_RETRY_COUNT */
472         PACKB(1, 2),    /* MBOX_GET_TAG_AGE_LIMIT */
473         PACKB(1, 2),    /* MBOX_GET_CLOCK_RATE */
474         PACKB(1, 2),    /* MBOX_GET_ACT_NEG_STATE */
475         PACKB(1, 2),    /* MBOX_GET_ASYNC_DATA_SETUP_TIME */
476         PACKB(1, 3),    /* MBOX_GET_PCI_PARAMS */
477         PACKB(2, 4),    /* MBOX_GET_TARGET_PARAMS */
478         PACKB(2, 4),    /* MBOX_GET_DEV_QUEUE_PARAMS */
479         PACKB(0, 0),    /* 0x002a */
480         PACKB(0, 0),    /* 0x002b */
481         PACKB(0, 0),    /* 0x002c */
482         PACKB(0, 0),    /* 0x002d */
483         PACKB(0, 0),    /* 0x002e */
484         PACKB(0, 0),    /* 0x002f */
485         PACKB(2, 2),    /* MBOX_SET_INIT_SCSI_ID */
486         PACKB(2, 2),    /* MBOX_SET_SELECT_TIMEOUT */
487         PACKB(3, 3),    /* MBOX_SET_RETRY_COUNT */
488         PACKB(2, 2),    /* MBOX_SET_TAG_AGE_LIMIT */
489         PACKB(2, 2),    /* MBOX_SET_CLOCK_RATE */
490         PACKB(2, 2),    /* MBOX_SET_ACTIVE_NEG_STATE */
491         PACKB(2, 2),    /* MBOX_SET_ASYNC_DATA_SETUP_TIME */
492         PACKB(3, 3),    /* MBOX_SET_PCI_CONTROL_PARAMS */
493         PACKB(4, 4),    /* MBOX_SET_TARGET_PARAMS */
494         PACKB(4, 4),    /* MBOX_SET_DEV_QUEUE_PARAMS */
495         PACKB(0, 0),    /* 0x003a */
496         PACKB(0, 0),    /* 0x003b */
497         PACKB(0, 0),    /* 0x003c */
498         PACKB(0, 0),    /* 0x003d */
499         PACKB(0, 0),    /* 0x003e */
500         PACKB(0, 0),    /* 0x003f */
501         PACKB(1, 2),    /* MBOX_RETURN_BIOS_BLOCK_ADDR */
502         PACKB(6, 1),    /* MBOX_WRITE_FOUR_RAM_WORDS */
503         PACKB(2, 3)     /* MBOX_EXEC_BIOS_IOCB */
504 #ifdef CONFIG_QL_ISP_A64
505         ,PACKB(0, 0),   /* 0x0043 */
506         PACKB(0, 0),    /* 0x0044 */
507         PACKB(0, 0),    /* 0x0045 */
508         PACKB(0, 0),    /* 0x0046 */
509         PACKB(0, 0),    /* 0x0047 */
510         PACKB(0, 0),    /* 0x0048 */
511         PACKB(0, 0),    /* 0x0049 */
512         PACKB(0, 0),    /* 0x004a */
513         PACKB(0, 0),    /* 0x004b */
514         PACKB(0, 0),    /* 0x004c */
515         PACKB(0, 0),    /* 0x004d */
516         PACKB(0, 0),    /* 0x004e */
517         PACKB(0, 0),    /* 0x004f */
518         PACKB(0, 0),    /* 0x0050 */
519         PACKB(0, 0),    /* 0x0051 */
520         PACKB(8, 8),    /* MBOX_CMD_INIT_REQUEST_QUEUE_64 (0x0052) */
521         PACKB(8, 8)     /* MBOX_CMD_INIT_RESPONSE_QUEUE_64 (0x0053) */
522 #endif /* CONFIG_QL_ISP_A64 */
523 };
524
525 #define MAX_MBOX_COMMAND        (sizeof(mbox_param)/sizeof(u_short))
526
527 struct host_param {
528         u_short         fifo_threshold;
529         u_short         host_adapter_enable;
530         u_short         initiator_scsi_id;
531         u_short         bus_reset_delay;
532         u_short         retry_count;
533         u_short         retry_delay;
534         u_short         async_data_setup_time;
535         u_short         req_ack_active_negation;
536         u_short         data_line_active_negation;
537         u_short         data_dma_burst_enable;
538         u_short         command_dma_burst_enable;
539         u_short         tag_aging;
540         u_short         selection_timeout;
541         u_short         max_queue_depth;
542 };
543
544 /*
545  * Device Flags:
546  *
547  * Bit  Name
548  * ---------
549  *  7   Disconnect Privilege
550  *  6   Parity Checking
551  *  5   Wide Data Transfers
552  *  4   Synchronous Data Transfers
553  *  3   Tagged Queuing
554  *  2   Automatic Request Sense
555  *  1   Stop Queue on Check Condition
556  *  0   Renegotiate on Error
557  */
558
559 struct dev_param {
560         u_short         device_flags;
561         u_short         execution_throttle;
562         u_short         synchronous_period;
563         u_short         synchronous_offset;
564         u_short         device_enable;
565         u_short         reserved; /* pad */
566 };
567
568 /*
569  * The result queue can be quite a bit smaller since continuation entries
570  * do not show up there:
571  */
572 #define RES_QUEUE_LEN           ((QLOGICISP_REQ_QUEUE_LEN + 1) / 8 - 1)
573 #define QUEUE_ENTRY_LEN         64
574 #define QSIZE(entries)  (((entries) + 1) * QUEUE_ENTRY_LEN)
575
576 struct isp_queue_entry {
577         char __opaque[QUEUE_ENTRY_LEN];
578 };
579
580 struct isp1020_hostdata {
581         void __iomem *memaddr;
582         u_char  revision;
583         struct  host_param host_param;
584         struct  dev_param dev_param[MAX_TARGETS];
585         struct  pci_dev *pci_dev;
586         
587         struct isp_queue_entry *res_cpu; /* CPU-side address of response queue. */
588         struct isp_queue_entry *req_cpu; /* CPU-size address of request queue. */
589
590         /* result and request queues (shared with isp1020): */
591         u_int   req_in_ptr;             /* index of next request slot */
592         u_int   res_out_ptr;            /* index of next result slot */
593
594         /* this is here so the queues are nicely aligned */
595         long    send_marker;            /* do we need to send a marker? */
596
597         /* The cmd->handle has a fixed size, and is only 32-bits.  We
598          * need to take care to handle 64-bit systems correctly thus what
599          * we actually place in cmd->handle is an index to the following
600          * table.  Kudos to Matt Jacob for the technique.  -DaveM
601          */
602         Scsi_Cmnd *cmd_slots[QLOGICISP_REQ_QUEUE_LEN + 1];
603
604         dma_addr_t res_dma;     /* PCI side view of response queue */
605         dma_addr_t req_dma;     /* PCI side view of request queue */
606 };
607
608 /* queue length's _must_ be power of two: */
609 #define QUEUE_DEPTH(in, out, ql)        ((in - out) & (ql))
610 #define REQ_QUEUE_DEPTH(in, out)        QUEUE_DEPTH(in, out,                 \
611                                                     QLOGICISP_REQ_QUEUE_LEN)
612 #define RES_QUEUE_DEPTH(in, out)        QUEUE_DEPTH(in, out, RES_QUEUE_LEN)
613
614 static void     isp1020_enable_irqs(struct Scsi_Host *);
615 static void     isp1020_disable_irqs(struct Scsi_Host *);
616 static int      isp1020_init(struct Scsi_Host *);
617 static int      isp1020_reset_hardware(struct Scsi_Host *);
618 static int      isp1020_set_defaults(struct Scsi_Host *);
619 static int      isp1020_load_parameters(struct Scsi_Host *);
620 static int      isp1020_mbox_command(struct Scsi_Host *, u_short []); 
621 static int      isp1020_return_status(struct Status_Entry *);
622 static void     isp1020_intr_handler(int, void *, struct pt_regs *);
623 static irqreturn_t do_isp1020_intr_handler(int, void *, struct pt_regs *);
624
625 #if USE_NVRAM_DEFAULTS
626 static int      isp1020_get_defaults(struct Scsi_Host *);
627 static int      isp1020_verify_nvram(struct Scsi_Host *);
628 static u_short  isp1020_read_nvram_word(struct Scsi_Host *, u_short);
629 #endif
630
631 #if DEBUG_ISP1020
632 static void     isp1020_print_scsi_cmd(Scsi_Cmnd *);
633 #endif
634 #if DEBUG_ISP1020_INTR
635 static void     isp1020_print_status_entry(struct Status_Entry *);
636 #endif
637
638 /* memaddr should be used to determine if memmapped port i/o is being used
639  * non-null memaddr == mmap'd
640  * JV 7-Jan-2000
641  */
642 static inline u_short isp_inw(struct Scsi_Host *host, long offset)
643 {
644         struct isp1020_hostdata *h = (struct isp1020_hostdata *)host->hostdata;
645         if (h->memaddr)
646                 return readw(h->memaddr + offset);
647         else
648                 return inw(host->io_port + offset);
649 }
650
651 static inline void isp_outw(u_short val, struct Scsi_Host *host, long offset)
652 {
653         struct isp1020_hostdata *h = (struct isp1020_hostdata *)host->hostdata;
654         if (h->memaddr)
655                 writew(val, h->memaddr + offset);
656         else
657                 outw(val, host->io_port + offset);
658 }
659
660 static inline void isp1020_enable_irqs(struct Scsi_Host *host)
661 {
662         isp_outw(ISP_EN_INT|ISP_EN_RISC, host, PCI_INTF_CTL);
663 }
664
665
666 static inline void isp1020_disable_irqs(struct Scsi_Host *host)
667 {
668         isp_outw(0x0, host, PCI_INTF_CTL);
669 }
670
671
672 static int isp1020_detect(Scsi_Host_Template *tmpt)
673 {
674         int hosts = 0;
675         struct Scsi_Host *host;
676         struct isp1020_hostdata *hostdata;
677         struct pci_dev *pdev = NULL;
678
679         ENTER("isp1020_detect");
680
681         tmpt->proc_name = "isp1020";
682
683         while ((pdev = pci_find_device(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP1020, pdev)))
684         {
685                 if (pci_enable_device(pdev))
686                         continue;
687
688                 host = scsi_register(tmpt, sizeof(struct isp1020_hostdata));
689                 if (!host)
690                         continue;
691
692                 hostdata = (struct isp1020_hostdata *) host->hostdata;
693
694                 memset(hostdata, 0, sizeof(struct isp1020_hostdata));
695
696                 hostdata->pci_dev = pdev;
697
698                 if (isp1020_init(host))
699                         goto fail_and_unregister;
700
701                 if (isp1020_reset_hardware(host)
702 #if USE_NVRAM_DEFAULTS
703                     || isp1020_get_defaults(host)
704 #else
705                     || isp1020_set_defaults(host)
706 #endif /* USE_NVRAM_DEFAULTS */
707                     || isp1020_load_parameters(host)) {
708                         goto fail_uninit;
709                 }
710
711                 host->this_id = hostdata->host_param.initiator_scsi_id;
712                 host->max_sectors = 64;
713
714                 if (request_irq(host->irq, do_isp1020_intr_handler, SA_INTERRUPT | SA_SHIRQ,
715                                 "qlogicisp", host))
716                 {
717                         printk("qlogicisp : interrupt %d already in use\n",
718                                host->irq);
719                         goto fail_uninit;
720                 }
721
722                 isp_outw(0x0, host, PCI_SEMAPHORE);
723                 isp_outw(HCCR_CLEAR_RISC_INTR, host, HOST_HCCR);
724                 isp1020_enable_irqs(host);
725
726                 hosts++;
727                 continue;
728
729         fail_uninit:
730                 iounmap(hostdata->memaddr);
731                 release_region(host->io_port, 0xff);
732         fail_and_unregister:
733                 if (hostdata->res_cpu)
734                         pci_free_consistent(hostdata->pci_dev,
735                                             QSIZE(RES_QUEUE_LEN),
736                                             hostdata->res_cpu,
737                                             hostdata->res_dma);
738                 if (hostdata->req_cpu)
739                         pci_free_consistent(hostdata->pci_dev,
740                                             QSIZE(QLOGICISP_REQ_QUEUE_LEN),
741                                             hostdata->req_cpu,
742                                             hostdata->req_dma);
743                 scsi_unregister(host);
744         }
745
746         LEAVE("isp1020_detect");
747
748         return hosts;
749 }
750
751
752 static int isp1020_release(struct Scsi_Host *host)
753 {
754         struct isp1020_hostdata *hostdata;
755
756         ENTER("isp1020_release");
757
758         hostdata = (struct isp1020_hostdata *) host->hostdata;
759
760         isp_outw(0x0, host, PCI_INTF_CTL);
761         free_irq(host->irq, host);
762
763         iounmap(hostdata->memaddr);
764
765         release_region(host->io_port, 0xff);
766
767         LEAVE("isp1020_release");
768
769         return 0;
770 }
771
772
773 static const char *isp1020_info(struct Scsi_Host *host)
774 {
775         static char buf[80];
776         struct isp1020_hostdata *hostdata;
777
778         ENTER("isp1020_info");
779
780         hostdata = (struct isp1020_hostdata *) host->hostdata;
781         sprintf(buf,
782                 "QLogic ISP1020 SCSI on PCI bus %02x device %02x irq %d %s base 0x%lx",
783                 hostdata->pci_dev->bus->number, hostdata->pci_dev->devfn, host->irq,
784                 (hostdata->memaddr ? "MEM" : "I/O"),
785                 (hostdata->memaddr ? (unsigned long)hostdata->memaddr : host->io_port));
786
787         LEAVE("isp1020_info");
788
789         return buf;
790 }
791
792
793 /*
794  * The middle SCSI layer ensures that queuecommand never gets invoked
795  * concurrently with itself or the interrupt handler (though the
796  * interrupt handler may call this routine as part of
797  * request-completion handling).
798  */
799 static int isp1020_queuecommand(Scsi_Cmnd *Cmnd, void (*done)(Scsi_Cmnd *))
800 {
801         int i, n, num_free;
802         u_int in_ptr, out_ptr;
803         struct dataseg * ds;
804         struct scatterlist *sg;
805         struct Command_Entry *cmd;
806         struct Continuation_Entry *cont;
807         struct Scsi_Host *host;
808         struct isp1020_hostdata *hostdata;
809         dma_addr_t      dma_addr;
810
811         ENTER("isp1020_queuecommand");
812
813         host = Cmnd->device->host;
814         hostdata = (struct isp1020_hostdata *) host->hostdata;
815         Cmnd->scsi_done = done;
816
817         DEBUG(isp1020_print_scsi_cmd(Cmnd));
818
819         out_ptr = isp_inw(host, + MBOX4);
820         in_ptr  = hostdata->req_in_ptr;
821
822         DEBUG(printk("qlogicisp : request queue depth %d\n",
823                      REQ_QUEUE_DEPTH(in_ptr, out_ptr)));
824
825         cmd = (struct Command_Entry *) &hostdata->req_cpu[in_ptr];
826         in_ptr = (in_ptr + 1) & QLOGICISP_REQ_QUEUE_LEN;
827         if (in_ptr == out_ptr) {
828                 printk("qlogicisp : request queue overflow\n");
829                 return 1;
830         }
831
832         if (hostdata->send_marker) {
833                 struct Marker_Entry *marker;
834
835                 TRACE("queue marker", in_ptr, 0);
836
837                 DEBUG(printk("qlogicisp : adding marker entry\n"));
838                 marker = (struct Marker_Entry *) cmd;
839                 memset(marker, 0, sizeof(struct Marker_Entry));
840
841                 marker->hdr.entry_type = ENTRY_MARKER;
842                 marker->hdr.entry_cnt = 1;
843                 marker->modifier = SYNC_ALL;
844
845                 hostdata->send_marker = 0;
846
847                 if (((in_ptr + 1) & QLOGICISP_REQ_QUEUE_LEN) == out_ptr) {
848                         isp_outw(in_ptr, host, MBOX4);
849                         hostdata->req_in_ptr = in_ptr;
850                         printk("qlogicisp : request queue overflow\n");
851                         return 1;
852                 }
853                 cmd = (struct Command_Entry *) &hostdata->req_cpu[in_ptr];
854                 in_ptr = (in_ptr + 1) & QLOGICISP_REQ_QUEUE_LEN;
855         }
856
857         TRACE("queue command", in_ptr, Cmnd);
858
859         memset(cmd, 0, sizeof(struct Command_Entry));
860
861         cmd->hdr.entry_type = ENTRY_COMMAND;
862         cmd->hdr.entry_cnt = 1;
863
864         cmd->target_lun = Cmnd->device->lun;
865         cmd->target_id = Cmnd->device->id;
866         cmd->cdb_length = cpu_to_le16(Cmnd->cmd_len);
867         cmd->control_flags = cpu_to_le16(CFLAG_READ | CFLAG_WRITE);
868         cmd->time_out = cpu_to_le16(30);
869
870         memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
871
872         if (Cmnd->use_sg) {
873                 int sg_count;
874
875                 sg = (struct scatterlist *) Cmnd->request_buffer;
876                 ds = cmd->dataseg;
877
878                 sg_count = pci_map_sg(hostdata->pci_dev, sg, Cmnd->use_sg,
879                                       Cmnd->sc_data_direction);
880
881                 cmd->segment_cnt = cpu_to_le16(sg_count);
882
883                 /* fill in first four sg entries: */
884                 n = sg_count;
885                 if (n > IOCB_SEGS)
886                         n = IOCB_SEGS;
887                 for (i = 0; i < n; i++) {
888                         dma_addr = sg_dma_address(sg);
889                         ds[i].d_base  = cpu_to_le32((u32) dma_addr);
890 #ifdef CONFIG_QL_ISP_A64
891                         ds[i].d_base_hi = cpu_to_le32((u32) (dma_addr>>32));
892 #endif /* CONFIG_QL_ISP_A64 */
893                         ds[i].d_count = cpu_to_le32(sg_dma_len(sg));
894                         ++sg;
895                 }
896                 sg_count -= IOCB_SEGS;
897
898                 while (sg_count > 0) {
899                         ++cmd->hdr.entry_cnt;
900                         cont = (struct Continuation_Entry *)
901                                 &hostdata->req_cpu[in_ptr];
902                         in_ptr = (in_ptr + 1) & QLOGICISP_REQ_QUEUE_LEN;
903                         if (in_ptr == out_ptr) {
904                                 printk("isp1020: unexpected request queue "
905                                        "overflow\n");
906                                 return 1;
907                         }
908                         TRACE("queue continuation", in_ptr, 0);
909                         cont->hdr.entry_type = ENTRY_CONTINUATION;
910                         cont->hdr.entry_cnt  = 0;
911                         cont->hdr.sys_def_1  = 0;
912                         cont->hdr.flags      = 0;
913 #ifndef CONFIG_QL_ISP_A64
914                         cont->reserved = 0;
915 #endif
916                         ds = cont->dataseg;
917                         n = sg_count;
918                         if (n > CONTINUATION_SEGS)
919                                 n = CONTINUATION_SEGS;
920                         for (i = 0; i < n; ++i) {
921                                 dma_addr = sg_dma_address(sg);
922                                 ds[i].d_base = cpu_to_le32((u32) dma_addr);
923 #ifdef CONFIG_QL_ISP_A64
924                                 ds[i].d_base_hi = cpu_to_le32((u32)(dma_addr>>32));
925 #endif /* CONFIG_QL_ISP_A64 */
926                                 ds[i].d_count = cpu_to_le32(sg_dma_len(sg));
927                                 ++sg;
928                         }
929                         sg_count -= n;
930                 }
931         } else if (Cmnd->request_bufflen) {
932                 /*Cmnd->SCp.ptr = (char *)(unsigned long)*/
933                 dma_addr = pci_map_single(hostdata->pci_dev,
934                                        Cmnd->request_buffer,
935                                        Cmnd->request_bufflen,
936                                        Cmnd->sc_data_direction);
937                 Cmnd->SCp.ptr = (char *)(unsigned long) dma_addr;
938
939                 cmd->dataseg[0].d_base =
940                         cpu_to_le32((u32) dma_addr);
941 #ifdef CONFIG_QL_ISP_A64
942                 cmd->dataseg[0].d_base_hi =
943                         cpu_to_le32((u32) (dma_addr>>32));
944 #endif /* CONFIG_QL_ISP_A64 */
945                 cmd->dataseg[0].d_count =
946                         cpu_to_le32((u32)Cmnd->request_bufflen);
947                 cmd->segment_cnt = cpu_to_le16(1);
948         } else {
949                 cmd->dataseg[0].d_base = 0;
950 #ifdef CONFIG_QL_ISP_A64
951                 cmd->dataseg[0].d_base_hi = 0;
952 #endif /* CONFIG_QL_ISP_A64 */
953                 cmd->dataseg[0].d_count = 0;
954                 cmd->segment_cnt = cpu_to_le16(1); /* Shouldn't this be 0? */
955         }
956
957         /* Committed, record Scsi_Cmd so we can find it later. */
958         cmd->handle = in_ptr;
959         hostdata->cmd_slots[in_ptr] = Cmnd;
960
961         isp_outw(in_ptr, host, MBOX4);
962         hostdata->req_in_ptr = in_ptr;
963
964         num_free = QLOGICISP_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr);
965         host->can_queue = host->host_busy + num_free;
966         host->sg_tablesize = QLOGICISP_MAX_SG(num_free);
967
968         LEAVE("isp1020_queuecommand");
969
970         return 0;
971 }
972
973
974 #define ASYNC_EVENT_INTERRUPT   0x01
975
976 irqreturn_t do_isp1020_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
977 {
978         struct Scsi_Host *host = dev_id;
979         unsigned long flags;
980
981         spin_lock_irqsave(host->host_lock, flags);
982         isp1020_intr_handler(irq, dev_id, regs);
983         spin_unlock_irqrestore(host->host_lock, flags);
984
985         return IRQ_HANDLED;
986 }
987
988 void isp1020_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
989 {
990         Scsi_Cmnd *Cmnd;
991         struct Status_Entry *sts;
992         struct Scsi_Host *host = dev_id;
993         struct isp1020_hostdata *hostdata;
994         u_int in_ptr, out_ptr;
995         u_short status;
996
997         ENTER_INTR("isp1020_intr_handler");
998
999         hostdata = (struct isp1020_hostdata *) host->hostdata;
1000
1001         DEBUG_INTR(printk("qlogicisp : interrupt on line %d\n", irq));
1002
1003         if (!(isp_inw(host, PCI_INTF_STS) & 0x04)) {
1004                 /* spurious interrupts can happen legally */
1005                 DEBUG_INTR(printk("qlogicisp: got spurious interrupt\n"));
1006                 return;
1007         }
1008         in_ptr = isp_inw(host, MBOX5);
1009         isp_outw(HCCR_CLEAR_RISC_INTR, host, HOST_HCCR);
1010
1011         if ((isp_inw(host, PCI_SEMAPHORE) & ASYNC_EVENT_INTERRUPT)) {
1012                 status = isp_inw(host, MBOX0);
1013
1014                 DEBUG_INTR(printk("qlogicisp : mbox completion status: %x\n",
1015                                   status));
1016
1017                 switch (status) {
1018                       case ASYNC_SCSI_BUS_RESET:
1019                       case EXECUTION_TIMEOUT_RESET:
1020                         hostdata->send_marker = 1;
1021                         break;
1022                       case INVALID_COMMAND:
1023                       case HOST_INTERFACE_ERROR:
1024                       case COMMAND_ERROR:
1025                       case COMMAND_PARAM_ERROR:
1026                         printk("qlogicisp : bad mailbox return status\n");
1027                         break;
1028                 }
1029                 isp_outw(0x0, host, PCI_SEMAPHORE);
1030         }
1031         out_ptr = hostdata->res_out_ptr;
1032
1033         DEBUG_INTR(printk("qlogicisp : response queue update\n"));
1034         DEBUG_INTR(printk("qlogicisp : response queue depth %d\n",
1035                           QUEUE_DEPTH(in_ptr, out_ptr, RES_QUEUE_LEN)));
1036
1037         while (out_ptr != in_ptr) {
1038                 u_int cmd_slot;
1039
1040                 sts = (struct Status_Entry *) &hostdata->res_cpu[out_ptr];
1041                 out_ptr = (out_ptr + 1) & RES_QUEUE_LEN;
1042
1043                 cmd_slot = sts->handle;
1044                 Cmnd = hostdata->cmd_slots[cmd_slot];
1045                 hostdata->cmd_slots[cmd_slot] = NULL;
1046
1047                 TRACE("done", out_ptr, Cmnd);
1048
1049                 if (le16_to_cpu(sts->completion_status) == CS_RESET_OCCURRED
1050                     || le16_to_cpu(sts->completion_status) == CS_ABORTED
1051                     || (le16_to_cpu(sts->status_flags) & STF_BUS_RESET))
1052                         hostdata->send_marker = 1;
1053
1054                 if (le16_to_cpu(sts->state_flags) & SF_GOT_SENSE)
1055                         memcpy(Cmnd->sense_buffer, sts->req_sense_data,
1056                                sizeof(Cmnd->sense_buffer));
1057
1058                 DEBUG_INTR(isp1020_print_status_entry(sts));
1059
1060                 if (sts->hdr.entry_type == ENTRY_STATUS)
1061                         Cmnd->result = isp1020_return_status(sts);
1062                 else
1063                         Cmnd->result = DID_ERROR << 16;
1064
1065                 if (Cmnd->use_sg)
1066                         pci_unmap_sg(hostdata->pci_dev,
1067                                      (struct scatterlist *)Cmnd->buffer,
1068                                      Cmnd->use_sg,
1069                                      Cmnd->sc_data_direction);
1070                 else if (Cmnd->request_bufflen)
1071                         pci_unmap_single(hostdata->pci_dev,
1072 #ifdef CONFIG_QL_ISP_A64
1073                                          (dma_addr_t)((long)Cmnd->SCp.ptr),
1074 #else
1075                                          (u32)((long)Cmnd->SCp.ptr),
1076 #endif
1077                                          Cmnd->request_bufflen,
1078                                          Cmnd->sc_data_direction);
1079
1080                 isp_outw(out_ptr, host, MBOX5);
1081                 (*Cmnd->scsi_done)(Cmnd);
1082         }
1083         hostdata->res_out_ptr = out_ptr;
1084
1085         LEAVE_INTR("isp1020_intr_handler");
1086 }
1087
1088
1089 static int isp1020_return_status(struct Status_Entry *sts)
1090 {
1091         int host_status = DID_ERROR;
1092 #if DEBUG_ISP1020_INTR
1093         static char *reason[] = {
1094                 "DID_OK",
1095                 "DID_NO_CONNECT",
1096                 "DID_BUS_BUSY",
1097                 "DID_TIME_OUT",
1098                 "DID_BAD_TARGET",
1099                 "DID_ABORT",
1100                 "DID_PARITY",
1101                 "DID_ERROR",
1102                 "DID_RESET",
1103                 "DID_BAD_INTR"
1104         };
1105 #endif /* DEBUG_ISP1020_INTR */
1106
1107         ENTER("isp1020_return_status");
1108
1109         DEBUG(printk("qlogicisp : completion status = 0x%04x\n",
1110                      le16_to_cpu(sts->completion_status)));
1111
1112         switch(le16_to_cpu(sts->completion_status)) {
1113               case CS_COMPLETE:
1114                 host_status = DID_OK;
1115                 break;
1116               case CS_INCOMPLETE:
1117                 if (!(le16_to_cpu(sts->state_flags) & SF_GOT_BUS))
1118                         host_status = DID_NO_CONNECT;
1119                 else if (!(le16_to_cpu(sts->state_flags) & SF_GOT_TARGET))
1120                         host_status = DID_BAD_TARGET;
1121                 else if (!(le16_to_cpu(sts->state_flags) & SF_SENT_CDB))
1122                         host_status = DID_ERROR;
1123                 else if (!(le16_to_cpu(sts->state_flags) & SF_TRANSFERRED_DATA))
1124                         host_status = DID_ERROR;
1125                 else if (!(le16_to_cpu(sts->state_flags) & SF_GOT_STATUS))
1126                         host_status = DID_ERROR;
1127                 else if (!(le16_to_cpu(sts->state_flags) & SF_GOT_SENSE))
1128                         host_status = DID_ERROR;
1129                 break;
1130               case CS_DMA_ERROR:
1131               case CS_TRANSPORT_ERROR:
1132                 host_status = DID_ERROR;
1133                 break;
1134               case CS_RESET_OCCURRED:
1135                 host_status = DID_RESET;
1136                 break;
1137               case CS_ABORTED:
1138                 host_status = DID_ABORT;
1139                 break;
1140               case CS_TIMEOUT:
1141                 host_status = DID_TIME_OUT;
1142                 break;
1143               case CS_DATA_OVERRUN:
1144               case CS_COMMAND_OVERRUN:
1145               case CS_STATUS_OVERRUN:
1146               case CS_BAD_MESSAGE:
1147               case CS_NO_MESSAGE_OUT:
1148               case CS_EXT_ID_FAILED:
1149               case CS_IDE_MSG_FAILED:
1150               case CS_ABORT_MSG_FAILED:
1151               case CS_NOP_MSG_FAILED:
1152               case CS_PARITY_ERROR_MSG_FAILED:
1153               case CS_DEVICE_RESET_MSG_FAILED:
1154               case CS_ID_MSG_FAILED:
1155               case CS_UNEXP_BUS_FREE:
1156                 host_status = DID_ERROR;
1157                 break;
1158               case CS_DATA_UNDERRUN:
1159                 host_status = DID_OK;
1160                 break;
1161               default:
1162                 printk("qlogicisp : unknown completion status 0x%04x\n",
1163                        le16_to_cpu(sts->completion_status));
1164                 host_status = DID_ERROR;
1165                 break;
1166         }
1167
1168         DEBUG_INTR(printk("qlogicisp : host status (%s) scsi status %x\n",
1169                           reason[host_status], le16_to_cpu(sts->scsi_status)));
1170
1171         LEAVE("isp1020_return_status");
1172
1173         return (le16_to_cpu(sts->scsi_status) & STATUS_MASK) | (host_status << 16);
1174 }
1175
1176
1177 static int isp1020_biosparam(struct scsi_device *sdev, struct block_device *n,
1178                 sector_t capacity, int ip[])
1179 {
1180         int size = capacity;
1181
1182         ENTER("isp1020_biosparam");
1183
1184         ip[0] = 64;
1185         ip[1] = 32;
1186         ip[2] = size >> 11;
1187         if (ip[2] > 1024) {
1188                 ip[0] = 255;
1189                 ip[1] = 63;
1190                 ip[2] = size / (ip[0] * ip[1]);
1191 #if 0
1192                 if (ip[2] > 1023)
1193                         ip[2] = 1023;
1194 #endif                  
1195         }
1196
1197         LEAVE("isp1020_biosparam");
1198
1199         return 0;
1200 }
1201
1202
1203 static int isp1020_reset_hardware(struct Scsi_Host *host)
1204 {
1205         u_short param[6];
1206         int loop_count;
1207
1208         ENTER("isp1020_reset_hardware");
1209
1210         isp_outw(ISP_RESET, host, PCI_INTF_CTL);
1211         udelay(100);
1212         isp_outw(HCCR_RESET, host, HOST_HCCR);
1213         udelay(100);
1214         isp_outw(HCCR_RELEASE, host, HOST_HCCR);
1215         isp_outw(HCCR_BIOS_DISABLE, host, HOST_HCCR);
1216
1217         loop_count = DEFAULT_LOOP_COUNT;
1218         while (--loop_count && isp_inw(host, HOST_HCCR) == RISC_BUSY) {
1219                 barrier();
1220                 cpu_relax();
1221         }
1222         if (!loop_count)
1223                 printk("qlogicisp: reset_hardware loop timeout\n");
1224
1225         isp_outw(0, host, ISP_CFG1);
1226
1227 #if DEBUG_ISP1020
1228         printk("qlogicisp : mbox 0 0x%04x \n", isp_inw(host, MBOX0));
1229         printk("qlogicisp : mbox 1 0x%04x \n", isp_inw(host, MBOX1));
1230         printk("qlogicisp : mbox 2 0x%04x \n", isp_inw(host, MBOX2));
1231         printk("qlogicisp : mbox 3 0x%04x \n", isp_inw(host, MBOX3));
1232         printk("qlogicisp : mbox 4 0x%04x \n", isp_inw(host, MBOX4));
1233         printk("qlogicisp : mbox 5 0x%04x \n", isp_inw(host, MBOX5));
1234 #endif /* DEBUG_ISP1020 */
1235
1236         param[0] = MBOX_NO_OP;
1237         isp1020_mbox_command(host, param);
1238         if (param[0] != MBOX_COMMAND_COMPLETE) {
1239                 printk("qlogicisp : NOP test failed\n");
1240                 return 1;
1241         }
1242
1243         DEBUG(printk("qlogicisp : loading risc ram\n"));
1244
1245 #if RELOAD_FIRMWARE
1246         for (loop_count = 0; loop_count < risc_code_length01; loop_count++) {
1247                 param[0] = MBOX_WRITE_RAM_WORD;
1248                 param[1] = risc_code_addr01 + loop_count;
1249                 param[2] = risc_code01[loop_count];
1250                 isp1020_mbox_command(host, param);
1251                 if (param[0] != MBOX_COMMAND_COMPLETE) {
1252                         printk("qlogicisp : firmware load failure at %d\n",
1253                             loop_count);
1254                         return 1;
1255                 }
1256         }
1257 #endif /* RELOAD_FIRMWARE */
1258
1259         DEBUG(printk("qlogicisp : verifying checksum\n"));
1260
1261         param[0] = MBOX_VERIFY_CHECKSUM;
1262         param[1] = risc_code_addr01;
1263
1264         isp1020_mbox_command(host, param);
1265
1266         if (param[0] != MBOX_COMMAND_COMPLETE) {
1267                 printk("qlogicisp : ram checksum failure\n");
1268                 return 1;
1269         }
1270
1271         DEBUG(printk("qlogicisp : executing firmware\n"));
1272
1273         param[0] = MBOX_EXEC_FIRMWARE;
1274         param[1] = risc_code_addr01;
1275
1276         isp1020_mbox_command(host, param);
1277
1278         param[0] = MBOX_ABOUT_FIRMWARE;
1279
1280         isp1020_mbox_command(host, param);
1281
1282         if (param[0] != MBOX_COMMAND_COMPLETE) {
1283                 printk("qlogicisp : about firmware failure\n");
1284                 return 1;
1285         }
1286
1287         DEBUG(printk("qlogicisp : firmware major revision %d\n", param[1]));
1288         DEBUG(printk("qlogicisp : firmware minor revision %d\n", param[2]));
1289
1290         LEAVE("isp1020_reset_hardware");
1291
1292         return 0;
1293 }
1294
1295
1296 static int isp1020_init(struct Scsi_Host *sh)
1297 {
1298         u_long io_base, mem_base, io_flags, mem_flags;
1299         struct isp1020_hostdata *hostdata;
1300         u_char revision;
1301         u_int irq;
1302         u_short command;
1303         struct pci_dev *pdev;
1304
1305         ENTER("isp1020_init");
1306
1307         hostdata = (struct isp1020_hostdata *) sh->hostdata;
1308         pdev = hostdata->pci_dev;
1309
1310         if (pci_read_config_word(pdev, PCI_COMMAND, &command)
1311             || pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision))
1312         {
1313                 printk("qlogicisp : error reading PCI configuration\n");
1314                 return 1;
1315         }
1316
1317         io_base = pci_resource_start(pdev, 0);
1318         mem_base = pci_resource_start(pdev, 1);
1319         io_flags = pci_resource_flags(pdev, 0);
1320         mem_flags = pci_resource_flags(pdev, 1);
1321         irq = pdev->irq;
1322
1323         if (pdev->vendor != PCI_VENDOR_ID_QLOGIC) {
1324                 printk("qlogicisp : 0x%04x is not QLogic vendor ID\n",
1325                        pdev->vendor);
1326                 return 1;
1327         }
1328
1329         if (pdev->device != PCI_DEVICE_ID_QLOGIC_ISP1020) {
1330                 printk("qlogicisp : 0x%04x does not match ISP1020 device id\n",
1331                        pdev->device);
1332                 return 1;
1333         }
1334
1335 #ifdef __alpha__
1336         /* Force ALPHA to use bus I/O and not bus MEM.
1337            This is to avoid having to use HAE_MEM registers,
1338            which is broken on some platforms and with SMP.  */
1339         command &= ~PCI_COMMAND_MEMORY; 
1340 #endif
1341
1342         sh->io_port = io_base;
1343
1344         if (!request_region(sh->io_port, 0xff, "qlogicisp")) {
1345                 printk("qlogicisp : i/o region 0x%lx-0x%lx already "
1346                        "in use\n",
1347                        sh->io_port, sh->io_port + 0xff);
1348                 return 1;
1349         }
1350
1351         if ((command & PCI_COMMAND_MEMORY) &&
1352             ((mem_flags & 1) == 0)) {
1353                 hostdata->memaddr = ioremap(mem_base, PAGE_SIZE);
1354                 if (!hostdata->memaddr) {
1355                         printk("qlogicisp : i/o remapping failed.\n");
1356                         goto out_release;
1357                 }
1358         } else {
1359                 if (command & PCI_COMMAND_IO && (io_flags & 3) != 1) {
1360                         printk("qlogicisp : i/o mapping is disabled\n");
1361                         goto out_release;
1362                 }
1363                 hostdata->memaddr = NULL; /* zero to signify no i/o mapping */
1364                 mem_base = 0;
1365         }
1366
1367         if (revision != ISP1020_REV_ID)
1368                 printk("qlogicisp : new isp1020 revision ID (%d)\n", revision);
1369
1370         if (isp_inw(sh,  PCI_ID_LOW) != PCI_VENDOR_ID_QLOGIC
1371             || isp_inw(sh, PCI_ID_HIGH) != PCI_DEVICE_ID_QLOGIC_ISP1020)
1372         {
1373                 printk("qlogicisp : can't decode %s address space 0x%lx\n",
1374                        (io_base ? "I/O" : "MEM"),
1375                        (io_base ? io_base : mem_base));
1376                 goto out_unmap;
1377         }
1378
1379         hostdata->revision = revision;
1380
1381         sh->irq = irq;
1382         sh->max_id = MAX_TARGETS;
1383         sh->max_lun = MAX_LUNS;
1384
1385         hostdata->res_cpu = pci_alloc_consistent(hostdata->pci_dev,
1386                                                  QSIZE(RES_QUEUE_LEN),
1387                                                  &hostdata->res_dma);
1388         if (hostdata->res_cpu == NULL) {
1389                 printk("qlogicisp : can't allocate response queue\n");
1390                 goto out_unmap;
1391         }
1392
1393         hostdata->req_cpu = pci_alloc_consistent(hostdata->pci_dev,
1394                                                  QSIZE(QLOGICISP_REQ_QUEUE_LEN),
1395                                                  &hostdata->req_dma);
1396         if (hostdata->req_cpu == NULL) {
1397                 pci_free_consistent(hostdata->pci_dev,
1398                                     QSIZE(RES_QUEUE_LEN),
1399                                     hostdata->res_cpu,
1400                                     hostdata->res_dma);
1401                 printk("qlogicisp : can't allocate request queue\n");
1402                 goto out_unmap;
1403         }
1404
1405         pci_set_master(pdev);
1406
1407         LEAVE("isp1020_init");
1408
1409         return 0;
1410
1411 out_unmap:
1412         iounmap(hostdata->memaddr);
1413 out_release:
1414         release_region(sh->io_port, 0xff);
1415         return 1;
1416 }
1417
1418
1419 #if USE_NVRAM_DEFAULTS
1420
1421 static int isp1020_get_defaults(struct Scsi_Host *host)
1422 {
1423         int i;
1424         u_short value;
1425         struct isp1020_hostdata *hostdata =
1426                 (struct isp1020_hostdata *) host->hostdata;
1427
1428         ENTER("isp1020_get_defaults");
1429
1430         if (!isp1020_verify_nvram(host)) {
1431                 printk("qlogicisp : nvram checksum failure\n");
1432                 printk("qlogicisp : attempting to use default parameters\n");
1433                 return isp1020_set_defaults(host);
1434         }
1435
1436         value = isp1020_read_nvram_word(host, 2);
1437         hostdata->host_param.fifo_threshold = (value >> 8) & 0x03;
1438         hostdata->host_param.host_adapter_enable = (value >> 11) & 0x01;
1439         hostdata->host_param.initiator_scsi_id = (value >> 12) & 0x0f;
1440
1441         value = isp1020_read_nvram_word(host, 3);
1442         hostdata->host_param.bus_reset_delay = value & 0xff;
1443         hostdata->host_param.retry_count = value >> 8;
1444
1445         value = isp1020_read_nvram_word(host, 4);
1446         hostdata->host_param.retry_delay = value & 0xff;
1447         hostdata->host_param.async_data_setup_time = (value >> 8) & 0x0f;
1448         hostdata->host_param.req_ack_active_negation = (value >> 12) & 0x01;
1449         hostdata->host_param.data_line_active_negation = (value >> 13) & 0x01;
1450         hostdata->host_param.data_dma_burst_enable = (value >> 14) & 0x01;
1451         hostdata->host_param.command_dma_burst_enable = (value >> 15);
1452
1453         value = isp1020_read_nvram_word(host, 5);
1454         hostdata->host_param.tag_aging = value & 0xff;
1455
1456         value = isp1020_read_nvram_word(host, 6);
1457         hostdata->host_param.selection_timeout = value & 0xffff;
1458
1459         value = isp1020_read_nvram_word(host, 7);
1460         hostdata->host_param.max_queue_depth = value & 0xffff;
1461
1462 #if DEBUG_ISP1020_SETUP
1463         printk("qlogicisp : fifo threshold=%d\n",
1464                hostdata->host_param.fifo_threshold);
1465         printk("qlogicisp : initiator scsi id=%d\n",
1466                hostdata->host_param.initiator_scsi_id);
1467         printk("qlogicisp : bus reset delay=%d\n",
1468                hostdata->host_param.bus_reset_delay);
1469         printk("qlogicisp : retry count=%d\n",
1470                hostdata->host_param.retry_count);
1471         printk("qlogicisp : retry delay=%d\n",
1472                hostdata->host_param.retry_delay);
1473         printk("qlogicisp : async data setup time=%d\n",
1474                hostdata->host_param.async_data_setup_time);
1475         printk("qlogicisp : req/ack active negation=%d\n",
1476                hostdata->host_param.req_ack_active_negation);
1477         printk("qlogicisp : data line active negation=%d\n",
1478                hostdata->host_param.data_line_active_negation);
1479         printk("qlogicisp : data DMA burst enable=%d\n",
1480                hostdata->host_param.data_dma_burst_enable);
1481         printk("qlogicisp : command DMA burst enable=%d\n",
1482                hostdata->host_param.command_dma_burst_enable);
1483         printk("qlogicisp : tag age limit=%d\n",
1484                hostdata->host_param.tag_aging);
1485         printk("qlogicisp : selection timeout limit=%d\n",
1486                hostdata->host_param.selection_timeout);
1487         printk("qlogicisp : max queue depth=%d\n",
1488                hostdata->host_param.max_queue_depth);
1489 #endif /* DEBUG_ISP1020_SETUP */
1490
1491         for (i = 0; i < MAX_TARGETS; i++) {
1492
1493                 value = isp1020_read_nvram_word(host, 14 + i * 3);
1494                 hostdata->dev_param[i].device_flags = value & 0xff;
1495                 hostdata->dev_param[i].execution_throttle = value >> 8;
1496
1497                 value = isp1020_read_nvram_word(host, 15 + i * 3);
1498                 hostdata->dev_param[i].synchronous_period = value & 0xff;
1499                 hostdata->dev_param[i].synchronous_offset = (value >> 8) & 0x0f;
1500                 hostdata->dev_param[i].device_enable = (value >> 12) & 0x01;
1501
1502 #if DEBUG_ISP1020_SETUP
1503                 printk("qlogicisp : target 0x%02x\n", i);
1504                 printk("qlogicisp :     device flags=0x%02x\n",
1505                        hostdata->dev_param[i].device_flags);
1506                 printk("qlogicisp :     execution throttle=%d\n",
1507                        hostdata->dev_param[i].execution_throttle);
1508                 printk("qlogicisp :     synchronous period=%d\n",
1509                        hostdata->dev_param[i].synchronous_period);
1510                 printk("qlogicisp :     synchronous offset=%d\n",
1511                        hostdata->dev_param[i].synchronous_offset);
1512                 printk("qlogicisp :     device enable=%d\n",
1513                        hostdata->dev_param[i].device_enable);
1514 #endif /* DEBUG_ISP1020_SETUP */
1515         }
1516
1517         LEAVE("isp1020_get_defaults");
1518
1519         return 0;
1520 }
1521
1522
1523 #define ISP1020_NVRAM_LEN       0x40
1524 #define ISP1020_NVRAM_SIG1      0x5349
1525 #define ISP1020_NVRAM_SIG2      0x2050
1526
1527 static int isp1020_verify_nvram(struct Scsi_Host *host)
1528 {
1529         int     i;
1530         u_short value;
1531         u_char checksum = 0;
1532
1533         for (i = 0; i < ISP1020_NVRAM_LEN; i++) {
1534                 value = isp1020_read_nvram_word(host, i);
1535
1536                 switch (i) {
1537                       case 0:
1538                         if (value != ISP1020_NVRAM_SIG1) return 0;
1539                         break;
1540                       case 1:
1541                         if (value != ISP1020_NVRAM_SIG2) return 0;
1542                         break;
1543                       case 2:
1544                         if ((value & 0xff) != 0x02) return 0;
1545                         break;
1546                 }
1547                 checksum += value & 0xff;
1548                 checksum += value >> 8;
1549         }
1550
1551         return (checksum == 0);
1552 }
1553
1554 #define NVRAM_DELAY() udelay(2) /* 2 microsecond delay */
1555
1556
1557 u_short isp1020_read_nvram_word(struct Scsi_Host *host, u_short byte)
1558 {
1559         int i;
1560         u_short value, output, input;
1561
1562         byte &= 0x3f; byte |= 0x0180;
1563
1564         for (i = 8; i >= 0; i--) {
1565                 output = ((byte >> i) & 0x1) ? 0x4 : 0x0;
1566                 isp_outw(output | 0x2, host, PCI_NVRAM); NVRAM_DELAY();
1567                 isp_outw(output | 0x3, host, PCI_NVRAM); NVRAM_DELAY();
1568                 isp_outw(output | 0x2, host, PCI_NVRAM); NVRAM_DELAY();
1569         }
1570
1571         for (i = 0xf, value = 0; i >= 0; i--) {
1572                 value <<= 1;
1573                 isp_outw(0x3, host, PCI_NVRAM); NVRAM_DELAY();
1574                 input = isp_inw(host, PCI_NVRAM); NVRAM_DELAY();
1575                 isp_outw(0x2, host, PCI_NVRAM); NVRAM_DELAY();
1576                 if (input & 0x8) value |= 1;
1577         }
1578
1579         isp_outw(0x0, host, PCI_NVRAM); NVRAM_DELAY();
1580
1581         return value;
1582 }
1583
1584 #endif /* USE_NVRAM_DEFAULTS */
1585
1586
1587 static int isp1020_set_defaults(struct Scsi_Host *host)
1588 {
1589         struct isp1020_hostdata *hostdata =
1590                 (struct isp1020_hostdata *) host->hostdata;
1591         int i;
1592
1593         ENTER("isp1020_set_defaults");
1594
1595         hostdata->host_param.fifo_threshold = 2;
1596         hostdata->host_param.host_adapter_enable = 1;
1597         hostdata->host_param.initiator_scsi_id = 7;
1598         hostdata->host_param.bus_reset_delay = 3;
1599         hostdata->host_param.retry_count = 0;
1600         hostdata->host_param.retry_delay = 1;
1601         hostdata->host_param.async_data_setup_time = 6;
1602         hostdata->host_param.req_ack_active_negation = 1;
1603         hostdata->host_param.data_line_active_negation = 1;
1604         hostdata->host_param.data_dma_burst_enable = 1;
1605         hostdata->host_param.command_dma_burst_enable = 1;
1606         hostdata->host_param.tag_aging = 8;
1607         hostdata->host_param.selection_timeout = 250;
1608         hostdata->host_param.max_queue_depth = 256;
1609
1610         for (i = 0; i < MAX_TARGETS; i++) {
1611                 hostdata->dev_param[i].device_flags = 0xfd;
1612                 hostdata->dev_param[i].execution_throttle = 16;
1613                 hostdata->dev_param[i].synchronous_period = 25;
1614                 hostdata->dev_param[i].synchronous_offset = 12;
1615                 hostdata->dev_param[i].device_enable = 1;
1616         }
1617
1618         LEAVE("isp1020_set_defaults");
1619
1620         return 0;
1621 }
1622
1623
1624 static int isp1020_load_parameters(struct Scsi_Host *host)
1625 {
1626         int i, k;
1627 #ifdef CONFIG_QL_ISP_A64
1628         u_long queue_addr;
1629         u_short param[8];
1630 #else
1631         u_int queue_addr;
1632         u_short param[6];
1633 #endif
1634         u_short isp_cfg1, hwrev;
1635         struct isp1020_hostdata *hostdata =
1636                 (struct isp1020_hostdata *) host->hostdata;
1637
1638         ENTER("isp1020_load_parameters");
1639
1640         hwrev = isp_inw(host, ISP_CFG0) & ISP_CFG0_HWMSK;
1641         isp_cfg1 = ISP_CFG1_F64 | ISP_CFG1_BENAB;
1642         if (hwrev == ISP_CFG0_1040A) {
1643                 /* Busted fifo, says mjacob. */
1644                 isp_cfg1 &= ISP_CFG1_BENAB;
1645         }
1646
1647         isp_outw(isp_inw(host, ISP_CFG1) | isp_cfg1, host, ISP_CFG1);
1648         isp_outw(isp_inw(host, CDMA_CONF) | DMA_CONF_BENAB, host, CDMA_CONF);
1649         isp_outw(isp_inw(host, DDMA_CONF) | DMA_CONF_BENAB, host, DDMA_CONF);
1650
1651         param[0] = MBOX_SET_INIT_SCSI_ID;
1652         param[1] = hostdata->host_param.initiator_scsi_id;
1653
1654         isp1020_mbox_command(host, param);
1655
1656         if (param[0] != MBOX_COMMAND_COMPLETE) {
1657                 printk("qlogicisp : set initiator id failure\n");
1658                 return 1;
1659         }
1660
1661         param[0] = MBOX_SET_RETRY_COUNT;
1662         param[1] = hostdata->host_param.retry_count;
1663         param[2] = hostdata->host_param.retry_delay;
1664
1665         isp1020_mbox_command(host, param);
1666
1667         if (param[0] != MBOX_COMMAND_COMPLETE) {
1668                 printk("qlogicisp : set retry count failure\n");
1669                 return 1;
1670         }
1671
1672         param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
1673         param[1] = hostdata->host_param.async_data_setup_time;
1674
1675         isp1020_mbox_command(host, param);
1676
1677         if (param[0] != MBOX_COMMAND_COMPLETE) {
1678                 printk("qlogicisp : async data setup time failure\n");
1679                 return 1;
1680         }
1681
1682         param[0] = MBOX_SET_ACTIVE_NEG_STATE;
1683         param[1] = (hostdata->host_param.req_ack_active_negation << 4)
1684                 | (hostdata->host_param.data_line_active_negation << 5);
1685
1686         isp1020_mbox_command(host, param);
1687
1688         if (param[0] != MBOX_COMMAND_COMPLETE) {
1689                 printk("qlogicisp : set active negation state failure\n");
1690                 return 1;
1691         }
1692
1693         param[0] = MBOX_SET_PCI_CONTROL_PARAMS;
1694         param[1] = hostdata->host_param.data_dma_burst_enable << 1;
1695         param[2] = hostdata->host_param.command_dma_burst_enable << 1;
1696
1697         isp1020_mbox_command(host, param);
1698
1699         if (param[0] != MBOX_COMMAND_COMPLETE) {
1700                 printk("qlogicisp : set pci control parameter failure\n");
1701                 return 1;
1702         }
1703
1704         param[0] = MBOX_SET_TAG_AGE_LIMIT;
1705         param[1] = hostdata->host_param.tag_aging;
1706
1707         isp1020_mbox_command(host, param);
1708
1709         if (param[0] != MBOX_COMMAND_COMPLETE) {
1710                 printk("qlogicisp : set tag age limit failure\n");
1711                 return 1;
1712         }
1713
1714         param[0] = MBOX_SET_SELECT_TIMEOUT;
1715         param[1] = hostdata->host_param.selection_timeout;
1716
1717         isp1020_mbox_command(host, param);
1718
1719         if (param[0] != MBOX_COMMAND_COMPLETE) {
1720                 printk("qlogicisp : set selection timeout failure\n");
1721                 return 1;
1722         }
1723
1724         for (i = 0; i < MAX_TARGETS; i++) {
1725
1726                 if (!hostdata->dev_param[i].device_enable)
1727                         continue;
1728
1729                 param[0] = MBOX_SET_TARGET_PARAMS;
1730                 param[1] = i << 8;
1731                 param[2] = hostdata->dev_param[i].device_flags << 8;
1732                 param[3] = (hostdata->dev_param[i].synchronous_offset << 8)
1733                         | hostdata->dev_param[i].synchronous_period;
1734
1735                 isp1020_mbox_command(host, param);
1736
1737                 if (param[0] != MBOX_COMMAND_COMPLETE) {
1738                         printk("qlogicisp : set target parameter failure\n");
1739                         return 1;
1740                 }
1741
1742                 for (k = 0; k < MAX_LUNS; k++) {
1743
1744                         param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
1745                         param[1] = (i << 8) | k;
1746                         param[2] = hostdata->host_param.max_queue_depth;
1747                         param[3] = hostdata->dev_param[i].execution_throttle;
1748
1749                         isp1020_mbox_command(host, param);
1750
1751                         if (param[0] != MBOX_COMMAND_COMPLETE) {
1752                                 printk("qlogicisp : set device queue "
1753                                        "parameter failure\n");
1754                                 return 1;
1755                         }
1756                 }
1757         }
1758
1759         queue_addr = hostdata->res_dma;
1760 #ifdef CONFIG_QL_ISP_A64
1761         param[0] = MBOX_CMD_INIT_RESPONSE_QUEUE_64;
1762 #else
1763         param[0] = MBOX_INIT_RES_QUEUE;
1764 #endif
1765         param[1] = RES_QUEUE_LEN + 1;
1766         param[2] = (u_short) (queue_addr >> 16);
1767         param[3] = (u_short) (queue_addr & 0xffff);
1768         param[4] = 0;
1769         param[5] = 0;
1770 #ifdef CONFIG_QL_ISP_A64
1771         param[6] = (u_short) (queue_addr >> 48);
1772         param[7] = (u_short) (queue_addr >> 32);
1773 #endif
1774
1775         isp1020_mbox_command(host, param);
1776
1777         if (param[0] != MBOX_COMMAND_COMPLETE) {
1778                 printk("qlogicisp : set response queue failure\n");
1779                 return 1;
1780         }
1781
1782         queue_addr = hostdata->req_dma;
1783 #ifdef CONFIG_QL_ISP_A64
1784         param[0] = MBOX_CMD_INIT_REQUEST_QUEUE_64;
1785 #else
1786         param[0] = MBOX_INIT_REQ_QUEUE;
1787 #endif
1788         param[1] = QLOGICISP_REQ_QUEUE_LEN + 1;
1789         param[2] = (u_short) (queue_addr >> 16);
1790         param[3] = (u_short) (queue_addr & 0xffff);
1791         param[4] = 0;
1792
1793 #ifdef CONFIG_QL_ISP_A64
1794         param[5] = 0;
1795         param[6] = (u_short) (queue_addr >> 48);
1796         param[7] = (u_short) (queue_addr >> 32);
1797 #endif
1798
1799         isp1020_mbox_command(host, param);
1800
1801         if (param[0] != MBOX_COMMAND_COMPLETE) {
1802                 printk("qlogicisp : set request queue failure\n");
1803                 return 1;
1804         }
1805
1806         LEAVE("isp1020_load_parameters");
1807
1808         return 0;
1809 }
1810
1811
1812 /*
1813  * currently, this is only called during initialization or abort/reset,
1814  * at which times interrupts are disabled, so polling is OK, I guess...
1815  */
1816 static int isp1020_mbox_command(struct Scsi_Host *host, u_short param[])
1817 {
1818         int loop_count;
1819
1820         if (mbox_param[param[0]] == 0)
1821                 return 1;
1822
1823         loop_count = DEFAULT_LOOP_COUNT;
1824         while (--loop_count && isp_inw(host, HOST_HCCR) & 0x0080) {
1825                 barrier();
1826                 cpu_relax();
1827         }
1828         if (!loop_count)
1829                 printk("qlogicisp: mbox_command loop timeout #1\n");
1830
1831         switch(mbox_param[param[0]] >> 4) {
1832               case 8: isp_outw(param[7], host, MBOX7);
1833               case 7: isp_outw(param[6], host, MBOX6);
1834               case 6: isp_outw(param[5], host, MBOX5);
1835               case 5: isp_outw(param[4], host, MBOX4);
1836               case 4: isp_outw(param[3], host, MBOX3);
1837               case 3: isp_outw(param[2], host, MBOX2);
1838               case 2: isp_outw(param[1], host, MBOX1);
1839               case 1: isp_outw(param[0], host, MBOX0);
1840         }
1841
1842         isp_outw(0x0, host, PCI_SEMAPHORE);
1843         isp_outw(HCCR_CLEAR_RISC_INTR, host, HOST_HCCR);
1844         isp_outw(HCCR_SET_HOST_INTR, host, HOST_HCCR);
1845
1846         loop_count = DEFAULT_LOOP_COUNT;
1847         while (--loop_count && !(isp_inw(host, PCI_INTF_STS) & 0x04)) {
1848                 barrier();
1849                 cpu_relax();
1850         }
1851         if (!loop_count)
1852                 printk("qlogicisp: mbox_command loop timeout #2\n");
1853
1854         loop_count = DEFAULT_LOOP_COUNT;
1855         while (--loop_count && isp_inw(host, MBOX0) == 0x04) {
1856                 barrier();
1857                 cpu_relax();
1858         }
1859         if (!loop_count)
1860                 printk("qlogicisp: mbox_command loop timeout #3\n");
1861
1862         switch(mbox_param[param[0]] & 0xf) {
1863               case 8: param[7] = isp_inw(host, MBOX7);
1864               case 7: param[6] = isp_inw(host, MBOX6);
1865               case 6: param[5] = isp_inw(host, MBOX5);
1866               case 5: param[4] = isp_inw(host, MBOX4);
1867               case 4: param[3] = isp_inw(host, MBOX3);
1868               case 3: param[2] = isp_inw(host, MBOX2);
1869               case 2: param[1] = isp_inw(host, MBOX1);
1870               case 1: param[0] = isp_inw(host, MBOX0);
1871         }
1872
1873         isp_outw(0x0, host, PCI_SEMAPHORE);
1874         isp_outw(HCCR_CLEAR_RISC_INTR, host, HOST_HCCR);
1875
1876         return 0;
1877 }
1878
1879
1880 #if DEBUG_ISP1020_INTR
1881
1882 void isp1020_print_status_entry(struct Status_Entry *status)
1883 {
1884         int i;
1885
1886         printk("qlogicisp : entry count = 0x%02x, type = 0x%02x, flags = 0x%02x\n",
1887                status->hdr.entry_cnt, status->hdr.entry_type, status->hdr.flags);
1888         printk("qlogicisp : scsi status = 0x%04x, completion status = 0x%04x\n",
1889                le16_to_cpu(status->scsi_status), le16_to_cpu(status->completion_status));
1890         printk("qlogicisp : state flags = 0x%04x, status flags = 0x%04x\n",
1891                le16_to_cpu(status->state_flags), le16_to_cpu(status->status_flags));
1892         printk("qlogicisp : time = 0x%04x, request sense length = 0x%04x\n",
1893                le16_to_cpu(status->time), le16_to_cpu(status->req_sense_len));
1894         printk("qlogicisp : residual transfer length = 0x%08x\n",
1895                le32_to_cpu(status->residual));
1896
1897         for (i = 0; i < le16_to_cpu(status->req_sense_len); i++)
1898                 printk("qlogicisp : sense data = 0x%02x\n", status->req_sense_data[i]);
1899 }
1900
1901 #endif /* DEBUG_ISP1020_INTR */
1902
1903
1904 #if DEBUG_ISP1020
1905
1906 void isp1020_print_scsi_cmd(Scsi_Cmnd *cmd)
1907 {
1908         int i;
1909
1910         printk("qlogicisp : target = 0x%02x, lun = 0x%02x, cmd_len = 0x%02x\n",
1911                cmd->target, cmd->lun, cmd->cmd_len);
1912         printk("qlogicisp : command = ");
1913         for (i = 0; i < cmd->cmd_len; i++)
1914                 printk("0x%02x ", cmd->cmnd[i]);
1915         printk("\n");
1916 }
1917
1918 #endif /* DEBUG_ISP1020 */
1919
1920 MODULE_LICENSE("GPL");
1921
1922 static Scsi_Host_Template driver_template = {
1923         .detect                 = isp1020_detect,
1924         .release                = isp1020_release,
1925         .info                   = isp1020_info, 
1926         .queuecommand           = isp1020_queuecommand,
1927         .bios_param             = isp1020_biosparam,
1928         .can_queue              = QLOGICISP_REQ_QUEUE_LEN,
1929         .this_id                = -1,
1930         .sg_tablesize           = QLOGICISP_MAX_SG(QLOGICISP_REQ_QUEUE_LEN),
1931         .cmd_per_lun            = 1,
1932         .use_clustering         = DISABLE_CLUSTERING,
1933 };
1934 #include "scsi_module.c"