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_IRUSR);
34 MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
35 int ql4xdontresethba = 0;
36 module_param(ql4xdontresethba, int, S_IRUGO | S_IRUSR);
37 MODULE_PARM_DESC(ql4xdontresethba,
38 "Dont reset the HBA when the driver gets 0x8002 AEN "
39 " default it will reset hba :0"
40 " set to 1 to avoid resetting HBA");
42 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
43 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IRUSR);
44 MODULE_PARM_DESC(ql4xextended_error_logging,
45 "Option to enable extended error logging, "
46 "Default is 0 - no logging, 1 - debug logging");
48 int ql4_mod_unload = 0;
50 #define QL4_DEF_QDEPTH 32
53 * SCSI host template entry points
55 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
58 * iSCSI template entry points
60 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
61 enum iscsi_tgt_dscvr type, uint32_t enable,
62 struct sockaddr *dst_addr);
63 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
64 enum iscsi_param param, char *buf);
65 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
66 enum iscsi_param param, char *buf);
67 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
68 enum iscsi_host_param param, char *buf);
69 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
70 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
73 * SCSI host template entry points
75 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
76 void (*done) (struct scsi_cmnd *));
77 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
78 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
79 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
80 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
81 static int qla4xxx_slave_alloc(struct scsi_device *device);
82 static int qla4xxx_slave_configure(struct scsi_device *device);
83 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
84 static void qla4xxx_scan_start(struct Scsi_Host *shost);
86 static struct scsi_host_template qla4xxx_driver_template = {
87 .module = THIS_MODULE,
89 .proc_name = DRIVER_NAME,
90 .queuecommand = qla4xxx_queuecommand,
92 .eh_abort_handler = qla4xxx_eh_abort,
93 .eh_device_reset_handler = qla4xxx_eh_device_reset,
94 .eh_target_reset_handler = qla4xxx_eh_target_reset,
95 .eh_host_reset_handler = qla4xxx_eh_host_reset,
96 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
98 .slave_configure = qla4xxx_slave_configure,
99 .slave_alloc = qla4xxx_slave_alloc,
100 .slave_destroy = qla4xxx_slave_destroy,
102 .scan_finished = iscsi_scan_finished,
103 .scan_start = qla4xxx_scan_start,
107 .use_clustering = ENABLE_CLUSTERING,
108 .sg_tablesize = SG_ALL,
110 .max_sectors = 0xFFFF,
113 static struct iscsi_transport qla4xxx_iscsi_transport = {
114 .owner = THIS_MODULE,
116 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
117 CAP_DATA_PATH_OFFLOAD,
118 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
119 ISCSI_TARGET_NAME | ISCSI_TPGT,
120 .host_param_mask = ISCSI_HOST_HWADDRESS |
121 ISCSI_HOST_IPADDRESS |
122 ISCSI_HOST_INITIATOR_NAME,
123 .tgt_dscvr = qla4xxx_tgt_dscvr,
124 .get_conn_param = qla4xxx_conn_get_param,
125 .get_session_param = qla4xxx_sess_get_param,
126 .get_host_param = qla4xxx_host_get_param,
127 .session_recovery_timedout = qla4xxx_recovery_timedout,
130 static struct scsi_transport_template *qla4xxx_scsi_transport;
132 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
134 struct iscsi_cls_session *session;
135 struct ddb_entry *ddb_entry;
137 session = starget_to_session(scsi_target(sc->device));
138 ddb_entry = session->dd_data;
140 /* if we are not logged in then the LLD is going to clean up the cmd */
141 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
142 return BLK_EH_RESET_TIMER;
144 return BLK_EH_NOT_HANDLED;
147 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
149 struct ddb_entry *ddb_entry = session->dd_data;
150 struct scsi_qla_host *ha = ddb_entry->ha;
152 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
153 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
155 DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count "
156 "of (%d) secs exhausted, marking device DEAD.\n",
157 ha->host_no, __func__, ddb_entry->fw_ddb_index,
158 ha->port_down_retry_count));
160 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc "
162 ha->host_no, __func__, ha->dpc_flags));
163 queue_work(ha->dpc_thread, &ha->dpc_work);
167 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
168 enum iscsi_host_param param, char *buf)
170 struct scsi_qla_host *ha = to_qla_host(shost);
174 case ISCSI_HOST_PARAM_HWADDRESS:
175 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
177 case ISCSI_HOST_PARAM_IPADDRESS:
178 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
179 ha->ip_address[1], ha->ip_address[2],
182 case ISCSI_HOST_PARAM_INITIATOR_NAME:
183 len = sprintf(buf, "%s\n", ha->name_string);
192 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
193 enum iscsi_param param, char *buf)
195 struct ddb_entry *ddb_entry = sess->dd_data;
199 case ISCSI_PARAM_TARGET_NAME:
200 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
201 ddb_entry->iscsi_name);
203 case ISCSI_PARAM_TPGT:
204 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
213 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
214 enum iscsi_param param, char *buf)
216 struct iscsi_cls_session *session;
217 struct ddb_entry *ddb_entry;
220 session = iscsi_dev_to_session(conn->dev.parent);
221 ddb_entry = session->dd_data;
224 case ISCSI_PARAM_CONN_PORT:
225 len = sprintf(buf, "%hu\n", ddb_entry->port);
227 case ISCSI_PARAM_CONN_ADDRESS:
228 /* TODO: what are the ipv6 bits */
229 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
238 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
239 enum iscsi_tgt_dscvr type, uint32_t enable,
240 struct sockaddr *dst_addr)
242 struct scsi_qla_host *ha;
243 struct sockaddr_in *addr;
244 struct sockaddr_in6 *addr6;
247 ha = (struct scsi_qla_host *) shost->hostdata;
250 case ISCSI_TGT_DSCVR_SEND_TARGETS:
251 if (dst_addr->sa_family == AF_INET) {
252 addr = (struct sockaddr_in *)dst_addr;
253 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
254 addr->sin_port) != QLA_SUCCESS)
256 } else if (dst_addr->sa_family == AF_INET6) {
258 * TODO: fix qla4xxx_send_tgts
260 addr6 = (struct sockaddr_in6 *)dst_addr;
261 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
262 addr6->sin6_port) != QLA_SUCCESS)
273 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
275 if (!ddb_entry->sess)
278 if (ddb_entry->conn) {
279 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
280 iscsi_remove_session(ddb_entry->sess);
282 iscsi_free_session(ddb_entry->sess);
285 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
289 ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
290 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
292 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
296 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
297 if (!ddb_entry->conn) {
298 iscsi_remove_session(ddb_entry->sess);
299 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
303 /* finally ready to go */
304 iscsi_unblock_session(ddb_entry->sess);
308 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
310 struct ddb_entry *ddb_entry;
311 struct iscsi_cls_session *sess;
313 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
314 sizeof(struct ddb_entry));
318 ddb_entry = sess->dd_data;
319 memset(ddb_entry, 0, sizeof(*ddb_entry));
321 ddb_entry->sess = sess;
325 static void qla4xxx_scan_start(struct Scsi_Host *shost)
327 struct scsi_qla_host *ha = shost_priv(shost);
328 struct ddb_entry *ddb_entry, *ddbtemp;
330 /* finish setup of sessions that were already setup in firmware */
331 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
332 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
333 qla4xxx_add_sess(ddb_entry);
341 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
342 unsigned long interval)
344 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
345 __func__, ha->host->host_no));
346 init_timer(&ha->timer);
347 ha->timer.expires = jiffies + interval * HZ;
348 ha->timer.data = (unsigned long)ha;
349 ha->timer.function = (void (*)(unsigned long))func;
350 add_timer(&ha->timer);
351 ha->timer_active = 1;
354 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
356 del_timer_sync(&ha->timer);
357 ha->timer_active = 0;
361 * qla4xxx_mark_device_missing - mark a device as missing.
362 * @ha: Pointer to host adapter structure.
363 * @ddb_entry: Pointer to device database entry
365 * This routine marks a device missing and resets the relogin retry count.
367 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
368 struct ddb_entry *ddb_entry)
370 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
371 DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
372 ha->host_no, ddb_entry->bus, ddb_entry->target,
373 ddb_entry->fw_ddb_index));
374 iscsi_block_session(ddb_entry->sess);
375 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
378 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
379 struct ddb_entry *ddb_entry,
380 struct scsi_cmnd *cmd,
381 void (*done)(struct scsi_cmnd *))
385 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
389 kref_init(&srb->srb_ref);
391 srb->ddb = ddb_entry;
394 CMD_SP(cmd) = (void *)srb;
395 cmd->scsi_done = done;
400 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
402 struct scsi_cmnd *cmd = srb->cmd;
404 if (srb->flags & SRB_DMA_VALID) {
406 srb->flags &= ~SRB_DMA_VALID;
411 void qla4xxx_srb_compl(struct kref *ref)
413 struct srb *srb = container_of(ref, struct srb, srb_ref);
414 struct scsi_cmnd *cmd = srb->cmd;
415 struct scsi_qla_host *ha = srb->ha;
417 qla4xxx_srb_free_dma(ha, srb);
419 mempool_free(srb, ha->srb_mempool);
425 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
426 * @cmd: Pointer to Linux's SCSI command structure
427 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
428 * that the command has been processed.
431 * This routine is invoked by Linux to send a SCSI command to the driver.
432 * The mid-level driver tries to ensure that queuecommand never gets
433 * invoked concurrently with itself or the interrupt handler (although
434 * the interrupt handler may call this routine as part of request-
435 * completion handling). Unfortunely, it sometimes calls the scheduler
436 * in interrupt context which is a big NO! NO!.
438 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
439 void (*done)(struct scsi_cmnd *))
441 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
442 struct ddb_entry *ddb_entry = cmd->device->hostdata;
443 struct iscsi_cls_session *sess = ddb_entry->sess;
448 cmd->result = DID_IMM_RETRY << 16;
449 goto qc_fail_command;
452 rval = iscsi_session_chkready(sess);
455 goto qc_fail_command;
458 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
459 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
460 cmd->result = DID_NO_CONNECT << 16;
461 goto qc_fail_command;
463 return SCSI_MLQUEUE_TARGET_BUSY;
466 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
469 spin_unlock_irq(ha->host->host_lock);
471 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
473 goto qc_host_busy_lock;
475 rval = qla4xxx_send_command_to_isp(ha, srb);
476 if (rval != QLA_SUCCESS)
477 goto qc_host_busy_free_sp;
479 spin_lock_irq(ha->host->host_lock);
482 qc_host_busy_free_sp:
483 qla4xxx_srb_free_dma(ha, srb);
484 mempool_free(srb, ha->srb_mempool);
487 spin_lock_irq(ha->host->host_lock);
490 return SCSI_MLQUEUE_HOST_BUSY;
499 * qla4xxx_mem_free - frees memory allocated to adapter
500 * @ha: Pointer to host adapter structure.
502 * Frees memory previously allocated by qla4xxx_mem_alloc
504 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
507 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
513 ha->request_ring = NULL;
515 ha->response_ring = NULL;
516 ha->response_dma = 0;
517 ha->shadow_regs = NULL;
518 ha->shadow_regs_dma = 0;
522 mempool_destroy(ha->srb_mempool);
524 ha->srb_mempool = NULL;
526 /* release io space registers */
529 pci_release_regions(ha->pdev);
533 * qla4xxx_mem_alloc - allocates memory for use by adapter.
534 * @ha: Pointer to host adapter structure
536 * Allocates DMA memory for request and response queues. Also allocates memory
539 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
543 /* Allocate contiguous block of DMA memory for queues. */
544 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
545 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
546 sizeof(struct shadow_regs) +
548 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
549 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
550 &ha->queues_dma, GFP_KERNEL);
551 if (ha->queues == NULL) {
552 dev_warn(&ha->pdev->dev,
553 "Memory Allocation failed - queues.\n");
555 goto mem_alloc_error_exit;
557 memset(ha->queues, 0, ha->queues_len);
560 * As per RISC alignment requirements -- the bus-address must be a
561 * multiple of the request-ring size (in bytes).
564 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
565 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
566 (MEM_ALIGN_VALUE - 1));
568 /* Update request and response queue pointers. */
569 ha->request_dma = ha->queues_dma + align;
570 ha->request_ring = (struct queue_entry *) (ha->queues + align);
571 ha->response_dma = ha->queues_dma + align +
572 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
573 ha->response_ring = (struct queue_entry *) (ha->queues + align +
574 (REQUEST_QUEUE_DEPTH *
576 ha->shadow_regs_dma = ha->queues_dma + align +
577 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
578 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
579 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
580 (REQUEST_QUEUE_DEPTH *
582 (RESPONSE_QUEUE_DEPTH *
585 /* Allocate memory for srb pool. */
586 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
587 mempool_free_slab, srb_cachep);
588 if (ha->srb_mempool == NULL) {
589 dev_warn(&ha->pdev->dev,
590 "Memory Allocation failed - SRB Pool.\n");
592 goto mem_alloc_error_exit;
597 mem_alloc_error_exit:
598 qla4xxx_mem_free(ha);
603 * qla4xxx_timer - checks every second for work to do.
604 * @ha: Pointer to host adapter structure.
606 static void qla4xxx_timer(struct scsi_qla_host *ha)
608 struct ddb_entry *ddb_entry, *dtemp;
611 /* Search for relogin's to time-out and port down retry. */
612 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
613 /* Count down time between sending relogins */
614 if (adapter_up(ha) &&
615 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
616 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
617 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
619 if (atomic_read(&ddb_entry->retry_relogin_timer)
621 atomic_set(&ddb_entry->
624 set_bit(DPC_RELOGIN_DEVICE,
626 set_bit(DF_RELOGIN, &ddb_entry->flags);
627 DEBUG2(printk("scsi%ld: %s: index [%d]"
629 ha->host_no, __func__,
630 ddb_entry->fw_ddb_index));
632 atomic_dec(&ddb_entry->
633 retry_relogin_timer);
637 /* Wait for relogin to timeout */
638 if (atomic_read(&ddb_entry->relogin_timer) &&
639 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
641 * If the relogin times out and the device is
642 * still NOT ONLINE then try and relogin again.
644 if (atomic_read(&ddb_entry->state) !=
646 ddb_entry->fw_ddb_device_state ==
647 DDB_DS_SESSION_FAILED) {
648 /* Reset retry relogin timer */
649 atomic_inc(&ddb_entry->relogin_retry_count);
650 DEBUG2(printk("scsi%ld: index[%d] relogin"
651 " timed out-retrying"
654 ddb_entry->fw_ddb_index,
655 atomic_read(&ddb_entry->
656 relogin_retry_count))
659 DEBUG(printk("scsi%ld:%d:%d: index [%d] "
660 "initate relogin after"
662 ha->host_no, ddb_entry->bus,
664 ddb_entry->fw_ddb_index,
665 ddb_entry->default_time2wait + 4)
668 atomic_set(&ddb_entry->retry_relogin_timer,
669 ddb_entry->default_time2wait + 4);
674 /* Check for heartbeat interval. */
675 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
676 ha->heartbeat_interval != 0) {
677 ha->seconds_since_last_heartbeat++;
678 if (ha->seconds_since_last_heartbeat >
679 ha->heartbeat_interval + 2)
680 set_bit(DPC_RESET_HA, &ha->dpc_flags);
684 /* Wakeup the dpc routine for this adapter, if needed. */
686 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
687 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
688 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
689 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
690 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
691 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
692 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
693 test_bit(DPC_AEN, &ha->dpc_flags)) &&
695 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
696 " - dpc flags = 0x%lx\n",
697 ha->host_no, __func__, ha->dpc_flags));
698 queue_work(ha->dpc_thread, &ha->dpc_work);
701 /* Reschedule timer thread to call us back in one second */
702 mod_timer(&ha->timer, jiffies + HZ);
704 DEBUG2(ha->seconds_since_last_intr++);
708 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
709 * @ha: Pointer to host adapter structure.
711 * This routine stalls the driver until all outstanding commands are returned.
712 * Caller must release the Hardware Lock prior to calling this routine.
714 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
717 int stat = QLA_SUCCESS;
719 struct scsi_cmnd *cmd;
720 int wait_cnt = WAIT_CMD_TOV; /*
721 * Initialized for 30 seconds as we
722 * expect all commands to retuned
727 spin_lock_irqsave(&ha->hardware_lock, flags);
728 /* Find a command that hasn't completed. */
729 for (index = 0; index < ha->host->can_queue; index++) {
730 cmd = scsi_host_find_tag(ha->host, index);
734 spin_unlock_irqrestore(&ha->hardware_lock, flags);
736 /* If No Commands are pending, wait is complete */
737 if (index == ha->host->can_queue) {
741 /* If we timed out on waiting for commands to come back
750 } /* End of While (wait_cnt) */
755 void qla4xxx_hw_reset(struct scsi_qla_host *ha)
757 uint32_t ctrl_status;
758 unsigned long flags = 0;
760 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
762 spin_lock_irqsave(&ha->hardware_lock, flags);
765 * If the SCSI Reset Interrupt bit is set, clear it.
766 * Otherwise, the Soft Reset won't work.
768 ctrl_status = readw(&ha->reg->ctrl_status);
769 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
770 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
772 /* Issue Soft Reset */
773 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
774 readl(&ha->reg->ctrl_status);
776 spin_unlock_irqrestore(&ha->hardware_lock, flags);
780 * qla4xxx_soft_reset - performs soft reset.
781 * @ha: Pointer to host adapter structure.
783 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
785 uint32_t max_wait_time;
786 unsigned long flags = 0;
787 int status = QLA_ERROR;
788 uint32_t ctrl_status;
790 qla4xxx_hw_reset(ha);
792 /* Wait until the Network Reset Intr bit is cleared */
793 max_wait_time = RESET_INTR_TOV;
795 spin_lock_irqsave(&ha->hardware_lock, flags);
796 ctrl_status = readw(&ha->reg->ctrl_status);
797 spin_unlock_irqrestore(&ha->hardware_lock, flags);
799 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
803 } while ((--max_wait_time));
805 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
806 DEBUG2(printk(KERN_WARNING
807 "scsi%ld: Network Reset Intr not cleared by "
808 "Network function, clearing it now!\n",
810 spin_lock_irqsave(&ha->hardware_lock, flags);
811 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
812 readl(&ha->reg->ctrl_status);
813 spin_unlock_irqrestore(&ha->hardware_lock, flags);
816 /* Wait until the firmware tells us the Soft Reset is done */
817 max_wait_time = SOFT_RESET_TOV;
819 spin_lock_irqsave(&ha->hardware_lock, flags);
820 ctrl_status = readw(&ha->reg->ctrl_status);
821 spin_unlock_irqrestore(&ha->hardware_lock, flags);
823 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
824 status = QLA_SUCCESS;
829 } while ((--max_wait_time));
832 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
833 * after the soft reset has taken place.
835 spin_lock_irqsave(&ha->hardware_lock, flags);
836 ctrl_status = readw(&ha->reg->ctrl_status);
837 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
838 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
839 readl(&ha->reg->ctrl_status);
841 spin_unlock_irqrestore(&ha->hardware_lock, flags);
843 /* If soft reset fails then most probably the bios on other
844 * function is also enabled.
845 * Since the initialization is sequential the other fn
846 * wont be able to acknowledge the soft reset.
847 * Issue a force soft reset to workaround this scenario.
849 if (max_wait_time == 0) {
850 /* Issue Force Soft Reset */
851 spin_lock_irqsave(&ha->hardware_lock, flags);
852 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
853 readl(&ha->reg->ctrl_status);
854 spin_unlock_irqrestore(&ha->hardware_lock, flags);
855 /* Wait until the firmware tells us the Soft Reset is done */
856 max_wait_time = SOFT_RESET_TOV;
858 spin_lock_irqsave(&ha->hardware_lock, flags);
859 ctrl_status = readw(&ha->reg->ctrl_status);
860 spin_unlock_irqrestore(&ha->hardware_lock, flags);
862 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
863 status = QLA_SUCCESS;
868 } while ((--max_wait_time));
875 * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
876 * @ha: Pointer to host adapter structure.
878 * This routine is called just prior to a HARD RESET to return all
879 * outstanding commands back to the Operating System.
880 * Caller should make sure that the following locks are released
881 * before this calling routine: Hardware lock, and io_request_lock.
883 static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
889 spin_lock_irqsave(&ha->hardware_lock, flags);
890 for (i = 0; i < ha->host->can_queue; i++) {
891 srb = qla4xxx_del_from_active_array(ha, i);
893 srb->cmd->result = DID_RESET << 16;
894 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
897 spin_unlock_irqrestore(&ha->hardware_lock, flags);
901 * qla4xxx_recover_adapter - recovers adapter after a fatal error
902 * @ha: Pointer to host adapter structure.
903 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
905 * renew_ddb_list value can be 0=preserve ddb list, 1=destroy and rebuild
908 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
909 uint8_t renew_ddb_list)
913 /* Stall incoming I/O until we are done */
914 clear_bit(AF_ONLINE, &ha->flags);
916 DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
919 /* Wait for outstanding commands to complete.
920 * Stalls the driver for max 30 secs
922 status = qla4xxx_cmd_wait(ha);
924 qla4xxx_disable_intrs(ha);
926 /* Flush any pending ddb changed AENs */
927 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
929 qla4xxx_flush_active_srbs(ha);
931 /* Reset the firmware. If successful, function
932 * returns with ISP interrupts enabled.
934 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
935 ha->host_no, __func__));
936 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
937 status = qla4xxx_soft_reset(ha);
941 /* Flush any pending ddb changed AENs */
942 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
944 /* Re-initialize firmware. If successful, function returns
945 * with ISP interrupts enabled */
946 if (status == QLA_SUCCESS) {
947 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
948 ha->host_no, __func__));
950 /* If successful, AF_ONLINE flag set in
951 * qla4xxx_initialize_adapter */
952 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
955 /* Failed adapter initialization?
956 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
957 if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
958 (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
959 /* Adapter initialization failed, see if we can retry
960 * resetting the ha */
961 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
962 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
963 DEBUG2(printk("scsi%ld: recover adapter - retrying "
964 "(%d) more times\n", ha->host_no,
965 ha->retry_reset_ha_cnt));
966 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
969 if (ha->retry_reset_ha_cnt > 0) {
970 /* Schedule another Reset HA--DPC will retry */
971 ha->retry_reset_ha_cnt--;
972 DEBUG2(printk("scsi%ld: recover adapter - "
973 "retry remaining %d\n",
975 ha->retry_reset_ha_cnt));
979 if (ha->retry_reset_ha_cnt == 0) {
980 /* Recover adapter retries have been exhausted.
982 DEBUG2(printk("scsi%ld: recover adapter "
983 "failed - board disabled\n",
985 qla4xxx_flush_active_srbs(ha);
986 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
987 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
988 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
994 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
995 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
996 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
999 ha->adapter_error_count++;
1001 if (status == QLA_SUCCESS)
1002 qla4xxx_enable_intrs(ha);
1004 DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
1009 * qla4xxx_do_dpc - dpc routine
1010 * @data: in our case pointer to adapter structure
1012 * This routine is a task that is schedule by the interrupt handler
1013 * to perform the background processing for interrupts. We put it
1014 * on a task queue that is consumed whenever the scheduler runs; that's
1015 * so you can do anything (i.e. put the process to sleep etc). In fact,
1016 * the mid-level tries to sleep when it reaches the driver threshold
1017 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1019 static void qla4xxx_do_dpc(struct work_struct *work)
1021 struct scsi_qla_host *ha =
1022 container_of(work, struct scsi_qla_host, dpc_work);
1023 struct ddb_entry *ddb_entry, *dtemp;
1024 int status = QLA_ERROR;
1026 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1027 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
1028 ha->host_no, __func__, ha->flags, ha->dpc_flags,
1029 readw(&ha->reg->ctrl_status)));
1031 /* Initialization not yet finished. Don't do anything yet. */
1032 if (!test_bit(AF_INIT_DONE, &ha->flags))
1035 if (adapter_up(ha) ||
1036 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1037 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1038 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
1039 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1040 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1041 qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1043 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1044 uint8_t wait_time = RESET_INTR_TOV;
1046 while ((readw(&ha->reg->ctrl_status) &
1047 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1048 if (--wait_time == 0)
1053 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1054 "bit not cleared-- resetting\n",
1055 ha->host_no, __func__));
1056 qla4xxx_flush_active_srbs(ha);
1057 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1058 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1059 status = qla4xxx_initialize_adapter(ha,
1062 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1063 if (status == QLA_SUCCESS)
1064 qla4xxx_enable_intrs(ha);
1068 /* ---- process AEN? --- */
1069 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1070 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1072 /* ---- Get DHCP IP Address? --- */
1073 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1074 qla4xxx_get_dhcp_ip_address(ha);
1076 /* ---- link change? --- */
1077 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1078 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1079 /* ---- link down? --- */
1080 list_for_each_entry_safe(ddb_entry, dtemp,
1081 &ha->ddb_list, list) {
1082 if (atomic_read(&ddb_entry->state) ==
1084 qla4xxx_mark_device_missing(ha,
1088 /* ---- link up? --- *
1089 * F/W will auto login to all devices ONLY ONCE after
1090 * link up during driver initialization and runtime
1091 * fatal error recovery. Therefore, the driver must
1092 * manually relogin to devices when recovering from
1093 * connection failures, logouts, expired KATO, etc. */
1095 list_for_each_entry_safe(ddb_entry, dtemp,
1096 &ha->ddb_list, list) {
1097 if ((atomic_read(&ddb_entry->state) ==
1098 DDB_STATE_MISSING) ||
1099 (atomic_read(&ddb_entry->state) ==
1101 if (ddb_entry->fw_ddb_device_state ==
1102 DDB_DS_SESSION_ACTIVE) {
1103 atomic_set(&ddb_entry->state,
1105 dev_info(&ha->pdev->dev,
1106 "scsi%ld: %s: ddb[%d]"
1109 ha->host_no, __func__,
1110 ddb_entry->fw_ddb_index,
1111 ddb_entry->os_target_id);
1113 iscsi_unblock_session(
1116 qla4xxx_relogin_device(
1124 /* ---- relogin device? --- */
1125 if (adapter_up(ha) &&
1126 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1127 list_for_each_entry_safe(ddb_entry, dtemp,
1128 &ha->ddb_list, list) {
1129 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1130 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1131 qla4xxx_relogin_device(ha, ddb_entry);
1134 * If mbx cmd times out there is no point
1135 * in continuing further.
1136 * With large no of targets this can hang
1139 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1140 printk(KERN_WARNING "scsi%ld: %s: "
1141 "need to reset hba\n",
1142 ha->host_no, __func__);
1150 * qla4xxx_free_adapter - release the adapter
1151 * @ha: pointer to adapter structure
1153 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1156 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1157 /* Turn-off interrupts on the card. */
1158 qla4xxx_disable_intrs(ha);
1161 /* Kill the kernel thread for this host */
1163 destroy_workqueue(ha->dpc_thread);
1165 /* Issue Soft Reset to put firmware in unknown state */
1166 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
1167 qla4xxx_hw_reset(ha);
1169 /* Remove timer thread, if present */
1170 if (ha->timer_active)
1171 qla4xxx_stop_timer(ha);
1173 /* Detach interrupts */
1174 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1175 free_irq(ha->pdev->irq, ha);
1177 /* free extra memory */
1178 qla4xxx_mem_free(ha);
1180 pci_disable_device(ha->pdev);
1185 * qla4xxx_iospace_config - maps registers
1186 * @ha: pointer to adapter structure
1188 * This routines maps HBA's registers from the pci address space
1189 * into the kernel virtual address space for memory mapped i/o.
1191 static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1193 unsigned long pio, pio_len, pio_flags;
1194 unsigned long mmio, mmio_len, mmio_flags;
1196 pio = pci_resource_start(ha->pdev, 0);
1197 pio_len = pci_resource_len(ha->pdev, 0);
1198 pio_flags = pci_resource_flags(ha->pdev, 0);
1199 if (pio_flags & IORESOURCE_IO) {
1200 if (pio_len < MIN_IOBASE_LEN) {
1201 dev_warn(&ha->pdev->dev,
1202 "Invalid PCI I/O region size\n");
1206 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1210 /* Use MMIO operations for all accesses. */
1211 mmio = pci_resource_start(ha->pdev, 1);
1212 mmio_len = pci_resource_len(ha->pdev, 1);
1213 mmio_flags = pci_resource_flags(ha->pdev, 1);
1215 if (!(mmio_flags & IORESOURCE_MEM)) {
1216 dev_err(&ha->pdev->dev,
1217 "region #0 not an MMIO resource, aborting\n");
1219 goto iospace_error_exit;
1221 if (mmio_len < MIN_IOBASE_LEN) {
1222 dev_err(&ha->pdev->dev,
1223 "Invalid PCI mem region size, aborting\n");
1224 goto iospace_error_exit;
1227 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1228 dev_warn(&ha->pdev->dev,
1229 "Failed to reserve PIO/MMIO regions\n");
1231 goto iospace_error_exit;
1234 ha->pio_address = pio;
1235 ha->pio_length = pio_len;
1236 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1238 dev_err(&ha->pdev->dev,
1239 "cannot remap MMIO, aborting\n");
1241 goto iospace_error_exit;
1251 * qla4xxx_probe_adapter - callback function to probe HBA
1252 * @pdev: pointer to pci_dev structure
1253 * @pci_device_id: pointer to pci_device entry
1255 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1256 * It returns zero if successful. It also initializes all data necessary for
1259 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1260 const struct pci_device_id *ent)
1262 int ret = -ENODEV, status;
1263 struct Scsi_Host *host;
1264 struct scsi_qla_host *ha;
1265 uint8_t init_retry_count = 0;
1268 if (pci_enable_device(pdev))
1271 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1274 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1275 goto probe_disable_device;
1278 /* Clear our data area */
1279 ha = (struct scsi_qla_host *) host->hostdata;
1280 memset(ha, 0, sizeof(*ha));
1282 /* Save the information from PCI BIOS. */
1285 ha->host_no = host->host_no;
1287 /* Configure PCI I/O space. */
1288 ret = qla4xxx_iospace_config(ha);
1292 dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1293 pdev->device, pdev->irq, ha->reg);
1295 qla4xxx_config_dma_addressing(ha);
1297 /* Initialize lists and spinlocks. */
1298 INIT_LIST_HEAD(&ha->ddb_list);
1299 INIT_LIST_HEAD(&ha->free_srb_q);
1301 mutex_init(&ha->mbox_sem);
1303 spin_lock_init(&ha->hardware_lock);
1305 /* Allocate dma buffers */
1306 if (qla4xxx_mem_alloc(ha)) {
1307 dev_warn(&ha->pdev->dev,
1308 "[ERROR] Failed to allocate memory for adapter\n");
1315 * Initialize the Host adapter request/response queues and
1317 * NOTE: interrupts enabled upon successful completion
1319 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1320 while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1321 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1322 "(%d)\n", __func__, init_retry_count));
1323 qla4xxx_soft_reset(ha);
1324 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1326 if (status == QLA_ERROR) {
1327 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1333 host->cmd_per_lun = 3;
1334 host->max_channel = 0;
1335 host->max_lun = MAX_LUNS - 1;
1336 host->max_id = MAX_TARGETS;
1337 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1338 host->can_queue = MAX_SRBS ;
1339 host->transportt = qla4xxx_scsi_transport;
1341 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1343 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed\n");
1347 /* Startup the kernel thread for this host adapter. */
1348 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1349 "qla4xxx_dpc\n", __func__));
1350 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1351 ha->dpc_thread = create_singlethread_workqueue(buf);
1352 if (!ha->dpc_thread) {
1353 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1357 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1359 ret = request_irq(pdev->irq, qla4xxx_intr_handler,
1360 IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
1362 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1363 " already in use.\n", pdev->irq);
1366 set_bit(AF_IRQ_ATTACHED, &ha->flags);
1367 host->irq = pdev->irq;
1368 DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1370 qla4xxx_enable_intrs(ha);
1372 /* Start timer thread. */
1373 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1375 set_bit(AF_INIT_DONE, &ha->flags);
1377 pci_set_drvdata(pdev, ha);
1379 ret = scsi_add_host(host, &pdev->dev);
1384 " QLogic iSCSI HBA Driver version: %s\n"
1385 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1386 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1387 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1388 ha->patch_number, ha->build_number);
1389 scsi_scan_host(host);
1393 qla4xxx_free_adapter(ha);
1394 scsi_host_put(ha->host);
1396 probe_disable_device:
1397 pci_disable_device(pdev);
1403 * qla4xxx_remove_adapter - calback function to remove adapter.
1404 * @pci_dev: PCI device pointer
1406 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1408 struct scsi_qla_host *ha;
1410 ha = pci_get_drvdata(pdev);
1412 qla4xxx_disable_intrs(ha);
1414 while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1417 /* remove devs from iscsi_sessions to scsi_devices */
1418 qla4xxx_free_ddb_list(ha);
1420 scsi_remove_host(ha->host);
1422 qla4xxx_free_adapter(ha);
1424 scsi_host_put(ha->host);
1426 pci_set_drvdata(pdev, NULL);
1430 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1433 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1434 * supported addressing method.
1436 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1440 /* Update our PCI device dma_mask for full 64 bit mask */
1441 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1442 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1443 dev_dbg(&ha->pdev->dev,
1444 "Failed to set 64 bit PCI consistent mask; "
1446 retval = pci_set_consistent_dma_mask(ha->pdev,
1450 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1453 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1455 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1456 struct ddb_entry *ddb = sess->dd_data;
1458 sdev->hostdata = ddb;
1459 sdev->tagged_supported = 1;
1460 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
1464 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1466 sdev->tagged_supported = 1;
1470 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1472 scsi_deactivate_tcq(sdev, 1);
1476 * qla4xxx_del_from_active_array - returns an active srb
1477 * @ha: Pointer to host adapter structure.
1478 * @index: index into the active_array
1480 * This routine removes and returns the srb at the specified index
1482 struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1484 struct srb *srb = NULL;
1485 struct scsi_cmnd *cmd = NULL;
1487 cmd = scsi_host_find_tag(ha->host, index);
1491 srb = (struct srb *)CMD_SP(cmd);
1495 /* update counters */
1496 if (srb->flags & SRB_DMA_VALID) {
1497 ha->req_q_count += srb->iocb_cnt;
1498 ha->iocb_cnt -= srb->iocb_cnt;
1500 srb->cmd->host_scribble =
1501 (unsigned char *)(unsigned long) MAX_SRBS;
1507 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1508 * @ha: Pointer to host adapter structure.
1509 * @cmd: Scsi Command to wait on.
1511 * This routine waits for the command to be returned by the Firmware
1512 * for some max time.
1514 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1515 struct scsi_cmnd *cmd)
1519 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1522 /* Checking to see if its returned to OS */
1523 rp = (struct srb *) CMD_SP(cmd);
1530 } while (max_wait_time--);
1536 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1537 * @ha: Pointer to host adapter structure
1539 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1541 unsigned long wait_online;
1543 wait_online = jiffies + (30 * HZ);
1544 while (time_before(jiffies, wait_online)) {
1548 else if (ha->retry_reset_ha_cnt == 0)
1558 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
1559 * @ha: pointer to HBA
1563 * This function waits for all outstanding commands to a lun to complete. It
1564 * returns 0 if all pending commands are returned and 1 otherwise.
1566 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1567 struct scsi_target *stgt,
1568 struct scsi_device *sdev)
1572 struct scsi_cmnd *cmd;
1575 * Waiting for all commands for the designated target or dev
1576 * in the active array
1578 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1579 cmd = scsi_host_find_tag(ha->host, cnt);
1580 if (cmd && stgt == scsi_target(cmd->device) &&
1581 (!sdev || sdev == cmd->device)) {
1582 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1592 * qla4xxx_eh_abort - callback for abort task.
1593 * @cmd: Pointer to Linux's SCSI command structure
1595 * This routine is called by the Linux OS to abort the specified
1598 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
1600 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1601 unsigned int id = cmd->device->id;
1602 unsigned int lun = cmd->device->lun;
1603 unsigned long serial = cmd->serial_number;
1604 struct srb *srb = NULL;
1608 dev_info(&ha->pdev->dev,
1609 "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
1610 ha->host_no, id, lun, cmd, serial);
1612 srb = (struct srb *) CMD_SP(cmd);
1617 kref_get(&srb->srb_ref);
1619 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
1620 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
1621 ha->host_no, id, lun));
1624 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
1625 ha->host_no, id, lun));
1629 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1631 /* Wait for command to complete */
1633 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1634 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
1635 ha->host_no, id, lun));
1640 dev_info(&ha->pdev->dev,
1641 "scsi%ld:%d:%d: Abort command - %s\n",
1642 ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
1648 * qla4xxx_eh_device_reset - callback for target reset.
1649 * @cmd: Pointer to Linux's SCSI command structure
1651 * This routine is called by the Linux OS to reset all luns on the
1654 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1656 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1657 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1658 int ret = FAILED, stat;
1663 dev_info(&ha->pdev->dev,
1664 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1665 cmd->device->channel, cmd->device->id, cmd->device->lun);
1667 DEBUG2(printk(KERN_INFO
1668 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1669 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
1670 cmd, jiffies, cmd->request->timeout / HZ,
1671 ha->dpc_flags, cmd->result, cmd->allowed));
1673 /* FIXME: wait for hba to go online */
1674 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1675 if (stat != QLA_SUCCESS) {
1676 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1677 goto eh_dev_reset_done;
1680 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1682 dev_info(&ha->pdev->dev,
1683 "DEVICE RESET FAILED - waiting for "
1685 goto eh_dev_reset_done;
1689 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1690 MM_LUN_RESET) != QLA_SUCCESS)
1691 goto eh_dev_reset_done;
1693 dev_info(&ha->pdev->dev,
1694 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1695 ha->host_no, cmd->device->channel, cmd->device->id,
1706 * qla4xxx_eh_target_reset - callback for target reset.
1707 * @cmd: Pointer to Linux's SCSI command structure
1709 * This routine is called by the Linux OS to reset the target.
1711 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
1713 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1714 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1720 starget_printk(KERN_INFO, scsi_target(cmd->device),
1721 "WARM TARGET RESET ISSUED.\n");
1723 DEBUG2(printk(KERN_INFO
1724 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
1725 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
1726 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
1727 ha->dpc_flags, cmd->result, cmd->allowed));
1729 stat = qla4xxx_reset_target(ha, ddb_entry);
1730 if (stat != QLA_SUCCESS) {
1731 starget_printk(KERN_INFO, scsi_target(cmd->device),
1732 "WARM TARGET RESET FAILED.\n");
1736 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1738 starget_printk(KERN_INFO, scsi_target(cmd->device),
1739 "WARM TARGET DEVICE RESET FAILED - "
1740 "waiting for commands.\n");
1745 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1746 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
1747 starget_printk(KERN_INFO, scsi_target(cmd->device),
1748 "WARM TARGET DEVICE RESET FAILED - "
1749 "marker iocb failed.\n");
1753 starget_printk(KERN_INFO, scsi_target(cmd->device),
1754 "WARM TARGET RESET SUCCEEDED.\n");
1759 * qla4xxx_eh_host_reset - kernel callback
1760 * @cmd: Pointer to Linux's SCSI command structure
1762 * This routine is invoked by the Linux kernel to perform fatal error
1763 * recovery on the specified adapter.
1765 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1767 int return_status = FAILED;
1768 struct scsi_qla_host *ha;
1770 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1772 dev_info(&ha->pdev->dev,
1773 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
1774 cmd->device->channel, cmd->device->id, cmd->device->lun);
1776 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1777 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
1778 "DEAD.\n", ha->host_no, cmd->device->channel,
1784 /* make sure the dpc thread is stopped while we reset the hba */
1785 clear_bit(AF_ONLINE, &ha->flags);
1786 flush_workqueue(ha->dpc_thread);
1788 if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS)
1789 return_status = SUCCESS;
1791 dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1792 return_status == FAILED ? "FAILED" : "SUCCEDED");
1794 return return_status;
1798 static struct pci_device_id qla4xxx_pci_tbl[] = {
1800 .vendor = PCI_VENDOR_ID_QLOGIC,
1801 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
1802 .subvendor = PCI_ANY_ID,
1803 .subdevice = PCI_ANY_ID,
1806 .vendor = PCI_VENDOR_ID_QLOGIC,
1807 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
1808 .subvendor = PCI_ANY_ID,
1809 .subdevice = PCI_ANY_ID,
1812 .vendor = PCI_VENDOR_ID_QLOGIC,
1813 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
1814 .subvendor = PCI_ANY_ID,
1815 .subdevice = PCI_ANY_ID,
1819 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1821 static struct pci_driver qla4xxx_pci_driver = {
1822 .name = DRIVER_NAME,
1823 .id_table = qla4xxx_pci_tbl,
1824 .probe = qla4xxx_probe_adapter,
1825 .remove = qla4xxx_remove_adapter,
1828 static int __init qla4xxx_module_init(void)
1832 /* Allocate cache for SRBs. */
1833 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
1834 SLAB_HWCACHE_ALIGN, NULL);
1835 if (srb_cachep == NULL) {
1837 "%s: Unable to allocate SRB cache..."
1838 "Failing load!\n", DRIVER_NAME);
1843 /* Derive version string. */
1844 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
1845 if (ql4xextended_error_logging)
1846 strcat(qla4xxx_version_str, "-debug");
1848 qla4xxx_scsi_transport =
1849 iscsi_register_transport(&qla4xxx_iscsi_transport);
1850 if (!qla4xxx_scsi_transport){
1852 goto release_srb_cache;
1855 ret = pci_register_driver(&qla4xxx_pci_driver);
1857 goto unregister_transport;
1859 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1862 unregister_transport:
1863 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1865 kmem_cache_destroy(srb_cachep);
1870 static void __exit qla4xxx_module_exit(void)
1873 pci_unregister_driver(&qla4xxx_pci_driver);
1874 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1875 kmem_cache_destroy(srb_cachep);
1878 module_init(qla4xxx_module_init);
1879 module_exit(qla4xxx_module_exit);
1881 MODULE_AUTHOR("QLogic Corporation");
1882 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1883 MODULE_LICENSE("GPL");
1884 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);