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