xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_init.c (revision 5ef12cb4a3a78ffb331c03a795a15eea4ae35155)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Limited and/or its subsidiaries.  *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 
24 #include <linux/blkdev.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/idr.h>
28 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/kthread.h>
31 #include <linux/pci.h>
32 #include <linux/spinlock.h>
33 #include <linux/ctype.h>
34 #include <linux/aer.h>
35 #include <linux/slab.h>
36 #include <linux/firmware.h>
37 #include <linux/miscdevice.h>
38 #include <linux/percpu.h>
39 #include <linux/msi.h>
40 #include <linux/bitops.h>
41 
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_device.h>
44 #include <scsi/scsi_host.h>
45 #include <scsi/scsi_transport_fc.h>
46 #include <scsi/scsi_tcq.h>
47 #include <scsi/fc/fc_fs.h>
48 
49 #include <linux/nvme-fc-driver.h>
50 
51 #include "lpfc_hw4.h"
52 #include "lpfc_hw.h"
53 #include "lpfc_sli.h"
54 #include "lpfc_sli4.h"
55 #include "lpfc_nl.h"
56 #include "lpfc_disc.h"
57 #include "lpfc.h"
58 #include "lpfc_scsi.h"
59 #include "lpfc_nvme.h"
60 #include "lpfc_nvmet.h"
61 #include "lpfc_logmsg.h"
62 #include "lpfc_crtn.h"
63 #include "lpfc_vport.h"
64 #include "lpfc_version.h"
65 #include "lpfc_ids.h"
66 
67 char *_dump_buf_data;
68 unsigned long _dump_buf_data_order;
69 char *_dump_buf_dif;
70 unsigned long _dump_buf_dif_order;
71 spinlock_t _dump_buf_lock;
72 
73 /* Used when mapping IRQ vectors in a driver centric manner */
74 uint16_t *lpfc_used_cpu;
75 uint32_t lpfc_present_cpu;
76 
77 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
78 static int lpfc_post_rcv_buf(struct lpfc_hba *);
79 static int lpfc_sli4_queue_verify(struct lpfc_hba *);
80 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
81 static int lpfc_setup_endian_order(struct lpfc_hba *);
82 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
83 static void lpfc_free_els_sgl_list(struct lpfc_hba *);
84 static void lpfc_free_nvmet_sgl_list(struct lpfc_hba *);
85 static void lpfc_init_sgl_list(struct lpfc_hba *);
86 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
87 static void lpfc_free_active_sgl(struct lpfc_hba *);
88 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
89 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
90 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
91 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
92 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
93 static void lpfc_sli4_disable_intr(struct lpfc_hba *);
94 static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
95 static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
96 
97 static struct scsi_transport_template *lpfc_transport_template = NULL;
98 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
99 static DEFINE_IDR(lpfc_hba_index);
100 #define LPFC_NVMET_BUF_POST 254
101 
102 /**
103  * lpfc_config_port_prep - Perform lpfc initialization prior to config port
104  * @phba: pointer to lpfc hba data structure.
105  *
106  * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
107  * mailbox command. It retrieves the revision information from the HBA and
108  * collects the Vital Product Data (VPD) about the HBA for preparing the
109  * configuration of the HBA.
110  *
111  * Return codes:
112  *   0 - success.
113  *   -ERESTART - requests the SLI layer to reset the HBA and try again.
114  *   Any other value - indicates an error.
115  **/
116 int
117 lpfc_config_port_prep(struct lpfc_hba *phba)
118 {
119 	lpfc_vpd_t *vp = &phba->vpd;
120 	int i = 0, rc;
121 	LPFC_MBOXQ_t *pmb;
122 	MAILBOX_t *mb;
123 	char *lpfc_vpd_data = NULL;
124 	uint16_t offset = 0;
125 	static char licensed[56] =
126 		    "key unlock for use with gnu public licensed code only\0";
127 	static int init_key = 1;
128 
129 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
130 	if (!pmb) {
131 		phba->link_state = LPFC_HBA_ERROR;
132 		return -ENOMEM;
133 	}
134 
135 	mb = &pmb->u.mb;
136 	phba->link_state = LPFC_INIT_MBX_CMDS;
137 
138 	if (lpfc_is_LC_HBA(phba->pcidev->device)) {
139 		if (init_key) {
140 			uint32_t *ptext = (uint32_t *) licensed;
141 
142 			for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
143 				*ptext = cpu_to_be32(*ptext);
144 			init_key = 0;
145 		}
146 
147 		lpfc_read_nv(phba, pmb);
148 		memset((char*)mb->un.varRDnvp.rsvd3, 0,
149 			sizeof (mb->un.varRDnvp.rsvd3));
150 		memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
151 			 sizeof (licensed));
152 
153 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154 
155 		if (rc != MBX_SUCCESS) {
156 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
157 					"0324 Config Port initialization "
158 					"error, mbxCmd x%x READ_NVPARM, "
159 					"mbxStatus x%x\n",
160 					mb->mbxCommand, mb->mbxStatus);
161 			mempool_free(pmb, phba->mbox_mem_pool);
162 			return -ERESTART;
163 		}
164 		memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
165 		       sizeof(phba->wwnn));
166 		memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
167 		       sizeof(phba->wwpn));
168 	}
169 
170 	phba->sli3_options = 0x0;
171 
172 	/* Setup and issue mailbox READ REV command */
173 	lpfc_read_rev(phba, pmb);
174 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
175 	if (rc != MBX_SUCCESS) {
176 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
177 				"0439 Adapter failed to init, mbxCmd x%x "
178 				"READ_REV, mbxStatus x%x\n",
179 				mb->mbxCommand, mb->mbxStatus);
180 		mempool_free( pmb, phba->mbox_mem_pool);
181 		return -ERESTART;
182 	}
183 
184 
185 	/*
186 	 * The value of rr must be 1 since the driver set the cv field to 1.
187 	 * This setting requires the FW to set all revision fields.
188 	 */
189 	if (mb->un.varRdRev.rr == 0) {
190 		vp->rev.rBit = 0;
191 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
192 				"0440 Adapter failed to init, READ_REV has "
193 				"missing revision information.\n");
194 		mempool_free(pmb, phba->mbox_mem_pool);
195 		return -ERESTART;
196 	}
197 
198 	if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
199 		mempool_free(pmb, phba->mbox_mem_pool);
200 		return -EINVAL;
201 	}
202 
203 	/* Save information as VPD data */
204 	vp->rev.rBit = 1;
205 	memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
206 	vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
207 	memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
208 	vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
209 	memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
210 	vp->rev.biuRev = mb->un.varRdRev.biuRev;
211 	vp->rev.smRev = mb->un.varRdRev.smRev;
212 	vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
213 	vp->rev.endecRev = mb->un.varRdRev.endecRev;
214 	vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
215 	vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
216 	vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
217 	vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
218 	vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
219 	vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
220 
221 	/* If the sli feature level is less then 9, we must
222 	 * tear down all RPIs and VPIs on link down if NPIV
223 	 * is enabled.
224 	 */
225 	if (vp->rev.feaLevelHigh < 9)
226 		phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
227 
228 	if (lpfc_is_LC_HBA(phba->pcidev->device))
229 		memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
230 						sizeof (phba->RandomData));
231 
232 	/* Get adapter VPD information */
233 	lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
234 	if (!lpfc_vpd_data)
235 		goto out_free_mbox;
236 	do {
237 		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
238 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
239 
240 		if (rc != MBX_SUCCESS) {
241 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
242 					"0441 VPD not present on adapter, "
243 					"mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
244 					mb->mbxCommand, mb->mbxStatus);
245 			mb->un.varDmp.word_cnt = 0;
246 		}
247 		/* dump mem may return a zero when finished or we got a
248 		 * mailbox error, either way we are done.
249 		 */
250 		if (mb->un.varDmp.word_cnt == 0)
251 			break;
252 		if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
253 			mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
254 		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
255 				      lpfc_vpd_data + offset,
256 				      mb->un.varDmp.word_cnt);
257 		offset += mb->un.varDmp.word_cnt;
258 	} while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
259 	lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
260 
261 	kfree(lpfc_vpd_data);
262 out_free_mbox:
263 	mempool_free(pmb, phba->mbox_mem_pool);
264 	return 0;
265 }
266 
267 /**
268  * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
269  * @phba: pointer to lpfc hba data structure.
270  * @pmboxq: pointer to the driver internal queue element for mailbox command.
271  *
272  * This is the completion handler for driver's configuring asynchronous event
273  * mailbox command to the device. If the mailbox command returns successfully,
274  * it will set internal async event support flag to 1; otherwise, it will
275  * set internal async event support flag to 0.
276  **/
277 static void
278 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
279 {
280 	if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
281 		phba->temp_sensor_support = 1;
282 	else
283 		phba->temp_sensor_support = 0;
284 	mempool_free(pmboxq, phba->mbox_mem_pool);
285 	return;
286 }
287 
288 /**
289  * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
290  * @phba: pointer to lpfc hba data structure.
291  * @pmboxq: pointer to the driver internal queue element for mailbox command.
292  *
293  * This is the completion handler for dump mailbox command for getting
294  * wake up parameters. When this command complete, the response contain
295  * Option rom version of the HBA. This function translate the version number
296  * into a human readable string and store it in OptionROMVersion.
297  **/
298 static void
299 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
300 {
301 	struct prog_id *prg;
302 	uint32_t prog_id_word;
303 	char dist = ' ';
304 	/* character array used for decoding dist type. */
305 	char dist_char[] = "nabx";
306 
307 	if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
308 		mempool_free(pmboxq, phba->mbox_mem_pool);
309 		return;
310 	}
311 
312 	prg = (struct prog_id *) &prog_id_word;
313 
314 	/* word 7 contain option rom version */
315 	prog_id_word = pmboxq->u.mb.un.varWords[7];
316 
317 	/* Decode the Option rom version word to a readable string */
318 	if (prg->dist < 4)
319 		dist = dist_char[prg->dist];
320 
321 	if ((prg->dist == 3) && (prg->num == 0))
322 		snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
323 			prg->ver, prg->rev, prg->lev);
324 	else
325 		snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
326 			prg->ver, prg->rev, prg->lev,
327 			dist, prg->num);
328 	mempool_free(pmboxq, phba->mbox_mem_pool);
329 	return;
330 }
331 
332 /**
333  * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
334  *	cfg_soft_wwnn, cfg_soft_wwpn
335  * @vport: pointer to lpfc vport data structure.
336  *
337  *
338  * Return codes
339  *   None.
340  **/
341 void
342 lpfc_update_vport_wwn(struct lpfc_vport *vport)
343 {
344 	uint8_t vvvl = vport->fc_sparam.cmn.valid_vendor_ver_level;
345 	u32 *fawwpn_key = (u32 *)&vport->fc_sparam.un.vendorVersion[0];
346 
347 	/* If the soft name exists then update it using the service params */
348 	if (vport->phba->cfg_soft_wwnn)
349 		u64_to_wwn(vport->phba->cfg_soft_wwnn,
350 			   vport->fc_sparam.nodeName.u.wwn);
351 	if (vport->phba->cfg_soft_wwpn)
352 		u64_to_wwn(vport->phba->cfg_soft_wwpn,
353 			   vport->fc_sparam.portName.u.wwn);
354 
355 	/*
356 	 * If the name is empty or there exists a soft name
357 	 * then copy the service params name, otherwise use the fc name
358 	 */
359 	if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
360 		memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
361 			sizeof(struct lpfc_name));
362 	else
363 		memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
364 			sizeof(struct lpfc_name));
365 
366 	/*
367 	 * If the port name has changed, then set the Param changes flag
368 	 * to unreg the login
369 	 */
370 	if (vport->fc_portname.u.wwn[0] != 0 &&
371 		memcmp(&vport->fc_portname, &vport->fc_sparam.portName,
372 			sizeof(struct lpfc_name)))
373 		vport->vport_flag |= FAWWPN_PARAM_CHG;
374 
375 	if (vport->fc_portname.u.wwn[0] == 0 ||
376 	    vport->phba->cfg_soft_wwpn ||
377 	    (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR) ||
378 	    vport->vport_flag & FAWWPN_SET) {
379 		memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
380 			sizeof(struct lpfc_name));
381 		vport->vport_flag &= ~FAWWPN_SET;
382 		if (vvvl == 1 && cpu_to_be32(*fawwpn_key) == FAPWWN_KEY_VENDOR)
383 			vport->vport_flag |= FAWWPN_SET;
384 	}
385 	else
386 		memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
387 			sizeof(struct lpfc_name));
388 }
389 
390 /**
391  * lpfc_config_port_post - Perform lpfc initialization after config port
392  * @phba: pointer to lpfc hba data structure.
393  *
394  * This routine will do LPFC initialization after the CONFIG_PORT mailbox
395  * command call. It performs all internal resource and state setups on the
396  * port: post IOCB buffers, enable appropriate host interrupt attentions,
397  * ELS ring timers, etc.
398  *
399  * Return codes
400  *   0 - success.
401  *   Any other value - error.
402  **/
403 int
404 lpfc_config_port_post(struct lpfc_hba *phba)
405 {
406 	struct lpfc_vport *vport = phba->pport;
407 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
408 	LPFC_MBOXQ_t *pmb;
409 	MAILBOX_t *mb;
410 	struct lpfc_dmabuf *mp;
411 	struct lpfc_sli *psli = &phba->sli;
412 	uint32_t status, timeout;
413 	int i, j;
414 	int rc;
415 
416 	spin_lock_irq(&phba->hbalock);
417 	/*
418 	 * If the Config port completed correctly the HBA is not
419 	 * over heated any more.
420 	 */
421 	if (phba->over_temp_state == HBA_OVER_TEMP)
422 		phba->over_temp_state = HBA_NORMAL_TEMP;
423 	spin_unlock_irq(&phba->hbalock);
424 
425 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
426 	if (!pmb) {
427 		phba->link_state = LPFC_HBA_ERROR;
428 		return -ENOMEM;
429 	}
430 	mb = &pmb->u.mb;
431 
432 	/* Get login parameters for NID.  */
433 	rc = lpfc_read_sparam(phba, pmb, 0);
434 	if (rc) {
435 		mempool_free(pmb, phba->mbox_mem_pool);
436 		return -ENOMEM;
437 	}
438 
439 	pmb->vport = vport;
440 	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
441 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
442 				"0448 Adapter failed init, mbxCmd x%x "
443 				"READ_SPARM mbxStatus x%x\n",
444 				mb->mbxCommand, mb->mbxStatus);
445 		phba->link_state = LPFC_HBA_ERROR;
446 		mp = (struct lpfc_dmabuf *) pmb->context1;
447 		mempool_free(pmb, phba->mbox_mem_pool);
448 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
449 		kfree(mp);
450 		return -EIO;
451 	}
452 
453 	mp = (struct lpfc_dmabuf *) pmb->context1;
454 
455 	memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
456 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
457 	kfree(mp);
458 	pmb->context1 = NULL;
459 	lpfc_update_vport_wwn(vport);
460 
461 	/* Update the fc_host data structures with new wwn. */
462 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
463 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
464 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
465 
466 	/* If no serial number in VPD data, use low 6 bytes of WWNN */
467 	/* This should be consolidated into parse_vpd ? - mr */
468 	if (phba->SerialNumber[0] == 0) {
469 		uint8_t *outptr;
470 
471 		outptr = &vport->fc_nodename.u.s.IEEE[0];
472 		for (i = 0; i < 12; i++) {
473 			status = *outptr++;
474 			j = ((status & 0xf0) >> 4);
475 			if (j <= 9)
476 				phba->SerialNumber[i] =
477 				    (char)((uint8_t) 0x30 + (uint8_t) j);
478 			else
479 				phba->SerialNumber[i] =
480 				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
481 			i++;
482 			j = (status & 0xf);
483 			if (j <= 9)
484 				phba->SerialNumber[i] =
485 				    (char)((uint8_t) 0x30 + (uint8_t) j);
486 			else
487 				phba->SerialNumber[i] =
488 				    (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
489 		}
490 	}
491 
492 	lpfc_read_config(phba, pmb);
493 	pmb->vport = vport;
494 	if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
495 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
496 				"0453 Adapter failed to init, mbxCmd x%x "
497 				"READ_CONFIG, mbxStatus x%x\n",
498 				mb->mbxCommand, mb->mbxStatus);
499 		phba->link_state = LPFC_HBA_ERROR;
500 		mempool_free( pmb, phba->mbox_mem_pool);
501 		return -EIO;
502 	}
503 
504 	/* Check if the port is disabled */
505 	lpfc_sli_read_link_ste(phba);
506 
507 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
508 	i = (mb->un.varRdConfig.max_xri + 1);
509 	if (phba->cfg_hba_queue_depth > i) {
510 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
511 				"3359 HBA queue depth changed from %d to %d\n",
512 				phba->cfg_hba_queue_depth, i);
513 		phba->cfg_hba_queue_depth = i;
514 	}
515 
516 	/* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
517 	i = (mb->un.varRdConfig.max_xri >> 3);
518 	if (phba->pport->cfg_lun_queue_depth > i) {
519 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
520 				"3360 LUN queue depth changed from %d to %d\n",
521 				phba->pport->cfg_lun_queue_depth, i);
522 		phba->pport->cfg_lun_queue_depth = i;
523 	}
524 
525 	phba->lmt = mb->un.varRdConfig.lmt;
526 
527 	/* Get the default values for Model Name and Description */
528 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
529 
530 	phba->link_state = LPFC_LINK_DOWN;
531 
532 	/* Only process IOCBs on ELS ring till hba_state is READY */
533 	if (psli->sli3_ring[LPFC_EXTRA_RING].sli.sli3.cmdringaddr)
534 		psli->sli3_ring[LPFC_EXTRA_RING].flag |= LPFC_STOP_IOCB_EVENT;
535 	if (psli->sli3_ring[LPFC_FCP_RING].sli.sli3.cmdringaddr)
536 		psli->sli3_ring[LPFC_FCP_RING].flag |= LPFC_STOP_IOCB_EVENT;
537 
538 	/* Post receive buffers for desired rings */
539 	if (phba->sli_rev != 3)
540 		lpfc_post_rcv_buf(phba);
541 
542 	/*
543 	 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
544 	 */
545 	if (phba->intr_type == MSIX) {
546 		rc = lpfc_config_msi(phba, pmb);
547 		if (rc) {
548 			mempool_free(pmb, phba->mbox_mem_pool);
549 			return -EIO;
550 		}
551 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
552 		if (rc != MBX_SUCCESS) {
553 			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
554 					"0352 Config MSI mailbox command "
555 					"failed, mbxCmd x%x, mbxStatus x%x\n",
556 					pmb->u.mb.mbxCommand,
557 					pmb->u.mb.mbxStatus);
558 			mempool_free(pmb, phba->mbox_mem_pool);
559 			return -EIO;
560 		}
561 	}
562 
563 	spin_lock_irq(&phba->hbalock);
564 	/* Initialize ERATT handling flag */
565 	phba->hba_flag &= ~HBA_ERATT_HANDLED;
566 
567 	/* Enable appropriate host interrupts */
568 	if (lpfc_readl(phba->HCregaddr, &status)) {
569 		spin_unlock_irq(&phba->hbalock);
570 		return -EIO;
571 	}
572 	status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
573 	if (psli->num_rings > 0)
574 		status |= HC_R0INT_ENA;
575 	if (psli->num_rings > 1)
576 		status |= HC_R1INT_ENA;
577 	if (psli->num_rings > 2)
578 		status |= HC_R2INT_ENA;
579 	if (psli->num_rings > 3)
580 		status |= HC_R3INT_ENA;
581 
582 	if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
583 	    (phba->cfg_poll & DISABLE_FCP_RING_INT))
584 		status &= ~(HC_R0INT_ENA);
585 
586 	writel(status, phba->HCregaddr);
587 	readl(phba->HCregaddr); /* flush */
588 	spin_unlock_irq(&phba->hbalock);
589 
590 	/* Set up ring-0 (ELS) timer */
591 	timeout = phba->fc_ratov * 2;
592 	mod_timer(&vport->els_tmofunc,
593 		  jiffies + msecs_to_jiffies(1000 * timeout));
594 	/* Set up heart beat (HB) timer */
595 	mod_timer(&phba->hb_tmofunc,
596 		  jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
597 	phba->hb_outstanding = 0;
598 	phba->last_completion_time = jiffies;
599 	/* Set up error attention (ERATT) polling timer */
600 	mod_timer(&phba->eratt_poll,
601 		  jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
602 
603 	if (phba->hba_flag & LINK_DISABLED) {
604 		lpfc_printf_log(phba,
605 			KERN_ERR, LOG_INIT,
606 			"2598 Adapter Link is disabled.\n");
607 		lpfc_down_link(phba, pmb);
608 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
609 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
610 		if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
611 			lpfc_printf_log(phba,
612 			KERN_ERR, LOG_INIT,
613 			"2599 Adapter failed to issue DOWN_LINK"
614 			" mbox command rc 0x%x\n", rc);
615 
616 			mempool_free(pmb, phba->mbox_mem_pool);
617 			return -EIO;
618 		}
619 	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
620 		mempool_free(pmb, phba->mbox_mem_pool);
621 		rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
622 		if (rc)
623 			return rc;
624 	}
625 	/* MBOX buffer will be freed in mbox compl */
626 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
627 	if (!pmb) {
628 		phba->link_state = LPFC_HBA_ERROR;
629 		return -ENOMEM;
630 	}
631 
632 	lpfc_config_async(phba, pmb, LPFC_ELS_RING);
633 	pmb->mbox_cmpl = lpfc_config_async_cmpl;
634 	pmb->vport = phba->pport;
635 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
636 
637 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
638 		lpfc_printf_log(phba,
639 				KERN_ERR,
640 				LOG_INIT,
641 				"0456 Adapter failed to issue "
642 				"ASYNCEVT_ENABLE mbox status x%x\n",
643 				rc);
644 		mempool_free(pmb, phba->mbox_mem_pool);
645 	}
646 
647 	/* Get Option rom version */
648 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
649 	if (!pmb) {
650 		phba->link_state = LPFC_HBA_ERROR;
651 		return -ENOMEM;
652 	}
653 
654 	lpfc_dump_wakeup_param(phba, pmb);
655 	pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
656 	pmb->vport = phba->pport;
657 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
658 
659 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
660 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
661 				"to get Option ROM version status x%x\n", rc);
662 		mempool_free(pmb, phba->mbox_mem_pool);
663 	}
664 
665 	return 0;
666 }
667 
668 /**
669  * lpfc_hba_init_link - Initialize the FC link
670  * @phba: pointer to lpfc hba data structure.
671  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
672  *
673  * This routine will issue the INIT_LINK mailbox command call.
674  * It is available to other drivers through the lpfc_hba data
675  * structure for use as a delayed link up mechanism with the
676  * module parameter lpfc_suppress_link_up.
677  *
678  * Return code
679  *		0 - success
680  *		Any other value - error
681  **/
682 static int
683 lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
684 {
685 	return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
686 }
687 
688 /**
689  * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
690  * @phba: pointer to lpfc hba data structure.
691  * @fc_topology: desired fc topology.
692  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
693  *
694  * This routine will issue the INIT_LINK mailbox command call.
695  * It is available to other drivers through the lpfc_hba data
696  * structure for use as a delayed link up mechanism with the
697  * module parameter lpfc_suppress_link_up.
698  *
699  * Return code
700  *              0 - success
701  *              Any other value - error
702  **/
703 int
704 lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
705 			       uint32_t flag)
706 {
707 	struct lpfc_vport *vport = phba->pport;
708 	LPFC_MBOXQ_t *pmb;
709 	MAILBOX_t *mb;
710 	int rc;
711 
712 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
713 	if (!pmb) {
714 		phba->link_state = LPFC_HBA_ERROR;
715 		return -ENOMEM;
716 	}
717 	mb = &pmb->u.mb;
718 	pmb->vport = vport;
719 
720 	if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
721 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
722 	     !(phba->lmt & LMT_1Gb)) ||
723 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
724 	     !(phba->lmt & LMT_2Gb)) ||
725 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
726 	     !(phba->lmt & LMT_4Gb)) ||
727 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
728 	     !(phba->lmt & LMT_8Gb)) ||
729 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
730 	     !(phba->lmt & LMT_10Gb)) ||
731 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
732 	     !(phba->lmt & LMT_16Gb)) ||
733 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_32G) &&
734 	     !(phba->lmt & LMT_32Gb)) ||
735 	    ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_64G) &&
736 	     !(phba->lmt & LMT_64Gb))) {
737 		/* Reset link speed to auto */
738 		lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
739 			"1302 Invalid speed for this board:%d "
740 			"Reset link speed to auto.\n",
741 			phba->cfg_link_speed);
742 			phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
743 	}
744 	lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
745 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
746 	if (phba->sli_rev < LPFC_SLI_REV4)
747 		lpfc_set_loopback_flag(phba);
748 	rc = lpfc_sli_issue_mbox(phba, pmb, flag);
749 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
750 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
751 			"0498 Adapter failed to init, mbxCmd x%x "
752 			"INIT_LINK, mbxStatus x%x\n",
753 			mb->mbxCommand, mb->mbxStatus);
754 		if (phba->sli_rev <= LPFC_SLI_REV3) {
755 			/* Clear all interrupt enable conditions */
756 			writel(0, phba->HCregaddr);
757 			readl(phba->HCregaddr); /* flush */
758 			/* Clear all pending interrupts */
759 			writel(0xffffffff, phba->HAregaddr);
760 			readl(phba->HAregaddr); /* flush */
761 		}
762 		phba->link_state = LPFC_HBA_ERROR;
763 		if (rc != MBX_BUSY || flag == MBX_POLL)
764 			mempool_free(pmb, phba->mbox_mem_pool);
765 		return -EIO;
766 	}
767 	phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
768 	if (flag == MBX_POLL)
769 		mempool_free(pmb, phba->mbox_mem_pool);
770 
771 	return 0;
772 }
773 
774 /**
775  * lpfc_hba_down_link - this routine downs the FC link
776  * @phba: pointer to lpfc hba data structure.
777  * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
778  *
779  * This routine will issue the DOWN_LINK mailbox command call.
780  * It is available to other drivers through the lpfc_hba data
781  * structure for use to stop the link.
782  *
783  * Return code
784  *		0 - success
785  *		Any other value - error
786  **/
787 static int
788 lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
789 {
790 	LPFC_MBOXQ_t *pmb;
791 	int rc;
792 
793 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
794 	if (!pmb) {
795 		phba->link_state = LPFC_HBA_ERROR;
796 		return -ENOMEM;
797 	}
798 
799 	lpfc_printf_log(phba,
800 		KERN_ERR, LOG_INIT,
801 		"0491 Adapter Link is disabled.\n");
802 	lpfc_down_link(phba, pmb);
803 	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
804 	rc = lpfc_sli_issue_mbox(phba, pmb, flag);
805 	if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
806 		lpfc_printf_log(phba,
807 		KERN_ERR, LOG_INIT,
808 		"2522 Adapter failed to issue DOWN_LINK"
809 		" mbox command rc 0x%x\n", rc);
810 
811 		mempool_free(pmb, phba->mbox_mem_pool);
812 		return -EIO;
813 	}
814 	if (flag == MBX_POLL)
815 		mempool_free(pmb, phba->mbox_mem_pool);
816 
817 	return 0;
818 }
819 
820 /**
821  * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
822  * @phba: pointer to lpfc HBA data structure.
823  *
824  * This routine will do LPFC uninitialization before the HBA is reset when
825  * bringing down the SLI Layer.
826  *
827  * Return codes
828  *   0 - success.
829  *   Any other value - error.
830  **/
831 int
832 lpfc_hba_down_prep(struct lpfc_hba *phba)
833 {
834 	struct lpfc_vport **vports;
835 	int i;
836 
837 	if (phba->sli_rev <= LPFC_SLI_REV3) {
838 		/* Disable interrupts */
839 		writel(0, phba->HCregaddr);
840 		readl(phba->HCregaddr); /* flush */
841 	}
842 
843 	if (phba->pport->load_flag & FC_UNLOADING)
844 		lpfc_cleanup_discovery_resources(phba->pport);
845 	else {
846 		vports = lpfc_create_vport_work_array(phba);
847 		if (vports != NULL)
848 			for (i = 0; i <= phba->max_vports &&
849 				vports[i] != NULL; i++)
850 				lpfc_cleanup_discovery_resources(vports[i]);
851 		lpfc_destroy_vport_work_array(phba, vports);
852 	}
853 	return 0;
854 }
855 
856 /**
857  * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
858  * rspiocb which got deferred
859  *
860  * @phba: pointer to lpfc HBA data structure.
861  *
862  * This routine will cleanup completed slow path events after HBA is reset
863  * when bringing down the SLI Layer.
864  *
865  *
866  * Return codes
867  *   void.
868  **/
869 static void
870 lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
871 {
872 	struct lpfc_iocbq *rspiocbq;
873 	struct hbq_dmabuf *dmabuf;
874 	struct lpfc_cq_event *cq_event;
875 
876 	spin_lock_irq(&phba->hbalock);
877 	phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
878 	spin_unlock_irq(&phba->hbalock);
879 
880 	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
881 		/* Get the response iocb from the head of work queue */
882 		spin_lock_irq(&phba->hbalock);
883 		list_remove_head(&phba->sli4_hba.sp_queue_event,
884 				 cq_event, struct lpfc_cq_event, list);
885 		spin_unlock_irq(&phba->hbalock);
886 
887 		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
888 		case CQE_CODE_COMPL_WQE:
889 			rspiocbq = container_of(cq_event, struct lpfc_iocbq,
890 						 cq_event);
891 			lpfc_sli_release_iocbq(phba, rspiocbq);
892 			break;
893 		case CQE_CODE_RECEIVE:
894 		case CQE_CODE_RECEIVE_V1:
895 			dmabuf = container_of(cq_event, struct hbq_dmabuf,
896 					      cq_event);
897 			lpfc_in_buf_free(phba, &dmabuf->dbuf);
898 		}
899 	}
900 }
901 
902 /**
903  * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
904  * @phba: pointer to lpfc HBA data structure.
905  *
906  * This routine will cleanup posted ELS buffers after the HBA is reset
907  * when bringing down the SLI Layer.
908  *
909  *
910  * Return codes
911  *   void.
912  **/
913 static void
914 lpfc_hba_free_post_buf(struct lpfc_hba *phba)
915 {
916 	struct lpfc_sli *psli = &phba->sli;
917 	struct lpfc_sli_ring *pring;
918 	struct lpfc_dmabuf *mp, *next_mp;
919 	LIST_HEAD(buflist);
920 	int count;
921 
922 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
923 		lpfc_sli_hbqbuf_free_all(phba);
924 	else {
925 		/* Cleanup preposted buffers on the ELS ring */
926 		pring = &psli->sli3_ring[LPFC_ELS_RING];
927 		spin_lock_irq(&phba->hbalock);
928 		list_splice_init(&pring->postbufq, &buflist);
929 		spin_unlock_irq(&phba->hbalock);
930 
931 		count = 0;
932 		list_for_each_entry_safe(mp, next_mp, &buflist, list) {
933 			list_del(&mp->list);
934 			count++;
935 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
936 			kfree(mp);
937 		}
938 
939 		spin_lock_irq(&phba->hbalock);
940 		pring->postbufq_cnt -= count;
941 		spin_unlock_irq(&phba->hbalock);
942 	}
943 }
944 
945 /**
946  * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
947  * @phba: pointer to lpfc HBA data structure.
948  *
949  * This routine will cleanup the txcmplq after the HBA is reset when bringing
950  * down the SLI Layer.
951  *
952  * Return codes
953  *   void
954  **/
955 static void
956 lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
957 {
958 	struct lpfc_sli *psli = &phba->sli;
959 	struct lpfc_queue *qp = NULL;
960 	struct lpfc_sli_ring *pring;
961 	LIST_HEAD(completions);
962 	int i;
963 	struct lpfc_iocbq *piocb, *next_iocb;
964 
965 	if (phba->sli_rev != LPFC_SLI_REV4) {
966 		for (i = 0; i < psli->num_rings; i++) {
967 			pring = &psli->sli3_ring[i];
968 			spin_lock_irq(&phba->hbalock);
969 			/* At this point in time the HBA is either reset or DOA
970 			 * Nothing should be on txcmplq as it will
971 			 * NEVER complete.
972 			 */
973 			list_splice_init(&pring->txcmplq, &completions);
974 			pring->txcmplq_cnt = 0;
975 			spin_unlock_irq(&phba->hbalock);
976 
977 			lpfc_sli_abort_iocb_ring(phba, pring);
978 		}
979 		/* Cancel all the IOCBs from the completions list */
980 		lpfc_sli_cancel_iocbs(phba, &completions,
981 				      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
982 		return;
983 	}
984 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
985 		pring = qp->pring;
986 		if (!pring)
987 			continue;
988 		spin_lock_irq(&pring->ring_lock);
989 		list_for_each_entry_safe(piocb, next_iocb,
990 					 &pring->txcmplq, list)
991 			piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
992 		list_splice_init(&pring->txcmplq, &completions);
993 		pring->txcmplq_cnt = 0;
994 		spin_unlock_irq(&pring->ring_lock);
995 		lpfc_sli_abort_iocb_ring(phba, pring);
996 	}
997 	/* Cancel all the IOCBs from the completions list */
998 	lpfc_sli_cancel_iocbs(phba, &completions,
999 			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
1000 }
1001 
1002 /**
1003  * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
1004 	int i;
1005  * @phba: pointer to lpfc HBA data structure.
1006  *
1007  * This routine will do uninitialization after the HBA is reset when bring
1008  * down the SLI Layer.
1009  *
1010  * Return codes
1011  *   0 - success.
1012  *   Any other value - error.
1013  **/
1014 static int
1015 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
1016 {
1017 	lpfc_hba_free_post_buf(phba);
1018 	lpfc_hba_clean_txcmplq(phba);
1019 	return 0;
1020 }
1021 
1022 /**
1023  * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
1024  * @phba: pointer to lpfc HBA data structure.
1025  *
1026  * This routine will do uninitialization after the HBA is reset when bring
1027  * down the SLI Layer.
1028  *
1029  * Return codes
1030  *   0 - success.
1031  *   Any other value - error.
1032  **/
1033 static int
1034 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
1035 {
1036 	struct lpfc_scsi_buf *psb, *psb_next;
1037 	struct lpfc_nvmet_rcv_ctx *ctxp, *ctxp_next;
1038 	LIST_HEAD(aborts);
1039 	LIST_HEAD(nvme_aborts);
1040 	LIST_HEAD(nvmet_aborts);
1041 	unsigned long iflag = 0;
1042 	struct lpfc_sglq *sglq_entry = NULL;
1043 	int cnt;
1044 
1045 
1046 	lpfc_sli_hbqbuf_free_all(phba);
1047 	lpfc_hba_clean_txcmplq(phba);
1048 
1049 	/* At this point in time the HBA is either reset or DOA. Either
1050 	 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
1051 	 * on the lpfc_els_sgl_list so that it can either be freed if the
1052 	 * driver is unloading or reposted if the driver is restarting
1053 	 * the port.
1054 	 */
1055 	spin_lock_irq(&phba->hbalock);  /* required for lpfc_els_sgl_list and */
1056 					/* scsl_buf_list */
1057 	/* sgl_list_lock required because worker thread uses this
1058 	 * list.
1059 	 */
1060 	spin_lock(&phba->sli4_hba.sgl_list_lock);
1061 	list_for_each_entry(sglq_entry,
1062 		&phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1063 		sglq_entry->state = SGL_FREED;
1064 
1065 	list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
1066 			&phba->sli4_hba.lpfc_els_sgl_list);
1067 
1068 
1069 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
1070 	/* abts_scsi_buf_list_lock required because worker thread uses this
1071 	 * list.
1072 	 */
1073 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
1074 		spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1075 		list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1076 				 &aborts);
1077 		spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1078 	}
1079 
1080 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1081 		spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1082 		list_splice_init(&phba->sli4_hba.lpfc_abts_nvme_buf_list,
1083 				 &nvme_aborts);
1084 		list_splice_init(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1085 				 &nvmet_aborts);
1086 		spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1087 	}
1088 
1089 	spin_unlock_irq(&phba->hbalock);
1090 
1091 	list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1092 		psb->pCmd = NULL;
1093 		psb->status = IOSTAT_SUCCESS;
1094 	}
1095 	spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1096 	list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1097 	spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1098 
1099 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1100 		cnt = 0;
1101 		list_for_each_entry_safe(psb, psb_next, &nvme_aborts, list) {
1102 			psb->pCmd = NULL;
1103 			psb->status = IOSTAT_SUCCESS;
1104 			cnt++;
1105 		}
1106 		spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
1107 		phba->put_nvme_bufs += cnt;
1108 		list_splice(&nvme_aborts, &phba->lpfc_nvme_buf_list_put);
1109 		spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
1110 
1111 		list_for_each_entry_safe(ctxp, ctxp_next, &nvmet_aborts, list) {
1112 			ctxp->flag &= ~(LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP);
1113 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
1114 		}
1115 	}
1116 
1117 	lpfc_sli4_free_sp_events(phba);
1118 	return 0;
1119 }
1120 
1121 /**
1122  * lpfc_hba_down_post - Wrapper func for hba down post routine
1123  * @phba: pointer to lpfc HBA data structure.
1124  *
1125  * This routine wraps the actual SLI3 or SLI4 routine for performing
1126  * uninitialization after the HBA is reset when bring down the SLI Layer.
1127  *
1128  * Return codes
1129  *   0 - success.
1130  *   Any other value - error.
1131  **/
1132 int
1133 lpfc_hba_down_post(struct lpfc_hba *phba)
1134 {
1135 	return (*phba->lpfc_hba_down_post)(phba);
1136 }
1137 
1138 /**
1139  * lpfc_hb_timeout - The HBA-timer timeout handler
1140  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1141  *
1142  * This is the HBA-timer timeout handler registered to the lpfc driver. When
1143  * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1144  * work-port-events bitmap and the worker thread is notified. This timeout
1145  * event will be used by the worker thread to invoke the actual timeout
1146  * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1147  * be performed in the timeout handler and the HBA timeout event bit shall
1148  * be cleared by the worker thread after it has taken the event bitmap out.
1149  **/
1150 static void
1151 lpfc_hb_timeout(struct timer_list *t)
1152 {
1153 	struct lpfc_hba *phba;
1154 	uint32_t tmo_posted;
1155 	unsigned long iflag;
1156 
1157 	phba = from_timer(phba, t, hb_tmofunc);
1158 
1159 	/* Check for heart beat timeout conditions */
1160 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
1161 	tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1162 	if (!tmo_posted)
1163 		phba->pport->work_port_events |= WORKER_HB_TMO;
1164 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1165 
1166 	/* Tell the worker thread there is work to do */
1167 	if (!tmo_posted)
1168 		lpfc_worker_wake_up(phba);
1169 	return;
1170 }
1171 
1172 /**
1173  * lpfc_rrq_timeout - The RRQ-timer timeout handler
1174  * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1175  *
1176  * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1177  * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1178  * work-port-events bitmap and the worker thread is notified. This timeout
1179  * event will be used by the worker thread to invoke the actual timeout
1180  * handler routine, lpfc_rrq_handler. Any periodical operations will
1181  * be performed in the timeout handler and the RRQ timeout event bit shall
1182  * be cleared by the worker thread after it has taken the event bitmap out.
1183  **/
1184 static void
1185 lpfc_rrq_timeout(struct timer_list *t)
1186 {
1187 	struct lpfc_hba *phba;
1188 	unsigned long iflag;
1189 
1190 	phba = from_timer(phba, t, rrq_tmr);
1191 	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
1192 	if (!(phba->pport->load_flag & FC_UNLOADING))
1193 		phba->hba_flag |= HBA_RRQ_ACTIVE;
1194 	else
1195 		phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1196 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1197 
1198 	if (!(phba->pport->load_flag & FC_UNLOADING))
1199 		lpfc_worker_wake_up(phba);
1200 }
1201 
1202 /**
1203  * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
1204  * @phba: pointer to lpfc hba data structure.
1205  * @pmboxq: pointer to the driver internal queue element for mailbox command.
1206  *
1207  * This is the callback function to the lpfc heart-beat mailbox command.
1208  * If configured, the lpfc driver issues the heart-beat mailbox command to
1209  * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1210  * heart-beat mailbox command is issued, the driver shall set up heart-beat
1211  * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1212  * heart-beat outstanding state. Once the mailbox command comes back and
1213  * no error conditions detected, the heart-beat mailbox command timer is
1214  * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1215  * state is cleared for the next heart-beat. If the timer expired with the
1216  * heart-beat outstanding state set, the driver will put the HBA offline.
1217  **/
1218 static void
1219 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1220 {
1221 	unsigned long drvr_flag;
1222 
1223 	spin_lock_irqsave(&phba->hbalock, drvr_flag);
1224 	phba->hb_outstanding = 0;
1225 	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1226 
1227 	/* Check and reset heart-beat timer is necessary */
1228 	mempool_free(pmboxq, phba->mbox_mem_pool);
1229 	if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1230 		!(phba->link_state == LPFC_HBA_ERROR) &&
1231 		!(phba->pport->load_flag & FC_UNLOADING))
1232 		mod_timer(&phba->hb_tmofunc,
1233 			  jiffies +
1234 			  msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1235 	return;
1236 }
1237 
1238 /**
1239  * lpfc_hb_timeout_handler - The HBA-timer timeout handler
1240  * @phba: pointer to lpfc hba data structure.
1241  *
1242  * This is the actual HBA-timer timeout handler to be invoked by the worker
1243  * thread whenever the HBA timer fired and HBA-timeout event posted. This
1244  * handler performs any periodic operations needed for the device. If such
1245  * periodic event has already been attended to either in the interrupt handler
1246  * or by processing slow-ring or fast-ring events within the HBA-timer
1247  * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1248  * the timer for the next timeout period. If lpfc heart-beat mailbox command
1249  * is configured and there is no heart-beat mailbox command outstanding, a
1250  * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1251  * has been a heart-beat mailbox command outstanding, the HBA shall be put
1252  * to offline.
1253  **/
1254 void
1255 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1256 {
1257 	struct lpfc_vport **vports;
1258 	LPFC_MBOXQ_t *pmboxq;
1259 	struct lpfc_dmabuf *buf_ptr;
1260 	int retval, i;
1261 	struct lpfc_sli *psli = &phba->sli;
1262 	LIST_HEAD(completions);
1263 	struct lpfc_queue *qp;
1264 	unsigned long time_elapsed;
1265 	uint32_t tick_cqe, max_cqe, val;
1266 	uint64_t tot, data1, data2, data3;
1267 	struct lpfc_nvmet_tgtport *tgtp;
1268 	struct lpfc_register reg_data;
1269 	void __iomem *eqdreg = phba->sli4_hba.u.if_type2.EQDregaddr;
1270 
1271 	vports = lpfc_create_vport_work_array(phba);
1272 	if (vports != NULL)
1273 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1274 			lpfc_rcv_seq_check_edtov(vports[i]);
1275 			lpfc_fdmi_num_disc_check(vports[i]);
1276 		}
1277 	lpfc_destroy_vport_work_array(phba, vports);
1278 
1279 	if ((phba->link_state == LPFC_HBA_ERROR) ||
1280 		(phba->pport->load_flag & FC_UNLOADING) ||
1281 		(phba->pport->fc_flag & FC_OFFLINE_MODE))
1282 		return;
1283 
1284 	if (phba->cfg_auto_imax) {
1285 		if (!phba->last_eqdelay_time) {
1286 			phba->last_eqdelay_time = jiffies;
1287 			goto skip_eqdelay;
1288 		}
1289 		time_elapsed = jiffies - phba->last_eqdelay_time;
1290 		phba->last_eqdelay_time = jiffies;
1291 
1292 		tot = 0xffff;
1293 		/* Check outstanding IO count */
1294 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
1295 			if (phba->nvmet_support) {
1296 				tgtp = phba->targetport->private;
1297 				/* Calculate outstanding IOs */
1298 				tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
1299 				tot += atomic_read(&tgtp->xmt_fcp_release);
1300 				tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
1301 			} else {
1302 				tot = atomic_read(&phba->fc4NvmeIoCmpls);
1303 				data1 = atomic_read(
1304 					&phba->fc4NvmeInputRequests);
1305 				data2 = atomic_read(
1306 					&phba->fc4NvmeOutputRequests);
1307 				data3 = atomic_read(
1308 					&phba->fc4NvmeControlRequests);
1309 				tot =  (data1 + data2 + data3) - tot;
1310 			}
1311 		}
1312 
1313 		/* Interrupts per sec per EQ */
1314 		val = phba->cfg_fcp_imax / phba->io_channel_irqs;
1315 		tick_cqe = val / CONFIG_HZ; /* Per tick per EQ */
1316 
1317 		/* Assume 1 CQE/ISR, calc max CQEs allowed for time duration */
1318 		max_cqe = time_elapsed * tick_cqe;
1319 
1320 		for (i = 0; i < phba->io_channel_irqs; i++) {
1321 			/* Fast-path EQ */
1322 			qp = phba->sli4_hba.hba_eq[i];
1323 			if (!qp)
1324 				continue;
1325 
1326 			/* Use no EQ delay if we don't have many outstanding
1327 			 * IOs, or if we are only processing 1 CQE/ISR or less.
1328 			 * Otherwise, assume we can process up to lpfc_fcp_imax
1329 			 * interrupts per HBA.
1330 			 */
1331 			if (tot < LPFC_NODELAY_MAX_IO ||
1332 			    qp->EQ_cqe_cnt <= max_cqe)
1333 				val = 0;
1334 			else
1335 				val = phba->cfg_fcp_imax;
1336 
1337 			if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
1338 				/* Use EQ Delay Register method */
1339 
1340 				/* Convert for EQ Delay register */
1341 				if (val) {
1342 					/* First, interrupts per sec per EQ */
1343 					val = phba->cfg_fcp_imax /
1344 						phba->io_channel_irqs;
1345 
1346 					/* us delay between each interrupt */
1347 					val = LPFC_SEC_TO_USEC / val;
1348 				}
1349 				if (val != qp->q_mode) {
1350 					reg_data.word0 = 0;
1351 					bf_set(lpfc_sliport_eqdelay_id,
1352 					       &reg_data, qp->queue_id);
1353 					bf_set(lpfc_sliport_eqdelay_delay,
1354 					       &reg_data, val);
1355 					writel(reg_data.word0, eqdreg);
1356 				}
1357 			} else {
1358 				/* Use mbox command method */
1359 				if (val != qp->q_mode)
1360 					lpfc_modify_hba_eq_delay(phba, i,
1361 								 1, val);
1362 			}
1363 
1364 			/*
1365 			 * val is cfg_fcp_imax or 0 for mbox delay or us delay
1366 			 * between interrupts for EQDR.
1367 			 */
1368 			qp->q_mode = val;
1369 			qp->EQ_cqe_cnt = 0;
1370 		}
1371 	}
1372 
1373 skip_eqdelay:
1374 	spin_lock_irq(&phba->pport->work_port_lock);
1375 
1376 	if (time_after(phba->last_completion_time +
1377 			msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1378 			jiffies)) {
1379 		spin_unlock_irq(&phba->pport->work_port_lock);
1380 		if (!phba->hb_outstanding)
1381 			mod_timer(&phba->hb_tmofunc,
1382 				jiffies +
1383 				msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1384 		else
1385 			mod_timer(&phba->hb_tmofunc,
1386 				jiffies +
1387 				msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1388 		return;
1389 	}
1390 	spin_unlock_irq(&phba->pport->work_port_lock);
1391 
1392 	if (phba->elsbuf_cnt &&
1393 		(phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1394 		spin_lock_irq(&phba->hbalock);
1395 		list_splice_init(&phba->elsbuf, &completions);
1396 		phba->elsbuf_cnt = 0;
1397 		phba->elsbuf_prev_cnt = 0;
1398 		spin_unlock_irq(&phba->hbalock);
1399 
1400 		while (!list_empty(&completions)) {
1401 			list_remove_head(&completions, buf_ptr,
1402 				struct lpfc_dmabuf, list);
1403 			lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1404 			kfree(buf_ptr);
1405 		}
1406 	}
1407 	phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1408 
1409 	/* If there is no heart beat outstanding, issue a heartbeat command */
1410 	if (phba->cfg_enable_hba_heartbeat) {
1411 		if (!phba->hb_outstanding) {
1412 			if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1413 				(list_empty(&psli->mboxq))) {
1414 				pmboxq = mempool_alloc(phba->mbox_mem_pool,
1415 							GFP_KERNEL);
1416 				if (!pmboxq) {
1417 					mod_timer(&phba->hb_tmofunc,
1418 						 jiffies +
1419 						 msecs_to_jiffies(1000 *
1420 						 LPFC_HB_MBOX_INTERVAL));
1421 					return;
1422 				}
1423 
1424 				lpfc_heart_beat(phba, pmboxq);
1425 				pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1426 				pmboxq->vport = phba->pport;
1427 				retval = lpfc_sli_issue_mbox(phba, pmboxq,
1428 						MBX_NOWAIT);
1429 
1430 				if (retval != MBX_BUSY &&
1431 					retval != MBX_SUCCESS) {
1432 					mempool_free(pmboxq,
1433 							phba->mbox_mem_pool);
1434 					mod_timer(&phba->hb_tmofunc,
1435 						jiffies +
1436 						msecs_to_jiffies(1000 *
1437 						LPFC_HB_MBOX_INTERVAL));
1438 					return;
1439 				}
1440 				phba->skipped_hb = 0;
1441 				phba->hb_outstanding = 1;
1442 			} else if (time_before_eq(phba->last_completion_time,
1443 					phba->skipped_hb)) {
1444 				lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1445 					"2857 Last completion time not "
1446 					" updated in %d ms\n",
1447 					jiffies_to_msecs(jiffies
1448 						 - phba->last_completion_time));
1449 			} else
1450 				phba->skipped_hb = jiffies;
1451 
1452 			mod_timer(&phba->hb_tmofunc,
1453 				 jiffies +
1454 				 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1455 			return;
1456 		} else {
1457 			/*
1458 			* If heart beat timeout called with hb_outstanding set
1459 			* we need to give the hb mailbox cmd a chance to
1460 			* complete or TMO.
1461 			*/
1462 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1463 					"0459 Adapter heartbeat still out"
1464 					"standing:last compl time was %d ms.\n",
1465 					jiffies_to_msecs(jiffies
1466 						 - phba->last_completion_time));
1467 			mod_timer(&phba->hb_tmofunc,
1468 				jiffies +
1469 				msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
1470 		}
1471 	} else {
1472 			mod_timer(&phba->hb_tmofunc,
1473 				jiffies +
1474 				msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
1475 	}
1476 }
1477 
1478 /**
1479  * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1480  * @phba: pointer to lpfc hba data structure.
1481  *
1482  * This routine is called to bring the HBA offline when HBA hardware error
1483  * other than Port Error 6 has been detected.
1484  **/
1485 static void
1486 lpfc_offline_eratt(struct lpfc_hba *phba)
1487 {
1488 	struct lpfc_sli   *psli = &phba->sli;
1489 
1490 	spin_lock_irq(&phba->hbalock);
1491 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1492 	spin_unlock_irq(&phba->hbalock);
1493 	lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1494 
1495 	lpfc_offline(phba);
1496 	lpfc_reset_barrier(phba);
1497 	spin_lock_irq(&phba->hbalock);
1498 	lpfc_sli_brdreset(phba);
1499 	spin_unlock_irq(&phba->hbalock);
1500 	lpfc_hba_down_post(phba);
1501 	lpfc_sli_brdready(phba, HS_MBRDY);
1502 	lpfc_unblock_mgmt_io(phba);
1503 	phba->link_state = LPFC_HBA_ERROR;
1504 	return;
1505 }
1506 
1507 /**
1508  * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1509  * @phba: pointer to lpfc hba data structure.
1510  *
1511  * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1512  * other than Port Error 6 has been detected.
1513  **/
1514 void
1515 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1516 {
1517 	spin_lock_irq(&phba->hbalock);
1518 	phba->link_state = LPFC_HBA_ERROR;
1519 	spin_unlock_irq(&phba->hbalock);
1520 
1521 	lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1522 	lpfc_offline(phba);
1523 	lpfc_hba_down_post(phba);
1524 	lpfc_unblock_mgmt_io(phba);
1525 }
1526 
1527 /**
1528  * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1529  * @phba: pointer to lpfc hba data structure.
1530  *
1531  * This routine is invoked to handle the deferred HBA hardware error
1532  * conditions. This type of error is indicated by HBA by setting ER1
1533  * and another ER bit in the host status register. The driver will
1534  * wait until the ER1 bit clears before handling the error condition.
1535  **/
1536 static void
1537 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1538 {
1539 	uint32_t old_host_status = phba->work_hs;
1540 	struct lpfc_sli *psli = &phba->sli;
1541 
1542 	/* If the pci channel is offline, ignore possible errors,
1543 	 * since we cannot communicate with the pci card anyway.
1544 	 */
1545 	if (pci_channel_offline(phba->pcidev)) {
1546 		spin_lock_irq(&phba->hbalock);
1547 		phba->hba_flag &= ~DEFER_ERATT;
1548 		spin_unlock_irq(&phba->hbalock);
1549 		return;
1550 	}
1551 
1552 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1553 		"0479 Deferred Adapter Hardware Error "
1554 		"Data: x%x x%x x%x\n",
1555 		phba->work_hs,
1556 		phba->work_status[0], phba->work_status[1]);
1557 
1558 	spin_lock_irq(&phba->hbalock);
1559 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1560 	spin_unlock_irq(&phba->hbalock);
1561 
1562 
1563 	/*
1564 	 * Firmware stops when it triggred erratt. That could cause the I/Os
1565 	 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1566 	 * SCSI layer retry it after re-establishing link.
1567 	 */
1568 	lpfc_sli_abort_fcp_rings(phba);
1569 
1570 	/*
1571 	 * There was a firmware error. Take the hba offline and then
1572 	 * attempt to restart it.
1573 	 */
1574 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
1575 	lpfc_offline(phba);
1576 
1577 	/* Wait for the ER1 bit to clear.*/
1578 	while (phba->work_hs & HS_FFER1) {
1579 		msleep(100);
1580 		if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1581 			phba->work_hs = UNPLUG_ERR ;
1582 			break;
1583 		}
1584 		/* If driver is unloading let the worker thread continue */
1585 		if (phba->pport->load_flag & FC_UNLOADING) {
1586 			phba->work_hs = 0;
1587 			break;
1588 		}
1589 	}
1590 
1591 	/*
1592 	 * This is to ptrotect against a race condition in which
1593 	 * first write to the host attention register clear the
1594 	 * host status register.
1595 	 */
1596 	if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1597 		phba->work_hs = old_host_status & ~HS_FFER1;
1598 
1599 	spin_lock_irq(&phba->hbalock);
1600 	phba->hba_flag &= ~DEFER_ERATT;
1601 	spin_unlock_irq(&phba->hbalock);
1602 	phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1603 	phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1604 }
1605 
1606 static void
1607 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1608 {
1609 	struct lpfc_board_event_header board_event;
1610 	struct Scsi_Host *shost;
1611 
1612 	board_event.event_type = FC_REG_BOARD_EVENT;
1613 	board_event.subcategory = LPFC_EVENT_PORTINTERR;
1614 	shost = lpfc_shost_from_vport(phba->pport);
1615 	fc_host_post_vendor_event(shost, fc_get_event_number(),
1616 				  sizeof(board_event),
1617 				  (char *) &board_event,
1618 				  LPFC_NL_VENDOR_ID);
1619 }
1620 
1621 /**
1622  * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1623  * @phba: pointer to lpfc hba data structure.
1624  *
1625  * This routine is invoked to handle the following HBA hardware error
1626  * conditions:
1627  * 1 - HBA error attention interrupt
1628  * 2 - DMA ring index out of range
1629  * 3 - Mailbox command came back as unknown
1630  **/
1631 static void
1632 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1633 {
1634 	struct lpfc_vport *vport = phba->pport;
1635 	struct lpfc_sli   *psli = &phba->sli;
1636 	uint32_t event_data;
1637 	unsigned long temperature;
1638 	struct temp_event temp_event_data;
1639 	struct Scsi_Host  *shost;
1640 
1641 	/* If the pci channel is offline, ignore possible errors,
1642 	 * since we cannot communicate with the pci card anyway.
1643 	 */
1644 	if (pci_channel_offline(phba->pcidev)) {
1645 		spin_lock_irq(&phba->hbalock);
1646 		phba->hba_flag &= ~DEFER_ERATT;
1647 		spin_unlock_irq(&phba->hbalock);
1648 		return;
1649 	}
1650 
1651 	/* If resets are disabled then leave the HBA alone and return */
1652 	if (!phba->cfg_enable_hba_reset)
1653 		return;
1654 
1655 	/* Send an internal error event to mgmt application */
1656 	lpfc_board_errevt_to_mgmt(phba);
1657 
1658 	if (phba->hba_flag & DEFER_ERATT)
1659 		lpfc_handle_deferred_eratt(phba);
1660 
1661 	if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1662 		if (phba->work_hs & HS_FFER6)
1663 			/* Re-establishing Link */
1664 			lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1665 					"1301 Re-establishing Link "
1666 					"Data: x%x x%x x%x\n",
1667 					phba->work_hs, phba->work_status[0],
1668 					phba->work_status[1]);
1669 		if (phba->work_hs & HS_FFER8)
1670 			/* Device Zeroization */
1671 			lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1672 					"2861 Host Authentication device "
1673 					"zeroization Data:x%x x%x x%x\n",
1674 					phba->work_hs, phba->work_status[0],
1675 					phba->work_status[1]);
1676 
1677 		spin_lock_irq(&phba->hbalock);
1678 		psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1679 		spin_unlock_irq(&phba->hbalock);
1680 
1681 		/*
1682 		* Firmware stops when it triggled erratt with HS_FFER6.
1683 		* That could cause the I/Os dropped by the firmware.
1684 		* Error iocb (I/O) on txcmplq and let the SCSI layer
1685 		* retry it after re-establishing link.
1686 		*/
1687 		lpfc_sli_abort_fcp_rings(phba);
1688 
1689 		/*
1690 		 * There was a firmware error.  Take the hba offline and then
1691 		 * attempt to restart it.
1692 		 */
1693 		lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
1694 		lpfc_offline(phba);
1695 		lpfc_sli_brdrestart(phba);
1696 		if (lpfc_online(phba) == 0) {	/* Initialize the HBA */
1697 			lpfc_unblock_mgmt_io(phba);
1698 			return;
1699 		}
1700 		lpfc_unblock_mgmt_io(phba);
1701 	} else if (phba->work_hs & HS_CRIT_TEMP) {
1702 		temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1703 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1704 		temp_event_data.event_code = LPFC_CRIT_TEMP;
1705 		temp_event_data.data = (uint32_t)temperature;
1706 
1707 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1708 				"0406 Adapter maximum temperature exceeded "
1709 				"(%ld), taking this port offline "
1710 				"Data: x%x x%x x%x\n",
1711 				temperature, phba->work_hs,
1712 				phba->work_status[0], phba->work_status[1]);
1713 
1714 		shost = lpfc_shost_from_vport(phba->pport);
1715 		fc_host_post_vendor_event(shost, fc_get_event_number(),
1716 					  sizeof(temp_event_data),
1717 					  (char *) &temp_event_data,
1718 					  SCSI_NL_VID_TYPE_PCI
1719 					  | PCI_VENDOR_ID_EMULEX);
1720 
1721 		spin_lock_irq(&phba->hbalock);
1722 		phba->over_temp_state = HBA_OVER_TEMP;
1723 		spin_unlock_irq(&phba->hbalock);
1724 		lpfc_offline_eratt(phba);
1725 
1726 	} else {
1727 		/* The if clause above forces this code path when the status
1728 		 * failure is a value other than FFER6. Do not call the offline
1729 		 * twice. This is the adapter hardware error path.
1730 		 */
1731 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1732 				"0457 Adapter Hardware Error "
1733 				"Data: x%x x%x x%x\n",
1734 				phba->work_hs,
1735 				phba->work_status[0], phba->work_status[1]);
1736 
1737 		event_data = FC_REG_DUMP_EVENT;
1738 		shost = lpfc_shost_from_vport(vport);
1739 		fc_host_post_vendor_event(shost, fc_get_event_number(),
1740 				sizeof(event_data), (char *) &event_data,
1741 				SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1742 
1743 		lpfc_offline_eratt(phba);
1744 	}
1745 	return;
1746 }
1747 
1748 /**
1749  * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1750  * @phba: pointer to lpfc hba data structure.
1751  * @mbx_action: flag for mailbox shutdown action.
1752  *
1753  * This routine is invoked to perform an SLI4 port PCI function reset in
1754  * response to port status register polling attention. It waits for port
1755  * status register (ERR, RDY, RN) bits before proceeding with function reset.
1756  * During this process, interrupt vectors are freed and later requested
1757  * for handling possible port resource change.
1758  **/
1759 static int
1760 lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1761 			    bool en_rn_msg)
1762 {
1763 	int rc;
1764 	uint32_t intr_mode;
1765 
1766 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
1767 	    LPFC_SLI_INTF_IF_TYPE_2) {
1768 		/*
1769 		 * On error status condition, driver need to wait for port
1770 		 * ready before performing reset.
1771 		 */
1772 		rc = lpfc_sli4_pdev_status_reg_wait(phba);
1773 		if (rc)
1774 			return rc;
1775 	}
1776 
1777 	/* need reset: attempt for port recovery */
1778 	if (en_rn_msg)
1779 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1780 				"2887 Reset Needed: Attempting Port "
1781 				"Recovery...\n");
1782 	lpfc_offline_prep(phba, mbx_action);
1783 	lpfc_offline(phba);
1784 	/* release interrupt for possible resource change */
1785 	lpfc_sli4_disable_intr(phba);
1786 	lpfc_sli_brdrestart(phba);
1787 	/* request and enable interrupt */
1788 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1789 	if (intr_mode == LPFC_INTR_ERROR) {
1790 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1791 				"3175 Failed to enable interrupt\n");
1792 		return -EIO;
1793 	}
1794 	phba->intr_mode = intr_mode;
1795 	rc = lpfc_online(phba);
1796 	if (rc == 0)
1797 		lpfc_unblock_mgmt_io(phba);
1798 
1799 	return rc;
1800 }
1801 
1802 /**
1803  * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1804  * @phba: pointer to lpfc hba data structure.
1805  *
1806  * This routine is invoked to handle the SLI4 HBA hardware error attention
1807  * conditions.
1808  **/
1809 static void
1810 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1811 {
1812 	struct lpfc_vport *vport = phba->pport;
1813 	uint32_t event_data;
1814 	struct Scsi_Host *shost;
1815 	uint32_t if_type;
1816 	struct lpfc_register portstat_reg = {0};
1817 	uint32_t reg_err1, reg_err2;
1818 	uint32_t uerrlo_reg, uemasklo_reg;
1819 	uint32_t smphr_port_status = 0, pci_rd_rc1, pci_rd_rc2;
1820 	bool en_rn_msg = true;
1821 	struct temp_event temp_event_data;
1822 	struct lpfc_register portsmphr_reg;
1823 	int rc, i;
1824 
1825 	/* If the pci channel is offline, ignore possible errors, since
1826 	 * we cannot communicate with the pci card anyway.
1827 	 */
1828 	if (pci_channel_offline(phba->pcidev))
1829 		return;
1830 
1831 	memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
1832 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1833 	switch (if_type) {
1834 	case LPFC_SLI_INTF_IF_TYPE_0:
1835 		pci_rd_rc1 = lpfc_readl(
1836 				phba->sli4_hba.u.if_type0.UERRLOregaddr,
1837 				&uerrlo_reg);
1838 		pci_rd_rc2 = lpfc_readl(
1839 				phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1840 				&uemasklo_reg);
1841 		/* consider PCI bus read error as pci_channel_offline */
1842 		if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1843 			return;
1844 		if (!(phba->hba_flag & HBA_RECOVERABLE_UE)) {
1845 			lpfc_sli4_offline_eratt(phba);
1846 			return;
1847 		}
1848 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1849 				"7623 Checking UE recoverable");
1850 
1851 		for (i = 0; i < phba->sli4_hba.ue_to_sr / 1000; i++) {
1852 			if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1853 				       &portsmphr_reg.word0))
1854 				continue;
1855 
1856 			smphr_port_status = bf_get(lpfc_port_smphr_port_status,
1857 						   &portsmphr_reg);
1858 			if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1859 			    LPFC_PORT_SEM_UE_RECOVERABLE)
1860 				break;
1861 			/*Sleep for 1Sec, before checking SEMAPHORE */
1862 			msleep(1000);
1863 		}
1864 
1865 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1866 				"4827 smphr_port_status x%x : Waited %dSec",
1867 				smphr_port_status, i);
1868 
1869 		/* Recoverable UE, reset the HBA device */
1870 		if ((smphr_port_status & LPFC_PORT_SEM_MASK) ==
1871 		    LPFC_PORT_SEM_UE_RECOVERABLE) {
1872 			for (i = 0; i < 20; i++) {
1873 				msleep(1000);
1874 				if (!lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
1875 				    &portsmphr_reg.word0) &&
1876 				    (LPFC_POST_STAGE_PORT_READY ==
1877 				     bf_get(lpfc_port_smphr_port_status,
1878 				     &portsmphr_reg))) {
1879 					rc = lpfc_sli4_port_sta_fn_reset(phba,
1880 						LPFC_MBX_NO_WAIT, en_rn_msg);
1881 					if (rc == 0)
1882 						return;
1883 					lpfc_printf_log(phba,
1884 						KERN_ERR, LOG_INIT,
1885 						"4215 Failed to recover UE");
1886 					break;
1887 				}
1888 			}
1889 		}
1890 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1891 				"7624 Firmware not ready: Failing UE recovery,"
1892 				" waited %dSec", i);
1893 		lpfc_sli4_offline_eratt(phba);
1894 		break;
1895 
1896 	case LPFC_SLI_INTF_IF_TYPE_2:
1897 	case LPFC_SLI_INTF_IF_TYPE_6:
1898 		pci_rd_rc1 = lpfc_readl(
1899 				phba->sli4_hba.u.if_type2.STATUSregaddr,
1900 				&portstat_reg.word0);
1901 		/* consider PCI bus read error as pci_channel_offline */
1902 		if (pci_rd_rc1 == -EIO) {
1903 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1904 				"3151 PCI bus read access failure: x%x\n",
1905 				readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
1906 			return;
1907 		}
1908 		reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1909 		reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
1910 		if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1911 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1912 				"2889 Port Overtemperature event, "
1913 				"taking port offline Data: x%x x%x\n",
1914 				reg_err1, reg_err2);
1915 
1916 			phba->sfp_alarm |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
1917 			temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1918 			temp_event_data.event_code = LPFC_CRIT_TEMP;
1919 			temp_event_data.data = 0xFFFFFFFF;
1920 
1921 			shost = lpfc_shost_from_vport(phba->pport);
1922 			fc_host_post_vendor_event(shost, fc_get_event_number(),
1923 						  sizeof(temp_event_data),
1924 						  (char *)&temp_event_data,
1925 						  SCSI_NL_VID_TYPE_PCI
1926 						  | PCI_VENDOR_ID_EMULEX);
1927 
1928 			spin_lock_irq(&phba->hbalock);
1929 			phba->over_temp_state = HBA_OVER_TEMP;
1930 			spin_unlock_irq(&phba->hbalock);
1931 			lpfc_sli4_offline_eratt(phba);
1932 			return;
1933 		}
1934 		if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1935 		    reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
1936 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1937 					"3143 Port Down: Firmware Update "
1938 					"Detected\n");
1939 			en_rn_msg = false;
1940 		} else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1941 			 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1942 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1943 					"3144 Port Down: Debug Dump\n");
1944 		else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1945 			 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1946 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1947 					"3145 Port Down: Provisioning\n");
1948 
1949 		/* If resets are disabled then leave the HBA alone and return */
1950 		if (!phba->cfg_enable_hba_reset)
1951 			return;
1952 
1953 		/* Check port status register for function reset */
1954 		rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1955 				en_rn_msg);
1956 		if (rc == 0) {
1957 			/* don't report event on forced debug dump */
1958 			if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1959 			    reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1960 				return;
1961 			else
1962 				break;
1963 		}
1964 		/* fall through for not able to recover */
1965 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1966 				"3152 Unrecoverable error, bring the port "
1967 				"offline\n");
1968 		lpfc_sli4_offline_eratt(phba);
1969 		break;
1970 	case LPFC_SLI_INTF_IF_TYPE_1:
1971 	default:
1972 		break;
1973 	}
1974 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1975 			"3123 Report dump event to upper layer\n");
1976 	/* Send an internal error event to mgmt application */
1977 	lpfc_board_errevt_to_mgmt(phba);
1978 
1979 	event_data = FC_REG_DUMP_EVENT;
1980 	shost = lpfc_shost_from_vport(vport);
1981 	fc_host_post_vendor_event(shost, fc_get_event_number(),
1982 				  sizeof(event_data), (char *) &event_data,
1983 				  SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1984 }
1985 
1986 /**
1987  * lpfc_handle_eratt - Wrapper func for handling hba error attention
1988  * @phba: pointer to lpfc HBA data structure.
1989  *
1990  * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1991  * routine from the API jump table function pointer from the lpfc_hba struct.
1992  *
1993  * Return codes
1994  *   0 - success.
1995  *   Any other value - error.
1996  **/
1997 void
1998 lpfc_handle_eratt(struct lpfc_hba *phba)
1999 {
2000 	(*phba->lpfc_handle_eratt)(phba);
2001 }
2002 
2003 /**
2004  * lpfc_handle_latt - The HBA link event handler
2005  * @phba: pointer to lpfc hba data structure.
2006  *
2007  * This routine is invoked from the worker thread to handle a HBA host
2008  * attention link event. SLI3 only.
2009  **/
2010 void
2011 lpfc_handle_latt(struct lpfc_hba *phba)
2012 {
2013 	struct lpfc_vport *vport = phba->pport;
2014 	struct lpfc_sli   *psli = &phba->sli;
2015 	LPFC_MBOXQ_t *pmb;
2016 	volatile uint32_t control;
2017 	struct lpfc_dmabuf *mp;
2018 	int rc = 0;
2019 
2020 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2021 	if (!pmb) {
2022 		rc = 1;
2023 		goto lpfc_handle_latt_err_exit;
2024 	}
2025 
2026 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2027 	if (!mp) {
2028 		rc = 2;
2029 		goto lpfc_handle_latt_free_pmb;
2030 	}
2031 
2032 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
2033 	if (!mp->virt) {
2034 		rc = 3;
2035 		goto lpfc_handle_latt_free_mp;
2036 	}
2037 
2038 	/* Cleanup any outstanding ELS commands */
2039 	lpfc_els_flush_all_cmd(phba);
2040 
2041 	psli->slistat.link_event++;
2042 	lpfc_read_topology(phba, pmb, mp);
2043 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
2044 	pmb->vport = vport;
2045 	/* Block ELS IOCBs until we have processed this mbox command */
2046 	phba->sli.sli3_ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
2047 	rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
2048 	if (rc == MBX_NOT_FINISHED) {
2049 		rc = 4;
2050 		goto lpfc_handle_latt_free_mbuf;
2051 	}
2052 
2053 	/* Clear Link Attention in HA REG */
2054 	spin_lock_irq(&phba->hbalock);
2055 	writel(HA_LATT, phba->HAregaddr);
2056 	readl(phba->HAregaddr); /* flush */
2057 	spin_unlock_irq(&phba->hbalock);
2058 
2059 	return;
2060 
2061 lpfc_handle_latt_free_mbuf:
2062 	phba->sli.sli3_ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
2063 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2064 lpfc_handle_latt_free_mp:
2065 	kfree(mp);
2066 lpfc_handle_latt_free_pmb:
2067 	mempool_free(pmb, phba->mbox_mem_pool);
2068 lpfc_handle_latt_err_exit:
2069 	/* Enable Link attention interrupts */
2070 	spin_lock_irq(&phba->hbalock);
2071 	psli->sli_flag |= LPFC_PROCESS_LA;
2072 	control = readl(phba->HCregaddr);
2073 	control |= HC_LAINT_ENA;
2074 	writel(control, phba->HCregaddr);
2075 	readl(phba->HCregaddr); /* flush */
2076 
2077 	/* Clear Link Attention in HA REG */
2078 	writel(HA_LATT, phba->HAregaddr);
2079 	readl(phba->HAregaddr); /* flush */
2080 	spin_unlock_irq(&phba->hbalock);
2081 	lpfc_linkdown(phba);
2082 	phba->link_state = LPFC_HBA_ERROR;
2083 
2084 	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
2085 		     "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
2086 
2087 	return;
2088 }
2089 
2090 /**
2091  * lpfc_parse_vpd - Parse VPD (Vital Product Data)
2092  * @phba: pointer to lpfc hba data structure.
2093  * @vpd: pointer to the vital product data.
2094  * @len: length of the vital product data in bytes.
2095  *
2096  * This routine parses the Vital Product Data (VPD). The VPD is treated as
2097  * an array of characters. In this routine, the ModelName, ProgramType, and
2098  * ModelDesc, etc. fields of the phba data structure will be populated.
2099  *
2100  * Return codes
2101  *   0 - pointer to the VPD passed in is NULL
2102  *   1 - success
2103  **/
2104 int
2105 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
2106 {
2107 	uint8_t lenlo, lenhi;
2108 	int Length;
2109 	int i, j;
2110 	int finished = 0;
2111 	int index = 0;
2112 
2113 	if (!vpd)
2114 		return 0;
2115 
2116 	/* Vital Product */
2117 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2118 			"0455 Vital Product Data: x%x x%x x%x x%x\n",
2119 			(uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
2120 			(uint32_t) vpd[3]);
2121 	while (!finished && (index < (len - 4))) {
2122 		switch (vpd[index]) {
2123 		case 0x82:
2124 		case 0x91:
2125 			index += 1;
2126 			lenlo = vpd[index];
2127 			index += 1;
2128 			lenhi = vpd[index];
2129 			index += 1;
2130 			i = ((((unsigned short)lenhi) << 8) + lenlo);
2131 			index += i;
2132 			break;
2133 		case 0x90:
2134 			index += 1;
2135 			lenlo = vpd[index];
2136 			index += 1;
2137 			lenhi = vpd[index];
2138 			index += 1;
2139 			Length = ((((unsigned short)lenhi) << 8) + lenlo);
2140 			if (Length > len - index)
2141 				Length = len - index;
2142 			while (Length > 0) {
2143 			/* Look for Serial Number */
2144 			if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
2145 				index += 2;
2146 				i = vpd[index];
2147 				index += 1;
2148 				j = 0;
2149 				Length -= (3+i);
2150 				while(i--) {
2151 					phba->SerialNumber[j++] = vpd[index++];
2152 					if (j == 31)
2153 						break;
2154 				}
2155 				phba->SerialNumber[j] = 0;
2156 				continue;
2157 			}
2158 			else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
2159 				phba->vpd_flag |= VPD_MODEL_DESC;
2160 				index += 2;
2161 				i = vpd[index];
2162 				index += 1;
2163 				j = 0;
2164 				Length -= (3+i);
2165 				while(i--) {
2166 					phba->ModelDesc[j++] = vpd[index++];
2167 					if (j == 255)
2168 						break;
2169 				}
2170 				phba->ModelDesc[j] = 0;
2171 				continue;
2172 			}
2173 			else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
2174 				phba->vpd_flag |= VPD_MODEL_NAME;
2175 				index += 2;
2176 				i = vpd[index];
2177 				index += 1;
2178 				j = 0;
2179 				Length -= (3+i);
2180 				while(i--) {
2181 					phba->ModelName[j++] = vpd[index++];
2182 					if (j == 79)
2183 						break;
2184 				}
2185 				phba->ModelName[j] = 0;
2186 				continue;
2187 			}
2188 			else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
2189 				phba->vpd_flag |= VPD_PROGRAM_TYPE;
2190 				index += 2;
2191 				i = vpd[index];
2192 				index += 1;
2193 				j = 0;
2194 				Length -= (3+i);
2195 				while(i--) {
2196 					phba->ProgramType[j++] = vpd[index++];
2197 					if (j == 255)
2198 						break;
2199 				}
2200 				phba->ProgramType[j] = 0;
2201 				continue;
2202 			}
2203 			else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
2204 				phba->vpd_flag |= VPD_PORT;
2205 				index += 2;
2206 				i = vpd[index];
2207 				index += 1;
2208 				j = 0;
2209 				Length -= (3+i);
2210 				while(i--) {
2211 					if ((phba->sli_rev == LPFC_SLI_REV4) &&
2212 					    (phba->sli4_hba.pport_name_sta ==
2213 					     LPFC_SLI4_PPNAME_GET)) {
2214 						j++;
2215 						index++;
2216 					} else
2217 						phba->Port[j++] = vpd[index++];
2218 					if (j == 19)
2219 						break;
2220 				}
2221 				if ((phba->sli_rev != LPFC_SLI_REV4) ||
2222 				    (phba->sli4_hba.pport_name_sta ==
2223 				     LPFC_SLI4_PPNAME_NON))
2224 					phba->Port[j] = 0;
2225 				continue;
2226 			}
2227 			else {
2228 				index += 2;
2229 				i = vpd[index];
2230 				index += 1;
2231 				index += i;
2232 				Length -= (3 + i);
2233 			}
2234 		}
2235 		finished = 0;
2236 		break;
2237 		case 0x78:
2238 			finished = 1;
2239 			break;
2240 		default:
2241 			index ++;
2242 			break;
2243 		}
2244 	}
2245 
2246 	return(1);
2247 }
2248 
2249 /**
2250  * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
2251  * @phba: pointer to lpfc hba data structure.
2252  * @mdp: pointer to the data structure to hold the derived model name.
2253  * @descp: pointer to the data structure to hold the derived description.
2254  *
2255  * This routine retrieves HBA's description based on its registered PCI device
2256  * ID. The @descp passed into this function points to an array of 256 chars. It
2257  * shall be returned with the model name, maximum speed, and the host bus type.
2258  * The @mdp passed into this function points to an array of 80 chars. When the
2259  * function returns, the @mdp will be filled with the model name.
2260  **/
2261 static void
2262 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
2263 {
2264 	lpfc_vpd_t *vp;
2265 	uint16_t dev_id = phba->pcidev->device;
2266 	int max_speed;
2267 	int GE = 0;
2268 	int oneConnect = 0; /* default is not a oneConnect */
2269 	struct {
2270 		char *name;
2271 		char *bus;
2272 		char *function;
2273 	} m = {"<Unknown>", "", ""};
2274 
2275 	if (mdp && mdp[0] != '\0'
2276 		&& descp && descp[0] != '\0')
2277 		return;
2278 
2279 	if (phba->lmt & LMT_64Gb)
2280 		max_speed = 64;
2281 	else if (phba->lmt & LMT_32Gb)
2282 		max_speed = 32;
2283 	else if (phba->lmt & LMT_16Gb)
2284 		max_speed = 16;
2285 	else if (phba->lmt & LMT_10Gb)
2286 		max_speed = 10;
2287 	else if (phba->lmt & LMT_8Gb)
2288 		max_speed = 8;
2289 	else if (phba->lmt & LMT_4Gb)
2290 		max_speed = 4;
2291 	else if (phba->lmt & LMT_2Gb)
2292 		max_speed = 2;
2293 	else if (phba->lmt & LMT_1Gb)
2294 		max_speed = 1;
2295 	else
2296 		max_speed = 0;
2297 
2298 	vp = &phba->vpd;
2299 
2300 	switch (dev_id) {
2301 	case PCI_DEVICE_ID_FIREFLY:
2302 		m = (typeof(m)){"LP6000", "PCI",
2303 				"Obsolete, Unsupported Fibre Channel Adapter"};
2304 		break;
2305 	case PCI_DEVICE_ID_SUPERFLY:
2306 		if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
2307 			m = (typeof(m)){"LP7000", "PCI", ""};
2308 		else
2309 			m = (typeof(m)){"LP7000E", "PCI", ""};
2310 		m.function = "Obsolete, Unsupported Fibre Channel Adapter";
2311 		break;
2312 	case PCI_DEVICE_ID_DRAGONFLY:
2313 		m = (typeof(m)){"LP8000", "PCI",
2314 				"Obsolete, Unsupported Fibre Channel Adapter"};
2315 		break;
2316 	case PCI_DEVICE_ID_CENTAUR:
2317 		if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
2318 			m = (typeof(m)){"LP9002", "PCI", ""};
2319 		else
2320 			m = (typeof(m)){"LP9000", "PCI", ""};
2321 		m.function = "Obsolete, Unsupported Fibre Channel Adapter";
2322 		break;
2323 	case PCI_DEVICE_ID_RFLY:
2324 		m = (typeof(m)){"LP952", "PCI",
2325 				"Obsolete, Unsupported Fibre Channel Adapter"};
2326 		break;
2327 	case PCI_DEVICE_ID_PEGASUS:
2328 		m = (typeof(m)){"LP9802", "PCI-X",
2329 				"Obsolete, Unsupported Fibre Channel Adapter"};
2330 		break;
2331 	case PCI_DEVICE_ID_THOR:
2332 		m = (typeof(m)){"LP10000", "PCI-X",
2333 				"Obsolete, Unsupported Fibre Channel Adapter"};
2334 		break;
2335 	case PCI_DEVICE_ID_VIPER:
2336 		m = (typeof(m)){"LPX1000",  "PCI-X",
2337 				"Obsolete, Unsupported Fibre Channel Adapter"};
2338 		break;
2339 	case PCI_DEVICE_ID_PFLY:
2340 		m = (typeof(m)){"LP982", "PCI-X",
2341 				"Obsolete, Unsupported Fibre Channel Adapter"};
2342 		break;
2343 	case PCI_DEVICE_ID_TFLY:
2344 		m = (typeof(m)){"LP1050", "PCI-X",
2345 				"Obsolete, Unsupported Fibre Channel Adapter"};
2346 		break;
2347 	case PCI_DEVICE_ID_HELIOS:
2348 		m = (typeof(m)){"LP11000", "PCI-X2",
2349 				"Obsolete, Unsupported Fibre Channel Adapter"};
2350 		break;
2351 	case PCI_DEVICE_ID_HELIOS_SCSP:
2352 		m = (typeof(m)){"LP11000-SP", "PCI-X2",
2353 				"Obsolete, Unsupported Fibre Channel Adapter"};
2354 		break;
2355 	case PCI_DEVICE_ID_HELIOS_DCSP:
2356 		m = (typeof(m)){"LP11002-SP",  "PCI-X2",
2357 				"Obsolete, Unsupported Fibre Channel Adapter"};
2358 		break;
2359 	case PCI_DEVICE_ID_NEPTUNE:
2360 		m = (typeof(m)){"LPe1000", "PCIe",
2361 				"Obsolete, Unsupported Fibre Channel Adapter"};
2362 		break;
2363 	case PCI_DEVICE_ID_NEPTUNE_SCSP:
2364 		m = (typeof(m)){"LPe1000-SP", "PCIe",
2365 				"Obsolete, Unsupported Fibre Channel Adapter"};
2366 		break;
2367 	case PCI_DEVICE_ID_NEPTUNE_DCSP:
2368 		m = (typeof(m)){"LPe1002-SP", "PCIe",
2369 				"Obsolete, Unsupported Fibre Channel Adapter"};
2370 		break;
2371 	case PCI_DEVICE_ID_BMID:
2372 		m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
2373 		break;
2374 	case PCI_DEVICE_ID_BSMB:
2375 		m = (typeof(m)){"LP111", "PCI-X2",
2376 				"Obsolete, Unsupported Fibre Channel Adapter"};
2377 		break;
2378 	case PCI_DEVICE_ID_ZEPHYR:
2379 		m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
2380 		break;
2381 	case PCI_DEVICE_ID_ZEPHYR_SCSP:
2382 		m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
2383 		break;
2384 	case PCI_DEVICE_ID_ZEPHYR_DCSP:
2385 		m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
2386 		GE = 1;
2387 		break;
2388 	case PCI_DEVICE_ID_ZMID:
2389 		m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
2390 		break;
2391 	case PCI_DEVICE_ID_ZSMB:
2392 		m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
2393 		break;
2394 	case PCI_DEVICE_ID_LP101:
2395 		m = (typeof(m)){"LP101", "PCI-X",
2396 				"Obsolete, Unsupported Fibre Channel Adapter"};
2397 		break;
2398 	case PCI_DEVICE_ID_LP10000S:
2399 		m = (typeof(m)){"LP10000-S", "PCI",
2400 				"Obsolete, Unsupported Fibre Channel Adapter"};
2401 		break;
2402 	case PCI_DEVICE_ID_LP11000S:
2403 		m = (typeof(m)){"LP11000-S", "PCI-X2",
2404 				"Obsolete, Unsupported Fibre Channel Adapter"};
2405 		break;
2406 	case PCI_DEVICE_ID_LPE11000S:
2407 		m = (typeof(m)){"LPe11000-S", "PCIe",
2408 				"Obsolete, Unsupported Fibre Channel Adapter"};
2409 		break;
2410 	case PCI_DEVICE_ID_SAT:
2411 		m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
2412 		break;
2413 	case PCI_DEVICE_ID_SAT_MID:
2414 		m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
2415 		break;
2416 	case PCI_DEVICE_ID_SAT_SMB:
2417 		m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
2418 		break;
2419 	case PCI_DEVICE_ID_SAT_DCSP:
2420 		m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
2421 		break;
2422 	case PCI_DEVICE_ID_SAT_SCSP:
2423 		m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
2424 		break;
2425 	case PCI_DEVICE_ID_SAT_S:
2426 		m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
2427 		break;
2428 	case PCI_DEVICE_ID_HORNET:
2429 		m = (typeof(m)){"LP21000", "PCIe",
2430 				"Obsolete, Unsupported FCoE Adapter"};
2431 		GE = 1;
2432 		break;
2433 	case PCI_DEVICE_ID_PROTEUS_VF:
2434 		m = (typeof(m)){"LPev12000", "PCIe IOV",
2435 				"Obsolete, Unsupported Fibre Channel Adapter"};
2436 		break;
2437 	case PCI_DEVICE_ID_PROTEUS_PF:
2438 		m = (typeof(m)){"LPev12000", "PCIe IOV",
2439 				"Obsolete, Unsupported Fibre Channel Adapter"};
2440 		break;
2441 	case PCI_DEVICE_ID_PROTEUS_S:
2442 		m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2443 				"Obsolete, Unsupported Fibre Channel Adapter"};
2444 		break;
2445 	case PCI_DEVICE_ID_TIGERSHARK:
2446 		oneConnect = 1;
2447 		m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
2448 		break;
2449 	case PCI_DEVICE_ID_TOMCAT:
2450 		oneConnect = 1;
2451 		m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2452 		break;
2453 	case PCI_DEVICE_ID_FALCON:
2454 		m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2455 				"EmulexSecure Fibre"};
2456 		break;
2457 	case PCI_DEVICE_ID_BALIUS:
2458 		m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2459 				"Obsolete, Unsupported Fibre Channel Adapter"};
2460 		break;
2461 	case PCI_DEVICE_ID_LANCER_FC:
2462 		m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
2463 		break;
2464 	case PCI_DEVICE_ID_LANCER_FC_VF:
2465 		m = (typeof(m)){"LPe16000", "PCIe",
2466 				"Obsolete, Unsupported Fibre Channel Adapter"};
2467 		break;
2468 	case PCI_DEVICE_ID_LANCER_FCOE:
2469 		oneConnect = 1;
2470 		m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
2471 		break;
2472 	case PCI_DEVICE_ID_LANCER_FCOE_VF:
2473 		oneConnect = 1;
2474 		m = (typeof(m)){"OCe15100", "PCIe",
2475 				"Obsolete, Unsupported FCoE"};
2476 		break;
2477 	case PCI_DEVICE_ID_LANCER_G6_FC:
2478 		m = (typeof(m)){"LPe32000", "PCIe", "Fibre Channel Adapter"};
2479 		break;
2480 	case PCI_DEVICE_ID_LANCER_G7_FC:
2481 		m = (typeof(m)){"LPe36000", "PCIe", "Fibre Channel Adapter"};
2482 		break;
2483 	case PCI_DEVICE_ID_SKYHAWK:
2484 	case PCI_DEVICE_ID_SKYHAWK_VF:
2485 		oneConnect = 1;
2486 		m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2487 		break;
2488 	default:
2489 		m = (typeof(m)){"Unknown", "", ""};
2490 		break;
2491 	}
2492 
2493 	if (mdp && mdp[0] == '\0')
2494 		snprintf(mdp, 79,"%s", m.name);
2495 	/*
2496 	 * oneConnect hba requires special processing, they are all initiators
2497 	 * and we put the port number on the end
2498 	 */
2499 	if (descp && descp[0] == '\0') {
2500 		if (oneConnect)
2501 			snprintf(descp, 255,
2502 				"Emulex OneConnect %s, %s Initiator %s",
2503 				m.name, m.function,
2504 				phba->Port);
2505 		else if (max_speed == 0)
2506 			snprintf(descp, 255,
2507 				"Emulex %s %s %s",
2508 				m.name, m.bus, m.function);
2509 		else
2510 			snprintf(descp, 255,
2511 				"Emulex %s %d%s %s %s",
2512 				m.name, max_speed, (GE) ? "GE" : "Gb",
2513 				m.bus, m.function);
2514 	}
2515 }
2516 
2517 /**
2518  * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
2519  * @phba: pointer to lpfc hba data structure.
2520  * @pring: pointer to a IOCB ring.
2521  * @cnt: the number of IOCBs to be posted to the IOCB ring.
2522  *
2523  * This routine posts a given number of IOCBs with the associated DMA buffer
2524  * descriptors specified by the cnt argument to the given IOCB ring.
2525  *
2526  * Return codes
2527  *   The number of IOCBs NOT able to be posted to the IOCB ring.
2528  **/
2529 int
2530 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
2531 {
2532 	IOCB_t *icmd;
2533 	struct lpfc_iocbq *iocb;
2534 	struct lpfc_dmabuf *mp1, *mp2;
2535 
2536 	cnt += pring->missbufcnt;
2537 
2538 	/* While there are buffers to post */
2539 	while (cnt > 0) {
2540 		/* Allocate buffer for  command iocb */
2541 		iocb = lpfc_sli_get_iocbq(phba);
2542 		if (iocb == NULL) {
2543 			pring->missbufcnt = cnt;
2544 			return cnt;
2545 		}
2546 		icmd = &iocb->iocb;
2547 
2548 		/* 2 buffers can be posted per command */
2549 		/* Allocate buffer to post */
2550 		mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2551 		if (mp1)
2552 		    mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2553 		if (!mp1 || !mp1->virt) {
2554 			kfree(mp1);
2555 			lpfc_sli_release_iocbq(phba, iocb);
2556 			pring->missbufcnt = cnt;
2557 			return cnt;
2558 		}
2559 
2560 		INIT_LIST_HEAD(&mp1->list);
2561 		/* Allocate buffer to post */
2562 		if (cnt > 1) {
2563 			mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2564 			if (mp2)
2565 				mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2566 							    &mp2->phys);
2567 			if (!mp2 || !mp2->virt) {
2568 				kfree(mp2);
2569 				lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2570 				kfree(mp1);
2571 				lpfc_sli_release_iocbq(phba, iocb);
2572 				pring->missbufcnt = cnt;
2573 				return cnt;
2574 			}
2575 
2576 			INIT_LIST_HEAD(&mp2->list);
2577 		} else {
2578 			mp2 = NULL;
2579 		}
2580 
2581 		icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2582 		icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2583 		icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2584 		icmd->ulpBdeCount = 1;
2585 		cnt--;
2586 		if (mp2) {
2587 			icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2588 			icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2589 			icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2590 			cnt--;
2591 			icmd->ulpBdeCount = 2;
2592 		}
2593 
2594 		icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2595 		icmd->ulpLe = 1;
2596 
2597 		if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2598 		    IOCB_ERROR) {
2599 			lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2600 			kfree(mp1);
2601 			cnt++;
2602 			if (mp2) {
2603 				lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2604 				kfree(mp2);
2605 				cnt++;
2606 			}
2607 			lpfc_sli_release_iocbq(phba, iocb);
2608 			pring->missbufcnt = cnt;
2609 			return cnt;
2610 		}
2611 		lpfc_sli_ringpostbuf_put(phba, pring, mp1);
2612 		if (mp2)
2613 			lpfc_sli_ringpostbuf_put(phba, pring, mp2);
2614 	}
2615 	pring->missbufcnt = 0;
2616 	return 0;
2617 }
2618 
2619 /**
2620  * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
2621  * @phba: pointer to lpfc hba data structure.
2622  *
2623  * This routine posts initial receive IOCB buffers to the ELS ring. The
2624  * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2625  * set to 64 IOCBs. SLI3 only.
2626  *
2627  * Return codes
2628  *   0 - success (currently always success)
2629  **/
2630 static int
2631 lpfc_post_rcv_buf(struct lpfc_hba *phba)
2632 {
2633 	struct lpfc_sli *psli = &phba->sli;
2634 
2635 	/* Ring 0, ELS / CT buffers */
2636 	lpfc_post_buffer(phba, &psli->sli3_ring[LPFC_ELS_RING], LPFC_BUF_RING0);
2637 	/* Ring 2 - FCP no buffers needed */
2638 
2639 	return 0;
2640 }
2641 
2642 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2643 
2644 /**
2645  * lpfc_sha_init - Set up initial array of hash table entries
2646  * @HashResultPointer: pointer to an array as hash table.
2647  *
2648  * This routine sets up the initial values to the array of hash table entries
2649  * for the LC HBAs.
2650  **/
2651 static void
2652 lpfc_sha_init(uint32_t * HashResultPointer)
2653 {
2654 	HashResultPointer[0] = 0x67452301;
2655 	HashResultPointer[1] = 0xEFCDAB89;
2656 	HashResultPointer[2] = 0x98BADCFE;
2657 	HashResultPointer[3] = 0x10325476;
2658 	HashResultPointer[4] = 0xC3D2E1F0;
2659 }
2660 
2661 /**
2662  * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2663  * @HashResultPointer: pointer to an initial/result hash table.
2664  * @HashWorkingPointer: pointer to an working hash table.
2665  *
2666  * This routine iterates an initial hash table pointed by @HashResultPointer
2667  * with the values from the working hash table pointeed by @HashWorkingPointer.
2668  * The results are putting back to the initial hash table, returned through
2669  * the @HashResultPointer as the result hash table.
2670  **/
2671 static void
2672 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2673 {
2674 	int t;
2675 	uint32_t TEMP;
2676 	uint32_t A, B, C, D, E;
2677 	t = 16;
2678 	do {
2679 		HashWorkingPointer[t] =
2680 		    S(1,
2681 		      HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2682 								     8] ^
2683 		      HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2684 	} while (++t <= 79);
2685 	t = 0;
2686 	A = HashResultPointer[0];
2687 	B = HashResultPointer[1];
2688 	C = HashResultPointer[2];
2689 	D = HashResultPointer[3];
2690 	E = HashResultPointer[4];
2691 
2692 	do {
2693 		if (t < 20) {
2694 			TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2695 		} else if (t < 40) {
2696 			TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2697 		} else if (t < 60) {
2698 			TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2699 		} else {
2700 			TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2701 		}
2702 		TEMP += S(5, A) + E + HashWorkingPointer[t];
2703 		E = D;
2704 		D = C;
2705 		C = S(30, B);
2706 		B = A;
2707 		A = TEMP;
2708 	} while (++t <= 79);
2709 
2710 	HashResultPointer[0] += A;
2711 	HashResultPointer[1] += B;
2712 	HashResultPointer[2] += C;
2713 	HashResultPointer[3] += D;
2714 	HashResultPointer[4] += E;
2715 
2716 }
2717 
2718 /**
2719  * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2720  * @RandomChallenge: pointer to the entry of host challenge random number array.
2721  * @HashWorking: pointer to the entry of the working hash array.
2722  *
2723  * This routine calculates the working hash array referred by @HashWorking
2724  * from the challenge random numbers associated with the host, referred by
2725  * @RandomChallenge. The result is put into the entry of the working hash
2726  * array and returned by reference through @HashWorking.
2727  **/
2728 static void
2729 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2730 {
2731 	*HashWorking = (*RandomChallenge ^ *HashWorking);
2732 }
2733 
2734 /**
2735  * lpfc_hba_init - Perform special handling for LC HBA initialization
2736  * @phba: pointer to lpfc hba data structure.
2737  * @hbainit: pointer to an array of unsigned 32-bit integers.
2738  *
2739  * This routine performs the special handling for LC HBA initialization.
2740  **/
2741 void
2742 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2743 {
2744 	int t;
2745 	uint32_t *HashWorking;
2746 	uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2747 
2748 	HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2749 	if (!HashWorking)
2750 		return;
2751 
2752 	HashWorking[0] = HashWorking[78] = *pwwnn++;
2753 	HashWorking[1] = HashWorking[79] = *pwwnn;
2754 
2755 	for (t = 0; t < 7; t++)
2756 		lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2757 
2758 	lpfc_sha_init(hbainit);
2759 	lpfc_sha_iterate(hbainit, HashWorking);
2760 	kfree(HashWorking);
2761 }
2762 
2763 /**
2764  * lpfc_cleanup - Performs vport cleanups before deleting a vport
2765  * @vport: pointer to a virtual N_Port data structure.
2766  *
2767  * This routine performs the necessary cleanups before deleting the @vport.
2768  * It invokes the discovery state machine to perform necessary state
2769  * transitions and to release the ndlps associated with the @vport. Note,
2770  * the physical port is treated as @vport 0.
2771  **/
2772 void
2773 lpfc_cleanup(struct lpfc_vport *vport)
2774 {
2775 	struct lpfc_hba   *phba = vport->phba;
2776 	struct lpfc_nodelist *ndlp, *next_ndlp;
2777 	int i = 0;
2778 
2779 	if (phba->link_state > LPFC_LINK_DOWN)
2780 		lpfc_port_link_failure(vport);
2781 
2782 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2783 		if (!NLP_CHK_NODE_ACT(ndlp)) {
2784 			ndlp = lpfc_enable_node(vport, ndlp,
2785 						NLP_STE_UNUSED_NODE);
2786 			if (!ndlp)
2787 				continue;
2788 			spin_lock_irq(&phba->ndlp_lock);
2789 			NLP_SET_FREE_REQ(ndlp);
2790 			spin_unlock_irq(&phba->ndlp_lock);
2791 			/* Trigger the release of the ndlp memory */
2792 			lpfc_nlp_put(ndlp);
2793 			continue;
2794 		}
2795 		spin_lock_irq(&phba->ndlp_lock);
2796 		if (NLP_CHK_FREE_REQ(ndlp)) {
2797 			/* The ndlp should not be in memory free mode already */
2798 			spin_unlock_irq(&phba->ndlp_lock);
2799 			continue;
2800 		} else
2801 			/* Indicate request for freeing ndlp memory */
2802 			NLP_SET_FREE_REQ(ndlp);
2803 		spin_unlock_irq(&phba->ndlp_lock);
2804 
2805 		if (vport->port_type != LPFC_PHYSICAL_PORT &&
2806 		    ndlp->nlp_DID == Fabric_DID) {
2807 			/* Just free up ndlp with Fabric_DID for vports */
2808 			lpfc_nlp_put(ndlp);
2809 			continue;
2810 		}
2811 
2812 		/* take care of nodes in unused state before the state
2813 		 * machine taking action.
2814 		 */
2815 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2816 			lpfc_nlp_put(ndlp);
2817 			continue;
2818 		}
2819 
2820 		if (ndlp->nlp_type & NLP_FABRIC)
2821 			lpfc_disc_state_machine(vport, ndlp, NULL,
2822 					NLP_EVT_DEVICE_RECOVERY);
2823 
2824 		lpfc_disc_state_machine(vport, ndlp, NULL,
2825 					     NLP_EVT_DEVICE_RM);
2826 	}
2827 
2828 	/* At this point, ALL ndlp's should be gone
2829 	 * because of the previous NLP_EVT_DEVICE_RM.
2830 	 * Lets wait for this to happen, if needed.
2831 	 */
2832 	while (!list_empty(&vport->fc_nodes)) {
2833 		if (i++ > 3000) {
2834 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2835 				"0233 Nodelist not empty\n");
2836 			list_for_each_entry_safe(ndlp, next_ndlp,
2837 						&vport->fc_nodes, nlp_listp) {
2838 				lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2839 						LOG_NODE,
2840 						"0282 did:x%x ndlp:x%p "
2841 						"usgmap:x%x refcnt:%d\n",
2842 						ndlp->nlp_DID, (void *)ndlp,
2843 						ndlp->nlp_usg_map,
2844 						kref_read(&ndlp->kref));
2845 			}
2846 			break;
2847 		}
2848 
2849 		/* Wait for any activity on ndlps to settle */
2850 		msleep(10);
2851 	}
2852 	lpfc_cleanup_vports_rrqs(vport, NULL);
2853 }
2854 
2855 /**
2856  * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2857  * @vport: pointer to a virtual N_Port data structure.
2858  *
2859  * This routine stops all the timers associated with a @vport. This function
2860  * is invoked before disabling or deleting a @vport. Note that the physical
2861  * port is treated as @vport 0.
2862  **/
2863 void
2864 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2865 {
2866 	del_timer_sync(&vport->els_tmofunc);
2867 	del_timer_sync(&vport->delayed_disc_tmo);
2868 	lpfc_can_disctmo(vport);
2869 	return;
2870 }
2871 
2872 /**
2873  * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2874  * @phba: pointer to lpfc hba data structure.
2875  *
2876  * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2877  * caller of this routine should already hold the host lock.
2878  **/
2879 void
2880 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2881 {
2882 	/* Clear pending FCF rediscovery wait flag */
2883 	phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2884 
2885 	/* Now, try to stop the timer */
2886 	del_timer(&phba->fcf.redisc_wait);
2887 }
2888 
2889 /**
2890  * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2891  * @phba: pointer to lpfc hba data structure.
2892  *
2893  * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2894  * checks whether the FCF rediscovery wait timer is pending with the host
2895  * lock held before proceeding with disabling the timer and clearing the
2896  * wait timer pendig flag.
2897  **/
2898 void
2899 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2900 {
2901 	spin_lock_irq(&phba->hbalock);
2902 	if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2903 		/* FCF rediscovery timer already fired or stopped */
2904 		spin_unlock_irq(&phba->hbalock);
2905 		return;
2906 	}
2907 	__lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2908 	/* Clear failover in progress flags */
2909 	phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
2910 	spin_unlock_irq(&phba->hbalock);
2911 }
2912 
2913 /**
2914  * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2915  * @phba: pointer to lpfc hba data structure.
2916  *
2917  * This routine stops all the timers associated with a HBA. This function is
2918  * invoked before either putting a HBA offline or unloading the driver.
2919  **/
2920 void
2921 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2922 {
2923 	lpfc_stop_vport_timers(phba->pport);
2924 	del_timer_sync(&phba->sli.mbox_tmo);
2925 	del_timer_sync(&phba->fabric_block_timer);
2926 	del_timer_sync(&phba->eratt_poll);
2927 	del_timer_sync(&phba->hb_tmofunc);
2928 	if (phba->sli_rev == LPFC_SLI_REV4) {
2929 		del_timer_sync(&phba->rrq_tmr);
2930 		phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2931 	}
2932 	phba->hb_outstanding = 0;
2933 
2934 	switch (phba->pci_dev_grp) {
2935 	case LPFC_PCI_DEV_LP:
2936 		/* Stop any LightPulse device specific driver timers */
2937 		del_timer_sync(&phba->fcp_poll_timer);
2938 		break;
2939 	case LPFC_PCI_DEV_OC:
2940 		/* Stop any OneConnect device sepcific driver timers */
2941 		lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2942 		break;
2943 	default:
2944 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2945 				"0297 Invalid device group (x%x)\n",
2946 				phba->pci_dev_grp);
2947 		break;
2948 	}
2949 	return;
2950 }
2951 
2952 /**
2953  * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2954  * @phba: pointer to lpfc hba data structure.
2955  *
2956  * This routine marks a HBA's management interface as blocked. Once the HBA's
2957  * management interface is marked as blocked, all the user space access to
2958  * the HBA, whether they are from sysfs interface or libdfc interface will
2959  * all be blocked. The HBA is set to block the management interface when the
2960  * driver prepares the HBA interface for online or offline.
2961  **/
2962 static void
2963 lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
2964 {
2965 	unsigned long iflag;
2966 	uint8_t actcmd = MBX_HEARTBEAT;
2967 	unsigned long timeout;
2968 
2969 	spin_lock_irqsave(&phba->hbalock, iflag);
2970 	phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2971 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2972 	if (mbx_action == LPFC_MBX_NO_WAIT)
2973 		return;
2974 	timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2975 	spin_lock_irqsave(&phba->hbalock, iflag);
2976 	if (phba->sli.mbox_active) {
2977 		actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
2978 		/* Determine how long we might wait for the active mailbox
2979 		 * command to be gracefully completed by firmware.
2980 		 */
2981 		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2982 				phba->sli.mbox_active) * 1000) + jiffies;
2983 	}
2984 	spin_unlock_irqrestore(&phba->hbalock, iflag);
2985 
2986 	/* Wait for the outstnading mailbox command to complete */
2987 	while (phba->sli.mbox_active) {
2988 		/* Check active mailbox complete status every 2ms */
2989 		msleep(2);
2990 		if (time_after(jiffies, timeout)) {
2991 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2992 				"2813 Mgmt IO is Blocked %x "
2993 				"- mbox cmd %x still active\n",
2994 				phba->sli.sli_flag, actcmd);
2995 			break;
2996 		}
2997 	}
2998 }
2999 
3000 /**
3001  * lpfc_sli4_node_prep - Assign RPIs for active nodes.
3002  * @phba: pointer to lpfc hba data structure.
3003  *
3004  * Allocate RPIs for all active remote nodes. This is needed whenever
3005  * an SLI4 adapter is reset and the driver is not unloading. Its purpose
3006  * is to fixup the temporary rpi assignments.
3007  **/
3008 void
3009 lpfc_sli4_node_prep(struct lpfc_hba *phba)
3010 {
3011 	struct lpfc_nodelist  *ndlp, *next_ndlp;
3012 	struct lpfc_vport **vports;
3013 	int i, rpi;
3014 	unsigned long flags;
3015 
3016 	if (phba->sli_rev != LPFC_SLI_REV4)
3017 		return;
3018 
3019 	vports = lpfc_create_vport_work_array(phba);
3020 	if (vports == NULL)
3021 		return;
3022 
3023 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3024 		if (vports[i]->load_flag & FC_UNLOADING)
3025 			continue;
3026 
3027 		list_for_each_entry_safe(ndlp, next_ndlp,
3028 					 &vports[i]->fc_nodes,
3029 					 nlp_listp) {
3030 			if (!NLP_CHK_NODE_ACT(ndlp))
3031 				continue;
3032 			rpi = lpfc_sli4_alloc_rpi(phba);
3033 			if (rpi == LPFC_RPI_ALLOC_ERROR) {
3034 				spin_lock_irqsave(&phba->ndlp_lock, flags);
3035 				NLP_CLR_NODE_ACT(ndlp);
3036 				spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3037 				continue;
3038 			}
3039 			ndlp->nlp_rpi = rpi;
3040 			lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3041 					 "0009 rpi:%x DID:%x "
3042 					 "flg:%x map:%x %p\n", ndlp->nlp_rpi,
3043 					 ndlp->nlp_DID, ndlp->nlp_flag,
3044 					 ndlp->nlp_usg_map, ndlp);
3045 		}
3046 	}
3047 	lpfc_destroy_vport_work_array(phba, vports);
3048 }
3049 
3050 /**
3051  * lpfc_online - Initialize and bring a HBA online
3052  * @phba: pointer to lpfc hba data structure.
3053  *
3054  * This routine initializes the HBA and brings a HBA online. During this
3055  * process, the management interface is blocked to prevent user space access
3056  * to the HBA interfering with the driver initialization.
3057  *
3058  * Return codes
3059  *   0 - successful
3060  *   1 - failed
3061  **/
3062 int
3063 lpfc_online(struct lpfc_hba *phba)
3064 {
3065 	struct lpfc_vport *vport;
3066 	struct lpfc_vport **vports;
3067 	int i, error = 0;
3068 	bool vpis_cleared = false;
3069 
3070 	if (!phba)
3071 		return 0;
3072 	vport = phba->pport;
3073 
3074 	if (!(vport->fc_flag & FC_OFFLINE_MODE))
3075 		return 0;
3076 
3077 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3078 			"0458 Bring Adapter online\n");
3079 
3080 	lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
3081 
3082 	if (phba->sli_rev == LPFC_SLI_REV4) {
3083 		if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
3084 			lpfc_unblock_mgmt_io(phba);
3085 			return 1;
3086 		}
3087 		spin_lock_irq(&phba->hbalock);
3088 		if (!phba->sli4_hba.max_cfg_param.vpi_used)
3089 			vpis_cleared = true;
3090 		spin_unlock_irq(&phba->hbalock);
3091 
3092 		/* Reestablish the local initiator port.
3093 		 * The offline process destroyed the previous lport.
3094 		 */
3095 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME &&
3096 				!phba->nvmet_support) {
3097 			error = lpfc_nvme_create_localport(phba->pport);
3098 			if (error)
3099 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3100 					"6132 NVME restore reg failed "
3101 					"on nvmei error x%x\n", error);
3102 		}
3103 	} else {
3104 		lpfc_sli_queue_init(phba);
3105 		if (lpfc_sli_hba_setup(phba)) {	/* Initialize SLI2/SLI3 HBA */
3106 			lpfc_unblock_mgmt_io(phba);
3107 			return 1;
3108 		}
3109 	}
3110 
3111 	vports = lpfc_create_vport_work_array(phba);
3112 	if (vports != NULL) {
3113 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3114 			struct Scsi_Host *shost;
3115 			shost = lpfc_shost_from_vport(vports[i]);
3116 			spin_lock_irq(shost->host_lock);
3117 			vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
3118 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3119 				vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3120 			if (phba->sli_rev == LPFC_SLI_REV4) {
3121 				vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
3122 				if ((vpis_cleared) &&
3123 				    (vports[i]->port_type !=
3124 					LPFC_PHYSICAL_PORT))
3125 					vports[i]->vpi = 0;
3126 			}
3127 			spin_unlock_irq(shost->host_lock);
3128 		}
3129 	}
3130 	lpfc_destroy_vport_work_array(phba, vports);
3131 
3132 	lpfc_unblock_mgmt_io(phba);
3133 	return 0;
3134 }
3135 
3136 /**
3137  * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
3138  * @phba: pointer to lpfc hba data structure.
3139  *
3140  * This routine marks a HBA's management interface as not blocked. Once the
3141  * HBA's management interface is marked as not blocked, all the user space
3142  * access to the HBA, whether they are from sysfs interface or libdfc
3143  * interface will be allowed. The HBA is set to block the management interface
3144  * when the driver prepares the HBA interface for online or offline and then
3145  * set to unblock the management interface afterwards.
3146  **/
3147 void
3148 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
3149 {
3150 	unsigned long iflag;
3151 
3152 	spin_lock_irqsave(&phba->hbalock, iflag);
3153 	phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
3154 	spin_unlock_irqrestore(&phba->hbalock, iflag);
3155 }
3156 
3157 /**
3158  * lpfc_offline_prep - Prepare a HBA to be brought offline
3159  * @phba: pointer to lpfc hba data structure.
3160  *
3161  * This routine is invoked to prepare a HBA to be brought offline. It performs
3162  * unregistration login to all the nodes on all vports and flushes the mailbox
3163  * queue to make it ready to be brought offline.
3164  **/
3165 void
3166 lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
3167 {
3168 	struct lpfc_vport *vport = phba->pport;
3169 	struct lpfc_nodelist  *ndlp, *next_ndlp;
3170 	struct lpfc_vport **vports;
3171 	struct Scsi_Host *shost;
3172 	int i;
3173 
3174 	if (vport->fc_flag & FC_OFFLINE_MODE)
3175 		return;
3176 
3177 	lpfc_block_mgmt_io(phba, mbx_action);
3178 
3179 	lpfc_linkdown(phba);
3180 
3181 	/* Issue an unreg_login to all nodes on all vports */
3182 	vports = lpfc_create_vport_work_array(phba);
3183 	if (vports != NULL) {
3184 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3185 			if (vports[i]->load_flag & FC_UNLOADING)
3186 				continue;
3187 			shost = lpfc_shost_from_vport(vports[i]);
3188 			spin_lock_irq(shost->host_lock);
3189 			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
3190 			vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3191 			vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
3192 			spin_unlock_irq(shost->host_lock);
3193 
3194 			shost =	lpfc_shost_from_vport(vports[i]);
3195 			list_for_each_entry_safe(ndlp, next_ndlp,
3196 						 &vports[i]->fc_nodes,
3197 						 nlp_listp) {
3198 				if (!NLP_CHK_NODE_ACT(ndlp))
3199 					continue;
3200 				if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3201 					continue;
3202 				if (ndlp->nlp_type & NLP_FABRIC) {
3203 					lpfc_disc_state_machine(vports[i], ndlp,
3204 						NULL, NLP_EVT_DEVICE_RECOVERY);
3205 					lpfc_disc_state_machine(vports[i], ndlp,
3206 						NULL, NLP_EVT_DEVICE_RM);
3207 				}
3208 				spin_lock_irq(shost->host_lock);
3209 				ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3210 				spin_unlock_irq(shost->host_lock);
3211 				/*
3212 				 * Whenever an SLI4 port goes offline, free the
3213 				 * RPI. Get a new RPI when the adapter port
3214 				 * comes back online.
3215 				 */
3216 				if (phba->sli_rev == LPFC_SLI_REV4) {
3217 					lpfc_printf_vlog(ndlp->vport,
3218 							 KERN_INFO, LOG_NODE,
3219 							 "0011 lpfc_offline: "
3220 							 "ndlp:x%p did %x "
3221 							 "usgmap:x%x rpi:%x\n",
3222 							 ndlp, ndlp->nlp_DID,
3223 							 ndlp->nlp_usg_map,
3224 							 ndlp->nlp_rpi);
3225 
3226 					lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
3227 				}
3228 				lpfc_unreg_rpi(vports[i], ndlp);
3229 			}
3230 		}
3231 	}
3232 	lpfc_destroy_vport_work_array(phba, vports);
3233 
3234 	lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
3235 
3236 	if (phba->wq)
3237 		flush_workqueue(phba->wq);
3238 }
3239 
3240 /**
3241  * lpfc_offline - Bring a HBA offline
3242  * @phba: pointer to lpfc hba data structure.
3243  *
3244  * This routine actually brings a HBA offline. It stops all the timers
3245  * associated with the HBA, brings down the SLI layer, and eventually
3246  * marks the HBA as in offline state for the upper layer protocol.
3247  **/
3248 void
3249 lpfc_offline(struct lpfc_hba *phba)
3250 {
3251 	struct Scsi_Host  *shost;
3252 	struct lpfc_vport **vports;
3253 	int i;
3254 
3255 	if (phba->pport->fc_flag & FC_OFFLINE_MODE)
3256 		return;
3257 
3258 	/* stop port and all timers associated with this hba */
3259 	lpfc_stop_port(phba);
3260 
3261 	/* Tear down the local and target port registrations.  The
3262 	 * nvme transports need to cleanup.
3263 	 */
3264 	lpfc_nvmet_destroy_targetport(phba);
3265 	lpfc_nvme_destroy_localport(phba->pport);
3266 
3267 	vports = lpfc_create_vport_work_array(phba);
3268 	if (vports != NULL)
3269 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3270 			lpfc_stop_vport_timers(vports[i]);
3271 	lpfc_destroy_vport_work_array(phba, vports);
3272 	lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3273 			"0460 Bring Adapter offline\n");
3274 	/* Bring down the SLI Layer and cleanup.  The HBA is offline
3275 	   now.  */
3276 	lpfc_sli_hba_down(phba);
3277 	spin_lock_irq(&phba->hbalock);
3278 	phba->work_ha = 0;
3279 	spin_unlock_irq(&phba->hbalock);
3280 	vports = lpfc_create_vport_work_array(phba);
3281 	if (vports != NULL)
3282 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3283 			shost = lpfc_shost_from_vport(vports[i]);
3284 			spin_lock_irq(shost->host_lock);
3285 			vports[i]->work_port_events = 0;
3286 			vports[i]->fc_flag |= FC_OFFLINE_MODE;
3287 			spin_unlock_irq(shost->host_lock);
3288 		}
3289 	lpfc_destroy_vport_work_array(phba, vports);
3290 }
3291 
3292 /**
3293  * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
3294  * @phba: pointer to lpfc hba data structure.
3295  *
3296  * This routine is to free all the SCSI buffers and IOCBs from the driver
3297  * list back to kernel. It is called from lpfc_pci_remove_one to free
3298  * the internal resources before the device is removed from the system.
3299  **/
3300 static void
3301 lpfc_scsi_free(struct lpfc_hba *phba)
3302 {
3303 	struct lpfc_scsi_buf *sb, *sb_next;
3304 
3305 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3306 		return;
3307 
3308 	spin_lock_irq(&phba->hbalock);
3309 
3310 	/* Release all the lpfc_scsi_bufs maintained by this host. */
3311 
3312 	spin_lock(&phba->scsi_buf_list_put_lock);
3313 	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3314 				 list) {
3315 		list_del(&sb->list);
3316 		dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
3317 			      sb->dma_handle);
3318 		kfree(sb);
3319 		phba->total_scsi_bufs--;
3320 	}
3321 	spin_unlock(&phba->scsi_buf_list_put_lock);
3322 
3323 	spin_lock(&phba->scsi_buf_list_get_lock);
3324 	list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3325 				 list) {
3326 		list_del(&sb->list);
3327 		dma_pool_free(phba->lpfc_sg_dma_buf_pool, sb->data,
3328 			      sb->dma_handle);
3329 		kfree(sb);
3330 		phba->total_scsi_bufs--;
3331 	}
3332 	spin_unlock(&phba->scsi_buf_list_get_lock);
3333 	spin_unlock_irq(&phba->hbalock);
3334 }
3335 /**
3336  * lpfc_nvme_free - Free all the NVME buffers and IOCBs from driver lists
3337  * @phba: pointer to lpfc hba data structure.
3338  *
3339  * This routine is to free all the NVME buffers and IOCBs from the driver
3340  * list back to kernel. It is called from lpfc_pci_remove_one to free
3341  * the internal resources before the device is removed from the system.
3342  **/
3343 static void
3344 lpfc_nvme_free(struct lpfc_hba *phba)
3345 {
3346 	struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
3347 
3348 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3349 		return;
3350 
3351 	spin_lock_irq(&phba->hbalock);
3352 
3353 	/* Release all the lpfc_nvme_bufs maintained by this host. */
3354 	spin_lock(&phba->nvme_buf_list_put_lock);
3355 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3356 				 &phba->lpfc_nvme_buf_list_put, list) {
3357 		list_del(&lpfc_ncmd->list);
3358 		phba->put_nvme_bufs--;
3359 		dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3360 			      lpfc_ncmd->dma_handle);
3361 		kfree(lpfc_ncmd);
3362 		phba->total_nvme_bufs--;
3363 	}
3364 	spin_unlock(&phba->nvme_buf_list_put_lock);
3365 
3366 	spin_lock(&phba->nvme_buf_list_get_lock);
3367 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3368 				 &phba->lpfc_nvme_buf_list_get, list) {
3369 		list_del(&lpfc_ncmd->list);
3370 		phba->get_nvme_bufs--;
3371 		dma_pool_free(phba->lpfc_sg_dma_buf_pool, lpfc_ncmd->data,
3372 			      lpfc_ncmd->dma_handle);
3373 		kfree(lpfc_ncmd);
3374 		phba->total_nvme_bufs--;
3375 	}
3376 	spin_unlock(&phba->nvme_buf_list_get_lock);
3377 	spin_unlock_irq(&phba->hbalock);
3378 }
3379 /**
3380  * lpfc_sli4_els_sgl_update - update ELS xri-sgl sizing and mapping
3381  * @phba: pointer to lpfc hba data structure.
3382  *
3383  * This routine first calculates the sizes of the current els and allocated
3384  * scsi sgl lists, and then goes through all sgls to updates the physical
3385  * XRIs assigned due to port function reset. During port initialization, the
3386  * current els and allocated scsi sgl lists are 0s.
3387  *
3388  * Return codes
3389  *   0 - successful (for now, it always returns 0)
3390  **/
3391 int
3392 lpfc_sli4_els_sgl_update(struct lpfc_hba *phba)
3393 {
3394 	struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3395 	uint16_t i, lxri, xri_cnt, els_xri_cnt;
3396 	LIST_HEAD(els_sgl_list);
3397 	int rc;
3398 
3399 	/*
3400 	 * update on pci function's els xri-sgl list
3401 	 */
3402 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3403 
3404 	if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3405 		/* els xri-sgl expanded */
3406 		xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3407 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3408 				"3157 ELS xri-sgl count increased from "
3409 				"%d to %d\n", phba->sli4_hba.els_xri_cnt,
3410 				els_xri_cnt);
3411 		/* allocate the additional els sgls */
3412 		for (i = 0; i < xri_cnt; i++) {
3413 			sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3414 					     GFP_KERNEL);
3415 			if (sglq_entry == NULL) {
3416 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3417 						"2562 Failure to allocate an "
3418 						"ELS sgl entry:%d\n", i);
3419 				rc = -ENOMEM;
3420 				goto out_free_mem;
3421 			}
3422 			sglq_entry->buff_type = GEN_BUFF_TYPE;
3423 			sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3424 							   &sglq_entry->phys);
3425 			if (sglq_entry->virt == NULL) {
3426 				kfree(sglq_entry);
3427 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3428 						"2563 Failure to allocate an "
3429 						"ELS mbuf:%d\n", i);
3430 				rc = -ENOMEM;
3431 				goto out_free_mem;
3432 			}
3433 			sglq_entry->sgl = sglq_entry->virt;
3434 			memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3435 			sglq_entry->state = SGL_FREED;
3436 			list_add_tail(&sglq_entry->list, &els_sgl_list);
3437 		}
3438 		spin_lock_irq(&phba->hbalock);
3439 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3440 		list_splice_init(&els_sgl_list,
3441 				 &phba->sli4_hba.lpfc_els_sgl_list);
3442 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3443 		spin_unlock_irq(&phba->hbalock);
3444 	} else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3445 		/* els xri-sgl shrinked */
3446 		xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3447 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3448 				"3158 ELS xri-sgl count decreased from "
3449 				"%d to %d\n", phba->sli4_hba.els_xri_cnt,
3450 				els_xri_cnt);
3451 		spin_lock_irq(&phba->hbalock);
3452 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3453 		list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list,
3454 				 &els_sgl_list);
3455 		/* release extra els sgls from list */
3456 		for (i = 0; i < xri_cnt; i++) {
3457 			list_remove_head(&els_sgl_list,
3458 					 sglq_entry, struct lpfc_sglq, list);
3459 			if (sglq_entry) {
3460 				__lpfc_mbuf_free(phba, sglq_entry->virt,
3461 						 sglq_entry->phys);
3462 				kfree(sglq_entry);
3463 			}
3464 		}
3465 		list_splice_init(&els_sgl_list,
3466 				 &phba->sli4_hba.lpfc_els_sgl_list);
3467 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3468 		spin_unlock_irq(&phba->hbalock);
3469 	} else
3470 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3471 				"3163 ELS xri-sgl count unchanged: %d\n",
3472 				els_xri_cnt);
3473 	phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3474 
3475 	/* update xris to els sgls on the list */
3476 	sglq_entry = NULL;
3477 	sglq_entry_next = NULL;
3478 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3479 				 &phba->sli4_hba.lpfc_els_sgl_list, list) {
3480 		lxri = lpfc_sli4_next_xritag(phba);
3481 		if (lxri == NO_XRI) {
3482 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3483 					"2400 Failed to allocate xri for "
3484 					"ELS sgl\n");
3485 			rc = -ENOMEM;
3486 			goto out_free_mem;
3487 		}
3488 		sglq_entry->sli4_lxritag = lxri;
3489 		sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3490 	}
3491 	return 0;
3492 
3493 out_free_mem:
3494 	lpfc_free_els_sgl_list(phba);
3495 	return rc;
3496 }
3497 
3498 /**
3499  * lpfc_sli4_nvmet_sgl_update - update xri-sgl sizing and mapping
3500  * @phba: pointer to lpfc hba data structure.
3501  *
3502  * This routine first calculates the sizes of the current els and allocated
3503  * scsi sgl lists, and then goes through all sgls to updates the physical
3504  * XRIs assigned due to port function reset. During port initialization, the
3505  * current els and allocated scsi sgl lists are 0s.
3506  *
3507  * Return codes
3508  *   0 - successful (for now, it always returns 0)
3509  **/
3510 int
3511 lpfc_sli4_nvmet_sgl_update(struct lpfc_hba *phba)
3512 {
3513 	struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3514 	uint16_t i, lxri, xri_cnt, els_xri_cnt;
3515 	uint16_t nvmet_xri_cnt;
3516 	LIST_HEAD(nvmet_sgl_list);
3517 	int rc;
3518 
3519 	/*
3520 	 * update on pci function's nvmet xri-sgl list
3521 	 */
3522 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3523 
3524 	/* For NVMET, ALL remaining XRIs are dedicated for IO processing */
3525 	nvmet_xri_cnt = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3526 	if (nvmet_xri_cnt > phba->sli4_hba.nvmet_xri_cnt) {
3527 		/* els xri-sgl expanded */
3528 		xri_cnt = nvmet_xri_cnt - phba->sli4_hba.nvmet_xri_cnt;
3529 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3530 				"6302 NVMET xri-sgl cnt grew from %d to %d\n",
3531 				phba->sli4_hba.nvmet_xri_cnt, nvmet_xri_cnt);
3532 		/* allocate the additional nvmet sgls */
3533 		for (i = 0; i < xri_cnt; i++) {
3534 			sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3535 					     GFP_KERNEL);
3536 			if (sglq_entry == NULL) {
3537 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3538 						"6303 Failure to allocate an "
3539 						"NVMET sgl entry:%d\n", i);
3540 				rc = -ENOMEM;
3541 				goto out_free_mem;
3542 			}
3543 			sglq_entry->buff_type = NVMET_BUFF_TYPE;
3544 			sglq_entry->virt = lpfc_nvmet_buf_alloc(phba, 0,
3545 							   &sglq_entry->phys);
3546 			if (sglq_entry->virt == NULL) {
3547 				kfree(sglq_entry);
3548 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3549 						"6304 Failure to allocate an "
3550 						"NVMET buf:%d\n", i);
3551 				rc = -ENOMEM;
3552 				goto out_free_mem;
3553 			}
3554 			sglq_entry->sgl = sglq_entry->virt;
3555 			memset(sglq_entry->sgl, 0,
3556 			       phba->cfg_sg_dma_buf_size);
3557 			sglq_entry->state = SGL_FREED;
3558 			list_add_tail(&sglq_entry->list, &nvmet_sgl_list);
3559 		}
3560 		spin_lock_irq(&phba->hbalock);
3561 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3562 		list_splice_init(&nvmet_sgl_list,
3563 				 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3564 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3565 		spin_unlock_irq(&phba->hbalock);
3566 	} else if (nvmet_xri_cnt < phba->sli4_hba.nvmet_xri_cnt) {
3567 		/* nvmet xri-sgl shrunk */
3568 		xri_cnt = phba->sli4_hba.nvmet_xri_cnt - nvmet_xri_cnt;
3569 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3570 				"6305 NVMET xri-sgl count decreased from "
3571 				"%d to %d\n", phba->sli4_hba.nvmet_xri_cnt,
3572 				nvmet_xri_cnt);
3573 		spin_lock_irq(&phba->hbalock);
3574 		spin_lock(&phba->sli4_hba.sgl_list_lock);
3575 		list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list,
3576 				 &nvmet_sgl_list);
3577 		/* release extra nvmet sgls from list */
3578 		for (i = 0; i < xri_cnt; i++) {
3579 			list_remove_head(&nvmet_sgl_list,
3580 					 sglq_entry, struct lpfc_sglq, list);
3581 			if (sglq_entry) {
3582 				lpfc_nvmet_buf_free(phba, sglq_entry->virt,
3583 						    sglq_entry->phys);
3584 				kfree(sglq_entry);
3585 			}
3586 		}
3587 		list_splice_init(&nvmet_sgl_list,
3588 				 &phba->sli4_hba.lpfc_nvmet_sgl_list);
3589 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
3590 		spin_unlock_irq(&phba->hbalock);
3591 	} else
3592 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3593 				"6306 NVMET xri-sgl count unchanged: %d\n",
3594 				nvmet_xri_cnt);
3595 	phba->sli4_hba.nvmet_xri_cnt = nvmet_xri_cnt;
3596 
3597 	/* update xris to nvmet sgls on the list */
3598 	sglq_entry = NULL;
3599 	sglq_entry_next = NULL;
3600 	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3601 				 &phba->sli4_hba.lpfc_nvmet_sgl_list, list) {
3602 		lxri = lpfc_sli4_next_xritag(phba);
3603 		if (lxri == NO_XRI) {
3604 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3605 					"6307 Failed to allocate xri for "
3606 					"NVMET sgl\n");
3607 			rc = -ENOMEM;
3608 			goto out_free_mem;
3609 		}
3610 		sglq_entry->sli4_lxritag = lxri;
3611 		sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3612 	}
3613 	return 0;
3614 
3615 out_free_mem:
3616 	lpfc_free_nvmet_sgl_list(phba);
3617 	return rc;
3618 }
3619 
3620 /**
3621  * lpfc_sli4_scsi_sgl_update - update xri-sgl sizing and mapping
3622  * @phba: pointer to lpfc hba data structure.
3623  *
3624  * This routine first calculates the sizes of the current els and allocated
3625  * scsi sgl lists, and then goes through all sgls to updates the physical
3626  * XRIs assigned due to port function reset. During port initialization, the
3627  * current els and allocated scsi sgl lists are 0s.
3628  *
3629  * Return codes
3630  *   0 - successful (for now, it always returns 0)
3631  **/
3632 int
3633 lpfc_sli4_scsi_sgl_update(struct lpfc_hba *phba)
3634 {
3635 	struct lpfc_scsi_buf *psb, *psb_next;
3636 	uint16_t i, lxri, els_xri_cnt, scsi_xri_cnt;
3637 	LIST_HEAD(scsi_sgl_list);
3638 	int rc;
3639 
3640 	/*
3641 	 * update on pci function's els xri-sgl list
3642 	 */
3643 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3644 	phba->total_scsi_bufs = 0;
3645 
3646 	/*
3647 	 * update on pci function's allocated scsi xri-sgl list
3648 	 */
3649 	/* maximum number of xris available for scsi buffers */
3650 	phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3651 				      els_xri_cnt;
3652 
3653 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
3654 		return 0;
3655 
3656 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3657 		phba->sli4_hba.scsi_xri_max =  /* Split them up */
3658 			(phba->sli4_hba.scsi_xri_max *
3659 			 phba->cfg_xri_split) / 100;
3660 
3661 	spin_lock_irq(&phba->scsi_buf_list_get_lock);
3662 	spin_lock(&phba->scsi_buf_list_put_lock);
3663 	list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3664 	list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
3665 	spin_unlock(&phba->scsi_buf_list_put_lock);
3666 	spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3667 
3668 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3669 			"6060 Current allocated SCSI xri-sgl count:%d, "
3670 			"maximum  SCSI xri count:%d (split:%d)\n",
3671 			phba->sli4_hba.scsi_xri_cnt,
3672 			phba->sli4_hba.scsi_xri_max, phba->cfg_xri_split);
3673 
3674 	if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3675 		/* max scsi xri shrinked below the allocated scsi buffers */
3676 		scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3677 					phba->sli4_hba.scsi_xri_max;
3678 		/* release the extra allocated scsi buffers */
3679 		for (i = 0; i < scsi_xri_cnt; i++) {
3680 			list_remove_head(&scsi_sgl_list, psb,
3681 					 struct lpfc_scsi_buf, list);
3682 			if (psb) {
3683 				dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3684 					      psb->data, psb->dma_handle);
3685 				kfree(psb);
3686 			}
3687 		}
3688 		spin_lock_irq(&phba->scsi_buf_list_get_lock);
3689 		phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
3690 		spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3691 	}
3692 
3693 	/* update xris associated to remaining allocated scsi buffers */
3694 	psb = NULL;
3695 	psb_next = NULL;
3696 	list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3697 		lxri = lpfc_sli4_next_xritag(phba);
3698 		if (lxri == NO_XRI) {
3699 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3700 					"2560 Failed to allocate xri for "
3701 					"scsi buffer\n");
3702 			rc = -ENOMEM;
3703 			goto out_free_mem;
3704 		}
3705 		psb->cur_iocbq.sli4_lxritag = lxri;
3706 		psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3707 	}
3708 	spin_lock_irq(&phba->scsi_buf_list_get_lock);
3709 	spin_lock(&phba->scsi_buf_list_put_lock);
3710 	list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3711 	INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
3712 	spin_unlock(&phba->scsi_buf_list_put_lock);
3713 	spin_unlock_irq(&phba->scsi_buf_list_get_lock);
3714 	return 0;
3715 
3716 out_free_mem:
3717 	lpfc_scsi_free(phba);
3718 	return rc;
3719 }
3720 
3721 static uint64_t
3722 lpfc_get_wwpn(struct lpfc_hba *phba)
3723 {
3724 	uint64_t wwn;
3725 	int rc;
3726 	LPFC_MBOXQ_t *mboxq;
3727 	MAILBOX_t *mb;
3728 
3729 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
3730 						GFP_KERNEL);
3731 	if (!mboxq)
3732 		return (uint64_t)-1;
3733 
3734 	/* First get WWN of HBA instance */
3735 	lpfc_read_nv(phba, mboxq);
3736 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3737 	if (rc != MBX_SUCCESS) {
3738 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3739 				"6019 Mailbox failed , mbxCmd x%x "
3740 				"READ_NV, mbxStatus x%x\n",
3741 				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
3742 				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
3743 		mempool_free(mboxq, phba->mbox_mem_pool);
3744 		return (uint64_t) -1;
3745 	}
3746 	mb = &mboxq->u.mb;
3747 	memcpy(&wwn, (char *)mb->un.varRDnvp.portname, sizeof(uint64_t));
3748 	/* wwn is WWPN of HBA instance */
3749 	mempool_free(mboxq, phba->mbox_mem_pool);
3750 	if (phba->sli_rev == LPFC_SLI_REV4)
3751 		return be64_to_cpu(wwn);
3752 	else
3753 		return rol64(wwn, 32);
3754 }
3755 
3756 /**
3757  * lpfc_sli4_nvme_sgl_update - update xri-sgl sizing and mapping
3758  * @phba: pointer to lpfc hba data structure.
3759  *
3760  * This routine first calculates the sizes of the current els and allocated
3761  * scsi sgl lists, and then goes through all sgls to updates the physical
3762  * XRIs assigned due to port function reset. During port initialization, the
3763  * current els and allocated scsi sgl lists are 0s.
3764  *
3765  * Return codes
3766  *   0 - successful (for now, it always returns 0)
3767  **/
3768 int
3769 lpfc_sli4_nvme_sgl_update(struct lpfc_hba *phba)
3770 {
3771 	struct lpfc_nvme_buf *lpfc_ncmd = NULL, *lpfc_ncmd_next = NULL;
3772 	uint16_t i, lxri, els_xri_cnt;
3773 	uint16_t nvme_xri_cnt, nvme_xri_max;
3774 	LIST_HEAD(nvme_sgl_list);
3775 	int rc, cnt;
3776 
3777 	phba->total_nvme_bufs = 0;
3778 	phba->get_nvme_bufs = 0;
3779 	phba->put_nvme_bufs = 0;
3780 
3781 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
3782 		return 0;
3783 	/*
3784 	 * update on pci function's allocated nvme xri-sgl list
3785 	 */
3786 
3787 	/* maximum number of xris available for nvme buffers */
3788 	els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3789 	nvme_xri_max = phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
3790 	phba->sli4_hba.nvme_xri_max = nvme_xri_max;
3791 	phba->sli4_hba.nvme_xri_max -= phba->sli4_hba.scsi_xri_max;
3792 
3793 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3794 			"6074 Current allocated NVME xri-sgl count:%d, "
3795 			"maximum  NVME xri count:%d\n",
3796 			phba->sli4_hba.nvme_xri_cnt,
3797 			phba->sli4_hba.nvme_xri_max);
3798 
3799 	spin_lock_irq(&phba->nvme_buf_list_get_lock);
3800 	spin_lock(&phba->nvme_buf_list_put_lock);
3801 	list_splice_init(&phba->lpfc_nvme_buf_list_get, &nvme_sgl_list);
3802 	list_splice(&phba->lpfc_nvme_buf_list_put, &nvme_sgl_list);
3803 	cnt = phba->get_nvme_bufs + phba->put_nvme_bufs;
3804 	phba->get_nvme_bufs = 0;
3805 	phba->put_nvme_bufs = 0;
3806 	spin_unlock(&phba->nvme_buf_list_put_lock);
3807 	spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3808 
3809 	if (phba->sli4_hba.nvme_xri_cnt > phba->sli4_hba.nvme_xri_max) {
3810 		/* max nvme xri shrunk below the allocated nvme buffers */
3811 		spin_lock_irq(&phba->nvme_buf_list_get_lock);
3812 		nvme_xri_cnt = phba->sli4_hba.nvme_xri_cnt -
3813 					phba->sli4_hba.nvme_xri_max;
3814 		spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3815 		/* release the extra allocated nvme buffers */
3816 		for (i = 0; i < nvme_xri_cnt; i++) {
3817 			list_remove_head(&nvme_sgl_list, lpfc_ncmd,
3818 					 struct lpfc_nvme_buf, list);
3819 			if (lpfc_ncmd) {
3820 				dma_pool_free(phba->lpfc_sg_dma_buf_pool,
3821 					      lpfc_ncmd->data,
3822 					      lpfc_ncmd->dma_handle);
3823 				kfree(lpfc_ncmd);
3824 			}
3825 		}
3826 		spin_lock_irq(&phba->nvme_buf_list_get_lock);
3827 		phba->sli4_hba.nvme_xri_cnt -= nvme_xri_cnt;
3828 		spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3829 	}
3830 
3831 	/* update xris associated to remaining allocated nvme buffers */
3832 	lpfc_ncmd = NULL;
3833 	lpfc_ncmd_next = NULL;
3834 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
3835 				 &nvme_sgl_list, list) {
3836 		lxri = lpfc_sli4_next_xritag(phba);
3837 		if (lxri == NO_XRI) {
3838 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3839 					"6075 Failed to allocate xri for "
3840 					"nvme buffer\n");
3841 			rc = -ENOMEM;
3842 			goto out_free_mem;
3843 		}
3844 		lpfc_ncmd->cur_iocbq.sli4_lxritag = lxri;
3845 		lpfc_ncmd->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3846 	}
3847 	spin_lock_irq(&phba->nvme_buf_list_get_lock);
3848 	spin_lock(&phba->nvme_buf_list_put_lock);
3849 	list_splice_init(&nvme_sgl_list, &phba->lpfc_nvme_buf_list_get);
3850 	phba->get_nvme_bufs = cnt;
3851 	INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
3852 	spin_unlock(&phba->nvme_buf_list_put_lock);
3853 	spin_unlock_irq(&phba->nvme_buf_list_get_lock);
3854 	return 0;
3855 
3856 out_free_mem:
3857 	lpfc_nvme_free(phba);
3858 	return rc;
3859 }
3860 
3861 /**
3862  * lpfc_create_port - Create an FC port
3863  * @phba: pointer to lpfc hba data structure.
3864  * @instance: a unique integer ID to this FC port.
3865  * @dev: pointer to the device data structure.
3866  *
3867  * This routine creates a FC port for the upper layer protocol. The FC port
3868  * can be created on top of either a physical port or a virtual port provided
3869  * by the HBA. This routine also allocates a SCSI host data structure (shost)
3870  * and associates the FC port created before adding the shost into the SCSI
3871  * layer.
3872  *
3873  * Return codes
3874  *   @vport - pointer to the virtual N_Port data structure.
3875  *   NULL - port create failed.
3876  **/
3877 struct lpfc_vport *
3878 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
3879 {
3880 	struct lpfc_vport *vport;
3881 	struct Scsi_Host  *shost = NULL;
3882 	int error = 0;
3883 	int i;
3884 	uint64_t wwn;
3885 	bool use_no_reset_hba = false;
3886 	int rc;
3887 
3888 	if (lpfc_no_hba_reset_cnt) {
3889 		if (phba->sli_rev < LPFC_SLI_REV4 &&
3890 		    dev == &phba->pcidev->dev) {
3891 			/* Reset the port first */
3892 			lpfc_sli_brdrestart(phba);
3893 			rc = lpfc_sli_chipset_init(phba);
3894 			if (rc)
3895 				return NULL;
3896 		}
3897 		wwn = lpfc_get_wwpn(phba);
3898 	}
3899 
3900 	for (i = 0; i < lpfc_no_hba_reset_cnt; i++) {
3901 		if (wwn == lpfc_no_hba_reset[i]) {
3902 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3903 					"6020 Setting use_no_reset port=%llx\n",
3904 					wwn);
3905 			use_no_reset_hba = true;
3906 			break;
3907 		}
3908 	}
3909 
3910 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
3911 		if (dev != &phba->pcidev->dev) {
3912 			shost = scsi_host_alloc(&lpfc_vport_template,
3913 						sizeof(struct lpfc_vport));
3914 		} else {
3915 			if (!use_no_reset_hba)
3916 				shost = scsi_host_alloc(&lpfc_template,
3917 						sizeof(struct lpfc_vport));
3918 			else
3919 				shost = scsi_host_alloc(&lpfc_template_no_hr,
3920 						sizeof(struct lpfc_vport));
3921 		}
3922 	} else if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
3923 		shost = scsi_host_alloc(&lpfc_template_nvme,
3924 					sizeof(struct lpfc_vport));
3925 	}
3926 	if (!shost)
3927 		goto out;
3928 
3929 	vport = (struct lpfc_vport *) shost->hostdata;
3930 	vport->phba = phba;
3931 	vport->load_flag |= FC_LOADING;
3932 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3933 	vport->fc_rscn_flush = 0;
3934 	lpfc_get_vport_cfgparam(vport);
3935 
3936 	shost->unique_id = instance;
3937 	shost->max_id = LPFC_MAX_TARGET;
3938 	shost->max_lun = vport->cfg_max_luns;
3939 	shost->this_id = -1;
3940 	shost->max_cmd_len = 16;
3941 	shost->nr_hw_queues = phba->cfg_fcp_io_channel;
3942 	if (phba->sli_rev == LPFC_SLI_REV4) {
3943 		shost->dma_boundary =
3944 			phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
3945 		shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3946 	}
3947 
3948 	/*
3949 	 * Set initial can_queue value since 0 is no longer supported and
3950 	 * scsi_add_host will fail. This will be adjusted later based on the
3951 	 * max xri value determined in hba setup.
3952 	 */
3953 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
3954 	if (dev != &phba->pcidev->dev) {
3955 		shost->transportt = lpfc_vport_transport_template;
3956 		vport->port_type = LPFC_NPIV_PORT;
3957 	} else {
3958 		shost->transportt = lpfc_transport_template;
3959 		vport->port_type = LPFC_PHYSICAL_PORT;
3960 	}
3961 
3962 	/* Initialize all internally managed lists. */
3963 	INIT_LIST_HEAD(&vport->fc_nodes);
3964 	INIT_LIST_HEAD(&vport->rcv_buffer_list);
3965 	spin_lock_init(&vport->work_port_lock);
3966 
3967 	timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
3968 
3969 	timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
3970 
3971 	timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
3972 
3973 	error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
3974 	if (error)
3975 		goto out_put_shost;
3976 
3977 	spin_lock_irq(&phba->hbalock);
3978 	list_add_tail(&vport->listentry, &phba->port_list);
3979 	spin_unlock_irq(&phba->hbalock);
3980 	return vport;
3981 
3982 out_put_shost:
3983 	scsi_host_put(shost);
3984 out:
3985 	return NULL;
3986 }
3987 
3988 /**
3989  * destroy_port -  destroy an FC port
3990  * @vport: pointer to an lpfc virtual N_Port data structure.
3991  *
3992  * This routine destroys a FC port from the upper layer protocol. All the
3993  * resources associated with the port are released.
3994  **/
3995 void
3996 destroy_port(struct lpfc_vport *vport)
3997 {
3998 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3999 	struct lpfc_hba  *phba = vport->phba;
4000 
4001 	lpfc_debugfs_terminate(vport);
4002 	fc_remove_host(shost);
4003 	scsi_remove_host(shost);
4004 
4005 	spin_lock_irq(&phba->hbalock);
4006 	list_del_init(&vport->listentry);
4007 	spin_unlock_irq(&phba->hbalock);
4008 
4009 	lpfc_cleanup(vport);
4010 	return;
4011 }
4012 
4013 /**
4014  * lpfc_get_instance - Get a unique integer ID
4015  *
4016  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
4017  * uses the kernel idr facility to perform the task.
4018  *
4019  * Return codes:
4020  *   instance - a unique integer ID allocated as the new instance.
4021  *   -1 - lpfc get instance failed.
4022  **/
4023 int
4024 lpfc_get_instance(void)
4025 {
4026 	int ret;
4027 
4028 	ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4029 	return ret < 0 ? -1 : ret;
4030 }
4031 
4032 /**
4033  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
4034  * @shost: pointer to SCSI host data structure.
4035  * @time: elapsed time of the scan in jiffies.
4036  *
4037  * This routine is called by the SCSI layer with a SCSI host to determine
4038  * whether the scan host is finished.
4039  *
4040  * Note: there is no scan_start function as adapter initialization will have
4041  * asynchronously kicked off the link initialization.
4042  *
4043  * Return codes
4044  *   0 - SCSI host scan is not over yet.
4045  *   1 - SCSI host scan is over.
4046  **/
4047 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4048 {
4049 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4050 	struct lpfc_hba   *phba = vport->phba;
4051 	int stat = 0;
4052 
4053 	spin_lock_irq(shost->host_lock);
4054 
4055 	if (vport->load_flag & FC_UNLOADING) {
4056 		stat = 1;
4057 		goto finished;
4058 	}
4059 	if (time >= msecs_to_jiffies(30 * 1000)) {
4060 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4061 				"0461 Scanning longer than 30 "
4062 				"seconds.  Continuing initialization\n");
4063 		stat = 1;
4064 		goto finished;
4065 	}
4066 	if (time >= msecs_to_jiffies(15 * 1000) &&
4067 	    phba->link_state <= LPFC_LINK_DOWN) {
4068 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4069 				"0465 Link down longer than 15 "
4070 				"seconds.  Continuing initialization\n");
4071 		stat = 1;
4072 		goto finished;
4073 	}
4074 
4075 	if (vport->port_state != LPFC_VPORT_READY)
4076 		goto finished;
4077 	if (vport->num_disc_nodes || vport->fc_prli_sent)
4078 		goto finished;
4079 	if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
4080 		goto finished;
4081 	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
4082 		goto finished;
4083 
4084 	stat = 1;
4085 
4086 finished:
4087 	spin_unlock_irq(shost->host_lock);
4088 	return stat;
4089 }
4090 
4091 /**
4092  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
4093  * @shost: pointer to SCSI host data structure.
4094  *
4095  * This routine initializes a given SCSI host attributes on a FC port. The
4096  * SCSI host can be either on top of a physical port or a virtual port.
4097  **/
4098 void lpfc_host_attrib_init(struct Scsi_Host *shost)
4099 {
4100 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4101 	struct lpfc_hba   *phba = vport->phba;
4102 	/*
4103 	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
4104 	 */
4105 
4106 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4107 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4108 	fc_host_supported_classes(shost) = FC_COS_CLASS3;
4109 
4110 	memset(fc_host_supported_fc4s(shost), 0,
4111 	       sizeof(fc_host_supported_fc4s(shost)));
4112 	fc_host_supported_fc4s(shost)[2] = 1;
4113 	fc_host_supported_fc4s(shost)[7] = 1;
4114 
4115 	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4116 				 sizeof fc_host_symbolic_name(shost));
4117 
4118 	fc_host_supported_speeds(shost) = 0;
4119 	if (phba->lmt & LMT_64Gb)
4120 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_64GBIT;
4121 	if (phba->lmt & LMT_32Gb)
4122 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4123 	if (phba->lmt & LMT_16Gb)
4124 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4125 	if (phba->lmt & LMT_10Gb)
4126 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4127 	if (phba->lmt & LMT_8Gb)
4128 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4129 	if (phba->lmt & LMT_4Gb)
4130 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4131 	if (phba->lmt & LMT_2Gb)
4132 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4133 	if (phba->lmt & LMT_1Gb)
4134 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4135 
4136 	fc_host_maxframe_size(shost) =
4137 		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4138 		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
4139 
4140 	fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4141 
4142 	/* This value is also unchanging */
4143 	memset(fc_host_active_fc4s(shost), 0,
4144 	       sizeof(fc_host_active_fc4s(shost)));
4145 	fc_host_active_fc4s(shost)[2] = 1;
4146 	fc_host_active_fc4s(shost)[7] = 1;
4147 
4148 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
4149 	spin_lock_irq(shost->host_lock);
4150 	vport->load_flag &= ~FC_LOADING;
4151 	spin_unlock_irq(shost->host_lock);
4152 }
4153 
4154 /**
4155  * lpfc_stop_port_s3 - Stop SLI3 device port
4156  * @phba: pointer to lpfc hba data structure.
4157  *
4158  * This routine is invoked to stop an SLI3 device port, it stops the device
4159  * from generating interrupts and stops the device driver's timers for the
4160  * device.
4161  **/
4162 static void
4163 lpfc_stop_port_s3(struct lpfc_hba *phba)
4164 {
4165 	/* Clear all interrupt enable conditions */
4166 	writel(0, phba->HCregaddr);
4167 	readl(phba->HCregaddr); /* flush */
4168 	/* Clear all pending interrupts */
4169 	writel(0xffffffff, phba->HAregaddr);
4170 	readl(phba->HAregaddr); /* flush */
4171 
4172 	/* Reset some HBA SLI setup states */
4173 	lpfc_stop_hba_timers(phba);
4174 	phba->pport->work_port_events = 0;
4175 }
4176 
4177 /**
4178  * lpfc_stop_port_s4 - Stop SLI4 device port
4179  * @phba: pointer to lpfc hba data structure.
4180  *
4181  * This routine is invoked to stop an SLI4 device port, it stops the device
4182  * from generating interrupts and stops the device driver's timers for the
4183  * device.
4184  **/
4185 static void
4186 lpfc_stop_port_s4(struct lpfc_hba *phba)
4187 {
4188 	/* Reset some HBA SLI4 setup states */
4189 	lpfc_stop_hba_timers(phba);
4190 	phba->pport->work_port_events = 0;
4191 	phba->sli4_hba.intr_enable = 0;
4192 }
4193 
4194 /**
4195  * lpfc_stop_port - Wrapper function for stopping hba port
4196  * @phba: Pointer to HBA context object.
4197  *
4198  * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4199  * the API jump table function pointer from the lpfc_hba struct.
4200  **/
4201 void
4202 lpfc_stop_port(struct lpfc_hba *phba)
4203 {
4204 	phba->lpfc_stop_port(phba);
4205 
4206 	if (phba->wq)
4207 		flush_workqueue(phba->wq);
4208 }
4209 
4210 /**
4211  * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4212  * @phba: Pointer to hba for which this call is being executed.
4213  *
4214  * This routine starts the timer waiting for the FCF rediscovery to complete.
4215  **/
4216 void
4217 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4218 {
4219 	unsigned long fcf_redisc_wait_tmo =
4220 		(jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4221 	/* Start fcf rediscovery wait period timer */
4222 	mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4223 	spin_lock_irq(&phba->hbalock);
4224 	/* Allow action to new fcf asynchronous event */
4225 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4226 	/* Mark the FCF rediscovery pending state */
4227 	phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4228 	spin_unlock_irq(&phba->hbalock);
4229 }
4230 
4231 /**
4232  * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4233  * @ptr: Map to lpfc_hba data structure pointer.
4234  *
4235  * This routine is invoked when waiting for FCF table rediscover has been
4236  * timed out. If new FCF record(s) has (have) been discovered during the
4237  * wait period, a new FCF event shall be added to the FCOE async event
4238  * list, and then worker thread shall be waked up for processing from the
4239  * worker thread context.
4240  **/
4241 static void
4242 lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
4243 {
4244 	struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
4245 
4246 	/* Don't send FCF rediscovery event if timer cancelled */
4247 	spin_lock_irq(&phba->hbalock);
4248 	if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4249 		spin_unlock_irq(&phba->hbalock);
4250 		return;
4251 	}
4252 	/* Clear FCF rediscovery timer pending flag */
4253 	phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4254 	/* FCF rediscovery event to worker thread */
4255 	phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4256 	spin_unlock_irq(&phba->hbalock);
4257 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
4258 			"2776 FCF rediscover quiescent timer expired\n");
4259 	/* wake up worker thread */
4260 	lpfc_worker_wake_up(phba);
4261 }
4262 
4263 /**
4264  * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4265  * @phba: pointer to lpfc hba data structure.
4266  * @acqe_link: pointer to the async link completion queue entry.
4267  *
4268  * This routine is to parse the SLI4 link-attention link fault code and
4269  * translate it into the base driver's read link attention mailbox command
4270  * status.
4271  *
4272  * Return: Link-attention status in terms of base driver's coding.
4273  **/
4274 static uint16_t
4275 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4276 			   struct lpfc_acqe_link *acqe_link)
4277 {
4278 	uint16_t latt_fault;
4279 
4280 	switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4281 	case LPFC_ASYNC_LINK_FAULT_NONE:
4282 	case LPFC_ASYNC_LINK_FAULT_LOCAL:
4283 	case LPFC_ASYNC_LINK_FAULT_REMOTE:
4284 		latt_fault = 0;
4285 		break;
4286 	default:
4287 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4288 				"0398 Invalid link fault code: x%x\n",
4289 				bf_get(lpfc_acqe_link_fault, acqe_link));
4290 		latt_fault = MBXERR_ERROR;
4291 		break;
4292 	}
4293 	return latt_fault;
4294 }
4295 
4296 /**
4297  * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4298  * @phba: pointer to lpfc hba data structure.
4299  * @acqe_link: pointer to the async link completion queue entry.
4300  *
4301  * This routine is to parse the SLI4 link attention type and translate it
4302  * into the base driver's link attention type coding.
4303  *
4304  * Return: Link attention type in terms of base driver's coding.
4305  **/
4306 static uint8_t
4307 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4308 			  struct lpfc_acqe_link *acqe_link)
4309 {
4310 	uint8_t att_type;
4311 
4312 	switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4313 	case LPFC_ASYNC_LINK_STATUS_DOWN:
4314 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
4315 		att_type = LPFC_ATT_LINK_DOWN;
4316 		break;
4317 	case LPFC_ASYNC_LINK_STATUS_UP:
4318 		/* Ignore physical link up events - wait for logical link up */
4319 		att_type = LPFC_ATT_RESERVED;
4320 		break;
4321 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
4322 		att_type = LPFC_ATT_LINK_UP;
4323 		break;
4324 	default:
4325 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4326 				"0399 Invalid link attention type: x%x\n",
4327 				bf_get(lpfc_acqe_link_status, acqe_link));
4328 		att_type = LPFC_ATT_RESERVED;
4329 		break;
4330 	}
4331 	return att_type;
4332 }
4333 
4334 /**
4335  * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4336  * @phba: pointer to lpfc hba data structure.
4337  *
4338  * This routine is to get an SLI3 FC port's link speed in Mbps.
4339  *
4340  * Return: link speed in terms of Mbps.
4341  **/
4342 uint32_t
4343 lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4344 {
4345 	uint32_t link_speed;
4346 
4347 	if (!lpfc_is_link_up(phba))
4348 		return 0;
4349 
4350 	if (phba->sli_rev <= LPFC_SLI_REV3) {
4351 		switch (phba->fc_linkspeed) {
4352 		case LPFC_LINK_SPEED_1GHZ:
4353 			link_speed = 1000;
4354 			break;
4355 		case LPFC_LINK_SPEED_2GHZ:
4356 			link_speed = 2000;
4357 			break;
4358 		case LPFC_LINK_SPEED_4GHZ:
4359 			link_speed = 4000;
4360 			break;
4361 		case LPFC_LINK_SPEED_8GHZ:
4362 			link_speed = 8000;
4363 			break;
4364 		case LPFC_LINK_SPEED_10GHZ:
4365 			link_speed = 10000;
4366 			break;
4367 		case LPFC_LINK_SPEED_16GHZ:
4368 			link_speed = 16000;
4369 			break;
4370 		default:
4371 			link_speed = 0;
4372 		}
4373 	} else {
4374 		if (phba->sli4_hba.link_state.logical_speed)
4375 			link_speed =
4376 			      phba->sli4_hba.link_state.logical_speed;
4377 		else
4378 			link_speed = phba->sli4_hba.link_state.speed;
4379 	}
4380 	return link_speed;
4381 }
4382 
4383 /**
4384  * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4385  * @phba: pointer to lpfc hba data structure.
4386  * @evt_code: asynchronous event code.
4387  * @speed_code: asynchronous event link speed code.
4388  *
4389  * This routine is to parse the giving SLI4 async event link speed code into
4390  * value of Mbps for the link speed.
4391  *
4392  * Return: link speed in terms of Mbps.
4393  **/
4394 static uint32_t
4395 lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4396 			   uint8_t speed_code)
4397 {
4398 	uint32_t port_speed;
4399 
4400 	switch (evt_code) {
4401 	case LPFC_TRAILER_CODE_LINK:
4402 		switch (speed_code) {
4403 		case LPFC_ASYNC_LINK_SPEED_ZERO:
4404 			port_speed = 0;
4405 			break;
4406 		case LPFC_ASYNC_LINK_SPEED_10MBPS:
4407 			port_speed = 10;
4408 			break;
4409 		case LPFC_ASYNC_LINK_SPEED_100MBPS:
4410 			port_speed = 100;
4411 			break;
4412 		case LPFC_ASYNC_LINK_SPEED_1GBPS:
4413 			port_speed = 1000;
4414 			break;
4415 		case LPFC_ASYNC_LINK_SPEED_10GBPS:
4416 			port_speed = 10000;
4417 			break;
4418 		case LPFC_ASYNC_LINK_SPEED_20GBPS:
4419 			port_speed = 20000;
4420 			break;
4421 		case LPFC_ASYNC_LINK_SPEED_25GBPS:
4422 			port_speed = 25000;
4423 			break;
4424 		case LPFC_ASYNC_LINK_SPEED_40GBPS:
4425 			port_speed = 40000;
4426 			break;
4427 		default:
4428 			port_speed = 0;
4429 		}
4430 		break;
4431 	case LPFC_TRAILER_CODE_FC:
4432 		switch (speed_code) {
4433 		case LPFC_FC_LA_SPEED_UNKNOWN:
4434 			port_speed = 0;
4435 			break;
4436 		case LPFC_FC_LA_SPEED_1G:
4437 			port_speed = 1000;
4438 			break;
4439 		case LPFC_FC_LA_SPEED_2G:
4440 			port_speed = 2000;
4441 			break;
4442 		case LPFC_FC_LA_SPEED_4G:
4443 			port_speed = 4000;
4444 			break;
4445 		case LPFC_FC_LA_SPEED_8G:
4446 			port_speed = 8000;
4447 			break;
4448 		case LPFC_FC_LA_SPEED_10G:
4449 			port_speed = 10000;
4450 			break;
4451 		case LPFC_FC_LA_SPEED_16G:
4452 			port_speed = 16000;
4453 			break;
4454 		case LPFC_FC_LA_SPEED_32G:
4455 			port_speed = 32000;
4456 			break;
4457 		case LPFC_FC_LA_SPEED_64G:
4458 			port_speed = 64000;
4459 			break;
4460 		default:
4461 			port_speed = 0;
4462 		}
4463 		break;
4464 	default:
4465 		port_speed = 0;
4466 	}
4467 	return port_speed;
4468 }
4469 
4470 /**
4471  * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
4472  * @phba: pointer to lpfc hba data structure.
4473  * @acqe_link: pointer to the async link completion queue entry.
4474  *
4475  * This routine is to handle the SLI4 asynchronous FCoE link event.
4476  **/
4477 static void
4478 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4479 			 struct lpfc_acqe_link *acqe_link)
4480 {
4481 	struct lpfc_dmabuf *mp;
4482 	LPFC_MBOXQ_t *pmb;
4483 	MAILBOX_t *mb;
4484 	struct lpfc_mbx_read_top *la;
4485 	uint8_t att_type;
4486 	int rc;
4487 
4488 	att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
4489 	if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
4490 		return;
4491 	phba->fcoe_eventtag = acqe_link->event_tag;
4492 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4493 	if (!pmb) {
4494 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4495 				"0395 The mboxq allocation failed\n");
4496 		return;
4497 	}
4498 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4499 	if (!mp) {
4500 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4501 				"0396 The lpfc_dmabuf allocation failed\n");
4502 		goto out_free_pmb;
4503 	}
4504 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4505 	if (!mp->virt) {
4506 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4507 				"0397 The mbuf allocation failed\n");
4508 		goto out_free_dmabuf;
4509 	}
4510 
4511 	/* Cleanup any outstanding ELS commands */
4512 	lpfc_els_flush_all_cmd(phba);
4513 
4514 	/* Block ELS IOCBs until we have done process link event */
4515 	phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4516 
4517 	/* Update link event statistics */
4518 	phba->sli.slistat.link_event++;
4519 
4520 	/* Create lpfc_handle_latt mailbox command from link ACQE */
4521 	lpfc_read_topology(phba, pmb, mp);
4522 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4523 	pmb->vport = phba->pport;
4524 
4525 	/* Keep the link status for extra SLI4 state machine reference */
4526 	phba->sli4_hba.link_state.speed =
4527 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4528 				bf_get(lpfc_acqe_link_speed, acqe_link));
4529 	phba->sli4_hba.link_state.duplex =
4530 				bf_get(lpfc_acqe_link_duplex, acqe_link);
4531 	phba->sli4_hba.link_state.status =
4532 				bf_get(lpfc_acqe_link_status, acqe_link);
4533 	phba->sli4_hba.link_state.type =
4534 				bf_get(lpfc_acqe_link_type, acqe_link);
4535 	phba->sli4_hba.link_state.number =
4536 				bf_get(lpfc_acqe_link_number, acqe_link);
4537 	phba->sli4_hba.link_state.fault =
4538 				bf_get(lpfc_acqe_link_fault, acqe_link);
4539 	phba->sli4_hba.link_state.logical_speed =
4540 			bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4541 
4542 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4543 			"2900 Async FC/FCoE Link event - Speed:%dGBit "
4544 			"duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4545 			"Logical speed:%dMbps Fault:%d\n",
4546 			phba->sli4_hba.link_state.speed,
4547 			phba->sli4_hba.link_state.topology,
4548 			phba->sli4_hba.link_state.status,
4549 			phba->sli4_hba.link_state.type,
4550 			phba->sli4_hba.link_state.number,
4551 			phba->sli4_hba.link_state.logical_speed,
4552 			phba->sli4_hba.link_state.fault);
4553 	/*
4554 	 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4555 	 * topology info. Note: Optional for non FC-AL ports.
4556 	 */
4557 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4558 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4559 		if (rc == MBX_NOT_FINISHED)
4560 			goto out_free_dmabuf;
4561 		return;
4562 	}
4563 	/*
4564 	 * For FCoE Mode: fill in all the topology information we need and call
4565 	 * the READ_TOPOLOGY completion routine to continue without actually
4566 	 * sending the READ_TOPOLOGY mailbox command to the port.
4567 	 */
4568 	/* Parse and translate status field */
4569 	mb = &pmb->u.mb;
4570 	mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4571 
4572 	/* Parse and translate link attention fields */
4573 	la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4574 	la->eventTag = acqe_link->event_tag;
4575 	bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4576 	bf_set(lpfc_mbx_read_top_link_spd, la,
4577 	       (bf_get(lpfc_acqe_link_speed, acqe_link)));
4578 
4579 	/* Fake the the following irrelvant fields */
4580 	bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4581 	bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4582 	bf_set(lpfc_mbx_read_top_il, la, 0);
4583 	bf_set(lpfc_mbx_read_top_pb, la, 0);
4584 	bf_set(lpfc_mbx_read_top_fa, la, 0);
4585 	bf_set(lpfc_mbx_read_top_mm, la, 0);
4586 
4587 	/* Invoke the lpfc_handle_latt mailbox command callback function */
4588 	lpfc_mbx_cmpl_read_topology(phba, pmb);
4589 
4590 	return;
4591 
4592 out_free_dmabuf:
4593 	kfree(mp);
4594 out_free_pmb:
4595 	mempool_free(pmb, phba->mbox_mem_pool);
4596 }
4597 
4598 /**
4599  * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4600  * @phba: pointer to lpfc hba data structure.
4601  * @acqe_fc: pointer to the async fc completion queue entry.
4602  *
4603  * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4604  * that the event was received and then issue a read_topology mailbox command so
4605  * that the rest of the driver will treat it the same as SLI3.
4606  **/
4607 static void
4608 lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4609 {
4610 	struct lpfc_dmabuf *mp;
4611 	LPFC_MBOXQ_t *pmb;
4612 	MAILBOX_t *mb;
4613 	struct lpfc_mbx_read_top *la;
4614 	int rc;
4615 
4616 	if (bf_get(lpfc_trailer_type, acqe_fc) !=
4617 	    LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4618 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4619 				"2895 Non FC link Event detected.(%d)\n",
4620 				bf_get(lpfc_trailer_type, acqe_fc));
4621 		return;
4622 	}
4623 	/* Keep the link status for extra SLI4 state machine reference */
4624 	phba->sli4_hba.link_state.speed =
4625 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4626 				bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
4627 	phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4628 	phba->sli4_hba.link_state.topology =
4629 				bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4630 	phba->sli4_hba.link_state.status =
4631 				bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4632 	phba->sli4_hba.link_state.type =
4633 				bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4634 	phba->sli4_hba.link_state.number =
4635 				bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4636 	phba->sli4_hba.link_state.fault =
4637 				bf_get(lpfc_acqe_link_fault, acqe_fc);
4638 	phba->sli4_hba.link_state.logical_speed =
4639 				bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
4640 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4641 			"2896 Async FC event - Speed:%dGBaud Topology:x%x "
4642 			"LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4643 			"%dMbps Fault:%d\n",
4644 			phba->sli4_hba.link_state.speed,
4645 			phba->sli4_hba.link_state.topology,
4646 			phba->sli4_hba.link_state.status,
4647 			phba->sli4_hba.link_state.type,
4648 			phba->sli4_hba.link_state.number,
4649 			phba->sli4_hba.link_state.logical_speed,
4650 			phba->sli4_hba.link_state.fault);
4651 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4652 	if (!pmb) {
4653 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4654 				"2897 The mboxq allocation failed\n");
4655 		return;
4656 	}
4657 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4658 	if (!mp) {
4659 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4660 				"2898 The lpfc_dmabuf allocation failed\n");
4661 		goto out_free_pmb;
4662 	}
4663 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4664 	if (!mp->virt) {
4665 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4666 				"2899 The mbuf allocation failed\n");
4667 		goto out_free_dmabuf;
4668 	}
4669 
4670 	/* Cleanup any outstanding ELS commands */
4671 	lpfc_els_flush_all_cmd(phba);
4672 
4673 	/* Block ELS IOCBs until we have done process link event */
4674 	phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4675 
4676 	/* Update link event statistics */
4677 	phba->sli.slistat.link_event++;
4678 
4679 	/* Create lpfc_handle_latt mailbox command from link ACQE */
4680 	lpfc_read_topology(phba, pmb, mp);
4681 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4682 	pmb->vport = phba->pport;
4683 
4684 	if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4685 		phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
4686 
4687 		switch (phba->sli4_hba.link_state.status) {
4688 		case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
4689 			phba->link_flag |= LS_MDS_LINK_DOWN;
4690 			break;
4691 		case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
4692 			phba->link_flag |= LS_MDS_LOOPBACK;
4693 			break;
4694 		default:
4695 			break;
4696 		}
4697 
4698 		/* Parse and translate status field */
4699 		mb = &pmb->u.mb;
4700 		mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4701 							   (void *)acqe_fc);
4702 
4703 		/* Parse and translate link attention fields */
4704 		la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4705 		la->eventTag = acqe_fc->event_tag;
4706 
4707 		if (phba->sli4_hba.link_state.status ==
4708 		    LPFC_FC_LA_TYPE_UNEXP_WWPN) {
4709 			bf_set(lpfc_mbx_read_top_att_type, la,
4710 			       LPFC_FC_LA_TYPE_UNEXP_WWPN);
4711 		} else {
4712 			bf_set(lpfc_mbx_read_top_att_type, la,
4713 			       LPFC_FC_LA_TYPE_LINK_DOWN);
4714 		}
4715 		/* Invoke the mailbox command callback function */
4716 		lpfc_mbx_cmpl_read_topology(phba, pmb);
4717 
4718 		return;
4719 	}
4720 
4721 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4722 	if (rc == MBX_NOT_FINISHED)
4723 		goto out_free_dmabuf;
4724 	return;
4725 
4726 out_free_dmabuf:
4727 	kfree(mp);
4728 out_free_pmb:
4729 	mempool_free(pmb, phba->mbox_mem_pool);
4730 }
4731 
4732 /**
4733  * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4734  * @phba: pointer to lpfc hba data structure.
4735  * @acqe_fc: pointer to the async SLI completion queue entry.
4736  *
4737  * This routine is to handle the SLI4 asynchronous SLI events.
4738  **/
4739 static void
4740 lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4741 {
4742 	char port_name;
4743 	char message[128];
4744 	uint8_t status;
4745 	uint8_t evt_type;
4746 	uint8_t operational = 0;
4747 	struct temp_event temp_event_data;
4748 	struct lpfc_acqe_misconfigured_event *misconfigured;
4749 	struct Scsi_Host  *shost;
4750 
4751 	evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4752 
4753 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4754 			"2901 Async SLI event - Event Data1:x%08x Event Data2:"
4755 			"x%08x SLI Event Type:%d\n",
4756 			acqe_sli->event_data1, acqe_sli->event_data2,
4757 			evt_type);
4758 
4759 	port_name = phba->Port[0];
4760 	if (port_name == 0x00)
4761 		port_name = '?'; /* get port name is empty */
4762 
4763 	switch (evt_type) {
4764 	case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4765 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4766 		temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4767 		temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4768 
4769 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4770 				"3190 Over Temperature:%d Celsius- Port Name %c\n",
4771 				acqe_sli->event_data1, port_name);
4772 
4773 		phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
4774 		shost = lpfc_shost_from_vport(phba->pport);
4775 		fc_host_post_vendor_event(shost, fc_get_event_number(),
4776 					  sizeof(temp_event_data),
4777 					  (char *)&temp_event_data,
4778 					  SCSI_NL_VID_TYPE_PCI
4779 					  | PCI_VENDOR_ID_EMULEX);
4780 		break;
4781 	case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4782 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4783 		temp_event_data.event_code = LPFC_NORMAL_TEMP;
4784 		temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4785 
4786 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4787 				"3191 Normal Temperature:%d Celsius - Port Name %c\n",
4788 				acqe_sli->event_data1, port_name);
4789 
4790 		shost = lpfc_shost_from_vport(phba->pport);
4791 		fc_host_post_vendor_event(shost, fc_get_event_number(),
4792 					  sizeof(temp_event_data),
4793 					  (char *)&temp_event_data,
4794 					  SCSI_NL_VID_TYPE_PCI
4795 					  | PCI_VENDOR_ID_EMULEX);
4796 		break;
4797 	case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4798 		misconfigured = (struct lpfc_acqe_misconfigured_event *)
4799 					&acqe_sli->event_data1;
4800 
4801 		/* fetch the status for this port */
4802 		switch (phba->sli4_hba.lnk_info.lnk_no) {
4803 		case LPFC_LINK_NUMBER_0:
4804 			status = bf_get(lpfc_sli_misconfigured_port0_state,
4805 					&misconfigured->theEvent);
4806 			operational = bf_get(lpfc_sli_misconfigured_port0_op,
4807 					&misconfigured->theEvent);
4808 			break;
4809 		case LPFC_LINK_NUMBER_1:
4810 			status = bf_get(lpfc_sli_misconfigured_port1_state,
4811 					&misconfigured->theEvent);
4812 			operational = bf_get(lpfc_sli_misconfigured_port1_op,
4813 					&misconfigured->theEvent);
4814 			break;
4815 		case LPFC_LINK_NUMBER_2:
4816 			status = bf_get(lpfc_sli_misconfigured_port2_state,
4817 					&misconfigured->theEvent);
4818 			operational = bf_get(lpfc_sli_misconfigured_port2_op,
4819 					&misconfigured->theEvent);
4820 			break;
4821 		case LPFC_LINK_NUMBER_3:
4822 			status = bf_get(lpfc_sli_misconfigured_port3_state,
4823 					&misconfigured->theEvent);
4824 			operational = bf_get(lpfc_sli_misconfigured_port3_op,
4825 					&misconfigured->theEvent);
4826 			break;
4827 		default:
4828 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4829 					"3296 "
4830 					"LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4831 					"event: Invalid link %d",
4832 					phba->sli4_hba.lnk_info.lnk_no);
4833 			return;
4834 		}
4835 
4836 		/* Skip if optic state unchanged */
4837 		if (phba->sli4_hba.lnk_info.optic_state == status)
4838 			return;
4839 
4840 		switch (status) {
4841 		case LPFC_SLI_EVENT_STATUS_VALID:
4842 			sprintf(message, "Physical Link is functional");
4843 			break;
4844 		case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4845 			sprintf(message, "Optics faulted/incorrectly "
4846 				"installed/not installed - Reseat optics, "
4847 				"if issue not resolved, replace.");
4848 			break;
4849 		case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4850 			sprintf(message,
4851 				"Optics of two types installed - Remove one "
4852 				"optic or install matching pair of optics.");
4853 			break;
4854 		case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4855 			sprintf(message, "Incompatible optics - Replace with "
4856 				"compatible optics for card to function.");
4857 			break;
4858 		case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4859 			sprintf(message, "Unqualified optics - Replace with "
4860 				"Avago optics for Warranty and Technical "
4861 				"Support - Link is%s operational",
4862 				(operational) ? " not" : "");
4863 			break;
4864 		case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4865 			sprintf(message, "Uncertified optics - Replace with "
4866 				"Avago-certified optics to enable link "
4867 				"operation - Link is%s operational",
4868 				(operational) ? " not" : "");
4869 			break;
4870 		default:
4871 			/* firmware is reporting a status we don't know about */
4872 			sprintf(message, "Unknown event status x%02x", status);
4873 			break;
4874 		}
4875 		phba->sli4_hba.lnk_info.optic_state = status;
4876 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4877 				"3176 Port Name %c %s\n", port_name, message);
4878 		break;
4879 	case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4880 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4881 				"3192 Remote DPort Test Initiated - "
4882 				"Event Data1:x%08x Event Data2: x%08x\n",
4883 				acqe_sli->event_data1, acqe_sli->event_data2);
4884 		break;
4885 	default:
4886 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4887 				"3193 Async SLI event - Event Data1:x%08x Event Data2:"
4888 				"x%08x SLI Event Type:%d\n",
4889 				acqe_sli->event_data1, acqe_sli->event_data2,
4890 				evt_type);
4891 		break;
4892 	}
4893 }
4894 
4895 /**
4896  * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4897  * @vport: pointer to vport data structure.
4898  *
4899  * This routine is to perform Clear Virtual Link (CVL) on a vport in
4900  * response to a CVL event.
4901  *
4902  * Return the pointer to the ndlp with the vport if successful, otherwise
4903  * return NULL.
4904  **/
4905 static struct lpfc_nodelist *
4906 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4907 {
4908 	struct lpfc_nodelist *ndlp;
4909 	struct Scsi_Host *shost;
4910 	struct lpfc_hba *phba;
4911 
4912 	if (!vport)
4913 		return NULL;
4914 	phba = vport->phba;
4915 	if (!phba)
4916 		return NULL;
4917 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
4918 	if (!ndlp) {
4919 		/* Cannot find existing Fabric ndlp, so allocate a new one */
4920 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
4921 		if (!ndlp)
4922 			return 0;
4923 		/* Set the node type */
4924 		ndlp->nlp_type |= NLP_FABRIC;
4925 		/* Put ndlp onto node list */
4926 		lpfc_enqueue_node(vport, ndlp);
4927 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
4928 		/* re-setup ndlp without removing from node list */
4929 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4930 		if (!ndlp)
4931 			return 0;
4932 	}
4933 	if ((phba->pport->port_state < LPFC_FLOGI) &&
4934 		(phba->pport->port_state != LPFC_VPORT_FAILED))
4935 		return NULL;
4936 	/* If virtual link is not yet instantiated ignore CVL */
4937 	if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4938 		&& (vport->port_state != LPFC_VPORT_FAILED))
4939 		return NULL;
4940 	shost = lpfc_shost_from_vport(vport);
4941 	if (!shost)
4942 		return NULL;
4943 	lpfc_linkdown_port(vport);
4944 	lpfc_cleanup_pending_mbox(vport);
4945 	spin_lock_irq(shost->host_lock);
4946 	vport->fc_flag |= FC_VPORT_CVL_RCVD;
4947 	spin_unlock_irq(shost->host_lock);
4948 
4949 	return ndlp;
4950 }
4951 
4952 /**
4953  * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4954  * @vport: pointer to lpfc hba data structure.
4955  *
4956  * This routine is to perform Clear Virtual Link (CVL) on all vports in
4957  * response to a FCF dead event.
4958  **/
4959 static void
4960 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4961 {
4962 	struct lpfc_vport **vports;
4963 	int i;
4964 
4965 	vports = lpfc_create_vport_work_array(phba);
4966 	if (vports)
4967 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4968 			lpfc_sli4_perform_vport_cvl(vports[i]);
4969 	lpfc_destroy_vport_work_array(phba, vports);
4970 }
4971 
4972 /**
4973  * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
4974  * @phba: pointer to lpfc hba data structure.
4975  * @acqe_link: pointer to the async fcoe completion queue entry.
4976  *
4977  * This routine is to handle the SLI4 asynchronous fcoe event.
4978  **/
4979 static void
4980 lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
4981 			struct lpfc_acqe_fip *acqe_fip)
4982 {
4983 	uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
4984 	int rc;
4985 	struct lpfc_vport *vport;
4986 	struct lpfc_nodelist *ndlp;
4987 	struct Scsi_Host  *shost;
4988 	int active_vlink_present;
4989 	struct lpfc_vport **vports;
4990 	int i;
4991 
4992 	phba->fc_eventTag = acqe_fip->event_tag;
4993 	phba->fcoe_eventtag = acqe_fip->event_tag;
4994 	switch (event_type) {
4995 	case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4996 	case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4997 		if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
4998 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4999 					LOG_DISCOVERY,
5000 					"2546 New FCF event, evt_tag:x%x, "
5001 					"index:x%x\n",
5002 					acqe_fip->event_tag,
5003 					acqe_fip->index);
5004 		else
5005 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
5006 					LOG_DISCOVERY,
5007 					"2788 FCF param modified event, "
5008 					"evt_tag:x%x, index:x%x\n",
5009 					acqe_fip->event_tag,
5010 					acqe_fip->index);
5011 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
5012 			/*
5013 			 * During period of FCF discovery, read the FCF
5014 			 * table record indexed by the event to update
5015 			 * FCF roundrobin failover eligible FCF bmask.
5016 			 */
5017 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5018 					LOG_DISCOVERY,
5019 					"2779 Read FCF (x%x) for updating "
5020 					"roundrobin FCF failover bmask\n",
5021 					acqe_fip->index);
5022 			rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
5023 		}
5024 
5025 		/* If the FCF discovery is in progress, do nothing. */
5026 		spin_lock_irq(&phba->hbalock);
5027 		if (phba->hba_flag & FCF_TS_INPROG) {
5028 			spin_unlock_irq(&phba->hbalock);
5029 			break;
5030 		}
5031 		/* If fast FCF failover rescan event is pending, do nothing */
5032 		if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
5033 			spin_unlock_irq(&phba->hbalock);
5034 			break;
5035 		}
5036 
5037 		/* If the FCF has been in discovered state, do nothing. */
5038 		if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
5039 			spin_unlock_irq(&phba->hbalock);
5040 			break;
5041 		}
5042 		spin_unlock_irq(&phba->hbalock);
5043 
5044 		/* Otherwise, scan the entire FCF table and re-discover SAN */
5045 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5046 				"2770 Start FCF table scan per async FCF "
5047 				"event, evt_tag:x%x, index:x%x\n",
5048 				acqe_fip->event_tag, acqe_fip->index);
5049 		rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5050 						     LPFC_FCOE_FCF_GET_FIRST);
5051 		if (rc)
5052 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5053 					"2547 Issue FCF scan read FCF mailbox "
5054 					"command failed (x%x)\n", rc);
5055 		break;
5056 
5057 	case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
5058 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5059 			"2548 FCF Table full count 0x%x tag 0x%x\n",
5060 			bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5061 			acqe_fip->event_tag);
5062 		break;
5063 
5064 	case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
5065 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5066 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5067 			"2549 FCF (x%x) disconnected from network, "
5068 			"tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
5069 		/*
5070 		 * If we are in the middle of FCF failover process, clear
5071 		 * the corresponding FCF bit in the roundrobin bitmap.
5072 		 */
5073 		spin_lock_irq(&phba->hbalock);
5074 		if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5075 		    (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
5076 			spin_unlock_irq(&phba->hbalock);
5077 			/* Update FLOGI FCF failover eligible FCF bmask */
5078 			lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
5079 			break;
5080 		}
5081 		spin_unlock_irq(&phba->hbalock);
5082 
5083 		/* If the event is not for currently used fcf do nothing */
5084 		if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
5085 			break;
5086 
5087 		/*
5088 		 * Otherwise, request the port to rediscover the entire FCF
5089 		 * table for a fast recovery from case that the current FCF
5090 		 * is no longer valid as we are not in the middle of FCF
5091 		 * failover process already.
5092 		 */
5093 		spin_lock_irq(&phba->hbalock);
5094 		/* Mark the fast failover process in progress */
5095 		phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5096 		spin_unlock_irq(&phba->hbalock);
5097 
5098 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5099 				"2771 Start FCF fast failover process due to "
5100 				"FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5101 				"\n", acqe_fip->event_tag, acqe_fip->index);
5102 		rc = lpfc_sli4_redisc_fcf_table(phba);
5103 		if (rc) {
5104 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5105 					LOG_DISCOVERY,
5106 					"2772 Issue FCF rediscover mabilbox "
5107 					"command failed, fail through to FCF "
5108 					"dead event\n");
5109 			spin_lock_irq(&phba->hbalock);
5110 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5111 			spin_unlock_irq(&phba->hbalock);
5112 			/*
5113 			 * Last resort will fail over by treating this
5114 			 * as a link down to FCF registration.
5115 			 */
5116 			lpfc_sli4_fcf_dead_failthrough(phba);
5117 		} else {
5118 			/* Reset FCF roundrobin bmask for new discovery */
5119 			lpfc_sli4_clear_fcf_rr_bmask(phba);
5120 			/*
5121 			 * Handling fast FCF failover to a DEAD FCF event is
5122 			 * considered equalivant to receiving CVL to all vports.
5123 			 */
5124 			lpfc_sli4_perform_all_vport_cvl(phba);
5125 		}
5126 		break;
5127 	case LPFC_FIP_EVENT_TYPE_CVL:
5128 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5129 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5130 			"2718 Clear Virtual Link Received for VPI 0x%x"
5131 			" tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
5132 
5133 		vport = lpfc_find_vport_by_vpid(phba,
5134 						acqe_fip->index);
5135 		ndlp = lpfc_sli4_perform_vport_cvl(vport);
5136 		if (!ndlp)
5137 			break;
5138 		active_vlink_present = 0;
5139 
5140 		vports = lpfc_create_vport_work_array(phba);
5141 		if (vports) {
5142 			for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5143 					i++) {
5144 				if ((!(vports[i]->fc_flag &
5145 					FC_VPORT_CVL_RCVD)) &&
5146 					(vports[i]->port_state > LPFC_FDISC)) {
5147 					active_vlink_present = 1;
5148 					break;
5149 				}
5150 			}
5151 			lpfc_destroy_vport_work_array(phba, vports);
5152 		}
5153 
5154 		/*
5155 		 * Don't re-instantiate if vport is marked for deletion.
5156 		 * If we are here first then vport_delete is going to wait
5157 		 * for discovery to complete.
5158 		 */
5159 		if (!(vport->load_flag & FC_UNLOADING) &&
5160 					active_vlink_present) {
5161 			/*
5162 			 * If there are other active VLinks present,
5163 			 * re-instantiate the Vlink using FDISC.
5164 			 */
5165 			mod_timer(&ndlp->nlp_delayfunc,
5166 				  jiffies + msecs_to_jiffies(1000));
5167 			shost = lpfc_shost_from_vport(vport);
5168 			spin_lock_irq(shost->host_lock);
5169 			ndlp->nlp_flag |= NLP_DELAY_TMO;
5170 			spin_unlock_irq(shost->host_lock);
5171 			ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5172 			vport->port_state = LPFC_FDISC;
5173 		} else {
5174 			/*
5175 			 * Otherwise, we request port to rediscover
5176 			 * the entire FCF table for a fast recovery
5177 			 * from possible case that the current FCF
5178 			 * is no longer valid if we are not already
5179 			 * in the FCF failover process.
5180 			 */
5181 			spin_lock_irq(&phba->hbalock);
5182 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
5183 				spin_unlock_irq(&phba->hbalock);
5184 				break;
5185 			}
5186 			/* Mark the fast failover process in progress */
5187 			phba->fcf.fcf_flag |= FCF_ACVL_DISC;
5188 			spin_unlock_irq(&phba->hbalock);
5189 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5190 					LOG_DISCOVERY,
5191 					"2773 Start FCF failover per CVL, "
5192 					"evt_tag:x%x\n", acqe_fip->event_tag);
5193 			rc = lpfc_sli4_redisc_fcf_table(phba);
5194 			if (rc) {
5195 				lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5196 						LOG_DISCOVERY,
5197 						"2774 Issue FCF rediscover "
5198 						"mabilbox command failed, "
5199 						"through to CVL event\n");
5200 				spin_lock_irq(&phba->hbalock);
5201 				phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
5202 				spin_unlock_irq(&phba->hbalock);
5203 				/*
5204 				 * Last resort will be re-try on the
5205 				 * the current registered FCF entry.
5206 				 */
5207 				lpfc_retry_pport_discovery(phba);
5208 			} else
5209 				/*
5210 				 * Reset FCF roundrobin bmask for new
5211 				 * discovery.
5212 				 */
5213 				lpfc_sli4_clear_fcf_rr_bmask(phba);
5214 		}
5215 		break;
5216 	default:
5217 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5218 			"0288 Unknown FCoE event type 0x%x event tag "
5219 			"0x%x\n", event_type, acqe_fip->event_tag);
5220 		break;
5221 	}
5222 }
5223 
5224 /**
5225  * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5226  * @phba: pointer to lpfc hba data structure.
5227  * @acqe_link: pointer to the async dcbx completion queue entry.
5228  *
5229  * This routine is to handle the SLI4 asynchronous dcbx event.
5230  **/
5231 static void
5232 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5233 			 struct lpfc_acqe_dcbx *acqe_dcbx)
5234 {
5235 	phba->fc_eventTag = acqe_dcbx->event_tag;
5236 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5237 			"0290 The SLI4 DCBX asynchronous event is not "
5238 			"handled yet\n");
5239 }
5240 
5241 /**
5242  * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5243  * @phba: pointer to lpfc hba data structure.
5244  * @acqe_link: pointer to the async grp5 completion queue entry.
5245  *
5246  * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5247  * is an asynchronous notified of a logical link speed change.  The Port
5248  * reports the logical link speed in units of 10Mbps.
5249  **/
5250 static void
5251 lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5252 			 struct lpfc_acqe_grp5 *acqe_grp5)
5253 {
5254 	uint16_t prev_ll_spd;
5255 
5256 	phba->fc_eventTag = acqe_grp5->event_tag;
5257 	phba->fcoe_eventtag = acqe_grp5->event_tag;
5258 	prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5259 	phba->sli4_hba.link_state.logical_speed =
5260 		(bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
5261 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5262 			"2789 GRP5 Async Event: Updating logical link speed "
5263 			"from %dMbps to %dMbps\n", prev_ll_spd,
5264 			phba->sli4_hba.link_state.logical_speed);
5265 }
5266 
5267 /**
5268  * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5269  * @phba: pointer to lpfc hba data structure.
5270  *
5271  * This routine is invoked by the worker thread to process all the pending
5272  * SLI4 asynchronous events.
5273  **/
5274 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5275 {
5276 	struct lpfc_cq_event *cq_event;
5277 
5278 	/* First, declare the async event has been handled */
5279 	spin_lock_irq(&phba->hbalock);
5280 	phba->hba_flag &= ~ASYNC_EVENT;
5281 	spin_unlock_irq(&phba->hbalock);
5282 	/* Now, handle all the async events */
5283 	while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5284 		/* Get the first event from the head of the event queue */
5285 		spin_lock_irq(&phba->hbalock);
5286 		list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5287 				 cq_event, struct lpfc_cq_event, list);
5288 		spin_unlock_irq(&phba->hbalock);
5289 		/* Process the asynchronous event */
5290 		switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5291 		case LPFC_TRAILER_CODE_LINK:
5292 			lpfc_sli4_async_link_evt(phba,
5293 						 &cq_event->cqe.acqe_link);
5294 			break;
5295 		case LPFC_TRAILER_CODE_FCOE:
5296 			lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
5297 			break;
5298 		case LPFC_TRAILER_CODE_DCBX:
5299 			lpfc_sli4_async_dcbx_evt(phba,
5300 						 &cq_event->cqe.acqe_dcbx);
5301 			break;
5302 		case LPFC_TRAILER_CODE_GRP5:
5303 			lpfc_sli4_async_grp5_evt(phba,
5304 						 &cq_event->cqe.acqe_grp5);
5305 			break;
5306 		case LPFC_TRAILER_CODE_FC:
5307 			lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5308 			break;
5309 		case LPFC_TRAILER_CODE_SLI:
5310 			lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5311 			break;
5312 		default:
5313 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5314 					"1804 Invalid asynchrous event code: "
5315 					"x%x\n", bf_get(lpfc_trailer_code,
5316 					&cq_event->cqe.mcqe_cmpl));
5317 			break;
5318 		}
5319 		/* Free the completion event processed to the free pool */
5320 		lpfc_sli4_cq_event_release(phba, cq_event);
5321 	}
5322 }
5323 
5324 /**
5325  * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5326  * @phba: pointer to lpfc hba data structure.
5327  *
5328  * This routine is invoked by the worker thread to process FCF table
5329  * rediscovery pending completion event.
5330  **/
5331 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5332 {
5333 	int rc;
5334 
5335 	spin_lock_irq(&phba->hbalock);
5336 	/* Clear FCF rediscovery timeout event */
5337 	phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5338 	/* Clear driver fast failover FCF record flag */
5339 	phba->fcf.failover_rec.flag = 0;
5340 	/* Set state for FCF fast failover */
5341 	phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5342 	spin_unlock_irq(&phba->hbalock);
5343 
5344 	/* Scan FCF table from the first entry to re-discover SAN */
5345 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5346 			"2777 Start post-quiescent FCF table scan\n");
5347 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
5348 	if (rc)
5349 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5350 				"2747 Issue FCF scan read FCF mailbox "
5351 				"command failed 0x%x\n", rc);
5352 }
5353 
5354 /**
5355  * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5356  * @phba: pointer to lpfc hba data structure.
5357  * @dev_grp: The HBA PCI-Device group number.
5358  *
5359  * This routine is invoked to set up the per HBA PCI-Device group function
5360  * API jump table entries.
5361  *
5362  * Return: 0 if success, otherwise -ENODEV
5363  **/
5364 int
5365 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5366 {
5367 	int rc;
5368 
5369 	/* Set up lpfc PCI-device group */
5370 	phba->pci_dev_grp = dev_grp;
5371 
5372 	/* The LPFC_PCI_DEV_OC uses SLI4 */
5373 	if (dev_grp == LPFC_PCI_DEV_OC)
5374 		phba->sli_rev = LPFC_SLI_REV4;
5375 
5376 	/* Set up device INIT API function jump table */
5377 	rc = lpfc_init_api_table_setup(phba, dev_grp);
5378 	if (rc)
5379 		return -ENODEV;
5380 	/* Set up SCSI API function jump table */
5381 	rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5382 	if (rc)
5383 		return -ENODEV;
5384 	/* Set up SLI API function jump table */
5385 	rc = lpfc_sli_api_table_setup(phba, dev_grp);
5386 	if (rc)
5387 		return -ENODEV;
5388 	/* Set up MBOX API function jump table */
5389 	rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5390 	if (rc)
5391 		return -ENODEV;
5392 
5393 	return 0;
5394 }
5395 
5396 /**
5397  * lpfc_log_intr_mode - Log the active interrupt mode
5398  * @phba: pointer to lpfc hba data structure.
5399  * @intr_mode: active interrupt mode adopted.
5400  *
5401  * This routine it invoked to log the currently used active interrupt mode
5402  * to the device.
5403  **/
5404 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
5405 {
5406 	switch (intr_mode) {
5407 	case 0:
5408 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5409 				"0470 Enable INTx interrupt mode.\n");
5410 		break;
5411 	case 1:
5412 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5413 				"0481 Enabled MSI interrupt mode.\n");
5414 		break;
5415 	case 2:
5416 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5417 				"0480 Enabled MSI-X interrupt mode.\n");
5418 		break;
5419 	default:
5420 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5421 				"0482 Illegal interrupt mode.\n");
5422 		break;
5423 	}
5424 	return;
5425 }
5426 
5427 /**
5428  * lpfc_enable_pci_dev - Enable a generic PCI device.
5429  * @phba: pointer to lpfc hba data structure.
5430  *
5431  * This routine is invoked to enable the PCI device that is common to all
5432  * PCI devices.
5433  *
5434  * Return codes
5435  * 	0 - successful
5436  * 	other values - error
5437  **/
5438 static int
5439 lpfc_enable_pci_dev(struct lpfc_hba *phba)
5440 {
5441 	struct pci_dev *pdev;
5442 
5443 	/* Obtain PCI device reference */
5444 	if (!phba->pcidev)
5445 		goto out_error;
5446 	else
5447 		pdev = phba->pcidev;
5448 	/* Enable PCI device */
5449 	if (pci_enable_device_mem(pdev))
5450 		goto out_error;
5451 	/* Request PCI resource for the device */
5452 	if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
5453 		goto out_disable_device;
5454 	/* Set up device as PCI master and save state for EEH */
5455 	pci_set_master(pdev);
5456 	pci_try_set_mwi(pdev);
5457 	pci_save_state(pdev);
5458 
5459 	/* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5460 	if (pci_is_pcie(pdev))
5461 		pdev->needs_freset = 1;
5462 
5463 	return 0;
5464 
5465 out_disable_device:
5466 	pci_disable_device(pdev);
5467 out_error:
5468 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5469 			"1401 Failed to enable pci device\n");
5470 	return -ENODEV;
5471 }
5472 
5473 /**
5474  * lpfc_disable_pci_dev - Disable a generic PCI device.
5475  * @phba: pointer to lpfc hba data structure.
5476  *
5477  * This routine is invoked to disable the PCI device that is common to all
5478  * PCI devices.
5479  **/
5480 static void
5481 lpfc_disable_pci_dev(struct lpfc_hba *phba)
5482 {
5483 	struct pci_dev *pdev;
5484 
5485 	/* Obtain PCI device reference */
5486 	if (!phba->pcidev)
5487 		return;
5488 	else
5489 		pdev = phba->pcidev;
5490 	/* Release PCI resource and disable PCI device */
5491 	pci_release_mem_regions(pdev);
5492 	pci_disable_device(pdev);
5493 
5494 	return;
5495 }
5496 
5497 /**
5498  * lpfc_reset_hba - Reset a hba
5499  * @phba: pointer to lpfc hba data structure.
5500  *
5501  * This routine is invoked to reset a hba device. It brings the HBA
5502  * offline, performs a board restart, and then brings the board back
5503  * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5504  * on outstanding mailbox commands.
5505  **/
5506 void
5507 lpfc_reset_hba(struct lpfc_hba *phba)
5508 {
5509 	/* If resets are disabled then set error state and return. */
5510 	if (!phba->cfg_enable_hba_reset) {
5511 		phba->link_state = LPFC_HBA_ERROR;
5512 		return;
5513 	}
5514 	if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5515 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5516 	else
5517 		lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
5518 	lpfc_offline(phba);
5519 	lpfc_sli_brdrestart(phba);
5520 	lpfc_online(phba);
5521 	lpfc_unblock_mgmt_io(phba);
5522 }
5523 
5524 /**
5525  * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5526  * @phba: pointer to lpfc hba data structure.
5527  *
5528  * This function enables the PCI SR-IOV virtual functions to a physical
5529  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5530  * enable the number of virtual functions to the physical function. As
5531  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5532  * API call does not considered as an error condition for most of the device.
5533  **/
5534 uint16_t
5535 lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5536 {
5537 	struct pci_dev *pdev = phba->pcidev;
5538 	uint16_t nr_virtfn;
5539 	int pos;
5540 
5541 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5542 	if (pos == 0)
5543 		return 0;
5544 
5545 	pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5546 	return nr_virtfn;
5547 }
5548 
5549 /**
5550  * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5551  * @phba: pointer to lpfc hba data structure.
5552  * @nr_vfn: number of virtual functions to be enabled.
5553  *
5554  * This function enables the PCI SR-IOV virtual functions to a physical
5555  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5556  * enable the number of virtual functions to the physical function. As
5557  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5558  * API call does not considered as an error condition for most of the device.
5559  **/
5560 int
5561 lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5562 {
5563 	struct pci_dev *pdev = phba->pcidev;
5564 	uint16_t max_nr_vfn;
5565 	int rc;
5566 
5567 	max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5568 	if (nr_vfn > max_nr_vfn) {
5569 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5570 				"3057 Requested vfs (%d) greater than "
5571 				"supported vfs (%d)", nr_vfn, max_nr_vfn);
5572 		return -EINVAL;
5573 	}
5574 
5575 	rc = pci_enable_sriov(pdev, nr_vfn);
5576 	if (rc) {
5577 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5578 				"2806 Failed to enable sriov on this device "
5579 				"with vfn number nr_vf:%d, rc:%d\n",
5580 				nr_vfn, rc);
5581 	} else
5582 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5583 				"2807 Successful enable sriov on this device "
5584 				"with vfn number nr_vf:%d\n", nr_vfn);
5585 	return rc;
5586 }
5587 
5588 /**
5589  * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5590  * @phba: pointer to lpfc hba data structure.
5591  *
5592  * This routine is invoked to set up the driver internal resources before the
5593  * device specific resource setup to support the HBA device it attached to.
5594  *
5595  * Return codes
5596  *	0 - successful
5597  *	other values - error
5598  **/
5599 static int
5600 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5601 {
5602 	struct lpfc_sli *psli = &phba->sli;
5603 
5604 	/*
5605 	 * Driver resources common to all SLI revisions
5606 	 */
5607 	atomic_set(&phba->fast_event_count, 0);
5608 	spin_lock_init(&phba->hbalock);
5609 
5610 	/* Initialize ndlp management spinlock */
5611 	spin_lock_init(&phba->ndlp_lock);
5612 
5613 	INIT_LIST_HEAD(&phba->port_list);
5614 	INIT_LIST_HEAD(&phba->work_list);
5615 	init_waitqueue_head(&phba->wait_4_mlo_m_q);
5616 
5617 	/* Initialize the wait queue head for the kernel thread */
5618 	init_waitqueue_head(&phba->work_waitq);
5619 
5620 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5621 			"1403 Protocols supported %s %s %s\n",
5622 			((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5623 				"SCSI" : " "),
5624 			((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
5625 				"NVME" : " "),
5626 			(phba->nvmet_support ? "NVMET" : " "));
5627 
5628 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5629 		/* Initialize the scsi buffer list used by driver for scsi IO */
5630 		spin_lock_init(&phba->scsi_buf_list_get_lock);
5631 		INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5632 		spin_lock_init(&phba->scsi_buf_list_put_lock);
5633 		INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5634 	}
5635 
5636 	if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5637 		(phba->nvmet_support == 0)) {
5638 		/* Initialize the NVME buffer list used by driver for NVME IO */
5639 		spin_lock_init(&phba->nvme_buf_list_get_lock);
5640 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5641 		phba->get_nvme_bufs = 0;
5642 		spin_lock_init(&phba->nvme_buf_list_put_lock);
5643 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5644 		phba->put_nvme_bufs = 0;
5645 	}
5646 
5647 	/* Initialize the fabric iocb list */
5648 	INIT_LIST_HEAD(&phba->fabric_iocb_list);
5649 
5650 	/* Initialize list to save ELS buffers */
5651 	INIT_LIST_HEAD(&phba->elsbuf);
5652 
5653 	/* Initialize FCF connection rec list */
5654 	INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5655 
5656 	/* Initialize OAS configuration list */
5657 	spin_lock_init(&phba->devicelock);
5658 	INIT_LIST_HEAD(&phba->luns);
5659 
5660 	/* MBOX heartbeat timer */
5661 	timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
5662 	/* Fabric block timer */
5663 	timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
5664 	/* EA polling mode timer */
5665 	timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
5666 	/* Heartbeat timer */
5667 	timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
5668 
5669 	return 0;
5670 }
5671 
5672 /**
5673  * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5674  * @phba: pointer to lpfc hba data structure.
5675  *
5676  * This routine is invoked to set up the driver internal resources specific to
5677  * support the SLI-3 HBA device it attached to.
5678  *
5679  * Return codes
5680  * 0 - successful
5681  * other values - error
5682  **/
5683 static int
5684 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5685 {
5686 	int rc;
5687 
5688 	/*
5689 	 * Initialize timers used by driver
5690 	 */
5691 
5692 	/* FCP polling mode timer */
5693 	timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
5694 
5695 	/* Host attention work mask setup */
5696 	phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5697 	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5698 
5699 	/* Get all the module params for configuring this host */
5700 	lpfc_get_cfgparam(phba);
5701 	/* Set up phase-1 common device driver resources */
5702 
5703 	rc = lpfc_setup_driver_resource_phase1(phba);
5704 	if (rc)
5705 		return -ENODEV;
5706 
5707 	if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5708 		phba->menlo_flag |= HBA_MENLO_SUPPORT;
5709 		/* check for menlo minimum sg count */
5710 		if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5711 			phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5712 	}
5713 
5714 	if (!phba->sli.sli3_ring)
5715 		phba->sli.sli3_ring = kzalloc(LPFC_SLI3_MAX_RING *
5716 			sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5717 	if (!phba->sli.sli3_ring)
5718 		return -ENOMEM;
5719 
5720 	/*
5721 	 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
5722 	 * used to create the sg_dma_buf_pool must be dynamically calculated.
5723 	 */
5724 
5725 	/* Initialize the host templates the configured values. */
5726 	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5727 	lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5728 	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5729 
5730 	/* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5731 	if (phba->cfg_enable_bg) {
5732 		/*
5733 		 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5734 		 * the FCP rsp, and a BDE for each. Sice we have no control
5735 		 * over how many protection data segments the SCSI Layer
5736 		 * will hand us (ie: there could be one for every block
5737 		 * in the IO), we just allocate enough BDEs to accomidate
5738 		 * our max amount and we need to limit lpfc_sg_seg_cnt to
5739 		 * minimize the risk of running out.
5740 		 */
5741 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5742 			sizeof(struct fcp_rsp) +
5743 			(LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5744 
5745 		if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5746 			phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5747 
5748 		/* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5749 		phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5750 	} else {
5751 		/*
5752 		 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5753 		 * the FCP rsp, a BDE for each, and a BDE for up to
5754 		 * cfg_sg_seg_cnt data segments.
5755 		 */
5756 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5757 			sizeof(struct fcp_rsp) +
5758 			((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5759 
5760 		/* Total BDEs in BPL for scsi_sg_list */
5761 		phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5762 	}
5763 
5764 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5765 			"9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5766 			phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5767 			phba->cfg_total_seg_cnt);
5768 
5769 	phba->max_vpi = LPFC_MAX_VPI;
5770 	/* This will be set to correct value after config_port mbox */
5771 	phba->max_vports = 0;
5772 
5773 	/*
5774 	 * Initialize the SLI Layer to run with lpfc HBAs.
5775 	 */
5776 	lpfc_sli_setup(phba);
5777 	lpfc_sli_queue_init(phba);
5778 
5779 	/* Allocate device driver memory */
5780 	if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5781 		return -ENOMEM;
5782 
5783 	/*
5784 	 * Enable sr-iov virtual functions if supported and configured
5785 	 * through the module parameter.
5786 	 */
5787 	if (phba->cfg_sriov_nr_virtfn > 0) {
5788 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5789 						 phba->cfg_sriov_nr_virtfn);
5790 		if (rc) {
5791 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5792 					"2808 Requested number of SR-IOV "
5793 					"virtual functions (%d) is not "
5794 					"supported\n",
5795 					phba->cfg_sriov_nr_virtfn);
5796 			phba->cfg_sriov_nr_virtfn = 0;
5797 		}
5798 	}
5799 
5800 	return 0;
5801 }
5802 
5803 /**
5804  * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5805  * @phba: pointer to lpfc hba data structure.
5806  *
5807  * This routine is invoked to unset the driver internal resources set up
5808  * specific for supporting the SLI-3 HBA device it attached to.
5809  **/
5810 static void
5811 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5812 {
5813 	/* Free device driver memory allocated */
5814 	lpfc_mem_free_all(phba);
5815 
5816 	return;
5817 }
5818 
5819 /**
5820  * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5821  * @phba: pointer to lpfc hba data structure.
5822  *
5823  * This routine is invoked to set up the driver internal resources specific to
5824  * support the SLI-4 HBA device it attached to.
5825  *
5826  * Return codes
5827  * 	0 - successful
5828  * 	other values - error
5829  **/
5830 static int
5831 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5832 {
5833 	LPFC_MBOXQ_t *mboxq;
5834 	MAILBOX_t *mb;
5835 	int rc, i, max_buf_size;
5836 	uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5837 	struct lpfc_mqe *mqe;
5838 	int longs;
5839 	int fof_vectors = 0;
5840 	int extra;
5841 	uint64_t wwn;
5842 
5843 	phba->sli4_hba.num_online_cpu = num_online_cpus();
5844 	phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5845 	phba->sli4_hba.curr_disp_cpu = 0;
5846 
5847 	/* Get all the module params for configuring this host */
5848 	lpfc_get_cfgparam(phba);
5849 
5850 	/* Set up phase-1 common device driver resources */
5851 	rc = lpfc_setup_driver_resource_phase1(phba);
5852 	if (rc)
5853 		return -ENODEV;
5854 
5855 	/* Before proceed, wait for POST done and device ready */
5856 	rc = lpfc_sli4_post_status_check(phba);
5857 	if (rc)
5858 		return -ENODEV;
5859 
5860 	/*
5861 	 * Initialize timers used by driver
5862 	 */
5863 
5864 	timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
5865 
5866 	/* FCF rediscover timer */
5867 	timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
5868 
5869 	/*
5870 	 * Control structure for handling external multi-buffer mailbox
5871 	 * command pass-through.
5872 	 */
5873 	memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5874 		sizeof(struct lpfc_mbox_ext_buf_ctx));
5875 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5876 
5877 	phba->max_vpi = LPFC_MAX_VPI;
5878 
5879 	/* This will be set to correct value after the read_config mbox */
5880 	phba->max_vports = 0;
5881 
5882 	/* Program the default value of vlan_id and fc_map */
5883 	phba->valid_vlan = 0;
5884 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5885 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5886 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5887 
5888 	/*
5889 	 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5890 	 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5891 	 * The WQ create will allocate the ring.
5892 	 */
5893 
5894 	/*
5895 	 * 1 for cmd, 1 for rsp, NVME adds an extra one
5896 	 * for boundary conditions in its max_sgl_segment template.
5897 	 */
5898 	extra = 2;
5899 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
5900 		extra++;
5901 
5902 	/*
5903 	 * It doesn't matter what family our adapter is in, we are
5904 	 * limited to 2 Pages, 512 SGEs, for our SGL.
5905 	 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5906 	 */
5907 	max_buf_size = (2 * SLI4_PAGE_SIZE);
5908 	if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - extra)
5909 		phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - extra;
5910 
5911 	/*
5912 	 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5913 	 * used to create the sg_dma_buf_pool must be calculated.
5914 	 */
5915 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5916 		/*
5917 		 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5918 		 * the FCP rsp, and a SGE. Sice we have no control
5919 		 * over how many protection segments the SCSI Layer
5920 		 * will hand us (ie: there could be one for every block
5921 		 * in the IO), just allocate enough SGEs to accomidate
5922 		 * our max amount and we need to limit lpfc_sg_seg_cnt
5923 		 * to minimize the risk of running out.
5924 		 */
5925 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5926 				sizeof(struct fcp_rsp) + max_buf_size;
5927 
5928 		/* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5929 		phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5930 
5931 		if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5932 			phba->cfg_sg_seg_cnt =
5933 				LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5934 	} else {
5935 		/*
5936 		 * The scsi_buf for a regular I/O holds the FCP cmnd,
5937 		 * the FCP rsp, a SGE for each, and a SGE for up to
5938 		 * cfg_sg_seg_cnt data segments.
5939 		 */
5940 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5941 				sizeof(struct fcp_rsp) +
5942 				((phba->cfg_sg_seg_cnt + extra) *
5943 				sizeof(struct sli4_sge));
5944 
5945 		/* Total SGEs for scsi_sg_list */
5946 		phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + extra;
5947 
5948 		/*
5949 		 * NOTE: if (phba->cfg_sg_seg_cnt + extra) <= 256 we only
5950 		 * need to post 1 page for the SGL.
5951 		 */
5952 	}
5953 
5954 	/* Initialize the host templates with the updated values. */
5955 	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5956 	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5957 	lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5958 
5959 	if (phba->cfg_sg_dma_buf_size  <= LPFC_MIN_SG_SLI4_BUF_SZ)
5960 		phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5961 	else
5962 		phba->cfg_sg_dma_buf_size =
5963 			SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5964 
5965 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5966 			"9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5967 			phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5968 			phba->cfg_total_seg_cnt);
5969 
5970 	/* Initialize buffer queue management fields */
5971 	INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
5972 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5973 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5974 
5975 	/*
5976 	 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5977 	 */
5978 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5979 		/* Initialize the Abort scsi buffer list used by driver */
5980 		spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5981 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5982 	}
5983 
5984 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
5985 		/* Initialize the Abort nvme buffer list used by driver */
5986 		spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
5987 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
5988 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
5989 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
5990 	}
5991 
5992 	/* This abort list used by worker thread */
5993 	spin_lock_init(&phba->sli4_hba.sgl_list_lock);
5994 	spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
5995 
5996 	/*
5997 	 * Initialize driver internal slow-path work queues
5998 	 */
5999 
6000 	/* Driver internel slow-path CQ Event pool */
6001 	INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
6002 	/* Response IOCB work queue list */
6003 	INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
6004 	/* Asynchronous event CQ Event work queue list */
6005 	INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
6006 	/* Fast-path XRI aborted CQ Event work queue list */
6007 	INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
6008 	/* Slow-path XRI aborted CQ Event work queue list */
6009 	INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
6010 	/* Receive queue CQ Event work queue list */
6011 	INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
6012 
6013 	/* Initialize extent block lists. */
6014 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
6015 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
6016 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
6017 	INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
6018 
6019 	/* Initialize mboxq lists. If the early init routines fail
6020 	 * these lists need to be correctly initialized.
6021 	 */
6022 	INIT_LIST_HEAD(&phba->sli.mboxq);
6023 	INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
6024 
6025 	/* initialize optic_state to 0xFF */
6026 	phba->sli4_hba.lnk_info.optic_state = 0xff;
6027 
6028 	/* Allocate device driver memory */
6029 	rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
6030 	if (rc)
6031 		return -ENOMEM;
6032 
6033 	/* IF Type 2 ports get initialized now. */
6034 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
6035 	    LPFC_SLI_INTF_IF_TYPE_2) {
6036 		rc = lpfc_pci_function_reset(phba);
6037 		if (unlikely(rc)) {
6038 			rc = -ENODEV;
6039 			goto out_free_mem;
6040 		}
6041 		phba->temp_sensor_support = 1;
6042 	}
6043 
6044 	/* Create the bootstrap mailbox command */
6045 	rc = lpfc_create_bootstrap_mbox(phba);
6046 	if (unlikely(rc))
6047 		goto out_free_mem;
6048 
6049 	/* Set up the host's endian order with the device. */
6050 	rc = lpfc_setup_endian_order(phba);
6051 	if (unlikely(rc))
6052 		goto out_free_bsmbx;
6053 
6054 	/* Set up the hba's configuration parameters. */
6055 	rc = lpfc_sli4_read_config(phba);
6056 	if (unlikely(rc))
6057 		goto out_free_bsmbx;
6058 	rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6059 	if (unlikely(rc))
6060 		goto out_free_bsmbx;
6061 
6062 	/* IF Type 0 ports get initialized now. */
6063 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6064 	    LPFC_SLI_INTF_IF_TYPE_0) {
6065 		rc = lpfc_pci_function_reset(phba);
6066 		if (unlikely(rc))
6067 			goto out_free_bsmbx;
6068 	}
6069 
6070 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6071 						       GFP_KERNEL);
6072 	if (!mboxq) {
6073 		rc = -ENOMEM;
6074 		goto out_free_bsmbx;
6075 	}
6076 
6077 	/* Check for NVMET being configured */
6078 	phba->nvmet_support = 0;
6079 	if (lpfc_enable_nvmet_cnt) {
6080 
6081 		/* First get WWN of HBA instance */
6082 		lpfc_read_nv(phba, mboxq);
6083 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6084 		if (rc != MBX_SUCCESS) {
6085 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6086 					"6016 Mailbox failed , mbxCmd x%x "
6087 					"READ_NV, mbxStatus x%x\n",
6088 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6089 					bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6090 			mempool_free(mboxq, phba->mbox_mem_pool);
6091 			rc = -EIO;
6092 			goto out_free_bsmbx;
6093 		}
6094 		mb = &mboxq->u.mb;
6095 		memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6096 		       sizeof(uint64_t));
6097 		wwn = cpu_to_be64(wwn);
6098 		phba->sli4_hba.wwnn.u.name = wwn;
6099 		memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6100 		       sizeof(uint64_t));
6101 		/* wwn is WWPN of HBA instance */
6102 		wwn = cpu_to_be64(wwn);
6103 		phba->sli4_hba.wwpn.u.name = wwn;
6104 
6105 		/* Check to see if it matches any module parameter */
6106 		for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6107 			if (wwn == lpfc_enable_nvmet[i]) {
6108 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
6109 				if (lpfc_nvmet_mem_alloc(phba))
6110 					break;
6111 
6112 				phba->nvmet_support = 1; /* a match */
6113 
6114 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6115 						"6017 NVME Target %016llx\n",
6116 						wwn);
6117 #else
6118 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6119 						"6021 Can't enable NVME Target."
6120 						" NVME_TARGET_FC infrastructure"
6121 						" is not in kernel\n");
6122 #endif
6123 				break;
6124 			}
6125 		}
6126 	}
6127 
6128 	lpfc_nvme_mod_param_dep(phba);
6129 
6130 	/* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
6131 	lpfc_supported_pages(mboxq);
6132 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6133 	if (!rc) {
6134 		mqe = &mboxq->u.mqe;
6135 		memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6136 		       LPFC_MAX_SUPPORTED_PAGES);
6137 		for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6138 			switch (pn_page[i]) {
6139 			case LPFC_SLI4_PARAMETERS:
6140 				phba->sli4_hba.pc_sli4_params.supported = 1;
6141 				break;
6142 			default:
6143 				break;
6144 			}
6145 		}
6146 		/* Read the port's SLI4 Parameters capabilities if supported. */
6147 		if (phba->sli4_hba.pc_sli4_params.supported)
6148 			rc = lpfc_pc_sli4_params_get(phba, mboxq);
6149 		if (rc) {
6150 			mempool_free(mboxq, phba->mbox_mem_pool);
6151 			rc = -EIO;
6152 			goto out_free_bsmbx;
6153 		}
6154 	}
6155 
6156 	/*
6157 	 * Get sli4 parameters that override parameters from Port capabilities.
6158 	 * If this call fails, it isn't critical unless the SLI4 parameters come
6159 	 * back in conflict.
6160 	 */
6161 	rc = lpfc_get_sli4_parameters(phba, mboxq);
6162 	if (rc) {
6163 		if (phba->sli4_hba.extents_in_use &&
6164 		    phba->sli4_hba.rpi_hdrs_in_use) {
6165 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6166 				"2999 Unsupported SLI4 Parameters "
6167 				"Extents and RPI headers enabled.\n");
6168 		}
6169 		mempool_free(mboxq, phba->mbox_mem_pool);
6170 		rc = -EIO;
6171 		goto out_free_bsmbx;
6172 	}
6173 
6174 	mempool_free(mboxq, phba->mbox_mem_pool);
6175 
6176 	/* Verify OAS is supported */
6177 	lpfc_sli4_oas_verify(phba);
6178 	if (phba->cfg_fof)
6179 		fof_vectors = 1;
6180 
6181 	/* Verify all the SLI4 queues */
6182 	rc = lpfc_sli4_queue_verify(phba);
6183 	if (rc)
6184 		goto out_free_bsmbx;
6185 
6186 	/* Create driver internal CQE event pool */
6187 	rc = lpfc_sli4_cq_event_pool_create(phba);
6188 	if (rc)
6189 		goto out_free_bsmbx;
6190 
6191 	/* Initialize sgl lists per host */
6192 	lpfc_init_sgl_list(phba);
6193 
6194 	/* Allocate and initialize active sgl array */
6195 	rc = lpfc_init_active_sgl_array(phba);
6196 	if (rc) {
6197 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6198 				"1430 Failed to initialize sgl list.\n");
6199 		goto out_destroy_cq_event_pool;
6200 	}
6201 	rc = lpfc_sli4_init_rpi_hdrs(phba);
6202 	if (rc) {
6203 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6204 				"1432 Failed to initialize rpi headers.\n");
6205 		goto out_free_active_sgl;
6206 	}
6207 
6208 	/* Allocate eligible FCF bmask memory for FCF roundrobin failover */
6209 	longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
6210 	phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
6211 					 GFP_KERNEL);
6212 	if (!phba->fcf.fcf_rr_bmask) {
6213 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6214 				"2759 Failed allocate memory for FCF round "
6215 				"robin failover bmask\n");
6216 		rc = -ENOMEM;
6217 		goto out_remove_rpi_hdrs;
6218 	}
6219 
6220 	phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6221 						sizeof(struct lpfc_hba_eq_hdl),
6222 						GFP_KERNEL);
6223 	if (!phba->sli4_hba.hba_eq_hdl) {
6224 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6225 				"2572 Failed allocate memory for "
6226 				"fast-path per-EQ handle array\n");
6227 		rc = -ENOMEM;
6228 		goto out_free_fcf_rr_bmask;
6229 	}
6230 
6231 	phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6232 					sizeof(struct lpfc_vector_map_info),
6233 					GFP_KERNEL);
6234 	if (!phba->sli4_hba.cpu_map) {
6235 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6236 				"3327 Failed allocate memory for msi-x "
6237 				"interrupt vector mapping\n");
6238 		rc = -ENOMEM;
6239 		goto out_free_hba_eq_hdl;
6240 	}
6241 	if (lpfc_used_cpu == NULL) {
6242 		lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6243 						GFP_KERNEL);
6244 		if (!lpfc_used_cpu) {
6245 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6246 					"3335 Failed allocate memory for msi-x "
6247 					"interrupt vector mapping\n");
6248 			kfree(phba->sli4_hba.cpu_map);
6249 			rc = -ENOMEM;
6250 			goto out_free_hba_eq_hdl;
6251 		}
6252 		for (i = 0; i < lpfc_present_cpu; i++)
6253 			lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6254 	}
6255 
6256 	/*
6257 	 * Enable sr-iov virtual functions if supported and configured
6258 	 * through the module parameter.
6259 	 */
6260 	if (phba->cfg_sriov_nr_virtfn > 0) {
6261 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6262 						 phba->cfg_sriov_nr_virtfn);
6263 		if (rc) {
6264 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6265 					"3020 Requested number of SR-IOV "
6266 					"virtual functions (%d) is not "
6267 					"supported\n",
6268 					phba->cfg_sriov_nr_virtfn);
6269 			phba->cfg_sriov_nr_virtfn = 0;
6270 		}
6271 	}
6272 
6273 	return 0;
6274 
6275 out_free_hba_eq_hdl:
6276 	kfree(phba->sli4_hba.hba_eq_hdl);
6277 out_free_fcf_rr_bmask:
6278 	kfree(phba->fcf.fcf_rr_bmask);
6279 out_remove_rpi_hdrs:
6280 	lpfc_sli4_remove_rpi_hdrs(phba);
6281 out_free_active_sgl:
6282 	lpfc_free_active_sgl(phba);
6283 out_destroy_cq_event_pool:
6284 	lpfc_sli4_cq_event_pool_destroy(phba);
6285 out_free_bsmbx:
6286 	lpfc_destroy_bootstrap_mbox(phba);
6287 out_free_mem:
6288 	lpfc_mem_free(phba);
6289 	return rc;
6290 }
6291 
6292 /**
6293  * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6294  * @phba: pointer to lpfc hba data structure.
6295  *
6296  * This routine is invoked to unset the driver internal resources set up
6297  * specific for supporting the SLI-4 HBA device it attached to.
6298  **/
6299 static void
6300 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6301 {
6302 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6303 
6304 	/* Free memory allocated for msi-x interrupt vector to CPU mapping */
6305 	kfree(phba->sli4_hba.cpu_map);
6306 	phba->sli4_hba.num_present_cpu = 0;
6307 	phba->sli4_hba.num_online_cpu = 0;
6308 	phba->sli4_hba.curr_disp_cpu = 0;
6309 
6310 	/* Free memory allocated for fast-path work queue handles */
6311 	kfree(phba->sli4_hba.hba_eq_hdl);
6312 
6313 	/* Free the allocated rpi headers. */
6314 	lpfc_sli4_remove_rpi_hdrs(phba);
6315 	lpfc_sli4_remove_rpis(phba);
6316 
6317 	/* Free eligible FCF index bmask */
6318 	kfree(phba->fcf.fcf_rr_bmask);
6319 
6320 	/* Free the ELS sgl list */
6321 	lpfc_free_active_sgl(phba);
6322 	lpfc_free_els_sgl_list(phba);
6323 	lpfc_free_nvmet_sgl_list(phba);
6324 
6325 	/* Free the completion queue EQ event pool */
6326 	lpfc_sli4_cq_event_release_all(phba);
6327 	lpfc_sli4_cq_event_pool_destroy(phba);
6328 
6329 	/* Release resource identifiers. */
6330 	lpfc_sli4_dealloc_resource_identifiers(phba);
6331 
6332 	/* Free the bsmbx region. */
6333 	lpfc_destroy_bootstrap_mbox(phba);
6334 
6335 	/* Free the SLI Layer memory with SLI4 HBAs */
6336 	lpfc_mem_free_all(phba);
6337 
6338 	/* Free the current connect table */
6339 	list_for_each_entry_safe(conn_entry, next_conn_entry,
6340 		&phba->fcf_conn_rec_list, list) {
6341 		list_del_init(&conn_entry->list);
6342 		kfree(conn_entry);
6343 	}
6344 
6345 	return;
6346 }
6347 
6348 /**
6349  * lpfc_init_api_table_setup - Set up init api function jump table
6350  * @phba: The hba struct for which this call is being executed.
6351  * @dev_grp: The HBA PCI-Device group number.
6352  *
6353  * This routine sets up the device INIT interface API function jump table
6354  * in @phba struct.
6355  *
6356  * Returns: 0 - success, -ENODEV - failure.
6357  **/
6358 int
6359 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6360 {
6361 	phba->lpfc_hba_init_link = lpfc_hba_init_link;
6362 	phba->lpfc_hba_down_link = lpfc_hba_down_link;
6363 	phba->lpfc_selective_reset = lpfc_selective_reset;
6364 	switch (dev_grp) {
6365 	case LPFC_PCI_DEV_LP:
6366 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6367 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6368 		phba->lpfc_stop_port = lpfc_stop_port_s3;
6369 		break;
6370 	case LPFC_PCI_DEV_OC:
6371 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6372 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6373 		phba->lpfc_stop_port = lpfc_stop_port_s4;
6374 		break;
6375 	default:
6376 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6377 				"1431 Invalid HBA PCI-device group: 0x%x\n",
6378 				dev_grp);
6379 		return -ENODEV;
6380 		break;
6381 	}
6382 	return 0;
6383 }
6384 
6385 /**
6386  * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6387  * @phba: pointer to lpfc hba data structure.
6388  *
6389  * This routine is invoked to set up the driver internal resources after the
6390  * device specific resource setup to support the HBA device it attached to.
6391  *
6392  * Return codes
6393  * 	0 - successful
6394  * 	other values - error
6395  **/
6396 static int
6397 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6398 {
6399 	int error;
6400 
6401 	/* Startup the kernel thread for this host adapter. */
6402 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
6403 					  "lpfc_worker_%d", phba->brd_no);
6404 	if (IS_ERR(phba->worker_thread)) {
6405 		error = PTR_ERR(phba->worker_thread);
6406 		return error;
6407 	}
6408 
6409 	/* workqueue for deferred irq use */
6410 	phba->wq = alloc_workqueue("lpfc_wq", WQ_MEM_RECLAIM, 0);
6411 
6412 	return 0;
6413 }
6414 
6415 /**
6416  * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6417  * @phba: pointer to lpfc hba data structure.
6418  *
6419  * This routine is invoked to unset the driver internal resources set up after
6420  * the device specific resource setup for supporting the HBA device it
6421  * attached to.
6422  **/
6423 static void
6424 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6425 {
6426 	if (phba->wq) {
6427 		flush_workqueue(phba->wq);
6428 		destroy_workqueue(phba->wq);
6429 		phba->wq = NULL;
6430 	}
6431 
6432 	/* Stop kernel worker thread */
6433 	kthread_stop(phba->worker_thread);
6434 }
6435 
6436 /**
6437  * lpfc_free_iocb_list - Free iocb list.
6438  * @phba: pointer to lpfc hba data structure.
6439  *
6440  * This routine is invoked to free the driver's IOCB list and memory.
6441  **/
6442 void
6443 lpfc_free_iocb_list(struct lpfc_hba *phba)
6444 {
6445 	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6446 
6447 	spin_lock_irq(&phba->hbalock);
6448 	list_for_each_entry_safe(iocbq_entry, iocbq_next,
6449 				 &phba->lpfc_iocb_list, list) {
6450 		list_del(&iocbq_entry->list);
6451 		kfree(iocbq_entry);
6452 		phba->total_iocbq_bufs--;
6453 	}
6454 	spin_unlock_irq(&phba->hbalock);
6455 
6456 	return;
6457 }
6458 
6459 /**
6460  * lpfc_init_iocb_list - Allocate and initialize iocb list.
6461  * @phba: pointer to lpfc hba data structure.
6462  *
6463  * This routine is invoked to allocate and initizlize the driver's IOCB
6464  * list and set up the IOCB tag array accordingly.
6465  *
6466  * Return codes
6467  *	0 - successful
6468  *	other values - error
6469  **/
6470 int
6471 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6472 {
6473 	struct lpfc_iocbq *iocbq_entry = NULL;
6474 	uint16_t iotag;
6475 	int i;
6476 
6477 	/* Initialize and populate the iocb list per host.  */
6478 	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
6479 	for (i = 0; i < iocb_count; i++) {
6480 		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
6481 		if (iocbq_entry == NULL) {
6482 			printk(KERN_ERR "%s: only allocated %d iocbs of "
6483 				"expected %d count. Unloading driver.\n",
6484 				__func__, i, LPFC_IOCB_LIST_CNT);
6485 			goto out_free_iocbq;
6486 		}
6487 
6488 		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6489 		if (iotag == 0) {
6490 			kfree(iocbq_entry);
6491 			printk(KERN_ERR "%s: failed to allocate IOTAG. "
6492 				"Unloading driver.\n", __func__);
6493 			goto out_free_iocbq;
6494 		}
6495 		iocbq_entry->sli4_lxritag = NO_XRI;
6496 		iocbq_entry->sli4_xritag = NO_XRI;
6497 
6498 		spin_lock_irq(&phba->hbalock);
6499 		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6500 		phba->total_iocbq_bufs++;
6501 		spin_unlock_irq(&phba->hbalock);
6502 	}
6503 
6504 	return 0;
6505 
6506 out_free_iocbq:
6507 	lpfc_free_iocb_list(phba);
6508 
6509 	return -ENOMEM;
6510 }
6511 
6512 /**
6513  * lpfc_free_sgl_list - Free a given sgl list.
6514  * @phba: pointer to lpfc hba data structure.
6515  * @sglq_list: pointer to the head of sgl list.
6516  *
6517  * This routine is invoked to free a give sgl list and memory.
6518  **/
6519 void
6520 lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
6521 {
6522 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6523 
6524 	list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6525 		list_del(&sglq_entry->list);
6526 		lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6527 		kfree(sglq_entry);
6528 	}
6529 }
6530 
6531 /**
6532  * lpfc_free_els_sgl_list - Free els sgl list.
6533  * @phba: pointer to lpfc hba data structure.
6534  *
6535  * This routine is invoked to free the driver's els sgl list and memory.
6536  **/
6537 static void
6538 lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6539 {
6540 	LIST_HEAD(sglq_list);
6541 
6542 	/* Retrieve all els sgls from driver list */
6543 	spin_lock_irq(&phba->hbalock);
6544 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6545 	list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6546 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6547 	spin_unlock_irq(&phba->hbalock);
6548 
6549 	/* Now free the sgl list */
6550 	lpfc_free_sgl_list(phba, &sglq_list);
6551 }
6552 
6553 /**
6554  * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6555  * @phba: pointer to lpfc hba data structure.
6556  *
6557  * This routine is invoked to free the driver's nvmet sgl list and memory.
6558  **/
6559 static void
6560 lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6561 {
6562 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6563 	LIST_HEAD(sglq_list);
6564 
6565 	/* Retrieve all nvmet sgls from driver list */
6566 	spin_lock_irq(&phba->hbalock);
6567 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6568 	list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6569 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6570 	spin_unlock_irq(&phba->hbalock);
6571 
6572 	/* Now free the sgl list */
6573 	list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6574 		list_del(&sglq_entry->list);
6575 		lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6576 		kfree(sglq_entry);
6577 	}
6578 
6579 	/* Update the nvmet_xri_cnt to reflect no current sgls.
6580 	 * The next initialization cycle sets the count and allocates
6581 	 * the sgls over again.
6582 	 */
6583 	phba->sli4_hba.nvmet_xri_cnt = 0;
6584 }
6585 
6586 /**
6587  * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6588  * @phba: pointer to lpfc hba data structure.
6589  *
6590  * This routine is invoked to allocate the driver's active sgl memory.
6591  * This array will hold the sglq_entry's for active IOs.
6592  **/
6593 static int
6594 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6595 {
6596 	int size;
6597 	size = sizeof(struct lpfc_sglq *);
6598 	size *= phba->sli4_hba.max_cfg_param.max_xri;
6599 
6600 	phba->sli4_hba.lpfc_sglq_active_list =
6601 		kzalloc(size, GFP_KERNEL);
6602 	if (!phba->sli4_hba.lpfc_sglq_active_list)
6603 		return -ENOMEM;
6604 	return 0;
6605 }
6606 
6607 /**
6608  * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6609  * @phba: pointer to lpfc hba data structure.
6610  *
6611  * This routine is invoked to walk through the array of active sglq entries
6612  * and free all of the resources.
6613  * This is just a place holder for now.
6614  **/
6615 static void
6616 lpfc_free_active_sgl(struct lpfc_hba *phba)
6617 {
6618 	kfree(phba->sli4_hba.lpfc_sglq_active_list);
6619 }
6620 
6621 /**
6622  * lpfc_init_sgl_list - Allocate and initialize sgl list.
6623  * @phba: pointer to lpfc hba data structure.
6624  *
6625  * This routine is invoked to allocate and initizlize the driver's sgl
6626  * list and set up the sgl xritag tag array accordingly.
6627  *
6628  **/
6629 static void
6630 lpfc_init_sgl_list(struct lpfc_hba *phba)
6631 {
6632 	/* Initialize and populate the sglq list per host/VF. */
6633 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
6634 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
6635 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
6636 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
6637 
6638 	/* els xri-sgl book keeping */
6639 	phba->sli4_hba.els_xri_cnt = 0;
6640 
6641 	/* scsi xri-buffer book keeping */
6642 	phba->sli4_hba.scsi_xri_cnt = 0;
6643 
6644 	/* nvme xri-buffer book keeping */
6645 	phba->sli4_hba.nvme_xri_cnt = 0;
6646 }
6647 
6648 /**
6649  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6650  * @phba: pointer to lpfc hba data structure.
6651  *
6652  * This routine is invoked to post rpi header templates to the
6653  * port for those SLI4 ports that do not support extents.  This routine
6654  * posts a PAGE_SIZE memory region to the port to hold up to
6655  * PAGE_SIZE modulo 64 rpi context headers.  This is an initialization routine
6656  * and should be called only when interrupts are disabled.
6657  *
6658  * Return codes
6659  * 	0 - successful
6660  *	-ERROR - otherwise.
6661  **/
6662 int
6663 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6664 {
6665 	int rc = 0;
6666 	struct lpfc_rpi_hdr *rpi_hdr;
6667 
6668 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
6669 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6670 		return rc;
6671 	if (phba->sli4_hba.extents_in_use)
6672 		return -EIO;
6673 
6674 	rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6675 	if (!rpi_hdr) {
6676 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6677 				"0391 Error during rpi post operation\n");
6678 		lpfc_sli4_remove_rpis(phba);
6679 		rc = -ENODEV;
6680 	}
6681 
6682 	return rc;
6683 }
6684 
6685 /**
6686  * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6687  * @phba: pointer to lpfc hba data structure.
6688  *
6689  * This routine is invoked to allocate a single 4KB memory region to
6690  * support rpis and stores them in the phba.  This single region
6691  * provides support for up to 64 rpis.  The region is used globally
6692  * by the device.
6693  *
6694  * Returns:
6695  *   A valid rpi hdr on success.
6696  *   A NULL pointer on any failure.
6697  **/
6698 struct lpfc_rpi_hdr *
6699 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6700 {
6701 	uint16_t rpi_limit, curr_rpi_range;
6702 	struct lpfc_dmabuf *dmabuf;
6703 	struct lpfc_rpi_hdr *rpi_hdr;
6704 
6705 	/*
6706 	 * If the SLI4 port supports extents, posting the rpi header isn't
6707 	 * required.  Set the expected maximum count and let the actual value
6708 	 * get set when extents are fully allocated.
6709 	 */
6710 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6711 		return NULL;
6712 	if (phba->sli4_hba.extents_in_use)
6713 		return NULL;
6714 
6715 	/* The limit on the logical index is just the max_rpi count. */
6716 	rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
6717 
6718 	spin_lock_irq(&phba->hbalock);
6719 	/*
6720 	 * Establish the starting RPI in this header block.  The starting
6721 	 * rpi is normalized to a zero base because the physical rpi is
6722 	 * port based.
6723 	 */
6724 	curr_rpi_range = phba->sli4_hba.next_rpi;
6725 	spin_unlock_irq(&phba->hbalock);
6726 
6727 	/* Reached full RPI range */
6728 	if (curr_rpi_range == rpi_limit)
6729 		return NULL;
6730 
6731 	/*
6732 	 * First allocate the protocol header region for the port.  The
6733 	 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6734 	 */
6735 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6736 	if (!dmabuf)
6737 		return NULL;
6738 
6739 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6740 					   LPFC_HDR_TEMPLATE_SIZE,
6741 					   &dmabuf->phys, GFP_KERNEL);
6742 	if (!dmabuf->virt) {
6743 		rpi_hdr = NULL;
6744 		goto err_free_dmabuf;
6745 	}
6746 
6747 	if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6748 		rpi_hdr = NULL;
6749 		goto err_free_coherent;
6750 	}
6751 
6752 	/* Save the rpi header data for cleanup later. */
6753 	rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6754 	if (!rpi_hdr)
6755 		goto err_free_coherent;
6756 
6757 	rpi_hdr->dmabuf = dmabuf;
6758 	rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6759 	rpi_hdr->page_count = 1;
6760 	spin_lock_irq(&phba->hbalock);
6761 
6762 	/* The rpi_hdr stores the logical index only. */
6763 	rpi_hdr->start_rpi = curr_rpi_range;
6764 	rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
6765 	list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6766 
6767 	spin_unlock_irq(&phba->hbalock);
6768 	return rpi_hdr;
6769 
6770  err_free_coherent:
6771 	dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6772 			  dmabuf->virt, dmabuf->phys);
6773  err_free_dmabuf:
6774 	kfree(dmabuf);
6775 	return NULL;
6776 }
6777 
6778 /**
6779  * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6780  * @phba: pointer to lpfc hba data structure.
6781  *
6782  * This routine is invoked to remove all memory resources allocated
6783  * to support rpis for SLI4 ports not supporting extents. This routine
6784  * presumes the caller has released all rpis consumed by fabric or port
6785  * logins and is prepared to have the header pages removed.
6786  **/
6787 void
6788 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6789 {
6790 	struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6791 
6792 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6793 		goto exit;
6794 
6795 	list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6796 				 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6797 		list_del(&rpi_hdr->list);
6798 		dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6799 				  rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6800 		kfree(rpi_hdr->dmabuf);
6801 		kfree(rpi_hdr);
6802 	}
6803  exit:
6804 	/* There are no rpis available to the port now. */
6805 	phba->sli4_hba.next_rpi = 0;
6806 }
6807 
6808 /**
6809  * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6810  * @pdev: pointer to pci device data structure.
6811  *
6812  * This routine is invoked to allocate the driver hba data structure for an
6813  * HBA device. If the allocation is successful, the phba reference to the
6814  * PCI device data structure is set.
6815  *
6816  * Return codes
6817  *      pointer to @phba - successful
6818  *      NULL - error
6819  **/
6820 static struct lpfc_hba *
6821 lpfc_hba_alloc(struct pci_dev *pdev)
6822 {
6823 	struct lpfc_hba *phba;
6824 
6825 	/* Allocate memory for HBA structure */
6826 	phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6827 	if (!phba) {
6828 		dev_err(&pdev->dev, "failed to allocate hba struct\n");
6829 		return NULL;
6830 	}
6831 
6832 	/* Set reference to PCI device in HBA structure */
6833 	phba->pcidev = pdev;
6834 
6835 	/* Assign an unused board number */
6836 	phba->brd_no = lpfc_get_instance();
6837 	if (phba->brd_no < 0) {
6838 		kfree(phba);
6839 		return NULL;
6840 	}
6841 	phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
6842 
6843 	spin_lock_init(&phba->ct_ev_lock);
6844 	INIT_LIST_HEAD(&phba->ct_ev_waiters);
6845 
6846 	return phba;
6847 }
6848 
6849 /**
6850  * lpfc_hba_free - Free driver hba data structure with a device.
6851  * @phba: pointer to lpfc hba data structure.
6852  *
6853  * This routine is invoked to free the driver hba data structure with an
6854  * HBA device.
6855  **/
6856 static void
6857 lpfc_hba_free(struct lpfc_hba *phba)
6858 {
6859 	/* Release the driver assigned board number */
6860 	idr_remove(&lpfc_hba_index, phba->brd_no);
6861 
6862 	/* Free memory allocated with sli3 rings */
6863 	kfree(phba->sli.sli3_ring);
6864 	phba->sli.sli3_ring = NULL;
6865 
6866 	kfree(phba);
6867 	return;
6868 }
6869 
6870 /**
6871  * lpfc_create_shost - Create hba physical port with associated scsi host.
6872  * @phba: pointer to lpfc hba data structure.
6873  *
6874  * This routine is invoked to create HBA physical port and associate a SCSI
6875  * host with it.
6876  *
6877  * Return codes
6878  *      0 - successful
6879  *      other values - error
6880  **/
6881 static int
6882 lpfc_create_shost(struct lpfc_hba *phba)
6883 {
6884 	struct lpfc_vport *vport;
6885 	struct Scsi_Host  *shost;
6886 
6887 	/* Initialize HBA FC structure */
6888 	phba->fc_edtov = FF_DEF_EDTOV;
6889 	phba->fc_ratov = FF_DEF_RATOV;
6890 	phba->fc_altov = FF_DEF_ALTOV;
6891 	phba->fc_arbtov = FF_DEF_ARBTOV;
6892 
6893 	atomic_set(&phba->sdev_cnt, 0);
6894 	atomic_set(&phba->fc4ScsiInputRequests, 0);
6895 	atomic_set(&phba->fc4ScsiOutputRequests, 0);
6896 	atomic_set(&phba->fc4ScsiControlRequests, 0);
6897 	atomic_set(&phba->fc4ScsiIoCmpls, 0);
6898 	atomic_set(&phba->fc4NvmeInputRequests, 0);
6899 	atomic_set(&phba->fc4NvmeOutputRequests, 0);
6900 	atomic_set(&phba->fc4NvmeControlRequests, 0);
6901 	atomic_set(&phba->fc4NvmeIoCmpls, 0);
6902 	atomic_set(&phba->fc4NvmeLsRequests, 0);
6903 	atomic_set(&phba->fc4NvmeLsCmpls, 0);
6904 	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
6905 	if (!vport)
6906 		return -ENODEV;
6907 
6908 	shost = lpfc_shost_from_vport(vport);
6909 	phba->pport = vport;
6910 
6911 	if (phba->nvmet_support) {
6912 		/* Only 1 vport (pport) will support NVME target */
6913 		if (phba->txrdy_payload_pool == NULL) {
6914 			phba->txrdy_payload_pool = dma_pool_create(
6915 				"txrdy_pool", &phba->pcidev->dev,
6916 				TXRDY_PAYLOAD_LEN, 16, 0);
6917 			if (phba->txrdy_payload_pool) {
6918 				phba->targetport = NULL;
6919 				phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6920 				lpfc_printf_log(phba, KERN_INFO,
6921 						LOG_INIT | LOG_NVME_DISC,
6922 						"6076 NVME Target Found\n");
6923 			}
6924 		}
6925 	}
6926 
6927 	lpfc_debugfs_initialize(vport);
6928 	/* Put reference to SCSI host to driver's device private data */
6929 	pci_set_drvdata(phba->pcidev, shost);
6930 
6931 	/*
6932 	 * At this point we are fully registered with PSA. In addition,
6933 	 * any initial discovery should be completed.
6934 	 */
6935 	vport->load_flag |= FC_ALLOW_FDMI;
6936 	if (phba->cfg_enable_SmartSAN ||
6937 	    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
6938 
6939 		/* Setup appropriate attribute masks */
6940 		vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
6941 		if (phba->cfg_enable_SmartSAN)
6942 			vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6943 		else
6944 			vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6945 	}
6946 	return 0;
6947 }
6948 
6949 /**
6950  * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6951  * @phba: pointer to lpfc hba data structure.
6952  *
6953  * This routine is invoked to destroy HBA physical port and the associated
6954  * SCSI host.
6955  **/
6956 static void
6957 lpfc_destroy_shost(struct lpfc_hba *phba)
6958 {
6959 	struct lpfc_vport *vport = phba->pport;
6960 
6961 	/* Destroy physical port that associated with the SCSI host */
6962 	destroy_port(vport);
6963 
6964 	return;
6965 }
6966 
6967 /**
6968  * lpfc_setup_bg - Setup Block guard structures and debug areas.
6969  * @phba: pointer to lpfc hba data structure.
6970  * @shost: the shost to be used to detect Block guard settings.
6971  *
6972  * This routine sets up the local Block guard protocol settings for @shost.
6973  * This routine also allocates memory for debugging bg buffers.
6974  **/
6975 static void
6976 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6977 {
6978 	uint32_t old_mask;
6979 	uint32_t old_guard;
6980 
6981 	int pagecnt = 10;
6982 	if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6983 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6984 				"1478 Registering BlockGuard with the "
6985 				"SCSI layer\n");
6986 
6987 		old_mask = phba->cfg_prot_mask;
6988 		old_guard = phba->cfg_prot_guard;
6989 
6990 		/* Only allow supported values */
6991 		phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6992 			SHOST_DIX_TYPE0_PROTECTION |
6993 			SHOST_DIX_TYPE1_PROTECTION);
6994 		phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6995 					 SHOST_DIX_GUARD_CRC);
6996 
6997 		/* DIF Type 1 protection for profiles AST1/C1 is end to end */
6998 		if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6999 			phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
7000 
7001 		if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
7002 			if ((old_mask != phba->cfg_prot_mask) ||
7003 				(old_guard != phba->cfg_prot_guard))
7004 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7005 					"1475 Registering BlockGuard with the "
7006 					"SCSI layer: mask %d  guard %d\n",
7007 					phba->cfg_prot_mask,
7008 					phba->cfg_prot_guard);
7009 
7010 			scsi_host_set_prot(shost, phba->cfg_prot_mask);
7011 			scsi_host_set_guard(shost, phba->cfg_prot_guard);
7012 		} else
7013 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7014 				"1479 Not Registering BlockGuard with the SCSI "
7015 				"layer, Bad protection parameters: %d %d\n",
7016 				old_mask, old_guard);
7017 	}
7018 
7019 	if (!_dump_buf_data) {
7020 		while (pagecnt) {
7021 			spin_lock_init(&_dump_buf_lock);
7022 			_dump_buf_data =
7023 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
7024 			if (_dump_buf_data) {
7025 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7026 					"9043 BLKGRD: allocated %d pages for "
7027 				       "_dump_buf_data at 0x%p\n",
7028 				       (1 << pagecnt), _dump_buf_data);
7029 				_dump_buf_data_order = pagecnt;
7030 				memset(_dump_buf_data, 0,
7031 				       ((1 << PAGE_SHIFT) << pagecnt));
7032 				break;
7033 			} else
7034 				--pagecnt;
7035 		}
7036 		if (!_dump_buf_data_order)
7037 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7038 				"9044 BLKGRD: ERROR unable to allocate "
7039 			       "memory for hexdump\n");
7040 	} else
7041 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7042 			"9045 BLKGRD: already allocated _dump_buf_data=0x%p"
7043 		       "\n", _dump_buf_data);
7044 	if (!_dump_buf_dif) {
7045 		while (pagecnt) {
7046 			_dump_buf_dif =
7047 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
7048 			if (_dump_buf_dif) {
7049 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7050 					"9046 BLKGRD: allocated %d pages for "
7051 				       "_dump_buf_dif at 0x%p\n",
7052 				       (1 << pagecnt), _dump_buf_dif);
7053 				_dump_buf_dif_order = pagecnt;
7054 				memset(_dump_buf_dif, 0,
7055 				       ((1 << PAGE_SHIFT) << pagecnt));
7056 				break;
7057 			} else
7058 				--pagecnt;
7059 		}
7060 		if (!_dump_buf_dif_order)
7061 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7062 			"9047 BLKGRD: ERROR unable to allocate "
7063 			       "memory for hexdump\n");
7064 	} else
7065 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7066 			"9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
7067 		       _dump_buf_dif);
7068 }
7069 
7070 /**
7071  * lpfc_post_init_setup - Perform necessary device post initialization setup.
7072  * @phba: pointer to lpfc hba data structure.
7073  *
7074  * This routine is invoked to perform all the necessary post initialization
7075  * setup for the device.
7076  **/
7077 static void
7078 lpfc_post_init_setup(struct lpfc_hba *phba)
7079 {
7080 	struct Scsi_Host  *shost;
7081 	struct lpfc_adapter_event_header adapter_event;
7082 
7083 	/* Get the default values for Model Name and Description */
7084 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7085 
7086 	/*
7087 	 * hba setup may have changed the hba_queue_depth so we need to
7088 	 * adjust the value of can_queue.
7089 	 */
7090 	shost = pci_get_drvdata(phba->pcidev);
7091 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
7092 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7093 		lpfc_setup_bg(phba, shost);
7094 
7095 	lpfc_host_attrib_init(shost);
7096 
7097 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7098 		spin_lock_irq(shost->host_lock);
7099 		lpfc_poll_start_timer(phba);
7100 		spin_unlock_irq(shost->host_lock);
7101 	}
7102 
7103 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7104 			"0428 Perform SCSI scan\n");
7105 	/* Send board arrival event to upper layer */
7106 	adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7107 	adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7108 	fc_host_post_vendor_event(shost, fc_get_event_number(),
7109 				  sizeof(adapter_event),
7110 				  (char *) &adapter_event,
7111 				  LPFC_NL_VENDOR_ID);
7112 	return;
7113 }
7114 
7115 /**
7116  * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7117  * @phba: pointer to lpfc hba data structure.
7118  *
7119  * This routine is invoked to set up the PCI device memory space for device
7120  * with SLI-3 interface spec.
7121  *
7122  * Return codes
7123  * 	0 - successful
7124  * 	other values - error
7125  **/
7126 static int
7127 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7128 {
7129 	struct pci_dev *pdev;
7130 	unsigned long bar0map_len, bar2map_len;
7131 	int i, hbq_count;
7132 	void *ptr;
7133 	int error = -ENODEV;
7134 
7135 	/* Obtain PCI device reference */
7136 	if (!phba->pcidev)
7137 		return error;
7138 	else
7139 		pdev = phba->pcidev;
7140 
7141 	/* Set the device DMA mask size */
7142 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7143 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7144 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7145 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
7146 			return error;
7147 		}
7148 	}
7149 
7150 	/* Get the bus address of Bar0 and Bar2 and the number of bytes
7151 	 * required by each mapping.
7152 	 */
7153 	phba->pci_bar0_map = pci_resource_start(pdev, 0);
7154 	bar0map_len = pci_resource_len(pdev, 0);
7155 
7156 	phba->pci_bar2_map = pci_resource_start(pdev, 2);
7157 	bar2map_len = pci_resource_len(pdev, 2);
7158 
7159 	/* Map HBA SLIM to a kernel virtual address. */
7160 	phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7161 	if (!phba->slim_memmap_p) {
7162 		dev_printk(KERN_ERR, &pdev->dev,
7163 			   "ioremap failed for SLIM memory.\n");
7164 		goto out;
7165 	}
7166 
7167 	/* Map HBA Control Registers to a kernel virtual address. */
7168 	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7169 	if (!phba->ctrl_regs_memmap_p) {
7170 		dev_printk(KERN_ERR, &pdev->dev,
7171 			   "ioremap failed for HBA control registers.\n");
7172 		goto out_iounmap_slim;
7173 	}
7174 
7175 	/* Allocate memory for SLI-2 structures */
7176 	phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7177 						&phba->slim2p.phys, GFP_KERNEL);
7178 	if (!phba->slim2p.virt)
7179 		goto out_iounmap;
7180 
7181 	phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
7182 	phba->mbox_ext = (phba->slim2p.virt +
7183 		offsetof(struct lpfc_sli2_slim, mbx_ext_words));
7184 	phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7185 	phba->IOCBs = (phba->slim2p.virt +
7186 		       offsetof(struct lpfc_sli2_slim, IOCBs));
7187 
7188 	phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7189 						 lpfc_sli_hbq_size(),
7190 						 &phba->hbqslimp.phys,
7191 						 GFP_KERNEL);
7192 	if (!phba->hbqslimp.virt)
7193 		goto out_free_slim;
7194 
7195 	hbq_count = lpfc_sli_hbq_count();
7196 	ptr = phba->hbqslimp.virt;
7197 	for (i = 0; i < hbq_count; ++i) {
7198 		phba->hbqs[i].hbq_virt = ptr;
7199 		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7200 		ptr += (lpfc_hbq_defs[i]->entry_count *
7201 			sizeof(struct lpfc_hbq_entry));
7202 	}
7203 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7204 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7205 
7206 	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7207 
7208 	phba->MBslimaddr = phba->slim_memmap_p;
7209 	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7210 	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7211 	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7212 	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
7213 
7214 	return 0;
7215 
7216 out_free_slim:
7217 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7218 			  phba->slim2p.virt, phba->slim2p.phys);
7219 out_iounmap:
7220 	iounmap(phba->ctrl_regs_memmap_p);
7221 out_iounmap_slim:
7222 	iounmap(phba->slim_memmap_p);
7223 out:
7224 	return error;
7225 }
7226 
7227 /**
7228  * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7229  * @phba: pointer to lpfc hba data structure.
7230  *
7231  * This routine is invoked to unset the PCI device memory space for device
7232  * with SLI-3 interface spec.
7233  **/
7234 static void
7235 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7236 {
7237 	struct pci_dev *pdev;
7238 
7239 	/* Obtain PCI device reference */
7240 	if (!phba->pcidev)
7241 		return;
7242 	else
7243 		pdev = phba->pcidev;
7244 
7245 	/* Free coherent DMA memory allocated */
7246 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7247 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
7248 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7249 			  phba->slim2p.virt, phba->slim2p.phys);
7250 
7251 	/* I/O memory unmap */
7252 	iounmap(phba->ctrl_regs_memmap_p);
7253 	iounmap(phba->slim_memmap_p);
7254 
7255 	return;
7256 }
7257 
7258 /**
7259  * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7260  * @phba: pointer to lpfc hba data structure.
7261  *
7262  * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7263  * done and check status.
7264  *
7265  * Return 0 if successful, otherwise -ENODEV.
7266  **/
7267 int
7268 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7269 {
7270 	struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7271 	struct lpfc_register reg_data;
7272 	int i, port_error = 0;
7273 	uint32_t if_type;
7274 
7275 	memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7276 	memset(&reg_data, 0, sizeof(reg_data));
7277 	if (!phba->sli4_hba.PSMPHRregaddr)
7278 		return -ENODEV;
7279 
7280 	/* Wait up to 30 seconds for the SLI Port POST done and ready */
7281 	for (i = 0; i < 3000; i++) {
7282 		if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7283 			&portsmphr_reg.word0) ||
7284 			(bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
7285 			/* Port has a fatal POST error, break out */
7286 			port_error = -ENODEV;
7287 			break;
7288 		}
7289 		if (LPFC_POST_STAGE_PORT_READY ==
7290 		    bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
7291 			break;
7292 		msleep(10);
7293 	}
7294 
7295 	/*
7296 	 * If there was a port error during POST, then don't proceed with
7297 	 * other register reads as the data may not be valid.  Just exit.
7298 	 */
7299 	if (port_error) {
7300 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7301 			"1408 Port Failed POST - portsmphr=0x%x, "
7302 			"perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7303 			"scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7304 			portsmphr_reg.word0,
7305 			bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7306 			bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7307 			bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7308 			bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7309 			bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7310 			bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7311 			bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7312 			bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7313 	} else {
7314 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7315 				"2534 Device Info: SLIFamily=0x%x, "
7316 				"SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7317 				"SLIHint_2=0x%x, FT=0x%x\n",
7318 				bf_get(lpfc_sli_intf_sli_family,
7319 				       &phba->sli4_hba.sli_intf),
7320 				bf_get(lpfc_sli_intf_slirev,
7321 				       &phba->sli4_hba.sli_intf),
7322 				bf_get(lpfc_sli_intf_if_type,
7323 				       &phba->sli4_hba.sli_intf),
7324 				bf_get(lpfc_sli_intf_sli_hint1,
7325 				       &phba->sli4_hba.sli_intf),
7326 				bf_get(lpfc_sli_intf_sli_hint2,
7327 				       &phba->sli4_hba.sli_intf),
7328 				bf_get(lpfc_sli_intf_func_type,
7329 				       &phba->sli4_hba.sli_intf));
7330 		/*
7331 		 * Check for other Port errors during the initialization
7332 		 * process.  Fail the load if the port did not come up
7333 		 * correctly.
7334 		 */
7335 		if_type = bf_get(lpfc_sli_intf_if_type,
7336 				 &phba->sli4_hba.sli_intf);
7337 		switch (if_type) {
7338 		case LPFC_SLI_INTF_IF_TYPE_0:
7339 			phba->sli4_hba.ue_mask_lo =
7340 			      readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7341 			phba->sli4_hba.ue_mask_hi =
7342 			      readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7343 			uerrlo_reg.word0 =
7344 			      readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7345 			uerrhi_reg.word0 =
7346 				readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7347 			if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7348 			    (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7349 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7350 						"1422 Unrecoverable Error "
7351 						"Detected during POST "
7352 						"uerr_lo_reg=0x%x, "
7353 						"uerr_hi_reg=0x%x, "
7354 						"ue_mask_lo_reg=0x%x, "
7355 						"ue_mask_hi_reg=0x%x\n",
7356 						uerrlo_reg.word0,
7357 						uerrhi_reg.word0,
7358 						phba->sli4_hba.ue_mask_lo,
7359 						phba->sli4_hba.ue_mask_hi);
7360 				port_error = -ENODEV;
7361 			}
7362 			break;
7363 		case LPFC_SLI_INTF_IF_TYPE_2:
7364 		case LPFC_SLI_INTF_IF_TYPE_6:
7365 			/* Final checks.  The port status should be clean. */
7366 			if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7367 				&reg_data.word0) ||
7368 				(bf_get(lpfc_sliport_status_err, &reg_data) &&
7369 				 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
7370 				phba->work_status[0] =
7371 					readl(phba->sli4_hba.u.if_type2.
7372 					      ERR1regaddr);
7373 				phba->work_status[1] =
7374 					readl(phba->sli4_hba.u.if_type2.
7375 					      ERR2regaddr);
7376 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7377 					"2888 Unrecoverable port error "
7378 					"following POST: port status reg "
7379 					"0x%x, port_smphr reg 0x%x, "
7380 					"error 1=0x%x, error 2=0x%x\n",
7381 					reg_data.word0,
7382 					portsmphr_reg.word0,
7383 					phba->work_status[0],
7384 					phba->work_status[1]);
7385 				port_error = -ENODEV;
7386 			}
7387 			break;
7388 		case LPFC_SLI_INTF_IF_TYPE_1:
7389 		default:
7390 			break;
7391 		}
7392 	}
7393 	return port_error;
7394 }
7395 
7396 /**
7397  * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7398  * @phba: pointer to lpfc hba data structure.
7399  * @if_type:  The SLI4 interface type getting configured.
7400  *
7401  * This routine is invoked to set up SLI4 BAR0 PCI config space register
7402  * memory map.
7403  **/
7404 static void
7405 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
7406 {
7407 	switch (if_type) {
7408 	case LPFC_SLI_INTF_IF_TYPE_0:
7409 		phba->sli4_hba.u.if_type0.UERRLOregaddr =
7410 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7411 		phba->sli4_hba.u.if_type0.UERRHIregaddr =
7412 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7413 		phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7414 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7415 		phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7416 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7417 		phba->sli4_hba.SLIINTFregaddr =
7418 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7419 		break;
7420 	case LPFC_SLI_INTF_IF_TYPE_2:
7421 		phba->sli4_hba.u.if_type2.EQDregaddr =
7422 			phba->sli4_hba.conf_regs_memmap_p +
7423 						LPFC_CTL_PORT_EQ_DELAY_OFFSET;
7424 		phba->sli4_hba.u.if_type2.ERR1regaddr =
7425 			phba->sli4_hba.conf_regs_memmap_p +
7426 						LPFC_CTL_PORT_ER1_OFFSET;
7427 		phba->sli4_hba.u.if_type2.ERR2regaddr =
7428 			phba->sli4_hba.conf_regs_memmap_p +
7429 						LPFC_CTL_PORT_ER2_OFFSET;
7430 		phba->sli4_hba.u.if_type2.CTRLregaddr =
7431 			phba->sli4_hba.conf_regs_memmap_p +
7432 						LPFC_CTL_PORT_CTL_OFFSET;
7433 		phba->sli4_hba.u.if_type2.STATUSregaddr =
7434 			phba->sli4_hba.conf_regs_memmap_p +
7435 						LPFC_CTL_PORT_STA_OFFSET;
7436 		phba->sli4_hba.SLIINTFregaddr =
7437 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7438 		phba->sli4_hba.PSMPHRregaddr =
7439 			phba->sli4_hba.conf_regs_memmap_p +
7440 						LPFC_CTL_PORT_SEM_OFFSET;
7441 		phba->sli4_hba.RQDBregaddr =
7442 			phba->sli4_hba.conf_regs_memmap_p +
7443 						LPFC_ULP0_RQ_DOORBELL;
7444 		phba->sli4_hba.WQDBregaddr =
7445 			phba->sli4_hba.conf_regs_memmap_p +
7446 						LPFC_ULP0_WQ_DOORBELL;
7447 		phba->sli4_hba.CQDBregaddr =
7448 			phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7449 		phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
7450 		phba->sli4_hba.MQDBregaddr =
7451 			phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7452 		phba->sli4_hba.BMBXregaddr =
7453 			phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7454 		break;
7455 	case LPFC_SLI_INTF_IF_TYPE_6:
7456 		phba->sli4_hba.u.if_type2.EQDregaddr =
7457 			phba->sli4_hba.conf_regs_memmap_p +
7458 						LPFC_CTL_PORT_EQ_DELAY_OFFSET;
7459 		phba->sli4_hba.u.if_type2.ERR1regaddr =
7460 			phba->sli4_hba.conf_regs_memmap_p +
7461 						LPFC_CTL_PORT_ER1_OFFSET;
7462 		phba->sli4_hba.u.if_type2.ERR2regaddr =
7463 			phba->sli4_hba.conf_regs_memmap_p +
7464 						LPFC_CTL_PORT_ER2_OFFSET;
7465 		phba->sli4_hba.u.if_type2.CTRLregaddr =
7466 			phba->sli4_hba.conf_regs_memmap_p +
7467 						LPFC_CTL_PORT_CTL_OFFSET;
7468 		phba->sli4_hba.u.if_type2.STATUSregaddr =
7469 			phba->sli4_hba.conf_regs_memmap_p +
7470 						LPFC_CTL_PORT_STA_OFFSET;
7471 		phba->sli4_hba.PSMPHRregaddr =
7472 			phba->sli4_hba.conf_regs_memmap_p +
7473 						LPFC_CTL_PORT_SEM_OFFSET;
7474 		phba->sli4_hba.BMBXregaddr =
7475 			phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7476 		break;
7477 	case LPFC_SLI_INTF_IF_TYPE_1:
7478 	default:
7479 		dev_printk(KERN_ERR, &phba->pcidev->dev,
7480 			   "FATAL - unsupported SLI4 interface type - %d\n",
7481 			   if_type);
7482 		break;
7483 	}
7484 }
7485 
7486 /**
7487  * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7488  * @phba: pointer to lpfc hba data structure.
7489  *
7490  * This routine is invoked to set up SLI4 BAR1 register memory map.
7491  **/
7492 static void
7493 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
7494 {
7495 	switch (if_type) {
7496 	case LPFC_SLI_INTF_IF_TYPE_0:
7497 		phba->sli4_hba.PSMPHRregaddr =
7498 			phba->sli4_hba.ctrl_regs_memmap_p +
7499 			LPFC_SLIPORT_IF0_SMPHR;
7500 		phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7501 			LPFC_HST_ISR0;
7502 		phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7503 			LPFC_HST_IMR0;
7504 		phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7505 			LPFC_HST_ISCR0;
7506 		break;
7507 	case LPFC_SLI_INTF_IF_TYPE_6:
7508 		phba->sli4_hba.RQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7509 			LPFC_IF6_RQ_DOORBELL;
7510 		phba->sli4_hba.WQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7511 			LPFC_IF6_WQ_DOORBELL;
7512 		phba->sli4_hba.CQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7513 			LPFC_IF6_CQ_DOORBELL;
7514 		phba->sli4_hba.EQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7515 			LPFC_IF6_EQ_DOORBELL;
7516 		phba->sli4_hba.MQDBregaddr = phba->sli4_hba.drbl_regs_memmap_p +
7517 			LPFC_IF6_MQ_DOORBELL;
7518 		break;
7519 	case LPFC_SLI_INTF_IF_TYPE_2:
7520 	case LPFC_SLI_INTF_IF_TYPE_1:
7521 	default:
7522 		dev_err(&phba->pcidev->dev,
7523 			   "FATAL - unsupported SLI4 interface type - %d\n",
7524 			   if_type);
7525 		break;
7526 	}
7527 }
7528 
7529 /**
7530  * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7531  * @phba: pointer to lpfc hba data structure.
7532  * @vf: virtual function number
7533  *
7534  * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7535  * based on the given viftual function number, @vf.
7536  *
7537  * Return 0 if successful, otherwise -ENODEV.
7538  **/
7539 static int
7540 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7541 {
7542 	if (vf > LPFC_VIR_FUNC_MAX)
7543 		return -ENODEV;
7544 
7545 	phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7546 				vf * LPFC_VFR_PAGE_SIZE +
7547 					LPFC_ULP0_RQ_DOORBELL);
7548 	phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7549 				vf * LPFC_VFR_PAGE_SIZE +
7550 					LPFC_ULP0_WQ_DOORBELL);
7551 	phba->sli4_hba.CQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7552 				vf * LPFC_VFR_PAGE_SIZE +
7553 					LPFC_EQCQ_DOORBELL);
7554 	phba->sli4_hba.EQDBregaddr = phba->sli4_hba.CQDBregaddr;
7555 	phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7556 				vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7557 	phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7558 				vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7559 	return 0;
7560 }
7561 
7562 /**
7563  * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7564  * @phba: pointer to lpfc hba data structure.
7565  *
7566  * This routine is invoked to create the bootstrap mailbox
7567  * region consistent with the SLI-4 interface spec.  This
7568  * routine allocates all memory necessary to communicate
7569  * mailbox commands to the port and sets up all alignment
7570  * needs.  No locks are expected to be held when calling
7571  * this routine.
7572  *
7573  * Return codes
7574  * 	0 - successful
7575  * 	-ENOMEM - could not allocated memory.
7576  **/
7577 static int
7578 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7579 {
7580 	uint32_t bmbx_size;
7581 	struct lpfc_dmabuf *dmabuf;
7582 	struct dma_address *dma_address;
7583 	uint32_t pa_addr;
7584 	uint64_t phys_addr;
7585 
7586 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7587 	if (!dmabuf)
7588 		return -ENOMEM;
7589 
7590 	/*
7591 	 * The bootstrap mailbox region is comprised of 2 parts
7592 	 * plus an alignment restriction of 16 bytes.
7593 	 */
7594 	bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
7595 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7596 					   &dmabuf->phys, GFP_KERNEL);
7597 	if (!dmabuf->virt) {
7598 		kfree(dmabuf);
7599 		return -ENOMEM;
7600 	}
7601 
7602 	/*
7603 	 * Initialize the bootstrap mailbox pointers now so that the register
7604 	 * operations are simple later.  The mailbox dma address is required
7605 	 * to be 16-byte aligned.  Also align the virtual memory as each
7606 	 * maibox is copied into the bmbx mailbox region before issuing the
7607 	 * command to the port.
7608 	 */
7609 	phba->sli4_hba.bmbx.dmabuf = dmabuf;
7610 	phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7611 
7612 	phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7613 					      LPFC_ALIGN_16_BYTE);
7614 	phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7615 					      LPFC_ALIGN_16_BYTE);
7616 
7617 	/*
7618 	 * Set the high and low physical addresses now.  The SLI4 alignment
7619 	 * requirement is 16 bytes and the mailbox is posted to the port
7620 	 * as two 30-bit addresses.  The other data is a bit marking whether
7621 	 * the 30-bit address is the high or low address.
7622 	 * Upcast bmbx aphys to 64bits so shift instruction compiles
7623 	 * clean on 32 bit machines.
7624 	 */
7625 	dma_address = &phba->sli4_hba.bmbx.dma_address;
7626 	phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7627 	pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7628 	dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7629 					   LPFC_BMBX_BIT1_ADDR_HI);
7630 
7631 	pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7632 	dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7633 					   LPFC_BMBX_BIT1_ADDR_LO);
7634 	return 0;
7635 }
7636 
7637 /**
7638  * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7639  * @phba: pointer to lpfc hba data structure.
7640  *
7641  * This routine is invoked to teardown the bootstrap mailbox
7642  * region and release all host resources. This routine requires
7643  * the caller to ensure all mailbox commands recovered, no
7644  * additional mailbox comands are sent, and interrupts are disabled
7645  * before calling this routine.
7646  *
7647  **/
7648 static void
7649 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7650 {
7651 	dma_free_coherent(&phba->pcidev->dev,
7652 			  phba->sli4_hba.bmbx.bmbx_size,
7653 			  phba->sli4_hba.bmbx.dmabuf->virt,
7654 			  phba->sli4_hba.bmbx.dmabuf->phys);
7655 
7656 	kfree(phba->sli4_hba.bmbx.dmabuf);
7657 	memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7658 }
7659 
7660 /**
7661  * lpfc_sli4_read_config - Get the config parameters.
7662  * @phba: pointer to lpfc hba data structure.
7663  *
7664  * This routine is invoked to read the configuration parameters from the HBA.
7665  * The configuration parameters are used to set the base and maximum values
7666  * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7667  * allocation for the port.
7668  *
7669  * Return codes
7670  * 	0 - successful
7671  * 	-ENOMEM - No available memory
7672  *      -EIO - The mailbox failed to complete successfully.
7673  **/
7674 int
7675 lpfc_sli4_read_config(struct lpfc_hba *phba)
7676 {
7677 	LPFC_MBOXQ_t *pmb;
7678 	struct lpfc_mbx_read_config *rd_config;
7679 	union  lpfc_sli4_cfg_shdr *shdr;
7680 	uint32_t shdr_status, shdr_add_status;
7681 	struct lpfc_mbx_get_func_cfg *get_func_cfg;
7682 	struct lpfc_rsrc_desc_fcfcoe *desc;
7683 	char *pdesc_0;
7684 	uint16_t forced_link_speed;
7685 	uint32_t if_type;
7686 	int length, i, rc = 0, rc2;
7687 
7688 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7689 	if (!pmb) {
7690 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7691 				"2011 Unable to allocate memory for issuing "
7692 				"SLI_CONFIG_SPECIAL mailbox command\n");
7693 		return -ENOMEM;
7694 	}
7695 
7696 	lpfc_read_config(phba, pmb);
7697 
7698 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7699 	if (rc != MBX_SUCCESS) {
7700 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7701 			"2012 Mailbox failed , mbxCmd x%x "
7702 			"READ_CONFIG, mbxStatus x%x\n",
7703 			bf_get(lpfc_mqe_command, &pmb->u.mqe),
7704 			bf_get(lpfc_mqe_status, &pmb->u.mqe));
7705 		rc = -EIO;
7706 	} else {
7707 		rd_config = &pmb->u.mqe.un.rd_config;
7708 		if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7709 			phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7710 			phba->sli4_hba.lnk_info.lnk_tp =
7711 				bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7712 			phba->sli4_hba.lnk_info.lnk_no =
7713 				bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7714 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7715 					"3081 lnk_type:%d, lnk_numb:%d\n",
7716 					phba->sli4_hba.lnk_info.lnk_tp,
7717 					phba->sli4_hba.lnk_info.lnk_no);
7718 		} else
7719 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7720 					"3082 Mailbox (x%x) returned ldv:x0\n",
7721 					bf_get(lpfc_mqe_command, &pmb->u.mqe));
7722 		if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
7723 			phba->bbcredit_support = 1;
7724 			phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
7725 		}
7726 
7727 		phba->sli4_hba.extents_in_use =
7728 			bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
7729 		phba->sli4_hba.max_cfg_param.max_xri =
7730 			bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7731 		phba->sli4_hba.max_cfg_param.xri_base =
7732 			bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7733 		phba->sli4_hba.max_cfg_param.max_vpi =
7734 			bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7735 		phba->sli4_hba.max_cfg_param.vpi_base =
7736 			bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7737 		phba->sli4_hba.max_cfg_param.max_rpi =
7738 			bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7739 		phba->sli4_hba.max_cfg_param.rpi_base =
7740 			bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7741 		phba->sli4_hba.max_cfg_param.max_vfi =
7742 			bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7743 		phba->sli4_hba.max_cfg_param.vfi_base =
7744 			bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7745 		phba->sli4_hba.max_cfg_param.max_fcfi =
7746 			bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
7747 		phba->sli4_hba.max_cfg_param.max_eq =
7748 			bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7749 		phba->sli4_hba.max_cfg_param.max_rq =
7750 			bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7751 		phba->sli4_hba.max_cfg_param.max_wq =
7752 			bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7753 		phba->sli4_hba.max_cfg_param.max_cq =
7754 			bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7755 		phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7756 		phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7757 		phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7758 		phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
7759 		phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7760 				(phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
7761 		phba->max_vports = phba->max_vpi;
7762 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7763 				"2003 cfg params Extents? %d "
7764 				"XRI(B:%d M:%d), "
7765 				"VPI(B:%d M:%d) "
7766 				"VFI(B:%d M:%d) "
7767 				"RPI(B:%d M:%d) "
7768 				"FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
7769 				phba->sli4_hba.extents_in_use,
7770 				phba->sli4_hba.max_cfg_param.xri_base,
7771 				phba->sli4_hba.max_cfg_param.max_xri,
7772 				phba->sli4_hba.max_cfg_param.vpi_base,
7773 				phba->sli4_hba.max_cfg_param.max_vpi,
7774 				phba->sli4_hba.max_cfg_param.vfi_base,
7775 				phba->sli4_hba.max_cfg_param.max_vfi,
7776 				phba->sli4_hba.max_cfg_param.rpi_base,
7777 				phba->sli4_hba.max_cfg_param.max_rpi,
7778 				phba->sli4_hba.max_cfg_param.max_fcfi,
7779 				phba->sli4_hba.max_cfg_param.max_eq,
7780 				phba->sli4_hba.max_cfg_param.max_cq,
7781 				phba->sli4_hba.max_cfg_param.max_wq,
7782 				phba->sli4_hba.max_cfg_param.max_rq);
7783 
7784 	}
7785 
7786 	if (rc)
7787 		goto read_cfg_out;
7788 
7789 	/* Update link speed if forced link speed is supported */
7790 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7791 	if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
7792 		forced_link_speed =
7793 			bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7794 		if (forced_link_speed) {
7795 			phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7796 
7797 			switch (forced_link_speed) {
7798 			case LINK_SPEED_1G:
7799 				phba->cfg_link_speed =
7800 					LPFC_USER_LINK_SPEED_1G;
7801 				break;
7802 			case LINK_SPEED_2G:
7803 				phba->cfg_link_speed =
7804 					LPFC_USER_LINK_SPEED_2G;
7805 				break;
7806 			case LINK_SPEED_4G:
7807 				phba->cfg_link_speed =
7808 					LPFC_USER_LINK_SPEED_4G;
7809 				break;
7810 			case LINK_SPEED_8G:
7811 				phba->cfg_link_speed =
7812 					LPFC_USER_LINK_SPEED_8G;
7813 				break;
7814 			case LINK_SPEED_10G:
7815 				phba->cfg_link_speed =
7816 					LPFC_USER_LINK_SPEED_10G;
7817 				break;
7818 			case LINK_SPEED_16G:
7819 				phba->cfg_link_speed =
7820 					LPFC_USER_LINK_SPEED_16G;
7821 				break;
7822 			case LINK_SPEED_32G:
7823 				phba->cfg_link_speed =
7824 					LPFC_USER_LINK_SPEED_32G;
7825 				break;
7826 			case LINK_SPEED_64G:
7827 				phba->cfg_link_speed =
7828 					LPFC_USER_LINK_SPEED_64G;
7829 				break;
7830 			case 0xffff:
7831 				phba->cfg_link_speed =
7832 					LPFC_USER_LINK_SPEED_AUTO;
7833 				break;
7834 			default:
7835 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7836 						"0047 Unrecognized link "
7837 						"speed : %d\n",
7838 						forced_link_speed);
7839 				phba->cfg_link_speed =
7840 					LPFC_USER_LINK_SPEED_AUTO;
7841 			}
7842 		}
7843 	}
7844 
7845 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
7846 	length = phba->sli4_hba.max_cfg_param.max_xri -
7847 			lpfc_sli4_get_els_iocb_cnt(phba);
7848 	if (phba->cfg_hba_queue_depth > length) {
7849 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7850 				"3361 HBA queue depth changed from %d to %d\n",
7851 				phba->cfg_hba_queue_depth, length);
7852 		phba->cfg_hba_queue_depth = length;
7853 	}
7854 
7855 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7856 	    LPFC_SLI_INTF_IF_TYPE_2)
7857 		goto read_cfg_out;
7858 
7859 	/* get the pf# and vf# for SLI4 if_type 2 port */
7860 	length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7861 		  sizeof(struct lpfc_sli4_cfg_mhdr));
7862 	lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7863 			 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7864 			 length, LPFC_SLI4_MBX_EMBED);
7865 
7866 	rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7867 	shdr = (union lpfc_sli4_cfg_shdr *)
7868 				&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7869 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7870 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7871 	if (rc2 || shdr_status || shdr_add_status) {
7872 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7873 				"3026 Mailbox failed , mbxCmd x%x "
7874 				"GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7875 				bf_get(lpfc_mqe_command, &pmb->u.mqe),
7876 				bf_get(lpfc_mqe_status, &pmb->u.mqe));
7877 		goto read_cfg_out;
7878 	}
7879 
7880 	/* search for fc_fcoe resrouce descriptor */
7881 	get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
7882 
7883 	pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7884 	desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7885 	length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7886 	if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7887 		length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7888 	else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7889 		goto read_cfg_out;
7890 
7891 	for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
7892 		desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
7893 		if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
7894 		    bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
7895 			phba->sli4_hba.iov.pf_number =
7896 				bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7897 			phba->sli4_hba.iov.vf_number =
7898 				bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7899 			break;
7900 		}
7901 	}
7902 
7903 	if (i < LPFC_RSRC_DESC_MAX_NUM)
7904 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7905 				"3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7906 				"vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7907 				phba->sli4_hba.iov.vf_number);
7908 	else
7909 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7910 				"3028 GET_FUNCTION_CONFIG: failed to find "
7911 				"Resrouce Descriptor:x%x\n",
7912 				LPFC_RSRC_DESC_TYPE_FCFCOE);
7913 
7914 read_cfg_out:
7915 	mempool_free(pmb, phba->mbox_mem_pool);
7916 	return rc;
7917 }
7918 
7919 /**
7920  * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
7921  * @phba: pointer to lpfc hba data structure.
7922  *
7923  * This routine is invoked to setup the port-side endian order when
7924  * the port if_type is 0.  This routine has no function for other
7925  * if_types.
7926  *
7927  * Return codes
7928  * 	0 - successful
7929  * 	-ENOMEM - No available memory
7930  *      -EIO - The mailbox failed to complete successfully.
7931  **/
7932 static int
7933 lpfc_setup_endian_order(struct lpfc_hba *phba)
7934 {
7935 	LPFC_MBOXQ_t *mboxq;
7936 	uint32_t if_type, rc = 0;
7937 	uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7938 				      HOST_ENDIAN_HIGH_WORD1};
7939 
7940 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7941 	switch (if_type) {
7942 	case LPFC_SLI_INTF_IF_TYPE_0:
7943 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7944 						       GFP_KERNEL);
7945 		if (!mboxq) {
7946 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7947 					"0492 Unable to allocate memory for "
7948 					"issuing SLI_CONFIG_SPECIAL mailbox "
7949 					"command\n");
7950 			return -ENOMEM;
7951 		}
7952 
7953 		/*
7954 		 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7955 		 * two words to contain special data values and no other data.
7956 		 */
7957 		memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7958 		memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7959 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7960 		if (rc != MBX_SUCCESS) {
7961 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7962 					"0493 SLI_CONFIG_SPECIAL mailbox "
7963 					"failed with status x%x\n",
7964 					rc);
7965 			rc = -EIO;
7966 		}
7967 		mempool_free(mboxq, phba->mbox_mem_pool);
7968 		break;
7969 	case LPFC_SLI_INTF_IF_TYPE_6:
7970 	case LPFC_SLI_INTF_IF_TYPE_2:
7971 	case LPFC_SLI_INTF_IF_TYPE_1:
7972 	default:
7973 		break;
7974 	}
7975 	return rc;
7976 }
7977 
7978 /**
7979  * lpfc_sli4_queue_verify - Verify and update EQ counts
7980  * @phba: pointer to lpfc hba data structure.
7981  *
7982  * This routine is invoked to check the user settable queue counts for EQs.
7983  * After this routine is called the counts will be set to valid values that
7984  * adhere to the constraints of the system's interrupt vectors and the port's
7985  * queue resources.
7986  *
7987  * Return codes
7988  *      0 - successful
7989  *      -ENOMEM - No available memory
7990  **/
7991 static int
7992 lpfc_sli4_queue_verify(struct lpfc_hba *phba)
7993 {
7994 	int io_channel;
7995 	int fof_vectors = phba->cfg_fof ? 1 : 0;
7996 
7997 	/*
7998 	 * Sanity check for configured queue parameters against the run-time
7999 	 * device parameters
8000 	 */
8001 
8002 	/* Sanity check on HBA EQ parameters */
8003 	io_channel = phba->io_channel_irqs;
8004 
8005 	if (phba->sli4_hba.num_online_cpu < io_channel) {
8006 		lpfc_printf_log(phba,
8007 				KERN_ERR, LOG_INIT,
8008 				"3188 Reducing IO channels to match number of "
8009 				"online CPUs: from %d to %d\n",
8010 				io_channel, phba->sli4_hba.num_online_cpu);
8011 		io_channel = phba->sli4_hba.num_online_cpu;
8012 	}
8013 
8014 	if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
8015 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8016 				"2575 Reducing IO channels to match number of "
8017 				"available EQs: from %d to %d\n",
8018 				io_channel,
8019 				phba->sli4_hba.max_cfg_param.max_eq);
8020 		io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
8021 	}
8022 
8023 	/* The actual number of FCP / NVME event queues adopted */
8024 	if (io_channel != phba->io_channel_irqs)
8025 		phba->io_channel_irqs = io_channel;
8026 	if (phba->cfg_fcp_io_channel > io_channel)
8027 		phba->cfg_fcp_io_channel = io_channel;
8028 	if (phba->cfg_nvme_io_channel > io_channel)
8029 		phba->cfg_nvme_io_channel = io_channel;
8030 	if (phba->nvmet_support) {
8031 		if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
8032 			phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
8033 	}
8034 	if (phba->cfg_nvmet_mrq > LPFC_NVMET_MRQ_MAX)
8035 		phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_MAX;
8036 
8037 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8038 			"2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
8039 			phba->io_channel_irqs, phba->cfg_fcp_io_channel,
8040 			phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
8041 
8042 	/* Get EQ depth from module parameter, fake the default for now */
8043 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8044 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8045 
8046 	/* Get CQ depth from module parameter, fake the default for now */
8047 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8048 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
8049 	return 0;
8050 }
8051 
8052 static int
8053 lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
8054 {
8055 	struct lpfc_queue *qdesc;
8056 
8057 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8058 				      phba->sli4_hba.cq_esize,
8059 				      LPFC_CQE_EXP_COUNT);
8060 	if (!qdesc) {
8061 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8062 				"0508 Failed allocate fast-path NVME CQ (%d)\n",
8063 				wqidx);
8064 		return 1;
8065 	}
8066 	qdesc->qe_valid = 1;
8067 	phba->sli4_hba.nvme_cq[wqidx] = qdesc;
8068 
8069 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8070 				      LPFC_WQE128_SIZE, LPFC_WQE_EXP_COUNT);
8071 	if (!qdesc) {
8072 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8073 				"0509 Failed allocate fast-path NVME WQ (%d)\n",
8074 				wqidx);
8075 		return 1;
8076 	}
8077 	phba->sli4_hba.nvme_wq[wqidx] = qdesc;
8078 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8079 	return 0;
8080 }
8081 
8082 static int
8083 lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
8084 {
8085 	struct lpfc_queue *qdesc;
8086 	uint32_t wqesize;
8087 
8088 	/* Create Fast Path FCP CQs */
8089 	if (phba->enab_exp_wqcq_pages)
8090 		/* Increase the CQ size when WQEs contain an embedded cdb */
8091 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8092 					      phba->sli4_hba.cq_esize,
8093 					      LPFC_CQE_EXP_COUNT);
8094 
8095 	else
8096 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8097 					      phba->sli4_hba.cq_esize,
8098 					      phba->sli4_hba.cq_ecount);
8099 	if (!qdesc) {
8100 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8101 			"0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
8102 		return 1;
8103 	}
8104 	qdesc->qe_valid = 1;
8105 	phba->sli4_hba.fcp_cq[wqidx] = qdesc;
8106 
8107 	/* Create Fast Path FCP WQs */
8108 	if (phba->enab_exp_wqcq_pages) {
8109 		/* Increase the WQ size when WQEs contain an embedded cdb */
8110 		wqesize = (phba->fcp_embed_io) ?
8111 			LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
8112 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_EXPANDED_PAGE_SIZE,
8113 					      wqesize,
8114 					      LPFC_WQE_EXP_COUNT);
8115 	} else
8116 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8117 					      phba->sli4_hba.wq_esize,
8118 					      phba->sli4_hba.wq_ecount);
8119 
8120 	if (!qdesc) {
8121 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8122 				"0503 Failed allocate fast-path FCP WQ (%d)\n",
8123 				wqidx);
8124 		return 1;
8125 	}
8126 	phba->sli4_hba.fcp_wq[wqidx] = qdesc;
8127 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8128 	return 0;
8129 }
8130 
8131 /**
8132  * lpfc_sli4_queue_create - Create all the SLI4 queues
8133  * @phba: pointer to lpfc hba data structure.
8134  *
8135  * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8136  * operation. For each SLI4 queue type, the parameters such as queue entry
8137  * count (queue depth) shall be taken from the module parameter. For now,
8138  * we just use some constant number as place holder.
8139  *
8140  * Return codes
8141  *      0 - successful
8142  *      -ENOMEM - No availble memory
8143  *      -EIO - The mailbox failed to complete successfully.
8144  **/
8145 int
8146 lpfc_sli4_queue_create(struct lpfc_hba *phba)
8147 {
8148 	struct lpfc_queue *qdesc;
8149 	int idx, io_channel;
8150 
8151 	/*
8152 	 * Create HBA Record arrays.
8153 	 * Both NVME and FCP will share that same vectors / EQs
8154 	 */
8155 	io_channel = phba->io_channel_irqs;
8156 	if (!io_channel)
8157 		return -ERANGE;
8158 
8159 	phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8160 	phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8161 	phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8162 	phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8163 	phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8164 	phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
8165 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8166 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8167 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8168 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
8169 
8170 	phba->sli4_hba.hba_eq =  kcalloc(io_channel,
8171 					sizeof(struct lpfc_queue *),
8172 					GFP_KERNEL);
8173 	if (!phba->sli4_hba.hba_eq) {
8174 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8175 			"2576 Failed allocate memory for "
8176 			"fast-path EQ record array\n");
8177 		goto out_error;
8178 	}
8179 
8180 	if (phba->cfg_fcp_io_channel) {
8181 		phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
8182 						sizeof(struct lpfc_queue *),
8183 						GFP_KERNEL);
8184 		if (!phba->sli4_hba.fcp_cq) {
8185 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8186 					"2577 Failed allocate memory for "
8187 					"fast-path CQ record array\n");
8188 			goto out_error;
8189 		}
8190 		phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
8191 						sizeof(struct lpfc_queue *),
8192 						GFP_KERNEL);
8193 		if (!phba->sli4_hba.fcp_wq) {
8194 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8195 					"2578 Failed allocate memory for "
8196 					"fast-path FCP WQ record array\n");
8197 			goto out_error;
8198 		}
8199 		/*
8200 		 * Since the first EQ can have multiple CQs associated with it,
8201 		 * this array is used to quickly see if we have a FCP fast-path
8202 		 * CQ match.
8203 		 */
8204 		phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
8205 							sizeof(uint16_t),
8206 							GFP_KERNEL);
8207 		if (!phba->sli4_hba.fcp_cq_map) {
8208 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8209 					"2545 Failed allocate memory for "
8210 					"fast-path CQ map\n");
8211 			goto out_error;
8212 		}
8213 	}
8214 
8215 	if (phba->cfg_nvme_io_channel) {
8216 		phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
8217 						sizeof(struct lpfc_queue *),
8218 						GFP_KERNEL);
8219 		if (!phba->sli4_hba.nvme_cq) {
8220 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8221 					"6077 Failed allocate memory for "
8222 					"fast-path CQ record array\n");
8223 			goto out_error;
8224 		}
8225 
8226 		phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
8227 						sizeof(struct lpfc_queue *),
8228 						GFP_KERNEL);
8229 		if (!phba->sli4_hba.nvme_wq) {
8230 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8231 					"2581 Failed allocate memory for "
8232 					"fast-path NVME WQ record array\n");
8233 			goto out_error;
8234 		}
8235 
8236 		/*
8237 		 * Since the first EQ can have multiple CQs associated with it,
8238 		 * this array is used to quickly see if we have a NVME fast-path
8239 		 * CQ match.
8240 		 */
8241 		phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
8242 							sizeof(uint16_t),
8243 							GFP_KERNEL);
8244 		if (!phba->sli4_hba.nvme_cq_map) {
8245 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8246 					"6078 Failed allocate memory for "
8247 					"fast-path CQ map\n");
8248 			goto out_error;
8249 		}
8250 
8251 		if (phba->nvmet_support) {
8252 			phba->sli4_hba.nvmet_cqset = kcalloc(
8253 					phba->cfg_nvmet_mrq,
8254 					sizeof(struct lpfc_queue *),
8255 					GFP_KERNEL);
8256 			if (!phba->sli4_hba.nvmet_cqset) {
8257 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8258 					"3121 Fail allocate memory for "
8259 					"fast-path CQ set array\n");
8260 				goto out_error;
8261 			}
8262 			phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8263 					phba->cfg_nvmet_mrq,
8264 					sizeof(struct lpfc_queue *),
8265 					GFP_KERNEL);
8266 			if (!phba->sli4_hba.nvmet_mrq_hdr) {
8267 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8268 					"3122 Fail allocate memory for "
8269 					"fast-path RQ set hdr array\n");
8270 				goto out_error;
8271 			}
8272 			phba->sli4_hba.nvmet_mrq_data = kcalloc(
8273 					phba->cfg_nvmet_mrq,
8274 					sizeof(struct lpfc_queue *),
8275 					GFP_KERNEL);
8276 			if (!phba->sli4_hba.nvmet_mrq_data) {
8277 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8278 					"3124 Fail allocate memory for "
8279 					"fast-path RQ set data array\n");
8280 				goto out_error;
8281 			}
8282 		}
8283 	}
8284 
8285 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8286 
8287 	/* Create HBA Event Queues (EQs) */
8288 	for (idx = 0; idx < io_channel; idx++) {
8289 		/* Create EQs */
8290 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8291 					      phba->sli4_hba.eq_esize,
8292 					      phba->sli4_hba.eq_ecount);
8293 		if (!qdesc) {
8294 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8295 					"0497 Failed allocate EQ (%d)\n", idx);
8296 			goto out_error;
8297 		}
8298 		qdesc->qe_valid = 1;
8299 		phba->sli4_hba.hba_eq[idx] = qdesc;
8300 	}
8301 
8302 	/* FCP and NVME io channels are not required to be balanced */
8303 
8304 	for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8305 		if (lpfc_alloc_fcp_wq_cq(phba, idx))
8306 			goto out_error;
8307 
8308 	for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
8309 		if (lpfc_alloc_nvme_wq_cq(phba, idx))
8310 			goto out_error;
8311 
8312 	if (phba->nvmet_support) {
8313 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8314 			qdesc = lpfc_sli4_queue_alloc(phba,
8315 						      LPFC_DEFAULT_PAGE_SIZE,
8316 						      phba->sli4_hba.cq_esize,
8317 						      phba->sli4_hba.cq_ecount);
8318 			if (!qdesc) {
8319 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8320 					"3142 Failed allocate NVME "
8321 					"CQ Set (%d)\n", idx);
8322 				goto out_error;
8323 			}
8324 			qdesc->qe_valid = 1;
8325 			phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8326 		}
8327 	}
8328 
8329 	/*
8330 	 * Create Slow Path Completion Queues (CQs)
8331 	 */
8332 
8333 	/* Create slow-path Mailbox Command Complete Queue */
8334 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8335 				      phba->sli4_hba.cq_esize,
8336 				      phba->sli4_hba.cq_ecount);
8337 	if (!qdesc) {
8338 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8339 				"0500 Failed allocate slow-path mailbox CQ\n");
8340 		goto out_error;
8341 	}
8342 	qdesc->qe_valid = 1;
8343 	phba->sli4_hba.mbx_cq = qdesc;
8344 
8345 	/* Create slow-path ELS Complete Queue */
8346 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8347 				      phba->sli4_hba.cq_esize,
8348 				      phba->sli4_hba.cq_ecount);
8349 	if (!qdesc) {
8350 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8351 				"0501 Failed allocate slow-path ELS CQ\n");
8352 		goto out_error;
8353 	}
8354 	qdesc->qe_valid = 1;
8355 	phba->sli4_hba.els_cq = qdesc;
8356 
8357 
8358 	/*
8359 	 * Create Slow Path Work Queues (WQs)
8360 	 */
8361 
8362 	/* Create Mailbox Command Queue */
8363 
8364 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8365 				      phba->sli4_hba.mq_esize,
8366 				      phba->sli4_hba.mq_ecount);
8367 	if (!qdesc) {
8368 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8369 				"0505 Failed allocate slow-path MQ\n");
8370 		goto out_error;
8371 	}
8372 	phba->sli4_hba.mbx_wq = qdesc;
8373 
8374 	/*
8375 	 * Create ELS Work Queues
8376 	 */
8377 
8378 	/* Create slow-path ELS Work Queue */
8379 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8380 				      phba->sli4_hba.wq_esize,
8381 				      phba->sli4_hba.wq_ecount);
8382 	if (!qdesc) {
8383 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8384 				"0504 Failed allocate slow-path ELS WQ\n");
8385 		goto out_error;
8386 	}
8387 	phba->sli4_hba.els_wq = qdesc;
8388 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8389 
8390 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8391 		/* Create NVME LS Complete Queue */
8392 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8393 					      phba->sli4_hba.cq_esize,
8394 					      phba->sli4_hba.cq_ecount);
8395 		if (!qdesc) {
8396 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8397 					"6079 Failed allocate NVME LS CQ\n");
8398 			goto out_error;
8399 		}
8400 		qdesc->qe_valid = 1;
8401 		phba->sli4_hba.nvmels_cq = qdesc;
8402 
8403 		/* Create NVME LS Work Queue */
8404 		qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8405 					      phba->sli4_hba.wq_esize,
8406 					      phba->sli4_hba.wq_ecount);
8407 		if (!qdesc) {
8408 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8409 					"6080 Failed allocate NVME LS WQ\n");
8410 			goto out_error;
8411 		}
8412 		phba->sli4_hba.nvmels_wq = qdesc;
8413 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8414 	}
8415 
8416 	/*
8417 	 * Create Receive Queue (RQ)
8418 	 */
8419 
8420 	/* Create Receive Queue for header */
8421 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8422 				      phba->sli4_hba.rq_esize,
8423 				      phba->sli4_hba.rq_ecount);
8424 	if (!qdesc) {
8425 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8426 				"0506 Failed allocate receive HRQ\n");
8427 		goto out_error;
8428 	}
8429 	phba->sli4_hba.hdr_rq = qdesc;
8430 
8431 	/* Create Receive Queue for data */
8432 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
8433 				      phba->sli4_hba.rq_esize,
8434 				      phba->sli4_hba.rq_ecount);
8435 	if (!qdesc) {
8436 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8437 				"0507 Failed allocate receive DRQ\n");
8438 		goto out_error;
8439 	}
8440 	phba->sli4_hba.dat_rq = qdesc;
8441 
8442 	if (phba->nvmet_support) {
8443 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8444 			/* Create NVMET Receive Queue for header */
8445 			qdesc = lpfc_sli4_queue_alloc(phba,
8446 						      LPFC_DEFAULT_PAGE_SIZE,
8447 						      phba->sli4_hba.rq_esize,
8448 						      LPFC_NVMET_RQE_DEF_COUNT);
8449 			if (!qdesc) {
8450 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8451 						"3146 Failed allocate "
8452 						"receive HRQ\n");
8453 				goto out_error;
8454 			}
8455 			phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8456 
8457 			/* Only needed for header of RQ pair */
8458 			qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8459 					      GFP_KERNEL);
8460 			if (qdesc->rqbp == NULL) {
8461 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8462 						"6131 Failed allocate "
8463 						"Header RQBP\n");
8464 				goto out_error;
8465 			}
8466 
8467 			/* Put list in known state in case driver load fails. */
8468 			INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
8469 
8470 			/* Create NVMET Receive Queue for data */
8471 			qdesc = lpfc_sli4_queue_alloc(phba,
8472 						      LPFC_DEFAULT_PAGE_SIZE,
8473 						      phba->sli4_hba.rq_esize,
8474 						      LPFC_NVMET_RQE_DEF_COUNT);
8475 			if (!qdesc) {
8476 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8477 						"3156 Failed allocate "
8478 						"receive DRQ\n");
8479 				goto out_error;
8480 			}
8481 			phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8482 		}
8483 	}
8484 
8485 	/* Create the Queues needed for Flash Optimized Fabric operations */
8486 	if (phba->cfg_fof)
8487 		lpfc_fof_queue_create(phba);
8488 	return 0;
8489 
8490 out_error:
8491 	lpfc_sli4_queue_destroy(phba);
8492 	return -ENOMEM;
8493 }
8494 
8495 static inline void
8496 __lpfc_sli4_release_queue(struct lpfc_queue **qp)
8497 {
8498 	if (*qp != NULL) {
8499 		lpfc_sli4_queue_free(*qp);
8500 		*qp = NULL;
8501 	}
8502 }
8503 
8504 static inline void
8505 lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8506 {
8507 	int idx;
8508 
8509 	if (*qs == NULL)
8510 		return;
8511 
8512 	for (idx = 0; idx < max; idx++)
8513 		__lpfc_sli4_release_queue(&(*qs)[idx]);
8514 
8515 	kfree(*qs);
8516 	*qs = NULL;
8517 }
8518 
8519 static inline void
8520 lpfc_sli4_release_queue_map(uint16_t **qmap)
8521 {
8522 	if (*qmap != NULL) {
8523 		kfree(*qmap);
8524 		*qmap = NULL;
8525 	}
8526 }
8527 
8528 /**
8529  * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8530  * @phba: pointer to lpfc hba data structure.
8531  *
8532  * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8533  * operation.
8534  *
8535  * Return codes
8536  *      0 - successful
8537  *      -ENOMEM - No available memory
8538  *      -EIO - The mailbox failed to complete successfully.
8539  **/
8540 void
8541 lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8542 {
8543 	if (phba->cfg_fof)
8544 		lpfc_fof_queue_destroy(phba);
8545 
8546 	/* Release HBA eqs */
8547 	lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
8548 
8549 	/* Release FCP cqs */
8550 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
8551 				 phba->cfg_fcp_io_channel);
8552 
8553 	/* Release FCP wqs */
8554 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
8555 				 phba->cfg_fcp_io_channel);
8556 
8557 	/* Release FCP CQ mapping array */
8558 	lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8559 
8560 	/* Release NVME cqs */
8561 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8562 					phba->cfg_nvme_io_channel);
8563 
8564 	/* Release NVME wqs */
8565 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8566 					phba->cfg_nvme_io_channel);
8567 
8568 	/* Release NVME CQ mapping array */
8569 	lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
8570 
8571 	if (phba->nvmet_support) {
8572 		lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8573 					 phba->cfg_nvmet_mrq);
8574 
8575 		lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8576 					 phba->cfg_nvmet_mrq);
8577 		lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8578 					 phba->cfg_nvmet_mrq);
8579 	}
8580 
8581 	/* Release mailbox command work queue */
8582 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
8583 
8584 	/* Release ELS work queue */
8585 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8586 
8587 	/* Release ELS work queue */
8588 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
8589 
8590 	/* Release unsolicited receive queue */
8591 	__lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8592 	__lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
8593 
8594 	/* Release ELS complete queue */
8595 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8596 
8597 	/* Release NVME LS complete queue */
8598 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
8599 
8600 	/* Release mailbox command complete queue */
8601 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8602 
8603 	/* Everything on this list has been freed */
8604 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8605 }
8606 
8607 int
8608 lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8609 {
8610 	struct lpfc_rqb *rqbp;
8611 	struct lpfc_dmabuf *h_buf;
8612 	struct rqb_dmabuf *rqb_buffer;
8613 
8614 	rqbp = rq->rqbp;
8615 	while (!list_empty(&rqbp->rqb_buffer_list)) {
8616 		list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8617 				 struct lpfc_dmabuf, list);
8618 
8619 		rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8620 		(rqbp->rqb_free_buffer)(phba, rqb_buffer);
8621 		rqbp->buffer_count--;
8622 	}
8623 	return 1;
8624 }
8625 
8626 static int
8627 lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8628 	struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8629 	int qidx, uint32_t qtype)
8630 {
8631 	struct lpfc_sli_ring *pring;
8632 	int rc;
8633 
8634 	if (!eq || !cq || !wq) {
8635 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8636 			"6085 Fast-path %s (%d) not allocated\n",
8637 			((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8638 		return -ENOMEM;
8639 	}
8640 
8641 	/* create the Cq first */
8642 	rc = lpfc_cq_create(phba, cq, eq,
8643 			(qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8644 	if (rc) {
8645 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8646 			"6086 Failed setup of CQ (%d), rc = 0x%x\n",
8647 			qidx, (uint32_t)rc);
8648 		return rc;
8649 	}
8650 	cq->chann = qidx;
8651 
8652 	if (qtype != LPFC_MBOX) {
8653 		/* Setup nvme_cq_map for fast lookup */
8654 		if (cq_map)
8655 			*cq_map = cq->queue_id;
8656 
8657 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8658 			"6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8659 			qidx, cq->queue_id, qidx, eq->queue_id);
8660 
8661 		/* create the wq */
8662 		rc = lpfc_wq_create(phba, wq, cq, qtype);
8663 		if (rc) {
8664 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8665 				"6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8666 				qidx, (uint32_t)rc);
8667 			/* no need to tear down cq - caller will do so */
8668 			return rc;
8669 		}
8670 		wq->chann = qidx;
8671 
8672 		/* Bind this CQ/WQ to the NVME ring */
8673 		pring = wq->pring;
8674 		pring->sli.sli4.wqp = (void *)wq;
8675 		cq->pring = pring;
8676 
8677 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8678 			"2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8679 			qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8680 	} else {
8681 		rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8682 		if (rc) {
8683 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8684 				"0539 Failed setup of slow-path MQ: "
8685 				"rc = 0x%x\n", rc);
8686 			/* no need to tear down cq - caller will do so */
8687 			return rc;
8688 		}
8689 
8690 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8691 			"2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8692 			phba->sli4_hba.mbx_wq->queue_id,
8693 			phba->sli4_hba.mbx_cq->queue_id);
8694 	}
8695 
8696 	return 0;
8697 }
8698 
8699 /**
8700  * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8701  * @phba: pointer to lpfc hba data structure.
8702  *
8703  * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8704  * operation.
8705  *
8706  * Return codes
8707  *      0 - successful
8708  *      -ENOMEM - No available memory
8709  *      -EIO - The mailbox failed to complete successfully.
8710  **/
8711 int
8712 lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8713 {
8714 	uint32_t shdr_status, shdr_add_status;
8715 	union lpfc_sli4_cfg_shdr *shdr;
8716 	LPFC_MBOXQ_t *mboxq;
8717 	int qidx;
8718 	uint32_t length, io_channel;
8719 	int rc = -ENOMEM;
8720 
8721 	/* Check for dual-ULP support */
8722 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8723 	if (!mboxq) {
8724 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8725 				"3249 Unable to allocate memory for "
8726 				"QUERY_FW_CFG mailbox command\n");
8727 		return -ENOMEM;
8728 	}
8729 	length = (sizeof(struct lpfc_mbx_query_fw_config) -
8730 		  sizeof(struct lpfc_sli4_cfg_mhdr));
8731 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8732 			 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8733 			 length, LPFC_SLI4_MBX_EMBED);
8734 
8735 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8736 
8737 	shdr = (union lpfc_sli4_cfg_shdr *)
8738 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8739 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8740 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8741 	if (shdr_status || shdr_add_status || rc) {
8742 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8743 				"3250 QUERY_FW_CFG mailbox failed with status "
8744 				"x%x add_status x%x, mbx status x%x\n",
8745 				shdr_status, shdr_add_status, rc);
8746 		if (rc != MBX_TIMEOUT)
8747 			mempool_free(mboxq, phba->mbox_mem_pool);
8748 		rc = -ENXIO;
8749 		goto out_error;
8750 	}
8751 
8752 	phba->sli4_hba.fw_func_mode =
8753 			mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8754 	phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8755 	phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
8756 	phba->sli4_hba.physical_port =
8757 			mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
8758 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8759 			"3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8760 			"ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8761 			phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8762 
8763 	if (rc != MBX_TIMEOUT)
8764 		mempool_free(mboxq, phba->mbox_mem_pool);
8765 
8766 	/*
8767 	 * Set up HBA Event Queues (EQs)
8768 	 */
8769 	io_channel = phba->io_channel_irqs;
8770 
8771 	/* Set up HBA event queue */
8772 	if (io_channel && !phba->sli4_hba.hba_eq) {
8773 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8774 				"3147 Fast-path EQs not allocated\n");
8775 		rc = -ENOMEM;
8776 		goto out_error;
8777 	}
8778 	for (qidx = 0; qidx < io_channel; qidx++) {
8779 		if (!phba->sli4_hba.hba_eq[qidx]) {
8780 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8781 					"0522 Fast-path EQ (%d) not "
8782 					"allocated\n", qidx);
8783 			rc = -ENOMEM;
8784 			goto out_destroy;
8785 		}
8786 		rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8787 						phba->cfg_fcp_imax);
8788 		if (rc) {
8789 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8790 					"0523 Failed setup of fast-path EQ "
8791 					"(%d), rc = 0x%x\n", qidx,
8792 					(uint32_t)rc);
8793 			goto out_destroy;
8794 		}
8795 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8796 				"2584 HBA EQ setup: queue[%d]-id=%d\n",
8797 				qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
8798 	}
8799 
8800 	if (phba->cfg_nvme_io_channel) {
8801 		if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
8802 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8803 				"6084 Fast-path NVME %s array not allocated\n",
8804 				(phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
8805 			rc = -ENOMEM;
8806 			goto out_destroy;
8807 		}
8808 
8809 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8810 			rc = lpfc_create_wq_cq(phba,
8811 					phba->sli4_hba.hba_eq[
8812 						qidx % io_channel],
8813 					phba->sli4_hba.nvme_cq[qidx],
8814 					phba->sli4_hba.nvme_wq[qidx],
8815 					&phba->sli4_hba.nvme_cq_map[qidx],
8816 					qidx, LPFC_NVME);
8817 			if (rc) {
8818 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8819 					"6123 Failed to setup fastpath "
8820 					"NVME WQ/CQ (%d), rc = 0x%x\n",
8821 					qidx, (uint32_t)rc);
8822 				goto out_destroy;
8823 			}
8824 		}
8825 	}
8826 
8827 	if (phba->cfg_fcp_io_channel) {
8828 		/* Set up fast-path FCP Response Complete Queue */
8829 		if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
8830 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8831 				"3148 Fast-path FCP %s array not allocated\n",
8832 				phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
8833 			rc = -ENOMEM;
8834 			goto out_destroy;
8835 		}
8836 
8837 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8838 			rc = lpfc_create_wq_cq(phba,
8839 					phba->sli4_hba.hba_eq[
8840 						qidx % io_channel],
8841 					phba->sli4_hba.fcp_cq[qidx],
8842 					phba->sli4_hba.fcp_wq[qidx],
8843 					&phba->sli4_hba.fcp_cq_map[qidx],
8844 					qidx, LPFC_FCP);
8845 			if (rc) {
8846 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8847 					"0535 Failed to setup fastpath "
8848 					"FCP WQ/CQ (%d), rc = 0x%x\n",
8849 					qidx, (uint32_t)rc);
8850 				goto out_destroy;
8851 			}
8852 		}
8853 	}
8854 
8855 	/*
8856 	 * Set up Slow Path Complete Queues (CQs)
8857 	 */
8858 
8859 	/* Set up slow-path MBOX CQ/MQ */
8860 
8861 	if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
8862 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8863 				"0528 %s not allocated\n",
8864 				phba->sli4_hba.mbx_cq ?
8865 				"Mailbox WQ" : "Mailbox CQ");
8866 		rc = -ENOMEM;
8867 		goto out_destroy;
8868 	}
8869 
8870 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8871 			       phba->sli4_hba.mbx_cq,
8872 			       phba->sli4_hba.mbx_wq,
8873 			       NULL, 0, LPFC_MBOX);
8874 	if (rc) {
8875 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8876 			"0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8877 			(uint32_t)rc);
8878 		goto out_destroy;
8879 	}
8880 	if (phba->nvmet_support) {
8881 		if (!phba->sli4_hba.nvmet_cqset) {
8882 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8883 					"3165 Fast-path NVME CQ Set "
8884 					"array not allocated\n");
8885 			rc = -ENOMEM;
8886 			goto out_destroy;
8887 		}
8888 		if (phba->cfg_nvmet_mrq > 1) {
8889 			rc = lpfc_cq_create_set(phba,
8890 					phba->sli4_hba.nvmet_cqset,
8891 					phba->sli4_hba.hba_eq,
8892 					LPFC_WCQ, LPFC_NVMET);
8893 			if (rc) {
8894 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8895 						"3164 Failed setup of NVME CQ "
8896 						"Set, rc = 0x%x\n",
8897 						(uint32_t)rc);
8898 				goto out_destroy;
8899 			}
8900 		} else {
8901 			/* Set up NVMET Receive Complete Queue */
8902 			rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8903 					    phba->sli4_hba.hba_eq[0],
8904 					    LPFC_WCQ, LPFC_NVMET);
8905 			if (rc) {
8906 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8907 						"6089 Failed setup NVMET CQ: "
8908 						"rc = 0x%x\n", (uint32_t)rc);
8909 				goto out_destroy;
8910 			}
8911 			phba->sli4_hba.nvmet_cqset[0]->chann = 0;
8912 
8913 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8914 					"6090 NVMET CQ setup: cq-id=%d, "
8915 					"parent eq-id=%d\n",
8916 					phba->sli4_hba.nvmet_cqset[0]->queue_id,
8917 					phba->sli4_hba.hba_eq[0]->queue_id);
8918 		}
8919 	}
8920 
8921 	/* Set up slow-path ELS WQ/CQ */
8922 	if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
8923 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8924 				"0530 ELS %s not allocated\n",
8925 				phba->sli4_hba.els_cq ? "WQ" : "CQ");
8926 		rc = -ENOMEM;
8927 		goto out_destroy;
8928 	}
8929 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8930 					phba->sli4_hba.els_cq,
8931 					phba->sli4_hba.els_wq,
8932 					NULL, 0, LPFC_ELS);
8933 	if (rc) {
8934 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8935 			"0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8936 			(uint32_t)rc);
8937 		goto out_destroy;
8938 	}
8939 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8940 			"2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8941 			phba->sli4_hba.els_wq->queue_id,
8942 			phba->sli4_hba.els_cq->queue_id);
8943 
8944 	if (phba->cfg_nvme_io_channel) {
8945 		/* Set up NVME LS Complete Queue */
8946 		if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8947 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8948 					"6091 LS %s not allocated\n",
8949 					phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8950 			rc = -ENOMEM;
8951 			goto out_destroy;
8952 		}
8953 		rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8954 					phba->sli4_hba.nvmels_cq,
8955 					phba->sli4_hba.nvmels_wq,
8956 					NULL, 0, LPFC_NVME_LS);
8957 		if (rc) {
8958 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8959 				"0529 Failed setup of NVVME LS WQ/CQ: "
8960 				"rc = 0x%x\n", (uint32_t)rc);
8961 			goto out_destroy;
8962 		}
8963 
8964 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8965 				"6096 ELS WQ setup: wq-id=%d, "
8966 				"parent cq-id=%d\n",
8967 				phba->sli4_hba.nvmels_wq->queue_id,
8968 				phba->sli4_hba.nvmels_cq->queue_id);
8969 	}
8970 
8971 	/*
8972 	 * Create NVMET Receive Queue (RQ)
8973 	 */
8974 	if (phba->nvmet_support) {
8975 		if ((!phba->sli4_hba.nvmet_cqset) ||
8976 		    (!phba->sli4_hba.nvmet_mrq_hdr) ||
8977 		    (!phba->sli4_hba.nvmet_mrq_data)) {
8978 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8979 					"6130 MRQ CQ Queues not "
8980 					"allocated\n");
8981 			rc = -ENOMEM;
8982 			goto out_destroy;
8983 		}
8984 		if (phba->cfg_nvmet_mrq > 1) {
8985 			rc = lpfc_mrq_create(phba,
8986 					     phba->sli4_hba.nvmet_mrq_hdr,
8987 					     phba->sli4_hba.nvmet_mrq_data,
8988 					     phba->sli4_hba.nvmet_cqset,
8989 					     LPFC_NVMET);
8990 			if (rc) {
8991 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8992 						"6098 Failed setup of NVMET "
8993 						"MRQ: rc = 0x%x\n",
8994 						(uint32_t)rc);
8995 				goto out_destroy;
8996 			}
8997 
8998 		} else {
8999 			rc = lpfc_rq_create(phba,
9000 					    phba->sli4_hba.nvmet_mrq_hdr[0],
9001 					    phba->sli4_hba.nvmet_mrq_data[0],
9002 					    phba->sli4_hba.nvmet_cqset[0],
9003 					    LPFC_NVMET);
9004 			if (rc) {
9005 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9006 						"6057 Failed setup of NVMET "
9007 						"Receive Queue: rc = 0x%x\n",
9008 						(uint32_t)rc);
9009 				goto out_destroy;
9010 			}
9011 
9012 			lpfc_printf_log(
9013 				phba, KERN_INFO, LOG_INIT,
9014 				"6099 NVMET RQ setup: hdr-rq-id=%d, "
9015 				"dat-rq-id=%d parent cq-id=%d\n",
9016 				phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
9017 				phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
9018 				phba->sli4_hba.nvmet_cqset[0]->queue_id);
9019 
9020 		}
9021 	}
9022 
9023 	if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
9024 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9025 				"0540 Receive Queue not allocated\n");
9026 		rc = -ENOMEM;
9027 		goto out_destroy;
9028 	}
9029 
9030 	rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
9031 			    phba->sli4_hba.els_cq, LPFC_USOL);
9032 	if (rc) {
9033 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9034 				"0541 Failed setup of Receive Queue: "
9035 				"rc = 0x%x\n", (uint32_t)rc);
9036 		goto out_destroy;
9037 	}
9038 
9039 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9040 			"2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
9041 			"parent cq-id=%d\n",
9042 			phba->sli4_hba.hdr_rq->queue_id,
9043 			phba->sli4_hba.dat_rq->queue_id,
9044 			phba->sli4_hba.els_cq->queue_id);
9045 
9046 	if (phba->cfg_fof) {
9047 		rc = lpfc_fof_queue_setup(phba);
9048 		if (rc) {
9049 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9050 					"0549 Failed setup of FOF Queues: "
9051 					"rc = 0x%x\n", rc);
9052 			goto out_destroy;
9053 		}
9054 	}
9055 
9056 	for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
9057 		lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
9058 					 phba->cfg_fcp_imax);
9059 
9060 	return 0;
9061 
9062 out_destroy:
9063 	lpfc_sli4_queue_unset(phba);
9064 out_error:
9065 	return rc;
9066 }
9067 
9068 /**
9069  * lpfc_sli4_queue_unset - Unset all the SLI4 queues
9070  * @phba: pointer to lpfc hba data structure.
9071  *
9072  * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
9073  * operation.
9074  *
9075  * Return codes
9076  *      0 - successful
9077  *      -ENOMEM - No available memory
9078  *      -EIO - The mailbox failed to complete successfully.
9079  **/
9080 void
9081 lpfc_sli4_queue_unset(struct lpfc_hba *phba)
9082 {
9083 	int qidx;
9084 
9085 	/* Unset the queues created for Flash Optimized Fabric operations */
9086 	if (phba->cfg_fof)
9087 		lpfc_fof_queue_destroy(phba);
9088 
9089 	/* Unset mailbox command work queue */
9090 	if (phba->sli4_hba.mbx_wq)
9091 		lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
9092 
9093 	/* Unset NVME LS work queue */
9094 	if (phba->sli4_hba.nvmels_wq)
9095 		lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
9096 
9097 	/* Unset ELS work queue */
9098 	if (phba->sli4_hba.els_wq)
9099 		lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
9100 
9101 	/* Unset unsolicited receive queue */
9102 	if (phba->sli4_hba.hdr_rq)
9103 		lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
9104 				phba->sli4_hba.dat_rq);
9105 
9106 	/* Unset FCP work queue */
9107 	if (phba->sli4_hba.fcp_wq)
9108 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9109 			lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
9110 
9111 	/* Unset NVME work queue */
9112 	if (phba->sli4_hba.nvme_wq) {
9113 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
9114 			lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
9115 	}
9116 
9117 	/* Unset mailbox command complete queue */
9118 	if (phba->sli4_hba.mbx_cq)
9119 		lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
9120 
9121 	/* Unset ELS complete queue */
9122 	if (phba->sli4_hba.els_cq)
9123 		lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
9124 
9125 	/* Unset NVME LS complete queue */
9126 	if (phba->sli4_hba.nvmels_cq)
9127 		lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
9128 
9129 	/* Unset NVME response complete queue */
9130 	if (phba->sli4_hba.nvme_cq)
9131 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
9132 			lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
9133 
9134 	if (phba->nvmet_support) {
9135 		/* Unset NVMET MRQ queue */
9136 		if (phba->sli4_hba.nvmet_mrq_hdr) {
9137 			for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9138 				lpfc_rq_destroy(
9139 					phba,
9140 					phba->sli4_hba.nvmet_mrq_hdr[qidx],
9141 					phba->sli4_hba.nvmet_mrq_data[qidx]);
9142 		}
9143 
9144 		/* Unset NVMET CQ Set complete queue */
9145 		if (phba->sli4_hba.nvmet_cqset) {
9146 			for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
9147 				lpfc_cq_destroy(
9148 					phba, phba->sli4_hba.nvmet_cqset[qidx]);
9149 		}
9150 	}
9151 
9152 	/* Unset FCP response complete queue */
9153 	if (phba->sli4_hba.fcp_cq)
9154 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9155 			lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
9156 
9157 	/* Unset fast-path event queue */
9158 	if (phba->sli4_hba.hba_eq)
9159 		for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
9160 			lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
9161 }
9162 
9163 /**
9164  * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9165  * @phba: pointer to lpfc hba data structure.
9166  *
9167  * This routine is invoked to allocate and set up a pool of completion queue
9168  * events. The body of the completion queue event is a completion queue entry
9169  * CQE. For now, this pool is used for the interrupt service routine to queue
9170  * the following HBA completion queue events for the worker thread to process:
9171  *   - Mailbox asynchronous events
9172  *   - Receive queue completion unsolicited events
9173  * Later, this can be used for all the slow-path events.
9174  *
9175  * Return codes
9176  *      0 - successful
9177  *      -ENOMEM - No available memory
9178  **/
9179 static int
9180 lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9181 {
9182 	struct lpfc_cq_event *cq_event;
9183 	int i;
9184 
9185 	for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9186 		cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9187 		if (!cq_event)
9188 			goto out_pool_create_fail;
9189 		list_add_tail(&cq_event->list,
9190 			      &phba->sli4_hba.sp_cqe_event_pool);
9191 	}
9192 	return 0;
9193 
9194 out_pool_create_fail:
9195 	lpfc_sli4_cq_event_pool_destroy(phba);
9196 	return -ENOMEM;
9197 }
9198 
9199 /**
9200  * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9201  * @phba: pointer to lpfc hba data structure.
9202  *
9203  * This routine is invoked to free the pool of completion queue events at
9204  * driver unload time. Note that, it is the responsibility of the driver
9205  * cleanup routine to free all the outstanding completion-queue events
9206  * allocated from this pool back into the pool before invoking this routine
9207  * to destroy the pool.
9208  **/
9209 static void
9210 lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9211 {
9212 	struct lpfc_cq_event *cq_event, *next_cq_event;
9213 
9214 	list_for_each_entry_safe(cq_event, next_cq_event,
9215 				 &phba->sli4_hba.sp_cqe_event_pool, list) {
9216 		list_del(&cq_event->list);
9217 		kfree(cq_event);
9218 	}
9219 }
9220 
9221 /**
9222  * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9223  * @phba: pointer to lpfc hba data structure.
9224  *
9225  * This routine is the lock free version of the API invoked to allocate a
9226  * completion-queue event from the free pool.
9227  *
9228  * Return: Pointer to the newly allocated completion-queue event if successful
9229  *         NULL otherwise.
9230  **/
9231 struct lpfc_cq_event *
9232 __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9233 {
9234 	struct lpfc_cq_event *cq_event = NULL;
9235 
9236 	list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9237 			 struct lpfc_cq_event, list);
9238 	return cq_event;
9239 }
9240 
9241 /**
9242  * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9243  * @phba: pointer to lpfc hba data structure.
9244  *
9245  * This routine is the lock version of the API invoked to allocate a
9246  * completion-queue event from the free pool.
9247  *
9248  * Return: Pointer to the newly allocated completion-queue event if successful
9249  *         NULL otherwise.
9250  **/
9251 struct lpfc_cq_event *
9252 lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9253 {
9254 	struct lpfc_cq_event *cq_event;
9255 	unsigned long iflags;
9256 
9257 	spin_lock_irqsave(&phba->hbalock, iflags);
9258 	cq_event = __lpfc_sli4_cq_event_alloc(phba);
9259 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9260 	return cq_event;
9261 }
9262 
9263 /**
9264  * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9265  * @phba: pointer to lpfc hba data structure.
9266  * @cq_event: pointer to the completion queue event to be freed.
9267  *
9268  * This routine is the lock free version of the API invoked to release a
9269  * completion-queue event back into the free pool.
9270  **/
9271 void
9272 __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9273 			     struct lpfc_cq_event *cq_event)
9274 {
9275 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9276 }
9277 
9278 /**
9279  * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9280  * @phba: pointer to lpfc hba data structure.
9281  * @cq_event: pointer to the completion queue event to be freed.
9282  *
9283  * This routine is the lock version of the API invoked to release a
9284  * completion-queue event back into the free pool.
9285  **/
9286 void
9287 lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9288 			   struct lpfc_cq_event *cq_event)
9289 {
9290 	unsigned long iflags;
9291 	spin_lock_irqsave(&phba->hbalock, iflags);
9292 	__lpfc_sli4_cq_event_release(phba, cq_event);
9293 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9294 }
9295 
9296 /**
9297  * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9298  * @phba: pointer to lpfc hba data structure.
9299  *
9300  * This routine is to free all the pending completion-queue events to the
9301  * back into the free pool for device reset.
9302  **/
9303 static void
9304 lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9305 {
9306 	LIST_HEAD(cqelist);
9307 	struct lpfc_cq_event *cqe;
9308 	unsigned long iflags;
9309 
9310 	/* Retrieve all the pending WCQEs from pending WCQE lists */
9311 	spin_lock_irqsave(&phba->hbalock, iflags);
9312 	/* Pending FCP XRI abort events */
9313 	list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9314 			 &cqelist);
9315 	/* Pending ELS XRI abort events */
9316 	list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9317 			 &cqelist);
9318 	/* Pending asynnc events */
9319 	list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9320 			 &cqelist);
9321 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9322 
9323 	while (!list_empty(&cqelist)) {
9324 		list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9325 		lpfc_sli4_cq_event_release(phba, cqe);
9326 	}
9327 }
9328 
9329 /**
9330  * lpfc_pci_function_reset - Reset pci function.
9331  * @phba: pointer to lpfc hba data structure.
9332  *
9333  * This routine is invoked to request a PCI function reset. It will destroys
9334  * all resources assigned to the PCI function which originates this request.
9335  *
9336  * Return codes
9337  *      0 - successful
9338  *      -ENOMEM - No available memory
9339  *      -EIO - The mailbox failed to complete successfully.
9340  **/
9341 int
9342 lpfc_pci_function_reset(struct lpfc_hba *phba)
9343 {
9344 	LPFC_MBOXQ_t *mboxq;
9345 	uint32_t rc = 0, if_type;
9346 	uint32_t shdr_status, shdr_add_status;
9347 	uint32_t rdy_chk;
9348 	uint32_t port_reset = 0;
9349 	union lpfc_sli4_cfg_shdr *shdr;
9350 	struct lpfc_register reg_data;
9351 	uint16_t devid;
9352 
9353 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9354 	switch (if_type) {
9355 	case LPFC_SLI_INTF_IF_TYPE_0:
9356 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9357 						       GFP_KERNEL);
9358 		if (!mboxq) {
9359 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9360 					"0494 Unable to allocate memory for "
9361 					"issuing SLI_FUNCTION_RESET mailbox "
9362 					"command\n");
9363 			return -ENOMEM;
9364 		}
9365 
9366 		/* Setup PCI function reset mailbox-ioctl command */
9367 		lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9368 				 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9369 				 LPFC_SLI4_MBX_EMBED);
9370 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9371 		shdr = (union lpfc_sli4_cfg_shdr *)
9372 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9373 		shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9374 		shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9375 					 &shdr->response);
9376 		if (rc != MBX_TIMEOUT)
9377 			mempool_free(mboxq, phba->mbox_mem_pool);
9378 		if (shdr_status || shdr_add_status || rc) {
9379 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9380 					"0495 SLI_FUNCTION_RESET mailbox "
9381 					"failed with status x%x add_status x%x,"
9382 					" mbx status x%x\n",
9383 					shdr_status, shdr_add_status, rc);
9384 			rc = -ENXIO;
9385 		}
9386 		break;
9387 	case LPFC_SLI_INTF_IF_TYPE_2:
9388 	case LPFC_SLI_INTF_IF_TYPE_6:
9389 wait:
9390 		/*
9391 		 * Poll the Port Status Register and wait for RDY for
9392 		 * up to 30 seconds. If the port doesn't respond, treat
9393 		 * it as an error.
9394 		 */
9395 		for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
9396 			if (lpfc_readl(phba->sli4_hba.u.if_type2.
9397 				STATUSregaddr, &reg_data.word0)) {
9398 				rc = -ENODEV;
9399 				goto out;
9400 			}
9401 			if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9402 				break;
9403 			msleep(20);
9404 		}
9405 
9406 		if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9407 			phba->work_status[0] = readl(
9408 				phba->sli4_hba.u.if_type2.ERR1regaddr);
9409 			phba->work_status[1] = readl(
9410 				phba->sli4_hba.u.if_type2.ERR2regaddr);
9411 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9412 					"2890 Port not ready, port status reg "
9413 					"0x%x error 1=0x%x, error 2=0x%x\n",
9414 					reg_data.word0,
9415 					phba->work_status[0],
9416 					phba->work_status[1]);
9417 			rc = -ENODEV;
9418 			goto out;
9419 		}
9420 
9421 		if (!port_reset) {
9422 			/*
9423 			 * Reset the port now
9424 			 */
9425 			reg_data.word0 = 0;
9426 			bf_set(lpfc_sliport_ctrl_end, &reg_data,
9427 			       LPFC_SLIPORT_LITTLE_ENDIAN);
9428 			bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9429 			       LPFC_SLIPORT_INIT_PORT);
9430 			writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9431 			       CTRLregaddr);
9432 			/* flush */
9433 			pci_read_config_word(phba->pcidev,
9434 					     PCI_DEVICE_ID, &devid);
9435 
9436 			port_reset = 1;
9437 			msleep(20);
9438 			goto wait;
9439 		} else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9440 			rc = -ENODEV;
9441 			goto out;
9442 		}
9443 		break;
9444 
9445 	case LPFC_SLI_INTF_IF_TYPE_1:
9446 	default:
9447 		break;
9448 	}
9449 
9450 out:
9451 	/* Catch the not-ready port failure after a port reset. */
9452 	if (rc) {
9453 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9454 				"3317 HBA not functional: IP Reset Failed "
9455 				"try: echo fw_reset > board_mode\n");
9456 		rc = -ENODEV;
9457 	}
9458 
9459 	return rc;
9460 }
9461 
9462 /**
9463  * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9464  * @phba: pointer to lpfc hba data structure.
9465  *
9466  * This routine is invoked to set up the PCI device memory space for device
9467  * with SLI-4 interface spec.
9468  *
9469  * Return codes
9470  * 	0 - successful
9471  * 	other values - error
9472  **/
9473 static int
9474 lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9475 {
9476 	struct pci_dev *pdev;
9477 	unsigned long bar0map_len, bar1map_len, bar2map_len;
9478 	int error = -ENODEV;
9479 	uint32_t if_type;
9480 
9481 	/* Obtain PCI device reference */
9482 	if (!phba->pcidev)
9483 		return error;
9484 	else
9485 		pdev = phba->pcidev;
9486 
9487 	/* Set the device DMA mask size */
9488 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9489 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9490 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9491 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
9492 			return error;
9493 		}
9494 	}
9495 
9496 	/*
9497 	 * The BARs and register set definitions and offset locations are
9498 	 * dependent on the if_type.
9499 	 */
9500 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9501 				  &phba->sli4_hba.sli_intf.word0)) {
9502 		return error;
9503 	}
9504 
9505 	/* There is no SLI3 failback for SLI4 devices. */
9506 	if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9507 	    LPFC_SLI_INTF_VALID) {
9508 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9509 				"2894 SLI_INTF reg contents invalid "
9510 				"sli_intf reg 0x%x\n",
9511 				phba->sli4_hba.sli_intf.word0);
9512 		return error;
9513 	}
9514 
9515 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9516 	/*
9517 	 * Get the bus address of SLI4 device Bar regions and the
9518 	 * number of bytes required by each mapping. The mapping of the
9519 	 * particular PCI BARs regions is dependent on the type of
9520 	 * SLI4 device.
9521 	 */
9522 	if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9523 		phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9524 		bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
9525 
9526 		/*
9527 		 * Map SLI4 PCI Config Space Register base to a kernel virtual
9528 		 * addr
9529 		 */
9530 		phba->sli4_hba.conf_regs_memmap_p =
9531 			ioremap(phba->pci_bar0_map, bar0map_len);
9532 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9533 			dev_printk(KERN_ERR, &pdev->dev,
9534 				   "ioremap failed for SLI4 PCI config "
9535 				   "registers.\n");
9536 			goto out;
9537 		}
9538 		phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
9539 		/* Set up BAR0 PCI config space register memory map */
9540 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9541 	} else {
9542 		phba->pci_bar0_map = pci_resource_start(pdev, 1);
9543 		bar0map_len = pci_resource_len(pdev, 1);
9544 		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9545 			dev_printk(KERN_ERR, &pdev->dev,
9546 			   "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9547 			goto out;
9548 		}
9549 		phba->sli4_hba.conf_regs_memmap_p =
9550 				ioremap(phba->pci_bar0_map, bar0map_len);
9551 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9552 			dev_printk(KERN_ERR, &pdev->dev,
9553 				"ioremap failed for SLI4 PCI config "
9554 				"registers.\n");
9555 				goto out;
9556 		}
9557 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9558 	}
9559 
9560 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9561 		if (pci_resource_start(pdev, PCI_64BIT_BAR2)) {
9562 			/*
9563 			 * Map SLI4 if type 0 HBA Control Register base to a
9564 			 * kernel virtual address and setup the registers.
9565 			 */
9566 			phba->pci_bar1_map = pci_resource_start(pdev,
9567 								PCI_64BIT_BAR2);
9568 			bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9569 			phba->sli4_hba.ctrl_regs_memmap_p =
9570 					ioremap(phba->pci_bar1_map,
9571 						bar1map_len);
9572 			if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9573 				dev_err(&pdev->dev,
9574 					   "ioremap failed for SLI4 HBA "
9575 					    "control registers.\n");
9576 				error = -ENOMEM;
9577 				goto out_iounmap_conf;
9578 			}
9579 			phba->pci_bar2_memmap_p =
9580 					 phba->sli4_hba.ctrl_regs_memmap_p;
9581 			lpfc_sli4_bar1_register_memmap(phba, if_type);
9582 		} else {
9583 			error = -ENOMEM;
9584 			goto out_iounmap_conf;
9585 		}
9586 	}
9587 
9588 	if ((if_type == LPFC_SLI_INTF_IF_TYPE_6) &&
9589 	    (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
9590 		/*
9591 		 * Map SLI4 if type 6 HBA Doorbell Register base to a kernel
9592 		 * virtual address and setup the registers.
9593 		 */
9594 		phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
9595 		bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9596 		phba->sli4_hba.drbl_regs_memmap_p =
9597 				ioremap(phba->pci_bar1_map, bar1map_len);
9598 		if (!phba->sli4_hba.drbl_regs_memmap_p) {
9599 			dev_err(&pdev->dev,
9600 			   "ioremap failed for SLI4 HBA doorbell registers.\n");
9601 			goto out_iounmap_conf;
9602 		}
9603 		phba->pci_bar2_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
9604 		lpfc_sli4_bar1_register_memmap(phba, if_type);
9605 	}
9606 
9607 	if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
9608 		if (pci_resource_start(pdev, PCI_64BIT_BAR4)) {
9609 			/*
9610 			 * Map SLI4 if type 0 HBA Doorbell Register base to
9611 			 * a kernel virtual address and setup the registers.
9612 			 */
9613 			phba->pci_bar2_map = pci_resource_start(pdev,
9614 								PCI_64BIT_BAR4);
9615 			bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9616 			phba->sli4_hba.drbl_regs_memmap_p =
9617 					ioremap(phba->pci_bar2_map,
9618 						bar2map_len);
9619 			if (!phba->sli4_hba.drbl_regs_memmap_p) {
9620 				dev_err(&pdev->dev,
9621 					   "ioremap failed for SLI4 HBA"
9622 					   " doorbell registers.\n");
9623 				error = -ENOMEM;
9624 				goto out_iounmap_ctrl;
9625 			}
9626 			phba->pci_bar4_memmap_p =
9627 					phba->sli4_hba.drbl_regs_memmap_p;
9628 			error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9629 			if (error)
9630 				goto out_iounmap_all;
9631 		} else {
9632 			error = -ENOMEM;
9633 			goto out_iounmap_all;
9634 		}
9635 	}
9636 
9637 	if (if_type == LPFC_SLI_INTF_IF_TYPE_6 &&
9638 	    pci_resource_start(pdev, PCI_64BIT_BAR4)) {
9639 		/*
9640 		 * Map SLI4 if type 6 HBA DPP Register base to a kernel
9641 		 * virtual address and setup the registers.
9642 		 */
9643 		phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
9644 		bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9645 		phba->sli4_hba.dpp_regs_memmap_p =
9646 				ioremap(phba->pci_bar2_map, bar2map_len);
9647 		if (!phba->sli4_hba.dpp_regs_memmap_p) {
9648 			dev_err(&pdev->dev,
9649 			   "ioremap failed for SLI4 HBA dpp registers.\n");
9650 			goto out_iounmap_ctrl;
9651 		}
9652 		phba->pci_bar4_memmap_p = phba->sli4_hba.dpp_regs_memmap_p;
9653 	}
9654 
9655 	/* Set up the EQ/CQ register handeling functions now */
9656 	switch (if_type) {
9657 	case LPFC_SLI_INTF_IF_TYPE_0:
9658 	case LPFC_SLI_INTF_IF_TYPE_2:
9659 		phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_eq_clr_intr;
9660 		phba->sli4_hba.sli4_eq_release = lpfc_sli4_eq_release;
9661 		phba->sli4_hba.sli4_cq_release = lpfc_sli4_cq_release;
9662 		break;
9663 	case LPFC_SLI_INTF_IF_TYPE_6:
9664 		phba->sli4_hba.sli4_eq_clr_intr = lpfc_sli4_if6_eq_clr_intr;
9665 		phba->sli4_hba.sli4_eq_release = lpfc_sli4_if6_eq_release;
9666 		phba->sli4_hba.sli4_cq_release = lpfc_sli4_if6_cq_release;
9667 		break;
9668 	default:
9669 		break;
9670 	}
9671 
9672 	return 0;
9673 
9674 out_iounmap_all:
9675 	iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9676 out_iounmap_ctrl:
9677 	iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9678 out_iounmap_conf:
9679 	iounmap(phba->sli4_hba.conf_regs_memmap_p);
9680 out:
9681 	return error;
9682 }
9683 
9684 /**
9685  * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9686  * @phba: pointer to lpfc hba data structure.
9687  *
9688  * This routine is invoked to unset the PCI device memory space for device
9689  * with SLI-4 interface spec.
9690  **/
9691 static void
9692 lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9693 {
9694 	uint32_t if_type;
9695 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9696 
9697 	switch (if_type) {
9698 	case LPFC_SLI_INTF_IF_TYPE_0:
9699 		iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9700 		iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9701 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9702 		break;
9703 	case LPFC_SLI_INTF_IF_TYPE_2:
9704 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9705 		break;
9706 	case LPFC_SLI_INTF_IF_TYPE_6:
9707 		iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9708 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9709 		break;
9710 	case LPFC_SLI_INTF_IF_TYPE_1:
9711 	default:
9712 		dev_printk(KERN_ERR, &phba->pcidev->dev,
9713 			   "FATAL - unsupported SLI4 interface type - %d\n",
9714 			   if_type);
9715 		break;
9716 	}
9717 }
9718 
9719 /**
9720  * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9721  * @phba: pointer to lpfc hba data structure.
9722  *
9723  * This routine is invoked to enable the MSI-X interrupt vectors to device
9724  * with SLI-3 interface specs.
9725  *
9726  * Return codes
9727  *   0 - successful
9728  *   other values - error
9729  **/
9730 static int
9731 lpfc_sli_enable_msix(struct lpfc_hba *phba)
9732 {
9733 	int rc;
9734 	LPFC_MBOXQ_t *pmb;
9735 
9736 	/* Set up MSI-X multi-message vectors */
9737 	rc = pci_alloc_irq_vectors(phba->pcidev,
9738 			LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9739 	if (rc < 0) {
9740 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9741 				"0420 PCI enable MSI-X failed (%d)\n", rc);
9742 		goto vec_fail_out;
9743 	}
9744 
9745 	/*
9746 	 * Assign MSI-X vectors to interrupt handlers
9747 	 */
9748 
9749 	/* vector-0 is associated to slow-path handler */
9750 	rc = request_irq(pci_irq_vector(phba->pcidev, 0),
9751 			 &lpfc_sli_sp_intr_handler, 0,
9752 			 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9753 	if (rc) {
9754 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9755 				"0421 MSI-X slow-path request_irq failed "
9756 				"(%d)\n", rc);
9757 		goto msi_fail_out;
9758 	}
9759 
9760 	/* vector-1 is associated to fast-path handler */
9761 	rc = request_irq(pci_irq_vector(phba->pcidev, 1),
9762 			 &lpfc_sli_fp_intr_handler, 0,
9763 			 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9764 
9765 	if (rc) {
9766 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9767 				"0429 MSI-X fast-path request_irq failed "
9768 				"(%d)\n", rc);
9769 		goto irq_fail_out;
9770 	}
9771 
9772 	/*
9773 	 * Configure HBA MSI-X attention conditions to messages
9774 	 */
9775 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9776 
9777 	if (!pmb) {
9778 		rc = -ENOMEM;
9779 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9780 				"0474 Unable to allocate memory for issuing "
9781 				"MBOX_CONFIG_MSI command\n");
9782 		goto mem_fail_out;
9783 	}
9784 	rc = lpfc_config_msi(phba, pmb);
9785 	if (rc)
9786 		goto mbx_fail_out;
9787 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9788 	if (rc != MBX_SUCCESS) {
9789 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9790 				"0351 Config MSI mailbox command failed, "
9791 				"mbxCmd x%x, mbxStatus x%x\n",
9792 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9793 		goto mbx_fail_out;
9794 	}
9795 
9796 	/* Free memory allocated for mailbox command */
9797 	mempool_free(pmb, phba->mbox_mem_pool);
9798 	return rc;
9799 
9800 mbx_fail_out:
9801 	/* Free memory allocated for mailbox command */
9802 	mempool_free(pmb, phba->mbox_mem_pool);
9803 
9804 mem_fail_out:
9805 	/* free the irq already requested */
9806 	free_irq(pci_irq_vector(phba->pcidev, 1), phba);
9807 
9808 irq_fail_out:
9809 	/* free the irq already requested */
9810 	free_irq(pci_irq_vector(phba->pcidev, 0), phba);
9811 
9812 msi_fail_out:
9813 	/* Unconfigure MSI-X capability structure */
9814 	pci_free_irq_vectors(phba->pcidev);
9815 
9816 vec_fail_out:
9817 	return rc;
9818 }
9819 
9820 /**
9821  * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9822  * @phba: pointer to lpfc hba data structure.
9823  *
9824  * This routine is invoked to enable the MSI interrupt mode to device with
9825  * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9826  * enable the MSI vector. The device driver is responsible for calling the
9827  * request_irq() to register MSI vector with a interrupt the handler, which
9828  * is done in this function.
9829  *
9830  * Return codes
9831  * 	0 - successful
9832  * 	other values - error
9833  */
9834 static int
9835 lpfc_sli_enable_msi(struct lpfc_hba *phba)
9836 {
9837 	int rc;
9838 
9839 	rc = pci_enable_msi(phba->pcidev);
9840 	if (!rc)
9841 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9842 				"0462 PCI enable MSI mode success.\n");
9843 	else {
9844 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9845 				"0471 PCI enable MSI mode failed (%d)\n", rc);
9846 		return rc;
9847 	}
9848 
9849 	rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9850 			 0, LPFC_DRIVER_NAME, phba);
9851 	if (rc) {
9852 		pci_disable_msi(phba->pcidev);
9853 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9854 				"0478 MSI request_irq failed (%d)\n", rc);
9855 	}
9856 	return rc;
9857 }
9858 
9859 /**
9860  * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9861  * @phba: pointer to lpfc hba data structure.
9862  *
9863  * This routine is invoked to enable device interrupt and associate driver's
9864  * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9865  * spec. Depends on the interrupt mode configured to the driver, the driver
9866  * will try to fallback from the configured interrupt mode to an interrupt
9867  * mode which is supported by the platform, kernel, and device in the order
9868  * of:
9869  * MSI-X -> MSI -> IRQ.
9870  *
9871  * Return codes
9872  *   0 - successful
9873  *   other values - error
9874  **/
9875 static uint32_t
9876 lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9877 {
9878 	uint32_t intr_mode = LPFC_INTR_ERROR;
9879 	int retval;
9880 
9881 	if (cfg_mode == 2) {
9882 		/* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9883 		retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9884 		if (!retval) {
9885 			/* Now, try to enable MSI-X interrupt mode */
9886 			retval = lpfc_sli_enable_msix(phba);
9887 			if (!retval) {
9888 				/* Indicate initialization to MSI-X mode */
9889 				phba->intr_type = MSIX;
9890 				intr_mode = 2;
9891 			}
9892 		}
9893 	}
9894 
9895 	/* Fallback to MSI if MSI-X initialization failed */
9896 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
9897 		retval = lpfc_sli_enable_msi(phba);
9898 		if (!retval) {
9899 			/* Indicate initialization to MSI mode */
9900 			phba->intr_type = MSI;
9901 			intr_mode = 1;
9902 		}
9903 	}
9904 
9905 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
9906 	if (phba->intr_type == NONE) {
9907 		retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9908 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9909 		if (!retval) {
9910 			/* Indicate initialization to INTx mode */
9911 			phba->intr_type = INTx;
9912 			intr_mode = 0;
9913 		}
9914 	}
9915 	return intr_mode;
9916 }
9917 
9918 /**
9919  * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9920  * @phba: pointer to lpfc hba data structure.
9921  *
9922  * This routine is invoked to disable device interrupt and disassociate the
9923  * driver's interrupt handler(s) from interrupt vector(s) to device with
9924  * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9925  * release the interrupt vector(s) for the message signaled interrupt.
9926  **/
9927 static void
9928 lpfc_sli_disable_intr(struct lpfc_hba *phba)
9929 {
9930 	int nr_irqs, i;
9931 
9932 	if (phba->intr_type == MSIX)
9933 		nr_irqs = LPFC_MSIX_VECTORS;
9934 	else
9935 		nr_irqs = 1;
9936 
9937 	for (i = 0; i < nr_irqs; i++)
9938 		free_irq(pci_irq_vector(phba->pcidev, i), phba);
9939 	pci_free_irq_vectors(phba->pcidev);
9940 
9941 	/* Reset interrupt management states */
9942 	phba->intr_type = NONE;
9943 	phba->sli.slistat.sli_intr = 0;
9944 }
9945 
9946 /**
9947  * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
9948  * @phba: pointer to lpfc hba data structure.
9949  * @vectors: number of msix vectors allocated.
9950  *
9951  * The routine will figure out the CPU affinity assignment for every
9952  * MSI-X vector allocated for the HBA.  The hba_eq_hdl will be updated
9953  * with a pointer to the CPU mask that defines ALL the CPUs this vector
9954  * can be associated with. If the vector can be unquely associated with
9955  * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9956  * In addition, the CPU to IO channel mapping will be calculated
9957  * and the phba->sli4_hba.cpu_map array will reflect this.
9958  */
9959 static void
9960 lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
9961 {
9962 	struct lpfc_vector_map_info *cpup;
9963 	int index = 0;
9964 	int vec = 0;
9965 	int cpu;
9966 #ifdef CONFIG_X86
9967 	struct cpuinfo_x86 *cpuinfo;
9968 #endif
9969 
9970 	/* Init cpu_map array */
9971 	memset(phba->sli4_hba.cpu_map, 0xff,
9972 	       (sizeof(struct lpfc_vector_map_info) *
9973 	       phba->sli4_hba.num_present_cpu));
9974 
9975 	/* Update CPU map with physical id and core id of each CPU */
9976 	cpup = phba->sli4_hba.cpu_map;
9977 	for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9978 #ifdef CONFIG_X86
9979 		cpuinfo = &cpu_data(cpu);
9980 		cpup->phys_id = cpuinfo->phys_proc_id;
9981 		cpup->core_id = cpuinfo->cpu_core_id;
9982 #else
9983 		/* No distinction between CPUs for other platforms */
9984 		cpup->phys_id = 0;
9985 		cpup->core_id = 0;
9986 #endif
9987 		cpup->channel_id = index;  /* For now round robin */
9988 		cpup->irq = pci_irq_vector(phba->pcidev, vec);
9989 		vec++;
9990 		if (vec >= vectors)
9991 			vec = 0;
9992 		index++;
9993 		if (index >= phba->cfg_fcp_io_channel)
9994 			index = 0;
9995 		cpup++;
9996 	}
9997 }
9998 
9999 
10000 /**
10001  * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
10002  * @phba: pointer to lpfc hba data structure.
10003  *
10004  * This routine is invoked to enable the MSI-X interrupt vectors to device
10005  * with SLI-4 interface spec.
10006  *
10007  * Return codes
10008  * 0 - successful
10009  * other values - error
10010  **/
10011 static int
10012 lpfc_sli4_enable_msix(struct lpfc_hba *phba)
10013 {
10014 	int vectors, rc, index;
10015 	char *name;
10016 
10017 	/* Set up MSI-X multi-message vectors */
10018 	vectors = phba->io_channel_irqs;
10019 	if (phba->cfg_fof)
10020 		vectors++;
10021 
10022 	rc = pci_alloc_irq_vectors(phba->pcidev,
10023 				(phba->nvmet_support) ? 1 : 2,
10024 				vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
10025 	if (rc < 0) {
10026 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10027 				"0484 PCI enable MSI-X failed (%d)\n", rc);
10028 		goto vec_fail_out;
10029 	}
10030 	vectors = rc;
10031 
10032 	/* Assign MSI-X vectors to interrupt handlers */
10033 	for (index = 0; index < vectors; index++) {
10034 		name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
10035 		memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
10036 		snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
10037 			 LPFC_DRIVER_HANDLER_NAME"%d", index);
10038 
10039 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
10040 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
10041 		atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
10042 		if (phba->cfg_fof && (index == (vectors - 1)))
10043 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
10044 				 &lpfc_sli4_fof_intr_handler, 0,
10045 				 name,
10046 				 &phba->sli4_hba.hba_eq_hdl[index]);
10047 		else
10048 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
10049 				 &lpfc_sli4_hba_intr_handler, 0,
10050 				 name,
10051 				 &phba->sli4_hba.hba_eq_hdl[index]);
10052 		if (rc) {
10053 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10054 					"0486 MSI-X fast-path (%d) "
10055 					"request_irq failed (%d)\n", index, rc);
10056 			goto cfg_fail_out;
10057 		}
10058 	}
10059 
10060 	if (phba->cfg_fof)
10061 		vectors--;
10062 
10063 	if (vectors != phba->io_channel_irqs) {
10064 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10065 				"3238 Reducing IO channels to match number of "
10066 				"MSI-X vectors, requested %d got %d\n",
10067 				phba->io_channel_irqs, vectors);
10068 		if (phba->cfg_fcp_io_channel > vectors)
10069 			phba->cfg_fcp_io_channel = vectors;
10070 		if (phba->cfg_nvme_io_channel > vectors)
10071 			phba->cfg_nvme_io_channel = vectors;
10072 		if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
10073 			phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10074 		else
10075 			phba->io_channel_irqs = phba->cfg_nvme_io_channel;
10076 	}
10077 	lpfc_cpu_affinity_check(phba, vectors);
10078 
10079 	return rc;
10080 
10081 cfg_fail_out:
10082 	/* free the irq already requested */
10083 	for (--index; index >= 0; index--)
10084 		free_irq(pci_irq_vector(phba->pcidev, index),
10085 				&phba->sli4_hba.hba_eq_hdl[index]);
10086 
10087 	/* Unconfigure MSI-X capability structure */
10088 	pci_free_irq_vectors(phba->pcidev);
10089 
10090 vec_fail_out:
10091 	return rc;
10092 }
10093 
10094 /**
10095  * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
10096  * @phba: pointer to lpfc hba data structure.
10097  *
10098  * This routine is invoked to enable the MSI interrupt mode to device with
10099  * SLI-4 interface spec. The kernel function pci_enable_msi() is called
10100  * to enable the MSI vector. The device driver is responsible for calling
10101  * the request_irq() to register MSI vector with a interrupt the handler,
10102  * which is done in this function.
10103  *
10104  * Return codes
10105  * 	0 - successful
10106  * 	other values - error
10107  **/
10108 static int
10109 lpfc_sli4_enable_msi(struct lpfc_hba *phba)
10110 {
10111 	int rc, index;
10112 
10113 	rc = pci_enable_msi(phba->pcidev);
10114 	if (!rc)
10115 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10116 				"0487 PCI enable MSI mode success.\n");
10117 	else {
10118 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10119 				"0488 PCI enable MSI mode failed (%d)\n", rc);
10120 		return rc;
10121 	}
10122 
10123 	rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
10124 			 0, LPFC_DRIVER_NAME, phba);
10125 	if (rc) {
10126 		pci_disable_msi(phba->pcidev);
10127 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10128 				"0490 MSI request_irq failed (%d)\n", rc);
10129 		return rc;
10130 	}
10131 
10132 	for (index = 0; index < phba->io_channel_irqs; index++) {
10133 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
10134 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
10135 	}
10136 
10137 	if (phba->cfg_fof) {
10138 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
10139 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
10140 	}
10141 	return 0;
10142 }
10143 
10144 /**
10145  * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
10146  * @phba: pointer to lpfc hba data structure.
10147  *
10148  * This routine is invoked to enable device interrupt and associate driver's
10149  * interrupt handler(s) to interrupt vector(s) to device with SLI-4
10150  * interface spec. Depends on the interrupt mode configured to the driver,
10151  * the driver will try to fallback from the configured interrupt mode to an
10152  * interrupt mode which is supported by the platform, kernel, and device in
10153  * the order of:
10154  * MSI-X -> MSI -> IRQ.
10155  *
10156  * Return codes
10157  * 	0 - successful
10158  * 	other values - error
10159  **/
10160 static uint32_t
10161 lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
10162 {
10163 	uint32_t intr_mode = LPFC_INTR_ERROR;
10164 	int retval, idx;
10165 
10166 	if (cfg_mode == 2) {
10167 		/* Preparation before conf_msi mbox cmd */
10168 		retval = 0;
10169 		if (!retval) {
10170 			/* Now, try to enable MSI-X interrupt mode */
10171 			retval = lpfc_sli4_enable_msix(phba);
10172 			if (!retval) {
10173 				/* Indicate initialization to MSI-X mode */
10174 				phba->intr_type = MSIX;
10175 				intr_mode = 2;
10176 			}
10177 		}
10178 	}
10179 
10180 	/* Fallback to MSI if MSI-X initialization failed */
10181 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
10182 		retval = lpfc_sli4_enable_msi(phba);
10183 		if (!retval) {
10184 			/* Indicate initialization to MSI mode */
10185 			phba->intr_type = MSI;
10186 			intr_mode = 1;
10187 		}
10188 	}
10189 
10190 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
10191 	if (phba->intr_type == NONE) {
10192 		retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
10193 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
10194 		if (!retval) {
10195 			struct lpfc_hba_eq_hdl *eqhdl;
10196 
10197 			/* Indicate initialization to INTx mode */
10198 			phba->intr_type = INTx;
10199 			intr_mode = 0;
10200 
10201 			for (idx = 0; idx < phba->io_channel_irqs; idx++) {
10202 				eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10203 				eqhdl->idx = idx;
10204 				eqhdl->phba = phba;
10205 				atomic_set(&eqhdl->hba_eq_in_use, 1);
10206 			}
10207 			if (phba->cfg_fof) {
10208 				eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
10209 				eqhdl->idx = idx;
10210 				eqhdl->phba = phba;
10211 				atomic_set(&eqhdl->hba_eq_in_use, 1);
10212 			}
10213 		}
10214 	}
10215 	return intr_mode;
10216 }
10217 
10218 /**
10219  * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
10220  * @phba: pointer to lpfc hba data structure.
10221  *
10222  * This routine is invoked to disable device interrupt and disassociate
10223  * the driver's interrupt handler(s) from interrupt vector(s) to device
10224  * with SLI-4 interface spec. Depending on the interrupt mode, the driver
10225  * will release the interrupt vector(s) for the message signaled interrupt.
10226  **/
10227 static void
10228 lpfc_sli4_disable_intr(struct lpfc_hba *phba)
10229 {
10230 	/* Disable the currently initialized interrupt mode */
10231 	if (phba->intr_type == MSIX) {
10232 		int index;
10233 
10234 		/* Free up MSI-X multi-message vectors */
10235 		for (index = 0; index < phba->io_channel_irqs; index++)
10236 			free_irq(pci_irq_vector(phba->pcidev, index),
10237 					&phba->sli4_hba.hba_eq_hdl[index]);
10238 
10239 		if (phba->cfg_fof)
10240 			free_irq(pci_irq_vector(phba->pcidev, index),
10241 					&phba->sli4_hba.hba_eq_hdl[index]);
10242 	} else {
10243 		free_irq(phba->pcidev->irq, phba);
10244 	}
10245 
10246 	pci_free_irq_vectors(phba->pcidev);
10247 
10248 	/* Reset interrupt management states */
10249 	phba->intr_type = NONE;
10250 	phba->sli.slistat.sli_intr = 0;
10251 }
10252 
10253 /**
10254  * lpfc_unset_hba - Unset SLI3 hba device initialization
10255  * @phba: pointer to lpfc hba data structure.
10256  *
10257  * This routine is invoked to unset the HBA device initialization steps to
10258  * a device with SLI-3 interface spec.
10259  **/
10260 static void
10261 lpfc_unset_hba(struct lpfc_hba *phba)
10262 {
10263 	struct lpfc_vport *vport = phba->pport;
10264 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10265 
10266 	spin_lock_irq(shost->host_lock);
10267 	vport->load_flag |= FC_UNLOADING;
10268 	spin_unlock_irq(shost->host_lock);
10269 
10270 	kfree(phba->vpi_bmask);
10271 	kfree(phba->vpi_ids);
10272 
10273 	lpfc_stop_hba_timers(phba);
10274 
10275 	phba->pport->work_port_events = 0;
10276 
10277 	lpfc_sli_hba_down(phba);
10278 
10279 	lpfc_sli_brdrestart(phba);
10280 
10281 	lpfc_sli_disable_intr(phba);
10282 
10283 	return;
10284 }
10285 
10286 /**
10287  * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10288  * @phba: Pointer to HBA context object.
10289  *
10290  * This function is called in the SLI4 code path to wait for completion
10291  * of device's XRIs exchange busy. It will check the XRI exchange busy
10292  * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10293  * that, it will check the XRI exchange busy on outstanding FCP and ELS
10294  * I/Os every 30 seconds, log error message, and wait forever. Only when
10295  * all XRI exchange busy complete, the driver unload shall proceed with
10296  * invoking the function reset ioctl mailbox command to the CNA and the
10297  * the rest of the driver unload resource release.
10298  **/
10299 static void
10300 lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
10301 {
10302 	int wait_time = 0;
10303 	int nvme_xri_cmpl = 1;
10304 	int nvmet_xri_cmpl = 1;
10305 	int fcp_xri_cmpl = 1;
10306 	int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10307 
10308 	/* Driver just aborted IOs during the hba_unset process.  Pause
10309 	 * here to give the HBA time to complete the IO and get entries
10310 	 * into the abts lists.
10311 	 */
10312 	msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1 * 5);
10313 
10314 	/* Wait for NVME pending IO to flush back to transport. */
10315 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
10316 		lpfc_nvme_wait_for_io_drain(phba);
10317 
10318 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10319 		fcp_xri_cmpl =
10320 			list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10321 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10322 		nvme_xri_cmpl =
10323 			list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10324 		nvmet_xri_cmpl =
10325 			list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10326 	}
10327 
10328 	while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
10329 	       !nvmet_xri_cmpl) {
10330 		if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
10331 			if (!nvme_xri_cmpl)
10332 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10333 						"6100 NVME XRI exchange busy "
10334 						"wait time: %d seconds.\n",
10335 						wait_time/1000);
10336 			if (!fcp_xri_cmpl)
10337 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10338 						"2877 FCP XRI exchange busy "
10339 						"wait time: %d seconds.\n",
10340 						wait_time/1000);
10341 			if (!els_xri_cmpl)
10342 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10343 						"2878 ELS XRI exchange busy "
10344 						"wait time: %d seconds.\n",
10345 						wait_time/1000);
10346 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
10347 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
10348 		} else {
10349 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
10350 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
10351 		}
10352 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10353 			nvme_xri_cmpl = list_empty(
10354 				&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10355 			nvmet_xri_cmpl = list_empty(
10356 				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10357 		}
10358 
10359 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10360 			fcp_xri_cmpl = list_empty(
10361 				&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10362 
10363 		els_xri_cmpl =
10364 			list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10365 
10366 	}
10367 }
10368 
10369 /**
10370  * lpfc_sli4_hba_unset - Unset the fcoe hba
10371  * @phba: Pointer to HBA context object.
10372  *
10373  * This function is called in the SLI4 code path to reset the HBA's FCoE
10374  * function. The caller is not required to hold any lock. This routine
10375  * issues PCI function reset mailbox command to reset the FCoE function.
10376  * At the end of the function, it calls lpfc_hba_down_post function to
10377  * free any pending commands.
10378  **/
10379 static void
10380 lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10381 {
10382 	int wait_cnt = 0;
10383 	LPFC_MBOXQ_t *mboxq;
10384 	struct pci_dev *pdev = phba->pcidev;
10385 
10386 	lpfc_stop_hba_timers(phba);
10387 	phba->sli4_hba.intr_enable = 0;
10388 
10389 	/*
10390 	 * Gracefully wait out the potential current outstanding asynchronous
10391 	 * mailbox command.
10392 	 */
10393 
10394 	/* First, block any pending async mailbox command from posted */
10395 	spin_lock_irq(&phba->hbalock);
10396 	phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10397 	spin_unlock_irq(&phba->hbalock);
10398 	/* Now, trying to wait it out if we can */
10399 	while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10400 		msleep(10);
10401 		if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10402 			break;
10403 	}
10404 	/* Forcefully release the outstanding mailbox command if timed out */
10405 	if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10406 		spin_lock_irq(&phba->hbalock);
10407 		mboxq = phba->sli.mbox_active;
10408 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10409 		__lpfc_mbox_cmpl_put(phba, mboxq);
10410 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10411 		phba->sli.mbox_active = NULL;
10412 		spin_unlock_irq(&phba->hbalock);
10413 	}
10414 
10415 	/* Abort all iocbs associated with the hba */
10416 	lpfc_sli_hba_iocb_abort(phba);
10417 
10418 	/* Wait for completion of device XRI exchange busy */
10419 	lpfc_sli4_xri_exchange_busy_wait(phba);
10420 
10421 	/* Disable PCI subsystem interrupt */
10422 	lpfc_sli4_disable_intr(phba);
10423 
10424 	/* Disable SR-IOV if enabled */
10425 	if (phba->cfg_sriov_nr_virtfn)
10426 		pci_disable_sriov(pdev);
10427 
10428 	/* Stop kthread signal shall trigger work_done one more time */
10429 	kthread_stop(phba->worker_thread);
10430 
10431 	/* Unset the queues shared with the hardware then release all
10432 	 * allocated resources.
10433 	 */
10434 	lpfc_sli4_queue_unset(phba);
10435 	lpfc_sli4_queue_destroy(phba);
10436 
10437 	/* Reset SLI4 HBA FCoE function */
10438 	lpfc_pci_function_reset(phba);
10439 
10440 	/* Stop the SLI4 device port */
10441 	phba->pport->work_port_events = 0;
10442 }
10443 
10444  /**
10445  * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10446  * @phba: Pointer to HBA context object.
10447  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10448  *
10449  * This function is called in the SLI4 code path to read the port's
10450  * sli4 capabilities.
10451  *
10452  * This function may be be called from any context that can block-wait
10453  * for the completion.  The expectation is that this routine is called
10454  * typically from probe_one or from the online routine.
10455  **/
10456 int
10457 lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10458 {
10459 	int rc;
10460 	struct lpfc_mqe *mqe;
10461 	struct lpfc_pc_sli4_params *sli4_params;
10462 	uint32_t mbox_tmo;
10463 
10464 	rc = 0;
10465 	mqe = &mboxq->u.mqe;
10466 
10467 	/* Read the port's SLI4 Parameters port capabilities */
10468 	lpfc_pc_sli4_params(mboxq);
10469 	if (!phba->sli4_hba.intr_enable)
10470 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10471 	else {
10472 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10473 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10474 	}
10475 
10476 	if (unlikely(rc))
10477 		return 1;
10478 
10479 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10480 	sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10481 	sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10482 	sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10483 	sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10484 					     &mqe->un.sli4_params);
10485 	sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10486 					     &mqe->un.sli4_params);
10487 	sli4_params->proto_types = mqe->un.sli4_params.word3;
10488 	sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10489 	sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10490 	sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10491 	sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10492 	sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10493 	sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10494 	sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10495 	sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10496 	sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10497 	sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10498 	sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10499 	sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10500 	sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10501 	sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10502 	sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10503 	sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10504 	sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10505 	sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10506 	sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10507 	sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
10508 
10509 	/* Make sure that sge_supp_len can be handled by the driver */
10510 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10511 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10512 
10513 	return rc;
10514 }
10515 
10516 /**
10517  * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10518  * @phba: Pointer to HBA context object.
10519  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10520  *
10521  * This function is called in the SLI4 code path to read the port's
10522  * sli4 capabilities.
10523  *
10524  * This function may be be called from any context that can block-wait
10525  * for the completion.  The expectation is that this routine is called
10526  * typically from probe_one or from the online routine.
10527  **/
10528 int
10529 lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10530 {
10531 	int rc;
10532 	struct lpfc_mqe *mqe = &mboxq->u.mqe;
10533 	struct lpfc_pc_sli4_params *sli4_params;
10534 	uint32_t mbox_tmo;
10535 	int length;
10536 	struct lpfc_sli4_parameters *mbx_sli4_parameters;
10537 
10538 	/*
10539 	 * By default, the driver assumes the SLI4 port requires RPI
10540 	 * header postings.  The SLI4_PARAM response will correct this
10541 	 * assumption.
10542 	 */
10543 	phba->sli4_hba.rpi_hdrs_in_use = 1;
10544 
10545 	/* Read the port's SLI4 Config Parameters */
10546 	length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10547 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10548 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10549 			 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10550 			 length, LPFC_SLI4_MBX_EMBED);
10551 	if (!phba->sli4_hba.intr_enable)
10552 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10553 	else {
10554 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10555 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10556 	}
10557 	if (unlikely(rc))
10558 		return rc;
10559 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10560 	mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10561 	sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10562 	sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10563 	sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10564 	sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10565 					     mbx_sli4_parameters);
10566 	sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10567 					     mbx_sli4_parameters);
10568 	if (bf_get(cfg_phwq, mbx_sli4_parameters))
10569 		phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10570 	else
10571 		phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10572 	sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10573 	sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
10574 	sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
10575 	sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10576 	sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10577 	sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10578 	sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
10579 	sli4_params->eqav = bf_get(cfg_eqav, mbx_sli4_parameters);
10580 	sli4_params->cqav = bf_get(cfg_cqav, mbx_sli4_parameters);
10581 	sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
10582 	sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10583 					    mbx_sli4_parameters);
10584 	sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
10585 	sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10586 					   mbx_sli4_parameters);
10587 	phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10588 	phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
10589 	phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10590 			      bf_get(cfg_xib, mbx_sli4_parameters));
10591 
10592 	if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10593 	    !phba->nvme_support) {
10594 		phba->nvme_support = 0;
10595 		phba->nvmet_support = 0;
10596 		phba->cfg_nvmet_mrq = LPFC_NVMET_MRQ_OFF;
10597 		phba->cfg_nvme_io_channel = 0;
10598 		phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10599 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10600 				"6101 Disabling NVME support: "
10601 				"Not supported by firmware: %d %d\n",
10602 				bf_get(cfg_nvme, mbx_sli4_parameters),
10603 				bf_get(cfg_xib, mbx_sli4_parameters));
10604 
10605 		/* If firmware doesn't support NVME, just use SCSI support */
10606 		if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10607 			return -ENODEV;
10608 		phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10609 	}
10610 
10611 	/* Only embed PBDE for if_type 6 */
10612 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
10613 	    LPFC_SLI_INTF_IF_TYPE_6) {
10614 		phba->fcp_embed_pbde = 1;
10615 		phba->nvme_embed_pbde = 1;
10616 	}
10617 
10618 	/* PBDE support requires xib be set */
10619 	if (!bf_get(cfg_xib, mbx_sli4_parameters)) {
10620 		phba->fcp_embed_pbde = 0;
10621 		phba->nvme_embed_pbde = 0;
10622 	}
10623 
10624 	/*
10625 	 * To support Suppress Response feature we must satisfy 3 conditions.
10626 	 * lpfc_suppress_rsp module parameter must be set (default).
10627 	 * In SLI4-Parameters Descriptor:
10628 	 * Extended Inline Buffers (XIB) must be supported.
10629 	 * Suppress Response IU Not Supported (SRIUNS) must NOT be supported
10630 	 * (double negative).
10631 	 */
10632 	if (phba->cfg_suppress_rsp && bf_get(cfg_xib, mbx_sli4_parameters) &&
10633 	    !(bf_get(cfg_nosr, mbx_sli4_parameters)))
10634 		phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
10635 	else
10636 		phba->cfg_suppress_rsp = 0;
10637 
10638 	if (bf_get(cfg_eqdr, mbx_sli4_parameters))
10639 		phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
10640 
10641 	/* Make sure that sge_supp_len can be handled by the driver */
10642 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10643 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10644 
10645 	/*
10646 	 * Check whether the adapter supports an embedded copy of the
10647 	 * FCP CMD IU within the WQE for FCP_Ixxx commands. In order
10648 	 * to use this option, 128-byte WQEs must be used.
10649 	 */
10650 	if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10651 		phba->fcp_embed_io = 1;
10652 	else
10653 		phba->fcp_embed_io = 0;
10654 
10655 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_NVME,
10656 			"6422 XIB %d: FCP %d %d NVME %d %d %d %d\n",
10657 			bf_get(cfg_xib, mbx_sli4_parameters),
10658 			phba->fcp_embed_pbde, phba->fcp_embed_io,
10659 			phba->nvme_support, phba->nvme_embed_pbde,
10660 			phba->cfg_nvme_embed_cmd, phba->cfg_suppress_rsp);
10661 
10662 	if ((bf_get(cfg_cqpsize, mbx_sli4_parameters) & LPFC_CQ_16K_PAGE_SZ) &&
10663 	    (bf_get(cfg_wqpsize, mbx_sli4_parameters) & LPFC_WQ_16K_PAGE_SZ) &&
10664 	    (sli4_params->wqsize & LPFC_WQ_SZ128_SUPPORT))
10665 		phba->enab_exp_wqcq_pages = 1;
10666 	else
10667 		phba->enab_exp_wqcq_pages = 0;
10668 	/*
10669 	 * Check if the SLI port supports MDS Diagnostics
10670 	 */
10671 	if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10672 		phba->mds_diags_support = 1;
10673 	else
10674 		phba->mds_diags_support = 0;
10675 	return 0;
10676 }
10677 
10678 /**
10679  * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10680  * @pdev: pointer to PCI device
10681  * @pid: pointer to PCI device identifier
10682  *
10683  * This routine is to be called to attach a device with SLI-3 interface spec
10684  * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10685  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10686  * information of the device and driver to see if the driver state that it can
10687  * support this kind of device. If the match is successful, the driver core
10688  * invokes this routine. If this routine determines it can claim the HBA, it
10689  * does all the initialization that it needs to do to handle the HBA properly.
10690  *
10691  * Return code
10692  * 	0 - driver can claim the device
10693  * 	negative value - driver can not claim the device
10694  **/
10695 static int
10696 lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10697 {
10698 	struct lpfc_hba   *phba;
10699 	struct lpfc_vport *vport = NULL;
10700 	struct Scsi_Host  *shost = NULL;
10701 	int error;
10702 	uint32_t cfg_mode, intr_mode;
10703 
10704 	/* Allocate memory for HBA structure */
10705 	phba = lpfc_hba_alloc(pdev);
10706 	if (!phba)
10707 		return -ENOMEM;
10708 
10709 	/* Perform generic PCI device enabling operation */
10710 	error = lpfc_enable_pci_dev(phba);
10711 	if (error)
10712 		goto out_free_phba;
10713 
10714 	/* Set up SLI API function jump table for PCI-device group-0 HBAs */
10715 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10716 	if (error)
10717 		goto out_disable_pci_dev;
10718 
10719 	/* Set up SLI-3 specific device PCI memory space */
10720 	error = lpfc_sli_pci_mem_setup(phba);
10721 	if (error) {
10722 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10723 				"1402 Failed to set up pci memory space.\n");
10724 		goto out_disable_pci_dev;
10725 	}
10726 
10727 	/* Set up SLI-3 specific device driver resources */
10728 	error = lpfc_sli_driver_resource_setup(phba);
10729 	if (error) {
10730 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10731 				"1404 Failed to set up driver resource.\n");
10732 		goto out_unset_pci_mem_s3;
10733 	}
10734 
10735 	/* Initialize and populate the iocb list per host */
10736 
10737 	error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10738 	if (error) {
10739 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10740 				"1405 Failed to initialize iocb list.\n");
10741 		goto out_unset_driver_resource_s3;
10742 	}
10743 
10744 	/* Set up common device driver resources */
10745 	error = lpfc_setup_driver_resource_phase2(phba);
10746 	if (error) {
10747 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10748 				"1406 Failed to set up driver resource.\n");
10749 		goto out_free_iocb_list;
10750 	}
10751 
10752 	/* Get the default values for Model Name and Description */
10753 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10754 
10755 	/* Create SCSI host to the physical port */
10756 	error = lpfc_create_shost(phba);
10757 	if (error) {
10758 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10759 				"1407 Failed to create scsi host.\n");
10760 		goto out_unset_driver_resource;
10761 	}
10762 
10763 	/* Configure sysfs attributes */
10764 	vport = phba->pport;
10765 	error = lpfc_alloc_sysfs_attr(vport);
10766 	if (error) {
10767 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10768 				"1476 Failed to allocate sysfs attr\n");
10769 		goto out_destroy_shost;
10770 	}
10771 
10772 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
10773 	/* Now, trying to enable interrupt and bring up the device */
10774 	cfg_mode = phba->cfg_use_msi;
10775 	while (true) {
10776 		/* Put device to a known state before enabling interrupt */
10777 		lpfc_stop_port(phba);
10778 		/* Configure and enable interrupt */
10779 		intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10780 		if (intr_mode == LPFC_INTR_ERROR) {
10781 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10782 					"0431 Failed to enable interrupt.\n");
10783 			error = -ENODEV;
10784 			goto out_free_sysfs_attr;
10785 		}
10786 		/* SLI-3 HBA setup */
10787 		if (lpfc_sli_hba_setup(phba)) {
10788 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10789 					"1477 Failed to set up hba\n");
10790 			error = -ENODEV;
10791 			goto out_remove_device;
10792 		}
10793 
10794 		/* Wait 50ms for the interrupts of previous mailbox commands */
10795 		msleep(50);
10796 		/* Check active interrupts on message signaled interrupts */
10797 		if (intr_mode == 0 ||
10798 		    phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10799 			/* Log the current active interrupt mode */
10800 			phba->intr_mode = intr_mode;
10801 			lpfc_log_intr_mode(phba, intr_mode);
10802 			break;
10803 		} else {
10804 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10805 					"0447 Configure interrupt mode (%d) "
10806 					"failed active interrupt test.\n",
10807 					intr_mode);
10808 			/* Disable the current interrupt mode */
10809 			lpfc_sli_disable_intr(phba);
10810 			/* Try next level of interrupt mode */
10811 			cfg_mode = --intr_mode;
10812 		}
10813 	}
10814 
10815 	/* Perform post initialization setup */
10816 	lpfc_post_init_setup(phba);
10817 
10818 	/* Check if there are static vports to be created. */
10819 	lpfc_create_static_vport(phba);
10820 
10821 	return 0;
10822 
10823 out_remove_device:
10824 	lpfc_unset_hba(phba);
10825 out_free_sysfs_attr:
10826 	lpfc_free_sysfs_attr(vport);
10827 out_destroy_shost:
10828 	lpfc_destroy_shost(phba);
10829 out_unset_driver_resource:
10830 	lpfc_unset_driver_resource_phase2(phba);
10831 out_free_iocb_list:
10832 	lpfc_free_iocb_list(phba);
10833 out_unset_driver_resource_s3:
10834 	lpfc_sli_driver_resource_unset(phba);
10835 out_unset_pci_mem_s3:
10836 	lpfc_sli_pci_mem_unset(phba);
10837 out_disable_pci_dev:
10838 	lpfc_disable_pci_dev(phba);
10839 	if (shost)
10840 		scsi_host_put(shost);
10841 out_free_phba:
10842 	lpfc_hba_free(phba);
10843 	return error;
10844 }
10845 
10846 /**
10847  * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
10848  * @pdev: pointer to PCI device
10849  *
10850  * This routine is to be called to disattach a device with SLI-3 interface
10851  * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10852  * removed from PCI bus, it performs all the necessary cleanup for the HBA
10853  * device to be removed from the PCI subsystem properly.
10854  **/
10855 static void
10856 lpfc_pci_remove_one_s3(struct pci_dev *pdev)
10857 {
10858 	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
10859 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10860 	struct lpfc_vport **vports;
10861 	struct lpfc_hba   *phba = vport->phba;
10862 	int i;
10863 
10864 	spin_lock_irq(&phba->hbalock);
10865 	vport->load_flag |= FC_UNLOADING;
10866 	spin_unlock_irq(&phba->hbalock);
10867 
10868 	lpfc_free_sysfs_attr(vport);
10869 
10870 	/* Release all the vports against this physical port */
10871 	vports = lpfc_create_vport_work_array(phba);
10872 	if (vports != NULL)
10873 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10874 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10875 				continue;
10876 			fc_vport_terminate(vports[i]->fc_vport);
10877 		}
10878 	lpfc_destroy_vport_work_array(phba, vports);
10879 
10880 	/* Remove FC host and then SCSI host with the physical port */
10881 	fc_remove_host(shost);
10882 	scsi_remove_host(shost);
10883 
10884 	lpfc_cleanup(vport);
10885 
10886 	/*
10887 	 * Bring down the SLI Layer. This step disable all interrupts,
10888 	 * clears the rings, discards all mailbox commands, and resets
10889 	 * the HBA.
10890 	 */
10891 
10892 	/* HBA interrupt will be disabled after this call */
10893 	lpfc_sli_hba_down(phba);
10894 	/* Stop kthread signal shall trigger work_done one more time */
10895 	kthread_stop(phba->worker_thread);
10896 	/* Final cleanup of txcmplq and reset the HBA */
10897 	lpfc_sli_brdrestart(phba);
10898 
10899 	kfree(phba->vpi_bmask);
10900 	kfree(phba->vpi_ids);
10901 
10902 	lpfc_stop_hba_timers(phba);
10903 	spin_lock_irq(&phba->hbalock);
10904 	list_del_init(&vport->listentry);
10905 	spin_unlock_irq(&phba->hbalock);
10906 
10907 	lpfc_debugfs_terminate(vport);
10908 
10909 	/* Disable SR-IOV if enabled */
10910 	if (phba->cfg_sriov_nr_virtfn)
10911 		pci_disable_sriov(pdev);
10912 
10913 	/* Disable interrupt */
10914 	lpfc_sli_disable_intr(phba);
10915 
10916 	scsi_host_put(shost);
10917 
10918 	/*
10919 	 * Call scsi_free before mem_free since scsi bufs are released to their
10920 	 * corresponding pools here.
10921 	 */
10922 	lpfc_scsi_free(phba);
10923 	lpfc_mem_free_all(phba);
10924 
10925 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10926 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
10927 
10928 	/* Free resources associated with SLI2 interface */
10929 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
10930 			  phba->slim2p.virt, phba->slim2p.phys);
10931 
10932 	/* unmap adapter SLIM and Control Registers */
10933 	iounmap(phba->ctrl_regs_memmap_p);
10934 	iounmap(phba->slim_memmap_p);
10935 
10936 	lpfc_hba_free(phba);
10937 
10938 	pci_release_mem_regions(pdev);
10939 	pci_disable_device(pdev);
10940 }
10941 
10942 /**
10943  * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
10944  * @pdev: pointer to PCI device
10945  * @msg: power management message
10946  *
10947  * This routine is to be called from the kernel's PCI subsystem to support
10948  * system Power Management (PM) to device with SLI-3 interface spec. When
10949  * PM invokes this method, it quiesces the device by stopping the driver's
10950  * worker thread for the device, turning off device's interrupt and DMA,
10951  * and bring the device offline. Note that as the driver implements the
10952  * minimum PM requirements to a power-aware driver's PM support for the
10953  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10954  * to the suspend() method call will be treated as SUSPEND and the driver will
10955  * fully reinitialize its device during resume() method call, the driver will
10956  * set device to PCI_D3hot state in PCI config space instead of setting it
10957  * according to the @msg provided by the PM.
10958  *
10959  * Return code
10960  * 	0 - driver suspended the device
10961  * 	Error otherwise
10962  **/
10963 static int
10964 lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
10965 {
10966 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10967 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10968 
10969 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10970 			"0473 PCI device Power Management suspend.\n");
10971 
10972 	/* Bring down the device */
10973 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
10974 	lpfc_offline(phba);
10975 	kthread_stop(phba->worker_thread);
10976 
10977 	/* Disable interrupt from device */
10978 	lpfc_sli_disable_intr(phba);
10979 
10980 	/* Save device state to PCI config space */
10981 	pci_save_state(pdev);
10982 	pci_set_power_state(pdev, PCI_D3hot);
10983 
10984 	return 0;
10985 }
10986 
10987 /**
10988  * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
10989  * @pdev: pointer to PCI device
10990  *
10991  * This routine is to be called from the kernel's PCI subsystem to support
10992  * system Power Management (PM) to device with SLI-3 interface spec. When PM
10993  * invokes this method, it restores the device's PCI config space state and
10994  * fully reinitializes the device and brings it online. Note that as the
10995  * driver implements the minimum PM requirements to a power-aware driver's
10996  * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10997  * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10998  * driver will fully reinitialize its device during resume() method call,
10999  * the device will be set to PCI_D0 directly in PCI config space before
11000  * restoring the state.
11001  *
11002  * Return code
11003  * 	0 - driver suspended the device
11004  * 	Error otherwise
11005  **/
11006 static int
11007 lpfc_pci_resume_one_s3(struct pci_dev *pdev)
11008 {
11009 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11010 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11011 	uint32_t intr_mode;
11012 	int error;
11013 
11014 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11015 			"0452 PCI device Power Management resume.\n");
11016 
11017 	/* Restore device state from PCI config space */
11018 	pci_set_power_state(pdev, PCI_D0);
11019 	pci_restore_state(pdev);
11020 
11021 	/*
11022 	 * As the new kernel behavior of pci_restore_state() API call clears
11023 	 * device saved_state flag, need to save the restored state again.
11024 	 */
11025 	pci_save_state(pdev);
11026 
11027 	if (pdev->is_busmaster)
11028 		pci_set_master(pdev);
11029 
11030 	/* Startup the kernel thread for this host adapter. */
11031 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
11032 					"lpfc_worker_%d", phba->brd_no);
11033 	if (IS_ERR(phba->worker_thread)) {
11034 		error = PTR_ERR(phba->worker_thread);
11035 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11036 				"0434 PM resume failed to start worker "
11037 				"thread: error=x%x.\n", error);
11038 		return error;
11039 	}
11040 
11041 	/* Configure and enable interrupt */
11042 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
11043 	if (intr_mode == LPFC_INTR_ERROR) {
11044 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11045 				"0430 PM resume Failed to enable interrupt\n");
11046 		return -EIO;
11047 	} else
11048 		phba->intr_mode = intr_mode;
11049 
11050 	/* Restart HBA and bring it online */
11051 	lpfc_sli_brdrestart(phba);
11052 	lpfc_online(phba);
11053 
11054 	/* Log the current active interrupt mode */
11055 	lpfc_log_intr_mode(phba, phba->intr_mode);
11056 
11057 	return 0;
11058 }
11059 
11060 /**
11061  * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
11062  * @phba: pointer to lpfc hba data structure.
11063  *
11064  * This routine is called to prepare the SLI3 device for PCI slot recover. It
11065  * aborts all the outstanding SCSI I/Os to the pci device.
11066  **/
11067 static void
11068 lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
11069 {
11070 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11071 			"2723 PCI channel I/O abort preparing for recovery\n");
11072 
11073 	/*
11074 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11075 	 * and let the SCSI mid-layer to retry them to recover.
11076 	 */
11077 	lpfc_sli_abort_fcp_rings(phba);
11078 }
11079 
11080 /**
11081  * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
11082  * @phba: pointer to lpfc hba data structure.
11083  *
11084  * This routine is called to prepare the SLI3 device for PCI slot reset. It
11085  * disables the device interrupt and pci device, and aborts the internal FCP
11086  * pending I/Os.
11087  **/
11088 static void
11089 lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
11090 {
11091 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11092 			"2710 PCI channel disable preparing for reset\n");
11093 
11094 	/* Block any management I/Os to the device */
11095 	lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
11096 
11097 	/* Block all SCSI devices' I/Os on the host */
11098 	lpfc_scsi_dev_block(phba);
11099 
11100 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
11101 	lpfc_sli_flush_fcp_rings(phba);
11102 
11103 	/* stop all timers */
11104 	lpfc_stop_hba_timers(phba);
11105 
11106 	/* Disable interrupt and pci device */
11107 	lpfc_sli_disable_intr(phba);
11108 	pci_disable_device(phba->pcidev);
11109 }
11110 
11111 /**
11112  * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
11113  * @phba: pointer to lpfc hba data structure.
11114  *
11115  * This routine is called to prepare the SLI3 device for PCI slot permanently
11116  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11117  * pending I/Os.
11118  **/
11119 static void
11120 lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11121 {
11122 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11123 			"2711 PCI channel permanent disable for failure\n");
11124 	/* Block all SCSI devices' I/Os on the host */
11125 	lpfc_scsi_dev_block(phba);
11126 
11127 	/* stop all timers */
11128 	lpfc_stop_hba_timers(phba);
11129 
11130 	/* Clean up all driver's outstanding SCSI I/Os */
11131 	lpfc_sli_flush_fcp_rings(phba);
11132 }
11133 
11134 /**
11135  * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
11136  * @pdev: pointer to PCI device.
11137  * @state: the current PCI connection state.
11138  *
11139  * This routine is called from the PCI subsystem for I/O error handling to
11140  * device with SLI-3 interface spec. This function is called by the PCI
11141  * subsystem after a PCI bus error affecting this device has been detected.
11142  * When this function is invoked, it will need to stop all the I/Os and
11143  * interrupt(s) to the device. Once that is done, it will return
11144  * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
11145  * as desired.
11146  *
11147  * Return codes
11148  * 	PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
11149  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11150  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11151  **/
11152 static pci_ers_result_t
11153 lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
11154 {
11155 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11156 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11157 
11158 	switch (state) {
11159 	case pci_channel_io_normal:
11160 		/* Non-fatal error, prepare for recovery */
11161 		lpfc_sli_prep_dev_for_recover(phba);
11162 		return PCI_ERS_RESULT_CAN_RECOVER;
11163 	case pci_channel_io_frozen:
11164 		/* Fatal error, prepare for slot reset */
11165 		lpfc_sli_prep_dev_for_reset(phba);
11166 		return PCI_ERS_RESULT_NEED_RESET;
11167 	case pci_channel_io_perm_failure:
11168 		/* Permanent failure, prepare for device down */
11169 		lpfc_sli_prep_dev_for_perm_failure(phba);
11170 		return PCI_ERS_RESULT_DISCONNECT;
11171 	default:
11172 		/* Unknown state, prepare and request slot reset */
11173 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11174 				"0472 Unknown PCI error state: x%x\n", state);
11175 		lpfc_sli_prep_dev_for_reset(phba);
11176 		return PCI_ERS_RESULT_NEED_RESET;
11177 	}
11178 }
11179 
11180 /**
11181  * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
11182  * @pdev: pointer to PCI device.
11183  *
11184  * This routine is called from the PCI subsystem for error handling to
11185  * device with SLI-3 interface spec. This is called after PCI bus has been
11186  * reset to restart the PCI card from scratch, as if from a cold-boot.
11187  * During the PCI subsystem error recovery, after driver returns
11188  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11189  * recovery and then call this routine before calling the .resume method
11190  * to recover the device. This function will initialize the HBA device,
11191  * enable the interrupt, but it will just put the HBA to offline state
11192  * without passing any I/O traffic.
11193  *
11194  * Return codes
11195  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
11196  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11197  */
11198 static pci_ers_result_t
11199 lpfc_io_slot_reset_s3(struct pci_dev *pdev)
11200 {
11201 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11202 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11203 	struct lpfc_sli *psli = &phba->sli;
11204 	uint32_t intr_mode;
11205 
11206 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11207 	if (pci_enable_device_mem(pdev)) {
11208 		printk(KERN_ERR "lpfc: Cannot re-enable "
11209 			"PCI device after reset.\n");
11210 		return PCI_ERS_RESULT_DISCONNECT;
11211 	}
11212 
11213 	pci_restore_state(pdev);
11214 
11215 	/*
11216 	 * As the new kernel behavior of pci_restore_state() API call clears
11217 	 * device saved_state flag, need to save the restored state again.
11218 	 */
11219 	pci_save_state(pdev);
11220 
11221 	if (pdev->is_busmaster)
11222 		pci_set_master(pdev);
11223 
11224 	spin_lock_irq(&phba->hbalock);
11225 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11226 	spin_unlock_irq(&phba->hbalock);
11227 
11228 	/* Configure and enable interrupt */
11229 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
11230 	if (intr_mode == LPFC_INTR_ERROR) {
11231 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11232 				"0427 Cannot re-enable interrupt after "
11233 				"slot reset.\n");
11234 		return PCI_ERS_RESULT_DISCONNECT;
11235 	} else
11236 		phba->intr_mode = intr_mode;
11237 
11238 	/* Take device offline, it will perform cleanup */
11239 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11240 	lpfc_offline(phba);
11241 	lpfc_sli_brdrestart(phba);
11242 
11243 	/* Log the current active interrupt mode */
11244 	lpfc_log_intr_mode(phba, phba->intr_mode);
11245 
11246 	return PCI_ERS_RESULT_RECOVERED;
11247 }
11248 
11249 /**
11250  * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
11251  * @pdev: pointer to PCI device
11252  *
11253  * This routine is called from the PCI subsystem for error handling to device
11254  * with SLI-3 interface spec. It is called when kernel error recovery tells
11255  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11256  * error recovery. After this call, traffic can start to flow from this device
11257  * again.
11258  */
11259 static void
11260 lpfc_io_resume_s3(struct pci_dev *pdev)
11261 {
11262 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11263 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11264 
11265 	/* Bring device online, it will be no-op for non-fatal error resume */
11266 	lpfc_online(phba);
11267 
11268 	/* Clean up Advanced Error Reporting (AER) if needed */
11269 	if (phba->hba_flag & HBA_AER_ENABLED)
11270 		pci_cleanup_aer_uncorrect_error_status(pdev);
11271 }
11272 
11273 /**
11274  * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
11275  * @phba: pointer to lpfc hba data structure.
11276  *
11277  * returns the number of ELS/CT IOCBs to reserve
11278  **/
11279 int
11280 lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
11281 {
11282 	int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
11283 
11284 	if (phba->sli_rev == LPFC_SLI_REV4) {
11285 		if (max_xri <= 100)
11286 			return 10;
11287 		else if (max_xri <= 256)
11288 			return 25;
11289 		else if (max_xri <= 512)
11290 			return 50;
11291 		else if (max_xri <= 1024)
11292 			return 100;
11293 		else if (max_xri <= 1536)
11294 			return 150;
11295 		else if (max_xri <= 2048)
11296 			return 200;
11297 		else
11298 			return 250;
11299 	} else
11300 		return 0;
11301 }
11302 
11303 /**
11304  * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
11305  * @phba: pointer to lpfc hba data structure.
11306  *
11307  * returns the number of ELS/CT + NVMET IOCBs to reserve
11308  **/
11309 int
11310 lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
11311 {
11312 	int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
11313 
11314 	if (phba->nvmet_support)
11315 		max_xri += LPFC_NVMET_BUF_POST;
11316 	return max_xri;
11317 }
11318 
11319 
11320 static void
11321 lpfc_log_write_firmware_error(struct lpfc_hba *phba, uint32_t offset,
11322 	uint32_t magic_number, uint32_t ftype, uint32_t fid, uint32_t fsize,
11323 	const struct firmware *fw)
11324 {
11325 	if (offset == ADD_STATUS_FW_NOT_SUPPORTED)
11326 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11327 			"3030 This firmware version is not supported on "
11328 			"this HBA model. Device:%x Magic:%x Type:%x "
11329 			"ID:%x Size %d %zd\n",
11330 			phba->pcidev->device, magic_number, ftype, fid,
11331 			fsize, fw->size);
11332 	else
11333 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11334 			"3022 FW Download failed. Device:%x Magic:%x Type:%x "
11335 			"ID:%x Size %d %zd\n",
11336 			phba->pcidev->device, magic_number, ftype, fid,
11337 			fsize, fw->size);
11338 }
11339 
11340 
11341 /**
11342  * lpfc_write_firmware - attempt to write a firmware image to the port
11343  * @fw: pointer to firmware image returned from request_firmware.
11344  * @phba: pointer to lpfc hba data structure.
11345  *
11346  **/
11347 static void
11348 lpfc_write_firmware(const struct firmware *fw, void *context)
11349 {
11350 	struct lpfc_hba *phba = (struct lpfc_hba *)context;
11351 	char fwrev[FW_REV_STR_SIZE];
11352 	struct lpfc_grp_hdr *image;
11353 	struct list_head dma_buffer_list;
11354 	int i, rc = 0;
11355 	struct lpfc_dmabuf *dmabuf, *next;
11356 	uint32_t offset = 0, temp_offset = 0;
11357 	uint32_t magic_number, ftype, fid, fsize;
11358 
11359 	/* It can be null in no-wait mode, sanity check */
11360 	if (!fw) {
11361 		rc = -ENXIO;
11362 		goto out;
11363 	}
11364 	image = (struct lpfc_grp_hdr *)fw->data;
11365 
11366 	magic_number = be32_to_cpu(image->magic_number);
11367 	ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
11368 	fid = bf_get_be32(lpfc_grp_hdr_id, image);
11369 	fsize = be32_to_cpu(image->size);
11370 
11371 	INIT_LIST_HEAD(&dma_buffer_list);
11372 	lpfc_decode_firmware_rev(phba, fwrev, 1);
11373 	if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
11374 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11375 				"3023 Updating Firmware, Current Version:%s "
11376 				"New Version:%s\n",
11377 				fwrev, image->revision);
11378 		for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
11379 			dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
11380 					 GFP_KERNEL);
11381 			if (!dmabuf) {
11382 				rc = -ENOMEM;
11383 				goto release_out;
11384 			}
11385 			dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11386 							  SLI4_PAGE_SIZE,
11387 							  &dmabuf->phys,
11388 							  GFP_KERNEL);
11389 			if (!dmabuf->virt) {
11390 				kfree(dmabuf);
11391 				rc = -ENOMEM;
11392 				goto release_out;
11393 			}
11394 			list_add_tail(&dmabuf->list, &dma_buffer_list);
11395 		}
11396 		while (offset < fw->size) {
11397 			temp_offset = offset;
11398 			list_for_each_entry(dmabuf, &dma_buffer_list, list) {
11399 				if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
11400 					memcpy(dmabuf->virt,
11401 					       fw->data + temp_offset,
11402 					       fw->size - temp_offset);
11403 					temp_offset = fw->size;
11404 					break;
11405 				}
11406 				memcpy(dmabuf->virt, fw->data + temp_offset,
11407 				       SLI4_PAGE_SIZE);
11408 				temp_offset += SLI4_PAGE_SIZE;
11409 			}
11410 			rc = lpfc_wr_object(phba, &dma_buffer_list,
11411 				    (fw->size - offset), &offset);
11412 			if (rc) {
11413 				lpfc_log_write_firmware_error(phba, offset,
11414 					magic_number, ftype, fid, fsize, fw);
11415 				goto release_out;
11416 			}
11417 		}
11418 		rc = offset;
11419 	} else
11420 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11421 				"3029 Skipped Firmware update, Current "
11422 				"Version:%s New Version:%s\n",
11423 				fwrev, image->revision);
11424 
11425 release_out:
11426 	list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
11427 		list_del(&dmabuf->list);
11428 		dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
11429 				  dmabuf->virt, dmabuf->phys);
11430 		kfree(dmabuf);
11431 	}
11432 	release_firmware(fw);
11433 out:
11434 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11435 			"3024 Firmware update done: %d.\n", rc);
11436 	return;
11437 }
11438 
11439 /**
11440  * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11441  * @phba: pointer to lpfc hba data structure.
11442  *
11443  * This routine is called to perform Linux generic firmware upgrade on device
11444  * that supports such feature.
11445  **/
11446 int
11447 lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11448 {
11449 	uint8_t file_name[ELX_MODEL_NAME_SIZE];
11450 	int ret;
11451 	const struct firmware *fw;
11452 
11453 	/* Only supported on SLI4 interface type 2 for now */
11454 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
11455 	    LPFC_SLI_INTF_IF_TYPE_2)
11456 		return -EPERM;
11457 
11458 	snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11459 
11460 	if (fw_upgrade == INT_FW_UPGRADE) {
11461 		ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11462 					file_name, &phba->pcidev->dev,
11463 					GFP_KERNEL, (void *)phba,
11464 					lpfc_write_firmware);
11465 	} else if (fw_upgrade == RUN_FW_UPGRADE) {
11466 		ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11467 		if (!ret)
11468 			lpfc_write_firmware(fw, (void *)phba);
11469 	} else {
11470 		ret = -EINVAL;
11471 	}
11472 
11473 	return ret;
11474 }
11475 
11476 /**
11477  * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11478  * @pdev: pointer to PCI device
11479  * @pid: pointer to PCI device identifier
11480  *
11481  * This routine is called from the kernel's PCI subsystem to device with
11482  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11483  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11484  * information of the device and driver to see if the driver state that it
11485  * can support this kind of device. If the match is successful, the driver
11486  * core invokes this routine. If this routine determines it can claim the HBA,
11487  * it does all the initialization that it needs to do to handle the HBA
11488  * properly.
11489  *
11490  * Return code
11491  * 	0 - driver can claim the device
11492  * 	negative value - driver can not claim the device
11493  **/
11494 static int
11495 lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11496 {
11497 	struct lpfc_hba   *phba;
11498 	struct lpfc_vport *vport = NULL;
11499 	struct Scsi_Host  *shost = NULL;
11500 	int error;
11501 	uint32_t cfg_mode, intr_mode;
11502 
11503 	/* Allocate memory for HBA structure */
11504 	phba = lpfc_hba_alloc(pdev);
11505 	if (!phba)
11506 		return -ENOMEM;
11507 
11508 	/* Perform generic PCI device enabling operation */
11509 	error = lpfc_enable_pci_dev(phba);
11510 	if (error)
11511 		goto out_free_phba;
11512 
11513 	/* Set up SLI API function jump table for PCI-device group-1 HBAs */
11514 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11515 	if (error)
11516 		goto out_disable_pci_dev;
11517 
11518 	/* Set up SLI-4 specific device PCI memory space */
11519 	error = lpfc_sli4_pci_mem_setup(phba);
11520 	if (error) {
11521 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11522 				"1410 Failed to set up pci memory space.\n");
11523 		goto out_disable_pci_dev;
11524 	}
11525 
11526 	/* Set up SLI-4 Specific device driver resources */
11527 	error = lpfc_sli4_driver_resource_setup(phba);
11528 	if (error) {
11529 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11530 				"1412 Failed to set up driver resource.\n");
11531 		goto out_unset_pci_mem_s4;
11532 	}
11533 
11534 	INIT_LIST_HEAD(&phba->active_rrq_list);
11535 	INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
11536 
11537 	/* Set up common device driver resources */
11538 	error = lpfc_setup_driver_resource_phase2(phba);
11539 	if (error) {
11540 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11541 				"1414 Failed to set up driver resource.\n");
11542 		goto out_unset_driver_resource_s4;
11543 	}
11544 
11545 	/* Get the default values for Model Name and Description */
11546 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11547 
11548 	/* Create SCSI host to the physical port */
11549 	error = lpfc_create_shost(phba);
11550 	if (error) {
11551 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11552 				"1415 Failed to create scsi host.\n");
11553 		goto out_unset_driver_resource;
11554 	}
11555 
11556 	/* Configure sysfs attributes */
11557 	vport = phba->pport;
11558 	error = lpfc_alloc_sysfs_attr(vport);
11559 	if (error) {
11560 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11561 				"1416 Failed to allocate sysfs attr\n");
11562 		goto out_destroy_shost;
11563 	}
11564 
11565 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
11566 	/* Now, trying to enable interrupt and bring up the device */
11567 	cfg_mode = phba->cfg_use_msi;
11568 
11569 	/* Put device to a known state before enabling interrupt */
11570 	lpfc_stop_port(phba);
11571 
11572 	/* Configure and enable interrupt */
11573 	intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11574 	if (intr_mode == LPFC_INTR_ERROR) {
11575 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11576 				"0426 Failed to enable interrupt.\n");
11577 		error = -ENODEV;
11578 		goto out_free_sysfs_attr;
11579 	}
11580 	/* Default to single EQ for non-MSI-X */
11581 	if (phba->intr_type != MSIX) {
11582 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11583 			phba->cfg_fcp_io_channel = 1;
11584 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11585 			phba->cfg_nvme_io_channel = 1;
11586 			if (phba->nvmet_support)
11587 				phba->cfg_nvmet_mrq = 1;
11588 		}
11589 		phba->io_channel_irqs = 1;
11590 	}
11591 
11592 	/* Set up SLI-4 HBA */
11593 	if (lpfc_sli4_hba_setup(phba)) {
11594 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11595 				"1421 Failed to set up hba\n");
11596 		error = -ENODEV;
11597 		goto out_disable_intr;
11598 	}
11599 
11600 	/* Log the current active interrupt mode */
11601 	phba->intr_mode = intr_mode;
11602 	lpfc_log_intr_mode(phba, intr_mode);
11603 
11604 	/* Perform post initialization setup */
11605 	lpfc_post_init_setup(phba);
11606 
11607 	/* NVME support in FW earlier in the driver load corrects the
11608 	 * FC4 type making a check for nvme_support unnecessary.
11609 	 */
11610 	if ((phba->nvmet_support == 0) &&
11611 	    (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11612 		/* Create NVME binding with nvme_fc_transport. This
11613 		 * ensures the vport is initialized.  If the localport
11614 		 * create fails, it should not unload the driver to
11615 		 * support field issues.
11616 		 */
11617 		error = lpfc_nvme_create_localport(vport);
11618 		if (error) {
11619 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11620 					"6004 NVME registration failed, "
11621 					"error x%x\n",
11622 					error);
11623 		}
11624 	}
11625 
11626 	/* check for firmware upgrade or downgrade */
11627 	if (phba->cfg_request_firmware_upgrade)
11628 		lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
11629 
11630 	/* Check if there are static vports to be created. */
11631 	lpfc_create_static_vport(phba);
11632 	return 0;
11633 
11634 out_disable_intr:
11635 	lpfc_sli4_disable_intr(phba);
11636 out_free_sysfs_attr:
11637 	lpfc_free_sysfs_attr(vport);
11638 out_destroy_shost:
11639 	lpfc_destroy_shost(phba);
11640 out_unset_driver_resource:
11641 	lpfc_unset_driver_resource_phase2(phba);
11642 out_unset_driver_resource_s4:
11643 	lpfc_sli4_driver_resource_unset(phba);
11644 out_unset_pci_mem_s4:
11645 	lpfc_sli4_pci_mem_unset(phba);
11646 out_disable_pci_dev:
11647 	lpfc_disable_pci_dev(phba);
11648 	if (shost)
11649 		scsi_host_put(shost);
11650 out_free_phba:
11651 	lpfc_hba_free(phba);
11652 	return error;
11653 }
11654 
11655 /**
11656  * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11657  * @pdev: pointer to PCI device
11658  *
11659  * This routine is called from the kernel's PCI subsystem to device with
11660  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11661  * removed from PCI bus, it performs all the necessary cleanup for the HBA
11662  * device to be removed from the PCI subsystem properly.
11663  **/
11664 static void
11665 lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11666 {
11667 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11668 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11669 	struct lpfc_vport **vports;
11670 	struct lpfc_hba *phba = vport->phba;
11671 	int i;
11672 
11673 	/* Mark the device unloading flag */
11674 	spin_lock_irq(&phba->hbalock);
11675 	vport->load_flag |= FC_UNLOADING;
11676 	spin_unlock_irq(&phba->hbalock);
11677 
11678 	/* Free the HBA sysfs attributes */
11679 	lpfc_free_sysfs_attr(vport);
11680 
11681 	/* Release all the vports against this physical port */
11682 	vports = lpfc_create_vport_work_array(phba);
11683 	if (vports != NULL)
11684 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11685 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11686 				continue;
11687 			fc_vport_terminate(vports[i]->fc_vport);
11688 		}
11689 	lpfc_destroy_vport_work_array(phba, vports);
11690 
11691 	/* Remove FC host and then SCSI host with the physical port */
11692 	fc_remove_host(shost);
11693 	scsi_remove_host(shost);
11694 
11695 	/* Perform ndlp cleanup on the physical port.  The nvme and nvmet
11696 	 * localports are destroyed after to cleanup all transport memory.
11697 	 */
11698 	lpfc_cleanup(vport);
11699 	lpfc_nvmet_destroy_targetport(phba);
11700 	lpfc_nvme_destroy_localport(vport);
11701 
11702 	/*
11703 	 * Bring down the SLI Layer. This step disables all interrupts,
11704 	 * clears the rings, discards all mailbox commands, and resets
11705 	 * the HBA FCoE function.
11706 	 */
11707 	lpfc_debugfs_terminate(vport);
11708 	lpfc_sli4_hba_unset(phba);
11709 
11710 	lpfc_stop_hba_timers(phba);
11711 	spin_lock_irq(&phba->hbalock);
11712 	list_del_init(&vport->listentry);
11713 	spin_unlock_irq(&phba->hbalock);
11714 
11715 	/* Perform scsi free before driver resource_unset since scsi
11716 	 * buffers are released to their corresponding pools here.
11717 	 */
11718 	lpfc_scsi_free(phba);
11719 	lpfc_nvme_free(phba);
11720 	lpfc_free_iocb_list(phba);
11721 
11722 	lpfc_sli4_driver_resource_unset(phba);
11723 
11724 	/* Unmap adapter Control and Doorbell registers */
11725 	lpfc_sli4_pci_mem_unset(phba);
11726 
11727 	/* Release PCI resources and disable device's PCI function */
11728 	scsi_host_put(shost);
11729 	lpfc_disable_pci_dev(phba);
11730 
11731 	/* Finally, free the driver's device data structure */
11732 	lpfc_hba_free(phba);
11733 
11734 	return;
11735 }
11736 
11737 /**
11738  * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11739  * @pdev: pointer to PCI device
11740  * @msg: power management message
11741  *
11742  * This routine is called from the kernel's PCI subsystem to support system
11743  * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11744  * this method, it quiesces the device by stopping the driver's worker
11745  * thread for the device, turning off device's interrupt and DMA, and bring
11746  * the device offline. Note that as the driver implements the minimum PM
11747  * requirements to a power-aware driver's PM support for suspend/resume -- all
11748  * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11749  * method call will be treated as SUSPEND and the driver will fully
11750  * reinitialize its device during resume() method call, the driver will set
11751  * device to PCI_D3hot state in PCI config space instead of setting it
11752  * according to the @msg provided by the PM.
11753  *
11754  * Return code
11755  * 	0 - driver suspended the device
11756  * 	Error otherwise
11757  **/
11758 static int
11759 lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11760 {
11761 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11762 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11763 
11764 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11765 			"2843 PCI device Power Management suspend.\n");
11766 
11767 	/* Bring down the device */
11768 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11769 	lpfc_offline(phba);
11770 	kthread_stop(phba->worker_thread);
11771 
11772 	/* Disable interrupt from device */
11773 	lpfc_sli4_disable_intr(phba);
11774 	lpfc_sli4_queue_destroy(phba);
11775 
11776 	/* Save device state to PCI config space */
11777 	pci_save_state(pdev);
11778 	pci_set_power_state(pdev, PCI_D3hot);
11779 
11780 	return 0;
11781 }
11782 
11783 /**
11784  * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11785  * @pdev: pointer to PCI device
11786  *
11787  * This routine is called from the kernel's PCI subsystem to support system
11788  * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11789  * this method, it restores the device's PCI config space state and fully
11790  * reinitializes the device and brings it online. Note that as the driver
11791  * implements the minimum PM requirements to a power-aware driver's PM for
11792  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11793  * to the suspend() method call will be treated as SUSPEND and the driver
11794  * will fully reinitialize its device during resume() method call, the device
11795  * will be set to PCI_D0 directly in PCI config space before restoring the
11796  * state.
11797  *
11798  * Return code
11799  * 	0 - driver suspended the device
11800  * 	Error otherwise
11801  **/
11802 static int
11803 lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11804 {
11805 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11806 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11807 	uint32_t intr_mode;
11808 	int error;
11809 
11810 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11811 			"0292 PCI device Power Management resume.\n");
11812 
11813 	/* Restore device state from PCI config space */
11814 	pci_set_power_state(pdev, PCI_D0);
11815 	pci_restore_state(pdev);
11816 
11817 	/*
11818 	 * As the new kernel behavior of pci_restore_state() API call clears
11819 	 * device saved_state flag, need to save the restored state again.
11820 	 */
11821 	pci_save_state(pdev);
11822 
11823 	if (pdev->is_busmaster)
11824 		pci_set_master(pdev);
11825 
11826 	 /* Startup the kernel thread for this host adapter. */
11827 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
11828 					"lpfc_worker_%d", phba->brd_no);
11829 	if (IS_ERR(phba->worker_thread)) {
11830 		error = PTR_ERR(phba->worker_thread);
11831 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11832 				"0293 PM resume failed to start worker "
11833 				"thread: error=x%x.\n", error);
11834 		return error;
11835 	}
11836 
11837 	/* Configure and enable interrupt */
11838 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11839 	if (intr_mode == LPFC_INTR_ERROR) {
11840 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11841 				"0294 PM resume Failed to enable interrupt\n");
11842 		return -EIO;
11843 	} else
11844 		phba->intr_mode = intr_mode;
11845 
11846 	/* Restart HBA and bring it online */
11847 	lpfc_sli_brdrestart(phba);
11848 	lpfc_online(phba);
11849 
11850 	/* Log the current active interrupt mode */
11851 	lpfc_log_intr_mode(phba, phba->intr_mode);
11852 
11853 	return 0;
11854 }
11855 
11856 /**
11857  * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11858  * @phba: pointer to lpfc hba data structure.
11859  *
11860  * This routine is called to prepare the SLI4 device for PCI slot recover. It
11861  * aborts all the outstanding SCSI I/Os to the pci device.
11862  **/
11863 static void
11864 lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11865 {
11866 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11867 			"2828 PCI channel I/O abort preparing for recovery\n");
11868 	/*
11869 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11870 	 * and let the SCSI mid-layer to retry them to recover.
11871 	 */
11872 	lpfc_sli_abort_fcp_rings(phba);
11873 }
11874 
11875 /**
11876  * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11877  * @phba: pointer to lpfc hba data structure.
11878  *
11879  * This routine is called to prepare the SLI4 device for PCI slot reset. It
11880  * disables the device interrupt and pci device, and aborts the internal FCP
11881  * pending I/Os.
11882  **/
11883 static void
11884 lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11885 {
11886 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11887 			"2826 PCI channel disable preparing for reset\n");
11888 
11889 	/* Block any management I/Os to the device */
11890 	lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
11891 
11892 	/* Block all SCSI devices' I/Os on the host */
11893 	lpfc_scsi_dev_block(phba);
11894 
11895 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
11896 	lpfc_sli_flush_fcp_rings(phba);
11897 
11898 	/* Flush the outstanding NVME IOs if fc4 type enabled. */
11899 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11900 		lpfc_sli_flush_nvme_rings(phba);
11901 
11902 	/* stop all timers */
11903 	lpfc_stop_hba_timers(phba);
11904 
11905 	/* Disable interrupt and pci device */
11906 	lpfc_sli4_disable_intr(phba);
11907 	lpfc_sli4_queue_destroy(phba);
11908 	pci_disable_device(phba->pcidev);
11909 }
11910 
11911 /**
11912  * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11913  * @phba: pointer to lpfc hba data structure.
11914  *
11915  * This routine is called to prepare the SLI4 device for PCI slot permanently
11916  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11917  * pending I/Os.
11918  **/
11919 static void
11920 lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11921 {
11922 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11923 			"2827 PCI channel permanent disable for failure\n");
11924 
11925 	/* Block all SCSI devices' I/Os on the host */
11926 	lpfc_scsi_dev_block(phba);
11927 
11928 	/* stop all timers */
11929 	lpfc_stop_hba_timers(phba);
11930 
11931 	/* Clean up all driver's outstanding SCSI I/Os */
11932 	lpfc_sli_flush_fcp_rings(phba);
11933 
11934 	/* Flush the outstanding NVME IOs if fc4 type enabled. */
11935 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
11936 		lpfc_sli_flush_nvme_rings(phba);
11937 }
11938 
11939 /**
11940  * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
11941  * @pdev: pointer to PCI device.
11942  * @state: the current PCI connection state.
11943  *
11944  * This routine is called from the PCI subsystem for error handling to device
11945  * with SLI-4 interface spec. This function is called by the PCI subsystem
11946  * after a PCI bus error affecting this device has been detected. When this
11947  * function is invoked, it will need to stop all the I/Os and interrupt(s)
11948  * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
11949  * for the PCI subsystem to perform proper recovery as desired.
11950  *
11951  * Return codes
11952  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11953  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11954  **/
11955 static pci_ers_result_t
11956 lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11957 {
11958 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11959 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11960 
11961 	switch (state) {
11962 	case pci_channel_io_normal:
11963 		/* Non-fatal error, prepare for recovery */
11964 		lpfc_sli4_prep_dev_for_recover(phba);
11965 		return PCI_ERS_RESULT_CAN_RECOVER;
11966 	case pci_channel_io_frozen:
11967 		/* Fatal error, prepare for slot reset */
11968 		lpfc_sli4_prep_dev_for_reset(phba);
11969 		return PCI_ERS_RESULT_NEED_RESET;
11970 	case pci_channel_io_perm_failure:
11971 		/* Permanent failure, prepare for device down */
11972 		lpfc_sli4_prep_dev_for_perm_failure(phba);
11973 		return PCI_ERS_RESULT_DISCONNECT;
11974 	default:
11975 		/* Unknown state, prepare and request slot reset */
11976 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11977 				"2825 Unknown PCI error state: x%x\n", state);
11978 		lpfc_sli4_prep_dev_for_reset(phba);
11979 		return PCI_ERS_RESULT_NEED_RESET;
11980 	}
11981 }
11982 
11983 /**
11984  * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11985  * @pdev: pointer to PCI device.
11986  *
11987  * This routine is called from the PCI subsystem for error handling to device
11988  * with SLI-4 interface spec. It is called after PCI bus has been reset to
11989  * restart the PCI card from scratch, as if from a cold-boot. During the
11990  * PCI subsystem error recovery, after the driver returns
11991  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11992  * recovery and then call this routine before calling the .resume method to
11993  * recover the device. This function will initialize the HBA device, enable
11994  * the interrupt, but it will just put the HBA to offline state without
11995  * passing any I/O traffic.
11996  *
11997  * Return codes
11998  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
11999  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12000  */
12001 static pci_ers_result_t
12002 lpfc_io_slot_reset_s4(struct pci_dev *pdev)
12003 {
12004 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12005 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12006 	struct lpfc_sli *psli = &phba->sli;
12007 	uint32_t intr_mode;
12008 
12009 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
12010 	if (pci_enable_device_mem(pdev)) {
12011 		printk(KERN_ERR "lpfc: Cannot re-enable "
12012 			"PCI device after reset.\n");
12013 		return PCI_ERS_RESULT_DISCONNECT;
12014 	}
12015 
12016 	pci_restore_state(pdev);
12017 
12018 	/*
12019 	 * As the new kernel behavior of pci_restore_state() API call clears
12020 	 * device saved_state flag, need to save the restored state again.
12021 	 */
12022 	pci_save_state(pdev);
12023 
12024 	if (pdev->is_busmaster)
12025 		pci_set_master(pdev);
12026 
12027 	spin_lock_irq(&phba->hbalock);
12028 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
12029 	spin_unlock_irq(&phba->hbalock);
12030 
12031 	/* Configure and enable interrupt */
12032 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
12033 	if (intr_mode == LPFC_INTR_ERROR) {
12034 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12035 				"2824 Cannot re-enable interrupt after "
12036 				"slot reset.\n");
12037 		return PCI_ERS_RESULT_DISCONNECT;
12038 	} else
12039 		phba->intr_mode = intr_mode;
12040 
12041 	/* Log the current active interrupt mode */
12042 	lpfc_log_intr_mode(phba, phba->intr_mode);
12043 
12044 	return PCI_ERS_RESULT_RECOVERED;
12045 }
12046 
12047 /**
12048  * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
12049  * @pdev: pointer to PCI device
12050  *
12051  * This routine is called from the PCI subsystem for error handling to device
12052  * with SLI-4 interface spec. It is called when kernel error recovery tells
12053  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
12054  * error recovery. After this call, traffic can start to flow from this device
12055  * again.
12056  **/
12057 static void
12058 lpfc_io_resume_s4(struct pci_dev *pdev)
12059 {
12060 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12061 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12062 
12063 	/*
12064 	 * In case of slot reset, as function reset is performed through
12065 	 * mailbox command which needs DMA to be enabled, this operation
12066 	 * has to be moved to the io resume phase. Taking device offline
12067 	 * will perform the necessary cleanup.
12068 	 */
12069 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
12070 		/* Perform device reset */
12071 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
12072 		lpfc_offline(phba);
12073 		lpfc_sli_brdrestart(phba);
12074 		/* Bring the device back online */
12075 		lpfc_online(phba);
12076 	}
12077 
12078 	/* Clean up Advanced Error Reporting (AER) if needed */
12079 	if (phba->hba_flag & HBA_AER_ENABLED)
12080 		pci_cleanup_aer_uncorrect_error_status(pdev);
12081 }
12082 
12083 /**
12084  * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
12085  * @pdev: pointer to PCI device
12086  * @pid: pointer to PCI device identifier
12087  *
12088  * This routine is to be registered to the kernel's PCI subsystem. When an
12089  * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
12090  * at PCI device-specific information of the device and driver to see if the
12091  * driver state that it can support this kind of device. If the match is
12092  * successful, the driver core invokes this routine. This routine dispatches
12093  * the action to the proper SLI-3 or SLI-4 device probing routine, which will
12094  * do all the initialization that it needs to do to handle the HBA device
12095  * properly.
12096  *
12097  * Return code
12098  * 	0 - driver can claim the device
12099  * 	negative value - driver can not claim the device
12100  **/
12101 static int
12102 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
12103 {
12104 	int rc;
12105 	struct lpfc_sli_intf intf;
12106 
12107 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
12108 		return -ENODEV;
12109 
12110 	if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
12111 	    (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
12112 		rc = lpfc_pci_probe_one_s4(pdev, pid);
12113 	else
12114 		rc = lpfc_pci_probe_one_s3(pdev, pid);
12115 
12116 	return rc;
12117 }
12118 
12119 /**
12120  * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
12121  * @pdev: pointer to PCI device
12122  *
12123  * This routine is to be registered to the kernel's PCI subsystem. When an
12124  * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
12125  * This routine dispatches the action to the proper SLI-3 or SLI-4 device
12126  * remove routine, which will perform all the necessary cleanup for the
12127  * device to be removed from the PCI subsystem properly.
12128  **/
12129 static void
12130 lpfc_pci_remove_one(struct pci_dev *pdev)
12131 {
12132 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12133 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12134 
12135 	switch (phba->pci_dev_grp) {
12136 	case LPFC_PCI_DEV_LP:
12137 		lpfc_pci_remove_one_s3(pdev);
12138 		break;
12139 	case LPFC_PCI_DEV_OC:
12140 		lpfc_pci_remove_one_s4(pdev);
12141 		break;
12142 	default:
12143 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12144 				"1424 Invalid PCI device group: 0x%x\n",
12145 				phba->pci_dev_grp);
12146 		break;
12147 	}
12148 	return;
12149 }
12150 
12151 /**
12152  * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
12153  * @pdev: pointer to PCI device
12154  * @msg: power management message
12155  *
12156  * This routine is to be registered to the kernel's PCI subsystem to support
12157  * system Power Management (PM). When PM invokes this method, it dispatches
12158  * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
12159  * suspend the device.
12160  *
12161  * Return code
12162  * 	0 - driver suspended the device
12163  * 	Error otherwise
12164  **/
12165 static int
12166 lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
12167 {
12168 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12169 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12170 	int rc = -ENODEV;
12171 
12172 	switch (phba->pci_dev_grp) {
12173 	case LPFC_PCI_DEV_LP:
12174 		rc = lpfc_pci_suspend_one_s3(pdev, msg);
12175 		break;
12176 	case LPFC_PCI_DEV_OC:
12177 		rc = lpfc_pci_suspend_one_s4(pdev, msg);
12178 		break;
12179 	default:
12180 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12181 				"1425 Invalid PCI device group: 0x%x\n",
12182 				phba->pci_dev_grp);
12183 		break;
12184 	}
12185 	return rc;
12186 }
12187 
12188 /**
12189  * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
12190  * @pdev: pointer to PCI device
12191  *
12192  * This routine is to be registered to the kernel's PCI subsystem to support
12193  * system Power Management (PM). When PM invokes this method, it dispatches
12194  * the action to the proper SLI-3 or SLI-4 device resume routine, which will
12195  * resume the device.
12196  *
12197  * Return code
12198  * 	0 - driver suspended the device
12199  * 	Error otherwise
12200  **/
12201 static int
12202 lpfc_pci_resume_one(struct pci_dev *pdev)
12203 {
12204 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12205 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12206 	int rc = -ENODEV;
12207 
12208 	switch (phba->pci_dev_grp) {
12209 	case LPFC_PCI_DEV_LP:
12210 		rc = lpfc_pci_resume_one_s3(pdev);
12211 		break;
12212 	case LPFC_PCI_DEV_OC:
12213 		rc = lpfc_pci_resume_one_s4(pdev);
12214 		break;
12215 	default:
12216 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12217 				"1426 Invalid PCI device group: 0x%x\n",
12218 				phba->pci_dev_grp);
12219 		break;
12220 	}
12221 	return rc;
12222 }
12223 
12224 /**
12225  * lpfc_io_error_detected - lpfc method for handling PCI I/O error
12226  * @pdev: pointer to PCI device.
12227  * @state: the current PCI connection state.
12228  *
12229  * This routine is registered to the PCI subsystem for error handling. This
12230  * function is called by the PCI subsystem after a PCI bus error affecting
12231  * this device has been detected. When this routine is invoked, it dispatches
12232  * the action to the proper SLI-3 or SLI-4 device error detected handling
12233  * routine, which will perform the proper error detected operation.
12234  *
12235  * Return codes
12236  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
12237  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12238  **/
12239 static pci_ers_result_t
12240 lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
12241 {
12242 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12243 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12244 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12245 
12246 	switch (phba->pci_dev_grp) {
12247 	case LPFC_PCI_DEV_LP:
12248 		rc = lpfc_io_error_detected_s3(pdev, state);
12249 		break;
12250 	case LPFC_PCI_DEV_OC:
12251 		rc = lpfc_io_error_detected_s4(pdev, state);
12252 		break;
12253 	default:
12254 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12255 				"1427 Invalid PCI device group: 0x%x\n",
12256 				phba->pci_dev_grp);
12257 		break;
12258 	}
12259 	return rc;
12260 }
12261 
12262 /**
12263  * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
12264  * @pdev: pointer to PCI device.
12265  *
12266  * This routine is registered to the PCI subsystem for error handling. This
12267  * function is called after PCI bus has been reset to restart the PCI card
12268  * from scratch, as if from a cold-boot. When this routine is invoked, it
12269  * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
12270  * routine, which will perform the proper device reset.
12271  *
12272  * Return codes
12273  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
12274  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
12275  **/
12276 static pci_ers_result_t
12277 lpfc_io_slot_reset(struct pci_dev *pdev)
12278 {
12279 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12280 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12281 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
12282 
12283 	switch (phba->pci_dev_grp) {
12284 	case LPFC_PCI_DEV_LP:
12285 		rc = lpfc_io_slot_reset_s3(pdev);
12286 		break;
12287 	case LPFC_PCI_DEV_OC:
12288 		rc = lpfc_io_slot_reset_s4(pdev);
12289 		break;
12290 	default:
12291 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12292 				"1428 Invalid PCI device group: 0x%x\n",
12293 				phba->pci_dev_grp);
12294 		break;
12295 	}
12296 	return rc;
12297 }
12298 
12299 /**
12300  * lpfc_io_resume - lpfc method for resuming PCI I/O operation
12301  * @pdev: pointer to PCI device
12302  *
12303  * This routine is registered to the PCI subsystem for error handling. It
12304  * is called when kernel error recovery tells the lpfc driver that it is
12305  * OK to resume normal PCI operation after PCI bus error recovery. When
12306  * this routine is invoked, it dispatches the action to the proper SLI-3
12307  * or SLI-4 device io_resume routine, which will resume the device operation.
12308  **/
12309 static void
12310 lpfc_io_resume(struct pci_dev *pdev)
12311 {
12312 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12313 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12314 
12315 	switch (phba->pci_dev_grp) {
12316 	case LPFC_PCI_DEV_LP:
12317 		lpfc_io_resume_s3(pdev);
12318 		break;
12319 	case LPFC_PCI_DEV_OC:
12320 		lpfc_io_resume_s4(pdev);
12321 		break;
12322 	default:
12323 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12324 				"1429 Invalid PCI device group: 0x%x\n",
12325 				phba->pci_dev_grp);
12326 		break;
12327 	}
12328 	return;
12329 }
12330 
12331 /**
12332  * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
12333  * @phba: pointer to lpfc hba data structure.
12334  *
12335  * This routine checks to see if OAS is supported for this adapter. If
12336  * supported, the configure Flash Optimized Fabric flag is set.  Otherwise,
12337  * the enable oas flag is cleared and the pool created for OAS device data
12338  * is destroyed.
12339  *
12340  **/
12341 void
12342 lpfc_sli4_oas_verify(struct lpfc_hba *phba)
12343 {
12344 
12345 	if (!phba->cfg_EnableXLane)
12346 		return;
12347 
12348 	if (phba->sli4_hba.pc_sli4_params.oas_supported) {
12349 		phba->cfg_fof = 1;
12350 	} else {
12351 		phba->cfg_fof = 0;
12352 		if (phba->device_data_mem_pool)
12353 			mempool_destroy(phba->device_data_mem_pool);
12354 		phba->device_data_mem_pool = NULL;
12355 	}
12356 
12357 	return;
12358 }
12359 
12360 /**
12361  * lpfc_fof_queue_setup - Set up all the fof queues
12362  * @phba: pointer to lpfc hba data structure.
12363  *
12364  * This routine is invoked to set up all the fof queues for the FC HBA
12365  * operation.
12366  *
12367  * Return codes
12368  *      0 - successful
12369  *      -ENOMEM - No available memory
12370  **/
12371 int
12372 lpfc_fof_queue_setup(struct lpfc_hba *phba)
12373 {
12374 	struct lpfc_sli_ring *pring;
12375 	int rc;
12376 
12377 	rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
12378 	if (rc)
12379 		return -ENOMEM;
12380 
12381 	if (phba->cfg_fof) {
12382 
12383 		rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
12384 				    phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
12385 		if (rc)
12386 			goto out_oas_cq;
12387 
12388 		rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
12389 				    phba->sli4_hba.oas_cq, LPFC_FCP);
12390 		if (rc)
12391 			goto out_oas_wq;
12392 
12393 		/* Bind this CQ/WQ to the NVME ring */
12394 		pring = phba->sli4_hba.oas_wq->pring;
12395 		pring->sli.sli4.wqp =
12396 			(void *)phba->sli4_hba.oas_wq;
12397 		phba->sli4_hba.oas_cq->pring = pring;
12398 	}
12399 
12400 	return 0;
12401 
12402 out_oas_wq:
12403 	lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
12404 out_oas_cq:
12405 	lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
12406 	return rc;
12407 
12408 }
12409 
12410 /**
12411  * lpfc_fof_queue_create - Create all the fof queues
12412  * @phba: pointer to lpfc hba data structure.
12413  *
12414  * This routine is invoked to allocate all the fof queues for the FC HBA
12415  * operation. For each SLI4 queue type, the parameters such as queue entry
12416  * count (queue depth) shall be taken from the module parameter. For now,
12417  * we just use some constant number as place holder.
12418  *
12419  * Return codes
12420  *      0 - successful
12421  *      -ENOMEM - No availble memory
12422  *      -EIO - The mailbox failed to complete successfully.
12423  **/
12424 int
12425 lpfc_fof_queue_create(struct lpfc_hba *phba)
12426 {
12427 	struct lpfc_queue *qdesc;
12428 	uint32_t wqesize;
12429 
12430 	/* Create FOF EQ */
12431 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_DEFAULT_PAGE_SIZE,
12432 				      phba->sli4_hba.eq_esize,
12433 				      phba->sli4_hba.eq_ecount);
12434 	if (!qdesc)
12435 		goto out_error;
12436 
12437 	qdesc->qe_valid = 1;
12438 	phba->sli4_hba.fof_eq = qdesc;
12439 
12440 	if (phba->cfg_fof) {
12441 
12442 		/* Create OAS CQ */
12443 		if (phba->enab_exp_wqcq_pages)
12444 			qdesc = lpfc_sli4_queue_alloc(phba,
12445 						      LPFC_EXPANDED_PAGE_SIZE,
12446 						      phba->sli4_hba.cq_esize,
12447 						      LPFC_CQE_EXP_COUNT);
12448 		else
12449 			qdesc = lpfc_sli4_queue_alloc(phba,
12450 						      LPFC_DEFAULT_PAGE_SIZE,
12451 						      phba->sli4_hba.cq_esize,
12452 						      phba->sli4_hba.cq_ecount);
12453 		if (!qdesc)
12454 			goto out_error;
12455 
12456 		qdesc->qe_valid = 1;
12457 		phba->sli4_hba.oas_cq = qdesc;
12458 
12459 		/* Create OAS WQ */
12460 		if (phba->enab_exp_wqcq_pages) {
12461 			wqesize = (phba->fcp_embed_io) ?
12462 				LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
12463 			qdesc = lpfc_sli4_queue_alloc(phba,
12464 						      LPFC_EXPANDED_PAGE_SIZE,
12465 						      wqesize,
12466 						      LPFC_WQE_EXP_COUNT);
12467 		} else
12468 			qdesc = lpfc_sli4_queue_alloc(phba,
12469 						      LPFC_DEFAULT_PAGE_SIZE,
12470 						      phba->sli4_hba.wq_esize,
12471 						      phba->sli4_hba.wq_ecount);
12472 
12473 		if (!qdesc)
12474 			goto out_error;
12475 
12476 		phba->sli4_hba.oas_wq = qdesc;
12477 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
12478 
12479 	}
12480 	return 0;
12481 
12482 out_error:
12483 	lpfc_fof_queue_destroy(phba);
12484 	return -ENOMEM;
12485 }
12486 
12487 /**
12488  * lpfc_fof_queue_destroy - Destroy all the fof queues
12489  * @phba: pointer to lpfc hba data structure.
12490  *
12491  * This routine is invoked to release all the SLI4 queues with the FC HBA
12492  * operation.
12493  *
12494  * Return codes
12495  *      0 - successful
12496  **/
12497 int
12498 lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12499 {
12500 	/* Release FOF Event queue */
12501 	if (phba->sli4_hba.fof_eq != NULL) {
12502 		lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12503 		phba->sli4_hba.fof_eq = NULL;
12504 	}
12505 
12506 	/* Release OAS Completion queue */
12507 	if (phba->sli4_hba.oas_cq != NULL) {
12508 		lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12509 		phba->sli4_hba.oas_cq = NULL;
12510 	}
12511 
12512 	/* Release OAS Work queue */
12513 	if (phba->sli4_hba.oas_wq != NULL) {
12514 		lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12515 		phba->sli4_hba.oas_wq = NULL;
12516 	}
12517 	return 0;
12518 }
12519 
12520 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12521 
12522 static const struct pci_error_handlers lpfc_err_handler = {
12523 	.error_detected = lpfc_io_error_detected,
12524 	.slot_reset = lpfc_io_slot_reset,
12525 	.resume = lpfc_io_resume,
12526 };
12527 
12528 static struct pci_driver lpfc_driver = {
12529 	.name		= LPFC_DRIVER_NAME,
12530 	.id_table	= lpfc_id_table,
12531 	.probe		= lpfc_pci_probe_one,
12532 	.remove		= lpfc_pci_remove_one,
12533 	.shutdown	= lpfc_pci_remove_one,
12534 	.suspend        = lpfc_pci_suspend_one,
12535 	.resume		= lpfc_pci_resume_one,
12536 	.err_handler    = &lpfc_err_handler,
12537 };
12538 
12539 static const struct file_operations lpfc_mgmt_fop = {
12540 	.owner = THIS_MODULE,
12541 };
12542 
12543 static struct miscdevice lpfc_mgmt_dev = {
12544 	.minor = MISC_DYNAMIC_MINOR,
12545 	.name = "lpfcmgmt",
12546 	.fops = &lpfc_mgmt_fop,
12547 };
12548 
12549 /**
12550  * lpfc_init - lpfc module initialization routine
12551  *
12552  * This routine is to be invoked when the lpfc module is loaded into the
12553  * kernel. The special kernel macro module_init() is used to indicate the
12554  * role of this routine to the kernel as lpfc module entry point.
12555  *
12556  * Return codes
12557  *   0 - successful
12558  *   -ENOMEM - FC attach transport failed
12559  *   all others - failed
12560  */
12561 static int __init
12562 lpfc_init(void)
12563 {
12564 	int error = 0;
12565 
12566 	printk(LPFC_MODULE_DESC "\n");
12567 	printk(LPFC_COPYRIGHT "\n");
12568 
12569 	error = misc_register(&lpfc_mgmt_dev);
12570 	if (error)
12571 		printk(KERN_ERR "Could not register lpfcmgmt device, "
12572 			"misc_register returned with status %d", error);
12573 
12574 	lpfc_transport_functions.vport_create = lpfc_vport_create;
12575 	lpfc_transport_functions.vport_delete = lpfc_vport_delete;
12576 	lpfc_transport_template =
12577 				fc_attach_transport(&lpfc_transport_functions);
12578 	if (lpfc_transport_template == NULL)
12579 		return -ENOMEM;
12580 	lpfc_vport_transport_template =
12581 		fc_attach_transport(&lpfc_vport_transport_functions);
12582 	if (lpfc_vport_transport_template == NULL) {
12583 		fc_release_transport(lpfc_transport_template);
12584 		return -ENOMEM;
12585 	}
12586 	lpfc_nvme_cmd_template();
12587 	lpfc_nvmet_cmd_template();
12588 
12589 	/* Initialize in case vector mapping is needed */
12590 	lpfc_used_cpu = NULL;
12591 	lpfc_present_cpu = num_present_cpus();
12592 
12593 	error = pci_register_driver(&lpfc_driver);
12594 	if (error) {
12595 		fc_release_transport(lpfc_transport_template);
12596 		fc_release_transport(lpfc_vport_transport_template);
12597 	}
12598 
12599 	return error;
12600 }
12601 
12602 /**
12603  * lpfc_exit - lpfc module removal routine
12604  *
12605  * This routine is invoked when the lpfc module is removed from the kernel.
12606  * The special kernel macro module_exit() is used to indicate the role of
12607  * this routine to the kernel as lpfc module exit point.
12608  */
12609 static void __exit
12610 lpfc_exit(void)
12611 {
12612 	misc_deregister(&lpfc_mgmt_dev);
12613 	pci_unregister_driver(&lpfc_driver);
12614 	fc_release_transport(lpfc_transport_template);
12615 	fc_release_transport(lpfc_vport_transport_template);
12616 	if (_dump_buf_data) {
12617 		printk(KERN_ERR	"9062 BLKGRD: freeing %lu pages for "
12618 				"_dump_buf_data at 0x%p\n",
12619 				(1L << _dump_buf_data_order), _dump_buf_data);
12620 		free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12621 	}
12622 
12623 	if (_dump_buf_dif) {
12624 		printk(KERN_ERR	"9049 BLKGRD: freeing %lu pages for "
12625 				"_dump_buf_dif at 0x%p\n",
12626 				(1L << _dump_buf_dif_order), _dump_buf_dif);
12627 		free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12628 	}
12629 	kfree(lpfc_used_cpu);
12630 	idr_destroy(&lpfc_hba_index);
12631 }
12632 
12633 module_init(lpfc_init);
12634 module_exit(lpfc_exit);
12635 MODULE_LICENSE("GPL");
12636 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
12637 MODULE_AUTHOR("Broadcom");
12638 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
12639