xref: /openbmc/linux/drivers/scsi/qla4xxx/ql4_init.c (revision 5b394b2d)
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2013 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7 
8 #include <scsi/iscsi_if.h>
9 #include "ql4_def.h"
10 #include "ql4_glbl.h"
11 #include "ql4_dbg.h"
12 #include "ql4_inline.h"
13 
14 static void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
15 {
16 	uint32_t value;
17 	uint8_t func_number;
18 	unsigned long flags;
19 
20 	/* Get the function number */
21 	spin_lock_irqsave(&ha->hardware_lock, flags);
22 	value = readw(&ha->reg->ctrl_status);
23 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
24 
25 	func_number = (uint8_t) ((value >> 4) & 0x30);
26 	switch (value & ISP_CONTROL_FN_MASK) {
27 	case ISP_CONTROL_FN0_SCSI:
28 		ha->mac_index = 1;
29 		break;
30 	case ISP_CONTROL_FN1_SCSI:
31 		ha->mac_index = 3;
32 		break;
33 	default:
34 		DEBUG2(printk("scsi%ld: %s: Invalid function number, "
35 			      "ispControlStatus = 0x%x\n", ha->host_no,
36 			      __func__, value));
37 		break;
38 	}
39 	DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
40 		      ha->mac_index));
41 }
42 
43 /**
44  * qla4xxx_free_ddb - deallocate ddb
45  * @ha: pointer to host adapter structure.
46  * @ddb_entry: pointer to device database entry
47  *
48  * This routine marks a DDB entry INVALID
49  **/
50 void qla4xxx_free_ddb(struct scsi_qla_host *ha,
51     struct ddb_entry *ddb_entry)
52 {
53 	/* Remove device pointer from index mapping arrays */
54 	ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
55 		(struct ddb_entry *) INVALID_ENTRY;
56 	ha->tot_ddbs--;
57 }
58 
59 /**
60  * qla4xxx_init_response_q_entries() - Initializes response queue entries.
61  * @ha: HA context
62  *
63  * Beginning of request ring has initialization control block already built
64  * by nvram config routine.
65  **/
66 static void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
67 {
68 	uint16_t cnt;
69 	struct response *pkt;
70 
71 	pkt = (struct response *)ha->response_ptr;
72 	for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
73 		pkt->signature = RESPONSE_PROCESSED;
74 		pkt++;
75 	}
76 }
77 
78 /**
79  * qla4xxx_init_rings - initialize hw queues
80  * @ha: pointer to host adapter structure.
81  *
82  * This routine initializes the internal queues for the specified adapter.
83  * The QLA4010 requires us to restart the queues at index 0.
84  * The QLA4000 doesn't care, so just default to QLA4010's requirement.
85  **/
86 int qla4xxx_init_rings(struct scsi_qla_host *ha)
87 {
88 	unsigned long flags = 0;
89 	int i;
90 
91 	/* Initialize request queue. */
92 	spin_lock_irqsave(&ha->hardware_lock, flags);
93 	ha->request_out = 0;
94 	ha->request_in = 0;
95 	ha->request_ptr = &ha->request_ring[ha->request_in];
96 	ha->req_q_count = REQUEST_QUEUE_DEPTH;
97 
98 	/* Initialize response queue. */
99 	ha->response_in = 0;
100 	ha->response_out = 0;
101 	ha->response_ptr = &ha->response_ring[ha->response_out];
102 
103 	if (is_qla8022(ha)) {
104 		writel(0,
105 		    (unsigned long  __iomem *)&ha->qla4_82xx_reg->req_q_out);
106 		writel(0,
107 		    (unsigned long  __iomem *)&ha->qla4_82xx_reg->rsp_q_in);
108 		writel(0,
109 		    (unsigned long  __iomem *)&ha->qla4_82xx_reg->rsp_q_out);
110 	} else if (is_qla8032(ha) || is_qla8042(ha)) {
111 		writel(0,
112 		       (unsigned long __iomem *)&ha->qla4_83xx_reg->req_q_in);
113 		writel(0,
114 		       (unsigned long __iomem *)&ha->qla4_83xx_reg->rsp_q_in);
115 		writel(0,
116 		       (unsigned long __iomem *)&ha->qla4_83xx_reg->rsp_q_out);
117 	} else {
118 		/*
119 		 * Initialize DMA Shadow registers.  The firmware is really
120 		 * supposed to take care of this, but on some uniprocessor
121 		 * systems, the shadow registers aren't cleared-- causing
122 		 * the interrupt_handler to think there are responses to be
123 		 * processed when there aren't.
124 		 */
125 		ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
126 		ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
127 		wmb();
128 
129 		writel(0, &ha->reg->req_q_in);
130 		writel(0, &ha->reg->rsp_q_out);
131 		readl(&ha->reg->rsp_q_out);
132 	}
133 
134 	qla4xxx_init_response_q_entries(ha);
135 
136 	/* Initialize mailbox active array */
137 	for (i = 0; i < MAX_MRB; i++)
138 		ha->active_mrb_array[i] = NULL;
139 
140 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
141 
142 	return QLA_SUCCESS;
143 }
144 
145 /**
146  * qla4xxx_get_sys_info - validate adapter MAC address(es)
147  * @ha: pointer to host adapter structure.
148  *
149  **/
150 int qla4xxx_get_sys_info(struct scsi_qla_host *ha)
151 {
152 	struct flash_sys_info *sys_info;
153 	dma_addr_t sys_info_dma;
154 	int status = QLA_ERROR;
155 
156 	sys_info = dma_zalloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
157 				       &sys_info_dma, GFP_KERNEL);
158 	if (sys_info == NULL) {
159 		DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
160 			      ha->host_no, __func__));
161 
162 		goto exit_get_sys_info_no_free;
163 	}
164 
165 	/* Get flash sys info */
166 	if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
167 			      sizeof(*sys_info)) != QLA_SUCCESS) {
168 		DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
169 			      "failed\n", ha->host_no, __func__));
170 
171 		goto exit_get_sys_info;
172 	}
173 
174 	/* Save M.A.C. address & serial_number */
175 	memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
176 	       min(sizeof(ha->my_mac),
177 		   sizeof(sys_info->physAddr[0].address)));
178 	memcpy(ha->serial_number, &sys_info->acSerialNumber,
179 	       min(sizeof(ha->serial_number),
180 		   sizeof(sys_info->acSerialNumber)));
181 
182 	status = QLA_SUCCESS;
183 
184 exit_get_sys_info:
185 	dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
186 			  sys_info_dma);
187 
188 exit_get_sys_info_no_free:
189 	return status;
190 }
191 
192 /**
193  * qla4xxx_init_local_data - initialize adapter specific local data
194  * @ha: pointer to host adapter structure.
195  *
196  **/
197 static void qla4xxx_init_local_data(struct scsi_qla_host *ha)
198 {
199 	/* Initialize aen queue */
200 	ha->aen_q_count = MAX_AEN_ENTRIES;
201 }
202 
203 static uint8_t
204 qla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
205 {
206 	uint8_t ipv4_wait = 0;
207 	uint8_t ipv6_wait = 0;
208 	int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
209 
210 	/* If both IPv4 & IPv6 are enabled, possibly only one
211 	 * IP address may be acquired, so check to see if we
212 	 * need to wait for another */
213 	if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
214 		if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
215 		    ((ha->addl_fw_state &
216 				    FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
217 			ipv4_wait = 1;
218 		}
219 		if (((ha->ip_config.ipv6_addl_options &
220 		      IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
221 		    ((ha->ip_config.ipv6_link_local_state ==
222 		      IP_ADDRSTATE_ACQUIRING) ||
223 		     (ha->ip_config.ipv6_addr0_state ==
224 		      IP_ADDRSTATE_ACQUIRING) ||
225 		     (ha->ip_config.ipv6_addr1_state ==
226 		      IP_ADDRSTATE_ACQUIRING))) {
227 
228 			ipv6_wait = 1;
229 
230 			if ((ha->ip_config.ipv6_link_local_state ==
231 			     IP_ADDRSTATE_PREFERRED) ||
232 			    (ha->ip_config.ipv6_addr0_state ==
233 			     IP_ADDRSTATE_PREFERRED) ||
234 			    (ha->ip_config.ipv6_addr1_state ==
235 			     IP_ADDRSTATE_PREFERRED)) {
236 				DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
237 					      "Preferred IP configured."
238 					      " Don't wait!\n", ha->host_no,
239 					      __func__));
240 				ipv6_wait = 0;
241 			}
242 			if (memcmp(&ha->ip_config.ipv6_default_router_addr,
243 				   ip_address, IPv6_ADDR_LEN) == 0) {
244 				DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
245 					      "No Router configured. "
246 					      "Don't wait!\n", ha->host_no,
247 					      __func__));
248 				ipv6_wait = 0;
249 			}
250 			if ((ha->ip_config.ipv6_default_router_state ==
251 			     IPV6_RTRSTATE_MANUAL) &&
252 			    (ha->ip_config.ipv6_link_local_state ==
253 			     IP_ADDRSTATE_TENTATIVE) &&
254 			    (memcmp(&ha->ip_config.ipv6_link_local_addr,
255 			     &ha->ip_config.ipv6_default_router_addr, 4) ==
256 			     0)) {
257 				DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
258 					"IP configured. Don't wait!\n",
259 					ha->host_no, __func__));
260 				ipv6_wait = 0;
261 			}
262 		}
263 		if (ipv4_wait || ipv6_wait) {
264 			DEBUG2(printk("scsi%ld: %s: Wait for additional "
265 				      "IP(s) \"", ha->host_no, __func__));
266 			if (ipv4_wait)
267 				DEBUG2(printk("IPv4 "));
268 			if (ha->ip_config.ipv6_link_local_state ==
269 			    IP_ADDRSTATE_ACQUIRING)
270 				DEBUG2(printk("IPv6LinkLocal "));
271 			if (ha->ip_config.ipv6_addr0_state ==
272 			    IP_ADDRSTATE_ACQUIRING)
273 				DEBUG2(printk("IPv6Addr0 "));
274 			if (ha->ip_config.ipv6_addr1_state ==
275 			    IP_ADDRSTATE_ACQUIRING)
276 				DEBUG2(printk("IPv6Addr1 "));
277 			DEBUG2(printk("\"\n"));
278 		}
279 	}
280 
281 	return ipv4_wait|ipv6_wait;
282 }
283 
284 static int qla4_80xx_is_minidump_dma_capable(struct scsi_qla_host *ha,
285 		struct qla4_8xxx_minidump_template_hdr *md_hdr)
286 {
287 	int offset = (is_qla8022(ha)) ? QLA8022_TEMPLATE_CAP_OFFSET :
288 					QLA83XX_TEMPLATE_CAP_OFFSET;
289 	int rval = 1;
290 	uint32_t *cap_offset;
291 
292 	cap_offset = (uint32_t *)((char *)md_hdr + offset);
293 
294 	if (!(le32_to_cpu(*cap_offset) & BIT_0)) {
295 		ql4_printk(KERN_INFO, ha, "PEX DMA Not supported %d\n",
296 			   *cap_offset);
297 		rval = 0;
298 	}
299 
300 	return rval;
301 }
302 
303 /**
304  * qla4xxx_alloc_fw_dump - Allocate memory for minidump data.
305  * @ha: pointer to host adapter structure.
306  **/
307 void qla4xxx_alloc_fw_dump(struct scsi_qla_host *ha)
308 {
309 	int status;
310 	uint32_t capture_debug_level;
311 	int hdr_entry_bit, k;
312 	void *md_tmp;
313 	dma_addr_t md_tmp_dma;
314 	struct qla4_8xxx_minidump_template_hdr *md_hdr;
315 	int dma_capable;
316 
317 	if (ha->fw_dump) {
318 		ql4_printk(KERN_WARNING, ha,
319 			   "Firmware dump previously allocated.\n");
320 		return;
321 	}
322 
323 	status = qla4xxx_req_template_size(ha);
324 	if (status != QLA_SUCCESS) {
325 		ql4_printk(KERN_INFO, ha,
326 			   "scsi%ld: Failed to get template size\n",
327 			   ha->host_no);
328 		return;
329 	}
330 
331 	clear_bit(AF_82XX_FW_DUMPED, &ha->flags);
332 
333 	/* Allocate memory for saving the template */
334 	md_tmp = dma_alloc_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
335 				    &md_tmp_dma, GFP_KERNEL);
336 	if (!md_tmp) {
337 		ql4_printk(KERN_INFO, ha,
338 			   "scsi%ld: Failed to allocate DMA memory\n",
339 			   ha->host_no);
340 		return;
341 	}
342 
343 	/* Request template */
344 	status =  qla4xxx_get_minidump_template(ha, md_tmp_dma);
345 	if (status != QLA_SUCCESS) {
346 		ql4_printk(KERN_INFO, ha,
347 			   "scsi%ld: Failed to get minidump template\n",
348 			   ha->host_no);
349 		goto alloc_cleanup;
350 	}
351 
352 	md_hdr = (struct qla4_8xxx_minidump_template_hdr *)md_tmp;
353 
354 	dma_capable = qla4_80xx_is_minidump_dma_capable(ha, md_hdr);
355 
356 	capture_debug_level = md_hdr->capture_debug_level;
357 
358 	/* Get capture mask based on module loadtime setting. */
359 	if ((ql4xmdcapmask >= 0x3 && ql4xmdcapmask <= 0x7F) ||
360 	    (ql4xmdcapmask == 0xFF && dma_capable))  {
361 		ha->fw_dump_capture_mask = ql4xmdcapmask;
362 	} else {
363 		if (ql4xmdcapmask == 0xFF)
364 			ql4_printk(KERN_INFO, ha, "Falling back to default capture mask, as PEX DMA is not supported\n");
365 		ha->fw_dump_capture_mask = capture_debug_level;
366 	}
367 
368 	md_hdr->driver_capture_mask = ha->fw_dump_capture_mask;
369 
370 	DEBUG2(ql4_printk(KERN_INFO, ha, "Minimum num of entries = %d\n",
371 			  md_hdr->num_of_entries));
372 	DEBUG2(ql4_printk(KERN_INFO, ha, "Dump template size  = %d\n",
373 			  ha->fw_dump_tmplt_size));
374 	DEBUG2(ql4_printk(KERN_INFO, ha, "Selected Capture mask =0x%x\n",
375 			  ha->fw_dump_capture_mask));
376 
377 	/* Calculate fw_dump_size */
378 	for (hdr_entry_bit = 0x2, k = 1; (hdr_entry_bit & 0xFF);
379 	     hdr_entry_bit <<= 1, k++) {
380 		if (hdr_entry_bit & ha->fw_dump_capture_mask)
381 			ha->fw_dump_size += md_hdr->capture_size_array[k];
382 	}
383 
384 	/* Total firmware dump size including command header */
385 	ha->fw_dump_size += ha->fw_dump_tmplt_size;
386 	ha->fw_dump = vmalloc(ha->fw_dump_size);
387 	if (!ha->fw_dump)
388 		goto alloc_cleanup;
389 
390 	DEBUG2(ql4_printk(KERN_INFO, ha,
391 			  "Minidump Template Size = 0x%x KB\n",
392 			  ha->fw_dump_tmplt_size));
393 	DEBUG2(ql4_printk(KERN_INFO, ha,
394 			  "Total Minidump size = 0x%x KB\n", ha->fw_dump_size));
395 
396 	memcpy(ha->fw_dump, md_tmp, ha->fw_dump_tmplt_size);
397 	ha->fw_dump_tmplt_hdr = ha->fw_dump;
398 
399 alloc_cleanup:
400 	dma_free_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
401 			  md_tmp, md_tmp_dma);
402 }
403 
404 static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
405 {
406 	uint32_t timeout_count;
407 	int ready = 0;
408 
409 	DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
410 	for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
411 	     timeout_count--) {
412 		if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
413 			qla4xxx_get_dhcp_ip_address(ha);
414 
415 		/* Get firmware state. */
416 		if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
417 			DEBUG2(printk("scsi%ld: %s: unable to get firmware "
418 				      "state\n", ha->host_no, __func__));
419 			break;
420 		}
421 
422 		if (ha->firmware_state & FW_STATE_ERROR) {
423 			DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
424 				      " occurred\n", ha->host_no, __func__));
425 			break;
426 
427 		}
428 		if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
429 			/*
430 			 * The firmware has not yet been issued an Initialize
431 			 * Firmware command, so issue it now.
432 			 */
433 			if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
434 				break;
435 
436 			/* Go back and test for ready state - no wait. */
437 			continue;
438 		}
439 
440 		if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
441 			DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
442 				      "AUTOCONNECT in progress\n",
443 				      ha->host_no, __func__));
444 		}
445 
446 		if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
447 			DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
448 				      " CONFIGURING IP\n",
449 				      ha->host_no, __func__));
450 			/*
451 			 * Check for link state after 15 secs and if link is
452 			 * still DOWN then, cable is unplugged. Ignore "DHCP
453 			 * in Progress/CONFIGURING IP" bit to check if firmware
454 			 * is in ready state or not after 15 secs.
455 			 * This is applicable for both 2.x & 3.x firmware
456 			 */
457 			if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
458 				if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
459 					DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
460 						  " LINK UP (Cable plugged)\n",
461 						  ha->host_no, __func__));
462 				} else if (ha->firmware_state &
463 					  (FW_STATE_CONFIGURING_IP |
464 							     FW_STATE_READY)) {
465 					DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
466 						"LINK DOWN (Cable unplugged)\n",
467 						ha->host_no, __func__));
468 					ha->firmware_state = FW_STATE_READY;
469 				}
470 			}
471 		}
472 
473 		if (ha->firmware_state == FW_STATE_READY) {
474 			/* If DHCP IP Addr is available, retrieve it now. */
475 			if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
476 								&ha->dpc_flags))
477 				qla4xxx_get_dhcp_ip_address(ha);
478 
479 			if (!qla4xxx_wait_for_ip_config(ha) ||
480 							timeout_count == 1) {
481 				DEBUG2(ql4_printk(KERN_INFO, ha,
482 				    "Firmware Ready..\n"));
483 				/* The firmware is ready to process SCSI
484 				   commands. */
485 				DEBUG2(ql4_printk(KERN_INFO, ha,
486 					"scsi%ld: %s: MEDIA TYPE"
487 					" - %s\n", ha->host_no,
488 					__func__, (ha->addl_fw_state &
489 					FW_ADDSTATE_OPTICAL_MEDIA)
490 					!= 0 ? "OPTICAL" : "COPPER"));
491 				DEBUG2(ql4_printk(KERN_INFO, ha,
492 					"scsi%ld: %s: DHCPv4 STATE"
493 					" Enabled %s\n", ha->host_no,
494 					 __func__, (ha->addl_fw_state &
495 					 FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
496 					"YES" : "NO"));
497 				DEBUG2(ql4_printk(KERN_INFO, ha,
498 					"scsi%ld: %s: LINK %s\n",
499 					ha->host_no, __func__,
500 					(ha->addl_fw_state &
501 					 FW_ADDSTATE_LINK_UP) != 0 ?
502 					"UP" : "DOWN"));
503 				DEBUG2(ql4_printk(KERN_INFO, ha,
504 					"scsi%ld: %s: iSNS Service "
505 					"Started %s\n",
506 					ha->host_no, __func__,
507 					(ha->addl_fw_state &
508 					 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
509 					"YES" : "NO"));
510 
511 				ready = 1;
512 				break;
513 			}
514 		}
515 		DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
516 			      "seconds expired= %d\n", ha->host_no, __func__,
517 			      ha->firmware_state, ha->addl_fw_state,
518 			      timeout_count));
519 		if (is_qla4032(ha) &&
520 			!(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
521 			(timeout_count < ADAPTER_INIT_TOV - 5)) {
522 			break;
523 		}
524 
525 		msleep(1000);
526 	}			/* end of for */
527 
528 	if (timeout_count <= 0)
529 		DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
530 			      ha->host_no, __func__));
531 
532 	if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
533 		DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
534 			      "it's waiting to configure an IP address\n",
535 			       ha->host_no, __func__));
536 		ready = 1;
537 	} else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
538 		DEBUG2(printk("scsi%ld: %s: FW initialized, but "
539 			      "auto-discovery still in process\n",
540 			       ha->host_no, __func__));
541 		ready = 1;
542 	}
543 
544 	return ready;
545 }
546 
547 /**
548  * qla4xxx_init_firmware - initializes the firmware.
549  * @ha: pointer to host adapter structure.
550  *
551  **/
552 static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
553 {
554 	int status = QLA_ERROR;
555 
556 	if (is_aer_supported(ha) &&
557 	    test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
558 		return status;
559 
560 	/* For 82xx, stop firmware before initializing because if BIOS
561 	 * has previously initialized firmware, then driver's initialize
562 	 * firmware will fail. */
563 	if (is_qla80XX(ha))
564 		qla4_8xxx_stop_firmware(ha);
565 
566 	ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
567 	if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
568 		DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
569 			      "control block\n", ha->host_no, __func__));
570 		return status;
571 	}
572 
573 	if (!qla4xxx_fw_ready(ha))
574 		return status;
575 
576 	if (is_qla80XX(ha) && !test_bit(AF_INIT_DONE, &ha->flags))
577 		qla4xxx_alloc_fw_dump(ha);
578 
579 	return qla4xxx_get_firmware_status(ha);
580 }
581 
582 static void qla4xxx_set_model_info(struct scsi_qla_host *ha)
583 {
584 	uint16_t board_id_string[8];
585 	int i;
586 	int size = sizeof(ha->nvram->isp4022.boardIdStr);
587 	int offset = offsetof(struct eeprom_data, isp4022.boardIdStr) / 2;
588 
589 	for (i = 0; i < (size / 2) ; i++) {
590 		board_id_string[i] = rd_nvram_word(ha, offset);
591 		offset += 1;
592 	}
593 
594 	memcpy(ha->model_name, board_id_string, size);
595 }
596 
597 static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
598 {
599 	unsigned long flags;
600 	union external_hw_config_reg extHwConfig;
601 
602 	DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
603 		      __func__));
604 	if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
605 		return QLA_ERROR;
606 	if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
607 		ql4xxx_unlock_flash(ha);
608 		return QLA_ERROR;
609 	}
610 
611 	/* Get EEPRom Parameters from NVRAM and validate */
612 	ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
613 	if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
614 		spin_lock_irqsave(&ha->hardware_lock, flags);
615 		extHwConfig.Asuint32_t =
616 			rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
617 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
618 	} else {
619 		ql4_printk(KERN_WARNING, ha,
620 		    "scsi%ld: %s: EEProm checksum invalid.  "
621 		    "Please update your EEPROM\n", ha->host_no,
622 		    __func__);
623 
624 		/* Attempt to set defaults */
625 		if (is_qla4010(ha))
626 			extHwConfig.Asuint32_t = 0x1912;
627 		else if (is_qla4022(ha) | is_qla4032(ha))
628 			extHwConfig.Asuint32_t = 0x0023;
629 		else
630 			return QLA_ERROR;
631 	}
632 
633 	if (is_qla4022(ha) || is_qla4032(ha))
634 		qla4xxx_set_model_info(ha);
635 	else
636 		strcpy(ha->model_name, "QLA4010");
637 
638 	DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
639 		     ha->host_no, __func__, extHwConfig.Asuint32_t));
640 
641 	spin_lock_irqsave(&ha->hardware_lock, flags);
642 	writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
643 	readl(isp_ext_hw_conf(ha));
644 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
645 
646 	ql4xxx_unlock_nvram(ha);
647 	ql4xxx_unlock_flash(ha);
648 
649 	return QLA_SUCCESS;
650 }
651 
652 /**
653  * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
654  * @ha: HA context
655  */
656 void qla4_8xxx_pci_config(struct scsi_qla_host *ha)
657 {
658 	pci_set_master(ha->pdev);
659 }
660 
661 void qla4xxx_pci_config(struct scsi_qla_host *ha)
662 {
663 	uint16_t w;
664 	int status;
665 
666 	ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
667 
668 	pci_set_master(ha->pdev);
669 	status = pci_set_mwi(ha->pdev);
670 	/*
671 	 * We want to respect framework's setting of PCI configuration space
672 	 * command register and also want to make sure that all bits of
673 	 * interest to us are properly set in command register.
674 	 */
675 	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
676 	w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
677 	w &= ~PCI_COMMAND_INTX_DISABLE;
678 	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
679 }
680 
681 static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
682 {
683 	int status = QLA_ERROR;
684 	unsigned long max_wait_time;
685 	unsigned long flags;
686 	uint32_t mbox_status;
687 
688 	ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
689 
690 	/*
691 	 * Start firmware from flash ROM
692 	 *
693 	 * WORKAROUND: Stuff a non-constant value that the firmware can
694 	 * use as a seed for a random number generator in MB7 prior to
695 	 * setting BOOT_ENABLE.	 Fixes problem where the TCP
696 	 * connections use the same TCP ports after each reboot,
697 	 * causing some connections to not get re-established.
698 	 */
699 	DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
700 		     ha->host_no, __func__));
701 
702 	spin_lock_irqsave(&ha->hardware_lock, flags);
703 	writel(jiffies, &ha->reg->mailbox[7]);
704 	if (is_qla4022(ha) | is_qla4032(ha))
705 		writel(set_rmask(NVR_WRITE_ENABLE),
706 		       &ha->reg->u1.isp4022.nvram);
707 
708         writel(2, &ha->reg->mailbox[6]);
709         readl(&ha->reg->mailbox[6]);
710 
711 	writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
712 	readl(&ha->reg->ctrl_status);
713 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
714 
715 	/* Wait for firmware to come UP. */
716 	DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
717 		      "boot firmware to complete...\n",
718 		      ha->host_no, __func__, FIRMWARE_UP_TOV));
719 	max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
720 	do {
721 		uint32_t ctrl_status;
722 
723 		spin_lock_irqsave(&ha->hardware_lock, flags);
724 		ctrl_status = readw(&ha->reg->ctrl_status);
725 		mbox_status = readw(&ha->reg->mailbox[0]);
726 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
727 
728 		if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
729 			break;
730 		if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
731 			break;
732 
733 		DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
734 		    "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
735 		    ha->host_no, __func__, ctrl_status, max_wait_time));
736 
737 		msleep_interruptible(250);
738 	} while (!time_after_eq(jiffies, max_wait_time));
739 
740 	if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
741 		DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
742 			     ha->host_no, __func__));
743 
744 		spin_lock_irqsave(&ha->hardware_lock, flags);
745 		writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
746 		       &ha->reg->ctrl_status);
747 		readl(&ha->reg->ctrl_status);
748 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
749 
750 		status = QLA_SUCCESS;
751 	} else {
752 		printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
753 		       "-  mbox status 0x%x\n", ha->host_no, __func__,
754 		       mbox_status);
755 		status = QLA_ERROR;
756 	}
757 	return status;
758 }
759 
760 int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
761 {
762 #define QL4_LOCK_DRVR_WAIT	60
763 #define QL4_LOCK_DRVR_SLEEP	1
764 
765 	int drvr_wait = QL4_LOCK_DRVR_WAIT;
766 	while (drvr_wait) {
767 		if (ql4xxx_lock_drvr(a) == 0) {
768 			ssleep(QL4_LOCK_DRVR_SLEEP);
769 			if (drvr_wait) {
770 				DEBUG2(printk("scsi%ld: %s: Waiting for "
771 					      "Global Init Semaphore(%d)...\n",
772 					      a->host_no,
773 					      __func__, drvr_wait));
774 			}
775 			drvr_wait -= QL4_LOCK_DRVR_SLEEP;
776 		} else {
777 			DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
778 				      "acquired\n", a->host_no, __func__));
779 			return QLA_SUCCESS;
780 		}
781 	}
782 	return QLA_ERROR;
783 }
784 
785 /**
786  * qla4xxx_start_firmware - starts qla4xxx firmware
787  * @ha: Pointer to host adapter structure.
788  *
789  * This routine performs the necessary steps to start the firmware for
790  * the QLA4010 adapter.
791  **/
792 int qla4xxx_start_firmware(struct scsi_qla_host *ha)
793 {
794 	unsigned long flags = 0;
795 	uint32_t mbox_status;
796 	int status = QLA_ERROR;
797 	int soft_reset = 1;
798 	int config_chip = 0;
799 
800 	if (is_qla4022(ha) | is_qla4032(ha))
801 		ql4xxx_set_mac_number(ha);
802 
803 	if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
804 		return QLA_ERROR;
805 
806 	spin_lock_irqsave(&ha->hardware_lock, flags);
807 
808 	DEBUG2(printk("scsi%ld: %s: port_ctrl	= 0x%08X\n", ha->host_no,
809 		      __func__, readw(isp_port_ctrl(ha))));
810 	DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
811 		     __func__, readw(isp_port_status(ha))));
812 
813 	/* Is Hardware already initialized? */
814 	if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
815 		DEBUG(printk("scsi%ld: %s: Hardware has already been "
816 			     "initialized\n", ha->host_no, __func__));
817 
818 		/* Receive firmware boot acknowledgement */
819 		mbox_status = readw(&ha->reg->mailbox[0]);
820 
821 		DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
822 			      "0x%x\n", ha->host_no, __func__, mbox_status));
823 
824 		/* Is firmware already booted? */
825 		if (mbox_status == 0) {
826 			/* F/W not running, must be config by net driver */
827 			config_chip = 1;
828 			soft_reset = 0;
829 		} else {
830 			writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
831 			       &ha->reg->ctrl_status);
832 			readl(&ha->reg->ctrl_status);
833 			writel(set_rmask(CSR_SCSI_COMPLETION_INTR),
834 			       &ha->reg->ctrl_status);
835 			readl(&ha->reg->ctrl_status);
836 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
837 			if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
838 				DEBUG2(printk("scsi%ld: %s: Get firmware "
839 					      "state -- state = 0x%x\n",
840 					      ha->host_no,
841 					      __func__, ha->firmware_state));
842 				/* F/W is running */
843 				if (ha->firmware_state &
844 				    FW_STATE_CONFIG_WAIT) {
845 					DEBUG2(printk("scsi%ld: %s: Firmware "
846 						      "in known state -- "
847 						      "config and "
848 						      "boot, state = 0x%x\n",
849 						      ha->host_no, __func__,
850 						      ha->firmware_state));
851 					config_chip = 1;
852 					soft_reset = 0;
853 				}
854 			} else {
855 				DEBUG2(printk("scsi%ld: %s: Firmware in "
856 					      "unknown state -- resetting,"
857 					      " state = "
858 					      "0x%x\n", ha->host_no, __func__,
859 					      ha->firmware_state));
860 			}
861 			spin_lock_irqsave(&ha->hardware_lock, flags);
862 		}
863 	} else {
864 		DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
865 			     "started - resetting\n", ha->host_no, __func__));
866 	}
867 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
868 
869 	DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
870 		     ha->host_no, __func__, soft_reset, config_chip));
871 	if (soft_reset) {
872 		DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
873 			     __func__));
874 		status = qla4xxx_soft_reset(ha);	/* NOTE: acquires drvr
875 							 * lock again, but ok */
876 		if (status == QLA_ERROR) {
877 			DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
878 				     ha->host_no, __func__));
879 			ql4xxx_unlock_drvr(ha);
880 			return QLA_ERROR;
881 		}
882 		config_chip = 1;
883 
884 		/* Reset clears the semaphore, so acquire again */
885 		if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
886 			return QLA_ERROR;
887 	}
888 
889 	if (config_chip) {
890 		if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
891 			status = qla4xxx_start_firmware_from_flash(ha);
892 	}
893 
894 	ql4xxx_unlock_drvr(ha);
895 	if (status == QLA_SUCCESS) {
896 		if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
897 			qla4xxx_get_crash_record(ha);
898 
899 		qla4xxx_init_rings(ha);
900 	} else {
901 		DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
902 			     ha->host_no, __func__));
903 	}
904 	return status;
905 }
906 /**
907  * qla4xxx_free_ddb_index - Free DDBs reserved by firmware
908  * @ha: pointer to adapter structure
909  *
910  * Since firmware is not running in autoconnect mode the DDB indices should
911  * be freed so that when login happens from user space there are free DDB
912  * indices available.
913  **/
914 void qla4xxx_free_ddb_index(struct scsi_qla_host *ha)
915 {
916 	int max_ddbs;
917 	int ret;
918 	uint32_t idx = 0, next_idx = 0;
919 	uint32_t state = 0, conn_err = 0;
920 
921 	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
922 				     MAX_DEV_DB_ENTRIES;
923 
924 	for (idx = 0; idx < max_ddbs; idx = next_idx) {
925 		ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
926 					      &next_idx, &state, &conn_err,
927 						NULL, NULL);
928 		if (ret == QLA_ERROR) {
929 			next_idx++;
930 			continue;
931 		}
932 		if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
933 		    state == DDB_DS_SESSION_FAILED) {
934 			DEBUG2(ql4_printk(KERN_INFO, ha,
935 					  "Freeing DDB index = 0x%x\n", idx));
936 			ret = qla4xxx_clear_ddb_entry(ha, idx);
937 			if (ret == QLA_ERROR)
938 				ql4_printk(KERN_ERR, ha,
939 					   "Unable to clear DDB index = "
940 					   "0x%x\n", idx);
941 		}
942 		if (next_idx == 0)
943 			break;
944 	}
945 }
946 
947 /**
948  * qla4xxx_initialize_adapter - initiailizes hba
949  * @ha: Pointer to host adapter structure.
950  *
951  * This routine parforms all of the steps necessary to initialize the adapter.
952  *
953  **/
954 int qla4xxx_initialize_adapter(struct scsi_qla_host *ha, int is_reset)
955 {
956 	int status = QLA_ERROR;
957 
958 	ha->eeprom_cmd_data = 0;
959 
960 	ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
961 	ha->isp_ops->pci_config(ha);
962 
963 	ha->isp_ops->disable_intrs(ha);
964 
965 	/* Initialize the Host adapter request/response queues and firmware */
966 	if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
967 		goto exit_init_hba;
968 
969 	/*
970 	 * For ISP83XX, mailbox and IOCB interrupts are enabled separately.
971 	 * Mailbox interrupts must be enabled prior to issuing any mailbox
972 	 * command in order to prevent the possibility of losing interrupts
973 	 * while switching from polling to interrupt mode. IOCB interrupts are
974 	 * enabled via isp_ops->enable_intrs.
975 	 */
976 	if (is_qla8032(ha) || is_qla8042(ha))
977 		qla4_83xx_enable_mbox_intrs(ha);
978 
979 	if (qla4xxx_about_firmware(ha) == QLA_ERROR)
980 		goto exit_init_hba;
981 
982 	if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
983 		goto exit_init_hba;
984 
985 	qla4xxx_init_local_data(ha);
986 
987 	status = qla4xxx_init_firmware(ha);
988 	if (status == QLA_ERROR)
989 		goto exit_init_hba;
990 
991 	if (is_reset == RESET_ADAPTER)
992 		qla4xxx_build_ddb_list(ha, is_reset);
993 
994 	set_bit(AF_ONLINE, &ha->flags);
995 
996 exit_init_hba:
997 	DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
998 	    status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
999 	return status;
1000 }
1001 
1002 int qla4xxx_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1003 		       struct ddb_entry *ddb_entry, uint32_t state)
1004 {
1005 	uint32_t old_fw_ddb_device_state;
1006 	int status = QLA_ERROR;
1007 
1008 	old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1009 	DEBUG2(ql4_printk(KERN_INFO, ha,
1010 			  "%s: DDB - old state = 0x%x, new state = 0x%x for "
1011 			  "index [%d]\n", __func__,
1012 			  ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1013 
1014 	ddb_entry->fw_ddb_device_state = state;
1015 
1016 	switch (old_fw_ddb_device_state) {
1017 	case DDB_DS_LOGIN_IN_PROCESS:
1018 		switch (state) {
1019 		case DDB_DS_SESSION_ACTIVE:
1020 		case DDB_DS_DISCOVERY:
1021 			qla4xxx_update_session_conn_param(ha, ddb_entry);
1022 			ddb_entry->unblock_sess(ddb_entry->sess);
1023 			status = QLA_SUCCESS;
1024 			break;
1025 		case DDB_DS_SESSION_FAILED:
1026 		case DDB_DS_NO_CONNECTION_ACTIVE:
1027 			iscsi_conn_login_event(ddb_entry->conn,
1028 					       ISCSI_CONN_STATE_FREE);
1029 			status = QLA_SUCCESS;
1030 			break;
1031 		}
1032 		break;
1033 	case DDB_DS_SESSION_ACTIVE:
1034 	case DDB_DS_DISCOVERY:
1035 		switch (state) {
1036 		case DDB_DS_SESSION_FAILED:
1037 			/*
1038 			 * iscsi_session failure  will cause userspace to
1039 			 * stop the connection which in turn would block the
1040 			 * iscsi_session and start relogin
1041 			 */
1042 			iscsi_session_failure(ddb_entry->sess->dd_data,
1043 					      ISCSI_ERR_CONN_FAILED);
1044 			status = QLA_SUCCESS;
1045 			break;
1046 		case DDB_DS_NO_CONNECTION_ACTIVE:
1047 			clear_bit(fw_ddb_index, ha->ddb_idx_map);
1048 			status = QLA_SUCCESS;
1049 			break;
1050 		}
1051 		break;
1052 	case DDB_DS_SESSION_FAILED:
1053 		switch (state) {
1054 		case DDB_DS_SESSION_ACTIVE:
1055 		case DDB_DS_DISCOVERY:
1056 			ddb_entry->unblock_sess(ddb_entry->sess);
1057 			qla4xxx_update_session_conn_param(ha, ddb_entry);
1058 			status = QLA_SUCCESS;
1059 			break;
1060 		case DDB_DS_SESSION_FAILED:
1061 			iscsi_session_failure(ddb_entry->sess->dd_data,
1062 					      ISCSI_ERR_CONN_FAILED);
1063 			status = QLA_SUCCESS;
1064 			break;
1065 		}
1066 		break;
1067 	default:
1068 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1069 				__func__));
1070 		break;
1071 	}
1072 	return status;
1073 }
1074 
1075 void qla4xxx_arm_relogin_timer(struct ddb_entry *ddb_entry)
1076 {
1077 	/*
1078 	 * This triggers a relogin.  After the relogin_timer
1079 	 * expires, the relogin gets scheduled.  We must wait a
1080 	 * minimum amount of time since receiving an 0x8014 AEN
1081 	 * with failed device_state or a logout response before
1082 	 * we can issue another relogin.
1083 	 *
1084 	 * Firmware pads this timeout: (time2wait +1).
1085 	 * Driver retry to login should be longer than F/W.
1086 	 * Otherwise F/W will fail
1087 	 * set_ddb() mbx cmd with 0x4005 since it still
1088 	 * counting down its time2wait.
1089 	 */
1090 	atomic_set(&ddb_entry->relogin_timer, 0);
1091 	atomic_set(&ddb_entry->retry_relogin_timer,
1092 		   ddb_entry->default_time2wait + 4);
1093 
1094 }
1095 
1096 int qla4xxx_flash_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
1097 			     struct ddb_entry *ddb_entry, uint32_t state)
1098 {
1099 	uint32_t old_fw_ddb_device_state;
1100 	int status = QLA_ERROR;
1101 
1102 	old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1103 	DEBUG2(ql4_printk(KERN_INFO, ha,
1104 			  "%s: DDB - old state = 0x%x, new state = 0x%x for "
1105 			  "index [%d]\n", __func__,
1106 			  ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1107 
1108 	ddb_entry->fw_ddb_device_state = state;
1109 
1110 	switch (old_fw_ddb_device_state) {
1111 	case DDB_DS_LOGIN_IN_PROCESS:
1112 	case DDB_DS_NO_CONNECTION_ACTIVE:
1113 		switch (state) {
1114 		case DDB_DS_SESSION_ACTIVE:
1115 			ddb_entry->unblock_sess(ddb_entry->sess);
1116 			qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1117 			status = QLA_SUCCESS;
1118 			break;
1119 		case DDB_DS_SESSION_FAILED:
1120 			iscsi_block_session(ddb_entry->sess);
1121 			if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1122 				qla4xxx_arm_relogin_timer(ddb_entry);
1123 			status = QLA_SUCCESS;
1124 			break;
1125 		}
1126 		break;
1127 	case DDB_DS_SESSION_ACTIVE:
1128 		switch (state) {
1129 		case DDB_DS_SESSION_FAILED:
1130 			iscsi_block_session(ddb_entry->sess);
1131 			if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1132 				qla4xxx_arm_relogin_timer(ddb_entry);
1133 			status = QLA_SUCCESS;
1134 			break;
1135 		}
1136 		break;
1137 	case DDB_DS_SESSION_FAILED:
1138 		switch (state) {
1139 		case DDB_DS_SESSION_ACTIVE:
1140 			ddb_entry->unblock_sess(ddb_entry->sess);
1141 			qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
1142 			status = QLA_SUCCESS;
1143 			break;
1144 		case DDB_DS_SESSION_FAILED:
1145 			if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
1146 				qla4xxx_arm_relogin_timer(ddb_entry);
1147 			status = QLA_SUCCESS;
1148 			break;
1149 		}
1150 		break;
1151 	default:
1152 		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
1153 				  __func__));
1154 		break;
1155 	}
1156 	return status;
1157 }
1158 
1159 /**
1160  * qla4xxx_process_ddb_changed - process ddb state change
1161  * @ha - Pointer to host adapter structure.
1162  * @fw_ddb_index - Firmware's device database index
1163  * @state - Device state
1164  *
1165  * This routine processes a Decive Database Changed AEN Event.
1166  **/
1167 int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
1168 				uint32_t fw_ddb_index,
1169 				uint32_t state, uint32_t conn_err)
1170 {
1171 	struct ddb_entry *ddb_entry;
1172 	int status = QLA_ERROR;
1173 
1174 	/* check for out of range index */
1175 	if (fw_ddb_index >= MAX_DDB_ENTRIES)
1176 		goto exit_ddb_event;
1177 
1178 	/* Get the corresponging ddb entry */
1179 	ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1180 	/* Device does not currently exist in our database. */
1181 	if (ddb_entry == NULL) {
1182 		ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
1183 			   __func__, fw_ddb_index);
1184 
1185 		if (state == DDB_DS_NO_CONNECTION_ACTIVE)
1186 			clear_bit(fw_ddb_index, ha->ddb_idx_map);
1187 
1188 		goto exit_ddb_event;
1189 	}
1190 
1191 	ddb_entry->ddb_change(ha, fw_ddb_index, ddb_entry, state);
1192 
1193 exit_ddb_event:
1194 	return status;
1195 }
1196 
1197 /**
1198  * qla4xxx_login_flash_ddb - Login to target (DDB)
1199  * @cls_session: Pointer to the session to login
1200  *
1201  * This routine logins to the target.
1202  * Issues setddb and conn open mbx
1203  **/
1204 void qla4xxx_login_flash_ddb(struct iscsi_cls_session *cls_session)
1205 {
1206 	struct iscsi_session *sess;
1207 	struct ddb_entry *ddb_entry;
1208 	struct scsi_qla_host *ha;
1209 	struct dev_db_entry *fw_ddb_entry = NULL;
1210 	dma_addr_t fw_ddb_dma;
1211 	uint32_t mbx_sts = 0;
1212 	int ret;
1213 
1214 	sess = cls_session->dd_data;
1215 	ddb_entry = sess->dd_data;
1216 	ha =  ddb_entry->ha;
1217 
1218 	if (!test_bit(AF_LINK_UP, &ha->flags))
1219 		return;
1220 
1221 	if (ddb_entry->ddb_type != FLASH_DDB) {
1222 		DEBUG2(ql4_printk(KERN_INFO, ha,
1223 				  "Skipping login to non FLASH DB"));
1224 		goto exit_login;
1225 	}
1226 
1227 	fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
1228 				      &fw_ddb_dma);
1229 	if (fw_ddb_entry == NULL) {
1230 		DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
1231 		goto exit_login;
1232 	}
1233 
1234 	if (ddb_entry->fw_ddb_index == INVALID_ENTRY) {
1235 		ret = qla4xxx_get_ddb_index(ha, &ddb_entry->fw_ddb_index);
1236 		if (ret == QLA_ERROR)
1237 			goto exit_login;
1238 
1239 		ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1240 		ha->tot_ddbs++;
1241 	}
1242 
1243 	memcpy(fw_ddb_entry, &ddb_entry->fw_ddb_entry,
1244 	       sizeof(struct dev_db_entry));
1245 	ddb_entry->sess->target_id = ddb_entry->fw_ddb_index;
1246 
1247 	ret = qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index,
1248 				    fw_ddb_dma, &mbx_sts);
1249 	if (ret == QLA_ERROR) {
1250 		DEBUG2(ql4_printk(KERN_ERR, ha, "Set DDB failed\n"));
1251 		goto exit_login;
1252 	}
1253 
1254 	ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1255 	ret = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1256 	if (ret == QLA_ERROR) {
1257 		ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1258 			   sess->targetname);
1259 		goto exit_login;
1260 	}
1261 
1262 exit_login:
1263 	if (fw_ddb_entry)
1264 		dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
1265 }
1266 
1267