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