vserver 2.0 rc7
[linux-2.6.git] / 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(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                         scsi_set_device(host, &pdev->dev);
750                         host->max_id = QLOGICFC_MAX_ID + 1;
751                         host->max_lun = QLOGICFC_MAX_LUN;
752                         hostdata = (struct isp2x00_hostdata *) host->hostdata;
753
754                         memset(hostdata, 0, sizeof(struct isp2x00_hostdata));
755                         hostdata->pci_dev = pdev;
756                         hostdata->res = pci_alloc_consistent(pdev, RES_SIZE + REQ_SIZE, &busaddr);
757
758                         if (!hostdata->res){
759                                 printk("qlogicfc%d : could not allocate memory for request and response queue.\n", hosts);
760                                 scsi_unregister(host);
761                                 continue;
762                         }
763                         hostdata->req = hostdata->res + (RES_QUEUE_LEN + 1)*QUEUE_ENTRY_LEN;
764                         hostdata->queued = 0;
765                         /* set up the control block */
766                         hostdata->control_block.version = 0x1;
767                         hostdata->control_block.firm_opts = cpu_to_le16(0x800e);
768                         hostdata->control_block.max_frame_len = cpu_to_le16(2048);
769                         hostdata->control_block.max_iocb = cpu_to_le16(QLOGICFC_REQ_QUEUE_LEN);
770                         hostdata->control_block.exec_throttle = cpu_to_le16(QLOGICFC_CMD_PER_LUN);
771                         hostdata->control_block.retry_delay = 5;
772                         hostdata->control_block.retry_cnt = 1;
773                         hostdata->control_block.node_name[0] = cpu_to_le16(0x0020);
774                         hostdata->control_block.node_name[1] = cpu_to_le16(0xE000);
775                         hostdata->control_block.node_name[2] = cpu_to_le16(0x008B);
776                         hostdata->control_block.node_name[3] = cpu_to_le16(0x0000);
777                         hostdata->control_block.hard_addr = cpu_to_le16(0x0003);
778                         hostdata->control_block.req_queue_len = cpu_to_le16(QLOGICFC_REQ_QUEUE_LEN + 1);
779                         hostdata->control_block.res_queue_len = cpu_to_le16(RES_QUEUE_LEN + 1);
780                         hostdata->control_block.res_queue_addr_lo = cpu_to_le32(pci64_dma_lo32(busaddr));
781                         hostdata->control_block.res_queue_addr_high = cpu_to_le32(pci64_dma_hi32(busaddr));
782                         hostdata->control_block.req_queue_addr_lo = cpu_to_le32(pci64_dma_lo32(busaddr + RES_SIZE));
783                         hostdata->control_block.req_queue_addr_high = cpu_to_le32(pci64_dma_hi32(busaddr + RES_SIZE));
784
785
786                         hostdata->control_block.add_firm_opts |= cpu_to_le16(CONNECTION_PREFERENCE<<4);
787                         hostdata->adapter_state = AS_LOOP_DOWN;
788                         hostdata->explore_timer.data = 1;
789                         hostdata->host_id = hosts;
790
791                         if (isp2x00_init(host) || isp2x00_reset_hardware(host)) {
792                                 pci_free_consistent (pdev, RES_SIZE + REQ_SIZE, hostdata->res, busaddr);
793                                 scsi_unregister(host);
794                                 continue;
795                         }
796                         host->this_id = 0;
797
798                         if (request_irq(host->irq, do_isp2x00_intr_handler, SA_INTERRUPT | SA_SHIRQ, "qlogicfc", host)) {
799                                 printk("qlogicfc%d : interrupt %d already in use\n",
800                                        hostdata->host_id, host->irq);
801                                 pci_free_consistent (pdev, RES_SIZE + REQ_SIZE, hostdata->res, busaddr);
802                                 scsi_unregister(host);
803                                 continue;
804                         }
805                         if (!request_region(host->io_port, 0xff, "qlogicfc")) {
806                                 printk("qlogicfc%d : i/o region 0x%lx-0x%lx already "
807                                        "in use\n",
808                                        hostdata->host_id, host->io_port, host->io_port + 0xff);
809                                 free_irq(host->irq, host);
810                                 pci_free_consistent (pdev, RES_SIZE + REQ_SIZE, hostdata->res, busaddr);
811                                 scsi_unregister(host);
812                                 continue;
813                         }
814
815                         outw(0x0, host->io_port + PCI_SEMAPHORE);
816                         outw(HCCR_CLEAR_RISC_INTR, host->io_port + HOST_HCCR);
817                         isp2x00_enable_irqs(host);
818                         /* wait for the loop to come up */
819                         for (wait_time = jiffies + 10 * HZ; time_before(jiffies, wait_time) && hostdata->adapter_state == AS_LOOP_DOWN;) {
820                                 barrier();
821                                 cpu_relax();
822                         }
823                         if (hostdata->adapter_state == AS_LOOP_DOWN) {
824                                 printk("qlogicfc%d : link is not up\n", hostdata->host_id);
825                         }
826                         hosts++;
827                         hostdata->explore_timer.data = 0;
828                 }
829         }
830
831
832         /* this busy loop should not be needed but the isp2x00 seems to need 
833            some time before recognizing it is attached to a fabric */
834
835 #if ISP2x00_FABRIC
836         if (hosts) {
837                 for (wait_time = jiffies + 5 * HZ; time_before(jiffies, wait_time);) {
838                         barrier();
839                         cpu_relax();
840                 }
841         }
842 #endif
843
844         LEAVE("isp2x00_detect");
845
846         return hosts;
847 }
848
849
850 static int isp2x00_make_portdb(struct Scsi_Host *host)
851 {
852
853         short param[8];
854         int i, j;
855         struct isp2x00_hostdata *hostdata;
856
857         isp2x00_disable_irqs(host);
858
859         hostdata = (struct isp2x00_hostdata *) host->hostdata;
860         memset(hostdata->tempmap, 0, sizeof(hostdata->tempmap));
861
862 #if ISP2x00_FABRIC
863         for (i = 0x81; i < QLOGICFC_MAX_ID; i++) {
864                 param[0] = MBOX_PORT_LOGOUT;
865                 param[1] = i << 8;
866                 param[2] = 0;
867                 param[3] = 0;
868
869                 isp2x00_mbox_command(host, param);
870
871                 if (param[0] != MBOX_COMMAND_COMPLETE) {
872
873                         DEBUG_FABRIC(printk("qlogicfc%d : logout failed %x  %x\n", hostdata->host_id, i, param[0]));
874                 }
875         }
876 #endif
877
878
879         param[0] = MBOX_GET_INIT_SCSI_ID;
880
881         isp2x00_mbox_command(host, param);
882
883         if (param[0] == MBOX_COMMAND_COMPLETE) {
884                 hostdata->port_id = ((u_int) param[3]) << 16;
885                 hostdata->port_id |= param[2];
886                 hostdata->tempmap[0].loop_id = param[1];
887                 hostdata->tempmap[0].wwn = hostdata->wwn;
888         }
889         else {
890                 printk("qlogicfc%d : error getting scsi id.\n", hostdata->host_id);
891         }
892
893         for (i = 0; i <=QLOGICFC_MAX_ID; i++)
894                 hostdata->tempmap[i].loop_id = hostdata->tempmap[0].loop_id;
895    
896         for (i = 0, j = 1; i <= QLOGICFC_MAX_LOOP_ID; i++) {
897                 param[0] = MBOX_GET_PORT_NAME;
898                 param[1] = (i << 8) & 0xff00;
899
900                 isp2x00_mbox_command(host, param);
901
902                 if (param[0] == MBOX_COMMAND_COMPLETE) {
903                         hostdata->tempmap[j].loop_id = i;
904                         hostdata->tempmap[j].wwn = ((u64) (param[2] & 0xff)) << 56;
905                         hostdata->tempmap[j].wwn |= ((u64) ((param[2] >> 8) & 0xff)) << 48;
906                         hostdata->tempmap[j].wwn |= ((u64) (param[3] & 0xff)) << 40;
907                         hostdata->tempmap[j].wwn |= ((u64) ((param[3] >> 8) & 0xff)) << 32;
908                         hostdata->tempmap[j].wwn |= ((u64) (param[6] & 0xff)) << 24;
909                         hostdata->tempmap[j].wwn |= ((u64) ((param[6] >> 8) & 0xff)) << 16;
910                         hostdata->tempmap[j].wwn |= ((u64) (param[7] & 0xff)) << 8;
911                         hostdata->tempmap[j].wwn |= ((u64) ((param[7] >> 8) & 0xff));
912
913                         j++;
914
915                 }
916         }
917
918
919 #if ISP2x00_FABRIC
920         isp2x00_init_fabric(host, hostdata->tempmap, j);
921 #endif
922
923         for (i = 0; i <= QLOGICFC_MAX_ID; i++) {
924                 if (hostdata->tempmap[i].wwn != hostdata->port_db[i].wwn) {
925                         for (j = 0; j <= QLOGICFC_MAX_ID; j++) {
926                                 if (hostdata->tempmap[j].wwn == hostdata->port_db[i].wwn) {
927                                         hostdata->port_db[i].loop_id = hostdata->tempmap[j].loop_id;
928                                         break;
929                                 }
930                         }
931                         if (j == QLOGICFC_MAX_ID + 1)
932                                 hostdata->port_db[i].loop_id = hostdata->tempmap[0].loop_id;
933
934                         for (j = 0; j <= QLOGICFC_MAX_ID; j++) {
935                                 if (hostdata->port_db[j].wwn == hostdata->tempmap[i].wwn || !hostdata->port_db[j].wwn) {
936                                         break;
937                                 }
938                         }
939                         if (j == QLOGICFC_MAX_ID + 1)
940                                 printk("qlogicfc%d : Too many scsi devices, no more room in port map.\n", hostdata->host_id);
941                         if (!hostdata->port_db[j].wwn) {
942                                 hostdata->port_db[j].loop_id = hostdata->tempmap[i].loop_id;
943                                 hostdata->port_db[j].wwn = hostdata->tempmap[i].wwn;
944                         }
945                 } else
946                         hostdata->port_db[i].loop_id = hostdata->tempmap[i].loop_id;
947
948         }
949
950         isp2x00_enable_irqs(host);
951
952         return 0;
953 }
954
955
956 #if ISP2x00_FABRIC
957
958 #define FABRIC_PORT          0x7e
959 #define FABRIC_CONTROLLER    0x7f
960 #define FABRIC_SNS           0x80
961
962 int isp2x00_init_fabric(struct Scsi_Host *host, struct id_name_map *port_db, int cur_scsi_id)
963 {
964
965         u_short param[8];
966         u64 wwn;
967         int done = 0;
968         u_short loop_id = 0x81;
969         u_short scsi_id = cur_scsi_id;
970         u_int port_id;
971         struct sns_cb *req;
972         u_char *sns_response;
973         dma_addr_t busaddr;
974         struct isp2x00_hostdata *hostdata;
975
976         hostdata = (struct isp2x00_hostdata *) host->hostdata;
977         
978         DEBUG_FABRIC(printk("qlogicfc%d : Checking for a fabric.\n", hostdata->host_id));
979         param[0] = MBOX_GET_PORT_NAME;
980         param[1] = (u16)FABRIC_PORT << 8;
981
982         isp2x00_mbox_command(host, param);
983
984         if (param[0] != MBOX_COMMAND_COMPLETE) {
985                 DEBUG_FABRIC(printk("qlogicfc%d : fabric check result %x\n", hostdata->host_id, param[0]));
986                 return 0;
987         }
988         printk("qlogicfc%d : Fabric found.\n", hostdata->host_id);
989
990         req = (struct sns_cb *)pci_alloc_consistent(hostdata->pci_dev, sizeof(*req) + 608, &busaddr);
991         
992         if (!req){
993                 printk("qlogicfc%d : Could not allocate DMA resources for fabric initialization\n", hostdata->host_id);
994                 return 0;
995         }
996         sns_response = (u_char *)(req + 1);
997
998         if (hostdata->adapter_state & AS_REDO_LOOP_PORTDB){
999                 memset(req, 0, sizeof(*req));
1000         
1001                 req->len = cpu_to_le16(8);
1002                 req->response_low = cpu_to_le32(pci64_dma_lo32(busaddr + sizeof(*req)));
1003                 req->response_high = cpu_to_le32(pci64_dma_hi32(busaddr + sizeof(*req)));
1004                 req->sub_len = cpu_to_le16(22);
1005                 req->data[0] = 0x17;
1006                 req->data[1] = 0x02;
1007                 req->data[8] = (u_char) (hostdata->port_id & 0xff);
1008                 req->data[9] = (u_char) (hostdata->port_id >> 8 & 0xff);
1009                 req->data[10] = (u_char) (hostdata->port_id >> 16 & 0xff);
1010                 req->data[13] = 0x01;
1011                 param[0] = MBOX_SEND_SNS;
1012                 param[1] = 30;
1013                 param[2] = pci64_dma_lo32(busaddr) >> 16;
1014                 param[3] = pci64_dma_lo32(busaddr);
1015                 param[6] = pci64_dma_hi32(busaddr) >> 16;
1016                 param[7] = pci64_dma_hi32(busaddr);
1017
1018                 isp2x00_mbox_command(host, param);
1019         
1020                 if (param[0] != MBOX_COMMAND_COMPLETE)
1021                         printk("qlogicfc%d : error sending RFC-4\n", hostdata->host_id);
1022         }
1023
1024         port_id = hostdata->port_id;
1025         while (!done) {
1026                 memset(req, 0, sizeof(*req));
1027
1028                 req->len = cpu_to_le16(304);
1029                 req->response_low = cpu_to_le32(pci64_dma_lo32(busaddr + sizeof(*req)));
1030                 req->response_high = cpu_to_le32(pci64_dma_hi32(busaddr + sizeof(*req)));
1031                 req->sub_len = cpu_to_le16(6);
1032                 req->data[0] = 0x00;
1033                 req->data[1] = 0x01;
1034                 req->data[8] = (u_char) (port_id & 0xff);
1035                 req->data[9] = (u_char) (port_id >> 8 & 0xff);
1036                 req->data[10] = (u_char) (port_id >> 16 & 0xff);
1037
1038                 param[0] = MBOX_SEND_SNS;
1039                 param[1] = 14;
1040                 param[2] = pci64_dma_lo32(busaddr) >> 16;
1041                 param[3] = pci64_dma_lo32(busaddr);
1042                 param[6] = pci64_dma_hi32(busaddr) >> 16;
1043                 param[7] = pci64_dma_hi32(busaddr);
1044
1045                 isp2x00_mbox_command(host, param);
1046
1047                 if (param[0] == MBOX_COMMAND_COMPLETE) {
1048                         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]));
1049                         DEBUG_FABRIC(printk("  port id: %02x%02x%02x\n", sns_response[17], sns_response[18], sns_response[19]));
1050                         port_id = ((u_int) sns_response[17]) << 16;
1051                         port_id |= ((u_int) sns_response[18]) << 8;
1052                         port_id |= ((u_int) sns_response[19]);
1053                         wwn = ((u64) sns_response[20]) << 56;
1054                         wwn |= ((u64) sns_response[21]) << 48;
1055                         wwn |= ((u64) sns_response[22]) << 40;
1056                         wwn |= ((u64) sns_response[23]) << 32;
1057                         wwn |= ((u64) sns_response[24]) << 24;
1058                         wwn |= ((u64) sns_response[25]) << 16;
1059                         wwn |= ((u64) sns_response[26]) << 8;
1060                         wwn |= ((u64) sns_response[27]);
1061                         if (hostdata->port_id >> 8 != port_id >> 8) {
1062                                 DEBUG_FABRIC(printk("qlogicfc%d : adding a fabric port: %x\n", hostdata->host_id, port_id));
1063                                 param[0] = MBOX_PORT_LOGIN;
1064                                 param[1] = loop_id << 8;
1065                                 param[2] = (u_short) (port_id >> 16);
1066                                 param[3] = (u_short) (port_id);
1067
1068                                 isp2x00_mbox_command(host, param);
1069
1070                                 if (param[0] == MBOX_COMMAND_COMPLETE) {
1071                                         port_db[scsi_id].wwn = wwn;
1072                                         port_db[scsi_id].loop_id = loop_id;
1073                                         loop_id++;
1074                                         scsi_id++;
1075                                 } else {
1076                                         printk("qlogicfc%d : Error performing port login %x\n", hostdata->host_id, param[0]);
1077                                         DEBUG_FABRIC(printk("qlogicfc%d : loop_id: %x\n", hostdata->host_id, loop_id));
1078                                         param[0] = MBOX_PORT_LOGOUT;
1079                                         param[1] = loop_id << 8;
1080                                         param[2] = 0;
1081                                         param[3] = 0;
1082
1083                                         isp2x00_mbox_command(host, param);
1084                                         
1085                                 }
1086
1087                         }
1088                         if (hostdata->port_id == port_id)
1089                                 done = 1;
1090                 } else {
1091                         printk("qlogicfc%d : Get All Next failed %x.\n", hostdata->host_id, param[0]);
1092                         pci_free_consistent(hostdata->pci_dev, sizeof(*req) + 608, req, busaddr);
1093                         return 0;
1094                 }
1095         }
1096
1097         pci_free_consistent(hostdata->pci_dev, sizeof(*req) + 608, req, busaddr);
1098         return 1;
1099 }
1100
1101 #endif                          /* ISP2x00_FABRIC */
1102
1103
1104 static int isp2x00_release(struct Scsi_Host *host)
1105 {
1106         struct isp2x00_hostdata *hostdata;
1107         dma_addr_t busaddr;
1108
1109         ENTER("isp2x00_release");
1110
1111         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1112
1113         outw(0x0, host->io_port + PCI_INTER_CTL);
1114         free_irq(host->irq, host);
1115
1116         release_region(host->io_port, 0xff);
1117
1118         busaddr = pci64_dma_build(le32_to_cpu(hostdata->control_block.res_queue_addr_high),
1119                                   le32_to_cpu(hostdata->control_block.res_queue_addr_lo));
1120         pci_free_consistent(hostdata->pci_dev, RES_SIZE + REQ_SIZE, hostdata->res, busaddr);
1121
1122         LEAVE("isp2x00_release");
1123
1124         return 0;
1125 }
1126
1127
1128 static const char *isp2x00_info(struct Scsi_Host *host)
1129 {
1130         static char buf[80];
1131         struct isp2x00_hostdata *hostdata;
1132         ENTER("isp2x00_info");
1133
1134         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1135         sprintf(buf,
1136                 "QLogic ISP%04x SCSI on PCI bus %02x device %02x irq %d base 0x%lx",
1137                 hostdata->pci_dev->device, hostdata->pci_dev->bus->number, hostdata->pci_dev->devfn, host->irq,
1138                 host->io_port);
1139
1140
1141         LEAVE("isp2x00_info");
1142
1143         return buf;
1144 }
1145
1146
1147 /*
1148  * The middle SCSI layer ensures that queuecommand never gets invoked
1149  * concurrently with itself or the interrupt handler (though the
1150  * interrupt handler may call this routine as part of
1151  * request-completion handling).
1152  */
1153 static int isp2x00_queuecommand(Scsi_Cmnd * Cmnd, void (*done) (Scsi_Cmnd *))
1154 {
1155         int i, sg_count, n, num_free;
1156         u_int in_ptr, out_ptr;
1157         struct dataseg *ds;
1158         struct scatterlist *sg;
1159         struct Command_Entry *cmd;
1160         struct Continuation_Entry *cont;
1161         struct Scsi_Host *host;
1162         struct isp2x00_hostdata *hostdata;
1163
1164         ENTER("isp2x00_queuecommand");
1165
1166         host = Cmnd->device->host;
1167         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1168         Cmnd->scsi_done = done;
1169
1170         DEBUG(isp2x00_print_scsi_cmd(Cmnd));
1171
1172         if (hostdata->adapter_state & AS_REDO_FABRIC_PORTDB || hostdata->adapter_state & AS_REDO_LOOP_PORTDB) {
1173                 isp2x00_make_portdb(host);
1174                 hostdata->adapter_state = AS_LOOP_GOOD;
1175                 printk("qlogicfc%d : Port Database\n", hostdata->host_id);
1176                 for (i = 0; hostdata->port_db[i].wwn != 0; i++) {
1177                         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);
1178                         if (hostdata->port_db[i].loop_id != hostdata->port_db[0].loop_id || i == 0)
1179                                 printk("%x", hostdata->port_db[i].loop_id);
1180                         else
1181                                 printk("Not Available");
1182                         printk("\n");
1183                 }
1184         }
1185         if (hostdata->adapter_state == AS_FIRMWARE_DEAD) {
1186                 printk("qlogicfc%d : The firmware is dead, just return.\n", hostdata->host_id);
1187                 host->max_id = 0;
1188                 return 0;
1189         }
1190
1191         out_ptr = inw(host->io_port + MBOX4);
1192         in_ptr = hostdata->req_in_ptr;
1193
1194         DEBUG(printk("qlogicfc%d : request queue depth %d\n", hostdata->host_id,
1195                      REQ_QUEUE_DEPTH(in_ptr, out_ptr)));
1196
1197         cmd = (struct Command_Entry *) &hostdata->req[in_ptr*QUEUE_ENTRY_LEN];
1198         in_ptr = (in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN;
1199         if (in_ptr == out_ptr) {
1200                 DEBUG(printk("qlogicfc%d : request queue overflow\n", hostdata->host_id));
1201                 return 1;
1202         }
1203         if (hostdata->send_marker) {
1204                 struct Marker_Entry *marker;
1205
1206                 TRACE("queue marker", in_ptr, 0);
1207
1208                 DEBUG(printk("qlogicfc%d : adding marker entry\n", hostdata->host_id));
1209                 marker = (struct Marker_Entry *) cmd;
1210                 memset(marker, 0, sizeof(struct Marker_Entry));
1211
1212                 marker->hdr.entry_type = ENTRY_MARKER;
1213                 marker->hdr.entry_cnt = 1;
1214                 marker->modifier = SYNC_ALL;
1215
1216                 hostdata->send_marker = 0;
1217
1218                 if (((in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN) == out_ptr) {
1219                         outw(in_ptr, host->io_port + MBOX4);
1220                         hostdata->req_in_ptr = in_ptr;
1221                         DEBUG(printk("qlogicfc%d : request queue overflow\n", hostdata->host_id));
1222                         return 1;
1223                 }
1224                 cmd = (struct Command_Entry *) &hostdata->req[in_ptr*QUEUE_ENTRY_LEN];
1225                 in_ptr = (in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN;
1226         }
1227         TRACE("queue command", in_ptr, Cmnd);
1228
1229         memset(cmd, 0, sizeof(struct Command_Entry));
1230
1231         /* find a free handle mapping slot */
1232         for (i = in_ptr; i != (in_ptr - 1) && hostdata->handle_ptrs[i]; i = ((i + 1) % (QLOGICFC_REQ_QUEUE_LEN + 1)));
1233
1234         if (!hostdata->handle_ptrs[i]) {
1235                 cmd->handle = cpu_to_le32(i);
1236                 hostdata->handle_ptrs[i] = Cmnd;
1237                 hostdata->handle_serials[i] = Cmnd->serial_number;
1238         } else {
1239                 printk("qlogicfc%d : no handle slots, this should not happen.\n", hostdata->host_id);
1240                 printk("hostdata->queued is %x, in_ptr: %x\n", hostdata->queued, in_ptr);
1241                 for (i = 0; i <= QLOGICFC_REQ_QUEUE_LEN; i++){
1242                         if (!hostdata->handle_ptrs[i]){
1243                                 printk("slot %d has %p\n", i, hostdata->handle_ptrs[i]);
1244                         }
1245                 }
1246                 return 1;
1247         }
1248
1249         cmd->hdr.entry_type = ENTRY_COMMAND;
1250         cmd->hdr.entry_cnt = 1;
1251         cmd->target_lun = Cmnd->device->lun;
1252         cmd->expanded_lun = cpu_to_le16(Cmnd->device->lun);
1253 #if ISP2x00_PORTDB
1254         cmd->target_id = hostdata->port_db[Cmnd->device->id].loop_id;
1255 #else
1256         cmd->target_id = Cmnd->target;
1257 #endif
1258         cmd->total_byte_cnt = cpu_to_le32(Cmnd->request_bufflen);
1259         cmd->time_out = 0;
1260         memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
1261
1262         if (Cmnd->use_sg) {
1263                 sg = (struct scatterlist *) Cmnd->request_buffer;
1264                 sg_count = pci_map_sg(hostdata->pci_dev, sg, Cmnd->use_sg, Cmnd->sc_data_direction);
1265                 cmd->segment_cnt = cpu_to_le16(sg_count);
1266                 ds = cmd->dataseg;
1267                 /* fill in first two sg entries: */
1268                 n = sg_count;
1269                 if (n > DATASEGS_PER_COMMAND)
1270                         n = DATASEGS_PER_COMMAND;
1271
1272                 for (i = 0; i < n; i++) {
1273                         ds[i].d_base = cpu_to_le32(pci64_dma_lo32(sg_dma_address(sg)));
1274                         ds[i].d_base_hi = cpu_to_le32(pci64_dma_hi32(sg_dma_address(sg)));
1275                         ds[i].d_count = cpu_to_le32(sg_dma_len(sg));
1276                         ++sg;
1277                 }
1278                 sg_count -= DATASEGS_PER_COMMAND;
1279
1280                 while (sg_count > 0) {
1281                         ++cmd->hdr.entry_cnt;
1282                         cont = (struct Continuation_Entry *)
1283                             &hostdata->req[in_ptr*QUEUE_ENTRY_LEN];
1284                         memset(cont, 0, sizeof(struct Continuation_Entry));
1285                         in_ptr = (in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN;
1286                         if (in_ptr == out_ptr) {
1287                                 DEBUG(printk("qlogicfc%d : unexpected request queue overflow\n", hostdata->host_id));
1288                                 return 1;
1289                         }
1290                         TRACE("queue continuation", in_ptr, 0);
1291                         cont->hdr.entry_type = ENTRY_CONTINUATION;
1292                         ds = cont->dataseg;
1293                         n = sg_count;
1294                         if (n > DATASEGS_PER_CONT)
1295                                 n = DATASEGS_PER_CONT;
1296                         for (i = 0; i < n; ++i) {
1297                                 ds[i].d_base = cpu_to_le32(pci64_dma_lo32(sg_dma_address(sg)));
1298                                 ds[i].d_base_hi = cpu_to_le32(pci64_dma_hi32(sg_dma_address(sg)));
1299                                 ds[i].d_count = cpu_to_le32(sg_dma_len(sg));
1300                                 ++sg;
1301                         }
1302                         sg_count -= n;
1303                 }
1304         } else if (Cmnd->request_bufflen && Cmnd->sc_data_direction != PCI_DMA_NONE) {
1305                 struct page *page = virt_to_page(Cmnd->request_buffer);
1306                 unsigned long offset = offset_in_page(Cmnd->request_buffer);
1307                 dma_addr_t busaddr = pci_map_page(hostdata->pci_dev,
1308                                                   page, offset,
1309                                                   Cmnd->request_bufflen,
1310                                                   Cmnd->sc_data_direction);
1311                 Cmnd->SCp.dma_handle = busaddr;
1312
1313                 cmd->dataseg[0].d_base = cpu_to_le32(pci64_dma_lo32(busaddr));
1314                 cmd->dataseg[0].d_base_hi = cpu_to_le32(pci64_dma_hi32(busaddr));
1315                 cmd->dataseg[0].d_count = cpu_to_le32(Cmnd->request_bufflen);
1316                 cmd->segment_cnt = cpu_to_le16(1);
1317         } else {
1318                 cmd->dataseg[0].d_base = 0;
1319                 cmd->dataseg[0].d_base_hi = 0;
1320                 cmd->segment_cnt = cpu_to_le16(1); /* Shouldn't this be 0? */
1321         }
1322
1323         if (Cmnd->sc_data_direction == DMA_TO_DEVICE)
1324                 cmd->control_flags = cpu_to_le16(CFLAG_WRITE);
1325         else 
1326                 cmd->control_flags = cpu_to_le16(CFLAG_READ);
1327
1328         if (Cmnd->device->tagged_supported) {
1329                 if ((jiffies - hostdata->tag_ages[Cmnd->device->id]) > (2 * ISP_TIMEOUT)) {
1330                         cmd->control_flags |= cpu_to_le16(CFLAG_ORDERED_TAG);
1331                         hostdata->tag_ages[Cmnd->device->id] = jiffies;
1332                 } else
1333                         switch (Cmnd->tag) {
1334                         case HEAD_OF_QUEUE_TAG:
1335                                 cmd->control_flags |= cpu_to_le16(CFLAG_HEAD_TAG);
1336                                 break;
1337                         case ORDERED_QUEUE_TAG:
1338                                 cmd->control_flags |= cpu_to_le16(CFLAG_ORDERED_TAG);
1339                                 break;
1340                         default:
1341                                 cmd->control_flags |= cpu_to_le16(CFLAG_SIMPLE_TAG);
1342                                 break;
1343                 }
1344         }
1345         /*
1346          * TEST_UNIT_READY commands from scsi_scan will fail due to "overlapped
1347          * commands attempted" unless we setup at least a simple queue (midlayer 
1348          * will embelish this once it can do an INQUIRY command to the device)
1349          */
1350         else
1351                 cmd->control_flags |= cpu_to_le16(CFLAG_SIMPLE_TAG);
1352         outw(in_ptr, host->io_port + MBOX4);
1353         hostdata->req_in_ptr = in_ptr;
1354
1355         hostdata->queued++;
1356
1357         num_free = QLOGICFC_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr);
1358         num_free = (num_free > 2) ? num_free - 2 : 0;
1359        host->can_queue = host->host_busy + num_free;
1360         if (host->can_queue > QLOGICFC_REQ_QUEUE_LEN)
1361                 host->can_queue = QLOGICFC_REQ_QUEUE_LEN;
1362         host->sg_tablesize = QLOGICFC_MAX_SG(num_free);
1363
1364         LEAVE("isp2x00_queuecommand");
1365
1366         return 0;
1367 }
1368
1369
1370 /* we have received an event, such as a lip or an RSCN, which may mean that
1371  * our port database is incorrect so the port database must be recreated.
1372  */
1373 static void redo_port_db(unsigned long arg)
1374 {
1375
1376         struct Scsi_Host * host = (struct Scsi_Host *) arg;
1377         struct isp2x00_hostdata * hostdata;
1378         unsigned long flags;
1379         int i;
1380
1381         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1382         hostdata->explore_timer.data = 0;
1383         del_timer(&hostdata->explore_timer);
1384
1385         spin_lock_irqsave(host->host_lock, flags);
1386
1387         if (hostdata->adapter_state & AS_REDO_FABRIC_PORTDB || hostdata->adapter_state & AS_REDO_LOOP_PORTDB) {
1388                 isp2x00_make_portdb(host);
1389                 printk("qlogicfc%d : Port Database\n", hostdata->host_id);
1390                 for (i = 0; hostdata->port_db[i].wwn != 0; i++) {
1391                         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);
1392                         if (hostdata->port_db[i].loop_id != hostdata->port_db[0].loop_id || i == 0)
1393                                 printk("%x", hostdata->port_db[i].loop_id);
1394                         else
1395                                 printk("Not Available");
1396                         printk("\n");
1397                 }
1398                 
1399                 for (i = 0; i < QLOGICFC_REQ_QUEUE_LEN; i++){ 
1400                         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)){
1401                                 if (hostdata->port_db[hostdata->handle_ptrs[i]->device->id].loop_id != hostdata->port_db[0].loop_id){
1402                                         Scsi_Cmnd *Cmnd = hostdata->handle_ptrs[i];
1403
1404                                          if (Cmnd->use_sg)
1405                                                  pci_unmap_sg(hostdata->pci_dev,
1406                                                               (struct scatterlist *)Cmnd->buffer,
1407                                                               Cmnd->use_sg,
1408                                                               Cmnd->sc_data_direction);
1409                                          else if (Cmnd->request_bufflen &&
1410                                                   Cmnd->sc_data_direction != PCI_DMA_NONE) {
1411                                                  pci_unmap_page(hostdata->pci_dev,
1412                                                                 Cmnd->SCp.dma_handle,
1413                                                                 Cmnd->request_bufflen,
1414                                                                 Cmnd->sc_data_direction);
1415                                          }
1416
1417                                          hostdata->handle_ptrs[i]->result = DID_SOFT_ERROR << 16;
1418
1419                                          if (hostdata->handle_ptrs[i]->scsi_done){
1420                                            (*hostdata->handle_ptrs[i]->scsi_done) (hostdata->handle_ptrs[i]);
1421                                          }
1422                                          else printk("qlogicfc%d : done is null?\n", hostdata->host_id);
1423                                          hostdata->handle_ptrs[i] = NULL;
1424                                          hostdata->handle_serials[i] = 0;
1425                                 }
1426                         }
1427                 }
1428                 
1429                 hostdata->adapter_state = AS_LOOP_GOOD;
1430         }
1431
1432         spin_unlock_irqrestore(host->host_lock, flags);
1433
1434 }
1435
1436 #define ASYNC_EVENT_INTERRUPT   0x01
1437
1438 irqreturn_t do_isp2x00_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1439 {
1440         struct Scsi_Host *host = dev_id;
1441         unsigned long flags;
1442
1443         spin_lock_irqsave(host->host_lock, flags);
1444         isp2x00_intr_handler(irq, dev_id, regs);
1445         spin_unlock_irqrestore(host->host_lock, flags);
1446
1447         return IRQ_HANDLED;
1448 }
1449
1450 void isp2x00_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1451 {
1452         Scsi_Cmnd *Cmnd;
1453         struct Status_Entry *sts;
1454         struct Scsi_Host *host = dev_id;
1455         struct isp2x00_hostdata *hostdata;
1456         u_int in_ptr, out_ptr, handle, num_free;
1457         u_short status;
1458
1459         ENTER_INTR("isp2x00_intr_handler");
1460
1461         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1462
1463         DEBUG_INTR(printk("qlogicfc%d : interrupt on line %d\n", hostdata->host_id, irq));
1464
1465         if (!(inw(host->io_port + PCI_INTER_STS) & 0x08)) {
1466                 /* spurious interrupts can happen legally */
1467                 DEBUG_INTR(printk("qlogicfc%d : got spurious interrupt\n", hostdata->host_id));
1468                 return;
1469         }
1470         in_ptr = inw(host->io_port + MBOX5);
1471         out_ptr = hostdata->res_out_ptr;
1472
1473         if ((inw(host->io_port + PCI_SEMAPHORE) & ASYNC_EVENT_INTERRUPT)) {
1474                 status = inw(host->io_port + MBOX0);
1475
1476                 DEBUG_INTR(printk("qlogicfc%d : mbox completion status: %x\n",
1477                                   hostdata->host_id, status));
1478
1479                 switch (status) {
1480                 case LOOP_UP:
1481                 case POINT_TO_POINT_UP:
1482                         printk("qlogicfc%d : Link is Up\n", hostdata->host_id);
1483                         hostdata->adapter_state = AS_REDO_FABRIC_PORTDB | AS_REDO_LOOP_PORTDB;
1484                         break;
1485                 case LOOP_DOWN:
1486                         printk("qlogicfc%d : Link is Down\n", hostdata->host_id);
1487                         hostdata->adapter_state = AS_LOOP_DOWN;
1488                         break;
1489                 case CONNECTION_MODE:
1490                         printk("received CONNECTION_MODE irq %x\n", inw(host->io_port + MBOX1));
1491                         break;
1492                 case CHANGE_NOTIFICATION:
1493                         printk("qlogicfc%d : RSCN Received\n", hostdata->host_id);
1494                         if (hostdata->adapter_state == AS_LOOP_GOOD)
1495                                 hostdata->adapter_state = AS_REDO_FABRIC_PORTDB;
1496                         break;                  
1497                 case LIP_OCCURRED:
1498                 case LIP_RECEIVED:
1499                         printk("qlogicfc%d : Loop Reinitialized\n", hostdata->host_id);
1500                         if (hostdata->adapter_state == AS_LOOP_GOOD)
1501                                 hostdata->adapter_state = AS_REDO_LOOP_PORTDB;
1502                         break;
1503                 case SYSTEM_ERROR:
1504                         printk("qlogicfc%d : The firmware just choked.\n", hostdata->host_id);
1505                         hostdata->adapter_state = AS_FIRMWARE_DEAD;
1506                         break;
1507                 case SCSI_COMMAND_COMPLETE:
1508                         handle = inw(host->io_port + MBOX1) | (inw(host->io_port + MBOX2) << 16);
1509                         Cmnd = hostdata->handle_ptrs[handle];
1510                         hostdata->handle_ptrs[handle] = NULL;
1511                         hostdata->handle_serials[handle] = 0;
1512                         hostdata->queued--;
1513                         if (Cmnd != NULL) {
1514                                 if (Cmnd->use_sg)
1515                                         pci_unmap_sg(hostdata->pci_dev,
1516                                                      (struct scatterlist *)Cmnd->buffer,
1517                                                      Cmnd->use_sg,
1518                                                      Cmnd->sc_data_direction);
1519                                 else if (Cmnd->request_bufflen &&
1520                                          Cmnd->sc_data_direction != PCI_DMA_NONE)
1521                                         pci_unmap_page(hostdata->pci_dev,
1522                                                        Cmnd->SCp.dma_handle,
1523                                                        Cmnd->request_bufflen,
1524                                                        Cmnd->sc_data_direction);
1525                                 Cmnd->result = 0x0;
1526                                 (*Cmnd->scsi_done) (Cmnd);
1527                         } else
1528                                 printk("qlogicfc%d.c : got a null value out of handle_ptrs, this sucks\n", hostdata->host_id);
1529                         break;
1530                 case MBOX_COMMAND_COMPLETE:
1531                 case INVALID_COMMAND:
1532                 case HOST_INTERFACE_ERROR:
1533                 case TEST_FAILED:
1534                 case COMMAND_ERROR:
1535                 case COMMAND_PARAM_ERROR:
1536                 case PORT_ID_USED:
1537                 case LOOP_ID_USED:
1538                 case ALL_IDS_USED:
1539                         hostdata->mbox_done = 1;
1540                         outw(HCCR_CLEAR_RISC_INTR, host->io_port + HOST_HCCR);
1541                         return;
1542                 default:
1543                         printk("qlogicfc%d : got an unknown status? %x\n", hostdata->host_id, status);
1544                 }
1545                 if ((hostdata->adapter_state & AS_REDO_LOOP_PORTDB || hostdata->adapter_state & AS_REDO_FABRIC_PORTDB) && hostdata->explore_timer.data == 0){
1546                         hostdata->explore_timer.function = redo_port_db;
1547                         hostdata->explore_timer.data = (unsigned long)host;
1548                         hostdata->explore_timer.expires = jiffies + (HZ/4);
1549                         init_timer(&hostdata->explore_timer);
1550                         add_timer(&hostdata->explore_timer);
1551                 }
1552                 outw(0x0, host->io_port + PCI_SEMAPHORE);
1553         } else {
1554                 DEBUG_INTR(printk("qlogicfc%d : response queue update\n", hostdata->host_id));
1555                 DEBUG_INTR(printk("qlogicfc%d : response queue depth %d\n", hostdata->host_id, RES_QUEUE_DEPTH(in_ptr, out_ptr)));
1556
1557                 while (out_ptr != in_ptr) {
1558                         unsigned le_hand;
1559                         sts = (struct Status_Entry *) &hostdata->res[out_ptr*QUEUE_ENTRY_LEN];
1560                         out_ptr = (out_ptr + 1) & RES_QUEUE_LEN;
1561                  
1562                         TRACE("done", out_ptr, Cmnd);
1563                         DEBUG_INTR(isp2x00_print_status_entry(sts));
1564                         le_hand = le32_to_cpu(sts->handle);
1565                         if (sts->hdr.entry_type == ENTRY_STATUS && (Cmnd = hostdata->handle_ptrs[le_hand])) {
1566                                 Cmnd->result = isp2x00_return_status(Cmnd, sts);
1567                                 hostdata->queued--;
1568
1569                                 if (Cmnd->use_sg)
1570                                         pci_unmap_sg(hostdata->pci_dev,
1571                                                      (struct scatterlist *)Cmnd->buffer, Cmnd->use_sg,
1572                                                      Cmnd->sc_data_direction);
1573                                 else if (Cmnd->request_bufflen && Cmnd->sc_data_direction != PCI_DMA_NONE)
1574                                         pci_unmap_page(hostdata->pci_dev,
1575                                                        Cmnd->SCp.dma_handle,
1576                                                        Cmnd->request_bufflen,
1577                                                        Cmnd->sc_data_direction);
1578
1579                                 /* 
1580                                  * if any of the following are true we do not
1581                                  * call scsi_done.  if the status is CS_ABORTED
1582                                  * we don't have to call done because the upper
1583                                  * level should already know its aborted.
1584                                  */
1585                                 if (hostdata->handle_serials[le_hand] != Cmnd->serial_number 
1586                                     || le16_to_cpu(sts->completion_status) == CS_ABORTED){
1587                                         hostdata->handle_serials[le_hand] = 0;
1588                                         hostdata->handle_ptrs[le_hand] = NULL;
1589                                         outw(out_ptr, host->io_port + MBOX5);
1590                                         continue;
1591                                 }
1592                                 /*
1593                                  * if we get back an error indicating the port
1594                                  * is not there or if the link is down and 
1595                                  * this is a device that used to be there 
1596                                  * allow the command to timeout.
1597                                  * the device may well be back in a couple of
1598                                  * seconds.
1599                                  */
1600                                 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){
1601                                         outw(out_ptr, host->io_port + MBOX5);
1602                                         continue;
1603                                 }
1604                         } else {
1605                                 outw(out_ptr, host->io_port + MBOX5);
1606                                 continue;
1607                         }
1608
1609                         hostdata->handle_ptrs[le_hand] = NULL;
1610
1611                         if (sts->completion_status == cpu_to_le16(CS_RESET_OCCURRED)
1612                             || (sts->status_flags & cpu_to_le16(STF_BUS_RESET)))
1613                                 hostdata->send_marker = 1;
1614
1615                         if (le16_to_cpu(sts->scsi_status) & 0x0200)
1616                                 memcpy(Cmnd->sense_buffer, sts->req_sense_data,
1617                                        sizeof(Cmnd->sense_buffer));
1618
1619                         outw(out_ptr, host->io_port + MBOX5);
1620
1621                         if (Cmnd->scsi_done != NULL) {
1622                                 (*Cmnd->scsi_done) (Cmnd);
1623                         } else
1624                                 printk("qlogicfc%d : Ouch, scsi done is NULL\n", hostdata->host_id);
1625                 }
1626                 hostdata->res_out_ptr = out_ptr;
1627         }
1628
1629
1630         out_ptr = inw(host->io_port + MBOX4);
1631         in_ptr = hostdata->req_in_ptr;
1632
1633         num_free = QLOGICFC_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr);
1634         num_free = (num_free > 2) ? num_free - 2 : 0;
1635        host->can_queue = host->host_busy + num_free;
1636         if (host->can_queue > QLOGICFC_REQ_QUEUE_LEN)
1637                 host->can_queue = QLOGICFC_REQ_QUEUE_LEN;
1638         host->sg_tablesize = QLOGICFC_MAX_SG(num_free);
1639
1640         outw(HCCR_CLEAR_RISC_INTR, host->io_port + HOST_HCCR);
1641         LEAVE_INTR("isp2x00_intr_handler");
1642 }
1643
1644
1645 static int isp2x00_return_status(Scsi_Cmnd *Cmnd, struct Status_Entry *sts)
1646 {
1647         int host_status = DID_ERROR;
1648 #if DEBUG_ISP2x00_INTR
1649         static char *reason[] =
1650         {
1651                 "DID_OK",
1652                 "DID_NO_CONNECT",
1653                 "DID_BUS_BUSY",
1654                 "DID_TIME_OUT",
1655                 "DID_BAD_TARGET",
1656                 "DID_ABORT",
1657                 "DID_PARITY",
1658                 "DID_ERROR",
1659                 "DID_RESET",
1660                 "DID_BAD_INTR"
1661         };
1662 #endif                          /* DEBUG_ISP2x00_INTR */
1663
1664         ENTER("isp2x00_return_status");
1665
1666         DEBUG(printk("qlogicfc : completion status = 0x%04x\n",
1667                      le16_to_cpu(sts->completion_status)));
1668
1669         switch (le16_to_cpu(sts->completion_status)) {
1670         case CS_COMPLETE:
1671                 host_status = DID_OK;
1672                 break;
1673         case CS_DMA_ERROR:
1674                 host_status = DID_ERROR;
1675                 break;
1676         case CS_RESET_OCCURRED:
1677                 host_status = DID_RESET;
1678                 break;
1679         case CS_ABORTED:
1680                 host_status = DID_ABORT;
1681                 break;
1682         case CS_TIMEOUT:
1683                 host_status = DID_TIME_OUT;
1684                 break;
1685         case CS_DATA_OVERRUN:
1686                 host_status = DID_ERROR;
1687                 break;
1688         case CS_DATA_UNDERRUN:
1689                 if (Cmnd->underflow <= (Cmnd->request_bufflen - le32_to_cpu(sts->residual)))
1690                         host_status = DID_OK;
1691                 else
1692                         host_status = DID_ERROR;
1693                 break;
1694         case CS_PORT_UNAVAILABLE:
1695         case CS_PORT_LOGGED_OUT:
1696         case CS_PORT_CONFIG_CHANGED:
1697                 host_status = DID_BAD_TARGET;
1698                 break;
1699         case CS_QUEUE_FULL:
1700                 host_status = DID_ERROR;
1701                 break;
1702         default:
1703                 printk("qlogicfc : unknown completion status 0x%04x\n",
1704                        le16_to_cpu(sts->completion_status));
1705                 host_status = DID_ERROR;
1706                 break;
1707         }
1708
1709         DEBUG_INTR(printk("qlogicfc : host status (%s) scsi status %x\n",
1710                           reason[host_status], le16_to_cpu(sts->scsi_status)));
1711
1712         LEAVE("isp2x00_return_status");
1713
1714         return (le16_to_cpu(sts->scsi_status) & STATUS_MASK) | (host_status << 16);
1715 }
1716
1717
1718 static int isp2x00_abort(Scsi_Cmnd * Cmnd)
1719 {
1720         u_short param[8];
1721         int i;
1722         struct Scsi_Host *host;
1723         struct isp2x00_hostdata *hostdata;
1724         int return_status = SUCCESS;
1725
1726         ENTER("isp2x00_abort");
1727
1728         host = Cmnd->device->host;
1729         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1730
1731         for (i = 0; i < QLOGICFC_REQ_QUEUE_LEN; i++)
1732                 if (hostdata->handle_ptrs[i] == Cmnd)
1733                         break;
1734
1735         if (i == QLOGICFC_REQ_QUEUE_LEN){
1736                 return SUCCESS;
1737         }
1738
1739         isp2x00_disable_irqs(host);
1740
1741         param[0] = MBOX_ABORT_IOCB;
1742 #if ISP2x00_PORTDB
1743         param[1] = (((u_short) hostdata->port_db[Cmnd->device->id].loop_id) << 8) | Cmnd->device->lun;
1744 #else
1745         param[1] = (((u_short) Cmnd->target) << 8) | Cmnd->lun;
1746 #endif
1747         param[2] = i & 0xffff;
1748         param[3] = i >> 16;
1749
1750         isp2x00_mbox_command(host, param);
1751
1752         if (param[0] != MBOX_COMMAND_COMPLETE) {
1753                 printk("qlogicfc%d : scsi abort failure: %x\n", hostdata->host_id, param[0]);
1754                 if (param[0] == 0x4005)
1755                         Cmnd->result = DID_ERROR << 16;
1756                 if (param[0] == 0x4006)
1757                         Cmnd->result = DID_BAD_TARGET << 16;
1758                 return_status = FAILED;
1759         }
1760
1761         if (return_status != SUCCESS){
1762                 param[0] = MBOX_GET_FIRMWARE_STATE;
1763                 isp2x00_mbox_command(host, param);
1764                 printk("qlogicfc%d : abort failed\n", hostdata->host_id);
1765                 printk("qlogicfc%d : firmware status is %x %x\n", hostdata->host_id, param[0], param[1]);
1766         }
1767
1768         isp2x00_enable_irqs(host);
1769
1770         LEAVE("isp2x00_abort");
1771
1772         return return_status;
1773 }
1774
1775
1776 static int isp2x00_biosparam(struct scsi_device *sdev, struct block_device *n,
1777                 sector_t capacity, int ip[])
1778 {
1779         int size = capacity;
1780
1781         ENTER("isp2x00_biosparam");
1782
1783         ip[0] = 64;
1784         ip[1] = 32;
1785         ip[2] = size >> 11;
1786         if (ip[2] > 1024) {
1787                 ip[0] = 255;
1788                 ip[1] = 63;
1789                 ip[2] = size / (ip[0] * ip[1]);
1790         }
1791         LEAVE("isp2x00_biosparam");
1792
1793         return 0;
1794 }
1795
1796 static int isp2x00_reset_hardware(struct Scsi_Host *host)
1797 {
1798         u_short param[8];
1799         struct isp2x00_hostdata *hostdata;
1800         int loop_count;
1801         dma_addr_t busaddr;
1802
1803         ENTER("isp2x00_reset_hardware");
1804
1805         hostdata = (struct isp2x00_hostdata *) host->hostdata;
1806
1807         /*
1808          *      This cannot be right - PCI writes are posted
1809          *      (apparently this is hardware design flaw not software ?)
1810          */
1811          
1812         outw(0x01, host->io_port + ISP_CTRL_STATUS);
1813         udelay(100);
1814         outw(HCCR_RESET, host->io_port + HOST_HCCR);
1815         udelay(100);
1816         outw(HCCR_RELEASE, host->io_port + HOST_HCCR);
1817         outw(HCCR_BIOS_DISABLE, host->io_port + HOST_HCCR);
1818
1819         loop_count = DEFAULT_LOOP_COUNT;
1820         while (--loop_count && inw(host->io_port + HOST_HCCR) == RISC_BUSY) {
1821                 barrier();
1822                 cpu_relax();
1823         }
1824         if (!loop_count)
1825                 printk("qlogicfc%d : reset_hardware loop timeout\n", hostdata->host_id);
1826
1827
1828
1829 #if DEBUG_ISP2x00
1830         printk("qlogicfc%d : mbox 0 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX0));
1831         printk("qlogicfc%d : mbox 1 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX1));
1832         printk("qlogicfc%d : mbox 2 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX2));
1833         printk("qlogicfc%d : mbox 3 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX3));
1834         printk("qlogicfc%d : mbox 4 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX4));
1835         printk("qlogicfc%d : mbox 5 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX5));
1836         printk("qlogicfc%d : mbox 6 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX6));
1837         printk("qlogicfc%d : mbox 7 0x%04x \n", hostdata->host_id,  inw(host->io_port + MBOX7));
1838 #endif                          /* DEBUG_ISP2x00 */
1839
1840         DEBUG(printk("qlogicfc%d : verifying checksum\n", hostdata->host_id));
1841
1842 #if defined(CONFIG_SCSI_QLOGIC_FC_FIRMWARE)
1843         {
1844                 int i;
1845                 unsigned short * risc_code = NULL;
1846                 unsigned short risc_code_len = 0;
1847                 if (hostdata->pci_dev->device == PCI_DEVICE_ID_QLOGIC_ISP2100){
1848                         risc_code = risc_code2100;
1849                         risc_code_len = risc_code_length2100;
1850                 }
1851                 else if (hostdata->pci_dev->device == PCI_DEVICE_ID_QLOGIC_ISP2200){
1852                         risc_code = risc_code2200;
1853                         risc_code_len = risc_code_length2200;
1854                 }
1855
1856                 for (i = 0; i < risc_code_len; i++) {
1857                         param[0] = MBOX_WRITE_RAM_WORD;
1858                         param[1] = risc_code_addr01 + i;
1859                         param[2] = risc_code[i];
1860
1861                         isp2x00_mbox_command(host, param);
1862
1863                         if (param[0] != MBOX_COMMAND_COMPLETE) {
1864                                 printk("qlogicfc%d : firmware load failure\n", hostdata->host_id);
1865                                 return 1;
1866                         }
1867                 }
1868         }
1869 #endif                          /* RELOAD_FIRMWARE */
1870
1871         param[0] = MBOX_VERIFY_CHECKSUM;
1872         param[1] = risc_code_addr01;
1873
1874         isp2x00_mbox_command(host, param);
1875
1876         if (param[0] != MBOX_COMMAND_COMPLETE) {
1877                 printk("qlogicfc%d : ram checksum failure\n", hostdata->host_id);
1878                 return 1;
1879         }
1880         DEBUG(printk("qlogicfc%d : executing firmware\n", hostdata->host_id));
1881
1882         param[0] = MBOX_EXEC_FIRMWARE;
1883         param[1] = risc_code_addr01;
1884
1885         isp2x00_mbox_command(host, param);
1886
1887         param[0] = MBOX_ABOUT_FIRMWARE;
1888
1889         isp2x00_mbox_command(host, param);
1890
1891         if (param[0] != MBOX_COMMAND_COMPLETE) {
1892                 printk("qlogicfc%d : about firmware failure\n", hostdata->host_id);
1893                 return 1;
1894         }
1895         DEBUG(printk("qlogicfc%d : firmware major revision %d\n", hostdata->host_id,  param[1]));
1896         DEBUG(printk("qlogicfc%d : firmware minor revision %d\n", hostdata->host_id,  param[2]));
1897
1898 #ifdef USE_NVRAM_DEFAULTS
1899
1900         if (isp2x00_get_nvram_defaults(host, &hostdata->control_block) != 0) {
1901                 printk("qlogicfc%d : Could not read from NVRAM\n", hostdata->host_id);
1902         }
1903 #endif
1904
1905         hostdata->wwn = (u64) (cpu_to_le16(hostdata->control_block.node_name[0])) << 56;
1906         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[0]) & 0xff00) << 48;
1907         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[1]) & 0xff00) << 24;
1908         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[1]) & 0x00ff) << 48;
1909         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[2]) & 0x00ff) << 24;
1910         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[2]) & 0xff00) << 8;
1911         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[3]) & 0x00ff) << 8;
1912         hostdata->wwn |= (u64) (cpu_to_le16(hostdata->control_block.node_name[3]) & 0xff00) >> 8;
1913
1914         /* FIXME: If the DMA transfer goes one way only, this should use
1915          *        PCI_DMA_TODEVICE and below as well.
1916          */
1917         busaddr = pci_map_page(hostdata->pci_dev,
1918                                virt_to_page(&hostdata->control_block),
1919                                offset_in_page(&hostdata->control_block),
1920                                sizeof(hostdata->control_block),
1921                                PCI_DMA_BIDIRECTIONAL);
1922
1923         param[0] = MBOX_INIT_FIRMWARE;
1924         param[2] = (u_short) (pci64_dma_lo32(busaddr) >> 16);
1925         param[3] = (u_short) (pci64_dma_lo32(busaddr) & 0xffff);
1926         param[4] = 0;
1927         param[5] = 0;
1928         param[6] = (u_short) (pci64_dma_hi32(busaddr) >> 16);
1929         param[7] = (u_short) (pci64_dma_hi32(busaddr) & 0xffff);
1930         isp2x00_mbox_command(host, param);
1931         if (param[0] != MBOX_COMMAND_COMPLETE) {
1932                 printk("qlogicfc%d.c: Ouch 0x%04x\n", hostdata->host_id,  param[0]);
1933                 pci_unmap_page(hostdata->pci_dev, busaddr,
1934                                sizeof(hostdata->control_block),
1935                                PCI_DMA_BIDIRECTIONAL);
1936                 return 1;
1937         }
1938         param[0] = MBOX_GET_FIRMWARE_STATE;
1939         isp2x00_mbox_command(host, param);
1940         if (param[0] != MBOX_COMMAND_COMPLETE) {
1941                 printk("qlogicfc%d.c: 0x%04x\n", hostdata->host_id,  param[0]);
1942                 pci_unmap_page(hostdata->pci_dev, busaddr,
1943                                sizeof(hostdata->control_block),
1944                                PCI_DMA_BIDIRECTIONAL);
1945                 return 1;
1946         }
1947
1948         pci_unmap_page(hostdata->pci_dev, busaddr,
1949                        sizeof(hostdata->control_block),
1950                        PCI_DMA_BIDIRECTIONAL);
1951         LEAVE("isp2x00_reset_hardware");
1952
1953         return 0;
1954 }
1955
1956 #ifdef USE_NVRAM_DEFAULTS
1957
1958 static int isp2x00_get_nvram_defaults(struct Scsi_Host *host, struct init_cb *control_block)
1959 {
1960
1961         u_short value;
1962         if (isp2x00_read_nvram_word(host, 0) != 0x5349)
1963                 return 1;
1964
1965         value = isp2x00_read_nvram_word(host, 8);
1966         control_block->node_name[0] = cpu_to_le16(isp2x00_read_nvram_word(host, 9));
1967         control_block->node_name[1] = cpu_to_le16(isp2x00_read_nvram_word(host, 10));
1968         control_block->node_name[2] = cpu_to_le16(isp2x00_read_nvram_word(host, 11));
1969         control_block->node_name[3] = cpu_to_le16(isp2x00_read_nvram_word(host, 12));
1970         control_block->hard_addr = cpu_to_le16(isp2x00_read_nvram_word(host, 13));
1971
1972         return 0;
1973
1974 }
1975
1976 #endif
1977
1978 static int isp2x00_init(struct Scsi_Host *sh)
1979 {
1980         u_long io_base;
1981         struct isp2x00_hostdata *hostdata;
1982         u_char revision;
1983         u_int irq;
1984         u_short command;
1985         struct pci_dev *pdev;
1986
1987
1988         ENTER("isp2x00_init");
1989
1990         hostdata = (struct isp2x00_hostdata *) sh->hostdata;
1991         pdev = hostdata->pci_dev;
1992
1993         if (pci_read_config_word(pdev, PCI_COMMAND, &command)
1994           || pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision)) {
1995                 printk("qlogicfc%d : error reading PCI configuration\n", hostdata->host_id);
1996                 return 1;
1997         }
1998         io_base = pci_resource_start(pdev, 0);
1999         irq = pdev->irq;
2000
2001
2002         if (pdev->vendor != PCI_VENDOR_ID_QLOGIC) {
2003                 printk("qlogicfc%d : 0x%04x is not QLogic vendor ID\n", hostdata->host_id, 
2004                        pdev->vendor);
2005                 return 1;
2006         }
2007         if (pdev->device != PCI_DEVICE_ID_QLOGIC_ISP2100 && pdev->device != PCI_DEVICE_ID_QLOGIC_ISP2200) {
2008                 printk("qlogicfc%d : 0x%04x does not match ISP2100 or ISP2200 device id\n", hostdata->host_id, 
2009                        pdev->device);
2010                 return 1;
2011         }
2012         if (!(command & PCI_COMMAND_IO) ||
2013             !(pdev->resource[0].flags & IORESOURCE_IO)) {
2014                 printk("qlogicfc%d : i/o mapping is disabled\n", hostdata->host_id);
2015                 return 1;
2016         }
2017
2018         pci_set_master(pdev);
2019         if (revision != ISP2100_REV_ID1 && revision != ISP2100_REV_ID3 && revision != ISP2200_REV_ID5)
2020                 printk("qlogicfc%d : new isp2x00 revision ID (%d)\n", hostdata->host_id,  revision);
2021
2022
2023         hostdata->revision = revision;
2024
2025         sh->irq = irq;
2026         sh->io_port = io_base;
2027
2028         LEAVE("isp2x00_init");
2029
2030         return 0;
2031 }
2032
2033 #if USE_NVRAM_DEFAULTS
2034
2035 #define NVRAM_DELAY() udelay(10)        /* 10 microsecond delay */
2036
2037
2038 u_short isp2x00_read_nvram_word(struct Scsi_Host * host, u_short byte)
2039 {
2040         int i;
2041         u_short value, output, input;
2042
2043         outw(0x2, host->io_port + PCI_NVRAM);
2044         NVRAM_DELAY();
2045         outw(0x3, host->io_port + PCI_NVRAM);
2046         NVRAM_DELAY();
2047
2048         byte &= 0xff;
2049         byte |= 0x0600;
2050         for (i = 10; i >= 0; i--) {
2051                 output = ((byte >> i) & 0x1) ? 0x4 : 0x0;
2052                 outw(output | 0x2, host->io_port + PCI_NVRAM);
2053                 NVRAM_DELAY();
2054                 outw(output | 0x3, host->io_port + PCI_NVRAM);
2055                 NVRAM_DELAY();
2056                 outw(output | 0x2, host->io_port + PCI_NVRAM);
2057                 NVRAM_DELAY();
2058         }
2059
2060         for (i = 0xf, value = 0; i >= 0; i--) {
2061                 value <<= 1;
2062                 outw(0x3, host->io_port + PCI_NVRAM);
2063                 NVRAM_DELAY();
2064                 input = inw(host->io_port + PCI_NVRAM);
2065                 NVRAM_DELAY();
2066                 outw(0x2, host->io_port + PCI_NVRAM);
2067                 NVRAM_DELAY();
2068                 if (input & 0x8)
2069                         value |= 1;
2070         }
2071
2072         outw(0x0, host->io_port + PCI_NVRAM);
2073         NVRAM_DELAY();
2074
2075         return value;
2076 }
2077
2078
2079 #endif                          /* USE_NVRAM_DEFAULTS */
2080
2081
2082
2083 /*
2084  * currently, this is only called during initialization or abort/reset,
2085  * at which times interrupts are disabled, so polling is OK, I guess...
2086  */
2087 static int isp2x00_mbox_command(struct Scsi_Host *host, u_short param[])
2088 {
2089         int loop_count;
2090         struct isp2x00_hostdata *hostdata = (struct isp2x00_hostdata *) host->hostdata;
2091
2092         if (mbox_param[param[0]] == 0 || hostdata->adapter_state == AS_FIRMWARE_DEAD)
2093                 return 1;
2094
2095         loop_count = DEFAULT_LOOP_COUNT;
2096         while (--loop_count && inw(host->io_port + HOST_HCCR) & 0x0080) {
2097                 barrier();
2098                 cpu_relax();
2099         }
2100         if (!loop_count) {
2101                 printk("qlogicfc%d : mbox_command loop timeout #1\n", hostdata->host_id);
2102                 param[0] = 0x4006;
2103                 hostdata->adapter_state = AS_FIRMWARE_DEAD;
2104                 return 1;
2105         }
2106         hostdata->mbox_done = 0;
2107
2108         if (mbox_param[param[0]] == 0)
2109                 printk("qlogicfc%d : invalid mbox command\n", hostdata->host_id);
2110
2111         if (mbox_param[param[0]] & 0x80)
2112                 outw(param[7], host->io_port + MBOX7);
2113         if (mbox_param[param[0]] & 0x40)
2114                 outw(param[6], host->io_port + MBOX6);
2115         if (mbox_param[param[0]] & 0x20)
2116                 outw(param[5], host->io_port + MBOX5);
2117         if (mbox_param[param[0]] & 0x10)
2118                 outw(param[4], host->io_port + MBOX4);
2119         if (mbox_param[param[0]] & 0x08)
2120                 outw(param[3], host->io_port + MBOX3);
2121         if (mbox_param[param[0]] & 0x04)
2122                 outw(param[2], host->io_port + MBOX2);
2123         if (mbox_param[param[0]] & 0x02)
2124                 outw(param[1], host->io_port + MBOX1);
2125         if (mbox_param[param[0]] & 0x01)
2126                 outw(param[0], host->io_port + MBOX0);
2127
2128
2129         outw(HCCR_SET_HOST_INTR, host->io_port + HOST_HCCR);
2130
2131         while (1) {
2132                 loop_count = DEFAULT_LOOP_COUNT;
2133                 while (--loop_count && !(inw(host->io_port + PCI_INTER_STS) & 0x08)) { 
2134                         barrier();
2135                         cpu_relax();
2136                 }
2137
2138                 if (!loop_count) {
2139                         hostdata->adapter_state = AS_FIRMWARE_DEAD;
2140                         printk("qlogicfc%d : mbox_command loop timeout #2\n", hostdata->host_id);
2141                         break;
2142                 }
2143                 isp2x00_intr_handler(host->irq, host, NULL);
2144
2145                 if (hostdata->mbox_done == 1)
2146                         break;
2147
2148         }
2149
2150         loop_count = DEFAULT_LOOP_COUNT;
2151         while (--loop_count && inw(host->io_port + MBOX0) == 0x04) {
2152                 barrier();
2153                 cpu_relax();
2154         }
2155         if (!loop_count)
2156                 printk("qlogicfc%d : mbox_command loop timeout #3\n", hostdata->host_id);
2157
2158         param[7] = inw(host->io_port + MBOX7);
2159         param[6] = inw(host->io_port + MBOX6);
2160         param[5] = inw(host->io_port + MBOX5);
2161         param[4] = inw(host->io_port + MBOX4);
2162         param[3] = inw(host->io_port + MBOX3);
2163         param[2] = inw(host->io_port + MBOX2);
2164         param[1] = inw(host->io_port + MBOX1);
2165         param[0] = inw(host->io_port + MBOX0);
2166
2167
2168         outw(0x0, host->io_port + PCI_SEMAPHORE);
2169
2170         if (inw(host->io_port + HOST_HCCR) & 0x0080) {
2171                 hostdata->adapter_state = AS_FIRMWARE_DEAD;
2172                 printk("qlogicfc%d : mbox op is still pending\n", hostdata->host_id);
2173         }
2174         return 0;
2175 }
2176
2177 #if DEBUG_ISP2x00_INTR
2178
2179 void isp2x00_print_status_entry(struct Status_Entry *status)
2180 {
2181         printk("qlogicfc : entry count = 0x%02x, type = 0x%02x, flags = 0x%02x\n", 
2182         status->hdr.entry_cnt, status->hdr.entry_type, status->hdr.flags);
2183         printk("qlogicfc : scsi status = 0x%04x, completion status = 0x%04x\n",
2184                le16_to_cpu(status->scsi_status), le16_to_cpu(status->completion_status));
2185         printk("qlogicfc : state flags = 0x%04x, status flags = 0x%04x\n", 
2186                le16_to_cpu(status->state_flags), le16_to_cpu(status->status_flags));
2187         printk("qlogicfc : response info length = 0x%04x, request sense length = 0x%04x\n",
2188                le16_to_cpu(status->res_info_len), le16_to_cpu(status->req_sense_len));
2189         printk("qlogicfc : residual transfer length = 0x%08x, response = 0x%02x\n", le32_to_cpu(status->residual), status->res_info[3]);
2190
2191 }
2192
2193 #endif                         /* DEBUG_ISP2x00_INTR */
2194
2195
2196 #if DEBUG_ISP2x00
2197
2198 void isp2x00_print_scsi_cmd(Scsi_Cmnd * cmd)
2199 {
2200         int i;
2201
2202         printk("qlogicfc : target = 0x%02x, lun = 0x%02x, cmd_len = 0x%02x\n", 
2203                cmd->target, cmd->lun, cmd->cmd_len);
2204         printk("qlogicfc : command = ");
2205         for (i = 0; i < cmd->cmd_len; i++)
2206                 printk("0x%02x ", cmd->cmnd[i]);
2207         printk("\n");
2208 }
2209
2210 #endif                          /* DEBUG_ISP2x00 */
2211
2212 MODULE_LICENSE("GPL");
2213
2214 static Scsi_Host_Template driver_template = {
2215         .detect                 = isp2x00_detect,
2216         .release                = isp2x00_release,
2217         .info                   = isp2x00_info,
2218         .queuecommand           = isp2x00_queuecommand,
2219         .eh_abort_handler       = isp2x00_abort,
2220         .bios_param             = isp2x00_biosparam,
2221         .can_queue              = QLOGICFC_REQ_QUEUE_LEN,
2222         .this_id                = -1,
2223         .sg_tablesize           = QLOGICFC_MAX_SG(QLOGICFC_REQ_QUEUE_LEN),
2224         .cmd_per_lun            = QLOGICFC_CMD_PER_LUN,
2225         .use_clustering         = ENABLE_CLUSTERING,
2226 };
2227 #include "scsi_module.c"