File: /usr/src/linux/drivers/scsi/qlogicfc.c

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