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