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_device_reset(struct scsi_cmnd *cmd);
78 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
79 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
80 static int qla4xxx_slave_alloc(struct scsi_device *device);
81 static int qla4xxx_slave_configure(struct scsi_device *device);
82 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
83 static void qla4xxx_scan_start(struct Scsi_Host *shost);
85 static struct scsi_host_template qla4xxx_driver_template = {
86 .module = THIS_MODULE,
88 .proc_name = DRIVER_NAME,
89 .queuecommand = qla4xxx_queuecommand,
91 .eh_device_reset_handler = qla4xxx_eh_device_reset,
92 .eh_target_reset_handler = qla4xxx_eh_target_reset,
93 .eh_host_reset_handler = qla4xxx_eh_host_reset,
94 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
96 .slave_configure = qla4xxx_slave_configure,
97 .slave_alloc = qla4xxx_slave_alloc,
98 .slave_destroy = qla4xxx_slave_destroy,
100 .scan_finished = iscsi_scan_finished,
101 .scan_start = qla4xxx_scan_start,
105 .use_clustering = ENABLE_CLUSTERING,
106 .sg_tablesize = SG_ALL,
108 .max_sectors = 0xFFFF,
111 static struct iscsi_transport qla4xxx_iscsi_transport = {
112 .owner = THIS_MODULE,
114 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
115 CAP_DATA_PATH_OFFLOAD,
116 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
117 ISCSI_TARGET_NAME | ISCSI_TPGT,
118 .host_param_mask = ISCSI_HOST_HWADDRESS |
119 ISCSI_HOST_IPADDRESS |
120 ISCSI_HOST_INITIATOR_NAME,
121 .tgt_dscvr = qla4xxx_tgt_dscvr,
122 .get_conn_param = qla4xxx_conn_get_param,
123 .get_session_param = qla4xxx_sess_get_param,
124 .get_host_param = qla4xxx_host_get_param,
125 .session_recovery_timedout = qla4xxx_recovery_timedout,
128 static struct scsi_transport_template *qla4xxx_scsi_transport;
130 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
132 struct iscsi_cls_session *session;
133 struct ddb_entry *ddb_entry;
135 session = starget_to_session(scsi_target(sc->device));
136 ddb_entry = session->dd_data;
138 /* if we are not logged in then the LLD is going to clean up the cmd */
139 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
140 return BLK_EH_RESET_TIMER;
142 return BLK_EH_NOT_HANDLED;
145 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
147 struct ddb_entry *ddb_entry = session->dd_data;
148 struct scsi_qla_host *ha = ddb_entry->ha;
150 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
151 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
153 DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count "
154 "of (%d) secs exhausted, marking device DEAD.\n",
155 ha->host_no, __func__, ddb_entry->fw_ddb_index,
156 ha->port_down_retry_count));
158 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc "
160 ha->host_no, __func__, ha->dpc_flags));
161 queue_work(ha->dpc_thread, &ha->dpc_work);
165 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
166 enum iscsi_host_param param, char *buf)
168 struct scsi_qla_host *ha = to_qla_host(shost);
172 case ISCSI_HOST_PARAM_HWADDRESS:
173 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
175 case ISCSI_HOST_PARAM_IPADDRESS:
176 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
177 ha->ip_address[1], ha->ip_address[2],
180 case ISCSI_HOST_PARAM_INITIATOR_NAME:
181 len = sprintf(buf, "%s\n", ha->name_string);
190 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
191 enum iscsi_param param, char *buf)
193 struct ddb_entry *ddb_entry = sess->dd_data;
197 case ISCSI_PARAM_TARGET_NAME:
198 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
199 ddb_entry->iscsi_name);
201 case ISCSI_PARAM_TPGT:
202 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
211 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
212 enum iscsi_param param, char *buf)
214 struct iscsi_cls_session *session;
215 struct ddb_entry *ddb_entry;
218 session = iscsi_dev_to_session(conn->dev.parent);
219 ddb_entry = session->dd_data;
222 case ISCSI_PARAM_CONN_PORT:
223 len = sprintf(buf, "%hu\n", ddb_entry->port);
225 case ISCSI_PARAM_CONN_ADDRESS:
226 /* TODO: what are the ipv6 bits */
227 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
236 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
237 enum iscsi_tgt_dscvr type, uint32_t enable,
238 struct sockaddr *dst_addr)
240 struct scsi_qla_host *ha;
241 struct sockaddr_in *addr;
242 struct sockaddr_in6 *addr6;
245 ha = (struct scsi_qla_host *) shost->hostdata;
248 case ISCSI_TGT_DSCVR_SEND_TARGETS:
249 if (dst_addr->sa_family == AF_INET) {
250 addr = (struct sockaddr_in *)dst_addr;
251 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
252 addr->sin_port) != QLA_SUCCESS)
254 } else if (dst_addr->sa_family == AF_INET6) {
256 * TODO: fix qla4xxx_send_tgts
258 addr6 = (struct sockaddr_in6 *)dst_addr;
259 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
260 addr6->sin6_port) != QLA_SUCCESS)
271 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
273 if (!ddb_entry->sess)
276 if (ddb_entry->conn) {
277 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
278 iscsi_remove_session(ddb_entry->sess);
280 iscsi_free_session(ddb_entry->sess);
283 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
287 ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
288 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
290 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
294 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
295 if (!ddb_entry->conn) {
296 iscsi_remove_session(ddb_entry->sess);
297 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
301 /* finally ready to go */
302 iscsi_unblock_session(ddb_entry->sess);
306 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
308 struct ddb_entry *ddb_entry;
309 struct iscsi_cls_session *sess;
311 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
312 sizeof(struct ddb_entry));
316 ddb_entry = sess->dd_data;
317 memset(ddb_entry, 0, sizeof(*ddb_entry));
319 ddb_entry->sess = sess;
323 static void qla4xxx_scan_start(struct Scsi_Host *shost)
325 struct scsi_qla_host *ha = shost_priv(shost);
326 struct ddb_entry *ddb_entry, *ddbtemp;
328 /* finish setup of sessions that were already setup in firmware */
329 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
330 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
331 qla4xxx_add_sess(ddb_entry);
339 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
340 unsigned long interval)
342 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
343 __func__, ha->host->host_no));
344 init_timer(&ha->timer);
345 ha->timer.expires = jiffies + interval * HZ;
346 ha->timer.data = (unsigned long)ha;
347 ha->timer.function = (void (*)(unsigned long))func;
348 add_timer(&ha->timer);
349 ha->timer_active = 1;
352 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
354 del_timer_sync(&ha->timer);
355 ha->timer_active = 0;
359 * qla4xxx_mark_device_missing - mark a device as missing.
360 * @ha: Pointer to host adapter structure.
361 * @ddb_entry: Pointer to device database entry
363 * This routine marks a device missing and resets the relogin retry count.
365 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
366 struct ddb_entry *ddb_entry)
368 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
369 DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
370 ha->host_no, ddb_entry->bus, ddb_entry->target,
371 ddb_entry->fw_ddb_index));
372 iscsi_block_session(ddb_entry->sess);
373 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
376 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
377 struct ddb_entry *ddb_entry,
378 struct scsi_cmnd *cmd,
379 void (*done)(struct scsi_cmnd *))
383 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
387 atomic_set(&srb->ref_count, 1);
389 srb->ddb = ddb_entry;
392 cmd->SCp.ptr = (void *)srb;
393 cmd->scsi_done = done;
398 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
400 struct scsi_cmnd *cmd = srb->cmd;
402 if (srb->flags & SRB_DMA_VALID) {
404 srb->flags &= ~SRB_DMA_VALID;
409 void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
411 struct scsi_cmnd *cmd = srb->cmd;
413 qla4xxx_srb_free_dma(ha, srb);
415 mempool_free(srb, ha->srb_mempool);
421 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
422 * @cmd: Pointer to Linux's SCSI command structure
423 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
424 * that the command has been processed.
427 * This routine is invoked by Linux to send a SCSI command to the driver.
428 * The mid-level driver tries to ensure that queuecommand never gets
429 * invoked concurrently with itself or the interrupt handler (although
430 * the interrupt handler may call this routine as part of request-
431 * completion handling). Unfortunely, it sometimes calls the scheduler
432 * in interrupt context which is a big NO! NO!.
434 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
435 void (*done)(struct scsi_cmnd *))
437 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
438 struct ddb_entry *ddb_entry = cmd->device->hostdata;
439 struct iscsi_cls_session *sess = ddb_entry->sess;
444 cmd->result = DID_IMM_RETRY << 16;
445 goto qc_fail_command;
448 rval = iscsi_session_chkready(sess);
451 goto qc_fail_command;
454 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
455 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
456 cmd->result = DID_NO_CONNECT << 16;
457 goto qc_fail_command;
459 return SCSI_MLQUEUE_TARGET_BUSY;
462 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
465 spin_unlock_irq(ha->host->host_lock);
467 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
469 goto qc_host_busy_lock;
471 rval = qla4xxx_send_command_to_isp(ha, srb);
472 if (rval != QLA_SUCCESS)
473 goto qc_host_busy_free_sp;
475 spin_lock_irq(ha->host->host_lock);
478 qc_host_busy_free_sp:
479 qla4xxx_srb_free_dma(ha, srb);
480 mempool_free(srb, ha->srb_mempool);
483 spin_lock_irq(ha->host->host_lock);
486 return SCSI_MLQUEUE_HOST_BUSY;
495 * qla4xxx_mem_free - frees memory allocated to adapter
496 * @ha: Pointer to host adapter structure.
498 * Frees memory previously allocated by qla4xxx_mem_alloc
500 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
503 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
509 ha->request_ring = NULL;
511 ha->response_ring = NULL;
512 ha->response_dma = 0;
513 ha->shadow_regs = NULL;
514 ha->shadow_regs_dma = 0;
518 mempool_destroy(ha->srb_mempool);
520 ha->srb_mempool = NULL;
522 /* release io space registers */
525 pci_release_regions(ha->pdev);
529 * qla4xxx_mem_alloc - allocates memory for use by adapter.
530 * @ha: Pointer to host adapter structure
532 * Allocates DMA memory for request and response queues. Also allocates memory
535 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
539 /* Allocate contiguous block of DMA memory for queues. */
540 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
541 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
542 sizeof(struct shadow_regs) +
544 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
545 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
546 &ha->queues_dma, GFP_KERNEL);
547 if (ha->queues == NULL) {
548 dev_warn(&ha->pdev->dev,
549 "Memory Allocation failed - queues.\n");
551 goto mem_alloc_error_exit;
553 memset(ha->queues, 0, ha->queues_len);
556 * As per RISC alignment requirements -- the bus-address must be a
557 * multiple of the request-ring size (in bytes).
560 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
561 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
562 (MEM_ALIGN_VALUE - 1));
564 /* Update request and response queue pointers. */
565 ha->request_dma = ha->queues_dma + align;
566 ha->request_ring = (struct queue_entry *) (ha->queues + align);
567 ha->response_dma = ha->queues_dma + align +
568 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
569 ha->response_ring = (struct queue_entry *) (ha->queues + align +
570 (REQUEST_QUEUE_DEPTH *
572 ha->shadow_regs_dma = ha->queues_dma + align +
573 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
574 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
575 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
576 (REQUEST_QUEUE_DEPTH *
578 (RESPONSE_QUEUE_DEPTH *
581 /* Allocate memory for srb pool. */
582 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
583 mempool_free_slab, srb_cachep);
584 if (ha->srb_mempool == NULL) {
585 dev_warn(&ha->pdev->dev,
586 "Memory Allocation failed - SRB Pool.\n");
588 goto mem_alloc_error_exit;
593 mem_alloc_error_exit:
594 qla4xxx_mem_free(ha);
599 * qla4xxx_timer - checks every second for work to do.
600 * @ha: Pointer to host adapter structure.
602 static void qla4xxx_timer(struct scsi_qla_host *ha)
604 struct ddb_entry *ddb_entry, *dtemp;
607 /* Search for relogin's to time-out and port down retry. */
608 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
609 /* Count down time between sending relogins */
610 if (adapter_up(ha) &&
611 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
612 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
613 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
615 if (atomic_read(&ddb_entry->retry_relogin_timer)
617 atomic_set(&ddb_entry->
620 set_bit(DPC_RELOGIN_DEVICE,
622 set_bit(DF_RELOGIN, &ddb_entry->flags);
623 DEBUG2(printk("scsi%ld: %s: index [%d]"
625 ha->host_no, __func__,
626 ddb_entry->fw_ddb_index));
628 atomic_dec(&ddb_entry->
629 retry_relogin_timer);
633 /* Wait for relogin to timeout */
634 if (atomic_read(&ddb_entry->relogin_timer) &&
635 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
637 * If the relogin times out and the device is
638 * still NOT ONLINE then try and relogin again.
640 if (atomic_read(&ddb_entry->state) !=
642 ddb_entry->fw_ddb_device_state ==
643 DDB_DS_SESSION_FAILED) {
644 /* Reset retry relogin timer */
645 atomic_inc(&ddb_entry->relogin_retry_count);
646 DEBUG2(printk("scsi%ld: index[%d] relogin"
647 " timed out-retrying"
650 ddb_entry->fw_ddb_index,
651 atomic_read(&ddb_entry->
652 relogin_retry_count))
655 DEBUG(printk("scsi%ld:%d:%d: index [%d] "
656 "initate relogin after"
658 ha->host_no, ddb_entry->bus,
660 ddb_entry->fw_ddb_index,
661 ddb_entry->default_time2wait + 4)
664 atomic_set(&ddb_entry->retry_relogin_timer,
665 ddb_entry->default_time2wait + 4);
670 /* Check for heartbeat interval. */
671 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
672 ha->heartbeat_interval != 0) {
673 ha->seconds_since_last_heartbeat++;
674 if (ha->seconds_since_last_heartbeat >
675 ha->heartbeat_interval + 2)
676 set_bit(DPC_RESET_HA, &ha->dpc_flags);
680 /* Wakeup the dpc routine for this adapter, if needed. */
682 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
683 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
684 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
685 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
686 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
687 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
688 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
689 test_bit(DPC_AEN, &ha->dpc_flags)) &&
691 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
692 " - dpc flags = 0x%lx\n",
693 ha->host_no, __func__, ha->dpc_flags));
694 queue_work(ha->dpc_thread, &ha->dpc_work);
697 /* Reschedule timer thread to call us back in one second */
698 mod_timer(&ha->timer, jiffies + HZ);
700 DEBUG2(ha->seconds_since_last_intr++);
704 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
705 * @ha: Pointer to host adapter structure.
707 * This routine stalls the driver until all outstanding commands are returned.
708 * Caller must release the Hardware Lock prior to calling this routine.
710 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
713 int stat = QLA_SUCCESS;
715 struct scsi_cmnd *cmd;
716 int wait_cnt = WAIT_CMD_TOV; /*
717 * Initialized for 30 seconds as we
718 * expect all commands to retuned
723 spin_lock_irqsave(&ha->hardware_lock, flags);
724 /* Find a command that hasn't completed. */
725 for (index = 0; index < ha->host->can_queue; index++) {
726 cmd = scsi_host_find_tag(ha->host, index);
730 spin_unlock_irqrestore(&ha->hardware_lock, flags);
732 /* If No Commands are pending, wait is complete */
733 if (index == ha->host->can_queue) {
737 /* If we timed out on waiting for commands to come back
746 } /* End of While (wait_cnt) */
751 void qla4xxx_hw_reset(struct scsi_qla_host *ha)
753 uint32_t ctrl_status;
754 unsigned long flags = 0;
756 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
758 spin_lock_irqsave(&ha->hardware_lock, flags);
761 * If the SCSI Reset Interrupt bit is set, clear it.
762 * Otherwise, the Soft Reset won't work.
764 ctrl_status = readw(&ha->reg->ctrl_status);
765 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
766 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
768 /* Issue Soft Reset */
769 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
770 readl(&ha->reg->ctrl_status);
772 spin_unlock_irqrestore(&ha->hardware_lock, flags);
776 * qla4xxx_soft_reset - performs soft reset.
777 * @ha: Pointer to host adapter structure.
779 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
781 uint32_t max_wait_time;
782 unsigned long flags = 0;
783 int status = QLA_ERROR;
784 uint32_t ctrl_status;
786 qla4xxx_hw_reset(ha);
788 /* Wait until the Network Reset Intr bit is cleared */
789 max_wait_time = RESET_INTR_TOV;
791 spin_lock_irqsave(&ha->hardware_lock, flags);
792 ctrl_status = readw(&ha->reg->ctrl_status);
793 spin_unlock_irqrestore(&ha->hardware_lock, flags);
795 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
799 } while ((--max_wait_time));
801 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
802 DEBUG2(printk(KERN_WARNING
803 "scsi%ld: Network Reset Intr not cleared by "
804 "Network function, clearing it now!\n",
806 spin_lock_irqsave(&ha->hardware_lock, flags);
807 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
808 readl(&ha->reg->ctrl_status);
809 spin_unlock_irqrestore(&ha->hardware_lock, flags);
812 /* Wait until the firmware tells us the Soft Reset is done */
813 max_wait_time = SOFT_RESET_TOV;
815 spin_lock_irqsave(&ha->hardware_lock, flags);
816 ctrl_status = readw(&ha->reg->ctrl_status);
817 spin_unlock_irqrestore(&ha->hardware_lock, flags);
819 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
820 status = QLA_SUCCESS;
825 } while ((--max_wait_time));
828 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
829 * after the soft reset has taken place.
831 spin_lock_irqsave(&ha->hardware_lock, flags);
832 ctrl_status = readw(&ha->reg->ctrl_status);
833 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
834 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
835 readl(&ha->reg->ctrl_status);
837 spin_unlock_irqrestore(&ha->hardware_lock, flags);
839 /* If soft reset fails then most probably the bios on other
840 * function is also enabled.
841 * Since the initialization is sequential the other fn
842 * wont be able to acknowledge the soft reset.
843 * Issue a force soft reset to workaround this scenario.
845 if (max_wait_time == 0) {
846 /* Issue Force Soft Reset */
847 spin_lock_irqsave(&ha->hardware_lock, flags);
848 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
849 readl(&ha->reg->ctrl_status);
850 spin_unlock_irqrestore(&ha->hardware_lock, flags);
851 /* Wait until the firmware tells us the Soft Reset is done */
852 max_wait_time = SOFT_RESET_TOV;
854 spin_lock_irqsave(&ha->hardware_lock, flags);
855 ctrl_status = readw(&ha->reg->ctrl_status);
856 spin_unlock_irqrestore(&ha->hardware_lock, flags);
858 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
859 status = QLA_SUCCESS;
864 } while ((--max_wait_time));
871 * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
872 * @ha: Pointer to host adapter structure.
874 * This routine is called just prior to a HARD RESET to return all
875 * outstanding commands back to the Operating System.
876 * Caller should make sure that the following locks are released
877 * before this calling routine: Hardware lock, and io_request_lock.
879 static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
885 spin_lock_irqsave(&ha->hardware_lock, flags);
886 for (i = 0; i < ha->host->can_queue; i++) {
887 srb = qla4xxx_del_from_active_array(ha, i);
889 srb->cmd->result = DID_RESET << 16;
890 qla4xxx_srb_compl(ha, srb);
893 spin_unlock_irqrestore(&ha->hardware_lock, flags);
898 * qla4xxx_recover_adapter - recovers adapter after a fatal error
899 * @ha: Pointer to host adapter structure.
900 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
902 * renew_ddb_list value can be 0=preserve ddb list, 1=destroy and rebuild
905 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
906 uint8_t renew_ddb_list)
910 /* Stall incoming I/O until we are done */
911 clear_bit(AF_ONLINE, &ha->flags);
913 DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
916 /* Wait for outstanding commands to complete.
917 * Stalls the driver for max 30 secs
919 status = qla4xxx_cmd_wait(ha);
921 qla4xxx_disable_intrs(ha);
923 /* Flush any pending ddb changed AENs */
924 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
926 qla4xxx_flush_active_srbs(ha);
928 /* Reset the firmware. If successful, function
929 * returns with ISP interrupts enabled.
931 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
932 ha->host_no, __func__));
933 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
934 status = qla4xxx_soft_reset(ha);
938 /* Flush any pending ddb changed AENs */
939 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
941 /* Re-initialize firmware. If successful, function returns
942 * with ISP interrupts enabled */
943 if (status == QLA_SUCCESS) {
944 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
945 ha->host_no, __func__));
947 /* If successful, AF_ONLINE flag set in
948 * qla4xxx_initialize_adapter */
949 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
952 /* Failed adapter initialization?
953 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
954 if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
955 (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
956 /* Adapter initialization failed, see if we can retry
957 * resetting the ha */
958 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
959 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
960 DEBUG2(printk("scsi%ld: recover adapter - retrying "
961 "(%d) more times\n", ha->host_no,
962 ha->retry_reset_ha_cnt));
963 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
966 if (ha->retry_reset_ha_cnt > 0) {
967 /* Schedule another Reset HA--DPC will retry */
968 ha->retry_reset_ha_cnt--;
969 DEBUG2(printk("scsi%ld: recover adapter - "
970 "retry remaining %d\n",
972 ha->retry_reset_ha_cnt));
976 if (ha->retry_reset_ha_cnt == 0) {
977 /* Recover adapter retries have been exhausted.
979 DEBUG2(printk("scsi%ld: recover adapter "
980 "failed - board disabled\n",
982 qla4xxx_flush_active_srbs(ha);
983 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
984 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
985 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
991 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
992 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
993 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
996 ha->adapter_error_count++;
998 if (status == QLA_SUCCESS)
999 qla4xxx_enable_intrs(ha);
1001 DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
1006 * qla4xxx_do_dpc - dpc routine
1007 * @data: in our case pointer to adapter structure
1009 * This routine is a task that is schedule by the interrupt handler
1010 * to perform the background processing for interrupts. We put it
1011 * on a task queue that is consumed whenever the scheduler runs; that's
1012 * so you can do anything (i.e. put the process to sleep etc). In fact,
1013 * the mid-level tries to sleep when it reaches the driver threshold
1014 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1016 static void qla4xxx_do_dpc(struct work_struct *work)
1018 struct scsi_qla_host *ha =
1019 container_of(work, struct scsi_qla_host, dpc_work);
1020 struct ddb_entry *ddb_entry, *dtemp;
1021 int status = QLA_ERROR;
1023 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1024 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
1025 ha->host_no, __func__, ha->flags, ha->dpc_flags,
1026 readw(&ha->reg->ctrl_status)));
1028 /* Initialization not yet finished. Don't do anything yet. */
1029 if (!test_bit(AF_INIT_DONE, &ha->flags))
1032 if (adapter_up(ha) ||
1033 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1034 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1035 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
1036 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1037 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1038 qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1040 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1041 uint8_t wait_time = RESET_INTR_TOV;
1043 while ((readw(&ha->reg->ctrl_status) &
1044 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1045 if (--wait_time == 0)
1050 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1051 "bit not cleared-- resetting\n",
1052 ha->host_no, __func__));
1053 qla4xxx_flush_active_srbs(ha);
1054 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1055 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1056 status = qla4xxx_initialize_adapter(ha,
1059 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1060 if (status == QLA_SUCCESS)
1061 qla4xxx_enable_intrs(ha);
1065 /* ---- process AEN? --- */
1066 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1067 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1069 /* ---- Get DHCP IP Address? --- */
1070 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1071 qla4xxx_get_dhcp_ip_address(ha);
1073 /* ---- link change? --- */
1074 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1075 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1076 /* ---- link down? --- */
1077 list_for_each_entry_safe(ddb_entry, dtemp,
1078 &ha->ddb_list, list) {
1079 if (atomic_read(&ddb_entry->state) ==
1081 qla4xxx_mark_device_missing(ha,
1085 /* ---- link up? --- *
1086 * F/W will auto login to all devices ONLY ONCE after
1087 * link up during driver initialization and runtime
1088 * fatal error recovery. Therefore, the driver must
1089 * manually relogin to devices when recovering from
1090 * connection failures, logouts, expired KATO, etc. */
1092 list_for_each_entry_safe(ddb_entry, dtemp,
1093 &ha->ddb_list, list) {
1094 if ((atomic_read(&ddb_entry->state) ==
1095 DDB_STATE_MISSING) ||
1096 (atomic_read(&ddb_entry->state) ==
1098 if (ddb_entry->fw_ddb_device_state ==
1099 DDB_DS_SESSION_ACTIVE) {
1100 atomic_set(&ddb_entry->state,
1102 dev_info(&ha->pdev->dev,
1103 "scsi%ld: %s: ddb[%d]"
1106 ha->host_no, __func__,
1107 ddb_entry->fw_ddb_index,
1108 ddb_entry->os_target_id);
1110 iscsi_unblock_session(
1113 qla4xxx_relogin_device(
1121 /* ---- relogin device? --- */
1122 if (adapter_up(ha) &&
1123 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1124 list_for_each_entry_safe(ddb_entry, dtemp,
1125 &ha->ddb_list, list) {
1126 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1127 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1128 qla4xxx_relogin_device(ha, ddb_entry);
1131 * If mbx cmd times out there is no point
1132 * in continuing further.
1133 * With large no of targets this can hang
1136 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1137 printk(KERN_WARNING "scsi%ld: %s: "
1138 "need to reset hba\n",
1139 ha->host_no, __func__);
1147 * qla4xxx_free_adapter - release the adapter
1148 * @ha: pointer to adapter structure
1150 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1153 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1154 /* Turn-off interrupts on the card. */
1155 qla4xxx_disable_intrs(ha);
1158 /* Kill the kernel thread for this host */
1160 destroy_workqueue(ha->dpc_thread);
1162 /* Issue Soft Reset to put firmware in unknown state */
1163 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
1164 qla4xxx_hw_reset(ha);
1166 /* Remove timer thread, if present */
1167 if (ha->timer_active)
1168 qla4xxx_stop_timer(ha);
1170 /* Detach interrupts */
1171 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1172 free_irq(ha->pdev->irq, ha);
1174 /* free extra memory */
1175 qla4xxx_mem_free(ha);
1177 pci_disable_device(ha->pdev);
1182 * qla4xxx_iospace_config - maps registers
1183 * @ha: pointer to adapter structure
1185 * This routines maps HBA's registers from the pci address space
1186 * into the kernel virtual address space for memory mapped i/o.
1188 static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1190 unsigned long pio, pio_len, pio_flags;
1191 unsigned long mmio, mmio_len, mmio_flags;
1193 pio = pci_resource_start(ha->pdev, 0);
1194 pio_len = pci_resource_len(ha->pdev, 0);
1195 pio_flags = pci_resource_flags(ha->pdev, 0);
1196 if (pio_flags & IORESOURCE_IO) {
1197 if (pio_len < MIN_IOBASE_LEN) {
1198 dev_warn(&ha->pdev->dev,
1199 "Invalid PCI I/O region size\n");
1203 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1207 /* Use MMIO operations for all accesses. */
1208 mmio = pci_resource_start(ha->pdev, 1);
1209 mmio_len = pci_resource_len(ha->pdev, 1);
1210 mmio_flags = pci_resource_flags(ha->pdev, 1);
1212 if (!(mmio_flags & IORESOURCE_MEM)) {
1213 dev_err(&ha->pdev->dev,
1214 "region #0 not an MMIO resource, aborting\n");
1216 goto iospace_error_exit;
1218 if (mmio_len < MIN_IOBASE_LEN) {
1219 dev_err(&ha->pdev->dev,
1220 "Invalid PCI mem region size, aborting\n");
1221 goto iospace_error_exit;
1224 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1225 dev_warn(&ha->pdev->dev,
1226 "Failed to reserve PIO/MMIO regions\n");
1228 goto iospace_error_exit;
1231 ha->pio_address = pio;
1232 ha->pio_length = pio_len;
1233 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1235 dev_err(&ha->pdev->dev,
1236 "cannot remap MMIO, aborting\n");
1238 goto iospace_error_exit;
1248 * qla4xxx_probe_adapter - callback function to probe HBA
1249 * @pdev: pointer to pci_dev structure
1250 * @pci_device_id: pointer to pci_device entry
1252 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1253 * It returns zero if successful. It also initializes all data necessary for
1256 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1257 const struct pci_device_id *ent)
1259 int ret = -ENODEV, status;
1260 struct Scsi_Host *host;
1261 struct scsi_qla_host *ha;
1262 uint8_t init_retry_count = 0;
1265 if (pci_enable_device(pdev))
1268 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1271 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1272 goto probe_disable_device;
1275 /* Clear our data area */
1276 ha = (struct scsi_qla_host *) host->hostdata;
1277 memset(ha, 0, sizeof(*ha));
1279 /* Save the information from PCI BIOS. */
1282 ha->host_no = host->host_no;
1284 /* Configure PCI I/O space. */
1285 ret = qla4xxx_iospace_config(ha);
1289 dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1290 pdev->device, pdev->irq, ha->reg);
1292 qla4xxx_config_dma_addressing(ha);
1294 /* Initialize lists and spinlocks. */
1295 INIT_LIST_HEAD(&ha->ddb_list);
1296 INIT_LIST_HEAD(&ha->free_srb_q);
1298 mutex_init(&ha->mbox_sem);
1300 spin_lock_init(&ha->hardware_lock);
1302 /* Allocate dma buffers */
1303 if (qla4xxx_mem_alloc(ha)) {
1304 dev_warn(&ha->pdev->dev,
1305 "[ERROR] Failed to allocate memory for adapter\n");
1312 * Initialize the Host adapter request/response queues and
1314 * NOTE: interrupts enabled upon successful completion
1316 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1317 while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1318 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1319 "(%d)\n", __func__, init_retry_count));
1320 qla4xxx_soft_reset(ha);
1321 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1323 if (status == QLA_ERROR) {
1324 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1330 host->cmd_per_lun = 3;
1331 host->max_channel = 0;
1332 host->max_lun = MAX_LUNS - 1;
1333 host->max_id = MAX_TARGETS;
1334 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1335 host->can_queue = MAX_SRBS ;
1336 host->transportt = qla4xxx_scsi_transport;
1338 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1340 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed\n");
1344 /* Startup the kernel thread for this host adapter. */
1345 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1346 "qla4xxx_dpc\n", __func__));
1347 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1348 ha->dpc_thread = create_singlethread_workqueue(buf);
1349 if (!ha->dpc_thread) {
1350 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1354 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1356 ret = request_irq(pdev->irq, qla4xxx_intr_handler,
1357 IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
1359 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1360 " already in use.\n", pdev->irq);
1363 set_bit(AF_IRQ_ATTACHED, &ha->flags);
1364 host->irq = pdev->irq;
1365 DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1367 qla4xxx_enable_intrs(ha);
1369 /* Start timer thread. */
1370 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1372 set_bit(AF_INIT_DONE, &ha->flags);
1374 pci_set_drvdata(pdev, ha);
1376 ret = scsi_add_host(host, &pdev->dev);
1381 " QLogic iSCSI HBA Driver version: %s\n"
1382 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1383 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1384 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1385 ha->patch_number, ha->build_number);
1386 scsi_scan_host(host);
1390 qla4xxx_free_adapter(ha);
1391 scsi_host_put(ha->host);
1393 probe_disable_device:
1394 pci_disable_device(pdev);
1400 * qla4xxx_remove_adapter - calback function to remove adapter.
1401 * @pci_dev: PCI device pointer
1403 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1405 struct scsi_qla_host *ha;
1407 ha = pci_get_drvdata(pdev);
1409 qla4xxx_disable_intrs(ha);
1411 while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1414 /* remove devs from iscsi_sessions to scsi_devices */
1415 qla4xxx_free_ddb_list(ha);
1417 scsi_remove_host(ha->host);
1419 qla4xxx_free_adapter(ha);
1421 scsi_host_put(ha->host);
1423 pci_set_drvdata(pdev, NULL);
1427 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1430 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1431 * supported addressing method.
1433 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1437 /* Update our PCI device dma_mask for full 64 bit mask */
1438 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1439 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1440 dev_dbg(&ha->pdev->dev,
1441 "Failed to set 64 bit PCI consistent mask; "
1443 retval = pci_set_consistent_dma_mask(ha->pdev,
1447 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1450 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1452 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1453 struct ddb_entry *ddb = sess->dd_data;
1455 sdev->hostdata = ddb;
1456 sdev->tagged_supported = 1;
1457 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
1461 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1463 sdev->tagged_supported = 1;
1467 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1469 scsi_deactivate_tcq(sdev, 1);
1473 * qla4xxx_del_from_active_array - returns an active srb
1474 * @ha: Pointer to host adapter structure.
1475 * @index: index into the active_array
1477 * This routine removes and returns the srb at the specified index
1479 struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1481 struct srb *srb = NULL;
1482 struct scsi_cmnd *cmd;
1484 if (!(cmd = scsi_host_find_tag(ha->host, index)))
1487 if (!(srb = (struct srb *)cmd->host_scribble))
1490 /* update counters */
1491 if (srb->flags & SRB_DMA_VALID) {
1492 ha->req_q_count += srb->iocb_cnt;
1493 ha->iocb_cnt -= srb->iocb_cnt;
1495 srb->cmd->host_scribble = NULL;
1501 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1502 * @ha: actual ha whose done queue will contain the comd returned by firmware.
1503 * @cmd: Scsi Command to wait on.
1505 * This routine waits for the command to be returned by the Firmware
1506 * for some max time.
1508 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1509 struct scsi_cmnd *cmd)
1513 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1516 /* Checking to see if its returned to OS */
1517 rp = (struct srb *) cmd->SCp.ptr;
1524 } while (max_wait_time--);
1530 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1531 * @ha: Pointer to host adapter structure
1533 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1535 unsigned long wait_online;
1537 wait_online = jiffies + (30 * HZ);
1538 while (time_before(jiffies, wait_online)) {
1542 else if (ha->retry_reset_ha_cnt == 0)
1552 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
1553 * @ha: pointer to HBA
1557 * This function waits for all outstanding commands to a lun to complete. It
1558 * returns 0 if all pending commands are returned and 1 otherwise.
1560 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1561 struct scsi_target *stgt,
1562 struct scsi_device *sdev)
1566 struct scsi_cmnd *cmd;
1569 * Waiting for all commands for the designated target or dev
1570 * in the active array
1572 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1573 cmd = scsi_host_find_tag(ha->host, cnt);
1574 if (cmd && stgt == scsi_target(cmd->device) &&
1575 (!sdev || sdev == cmd->device)) {
1576 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1586 * qla4xxx_eh_device_reset - callback for target reset.
1587 * @cmd: Pointer to Linux's SCSI command structure
1589 * This routine is called by the Linux OS to reset all luns on the
1592 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1594 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1595 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1596 int ret = FAILED, stat;
1601 dev_info(&ha->pdev->dev,
1602 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1603 cmd->device->channel, cmd->device->id, cmd->device->lun);
1605 DEBUG2(printk(KERN_INFO
1606 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1607 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
1608 cmd, jiffies, cmd->request->timeout / HZ,
1609 ha->dpc_flags, cmd->result, cmd->allowed));
1611 /* FIXME: wait for hba to go online */
1612 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1613 if (stat != QLA_SUCCESS) {
1614 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1615 goto eh_dev_reset_done;
1618 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1620 dev_info(&ha->pdev->dev,
1621 "DEVICE RESET FAILED - waiting for "
1623 goto eh_dev_reset_done;
1627 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1628 MM_LUN_RESET) != QLA_SUCCESS)
1629 goto eh_dev_reset_done;
1631 dev_info(&ha->pdev->dev,
1632 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1633 ha->host_no, cmd->device->channel, cmd->device->id,
1644 * qla4xxx_eh_target_reset - callback for target reset.
1645 * @cmd: Pointer to Linux's SCSI command structure
1647 * This routine is called by the Linux OS to reset the target.
1649 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
1651 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1652 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1658 starget_printk(KERN_INFO, scsi_target(cmd->device),
1659 "WARM TARGET RESET ISSUED.\n");
1661 DEBUG2(printk(KERN_INFO
1662 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
1663 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
1664 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
1665 ha->dpc_flags, cmd->result, cmd->allowed));
1667 stat = qla4xxx_reset_target(ha, ddb_entry);
1668 if (stat != QLA_SUCCESS) {
1669 starget_printk(KERN_INFO, scsi_target(cmd->device),
1670 "WARM TARGET RESET FAILED.\n");
1674 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1676 starget_printk(KERN_INFO, scsi_target(cmd->device),
1677 "WARM TARGET DEVICE RESET FAILED - "
1678 "waiting for commands.\n");
1683 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1684 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
1685 starget_printk(KERN_INFO, scsi_target(cmd->device),
1686 "WARM TARGET DEVICE RESET FAILED - "
1687 "marker iocb failed.\n");
1691 starget_printk(KERN_INFO, scsi_target(cmd->device),
1692 "WARM TARGET RESET SUCCEEDED.\n");
1697 * qla4xxx_eh_host_reset - kernel callback
1698 * @cmd: Pointer to Linux's SCSI command structure
1700 * This routine is invoked by the Linux kernel to perform fatal error
1701 * recovery on the specified adapter.
1703 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1705 int return_status = FAILED;
1706 struct scsi_qla_host *ha;
1708 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1710 dev_info(&ha->pdev->dev,
1711 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
1712 cmd->device->channel, cmd->device->id, cmd->device->lun);
1714 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1715 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
1716 "DEAD.\n", ha->host_no, cmd->device->channel,
1722 /* make sure the dpc thread is stopped while we reset the hba */
1723 clear_bit(AF_ONLINE, &ha->flags);
1724 flush_workqueue(ha->dpc_thread);
1726 if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS)
1727 return_status = SUCCESS;
1729 dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1730 return_status == FAILED ? "FAILED" : "SUCCEDED");
1732 return return_status;
1736 static struct pci_device_id qla4xxx_pci_tbl[] = {
1738 .vendor = PCI_VENDOR_ID_QLOGIC,
1739 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
1740 .subvendor = PCI_ANY_ID,
1741 .subdevice = PCI_ANY_ID,
1744 .vendor = PCI_VENDOR_ID_QLOGIC,
1745 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
1746 .subvendor = PCI_ANY_ID,
1747 .subdevice = PCI_ANY_ID,
1750 .vendor = PCI_VENDOR_ID_QLOGIC,
1751 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
1752 .subvendor = PCI_ANY_ID,
1753 .subdevice = PCI_ANY_ID,
1757 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1759 static struct pci_driver qla4xxx_pci_driver = {
1760 .name = DRIVER_NAME,
1761 .id_table = qla4xxx_pci_tbl,
1762 .probe = qla4xxx_probe_adapter,
1763 .remove = qla4xxx_remove_adapter,
1766 static int __init qla4xxx_module_init(void)
1770 /* Allocate cache for SRBs. */
1771 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
1772 SLAB_HWCACHE_ALIGN, NULL);
1773 if (srb_cachep == NULL) {
1775 "%s: Unable to allocate SRB cache..."
1776 "Failing load!\n", DRIVER_NAME);
1781 /* Derive version string. */
1782 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
1783 if (ql4xextended_error_logging)
1784 strcat(qla4xxx_version_str, "-debug");
1786 qla4xxx_scsi_transport =
1787 iscsi_register_transport(&qla4xxx_iscsi_transport);
1788 if (!qla4xxx_scsi_transport){
1790 goto release_srb_cache;
1793 ret = pci_register_driver(&qla4xxx_pci_driver);
1795 goto unregister_transport;
1797 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1800 unregister_transport:
1801 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1803 kmem_cache_destroy(srb_cachep);
1808 static void __exit qla4xxx_module_exit(void)
1811 pci_unregister_driver(&qla4xxx_pci_driver);
1812 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1813 kmem_cache_destroy(srb_cachep);
1816 module_init(qla4xxx_module_init);
1817 module_exit(qla4xxx_module_exit);
1819 MODULE_AUTHOR("QLogic Corporation");
1820 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1821 MODULE_LICENSE("GPL");
1822 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);