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