• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/scsi/qla4xxx/
1/*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c)  2003-2006 QLogic Corporation
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
8#include <linux/slab.h>
9
10#include <scsi/scsi_tcq.h>
11#include <scsi/scsicam.h>
12
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"
18
19/*
20 * Driver version
21 */
22static char qla4xxx_version_str[40];
23
24/*
25 * SRB allocation cache
26 */
27static struct kmem_cache *srb_cachep;
28
29/*
30 * Module parameter information and variables
31 */
32int ql4xdiscoverywait = 60;
33module_param(ql4xdiscoverywait, int, S_IRUGO | S_IWUSR);
34MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
35
36int ql4xdontresethba = 0;
37module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
38MODULE_PARM_DESC(ql4xdontresethba,
39		"Don't reset the HBA for driver recovery \n"
40		" 0 - It will reset HBA (Default)\n"
41		" 1 - It will NOT reset HBA");
42
43int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
44module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
45MODULE_PARM_DESC(ql4xextended_error_logging,
46		 "Option to enable extended error logging, "
47		 "Default is 0 - no logging, 1 - debug logging");
48
49int ql4xenablemsix = 1;
50module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
51MODULE_PARM_DESC(ql4xenablemsix,
52		"Set to enable MSI or MSI-X interrupt mechanism.\n"
53		" 0 = enable INTx interrupt mechanism.\n"
54		" 1 = enable MSI-X interrupt mechanism (Default).\n"
55		" 2 = enable MSI interrupt mechanism.");
56
57#define QL4_DEF_QDEPTH 32
58
59/*
60 * SCSI host template entry points
61 */
62static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
63
64/*
65 * iSCSI template entry points
66 */
67static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
68			     enum iscsi_tgt_dscvr type, uint32_t enable,
69			     struct sockaddr *dst_addr);
70static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
71				  enum iscsi_param param, char *buf);
72static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
73				  enum iscsi_param param, char *buf);
74static int qla4xxx_host_get_param(struct Scsi_Host *shost,
75				  enum iscsi_host_param param, char *buf);
76static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
77static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
78
79/*
80 * SCSI host template entry points
81 */
82static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
83				void (*done) (struct scsi_cmnd *));
84static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
85static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
86static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
87static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
88static int qla4xxx_slave_alloc(struct scsi_device *device);
89static int qla4xxx_slave_configure(struct scsi_device *device);
90static void qla4xxx_slave_destroy(struct scsi_device *sdev);
91static void qla4xxx_scan_start(struct Scsi_Host *shost);
92
93static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
94    QLA82XX_LEGACY_INTR_CONFIG;
95
96static struct scsi_host_template qla4xxx_driver_template = {
97	.module			= THIS_MODULE,
98	.name			= DRIVER_NAME,
99	.proc_name		= DRIVER_NAME,
100	.queuecommand		= qla4xxx_queuecommand,
101
102	.eh_abort_handler	= qla4xxx_eh_abort,
103	.eh_device_reset_handler = qla4xxx_eh_device_reset,
104	.eh_target_reset_handler = qla4xxx_eh_target_reset,
105	.eh_host_reset_handler	= qla4xxx_eh_host_reset,
106	.eh_timed_out		= qla4xxx_eh_cmd_timed_out,
107
108	.slave_configure	= qla4xxx_slave_configure,
109	.slave_alloc		= qla4xxx_slave_alloc,
110	.slave_destroy		= qla4xxx_slave_destroy,
111
112	.scan_finished		= iscsi_scan_finished,
113	.scan_start		= qla4xxx_scan_start,
114
115	.this_id		= -1,
116	.cmd_per_lun		= 3,
117	.use_clustering		= ENABLE_CLUSTERING,
118	.sg_tablesize		= SG_ALL,
119
120	.max_sectors		= 0xFFFF,
121};
122
123static struct iscsi_transport qla4xxx_iscsi_transport = {
124	.owner			= THIS_MODULE,
125	.name			= DRIVER_NAME,
126	.caps			= CAP_FW_DB | CAP_SENDTARGETS_OFFLOAD |
127				  CAP_DATA_PATH_OFFLOAD,
128	.param_mask		= ISCSI_CONN_PORT | ISCSI_CONN_ADDRESS |
129				  ISCSI_TARGET_NAME | ISCSI_TPGT |
130				  ISCSI_TARGET_ALIAS,
131	.host_param_mask	= ISCSI_HOST_HWADDRESS |
132				  ISCSI_HOST_IPADDRESS |
133				  ISCSI_HOST_INITIATOR_NAME,
134	.tgt_dscvr		= qla4xxx_tgt_dscvr,
135	.get_conn_param		= qla4xxx_conn_get_param,
136	.get_session_param	= qla4xxx_sess_get_param,
137	.get_host_param		= qla4xxx_host_get_param,
138	.session_recovery_timedout = qla4xxx_recovery_timedout,
139};
140
141static struct scsi_transport_template *qla4xxx_scsi_transport;
142
143static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
144{
145	struct iscsi_cls_session *session;
146	struct ddb_entry *ddb_entry;
147
148	session = starget_to_session(scsi_target(sc->device));
149	ddb_entry = session->dd_data;
150
151	/* if we are not logged in then the LLD is going to clean up the cmd */
152	if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
153		return BLK_EH_RESET_TIMER;
154	else
155		return BLK_EH_NOT_HANDLED;
156}
157
158static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
159{
160	struct ddb_entry *ddb_entry = session->dd_data;
161	struct scsi_qla_host *ha = ddb_entry->ha;
162
163	if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
164		atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
165
166		DEBUG2(printk("scsi%ld: %s: ddb [%d] session recovery timeout "
167			      "of (%d) secs exhausted, marking device DEAD.\n",
168			      ha->host_no, __func__, ddb_entry->fw_ddb_index,
169			      QL4_SESS_RECOVERY_TMO));
170
171		qla4xxx_wake_dpc(ha);
172	}
173}
174
175static int qla4xxx_host_get_param(struct Scsi_Host *shost,
176				  enum iscsi_host_param param, char *buf)
177{
178	struct scsi_qla_host *ha = to_qla_host(shost);
179	int len;
180
181	switch (param) {
182	case ISCSI_HOST_PARAM_HWADDRESS:
183		len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
184		break;
185	case ISCSI_HOST_PARAM_IPADDRESS:
186		len = sprintf(buf, "%d.%d.%d.%d\n", ha->ip_address[0],
187			      ha->ip_address[1], ha->ip_address[2],
188			      ha->ip_address[3]);
189		break;
190	case ISCSI_HOST_PARAM_INITIATOR_NAME:
191		len = sprintf(buf, "%s\n", ha->name_string);
192		break;
193	default:
194		return -ENOSYS;
195	}
196
197	return len;
198}
199
200static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
201				  enum iscsi_param param, char *buf)
202{
203	struct ddb_entry *ddb_entry = sess->dd_data;
204	int len;
205
206	switch (param) {
207	case ISCSI_PARAM_TARGET_NAME:
208		len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
209			       ddb_entry->iscsi_name);
210		break;
211	case ISCSI_PARAM_TPGT:
212		len = sprintf(buf, "%u\n", ddb_entry->tpgt);
213		break;
214	case ISCSI_PARAM_TARGET_ALIAS:
215		len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
216		    ddb_entry->iscsi_alias);
217		break;
218	default:
219		return -ENOSYS;
220	}
221
222	return len;
223}
224
225static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
226				  enum iscsi_param param, char *buf)
227{
228	struct iscsi_cls_session *session;
229	struct ddb_entry *ddb_entry;
230	int len;
231
232	session = iscsi_dev_to_session(conn->dev.parent);
233	ddb_entry = session->dd_data;
234
235	switch (param) {
236	case ISCSI_PARAM_CONN_PORT:
237		len = sprintf(buf, "%hu\n", ddb_entry->port);
238		break;
239	case ISCSI_PARAM_CONN_ADDRESS:
240		/* TODO: what are the ipv6 bits */
241		len = sprintf(buf, "%pI4\n", &ddb_entry->ip_addr);
242		break;
243	default:
244		return -ENOSYS;
245	}
246
247	return len;
248}
249
250static int qla4xxx_tgt_dscvr(struct Scsi_Host *shost,
251			     enum iscsi_tgt_dscvr type, uint32_t enable,
252			     struct sockaddr *dst_addr)
253{
254	struct scsi_qla_host *ha;
255	struct sockaddr_in *addr;
256	struct sockaddr_in6 *addr6;
257	int ret = 0;
258
259	ha = (struct scsi_qla_host *) shost->hostdata;
260
261	switch (type) {
262	case ISCSI_TGT_DSCVR_SEND_TARGETS:
263		if (dst_addr->sa_family == AF_INET) {
264			addr = (struct sockaddr_in *)dst_addr;
265			if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
266					      addr->sin_port) != QLA_SUCCESS)
267				ret = -EIO;
268		} else if (dst_addr->sa_family == AF_INET6) {
269			/*
270			 * TODO: fix qla4xxx_send_tgts
271			 */
272			addr6 = (struct sockaddr_in6 *)dst_addr;
273			if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
274					      addr6->sin6_port) != QLA_SUCCESS)
275				ret = -EIO;
276		} else
277			ret = -ENOSYS;
278		break;
279	default:
280		ret = -ENOSYS;
281	}
282	return ret;
283}
284
285void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
286{
287	if (!ddb_entry->sess)
288		return;
289
290	if (ddb_entry->conn) {
291		atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
292		iscsi_remove_session(ddb_entry->sess);
293	}
294	iscsi_free_session(ddb_entry->sess);
295}
296
297int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
298{
299	int err;
300
301	ddb_entry->sess->recovery_tmo = QL4_SESS_RECOVERY_TMO;
302
303	err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
304	if (err) {
305		DEBUG2(printk(KERN_ERR "Could not add session.\n"));
306		return err;
307	}
308
309	ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0, 0);
310	if (!ddb_entry->conn) {
311		iscsi_remove_session(ddb_entry->sess);
312		DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
313		return -ENOMEM;
314	}
315
316	/* finally ready to go */
317	iscsi_unblock_session(ddb_entry->sess);
318	return 0;
319}
320
321struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
322{
323	struct ddb_entry *ddb_entry;
324	struct iscsi_cls_session *sess;
325
326	sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport,
327				   sizeof(struct ddb_entry));
328	if (!sess)
329		return NULL;
330
331	ddb_entry = sess->dd_data;
332	memset(ddb_entry, 0, sizeof(*ddb_entry));
333	ddb_entry->ha = ha;
334	ddb_entry->sess = sess;
335	return ddb_entry;
336}
337
338static void qla4xxx_scan_start(struct Scsi_Host *shost)
339{
340	struct scsi_qla_host *ha = shost_priv(shost);
341	struct ddb_entry *ddb_entry, *ddbtemp;
342
343	/* finish setup of sessions that were already setup in firmware */
344	list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
345		if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
346			qla4xxx_add_sess(ddb_entry);
347	}
348}
349
350/*
351 * Timer routines
352 */
353
354static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
355				unsigned long interval)
356{
357	DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
358		     __func__, ha->host->host_no));
359	init_timer(&ha->timer);
360	ha->timer.expires = jiffies + interval * HZ;
361	ha->timer.data = (unsigned long)ha;
362	ha->timer.function = (void (*)(unsigned long))func;
363	add_timer(&ha->timer);
364	ha->timer_active = 1;
365}
366
367static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
368{
369	del_timer_sync(&ha->timer);
370	ha->timer_active = 0;
371}
372
373/***
374 * qla4xxx_mark_device_missing - mark a device as missing.
375 * @ha: Pointer to host adapter structure.
376 * @ddb_entry: Pointer to device database entry
377 *
378 * This routine marks a device missing and close connection.
379 **/
380void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
381				 struct ddb_entry *ddb_entry)
382{
383	if ((atomic_read(&ddb_entry->state) != DDB_STATE_DEAD)) {
384		atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
385		DEBUG2(printk("scsi%ld: ddb [%d] marked MISSING\n",
386		    ha->host_no, ddb_entry->fw_ddb_index));
387	} else
388		DEBUG2(printk("scsi%ld: ddb [%d] DEAD\n", ha->host_no,
389		    ddb_entry->fw_ddb_index))
390
391	iscsi_block_session(ddb_entry->sess);
392	iscsi_conn_error_event(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
393}
394
395/**
396 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
397 * @ha: Pointer to host adapter structure.
398 *
399 * This routine marks a device missing and resets the relogin retry count.
400 **/
401void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
402{
403	struct ddb_entry *ddb_entry, *ddbtemp;
404	list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
405		qla4xxx_mark_device_missing(ha, ddb_entry);
406	}
407}
408
409static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
410				       struct ddb_entry *ddb_entry,
411				       struct scsi_cmnd *cmd,
412				       void (*done)(struct scsi_cmnd *))
413{
414	struct srb *srb;
415
416	srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
417	if (!srb)
418		return srb;
419
420	kref_init(&srb->srb_ref);
421	srb->ha = ha;
422	srb->ddb = ddb_entry;
423	srb->cmd = cmd;
424	srb->flags = 0;
425	CMD_SP(cmd) = (void *)srb;
426	cmd->scsi_done = done;
427
428	return srb;
429}
430
431static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
432{
433	struct scsi_cmnd *cmd = srb->cmd;
434
435	if (srb->flags & SRB_DMA_VALID) {
436		scsi_dma_unmap(cmd);
437		srb->flags &= ~SRB_DMA_VALID;
438	}
439	CMD_SP(cmd) = NULL;
440}
441
442void qla4xxx_srb_compl(struct kref *ref)
443{
444	struct srb *srb = container_of(ref, struct srb, srb_ref);
445	struct scsi_cmnd *cmd = srb->cmd;
446	struct scsi_qla_host *ha = srb->ha;
447
448	qla4xxx_srb_free_dma(ha, srb);
449
450	mempool_free(srb, ha->srb_mempool);
451
452	cmd->scsi_done(cmd);
453}
454
455/**
456 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
457 * @cmd: Pointer to Linux's SCSI command structure
458 * @done_fn: Function that the driver calls to notify the SCSI mid-layer
459 *	that the command has been processed.
460 *
461 * Remarks:
462 * This routine is invoked by Linux to send a SCSI command to the driver.
463 * The mid-level driver tries to ensure that queuecommand never gets
464 * invoked concurrently with itself or the interrupt handler (although
465 * the interrupt handler may call this routine as part of request-
466 * completion handling).   Unfortunely, it sometimes calls the scheduler
467 * in interrupt context which is a big NO! NO!.
468 **/
469static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
470				void (*done)(struct scsi_cmnd *))
471{
472	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
473	struct ddb_entry *ddb_entry = cmd->device->hostdata;
474	struct iscsi_cls_session *sess = ddb_entry->sess;
475	struct srb *srb;
476	int rval;
477
478	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
479		if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
480			cmd->result = DID_NO_CONNECT << 16;
481		else
482			cmd->result = DID_REQUEUE << 16;
483		goto qc_fail_command;
484	}
485
486	if (!sess) {
487		cmd->result = DID_IMM_RETRY << 16;
488		goto qc_fail_command;
489	}
490
491	rval = iscsi_session_chkready(sess);
492	if (rval) {
493		cmd->result = rval;
494		goto qc_fail_command;
495	}
496
497	if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
498		if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
499			cmd->result = DID_NO_CONNECT << 16;
500			goto qc_fail_command;
501		}
502		return SCSI_MLQUEUE_TARGET_BUSY;
503	}
504
505	if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
506	    test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
507	    test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
508	    test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
509	    test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
510	    !test_bit(AF_ONLINE, &ha->flags) ||
511	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
512		goto qc_host_busy;
513
514	spin_unlock_irq(ha->host->host_lock);
515
516	srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
517	if (!srb)
518		goto qc_host_busy_lock;
519
520	rval = qla4xxx_send_command_to_isp(ha, srb);
521	if (rval != QLA_SUCCESS)
522		goto qc_host_busy_free_sp;
523
524	spin_lock_irq(ha->host->host_lock);
525	return 0;
526
527qc_host_busy_free_sp:
528	qla4xxx_srb_free_dma(ha, srb);
529	mempool_free(srb, ha->srb_mempool);
530
531qc_host_busy_lock:
532	spin_lock_irq(ha->host->host_lock);
533
534qc_host_busy:
535	return SCSI_MLQUEUE_HOST_BUSY;
536
537qc_fail_command:
538	done(cmd);
539
540	return 0;
541}
542
543/**
544 * qla4xxx_mem_free - frees memory allocated to adapter
545 * @ha: Pointer to host adapter structure.
546 *
547 * Frees memory previously allocated by qla4xxx_mem_alloc
548 **/
549static void qla4xxx_mem_free(struct scsi_qla_host *ha)
550{
551	if (ha->queues)
552		dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
553				  ha->queues_dma);
554
555	ha->queues_len = 0;
556	ha->queues = NULL;
557	ha->queues_dma = 0;
558	ha->request_ring = NULL;
559	ha->request_dma = 0;
560	ha->response_ring = NULL;
561	ha->response_dma = 0;
562	ha->shadow_regs = NULL;
563	ha->shadow_regs_dma = 0;
564
565	/* Free srb pool. */
566	if (ha->srb_mempool)
567		mempool_destroy(ha->srb_mempool);
568
569	ha->srb_mempool = NULL;
570
571	/* release io space registers  */
572	if (is_qla8022(ha)) {
573		if (ha->nx_pcibase)
574			iounmap(
575			    (struct device_reg_82xx __iomem *)ha->nx_pcibase);
576
577		if (ha->nx_db_wr_ptr)
578			iounmap(
579			    (struct device_reg_82xx __iomem *)ha->nx_db_wr_ptr);
580	} else if (ha->reg)
581		iounmap(ha->reg);
582	pci_release_regions(ha->pdev);
583}
584
585/**
586 * qla4xxx_mem_alloc - allocates memory for use by adapter.
587 * @ha: Pointer to host adapter structure
588 *
589 * Allocates DMA memory for request and response queues. Also allocates memory
590 * for srbs.
591 **/
592static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
593{
594	unsigned long align;
595
596	/* Allocate contiguous block of DMA memory for queues. */
597	ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
598			  (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
599			  sizeof(struct shadow_regs) +
600			  MEM_ALIGN_VALUE +
601			  (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
602	ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
603					&ha->queues_dma, GFP_KERNEL);
604	if (ha->queues == NULL) {
605		ql4_printk(KERN_WARNING, ha,
606		    "Memory Allocation failed - queues.\n");
607
608		goto mem_alloc_error_exit;
609	}
610	memset(ha->queues, 0, ha->queues_len);
611
612	/*
613	 * As per RISC alignment requirements -- the bus-address must be a
614	 * multiple of the request-ring size (in bytes).
615	 */
616	align = 0;
617	if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
618		align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
619					   (MEM_ALIGN_VALUE - 1));
620
621	/* Update request and response queue pointers. */
622	ha->request_dma = ha->queues_dma + align;
623	ha->request_ring = (struct queue_entry *) (ha->queues + align);
624	ha->response_dma = ha->queues_dma + align +
625		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
626	ha->response_ring = (struct queue_entry *) (ha->queues + align +
627						    (REQUEST_QUEUE_DEPTH *
628						     QUEUE_SIZE));
629	ha->shadow_regs_dma = ha->queues_dma + align +
630		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
631		(RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
632	ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
633						  (REQUEST_QUEUE_DEPTH *
634						   QUEUE_SIZE) +
635						  (RESPONSE_QUEUE_DEPTH *
636						   QUEUE_SIZE));
637
638	/* Allocate memory for srb pool. */
639	ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
640					 mempool_free_slab, srb_cachep);
641	if (ha->srb_mempool == NULL) {
642		ql4_printk(KERN_WARNING, ha,
643		    "Memory Allocation failed - SRB Pool.\n");
644
645		goto mem_alloc_error_exit;
646	}
647
648	return QLA_SUCCESS;
649
650mem_alloc_error_exit:
651	qla4xxx_mem_free(ha);
652	return QLA_ERROR;
653}
654
655/**
656 * qla4_8xxx_check_fw_alive  - Check firmware health
657 * @ha: Pointer to host adapter structure.
658 *
659 * Context: Interrupt
660 **/
661static void qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
662{
663	uint32_t fw_heartbeat_counter, halt_status;
664
665	fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
666	/* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
667	if (fw_heartbeat_counter == 0xffffffff) {
668		DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
669		    "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
670		    ha->host_no, __func__));
671		return;
672	}
673
674	if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
675		ha->seconds_since_last_heartbeat++;
676		/* FW not alive after 2 seconds */
677		if (ha->seconds_since_last_heartbeat == 2) {
678			ha->seconds_since_last_heartbeat = 0;
679			halt_status = qla4_8xxx_rd_32(ha,
680			    QLA82XX_PEG_HALT_STATUS1);
681
682			/* Since we cannot change dev_state in interrupt
683			 * context, set appropriate DPC flag then wakeup
684			 * DPC */
685			if (halt_status & HALT_STATUS_UNRECOVERABLE)
686				set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
687			else {
688				printk("scsi%ld: %s: detect abort needed!\n",
689				    ha->host_no, __func__);
690				set_bit(DPC_RESET_HA, &ha->dpc_flags);
691			}
692			qla4xxx_wake_dpc(ha);
693			qla4xxx_mailbox_premature_completion(ha);
694		}
695	}
696	ha->fw_heartbeat_counter = fw_heartbeat_counter;
697}
698
699/**
700 * qla4_8xxx_watchdog - Poll dev state
701 * @ha: Pointer to host adapter structure.
702 *
703 * Context: Interrupt
704 **/
705void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
706{
707	uint32_t dev_state;
708
709	dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
710
711	/* don't poll if reset is going on */
712	if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags)) {
713		if (dev_state == QLA82XX_DEV_NEED_RESET &&
714		    !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
715			printk("scsi%ld: %s: HW State: NEED RESET!\n",
716			    ha->host_no, __func__);
717			set_bit(DPC_RESET_HA, &ha->dpc_flags);
718			qla4xxx_wake_dpc(ha);
719			qla4xxx_mailbox_premature_completion(ha);
720		} else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
721		    !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
722			printk("scsi%ld: %s: HW State: NEED QUIES!\n",
723			    ha->host_no, __func__);
724			set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
725			qla4xxx_wake_dpc(ha);
726		} else  {
727			/* Check firmware health */
728			qla4_8xxx_check_fw_alive(ha);
729		}
730	}
731}
732
733/**
734 * qla4xxx_timer - checks every second for work to do.
735 * @ha: Pointer to host adapter structure.
736 **/
737static void qla4xxx_timer(struct scsi_qla_host *ha)
738{
739	struct ddb_entry *ddb_entry, *dtemp;
740	int start_dpc = 0;
741	uint16_t w;
742
743	/* If we are in the middle of AER/EEH processing
744	 * skip any processing and reschedule the timer
745	 */
746	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
747		mod_timer(&ha->timer, jiffies + HZ);
748		return;
749	}
750
751	/* Hardware read to trigger an EEH error during mailbox waits. */
752	if (!pci_channel_offline(ha->pdev))
753		pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
754
755	if (test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
756		DEBUG2(ql4_printk(KERN_INFO, ha, "%s exited. HBA GOING AWAY\n",
757		    __func__));
758		return;
759	}
760
761	if (is_qla8022(ha)) {
762		qla4_8xxx_watchdog(ha);
763	}
764
765	/* Search for relogin's to time-out and port down retry. */
766	list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
767		/* Count down time between sending relogins */
768		if (adapter_up(ha) &&
769		    !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
770		    atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
771			if (atomic_read(&ddb_entry->retry_relogin_timer) !=
772			    INVALID_ENTRY) {
773				if (atomic_read(&ddb_entry->retry_relogin_timer)
774				    		== 0) {
775					atomic_set(&ddb_entry->
776						retry_relogin_timer,
777						INVALID_ENTRY);
778					set_bit(DPC_RELOGIN_DEVICE,
779						&ha->dpc_flags);
780					set_bit(DF_RELOGIN, &ddb_entry->flags);
781					DEBUG2(printk("scsi%ld: %s: ddb [%d]"
782						      " login device\n",
783						      ha->host_no, __func__,
784						      ddb_entry->fw_ddb_index));
785				} else
786					atomic_dec(&ddb_entry->
787							retry_relogin_timer);
788			}
789		}
790
791		/* Wait for relogin to timeout */
792		if (atomic_read(&ddb_entry->relogin_timer) &&
793		    (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
794			/*
795			 * If the relogin times out and the device is
796			 * still NOT ONLINE then try and relogin again.
797			 */
798			if (atomic_read(&ddb_entry->state) !=
799			    DDB_STATE_ONLINE &&
800			    ddb_entry->fw_ddb_device_state ==
801			    DDB_DS_SESSION_FAILED) {
802				/* Reset retry relogin timer */
803				atomic_inc(&ddb_entry->relogin_retry_count);
804				DEBUG2(printk("scsi%ld: ddb [%d] relogin"
805					      " timed out-retrying"
806					      " relogin (%d)\n",
807					      ha->host_no,
808					      ddb_entry->fw_ddb_index,
809					      atomic_read(&ddb_entry->
810							  relogin_retry_count))
811					);
812				start_dpc++;
813				DEBUG(printk("scsi%ld:%d:%d: ddb [%d] "
814					     "initate relogin after"
815					     " %d seconds\n",
816					     ha->host_no, ddb_entry->bus,
817					     ddb_entry->target,
818					     ddb_entry->fw_ddb_index,
819					     ddb_entry->default_time2wait + 4)
820					);
821
822				atomic_set(&ddb_entry->retry_relogin_timer,
823					   ddb_entry->default_time2wait + 4);
824			}
825		}
826	}
827
828	if (!is_qla8022(ha)) {
829		/* Check for heartbeat interval. */
830		if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
831		    ha->heartbeat_interval != 0) {
832			ha->seconds_since_last_heartbeat++;
833			if (ha->seconds_since_last_heartbeat >
834			    ha->heartbeat_interval + 2)
835				set_bit(DPC_RESET_HA, &ha->dpc_flags);
836		}
837	}
838
839	/* Wakeup the dpc routine for this adapter, if needed. */
840	if ((start_dpc ||
841	     test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
842	     test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
843	     test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
844	     test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
845	     test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
846	     test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
847	     test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
848	     test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
849	     test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
850	     test_bit(DPC_AEN, &ha->dpc_flags)) &&
851	     !test_bit(AF_DPC_SCHEDULED, &ha->flags) &&
852	     ha->dpc_thread) {
853		DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
854			      " - dpc flags = 0x%lx\n",
855			      ha->host_no, __func__, ha->dpc_flags));
856		qla4xxx_wake_dpc(ha);
857	}
858
859	/* Reschedule timer thread to call us back in one second */
860	mod_timer(&ha->timer, jiffies + HZ);
861
862	DEBUG2(ha->seconds_since_last_intr++);
863}
864
865/**
866 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
867 * @ha: Pointer to host adapter structure.
868 *
869 * This routine stalls the driver until all outstanding commands are returned.
870 * Caller must release the Hardware Lock prior to calling this routine.
871 **/
872static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
873{
874	uint32_t index = 0;
875	unsigned long flags;
876	struct scsi_cmnd *cmd;
877
878	unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
879
880	DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
881	    "complete\n", WAIT_CMD_TOV));
882
883	while (!time_after_eq(jiffies, wtime)) {
884		spin_lock_irqsave(&ha->hardware_lock, flags);
885		/* Find a command that hasn't completed. */
886		for (index = 0; index < ha->host->can_queue; index++) {
887			cmd = scsi_host_find_tag(ha->host, index);
888			if (cmd != NULL)
889				break;
890		}
891		spin_unlock_irqrestore(&ha->hardware_lock, flags);
892
893		/* If No Commands are pending, wait is complete */
894		if (index == ha->host->can_queue)
895			return QLA_SUCCESS;
896
897		msleep(1000);
898	}
899	/* If we timed out on waiting for commands to come back
900	 * return ERROR. */
901	return QLA_ERROR;
902}
903
904int qla4xxx_hw_reset(struct scsi_qla_host *ha)
905{
906	uint32_t ctrl_status;
907	unsigned long flags = 0;
908
909	DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
910
911	if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
912		return QLA_ERROR;
913
914	spin_lock_irqsave(&ha->hardware_lock, flags);
915
916	/*
917	 * If the SCSI Reset Interrupt bit is set, clear it.
918	 * Otherwise, the Soft Reset won't work.
919	 */
920	ctrl_status = readw(&ha->reg->ctrl_status);
921	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
922		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
923
924	/* Issue Soft Reset */
925	writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
926	readl(&ha->reg->ctrl_status);
927
928	spin_unlock_irqrestore(&ha->hardware_lock, flags);
929	return QLA_SUCCESS;
930}
931
932/**
933 * qla4xxx_soft_reset - performs soft reset.
934 * @ha: Pointer to host adapter structure.
935 **/
936int qla4xxx_soft_reset(struct scsi_qla_host *ha)
937{
938	uint32_t max_wait_time;
939	unsigned long flags = 0;
940	int status = QLA_ERROR;
941	uint32_t ctrl_status;
942
943	qla4xxx_hw_reset(ha);
944
945	/* Wait until the Network Reset Intr bit is cleared */
946	max_wait_time = RESET_INTR_TOV;
947	do {
948		spin_lock_irqsave(&ha->hardware_lock, flags);
949		ctrl_status = readw(&ha->reg->ctrl_status);
950		spin_unlock_irqrestore(&ha->hardware_lock, flags);
951
952		if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
953			break;
954
955		msleep(1000);
956	} while ((--max_wait_time));
957
958	if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
959		DEBUG2(printk(KERN_WARNING
960			      "scsi%ld: Network Reset Intr not cleared by "
961			      "Network function, clearing it now!\n",
962			      ha->host_no));
963		spin_lock_irqsave(&ha->hardware_lock, flags);
964		writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
965		readl(&ha->reg->ctrl_status);
966		spin_unlock_irqrestore(&ha->hardware_lock, flags);
967	}
968
969	/* Wait until the firmware tells us the Soft Reset is done */
970	max_wait_time = SOFT_RESET_TOV;
971	do {
972		spin_lock_irqsave(&ha->hardware_lock, flags);
973		ctrl_status = readw(&ha->reg->ctrl_status);
974		spin_unlock_irqrestore(&ha->hardware_lock, flags);
975
976		if ((ctrl_status & CSR_SOFT_RESET) == 0) {
977			status = QLA_SUCCESS;
978			break;
979		}
980
981		msleep(1000);
982	} while ((--max_wait_time));
983
984	/*
985	 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
986	 * after the soft reset has taken place.
987	 */
988	spin_lock_irqsave(&ha->hardware_lock, flags);
989	ctrl_status = readw(&ha->reg->ctrl_status);
990	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
991		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
992		readl(&ha->reg->ctrl_status);
993	}
994	spin_unlock_irqrestore(&ha->hardware_lock, flags);
995
996	if (max_wait_time == 0) {
997		/* Issue Force Soft Reset */
998		spin_lock_irqsave(&ha->hardware_lock, flags);
999		writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
1000		readl(&ha->reg->ctrl_status);
1001		spin_unlock_irqrestore(&ha->hardware_lock, flags);
1002		/* Wait until the firmware tells us the Soft Reset is done */
1003		max_wait_time = SOFT_RESET_TOV;
1004		do {
1005			spin_lock_irqsave(&ha->hardware_lock, flags);
1006			ctrl_status = readw(&ha->reg->ctrl_status);
1007			spin_unlock_irqrestore(&ha->hardware_lock, flags);
1008
1009			if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
1010				status = QLA_SUCCESS;
1011				break;
1012			}
1013
1014			msleep(1000);
1015		} while ((--max_wait_time));
1016	}
1017
1018	return status;
1019}
1020
1021/**
1022 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
1023 * @ha: Pointer to host adapter structure.
1024 * @res: returned scsi status
1025 *
1026 * This routine is called just prior to a HARD RESET to return all
1027 * outstanding commands back to the Operating System.
1028 * Caller should make sure that the following locks are released
1029 * before this calling routine: Hardware lock, and io_request_lock.
1030 **/
1031static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
1032{
1033	struct srb *srb;
1034	int i;
1035	unsigned long flags;
1036
1037	spin_lock_irqsave(&ha->hardware_lock, flags);
1038	for (i = 0; i < ha->host->can_queue; i++) {
1039		srb = qla4xxx_del_from_active_array(ha, i);
1040		if (srb != NULL) {
1041			srb->cmd->result = res;
1042			kref_put(&srb->srb_ref, qla4xxx_srb_compl);
1043		}
1044	}
1045	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1046}
1047
1048void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
1049{
1050	clear_bit(AF_ONLINE, &ha->flags);
1051
1052	/* Disable the board */
1053	ql4_printk(KERN_INFO, ha, "Disabling the board\n");
1054	set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1055
1056	qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
1057	qla4xxx_mark_all_devices_missing(ha);
1058	clear_bit(AF_INIT_DONE, &ha->flags);
1059}
1060
1061/**
1062 * qla4xxx_recover_adapter - recovers adapter after a fatal error
1063 * @ha: Pointer to host adapter structure.
1064 **/
1065static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
1066{
1067	int status = QLA_ERROR;
1068	uint8_t reset_chip = 0;
1069
1070	/* Stall incoming I/O until we are done */
1071	scsi_block_requests(ha->host);
1072	clear_bit(AF_ONLINE, &ha->flags);
1073
1074	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
1075
1076	set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1077
1078	if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1079		reset_chip = 1;
1080
1081	/* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
1082	 * do not reset adapter, jump to initialize_adapter */
1083	if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1084		status = QLA_SUCCESS;
1085		goto recover_ha_init_adapter;
1086	}
1087
1088	/* For the ISP-82xx adapter, issue a stop_firmware if invoked
1089	 * from eh_host_reset or ioctl module */
1090	if (is_qla8022(ha) && !reset_chip &&
1091	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
1092
1093		DEBUG2(ql4_printk(KERN_INFO, ha,
1094		    "scsi%ld: %s - Performing stop_firmware...\n",
1095		    ha->host_no, __func__));
1096		status = ha->isp_ops->reset_firmware(ha);
1097		if (status == QLA_SUCCESS) {
1098			qla4xxx_cmd_wait(ha);
1099			ha->isp_ops->disable_intrs(ha);
1100			qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1101			qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1102		} else {
1103			/* If the stop_firmware fails then
1104			 * reset the entire chip */
1105			reset_chip = 1;
1106			clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1107			set_bit(DPC_RESET_HA, &ha->dpc_flags);
1108		}
1109	}
1110
1111	/* Issue full chip reset if recovering from a catastrophic error,
1112	 * or if stop_firmware fails for ISP-82xx.
1113	 * This is the default case for ISP-4xxx */
1114	if (!is_qla8022(ha) || reset_chip) {
1115		qla4xxx_cmd_wait(ha);
1116		qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1117		qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1118		DEBUG2(ql4_printk(KERN_INFO, ha,
1119		    "scsi%ld: %s - Performing chip reset..\n",
1120		    ha->host_no, __func__));
1121		status = ha->isp_ops->reset_chip(ha);
1122	}
1123
1124	/* Flush any pending ddb changed AENs */
1125	qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1126
1127recover_ha_init_adapter:
1128	/* Upon successful firmware/chip reset, re-initialize the adapter */
1129	if (status == QLA_SUCCESS) {
1130		/* For ISP-4xxx, force function 1 to always initialize
1131		 * before function 3 to prevent both funcions from
1132		 * stepping on top of the other */
1133		if (!is_qla8022(ha) && (ha->mac_index == 3))
1134			ssleep(6);
1135
1136		/* NOTE: AF_ONLINE flag set upon successful completion of
1137		 *       qla4xxx_initialize_adapter */
1138		status = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
1139	}
1140
1141	/* Retry failed adapter initialization, if necessary
1142	 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
1143	 * case to prevent ping-pong resets between functions */
1144	if (!test_bit(AF_ONLINE, &ha->flags) &&
1145	    !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1146		/* Adapter initialization failed, see if we can retry
1147		 * resetting the ha.
1148		 * Since we don't want to block the DPC for too long
1149		 * with multiple resets in the same thread,
1150		 * utilize DPC to retry */
1151		if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
1152			ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
1153			DEBUG2(printk("scsi%ld: recover adapter - retrying "
1154				      "(%d) more times\n", ha->host_no,
1155				      ha->retry_reset_ha_cnt));
1156			set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1157			status = QLA_ERROR;
1158		} else {
1159			if (ha->retry_reset_ha_cnt > 0) {
1160				/* Schedule another Reset HA--DPC will retry */
1161				ha->retry_reset_ha_cnt--;
1162				DEBUG2(printk("scsi%ld: recover adapter - "
1163					      "retry remaining %d\n",
1164					      ha->host_no,
1165					      ha->retry_reset_ha_cnt));
1166				status = QLA_ERROR;
1167			}
1168
1169			if (ha->retry_reset_ha_cnt == 0) {
1170				/* Recover adapter retries have been exhausted.
1171				 * Adapter DEAD */
1172				DEBUG2(printk("scsi%ld: recover adapter "
1173					      "failed - board disabled\n",
1174					      ha->host_no));
1175				qla4xxx_dead_adapter_cleanup(ha);
1176				clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1177				clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1178				clear_bit(DPC_RESET_HA_FW_CONTEXT,
1179					  &ha->dpc_flags);
1180				status = QLA_ERROR;
1181			}
1182		}
1183	} else {
1184		clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1185		clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1186		clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
1187	}
1188
1189	ha->adapter_error_count++;
1190
1191	if (test_bit(AF_ONLINE, &ha->flags))
1192		ha->isp_ops->enable_intrs(ha);
1193
1194	scsi_unblock_requests(ha->host);
1195
1196	clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
1197	DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
1198	    status == QLA_ERROR ? "FAILED" : "SUCCEDED"));
1199
1200	return status;
1201}
1202
1203void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
1204{
1205	if (ha->dpc_thread &&
1206	    !test_bit(AF_DPC_SCHEDULED, &ha->flags)) {
1207		set_bit(AF_DPC_SCHEDULED, &ha->flags);
1208		queue_work(ha->dpc_thread, &ha->dpc_work);
1209	}
1210}
1211
1212/**
1213 * qla4xxx_do_dpc - dpc routine
1214 * @data: in our case pointer to adapter structure
1215 *
1216 * This routine is a task that is schedule by the interrupt handler
1217 * to perform the background processing for interrupts.  We put it
1218 * on a task queue that is consumed whenever the scheduler runs; that's
1219 * so you can do anything (i.e. put the process to sleep etc).  In fact,
1220 * the mid-level tries to sleep when it reaches the driver threshold
1221 * "host->can_queue". This can cause a panic if we were in our interrupt code.
1222 **/
1223static void qla4xxx_do_dpc(struct work_struct *work)
1224{
1225	struct scsi_qla_host *ha =
1226		container_of(work, struct scsi_qla_host, dpc_work);
1227	struct ddb_entry *ddb_entry, *dtemp;
1228	int status = QLA_ERROR;
1229
1230	DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
1231	    "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
1232	    ha->host_no, __func__, ha->flags, ha->dpc_flags))
1233
1234	/* Initialization not yet finished. Don't do anything yet. */
1235	if (!test_bit(AF_INIT_DONE, &ha->flags))
1236		goto do_dpc_exit;
1237
1238	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
1239		DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
1240		    ha->host_no, __func__, ha->flags));
1241		goto do_dpc_exit;
1242	}
1243
1244	/* HBA is in the process of being permanently disabled.
1245	 * Don't process anything */
1246	if (test_bit(AF_HBA_GOING_AWAY, &ha->flags))
1247		return;
1248
1249	if (is_qla8022(ha)) {
1250		if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
1251			qla4_8xxx_idc_lock(ha);
1252			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
1253			    QLA82XX_DEV_FAILED);
1254			qla4_8xxx_idc_unlock(ha);
1255			ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
1256			qla4_8xxx_device_state_handler(ha);
1257		}
1258		if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
1259			qla4_8xxx_need_qsnt_handler(ha);
1260		}
1261	}
1262
1263	if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
1264	    (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
1265	    test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
1266	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
1267		if (ql4xdontresethba) {
1268			DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
1269			    ha->host_no, __func__));
1270			clear_bit(DPC_RESET_HA, &ha->dpc_flags);
1271			clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1272			clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
1273			goto dpc_post_reset_ha;
1274		}
1275		if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
1276		    test_bit(DPC_RESET_HA, &ha->dpc_flags))
1277			qla4xxx_recover_adapter(ha);
1278
1279		if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1280			uint8_t wait_time = RESET_INTR_TOV;
1281
1282			while ((readw(&ha->reg->ctrl_status) &
1283				(CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1284				if (--wait_time == 0)
1285					break;
1286				msleep(1000);
1287			}
1288			if (wait_time == 0)
1289				DEBUG2(printk("scsi%ld: %s: SR|FSR "
1290					      "bit not cleared-- resetting\n",
1291					      ha->host_no, __func__));
1292			qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
1293			if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1294				qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1295				status = qla4xxx_recover_adapter(ha);
1296			}
1297			clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1298			if (status == QLA_SUCCESS)
1299				ha->isp_ops->enable_intrs(ha);
1300		}
1301	}
1302
1303dpc_post_reset_ha:
1304	/* ---- process AEN? --- */
1305	if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1306		qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1307
1308	/* ---- Get DHCP IP Address? --- */
1309	if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1310		qla4xxx_get_dhcp_ip_address(ha);
1311
1312	/* ---- link change? --- */
1313	if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
1314		if (!test_bit(AF_LINK_UP, &ha->flags)) {
1315			/* ---- link down? --- */
1316			list_for_each_entry_safe(ddb_entry, dtemp,
1317						 &ha->ddb_list, list) {
1318				if (atomic_read(&ddb_entry->state) ==
1319						DDB_STATE_ONLINE)
1320					qla4xxx_mark_device_missing(ha,
1321							ddb_entry);
1322			}
1323		} else {
1324			/* ---- link up? --- *
1325			 * F/W will auto login to all devices ONLY ONCE after
1326			 * link up during driver initialization and runtime
1327			 * fatal error recovery.  Therefore, the driver must
1328			 * manually relogin to devices when recovering from
1329			 * connection failures, logouts, expired KATO, etc. */
1330
1331			list_for_each_entry_safe(ddb_entry, dtemp,
1332							&ha->ddb_list, list) {
1333				if ((atomic_read(&ddb_entry->state) ==
1334						 DDB_STATE_MISSING) ||
1335				    (atomic_read(&ddb_entry->state) ==
1336						 DDB_STATE_DEAD)) {
1337					if (ddb_entry->fw_ddb_device_state ==
1338					    DDB_DS_SESSION_ACTIVE) {
1339						atomic_set(&ddb_entry->state,
1340							   DDB_STATE_ONLINE);
1341						ql4_printk(KERN_INFO, ha,
1342						    "scsi%ld: %s: ddb[%d]"
1343						    " marked ONLINE\n",
1344						    ha->host_no, __func__,
1345						    ddb_entry->fw_ddb_index);
1346
1347						iscsi_unblock_session(
1348						    ddb_entry->sess);
1349					} else
1350						qla4xxx_relogin_device(
1351						    ha, ddb_entry);
1352				}
1353
1354			}
1355		}
1356	}
1357
1358	/* ---- relogin device? --- */
1359	if (adapter_up(ha) &&
1360	    test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1361		list_for_each_entry_safe(ddb_entry, dtemp,
1362					 &ha->ddb_list, list) {
1363			if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1364			    atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1365				qla4xxx_relogin_device(ha, ddb_entry);
1366
1367			/*
1368			 * If mbx cmd times out there is no point
1369			 * in continuing further.
1370			 * With large no of targets this can hang
1371			 * the system.
1372			 */
1373			if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1374				printk(KERN_WARNING "scsi%ld: %s: "
1375				       "need to reset hba\n",
1376				       ha->host_no, __func__);
1377				break;
1378			}
1379		}
1380	}
1381
1382do_dpc_exit:
1383	clear_bit(AF_DPC_SCHEDULED, &ha->flags);
1384}
1385
1386/**
1387 * qla4xxx_free_adapter - release the adapter
1388 * @ha: pointer to adapter structure
1389 **/
1390static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1391{
1392
1393	if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1394		/* Turn-off interrupts on the card. */
1395		ha->isp_ops->disable_intrs(ha);
1396	}
1397
1398	/* Remove timer thread, if present */
1399	if (ha->timer_active)
1400		qla4xxx_stop_timer(ha);
1401
1402	/* Kill the kernel thread for this host */
1403	if (ha->dpc_thread)
1404		destroy_workqueue(ha->dpc_thread);
1405
1406	/* Put firmware in known state */
1407	ha->isp_ops->reset_firmware(ha);
1408
1409	if (is_qla8022(ha)) {
1410		qla4_8xxx_idc_lock(ha);
1411		qla4_8xxx_clear_drv_active(ha);
1412		qla4_8xxx_idc_unlock(ha);
1413	}
1414
1415	/* Detach interrupts */
1416	if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1417		qla4xxx_free_irqs(ha);
1418
1419	/* free extra memory */
1420	qla4xxx_mem_free(ha);
1421}
1422
1423int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
1424{
1425	int status = 0;
1426	uint8_t revision_id;
1427	unsigned long mem_base, mem_len, db_base, db_len;
1428	struct pci_dev *pdev = ha->pdev;
1429
1430	status = pci_request_regions(pdev, DRIVER_NAME);
1431	if (status) {
1432		printk(KERN_WARNING
1433		    "scsi(%ld) Failed to reserve PIO regions (%s) "
1434		    "status=%d\n", ha->host_no, pci_name(pdev), status);
1435		goto iospace_error_exit;
1436	}
1437
1438	pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
1439	DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
1440	    __func__, revision_id));
1441	ha->revision_id = revision_id;
1442
1443	/* remap phys address */
1444	mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
1445	mem_len = pci_resource_len(pdev, 0);
1446	DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
1447	    __func__, mem_base, mem_len));
1448
1449	/* mapping of pcibase pointer */
1450	ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
1451	if (!ha->nx_pcibase) {
1452		printk(KERN_ERR
1453		    "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
1454		pci_release_regions(ha->pdev);
1455		goto iospace_error_exit;
1456	}
1457
1458	/* Mapping of IO base pointer, door bell read and write pointer */
1459
1460	/* mapping of IO base pointer */
1461	ha->qla4_8xxx_reg =
1462	    (struct device_reg_82xx  __iomem *)((uint8_t *)ha->nx_pcibase +
1463	    0xbc000 + (ha->pdev->devfn << 11));
1464
1465	db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
1466	db_len = pci_resource_len(pdev, 4);
1467
1468	/* mapping of doorbell write pointer */
1469	ha->nx_db_wr_ptr = (unsigned long)ioremap(db_base +
1470	    (ha->pdev->devfn << 12), 4);
1471	if (!ha->nx_db_wr_ptr) {
1472		printk(KERN_ERR
1473		    "cannot remap MMIO doorbell-write (%s), aborting\n",
1474		    pci_name(pdev));
1475		goto iospace_error_exit;
1476	}
1477	/* mapping of doorbell read pointer */
1478	ha->nx_db_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1479	    (ha->pdev->devfn * 8);
1480	if (!ha->nx_db_rd_ptr)
1481		printk(KERN_ERR
1482		    "cannot remap MMIO doorbell-read (%s), aborting\n",
1483		    pci_name(pdev));
1484	return 0;
1485
1486iospace_error_exit:
1487	return -ENOMEM;
1488}
1489
1490/***
1491 * qla4xxx_iospace_config - maps registers
1492 * @ha: pointer to adapter structure
1493 *
1494 * This routines maps HBA's registers from the pci address space
1495 * into the kernel virtual address space for memory mapped i/o.
1496 **/
1497int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1498{
1499	unsigned long pio, pio_len, pio_flags;
1500	unsigned long mmio, mmio_len, mmio_flags;
1501
1502	pio = pci_resource_start(ha->pdev, 0);
1503	pio_len = pci_resource_len(ha->pdev, 0);
1504	pio_flags = pci_resource_flags(ha->pdev, 0);
1505	if (pio_flags & IORESOURCE_IO) {
1506		if (pio_len < MIN_IOBASE_LEN) {
1507			ql4_printk(KERN_WARNING, ha,
1508				"Invalid PCI I/O region size\n");
1509			pio = 0;
1510		}
1511	} else {
1512		ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
1513		pio = 0;
1514	}
1515
1516	/* Use MMIO operations for all accesses. */
1517	mmio = pci_resource_start(ha->pdev, 1);
1518	mmio_len = pci_resource_len(ha->pdev, 1);
1519	mmio_flags = pci_resource_flags(ha->pdev, 1);
1520
1521	if (!(mmio_flags & IORESOURCE_MEM)) {
1522		ql4_printk(KERN_ERR, ha,
1523		    "region #0 not an MMIO resource, aborting\n");
1524
1525		goto iospace_error_exit;
1526	}
1527
1528	if (mmio_len < MIN_IOBASE_LEN) {
1529		ql4_printk(KERN_ERR, ha,
1530		    "Invalid PCI mem region size, aborting\n");
1531		goto iospace_error_exit;
1532	}
1533
1534	if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1535		ql4_printk(KERN_WARNING, ha,
1536		    "Failed to reserve PIO/MMIO regions\n");
1537
1538		goto iospace_error_exit;
1539	}
1540
1541	ha->pio_address = pio;
1542	ha->pio_length = pio_len;
1543	ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1544	if (!ha->reg) {
1545		ql4_printk(KERN_ERR, ha,
1546		    "cannot remap MMIO, aborting\n");
1547
1548		goto iospace_error_exit;
1549	}
1550
1551	return 0;
1552
1553iospace_error_exit:
1554	return -ENOMEM;
1555}
1556
1557static struct isp_operations qla4xxx_isp_ops = {
1558	.iospace_config         = qla4xxx_iospace_config,
1559	.pci_config             = qla4xxx_pci_config,
1560	.disable_intrs          = qla4xxx_disable_intrs,
1561	.enable_intrs           = qla4xxx_enable_intrs,
1562	.start_firmware         = qla4xxx_start_firmware,
1563	.intr_handler           = qla4xxx_intr_handler,
1564	.interrupt_service_routine = qla4xxx_interrupt_service_routine,
1565	.reset_chip             = qla4xxx_soft_reset,
1566	.reset_firmware         = qla4xxx_hw_reset,
1567	.queue_iocb             = qla4xxx_queue_iocb,
1568	.complete_iocb          = qla4xxx_complete_iocb,
1569	.rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
1570	.rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
1571	.get_sys_info           = qla4xxx_get_sys_info,
1572};
1573
1574static struct isp_operations qla4_8xxx_isp_ops = {
1575	.iospace_config         = qla4_8xxx_iospace_config,
1576	.pci_config             = qla4_8xxx_pci_config,
1577	.disable_intrs          = qla4_8xxx_disable_intrs,
1578	.enable_intrs           = qla4_8xxx_enable_intrs,
1579	.start_firmware         = qla4_8xxx_load_risc,
1580	.intr_handler           = qla4_8xxx_intr_handler,
1581	.interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
1582	.reset_chip             = qla4_8xxx_isp_reset,
1583	.reset_firmware         = qla4_8xxx_stop_firmware,
1584	.queue_iocb             = qla4_8xxx_queue_iocb,
1585	.complete_iocb          = qla4_8xxx_complete_iocb,
1586	.rd_shdw_req_q_out      = qla4_8xxx_rd_shdw_req_q_out,
1587	.rd_shdw_rsp_q_in       = qla4_8xxx_rd_shdw_rsp_q_in,
1588	.get_sys_info           = qla4_8xxx_get_sys_info,
1589};
1590
1591uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1592{
1593	return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
1594}
1595
1596uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
1597{
1598	return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
1599}
1600
1601uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1602{
1603	return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
1604}
1605
1606uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
1607{
1608	return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
1609}
1610
1611/**
1612 * qla4xxx_probe_adapter - callback function to probe HBA
1613 * @pdev: pointer to pci_dev structure
1614 * @pci_device_id: pointer to pci_device entry
1615 *
1616 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1617 * It returns zero if successful. It also initializes all data necessary for
1618 * the driver.
1619 **/
1620static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1621					   const struct pci_device_id *ent)
1622{
1623	int ret = -ENODEV, status;
1624	struct Scsi_Host *host;
1625	struct scsi_qla_host *ha;
1626	uint8_t init_retry_count = 0;
1627	char buf[34];
1628	struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
1629
1630	if (pci_enable_device(pdev))
1631		return -1;
1632
1633	host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1634	if (host == NULL) {
1635		printk(KERN_WARNING
1636		       "qla4xxx: Couldn't allocate host from scsi layer!\n");
1637		goto probe_disable_device;
1638	}
1639
1640	/* Clear our data area */
1641	ha = (struct scsi_qla_host *) host->hostdata;
1642	memset(ha, 0, sizeof(*ha));
1643
1644	/* Save the information from PCI BIOS.	*/
1645	ha->pdev = pdev;
1646	ha->host = host;
1647	ha->host_no = host->host_no;
1648
1649	pci_enable_pcie_error_reporting(pdev);
1650
1651	/* Setup Runtime configurable options */
1652	if (is_qla8022(ha)) {
1653		ha->isp_ops = &qla4_8xxx_isp_ops;
1654		rwlock_init(&ha->hw_lock);
1655		ha->qdr_sn_window = -1;
1656		ha->ddr_mn_window = -1;
1657		ha->curr_window = 255;
1658		ha->func_num = PCI_FUNC(ha->pdev->devfn);
1659		nx_legacy_intr = &legacy_intr[ha->func_num];
1660		ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
1661		ha->nx_legacy_intr.tgt_status_reg =
1662			nx_legacy_intr->tgt_status_reg;
1663		ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
1664		ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
1665	} else {
1666		ha->isp_ops = &qla4xxx_isp_ops;
1667	}
1668
1669	/* Set EEH reset type to fundamental if required by hba */
1670	if (is_qla8022(ha))
1671		pdev->needs_freset = 1;
1672
1673	/* Configure PCI I/O space. */
1674	ret = ha->isp_ops->iospace_config(ha);
1675	if (ret)
1676		goto probe_failed_ioconfig;
1677
1678	ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1679		   pdev->device, pdev->irq, ha->reg);
1680
1681	qla4xxx_config_dma_addressing(ha);
1682
1683	/* Initialize lists and spinlocks. */
1684	INIT_LIST_HEAD(&ha->ddb_list);
1685	INIT_LIST_HEAD(&ha->free_srb_q);
1686
1687	mutex_init(&ha->mbox_sem);
1688	init_completion(&ha->mbx_intr_comp);
1689
1690	spin_lock_init(&ha->hardware_lock);
1691
1692	/* Allocate dma buffers */
1693	if (qla4xxx_mem_alloc(ha)) {
1694		ql4_printk(KERN_WARNING, ha,
1695		    "[ERROR] Failed to allocate memory for adapter\n");
1696
1697		ret = -ENOMEM;
1698		goto probe_failed;
1699	}
1700
1701	if (is_qla8022(ha))
1702		(void) qla4_8xxx_get_flash_info(ha);
1703
1704	/*
1705	 * Initialize the Host adapter request/response queues and
1706	 * firmware
1707	 * NOTE: interrupts enabled upon successful completion
1708	 */
1709	status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1710	while ((!test_bit(AF_ONLINE, &ha->flags)) &&
1711	    init_retry_count++ < MAX_INIT_RETRIES) {
1712		DEBUG2(printk("scsi: %s: retrying adapter initialization "
1713			      "(%d)\n", __func__, init_retry_count));
1714
1715		if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
1716			continue;
1717
1718		status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1719	}
1720
1721	if (!test_bit(AF_ONLINE, &ha->flags)) {
1722		ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
1723
1724		ret = -ENODEV;
1725		goto probe_failed;
1726	}
1727
1728	host->cmd_per_lun = 3;
1729	host->max_channel = 0;
1730	host->max_lun = MAX_LUNS - 1;
1731	host->max_id = MAX_TARGETS;
1732	host->max_cmd_len = IOCB_MAX_CDB_LEN;
1733	host->can_queue = MAX_SRBS ;
1734	host->transportt = qla4xxx_scsi_transport;
1735
1736        ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1737        if (ret) {
1738		ql4_printk(KERN_WARNING, ha,
1739		    "scsi_init_shared_tag_map failed\n");
1740		goto probe_failed;
1741        }
1742
1743	/* Startup the kernel thread for this host adapter. */
1744	DEBUG2(printk("scsi: %s: Starting kernel thread for "
1745		      "qla4xxx_dpc\n", __func__));
1746	sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1747	ha->dpc_thread = create_singlethread_workqueue(buf);
1748	if (!ha->dpc_thread) {
1749		ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
1750		ret = -ENODEV;
1751		goto probe_failed;
1752	}
1753	INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1754
1755	/* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
1756	 * (which is called indirectly by qla4xxx_initialize_adapter),
1757	 * so that irqs will be registered after crbinit but before
1758	 * mbx_intr_enable.
1759	 */
1760	if (!is_qla8022(ha)) {
1761		ret = qla4xxx_request_irqs(ha);
1762		if (ret) {
1763			ql4_printk(KERN_WARNING, ha, "Failed to reserve "
1764			    "interrupt %d already in use.\n", pdev->irq);
1765			goto probe_failed;
1766		}
1767	}
1768
1769	pci_save_state(ha->pdev);
1770	ha->isp_ops->enable_intrs(ha);
1771
1772	/* Start timer thread. */
1773	qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1774
1775	set_bit(AF_INIT_DONE, &ha->flags);
1776
1777	pci_set_drvdata(pdev, ha);
1778
1779	ret = scsi_add_host(host, &pdev->dev);
1780	if (ret)
1781		goto probe_failed;
1782
1783	printk(KERN_INFO
1784	       " QLogic iSCSI HBA Driver version: %s\n"
1785	       "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1786	       qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1787	       ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1788	       ha->patch_number, ha->build_number);
1789	scsi_scan_host(host);
1790	return 0;
1791
1792probe_failed:
1793	qla4xxx_free_adapter(ha);
1794
1795probe_failed_ioconfig:
1796	pci_disable_pcie_error_reporting(pdev);
1797	scsi_host_put(ha->host);
1798
1799probe_disable_device:
1800	pci_disable_device(pdev);
1801
1802	return ret;
1803}
1804
1805/**
1806 * qla4xxx_remove_adapter - calback function to remove adapter.
1807 * @pci_dev: PCI device pointer
1808 **/
1809static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1810{
1811	struct scsi_qla_host *ha;
1812
1813	ha = pci_get_drvdata(pdev);
1814
1815	set_bit(AF_HBA_GOING_AWAY, &ha->flags);
1816
1817	/* remove devs from iscsi_sessions to scsi_devices */
1818	qla4xxx_free_ddb_list(ha);
1819
1820	scsi_remove_host(ha->host);
1821
1822	qla4xxx_free_adapter(ha);
1823
1824	scsi_host_put(ha->host);
1825
1826	pci_disable_pcie_error_reporting(pdev);
1827	pci_disable_device(pdev);
1828	pci_set_drvdata(pdev, NULL);
1829}
1830
1831/**
1832 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1833 * @ha: HA context
1834 *
1835 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1836 * supported addressing method.
1837 */
1838static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1839{
1840	int retval;
1841
1842	/* Update our PCI device dma_mask for full 64 bit mask */
1843	if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
1844		if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1845			dev_dbg(&ha->pdev->dev,
1846				  "Failed to set 64 bit PCI consistent mask; "
1847				   "using 32 bit.\n");
1848			retval = pci_set_consistent_dma_mask(ha->pdev,
1849							     DMA_BIT_MASK(32));
1850		}
1851	} else
1852		retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1853}
1854
1855static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1856{
1857	struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1858	struct ddb_entry *ddb = sess->dd_data;
1859
1860	sdev->hostdata = ddb;
1861	sdev->tagged_supported = 1;
1862	scsi_activate_tcq(sdev, QL4_DEF_QDEPTH);
1863	return 0;
1864}
1865
1866static int qla4xxx_slave_configure(struct scsi_device *sdev)
1867{
1868	sdev->tagged_supported = 1;
1869	return 0;
1870}
1871
1872static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1873{
1874	scsi_deactivate_tcq(sdev, 1);
1875}
1876
1877/**
1878 * qla4xxx_del_from_active_array - returns an active srb
1879 * @ha: Pointer to host adapter structure.
1880 * @index: index into the active_array
1881 *
1882 * This routine removes and returns the srb at the specified index
1883 **/
1884struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
1885    uint32_t index)
1886{
1887	struct srb *srb = NULL;
1888	struct scsi_cmnd *cmd = NULL;
1889
1890	cmd = scsi_host_find_tag(ha->host, index);
1891	if (!cmd)
1892		return srb;
1893
1894	srb = (struct srb *)CMD_SP(cmd);
1895	if (!srb)
1896		return srb;
1897
1898	/* update counters */
1899	if (srb->flags & SRB_DMA_VALID) {
1900		ha->req_q_count += srb->iocb_cnt;
1901		ha->iocb_cnt -= srb->iocb_cnt;
1902		if (srb->cmd)
1903			srb->cmd->host_scribble =
1904				(unsigned char *)(unsigned long) MAX_SRBS;
1905	}
1906	return srb;
1907}
1908
1909/**
1910 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1911 * @ha: Pointer to host adapter structure.
1912 * @cmd: Scsi Command to wait on.
1913 *
1914 * This routine waits for the command to be returned by the Firmware
1915 * for some max time.
1916 **/
1917static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1918				      struct scsi_cmnd *cmd)
1919{
1920	int done = 0;
1921	struct srb *rp;
1922	uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1923	int ret = SUCCESS;
1924
1925	/* Dont wait on command if PCI error is being handled
1926	 * by PCI AER driver
1927	 */
1928	if (unlikely(pci_channel_offline(ha->pdev)) ||
1929	    (test_bit(AF_EEH_BUSY, &ha->flags))) {
1930		ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
1931		    ha->host_no, __func__);
1932		return ret;
1933	}
1934
1935	do {
1936		/* Checking to see if its returned to OS */
1937		rp = (struct srb *) CMD_SP(cmd);
1938		if (rp == NULL) {
1939			done++;
1940			break;
1941		}
1942
1943		msleep(2000);
1944	} while (max_wait_time--);
1945
1946	return done;
1947}
1948
1949/**
1950 * qla4xxx_wait_for_hba_online - waits for HBA to come online
1951 * @ha: Pointer to host adapter structure
1952 **/
1953static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1954{
1955	unsigned long wait_online;
1956
1957	wait_online = jiffies + (30 * HZ);
1958	while (time_before(jiffies, wait_online)) {
1959
1960		if (adapter_up(ha))
1961			return QLA_SUCCESS;
1962		else if (ha->retry_reset_ha_cnt == 0)
1963			return QLA_ERROR;
1964
1965		msleep(2000);
1966	}
1967
1968	return QLA_ERROR;
1969}
1970
1971/**
1972 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
1973 * @ha: pointer to HBA
1974 * @t: target id
1975 * @l: lun id
1976 *
1977 * This function waits for all outstanding commands to a lun to complete. It
1978 * returns 0 if all pending commands are returned and 1 otherwise.
1979 **/
1980static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
1981					struct scsi_target *stgt,
1982					struct scsi_device *sdev)
1983{
1984	int cnt;
1985	int status = 0;
1986	struct scsi_cmnd *cmd;
1987
1988	/*
1989	 * Waiting for all commands for the designated target or dev
1990	 * in the active array
1991	 */
1992	for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1993		cmd = scsi_host_find_tag(ha->host, cnt);
1994		if (cmd && stgt == scsi_target(cmd->device) &&
1995		    (!sdev || sdev == cmd->device)) {
1996			if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1997				status++;
1998				break;
1999			}
2000		}
2001	}
2002	return status;
2003}
2004
2005/**
2006 * qla4xxx_eh_abort - callback for abort task.
2007 * @cmd: Pointer to Linux's SCSI command structure
2008 *
2009 * This routine is called by the Linux OS to abort the specified
2010 * command.
2011 **/
2012static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
2013{
2014	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2015	unsigned int id = cmd->device->id;
2016	unsigned int lun = cmd->device->lun;
2017	unsigned long serial = cmd->serial_number;
2018	struct srb *srb = NULL;
2019	int ret = SUCCESS;
2020	int wait = 0;
2021
2022	ql4_printk(KERN_INFO, ha,
2023	    "scsi%ld:%d:%d: Abort command issued cmd=%p, pid=%ld\n",
2024	    ha->host_no, id, lun, cmd, serial);
2025
2026	srb = (struct srb *) CMD_SP(cmd);
2027
2028	if (!srb)
2029		return SUCCESS;
2030
2031	kref_get(&srb->srb_ref);
2032
2033	if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
2034		DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
2035		    ha->host_no, id, lun));
2036		ret = FAILED;
2037	} else {
2038		DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
2039		    ha->host_no, id, lun));
2040		wait = 1;
2041	}
2042
2043	kref_put(&srb->srb_ref, qla4xxx_srb_compl);
2044
2045	/* Wait for command to complete */
2046	if (wait) {
2047		if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
2048			DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
2049			    ha->host_no, id, lun));
2050			ret = FAILED;
2051		}
2052	}
2053
2054	ql4_printk(KERN_INFO, ha,
2055	    "scsi%ld:%d:%d: Abort command - %s\n",
2056	    ha->host_no, id, lun, (ret == SUCCESS) ? "succeded" : "failed");
2057
2058	return ret;
2059}
2060
2061/**
2062 * qla4xxx_eh_device_reset - callback for target reset.
2063 * @cmd: Pointer to Linux's SCSI command structure
2064 *
2065 * This routine is called by the Linux OS to reset all luns on the
2066 * specified target.
2067 **/
2068static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
2069{
2070	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2071	struct ddb_entry *ddb_entry = cmd->device->hostdata;
2072	int ret = FAILED, stat;
2073
2074	if (!ddb_entry)
2075		return ret;
2076
2077	ret = iscsi_block_scsi_eh(cmd);
2078	if (ret)
2079		return ret;
2080	ret = FAILED;
2081
2082	ql4_printk(KERN_INFO, ha,
2083		   "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
2084		   cmd->device->channel, cmd->device->id, cmd->device->lun);
2085
2086	DEBUG2(printk(KERN_INFO
2087		      "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
2088		      "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
2089		      cmd, jiffies, cmd->request->timeout / HZ,
2090		      ha->dpc_flags, cmd->result, cmd->allowed));
2091
2092	stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
2093	if (stat != QLA_SUCCESS) {
2094		ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
2095		goto eh_dev_reset_done;
2096	}
2097
2098	if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2099					 cmd->device)) {
2100		ql4_printk(KERN_INFO, ha,
2101			   "DEVICE RESET FAILED - waiting for "
2102			   "commands.\n");
2103		goto eh_dev_reset_done;
2104	}
2105
2106	/* Send marker. */
2107	if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2108		MM_LUN_RESET) != QLA_SUCCESS)
2109		goto eh_dev_reset_done;
2110
2111	ql4_printk(KERN_INFO, ha,
2112		   "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
2113		   ha->host_no, cmd->device->channel, cmd->device->id,
2114		   cmd->device->lun);
2115
2116	ret = SUCCESS;
2117
2118eh_dev_reset_done:
2119
2120	return ret;
2121}
2122
2123/**
2124 * qla4xxx_eh_target_reset - callback for target reset.
2125 * @cmd: Pointer to Linux's SCSI command structure
2126 *
2127 * This routine is called by the Linux OS to reset the target.
2128 **/
2129static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
2130{
2131	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
2132	struct ddb_entry *ddb_entry = cmd->device->hostdata;
2133	int stat, ret;
2134
2135	if (!ddb_entry)
2136		return FAILED;
2137
2138	ret = iscsi_block_scsi_eh(cmd);
2139	if (ret)
2140		return ret;
2141
2142	starget_printk(KERN_INFO, scsi_target(cmd->device),
2143		       "WARM TARGET RESET ISSUED.\n");
2144
2145	DEBUG2(printk(KERN_INFO
2146		      "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
2147		      "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
2148		      ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
2149		      ha->dpc_flags, cmd->result, cmd->allowed));
2150
2151	stat = qla4xxx_reset_target(ha, ddb_entry);
2152	if (stat != QLA_SUCCESS) {
2153		starget_printk(KERN_INFO, scsi_target(cmd->device),
2154			       "WARM TARGET RESET FAILED.\n");
2155		return FAILED;
2156	}
2157
2158	if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
2159					 NULL)) {
2160		starget_printk(KERN_INFO, scsi_target(cmd->device),
2161			       "WARM TARGET DEVICE RESET FAILED - "
2162			       "waiting for commands.\n");
2163		return FAILED;
2164	}
2165
2166	/* Send marker. */
2167	if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
2168		MM_TGT_WARM_RESET) != QLA_SUCCESS) {
2169		starget_printk(KERN_INFO, scsi_target(cmd->device),
2170			       "WARM TARGET DEVICE RESET FAILED - "
2171			       "marker iocb failed.\n");
2172		return FAILED;
2173	}
2174
2175	starget_printk(KERN_INFO, scsi_target(cmd->device),
2176		       "WARM TARGET RESET SUCCEEDED.\n");
2177	return SUCCESS;
2178}
2179
2180/**
2181 * qla4xxx_eh_host_reset - kernel callback
2182 * @cmd: Pointer to Linux's SCSI command structure
2183 *
2184 * This routine is invoked by the Linux kernel to perform fatal error
2185 * recovery on the specified adapter.
2186 **/
2187static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
2188{
2189	int return_status = FAILED;
2190	struct scsi_qla_host *ha;
2191
2192	ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
2193
2194	if (ql4xdontresethba) {
2195		DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
2196		     ha->host_no, __func__));
2197		return FAILED;
2198	}
2199
2200	ql4_printk(KERN_INFO, ha,
2201		   "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
2202		   cmd->device->channel, cmd->device->id, cmd->device->lun);
2203
2204	if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
2205		DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
2206			      "DEAD.\n", ha->host_no, cmd->device->channel,
2207			      __func__));
2208
2209		return FAILED;
2210	}
2211
2212	if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2213		if (is_qla8022(ha))
2214			set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2215		else
2216			set_bit(DPC_RESET_HA, &ha->dpc_flags);
2217	}
2218
2219	if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
2220		return_status = SUCCESS;
2221
2222	ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
2223		   return_status == FAILED ? "FAILED" : "SUCCEDED");
2224
2225	return return_status;
2226}
2227
2228/* PCI AER driver recovers from all correctable errors w/o
2229 * driver intervention. For uncorrectable errors PCI AER
2230 * driver calls the following device driver's callbacks
2231 *
2232 * - Fatal Errors - link_reset
2233 * - Non-Fatal Errors - driver's pci_error_detected() which
2234 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
2235 *
2236 * PCI AER driver calls
2237 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
2238 *               returns RECOVERED or NEED_RESET if fw_hung
2239 * NEED_RESET - driver's slot_reset()
2240 * DISCONNECT - device is dead & cannot recover
2241 * RECOVERED - driver's pci_resume()
2242 */
2243static pci_ers_result_t
2244qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
2245{
2246	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2247
2248	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
2249	    ha->host_no, __func__, state);
2250
2251	if (!is_aer_supported(ha))
2252		return PCI_ERS_RESULT_NONE;
2253
2254	switch (state) {
2255	case pci_channel_io_normal:
2256		clear_bit(AF_EEH_BUSY, &ha->flags);
2257		return PCI_ERS_RESULT_CAN_RECOVER;
2258	case pci_channel_io_frozen:
2259		set_bit(AF_EEH_BUSY, &ha->flags);
2260		qla4xxx_mailbox_premature_completion(ha);
2261		qla4xxx_free_irqs(ha);
2262		pci_disable_device(pdev);
2263		return PCI_ERS_RESULT_NEED_RESET;
2264	case pci_channel_io_perm_failure:
2265		set_bit(AF_EEH_BUSY, &ha->flags);
2266		set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
2267		qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2268		return PCI_ERS_RESULT_DISCONNECT;
2269	}
2270	return PCI_ERS_RESULT_NEED_RESET;
2271}
2272
2273/**
2274 * qla4xxx_pci_mmio_enabled() gets called if
2275 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
2276 * and read/write to the device still works.
2277 **/
2278static pci_ers_result_t
2279qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
2280{
2281	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2282
2283	if (!is_aer_supported(ha))
2284		return PCI_ERS_RESULT_NONE;
2285
2286	if (test_bit(AF_FW_RECOVERY, &ha->flags)) {
2287		ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: firmware hang  -- "
2288		    "mmio_enabled\n", ha->host_no, __func__);
2289		return PCI_ERS_RESULT_NEED_RESET;
2290	} else
2291		return PCI_ERS_RESULT_RECOVERED;
2292}
2293
2294uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2295{
2296	uint32_t rval = QLA_ERROR;
2297	int fn;
2298	struct pci_dev *other_pdev = NULL;
2299
2300	ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
2301
2302	set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2303
2304	if (test_bit(AF_ONLINE, &ha->flags)) {
2305		clear_bit(AF_ONLINE, &ha->flags);
2306		qla4xxx_mark_all_devices_missing(ha);
2307		qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2308		qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2309	}
2310
2311	fn = PCI_FUNC(ha->pdev->devfn);
2312	while (fn > 0) {
2313		fn--;
2314		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
2315		    "func %x\n", ha->host_no, __func__, fn);
2316		/* Get the pci device given the domain, bus,
2317		 * slot/function number */
2318		other_pdev =
2319		    pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
2320		    ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
2321		    fn));
2322
2323		if (!other_pdev)
2324			continue;
2325
2326		if (atomic_read(&other_pdev->enable_cnt)) {
2327			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
2328			    "func in enabled state%x\n", ha->host_no,
2329			    __func__, fn);
2330			pci_dev_put(other_pdev);
2331			break;
2332		}
2333		pci_dev_put(other_pdev);
2334	}
2335
2336	/* The first function on the card, the reset owner will
2337	 * start & initialize the firmware. The other functions
2338	 * on the card will reset the firmware context
2339	 */
2340	if (!fn) {
2341		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
2342		    "0x%x is the owner\n", ha->host_no, __func__,
2343		    ha->pdev->devfn);
2344
2345		qla4_8xxx_idc_lock(ha);
2346		qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2347		    QLA82XX_DEV_COLD);
2348
2349		qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2350		    QLA82XX_IDC_VERSION);
2351
2352		qla4_8xxx_idc_unlock(ha);
2353		clear_bit(AF_FW_RECOVERY, &ha->flags);
2354		rval = qla4xxx_initialize_adapter(ha, PRESERVE_DDB_LIST);
2355		qla4_8xxx_idc_lock(ha);
2356
2357		if (rval != QLA_SUCCESS) {
2358			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2359			    "FAILED\n", ha->host_no, __func__);
2360			qla4_8xxx_clear_drv_active(ha);
2361			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2362			    QLA82XX_DEV_FAILED);
2363		} else {
2364			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
2365			    "READY\n", ha->host_no, __func__);
2366			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
2367			    QLA82XX_DEV_READY);
2368			/* Clear driver state register */
2369			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
2370			qla4_8xxx_set_drv_active(ha);
2371			ha->isp_ops->enable_intrs(ha);
2372		}
2373		qla4_8xxx_idc_unlock(ha);
2374	} else {
2375		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
2376		    "the reset owner\n", ha->host_no, __func__,
2377		    ha->pdev->devfn);
2378		if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
2379		    QLA82XX_DEV_READY)) {
2380			clear_bit(AF_FW_RECOVERY, &ha->flags);
2381			rval = qla4xxx_initialize_adapter(ha,
2382			    PRESERVE_DDB_LIST);
2383			if (rval == QLA_SUCCESS)
2384				ha->isp_ops->enable_intrs(ha);
2385			qla4_8xxx_idc_lock(ha);
2386			qla4_8xxx_set_drv_active(ha);
2387			qla4_8xxx_idc_unlock(ha);
2388		}
2389	}
2390	clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
2391	return rval;
2392}
2393
2394static pci_ers_result_t
2395qla4xxx_pci_slot_reset(struct pci_dev *pdev)
2396{
2397	pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
2398	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2399	int rc;
2400
2401	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
2402	    ha->host_no, __func__);
2403
2404	if (!is_aer_supported(ha))
2405		return PCI_ERS_RESULT_NONE;
2406
2407	/* Restore the saved state of PCIe device -
2408	 * BAR registers, PCI Config space, PCIX, MSI,
2409	 * IOV states
2410	 */
2411	pci_restore_state(pdev);
2412
2413	/* pci_restore_state() clears the saved_state flag of the device
2414	 * save restored state which resets saved_state flag
2415	 */
2416	pci_save_state(pdev);
2417
2418	/* Initialize device or resume if in suspended state */
2419	rc = pci_enable_device(pdev);
2420	if (rc) {
2421		ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Cant re-enable "
2422		    "device after reset\n", ha->host_no, __func__);
2423		goto exit_slot_reset;
2424	}
2425
2426	ret = qla4xxx_request_irqs(ha);
2427	if (ret) {
2428		ql4_printk(KERN_WARNING, ha, "Failed to reserve interrupt %d"
2429		    " already in use.\n", pdev->irq);
2430		goto exit_slot_reset;
2431	}
2432
2433	if (is_qla8022(ha)) {
2434		if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
2435			ret = PCI_ERS_RESULT_RECOVERED;
2436			goto exit_slot_reset;
2437		} else
2438			goto exit_slot_reset;
2439	}
2440
2441exit_slot_reset:
2442	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
2443	    "device after reset\n", ha->host_no, __func__, ret);
2444	return ret;
2445}
2446
2447static void
2448qla4xxx_pci_resume(struct pci_dev *pdev)
2449{
2450	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
2451	int ret;
2452
2453	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
2454	    ha->host_no, __func__);
2455
2456	ret = qla4xxx_wait_for_hba_online(ha);
2457	if (ret != QLA_SUCCESS) {
2458		ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
2459		    "resume I/O from slot/link_reset\n", ha->host_no,
2460		     __func__);
2461	}
2462
2463	pci_cleanup_aer_uncorrect_error_status(pdev);
2464	clear_bit(AF_EEH_BUSY, &ha->flags);
2465}
2466
2467static struct pci_error_handlers qla4xxx_err_handler = {
2468	.error_detected = qla4xxx_pci_error_detected,
2469	.mmio_enabled = qla4xxx_pci_mmio_enabled,
2470	.slot_reset = qla4xxx_pci_slot_reset,
2471	.resume = qla4xxx_pci_resume,
2472};
2473
2474static struct pci_device_id qla4xxx_pci_tbl[] = {
2475	{
2476		.vendor		= PCI_VENDOR_ID_QLOGIC,
2477		.device		= PCI_DEVICE_ID_QLOGIC_ISP4010,
2478		.subvendor	= PCI_ANY_ID,
2479		.subdevice	= PCI_ANY_ID,
2480	},
2481	{
2482		.vendor		= PCI_VENDOR_ID_QLOGIC,
2483		.device		= PCI_DEVICE_ID_QLOGIC_ISP4022,
2484		.subvendor	= PCI_ANY_ID,
2485		.subdevice	= PCI_ANY_ID,
2486	},
2487	{
2488		.vendor		= PCI_VENDOR_ID_QLOGIC,
2489		.device		= PCI_DEVICE_ID_QLOGIC_ISP4032,
2490		.subvendor	= PCI_ANY_ID,
2491		.subdevice	= PCI_ANY_ID,
2492	},
2493	{
2494		.vendor         = PCI_VENDOR_ID_QLOGIC,
2495		.device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
2496		.subvendor      = PCI_ANY_ID,
2497		.subdevice      = PCI_ANY_ID,
2498	},
2499	{0, 0},
2500};
2501MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
2502
2503static struct pci_driver qla4xxx_pci_driver = {
2504	.name		= DRIVER_NAME,
2505	.id_table	= qla4xxx_pci_tbl,
2506	.probe		= qla4xxx_probe_adapter,
2507	.remove		= qla4xxx_remove_adapter,
2508	.err_handler = &qla4xxx_err_handler,
2509};
2510
2511static int __init qla4xxx_module_init(void)
2512{
2513	int ret;
2514
2515	/* Allocate cache for SRBs. */
2516	srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
2517				       SLAB_HWCACHE_ALIGN, NULL);
2518	if (srb_cachep == NULL) {
2519		printk(KERN_ERR
2520		       "%s: Unable to allocate SRB cache..."
2521		       "Failing load!\n", DRIVER_NAME);
2522		ret = -ENOMEM;
2523		goto no_srp_cache;
2524	}
2525
2526	/* Derive version string. */
2527	strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
2528	if (ql4xextended_error_logging)
2529		strcat(qla4xxx_version_str, "-debug");
2530
2531	qla4xxx_scsi_transport =
2532		iscsi_register_transport(&qla4xxx_iscsi_transport);
2533	if (!qla4xxx_scsi_transport){
2534		ret = -ENODEV;
2535		goto release_srb_cache;
2536	}
2537
2538	ret = pci_register_driver(&qla4xxx_pci_driver);
2539	if (ret)
2540		goto unregister_transport;
2541
2542	printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
2543	return 0;
2544
2545unregister_transport:
2546	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2547release_srb_cache:
2548	kmem_cache_destroy(srb_cachep);
2549no_srp_cache:
2550	return ret;
2551}
2552
2553static void __exit qla4xxx_module_exit(void)
2554{
2555	pci_unregister_driver(&qla4xxx_pci_driver);
2556	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
2557	kmem_cache_destroy(srb_cachep);
2558}
2559
2560module_init(qla4xxx_module_init);
2561module_exit(qla4xxx_module_exit);
2562
2563MODULE_AUTHOR("QLogic Corporation");
2564MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
2565MODULE_LICENSE("GPL");
2566MODULE_VERSION(QLA4XXX_DRIVER_VERSION);
2567