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