Import 2.3.18pre1
[davej-history.git] / drivers / scsi / qlogicfc.c
blob7e209ac3ab7a338e2dbcee9f08e464e0d1e6be0f
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
24 * $Date: 1995/09/22 02:23:15 $
25 * $Revision: 0.5 $
27 * $Log: isp1020.c,v $
28 * Revision 0.5 1995/09/22 02:23:15 root
29 * do auto request sense
31 * Revision 0.4 1995/08/07 04:44:33 root
32 * supply firmware with driver.
33 * numerous bug fixes/general cleanup of code.
35 * Revision 0.3 1995/07/16 16:15:39 root
36 * added reset/abort code.
38 * Revision 0.2 1995/06/29 03:14:19 root
39 * fixed biosparam.
40 * added queue protocol.
42 * Revision 0.1 1995/06/25 01:55:45 root
43 * Initial release.
47 #include <linux/blk.h>
48 #include <linux/kernel.h>
49 #include <linux/string.h>
50 #include <linux/ioport.h>
51 #include <linux/sched.h>
52 #include <linux/types.h>
53 #include <linux/pci.h>
54 #include <linux/delay.h>
55 #include <linux/unistd.h>
56 #include <linux/spinlock.h>
57 #include <asm/io.h>
58 #include <asm/irq.h>
60 #include "sd.h"
61 #include "hosts.h"
62 #include "qlogicfc.h"
64 /* Configuration section **************************************************** */
66 /* Set the following macro to 1 to reload the ISP2x00's firmware. This is
67 version 1.15.37 of the isp2100's firmware and version 2.00.16 of the
68 isp2200's firmware.
71 #define RELOAD_FIRMWARE 1
73 #define USE_NVRAM_DEFAULTS 1
75 #define ISP2x00_PORTDB 1
77 /* Set the following to 1 to include fabric support, fabric support is
78 * currently not as well tested as the other aspects of the driver */
80 #define ISP2x00_FABRIC 0
82 /* Macros used for debugging */
84 #define DEBUG_ISP2x00 1
85 #define DEBUG_ISP2x00_INT 1
86 #define DEBUG_ISP2x00_INTR 1
87 #define DEBUG_ISP2x00_SETUP 1
89 #define DEBUG_ISP2x00_FABRIC 1
91 /* #define TRACE_ISP 1 */
94 #define DEFAULT_LOOP_COUNT 10000000
96 /* End Configuration section ************************************************ */
98 #include <linux/module.h>
100 #if TRACE_ISP
102 #define TRACE_BUF_LEN (32*1024)
104 struct {
105 u_long next;
106 struct {
107 u_long time;
108 u_int index;
109 u_int addr;
110 u_char *name;
111 } buf[TRACE_BUF_LEN];
112 } trace;
114 #define TRACE(w, i, a) \
116 unsigned long flags; \
118 save_flags(flags); \
119 cli(); \
120 trace.buf[trace.next].name = (w); \
121 trace.buf[trace.next].time = jiffies; \
122 trace.buf[trace.next].index = (i); \
123 trace.buf[trace.next].addr = (long) (a); \
124 trace.next = (trace.next + 1) & (TRACE_BUF_LEN - 1); \
125 restore_flags(flags); \
128 #else
129 #define TRACE(w, i, a)
130 #endif
132 #if DEBUG_ISP2x00_FABRIC
133 #define DEBUG_FABRIC(x) x
134 #else
135 #define DEBUG_FABRIC(x)
136 #endif /* DEBUG_ISP2x00_FABRIC */
139 #if DEBUG_ISP2x00
140 #define ENTER(x) printk("isp2x00 : entering %s()\n", x);
141 #define LEAVE(x) printk("isp2x00 : leaving %s()\n", x);
142 #define DEBUG(x) x
143 #else
144 #define ENTER(x)
145 #define LEAVE(x)
146 #define DEBUG(x)
147 #endif /* DEBUG_ISP2x00 */
149 #if DEBUG_ISP2x00_INTR
150 #define ENTER_INTR(x) printk("isp2x00 : entering %s()\n", x);
151 #define LEAVE_INTR(x) printk("isp2x00 : leaving %s()\n", x);
152 #define DEBUG_INTR(x) x
153 #else
154 #define ENTER_INTR(x)
155 #define LEAVE_INTR(x)
156 #define DEBUG_INTR(x)
157 #endif /* DEBUG ISP2x00_INTR */
160 #if BITS_PER_LONG > 32
161 #define virt_to_bus_low32(x) ((u32) (0xffffffff & virt_to_bus(x)))
162 #define virt_to_bus_high32(x) ((u32) (0xffffffff & (virt_to_bus(x)>>32)))
163 #define bus_to_virt_low32(x) ((u32) (0xffffffff & bus_to_virt(x)))
164 #define bus_to_virt_high32(x) ((u32) (0xffffffff & (bus_to_virt(x)>>32)))
165 #else
166 #define virt_to_bus_low32(x) virt_to_bus(x)
167 #define virt_to_bus_high32(x) 0x0
168 #define bus_to_virt_low32(x) bus_to_virt(x)
169 #define bus_to_virt_high32(x) 0x0
170 #endif
172 #define ISP2100_REV_ID1 1
173 #define ISP2100_REV_ID3 3
174 #define ISP2200_REV_ID5 5
176 /* host configuration and control registers */
177 #define HOST_HCCR 0xc0 /* host command and control */
179 /* pci bus interface registers */
180 #define FLASH_BIOS_ADDR 0x00
181 #define FLASH_BIOS_DATA 0x02
182 #define ISP_CTRL_STATUS 0x06 /* configuration register #1 */
183 #define PCI_INTER_CTL 0x08 /* pci interupt control */
184 #define PCI_INTER_STS 0x0a /* pci interupt status */
185 #define PCI_SEMAPHORE 0x0c /* pci semaphore */
186 #define PCI_NVRAM 0x0e /* pci nvram interface */
188 /* mailbox registers */
189 #define MBOX0 0x10 /* mailbox 0 */
190 #define MBOX1 0x12 /* mailbox 1 */
191 #define MBOX2 0x14 /* mailbox 2 */
192 #define MBOX3 0x16 /* mailbox 3 */
193 #define MBOX4 0x18 /* mailbox 4 */
194 #define MBOX5 0x1a /* mailbox 5 */
195 #define MBOX6 0x1c /* mailbox 6 */
196 #define MBOX7 0x1e /* mailbox 7 */
198 /* mailbox command complete status codes */
199 #define MBOX_COMMAND_COMPLETE 0x4000
200 #define INVALID_COMMAND 0x4001
201 #define HOST_INTERFACE_ERROR 0x4002
202 #define TEST_FAILED 0x4003
203 #define COMMAND_ERROR 0x4005
204 #define COMMAND_PARAM_ERROR 0x4006
205 #define PORT_ID_USED 0x4007
206 #define LOOP_ID_USED 0x4008
207 #define ALL_IDS_USED 0x4009
209 /* async event status codes */
210 #define RESET_DETECTED 0x8001
211 #define SYSTEM_ERROR 0x8002
212 #define REQUEST_TRANSFER_ERROR 0x8003
213 #define RESPONSE_TRANSFER_ERROR 0x8004
214 #define REQUEST_QUEUE_WAKEUP 0x8005
215 #define LIP_OCCURED 0x8010
216 #define LOOP_UP 0x8011
217 #define LOOP_DOWN 0x8012
218 #define LIP_RECEIVED 0x8013
219 #define PORT_DB_CHANGED 0x8014
220 #define CHANGE_NOTIFICATION 0x8015
221 #define SCSI_COMMAND_COMPLETE 0x8020
222 #define POINT_TO_POINT_UP 0x8030
223 #define CONNECTION_MODE 0x8036
225 struct Entry_header {
226 u_char entry_type;
227 u_char entry_cnt;
228 u_char sys_def_1;
229 u_char flags;
232 /* entry header type commands */
233 #if BITS_PER_LONG > 32
234 #define ENTRY_COMMAND 0x19
235 #define ENTRY_CONTINUATION 0x0a
236 #else
237 #define ENTRY_COMMAND 0x11
238 #define ENTRY_CONTINUATION 0x02
239 #endif
241 #define ENTRY_STATUS 0x03
242 #define ENTRY_MARKER 0x04
245 /* entry header flag definitions */
246 #define EFLAG_BUSY 2
247 #define EFLAG_BAD_HEADER 4
248 #define EFLAG_BAD_PAYLOAD 8
250 #if BITS_PER_LONG > 32
251 struct dataseg {
252 u_int d_base;
253 u_int d_base_hi;
254 u_int d_count;
257 struct Command_Entry {
258 struct Entry_header hdr;
259 u_int handle;
260 u_char target_lun;
261 u_char target_id;
262 u_short expanded_lun;
263 u_short control_flags;
264 u_short rsvd2;
265 u_short time_out;
266 u_short segment_cnt;
267 u_char cdb[16];
268 u_int total_byte_cnt;
269 struct dataseg dataseg[DATASEGS_PER_COMMAND];
272 #else
273 struct dataseg {
274 u_int d_base;
275 u_int d_count;
278 struct Command_Entry {
279 struct Entry_header hdr;
280 u_int handle;
281 u_char target_lun;
282 u_char target_id;
283 u_short expanded_lun;
284 u_short control_flags;
285 u_short rsvd2;
286 u_short time_out;
287 u_short segment_cnt;
288 u_char cdb[16];
289 u_int total_byte_cnt;
290 struct dataseg dataseg[DATASEGS_PER_COMMAND];
293 #endif
296 /* command entry control flag definitions */
297 #define CFLAG_NODISC 0x01
298 #define CFLAG_HEAD_TAG 0x02
299 #define CFLAG_ORDERED_TAG 0x04
300 #define CFLAG_SIMPLE_TAG 0x08
301 #define CFLAG_TAR_RTN 0x10
302 #define CFLAG_READ 0x20
303 #define CFLAG_WRITE 0x40
305 #if BITS_PER_LONG > 32
306 struct Continuation_Entry {
307 struct Entry_header hdr;
308 struct dataseg dataseg[DATASEGS_PER_CONT];
310 #else
311 struct Continuation_Entry {
312 struct Entry_header hdr;
313 u32 rsvd;
314 struct dataseg dataseg[DATASEGS_PER_CONT];
316 #endif
318 struct Marker_Entry {
319 struct Entry_header hdr;
320 u_int reserved;
321 u_char target_lun;
322 u_char target_id;
323 u_char modifier;
324 u_char expanded_lun;
325 u_char rsvds[52];
328 /* marker entry modifier definitions */
329 #define SYNC_DEVICE 0
330 #define SYNC_TARGET 1
331 #define SYNC_ALL 2
333 struct Status_Entry {
334 struct Entry_header hdr;
335 u_int handle;
336 u_short scsi_status;
337 u_short completion_status;
338 u_short state_flags;
339 u_short status_flags;
340 u_short res_info_len;
341 u_short req_sense_len;
342 u_int residual;
343 u_char res_info[8];
344 u_char req_sense_data[32];
347 /* status entry completion status definitions */
348 #define CS_COMPLETE 0x0000
349 #define CS_DMA_ERROR 0x0002
350 #define CS_RESET_OCCURRED 0x0004
351 #define CS_ABORTED 0x0005
352 #define CS_TIMEOUT 0x0006
353 #define CS_DATA_OVERRUN 0x0007
354 #define CS_DATA_UNDERRUN 0x0015
355 #define CS_QUEUE_FULL 0x001c
356 #define CS_PORT_UNAVAILABLE 0x0028
357 #define CS_PORT_LOGGED_OUT 0x0029
358 #define CS_PORT_CONFIG_CHANGED 0x002a
360 /* status entry state flag definitions */
361 #define SF_SENT_CDB 0x0400
362 #define SF_TRANSFERRED_DATA 0x0800
363 #define SF_GOT_STATUS 0x1000
365 /* status entry status flag definitions */
366 #define STF_BUS_RESET 0x0008
367 #define STF_DEVICE_RESET 0x0010
368 #define STF_ABORTED 0x0020
369 #define STF_TIMEOUT 0x0040
371 /* interupt control commands */
372 #define ISP_EN_INT 0x8000
373 #define ISP_EN_RISC 0x0008
375 /* host control commands */
376 #define HCCR_NOP 0x0000
377 #define HCCR_RESET 0x1000
378 #define HCCR_PAUSE 0x2000
379 #define HCCR_RELEASE 0x3000
380 #define HCCR_SINGLE_STEP 0x4000
381 #define HCCR_SET_HOST_INTR 0x5000
382 #define HCCR_CLEAR_HOST_INTR 0x6000
383 #define HCCR_CLEAR_RISC_INTR 0x7000
384 #define HCCR_BP_ENABLE 0x8000
385 #define HCCR_BIOS_DISABLE 0x9000
386 #define HCCR_TEST_MODE 0xf000
388 #define RISC_BUSY 0x0004
390 /* mailbox commands */
391 #define MBOX_NO_OP 0x0000
392 #define MBOX_LOAD_RAM 0x0001
393 #define MBOX_EXEC_FIRMWARE 0x0002
394 #define MBOX_DUMP_RAM 0x0003
395 #define MBOX_WRITE_RAM_WORD 0x0004
396 #define MBOX_READ_RAM_WORD 0x0005
397 #define MBOX_MAILBOX_REG_TEST 0x0006
398 #define MBOX_VERIFY_CHECKSUM 0x0007
399 #define MBOX_ABOUT_FIRMWARE 0x0008
400 #define MBOX_LOAD_RISC_RAM 0x0009
401 #define MBOX_DUMP_RISC_RAM 0x000a
402 #define MBOX_CHECK_FIRMWARE 0x000e
403 #define MBOX_INIT_REQ_QUEUE 0x0010
404 #define MBOX_INIT_RES_QUEUE 0x0011
405 #define MBOX_EXECUTE_IOCB 0x0012
406 #define MBOX_WAKE_UP 0x0013
407 #define MBOX_STOP_FIRMWARE 0x0014
408 #define MBOX_ABORT_IOCB 0x0015
409 #define MBOX_ABORT_DEVICE 0x0016
410 #define MBOX_ABORT_TARGET 0x0017
411 #define MBOX_BUS_RESET 0x0018
412 #define MBOX_STOP_QUEUE 0x0019
413 #define MBOX_START_QUEUE 0x001a
414 #define MBOX_SINGLE_STEP_QUEUE 0x001b
415 #define MBOX_ABORT_QUEUE 0x001c
416 #define MBOX_GET_DEV_QUEUE_STATUS 0x001d
417 #define MBOX_GET_FIRMWARE_STATUS 0x001f
418 #define MBOX_GET_INIT_SCSI_ID 0x0020
419 #define MBOX_GET_RETRY_COUNT 0x0022
420 #define MBOX_GET_TARGET_PARAMS 0x0028
421 #define MBOX_GET_DEV_QUEUE_PARAMS 0x0029
422 #define MBOX_SET_RETRY_COUNT 0x0032
423 #define MBOX_SET_TARGET_PARAMS 0x0038
424 #define MBOX_SET_DEV_QUEUE_PARAMS 0x0039
425 #define MBOX_EXECUTE_IOCB64 0x0054
426 #define MBOX_INIT_FIRMWARE 0x0060
427 #define MBOX_GET_INIT_CB 0x0061
428 #define MBOX_INIT_LIP 0x0062
429 #define MBOX_GET_POS_MAP 0x0063
430 #define MBOX_GET_PORT_DB 0x0064
431 #define MBOX_CLEAR_ACA 0x0065
432 #define MBOX_TARGET_RESET 0x0066
433 #define MBOX_CLEAR_TASK_SET 0x0067
434 #define MBOX_ABORT_TASK_SET 0x0068
435 #define MBOX_GET_FIRMWARE_STATE 0x0069
436 #define MBOX_GET_PORT_NAME 0x006a
437 #define MBOX_SEND_SNS 0x006e
438 #define MBOX_PORT_LOGIN 0x006f
439 #define MBOX_SEND_CHANGE_REQUEST 0x0070
440 #define MBOX_PORT_LOGOUT 0x0071
442 #include "qlogicfc_asm.c"
444 /* Each element in mbox_param is an 8 bit bitmap where each bit indicates
445 if that mbox should be copied as input. For example 0x2 would mean
446 only copy mbox1. */
448 const u_char mbox_param[] =
450 0x01, /* MBOX_NO_OP */
451 0x1f, /* MBOX_LOAD_RAM */
452 0x03, /* MBOX_EXEC_FIRMWARE */
453 0x1f, /* MBOX_DUMP_RAM */
454 0x07, /* MBOX_WRITE_RAM_WORD */
455 0x03, /* MBOX_READ_RAM_WORD */
456 0xff, /* MBOX_MAILBOX_REG_TEST */
457 0x03, /* MBOX_VERIFY_CHECKSUM */
458 0x01, /* MBOX_ABOUT_FIRMWARE */
459 0xff, /* MBOX_LOAD_RISC_RAM */
460 0xff, /* MBOX_DUMP_RISC_RAM */
461 0x00, /* 0x000b */
462 0x00, /* 0x000c */
463 0x00, /* 0x000d */
464 0x01, /* MBOX_CHECK_FIRMWARE */
465 0x00, /* 0x000f */
466 0x1f, /* MBOX_INIT_REQ_QUEUE */
467 0x2f, /* MBOX_INIT_RES_QUEUE */
468 0x0f, /* MBOX_EXECUTE_IOCB */
469 0x03, /* MBOX_WAKE_UP */
470 0x01, /* MBOX_STOP_FIRMWARE */
471 0x0f, /* MBOX_ABORT_IOCB */
472 0x03, /* MBOX_ABORT_DEVICE */
473 0x07, /* MBOX_ABORT_TARGET */
474 0x03, /* MBOX_BUS_RESET */
475 0x03, /* MBOX_STOP_QUEUE */
476 0x03, /* MBOX_START_QUEUE */
477 0x03, /* MBOX_SINGLE_STEP_QUEUE */
478 0x03, /* MBOX_ABORT_QUEUE */
479 0x03, /* MBOX_GET_DEV_QUEUE_STATUS */
480 0x00, /* 0x001e */
481 0x01, /* MBOX_GET_FIRMWARE_STATUS */
482 0x01, /* MBOX_GET_INIT_SCSI_ID */
483 0x00, /* 0x0021 */
484 0x01, /* MBOX_GET_RETRY_COUNT */
485 0x00, /* 0x0023 */
486 0x00, /* 0x0024 */
487 0x00, /* 0x0025 */
488 0x00, /* 0x0026 */
489 0x00, /* 0x0027 */
490 0x03, /* MBOX_GET_TARGET_PARAMS */
491 0x03, /* MBOX_GET_DEV_QUEUE_PARAMS */
492 0x00, /* 0x002a */
493 0x00, /* 0x002b */
494 0x00, /* 0x002c */
495 0x00, /* 0x002d */
496 0x00, /* 0x002e */
497 0x00, /* 0x002f */
498 0x00, /* 0x0030 */
499 0x00, /* 0x0031 */
500 0x07, /* MBOX_SET_RETRY_COUNT */
501 0x00, /* 0x0033 */
502 0x00, /* 0x0034 */
503 0x00, /* 0x0035 */
504 0x00, /* 0x0036 */
505 0x00, /* 0x0037 */
506 0x0f, /* MBOX_SET_TARGET_PARAMS */
507 0x0f, /* MBOX_SET_DEV_QUEUE_PARAMS */
508 0x00, /* 0x003a */
509 0x00, /* 0x003b */
510 0x00, /* 0x003c */
511 0x00, /* 0x003d */
512 0x00, /* 0x003e */
513 0x00, /* 0x003f */
514 0x00, /* 0x0040 */
515 0x00, /* 0x0041 */
516 0x00, /* 0x0042 */
517 0x00, /* 0x0043 */
518 0x00, /* 0x0044 */
519 0x00, /* 0x0045 */
520 0x00, /* 0x0046 */
521 0x00, /* 0x0047 */
522 0x00, /* 0x0048 */
523 0x00, /* 0x0049 */
524 0x00, /* 0x004a */
525 0x00, /* 0x004b */
526 0x00, /* 0x004c */
527 0x00, /* 0x004d */
528 0x00, /* 0x004e */
529 0x00, /* 0x004f */
530 0x00, /* 0x0050 */
531 0x00, /* 0x0051 */
532 0x00, /* 0x0052 */
533 0x00, /* 0x0053 */
534 0xcf, /* MBOX_EXECUTE_IOCB64 */
535 0x00, /* 0x0055 */
536 0x00, /* 0x0056 */
537 0x00, /* 0x0057 */
538 0x00, /* 0x0058 */
539 0x00, /* 0x0059 */
540 0x00, /* 0x005a */
541 0x00, /* 0x005b */
542 0x00, /* 0x005c */
543 0x00, /* 0x005d */
544 0x00, /* 0x005e */
545 0x00, /* 0x005f */
546 0xff, /* MBOX_INIT_FIRMWARE */
547 0xcd, /* MBOX_GET_INIT_CB */
548 0x01, /* MBOX_INIT_LIP */
549 0xcd, /* MBOX_GET_POS_MAP */
550 0xcf, /* MBOX_GET_PORT_DB */
551 0x03, /* MBOX_CLEAR_ACA */
552 0x03, /* MBOX_TARGET_RESET */
553 0x03, /* MBOX_CLEAR_TASK_SET */
554 0x03, /* MBOX_ABORT_TASK_SET */
555 0x01, /* MBOX_GET_FIRMWARE_STATE */
556 0x03, /* MBOX_GET_PORT_NAME */
557 0x00, /* 0x006b */
558 0x00, /* 0x006c */
559 0x00, /* 0x006d */
560 0xcf, /* MBOX_SEND_SNS */
561 0x0f, /* MBOX_PORT_LOGIN */
562 0x03, /* MBOX_SEND_CHANGE_REQUEST */
563 0x03, /* MBOX_PORT_LOGOUT */
566 #define MAX_MBOX_COMMAND (sizeof(mbox_param)/sizeof(u_short))
569 struct id_name_map {
570 u64 wwn;
571 u_char loop_id;
574 struct sns_cb {
575 u_short len;
576 u_short res1;
577 u_int response_low;
578 u_int response_high;
579 u_short sub_len;
580 u_short res2;
581 u_char data[44];
584 /* address of instance of this struct is passed to adapter to initialize things
586 struct init_cb {
587 u_char version;
588 u_char reseverd1[1];
589 u_short firm_opts;
590 u_short max_frame_len;
591 u_short max_iocb;
592 u_short exec_throttle;
593 u_char retry_cnt;
594 u_char retry_delay;
595 u_short node_name[4];
596 u_short hard_addr;
597 u_char reserved2[10];
598 u_short req_queue_out;
599 u_short res_queue_in;
600 u_short req_queue_len;
601 u_short res_queue_len;
602 u_int req_queue_addr_lo;
603 u_int req_queue_addr_high;
604 u_int res_queue_addr_lo;
605 u_int res_queue_addr_high;
606 /* the rest of this structure only applies to the isp2200 */
607 u_short lun_enables;
608 u_char cmd_resource_cnt;
609 u_char notify_resource_cnt;
610 u_short timeout;
611 u_short reserved3;
612 u_short add_firm_opts;
613 u_char res_accum_timer;
614 u_char irq_delay_timer;
615 u_short special_options;
616 u_short reserved4[13];
620 * The result queue can be quite a bit smaller since continuation entries
621 * do not show up there:
623 #define RES_QUEUE_LEN ((QLOGICFC_REQ_QUEUE_LEN + 1) / 8 - 1)
624 #define QUEUE_ENTRY_LEN 64
626 #if ISP2x00_FABRIC
627 #define QLOGICFC_MAX_ID 0xff
628 #else
629 #define QLOGICFC_MAX_ID 0x7d
630 #endif
632 #define QLOGICFC_MAX_LOOP_ID 0x7d
634 /* adapter_state values */
635 #define AS_FIRMWARE_DEAD -1
636 #define AS_LOOP_DOWN 0
637 #define AS_LOOP_GOOD 1
638 #define AS_REDO_FABRIC_PORTDB 2
639 #define AS_REDO_LOOP_PORTDB 4
641 struct isp2x00_hostdata {
642 u_char revision;
643 struct pci_dev *pci_dev;
644 /* result and request queues (shared with isp2x00): */
645 u_int req_in_ptr; /* index of next request slot */
646 u_int res_out_ptr; /* index of next result slot */
648 /* this is here so the queues are nicely aligned */
649 long send_marker; /* do we need to send a marker? */
651 char res[RES_QUEUE_LEN + 1][QUEUE_ENTRY_LEN];
652 char req[QLOGICFC_REQ_QUEUE_LEN + 1][QUEUE_ENTRY_LEN];
653 struct init_cb control_block;
654 int adapter_state;
655 unsigned long int tag_ages[126];
656 Scsi_Cmnd *handle_ptrs[QLOGICFC_REQ_QUEUE_LEN + 1];
657 unsigned long handle_serials[QLOGICFC_REQ_QUEUE_LEN + 1];
658 struct id_name_map port_db[QLOGICFC_MAX_ID + 1];
659 u_char mbox_done;
660 u64 wwn;
661 u_int port_id;
662 u_char queued;
663 u_char host_id;
667 /* queue length's _must_ be power of two: */
668 #define QUEUE_DEPTH(in, out, ql) ((in - out) & (ql))
669 #define REQ_QUEUE_DEPTH(in, out) QUEUE_DEPTH(in, out, \
670 QLOGICFC_REQ_QUEUE_LEN)
671 #define RES_QUEUE_DEPTH(in, out) QUEUE_DEPTH(in, out, RES_QUEUE_LEN)
673 static void isp2x00_enable_irqs(struct Scsi_Host *);
674 static void isp2x00_disable_irqs(struct Scsi_Host *);
675 static int isp2x00_init(struct Scsi_Host *);
676 static int isp2x00_reset_hardware(struct Scsi_Host *);
677 static int isp2x00_mbox_command(struct Scsi_Host *, u_short[]);
678 static int isp2x00_return_status(struct Status_Entry *);
679 static void isp2x00_intr_handler(int, void *, struct pt_regs *);
680 static void do_isp2x00_intr_handler(int, void *, struct pt_regs *);
681 static int isp2x00_make_portdb(struct Scsi_Host *);
683 #if ISP2x00_FABRIC
684 static int isp2x00_init_fabric(struct Scsi_Host *, struct id_name_map *, int);
685 #endif
687 #if USE_NVRAM_DEFAULTS
688 static int isp2x00_get_nvram_defaults(struct Scsi_Host *, struct init_cb *);
689 static u_short isp2x00_read_nvram_word(struct Scsi_Host *, u_short);
690 #endif
692 #if DEBUG_ISP2x00
693 static void isp2x00_print_scsi_cmd(Scsi_Cmnd *);
694 #endif
696 #if DEBUG_ISP2x00_INTR
697 static void isp2x00_print_status_entry(struct Status_Entry *);
698 #endif
700 static struct proc_dir_entry proc_scsi_isp2x00 =
702 PROC_SCSI_QLOGICFC, 7, "isp2x00",
703 S_IFDIR | S_IRUGO | S_IXUGO, 2
707 static inline void isp2x00_enable_irqs(struct Scsi_Host *host)
709 outw(ISP_EN_INT | ISP_EN_RISC, host->io_port + PCI_INTER_CTL);
713 static inline void isp2x00_disable_irqs(struct Scsi_Host *host)
715 outw(0x0, host->io_port + PCI_INTER_CTL);
719 int isp2x00_detect(Scsi_Host_Template * tmpt)
721 int hosts = 0;
722 int wait_time;
723 struct Scsi_Host *host = NULL;
724 struct isp2x00_hostdata *hostdata;
725 struct pci_dev *pdev = NULL;
726 unsigned short device_ids[2];
727 int i;
730 ENTER("isp2x00_detect");
732 device_ids[0] = PCI_DEVICE_ID_QLOGIC_ISP2100;
733 device_ids[1] = PCI_DEVICE_ID_QLOGIC_ISP2200;
735 tmpt->proc_dir = &proc_scsi_isp2x00;
737 if (pci_present() == 0) {
738 printk("qlogicfc : PCI not present\n");
739 return 0;
742 for (i=0; i<2; i++){
743 while ((pdev = pci_find_device(PCI_VENDOR_ID_QLOGIC, device_ids[i], pdev))) {
745 host = scsi_register(tmpt, sizeof(struct isp2x00_hostdata));
746 host->max_id = QLOGICFC_MAX_ID + 1;
747 host->hostt->use_new_eh_code = 1;
748 hostdata = (struct isp2x00_hostdata *) host->hostdata;
750 memset(hostdata, 0, sizeof(struct isp2x00_hostdata));
751 hostdata->pci_dev = pdev;
753 hostdata->queued = 0;
754 /* set up the control block */
755 hostdata->control_block.version = 0x1;
756 hostdata->control_block.firm_opts = 0x0108;
757 hostdata->control_block.max_frame_len = 2048;
758 hostdata->control_block.max_iocb = 256;
759 hostdata->control_block.exec_throttle = 8;
760 hostdata->control_block.retry_delay = 5;
761 hostdata->control_block.retry_cnt = 1;
762 hostdata->control_block.node_name[0] = 0x0020;
763 hostdata->control_block.node_name[1] = 0xE000;
764 hostdata->control_block.node_name[2] = 0x008B;
765 hostdata->control_block.node_name[3] = 0x0000;
766 hostdata->control_block.hard_addr = 0x0003;
767 hostdata->control_block.req_queue_len = QLOGICFC_REQ_QUEUE_LEN + 1;
768 hostdata->control_block.res_queue_len = RES_QUEUE_LEN + 1;
769 hostdata->control_block.res_queue_addr_lo = virt_to_bus_low32(&hostdata->res);
770 hostdata->control_block.res_queue_addr_high = virt_to_bus_high32(&hostdata->res);
771 hostdata->control_block.req_queue_addr_lo = virt_to_bus_low32(&hostdata->req);
772 hostdata->control_block.req_queue_addr_high = virt_to_bus_high32(&hostdata->req);
775 hostdata->adapter_state = AS_LOOP_DOWN;
776 hostdata->host_id = hosts;
778 if (isp2x00_init(host) || isp2x00_reset_hardware(host)) {
779 scsi_unregister(host);
780 continue;
782 host->this_id = 0;
784 if (request_irq(host->irq, do_isp2x00_intr_handler, SA_INTERRUPT | SA_SHIRQ, "qlogicfc", host)) {
785 printk("qlogicfc%d : interrupt %d already in use\n",
786 hostdata->host_id, host->irq);
787 scsi_unregister(host);
788 continue;
790 if (check_region(host->io_port, 0xff)) {
791 printk("qlogicfc%d : i/o region 0x%lx-0x%lx already "
792 "in use\n",
793 hostdata->host_id, host->io_port, host->io_port + 0xff);
794 free_irq(host->irq, host);
795 scsi_unregister(host);
796 continue;
798 request_region(host->io_port, 0xff, "qlogicfc");
800 outw(0x0, host->io_port + PCI_SEMAPHORE);
801 outw(HCCR_CLEAR_RISC_INTR, host->io_port + HOST_HCCR);
802 isp2x00_enable_irqs(host);
803 /* wait for the loop to come up */
804 for (wait_time = jiffies + 10 * HZ; wait_time > jiffies && hostdata->adapter_state == AS_LOOP_DOWN;)
805 barrier();
807 if (hostdata->adapter_state == AS_LOOP_DOWN) {
808 printk("qlogicfc%d : link is not up\n", hostdata->host_id);
810 hosts++;
815 /* this busy loop should not be needed but the isp2x00 seems to need
816 some time before recognizing it is attached to a fabric */
818 #if ISP2x00_FABRIC
819 for (wait_time = jiffies + 5 * HZ; wait_time > jiffies;)
820 barrier();
821 #endif
823 LEAVE("isp2x00_detect");
825 return hosts;
829 static int isp2x00_make_portdb(struct Scsi_Host *host)
832 short param[8];
833 int i, j;
834 struct id_name_map temp[QLOGICFC_MAX_ID + 1];
835 struct isp2x00_hostdata *hostdata;
837 isp2x00_disable_irqs(host);
839 memset(temp, 0, sizeof(temp));
840 hostdata = (struct isp2x00_hostdata *) host->hostdata;
842 #if ISP2x00_FABRIC
843 for (i = 0x81; i < QLOGICFC_MAX_ID; i++) {
844 param[0] = MBOX_PORT_LOGOUT;
845 param[1] = i << 8;
846 param[2] = 0;
847 param[3] = 0;
849 isp2x00_mbox_command(host, param);
851 if (param[0] != MBOX_COMMAND_COMPLETE) {
853 DEBUG_FABRIC(printk("qlogicfc%d : logout failed %x %x\n", hostdata->host_id, i, param[0]));
856 #endif
859 param[0] = MBOX_GET_INIT_SCSI_ID;
861 isp2x00_mbox_command(host, param);
863 if (param[0] == MBOX_COMMAND_COMPLETE) {
864 hostdata->port_id = ((u_int) param[3]) << 16;
865 hostdata->port_id |= param[2];
866 temp[0].loop_id = param[1];
867 temp[0].wwn = hostdata->wwn;
869 else {
870 printk("qlogicfc%d : error getting scsi id.\n", hostdata->host_id);
873 for (i = 0; i <=QLOGICFC_MAX_ID; i++)
874 temp[i].loop_id = temp[0].loop_id;
876 for (i = 0, j = 1; i <= QLOGICFC_MAX_LOOP_ID; i++) {
877 param[0] = MBOX_GET_PORT_NAME;
878 param[1] = (i << 8) & 0xff00;
880 isp2x00_mbox_command(host, param);
882 if (param[0] == MBOX_COMMAND_COMPLETE) {
883 temp[j].loop_id = i;
884 temp[j].wwn = ((u64) (param[2] & 0xff)) << 56;
885 temp[j].wwn |= ((u64) ((param[2] >> 8) & 0xff)) << 48;
886 temp[j].wwn |= ((u64) (param[3] & 0xff)) << 40;
887 temp[j].wwn |= ((u64) ((param[3] >> 8) & 0xff)) << 32;
888 temp[j].wwn |= ((u64) (param[6] & 0xff)) << 24;
889 temp[j].wwn |= ((u64) ((param[6] >> 8) & 0xff)) << 16;
890 temp[j].wwn |= ((u64) (param[7] & 0xff)) << 8;
891 temp[j].wwn |= ((u64) ((param[7] >> 8) & 0xff));
893 j++;
899 #if ISP2x00_FABRIC
900 isp2x00_init_fabric(host, temp, j);
901 #endif
903 for (i = 0; i <= QLOGICFC_MAX_ID; i++) {
904 if (temp[i].wwn != hostdata->port_db[i].wwn) {
905 for (j = 0; j <= QLOGICFC_MAX_ID; j++) {
906 if (temp[j].wwn == hostdata->port_db[i].wwn) {
907 hostdata->port_db[i].loop_id = temp[j].loop_id;
908 break;
911 if (j == QLOGICFC_MAX_ID + 1)
912 hostdata->port_db[i].loop_id = temp[0].loop_id;
914 for (j = 0; j <= QLOGICFC_MAX_ID; j++) {
915 if (hostdata->port_db[j].wwn == temp[i].wwn || !hostdata->port_db[j].wwn) {
916 break;
919 if (j == QLOGICFC_MAX_ID + 1)
920 printk("qlogicfc%d : Too many scsi devices, no more room in port map.\n", hostdata->host_id);
921 if (!hostdata->port_db[j].wwn) {
922 hostdata->port_db[j].loop_id = temp[i].loop_id;
923 hostdata->port_db[j].wwn = temp[i].wwn;
925 } else
926 hostdata->port_db[i].loop_id = temp[i].loop_id;
930 isp2x00_enable_irqs(host);
932 return 0;
936 #if ISP2x00_FABRIC
938 #define FABRIC_PORT 0x7e
939 #define FABRIC_CONTROLLER 0x7f
940 #define FABRIC_SNS 0x80
942 int isp2x00_init_fabric(struct Scsi_Host *host, struct id_name_map *port_db, int cur_scsi_id)
945 u_short param[8];
946 u64 wwn;
947 int done = 0;
948 u_short loop_id = 0x81;
949 u_short scsi_id = cur_scsi_id;
950 u_int port_id;
951 struct sns_cb req;
952 u_char sns_response[608];
953 struct isp2x00_hostdata *hostdata;
955 hostdata = (struct isp2x00_hostdata *) host->hostdata;
957 DEBUG_FABRIC(printk("qlogicfc%d : Checking for a fabric.\n", hostdata->host_id));
958 param[0] = MBOX_GET_PORT_NAME;
959 param[1] = (u16)FABRIC_PORT << 8;
961 isp2x00_mbox_command(host, param);
963 if (param[0] != MBOX_COMMAND_COMPLETE) {
964 DEBUG_FABRIC(printk("qlogicfc%d : fabric check result %x\n", hostdata->host_id, param[0]));
965 return 0;
967 printk("qlogicfc%d : Fabric found.\n", hostdata->host_id);
969 if (hostdata->adapter_state & AS_REDO_LOOP_PORTDB){
970 memset(&req, 0, sizeof(req));
972 req.len = 8;
973 req.response_low = virt_to_bus_low32(sns_response);
974 req.response_high = virt_to_bus_high32(sns_response);
975 req.sub_len = 22;
976 req.data[0] = 0x17;
977 req.data[1] = 0x02;
978 req.data[8] = (u_char) (hostdata->port_id & 0xff);
979 req.data[9] = (u_char) (hostdata->port_id >> 8 & 0xff);
980 req.data[10] = (u_char) (hostdata->port_id >> 16 & 0xff);
981 req.data[13] = 0x01;
982 param[0] = MBOX_SEND_SNS;
983 param[1] = 30;
984 param[2] = virt_to_bus_low32(&req) >> 16;
985 param[3] = virt_to_bus_low32(&req);
986 param[6] = virt_to_bus_high32(&req) >> 16;
987 param[7] = virt_to_bus_high32(&req);
989 isp2x00_mbox_command(host, param);
991 if (param[0] != MBOX_COMMAND_COMPLETE)
992 printk("qlogicfc%d : error sending RFC-4\n", hostdata->host_id);
995 port_id = hostdata->port_id;
996 while (!done) {
997 memset(&req, 0, sizeof(req));
999 req.len = 304;
1000 req.response_low = virt_to_bus_low32(sns_response);
1001 req.response_high = virt_to_bus_high32(sns_response);
1002 req.sub_len = 6;
1003 req.data[0] = 0x00;
1004 req.data[1] = 0x01;
1005 req.data[8] = (u_char) (port_id & 0xff);
1006 req.data[9] = (u_char) (port_id >> 8 & 0xff);
1007 req.data[10] = (u_char) (port_id >> 16 & 0xff);
1009 param[0] = MBOX_SEND_SNS;
1010 param[1] = 14;
1011 param[2] = virt_to_bus_low32(&req) >> 16;
1012 param[3] = virt_to_bus_low32(&req);
1013 param[6] = virt_to_bus_high32(&req) >> 16;
1014 param[7] = virt_to_bus_high32(&req);
1016 isp2x00_mbox_command(host, param);
1018 if (param[0] == MBOX_COMMAND_COMPLETE) {
1019 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]));
1020 DEBUG_FABRIC(printk(" port id: %02x%02x%02x\n", sns_response[17], sns_response[18], sns_response[19]));
1021 port_id = ((u_int) sns_response[17]) << 16;
1022 port_id |= ((u_int) sns_response[18]) << 8;
1023 port_id |= ((u_int) sns_response[19]);
1024 wwn = ((u64) sns_response[20]) << 56;
1025 wwn |= ((u64) sns_response[21]) << 48;
1026 wwn |= ((u64) sns_response[22]) << 40;
1027 wwn |= ((u64) sns_response[23]) << 32;
1028 wwn |= ((u64) sns_response[24]) << 24;
1029 wwn |= ((u64) sns_response[25]) << 16;
1030 wwn |= ((u64) sns_response[26]) << 8;
1031 wwn |= ((u64) sns_response[27]);
1032 if (hostdata->port_id >> 8 != port_id >> 8) {
1033 DEBUG_FABRIC(printk("qlogicfc%d : adding a fabric port: %x\n", hostdata->host_id, port_id));
1034 param[0] = MBOX_PORT_LOGIN;
1035 param[1] = loop_id << 8;
1036 param[2] = (u_short) (port_id >> 16);
1037 param[3] = (u_short) (port_id);
1039 isp2x00_mbox_command(host, param);
1041 if (param[0] == MBOX_COMMAND_COMPLETE) {
1042 port_db[scsi_id].wwn = wwn;
1043 port_db[scsi_id].loop_id = loop_id;
1044 loop_id++;
1045 scsi_id++;
1046 } else {
1047 printk("qlogicfc%d : Error performing port login %x\n", hostdata->host_id, param[0]);
1048 DEBUG_FABRIC(printk("qlogicfc%d : loop_id: %x\n", hostdata->host_id, loop_id));
1049 param[0] = MBOX_PORT_LOGOUT;
1050 param[1] = loop_id << 8;
1051 param[2] = 0;
1052 param[3] = 0;
1054 isp2x00_mbox_command(host, param);
1059 if (hostdata->port_id == port_id)
1060 done = 1;
1061 } else {
1062 printk("qlogicfc%d : Get All Next failed %x.\n", hostdata->host_id, param[0]);
1063 return 0;
1067 return 1;
1070 #endif /* ISP2x00_FABRIC */
1073 int isp2x00_release(struct Scsi_Host *host)
1075 struct isp2x00_hostdata *hostdata;
1077 ENTER("isp2x00_release");
1079 hostdata = (struct isp2x00_hostdata *) host->hostdata;
1081 outw(0x0, host->io_port + PCI_INTER_CTL);
1082 free_irq(host->irq, host);
1084 release_region(host->io_port, 0xff);
1086 LEAVE("isp2x00_release");
1088 return 0;
1092 const char *isp2x00_info(struct Scsi_Host *host)
1094 static char buf[80];
1095 struct isp2x00_hostdata *hostdata;
1096 ENTER("isp2x00_info");
1098 hostdata = (struct isp2x00_hostdata *) host->hostdata;
1099 sprintf(buf,
1100 "QLogic ISP%04x SCSI on PCI bus %02x device %02x irq %d base 0x%lx",
1101 hostdata->pci_dev->device, hostdata->pci_dev->bus->number, hostdata->pci_dev->devfn, host->irq,
1102 host->io_port);
1105 LEAVE("isp2x00_info");
1107 return buf;
1112 * The middle SCSI layer ensures that queuecommand never gets invoked
1113 * concurrently with itself or the interrupt handler (though the
1114 * interrupt handler may call this routine as part of
1115 * request-completion handling).
1117 int isp2x00_queuecommand(Scsi_Cmnd * Cmnd, void (*done) (Scsi_Cmnd *))
1119 int i, sg_count, n, num_free;
1120 u_int in_ptr, out_ptr;
1121 struct dataseg *ds;
1122 struct scatterlist *sg;
1123 struct Command_Entry *cmd;
1124 struct Continuation_Entry *cont;
1125 struct Scsi_Host *host;
1126 struct isp2x00_hostdata *hostdata;
1128 ENTER("isp2x00_queuecommand");
1130 host = Cmnd->host;
1131 hostdata = (struct isp2x00_hostdata *) host->hostdata;
1132 Cmnd->scsi_done = done;
1134 DEBUG(isp2x00_print_scsi_cmd(Cmnd));
1136 if (hostdata->adapter_state & AS_REDO_FABRIC_PORTDB || hostdata->adapter_state & AS_REDO_LOOP_PORTDB) {
1137 isp2x00_make_portdb(host);
1138 hostdata->adapter_state = AS_LOOP_GOOD;
1139 printk("qlogicfc%d : Port Database\n", hostdata->host_id);
1140 for (i = 0; hostdata->port_db[i].wwn != 0; i++) {
1141 printk("wwn: %08x%08x scsi_id: %x loop_id: %x\n", (u_int) (hostdata->port_db[i].wwn >> 32), (u_int) hostdata->port_db[i].wwn, i, hostdata->port_db[i].loop_id);
1144 if (hostdata->adapter_state == AS_FIRMWARE_DEAD) {
1145 printk("qlogicfc%d : The firmware is dead, just return.\n", hostdata->host_id);
1146 host->max_id = 0;
1147 return 0;
1150 out_ptr = inw(host->io_port + MBOX4);
1151 in_ptr = hostdata->req_in_ptr;
1153 DEBUG(printk("qlogicfc%d : request queue depth %d\n", hostdata->host_id,
1154 REQ_QUEUE_DEPTH(in_ptr, out_ptr)));
1156 cmd = (struct Command_Entry *) &hostdata->req[in_ptr][0];
1157 in_ptr = (in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN;
1158 if (in_ptr == out_ptr) {
1159 DEBUG(printk("qlogicfc%d : request queue overflow\n", hostdata->host_id));
1160 return 1;
1162 if (hostdata->send_marker) {
1163 struct Marker_Entry *marker;
1165 TRACE("queue marker", in_ptr, 0);
1167 DEBUG(printk("qlogicfc%d : adding marker entry\n", hostdata->host_id));
1168 marker = (struct Marker_Entry *) cmd;
1169 memset(marker, 0, sizeof(struct Marker_Entry));
1171 marker->hdr.entry_type = ENTRY_MARKER;
1172 marker->hdr.entry_cnt = 1;
1173 marker->modifier = SYNC_ALL;
1175 hostdata->send_marker = 0;
1177 if (((in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN) == out_ptr) {
1178 outw(in_ptr, host->io_port + MBOX4);
1179 hostdata->req_in_ptr = in_ptr;
1180 DEBUG(printk("qlogicfc%d : request queue overflow\n", hostdata->host_id));
1181 return 1;
1183 cmd = (struct Command_Entry *) &hostdata->req[in_ptr][0];
1184 in_ptr = (in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN;
1186 TRACE("queue command", in_ptr, Cmnd);
1188 memset(cmd, 0, sizeof(struct Command_Entry));
1190 /* find a free handle mapping slot */
1191 for (i = in_ptr; i != (in_ptr - 1) && hostdata->handle_ptrs[i]; i = ((i + 1) % (QLOGICFC_REQ_QUEUE_LEN + 1)));
1193 if (!hostdata->handle_ptrs[i]) {
1194 cmd->handle = i;
1195 hostdata->handle_ptrs[i] = Cmnd;
1196 hostdata->handle_serials[i] = Cmnd->serial_number;
1197 } else {
1198 printk("qlogicfc%d : no handle slots, this should not happen.\n", hostdata->host_id);
1199 printk("hostdata->queued is %x, in_ptr: %x\n", hostdata->queued, in_ptr);
1200 for (i = 0; i <= QLOGICFC_REQ_QUEUE_LEN; i++){
1201 if (!hostdata->handle_ptrs[i]){
1202 printk("slot %d has %p\n", i, hostdata->handle_ptrs[i]);
1207 cmd->hdr.entry_type = ENTRY_COMMAND;
1208 cmd->hdr.entry_cnt = 1;
1209 cmd->target_lun = Cmnd->lun;
1210 cmd->expanded_lun = Cmnd->lun;
1211 #if ISP2x00_PORTDB
1212 cmd->target_id = hostdata->port_db[Cmnd->target].loop_id;
1213 #else
1214 cmd->target_id = Cmnd->target;
1215 #endif
1216 cmd->total_byte_cnt = (u_int) Cmnd->request_bufflen;
1217 cmd->time_out = 0;
1218 memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
1220 if (Cmnd->use_sg) {
1221 cmd->segment_cnt = sg_count = Cmnd->use_sg;
1222 sg = (struct scatterlist *) Cmnd->request_buffer;
1223 ds = cmd->dataseg;
1224 /* fill in first two sg entries: */
1225 n = sg_count;
1226 if (n > DATASEGS_PER_COMMAND)
1227 n = DATASEGS_PER_COMMAND;
1229 for (i = 0; i < n; i++) {
1230 ds[i].d_base = virt_to_bus_low32(sg->address);
1231 #if BITS_PER_LONG > 32
1232 ds[i].d_base_hi = virt_to_bus_high32(sg->address);
1233 #endif
1234 ds[i].d_count = sg->length;
1235 ++sg;
1237 sg_count -= DATASEGS_PER_COMMAND;
1239 while (sg_count > 0) {
1240 ++cmd->hdr.entry_cnt;
1241 cont = (struct Continuation_Entry *)
1242 &hostdata->req[in_ptr][0];
1243 memset(cont, 0, sizeof(struct Continuation_Entry));
1244 in_ptr = (in_ptr + 1) & QLOGICFC_REQ_QUEUE_LEN;
1245 if (in_ptr == out_ptr) {
1246 DEBUG(printk("qlogicfc%d : unexpected request queue overflow\n", hostdata->host_id));
1247 return 1;
1249 TRACE("queue continuation", in_ptr, 0);
1250 cont->hdr.entry_type = ENTRY_CONTINUATION;
1251 ds = cont->dataseg;
1252 n = sg_count;
1253 if (n > DATASEGS_PER_CONT)
1254 n = DATASEGS_PER_CONT;
1255 for (i = 0; i < n; ++i) {
1256 ds[i].d_base = virt_to_bus_low32(sg->address);
1257 #if BITS_PER_LONG > 32
1258 ds[i].d_base_hi = virt_to_bus_high32(sg->address);
1259 #endif
1260 ds[i].d_count = sg->length;
1261 ++sg;
1263 sg_count -= n;
1265 } else {
1266 cmd->dataseg[0].d_base = virt_to_bus_low32(Cmnd->request_buffer);
1267 #if BITS_PER_LONG > 32
1268 cmd->dataseg[0].d_base_hi = virt_to_bus_high32(Cmnd->request_buffer);
1269 #endif
1270 cmd->dataseg[0].d_count = (u_int) Cmnd->request_bufflen;
1271 cmd->segment_cnt = 1;
1274 switch (Cmnd->cmnd[0]) {
1275 case TEST_UNIT_READY:
1276 case START_STOP:
1277 break;
1278 case WRITE_10:
1279 case WRITE_6:
1280 case WRITE_BUFFER:
1281 case MODE_SELECT:
1282 cmd->control_flags = CFLAG_WRITE;
1283 break;
1284 case REQUEST_SENSE:
1285 /* scsi.c expects sense info in a different buffer */
1286 cmd->dataseg[0].d_base = virt_to_bus_low32(Cmnd->sense_buffer);
1287 #if BITS_PER_LONG > 32
1288 cmd->dataseg[0].d_base_hi = virt_to_bus_high32(Cmnd->request_buffer);
1289 #endif
1290 cmd->segment_cnt = 1;
1291 cmd->control_flags = CFLAG_READ;
1292 break;
1293 default:
1294 cmd->control_flags = CFLAG_READ;
1295 break;
1299 if (Cmnd->device->tagged_supported) {
1300 if ((jiffies - hostdata->tag_ages[Cmnd->target]) > (2 * SCSI_TIMEOUT)) {
1301 cmd->control_flags |= CFLAG_ORDERED_TAG;
1302 hostdata->tag_ages[Cmnd->target] = jiffies;
1303 } else
1304 switch (Cmnd->tag) {
1305 case HEAD_OF_QUEUE_TAG:
1306 cmd->control_flags |= CFLAG_HEAD_TAG;
1307 break;
1308 case ORDERED_QUEUE_TAG:
1309 cmd->control_flags |= CFLAG_ORDERED_TAG;
1310 break;
1311 default:
1312 cmd->control_flags |= CFLAG_SIMPLE_TAG;
1313 break;
1316 outw(in_ptr, host->io_port + MBOX4);
1317 hostdata->req_in_ptr = in_ptr;
1319 hostdata->queued++;
1321 num_free = QLOGICFC_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr);
1322 num_free = (num_free > 2) ? num_free - 2 : 0;
1323 host->can_queue = hostdata->queued + num_free;
1324 if (host->can_queue > QLOGICFC_REQ_QUEUE_LEN)
1325 host->can_queue = QLOGICFC_REQ_QUEUE_LEN;
1326 host->sg_tablesize = QLOGICFC_MAX_SG(num_free);
1328 /* this is really gross */
1329 if (host->can_queue <= host->host_busy){
1330 if (host->can_queue+2 < host->host_busy)
1331 DEBUG(printk("qlogicfc%d.c crosses its fingers.\n", hostdata->host_id));
1332 host->can_queue = host->host_busy + 1;
1335 LEAVE("isp2x00_queuecommand");
1337 return 0;
1341 #define ASYNC_EVENT_INTERRUPT 0x01
1344 void do_isp2x00_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1346 unsigned long flags;
1348 spin_lock_irqsave(&io_request_lock, flags);
1349 isp2x00_intr_handler(irq, dev_id, regs);
1350 spin_unlock_irqrestore(&io_request_lock, flags);
1353 void isp2x00_intr_handler(int irq, void *dev_id, struct pt_regs *regs)
1355 Scsi_Cmnd *Cmnd;
1356 struct Status_Entry *sts;
1357 struct Scsi_Host *host = dev_id;
1358 struct isp2x00_hostdata *hostdata;
1359 u_int in_ptr, out_ptr, handle, num_free;
1360 u_short status;
1362 ENTER_INTR("isp2x00_intr_handler");
1364 hostdata = (struct isp2x00_hostdata *) host->hostdata;
1366 DEBUG_INTR(printk("qlogicfc%d : interrupt on line %d\n", hostdata->host_id, irq));
1368 if (!(inw(host->io_port + PCI_INTER_STS) & 0x08)) {
1369 /* spurious interrupts can happen legally */
1370 DEBUG_INTR(printk("qlogicfc%d : got spurious interrupt\n", hostdata->host_id));
1371 return;
1373 in_ptr = inw(host->io_port + MBOX5);
1374 out_ptr = hostdata->res_out_ptr;
1376 if ((inw(host->io_port + PCI_SEMAPHORE) & ASYNC_EVENT_INTERRUPT)) {
1377 status = inw(host->io_port + MBOX0);
1379 DEBUG_INTR(printk("qlogicfc%d : mbox completion status: %x\n",
1380 hostdata->host_id, status));
1382 switch (status) {
1383 case LOOP_UP:
1384 case POINT_TO_POINT_UP:
1385 printk("qlogicfc%d : link is up\n", hostdata->host_id);
1386 hostdata->adapter_state = AS_REDO_FABRIC_PORTDB | AS_REDO_LOOP_PORTDB;
1387 break;
1388 case LOOP_DOWN:
1389 printk("qlogicfc%d : link is down\n", hostdata->host_id);
1390 hostdata->adapter_state = AS_LOOP_DOWN;
1391 break;
1392 case CONNECTION_MODE:
1393 printk("received CONNECTION_MODE irq %x\n", inw(host->io_port + MBOX1));
1394 break;
1395 case CHANGE_NOTIFICATION:
1396 if (hostdata->adapter_state == AS_LOOP_GOOD)
1397 hostdata->adapter_state = AS_REDO_FABRIC_PORTDB;
1398 break;
1399 case LIP_OCCURED:
1400 case PORT_DB_CHANGED:
1401 case LIP_RECEIVED:
1402 if (hostdata->adapter_state == AS_LOOP_GOOD)
1403 hostdata->adapter_state = AS_REDO_LOOP_PORTDB;
1404 break;
1405 case SYSTEM_ERROR:
1406 printk("qlogicfc%d : The firmware just choked.\n", hostdata->host_id);
1407 hostdata->adapter_state = AS_FIRMWARE_DEAD;
1408 break;
1409 case SCSI_COMMAND_COMPLETE:
1410 handle = inw(host->io_port + MBOX1) | (inw(host->io_port + MBOX2) << 16);
1411 Cmnd = hostdata->handle_ptrs[handle];
1412 hostdata->handle_ptrs[handle] = NULL;
1413 hostdata->handle_serials[handle] = 0;
1414 hostdata->queued--;
1415 if (Cmnd != NULL) {
1416 Cmnd->result = 0x0;
1417 (*Cmnd->scsi_done) (Cmnd);
1418 } else
1419 printk("qlogicfc%d.c : got a null value out of handle_ptrs, this sucks\n", hostdata->host_id);
1420 break;
1421 case MBOX_COMMAND_COMPLETE:
1422 case INVALID_COMMAND:
1423 case HOST_INTERFACE_ERROR:
1424 case TEST_FAILED:
1425 case COMMAND_ERROR:
1426 case COMMAND_PARAM_ERROR:
1427 case PORT_ID_USED:
1428 case LOOP_ID_USED:
1429 case ALL_IDS_USED:
1430 hostdata->mbox_done = 1;
1431 outw(HCCR_CLEAR_RISC_INTR, host->io_port + HOST_HCCR);
1432 return;
1433 default:
1434 printk("qlogicfc%d : got an unknown status? %x\n", hostdata->host_id, status);
1436 outw(0x0, host->io_port + PCI_SEMAPHORE);
1437 } else {
1438 DEBUG_INTR(printk("qlogicfc%d : response queue update\n", hostdata->host_id));
1439 DEBUG_INTR(printk("qlogicfc%d : response queue depth %d\n", hostdata->host_id, RES_QUEUE_DEPTH(in_ptr, out_ptr)));
1441 while (out_ptr != in_ptr) {
1442 sts = (struct Status_Entry *) &hostdata->res[out_ptr][0];
1443 out_ptr = (out_ptr + 1) & RES_QUEUE_LEN;
1445 TRACE("done", out_ptr, Cmnd);
1446 DEBUG_INTR(isp2x00_print_status_entry(sts));
1447 if (sts->hdr.entry_type == ENTRY_STATUS && (Cmnd = hostdata->handle_ptrs[sts->handle])) {
1448 Cmnd->result = isp2x00_return_status(sts);
1449 hostdata->handle_ptrs[sts->handle] = NULL;
1450 hostdata->queued--;
1453 * if any of the following are true we do not
1454 * call scsi_done. if the status is CS_ABORTED
1455 * we dont have to call done because the upper
1456 * level should already know its aborted.
1458 if (hostdata->handle_serials[sts->handle] != Cmnd->serial_number
1459 || sts->completion_status == CS_ABORTED){
1460 hostdata->handle_serials[sts->handle] = 0;
1461 outw(out_ptr, host->io_port + MBOX5);
1462 continue;
1465 * if we get back an error indicating the port
1466 * is not there or if the link is down and
1467 * this is a device that used to be there
1468 * allow the command to timeout.
1469 * the device may well be back in a couple of
1470 * seconds.
1472 if ((hostdata->adapter_state == AS_LOOP_DOWN || sts->completion_status == CS_PORT_UNAVAILABLE || sts->completion_status == CS_PORT_LOGGED_OUT || sts->completion_status == CS_PORT_CONFIG_CHANGED) && hostdata->port_db[Cmnd->target].wwn){
1473 outw(out_ptr, host->io_port + MBOX5);
1474 continue;
1476 } else {
1477 outw(out_ptr, host->io_port + MBOX5);
1478 continue;
1481 if (sts->completion_status == CS_RESET_OCCURRED
1482 || (sts->status_flags & STF_BUS_RESET))
1483 hostdata->send_marker = 1;
1485 memset(Cmnd->sense_buffer, 0, sizeof(Cmnd->sense_buffer));
1486 if (sts->scsi_status & 0x0200)
1487 memcpy(Cmnd->sense_buffer, sts->req_sense_data,
1488 sizeof(Cmnd->sense_buffer));
1490 outw(out_ptr, host->io_port + MBOX5);
1492 if (Cmnd->scsi_done != NULL) {
1493 (*Cmnd->scsi_done) (Cmnd);
1494 } else
1495 printk("qlogicfc%d : Ouch, scsi done is NULL\n", hostdata->host_id);
1497 hostdata->res_out_ptr = out_ptr;
1501 out_ptr = inw(host->io_port + MBOX4);
1502 in_ptr = hostdata->req_in_ptr;
1504 num_free = QLOGICFC_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr);
1505 num_free = (num_free > 2) ? num_free - 2 : 0;
1506 host->can_queue = hostdata->queued + num_free;
1507 if (host->can_queue > QLOGICFC_REQ_QUEUE_LEN)
1508 host->can_queue = QLOGICFC_REQ_QUEUE_LEN;
1509 host->sg_tablesize = QLOGICFC_MAX_SG(num_free);
1511 if (host->can_queue <= host->host_busy){
1512 if (host->can_queue+2 < host->host_busy)
1513 DEBUG(printk("qlogicfc%d : crosses its fingers.\n", hostdata->host_id));
1514 host->can_queue = host->host_busy + 1;
1517 outw(HCCR_CLEAR_RISC_INTR, host->io_port + HOST_HCCR);
1518 LEAVE_INTR("isp2x00_intr_handler");
1522 static int isp2x00_return_status(struct Status_Entry *sts)
1524 int host_status = DID_ERROR;
1525 #if DEBUG_ISP2x00_INTR
1526 static char *reason[] =
1528 "DID_OK",
1529 "DID_NO_CONNECT",
1530 "DID_BUS_BUSY",
1531 "DID_TIME_OUT",
1532 "DID_BAD_TARGET",
1533 "DID_ABORT",
1534 "DID_PARITY",
1535 "DID_ERROR",
1536 "DID_RESET",
1537 "DID_BAD_INTR"
1539 #endif /* DEBUG_ISP2x00_INTR */
1541 ENTER("isp2x00_return_status");
1543 DEBUG(printk("qlogicfc : completion status = 0x%04x\n",
1544 sts->completion_status));
1546 switch (sts->completion_status) {
1547 case CS_COMPLETE:
1548 host_status = DID_OK;
1549 break;
1550 case CS_DMA_ERROR:
1551 host_status = DID_ERROR;
1552 break;
1553 case CS_RESET_OCCURRED:
1554 host_status = DID_RESET;
1555 break;
1556 case CS_ABORTED:
1557 host_status = DID_ABORT;
1558 break;
1559 case CS_TIMEOUT:
1560 host_status = DID_TIME_OUT;
1561 break;
1562 case CS_DATA_OVERRUN:
1563 host_status = DID_ERROR;
1564 break;
1565 case CS_DATA_UNDERRUN:
1566 host_status = DID_OK;
1567 break;
1568 case CS_PORT_UNAVAILABLE:
1569 case CS_PORT_LOGGED_OUT:
1570 case CS_PORT_CONFIG_CHANGED:
1571 host_status = DID_BAD_TARGET;
1572 break;
1573 case CS_QUEUE_FULL:
1574 host_status = DID_ERROR;
1575 break;
1576 default:
1577 printk("qlogicfc : unknown completion status 0x%04x\n",
1578 sts->completion_status);
1579 host_status = DID_ERROR;
1580 break;
1583 DEBUG_INTR(printk("qlogicfc : host status (%s) scsi status %x\n",
1584 reason[host_status], sts->scsi_status));
1586 LEAVE("isp2x00_return_status");
1588 return (sts->scsi_status & STATUS_MASK) | (host_status << 16);
1592 int isp2x00_abort(Scsi_Cmnd * Cmnd)
1594 u_short param[8];
1595 int i;
1596 struct Scsi_Host *host;
1597 struct isp2x00_hostdata *hostdata;
1598 int return_status = SUCCESS;
1600 ENTER("isp2x00_abort");
1602 host = Cmnd->host;
1603 hostdata = (struct isp2x00_hostdata *) host->hostdata;
1605 for (i = 0; i < QLOGICFC_REQ_QUEUE_LEN; i++)
1606 if (hostdata->handle_ptrs[i] == Cmnd)
1607 break;
1609 if (i == QLOGICFC_REQ_QUEUE_LEN){
1610 return SUCCESS;
1613 isp2x00_disable_irqs(host);
1615 param[0] = MBOX_ABORT_IOCB;
1616 #if ISP2x00_PORTDB
1617 param[1] = (((u_short) hostdata->port_db[Cmnd->target].loop_id) << 8) | Cmnd->lun;
1618 #else
1619 param[1] = (((u_short) Cmnd->target) << 8) | Cmnd->lun;
1620 #endif
1621 param[2] = i & 0xffff;
1622 param[3] = i >> 16;
1624 isp2x00_mbox_command(host, param);
1626 if (param[0] != MBOX_COMMAND_COMPLETE) {
1627 printk("qlogicfc%d : scsi abort failure: %x\n", hostdata->host_id, param[0]);
1628 if (param[0] == 0x4005)
1629 Cmnd->result = DID_ERROR << 16;
1630 if (param[0] == 0x4006)
1631 Cmnd->result = DID_BAD_TARGET << 16;
1632 return_status = FAILED;
1635 if (return_status != SUCCESS){
1636 param[0] = MBOX_GET_FIRMWARE_STATE;
1637 isp2x00_mbox_command(host, param);
1638 printk("qlogicfc%d : abort failed\n", hostdata->host_id);
1639 printk("qlogicfc%d : firmware status is %x %x\n", hostdata->host_id, param[0], param[1]);
1642 isp2x00_enable_irqs(host);
1644 LEAVE("isp2x00_abort");
1646 return return_status;
1650 int isp2x00_reset(Scsi_Cmnd * Cmnd, unsigned int reset_flags)
1652 u_short param[8];
1653 struct Scsi_Host *host;
1654 struct isp2x00_hostdata *hostdata;
1655 int return_status = SCSI_RESET_SUCCESS;
1657 ENTER("isp2x00_reset");
1659 host = Cmnd->host;
1660 hostdata = (struct isp2x00_hostdata *) host->hostdata;
1661 param[0] = MBOX_BUS_RESET;
1662 param[1] = 3;
1664 isp2x00_disable_irqs(host);
1666 isp2x00_mbox_command(host, param);
1668 if (param[0] != MBOX_COMMAND_COMPLETE) {
1669 printk("qlogicfc%d : scsi bus reset failure: %x\n", hostdata->host_id, param[0]);
1670 return_status = SCSI_RESET_ERROR;
1672 isp2x00_enable_irqs(host);
1674 LEAVE("isp2x00_reset");
1676 return return_status;;
1680 int isp2x00_biosparam(Disk * disk, kdev_t n, int ip[])
1682 int size = disk->capacity;
1684 ENTER("isp2x00_biosparam");
1686 ip[0] = 64;
1687 ip[1] = 32;
1688 ip[2] = size >> 11;
1689 if (ip[2] > 1024) {
1690 ip[0] = 255;
1691 ip[1] = 63;
1692 ip[2] = size / (ip[0] * ip[1]);
1694 LEAVE("isp2x00_biosparam");
1696 return 0;
1700 static int isp2x00_reset_hardware(struct Scsi_Host *host)
1702 u_short param[8];
1703 struct isp2x00_hostdata *hostdata;
1704 int loop_count;
1706 ENTER("isp2x00_reset_hardware");
1708 hostdata = (struct isp2x00_hostdata *) host->hostdata;
1710 outw(0x01, host->io_port + ISP_CTRL_STATUS);
1711 outw(HCCR_RESET, host->io_port + HOST_HCCR);
1712 outw(HCCR_RELEASE, host->io_port + HOST_HCCR);
1713 outw(HCCR_BIOS_DISABLE, host->io_port + HOST_HCCR);
1715 loop_count = DEFAULT_LOOP_COUNT;
1716 while (--loop_count && inw(host->io_port + HOST_HCCR) == RISC_BUSY)
1717 barrier();
1718 if (!loop_count)
1719 printk("qlogicfc%d : reset_hardware loop timeout\n", hostdata->host_id);
1723 #if DEBUG_ISP2x00
1724 printk("qlogicfc%d : mbox 0 0x%04x \n", hostdata->host_id, inw(host->io_port + MBOX0));
1725 printk("qlogicfc%d : mbox 1 0x%04x \n", hostdata->host_id, inw(host->io_port + MBOX1));
1726 printk("qlogicfc%d : mbox 2 0x%04x \n", hostdata->host_id, inw(host->io_port + MBOX2));
1727 printk("qlogicfc%d : mbox 3 0x%04x \n", hostdata->host_id, inw(host->io_port + MBOX3));
1728 printk("qlogicfc%d : mbox 4 0x%04x \n", hostdata->host_id, inw(host->io_port + MBOX4));
1729 printk("qlogicfc%d : mbox 5 0x%04x \n", hostdata->host_id, inw(host->io_port + MBOX5));
1730 printk("qlogicfc%d : mbox 6 0x%04x \n", hostdata->host_id, inw(host->io_port + MBOX6));
1731 printk("qlogicfc%d : mbox 7 0x%04x \n", hostdata->host_id, inw(host->io_port + MBOX7));
1732 #endif /* DEBUG_ISP2x00 */
1734 DEBUG(printk("qlogicfc%d : verifying checksum\n", hostdata->host_id));
1736 #if RELOAD_FIRMWARE
1738 int i;
1739 unsigned short * risc_code = NULL;
1740 unsigned short risc_code_len = 0;
1741 if (hostdata->pci_dev->device == PCI_DEVICE_ID_QLOGIC_ISP2100){
1742 risc_code = risc_code2100;
1743 risc_code_len = risc_code_length2100;
1745 else if (hostdata->pci_dev->device == PCI_DEVICE_ID_QLOGIC_ISP2200){
1746 risc_code = risc_code2200;
1747 risc_code_len = risc_code_length2200;
1750 for (i = 0; i < risc_code_len; i++) {
1751 param[0] = MBOX_WRITE_RAM_WORD;
1752 param[1] = risc_code_addr01 + i;
1753 param[2] = risc_code[i];
1755 isp2x00_mbox_command(host, param);
1757 if (param[0] != MBOX_COMMAND_COMPLETE) {
1758 printk("qlogicfc%d : firmware load failure\n", hostdata->host_id);
1759 return 1;
1763 #endif /* RELOAD_FIRMWARE */
1765 param[0] = MBOX_VERIFY_CHECKSUM;
1766 param[1] = risc_code_addr01;
1768 isp2x00_mbox_command(host, param);
1770 if (param[0] != MBOX_COMMAND_COMPLETE) {
1771 printk("qlogicfc%d : ram checksum failure\n", hostdata->host_id);
1772 return 1;
1774 DEBUG(printk("qlogicfc%d : executing firmware\n", hostdata->host_id));
1776 param[0] = MBOX_EXEC_FIRMWARE;
1777 param[1] = risc_code_addr01;
1779 isp2x00_mbox_command(host, param);
1781 param[0] = MBOX_ABOUT_FIRMWARE;
1783 isp2x00_mbox_command(host, param);
1785 if (param[0] != MBOX_COMMAND_COMPLETE) {
1786 printk("qlogicfc%d : about firmware failure\n", hostdata->host_id);
1787 return 1;
1789 DEBUG(printk("qlogicfc%d : firmware major revision %d\n", hostdata->host_id, param[1]));
1790 DEBUG(printk("qlogicfc%d : firmware minor revision %d\n", hostdata->host_id, param[2]));
1792 #ifdef USE_NVRAM_DEFAULTS
1794 if (isp2x00_get_nvram_defaults(host, &hostdata->control_block) != 0) {
1795 printk("qlogicfc%d : Could not read from NVRAM\n", hostdata->host_id);
1797 #endif
1799 hostdata->wwn = (u64) (hostdata->control_block.node_name[0]) << 56;
1800 hostdata->wwn |= (u64) (hostdata->control_block.node_name[0] & 0xff00) << 48;
1801 hostdata->wwn |= (u64) (hostdata->control_block.node_name[1] & 0xff00) << 24;
1802 hostdata->wwn |= (u64) (hostdata->control_block.node_name[1] & 0x00ff) << 48;
1803 hostdata->wwn |= (u64) (hostdata->control_block.node_name[2] & 0x00ff) << 24;
1804 hostdata->wwn |= (u64) (hostdata->control_block.node_name[2] & 0xff00) << 8;
1805 hostdata->wwn |= (u64) (hostdata->control_block.node_name[3] & 0x00ff) << 8;
1806 hostdata->wwn |= (u64) (hostdata->control_block.node_name[3] & 0xff00) >> 8;
1808 param[0] = MBOX_INIT_FIRMWARE;
1809 param[2] = (u_short) (virt_to_bus_low32(&hostdata->control_block) >> 16);
1810 param[3] = (u_short) (virt_to_bus_low32(&hostdata->control_block) & 0xffff);
1811 param[4] = 0;
1812 param[5] = 0;
1813 param[6] = (u_short) (virt_to_bus_high32(&hostdata->control_block) >> 16);
1814 param[7] = (u_short) (virt_to_bus_high32(&hostdata->control_block) & 0xffff);
1815 isp2x00_mbox_command(host, param);
1816 if (param[0] != MBOX_COMMAND_COMPLETE) {
1817 printk("qlogicfc%d.c: Ouch 0x%04x\n", hostdata->host_id, param[0]);
1818 return 1;
1820 param[0] = MBOX_GET_FIRMWARE_STATE;
1821 isp2x00_mbox_command(host, param);
1822 if (param[0] != MBOX_COMMAND_COMPLETE) {
1823 printk("qlogicfc%d.c: 0x%04x\n", hostdata->host_id, param[0]);
1824 return 1;
1827 LEAVE("isp2x00_reset_hardware");
1829 return 0;
1832 #ifdef USE_NVRAM_DEFAULTS
1834 static int isp2x00_get_nvram_defaults(struct Scsi_Host *host, struct init_cb *control_block)
1837 u_short value;
1838 if (isp2x00_read_nvram_word(host, 0) != 0x5349)
1839 return 1;
1841 value = isp2x00_read_nvram_word(host, 8);
1842 control_block->node_name[0] = isp2x00_read_nvram_word(host, 9);
1843 control_block->node_name[1] = isp2x00_read_nvram_word(host, 10);
1844 control_block->node_name[2] = isp2x00_read_nvram_word(host, 11);
1845 control_block->node_name[3] = isp2x00_read_nvram_word(host, 12);
1846 control_block->hard_addr = isp2x00_read_nvram_word(host, 13);
1848 return 0;
1852 #endif
1854 static int isp2x00_init(struct Scsi_Host *sh)
1856 u_int io_base;
1857 struct isp2x00_hostdata *hostdata;
1858 u_char revision;
1859 u_int irq;
1860 u_short command;
1861 struct pci_dev *pdev;
1864 ENTER("isp2x00_init");
1866 hostdata = (struct isp2x00_hostdata *) sh->hostdata;
1867 pdev = hostdata->pci_dev;
1869 if (pci_read_config_word(pdev, PCI_COMMAND, &command)
1870 || pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision)) {
1871 printk("qlogicfc%d : error reading PCI configuration\n", hostdata->host_id);
1872 return 1;
1874 io_base = pdev->resource[0].start;
1875 irq = pdev->irq;
1878 if (pdev->vendor != PCI_VENDOR_ID_QLOGIC) {
1879 printk("qlogicfc%d : 0x%04x is not QLogic vendor ID\n", hostdata->host_id,
1880 pdev->vendor);
1881 return 1;
1883 if (pdev->device != PCI_DEVICE_ID_QLOGIC_ISP2100 && pdev->device != PCI_DEVICE_ID_QLOGIC_ISP2200) {
1884 printk("qlogicfc%d : 0x%04x does not match ISP2100 or ISP2200 device id\n", hostdata->host_id,
1885 pdev->device);
1886 return 1;
1888 if (!(command & PCI_COMMAND_IO) ||
1889 !(pdev->resource[0].flags & IORESOURCE_IO)) {
1890 printk("qlogicfc%d : i/o mapping is disabled\n", hostdata->host_id);
1891 return 1;
1894 if (!(command & PCI_COMMAND_MASTER)) {
1895 printk("qlogicfc%d : bus mastering is disabled\n", hostdata->host_id);
1896 return 1;
1898 if (revision != ISP2100_REV_ID1 && revision != ISP2100_REV_ID3 && revision != ISP2200_REV_ID5)
1899 printk("qlogicfc%d : new isp2x00 revision ID (%d)\n", hostdata->host_id, revision);
1902 hostdata->revision = revision;
1904 sh->irq = irq;
1905 sh->io_port = io_base;
1907 LEAVE("isp2x00_init");
1909 return 0;
1912 #if USE_NVRAM_DEFAULTS
1914 #define NVRAM_DELAY() udelay(10) /* 10 microsecond delay */
1917 u_short isp2x00_read_nvram_word(struct Scsi_Host * host, u_short byte)
1919 int i;
1920 u_short value, output, input;
1922 outw(0x2, host->io_port + PCI_NVRAM);
1923 NVRAM_DELAY();
1924 outw(0x3, host->io_port + PCI_NVRAM);
1925 NVRAM_DELAY();
1927 byte &= 0xff;
1928 byte |= 0x0600;
1929 for (i = 10; i >= 0; i--) {
1930 output = ((byte >> i) & 0x1) ? 0x4 : 0x0;
1931 outw(output | 0x2, host->io_port + PCI_NVRAM);
1932 NVRAM_DELAY();
1933 outw(output | 0x3, host->io_port + PCI_NVRAM);
1934 NVRAM_DELAY();
1935 outw(output | 0x2, host->io_port + PCI_NVRAM);
1936 NVRAM_DELAY();
1939 for (i = 0xf, value = 0; i >= 0; i--) {
1940 value <<= 1;
1941 outw(0x3, host->io_port + PCI_NVRAM);
1942 NVRAM_DELAY();
1943 input = inw(host->io_port + PCI_NVRAM);
1944 NVRAM_DELAY();
1945 outw(0x2, host->io_port + PCI_NVRAM);
1946 NVRAM_DELAY();
1947 if (input & 0x8)
1948 value |= 1;
1951 outw(0x0, host->io_port + PCI_NVRAM);
1952 NVRAM_DELAY();
1954 return value;
1958 #endif /* USE_NVRAM_DEFAULTS */
1963 * currently, this is only called during initialization or abort/reset,
1964 * at which times interrupts are disabled, so polling is OK, I guess...
1966 static int isp2x00_mbox_command(struct Scsi_Host *host, u_short param[])
1968 int loop_count;
1969 struct isp2x00_hostdata *hostdata = (struct isp2x00_hostdata *) host->hostdata;
1971 if (mbox_param[param[0]] == 0 || hostdata->adapter_state == AS_FIRMWARE_DEAD)
1972 return 1;
1974 loop_count = DEFAULT_LOOP_COUNT;
1975 while (--loop_count && inw(host->io_port + HOST_HCCR) & 0x0080)
1976 barrier();
1977 if (!loop_count) {
1978 printk("qlogicfc%d : mbox_command loop timeout #1\n", hostdata->host_id);
1979 param[0] = 0x4006;
1980 hostdata->adapter_state = AS_FIRMWARE_DEAD;
1981 return 1;
1983 hostdata->mbox_done = 0;
1985 if (mbox_param[param[0]] == 0)
1986 printk("qlogicfc%d : invalid mbox command\n", hostdata->host_id);
1988 if (mbox_param[param[0]] & 0x80)
1989 outw(param[7], host->io_port + MBOX7);
1990 if (mbox_param[param[0]] & 0x40)
1991 outw(param[6], host->io_port + MBOX6);
1992 if (mbox_param[param[0]] & 0x20)
1993 outw(param[5], host->io_port + MBOX5);
1994 if (mbox_param[param[0]] & 0x10)
1995 outw(param[4], host->io_port + MBOX4);
1996 if (mbox_param[param[0]] & 0x08)
1997 outw(param[3], host->io_port + MBOX3);
1998 if (mbox_param[param[0]] & 0x04)
1999 outw(param[2], host->io_port + MBOX2);
2000 if (mbox_param[param[0]] & 0x02)
2001 outw(param[1], host->io_port + MBOX1);
2002 if (mbox_param[param[0]] & 0x01)
2003 outw(param[0], host->io_port + MBOX0);
2006 outw(HCCR_SET_HOST_INTR, host->io_port + HOST_HCCR);
2008 while (1) {
2009 loop_count = DEFAULT_LOOP_COUNT;
2010 while (--loop_count && !(inw(host->io_port + PCI_INTER_STS) & 0x08)) {
2011 barrier();
2014 if (!loop_count) {
2015 hostdata->adapter_state = AS_FIRMWARE_DEAD;
2016 printk("qlogicfc%d : mbox_command loop timeout #2\n", hostdata->host_id);
2017 break;
2019 isp2x00_intr_handler(host->irq, host, NULL);
2021 if (hostdata->mbox_done == 1)
2022 break;
2026 loop_count = DEFAULT_LOOP_COUNT;
2027 while (--loop_count && inw(host->io_port + MBOX0) == 0x04) {
2028 barrier();
2030 if (!loop_count)
2031 printk("qlogicfc%d : mbox_command loop timeout #3\n", hostdata->host_id);
2033 param[7] = inw(host->io_port + MBOX7);
2034 param[6] = inw(host->io_port + MBOX6);
2035 param[5] = inw(host->io_port + MBOX5);
2036 param[4] = inw(host->io_port + MBOX4);
2037 param[3] = inw(host->io_port + MBOX3);
2038 param[2] = inw(host->io_port + MBOX2);
2039 param[1] = inw(host->io_port + MBOX1);
2040 param[0] = inw(host->io_port + MBOX0);
2043 outw(0x0, host->io_port + PCI_SEMAPHORE);
2045 if (inw(host->io_port + HOST_HCCR) & 0x0080) {
2046 hostdata->adapter_state = AS_FIRMWARE_DEAD;
2047 printk("qlogicfc%d : mbox op is still pending\n", hostdata->host_id);
2049 return 0;
2052 #if DEBUG_ISP2x00_INTR
2054 void isp2x00_print_status_entry(struct Status_Entry *status)
2056 printk("qlogicfc : entry count = 0x%02x, type = 0x%02x, flags = 0x%02x\n",
2057 status->hdr.entry_cnt, status->hdr.entry_type, status->hdr.flags);
2058 printk("qlogicfc : scsi status = 0x%04x, completion status = 0x%04x\n",
2059 status->scsi_status, status->completion_status);
2060 printk("qlogicfc : state flags = 0x%04x, status flags = 0x%04x\n",
2061 status->state_flags, status->status_flags);
2062 printk("qlogicfc : response info length = 0x%04x, request sense length = 0x%04x\n",
2063 status->res_info_len, status->req_sense_len);
2064 printk("qlogicfc : residual transfer length = 0x%08x, response = 0x%02x\n", status->residual, status->res_info[3]);
2068 #endif /* DEBUG_ISP2x00_INTR */
2071 #if DEBUG_ISP2x00
2073 void isp2x00_print_scsi_cmd(Scsi_Cmnd * cmd)
2075 int i;
2077 printk("qlogicfc : target = 0x%02x, lun = 0x%02x, cmd_len = 0x%02x\n",
2078 cmd->target, cmd->lun, cmd->cmd_len);
2079 printk("qlogicfc : command = ");
2080 for (i = 0; i < cmd->cmd_len; i++)
2081 printk("0x%02x ", cmd->cmnd[i]);
2082 printk("\n");
2085 #endif /* DEBUG_ISP2x00 */
2088 #ifdef MODULE
2090 Scsi_Host_Template driver_template = QLOGICFC;
2092 #include "scsi_module.c"
2094 #endif