xref: /openbmc/linux/drivers/scsi/qla4xxx/ql4_os.c (revision 64c70b1c)
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 
9 #include <scsi/scsi_tcq.h>
10 #include <scsi/scsicam.h>
11 
12 #include "ql4_def.h"
13 
14 /*
15  * Driver version
16  */
17 static char qla4xxx_version_str[40];
18 
19 /*
20  * SRB allocation cache
21  */
22 static struct kmem_cache *srb_cachep;
23 
24 /*
25  * Module parameter information and variables
26  */
27 int ql4xdiscoverywait = 60;
28 module_param(ql4xdiscoverywait, int, S_IRUGO | S_IRUSR);
29 MODULE_PARM_DESC(ql4xdiscoverywait, "Discovery wait time");
30 int ql4xdontresethba = 0;
31 module_param(ql4xdontresethba, int, S_IRUGO | S_IRUSR);
32 MODULE_PARM_DESC(ql4xdontresethba,
33 		 "Dont reset the HBA when the driver gets 0x8002 AEN "
34 		 " default it will reset hba :0"
35 		 " set to 1 to avoid resetting HBA");
36 
37 int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
38 module_param(ql4xextended_error_logging, int, S_IRUGO | S_IRUSR);
39 MODULE_PARM_DESC(ql4xextended_error_logging,
40 		 "Option to enable extended error logging, "
41 		 "Default is 0 - no logging, 1 - debug logging");
42 
43 int ql4_mod_unload = 0;
44 
45 /*
46  * SCSI host template entry points
47  */
48 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
49 
50 /*
51  * iSCSI template entry points
52  */
53 static int qla4xxx_tgt_dscvr(enum iscsi_tgt_dscvr type, uint32_t host_no,
54 			     uint32_t enable, struct sockaddr *dst_addr);
55 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
56 				  enum iscsi_param param, char *buf);
57 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
58 				  enum iscsi_param param, char *buf);
59 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag);
60 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
61 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session);
62 
63 /*
64  * SCSI host template entry points
65  */
66 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
67 				void (*done) (struct scsi_cmnd *));
68 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
69 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
70 static int qla4xxx_slave_alloc(struct scsi_device *device);
71 static int qla4xxx_slave_configure(struct scsi_device *device);
72 static void qla4xxx_slave_destroy(struct scsi_device *sdev);
73 
74 static struct scsi_host_template qla4xxx_driver_template = {
75 	.module			= THIS_MODULE,
76 	.name			= DRIVER_NAME,
77 	.proc_name		= DRIVER_NAME,
78 	.queuecommand		= qla4xxx_queuecommand,
79 
80 	.eh_device_reset_handler = qla4xxx_eh_device_reset,
81 	.eh_host_reset_handler	= qla4xxx_eh_host_reset,
82 
83 	.slave_configure	= qla4xxx_slave_configure,
84 	.slave_alloc		= qla4xxx_slave_alloc,
85 	.slave_destroy		= qla4xxx_slave_destroy,
86 
87 	.this_id		= -1,
88 	.cmd_per_lun		= 3,
89 	.use_clustering		= ENABLE_CLUSTERING,
90 	.sg_tablesize		= SG_ALL,
91 
92 	.max_sectors		= 0xFFFF,
93 };
94 
95 static struct iscsi_transport qla4xxx_iscsi_transport = {
96 	.owner			= THIS_MODULE,
97 	.name			= DRIVER_NAME,
98 	.param_mask		= ISCSI_CONN_PORT |
99 				  ISCSI_CONN_ADDRESS |
100 				  ISCSI_TARGET_NAME |
101 				  ISCSI_TPGT,
102 	.sessiondata_size	= sizeof(struct ddb_entry),
103 	.host_template		= &qla4xxx_driver_template,
104 
105 	.tgt_dscvr		= qla4xxx_tgt_dscvr,
106 	.get_conn_param		= qla4xxx_conn_get_param,
107 	.get_session_param	= qla4xxx_sess_get_param,
108 	.start_conn		= qla4xxx_conn_start,
109 	.stop_conn		= qla4xxx_conn_stop,
110 	.session_recovery_timedout = qla4xxx_recovery_timedout,
111 };
112 
113 static struct scsi_transport_template *qla4xxx_scsi_transport;
114 
115 static void qla4xxx_recovery_timedout(struct iscsi_cls_session *session)
116 {
117 	struct ddb_entry *ddb_entry = session->dd_data;
118 	struct scsi_qla_host *ha = ddb_entry->ha;
119 
120 	DEBUG2(printk("scsi%ld: %s: index [%d] port down retry count of (%d) "
121 		      "secs exhausted, marking device DEAD.\n", ha->host_no,
122 		      __func__, ddb_entry->fw_ddb_index,
123 		      ha->port_down_retry_count));
124 
125 	atomic_set(&ddb_entry->state, DDB_STATE_DEAD);
126 
127 	DEBUG2(printk("scsi%ld: %s: scheduling dpc routine - dpc flags = "
128 		      "0x%lx\n", ha->host_no, __func__, ha->dpc_flags));
129 	queue_work(ha->dpc_thread, &ha->dpc_work);
130 }
131 
132 static int qla4xxx_conn_start(struct iscsi_cls_conn *conn)
133 {
134 	struct iscsi_cls_session *session;
135 	struct ddb_entry *ddb_entry;
136 
137 	session = iscsi_dev_to_session(conn->dev.parent);
138 	ddb_entry = session->dd_data;
139 
140 	DEBUG2(printk("scsi%ld: %s: index [%d] starting conn\n",
141 		      ddb_entry->ha->host_no, __func__,
142 		      ddb_entry->fw_ddb_index));
143 	iscsi_unblock_session(session);
144 	return 0;
145 }
146 
147 static void qla4xxx_conn_stop(struct iscsi_cls_conn *conn, int flag)
148 {
149 	struct iscsi_cls_session *session;
150 	struct ddb_entry *ddb_entry;
151 
152 	session = iscsi_dev_to_session(conn->dev.parent);
153 	ddb_entry = session->dd_data;
154 
155 	DEBUG2(printk("scsi%ld: %s: index [%d] stopping conn\n",
156 		      ddb_entry->ha->host_no, __func__,
157 		      ddb_entry->fw_ddb_index));
158 	if (flag == STOP_CONN_RECOVER)
159 		iscsi_block_session(session);
160 	else
161 		printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
162 }
163 
164 static int qla4xxx_sess_get_param(struct iscsi_cls_session *sess,
165 				  enum iscsi_param param, char *buf)
166 {
167 	struct ddb_entry *ddb_entry = sess->dd_data;
168 	int len;
169 
170 	switch (param) {
171 	case ISCSI_PARAM_TARGET_NAME:
172 		len = snprintf(buf, PAGE_SIZE - 1, "%s\n",
173 			       ddb_entry->iscsi_name);
174 		break;
175 	case ISCSI_PARAM_TPGT:
176 		len = sprintf(buf, "%u\n", ddb_entry->tpgt);
177 		break;
178 	default:
179 		return -ENOSYS;
180 	}
181 
182 	return len;
183 }
184 
185 static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
186 				  enum iscsi_param param, char *buf)
187 {
188 	struct iscsi_cls_session *session;
189 	struct ddb_entry *ddb_entry;
190 	int len;
191 
192 	session = iscsi_dev_to_session(conn->dev.parent);
193 	ddb_entry = session->dd_data;
194 
195 	switch (param) {
196 	case ISCSI_PARAM_CONN_PORT:
197 		len = sprintf(buf, "%hu\n", ddb_entry->port);
198 		break;
199 	case ISCSI_PARAM_CONN_ADDRESS:
200 		/* TODO: what are the ipv6 bits */
201 		len = sprintf(buf, "%u.%u.%u.%u\n",
202 			      NIPQUAD(ddb_entry->ip_addr));
203 		break;
204 	default:
205 		return -ENOSYS;
206 	}
207 
208 	return len;
209 }
210 
211 static int qla4xxx_tgt_dscvr(enum iscsi_tgt_dscvr type, uint32_t host_no,
212 			     uint32_t enable, struct sockaddr *dst_addr)
213 {
214 	struct scsi_qla_host *ha;
215 	struct Scsi_Host *shost;
216 	struct sockaddr_in *addr;
217 	struct sockaddr_in6 *addr6;
218 	int ret = 0;
219 
220 	shost = scsi_host_lookup(host_no);
221 	if (IS_ERR(shost)) {
222 		printk(KERN_ERR "Could not find host no %u\n", host_no);
223 		return -ENODEV;
224 	}
225 
226 	ha = (struct scsi_qla_host *) shost->hostdata;
227 
228 	switch (type) {
229 	case ISCSI_TGT_DSCVR_SEND_TARGETS:
230 		if (dst_addr->sa_family == AF_INET) {
231 			addr = (struct sockaddr_in *)dst_addr;
232 			if (qla4xxx_send_tgts(ha, (char *)&addr->sin_addr,
233 					      addr->sin_port) != QLA_SUCCESS)
234 				ret = -EIO;
235 		} else if (dst_addr->sa_family == AF_INET6) {
236 			/*
237 			 * TODO: fix qla4xxx_send_tgts
238 			 */
239 			addr6 = (struct sockaddr_in6 *)dst_addr;
240 			if (qla4xxx_send_tgts(ha, (char *)&addr6->sin6_addr,
241 					      addr6->sin6_port) != QLA_SUCCESS)
242 				ret = -EIO;
243 		} else
244 			ret = -ENOSYS;
245 		break;
246 	default:
247 		ret = -ENOSYS;
248 	}
249 
250 	scsi_host_put(shost);
251 	return ret;
252 }
253 
254 void qla4xxx_destroy_sess(struct ddb_entry *ddb_entry)
255 {
256 	if (!ddb_entry->sess)
257 		return;
258 
259 	if (ddb_entry->conn) {
260 		iscsi_if_destroy_session_done(ddb_entry->conn);
261 		iscsi_destroy_conn(ddb_entry->conn);
262 		iscsi_remove_session(ddb_entry->sess);
263 	}
264 	iscsi_free_session(ddb_entry->sess);
265 }
266 
267 int qla4xxx_add_sess(struct ddb_entry *ddb_entry)
268 {
269 	int err;
270 
271 	err = iscsi_add_session(ddb_entry->sess, ddb_entry->fw_ddb_index);
272 	if (err) {
273 		DEBUG2(printk(KERN_ERR "Could not add session.\n"));
274 		return err;
275 	}
276 
277 	ddb_entry->conn = iscsi_create_conn(ddb_entry->sess, 0);
278 	if (!ddb_entry->conn) {
279 		iscsi_remove_session(ddb_entry->sess);
280 		DEBUG2(printk(KERN_ERR "Could not add connection.\n"));
281 		return -ENOMEM;
282 	}
283 
284 	ddb_entry->sess->recovery_tmo = ddb_entry->ha->port_down_retry_count;
285 	iscsi_if_create_session_done(ddb_entry->conn);
286 	return 0;
287 }
288 
289 struct ddb_entry *qla4xxx_alloc_sess(struct scsi_qla_host *ha)
290 {
291 	struct ddb_entry *ddb_entry;
292 	struct iscsi_cls_session *sess;
293 
294 	sess = iscsi_alloc_session(ha->host, &qla4xxx_iscsi_transport);
295 	if (!sess)
296 		return NULL;
297 
298 	ddb_entry = sess->dd_data;
299 	memset(ddb_entry, 0, sizeof(*ddb_entry));
300 	ddb_entry->ha = ha;
301 	ddb_entry->sess = sess;
302 	return ddb_entry;
303 }
304 
305 /*
306  * Timer routines
307  */
308 
309 static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
310 				unsigned long interval)
311 {
312 	DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
313 		     __func__, ha->host->host_no));
314 	init_timer(&ha->timer);
315 	ha->timer.expires = jiffies + interval * HZ;
316 	ha->timer.data = (unsigned long)ha;
317 	ha->timer.function = (void (*)(unsigned long))func;
318 	add_timer(&ha->timer);
319 	ha->timer_active = 1;
320 }
321 
322 static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
323 {
324 	del_timer_sync(&ha->timer);
325 	ha->timer_active = 0;
326 }
327 
328 /***
329  * qla4xxx_mark_device_missing - mark a device as missing.
330  * @ha: Pointer to host adapter structure.
331  * @ddb_entry: Pointer to device database entry
332  *
333  * This routine marks a device missing and resets the relogin retry count.
334  **/
335 void qla4xxx_mark_device_missing(struct scsi_qla_host *ha,
336 				 struct ddb_entry *ddb_entry)
337 {
338 	atomic_set(&ddb_entry->state, DDB_STATE_MISSING);
339 	DEBUG3(printk("scsi%d:%d:%d: index [%d] marked MISSING\n",
340 		      ha->host_no, ddb_entry->bus, ddb_entry->target,
341 		      ddb_entry->fw_ddb_index));
342 	iscsi_conn_error(ddb_entry->conn, ISCSI_ERR_CONN_FAILED);
343 }
344 
345 static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
346 				       struct ddb_entry *ddb_entry,
347 				       struct scsi_cmnd *cmd,
348 				       void (*done)(struct scsi_cmnd *))
349 {
350 	struct srb *srb;
351 
352 	srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
353 	if (!srb)
354 		return srb;
355 
356 	atomic_set(&srb->ref_count, 1);
357 	srb->ha = ha;
358 	srb->ddb = ddb_entry;
359 	srb->cmd = cmd;
360 	srb->flags = 0;
361 	cmd->SCp.ptr = (void *)srb;
362 	cmd->scsi_done = done;
363 
364 	return srb;
365 }
366 
367 static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
368 {
369 	struct scsi_cmnd *cmd = srb->cmd;
370 
371 	if (srb->flags & SRB_DMA_VALID) {
372 		if (cmd->use_sg) {
373 			pci_unmap_sg(ha->pdev, cmd->request_buffer,
374 				     cmd->use_sg, cmd->sc_data_direction);
375 		} else if (cmd->request_bufflen) {
376 			pci_unmap_single(ha->pdev, srb->dma_handle,
377 					 cmd->request_bufflen,
378 					 cmd->sc_data_direction);
379 		}
380 		srb->flags &= ~SRB_DMA_VALID;
381 	}
382 	cmd->SCp.ptr = NULL;
383 }
384 
385 void qla4xxx_srb_compl(struct scsi_qla_host *ha, struct srb *srb)
386 {
387 	struct scsi_cmnd *cmd = srb->cmd;
388 
389 	qla4xxx_srb_free_dma(ha, srb);
390 
391 	mempool_free(srb, ha->srb_mempool);
392 
393 	cmd->scsi_done(cmd);
394 }
395 
396 /**
397  * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
398  * @cmd: Pointer to Linux's SCSI command structure
399  * @done_fn: Function that the driver calls to notify the SCSI mid-layer
400  *	that the command has been processed.
401  *
402  * Remarks:
403  * This routine is invoked by Linux to send a SCSI command to the driver.
404  * The mid-level driver tries to ensure that queuecommand never gets
405  * invoked concurrently with itself or the interrupt handler (although
406  * the interrupt handler may call this routine as part of request-
407  * completion handling).   Unfortunely, it sometimes calls the scheduler
408  * in interrupt context which is a big NO! NO!.
409  **/
410 static int qla4xxx_queuecommand(struct scsi_cmnd *cmd,
411 				void (*done)(struct scsi_cmnd *))
412 {
413 	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
414 	struct ddb_entry *ddb_entry = cmd->device->hostdata;
415 	struct srb *srb;
416 	int rval;
417 
418 	if (atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
419 		if (atomic_read(&ddb_entry->state) == DDB_STATE_DEAD) {
420 			cmd->result = DID_NO_CONNECT << 16;
421 			goto qc_fail_command;
422 		}
423 		goto qc_host_busy;
424 	}
425 
426 	if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags))
427 		goto qc_host_busy;
428 
429 	spin_unlock_irq(ha->host->host_lock);
430 
431 	srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd, done);
432 	if (!srb)
433 		goto qc_host_busy_lock;
434 
435 	rval = qla4xxx_send_command_to_isp(ha, srb);
436 	if (rval != QLA_SUCCESS)
437 		goto qc_host_busy_free_sp;
438 
439 	spin_lock_irq(ha->host->host_lock);
440 	return 0;
441 
442 qc_host_busy_free_sp:
443 	qla4xxx_srb_free_dma(ha, srb);
444 	mempool_free(srb, ha->srb_mempool);
445 
446 qc_host_busy_lock:
447 	spin_lock_irq(ha->host->host_lock);
448 
449 qc_host_busy:
450 	return SCSI_MLQUEUE_HOST_BUSY;
451 
452 qc_fail_command:
453 	done(cmd);
454 
455 	return 0;
456 }
457 
458 /**
459  * qla4xxx_mem_free - frees memory allocated to adapter
460  * @ha: Pointer to host adapter structure.
461  *
462  * Frees memory previously allocated by qla4xxx_mem_alloc
463  **/
464 static void qla4xxx_mem_free(struct scsi_qla_host *ha)
465 {
466 	if (ha->queues)
467 		dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
468 				  ha->queues_dma);
469 
470 	ha->queues_len = 0;
471 	ha->queues = NULL;
472 	ha->queues_dma = 0;
473 	ha->request_ring = NULL;
474 	ha->request_dma = 0;
475 	ha->response_ring = NULL;
476 	ha->response_dma = 0;
477 	ha->shadow_regs = NULL;
478 	ha->shadow_regs_dma = 0;
479 
480 	/* Free srb pool. */
481 	if (ha->srb_mempool)
482 		mempool_destroy(ha->srb_mempool);
483 
484 	ha->srb_mempool = NULL;
485 
486 	/* release io space registers  */
487 	if (ha->reg)
488 		iounmap(ha->reg);
489 	pci_release_regions(ha->pdev);
490 }
491 
492 /**
493  * qla4xxx_mem_alloc - allocates memory for use by adapter.
494  * @ha: Pointer to host adapter structure
495  *
496  * Allocates DMA memory for request and response queues. Also allocates memory
497  * for srbs.
498  **/
499 static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
500 {
501 	unsigned long align;
502 
503 	/* Allocate contiguous block of DMA memory for queues. */
504 	ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
505 			  (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
506 			  sizeof(struct shadow_regs) +
507 			  MEM_ALIGN_VALUE +
508 			  (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
509 	ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
510 					&ha->queues_dma, GFP_KERNEL);
511 	if (ha->queues == NULL) {
512 		dev_warn(&ha->pdev->dev,
513 			"Memory Allocation failed - queues.\n");
514 
515 		goto mem_alloc_error_exit;
516 	}
517 	memset(ha->queues, 0, ha->queues_len);
518 
519 	/*
520 	 * As per RISC alignment requirements -- the bus-address must be a
521 	 * multiple of the request-ring size (in bytes).
522 	 */
523 	align = 0;
524 	if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
525 		align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
526 					   (MEM_ALIGN_VALUE - 1));
527 
528 	/* Update request and response queue pointers. */
529 	ha->request_dma = ha->queues_dma + align;
530 	ha->request_ring = (struct queue_entry *) (ha->queues + align);
531 	ha->response_dma = ha->queues_dma + align +
532 		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
533 	ha->response_ring = (struct queue_entry *) (ha->queues + align +
534 						    (REQUEST_QUEUE_DEPTH *
535 						     QUEUE_SIZE));
536 	ha->shadow_regs_dma = ha->queues_dma + align +
537 		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
538 		(RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
539 	ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
540 						  (REQUEST_QUEUE_DEPTH *
541 						   QUEUE_SIZE) +
542 						  (RESPONSE_QUEUE_DEPTH *
543 						   QUEUE_SIZE));
544 
545 	/* Allocate memory for srb pool. */
546 	ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
547 					 mempool_free_slab, srb_cachep);
548 	if (ha->srb_mempool == NULL) {
549 		dev_warn(&ha->pdev->dev,
550 			"Memory Allocation failed - SRB Pool.\n");
551 
552 		goto mem_alloc_error_exit;
553 	}
554 
555 	return QLA_SUCCESS;
556 
557 mem_alloc_error_exit:
558 	qla4xxx_mem_free(ha);
559 	return QLA_ERROR;
560 }
561 
562 /**
563  * qla4xxx_timer - checks every second for work to do.
564  * @ha: Pointer to host adapter structure.
565  **/
566 static void qla4xxx_timer(struct scsi_qla_host *ha)
567 {
568 	struct ddb_entry *ddb_entry, *dtemp;
569 	int start_dpc = 0;
570 
571 	/* Search for relogin's to time-out and port down retry. */
572 	list_for_each_entry_safe(ddb_entry, dtemp, &ha->ddb_list, list) {
573 		/* Count down time between sending relogins */
574 		if (adapter_up(ha) &&
575 		    !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
576 		    atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE) {
577 			if (atomic_read(&ddb_entry->retry_relogin_timer) !=
578 			    INVALID_ENTRY) {
579 				if (atomic_read(&ddb_entry->retry_relogin_timer)
580 				    		== 0) {
581 					atomic_set(&ddb_entry->
582 						retry_relogin_timer,
583 						INVALID_ENTRY);
584 					set_bit(DPC_RELOGIN_DEVICE,
585 						&ha->dpc_flags);
586 					set_bit(DF_RELOGIN, &ddb_entry->flags);
587 					DEBUG2(printk("scsi%ld: %s: index [%d]"
588 						      " login device\n",
589 						      ha->host_no, __func__,
590 						      ddb_entry->fw_ddb_index));
591 				} else
592 					atomic_dec(&ddb_entry->
593 							retry_relogin_timer);
594 			}
595 		}
596 
597 		/* Wait for relogin to timeout */
598 		if (atomic_read(&ddb_entry->relogin_timer) &&
599 		    (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
600 			/*
601 			 * If the relogin times out and the device is
602 			 * still NOT ONLINE then try and relogin again.
603 			 */
604 			if (atomic_read(&ddb_entry->state) !=
605 			    DDB_STATE_ONLINE &&
606 			    ddb_entry->fw_ddb_device_state ==
607 			    DDB_DS_SESSION_FAILED) {
608 				/* Reset retry relogin timer */
609 				atomic_inc(&ddb_entry->relogin_retry_count);
610 				DEBUG2(printk("scsi%ld: index[%d] relogin"
611 					      " timed out-retrying"
612 					      " relogin (%d)\n",
613 					      ha->host_no,
614 					      ddb_entry->fw_ddb_index,
615 					      atomic_read(&ddb_entry->
616 							  relogin_retry_count))
617 					);
618 				start_dpc++;
619 				DEBUG(printk("scsi%ld:%d:%d: index [%d] "
620 					     "initate relogin after"
621 					     " %d seconds\n",
622 					     ha->host_no, ddb_entry->bus,
623 					     ddb_entry->target,
624 					     ddb_entry->fw_ddb_index,
625 					     ddb_entry->default_time2wait + 4)
626 					);
627 
628 				atomic_set(&ddb_entry->retry_relogin_timer,
629 					   ddb_entry->default_time2wait + 4);
630 			}
631 		}
632 	}
633 
634 	/* Check for heartbeat interval. */
635 	if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
636 	    ha->heartbeat_interval != 0) {
637 		ha->seconds_since_last_heartbeat++;
638 		if (ha->seconds_since_last_heartbeat >
639 		    ha->heartbeat_interval + 2)
640 			set_bit(DPC_RESET_HA, &ha->dpc_flags);
641 	}
642 
643 
644 	/* Wakeup the dpc routine for this adapter, if needed. */
645 	if ((start_dpc ||
646 	     test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
647 	     test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
648 	     test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
649 	     test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
650 	     test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
651 	     test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
652 	     test_bit(DPC_AEN, &ha->dpc_flags)) &&
653 	     ha->dpc_thread) {
654 		DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
655 			      " - dpc flags = 0x%lx\n",
656 			      ha->host_no, __func__, ha->dpc_flags));
657 		queue_work(ha->dpc_thread, &ha->dpc_work);
658 	}
659 
660 	/* Reschedule timer thread to call us back in one second */
661 	mod_timer(&ha->timer, jiffies + HZ);
662 
663 	DEBUG2(ha->seconds_since_last_intr++);
664 }
665 
666 /**
667  * qla4xxx_cmd_wait - waits for all outstanding commands to complete
668  * @ha: Pointer to host adapter structure.
669  *
670  * This routine stalls the driver until all outstanding commands are returned.
671  * Caller must release the Hardware Lock prior to calling this routine.
672  **/
673 static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
674 {
675 	uint32_t index = 0;
676 	int stat = QLA_SUCCESS;
677 	unsigned long flags;
678 	struct scsi_cmnd *cmd;
679 	int wait_cnt = WAIT_CMD_TOV;	/*
680 					 * Initialized for 30 seconds as we
681 					 * expect all commands to retuned
682 					 * ASAP.
683 					 */
684 
685 	while (wait_cnt) {
686 		spin_lock_irqsave(&ha->hardware_lock, flags);
687 		/* Find a command that hasn't completed. */
688 		for (index = 0; index < ha->host->can_queue; index++) {
689 			cmd = scsi_host_find_tag(ha->host, index);
690 			if (cmd != NULL)
691 				break;
692 		}
693 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
694 
695 		/* If No Commands are pending, wait is complete */
696 		if (index == ha->host->can_queue) {
697 			break;
698 		}
699 
700 		/* If we timed out on waiting for commands to come back
701 		 * return ERROR.
702 		 */
703 		wait_cnt--;
704 		if (wait_cnt == 0)
705 			stat = QLA_ERROR;
706 		else {
707 			msleep(1000);
708 		}
709 	}			/* End of While (wait_cnt) */
710 
711 	return stat;
712 }
713 
714 static void qla4xxx_hw_reset(struct scsi_qla_host *ha)
715 {
716 	uint32_t ctrl_status;
717 	unsigned long flags = 0;
718 
719 	DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
720 
721 	spin_lock_irqsave(&ha->hardware_lock, flags);
722 
723 	/*
724 	 * If the SCSI Reset Interrupt bit is set, clear it.
725 	 * Otherwise, the Soft Reset won't work.
726 	 */
727 	ctrl_status = readw(&ha->reg->ctrl_status);
728 	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
729 		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
730 
731 	/* Issue Soft Reset */
732 	writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
733 	readl(&ha->reg->ctrl_status);
734 
735 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
736 }
737 
738 /**
739  * qla4xxx_soft_reset - performs soft reset.
740  * @ha: Pointer to host adapter structure.
741  **/
742 int qla4xxx_soft_reset(struct scsi_qla_host *ha)
743 {
744 	uint32_t max_wait_time;
745 	unsigned long flags = 0;
746 	int status = QLA_ERROR;
747 	uint32_t ctrl_status;
748 
749 	qla4xxx_hw_reset(ha);
750 
751 	/* Wait until the Network Reset Intr bit is cleared */
752 	max_wait_time = RESET_INTR_TOV;
753 	do {
754 		spin_lock_irqsave(&ha->hardware_lock, flags);
755 		ctrl_status = readw(&ha->reg->ctrl_status);
756 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
757 
758 		if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
759 			break;
760 
761 		msleep(1000);
762 	} while ((--max_wait_time));
763 
764 	if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
765 		DEBUG2(printk(KERN_WARNING
766 			      "scsi%ld: Network Reset Intr not cleared by "
767 			      "Network function, clearing it now!\n",
768 			      ha->host_no));
769 		spin_lock_irqsave(&ha->hardware_lock, flags);
770 		writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
771 		readl(&ha->reg->ctrl_status);
772 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
773 	}
774 
775 	/* Wait until the firmware tells us the Soft Reset is done */
776 	max_wait_time = SOFT_RESET_TOV;
777 	do {
778 		spin_lock_irqsave(&ha->hardware_lock, flags);
779 		ctrl_status = readw(&ha->reg->ctrl_status);
780 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
781 
782 		if ((ctrl_status & CSR_SOFT_RESET) == 0) {
783 			status = QLA_SUCCESS;
784 			break;
785 		}
786 
787 		msleep(1000);
788 	} while ((--max_wait_time));
789 
790 	/*
791 	 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
792 	 * after the soft reset has taken place.
793 	 */
794 	spin_lock_irqsave(&ha->hardware_lock, flags);
795 	ctrl_status = readw(&ha->reg->ctrl_status);
796 	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
797 		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
798 		readl(&ha->reg->ctrl_status);
799 	}
800 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
801 
802 	/* If soft reset fails then most probably the bios on other
803 	 * function is also enabled.
804 	 * Since the initialization is sequential the other fn
805 	 * wont be able to acknowledge the soft reset.
806 	 * Issue a force soft reset to workaround this scenario.
807 	 */
808 	if (max_wait_time == 0) {
809 		/* Issue Force Soft Reset */
810 		spin_lock_irqsave(&ha->hardware_lock, flags);
811 		writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
812 		readl(&ha->reg->ctrl_status);
813 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
814 		/* Wait until the firmware tells us the Soft Reset is done */
815 		max_wait_time = SOFT_RESET_TOV;
816 		do {
817 			spin_lock_irqsave(&ha->hardware_lock, flags);
818 			ctrl_status = readw(&ha->reg->ctrl_status);
819 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
820 
821 			if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
822 				status = QLA_SUCCESS;
823 				break;
824 			}
825 
826 			msleep(1000);
827 		} while ((--max_wait_time));
828 	}
829 
830 	return status;
831 }
832 
833 /**
834  * qla4xxx_flush_active_srbs - returns all outstanding i/o requests to O.S.
835  * @ha: Pointer to host adapter structure.
836  *
837  * This routine is called just prior to a HARD RESET to return all
838  * outstanding commands back to the Operating System.
839  * Caller should make sure that the following locks are released
840  * before this calling routine: Hardware lock, and io_request_lock.
841  **/
842 static void qla4xxx_flush_active_srbs(struct scsi_qla_host *ha)
843 {
844 	struct srb *srb;
845 	int i;
846 	unsigned long flags;
847 
848 	spin_lock_irqsave(&ha->hardware_lock, flags);
849 	for (i = 0; i < ha->host->can_queue; i++) {
850 		srb = qla4xxx_del_from_active_array(ha, i);
851 		if (srb != NULL) {
852 			srb->cmd->result = DID_RESET << 16;
853 			qla4xxx_srb_compl(ha, srb);
854 		}
855 	}
856 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
857 
858 }
859 
860 /**
861  * qla4xxx_recover_adapter - recovers adapter after a fatal error
862  * @ha: Pointer to host adapter structure.
863  * @renew_ddb_list: Indicates what to do with the adapter's ddb list
864  *	after adapter recovery has completed.
865  *	0=preserve ddb list, 1=destroy and rebuild ddb list
866  **/
867 static int qla4xxx_recover_adapter(struct scsi_qla_host *ha,
868 				uint8_t renew_ddb_list)
869 {
870 	int status;
871 
872 	/* Stall incoming I/O until we are done */
873 	clear_bit(AF_ONLINE, &ha->flags);
874 	DEBUG2(printk("scsi%ld: %s calling qla4xxx_cmd_wait\n", ha->host_no,
875 		      __func__));
876 
877 	/* Wait for outstanding commands to complete.
878 	 * Stalls the driver for max 30 secs
879 	 */
880 	status = qla4xxx_cmd_wait(ha);
881 
882 	qla4xxx_disable_intrs(ha);
883 
884 	/* Flush any pending ddb changed AENs */
885 	qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
886 
887 	/* Reset the firmware.	If successful, function
888 	 * returns with ISP interrupts enabled.
889 	 */
890 	if (status == QLA_SUCCESS) {
891 		DEBUG2(printk("scsi%ld: %s - Performing soft reset..\n",
892 			      ha->host_no, __func__));
893 		qla4xxx_flush_active_srbs(ha);
894 		if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
895 			status = qla4xxx_soft_reset(ha);
896 		else
897 			status = QLA_ERROR;
898 	}
899 
900 	/* Flush any pending ddb changed AENs */
901 	qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
902 
903 	/* Re-initialize firmware. If successful, function returns
904 	 * with ISP interrupts enabled */
905 	if (status == QLA_SUCCESS) {
906 		DEBUG2(printk("scsi%ld: %s - Initializing adapter..\n",
907 			      ha->host_no, __func__));
908 
909 		/* If successful, AF_ONLINE flag set in
910 		 * qla4xxx_initialize_adapter */
911 		status = qla4xxx_initialize_adapter(ha, renew_ddb_list);
912 	}
913 
914 	/* Failed adapter initialization?
915 	 * Retry reset_ha only if invoked via DPC (DPC_RESET_HA) */
916 	if ((test_bit(AF_ONLINE, &ha->flags) == 0) &&
917 	    (test_bit(DPC_RESET_HA, &ha->dpc_flags))) {
918 		/* Adapter initialization failed, see if we can retry
919 		 * resetting the ha */
920 		if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
921 			ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
922 			DEBUG2(printk("scsi%ld: recover adapter - retrying "
923 				      "(%d) more times\n", ha->host_no,
924 				      ha->retry_reset_ha_cnt));
925 			set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
926 			status = QLA_ERROR;
927 		} else {
928 			if (ha->retry_reset_ha_cnt > 0) {
929 				/* Schedule another Reset HA--DPC will retry */
930 				ha->retry_reset_ha_cnt--;
931 				DEBUG2(printk("scsi%ld: recover adapter - "
932 					      "retry remaining %d\n",
933 					      ha->host_no,
934 					      ha->retry_reset_ha_cnt));
935 				status = QLA_ERROR;
936 			}
937 
938 			if (ha->retry_reset_ha_cnt == 0) {
939 				/* Recover adapter retries have been exhausted.
940 				 * Adapter DEAD */
941 				DEBUG2(printk("scsi%ld: recover adapter "
942 					      "failed - board disabled\n",
943 					      ha->host_no));
944 				qla4xxx_flush_active_srbs(ha);
945 				clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
946 				clear_bit(DPC_RESET_HA, &ha->dpc_flags);
947 				clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST,
948 					  &ha->dpc_flags);
949 				status = QLA_ERROR;
950 			}
951 		}
952 	} else {
953 		clear_bit(DPC_RESET_HA, &ha->dpc_flags);
954 		clear_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags);
955 		clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
956 	}
957 
958 	ha->adapter_error_count++;
959 
960 	if (status == QLA_SUCCESS)
961 		qla4xxx_enable_intrs(ha);
962 
963 	DEBUG2(printk("scsi%ld: recover adapter .. DONE\n", ha->host_no));
964 	return status;
965 }
966 
967 /**
968  * qla4xxx_do_dpc - dpc routine
969  * @data: in our case pointer to adapter structure
970  *
971  * This routine is a task that is schedule by the interrupt handler
972  * to perform the background processing for interrupts.  We put it
973  * on a task queue that is consumed whenever the scheduler runs; that's
974  * so you can do anything (i.e. put the process to sleep etc).  In fact,
975  * the mid-level tries to sleep when it reaches the driver threshold
976  * "host->can_queue". This can cause a panic if we were in our interrupt code.
977  **/
978 static void qla4xxx_do_dpc(struct work_struct *work)
979 {
980 	struct scsi_qla_host *ha =
981 		container_of(work, struct scsi_qla_host, dpc_work);
982 	struct ddb_entry *ddb_entry, *dtemp;
983 	int status = QLA_ERROR;
984 
985 	DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
986 		"flags = 0x%08lx, dpc_flags = 0x%08lx ctrl_stat = 0x%08x\n",
987 		ha->host_no, __func__, ha->flags, ha->dpc_flags,
988 		readw(&ha->reg->ctrl_status)));
989 
990 	/* Initialization not yet finished. Don't do anything yet. */
991 	if (!test_bit(AF_INIT_DONE, &ha->flags))
992 		return;
993 
994 	if (adapter_up(ha) ||
995 	    test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
996 	    test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
997 	    test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags)) {
998 		if (test_bit(DPC_RESET_HA_DESTROY_DDB_LIST, &ha->dpc_flags) ||
999 			test_bit(DPC_RESET_HA, &ha->dpc_flags))
1000 			qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST);
1001 
1002 		if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
1003 			uint8_t wait_time = RESET_INTR_TOV;
1004 
1005 			while ((readw(&ha->reg->ctrl_status) &
1006 				(CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
1007 				if (--wait_time == 0)
1008 					break;
1009 				msleep(1000);
1010 			}
1011 			if (wait_time == 0)
1012 				DEBUG2(printk("scsi%ld: %s: SR|FSR "
1013 					      "bit not cleared-- resetting\n",
1014 					      ha->host_no, __func__));
1015 			qla4xxx_flush_active_srbs(ha);
1016 			if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
1017 				qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
1018 				status = qla4xxx_initialize_adapter(ha,
1019 						PRESERVE_DDB_LIST);
1020 			}
1021 			clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1022 			if (status == QLA_SUCCESS)
1023 				qla4xxx_enable_intrs(ha);
1024 		}
1025 	}
1026 
1027 	/* ---- process AEN? --- */
1028 	if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
1029 		qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
1030 
1031 	/* ---- Get DHCP IP Address? --- */
1032 	if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
1033 		qla4xxx_get_dhcp_ip_address(ha);
1034 
1035 	/* ---- relogin device? --- */
1036 	if (adapter_up(ha) &&
1037 	    test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
1038 		list_for_each_entry_safe(ddb_entry, dtemp,
1039 					 &ha->ddb_list, list) {
1040 			if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
1041 			    atomic_read(&ddb_entry->state) != DDB_STATE_ONLINE)
1042 				qla4xxx_relogin_device(ha, ddb_entry);
1043 
1044 			/*
1045 			 * If mbx cmd times out there is no point
1046 			 * in continuing further.
1047 			 * With large no of targets this can hang
1048 			 * the system.
1049 			 */
1050 			if (test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
1051 				printk(KERN_WARNING "scsi%ld: %s: "
1052 				       "need to reset hba\n",
1053 				       ha->host_no, __func__);
1054 				break;
1055 			}
1056 		}
1057 	}
1058 }
1059 
1060 /**
1061  * qla4xxx_free_adapter - release the adapter
1062  * @ha: pointer to adapter structure
1063  **/
1064 static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
1065 {
1066 
1067 	if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
1068 		/* Turn-off interrupts on the card. */
1069 		qla4xxx_disable_intrs(ha);
1070 	}
1071 
1072 	/* Kill the kernel thread for this host */
1073 	if (ha->dpc_thread)
1074 		destroy_workqueue(ha->dpc_thread);
1075 
1076 	/* Issue Soft Reset to put firmware in unknown state */
1077 	if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS)
1078 		qla4xxx_hw_reset(ha);
1079 
1080 	/* Remove timer thread, if present */
1081 	if (ha->timer_active)
1082 		qla4xxx_stop_timer(ha);
1083 
1084 	/* free extra memory */
1085 	qla4xxx_mem_free(ha);
1086 
1087 	/* Detach interrupts */
1088 	if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
1089 		free_irq(ha->pdev->irq, ha);
1090 
1091 	pci_disable_device(ha->pdev);
1092 
1093 }
1094 
1095 /***
1096  * qla4xxx_iospace_config - maps registers
1097  * @ha: pointer to adapter structure
1098  *
1099  * This routines maps HBA's registers from the pci address space
1100  * into the kernel virtual address space for memory mapped i/o.
1101  **/
1102 static int qla4xxx_iospace_config(struct scsi_qla_host *ha)
1103 {
1104 	unsigned long pio, pio_len, pio_flags;
1105 	unsigned long mmio, mmio_len, mmio_flags;
1106 
1107 	pio = pci_resource_start(ha->pdev, 0);
1108 	pio_len = pci_resource_len(ha->pdev, 0);
1109 	pio_flags = pci_resource_flags(ha->pdev, 0);
1110 	if (pio_flags & IORESOURCE_IO) {
1111 		if (pio_len < MIN_IOBASE_LEN) {
1112 			dev_warn(&ha->pdev->dev,
1113 				"Invalid PCI I/O region size\n");
1114 			pio = 0;
1115 		}
1116 	} else {
1117 		dev_warn(&ha->pdev->dev, "region #0 not a PIO resource\n");
1118 		pio = 0;
1119 	}
1120 
1121 	/* Use MMIO operations for all accesses. */
1122 	mmio = pci_resource_start(ha->pdev, 1);
1123 	mmio_len = pci_resource_len(ha->pdev, 1);
1124 	mmio_flags = pci_resource_flags(ha->pdev, 1);
1125 
1126 	if (!(mmio_flags & IORESOURCE_MEM)) {
1127 		dev_err(&ha->pdev->dev,
1128 			"region #0 not an MMIO resource, aborting\n");
1129 
1130 		goto iospace_error_exit;
1131 	}
1132 	if (mmio_len < MIN_IOBASE_LEN) {
1133 		dev_err(&ha->pdev->dev,
1134 			"Invalid PCI mem region size, aborting\n");
1135 		goto iospace_error_exit;
1136 	}
1137 
1138 	if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
1139 		dev_warn(&ha->pdev->dev,
1140 			"Failed to reserve PIO/MMIO regions\n");
1141 
1142 		goto iospace_error_exit;
1143 	}
1144 
1145 	ha->pio_address = pio;
1146 	ha->pio_length = pio_len;
1147 	ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
1148 	if (!ha->reg) {
1149 		dev_err(&ha->pdev->dev,
1150 			"cannot remap MMIO, aborting\n");
1151 
1152 		goto iospace_error_exit;
1153 	}
1154 
1155 	return 0;
1156 
1157 iospace_error_exit:
1158 	return -ENOMEM;
1159 }
1160 
1161 /**
1162  * qla4xxx_probe_adapter - callback function to probe HBA
1163  * @pdev: pointer to pci_dev structure
1164  * @pci_device_id: pointer to pci_device entry
1165  *
1166  * This routine will probe for Qlogic 4xxx iSCSI host adapters.
1167  * It returns zero if successful. It also initializes all data necessary for
1168  * the driver.
1169  **/
1170 static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
1171 					   const struct pci_device_id *ent)
1172 {
1173 	int ret = -ENODEV, status;
1174 	struct Scsi_Host *host;
1175 	struct scsi_qla_host *ha;
1176 	struct ddb_entry *ddb_entry, *ddbtemp;
1177 	uint8_t init_retry_count = 0;
1178 	char buf[34];
1179 
1180 	if (pci_enable_device(pdev))
1181 		return -1;
1182 
1183 	host = scsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha));
1184 	if (host == NULL) {
1185 		printk(KERN_WARNING
1186 		       "qla4xxx: Couldn't allocate host from scsi layer!\n");
1187 		goto probe_disable_device;
1188 	}
1189 
1190 	/* Clear our data area */
1191 	ha = (struct scsi_qla_host *) host->hostdata;
1192 	memset(ha, 0, sizeof(*ha));
1193 
1194 	/* Save the information from PCI BIOS.	*/
1195 	ha->pdev = pdev;
1196 	ha->host = host;
1197 	ha->host_no = host->host_no;
1198 
1199 	/* Configure PCI I/O space. */
1200 	ret = qla4xxx_iospace_config(ha);
1201 	if (ret)
1202 		goto probe_failed;
1203 
1204 	dev_info(&ha->pdev->dev, "Found an ISP%04x, irq %d, iobase 0x%p\n",
1205 		   pdev->device, pdev->irq, ha->reg);
1206 
1207 	qla4xxx_config_dma_addressing(ha);
1208 
1209 	/* Initialize lists and spinlocks. */
1210 	INIT_LIST_HEAD(&ha->ddb_list);
1211 	INIT_LIST_HEAD(&ha->free_srb_q);
1212 
1213 	mutex_init(&ha->mbox_sem);
1214 
1215 	spin_lock_init(&ha->hardware_lock);
1216 
1217 	/* Allocate dma buffers */
1218 	if (qla4xxx_mem_alloc(ha)) {
1219 		dev_warn(&ha->pdev->dev,
1220 			   "[ERROR] Failed to allocate memory for adapter\n");
1221 
1222 		ret = -ENOMEM;
1223 		goto probe_failed;
1224 	}
1225 
1226 	/*
1227 	 * Initialize the Host adapter request/response queues and
1228 	 * firmware
1229 	 * NOTE: interrupts enabled upon successful completion
1230 	 */
1231 	status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1232 	while (status == QLA_ERROR && init_retry_count++ < MAX_INIT_RETRIES) {
1233 		DEBUG2(printk("scsi: %s: retrying adapter initialization "
1234 			      "(%d)\n", __func__, init_retry_count));
1235 		qla4xxx_soft_reset(ha);
1236 		status = qla4xxx_initialize_adapter(ha, REBUILD_DDB_LIST);
1237 	}
1238 	if (status == QLA_ERROR) {
1239 		dev_warn(&ha->pdev->dev, "Failed to initialize adapter\n");
1240 
1241 		ret = -ENODEV;
1242 		goto probe_failed;
1243 	}
1244 
1245 	host->cmd_per_lun = 3;
1246 	host->max_channel = 0;
1247 	host->max_lun = MAX_LUNS - 1;
1248 	host->max_id = MAX_TARGETS;
1249 	host->max_cmd_len = IOCB_MAX_CDB_LEN;
1250 	host->can_queue = MAX_SRBS ;
1251 	host->transportt = qla4xxx_scsi_transport;
1252 
1253         ret = scsi_init_shared_tag_map(host, MAX_SRBS);
1254         if (ret) {
1255                 dev_warn(&ha->pdev->dev, "scsi_init_shared_tag_map failed");
1256                 goto probe_failed;
1257         }
1258 
1259 	/* Startup the kernel thread for this host adapter. */
1260 	DEBUG2(printk("scsi: %s: Starting kernel thread for "
1261 		      "qla4xxx_dpc\n", __func__));
1262 	sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
1263 	ha->dpc_thread = create_singlethread_workqueue(buf);
1264 	if (!ha->dpc_thread) {
1265 		dev_warn(&ha->pdev->dev, "Unable to start DPC thread!\n");
1266 		ret = -ENODEV;
1267 		goto probe_failed;
1268 	}
1269 	INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
1270 
1271 	ret = request_irq(pdev->irq, qla4xxx_intr_handler,
1272 			  IRQF_DISABLED | IRQF_SHARED, "qla4xxx", ha);
1273 	if (ret) {
1274 		dev_warn(&ha->pdev->dev, "Failed to reserve interrupt %d"
1275 			" already in use.\n", pdev->irq);
1276 		goto probe_failed;
1277 	}
1278 	set_bit(AF_IRQ_ATTACHED, &ha->flags);
1279 	host->irq = pdev->irq;
1280 	DEBUG(printk("scsi%d: irq %d attached\n", ha->host_no, ha->pdev->irq));
1281 
1282 	qla4xxx_enable_intrs(ha);
1283 
1284 	/* Start timer thread. */
1285 	qla4xxx_start_timer(ha, qla4xxx_timer, 1);
1286 
1287 	set_bit(AF_INIT_DONE, &ha->flags);
1288 
1289 	pci_set_drvdata(pdev, ha);
1290 
1291 	ret = scsi_add_host(host, &pdev->dev);
1292 	if (ret)
1293 		goto probe_failed;
1294 
1295 	/* Update transport device information for all devices. */
1296 	list_for_each_entry_safe(ddb_entry, ddbtemp, &ha->ddb_list, list) {
1297 		if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE)
1298 			if (qla4xxx_add_sess(ddb_entry))
1299 				goto remove_host;
1300 	}
1301 
1302 	printk(KERN_INFO
1303 	       " QLogic iSCSI HBA Driver version: %s\n"
1304 	       "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
1305 	       qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
1306 	       ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
1307 	       ha->patch_number, ha->build_number);
1308 
1309 	return 0;
1310 
1311 remove_host:
1312 	qla4xxx_free_ddb_list(ha);
1313 	scsi_remove_host(host);
1314 
1315 probe_failed:
1316 	qla4xxx_free_adapter(ha);
1317 	scsi_host_put(ha->host);
1318 
1319 probe_disable_device:
1320 	pci_disable_device(pdev);
1321 
1322 	return ret;
1323 }
1324 
1325 /**
1326  * qla4xxx_remove_adapter - calback function to remove adapter.
1327  * @pci_dev: PCI device pointer
1328  **/
1329 static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
1330 {
1331 	struct scsi_qla_host *ha;
1332 
1333 	ha = pci_get_drvdata(pdev);
1334 
1335 	/* remove devs from iscsi_sessions to scsi_devices */
1336 	qla4xxx_free_ddb_list(ha);
1337 
1338 	scsi_remove_host(ha->host);
1339 
1340 	qla4xxx_free_adapter(ha);
1341 
1342 	scsi_host_put(ha->host);
1343 
1344 	pci_set_drvdata(pdev, NULL);
1345 }
1346 
1347 /**
1348  * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
1349  * @ha: HA context
1350  *
1351  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1352  * supported addressing method.
1353  */
1354 static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
1355 {
1356 	int retval;
1357 
1358 	/* Update our PCI device dma_mask for full 64 bit mask */
1359 	if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1360 		if (pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1361 			dev_dbg(&ha->pdev->dev,
1362 				  "Failed to set 64 bit PCI consistent mask; "
1363 				   "using 32 bit.\n");
1364 			retval = pci_set_consistent_dma_mask(ha->pdev,
1365 							     DMA_32BIT_MASK);
1366 		}
1367 	} else
1368 		retval = pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1369 }
1370 
1371 static int qla4xxx_slave_alloc(struct scsi_device *sdev)
1372 {
1373 	struct iscsi_cls_session *sess = starget_to_session(sdev->sdev_target);
1374 	struct ddb_entry *ddb = sess->dd_data;
1375 
1376 	sdev->hostdata = ddb;
1377 	sdev->tagged_supported = 1;
1378 	scsi_activate_tcq(sdev, sdev->host->can_queue);
1379 	return 0;
1380 }
1381 
1382 static int qla4xxx_slave_configure(struct scsi_device *sdev)
1383 {
1384 	sdev->tagged_supported = 1;
1385 	return 0;
1386 }
1387 
1388 static void qla4xxx_slave_destroy(struct scsi_device *sdev)
1389 {
1390 	scsi_deactivate_tcq(sdev, 1);
1391 }
1392 
1393 /**
1394  * qla4xxx_del_from_active_array - returns an active srb
1395  * @ha: Pointer to host adapter structure.
1396  * @index: index into to the active_array
1397  *
1398  * This routine removes and returns the srb at the specified index
1399  **/
1400 struct srb * qla4xxx_del_from_active_array(struct scsi_qla_host *ha, uint32_t index)
1401 {
1402 	struct srb *srb = NULL;
1403 	struct scsi_cmnd *cmd;
1404 
1405 	if (!(cmd = scsi_host_find_tag(ha->host, index)))
1406 		return srb;
1407 
1408 	if (!(srb = (struct srb *)cmd->host_scribble))
1409 		return srb;
1410 
1411 	/* update counters */
1412 	if (srb->flags & SRB_DMA_VALID) {
1413 		ha->req_q_count += srb->iocb_cnt;
1414 		ha->iocb_cnt -= srb->iocb_cnt;
1415 		if (srb->cmd)
1416 			srb->cmd->host_scribble = NULL;
1417 	}
1418 	return srb;
1419 }
1420 
1421 /**
1422  * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
1423  * @ha: actual ha whose done queue will contain the comd returned by firmware.
1424  * @cmd: Scsi Command to wait on.
1425  *
1426  * This routine waits for the command to be returned by the Firmware
1427  * for some max time.
1428  **/
1429 static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
1430 				      struct scsi_cmnd *cmd)
1431 {
1432 	int done = 0;
1433 	struct srb *rp;
1434 	uint32_t max_wait_time = EH_WAIT_CMD_TOV;
1435 
1436 	do {
1437 		/* Checking to see if its returned to OS */
1438 		rp = (struct srb *) cmd->SCp.ptr;
1439 		if (rp == NULL) {
1440 			done++;
1441 			break;
1442 		}
1443 
1444 		msleep(2000);
1445 	} while (max_wait_time--);
1446 
1447 	return done;
1448 }
1449 
1450 /**
1451  * qla4xxx_wait_for_hba_online - waits for HBA to come online
1452  * @ha: Pointer to host adapter structure
1453  **/
1454 static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
1455 {
1456 	unsigned long wait_online;
1457 
1458 	wait_online = jiffies + (30 * HZ);
1459 	while (time_before(jiffies, wait_online)) {
1460 
1461 		if (adapter_up(ha))
1462 			return QLA_SUCCESS;
1463 		else if (ha->retry_reset_ha_cnt == 0)
1464 			return QLA_ERROR;
1465 
1466 		msleep(2000);
1467 	}
1468 
1469 	return QLA_ERROR;
1470 }
1471 
1472 /**
1473  * qla4xxx_eh_wait_for_active_target_commands - wait for active cmds to finish.
1474  * @ha: pointer to to HBA
1475  * @t: target id
1476  * @l: lun id
1477  *
1478  * This function waits for all outstanding commands to a lun to complete. It
1479  * returns 0 if all pending commands are returned and 1 otherwise.
1480  **/
1481 static int qla4xxx_eh_wait_for_active_target_commands(struct scsi_qla_host *ha,
1482 						 int t, int l)
1483 {
1484 	int cnt;
1485 	int status = 0;
1486 	struct scsi_cmnd *cmd;
1487 
1488 	/*
1489 	 * Waiting for all commands for the designated target in the active
1490 	 * array
1491 	 */
1492 	for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
1493 		cmd = scsi_host_find_tag(ha->host, cnt);
1494 		if (cmd && cmd->device->id == t && cmd->device->lun == l) {
1495 			if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
1496 				status++;
1497 				break;
1498 			}
1499 		}
1500 	}
1501 	return status;
1502 }
1503 
1504 /**
1505  * qla4xxx_eh_device_reset - callback for target reset.
1506  * @cmd: Pointer to Linux's SCSI command structure
1507  *
1508  * This routine is called by the Linux OS to reset all luns on the
1509  * specified target.
1510  **/
1511 static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
1512 {
1513 	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
1514 	struct ddb_entry *ddb_entry = cmd->device->hostdata;
1515 	struct srb *sp;
1516 	int ret = FAILED, stat;
1517 
1518 	sp = (struct srb *) cmd->SCp.ptr;
1519 	if (!sp || !ddb_entry)
1520 		return ret;
1521 
1522 	dev_info(&ha->pdev->dev,
1523 		   "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
1524 		   cmd->device->channel, cmd->device->id, cmd->device->lun);
1525 
1526 	DEBUG2(printk(KERN_INFO
1527 		      "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
1528 		      "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
1529 		      cmd, jiffies, cmd->timeout_per_command / HZ,
1530 		      ha->dpc_flags, cmd->result, cmd->allowed));
1531 
1532 	/* FIXME: wait for hba to go online */
1533 	stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
1534 	if (stat != QLA_SUCCESS) {
1535 		dev_info(&ha->pdev->dev, "DEVICE RESET FAILED. %d\n", stat);
1536 		goto eh_dev_reset_done;
1537 	}
1538 
1539 	/* Send marker. */
1540 	ha->marker_needed = 1;
1541 
1542 	/*
1543 	 * If we are coming down the EH path, wait for all commands to complete
1544 	 * for the device.
1545 	 */
1546 	if (cmd->device->host->shost_state == SHOST_RECOVERY) {
1547 		if (qla4xxx_eh_wait_for_active_target_commands(ha,
1548 							  cmd->device->id,
1549 							  cmd->device->lun)){
1550 			dev_info(&ha->pdev->dev,
1551 				   "DEVICE RESET FAILED - waiting for "
1552 				   "commands.\n");
1553 			goto eh_dev_reset_done;
1554 		}
1555 	}
1556 
1557 	dev_info(&ha->pdev->dev,
1558 		   "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1559 		   ha->host_no, cmd->device->channel, cmd->device->id,
1560 		   cmd->device->lun);
1561 
1562 	ret = SUCCESS;
1563 
1564 eh_dev_reset_done:
1565 
1566 	return ret;
1567 }
1568 
1569 /**
1570  * qla4xxx_eh_host_reset - kernel callback
1571  * @cmd: Pointer to Linux's SCSI command structure
1572  *
1573  * This routine is invoked by the Linux kernel to perform fatal error
1574  * recovery on the specified adapter.
1575  **/
1576 static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
1577 {
1578 	int return_status = FAILED;
1579 	struct scsi_qla_host *ha;
1580 
1581 	ha = (struct scsi_qla_host *) cmd->device->host->hostdata;
1582 
1583 	dev_info(&ha->pdev->dev,
1584 		   "scsi(%ld:%d:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no,
1585 		   cmd->device->channel, cmd->device->id, cmd->device->lun);
1586 
1587 	if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
1588 		DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
1589 			      "DEAD.\n", ha->host_no, cmd->device->channel,
1590 			      __func__));
1591 
1592 		return FAILED;
1593 	}
1594 
1595 	if (qla4xxx_recover_adapter(ha, PRESERVE_DDB_LIST) == QLA_SUCCESS) {
1596 		return_status = SUCCESS;
1597 	}
1598 
1599 	dev_info(&ha->pdev->dev, "HOST RESET %s.\n",
1600 		   return_status == FAILED ? "FAILED" : "SUCCEDED");
1601 
1602 	return return_status;
1603 }
1604 
1605 
1606 static struct pci_device_id qla4xxx_pci_tbl[] = {
1607 	{
1608 		.vendor		= PCI_VENDOR_ID_QLOGIC,
1609 		.device		= PCI_DEVICE_ID_QLOGIC_ISP4010,
1610 		.subvendor	= PCI_ANY_ID,
1611 		.subdevice	= PCI_ANY_ID,
1612 	},
1613 	{
1614 		.vendor		= PCI_VENDOR_ID_QLOGIC,
1615 		.device		= PCI_DEVICE_ID_QLOGIC_ISP4022,
1616 		.subvendor	= PCI_ANY_ID,
1617 		.subdevice	= PCI_ANY_ID,
1618 	},
1619 	{
1620 		.vendor		= PCI_VENDOR_ID_QLOGIC,
1621 		.device		= PCI_DEVICE_ID_QLOGIC_ISP4032,
1622 		.subvendor	= PCI_ANY_ID,
1623 		.subdevice	= PCI_ANY_ID,
1624 	},
1625 	{0, 0},
1626 };
1627 MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
1628 
1629 static struct pci_driver qla4xxx_pci_driver = {
1630 	.name		= DRIVER_NAME,
1631 	.id_table	= qla4xxx_pci_tbl,
1632 	.probe		= qla4xxx_probe_adapter,
1633 	.remove		= qla4xxx_remove_adapter,
1634 };
1635 
1636 static int __init qla4xxx_module_init(void)
1637 {
1638 	int ret;
1639 
1640 	/* Allocate cache for SRBs. */
1641 	srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
1642 				       SLAB_HWCACHE_ALIGN, NULL, NULL);
1643 	if (srb_cachep == NULL) {
1644 		printk(KERN_ERR
1645 		       "%s: Unable to allocate SRB cache..."
1646 		       "Failing load!\n", DRIVER_NAME);
1647 		ret = -ENOMEM;
1648 		goto no_srp_cache;
1649 	}
1650 
1651 	/* Derive version string. */
1652 	strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
1653 	if (ql4xextended_error_logging)
1654 		strcat(qla4xxx_version_str, "-debug");
1655 
1656 	qla4xxx_scsi_transport =
1657 		iscsi_register_transport(&qla4xxx_iscsi_transport);
1658 	if (!qla4xxx_scsi_transport){
1659 		ret = -ENODEV;
1660 		goto release_srb_cache;
1661 	}
1662 
1663 	ret = pci_register_driver(&qla4xxx_pci_driver);
1664 	if (ret)
1665 		goto unregister_transport;
1666 
1667 	printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
1668 	return 0;
1669 
1670 unregister_transport:
1671 	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1672 release_srb_cache:
1673 	kmem_cache_destroy(srb_cachep);
1674 no_srp_cache:
1675 	return ret;
1676 }
1677 
1678 static void __exit qla4xxx_module_exit(void)
1679 {
1680 	ql4_mod_unload = 1;
1681 	pci_unregister_driver(&qla4xxx_pci_driver);
1682 	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
1683 	kmem_cache_destroy(srb_cachep);
1684 }
1685 
1686 module_init(qla4xxx_module_init);
1687 module_exit(qla4xxx_module_exit);
1688 
1689 MODULE_AUTHOR("QLogic Corporation");
1690 MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
1691 MODULE_LICENSE("GPL");
1692 MODULE_VERSION(QLA4XXX_DRIVER_VERSION);
1693