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));
171 qla4xxx_wake_dpc(ha);
175 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
176 enum iscsi_host_param param, char *buf)
178 struct scsi_qla_host *ha = to_qla_host(shost);
182 case ISCSI_HOST_PARAM_HWADDRESS:
183 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
185 case ISCSI_HOST_PARAM_IPADDRESS:
186 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
187 ha->ip_address[1], ha->ip_address[2],
190 case ISCSI_HOST_PARAM_INITIATOR_NAME:
191 len = sprintf(buf, "%s\n", ha->name_string);
200 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
201 enum iscsi_param param, char *buf)
203 struct ddb_entry *ddb_entry = sess->dd_data;
207 case ISCSI_PARAM_TARGET_NAME:
208 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
209 ddb_entry->iscsi_name);
211 case ISCSI_PARAM_TPGT:
212 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
214 case ISCSI_PARAM_TARGET_ALIAS:
215 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
216 ddb_entry->iscsi_alias);
225 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
226 enum iscsi_param param, char *buf)
228 struct iscsi_cls_session *session;
229 struct ddb_entry *ddb_entry;
232 session = iscsi_dev_to_session(conn->dev.parent);
233 ddb_entry = session->dd_data;
236 case ISCSI_PARAM_CONN_PORT:
237 len = sprintf(buf, "%hu\n", ddb_entry->port);
239 case ISCSI_PARAM_CONN_ADDRESS:
240 /* TODO: what are the ipv6 bits */
241 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
250 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
251 enum iscsi_tgt_dscvr type, uint32_t enable,
252 struct sockaddr *dst_addr)
254 struct scsi_qla_host *ha;
255 struct sockaddr_in *addr;
256 struct sockaddr_in6 *addr6;
259 ha = (struct scsi_qla_host *) shost->hostdata;
262 case ISCSI_TGT_DSCVR_SEND_TARGETS:
263 if (dst_addr->sa_family == AF_INET) {
264 addr = (struct sockaddr_in *)dst_addr;
265 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
266 addr->sin_port) != QLA_SUCCESS)
268 } else if (dst_addr->sa_family == AF_INET6) {
270 * TODO: fix qla4xxx_send_tgts
272 addr6 = (struct sockaddr_in6 *)dst_addr;
273 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
274 addr6->sin6_port) != QLA_SUCCESS)
285 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
287 if (!ddb_entry->sess)
290 if (ddb_entry->conn) {
291 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
292 iscsi_remove_session(ddb_entry->sess);
294 iscsi_free_session(ddb_entry->sess);
297 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
301 ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
303 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
305 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
309 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
310 if (!ddb_entry->conn) {
311 iscsi_remove_session(ddb_entry->sess);
312 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
316 /* finally ready to go */
317 iscsi_unblock_session(ddb_entry->sess);
321 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
323 struct ddb_entry *ddb_entry;
324 struct iscsi_cls_session *sess;
326 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
327 sizeof(struct ddb_entry));
331 ddb_entry = sess->dd_data;
332 memset(ddb_entry, 0, sizeof(*ddb_entry));
334 ddb_entry->sess = sess;
338 static void qla4xxx_scan_start(struct Scsi_Host *shost)
340 struct scsi_qla_host *ha = shost_priv(shost);
341 struct ddb_entry *ddb_entry, *ddbtemp;
343 /* finish setup of sessions that were already setup in firmware */
344 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
345 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
346 qla4xxx_add_sess(ddb_entry);
354 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
355 unsigned long interval)
357 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
358 __func__, ha->host->host_no));
359 init_timer(&ha->timer);
360 ha->timer.expires = jiffies + interval * HZ;
361 ha->timer.data = (unsigned long)ha;
362 ha->timer.function = (void (*)(unsigned long))func;
363 add_timer(&ha->timer);
364 ha->timer_active = 1;
367 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
369 del_timer_sync(&ha->timer);
370 ha->timer_active = 0;
374 * qla4xxx_mark_device_missing - mark a device as missing.
375 * @ha: Pointer to host adapter structure.
376 * @ddb_entry: Pointer to device database entry
378 * This routine marks a device missing and close connection.
380 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
381 struct ddb_entry *ddb_entry)
383 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
384 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
385 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
386 ha->host_no, ddb_entry->fw_ddb_index));
388 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
389 ddb_entry->fw_ddb_index))
391 iscsi_block_session(ddb_entry->sess);
392 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
396 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
397 * @ha: Pointer to host adapter structure.
399 * This routine marks a device missing and resets the relogin retry count.
401 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
403 struct ddb_entry *ddb_entry, *ddbtemp;
404 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
405 qla4xxx_mark_device_missing(ha, ddb_entry);
409 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
410 struct ddb_entry *ddb_entry,
411 struct scsi_cmnd *cmd,
412 void (*done)(struct scsi_cmnd *))
416 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
420 kref_init(&srb->srb_ref);
422 srb->ddb = ddb_entry;
425 CMD_SP(cmd) = (void *)srb;
426 cmd->scsi_done = done;
431 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
433 struct scsi_cmnd *cmd = srb->cmd;
435 if (srb->flags & SRB_DMA_VALID) {
437 srb->flags &= ~SRB_DMA_VALID;
442 void qla4xxx_srb_compl(struct kref *ref)
444 struct srb *srb = container_of(ref, struct srb, srb_ref);
445 struct scsi_cmnd *cmd = srb->cmd;
446 struct scsi_qla_host *ha = srb->ha;
448 qla4xxx_srb_free_dma(ha, srb);
450 mempool_free(srb, ha->srb_mempool);
456 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
457 * @cmd: Pointer to Linux's SCSI command structure
458 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
459 * that the command has been processed.
462 * This routine is invoked by Linux to send a SCSI command to the driver.
463 * The mid-level driver tries to ensure that queuecommand never gets
464 * invoked concurrently with itself or the interrupt handler (although
465 * the interrupt handler may call this routine as part of request-
466 * completion handling). Unfortunely, it sometimes calls the scheduler
467 * in interrupt context which is a big NO! NO!.
469 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
470 void (*done)(struct scsi_cmnd *))
472 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
473 struct ddb_entry *ddb_entry = cmd->device->hostdata;
474 struct iscsi_cls_session *sess = ddb_entry->sess;
478 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
479 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
480 cmd->result = DID_NO_CONNECT << 16;
482 cmd->result = DID_REQUEUE << 16;
483 goto qc_fail_command;
487 cmd->result = DID_IMM_RETRY << 16;
488 goto qc_fail_command;
491 rval = iscsi_session_chkready(sess);
494 goto qc_fail_command;
497 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
498 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
499 cmd->result = DID_NO_CONNECT << 16;
500 goto qc_fail_command;
502 return SCSI_MLQUEUE_TARGET_BUSY;
505 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
506 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
507 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
508 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
509 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
510 !test_bit(AF_ONLINE, &ha->flags) ||
511 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
514 spin_unlock_irq(ha->host->host_lock);
516 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
518 goto qc_host_busy_lock;
520 rval = qla4xxx_send_command_to_isp(ha, srb);
521 if (rval != QLA_SUCCESS)
522 goto qc_host_busy_free_sp;
524 spin_lock_irq(ha->host->host_lock);
527 qc_host_busy_free_sp:
528 qla4xxx_srb_free_dma(ha, srb);
529 mempool_free(srb, ha->srb_mempool);
532 spin_lock_irq(ha->host->host_lock);
535 return SCSI_MLQUEUE_HOST_BUSY;
544 * qla4xxx_mem_free - frees memory allocated to adapter
545 * @ha: Pointer to host adapter structure.
547 * Frees memory previously allocated by qla4xxx_mem_alloc
549 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
552 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
558 ha->request_ring = NULL;
560 ha->response_ring = NULL;
561 ha->response_dma = 0;
562 ha->shadow_regs = NULL;
563 ha->shadow_regs_dma = 0;
567 mempool_destroy(ha->srb_mempool);
569 ha->srb_mempool = NULL;
571 /* release io space registers */
572 if (is_qla8022(ha)) {
575 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
577 if (ha->nx_db_wr_ptr)
579 (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
582 pci_release_regions(ha->pdev);
586 * qla4xxx_mem_alloc - allocates memory for use by adapter.
587 * @ha: Pointer to host adapter structure
589 * Allocates DMA memory for request and response queues. Also allocates memory
592 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
596 /* Allocate contiguous block of DMA memory for queues. */
597 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
598 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
599 sizeof(struct shadow_regs) +
601 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
602 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
603 &ha->queues_dma, GFP_KERNEL);
604 if (ha->queues == NULL) {
605 ql4_printk(KERN_WARNING, ha,
606 "Memory Allocation failed - queues.\n");
608 goto mem_alloc_error_exit;
610 memset(ha->queues, 0, ha->queues_len);
613 * As per RISC alignment requirements -- the bus-address must be a
614 * multiple of the request-ring size (in bytes).
617 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
618 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
619 (MEM_ALIGN_VALUE - 1));
621 /* Update request and response queue pointers. */
622 ha->request_dma = ha->queues_dma + align;
623 ha->request_ring = (struct queue_entry *) (ha->queues + align);
624 ha->response_dma = ha->queues_dma + align +
625 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
626 ha->response_ring = (struct queue_entry *) (ha->queues + align +
627 (REQUEST_QUEUE_DEPTH *
629 ha->shadow_regs_dma = ha->queues_dma + align +
630 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
631 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
632 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
633 (REQUEST_QUEUE_DEPTH *
635 (RESPONSE_QUEUE_DEPTH *
638 /* Allocate memory for srb pool. */
639 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
640 mempool_free_slab, srb_cachep);
641 if (ha->srb_mempool == NULL) {
642 ql4_printk(KERN_WARNING, ha,
643 "Memory Allocation failed - SRB Pool.\n");
645 goto mem_alloc_error_exit;
650 mem_alloc_error_exit:
651 qla4xxx_mem_free(ha);
656 * qla4_8xxx_check_fw_alive - Check firmware health
657 * @ha: Pointer to host adapter structure.
661 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
663 uint32_t fw_heartbeat_counter, halt_status;
665 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
666 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
667 if (fw_heartbeat_counter == 0xffffffff) {
668 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
669 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
670 ha->host_no, __func__));
674 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
675 ha->seconds_since_last_heartbeat++;
676 /* FW not alive after 2 seconds */
677 if (ha->seconds_since_last_heartbeat == 2) {
678 ha->seconds_since_last_heartbeat = 0;
679 halt_status = qla4_8xxx_rd_32(ha,
680 QLA82XX_PEG_HALT_STATUS1);
682 /* Since we cannot change dev_state in interrupt
683 * context, set appropriate DPC flag then wakeup
685 if (halt_status & HALT_STATUS_UNRECOVERABLE)
686 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
688 printk("scsi%ld: %s: detect abort needed!\n",
689 ha->host_no, __func__);
690 set_bit(DPC_RESET_HA, &ha->dpc_flags);
692 qla4xxx_wake_dpc(ha);
693 qla4xxx_mailbox_premature_completion(ha);
696 ha->fw_heartbeat_counter = fw_heartbeat_counter;
700 * qla4_8xxx_watchdog - Poll dev state
701 * @ha: Pointer to host adapter structure.
705 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
709 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
711 /* don't poll if reset is going on */
712 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
713 if (dev_state == QLA82XX_DEV_NEED_RESET &&
714 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
715 printk("scsi%ld: %s: HW State: NEED RESET!\n",
716 ha->host_no, __func__);
717 set_bit(DPC_RESET_HA, &ha->dpc_flags);
718 qla4xxx_wake_dpc(ha);
719 qla4xxx_mailbox_premature_completion(ha);
720 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
721 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
722 printk("scsi%ld: %s: HW State: NEED QUIES!\n",
723 ha->host_no, __func__);
724 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
725 qla4xxx_wake_dpc(ha);
727 /* Check firmware health */
728 qla4_8xxx_check_fw_alive(ha);
734 * qla4xxx_timer - checks every second for work to do.
735 * @ha: Pointer to host adapter structure.
737 static void qla4xxx_timer(struct scsi_qla_host *ha)
739 struct ddb_entry *ddb_entry, *dtemp;
743 /* If we are in the middle of AER/EEH processing
744 * skip any processing and reschedule the timer
746 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
747 mod_timer(&ha->timer, jiffies + HZ);
751 /* Hardware read to trigger an EEH error during mailbox waits. */
752 if (!pci_channel_offline(ha->pdev))
753 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
755 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
756 DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
761 if (is_qla8022(ha)) {
762 qla4_8xxx_watchdog(ha);
765 /* Search for relogin's to time-out and port down retry. */
766 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
767 /* Count down time between sending relogins */
768 if (adapter_up(ha) &&
769 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
770 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
771 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
773 if (atomic_read(&ddb_entry->retry_relogin_timer)
775 atomic_set(&ddb_entry->
778 set_bit(DPC_RELOGIN_DEVICE,
780 set_bit(DF_RELOGIN, &ddb_entry->flags);
781 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
783 ha->host_no, __func__,
784 ddb_entry->fw_ddb_index));
786 atomic_dec(&ddb_entry->
787 retry_relogin_timer);
791 /* Wait for relogin to timeout */
792 if (atomic_read(&ddb_entry->relogin_timer) &&
793 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
795 * If the relogin times out and the device is
796 * still NOT ONLINE then try and relogin again.
798 if (atomic_read(&ddb_entry->state) !=
800 ddb_entry->fw_ddb_device_state ==
801 DDB_DS_SESSION_FAILED) {
802 /* Reset retry relogin timer */
803 atomic_inc(&ddb_entry->relogin_retry_count);
804 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
805 " timed out-retrying"
808 ddb_entry->fw_ddb_index,
809 atomic_read(&ddb_entry->
810 relogin_retry_count))
813 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
814 "initate relogin after"
816 ha->host_no, ddb_entry->bus,
818 ddb_entry->fw_ddb_index,
819 ddb_entry->default_time2wait + 4)
822 atomic_set(&ddb_entry->retry_relogin_timer,
823 ddb_entry->default_time2wait + 4);
828 if (!is_qla8022(ha)) {
829 /* Check for heartbeat interval. */
830 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
831 ha->heartbeat_interval != 0) {
832 ha->seconds_since_last_heartbeat++;
833 if (ha->seconds_since_last_heartbeat >
834 ha->heartbeat_interval + 2)
835 set_bit(DPC_RESET_HA, &ha->dpc_flags);
839 /* Wakeup the dpc routine for this adapter, if needed. */
841 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
842 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
843 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
844 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
845 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
846 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
847 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
848 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
849 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
850 test_bit(DPC_AEN, &ha->dpc_flags)) &&
851 !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
853 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
854 " - dpc flags = 0x%lx\n",
855 ha->host_no, __func__, ha->dpc_flags));
856 qla4xxx_wake_dpc(ha);
859 /* Reschedule timer thread to call us back in one second */
860 mod_timer(&ha->timer, jiffies + HZ);
862 DEBUG2(ha->seconds_since_last_intr++);
866 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
867 * @ha: Pointer to host adapter structure.
869 * This routine stalls the driver until all outstanding commands are returned.
870 * Caller must release the Hardware Lock prior to calling this routine.
872 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
876 struct scsi_cmnd *cmd;
878 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
880 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
881 "complete\n", WAIT_CMD_TOV));
883 while (!time_after_eq(jiffies, wtime)) {
884 spin_lock_irqsave(&ha->hardware_lock, flags);
885 /* Find a command that hasn't completed. */
886 for (index = 0; index < ha->host->can_queue; index++) {
887 cmd = scsi_host_find_tag(ha->host, index);
891 spin_unlock_irqrestore(&ha->hardware_lock, flags);
893 /* If No Commands are pending, wait is complete */
894 if (index == ha->host->can_queue)
899 /* If we timed out on waiting for commands to come back
904 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
906 uint32_t ctrl_status;
907 unsigned long flags = 0;
909 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
911 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
914 spin_lock_irqsave(&ha->hardware_lock, flags);
917 * If the SCSI Reset Interrupt bit is set, clear it.
918 * Otherwise, the Soft Reset won't work.
920 ctrl_status = readw(&ha->reg->ctrl_status);
921 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
922 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
924 /* Issue Soft Reset */
925 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
926 readl(&ha->reg->ctrl_status);
928 spin_unlock_irqrestore(&ha->hardware_lock, flags);
933 * qla4xxx_soft_reset - performs soft reset.
934 * @ha: Pointer to host adapter structure.
936 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
938 uint32_t max_wait_time;
939 unsigned long flags = 0;
940 int status = QLA_ERROR;
941 uint32_t ctrl_status;
943 qla4xxx_hw_reset(ha);
945 /* Wait until the Network Reset Intr bit is cleared */
946 max_wait_time = RESET_INTR_TOV;
948 spin_lock_irqsave(&ha->hardware_lock, flags);
949 ctrl_status = readw(&ha->reg->ctrl_status);
950 spin_unlock_irqrestore(&ha->hardware_lock, flags);
952 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
956 } while ((--max_wait_time));
958 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
959 DEBUG2(printk(KERN_WARNING
960 "scsi%ld: Network Reset Intr not cleared by "
961 "Network function, clearing it now!\n",
963 spin_lock_irqsave(&ha->hardware_lock, flags);
964 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
965 readl(&ha->reg->ctrl_status);
966 spin_unlock_irqrestore(&ha->hardware_lock, flags);
969 /* Wait until the firmware tells us the Soft Reset is done */
970 max_wait_time = SOFT_RESET_TOV;
972 spin_lock_irqsave(&ha->hardware_lock, flags);
973 ctrl_status = readw(&ha->reg->ctrl_status);
974 spin_unlock_irqrestore(&ha->hardware_lock, flags);
976 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
977 status = QLA_SUCCESS;
982 } while ((--max_wait_time));
985 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
986 * after the soft reset has taken place.
988 spin_lock_irqsave(&ha->hardware_lock, flags);
989 ctrl_status = readw(&ha->reg->ctrl_status);
990 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
991 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
992 readl(&ha->reg->ctrl_status);
994 spin_unlock_irqrestore(&ha->hardware_lock, flags);
996 /* If soft reset fails then most probably the bios on other
997 * function is also enabled.
998 * Since the initialization is sequential the other fn
999 * wont be able to acknowledge the soft reset.
1000 * Issue a force soft reset to workaround this scenario.
1002 if (max_wait_time == 0) {
1003 /* Issue Force Soft Reset */
1004 spin_lock_irqsave(&ha->hardware_lock, flags);
1005 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1006 readl(&ha->reg->ctrl_status);
1007 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1008 /* Wait until the firmware tells us the Soft Reset is done */
1009 max_wait_time = SOFT_RESET_TOV;
1011 spin_lock_irqsave(&ha->hardware_lock, flags);
1012 ctrl_status = readw(&ha->reg->ctrl_status);
1013 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1015 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1016 status = QLA_SUCCESS;
1021 } while ((--max_wait_time));
1028 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
1029 * @ha: Pointer to host adapter structure.
1030 * @res: returned scsi status
1032 * This routine is called just prior to a HARD RESET to return all
1033 * outstanding commands back to the Operating System.
1034 * Caller should make sure that the following locks are released
1035 * before this calling routine: Hardware lock, and io_request_lock.
1037 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
1041 unsigned long flags;
1043 spin_lock_irqsave(&ha->hardware_lock, flags);
1044 for (i = 0; i < ha->host->can_queue; i++) {
1045 srb = qla4xxx_del_from_active_array(ha, i);
1047 srb->cmd->result = res;
1048 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1051 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1054 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1056 clear_bit(AF_ONLINE, &ha->flags);
1058 /* Disable the board */
1059 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1060 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1062 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1063 qla4xxx_mark_all_devices_missing(ha);
1064 clear_bit(AF_INIT_DONE, &ha->flags);
1068 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1069 * @ha: Pointer to host adapter structure.
1071 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
1073 int status = QLA_ERROR;
1074 uint8_t reset_chip = 0;
1076 /* Stall incoming I/O until we are done */
1077 scsi_block_requests(ha->host);
1078 clear_bit(AF_ONLINE, &ha->flags);
1080 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
1082 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1084 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1087 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1088 * do not reset adapter, jump to initialize_adapter */
1089 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1090 status = QLA_SUCCESS;
1091 goto recover_ha_init_adapter;
1094 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1095 * from eh_host_reset or ioctl module */
1096 if (is_qla8022(ha) && !reset_chip &&
1097 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1099 DEBUG2(ql4_printk(KERN_INFO, ha,
1100 "scsi%ld: %s - Performing stop_firmware...\n",
1101 ha->host_no, __func__));
1102 status = ha->isp_ops->reset_firmware(ha);
1103 if (status == QLA_SUCCESS) {
1104 qla4xxx_cmd_wait(ha);
1105 ha->isp_ops->disable_intrs(ha);
1106 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1107 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1109 /* If the stop_firmware fails then
1110 * reset the entire chip */
1112 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1113 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1117 /* Issue full chip reset if recovering from a catastrophic error,
1118 * or if stop_firmware fails for ISP-82xx.
1119 * This is the default case for ISP-4xxx */
1120 if (!is_qla8022(ha) || reset_chip) {
1121 qla4xxx_cmd_wait(ha);
1122 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1123 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1124 DEBUG2(ql4_printk(KERN_INFO, ha,
1125 "scsi%ld: %s - Performing chip reset..\n",
1126 ha->host_no, __func__));
1127 status = ha->isp_ops->reset_chip(ha);
1130 /* Flush any pending ddb changed AENs */
1131 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1133 recover_ha_init_adapter:
1134 /* Upon successful firmware/chip reset, re-initialize the adapter */
1135 if (status == QLA_SUCCESS) {
1136 /* For ISP-4xxx, force function 1 to always initialize
1137 * before function 3 to prevent both funcions from
1138 * stepping on top of the other */
1139 if (!is_qla8022(ha) && (ha->mac_index == 3))
1142 /* NOTE: AF_ONLINE flag set upon successful completion of
1143 * qla4xxx_initialize_adapter */
1144 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1147 /* Retry failed adapter initialization, if necessary
1148 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1149 * case to prevent ping-pong resets between functions */
1150 if (!test_bit(AF_ONLINE, &ha->flags) &&
1151 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1152 /* Adapter initialization failed, see if we can retry
1154 * Since we don't want to block the DPC for too long
1155 * with multiple resets in the same thread,
1156 * utilize DPC to retry */
1157 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1158 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1159 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1160 "(%d) more times\n", ha->host_no,
1161 ha->retry_reset_ha_cnt));
1162 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1165 if (ha->retry_reset_ha_cnt > 0) {
1166 /* Schedule another Reset HA--DPC will retry */
1167 ha->retry_reset_ha_cnt--;
1168 DEBUG2(printk("scsi%ld: recover adapter - "
1169 "retry remaining %d\n",
1171 ha->retry_reset_ha_cnt));
1175 if (ha->retry_reset_ha_cnt == 0) {
1176 /* Recover adapter retries have been exhausted.
1178 DEBUG2(printk("scsi%ld: recover adapter "
1179 "failed - board disabled\n",
1181 qla4xxx_dead_adapter_cleanup(ha);
1182 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1183 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1184 clear_bit(DPC_RESET_HA_FW_CONTEXT,
1190 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1191 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1192 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1195 ha->adapter_error_count++;
1197 if (test_bit(AF_ONLINE, &ha->flags))
1198 ha->isp_ops->enable_intrs(ha);
1200 scsi_unblock_requests(ha->host);
1202 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1203 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1204 status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1209 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1211 if (ha->dpc_thread &&
1212 !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1213 set_bit(AF_DPC_SCHEDULED, &ha->flags);
1214 queue_work(ha->dpc_thread, &ha->dpc_work);
1219 * qla4xxx_do_dpc - dpc routine
1220 * @data: in our case pointer to adapter structure
1222 * This routine is a task that is schedule by the interrupt handler
1223 * to perform the background processing for interrupts. We put it
1224 * on a task queue that is consumed whenever the scheduler runs; that's
1225 * so you can do anything (i.e. put the process to sleep etc). In fact,
1226 * the mid-level tries to sleep when it reaches the driver threshold
1227 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1229 static void qla4xxx_do_dpc(struct work_struct *work)
1231 struct scsi_qla_host *ha =
1232 container_of(work, struct scsi_qla_host, dpc_work);
1233 struct ddb_entry *ddb_entry, *dtemp;
1234 int status = QLA_ERROR;
1236 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1237 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1238 ha->host_no, __func__, ha->flags, ha->dpc_flags))
1240 /* Initialization not yet finished. Don't do anything yet. */
1241 if (!test_bit(AF_INIT_DONE, &ha->flags))
1244 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1245 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1246 ha->host_no, __func__, ha->flags));
1250 /* HBA is in the process of being permanently disabled.
1251 * Don't process anything */
1252 if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1255 if (is_qla8022(ha)) {
1256 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1257 qla4_8xxx_idc_lock(ha);
1258 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1259 QLA82XX_DEV_FAILED);
1260 qla4_8xxx_idc_unlock(ha);
1261 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1262 qla4_8xxx_device_state_handler(ha);
1264 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1265 qla4_8xxx_need_qsnt_handler(ha);
1269 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1270 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1271 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1272 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1273 if (ql4xdontresethba) {
1274 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1275 ha->host_no, __func__));
1276 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1277 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1278 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1279 goto dpc_post_reset_ha;
1281 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1282 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1283 qla4xxx_recover_adapter(ha);
1285 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1286 uint8_t wait_time = RESET_INTR_TOV;
1288 while ((readw(&ha->reg->ctrl_status) &
1289 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1290 if (--wait_time == 0)
1295 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1296 "bit not cleared-- resetting\n",
1297 ha->host_no, __func__));
1298 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1299 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1300 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1301 status = qla4xxx_recover_adapter(ha);
1303 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1304 if (status == QLA_SUCCESS)
1305 ha->isp_ops->enable_intrs(ha);
1310 /* ---- process AEN? --- */
1311 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1312 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1314 /* ---- Get DHCP IP Address? --- */
1315 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1316 qla4xxx_get_dhcp_ip_address(ha);
1318 /* ---- link change? --- */
1319 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1320 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1321 /* ---- link down? --- */
1322 list_for_each_entry_safe(ddb_entry, dtemp,
1323 &ha->ddb_list, list) {
1324 if (atomic_read(&ddb_entry->state) ==
1326 qla4xxx_mark_device_missing(ha,
1330 /* ---- link up? --- *
1331 * F/W will auto login to all devices ONLY ONCE after
1332 * link up during driver initialization and runtime
1333 * fatal error recovery. Therefore, the driver must
1334 * manually relogin to devices when recovering from
1335 * connection failures, logouts, expired KATO, etc. */
1337 list_for_each_entry_safe(ddb_entry, dtemp,
1338 &ha->ddb_list, list) {
1339 if ((atomic_read(&ddb_entry->state) ==
1340 DDB_STATE_MISSING) ||
1341 (atomic_read(&ddb_entry->state) ==
1343 if (ddb_entry->fw_ddb_device_state ==
1344 DDB_DS_SESSION_ACTIVE) {
1345 atomic_set(&ddb_entry->state,
1347 ql4_printk(KERN_INFO, ha,
1348 "scsi%ld: %s: ddb[%d]"
1350 ha->host_no, __func__,
1351 ddb_entry->fw_ddb_index);
1353 iscsi_unblock_session(
1356 qla4xxx_relogin_device(
1364 /* ---- relogin device? --- */
1365 if (adapter_up(ha) &&
1366 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1367 list_for_each_entry_safe(ddb_entry, dtemp,
1368 &ha->ddb_list, list) {
1369 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1370 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1371 qla4xxx_relogin_device(ha, ddb_entry);
1374 * If mbx cmd times out there is no point
1375 * in continuing further.
1376 * With large no of targets this can hang
1379 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1380 printk(KERN_WARNING "scsi%ld: %s: "
1381 "need to reset hba\n",
1382 ha->host_no, __func__);
1389 clear_bit(AF_DPC_SCHEDULED, &ha->flags);
1393 * qla4xxx_free_adapter - release the adapter
1394 * @ha: pointer to adapter structure
1396 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1399 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1400 /* Turn-off interrupts on the card. */
1401 ha->isp_ops->disable_intrs(ha);
1404 /* Remove timer thread, if present */
1405 if (ha->timer_active)
1406 qla4xxx_stop_timer(ha);
1408 /* Kill the kernel thread for this host */
1410 destroy_workqueue(ha->dpc_thread);
1412 /* Put firmware in known state */
1413 ha->isp_ops->reset_firmware(ha);
1415 if (is_qla8022(ha)) {
1416 qla4_8xxx_idc_lock(ha);
1417 qla4_8xxx_clear_drv_active(ha);
1418 qla4_8xxx_idc_unlock(ha);
1421 /* Detach interrupts */
1422 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1423 qla4xxx_free_irqs(ha);
1425 /* free extra memory */
1426 qla4xxx_mem_free(ha);
1429 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1432 uint8_t revision_id;
1433 unsigned long mem_base, mem_len, db_base, db_len;
1434 struct pci_dev *pdev = ha->pdev;
1436 status = pci_request_regions(pdev, DRIVER_NAME);
1439 "scsi(%ld) Failed to reserve PIO regions (%s) "
1440 "status=%d\n", ha->host_no, pci_name(pdev), status);
1441 goto iospace_error_exit;
1444 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1445 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1446 __func__, revision_id));
1447 ha->revision_id = revision_id;
1449 /* remap phys address */
1450 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1451 mem_len = pci_resource_len(pdev, 0);
1452 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1453 __func__, mem_base, mem_len));
1455 /* mapping of pcibase pointer */
1456 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1457 if (!ha->nx_pcibase) {
1459 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1460 pci_release_regions(ha->pdev);
1461 goto iospace_error_exit;
1464 /* Mapping of IO base pointer, door bell read and write pointer */
1466 /* mapping of IO base pointer */
1468 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1469 0xbc000 + (ha->pdev->devfn << 11));
1471 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1472 db_len = pci_resource_len(pdev, 4);
1474 /* mapping of doorbell write pointer */
1475 ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1476 (ha->pdev->devfn << 12), 4);
1477 if (!ha->nx_db_wr_ptr) {
1479 "cannot remap MMIO doorbell-write (%s), aborting\n",
1481 goto iospace_error_exit;
1483 /* mapping of doorbell read pointer */
1484 ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1485 (ha->pdev->devfn * 8);
1486 if (!ha->nx_db_rd_ptr)
1488 "cannot remap MMIO doorbell-read (%s), aborting\n",
1497 * qla4xxx_iospace_config - maps registers
1498 * @ha: pointer to adapter structure
1500 * This routines maps HBA's registers from the pci address space
1501 * into the kernel virtual address space for memory mapped i/o.
1503 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1505 unsigned long pio, pio_len, pio_flags;
1506 unsigned long mmio, mmio_len, mmio_flags;
1508 pio = pci_resource_start(ha->pdev, 0);
1509 pio_len = pci_resource_len(ha->pdev, 0);
1510 pio_flags = pci_resource_flags(ha->pdev, 0);
1511 if (pio_flags & IORESOURCE_IO) {
1512 if (pio_len < MIN_IOBASE_LEN) {
1513 ql4_printk(KERN_WARNING, ha,
1514 "Invalid PCI I/O region size\n");
1518 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
1522 /* Use MMIO operations for all accesses. */
1523 mmio = pci_resource_start(ha->pdev, 1);
1524 mmio_len = pci_resource_len(ha->pdev, 1);
1525 mmio_flags = pci_resource_flags(ha->pdev, 1);
1527 if (!(mmio_flags & IORESOURCE_MEM)) {
1528 ql4_printk(KERN_ERR, ha,
1529 "region #0 not an MMIO resource, aborting\n");
1531 goto iospace_error_exit;
1534 if (mmio_len < MIN_IOBASE_LEN) {
1535 ql4_printk(KERN_ERR, ha,
1536 "Invalid PCI mem region size, aborting\n");
1537 goto iospace_error_exit;
1540 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1541 ql4_printk(KERN_WARNING, ha,
1542 "Failed to reserve PIO/MMIO regions\n");
1544 goto iospace_error_exit;
1547 ha->pio_address = pio;
1548 ha->pio_length = pio_len;
1549 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1551 ql4_printk(KERN_ERR, ha,
1552 "cannot remap MMIO, aborting\n");
1554 goto iospace_error_exit;
1563 static struct isp_operations qla4xxx_isp_ops = {
1564 .iospace_config = qla4xxx_iospace_config,
1565 .pci_config = qla4xxx_pci_config,
1566 .disable_intrs = qla4xxx_disable_intrs,
1567 .enable_intrs = qla4xxx_enable_intrs,
1568 .start_firmware = qla4xxx_start_firmware,
1569 .intr_handler = qla4xxx_intr_handler,
1570 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1571 .reset_chip = qla4xxx_soft_reset,
1572 .reset_firmware = qla4xxx_hw_reset,
1573 .queue_iocb = qla4xxx_queue_iocb,
1574 .complete_iocb = qla4xxx_complete_iocb,
1575 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1576 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1577 .get_sys_info = qla4xxx_get_sys_info,
1580 static struct isp_operations qla4_8xxx_isp_ops = {
1581 .iospace_config = qla4_8xxx_iospace_config,
1582 .pci_config = qla4_8xxx_pci_config,
1583 .disable_intrs = qla4_8xxx_disable_intrs,
1584 .enable_intrs = qla4_8xxx_enable_intrs,
1585 .start_firmware = qla4_8xxx_load_risc,
1586 .intr_handler = qla4_8xxx_intr_handler,
1587 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1588 .reset_chip = qla4_8xxx_isp_reset,
1589 .reset_firmware = qla4_8xxx_stop_firmware,
1590 .queue_iocb = qla4_8xxx_queue_iocb,
1591 .complete_iocb = qla4_8xxx_complete_iocb,
1592 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1593 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1594 .get_sys_info = qla4_8xxx_get_sys_info,
1597 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1599 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1602 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1604 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1607 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1609 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1612 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1614 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1618 * qla4xxx_probe_adapter - callback function to probe HBA
1619 * @pdev: pointer to pci_dev structure
1620 * @pci_device_id: pointer to pci_device entry
1622 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1623 * It returns zero if successful. It also initializes all data necessary for
1626 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1627 const struct pci_device_id *ent)
1629 int ret = -ENODEV, status;
1630 struct Scsi_Host *host;
1631 struct scsi_qla_host *ha;
1632 uint8_t init_retry_count = 0;
1634 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
1636 if (pci_enable_device(pdev))
1639 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1642 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1643 goto probe_disable_device;
1646 /* Clear our data area */
1647 ha = (struct scsi_qla_host *) host->hostdata;
1648 memset(ha, 0, sizeof(*ha));
1650 /* Save the information from PCI BIOS. */
1653 ha->host_no = host->host_no;
1655 pci_enable_pcie_error_reporting(pdev);
1657 /* Setup Runtime configurable options */
1658 if (is_qla8022(ha)) {
1659 ha->isp_ops = &qla4_8xxx_isp_ops;
1660 rwlock_init(&ha->hw_lock);
1661 ha->qdr_sn_window = -1;
1662 ha->ddr_mn_window = -1;
1663 ha->curr_window = 255;
1664 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1665 nx_legacy_intr = &legacy_intr[ha->func_num];
1666 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1667 ha->nx_legacy_intr.tgt_status_reg =
1668 nx_legacy_intr->tgt_status_reg;
1669 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1670 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1672 ha->isp_ops = &qla4xxx_isp_ops;
1675 /* Set EEH reset type to fundamental if required by hba */
1677 pdev->needs_freset = 1;
1679 /* Configure PCI I/O space. */
1680 ret = ha->isp_ops->iospace_config(ha);
1682 goto probe_failed_ioconfig;
1684 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1685 pdev->device, pdev->irq, ha->reg);
1687 qla4xxx_config_dma_addressing(ha);
1689 /* Initialize lists and spinlocks. */
1690 INIT_LIST_HEAD(&ha->ddb_list);
1691 INIT_LIST_HEAD(&ha->free_srb_q);
1693 mutex_init(&ha->mbox_sem);
1694 init_completion(&ha->mbx_intr_comp);
1696 spin_lock_init(&ha->hardware_lock);
1698 /* Allocate dma buffers */
1699 if (qla4xxx_mem_alloc(ha)) {
1700 ql4_printk(KERN_WARNING, ha,
1701 "[ERROR] Failed to allocate memory for adapter\n");
1708 (void) qla4_8xxx_get_flash_info(ha);
1711 * Initialize the Host adapter request/response queues and
1713 * NOTE: interrupts enabled upon successful completion
1715 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1716 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1717 init_retry_count++ < MAX_INIT_RETRIES) {
1718 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1719 "(%d)\n", __func__, init_retry_count));
1721 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1724 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1727 if (!test_bit(AF_ONLINE, &ha->flags)) {
1728 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
1734 host->cmd_per_lun = 3;
1735 host->max_channel = 0;
1736 host->max_lun = MAX_LUNS - 1;
1737 host->max_id = MAX_TARGETS;
1738 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1739 host->can_queue = MAX_SRBS ;
1740 host->transportt = qla4xxx_scsi_transport;
1742 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1744 ql4_printk(KERN_WARNING, ha,
1745 "scsi_init_shared_tag_map failed\n");
1749 /* Startup the kernel thread for this host adapter. */
1750 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1751 "qla4xxx_dpc\n", __func__));
1752 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1753 ha->dpc_thread = create_singlethread_workqueue(buf);
1754 if (!ha->dpc_thread) {
1755 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
1759 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1761 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1762 * (which is called indirectly by qla4xxx_initialize_adapter),
1763 * so that irqs will be registered after crbinit but before
1766 if (!is_qla8022(ha)) {
1767 ret = qla4xxx_request_irqs(ha);
1769 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1770 "interrupt %d already in use.\n", pdev->irq);
1775 pci_save_state(ha->pdev);
1776 ha->isp_ops->enable_intrs(ha);
1778 /* Start timer thread. */
1779 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1781 set_bit(AF_INIT_DONE, &ha->flags);
1783 pci_set_drvdata(pdev, ha);
1785 ret = scsi_add_host(host, &pdev->dev);
1790 " QLogic iSCSI HBA Driver version: %s\n"
1791 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1792 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1793 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1794 ha->patch_number, ha->build_number);
1795 scsi_scan_host(host);
1799 qla4xxx_free_adapter(ha);
1801 probe_failed_ioconfig:
1802 pci_disable_pcie_error_reporting(pdev);
1803 scsi_host_put(ha->host);
1805 probe_disable_device:
1806 pci_disable_device(pdev);
1812 * qla4xxx_remove_adapter - calback function to remove adapter.
1813 * @pci_dev: PCI device pointer
1815 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1817 struct scsi_qla_host *ha;
1819 ha = pci_get_drvdata(pdev);
1821 set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1823 /* remove devs from iscsi_sessions to scsi_devices */
1824 qla4xxx_free_ddb_list(ha);
1826 scsi_remove_host(ha->host);
1828 qla4xxx_free_adapter(ha);
1830 scsi_host_put(ha->host);
1832 pci_disable_pcie_error_reporting(pdev);
1833 pci_disable_device(pdev);
1834 pci_set_drvdata(pdev, NULL);
1838 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1841 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1842 * supported addressing method.
1844 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1848 /* Update our PCI device dma_mask for full 64 bit mask */
1849 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1850 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1851 dev_dbg(&ha->pdev->dev,
1852 "Failed to set 64 bit PCI consistent mask; "
1854 retval = pci_set_consistent_dma_mask(ha->pdev,
1858 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1861 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1863 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1864 struct ddb_entry *ddb = sess->dd_data;
1866 sdev->hostdata = ddb;
1867 sdev->tagged_supported = 1;
1868 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
1872 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1874 sdev->tagged_supported = 1;
1878 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1880 scsi_deactivate_tcq(sdev, 1);
1884 * qla4xxx_del_from_active_array - returns an active srb
1885 * @ha: Pointer to host adapter structure.
1886 * @index: index into the active_array
1888 * This routine removes and returns the srb at the specified index
1890 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1893 struct srb *srb = NULL;
1894 struct scsi_cmnd *cmd = NULL;
1896 cmd = scsi_host_find_tag(ha->host, index);
1900 srb = (struct srb *)CMD_SP(cmd);
1904 /* update counters */
1905 if (srb->flags & SRB_DMA_VALID) {
1906 ha->req_q_count += srb->iocb_cnt;
1907 ha->iocb_cnt -= srb->iocb_cnt;
1909 srb->cmd->host_scribble =
1910 (unsigned char *)(unsigned long) MAX_SRBS;
1916 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1917 * @ha: Pointer to host adapter structure.
1918 * @cmd: Scsi Command to wait on.
1920 * This routine waits for the command to be returned by the Firmware
1921 * for some max time.
1923 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1924 struct scsi_cmnd *cmd)
1928 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1931 /* Dont wait on command if PCI error is being handled
1934 if (unlikely(pci_channel_offline(ha->pdev)) ||
1935 (test_bit(AF_EEH_BUSY, &ha->flags))) {
1936 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1937 ha->host_no, __func__);
1942 /* Checking to see if its returned to OS */
1943 rp = (struct srb *) CMD_SP(cmd);
1950 } while (max_wait_time--);
1956 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1957 * @ha: Pointer to host adapter structure
1959 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1961 unsigned long wait_online;
1963 wait_online = jiffies + (30 * HZ);
1964 while (time_before(jiffies, wait_online)) {
1968 else if (ha->retry_reset_ha_cnt == 0)
1978 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
1979 * @ha: pointer to HBA
1983 * This function waits for all outstanding commands to a lun to complete. It
1984 * returns 0 if all pending commands are returned and 1 otherwise.
1986 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1987 struct scsi_target *stgt,
1988 struct scsi_device *sdev)
1992 struct scsi_cmnd *cmd;
1995 * Waiting for all commands for the designated target or dev
1996 * in the active array
1998 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1999 cmd = scsi_host_find_tag(ha->host, cnt);
2000 if (cmd && stgt == scsi_target(cmd->device) &&
2001 (!sdev || sdev == cmd->device)) {
2002 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2012 * qla4xxx_eh_abort - callback for abort task.
2013 * @cmd: Pointer to Linux's SCSI command structure
2015 * This routine is called by the Linux OS to abort the specified
2018 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2020 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2021 unsigned int id = cmd->device->id;
2022 unsigned int lun = cmd->device->lun;
2023 unsigned long serial = cmd->serial_number;
2024 struct srb *srb = NULL;
2028 ql4_printk(KERN_INFO, ha,
2029 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
2030 ha->host_no, id, lun, cmd, serial);
2032 srb = (struct srb *) CMD_SP(cmd);
2037 kref_get(&srb->srb_ref);
2039 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2040 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2041 ha->host_no, id, lun));
2044 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2045 ha->host_no, id, lun));
2049 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2051 /* Wait for command to complete */
2053 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2054 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2055 ha->host_no, id, lun));
2060 ql4_printk(KERN_INFO, ha,
2061 "scsi%ld:%d:%d: Abort command - %s\n",
2062 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2068 * qla4xxx_eh_device_reset - callback for target reset.
2069 * @cmd: Pointer to Linux's SCSI command structure
2071 * This routine is called by the Linux OS to reset all luns on the
2074 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2076 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2077 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2078 int ret = FAILED, stat;
2083 ret = iscsi_block_scsi_eh(cmd);
2088 ql4_printk(KERN_INFO, ha,
2089 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2090 cmd->device->channel, cmd->device->id, cmd->device->lun);
2092 DEBUG2(printk(KERN_INFO
2093 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2094 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
2095 cmd, jiffies, cmd->request->timeout / HZ,
2096 ha->dpc_flags, cmd->result, cmd->allowed));
2098 /* FIXME: wait for hba to go online */
2099 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2100 if (stat != QLA_SUCCESS) {
2101 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
2102 goto eh_dev_reset_done;
2105 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2107 ql4_printk(KERN_INFO, ha,
2108 "DEVICE RESET FAILED - waiting for "
2110 goto eh_dev_reset_done;
2114 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2115 MM_LUN_RESET) != QLA_SUCCESS)
2116 goto eh_dev_reset_done;
2118 ql4_printk(KERN_INFO, ha,
2119 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2120 ha->host_no, cmd->device->channel, cmd->device->id,
2131 * qla4xxx_eh_target_reset - callback for target reset.
2132 * @cmd: Pointer to Linux's SCSI command structure
2134 * This routine is called by the Linux OS to reset the target.
2136 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2138 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2139 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2145 ret = iscsi_block_scsi_eh(cmd);
2149 starget_printk(KERN_INFO, scsi_target(cmd->device),
2150 "WARM TARGET RESET ISSUED.\n");
2152 DEBUG2(printk(KERN_INFO
2153 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2154 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
2155 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
2156 ha->dpc_flags, cmd->result, cmd->allowed));
2158 stat = qla4xxx_reset_target(ha, ddb_entry);
2159 if (stat != QLA_SUCCESS) {
2160 starget_printk(KERN_INFO, scsi_target(cmd->device),
2161 "WARM TARGET RESET FAILED.\n");
2165 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2167 starget_printk(KERN_INFO, scsi_target(cmd->device),
2168 "WARM TARGET DEVICE RESET FAILED - "
2169 "waiting for commands.\n");
2174 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2175 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2176 starget_printk(KERN_INFO, scsi_target(cmd->device),
2177 "WARM TARGET DEVICE RESET FAILED - "
2178 "marker iocb failed.\n");
2182 starget_printk(KERN_INFO, scsi_target(cmd->device),
2183 "WARM TARGET RESET SUCCEEDED.\n");
2188 * qla4xxx_eh_host_reset - kernel callback
2189 * @cmd: Pointer to Linux's SCSI command structure
2191 * This routine is invoked by the Linux kernel to perform fatal error
2192 * recovery on the specified adapter.
2194 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2196 int return_status = FAILED;
2197 struct scsi_qla_host *ha;
2199 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2201 if (ql4xdontresethba) {
2202 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2203 ha->host_no, __func__));
2207 ql4_printk(KERN_INFO, ha,
2208 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
2209 cmd->device->channel, cmd->device->id, cmd->device->lun);
2211 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2212 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2213 "DEAD.\n", ha->host_no, cmd->device->channel,
2219 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2221 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2223 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2226 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
2227 return_status = SUCCESS;
2229 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
2230 return_status == FAILED ? "FAILED" : "SUCCEDED");
2232 return return_status;
2235 /* PCI AER driver recovers from all correctable errors w/o
2236 * driver intervention. For uncorrectable errors PCI AER
2237 * driver calls the following device driver's callbacks
2239 * - Fatal Errors - link_reset
2240 * - Non-Fatal Errors - driver's pci_error_detected() which
2241 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2243 * PCI AER driver calls
2244 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2245 * returns RECOVERED or NEED_RESET if fw_hung
2246 * NEED_RESET - driver's slot_reset()
2247 * DISCONNECT - device is dead & cannot recover
2248 * RECOVERED - driver's pci_resume()
2250 static pci_ers_result_t
2251 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2253 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2255 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2256 ha->host_no, __func__, state);
2258 if (!is_aer_supported(ha))
2259 return PCI_ERS_RESULT_NONE;
2262 case pci_channel_io_normal:
2263 clear_bit(AF_EEH_BUSY, &ha->flags);
2264 return PCI_ERS_RESULT_CAN_RECOVER;
2265 case pci_channel_io_frozen:
2266 set_bit(AF_EEH_BUSY, &ha->flags);
2267 qla4xxx_mailbox_premature_completion(ha);
2268 qla4xxx_free_irqs(ha);
2269 pci_disable_device(pdev);
2270 return PCI_ERS_RESULT_NEED_RESET;
2271 case pci_channel_io_perm_failure:
2272 set_bit(AF_EEH_BUSY, &ha->flags);
2273 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2274 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2275 return PCI_ERS_RESULT_DISCONNECT;
2277 return PCI_ERS_RESULT_NEED_RESET;
2281 * qla4xxx_pci_mmio_enabled() gets called if
2282 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2283 * and read/write to the device still works.
2285 static pci_ers_result_t
2286 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2288 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2290 if (!is_aer_supported(ha))
2291 return PCI_ERS_RESULT_NONE;
2293 if (test_bit(AF_FW_RECOVERY, &ha->flags)) {
2294 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: firmware hang -- "
2295 "mmio_enabled\n", ha->host_no, __func__);
2296 return PCI_ERS_RESULT_NEED_RESET;
2298 return PCI_ERS_RESULT_RECOVERED;
2301 uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2303 uint32_t rval = QLA_ERROR;
2305 struct pci_dev *other_pdev = NULL;
2307 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2309 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2311 if (test_bit(AF_ONLINE, &ha->flags)) {
2312 clear_bit(AF_ONLINE, &ha->flags);
2313 qla4xxx_mark_all_devices_missing(ha);
2314 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2315 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2318 fn = PCI_FUNC(ha->pdev->devfn);
2321 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2322 "func %x\n", ha->host_no, __func__, fn);
2323 /* Get the pci device given the domain, bus,
2324 * slot/function number */
2326 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2327 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2333 if (atomic_read(&other_pdev->enable_cnt)) {
2334 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2335 "func in enabled state%x\n", ha->host_no,
2337 pci_dev_put(other_pdev);
2340 pci_dev_put(other_pdev);
2343 /* The first function on the card, the reset owner will
2344 * start & initialize the firmware. The other functions
2345 * on the card will reset the firmware context
2348 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2349 "0x%x is the owner\n", ha->host_no, __func__,
2352 qla4_8xxx_idc_lock(ha);
2353 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2356 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2357 QLA82XX_IDC_VERSION);
2359 qla4_8xxx_idc_unlock(ha);
2360 clear_bit(AF_FW_RECOVERY, &ha->flags);
2361 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2362 qla4_8xxx_idc_lock(ha);
2364 if (rval != QLA_SUCCESS) {
2365 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2366 "FAILED\n", ha->host_no, __func__);
2367 qla4_8xxx_clear_drv_active(ha);
2368 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2369 QLA82XX_DEV_FAILED);
2371 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2372 "READY\n", ha->host_no, __func__);
2373 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2375 /* Clear driver state register */
2376 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2377 qla4_8xxx_set_drv_active(ha);
2378 ha->isp_ops->enable_intrs(ha);
2380 qla4_8xxx_idc_unlock(ha);
2382 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2383 "the reset owner\n", ha->host_no, __func__,
2385 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2386 QLA82XX_DEV_READY)) {
2387 clear_bit(AF_FW_RECOVERY, &ha->flags);
2388 rval = qla4xxx_initialize_adapter(ha,
2390 if (rval == QLA_SUCCESS)
2391 ha->isp_ops->enable_intrs(ha);
2392 qla4_8xxx_idc_lock(ha);
2393 qla4_8xxx_set_drv_active(ha);
2394 qla4_8xxx_idc_unlock(ha);
2397 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2401 static pci_ers_result_t
2402 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2404 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2405 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2408 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2409 ha->host_no, __func__);
2411 if (!is_aer_supported(ha))
2412 return PCI_ERS_RESULT_NONE;
2414 /* Restore the saved state of PCIe device -
2415 * BAR registers, PCI Config space, PCIX, MSI,
2418 pci_restore_state(pdev);
2420 /* pci_restore_state() clears the saved_state flag of the device
2421 * save restored state which resets saved_state flag
2423 pci_save_state(pdev);
2425 /* Initialize device or resume if in suspended state */
2426 rc = pci_enable_device(pdev);
2428 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
2429 "device after reset\n", ha->host_no, __func__);
2430 goto exit_slot_reset;
2433 ret = qla4xxx_request_irqs(ha);
2435 ql4_printk(KERN_WARNING, ha, "Failed to reserve interrupt %d"
2436 " already in use.\n", pdev->irq);
2437 goto exit_slot_reset;
2440 if (is_qla8022(ha)) {
2441 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2442 ret = PCI_ERS_RESULT_RECOVERED;
2443 goto exit_slot_reset;
2445 goto exit_slot_reset;
2449 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2450 "device after reset\n", ha->host_no, __func__, ret);
2455 qla4xxx_pci_resume(struct pci_dev *pdev)
2457 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2460 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2461 ha->host_no, __func__);
2463 ret = qla4xxx_wait_for_hba_online(ha);
2464 if (ret != QLA_SUCCESS) {
2465 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2466 "resume I/O from slot/link_reset\n", ha->host_no,
2470 pci_cleanup_aer_uncorrect_error_status(pdev);
2471 clear_bit(AF_EEH_BUSY, &ha->flags);
2474 static struct pci_error_handlers qla4xxx_err_handler = {
2475 .error_detected = qla4xxx_pci_error_detected,
2476 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2477 .slot_reset = qla4xxx_pci_slot_reset,
2478 .resume = qla4xxx_pci_resume,
2481 static struct pci_device_id qla4xxx_pci_tbl[] = {
2483 .vendor = PCI_VENDOR_ID_QLOGIC,
2484 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2485 .subvendor = PCI_ANY_ID,
2486 .subdevice = PCI_ANY_ID,
2489 .vendor = PCI_VENDOR_ID_QLOGIC,
2490 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2491 .subvendor = PCI_ANY_ID,
2492 .subdevice = PCI_ANY_ID,
2495 .vendor = PCI_VENDOR_ID_QLOGIC,
2496 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2497 .subvendor = PCI_ANY_ID,
2498 .subdevice = PCI_ANY_ID,
2501 .vendor = PCI_VENDOR_ID_QLOGIC,
2502 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2503 .subvendor = PCI_ANY_ID,
2504 .subdevice = PCI_ANY_ID,
2508 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2510 static struct pci_driver qla4xxx_pci_driver = {
2511 .name = DRIVER_NAME,
2512 .id_table = qla4xxx_pci_tbl,
2513 .probe = qla4xxx_probe_adapter,
2514 .remove = qla4xxx_remove_adapter,
2515 .err_handler = &qla4xxx_err_handler,
2518 static int __init qla4xxx_module_init(void)
2522 /* Allocate cache for SRBs. */
2523 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
2524 SLAB_HWCACHE_ALIGN, NULL);
2525 if (srb_cachep == NULL) {
2527 "%s: Unable to allocate SRB cache..."
2528 "Failing load!\n", DRIVER_NAME);
2533 /* Derive version string. */
2534 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
2535 if (ql4xextended_error_logging)
2536 strcat(qla4xxx_version_str, "-debug");
2538 qla4xxx_scsi_transport =
2539 iscsi_register_transport(&qla4xxx_iscsi_transport);
2540 if (!qla4xxx_scsi_transport){
2542 goto release_srb_cache;
2545 ret = pci_register_driver(&qla4xxx_pci_driver);
2547 goto unregister_transport;
2549 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2552 unregister_transport:
2553 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2555 kmem_cache_destroy(srb_cachep);
2560 static void __exit qla4xxx_module_exit(void)
2562 pci_unregister_driver(&qla4xxx_pci_driver);
2563 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2564 kmem_cache_destroy(srb_cachep);
2567 module_init(qla4xxx_module_init);
2568 module_exit(qla4xxx_module_exit);
2570 MODULE_AUTHOR("QLogic Corporation");
2571 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2572 MODULE_LICENSE("GPL");
2573 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);