rcv reorder queue bugfix
[cor_2_6_31.git] / drivers / scsi / qla4xxx / ql4_os.c
blobec9da6ce8489ceedf36ab7efe8bd59c56cebfc45
1 /*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7 #include <linux/moduleparam.h>
9 #include <scsi/scsi_tcq.h>
10 #include <scsi/scsicam.h>
12 #include "ql4_def.h"
13 #include "ql4_version.h"
14 #include "ql4_glbl.h"
15 #include "ql4_dbg.h"
16 #include "ql4_inline.h"
19 * Driver version
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;
49 #define QL4_DEF_QDEPTH 32
52 * SCSI host template entry points
54 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
57 * iSCSI template entry points
59 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
60 enum iscsi_tgt_dscvr type, uint32_t enable,
61 struct sockaddr *dst_addr);
62 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
63 enum iscsi_param param, char *buf);
64 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
65 enum iscsi_param param, char *buf);
66 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
67 enum iscsi_host_param param, char *buf);
68 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
71 * SCSI host template entry points
73 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
74 void (*done) (struct scsi_cmnd *));
75 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
76 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
77 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
78 static int qla4xxx_slave_alloc(struct scsi_device *device);
79 static int qla4xxx_slave_configure(struct scsi_device *device);
80 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
81 static void qla4xxx_scan_start(struct Scsi_Host *shost);
83 static struct scsi_host_template qla4xxx_driver_template = {
84 .module = THIS_MODULE,
85 .name = DRIVER_NAME,
86 .proc_name = DRIVER_NAME,
87 .queuecommand = qla4xxx_queuecommand,
89 .eh_device_reset_handler = qla4xxx_eh_device_reset,
90 .eh_target_reset_handler = qla4xxx_eh_target_reset,
91 .eh_host_reset_handler = qla4xxx_eh_host_reset,
93 .slave_configure = qla4xxx_slave_configure,
94 .slave_alloc = qla4xxx_slave_alloc,
95 .slave_destroy = qla4xxx_slave_destroy,
97 .scan_finished = iscsi_scan_finished,
98 .scan_start = qla4xxx_scan_start,
100 .this_id = -1,
101 .cmd_per_lun = 3,
102 .use_clustering = ENABLE_CLUSTERING,
103 .sg_tablesize = SG_ALL,
105 .max_sectors = 0xFFFF,
108 static struct iscsi_transport qla4xxx_iscsi_transport = {
109 .owner = THIS_MODULE,
110 .name = DRIVER_NAME,
111 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
112 CAP_DATA_PATH_OFFLOAD,
113 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
114 ISCSI_TARGET_NAME | ISCSI_TPGT,
115 .host_param_mask = ISCSI_HOST_HWADDRESS |
116 ISCSI_HOST_IPADDRESS |
117 ISCSI_HOST_INITIATOR_NAME,
118 .tgt_dscvr = qla4xxx_tgt_dscvr,
119 .get_conn_param = qla4xxx_conn_get_param,
120 .get_session_param = qla4xxx_sess_get_param,
121 .get_host_param = qla4xxx_host_get_param,
122 .session_recovery_timedout = qla4xxx_recovery_timedout,
125 static struct scsi_transport_template *qla4xxx_scsi_transport;
127 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
129 struct ddb_entry *ddb_entry = session->dd_data;
130 struct scsi_qla_host *ha = ddb_entry->ha;
132 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
133 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
135 DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count "
136 "of (%d) secs exhausted, marking device DEAD.\n",
137 ha->host_no, __func__, ddb_entry->fw_ddb_index,
138 ha->port_down_retry_count));
140 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc "
141 "flags = 0x%lx\n",
142 ha->host_no, __func__, ha->dpc_flags));
143 queue_work(ha->dpc_thread, &ha->dpc_work);
147 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
148 enum iscsi_host_param param, char *buf)
150 struct scsi_qla_host *ha = to_qla_host(shost);
151 int len;
153 switch (param) {
154 case ISCSI_HOST_PARAM_HWADDRESS:
155 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
156 break;
157 case ISCSI_HOST_PARAM_IPADDRESS:
158 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
159 ha->ip_address[1], ha->ip_address[2],
160 ha->ip_address[3]);
161 break;
162 case ISCSI_HOST_PARAM_INITIATOR_NAME:
163 len = sprintf(buf, "%s\n", ha->name_string);
164 break;
165 default:
166 return -ENOSYS;
169 return len;
172 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
173 enum iscsi_param param, char *buf)
175 struct ddb_entry *ddb_entry = sess->dd_data;
176 int len;
178 switch (param) {
179 case ISCSI_PARAM_TARGET_NAME:
180 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
181 ddb_entry->iscsi_name);
182 break;
183 case ISCSI_PARAM_TPGT:
184 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
185 break;
186 default:
187 return -ENOSYS;
190 return len;
193 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
194 enum iscsi_param param, char *buf)
196 struct iscsi_cls_session *session;
197 struct ddb_entry *ddb_entry;
198 int len;
200 session = iscsi_dev_to_session(conn->dev.parent);
201 ddb_entry = session->dd_data;
203 switch (param) {
204 case ISCSI_PARAM_CONN_PORT:
205 len = sprintf(buf, "%hu\n", ddb_entry->port);
206 break;
207 case ISCSI_PARAM_CONN_ADDRESS:
208 /* TODO: what are the ipv6 bits */
209 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
210 break;
211 default:
212 return -ENOSYS;
215 return len;
218 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
219 enum iscsi_tgt_dscvr type, uint32_t enable,
220 struct sockaddr *dst_addr)
222 struct scsi_qla_host *ha;
223 struct sockaddr_in *addr;
224 struct sockaddr_in6 *addr6;
225 int ret = 0;
227 ha = (struct scsi_qla_host *) shost->hostdata;
229 switch (type) {
230 case ISCSI_TGT_DSCVR_SEND_TARGETS:
231 if (dst_addr->sa_family == AF_INET) {
232 addr = (struct sockaddr_in *)dst_addr;
233 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
234 addr->sin_port) != QLA_SUCCESS)
235 ret = -EIO;
236 } else if (dst_addr->sa_family == AF_INET6) {
238 * TODO: fix qla4xxx_send_tgts
240 addr6 = (struct sockaddr_in6 *)dst_addr;
241 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
242 addr6->sin6_port) != QLA_SUCCESS)
243 ret = -EIO;
244 } else
245 ret = -ENOSYS;
246 break;
247 default:
248 ret = -ENOSYS;
250 return ret;
253 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
255 if (!ddb_entry->sess)
256 return;
258 if (ddb_entry->conn) {
259 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
260 iscsi_remove_session(ddb_entry->sess);
262 iscsi_free_session(ddb_entry->sess);
265 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
267 int err;
269 ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
270 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
271 if (err) {
272 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
273 return err;
276 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
277 if (!ddb_entry->conn) {
278 iscsi_remove_session(ddb_entry->sess);
279 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
280 return -ENOMEM;
283 /* finally ready to go */
284 iscsi_unblock_session(ddb_entry->sess);
285 return 0;
288 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
290 struct ddb_entry *ddb_entry;
291 struct iscsi_cls_session *sess;
293 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
294 sizeof(struct ddb_entry));
295 if (!sess)
296 return NULL;
298 ddb_entry = sess->dd_data;
299 memset(ddb_entry, 0, sizeof(*ddb_entry));
300 ddb_entry->ha = ha;
301 ddb_entry->sess = sess;
302 return ddb_entry;
305 static void qla4xxx_scan_start(struct Scsi_Host *shost)
307 struct scsi_qla_host *ha = shost_priv(shost);
308 struct ddb_entry *ddb_entry, *ddbtemp;
310 /* finish setup of sessions that were already setup in firmware */
311 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
312 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
313 qla4xxx_add_sess(ddb_entry);
318 * Timer routines
321 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
322 unsigned long interval)
324 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
325 __func__, ha->host->host_no));
326 init_timer(&ha->timer);
327 ha->timer.expires = jiffies + interval * HZ;
328 ha->timer.data = (unsigned long)ha;
329 ha->timer.function = (void (*)(unsigned long))func;
330 add_timer(&ha->timer);
331 ha->timer_active = 1;
334 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
336 del_timer_sync(&ha->timer);
337 ha->timer_active = 0;
340 /***
341 * qla4xxx_mark_device_missing - mark a device as missing.
342 * @ha: Pointer to host adapter structure.
343 * @ddb_entry: Pointer to device database entry
345 * This routine marks a device missing and resets the relogin retry count.
347 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
348 struct ddb_entry *ddb_entry)
350 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
351 DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
352 ha->host_no, ddb_entry->bus, ddb_entry->target,
353 ddb_entry->fw_ddb_index));
354 iscsi_block_session(ddb_entry->sess);
355 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
358 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
359 struct ddb_entry *ddb_entry,
360 struct scsi_cmnd *cmd,
361 void (*done)(struct scsi_cmnd *))
363 struct srb *srb;
365 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
366 if (!srb)
367 return srb;
369 atomic_set(&srb->ref_count, 1);
370 srb->ha = ha;
371 srb->ddb = ddb_entry;
372 srb->cmd = cmd;
373 srb->flags = 0;
374 cmd->SCp.ptr = (void *)srb;
375 cmd->scsi_done = done;
377 return srb;
380 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
382 struct scsi_cmnd *cmd = srb->cmd;
384 if (srb->flags & SRB_DMA_VALID) {
385 scsi_dma_unmap(cmd);
386 srb->flags &= ~SRB_DMA_VALID;
388 cmd->SCp.ptr = NULL;
391 void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
393 struct scsi_cmnd *cmd = srb->cmd;
395 qla4xxx_srb_free_dma(ha, srb);
397 mempool_free(srb, ha->srb_mempool);
399 cmd->scsi_done(cmd);
403 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
404 * @cmd: Pointer to Linux's SCSI command structure
405 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
406 * that the command has been processed.
408 * Remarks:
409 * This routine is invoked by Linux to send a SCSI command to the driver.
410 * The mid-level driver tries to ensure that queuecommand never gets
411 * invoked concurrently with itself or the interrupt handler (although
412 * the interrupt handler may call this routine as part of request-
413 * completion handling). Unfortunely, it sometimes calls the scheduler
414 * in interrupt context which is a big NO! NO!.
416 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
417 void (*done)(struct scsi_cmnd *))
419 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
420 struct ddb_entry *ddb_entry = cmd->device->hostdata;
421 struct iscsi_cls_session *sess = ddb_entry->sess;
422 struct srb *srb;
423 int rval;
425 if (!sess) {
426 cmd->result = DID_IMM_RETRY << 16;
427 goto qc_fail_command;
430 rval = iscsi_session_chkready(sess);
431 if (rval) {
432 cmd->result = rval;
433 goto qc_fail_command;
436 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
437 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
438 cmd->result = DID_NO_CONNECT << 16;
439 goto qc_fail_command;
441 return SCSI_MLQUEUE_TARGET_BUSY;
444 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
445 goto qc_host_busy;
447 spin_unlock_irq(ha->host->host_lock);
449 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
450 if (!srb)
451 goto qc_host_busy_lock;
453 rval = qla4xxx_send_command_to_isp(ha, srb);
454 if (rval != QLA_SUCCESS)
455 goto qc_host_busy_free_sp;
457 spin_lock_irq(ha->host->host_lock);
458 return 0;
460 qc_host_busy_free_sp:
461 qla4xxx_srb_free_dma(ha, srb);
462 mempool_free(srb, ha->srb_mempool);
464 qc_host_busy_lock:
465 spin_lock_irq(ha->host->host_lock);
467 qc_host_busy:
468 return SCSI_MLQUEUE_HOST_BUSY;
470 qc_fail_command:
471 done(cmd);
473 return 0;
477 * qla4xxx_mem_free - frees memory allocated to adapter
478 * @ha: Pointer to host adapter structure.
480 * Frees memory previously allocated by qla4xxx_mem_alloc
482 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
484 if (ha->queues)
485 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
486 ha->queues_dma);
488 ha->queues_len = 0;
489 ha->queues = NULL;
490 ha->queues_dma = 0;
491 ha->request_ring = NULL;
492 ha->request_dma = 0;
493 ha->response_ring = NULL;
494 ha->response_dma = 0;
495 ha->shadow_regs = NULL;
496 ha->shadow_regs_dma = 0;
498 /* Free srb pool. */
499 if (ha->srb_mempool)
500 mempool_destroy(ha->srb_mempool);
502 ha->srb_mempool = NULL;
504 /* release io space registers */
505 if (ha->reg)
506 iounmap(ha->reg);
507 pci_release_regions(ha->pdev);
511 * qla4xxx_mem_alloc - allocates memory for use by adapter.
512 * @ha: Pointer to host adapter structure
514 * Allocates DMA memory for request and response queues. Also allocates memory
515 * for srbs.
517 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
519 unsigned long align;
521 /* Allocate contiguous block of DMA memory for queues. */
522 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
523 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
524 sizeof(struct shadow_regs) +
525 MEM_ALIGN_VALUE +
526 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
527 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
528 &ha->queues_dma, GFP_KERNEL);
529 if (ha->queues == NULL) {
530 dev_warn(&ha->pdev->dev,
531 "Memory Allocation failed - queues.\n");
533 goto mem_alloc_error_exit;
535 memset(ha->queues, 0, ha->queues_len);
538 * As per RISC alignment requirements -- the bus-address must be a
539 * multiple of the request-ring size (in bytes).
541 align = 0;
542 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
543 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
544 (MEM_ALIGN_VALUE - 1));
546 /* Update request and response queue pointers. */
547 ha->request_dma = ha->queues_dma + align;
548 ha->request_ring = (struct queue_entry *) (ha->queues + align);
549 ha->response_dma = ha->queues_dma + align +
550 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
551 ha->response_ring = (struct queue_entry *) (ha->queues + align +
552 (REQUEST_QUEUE_DEPTH *
553 QUEUE_SIZE));
554 ha->shadow_regs_dma = ha->queues_dma + align +
555 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
556 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
557 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
558 (REQUEST_QUEUE_DEPTH *
559 QUEUE_SIZE) +
560 (RESPONSE_QUEUE_DEPTH *
561 QUEUE_SIZE));
563 /* Allocate memory for srb pool. */
564 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
565 mempool_free_slab, srb_cachep);
566 if (ha->srb_mempool == NULL) {
567 dev_warn(&ha->pdev->dev,
568 "Memory Allocation failed - SRB Pool.\n");
570 goto mem_alloc_error_exit;
573 return QLA_SUCCESS;
575 mem_alloc_error_exit:
576 qla4xxx_mem_free(ha);
577 return QLA_ERROR;
581 * qla4xxx_timer - checks every second for work to do.
582 * @ha: Pointer to host adapter structure.
584 static void qla4xxx_timer(struct scsi_qla_host *ha)
586 struct ddb_entry *ddb_entry, *dtemp;
587 int start_dpc = 0;
589 /* Search for relogin's to time-out and port down retry. */
590 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
591 /* Count down time between sending relogins */
592 if (adapter_up(ha) &&
593 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
594 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
595 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
596 INVALID_ENTRY) {
597 if (atomic_read(&ddb_entry->retry_relogin_timer)
598 == 0) {
599 atomic_set(&ddb_entry->
600 retry_relogin_timer,
601 INVALID_ENTRY);
602 set_bit(DPC_RELOGIN_DEVICE,
603 &ha->dpc_flags);
604 set_bit(DF_RELOGIN, &ddb_entry->flags);
605 DEBUG2(printk("scsi%ld: %s: index [%d]"
606 " login device\n",
607 ha->host_no, __func__,
608 ddb_entry->fw_ddb_index));
609 } else
610 atomic_dec(&ddb_entry->
611 retry_relogin_timer);
615 /* Wait for relogin to timeout */
616 if (atomic_read(&ddb_entry->relogin_timer) &&
617 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
619 * If the relogin times out and the device is
620 * still NOT ONLINE then try and relogin again.
622 if (atomic_read(&ddb_entry->state) !=
623 DDB_STATE_ONLINE &&
624 ddb_entry->fw_ddb_device_state ==
625 DDB_DS_SESSION_FAILED) {
626 /* Reset retry relogin timer */
627 atomic_inc(&ddb_entry->relogin_retry_count);
628 DEBUG2(printk("scsi%ld: index[%d] relogin"
629 " timed out-retrying"
630 " relogin (%d)\n",
631 ha->host_no,
632 ddb_entry->fw_ddb_index,
633 atomic_read(&ddb_entry->
634 relogin_retry_count))
636 start_dpc++;
637 DEBUG(printk("scsi%ld:%d:%d: index [%d] "
638 "initate relogin after"
639 " %d seconds\n",
640 ha->host_no, ddb_entry->bus,
641 ddb_entry->target,
642 ddb_entry->fw_ddb_index,
643 ddb_entry->default_time2wait + 4)
646 atomic_set(&ddb_entry->retry_relogin_timer,
647 ddb_entry->default_time2wait + 4);
652 /* Check for heartbeat interval. */
653 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
654 ha->heartbeat_interval != 0) {
655 ha->seconds_since_last_heartbeat++;
656 if (ha->seconds_since_last_heartbeat >
657 ha->heartbeat_interval + 2)
658 set_bit(DPC_RESET_HA, &ha->dpc_flags);
662 /* Wakeup the dpc routine for this adapter, if needed. */
663 if ((start_dpc ||
664 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
665 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
666 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
667 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
668 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
669 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
670 test_bit(DPC_AEN, &ha->dpc_flags)) &&
671 ha->dpc_thread) {
672 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
673 " - dpc flags = 0x%lx\n",
674 ha->host_no, __func__, ha->dpc_flags));
675 queue_work(ha->dpc_thread, &ha->dpc_work);
678 /* Reschedule timer thread to call us back in one second */
679 mod_timer(&ha->timer, jiffies + HZ);
681 DEBUG2(ha->seconds_since_last_intr++);
685 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
686 * @ha: Pointer to host adapter structure.
688 * This routine stalls the driver until all outstanding commands are returned.
689 * Caller must release the Hardware Lock prior to calling this routine.
691 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
693 uint32_t index = 0;
694 int stat = QLA_SUCCESS;
695 unsigned long flags;
696 struct scsi_cmnd *cmd;
697 int wait_cnt = WAIT_CMD_TOV; /*
698 * Initialized for 30 seconds as we
699 * expect all commands to retuned
700 * ASAP.
703 while (wait_cnt) {
704 spin_lock_irqsave(&ha->hardware_lock, flags);
705 /* Find a command that hasn't completed. */
706 for (index = 0; index < ha->host->can_queue; index++) {
707 cmd = scsi_host_find_tag(ha->host, index);
708 if (cmd != NULL)
709 break;
711 spin_unlock_irqrestore(&ha->hardware_lock, flags);
713 /* If No Commands are pending, wait is complete */
714 if (index == ha->host->can_queue) {
715 break;
718 /* If we timed out on waiting for commands to come back
719 * return ERROR.
721 wait_cnt--;
722 if (wait_cnt == 0)
723 stat = QLA_ERROR;
724 else {
725 msleep(1000);
727 } /* End of While (wait_cnt) */
729 return stat;
732 void qla4xxx_hw_reset(struct scsi_qla_host *ha)
734 uint32_t ctrl_status;
735 unsigned long flags = 0;
737 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
739 spin_lock_irqsave(&ha->hardware_lock, flags);
742 * If the SCSI Reset Interrupt bit is set, clear it.
743 * Otherwise, the Soft Reset won't work.
745 ctrl_status = readw(&ha->reg->ctrl_status);
746 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
747 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
749 /* Issue Soft Reset */
750 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
751 readl(&ha->reg->ctrl_status);
753 spin_unlock_irqrestore(&ha->hardware_lock, flags);
757 * qla4xxx_soft_reset - performs soft reset.
758 * @ha: Pointer to host adapter structure.
760 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
762 uint32_t max_wait_time;
763 unsigned long flags = 0;
764 int status = QLA_ERROR;
765 uint32_t ctrl_status;
767 qla4xxx_hw_reset(ha);
769 /* Wait until the Network Reset Intr bit is cleared */
770 max_wait_time = RESET_INTR_TOV;
771 do {
772 spin_lock_irqsave(&ha->hardware_lock, flags);
773 ctrl_status = readw(&ha->reg->ctrl_status);
774 spin_unlock_irqrestore(&ha->hardware_lock, flags);
776 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
777 break;
779 msleep(1000);
780 } while ((--max_wait_time));
782 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
783 DEBUG2(printk(KERN_WARNING
784 "scsi%ld: Network Reset Intr not cleared by "
785 "Network function, clearing it now!\n",
786 ha->host_no));
787 spin_lock_irqsave(&ha->hardware_lock, flags);
788 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
789 readl(&ha->reg->ctrl_status);
790 spin_unlock_irqrestore(&ha->hardware_lock, flags);
793 /* Wait until the firmware tells us the Soft Reset is done */
794 max_wait_time = SOFT_RESET_TOV;
795 do {
796 spin_lock_irqsave(&ha->hardware_lock, flags);
797 ctrl_status = readw(&ha->reg->ctrl_status);
798 spin_unlock_irqrestore(&ha->hardware_lock, flags);
800 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
801 status = QLA_SUCCESS;
802 break;
805 msleep(1000);
806 } while ((--max_wait_time));
809 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
810 * after the soft reset has taken place.
812 spin_lock_irqsave(&ha->hardware_lock, flags);
813 ctrl_status = readw(&ha->reg->ctrl_status);
814 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
815 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
816 readl(&ha->reg->ctrl_status);
818 spin_unlock_irqrestore(&ha->hardware_lock, flags);
820 /* If soft reset fails then most probably the bios on other
821 * function is also enabled.
822 * Since the initialization is sequential the other fn
823 * wont be able to acknowledge the soft reset.
824 * Issue a force soft reset to workaround this scenario.
826 if (max_wait_time == 0) {
827 /* Issue Force Soft Reset */
828 spin_lock_irqsave(&ha->hardware_lock, flags);
829 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
830 readl(&ha->reg->ctrl_status);
831 spin_unlock_irqrestore(&ha->hardware_lock, flags);
832 /* Wait until the firmware tells us the Soft Reset is done */
833 max_wait_time = SOFT_RESET_TOV;
834 do {
835 spin_lock_irqsave(&ha->hardware_lock, flags);
836 ctrl_status = readw(&ha->reg->ctrl_status);
837 spin_unlock_irqrestore(&ha->hardware_lock, flags);
839 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
840 status = QLA_SUCCESS;
841 break;
844 msleep(1000);
845 } while ((--max_wait_time));
848 return status;
852 * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
853 * @ha: Pointer to host adapter structure.
855 * This routine is called just prior to a HARD RESET to return all
856 * outstanding commands back to the Operating System.
857 * Caller should make sure that the following locks are released
858 * before this calling routine: Hardware lock, and io_request_lock.
860 static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
862 struct srb *srb;
863 int i;
864 unsigned long flags;
866 spin_lock_irqsave(&ha->hardware_lock, flags);
867 for (i = 0; i < ha->host->can_queue; i++) {
868 srb = qla4xxx_del_from_active_array(ha, i);
869 if (srb != NULL) {
870 srb->cmd->result = DID_RESET << 16;
871 qla4xxx_srb_compl(ha, srb);
874 spin_unlock_irqrestore(&ha->hardware_lock, flags);
879 * qla4xxx_recover_adapter - recovers adapter after a fatal error
880 * @ha: Pointer to host adapter structure.
881 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
883 * renew_ddb_list value can be 0=preserve ddb list, 1=destroy and rebuild
884 * ddb list.
886 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
887 uint8_t renew_ddb_list)
889 int status;
891 /* Stall incoming I/O until we are done */
892 clear_bit(AF_ONLINE, &ha->flags);
894 DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
895 __func__));
897 /* Wait for outstanding commands to complete.
898 * Stalls the driver for max 30 secs
900 status = qla4xxx_cmd_wait(ha);
902 qla4xxx_disable_intrs(ha);
904 /* Flush any pending ddb changed AENs */
905 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
907 /* Reset the firmware. If successful, function
908 * returns with ISP interrupts enabled.
910 if (status == QLA_SUCCESS) {
911 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
912 ha->host_no, __func__));
913 qla4xxx_flush_active_srbs(ha);
914 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
915 status = qla4xxx_soft_reset(ha);
916 else
917 status = QLA_ERROR;
920 /* Flush any pending ddb changed AENs */
921 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
923 /* Re-initialize firmware. If successful, function returns
924 * with ISP interrupts enabled */
925 if (status == QLA_SUCCESS) {
926 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
927 ha->host_no, __func__));
929 /* If successful, AF_ONLINE flag set in
930 * qla4xxx_initialize_adapter */
931 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
934 /* Failed adapter initialization?
935 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
936 if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
937 (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
938 /* Adapter initialization failed, see if we can retry
939 * resetting the ha */
940 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
941 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
942 DEBUG2(printk("scsi%ld: recover adapter - retrying "
943 "(%d) more times\n", ha->host_no,
944 ha->retry_reset_ha_cnt));
945 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
946 status = QLA_ERROR;
947 } else {
948 if (ha->retry_reset_ha_cnt > 0) {
949 /* Schedule another Reset HA--DPC will retry */
950 ha->retry_reset_ha_cnt--;
951 DEBUG2(printk("scsi%ld: recover adapter - "
952 "retry remaining %d\n",
953 ha->host_no,
954 ha->retry_reset_ha_cnt));
955 status = QLA_ERROR;
958 if (ha->retry_reset_ha_cnt == 0) {
959 /* Recover adapter retries have been exhausted.
960 * Adapter DEAD */
961 DEBUG2(printk("scsi%ld: recover adapter "
962 "failed - board disabled\n",
963 ha->host_no));
964 qla4xxx_flush_active_srbs(ha);
965 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
966 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
967 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
968 &ha->dpc_flags);
969 status = QLA_ERROR;
972 } else {
973 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
974 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
975 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
978 ha->adapter_error_count++;
980 if (status == QLA_SUCCESS)
981 qla4xxx_enable_intrs(ha);
983 DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
984 return status;
988 * qla4xxx_do_dpc - dpc routine
989 * @data: in our case pointer to adapter structure
991 * This routine is a task that is schedule by the interrupt handler
992 * to perform the background processing for interrupts. We put it
993 * on a task queue that is consumed whenever the scheduler runs; that's
994 * so you can do anything (i.e. put the process to sleep etc). In fact,
995 * the mid-level tries to sleep when it reaches the driver threshold
996 * "host->can_queue". This can cause a panic if we were in our interrupt code.
998 static void qla4xxx_do_dpc(struct work_struct *work)
1000 struct scsi_qla_host *ha =
1001 container_of(work, struct scsi_qla_host, dpc_work);
1002 struct ddb_entry *ddb_entry, *dtemp;
1003 int status = QLA_ERROR;
1005 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1006 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
1007 ha->host_no, __func__, ha->flags, ha->dpc_flags,
1008 readw(&ha->reg->ctrl_status)));
1010 /* Initialization not yet finished. Don't do anything yet. */
1011 if (!test_bit(AF_INIT_DONE, &ha->flags))
1012 return;
1014 if (adapter_up(ha) ||
1015 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1016 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1017 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
1018 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1019 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1020 qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1022 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1023 uint8_t wait_time = RESET_INTR_TOV;
1025 while ((readw(&ha->reg->ctrl_status) &
1026 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1027 if (--wait_time == 0)
1028 break;
1029 msleep(1000);
1031 if (wait_time == 0)
1032 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1033 "bit not cleared-- resetting\n",
1034 ha->host_no, __func__));
1035 qla4xxx_flush_active_srbs(ha);
1036 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1037 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1038 status = qla4xxx_initialize_adapter(ha,
1039 PRESERVE_DDB_LIST);
1041 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1042 if (status == QLA_SUCCESS)
1043 qla4xxx_enable_intrs(ha);
1047 /* ---- process AEN? --- */
1048 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1049 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1051 /* ---- Get DHCP IP Address? --- */
1052 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1053 qla4xxx_get_dhcp_ip_address(ha);
1055 /* ---- relogin device? --- */
1056 if (adapter_up(ha) &&
1057 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1058 list_for_each_entry_safe(ddb_entry, dtemp,
1059 &ha->ddb_list, list) {
1060 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1061 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1062 qla4xxx_relogin_device(ha, ddb_entry);
1065 * If mbx cmd times out there is no point
1066 * in continuing further.
1067 * With large no of targets this can hang
1068 * the system.
1070 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1071 printk(KERN_WARNING "scsi%ld: %s: "
1072 "need to reset hba\n",
1073 ha->host_no, __func__);
1074 break;
1081 * qla4xxx_free_adapter - release the adapter
1082 * @ha: pointer to adapter structure
1084 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1087 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1088 /* Turn-off interrupts on the card. */
1089 qla4xxx_disable_intrs(ha);
1092 /* Kill the kernel thread for this host */
1093 if (ha->dpc_thread)
1094 destroy_workqueue(ha->dpc_thread);
1096 /* Issue Soft Reset to put firmware in unknown state */
1097 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
1098 qla4xxx_hw_reset(ha);
1100 /* Remove timer thread, if present */
1101 if (ha->timer_active)
1102 qla4xxx_stop_timer(ha);
1104 /* Detach interrupts */
1105 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1106 free_irq(ha->pdev->irq, ha);
1108 /* free extra memory */
1109 qla4xxx_mem_free(ha);
1111 pci_disable_device(ha->pdev);
1115 /***
1116 * qla4xxx_iospace_config - maps registers
1117 * @ha: pointer to adapter structure
1119 * This routines maps HBA's registers from the pci address space
1120 * into the kernel virtual address space for memory mapped i/o.
1122 static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1124 unsigned long pio, pio_len, pio_flags;
1125 unsigned long mmio, mmio_len, mmio_flags;
1127 pio = pci_resource_start(ha->pdev, 0);
1128 pio_len = pci_resource_len(ha->pdev, 0);
1129 pio_flags = pci_resource_flags(ha->pdev, 0);
1130 if (pio_flags & IORESOURCE_IO) {
1131 if (pio_len < MIN_IOBASE_LEN) {
1132 dev_warn(&ha->pdev->dev,
1133 "Invalid PCI I/O region size\n");
1134 pio = 0;
1136 } else {
1137 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1138 pio = 0;
1141 /* Use MMIO operations for all accesses. */
1142 mmio = pci_resource_start(ha->pdev, 1);
1143 mmio_len = pci_resource_len(ha->pdev, 1);
1144 mmio_flags = pci_resource_flags(ha->pdev, 1);
1146 if (!(mmio_flags & IORESOURCE_MEM)) {
1147 dev_err(&ha->pdev->dev,
1148 "region #0 not an MMIO resource, aborting\n");
1150 goto iospace_error_exit;
1152 if (mmio_len < MIN_IOBASE_LEN) {
1153 dev_err(&ha->pdev->dev,
1154 "Invalid PCI mem region size, aborting\n");
1155 goto iospace_error_exit;
1158 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1159 dev_warn(&ha->pdev->dev,
1160 "Failed to reserve PIO/MMIO regions\n");
1162 goto iospace_error_exit;
1165 ha->pio_address = pio;
1166 ha->pio_length = pio_len;
1167 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1168 if (!ha->reg) {
1169 dev_err(&ha->pdev->dev,
1170 "cannot remap MMIO, aborting\n");
1172 goto iospace_error_exit;
1175 return 0;
1177 iospace_error_exit:
1178 return -ENOMEM;
1182 * qla4xxx_probe_adapter - callback function to probe HBA
1183 * @pdev: pointer to pci_dev structure
1184 * @pci_device_id: pointer to pci_device entry
1186 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1187 * It returns zero if successful. It also initializes all data necessary for
1188 * the driver.
1190 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1191 const struct pci_device_id *ent)
1193 int ret = -ENODEV, status;
1194 struct Scsi_Host *host;
1195 struct scsi_qla_host *ha;
1196 uint8_t init_retry_count = 0;
1197 char buf[34];
1199 if (pci_enable_device(pdev))
1200 return -1;
1202 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1203 if (host == NULL) {
1204 printk(KERN_WARNING
1205 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1206 goto probe_disable_device;
1209 /* Clear our data area */
1210 ha = (struct scsi_qla_host *) host->hostdata;
1211 memset(ha, 0, sizeof(*ha));
1213 /* Save the information from PCI BIOS. */
1214 ha->pdev = pdev;
1215 ha->host = host;
1216 ha->host_no = host->host_no;
1218 /* Configure PCI I/O space. */
1219 ret = qla4xxx_iospace_config(ha);
1220 if (ret)
1221 goto probe_failed;
1223 dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1224 pdev->device, pdev->irq, ha->reg);
1226 qla4xxx_config_dma_addressing(ha);
1228 /* Initialize lists and spinlocks. */
1229 INIT_LIST_HEAD(&ha->ddb_list);
1230 INIT_LIST_HEAD(&ha->free_srb_q);
1232 mutex_init(&ha->mbox_sem);
1234 spin_lock_init(&ha->hardware_lock);
1236 /* Allocate dma buffers */
1237 if (qla4xxx_mem_alloc(ha)) {
1238 dev_warn(&ha->pdev->dev,
1239 "[ERROR] Failed to allocate memory for adapter\n");
1241 ret = -ENOMEM;
1242 goto probe_failed;
1246 * Initialize the Host adapter request/response queues and
1247 * firmware
1248 * NOTE: interrupts enabled upon successful completion
1250 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1251 while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1252 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1253 "(%d)\n", __func__, init_retry_count));
1254 qla4xxx_soft_reset(ha);
1255 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1257 if (status == QLA_ERROR) {
1258 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1260 ret = -ENODEV;
1261 goto probe_failed;
1264 host->cmd_per_lun = 3;
1265 host->max_channel = 0;
1266 host->max_lun = MAX_LUNS - 1;
1267 host->max_id = MAX_TARGETS;
1268 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1269 host->can_queue = MAX_SRBS ;
1270 host->transportt = qla4xxx_scsi_transport;
1272 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1273 if (ret) {
1274 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed\n");
1275 goto probe_failed;
1278 /* Startup the kernel thread for this host adapter. */
1279 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1280 "qla4xxx_dpc\n", __func__));
1281 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1282 ha->dpc_thread = create_singlethread_workqueue(buf);
1283 if (!ha->dpc_thread) {
1284 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1285 ret = -ENODEV;
1286 goto probe_failed;
1288 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1290 ret = request_irq(pdev->irq, qla4xxx_intr_handler,
1291 IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
1292 if (ret) {
1293 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1294 " already in use.\n", pdev->irq);
1295 goto probe_failed;
1297 set_bit(AF_IRQ_ATTACHED, &ha->flags);
1298 host->irq = pdev->irq;
1299 DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1301 qla4xxx_enable_intrs(ha);
1303 /* Start timer thread. */
1304 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1306 set_bit(AF_INIT_DONE, &ha->flags);
1308 pci_set_drvdata(pdev, ha);
1310 ret = scsi_add_host(host, &pdev->dev);
1311 if (ret)
1312 goto probe_failed;
1314 printk(KERN_INFO
1315 " QLogic iSCSI HBA Driver version: %s\n"
1316 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1317 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1318 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1319 ha->patch_number, ha->build_number);
1320 scsi_scan_host(host);
1321 return 0;
1323 probe_failed:
1324 qla4xxx_free_adapter(ha);
1325 scsi_host_put(ha->host);
1327 probe_disable_device:
1328 pci_disable_device(pdev);
1330 return ret;
1334 * qla4xxx_remove_adapter - calback function to remove adapter.
1335 * @pci_dev: PCI device pointer
1337 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1339 struct scsi_qla_host *ha;
1341 ha = pci_get_drvdata(pdev);
1343 qla4xxx_disable_intrs(ha);
1345 while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1346 ssleep(1);
1348 /* remove devs from iscsi_sessions to scsi_devices */
1349 qla4xxx_free_ddb_list(ha);
1351 scsi_remove_host(ha->host);
1353 qla4xxx_free_adapter(ha);
1355 scsi_host_put(ha->host);
1357 pci_set_drvdata(pdev, NULL);
1361 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1362 * @ha: HA context
1364 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1365 * supported addressing method.
1367 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1369 int retval;
1371 /* Update our PCI device dma_mask for full 64 bit mask */
1372 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1373 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1374 dev_dbg(&ha->pdev->dev,
1375 "Failed to set 64 bit PCI consistent mask; "
1376 "using 32 bit.\n");
1377 retval = pci_set_consistent_dma_mask(ha->pdev,
1378 DMA_BIT_MASK(32));
1380 } else
1381 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1384 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1386 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1387 struct ddb_entry *ddb = sess->dd_data;
1389 sdev->hostdata = ddb;
1390 sdev->tagged_supported = 1;
1391 scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
1392 return 0;
1395 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1397 sdev->tagged_supported = 1;
1398 return 0;
1401 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1403 scsi_deactivate_tcq(sdev, 1);
1407 * qla4xxx_del_from_active_array - returns an active srb
1408 * @ha: Pointer to host adapter structure.
1409 * @index: index into to the active_array
1411 * This routine removes and returns the srb at the specified index
1413 struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1415 struct srb *srb = NULL;
1416 struct scsi_cmnd *cmd;
1418 if (!(cmd = scsi_host_find_tag(ha->host, index)))
1419 return srb;
1421 if (!(srb = (struct srb *)cmd->host_scribble))
1422 return srb;
1424 /* update counters */
1425 if (srb->flags & SRB_DMA_VALID) {
1426 ha->req_q_count += srb->iocb_cnt;
1427 ha->iocb_cnt -= srb->iocb_cnt;
1428 if (srb->cmd)
1429 srb->cmd->host_scribble = NULL;
1431 return srb;
1435 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1436 * @ha: actual ha whose done queue will contain the comd returned by firmware.
1437 * @cmd: Scsi Command to wait on.
1439 * This routine waits for the command to be returned by the Firmware
1440 * for some max time.
1442 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1443 struct scsi_cmnd *cmd)
1445 int done = 0;
1446 struct srb *rp;
1447 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1449 do {
1450 /* Checking to see if its returned to OS */
1451 rp = (struct srb *) cmd->SCp.ptr;
1452 if (rp == NULL) {
1453 done++;
1454 break;
1457 msleep(2000);
1458 } while (max_wait_time--);
1460 return done;
1464 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1465 * @ha: Pointer to host adapter structure
1467 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1469 unsigned long wait_online;
1471 wait_online = jiffies + (30 * HZ);
1472 while (time_before(jiffies, wait_online)) {
1474 if (adapter_up(ha))
1475 return QLA_SUCCESS;
1476 else if (ha->retry_reset_ha_cnt == 0)
1477 return QLA_ERROR;
1479 msleep(2000);
1482 return QLA_ERROR;
1486 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
1487 * @ha: pointer to to HBA
1488 * @t: target id
1489 * @l: lun id
1491 * This function waits for all outstanding commands to a lun to complete. It
1492 * returns 0 if all pending commands are returned and 1 otherwise.
1494 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1495 struct scsi_target *stgt,
1496 struct scsi_device *sdev)
1498 int cnt;
1499 int status = 0;
1500 struct scsi_cmnd *cmd;
1503 * Waiting for all commands for the designated target or dev
1504 * in the active array
1506 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1507 cmd = scsi_host_find_tag(ha->host, cnt);
1508 if (cmd && stgt == scsi_target(cmd->device) &&
1509 (!sdev || sdev == cmd->device)) {
1510 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1511 status++;
1512 break;
1516 return status;
1520 * qla4xxx_eh_device_reset - callback for target reset.
1521 * @cmd: Pointer to Linux's SCSI command structure
1523 * This routine is called by the Linux OS to reset all luns on the
1524 * specified target.
1526 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1528 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1529 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1530 struct srb *sp;
1531 int ret = FAILED, stat;
1533 sp = (struct srb *) cmd->SCp.ptr;
1534 if (!sp || !ddb_entry)
1535 return ret;
1537 dev_info(&ha->pdev->dev,
1538 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1539 cmd->device->channel, cmd->device->id, cmd->device->lun);
1541 DEBUG2(printk(KERN_INFO
1542 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1543 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
1544 cmd, jiffies, cmd->request->timeout / HZ,
1545 ha->dpc_flags, cmd->result, cmd->allowed));
1547 /* FIXME: wait for hba to go online */
1548 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1549 if (stat != QLA_SUCCESS) {
1550 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1551 goto eh_dev_reset_done;
1554 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1555 cmd->device)) {
1556 dev_info(&ha->pdev->dev,
1557 "DEVICE RESET FAILED - waiting for "
1558 "commands.\n");
1559 goto eh_dev_reset_done;
1562 /* Send marker. */
1563 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1564 MM_LUN_RESET) != QLA_SUCCESS)
1565 goto eh_dev_reset_done;
1567 dev_info(&ha->pdev->dev,
1568 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1569 ha->host_no, cmd->device->channel, cmd->device->id,
1570 cmd->device->lun);
1572 ret = SUCCESS;
1574 eh_dev_reset_done:
1576 return ret;
1580 * qla4xxx_eh_target_reset - callback for target reset.
1581 * @cmd: Pointer to Linux's SCSI command structure
1583 * This routine is called by the Linux OS to reset the target.
1585 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
1587 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1588 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1589 int stat;
1591 if (!ddb_entry)
1592 return FAILED;
1594 starget_printk(KERN_INFO, scsi_target(cmd->device),
1595 "WARM TARGET RESET ISSUED.\n");
1597 DEBUG2(printk(KERN_INFO
1598 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
1599 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
1600 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
1601 ha->dpc_flags, cmd->result, cmd->allowed));
1603 stat = qla4xxx_reset_target(ha, ddb_entry);
1604 if (stat != QLA_SUCCESS) {
1605 starget_printk(KERN_INFO, scsi_target(cmd->device),
1606 "WARM TARGET RESET FAILED.\n");
1607 return FAILED;
1610 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
1611 NULL)) {
1612 starget_printk(KERN_INFO, scsi_target(cmd->device),
1613 "WARM TARGET DEVICE RESET FAILED - "
1614 "waiting for commands.\n");
1615 return FAILED;
1618 /* Send marker. */
1619 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
1620 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
1621 starget_printk(KERN_INFO, scsi_target(cmd->device),
1622 "WARM TARGET DEVICE RESET FAILED - "
1623 "marker iocb failed.\n");
1624 return FAILED;
1627 starget_printk(KERN_INFO, scsi_target(cmd->device),
1628 "WARM TARGET RESET SUCCEEDED.\n");
1629 return SUCCESS;
1633 * qla4xxx_eh_host_reset - kernel callback
1634 * @cmd: Pointer to Linux's SCSI command structure
1636 * This routine is invoked by the Linux kernel to perform fatal error
1637 * recovery on the specified adapter.
1639 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1641 int return_status = FAILED;
1642 struct scsi_qla_host *ha;
1644 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1646 dev_info(&ha->pdev->dev,
1647 "scsi(%ld:%d:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no,
1648 cmd->device->channel, cmd->device->id, cmd->device->lun);
1650 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1651 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
1652 "DEAD.\n", ha->host_no, cmd->device->channel,
1653 __func__));
1655 return FAILED;
1658 /* make sure the dpc thread is stopped while we reset the hba */
1659 clear_bit(AF_ONLINE, &ha->flags);
1660 flush_workqueue(ha->dpc_thread);
1662 if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS)
1663 return_status = SUCCESS;
1665 dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1666 return_status == FAILED ? "FAILED" : "SUCCEDED");
1668 return return_status;
1672 static struct pci_device_id qla4xxx_pci_tbl[] = {
1674 .vendor = PCI_VENDOR_ID_QLOGIC,
1675 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
1676 .subvendor = PCI_ANY_ID,
1677 .subdevice = PCI_ANY_ID,
1680 .vendor = PCI_VENDOR_ID_QLOGIC,
1681 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
1682 .subvendor = PCI_ANY_ID,
1683 .subdevice = PCI_ANY_ID,
1686 .vendor = PCI_VENDOR_ID_QLOGIC,
1687 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
1688 .subvendor = PCI_ANY_ID,
1689 .subdevice = PCI_ANY_ID,
1691 {0, 0},
1693 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1695 static struct pci_driver qla4xxx_pci_driver = {
1696 .name = DRIVER_NAME,
1697 .id_table = qla4xxx_pci_tbl,
1698 .probe = qla4xxx_probe_adapter,
1699 .remove = qla4xxx_remove_adapter,
1702 static int __init qla4xxx_module_init(void)
1704 int ret;
1706 /* Allocate cache for SRBs. */
1707 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
1708 SLAB_HWCACHE_ALIGN, NULL);
1709 if (srb_cachep == NULL) {
1710 printk(KERN_ERR
1711 "%s: Unable to allocate SRB cache..."
1712 "Failing load!\n", DRIVER_NAME);
1713 ret = -ENOMEM;
1714 goto no_srp_cache;
1717 /* Derive version string. */
1718 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
1719 if (ql4xextended_error_logging)
1720 strcat(qla4xxx_version_str, "-debug");
1722 qla4xxx_scsi_transport =
1723 iscsi_register_transport(&qla4xxx_iscsi_transport);
1724 if (!qla4xxx_scsi_transport){
1725 ret = -ENODEV;
1726 goto release_srb_cache;
1729 ret = pci_register_driver(&qla4xxx_pci_driver);
1730 if (ret)
1731 goto unregister_transport;
1733 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1734 return 0;
1736 unregister_transport:
1737 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1738 release_srb_cache:
1739 kmem_cache_destroy(srb_cachep);
1740 no_srp_cache:
1741 return ret;
1744 static void __exit qla4xxx_module_exit(void)
1746 ql4_mod_unload = 1;
1747 pci_unregister_driver(&qla4xxx_pci_driver);
1748 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1749 kmem_cache_destroy(srb_cachep);
1752 module_init(qla4xxx_module_init);
1753 module_exit(qla4xxx_module_exit);
1755 MODULE_AUTHOR("QLogic Corporation");
1756 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1757 MODULE_LICENSE("GPL");
1758 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);