18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * QLogic iSCSI HBA Driver
48c2ecf20Sopenharmony_ci * Copyright (c)  2003-2013 QLogic Corporation
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <scsi/iscsi_if.h>
88c2ecf20Sopenharmony_ci#include "ql4_def.h"
98c2ecf20Sopenharmony_ci#include "ql4_glbl.h"
108c2ecf20Sopenharmony_ci#include "ql4_dbg.h"
118c2ecf20Sopenharmony_ci#include "ql4_inline.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	uint32_t value;
168c2ecf20Sopenharmony_ci	unsigned long flags;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	/* Get the function number */
198c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
208c2ecf20Sopenharmony_ci	value = readw(&ha->reg->ctrl_status);
218c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	switch (value & ISP_CONTROL_FN_MASK) {
248c2ecf20Sopenharmony_ci	case ISP_CONTROL_FN0_SCSI:
258c2ecf20Sopenharmony_ci		ha->mac_index = 1;
268c2ecf20Sopenharmony_ci		break;
278c2ecf20Sopenharmony_ci	case ISP_CONTROL_FN1_SCSI:
288c2ecf20Sopenharmony_ci		ha->mac_index = 3;
298c2ecf20Sopenharmony_ci		break;
308c2ecf20Sopenharmony_ci	default:
318c2ecf20Sopenharmony_ci		DEBUG2(printk("scsi%ld: %s: Invalid function number, "
328c2ecf20Sopenharmony_ci			      "ispControlStatus = 0x%x\n", ha->host_no,
338c2ecf20Sopenharmony_ci			      __func__, value));
348c2ecf20Sopenharmony_ci		break;
358c2ecf20Sopenharmony_ci	}
368c2ecf20Sopenharmony_ci	DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
378c2ecf20Sopenharmony_ci		      ha->mac_index));
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/**
418c2ecf20Sopenharmony_ci * qla4xxx_free_ddb - deallocate ddb
428c2ecf20Sopenharmony_ci * @ha: pointer to host adapter structure.
438c2ecf20Sopenharmony_ci * @ddb_entry: pointer to device database entry
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci * This routine marks a DDB entry INVALID
468c2ecf20Sopenharmony_ci **/
478c2ecf20Sopenharmony_civoid qla4xxx_free_ddb(struct scsi_qla_host *ha,
488c2ecf20Sopenharmony_ci    struct ddb_entry *ddb_entry)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	/* Remove device pointer from index mapping arrays */
518c2ecf20Sopenharmony_ci	ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
528c2ecf20Sopenharmony_ci		(struct ddb_entry *) INVALID_ENTRY;
538c2ecf20Sopenharmony_ci	ha->tot_ddbs--;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/**
578c2ecf20Sopenharmony_ci * qla4xxx_init_response_q_entries() - Initializes response queue entries.
588c2ecf20Sopenharmony_ci * @ha: HA context
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * Beginning of request ring has initialization control block already built
618c2ecf20Sopenharmony_ci * by nvram config routine.
628c2ecf20Sopenharmony_ci **/
638c2ecf20Sopenharmony_cistatic void qla4xxx_init_response_q_entries(struct scsi_qla_host *ha)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	uint16_t cnt;
668c2ecf20Sopenharmony_ci	struct response *pkt;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	pkt = (struct response *)ha->response_ptr;
698c2ecf20Sopenharmony_ci	for (cnt = 0; cnt < RESPONSE_QUEUE_DEPTH; cnt++) {
708c2ecf20Sopenharmony_ci		pkt->signature = RESPONSE_PROCESSED;
718c2ecf20Sopenharmony_ci		pkt++;
728c2ecf20Sopenharmony_ci	}
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/**
768c2ecf20Sopenharmony_ci * qla4xxx_init_rings - initialize hw queues
778c2ecf20Sopenharmony_ci * @ha: pointer to host adapter structure.
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * This routine initializes the internal queues for the specified adapter.
808c2ecf20Sopenharmony_ci * The QLA4010 requires us to restart the queues at index 0.
818c2ecf20Sopenharmony_ci * The QLA4000 doesn't care, so just default to QLA4010's requirement.
828c2ecf20Sopenharmony_ci **/
838c2ecf20Sopenharmony_ciint qla4xxx_init_rings(struct scsi_qla_host *ha)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	unsigned long flags = 0;
868c2ecf20Sopenharmony_ci	int i;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/* Initialize request queue. */
898c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
908c2ecf20Sopenharmony_ci	ha->request_out = 0;
918c2ecf20Sopenharmony_ci	ha->request_in = 0;
928c2ecf20Sopenharmony_ci	ha->request_ptr = &ha->request_ring[ha->request_in];
938c2ecf20Sopenharmony_ci	ha->req_q_count = REQUEST_QUEUE_DEPTH;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/* Initialize response queue. */
968c2ecf20Sopenharmony_ci	ha->response_in = 0;
978c2ecf20Sopenharmony_ci	ha->response_out = 0;
988c2ecf20Sopenharmony_ci	ha->response_ptr = &ha->response_ring[ha->response_out];
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (is_qla8022(ha)) {
1018c2ecf20Sopenharmony_ci		writel(0,
1028c2ecf20Sopenharmony_ci		    (unsigned long  __iomem *)&ha->qla4_82xx_reg->req_q_out);
1038c2ecf20Sopenharmony_ci		writel(0,
1048c2ecf20Sopenharmony_ci		    (unsigned long  __iomem *)&ha->qla4_82xx_reg->rsp_q_in);
1058c2ecf20Sopenharmony_ci		writel(0,
1068c2ecf20Sopenharmony_ci		    (unsigned long  __iomem *)&ha->qla4_82xx_reg->rsp_q_out);
1078c2ecf20Sopenharmony_ci	} else if (is_qla8032(ha) || is_qla8042(ha)) {
1088c2ecf20Sopenharmony_ci		writel(0,
1098c2ecf20Sopenharmony_ci		       (unsigned long __iomem *)&ha->qla4_83xx_reg->req_q_in);
1108c2ecf20Sopenharmony_ci		writel(0,
1118c2ecf20Sopenharmony_ci		       (unsigned long __iomem *)&ha->qla4_83xx_reg->rsp_q_in);
1128c2ecf20Sopenharmony_ci		writel(0,
1138c2ecf20Sopenharmony_ci		       (unsigned long __iomem *)&ha->qla4_83xx_reg->rsp_q_out);
1148c2ecf20Sopenharmony_ci	} else {
1158c2ecf20Sopenharmony_ci		/*
1168c2ecf20Sopenharmony_ci		 * Initialize DMA Shadow registers.  The firmware is really
1178c2ecf20Sopenharmony_ci		 * supposed to take care of this, but on some uniprocessor
1188c2ecf20Sopenharmony_ci		 * systems, the shadow registers aren't cleared-- causing
1198c2ecf20Sopenharmony_ci		 * the interrupt_handler to think there are responses to be
1208c2ecf20Sopenharmony_ci		 * processed when there aren't.
1218c2ecf20Sopenharmony_ci		 */
1228c2ecf20Sopenharmony_ci		ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
1238c2ecf20Sopenharmony_ci		ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
1248c2ecf20Sopenharmony_ci		wmb();
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci		writel(0, &ha->reg->req_q_in);
1278c2ecf20Sopenharmony_ci		writel(0, &ha->reg->rsp_q_out);
1288c2ecf20Sopenharmony_ci		readl(&ha->reg->rsp_q_out);
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	qla4xxx_init_response_q_entries(ha);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* Initialize mailbox active array */
1348c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_MRB; i++)
1358c2ecf20Sopenharmony_ci		ha->active_mrb_array[i] = NULL;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/**
1438c2ecf20Sopenharmony_ci * qla4xxx_get_sys_info - validate adapter MAC address(es)
1448c2ecf20Sopenharmony_ci * @ha: pointer to host adapter structure.
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci **/
1478c2ecf20Sopenharmony_ciint qla4xxx_get_sys_info(struct scsi_qla_host *ha)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	struct flash_sys_info *sys_info;
1508c2ecf20Sopenharmony_ci	dma_addr_t sys_info_dma;
1518c2ecf20Sopenharmony_ci	int status = QLA_ERROR;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
1548c2ecf20Sopenharmony_ci				      &sys_info_dma, GFP_KERNEL);
1558c2ecf20Sopenharmony_ci	if (sys_info == NULL) {
1568c2ecf20Sopenharmony_ci		DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
1578c2ecf20Sopenharmony_ci			      ha->host_no, __func__));
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		goto exit_get_sys_info_no_free;
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	/* Get flash sys info */
1638c2ecf20Sopenharmony_ci	if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
1648c2ecf20Sopenharmony_ci			      sizeof(*sys_info)) != QLA_SUCCESS) {
1658c2ecf20Sopenharmony_ci		DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
1668c2ecf20Sopenharmony_ci			      "failed\n", ha->host_no, __func__));
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci		goto exit_get_sys_info;
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/* Save M.A.C. address & serial_number */
1728c2ecf20Sopenharmony_ci	memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
1738c2ecf20Sopenharmony_ci	       min(sizeof(ha->my_mac),
1748c2ecf20Sopenharmony_ci		   sizeof(sys_info->physAddr[0].address)));
1758c2ecf20Sopenharmony_ci	memcpy(ha->serial_number, &sys_info->acSerialNumber,
1768c2ecf20Sopenharmony_ci	       min(sizeof(ha->serial_number),
1778c2ecf20Sopenharmony_ci		   sizeof(sys_info->acSerialNumber)));
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	status = QLA_SUCCESS;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciexit_get_sys_info:
1828c2ecf20Sopenharmony_ci	dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
1838c2ecf20Sopenharmony_ci			  sys_info_dma);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciexit_get_sys_info_no_free:
1868c2ecf20Sopenharmony_ci	return status;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/**
1908c2ecf20Sopenharmony_ci * qla4xxx_init_local_data - initialize adapter specific local data
1918c2ecf20Sopenharmony_ci * @ha: pointer to host adapter structure.
1928c2ecf20Sopenharmony_ci *
1938c2ecf20Sopenharmony_ci **/
1948c2ecf20Sopenharmony_cistatic void qla4xxx_init_local_data(struct scsi_qla_host *ha)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	/* Initialize aen queue */
1978c2ecf20Sopenharmony_ci	ha->aen_q_count = MAX_AEN_ENTRIES;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic uint8_t
2018c2ecf20Sopenharmony_ciqla4xxx_wait_for_ip_config(struct scsi_qla_host *ha)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	uint8_t ipv4_wait = 0;
2048c2ecf20Sopenharmony_ci	uint8_t ipv6_wait = 0;
2058c2ecf20Sopenharmony_ci	int8_t ip_address[IPv6_ADDR_LEN] = {0} ;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/* If both IPv4 & IPv6 are enabled, possibly only one
2088c2ecf20Sopenharmony_ci	 * IP address may be acquired, so check to see if we
2098c2ecf20Sopenharmony_ci	 * need to wait for another */
2108c2ecf20Sopenharmony_ci	if (is_ipv4_enabled(ha) && is_ipv6_enabled(ha)) {
2118c2ecf20Sopenharmony_ci		if (((ha->addl_fw_state & FW_ADDSTATE_DHCPv4_ENABLED) != 0) &&
2128c2ecf20Sopenharmony_ci		    ((ha->addl_fw_state &
2138c2ecf20Sopenharmony_ci				    FW_ADDSTATE_DHCPv4_LEASE_ACQUIRED) == 0)) {
2148c2ecf20Sopenharmony_ci			ipv4_wait = 1;
2158c2ecf20Sopenharmony_ci		}
2168c2ecf20Sopenharmony_ci		if (((ha->ip_config.ipv6_addl_options &
2178c2ecf20Sopenharmony_ci		      IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) != 0) &&
2188c2ecf20Sopenharmony_ci		    ((ha->ip_config.ipv6_link_local_state ==
2198c2ecf20Sopenharmony_ci		      IP_ADDRSTATE_ACQUIRING) ||
2208c2ecf20Sopenharmony_ci		     (ha->ip_config.ipv6_addr0_state ==
2218c2ecf20Sopenharmony_ci		      IP_ADDRSTATE_ACQUIRING) ||
2228c2ecf20Sopenharmony_ci		     (ha->ip_config.ipv6_addr1_state ==
2238c2ecf20Sopenharmony_ci		      IP_ADDRSTATE_ACQUIRING))) {
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci			ipv6_wait = 1;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci			if ((ha->ip_config.ipv6_link_local_state ==
2288c2ecf20Sopenharmony_ci			     IP_ADDRSTATE_PREFERRED) ||
2298c2ecf20Sopenharmony_ci			    (ha->ip_config.ipv6_addr0_state ==
2308c2ecf20Sopenharmony_ci			     IP_ADDRSTATE_PREFERRED) ||
2318c2ecf20Sopenharmony_ci			    (ha->ip_config.ipv6_addr1_state ==
2328c2ecf20Sopenharmony_ci			     IP_ADDRSTATE_PREFERRED)) {
2338c2ecf20Sopenharmony_ci				DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
2348c2ecf20Sopenharmony_ci					      "Preferred IP configured."
2358c2ecf20Sopenharmony_ci					      " Don't wait!\n", ha->host_no,
2368c2ecf20Sopenharmony_ci					      __func__));
2378c2ecf20Sopenharmony_ci				ipv6_wait = 0;
2388c2ecf20Sopenharmony_ci			}
2398c2ecf20Sopenharmony_ci			if (memcmp(&ha->ip_config.ipv6_default_router_addr,
2408c2ecf20Sopenharmony_ci				   ip_address, IPv6_ADDR_LEN) == 0) {
2418c2ecf20Sopenharmony_ci				DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
2428c2ecf20Sopenharmony_ci					      "No Router configured. "
2438c2ecf20Sopenharmony_ci					      "Don't wait!\n", ha->host_no,
2448c2ecf20Sopenharmony_ci					      __func__));
2458c2ecf20Sopenharmony_ci				ipv6_wait = 0;
2468c2ecf20Sopenharmony_ci			}
2478c2ecf20Sopenharmony_ci			if ((ha->ip_config.ipv6_default_router_state ==
2488c2ecf20Sopenharmony_ci			     IPV6_RTRSTATE_MANUAL) &&
2498c2ecf20Sopenharmony_ci			    (ha->ip_config.ipv6_link_local_state ==
2508c2ecf20Sopenharmony_ci			     IP_ADDRSTATE_TENTATIVE) &&
2518c2ecf20Sopenharmony_ci			    (memcmp(&ha->ip_config.ipv6_link_local_addr,
2528c2ecf20Sopenharmony_ci			     &ha->ip_config.ipv6_default_router_addr, 4) ==
2538c2ecf20Sopenharmony_ci			     0)) {
2548c2ecf20Sopenharmony_ci				DEBUG2(printk("scsi%ld: %s: LinkLocal Router & "
2558c2ecf20Sopenharmony_ci					"IP configured. Don't wait!\n",
2568c2ecf20Sopenharmony_ci					ha->host_no, __func__));
2578c2ecf20Sopenharmony_ci				ipv6_wait = 0;
2588c2ecf20Sopenharmony_ci			}
2598c2ecf20Sopenharmony_ci		}
2608c2ecf20Sopenharmony_ci		if (ipv4_wait || ipv6_wait) {
2618c2ecf20Sopenharmony_ci			DEBUG2(printk("scsi%ld: %s: Wait for additional "
2628c2ecf20Sopenharmony_ci				      "IP(s) \"", ha->host_no, __func__));
2638c2ecf20Sopenharmony_ci			if (ipv4_wait)
2648c2ecf20Sopenharmony_ci				DEBUG2(printk("IPv4 "));
2658c2ecf20Sopenharmony_ci			if (ha->ip_config.ipv6_link_local_state ==
2668c2ecf20Sopenharmony_ci			    IP_ADDRSTATE_ACQUIRING)
2678c2ecf20Sopenharmony_ci				DEBUG2(printk("IPv6LinkLocal "));
2688c2ecf20Sopenharmony_ci			if (ha->ip_config.ipv6_addr0_state ==
2698c2ecf20Sopenharmony_ci			    IP_ADDRSTATE_ACQUIRING)
2708c2ecf20Sopenharmony_ci				DEBUG2(printk("IPv6Addr0 "));
2718c2ecf20Sopenharmony_ci			if (ha->ip_config.ipv6_addr1_state ==
2728c2ecf20Sopenharmony_ci			    IP_ADDRSTATE_ACQUIRING)
2738c2ecf20Sopenharmony_ci				DEBUG2(printk("IPv6Addr1 "));
2748c2ecf20Sopenharmony_ci			DEBUG2(printk("\"\n"));
2758c2ecf20Sopenharmony_ci		}
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	return ipv4_wait|ipv6_wait;
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic int qla4_80xx_is_minidump_dma_capable(struct scsi_qla_host *ha,
2828c2ecf20Sopenharmony_ci		struct qla4_8xxx_minidump_template_hdr *md_hdr)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	int offset = (is_qla8022(ha)) ? QLA8022_TEMPLATE_CAP_OFFSET :
2858c2ecf20Sopenharmony_ci					QLA83XX_TEMPLATE_CAP_OFFSET;
2868c2ecf20Sopenharmony_ci	int rval = 1;
2878c2ecf20Sopenharmony_ci	uint32_t *cap_offset;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	cap_offset = (uint32_t *)((char *)md_hdr + offset);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	if (!(le32_to_cpu(*cap_offset) & BIT_0)) {
2928c2ecf20Sopenharmony_ci		ql4_printk(KERN_INFO, ha, "PEX DMA Not supported %d\n",
2938c2ecf20Sopenharmony_ci			   *cap_offset);
2948c2ecf20Sopenharmony_ci		rval = 0;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return rval;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci/**
3018c2ecf20Sopenharmony_ci * qla4xxx_alloc_fw_dump - Allocate memory for minidump data.
3028c2ecf20Sopenharmony_ci * @ha: pointer to host adapter structure.
3038c2ecf20Sopenharmony_ci **/
3048c2ecf20Sopenharmony_civoid qla4xxx_alloc_fw_dump(struct scsi_qla_host *ha)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	int status;
3078c2ecf20Sopenharmony_ci	uint32_t capture_debug_level;
3088c2ecf20Sopenharmony_ci	int hdr_entry_bit, k;
3098c2ecf20Sopenharmony_ci	void *md_tmp;
3108c2ecf20Sopenharmony_ci	dma_addr_t md_tmp_dma;
3118c2ecf20Sopenharmony_ci	struct qla4_8xxx_minidump_template_hdr *md_hdr;
3128c2ecf20Sopenharmony_ci	int dma_capable;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	if (ha->fw_dump) {
3158c2ecf20Sopenharmony_ci		ql4_printk(KERN_WARNING, ha,
3168c2ecf20Sopenharmony_ci			   "Firmware dump previously allocated.\n");
3178c2ecf20Sopenharmony_ci		return;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	status = qla4xxx_req_template_size(ha);
3218c2ecf20Sopenharmony_ci	if (status != QLA_SUCCESS) {
3228c2ecf20Sopenharmony_ci		ql4_printk(KERN_INFO, ha,
3238c2ecf20Sopenharmony_ci			   "scsi%ld: Failed to get template size\n",
3248c2ecf20Sopenharmony_ci			   ha->host_no);
3258c2ecf20Sopenharmony_ci		return;
3268c2ecf20Sopenharmony_ci	}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	clear_bit(AF_82XX_FW_DUMPED, &ha->flags);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	/* Allocate memory for saving the template */
3318c2ecf20Sopenharmony_ci	md_tmp = dma_alloc_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
3328c2ecf20Sopenharmony_ci				    &md_tmp_dma, GFP_KERNEL);
3338c2ecf20Sopenharmony_ci	if (!md_tmp) {
3348c2ecf20Sopenharmony_ci		ql4_printk(KERN_INFO, ha,
3358c2ecf20Sopenharmony_ci			   "scsi%ld: Failed to allocate DMA memory\n",
3368c2ecf20Sopenharmony_ci			   ha->host_no);
3378c2ecf20Sopenharmony_ci		return;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	/* Request template */
3418c2ecf20Sopenharmony_ci	status =  qla4xxx_get_minidump_template(ha, md_tmp_dma);
3428c2ecf20Sopenharmony_ci	if (status != QLA_SUCCESS) {
3438c2ecf20Sopenharmony_ci		ql4_printk(KERN_INFO, ha,
3448c2ecf20Sopenharmony_ci			   "scsi%ld: Failed to get minidump template\n",
3458c2ecf20Sopenharmony_ci			   ha->host_no);
3468c2ecf20Sopenharmony_ci		goto alloc_cleanup;
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	md_hdr = (struct qla4_8xxx_minidump_template_hdr *)md_tmp;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	dma_capable = qla4_80xx_is_minidump_dma_capable(ha, md_hdr);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	capture_debug_level = md_hdr->capture_debug_level;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	/* Get capture mask based on module loadtime setting. */
3568c2ecf20Sopenharmony_ci	if ((ql4xmdcapmask >= 0x3 && ql4xmdcapmask <= 0x7F) ||
3578c2ecf20Sopenharmony_ci	    (ql4xmdcapmask == 0xFF && dma_capable))  {
3588c2ecf20Sopenharmony_ci		ha->fw_dump_capture_mask = ql4xmdcapmask;
3598c2ecf20Sopenharmony_ci	} else {
3608c2ecf20Sopenharmony_ci		if (ql4xmdcapmask == 0xFF)
3618c2ecf20Sopenharmony_ci			ql4_printk(KERN_INFO, ha, "Falling back to default capture mask, as PEX DMA is not supported\n");
3628c2ecf20Sopenharmony_ci		ha->fw_dump_capture_mask = capture_debug_level;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	md_hdr->driver_capture_mask = ha->fw_dump_capture_mask;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	DEBUG2(ql4_printk(KERN_INFO, ha, "Minimum num of entries = %d\n",
3688c2ecf20Sopenharmony_ci			  md_hdr->num_of_entries));
3698c2ecf20Sopenharmony_ci	DEBUG2(ql4_printk(KERN_INFO, ha, "Dump template size  = %d\n",
3708c2ecf20Sopenharmony_ci			  ha->fw_dump_tmplt_size));
3718c2ecf20Sopenharmony_ci	DEBUG2(ql4_printk(KERN_INFO, ha, "Selected Capture mask =0x%x\n",
3728c2ecf20Sopenharmony_ci			  ha->fw_dump_capture_mask));
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	/* Calculate fw_dump_size */
3758c2ecf20Sopenharmony_ci	for (hdr_entry_bit = 0x2, k = 1; (hdr_entry_bit & 0xFF);
3768c2ecf20Sopenharmony_ci	     hdr_entry_bit <<= 1, k++) {
3778c2ecf20Sopenharmony_ci		if (hdr_entry_bit & ha->fw_dump_capture_mask)
3788c2ecf20Sopenharmony_ci			ha->fw_dump_size += md_hdr->capture_size_array[k];
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	/* Total firmware dump size including command header */
3828c2ecf20Sopenharmony_ci	ha->fw_dump_size += ha->fw_dump_tmplt_size;
3838c2ecf20Sopenharmony_ci	ha->fw_dump = vmalloc(ha->fw_dump_size);
3848c2ecf20Sopenharmony_ci	if (!ha->fw_dump)
3858c2ecf20Sopenharmony_ci		goto alloc_cleanup;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	DEBUG2(ql4_printk(KERN_INFO, ha,
3888c2ecf20Sopenharmony_ci			  "Minidump Template Size = 0x%x KB\n",
3898c2ecf20Sopenharmony_ci			  ha->fw_dump_tmplt_size));
3908c2ecf20Sopenharmony_ci	DEBUG2(ql4_printk(KERN_INFO, ha,
3918c2ecf20Sopenharmony_ci			  "Total Minidump size = 0x%x KB\n", ha->fw_dump_size));
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	memcpy(ha->fw_dump, md_tmp, ha->fw_dump_tmplt_size);
3948c2ecf20Sopenharmony_ci	ha->fw_dump_tmplt_hdr = ha->fw_dump;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cialloc_cleanup:
3978c2ecf20Sopenharmony_ci	dma_free_coherent(&ha->pdev->dev, ha->fw_dump_tmplt_size,
3988c2ecf20Sopenharmony_ci			  md_tmp, md_tmp_dma);
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic int qla4xxx_fw_ready(struct scsi_qla_host *ha)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	uint32_t timeout_count;
4048c2ecf20Sopenharmony_ci	int ready = 0;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	DEBUG2(ql4_printk(KERN_INFO, ha, "Waiting for Firmware Ready..\n"));
4078c2ecf20Sopenharmony_ci	for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
4088c2ecf20Sopenharmony_ci	     timeout_count--) {
4098c2ecf20Sopenharmony_ci		if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
4108c2ecf20Sopenharmony_ci			qla4xxx_get_dhcp_ip_address(ha);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci		/* Get firmware state. */
4138c2ecf20Sopenharmony_ci		if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
4148c2ecf20Sopenharmony_ci			DEBUG2(printk("scsi%ld: %s: unable to get firmware "
4158c2ecf20Sopenharmony_ci				      "state\n", ha->host_no, __func__));
4168c2ecf20Sopenharmony_ci			break;
4178c2ecf20Sopenharmony_ci		}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci		if (ha->firmware_state & FW_STATE_ERROR) {
4208c2ecf20Sopenharmony_ci			DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
4218c2ecf20Sopenharmony_ci				      " occurred\n", ha->host_no, __func__));
4228c2ecf20Sopenharmony_ci			break;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci		}
4258c2ecf20Sopenharmony_ci		if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
4268c2ecf20Sopenharmony_ci			/*
4278c2ecf20Sopenharmony_ci			 * The firmware has not yet been issued an Initialize
4288c2ecf20Sopenharmony_ci			 * Firmware command, so issue it now.
4298c2ecf20Sopenharmony_ci			 */
4308c2ecf20Sopenharmony_ci			if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
4318c2ecf20Sopenharmony_ci				break;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci			/* Go back and test for ready state - no wait. */
4348c2ecf20Sopenharmony_ci			continue;
4358c2ecf20Sopenharmony_ci		}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci		if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
4388c2ecf20Sopenharmony_ci			DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
4398c2ecf20Sopenharmony_ci				      "AUTOCONNECT in progress\n",
4408c2ecf20Sopenharmony_ci				      ha->host_no, __func__));
4418c2ecf20Sopenharmony_ci		}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci		if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
4448c2ecf20Sopenharmony_ci			DEBUG2(printk(KERN_INFO "scsi%ld: %s: fwstate:"
4458c2ecf20Sopenharmony_ci				      " CONFIGURING IP\n",
4468c2ecf20Sopenharmony_ci				      ha->host_no, __func__));
4478c2ecf20Sopenharmony_ci			/*
4488c2ecf20Sopenharmony_ci			 * Check for link state after 15 secs and if link is
4498c2ecf20Sopenharmony_ci			 * still DOWN then, cable is unplugged. Ignore "DHCP
4508c2ecf20Sopenharmony_ci			 * in Progress/CONFIGURING IP" bit to check if firmware
4518c2ecf20Sopenharmony_ci			 * is in ready state or not after 15 secs.
4528c2ecf20Sopenharmony_ci			 * This is applicable for both 2.x & 3.x firmware
4538c2ecf20Sopenharmony_ci			 */
4548c2ecf20Sopenharmony_ci			if (timeout_count <= (ADAPTER_INIT_TOV - 15)) {
4558c2ecf20Sopenharmony_ci				if (ha->addl_fw_state & FW_ADDSTATE_LINK_UP) {
4568c2ecf20Sopenharmony_ci					DEBUG2(printk(KERN_INFO "scsi%ld: %s:"
4578c2ecf20Sopenharmony_ci						  " LINK UP (Cable plugged)\n",
4588c2ecf20Sopenharmony_ci						  ha->host_no, __func__));
4598c2ecf20Sopenharmony_ci				} else if (ha->firmware_state &
4608c2ecf20Sopenharmony_ci					  (FW_STATE_CONFIGURING_IP |
4618c2ecf20Sopenharmony_ci							     FW_STATE_READY)) {
4628c2ecf20Sopenharmony_ci					DEBUG2(printk(KERN_INFO "scsi%ld: %s: "
4638c2ecf20Sopenharmony_ci						"LINK DOWN (Cable unplugged)\n",
4648c2ecf20Sopenharmony_ci						ha->host_no, __func__));
4658c2ecf20Sopenharmony_ci					ha->firmware_state = FW_STATE_READY;
4668c2ecf20Sopenharmony_ci				}
4678c2ecf20Sopenharmony_ci			}
4688c2ecf20Sopenharmony_ci		}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci		if (ha->firmware_state == FW_STATE_READY) {
4718c2ecf20Sopenharmony_ci			/* If DHCP IP Addr is available, retrieve it now. */
4728c2ecf20Sopenharmony_ci			if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR,
4738c2ecf20Sopenharmony_ci								&ha->dpc_flags))
4748c2ecf20Sopenharmony_ci				qla4xxx_get_dhcp_ip_address(ha);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci			if (!qla4xxx_wait_for_ip_config(ha) ||
4778c2ecf20Sopenharmony_ci							timeout_count == 1) {
4788c2ecf20Sopenharmony_ci				DEBUG2(ql4_printk(KERN_INFO, ha,
4798c2ecf20Sopenharmony_ci				    "Firmware Ready..\n"));
4808c2ecf20Sopenharmony_ci				/* The firmware is ready to process SCSI
4818c2ecf20Sopenharmony_ci				   commands. */
4828c2ecf20Sopenharmony_ci				DEBUG2(ql4_printk(KERN_INFO, ha,
4838c2ecf20Sopenharmony_ci					"scsi%ld: %s: MEDIA TYPE"
4848c2ecf20Sopenharmony_ci					" - %s\n", ha->host_no,
4858c2ecf20Sopenharmony_ci					__func__, (ha->addl_fw_state &
4868c2ecf20Sopenharmony_ci					FW_ADDSTATE_OPTICAL_MEDIA)
4878c2ecf20Sopenharmony_ci					!= 0 ? "OPTICAL" : "COPPER"));
4888c2ecf20Sopenharmony_ci				DEBUG2(ql4_printk(KERN_INFO, ha,
4898c2ecf20Sopenharmony_ci					"scsi%ld: %s: DHCPv4 STATE"
4908c2ecf20Sopenharmony_ci					" Enabled %s\n", ha->host_no,
4918c2ecf20Sopenharmony_ci					 __func__, (ha->addl_fw_state &
4928c2ecf20Sopenharmony_ci					 FW_ADDSTATE_DHCPv4_ENABLED) != 0 ?
4938c2ecf20Sopenharmony_ci					"YES" : "NO"));
4948c2ecf20Sopenharmony_ci				DEBUG2(ql4_printk(KERN_INFO, ha,
4958c2ecf20Sopenharmony_ci					"scsi%ld: %s: LINK %s\n",
4968c2ecf20Sopenharmony_ci					ha->host_no, __func__,
4978c2ecf20Sopenharmony_ci					(ha->addl_fw_state &
4988c2ecf20Sopenharmony_ci					 FW_ADDSTATE_LINK_UP) != 0 ?
4998c2ecf20Sopenharmony_ci					"UP" : "DOWN"));
5008c2ecf20Sopenharmony_ci				DEBUG2(ql4_printk(KERN_INFO, ha,
5018c2ecf20Sopenharmony_ci					"scsi%ld: %s: iSNS Service "
5028c2ecf20Sopenharmony_ci					"Started %s\n",
5038c2ecf20Sopenharmony_ci					ha->host_no, __func__,
5048c2ecf20Sopenharmony_ci					(ha->addl_fw_state &
5058c2ecf20Sopenharmony_ci					 FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
5068c2ecf20Sopenharmony_ci					"YES" : "NO"));
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci				ready = 1;
5098c2ecf20Sopenharmony_ci				break;
5108c2ecf20Sopenharmony_ci			}
5118c2ecf20Sopenharmony_ci		}
5128c2ecf20Sopenharmony_ci		DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
5138c2ecf20Sopenharmony_ci			      "seconds expired= %d\n", ha->host_no, __func__,
5148c2ecf20Sopenharmony_ci			      ha->firmware_state, ha->addl_fw_state,
5158c2ecf20Sopenharmony_ci			      timeout_count));
5168c2ecf20Sopenharmony_ci		if (is_qla4032(ha) &&
5178c2ecf20Sopenharmony_ci			!(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
5188c2ecf20Sopenharmony_ci			(timeout_count < ADAPTER_INIT_TOV - 5)) {
5198c2ecf20Sopenharmony_ci			break;
5208c2ecf20Sopenharmony_ci		}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci		msleep(1000);
5238c2ecf20Sopenharmony_ci	}			/* end of for */
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	if (timeout_count <= 0)
5268c2ecf20Sopenharmony_ci		DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
5278c2ecf20Sopenharmony_ci			      ha->host_no, __func__));
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	if (ha->firmware_state & FW_STATE_CONFIGURING_IP) {
5308c2ecf20Sopenharmony_ci		DEBUG2(printk("scsi%ld: %s: FW initialized, but is reporting "
5318c2ecf20Sopenharmony_ci			      "it's waiting to configure an IP address\n",
5328c2ecf20Sopenharmony_ci			       ha->host_no, __func__));
5338c2ecf20Sopenharmony_ci		ready = 1;
5348c2ecf20Sopenharmony_ci	} else if (ha->firmware_state & FW_STATE_WAIT_AUTOCONNECT) {
5358c2ecf20Sopenharmony_ci		DEBUG2(printk("scsi%ld: %s: FW initialized, but "
5368c2ecf20Sopenharmony_ci			      "auto-discovery still in process\n",
5378c2ecf20Sopenharmony_ci			       ha->host_no, __func__));
5388c2ecf20Sopenharmony_ci		ready = 1;
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	return ready;
5428c2ecf20Sopenharmony_ci}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci/**
5458c2ecf20Sopenharmony_ci * qla4xxx_init_firmware - initializes the firmware.
5468c2ecf20Sopenharmony_ci * @ha: pointer to host adapter structure.
5478c2ecf20Sopenharmony_ci *
5488c2ecf20Sopenharmony_ci **/
5498c2ecf20Sopenharmony_cistatic int qla4xxx_init_firmware(struct scsi_qla_host *ha)
5508c2ecf20Sopenharmony_ci{
5518c2ecf20Sopenharmony_ci	int status = QLA_ERROR;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	if (is_aer_supported(ha) &&
5548c2ecf20Sopenharmony_ci	    test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
5558c2ecf20Sopenharmony_ci		return status;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	/* For 82xx, stop firmware before initializing because if BIOS
5588c2ecf20Sopenharmony_ci	 * has previously initialized firmware, then driver's initialize
5598c2ecf20Sopenharmony_ci	 * firmware will fail. */
5608c2ecf20Sopenharmony_ci	if (is_qla80XX(ha))
5618c2ecf20Sopenharmony_ci		qla4_8xxx_stop_firmware(ha);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	ql4_printk(KERN_INFO, ha, "Initializing firmware..\n");
5648c2ecf20Sopenharmony_ci	if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
5658c2ecf20Sopenharmony_ci		DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
5668c2ecf20Sopenharmony_ci			      "control block\n", ha->host_no, __func__));
5678c2ecf20Sopenharmony_ci		return status;
5688c2ecf20Sopenharmony_ci	}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	if (!qla4xxx_fw_ready(ha))
5718c2ecf20Sopenharmony_ci		return status;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	if (is_qla80XX(ha) && !test_bit(AF_INIT_DONE, &ha->flags))
5748c2ecf20Sopenharmony_ci		qla4xxx_alloc_fw_dump(ha);
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	return qla4xxx_get_firmware_status(ha);
5778c2ecf20Sopenharmony_ci}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cistatic void qla4xxx_set_model_info(struct scsi_qla_host *ha)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	uint16_t board_id_string[8];
5828c2ecf20Sopenharmony_ci	int i;
5838c2ecf20Sopenharmony_ci	int size = sizeof(ha->nvram->isp4022.boardIdStr);
5848c2ecf20Sopenharmony_ci	int offset = offsetof(struct eeprom_data, isp4022.boardIdStr) / 2;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	for (i = 0; i < (size / 2) ; i++) {
5878c2ecf20Sopenharmony_ci		board_id_string[i] = rd_nvram_word(ha, offset);
5888c2ecf20Sopenharmony_ci		offset += 1;
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	memcpy(ha->model_name, board_id_string, size);
5928c2ecf20Sopenharmony_ci}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_cistatic int qla4xxx_config_nvram(struct scsi_qla_host *ha)
5958c2ecf20Sopenharmony_ci{
5968c2ecf20Sopenharmony_ci	unsigned long flags;
5978c2ecf20Sopenharmony_ci	union external_hw_config_reg extHwConfig;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
6008c2ecf20Sopenharmony_ci		      __func__));
6018c2ecf20Sopenharmony_ci	if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
6028c2ecf20Sopenharmony_ci		return QLA_ERROR;
6038c2ecf20Sopenharmony_ci	if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
6048c2ecf20Sopenharmony_ci		ql4xxx_unlock_flash(ha);
6058c2ecf20Sopenharmony_ci		return QLA_ERROR;
6068c2ecf20Sopenharmony_ci	}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	/* Get EEPRom Parameters from NVRAM and validate */
6098c2ecf20Sopenharmony_ci	ql4_printk(KERN_INFO, ha, "Configuring NVRAM ...\n");
6108c2ecf20Sopenharmony_ci	if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
6118c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->hardware_lock, flags);
6128c2ecf20Sopenharmony_ci		extHwConfig.Asuint32_t =
6138c2ecf20Sopenharmony_ci			rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
6148c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->hardware_lock, flags);
6158c2ecf20Sopenharmony_ci	} else {
6168c2ecf20Sopenharmony_ci		ql4_printk(KERN_WARNING, ha,
6178c2ecf20Sopenharmony_ci		    "scsi%ld: %s: EEProm checksum invalid.  "
6188c2ecf20Sopenharmony_ci		    "Please update your EEPROM\n", ha->host_no,
6198c2ecf20Sopenharmony_ci		    __func__);
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci		/* Attempt to set defaults */
6228c2ecf20Sopenharmony_ci		if (is_qla4010(ha))
6238c2ecf20Sopenharmony_ci			extHwConfig.Asuint32_t = 0x1912;
6248c2ecf20Sopenharmony_ci		else if (is_qla4022(ha) | is_qla4032(ha))
6258c2ecf20Sopenharmony_ci			extHwConfig.Asuint32_t = 0x0023;
6268c2ecf20Sopenharmony_ci		else
6278c2ecf20Sopenharmony_ci			return QLA_ERROR;
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	if (is_qla4022(ha) || is_qla4032(ha))
6318c2ecf20Sopenharmony_ci		qla4xxx_set_model_info(ha);
6328c2ecf20Sopenharmony_ci	else
6338c2ecf20Sopenharmony_ci		strcpy(ha->model_name, "QLA4010");
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
6368c2ecf20Sopenharmony_ci		     ha->host_no, __func__, extHwConfig.Asuint32_t));
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
6398c2ecf20Sopenharmony_ci	writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
6408c2ecf20Sopenharmony_ci	readl(isp_ext_hw_conf(ha));
6418c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	ql4xxx_unlock_nvram(ha);
6448c2ecf20Sopenharmony_ci	ql4xxx_unlock_flash(ha);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	return QLA_SUCCESS;
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci/**
6508c2ecf20Sopenharmony_ci * qla4_8xxx_pci_config() - Setup ISP82xx PCI configuration registers.
6518c2ecf20Sopenharmony_ci * @ha: HA context
6528c2ecf20Sopenharmony_ci */
6538c2ecf20Sopenharmony_civoid qla4_8xxx_pci_config(struct scsi_qla_host *ha)
6548c2ecf20Sopenharmony_ci{
6558c2ecf20Sopenharmony_ci	pci_set_master(ha->pdev);
6568c2ecf20Sopenharmony_ci}
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_civoid qla4xxx_pci_config(struct scsi_qla_host *ha)
6598c2ecf20Sopenharmony_ci{
6608c2ecf20Sopenharmony_ci	uint16_t w;
6618c2ecf20Sopenharmony_ci	int status;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	pci_set_master(ha->pdev);
6668c2ecf20Sopenharmony_ci	status = pci_set_mwi(ha->pdev);
6678c2ecf20Sopenharmony_ci	if (status)
6688c2ecf20Sopenharmony_ci		ql4_printk(KERN_WARNING, ha, "Failed to set MWI\n");
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	/*
6718c2ecf20Sopenharmony_ci	 * We want to respect framework's setting of PCI configuration space
6728c2ecf20Sopenharmony_ci	 * command register and also want to make sure that all bits of
6738c2ecf20Sopenharmony_ci	 * interest to us are properly set in command register.
6748c2ecf20Sopenharmony_ci	 */
6758c2ecf20Sopenharmony_ci	pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
6768c2ecf20Sopenharmony_ci	w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
6778c2ecf20Sopenharmony_ci	w &= ~PCI_COMMAND_INTX_DISABLE;
6788c2ecf20Sopenharmony_ci	pci_write_config_word(ha->pdev, PCI_COMMAND, w);
6798c2ecf20Sopenharmony_ci}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_cistatic int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	int status = QLA_ERROR;
6848c2ecf20Sopenharmony_ci	unsigned long max_wait_time;
6858c2ecf20Sopenharmony_ci	unsigned long flags;
6868c2ecf20Sopenharmony_ci	uint32_t mbox_status;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	ql4_printk(KERN_INFO, ha, "Starting firmware ...\n");
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	/*
6918c2ecf20Sopenharmony_ci	 * Start firmware from flash ROM
6928c2ecf20Sopenharmony_ci	 *
6938c2ecf20Sopenharmony_ci	 * WORKAROUND: Stuff a non-constant value that the firmware can
6948c2ecf20Sopenharmony_ci	 * use as a seed for a random number generator in MB7 prior to
6958c2ecf20Sopenharmony_ci	 * setting BOOT_ENABLE.	 Fixes problem where the TCP
6968c2ecf20Sopenharmony_ci	 * connections use the same TCP ports after each reboot,
6978c2ecf20Sopenharmony_ci	 * causing some connections to not get re-established.
6988c2ecf20Sopenharmony_ci	 */
6998c2ecf20Sopenharmony_ci	DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
7008c2ecf20Sopenharmony_ci		     ha->host_no, __func__));
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
7038c2ecf20Sopenharmony_ci	writel(jiffies, &ha->reg->mailbox[7]);
7048c2ecf20Sopenharmony_ci	if (is_qla4022(ha) | is_qla4032(ha))
7058c2ecf20Sopenharmony_ci		writel(set_rmask(NVR_WRITE_ENABLE),
7068c2ecf20Sopenharmony_ci		       &ha->reg->u1.isp4022.nvram);
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci        writel(2, &ha->reg->mailbox[6]);
7098c2ecf20Sopenharmony_ci        readl(&ha->reg->mailbox[6]);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
7128c2ecf20Sopenharmony_ci	readl(&ha->reg->ctrl_status);
7138c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	/* Wait for firmware to come UP. */
7168c2ecf20Sopenharmony_ci	DEBUG2(printk(KERN_INFO "scsi%ld: %s: Wait up to %d seconds for "
7178c2ecf20Sopenharmony_ci		      "boot firmware to complete...\n",
7188c2ecf20Sopenharmony_ci		      ha->host_no, __func__, FIRMWARE_UP_TOV));
7198c2ecf20Sopenharmony_ci	max_wait_time = jiffies + (FIRMWARE_UP_TOV * HZ);
7208c2ecf20Sopenharmony_ci	do {
7218c2ecf20Sopenharmony_ci		uint32_t ctrl_status;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->hardware_lock, flags);
7248c2ecf20Sopenharmony_ci		ctrl_status = readw(&ha->reg->ctrl_status);
7258c2ecf20Sopenharmony_ci		mbox_status = readw(&ha->reg->mailbox[0]);
7268c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->hardware_lock, flags);
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci		if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
7298c2ecf20Sopenharmony_ci			break;
7308c2ecf20Sopenharmony_ci		if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
7318c2ecf20Sopenharmony_ci			break;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		DEBUG2(printk(KERN_INFO "scsi%ld: %s: Waiting for boot "
7348c2ecf20Sopenharmony_ci		    "firmware to complete... ctrl_sts=0x%x, remaining=%ld\n",
7358c2ecf20Sopenharmony_ci		    ha->host_no, __func__, ctrl_status, max_wait_time));
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci		msleep_interruptible(250);
7388c2ecf20Sopenharmony_ci	} while (!time_after_eq(jiffies, max_wait_time));
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
7418c2ecf20Sopenharmony_ci		DEBUG(printk(KERN_INFO "scsi%ld: %s: Firmware has started\n",
7428c2ecf20Sopenharmony_ci			     ha->host_no, __func__));
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ha->hardware_lock, flags);
7458c2ecf20Sopenharmony_ci		writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
7468c2ecf20Sopenharmony_ci		       &ha->reg->ctrl_status);
7478c2ecf20Sopenharmony_ci		readl(&ha->reg->ctrl_status);
7488c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ha->hardware_lock, flags);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci		status = QLA_SUCCESS;
7518c2ecf20Sopenharmony_ci	} else {
7528c2ecf20Sopenharmony_ci		printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
7538c2ecf20Sopenharmony_ci		       "-  mbox status 0x%x\n", ha->host_no, __func__,
7548c2ecf20Sopenharmony_ci		       mbox_status);
7558c2ecf20Sopenharmony_ci		status = QLA_ERROR;
7568c2ecf20Sopenharmony_ci	}
7578c2ecf20Sopenharmony_ci	return status;
7588c2ecf20Sopenharmony_ci}
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ciint ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
7618c2ecf20Sopenharmony_ci{
7628c2ecf20Sopenharmony_ci#define QL4_LOCK_DRVR_WAIT	60
7638c2ecf20Sopenharmony_ci#define QL4_LOCK_DRVR_SLEEP	1
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	int drvr_wait = QL4_LOCK_DRVR_WAIT;
7668c2ecf20Sopenharmony_ci	while (drvr_wait) {
7678c2ecf20Sopenharmony_ci		if (ql4xxx_lock_drvr(a) == 0) {
7688c2ecf20Sopenharmony_ci			ssleep(QL4_LOCK_DRVR_SLEEP);
7698c2ecf20Sopenharmony_ci			DEBUG2(printk("scsi%ld: %s: Waiting for "
7708c2ecf20Sopenharmony_ci				      "Global Init Semaphore(%d)...\n",
7718c2ecf20Sopenharmony_ci				      a->host_no,
7728c2ecf20Sopenharmony_ci				      __func__, drvr_wait));
7738c2ecf20Sopenharmony_ci			drvr_wait -= QL4_LOCK_DRVR_SLEEP;
7748c2ecf20Sopenharmony_ci		} else {
7758c2ecf20Sopenharmony_ci			DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
7768c2ecf20Sopenharmony_ci				      "acquired\n", a->host_no, __func__));
7778c2ecf20Sopenharmony_ci			return QLA_SUCCESS;
7788c2ecf20Sopenharmony_ci		}
7798c2ecf20Sopenharmony_ci	}
7808c2ecf20Sopenharmony_ci	return QLA_ERROR;
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci/**
7848c2ecf20Sopenharmony_ci * qla4xxx_start_firmware - starts qla4xxx firmware
7858c2ecf20Sopenharmony_ci * @ha: Pointer to host adapter structure.
7868c2ecf20Sopenharmony_ci *
7878c2ecf20Sopenharmony_ci * This routine performs the necessary steps to start the firmware for
7888c2ecf20Sopenharmony_ci * the QLA4010 adapter.
7898c2ecf20Sopenharmony_ci **/
7908c2ecf20Sopenharmony_ciint qla4xxx_start_firmware(struct scsi_qla_host *ha)
7918c2ecf20Sopenharmony_ci{
7928c2ecf20Sopenharmony_ci	unsigned long flags = 0;
7938c2ecf20Sopenharmony_ci	uint32_t mbox_status;
7948c2ecf20Sopenharmony_ci	int status = QLA_ERROR;
7958c2ecf20Sopenharmony_ci	int soft_reset = 1;
7968c2ecf20Sopenharmony_ci	int config_chip = 0;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	if (is_qla4022(ha) | is_qla4032(ha))
7998c2ecf20Sopenharmony_ci		ql4xxx_set_mac_number(ha);
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
8028c2ecf20Sopenharmony_ci		return QLA_ERROR;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ha->hardware_lock, flags);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	DEBUG2(printk("scsi%ld: %s: port_ctrl	= 0x%08X\n", ha->host_no,
8078c2ecf20Sopenharmony_ci		      __func__, readw(isp_port_ctrl(ha))));
8088c2ecf20Sopenharmony_ci	DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
8098c2ecf20Sopenharmony_ci		     __func__, readw(isp_port_status(ha))));
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	/* Is Hardware already initialized? */
8128c2ecf20Sopenharmony_ci	if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
8138c2ecf20Sopenharmony_ci		DEBUG(printk("scsi%ld: %s: Hardware has already been "
8148c2ecf20Sopenharmony_ci			     "initialized\n", ha->host_no, __func__));
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci		/* Receive firmware boot acknowledgement */
8178c2ecf20Sopenharmony_ci		mbox_status = readw(&ha->reg->mailbox[0]);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci		DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
8208c2ecf20Sopenharmony_ci			      "0x%x\n", ha->host_no, __func__, mbox_status));
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci		/* Is firmware already booted? */
8238c2ecf20Sopenharmony_ci		if (mbox_status == 0) {
8248c2ecf20Sopenharmony_ci			/* F/W not running, must be config by net driver */
8258c2ecf20Sopenharmony_ci			config_chip = 1;
8268c2ecf20Sopenharmony_ci			soft_reset = 0;
8278c2ecf20Sopenharmony_ci		} else {
8288c2ecf20Sopenharmony_ci			writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
8298c2ecf20Sopenharmony_ci			       &ha->reg->ctrl_status);
8308c2ecf20Sopenharmony_ci			readl(&ha->reg->ctrl_status);
8318c2ecf20Sopenharmony_ci			writel(set_rmask(CSR_SCSI_COMPLETION_INTR),
8328c2ecf20Sopenharmony_ci			       &ha->reg->ctrl_status);
8338c2ecf20Sopenharmony_ci			readl(&ha->reg->ctrl_status);
8348c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&ha->hardware_lock, flags);
8358c2ecf20Sopenharmony_ci			if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
8368c2ecf20Sopenharmony_ci				DEBUG2(printk("scsi%ld: %s: Get firmware "
8378c2ecf20Sopenharmony_ci					      "state -- state = 0x%x\n",
8388c2ecf20Sopenharmony_ci					      ha->host_no,
8398c2ecf20Sopenharmony_ci					      __func__, ha->firmware_state));
8408c2ecf20Sopenharmony_ci				/* F/W is running */
8418c2ecf20Sopenharmony_ci				if (ha->firmware_state &
8428c2ecf20Sopenharmony_ci				    FW_STATE_CONFIG_WAIT) {
8438c2ecf20Sopenharmony_ci					DEBUG2(printk("scsi%ld: %s: Firmware "
8448c2ecf20Sopenharmony_ci						      "in known state -- "
8458c2ecf20Sopenharmony_ci						      "config and "
8468c2ecf20Sopenharmony_ci						      "boot, state = 0x%x\n",
8478c2ecf20Sopenharmony_ci						      ha->host_no, __func__,
8488c2ecf20Sopenharmony_ci						      ha->firmware_state));
8498c2ecf20Sopenharmony_ci					config_chip = 1;
8508c2ecf20Sopenharmony_ci					soft_reset = 0;
8518c2ecf20Sopenharmony_ci				}
8528c2ecf20Sopenharmony_ci			} else {
8538c2ecf20Sopenharmony_ci				DEBUG2(printk("scsi%ld: %s: Firmware in "
8548c2ecf20Sopenharmony_ci					      "unknown state -- resetting,"
8558c2ecf20Sopenharmony_ci					      " state = "
8568c2ecf20Sopenharmony_ci					      "0x%x\n", ha->host_no, __func__,
8578c2ecf20Sopenharmony_ci					      ha->firmware_state));
8588c2ecf20Sopenharmony_ci			}
8598c2ecf20Sopenharmony_ci			spin_lock_irqsave(&ha->hardware_lock, flags);
8608c2ecf20Sopenharmony_ci		}
8618c2ecf20Sopenharmony_ci	} else {
8628c2ecf20Sopenharmony_ci		DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
8638c2ecf20Sopenharmony_ci			     "started - resetting\n", ha->host_no, __func__));
8648c2ecf20Sopenharmony_ci	}
8658c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ha->hardware_lock, flags);
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
8688c2ecf20Sopenharmony_ci		     ha->host_no, __func__, soft_reset, config_chip));
8698c2ecf20Sopenharmony_ci	if (soft_reset) {
8708c2ecf20Sopenharmony_ci		DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
8718c2ecf20Sopenharmony_ci			     __func__));
8728c2ecf20Sopenharmony_ci		status = qla4xxx_soft_reset(ha);	/* NOTE: acquires drvr
8738c2ecf20Sopenharmony_ci							 * lock again, but ok */
8748c2ecf20Sopenharmony_ci		if (status == QLA_ERROR) {
8758c2ecf20Sopenharmony_ci			DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
8768c2ecf20Sopenharmony_ci				     ha->host_no, __func__));
8778c2ecf20Sopenharmony_ci			ql4xxx_unlock_drvr(ha);
8788c2ecf20Sopenharmony_ci			return QLA_ERROR;
8798c2ecf20Sopenharmony_ci		}
8808c2ecf20Sopenharmony_ci		config_chip = 1;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci		/* Reset clears the semaphore, so acquire again */
8838c2ecf20Sopenharmony_ci		if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
8848c2ecf20Sopenharmony_ci			return QLA_ERROR;
8858c2ecf20Sopenharmony_ci	}
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	if (config_chip) {
8888c2ecf20Sopenharmony_ci		if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
8898c2ecf20Sopenharmony_ci			status = qla4xxx_start_firmware_from_flash(ha);
8908c2ecf20Sopenharmony_ci	}
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	ql4xxx_unlock_drvr(ha);
8938c2ecf20Sopenharmony_ci	if (status == QLA_SUCCESS) {
8948c2ecf20Sopenharmony_ci		if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
8958c2ecf20Sopenharmony_ci			qla4xxx_get_crash_record(ha);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci		qla4xxx_init_rings(ha);
8988c2ecf20Sopenharmony_ci	} else {
8998c2ecf20Sopenharmony_ci		DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
9008c2ecf20Sopenharmony_ci			     ha->host_no, __func__));
9018c2ecf20Sopenharmony_ci	}
9028c2ecf20Sopenharmony_ci	return status;
9038c2ecf20Sopenharmony_ci}
9048c2ecf20Sopenharmony_ci/**
9058c2ecf20Sopenharmony_ci * qla4xxx_free_ddb_index - Free DDBs reserved by firmware
9068c2ecf20Sopenharmony_ci * @ha: pointer to adapter structure
9078c2ecf20Sopenharmony_ci *
9088c2ecf20Sopenharmony_ci * Since firmware is not running in autoconnect mode the DDB indices should
9098c2ecf20Sopenharmony_ci * be freed so that when login happens from user space there are free DDB
9108c2ecf20Sopenharmony_ci * indices available.
9118c2ecf20Sopenharmony_ci **/
9128c2ecf20Sopenharmony_civoid qla4xxx_free_ddb_index(struct scsi_qla_host *ha)
9138c2ecf20Sopenharmony_ci{
9148c2ecf20Sopenharmony_ci	int max_ddbs;
9158c2ecf20Sopenharmony_ci	int ret;
9168c2ecf20Sopenharmony_ci	uint32_t idx = 0, next_idx = 0;
9178c2ecf20Sopenharmony_ci	uint32_t state = 0, conn_err = 0;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
9208c2ecf20Sopenharmony_ci				     MAX_DEV_DB_ENTRIES;
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	for (idx = 0; idx < max_ddbs; idx = next_idx) {
9238c2ecf20Sopenharmony_ci		ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
9248c2ecf20Sopenharmony_ci					      &next_idx, &state, &conn_err,
9258c2ecf20Sopenharmony_ci						NULL, NULL);
9268c2ecf20Sopenharmony_ci		if (ret == QLA_ERROR) {
9278c2ecf20Sopenharmony_ci			next_idx++;
9288c2ecf20Sopenharmony_ci			continue;
9298c2ecf20Sopenharmony_ci		}
9308c2ecf20Sopenharmony_ci		if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
9318c2ecf20Sopenharmony_ci		    state == DDB_DS_SESSION_FAILED) {
9328c2ecf20Sopenharmony_ci			DEBUG2(ql4_printk(KERN_INFO, ha,
9338c2ecf20Sopenharmony_ci					  "Freeing DDB index = 0x%x\n", idx));
9348c2ecf20Sopenharmony_ci			ret = qla4xxx_clear_ddb_entry(ha, idx);
9358c2ecf20Sopenharmony_ci			if (ret == QLA_ERROR)
9368c2ecf20Sopenharmony_ci				ql4_printk(KERN_ERR, ha,
9378c2ecf20Sopenharmony_ci					   "Unable to clear DDB index = "
9388c2ecf20Sopenharmony_ci					   "0x%x\n", idx);
9398c2ecf20Sopenharmony_ci		}
9408c2ecf20Sopenharmony_ci		if (next_idx == 0)
9418c2ecf20Sopenharmony_ci			break;
9428c2ecf20Sopenharmony_ci	}
9438c2ecf20Sopenharmony_ci}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci/**
9468c2ecf20Sopenharmony_ci * qla4xxx_initialize_adapter - initiailizes hba
9478c2ecf20Sopenharmony_ci * @ha: Pointer to host adapter structure.
9488c2ecf20Sopenharmony_ci * @is_reset: Is this init path or reset path
9498c2ecf20Sopenharmony_ci *
9508c2ecf20Sopenharmony_ci * This routine parforms all of the steps necessary to initialize the adapter.
9518c2ecf20Sopenharmony_ci *
9528c2ecf20Sopenharmony_ci **/
9538c2ecf20Sopenharmony_ciint qla4xxx_initialize_adapter(struct scsi_qla_host *ha, int is_reset)
9548c2ecf20Sopenharmony_ci{
9558c2ecf20Sopenharmony_ci	int status = QLA_ERROR;
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	ha->eeprom_cmd_data = 0;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	ql4_printk(KERN_INFO, ha, "Configuring PCI space...\n");
9608c2ecf20Sopenharmony_ci	ha->isp_ops->pci_config(ha);
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	ha->isp_ops->disable_intrs(ha);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	/* Initialize the Host adapter request/response queues and firmware */
9658c2ecf20Sopenharmony_ci	if (ha->isp_ops->start_firmware(ha) == QLA_ERROR)
9668c2ecf20Sopenharmony_ci		goto exit_init_hba;
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	/*
9698c2ecf20Sopenharmony_ci	 * For ISP83XX, mailbox and IOCB interrupts are enabled separately.
9708c2ecf20Sopenharmony_ci	 * Mailbox interrupts must be enabled prior to issuing any mailbox
9718c2ecf20Sopenharmony_ci	 * command in order to prevent the possibility of losing interrupts
9728c2ecf20Sopenharmony_ci	 * while switching from polling to interrupt mode. IOCB interrupts are
9738c2ecf20Sopenharmony_ci	 * enabled via isp_ops->enable_intrs.
9748c2ecf20Sopenharmony_ci	 */
9758c2ecf20Sopenharmony_ci	if (is_qla8032(ha) || is_qla8042(ha))
9768c2ecf20Sopenharmony_ci		qla4_83xx_enable_mbox_intrs(ha);
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	if (qla4xxx_about_firmware(ha) == QLA_ERROR)
9798c2ecf20Sopenharmony_ci		goto exit_init_hba;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	if (ha->isp_ops->get_sys_info(ha) == QLA_ERROR)
9828c2ecf20Sopenharmony_ci		goto exit_init_hba;
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	qla4xxx_init_local_data(ha);
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	status = qla4xxx_init_firmware(ha);
9878c2ecf20Sopenharmony_ci	if (status == QLA_ERROR)
9888c2ecf20Sopenharmony_ci		goto exit_init_hba;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	if (is_reset == RESET_ADAPTER)
9918c2ecf20Sopenharmony_ci		qla4xxx_build_ddb_list(ha, is_reset);
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	set_bit(AF_ONLINE, &ha->flags);
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ciexit_init_hba:
9968c2ecf20Sopenharmony_ci	DEBUG2(printk("scsi%ld: initialize adapter: %s\n", ha->host_no,
9978c2ecf20Sopenharmony_ci	    status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
9988c2ecf20Sopenharmony_ci	return status;
9998c2ecf20Sopenharmony_ci}
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ciint qla4xxx_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
10028c2ecf20Sopenharmony_ci		       struct ddb_entry *ddb_entry, uint32_t state)
10038c2ecf20Sopenharmony_ci{
10048c2ecf20Sopenharmony_ci	uint32_t old_fw_ddb_device_state;
10058c2ecf20Sopenharmony_ci	int status = QLA_ERROR;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
10088c2ecf20Sopenharmony_ci	DEBUG2(ql4_printk(KERN_INFO, ha,
10098c2ecf20Sopenharmony_ci			  "%s: DDB - old state = 0x%x, new state = 0x%x for "
10108c2ecf20Sopenharmony_ci			  "index [%d]\n", __func__,
10118c2ecf20Sopenharmony_ci			  ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	ddb_entry->fw_ddb_device_state = state;
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	switch (old_fw_ddb_device_state) {
10168c2ecf20Sopenharmony_ci	case DDB_DS_LOGIN_IN_PROCESS:
10178c2ecf20Sopenharmony_ci		switch (state) {
10188c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_ACTIVE:
10198c2ecf20Sopenharmony_ci		case DDB_DS_DISCOVERY:
10208c2ecf20Sopenharmony_ci			qla4xxx_update_session_conn_param(ha, ddb_entry);
10218c2ecf20Sopenharmony_ci			ddb_entry->unblock_sess(ddb_entry->sess);
10228c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
10238c2ecf20Sopenharmony_ci			break;
10248c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_FAILED:
10258c2ecf20Sopenharmony_ci		case DDB_DS_NO_CONNECTION_ACTIVE:
10268c2ecf20Sopenharmony_ci			iscsi_conn_login_event(ddb_entry->conn,
10278c2ecf20Sopenharmony_ci					       ISCSI_CONN_STATE_FREE);
10288c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
10298c2ecf20Sopenharmony_ci			break;
10308c2ecf20Sopenharmony_ci		}
10318c2ecf20Sopenharmony_ci		break;
10328c2ecf20Sopenharmony_ci	case DDB_DS_SESSION_ACTIVE:
10338c2ecf20Sopenharmony_ci	case DDB_DS_DISCOVERY:
10348c2ecf20Sopenharmony_ci		switch (state) {
10358c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_FAILED:
10368c2ecf20Sopenharmony_ci			/*
10378c2ecf20Sopenharmony_ci			 * iscsi_session failure  will cause userspace to
10388c2ecf20Sopenharmony_ci			 * stop the connection which in turn would block the
10398c2ecf20Sopenharmony_ci			 * iscsi_session and start relogin
10408c2ecf20Sopenharmony_ci			 */
10418c2ecf20Sopenharmony_ci			iscsi_session_failure(ddb_entry->sess->dd_data,
10428c2ecf20Sopenharmony_ci					      ISCSI_ERR_CONN_FAILED);
10438c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
10448c2ecf20Sopenharmony_ci			break;
10458c2ecf20Sopenharmony_ci		case DDB_DS_NO_CONNECTION_ACTIVE:
10468c2ecf20Sopenharmony_ci			clear_bit(fw_ddb_index, ha->ddb_idx_map);
10478c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
10488c2ecf20Sopenharmony_ci			break;
10498c2ecf20Sopenharmony_ci		}
10508c2ecf20Sopenharmony_ci		break;
10518c2ecf20Sopenharmony_ci	case DDB_DS_SESSION_FAILED:
10528c2ecf20Sopenharmony_ci		switch (state) {
10538c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_ACTIVE:
10548c2ecf20Sopenharmony_ci		case DDB_DS_DISCOVERY:
10558c2ecf20Sopenharmony_ci			ddb_entry->unblock_sess(ddb_entry->sess);
10568c2ecf20Sopenharmony_ci			qla4xxx_update_session_conn_param(ha, ddb_entry);
10578c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
10588c2ecf20Sopenharmony_ci			break;
10598c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_FAILED:
10608c2ecf20Sopenharmony_ci			iscsi_session_failure(ddb_entry->sess->dd_data,
10618c2ecf20Sopenharmony_ci					      ISCSI_ERR_CONN_FAILED);
10628c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
10638c2ecf20Sopenharmony_ci			break;
10648c2ecf20Sopenharmony_ci		}
10658c2ecf20Sopenharmony_ci		break;
10668c2ecf20Sopenharmony_ci	default:
10678c2ecf20Sopenharmony_ci		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
10688c2ecf20Sopenharmony_ci				__func__));
10698c2ecf20Sopenharmony_ci		break;
10708c2ecf20Sopenharmony_ci	}
10718c2ecf20Sopenharmony_ci	return status;
10728c2ecf20Sopenharmony_ci}
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_civoid qla4xxx_arm_relogin_timer(struct ddb_entry *ddb_entry)
10758c2ecf20Sopenharmony_ci{
10768c2ecf20Sopenharmony_ci	/*
10778c2ecf20Sopenharmony_ci	 * This triggers a relogin.  After the relogin_timer
10788c2ecf20Sopenharmony_ci	 * expires, the relogin gets scheduled.  We must wait a
10798c2ecf20Sopenharmony_ci	 * minimum amount of time since receiving an 0x8014 AEN
10808c2ecf20Sopenharmony_ci	 * with failed device_state or a logout response before
10818c2ecf20Sopenharmony_ci	 * we can issue another relogin.
10828c2ecf20Sopenharmony_ci	 *
10838c2ecf20Sopenharmony_ci	 * Firmware pads this timeout: (time2wait +1).
10848c2ecf20Sopenharmony_ci	 * Driver retry to login should be longer than F/W.
10858c2ecf20Sopenharmony_ci	 * Otherwise F/W will fail
10868c2ecf20Sopenharmony_ci	 * set_ddb() mbx cmd with 0x4005 since it still
10878c2ecf20Sopenharmony_ci	 * counting down its time2wait.
10888c2ecf20Sopenharmony_ci	 */
10898c2ecf20Sopenharmony_ci	atomic_set(&ddb_entry->relogin_timer, 0);
10908c2ecf20Sopenharmony_ci	atomic_set(&ddb_entry->retry_relogin_timer,
10918c2ecf20Sopenharmony_ci		   ddb_entry->default_time2wait + 4);
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci}
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ciint qla4xxx_flash_ddb_change(struct scsi_qla_host *ha, uint32_t fw_ddb_index,
10968c2ecf20Sopenharmony_ci			     struct ddb_entry *ddb_entry, uint32_t state)
10978c2ecf20Sopenharmony_ci{
10988c2ecf20Sopenharmony_ci	uint32_t old_fw_ddb_device_state;
10998c2ecf20Sopenharmony_ci	int status = QLA_ERROR;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
11028c2ecf20Sopenharmony_ci	DEBUG2(ql4_printk(KERN_INFO, ha,
11038c2ecf20Sopenharmony_ci			  "%s: DDB - old state = 0x%x, new state = 0x%x for "
11048c2ecf20Sopenharmony_ci			  "index [%d]\n", __func__,
11058c2ecf20Sopenharmony_ci			  ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	ddb_entry->fw_ddb_device_state = state;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	switch (old_fw_ddb_device_state) {
11108c2ecf20Sopenharmony_ci	case DDB_DS_LOGIN_IN_PROCESS:
11118c2ecf20Sopenharmony_ci	case DDB_DS_NO_CONNECTION_ACTIVE:
11128c2ecf20Sopenharmony_ci		switch (state) {
11138c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_ACTIVE:
11148c2ecf20Sopenharmony_ci			ddb_entry->unblock_sess(ddb_entry->sess);
11158c2ecf20Sopenharmony_ci			qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
11168c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
11178c2ecf20Sopenharmony_ci			break;
11188c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_FAILED:
11198c2ecf20Sopenharmony_ci			iscsi_block_session(ddb_entry->sess);
11208c2ecf20Sopenharmony_ci			if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
11218c2ecf20Sopenharmony_ci				qla4xxx_arm_relogin_timer(ddb_entry);
11228c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
11238c2ecf20Sopenharmony_ci			break;
11248c2ecf20Sopenharmony_ci		}
11258c2ecf20Sopenharmony_ci		break;
11268c2ecf20Sopenharmony_ci	case DDB_DS_SESSION_ACTIVE:
11278c2ecf20Sopenharmony_ci		switch (state) {
11288c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_FAILED:
11298c2ecf20Sopenharmony_ci			iscsi_block_session(ddb_entry->sess);
11308c2ecf20Sopenharmony_ci			if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
11318c2ecf20Sopenharmony_ci				qla4xxx_arm_relogin_timer(ddb_entry);
11328c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
11338c2ecf20Sopenharmony_ci			break;
11348c2ecf20Sopenharmony_ci		}
11358c2ecf20Sopenharmony_ci		break;
11368c2ecf20Sopenharmony_ci	case DDB_DS_SESSION_FAILED:
11378c2ecf20Sopenharmony_ci		switch (state) {
11388c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_ACTIVE:
11398c2ecf20Sopenharmony_ci			ddb_entry->unblock_sess(ddb_entry->sess);
11408c2ecf20Sopenharmony_ci			qla4xxx_update_session_conn_fwddb_param(ha, ddb_entry);
11418c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
11428c2ecf20Sopenharmony_ci			break;
11438c2ecf20Sopenharmony_ci		case DDB_DS_SESSION_FAILED:
11448c2ecf20Sopenharmony_ci			if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
11458c2ecf20Sopenharmony_ci				qla4xxx_arm_relogin_timer(ddb_entry);
11468c2ecf20Sopenharmony_ci			status = QLA_SUCCESS;
11478c2ecf20Sopenharmony_ci			break;
11488c2ecf20Sopenharmony_ci		}
11498c2ecf20Sopenharmony_ci		break;
11508c2ecf20Sopenharmony_ci	default:
11518c2ecf20Sopenharmony_ci		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unknown Event\n",
11528c2ecf20Sopenharmony_ci				  __func__));
11538c2ecf20Sopenharmony_ci		break;
11548c2ecf20Sopenharmony_ci	}
11558c2ecf20Sopenharmony_ci	return status;
11568c2ecf20Sopenharmony_ci}
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci/**
11598c2ecf20Sopenharmony_ci * qla4xxx_process_ddb_changed - process ddb state change
11608c2ecf20Sopenharmony_ci * @ha: Pointer to host adapter structure.
11618c2ecf20Sopenharmony_ci * @fw_ddb_index: Firmware's device database index
11628c2ecf20Sopenharmony_ci * @state: Device state
11638c2ecf20Sopenharmony_ci * @conn_err: Unused
11648c2ecf20Sopenharmony_ci *
11658c2ecf20Sopenharmony_ci * This routine processes a Decive Database Changed AEN Event.
11668c2ecf20Sopenharmony_ci **/
11678c2ecf20Sopenharmony_ciint qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
11688c2ecf20Sopenharmony_ci				uint32_t fw_ddb_index,
11698c2ecf20Sopenharmony_ci				uint32_t state, uint32_t conn_err)
11708c2ecf20Sopenharmony_ci{
11718c2ecf20Sopenharmony_ci	struct ddb_entry *ddb_entry;
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	/* check for out of range index */
11748c2ecf20Sopenharmony_ci	if (fw_ddb_index >= MAX_DDB_ENTRIES)
11758c2ecf20Sopenharmony_ci		goto exit_ddb_event;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	/* Get the corresponging ddb entry */
11788c2ecf20Sopenharmony_ci	ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
11798c2ecf20Sopenharmony_ci	/* Device does not currently exist in our database. */
11808c2ecf20Sopenharmony_ci	if (ddb_entry == NULL) {
11818c2ecf20Sopenharmony_ci		ql4_printk(KERN_ERR, ha, "%s: No ddb_entry at FW index [%d]\n",
11828c2ecf20Sopenharmony_ci			   __func__, fw_ddb_index);
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci		if (state == DDB_DS_NO_CONNECTION_ACTIVE)
11858c2ecf20Sopenharmony_ci			clear_bit(fw_ddb_index, ha->ddb_idx_map);
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci		goto exit_ddb_event;
11888c2ecf20Sopenharmony_ci	}
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	ddb_entry->ddb_change(ha, fw_ddb_index, ddb_entry, state);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ciexit_ddb_event:
11938c2ecf20Sopenharmony_ci	return QLA_ERROR;
11948c2ecf20Sopenharmony_ci}
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci/**
11978c2ecf20Sopenharmony_ci * qla4xxx_login_flash_ddb - Login to target (DDB)
11988c2ecf20Sopenharmony_ci * @cls_session: Pointer to the session to login
11998c2ecf20Sopenharmony_ci *
12008c2ecf20Sopenharmony_ci * This routine logins to the target.
12018c2ecf20Sopenharmony_ci * Issues setddb and conn open mbx
12028c2ecf20Sopenharmony_ci **/
12038c2ecf20Sopenharmony_civoid qla4xxx_login_flash_ddb(struct iscsi_cls_session *cls_session)
12048c2ecf20Sopenharmony_ci{
12058c2ecf20Sopenharmony_ci	struct iscsi_session *sess;
12068c2ecf20Sopenharmony_ci	struct ddb_entry *ddb_entry;
12078c2ecf20Sopenharmony_ci	struct scsi_qla_host *ha;
12088c2ecf20Sopenharmony_ci	struct dev_db_entry *fw_ddb_entry = NULL;
12098c2ecf20Sopenharmony_ci	dma_addr_t fw_ddb_dma;
12108c2ecf20Sopenharmony_ci	uint32_t mbx_sts = 0;
12118c2ecf20Sopenharmony_ci	int ret;
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	sess = cls_session->dd_data;
12148c2ecf20Sopenharmony_ci	ddb_entry = sess->dd_data;
12158c2ecf20Sopenharmony_ci	ha =  ddb_entry->ha;
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	if (!test_bit(AF_LINK_UP, &ha->flags))
12188c2ecf20Sopenharmony_ci		return;
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	if (ddb_entry->ddb_type != FLASH_DDB) {
12218c2ecf20Sopenharmony_ci		DEBUG2(ql4_printk(KERN_INFO, ha,
12228c2ecf20Sopenharmony_ci				  "Skipping login to non FLASH DB"));
12238c2ecf20Sopenharmony_ci		goto exit_login;
12248c2ecf20Sopenharmony_ci	}
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
12278c2ecf20Sopenharmony_ci				      &fw_ddb_dma);
12288c2ecf20Sopenharmony_ci	if (fw_ddb_entry == NULL) {
12298c2ecf20Sopenharmony_ci		DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
12308c2ecf20Sopenharmony_ci		goto exit_login;
12318c2ecf20Sopenharmony_ci	}
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	if (ddb_entry->fw_ddb_index == INVALID_ENTRY) {
12348c2ecf20Sopenharmony_ci		ret = qla4xxx_get_ddb_index(ha, &ddb_entry->fw_ddb_index);
12358c2ecf20Sopenharmony_ci		if (ret == QLA_ERROR)
12368c2ecf20Sopenharmony_ci			goto exit_login;
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci		ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
12398c2ecf20Sopenharmony_ci		ha->tot_ddbs++;
12408c2ecf20Sopenharmony_ci	}
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	memcpy(fw_ddb_entry, &ddb_entry->fw_ddb_entry,
12438c2ecf20Sopenharmony_ci	       sizeof(struct dev_db_entry));
12448c2ecf20Sopenharmony_ci	ddb_entry->sess->target_id = ddb_entry->fw_ddb_index;
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	ret = qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index,
12478c2ecf20Sopenharmony_ci				    fw_ddb_dma, &mbx_sts);
12488c2ecf20Sopenharmony_ci	if (ret == QLA_ERROR) {
12498c2ecf20Sopenharmony_ci		DEBUG2(ql4_printk(KERN_ERR, ha, "Set DDB failed\n"));
12508c2ecf20Sopenharmony_ci		goto exit_login;
12518c2ecf20Sopenharmony_ci	}
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
12548c2ecf20Sopenharmony_ci	ret = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
12558c2ecf20Sopenharmony_ci	if (ret == QLA_ERROR) {
12568c2ecf20Sopenharmony_ci		ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
12578c2ecf20Sopenharmony_ci			   sess->targetname);
12588c2ecf20Sopenharmony_ci		goto exit_login;
12598c2ecf20Sopenharmony_ci	}
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ciexit_login:
12628c2ecf20Sopenharmony_ci	if (fw_ddb_entry)
12638c2ecf20Sopenharmony_ci		dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
12648c2ecf20Sopenharmony_ci}
12658c2ecf20Sopenharmony_ci
1266