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