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>
9 #include <scsi/scsi_tcq.h>
10 #include <scsi/scsicam.h>
13 #include "ql4_version.h"
16 #include "ql4_inline.h"
21 static char qla4xxx_version_str[40];
24 * SRB allocation cache
26 static struct kmem_cache *srb_cachep;
29 * Module parameter information and variables
31 int ql4xdiscoverywait = 60;
32 module_param(ql4xdiscoverywait, int, S_IRUGO | S_IRUSR);
33 MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
34 int ql4xdontresethba = 0;
35 module_param(ql4xdontresethba, int, S_IRUGO | S_IRUSR);
36 MODULE_PARM_DESC(ql4xdontresethba,
37 "Dont reset the HBA when the driver gets 0x8002 AEN "
38 " default it will reset hba :0"
39 " set to 1 to avoid resetting HBA");
41 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
42 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IRUSR);
43 MODULE_PARM_DESC(ql4xextended_error_logging,
44 "Option to enable extended error logging, "
45 "Default is 0 - no logging, 1 - debug logging");
47 int ql4_mod_unload = 0;
50 * SCSI host template entry points
52 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
55 * iSCSI template entry points
57 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
58 enum iscsi_tgt_dscvr type, uint32_t enable,
59 struct sockaddr *dst_addr);
60 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
61 enum iscsi_param param, char *buf);
62 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
63 enum iscsi_param param, char *buf);
64 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
65 enum iscsi_host_param param, char *buf);
66 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag);
67 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
68 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
71 * SCSI host template entry points
73 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
74 void (*done) (struct scsi_cmnd *));
75 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
76 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
77 static int qla4xxx_slave_alloc(struct scsi_device *device);
78 static int qla4xxx_slave_configure(struct scsi_device *device);
79 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
81 static struct scsi_host_template qla4xxx_driver_template = {
82 .module = THIS_MODULE,
84 .proc_name = DRIVER_NAME,
85 .queuecommand = qla4xxx_queuecommand,
87 .eh_device_reset_handler = qla4xxx_eh_device_reset,
88 .eh_host_reset_handler = qla4xxx_eh_host_reset,
90 .slave_configure = qla4xxx_slave_configure,
91 .slave_alloc = qla4xxx_slave_alloc,
92 .slave_destroy = qla4xxx_slave_destroy,
96 .use_clustering = ENABLE_CLUSTERING,
97 .sg_tablesize = SG_ALL,
99 .max_sectors = 0xFFFF,
102 static struct iscsi_transport qla4xxx_iscsi_transport = {
103 .owner = THIS_MODULE,
105 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
106 CAP_DATA_PATH_OFFLOAD,
107 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
108 ISCSI_TARGET_NAME | ISCSI_TPGT,
109 .host_param_mask = ISCSI_HOST_HWADDRESS |
110 ISCSI_HOST_IPADDRESS |
111 ISCSI_HOST_INITIATOR_NAME,
112 .sessiondata_size = sizeof(struct ddb_entry),
113 .host_template = &qla4xxx_driver_template,
115 .tgt_dscvr = qla4xxx_tgt_dscvr,
116 .get_conn_param = qla4xxx_conn_get_param,
117 .get_session_param = qla4xxx_sess_get_param,
118 .get_host_param = qla4xxx_host_get_param,
119 .start_conn = qla4xxx_conn_start,
120 .stop_conn = qla4xxx_conn_stop,
121 .session_recovery_timedout = qla4xxx_recovery_timedout,
124 static struct scsi_transport_template *qla4xxx_scsi_transport;
126 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
128 struct ddb_entry *ddb_entry = session->dd_data;
129 struct scsi_qla_host *ha = ddb_entry->ha;
131 DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count of (%d) "
132 "secs exhausted, marking device DEAD.\n", ha->host_no,
133 __func__, ddb_entry->fw_ddb_index,
134 ha->port_down_retry_count));
136 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
138 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc flags = "
139 "0x%lx\n", ha->host_no, __func__, ha->dpc_flags));
140 queue_work(ha->dpc_thread, &ha->dpc_work);
143 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn)
145 struct iscsi_cls_session *session;
146 struct ddb_entry *ddb_entry;
148 session = iscsi_dev_to_session(conn->dev.parent);
149 ddb_entry = session->dd_data;
151 DEBUG2(printk("scsi%ld: %s: index [%d] starting conn\n",
152 ddb_entry->ha->host_no, __func__,
153 ddb_entry->fw_ddb_index));
154 iscsi_unblock_session(session);
158 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag)
160 struct iscsi_cls_session *session;
161 struct ddb_entry *ddb_entry;
163 session = iscsi_dev_to_session(conn->dev.parent);
164 ddb_entry = session->dd_data;
166 DEBUG2(printk("scsi%ld: %s: index [%d] stopping conn\n",
167 ddb_entry->ha->host_no, __func__,
168 ddb_entry->fw_ddb_index));
169 if (flag == STOP_CONN_RECOVER)
170 iscsi_block_session(session);
172 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
175 static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
180 for (i = 0; i < len; i++)
181 cp += sprintf(cp, "%02x%c", addr[i],
182 i == (len - 1) ? '\n' : ':');
187 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
188 enum iscsi_host_param param, char *buf)
190 struct scsi_qla_host *ha = to_qla_host(shost);
194 case ISCSI_HOST_PARAM_HWADDRESS:
195 len = format_addr(buf, ha->my_mac, MAC_ADDR_LEN);
197 case ISCSI_HOST_PARAM_IPADDRESS:
198 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
199 ha->ip_address[1], ha->ip_address[2],
202 case ISCSI_HOST_PARAM_INITIATOR_NAME:
203 len = sprintf(buf, "%s\n", ha->name_string);
212 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
213 enum iscsi_param param, char *buf)
215 struct ddb_entry *ddb_entry = sess->dd_data;
219 case ISCSI_PARAM_TARGET_NAME:
220 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
221 ddb_entry->iscsi_name);
223 case ISCSI_PARAM_TPGT:
224 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
233 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
234 enum iscsi_param param, char *buf)
236 struct iscsi_cls_session *session;
237 struct ddb_entry *ddb_entry;
240 session = iscsi_dev_to_session(conn->dev.parent);
241 ddb_entry = session->dd_data;
244 case ISCSI_PARAM_CONN_PORT:
245 len = sprintf(buf, "%hu\n", ddb_entry->port);
247 case ISCSI_PARAM_CONN_ADDRESS:
248 /* TODO: what are the ipv6 bits */
249 len = sprintf(buf, "%u.%u.%u.%u\n",
250 NIPQUAD(ddb_entry->ip_addr));
259 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
260 enum iscsi_tgt_dscvr type, uint32_t enable,
261 struct sockaddr *dst_addr)
263 struct scsi_qla_host *ha;
264 struct sockaddr_in *addr;
265 struct sockaddr_in6 *addr6;
268 ha = (struct scsi_qla_host *) shost->hostdata;
271 case ISCSI_TGT_DSCVR_SEND_TARGETS:
272 if (dst_addr->sa_family == AF_INET) {
273 addr = (struct sockaddr_in *)dst_addr;
274 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
275 addr->sin_port) != QLA_SUCCESS)
277 } else if (dst_addr->sa_family == AF_INET6) {
279 * TODO: fix qla4xxx_send_tgts
281 addr6 = (struct sockaddr_in6 *)dst_addr;
282 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
283 addr6->sin6_port) != QLA_SUCCESS)
294 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
296 if (!ddb_entry->sess)
299 if (ddb_entry->conn) {
300 iscsi_if_destroy_session_done(ddb_entry->conn);
301 iscsi_destroy_conn(ddb_entry->conn);
302 iscsi_remove_session(ddb_entry->sess);
304 iscsi_free_session(ddb_entry->sess);
307 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
311 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
313 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
317 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0);
318 if (!ddb_entry->conn) {
319 iscsi_remove_session(ddb_entry->sess);
320 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
324 ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
325 iscsi_if_create_session_done(ddb_entry->conn);
329 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
331 struct ddb_entry *ddb_entry;
332 struct iscsi_cls_session *sess;
334 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport);
338 ddb_entry = sess->dd_data;
339 memset(ddb_entry, 0, sizeof(*ddb_entry));
341 ddb_entry->sess = sess;
349 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
350 unsigned long interval)
352 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
353 __func__, ha->host->host_no));
354 init_timer(&ha->timer);
355 ha->timer.expires = jiffies + interval * HZ;
356 ha->timer.data = (unsigned long)ha;
357 ha->timer.function = (void (*)(unsigned long))func;
358 add_timer(&ha->timer);
359 ha->timer_active = 1;
362 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
364 del_timer_sync(&ha->timer);
365 ha->timer_active = 0;
369 * qla4xxx_mark_device_missing - mark a device as missing.
370 * @ha: Pointer to host adapter structure.
371 * @ddb_entry: Pointer to device database entry
373 * This routine marks a device missing and resets the relogin retry count.
375 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
376 struct ddb_entry *ddb_entry)
378 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
379 DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
380 ha->host_no, ddb_entry->bus, ddb_entry->target,
381 ddb_entry->fw_ddb_index));
382 iscsi_conn_error(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
385 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
386 struct ddb_entry *ddb_entry,
387 struct scsi_cmnd *cmd,
388 void (*done)(struct scsi_cmnd *))
392 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
396 atomic_set(&srb->ref_count, 1);
398 srb->ddb = ddb_entry;
401 cmd->SCp.ptr = (void *)srb;
402 cmd->scsi_done = done;
407 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
409 struct scsi_cmnd *cmd = srb->cmd;
411 if (srb->flags & SRB_DMA_VALID) {
413 srb->flags &= ~SRB_DMA_VALID;
418 void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
420 struct scsi_cmnd *cmd = srb->cmd;
422 qla4xxx_srb_free_dma(ha, srb);
424 mempool_free(srb, ha->srb_mempool);
430 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
431 * @cmd: Pointer to Linux's SCSI command structure
432 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
433 * that the command has been processed.
436 * This routine is invoked by Linux to send a SCSI command to the driver.
437 * The mid-level driver tries to ensure that queuecommand never gets
438 * invoked concurrently with itself or the interrupt handler (although
439 * the interrupt handler may call this routine as part of request-
440 * completion handling). Unfortunely, it sometimes calls the scheduler
441 * in interrupt context which is a big NO! NO!.
443 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
444 void (*done)(struct scsi_cmnd *))
446 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
447 struct ddb_entry *ddb_entry = cmd->device->hostdata;
451 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
452 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
453 cmd->result = DID_NO_CONNECT << 16;
454 goto qc_fail_command;
459 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
462 spin_unlock_irq(ha->host->host_lock);
464 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
466 goto qc_host_busy_lock;
468 rval = qla4xxx_send_command_to_isp(ha, srb);
469 if (rval != QLA_SUCCESS)
470 goto qc_host_busy_free_sp;
472 spin_lock_irq(ha->host->host_lock);
475 qc_host_busy_free_sp:
476 qla4xxx_srb_free_dma(ha, srb);
477 mempool_free(srb, ha->srb_mempool);
480 spin_lock_irq(ha->host->host_lock);
483 return SCSI_MLQUEUE_HOST_BUSY;
492 * qla4xxx_mem_free - frees memory allocated to adapter
493 * @ha: Pointer to host adapter structure.
495 * Frees memory previously allocated by qla4xxx_mem_alloc
497 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
500 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
506 ha->request_ring = NULL;
508 ha->response_ring = NULL;
509 ha->response_dma = 0;
510 ha->shadow_regs = NULL;
511 ha->shadow_regs_dma = 0;
515 mempool_destroy(ha->srb_mempool);
517 ha->srb_mempool = NULL;
519 /* release io space registers */
522 pci_release_regions(ha->pdev);
526 * qla4xxx_mem_alloc - allocates memory for use by adapter.
527 * @ha: Pointer to host adapter structure
529 * Allocates DMA memory for request and response queues. Also allocates memory
532 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
536 /* Allocate contiguous block of DMA memory for queues. */
537 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
538 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
539 sizeof(struct shadow_regs) +
541 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
542 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
543 &ha->queues_dma, GFP_KERNEL);
544 if (ha->queues == NULL) {
545 dev_warn(&ha->pdev->dev,
546 "Memory Allocation failed - queues.\n");
548 goto mem_alloc_error_exit;
550 memset(ha->queues, 0, ha->queues_len);
553 * As per RISC alignment requirements -- the bus-address must be a
554 * multiple of the request-ring size (in bytes).
557 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
558 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
559 (MEM_ALIGN_VALUE - 1));
561 /* Update request and response queue pointers. */
562 ha->request_dma = ha->queues_dma + align;
563 ha->request_ring = (struct queue_entry *) (ha->queues + align);
564 ha->response_dma = ha->queues_dma + align +
565 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
566 ha->response_ring = (struct queue_entry *) (ha->queues + align +
567 (REQUEST_QUEUE_DEPTH *
569 ha->shadow_regs_dma = ha->queues_dma + align +
570 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
571 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
572 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
573 (REQUEST_QUEUE_DEPTH *
575 (RESPONSE_QUEUE_DEPTH *
578 /* Allocate memory for srb pool. */
579 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
580 mempool_free_slab, srb_cachep);
581 if (ha->srb_mempool == NULL) {
582 dev_warn(&ha->pdev->dev,
583 "Memory Allocation failed - SRB Pool.\n");
585 goto mem_alloc_error_exit;
590 mem_alloc_error_exit:
591 qla4xxx_mem_free(ha);
596 * qla4xxx_timer - checks every second for work to do.
597 * @ha: Pointer to host adapter structure.
599 static void qla4xxx_timer(struct scsi_qla_host *ha)
601 struct ddb_entry *ddb_entry, *dtemp;
604 /* Search for relogin's to time-out and port down retry. */
605 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
606 /* Count down time between sending relogins */
607 if (adapter_up(ha) &&
608 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
609 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
610 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
612 if (atomic_read(&ddb_entry->retry_relogin_timer)
614 atomic_set(&ddb_entry->
617 set_bit(DPC_RELOGIN_DEVICE,
619 set_bit(DF_RELOGIN, &ddb_entry->flags);
620 DEBUG2(printk("scsi%ld: %s: index [%d]"
622 ha->host_no, __func__,
623 ddb_entry->fw_ddb_index));
625 atomic_dec(&ddb_entry->
626 retry_relogin_timer);
630 /* Wait for relogin to timeout */
631 if (atomic_read(&ddb_entry->relogin_timer) &&
632 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
634 * If the relogin times out and the device is
635 * still NOT ONLINE then try and relogin again.
637 if (atomic_read(&ddb_entry->state) !=
639 ddb_entry->fw_ddb_device_state ==
640 DDB_DS_SESSION_FAILED) {
641 /* Reset retry relogin timer */
642 atomic_inc(&ddb_entry->relogin_retry_count);
643 DEBUG2(printk("scsi%ld: index[%d] relogin"
644 " timed out-retrying"
647 ddb_entry->fw_ddb_index,
648 atomic_read(&ddb_entry->
649 relogin_retry_count))
652 DEBUG(printk("scsi%ld:%d:%d: index [%d] "
653 "initate relogin after"
655 ha->host_no, ddb_entry->bus,
657 ddb_entry->fw_ddb_index,
658 ddb_entry->default_time2wait + 4)
661 atomic_set(&ddb_entry->retry_relogin_timer,
662 ddb_entry->default_time2wait + 4);
667 /* Check for heartbeat interval. */
668 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
669 ha->heartbeat_interval != 0) {
670 ha->seconds_since_last_heartbeat++;
671 if (ha->seconds_since_last_heartbeat >
672 ha->heartbeat_interval + 2)
673 set_bit(DPC_RESET_HA, &ha->dpc_flags);
677 /* Wakeup the dpc routine for this adapter, if needed. */
679 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
680 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
681 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
682 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
683 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
684 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
685 test_bit(DPC_AEN, &ha->dpc_flags)) &&
687 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
688 " - dpc flags = 0x%lx\n",
689 ha->host_no, __func__, ha->dpc_flags));
690 queue_work(ha->dpc_thread, &ha->dpc_work);
693 /* Reschedule timer thread to call us back in one second */
694 mod_timer(&ha->timer, jiffies + HZ);
696 DEBUG2(ha->seconds_since_last_intr++);
700 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
701 * @ha: Pointer to host adapter structure.
703 * This routine stalls the driver until all outstanding commands are returned.
704 * Caller must release the Hardware Lock prior to calling this routine.
706 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
709 int stat = QLA_SUCCESS;
711 struct scsi_cmnd *cmd;
712 int wait_cnt = WAIT_CMD_TOV; /*
713 * Initialized for 30 seconds as we
714 * expect all commands to retuned
719 spin_lock_irqsave(&ha->hardware_lock, flags);
720 /* Find a command that hasn't completed. */
721 for (index = 0; index < ha->host->can_queue; index++) {
722 cmd = scsi_host_find_tag(ha->host, index);
726 spin_unlock_irqrestore(&ha->hardware_lock, flags);
728 /* If No Commands are pending, wait is complete */
729 if (index == ha->host->can_queue) {
733 /* If we timed out on waiting for commands to come back
742 } /* End of While (wait_cnt) */
747 void qla4xxx_hw_reset(struct scsi_qla_host *ha)
749 uint32_t ctrl_status;
750 unsigned long flags = 0;
752 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
754 spin_lock_irqsave(&ha->hardware_lock, flags);
757 * If the SCSI Reset Interrupt bit is set, clear it.
758 * Otherwise, the Soft Reset won't work.
760 ctrl_status = readw(&ha->reg->ctrl_status);
761 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
762 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
764 /* Issue Soft Reset */
765 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
766 readl(&ha->reg->ctrl_status);
768 spin_unlock_irqrestore(&ha->hardware_lock, flags);
772 * qla4xxx_soft_reset - performs soft reset.
773 * @ha: Pointer to host adapter structure.
775 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
777 uint32_t max_wait_time;
778 unsigned long flags = 0;
779 int status = QLA_ERROR;
780 uint32_t ctrl_status;
782 qla4xxx_hw_reset(ha);
784 /* Wait until the Network Reset Intr bit is cleared */
785 max_wait_time = RESET_INTR_TOV;
787 spin_lock_irqsave(&ha->hardware_lock, flags);
788 ctrl_status = readw(&ha->reg->ctrl_status);
789 spin_unlock_irqrestore(&ha->hardware_lock, flags);
791 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
795 } while ((--max_wait_time));
797 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
798 DEBUG2(printk(KERN_WARNING
799 "scsi%ld: Network Reset Intr not cleared by "
800 "Network function, clearing it now!\n",
802 spin_lock_irqsave(&ha->hardware_lock, flags);
803 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
804 readl(&ha->reg->ctrl_status);
805 spin_unlock_irqrestore(&ha->hardware_lock, flags);
808 /* Wait until the firmware tells us the Soft Reset is done */
809 max_wait_time = SOFT_RESET_TOV;
811 spin_lock_irqsave(&ha->hardware_lock, flags);
812 ctrl_status = readw(&ha->reg->ctrl_status);
813 spin_unlock_irqrestore(&ha->hardware_lock, flags);
815 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
816 status = QLA_SUCCESS;
821 } while ((--max_wait_time));
824 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
825 * after the soft reset has taken place.
827 spin_lock_irqsave(&ha->hardware_lock, flags);
828 ctrl_status = readw(&ha->reg->ctrl_status);
829 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
830 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
831 readl(&ha->reg->ctrl_status);
833 spin_unlock_irqrestore(&ha->hardware_lock, flags);
835 /* If soft reset fails then most probably the bios on other
836 * function is also enabled.
837 * Since the initialization is sequential the other fn
838 * wont be able to acknowledge the soft reset.
839 * Issue a force soft reset to workaround this scenario.
841 if (max_wait_time == 0) {
842 /* Issue Force Soft Reset */
843 spin_lock_irqsave(&ha->hardware_lock, flags);
844 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
845 readl(&ha->reg->ctrl_status);
846 spin_unlock_irqrestore(&ha->hardware_lock, flags);
847 /* Wait until the firmware tells us the Soft Reset is done */
848 max_wait_time = SOFT_RESET_TOV;
850 spin_lock_irqsave(&ha->hardware_lock, flags);
851 ctrl_status = readw(&ha->reg->ctrl_status);
852 spin_unlock_irqrestore(&ha->hardware_lock, flags);
854 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
855 status = QLA_SUCCESS;
860 } while ((--max_wait_time));
867 * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
868 * @ha: Pointer to host adapter structure.
870 * This routine is called just prior to a HARD RESET to return all
871 * outstanding commands back to the Operating System.
872 * Caller should make sure that the following locks are released
873 * before this calling routine: Hardware lock, and io_request_lock.
875 static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
881 spin_lock_irqsave(&ha->hardware_lock, flags);
882 for (i = 0; i < ha->host->can_queue; i++) {
883 srb = qla4xxx_del_from_active_array(ha, i);
885 srb->cmd->result = DID_RESET << 16;
886 qla4xxx_srb_compl(ha, srb);
889 spin_unlock_irqrestore(&ha->hardware_lock, flags);
894 * qla4xxx_recover_adapter - recovers adapter after a fatal error
895 * @ha: Pointer to host adapter structure.
896 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
897 * after adapter recovery has completed.
898 * 0=preserve ddb list, 1=destroy and rebuild ddb list
900 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
901 uint8_t renew_ddb_list)
905 /* Stall incoming I/O until we are done */
906 clear_bit(AF_ONLINE, &ha->flags);
907 DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
910 /* Wait for outstanding commands to complete.
911 * Stalls the driver for max 30 secs
913 status = qla4xxx_cmd_wait(ha);
915 qla4xxx_disable_intrs(ha);
917 /* Flush any pending ddb changed AENs */
918 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
920 /* Reset the firmware. If successful, function
921 * returns with ISP interrupts enabled.
923 if (status == QLA_SUCCESS) {
924 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
925 ha->host_no, __func__));
926 qla4xxx_flush_active_srbs(ha);
927 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
928 status = qla4xxx_soft_reset(ha);
933 /* Flush any pending ddb changed AENs */
934 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
936 /* Re-initialize firmware. If successful, function returns
937 * with ISP interrupts enabled */
938 if (status == QLA_SUCCESS) {
939 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
940 ha->host_no, __func__));
942 /* If successful, AF_ONLINE flag set in
943 * qla4xxx_initialize_adapter */
944 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
947 /* Failed adapter initialization?
948 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
949 if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
950 (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
951 /* Adapter initialization failed, see if we can retry
952 * resetting the ha */
953 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
954 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
955 DEBUG2(printk("scsi%ld: recover adapter - retrying "
956 "(%d) more times\n", ha->host_no,
957 ha->retry_reset_ha_cnt));
958 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
961 if (ha->retry_reset_ha_cnt > 0) {
962 /* Schedule another Reset HA--DPC will retry */
963 ha->retry_reset_ha_cnt--;
964 DEBUG2(printk("scsi%ld: recover adapter - "
965 "retry remaining %d\n",
967 ha->retry_reset_ha_cnt));
971 if (ha->retry_reset_ha_cnt == 0) {
972 /* Recover adapter retries have been exhausted.
974 DEBUG2(printk("scsi%ld: recover adapter "
975 "failed - board disabled\n",
977 qla4xxx_flush_active_srbs(ha);
978 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
979 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
980 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
986 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
987 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
988 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
991 ha->adapter_error_count++;
993 if (status == QLA_SUCCESS)
994 qla4xxx_enable_intrs(ha);
996 DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
1001 * qla4xxx_do_dpc - dpc routine
1002 * @data: in our case pointer to adapter structure
1004 * This routine is a task that is schedule by the interrupt handler
1005 * to perform the background processing for interrupts. We put it
1006 * on a task queue that is consumed whenever the scheduler runs; that's
1007 * so you can do anything (i.e. put the process to sleep etc). In fact,
1008 * the mid-level tries to sleep when it reaches the driver threshold
1009 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1011 static void qla4xxx_do_dpc(struct work_struct *work)
1013 struct scsi_qla_host *ha =
1014 container_of(work, struct scsi_qla_host, dpc_work);
1015 struct ddb_entry *ddb_entry, *dtemp;
1016 int status = QLA_ERROR;
1018 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1019 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
1020 ha->host_no, __func__, ha->flags, ha->dpc_flags,
1021 readw(&ha->reg->ctrl_status)));
1023 /* Initialization not yet finished. Don't do anything yet. */
1024 if (!test_bit(AF_INIT_DONE, &ha->flags))
1027 if (adapter_up(ha) ||
1028 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1029 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1030 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
1031 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1032 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1033 qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1035 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1036 uint8_t wait_time = RESET_INTR_TOV;
1038 while ((readw(&ha->reg->ctrl_status) &
1039 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1040 if (--wait_time == 0)
1045 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1046 "bit not cleared-- resetting\n",
1047 ha->host_no, __func__));
1048 qla4xxx_flush_active_srbs(ha);
1049 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1050 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1051 status = qla4xxx_initialize_adapter(ha,
1054 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1055 if (status == QLA_SUCCESS)
1056 qla4xxx_enable_intrs(ha);
1060 /* ---- process AEN? --- */
1061 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1062 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1064 /* ---- Get DHCP IP Address? --- */
1065 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1066 qla4xxx_get_dhcp_ip_address(ha);
1068 /* ---- relogin device? --- */
1069 if (adapter_up(ha) &&
1070 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1071 list_for_each_entry_safe(ddb_entry, dtemp,
1072 &ha->ddb_list, list) {
1073 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1074 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1075 qla4xxx_relogin_device(ha, ddb_entry);
1078 * If mbx cmd times out there is no point
1079 * in continuing further.
1080 * With large no of targets this can hang
1083 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1084 printk(KERN_WARNING "scsi%ld: %s: "
1085 "need to reset hba\n",
1086 ha->host_no, __func__);
1094 * qla4xxx_free_adapter - release the adapter
1095 * @ha: pointer to adapter structure
1097 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1100 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1101 /* Turn-off interrupts on the card. */
1102 qla4xxx_disable_intrs(ha);
1105 /* Kill the kernel thread for this host */
1107 destroy_workqueue(ha->dpc_thread);
1109 /* Issue Soft Reset to put firmware in unknown state */
1110 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
1111 qla4xxx_hw_reset(ha);
1113 /* Remove timer thread, if present */
1114 if (ha->timer_active)
1115 qla4xxx_stop_timer(ha);
1117 /* Detach interrupts */
1118 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1119 free_irq(ha->pdev->irq, ha);
1121 /* free extra memory */
1122 qla4xxx_mem_free(ha);
1124 pci_disable_device(ha->pdev);
1129 * qla4xxx_iospace_config - maps registers
1130 * @ha: pointer to adapter structure
1132 * This routines maps HBA's registers from the pci address space
1133 * into the kernel virtual address space for memory mapped i/o.
1135 static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1137 unsigned long pio, pio_len, pio_flags;
1138 unsigned long mmio, mmio_len, mmio_flags;
1140 pio = pci_resource_start(ha->pdev, 0);
1141 pio_len = pci_resource_len(ha->pdev, 0);
1142 pio_flags = pci_resource_flags(ha->pdev, 0);
1143 if (pio_flags & IORESOURCE_IO) {
1144 if (pio_len < MIN_IOBASE_LEN) {
1145 dev_warn(&ha->pdev->dev,
1146 "Invalid PCI I/O region size\n");
1150 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1154 /* Use MMIO operations for all accesses. */
1155 mmio = pci_resource_start(ha->pdev, 1);
1156 mmio_len = pci_resource_len(ha->pdev, 1);
1157 mmio_flags = pci_resource_flags(ha->pdev, 1);
1159 if (!(mmio_flags & IORESOURCE_MEM)) {
1160 dev_err(&ha->pdev->dev,
1161 "region #0 not an MMIO resource, aborting\n");
1163 goto iospace_error_exit;
1165 if (mmio_len < MIN_IOBASE_LEN) {
1166 dev_err(&ha->pdev->dev,
1167 "Invalid PCI mem region size, aborting\n");
1168 goto iospace_error_exit;
1171 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1172 dev_warn(&ha->pdev->dev,
1173 "Failed to reserve PIO/MMIO regions\n");
1175 goto iospace_error_exit;
1178 ha->pio_address = pio;
1179 ha->pio_length = pio_len;
1180 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1182 dev_err(&ha->pdev->dev,
1183 "cannot remap MMIO, aborting\n");
1185 goto iospace_error_exit;
1195 * qla4xxx_probe_adapter - callback function to probe HBA
1196 * @pdev: pointer to pci_dev structure
1197 * @pci_device_id: pointer to pci_device entry
1199 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1200 * It returns zero if successful. It also initializes all data necessary for
1203 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1204 const struct pci_device_id *ent)
1206 int ret = -ENODEV, status;
1207 struct Scsi_Host *host;
1208 struct scsi_qla_host *ha;
1209 struct ddb_entry *ddb_entry, *ddbtemp;
1210 uint8_t init_retry_count = 0;
1213 if (pci_enable_device(pdev))
1216 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1219 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1220 goto probe_disable_device;
1223 /* Clear our data area */
1224 ha = (struct scsi_qla_host *) host->hostdata;
1225 memset(ha, 0, sizeof(*ha));
1227 /* Save the information from PCI BIOS. */
1230 ha->host_no = host->host_no;
1232 /* Configure PCI I/O space. */
1233 ret = qla4xxx_iospace_config(ha);
1237 dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1238 pdev->device, pdev->irq, ha->reg);
1240 qla4xxx_config_dma_addressing(ha);
1242 /* Initialize lists and spinlocks. */
1243 INIT_LIST_HEAD(&ha->ddb_list);
1244 INIT_LIST_HEAD(&ha->free_srb_q);
1246 mutex_init(&ha->mbox_sem);
1248 spin_lock_init(&ha->hardware_lock);
1250 /* Allocate dma buffers */
1251 if (qla4xxx_mem_alloc(ha)) {
1252 dev_warn(&ha->pdev->dev,
1253 "[ERROR] Failed to allocate memory for adapter\n");
1260 * Initialize the Host adapter request/response queues and
1262 * NOTE: interrupts enabled upon successful completion
1264 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1265 while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1266 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1267 "(%d)\n", __func__, init_retry_count));
1268 qla4xxx_soft_reset(ha);
1269 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1271 if (status == QLA_ERROR) {
1272 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1278 host->cmd_per_lun = 3;
1279 host->max_channel = 0;
1280 host->max_lun = MAX_LUNS - 1;
1281 host->max_id = MAX_TARGETS;
1282 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1283 host->can_queue = MAX_SRBS ;
1284 host->transportt = qla4xxx_scsi_transport;
1286 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1288 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed");
1292 /* Startup the kernel thread for this host adapter. */
1293 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1294 "qla4xxx_dpc\n", __func__));
1295 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1296 ha->dpc_thread = create_singlethread_workqueue(buf);
1297 if (!ha->dpc_thread) {
1298 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1302 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1304 ret = request_irq(pdev->irq, qla4xxx_intr_handler,
1305 IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
1307 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1308 " already in use.\n", pdev->irq);
1311 set_bit(AF_IRQ_ATTACHED, &ha->flags);
1312 host->irq = pdev->irq;
1313 DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1315 qla4xxx_enable_intrs(ha);
1317 /* Start timer thread. */
1318 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1320 set_bit(AF_INIT_DONE, &ha->flags);
1322 pci_set_drvdata(pdev, ha);
1324 ret = scsi_add_host(host, &pdev->dev);
1328 /* Update transport device information for all devices. */
1329 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
1330 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
1331 if (qla4xxx_add_sess(ddb_entry))
1336 " QLogic iSCSI HBA Driver version: %s\n"
1337 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1338 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1339 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1340 ha->patch_number, ha->build_number);
1345 qla4xxx_free_ddb_list(ha);
1346 scsi_remove_host(host);
1349 qla4xxx_free_adapter(ha);
1350 scsi_host_put(ha->host);
1352 probe_disable_device:
1353 pci_disable_device(pdev);
1359 * qla4xxx_remove_adapter - calback function to remove adapter.
1360 * @pci_dev: PCI device pointer
1362 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1364 struct scsi_qla_host *ha;
1366 ha = pci_get_drvdata(pdev);
1368 qla4xxx_disable_intrs(ha);
1370 while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1373 /* remove devs from iscsi_sessions to scsi_devices */
1374 qla4xxx_free_ddb_list(ha);
1376 scsi_remove_host(ha->host);
1378 qla4xxx_free_adapter(ha);
1380 scsi_host_put(ha->host);
1382 pci_set_drvdata(pdev, NULL);
1386 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1389 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1390 * supported addressing method.
1392 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1396 /* Update our PCI device dma_mask for full 64 bit mask */
1397 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1398 if (pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1399 dev_dbg(&ha->pdev->dev,
1400 "Failed to set 64 bit PCI consistent mask; "
1402 retval = pci_set_consistent_dma_mask(ha->pdev,
1406 retval = pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1409 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1411 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1412 struct ddb_entry *ddb = sess->dd_data;
1414 sdev->hostdata = ddb;
1415 sdev->tagged_supported = 1;
1416 scsi_activate_tcq(sdev, sdev->host->can_queue);
1420 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1422 sdev->tagged_supported = 1;
1426 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1428 scsi_deactivate_tcq(sdev, 1);
1432 * qla4xxx_del_from_active_array - returns an active srb
1433 * @ha: Pointer to host adapter structure.
1434 * @index: index into to the active_array
1436 * This routine removes and returns the srb at the specified index
1438 struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1440 struct srb *srb = NULL;
1441 struct scsi_cmnd *cmd;
1443 if (!(cmd = scsi_host_find_tag(ha->host, index)))
1446 if (!(srb = (struct srb *)cmd->host_scribble))
1449 /* update counters */
1450 if (srb->flags & SRB_DMA_VALID) {
1451 ha->req_q_count += srb->iocb_cnt;
1452 ha->iocb_cnt -= srb->iocb_cnt;
1454 srb->cmd->host_scribble = NULL;
1460 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1461 * @ha: actual ha whose done queue will contain the comd returned by firmware.
1462 * @cmd: Scsi Command to wait on.
1464 * This routine waits for the command to be returned by the Firmware
1465 * for some max time.
1467 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1468 struct scsi_cmnd *cmd)
1472 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1475 /* Checking to see if its returned to OS */
1476 rp = (struct srb *) cmd->SCp.ptr;
1483 } while (max_wait_time--);
1489 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1490 * @ha: Pointer to host adapter structure
1492 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1494 unsigned long wait_online;
1496 wait_online = jiffies + (30 * HZ);
1497 while (time_before(jiffies, wait_online)) {
1501 else if (ha->retry_reset_ha_cnt == 0)
1511 * qla4xxx_eh_wait_for_active_target_commands - wait for active cmds to finish.
1512 * @ha: pointer to to HBA
1516 * This function waits for all outstanding commands to a lun to complete. It
1517 * returns 0 if all pending commands are returned and 1 otherwise.
1519 static int qla4xxx_eh_wait_for_active_target_commands(struct scsi_qla_host *ha,
1524 struct scsi_cmnd *cmd;
1527 * Waiting for all commands for the designated target in the active
1530 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1531 cmd = scsi_host_find_tag(ha->host, cnt);
1532 if (cmd && cmd->device->id == t && cmd->device->lun == l) {
1533 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1543 * qla4xxx_eh_device_reset - callback for target reset.
1544 * @cmd: Pointer to Linux's SCSI command structure
1546 * This routine is called by the Linux OS to reset all luns on the
1549 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1551 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1552 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1554 int ret = FAILED, stat;
1556 sp = (struct srb *) cmd->SCp.ptr;
1557 if (!sp || !ddb_entry)
1560 dev_info(&ha->pdev->dev,
1561 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1562 cmd->device->channel, cmd->device->id, cmd->device->lun);
1564 DEBUG2(printk(KERN_INFO
1565 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1566 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
1567 cmd, jiffies, cmd->timeout_per_command / HZ,
1568 ha->dpc_flags, cmd->result, cmd->allowed));
1570 /* FIXME: wait for hba to go online */
1571 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1572 if (stat != QLA_SUCCESS) {
1573 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1574 goto eh_dev_reset_done;
1578 ha->marker_needed = 1;
1581 * If we are coming down the EH path, wait for all commands to complete
1584 if (cmd->device->host->shost_state == SHOST_RECOVERY) {
1585 if (qla4xxx_eh_wait_for_active_target_commands(ha,
1588 dev_info(&ha->pdev->dev,
1589 "DEVICE RESET FAILED - waiting for "
1591 goto eh_dev_reset_done;
1595 dev_info(&ha->pdev->dev,
1596 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1597 ha->host_no, cmd->device->channel, cmd->device->id,
1608 * qla4xxx_eh_host_reset - kernel callback
1609 * @cmd: Pointer to Linux's SCSI command structure
1611 * This routine is invoked by the Linux kernel to perform fatal error
1612 * recovery on the specified adapter.
1614 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1616 int return_status = FAILED;
1617 struct scsi_qla_host *ha;
1619 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1621 dev_info(&ha->pdev->dev,
1622 "scsi(%ld:%d:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no,
1623 cmd->device->channel, cmd->device->id, cmd->device->lun);
1625 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1626 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
1627 "DEAD.\n", ha->host_no, cmd->device->channel,
1633 if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS) {
1634 return_status = SUCCESS;
1637 dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1638 return_status == FAILED ? "FAILED" : "SUCCEDED");
1640 return return_status;
1644 static struct pci_device_id qla4xxx_pci_tbl[] = {
1646 .vendor = PCI_VENDOR_ID_QLOGIC,
1647 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
1648 .subvendor = PCI_ANY_ID,
1649 .subdevice = PCI_ANY_ID,
1652 .vendor = PCI_VENDOR_ID_QLOGIC,
1653 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
1654 .subvendor = PCI_ANY_ID,
1655 .subdevice = PCI_ANY_ID,
1658 .vendor = PCI_VENDOR_ID_QLOGIC,
1659 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
1660 .subvendor = PCI_ANY_ID,
1661 .subdevice = PCI_ANY_ID,
1665 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1667 static struct pci_driver qla4xxx_pci_driver = {
1668 .name = DRIVER_NAME,
1669 .id_table = qla4xxx_pci_tbl,
1670 .probe = qla4xxx_probe_adapter,
1671 .remove = qla4xxx_remove_adapter,
1674 static int __init qla4xxx_module_init(void)
1678 /* Allocate cache for SRBs. */
1679 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
1680 SLAB_HWCACHE_ALIGN, NULL, NULL);
1681 if (srb_cachep == NULL) {
1683 "%s: Unable to allocate SRB cache..."
1684 "Failing load!\n", DRIVER_NAME);
1689 /* Derive version string. */
1690 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
1691 if (ql4xextended_error_logging)
1692 strcat(qla4xxx_version_str, "-debug");
1694 qla4xxx_scsi_transport =
1695 iscsi_register_transport(&qla4xxx_iscsi_transport);
1696 if (!qla4xxx_scsi_transport){
1698 goto release_srb_cache;
1701 ret = pci_register_driver(&qla4xxx_pci_driver);
1703 goto unregister_transport;
1705 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1708 unregister_transport:
1709 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1711 kmem_cache_destroy(srb_cachep);
1716 static void __exit qla4xxx_module_exit(void)
1719 pci_unregister_driver(&qla4xxx_pci_driver);
1720 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1721 kmem_cache_destroy(srb_cachep);
1724 module_init(qla4xxx_module_init);
1725 module_exit(qla4xxx_module_exit);
1727 MODULE_AUTHOR("QLogic Corporation");
1728 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1729 MODULE_LICENSE("GPL");
1730 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);