[PATCH] parport: Kconfig dependency fixes
[linux-2.6] / drivers / scsi / qlogicfc.c
1 /*
2  * QLogic ISP2x00 SCSI-FCP
3  * Written by Erik H. Moe, ehm@cris.com
4  * Copyright 1995, Erik H. Moe
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2, or (at your option) any
9  * later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  */
16
17 /* Renamed and updated to 1.3.x by Michael Griffith <grif@cs.ucr.edu> */
18
19 /* This is a version of the isp1020 driver which was modified by
20  * Chris Loveland <cwl@iol.unh.edu> to support the isp2100 and isp2200
21  *
22  * Big endian support and dynamic DMA mapping added
23  * by Jakub Jelinek <jakub@redhat.com>.
24  *
25  * Conversion to final pci64 DMA interfaces
26  * by David S. Miller <davem@redhat.com>.
27  */
28
29 /*
30  * $Date: 1995/09/22 02:23:15 $
31  * $Revision: 0.5 $
32  *
33  * $Log: isp1020.c,v $
34  * Revision 0.5  1995/09/22  02:23:15  root
35  * do auto request sense
36  *
37  * Revision 0.4  1995/08/07  04:44:33  root
38  * supply firmware with driver.
39  * numerous bug fixes/general cleanup of code.
40  *
41  * Revision 0.3  1995/07/16  16:15:39  root
42  * added reset/abort code.
43  *
44  * Revision 0.2  1995/06/29  03:14:19  root
45  * fixed biosparam.
46  * added queue protocol.
47  *
48  * Revision 0.1  1995/06/25  01:55:45  root
49  * Initial release.
50  *
51  */
52
53 #include <linux/blkdev.h>
54 #include <linux/kernel.h>
55 #include <linux/string.h>
56 #include <linux/ioport.h>
57 #include <linux/sched.h>
58 #include <linux/types.h>
59 #include <linux/pci.h>
60 #include <linux/delay.h>
61 #include <linux/unistd.h>
62 #include <linux/spinlock.h>
63 #include <linux/interrupt.h>
64 #include <asm/io.h>
65 #include <asm/irq.h>
66 #include "scsi.h"
67 #include <scsi/scsi_host.h>
68
69 #define pci64_dma_hi32(a) ((u32) (0xffffffff & (((u64)(a))>>32)))
70 #define pci64_dma_lo32(a) ((u32) (0xffffffff & (((u64)(a)))))
71 #define pci64_dma_build(hi,lo) \
72         ((dma_addr_t)(((u64)(lo))|(((u64)(hi))<<32)))
73
74 /*
75  * With the qlogic interface, every queue slot can hold a SCSI
76  * command with up to 2 scatter/gather entries.  If we need more
77  * than 2 entries, continuation entries can be used that hold
78  * another 5 entries each.  Unlike for other drivers, this means
79  * that the maximum number of scatter/gather entries we can
80  * support at any given time is a function of the number of queue
81  * slots available.  That is, host->can_queue and host->sg_tablesize
82  * are dynamic and _not_ independent.  This all works fine because
83  * requests are queued serially and the scatter/gather limit is
84  * determined for each queue request anew.
85  */
86
87 #define DATASEGS_PER_COMMAND 2
88 #define DATASEGS_PER_CONT 5
89
90 #define QLOGICFC_REQ_QUEUE_LEN 255     /* must be power of two - 1 */
91 #define QLOGICFC_MAX_SG(ql)     (DATASEGS_PER_COMMAND + (((ql) > 0) ? DATASEGS_PER_CONT*((ql) - 1) : 0))
92 #define QLOGICFC_CMD_PER_LUN    8
93
94 /* Configuration section **************************************************** */
95
96 /* Set the following macro to 1 to reload the ISP2x00's firmware.  This is
97    version 1.17.30 of the isp2100's firmware and version 2.00.40 of the 
98    isp2200's firmware. 
99 */
100
101 #define USE_NVRAM_DEFAULTS      1
102
103 #define ISP2x00_PORTDB          1
104
105 /* Set the following to 1 to include fabric support, fabric support is 
106  * currently not as well tested as the other aspects of the driver */
107
108 #define ISP2x00_FABRIC          1
109
110 /*  Macros used for debugging */
111 #define DEBUG_ISP2x00           0
112 #define DEBUG_ISP2x00_INT       0
113 #define DEBUG_ISP2x00_INTR      0
114 #define DEBUG_ISP2x00_SETUP     0
115 #define DEBUG_ISP2x00_FABRIC    0
116 #define TRACE_ISP               0 
117
118
119 #define DEFAULT_LOOP_COUNT      1000000000
120
121 #define ISP_TIMEOUT (2*HZ)
122 /* End Configuration section ************************************************ */
123
124 #include <linux/module.h>
125
126 #if TRACE_ISP
127
128 #define TRACE_BUF_LEN   (32*1024)
129
130 struct {
131         u_long next;
132         struct {
133                 u_long time;
134                 u_int index;
135                 u_int addr;
136                 u_char *name;
137         } buf[TRACE_BUF_LEN];
138 } trace;
139
140 #define TRACE(w, i, a)                                          \
141 {                                                               \
142         unsigned long flags;                                    \
143                                                                 \
144         save_flags(flags);                                      \
145         cli();                                                  \
146         trace.buf[trace.next].name  = (w);                      \
147         trace.buf[trace.next].time  = jiffies;                  \
148         trace.buf[trace.next].index = (i);                      \
149         trace.buf[trace.next].addr  = (long) (a);               \
150         trace.next = (trace.next + 1) & (TRACE_BUF_LEN - 1);    \
151         restore_flags(flags);                                   \
152 }
153
154 #else
155 #define TRACE(w, i, a)
156 #endif
157
158 #if DEBUG_ISP2x00_FABRIC
159 #define DEBUG_FABRIC(x) x
160 #else
161 #define DEBUG_FABRIC(x)
162 #endif                          /* DEBUG_ISP2x00_FABRIC */
163
164
165 #if DEBUG_ISP2x00
166 #define ENTER(x)        printk("isp2x00 : entering %s()\n", x);
167 #define LEAVE(x)        printk("isp2x00 : leaving %s()\n", x);
168 #define DEBUG(x)        x
169 #else
170 #define ENTER(x)
171 #define LEAVE(x)
172 #define DEBUG(x)
173 #endif                          /* DEBUG_ISP2x00 */
174
175 #if DEBUG_ISP2x00_INTR
176 #define ENTER_INTR(x)   printk("isp2x00 : entering %s()\n", x);
177 #define LEAVE_INTR(x)   printk("isp2x00 : leaving %s()\n", x);
178 #define DEBUG_INTR(x)   x
179 #else
180 #define ENTER_INTR(x)
181 #define LEAVE_INTR(x)
182 #define DEBUG_INTR(x)
183 #endif                          /* DEBUG ISP2x00_INTR */
184
185
186 #define ISP2100_REV_ID1        1
187 #define ISP2100_REV_ID3        3
188 #define ISP2200_REV_ID5        5
189
190 /* host configuration and control registers */
191 #define HOST_HCCR       0xc0    /* host command and control */
192
193 /* pci bus interface registers */
194 #define FLASH_BIOS_ADDR 0x00
195 #define FLASH_BIOS_DATA 0x02
196 #define ISP_CTRL_STATUS 0x06    /* configuration register #1 */
197 #define PCI_INTER_CTL   0x08    /* pci interrupt control */
198 #define PCI_INTER_STS   0x0a    /* pci interrupt status */
199 #define PCI_SEMAPHORE   0x0c    /* pci semaphore */
200 #define PCI_NVRAM       0x0e    /* pci nvram interface */
201
202 /* mailbox registers */
203 #define MBOX0           0x10    /* mailbox 0 */
204 #define MBOX1           0x12    /* mailbox 1 */
205 #define MBOX2           0x14    /* mailbox 2 */
206 #define MBOX3           0x16    /* mailbox 3 */
207 #define MBOX4           0x18    /* mailbox 4 */
208 #define MBOX5           0x1a    /* mailbox 5 */
209 #define MBOX6           0x1c    /* mailbox 6 */
210 #define MBOX7           0x1e    /* mailbox 7 */
211
212 /* mailbox command complete status codes */
213 #define MBOX_COMMAND_COMPLETE           0x4000
214 #define INVALID_COMMAND                 0x4001
215 #define HOST_INTERFACE_ERROR            0x4002
216 #define TEST_FAILED                     0x4003
217 #define COMMAND_ERROR                   0x4005
218 #define COMMAND_PARAM_ERROR             0x4006
219 #define PORT_ID_USED                    0x4007
220 #define LOOP_ID_USED                    0x4008
221 #define ALL_IDS_USED                    0x4009
222
223 /* async event status codes */
224 #define RESET_DETECTED                  0x8001
225 #define SYSTEM_ERROR                    0x8002
226 #define REQUEST_TRANSFER_ERROR          0x8003
227 #define RESPONSE_TRANSFER_ERROR         0x8004
228 #define REQUEST_QUEUE_WAKEUP            0x8005
229 #define LIP_OCCURRED                     0x8010
230 #define LOOP_UP                         0x8011
231 #define LOOP_DOWN                       0x8012
232 #define LIP_RECEIVED                    0x8013
233 #define PORT_DB_CHANGED                 0x8014
234 #define CHANGE_NOTIFICATION             0x8015
235 #define SCSI_COMMAND_COMPLETE           0x8020
236 #define POINT_TO_POINT_UP               0x8030
237 #define CONNECTION_MODE                 0x8036
238
239 struct Entry_header {
240         u_char entry_type;
241         u_char entry_cnt;
242         u_char sys_def_1;
243         u_char flags;
244 };
245
246 /* entry header type commands */
247 #define ENTRY_COMMAND           0x19
248 #define ENTRY_CONTINUATION      0x0a
249
250 #define ENTRY_STATUS            0x03
251 #define ENTRY_MARKER            0x04
252
253
254 /* entry header flag definitions */
255 #define EFLAG_BUSY              2
256 #define EFLAG_BAD_HEADER        4
257 #define EFLAG_BAD_PAYLOAD       8
258
259 struct dataseg {
260         u_int d_base;
261         u_int d_base_hi;
262         u_int d_count;
263 };
264
265 struct Command_Entry {
266         struct Entry_header hdr;
267         u_int handle;
268         u_char target_lun;
269         u_char target_id;
270         u_short expanded_lun;
271         u_short control_flags;
272         u_short rsvd2;
273         u_short time_out;
274         u_short segment_cnt;
275         u_char cdb[16];
276         u_int total_byte_cnt;
277         struct dataseg dataseg[DATASEGS_PER_COMMAND];
278 };
279
280 /* command entry control flag definitions */
281 #define CFLAG_NODISC            0x01
282 #define CFLAG_HEAD_TAG          0x02
283 #define CFLAG_ORDERED_TAG       0x04
284 #define CFLAG_SIMPLE_TAG        0x08
285 #define CFLAG_TAR_RTN           0x10
286 #define CFLAG_READ              0x20
287 #define CFLAG_WRITE             0x40
288
289 struct Continuation_Entry {
290         struct Entry_header hdr;
291         struct dataseg dataseg[DATASEGS_PER_CONT];
292 };
293
294 struct Marker_Entry {
295         struct Entry_header hdr;
296         u_int reserved;
297         u_char target_lun;
298         u_char target_id;
299         u_char modifier;
300         u_char expanded_lun;
301         u_char rsvds[52];
302 };
303
304 /* marker entry modifier definitions */
305 #define SYNC_DEVICE     0
306 #define SYNC_TARGET     1
307 #define SYNC_ALL        2
308
309 struct Status_Entry {
310         struct Entry_header hdr;
311         u_int handle;
312         u_short scsi_status;
313         u_short completion_status;
314         u_short state_flags;
315         u_short status_flags;
316         u_short res_info_len;
317         u_short req_sense_len;
318         u_int residual;
319         u_char res_info[8];
320         u_char req_sense_data[32];
321 };
322
323 /* status entry completion status definitions */
324 #define CS_COMPLETE                     0x0000
325 #define CS_DMA_ERROR                    0x0002
326 #define CS_RESET_OCCURRED               0x0004
327 #define CS_ABORTED                      0x0005
328 #define CS_TIMEOUT                      0x0006
329 #define CS_DATA_OVERRUN                 0x0007
330 #define CS_DATA_UNDERRUN                0x0015
331 #define CS_QUEUE_FULL                   0x001c
332 #define CS_PORT_UNAVAILABLE             0x0028
333 #define CS_PORT_LOGGED_OUT              0x0029
334 #define CS_PORT_CONFIG_CHANGED          0x002a
335
336 /* status entry state flag definitions */
337 #define SF_SENT_CDB                     0x0400
338 #define SF_TRANSFERRED_DATA             0x0800
339 #define SF_GOT_STATUS                   0x1000
340
341 /* status entry status flag definitions */
342 #define STF_BUS_RESET                   0x0008
343 #define STF_DEVICE_RESET                0x0010
344 #define STF_ABORTED                     0x0020
345 #define STF_TIMEOUT                     0x0040
346
347 /* interrupt control commands */
348 #define ISP_EN_INT                      0x8000
349 #define ISP_EN_RISC                     0x0008
350
351 /* host control commands */
352 #define HCCR_NOP                        0x0000
353 #define HCCR_RESET                      0x1000
354 #define HCCR_PAUSE                      0x2000
355 #define HCCR_RELEASE                    0x3000
356 #define HCCR_SINGLE_STEP                0x4000
357 #define HCCR_SET_HOST_INTR              0x5000
358 #define HCCR_CLEAR_HOST_INTR            0x6000
359 #define HCCR_CLEAR_RISC_INTR            0x7000
360 #define HCCR_BP_ENABLE                  0x8000
361 #define HCCR_BIOS_DISABLE               0x9000
362 #define HCCR_TEST_MODE                  0xf000
363
364 #define RISC_BUSY                       0x0004
365
366 /* mailbox commands */
367 #define MBOX_NO_OP                      0x0000
368 #define MBOX_LOAD_RAM                   0x0001
369 #define MBOX_EXEC_FIRMWARE              0x0002
370 #define MBOX_DUMP_RAM                   0x0003
371 #define MBOX_WRITE_RAM_WORD             0x0004
372 #define MBOX_READ_RAM_WORD              0x0005
373 #define MBOX_MAILBOX_REG_TEST           0x0006
374 #define MBOX_VERIFY_CHECKSUM            0x0007
375 #define MBOX_ABOUT_FIRMWARE             0x0008
376 #define MBOX_LOAD_RISC_RAM              0x0009
377 #define MBOX_DUMP_RISC_RAM              0x000a
378 #define MBOX_CHECK_FIRMWARE             0x000e
379 #define MBOX_INIT_REQ_QUEUE             0x0010
380 #define MBOX_INIT_RES_QUEUE             0x0011
381 #define MBOX_EXECUTE_IOCB               0x0012
382 #define MBOX_WAKE_UP                    0x0013
383 #define MBOX_STOP_FIRMWARE              0x0014
384 #define MBOX_ABORT_IOCB                 0x0015
385 #define MBOX_ABORT_DEVICE               0x0016
386 #define MBOX_ABORT_TARGET               0x0017
387 #define MBOX_BUS_RESET                  0x0018
388 #define MBOX_STOP_QUEUE                 0x0019
389 #define MBOX_START_QUEUE                0x001a
390 #define MBOX_SINGLE_STEP_QUEUE          0x001b
391 #define MBOX_ABORT_QUEUE                0x001c
392 #define MBOX_GET_DEV_QUEUE_STATUS       0x001d
393 #define MBOX_GET_FIRMWARE_STATUS        0x001f
394 #define MBOX_GET_INIT_SCSI_ID           0x0020
395 #define MBOX_GET_RETRY_COUNT            0x0022
396 #define MBOX_GET_TARGET_PARAMS          0x0028
397 #define MBOX_GET_DEV_QUEUE_PARAMS       0x0029
398 #define MBOX_SET_RETRY_COUNT            0x0032
399 #define MBOX_SET_TARGET_PARAMS          0x0038
400 #define MBOX_SET_DEV_QUEUE_PARAMS       0x0039
401 #define MBOX_EXECUTE_IOCB64             0x0054
402 #define MBOX_INIT_FIRMWARE              0x0060
403 #define MBOX_GET_INIT_CB                0x0061
404 #define MBOX_INIT_LIP                   0x0062
405 #define MBOX_GET_POS_MAP                0x0063
406 #define MBOX_GET_PORT_DB                0x0064
407 #define MBOX_CLEAR_ACA                  0x0065
408 #define MBOX_TARGET_RESET               0x0066
409 #define MBOX_CLEAR_TASK_SET             0x0067
410 #define MBOX_ABORT_TASK_SET             0x0068
411 #define MBOX_GET_FIRMWARE_STATE         0x0069
412 #define MBOX_GET_PORT_NAME              0x006a
413 #define MBOX_SEND_SNS                   0x006e
414 #define MBOX_PORT_LOGIN                 0x006f
415 #define MBOX_SEND_CHANGE_REQUEST        0x0070
416 #define MBOX_PORT_LOGOUT                0x0071
417
418 /*
419  *      Firmware if needed (note this is a hack, it belongs in a separate
420  *      module.
421  */
422  
423 #ifdef CONFIG_SCSI_QLOGIC_FC_FIRMWARE
424 #include "qlogicfc_asm.c"
425 #else
426 static unsigned short risc_code_addr01 = 0x1000 ;
427 #endif
428
429 /* Each element in mbox_param is an 8 bit bitmap where each bit indicates
430    if that mbox should be copied as input.  For example 0x2 would mean
431    only copy mbox1. */
432
433 static const u_char mbox_param[] =
434 {
435         0x01,                   /* MBOX_NO_OP */
436         0x1f,                   /* MBOX_LOAD_RAM */
437         0x03,                   /* MBOX_EXEC_FIRMWARE */
438         0x1f,                   /* MBOX_DUMP_RAM */
439         0x07,                   /* MBOX_WRITE_RAM_WORD */
440         0x03,                   /* MBOX_READ_RAM_WORD */
441         0xff,                   /* MBOX_MAILBOX_REG_TEST */
442         0x03,                   /* MBOX_VERIFY_CHECKSUM */
443         0x01,                   /* MBOX_ABOUT_FIRMWARE */
444         0xff,                   /* MBOX_LOAD_RISC_RAM */
445         0xff,                   /* MBOX_DUMP_RISC_RAM */
446         0x00,                   /* 0x000b */
447         0x00,                   /* 0x000c */
448         0x00,                   /* 0x000d */
449         0x01,                   /* MBOX_CHECK_FIRMWARE */
450         0x00,                   /* 0x000f */
451         0x1f,                   /* MBOX_INIT_REQ_QUEUE */
452         0x2f,                   /* MBOX_INIT_RES_QUEUE */
453         0x0f,                   /* MBOX_EXECUTE_IOCB */
454         0x03,                   /* MBOX_WAKE_UP */
455         0x01,                   /* MBOX_STOP_FIRMWARE */
456         0x0f,                   /* MBOX_ABORT_IOCB */
457         0x03,                   /* MBOX_ABORT_DEVICE */
458         0x07,                   /* MBOX_ABORT_TARGET */
459         0x03,                   /* MBOX_BUS_RESET */
460         0x03,                   /* MBOX_STOP_QUEUE */
461         0x03,                   /* MBOX_START_QUEUE */
462         0x03,                   /* MBOX_SINGLE_STEP_QUEUE */
463         0x03,                   /* MBOX_ABORT_QUEUE */
464         0x03,                   /* MBOX_GET_DEV_QUEUE_STATUS */
465         0x00,                   /* 0x001e */
466         0x01,                   /* MBOX_GET_FIRMWARE_STATUS */
467         0x01,                   /* MBOX_GET_INIT_SCSI_ID */
468         0x00,                   /* 0x0021 */
469         0x01,                   /* MBOX_GET_RETRY_COUNT */
470         0x00,                   /* 0x0023 */
471         0x00,                   /* 0x0024 */
472         0x00,                   /* 0x0025 */
473         0x00,                   /* 0x0026 */
474         0x00,                   /* 0x0027 */
475         0x03,                   /* MBOX_GET_TARGET_PARAMS */
476         0x03,                   /* MBOX_GET_DEV_QUEUE_PARAMS */
477         0x00,                   /* 0x002a */
478         0x00,                   /* 0x002b */
479         0x00,                   /* 0x002c */
480         0x00,                   /* 0x002d */
481         0x00,                   /* 0x002e */
482         0x00,                   /* 0x002f */
483         0x00,                   /* 0x0030 */
484         0x00,                   /* 0x0031 */
485         0x07,                   /* MBOX_SET_RETRY_COUNT */
486         0x00,                   /* 0x0033 */
487         0x00,                   /* 0x0034 */
488         0x00,                   /* 0x0035 */
489         0x00,                   /* 0x0036 */
490         0x00,                   /* 0x0037 */
491         0x0f,                   /* MBOX_SET_TARGET_PARAMS */
492         0x0f,                   /* MBOX_SET_DEV_QUEUE_PARAMS */
493         0x00,                   /* 0x003a */
494         0x00,                   /* 0x003b */
495         0x00,                   /* 0x003c */
496         0x00,                   /* 0x003d */
497         0x00,                   /* 0x003e */
498         0x00,                   /* 0x003f */
499         0x00,                   /* 0x0040 */
500         0x00,                   /* 0x0041 */
501         0x00,                   /* 0x0042 */
502         0x00,                   /* 0x0043 */
503         0x00,                   /* 0x0044 */
504         0x00,                   /* 0x0045 */
505         0x00,                   /* 0x0046 */
506         0x00,                   /* 0x0047 */
507         0x00,                   /* 0x0048 */
508         0x00,                   /* 0x0049 */
509         0x00,                   /* 0x004a */
510         0x00,                   /* 0x004b */
511         0x00,                   /* 0x004c */
512         0x00,                   /* 0x004d */
513         0x00,                   /* 0x004e */
514         0x00,                   /* 0x004f */
515         0x00,                   /* 0x0050 */
516         0x00,                   /* 0x0051 */
517         0x00,                   /* 0x0052 */
518         0x00,                   /* 0x0053 */
519         0xcf,                   /* MBOX_EXECUTE_IOCB64 */
520         0x00,                   /* 0x0055 */
521         0x00,                   /* 0x0056 */
522         0x00,                   /* 0x0057 */
523         0x00,                   /* 0x0058 */
524         0x00,                   /* 0x0059 */
525         0x00,                   /* 0x005a */
526         0x00,                   /* 0x005b */
527         0x00,                   /* 0x005c */
528         0x00,                   /* 0x005d */
529         0x00,                   /* 0x005e */
530         0x00,                   /* 0x005f */
531         0xff,                   /* MBOX_INIT_FIRMWARE */
532         0xcd,                   /* MBOX_GET_INIT_CB */
533         0x01,                   /* MBOX_INIT_LIP */
534         0xcd,                   /* MBOX_GET_POS_MAP */
535         0xcf,                   /* MBOX_GET_PORT_DB */
536         0x03,                   /* MBOX_CLEAR_ACA */
537         0x03,                   /* MBOX_TARGET_RESET */
538         0x03,                   /* MBOX_CLEAR_TASK_SET */
539         0x03,                   /* MBOX_ABORT_TASK_SET */
540         0x01,                   /* MBOX_GET_FIRMWARE_STATE */
541         0x03,                   /* MBOX_GET_PORT_NAME */
542         0x00,                   /* 0x006b */
543         0x00,                   /* 0x006c */
544         0x00,                   /* 0x006d */
545         0xcf,                   /* MBOX_SEND_SNS */
546         0x0f,                   /* MBOX_PORT_LOGIN */
547         0x03,                   /* MBOX_SEND_CHANGE_REQUEST */
548         0x03,                   /* MBOX_PORT_LOGOUT */
549 };
550
551 #define MAX_MBOX_COMMAND        (sizeof(mbox_param)/sizeof(u_short))
552
553
554 struct id_name_map {
555         u64 wwn;
556         u_char loop_id;
557 };
558
559 struct sns_cb {
560         u_short len;
561         u_short res1;
562         u_int response_low;
563         u_int response_high;
564         u_short sub_len;
565         u_short res2;
566         u_char data[44];
567 };
568
569 /* address of instance of this struct is passed to adapter to initialize things
570  */
571 struct init_cb {
572         u_char version;
573         u_char reseverd1[1];
574         u_short firm_opts;
575         u_short max_frame_len;
576         u_short max_iocb;
577         u_short exec_throttle;
578         u_char retry_cnt;
579         u_char retry_delay;
580         u_short node_name[4];
581         u_short hard_addr;
582         u_char reserved2[10];
583         u_short req_queue_out;
584         u_short res_queue_in;
585         u_short req_queue_len;
586         u_short res_queue_len;
587         u_int req_queue_addr_lo;
588         u_int req_queue_addr_high;
589         u_int res_queue_addr_lo;
590         u_int res_queue_addr_high;
591         /* the rest of this structure only applies to the isp2200 */
592         u_short lun_enables;
593         u_char cmd_resource_cnt;
594         u_char notify_resource_cnt;
595         u_short timeout;
596         u_short reserved3;
597         u_short add_firm_opts;
598         u_char res_accum_timer;
599         u_char irq_delay_timer;
600         u_short special_options;
601         u_short reserved4[13];
602 };
603
604 /*
605  * The result queue can be quite a bit smaller since continuation entries
606  * do not show up there:
607  */
608 #define RES_QUEUE_LEN           ((QLOGICFC_REQ_QUEUE_LEN + 1) / 8 - 1)
609 #define QUEUE_ENTRY_LEN         64
610
611 #if ISP2x00_FABRIC
612 #define QLOGICFC_MAX_ID    0xff
613 #else
614 #define QLOGICFC_MAX_ID    0x7d
615 #endif
616
617 #define QLOGICFC_MAX_LUN        128
618 #define QLOGICFC_MAX_LOOP_ID    0x7d
619
620 /* the following connection options only apply to the 2200.  i have only
621  * had success with LOOP_ONLY and P2P_ONLY.
622  */
623
624 #define LOOP_ONLY              0
625 #define P2P_ONLY               1
626 #define LOOP_PREFERED          2
627 #define P2P_PREFERED           3
628
629 #define CONNECTION_PREFERENCE  LOOP_ONLY
630
631 /* adapter_state values */
632 #define AS_FIRMWARE_DEAD      -1
633 #define AS_LOOP_DOWN           0
634 #define AS_LOOP_GOOD           1
635 #define AS_REDO_FABRIC_PORTDB  2
636 #define AS_REDO_LOOP_PORTDB    4
637
638 #define RES_SIZE        ((RES_QUEUE_LEN + 1)*QUEUE_ENTRY_LEN)
639 #define REQ_SIZE        ((QLOGICFC_REQ_QUEUE_LEN + 1)*QUEUE_ENTRY_LEN)
640
641 struct isp2x00_hostdata {
642         u_char revision;
643         struct pci_dev *pci_dev;
644         /* result and request queues (shared with isp2x00): */
645         u_int req_in_ptr;       /* index of next request slot */
646         u_int res_out_ptr;      /* index of next result slot */
647
648         /* this is here so the queues are nicely aligned */
649         long send_marker;       /* do we need to send a marker? */
650
651         char * res;
652         char * req;
653         struct init_cb control_block;
654         int adapter_state;
655         unsigned long int tag_ages[QLOGICFC_MAX_ID + 1];
656         Scsi_Cmnd *handle_ptrs[QLOGICFC_REQ_QUEUE_LEN + 1];
657         unsigned long handle_serials[QLOGICFC_REQ_QUEUE_LEN + 1];
658         struct id_name_map port_db[QLOGICFC_MAX_ID + 1];
659         u_char mbox_done;
660         u64 wwn;
661         u_int port_id;
662         u_char queued;
663         u_char host_id;
664         struct timer_list explore_timer;
665         struct id_name_map tempmap[QLOGICFC_MAX_ID + 1];
666 };
667
668
669 /* queue length's _must_ be power of two: */
670 #define QUEUE_DEPTH(in, out, ql)        ((in - out) & (ql))
671 #define REQ_QUEUE_DEPTH(in, out)        QUEUE_DEPTH(in, out,                 \
672                                                     QLOGICFC_REQ_QUEUE_LEN)
673 #define RES_QUEUE_DEPTH(in, out)        QUEUE_DEPTH(in, out, RES_QUEUE_LEN)
674
675 static void isp2x00_enable_irqs(struct Scsi_Host *);
676 static void isp2x00_disable_irqs(struct Scsi_Host *);
677 static int isp2x00_init(struct Scsi_Host *);
678 static int isp2x00_reset_hardware(struct Scsi_Host *);
679 static int isp2x00_mbox_command(struct Scsi_Host *, u_short[]);
680 static int isp2x00_return_status(Scsi_Cmnd *, struct Status_Entry *);
681 static void isp2x00_intr_handler(int, void *, struct pt_regs *);
682 static irqreturn_t do_isp2x00_intr_handler(int, void *, struct pt_regs *);
683 static int isp2x00_make_portdb(struct Scsi_Host *);
684
685 #if ISP2x00_FABRIC
686 static int isp2x00_init_fabric(struct Scsi_Host *, struct id_name_map *, int);
687 #endif
688
689 #if USE_NVRAM_DEFAULTS
690 static int isp2x00_get_nvram_defaults(struct Scsi_Host *, struct init_cb *);
691 static u_short isp2x00_read_nvram_word(struct Scsi_Host *, u_short);
692 #endif
693
694 #if DEBUG_ISP2x00
695 static void isp2x00_print_scsi_cmd(Scsi_Cmnd *);
696 #endif
697
698 #if DEBUG_ISP2x00_INTR
699 static void isp2x00_print_status_entry(struct Status_Entry *);
700 #endif
701
702 static inline void isp2x00_enable_irqs(struct Scsi_Host *host)
703 {
704         outw(ISP_EN_INT | ISP_EN_RISC, host->io_port + PCI_INTER_CTL);
705 }
706
707
708 static inline void isp2x00_disable_irqs(struct Scsi_Host *host)
709 {
710         outw(0x0, host->io_port + PCI_INTER_CTL);
711 }
712
713
714 static int isp2x00_detect(struct scsi_host_template * tmpt)
715 {
716         int hosts = 0;
717         unsigned long wait_time;
718         struct Scsi_Host *host = NULL;
719         struct isp2x00_hostdata *hostdata;
720         struct pci_dev *pdev;
721         unsigned short device_ids[2];
722         dma_addr_t busaddr;
723         int i;
724
725
726         ENTER("isp2x00_detect");
727
728         device_ids[0] = PCI_DEVICE_ID_QLOGIC_ISP2100;
729         device_ids[1] = PCI_DEVICE_ID_QLOGIC_ISP2200;
730
731         tmpt->proc_name = "isp2x00";
732
733         for (i=0; i<2; i++){
734                 pdev = NULL;
735                 while ((pdev = pci_find_device(PCI_VENDOR_ID_QLOGIC, device_ids[i], pdev))) {
736                         if (pci_enable_device(pdev))
737                                 continue;
738
739                         /* Try to configure DMA attributes. */
740                         if (pci_set_dma_mask(pdev, 0xffffffffffffffffULL) &&
741                             pci_set_dma_mask(pdev, 0xffffffffULL))
742                                         continue;
743
744                         host = scsi_register(tmpt, sizeof(struct isp2x00_hostdata));
745                         if (!host) {
746                                 printk("qlogicfc%d : could not register host.\n", hosts);
747                                 continue;
748                         }
749                         host->max_id = QLOGICFC_MAX_ID + 1;
750                         host->max_lun = QLOGICFC_MAX_LUN;
751                         hostdata = (struct isp2x00_hostdata *) host->hostdata;
752
753                         memset(hostdata, 0, sizeof(struct isp2x00_hostdata));
754                         hostdata->pci_dev = pdev;
755                         hostdata->res = pci_alloc_consistent(pdev, RES_SIZE + REQ_SIZE, &busaddr);
756
757                         if (!hostdata->res){
758                                 printk("qlogicfc%d : could not allocate memory for request and response queue.\n", hosts);
759                                 scsi_unregister(host);
760                                 continue;
761                         }
762                         hostdata->req = hostdata->res + (RES_QUEUE_LEN + 1)*QUEUE_ENTRY_LEN;
763                         hostdata->queued = 0;
764                         /* set up the control block */
765                         hostdata->control_block.version = 0x1;
766                         hostdata->control_block.firm_opts = cpu_to_le16(0x800e);
767                         hostdata->control_block.max_frame_len = cpu_to_le16(2048);
768                         hostdata->control_block.max_iocb = cpu_to_le16(QLOGICFC_REQ_QUEUE_LEN);
769                         hostdata->control_block.exec_throttle = cpu_to_le16(QLOGICFC_CMD_PER_LUN);
770                         hostdata->control_block.retry_delay = 5;
771                         hostdata->control_block.retry_cnt = 1;
772                         hostdata->control_block.node_name[0] = cpu_to_le16(0x0020);
773                         hostdata->control_block.node_name[1] = cpu_to_le16(0xE000);
774                         hostdata->control_block.node_name[2] = cpu_to_le16(0x008B);
775                         hostdata->control_block.node_name[3] = cpu_to_le16(0x0000);
776                         hostdata->control_block.hard_addr = cpu_to_le16(0x0003);
777                         hostdata->control_block.req_queue_len = cpu_to_le16(QLOGICFC_REQ_QUEUE_LEN + 1);
778                         hostdata->control_block.res_queue_len = cpu_to_le16(RES_QUEUE_LEN + 1);
779                         hostdata->control_block.res_queue_addr_lo = cpu_to_le32(pci64_dma_lo32(busaddr));
780                         hostdata->control_block.res_queue_addr_high = cpu_to_le32(pci64_dma_hi32(busaddr));
781                         hostdata->control_block.req_queue_addr_lo = cpu_to_le32(pci64_dma_lo32(busaddr + RES_SIZE));
782                         hostdata->control_block.req_queue_addr_high = cpu_to_le32(pci64_dma_hi32(busaddr + RES_SIZE));
783
784
785                         hostdata->control_block.add_firm_opts |= cpu_to_le16(CONNECTION_PREFERENCE<<4);
786                         hostdata->adapter_state = AS_LOOP_DOWN;
787                         hostdata->explore_timer.data = 1;
788                         hostdata->host_id = hosts;
789
790                         if (isp2x00_init(host) || isp2x00_reset_hardware(host)) {
791                                 pci_free_consistent (pdev, RES_SIZE + REQ_SIZE, hostdata->res, busaddr);
792                                 scsi_unregister(host);
793                                 continue;
794                         }
795                         host->this_id = 0;
796
797                         if (request_irq(host->irq, do_isp2x00_intr_handler, SA_INTERRUPT | SA_SHIRQ, "qlogicfc", host)) {
798                                 printk("qlogicfc%d : interrupt %d already in use\n",
799                                        hostdata->host_id, host->irq);
800                                 pci_free_consistent (pdev, RES_SIZE + REQ_SIZE, hostdata->res, busaddr);
801                                 scsi_unregister(host);
802                                 continue;
803                         }
804                         if (!request_region(host->io_port, 0xff, "qlogicfc")) {
805                                 printk("qlogicfc%d : i/o region 0x%lx-0x%lx already "
806                                        "in use\n",
807                                        hostdata->host_id, host->io_port, host->io_port + 0xff);
808                                 free_irq(host->irq, host);
809                                 pci_free_consistent (pdev, RES_SIZE + REQ_SIZE, hostdata->res, busaddr);
810                                 scsi_unregister(host);
811                                 continue;
812                         }
813
814                         outw(0x0, host->io_port + PCI_SEMAPHORE);
815                         outw(HCCR_CLEAR_RISC_INTR, host->io_port + HOST_HCCR);
816                         isp2x00_enable_irqs(host);
817                         /* wait for the loop to come up */
818                         for (wait_time = jiffies + 10 * HZ; time_before(jiffies, wait_time) && hostdata->adapter_state == AS_LOOP_DOWN;) {
819                                 barrier();
820                                 cpu_relax();
821                         }
822                         if (hostdata->adapter_state == AS_LOOP_DOWN) {
823                                 printk("qlogicfc%d : link is not up\n", hostdata->host_id);
824                         }
825                         hosts++;
826                         hostdata->explore_timer.data = 0;
827                 }
828         }
829
830
831         /* this busy loop should not be needed but the isp2x00 seems to need 
832            some time before recognizing it is attached to a fabric */
833
834 #if ISP2x00_FABRIC
835         if (hosts) {
836                 for (wait_time = jiffies + 5 * HZ; time_before(jiffies, wait_time);) {
837                         barrier();
838                         cpu_relax();
839                 }
840         }
841 #endif
842
843         LEAVE("isp2x00_detect");
844
845         return hosts;
846 }
847
848
849 static int isp2x00_make_portdb(struct Scsi_Host *host)
850 {
851
852         short param[8];
853         int i, j;
854         struct isp2x00_hostdata *hostdata;
855
856         isp2x00_disable_irqs(host);
857
858         hostdata = (struct isp2x00_hostdata *) host->hostdata;
859         memset(hostdata->tempmap, 0, sizeof(hostdata->tempmap));
860
861 #if ISP2x00_FABRIC
862         for (i = 0x81; i < QLOGICFC_MAX_ID; i++) {
863                 param[0] = MBOX_PORT_LOGOUT;
864                 param[1] = i << 8;
865                 param[2] = 0;
866                 param[3] = 0;
867
868                 isp2x00_mbox_command(host, param);
869
870                 if (param[0] != MBOX_COMMAND_COMPLETE) {
871
872                         DEBUG_FABRIC(printk("qlogicfc%d : logout failed %x  %x\n", hostdata->host_id, i, param[0]));
873                 }
874         }
875 #endif
876
877
878         param[0] = MBOX_GET_INIT_SCSI_ID;
879
880         isp2x00_mbox_command(host, param);
881
882         if (param[0] == MBOX_COMMAND_COMPLETE) {
883                 hostdata->port_id = ((u_int) param[3]) << 16;
884                 hostdata->port_id |= param[2];
885                 hostdata->tempmap[0].loop_id = param[1];
886                 hostdata->tempmap[0].wwn = hostdata->wwn;
887         }
888         else {
889                 printk("qlogicfc%d : error getting scsi id.\n", hostdata->host_id);
890         }
891
892         for (i = 0; i <=QLOGICFC_MAX_ID; i++)
893                 hostdata->tempmap[i].loop_id = hostdata->tempmap[0].loop_id;
894    
895         for (i = 0, j = 1; i <= QLOGICFC_MAX_LOOP_ID; i++) {
896                 param[0] = MBOX_GET_PORT_NAME;
897                 param[1] = (i << 8) & 0xff00;
898
899                 isp2x00_mbox_command(host, param);
900
901                 if (param[0] == MBOX_COMMAND_COMPLETE) {
902                         hostdata->tempmap[j].loop_id = i;
903                         hostdata->tempmap[j].wwn = ((u64) (param[2] & 0xff)) << 56;
904                         hostdata->tempmap[j].wwn |= ((u64) ((param[2] >> 8) & 0xff)) << 48;
905                         hostdata->tempmap[j].wwn |= ((u64) (param[3] & 0xff)) << 40;
906                         hostdata->tempmap[j].wwn |= ((u64) ((param[3] >> 8) & 0xff)) << 32;
907                         hostdata->tempmap[j].wwn |= ((u64) (param[6] & 0xff)) << 24;
908                         hostdata->tempmap[j].wwn |= ((u64) ((param[6] >> 8) & 0xff)) << 16;
909                         hostdata->tempmap[j].wwn |= ((u64) (param[7] & 0xff)) << 8;
910                         hostdata->tempmap[j].wwn |= ((u64) ((param[7] >> 8) & 0xff));
911
912                         j++;
913
914                 }
915         }
916
917
918 #if ISP2x00_FABRIC
919         isp2x00_init_fabric(host, hostdata->tempmap, j);
920 #endif
921
922         for (i = 0; i <= QLOGICFC_MAX_ID; i++) {
923                 if (hostdata->tempmap[i].wwn != hostdata->port_db[i].wwn) {
924                         for (j = 0; j <= QLOGICFC_MAX_ID; j++) {
925                                 if (hostdata->tempmap[j].wwn == hostdata->port_db[i].wwn) {
926                                         hostdata->port_db[i].loop_id = hostdata->tempmap[j].loop_id;
927                                         break;
928                                 }
929                         }
930                         if (j == QLOGICFC_MAX_ID + 1)
931                                 hostdata->port_db[i].loop_id = hostdata->tempmap[0].loop_id;
932
933                         for (j = 0; j <= QLOGICFC_MAX_ID; j++) {
934                                 if (hostdata->port_db[j].wwn == hostdata->tempmap[i].wwn || !hostdata->port_db[j].wwn) {
935                                         break;
936                                 }
937                         }
938                         if (j == QLOGICFC_MAX_ID + 1)
939                                 printk("qlogicfc%d : Too many scsi devices, no more room in port map.\n", hostdata->host_id);
940                         if (!hostdata->port_db[j].wwn) {
941                                 hostdata->port_db[j].loop_id = hostdata->tempmap[i].loop_id;
942                                 hostdata->port_db[j].wwn = hostdata->tempmap[i].wwn;
943                         }
944                 } else
945                         hostdata->port_db[i].loop_id = hostdata->tempmap[i].loop_id;
946
947         }
948
949         isp2x00_enable_irqs(host);
950
951         return 0;
952 }
953
954
955 #if ISP2x00_FABRIC
956
957 #define FABRIC_PORT          0x7e
958 #define FABRIC_CONTROLLER    0x7f
959 #define FABRIC_SNS           0x80
960
961 int isp2x00_init_fabric(struct Scsi_Host *host, struct id_name_map *port_db, int cur_scsi_id)
962 {
963
964         u_short param[8];
965         u64 wwn;
966         int done = 0;
967         u_short loop_id = 0x81;
968         u_short scsi_id = cur_scsi_id;
969         u_int port_id;
970         struct sns_cb *req;
971         u_char *sns_response;
972         dma_addr_t busaddr;
973         struct isp2x00_hostdata *hostdata;
974
975         hostdata = (struct isp2x00_hostdata *) host->hostdata;
976         
977         DEBUG_FABRIC(printk("qlogicfc%d : Checking for a fabric.\n", hostdata->host_id));
978         param[0] = MBOX_GET_PORT_NAME;
979         param[1] = (u16)FABRIC_PORT << 8;
980
981         isp2x00_mbox_command(host, param);
982
983         if (param[0] != MBOX_COMMAND_COMPLETE) {
984                 DEBUG_FABRIC(printk("qlogicfc%d : fabric check result %x\n", hostdata->host_id, param[0]));
985                 return 0;
986         }
987         printk("qlogicfc%d : Fabric found.\n", hostdata->host_id);
988
989         req = (struct sns_cb *)pci_alloc_consistent(hostdata->pci_dev, sizeof(*req) + 608, &busaddr);
990         
991         if (!req){
992                 printk("qlogicfc%d : Could not allocate DMA resources for fabric initialization\n", hostdata->host_id);
993                 return 0;
994         }
995         sns_response = (u_char *)(req + 1);
996
997         if (hostdata->adapter_state & AS_REDO_LOOP_PORTDB){
998                 memset(req, 0, sizeof(*req));
999         
1000                 req->len = cpu_to_le16(8);
1001                 req->response_low = cpu_to_le32(pci64_dma_lo32(busaddr + sizeof(*req)));
1002                 req->response_high = cpu_to_le32(pci64_dma_hi32(busaddr + sizeof(*req)));
1003                 req->sub_len = cpu_to_le16(22);
1004                 req->data[0] = 0x17;
1005                 req->data[1] = 0x02;
1006                 req->data[8] = (u_char) (hostdata->port_id & 0xff);
1007                 req->data[9] = (u_char) (hostdata->port_id >> 8 & 0xff);
1008                 req->data[10] = (u_char) (hostdata->port_id >> 16 & 0xff);
1009                 req->data[13] = 0x01;
1010                 param[0] = MBOX_SEND_SNS;
1011                 param[1] = 30;
1012                 param[2] = pci64_dma_lo32(busaddr) >> 16;
1013                 param[3] = pci64_dma_lo32(busaddr);
1014                 param[6] = pci64_dma_hi32(busaddr) >> 16;
1015                 param[7] = pci64_dma_hi32(busaddr);
1016
1017                 isp2x00_mbox_command(host, param);
1018         
1019                 if (param[0] != MBOX_COMMAND_COMPLETE)
1020                         printk("qlogicfc%d : error sending RFC-4\n", hostdata->host_id);
1021         }
1022
1023         port_id = hostdata->port_id;
1024         while (!done) {
1025                 memset(req, 0, sizeof(*req));
1026
1027                 req->len = cpu_to_le16(304);
1028                 req->response_low = cpu_to_le32(pci64_dma_lo32(busaddr + sizeof(*req)));
1029                 req->response_high = cpu_to_le32(pci64_dma_hi32(busaddr + sizeof(*req)));
1030                 req->sub_len = cpu_to_le16(6);
1031                 req->data[0] = 0x00;
1032                 req->data[1] = 0x01;
1033                 req->data[8] = (u_char) (port_id & 0xff);
1034                 req->data[9] = (u_char) (port_id >> 8 & 0xff);
1035                 req->data[10] = (u_char) (port_id >> 16 & 0xff);
1036
1037                 param[0] = MBOX_SEND_SNS;
1038                 param[1] = 14;
1039                 param[2] = pci64_dma_lo32(busaddr) >> 16;
1040                 param[3] = pci64_dma_lo32(busaddr);
1041                 param[6] = pci64_dma_hi32(busaddr) >> 16;
1042                 param[7] = pci64_dma_hi32(busaddr);
1043
1044                 isp2x00_mbox_command(host, param);
1045
1046                 if (param[0] == MBOX_COMMAND_COMPLETE) {
1047                         DEBUG_FABRIC(printk("qlogicfc%d : found node %02x%02x%02x%02x%02x%02x%02x%02x ", hostdata->host_id, sns_response[20], sns_response[21], sns_response[22], sns_response[23], sns_response[24], sns_response[25], sns_response[26], sns_response[27]));
1048                         DEBUG_FABRIC(printk("  port id: %02x%02x%02x\n", sns_response[17], sns_response[18], sns_response[19]));
1049                         port_id = ((u_int) sns_response[17]) << 16;
1050                         port_id |= ((u_int) sns_response[18]) << 8;
1051                         port_id |= ((u_int) sns_response[19]);
1052                         wwn = ((u64) sns_response[20]) << 56;
1053                         wwn |= ((u64) sns_response[21]) << 48;
1054                         wwn |= ((u64) sns_response[22]) << 40;
1055                         wwn |= ((u64) sns_response[23]) << 32;
1056                         wwn |= ((u64) sns_response[24]) << 24;
1057                         wwn |= ((u64) sns_response[25]) << 16;
1058                         wwn |= ((u64) sns_response[26]) << 8;
1059                         wwn |= ((u64) sns_response[27]);
1060                         if (hostdata->port_id >> 8 != port_id >> 8) {
1061                                 DEBUG_FABRIC(printk("qlogicfc%d : adding a fabric port: %x\n", hostdata->host_id, port_id));
1062                                 param[0] = MBOX_PORT_LOGIN;
1063                                 param[1] = loop_id << 8;
1064                                 param[2] = (u_short) (port_id >> 16);
1065                                 param[3] = (u_short) (port_id);
1066
1067                                 isp2x00_mbox_command(host, param);
1068
1069                                 if (param[0] == MBOX_COMMAND_COMPLETE) {
1070                                         port_db[scsi_id].wwn = wwn;
1071                                         port_db[scsi_id].loop_id = loop_id;
1072                                         loop_id++;
1073                                         scsi_id++;
1074                                 } else {
1075                                         printk("qlogicfc%d : Error performing port login %x\n", hostdata->host_id, param[0]);
1076                                         DEBUG_FABRIC(printk("qlogicfc%d : loop_id: %x\n", hostdata->host_id, loop_id));
1077                                         param[0] = MBOX_PORT_LOGOUT;
1078                                         param[1] = loop_id << 8;
1079                                         param[2] = 0;
1080                                         param[3] = 0;
1081
1082                                         isp2x00_mbox_command(host, param);
1083                                         
1084                                 }
1085
1086                         }
1087                         if (hostdata->port_id == port_id)
1088                                 done = 1;
1089                 } else {
1090                         printk("qlogicfc%d : Get All Next failed %x.\n", hostdata->host_id, param[0]);
1091                         pci_free_consistent(hostdata->pci_dev, sizeof(*req) + 608, req, busaddr);
1092                         return 0;
1093                 }
1094         }
1095
1096         pci_free_consistent(hostdata->pci_dev, sizeof(*req) + 608, req, busaddr);
1097         return 1;
1098 }
1099
1100 #endif                          /* ISP2x00_FABRIC */
1101
1102
1103 static int isp2x00_release(struct Scsi_Host *host)
1104 {
1105         struct isp2x00_hostdata *hostdata;
1106         dma_addr_t busaddr;
1107
1108         ENTER("isp2x00_release");
1109
1110         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1111
1112         outw(0x0, host->io_port + PCI_INTER_CTL);
1113         free_irq(host->irq, host);
1114
1115         release_region(host->io_port, 0xff);
1116
1117         busaddr = pci64_dma_build(le32_to_cpu(hostdata->control_block.res_queue_addr_high),
1118                                   le32_to_cpu(hostdata->control_block.res_queue_addr_lo));
1119         pci_free_consistent(hostdata->pci_dev, RES_SIZE + REQ_SIZE, hostdata->res, busaddr);
1120
1121         LEAVE("isp2x00_release");
1122
1123         return 0;
1124 }
1125
1126
1127 static const char *isp2x00_info(struct Scsi_Host *host)
1128 {
1129         static char buf[80];
1130         struct isp2x00_hostdata *hostdata;
1131         ENTER("isp2x00_info");
1132
1133         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1134         sprintf(buf,
1135                 "QLogic ISP%04x SCSI on PCI bus %02x device %02x irq %d base 0x%lx",
1136                 hostdata->pci_dev->device, hostdata->pci_dev->bus->number, hostdata->pci_dev->devfn, host->irq,
1137                 host->io_port);
1138
1139
1140         LEAVE("isp2x00_info");
1141
1142         return buf;
1143 }
1144
1145
1146 /*
1147  * The middle SCSI layer ensures that queuecommand never gets invoked
1148  * concurrently with itself or the interrupt handler (though the
1149  * interrupt handler may call this routine as part of
1150  * request-completion handling).
1151  */
1152 static int isp2x00_queuecommand(Scsi_Cmnd * Cmnd, void (*done) (Scsi_Cmnd *))
1153 {
1154         int i, sg_count, n, num_free;
1155         u_int in_ptr, out_ptr;
1156         struct dataseg *ds;
1157         struct scatterlist *sg;
1158         struct Command_Entry *cmd;
1159         struct Continuation_Entry *cont;
1160         struct Scsi_Host *host;
1161         struct isp2x00_hostdata *hostdata;
1162
1163         ENTER("isp2x00_queuecommand");
1164
1165         host = Cmnd->device->host;
1166         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1167         Cmnd->scsi_done = done;
1168
1169         DEBUG(isp2x00_print_scsi_cmd(Cmnd));
1170
1171         if (hostdata->adapter_state & AS_REDO_FABRIC_PORTDB || hostdata->adapter_state & AS_REDO_LOOP_PORTDB) {
1172                 isp2x00_make_portdb(host);
1173                 hostdata->adapter_state = AS_LOOP_GOOD;
1174                 printk("qlogicfc%d : Port Database\n", hostdata->host_id);
1175                 for (i = 0; hostdata->port_db[i].wwn != 0; i++) {
1176                         printk("wwn: %08x%08x  scsi_id: %x  loop_id: ", (u_int) (hostdata->port_db[i].wwn >> 32), (u_int) hostdata->port_db[i].wwn, i);
1177                         if (hostdata->port_db[i].loop_id != hostdata->port_db[0].loop_id || i == 0)
1178                                 printk("%x", hostdata->port_db[i].loop_id);
1179                         else
1180                                 printk("Not Available");
1181                         printk("\n");
1182                 }
1183         }
1184         if (hostdata->adapter_state == AS_FIRMWARE_DEAD) {
1185                 printk("qlogicfc%d : The firmware is dead, just return.\n", hostdata->host_id);
1186                 host->max_id = 0;
1187                 return 0;
1188         }
1189
1190         out_ptr = inw(host->io_port + MBOX4);
1191         in_ptr = hostdata->req_in_ptr;
1192
1193         DEBUG(printk("qlogicfc%d : request queue depth %d\n", hostdata->host_id,
1194                      REQ_QUEUE_DEPTH(in_ptr, out_ptr)));
1195
1196         cmd = (struct Command_Entry *) &hostdata->req[in_ptr*QUEUE_ENTRY_LEN];
1197         in_ptr = (in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN;
1198         if (in_ptr == out_ptr) {
1199                 DEBUG(printk("qlogicfc%d : request queue overflow\n", hostdata->host_id));
1200                 return 1;
1201         }
1202         if (hostdata->send_marker) {
1203                 struct Marker_Entry *marker;
1204
1205                 TRACE("queue marker", in_ptr, 0);
1206
1207                 DEBUG(printk("qlogicfc%d : adding marker entry\n", hostdata->host_id));
1208                 marker = (struct Marker_Entry *) cmd;
1209                 memset(marker, 0, sizeof(struct Marker_Entry));
1210
1211                 marker->hdr.entry_type = ENTRY_MARKER;
1212                 marker->hdr.entry_cnt = 1;
1213                 marker->modifier = SYNC_ALL;
1214
1215                 hostdata->send_marker = 0;
1216
1217                 if (((in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN) == out_ptr) {
1218                         outw(in_ptr, host->io_port + MBOX4);
1219                         hostdata->req_in_ptr = in_ptr;
1220                         DEBUG(printk("qlogicfc%d : request queue overflow\n", hostdata->host_id));
1221                         return 1;
1222                 }
1223                 cmd = (struct Command_Entry *) &hostdata->req[in_ptr*QUEUE_ENTRY_LEN];
1224                 in_ptr = (in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN;
1225         }
1226         TRACE("queue command", in_ptr, Cmnd);
1227
1228         memset(cmd, 0, sizeof(struct Command_Entry));
1229
1230         /* find a free handle mapping slot */
1231         for (i = in_ptr; i != (in_ptr - 1) && hostdata->handle_ptrs[i]; i = ((i + 1) % (QLOGICFC_REQ_QUEUE_LEN + 1)));
1232
1233         if (!hostdata->handle_ptrs[i]) {
1234                 cmd->handle = cpu_to_le32(i);
1235                 hostdata->handle_ptrs[i] = Cmnd;
1236                 hostdata->handle_serials[i] = Cmnd->serial_number;
1237         } else {
1238                 printk("qlogicfc%d : no handle slots, this should not happen.\n", hostdata->host_id);
1239                 printk("hostdata->queued is %x, in_ptr: %x\n", hostdata->queued, in_ptr);
1240                 for (i = 0; i <= QLOGICFC_REQ_QUEUE_LEN; i++){
1241                         if (!hostdata->handle_ptrs[i]){
1242                                 printk("slot %d has %p\n", i, hostdata->handle_ptrs[i]);
1243                         }
1244                 }
1245                 return 1;
1246         }
1247
1248         cmd->hdr.entry_type = ENTRY_COMMAND;
1249         cmd->hdr.entry_cnt = 1;
1250         cmd->target_lun = Cmnd->device->lun;
1251         cmd->expanded_lun = cpu_to_le16(Cmnd->device->lun);
1252 #if ISP2x00_PORTDB
1253         cmd->target_id = hostdata->port_db[Cmnd->device->id].loop_id;
1254 #else
1255         cmd->target_id = Cmnd->target;
1256 #endif
1257         cmd->total_byte_cnt = cpu_to_le32(Cmnd->request_bufflen);
1258         cmd->time_out = 0;
1259         memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
1260
1261         if (Cmnd->use_sg) {
1262                 sg = (struct scatterlist *) Cmnd->request_buffer;
1263                 sg_count = pci_map_sg(hostdata->pci_dev, sg, Cmnd->use_sg, Cmnd->sc_data_direction);
1264                 cmd->segment_cnt = cpu_to_le16(sg_count);
1265                 ds = cmd->dataseg;
1266                 /* fill in first two sg entries: */
1267                 n = sg_count;
1268                 if (n > DATASEGS_PER_COMMAND)
1269                         n = DATASEGS_PER_COMMAND;
1270
1271                 for (i = 0; i < n; i++) {
1272                         ds[i].d_base = cpu_to_le32(pci64_dma_lo32(sg_dma_address(sg)));
1273                         ds[i].d_base_hi = cpu_to_le32(pci64_dma_hi32(sg_dma_address(sg)));
1274                         ds[i].d_count = cpu_to_le32(sg_dma_len(sg));
1275                         ++sg;
1276                 }
1277                 sg_count -= DATASEGS_PER_COMMAND;
1278
1279                 while (sg_count > 0) {
1280                         ++cmd->hdr.entry_cnt;
1281                         cont = (struct Continuation_Entry *)
1282                             &hostdata->req[in_ptr*QUEUE_ENTRY_LEN];
1283                         memset(cont, 0, sizeof(struct Continuation_Entry));
1284                         in_ptr = (in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN;
1285                         if (in_ptr == out_ptr) {
1286                                 DEBUG(printk("qlogicfc%d : unexpected request queue overflow\n", hostdata->host_id));
1287                                 return 1;
1288                         }
1289                         TRACE("queue continuation", in_ptr, 0);
1290                         cont->hdr.entry_type = ENTRY_CONTINUATION;
1291                         ds = cont->dataseg;
1292                         n = sg_count;
1293                         if (n > DATASEGS_PER_CONT)
1294                                 n = DATASEGS_PER_CONT;
1295                         for (i = 0; i < n; ++i) {
1296                                 ds[i].d_base = cpu_to_le32(pci64_dma_lo32(sg_dma_address(sg)));
1297                                 ds[i].d_base_hi = cpu_to_le32(pci64_dma_hi32(sg_dma_address(sg)));
1298                                 ds[i].d_count = cpu_to_le32(sg_dma_len(sg));
1299                                 ++sg;
1300                         }
1301                         sg_count -= n;
1302                 }
1303         } else if (Cmnd->request_bufflen && Cmnd->sc_data_direction != PCI_DMA_NONE) {
1304                 struct page *page = virt_to_page(Cmnd->request_buffer);
1305                 unsigned long offset = offset_in_page(Cmnd->request_buffer);
1306                 dma_addr_t busaddr = pci_map_page(hostdata->pci_dev,
1307                                                   page, offset,
1308                                                   Cmnd->request_bufflen,
1309                                                   Cmnd->sc_data_direction);
1310                 Cmnd->SCp.dma_handle = busaddr;
1311
1312                 cmd->dataseg[0].d_base = cpu_to_le32(pci64_dma_lo32(busaddr));
1313                 cmd->dataseg[0].d_base_hi = cpu_to_le32(pci64_dma_hi32(busaddr));
1314                 cmd->dataseg[0].d_count = cpu_to_le32(Cmnd->request_bufflen);
1315                 cmd->segment_cnt = cpu_to_le16(1);
1316         } else {
1317                 cmd->dataseg[0].d_base = 0;
1318                 cmd->dataseg[0].d_base_hi = 0;
1319                 cmd->segment_cnt = cpu_to_le16(1); /* Shouldn't this be 0? */
1320         }
1321
1322         if (Cmnd->sc_data_direction == DMA_TO_DEVICE)
1323                 cmd->control_flags = cpu_to_le16(CFLAG_WRITE);
1324         else 
1325                 cmd->control_flags = cpu_to_le16(CFLAG_READ);
1326
1327         if (Cmnd->device->tagged_supported) {
1328                 if ((jiffies - hostdata->tag_ages[Cmnd->device->id]) > (2 * ISP_TIMEOUT)) {
1329                         cmd->control_flags |= cpu_to_le16(CFLAG_ORDERED_TAG);
1330                         hostdata->tag_ages[Cmnd->device->id] = jiffies;
1331                 } else
1332                         switch (Cmnd->tag) {
1333                         case HEAD_OF_QUEUE_TAG:
1334                                 cmd->control_flags |= cpu_to_le16(CFLAG_HEAD_TAG);
1335                                 break;
1336                         case ORDERED_QUEUE_TAG:
1337                                 cmd->control_flags |= cpu_to_le16(CFLAG_ORDERED_TAG);
1338                                 break;
1339                         default:
1340                                 cmd->control_flags |= cpu_to_le16(CFLAG_SIMPLE_TAG);
1341                                 break;
1342                 }
1343         }
1344         /*
1345          * TEST_UNIT_READY commands from scsi_scan will fail due to "overlapped
1346          * commands attempted" unless we setup at least a simple queue (midlayer 
1347          * will embelish this once it can do an INQUIRY command to the device)
1348          */
1349         else
1350                 cmd->control_flags |= cpu_to_le16(CFLAG_SIMPLE_TAG);
1351         outw(in_ptr, host->io_port + MBOX4);
1352         hostdata->req_in_ptr = in_ptr;
1353
1354         hostdata->queued++;
1355
1356         num_free = QLOGICFC_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr);
1357         num_free = (num_free > 2) ? num_free - 2 : 0;
1358        host->can_queue = host->host_busy + num_free;
1359         if (host->can_queue > QLOGICFC_REQ_QUEUE_LEN)
1360                 host->can_queue = QLOGICFC_REQ_QUEUE_LEN;
1361         host->sg_tablesize = QLOGICFC_MAX_SG(num_free);
1362
1363         LEAVE("isp2x00_queuecommand");
1364
1365         return 0;
1366 }
1367
1368
1369 /* we have received an event, such as a lip or an RSCN, which may mean that
1370  * our port database is incorrect so the port database must be recreated.
1371  */
1372 static void redo_port_db(unsigned long arg)
1373 {
1374
1375         struct Scsi_Host * host = (struct Scsi_Host *) arg;
1376         struct isp2x00_hostdata * hostdata;
1377         unsigned long flags;
1378         int i;
1379
1380         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1381         hostdata->explore_timer.data = 0;
1382         del_timer(&hostdata->explore_timer);
1383
1384         spin_lock_irqsave(host->host_lock, flags);
1385
1386         if (hostdata->adapter_state & AS_REDO_FABRIC_PORTDB || hostdata->adapter_state & AS_REDO_LOOP_PORTDB) {
1387                 isp2x00_make_portdb(host);
1388                 printk("qlogicfc%d : Port Database\n", hostdata->host_id);
1389                 for (i = 0; hostdata->port_db[i].wwn != 0; i++) {
1390                         printk("wwn: %08x%08x  scsi_id: %x  loop_id: ", (u_int) (hostdata->port_db[i].wwn >> 32), (u_int) hostdata->port_db[i].wwn, i);
1391                         if (hostdata->port_db[i].loop_id != hostdata->port_db[0].loop_id || i == 0)
1392                                 printk("%x", hostdata->port_db[i].loop_id);
1393                         else
1394                                 printk("Not Available");
1395                         printk("\n");
1396                 }
1397                 
1398                 for (i = 0; i < QLOGICFC_REQ_QUEUE_LEN; i++){ 
1399                         if (hostdata->handle_ptrs[i] && (hostdata->port_db[hostdata->handle_ptrs[i]->device->id].loop_id > QLOGICFC_MAX_LOOP_ID || hostdata->adapter_state & AS_REDO_LOOP_PORTDB)){
1400                                 if (hostdata->port_db[hostdata->handle_ptrs[i]->device->id].loop_id != hostdata->port_db[0].loop_id){
1401                                         Scsi_Cmnd *Cmnd = hostdata->handle_ptrs[i];
1402
1403                                          if (Cmnd->use_sg)
1404                                                  pci_unmap_sg(hostdata->pci_dev,
1405                                                               (struct scatterlist *)Cmnd->buffer,
1406                                                               Cmnd->use_sg,
1407                                                               Cmnd->sc_data_direction);
1408                                          else if (Cmnd->request_bufflen &&
1409                                                   Cmnd->sc_data_direction != PCI_DMA_NONE) {
1410                                                  pci_unmap_page(hostdata->pci_dev,
1411                                                                 Cmnd->SCp.dma_handle,
1412                                                                 Cmnd->request_bufflen,
1413                                                                 Cmnd->sc_data_direction);
1414                                          }
1415
1416                                          hostdata->handle_ptrs[i]->result = DID_SOFT_ERROR << 16;
1417
1418                                          if (hostdata->handle_ptrs[i]->scsi_done){
1419                                            (*hostdata->handle_ptrs[i]->scsi_done) (hostdata->handle_ptrs[i]);
1420                                          }
1421                                          else printk("qlogicfc%d : done is null?\n", hostdata->host_id);
1422                                          hostdata->handle_ptrs[i] = NULL;
1423                                          hostdata->handle_serials[i] = 0;
1424                                 }
1425                         }
1426                 }
1427                 
1428                 hostdata->adapter_state = AS_LOOP_GOOD;
1429         }
1430
1431         spin_unlock_irqrestore(host->host_lock, flags);
1432
1433 }
1434
1435 #define ASYNC_EVENT_INTERRUPT   0x01
1436
1437 irqreturn_t do_isp2x00_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1438 {
1439         struct Scsi_Host *host = dev_id;
1440         unsigned long flags;
1441
1442         spin_lock_irqsave(host->host_lock, flags);
1443         isp2x00_intr_handler(irq, dev_id, regs);
1444         spin_unlock_irqrestore(host->host_lock, flags);
1445
1446         return IRQ_HANDLED;
1447 }
1448
1449 void isp2x00_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1450 {
1451         Scsi_Cmnd *Cmnd;
1452         struct Status_Entry *sts;
1453         struct Scsi_Host *host = dev_id;
1454         struct isp2x00_hostdata *hostdata;
1455         u_int in_ptr, out_ptr, handle, num_free;
1456         u_short status;
1457
1458         ENTER_INTR("isp2x00_intr_handler");
1459
1460         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1461
1462         DEBUG_INTR(printk("qlogicfc%d : interrupt on line %d\n", hostdata->host_id, irq));
1463
1464         if (!(inw(host->io_port + PCI_INTER_STS) & 0x08)) {
1465                 /* spurious interrupts can happen legally */
1466                 DEBUG_INTR(printk("qlogicfc%d : got spurious interrupt\n", hostdata->host_id));
1467                 return;
1468         }
1469         in_ptr = inw(host->io_port + MBOX5);
1470         out_ptr = hostdata->res_out_ptr;
1471
1472         if ((inw(host->io_port + PCI_SEMAPHORE) & ASYNC_EVENT_INTERRUPT)) {
1473                 status = inw(host->io_port + MBOX0);
1474
1475                 DEBUG_INTR(printk("qlogicfc%d : mbox completion status: %x\n",
1476                                   hostdata->host_id, status));
1477
1478                 switch (status) {
1479                 case LOOP_UP:
1480                 case POINT_TO_POINT_UP:
1481                         printk("qlogicfc%d : Link is Up\n", hostdata->host_id);
1482                         hostdata->adapter_state = AS_REDO_FABRIC_PORTDB | AS_REDO_LOOP_PORTDB;
1483                         break;
1484                 case LOOP_DOWN:
1485                         printk("qlogicfc%d : Link is Down\n", hostdata->host_id);
1486                         hostdata->adapter_state = AS_LOOP_DOWN;
1487                         break;
1488                 case CONNECTION_MODE:
1489                         printk("received CONNECTION_MODE irq %x\n", inw(host->io_port + MBOX1));
1490                         break;
1491                 case CHANGE_NOTIFICATION:
1492                         printk("qlogicfc%d : RSCN Received\n", hostdata->host_id);
1493                         if (hostdata->adapter_state == AS_LOOP_GOOD)
1494                                 hostdata->adapter_state = AS_REDO_FABRIC_PORTDB;
1495                         break;                  
1496                 case LIP_OCCURRED:
1497                 case LIP_RECEIVED:
1498                         printk("qlogicfc%d : Loop Reinitialized\n", hostdata->host_id);
1499                         if (hostdata->adapter_state == AS_LOOP_GOOD)
1500                                 hostdata->adapter_state = AS_REDO_LOOP_PORTDB;
1501                         break;
1502                 case SYSTEM_ERROR:
1503                         printk("qlogicfc%d : The firmware just choked.\n", hostdata->host_id);
1504                         hostdata->adapter_state = AS_FIRMWARE_DEAD;
1505                         break;
1506                 case SCSI_COMMAND_COMPLETE:
1507                         handle = inw(host->io_port + MBOX1) | (inw(host->io_port + MBOX2) << 16);
1508                         Cmnd = hostdata->handle_ptrs[handle];
1509                         hostdata->handle_ptrs[handle] = NULL;
1510                         hostdata->handle_serials[handle] = 0;
1511                         hostdata->queued--;
1512                         if (Cmnd != NULL) {
1513                                 if (Cmnd->use_sg)
1514                                         pci_unmap_sg(hostdata->pci_dev,
1515                                                      (struct scatterlist *)Cmnd->buffer,
1516                                                      Cmnd->use_sg,
1517                                                      Cmnd->sc_data_direction);
1518                                 else if (Cmnd->request_bufflen &&
1519                                          Cmnd->sc_data_direction != PCI_DMA_NONE)
1520                                         pci_unmap_page(hostdata->pci_dev,
1521                                                        Cmnd->SCp.dma_handle,
1522                                                        Cmnd->request_bufflen,
1523                                                        Cmnd->sc_data_direction);
1524                                 Cmnd->result = 0x0;
1525                                 (*Cmnd->scsi_done) (Cmnd);
1526                         } else
1527                                 printk("qlogicfc%d.c : got a null value out of handle_ptrs, this sucks\n", hostdata->host_id);
1528                         break;
1529                 case MBOX_COMMAND_COMPLETE:
1530                 case INVALID_COMMAND:
1531                 case HOST_INTERFACE_ERROR:
1532                 case TEST_FAILED:
1533                 case COMMAND_ERROR:
1534                 case COMMAND_PARAM_ERROR:
1535                 case PORT_ID_USED:
1536                 case LOOP_ID_USED:
1537                 case ALL_IDS_USED:
1538                         hostdata->mbox_done = 1;
1539                         outw(HCCR_CLEAR_RISC_INTR, host->io_port + HOST_HCCR);
1540                         return;
1541                 default:
1542                         printk("qlogicfc%d : got an unknown status? %x\n", hostdata->host_id, status);
1543                 }
1544                 if ((hostdata->adapter_state & AS_REDO_LOOP_PORTDB || hostdata->adapter_state & AS_REDO_FABRIC_PORTDB) && hostdata->explore_timer.data == 0){
1545                         hostdata->explore_timer.function = redo_port_db;
1546                         hostdata->explore_timer.data = (unsigned long)host;
1547                         hostdata->explore_timer.expires = jiffies + (HZ/4);
1548                         init_timer(&hostdata->explore_timer);
1549                         add_timer(&hostdata->explore_timer);
1550                 }
1551                 outw(0x0, host->io_port + PCI_SEMAPHORE);
1552         } else {
1553                 DEBUG_INTR(printk("qlogicfc%d : response queue update\n", hostdata->host_id));
1554                 DEBUG_INTR(printk("qlogicfc%d : response queue depth %d\n", hostdata->host_id, RES_QUEUE_DEPTH(in_ptr, out_ptr)));
1555
1556                 while (out_ptr != in_ptr) {
1557                         unsigned le_hand;
1558                         sts = (struct Status_Entry *) &hostdata->res[out_ptr*QUEUE_ENTRY_LEN];
1559                         out_ptr = (out_ptr + 1) & RES_QUEUE_LEN;
1560                  
1561                         TRACE("done", out_ptr, Cmnd);
1562                         DEBUG_INTR(isp2x00_print_status_entry(sts));
1563                         le_hand = le32_to_cpu(sts->handle);
1564                         if (sts->hdr.entry_type == ENTRY_STATUS && (Cmnd = hostdata->handle_ptrs[le_hand])) {
1565                                 Cmnd->result = isp2x00_return_status(Cmnd, sts);
1566                                 hostdata->queued--;
1567
1568                                 if (Cmnd->use_sg)
1569                                         pci_unmap_sg(hostdata->pci_dev,
1570                                                      (struct scatterlist *)Cmnd->buffer, Cmnd->use_sg,
1571                                                      Cmnd->sc_data_direction);
1572                                 else if (Cmnd->request_bufflen && Cmnd->sc_data_direction != PCI_DMA_NONE)
1573                                         pci_unmap_page(hostdata->pci_dev,
1574                                                        Cmnd->SCp.dma_handle,
1575                                                        Cmnd->request_bufflen,
1576                                                        Cmnd->sc_data_direction);
1577
1578                                 /* 
1579                                  * if any of the following are true we do not
1580                                  * call scsi_done.  if the status is CS_ABORTED
1581                                  * we don't have to call done because the upper
1582                                  * level should already know its aborted.
1583                                  */
1584                                 if (hostdata->handle_serials[le_hand] != Cmnd->serial_number 
1585                                     || le16_to_cpu(sts->completion_status) == CS_ABORTED){
1586                                         hostdata->handle_serials[le_hand] = 0;
1587                                         hostdata->handle_ptrs[le_hand] = NULL;
1588                                         outw(out_ptr, host->io_port + MBOX5);
1589                                         continue;
1590                                 }
1591                                 /*
1592                                  * if we get back an error indicating the port
1593                                  * is not there or if the link is down and 
1594                                  * this is a device that used to be there 
1595                                  * allow the command to timeout.
1596                                  * the device may well be back in a couple of
1597                                  * seconds.
1598                                  */
1599                                 if ((hostdata->adapter_state == AS_LOOP_DOWN || sts->completion_status == cpu_to_le16(CS_PORT_UNAVAILABLE) || sts->completion_status == cpu_to_le16(CS_PORT_LOGGED_OUT) || sts->completion_status == cpu_to_le16(CS_PORT_CONFIG_CHANGED)) && hostdata->port_db[Cmnd->device->id].wwn){
1600                                         outw(out_ptr, host->io_port + MBOX5);
1601                                         continue;
1602                                 }
1603                         } else {
1604                                 outw(out_ptr, host->io_port + MBOX5);
1605                                 continue;
1606                         }
1607
1608                         hostdata->handle_ptrs[le_hand] = NULL;
1609
1610                         if (sts->completion_status == cpu_to_le16(CS_RESET_OCCURRED)
1611                             || (sts->status_flags & cpu_to_le16(STF_BUS_RESET)))
1612                                 hostdata->send_marker = 1;
1613
1614                         if (le16_to_cpu(sts->scsi_status) & 0x0200)
1615                                 memcpy(Cmnd->sense_buffer, sts->req_sense_data,
1616                                        sizeof(Cmnd->sense_buffer));
1617
1618                         outw(out_ptr, host->io_port + MBOX5);
1619
1620                         if (Cmnd->scsi_done != NULL) {
1621                                 (*Cmnd->scsi_done) (Cmnd);
1622                         } else
1623                                 printk("qlogicfc%d : Ouch, scsi done is NULL\n", hostdata->host_id);
1624                 }
1625                 hostdata->res_out_ptr = out_ptr;
1626         }
1627
1628
1629         out_ptr = inw(host->io_port + MBOX4);
1630         in_ptr = hostdata->req_in_ptr;
1631
1632         num_free = QLOGICFC_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr);
1633         num_free = (num_free > 2) ? num_free - 2 : 0;
1634        host->can_queue = host->host_busy + num_free;
1635         if (host->can_queue > QLOGICFC_REQ_QUEUE_LEN)
1636                 host->can_queue = QLOGICFC_REQ_QUEUE_LEN;
1637         host->sg_tablesize = QLOGICFC_MAX_SG(num_free);
1638
1639         outw(HCCR_CLEAR_RISC_INTR, host->io_port + HOST_HCCR);
1640         LEAVE_INTR("isp2x00_intr_handler");
1641 }
1642
1643
1644 static int isp2x00_return_status(Scsi_Cmnd *Cmnd, struct Status_Entry *sts)
1645 {
1646         int host_status = DID_ERROR;
1647 #if DEBUG_ISP2x00_INTR
1648         static char *reason[] =
1649         {
1650                 "DID_OK",
1651                 "DID_NO_CONNECT",
1652                 "DID_BUS_BUSY",
1653                 "DID_TIME_OUT",
1654                 "DID_BAD_TARGET",
1655                 "DID_ABORT",
1656                 "DID_PARITY",
1657                 "DID_ERROR",
1658                 "DID_RESET",
1659                 "DID_BAD_INTR"
1660         };
1661 #endif                          /* DEBUG_ISP2x00_INTR */
1662
1663         ENTER("isp2x00_return_status");
1664
1665         DEBUG(printk("qlogicfc : completion status = 0x%04x\n",
1666                      le16_to_cpu(sts->completion_status)));
1667
1668         switch (le16_to_cpu(sts->completion_status)) {
1669         case CS_COMPLETE:
1670                 host_status = DID_OK;
1671                 break;
1672         case CS_DMA_ERROR:
1673                 host_status = DID_ERROR;
1674                 break;
1675         case CS_RESET_OCCURRED:
1676                 host_status = DID_RESET;
1677                 break;
1678         case CS_ABORTED:
1679                 host_status = DID_ABORT;
1680                 break;
1681         case CS_TIMEOUT:
1682                 host_status = DID_TIME_OUT;
1683                 break;
1684         case CS_DATA_OVERRUN:
1685                 host_status = DID_ERROR;
1686                 break;
1687         case CS_DATA_UNDERRUN:
1688                 if (Cmnd->underflow <= (Cmnd->request_bufflen - le32_to_cpu(sts->residual)))
1689                         host_status = DID_OK;
1690                 else
1691                         host_status = DID_ERROR;
1692                 break;
1693         case CS_PORT_UNAVAILABLE:
1694         case CS_PORT_LOGGED_OUT:
1695         case CS_PORT_CONFIG_CHANGED:
1696                 host_status = DID_BAD_TARGET;
1697                 break;
1698         case CS_QUEUE_FULL:
1699                 host_status = DID_ERROR;
1700                 break;
1701         default:
1702                 printk("qlogicfc : unknown completion status 0x%04x\n",
1703                        le16_to_cpu(sts->completion_status));
1704                 host_status = DID_ERROR;
1705                 break;
1706         }
1707
1708         DEBUG_INTR(printk("qlogicfc : host status (%s) scsi status %x\n",
1709                           reason[host_status], le16_to_cpu(sts->scsi_status)));
1710
1711         LEAVE("isp2x00_return_status");
1712
1713         return (le16_to_cpu(sts->scsi_status) & STATUS_MASK) | (host_status << 16);
1714 }
1715
1716
1717 static int isp2x00_abort(Scsi_Cmnd * Cmnd)
1718 {
1719         u_short param[8];
1720         int i;
1721         struct Scsi_Host *host;
1722         struct isp2x00_hostdata *hostdata;
1723         int return_status = SUCCESS;
1724
1725         ENTER("isp2x00_abort");
1726
1727         host = Cmnd->device->host;
1728         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1729
1730         for (i = 0; i < QLOGICFC_REQ_QUEUE_LEN; i++)
1731                 if (hostdata->handle_ptrs[i] == Cmnd)
1732                         break;
1733
1734         if (i == QLOGICFC_REQ_QUEUE_LEN){
1735                 return SUCCESS;
1736         }
1737
1738         isp2x00_disable_irqs(host);
1739
1740         param[0] = MBOX_ABORT_IOCB;
1741 #if ISP2x00_PORTDB
1742         param[1] = (((u_short) hostdata->port_db[Cmnd->device->id].loop_id) << 8) | Cmnd->device->lun;
1743 #else
1744         param[1] = (((u_short) Cmnd->target) << 8) | Cmnd->lun;
1745 #endif
1746         param[2] = i & 0xffff;
1747         param[3] = i >> 16;
1748
1749         isp2x00_mbox_command(host, param);
1750
1751         if (param[0] != MBOX_COMMAND_COMPLETE) {
1752                 printk("qlogicfc%d : scsi abort failure: %x\n", hostdata->host_id, param[0]);
1753                 if (param[0] == 0x4005)
1754                         Cmnd->result = DID_ERROR << 16;
1755                 if (param[0] == 0x4006)
1756                         Cmnd->result = DID_BAD_TARGET << 16;
1757                 return_status = FAILED;
1758         }
1759
1760         if (return_status != SUCCESS){
1761                 param[0] = MBOX_GET_FIRMWARE_STATE;
1762                 isp2x00_mbox_command(host, param);
1763                 printk("qlogicfc%d : abort failed\n", hostdata->host_id);
1764                 printk("qlogicfc%d : firmware status is %x %x\n", hostdata->host_id, param[0], param[1]);
1765         }
1766
1767         isp2x00_enable_irqs(host);
1768
1769         LEAVE("isp2x00_abort");
1770
1771         return return_status;
1772 }
1773
1774
1775 static int isp2x00_biosparam(struct scsi_device *sdev, struct block_device *n,
1776                 sector_t capacity, int ip[])
1777 {
1778         int size = capacity;
1779
1780         ENTER("isp2x00_biosparam");
1781
1782         ip[0] = 64;
1783         ip[1] = 32;
1784         ip[2] = size >> 11;
1785         if (ip[2] > 1024) {
1786                 ip[0] = 255;
1787                 ip[1] = 63;
1788                 ip[2] = size / (ip[0] * ip[1]);
1789         }
1790         LEAVE("isp2x00_biosparam");
1791
1792         return 0;
1793 }
1794
1795 static int isp2x00_reset_hardware(struct Scsi_Host *host)
1796 {
1797         u_short param[8];
1798         struct isp2x00_hostdata *hostdata;
1799         int loop_count;
1800         dma_addr_t busaddr;
1801
1802         ENTER("isp2x00_reset_hardware");
1803
1804         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1805
1806         /*
1807          *      This cannot be right - PCI writes are posted
1808          *      (apparently this is hardware design flaw not software ?)
1809          */
1810          
1811         outw(0x01, host->io_port + ISP_CTRL_STATUS);
1812         udelay(100);
1813         outw(HCCR_RESET, host->io_port + HOST_HCCR);
1814         udelay(100);
1815         outw(HCCR_RELEASE, host->io_port + HOST_HCCR);
1816         outw(HCCR_BIOS_DISABLE, host->io_port + HOST_HCCR);
1817
1818         loop_count = DEFAULT_LOOP_COUNT;
1819         while (--loop_count && inw(host->io_port + HOST_HCCR) == RISC_BUSY) {
1820                 barrier();
1821                 cpu_relax();
1822         }
1823         if (!loop_count)
1824                 printk("qlogicfc%d : reset_hardware loop timeout\n", hostdata->host_id);
1825
1826
1827
1828 #if DEBUG_ISP2x00
1829         printk("qlogicfc%d : mbox 0 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX0));
1830         printk("qlogicfc%d : mbox 1 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX1));
1831         printk("qlogicfc%d : mbox 2 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX2));
1832         printk("qlogicfc%d : mbox 3 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX3));
1833         printk("qlogicfc%d : mbox 4 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX4));
1834         printk("qlogicfc%d : mbox 5 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX5));
1835         printk("qlogicfc%d : mbox 6 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX6));
1836         printk("qlogicfc%d : mbox 7 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX7));
1837 #endif                          /* DEBUG_ISP2x00 */
1838
1839         DEBUG(printk("qlogicfc%d : verifying checksum\n", hostdata->host_id));
1840
1841 #if defined(CONFIG_SCSI_QLOGIC_FC_FIRMWARE)
1842         {
1843                 int i;
1844                 unsigned short * risc_code = NULL;
1845                 unsigned short risc_code_len = 0;
1846                 if (hostdata->pci_dev->device == PCI_DEVICE_ID_QLOGIC_ISP2100){
1847                         risc_code = risc_code2100;
1848                         risc_code_len = risc_code_length2100;
1849                 }
1850                 else if (hostdata->pci_dev->device == PCI_DEVICE_ID_QLOGIC_ISP2200){
1851                         risc_code = risc_code2200;
1852                         risc_code_len = risc_code_length2200;
1853                 }
1854
1855                 for (i = 0; i < risc_code_len; i++) {
1856                         param[0] = MBOX_WRITE_RAM_WORD;
1857                         param[1] = risc_code_addr01 + i;
1858                         param[2] = risc_code[i];
1859
1860                         isp2x00_mbox_command(host, param);
1861
1862                         if (param[0] != MBOX_COMMAND_COMPLETE) {
1863                                 printk("qlogicfc%d : firmware load failure\n", hostdata->host_id);
1864                                 return 1;
1865                         }
1866                 }
1867         }
1868 #endif                          /* RELOAD_FIRMWARE */
1869
1870         param[0] = MBOX_VERIFY_CHECKSUM;
1871         param[1] = risc_code_addr01;
1872
1873         isp2x00_mbox_command(host, param);
1874
1875         if (param[0] != MBOX_COMMAND_COMPLETE) {
1876                 printk("qlogicfc%d : ram checksum failure\n", hostdata->host_id);
1877                 return 1;
1878         }
1879         DEBUG(printk("qlogicfc%d : executing firmware\n", hostdata->host_id));
1880
1881         param[0] = MBOX_EXEC_FIRMWARE;
1882         param[1] = risc_code_addr01;
1883
1884         isp2x00_mbox_command(host, param);
1885
1886         param[0] = MBOX_ABOUT_FIRMWARE;
1887
1888         isp2x00_mbox_command(host, param);
1889
1890         if (param[0] != MBOX_COMMAND_COMPLETE) {
1891                 printk("qlogicfc%d : about firmware failure\n", hostdata->host_id);
1892                 return 1;
1893         }
1894         DEBUG(printk("qlogicfc%d : firmware major revision %d\n", hostdata->host_id,  param[1]));
1895         DEBUG(printk("qlogicfc%d : firmware minor revision %d\n", hostdata->host_id,  param[2]));
1896
1897 #ifdef USE_NVRAM_DEFAULTS
1898
1899         if (isp2x00_get_nvram_defaults(host, &hostdata->control_block) != 0) {
1900                 printk("qlogicfc%d : Could not read from NVRAM\n", hostdata->host_id);
1901         }
1902 #endif
1903
1904         hostdata->wwn = (u64) (cpu_to_le16(hostdata->control_block.node_name[0])) << 56;
1905         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[0]) & 0xff00) << 48;
1906         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[1]) & 0xff00) << 24;
1907         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[1]) & 0x00ff) << 48;
1908         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[2]) & 0x00ff) << 24;
1909         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[2]) & 0xff00) << 8;
1910         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[3]) & 0x00ff) << 8;
1911         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[3]) & 0xff00) >> 8;
1912
1913         /* FIXME: If the DMA transfer goes one way only, this should use
1914          *        PCI_DMA_TODEVICE and below as well.
1915          */
1916         busaddr = pci_map_page(hostdata->pci_dev,
1917                                virt_to_page(&hostdata->control_block),
1918                                offset_in_page(&hostdata->control_block),
1919                                sizeof(hostdata->control_block),
1920                                PCI_DMA_BIDIRECTIONAL);
1921
1922         param[0] = MBOX_INIT_FIRMWARE;
1923         param[2] = (u_short) (pci64_dma_lo32(busaddr) >> 16);
1924         param[3] = (u_short) (pci64_dma_lo32(busaddr) & 0xffff);
1925         param[4] = 0;
1926         param[5] = 0;
1927         param[6] = (u_short) (pci64_dma_hi32(busaddr) >> 16);
1928         param[7] = (u_short) (pci64_dma_hi32(busaddr) & 0xffff);
1929         isp2x00_mbox_command(host, param);
1930         if (param[0] != MBOX_COMMAND_COMPLETE) {
1931                 printk("qlogicfc%d.c: Ouch 0x%04x\n", hostdata->host_id,  param[0]);
1932                 pci_unmap_page(hostdata->pci_dev, busaddr,
1933                                sizeof(hostdata->control_block),
1934                                PCI_DMA_BIDIRECTIONAL);
1935                 return 1;
1936         }
1937         param[0] = MBOX_GET_FIRMWARE_STATE;
1938         isp2x00_mbox_command(host, param);
1939         if (param[0] != MBOX_COMMAND_COMPLETE) {
1940                 printk("qlogicfc%d.c: 0x%04x\n", hostdata->host_id,  param[0]);
1941                 pci_unmap_page(hostdata->pci_dev, busaddr,
1942                                sizeof(hostdata->control_block),
1943                                PCI_DMA_BIDIRECTIONAL);
1944                 return 1;
1945         }
1946
1947         pci_unmap_page(hostdata->pci_dev, busaddr,
1948                        sizeof(hostdata->control_block),
1949                        PCI_DMA_BIDIRECTIONAL);
1950         LEAVE("isp2x00_reset_hardware");
1951
1952         return 0;
1953 }
1954
1955 #ifdef USE_NVRAM_DEFAULTS
1956
1957 static int isp2x00_get_nvram_defaults(struct Scsi_Host *host, struct init_cb *control_block)
1958 {
1959
1960         u_short value;
1961         if (isp2x00_read_nvram_word(host, 0) != 0x5349)
1962                 return 1;
1963
1964         value = isp2x00_read_nvram_word(host, 8);
1965         control_block->node_name[0] = cpu_to_le16(isp2x00_read_nvram_word(host, 9));
1966         control_block->node_name[1] = cpu_to_le16(isp2x00_read_nvram_word(host, 10));
1967         control_block->node_name[2] = cpu_to_le16(isp2x00_read_nvram_word(host, 11));
1968         control_block->node_name[3] = cpu_to_le16(isp2x00_read_nvram_word(host, 12));
1969         control_block->hard_addr = cpu_to_le16(isp2x00_read_nvram_word(host, 13));
1970
1971         return 0;
1972
1973 }
1974
1975 #endif
1976
1977 static int isp2x00_init(struct Scsi_Host *sh)
1978 {
1979         u_long io_base;
1980         struct isp2x00_hostdata *hostdata;
1981         u_char revision;
1982         u_int irq;
1983         u_short command;
1984         struct pci_dev *pdev;
1985
1986
1987         ENTER("isp2x00_init");
1988
1989         hostdata = (struct isp2x00_hostdata *) sh->hostdata;
1990         pdev = hostdata->pci_dev;
1991
1992         if (pci_read_config_word(pdev, PCI_COMMAND, &command)
1993           || pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision)) {
1994                 printk("qlogicfc%d : error reading PCI configuration\n", hostdata->host_id);
1995                 return 1;
1996         }
1997         io_base = pci_resource_start(pdev, 0);
1998         irq = pdev->irq;
1999
2000
2001         if (pdev->vendor != PCI_VENDOR_ID_QLOGIC) {
2002                 printk("qlogicfc%d : 0x%04x is not QLogic vendor ID\n", hostdata->host_id, 
2003                        pdev->vendor);
2004                 return 1;
2005         }
2006         if (pdev->device != PCI_DEVICE_ID_QLOGIC_ISP2100 && pdev->device != PCI_DEVICE_ID_QLOGIC_ISP2200) {
2007                 printk("qlogicfc%d : 0x%04x does not match ISP2100 or ISP2200 device id\n", hostdata->host_id, 
2008                        pdev->device);
2009                 return 1;
2010         }
2011         if (!(command & PCI_COMMAND_IO) ||
2012             !(pdev->resource[0].flags & IORESOURCE_IO)) {
2013                 printk("qlogicfc%d : i/o mapping is disabled\n", hostdata->host_id);
2014                 return 1;
2015         }
2016
2017         pci_set_master(pdev);
2018         if (revision != ISP2100_REV_ID1 && revision != ISP2100_REV_ID3 && revision != ISP2200_REV_ID5)
2019                 printk("qlogicfc%d : new isp2x00 revision ID (%d)\n", hostdata->host_id,  revision);
2020
2021
2022         hostdata->revision = revision;
2023
2024         sh->irq = irq;
2025         sh->io_port = io_base;
2026
2027         LEAVE("isp2x00_init");
2028
2029         return 0;
2030 }
2031
2032 #if USE_NVRAM_DEFAULTS
2033
2034 #define NVRAM_DELAY() udelay(10)        /* 10 microsecond delay */
2035
2036
2037 u_short isp2x00_read_nvram_word(struct Scsi_Host * host, u_short byte)
2038 {
2039         int i;
2040         u_short value, output, input;
2041
2042         outw(0x2, host->io_port + PCI_NVRAM);
2043         NVRAM_DELAY();
2044         outw(0x3, host->io_port + PCI_NVRAM);
2045         NVRAM_DELAY();
2046
2047         byte &= 0xff;
2048         byte |= 0x0600;
2049         for (i = 10; i >= 0; i--) {
2050                 output = ((byte >> i) & 0x1) ? 0x4 : 0x0;
2051                 outw(output | 0x2, host->io_port + PCI_NVRAM);
2052                 NVRAM_DELAY();
2053                 outw(output | 0x3, host->io_port + PCI_NVRAM);
2054                 NVRAM_DELAY();
2055                 outw(output | 0x2, host->io_port + PCI_NVRAM);
2056                 NVRAM_DELAY();
2057         }
2058
2059         for (i = 0xf, value = 0; i >= 0; i--) {
2060                 value <<= 1;
2061                 outw(0x3, host->io_port + PCI_NVRAM);
2062                 NVRAM_DELAY();
2063                 input = inw(host->io_port + PCI_NVRAM);
2064                 NVRAM_DELAY();
2065                 outw(0x2, host->io_port + PCI_NVRAM);
2066                 NVRAM_DELAY();
2067                 if (input & 0x8)
2068                         value |= 1;
2069         }
2070
2071         outw(0x0, host->io_port + PCI_NVRAM);
2072         NVRAM_DELAY();
2073
2074         return value;
2075 }
2076
2077
2078 #endif                          /* USE_NVRAM_DEFAULTS */
2079
2080
2081
2082 /*
2083  * currently, this is only called during initialization or abort/reset,
2084  * at which times interrupts are disabled, so polling is OK, I guess...
2085  */
2086 static int isp2x00_mbox_command(struct Scsi_Host *host, u_short param[])
2087 {
2088         int loop_count;
2089         struct isp2x00_hostdata *hostdata = (struct isp2x00_hostdata *) host->hostdata;
2090
2091         if (mbox_param[param[0]] == 0 || hostdata->adapter_state == AS_FIRMWARE_DEAD)
2092                 return 1;
2093
2094         loop_count = DEFAULT_LOOP_COUNT;
2095         while (--loop_count && inw(host->io_port + HOST_HCCR) & 0x0080) {
2096                 barrier();
2097                 cpu_relax();
2098         }
2099         if (!loop_count) {
2100                 printk("qlogicfc%d : mbox_command loop timeout #1\n", hostdata->host_id);
2101                 param[0] = 0x4006;
2102                 hostdata->adapter_state = AS_FIRMWARE_DEAD;
2103                 return 1;
2104         }
2105         hostdata->mbox_done = 0;
2106
2107         if (mbox_param[param[0]] == 0)
2108                 printk("qlogicfc%d : invalid mbox command\n", hostdata->host_id);
2109
2110         if (mbox_param[param[0]] & 0x80)
2111                 outw(param[7], host->io_port + MBOX7);
2112         if (mbox_param[param[0]] & 0x40)
2113                 outw(param[6], host->io_port + MBOX6);
2114         if (mbox_param[param[0]] & 0x20)
2115                 outw(param[5], host->io_port + MBOX5);
2116         if (mbox_param[param[0]] & 0x10)
2117                 outw(param[4], host->io_port + MBOX4);
2118         if (mbox_param[param[0]] & 0x08)
2119                 outw(param[3], host->io_port + MBOX3);
2120         if (mbox_param[param[0]] & 0x04)
2121                 outw(param[2], host->io_port + MBOX2);
2122         if (mbox_param[param[0]] & 0x02)
2123                 outw(param[1], host->io_port + MBOX1);
2124         if (mbox_param[param[0]] & 0x01)
2125                 outw(param[0], host->io_port + MBOX0);
2126
2127
2128         outw(HCCR_SET_HOST_INTR, host->io_port + HOST_HCCR);
2129
2130         while (1) {
2131                 loop_count = DEFAULT_LOOP_COUNT;
2132                 while (--loop_count && !(inw(host->io_port + PCI_INTER_STS) & 0x08)) { 
2133                         barrier();
2134                         cpu_relax();
2135                 }
2136
2137                 if (!loop_count) {
2138                         hostdata->adapter_state = AS_FIRMWARE_DEAD;
2139                         printk("qlogicfc%d : mbox_command loop timeout #2\n", hostdata->host_id);
2140                         break;
2141                 }
2142                 isp2x00_intr_handler(host->irq, host, NULL);
2143
2144                 if (hostdata->mbox_done == 1)
2145                         break;
2146
2147         }
2148
2149         loop_count = DEFAULT_LOOP_COUNT;
2150         while (--loop_count && inw(host->io_port + MBOX0) == 0x04) {
2151                 barrier();
2152                 cpu_relax();
2153         }
2154         if (!loop_count)
2155                 printk("qlogicfc%d : mbox_command loop timeout #3\n", hostdata->host_id);
2156
2157         param[7] = inw(host->io_port + MBOX7);
2158         param[6] = inw(host->io_port + MBOX6);
2159         param[5] = inw(host->io_port + MBOX5);
2160         param[4] = inw(host->io_port + MBOX4);
2161         param[3] = inw(host->io_port + MBOX3);
2162         param[2] = inw(host->io_port + MBOX2);
2163         param[1] = inw(host->io_port + MBOX1);
2164         param[0] = inw(host->io_port + MBOX0);
2165
2166
2167         outw(0x0, host->io_port + PCI_SEMAPHORE);
2168
2169         if (inw(host->io_port + HOST_HCCR) & 0x0080) {
2170                 hostdata->adapter_state = AS_FIRMWARE_DEAD;
2171                 printk("qlogicfc%d : mbox op is still pending\n", hostdata->host_id);
2172         }
2173         return 0;
2174 }
2175
2176 #if DEBUG_ISP2x00_INTR
2177
2178 void isp2x00_print_status_entry(struct Status_Entry *status)
2179 {
2180         printk("qlogicfc : entry count = 0x%02x, type = 0x%02x, flags = 0x%02x\n", 
2181         status->hdr.entry_cnt, status->hdr.entry_type, status->hdr.flags);
2182         printk("qlogicfc : scsi status = 0x%04x, completion status = 0x%04x\n",
2183                le16_to_cpu(status->scsi_status), le16_to_cpu(status->completion_status));
2184         printk("qlogicfc : state flags = 0x%04x, status flags = 0x%04x\n", 
2185                le16_to_cpu(status->state_flags), le16_to_cpu(status->status_flags));
2186         printk("qlogicfc : response info length = 0x%04x, request sense length = 0x%04x\n",
2187                le16_to_cpu(status->res_info_len), le16_to_cpu(status->req_sense_len));
2188         printk("qlogicfc : residual transfer length = 0x%08x, response = 0x%02x\n", le32_to_cpu(status->residual), status->res_info[3]);
2189
2190 }
2191
2192 #endif                         /* DEBUG_ISP2x00_INTR */
2193
2194
2195 #if DEBUG_ISP2x00
2196
2197 void isp2x00_print_scsi_cmd(Scsi_Cmnd * cmd)
2198 {
2199         int i;
2200
2201         printk("qlogicfc : target = 0x%02x, lun = 0x%02x, cmd_len = 0x%02x\n", 
2202                cmd->target, cmd->lun, cmd->cmd_len);
2203         printk("qlogicfc : command = ");
2204         for (i = 0; i < cmd->cmd_len; i++)
2205                 printk("0x%02x ", cmd->cmnd[i]);
2206         printk("\n");
2207 }
2208
2209 #endif                          /* DEBUG_ISP2x00 */
2210
2211 MODULE_LICENSE("GPL");
2212
2213 static struct scsi_host_template driver_template = {
2214         .detect                 = isp2x00_detect,
2215         .release                = isp2x00_release,
2216         .info                   = isp2x00_info,
2217         .queuecommand           = isp2x00_queuecommand,
2218         .eh_abort_handler       = isp2x00_abort,
2219         .bios_param             = isp2x00_biosparam,
2220         .can_queue              = QLOGICFC_REQ_QUEUE_LEN,
2221         .this_id                = -1,
2222         .sg_tablesize           = QLOGICFC_MAX_SG(QLOGICFC_REQ_QUEUE_LEN),
2223         .cmd_per_lun            = QLOGICFC_CMD_PER_LUN,
2224         .use_clustering         = ENABLE_CLUSTERING,
2225 };
2226 #include "scsi_module.c"