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