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