2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
5 * See LICENSE.qla4xxx for copyright and licensing details.
7 #include <linux/moduleparam.h>
9 #include <scsi/scsi_tcq.h>
10 #include <scsi/scsicam.h>
13 #include "ql4_version.h"
16 #include "ql4_inline.h"
21 static char qla4xxx_version_str[40];
24 * SRB allocation cache
26 static struct kmem_cache *srb_cachep;
29 * Module parameter information and variables
31 int ql4xdiscoverywait = 60;
32 module_param(ql4xdiscoverywait, int, S_IRUGO | S_IRUSR);
33 MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
34 int ql4xdontresethba = 0;
35 module_param(ql4xdontresethba, int, S_IRUGO | S_IRUSR);
36 MODULE_PARM_DESC(ql4xdontresethba,
37 "Dont reset the HBA when the driver gets 0x8002 AEN "
38 " default it will reset hba :0"
39 " set to 1 to avoid resetting HBA");
41 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
42 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IRUSR);
43 MODULE_PARM_DESC(ql4xextended_error_logging,
44 "Option to enable extended error logging, "
45 "Default is 0 - no logging, 1 - debug logging");
47 int ql4_mod_unload = 0;
50 * SCSI host template entry points
52 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
55 * iSCSI template entry points
57 static int qla4xxx_tgt_dscvr(enum iscsi_tgt_dscvr type, uint32_t host_no,
58 uint32_t enable, struct sockaddr *dst_addr);
59 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
60 enum iscsi_param param, char *buf);
61 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
62 enum iscsi_param param, char *buf);
63 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
64 enum iscsi_host_param param, char *buf);
65 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag);
66 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
67 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
70 * SCSI host template entry points
72 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
73 void (*done) (struct scsi_cmnd *));
74 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
75 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
76 static int qla4xxx_slave_alloc(struct scsi_device *device);
77 static int qla4xxx_slave_configure(struct scsi_device *device);
78 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
80 static struct scsi_host_template qla4xxx_driver_template = {
81 .module = THIS_MODULE,
83 .proc_name = DRIVER_NAME,
84 .queuecommand = qla4xxx_queuecommand,
86 .eh_device_reset_handler = qla4xxx_eh_device_reset,
87 .eh_host_reset_handler = qla4xxx_eh_host_reset,
89 .slave_configure = qla4xxx_slave_configure,
90 .slave_alloc = qla4xxx_slave_alloc,
91 .slave_destroy = qla4xxx_slave_destroy,
95 .use_clustering = ENABLE_CLUSTERING,
96 .sg_tablesize = SG_ALL,
98 .max_sectors = 0xFFFF,
101 static struct iscsi_transport qla4xxx_iscsi_transport = {
102 .owner = THIS_MODULE,
104 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
105 ISCSI_TARGET_NAME | ISCSI_TPGT,
106 .host_param_mask = ISCSI_HOST_HWADDRESS,
107 .sessiondata_size = sizeof(struct ddb_entry),
108 .host_template = &qla4xxx_driver_template,
110 .tgt_dscvr = qla4xxx_tgt_dscvr,
111 .get_conn_param = qla4xxx_conn_get_param,
112 .get_session_param = qla4xxx_sess_get_param,
113 .get_host_param = qla4xxx_host_get_param,
114 .start_conn = qla4xxx_conn_start,
115 .stop_conn = qla4xxx_conn_stop,
116 .session_recovery_timedout = qla4xxx_recovery_timedout,
119 static struct scsi_transport_template *qla4xxx_scsi_transport;
121 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
123 struct ddb_entry *ddb_entry = session->dd_data;
124 struct scsi_qla_host *ha = ddb_entry->ha;
126 DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count of (%d) "
127 "secs exhausted, marking device DEAD.\n", ha->host_no,
128 __func__, ddb_entry->fw_ddb_index,
129 ha->port_down_retry_count));
131 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
133 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc flags = "
134 "0x%lx\n", ha->host_no, __func__, ha->dpc_flags));
135 queue_work(ha->dpc_thread, &ha->dpc_work);
138 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn)
140 struct iscsi_cls_session *session;
141 struct ddb_entry *ddb_entry;
143 session = iscsi_dev_to_session(conn->dev.parent);
144 ddb_entry = session->dd_data;
146 DEBUG2(printk("scsi%ld: %s: index [%d] starting conn\n",
147 ddb_entry->ha->host_no, __func__,
148 ddb_entry->fw_ddb_index));
149 iscsi_unblock_session(session);
153 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag)
155 struct iscsi_cls_session *session;
156 struct ddb_entry *ddb_entry;
158 session = iscsi_dev_to_session(conn->dev.parent);
159 ddb_entry = session->dd_data;
161 DEBUG2(printk("scsi%ld: %s: index [%d] stopping conn\n",
162 ddb_entry->ha->host_no, __func__,
163 ddb_entry->fw_ddb_index));
164 if (flag == STOP_CONN_RECOVER)
165 iscsi_block_session(session);
167 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
170 static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
175 for (i = 0; i < len; i++)
176 cp += sprintf(cp, "%02x%c", addr[i],
177 i == (len - 1) ? '\n' : ':');
182 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
183 enum iscsi_host_param param, char *buf)
185 struct scsi_qla_host *ha = to_qla_host(shost);
189 case ISCSI_HOST_PARAM_HWADDRESS:
190 len = format_addr(buf, ha->my_mac, MAC_ADDR_LEN);
199 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
200 enum iscsi_param param, char *buf)
202 struct ddb_entry *ddb_entry = sess->dd_data;
206 case ISCSI_PARAM_TARGET_NAME:
207 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
208 ddb_entry->iscsi_name);
210 case ISCSI_PARAM_TPGT:
211 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
220 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
221 enum iscsi_param param, char *buf)
223 struct iscsi_cls_session *session;
224 struct ddb_entry *ddb_entry;
227 session = iscsi_dev_to_session(conn->dev.parent);
228 ddb_entry = session->dd_data;
231 case ISCSI_PARAM_CONN_PORT:
232 len = sprintf(buf, "%hu\n", ddb_entry->port);
234 case ISCSI_PARAM_CONN_ADDRESS:
235 /* TODO: what are the ipv6 bits */
236 len = sprintf(buf, "%u.%u.%u.%u\n",
237 NIPQUAD(ddb_entry->ip_addr));
246 static int qla4xxx_tgt_dscvr(enum iscsi_tgt_dscvr type, uint32_t host_no,
247 uint32_t enable, struct sockaddr *dst_addr)
249 struct scsi_qla_host *ha;
250 struct Scsi_Host *shost;
251 struct sockaddr_in *addr;
252 struct sockaddr_in6 *addr6;
255 shost = scsi_host_lookup(host_no);
257 printk(KERN_ERR "Could not find host no %u\n", host_no);
261 ha = (struct scsi_qla_host *) shost->hostdata;
264 case ISCSI_TGT_DSCVR_SEND_TARGETS:
265 if (dst_addr->sa_family == AF_INET) {
266 addr = (struct sockaddr_in *)dst_addr;
267 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
268 addr->sin_port) != QLA_SUCCESS)
270 } else if (dst_addr->sa_family == AF_INET6) {
272 * TODO: fix qla4xxx_send_tgts
274 addr6 = (struct sockaddr_in6 *)dst_addr;
275 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
276 addr6->sin6_port) != QLA_SUCCESS)
285 scsi_host_put(shost);
289 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
291 if (!ddb_entry->sess)
294 if (ddb_entry->conn) {
295 iscsi_if_destroy_session_done(ddb_entry->conn);
296 iscsi_destroy_conn(ddb_entry->conn);
297 iscsi_remove_session(ddb_entry->sess);
299 iscsi_free_session(ddb_entry->sess);
302 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
306 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
308 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
312 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0);
313 if (!ddb_entry->conn) {
314 iscsi_remove_session(ddb_entry->sess);
315 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
319 ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
320 iscsi_if_create_session_done(ddb_entry->conn);
324 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
326 struct ddb_entry *ddb_entry;
327 struct iscsi_cls_session *sess;
329 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport);
333 ddb_entry = sess->dd_data;
334 memset(ddb_entry, 0, sizeof(*ddb_entry));
336 ddb_entry->sess = sess;
344 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
345 unsigned long interval)
347 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
348 __func__, ha->host->host_no));
349 init_timer(&ha->timer);
350 ha->timer.expires = jiffies + interval * HZ;
351 ha->timer.data = (unsigned long)ha;
352 ha->timer.function = (void (*)(unsigned long))func;
353 add_timer(&ha->timer);
354 ha->timer_active = 1;
357 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
359 del_timer_sync(&ha->timer);
360 ha->timer_active = 0;
364 * qla4xxx_mark_device_missing - mark a device as missing.
365 * @ha: Pointer to host adapter structure.
366 * @ddb_entry: Pointer to device database entry
368 * This routine marks a device missing and resets the relogin retry count.
370 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
371 struct ddb_entry *ddb_entry)
373 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
374 DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
375 ha->host_no, ddb_entry->bus, ddb_entry->target,
376 ddb_entry->fw_ddb_index));
377 iscsi_conn_error(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
380 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
381 struct ddb_entry *ddb_entry,
382 struct scsi_cmnd *cmd,
383 void (*done)(struct scsi_cmnd *))
387 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
391 atomic_set(&srb->ref_count, 1);
393 srb->ddb = ddb_entry;
396 cmd->SCp.ptr = (void *)srb;
397 cmd->scsi_done = done;
402 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
404 struct scsi_cmnd *cmd = srb->cmd;
406 if (srb->flags & SRB_DMA_VALID) {
408 srb->flags &= ~SRB_DMA_VALID;
413 void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
415 struct scsi_cmnd *cmd = srb->cmd;
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;
446 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
447 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
448 cmd->result = DID_NO_CONNECT << 16;
449 goto qc_fail_command;
454 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
457 spin_unlock_irq(ha->host->host_lock);
459 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
461 goto qc_host_busy_lock;
463 rval = qla4xxx_send_command_to_isp(ha, srb);
464 if (rval != QLA_SUCCESS)
465 goto qc_host_busy_free_sp;
467 spin_lock_irq(ha->host->host_lock);
470 qc_host_busy_free_sp:
471 qla4xxx_srb_free_dma(ha, srb);
472 mempool_free(srb, ha->srb_mempool);
475 spin_lock_irq(ha->host->host_lock);
478 return SCSI_MLQUEUE_HOST_BUSY;
487 * qla4xxx_mem_free - frees memory allocated to adapter
488 * @ha: Pointer to host adapter structure.
490 * Frees memory previously allocated by qla4xxx_mem_alloc
492 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
495 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
501 ha->request_ring = NULL;
503 ha->response_ring = NULL;
504 ha->response_dma = 0;
505 ha->shadow_regs = NULL;
506 ha->shadow_regs_dma = 0;
510 mempool_destroy(ha->srb_mempool);
512 ha->srb_mempool = NULL;
514 /* release io space registers */
517 pci_release_regions(ha->pdev);
521 * qla4xxx_mem_alloc - allocates memory for use by adapter.
522 * @ha: Pointer to host adapter structure
524 * Allocates DMA memory for request and response queues. Also allocates memory
527 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
531 /* Allocate contiguous block of DMA memory for queues. */
532 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
533 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
534 sizeof(struct shadow_regs) +
536 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
537 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
538 &ha->queues_dma, GFP_KERNEL);
539 if (ha->queues == NULL) {
540 dev_warn(&ha->pdev->dev,
541 "Memory Allocation failed - queues.\n");
543 goto mem_alloc_error_exit;
545 memset(ha->queues, 0, ha->queues_len);
548 * As per RISC alignment requirements -- the bus-address must be a
549 * multiple of the request-ring size (in bytes).
552 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
553 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
554 (MEM_ALIGN_VALUE - 1));
556 /* Update request and response queue pointers. */
557 ha->request_dma = ha->queues_dma + align;
558 ha->request_ring = (struct queue_entry *) (ha->queues + align);
559 ha->response_dma = ha->queues_dma + align +
560 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
561 ha->response_ring = (struct queue_entry *) (ha->queues + align +
562 (REQUEST_QUEUE_DEPTH *
564 ha->shadow_regs_dma = ha->queues_dma + align +
565 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
566 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
567 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
568 (REQUEST_QUEUE_DEPTH *
570 (RESPONSE_QUEUE_DEPTH *
573 /* Allocate memory for srb pool. */
574 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
575 mempool_free_slab, srb_cachep);
576 if (ha->srb_mempool == NULL) {
577 dev_warn(&ha->pdev->dev,
578 "Memory Allocation failed - SRB Pool.\n");
580 goto mem_alloc_error_exit;
585 mem_alloc_error_exit:
586 qla4xxx_mem_free(ha);
591 * qla4xxx_timer - checks every second for work to do.
592 * @ha: Pointer to host adapter structure.
594 static void qla4xxx_timer(struct scsi_qla_host *ha)
596 struct ddb_entry *ddb_entry, *dtemp;
599 /* Search for relogin's to time-out and port down retry. */
600 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
601 /* Count down time between sending relogins */
602 if (adapter_up(ha) &&
603 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
604 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
605 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
607 if (atomic_read(&ddb_entry->retry_relogin_timer)
609 atomic_set(&ddb_entry->
612 set_bit(DPC_RELOGIN_DEVICE,
614 set_bit(DF_RELOGIN, &ddb_entry->flags);
615 DEBUG2(printk("scsi%ld: %s: index [%d]"
617 ha->host_no, __func__,
618 ddb_entry->fw_ddb_index));
620 atomic_dec(&ddb_entry->
621 retry_relogin_timer);
625 /* Wait for relogin to timeout */
626 if (atomic_read(&ddb_entry->relogin_timer) &&
627 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
629 * If the relogin times out and the device is
630 * still NOT ONLINE then try and relogin again.
632 if (atomic_read(&ddb_entry->state) !=
634 ddb_entry->fw_ddb_device_state ==
635 DDB_DS_SESSION_FAILED) {
636 /* Reset retry relogin timer */
637 atomic_inc(&ddb_entry->relogin_retry_count);
638 DEBUG2(printk("scsi%ld: index[%d] relogin"
639 " timed out-retrying"
642 ddb_entry->fw_ddb_index,
643 atomic_read(&ddb_entry->
644 relogin_retry_count))
647 DEBUG(printk("scsi%ld:%d:%d: index [%d] "
648 "initate relogin after"
650 ha->host_no, ddb_entry->bus,
652 ddb_entry->fw_ddb_index,
653 ddb_entry->default_time2wait + 4)
656 atomic_set(&ddb_entry->retry_relogin_timer,
657 ddb_entry->default_time2wait + 4);
662 /* Check for heartbeat interval. */
663 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
664 ha->heartbeat_interval != 0) {
665 ha->seconds_since_last_heartbeat++;
666 if (ha->seconds_since_last_heartbeat >
667 ha->heartbeat_interval + 2)
668 set_bit(DPC_RESET_HA, &ha->dpc_flags);
672 /* Wakeup the dpc routine for this adapter, if needed. */
674 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
675 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
676 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
677 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
678 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
679 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
680 test_bit(DPC_AEN, &ha->dpc_flags)) &&
682 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
683 " - dpc flags = 0x%lx\n",
684 ha->host_no, __func__, ha->dpc_flags));
685 queue_work(ha->dpc_thread, &ha->dpc_work);
688 /* Reschedule timer thread to call us back in one second */
689 mod_timer(&ha->timer, jiffies + HZ);
691 DEBUG2(ha->seconds_since_last_intr++);
695 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
696 * @ha: Pointer to host adapter structure.
698 * This routine stalls the driver until all outstanding commands are returned.
699 * Caller must release the Hardware Lock prior to calling this routine.
701 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
704 int stat = QLA_SUCCESS;
706 struct scsi_cmnd *cmd;
707 int wait_cnt = WAIT_CMD_TOV; /*
708 * Initialized for 30 seconds as we
709 * expect all commands to retuned
714 spin_lock_irqsave(&ha->hardware_lock, flags);
715 /* Find a command that hasn't completed. */
716 for (index = 0; index < ha->host->can_queue; index++) {
717 cmd = scsi_host_find_tag(ha->host, index);
721 spin_unlock_irqrestore(&ha->hardware_lock, flags);
723 /* If No Commands are pending, wait is complete */
724 if (index == ha->host->can_queue) {
728 /* If we timed out on waiting for commands to come back
737 } /* End of While (wait_cnt) */
742 void qla4xxx_hw_reset(struct scsi_qla_host *ha)
744 uint32_t ctrl_status;
745 unsigned long flags = 0;
747 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
749 spin_lock_irqsave(&ha->hardware_lock, flags);
752 * If the SCSI Reset Interrupt bit is set, clear it.
753 * Otherwise, the Soft Reset won't work.
755 ctrl_status = readw(&ha->reg->ctrl_status);
756 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
757 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
759 /* Issue Soft Reset */
760 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
761 readl(&ha->reg->ctrl_status);
763 spin_unlock_irqrestore(&ha->hardware_lock, flags);
767 * qla4xxx_soft_reset - performs soft reset.
768 * @ha: Pointer to host adapter structure.
770 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
772 uint32_t max_wait_time;
773 unsigned long flags = 0;
774 int status = QLA_ERROR;
775 uint32_t ctrl_status;
777 qla4xxx_hw_reset(ha);
779 /* Wait until the Network Reset Intr bit is cleared */
780 max_wait_time = RESET_INTR_TOV;
782 spin_lock_irqsave(&ha->hardware_lock, flags);
783 ctrl_status = readw(&ha->reg->ctrl_status);
784 spin_unlock_irqrestore(&ha->hardware_lock, flags);
786 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
790 } while ((--max_wait_time));
792 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
793 DEBUG2(printk(KERN_WARNING
794 "scsi%ld: Network Reset Intr not cleared by "
795 "Network function, clearing it now!\n",
797 spin_lock_irqsave(&ha->hardware_lock, flags);
798 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
799 readl(&ha->reg->ctrl_status);
800 spin_unlock_irqrestore(&ha->hardware_lock, flags);
803 /* Wait until the firmware tells us the Soft Reset is done */
804 max_wait_time = SOFT_RESET_TOV;
806 spin_lock_irqsave(&ha->hardware_lock, flags);
807 ctrl_status = readw(&ha->reg->ctrl_status);
808 spin_unlock_irqrestore(&ha->hardware_lock, flags);
810 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
811 status = QLA_SUCCESS;
816 } while ((--max_wait_time));
819 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
820 * after the soft reset has taken place.
822 spin_lock_irqsave(&ha->hardware_lock, flags);
823 ctrl_status = readw(&ha->reg->ctrl_status);
824 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
825 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
826 readl(&ha->reg->ctrl_status);
828 spin_unlock_irqrestore(&ha->hardware_lock, flags);
830 /* If soft reset fails then most probably the bios on other
831 * function is also enabled.
832 * Since the initialization is sequential the other fn
833 * wont be able to acknowledge the soft reset.
834 * Issue a force soft reset to workaround this scenario.
836 if (max_wait_time == 0) {
837 /* Issue Force Soft Reset */
838 spin_lock_irqsave(&ha->hardware_lock, flags);
839 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
840 readl(&ha->reg->ctrl_status);
841 spin_unlock_irqrestore(&ha->hardware_lock, flags);
842 /* Wait until the firmware tells us the Soft Reset is done */
843 max_wait_time = SOFT_RESET_TOV;
845 spin_lock_irqsave(&ha->hardware_lock, flags);
846 ctrl_status = readw(&ha->reg->ctrl_status);
847 spin_unlock_irqrestore(&ha->hardware_lock, flags);
849 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
850 status = QLA_SUCCESS;
855 } while ((--max_wait_time));
862 * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
863 * @ha: Pointer to host adapter structure.
865 * This routine is called just prior to a HARD RESET to return all
866 * outstanding commands back to the Operating System.
867 * Caller should make sure that the following locks are released
868 * before this calling routine: Hardware lock, and io_request_lock.
870 static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
876 spin_lock_irqsave(&ha->hardware_lock, flags);
877 for (i = 0; i < ha->host->can_queue; i++) {
878 srb = qla4xxx_del_from_active_array(ha, i);
880 srb->cmd->result = DID_RESET << 16;
881 qla4xxx_srb_compl(ha, srb);
884 spin_unlock_irqrestore(&ha->hardware_lock, flags);
889 * qla4xxx_recover_adapter - recovers adapter after a fatal error
890 * @ha: Pointer to host adapter structure.
891 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
892 * after adapter recovery has completed.
893 * 0=preserve ddb list, 1=destroy and rebuild ddb list
895 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
896 uint8_t renew_ddb_list)
900 /* Stall incoming I/O until we are done */
901 clear_bit(AF_ONLINE, &ha->flags);
902 DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
905 /* Wait for outstanding commands to complete.
906 * Stalls the driver for max 30 secs
908 status = qla4xxx_cmd_wait(ha);
910 qla4xxx_disable_intrs(ha);
912 /* Flush any pending ddb changed AENs */
913 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
915 /* Reset the firmware. If successful, function
916 * returns with ISP interrupts enabled.
918 if (status == QLA_SUCCESS) {
919 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
920 ha->host_no, __func__));
921 qla4xxx_flush_active_srbs(ha);
922 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
923 status = qla4xxx_soft_reset(ha);
928 /* Flush any pending ddb changed AENs */
929 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
931 /* Re-initialize firmware. If successful, function returns
932 * with ISP interrupts enabled */
933 if (status == QLA_SUCCESS) {
934 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
935 ha->host_no, __func__));
937 /* If successful, AF_ONLINE flag set in
938 * qla4xxx_initialize_adapter */
939 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
942 /* Failed adapter initialization?
943 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
944 if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
945 (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
946 /* Adapter initialization failed, see if we can retry
947 * resetting the ha */
948 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
949 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
950 DEBUG2(printk("scsi%ld: recover adapter - retrying "
951 "(%d) more times\n", ha->host_no,
952 ha->retry_reset_ha_cnt));
953 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
956 if (ha->retry_reset_ha_cnt > 0) {
957 /* Schedule another Reset HA--DPC will retry */
958 ha->retry_reset_ha_cnt--;
959 DEBUG2(printk("scsi%ld: recover adapter - "
960 "retry remaining %d\n",
962 ha->retry_reset_ha_cnt));
966 if (ha->retry_reset_ha_cnt == 0) {
967 /* Recover adapter retries have been exhausted.
969 DEBUG2(printk("scsi%ld: recover adapter "
970 "failed - board disabled\n",
972 qla4xxx_flush_active_srbs(ha);
973 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
974 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
975 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
981 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
982 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
983 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
986 ha->adapter_error_count++;
988 if (status == QLA_SUCCESS)
989 qla4xxx_enable_intrs(ha);
991 DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
996 * qla4xxx_do_dpc - dpc routine
997 * @data: in our case pointer to adapter structure
999 * This routine is a task that is schedule by the interrupt handler
1000 * to perform the background processing for interrupts. We put it
1001 * on a task queue that is consumed whenever the scheduler runs; that's
1002 * so you can do anything (i.e. put the process to sleep etc). In fact,
1003 * the mid-level tries to sleep when it reaches the driver threshold
1004 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1006 static void qla4xxx_do_dpc(struct work_struct *work)
1008 struct scsi_qla_host *ha =
1009 container_of(work, struct scsi_qla_host, dpc_work);
1010 struct ddb_entry *ddb_entry, *dtemp;
1011 int status = QLA_ERROR;
1013 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1014 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
1015 ha->host_no, __func__, ha->flags, ha->dpc_flags,
1016 readw(&ha->reg->ctrl_status)));
1018 /* Initialization not yet finished. Don't do anything yet. */
1019 if (!test_bit(AF_INIT_DONE, &ha->flags))
1022 if (adapter_up(ha) ||
1023 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1024 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1025 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
1026 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1027 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1028 qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1030 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1031 uint8_t wait_time = RESET_INTR_TOV;
1033 while ((readw(&ha->reg->ctrl_status) &
1034 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1035 if (--wait_time == 0)
1040 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1041 "bit not cleared-- resetting\n",
1042 ha->host_no, __func__));
1043 qla4xxx_flush_active_srbs(ha);
1044 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1045 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1046 status = qla4xxx_initialize_adapter(ha,
1049 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1050 if (status == QLA_SUCCESS)
1051 qla4xxx_enable_intrs(ha);
1055 /* ---- process AEN? --- */
1056 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1057 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1059 /* ---- Get DHCP IP Address? --- */
1060 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1061 qla4xxx_get_dhcp_ip_address(ha);
1063 /* ---- relogin device? --- */
1064 if (adapter_up(ha) &&
1065 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1066 list_for_each_entry_safe(ddb_entry, dtemp,
1067 &ha->ddb_list, list) {
1068 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1069 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1070 qla4xxx_relogin_device(ha, ddb_entry);
1073 * If mbx cmd times out there is no point
1074 * in continuing further.
1075 * With large no of targets this can hang
1078 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1079 printk(KERN_WARNING "scsi%ld: %s: "
1080 "need to reset hba\n",
1081 ha->host_no, __func__);
1089 * qla4xxx_free_adapter - release the adapter
1090 * @ha: pointer to adapter structure
1092 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1095 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1096 /* Turn-off interrupts on the card. */
1097 qla4xxx_disable_intrs(ha);
1100 /* Kill the kernel thread for this host */
1102 destroy_workqueue(ha->dpc_thread);
1104 /* Issue Soft Reset to put firmware in unknown state */
1105 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
1106 qla4xxx_hw_reset(ha);
1108 /* Remove timer thread, if present */
1109 if (ha->timer_active)
1110 qla4xxx_stop_timer(ha);
1112 /* Detach interrupts */
1113 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1114 free_irq(ha->pdev->irq, ha);
1116 /* free extra memory */
1117 qla4xxx_mem_free(ha);
1119 pci_disable_device(ha->pdev);
1124 * qla4xxx_iospace_config - maps registers
1125 * @ha: pointer to adapter structure
1127 * This routines maps HBA's registers from the pci address space
1128 * into the kernel virtual address space for memory mapped i/o.
1130 static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1132 unsigned long pio, pio_len, pio_flags;
1133 unsigned long mmio, mmio_len, mmio_flags;
1135 pio = pci_resource_start(ha->pdev, 0);
1136 pio_len = pci_resource_len(ha->pdev, 0);
1137 pio_flags = pci_resource_flags(ha->pdev, 0);
1138 if (pio_flags & IORESOURCE_IO) {
1139 if (pio_len < MIN_IOBASE_LEN) {
1140 dev_warn(&ha->pdev->dev,
1141 "Invalid PCI I/O region size\n");
1145 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1149 /* Use MMIO operations for all accesses. */
1150 mmio = pci_resource_start(ha->pdev, 1);
1151 mmio_len = pci_resource_len(ha->pdev, 1);
1152 mmio_flags = pci_resource_flags(ha->pdev, 1);
1154 if (!(mmio_flags & IORESOURCE_MEM)) {
1155 dev_err(&ha->pdev->dev,
1156 "region #0 not an MMIO resource, aborting\n");
1158 goto iospace_error_exit;
1160 if (mmio_len < MIN_IOBASE_LEN) {
1161 dev_err(&ha->pdev->dev,
1162 "Invalid PCI mem region size, aborting\n");
1163 goto iospace_error_exit;
1166 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1167 dev_warn(&ha->pdev->dev,
1168 "Failed to reserve PIO/MMIO regions\n");
1170 goto iospace_error_exit;
1173 ha->pio_address = pio;
1174 ha->pio_length = pio_len;
1175 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1177 dev_err(&ha->pdev->dev,
1178 "cannot remap MMIO, aborting\n");
1180 goto iospace_error_exit;
1190 * qla4xxx_probe_adapter - callback function to probe HBA
1191 * @pdev: pointer to pci_dev structure
1192 * @pci_device_id: pointer to pci_device entry
1194 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1195 * It returns zero if successful. It also initializes all data necessary for
1198 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1199 const struct pci_device_id *ent)
1201 int ret = -ENODEV, status;
1202 struct Scsi_Host *host;
1203 struct scsi_qla_host *ha;
1204 struct ddb_entry *ddb_entry, *ddbtemp;
1205 uint8_t init_retry_count = 0;
1208 if (pci_enable_device(pdev))
1211 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1214 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1215 goto probe_disable_device;
1218 /* Clear our data area */
1219 ha = (struct scsi_qla_host *) host->hostdata;
1220 memset(ha, 0, sizeof(*ha));
1222 /* Save the information from PCI BIOS. */
1225 ha->host_no = host->host_no;
1227 /* Configure PCI I/O space. */
1228 ret = qla4xxx_iospace_config(ha);
1232 dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1233 pdev->device, pdev->irq, ha->reg);
1235 qla4xxx_config_dma_addressing(ha);
1237 /* Initialize lists and spinlocks. */
1238 INIT_LIST_HEAD(&ha->ddb_list);
1239 INIT_LIST_HEAD(&ha->free_srb_q);
1241 mutex_init(&ha->mbox_sem);
1243 spin_lock_init(&ha->hardware_lock);
1245 /* Allocate dma buffers */
1246 if (qla4xxx_mem_alloc(ha)) {
1247 dev_warn(&ha->pdev->dev,
1248 "[ERROR] Failed to allocate memory for adapter\n");
1255 * Initialize the Host adapter request/response queues and
1257 * NOTE: interrupts enabled upon successful completion
1259 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1260 while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1261 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1262 "(%d)\n", __func__, init_retry_count));
1263 qla4xxx_soft_reset(ha);
1264 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1266 if (status == QLA_ERROR) {
1267 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1273 host->cmd_per_lun = 3;
1274 host->max_channel = 0;
1275 host->max_lun = MAX_LUNS - 1;
1276 host->max_id = MAX_TARGETS;
1277 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1278 host->can_queue = MAX_SRBS ;
1279 host->transportt = qla4xxx_scsi_transport;
1281 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1283 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed");
1287 /* Startup the kernel thread for this host adapter. */
1288 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1289 "qla4xxx_dpc\n", __func__));
1290 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1291 ha->dpc_thread = create_singlethread_workqueue(buf);
1292 if (!ha->dpc_thread) {
1293 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1297 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1299 ret = request_irq(pdev->irq, qla4xxx_intr_handler,
1300 IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
1302 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1303 " already in use.\n", pdev->irq);
1306 set_bit(AF_IRQ_ATTACHED, &ha->flags);
1307 host->irq = pdev->irq;
1308 DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1310 qla4xxx_enable_intrs(ha);
1312 /* Start timer thread. */
1313 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1315 set_bit(AF_INIT_DONE, &ha->flags);
1317 pci_set_drvdata(pdev, ha);
1319 ret = scsi_add_host(host, &pdev->dev);
1323 /* Update transport device information for all devices. */
1324 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
1325 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
1326 if (qla4xxx_add_sess(ddb_entry))
1331 " QLogic iSCSI HBA Driver version: %s\n"
1332 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1333 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1334 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1335 ha->patch_number, ha->build_number);
1340 qla4xxx_free_ddb_list(ha);
1341 scsi_remove_host(host);
1344 qla4xxx_free_adapter(ha);
1345 scsi_host_put(ha->host);
1347 probe_disable_device:
1348 pci_disable_device(pdev);
1354 * qla4xxx_remove_adapter - calback function to remove adapter.
1355 * @pci_dev: PCI device pointer
1357 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1359 struct scsi_qla_host *ha;
1361 ha = pci_get_drvdata(pdev);
1363 qla4xxx_disable_intrs(ha);
1365 while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1368 /* remove devs from iscsi_sessions to scsi_devices */
1369 qla4xxx_free_ddb_list(ha);
1371 scsi_remove_host(ha->host);
1373 qla4xxx_free_adapter(ha);
1375 scsi_host_put(ha->host);
1377 pci_set_drvdata(pdev, NULL);
1381 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1384 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1385 * supported addressing method.
1387 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1391 /* Update our PCI device dma_mask for full 64 bit mask */
1392 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1393 if (pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1394 dev_dbg(&ha->pdev->dev,
1395 "Failed to set 64 bit PCI consistent mask; "
1397 retval = pci_set_consistent_dma_mask(ha->pdev,
1401 retval = pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1404 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1406 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1407 struct ddb_entry *ddb = sess->dd_data;
1409 sdev->hostdata = ddb;
1410 sdev->tagged_supported = 1;
1411 scsi_activate_tcq(sdev, sdev->host->can_queue);
1415 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1417 sdev->tagged_supported = 1;
1421 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1423 scsi_deactivate_tcq(sdev, 1);
1427 * qla4xxx_del_from_active_array - returns an active srb
1428 * @ha: Pointer to host adapter structure.
1429 * @index: index into to the active_array
1431 * This routine removes and returns the srb at the specified index
1433 struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1435 struct srb *srb = NULL;
1436 struct scsi_cmnd *cmd;
1438 if (!(cmd = scsi_host_find_tag(ha->host, index)))
1441 if (!(srb = (struct srb *)cmd->host_scribble))
1444 /* update counters */
1445 if (srb->flags & SRB_DMA_VALID) {
1446 ha->req_q_count += srb->iocb_cnt;
1447 ha->iocb_cnt -= srb->iocb_cnt;
1449 srb->cmd->host_scribble = NULL;
1455 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1456 * @ha: actual ha whose done queue will contain the comd returned by firmware.
1457 * @cmd: Scsi Command to wait on.
1459 * This routine waits for the command to be returned by the Firmware
1460 * for some max time.
1462 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1463 struct scsi_cmnd *cmd)
1467 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1470 /* Checking to see if its returned to OS */
1471 rp = (struct srb *) cmd->SCp.ptr;
1478 } while (max_wait_time--);
1484 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1485 * @ha: Pointer to host adapter structure
1487 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1489 unsigned long wait_online;
1491 wait_online = jiffies + (30 * HZ);
1492 while (time_before(jiffies, wait_online)) {
1496 else if (ha->retry_reset_ha_cnt == 0)
1506 * qla4xxx_eh_wait_for_active_target_commands - wait for active cmds to finish.
1507 * @ha: pointer to to HBA
1511 * This function waits for all outstanding commands to a lun to complete. It
1512 * returns 0 if all pending commands are returned and 1 otherwise.
1514 static int qla4xxx_eh_wait_for_active_target_commands(struct scsi_qla_host *ha,
1519 struct scsi_cmnd *cmd;
1522 * Waiting for all commands for the designated target in the active
1525 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1526 cmd = scsi_host_find_tag(ha->host, cnt);
1527 if (cmd && cmd->device->id == t && cmd->device->lun == l) {
1528 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1538 * qla4xxx_eh_device_reset - callback for target reset.
1539 * @cmd: Pointer to Linux's SCSI command structure
1541 * This routine is called by the Linux OS to reset all luns on the
1544 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1546 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1547 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1549 int ret = FAILED, stat;
1551 sp = (struct srb *) cmd->SCp.ptr;
1552 if (!sp || !ddb_entry)
1555 dev_info(&ha->pdev->dev,
1556 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1557 cmd->device->channel, cmd->device->id, cmd->device->lun);
1559 DEBUG2(printk(KERN_INFO
1560 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1561 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
1562 cmd, jiffies, cmd->timeout_per_command / HZ,
1563 ha->dpc_flags, cmd->result, cmd->allowed));
1565 /* FIXME: wait for hba to go online */
1566 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1567 if (stat != QLA_SUCCESS) {
1568 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1569 goto eh_dev_reset_done;
1573 ha->marker_needed = 1;
1576 * If we are coming down the EH path, wait for all commands to complete
1579 if (cmd->device->host->shost_state == SHOST_RECOVERY) {
1580 if (qla4xxx_eh_wait_for_active_target_commands(ha,
1583 dev_info(&ha->pdev->dev,
1584 "DEVICE RESET FAILED - waiting for "
1586 goto eh_dev_reset_done;
1590 dev_info(&ha->pdev->dev,
1591 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1592 ha->host_no, cmd->device->channel, cmd->device->id,
1603 * qla4xxx_eh_host_reset - kernel callback
1604 * @cmd: Pointer to Linux's SCSI command structure
1606 * This routine is invoked by the Linux kernel to perform fatal error
1607 * recovery on the specified adapter.
1609 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1611 int return_status = FAILED;
1612 struct scsi_qla_host *ha;
1614 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1616 dev_info(&ha->pdev->dev,
1617 "scsi(%ld:%d:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no,
1618 cmd->device->channel, cmd->device->id, cmd->device->lun);
1620 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1621 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
1622 "DEAD.\n", ha->host_no, cmd->device->channel,
1628 if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS) {
1629 return_status = SUCCESS;
1632 dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1633 return_status == FAILED ? "FAILED" : "SUCCEDED");
1635 return return_status;
1639 static struct pci_device_id qla4xxx_pci_tbl[] = {
1641 .vendor = PCI_VENDOR_ID_QLOGIC,
1642 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
1643 .subvendor = PCI_ANY_ID,
1644 .subdevice = PCI_ANY_ID,
1647 .vendor = PCI_VENDOR_ID_QLOGIC,
1648 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
1649 .subvendor = PCI_ANY_ID,
1650 .subdevice = PCI_ANY_ID,
1653 .vendor = PCI_VENDOR_ID_QLOGIC,
1654 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
1655 .subvendor = PCI_ANY_ID,
1656 .subdevice = PCI_ANY_ID,
1660 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1662 static struct pci_driver qla4xxx_pci_driver = {
1663 .name = DRIVER_NAME,
1664 .id_table = qla4xxx_pci_tbl,
1665 .probe = qla4xxx_probe_adapter,
1666 .remove = qla4xxx_remove_adapter,
1669 static int __init qla4xxx_module_init(void)
1673 /* Allocate cache for SRBs. */
1674 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
1675 SLAB_HWCACHE_ALIGN, NULL, NULL);
1676 if (srb_cachep == NULL) {
1678 "%s: Unable to allocate SRB cache..."
1679 "Failing load!\n", DRIVER_NAME);
1684 /* Derive version string. */
1685 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
1686 if (ql4xextended_error_logging)
1687 strcat(qla4xxx_version_str, "-debug");
1689 qla4xxx_scsi_transport =
1690 iscsi_register_transport(&qla4xxx_iscsi_transport);
1691 if (!qla4xxx_scsi_transport){
1693 goto release_srb_cache;
1696 ret = pci_register_driver(&qla4xxx_pci_driver);
1698 goto unregister_transport;
1700 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1703 unregister_transport:
1704 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1706 kmem_cache_destroy(srb_cachep);
1711 static void __exit qla4xxx_module_exit(void)
1714 pci_unregister_driver(&qla4xxx_pci_driver);
1715 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1716 kmem_cache_destroy(srb_cachep);
1719 module_init(qla4xxx_module_init);
1720 module_exit(qla4xxx_module_exit);
1722 MODULE_AUTHOR("QLogic Corporation");
1723 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1724 MODULE_LICENSE("GPL");
1725 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);