Use access mode instead of open flags to determine needed permissions
[pv_ops_mirror.git] / drivers / scsi / qla4xxx / ql4_os.c
blob89460d27c68959225bc27bfb818f989a06426738
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;
50 * SCSI host template entry points
52 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
55 * iSCSI template entry points
57 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
58 enum iscsi_tgt_dscvr type, uint32_t enable,
59 struct sockaddr *dst_addr);
60 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
61 enum iscsi_param param, char *buf);
62 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
63 enum iscsi_param param, char *buf);
64 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
65 enum iscsi_host_param param, char *buf);
66 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag);
67 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
68 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
71 * SCSI host template entry points
73 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
74 void (*done) (struct scsi_cmnd *));
75 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
76 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
77 static int qla4xxx_slave_alloc(struct scsi_device *device);
78 static int qla4xxx_slave_configure(struct scsi_device *device);
79 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
81 static struct scsi_host_template qla4xxx_driver_template = {
82 .module = THIS_MODULE,
83 .name = DRIVER_NAME,
84 .proc_name = DRIVER_NAME,
85 .queuecommand = qla4xxx_queuecommand,
87 .eh_device_reset_handler = qla4xxx_eh_device_reset,
88 .eh_host_reset_handler = qla4xxx_eh_host_reset,
90 .slave_configure = qla4xxx_slave_configure,
91 .slave_alloc = qla4xxx_slave_alloc,
92 .slave_destroy = qla4xxx_slave_destroy,
94 .this_id = -1,
95 .cmd_per_lun = 3,
96 .use_clustering = ENABLE_CLUSTERING,
97 .use_sg_chaining = ENABLE_SG_CHAINING,
98 .sg_tablesize = SG_ALL,
100 .max_sectors = 0xFFFF,
103 static struct iscsi_transport qla4xxx_iscsi_transport = {
104 .owner = THIS_MODULE,
105 .name = DRIVER_NAME,
106 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
107 CAP_DATA_PATH_OFFLOAD,
108 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
109 ISCSI_TARGET_NAME | ISCSI_TPGT,
110 .host_param_mask = ISCSI_HOST_HWADDRESS |
111 ISCSI_HOST_IPADDRESS |
112 ISCSI_HOST_INITIATOR_NAME,
113 .sessiondata_size = sizeof(struct ddb_entry),
114 .host_template = &qla4xxx_driver_template,
116 .tgt_dscvr = qla4xxx_tgt_dscvr,
117 .get_conn_param = qla4xxx_conn_get_param,
118 .get_session_param = qla4xxx_sess_get_param,
119 .get_host_param = qla4xxx_host_get_param,
120 .start_conn = qla4xxx_conn_start,
121 .stop_conn = qla4xxx_conn_stop,
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 DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count of (%d) "
133 "secs exhausted, marking device DEAD.\n", ha->host_no,
134 __func__, ddb_entry->fw_ddb_index,
135 ha->port_down_retry_count));
137 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
139 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc flags = "
140 "0x%lx\n", ha->host_no, __func__, ha->dpc_flags));
141 queue_work(ha->dpc_thread, &ha->dpc_work);
144 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn)
146 struct iscsi_cls_session *session;
147 struct ddb_entry *ddb_entry;
149 session = iscsi_dev_to_session(conn->dev.parent);
150 ddb_entry = session->dd_data;
152 DEBUG2(printk("scsi%ld: %s: index [%d] starting conn\n",
153 ddb_entry->ha->host_no, __func__,
154 ddb_entry->fw_ddb_index));
155 iscsi_unblock_session(session);
156 return 0;
159 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag)
161 struct iscsi_cls_session *session;
162 struct ddb_entry *ddb_entry;
164 session = iscsi_dev_to_session(conn->dev.parent);
165 ddb_entry = session->dd_data;
167 DEBUG2(printk("scsi%ld: %s: index [%d] stopping conn\n",
168 ddb_entry->ha->host_no, __func__,
169 ddb_entry->fw_ddb_index));
170 if (flag == STOP_CONN_RECOVER)
171 iscsi_block_session(session);
172 else
173 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
176 static ssize_t format_addr(char *buf, const unsigned char *addr, int len)
178 int i;
179 char *cp = buf;
181 for (i = 0; i < len; i++)
182 cp += sprintf(cp, "%02x%c", addr[i],
183 i == (len - 1) ? '\n' : ':');
184 return cp - buf;
188 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
189 enum iscsi_host_param param, char *buf)
191 struct scsi_qla_host *ha = to_qla_host(shost);
192 int len;
194 switch (param) {
195 case ISCSI_HOST_PARAM_HWADDRESS:
196 len = format_addr(buf, ha->my_mac, MAC_ADDR_LEN);
197 break;
198 case ISCSI_HOST_PARAM_IPADDRESS:
199 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
200 ha->ip_address[1], ha->ip_address[2],
201 ha->ip_address[3]);
202 break;
203 case ISCSI_HOST_PARAM_INITIATOR_NAME:
204 len = sprintf(buf, "%s\n", ha->name_string);
205 break;
206 default:
207 return -ENOSYS;
210 return len;
213 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
214 enum iscsi_param param, char *buf)
216 struct ddb_entry *ddb_entry = sess->dd_data;
217 int len;
219 switch (param) {
220 case ISCSI_PARAM_TARGET_NAME:
221 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
222 ddb_entry->iscsi_name);
223 break;
224 case ISCSI_PARAM_TPGT:
225 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
226 break;
227 default:
228 return -ENOSYS;
231 return len;
234 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
235 enum iscsi_param param, char *buf)
237 struct iscsi_cls_session *session;
238 struct ddb_entry *ddb_entry;
239 int len;
241 session = iscsi_dev_to_session(conn->dev.parent);
242 ddb_entry = session->dd_data;
244 switch (param) {
245 case ISCSI_PARAM_CONN_PORT:
246 len = sprintf(buf, "%hu\n", ddb_entry->port);
247 break;
248 case ISCSI_PARAM_CONN_ADDRESS:
249 /* TODO: what are the ipv6 bits */
250 len = sprintf(buf, "%u.%u.%u.%u\n",
251 NIPQUAD(ddb_entry->ip_addr));
252 break;
253 default:
254 return -ENOSYS;
257 return len;
260 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
261 enum iscsi_tgt_dscvr type, uint32_t enable,
262 struct sockaddr *dst_addr)
264 struct scsi_qla_host *ha;
265 struct sockaddr_in *addr;
266 struct sockaddr_in6 *addr6;
267 int ret = 0;
269 ha = (struct scsi_qla_host *) shost->hostdata;
271 switch (type) {
272 case ISCSI_TGT_DSCVR_SEND_TARGETS:
273 if (dst_addr->sa_family == AF_INET) {
274 addr = (struct sockaddr_in *)dst_addr;
275 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
276 addr->sin_port) != QLA_SUCCESS)
277 ret = -EIO;
278 } else if (dst_addr->sa_family == AF_INET6) {
280 * TODO: fix qla4xxx_send_tgts
282 addr6 = (struct sockaddr_in6 *)dst_addr;
283 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
284 addr6->sin6_port) != QLA_SUCCESS)
285 ret = -EIO;
286 } else
287 ret = -ENOSYS;
288 break;
289 default:
290 ret = -ENOSYS;
292 return ret;
295 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
297 if (!ddb_entry->sess)
298 return;
300 if (ddb_entry->conn) {
301 iscsi_if_destroy_session_done(ddb_entry->conn);
302 iscsi_destroy_conn(ddb_entry->conn);
303 iscsi_remove_session(ddb_entry->sess);
305 iscsi_free_session(ddb_entry->sess);
308 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
310 int err;
312 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
313 if (err) {
314 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
315 return err;
318 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0);
319 if (!ddb_entry->conn) {
320 iscsi_remove_session(ddb_entry->sess);
321 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
322 return -ENOMEM;
325 ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
326 iscsi_if_create_session_done(ddb_entry->conn);
327 return 0;
330 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
332 struct ddb_entry *ddb_entry;
333 struct iscsi_cls_session *sess;
335 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport);
336 if (!sess)
337 return NULL;
339 ddb_entry = sess->dd_data;
340 memset(ddb_entry, 0, sizeof(*ddb_entry));
341 ddb_entry->ha = ha;
342 ddb_entry->sess = sess;
343 return ddb_entry;
347 * Timer routines
350 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
351 unsigned long interval)
353 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
354 __func__, ha->host->host_no));
355 init_timer(&ha->timer);
356 ha->timer.expires = jiffies + interval * HZ;
357 ha->timer.data = (unsigned long)ha;
358 ha->timer.function = (void (*)(unsigned long))func;
359 add_timer(&ha->timer);
360 ha->timer_active = 1;
363 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
365 del_timer_sync(&ha->timer);
366 ha->timer_active = 0;
369 /***
370 * qla4xxx_mark_device_missing - mark a device as missing.
371 * @ha: Pointer to host adapter structure.
372 * @ddb_entry: Pointer to device database entry
374 * This routine marks a device missing and resets the relogin retry count.
376 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
377 struct ddb_entry *ddb_entry)
379 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
380 DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
381 ha->host_no, ddb_entry->bus, ddb_entry->target,
382 ddb_entry->fw_ddb_index));
383 iscsi_conn_error(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
386 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
387 struct ddb_entry *ddb_entry,
388 struct scsi_cmnd *cmd,
389 void (*done)(struct scsi_cmnd *))
391 struct srb *srb;
393 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
394 if (!srb)
395 return srb;
397 atomic_set(&srb->ref_count, 1);
398 srb->ha = ha;
399 srb->ddb = ddb_entry;
400 srb->cmd = cmd;
401 srb->flags = 0;
402 cmd->SCp.ptr = (void *)srb;
403 cmd->scsi_done = done;
405 return srb;
408 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
410 struct scsi_cmnd *cmd = srb->cmd;
412 if (srb->flags & SRB_DMA_VALID) {
413 scsi_dma_unmap(cmd);
414 srb->flags &= ~SRB_DMA_VALID;
416 cmd->SCp.ptr = NULL;
419 void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
421 struct scsi_cmnd *cmd = srb->cmd;
423 qla4xxx_srb_free_dma(ha, srb);
425 mempool_free(srb, ha->srb_mempool);
427 cmd->scsi_done(cmd);
431 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
432 * @cmd: Pointer to Linux's SCSI command structure
433 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
434 * that the command has been processed.
436 * Remarks:
437 * This routine is invoked by Linux to send a SCSI command to the driver.
438 * The mid-level driver tries to ensure that queuecommand never gets
439 * invoked concurrently with itself or the interrupt handler (although
440 * the interrupt handler may call this routine as part of request-
441 * completion handling). Unfortunely, it sometimes calls the scheduler
442 * in interrupt context which is a big NO! NO!.
444 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
445 void (*done)(struct scsi_cmnd *))
447 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
448 struct ddb_entry *ddb_entry = cmd->device->hostdata;
449 struct srb *srb;
450 int rval;
452 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
453 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
454 cmd->result = DID_NO_CONNECT << 16;
455 goto qc_fail_command;
457 goto qc_host_busy;
460 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
461 goto qc_host_busy;
463 spin_unlock_irq(ha->host->host_lock);
465 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
466 if (!srb)
467 goto qc_host_busy_lock;
469 rval = qla4xxx_send_command_to_isp(ha, srb);
470 if (rval != QLA_SUCCESS)
471 goto qc_host_busy_free_sp;
473 spin_lock_irq(ha->host->host_lock);
474 return 0;
476 qc_host_busy_free_sp:
477 qla4xxx_srb_free_dma(ha, srb);
478 mempool_free(srb, ha->srb_mempool);
480 qc_host_busy_lock:
481 spin_lock_irq(ha->host->host_lock);
483 qc_host_busy:
484 return SCSI_MLQUEUE_HOST_BUSY;
486 qc_fail_command:
487 done(cmd);
489 return 0;
493 * qla4xxx_mem_free - frees memory allocated to adapter
494 * @ha: Pointer to host adapter structure.
496 * Frees memory previously allocated by qla4xxx_mem_alloc
498 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
500 if (ha->queues)
501 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
502 ha->queues_dma);
504 ha->queues_len = 0;
505 ha->queues = NULL;
506 ha->queues_dma = 0;
507 ha->request_ring = NULL;
508 ha->request_dma = 0;
509 ha->response_ring = NULL;
510 ha->response_dma = 0;
511 ha->shadow_regs = NULL;
512 ha->shadow_regs_dma = 0;
514 /* Free srb pool. */
515 if (ha->srb_mempool)
516 mempool_destroy(ha->srb_mempool);
518 ha->srb_mempool = NULL;
520 /* release io space registers */
521 if (ha->reg)
522 iounmap(ha->reg);
523 pci_release_regions(ha->pdev);
527 * qla4xxx_mem_alloc - allocates memory for use by adapter.
528 * @ha: Pointer to host adapter structure
530 * Allocates DMA memory for request and response queues. Also allocates memory
531 * for srbs.
533 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
535 unsigned long align;
537 /* Allocate contiguous block of DMA memory for queues. */
538 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
539 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
540 sizeof(struct shadow_regs) +
541 MEM_ALIGN_VALUE +
542 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
543 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
544 &ha->queues_dma, GFP_KERNEL);
545 if (ha->queues == NULL) {
546 dev_warn(&ha->pdev->dev,
547 "Memory Allocation failed - queues.\n");
549 goto mem_alloc_error_exit;
551 memset(ha->queues, 0, ha->queues_len);
554 * As per RISC alignment requirements -- the bus-address must be a
555 * multiple of the request-ring size (in bytes).
557 align = 0;
558 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
559 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
560 (MEM_ALIGN_VALUE - 1));
562 /* Update request and response queue pointers. */
563 ha->request_dma = ha->queues_dma + align;
564 ha->request_ring = (struct queue_entry *) (ha->queues + align);
565 ha->response_dma = ha->queues_dma + align +
566 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
567 ha->response_ring = (struct queue_entry *) (ha->queues + align +
568 (REQUEST_QUEUE_DEPTH *
569 QUEUE_SIZE));
570 ha->shadow_regs_dma = ha->queues_dma + align +
571 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
572 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
573 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
574 (REQUEST_QUEUE_DEPTH *
575 QUEUE_SIZE) +
576 (RESPONSE_QUEUE_DEPTH *
577 QUEUE_SIZE));
579 /* Allocate memory for srb pool. */
580 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
581 mempool_free_slab, srb_cachep);
582 if (ha->srb_mempool == NULL) {
583 dev_warn(&ha->pdev->dev,
584 "Memory Allocation failed - SRB Pool.\n");
586 goto mem_alloc_error_exit;
589 return QLA_SUCCESS;
591 mem_alloc_error_exit:
592 qla4xxx_mem_free(ha);
593 return QLA_ERROR;
597 * qla4xxx_timer - checks every second for work to do.
598 * @ha: Pointer to host adapter structure.
600 static void qla4xxx_timer(struct scsi_qla_host *ha)
602 struct ddb_entry *ddb_entry, *dtemp;
603 int start_dpc = 0;
605 /* Search for relogin's to time-out and port down retry. */
606 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
607 /* Count down time between sending relogins */
608 if (adapter_up(ha) &&
609 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
610 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
611 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
612 INVALID_ENTRY) {
613 if (atomic_read(&ddb_entry->retry_relogin_timer)
614 == 0) {
615 atomic_set(&ddb_entry->
616 retry_relogin_timer,
617 INVALID_ENTRY);
618 set_bit(DPC_RELOGIN_DEVICE,
619 &ha->dpc_flags);
620 set_bit(DF_RELOGIN, &ddb_entry->flags);
621 DEBUG2(printk("scsi%ld: %s: index [%d]"
622 " login device\n",
623 ha->host_no, __func__,
624 ddb_entry->fw_ddb_index));
625 } else
626 atomic_dec(&ddb_entry->
627 retry_relogin_timer);
631 /* Wait for relogin to timeout */
632 if (atomic_read(&ddb_entry->relogin_timer) &&
633 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
635 * If the relogin times out and the device is
636 * still NOT ONLINE then try and relogin again.
638 if (atomic_read(&ddb_entry->state) !=
639 DDB_STATE_ONLINE &&
640 ddb_entry->fw_ddb_device_state ==
641 DDB_DS_SESSION_FAILED) {
642 /* Reset retry relogin timer */
643 atomic_inc(&ddb_entry->relogin_retry_count);
644 DEBUG2(printk("scsi%ld: index[%d] relogin"
645 " timed out-retrying"
646 " relogin (%d)\n",
647 ha->host_no,
648 ddb_entry->fw_ddb_index,
649 atomic_read(&ddb_entry->
650 relogin_retry_count))
652 start_dpc++;
653 DEBUG(printk("scsi%ld:%d:%d: index [%d] "
654 "initate relogin after"
655 " %d seconds\n",
656 ha->host_no, ddb_entry->bus,
657 ddb_entry->target,
658 ddb_entry->fw_ddb_index,
659 ddb_entry->default_time2wait + 4)
662 atomic_set(&ddb_entry->retry_relogin_timer,
663 ddb_entry->default_time2wait + 4);
668 /* Check for heartbeat interval. */
669 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
670 ha->heartbeat_interval != 0) {
671 ha->seconds_since_last_heartbeat++;
672 if (ha->seconds_since_last_heartbeat >
673 ha->heartbeat_interval + 2)
674 set_bit(DPC_RESET_HA, &ha->dpc_flags);
678 /* Wakeup the dpc routine for this adapter, if needed. */
679 if ((start_dpc ||
680 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
681 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
682 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
683 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
684 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
685 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
686 test_bit(DPC_AEN, &ha->dpc_flags)) &&
687 ha->dpc_thread) {
688 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
689 " - dpc flags = 0x%lx\n",
690 ha->host_no, __func__, ha->dpc_flags));
691 queue_work(ha->dpc_thread, &ha->dpc_work);
694 /* Reschedule timer thread to call us back in one second */
695 mod_timer(&ha->timer, jiffies + HZ);
697 DEBUG2(ha->seconds_since_last_intr++);
701 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
702 * @ha: Pointer to host adapter structure.
704 * This routine stalls the driver until all outstanding commands are returned.
705 * Caller must release the Hardware Lock prior to calling this routine.
707 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
709 uint32_t index = 0;
710 int stat = QLA_SUCCESS;
711 unsigned long flags;
712 struct scsi_cmnd *cmd;
713 int wait_cnt = WAIT_CMD_TOV; /*
714 * Initialized for 30 seconds as we
715 * expect all commands to retuned
716 * ASAP.
719 while (wait_cnt) {
720 spin_lock_irqsave(&ha->hardware_lock, flags);
721 /* Find a command that hasn't completed. */
722 for (index = 0; index < ha->host->can_queue; index++) {
723 cmd = scsi_host_find_tag(ha->host, index);
724 if (cmd != NULL)
725 break;
727 spin_unlock_irqrestore(&ha->hardware_lock, flags);
729 /* If No Commands are pending, wait is complete */
730 if (index == ha->host->can_queue) {
731 break;
734 /* If we timed out on waiting for commands to come back
735 * return ERROR.
737 wait_cnt--;
738 if (wait_cnt == 0)
739 stat = QLA_ERROR;
740 else {
741 msleep(1000);
743 } /* End of While (wait_cnt) */
745 return stat;
748 void qla4xxx_hw_reset(struct scsi_qla_host *ha)
750 uint32_t ctrl_status;
751 unsigned long flags = 0;
753 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
755 spin_lock_irqsave(&ha->hardware_lock, flags);
758 * If the SCSI Reset Interrupt bit is set, clear it.
759 * Otherwise, the Soft Reset won't work.
761 ctrl_status = readw(&ha->reg->ctrl_status);
762 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
763 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
765 /* Issue Soft Reset */
766 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
767 readl(&ha->reg->ctrl_status);
769 spin_unlock_irqrestore(&ha->hardware_lock, flags);
773 * qla4xxx_soft_reset - performs soft reset.
774 * @ha: Pointer to host adapter structure.
776 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
778 uint32_t max_wait_time;
779 unsigned long flags = 0;
780 int status = QLA_ERROR;
781 uint32_t ctrl_status;
783 qla4xxx_hw_reset(ha);
785 /* Wait until the Network Reset Intr bit is cleared */
786 max_wait_time = RESET_INTR_TOV;
787 do {
788 spin_lock_irqsave(&ha->hardware_lock, flags);
789 ctrl_status = readw(&ha->reg->ctrl_status);
790 spin_unlock_irqrestore(&ha->hardware_lock, flags);
792 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
793 break;
795 msleep(1000);
796 } while ((--max_wait_time));
798 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
799 DEBUG2(printk(KERN_WARNING
800 "scsi%ld: Network Reset Intr not cleared by "
801 "Network function, clearing it now!\n",
802 ha->host_no));
803 spin_lock_irqsave(&ha->hardware_lock, flags);
804 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
805 readl(&ha->reg->ctrl_status);
806 spin_unlock_irqrestore(&ha->hardware_lock, flags);
809 /* Wait until the firmware tells us the Soft Reset is done */
810 max_wait_time = SOFT_RESET_TOV;
811 do {
812 spin_lock_irqsave(&ha->hardware_lock, flags);
813 ctrl_status = readw(&ha->reg->ctrl_status);
814 spin_unlock_irqrestore(&ha->hardware_lock, flags);
816 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
817 status = QLA_SUCCESS;
818 break;
821 msleep(1000);
822 } while ((--max_wait_time));
825 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
826 * after the soft reset has taken place.
828 spin_lock_irqsave(&ha->hardware_lock, flags);
829 ctrl_status = readw(&ha->reg->ctrl_status);
830 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
831 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
832 readl(&ha->reg->ctrl_status);
834 spin_unlock_irqrestore(&ha->hardware_lock, flags);
836 /* If soft reset fails then most probably the bios on other
837 * function is also enabled.
838 * Since the initialization is sequential the other fn
839 * wont be able to acknowledge the soft reset.
840 * Issue a force soft reset to workaround this scenario.
842 if (max_wait_time == 0) {
843 /* Issue Force Soft Reset */
844 spin_lock_irqsave(&ha->hardware_lock, flags);
845 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
846 readl(&ha->reg->ctrl_status);
847 spin_unlock_irqrestore(&ha->hardware_lock, flags);
848 /* Wait until the firmware tells us the Soft Reset is done */
849 max_wait_time = SOFT_RESET_TOV;
850 do {
851 spin_lock_irqsave(&ha->hardware_lock, flags);
852 ctrl_status = readw(&ha->reg->ctrl_status);
853 spin_unlock_irqrestore(&ha->hardware_lock, flags);
855 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
856 status = QLA_SUCCESS;
857 break;
860 msleep(1000);
861 } while ((--max_wait_time));
864 return status;
868 * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
869 * @ha: Pointer to host adapter structure.
871 * This routine is called just prior to a HARD RESET to return all
872 * outstanding commands back to the Operating System.
873 * Caller should make sure that the following locks are released
874 * before this calling routine: Hardware lock, and io_request_lock.
876 static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
878 struct srb *srb;
879 int i;
880 unsigned long flags;
882 spin_lock_irqsave(&ha->hardware_lock, flags);
883 for (i = 0; i < ha->host->can_queue; i++) {
884 srb = qla4xxx_del_from_active_array(ha, i);
885 if (srb != NULL) {
886 srb->cmd->result = DID_RESET << 16;
887 qla4xxx_srb_compl(ha, srb);
890 spin_unlock_irqrestore(&ha->hardware_lock, flags);
895 * qla4xxx_recover_adapter - recovers adapter after a fatal error
896 * @ha: Pointer to host adapter structure.
897 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
898 * after adapter recovery has completed.
899 * 0=preserve ddb list, 1=destroy and rebuild ddb list
901 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
902 uint8_t renew_ddb_list)
904 int status;
906 /* Stall incoming I/O until we are done */
907 clear_bit(AF_ONLINE, &ha->flags);
908 DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
909 __func__));
911 /* Wait for outstanding commands to complete.
912 * Stalls the driver for max 30 secs
914 status = qla4xxx_cmd_wait(ha);
916 qla4xxx_disable_intrs(ha);
918 /* Flush any pending ddb changed AENs */
919 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
921 /* Reset the firmware. If successful, function
922 * returns with ISP interrupts enabled.
924 if (status == QLA_SUCCESS) {
925 DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
926 ha->host_no, __func__));
927 qla4xxx_flush_active_srbs(ha);
928 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
929 status = qla4xxx_soft_reset(ha);
930 else
931 status = QLA_ERROR;
934 /* Flush any pending ddb changed AENs */
935 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
937 /* Re-initialize firmware. If successful, function returns
938 * with ISP interrupts enabled */
939 if (status == QLA_SUCCESS) {
940 DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
941 ha->host_no, __func__));
943 /* If successful, AF_ONLINE flag set in
944 * qla4xxx_initialize_adapter */
945 status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
948 /* Failed adapter initialization?
949 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
950 if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
951 (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
952 /* Adapter initialization failed, see if we can retry
953 * resetting the ha */
954 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
955 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
956 DEBUG2(printk("scsi%ld: recover adapter - retrying "
957 "(%d) more times\n", ha->host_no,
958 ha->retry_reset_ha_cnt));
959 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
960 status = QLA_ERROR;
961 } else {
962 if (ha->retry_reset_ha_cnt > 0) {
963 /* Schedule another Reset HA--DPC will retry */
964 ha->retry_reset_ha_cnt--;
965 DEBUG2(printk("scsi%ld: recover adapter - "
966 "retry remaining %d\n",
967 ha->host_no,
968 ha->retry_reset_ha_cnt));
969 status = QLA_ERROR;
972 if (ha->retry_reset_ha_cnt == 0) {
973 /* Recover adapter retries have been exhausted.
974 * Adapter DEAD */
975 DEBUG2(printk("scsi%ld: recover adapter "
976 "failed - board disabled\n",
977 ha->host_no));
978 qla4xxx_flush_active_srbs(ha);
979 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
980 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
981 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
982 &ha->dpc_flags);
983 status = QLA_ERROR;
986 } else {
987 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
988 clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
989 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
992 ha->adapter_error_count++;
994 if (status == QLA_SUCCESS)
995 qla4xxx_enable_intrs(ha);
997 DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
998 return status;
1002 * qla4xxx_do_dpc - dpc routine
1003 * @data: in our case pointer to adapter structure
1005 * This routine is a task that is schedule by the interrupt handler
1006 * to perform the background processing for interrupts. We put it
1007 * on a task queue that is consumed whenever the scheduler runs; that's
1008 * so you can do anything (i.e. put the process to sleep etc). In fact,
1009 * the mid-level tries to sleep when it reaches the driver threshold
1010 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1012 static void qla4xxx_do_dpc(struct work_struct *work)
1014 struct scsi_qla_host *ha =
1015 container_of(work, struct scsi_qla_host, dpc_work);
1016 struct ddb_entry *ddb_entry, *dtemp;
1017 int status = QLA_ERROR;
1019 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1020 "flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
1021 ha->host_no, __func__, ha->flags, ha->dpc_flags,
1022 readw(&ha->reg->ctrl_status)));
1024 /* Initialization not yet finished. Don't do anything yet. */
1025 if (!test_bit(AF_INIT_DONE, &ha->flags))
1026 return;
1028 if (adapter_up(ha) ||
1029 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1030 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1031 test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
1032 if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
1033 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1034 qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1036 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1037 uint8_t wait_time = RESET_INTR_TOV;
1039 while ((readw(&ha->reg->ctrl_status) &
1040 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1041 if (--wait_time == 0)
1042 break;
1043 msleep(1000);
1045 if (wait_time == 0)
1046 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1047 "bit not cleared-- resetting\n",
1048 ha->host_no, __func__));
1049 qla4xxx_flush_active_srbs(ha);
1050 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1051 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1052 status = qla4xxx_initialize_adapter(ha,
1053 PRESERVE_DDB_LIST);
1055 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1056 if (status == QLA_SUCCESS)
1057 qla4xxx_enable_intrs(ha);
1061 /* ---- process AEN? --- */
1062 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1063 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1065 /* ---- Get DHCP IP Address? --- */
1066 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1067 qla4xxx_get_dhcp_ip_address(ha);
1069 /* ---- relogin device? --- */
1070 if (adapter_up(ha) &&
1071 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1072 list_for_each_entry_safe(ddb_entry, dtemp,
1073 &ha->ddb_list, list) {
1074 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1075 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1076 qla4xxx_relogin_device(ha, ddb_entry);
1079 * If mbx cmd times out there is no point
1080 * in continuing further.
1081 * With large no of targets this can hang
1082 * the system.
1084 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1085 printk(KERN_WARNING "scsi%ld: %s: "
1086 "need to reset hba\n",
1087 ha->host_no, __func__);
1088 break;
1095 * qla4xxx_free_adapter - release the adapter
1096 * @ha: pointer to adapter structure
1098 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1101 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1102 /* Turn-off interrupts on the card. */
1103 qla4xxx_disable_intrs(ha);
1106 /* Kill the kernel thread for this host */
1107 if (ha->dpc_thread)
1108 destroy_workqueue(ha->dpc_thread);
1110 /* Issue Soft Reset to put firmware in unknown state */
1111 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
1112 qla4xxx_hw_reset(ha);
1114 /* Remove timer thread, if present */
1115 if (ha->timer_active)
1116 qla4xxx_stop_timer(ha);
1118 /* Detach interrupts */
1119 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1120 free_irq(ha->pdev->irq, ha);
1122 /* free extra memory */
1123 qla4xxx_mem_free(ha);
1125 pci_disable_device(ha->pdev);
1129 /***
1130 * qla4xxx_iospace_config - maps registers
1131 * @ha: pointer to adapter structure
1133 * This routines maps HBA's registers from the pci address space
1134 * into the kernel virtual address space for memory mapped i/o.
1136 static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1138 unsigned long pio, pio_len, pio_flags;
1139 unsigned long mmio, mmio_len, mmio_flags;
1141 pio = pci_resource_start(ha->pdev, 0);
1142 pio_len = pci_resource_len(ha->pdev, 0);
1143 pio_flags = pci_resource_flags(ha->pdev, 0);
1144 if (pio_flags & IORESOURCE_IO) {
1145 if (pio_len < MIN_IOBASE_LEN) {
1146 dev_warn(&ha->pdev->dev,
1147 "Invalid PCI I/O region size\n");
1148 pio = 0;
1150 } else {
1151 dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1152 pio = 0;
1155 /* Use MMIO operations for all accesses. */
1156 mmio = pci_resource_start(ha->pdev, 1);
1157 mmio_len = pci_resource_len(ha->pdev, 1);
1158 mmio_flags = pci_resource_flags(ha->pdev, 1);
1160 if (!(mmio_flags & IORESOURCE_MEM)) {
1161 dev_err(&ha->pdev->dev,
1162 "region #0 not an MMIO resource, aborting\n");
1164 goto iospace_error_exit;
1166 if (mmio_len < MIN_IOBASE_LEN) {
1167 dev_err(&ha->pdev->dev,
1168 "Invalid PCI mem region size, aborting\n");
1169 goto iospace_error_exit;
1172 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1173 dev_warn(&ha->pdev->dev,
1174 "Failed to reserve PIO/MMIO regions\n");
1176 goto iospace_error_exit;
1179 ha->pio_address = pio;
1180 ha->pio_length = pio_len;
1181 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1182 if (!ha->reg) {
1183 dev_err(&ha->pdev->dev,
1184 "cannot remap MMIO, aborting\n");
1186 goto iospace_error_exit;
1189 return 0;
1191 iospace_error_exit:
1192 return -ENOMEM;
1196 * qla4xxx_probe_adapter - callback function to probe HBA
1197 * @pdev: pointer to pci_dev structure
1198 * @pci_device_id: pointer to pci_device entry
1200 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1201 * It returns zero if successful. It also initializes all data necessary for
1202 * the driver.
1204 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1205 const struct pci_device_id *ent)
1207 int ret = -ENODEV, status;
1208 struct Scsi_Host *host;
1209 struct scsi_qla_host *ha;
1210 struct ddb_entry *ddb_entry, *ddbtemp;
1211 uint8_t init_retry_count = 0;
1212 char buf[34];
1214 if (pci_enable_device(pdev))
1215 return -1;
1217 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1218 if (host == NULL) {
1219 printk(KERN_WARNING
1220 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1221 goto probe_disable_device;
1224 /* Clear our data area */
1225 ha = (struct scsi_qla_host *) host->hostdata;
1226 memset(ha, 0, sizeof(*ha));
1228 /* Save the information from PCI BIOS. */
1229 ha->pdev = pdev;
1230 ha->host = host;
1231 ha->host_no = host->host_no;
1233 /* Configure PCI I/O space. */
1234 ret = qla4xxx_iospace_config(ha);
1235 if (ret)
1236 goto probe_failed;
1238 dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1239 pdev->device, pdev->irq, ha->reg);
1241 qla4xxx_config_dma_addressing(ha);
1243 /* Initialize lists and spinlocks. */
1244 INIT_LIST_HEAD(&ha->ddb_list);
1245 INIT_LIST_HEAD(&ha->free_srb_q);
1247 mutex_init(&ha->mbox_sem);
1249 spin_lock_init(&ha->hardware_lock);
1251 /* Allocate dma buffers */
1252 if (qla4xxx_mem_alloc(ha)) {
1253 dev_warn(&ha->pdev->dev,
1254 "[ERROR] Failed to allocate memory for adapter\n");
1256 ret = -ENOMEM;
1257 goto probe_failed;
1261 * Initialize the Host adapter request/response queues and
1262 * firmware
1263 * NOTE: interrupts enabled upon successful completion
1265 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1266 while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1267 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1268 "(%d)\n", __func__, init_retry_count));
1269 qla4xxx_soft_reset(ha);
1270 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1272 if (status == QLA_ERROR) {
1273 dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1275 ret = -ENODEV;
1276 goto probe_failed;
1279 host->cmd_per_lun = 3;
1280 host->max_channel = 0;
1281 host->max_lun = MAX_LUNS - 1;
1282 host->max_id = MAX_TARGETS;
1283 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1284 host->can_queue = MAX_SRBS ;
1285 host->transportt = qla4xxx_scsi_transport;
1287 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1288 if (ret) {
1289 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed\n");
1290 goto probe_failed;
1293 /* Startup the kernel thread for this host adapter. */
1294 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1295 "qla4xxx_dpc\n", __func__));
1296 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1297 ha->dpc_thread = create_singlethread_workqueue(buf);
1298 if (!ha->dpc_thread) {
1299 dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1300 ret = -ENODEV;
1301 goto probe_failed;
1303 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1305 ret = request_irq(pdev->irq, qla4xxx_intr_handler,
1306 IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
1307 if (ret) {
1308 dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1309 " already in use.\n", pdev->irq);
1310 goto probe_failed;
1312 set_bit(AF_IRQ_ATTACHED, &ha->flags);
1313 host->irq = pdev->irq;
1314 DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1316 qla4xxx_enable_intrs(ha);
1318 /* Start timer thread. */
1319 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1321 set_bit(AF_INIT_DONE, &ha->flags);
1323 pci_set_drvdata(pdev, ha);
1325 ret = scsi_add_host(host, &pdev->dev);
1326 if (ret)
1327 goto probe_failed;
1329 /* Update transport device information for all devices. */
1330 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
1331 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
1332 if (qla4xxx_add_sess(ddb_entry))
1333 goto remove_host;
1336 printk(KERN_INFO
1337 " QLogic iSCSI HBA Driver version: %s\n"
1338 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1339 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1340 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1341 ha->patch_number, ha->build_number);
1343 return 0;
1345 remove_host:
1346 qla4xxx_free_ddb_list(ha);
1347 scsi_remove_host(host);
1349 probe_failed:
1350 qla4xxx_free_adapter(ha);
1351 scsi_host_put(ha->host);
1353 probe_disable_device:
1354 pci_disable_device(pdev);
1356 return ret;
1360 * qla4xxx_remove_adapter - calback function to remove adapter.
1361 * @pci_dev: PCI device pointer
1363 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1365 struct scsi_qla_host *ha;
1367 ha = pci_get_drvdata(pdev);
1369 qla4xxx_disable_intrs(ha);
1371 while (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
1372 ssleep(1);
1374 /* remove devs from iscsi_sessions to scsi_devices */
1375 qla4xxx_free_ddb_list(ha);
1377 scsi_remove_host(ha->host);
1379 qla4xxx_free_adapter(ha);
1381 scsi_host_put(ha->host);
1383 pci_set_drvdata(pdev, NULL);
1387 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1388 * @ha: HA context
1390 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1391 * supported addressing method.
1393 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1395 int retval;
1397 /* Update our PCI device dma_mask for full 64 bit mask */
1398 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1399 if (pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1400 dev_dbg(&ha->pdev->dev,
1401 "Failed to set 64 bit PCI consistent mask; "
1402 "using 32 bit.\n");
1403 retval = pci_set_consistent_dma_mask(ha->pdev,
1404 DMA_32BIT_MASK);
1406 } else
1407 retval = pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1410 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1412 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1413 struct ddb_entry *ddb = sess->dd_data;
1415 sdev->hostdata = ddb;
1416 sdev->tagged_supported = 1;
1417 scsi_activate_tcq(sdev, sdev->host->can_queue);
1418 return 0;
1421 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1423 sdev->tagged_supported = 1;
1424 return 0;
1427 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1429 scsi_deactivate_tcq(sdev, 1);
1433 * qla4xxx_del_from_active_array - returns an active srb
1434 * @ha: Pointer to host adapter structure.
1435 * @index: index into to the active_array
1437 * This routine removes and returns the srb at the specified index
1439 struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1441 struct srb *srb = NULL;
1442 struct scsi_cmnd *cmd;
1444 if (!(cmd = scsi_host_find_tag(ha->host, index)))
1445 return srb;
1447 if (!(srb = (struct srb *)cmd->host_scribble))
1448 return srb;
1450 /* update counters */
1451 if (srb->flags & SRB_DMA_VALID) {
1452 ha->req_q_count += srb->iocb_cnt;
1453 ha->iocb_cnt -= srb->iocb_cnt;
1454 if (srb->cmd)
1455 srb->cmd->host_scribble = NULL;
1457 return srb;
1461 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1462 * @ha: actual ha whose done queue will contain the comd returned by firmware.
1463 * @cmd: Scsi Command to wait on.
1465 * This routine waits for the command to be returned by the Firmware
1466 * for some max time.
1468 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1469 struct scsi_cmnd *cmd)
1471 int done = 0;
1472 struct srb *rp;
1473 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1475 do {
1476 /* Checking to see if its returned to OS */
1477 rp = (struct srb *) cmd->SCp.ptr;
1478 if (rp == NULL) {
1479 done++;
1480 break;
1483 msleep(2000);
1484 } while (max_wait_time--);
1486 return done;
1490 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1491 * @ha: Pointer to host adapter structure
1493 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1495 unsigned long wait_online;
1497 wait_online = jiffies + (30 * HZ);
1498 while (time_before(jiffies, wait_online)) {
1500 if (adapter_up(ha))
1501 return QLA_SUCCESS;
1502 else if (ha->retry_reset_ha_cnt == 0)
1503 return QLA_ERROR;
1505 msleep(2000);
1508 return QLA_ERROR;
1512 * qla4xxx_eh_wait_for_active_target_commands - wait for active cmds to finish.
1513 * @ha: pointer to to HBA
1514 * @t: target id
1515 * @l: lun id
1517 * This function waits for all outstanding commands to a lun to complete. It
1518 * returns 0 if all pending commands are returned and 1 otherwise.
1520 static int qla4xxx_eh_wait_for_active_target_commands(struct scsi_qla_host *ha,
1521 int t, int l)
1523 int cnt;
1524 int status = 0;
1525 struct scsi_cmnd *cmd;
1528 * Waiting for all commands for the designated target in the active
1529 * array
1531 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1532 cmd = scsi_host_find_tag(ha->host, cnt);
1533 if (cmd && cmd->device->id == t && cmd->device->lun == l) {
1534 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1535 status++;
1536 break;
1540 return status;
1544 * qla4xxx_eh_device_reset - callback for target reset.
1545 * @cmd: Pointer to Linux's SCSI command structure
1547 * This routine is called by the Linux OS to reset all luns on the
1548 * specified target.
1550 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1552 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1553 struct ddb_entry *ddb_entry = cmd->device->hostdata;
1554 struct srb *sp;
1555 int ret = FAILED, stat;
1557 sp = (struct srb *) cmd->SCp.ptr;
1558 if (!sp || !ddb_entry)
1559 return ret;
1561 dev_info(&ha->pdev->dev,
1562 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1563 cmd->device->channel, cmd->device->id, cmd->device->lun);
1565 DEBUG2(printk(KERN_INFO
1566 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1567 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
1568 cmd, jiffies, cmd->timeout_per_command / HZ,
1569 ha->dpc_flags, cmd->result, cmd->allowed));
1571 /* FIXME: wait for hba to go online */
1572 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1573 if (stat != QLA_SUCCESS) {
1574 dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1575 goto eh_dev_reset_done;
1578 /* Send marker. */
1579 ha->marker_needed = 1;
1582 * If we are coming down the EH path, wait for all commands to complete
1583 * for the device.
1585 if (cmd->device->host->shost_state == SHOST_RECOVERY) {
1586 if (qla4xxx_eh_wait_for_active_target_commands(ha,
1587 cmd->device->id,
1588 cmd->device->lun)){
1589 dev_info(&ha->pdev->dev,
1590 "DEVICE RESET FAILED - waiting for "
1591 "commands.\n");
1592 goto eh_dev_reset_done;
1596 dev_info(&ha->pdev->dev,
1597 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1598 ha->host_no, cmd->device->channel, cmd->device->id,
1599 cmd->device->lun);
1601 ret = SUCCESS;
1603 eh_dev_reset_done:
1605 return ret;
1609 * qla4xxx_eh_host_reset - kernel callback
1610 * @cmd: Pointer to Linux's SCSI command structure
1612 * This routine is invoked by the Linux kernel to perform fatal error
1613 * recovery on the specified adapter.
1615 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1617 int return_status = FAILED;
1618 struct scsi_qla_host *ha;
1620 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1622 dev_info(&ha->pdev->dev,
1623 "scsi(%ld:%d:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no,
1624 cmd->device->channel, cmd->device->id, cmd->device->lun);
1626 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1627 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
1628 "DEAD.\n", ha->host_no, cmd->device->channel,
1629 __func__));
1631 return FAILED;
1634 if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS) {
1635 return_status = SUCCESS;
1638 dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1639 return_status == FAILED ? "FAILED" : "SUCCEDED");
1641 return return_status;
1645 static struct pci_device_id qla4xxx_pci_tbl[] = {
1647 .vendor = PCI_VENDOR_ID_QLOGIC,
1648 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
1649 .subvendor = PCI_ANY_ID,
1650 .subdevice = PCI_ANY_ID,
1653 .vendor = PCI_VENDOR_ID_QLOGIC,
1654 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
1655 .subvendor = PCI_ANY_ID,
1656 .subdevice = PCI_ANY_ID,
1659 .vendor = PCI_VENDOR_ID_QLOGIC,
1660 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
1661 .subvendor = PCI_ANY_ID,
1662 .subdevice = PCI_ANY_ID,
1664 {0, 0},
1666 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1668 static struct pci_driver qla4xxx_pci_driver = {
1669 .name = DRIVER_NAME,
1670 .id_table = qla4xxx_pci_tbl,
1671 .probe = qla4xxx_probe_adapter,
1672 .remove = qla4xxx_remove_adapter,
1675 static int __init qla4xxx_module_init(void)
1677 int ret;
1679 /* Allocate cache for SRBs. */
1680 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
1681 SLAB_HWCACHE_ALIGN, NULL);
1682 if (srb_cachep == NULL) {
1683 printk(KERN_ERR
1684 "%s: Unable to allocate SRB cache..."
1685 "Failing load!\n", DRIVER_NAME);
1686 ret = -ENOMEM;
1687 goto no_srp_cache;
1690 /* Derive version string. */
1691 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
1692 if (ql4xextended_error_logging)
1693 strcat(qla4xxx_version_str, "-debug");
1695 qla4xxx_scsi_transport =
1696 iscsi_register_transport(&qla4xxx_iscsi_transport);
1697 if (!qla4xxx_scsi_transport){
1698 ret = -ENODEV;
1699 goto release_srb_cache;
1702 ret = pci_register_driver(&qla4xxx_pci_driver);
1703 if (ret)
1704 goto unregister_transport;
1706 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1707 return 0;
1709 unregister_transport:
1710 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1711 release_srb_cache:
1712 kmem_cache_destroy(srb_cachep);
1713 no_srp_cache:
1714 return ret;
1717 static void __exit qla4xxx_module_exit(void)
1719 ql4_mod_unload = 1;
1720 pci_unregister_driver(&qla4xxx_pci_driver);
1721 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1722 kmem_cache_destroy(srb_cachep);
1725 module_init(qla4xxx_module_init);
1726 module_exit(qla4xxx_module_exit);
1728 MODULE_AUTHOR("QLogic Corporation");
1729 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1730 MODULE_LICENSE("GPL");
1731 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);