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