include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / scsi / qla4xxx / ql4_os.c
blobf2364ec59f0349e43950fd2a4585ba7f2c26838c
1 /*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2010 QLogic Corporation
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7 #include <linux/moduleparam.h>
8 #include <linux/slab.h>
10 #include <scsi/scsi_tcq.h>
11 #include <scsi/scsicam.h>
13 #include "ql4_def.h"
14 #include "ql4_version.h"
15 #include "ql4_glbl.h"
16 #include "ql4_dbg.h"
17 #include "ql4_inline.h"
20 * Driver version
22 static char qla4xxx_version_str[40];
25 * SRB allocation cache
27 static struct kmem_cache *srb_cachep;
30 * Module parameter information and variables
32 int ql4xdontresethba = 0;
33 module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(ql4xdontresethba,
35 "Don't reset the HBA for driver recovery \n"
36 " 0 - It will reset HBA (Default)\n"
37 " 1 - It will NOT reset HBA");
39 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
40 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
41 MODULE_PARM_DESC(ql4xextended_error_logging,
42 "Option to enable extended error logging, "
43 "Default is 0 - no logging, 1 - debug logging");
45 int ql4xenablemsix = 1;
46 module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
47 MODULE_PARM_DESC(ql4xenablemsix,
48 "Set to enable MSI or MSI-X interrupt mechanism.\n"
49 " 0 = enable INTx interrupt mechanism.\n"
50 " 1 = enable MSI-X interrupt mechanism (Default).\n"
51 " 2 = enable MSI interrupt mechanism.");
53 #define QL4_DEF_QDEPTH 32
54 static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
55 module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(ql4xmaxqdepth,
57 "Maximum queue depth to report for target devices.\n"
58 " Default: 32.");
60 static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
61 module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
62 MODULE_PARM_DESC(ql4xsess_recovery_tmo,
63 "Target Session Recovery Timeout.\n"
64 " Default: 30 sec.");
67 * SCSI host template entry points
69 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
72 * iSCSI template entry points
74 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
75 enum iscsi_tgt_dscvr type, uint32_t enable,
76 struct sockaddr *dst_addr);
77 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
78 enum iscsi_param param, char *buf);
79 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
80 enum iscsi_param param, char *buf);
81 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
82 enum iscsi_host_param param, char *buf);
83 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
84 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
87 * SCSI host template entry points
89 static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
90 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
91 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
92 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
93 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
94 static int qla4xxx_slave_alloc(struct scsi_device *device);
95 static int qla4xxx_slave_configure(struct scsi_device *device);
96 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
97 static void qla4xxx_scan_start(struct Scsi_Host *shost);
99 static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
100 QLA82XX_LEGACY_INTR_CONFIG;
102 static struct scsi_host_template qla4xxx_driver_template = {
103 .module = THIS_MODULE,
104 .name = DRIVER_NAME,
105 .proc_name = DRIVER_NAME,
106 .queuecommand = qla4xxx_queuecommand,
108 .eh_abort_handler = qla4xxx_eh_abort,
109 .eh_device_reset_handler = qla4xxx_eh_device_reset,
110 .eh_target_reset_handler = qla4xxx_eh_target_reset,
111 .eh_host_reset_handler = qla4xxx_eh_host_reset,
112 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
114 .slave_configure = qla4xxx_slave_configure,
115 .slave_alloc = qla4xxx_slave_alloc,
116 .slave_destroy = qla4xxx_slave_destroy,
118 .scan_finished = iscsi_scan_finished,
119 .scan_start = qla4xxx_scan_start,
121 .this_id = -1,
122 .cmd_per_lun = 3,
123 .use_clustering = ENABLE_CLUSTERING,
124 .sg_tablesize = SG_ALL,
126 .max_sectors = 0xFFFF,
127 .shost_attrs = qla4xxx_host_attrs,
130 static struct iscsi_transport qla4xxx_iscsi_transport = {
131 .owner = THIS_MODULE,
132 .name = DRIVER_NAME,
133 .caps = CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
134 CAP_DATA_PATH_OFFLOAD,
135 .param_mask = ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
136 ISCSI_TARGET_NAME | ISCSI_TPGT |
137 ISCSI_TARGET_ALIAS,
138 .host_param_mask = ISCSI_HOST_HWADDRESS |
139 ISCSI_HOST_IPADDRESS |
140 ISCSI_HOST_INITIATOR_NAME,
141 .tgt_dscvr = qla4xxx_tgt_dscvr,
142 .get_conn_param = qla4xxx_conn_get_param,
143 .get_session_param = qla4xxx_sess_get_param,
144 .get_host_param = qla4xxx_host_get_param,
145 .session_recovery_timedout = qla4xxx_recovery_timedout,
148 static struct scsi_transport_template *qla4xxx_scsi_transport;
150 static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
152 struct iscsi_cls_session *session;
153 struct ddb_entry *ddb_entry;
155 session = starget_to_session(scsi_target(sc->device));
156 ddb_entry = session->dd_data;
158 /* if we are not logged in then the LLD is going to clean up the cmd */
159 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
160 return BLK_EH_RESET_TIMER;
161 else
162 return BLK_EH_NOT_HANDLED;
165 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
167 struct ddb_entry *ddb_entry = session->dd_data;
168 struct scsi_qla_host *ha = ddb_entry->ha;
170 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
171 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
173 DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
174 "of (%d) secs exhausted, marking device DEAD.\n",
175 ha->host_no, __func__, ddb_entry->fw_ddb_index,
176 ddb_entry->sess->recovery_tmo));
180 static int qla4xxx_host_get_param(struct Scsi_Host *shost,
181 enum iscsi_host_param param, char *buf)
183 struct scsi_qla_host *ha = to_qla_host(shost);
184 int len;
186 switch (param) {
187 case ISCSI_HOST_PARAM_HWADDRESS:
188 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
189 break;
190 case ISCSI_HOST_PARAM_IPADDRESS:
191 len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
192 ha->ip_address[1], ha->ip_address[2],
193 ha->ip_address[3]);
194 break;
195 case ISCSI_HOST_PARAM_INITIATOR_NAME:
196 len = sprintf(buf, "%s\n", ha->name_string);
197 break;
198 default:
199 return -ENOSYS;
202 return len;
205 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
206 enum iscsi_param param, char *buf)
208 struct ddb_entry *ddb_entry = sess->dd_data;
209 int len;
211 switch (param) {
212 case ISCSI_PARAM_TARGET_NAME:
213 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
214 ddb_entry->iscsi_name);
215 break;
216 case ISCSI_PARAM_TPGT:
217 len = sprintf(buf, "%u\n", ddb_entry->tpgt);
218 break;
219 case ISCSI_PARAM_TARGET_ALIAS:
220 len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
221 ddb_entry->iscsi_alias);
222 break;
223 default:
224 return -ENOSYS;
227 return len;
230 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
231 enum iscsi_param param, char *buf)
233 struct iscsi_cls_session *session;
234 struct ddb_entry *ddb_entry;
235 int len;
237 session = iscsi_dev_to_session(conn->dev.parent);
238 ddb_entry = session->dd_data;
240 switch (param) {
241 case ISCSI_PARAM_CONN_PORT:
242 len = sprintf(buf, "%hu\n", ddb_entry->port);
243 break;
244 case ISCSI_PARAM_CONN_ADDRESS:
245 /* TODO: what are the ipv6 bits */
246 len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
247 break;
248 default:
249 return -ENOSYS;
252 return len;
255 static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
256 enum iscsi_tgt_dscvr type, uint32_t enable,
257 struct sockaddr *dst_addr)
259 struct scsi_qla_host *ha;
260 struct sockaddr_in *addr;
261 struct sockaddr_in6 *addr6;
262 int ret = 0;
264 ha = (struct scsi_qla_host *) shost->hostdata;
266 switch (type) {
267 case ISCSI_TGT_DSCVR_SEND_TARGETS:
268 if (dst_addr->sa_family == AF_INET) {
269 addr = (struct sockaddr_in *)dst_addr;
270 if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
271 addr->sin_port) != QLA_SUCCESS)
272 ret = -EIO;
273 } else if (dst_addr->sa_family == AF_INET6) {
275 * TODO: fix qla4xxx_send_tgts
277 addr6 = (struct sockaddr_in6 *)dst_addr;
278 if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
279 addr6->sin6_port) != QLA_SUCCESS)
280 ret = -EIO;
281 } else
282 ret = -ENOSYS;
283 break;
284 default:
285 ret = -ENOSYS;
287 return ret;
290 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
292 if (!ddb_entry->sess)
293 return;
295 if (ddb_entry->conn) {
296 atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
297 iscsi_remove_session(ddb_entry->sess);
299 iscsi_free_session(ddb_entry->sess);
302 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
304 int err;
306 ddb_entry->sess->recovery_tmo = ql4xsess_recovery_tmo;
308 err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
309 if (err) {
310 DEBUG2(printk(KERN_ERR "Could not add session.\n"));
311 return err;
314 ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
315 if (!ddb_entry->conn) {
316 iscsi_remove_session(ddb_entry->sess);
317 DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
318 return -ENOMEM;
321 /* finally ready to go */
322 iscsi_unblock_session(ddb_entry->sess);
323 return 0;
326 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
328 struct ddb_entry *ddb_entry;
329 struct iscsi_cls_session *sess;
331 sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
332 sizeof(struct ddb_entry));
333 if (!sess)
334 return NULL;
336 ddb_entry = sess->dd_data;
337 memset(ddb_entry, 0, sizeof(*ddb_entry));
338 ddb_entry->ha = ha;
339 ddb_entry->sess = sess;
340 return ddb_entry;
343 static void qla4xxx_scan_start(struct Scsi_Host *shost)
345 struct scsi_qla_host *ha = shost_priv(shost);
346 struct ddb_entry *ddb_entry, *ddbtemp;
348 /* finish setup of sessions that were already setup in firmware */
349 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
350 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
351 qla4xxx_add_sess(ddb_entry);
356 * Timer routines
359 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
360 unsigned long interval)
362 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
363 __func__, ha->host->host_no));
364 init_timer(&ha->timer);
365 ha->timer.expires = jiffies + interval * HZ;
366 ha->timer.data = (unsigned long)ha;
367 ha->timer.function = (void (*)(unsigned long))func;
368 add_timer(&ha->timer);
369 ha->timer_active = 1;
372 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
374 del_timer_sync(&ha->timer);
375 ha->timer_active = 0;
378 /***
379 * qla4xxx_mark_device_missing - mark a device as missing.
380 * @ha: Pointer to host adapter structure.
381 * @ddb_entry: Pointer to device database entry
383 * This routine marks a device missing and close connection.
385 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
386 struct ddb_entry *ddb_entry)
388 if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
389 atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
390 DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
391 ha->host_no, ddb_entry->fw_ddb_index));
392 } else
393 DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
394 ddb_entry->fw_ddb_index))
396 iscsi_block_session(ddb_entry->sess);
397 iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
401 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
402 * @ha: Pointer to host adapter structure.
404 * This routine marks a device missing and resets the relogin retry count.
406 void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
408 struct ddb_entry *ddb_entry, *ddbtemp;
409 list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
410 qla4xxx_mark_device_missing(ha, ddb_entry);
414 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
415 struct ddb_entry *ddb_entry,
416 struct scsi_cmnd *cmd)
418 struct srb *srb;
420 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
421 if (!srb)
422 return srb;
424 kref_init(&srb->srb_ref);
425 srb->ha = ha;
426 srb->ddb = ddb_entry;
427 srb->cmd = cmd;
428 srb->flags = 0;
429 CMD_SP(cmd) = (void *)srb;
431 return srb;
434 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
436 struct scsi_cmnd *cmd = srb->cmd;
438 if (srb->flags & SRB_DMA_VALID) {
439 scsi_dma_unmap(cmd);
440 srb->flags &= ~SRB_DMA_VALID;
442 CMD_SP(cmd) = NULL;
445 void qla4xxx_srb_compl(struct kref *ref)
447 struct srb *srb = container_of(ref, struct srb, srb_ref);
448 struct scsi_cmnd *cmd = srb->cmd;
449 struct scsi_qla_host *ha = srb->ha;
451 qla4xxx_srb_free_dma(ha, srb);
453 mempool_free(srb, ha->srb_mempool);
455 cmd->scsi_done(cmd);
459 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
460 * @host: scsi host
461 * @cmd: Pointer to Linux's SCSI command structure
463 * Remarks:
464 * This routine is invoked by Linux to send a SCSI command to the driver.
465 * The mid-level driver tries to ensure that queuecommand never gets
466 * invoked concurrently with itself or the interrupt handler (although
467 * the interrupt handler may call this routine as part of request-
468 * completion handling). Unfortunely, it sometimes calls the scheduler
469 * in interrupt context which is a big NO! NO!.
471 static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
473 struct scsi_qla_host *ha = to_qla_host(host);
474 struct ddb_entry *ddb_entry = cmd->device->hostdata;
475 struct iscsi_cls_session *sess = ddb_entry->sess;
476 struct srb *srb;
477 int rval;
479 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
480 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
481 cmd->result = DID_NO_CONNECT << 16;
482 else
483 cmd->result = DID_REQUEUE << 16;
484 goto qc_fail_command;
487 if (!sess) {
488 cmd->result = DID_IMM_RETRY << 16;
489 goto qc_fail_command;
492 rval = iscsi_session_chkready(sess);
493 if (rval) {
494 cmd->result = rval;
495 goto qc_fail_command;
498 if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
499 if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
500 cmd->result = DID_NO_CONNECT << 16;
501 goto qc_fail_command;
503 return SCSI_MLQUEUE_TARGET_BUSY;
506 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
507 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
508 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
509 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
510 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
511 !test_bit(AF_ONLINE, &ha->flags) ||
512 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
513 goto qc_host_busy;
515 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
516 if (!srb)
517 goto qc_host_busy;
519 rval = qla4xxx_send_command_to_isp(ha, srb);
520 if (rval != QLA_SUCCESS)
521 goto qc_host_busy_free_sp;
523 return 0;
525 qc_host_busy_free_sp:
526 qla4xxx_srb_free_dma(ha, srb);
527 mempool_free(srb, ha->srb_mempool);
529 qc_host_busy:
530 return SCSI_MLQUEUE_HOST_BUSY;
532 qc_fail_command:
533 cmd->scsi_done(cmd);
535 return 0;
539 * qla4xxx_mem_free - frees memory allocated to adapter
540 * @ha: Pointer to host adapter structure.
542 * Frees memory previously allocated by qla4xxx_mem_alloc
544 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
546 if (ha->queues)
547 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
548 ha->queues_dma);
550 ha->queues_len = 0;
551 ha->queues = NULL;
552 ha->queues_dma = 0;
553 ha->request_ring = NULL;
554 ha->request_dma = 0;
555 ha->response_ring = NULL;
556 ha->response_dma = 0;
557 ha->shadow_regs = NULL;
558 ha->shadow_regs_dma = 0;
560 /* Free srb pool. */
561 if (ha->srb_mempool)
562 mempool_destroy(ha->srb_mempool);
564 ha->srb_mempool = NULL;
566 /* release io space registers */
567 if (is_qla8022(ha)) {
568 if (ha->nx_pcibase)
569 iounmap(
570 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
571 } else if (ha->reg)
572 iounmap(ha->reg);
573 pci_release_regions(ha->pdev);
577 * qla4xxx_mem_alloc - allocates memory for use by adapter.
578 * @ha: Pointer to host adapter structure
580 * Allocates DMA memory for request and response queues. Also allocates memory
581 * for srbs.
583 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
585 unsigned long align;
587 /* Allocate contiguous block of DMA memory for queues. */
588 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
589 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
590 sizeof(struct shadow_regs) +
591 MEM_ALIGN_VALUE +
592 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
593 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
594 &ha->queues_dma, GFP_KERNEL);
595 if (ha->queues == NULL) {
596 ql4_printk(KERN_WARNING, ha,
597 "Memory Allocation failed - queues.\n");
599 goto mem_alloc_error_exit;
601 memset(ha->queues, 0, ha->queues_len);
604 * As per RISC alignment requirements -- the bus-address must be a
605 * multiple of the request-ring size (in bytes).
607 align = 0;
608 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
609 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
610 (MEM_ALIGN_VALUE - 1));
612 /* Update request and response queue pointers. */
613 ha->request_dma = ha->queues_dma + align;
614 ha->request_ring = (struct queue_entry *) (ha->queues + align);
615 ha->response_dma = ha->queues_dma + align +
616 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
617 ha->response_ring = (struct queue_entry *) (ha->queues + align +
618 (REQUEST_QUEUE_DEPTH *
619 QUEUE_SIZE));
620 ha->shadow_regs_dma = ha->queues_dma + align +
621 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
622 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
623 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
624 (REQUEST_QUEUE_DEPTH *
625 QUEUE_SIZE) +
626 (RESPONSE_QUEUE_DEPTH *
627 QUEUE_SIZE));
629 /* Allocate memory for srb pool. */
630 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
631 mempool_free_slab, srb_cachep);
632 if (ha->srb_mempool == NULL) {
633 ql4_printk(KERN_WARNING, ha,
634 "Memory Allocation failed - SRB Pool.\n");
636 goto mem_alloc_error_exit;
639 return QLA_SUCCESS;
641 mem_alloc_error_exit:
642 qla4xxx_mem_free(ha);
643 return QLA_ERROR;
647 * qla4_8xxx_check_fw_alive - Check firmware health
648 * @ha: Pointer to host adapter structure.
650 * Context: Interrupt
652 static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
654 uint32_t fw_heartbeat_counter, halt_status;
656 fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
657 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
658 if (fw_heartbeat_counter == 0xffffffff) {
659 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
660 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
661 ha->host_no, __func__));
662 return;
665 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
666 ha->seconds_since_last_heartbeat++;
667 /* FW not alive after 2 seconds */
668 if (ha->seconds_since_last_heartbeat == 2) {
669 ha->seconds_since_last_heartbeat = 0;
670 halt_status = qla4_8xxx_rd_32(ha,
671 QLA82XX_PEG_HALT_STATUS1);
673 ql4_printk(KERN_INFO, ha,
674 "scsi(%ld): %s, Dumping hw/fw registers:\n "
675 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
676 " 0x%x,\n PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
677 " 0x%x,\n PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
678 " 0x%x,\n PEG_NET_4_PC: 0x%x\n",
679 ha->host_no, __func__, halt_status,
680 qla4_8xxx_rd_32(ha,
681 QLA82XX_PEG_HALT_STATUS2),
682 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
683 0x3c),
684 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
685 0x3c),
686 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
687 0x3c),
688 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
689 0x3c),
690 qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
691 0x3c));
693 /* Since we cannot change dev_state in interrupt
694 * context, set appropriate DPC flag then wakeup
695 * DPC */
696 if (halt_status & HALT_STATUS_UNRECOVERABLE)
697 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
698 else {
699 printk("scsi%ld: %s: detect abort needed!\n",
700 ha->host_no, __func__);
701 set_bit(DPC_RESET_HA, &ha->dpc_flags);
703 qla4xxx_wake_dpc(ha);
704 qla4xxx_mailbox_premature_completion(ha);
706 } else
707 ha->seconds_since_last_heartbeat = 0;
709 ha->fw_heartbeat_counter = fw_heartbeat_counter;
713 * qla4_8xxx_watchdog - Poll dev state
714 * @ha: Pointer to host adapter structure.
716 * Context: Interrupt
718 void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
720 uint32_t dev_state;
722 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
724 /* don't poll if reset is going on */
725 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
726 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
727 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
728 if (dev_state == QLA82XX_DEV_NEED_RESET &&
729 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
730 if (!ql4xdontresethba) {
731 ql4_printk(KERN_INFO, ha, "%s: HW State: "
732 "NEED RESET!\n", __func__);
733 set_bit(DPC_RESET_HA, &ha->dpc_flags);
734 qla4xxx_wake_dpc(ha);
735 qla4xxx_mailbox_premature_completion(ha);
737 } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
738 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
739 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
740 __func__);
741 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
742 qla4xxx_wake_dpc(ha);
743 } else {
744 /* Check firmware health */
745 qla4_8xxx_check_fw_alive(ha);
751 * qla4xxx_timer - checks every second for work to do.
752 * @ha: Pointer to host adapter structure.
754 static void qla4xxx_timer(struct scsi_qla_host *ha)
756 struct ddb_entry *ddb_entry, *dtemp;
757 int start_dpc = 0;
758 uint16_t w;
760 /* If we are in the middle of AER/EEH processing
761 * skip any processing and reschedule the timer
763 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
764 mod_timer(&ha->timer, jiffies + HZ);
765 return;
768 /* Hardware read to trigger an EEH error during mailbox waits. */
769 if (!pci_channel_offline(ha->pdev))
770 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
772 if (is_qla8022(ha)) {
773 qla4_8xxx_watchdog(ha);
776 /* Search for relogin's to time-out and port down retry. */
777 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
778 /* Count down time between sending relogins */
779 if (adapter_up(ha) &&
780 !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
781 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
782 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
783 INVALID_ENTRY) {
784 if (atomic_read(&ddb_entry->retry_relogin_timer)
785 == 0) {
786 atomic_set(&ddb_entry->
787 retry_relogin_timer,
788 INVALID_ENTRY);
789 set_bit(DPC_RELOGIN_DEVICE,
790 &ha->dpc_flags);
791 set_bit(DF_RELOGIN, &ddb_entry->flags);
792 DEBUG2(printk("scsi%ld: %s: ddb [%d]"
793 " login device\n",
794 ha->host_no, __func__,
795 ddb_entry->fw_ddb_index));
796 } else
797 atomic_dec(&ddb_entry->
798 retry_relogin_timer);
802 /* Wait for relogin to timeout */
803 if (atomic_read(&ddb_entry->relogin_timer) &&
804 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
806 * If the relogin times out and the device is
807 * still NOT ONLINE then try and relogin again.
809 if (atomic_read(&ddb_entry->state) !=
810 DDB_STATE_ONLINE &&
811 ddb_entry->fw_ddb_device_state ==
812 DDB_DS_SESSION_FAILED) {
813 /* Reset retry relogin timer */
814 atomic_inc(&ddb_entry->relogin_retry_count);
815 DEBUG2(printk("scsi%ld: ddb [%d] relogin"
816 " timed out-retrying"
817 " relogin (%d)\n",
818 ha->host_no,
819 ddb_entry->fw_ddb_index,
820 atomic_read(&ddb_entry->
821 relogin_retry_count))
823 start_dpc++;
824 DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
825 "initiate relogin after"
826 " %d seconds\n",
827 ha->host_no, ddb_entry->bus,
828 ddb_entry->target,
829 ddb_entry->fw_ddb_index,
830 ddb_entry->default_time2wait + 4)
833 atomic_set(&ddb_entry->retry_relogin_timer,
834 ddb_entry->default_time2wait + 4);
839 if (!is_qla8022(ha)) {
840 /* Check for heartbeat interval. */
841 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
842 ha->heartbeat_interval != 0) {
843 ha->seconds_since_last_heartbeat++;
844 if (ha->seconds_since_last_heartbeat >
845 ha->heartbeat_interval + 2)
846 set_bit(DPC_RESET_HA, &ha->dpc_flags);
850 /* Wakeup the dpc routine for this adapter, if needed. */
851 if (start_dpc ||
852 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
853 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
854 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
855 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
856 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
857 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
858 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
859 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
860 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
861 test_bit(DPC_AEN, &ha->dpc_flags)) {
862 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
863 " - dpc flags = 0x%lx\n",
864 ha->host_no, __func__, ha->dpc_flags));
865 qla4xxx_wake_dpc(ha);
868 /* Reschedule timer thread to call us back in one second */
869 mod_timer(&ha->timer, jiffies + HZ);
871 DEBUG2(ha->seconds_since_last_intr++);
875 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
876 * @ha: Pointer to host adapter structure.
878 * This routine stalls the driver until all outstanding commands are returned.
879 * Caller must release the Hardware Lock prior to calling this routine.
881 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
883 uint32_t index = 0;
884 unsigned long flags;
885 struct scsi_cmnd *cmd;
887 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
889 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
890 "complete\n", WAIT_CMD_TOV));
892 while (!time_after_eq(jiffies, wtime)) {
893 spin_lock_irqsave(&ha->hardware_lock, flags);
894 /* Find a command that hasn't completed. */
895 for (index = 0; index < ha->host->can_queue; index++) {
896 cmd = scsi_host_find_tag(ha->host, index);
898 * We cannot just check if the index is valid,
899 * becase if we are run from the scsi eh, then
900 * the scsi/block layer is going to prevent
901 * the tag from being released.
903 if (cmd != NULL && CMD_SP(cmd))
904 break;
906 spin_unlock_irqrestore(&ha->hardware_lock, flags);
908 /* If No Commands are pending, wait is complete */
909 if (index == ha->host->can_queue)
910 return QLA_SUCCESS;
912 msleep(1000);
914 /* If we timed out on waiting for commands to come back
915 * return ERROR. */
916 return QLA_ERROR;
919 int qla4xxx_hw_reset(struct scsi_qla_host *ha)
921 uint32_t ctrl_status;
922 unsigned long flags = 0;
924 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
926 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
927 return QLA_ERROR;
929 spin_lock_irqsave(&ha->hardware_lock, flags);
932 * If the SCSI Reset Interrupt bit is set, clear it.
933 * Otherwise, the Soft Reset won't work.
935 ctrl_status = readw(&ha->reg->ctrl_status);
936 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
937 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
939 /* Issue Soft Reset */
940 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
941 readl(&ha->reg->ctrl_status);
943 spin_unlock_irqrestore(&ha->hardware_lock, flags);
944 return QLA_SUCCESS;
948 * qla4xxx_soft_reset - performs soft reset.
949 * @ha: Pointer to host adapter structure.
951 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
953 uint32_t max_wait_time;
954 unsigned long flags = 0;
955 int status;
956 uint32_t ctrl_status;
958 status = qla4xxx_hw_reset(ha);
959 if (status != QLA_SUCCESS)
960 return status;
962 status = QLA_ERROR;
963 /* Wait until the Network Reset Intr bit is cleared */
964 max_wait_time = RESET_INTR_TOV;
965 do {
966 spin_lock_irqsave(&ha->hardware_lock, flags);
967 ctrl_status = readw(&ha->reg->ctrl_status);
968 spin_unlock_irqrestore(&ha->hardware_lock, flags);
970 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
971 break;
973 msleep(1000);
974 } while ((--max_wait_time));
976 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
977 DEBUG2(printk(KERN_WARNING
978 "scsi%ld: Network Reset Intr not cleared by "
979 "Network function, clearing it now!\n",
980 ha->host_no));
981 spin_lock_irqsave(&ha->hardware_lock, flags);
982 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
983 readl(&ha->reg->ctrl_status);
984 spin_unlock_irqrestore(&ha->hardware_lock, flags);
987 /* Wait until the firmware tells us the Soft Reset is done */
988 max_wait_time = SOFT_RESET_TOV;
989 do {
990 spin_lock_irqsave(&ha->hardware_lock, flags);
991 ctrl_status = readw(&ha->reg->ctrl_status);
992 spin_unlock_irqrestore(&ha->hardware_lock, flags);
994 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
995 status = QLA_SUCCESS;
996 break;
999 msleep(1000);
1000 } while ((--max_wait_time));
1003 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
1004 * after the soft reset has taken place.
1006 spin_lock_irqsave(&ha->hardware_lock, flags);
1007 ctrl_status = readw(&ha->reg->ctrl_status);
1008 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
1009 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
1010 readl(&ha->reg->ctrl_status);
1012 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1014 /* If soft reset fails then most probably the bios on other
1015 * function is also enabled.
1016 * Since the initialization is sequential the other fn
1017 * wont be able to acknowledge the soft reset.
1018 * Issue a force soft reset to workaround this scenario.
1020 if (max_wait_time == 0) {
1021 /* Issue Force Soft Reset */
1022 spin_lock_irqsave(&ha->hardware_lock, flags);
1023 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1024 readl(&ha->reg->ctrl_status);
1025 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1026 /* Wait until the firmware tells us the Soft Reset is done */
1027 max_wait_time = SOFT_RESET_TOV;
1028 do {
1029 spin_lock_irqsave(&ha->hardware_lock, flags);
1030 ctrl_status = readw(&ha->reg->ctrl_status);
1031 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1033 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1034 status = QLA_SUCCESS;
1035 break;
1038 msleep(1000);
1039 } while ((--max_wait_time));
1042 return status;
1046 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
1047 * @ha: Pointer to host adapter structure.
1048 * @res: returned scsi status
1050 * This routine is called just prior to a HARD RESET to return all
1051 * outstanding commands back to the Operating System.
1052 * Caller should make sure that the following locks are released
1053 * before this calling routine: Hardware lock, and io_request_lock.
1055 static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
1057 struct srb *srb;
1058 int i;
1059 unsigned long flags;
1061 spin_lock_irqsave(&ha->hardware_lock, flags);
1062 for (i = 0; i < ha->host->can_queue; i++) {
1063 srb = qla4xxx_del_from_active_array(ha, i);
1064 if (srb != NULL) {
1065 srb->cmd->result = res;
1066 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1069 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1072 void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1074 clear_bit(AF_ONLINE, &ha->flags);
1076 /* Disable the board */
1077 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1079 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1080 qla4xxx_mark_all_devices_missing(ha);
1081 clear_bit(AF_INIT_DONE, &ha->flags);
1085 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1086 * @ha: Pointer to host adapter structure.
1088 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
1090 int status = QLA_ERROR;
1091 uint8_t reset_chip = 0;
1093 /* Stall incoming I/O until we are done */
1094 scsi_block_requests(ha->host);
1095 clear_bit(AF_ONLINE, &ha->flags);
1097 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
1099 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1101 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1102 reset_chip = 1;
1104 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1105 * do not reset adapter, jump to initialize_adapter */
1106 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1107 status = QLA_SUCCESS;
1108 goto recover_ha_init_adapter;
1111 /* For the ISP-82xx adapter, issue a stop_firmware if invoked
1112 * from eh_host_reset or ioctl module */
1113 if (is_qla8022(ha) && !reset_chip &&
1114 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1116 DEBUG2(ql4_printk(KERN_INFO, ha,
1117 "scsi%ld: %s - Performing stop_firmware...\n",
1118 ha->host_no, __func__));
1119 status = ha->isp_ops->reset_firmware(ha);
1120 if (status == QLA_SUCCESS) {
1121 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1122 qla4xxx_cmd_wait(ha);
1123 ha->isp_ops->disable_intrs(ha);
1124 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1125 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1126 } else {
1127 /* If the stop_firmware fails then
1128 * reset the entire chip */
1129 reset_chip = 1;
1130 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1131 set_bit(DPC_RESET_HA, &ha->dpc_flags);
1135 /* Issue full chip reset if recovering from a catastrophic error,
1136 * or if stop_firmware fails for ISP-82xx.
1137 * This is the default case for ISP-4xxx */
1138 if (!is_qla8022(ha) || reset_chip) {
1139 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
1140 qla4xxx_cmd_wait(ha);
1141 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1142 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1143 DEBUG2(ql4_printk(KERN_INFO, ha,
1144 "scsi%ld: %s - Performing chip reset..\n",
1145 ha->host_no, __func__));
1146 status = ha->isp_ops->reset_chip(ha);
1149 /* Flush any pending ddb changed AENs */
1150 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1152 recover_ha_init_adapter:
1153 /* Upon successful firmware/chip reset, re-initialize the adapter */
1154 if (status == QLA_SUCCESS) {
1155 /* For ISP-4xxx, force function 1 to always initialize
1156 * before function 3 to prevent both funcions from
1157 * stepping on top of the other */
1158 if (!is_qla8022(ha) && (ha->mac_index == 3))
1159 ssleep(6);
1161 /* NOTE: AF_ONLINE flag set upon successful completion of
1162 * qla4xxx_initialize_adapter */
1163 status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1166 /* Retry failed adapter initialization, if necessary
1167 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1168 * case to prevent ping-pong resets between functions */
1169 if (!test_bit(AF_ONLINE, &ha->flags) &&
1170 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1171 /* Adapter initialization failed, see if we can retry
1172 * resetting the ha.
1173 * Since we don't want to block the DPC for too long
1174 * with multiple resets in the same thread,
1175 * utilize DPC to retry */
1176 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1177 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1178 DEBUG2(printk("scsi%ld: recover adapter - retrying "
1179 "(%d) more times\n", ha->host_no,
1180 ha->retry_reset_ha_cnt));
1181 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1182 status = QLA_ERROR;
1183 } else {
1184 if (ha->retry_reset_ha_cnt > 0) {
1185 /* Schedule another Reset HA--DPC will retry */
1186 ha->retry_reset_ha_cnt--;
1187 DEBUG2(printk("scsi%ld: recover adapter - "
1188 "retry remaining %d\n",
1189 ha->host_no,
1190 ha->retry_reset_ha_cnt));
1191 status = QLA_ERROR;
1194 if (ha->retry_reset_ha_cnt == 0) {
1195 /* Recover adapter retries have been exhausted.
1196 * Adapter DEAD */
1197 DEBUG2(printk("scsi%ld: recover adapter "
1198 "failed - board disabled\n",
1199 ha->host_no));
1200 qla4xxx_dead_adapter_cleanup(ha);
1201 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1202 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1203 clear_bit(DPC_RESET_HA_FW_CONTEXT,
1204 &ha->dpc_flags);
1205 status = QLA_ERROR;
1208 } else {
1209 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1210 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1211 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1214 ha->adapter_error_count++;
1216 if (test_bit(AF_ONLINE, &ha->flags))
1217 ha->isp_ops->enable_intrs(ha);
1219 scsi_unblock_requests(ha->host);
1221 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1222 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1223 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
1225 return status;
1228 static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
1230 struct ddb_entry *ddb_entry, *dtemp;
1232 list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
1233 if ((atomic_read(&ddb_entry->state) == DDB_STATE_MISSING) ||
1234 (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD)) {
1235 if (ddb_entry->fw_ddb_device_state ==
1236 DDB_DS_SESSION_ACTIVE) {
1237 atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1238 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
1239 " marked ONLINE\n", ha->host_no, __func__,
1240 ddb_entry->fw_ddb_index);
1242 iscsi_unblock_session(ddb_entry->sess);
1243 } else
1244 qla4xxx_relogin_device(ha, ddb_entry);
1249 void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1251 if (ha->dpc_thread)
1252 queue_work(ha->dpc_thread, &ha->dpc_work);
1256 * qla4xxx_do_dpc - dpc routine
1257 * @data: in our case pointer to adapter structure
1259 * This routine is a task that is schedule by the interrupt handler
1260 * to perform the background processing for interrupts. We put it
1261 * on a task queue that is consumed whenever the scheduler runs; that's
1262 * so you can do anything (i.e. put the process to sleep etc). In fact,
1263 * the mid-level tries to sleep when it reaches the driver threshold
1264 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1266 static void qla4xxx_do_dpc(struct work_struct *work)
1268 struct scsi_qla_host *ha =
1269 container_of(work, struct scsi_qla_host, dpc_work);
1270 struct ddb_entry *ddb_entry, *dtemp;
1271 int status = QLA_ERROR;
1273 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1274 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1275 ha->host_no, __func__, ha->flags, ha->dpc_flags))
1277 /* Initialization not yet finished. Don't do anything yet. */
1278 if (!test_bit(AF_INIT_DONE, &ha->flags))
1279 return;
1281 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1282 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1283 ha->host_no, __func__, ha->flags));
1284 return;
1287 if (is_qla8022(ha)) {
1288 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1289 qla4_8xxx_idc_lock(ha);
1290 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1291 QLA82XX_DEV_FAILED);
1292 qla4_8xxx_idc_unlock(ha);
1293 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1294 qla4_8xxx_device_state_handler(ha);
1296 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1297 qla4_8xxx_need_qsnt_handler(ha);
1301 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1302 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1303 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1304 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1305 if (ql4xdontresethba) {
1306 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1307 ha->host_no, __func__));
1308 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1309 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1310 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1311 goto dpc_post_reset_ha;
1313 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1314 test_bit(DPC_RESET_HA, &ha->dpc_flags))
1315 qla4xxx_recover_adapter(ha);
1317 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1318 uint8_t wait_time = RESET_INTR_TOV;
1320 while ((readw(&ha->reg->ctrl_status) &
1321 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1322 if (--wait_time == 0)
1323 break;
1324 msleep(1000);
1326 if (wait_time == 0)
1327 DEBUG2(printk("scsi%ld: %s: SR|FSR "
1328 "bit not cleared-- resetting\n",
1329 ha->host_no, __func__));
1330 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1331 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1332 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1333 status = qla4xxx_recover_adapter(ha);
1335 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1336 if (status == QLA_SUCCESS)
1337 ha->isp_ops->enable_intrs(ha);
1341 dpc_post_reset_ha:
1342 /* ---- process AEN? --- */
1343 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1344 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1346 /* ---- Get DHCP IP Address? --- */
1347 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1348 qla4xxx_get_dhcp_ip_address(ha);
1350 /* ---- link change? --- */
1351 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1352 if (!test_bit(AF_LINK_UP, &ha->flags)) {
1353 /* ---- link down? --- */
1354 qla4xxx_mark_all_devices_missing(ha);
1355 } else {
1356 /* ---- link up? --- *
1357 * F/W will auto login to all devices ONLY ONCE after
1358 * link up during driver initialization and runtime
1359 * fatal error recovery. Therefore, the driver must
1360 * manually relogin to devices when recovering from
1361 * connection failures, logouts, expired KATO, etc. */
1363 qla4xxx_relogin_all_devices(ha);
1367 /* ---- relogin device? --- */
1368 if (adapter_up(ha) &&
1369 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1370 list_for_each_entry_safe(ddb_entry, dtemp,
1371 &ha->ddb_list, list) {
1372 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1373 atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1374 qla4xxx_relogin_device(ha, ddb_entry);
1377 * If mbx cmd times out there is no point
1378 * in continuing further.
1379 * With large no of targets this can hang
1380 * the system.
1382 if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1383 printk(KERN_WARNING "scsi%ld: %s: "
1384 "need to reset hba\n",
1385 ha->host_no, __func__);
1386 break;
1394 * qla4xxx_free_adapter - release the adapter
1395 * @ha: pointer to adapter structure
1397 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1400 if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1401 /* Turn-off interrupts on the card. */
1402 ha->isp_ops->disable_intrs(ha);
1405 /* Remove timer thread, if present */
1406 if (ha->timer_active)
1407 qla4xxx_stop_timer(ha);
1409 /* Kill the kernel thread for this host */
1410 if (ha->dpc_thread)
1411 destroy_workqueue(ha->dpc_thread);
1413 /* Put firmware in known state */
1414 ha->isp_ops->reset_firmware(ha);
1416 if (is_qla8022(ha)) {
1417 qla4_8xxx_idc_lock(ha);
1418 qla4_8xxx_clear_drv_active(ha);
1419 qla4_8xxx_idc_unlock(ha);
1422 /* Detach interrupts */
1423 if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1424 qla4xxx_free_irqs(ha);
1426 /* free extra memory */
1427 qla4xxx_mem_free(ha);
1430 int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1432 int status = 0;
1433 uint8_t revision_id;
1434 unsigned long mem_base, mem_len, db_base, db_len;
1435 struct pci_dev *pdev = ha->pdev;
1437 status = pci_request_regions(pdev, DRIVER_NAME);
1438 if (status) {
1439 printk(KERN_WARNING
1440 "scsi(%ld) Failed to reserve PIO regions (%s) "
1441 "status=%d\n", ha->host_no, pci_name(pdev), status);
1442 goto iospace_error_exit;
1445 pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1446 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1447 __func__, revision_id));
1448 ha->revision_id = revision_id;
1450 /* remap phys address */
1451 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1452 mem_len = pci_resource_len(pdev, 0);
1453 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1454 __func__, mem_base, mem_len));
1456 /* mapping of pcibase pointer */
1457 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1458 if (!ha->nx_pcibase) {
1459 printk(KERN_ERR
1460 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1461 pci_release_regions(ha->pdev);
1462 goto iospace_error_exit;
1465 /* Mapping of IO base pointer, door bell read and write pointer */
1467 /* mapping of IO base pointer */
1468 ha->qla4_8xxx_reg =
1469 (struct device_reg_82xx __iomem *)((uint8_t *)ha->nx_pcibase +
1470 0xbc000 + (ha->pdev->devfn << 11));
1472 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
1473 db_len = pci_resource_len(pdev, 4);
1475 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
1476 QLA82XX_CAM_RAM_DB2);
1478 return 0;
1479 iospace_error_exit:
1480 return -ENOMEM;
1483 /***
1484 * qla4xxx_iospace_config - maps registers
1485 * @ha: pointer to adapter structure
1487 * This routines maps HBA's registers from the pci address space
1488 * into the kernel virtual address space for memory mapped i/o.
1490 int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1492 unsigned long pio, pio_len, pio_flags;
1493 unsigned long mmio, mmio_len, mmio_flags;
1495 pio = pci_resource_start(ha->pdev, 0);
1496 pio_len = pci_resource_len(ha->pdev, 0);
1497 pio_flags = pci_resource_flags(ha->pdev, 0);
1498 if (pio_flags & IORESOURCE_IO) {
1499 if (pio_len < MIN_IOBASE_LEN) {
1500 ql4_printk(KERN_WARNING, ha,
1501 "Invalid PCI I/O region size\n");
1502 pio = 0;
1504 } else {
1505 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
1506 pio = 0;
1509 /* Use MMIO operations for all accesses. */
1510 mmio = pci_resource_start(ha->pdev, 1);
1511 mmio_len = pci_resource_len(ha->pdev, 1);
1512 mmio_flags = pci_resource_flags(ha->pdev, 1);
1514 if (!(mmio_flags & IORESOURCE_MEM)) {
1515 ql4_printk(KERN_ERR, ha,
1516 "region #0 not an MMIO resource, aborting\n");
1518 goto iospace_error_exit;
1521 if (mmio_len < MIN_IOBASE_LEN) {
1522 ql4_printk(KERN_ERR, ha,
1523 "Invalid PCI mem region size, aborting\n");
1524 goto iospace_error_exit;
1527 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1528 ql4_printk(KERN_WARNING, ha,
1529 "Failed to reserve PIO/MMIO regions\n");
1531 goto iospace_error_exit;
1534 ha->pio_address = pio;
1535 ha->pio_length = pio_len;
1536 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1537 if (!ha->reg) {
1538 ql4_printk(KERN_ERR, ha,
1539 "cannot remap MMIO, aborting\n");
1541 goto iospace_error_exit;
1544 return 0;
1546 iospace_error_exit:
1547 return -ENOMEM;
1550 static struct isp_operations qla4xxx_isp_ops = {
1551 .iospace_config = qla4xxx_iospace_config,
1552 .pci_config = qla4xxx_pci_config,
1553 .disable_intrs = qla4xxx_disable_intrs,
1554 .enable_intrs = qla4xxx_enable_intrs,
1555 .start_firmware = qla4xxx_start_firmware,
1556 .intr_handler = qla4xxx_intr_handler,
1557 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
1558 .reset_chip = qla4xxx_soft_reset,
1559 .reset_firmware = qla4xxx_hw_reset,
1560 .queue_iocb = qla4xxx_queue_iocb,
1561 .complete_iocb = qla4xxx_complete_iocb,
1562 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
1563 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
1564 .get_sys_info = qla4xxx_get_sys_info,
1567 static struct isp_operations qla4_8xxx_isp_ops = {
1568 .iospace_config = qla4_8xxx_iospace_config,
1569 .pci_config = qla4_8xxx_pci_config,
1570 .disable_intrs = qla4_8xxx_disable_intrs,
1571 .enable_intrs = qla4_8xxx_enable_intrs,
1572 .start_firmware = qla4_8xxx_load_risc,
1573 .intr_handler = qla4_8xxx_intr_handler,
1574 .interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1575 .reset_chip = qla4_8xxx_isp_reset,
1576 .reset_firmware = qla4_8xxx_stop_firmware,
1577 .queue_iocb = qla4_8xxx_queue_iocb,
1578 .complete_iocb = qla4_8xxx_complete_iocb,
1579 .rd_shdw_req_q_out = qla4_8xxx_rd_shdw_req_q_out,
1580 .rd_shdw_rsp_q_in = qla4_8xxx_rd_shdw_rsp_q_in,
1581 .get_sys_info = qla4_8xxx_get_sys_info,
1584 uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1586 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1589 uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1591 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1594 uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1596 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1599 uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1601 return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1605 * qla4xxx_probe_adapter - callback function to probe HBA
1606 * @pdev: pointer to pci_dev structure
1607 * @pci_device_id: pointer to pci_device entry
1609 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1610 * It returns zero if successful. It also initializes all data necessary for
1611 * the driver.
1613 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1614 const struct pci_device_id *ent)
1616 int ret = -ENODEV, status;
1617 struct Scsi_Host *host;
1618 struct scsi_qla_host *ha;
1619 uint8_t init_retry_count = 0;
1620 char buf[34];
1621 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
1622 uint32_t dev_state;
1624 if (pci_enable_device(pdev))
1625 return -1;
1627 host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1628 if (host == NULL) {
1629 printk(KERN_WARNING
1630 "qla4xxx: Couldn't allocate host from scsi layer!\n");
1631 goto probe_disable_device;
1634 /* Clear our data area */
1635 ha = (struct scsi_qla_host *) host->hostdata;
1636 memset(ha, 0, sizeof(*ha));
1638 /* Save the information from PCI BIOS. */
1639 ha->pdev = pdev;
1640 ha->host = host;
1641 ha->host_no = host->host_no;
1643 pci_enable_pcie_error_reporting(pdev);
1645 /* Setup Runtime configurable options */
1646 if (is_qla8022(ha)) {
1647 ha->isp_ops = &qla4_8xxx_isp_ops;
1648 rwlock_init(&ha->hw_lock);
1649 ha->qdr_sn_window = -1;
1650 ha->ddr_mn_window = -1;
1651 ha->curr_window = 255;
1652 ha->func_num = PCI_FUNC(ha->pdev->devfn);
1653 nx_legacy_intr = &legacy_intr[ha->func_num];
1654 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1655 ha->nx_legacy_intr.tgt_status_reg =
1656 nx_legacy_intr->tgt_status_reg;
1657 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1658 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1659 } else {
1660 ha->isp_ops = &qla4xxx_isp_ops;
1663 /* Set EEH reset type to fundamental if required by hba */
1664 if (is_qla8022(ha))
1665 pdev->needs_freset = 1;
1667 /* Configure PCI I/O space. */
1668 ret = ha->isp_ops->iospace_config(ha);
1669 if (ret)
1670 goto probe_failed_ioconfig;
1672 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1673 pdev->device, pdev->irq, ha->reg);
1675 qla4xxx_config_dma_addressing(ha);
1677 /* Initialize lists and spinlocks. */
1678 INIT_LIST_HEAD(&ha->ddb_list);
1679 INIT_LIST_HEAD(&ha->free_srb_q);
1681 mutex_init(&ha->mbox_sem);
1682 init_completion(&ha->mbx_intr_comp);
1684 spin_lock_init(&ha->hardware_lock);
1686 /* Allocate dma buffers */
1687 if (qla4xxx_mem_alloc(ha)) {
1688 ql4_printk(KERN_WARNING, ha,
1689 "[ERROR] Failed to allocate memory for adapter\n");
1691 ret = -ENOMEM;
1692 goto probe_failed;
1695 if (is_qla8022(ha))
1696 (void) qla4_8xxx_get_flash_info(ha);
1699 * Initialize the Host adapter request/response queues and
1700 * firmware
1701 * NOTE: interrupts enabled upon successful completion
1703 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1704 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1705 init_retry_count++ < MAX_INIT_RETRIES) {
1707 if (is_qla8022(ha)) {
1708 qla4_8xxx_idc_lock(ha);
1709 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
1710 qla4_8xxx_idc_unlock(ha);
1711 if (dev_state == QLA82XX_DEV_FAILED) {
1712 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
1713 "initialize adapter. H/W is in failed state\n",
1714 __func__);
1715 break;
1718 DEBUG2(printk("scsi: %s: retrying adapter initialization "
1719 "(%d)\n", __func__, init_retry_count));
1721 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1722 continue;
1724 status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1727 if (!test_bit(AF_ONLINE, &ha->flags)) {
1728 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
1730 if (is_qla8022(ha) && ql4xdontresethba) {
1731 /* Put the device in failed state. */
1732 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
1733 qla4_8xxx_idc_lock(ha);
1734 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1735 QLA82XX_DEV_FAILED);
1736 qla4_8xxx_idc_unlock(ha);
1738 ret = -ENODEV;
1739 goto probe_failed;
1742 host->cmd_per_lun = 3;
1743 host->max_channel = 0;
1744 host->max_lun = MAX_LUNS - 1;
1745 host->max_id = MAX_TARGETS;
1746 host->max_cmd_len = IOCB_MAX_CDB_LEN;
1747 host->can_queue = MAX_SRBS ;
1748 host->transportt = qla4xxx_scsi_transport;
1750 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1751 if (ret) {
1752 ql4_printk(KERN_WARNING, ha,
1753 "scsi_init_shared_tag_map failed\n");
1754 goto probe_failed;
1757 /* Startup the kernel thread for this host adapter. */
1758 DEBUG2(printk("scsi: %s: Starting kernel thread for "
1759 "qla4xxx_dpc\n", __func__));
1760 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1761 ha->dpc_thread = create_singlethread_workqueue(buf);
1762 if (!ha->dpc_thread) {
1763 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
1764 ret = -ENODEV;
1765 goto probe_failed;
1767 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1769 /* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1770 * (which is called indirectly by qla4xxx_initialize_adapter),
1771 * so that irqs will be registered after crbinit but before
1772 * mbx_intr_enable.
1774 if (!is_qla8022(ha)) {
1775 ret = qla4xxx_request_irqs(ha);
1776 if (ret) {
1777 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1778 "interrupt %d already in use.\n", pdev->irq);
1779 goto probe_failed;
1783 pci_save_state(ha->pdev);
1784 ha->isp_ops->enable_intrs(ha);
1786 /* Start timer thread. */
1787 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1789 set_bit(AF_INIT_DONE, &ha->flags);
1791 pci_set_drvdata(pdev, ha);
1793 ret = scsi_add_host(host, &pdev->dev);
1794 if (ret)
1795 goto probe_failed;
1797 printk(KERN_INFO
1798 " QLogic iSCSI HBA Driver version: %s\n"
1799 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1800 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1801 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1802 ha->patch_number, ha->build_number);
1803 scsi_scan_host(host);
1804 return 0;
1806 probe_failed:
1807 qla4xxx_free_adapter(ha);
1809 probe_failed_ioconfig:
1810 pci_disable_pcie_error_reporting(pdev);
1811 scsi_host_put(ha->host);
1813 probe_disable_device:
1814 pci_disable_device(pdev);
1816 return ret;
1820 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
1821 * @ha: pointer to adapter structure
1823 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
1824 * so that the other port will not re-initialize while in the process of
1825 * removing the ha due to driver unload or hba hotplug.
1827 static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
1829 struct scsi_qla_host *other_ha = NULL;
1830 struct pci_dev *other_pdev = NULL;
1831 int fn = ISP4XXX_PCI_FN_2;
1833 /*iscsi function numbers for ISP4xxx is 1 and 3*/
1834 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
1835 fn = ISP4XXX_PCI_FN_1;
1837 other_pdev =
1838 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
1839 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
1840 fn));
1842 /* Get other_ha if other_pdev is valid and state is enable*/
1843 if (other_pdev) {
1844 if (atomic_read(&other_pdev->enable_cnt)) {
1845 other_ha = pci_get_drvdata(other_pdev);
1846 if (other_ha) {
1847 set_bit(AF_HA_REMOVAL, &other_ha->flags);
1848 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
1849 "Prevent %s reinit\n", __func__,
1850 dev_name(&other_ha->pdev->dev)));
1853 pci_dev_put(other_pdev);
1858 * qla4xxx_remove_adapter - calback function to remove adapter.
1859 * @pci_dev: PCI device pointer
1861 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1863 struct scsi_qla_host *ha;
1865 ha = pci_get_drvdata(pdev);
1867 if (!is_qla8022(ha))
1868 qla4xxx_prevent_other_port_reinit(ha);
1870 /* remove devs from iscsi_sessions to scsi_devices */
1871 qla4xxx_free_ddb_list(ha);
1873 scsi_remove_host(ha->host);
1875 qla4xxx_free_adapter(ha);
1877 scsi_host_put(ha->host);
1879 pci_disable_pcie_error_reporting(pdev);
1880 pci_disable_device(pdev);
1881 pci_set_drvdata(pdev, NULL);
1885 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1886 * @ha: HA context
1888 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1889 * supported addressing method.
1891 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1893 int retval;
1895 /* Update our PCI device dma_mask for full 64 bit mask */
1896 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1897 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1898 dev_dbg(&ha->pdev->dev,
1899 "Failed to set 64 bit PCI consistent mask; "
1900 "using 32 bit.\n");
1901 retval = pci_set_consistent_dma_mask(ha->pdev,
1902 DMA_BIT_MASK(32));
1904 } else
1905 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1908 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1910 struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1911 struct ddb_entry *ddb = sess->dd_data;
1912 int queue_depth = QL4_DEF_QDEPTH;
1914 sdev->hostdata = ddb;
1915 sdev->tagged_supported = 1;
1917 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
1918 queue_depth = ql4xmaxqdepth;
1920 scsi_activate_tcq(sdev, queue_depth);
1921 return 0;
1924 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1926 sdev->tagged_supported = 1;
1927 return 0;
1930 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1932 scsi_deactivate_tcq(sdev, 1);
1936 * qla4xxx_del_from_active_array - returns an active srb
1937 * @ha: Pointer to host adapter structure.
1938 * @index: index into the active_array
1940 * This routine removes and returns the srb at the specified index
1942 struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1943 uint32_t index)
1945 struct srb *srb = NULL;
1946 struct scsi_cmnd *cmd = NULL;
1948 cmd = scsi_host_find_tag(ha->host, index);
1949 if (!cmd)
1950 return srb;
1952 srb = (struct srb *)CMD_SP(cmd);
1953 if (!srb)
1954 return srb;
1956 /* update counters */
1957 if (srb->flags & SRB_DMA_VALID) {
1958 ha->req_q_count += srb->iocb_cnt;
1959 ha->iocb_cnt -= srb->iocb_cnt;
1960 if (srb->cmd)
1961 srb->cmd->host_scribble =
1962 (unsigned char *)(unsigned long) MAX_SRBS;
1964 return srb;
1968 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1969 * @ha: Pointer to host adapter structure.
1970 * @cmd: Scsi Command to wait on.
1972 * This routine waits for the command to be returned by the Firmware
1973 * for some max time.
1975 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1976 struct scsi_cmnd *cmd)
1978 int done = 0;
1979 struct srb *rp;
1980 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1981 int ret = SUCCESS;
1983 /* Dont wait on command if PCI error is being handled
1984 * by PCI AER driver
1986 if (unlikely(pci_channel_offline(ha->pdev)) ||
1987 (test_bit(AF_EEH_BUSY, &ha->flags))) {
1988 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1989 ha->host_no, __func__);
1990 return ret;
1993 do {
1994 /* Checking to see if its returned to OS */
1995 rp = (struct srb *) CMD_SP(cmd);
1996 if (rp == NULL) {
1997 done++;
1998 break;
2001 msleep(2000);
2002 } while (max_wait_time--);
2004 return done;
2008 * qla4xxx_wait_for_hba_online - waits for HBA to come online
2009 * @ha: Pointer to host adapter structure
2011 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
2013 unsigned long wait_online;
2015 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
2016 while (time_before(jiffies, wait_online)) {
2018 if (adapter_up(ha))
2019 return QLA_SUCCESS;
2021 msleep(2000);
2024 return QLA_ERROR;
2028 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
2029 * @ha: pointer to HBA
2030 * @t: target id
2031 * @l: lun id
2033 * This function waits for all outstanding commands to a lun to complete. It
2034 * returns 0 if all pending commands are returned and 1 otherwise.
2036 static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
2037 struct scsi_target *stgt,
2038 struct scsi_device *sdev)
2040 int cnt;
2041 int status = 0;
2042 struct scsi_cmnd *cmd;
2045 * Waiting for all commands for the designated target or dev
2046 * in the active array
2048 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
2049 cmd = scsi_host_find_tag(ha->host, cnt);
2050 if (cmd && stgt == scsi_target(cmd->device) &&
2051 (!sdev || sdev == cmd->device)) {
2052 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2053 status++;
2054 break;
2058 return status;
2062 * qla4xxx_eh_abort - callback for abort task.
2063 * @cmd: Pointer to Linux's SCSI command structure
2065 * This routine is called by the Linux OS to abort the specified
2066 * command.
2068 static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2070 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2071 unsigned int id = cmd->device->id;
2072 unsigned int lun = cmd->device->lun;
2073 unsigned long flags;
2074 struct srb *srb = NULL;
2075 int ret = SUCCESS;
2076 int wait = 0;
2078 ql4_printk(KERN_INFO, ha,
2079 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
2080 ha->host_no, id, lun, cmd);
2082 spin_lock_irqsave(&ha->hardware_lock, flags);
2083 srb = (struct srb *) CMD_SP(cmd);
2084 if (!srb) {
2085 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2086 return SUCCESS;
2088 kref_get(&srb->srb_ref);
2089 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2091 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2092 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2093 ha->host_no, id, lun));
2094 ret = FAILED;
2095 } else {
2096 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2097 ha->host_no, id, lun));
2098 wait = 1;
2101 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2103 /* Wait for command to complete */
2104 if (wait) {
2105 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2106 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2107 ha->host_no, id, lun));
2108 ret = FAILED;
2112 ql4_printk(KERN_INFO, ha,
2113 "scsi%ld:%d:%d: Abort command - %s\n",
2114 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
2116 return ret;
2120 * qla4xxx_eh_device_reset - callback for target reset.
2121 * @cmd: Pointer to Linux's SCSI command structure
2123 * This routine is called by the Linux OS to reset all luns on the
2124 * specified target.
2126 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2128 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2129 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2130 int ret = FAILED, stat;
2132 if (!ddb_entry)
2133 return ret;
2135 ret = iscsi_block_scsi_eh(cmd);
2136 if (ret)
2137 return ret;
2138 ret = FAILED;
2140 ql4_printk(KERN_INFO, ha,
2141 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2142 cmd->device->channel, cmd->device->id, cmd->device->lun);
2144 DEBUG2(printk(KERN_INFO
2145 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2146 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
2147 cmd, jiffies, cmd->request->timeout / HZ,
2148 ha->dpc_flags, cmd->result, cmd->allowed));
2150 /* FIXME: wait for hba to go online */
2151 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2152 if (stat != QLA_SUCCESS) {
2153 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
2154 goto eh_dev_reset_done;
2157 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2158 cmd->device)) {
2159 ql4_printk(KERN_INFO, ha,
2160 "DEVICE RESET FAILED - waiting for "
2161 "commands.\n");
2162 goto eh_dev_reset_done;
2165 /* Send marker. */
2166 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2167 MM_LUN_RESET) != QLA_SUCCESS)
2168 goto eh_dev_reset_done;
2170 ql4_printk(KERN_INFO, ha,
2171 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2172 ha->host_no, cmd->device->channel, cmd->device->id,
2173 cmd->device->lun);
2175 ret = SUCCESS;
2177 eh_dev_reset_done:
2179 return ret;
2183 * qla4xxx_eh_target_reset - callback for target reset.
2184 * @cmd: Pointer to Linux's SCSI command structure
2186 * This routine is called by the Linux OS to reset the target.
2188 static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2190 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2191 struct ddb_entry *ddb_entry = cmd->device->hostdata;
2192 int stat, ret;
2194 if (!ddb_entry)
2195 return FAILED;
2197 ret = iscsi_block_scsi_eh(cmd);
2198 if (ret)
2199 return ret;
2201 starget_printk(KERN_INFO, scsi_target(cmd->device),
2202 "WARM TARGET RESET ISSUED.\n");
2204 DEBUG2(printk(KERN_INFO
2205 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2206 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
2207 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
2208 ha->dpc_flags, cmd->result, cmd->allowed));
2210 stat = qla4xxx_reset_target(ha, ddb_entry);
2211 if (stat != QLA_SUCCESS) {
2212 starget_printk(KERN_INFO, scsi_target(cmd->device),
2213 "WARM TARGET RESET FAILED.\n");
2214 return FAILED;
2217 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2218 NULL)) {
2219 starget_printk(KERN_INFO, scsi_target(cmd->device),
2220 "WARM TARGET DEVICE RESET FAILED - "
2221 "waiting for commands.\n");
2222 return FAILED;
2225 /* Send marker. */
2226 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2227 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2228 starget_printk(KERN_INFO, scsi_target(cmd->device),
2229 "WARM TARGET DEVICE RESET FAILED - "
2230 "marker iocb failed.\n");
2231 return FAILED;
2234 starget_printk(KERN_INFO, scsi_target(cmd->device),
2235 "WARM TARGET RESET SUCCEEDED.\n");
2236 return SUCCESS;
2240 * qla4xxx_eh_host_reset - kernel callback
2241 * @cmd: Pointer to Linux's SCSI command structure
2243 * This routine is invoked by the Linux kernel to perform fatal error
2244 * recovery on the specified adapter.
2246 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2248 int return_status = FAILED;
2249 struct scsi_qla_host *ha;
2251 ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2253 if (ql4xdontresethba) {
2254 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2255 ha->host_no, __func__));
2256 return FAILED;
2259 ql4_printk(KERN_INFO, ha,
2260 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
2261 cmd->device->channel, cmd->device->id, cmd->device->lun);
2263 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2264 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
2265 "DEAD.\n", ha->host_no, cmd->device->channel,
2266 __func__));
2268 return FAILED;
2271 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2272 if (is_qla8022(ha))
2273 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2274 else
2275 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2278 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
2279 return_status = SUCCESS;
2281 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
2282 return_status == FAILED ? "FAILED" : "SUCCEEDED");
2284 return return_status;
2287 /* PCI AER driver recovers from all correctable errors w/o
2288 * driver intervention. For uncorrectable errors PCI AER
2289 * driver calls the following device driver's callbacks
2291 * - Fatal Errors - link_reset
2292 * - Non-Fatal Errors - driver's pci_error_detected() which
2293 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2295 * PCI AER driver calls
2296 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2297 * returns RECOVERED or NEED_RESET if fw_hung
2298 * NEED_RESET - driver's slot_reset()
2299 * DISCONNECT - device is dead & cannot recover
2300 * RECOVERED - driver's pci_resume()
2302 static pci_ers_result_t
2303 qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2305 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2307 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2308 ha->host_no, __func__, state);
2310 if (!is_aer_supported(ha))
2311 return PCI_ERS_RESULT_NONE;
2313 switch (state) {
2314 case pci_channel_io_normal:
2315 clear_bit(AF_EEH_BUSY, &ha->flags);
2316 return PCI_ERS_RESULT_CAN_RECOVER;
2317 case pci_channel_io_frozen:
2318 set_bit(AF_EEH_BUSY, &ha->flags);
2319 qla4xxx_mailbox_premature_completion(ha);
2320 qla4xxx_free_irqs(ha);
2321 pci_disable_device(pdev);
2322 /* Return back all IOs */
2323 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2324 return PCI_ERS_RESULT_NEED_RESET;
2325 case pci_channel_io_perm_failure:
2326 set_bit(AF_EEH_BUSY, &ha->flags);
2327 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2328 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2329 return PCI_ERS_RESULT_DISCONNECT;
2331 return PCI_ERS_RESULT_NEED_RESET;
2335 * qla4xxx_pci_mmio_enabled() gets called if
2336 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2337 * and read/write to the device still works.
2339 static pci_ers_result_t
2340 qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2342 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2344 if (!is_aer_supported(ha))
2345 return PCI_ERS_RESULT_NONE;
2347 return PCI_ERS_RESULT_RECOVERED;
2350 static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2352 uint32_t rval = QLA_ERROR;
2353 uint32_t ret = 0;
2354 int fn;
2355 struct pci_dev *other_pdev = NULL;
2357 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2359 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2361 if (test_bit(AF_ONLINE, &ha->flags)) {
2362 clear_bit(AF_ONLINE, &ha->flags);
2363 qla4xxx_mark_all_devices_missing(ha);
2364 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2367 fn = PCI_FUNC(ha->pdev->devfn);
2368 while (fn > 0) {
2369 fn--;
2370 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2371 "func %x\n", ha->host_no, __func__, fn);
2372 /* Get the pci device given the domain, bus,
2373 * slot/function number */
2374 other_pdev =
2375 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2376 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2377 fn));
2379 if (!other_pdev)
2380 continue;
2382 if (atomic_read(&other_pdev->enable_cnt)) {
2383 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2384 "func in enabled state%x\n", ha->host_no,
2385 __func__, fn);
2386 pci_dev_put(other_pdev);
2387 break;
2389 pci_dev_put(other_pdev);
2392 /* The first function on the card, the reset owner will
2393 * start & initialize the firmware. The other functions
2394 * on the card will reset the firmware context
2396 if (!fn) {
2397 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2398 "0x%x is the owner\n", ha->host_no, __func__,
2399 ha->pdev->devfn);
2401 qla4_8xxx_idc_lock(ha);
2402 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2403 QLA82XX_DEV_COLD);
2405 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2406 QLA82XX_IDC_VERSION);
2408 qla4_8xxx_idc_unlock(ha);
2409 clear_bit(AF_FW_RECOVERY, &ha->flags);
2410 rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2411 qla4_8xxx_idc_lock(ha);
2413 if (rval != QLA_SUCCESS) {
2414 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2415 "FAILED\n", ha->host_no, __func__);
2416 qla4_8xxx_clear_drv_active(ha);
2417 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2418 QLA82XX_DEV_FAILED);
2419 } else {
2420 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2421 "READY\n", ha->host_no, __func__);
2422 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2423 QLA82XX_DEV_READY);
2424 /* Clear driver state register */
2425 qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2426 qla4_8xxx_set_drv_active(ha);
2427 ret = qla4xxx_request_irqs(ha);
2428 if (ret) {
2429 ql4_printk(KERN_WARNING, ha, "Failed to "
2430 "reserve interrupt %d already in use.\n",
2431 ha->pdev->irq);
2432 rval = QLA_ERROR;
2433 } else {
2434 ha->isp_ops->enable_intrs(ha);
2435 rval = QLA_SUCCESS;
2438 qla4_8xxx_idc_unlock(ha);
2439 } else {
2440 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2441 "the reset owner\n", ha->host_no, __func__,
2442 ha->pdev->devfn);
2443 if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2444 QLA82XX_DEV_READY)) {
2445 clear_bit(AF_FW_RECOVERY, &ha->flags);
2446 rval = qla4xxx_initialize_adapter(ha,
2447 PRESERVE_DDB_LIST);
2448 if (rval == QLA_SUCCESS) {
2449 ret = qla4xxx_request_irqs(ha);
2450 if (ret) {
2451 ql4_printk(KERN_WARNING, ha, "Failed to"
2452 " reserve interrupt %d already in"
2453 " use.\n", ha->pdev->irq);
2454 rval = QLA_ERROR;
2455 } else {
2456 ha->isp_ops->enable_intrs(ha);
2457 rval = QLA_SUCCESS;
2460 qla4_8xxx_idc_lock(ha);
2461 qla4_8xxx_set_drv_active(ha);
2462 qla4_8xxx_idc_unlock(ha);
2465 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2466 return rval;
2469 static pci_ers_result_t
2470 qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2472 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2473 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2474 int rc;
2476 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2477 ha->host_no, __func__);
2479 if (!is_aer_supported(ha))
2480 return PCI_ERS_RESULT_NONE;
2482 /* Restore the saved state of PCIe device -
2483 * BAR registers, PCI Config space, PCIX, MSI,
2484 * IOV states
2486 pci_restore_state(pdev);
2488 /* pci_restore_state() clears the saved_state flag of the device
2489 * save restored state which resets saved_state flag
2491 pci_save_state(pdev);
2493 /* Initialize device or resume if in suspended state */
2494 rc = pci_enable_device(pdev);
2495 if (rc) {
2496 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
2497 "device after reset\n", ha->host_no, __func__);
2498 goto exit_slot_reset;
2501 ha->isp_ops->disable_intrs(ha);
2503 if (is_qla8022(ha)) {
2504 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2505 ret = PCI_ERS_RESULT_RECOVERED;
2506 goto exit_slot_reset;
2507 } else
2508 goto exit_slot_reset;
2511 exit_slot_reset:
2512 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2513 "device after reset\n", ha->host_no, __func__, ret);
2514 return ret;
2517 static void
2518 qla4xxx_pci_resume(struct pci_dev *pdev)
2520 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2521 int ret;
2523 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2524 ha->host_no, __func__);
2526 ret = qla4xxx_wait_for_hba_online(ha);
2527 if (ret != QLA_SUCCESS) {
2528 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2529 "resume I/O from slot/link_reset\n", ha->host_no,
2530 __func__);
2533 pci_cleanup_aer_uncorrect_error_status(pdev);
2534 clear_bit(AF_EEH_BUSY, &ha->flags);
2537 static struct pci_error_handlers qla4xxx_err_handler = {
2538 .error_detected = qla4xxx_pci_error_detected,
2539 .mmio_enabled = qla4xxx_pci_mmio_enabled,
2540 .slot_reset = qla4xxx_pci_slot_reset,
2541 .resume = qla4xxx_pci_resume,
2544 static struct pci_device_id qla4xxx_pci_tbl[] = {
2546 .vendor = PCI_VENDOR_ID_QLOGIC,
2547 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
2548 .subvendor = PCI_ANY_ID,
2549 .subdevice = PCI_ANY_ID,
2552 .vendor = PCI_VENDOR_ID_QLOGIC,
2553 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
2554 .subvendor = PCI_ANY_ID,
2555 .subdevice = PCI_ANY_ID,
2558 .vendor = PCI_VENDOR_ID_QLOGIC,
2559 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
2560 .subvendor = PCI_ANY_ID,
2561 .subdevice = PCI_ANY_ID,
2564 .vendor = PCI_VENDOR_ID_QLOGIC,
2565 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
2566 .subvendor = PCI_ANY_ID,
2567 .subdevice = PCI_ANY_ID,
2569 {0, 0},
2571 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2573 static struct pci_driver qla4xxx_pci_driver = {
2574 .name = DRIVER_NAME,
2575 .id_table = qla4xxx_pci_tbl,
2576 .probe = qla4xxx_probe_adapter,
2577 .remove = qla4xxx_remove_adapter,
2578 .err_handler = &qla4xxx_err_handler,
2581 static int __init qla4xxx_module_init(void)
2583 int ret;
2585 /* Allocate cache for SRBs. */
2586 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
2587 SLAB_HWCACHE_ALIGN, NULL);
2588 if (srb_cachep == NULL) {
2589 printk(KERN_ERR
2590 "%s: Unable to allocate SRB cache..."
2591 "Failing load!\n", DRIVER_NAME);
2592 ret = -ENOMEM;
2593 goto no_srp_cache;
2596 /* Derive version string. */
2597 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
2598 if (ql4xextended_error_logging)
2599 strcat(qla4xxx_version_str, "-debug");
2601 qla4xxx_scsi_transport =
2602 iscsi_register_transport(&qla4xxx_iscsi_transport);
2603 if (!qla4xxx_scsi_transport){
2604 ret = -ENODEV;
2605 goto release_srb_cache;
2608 ret = pci_register_driver(&qla4xxx_pci_driver);
2609 if (ret)
2610 goto unregister_transport;
2612 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2613 return 0;
2615 unregister_transport:
2616 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2617 release_srb_cache:
2618 kmem_cache_destroy(srb_cachep);
2619 no_srp_cache:
2620 return ret;
2623 static void __exit qla4xxx_module_exit(void)
2625 pci_unregister_driver(&qla4xxx_pci_driver);
2626 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2627 kmem_cache_destroy(srb_cachep);
2630 module_init(qla4xxx_module_init);
2631 module_exit(qla4xxx_module_exit);
2633 MODULE_AUTHOR("QLogic Corporation");
2634 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2635 MODULE_LICENSE("GPL");
2636 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);