xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_init.c (revision 711aab1d)
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 		goto out_free_bsmbx;
6135 	}
6136 
6137 	mempool_free(mboxq, phba->mbox_mem_pool);
6138 
6139 	/* Verify OAS is supported */
6140 	lpfc_sli4_oas_verify(phba);
6141 	if (phba->cfg_fof)
6142 		fof_vectors = 1;
6143 
6144 	/* Verify all the SLI4 queues */
6145 	rc = lpfc_sli4_queue_verify(phba);
6146 	if (rc)
6147 		goto out_free_bsmbx;
6148 
6149 	/* Create driver internal CQE event pool */
6150 	rc = lpfc_sli4_cq_event_pool_create(phba);
6151 	if (rc)
6152 		goto out_free_bsmbx;
6153 
6154 	/* Initialize sgl lists per host */
6155 	lpfc_init_sgl_list(phba);
6156 
6157 	/* Allocate and initialize active sgl array */
6158 	rc = lpfc_init_active_sgl_array(phba);
6159 	if (rc) {
6160 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6161 				"1430 Failed to initialize sgl list.\n");
6162 		goto out_destroy_cq_event_pool;
6163 	}
6164 	rc = lpfc_sli4_init_rpi_hdrs(phba);
6165 	if (rc) {
6166 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6167 				"1432 Failed to initialize rpi headers.\n");
6168 		goto out_free_active_sgl;
6169 	}
6170 
6171 	/* Allocate eligible FCF bmask memory for FCF roundrobin failover */
6172 	longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
6173 	phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
6174 					 GFP_KERNEL);
6175 	if (!phba->fcf.fcf_rr_bmask) {
6176 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6177 				"2759 Failed allocate memory for FCF round "
6178 				"robin failover bmask\n");
6179 		rc = -ENOMEM;
6180 		goto out_remove_rpi_hdrs;
6181 	}
6182 
6183 	phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6184 						sizeof(struct lpfc_hba_eq_hdl),
6185 						GFP_KERNEL);
6186 	if (!phba->sli4_hba.hba_eq_hdl) {
6187 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6188 				"2572 Failed allocate memory for "
6189 				"fast-path per-EQ handle array\n");
6190 		rc = -ENOMEM;
6191 		goto out_free_fcf_rr_bmask;
6192 	}
6193 
6194 	phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6195 					sizeof(struct lpfc_vector_map_info),
6196 					GFP_KERNEL);
6197 	if (!phba->sli4_hba.cpu_map) {
6198 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6199 				"3327 Failed allocate memory for msi-x "
6200 				"interrupt vector mapping\n");
6201 		rc = -ENOMEM;
6202 		goto out_free_hba_eq_hdl;
6203 	}
6204 	if (lpfc_used_cpu == NULL) {
6205 		lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6206 						GFP_KERNEL);
6207 		if (!lpfc_used_cpu) {
6208 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6209 					"3335 Failed allocate memory for msi-x "
6210 					"interrupt vector mapping\n");
6211 			kfree(phba->sli4_hba.cpu_map);
6212 			rc = -ENOMEM;
6213 			goto out_free_hba_eq_hdl;
6214 		}
6215 		for (i = 0; i < lpfc_present_cpu; i++)
6216 			lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6217 	}
6218 
6219 	/*
6220 	 * Enable sr-iov virtual functions if supported and configured
6221 	 * through the module parameter.
6222 	 */
6223 	if (phba->cfg_sriov_nr_virtfn > 0) {
6224 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6225 						 phba->cfg_sriov_nr_virtfn);
6226 		if (rc) {
6227 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6228 					"3020 Requested number of SR-IOV "
6229 					"virtual functions (%d) is not "
6230 					"supported\n",
6231 					phba->cfg_sriov_nr_virtfn);
6232 			phba->cfg_sriov_nr_virtfn = 0;
6233 		}
6234 	}
6235 
6236 	return 0;
6237 
6238 out_free_hba_eq_hdl:
6239 	kfree(phba->sli4_hba.hba_eq_hdl);
6240 out_free_fcf_rr_bmask:
6241 	kfree(phba->fcf.fcf_rr_bmask);
6242 out_remove_rpi_hdrs:
6243 	lpfc_sli4_remove_rpi_hdrs(phba);
6244 out_free_active_sgl:
6245 	lpfc_free_active_sgl(phba);
6246 out_destroy_cq_event_pool:
6247 	lpfc_sli4_cq_event_pool_destroy(phba);
6248 out_free_bsmbx:
6249 	lpfc_destroy_bootstrap_mbox(phba);
6250 out_free_mem:
6251 	lpfc_mem_free(phba);
6252 	return rc;
6253 }
6254 
6255 /**
6256  * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6257  * @phba: pointer to lpfc hba data structure.
6258  *
6259  * This routine is invoked to unset the driver internal resources set up
6260  * specific for supporting the SLI-4 HBA device it attached to.
6261  **/
6262 static void
6263 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6264 {
6265 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6266 
6267 	/* Free memory allocated for msi-x interrupt vector to CPU mapping */
6268 	kfree(phba->sli4_hba.cpu_map);
6269 	phba->sli4_hba.num_present_cpu = 0;
6270 	phba->sli4_hba.num_online_cpu = 0;
6271 	phba->sli4_hba.curr_disp_cpu = 0;
6272 
6273 	/* Free memory allocated for fast-path work queue handles */
6274 	kfree(phba->sli4_hba.hba_eq_hdl);
6275 
6276 	/* Free the allocated rpi headers. */
6277 	lpfc_sli4_remove_rpi_hdrs(phba);
6278 	lpfc_sli4_remove_rpis(phba);
6279 
6280 	/* Free eligible FCF index bmask */
6281 	kfree(phba->fcf.fcf_rr_bmask);
6282 
6283 	/* Free the ELS sgl list */
6284 	lpfc_free_active_sgl(phba);
6285 	lpfc_free_els_sgl_list(phba);
6286 	lpfc_free_nvmet_sgl_list(phba);
6287 
6288 	/* Free the completion queue EQ event pool */
6289 	lpfc_sli4_cq_event_release_all(phba);
6290 	lpfc_sli4_cq_event_pool_destroy(phba);
6291 
6292 	/* Release resource identifiers. */
6293 	lpfc_sli4_dealloc_resource_identifiers(phba);
6294 
6295 	/* Free the bsmbx region. */
6296 	lpfc_destroy_bootstrap_mbox(phba);
6297 
6298 	/* Free the SLI Layer memory with SLI4 HBAs */
6299 	lpfc_mem_free_all(phba);
6300 
6301 	/* Free the current connect table */
6302 	list_for_each_entry_safe(conn_entry, next_conn_entry,
6303 		&phba->fcf_conn_rec_list, list) {
6304 		list_del_init(&conn_entry->list);
6305 		kfree(conn_entry);
6306 	}
6307 
6308 	return;
6309 }
6310 
6311 /**
6312  * lpfc_init_api_table_setup - Set up init api function jump table
6313  * @phba: The hba struct for which this call is being executed.
6314  * @dev_grp: The HBA PCI-Device group number.
6315  *
6316  * This routine sets up the device INIT interface API function jump table
6317  * in @phba struct.
6318  *
6319  * Returns: 0 - success, -ENODEV - failure.
6320  **/
6321 int
6322 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6323 {
6324 	phba->lpfc_hba_init_link = lpfc_hba_init_link;
6325 	phba->lpfc_hba_down_link = lpfc_hba_down_link;
6326 	phba->lpfc_selective_reset = lpfc_selective_reset;
6327 	switch (dev_grp) {
6328 	case LPFC_PCI_DEV_LP:
6329 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6330 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6331 		phba->lpfc_stop_port = lpfc_stop_port_s3;
6332 		break;
6333 	case LPFC_PCI_DEV_OC:
6334 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6335 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6336 		phba->lpfc_stop_port = lpfc_stop_port_s4;
6337 		break;
6338 	default:
6339 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6340 				"1431 Invalid HBA PCI-device group: 0x%x\n",
6341 				dev_grp);
6342 		return -ENODEV;
6343 		break;
6344 	}
6345 	return 0;
6346 }
6347 
6348 /**
6349  * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6350  * @phba: pointer to lpfc hba data structure.
6351  *
6352  * This routine is invoked to set up the driver internal resources after the
6353  * device specific resource setup to support the HBA device it attached to.
6354  *
6355  * Return codes
6356  * 	0 - successful
6357  * 	other values - error
6358  **/
6359 static int
6360 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6361 {
6362 	int error;
6363 
6364 	/* Startup the kernel thread for this host adapter. */
6365 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
6366 					  "lpfc_worker_%d", phba->brd_no);
6367 	if (IS_ERR(phba->worker_thread)) {
6368 		error = PTR_ERR(phba->worker_thread);
6369 		return error;
6370 	}
6371 
6372 	return 0;
6373 }
6374 
6375 /**
6376  * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6377  * @phba: pointer to lpfc hba data structure.
6378  *
6379  * This routine is invoked to unset the driver internal resources set up after
6380  * the device specific resource setup for supporting the HBA device it
6381  * attached to.
6382  **/
6383 static void
6384 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6385 {
6386 	/* Stop kernel worker thread */
6387 	kthread_stop(phba->worker_thread);
6388 }
6389 
6390 /**
6391  * lpfc_free_iocb_list - Free iocb list.
6392  * @phba: pointer to lpfc hba data structure.
6393  *
6394  * This routine is invoked to free the driver's IOCB list and memory.
6395  **/
6396 void
6397 lpfc_free_iocb_list(struct lpfc_hba *phba)
6398 {
6399 	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6400 
6401 	spin_lock_irq(&phba->hbalock);
6402 	list_for_each_entry_safe(iocbq_entry, iocbq_next,
6403 				 &phba->lpfc_iocb_list, list) {
6404 		list_del(&iocbq_entry->list);
6405 		kfree(iocbq_entry);
6406 		phba->total_iocbq_bufs--;
6407 	}
6408 	spin_unlock_irq(&phba->hbalock);
6409 
6410 	return;
6411 }
6412 
6413 /**
6414  * lpfc_init_iocb_list - Allocate and initialize iocb list.
6415  * @phba: pointer to lpfc hba data structure.
6416  *
6417  * This routine is invoked to allocate and initizlize the driver's IOCB
6418  * list and set up the IOCB tag array accordingly.
6419  *
6420  * Return codes
6421  *	0 - successful
6422  *	other values - error
6423  **/
6424 int
6425 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6426 {
6427 	struct lpfc_iocbq *iocbq_entry = NULL;
6428 	uint16_t iotag;
6429 	int i;
6430 
6431 	/* Initialize and populate the iocb list per host.  */
6432 	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
6433 	for (i = 0; i < iocb_count; i++) {
6434 		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
6435 		if (iocbq_entry == NULL) {
6436 			printk(KERN_ERR "%s: only allocated %d iocbs of "
6437 				"expected %d count. Unloading driver.\n",
6438 				__func__, i, LPFC_IOCB_LIST_CNT);
6439 			goto out_free_iocbq;
6440 		}
6441 
6442 		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6443 		if (iotag == 0) {
6444 			kfree(iocbq_entry);
6445 			printk(KERN_ERR "%s: failed to allocate IOTAG. "
6446 				"Unloading driver.\n", __func__);
6447 			goto out_free_iocbq;
6448 		}
6449 		iocbq_entry->sli4_lxritag = NO_XRI;
6450 		iocbq_entry->sli4_xritag = NO_XRI;
6451 
6452 		spin_lock_irq(&phba->hbalock);
6453 		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6454 		phba->total_iocbq_bufs++;
6455 		spin_unlock_irq(&phba->hbalock);
6456 	}
6457 
6458 	return 0;
6459 
6460 out_free_iocbq:
6461 	lpfc_free_iocb_list(phba);
6462 
6463 	return -ENOMEM;
6464 }
6465 
6466 /**
6467  * lpfc_free_sgl_list - Free a given sgl list.
6468  * @phba: pointer to lpfc hba data structure.
6469  * @sglq_list: pointer to the head of sgl list.
6470  *
6471  * This routine is invoked to free a give sgl list and memory.
6472  **/
6473 void
6474 lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
6475 {
6476 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6477 
6478 	list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6479 		list_del(&sglq_entry->list);
6480 		lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6481 		kfree(sglq_entry);
6482 	}
6483 }
6484 
6485 /**
6486  * lpfc_free_els_sgl_list - Free els sgl list.
6487  * @phba: pointer to lpfc hba data structure.
6488  *
6489  * This routine is invoked to free the driver's els sgl list and memory.
6490  **/
6491 static void
6492 lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6493 {
6494 	LIST_HEAD(sglq_list);
6495 
6496 	/* Retrieve all els sgls from driver list */
6497 	spin_lock_irq(&phba->hbalock);
6498 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6499 	list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6500 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6501 	spin_unlock_irq(&phba->hbalock);
6502 
6503 	/* Now free the sgl list */
6504 	lpfc_free_sgl_list(phba, &sglq_list);
6505 }
6506 
6507 /**
6508  * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6509  * @phba: pointer to lpfc hba data structure.
6510  *
6511  * This routine is invoked to free the driver's nvmet sgl list and memory.
6512  **/
6513 static void
6514 lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6515 {
6516 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6517 	LIST_HEAD(sglq_list);
6518 
6519 	/* Retrieve all nvmet sgls from driver list */
6520 	spin_lock_irq(&phba->hbalock);
6521 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6522 	list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6523 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6524 	spin_unlock_irq(&phba->hbalock);
6525 
6526 	/* Now free the sgl list */
6527 	list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6528 		list_del(&sglq_entry->list);
6529 		lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6530 		kfree(sglq_entry);
6531 	}
6532 
6533 	/* Update the nvmet_xri_cnt to reflect no current sgls.
6534 	 * The next initialization cycle sets the count and allocates
6535 	 * the sgls over again.
6536 	 */
6537 	phba->sli4_hba.nvmet_xri_cnt = 0;
6538 }
6539 
6540 /**
6541  * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6542  * @phba: pointer to lpfc hba data structure.
6543  *
6544  * This routine is invoked to allocate the driver's active sgl memory.
6545  * This array will hold the sglq_entry's for active IOs.
6546  **/
6547 static int
6548 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6549 {
6550 	int size;
6551 	size = sizeof(struct lpfc_sglq *);
6552 	size *= phba->sli4_hba.max_cfg_param.max_xri;
6553 
6554 	phba->sli4_hba.lpfc_sglq_active_list =
6555 		kzalloc(size, GFP_KERNEL);
6556 	if (!phba->sli4_hba.lpfc_sglq_active_list)
6557 		return -ENOMEM;
6558 	return 0;
6559 }
6560 
6561 /**
6562  * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6563  * @phba: pointer to lpfc hba data structure.
6564  *
6565  * This routine is invoked to walk through the array of active sglq entries
6566  * and free all of the resources.
6567  * This is just a place holder for now.
6568  **/
6569 static void
6570 lpfc_free_active_sgl(struct lpfc_hba *phba)
6571 {
6572 	kfree(phba->sli4_hba.lpfc_sglq_active_list);
6573 }
6574 
6575 /**
6576  * lpfc_init_sgl_list - Allocate and initialize sgl list.
6577  * @phba: pointer to lpfc hba data structure.
6578  *
6579  * This routine is invoked to allocate and initizlize the driver's sgl
6580  * list and set up the sgl xritag tag array accordingly.
6581  *
6582  **/
6583 static void
6584 lpfc_init_sgl_list(struct lpfc_hba *phba)
6585 {
6586 	/* Initialize and populate the sglq list per host/VF. */
6587 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
6588 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
6589 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
6590 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
6591 
6592 	/* els xri-sgl book keeping */
6593 	phba->sli4_hba.els_xri_cnt = 0;
6594 
6595 	/* scsi xri-buffer book keeping */
6596 	phba->sli4_hba.scsi_xri_cnt = 0;
6597 
6598 	/* nvme xri-buffer book keeping */
6599 	phba->sli4_hba.nvme_xri_cnt = 0;
6600 }
6601 
6602 /**
6603  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6604  * @phba: pointer to lpfc hba data structure.
6605  *
6606  * This routine is invoked to post rpi header templates to the
6607  * port for those SLI4 ports that do not support extents.  This routine
6608  * posts a PAGE_SIZE memory region to the port to hold up to
6609  * PAGE_SIZE modulo 64 rpi context headers.  This is an initialization routine
6610  * and should be called only when interrupts are disabled.
6611  *
6612  * Return codes
6613  * 	0 - successful
6614  *	-ERROR - otherwise.
6615  **/
6616 int
6617 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6618 {
6619 	int rc = 0;
6620 	struct lpfc_rpi_hdr *rpi_hdr;
6621 
6622 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
6623 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6624 		return rc;
6625 	if (phba->sli4_hba.extents_in_use)
6626 		return -EIO;
6627 
6628 	rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6629 	if (!rpi_hdr) {
6630 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6631 				"0391 Error during rpi post operation\n");
6632 		lpfc_sli4_remove_rpis(phba);
6633 		rc = -ENODEV;
6634 	}
6635 
6636 	return rc;
6637 }
6638 
6639 /**
6640  * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6641  * @phba: pointer to lpfc hba data structure.
6642  *
6643  * This routine is invoked to allocate a single 4KB memory region to
6644  * support rpis and stores them in the phba.  This single region
6645  * provides support for up to 64 rpis.  The region is used globally
6646  * by the device.
6647  *
6648  * Returns:
6649  *   A valid rpi hdr on success.
6650  *   A NULL pointer on any failure.
6651  **/
6652 struct lpfc_rpi_hdr *
6653 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6654 {
6655 	uint16_t rpi_limit, curr_rpi_range;
6656 	struct lpfc_dmabuf *dmabuf;
6657 	struct lpfc_rpi_hdr *rpi_hdr;
6658 
6659 	/*
6660 	 * If the SLI4 port supports extents, posting the rpi header isn't
6661 	 * required.  Set the expected maximum count and let the actual value
6662 	 * get set when extents are fully allocated.
6663 	 */
6664 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6665 		return NULL;
6666 	if (phba->sli4_hba.extents_in_use)
6667 		return NULL;
6668 
6669 	/* The limit on the logical index is just the max_rpi count. */
6670 	rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
6671 
6672 	spin_lock_irq(&phba->hbalock);
6673 	/*
6674 	 * Establish the starting RPI in this header block.  The starting
6675 	 * rpi is normalized to a zero base because the physical rpi is
6676 	 * port based.
6677 	 */
6678 	curr_rpi_range = phba->sli4_hba.next_rpi;
6679 	spin_unlock_irq(&phba->hbalock);
6680 
6681 	/* Reached full RPI range */
6682 	if (curr_rpi_range == rpi_limit)
6683 		return NULL;
6684 
6685 	/*
6686 	 * First allocate the protocol header region for the port.  The
6687 	 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6688 	 */
6689 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6690 	if (!dmabuf)
6691 		return NULL;
6692 
6693 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6694 					   LPFC_HDR_TEMPLATE_SIZE,
6695 					   &dmabuf->phys, GFP_KERNEL);
6696 	if (!dmabuf->virt) {
6697 		rpi_hdr = NULL;
6698 		goto err_free_dmabuf;
6699 	}
6700 
6701 	if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6702 		rpi_hdr = NULL;
6703 		goto err_free_coherent;
6704 	}
6705 
6706 	/* Save the rpi header data for cleanup later. */
6707 	rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6708 	if (!rpi_hdr)
6709 		goto err_free_coherent;
6710 
6711 	rpi_hdr->dmabuf = dmabuf;
6712 	rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6713 	rpi_hdr->page_count = 1;
6714 	spin_lock_irq(&phba->hbalock);
6715 
6716 	/* The rpi_hdr stores the logical index only. */
6717 	rpi_hdr->start_rpi = curr_rpi_range;
6718 	rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
6719 	list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6720 
6721 	spin_unlock_irq(&phba->hbalock);
6722 	return rpi_hdr;
6723 
6724  err_free_coherent:
6725 	dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6726 			  dmabuf->virt, dmabuf->phys);
6727  err_free_dmabuf:
6728 	kfree(dmabuf);
6729 	return NULL;
6730 }
6731 
6732 /**
6733  * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6734  * @phba: pointer to lpfc hba data structure.
6735  *
6736  * This routine is invoked to remove all memory resources allocated
6737  * to support rpis for SLI4 ports not supporting extents. This routine
6738  * presumes the caller has released all rpis consumed by fabric or port
6739  * logins and is prepared to have the header pages removed.
6740  **/
6741 void
6742 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6743 {
6744 	struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6745 
6746 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6747 		goto exit;
6748 
6749 	list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6750 				 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6751 		list_del(&rpi_hdr->list);
6752 		dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6753 				  rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6754 		kfree(rpi_hdr->dmabuf);
6755 		kfree(rpi_hdr);
6756 	}
6757  exit:
6758 	/* There are no rpis available to the port now. */
6759 	phba->sli4_hba.next_rpi = 0;
6760 }
6761 
6762 /**
6763  * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6764  * @pdev: pointer to pci device data structure.
6765  *
6766  * This routine is invoked to allocate the driver hba data structure for an
6767  * HBA device. If the allocation is successful, the phba reference to the
6768  * PCI device data structure is set.
6769  *
6770  * Return codes
6771  *      pointer to @phba - successful
6772  *      NULL - error
6773  **/
6774 static struct lpfc_hba *
6775 lpfc_hba_alloc(struct pci_dev *pdev)
6776 {
6777 	struct lpfc_hba *phba;
6778 
6779 	/* Allocate memory for HBA structure */
6780 	phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6781 	if (!phba) {
6782 		dev_err(&pdev->dev, "failed to allocate hba struct\n");
6783 		return NULL;
6784 	}
6785 
6786 	/* Set reference to PCI device in HBA structure */
6787 	phba->pcidev = pdev;
6788 
6789 	/* Assign an unused board number */
6790 	phba->brd_no = lpfc_get_instance();
6791 	if (phba->brd_no < 0) {
6792 		kfree(phba);
6793 		return NULL;
6794 	}
6795 	phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
6796 
6797 	spin_lock_init(&phba->ct_ev_lock);
6798 	INIT_LIST_HEAD(&phba->ct_ev_waiters);
6799 
6800 	return phba;
6801 }
6802 
6803 /**
6804  * lpfc_hba_free - Free driver hba data structure with a device.
6805  * @phba: pointer to lpfc hba data structure.
6806  *
6807  * This routine is invoked to free the driver hba data structure with an
6808  * HBA device.
6809  **/
6810 static void
6811 lpfc_hba_free(struct lpfc_hba *phba)
6812 {
6813 	/* Release the driver assigned board number */
6814 	idr_remove(&lpfc_hba_index, phba->brd_no);
6815 
6816 	/* Free memory allocated with sli3 rings */
6817 	kfree(phba->sli.sli3_ring);
6818 	phba->sli.sli3_ring = NULL;
6819 
6820 	kfree(phba);
6821 	return;
6822 }
6823 
6824 /**
6825  * lpfc_create_shost - Create hba physical port with associated scsi host.
6826  * @phba: pointer to lpfc hba data structure.
6827  *
6828  * This routine is invoked to create HBA physical port and associate a SCSI
6829  * host with it.
6830  *
6831  * Return codes
6832  *      0 - successful
6833  *      other values - error
6834  **/
6835 static int
6836 lpfc_create_shost(struct lpfc_hba *phba)
6837 {
6838 	struct lpfc_vport *vport;
6839 	struct Scsi_Host  *shost;
6840 
6841 	/* Initialize HBA FC structure */
6842 	phba->fc_edtov = FF_DEF_EDTOV;
6843 	phba->fc_ratov = FF_DEF_RATOV;
6844 	phba->fc_altov = FF_DEF_ALTOV;
6845 	phba->fc_arbtov = FF_DEF_ARBTOV;
6846 
6847 	atomic_set(&phba->sdev_cnt, 0);
6848 	atomic_set(&phba->fc4ScsiInputRequests, 0);
6849 	atomic_set(&phba->fc4ScsiOutputRequests, 0);
6850 	atomic_set(&phba->fc4ScsiControlRequests, 0);
6851 	atomic_set(&phba->fc4ScsiIoCmpls, 0);
6852 	atomic_set(&phba->fc4NvmeInputRequests, 0);
6853 	atomic_set(&phba->fc4NvmeOutputRequests, 0);
6854 	atomic_set(&phba->fc4NvmeControlRequests, 0);
6855 	atomic_set(&phba->fc4NvmeIoCmpls, 0);
6856 	atomic_set(&phba->fc4NvmeLsRequests, 0);
6857 	atomic_set(&phba->fc4NvmeLsCmpls, 0);
6858 	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
6859 	if (!vport)
6860 		return -ENODEV;
6861 
6862 	shost = lpfc_shost_from_vport(vport);
6863 	phba->pport = vport;
6864 
6865 	if (phba->nvmet_support) {
6866 		/* Only 1 vport (pport) will support NVME target */
6867 		if (phba->txrdy_payload_pool == NULL) {
6868 			phba->txrdy_payload_pool = dma_pool_create(
6869 				"txrdy_pool", &phba->pcidev->dev,
6870 				TXRDY_PAYLOAD_LEN, 16, 0);
6871 			if (phba->txrdy_payload_pool) {
6872 				phba->targetport = NULL;
6873 				phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6874 				lpfc_printf_log(phba, KERN_INFO,
6875 						LOG_INIT | LOG_NVME_DISC,
6876 						"6076 NVME Target Found\n");
6877 			}
6878 		}
6879 	}
6880 
6881 	lpfc_debugfs_initialize(vport);
6882 	/* Put reference to SCSI host to driver's device private data */
6883 	pci_set_drvdata(phba->pcidev, shost);
6884 
6885 	/*
6886 	 * At this point we are fully registered with PSA. In addition,
6887 	 * any initial discovery should be completed.
6888 	 */
6889 	vport->load_flag |= FC_ALLOW_FDMI;
6890 	if (phba->cfg_enable_SmartSAN ||
6891 	    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
6892 
6893 		/* Setup appropriate attribute masks */
6894 		vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
6895 		if (phba->cfg_enable_SmartSAN)
6896 			vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6897 		else
6898 			vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6899 	}
6900 	return 0;
6901 }
6902 
6903 /**
6904  * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6905  * @phba: pointer to lpfc hba data structure.
6906  *
6907  * This routine is invoked to destroy HBA physical port and the associated
6908  * SCSI host.
6909  **/
6910 static void
6911 lpfc_destroy_shost(struct lpfc_hba *phba)
6912 {
6913 	struct lpfc_vport *vport = phba->pport;
6914 
6915 	/* Destroy physical port that associated with the SCSI host */
6916 	destroy_port(vport);
6917 
6918 	return;
6919 }
6920 
6921 /**
6922  * lpfc_setup_bg - Setup Block guard structures and debug areas.
6923  * @phba: pointer to lpfc hba data structure.
6924  * @shost: the shost to be used to detect Block guard settings.
6925  *
6926  * This routine sets up the local Block guard protocol settings for @shost.
6927  * This routine also allocates memory for debugging bg buffers.
6928  **/
6929 static void
6930 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6931 {
6932 	uint32_t old_mask;
6933 	uint32_t old_guard;
6934 
6935 	int pagecnt = 10;
6936 	if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6937 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6938 				"1478 Registering BlockGuard with the "
6939 				"SCSI layer\n");
6940 
6941 		old_mask = phba->cfg_prot_mask;
6942 		old_guard = phba->cfg_prot_guard;
6943 
6944 		/* Only allow supported values */
6945 		phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6946 			SHOST_DIX_TYPE0_PROTECTION |
6947 			SHOST_DIX_TYPE1_PROTECTION);
6948 		phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6949 					 SHOST_DIX_GUARD_CRC);
6950 
6951 		/* DIF Type 1 protection for profiles AST1/C1 is end to end */
6952 		if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6953 			phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6954 
6955 		if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6956 			if ((old_mask != phba->cfg_prot_mask) ||
6957 				(old_guard != phba->cfg_prot_guard))
6958 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6959 					"1475 Registering BlockGuard with the "
6960 					"SCSI layer: mask %d  guard %d\n",
6961 					phba->cfg_prot_mask,
6962 					phba->cfg_prot_guard);
6963 
6964 			scsi_host_set_prot(shost, phba->cfg_prot_mask);
6965 			scsi_host_set_guard(shost, phba->cfg_prot_guard);
6966 		} else
6967 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6968 				"1479 Not Registering BlockGuard with the SCSI "
6969 				"layer, Bad protection parameters: %d %d\n",
6970 				old_mask, old_guard);
6971 	}
6972 
6973 	if (!_dump_buf_data) {
6974 		while (pagecnt) {
6975 			spin_lock_init(&_dump_buf_lock);
6976 			_dump_buf_data =
6977 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
6978 			if (_dump_buf_data) {
6979 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6980 					"9043 BLKGRD: allocated %d pages for "
6981 				       "_dump_buf_data at 0x%p\n",
6982 				       (1 << pagecnt), _dump_buf_data);
6983 				_dump_buf_data_order = pagecnt;
6984 				memset(_dump_buf_data, 0,
6985 				       ((1 << PAGE_SHIFT) << pagecnt));
6986 				break;
6987 			} else
6988 				--pagecnt;
6989 		}
6990 		if (!_dump_buf_data_order)
6991 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6992 				"9044 BLKGRD: ERROR unable to allocate "
6993 			       "memory for hexdump\n");
6994 	} else
6995 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6996 			"9045 BLKGRD: already allocated _dump_buf_data=0x%p"
6997 		       "\n", _dump_buf_data);
6998 	if (!_dump_buf_dif) {
6999 		while (pagecnt) {
7000 			_dump_buf_dif =
7001 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
7002 			if (_dump_buf_dif) {
7003 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7004 					"9046 BLKGRD: allocated %d pages for "
7005 				       "_dump_buf_dif at 0x%p\n",
7006 				       (1 << pagecnt), _dump_buf_dif);
7007 				_dump_buf_dif_order = pagecnt;
7008 				memset(_dump_buf_dif, 0,
7009 				       ((1 << PAGE_SHIFT) << pagecnt));
7010 				break;
7011 			} else
7012 				--pagecnt;
7013 		}
7014 		if (!_dump_buf_dif_order)
7015 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7016 			"9047 BLKGRD: ERROR unable to allocate "
7017 			       "memory for hexdump\n");
7018 	} else
7019 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7020 			"9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
7021 		       _dump_buf_dif);
7022 }
7023 
7024 /**
7025  * lpfc_post_init_setup - Perform necessary device post initialization setup.
7026  * @phba: pointer to lpfc hba data structure.
7027  *
7028  * This routine is invoked to perform all the necessary post initialization
7029  * setup for the device.
7030  **/
7031 static void
7032 lpfc_post_init_setup(struct lpfc_hba *phba)
7033 {
7034 	struct Scsi_Host  *shost;
7035 	struct lpfc_adapter_event_header adapter_event;
7036 
7037 	/* Get the default values for Model Name and Description */
7038 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7039 
7040 	/*
7041 	 * hba setup may have changed the hba_queue_depth so we need to
7042 	 * adjust the value of can_queue.
7043 	 */
7044 	shost = pci_get_drvdata(phba->pcidev);
7045 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
7046 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7047 		lpfc_setup_bg(phba, shost);
7048 
7049 	lpfc_host_attrib_init(shost);
7050 
7051 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7052 		spin_lock_irq(shost->host_lock);
7053 		lpfc_poll_start_timer(phba);
7054 		spin_unlock_irq(shost->host_lock);
7055 	}
7056 
7057 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7058 			"0428 Perform SCSI scan\n");
7059 	/* Send board arrival event to upper layer */
7060 	adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7061 	adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7062 	fc_host_post_vendor_event(shost, fc_get_event_number(),
7063 				  sizeof(adapter_event),
7064 				  (char *) &adapter_event,
7065 				  LPFC_NL_VENDOR_ID);
7066 	return;
7067 }
7068 
7069 /**
7070  * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7071  * @phba: pointer to lpfc hba data structure.
7072  *
7073  * This routine is invoked to set up the PCI device memory space for device
7074  * with SLI-3 interface spec.
7075  *
7076  * Return codes
7077  * 	0 - successful
7078  * 	other values - error
7079  **/
7080 static int
7081 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7082 {
7083 	struct pci_dev *pdev;
7084 	unsigned long bar0map_len, bar2map_len;
7085 	int i, hbq_count;
7086 	void *ptr;
7087 	int error = -ENODEV;
7088 
7089 	/* Obtain PCI device reference */
7090 	if (!phba->pcidev)
7091 		return error;
7092 	else
7093 		pdev = phba->pcidev;
7094 
7095 	/* Set the device DMA mask size */
7096 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7097 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7098 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7099 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
7100 			return error;
7101 		}
7102 	}
7103 
7104 	/* Get the bus address of Bar0 and Bar2 and the number of bytes
7105 	 * required by each mapping.
7106 	 */
7107 	phba->pci_bar0_map = pci_resource_start(pdev, 0);
7108 	bar0map_len = pci_resource_len(pdev, 0);
7109 
7110 	phba->pci_bar2_map = pci_resource_start(pdev, 2);
7111 	bar2map_len = pci_resource_len(pdev, 2);
7112 
7113 	/* Map HBA SLIM to a kernel virtual address. */
7114 	phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7115 	if (!phba->slim_memmap_p) {
7116 		dev_printk(KERN_ERR, &pdev->dev,
7117 			   "ioremap failed for SLIM memory.\n");
7118 		goto out;
7119 	}
7120 
7121 	/* Map HBA Control Registers to a kernel virtual address. */
7122 	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7123 	if (!phba->ctrl_regs_memmap_p) {
7124 		dev_printk(KERN_ERR, &pdev->dev,
7125 			   "ioremap failed for HBA control registers.\n");
7126 		goto out_iounmap_slim;
7127 	}
7128 
7129 	/* Allocate memory for SLI-2 structures */
7130 	phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7131 						&phba->slim2p.phys, GFP_KERNEL);
7132 	if (!phba->slim2p.virt)
7133 		goto out_iounmap;
7134 
7135 	phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
7136 	phba->mbox_ext = (phba->slim2p.virt +
7137 		offsetof(struct lpfc_sli2_slim, mbx_ext_words));
7138 	phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7139 	phba->IOCBs = (phba->slim2p.virt +
7140 		       offsetof(struct lpfc_sli2_slim, IOCBs));
7141 
7142 	phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7143 						 lpfc_sli_hbq_size(),
7144 						 &phba->hbqslimp.phys,
7145 						 GFP_KERNEL);
7146 	if (!phba->hbqslimp.virt)
7147 		goto out_free_slim;
7148 
7149 	hbq_count = lpfc_sli_hbq_count();
7150 	ptr = phba->hbqslimp.virt;
7151 	for (i = 0; i < hbq_count; ++i) {
7152 		phba->hbqs[i].hbq_virt = ptr;
7153 		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7154 		ptr += (lpfc_hbq_defs[i]->entry_count *
7155 			sizeof(struct lpfc_hbq_entry));
7156 	}
7157 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7158 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7159 
7160 	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7161 
7162 	phba->MBslimaddr = phba->slim_memmap_p;
7163 	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7164 	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7165 	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7166 	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
7167 
7168 	return 0;
7169 
7170 out_free_slim:
7171 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7172 			  phba->slim2p.virt, phba->slim2p.phys);
7173 out_iounmap:
7174 	iounmap(phba->ctrl_regs_memmap_p);
7175 out_iounmap_slim:
7176 	iounmap(phba->slim_memmap_p);
7177 out:
7178 	return error;
7179 }
7180 
7181 /**
7182  * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7183  * @phba: pointer to lpfc hba data structure.
7184  *
7185  * This routine is invoked to unset the PCI device memory space for device
7186  * with SLI-3 interface spec.
7187  **/
7188 static void
7189 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7190 {
7191 	struct pci_dev *pdev;
7192 
7193 	/* Obtain PCI device reference */
7194 	if (!phba->pcidev)
7195 		return;
7196 	else
7197 		pdev = phba->pcidev;
7198 
7199 	/* Free coherent DMA memory allocated */
7200 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7201 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
7202 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7203 			  phba->slim2p.virt, phba->slim2p.phys);
7204 
7205 	/* I/O memory unmap */
7206 	iounmap(phba->ctrl_regs_memmap_p);
7207 	iounmap(phba->slim_memmap_p);
7208 
7209 	return;
7210 }
7211 
7212 /**
7213  * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7214  * @phba: pointer to lpfc hba data structure.
7215  *
7216  * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7217  * done and check status.
7218  *
7219  * Return 0 if successful, otherwise -ENODEV.
7220  **/
7221 int
7222 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7223 {
7224 	struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7225 	struct lpfc_register reg_data;
7226 	int i, port_error = 0;
7227 	uint32_t if_type;
7228 
7229 	memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7230 	memset(&reg_data, 0, sizeof(reg_data));
7231 	if (!phba->sli4_hba.PSMPHRregaddr)
7232 		return -ENODEV;
7233 
7234 	/* Wait up to 30 seconds for the SLI Port POST done and ready */
7235 	for (i = 0; i < 3000; i++) {
7236 		if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7237 			&portsmphr_reg.word0) ||
7238 			(bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
7239 			/* Port has a fatal POST error, break out */
7240 			port_error = -ENODEV;
7241 			break;
7242 		}
7243 		if (LPFC_POST_STAGE_PORT_READY ==
7244 		    bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
7245 			break;
7246 		msleep(10);
7247 	}
7248 
7249 	/*
7250 	 * If there was a port error during POST, then don't proceed with
7251 	 * other register reads as the data may not be valid.  Just exit.
7252 	 */
7253 	if (port_error) {
7254 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7255 			"1408 Port Failed POST - portsmphr=0x%x, "
7256 			"perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7257 			"scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7258 			portsmphr_reg.word0,
7259 			bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7260 			bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7261 			bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7262 			bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7263 			bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7264 			bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7265 			bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7266 			bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7267 	} else {
7268 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7269 				"2534 Device Info: SLIFamily=0x%x, "
7270 				"SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7271 				"SLIHint_2=0x%x, FT=0x%x\n",
7272 				bf_get(lpfc_sli_intf_sli_family,
7273 				       &phba->sli4_hba.sli_intf),
7274 				bf_get(lpfc_sli_intf_slirev,
7275 				       &phba->sli4_hba.sli_intf),
7276 				bf_get(lpfc_sli_intf_if_type,
7277 				       &phba->sli4_hba.sli_intf),
7278 				bf_get(lpfc_sli_intf_sli_hint1,
7279 				       &phba->sli4_hba.sli_intf),
7280 				bf_get(lpfc_sli_intf_sli_hint2,
7281 				       &phba->sli4_hba.sli_intf),
7282 				bf_get(lpfc_sli_intf_func_type,
7283 				       &phba->sli4_hba.sli_intf));
7284 		/*
7285 		 * Check for other Port errors during the initialization
7286 		 * process.  Fail the load if the port did not come up
7287 		 * correctly.
7288 		 */
7289 		if_type = bf_get(lpfc_sli_intf_if_type,
7290 				 &phba->sli4_hba.sli_intf);
7291 		switch (if_type) {
7292 		case LPFC_SLI_INTF_IF_TYPE_0:
7293 			phba->sli4_hba.ue_mask_lo =
7294 			      readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7295 			phba->sli4_hba.ue_mask_hi =
7296 			      readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7297 			uerrlo_reg.word0 =
7298 			      readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7299 			uerrhi_reg.word0 =
7300 				readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7301 			if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7302 			    (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7303 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7304 						"1422 Unrecoverable Error "
7305 						"Detected during POST "
7306 						"uerr_lo_reg=0x%x, "
7307 						"uerr_hi_reg=0x%x, "
7308 						"ue_mask_lo_reg=0x%x, "
7309 						"ue_mask_hi_reg=0x%x\n",
7310 						uerrlo_reg.word0,
7311 						uerrhi_reg.word0,
7312 						phba->sli4_hba.ue_mask_lo,
7313 						phba->sli4_hba.ue_mask_hi);
7314 				port_error = -ENODEV;
7315 			}
7316 			break;
7317 		case LPFC_SLI_INTF_IF_TYPE_2:
7318 			/* Final checks.  The port status should be clean. */
7319 			if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7320 				&reg_data.word0) ||
7321 				(bf_get(lpfc_sliport_status_err, &reg_data) &&
7322 				 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
7323 				phba->work_status[0] =
7324 					readl(phba->sli4_hba.u.if_type2.
7325 					      ERR1regaddr);
7326 				phba->work_status[1] =
7327 					readl(phba->sli4_hba.u.if_type2.
7328 					      ERR2regaddr);
7329 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7330 					"2888 Unrecoverable port error "
7331 					"following POST: port status reg "
7332 					"0x%x, port_smphr reg 0x%x, "
7333 					"error 1=0x%x, error 2=0x%x\n",
7334 					reg_data.word0,
7335 					portsmphr_reg.word0,
7336 					phba->work_status[0],
7337 					phba->work_status[1]);
7338 				port_error = -ENODEV;
7339 			}
7340 			break;
7341 		case LPFC_SLI_INTF_IF_TYPE_1:
7342 		default:
7343 			break;
7344 		}
7345 	}
7346 	return port_error;
7347 }
7348 
7349 /**
7350  * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7351  * @phba: pointer to lpfc hba data structure.
7352  * @if_type:  The SLI4 interface type getting configured.
7353  *
7354  * This routine is invoked to set up SLI4 BAR0 PCI config space register
7355  * memory map.
7356  **/
7357 static void
7358 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
7359 {
7360 	switch (if_type) {
7361 	case LPFC_SLI_INTF_IF_TYPE_0:
7362 		phba->sli4_hba.u.if_type0.UERRLOregaddr =
7363 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7364 		phba->sli4_hba.u.if_type0.UERRHIregaddr =
7365 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7366 		phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7367 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7368 		phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7369 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7370 		phba->sli4_hba.SLIINTFregaddr =
7371 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7372 		break;
7373 	case LPFC_SLI_INTF_IF_TYPE_2:
7374 		phba->sli4_hba.u.if_type2.EQDregaddr =
7375 			phba->sli4_hba.conf_regs_memmap_p +
7376 						LPFC_CTL_PORT_EQ_DELAY_OFFSET;
7377 		phba->sli4_hba.u.if_type2.ERR1regaddr =
7378 			phba->sli4_hba.conf_regs_memmap_p +
7379 						LPFC_CTL_PORT_ER1_OFFSET;
7380 		phba->sli4_hba.u.if_type2.ERR2regaddr =
7381 			phba->sli4_hba.conf_regs_memmap_p +
7382 						LPFC_CTL_PORT_ER2_OFFSET;
7383 		phba->sli4_hba.u.if_type2.CTRLregaddr =
7384 			phba->sli4_hba.conf_regs_memmap_p +
7385 						LPFC_CTL_PORT_CTL_OFFSET;
7386 		phba->sli4_hba.u.if_type2.STATUSregaddr =
7387 			phba->sli4_hba.conf_regs_memmap_p +
7388 						LPFC_CTL_PORT_STA_OFFSET;
7389 		phba->sli4_hba.SLIINTFregaddr =
7390 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7391 		phba->sli4_hba.PSMPHRregaddr =
7392 			phba->sli4_hba.conf_regs_memmap_p +
7393 						LPFC_CTL_PORT_SEM_OFFSET;
7394 		phba->sli4_hba.RQDBregaddr =
7395 			phba->sli4_hba.conf_regs_memmap_p +
7396 						LPFC_ULP0_RQ_DOORBELL;
7397 		phba->sli4_hba.WQDBregaddr =
7398 			phba->sli4_hba.conf_regs_memmap_p +
7399 						LPFC_ULP0_WQ_DOORBELL;
7400 		phba->sli4_hba.EQCQDBregaddr =
7401 			phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7402 		phba->sli4_hba.MQDBregaddr =
7403 			phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7404 		phba->sli4_hba.BMBXregaddr =
7405 			phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7406 		break;
7407 	case LPFC_SLI_INTF_IF_TYPE_1:
7408 	default:
7409 		dev_printk(KERN_ERR, &phba->pcidev->dev,
7410 			   "FATAL - unsupported SLI4 interface type - %d\n",
7411 			   if_type);
7412 		break;
7413 	}
7414 }
7415 
7416 /**
7417  * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7418  * @phba: pointer to lpfc hba data structure.
7419  *
7420  * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
7421  * memory map.
7422  **/
7423 static void
7424 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
7425 {
7426 	phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7427 		LPFC_SLIPORT_IF0_SMPHR;
7428 	phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7429 		LPFC_HST_ISR0;
7430 	phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7431 		LPFC_HST_IMR0;
7432 	phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7433 		LPFC_HST_ISCR0;
7434 }
7435 
7436 /**
7437  * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7438  * @phba: pointer to lpfc hba data structure.
7439  * @vf: virtual function number
7440  *
7441  * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7442  * based on the given viftual function number, @vf.
7443  *
7444  * Return 0 if successful, otherwise -ENODEV.
7445  **/
7446 static int
7447 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7448 {
7449 	if (vf > LPFC_VIR_FUNC_MAX)
7450 		return -ENODEV;
7451 
7452 	phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7453 				vf * LPFC_VFR_PAGE_SIZE +
7454 					LPFC_ULP0_RQ_DOORBELL);
7455 	phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7456 				vf * LPFC_VFR_PAGE_SIZE +
7457 					LPFC_ULP0_WQ_DOORBELL);
7458 	phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7459 				vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
7460 	phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7461 				vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7462 	phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7463 				vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7464 	return 0;
7465 }
7466 
7467 /**
7468  * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7469  * @phba: pointer to lpfc hba data structure.
7470  *
7471  * This routine is invoked to create the bootstrap mailbox
7472  * region consistent with the SLI-4 interface spec.  This
7473  * routine allocates all memory necessary to communicate
7474  * mailbox commands to the port and sets up all alignment
7475  * needs.  No locks are expected to be held when calling
7476  * this routine.
7477  *
7478  * Return codes
7479  * 	0 - successful
7480  * 	-ENOMEM - could not allocated memory.
7481  **/
7482 static int
7483 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7484 {
7485 	uint32_t bmbx_size;
7486 	struct lpfc_dmabuf *dmabuf;
7487 	struct dma_address *dma_address;
7488 	uint32_t pa_addr;
7489 	uint64_t phys_addr;
7490 
7491 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7492 	if (!dmabuf)
7493 		return -ENOMEM;
7494 
7495 	/*
7496 	 * The bootstrap mailbox region is comprised of 2 parts
7497 	 * plus an alignment restriction of 16 bytes.
7498 	 */
7499 	bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
7500 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7501 					   &dmabuf->phys, GFP_KERNEL);
7502 	if (!dmabuf->virt) {
7503 		kfree(dmabuf);
7504 		return -ENOMEM;
7505 	}
7506 
7507 	/*
7508 	 * Initialize the bootstrap mailbox pointers now so that the register
7509 	 * operations are simple later.  The mailbox dma address is required
7510 	 * to be 16-byte aligned.  Also align the virtual memory as each
7511 	 * maibox is copied into the bmbx mailbox region before issuing the
7512 	 * command to the port.
7513 	 */
7514 	phba->sli4_hba.bmbx.dmabuf = dmabuf;
7515 	phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7516 
7517 	phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7518 					      LPFC_ALIGN_16_BYTE);
7519 	phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7520 					      LPFC_ALIGN_16_BYTE);
7521 
7522 	/*
7523 	 * Set the high and low physical addresses now.  The SLI4 alignment
7524 	 * requirement is 16 bytes and the mailbox is posted to the port
7525 	 * as two 30-bit addresses.  The other data is a bit marking whether
7526 	 * the 30-bit address is the high or low address.
7527 	 * Upcast bmbx aphys to 64bits so shift instruction compiles
7528 	 * clean on 32 bit machines.
7529 	 */
7530 	dma_address = &phba->sli4_hba.bmbx.dma_address;
7531 	phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7532 	pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7533 	dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7534 					   LPFC_BMBX_BIT1_ADDR_HI);
7535 
7536 	pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7537 	dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7538 					   LPFC_BMBX_BIT1_ADDR_LO);
7539 	return 0;
7540 }
7541 
7542 /**
7543  * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7544  * @phba: pointer to lpfc hba data structure.
7545  *
7546  * This routine is invoked to teardown the bootstrap mailbox
7547  * region and release all host resources. This routine requires
7548  * the caller to ensure all mailbox commands recovered, no
7549  * additional mailbox comands are sent, and interrupts are disabled
7550  * before calling this routine.
7551  *
7552  **/
7553 static void
7554 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7555 {
7556 	dma_free_coherent(&phba->pcidev->dev,
7557 			  phba->sli4_hba.bmbx.bmbx_size,
7558 			  phba->sli4_hba.bmbx.dmabuf->virt,
7559 			  phba->sli4_hba.bmbx.dmabuf->phys);
7560 
7561 	kfree(phba->sli4_hba.bmbx.dmabuf);
7562 	memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7563 }
7564 
7565 /**
7566  * lpfc_sli4_read_config - Get the config parameters.
7567  * @phba: pointer to lpfc hba data structure.
7568  *
7569  * This routine is invoked to read the configuration parameters from the HBA.
7570  * The configuration parameters are used to set the base and maximum values
7571  * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7572  * allocation for the port.
7573  *
7574  * Return codes
7575  * 	0 - successful
7576  * 	-ENOMEM - No available memory
7577  *      -EIO - The mailbox failed to complete successfully.
7578  **/
7579 int
7580 lpfc_sli4_read_config(struct lpfc_hba *phba)
7581 {
7582 	LPFC_MBOXQ_t *pmb;
7583 	struct lpfc_mbx_read_config *rd_config;
7584 	union  lpfc_sli4_cfg_shdr *shdr;
7585 	uint32_t shdr_status, shdr_add_status;
7586 	struct lpfc_mbx_get_func_cfg *get_func_cfg;
7587 	struct lpfc_rsrc_desc_fcfcoe *desc;
7588 	char *pdesc_0;
7589 	uint16_t forced_link_speed;
7590 	uint32_t if_type;
7591 	int length, i, rc = 0, rc2;
7592 
7593 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7594 	if (!pmb) {
7595 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7596 				"2011 Unable to allocate memory for issuing "
7597 				"SLI_CONFIG_SPECIAL mailbox command\n");
7598 		return -ENOMEM;
7599 	}
7600 
7601 	lpfc_read_config(phba, pmb);
7602 
7603 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7604 	if (rc != MBX_SUCCESS) {
7605 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7606 			"2012 Mailbox failed , mbxCmd x%x "
7607 			"READ_CONFIG, mbxStatus x%x\n",
7608 			bf_get(lpfc_mqe_command, &pmb->u.mqe),
7609 			bf_get(lpfc_mqe_status, &pmb->u.mqe));
7610 		rc = -EIO;
7611 	} else {
7612 		rd_config = &pmb->u.mqe.un.rd_config;
7613 		if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7614 			phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7615 			phba->sli4_hba.lnk_info.lnk_tp =
7616 				bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7617 			phba->sli4_hba.lnk_info.lnk_no =
7618 				bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7619 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7620 					"3081 lnk_type:%d, lnk_numb:%d\n",
7621 					phba->sli4_hba.lnk_info.lnk_tp,
7622 					phba->sli4_hba.lnk_info.lnk_no);
7623 		} else
7624 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7625 					"3082 Mailbox (x%x) returned ldv:x0\n",
7626 					bf_get(lpfc_mqe_command, &pmb->u.mqe));
7627 		if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
7628 			phba->bbcredit_support = 1;
7629 			phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
7630 		}
7631 
7632 		phba->sli4_hba.extents_in_use =
7633 			bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
7634 		phba->sli4_hba.max_cfg_param.max_xri =
7635 			bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7636 		phba->sli4_hba.max_cfg_param.xri_base =
7637 			bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7638 		phba->sli4_hba.max_cfg_param.max_vpi =
7639 			bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7640 		phba->sli4_hba.max_cfg_param.vpi_base =
7641 			bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7642 		phba->sli4_hba.max_cfg_param.max_rpi =
7643 			bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7644 		phba->sli4_hba.max_cfg_param.rpi_base =
7645 			bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7646 		phba->sli4_hba.max_cfg_param.max_vfi =
7647 			bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7648 		phba->sli4_hba.max_cfg_param.vfi_base =
7649 			bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7650 		phba->sli4_hba.max_cfg_param.max_fcfi =
7651 			bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
7652 		phba->sli4_hba.max_cfg_param.max_eq =
7653 			bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7654 		phba->sli4_hba.max_cfg_param.max_rq =
7655 			bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7656 		phba->sli4_hba.max_cfg_param.max_wq =
7657 			bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7658 		phba->sli4_hba.max_cfg_param.max_cq =
7659 			bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7660 		phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7661 		phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7662 		phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7663 		phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
7664 		phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7665 				(phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
7666 		phba->max_vports = phba->max_vpi;
7667 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7668 				"2003 cfg params Extents? %d "
7669 				"XRI(B:%d M:%d), "
7670 				"VPI(B:%d M:%d) "
7671 				"VFI(B:%d M:%d) "
7672 				"RPI(B:%d M:%d) "
7673 				"FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
7674 				phba->sli4_hba.extents_in_use,
7675 				phba->sli4_hba.max_cfg_param.xri_base,
7676 				phba->sli4_hba.max_cfg_param.max_xri,
7677 				phba->sli4_hba.max_cfg_param.vpi_base,
7678 				phba->sli4_hba.max_cfg_param.max_vpi,
7679 				phba->sli4_hba.max_cfg_param.vfi_base,
7680 				phba->sli4_hba.max_cfg_param.max_vfi,
7681 				phba->sli4_hba.max_cfg_param.rpi_base,
7682 				phba->sli4_hba.max_cfg_param.max_rpi,
7683 				phba->sli4_hba.max_cfg_param.max_fcfi,
7684 				phba->sli4_hba.max_cfg_param.max_eq,
7685 				phba->sli4_hba.max_cfg_param.max_cq,
7686 				phba->sli4_hba.max_cfg_param.max_wq,
7687 				phba->sli4_hba.max_cfg_param.max_rq);
7688 
7689 	}
7690 
7691 	if (rc)
7692 		goto read_cfg_out;
7693 
7694 	/* Update link speed if forced link speed is supported */
7695 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7696 	if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7697 		forced_link_speed =
7698 			bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7699 		if (forced_link_speed) {
7700 			phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7701 
7702 			switch (forced_link_speed) {
7703 			case LINK_SPEED_1G:
7704 				phba->cfg_link_speed =
7705 					LPFC_USER_LINK_SPEED_1G;
7706 				break;
7707 			case LINK_SPEED_2G:
7708 				phba->cfg_link_speed =
7709 					LPFC_USER_LINK_SPEED_2G;
7710 				break;
7711 			case LINK_SPEED_4G:
7712 				phba->cfg_link_speed =
7713 					LPFC_USER_LINK_SPEED_4G;
7714 				break;
7715 			case LINK_SPEED_8G:
7716 				phba->cfg_link_speed =
7717 					LPFC_USER_LINK_SPEED_8G;
7718 				break;
7719 			case LINK_SPEED_10G:
7720 				phba->cfg_link_speed =
7721 					LPFC_USER_LINK_SPEED_10G;
7722 				break;
7723 			case LINK_SPEED_16G:
7724 				phba->cfg_link_speed =
7725 					LPFC_USER_LINK_SPEED_16G;
7726 				break;
7727 			case LINK_SPEED_32G:
7728 				phba->cfg_link_speed =
7729 					LPFC_USER_LINK_SPEED_32G;
7730 				break;
7731 			case 0xffff:
7732 				phba->cfg_link_speed =
7733 					LPFC_USER_LINK_SPEED_AUTO;
7734 				break;
7735 			default:
7736 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7737 						"0047 Unrecognized link "
7738 						"speed : %d\n",
7739 						forced_link_speed);
7740 				phba->cfg_link_speed =
7741 					LPFC_USER_LINK_SPEED_AUTO;
7742 			}
7743 		}
7744 	}
7745 
7746 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
7747 	length = phba->sli4_hba.max_cfg_param.max_xri -
7748 			lpfc_sli4_get_els_iocb_cnt(phba);
7749 	if (phba->cfg_hba_queue_depth > length) {
7750 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7751 				"3361 HBA queue depth changed from %d to %d\n",
7752 				phba->cfg_hba_queue_depth, length);
7753 		phba->cfg_hba_queue_depth = length;
7754 	}
7755 
7756 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7757 	    LPFC_SLI_INTF_IF_TYPE_2)
7758 		goto read_cfg_out;
7759 
7760 	/* get the pf# and vf# for SLI4 if_type 2 port */
7761 	length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7762 		  sizeof(struct lpfc_sli4_cfg_mhdr));
7763 	lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7764 			 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7765 			 length, LPFC_SLI4_MBX_EMBED);
7766 
7767 	rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7768 	shdr = (union lpfc_sli4_cfg_shdr *)
7769 				&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7770 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7771 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7772 	if (rc2 || shdr_status || shdr_add_status) {
7773 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7774 				"3026 Mailbox failed , mbxCmd x%x "
7775 				"GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7776 				bf_get(lpfc_mqe_command, &pmb->u.mqe),
7777 				bf_get(lpfc_mqe_status, &pmb->u.mqe));
7778 		goto read_cfg_out;
7779 	}
7780 
7781 	/* search for fc_fcoe resrouce descriptor */
7782 	get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
7783 
7784 	pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7785 	desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7786 	length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7787 	if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7788 		length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7789 	else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7790 		goto read_cfg_out;
7791 
7792 	for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
7793 		desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
7794 		if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
7795 		    bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
7796 			phba->sli4_hba.iov.pf_number =
7797 				bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7798 			phba->sli4_hba.iov.vf_number =
7799 				bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7800 			break;
7801 		}
7802 	}
7803 
7804 	if (i < LPFC_RSRC_DESC_MAX_NUM)
7805 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7806 				"3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7807 				"vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7808 				phba->sli4_hba.iov.vf_number);
7809 	else
7810 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7811 				"3028 GET_FUNCTION_CONFIG: failed to find "
7812 				"Resrouce Descriptor:x%x\n",
7813 				LPFC_RSRC_DESC_TYPE_FCFCOE);
7814 
7815 read_cfg_out:
7816 	mempool_free(pmb, phba->mbox_mem_pool);
7817 	return rc;
7818 }
7819 
7820 /**
7821  * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
7822  * @phba: pointer to lpfc hba data structure.
7823  *
7824  * This routine is invoked to setup the port-side endian order when
7825  * the port if_type is 0.  This routine has no function for other
7826  * if_types.
7827  *
7828  * Return codes
7829  * 	0 - successful
7830  * 	-ENOMEM - No available memory
7831  *      -EIO - The mailbox failed to complete successfully.
7832  **/
7833 static int
7834 lpfc_setup_endian_order(struct lpfc_hba *phba)
7835 {
7836 	LPFC_MBOXQ_t *mboxq;
7837 	uint32_t if_type, rc = 0;
7838 	uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7839 				      HOST_ENDIAN_HIGH_WORD1};
7840 
7841 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7842 	switch (if_type) {
7843 	case LPFC_SLI_INTF_IF_TYPE_0:
7844 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7845 						       GFP_KERNEL);
7846 		if (!mboxq) {
7847 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7848 					"0492 Unable to allocate memory for "
7849 					"issuing SLI_CONFIG_SPECIAL mailbox "
7850 					"command\n");
7851 			return -ENOMEM;
7852 		}
7853 
7854 		/*
7855 		 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7856 		 * two words to contain special data values and no other data.
7857 		 */
7858 		memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7859 		memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7860 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7861 		if (rc != MBX_SUCCESS) {
7862 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7863 					"0493 SLI_CONFIG_SPECIAL mailbox "
7864 					"failed with status x%x\n",
7865 					rc);
7866 			rc = -EIO;
7867 		}
7868 		mempool_free(mboxq, phba->mbox_mem_pool);
7869 		break;
7870 	case LPFC_SLI_INTF_IF_TYPE_2:
7871 	case LPFC_SLI_INTF_IF_TYPE_1:
7872 	default:
7873 		break;
7874 	}
7875 	return rc;
7876 }
7877 
7878 /**
7879  * lpfc_sli4_queue_verify - Verify and update EQ counts
7880  * @phba: pointer to lpfc hba data structure.
7881  *
7882  * This routine is invoked to check the user settable queue counts for EQs.
7883  * After this routine is called the counts will be set to valid values that
7884  * adhere to the constraints of the system's interrupt vectors and the port's
7885  * queue resources.
7886  *
7887  * Return codes
7888  *      0 - successful
7889  *      -ENOMEM - No available memory
7890  **/
7891 static int
7892 lpfc_sli4_queue_verify(struct lpfc_hba *phba)
7893 {
7894 	int io_channel;
7895 	int fof_vectors = phba->cfg_fof ? 1 : 0;
7896 
7897 	/*
7898 	 * Sanity check for configured queue parameters against the run-time
7899 	 * device parameters
7900 	 */
7901 
7902 	/* Sanity check on HBA EQ parameters */
7903 	io_channel = phba->io_channel_irqs;
7904 
7905 	if (phba->sli4_hba.num_online_cpu < io_channel) {
7906 		lpfc_printf_log(phba,
7907 				KERN_ERR, LOG_INIT,
7908 				"3188 Reducing IO channels to match number of "
7909 				"online CPUs: from %d to %d\n",
7910 				io_channel, phba->sli4_hba.num_online_cpu);
7911 		io_channel = phba->sli4_hba.num_online_cpu;
7912 	}
7913 
7914 	if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
7915 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7916 				"2575 Reducing IO channels to match number of "
7917 				"available EQs: from %d to %d\n",
7918 				io_channel,
7919 				phba->sli4_hba.max_cfg_param.max_eq);
7920 		io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
7921 	}
7922 
7923 	/* The actual number of FCP / NVME event queues adopted */
7924 	if (io_channel != phba->io_channel_irqs)
7925 		phba->io_channel_irqs = io_channel;
7926 	if (phba->cfg_fcp_io_channel > io_channel)
7927 		phba->cfg_fcp_io_channel = io_channel;
7928 	if (phba->cfg_nvme_io_channel > io_channel)
7929 		phba->cfg_nvme_io_channel = io_channel;
7930 	if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
7931 		phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
7932 
7933 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7934 			"2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
7935 			phba->io_channel_irqs, phba->cfg_fcp_io_channel,
7936 			phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
7937 
7938 	/* Get EQ depth from module parameter, fake the default for now */
7939 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7940 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7941 
7942 	/* Get CQ depth from module parameter, fake the default for now */
7943 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7944 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
7945 	return 0;
7946 }
7947 
7948 static int
7949 lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
7950 {
7951 	struct lpfc_queue *qdesc;
7952 	int cnt;
7953 
7954 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7955 					    phba->sli4_hba.cq_ecount);
7956 	if (!qdesc) {
7957 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7958 				"0508 Failed allocate fast-path NVME CQ (%d)\n",
7959 				wqidx);
7960 		return 1;
7961 	}
7962 	phba->sli4_hba.nvme_cq[wqidx] = qdesc;
7963 
7964 	cnt = LPFC_NVME_WQSIZE;
7965 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_WQE128_SIZE, cnt);
7966 	if (!qdesc) {
7967 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7968 				"0509 Failed allocate fast-path NVME WQ (%d)\n",
7969 				wqidx);
7970 		return 1;
7971 	}
7972 	phba->sli4_hba.nvme_wq[wqidx] = qdesc;
7973 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7974 	return 0;
7975 }
7976 
7977 static int
7978 lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
7979 {
7980 	struct lpfc_queue *qdesc;
7981 	uint32_t wqesize;
7982 
7983 	/* Create Fast Path FCP CQs */
7984 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7985 					phba->sli4_hba.cq_ecount);
7986 	if (!qdesc) {
7987 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7988 			"0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
7989 		return 1;
7990 	}
7991 	phba->sli4_hba.fcp_cq[wqidx] = qdesc;
7992 
7993 	/* Create Fast Path FCP WQs */
7994 	wqesize = (phba->fcp_embed_io) ?
7995 		LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
7996 	qdesc = lpfc_sli4_queue_alloc(phba, wqesize, phba->sli4_hba.wq_ecount);
7997 	if (!qdesc) {
7998 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7999 				"0503 Failed allocate fast-path FCP WQ (%d)\n",
8000 				wqidx);
8001 		return 1;
8002 	}
8003 	phba->sli4_hba.fcp_wq[wqidx] = qdesc;
8004 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8005 	return 0;
8006 }
8007 
8008 /**
8009  * lpfc_sli4_queue_create - Create all the SLI4 queues
8010  * @phba: pointer to lpfc hba data structure.
8011  *
8012  * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8013  * operation. For each SLI4 queue type, the parameters such as queue entry
8014  * count (queue depth) shall be taken from the module parameter. For now,
8015  * we just use some constant number as place holder.
8016  *
8017  * Return codes
8018  *      0 - successful
8019  *      -ENOMEM - No availble memory
8020  *      -EIO - The mailbox failed to complete successfully.
8021  **/
8022 int
8023 lpfc_sli4_queue_create(struct lpfc_hba *phba)
8024 {
8025 	struct lpfc_queue *qdesc;
8026 	int idx, io_channel;
8027 
8028 	/*
8029 	 * Create HBA Record arrays.
8030 	 * Both NVME and FCP will share that same vectors / EQs
8031 	 */
8032 	io_channel = phba->io_channel_irqs;
8033 	if (!io_channel)
8034 		return -ERANGE;
8035 
8036 	phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8037 	phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8038 	phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8039 	phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8040 	phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8041 	phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
8042 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8043 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8044 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8045 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
8046 
8047 	phba->sli4_hba.hba_eq =  kcalloc(io_channel,
8048 					sizeof(struct lpfc_queue *),
8049 					GFP_KERNEL);
8050 	if (!phba->sli4_hba.hba_eq) {
8051 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8052 			"2576 Failed allocate memory for "
8053 			"fast-path EQ record array\n");
8054 		goto out_error;
8055 	}
8056 
8057 	if (phba->cfg_fcp_io_channel) {
8058 		phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
8059 						sizeof(struct lpfc_queue *),
8060 						GFP_KERNEL);
8061 		if (!phba->sli4_hba.fcp_cq) {
8062 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8063 					"2577 Failed allocate memory for "
8064 					"fast-path CQ record array\n");
8065 			goto out_error;
8066 		}
8067 		phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
8068 						sizeof(struct lpfc_queue *),
8069 						GFP_KERNEL);
8070 		if (!phba->sli4_hba.fcp_wq) {
8071 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8072 					"2578 Failed allocate memory for "
8073 					"fast-path FCP WQ record array\n");
8074 			goto out_error;
8075 		}
8076 		/*
8077 		 * Since the first EQ can have multiple CQs associated with it,
8078 		 * this array is used to quickly see if we have a FCP fast-path
8079 		 * CQ match.
8080 		 */
8081 		phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
8082 							sizeof(uint16_t),
8083 							GFP_KERNEL);
8084 		if (!phba->sli4_hba.fcp_cq_map) {
8085 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8086 					"2545 Failed allocate memory for "
8087 					"fast-path CQ map\n");
8088 			goto out_error;
8089 		}
8090 	}
8091 
8092 	if (phba->cfg_nvme_io_channel) {
8093 		phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
8094 						sizeof(struct lpfc_queue *),
8095 						GFP_KERNEL);
8096 		if (!phba->sli4_hba.nvme_cq) {
8097 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8098 					"6077 Failed allocate memory for "
8099 					"fast-path CQ record array\n");
8100 			goto out_error;
8101 		}
8102 
8103 		phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
8104 						sizeof(struct lpfc_queue *),
8105 						GFP_KERNEL);
8106 		if (!phba->sli4_hba.nvme_wq) {
8107 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8108 					"2581 Failed allocate memory for "
8109 					"fast-path NVME WQ record array\n");
8110 			goto out_error;
8111 		}
8112 
8113 		/*
8114 		 * Since the first EQ can have multiple CQs associated with it,
8115 		 * this array is used to quickly see if we have a NVME fast-path
8116 		 * CQ match.
8117 		 */
8118 		phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
8119 							sizeof(uint16_t),
8120 							GFP_KERNEL);
8121 		if (!phba->sli4_hba.nvme_cq_map) {
8122 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8123 					"6078 Failed allocate memory for "
8124 					"fast-path CQ map\n");
8125 			goto out_error;
8126 		}
8127 
8128 		if (phba->nvmet_support) {
8129 			phba->sli4_hba.nvmet_cqset = kcalloc(
8130 					phba->cfg_nvmet_mrq,
8131 					sizeof(struct lpfc_queue *),
8132 					GFP_KERNEL);
8133 			if (!phba->sli4_hba.nvmet_cqset) {
8134 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8135 					"3121 Fail allocate memory for "
8136 					"fast-path CQ set array\n");
8137 				goto out_error;
8138 			}
8139 			phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8140 					phba->cfg_nvmet_mrq,
8141 					sizeof(struct lpfc_queue *),
8142 					GFP_KERNEL);
8143 			if (!phba->sli4_hba.nvmet_mrq_hdr) {
8144 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8145 					"3122 Fail allocate memory for "
8146 					"fast-path RQ set hdr array\n");
8147 				goto out_error;
8148 			}
8149 			phba->sli4_hba.nvmet_mrq_data = kcalloc(
8150 					phba->cfg_nvmet_mrq,
8151 					sizeof(struct lpfc_queue *),
8152 					GFP_KERNEL);
8153 			if (!phba->sli4_hba.nvmet_mrq_data) {
8154 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8155 					"3124 Fail allocate memory for "
8156 					"fast-path RQ set data array\n");
8157 				goto out_error;
8158 			}
8159 		}
8160 	}
8161 
8162 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8163 
8164 	/* Create HBA Event Queues (EQs) */
8165 	for (idx = 0; idx < io_channel; idx++) {
8166 		/* Create EQs */
8167 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
8168 					      phba->sli4_hba.eq_ecount);
8169 		if (!qdesc) {
8170 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8171 					"0497 Failed allocate EQ (%d)\n", idx);
8172 			goto out_error;
8173 		}
8174 		phba->sli4_hba.hba_eq[idx] = qdesc;
8175 	}
8176 
8177 	/* FCP and NVME io channels are not required to be balanced */
8178 
8179 	for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8180 		if (lpfc_alloc_fcp_wq_cq(phba, idx))
8181 			goto out_error;
8182 
8183 	for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
8184 		if (lpfc_alloc_nvme_wq_cq(phba, idx))
8185 			goto out_error;
8186 
8187 	if (phba->nvmet_support) {
8188 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8189 			qdesc = lpfc_sli4_queue_alloc(phba,
8190 					phba->sli4_hba.cq_esize,
8191 					phba->sli4_hba.cq_ecount);
8192 			if (!qdesc) {
8193 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8194 					"3142 Failed allocate NVME "
8195 					"CQ Set (%d)\n", idx);
8196 				goto out_error;
8197 			}
8198 			phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8199 		}
8200 	}
8201 
8202 	/*
8203 	 * Create Slow Path Completion Queues (CQs)
8204 	 */
8205 
8206 	/* Create slow-path Mailbox Command Complete Queue */
8207 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8208 				      phba->sli4_hba.cq_ecount);
8209 	if (!qdesc) {
8210 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8211 				"0500 Failed allocate slow-path mailbox CQ\n");
8212 		goto out_error;
8213 	}
8214 	phba->sli4_hba.mbx_cq = qdesc;
8215 
8216 	/* Create slow-path ELS Complete Queue */
8217 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8218 				      phba->sli4_hba.cq_ecount);
8219 	if (!qdesc) {
8220 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8221 				"0501 Failed allocate slow-path ELS CQ\n");
8222 		goto out_error;
8223 	}
8224 	phba->sli4_hba.els_cq = qdesc;
8225 
8226 
8227 	/*
8228 	 * Create Slow Path Work Queues (WQs)
8229 	 */
8230 
8231 	/* Create Mailbox Command Queue */
8232 
8233 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
8234 				      phba->sli4_hba.mq_ecount);
8235 	if (!qdesc) {
8236 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8237 				"0505 Failed allocate slow-path MQ\n");
8238 		goto out_error;
8239 	}
8240 	phba->sli4_hba.mbx_wq = qdesc;
8241 
8242 	/*
8243 	 * Create ELS Work Queues
8244 	 */
8245 
8246 	/* Create slow-path ELS Work Queue */
8247 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8248 				      phba->sli4_hba.wq_ecount);
8249 	if (!qdesc) {
8250 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8251 				"0504 Failed allocate slow-path ELS WQ\n");
8252 		goto out_error;
8253 	}
8254 	phba->sli4_hba.els_wq = qdesc;
8255 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8256 
8257 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8258 		/* Create NVME LS Complete Queue */
8259 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8260 					      phba->sli4_hba.cq_ecount);
8261 		if (!qdesc) {
8262 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8263 					"6079 Failed allocate NVME LS CQ\n");
8264 			goto out_error;
8265 		}
8266 		phba->sli4_hba.nvmels_cq = qdesc;
8267 
8268 		/* Create NVME LS Work Queue */
8269 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8270 					      phba->sli4_hba.wq_ecount);
8271 		if (!qdesc) {
8272 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8273 					"6080 Failed allocate NVME LS WQ\n");
8274 			goto out_error;
8275 		}
8276 		phba->sli4_hba.nvmels_wq = qdesc;
8277 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8278 	}
8279 
8280 	/*
8281 	 * Create Receive Queue (RQ)
8282 	 */
8283 
8284 	/* Create Receive Queue for header */
8285 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8286 				      phba->sli4_hba.rq_ecount);
8287 	if (!qdesc) {
8288 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8289 				"0506 Failed allocate receive HRQ\n");
8290 		goto out_error;
8291 	}
8292 	phba->sli4_hba.hdr_rq = qdesc;
8293 
8294 	/* Create Receive Queue for data */
8295 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8296 				      phba->sli4_hba.rq_ecount);
8297 	if (!qdesc) {
8298 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8299 				"0507 Failed allocate receive DRQ\n");
8300 		goto out_error;
8301 	}
8302 	phba->sli4_hba.dat_rq = qdesc;
8303 
8304 	if (phba->nvmet_support) {
8305 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8306 			/* Create NVMET Receive Queue for header */
8307 			qdesc = lpfc_sli4_queue_alloc(phba,
8308 						      phba->sli4_hba.rq_esize,
8309 						      LPFC_NVMET_RQE_DEF_COUNT);
8310 			if (!qdesc) {
8311 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8312 						"3146 Failed allocate "
8313 						"receive HRQ\n");
8314 				goto out_error;
8315 			}
8316 			phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8317 
8318 			/* Only needed for header of RQ pair */
8319 			qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8320 					      GFP_KERNEL);
8321 			if (qdesc->rqbp == NULL) {
8322 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8323 						"6131 Failed allocate "
8324 						"Header RQBP\n");
8325 				goto out_error;
8326 			}
8327 
8328 			/* Put list in known state in case driver load fails. */
8329 			INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
8330 
8331 			/* Create NVMET Receive Queue for data */
8332 			qdesc = lpfc_sli4_queue_alloc(phba,
8333 						      phba->sli4_hba.rq_esize,
8334 						      LPFC_NVMET_RQE_DEF_COUNT);
8335 			if (!qdesc) {
8336 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8337 						"3156 Failed allocate "
8338 						"receive DRQ\n");
8339 				goto out_error;
8340 			}
8341 			phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8342 		}
8343 	}
8344 
8345 	/* Create the Queues needed for Flash Optimized Fabric operations */
8346 	if (phba->cfg_fof)
8347 		lpfc_fof_queue_create(phba);
8348 	return 0;
8349 
8350 out_error:
8351 	lpfc_sli4_queue_destroy(phba);
8352 	return -ENOMEM;
8353 }
8354 
8355 static inline void
8356 __lpfc_sli4_release_queue(struct lpfc_queue **qp)
8357 {
8358 	if (*qp != NULL) {
8359 		lpfc_sli4_queue_free(*qp);
8360 		*qp = NULL;
8361 	}
8362 }
8363 
8364 static inline void
8365 lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8366 {
8367 	int idx;
8368 
8369 	if (*qs == NULL)
8370 		return;
8371 
8372 	for (idx = 0; idx < max; idx++)
8373 		__lpfc_sli4_release_queue(&(*qs)[idx]);
8374 
8375 	kfree(*qs);
8376 	*qs = NULL;
8377 }
8378 
8379 static inline void
8380 lpfc_sli4_release_queue_map(uint16_t **qmap)
8381 {
8382 	if (*qmap != NULL) {
8383 		kfree(*qmap);
8384 		*qmap = NULL;
8385 	}
8386 }
8387 
8388 /**
8389  * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8390  * @phba: pointer to lpfc hba data structure.
8391  *
8392  * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8393  * operation.
8394  *
8395  * Return codes
8396  *      0 - successful
8397  *      -ENOMEM - No available memory
8398  *      -EIO - The mailbox failed to complete successfully.
8399  **/
8400 void
8401 lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8402 {
8403 	if (phba->cfg_fof)
8404 		lpfc_fof_queue_destroy(phba);
8405 
8406 	/* Release HBA eqs */
8407 	lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
8408 
8409 	/* Release FCP cqs */
8410 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
8411 				 phba->cfg_fcp_io_channel);
8412 
8413 	/* Release FCP wqs */
8414 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
8415 				 phba->cfg_fcp_io_channel);
8416 
8417 	/* Release FCP CQ mapping array */
8418 	lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8419 
8420 	/* Release NVME cqs */
8421 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8422 					phba->cfg_nvme_io_channel);
8423 
8424 	/* Release NVME wqs */
8425 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8426 					phba->cfg_nvme_io_channel);
8427 
8428 	/* Release NVME CQ mapping array */
8429 	lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
8430 
8431 	lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8432 					phba->cfg_nvmet_mrq);
8433 
8434 	lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8435 					phba->cfg_nvmet_mrq);
8436 	lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8437 					phba->cfg_nvmet_mrq);
8438 
8439 	/* Release mailbox command work queue */
8440 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
8441 
8442 	/* Release ELS work queue */
8443 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8444 
8445 	/* Release ELS work queue */
8446 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
8447 
8448 	/* Release unsolicited receive queue */
8449 	__lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8450 	__lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
8451 
8452 	/* Release ELS complete queue */
8453 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8454 
8455 	/* Release NVME LS complete queue */
8456 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
8457 
8458 	/* Release mailbox command complete queue */
8459 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8460 
8461 	/* Everything on this list has been freed */
8462 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8463 }
8464 
8465 int
8466 lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8467 {
8468 	struct lpfc_rqb *rqbp;
8469 	struct lpfc_dmabuf *h_buf;
8470 	struct rqb_dmabuf *rqb_buffer;
8471 
8472 	rqbp = rq->rqbp;
8473 	while (!list_empty(&rqbp->rqb_buffer_list)) {
8474 		list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8475 				 struct lpfc_dmabuf, list);
8476 
8477 		rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8478 		(rqbp->rqb_free_buffer)(phba, rqb_buffer);
8479 		rqbp->buffer_count--;
8480 	}
8481 	return 1;
8482 }
8483 
8484 static int
8485 lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8486 	struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8487 	int qidx, uint32_t qtype)
8488 {
8489 	struct lpfc_sli_ring *pring;
8490 	int rc;
8491 
8492 	if (!eq || !cq || !wq) {
8493 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8494 			"6085 Fast-path %s (%d) not allocated\n",
8495 			((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8496 		return -ENOMEM;
8497 	}
8498 
8499 	/* create the Cq first */
8500 	rc = lpfc_cq_create(phba, cq, eq,
8501 			(qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8502 	if (rc) {
8503 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8504 			"6086 Failed setup of CQ (%d), rc = 0x%x\n",
8505 			qidx, (uint32_t)rc);
8506 		return rc;
8507 	}
8508 
8509 	if (qtype != LPFC_MBOX) {
8510 		/* Setup nvme_cq_map for fast lookup */
8511 		if (cq_map)
8512 			*cq_map = cq->queue_id;
8513 
8514 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8515 			"6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8516 			qidx, cq->queue_id, qidx, eq->queue_id);
8517 
8518 		/* create the wq */
8519 		rc = lpfc_wq_create(phba, wq, cq, qtype);
8520 		if (rc) {
8521 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8522 				"6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8523 				qidx, (uint32_t)rc);
8524 			/* no need to tear down cq - caller will do so */
8525 			return rc;
8526 		}
8527 
8528 		/* Bind this CQ/WQ to the NVME ring */
8529 		pring = wq->pring;
8530 		pring->sli.sli4.wqp = (void *)wq;
8531 		cq->pring = pring;
8532 
8533 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8534 			"2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8535 			qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8536 	} else {
8537 		rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8538 		if (rc) {
8539 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8540 				"0539 Failed setup of slow-path MQ: "
8541 				"rc = 0x%x\n", rc);
8542 			/* no need to tear down cq - caller will do so */
8543 			return rc;
8544 		}
8545 
8546 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8547 			"2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8548 			phba->sli4_hba.mbx_wq->queue_id,
8549 			phba->sli4_hba.mbx_cq->queue_id);
8550 	}
8551 
8552 	return 0;
8553 }
8554 
8555 /**
8556  * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8557  * @phba: pointer to lpfc hba data structure.
8558  *
8559  * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8560  * operation.
8561  *
8562  * Return codes
8563  *      0 - successful
8564  *      -ENOMEM - No available memory
8565  *      -EIO - The mailbox failed to complete successfully.
8566  **/
8567 int
8568 lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8569 {
8570 	uint32_t shdr_status, shdr_add_status;
8571 	union lpfc_sli4_cfg_shdr *shdr;
8572 	LPFC_MBOXQ_t *mboxq;
8573 	int qidx;
8574 	uint32_t length, io_channel;
8575 	int rc = -ENOMEM;
8576 
8577 	/* Check for dual-ULP support */
8578 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8579 	if (!mboxq) {
8580 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8581 				"3249 Unable to allocate memory for "
8582 				"QUERY_FW_CFG mailbox command\n");
8583 		return -ENOMEM;
8584 	}
8585 	length = (sizeof(struct lpfc_mbx_query_fw_config) -
8586 		  sizeof(struct lpfc_sli4_cfg_mhdr));
8587 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8588 			 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8589 			 length, LPFC_SLI4_MBX_EMBED);
8590 
8591 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8592 
8593 	shdr = (union lpfc_sli4_cfg_shdr *)
8594 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8595 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8596 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8597 	if (shdr_status || shdr_add_status || rc) {
8598 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8599 				"3250 QUERY_FW_CFG mailbox failed with status "
8600 				"x%x add_status x%x, mbx status x%x\n",
8601 				shdr_status, shdr_add_status, rc);
8602 		if (rc != MBX_TIMEOUT)
8603 			mempool_free(mboxq, phba->mbox_mem_pool);
8604 		rc = -ENXIO;
8605 		goto out_error;
8606 	}
8607 
8608 	phba->sli4_hba.fw_func_mode =
8609 			mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8610 	phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8611 	phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
8612 	phba->sli4_hba.physical_port =
8613 			mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
8614 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8615 			"3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8616 			"ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8617 			phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8618 
8619 	if (rc != MBX_TIMEOUT)
8620 		mempool_free(mboxq, phba->mbox_mem_pool);
8621 
8622 	/*
8623 	 * Set up HBA Event Queues (EQs)
8624 	 */
8625 	io_channel = phba->io_channel_irqs;
8626 
8627 	/* Set up HBA event queue */
8628 	if (io_channel && !phba->sli4_hba.hba_eq) {
8629 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8630 				"3147 Fast-path EQs not allocated\n");
8631 		rc = -ENOMEM;
8632 		goto out_error;
8633 	}
8634 	for (qidx = 0; qidx < io_channel; qidx++) {
8635 		if (!phba->sli4_hba.hba_eq[qidx]) {
8636 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8637 					"0522 Fast-path EQ (%d) not "
8638 					"allocated\n", qidx);
8639 			rc = -ENOMEM;
8640 			goto out_destroy;
8641 		}
8642 		rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8643 						phba->cfg_fcp_imax);
8644 		if (rc) {
8645 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8646 					"0523 Failed setup of fast-path EQ "
8647 					"(%d), rc = 0x%x\n", qidx,
8648 					(uint32_t)rc);
8649 			goto out_destroy;
8650 		}
8651 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8652 				"2584 HBA EQ setup: queue[%d]-id=%d\n",
8653 				qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
8654 	}
8655 
8656 	if (phba->cfg_nvme_io_channel) {
8657 		if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
8658 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8659 				"6084 Fast-path NVME %s array not allocated\n",
8660 				(phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
8661 			rc = -ENOMEM;
8662 			goto out_destroy;
8663 		}
8664 
8665 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8666 			rc = lpfc_create_wq_cq(phba,
8667 					phba->sli4_hba.hba_eq[
8668 						qidx % io_channel],
8669 					phba->sli4_hba.nvme_cq[qidx],
8670 					phba->sli4_hba.nvme_wq[qidx],
8671 					&phba->sli4_hba.nvme_cq_map[qidx],
8672 					qidx, LPFC_NVME);
8673 			if (rc) {
8674 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8675 					"6123 Failed to setup fastpath "
8676 					"NVME WQ/CQ (%d), rc = 0x%x\n",
8677 					qidx, (uint32_t)rc);
8678 				goto out_destroy;
8679 			}
8680 		}
8681 	}
8682 
8683 	if (phba->cfg_fcp_io_channel) {
8684 		/* Set up fast-path FCP Response Complete Queue */
8685 		if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
8686 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8687 				"3148 Fast-path FCP %s array not allocated\n",
8688 				phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
8689 			rc = -ENOMEM;
8690 			goto out_destroy;
8691 		}
8692 
8693 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8694 			rc = lpfc_create_wq_cq(phba,
8695 					phba->sli4_hba.hba_eq[
8696 						qidx % io_channel],
8697 					phba->sli4_hba.fcp_cq[qidx],
8698 					phba->sli4_hba.fcp_wq[qidx],
8699 					&phba->sli4_hba.fcp_cq_map[qidx],
8700 					qidx, LPFC_FCP);
8701 			if (rc) {
8702 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8703 					"0535 Failed to setup fastpath "
8704 					"FCP WQ/CQ (%d), rc = 0x%x\n",
8705 					qidx, (uint32_t)rc);
8706 				goto out_destroy;
8707 			}
8708 		}
8709 	}
8710 
8711 	/*
8712 	 * Set up Slow Path Complete Queues (CQs)
8713 	 */
8714 
8715 	/* Set up slow-path MBOX CQ/MQ */
8716 
8717 	if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
8718 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8719 				"0528 %s not allocated\n",
8720 				phba->sli4_hba.mbx_cq ?
8721 				"Mailbox WQ" : "Mailbox CQ");
8722 		rc = -ENOMEM;
8723 		goto out_destroy;
8724 	}
8725 
8726 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8727 			       phba->sli4_hba.mbx_cq,
8728 			       phba->sli4_hba.mbx_wq,
8729 			       NULL, 0, LPFC_MBOX);
8730 	if (rc) {
8731 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8732 			"0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8733 			(uint32_t)rc);
8734 		goto out_destroy;
8735 	}
8736 	if (phba->nvmet_support) {
8737 		if (!phba->sli4_hba.nvmet_cqset) {
8738 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8739 					"3165 Fast-path NVME CQ Set "
8740 					"array not allocated\n");
8741 			rc = -ENOMEM;
8742 			goto out_destroy;
8743 		}
8744 		if (phba->cfg_nvmet_mrq > 1) {
8745 			rc = lpfc_cq_create_set(phba,
8746 					phba->sli4_hba.nvmet_cqset,
8747 					phba->sli4_hba.hba_eq,
8748 					LPFC_WCQ, LPFC_NVMET);
8749 			if (rc) {
8750 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8751 						"3164 Failed setup of NVME CQ "
8752 						"Set, rc = 0x%x\n",
8753 						(uint32_t)rc);
8754 				goto out_destroy;
8755 			}
8756 		} else {
8757 			/* Set up NVMET Receive Complete Queue */
8758 			rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8759 					    phba->sli4_hba.hba_eq[0],
8760 					    LPFC_WCQ, LPFC_NVMET);
8761 			if (rc) {
8762 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8763 						"6089 Failed setup NVMET CQ: "
8764 						"rc = 0x%x\n", (uint32_t)rc);
8765 				goto out_destroy;
8766 			}
8767 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8768 					"6090 NVMET CQ setup: cq-id=%d, "
8769 					"parent eq-id=%d\n",
8770 					phba->sli4_hba.nvmet_cqset[0]->queue_id,
8771 					phba->sli4_hba.hba_eq[0]->queue_id);
8772 		}
8773 	}
8774 
8775 	/* Set up slow-path ELS WQ/CQ */
8776 	if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
8777 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8778 				"0530 ELS %s not allocated\n",
8779 				phba->sli4_hba.els_cq ? "WQ" : "CQ");
8780 		rc = -ENOMEM;
8781 		goto out_destroy;
8782 	}
8783 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8784 					phba->sli4_hba.els_cq,
8785 					phba->sli4_hba.els_wq,
8786 					NULL, 0, LPFC_ELS);
8787 	if (rc) {
8788 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8789 			"0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8790 			(uint32_t)rc);
8791 		goto out_destroy;
8792 	}
8793 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8794 			"2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8795 			phba->sli4_hba.els_wq->queue_id,
8796 			phba->sli4_hba.els_cq->queue_id);
8797 
8798 	if (phba->cfg_nvme_io_channel) {
8799 		/* Set up NVME LS Complete Queue */
8800 		if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8801 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8802 					"6091 LS %s not allocated\n",
8803 					phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8804 			rc = -ENOMEM;
8805 			goto out_destroy;
8806 		}
8807 		rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8808 					phba->sli4_hba.nvmels_cq,
8809 					phba->sli4_hba.nvmels_wq,
8810 					NULL, 0, LPFC_NVME_LS);
8811 		if (rc) {
8812 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8813 				"0529 Failed setup of NVVME LS WQ/CQ: "
8814 				"rc = 0x%x\n", (uint32_t)rc);
8815 			goto out_destroy;
8816 		}
8817 
8818 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8819 				"6096 ELS WQ setup: wq-id=%d, "
8820 				"parent cq-id=%d\n",
8821 				phba->sli4_hba.nvmels_wq->queue_id,
8822 				phba->sli4_hba.nvmels_cq->queue_id);
8823 	}
8824 
8825 	/*
8826 	 * Create NVMET Receive Queue (RQ)
8827 	 */
8828 	if (phba->nvmet_support) {
8829 		if ((!phba->sli4_hba.nvmet_cqset) ||
8830 		    (!phba->sli4_hba.nvmet_mrq_hdr) ||
8831 		    (!phba->sli4_hba.nvmet_mrq_data)) {
8832 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8833 					"6130 MRQ CQ Queues not "
8834 					"allocated\n");
8835 			rc = -ENOMEM;
8836 			goto out_destroy;
8837 		}
8838 		if (phba->cfg_nvmet_mrq > 1) {
8839 			rc = lpfc_mrq_create(phba,
8840 					     phba->sli4_hba.nvmet_mrq_hdr,
8841 					     phba->sli4_hba.nvmet_mrq_data,
8842 					     phba->sli4_hba.nvmet_cqset,
8843 					     LPFC_NVMET);
8844 			if (rc) {
8845 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8846 						"6098 Failed setup of NVMET "
8847 						"MRQ: rc = 0x%x\n",
8848 						(uint32_t)rc);
8849 				goto out_destroy;
8850 			}
8851 
8852 		} else {
8853 			rc = lpfc_rq_create(phba,
8854 					    phba->sli4_hba.nvmet_mrq_hdr[0],
8855 					    phba->sli4_hba.nvmet_mrq_data[0],
8856 					    phba->sli4_hba.nvmet_cqset[0],
8857 					    LPFC_NVMET);
8858 			if (rc) {
8859 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8860 						"6057 Failed setup of NVMET "
8861 						"Receive Queue: rc = 0x%x\n",
8862 						(uint32_t)rc);
8863 				goto out_destroy;
8864 			}
8865 
8866 			lpfc_printf_log(
8867 				phba, KERN_INFO, LOG_INIT,
8868 				"6099 NVMET RQ setup: hdr-rq-id=%d, "
8869 				"dat-rq-id=%d parent cq-id=%d\n",
8870 				phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
8871 				phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
8872 				phba->sli4_hba.nvmet_cqset[0]->queue_id);
8873 
8874 		}
8875 	}
8876 
8877 	if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
8878 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8879 				"0540 Receive Queue not allocated\n");
8880 		rc = -ENOMEM;
8881 		goto out_destroy;
8882 	}
8883 
8884 	rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
8885 			    phba->sli4_hba.els_cq, LPFC_USOL);
8886 	if (rc) {
8887 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8888 				"0541 Failed setup of Receive Queue: "
8889 				"rc = 0x%x\n", (uint32_t)rc);
8890 		goto out_destroy;
8891 	}
8892 
8893 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8894 			"2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
8895 			"parent cq-id=%d\n",
8896 			phba->sli4_hba.hdr_rq->queue_id,
8897 			phba->sli4_hba.dat_rq->queue_id,
8898 			phba->sli4_hba.els_cq->queue_id);
8899 
8900 	if (phba->cfg_fof) {
8901 		rc = lpfc_fof_queue_setup(phba);
8902 		if (rc) {
8903 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8904 					"0549 Failed setup of FOF Queues: "
8905 					"rc = 0x%x\n", rc);
8906 			goto out_destroy;
8907 		}
8908 	}
8909 
8910 	for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
8911 		lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
8912 					 phba->cfg_fcp_imax);
8913 
8914 	return 0;
8915 
8916 out_destroy:
8917 	lpfc_sli4_queue_unset(phba);
8918 out_error:
8919 	return rc;
8920 }
8921 
8922 /**
8923  * lpfc_sli4_queue_unset - Unset all the SLI4 queues
8924  * @phba: pointer to lpfc hba data structure.
8925  *
8926  * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
8927  * operation.
8928  *
8929  * Return codes
8930  *      0 - successful
8931  *      -ENOMEM - No available memory
8932  *      -EIO - The mailbox failed to complete successfully.
8933  **/
8934 void
8935 lpfc_sli4_queue_unset(struct lpfc_hba *phba)
8936 {
8937 	int qidx;
8938 
8939 	/* Unset the queues created for Flash Optimized Fabric operations */
8940 	if (phba->cfg_fof)
8941 		lpfc_fof_queue_destroy(phba);
8942 
8943 	/* Unset mailbox command work queue */
8944 	if (phba->sli4_hba.mbx_wq)
8945 		lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
8946 
8947 	/* Unset NVME LS work queue */
8948 	if (phba->sli4_hba.nvmels_wq)
8949 		lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
8950 
8951 	/* Unset ELS work queue */
8952 	if (phba->sli4_hba.els_wq)
8953 		lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
8954 
8955 	/* Unset unsolicited receive queue */
8956 	if (phba->sli4_hba.hdr_rq)
8957 		lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
8958 				phba->sli4_hba.dat_rq);
8959 
8960 	/* Unset FCP work queue */
8961 	if (phba->sli4_hba.fcp_wq)
8962 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8963 			lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
8964 
8965 	/* Unset NVME work queue */
8966 	if (phba->sli4_hba.nvme_wq) {
8967 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8968 			lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
8969 	}
8970 
8971 	/* Unset mailbox command complete queue */
8972 	if (phba->sli4_hba.mbx_cq)
8973 		lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
8974 
8975 	/* Unset ELS complete queue */
8976 	if (phba->sli4_hba.els_cq)
8977 		lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
8978 
8979 	/* Unset NVME LS complete queue */
8980 	if (phba->sli4_hba.nvmels_cq)
8981 		lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
8982 
8983 	/* Unset NVME response complete queue */
8984 	if (phba->sli4_hba.nvme_cq)
8985 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8986 			lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
8987 
8988 	/* Unset NVMET MRQ queue */
8989 	if (phba->sli4_hba.nvmet_mrq_hdr) {
8990 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8991 			lpfc_rq_destroy(phba,
8992 					phba->sli4_hba.nvmet_mrq_hdr[qidx],
8993 					phba->sli4_hba.nvmet_mrq_data[qidx]);
8994 	}
8995 
8996 	/* Unset NVMET CQ Set complete queue */
8997 	if (phba->sli4_hba.nvmet_cqset) {
8998 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8999 			lpfc_cq_destroy(phba,
9000 					phba->sli4_hba.nvmet_cqset[qidx]);
9001 	}
9002 
9003 	/* Unset FCP response complete queue */
9004 	if (phba->sli4_hba.fcp_cq)
9005 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9006 			lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
9007 
9008 	/* Unset fast-path event queue */
9009 	if (phba->sli4_hba.hba_eq)
9010 		for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
9011 			lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
9012 }
9013 
9014 /**
9015  * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9016  * @phba: pointer to lpfc hba data structure.
9017  *
9018  * This routine is invoked to allocate and set up a pool of completion queue
9019  * events. The body of the completion queue event is a completion queue entry
9020  * CQE. For now, this pool is used for the interrupt service routine to queue
9021  * the following HBA completion queue events for the worker thread to process:
9022  *   - Mailbox asynchronous events
9023  *   - Receive queue completion unsolicited events
9024  * Later, this can be used for all the slow-path events.
9025  *
9026  * Return codes
9027  *      0 - successful
9028  *      -ENOMEM - No available memory
9029  **/
9030 static int
9031 lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9032 {
9033 	struct lpfc_cq_event *cq_event;
9034 	int i;
9035 
9036 	for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9037 		cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9038 		if (!cq_event)
9039 			goto out_pool_create_fail;
9040 		list_add_tail(&cq_event->list,
9041 			      &phba->sli4_hba.sp_cqe_event_pool);
9042 	}
9043 	return 0;
9044 
9045 out_pool_create_fail:
9046 	lpfc_sli4_cq_event_pool_destroy(phba);
9047 	return -ENOMEM;
9048 }
9049 
9050 /**
9051  * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9052  * @phba: pointer to lpfc hba data structure.
9053  *
9054  * This routine is invoked to free the pool of completion queue events at
9055  * driver unload time. Note that, it is the responsibility of the driver
9056  * cleanup routine to free all the outstanding completion-queue events
9057  * allocated from this pool back into the pool before invoking this routine
9058  * to destroy the pool.
9059  **/
9060 static void
9061 lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9062 {
9063 	struct lpfc_cq_event *cq_event, *next_cq_event;
9064 
9065 	list_for_each_entry_safe(cq_event, next_cq_event,
9066 				 &phba->sli4_hba.sp_cqe_event_pool, list) {
9067 		list_del(&cq_event->list);
9068 		kfree(cq_event);
9069 	}
9070 }
9071 
9072 /**
9073  * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9074  * @phba: pointer to lpfc hba data structure.
9075  *
9076  * This routine is the lock free version of the API invoked to allocate a
9077  * completion-queue event from the free pool.
9078  *
9079  * Return: Pointer to the newly allocated completion-queue event if successful
9080  *         NULL otherwise.
9081  **/
9082 struct lpfc_cq_event *
9083 __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9084 {
9085 	struct lpfc_cq_event *cq_event = NULL;
9086 
9087 	list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9088 			 struct lpfc_cq_event, list);
9089 	return cq_event;
9090 }
9091 
9092 /**
9093  * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9094  * @phba: pointer to lpfc hba data structure.
9095  *
9096  * This routine is the lock version of the API invoked to allocate a
9097  * completion-queue event from the free pool.
9098  *
9099  * Return: Pointer to the newly allocated completion-queue event if successful
9100  *         NULL otherwise.
9101  **/
9102 struct lpfc_cq_event *
9103 lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9104 {
9105 	struct lpfc_cq_event *cq_event;
9106 	unsigned long iflags;
9107 
9108 	spin_lock_irqsave(&phba->hbalock, iflags);
9109 	cq_event = __lpfc_sli4_cq_event_alloc(phba);
9110 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9111 	return cq_event;
9112 }
9113 
9114 /**
9115  * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9116  * @phba: pointer to lpfc hba data structure.
9117  * @cq_event: pointer to the completion queue event to be freed.
9118  *
9119  * This routine is the lock free version of the API invoked to release a
9120  * completion-queue event back into the free pool.
9121  **/
9122 void
9123 __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9124 			     struct lpfc_cq_event *cq_event)
9125 {
9126 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9127 }
9128 
9129 /**
9130  * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9131  * @phba: pointer to lpfc hba data structure.
9132  * @cq_event: pointer to the completion queue event to be freed.
9133  *
9134  * This routine is the lock version of the API invoked to release a
9135  * completion-queue event back into the free pool.
9136  **/
9137 void
9138 lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9139 			   struct lpfc_cq_event *cq_event)
9140 {
9141 	unsigned long iflags;
9142 	spin_lock_irqsave(&phba->hbalock, iflags);
9143 	__lpfc_sli4_cq_event_release(phba, cq_event);
9144 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9145 }
9146 
9147 /**
9148  * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9149  * @phba: pointer to lpfc hba data structure.
9150  *
9151  * This routine is to free all the pending completion-queue events to the
9152  * back into the free pool for device reset.
9153  **/
9154 static void
9155 lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9156 {
9157 	LIST_HEAD(cqelist);
9158 	struct lpfc_cq_event *cqe;
9159 	unsigned long iflags;
9160 
9161 	/* Retrieve all the pending WCQEs from pending WCQE lists */
9162 	spin_lock_irqsave(&phba->hbalock, iflags);
9163 	/* Pending FCP XRI abort events */
9164 	list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9165 			 &cqelist);
9166 	/* Pending ELS XRI abort events */
9167 	list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9168 			 &cqelist);
9169 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9170 		/* Pending NVME XRI abort events */
9171 		list_splice_init(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
9172 				 &cqelist);
9173 	}
9174 	/* Pending asynnc events */
9175 	list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9176 			 &cqelist);
9177 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9178 
9179 	while (!list_empty(&cqelist)) {
9180 		list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9181 		lpfc_sli4_cq_event_release(phba, cqe);
9182 	}
9183 }
9184 
9185 /**
9186  * lpfc_pci_function_reset - Reset pci function.
9187  * @phba: pointer to lpfc hba data structure.
9188  *
9189  * This routine is invoked to request a PCI function reset. It will destroys
9190  * all resources assigned to the PCI function which originates this request.
9191  *
9192  * Return codes
9193  *      0 - successful
9194  *      -ENOMEM - No available memory
9195  *      -EIO - The mailbox failed to complete successfully.
9196  **/
9197 int
9198 lpfc_pci_function_reset(struct lpfc_hba *phba)
9199 {
9200 	LPFC_MBOXQ_t *mboxq;
9201 	uint32_t rc = 0, if_type;
9202 	uint32_t shdr_status, shdr_add_status;
9203 	uint32_t rdy_chk;
9204 	uint32_t port_reset = 0;
9205 	union lpfc_sli4_cfg_shdr *shdr;
9206 	struct lpfc_register reg_data;
9207 	uint16_t devid;
9208 
9209 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9210 	switch (if_type) {
9211 	case LPFC_SLI_INTF_IF_TYPE_0:
9212 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9213 						       GFP_KERNEL);
9214 		if (!mboxq) {
9215 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9216 					"0494 Unable to allocate memory for "
9217 					"issuing SLI_FUNCTION_RESET mailbox "
9218 					"command\n");
9219 			return -ENOMEM;
9220 		}
9221 
9222 		/* Setup PCI function reset mailbox-ioctl command */
9223 		lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9224 				 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9225 				 LPFC_SLI4_MBX_EMBED);
9226 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9227 		shdr = (union lpfc_sli4_cfg_shdr *)
9228 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9229 		shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9230 		shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9231 					 &shdr->response);
9232 		if (rc != MBX_TIMEOUT)
9233 			mempool_free(mboxq, phba->mbox_mem_pool);
9234 		if (shdr_status || shdr_add_status || rc) {
9235 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9236 					"0495 SLI_FUNCTION_RESET mailbox "
9237 					"failed with status x%x add_status x%x,"
9238 					" mbx status x%x\n",
9239 					shdr_status, shdr_add_status, rc);
9240 			rc = -ENXIO;
9241 		}
9242 		break;
9243 	case LPFC_SLI_INTF_IF_TYPE_2:
9244 wait:
9245 		/*
9246 		 * Poll the Port Status Register and wait for RDY for
9247 		 * up to 30 seconds. If the port doesn't respond, treat
9248 		 * it as an error.
9249 		 */
9250 		for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
9251 			if (lpfc_readl(phba->sli4_hba.u.if_type2.
9252 				STATUSregaddr, &reg_data.word0)) {
9253 				rc = -ENODEV;
9254 				goto out;
9255 			}
9256 			if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9257 				break;
9258 			msleep(20);
9259 		}
9260 
9261 		if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9262 			phba->work_status[0] = readl(
9263 				phba->sli4_hba.u.if_type2.ERR1regaddr);
9264 			phba->work_status[1] = readl(
9265 				phba->sli4_hba.u.if_type2.ERR2regaddr);
9266 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9267 					"2890 Port not ready, port status reg "
9268 					"0x%x error 1=0x%x, error 2=0x%x\n",
9269 					reg_data.word0,
9270 					phba->work_status[0],
9271 					phba->work_status[1]);
9272 			rc = -ENODEV;
9273 			goto out;
9274 		}
9275 
9276 		if (!port_reset) {
9277 			/*
9278 			 * Reset the port now
9279 			 */
9280 			reg_data.word0 = 0;
9281 			bf_set(lpfc_sliport_ctrl_end, &reg_data,
9282 			       LPFC_SLIPORT_LITTLE_ENDIAN);
9283 			bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9284 			       LPFC_SLIPORT_INIT_PORT);
9285 			writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9286 			       CTRLregaddr);
9287 			/* flush */
9288 			pci_read_config_word(phba->pcidev,
9289 					     PCI_DEVICE_ID, &devid);
9290 
9291 			port_reset = 1;
9292 			msleep(20);
9293 			goto wait;
9294 		} else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9295 			rc = -ENODEV;
9296 			goto out;
9297 		}
9298 		break;
9299 
9300 	case LPFC_SLI_INTF_IF_TYPE_1:
9301 	default:
9302 		break;
9303 	}
9304 
9305 out:
9306 	/* Catch the not-ready port failure after a port reset. */
9307 	if (rc) {
9308 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9309 				"3317 HBA not functional: IP Reset Failed "
9310 				"try: echo fw_reset > board_mode\n");
9311 		rc = -ENODEV;
9312 	}
9313 
9314 	return rc;
9315 }
9316 
9317 /**
9318  * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9319  * @phba: pointer to lpfc hba data structure.
9320  *
9321  * This routine is invoked to set up the PCI device memory space for device
9322  * with SLI-4 interface spec.
9323  *
9324  * Return codes
9325  * 	0 - successful
9326  * 	other values - error
9327  **/
9328 static int
9329 lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9330 {
9331 	struct pci_dev *pdev;
9332 	unsigned long bar0map_len, bar1map_len, bar2map_len;
9333 	int error = -ENODEV;
9334 	uint32_t if_type;
9335 
9336 	/* Obtain PCI device reference */
9337 	if (!phba->pcidev)
9338 		return error;
9339 	else
9340 		pdev = phba->pcidev;
9341 
9342 	/* Set the device DMA mask size */
9343 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9344 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9345 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9346 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
9347 			return error;
9348 		}
9349 	}
9350 
9351 	/*
9352 	 * The BARs and register set definitions and offset locations are
9353 	 * dependent on the if_type.
9354 	 */
9355 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9356 				  &phba->sli4_hba.sli_intf.word0)) {
9357 		return error;
9358 	}
9359 
9360 	/* There is no SLI3 failback for SLI4 devices. */
9361 	if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9362 	    LPFC_SLI_INTF_VALID) {
9363 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9364 				"2894 SLI_INTF reg contents invalid "
9365 				"sli_intf reg 0x%x\n",
9366 				phba->sli4_hba.sli_intf.word0);
9367 		return error;
9368 	}
9369 
9370 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9371 	/*
9372 	 * Get the bus address of SLI4 device Bar regions and the
9373 	 * number of bytes required by each mapping. The mapping of the
9374 	 * particular PCI BARs regions is dependent on the type of
9375 	 * SLI4 device.
9376 	 */
9377 	if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9378 		phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9379 		bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
9380 
9381 		/*
9382 		 * Map SLI4 PCI Config Space Register base to a kernel virtual
9383 		 * addr
9384 		 */
9385 		phba->sli4_hba.conf_regs_memmap_p =
9386 			ioremap(phba->pci_bar0_map, bar0map_len);
9387 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9388 			dev_printk(KERN_ERR, &pdev->dev,
9389 				   "ioremap failed for SLI4 PCI config "
9390 				   "registers.\n");
9391 			goto out;
9392 		}
9393 		phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
9394 		/* Set up BAR0 PCI config space register memory map */
9395 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9396 	} else {
9397 		phba->pci_bar0_map = pci_resource_start(pdev, 1);
9398 		bar0map_len = pci_resource_len(pdev, 1);
9399 		if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9400 			dev_printk(KERN_ERR, &pdev->dev,
9401 			   "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9402 			goto out;
9403 		}
9404 		phba->sli4_hba.conf_regs_memmap_p =
9405 				ioremap(phba->pci_bar0_map, bar0map_len);
9406 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9407 			dev_printk(KERN_ERR, &pdev->dev,
9408 				"ioremap failed for SLI4 PCI config "
9409 				"registers.\n");
9410 				goto out;
9411 		}
9412 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9413 	}
9414 
9415 	if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
9416 	    (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
9417 		/*
9418 		 * Map SLI4 if type 0 HBA Control Register base to a kernel
9419 		 * virtual address and setup the registers.
9420 		 */
9421 		phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
9422 		bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9423 		phba->sli4_hba.ctrl_regs_memmap_p =
9424 				ioremap(phba->pci_bar1_map, bar1map_len);
9425 		if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9426 			dev_printk(KERN_ERR, &pdev->dev,
9427 			   "ioremap failed for SLI4 HBA control registers.\n");
9428 			goto out_iounmap_conf;
9429 		}
9430 		phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
9431 		lpfc_sli4_bar1_register_memmap(phba);
9432 	}
9433 
9434 	if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
9435 	    (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
9436 		/*
9437 		 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
9438 		 * virtual address and setup the registers.
9439 		 */
9440 		phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
9441 		bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9442 		phba->sli4_hba.drbl_regs_memmap_p =
9443 				ioremap(phba->pci_bar2_map, bar2map_len);
9444 		if (!phba->sli4_hba.drbl_regs_memmap_p) {
9445 			dev_printk(KERN_ERR, &pdev->dev,
9446 			   "ioremap failed for SLI4 HBA doorbell registers.\n");
9447 			goto out_iounmap_ctrl;
9448 		}
9449 		phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
9450 		error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9451 		if (error)
9452 			goto out_iounmap_all;
9453 	}
9454 
9455 	return 0;
9456 
9457 out_iounmap_all:
9458 	iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9459 out_iounmap_ctrl:
9460 	iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9461 out_iounmap_conf:
9462 	iounmap(phba->sli4_hba.conf_regs_memmap_p);
9463 out:
9464 	return error;
9465 }
9466 
9467 /**
9468  * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9469  * @phba: pointer to lpfc hba data structure.
9470  *
9471  * This routine is invoked to unset the PCI device memory space for device
9472  * with SLI-4 interface spec.
9473  **/
9474 static void
9475 lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9476 {
9477 	uint32_t if_type;
9478 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9479 
9480 	switch (if_type) {
9481 	case LPFC_SLI_INTF_IF_TYPE_0:
9482 		iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9483 		iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9484 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9485 		break;
9486 	case LPFC_SLI_INTF_IF_TYPE_2:
9487 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9488 		break;
9489 	case LPFC_SLI_INTF_IF_TYPE_1:
9490 	default:
9491 		dev_printk(KERN_ERR, &phba->pcidev->dev,
9492 			   "FATAL - unsupported SLI4 interface type - %d\n",
9493 			   if_type);
9494 		break;
9495 	}
9496 }
9497 
9498 /**
9499  * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9500  * @phba: pointer to lpfc hba data structure.
9501  *
9502  * This routine is invoked to enable the MSI-X interrupt vectors to device
9503  * with SLI-3 interface specs.
9504  *
9505  * Return codes
9506  *   0 - successful
9507  *   other values - error
9508  **/
9509 static int
9510 lpfc_sli_enable_msix(struct lpfc_hba *phba)
9511 {
9512 	int rc;
9513 	LPFC_MBOXQ_t *pmb;
9514 
9515 	/* Set up MSI-X multi-message vectors */
9516 	rc = pci_alloc_irq_vectors(phba->pcidev,
9517 			LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9518 	if (rc < 0) {
9519 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9520 				"0420 PCI enable MSI-X failed (%d)\n", rc);
9521 		goto vec_fail_out;
9522 	}
9523 
9524 	/*
9525 	 * Assign MSI-X vectors to interrupt handlers
9526 	 */
9527 
9528 	/* vector-0 is associated to slow-path handler */
9529 	rc = request_irq(pci_irq_vector(phba->pcidev, 0),
9530 			 &lpfc_sli_sp_intr_handler, 0,
9531 			 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9532 	if (rc) {
9533 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9534 				"0421 MSI-X slow-path request_irq failed "
9535 				"(%d)\n", rc);
9536 		goto msi_fail_out;
9537 	}
9538 
9539 	/* vector-1 is associated to fast-path handler */
9540 	rc = request_irq(pci_irq_vector(phba->pcidev, 1),
9541 			 &lpfc_sli_fp_intr_handler, 0,
9542 			 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9543 
9544 	if (rc) {
9545 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9546 				"0429 MSI-X fast-path request_irq failed "
9547 				"(%d)\n", rc);
9548 		goto irq_fail_out;
9549 	}
9550 
9551 	/*
9552 	 * Configure HBA MSI-X attention conditions to messages
9553 	 */
9554 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9555 
9556 	if (!pmb) {
9557 		rc = -ENOMEM;
9558 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9559 				"0474 Unable to allocate memory for issuing "
9560 				"MBOX_CONFIG_MSI command\n");
9561 		goto mem_fail_out;
9562 	}
9563 	rc = lpfc_config_msi(phba, pmb);
9564 	if (rc)
9565 		goto mbx_fail_out;
9566 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9567 	if (rc != MBX_SUCCESS) {
9568 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9569 				"0351 Config MSI mailbox command failed, "
9570 				"mbxCmd x%x, mbxStatus x%x\n",
9571 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9572 		goto mbx_fail_out;
9573 	}
9574 
9575 	/* Free memory allocated for mailbox command */
9576 	mempool_free(pmb, phba->mbox_mem_pool);
9577 	return rc;
9578 
9579 mbx_fail_out:
9580 	/* Free memory allocated for mailbox command */
9581 	mempool_free(pmb, phba->mbox_mem_pool);
9582 
9583 mem_fail_out:
9584 	/* free the irq already requested */
9585 	free_irq(pci_irq_vector(phba->pcidev, 1), phba);
9586 
9587 irq_fail_out:
9588 	/* free the irq already requested */
9589 	free_irq(pci_irq_vector(phba->pcidev, 0), phba);
9590 
9591 msi_fail_out:
9592 	/* Unconfigure MSI-X capability structure */
9593 	pci_free_irq_vectors(phba->pcidev);
9594 
9595 vec_fail_out:
9596 	return rc;
9597 }
9598 
9599 /**
9600  * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9601  * @phba: pointer to lpfc hba data structure.
9602  *
9603  * This routine is invoked to enable the MSI interrupt mode to device with
9604  * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9605  * enable the MSI vector. The device driver is responsible for calling the
9606  * request_irq() to register MSI vector with a interrupt the handler, which
9607  * is done in this function.
9608  *
9609  * Return codes
9610  * 	0 - successful
9611  * 	other values - error
9612  */
9613 static int
9614 lpfc_sli_enable_msi(struct lpfc_hba *phba)
9615 {
9616 	int rc;
9617 
9618 	rc = pci_enable_msi(phba->pcidev);
9619 	if (!rc)
9620 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9621 				"0462 PCI enable MSI mode success.\n");
9622 	else {
9623 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9624 				"0471 PCI enable MSI mode failed (%d)\n", rc);
9625 		return rc;
9626 	}
9627 
9628 	rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9629 			 0, LPFC_DRIVER_NAME, phba);
9630 	if (rc) {
9631 		pci_disable_msi(phba->pcidev);
9632 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9633 				"0478 MSI request_irq failed (%d)\n", rc);
9634 	}
9635 	return rc;
9636 }
9637 
9638 /**
9639  * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9640  * @phba: pointer to lpfc hba data structure.
9641  *
9642  * This routine is invoked to enable device interrupt and associate driver's
9643  * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9644  * spec. Depends on the interrupt mode configured to the driver, the driver
9645  * will try to fallback from the configured interrupt mode to an interrupt
9646  * mode which is supported by the platform, kernel, and device in the order
9647  * of:
9648  * MSI-X -> MSI -> IRQ.
9649  *
9650  * Return codes
9651  *   0 - successful
9652  *   other values - error
9653  **/
9654 static uint32_t
9655 lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9656 {
9657 	uint32_t intr_mode = LPFC_INTR_ERROR;
9658 	int retval;
9659 
9660 	if (cfg_mode == 2) {
9661 		/* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9662 		retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9663 		if (!retval) {
9664 			/* Now, try to enable MSI-X interrupt mode */
9665 			retval = lpfc_sli_enable_msix(phba);
9666 			if (!retval) {
9667 				/* Indicate initialization to MSI-X mode */
9668 				phba->intr_type = MSIX;
9669 				intr_mode = 2;
9670 			}
9671 		}
9672 	}
9673 
9674 	/* Fallback to MSI if MSI-X initialization failed */
9675 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
9676 		retval = lpfc_sli_enable_msi(phba);
9677 		if (!retval) {
9678 			/* Indicate initialization to MSI mode */
9679 			phba->intr_type = MSI;
9680 			intr_mode = 1;
9681 		}
9682 	}
9683 
9684 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
9685 	if (phba->intr_type == NONE) {
9686 		retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9687 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9688 		if (!retval) {
9689 			/* Indicate initialization to INTx mode */
9690 			phba->intr_type = INTx;
9691 			intr_mode = 0;
9692 		}
9693 	}
9694 	return intr_mode;
9695 }
9696 
9697 /**
9698  * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9699  * @phba: pointer to lpfc hba data structure.
9700  *
9701  * This routine is invoked to disable device interrupt and disassociate the
9702  * driver's interrupt handler(s) from interrupt vector(s) to device with
9703  * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9704  * release the interrupt vector(s) for the message signaled interrupt.
9705  **/
9706 static void
9707 lpfc_sli_disable_intr(struct lpfc_hba *phba)
9708 {
9709 	int nr_irqs, i;
9710 
9711 	if (phba->intr_type == MSIX)
9712 		nr_irqs = LPFC_MSIX_VECTORS;
9713 	else
9714 		nr_irqs = 1;
9715 
9716 	for (i = 0; i < nr_irqs; i++)
9717 		free_irq(pci_irq_vector(phba->pcidev, i), phba);
9718 	pci_free_irq_vectors(phba->pcidev);
9719 
9720 	/* Reset interrupt management states */
9721 	phba->intr_type = NONE;
9722 	phba->sli.slistat.sli_intr = 0;
9723 }
9724 
9725 /**
9726  * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
9727  * @phba: pointer to lpfc hba data structure.
9728  * @vectors: number of msix vectors allocated.
9729  *
9730  * The routine will figure out the CPU affinity assignment for every
9731  * MSI-X vector allocated for the HBA.  The hba_eq_hdl will be updated
9732  * with a pointer to the CPU mask that defines ALL the CPUs this vector
9733  * can be associated with. If the vector can be unquely associated with
9734  * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9735  * In addition, the CPU to IO channel mapping will be calculated
9736  * and the phba->sli4_hba.cpu_map array will reflect this.
9737  */
9738 static void
9739 lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
9740 {
9741 	struct lpfc_vector_map_info *cpup;
9742 	int index = 0;
9743 	int vec = 0;
9744 	int cpu;
9745 #ifdef CONFIG_X86
9746 	struct cpuinfo_x86 *cpuinfo;
9747 #endif
9748 
9749 	/* Init cpu_map array */
9750 	memset(phba->sli4_hba.cpu_map, 0xff,
9751 	       (sizeof(struct lpfc_vector_map_info) *
9752 	       phba->sli4_hba.num_present_cpu));
9753 
9754 	/* Update CPU map with physical id and core id of each CPU */
9755 	cpup = phba->sli4_hba.cpu_map;
9756 	for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9757 #ifdef CONFIG_X86
9758 		cpuinfo = &cpu_data(cpu);
9759 		cpup->phys_id = cpuinfo->phys_proc_id;
9760 		cpup->core_id = cpuinfo->cpu_core_id;
9761 #else
9762 		/* No distinction between CPUs for other platforms */
9763 		cpup->phys_id = 0;
9764 		cpup->core_id = 0;
9765 #endif
9766 		cpup->channel_id = index;  /* For now round robin */
9767 		cpup->irq = pci_irq_vector(phba->pcidev, vec);
9768 		vec++;
9769 		if (vec >= vectors)
9770 			vec = 0;
9771 		index++;
9772 		if (index >= phba->cfg_fcp_io_channel)
9773 			index = 0;
9774 		cpup++;
9775 	}
9776 }
9777 
9778 
9779 /**
9780  * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9781  * @phba: pointer to lpfc hba data structure.
9782  *
9783  * This routine is invoked to enable the MSI-X interrupt vectors to device
9784  * with SLI-4 interface spec.
9785  *
9786  * Return codes
9787  * 0 - successful
9788  * other values - error
9789  **/
9790 static int
9791 lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9792 {
9793 	int vectors, rc, index;
9794 	char *name;
9795 
9796 	/* Set up MSI-X multi-message vectors */
9797 	vectors = phba->io_channel_irqs;
9798 	if (phba->cfg_fof)
9799 		vectors++;
9800 
9801 	rc = pci_alloc_irq_vectors(phba->pcidev,
9802 				(phba->nvmet_support) ? 1 : 2,
9803 				vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
9804 	if (rc < 0) {
9805 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9806 				"0484 PCI enable MSI-X failed (%d)\n", rc);
9807 		goto vec_fail_out;
9808 	}
9809 	vectors = rc;
9810 
9811 	/* Assign MSI-X vectors to interrupt handlers */
9812 	for (index = 0; index < vectors; index++) {
9813 		name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
9814 		memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
9815 		snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
9816 			 LPFC_DRIVER_HANDLER_NAME"%d", index);
9817 
9818 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
9819 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9820 		atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
9821 		if (phba->cfg_fof && (index == (vectors - 1)))
9822 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
9823 				 &lpfc_sli4_fof_intr_handler, 0,
9824 				 name,
9825 				 &phba->sli4_hba.hba_eq_hdl[index]);
9826 		else
9827 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
9828 				 &lpfc_sli4_hba_intr_handler, 0,
9829 				 name,
9830 				 &phba->sli4_hba.hba_eq_hdl[index]);
9831 		if (rc) {
9832 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9833 					"0486 MSI-X fast-path (%d) "
9834 					"request_irq failed (%d)\n", index, rc);
9835 			goto cfg_fail_out;
9836 		}
9837 	}
9838 
9839 	if (phba->cfg_fof)
9840 		vectors--;
9841 
9842 	if (vectors != phba->io_channel_irqs) {
9843 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9844 				"3238 Reducing IO channels to match number of "
9845 				"MSI-X vectors, requested %d got %d\n",
9846 				phba->io_channel_irqs, vectors);
9847 		if (phba->cfg_fcp_io_channel > vectors)
9848 			phba->cfg_fcp_io_channel = vectors;
9849 		if (phba->cfg_nvme_io_channel > vectors)
9850 			phba->cfg_nvme_io_channel = vectors;
9851 		if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
9852 			phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9853 		else
9854 			phba->io_channel_irqs = phba->cfg_nvme_io_channel;
9855 	}
9856 	lpfc_cpu_affinity_check(phba, vectors);
9857 
9858 	return rc;
9859 
9860 cfg_fail_out:
9861 	/* free the irq already requested */
9862 	for (--index; index >= 0; index--)
9863 		free_irq(pci_irq_vector(phba->pcidev, index),
9864 				&phba->sli4_hba.hba_eq_hdl[index]);
9865 
9866 	/* Unconfigure MSI-X capability structure */
9867 	pci_free_irq_vectors(phba->pcidev);
9868 
9869 vec_fail_out:
9870 	return rc;
9871 }
9872 
9873 /**
9874  * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9875  * @phba: pointer to lpfc hba data structure.
9876  *
9877  * This routine is invoked to enable the MSI interrupt mode to device with
9878  * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9879  * to enable the MSI vector. The device driver is responsible for calling
9880  * the request_irq() to register MSI vector with a interrupt the handler,
9881  * which is done in this function.
9882  *
9883  * Return codes
9884  * 	0 - successful
9885  * 	other values - error
9886  **/
9887 static int
9888 lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9889 {
9890 	int rc, index;
9891 
9892 	rc = pci_enable_msi(phba->pcidev);
9893 	if (!rc)
9894 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9895 				"0487 PCI enable MSI mode success.\n");
9896 	else {
9897 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9898 				"0488 PCI enable MSI mode failed (%d)\n", rc);
9899 		return rc;
9900 	}
9901 
9902 	rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9903 			 0, LPFC_DRIVER_NAME, phba);
9904 	if (rc) {
9905 		pci_disable_msi(phba->pcidev);
9906 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9907 				"0490 MSI request_irq failed (%d)\n", rc);
9908 		return rc;
9909 	}
9910 
9911 	for (index = 0; index < phba->io_channel_irqs; index++) {
9912 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
9913 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9914 	}
9915 
9916 	if (phba->cfg_fof) {
9917 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
9918 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9919 	}
9920 	return 0;
9921 }
9922 
9923 /**
9924  * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9925  * @phba: pointer to lpfc hba data structure.
9926  *
9927  * This routine is invoked to enable device interrupt and associate driver's
9928  * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9929  * interface spec. Depends on the interrupt mode configured to the driver,
9930  * the driver will try to fallback from the configured interrupt mode to an
9931  * interrupt mode which is supported by the platform, kernel, and device in
9932  * the order of:
9933  * MSI-X -> MSI -> IRQ.
9934  *
9935  * Return codes
9936  * 	0 - successful
9937  * 	other values - error
9938  **/
9939 static uint32_t
9940 lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9941 {
9942 	uint32_t intr_mode = LPFC_INTR_ERROR;
9943 	int retval, idx;
9944 
9945 	if (cfg_mode == 2) {
9946 		/* Preparation before conf_msi mbox cmd */
9947 		retval = 0;
9948 		if (!retval) {
9949 			/* Now, try to enable MSI-X interrupt mode */
9950 			retval = lpfc_sli4_enable_msix(phba);
9951 			if (!retval) {
9952 				/* Indicate initialization to MSI-X mode */
9953 				phba->intr_type = MSIX;
9954 				intr_mode = 2;
9955 			}
9956 		}
9957 	}
9958 
9959 	/* Fallback to MSI if MSI-X initialization failed */
9960 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
9961 		retval = lpfc_sli4_enable_msi(phba);
9962 		if (!retval) {
9963 			/* Indicate initialization to MSI mode */
9964 			phba->intr_type = MSI;
9965 			intr_mode = 1;
9966 		}
9967 	}
9968 
9969 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
9970 	if (phba->intr_type == NONE) {
9971 		retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9972 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9973 		if (!retval) {
9974 			struct lpfc_hba_eq_hdl *eqhdl;
9975 
9976 			/* Indicate initialization to INTx mode */
9977 			phba->intr_type = INTx;
9978 			intr_mode = 0;
9979 
9980 			for (idx = 0; idx < phba->io_channel_irqs; idx++) {
9981 				eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9982 				eqhdl->idx = idx;
9983 				eqhdl->phba = phba;
9984 				atomic_set(&eqhdl->hba_eq_in_use, 1);
9985 			}
9986 			if (phba->cfg_fof) {
9987 				eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9988 				eqhdl->idx = idx;
9989 				eqhdl->phba = phba;
9990 				atomic_set(&eqhdl->hba_eq_in_use, 1);
9991 			}
9992 		}
9993 	}
9994 	return intr_mode;
9995 }
9996 
9997 /**
9998  * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9999  * @phba: pointer to lpfc hba data structure.
10000  *
10001  * This routine is invoked to disable device interrupt and disassociate
10002  * the driver's interrupt handler(s) from interrupt vector(s) to device
10003  * with SLI-4 interface spec. Depending on the interrupt mode, the driver
10004  * will release the interrupt vector(s) for the message signaled interrupt.
10005  **/
10006 static void
10007 lpfc_sli4_disable_intr(struct lpfc_hba *phba)
10008 {
10009 	/* Disable the currently initialized interrupt mode */
10010 	if (phba->intr_type == MSIX) {
10011 		int index;
10012 
10013 		/* Free up MSI-X multi-message vectors */
10014 		for (index = 0; index < phba->io_channel_irqs; index++)
10015 			free_irq(pci_irq_vector(phba->pcidev, index),
10016 					&phba->sli4_hba.hba_eq_hdl[index]);
10017 
10018 		if (phba->cfg_fof)
10019 			free_irq(pci_irq_vector(phba->pcidev, index),
10020 					&phba->sli4_hba.hba_eq_hdl[index]);
10021 	} else {
10022 		free_irq(phba->pcidev->irq, phba);
10023 	}
10024 
10025 	pci_free_irq_vectors(phba->pcidev);
10026 
10027 	/* Reset interrupt management states */
10028 	phba->intr_type = NONE;
10029 	phba->sli.slistat.sli_intr = 0;
10030 }
10031 
10032 /**
10033  * lpfc_unset_hba - Unset SLI3 hba device initialization
10034  * @phba: pointer to lpfc hba data structure.
10035  *
10036  * This routine is invoked to unset the HBA device initialization steps to
10037  * a device with SLI-3 interface spec.
10038  **/
10039 static void
10040 lpfc_unset_hba(struct lpfc_hba *phba)
10041 {
10042 	struct lpfc_vport *vport = phba->pport;
10043 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10044 
10045 	spin_lock_irq(shost->host_lock);
10046 	vport->load_flag |= FC_UNLOADING;
10047 	spin_unlock_irq(shost->host_lock);
10048 
10049 	kfree(phba->vpi_bmask);
10050 	kfree(phba->vpi_ids);
10051 
10052 	lpfc_stop_hba_timers(phba);
10053 
10054 	phba->pport->work_port_events = 0;
10055 
10056 	lpfc_sli_hba_down(phba);
10057 
10058 	lpfc_sli_brdrestart(phba);
10059 
10060 	lpfc_sli_disable_intr(phba);
10061 
10062 	return;
10063 }
10064 
10065 /**
10066  * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10067  * @phba: Pointer to HBA context object.
10068  *
10069  * This function is called in the SLI4 code path to wait for completion
10070  * of device's XRIs exchange busy. It will check the XRI exchange busy
10071  * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10072  * that, it will check the XRI exchange busy on outstanding FCP and ELS
10073  * I/Os every 30 seconds, log error message, and wait forever. Only when
10074  * all XRI exchange busy complete, the driver unload shall proceed with
10075  * invoking the function reset ioctl mailbox command to the CNA and the
10076  * the rest of the driver unload resource release.
10077  **/
10078 static void
10079 lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
10080 {
10081 	int wait_time = 0;
10082 	int nvme_xri_cmpl = 1;
10083 	int nvmet_xri_cmpl = 1;
10084 	int fcp_xri_cmpl = 1;
10085 	int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10086 
10087 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10088 		fcp_xri_cmpl =
10089 			list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10090 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10091 		nvme_xri_cmpl =
10092 			list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10093 		nvmet_xri_cmpl =
10094 			list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10095 	}
10096 
10097 	while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
10098 	       !nvmet_xri_cmpl) {
10099 		if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
10100 			if (!nvme_xri_cmpl)
10101 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10102 						"6100 NVME XRI exchange busy "
10103 						"wait time: %d seconds.\n",
10104 						wait_time/1000);
10105 			if (!fcp_xri_cmpl)
10106 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10107 						"2877 FCP XRI exchange busy "
10108 						"wait time: %d seconds.\n",
10109 						wait_time/1000);
10110 			if (!els_xri_cmpl)
10111 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10112 						"2878 ELS XRI exchange busy "
10113 						"wait time: %d seconds.\n",
10114 						wait_time/1000);
10115 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
10116 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
10117 		} else {
10118 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
10119 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
10120 		}
10121 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10122 			nvme_xri_cmpl = list_empty(
10123 				&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10124 			nvmet_xri_cmpl = list_empty(
10125 				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10126 		}
10127 
10128 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10129 			fcp_xri_cmpl = list_empty(
10130 				&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10131 
10132 		els_xri_cmpl =
10133 			list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10134 
10135 	}
10136 }
10137 
10138 /**
10139  * lpfc_sli4_hba_unset - Unset the fcoe hba
10140  * @phba: Pointer to HBA context object.
10141  *
10142  * This function is called in the SLI4 code path to reset the HBA's FCoE
10143  * function. The caller is not required to hold any lock. This routine
10144  * issues PCI function reset mailbox command to reset the FCoE function.
10145  * At the end of the function, it calls lpfc_hba_down_post function to
10146  * free any pending commands.
10147  **/
10148 static void
10149 lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10150 {
10151 	int wait_cnt = 0;
10152 	LPFC_MBOXQ_t *mboxq;
10153 	struct pci_dev *pdev = phba->pcidev;
10154 
10155 	lpfc_stop_hba_timers(phba);
10156 	phba->sli4_hba.intr_enable = 0;
10157 
10158 	/*
10159 	 * Gracefully wait out the potential current outstanding asynchronous
10160 	 * mailbox command.
10161 	 */
10162 
10163 	/* First, block any pending async mailbox command from posted */
10164 	spin_lock_irq(&phba->hbalock);
10165 	phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10166 	spin_unlock_irq(&phba->hbalock);
10167 	/* Now, trying to wait it out if we can */
10168 	while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10169 		msleep(10);
10170 		if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10171 			break;
10172 	}
10173 	/* Forcefully release the outstanding mailbox command if timed out */
10174 	if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10175 		spin_lock_irq(&phba->hbalock);
10176 		mboxq = phba->sli.mbox_active;
10177 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10178 		__lpfc_mbox_cmpl_put(phba, mboxq);
10179 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10180 		phba->sli.mbox_active = NULL;
10181 		spin_unlock_irq(&phba->hbalock);
10182 	}
10183 
10184 	/* Abort all iocbs associated with the hba */
10185 	lpfc_sli_hba_iocb_abort(phba);
10186 
10187 	/* Wait for completion of device XRI exchange busy */
10188 	lpfc_sli4_xri_exchange_busy_wait(phba);
10189 
10190 	/* Disable PCI subsystem interrupt */
10191 	lpfc_sli4_disable_intr(phba);
10192 
10193 	/* Disable SR-IOV if enabled */
10194 	if (phba->cfg_sriov_nr_virtfn)
10195 		pci_disable_sriov(pdev);
10196 
10197 	/* Stop kthread signal shall trigger work_done one more time */
10198 	kthread_stop(phba->worker_thread);
10199 
10200 	/* Unset the queues shared with the hardware then release all
10201 	 * allocated resources.
10202 	 */
10203 	lpfc_sli4_queue_unset(phba);
10204 	lpfc_sli4_queue_destroy(phba);
10205 
10206 	/* Reset SLI4 HBA FCoE function */
10207 	lpfc_pci_function_reset(phba);
10208 
10209 	/* Stop the SLI4 device port */
10210 	phba->pport->work_port_events = 0;
10211 }
10212 
10213  /**
10214  * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10215  * @phba: Pointer to HBA context object.
10216  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10217  *
10218  * This function is called in the SLI4 code path to read the port's
10219  * sli4 capabilities.
10220  *
10221  * This function may be be called from any context that can block-wait
10222  * for the completion.  The expectation is that this routine is called
10223  * typically from probe_one or from the online routine.
10224  **/
10225 int
10226 lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10227 {
10228 	int rc;
10229 	struct lpfc_mqe *mqe;
10230 	struct lpfc_pc_sli4_params *sli4_params;
10231 	uint32_t mbox_tmo;
10232 
10233 	rc = 0;
10234 	mqe = &mboxq->u.mqe;
10235 
10236 	/* Read the port's SLI4 Parameters port capabilities */
10237 	lpfc_pc_sli4_params(mboxq);
10238 	if (!phba->sli4_hba.intr_enable)
10239 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10240 	else {
10241 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10242 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10243 	}
10244 
10245 	if (unlikely(rc))
10246 		return 1;
10247 
10248 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10249 	sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10250 	sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10251 	sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10252 	sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10253 					     &mqe->un.sli4_params);
10254 	sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10255 					     &mqe->un.sli4_params);
10256 	sli4_params->proto_types = mqe->un.sli4_params.word3;
10257 	sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10258 	sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10259 	sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10260 	sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10261 	sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10262 	sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10263 	sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10264 	sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10265 	sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10266 	sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10267 	sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10268 	sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10269 	sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10270 	sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10271 	sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10272 	sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10273 	sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10274 	sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10275 	sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10276 	sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
10277 
10278 	/* Make sure that sge_supp_len can be handled by the driver */
10279 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10280 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10281 
10282 	return rc;
10283 }
10284 
10285 /**
10286  * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10287  * @phba: Pointer to HBA context object.
10288  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10289  *
10290  * This function is called in the SLI4 code path to read the port's
10291  * sli4 capabilities.
10292  *
10293  * This function may be be called from any context that can block-wait
10294  * for the completion.  The expectation is that this routine is called
10295  * typically from probe_one or from the online routine.
10296  **/
10297 int
10298 lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10299 {
10300 	int rc;
10301 	struct lpfc_mqe *mqe = &mboxq->u.mqe;
10302 	struct lpfc_pc_sli4_params *sli4_params;
10303 	uint32_t mbox_tmo;
10304 	int length;
10305 	struct lpfc_sli4_parameters *mbx_sli4_parameters;
10306 
10307 	/*
10308 	 * By default, the driver assumes the SLI4 port requires RPI
10309 	 * header postings.  The SLI4_PARAM response will correct this
10310 	 * assumption.
10311 	 */
10312 	phba->sli4_hba.rpi_hdrs_in_use = 1;
10313 
10314 	/* Read the port's SLI4 Config Parameters */
10315 	length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10316 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10317 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10318 			 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10319 			 length, LPFC_SLI4_MBX_EMBED);
10320 	if (!phba->sli4_hba.intr_enable)
10321 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10322 	else {
10323 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10324 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10325 	}
10326 	if (unlikely(rc))
10327 		return rc;
10328 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10329 	mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10330 	sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10331 	sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10332 	sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10333 	sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10334 					     mbx_sli4_parameters);
10335 	sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10336 					     mbx_sli4_parameters);
10337 	if (bf_get(cfg_phwq, mbx_sli4_parameters))
10338 		phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10339 	else
10340 		phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10341 	sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10342 	sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
10343 	sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
10344 	sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10345 	sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10346 	sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10347 	sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
10348 	sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
10349 	sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10350 					    mbx_sli4_parameters);
10351 	sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
10352 	sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10353 					   mbx_sli4_parameters);
10354 	phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10355 	phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
10356 	phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10357 			      bf_get(cfg_xib, mbx_sli4_parameters));
10358 
10359 	if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10360 	    !phba->nvme_support) {
10361 		phba->nvme_support = 0;
10362 		phba->nvmet_support = 0;
10363 		phba->cfg_nvmet_mrq = 0;
10364 		phba->cfg_nvme_io_channel = 0;
10365 		phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10366 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10367 				"6101 Disabling NVME support: "
10368 				"Not supported by firmware: %d %d\n",
10369 				bf_get(cfg_nvme, mbx_sli4_parameters),
10370 				bf_get(cfg_xib, mbx_sli4_parameters));
10371 
10372 		/* If firmware doesn't support NVME, just use SCSI support */
10373 		if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10374 			return -ENODEV;
10375 		phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10376 	}
10377 
10378 	if (bf_get(cfg_xib, mbx_sli4_parameters) && phba->cfg_suppress_rsp)
10379 		phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
10380 
10381 	if (bf_get(cfg_eqdr, mbx_sli4_parameters))
10382 		phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
10383 
10384 	/* Make sure that sge_supp_len can be handled by the driver */
10385 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10386 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10387 
10388 	/*
10389 	 * Issue IOs with CDB embedded in WQE to minimized the number
10390 	 * of DMAs the firmware has to do. Setting this to 1 also forces
10391 	 * the driver to use 128 bytes WQEs for FCP IOs.
10392 	 */
10393 	if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10394 		phba->fcp_embed_io = 1;
10395 	else
10396 		phba->fcp_embed_io = 0;
10397 
10398 	/*
10399 	 * Check if the SLI port supports MDS Diagnostics
10400 	 */
10401 	if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10402 		phba->mds_diags_support = 1;
10403 	else
10404 		phba->mds_diags_support = 0;
10405 	return 0;
10406 }
10407 
10408 /**
10409  * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10410  * @pdev: pointer to PCI device
10411  * @pid: pointer to PCI device identifier
10412  *
10413  * This routine is to be called to attach a device with SLI-3 interface spec
10414  * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10415  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10416  * information of the device and driver to see if the driver state that it can
10417  * support this kind of device. If the match is successful, the driver core
10418  * invokes this routine. If this routine determines it can claim the HBA, it
10419  * does all the initialization that it needs to do to handle the HBA properly.
10420  *
10421  * Return code
10422  * 	0 - driver can claim the device
10423  * 	negative value - driver can not claim the device
10424  **/
10425 static int
10426 lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10427 {
10428 	struct lpfc_hba   *phba;
10429 	struct lpfc_vport *vport = NULL;
10430 	struct Scsi_Host  *shost = NULL;
10431 	int error;
10432 	uint32_t cfg_mode, intr_mode;
10433 
10434 	/* Allocate memory for HBA structure */
10435 	phba = lpfc_hba_alloc(pdev);
10436 	if (!phba)
10437 		return -ENOMEM;
10438 
10439 	/* Perform generic PCI device enabling operation */
10440 	error = lpfc_enable_pci_dev(phba);
10441 	if (error)
10442 		goto out_free_phba;
10443 
10444 	/* Set up SLI API function jump table for PCI-device group-0 HBAs */
10445 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10446 	if (error)
10447 		goto out_disable_pci_dev;
10448 
10449 	/* Set up SLI-3 specific device PCI memory space */
10450 	error = lpfc_sli_pci_mem_setup(phba);
10451 	if (error) {
10452 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10453 				"1402 Failed to set up pci memory space.\n");
10454 		goto out_disable_pci_dev;
10455 	}
10456 
10457 	/* Set up SLI-3 specific device driver resources */
10458 	error = lpfc_sli_driver_resource_setup(phba);
10459 	if (error) {
10460 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10461 				"1404 Failed to set up driver resource.\n");
10462 		goto out_unset_pci_mem_s3;
10463 	}
10464 
10465 	/* Initialize and populate the iocb list per host */
10466 
10467 	error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10468 	if (error) {
10469 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10470 				"1405 Failed to initialize iocb list.\n");
10471 		goto out_unset_driver_resource_s3;
10472 	}
10473 
10474 	/* Set up common device driver resources */
10475 	error = lpfc_setup_driver_resource_phase2(phba);
10476 	if (error) {
10477 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10478 				"1406 Failed to set up driver resource.\n");
10479 		goto out_free_iocb_list;
10480 	}
10481 
10482 	/* Get the default values for Model Name and Description */
10483 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10484 
10485 	/* Create SCSI host to the physical port */
10486 	error = lpfc_create_shost(phba);
10487 	if (error) {
10488 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10489 				"1407 Failed to create scsi host.\n");
10490 		goto out_unset_driver_resource;
10491 	}
10492 
10493 	/* Configure sysfs attributes */
10494 	vport = phba->pport;
10495 	error = lpfc_alloc_sysfs_attr(vport);
10496 	if (error) {
10497 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10498 				"1476 Failed to allocate sysfs attr\n");
10499 		goto out_destroy_shost;
10500 	}
10501 
10502 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
10503 	/* Now, trying to enable interrupt and bring up the device */
10504 	cfg_mode = phba->cfg_use_msi;
10505 	while (true) {
10506 		/* Put device to a known state before enabling interrupt */
10507 		lpfc_stop_port(phba);
10508 		/* Configure and enable interrupt */
10509 		intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10510 		if (intr_mode == LPFC_INTR_ERROR) {
10511 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10512 					"0431 Failed to enable interrupt.\n");
10513 			error = -ENODEV;
10514 			goto out_free_sysfs_attr;
10515 		}
10516 		/* SLI-3 HBA setup */
10517 		if (lpfc_sli_hba_setup(phba)) {
10518 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10519 					"1477 Failed to set up hba\n");
10520 			error = -ENODEV;
10521 			goto out_remove_device;
10522 		}
10523 
10524 		/* Wait 50ms for the interrupts of previous mailbox commands */
10525 		msleep(50);
10526 		/* Check active interrupts on message signaled interrupts */
10527 		if (intr_mode == 0 ||
10528 		    phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10529 			/* Log the current active interrupt mode */
10530 			phba->intr_mode = intr_mode;
10531 			lpfc_log_intr_mode(phba, intr_mode);
10532 			break;
10533 		} else {
10534 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10535 					"0447 Configure interrupt mode (%d) "
10536 					"failed active interrupt test.\n",
10537 					intr_mode);
10538 			/* Disable the current interrupt mode */
10539 			lpfc_sli_disable_intr(phba);
10540 			/* Try next level of interrupt mode */
10541 			cfg_mode = --intr_mode;
10542 		}
10543 	}
10544 
10545 	/* Perform post initialization setup */
10546 	lpfc_post_init_setup(phba);
10547 
10548 	/* Check if there are static vports to be created. */
10549 	lpfc_create_static_vport(phba);
10550 
10551 	return 0;
10552 
10553 out_remove_device:
10554 	lpfc_unset_hba(phba);
10555 out_free_sysfs_attr:
10556 	lpfc_free_sysfs_attr(vport);
10557 out_destroy_shost:
10558 	lpfc_destroy_shost(phba);
10559 out_unset_driver_resource:
10560 	lpfc_unset_driver_resource_phase2(phba);
10561 out_free_iocb_list:
10562 	lpfc_free_iocb_list(phba);
10563 out_unset_driver_resource_s3:
10564 	lpfc_sli_driver_resource_unset(phba);
10565 out_unset_pci_mem_s3:
10566 	lpfc_sli_pci_mem_unset(phba);
10567 out_disable_pci_dev:
10568 	lpfc_disable_pci_dev(phba);
10569 	if (shost)
10570 		scsi_host_put(shost);
10571 out_free_phba:
10572 	lpfc_hba_free(phba);
10573 	return error;
10574 }
10575 
10576 /**
10577  * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
10578  * @pdev: pointer to PCI device
10579  *
10580  * This routine is to be called to disattach a device with SLI-3 interface
10581  * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10582  * removed from PCI bus, it performs all the necessary cleanup for the HBA
10583  * device to be removed from the PCI subsystem properly.
10584  **/
10585 static void
10586 lpfc_pci_remove_one_s3(struct pci_dev *pdev)
10587 {
10588 	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
10589 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10590 	struct lpfc_vport **vports;
10591 	struct lpfc_hba   *phba = vport->phba;
10592 	int i;
10593 
10594 	spin_lock_irq(&phba->hbalock);
10595 	vport->load_flag |= FC_UNLOADING;
10596 	spin_unlock_irq(&phba->hbalock);
10597 
10598 	lpfc_free_sysfs_attr(vport);
10599 
10600 	/* Release all the vports against this physical port */
10601 	vports = lpfc_create_vport_work_array(phba);
10602 	if (vports != NULL)
10603 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10604 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10605 				continue;
10606 			fc_vport_terminate(vports[i]->fc_vport);
10607 		}
10608 	lpfc_destroy_vport_work_array(phba, vports);
10609 
10610 	/* Remove FC host and then SCSI host with the physical port */
10611 	fc_remove_host(shost);
10612 	scsi_remove_host(shost);
10613 
10614 	lpfc_cleanup(vport);
10615 
10616 	/*
10617 	 * Bring down the SLI Layer. This step disable all interrupts,
10618 	 * clears the rings, discards all mailbox commands, and resets
10619 	 * the HBA.
10620 	 */
10621 
10622 	/* HBA interrupt will be disabled after this call */
10623 	lpfc_sli_hba_down(phba);
10624 	/* Stop kthread signal shall trigger work_done one more time */
10625 	kthread_stop(phba->worker_thread);
10626 	/* Final cleanup of txcmplq and reset the HBA */
10627 	lpfc_sli_brdrestart(phba);
10628 
10629 	kfree(phba->vpi_bmask);
10630 	kfree(phba->vpi_ids);
10631 
10632 	lpfc_stop_hba_timers(phba);
10633 	spin_lock_irq(&phba->hbalock);
10634 	list_del_init(&vport->listentry);
10635 	spin_unlock_irq(&phba->hbalock);
10636 
10637 	lpfc_debugfs_terminate(vport);
10638 
10639 	/* Disable SR-IOV if enabled */
10640 	if (phba->cfg_sriov_nr_virtfn)
10641 		pci_disable_sriov(pdev);
10642 
10643 	/* Disable interrupt */
10644 	lpfc_sli_disable_intr(phba);
10645 
10646 	scsi_host_put(shost);
10647 
10648 	/*
10649 	 * Call scsi_free before mem_free since scsi bufs are released to their
10650 	 * corresponding pools here.
10651 	 */
10652 	lpfc_scsi_free(phba);
10653 	lpfc_mem_free_all(phba);
10654 
10655 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10656 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
10657 
10658 	/* Free resources associated with SLI2 interface */
10659 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
10660 			  phba->slim2p.virt, phba->slim2p.phys);
10661 
10662 	/* unmap adapter SLIM and Control Registers */
10663 	iounmap(phba->ctrl_regs_memmap_p);
10664 	iounmap(phba->slim_memmap_p);
10665 
10666 	lpfc_hba_free(phba);
10667 
10668 	pci_release_mem_regions(pdev);
10669 	pci_disable_device(pdev);
10670 }
10671 
10672 /**
10673  * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
10674  * @pdev: pointer to PCI device
10675  * @msg: power management message
10676  *
10677  * This routine is to be called from the kernel's PCI subsystem to support
10678  * system Power Management (PM) to device with SLI-3 interface spec. When
10679  * PM invokes this method, it quiesces the device by stopping the driver's
10680  * worker thread for the device, turning off device's interrupt and DMA,
10681  * and bring the device offline. Note that as the driver implements the
10682  * minimum PM requirements to a power-aware driver's PM support for the
10683  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10684  * to the suspend() method call will be treated as SUSPEND and the driver will
10685  * fully reinitialize its device during resume() method call, the driver will
10686  * set device to PCI_D3hot state in PCI config space instead of setting it
10687  * according to the @msg provided by the PM.
10688  *
10689  * Return code
10690  * 	0 - driver suspended the device
10691  * 	Error otherwise
10692  **/
10693 static int
10694 lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
10695 {
10696 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10697 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10698 
10699 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10700 			"0473 PCI device Power Management suspend.\n");
10701 
10702 	/* Bring down the device */
10703 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
10704 	lpfc_offline(phba);
10705 	kthread_stop(phba->worker_thread);
10706 
10707 	/* Disable interrupt from device */
10708 	lpfc_sli_disable_intr(phba);
10709 
10710 	/* Save device state to PCI config space */
10711 	pci_save_state(pdev);
10712 	pci_set_power_state(pdev, PCI_D3hot);
10713 
10714 	return 0;
10715 }
10716 
10717 /**
10718  * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
10719  * @pdev: pointer to PCI device
10720  *
10721  * This routine is to be called from the kernel's PCI subsystem to support
10722  * system Power Management (PM) to device with SLI-3 interface spec. When PM
10723  * invokes this method, it restores the device's PCI config space state and
10724  * fully reinitializes the device and brings it online. Note that as the
10725  * driver implements the minimum PM requirements to a power-aware driver's
10726  * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10727  * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10728  * driver will fully reinitialize its device during resume() method call,
10729  * the device will be set to PCI_D0 directly in PCI config space before
10730  * restoring the state.
10731  *
10732  * Return code
10733  * 	0 - driver suspended the device
10734  * 	Error otherwise
10735  **/
10736 static int
10737 lpfc_pci_resume_one_s3(struct pci_dev *pdev)
10738 {
10739 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10740 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10741 	uint32_t intr_mode;
10742 	int error;
10743 
10744 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10745 			"0452 PCI device Power Management resume.\n");
10746 
10747 	/* Restore device state from PCI config space */
10748 	pci_set_power_state(pdev, PCI_D0);
10749 	pci_restore_state(pdev);
10750 
10751 	/*
10752 	 * As the new kernel behavior of pci_restore_state() API call clears
10753 	 * device saved_state flag, need to save the restored state again.
10754 	 */
10755 	pci_save_state(pdev);
10756 
10757 	if (pdev->is_busmaster)
10758 		pci_set_master(pdev);
10759 
10760 	/* Startup the kernel thread for this host adapter. */
10761 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
10762 					"lpfc_worker_%d", phba->brd_no);
10763 	if (IS_ERR(phba->worker_thread)) {
10764 		error = PTR_ERR(phba->worker_thread);
10765 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10766 				"0434 PM resume failed to start worker "
10767 				"thread: error=x%x.\n", error);
10768 		return error;
10769 	}
10770 
10771 	/* Configure and enable interrupt */
10772 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
10773 	if (intr_mode == LPFC_INTR_ERROR) {
10774 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10775 				"0430 PM resume Failed to enable interrupt\n");
10776 		return -EIO;
10777 	} else
10778 		phba->intr_mode = intr_mode;
10779 
10780 	/* Restart HBA and bring it online */
10781 	lpfc_sli_brdrestart(phba);
10782 	lpfc_online(phba);
10783 
10784 	/* Log the current active interrupt mode */
10785 	lpfc_log_intr_mode(phba, phba->intr_mode);
10786 
10787 	return 0;
10788 }
10789 
10790 /**
10791  * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10792  * @phba: pointer to lpfc hba data structure.
10793  *
10794  * This routine is called to prepare the SLI3 device for PCI slot recover. It
10795  * aborts all the outstanding SCSI I/Os to the pci device.
10796  **/
10797 static void
10798 lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10799 {
10800 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10801 			"2723 PCI channel I/O abort preparing for recovery\n");
10802 
10803 	/*
10804 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10805 	 * and let the SCSI mid-layer to retry them to recover.
10806 	 */
10807 	lpfc_sli_abort_fcp_rings(phba);
10808 }
10809 
10810 /**
10811  * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10812  * @phba: pointer to lpfc hba data structure.
10813  *
10814  * This routine is called to prepare the SLI3 device for PCI slot reset. It
10815  * disables the device interrupt and pci device, and aborts the internal FCP
10816  * pending I/Os.
10817  **/
10818 static void
10819 lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10820 {
10821 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10822 			"2710 PCI channel disable preparing for reset\n");
10823 
10824 	/* Block any management I/Os to the device */
10825 	lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
10826 
10827 	/* Block all SCSI devices' I/Os on the host */
10828 	lpfc_scsi_dev_block(phba);
10829 
10830 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
10831 	lpfc_sli_flush_fcp_rings(phba);
10832 
10833 	/* stop all timers */
10834 	lpfc_stop_hba_timers(phba);
10835 
10836 	/* Disable interrupt and pci device */
10837 	lpfc_sli_disable_intr(phba);
10838 	pci_disable_device(phba->pcidev);
10839 }
10840 
10841 /**
10842  * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10843  * @phba: pointer to lpfc hba data structure.
10844  *
10845  * This routine is called to prepare the SLI3 device for PCI slot permanently
10846  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10847  * pending I/Os.
10848  **/
10849 static void
10850 lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10851 {
10852 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10853 			"2711 PCI channel permanent disable for failure\n");
10854 	/* Block all SCSI devices' I/Os on the host */
10855 	lpfc_scsi_dev_block(phba);
10856 
10857 	/* stop all timers */
10858 	lpfc_stop_hba_timers(phba);
10859 
10860 	/* Clean up all driver's outstanding SCSI I/Os */
10861 	lpfc_sli_flush_fcp_rings(phba);
10862 }
10863 
10864 /**
10865  * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
10866  * @pdev: pointer to PCI device.
10867  * @state: the current PCI connection state.
10868  *
10869  * This routine is called from the PCI subsystem for I/O error handling to
10870  * device with SLI-3 interface spec. This function is called by the PCI
10871  * subsystem after a PCI bus error affecting this device has been detected.
10872  * When this function is invoked, it will need to stop all the I/Os and
10873  * interrupt(s) to the device. Once that is done, it will return
10874  * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10875  * as desired.
10876  *
10877  * Return codes
10878  * 	PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
10879  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10880  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10881  **/
10882 static pci_ers_result_t
10883 lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
10884 {
10885 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10886 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10887 
10888 	switch (state) {
10889 	case pci_channel_io_normal:
10890 		/* Non-fatal error, prepare for recovery */
10891 		lpfc_sli_prep_dev_for_recover(phba);
10892 		return PCI_ERS_RESULT_CAN_RECOVER;
10893 	case pci_channel_io_frozen:
10894 		/* Fatal error, prepare for slot reset */
10895 		lpfc_sli_prep_dev_for_reset(phba);
10896 		return PCI_ERS_RESULT_NEED_RESET;
10897 	case pci_channel_io_perm_failure:
10898 		/* Permanent failure, prepare for device down */
10899 		lpfc_sli_prep_dev_for_perm_failure(phba);
10900 		return PCI_ERS_RESULT_DISCONNECT;
10901 	default:
10902 		/* Unknown state, prepare and request slot reset */
10903 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10904 				"0472 Unknown PCI error state: x%x\n", state);
10905 		lpfc_sli_prep_dev_for_reset(phba);
10906 		return PCI_ERS_RESULT_NEED_RESET;
10907 	}
10908 }
10909 
10910 /**
10911  * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
10912  * @pdev: pointer to PCI device.
10913  *
10914  * This routine is called from the PCI subsystem for error handling to
10915  * device with SLI-3 interface spec. This is called after PCI bus has been
10916  * reset to restart the PCI card from scratch, as if from a cold-boot.
10917  * During the PCI subsystem error recovery, after driver returns
10918  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10919  * recovery and then call this routine before calling the .resume method
10920  * to recover the device. This function will initialize the HBA device,
10921  * enable the interrupt, but it will just put the HBA to offline state
10922  * without passing any I/O traffic.
10923  *
10924  * Return codes
10925  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
10926  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10927  */
10928 static pci_ers_result_t
10929 lpfc_io_slot_reset_s3(struct pci_dev *pdev)
10930 {
10931 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10932 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10933 	struct lpfc_sli *psli = &phba->sli;
10934 	uint32_t intr_mode;
10935 
10936 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10937 	if (pci_enable_device_mem(pdev)) {
10938 		printk(KERN_ERR "lpfc: Cannot re-enable "
10939 			"PCI device after reset.\n");
10940 		return PCI_ERS_RESULT_DISCONNECT;
10941 	}
10942 
10943 	pci_restore_state(pdev);
10944 
10945 	/*
10946 	 * As the new kernel behavior of pci_restore_state() API call clears
10947 	 * device saved_state flag, need to save the restored state again.
10948 	 */
10949 	pci_save_state(pdev);
10950 
10951 	if (pdev->is_busmaster)
10952 		pci_set_master(pdev);
10953 
10954 	spin_lock_irq(&phba->hbalock);
10955 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10956 	spin_unlock_irq(&phba->hbalock);
10957 
10958 	/* Configure and enable interrupt */
10959 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
10960 	if (intr_mode == LPFC_INTR_ERROR) {
10961 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10962 				"0427 Cannot re-enable interrupt after "
10963 				"slot reset.\n");
10964 		return PCI_ERS_RESULT_DISCONNECT;
10965 	} else
10966 		phba->intr_mode = intr_mode;
10967 
10968 	/* Take device offline, it will perform cleanup */
10969 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
10970 	lpfc_offline(phba);
10971 	lpfc_sli_brdrestart(phba);
10972 
10973 	/* Log the current active interrupt mode */
10974 	lpfc_log_intr_mode(phba, phba->intr_mode);
10975 
10976 	return PCI_ERS_RESULT_RECOVERED;
10977 }
10978 
10979 /**
10980  * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
10981  * @pdev: pointer to PCI device
10982  *
10983  * This routine is called from the PCI subsystem for error handling to device
10984  * with SLI-3 interface spec. It is called when kernel error recovery tells
10985  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10986  * error recovery. After this call, traffic can start to flow from this device
10987  * again.
10988  */
10989 static void
10990 lpfc_io_resume_s3(struct pci_dev *pdev)
10991 {
10992 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10993 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10994 
10995 	/* Bring device online, it will be no-op for non-fatal error resume */
10996 	lpfc_online(phba);
10997 
10998 	/* Clean up Advanced Error Reporting (AER) if needed */
10999 	if (phba->hba_flag & HBA_AER_ENABLED)
11000 		pci_cleanup_aer_uncorrect_error_status(pdev);
11001 }
11002 
11003 /**
11004  * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
11005  * @phba: pointer to lpfc hba data structure.
11006  *
11007  * returns the number of ELS/CT IOCBs to reserve
11008  **/
11009 int
11010 lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
11011 {
11012 	int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
11013 
11014 	if (phba->sli_rev == LPFC_SLI_REV4) {
11015 		if (max_xri <= 100)
11016 			return 10;
11017 		else if (max_xri <= 256)
11018 			return 25;
11019 		else if (max_xri <= 512)
11020 			return 50;
11021 		else if (max_xri <= 1024)
11022 			return 100;
11023 		else if (max_xri <= 1536)
11024 			return 150;
11025 		else if (max_xri <= 2048)
11026 			return 200;
11027 		else
11028 			return 250;
11029 	} else
11030 		return 0;
11031 }
11032 
11033 /**
11034  * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
11035  * @phba: pointer to lpfc hba data structure.
11036  *
11037  * returns the number of ELS/CT + NVMET IOCBs to reserve
11038  **/
11039 int
11040 lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
11041 {
11042 	int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
11043 
11044 	if (phba->nvmet_support)
11045 		max_xri += LPFC_NVMET_BUF_POST;
11046 	return max_xri;
11047 }
11048 
11049 
11050 /**
11051  * lpfc_write_firmware - attempt to write a firmware image to the port
11052  * @fw: pointer to firmware image returned from request_firmware.
11053  * @phba: pointer to lpfc hba data structure.
11054  *
11055  **/
11056 static void
11057 lpfc_write_firmware(const struct firmware *fw, void *context)
11058 {
11059 	struct lpfc_hba *phba = (struct lpfc_hba *)context;
11060 	char fwrev[FW_REV_STR_SIZE];
11061 	struct lpfc_grp_hdr *image;
11062 	struct list_head dma_buffer_list;
11063 	int i, rc = 0;
11064 	struct lpfc_dmabuf *dmabuf, *next;
11065 	uint32_t offset = 0, temp_offset = 0;
11066 	uint32_t magic_number, ftype, fid, fsize;
11067 
11068 	/* It can be null in no-wait mode, sanity check */
11069 	if (!fw) {
11070 		rc = -ENXIO;
11071 		goto out;
11072 	}
11073 	image = (struct lpfc_grp_hdr *)fw->data;
11074 
11075 	magic_number = be32_to_cpu(image->magic_number);
11076 	ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
11077 	fid = bf_get_be32(lpfc_grp_hdr_id, image),
11078 	fsize = be32_to_cpu(image->size);
11079 
11080 	INIT_LIST_HEAD(&dma_buffer_list);
11081 	if ((magic_number != LPFC_GROUP_OJECT_MAGIC_G5 &&
11082 	     magic_number != LPFC_GROUP_OJECT_MAGIC_G6) ||
11083 	    ftype != LPFC_FILE_TYPE_GROUP || fsize != fw->size) {
11084 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11085 				"3022 Invalid FW image found. "
11086 				"Magic:%x Type:%x ID:%x Size %d %zd\n",
11087 				magic_number, ftype, fid, fsize, fw->size);
11088 		rc = -EINVAL;
11089 		goto release_out;
11090 	}
11091 	lpfc_decode_firmware_rev(phba, fwrev, 1);
11092 	if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
11093 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11094 				"3023 Updating Firmware, Current Version:%s "
11095 				"New Version:%s\n",
11096 				fwrev, image->revision);
11097 		for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
11098 			dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
11099 					 GFP_KERNEL);
11100 			if (!dmabuf) {
11101 				rc = -ENOMEM;
11102 				goto release_out;
11103 			}
11104 			dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11105 							  SLI4_PAGE_SIZE,
11106 							  &dmabuf->phys,
11107 							  GFP_KERNEL);
11108 			if (!dmabuf->virt) {
11109 				kfree(dmabuf);
11110 				rc = -ENOMEM;
11111 				goto release_out;
11112 			}
11113 			list_add_tail(&dmabuf->list, &dma_buffer_list);
11114 		}
11115 		while (offset < fw->size) {
11116 			temp_offset = offset;
11117 			list_for_each_entry(dmabuf, &dma_buffer_list, list) {
11118 				if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
11119 					memcpy(dmabuf->virt,
11120 					       fw->data + temp_offset,
11121 					       fw->size - temp_offset);
11122 					temp_offset = fw->size;
11123 					break;
11124 				}
11125 				memcpy(dmabuf->virt, fw->data + temp_offset,
11126 				       SLI4_PAGE_SIZE);
11127 				temp_offset += SLI4_PAGE_SIZE;
11128 			}
11129 			rc = lpfc_wr_object(phba, &dma_buffer_list,
11130 				    (fw->size - offset), &offset);
11131 			if (rc)
11132 				goto release_out;
11133 		}
11134 		rc = offset;
11135 	}
11136 
11137 release_out:
11138 	list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
11139 		list_del(&dmabuf->list);
11140 		dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
11141 				  dmabuf->virt, dmabuf->phys);
11142 		kfree(dmabuf);
11143 	}
11144 	release_firmware(fw);
11145 out:
11146 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11147 			"3024 Firmware update done: %d.\n", rc);
11148 	return;
11149 }
11150 
11151 /**
11152  * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11153  * @phba: pointer to lpfc hba data structure.
11154  *
11155  * This routine is called to perform Linux generic firmware upgrade on device
11156  * that supports such feature.
11157  **/
11158 int
11159 lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11160 {
11161 	uint8_t file_name[ELX_MODEL_NAME_SIZE];
11162 	int ret;
11163 	const struct firmware *fw;
11164 
11165 	/* Only supported on SLI4 interface type 2 for now */
11166 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11167 	    LPFC_SLI_INTF_IF_TYPE_2)
11168 		return -EPERM;
11169 
11170 	snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11171 
11172 	if (fw_upgrade == INT_FW_UPGRADE) {
11173 		ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11174 					file_name, &phba->pcidev->dev,
11175 					GFP_KERNEL, (void *)phba,
11176 					lpfc_write_firmware);
11177 	} else if (fw_upgrade == RUN_FW_UPGRADE) {
11178 		ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11179 		if (!ret)
11180 			lpfc_write_firmware(fw, (void *)phba);
11181 	} else {
11182 		ret = -EINVAL;
11183 	}
11184 
11185 	return ret;
11186 }
11187 
11188 /**
11189  * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11190  * @pdev: pointer to PCI device
11191  * @pid: pointer to PCI device identifier
11192  *
11193  * This routine is called from the kernel's PCI subsystem to device with
11194  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11195  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11196  * information of the device and driver to see if the driver state that it
11197  * can support this kind of device. If the match is successful, the driver
11198  * core invokes this routine. If this routine determines it can claim the HBA,
11199  * it does all the initialization that it needs to do to handle the HBA
11200  * properly.
11201  *
11202  * Return code
11203  * 	0 - driver can claim the device
11204  * 	negative value - driver can not claim the device
11205  **/
11206 static int
11207 lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11208 {
11209 	struct lpfc_hba   *phba;
11210 	struct lpfc_vport *vport = NULL;
11211 	struct Scsi_Host  *shost = NULL;
11212 	int error;
11213 	uint32_t cfg_mode, intr_mode;
11214 
11215 	/* Allocate memory for HBA structure */
11216 	phba = lpfc_hba_alloc(pdev);
11217 	if (!phba)
11218 		return -ENOMEM;
11219 
11220 	/* Perform generic PCI device enabling operation */
11221 	error = lpfc_enable_pci_dev(phba);
11222 	if (error)
11223 		goto out_free_phba;
11224 
11225 	/* Set up SLI API function jump table for PCI-device group-1 HBAs */
11226 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11227 	if (error)
11228 		goto out_disable_pci_dev;
11229 
11230 	/* Set up SLI-4 specific device PCI memory space */
11231 	error = lpfc_sli4_pci_mem_setup(phba);
11232 	if (error) {
11233 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11234 				"1410 Failed to set up pci memory space.\n");
11235 		goto out_disable_pci_dev;
11236 	}
11237 
11238 	/* Set up SLI-4 Specific device driver resources */
11239 	error = lpfc_sli4_driver_resource_setup(phba);
11240 	if (error) {
11241 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11242 				"1412 Failed to set up driver resource.\n");
11243 		goto out_unset_pci_mem_s4;
11244 	}
11245 
11246 	INIT_LIST_HEAD(&phba->active_rrq_list);
11247 	INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
11248 
11249 	/* Set up common device driver resources */
11250 	error = lpfc_setup_driver_resource_phase2(phba);
11251 	if (error) {
11252 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11253 				"1414 Failed to set up driver resource.\n");
11254 		goto out_unset_driver_resource_s4;
11255 	}
11256 
11257 	/* Get the default values for Model Name and Description */
11258 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11259 
11260 	/* Create SCSI host to the physical port */
11261 	error = lpfc_create_shost(phba);
11262 	if (error) {
11263 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11264 				"1415 Failed to create scsi host.\n");
11265 		goto out_unset_driver_resource;
11266 	}
11267 
11268 	/* Configure sysfs attributes */
11269 	vport = phba->pport;
11270 	error = lpfc_alloc_sysfs_attr(vport);
11271 	if (error) {
11272 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11273 				"1416 Failed to allocate sysfs attr\n");
11274 		goto out_destroy_shost;
11275 	}
11276 
11277 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
11278 	/* Now, trying to enable interrupt and bring up the device */
11279 	cfg_mode = phba->cfg_use_msi;
11280 
11281 	/* Put device to a known state before enabling interrupt */
11282 	lpfc_stop_port(phba);
11283 
11284 	/* Configure and enable interrupt */
11285 	intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11286 	if (intr_mode == LPFC_INTR_ERROR) {
11287 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11288 				"0426 Failed to enable interrupt.\n");
11289 		error = -ENODEV;
11290 		goto out_free_sysfs_attr;
11291 	}
11292 	/* Default to single EQ for non-MSI-X */
11293 	if (phba->intr_type != MSIX) {
11294 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11295 			phba->cfg_fcp_io_channel = 1;
11296 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11297 			phba->cfg_nvme_io_channel = 1;
11298 			if (phba->nvmet_support)
11299 				phba->cfg_nvmet_mrq = 1;
11300 		}
11301 		phba->io_channel_irqs = 1;
11302 	}
11303 
11304 	/* Set up SLI-4 HBA */
11305 	if (lpfc_sli4_hba_setup(phba)) {
11306 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11307 				"1421 Failed to set up hba\n");
11308 		error = -ENODEV;
11309 		goto out_disable_intr;
11310 	}
11311 
11312 	/* Log the current active interrupt mode */
11313 	phba->intr_mode = intr_mode;
11314 	lpfc_log_intr_mode(phba, intr_mode);
11315 
11316 	/* Perform post initialization setup */
11317 	lpfc_post_init_setup(phba);
11318 
11319 	/* NVME support in FW earlier in the driver load corrects the
11320 	 * FC4 type making a check for nvme_support unnecessary.
11321 	 */
11322 	if ((phba->nvmet_support == 0) &&
11323 	    (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11324 		/* Create NVME binding with nvme_fc_transport. This
11325 		 * ensures the vport is initialized.  If the localport
11326 		 * create fails, it should not unload the driver to
11327 		 * support field issues.
11328 		 */
11329 		error = lpfc_nvme_create_localport(vport);
11330 		if (error) {
11331 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11332 					"6004 NVME registration failed, "
11333 					"error x%x\n",
11334 					error);
11335 		}
11336 	}
11337 
11338 	/* check for firmware upgrade or downgrade */
11339 	if (phba->cfg_request_firmware_upgrade)
11340 		lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
11341 
11342 	/* Check if there are static vports to be created. */
11343 	lpfc_create_static_vport(phba);
11344 	return 0;
11345 
11346 out_disable_intr:
11347 	lpfc_sli4_disable_intr(phba);
11348 out_free_sysfs_attr:
11349 	lpfc_free_sysfs_attr(vport);
11350 out_destroy_shost:
11351 	lpfc_destroy_shost(phba);
11352 out_unset_driver_resource:
11353 	lpfc_unset_driver_resource_phase2(phba);
11354 out_unset_driver_resource_s4:
11355 	lpfc_sli4_driver_resource_unset(phba);
11356 out_unset_pci_mem_s4:
11357 	lpfc_sli4_pci_mem_unset(phba);
11358 out_disable_pci_dev:
11359 	lpfc_disable_pci_dev(phba);
11360 	if (shost)
11361 		scsi_host_put(shost);
11362 out_free_phba:
11363 	lpfc_hba_free(phba);
11364 	return error;
11365 }
11366 
11367 /**
11368  * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11369  * @pdev: pointer to PCI device
11370  *
11371  * This routine is called from the kernel's PCI subsystem to device with
11372  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11373  * removed from PCI bus, it performs all the necessary cleanup for the HBA
11374  * device to be removed from the PCI subsystem properly.
11375  **/
11376 static void
11377 lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11378 {
11379 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11380 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11381 	struct lpfc_vport **vports;
11382 	struct lpfc_hba *phba = vport->phba;
11383 	int i;
11384 
11385 	/* Mark the device unloading flag */
11386 	spin_lock_irq(&phba->hbalock);
11387 	vport->load_flag |= FC_UNLOADING;
11388 	spin_unlock_irq(&phba->hbalock);
11389 
11390 	/* Free the HBA sysfs attributes */
11391 	lpfc_free_sysfs_attr(vport);
11392 
11393 	/* Release all the vports against this physical port */
11394 	vports = lpfc_create_vport_work_array(phba);
11395 	if (vports != NULL)
11396 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11397 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11398 				continue;
11399 			fc_vport_terminate(vports[i]->fc_vport);
11400 		}
11401 	lpfc_destroy_vport_work_array(phba, vports);
11402 
11403 	/* Remove FC host and then SCSI host with the physical port */
11404 	fc_remove_host(shost);
11405 	scsi_remove_host(shost);
11406 
11407 	/* Perform ndlp cleanup on the physical port.  The nvme and nvmet
11408 	 * localports are destroyed after to cleanup all transport memory.
11409 	 */
11410 	lpfc_cleanup(vport);
11411 	lpfc_nvmet_destroy_targetport(phba);
11412 	lpfc_nvme_destroy_localport(vport);
11413 
11414 	/*
11415 	 * Bring down the SLI Layer. This step disables all interrupts,
11416 	 * clears the rings, discards all mailbox commands, and resets
11417 	 * the HBA FCoE function.
11418 	 */
11419 	lpfc_debugfs_terminate(vport);
11420 	lpfc_sli4_hba_unset(phba);
11421 
11422 	spin_lock_irq(&phba->hbalock);
11423 	list_del_init(&vport->listentry);
11424 	spin_unlock_irq(&phba->hbalock);
11425 
11426 	/* Perform scsi free before driver resource_unset since scsi
11427 	 * buffers are released to their corresponding pools here.
11428 	 */
11429 	lpfc_scsi_free(phba);
11430 	lpfc_nvme_free(phba);
11431 	lpfc_free_iocb_list(phba);
11432 
11433 	lpfc_sli4_driver_resource_unset(phba);
11434 
11435 	/* Unmap adapter Control and Doorbell registers */
11436 	lpfc_sli4_pci_mem_unset(phba);
11437 
11438 	/* Release PCI resources and disable device's PCI function */
11439 	scsi_host_put(shost);
11440 	lpfc_disable_pci_dev(phba);
11441 
11442 	/* Finally, free the driver's device data structure */
11443 	lpfc_hba_free(phba);
11444 
11445 	return;
11446 }
11447 
11448 /**
11449  * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11450  * @pdev: pointer to PCI device
11451  * @msg: power management message
11452  *
11453  * This routine is called from the kernel's PCI subsystem to support system
11454  * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11455  * this method, it quiesces the device by stopping the driver's worker
11456  * thread for the device, turning off device's interrupt and DMA, and bring
11457  * the device offline. Note that as the driver implements the minimum PM
11458  * requirements to a power-aware driver's PM support for suspend/resume -- all
11459  * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11460  * method call will be treated as SUSPEND and the driver will fully
11461  * reinitialize its device during resume() method call, the driver will set
11462  * device to PCI_D3hot state in PCI config space instead of setting it
11463  * according to the @msg provided by the PM.
11464  *
11465  * Return code
11466  * 	0 - driver suspended the device
11467  * 	Error otherwise
11468  **/
11469 static int
11470 lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11471 {
11472 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11473 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11474 
11475 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11476 			"2843 PCI device Power Management suspend.\n");
11477 
11478 	/* Bring down the device */
11479 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11480 	lpfc_offline(phba);
11481 	kthread_stop(phba->worker_thread);
11482 
11483 	/* Disable interrupt from device */
11484 	lpfc_sli4_disable_intr(phba);
11485 	lpfc_sli4_queue_destroy(phba);
11486 
11487 	/* Save device state to PCI config space */
11488 	pci_save_state(pdev);
11489 	pci_set_power_state(pdev, PCI_D3hot);
11490 
11491 	return 0;
11492 }
11493 
11494 /**
11495  * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11496  * @pdev: pointer to PCI device
11497  *
11498  * This routine is called from the kernel's PCI subsystem to support system
11499  * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11500  * this method, it restores the device's PCI config space state and fully
11501  * reinitializes the device and brings it online. Note that as the driver
11502  * implements the minimum PM requirements to a power-aware driver's PM for
11503  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11504  * to the suspend() method call will be treated as SUSPEND and the driver
11505  * will fully reinitialize its device during resume() method call, the device
11506  * will be set to PCI_D0 directly in PCI config space before restoring the
11507  * state.
11508  *
11509  * Return code
11510  * 	0 - driver suspended the device
11511  * 	Error otherwise
11512  **/
11513 static int
11514 lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11515 {
11516 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11517 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11518 	uint32_t intr_mode;
11519 	int error;
11520 
11521 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11522 			"0292 PCI device Power Management resume.\n");
11523 
11524 	/* Restore device state from PCI config space */
11525 	pci_set_power_state(pdev, PCI_D0);
11526 	pci_restore_state(pdev);
11527 
11528 	/*
11529 	 * As the new kernel behavior of pci_restore_state() API call clears
11530 	 * device saved_state flag, need to save the restored state again.
11531 	 */
11532 	pci_save_state(pdev);
11533 
11534 	if (pdev->is_busmaster)
11535 		pci_set_master(pdev);
11536 
11537 	 /* Startup the kernel thread for this host adapter. */
11538 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
11539 					"lpfc_worker_%d", phba->brd_no);
11540 	if (IS_ERR(phba->worker_thread)) {
11541 		error = PTR_ERR(phba->worker_thread);
11542 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11543 				"0293 PM resume failed to start worker "
11544 				"thread: error=x%x.\n", error);
11545 		return error;
11546 	}
11547 
11548 	/* Configure and enable interrupt */
11549 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11550 	if (intr_mode == LPFC_INTR_ERROR) {
11551 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11552 				"0294 PM resume Failed to enable interrupt\n");
11553 		return -EIO;
11554 	} else
11555 		phba->intr_mode = intr_mode;
11556 
11557 	/* Restart HBA and bring it online */
11558 	lpfc_sli_brdrestart(phba);
11559 	lpfc_online(phba);
11560 
11561 	/* Log the current active interrupt mode */
11562 	lpfc_log_intr_mode(phba, phba->intr_mode);
11563 
11564 	return 0;
11565 }
11566 
11567 /**
11568  * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11569  * @phba: pointer to lpfc hba data structure.
11570  *
11571  * This routine is called to prepare the SLI4 device for PCI slot recover. It
11572  * aborts all the outstanding SCSI I/Os to the pci device.
11573  **/
11574 static void
11575 lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11576 {
11577 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11578 			"2828 PCI channel I/O abort preparing for recovery\n");
11579 	/*
11580 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11581 	 * and let the SCSI mid-layer to retry them to recover.
11582 	 */
11583 	lpfc_sli_abort_fcp_rings(phba);
11584 }
11585 
11586 /**
11587  * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11588  * @phba: pointer to lpfc hba data structure.
11589  *
11590  * This routine is called to prepare the SLI4 device for PCI slot reset. It
11591  * disables the device interrupt and pci device, and aborts the internal FCP
11592  * pending I/Os.
11593  **/
11594 static void
11595 lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11596 {
11597 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11598 			"2826 PCI channel disable preparing for reset\n");
11599 
11600 	/* Block any management I/Os to the device */
11601 	lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
11602 
11603 	/* Block all SCSI devices' I/Os on the host */
11604 	lpfc_scsi_dev_block(phba);
11605 
11606 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
11607 	lpfc_sli_flush_fcp_rings(phba);
11608 
11609 	/* stop all timers */
11610 	lpfc_stop_hba_timers(phba);
11611 
11612 	/* Disable interrupt and pci device */
11613 	lpfc_sli4_disable_intr(phba);
11614 	lpfc_sli4_queue_destroy(phba);
11615 	pci_disable_device(phba->pcidev);
11616 }
11617 
11618 /**
11619  * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11620  * @phba: pointer to lpfc hba data structure.
11621  *
11622  * This routine is called to prepare the SLI4 device for PCI slot permanently
11623  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11624  * pending I/Os.
11625  **/
11626 static void
11627 lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11628 {
11629 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11630 			"2827 PCI channel permanent disable for failure\n");
11631 
11632 	/* Block all SCSI devices' I/Os on the host */
11633 	lpfc_scsi_dev_block(phba);
11634 
11635 	/* stop all timers */
11636 	lpfc_stop_hba_timers(phba);
11637 
11638 	/* Clean up all driver's outstanding SCSI I/Os */
11639 	lpfc_sli_flush_fcp_rings(phba);
11640 }
11641 
11642 /**
11643  * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
11644  * @pdev: pointer to PCI device.
11645  * @state: the current PCI connection state.
11646  *
11647  * This routine is called from the PCI subsystem for error handling to device
11648  * with SLI-4 interface spec. This function is called by the PCI subsystem
11649  * after a PCI bus error affecting this device has been detected. When this
11650  * function is invoked, it will need to stop all the I/Os and interrupt(s)
11651  * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
11652  * for the PCI subsystem to perform proper recovery as desired.
11653  *
11654  * Return codes
11655  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11656  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11657  **/
11658 static pci_ers_result_t
11659 lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11660 {
11661 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11662 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11663 
11664 	switch (state) {
11665 	case pci_channel_io_normal:
11666 		/* Non-fatal error, prepare for recovery */
11667 		lpfc_sli4_prep_dev_for_recover(phba);
11668 		return PCI_ERS_RESULT_CAN_RECOVER;
11669 	case pci_channel_io_frozen:
11670 		/* Fatal error, prepare for slot reset */
11671 		lpfc_sli4_prep_dev_for_reset(phba);
11672 		return PCI_ERS_RESULT_NEED_RESET;
11673 	case pci_channel_io_perm_failure:
11674 		/* Permanent failure, prepare for device down */
11675 		lpfc_sli4_prep_dev_for_perm_failure(phba);
11676 		return PCI_ERS_RESULT_DISCONNECT;
11677 	default:
11678 		/* Unknown state, prepare and request slot reset */
11679 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11680 				"2825 Unknown PCI error state: x%x\n", state);
11681 		lpfc_sli4_prep_dev_for_reset(phba);
11682 		return PCI_ERS_RESULT_NEED_RESET;
11683 	}
11684 }
11685 
11686 /**
11687  * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11688  * @pdev: pointer to PCI device.
11689  *
11690  * This routine is called from the PCI subsystem for error handling to device
11691  * with SLI-4 interface spec. It is called after PCI bus has been reset to
11692  * restart the PCI card from scratch, as if from a cold-boot. During the
11693  * PCI subsystem error recovery, after the driver returns
11694  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11695  * recovery and then call this routine before calling the .resume method to
11696  * recover the device. This function will initialize the HBA device, enable
11697  * the interrupt, but it will just put the HBA to offline state without
11698  * passing any I/O traffic.
11699  *
11700  * Return codes
11701  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
11702  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11703  */
11704 static pci_ers_result_t
11705 lpfc_io_slot_reset_s4(struct pci_dev *pdev)
11706 {
11707 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11708 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11709 	struct lpfc_sli *psli = &phba->sli;
11710 	uint32_t intr_mode;
11711 
11712 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11713 	if (pci_enable_device_mem(pdev)) {
11714 		printk(KERN_ERR "lpfc: Cannot re-enable "
11715 			"PCI device after reset.\n");
11716 		return PCI_ERS_RESULT_DISCONNECT;
11717 	}
11718 
11719 	pci_restore_state(pdev);
11720 
11721 	/*
11722 	 * As the new kernel behavior of pci_restore_state() API call clears
11723 	 * device saved_state flag, need to save the restored state again.
11724 	 */
11725 	pci_save_state(pdev);
11726 
11727 	if (pdev->is_busmaster)
11728 		pci_set_master(pdev);
11729 
11730 	spin_lock_irq(&phba->hbalock);
11731 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11732 	spin_unlock_irq(&phba->hbalock);
11733 
11734 	/* Configure and enable interrupt */
11735 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11736 	if (intr_mode == LPFC_INTR_ERROR) {
11737 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11738 				"2824 Cannot re-enable interrupt after "
11739 				"slot reset.\n");
11740 		return PCI_ERS_RESULT_DISCONNECT;
11741 	} else
11742 		phba->intr_mode = intr_mode;
11743 
11744 	/* Log the current active interrupt mode */
11745 	lpfc_log_intr_mode(phba, phba->intr_mode);
11746 
11747 	return PCI_ERS_RESULT_RECOVERED;
11748 }
11749 
11750 /**
11751  * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
11752  * @pdev: pointer to PCI device
11753  *
11754  * This routine is called from the PCI subsystem for error handling to device
11755  * with SLI-4 interface spec. It is called when kernel error recovery tells
11756  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11757  * error recovery. After this call, traffic can start to flow from this device
11758  * again.
11759  **/
11760 static void
11761 lpfc_io_resume_s4(struct pci_dev *pdev)
11762 {
11763 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11764 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11765 
11766 	/*
11767 	 * In case of slot reset, as function reset is performed through
11768 	 * mailbox command which needs DMA to be enabled, this operation
11769 	 * has to be moved to the io resume phase. Taking device offline
11770 	 * will perform the necessary cleanup.
11771 	 */
11772 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
11773 		/* Perform device reset */
11774 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11775 		lpfc_offline(phba);
11776 		lpfc_sli_brdrestart(phba);
11777 		/* Bring the device back online */
11778 		lpfc_online(phba);
11779 	}
11780 
11781 	/* Clean up Advanced Error Reporting (AER) if needed */
11782 	if (phba->hba_flag & HBA_AER_ENABLED)
11783 		pci_cleanup_aer_uncorrect_error_status(pdev);
11784 }
11785 
11786 /**
11787  * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
11788  * @pdev: pointer to PCI device
11789  * @pid: pointer to PCI device identifier
11790  *
11791  * This routine is to be registered to the kernel's PCI subsystem. When an
11792  * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
11793  * at PCI device-specific information of the device and driver to see if the
11794  * driver state that it can support this kind of device. If the match is
11795  * successful, the driver core invokes this routine. This routine dispatches
11796  * the action to the proper SLI-3 or SLI-4 device probing routine, which will
11797  * do all the initialization that it needs to do to handle the HBA device
11798  * properly.
11799  *
11800  * Return code
11801  * 	0 - driver can claim the device
11802  * 	negative value - driver can not claim the device
11803  **/
11804 static int
11805 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11806 {
11807 	int rc;
11808 	struct lpfc_sli_intf intf;
11809 
11810 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
11811 		return -ENODEV;
11812 
11813 	if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
11814 	    (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
11815 		rc = lpfc_pci_probe_one_s4(pdev, pid);
11816 	else
11817 		rc = lpfc_pci_probe_one_s3(pdev, pid);
11818 
11819 	return rc;
11820 }
11821 
11822 /**
11823  * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11824  * @pdev: pointer to PCI device
11825  *
11826  * This routine is to be registered to the kernel's PCI subsystem. When an
11827  * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11828  * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11829  * remove routine, which will perform all the necessary cleanup for the
11830  * device to be removed from the PCI subsystem properly.
11831  **/
11832 static void
11833 lpfc_pci_remove_one(struct pci_dev *pdev)
11834 {
11835 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11836 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11837 
11838 	switch (phba->pci_dev_grp) {
11839 	case LPFC_PCI_DEV_LP:
11840 		lpfc_pci_remove_one_s3(pdev);
11841 		break;
11842 	case LPFC_PCI_DEV_OC:
11843 		lpfc_pci_remove_one_s4(pdev);
11844 		break;
11845 	default:
11846 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11847 				"1424 Invalid PCI device group: 0x%x\n",
11848 				phba->pci_dev_grp);
11849 		break;
11850 	}
11851 	return;
11852 }
11853 
11854 /**
11855  * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11856  * @pdev: pointer to PCI device
11857  * @msg: power management message
11858  *
11859  * This routine is to be registered to the kernel's PCI subsystem to support
11860  * system Power Management (PM). When PM invokes this method, it dispatches
11861  * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11862  * suspend the device.
11863  *
11864  * Return code
11865  * 	0 - driver suspended the device
11866  * 	Error otherwise
11867  **/
11868 static int
11869 lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11870 {
11871 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11872 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11873 	int rc = -ENODEV;
11874 
11875 	switch (phba->pci_dev_grp) {
11876 	case LPFC_PCI_DEV_LP:
11877 		rc = lpfc_pci_suspend_one_s3(pdev, msg);
11878 		break;
11879 	case LPFC_PCI_DEV_OC:
11880 		rc = lpfc_pci_suspend_one_s4(pdev, msg);
11881 		break;
11882 	default:
11883 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11884 				"1425 Invalid PCI device group: 0x%x\n",
11885 				phba->pci_dev_grp);
11886 		break;
11887 	}
11888 	return rc;
11889 }
11890 
11891 /**
11892  * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11893  * @pdev: pointer to PCI device
11894  *
11895  * This routine is to be registered to the kernel's PCI subsystem to support
11896  * system Power Management (PM). When PM invokes this method, it dispatches
11897  * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11898  * resume the device.
11899  *
11900  * Return code
11901  * 	0 - driver suspended the device
11902  * 	Error otherwise
11903  **/
11904 static int
11905 lpfc_pci_resume_one(struct pci_dev *pdev)
11906 {
11907 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11908 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11909 	int rc = -ENODEV;
11910 
11911 	switch (phba->pci_dev_grp) {
11912 	case LPFC_PCI_DEV_LP:
11913 		rc = lpfc_pci_resume_one_s3(pdev);
11914 		break;
11915 	case LPFC_PCI_DEV_OC:
11916 		rc = lpfc_pci_resume_one_s4(pdev);
11917 		break;
11918 	default:
11919 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11920 				"1426 Invalid PCI device group: 0x%x\n",
11921 				phba->pci_dev_grp);
11922 		break;
11923 	}
11924 	return rc;
11925 }
11926 
11927 /**
11928  * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11929  * @pdev: pointer to PCI device.
11930  * @state: the current PCI connection state.
11931  *
11932  * This routine is registered to the PCI subsystem for error handling. This
11933  * function is called by the PCI subsystem after a PCI bus error affecting
11934  * this device has been detected. When this routine is invoked, it dispatches
11935  * the action to the proper SLI-3 or SLI-4 device error detected handling
11936  * routine, which will perform the proper error detected operation.
11937  *
11938  * Return codes
11939  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11940  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11941  **/
11942 static pci_ers_result_t
11943 lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11944 {
11945 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11946 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11947 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11948 
11949 	switch (phba->pci_dev_grp) {
11950 	case LPFC_PCI_DEV_LP:
11951 		rc = lpfc_io_error_detected_s3(pdev, state);
11952 		break;
11953 	case LPFC_PCI_DEV_OC:
11954 		rc = lpfc_io_error_detected_s4(pdev, state);
11955 		break;
11956 	default:
11957 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11958 				"1427 Invalid PCI device group: 0x%x\n",
11959 				phba->pci_dev_grp);
11960 		break;
11961 	}
11962 	return rc;
11963 }
11964 
11965 /**
11966  * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11967  * @pdev: pointer to PCI device.
11968  *
11969  * This routine is registered to the PCI subsystem for error handling. This
11970  * function is called after PCI bus has been reset to restart the PCI card
11971  * from scratch, as if from a cold-boot. When this routine is invoked, it
11972  * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
11973  * routine, which will perform the proper device reset.
11974  *
11975  * Return codes
11976  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
11977  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11978  **/
11979 static pci_ers_result_t
11980 lpfc_io_slot_reset(struct pci_dev *pdev)
11981 {
11982 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11983 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11984 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11985 
11986 	switch (phba->pci_dev_grp) {
11987 	case LPFC_PCI_DEV_LP:
11988 		rc = lpfc_io_slot_reset_s3(pdev);
11989 		break;
11990 	case LPFC_PCI_DEV_OC:
11991 		rc = lpfc_io_slot_reset_s4(pdev);
11992 		break;
11993 	default:
11994 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11995 				"1428 Invalid PCI device group: 0x%x\n",
11996 				phba->pci_dev_grp);
11997 		break;
11998 	}
11999 	return rc;
12000 }
12001 
12002 /**
12003  * lpfc_io_resume - lpfc method for resuming PCI I/O operation
12004  * @pdev: pointer to PCI device
12005  *
12006  * This routine is registered to the PCI subsystem for error handling. It
12007  * is called when kernel error recovery tells the lpfc driver that it is
12008  * OK to resume normal PCI operation after PCI bus error recovery. When
12009  * this routine is invoked, it dispatches the action to the proper SLI-3
12010  * or SLI-4 device io_resume routine, which will resume the device operation.
12011  **/
12012 static void
12013 lpfc_io_resume(struct pci_dev *pdev)
12014 {
12015 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12016 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12017 
12018 	switch (phba->pci_dev_grp) {
12019 	case LPFC_PCI_DEV_LP:
12020 		lpfc_io_resume_s3(pdev);
12021 		break;
12022 	case LPFC_PCI_DEV_OC:
12023 		lpfc_io_resume_s4(pdev);
12024 		break;
12025 	default:
12026 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12027 				"1429 Invalid PCI device group: 0x%x\n",
12028 				phba->pci_dev_grp);
12029 		break;
12030 	}
12031 	return;
12032 }
12033 
12034 /**
12035  * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
12036  * @phba: pointer to lpfc hba data structure.
12037  *
12038  * This routine checks to see if OAS is supported for this adapter. If
12039  * supported, the configure Flash Optimized Fabric flag is set.  Otherwise,
12040  * the enable oas flag is cleared and the pool created for OAS device data
12041  * is destroyed.
12042  *
12043  **/
12044 void
12045 lpfc_sli4_oas_verify(struct lpfc_hba *phba)
12046 {
12047 
12048 	if (!phba->cfg_EnableXLane)
12049 		return;
12050 
12051 	if (phba->sli4_hba.pc_sli4_params.oas_supported) {
12052 		phba->cfg_fof = 1;
12053 	} else {
12054 		phba->cfg_fof = 0;
12055 		if (phba->device_data_mem_pool)
12056 			mempool_destroy(phba->device_data_mem_pool);
12057 		phba->device_data_mem_pool = NULL;
12058 	}
12059 
12060 	return;
12061 }
12062 
12063 /**
12064  * lpfc_fof_queue_setup - Set up all the fof queues
12065  * @phba: pointer to lpfc hba data structure.
12066  *
12067  * This routine is invoked to set up all the fof queues for the FC HBA
12068  * operation.
12069  *
12070  * Return codes
12071  *      0 - successful
12072  *      -ENOMEM - No available memory
12073  **/
12074 int
12075 lpfc_fof_queue_setup(struct lpfc_hba *phba)
12076 {
12077 	struct lpfc_sli_ring *pring;
12078 	int rc;
12079 
12080 	rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
12081 	if (rc)
12082 		return -ENOMEM;
12083 
12084 	if (phba->cfg_fof) {
12085 
12086 		rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
12087 				    phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
12088 		if (rc)
12089 			goto out_oas_cq;
12090 
12091 		rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
12092 				    phba->sli4_hba.oas_cq, LPFC_FCP);
12093 		if (rc)
12094 			goto out_oas_wq;
12095 
12096 		/* Bind this CQ/WQ to the NVME ring */
12097 		pring = phba->sli4_hba.oas_wq->pring;
12098 		pring->sli.sli4.wqp =
12099 			(void *)phba->sli4_hba.oas_wq;
12100 		phba->sli4_hba.oas_cq->pring = pring;
12101 	}
12102 
12103 	return 0;
12104 
12105 out_oas_wq:
12106 	lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
12107 out_oas_cq:
12108 	lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
12109 	return rc;
12110 
12111 }
12112 
12113 /**
12114  * lpfc_fof_queue_create - Create all the fof queues
12115  * @phba: pointer to lpfc hba data structure.
12116  *
12117  * This routine is invoked to allocate all the fof queues for the FC HBA
12118  * operation. For each SLI4 queue type, the parameters such as queue entry
12119  * count (queue depth) shall be taken from the module parameter. For now,
12120  * we just use some constant number as place holder.
12121  *
12122  * Return codes
12123  *      0 - successful
12124  *      -ENOMEM - No availble memory
12125  *      -EIO - The mailbox failed to complete successfully.
12126  **/
12127 int
12128 lpfc_fof_queue_create(struct lpfc_hba *phba)
12129 {
12130 	struct lpfc_queue *qdesc;
12131 	uint32_t wqesize;
12132 
12133 	/* Create FOF EQ */
12134 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
12135 				      phba->sli4_hba.eq_ecount);
12136 	if (!qdesc)
12137 		goto out_error;
12138 
12139 	phba->sli4_hba.fof_eq = qdesc;
12140 
12141 	if (phba->cfg_fof) {
12142 
12143 		/* Create OAS CQ */
12144 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
12145 						      phba->sli4_hba.cq_ecount);
12146 		if (!qdesc)
12147 			goto out_error;
12148 
12149 		phba->sli4_hba.oas_cq = qdesc;
12150 
12151 		/* Create OAS WQ */
12152 		wqesize = (phba->fcp_embed_io) ?
12153 				LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
12154 		qdesc = lpfc_sli4_queue_alloc(phba, wqesize,
12155 					      phba->sli4_hba.wq_ecount);
12156 
12157 		if (!qdesc)
12158 			goto out_error;
12159 
12160 		phba->sli4_hba.oas_wq = qdesc;
12161 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
12162 
12163 	}
12164 	return 0;
12165 
12166 out_error:
12167 	lpfc_fof_queue_destroy(phba);
12168 	return -ENOMEM;
12169 }
12170 
12171 /**
12172  * lpfc_fof_queue_destroy - Destroy all the fof queues
12173  * @phba: pointer to lpfc hba data structure.
12174  *
12175  * This routine is invoked to release all the SLI4 queues with the FC HBA
12176  * operation.
12177  *
12178  * Return codes
12179  *      0 - successful
12180  **/
12181 int
12182 lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12183 {
12184 	/* Release FOF Event queue */
12185 	if (phba->sli4_hba.fof_eq != NULL) {
12186 		lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12187 		phba->sli4_hba.fof_eq = NULL;
12188 	}
12189 
12190 	/* Release OAS Completion queue */
12191 	if (phba->sli4_hba.oas_cq != NULL) {
12192 		lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12193 		phba->sli4_hba.oas_cq = NULL;
12194 	}
12195 
12196 	/* Release OAS Work queue */
12197 	if (phba->sli4_hba.oas_wq != NULL) {
12198 		lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12199 		phba->sli4_hba.oas_wq = NULL;
12200 	}
12201 	return 0;
12202 }
12203 
12204 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12205 
12206 static const struct pci_error_handlers lpfc_err_handler = {
12207 	.error_detected = lpfc_io_error_detected,
12208 	.slot_reset = lpfc_io_slot_reset,
12209 	.resume = lpfc_io_resume,
12210 };
12211 
12212 static struct pci_driver lpfc_driver = {
12213 	.name		= LPFC_DRIVER_NAME,
12214 	.id_table	= lpfc_id_table,
12215 	.probe		= lpfc_pci_probe_one,
12216 	.remove		= lpfc_pci_remove_one,
12217 	.shutdown	= lpfc_pci_remove_one,
12218 	.suspend        = lpfc_pci_suspend_one,
12219 	.resume		= lpfc_pci_resume_one,
12220 	.err_handler    = &lpfc_err_handler,
12221 };
12222 
12223 static const struct file_operations lpfc_mgmt_fop = {
12224 	.owner = THIS_MODULE,
12225 };
12226 
12227 static struct miscdevice lpfc_mgmt_dev = {
12228 	.minor = MISC_DYNAMIC_MINOR,
12229 	.name = "lpfcmgmt",
12230 	.fops = &lpfc_mgmt_fop,
12231 };
12232 
12233 /**
12234  * lpfc_init - lpfc module initialization routine
12235  *
12236  * This routine is to be invoked when the lpfc module is loaded into the
12237  * kernel. The special kernel macro module_init() is used to indicate the
12238  * role of this routine to the kernel as lpfc module entry point.
12239  *
12240  * Return codes
12241  *   0 - successful
12242  *   -ENOMEM - FC attach transport failed
12243  *   all others - failed
12244  */
12245 static int __init
12246 lpfc_init(void)
12247 {
12248 	int error = 0;
12249 
12250 	printk(LPFC_MODULE_DESC "\n");
12251 	printk(LPFC_COPYRIGHT "\n");
12252 
12253 	error = misc_register(&lpfc_mgmt_dev);
12254 	if (error)
12255 		printk(KERN_ERR "Could not register lpfcmgmt device, "
12256 			"misc_register returned with status %d", error);
12257 
12258 	lpfc_transport_functions.vport_create = lpfc_vport_create;
12259 	lpfc_transport_functions.vport_delete = lpfc_vport_delete;
12260 	lpfc_transport_template =
12261 				fc_attach_transport(&lpfc_transport_functions);
12262 	if (lpfc_transport_template == NULL)
12263 		return -ENOMEM;
12264 	lpfc_vport_transport_template =
12265 		fc_attach_transport(&lpfc_vport_transport_functions);
12266 	if (lpfc_vport_transport_template == NULL) {
12267 		fc_release_transport(lpfc_transport_template);
12268 		return -ENOMEM;
12269 	}
12270 
12271 	/* Initialize in case vector mapping is needed */
12272 	lpfc_used_cpu = NULL;
12273 	lpfc_present_cpu = num_present_cpus();
12274 
12275 	error = pci_register_driver(&lpfc_driver);
12276 	if (error) {
12277 		fc_release_transport(lpfc_transport_template);
12278 		fc_release_transport(lpfc_vport_transport_template);
12279 	}
12280 
12281 	return error;
12282 }
12283 
12284 /**
12285  * lpfc_exit - lpfc module removal routine
12286  *
12287  * This routine is invoked when the lpfc module is removed from the kernel.
12288  * The special kernel macro module_exit() is used to indicate the role of
12289  * this routine to the kernel as lpfc module exit point.
12290  */
12291 static void __exit
12292 lpfc_exit(void)
12293 {
12294 	misc_deregister(&lpfc_mgmt_dev);
12295 	pci_unregister_driver(&lpfc_driver);
12296 	fc_release_transport(lpfc_transport_template);
12297 	fc_release_transport(lpfc_vport_transport_template);
12298 	if (_dump_buf_data) {
12299 		printk(KERN_ERR	"9062 BLKGRD: freeing %lu pages for "
12300 				"_dump_buf_data at 0x%p\n",
12301 				(1L << _dump_buf_data_order), _dump_buf_data);
12302 		free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12303 	}
12304 
12305 	if (_dump_buf_dif) {
12306 		printk(KERN_ERR	"9049 BLKGRD: freeing %lu pages for "
12307 				"_dump_buf_dif at 0x%p\n",
12308 				(1L << _dump_buf_dif_order), _dump_buf_dif);
12309 		free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12310 	}
12311 	kfree(lpfc_used_cpu);
12312 	idr_destroy(&lpfc_hba_index);
12313 }
12314 
12315 module_init(lpfc_init);
12316 module_exit(lpfc_exit);
12317 MODULE_LICENSE("GPL");
12318 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
12319 MODULE_AUTHOR("Broadcom");
12320 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
12321