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