2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
5 * See LICENSE.qla4xxx for copyright and licensing details.
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
10 #include <scsi/scsi_tcq.h>
11 #include <scsi/scsicam.h>
14 #include "ql4_version.h"
17 #include "ql4_inline.h"
22 static char qla4xxx_version_str[40];
25 * SRB allocation cache
27 static struct kmem_cache *srb_cachep;
30 * Module parameter information and variables
32 int ql4xdiscoverywait = 60;
33 module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
36 int ql4xdontresethba = 0;
37 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
38 MODULE_PARM_DESC(ql4xdontresethba,
39 "Don't reset the HBA for driver recovery \n"
40 " 0 - It will reset HBA (Default)\n"
41 " 1 - It will NOT reset HBA");
43 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
44 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
45 MODULE_PARM_DESC(ql4xextended_error_logging,
46 "Option to enable extended error logging, "
47 "Default is 0 - no logging, 1 - debug logging");
49 int ql4xenablemsix = 1;
50 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
51 MODULE_PARM_DESC(ql4xenablemsix,
52 "Set to enable MSI or MSI-X interrupt mechanism.\n"
53 " 0 = enable INTx interrupt mechanism.\n"
54 " 1 = enable MSI-X interrupt mechanism (Default).\n"
55 " 2 = enable MSI interrupt mechanism.");
57 #define QL4_DEF_QDEPTH 32
60 * SCSI host template entry points
62 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
65 * iSCSI template entry points
67 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
68 enum iscsi_tgt_dscvr type, uint32_t enable,
69 struct sockaddr *dst_addr);
70 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
71 enum iscsi_param param, char *buf);
72 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
73 enum iscsi_param param, char *buf);
74 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
75 enum iscsi_host_param param, char *buf);
76 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
77 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
80 * SCSI host template entry points
82 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
83 void (*done) (struct scsi_cmnd *));
84 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
85 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
86 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
87 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
88 static int qla4xxx_slave_alloc(struct scsi_device *device);
89 static int qla4xxx_slave_configure(struct scsi_device *device);
90 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
91 static void qla4xxx_scan_start(struct Scsi_Host *shost);
93 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
94 QLA82XX_LEGACY_INTR_CONFIG;
96 static struct scsi_host_template qla4xxx_driver_template = {
97 .module = THIS_MODULE,
99 .proc_name = DRIVER_NAME,
100 .queuecommand = qla4xxx_queuecommand,
102 .eh_abort_handler = qla4xxx_eh_abort,
103 .eh_device_reset_handler = qla4xxx_eh_device_reset,
104 .eh_target_reset_handler = qla4xxx_eh_target_reset,
105 .eh_host_reset_handler = qla4xxx_eh_host_reset,
106 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
108 .slave_configure = qla4xxx_slave_configure,
109 .slave_alloc = qla4xxx_slave_alloc,
110 .slave_destroy = qla4xxx_slave_destroy,
112 .scan_finished = iscsi_scan_finished,
113 .scan_start = qla4xxx_scan_start,
117 .use_clustering = ENABLE_CLUSTERING,
118 .sg_tablesize = SG_ALL,
120 .max_sectors = 0xFFFF,
123 static struct iscsi_transport qla4xxx_iscsi_transport = {
124 .owner = THIS_MODULE,
126 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
127 CAP_DATA_PATH_OFFLOAD,
128 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
129 ISCSI_TARGET_NAME | ISCSI_TPGT |
131 .host_param_mask = ISCSI_HOST_HWADDRESS |
132 ISCSI_HOST_IPADDRESS |
133 ISCSI_HOST_INITIATOR_NAME,
134 .tgt_dscvr = qla4xxx_tgt_dscvr,
135 .get_conn_param = qla4xxx_conn_get_param,
136 .get_session_param = qla4xxx_sess_get_param,
137 .get_host_param = qla4xxx_host_get_param,
138 .session_recovery_timedout = qla4xxx_recovery_timedout,
141 static struct scsi_transport_template *qla4xxx_scsi_transport;
143 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
145 struct iscsi_cls_session *session;
146 struct ddb_entry *ddb_entry;
148 session = starget_to_session(scsi_target(sc->device));
149 ddb_entry = session->dd_data;
151 /* if we are not logged in then the LLD is going to clean up the cmd */
152 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
153 return BLK_EH_RESET_TIMER;
155 return BLK_EH_NOT_HANDLED;
158 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
160 struct ddb_entry *ddb_entry = session->dd_data;
161 struct scsi_qla_host *ha = ddb_entry->ha;
163 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
164 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
166 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
167 "of (%d) secs exhausted, marking device DEAD.\n",
168 ha->host_no, __func__, ddb_entry->fw_ddb_index,
169 QL4_SESS_RECOVERY_TMO));
173 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
174 enum iscsi_host_param param, char *buf)
176 struct scsi_qla_host *ha = to_qla_host(shost);
180 case ISCSI_HOST_PARAM_HWADDRESS:
181 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
183 case ISCSI_HOST_PARAM_IPADDRESS:
184 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
185 ha->ip_address[1], ha->ip_address[2],
188 case ISCSI_HOST_PARAM_INITIATOR_NAME:
189 len = sprintf(buf, "%s\n", ha->name_string);
198 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
199 enum iscsi_param param, char *buf)
201 struct ddb_entry *ddb_entry = sess->dd_data;
205 case ISCSI_PARAM_TARGET_NAME:
206 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
207 ddb_entry->iscsi_name);
209 case ISCSI_PARAM_TPGT:
210 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
212 case ISCSI_PARAM_TARGET_ALIAS:
213 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
214 ddb_entry->iscsi_alias);
223 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
224 enum iscsi_param param, char *buf)
226 struct iscsi_cls_session *session;
227 struct ddb_entry *ddb_entry;
230 session = iscsi_dev_to_session(conn->dev.parent);
231 ddb_entry = session->dd_data;
234 case ISCSI_PARAM_CONN_PORT:
235 len = sprintf(buf, "%hu\n", ddb_entry->port);
237 case ISCSI_PARAM_CONN_ADDRESS:
238 /* TODO: what are the ipv6 bits */
239 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
248 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
249 enum iscsi_tgt_dscvr type, uint32_t enable,
250 struct sockaddr *dst_addr)
252 struct scsi_qla_host *ha;
253 struct sockaddr_in *addr;
254 struct sockaddr_in6 *addr6;
257 ha = (struct scsi_qla_host *) shost->hostdata;
260 case ISCSI_TGT_DSCVR_SEND_TARGETS:
261 if (dst_addr->sa_family == AF_INET) {
262 addr = (struct sockaddr_in *)dst_addr;
263 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
264 addr->sin_port) != QLA_SUCCESS)
266 } else if (dst_addr->sa_family == AF_INET6) {
268 * TODO: fix qla4xxx_send_tgts
270 addr6 = (struct sockaddr_in6 *)dst_addr;
271 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
272 addr6->sin6_port) != QLA_SUCCESS)
283 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
285 if (!ddb_entry->sess)
288 if (ddb_entry->conn) {
289 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
290 iscsi_remove_session(ddb_entry->sess);
292 iscsi_free_session(ddb_entry->sess);
295 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
299 ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
301 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
303 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
307 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
308 if (!ddb_entry->conn) {
309 iscsi_remove_session(ddb_entry->sess);
310 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
314 /* finally ready to go */
315 iscsi_unblock_session(ddb_entry->sess);
319 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
321 struct ddb_entry *ddb_entry;
322 struct iscsi_cls_session *sess;
324 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
325 sizeof(struct ddb_entry));
329 ddb_entry = sess->dd_data;
330 memset(ddb_entry, 0, sizeof(*ddb_entry));
332 ddb_entry->sess = sess;
336 static void qla4xxx_scan_start(struct Scsi_Host *shost)
338 struct scsi_qla_host *ha = shost_priv(shost);
339 struct ddb_entry *ddb_entry, *ddbtemp;
341 /* finish setup of sessions that were already setup in firmware */
342 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
343 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
344 qla4xxx_add_sess(ddb_entry);
352 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
353 unsigned long interval)
355 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
356 __func__, ha->host->host_no));
357 init_timer(&ha->timer);
358 ha->timer.expires = jiffies + interval * HZ;
359 ha->timer.data = (unsigned long)ha;
360 ha->timer.function = (void (*)(unsigned long))func;
361 add_timer(&ha->timer);
362 ha->timer_active = 1;
365 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
367 del_timer_sync(&ha->timer);
368 ha->timer_active = 0;
372 * qla4xxx_mark_device_missing - mark a device as missing.
373 * @ha: Pointer to host adapter structure.
374 * @ddb_entry: Pointer to device database entry
376 * This routine marks a device missing and close connection.
378 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
379 struct ddb_entry *ddb_entry)
381 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
382 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
383 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
384 ha->host_no, ddb_entry->fw_ddb_index));
386 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
387 ddb_entry->fw_ddb_index))
389 iscsi_block_session(ddb_entry->sess);
390 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
394 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
395 * @ha: Pointer to host adapter structure.
397 * This routine marks a device missing and resets the relogin retry count.
399 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
401 struct ddb_entry *ddb_entry, *ddbtemp;
402 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
403 qla4xxx_mark_device_missing(ha, ddb_entry);
407 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
408 struct ddb_entry *ddb_entry,
409 struct scsi_cmnd *cmd,
410 void (*done)(struct scsi_cmnd *))
414 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
418 kref_init(&srb->srb_ref);
420 srb->ddb = ddb_entry;
423 CMD_SP(cmd) = (void *)srb;
424 cmd->scsi_done = done;
429 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
431 struct scsi_cmnd *cmd = srb->cmd;
433 if (srb->flags & SRB_DMA_VALID) {
435 srb->flags &= ~SRB_DMA_VALID;
440 void qla4xxx_srb_compl(struct kref *ref)
442 struct srb *srb = container_of(ref, struct srb, srb_ref);
443 struct scsi_cmnd *cmd = srb->cmd;
444 struct scsi_qla_host *ha = srb->ha;
446 qla4xxx_srb_free_dma(ha, srb);
448 mempool_free(srb, ha->srb_mempool);
454 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
455 * @cmd: Pointer to Linux's SCSI command structure
456 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
457 * that the command has been processed.
460 * This routine is invoked by Linux to send a SCSI command to the driver.
461 * The mid-level driver tries to ensure that queuecommand never gets
462 * invoked concurrently with itself or the interrupt handler (although
463 * the interrupt handler may call this routine as part of request-
464 * completion handling). Unfortunely, it sometimes calls the scheduler
465 * in interrupt context which is a big NO! NO!.
467 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
468 void (*done)(struct scsi_cmnd *))
470 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
471 struct ddb_entry *ddb_entry = cmd->device->hostdata;
472 struct iscsi_cls_session *sess = ddb_entry->sess;
476 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
477 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
478 cmd->result = DID_NO_CONNECT << 16;
480 cmd->result = DID_REQUEUE << 16;
481 goto qc_fail_command;
485 cmd->result = DID_IMM_RETRY << 16;
486 goto qc_fail_command;
489 rval = iscsi_session_chkready(sess);
492 goto qc_fail_command;
495 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
496 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
497 cmd->result = DID_NO_CONNECT << 16;
498 goto qc_fail_command;
500 return SCSI_MLQUEUE_TARGET_BUSY;
503 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
504 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
505 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
506 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
507 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
508 !test_bit(AF_ONLINE, &ha->flags) ||
509 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
512 spin_unlock_irq(ha->host->host_lock);
514 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
516 goto qc_host_busy_lock;
518 rval = qla4xxx_send_command_to_isp(ha, srb);
519 if (rval != QLA_SUCCESS)
520 goto qc_host_busy_free_sp;
522 spin_lock_irq(ha->host->host_lock);
525 qc_host_busy_free_sp:
526 qla4xxx_srb_free_dma(ha, srb);
527 mempool_free(srb, ha->srb_mempool);
530 spin_lock_irq(ha->host->host_lock);
533 return SCSI_MLQUEUE_HOST_BUSY;
542 * qla4xxx_mem_free - frees memory allocated to adapter
543 * @ha: Pointer to host adapter structure.
545 * Frees memory previously allocated by qla4xxx_mem_alloc
547 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
550 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
556 ha->request_ring = NULL;
558 ha->response_ring = NULL;
559 ha->response_dma = 0;
560 ha->shadow_regs = NULL;
561 ha->shadow_regs_dma = 0;
565 mempool_destroy(ha->srb_mempool);
567 ha->srb_mempool = NULL;
569 /* release io space registers */
570 if (is_qla8022(ha)) {
573 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
575 if (ha->nx_db_wr_ptr)
577 (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
580 pci_release_regions(ha->pdev);
584 * qla4xxx_mem_alloc - allocates memory for use by adapter.
585 * @ha: Pointer to host adapter structure
587 * Allocates DMA memory for request and response queues. Also allocates memory
590 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
594 /* Allocate contiguous block of DMA memory for queues. */
595 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
596 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
597 sizeof(struct shadow_regs) +
599 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
600 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
601 &ha->queues_dma, GFP_KERNEL);
602 if (ha->queues == NULL) {
603 ql4_printk(KERN_WARNING, ha,
604 "Memory Allocation failed - queues.\n");
606 goto mem_alloc_error_exit;
608 memset(ha->queues, 0, ha->queues_len);
611 * As per RISC alignment requirements -- the bus-address must be a
612 * multiple of the request-ring size (in bytes).
615 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
616 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
617 (MEM_ALIGN_VALUE - 1));
619 /* Update request and response queue pointers. */
620 ha->request_dma = ha->queues_dma + align;
621 ha->request_ring = (struct queue_entry *) (ha->queues + align);
622 ha->response_dma = ha->queues_dma + align +
623 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
624 ha->response_ring = (struct queue_entry *) (ha->queues + align +
625 (REQUEST_QUEUE_DEPTH *
627 ha->shadow_regs_dma = ha->queues_dma + align +
628 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
629 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
630 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
631 (REQUEST_QUEUE_DEPTH *
633 (RESPONSE_QUEUE_DEPTH *
636 /* Allocate memory for srb pool. */
637 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
638 mempool_free_slab, srb_cachep);
639 if (ha->srb_mempool == NULL) {
640 ql4_printk(KERN_WARNING, ha,
641 "Memory Allocation failed - SRB Pool.\n");
643 goto mem_alloc_error_exit;
648 mem_alloc_error_exit:
649 qla4xxx_mem_free(ha);
654 * qla4_8xxx_check_fw_alive - Check firmware health
655 * @ha: Pointer to host adapter structure.
659 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
661 uint32_t fw_heartbeat_counter, halt_status;
663 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
664 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
665 if (fw_heartbeat_counter == 0xffffffff) {
666 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
667 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
668 ha->host_no, __func__));
672 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
673 ha->seconds_since_last_heartbeat++;
674 /* FW not alive after 2 seconds */
675 if (ha->seconds_since_last_heartbeat == 2) {
676 ha->seconds_since_last_heartbeat = 0;
677 halt_status = qla4_8xxx_rd_32(ha,
678 QLA82XX_PEG_HALT_STATUS1);
680 /* Since we cannot change dev_state in interrupt
681 * context, set appropriate DPC flag then wakeup
683 if (halt_status & HALT_STATUS_UNRECOVERABLE)
684 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
686 printk("scsi%ld: %s: detect abort needed!\n",
687 ha->host_no, __func__);
688 set_bit(DPC_RESET_HA, &ha->dpc_flags);
690 qla4xxx_wake_dpc(ha);
691 qla4xxx_mailbox_premature_completion(ha);
694 ha->fw_heartbeat_counter = fw_heartbeat_counter;
698 * qla4_8xxx_watchdog - Poll dev state
699 * @ha: Pointer to host adapter structure.
703 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
707 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
709 /* don't poll if reset is going on */
710 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
711 if (dev_state == QLA82XX_DEV_NEED_RESET &&
712 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
713 printk("scsi%ld: %s: HW State: NEED RESET!\n",
714 ha->host_no, __func__);
715 set_bit(DPC_RESET_HA, &ha->dpc_flags);
716 qla4xxx_wake_dpc(ha);
717 qla4xxx_mailbox_premature_completion(ha);
718 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
719 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
720 printk("scsi%ld: %s: HW State: NEED QUIES!\n",
721 ha->host_no, __func__);
722 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
723 qla4xxx_wake_dpc(ha);
725 /* Check firmware health */
726 qla4_8xxx_check_fw_alive(ha);
732 * qla4xxx_timer - checks every second for work to do.
733 * @ha: Pointer to host adapter structure.
735 static void qla4xxx_timer(struct scsi_qla_host *ha)
737 struct ddb_entry *ddb_entry, *dtemp;
741 /* If we are in the middle of AER/EEH processing
742 * skip any processing and reschedule the timer
744 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
745 mod_timer(&ha->timer, jiffies + HZ);
749 /* Hardware read to trigger an EEH error during mailbox waits. */
750 if (!pci_channel_offline(ha->pdev))
751 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
753 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
754 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
759 if (is_qla8022(ha)) {
760 qla4_8xxx_watchdog(ha);
763 /* Search for relogin's to time-out and port down retry. */
764 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
765 /* Count down time between sending relogins */
766 if (adapter_up(ha) &&
767 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
768 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
769 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
771 if (atomic_read(&ddb_entry->retry_relogin_timer)
773 atomic_set(&ddb_entry->
776 set_bit(DPC_RELOGIN_DEVICE,
778 set_bit(DF_RELOGIN, &ddb_entry->flags);
779 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
781 ha->host_no, __func__,
782 ddb_entry->fw_ddb_index));
784 atomic_dec(&ddb_entry->
785 retry_relogin_timer);
789 /* Wait for relogin to timeout */
790 if (atomic_read(&ddb_entry->relogin_timer) &&
791 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
793 * If the relogin times out and the device is
794 * still NOT ONLINE then try and relogin again.
796 if (atomic_read(&ddb_entry->state) !=
798 ddb_entry->fw_ddb_device_state ==
799 DDB_DS_SESSION_FAILED) {
800 /* Reset retry relogin timer */
801 atomic_inc(&ddb_entry->relogin_retry_count);
802 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
803 " timed out-retrying"
806 ddb_entry->fw_ddb_index,
807 atomic_read(&ddb_entry->
808 relogin_retry_count))
811 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
812 "initate relogin after"
814 ha->host_no, ddb_entry->bus,
816 ddb_entry->fw_ddb_index,
817 ddb_entry->default_time2wait + 4)
820 atomic_set(&ddb_entry->retry_relogin_timer,
821 ddb_entry->default_time2wait + 4);
826 if (!is_qla8022(ha)) {
827 /* Check for heartbeat interval. */
828 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
829 ha->heartbeat_interval != 0) {
830 ha->seconds_since_last_heartbeat++;
831 if (ha->seconds_since_last_heartbeat >
832 ha->heartbeat_interval + 2)
833 set_bit(DPC_RESET_HA, &ha->dpc_flags);
837 /* Wakeup the dpc routine for this adapter, if needed. */
839 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
840 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
841 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
842 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
843 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
844 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
845 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
846 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
847 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
848 test_bit(DPC_AEN, &ha->dpc_flags)) &&
849 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
851 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
852 " - dpc flags = 0x%lx\n",
853 ha->host_no, __func__, ha->dpc_flags));
854 qla4xxx_wake_dpc(ha);
857 /* Reschedule timer thread to call us back in one second */
858 mod_timer(&ha->timer, jiffies + HZ);
860 DEBUG2(ha->seconds_since_last_intr++);
864 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
865 * @ha: Pointer to host adapter structure.
867 * This routine stalls the driver until all outstanding commands are returned.
868 * Caller must release the Hardware Lock prior to calling this routine.
870 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
874 struct scsi_cmnd *cmd;
876 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
878 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
879 "complete\n", WAIT_CMD_TOV));
881 while (!time_after_eq(jiffies, wtime)) {
882 spin_lock_irqsave(&ha->hardware_lock, flags);
883 /* Find a command that hasn't completed. */
884 for (index = 0; index < ha->host->can_queue; index++) {
885 cmd = scsi_host_find_tag(ha->host, index);
889 spin_unlock_irqrestore(&ha->hardware_lock, flags);
891 /* If No Commands are pending, wait is complete */
892 if (index == ha->host->can_queue)
897 /* If we timed out on waiting for commands to come back
902 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
904 uint32_t ctrl_status;
905 unsigned long flags = 0;
907 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
909 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
912 spin_lock_irqsave(&ha->hardware_lock, flags);
915 * If the SCSI Reset Interrupt bit is set, clear it.
916 * Otherwise, the Soft Reset won't work.
918 ctrl_status = readw(&ha->reg->ctrl_status);
919 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
920 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
922 /* Issue Soft Reset */
923 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
924 readl(&ha->reg->ctrl_status);
926 spin_unlock_irqrestore(&ha->hardware_lock, flags);
931 * qla4xxx_soft_reset - performs soft reset.
932 * @ha: Pointer to host adapter structure.
934 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
936 uint32_t max_wait_time;
937 unsigned long flags = 0;
938 int status = QLA_ERROR;
939 uint32_t ctrl_status;
941 qla4xxx_hw_reset(ha);
943 /* Wait until the Network Reset Intr bit is cleared */
944 max_wait_time = RESET_INTR_TOV;
946 spin_lock_irqsave(&ha->hardware_lock, flags);
947 ctrl_status = readw(&ha->reg->ctrl_status);
948 spin_unlock_irqrestore(&ha->hardware_lock, flags);
950 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
954 } while ((--max_wait_time));
956 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
957 DEBUG2(printk(KERN_WARNING
958 "scsi%ld: Network Reset Intr not cleared by "
959 "Network function, clearing it now!\n",
961 spin_lock_irqsave(&ha->hardware_lock, flags);
962 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
963 readl(&ha->reg->ctrl_status);
964 spin_unlock_irqrestore(&ha->hardware_lock, flags);
967 /* Wait until the firmware tells us the Soft Reset is done */
968 max_wait_time = SOFT_RESET_TOV;
970 spin_lock_irqsave(&ha->hardware_lock, flags);
971 ctrl_status = readw(&ha->reg->ctrl_status);
972 spin_unlock_irqrestore(&ha->hardware_lock, flags);
974 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
975 status = QLA_SUCCESS;
980 } while ((--max_wait_time));
983 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
984 * after the soft reset has taken place.
986 spin_lock_irqsave(&ha->hardware_lock, flags);
987 ctrl_status = readw(&ha->reg->ctrl_status);
988 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
989 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
990 readl(&ha->reg->ctrl_status);
992 spin_unlock_irqrestore(&ha->hardware_lock, flags);
994 /* If soft reset fails then most probably the bios on other
995 * function is also enabled.
996 * Since the initialization is sequential the other fn
997 * wont be able to acknowledge the soft reset.
998 * Issue a force soft reset to workaround this scenario.
1000 if (max_wait_time == 0) {
1001 /* Issue Force Soft Reset */
1002 spin_lock_irqsave(&ha->hardware_lock, flags);
1003 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1004 readl(&ha->reg->ctrl_status);
1005 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1006 /* Wait until the firmware tells us the Soft Reset is done */
1007 max_wait_time = SOFT_RESET_TOV;
1009 spin_lock_irqsave(&ha->hardware_lock, flags);
1010 ctrl_status = readw(&ha->reg->ctrl_status);
1011 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1013 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1014 status = QLA_SUCCESS;
1019 } while ((--max_wait_time));
1026 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
1027 * @ha: Pointer to host adapter structure.
1028 * @res: returned scsi status
1030 * This routine is called just prior to a HARD RESET to return all
1031 * outstanding commands back to the Operating System.
1032 * Caller should make sure that the following locks are released
1033 * before this calling routine: Hardware lock, and io_request_lock.
1035 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
1039 unsigned long flags;
1041 spin_lock_irqsave(&ha->hardware_lock, flags);
1042 for (i = 0; i < ha->host->can_queue; i++) {
1043 srb = qla4xxx_del_from_active_array(ha, i);
1045 srb->cmd->result = res;
1046 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1049 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1052 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1054 clear_bit(AF_ONLINE, &ha->flags);
1056 /* Disable the board */
1057 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1058 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1060 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1061 qla4xxx_mark_all_devices_missing(ha);
1062 clear_bit(AF_INIT_DONE, &ha->flags);
1066 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1067 * @ha: Pointer to host adapter structure.
1069 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
1071 int status = QLA_ERROR;
1072 uint8_t reset_chip = 0;
1074 /* Stall incoming I/O until we are done */
1075 scsi_block_requests(ha->host);
1076 clear_bit(AF_ONLINE, &ha->flags);
1078 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
1080 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1082 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1085 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1086 * do not reset adapter, jump to initialize_adapter */
1087 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1088 status = QLA_SUCCESS;
1089 goto recover_ha_init_adapter;
1092 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1093 * from eh_host_reset or ioctl module */
1094 if (is_qla8022(ha) && !reset_chip &&
1095 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1097 DEBUG2(ql4_printk(KERN_INFO, ha,
1098 "scsi%ld: %s - Performing stop_firmware...\n",
1099 ha->host_no, __func__));
1100 status = ha->isp_ops->reset_firmware(ha);
1101 if (status == QLA_SUCCESS) {
1102 qla4xxx_cmd_wait(ha);
1103 ha->isp_ops->disable_intrs(ha);
1104 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1105 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1107 /* If the stop_firmware fails then
1108 * reset the entire chip */
1110 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1111 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1115 /* Issue full chip reset if recovering from a catastrophic error,
1116 * or if stop_firmware fails for ISP-82xx.
1117 * This is the default case for ISP-4xxx */
1118 if (!is_qla8022(ha) || reset_chip) {
1119 qla4xxx_cmd_wait(ha);
1120 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1121 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1122 DEBUG2(ql4_printk(KERN_INFO, ha,
1123 "scsi%ld: %s - Performing chip reset..\n",
1124 ha->host_no, __func__));
1125 status = ha->isp_ops->reset_chip(ha);
1128 /* Flush any pending ddb changed AENs */
1129 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1131 recover_ha_init_adapter:
1132 /* Upon successful firmware/chip reset, re-initialize the adapter */
1133 if (status == QLA_SUCCESS) {
1134 /* For ISP-4xxx, force function 1 to always initialize
1135 * before function 3 to prevent both funcions from
1136 * stepping on top of the other */
1137 if (!is_qla8022(ha) && (ha->mac_index == 3))
1140 /* NOTE: AF_ONLINE flag set upon successful completion of
1141 * qla4xxx_initialize_adapter */
1142 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1145 /* Retry failed adapter initialization, if necessary
1146 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1147 * case to prevent ping-pong resets between functions */
1148 if (!test_bit(AF_ONLINE, &ha->flags) &&
1149 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1150 /* Adapter initialization failed, see if we can retry
1152 * Since we don't want to block the DPC for too long
1153 * with multiple resets in the same thread,
1154 * utilize DPC to retry */
1155 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1156 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1157 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1158 "(%d) more times\n", ha->host_no,
1159 ha->retry_reset_ha_cnt));
1160 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1163 if (ha->retry_reset_ha_cnt > 0) {
1164 /* Schedule another Reset HA--DPC will retry */
1165 ha->retry_reset_ha_cnt--;
1166 DEBUG2(printk("scsi%ld: recover adapter - "
1167 "retry remaining %d\n",
1169 ha->retry_reset_ha_cnt));
1173 if (ha->retry_reset_ha_cnt == 0) {
1174 /* Recover adapter retries have been exhausted.
1176 DEBUG2(printk("scsi%ld: recover adapter "
1177 "failed - board disabled\n",
1179 qla4xxx_dead_adapter_cleanup(ha);
1180 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1181 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1182 clear_bit(DPC_RESET_HA_FW_CONTEXT,
1188 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1189 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1190 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1193 ha->adapter_error_count++;
1195 if (test_bit(AF_ONLINE, &ha->flags))
1196 ha->isp_ops->enable_intrs(ha);
1198 scsi_unblock_requests(ha->host);
1200 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1201 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1202 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1207 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1209 if (ha->dpc_thread &&
1210 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1211 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1212 queue_work(ha->dpc_thread, &ha->dpc_work);
1217 * qla4xxx_do_dpc - dpc routine
1218 * @data: in our case pointer to adapter structure
1220 * This routine is a task that is schedule by the interrupt handler
1221 * to perform the background processing for interrupts. We put it
1222 * on a task queue that is consumed whenever the scheduler runs; that's
1223 * so you can do anything (i.e. put the process to sleep etc). In fact,
1224 * the mid-level tries to sleep when it reaches the driver threshold
1225 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1227 static void qla4xxx_do_dpc(struct work_struct *work)
1229 struct scsi_qla_host *ha =
1230 container_of(work, struct scsi_qla_host, dpc_work);
1231 struct ddb_entry *ddb_entry, *dtemp;
1232 int status = QLA_ERROR;
1234 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1235 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1236 ha->host_no, __func__, ha->flags, ha->dpc_flags))
1238 /* Initialization not yet finished. Don't do anything yet. */
1239 if (!test_bit(AF_INIT_DONE, &ha->flags))
1242 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1243 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1244 ha->host_no, __func__, ha->flags));
1248 /* HBA is in the process of being permanently disabled.
1249 * Don't process anything */
1250 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1253 if (is_qla8022(ha)) {
1254 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1255 qla4_8xxx_idc_lock(ha);
1256 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1257 QLA82XX_DEV_FAILED);
1258 qla4_8xxx_idc_unlock(ha);
1259 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1260 qla4_8xxx_device_state_handler(ha);
1262 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1263 qla4_8xxx_need_qsnt_handler(ha);
1267 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1268 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1269 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1270 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1271 if (ql4xdontresethba) {
1272 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1273 ha->host_no, __func__));
1274 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1275 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1276 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1277 goto dpc_post_reset_ha;
1279 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1280 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1281 qla4xxx_recover_adapter(ha);
1283 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1284 uint8_t wait_time = RESET_INTR_TOV;
1286 while ((readw(&ha->reg->ctrl_status) &
1287 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1288 if (--wait_time == 0)
1293 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1294 "bit not cleared-- resetting\n",
1295 ha->host_no, __func__));
1296 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1297 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1298 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1299 status = qla4xxx_recover_adapter(ha);
1301 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1302 if (status == QLA_SUCCESS)
1303 ha->isp_ops->enable_intrs(ha);
1308 /* ---- process AEN? --- */
1309 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1310 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1312 /* ---- Get DHCP IP Address? --- */
1313 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1314 qla4xxx_get_dhcp_ip_address(ha);
1316 /* ---- link change? --- */
1317 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1318 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1319 /* ---- link down? --- */
1320 list_for_each_entry_safe(ddb_entry, dtemp,
1321 &ha->ddb_list, list) {
1322 if (atomic_read(&ddb_entry->state) ==
1324 qla4xxx_mark_device_missing(ha,
1328 /* ---- link up? --- *
1329 * F/W will auto login to all devices ONLY ONCE after
1330 * link up during driver initialization and runtime
1331 * fatal error recovery. Therefore, the driver must
1332 * manually relogin to devices when recovering from
1333 * connection failures, logouts, expired KATO, etc. */
1335 list_for_each_entry_safe(ddb_entry, dtemp,
1336 &ha->ddb_list, list) {
1337 if ((atomic_read(&ddb_entry->state) ==
1338 DDB_STATE_MISSING) ||
1339 (atomic_read(&ddb_entry->state) ==
1341 if (ddb_entry->fw_ddb_device_state ==
1342 DDB_DS_SESSION_ACTIVE) {
1343 atomic_set(&ddb_entry->state,
1345 ql4_printk(KERN_INFO, ha,
1346 "scsi%ld: %s: ddb[%d]"
1348 ha->host_no, __func__,
1349 ddb_entry->fw_ddb_index);
1351 iscsi_unblock_session(
1354 qla4xxx_relogin_device(
1362 /* ---- relogin device? --- */
1363 if (adapter_up(ha) &&
1364 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1365 list_for_each_entry_safe(ddb_entry, dtemp,
1366 &ha->ddb_list, list) {
1367 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1368 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1369 qla4xxx_relogin_device(ha, ddb_entry);
1372 * If mbx cmd times out there is no point
1373 * in continuing further.
1374 * With large no of targets this can hang
1377 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1378 printk(KERN_WARNING "scsi%ld: %s: "
1379 "need to reset hba\n",
1380 ha->host_no, __func__);
1387 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
1391 * qla4xxx_free_adapter - release the adapter
1392 * @ha: pointer to adapter structure
1394 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1397 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1398 /* Turn-off interrupts on the card. */
1399 ha->isp_ops->disable_intrs(ha);
1402 /* Remove timer thread, if present */
1403 if (ha->timer_active)
1404 qla4xxx_stop_timer(ha);
1406 /* Kill the kernel thread for this host */
1408 destroy_workqueue(ha->dpc_thread);
1410 /* Put firmware in known state */
1411 ha->isp_ops->reset_firmware(ha);
1413 if (is_qla8022(ha)) {
1414 qla4_8xxx_idc_lock(ha);
1415 qla4_8xxx_clear_drv_active(ha);
1416 qla4_8xxx_idc_unlock(ha);
1419 /* Detach interrupts */
1420 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1421 qla4xxx_free_irqs(ha);
1423 /* free extra memory */
1424 qla4xxx_mem_free(ha);
1427 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1430 uint8_t revision_id;
1431 unsigned long mem_base, mem_len, db_base, db_len;
1432 struct pci_dev *pdev = ha->pdev;
1434 status = pci_request_regions(pdev, DRIVER_NAME);
1437 "scsi(%ld) Failed to reserve PIO regions (%s) "
1438 "status=%d\n", ha->host_no, pci_name(pdev), status);
1439 goto iospace_error_exit;
1442 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1443 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1444 __func__, revision_id));
1445 ha->revision_id = revision_id;
1447 /* remap phys address */
1448 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1449 mem_len = pci_resource_len(pdev, 0);
1450 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1451 __func__, mem_base, mem_len));
1453 /* mapping of pcibase pointer */
1454 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1455 if (!ha->nx_pcibase) {
1457 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1458 pci_release_regions(ha->pdev);
1459 goto iospace_error_exit;
1462 /* Mapping of IO base pointer, door bell read and write pointer */
1464 /* mapping of IO base pointer */
1466 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1467 0xbc000 + (ha->pdev->devfn << 11));
1469 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1470 db_len = pci_resource_len(pdev, 4);
1472 /* mapping of doorbell write pointer */
1473 ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1474 (ha->pdev->devfn << 12), 4);
1475 if (!ha->nx_db_wr_ptr) {
1477 "cannot remap MMIO doorbell-write (%s), aborting\n",
1479 goto iospace_error_exit;
1481 /* mapping of doorbell read pointer */
1482 ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1483 (ha->pdev->devfn * 8);
1484 if (!ha->nx_db_rd_ptr)
1486 "cannot remap MMIO doorbell-read (%s), aborting\n",
1495 * qla4xxx_iospace_config - maps registers
1496 * @ha: pointer to adapter structure
1498 * This routines maps HBA's registers from the pci address space
1499 * into the kernel virtual address space for memory mapped i/o.
1501 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1503 unsigned long pio, pio_len, pio_flags;
1504 unsigned long mmio, mmio_len, mmio_flags;
1506 pio = pci_resource_start(ha->pdev, 0);
1507 pio_len = pci_resource_len(ha->pdev, 0);
1508 pio_flags = pci_resource_flags(ha->pdev, 0);
1509 if (pio_flags & IORESOURCE_IO) {
1510 if (pio_len < MIN_IOBASE_LEN) {
1511 ql4_printk(KERN_WARNING, ha,
1512 "Invalid PCI I/O region size\n");
1516 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
1520 /* Use MMIO operations for all accesses. */
1521 mmio = pci_resource_start(ha->pdev, 1);
1522 mmio_len = pci_resource_len(ha->pdev, 1);
1523 mmio_flags = pci_resource_flags(ha->pdev, 1);
1525 if (!(mmio_flags & IORESOURCE_MEM)) {
1526 ql4_printk(KERN_ERR, ha,
1527 "region #0 not an MMIO resource, aborting\n");
1529 goto iospace_error_exit;
1532 if (mmio_len < MIN_IOBASE_LEN) {
1533 ql4_printk(KERN_ERR, ha,
1534 "Invalid PCI mem region size, aborting\n");
1535 goto iospace_error_exit;
1538 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1539 ql4_printk(KERN_WARNING, ha,
1540 "Failed to reserve PIO/MMIO regions\n");
1542 goto iospace_error_exit;
1545 ha->pio_address = pio;
1546 ha->pio_length = pio_len;
1547 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1549 ql4_printk(KERN_ERR, ha,
1550 "cannot remap MMIO, aborting\n");
1552 goto iospace_error_exit;
1561 static struct isp_operations qla4xxx_isp_ops = {
1562 .iospace_config = qla4xxx_iospace_config,
1563 .pci_config = qla4xxx_pci_config,
1564 .disable_intrs = qla4xxx_disable_intrs,
1565 .enable_intrs = qla4xxx_enable_intrs,
1566 .start_firmware = qla4xxx_start_firmware,
1567 .intr_handler = qla4xxx_intr_handler,
1568 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1569 .reset_chip = qla4xxx_soft_reset,
1570 .reset_firmware = qla4xxx_hw_reset,
1571 .queue_iocb = qla4xxx_queue_iocb,
1572 .complete_iocb = qla4xxx_complete_iocb,
1573 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1574 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1575 .get_sys_info = qla4xxx_get_sys_info,
1578 static struct isp_operations qla4_8xxx_isp_ops = {
1579 .iospace_config = qla4_8xxx_iospace_config,
1580 .pci_config = qla4_8xxx_pci_config,
1581 .disable_intrs = qla4_8xxx_disable_intrs,
1582 .enable_intrs = qla4_8xxx_enable_intrs,
1583 .start_firmware = qla4_8xxx_load_risc,
1584 .intr_handler = qla4_8xxx_intr_handler,
1585 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1586 .reset_chip = qla4_8xxx_isp_reset,
1587 .reset_firmware = qla4_8xxx_stop_firmware,
1588 .queue_iocb = qla4_8xxx_queue_iocb,
1589 .complete_iocb = qla4_8xxx_complete_iocb,
1590 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1591 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1592 .get_sys_info = qla4_8xxx_get_sys_info,
1595 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1597 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1600 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1602 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1605 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1607 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1610 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1612 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1616 * qla4xxx_probe_adapter - callback function to probe HBA
1617 * @pdev: pointer to pci_dev structure
1618 * @pci_device_id: pointer to pci_device entry
1620 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1621 * It returns zero if successful. It also initializes all data necessary for
1624 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1625 const struct pci_device_id *ent)
1627 int ret = -ENODEV, status;
1628 struct Scsi_Host *host;
1629 struct scsi_qla_host *ha;
1630 uint8_t init_retry_count = 0;
1632 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
1634 if (pci_enable_device(pdev))
1637 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1640 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1641 goto probe_disable_device;
1644 /* Clear our data area */
1645 ha = (struct scsi_qla_host *) host->hostdata;
1646 memset(ha, 0, sizeof(*ha));
1648 /* Save the information from PCI BIOS. */
1651 ha->host_no = host->host_no;
1653 pci_enable_pcie_error_reporting(pdev);
1655 /* Setup Runtime configurable options */
1656 if (is_qla8022(ha)) {
1657 ha->isp_ops = &qla4_8xxx_isp_ops;
1658 rwlock_init(&ha->hw_lock);
1659 ha->qdr_sn_window = -1;
1660 ha->ddr_mn_window = -1;
1661 ha->curr_window = 255;
1662 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1663 nx_legacy_intr = &legacy_intr[ha->func_num];
1664 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1665 ha->nx_legacy_intr.tgt_status_reg =
1666 nx_legacy_intr->tgt_status_reg;
1667 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1668 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1670 ha->isp_ops = &qla4xxx_isp_ops;
1673 /* Set EEH reset type to fundamental if required by hba */
1675 pdev->needs_freset = 1;
1677 /* Configure PCI I/O space. */
1678 ret = ha->isp_ops->iospace_config(ha);
1680 goto probe_failed_ioconfig;
1682 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1683 pdev->device, pdev->irq, ha->reg);
1685 qla4xxx_config_dma_addressing(ha);
1687 /* Initialize lists and spinlocks. */
1688 INIT_LIST_HEAD(&ha->ddb_list);
1689 INIT_LIST_HEAD(&ha->free_srb_q);
1691 mutex_init(&ha->mbox_sem);
1692 init_completion(&ha->mbx_intr_comp);
1694 spin_lock_init(&ha->hardware_lock);
1696 /* Allocate dma buffers */
1697 if (qla4xxx_mem_alloc(ha)) {
1698 ql4_printk(KERN_WARNING, ha,
1699 "[ERROR] Failed to allocate memory for adapter\n");
1706 (void) qla4_8xxx_get_flash_info(ha);
1709 * Initialize the Host adapter request/response queues and
1711 * NOTE: interrupts enabled upon successful completion
1713 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1714 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1715 init_retry_count++ < MAX_INIT_RETRIES) {
1716 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1717 "(%d)\n", __func__, init_retry_count));
1719 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1722 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1725 if (!test_bit(AF_ONLINE, &ha->flags)) {
1726 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
1732 host->cmd_per_lun = 3;
1733 host->max_channel = 0;
1734 host->max_lun = MAX_LUNS - 1;
1735 host->max_id = MAX_TARGETS;
1736 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1737 host->can_queue = MAX_SRBS ;
1738 host->transportt = qla4xxx_scsi_transport;
1740 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1742 ql4_printk(KERN_WARNING, ha,
1743 "scsi_init_shared_tag_map failed\n");
1747 /* Startup the kernel thread for this host adapter. */
1748 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1749 "qla4xxx_dpc\n", __func__));
1750 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1751 ha->dpc_thread = create_singlethread_workqueue(buf);
1752 if (!ha->dpc_thread) {
1753 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
1757 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1759 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1760 * (which is called indirectly by qla4xxx_initialize_adapter),
1761 * so that irqs will be registered after crbinit but before
1764 if (!is_qla8022(ha)) {
1765 ret = qla4xxx_request_irqs(ha);
1767 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1768 "interrupt %d already in use.\n", pdev->irq);
1773 pci_save_state(ha->pdev);
1774 ha->isp_ops->enable_intrs(ha);
1776 /* Start timer thread. */
1777 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1779 set_bit(AF_INIT_DONE, &ha->flags);
1781 pci_set_drvdata(pdev, ha);
1783 ret = scsi_add_host(host, &pdev->dev);
1788 " QLogic iSCSI HBA Driver version: %s\n"
1789 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1790 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1791 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1792 ha->patch_number, ha->build_number);
1793 scsi_scan_host(host);
1797 qla4xxx_free_adapter(ha);
1799 probe_failed_ioconfig:
1800 pci_disable_pcie_error_reporting(pdev);
1801 scsi_host_put(ha->host);
1803 probe_disable_device:
1804 pci_disable_device(pdev);
1810 * qla4xxx_remove_adapter - calback function to remove adapter.
1811 * @pci_dev: PCI device pointer
1813 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1815 struct scsi_qla_host *ha;
1817 ha = pci_get_drvdata(pdev);
1819 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1821 /* remove devs from iscsi_sessions to scsi_devices */
1822 qla4xxx_free_ddb_list(ha);
1824 scsi_remove_host(ha->host);
1826 qla4xxx_free_adapter(ha);
1828 scsi_host_put(ha->host);
1830 pci_disable_pcie_error_reporting(pdev);
1831 pci_disable_device(pdev);
1832 pci_set_drvdata(pdev, NULL);
1836 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1839 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1840 * supported addressing method.
1842 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1846 /* Update our PCI device dma_mask for full 64 bit mask */
1847 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1848 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1849 dev_dbg(&ha->pdev->dev,
1850 "Failed to set 64 bit PCI consistent mask; "
1852 retval = pci_set_consistent_dma_mask(ha->pdev,
1856 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1859 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1861 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1862 struct ddb_entry *ddb = sess->dd_data;
1864 sdev->hostdata = ddb;
1865 sdev->tagged_supported = 1;
1866 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
1870 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1872 sdev->tagged_supported = 1;
1876 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1878 scsi_deactivate_tcq(sdev, 1);
1882 * qla4xxx_del_from_active_array - returns an active srb
1883 * @ha: Pointer to host adapter structure.
1884 * @index: index into the active_array
1886 * This routine removes and returns the srb at the specified index
1888 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1891 struct srb *srb = NULL;
1892 struct scsi_cmnd *cmd = NULL;
1894 cmd = scsi_host_find_tag(ha->host, index);
1898 srb = (struct srb *)CMD_SP(cmd);
1902 /* update counters */
1903 if (srb->flags & SRB_DMA_VALID) {
1904 ha->req_q_count += srb->iocb_cnt;
1905 ha->iocb_cnt -= srb->iocb_cnt;
1907 srb->cmd->host_scribble =
1908 (unsigned char *)(unsigned long) MAX_SRBS;
1914 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1915 * @ha: Pointer to host adapter structure.
1916 * @cmd: Scsi Command to wait on.
1918 * This routine waits for the command to be returned by the Firmware
1919 * for some max time.
1921 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1922 struct scsi_cmnd *cmd)
1926 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1929 /* Dont wait on command if PCI error is being handled
1932 if (unlikely(pci_channel_offline(ha->pdev)) ||
1933 (test_bit(AF_EEH_BUSY, &ha->flags))) {
1934 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1935 ha->host_no, __func__);
1940 /* Checking to see if its returned to OS */
1941 rp = (struct srb *) CMD_SP(cmd);
1948 } while (max_wait_time--);
1954 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1955 * @ha: Pointer to host adapter structure
1957 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1959 unsigned long wait_online;
1961 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
1962 while (time_before(jiffies, wait_online)) {
1966 else if (ha->retry_reset_ha_cnt == 0)
1976 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
1977 * @ha: pointer to HBA
1981 * This function waits for all outstanding commands to a lun to complete. It
1982 * returns 0 if all pending commands are returned and 1 otherwise.
1984 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1985 struct scsi_target *stgt,
1986 struct scsi_device *sdev)
1990 struct scsi_cmnd *cmd;
1993 * Waiting for all commands for the designated target or dev
1994 * in the active array
1996 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1997 cmd = scsi_host_find_tag(ha->host, cnt);
1998 if (cmd && stgt == scsi_target(cmd->device) &&
1999 (!sdev || sdev == cmd->device)) {
2000 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2010 * qla4xxx_eh_abort - callback for abort task.
2011 * @cmd: Pointer to Linux's SCSI command structure
2013 * This routine is called by the Linux OS to abort the specified
2016 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2018 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2019 unsigned int id = cmd->device->id;
2020 unsigned int lun = cmd->device->lun;
2021 unsigned long serial = cmd->serial_number;
2022 struct srb *srb = NULL;
2026 ql4_printk(KERN_INFO, ha,
2027 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
2028 ha->host_no, id, lun, cmd, serial);
2030 srb = (struct srb *) CMD_SP(cmd);
2035 kref_get(&srb->srb_ref);
2037 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2038 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2039 ha->host_no, id, lun));
2042 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2043 ha->host_no, id, lun));
2047 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2049 /* Wait for command to complete */
2051 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2052 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2053 ha->host_no, id, lun));
2058 ql4_printk(KERN_INFO, ha,
2059 "scsi%ld:%d:%d: Abort command - %s\n",
2060 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2066 * qla4xxx_eh_device_reset - callback for target reset.
2067 * @cmd: Pointer to Linux's SCSI command structure
2069 * This routine is called by the Linux OS to reset all luns on the
2072 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2074 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2075 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2076 int ret = FAILED, stat;
2081 ret = iscsi_block_scsi_eh(cmd);
2086 ql4_printk(KERN_INFO, ha,
2087 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2088 cmd->device->channel, cmd->device->id, cmd->device->lun);
2090 DEBUG2(printk(KERN_INFO
2091 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2092 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
2093 cmd, jiffies, cmd->request->timeout / HZ,
2094 ha->dpc_flags, cmd->result, cmd->allowed));
2096 /* FIXME: wait for hba to go online */
2097 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2098 if (stat != QLA_SUCCESS) {
2099 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
2100 goto eh_dev_reset_done;
2103 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2105 ql4_printk(KERN_INFO, ha,
2106 "DEVICE RESET FAILED - waiting for "
2108 goto eh_dev_reset_done;
2112 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2113 MM_LUN_RESET) != QLA_SUCCESS)
2114 goto eh_dev_reset_done;
2116 ql4_printk(KERN_INFO, ha,
2117 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2118 ha->host_no, cmd->device->channel, cmd->device->id,
2129 * qla4xxx_eh_target_reset - callback for target reset.
2130 * @cmd: Pointer to Linux's SCSI command structure
2132 * This routine is called by the Linux OS to reset the target.
2134 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2136 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2137 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2143 ret = iscsi_block_scsi_eh(cmd);
2147 starget_printk(KERN_INFO, scsi_target(cmd->device),
2148 "WARM TARGET RESET ISSUED.\n");
2150 DEBUG2(printk(KERN_INFO
2151 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2152 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
2153 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
2154 ha->dpc_flags, cmd->result, cmd->allowed));
2156 stat = qla4xxx_reset_target(ha, ddb_entry);
2157 if (stat != QLA_SUCCESS) {
2158 starget_printk(KERN_INFO, scsi_target(cmd->device),
2159 "WARM TARGET RESET FAILED.\n");
2163 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2165 starget_printk(KERN_INFO, scsi_target(cmd->device),
2166 "WARM TARGET DEVICE RESET FAILED - "
2167 "waiting for commands.\n");
2172 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2173 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2174 starget_printk(KERN_INFO, scsi_target(cmd->device),
2175 "WARM TARGET DEVICE RESET FAILED - "
2176 "marker iocb failed.\n");
2180 starget_printk(KERN_INFO, scsi_target(cmd->device),
2181 "WARM TARGET RESET SUCCEEDED.\n");
2186 * qla4xxx_eh_host_reset - kernel callback
2187 * @cmd: Pointer to Linux's SCSI command structure
2189 * This routine is invoked by the Linux kernel to perform fatal error
2190 * recovery on the specified adapter.
2192 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2194 int return_status = FAILED;
2195 struct scsi_qla_host *ha;
2197 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2199 if (ql4xdontresethba) {
2200 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2201 ha->host_no, __func__));
2205 ql4_printk(KERN_INFO, ha,
2206 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
2207 cmd->device->channel, cmd->device->id, cmd->device->lun);
2209 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2210 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2211 "DEAD.\n", ha->host_no, cmd->device->channel,
2217 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2219 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2221 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2224 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
2225 return_status = SUCCESS;
2227 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
2228 return_status == FAILED ? "FAILED" : "SUCCEDED");
2230 return return_status;
2233 /* PCI AER driver recovers from all correctable errors w/o
2234 * driver intervention. For uncorrectable errors PCI AER
2235 * driver calls the following device driver's callbacks
2237 * - Fatal Errors - link_reset
2238 * - Non-Fatal Errors - driver's pci_error_detected() which
2239 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2241 * PCI AER driver calls
2242 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2243 * returns RECOVERED or NEED_RESET if fw_hung
2244 * NEED_RESET - driver's slot_reset()
2245 * DISCONNECT - device is dead & cannot recover
2246 * RECOVERED - driver's pci_resume()
2248 static pci_ers_result_t
2249 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2251 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2253 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2254 ha->host_no, __func__, state);
2256 if (!is_aer_supported(ha))
2257 return PCI_ERS_RESULT_NONE;
2260 case pci_channel_io_normal:
2261 clear_bit(AF_EEH_BUSY, &ha->flags);
2262 return PCI_ERS_RESULT_CAN_RECOVER;
2263 case pci_channel_io_frozen:
2264 set_bit(AF_EEH_BUSY, &ha->flags);
2265 qla4xxx_mailbox_premature_completion(ha);
2266 qla4xxx_free_irqs(ha);
2267 pci_disable_device(pdev);
2268 return PCI_ERS_RESULT_NEED_RESET;
2269 case pci_channel_io_perm_failure:
2270 set_bit(AF_EEH_BUSY, &ha->flags);
2271 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2272 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2273 return PCI_ERS_RESULT_DISCONNECT;
2275 return PCI_ERS_RESULT_NEED_RESET;
2279 * qla4xxx_pci_mmio_enabled() gets called if
2280 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2281 * and read/write to the device still works.
2283 static pci_ers_result_t
2284 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2286 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2288 if (!is_aer_supported(ha))
2289 return PCI_ERS_RESULT_NONE;
2291 if (test_bit(AF_FW_RECOVERY, &ha->flags)) {
2292 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: firmware hang -- "
2293 "mmio_enabled\n", ha->host_no, __func__);
2294 return PCI_ERS_RESULT_NEED_RESET;
2296 return PCI_ERS_RESULT_RECOVERED;
2299 uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2301 uint32_t rval = QLA_ERROR;
2303 struct pci_dev *other_pdev = NULL;
2305 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2307 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2309 if (test_bit(AF_ONLINE, &ha->flags)) {
2310 clear_bit(AF_ONLINE, &ha->flags);
2311 qla4xxx_mark_all_devices_missing(ha);
2312 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2313 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2316 fn = PCI_FUNC(ha->pdev->devfn);
2319 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2320 "func %x\n", ha->host_no, __func__, fn);
2321 /* Get the pci device given the domain, bus,
2322 * slot/function number */
2324 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2325 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2331 if (atomic_read(&other_pdev->enable_cnt)) {
2332 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2333 "func in enabled state%x\n", ha->host_no,
2335 pci_dev_put(other_pdev);
2338 pci_dev_put(other_pdev);
2341 /* The first function on the card, the reset owner will
2342 * start & initialize the firmware. The other functions
2343 * on the card will reset the firmware context
2346 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2347 "0x%x is the owner\n", ha->host_no, __func__,
2350 qla4_8xxx_idc_lock(ha);
2351 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2354 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2355 QLA82XX_IDC_VERSION);
2357 qla4_8xxx_idc_unlock(ha);
2358 clear_bit(AF_FW_RECOVERY, &ha->flags);
2359 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2360 qla4_8xxx_idc_lock(ha);
2362 if (rval != QLA_SUCCESS) {
2363 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2364 "FAILED\n", ha->host_no, __func__);
2365 qla4_8xxx_clear_drv_active(ha);
2366 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2367 QLA82XX_DEV_FAILED);
2369 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2370 "READY\n", ha->host_no, __func__);
2371 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2373 /* Clear driver state register */
2374 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2375 qla4_8xxx_set_drv_active(ha);
2376 ha->isp_ops->enable_intrs(ha);
2378 qla4_8xxx_idc_unlock(ha);
2380 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2381 "the reset owner\n", ha->host_no, __func__,
2383 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2384 QLA82XX_DEV_READY)) {
2385 clear_bit(AF_FW_RECOVERY, &ha->flags);
2386 rval = qla4xxx_initialize_adapter(ha,
2388 if (rval == QLA_SUCCESS)
2389 ha->isp_ops->enable_intrs(ha);
2390 qla4_8xxx_idc_lock(ha);
2391 qla4_8xxx_set_drv_active(ha);
2392 qla4_8xxx_idc_unlock(ha);
2395 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2399 static pci_ers_result_t
2400 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2402 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2403 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2406 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2407 ha->host_no, __func__);
2409 if (!is_aer_supported(ha))
2410 return PCI_ERS_RESULT_NONE;
2412 /* Restore the saved state of PCIe device -
2413 * BAR registers, PCI Config space, PCIX, MSI,
2416 pci_restore_state(pdev);
2418 /* pci_restore_state() clears the saved_state flag of the device
2419 * save restored state which resets saved_state flag
2421 pci_save_state(pdev);
2423 /* Initialize device or resume if in suspended state */
2424 rc = pci_enable_device(pdev);
2426 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
2427 "device after reset\n", ha->host_no, __func__);
2428 goto exit_slot_reset;
2431 ret = qla4xxx_request_irqs(ha);
2433 ql4_printk(KERN_WARNING, ha, "Failed to reserve interrupt %d"
2434 " already in use.\n", pdev->irq);
2435 goto exit_slot_reset;
2438 if (is_qla8022(ha)) {
2439 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2440 ret = PCI_ERS_RESULT_RECOVERED;
2441 goto exit_slot_reset;
2443 goto exit_slot_reset;
2447 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2448 "device after reset\n", ha->host_no, __func__, ret);
2453 qla4xxx_pci_resume(struct pci_dev *pdev)
2455 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2458 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2459 ha->host_no, __func__);
2461 ret = qla4xxx_wait_for_hba_online(ha);
2462 if (ret != QLA_SUCCESS) {
2463 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2464 "resume I/O from slot/link_reset\n", ha->host_no,
2468 pci_cleanup_aer_uncorrect_error_status(pdev);
2469 clear_bit(AF_EEH_BUSY, &ha->flags);
2472 static struct pci_error_handlers qla4xxx_err_handler = {
2473 .error_detected = qla4xxx_pci_error_detected,
2474 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2475 .slot_reset = qla4xxx_pci_slot_reset,
2476 .resume = qla4xxx_pci_resume,
2479 static struct pci_device_id qla4xxx_pci_tbl[] = {
2481 .vendor = PCI_VENDOR_ID_QLOGIC,
2482 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2483 .subvendor = PCI_ANY_ID,
2484 .subdevice = PCI_ANY_ID,
2487 .vendor = PCI_VENDOR_ID_QLOGIC,
2488 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2489 .subvendor = PCI_ANY_ID,
2490 .subdevice = PCI_ANY_ID,
2493 .vendor = PCI_VENDOR_ID_QLOGIC,
2494 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2495 .subvendor = PCI_ANY_ID,
2496 .subdevice = PCI_ANY_ID,
2499 .vendor = PCI_VENDOR_ID_QLOGIC,
2500 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2501 .subvendor = PCI_ANY_ID,
2502 .subdevice = PCI_ANY_ID,
2506 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2508 static struct pci_driver qla4xxx_pci_driver = {
2509 .name = DRIVER_NAME,
2510 .id_table = qla4xxx_pci_tbl,
2511 .probe = qla4xxx_probe_adapter,
2512 .remove = qla4xxx_remove_adapter,
2513 .err_handler = &qla4xxx_err_handler,
2516 static int __init qla4xxx_module_init(void)
2520 /* Allocate cache for SRBs. */
2521 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
2522 SLAB_HWCACHE_ALIGN, NULL);
2523 if (srb_cachep == NULL) {
2525 "%s: Unable to allocate SRB cache..."
2526 "Failing load!\n", DRIVER_NAME);
2531 /* Derive version string. */
2532 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
2533 if (ql4xextended_error_logging)
2534 strcat(qla4xxx_version_str, "-debug");
2536 qla4xxx_scsi_transport =
2537 iscsi_register_transport(&qla4xxx_iscsi_transport);
2538 if (!qla4xxx_scsi_transport){
2540 goto release_srb_cache;
2543 ret = pci_register_driver(&qla4xxx_pci_driver);
2545 goto unregister_transport;
2547 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2550 unregister_transport:
2551 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2553 kmem_cache_destroy(srb_cachep);
2558 static void __exit qla4xxx_module_exit(void)
2560 pci_unregister_driver(&qla4xxx_pci_driver);
2561 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2562 kmem_cache_destroy(srb_cachep);
2565 module_init(qla4xxx_module_init);
2566 module_exit(qla4xxx_module_exit);
2568 MODULE_AUTHOR("QLogic Corporation");
2569 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2570 MODULE_LICENSE("GPL");
2571 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);