xref: /linux/drivers/scsi/lpfc/lpfc_init.c (revision fb0255fb2941ef6f21742b2bc146d6b9aef4fedc)
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(struct timer_list *t)
1142 {
1143 	struct lpfc_hba *phba;
1144 	uint32_t tmo_posted;
1145 	unsigned long iflag;
1146 
1147 	phba = from_timer(phba, t, hb_tmofunc);
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(struct timer_list *t)
1176 {
1177 	struct lpfc_hba *phba;
1178 	unsigned long iflag;
1179 
1180 	phba = from_timer(phba, t, rrq_tmr);
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 	timer_setup(&vport->fc_disctmo, lpfc_disc_timeout, 0);
3941 
3942 	timer_setup(&vport->els_tmofunc, lpfc_els_timeout, 0);
3943 
3944 	timer_setup(&vport->delayed_disc_tmo, lpfc_delayed_disc_tmo, 0);
3945 
3946 	error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
3947 	if (error)
3948 		goto out_put_shost;
3949 
3950 	spin_lock_irq(&phba->hbalock);
3951 	list_add_tail(&vport->listentry, &phba->port_list);
3952 	spin_unlock_irq(&phba->hbalock);
3953 	return vport;
3954 
3955 out_put_shost:
3956 	scsi_host_put(shost);
3957 out:
3958 	return NULL;
3959 }
3960 
3961 /**
3962  * destroy_port -  destroy an FC port
3963  * @vport: pointer to an lpfc virtual N_Port data structure.
3964  *
3965  * This routine destroys a FC port from the upper layer protocol. All the
3966  * resources associated with the port are released.
3967  **/
3968 void
3969 destroy_port(struct lpfc_vport *vport)
3970 {
3971 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3972 	struct lpfc_hba  *phba = vport->phba;
3973 
3974 	lpfc_debugfs_terminate(vport);
3975 	fc_remove_host(shost);
3976 	scsi_remove_host(shost);
3977 
3978 	spin_lock_irq(&phba->hbalock);
3979 	list_del_init(&vport->listentry);
3980 	spin_unlock_irq(&phba->hbalock);
3981 
3982 	lpfc_cleanup(vport);
3983 	return;
3984 }
3985 
3986 /**
3987  * lpfc_get_instance - Get a unique integer ID
3988  *
3989  * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3990  * uses the kernel idr facility to perform the task.
3991  *
3992  * Return codes:
3993  *   instance - a unique integer ID allocated as the new instance.
3994  *   -1 - lpfc get instance failed.
3995  **/
3996 int
3997 lpfc_get_instance(void)
3998 {
3999 	int ret;
4000 
4001 	ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
4002 	return ret < 0 ? -1 : ret;
4003 }
4004 
4005 /**
4006  * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
4007  * @shost: pointer to SCSI host data structure.
4008  * @time: elapsed time of the scan in jiffies.
4009  *
4010  * This routine is called by the SCSI layer with a SCSI host to determine
4011  * whether the scan host is finished.
4012  *
4013  * Note: there is no scan_start function as adapter initialization will have
4014  * asynchronously kicked off the link initialization.
4015  *
4016  * Return codes
4017  *   0 - SCSI host scan is not over yet.
4018  *   1 - SCSI host scan is over.
4019  **/
4020 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
4021 {
4022 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4023 	struct lpfc_hba   *phba = vport->phba;
4024 	int stat = 0;
4025 
4026 	spin_lock_irq(shost->host_lock);
4027 
4028 	if (vport->load_flag & FC_UNLOADING) {
4029 		stat = 1;
4030 		goto finished;
4031 	}
4032 	if (time >= msecs_to_jiffies(30 * 1000)) {
4033 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4034 				"0461 Scanning longer than 30 "
4035 				"seconds.  Continuing initialization\n");
4036 		stat = 1;
4037 		goto finished;
4038 	}
4039 	if (time >= msecs_to_jiffies(15 * 1000) &&
4040 	    phba->link_state <= LPFC_LINK_DOWN) {
4041 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4042 				"0465 Link down longer than 15 "
4043 				"seconds.  Continuing initialization\n");
4044 		stat = 1;
4045 		goto finished;
4046 	}
4047 
4048 	if (vport->port_state != LPFC_VPORT_READY)
4049 		goto finished;
4050 	if (vport->num_disc_nodes || vport->fc_prli_sent)
4051 		goto finished;
4052 	if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
4053 		goto finished;
4054 	if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
4055 		goto finished;
4056 
4057 	stat = 1;
4058 
4059 finished:
4060 	spin_unlock_irq(shost->host_lock);
4061 	return stat;
4062 }
4063 
4064 /**
4065  * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
4066  * @shost: pointer to SCSI host data structure.
4067  *
4068  * This routine initializes a given SCSI host attributes on a FC port. The
4069  * SCSI host can be either on top of a physical port or a virtual port.
4070  **/
4071 void lpfc_host_attrib_init(struct Scsi_Host *shost)
4072 {
4073 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4074 	struct lpfc_hba   *phba = vport->phba;
4075 	/*
4076 	 * Set fixed host attributes.  Must done after lpfc_sli_hba_setup().
4077 	 */
4078 
4079 	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4080 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4081 	fc_host_supported_classes(shost) = FC_COS_CLASS3;
4082 
4083 	memset(fc_host_supported_fc4s(shost), 0,
4084 	       sizeof(fc_host_supported_fc4s(shost)));
4085 	fc_host_supported_fc4s(shost)[2] = 1;
4086 	fc_host_supported_fc4s(shost)[7] = 1;
4087 
4088 	lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
4089 				 sizeof fc_host_symbolic_name(shost));
4090 
4091 	fc_host_supported_speeds(shost) = 0;
4092 	if (phba->lmt & LMT_32Gb)
4093 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_32GBIT;
4094 	if (phba->lmt & LMT_16Gb)
4095 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
4096 	if (phba->lmt & LMT_10Gb)
4097 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
4098 	if (phba->lmt & LMT_8Gb)
4099 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
4100 	if (phba->lmt & LMT_4Gb)
4101 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
4102 	if (phba->lmt & LMT_2Gb)
4103 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
4104 	if (phba->lmt & LMT_1Gb)
4105 		fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
4106 
4107 	fc_host_maxframe_size(shost) =
4108 		(((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
4109 		(uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
4110 
4111 	fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
4112 
4113 	/* This value is also unchanging */
4114 	memset(fc_host_active_fc4s(shost), 0,
4115 	       sizeof(fc_host_active_fc4s(shost)));
4116 	fc_host_active_fc4s(shost)[2] = 1;
4117 	fc_host_active_fc4s(shost)[7] = 1;
4118 
4119 	fc_host_max_npiv_vports(shost) = phba->max_vpi;
4120 	spin_lock_irq(shost->host_lock);
4121 	vport->load_flag &= ~FC_LOADING;
4122 	spin_unlock_irq(shost->host_lock);
4123 }
4124 
4125 /**
4126  * lpfc_stop_port_s3 - Stop SLI3 device port
4127  * @phba: pointer to lpfc hba data structure.
4128  *
4129  * This routine is invoked to stop an SLI3 device port, it stops the device
4130  * from generating interrupts and stops the device driver's timers for the
4131  * device.
4132  **/
4133 static void
4134 lpfc_stop_port_s3(struct lpfc_hba *phba)
4135 {
4136 	/* Clear all interrupt enable conditions */
4137 	writel(0, phba->HCregaddr);
4138 	readl(phba->HCregaddr); /* flush */
4139 	/* Clear all pending interrupts */
4140 	writel(0xffffffff, phba->HAregaddr);
4141 	readl(phba->HAregaddr); /* flush */
4142 
4143 	/* Reset some HBA SLI setup states */
4144 	lpfc_stop_hba_timers(phba);
4145 	phba->pport->work_port_events = 0;
4146 }
4147 
4148 /**
4149  * lpfc_stop_port_s4 - Stop SLI4 device port
4150  * @phba: pointer to lpfc hba data structure.
4151  *
4152  * This routine is invoked to stop an SLI4 device port, it stops the device
4153  * from generating interrupts and stops the device driver's timers for the
4154  * device.
4155  **/
4156 static void
4157 lpfc_stop_port_s4(struct lpfc_hba *phba)
4158 {
4159 	/* Reset some HBA SLI4 setup states */
4160 	lpfc_stop_hba_timers(phba);
4161 	phba->pport->work_port_events = 0;
4162 	phba->sli4_hba.intr_enable = 0;
4163 }
4164 
4165 /**
4166  * lpfc_stop_port - Wrapper function for stopping hba port
4167  * @phba: Pointer to HBA context object.
4168  *
4169  * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
4170  * the API jump table function pointer from the lpfc_hba struct.
4171  **/
4172 void
4173 lpfc_stop_port(struct lpfc_hba *phba)
4174 {
4175 	phba->lpfc_stop_port(phba);
4176 }
4177 
4178 /**
4179  * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
4180  * @phba: Pointer to hba for which this call is being executed.
4181  *
4182  * This routine starts the timer waiting for the FCF rediscovery to complete.
4183  **/
4184 void
4185 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
4186 {
4187 	unsigned long fcf_redisc_wait_tmo =
4188 		(jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
4189 	/* Start fcf rediscovery wait period timer */
4190 	mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
4191 	spin_lock_irq(&phba->hbalock);
4192 	/* Allow action to new fcf asynchronous event */
4193 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
4194 	/* Mark the FCF rediscovery pending state */
4195 	phba->fcf.fcf_flag |= FCF_REDISC_PEND;
4196 	spin_unlock_irq(&phba->hbalock);
4197 }
4198 
4199 /**
4200  * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
4201  * @ptr: Map to lpfc_hba data structure pointer.
4202  *
4203  * This routine is invoked when waiting for FCF table rediscover has been
4204  * timed out. If new FCF record(s) has (have) been discovered during the
4205  * wait period, a new FCF event shall be added to the FCOE async event
4206  * list, and then worker thread shall be waked up for processing from the
4207  * worker thread context.
4208  **/
4209 static void
4210 lpfc_sli4_fcf_redisc_wait_tmo(struct timer_list *t)
4211 {
4212 	struct lpfc_hba *phba = from_timer(phba, t, fcf.redisc_wait);
4213 
4214 	/* Don't send FCF rediscovery event if timer cancelled */
4215 	spin_lock_irq(&phba->hbalock);
4216 	if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
4217 		spin_unlock_irq(&phba->hbalock);
4218 		return;
4219 	}
4220 	/* Clear FCF rediscovery timer pending flag */
4221 	phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
4222 	/* FCF rediscovery event to worker thread */
4223 	phba->fcf.fcf_flag |= FCF_REDISC_EVT;
4224 	spin_unlock_irq(&phba->hbalock);
4225 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
4226 			"2776 FCF rediscover quiescent timer expired\n");
4227 	/* wake up worker thread */
4228 	lpfc_worker_wake_up(phba);
4229 }
4230 
4231 /**
4232  * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
4233  * @phba: pointer to lpfc hba data structure.
4234  * @acqe_link: pointer to the async link completion queue entry.
4235  *
4236  * This routine is to parse the SLI4 link-attention link fault code and
4237  * translate it into the base driver's read link attention mailbox command
4238  * status.
4239  *
4240  * Return: Link-attention status in terms of base driver's coding.
4241  **/
4242 static uint16_t
4243 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
4244 			   struct lpfc_acqe_link *acqe_link)
4245 {
4246 	uint16_t latt_fault;
4247 
4248 	switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
4249 	case LPFC_ASYNC_LINK_FAULT_NONE:
4250 	case LPFC_ASYNC_LINK_FAULT_LOCAL:
4251 	case LPFC_ASYNC_LINK_FAULT_REMOTE:
4252 		latt_fault = 0;
4253 		break;
4254 	default:
4255 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4256 				"0398 Invalid link fault code: x%x\n",
4257 				bf_get(lpfc_acqe_link_fault, acqe_link));
4258 		latt_fault = MBXERR_ERROR;
4259 		break;
4260 	}
4261 	return latt_fault;
4262 }
4263 
4264 /**
4265  * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
4266  * @phba: pointer to lpfc hba data structure.
4267  * @acqe_link: pointer to the async link completion queue entry.
4268  *
4269  * This routine is to parse the SLI4 link attention type and translate it
4270  * into the base driver's link attention type coding.
4271  *
4272  * Return: Link attention type in terms of base driver's coding.
4273  **/
4274 static uint8_t
4275 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
4276 			  struct lpfc_acqe_link *acqe_link)
4277 {
4278 	uint8_t att_type;
4279 
4280 	switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
4281 	case LPFC_ASYNC_LINK_STATUS_DOWN:
4282 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
4283 		att_type = LPFC_ATT_LINK_DOWN;
4284 		break;
4285 	case LPFC_ASYNC_LINK_STATUS_UP:
4286 		/* Ignore physical link up events - wait for logical link up */
4287 		att_type = LPFC_ATT_RESERVED;
4288 		break;
4289 	case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
4290 		att_type = LPFC_ATT_LINK_UP;
4291 		break;
4292 	default:
4293 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4294 				"0399 Invalid link attention type: x%x\n",
4295 				bf_get(lpfc_acqe_link_status, acqe_link));
4296 		att_type = LPFC_ATT_RESERVED;
4297 		break;
4298 	}
4299 	return att_type;
4300 }
4301 
4302 /**
4303  * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
4304  * @phba: pointer to lpfc hba data structure.
4305  *
4306  * This routine is to get an SLI3 FC port's link speed in Mbps.
4307  *
4308  * Return: link speed in terms of Mbps.
4309  **/
4310 uint32_t
4311 lpfc_sli_port_speed_get(struct lpfc_hba *phba)
4312 {
4313 	uint32_t link_speed;
4314 
4315 	if (!lpfc_is_link_up(phba))
4316 		return 0;
4317 
4318 	if (phba->sli_rev <= LPFC_SLI_REV3) {
4319 		switch (phba->fc_linkspeed) {
4320 		case LPFC_LINK_SPEED_1GHZ:
4321 			link_speed = 1000;
4322 			break;
4323 		case LPFC_LINK_SPEED_2GHZ:
4324 			link_speed = 2000;
4325 			break;
4326 		case LPFC_LINK_SPEED_4GHZ:
4327 			link_speed = 4000;
4328 			break;
4329 		case LPFC_LINK_SPEED_8GHZ:
4330 			link_speed = 8000;
4331 			break;
4332 		case LPFC_LINK_SPEED_10GHZ:
4333 			link_speed = 10000;
4334 			break;
4335 		case LPFC_LINK_SPEED_16GHZ:
4336 			link_speed = 16000;
4337 			break;
4338 		default:
4339 			link_speed = 0;
4340 		}
4341 	} else {
4342 		if (phba->sli4_hba.link_state.logical_speed)
4343 			link_speed =
4344 			      phba->sli4_hba.link_state.logical_speed;
4345 		else
4346 			link_speed = phba->sli4_hba.link_state.speed;
4347 	}
4348 	return link_speed;
4349 }
4350 
4351 /**
4352  * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
4353  * @phba: pointer to lpfc hba data structure.
4354  * @evt_code: asynchronous event code.
4355  * @speed_code: asynchronous event link speed code.
4356  *
4357  * This routine is to parse the giving SLI4 async event link speed code into
4358  * value of Mbps for the link speed.
4359  *
4360  * Return: link speed in terms of Mbps.
4361  **/
4362 static uint32_t
4363 lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
4364 			   uint8_t speed_code)
4365 {
4366 	uint32_t port_speed;
4367 
4368 	switch (evt_code) {
4369 	case LPFC_TRAILER_CODE_LINK:
4370 		switch (speed_code) {
4371 		case LPFC_ASYNC_LINK_SPEED_ZERO:
4372 			port_speed = 0;
4373 			break;
4374 		case LPFC_ASYNC_LINK_SPEED_10MBPS:
4375 			port_speed = 10;
4376 			break;
4377 		case LPFC_ASYNC_LINK_SPEED_100MBPS:
4378 			port_speed = 100;
4379 			break;
4380 		case LPFC_ASYNC_LINK_SPEED_1GBPS:
4381 			port_speed = 1000;
4382 			break;
4383 		case LPFC_ASYNC_LINK_SPEED_10GBPS:
4384 			port_speed = 10000;
4385 			break;
4386 		case LPFC_ASYNC_LINK_SPEED_20GBPS:
4387 			port_speed = 20000;
4388 			break;
4389 		case LPFC_ASYNC_LINK_SPEED_25GBPS:
4390 			port_speed = 25000;
4391 			break;
4392 		case LPFC_ASYNC_LINK_SPEED_40GBPS:
4393 			port_speed = 40000;
4394 			break;
4395 		default:
4396 			port_speed = 0;
4397 		}
4398 		break;
4399 	case LPFC_TRAILER_CODE_FC:
4400 		switch (speed_code) {
4401 		case LPFC_FC_LA_SPEED_UNKNOWN:
4402 			port_speed = 0;
4403 			break;
4404 		case LPFC_FC_LA_SPEED_1G:
4405 			port_speed = 1000;
4406 			break;
4407 		case LPFC_FC_LA_SPEED_2G:
4408 			port_speed = 2000;
4409 			break;
4410 		case LPFC_FC_LA_SPEED_4G:
4411 			port_speed = 4000;
4412 			break;
4413 		case LPFC_FC_LA_SPEED_8G:
4414 			port_speed = 8000;
4415 			break;
4416 		case LPFC_FC_LA_SPEED_10G:
4417 			port_speed = 10000;
4418 			break;
4419 		case LPFC_FC_LA_SPEED_16G:
4420 			port_speed = 16000;
4421 			break;
4422 		case LPFC_FC_LA_SPEED_32G:
4423 			port_speed = 32000;
4424 			break;
4425 		default:
4426 			port_speed = 0;
4427 		}
4428 		break;
4429 	default:
4430 		port_speed = 0;
4431 	}
4432 	return port_speed;
4433 }
4434 
4435 /**
4436  * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
4437  * @phba: pointer to lpfc hba data structure.
4438  * @acqe_link: pointer to the async link completion queue entry.
4439  *
4440  * This routine is to handle the SLI4 asynchronous FCoE link event.
4441  **/
4442 static void
4443 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
4444 			 struct lpfc_acqe_link *acqe_link)
4445 {
4446 	struct lpfc_dmabuf *mp;
4447 	LPFC_MBOXQ_t *pmb;
4448 	MAILBOX_t *mb;
4449 	struct lpfc_mbx_read_top *la;
4450 	uint8_t att_type;
4451 	int rc;
4452 
4453 	att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
4454 	if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
4455 		return;
4456 	phba->fcoe_eventtag = acqe_link->event_tag;
4457 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4458 	if (!pmb) {
4459 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4460 				"0395 The mboxq allocation failed\n");
4461 		return;
4462 	}
4463 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4464 	if (!mp) {
4465 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4466 				"0396 The lpfc_dmabuf allocation failed\n");
4467 		goto out_free_pmb;
4468 	}
4469 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4470 	if (!mp->virt) {
4471 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4472 				"0397 The mbuf allocation failed\n");
4473 		goto out_free_dmabuf;
4474 	}
4475 
4476 	/* Cleanup any outstanding ELS commands */
4477 	lpfc_els_flush_all_cmd(phba);
4478 
4479 	/* Block ELS IOCBs until we have done process link event */
4480 	phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4481 
4482 	/* Update link event statistics */
4483 	phba->sli.slistat.link_event++;
4484 
4485 	/* Create lpfc_handle_latt mailbox command from link ACQE */
4486 	lpfc_read_topology(phba, pmb, mp);
4487 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4488 	pmb->vport = phba->pport;
4489 
4490 	/* Keep the link status for extra SLI4 state machine reference */
4491 	phba->sli4_hba.link_state.speed =
4492 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
4493 				bf_get(lpfc_acqe_link_speed, acqe_link));
4494 	phba->sli4_hba.link_state.duplex =
4495 				bf_get(lpfc_acqe_link_duplex, acqe_link);
4496 	phba->sli4_hba.link_state.status =
4497 				bf_get(lpfc_acqe_link_status, acqe_link);
4498 	phba->sli4_hba.link_state.type =
4499 				bf_get(lpfc_acqe_link_type, acqe_link);
4500 	phba->sli4_hba.link_state.number =
4501 				bf_get(lpfc_acqe_link_number, acqe_link);
4502 	phba->sli4_hba.link_state.fault =
4503 				bf_get(lpfc_acqe_link_fault, acqe_link);
4504 	phba->sli4_hba.link_state.logical_speed =
4505 			bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
4506 
4507 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4508 			"2900 Async FC/FCoE Link event - Speed:%dGBit "
4509 			"duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
4510 			"Logical speed:%dMbps Fault:%d\n",
4511 			phba->sli4_hba.link_state.speed,
4512 			phba->sli4_hba.link_state.topology,
4513 			phba->sli4_hba.link_state.status,
4514 			phba->sli4_hba.link_state.type,
4515 			phba->sli4_hba.link_state.number,
4516 			phba->sli4_hba.link_state.logical_speed,
4517 			phba->sli4_hba.link_state.fault);
4518 	/*
4519 	 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
4520 	 * topology info. Note: Optional for non FC-AL ports.
4521 	 */
4522 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4523 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4524 		if (rc == MBX_NOT_FINISHED)
4525 			goto out_free_dmabuf;
4526 		return;
4527 	}
4528 	/*
4529 	 * For FCoE Mode: fill in all the topology information we need and call
4530 	 * the READ_TOPOLOGY completion routine to continue without actually
4531 	 * sending the READ_TOPOLOGY mailbox command to the port.
4532 	 */
4533 	/* Parse and translate status field */
4534 	mb = &pmb->u.mb;
4535 	mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
4536 
4537 	/* Parse and translate link attention fields */
4538 	la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
4539 	la->eventTag = acqe_link->event_tag;
4540 	bf_set(lpfc_mbx_read_top_att_type, la, att_type);
4541 	bf_set(lpfc_mbx_read_top_link_spd, la,
4542 	       (bf_get(lpfc_acqe_link_speed, acqe_link)));
4543 
4544 	/* Fake the the following irrelvant fields */
4545 	bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
4546 	bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
4547 	bf_set(lpfc_mbx_read_top_il, la, 0);
4548 	bf_set(lpfc_mbx_read_top_pb, la, 0);
4549 	bf_set(lpfc_mbx_read_top_fa, la, 0);
4550 	bf_set(lpfc_mbx_read_top_mm, la, 0);
4551 
4552 	/* Invoke the lpfc_handle_latt mailbox command callback function */
4553 	lpfc_mbx_cmpl_read_topology(phba, pmb);
4554 
4555 	return;
4556 
4557 out_free_dmabuf:
4558 	kfree(mp);
4559 out_free_pmb:
4560 	mempool_free(pmb, phba->mbox_mem_pool);
4561 }
4562 
4563 /**
4564  * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
4565  * @phba: pointer to lpfc hba data structure.
4566  * @acqe_fc: pointer to the async fc completion queue entry.
4567  *
4568  * This routine is to handle the SLI4 asynchronous FC event. It will simply log
4569  * that the event was received and then issue a read_topology mailbox command so
4570  * that the rest of the driver will treat it the same as SLI3.
4571  **/
4572 static void
4573 lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
4574 {
4575 	struct lpfc_dmabuf *mp;
4576 	LPFC_MBOXQ_t *pmb;
4577 	MAILBOX_t *mb;
4578 	struct lpfc_mbx_read_top *la;
4579 	int rc;
4580 
4581 	if (bf_get(lpfc_trailer_type, acqe_fc) !=
4582 	    LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
4583 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4584 				"2895 Non FC link Event detected.(%d)\n",
4585 				bf_get(lpfc_trailer_type, acqe_fc));
4586 		return;
4587 	}
4588 	/* Keep the link status for extra SLI4 state machine reference */
4589 	phba->sli4_hba.link_state.speed =
4590 			lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
4591 				bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
4592 	phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
4593 	phba->sli4_hba.link_state.topology =
4594 				bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
4595 	phba->sli4_hba.link_state.status =
4596 				bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
4597 	phba->sli4_hba.link_state.type =
4598 				bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
4599 	phba->sli4_hba.link_state.number =
4600 				bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
4601 	phba->sli4_hba.link_state.fault =
4602 				bf_get(lpfc_acqe_link_fault, acqe_fc);
4603 	phba->sli4_hba.link_state.logical_speed =
4604 				bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
4605 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4606 			"2896 Async FC event - Speed:%dGBaud Topology:x%x "
4607 			"LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
4608 			"%dMbps Fault:%d\n",
4609 			phba->sli4_hba.link_state.speed,
4610 			phba->sli4_hba.link_state.topology,
4611 			phba->sli4_hba.link_state.status,
4612 			phba->sli4_hba.link_state.type,
4613 			phba->sli4_hba.link_state.number,
4614 			phba->sli4_hba.link_state.logical_speed,
4615 			phba->sli4_hba.link_state.fault);
4616 	pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4617 	if (!pmb) {
4618 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4619 				"2897 The mboxq allocation failed\n");
4620 		return;
4621 	}
4622 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4623 	if (!mp) {
4624 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4625 				"2898 The lpfc_dmabuf allocation failed\n");
4626 		goto out_free_pmb;
4627 	}
4628 	mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4629 	if (!mp->virt) {
4630 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4631 				"2899 The mbuf allocation failed\n");
4632 		goto out_free_dmabuf;
4633 	}
4634 
4635 	/* Cleanup any outstanding ELS commands */
4636 	lpfc_els_flush_all_cmd(phba);
4637 
4638 	/* Block ELS IOCBs until we have done process link event */
4639 	phba->sli4_hba.els_wq->pring->flag |= LPFC_STOP_IOCB_EVENT;
4640 
4641 	/* Update link event statistics */
4642 	phba->sli.slistat.link_event++;
4643 
4644 	/* Create lpfc_handle_latt mailbox command from link ACQE */
4645 	lpfc_read_topology(phba, pmb, mp);
4646 	pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4647 	pmb->vport = phba->pport;
4648 
4649 	if (phba->sli4_hba.link_state.status != LPFC_FC_LA_TYPE_LINK_UP) {
4650 		phba->link_flag &= ~(LS_MDS_LINK_DOWN | LS_MDS_LOOPBACK);
4651 
4652 		switch (phba->sli4_hba.link_state.status) {
4653 		case LPFC_FC_LA_TYPE_MDS_LINK_DOWN:
4654 			phba->link_flag |= LS_MDS_LINK_DOWN;
4655 			break;
4656 		case LPFC_FC_LA_TYPE_MDS_LOOPBACK:
4657 			phba->link_flag |= LS_MDS_LOOPBACK;
4658 			break;
4659 		default:
4660 			break;
4661 		}
4662 
4663 		/* Parse and translate status field */
4664 		mb = &pmb->u.mb;
4665 		mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba,
4666 							   (void *)acqe_fc);
4667 
4668 		/* Parse and translate link attention fields */
4669 		la = (struct lpfc_mbx_read_top *)&pmb->u.mb.un.varReadTop;
4670 		la->eventTag = acqe_fc->event_tag;
4671 
4672 		if (phba->sli4_hba.link_state.status ==
4673 		    LPFC_FC_LA_TYPE_UNEXP_WWPN) {
4674 			bf_set(lpfc_mbx_read_top_att_type, la,
4675 			       LPFC_FC_LA_TYPE_UNEXP_WWPN);
4676 		} else {
4677 			bf_set(lpfc_mbx_read_top_att_type, la,
4678 			       LPFC_FC_LA_TYPE_LINK_DOWN);
4679 		}
4680 		/* Invoke the mailbox command callback function */
4681 		lpfc_mbx_cmpl_read_topology(phba, pmb);
4682 
4683 		return;
4684 	}
4685 
4686 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4687 	if (rc == MBX_NOT_FINISHED)
4688 		goto out_free_dmabuf;
4689 	return;
4690 
4691 out_free_dmabuf:
4692 	kfree(mp);
4693 out_free_pmb:
4694 	mempool_free(pmb, phba->mbox_mem_pool);
4695 }
4696 
4697 /**
4698  * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4699  * @phba: pointer to lpfc hba data structure.
4700  * @acqe_fc: pointer to the async SLI completion queue entry.
4701  *
4702  * This routine is to handle the SLI4 asynchronous SLI events.
4703  **/
4704 static void
4705 lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4706 {
4707 	char port_name;
4708 	char message[128];
4709 	uint8_t status;
4710 	uint8_t evt_type;
4711 	uint8_t operational = 0;
4712 	struct temp_event temp_event_data;
4713 	struct lpfc_acqe_misconfigured_event *misconfigured;
4714 	struct Scsi_Host  *shost;
4715 
4716 	evt_type = bf_get(lpfc_trailer_type, acqe_sli);
4717 
4718 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4719 			"2901 Async SLI event - Event Data1:x%08x Event Data2:"
4720 			"x%08x SLI Event Type:%d\n",
4721 			acqe_sli->event_data1, acqe_sli->event_data2,
4722 			evt_type);
4723 
4724 	port_name = phba->Port[0];
4725 	if (port_name == 0x00)
4726 		port_name = '?'; /* get port name is empty */
4727 
4728 	switch (evt_type) {
4729 	case LPFC_SLI_EVENT_TYPE_OVER_TEMP:
4730 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4731 		temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
4732 		temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4733 
4734 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4735 				"3190 Over Temperature:%d Celsius- Port Name %c\n",
4736 				acqe_sli->event_data1, port_name);
4737 
4738 		phba->sfp_warning |= LPFC_TRANSGRESSION_HIGH_TEMPERATURE;
4739 		shost = lpfc_shost_from_vport(phba->pport);
4740 		fc_host_post_vendor_event(shost, fc_get_event_number(),
4741 					  sizeof(temp_event_data),
4742 					  (char *)&temp_event_data,
4743 					  SCSI_NL_VID_TYPE_PCI
4744 					  | PCI_VENDOR_ID_EMULEX);
4745 		break;
4746 	case LPFC_SLI_EVENT_TYPE_NORM_TEMP:
4747 		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
4748 		temp_event_data.event_code = LPFC_NORMAL_TEMP;
4749 		temp_event_data.data = (uint32_t)acqe_sli->event_data1;
4750 
4751 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4752 				"3191 Normal Temperature:%d Celsius - Port Name %c\n",
4753 				acqe_sli->event_data1, port_name);
4754 
4755 		shost = lpfc_shost_from_vport(phba->pport);
4756 		fc_host_post_vendor_event(shost, fc_get_event_number(),
4757 					  sizeof(temp_event_data),
4758 					  (char *)&temp_event_data,
4759 					  SCSI_NL_VID_TYPE_PCI
4760 					  | PCI_VENDOR_ID_EMULEX);
4761 		break;
4762 	case LPFC_SLI_EVENT_TYPE_MISCONFIGURED:
4763 		misconfigured = (struct lpfc_acqe_misconfigured_event *)
4764 					&acqe_sli->event_data1;
4765 
4766 		/* fetch the status for this port */
4767 		switch (phba->sli4_hba.lnk_info.lnk_no) {
4768 		case LPFC_LINK_NUMBER_0:
4769 			status = bf_get(lpfc_sli_misconfigured_port0_state,
4770 					&misconfigured->theEvent);
4771 			operational = bf_get(lpfc_sli_misconfigured_port0_op,
4772 					&misconfigured->theEvent);
4773 			break;
4774 		case LPFC_LINK_NUMBER_1:
4775 			status = bf_get(lpfc_sli_misconfigured_port1_state,
4776 					&misconfigured->theEvent);
4777 			operational = bf_get(lpfc_sli_misconfigured_port1_op,
4778 					&misconfigured->theEvent);
4779 			break;
4780 		case LPFC_LINK_NUMBER_2:
4781 			status = bf_get(lpfc_sli_misconfigured_port2_state,
4782 					&misconfigured->theEvent);
4783 			operational = bf_get(lpfc_sli_misconfigured_port2_op,
4784 					&misconfigured->theEvent);
4785 			break;
4786 		case LPFC_LINK_NUMBER_3:
4787 			status = bf_get(lpfc_sli_misconfigured_port3_state,
4788 					&misconfigured->theEvent);
4789 			operational = bf_get(lpfc_sli_misconfigured_port3_op,
4790 					&misconfigured->theEvent);
4791 			break;
4792 		default:
4793 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4794 					"3296 "
4795 					"LPFC_SLI_EVENT_TYPE_MISCONFIGURED "
4796 					"event: Invalid link %d",
4797 					phba->sli4_hba.lnk_info.lnk_no);
4798 			return;
4799 		}
4800 
4801 		/* Skip if optic state unchanged */
4802 		if (phba->sli4_hba.lnk_info.optic_state == status)
4803 			return;
4804 
4805 		switch (status) {
4806 		case LPFC_SLI_EVENT_STATUS_VALID:
4807 			sprintf(message, "Physical Link is functional");
4808 			break;
4809 		case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
4810 			sprintf(message, "Optics faulted/incorrectly "
4811 				"installed/not installed - Reseat optics, "
4812 				"if issue not resolved, replace.");
4813 			break;
4814 		case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4815 			sprintf(message,
4816 				"Optics of two types installed - Remove one "
4817 				"optic or install matching pair of optics.");
4818 			break;
4819 		case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
4820 			sprintf(message, "Incompatible optics - Replace with "
4821 				"compatible optics for card to function.");
4822 			break;
4823 		case LPFC_SLI_EVENT_STATUS_UNQUALIFIED:
4824 			sprintf(message, "Unqualified optics - Replace with "
4825 				"Avago optics for Warranty and Technical "
4826 				"Support - Link is%s operational",
4827 				(operational) ? " not" : "");
4828 			break;
4829 		case LPFC_SLI_EVENT_STATUS_UNCERTIFIED:
4830 			sprintf(message, "Uncertified optics - Replace with "
4831 				"Avago-certified optics to enable link "
4832 				"operation - Link is%s operational",
4833 				(operational) ? " not" : "");
4834 			break;
4835 		default:
4836 			/* firmware is reporting a status we don't know about */
4837 			sprintf(message, "Unknown event status x%02x", status);
4838 			break;
4839 		}
4840 		phba->sli4_hba.lnk_info.optic_state = status;
4841 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4842 				"3176 Port Name %c %s\n", port_name, message);
4843 		break;
4844 	case LPFC_SLI_EVENT_TYPE_REMOTE_DPORT:
4845 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4846 				"3192 Remote DPort Test Initiated - "
4847 				"Event Data1:x%08x Event Data2: x%08x\n",
4848 				acqe_sli->event_data1, acqe_sli->event_data2);
4849 		break;
4850 	default:
4851 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4852 				"3193 Async SLI event - Event Data1:x%08x Event Data2:"
4853 				"x%08x SLI Event Type:%d\n",
4854 				acqe_sli->event_data1, acqe_sli->event_data2,
4855 				evt_type);
4856 		break;
4857 	}
4858 }
4859 
4860 /**
4861  * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4862  * @vport: pointer to vport data structure.
4863  *
4864  * This routine is to perform Clear Virtual Link (CVL) on a vport in
4865  * response to a CVL event.
4866  *
4867  * Return the pointer to the ndlp with the vport if successful, otherwise
4868  * return NULL.
4869  **/
4870 static struct lpfc_nodelist *
4871 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4872 {
4873 	struct lpfc_nodelist *ndlp;
4874 	struct Scsi_Host *shost;
4875 	struct lpfc_hba *phba;
4876 
4877 	if (!vport)
4878 		return NULL;
4879 	phba = vport->phba;
4880 	if (!phba)
4881 		return NULL;
4882 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
4883 	if (!ndlp) {
4884 		/* Cannot find existing Fabric ndlp, so allocate a new one */
4885 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
4886 		if (!ndlp)
4887 			return 0;
4888 		/* Set the node type */
4889 		ndlp->nlp_type |= NLP_FABRIC;
4890 		/* Put ndlp onto node list */
4891 		lpfc_enqueue_node(vport, ndlp);
4892 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
4893 		/* re-setup ndlp without removing from node list */
4894 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4895 		if (!ndlp)
4896 			return 0;
4897 	}
4898 	if ((phba->pport->port_state < LPFC_FLOGI) &&
4899 		(phba->pport->port_state != LPFC_VPORT_FAILED))
4900 		return NULL;
4901 	/* If virtual link is not yet instantiated ignore CVL */
4902 	if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4903 		&& (vport->port_state != LPFC_VPORT_FAILED))
4904 		return NULL;
4905 	shost = lpfc_shost_from_vport(vport);
4906 	if (!shost)
4907 		return NULL;
4908 	lpfc_linkdown_port(vport);
4909 	lpfc_cleanup_pending_mbox(vport);
4910 	spin_lock_irq(shost->host_lock);
4911 	vport->fc_flag |= FC_VPORT_CVL_RCVD;
4912 	spin_unlock_irq(shost->host_lock);
4913 
4914 	return ndlp;
4915 }
4916 
4917 /**
4918  * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4919  * @vport: pointer to lpfc hba data structure.
4920  *
4921  * This routine is to perform Clear Virtual Link (CVL) on all vports in
4922  * response to a FCF dead event.
4923  **/
4924 static void
4925 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4926 {
4927 	struct lpfc_vport **vports;
4928 	int i;
4929 
4930 	vports = lpfc_create_vport_work_array(phba);
4931 	if (vports)
4932 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4933 			lpfc_sli4_perform_vport_cvl(vports[i]);
4934 	lpfc_destroy_vport_work_array(phba, vports);
4935 }
4936 
4937 /**
4938  * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
4939  * @phba: pointer to lpfc hba data structure.
4940  * @acqe_link: pointer to the async fcoe completion queue entry.
4941  *
4942  * This routine is to handle the SLI4 asynchronous fcoe event.
4943  **/
4944 static void
4945 lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
4946 			struct lpfc_acqe_fip *acqe_fip)
4947 {
4948 	uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
4949 	int rc;
4950 	struct lpfc_vport *vport;
4951 	struct lpfc_nodelist *ndlp;
4952 	struct Scsi_Host  *shost;
4953 	int active_vlink_present;
4954 	struct lpfc_vport **vports;
4955 	int i;
4956 
4957 	phba->fc_eventTag = acqe_fip->event_tag;
4958 	phba->fcoe_eventtag = acqe_fip->event_tag;
4959 	switch (event_type) {
4960 	case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4961 	case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4962 		if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
4963 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4964 					LOG_DISCOVERY,
4965 					"2546 New FCF event, evt_tag:x%x, "
4966 					"index:x%x\n",
4967 					acqe_fip->event_tag,
4968 					acqe_fip->index);
4969 		else
4970 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4971 					LOG_DISCOVERY,
4972 					"2788 FCF param modified event, "
4973 					"evt_tag:x%x, index:x%x\n",
4974 					acqe_fip->event_tag,
4975 					acqe_fip->index);
4976 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4977 			/*
4978 			 * During period of FCF discovery, read the FCF
4979 			 * table record indexed by the event to update
4980 			 * FCF roundrobin failover eligible FCF bmask.
4981 			 */
4982 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4983 					LOG_DISCOVERY,
4984 					"2779 Read FCF (x%x) for updating "
4985 					"roundrobin FCF failover bmask\n",
4986 					acqe_fip->index);
4987 			rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
4988 		}
4989 
4990 		/* If the FCF discovery is in progress, do nothing. */
4991 		spin_lock_irq(&phba->hbalock);
4992 		if (phba->hba_flag & FCF_TS_INPROG) {
4993 			spin_unlock_irq(&phba->hbalock);
4994 			break;
4995 		}
4996 		/* If fast FCF failover rescan event is pending, do nothing */
4997 		if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4998 			spin_unlock_irq(&phba->hbalock);
4999 			break;
5000 		}
5001 
5002 		/* If the FCF has been in discovered state, do nothing. */
5003 		if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
5004 			spin_unlock_irq(&phba->hbalock);
5005 			break;
5006 		}
5007 		spin_unlock_irq(&phba->hbalock);
5008 
5009 		/* Otherwise, scan the entire FCF table and re-discover SAN */
5010 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5011 				"2770 Start FCF table scan per async FCF "
5012 				"event, evt_tag:x%x, index:x%x\n",
5013 				acqe_fip->event_tag, acqe_fip->index);
5014 		rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
5015 						     LPFC_FCOE_FCF_GET_FIRST);
5016 		if (rc)
5017 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5018 					"2547 Issue FCF scan read FCF mailbox "
5019 					"command failed (x%x)\n", rc);
5020 		break;
5021 
5022 	case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
5023 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5024 			"2548 FCF Table full count 0x%x tag 0x%x\n",
5025 			bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
5026 			acqe_fip->event_tag);
5027 		break;
5028 
5029 	case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
5030 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5031 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5032 			"2549 FCF (x%x) disconnected from network, "
5033 			"tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
5034 		/*
5035 		 * If we are in the middle of FCF failover process, clear
5036 		 * the corresponding FCF bit in the roundrobin bitmap.
5037 		 */
5038 		spin_lock_irq(&phba->hbalock);
5039 		if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
5040 		    (phba->fcf.current_rec.fcf_indx != acqe_fip->index)) {
5041 			spin_unlock_irq(&phba->hbalock);
5042 			/* Update FLOGI FCF failover eligible FCF bmask */
5043 			lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
5044 			break;
5045 		}
5046 		spin_unlock_irq(&phba->hbalock);
5047 
5048 		/* If the event is not for currently used fcf do nothing */
5049 		if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
5050 			break;
5051 
5052 		/*
5053 		 * Otherwise, request the port to rediscover the entire FCF
5054 		 * table for a fast recovery from case that the current FCF
5055 		 * is no longer valid as we are not in the middle of FCF
5056 		 * failover process already.
5057 		 */
5058 		spin_lock_irq(&phba->hbalock);
5059 		/* Mark the fast failover process in progress */
5060 		phba->fcf.fcf_flag |= FCF_DEAD_DISC;
5061 		spin_unlock_irq(&phba->hbalock);
5062 
5063 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5064 				"2771 Start FCF fast failover process due to "
5065 				"FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
5066 				"\n", acqe_fip->event_tag, acqe_fip->index);
5067 		rc = lpfc_sli4_redisc_fcf_table(phba);
5068 		if (rc) {
5069 			lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5070 					LOG_DISCOVERY,
5071 					"2772 Issue FCF rediscover mabilbox "
5072 					"command failed, fail through to FCF "
5073 					"dead event\n");
5074 			spin_lock_irq(&phba->hbalock);
5075 			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
5076 			spin_unlock_irq(&phba->hbalock);
5077 			/*
5078 			 * Last resort will fail over by treating this
5079 			 * as a link down to FCF registration.
5080 			 */
5081 			lpfc_sli4_fcf_dead_failthrough(phba);
5082 		} else {
5083 			/* Reset FCF roundrobin bmask for new discovery */
5084 			lpfc_sli4_clear_fcf_rr_bmask(phba);
5085 			/*
5086 			 * Handling fast FCF failover to a DEAD FCF event is
5087 			 * considered equalivant to receiving CVL to all vports.
5088 			 */
5089 			lpfc_sli4_perform_all_vport_cvl(phba);
5090 		}
5091 		break;
5092 	case LPFC_FIP_EVENT_TYPE_CVL:
5093 		phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
5094 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5095 			"2718 Clear Virtual Link Received for VPI 0x%x"
5096 			" tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
5097 
5098 		vport = lpfc_find_vport_by_vpid(phba,
5099 						acqe_fip->index);
5100 		ndlp = lpfc_sli4_perform_vport_cvl(vport);
5101 		if (!ndlp)
5102 			break;
5103 		active_vlink_present = 0;
5104 
5105 		vports = lpfc_create_vport_work_array(phba);
5106 		if (vports) {
5107 			for (i = 0; i <= phba->max_vports && vports[i] != NULL;
5108 					i++) {
5109 				if ((!(vports[i]->fc_flag &
5110 					FC_VPORT_CVL_RCVD)) &&
5111 					(vports[i]->port_state > LPFC_FDISC)) {
5112 					active_vlink_present = 1;
5113 					break;
5114 				}
5115 			}
5116 			lpfc_destroy_vport_work_array(phba, vports);
5117 		}
5118 
5119 		/*
5120 		 * Don't re-instantiate if vport is marked for deletion.
5121 		 * If we are here first then vport_delete is going to wait
5122 		 * for discovery to complete.
5123 		 */
5124 		if (!(vport->load_flag & FC_UNLOADING) &&
5125 					active_vlink_present) {
5126 			/*
5127 			 * If there are other active VLinks present,
5128 			 * re-instantiate the Vlink using FDISC.
5129 			 */
5130 			mod_timer(&ndlp->nlp_delayfunc,
5131 				  jiffies + msecs_to_jiffies(1000));
5132 			shost = lpfc_shost_from_vport(vport);
5133 			spin_lock_irq(shost->host_lock);
5134 			ndlp->nlp_flag |= NLP_DELAY_TMO;
5135 			spin_unlock_irq(shost->host_lock);
5136 			ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
5137 			vport->port_state = LPFC_FDISC;
5138 		} else {
5139 			/*
5140 			 * Otherwise, we request port to rediscover
5141 			 * the entire FCF table for a fast recovery
5142 			 * from possible case that the current FCF
5143 			 * is no longer valid if we are not already
5144 			 * in the FCF failover process.
5145 			 */
5146 			spin_lock_irq(&phba->hbalock);
5147 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
5148 				spin_unlock_irq(&phba->hbalock);
5149 				break;
5150 			}
5151 			/* Mark the fast failover process in progress */
5152 			phba->fcf.fcf_flag |= FCF_ACVL_DISC;
5153 			spin_unlock_irq(&phba->hbalock);
5154 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
5155 					LOG_DISCOVERY,
5156 					"2773 Start FCF failover per CVL, "
5157 					"evt_tag:x%x\n", acqe_fip->event_tag);
5158 			rc = lpfc_sli4_redisc_fcf_table(phba);
5159 			if (rc) {
5160 				lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
5161 						LOG_DISCOVERY,
5162 						"2774 Issue FCF rediscover "
5163 						"mabilbox command failed, "
5164 						"through to CVL event\n");
5165 				spin_lock_irq(&phba->hbalock);
5166 				phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
5167 				spin_unlock_irq(&phba->hbalock);
5168 				/*
5169 				 * Last resort will be re-try on the
5170 				 * the current registered FCF entry.
5171 				 */
5172 				lpfc_retry_pport_discovery(phba);
5173 			} else
5174 				/*
5175 				 * Reset FCF roundrobin bmask for new
5176 				 * discovery.
5177 				 */
5178 				lpfc_sli4_clear_fcf_rr_bmask(phba);
5179 		}
5180 		break;
5181 	default:
5182 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5183 			"0288 Unknown FCoE event type 0x%x event tag "
5184 			"0x%x\n", event_type, acqe_fip->event_tag);
5185 		break;
5186 	}
5187 }
5188 
5189 /**
5190  * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
5191  * @phba: pointer to lpfc hba data structure.
5192  * @acqe_link: pointer to the async dcbx completion queue entry.
5193  *
5194  * This routine is to handle the SLI4 asynchronous dcbx event.
5195  **/
5196 static void
5197 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
5198 			 struct lpfc_acqe_dcbx *acqe_dcbx)
5199 {
5200 	phba->fc_eventTag = acqe_dcbx->event_tag;
5201 	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5202 			"0290 The SLI4 DCBX asynchronous event is not "
5203 			"handled yet\n");
5204 }
5205 
5206 /**
5207  * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
5208  * @phba: pointer to lpfc hba data structure.
5209  * @acqe_link: pointer to the async grp5 completion queue entry.
5210  *
5211  * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
5212  * is an asynchronous notified of a logical link speed change.  The Port
5213  * reports the logical link speed in units of 10Mbps.
5214  **/
5215 static void
5216 lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
5217 			 struct lpfc_acqe_grp5 *acqe_grp5)
5218 {
5219 	uint16_t prev_ll_spd;
5220 
5221 	phba->fc_eventTag = acqe_grp5->event_tag;
5222 	phba->fcoe_eventtag = acqe_grp5->event_tag;
5223 	prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
5224 	phba->sli4_hba.link_state.logical_speed =
5225 		(bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
5226 	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5227 			"2789 GRP5 Async Event: Updating logical link speed "
5228 			"from %dMbps to %dMbps\n", prev_ll_spd,
5229 			phba->sli4_hba.link_state.logical_speed);
5230 }
5231 
5232 /**
5233  * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
5234  * @phba: pointer to lpfc hba data structure.
5235  *
5236  * This routine is invoked by the worker thread to process all the pending
5237  * SLI4 asynchronous events.
5238  **/
5239 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
5240 {
5241 	struct lpfc_cq_event *cq_event;
5242 
5243 	/* First, declare the async event has been handled */
5244 	spin_lock_irq(&phba->hbalock);
5245 	phba->hba_flag &= ~ASYNC_EVENT;
5246 	spin_unlock_irq(&phba->hbalock);
5247 	/* Now, handle all the async events */
5248 	while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
5249 		/* Get the first event from the head of the event queue */
5250 		spin_lock_irq(&phba->hbalock);
5251 		list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
5252 				 cq_event, struct lpfc_cq_event, list);
5253 		spin_unlock_irq(&phba->hbalock);
5254 		/* Process the asynchronous event */
5255 		switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
5256 		case LPFC_TRAILER_CODE_LINK:
5257 			lpfc_sli4_async_link_evt(phba,
5258 						 &cq_event->cqe.acqe_link);
5259 			break;
5260 		case LPFC_TRAILER_CODE_FCOE:
5261 			lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
5262 			break;
5263 		case LPFC_TRAILER_CODE_DCBX:
5264 			lpfc_sli4_async_dcbx_evt(phba,
5265 						 &cq_event->cqe.acqe_dcbx);
5266 			break;
5267 		case LPFC_TRAILER_CODE_GRP5:
5268 			lpfc_sli4_async_grp5_evt(phba,
5269 						 &cq_event->cqe.acqe_grp5);
5270 			break;
5271 		case LPFC_TRAILER_CODE_FC:
5272 			lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
5273 			break;
5274 		case LPFC_TRAILER_CODE_SLI:
5275 			lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
5276 			break;
5277 		default:
5278 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5279 					"1804 Invalid asynchrous event code: "
5280 					"x%x\n", bf_get(lpfc_trailer_code,
5281 					&cq_event->cqe.mcqe_cmpl));
5282 			break;
5283 		}
5284 		/* Free the completion event processed to the free pool */
5285 		lpfc_sli4_cq_event_release(phba, cq_event);
5286 	}
5287 }
5288 
5289 /**
5290  * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
5291  * @phba: pointer to lpfc hba data structure.
5292  *
5293  * This routine is invoked by the worker thread to process FCF table
5294  * rediscovery pending completion event.
5295  **/
5296 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
5297 {
5298 	int rc;
5299 
5300 	spin_lock_irq(&phba->hbalock);
5301 	/* Clear FCF rediscovery timeout event */
5302 	phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
5303 	/* Clear driver fast failover FCF record flag */
5304 	phba->fcf.failover_rec.flag = 0;
5305 	/* Set state for FCF fast failover */
5306 	phba->fcf.fcf_flag |= FCF_REDISC_FOV;
5307 	spin_unlock_irq(&phba->hbalock);
5308 
5309 	/* Scan FCF table from the first entry to re-discover SAN */
5310 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
5311 			"2777 Start post-quiescent FCF table scan\n");
5312 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
5313 	if (rc)
5314 		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
5315 				"2747 Issue FCF scan read FCF mailbox "
5316 				"command failed 0x%x\n", rc);
5317 }
5318 
5319 /**
5320  * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
5321  * @phba: pointer to lpfc hba data structure.
5322  * @dev_grp: The HBA PCI-Device group number.
5323  *
5324  * This routine is invoked to set up the per HBA PCI-Device group function
5325  * API jump table entries.
5326  *
5327  * Return: 0 if success, otherwise -ENODEV
5328  **/
5329 int
5330 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5331 {
5332 	int rc;
5333 
5334 	/* Set up lpfc PCI-device group */
5335 	phba->pci_dev_grp = dev_grp;
5336 
5337 	/* The LPFC_PCI_DEV_OC uses SLI4 */
5338 	if (dev_grp == LPFC_PCI_DEV_OC)
5339 		phba->sli_rev = LPFC_SLI_REV4;
5340 
5341 	/* Set up device INIT API function jump table */
5342 	rc = lpfc_init_api_table_setup(phba, dev_grp);
5343 	if (rc)
5344 		return -ENODEV;
5345 	/* Set up SCSI API function jump table */
5346 	rc = lpfc_scsi_api_table_setup(phba, dev_grp);
5347 	if (rc)
5348 		return -ENODEV;
5349 	/* Set up SLI API function jump table */
5350 	rc = lpfc_sli_api_table_setup(phba, dev_grp);
5351 	if (rc)
5352 		return -ENODEV;
5353 	/* Set up MBOX API function jump table */
5354 	rc = lpfc_mbox_api_table_setup(phba, dev_grp);
5355 	if (rc)
5356 		return -ENODEV;
5357 
5358 	return 0;
5359 }
5360 
5361 /**
5362  * lpfc_log_intr_mode - Log the active interrupt mode
5363  * @phba: pointer to lpfc hba data structure.
5364  * @intr_mode: active interrupt mode adopted.
5365  *
5366  * This routine it invoked to log the currently used active interrupt mode
5367  * to the device.
5368  **/
5369 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
5370 {
5371 	switch (intr_mode) {
5372 	case 0:
5373 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5374 				"0470 Enable INTx interrupt mode.\n");
5375 		break;
5376 	case 1:
5377 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5378 				"0481 Enabled MSI interrupt mode.\n");
5379 		break;
5380 	case 2:
5381 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5382 				"0480 Enabled MSI-X interrupt mode.\n");
5383 		break;
5384 	default:
5385 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5386 				"0482 Illegal interrupt mode.\n");
5387 		break;
5388 	}
5389 	return;
5390 }
5391 
5392 /**
5393  * lpfc_enable_pci_dev - Enable a generic PCI device.
5394  * @phba: pointer to lpfc hba data structure.
5395  *
5396  * This routine is invoked to enable the PCI device that is common to all
5397  * PCI devices.
5398  *
5399  * Return codes
5400  * 	0 - successful
5401  * 	other values - error
5402  **/
5403 static int
5404 lpfc_enable_pci_dev(struct lpfc_hba *phba)
5405 {
5406 	struct pci_dev *pdev;
5407 
5408 	/* Obtain PCI device reference */
5409 	if (!phba->pcidev)
5410 		goto out_error;
5411 	else
5412 		pdev = phba->pcidev;
5413 	/* Enable PCI device */
5414 	if (pci_enable_device_mem(pdev))
5415 		goto out_error;
5416 	/* Request PCI resource for the device */
5417 	if (pci_request_mem_regions(pdev, LPFC_DRIVER_NAME))
5418 		goto out_disable_device;
5419 	/* Set up device as PCI master and save state for EEH */
5420 	pci_set_master(pdev);
5421 	pci_try_set_mwi(pdev);
5422 	pci_save_state(pdev);
5423 
5424 	/* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5425 	if (pci_is_pcie(pdev))
5426 		pdev->needs_freset = 1;
5427 
5428 	return 0;
5429 
5430 out_disable_device:
5431 	pci_disable_device(pdev);
5432 out_error:
5433 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5434 			"1401 Failed to enable pci device\n");
5435 	return -ENODEV;
5436 }
5437 
5438 /**
5439  * lpfc_disable_pci_dev - Disable a generic PCI device.
5440  * @phba: pointer to lpfc hba data structure.
5441  *
5442  * This routine is invoked to disable the PCI device that is common to all
5443  * PCI devices.
5444  **/
5445 static void
5446 lpfc_disable_pci_dev(struct lpfc_hba *phba)
5447 {
5448 	struct pci_dev *pdev;
5449 
5450 	/* Obtain PCI device reference */
5451 	if (!phba->pcidev)
5452 		return;
5453 	else
5454 		pdev = phba->pcidev;
5455 	/* Release PCI resource and disable PCI device */
5456 	pci_release_mem_regions(pdev);
5457 	pci_disable_device(pdev);
5458 
5459 	return;
5460 }
5461 
5462 /**
5463  * lpfc_reset_hba - Reset a hba
5464  * @phba: pointer to lpfc hba data structure.
5465  *
5466  * This routine is invoked to reset a hba device. It brings the HBA
5467  * offline, performs a board restart, and then brings the board back
5468  * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
5469  * on outstanding mailbox commands.
5470  **/
5471 void
5472 lpfc_reset_hba(struct lpfc_hba *phba)
5473 {
5474 	/* If resets are disabled then set error state and return. */
5475 	if (!phba->cfg_enable_hba_reset) {
5476 		phba->link_state = LPFC_HBA_ERROR;
5477 		return;
5478 	}
5479 	if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
5480 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5481 	else
5482 		lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
5483 	lpfc_offline(phba);
5484 	lpfc_sli_brdrestart(phba);
5485 	lpfc_online(phba);
5486 	lpfc_unblock_mgmt_io(phba);
5487 }
5488 
5489 /**
5490  * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
5491  * @phba: pointer to lpfc hba data structure.
5492  *
5493  * This function enables the PCI SR-IOV virtual functions to a physical
5494  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5495  * enable the number of virtual functions to the physical function. As
5496  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5497  * API call does not considered as an error condition for most of the device.
5498  **/
5499 uint16_t
5500 lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
5501 {
5502 	struct pci_dev *pdev = phba->pcidev;
5503 	uint16_t nr_virtfn;
5504 	int pos;
5505 
5506 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
5507 	if (pos == 0)
5508 		return 0;
5509 
5510 	pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
5511 	return nr_virtfn;
5512 }
5513 
5514 /**
5515  * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
5516  * @phba: pointer to lpfc hba data structure.
5517  * @nr_vfn: number of virtual functions to be enabled.
5518  *
5519  * This function enables the PCI SR-IOV virtual functions to a physical
5520  * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
5521  * enable the number of virtual functions to the physical function. As
5522  * not all devices support SR-IOV, the return code from the pci_enable_sriov()
5523  * API call does not considered as an error condition for most of the device.
5524  **/
5525 int
5526 lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
5527 {
5528 	struct pci_dev *pdev = phba->pcidev;
5529 	uint16_t max_nr_vfn;
5530 	int rc;
5531 
5532 	max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
5533 	if (nr_vfn > max_nr_vfn) {
5534 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5535 				"3057 Requested vfs (%d) greater than "
5536 				"supported vfs (%d)", nr_vfn, max_nr_vfn);
5537 		return -EINVAL;
5538 	}
5539 
5540 	rc = pci_enable_sriov(pdev, nr_vfn);
5541 	if (rc) {
5542 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5543 				"2806 Failed to enable sriov on this device "
5544 				"with vfn number nr_vf:%d, rc:%d\n",
5545 				nr_vfn, rc);
5546 	} else
5547 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5548 				"2807 Successful enable sriov on this device "
5549 				"with vfn number nr_vf:%d\n", nr_vfn);
5550 	return rc;
5551 }
5552 
5553 /**
5554  * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5555  * @phba: pointer to lpfc hba data structure.
5556  *
5557  * This routine is invoked to set up the driver internal resources before the
5558  * device specific resource setup to support the HBA device it attached to.
5559  *
5560  * Return codes
5561  *	0 - successful
5562  *	other values - error
5563  **/
5564 static int
5565 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5566 {
5567 	struct lpfc_sli *psli = &phba->sli;
5568 
5569 	/*
5570 	 * Driver resources common to all SLI revisions
5571 	 */
5572 	atomic_set(&phba->fast_event_count, 0);
5573 	spin_lock_init(&phba->hbalock);
5574 
5575 	/* Initialize ndlp management spinlock */
5576 	spin_lock_init(&phba->ndlp_lock);
5577 
5578 	INIT_LIST_HEAD(&phba->port_list);
5579 	INIT_LIST_HEAD(&phba->work_list);
5580 	init_waitqueue_head(&phba->wait_4_mlo_m_q);
5581 
5582 	/* Initialize the wait queue head for the kernel thread */
5583 	init_waitqueue_head(&phba->work_waitq);
5584 
5585 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5586 			"1403 Protocols supported %s %s %s\n",
5587 			((phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) ?
5588 				"SCSI" : " "),
5589 			((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) ?
5590 				"NVME" : " "),
5591 			(phba->nvmet_support ? "NVMET" : " "));
5592 
5593 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5594 		/* Initialize the scsi buffer list used by driver for scsi IO */
5595 		spin_lock_init(&phba->scsi_buf_list_get_lock);
5596 		INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5597 		spin_lock_init(&phba->scsi_buf_list_put_lock);
5598 		INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
5599 	}
5600 
5601 	if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
5602 		(phba->nvmet_support == 0)) {
5603 		/* Initialize the NVME buffer list used by driver for NVME IO */
5604 		spin_lock_init(&phba->nvme_buf_list_get_lock);
5605 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_get);
5606 		spin_lock_init(&phba->nvme_buf_list_put_lock);
5607 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
5608 	}
5609 
5610 	/* Initialize the fabric iocb list */
5611 	INIT_LIST_HEAD(&phba->fabric_iocb_list);
5612 
5613 	/* Initialize list to save ELS buffers */
5614 	INIT_LIST_HEAD(&phba->elsbuf);
5615 
5616 	/* Initialize FCF connection rec list */
5617 	INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
5618 
5619 	/* Initialize OAS configuration list */
5620 	spin_lock_init(&phba->devicelock);
5621 	INIT_LIST_HEAD(&phba->luns);
5622 
5623 	/* MBOX heartbeat timer */
5624 	timer_setup(&psli->mbox_tmo, lpfc_mbox_timeout, 0);
5625 	/* Fabric block timer */
5626 	timer_setup(&phba->fabric_block_timer, lpfc_fabric_block_timeout, 0);
5627 	/* EA polling mode timer */
5628 	timer_setup(&phba->eratt_poll, lpfc_poll_eratt, 0);
5629 	/* Heartbeat timer */
5630 	timer_setup(&phba->hb_tmofunc, lpfc_hb_timeout, 0);
5631 
5632 	return 0;
5633 }
5634 
5635 /**
5636  * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev
5637  * @phba: pointer to lpfc hba data structure.
5638  *
5639  * This routine is invoked to set up the driver internal resources specific to
5640  * support the SLI-3 HBA device it attached to.
5641  *
5642  * Return codes
5643  * 0 - successful
5644  * other values - error
5645  **/
5646 static int
5647 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
5648 {
5649 	int rc;
5650 
5651 	/*
5652 	 * Initialize timers used by driver
5653 	 */
5654 
5655 	/* FCP polling mode timer */
5656 	timer_setup(&phba->fcp_poll_timer, lpfc_poll_timeout, 0);
5657 
5658 	/* Host attention work mask setup */
5659 	phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
5660 	phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
5661 
5662 	/* Get all the module params for configuring this host */
5663 	lpfc_get_cfgparam(phba);
5664 	/* Set up phase-1 common device driver resources */
5665 
5666 	rc = lpfc_setup_driver_resource_phase1(phba);
5667 	if (rc)
5668 		return -ENODEV;
5669 
5670 	if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
5671 		phba->menlo_flag |= HBA_MENLO_SUPPORT;
5672 		/* check for menlo minimum sg count */
5673 		if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
5674 			phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
5675 	}
5676 
5677 	if (!phba->sli.sli3_ring)
5678 		phba->sli.sli3_ring = kzalloc(LPFC_SLI3_MAX_RING *
5679 			sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5680 	if (!phba->sli.sli3_ring)
5681 		return -ENOMEM;
5682 
5683 	/*
5684 	 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
5685 	 * used to create the sg_dma_buf_pool must be dynamically calculated.
5686 	 */
5687 
5688 	/* Initialize the host templates the configured values. */
5689 	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5690 	lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5691 	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5692 
5693 	/* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
5694 	if (phba->cfg_enable_bg) {
5695 		/*
5696 		 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5697 		 * the FCP rsp, and a BDE for each. Sice we have no control
5698 		 * over how many protection data segments the SCSI Layer
5699 		 * will hand us (ie: there could be one for every block
5700 		 * in the IO), we just allocate enough BDEs to accomidate
5701 		 * our max amount and we need to limit lpfc_sg_seg_cnt to
5702 		 * minimize the risk of running out.
5703 		 */
5704 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5705 			sizeof(struct fcp_rsp) +
5706 			(LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
5707 
5708 		if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
5709 			phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
5710 
5711 		/* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
5712 		phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
5713 	} else {
5714 		/*
5715 		 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5716 		 * the FCP rsp, a BDE for each, and a BDE for up to
5717 		 * cfg_sg_seg_cnt data segments.
5718 		 */
5719 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5720 			sizeof(struct fcp_rsp) +
5721 			((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
5722 
5723 		/* Total BDEs in BPL for scsi_sg_list */
5724 		phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5725 	}
5726 
5727 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5728 			"9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
5729 			phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5730 			phba->cfg_total_seg_cnt);
5731 
5732 	phba->max_vpi = LPFC_MAX_VPI;
5733 	/* This will be set to correct value after config_port mbox */
5734 	phba->max_vports = 0;
5735 
5736 	/*
5737 	 * Initialize the SLI Layer to run with lpfc HBAs.
5738 	 */
5739 	lpfc_sli_setup(phba);
5740 	lpfc_sli_queue_init(phba);
5741 
5742 	/* Allocate device driver memory */
5743 	if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
5744 		return -ENOMEM;
5745 
5746 	/*
5747 	 * Enable sr-iov virtual functions if supported and configured
5748 	 * through the module parameter.
5749 	 */
5750 	if (phba->cfg_sriov_nr_virtfn > 0) {
5751 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5752 						 phba->cfg_sriov_nr_virtfn);
5753 		if (rc) {
5754 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5755 					"2808 Requested number of SR-IOV "
5756 					"virtual functions (%d) is not "
5757 					"supported\n",
5758 					phba->cfg_sriov_nr_virtfn);
5759 			phba->cfg_sriov_nr_virtfn = 0;
5760 		}
5761 	}
5762 
5763 	return 0;
5764 }
5765 
5766 /**
5767  * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
5768  * @phba: pointer to lpfc hba data structure.
5769  *
5770  * This routine is invoked to unset the driver internal resources set up
5771  * specific for supporting the SLI-3 HBA device it attached to.
5772  **/
5773 static void
5774 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
5775 {
5776 	/* Free device driver memory allocated */
5777 	lpfc_mem_free_all(phba);
5778 
5779 	return;
5780 }
5781 
5782 /**
5783  * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
5784  * @phba: pointer to lpfc hba data structure.
5785  *
5786  * This routine is invoked to set up the driver internal resources specific to
5787  * support the SLI-4 HBA device it attached to.
5788  *
5789  * Return codes
5790  * 	0 - successful
5791  * 	other values - error
5792  **/
5793 static int
5794 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
5795 {
5796 	LPFC_MBOXQ_t *mboxq;
5797 	MAILBOX_t *mb;
5798 	int rc, i, max_buf_size;
5799 	uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
5800 	struct lpfc_mqe *mqe;
5801 	int longs;
5802 	int fof_vectors = 0;
5803 	uint64_t wwn;
5804 
5805 	phba->sli4_hba.num_online_cpu = num_online_cpus();
5806 	phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
5807 	phba->sli4_hba.curr_disp_cpu = 0;
5808 
5809 	/* Get all the module params for configuring this host */
5810 	lpfc_get_cfgparam(phba);
5811 
5812 	/* Set up phase-1 common device driver resources */
5813 	rc = lpfc_setup_driver_resource_phase1(phba);
5814 	if (rc)
5815 		return -ENODEV;
5816 
5817 	/* Before proceed, wait for POST done and device ready */
5818 	rc = lpfc_sli4_post_status_check(phba);
5819 	if (rc)
5820 		return -ENODEV;
5821 
5822 	/*
5823 	 * Initialize timers used by driver
5824 	 */
5825 
5826 	timer_setup(&phba->rrq_tmr, lpfc_rrq_timeout, 0);
5827 
5828 	/* FCF rediscover timer */
5829 	timer_setup(&phba->fcf.redisc_wait, lpfc_sli4_fcf_redisc_wait_tmo, 0);
5830 
5831 	/*
5832 	 * Control structure for handling external multi-buffer mailbox
5833 	 * command pass-through.
5834 	 */
5835 	memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5836 		sizeof(struct lpfc_mbox_ext_buf_ctx));
5837 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5838 
5839 	phba->max_vpi = LPFC_MAX_VPI;
5840 
5841 	/* This will be set to correct value after the read_config mbox */
5842 	phba->max_vports = 0;
5843 
5844 	/* Program the default value of vlan_id and fc_map */
5845 	phba->valid_vlan = 0;
5846 	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5847 	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5848 	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5849 
5850 	/*
5851 	 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5852 	 * we will associate a new ring, for each EQ/CQ/WQ tuple.
5853 	 * The WQ create will allocate the ring.
5854 	 */
5855 
5856 	/*
5857 	 * It doesn't matter what family our adapter is in, we are
5858 	 * limited to 2 Pages, 512 SGEs, for our SGL.
5859 	 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5860 	 */
5861 	max_buf_size = (2 * SLI4_PAGE_SIZE);
5862 	if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5863 		phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
5864 
5865 	/*
5866 	 * Since lpfc_sg_seg_cnt is module param, the sg_dma_buf_size
5867 	 * used to create the sg_dma_buf_pool must be calculated.
5868 	 */
5869 	if (phba->cfg_enable_bg) {
5870 		/*
5871 		 * The scsi_buf for a T10-DIF I/O holds the FCP cmnd,
5872 		 * the FCP rsp, and a SGE. Sice we have no control
5873 		 * over how many protection segments the SCSI Layer
5874 		 * will hand us (ie: there could be one for every block
5875 		 * in the IO), just allocate enough SGEs to accomidate
5876 		 * our max amount and we need to limit lpfc_sg_seg_cnt
5877 		 * to minimize the risk of running out.
5878 		 */
5879 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5880 				sizeof(struct fcp_rsp) + max_buf_size;
5881 
5882 		/* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5883 		phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5884 
5885 		if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5886 			phba->cfg_sg_seg_cnt =
5887 				LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5888 	} else {
5889 		/*
5890 		 * The scsi_buf for a regular I/O holds the FCP cmnd,
5891 		 * the FCP rsp, a SGE for each, and a SGE for up to
5892 		 * cfg_sg_seg_cnt data segments.
5893 		 */
5894 		phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5895 				sizeof(struct fcp_rsp) +
5896 				((phba->cfg_sg_seg_cnt + 2) *
5897 				sizeof(struct sli4_sge));
5898 
5899 		/* Total SGEs for scsi_sg_list */
5900 		phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5901 
5902 		/*
5903 		 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only
5904 		 * need to post 1 page for the SGL.
5905 		 */
5906 	}
5907 
5908 	/* Initialize the host templates with the updated values. */
5909 	lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5910 	lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5911 	lpfc_template_no_hr.sg_tablesize = phba->cfg_sg_seg_cnt;
5912 
5913 	if (phba->cfg_sg_dma_buf_size  <= LPFC_MIN_SG_SLI4_BUF_SZ)
5914 		phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5915 	else
5916 		phba->cfg_sg_dma_buf_size =
5917 			SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5918 
5919 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5920 			"9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5921 			phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5922 			phba->cfg_total_seg_cnt);
5923 
5924 	/* Initialize buffer queue management fields */
5925 	INIT_LIST_HEAD(&phba->hbqs[LPFC_ELS_HBQ].hbq_buffer_list);
5926 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5927 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5928 
5929 	/*
5930 	 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5931 	 */
5932 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
5933 		/* Initialize the Abort scsi buffer list used by driver */
5934 		spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5935 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5936 	}
5937 
5938 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
5939 		/* Initialize the Abort nvme buffer list used by driver */
5940 		spin_lock_init(&phba->sli4_hba.abts_nvme_buf_list_lock);
5941 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
5942 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
5943 		INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_io_wait_list);
5944 
5945 		/* Fast-path XRI aborted CQ Event work queue list */
5946 		INIT_LIST_HEAD(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
5947 	}
5948 
5949 	/* This abort list used by worker thread */
5950 	spin_lock_init(&phba->sli4_hba.sgl_list_lock);
5951 	spin_lock_init(&phba->sli4_hba.nvmet_io_wait_lock);
5952 
5953 	/*
5954 	 * Initialize driver internal slow-path work queues
5955 	 */
5956 
5957 	/* Driver internel slow-path CQ Event pool */
5958 	INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5959 	/* Response IOCB work queue list */
5960 	INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
5961 	/* Asynchronous event CQ Event work queue list */
5962 	INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5963 	/* Fast-path XRI aborted CQ Event work queue list */
5964 	INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5965 	/* Slow-path XRI aborted CQ Event work queue list */
5966 	INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5967 	/* Receive queue CQ Event work queue list */
5968 	INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5969 
5970 	/* Initialize extent block lists. */
5971 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5972 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5973 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5974 	INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5975 
5976 	/* Initialize mboxq lists. If the early init routines fail
5977 	 * these lists need to be correctly initialized.
5978 	 */
5979 	INIT_LIST_HEAD(&phba->sli.mboxq);
5980 	INIT_LIST_HEAD(&phba->sli.mboxq_cmpl);
5981 
5982 	/* initialize optic_state to 0xFF */
5983 	phba->sli4_hba.lnk_info.optic_state = 0xff;
5984 
5985 	/* Allocate device driver memory */
5986 	rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5987 	if (rc)
5988 		return -ENOMEM;
5989 
5990 	/* IF Type 2 ports get initialized now. */
5991 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5992 	    LPFC_SLI_INTF_IF_TYPE_2) {
5993 		rc = lpfc_pci_function_reset(phba);
5994 		if (unlikely(rc)) {
5995 			rc = -ENODEV;
5996 			goto out_free_mem;
5997 		}
5998 		phba->temp_sensor_support = 1;
5999 	}
6000 
6001 	/* Create the bootstrap mailbox command */
6002 	rc = lpfc_create_bootstrap_mbox(phba);
6003 	if (unlikely(rc))
6004 		goto out_free_mem;
6005 
6006 	/* Set up the host's endian order with the device. */
6007 	rc = lpfc_setup_endian_order(phba);
6008 	if (unlikely(rc))
6009 		goto out_free_bsmbx;
6010 
6011 	/* Set up the hba's configuration parameters. */
6012 	rc = lpfc_sli4_read_config(phba);
6013 	if (unlikely(rc))
6014 		goto out_free_bsmbx;
6015 	rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
6016 	if (unlikely(rc))
6017 		goto out_free_bsmbx;
6018 
6019 	/* IF Type 0 ports get initialized now. */
6020 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6021 	    LPFC_SLI_INTF_IF_TYPE_0) {
6022 		rc = lpfc_pci_function_reset(phba);
6023 		if (unlikely(rc))
6024 			goto out_free_bsmbx;
6025 	}
6026 
6027 	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6028 						       GFP_KERNEL);
6029 	if (!mboxq) {
6030 		rc = -ENOMEM;
6031 		goto out_free_bsmbx;
6032 	}
6033 
6034 	/* Check for NVMET being configured */
6035 	phba->nvmet_support = 0;
6036 	if (lpfc_enable_nvmet_cnt) {
6037 
6038 		/* First get WWN of HBA instance */
6039 		lpfc_read_nv(phba, mboxq);
6040 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6041 		if (rc != MBX_SUCCESS) {
6042 			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6043 					"6016 Mailbox failed , mbxCmd x%x "
6044 					"READ_NV, mbxStatus x%x\n",
6045 					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6046 					bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6047 			mempool_free(mboxq, phba->mbox_mem_pool);
6048 			rc = -EIO;
6049 			goto out_free_bsmbx;
6050 		}
6051 		mb = &mboxq->u.mb;
6052 		memcpy(&wwn, (char *)mb->un.varRDnvp.nodename,
6053 		       sizeof(uint64_t));
6054 		wwn = cpu_to_be64(wwn);
6055 		phba->sli4_hba.wwnn.u.name = wwn;
6056 		memcpy(&wwn, (char *)mb->un.varRDnvp.portname,
6057 		       sizeof(uint64_t));
6058 		/* wwn is WWPN of HBA instance */
6059 		wwn = cpu_to_be64(wwn);
6060 		phba->sli4_hba.wwpn.u.name = wwn;
6061 
6062 		/* Check to see if it matches any module parameter */
6063 		for (i = 0; i < lpfc_enable_nvmet_cnt; i++) {
6064 			if (wwn == lpfc_enable_nvmet[i]) {
6065 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
6066 				if (lpfc_nvmet_mem_alloc(phba))
6067 					break;
6068 
6069 				phba->nvmet_support = 1; /* a match */
6070 
6071 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6072 						"6017 NVME Target %016llx\n",
6073 						wwn);
6074 #else
6075 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6076 						"6021 Can't enable NVME Target."
6077 						" NVME_TARGET_FC infrastructure"
6078 						" is not in kernel\n");
6079 #endif
6080 				break;
6081 			}
6082 		}
6083 	}
6084 
6085 	lpfc_nvme_mod_param_dep(phba);
6086 
6087 	/* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
6088 	lpfc_supported_pages(mboxq);
6089 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6090 	if (!rc) {
6091 		mqe = &mboxq->u.mqe;
6092 		memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
6093 		       LPFC_MAX_SUPPORTED_PAGES);
6094 		for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
6095 			switch (pn_page[i]) {
6096 			case LPFC_SLI4_PARAMETERS:
6097 				phba->sli4_hba.pc_sli4_params.supported = 1;
6098 				break;
6099 			default:
6100 				break;
6101 			}
6102 		}
6103 		/* Read the port's SLI4 Parameters capabilities if supported. */
6104 		if (phba->sli4_hba.pc_sli4_params.supported)
6105 			rc = lpfc_pc_sli4_params_get(phba, mboxq);
6106 		if (rc) {
6107 			mempool_free(mboxq, phba->mbox_mem_pool);
6108 			rc = -EIO;
6109 			goto out_free_bsmbx;
6110 		}
6111 	}
6112 
6113 	/*
6114 	 * Get sli4 parameters that override parameters from Port capabilities.
6115 	 * If this call fails, it isn't critical unless the SLI4 parameters come
6116 	 * back in conflict.
6117 	 */
6118 	rc = lpfc_get_sli4_parameters(phba, mboxq);
6119 	if (rc) {
6120 		if (phba->sli4_hba.extents_in_use &&
6121 		    phba->sli4_hba.rpi_hdrs_in_use) {
6122 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6123 				"2999 Unsupported SLI4 Parameters "
6124 				"Extents and RPI headers enabled.\n");
6125 		}
6126 		mempool_free(mboxq, phba->mbox_mem_pool);
6127 		rc = -EIO;
6128 		goto out_free_bsmbx;
6129 	}
6130 
6131 	mempool_free(mboxq, phba->mbox_mem_pool);
6132 
6133 	/* Verify OAS is supported */
6134 	lpfc_sli4_oas_verify(phba);
6135 	if (phba->cfg_fof)
6136 		fof_vectors = 1;
6137 
6138 	/* Verify all the SLI4 queues */
6139 	rc = lpfc_sli4_queue_verify(phba);
6140 	if (rc)
6141 		goto out_free_bsmbx;
6142 
6143 	/* Create driver internal CQE event pool */
6144 	rc = lpfc_sli4_cq_event_pool_create(phba);
6145 	if (rc)
6146 		goto out_free_bsmbx;
6147 
6148 	/* Initialize sgl lists per host */
6149 	lpfc_init_sgl_list(phba);
6150 
6151 	/* Allocate and initialize active sgl array */
6152 	rc = lpfc_init_active_sgl_array(phba);
6153 	if (rc) {
6154 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6155 				"1430 Failed to initialize sgl list.\n");
6156 		goto out_destroy_cq_event_pool;
6157 	}
6158 	rc = lpfc_sli4_init_rpi_hdrs(phba);
6159 	if (rc) {
6160 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6161 				"1432 Failed to initialize rpi headers.\n");
6162 		goto out_free_active_sgl;
6163 	}
6164 
6165 	/* Allocate eligible FCF bmask memory for FCF roundrobin failover */
6166 	longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
6167 	phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
6168 					 GFP_KERNEL);
6169 	if (!phba->fcf.fcf_rr_bmask) {
6170 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6171 				"2759 Failed allocate memory for FCF round "
6172 				"robin failover bmask\n");
6173 		rc = -ENOMEM;
6174 		goto out_remove_rpi_hdrs;
6175 	}
6176 
6177 	phba->sli4_hba.hba_eq_hdl = kcalloc(fof_vectors + phba->io_channel_irqs,
6178 						sizeof(struct lpfc_hba_eq_hdl),
6179 						GFP_KERNEL);
6180 	if (!phba->sli4_hba.hba_eq_hdl) {
6181 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6182 				"2572 Failed allocate memory for "
6183 				"fast-path per-EQ handle array\n");
6184 		rc = -ENOMEM;
6185 		goto out_free_fcf_rr_bmask;
6186 	}
6187 
6188 	phba->sli4_hba.cpu_map = kcalloc(phba->sli4_hba.num_present_cpu,
6189 					sizeof(struct lpfc_vector_map_info),
6190 					GFP_KERNEL);
6191 	if (!phba->sli4_hba.cpu_map) {
6192 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6193 				"3327 Failed allocate memory for msi-x "
6194 				"interrupt vector mapping\n");
6195 		rc = -ENOMEM;
6196 		goto out_free_hba_eq_hdl;
6197 	}
6198 	if (lpfc_used_cpu == NULL) {
6199 		lpfc_used_cpu = kcalloc(lpfc_present_cpu, sizeof(uint16_t),
6200 						GFP_KERNEL);
6201 		if (!lpfc_used_cpu) {
6202 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6203 					"3335 Failed allocate memory for msi-x "
6204 					"interrupt vector mapping\n");
6205 			kfree(phba->sli4_hba.cpu_map);
6206 			rc = -ENOMEM;
6207 			goto out_free_hba_eq_hdl;
6208 		}
6209 		for (i = 0; i < lpfc_present_cpu; i++)
6210 			lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
6211 	}
6212 
6213 	/*
6214 	 * Enable sr-iov virtual functions if supported and configured
6215 	 * through the module parameter.
6216 	 */
6217 	if (phba->cfg_sriov_nr_virtfn > 0) {
6218 		rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
6219 						 phba->cfg_sriov_nr_virtfn);
6220 		if (rc) {
6221 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6222 					"3020 Requested number of SR-IOV "
6223 					"virtual functions (%d) is not "
6224 					"supported\n",
6225 					phba->cfg_sriov_nr_virtfn);
6226 			phba->cfg_sriov_nr_virtfn = 0;
6227 		}
6228 	}
6229 
6230 	return 0;
6231 
6232 out_free_hba_eq_hdl:
6233 	kfree(phba->sli4_hba.hba_eq_hdl);
6234 out_free_fcf_rr_bmask:
6235 	kfree(phba->fcf.fcf_rr_bmask);
6236 out_remove_rpi_hdrs:
6237 	lpfc_sli4_remove_rpi_hdrs(phba);
6238 out_free_active_sgl:
6239 	lpfc_free_active_sgl(phba);
6240 out_destroy_cq_event_pool:
6241 	lpfc_sli4_cq_event_pool_destroy(phba);
6242 out_free_bsmbx:
6243 	lpfc_destroy_bootstrap_mbox(phba);
6244 out_free_mem:
6245 	lpfc_mem_free(phba);
6246 	return rc;
6247 }
6248 
6249 /**
6250  * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
6251  * @phba: pointer to lpfc hba data structure.
6252  *
6253  * This routine is invoked to unset the driver internal resources set up
6254  * specific for supporting the SLI-4 HBA device it attached to.
6255  **/
6256 static void
6257 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
6258 {
6259 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6260 
6261 	/* Free memory allocated for msi-x interrupt vector to CPU mapping */
6262 	kfree(phba->sli4_hba.cpu_map);
6263 	phba->sli4_hba.num_present_cpu = 0;
6264 	phba->sli4_hba.num_online_cpu = 0;
6265 	phba->sli4_hba.curr_disp_cpu = 0;
6266 
6267 	/* Free memory allocated for fast-path work queue handles */
6268 	kfree(phba->sli4_hba.hba_eq_hdl);
6269 
6270 	/* Free the allocated rpi headers. */
6271 	lpfc_sli4_remove_rpi_hdrs(phba);
6272 	lpfc_sli4_remove_rpis(phba);
6273 
6274 	/* Free eligible FCF index bmask */
6275 	kfree(phba->fcf.fcf_rr_bmask);
6276 
6277 	/* Free the ELS sgl list */
6278 	lpfc_free_active_sgl(phba);
6279 	lpfc_free_els_sgl_list(phba);
6280 	lpfc_free_nvmet_sgl_list(phba);
6281 
6282 	/* Free the completion queue EQ event pool */
6283 	lpfc_sli4_cq_event_release_all(phba);
6284 	lpfc_sli4_cq_event_pool_destroy(phba);
6285 
6286 	/* Release resource identifiers. */
6287 	lpfc_sli4_dealloc_resource_identifiers(phba);
6288 
6289 	/* Free the bsmbx region. */
6290 	lpfc_destroy_bootstrap_mbox(phba);
6291 
6292 	/* Free the SLI Layer memory with SLI4 HBAs */
6293 	lpfc_mem_free_all(phba);
6294 
6295 	/* Free the current connect table */
6296 	list_for_each_entry_safe(conn_entry, next_conn_entry,
6297 		&phba->fcf_conn_rec_list, list) {
6298 		list_del_init(&conn_entry->list);
6299 		kfree(conn_entry);
6300 	}
6301 
6302 	return;
6303 }
6304 
6305 /**
6306  * lpfc_init_api_table_setup - Set up init api function jump table
6307  * @phba: The hba struct for which this call is being executed.
6308  * @dev_grp: The HBA PCI-Device group number.
6309  *
6310  * This routine sets up the device INIT interface API function jump table
6311  * in @phba struct.
6312  *
6313  * Returns: 0 - success, -ENODEV - failure.
6314  **/
6315 int
6316 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6317 {
6318 	phba->lpfc_hba_init_link = lpfc_hba_init_link;
6319 	phba->lpfc_hba_down_link = lpfc_hba_down_link;
6320 	phba->lpfc_selective_reset = lpfc_selective_reset;
6321 	switch (dev_grp) {
6322 	case LPFC_PCI_DEV_LP:
6323 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
6324 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
6325 		phba->lpfc_stop_port = lpfc_stop_port_s3;
6326 		break;
6327 	case LPFC_PCI_DEV_OC:
6328 		phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
6329 		phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
6330 		phba->lpfc_stop_port = lpfc_stop_port_s4;
6331 		break;
6332 	default:
6333 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6334 				"1431 Invalid HBA PCI-device group: 0x%x\n",
6335 				dev_grp);
6336 		return -ENODEV;
6337 		break;
6338 	}
6339 	return 0;
6340 }
6341 
6342 /**
6343  * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
6344  * @phba: pointer to lpfc hba data structure.
6345  *
6346  * This routine is invoked to set up the driver internal resources after the
6347  * device specific resource setup to support the HBA device it attached to.
6348  *
6349  * Return codes
6350  * 	0 - successful
6351  * 	other values - error
6352  **/
6353 static int
6354 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
6355 {
6356 	int error;
6357 
6358 	/* Startup the kernel thread for this host adapter. */
6359 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
6360 					  "lpfc_worker_%d", phba->brd_no);
6361 	if (IS_ERR(phba->worker_thread)) {
6362 		error = PTR_ERR(phba->worker_thread);
6363 		return error;
6364 	}
6365 
6366 	return 0;
6367 }
6368 
6369 /**
6370  * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
6371  * @phba: pointer to lpfc hba data structure.
6372  *
6373  * This routine is invoked to unset the driver internal resources set up after
6374  * the device specific resource setup for supporting the HBA device it
6375  * attached to.
6376  **/
6377 static void
6378 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
6379 {
6380 	/* Stop kernel worker thread */
6381 	kthread_stop(phba->worker_thread);
6382 }
6383 
6384 /**
6385  * lpfc_free_iocb_list - Free iocb list.
6386  * @phba: pointer to lpfc hba data structure.
6387  *
6388  * This routine is invoked to free the driver's IOCB list and memory.
6389  **/
6390 void
6391 lpfc_free_iocb_list(struct lpfc_hba *phba)
6392 {
6393 	struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
6394 
6395 	spin_lock_irq(&phba->hbalock);
6396 	list_for_each_entry_safe(iocbq_entry, iocbq_next,
6397 				 &phba->lpfc_iocb_list, list) {
6398 		list_del(&iocbq_entry->list);
6399 		kfree(iocbq_entry);
6400 		phba->total_iocbq_bufs--;
6401 	}
6402 	spin_unlock_irq(&phba->hbalock);
6403 
6404 	return;
6405 }
6406 
6407 /**
6408  * lpfc_init_iocb_list - Allocate and initialize iocb list.
6409  * @phba: pointer to lpfc hba data structure.
6410  *
6411  * This routine is invoked to allocate and initizlize the driver's IOCB
6412  * list and set up the IOCB tag array accordingly.
6413  *
6414  * Return codes
6415  *	0 - successful
6416  *	other values - error
6417  **/
6418 int
6419 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
6420 {
6421 	struct lpfc_iocbq *iocbq_entry = NULL;
6422 	uint16_t iotag;
6423 	int i;
6424 
6425 	/* Initialize and populate the iocb list per host.  */
6426 	INIT_LIST_HEAD(&phba->lpfc_iocb_list);
6427 	for (i = 0; i < iocb_count; i++) {
6428 		iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
6429 		if (iocbq_entry == NULL) {
6430 			printk(KERN_ERR "%s: only allocated %d iocbs of "
6431 				"expected %d count. Unloading driver.\n",
6432 				__func__, i, LPFC_IOCB_LIST_CNT);
6433 			goto out_free_iocbq;
6434 		}
6435 
6436 		iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
6437 		if (iotag == 0) {
6438 			kfree(iocbq_entry);
6439 			printk(KERN_ERR "%s: failed to allocate IOTAG. "
6440 				"Unloading driver.\n", __func__);
6441 			goto out_free_iocbq;
6442 		}
6443 		iocbq_entry->sli4_lxritag = NO_XRI;
6444 		iocbq_entry->sli4_xritag = NO_XRI;
6445 
6446 		spin_lock_irq(&phba->hbalock);
6447 		list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
6448 		phba->total_iocbq_bufs++;
6449 		spin_unlock_irq(&phba->hbalock);
6450 	}
6451 
6452 	return 0;
6453 
6454 out_free_iocbq:
6455 	lpfc_free_iocb_list(phba);
6456 
6457 	return -ENOMEM;
6458 }
6459 
6460 /**
6461  * lpfc_free_sgl_list - Free a given sgl list.
6462  * @phba: pointer to lpfc hba data structure.
6463  * @sglq_list: pointer to the head of sgl list.
6464  *
6465  * This routine is invoked to free a give sgl list and memory.
6466  **/
6467 void
6468 lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
6469 {
6470 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6471 
6472 	list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
6473 		list_del(&sglq_entry->list);
6474 		lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
6475 		kfree(sglq_entry);
6476 	}
6477 }
6478 
6479 /**
6480  * lpfc_free_els_sgl_list - Free els sgl list.
6481  * @phba: pointer to lpfc hba data structure.
6482  *
6483  * This routine is invoked to free the driver's els sgl list and memory.
6484  **/
6485 static void
6486 lpfc_free_els_sgl_list(struct lpfc_hba *phba)
6487 {
6488 	LIST_HEAD(sglq_list);
6489 
6490 	/* Retrieve all els sgls from driver list */
6491 	spin_lock_irq(&phba->hbalock);
6492 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6493 	list_splice_init(&phba->sli4_hba.lpfc_els_sgl_list, &sglq_list);
6494 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6495 	spin_unlock_irq(&phba->hbalock);
6496 
6497 	/* Now free the sgl list */
6498 	lpfc_free_sgl_list(phba, &sglq_list);
6499 }
6500 
6501 /**
6502  * lpfc_free_nvmet_sgl_list - Free nvmet sgl list.
6503  * @phba: pointer to lpfc hba data structure.
6504  *
6505  * This routine is invoked to free the driver's nvmet sgl list and memory.
6506  **/
6507 static void
6508 lpfc_free_nvmet_sgl_list(struct lpfc_hba *phba)
6509 {
6510 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
6511 	LIST_HEAD(sglq_list);
6512 
6513 	/* Retrieve all nvmet sgls from driver list */
6514 	spin_lock_irq(&phba->hbalock);
6515 	spin_lock(&phba->sli4_hba.sgl_list_lock);
6516 	list_splice_init(&phba->sli4_hba.lpfc_nvmet_sgl_list, &sglq_list);
6517 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
6518 	spin_unlock_irq(&phba->hbalock);
6519 
6520 	/* Now free the sgl list */
6521 	list_for_each_entry_safe(sglq_entry, sglq_next, &sglq_list, list) {
6522 		list_del(&sglq_entry->list);
6523 		lpfc_nvmet_buf_free(phba, sglq_entry->virt, sglq_entry->phys);
6524 		kfree(sglq_entry);
6525 	}
6526 
6527 	/* Update the nvmet_xri_cnt to reflect no current sgls.
6528 	 * The next initialization cycle sets the count and allocates
6529 	 * the sgls over again.
6530 	 */
6531 	phba->sli4_hba.nvmet_xri_cnt = 0;
6532 }
6533 
6534 /**
6535  * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
6536  * @phba: pointer to lpfc hba data structure.
6537  *
6538  * This routine is invoked to allocate the driver's active sgl memory.
6539  * This array will hold the sglq_entry's for active IOs.
6540  **/
6541 static int
6542 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
6543 {
6544 	int size;
6545 	size = sizeof(struct lpfc_sglq *);
6546 	size *= phba->sli4_hba.max_cfg_param.max_xri;
6547 
6548 	phba->sli4_hba.lpfc_sglq_active_list =
6549 		kzalloc(size, GFP_KERNEL);
6550 	if (!phba->sli4_hba.lpfc_sglq_active_list)
6551 		return -ENOMEM;
6552 	return 0;
6553 }
6554 
6555 /**
6556  * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
6557  * @phba: pointer to lpfc hba data structure.
6558  *
6559  * This routine is invoked to walk through the array of active sglq entries
6560  * and free all of the resources.
6561  * This is just a place holder for now.
6562  **/
6563 static void
6564 lpfc_free_active_sgl(struct lpfc_hba *phba)
6565 {
6566 	kfree(phba->sli4_hba.lpfc_sglq_active_list);
6567 }
6568 
6569 /**
6570  * lpfc_init_sgl_list - Allocate and initialize sgl list.
6571  * @phba: pointer to lpfc hba data structure.
6572  *
6573  * This routine is invoked to allocate and initizlize the driver's sgl
6574  * list and set up the sgl xritag tag array accordingly.
6575  *
6576  **/
6577 static void
6578 lpfc_init_sgl_list(struct lpfc_hba *phba)
6579 {
6580 	/* Initialize and populate the sglq list per host/VF. */
6581 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_els_sgl_list);
6582 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
6583 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_nvmet_sgl_list);
6584 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
6585 
6586 	/* els xri-sgl book keeping */
6587 	phba->sli4_hba.els_xri_cnt = 0;
6588 
6589 	/* scsi xri-buffer book keeping */
6590 	phba->sli4_hba.scsi_xri_cnt = 0;
6591 
6592 	/* nvme xri-buffer book keeping */
6593 	phba->sli4_hba.nvme_xri_cnt = 0;
6594 }
6595 
6596 /**
6597  * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
6598  * @phba: pointer to lpfc hba data structure.
6599  *
6600  * This routine is invoked to post rpi header templates to the
6601  * port for those SLI4 ports that do not support extents.  This routine
6602  * posts a PAGE_SIZE memory region to the port to hold up to
6603  * PAGE_SIZE modulo 64 rpi context headers.  This is an initialization routine
6604  * and should be called only when interrupts are disabled.
6605  *
6606  * Return codes
6607  * 	0 - successful
6608  *	-ERROR - otherwise.
6609  **/
6610 int
6611 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
6612 {
6613 	int rc = 0;
6614 	struct lpfc_rpi_hdr *rpi_hdr;
6615 
6616 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
6617 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6618 		return rc;
6619 	if (phba->sli4_hba.extents_in_use)
6620 		return -EIO;
6621 
6622 	rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
6623 	if (!rpi_hdr) {
6624 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6625 				"0391 Error during rpi post operation\n");
6626 		lpfc_sli4_remove_rpis(phba);
6627 		rc = -ENODEV;
6628 	}
6629 
6630 	return rc;
6631 }
6632 
6633 /**
6634  * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
6635  * @phba: pointer to lpfc hba data structure.
6636  *
6637  * This routine is invoked to allocate a single 4KB memory region to
6638  * support rpis and stores them in the phba.  This single region
6639  * provides support for up to 64 rpis.  The region is used globally
6640  * by the device.
6641  *
6642  * Returns:
6643  *   A valid rpi hdr on success.
6644  *   A NULL pointer on any failure.
6645  **/
6646 struct lpfc_rpi_hdr *
6647 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
6648 {
6649 	uint16_t rpi_limit, curr_rpi_range;
6650 	struct lpfc_dmabuf *dmabuf;
6651 	struct lpfc_rpi_hdr *rpi_hdr;
6652 
6653 	/*
6654 	 * If the SLI4 port supports extents, posting the rpi header isn't
6655 	 * required.  Set the expected maximum count and let the actual value
6656 	 * get set when extents are fully allocated.
6657 	 */
6658 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6659 		return NULL;
6660 	if (phba->sli4_hba.extents_in_use)
6661 		return NULL;
6662 
6663 	/* The limit on the logical index is just the max_rpi count. */
6664 	rpi_limit = phba->sli4_hba.max_cfg_param.max_rpi;
6665 
6666 	spin_lock_irq(&phba->hbalock);
6667 	/*
6668 	 * Establish the starting RPI in this header block.  The starting
6669 	 * rpi is normalized to a zero base because the physical rpi is
6670 	 * port based.
6671 	 */
6672 	curr_rpi_range = phba->sli4_hba.next_rpi;
6673 	spin_unlock_irq(&phba->hbalock);
6674 
6675 	/* Reached full RPI range */
6676 	if (curr_rpi_range == rpi_limit)
6677 		return NULL;
6678 
6679 	/*
6680 	 * First allocate the protocol header region for the port.  The
6681 	 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
6682 	 */
6683 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6684 	if (!dmabuf)
6685 		return NULL;
6686 
6687 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6688 					   LPFC_HDR_TEMPLATE_SIZE,
6689 					   &dmabuf->phys, GFP_KERNEL);
6690 	if (!dmabuf->virt) {
6691 		rpi_hdr = NULL;
6692 		goto err_free_dmabuf;
6693 	}
6694 
6695 	if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
6696 		rpi_hdr = NULL;
6697 		goto err_free_coherent;
6698 	}
6699 
6700 	/* Save the rpi header data for cleanup later. */
6701 	rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
6702 	if (!rpi_hdr)
6703 		goto err_free_coherent;
6704 
6705 	rpi_hdr->dmabuf = dmabuf;
6706 	rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
6707 	rpi_hdr->page_count = 1;
6708 	spin_lock_irq(&phba->hbalock);
6709 
6710 	/* The rpi_hdr stores the logical index only. */
6711 	rpi_hdr->start_rpi = curr_rpi_range;
6712 	rpi_hdr->next_rpi = phba->sli4_hba.next_rpi + LPFC_RPI_HDR_COUNT;
6713 	list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
6714 
6715 	spin_unlock_irq(&phba->hbalock);
6716 	return rpi_hdr;
6717 
6718  err_free_coherent:
6719 	dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
6720 			  dmabuf->virt, dmabuf->phys);
6721  err_free_dmabuf:
6722 	kfree(dmabuf);
6723 	return NULL;
6724 }
6725 
6726 /**
6727  * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
6728  * @phba: pointer to lpfc hba data structure.
6729  *
6730  * This routine is invoked to remove all memory resources allocated
6731  * to support rpis for SLI4 ports not supporting extents. This routine
6732  * presumes the caller has released all rpis consumed by fabric or port
6733  * logins and is prepared to have the header pages removed.
6734  **/
6735 void
6736 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
6737 {
6738 	struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
6739 
6740 	if (!phba->sli4_hba.rpi_hdrs_in_use)
6741 		goto exit;
6742 
6743 	list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
6744 				 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6745 		list_del(&rpi_hdr->list);
6746 		dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
6747 				  rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
6748 		kfree(rpi_hdr->dmabuf);
6749 		kfree(rpi_hdr);
6750 	}
6751  exit:
6752 	/* There are no rpis available to the port now. */
6753 	phba->sli4_hba.next_rpi = 0;
6754 }
6755 
6756 /**
6757  * lpfc_hba_alloc - Allocate driver hba data structure for a device.
6758  * @pdev: pointer to pci device data structure.
6759  *
6760  * This routine is invoked to allocate the driver hba data structure for an
6761  * HBA device. If the allocation is successful, the phba reference to the
6762  * PCI device data structure is set.
6763  *
6764  * Return codes
6765  *      pointer to @phba - successful
6766  *      NULL - error
6767  **/
6768 static struct lpfc_hba *
6769 lpfc_hba_alloc(struct pci_dev *pdev)
6770 {
6771 	struct lpfc_hba *phba;
6772 
6773 	/* Allocate memory for HBA structure */
6774 	phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
6775 	if (!phba) {
6776 		dev_err(&pdev->dev, "failed to allocate hba struct\n");
6777 		return NULL;
6778 	}
6779 
6780 	/* Set reference to PCI device in HBA structure */
6781 	phba->pcidev = pdev;
6782 
6783 	/* Assign an unused board number */
6784 	phba->brd_no = lpfc_get_instance();
6785 	if (phba->brd_no < 0) {
6786 		kfree(phba);
6787 		return NULL;
6788 	}
6789 	phba->eratt_poll_interval = LPFC_ERATT_POLL_INTERVAL;
6790 
6791 	spin_lock_init(&phba->ct_ev_lock);
6792 	INIT_LIST_HEAD(&phba->ct_ev_waiters);
6793 
6794 	return phba;
6795 }
6796 
6797 /**
6798  * lpfc_hba_free - Free driver hba data structure with a device.
6799  * @phba: pointer to lpfc hba data structure.
6800  *
6801  * This routine is invoked to free the driver hba data structure with an
6802  * HBA device.
6803  **/
6804 static void
6805 lpfc_hba_free(struct lpfc_hba *phba)
6806 {
6807 	/* Release the driver assigned board number */
6808 	idr_remove(&lpfc_hba_index, phba->brd_no);
6809 
6810 	/* Free memory allocated with sli3 rings */
6811 	kfree(phba->sli.sli3_ring);
6812 	phba->sli.sli3_ring = NULL;
6813 
6814 	kfree(phba);
6815 	return;
6816 }
6817 
6818 /**
6819  * lpfc_create_shost - Create hba physical port with associated scsi host.
6820  * @phba: pointer to lpfc hba data structure.
6821  *
6822  * This routine is invoked to create HBA physical port and associate a SCSI
6823  * host with it.
6824  *
6825  * Return codes
6826  *      0 - successful
6827  *      other values - error
6828  **/
6829 static int
6830 lpfc_create_shost(struct lpfc_hba *phba)
6831 {
6832 	struct lpfc_vport *vport;
6833 	struct Scsi_Host  *shost;
6834 
6835 	/* Initialize HBA FC structure */
6836 	phba->fc_edtov = FF_DEF_EDTOV;
6837 	phba->fc_ratov = FF_DEF_RATOV;
6838 	phba->fc_altov = FF_DEF_ALTOV;
6839 	phba->fc_arbtov = FF_DEF_ARBTOV;
6840 
6841 	atomic_set(&phba->sdev_cnt, 0);
6842 	atomic_set(&phba->fc4ScsiInputRequests, 0);
6843 	atomic_set(&phba->fc4ScsiOutputRequests, 0);
6844 	atomic_set(&phba->fc4ScsiControlRequests, 0);
6845 	atomic_set(&phba->fc4ScsiIoCmpls, 0);
6846 	atomic_set(&phba->fc4NvmeInputRequests, 0);
6847 	atomic_set(&phba->fc4NvmeOutputRequests, 0);
6848 	atomic_set(&phba->fc4NvmeControlRequests, 0);
6849 	atomic_set(&phba->fc4NvmeIoCmpls, 0);
6850 	atomic_set(&phba->fc4NvmeLsRequests, 0);
6851 	atomic_set(&phba->fc4NvmeLsCmpls, 0);
6852 	vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
6853 	if (!vport)
6854 		return -ENODEV;
6855 
6856 	shost = lpfc_shost_from_vport(vport);
6857 	phba->pport = vport;
6858 
6859 	if (phba->nvmet_support) {
6860 		/* Only 1 vport (pport) will support NVME target */
6861 		if (phba->txrdy_payload_pool == NULL) {
6862 			phba->txrdy_payload_pool = dma_pool_create(
6863 				"txrdy_pool", &phba->pcidev->dev,
6864 				TXRDY_PAYLOAD_LEN, 16, 0);
6865 			if (phba->txrdy_payload_pool) {
6866 				phba->targetport = NULL;
6867 				phba->cfg_enable_fc4_type = LPFC_ENABLE_NVME;
6868 				lpfc_printf_log(phba, KERN_INFO,
6869 						LOG_INIT | LOG_NVME_DISC,
6870 						"6076 NVME Target Found\n");
6871 			}
6872 		}
6873 	}
6874 
6875 	lpfc_debugfs_initialize(vport);
6876 	/* Put reference to SCSI host to driver's device private data */
6877 	pci_set_drvdata(phba->pcidev, shost);
6878 
6879 	/*
6880 	 * At this point we are fully registered with PSA. In addition,
6881 	 * any initial discovery should be completed.
6882 	 */
6883 	vport->load_flag |= FC_ALLOW_FDMI;
6884 	if (phba->cfg_enable_SmartSAN ||
6885 	    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
6886 
6887 		/* Setup appropriate attribute masks */
6888 		vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
6889 		if (phba->cfg_enable_SmartSAN)
6890 			vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
6891 		else
6892 			vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
6893 	}
6894 	return 0;
6895 }
6896 
6897 /**
6898  * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6899  * @phba: pointer to lpfc hba data structure.
6900  *
6901  * This routine is invoked to destroy HBA physical port and the associated
6902  * SCSI host.
6903  **/
6904 static void
6905 lpfc_destroy_shost(struct lpfc_hba *phba)
6906 {
6907 	struct lpfc_vport *vport = phba->pport;
6908 
6909 	/* Destroy physical port that associated with the SCSI host */
6910 	destroy_port(vport);
6911 
6912 	return;
6913 }
6914 
6915 /**
6916  * lpfc_setup_bg - Setup Block guard structures and debug areas.
6917  * @phba: pointer to lpfc hba data structure.
6918  * @shost: the shost to be used to detect Block guard settings.
6919  *
6920  * This routine sets up the local Block guard protocol settings for @shost.
6921  * This routine also allocates memory for debugging bg buffers.
6922  **/
6923 static void
6924 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6925 {
6926 	uint32_t old_mask;
6927 	uint32_t old_guard;
6928 
6929 	int pagecnt = 10;
6930 	if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6931 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6932 				"1478 Registering BlockGuard with the "
6933 				"SCSI layer\n");
6934 
6935 		old_mask = phba->cfg_prot_mask;
6936 		old_guard = phba->cfg_prot_guard;
6937 
6938 		/* Only allow supported values */
6939 		phba->cfg_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6940 			SHOST_DIX_TYPE0_PROTECTION |
6941 			SHOST_DIX_TYPE1_PROTECTION);
6942 		phba->cfg_prot_guard &= (SHOST_DIX_GUARD_IP |
6943 					 SHOST_DIX_GUARD_CRC);
6944 
6945 		/* DIF Type 1 protection for profiles AST1/C1 is end to end */
6946 		if (phba->cfg_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6947 			phba->cfg_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6948 
6949 		if (phba->cfg_prot_mask && phba->cfg_prot_guard) {
6950 			if ((old_mask != phba->cfg_prot_mask) ||
6951 				(old_guard != phba->cfg_prot_guard))
6952 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6953 					"1475 Registering BlockGuard with the "
6954 					"SCSI layer: mask %d  guard %d\n",
6955 					phba->cfg_prot_mask,
6956 					phba->cfg_prot_guard);
6957 
6958 			scsi_host_set_prot(shost, phba->cfg_prot_mask);
6959 			scsi_host_set_guard(shost, phba->cfg_prot_guard);
6960 		} else
6961 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6962 				"1479 Not Registering BlockGuard with the SCSI "
6963 				"layer, Bad protection parameters: %d %d\n",
6964 				old_mask, old_guard);
6965 	}
6966 
6967 	if (!_dump_buf_data) {
6968 		while (pagecnt) {
6969 			spin_lock_init(&_dump_buf_lock);
6970 			_dump_buf_data =
6971 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
6972 			if (_dump_buf_data) {
6973 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6974 					"9043 BLKGRD: allocated %d pages for "
6975 				       "_dump_buf_data at 0x%p\n",
6976 				       (1 << pagecnt), _dump_buf_data);
6977 				_dump_buf_data_order = pagecnt;
6978 				memset(_dump_buf_data, 0,
6979 				       ((1 << PAGE_SHIFT) << pagecnt));
6980 				break;
6981 			} else
6982 				--pagecnt;
6983 		}
6984 		if (!_dump_buf_data_order)
6985 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6986 				"9044 BLKGRD: ERROR unable to allocate "
6987 			       "memory for hexdump\n");
6988 	} else
6989 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6990 			"9045 BLKGRD: already allocated _dump_buf_data=0x%p"
6991 		       "\n", _dump_buf_data);
6992 	if (!_dump_buf_dif) {
6993 		while (pagecnt) {
6994 			_dump_buf_dif =
6995 				(char *) __get_free_pages(GFP_KERNEL, pagecnt);
6996 			if (_dump_buf_dif) {
6997 				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6998 					"9046 BLKGRD: allocated %d pages for "
6999 				       "_dump_buf_dif at 0x%p\n",
7000 				       (1 << pagecnt), _dump_buf_dif);
7001 				_dump_buf_dif_order = pagecnt;
7002 				memset(_dump_buf_dif, 0,
7003 				       ((1 << PAGE_SHIFT) << pagecnt));
7004 				break;
7005 			} else
7006 				--pagecnt;
7007 		}
7008 		if (!_dump_buf_dif_order)
7009 			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7010 			"9047 BLKGRD: ERROR unable to allocate "
7011 			       "memory for hexdump\n");
7012 	} else
7013 		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7014 			"9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
7015 		       _dump_buf_dif);
7016 }
7017 
7018 /**
7019  * lpfc_post_init_setup - Perform necessary device post initialization setup.
7020  * @phba: pointer to lpfc hba data structure.
7021  *
7022  * This routine is invoked to perform all the necessary post initialization
7023  * setup for the device.
7024  **/
7025 static void
7026 lpfc_post_init_setup(struct lpfc_hba *phba)
7027 {
7028 	struct Scsi_Host  *shost;
7029 	struct lpfc_adapter_event_header adapter_event;
7030 
7031 	/* Get the default values for Model Name and Description */
7032 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
7033 
7034 	/*
7035 	 * hba setup may have changed the hba_queue_depth so we need to
7036 	 * adjust the value of can_queue.
7037 	 */
7038 	shost = pci_get_drvdata(phba->pcidev);
7039 	shost->can_queue = phba->cfg_hba_queue_depth - 10;
7040 	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
7041 		lpfc_setup_bg(phba, shost);
7042 
7043 	lpfc_host_attrib_init(shost);
7044 
7045 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7046 		spin_lock_irq(shost->host_lock);
7047 		lpfc_poll_start_timer(phba);
7048 		spin_unlock_irq(shost->host_lock);
7049 	}
7050 
7051 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7052 			"0428 Perform SCSI scan\n");
7053 	/* Send board arrival event to upper layer */
7054 	adapter_event.event_type = FC_REG_ADAPTER_EVENT;
7055 	adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
7056 	fc_host_post_vendor_event(shost, fc_get_event_number(),
7057 				  sizeof(adapter_event),
7058 				  (char *) &adapter_event,
7059 				  LPFC_NL_VENDOR_ID);
7060 	return;
7061 }
7062 
7063 /**
7064  * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
7065  * @phba: pointer to lpfc hba data structure.
7066  *
7067  * This routine is invoked to set up the PCI device memory space for device
7068  * with SLI-3 interface spec.
7069  *
7070  * Return codes
7071  * 	0 - successful
7072  * 	other values - error
7073  **/
7074 static int
7075 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
7076 {
7077 	struct pci_dev *pdev;
7078 	unsigned long bar0map_len, bar2map_len;
7079 	int i, hbq_count;
7080 	void *ptr;
7081 	int error = -ENODEV;
7082 
7083 	/* Obtain PCI device reference */
7084 	if (!phba->pcidev)
7085 		return error;
7086 	else
7087 		pdev = phba->pcidev;
7088 
7089 	/* Set the device DMA mask size */
7090 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7091 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7092 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7093 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
7094 			return error;
7095 		}
7096 	}
7097 
7098 	/* Get the bus address of Bar0 and Bar2 and the number of bytes
7099 	 * required by each mapping.
7100 	 */
7101 	phba->pci_bar0_map = pci_resource_start(pdev, 0);
7102 	bar0map_len = pci_resource_len(pdev, 0);
7103 
7104 	phba->pci_bar2_map = pci_resource_start(pdev, 2);
7105 	bar2map_len = pci_resource_len(pdev, 2);
7106 
7107 	/* Map HBA SLIM to a kernel virtual address. */
7108 	phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
7109 	if (!phba->slim_memmap_p) {
7110 		dev_printk(KERN_ERR, &pdev->dev,
7111 			   "ioremap failed for SLIM memory.\n");
7112 		goto out;
7113 	}
7114 
7115 	/* Map HBA Control Registers to a kernel virtual address. */
7116 	phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
7117 	if (!phba->ctrl_regs_memmap_p) {
7118 		dev_printk(KERN_ERR, &pdev->dev,
7119 			   "ioremap failed for HBA control registers.\n");
7120 		goto out_iounmap_slim;
7121 	}
7122 
7123 	/* Allocate memory for SLI-2 structures */
7124 	phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7125 						&phba->slim2p.phys, GFP_KERNEL);
7126 	if (!phba->slim2p.virt)
7127 		goto out_iounmap;
7128 
7129 	phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
7130 	phba->mbox_ext = (phba->slim2p.virt +
7131 		offsetof(struct lpfc_sli2_slim, mbx_ext_words));
7132 	phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
7133 	phba->IOCBs = (phba->slim2p.virt +
7134 		       offsetof(struct lpfc_sli2_slim, IOCBs));
7135 
7136 	phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
7137 						 lpfc_sli_hbq_size(),
7138 						 &phba->hbqslimp.phys,
7139 						 GFP_KERNEL);
7140 	if (!phba->hbqslimp.virt)
7141 		goto out_free_slim;
7142 
7143 	hbq_count = lpfc_sli_hbq_count();
7144 	ptr = phba->hbqslimp.virt;
7145 	for (i = 0; i < hbq_count; ++i) {
7146 		phba->hbqs[i].hbq_virt = ptr;
7147 		INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
7148 		ptr += (lpfc_hbq_defs[i]->entry_count *
7149 			sizeof(struct lpfc_hbq_entry));
7150 	}
7151 	phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
7152 	phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
7153 
7154 	memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
7155 
7156 	phba->MBslimaddr = phba->slim_memmap_p;
7157 	phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
7158 	phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
7159 	phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
7160 	phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
7161 
7162 	return 0;
7163 
7164 out_free_slim:
7165 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7166 			  phba->slim2p.virt, phba->slim2p.phys);
7167 out_iounmap:
7168 	iounmap(phba->ctrl_regs_memmap_p);
7169 out_iounmap_slim:
7170 	iounmap(phba->slim_memmap_p);
7171 out:
7172 	return error;
7173 }
7174 
7175 /**
7176  * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
7177  * @phba: pointer to lpfc hba data structure.
7178  *
7179  * This routine is invoked to unset the PCI device memory space for device
7180  * with SLI-3 interface spec.
7181  **/
7182 static void
7183 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
7184 {
7185 	struct pci_dev *pdev;
7186 
7187 	/* Obtain PCI device reference */
7188 	if (!phba->pcidev)
7189 		return;
7190 	else
7191 		pdev = phba->pcidev;
7192 
7193 	/* Free coherent DMA memory allocated */
7194 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7195 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
7196 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
7197 			  phba->slim2p.virt, phba->slim2p.phys);
7198 
7199 	/* I/O memory unmap */
7200 	iounmap(phba->ctrl_regs_memmap_p);
7201 	iounmap(phba->slim_memmap_p);
7202 
7203 	return;
7204 }
7205 
7206 /**
7207  * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
7208  * @phba: pointer to lpfc hba data structure.
7209  *
7210  * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
7211  * done and check status.
7212  *
7213  * Return 0 if successful, otherwise -ENODEV.
7214  **/
7215 int
7216 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
7217 {
7218 	struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
7219 	struct lpfc_register reg_data;
7220 	int i, port_error = 0;
7221 	uint32_t if_type;
7222 
7223 	memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
7224 	memset(&reg_data, 0, sizeof(reg_data));
7225 	if (!phba->sli4_hba.PSMPHRregaddr)
7226 		return -ENODEV;
7227 
7228 	/* Wait up to 30 seconds for the SLI Port POST done and ready */
7229 	for (i = 0; i < 3000; i++) {
7230 		if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
7231 			&portsmphr_reg.word0) ||
7232 			(bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
7233 			/* Port has a fatal POST error, break out */
7234 			port_error = -ENODEV;
7235 			break;
7236 		}
7237 		if (LPFC_POST_STAGE_PORT_READY ==
7238 		    bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
7239 			break;
7240 		msleep(10);
7241 	}
7242 
7243 	/*
7244 	 * If there was a port error during POST, then don't proceed with
7245 	 * other register reads as the data may not be valid.  Just exit.
7246 	 */
7247 	if (port_error) {
7248 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7249 			"1408 Port Failed POST - portsmphr=0x%x, "
7250 			"perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
7251 			"scr2=x%x, hscratch=x%x, pstatus=x%x\n",
7252 			portsmphr_reg.word0,
7253 			bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
7254 			bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
7255 			bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
7256 			bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
7257 			bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
7258 			bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
7259 			bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
7260 			bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
7261 	} else {
7262 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7263 				"2534 Device Info: SLIFamily=0x%x, "
7264 				"SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
7265 				"SLIHint_2=0x%x, FT=0x%x\n",
7266 				bf_get(lpfc_sli_intf_sli_family,
7267 				       &phba->sli4_hba.sli_intf),
7268 				bf_get(lpfc_sli_intf_slirev,
7269 				       &phba->sli4_hba.sli_intf),
7270 				bf_get(lpfc_sli_intf_if_type,
7271 				       &phba->sli4_hba.sli_intf),
7272 				bf_get(lpfc_sli_intf_sli_hint1,
7273 				       &phba->sli4_hba.sli_intf),
7274 				bf_get(lpfc_sli_intf_sli_hint2,
7275 				       &phba->sli4_hba.sli_intf),
7276 				bf_get(lpfc_sli_intf_func_type,
7277 				       &phba->sli4_hba.sli_intf));
7278 		/*
7279 		 * Check for other Port errors during the initialization
7280 		 * process.  Fail the load if the port did not come up
7281 		 * correctly.
7282 		 */
7283 		if_type = bf_get(lpfc_sli_intf_if_type,
7284 				 &phba->sli4_hba.sli_intf);
7285 		switch (if_type) {
7286 		case LPFC_SLI_INTF_IF_TYPE_0:
7287 			phba->sli4_hba.ue_mask_lo =
7288 			      readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
7289 			phba->sli4_hba.ue_mask_hi =
7290 			      readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
7291 			uerrlo_reg.word0 =
7292 			      readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
7293 			uerrhi_reg.word0 =
7294 				readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
7295 			if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
7296 			    (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
7297 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7298 						"1422 Unrecoverable Error "
7299 						"Detected during POST "
7300 						"uerr_lo_reg=0x%x, "
7301 						"uerr_hi_reg=0x%x, "
7302 						"ue_mask_lo_reg=0x%x, "
7303 						"ue_mask_hi_reg=0x%x\n",
7304 						uerrlo_reg.word0,
7305 						uerrhi_reg.word0,
7306 						phba->sli4_hba.ue_mask_lo,
7307 						phba->sli4_hba.ue_mask_hi);
7308 				port_error = -ENODEV;
7309 			}
7310 			break;
7311 		case LPFC_SLI_INTF_IF_TYPE_2:
7312 			/* Final checks.  The port status should be clean. */
7313 			if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7314 				&reg_data.word0) ||
7315 				(bf_get(lpfc_sliport_status_err, &reg_data) &&
7316 				 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
7317 				phba->work_status[0] =
7318 					readl(phba->sli4_hba.u.if_type2.
7319 					      ERR1regaddr);
7320 				phba->work_status[1] =
7321 					readl(phba->sli4_hba.u.if_type2.
7322 					      ERR2regaddr);
7323 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7324 					"2888 Unrecoverable port error "
7325 					"following POST: port status reg "
7326 					"0x%x, port_smphr reg 0x%x, "
7327 					"error 1=0x%x, error 2=0x%x\n",
7328 					reg_data.word0,
7329 					portsmphr_reg.word0,
7330 					phba->work_status[0],
7331 					phba->work_status[1]);
7332 				port_error = -ENODEV;
7333 			}
7334 			break;
7335 		case LPFC_SLI_INTF_IF_TYPE_1:
7336 		default:
7337 			break;
7338 		}
7339 	}
7340 	return port_error;
7341 }
7342 
7343 /**
7344  * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
7345  * @phba: pointer to lpfc hba data structure.
7346  * @if_type:  The SLI4 interface type getting configured.
7347  *
7348  * This routine is invoked to set up SLI4 BAR0 PCI config space register
7349  * memory map.
7350  **/
7351 static void
7352 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
7353 {
7354 	switch (if_type) {
7355 	case LPFC_SLI_INTF_IF_TYPE_0:
7356 		phba->sli4_hba.u.if_type0.UERRLOregaddr =
7357 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
7358 		phba->sli4_hba.u.if_type0.UERRHIregaddr =
7359 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
7360 		phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
7361 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
7362 		phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
7363 			phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
7364 		phba->sli4_hba.SLIINTFregaddr =
7365 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7366 		break;
7367 	case LPFC_SLI_INTF_IF_TYPE_2:
7368 		phba->sli4_hba.u.if_type2.EQDregaddr =
7369 			phba->sli4_hba.conf_regs_memmap_p +
7370 						LPFC_CTL_PORT_EQ_DELAY_OFFSET;
7371 		phba->sli4_hba.u.if_type2.ERR1regaddr =
7372 			phba->sli4_hba.conf_regs_memmap_p +
7373 						LPFC_CTL_PORT_ER1_OFFSET;
7374 		phba->sli4_hba.u.if_type2.ERR2regaddr =
7375 			phba->sli4_hba.conf_regs_memmap_p +
7376 						LPFC_CTL_PORT_ER2_OFFSET;
7377 		phba->sli4_hba.u.if_type2.CTRLregaddr =
7378 			phba->sli4_hba.conf_regs_memmap_p +
7379 						LPFC_CTL_PORT_CTL_OFFSET;
7380 		phba->sli4_hba.u.if_type2.STATUSregaddr =
7381 			phba->sli4_hba.conf_regs_memmap_p +
7382 						LPFC_CTL_PORT_STA_OFFSET;
7383 		phba->sli4_hba.SLIINTFregaddr =
7384 			phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
7385 		phba->sli4_hba.PSMPHRregaddr =
7386 			phba->sli4_hba.conf_regs_memmap_p +
7387 						LPFC_CTL_PORT_SEM_OFFSET;
7388 		phba->sli4_hba.RQDBregaddr =
7389 			phba->sli4_hba.conf_regs_memmap_p +
7390 						LPFC_ULP0_RQ_DOORBELL;
7391 		phba->sli4_hba.WQDBregaddr =
7392 			phba->sli4_hba.conf_regs_memmap_p +
7393 						LPFC_ULP0_WQ_DOORBELL;
7394 		phba->sli4_hba.EQCQDBregaddr =
7395 			phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
7396 		phba->sli4_hba.MQDBregaddr =
7397 			phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
7398 		phba->sli4_hba.BMBXregaddr =
7399 			phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
7400 		break;
7401 	case LPFC_SLI_INTF_IF_TYPE_1:
7402 	default:
7403 		dev_printk(KERN_ERR, &phba->pcidev->dev,
7404 			   "FATAL - unsupported SLI4 interface type - %d\n",
7405 			   if_type);
7406 		break;
7407 	}
7408 }
7409 
7410 /**
7411  * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
7412  * @phba: pointer to lpfc hba data structure.
7413  *
7414  * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
7415  * memory map.
7416  **/
7417 static void
7418 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
7419 {
7420 	phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7421 		LPFC_SLIPORT_IF0_SMPHR;
7422 	phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7423 		LPFC_HST_ISR0;
7424 	phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7425 		LPFC_HST_IMR0;
7426 	phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
7427 		LPFC_HST_ISCR0;
7428 }
7429 
7430 /**
7431  * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
7432  * @phba: pointer to lpfc hba data structure.
7433  * @vf: virtual function number
7434  *
7435  * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
7436  * based on the given viftual function number, @vf.
7437  *
7438  * Return 0 if successful, otherwise -ENODEV.
7439  **/
7440 static int
7441 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
7442 {
7443 	if (vf > LPFC_VIR_FUNC_MAX)
7444 		return -ENODEV;
7445 
7446 	phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7447 				vf * LPFC_VFR_PAGE_SIZE +
7448 					LPFC_ULP0_RQ_DOORBELL);
7449 	phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7450 				vf * LPFC_VFR_PAGE_SIZE +
7451 					LPFC_ULP0_WQ_DOORBELL);
7452 	phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7453 				vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
7454 	phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7455 				vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
7456 	phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
7457 				vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
7458 	return 0;
7459 }
7460 
7461 /**
7462  * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
7463  * @phba: pointer to lpfc hba data structure.
7464  *
7465  * This routine is invoked to create the bootstrap mailbox
7466  * region consistent with the SLI-4 interface spec.  This
7467  * routine allocates all memory necessary to communicate
7468  * mailbox commands to the port and sets up all alignment
7469  * needs.  No locks are expected to be held when calling
7470  * this routine.
7471  *
7472  * Return codes
7473  * 	0 - successful
7474  * 	-ENOMEM - could not allocated memory.
7475  **/
7476 static int
7477 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
7478 {
7479 	uint32_t bmbx_size;
7480 	struct lpfc_dmabuf *dmabuf;
7481 	struct dma_address *dma_address;
7482 	uint32_t pa_addr;
7483 	uint64_t phys_addr;
7484 
7485 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
7486 	if (!dmabuf)
7487 		return -ENOMEM;
7488 
7489 	/*
7490 	 * The bootstrap mailbox region is comprised of 2 parts
7491 	 * plus an alignment restriction of 16 bytes.
7492 	 */
7493 	bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
7494 	dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
7495 					   &dmabuf->phys, GFP_KERNEL);
7496 	if (!dmabuf->virt) {
7497 		kfree(dmabuf);
7498 		return -ENOMEM;
7499 	}
7500 
7501 	/*
7502 	 * Initialize the bootstrap mailbox pointers now so that the register
7503 	 * operations are simple later.  The mailbox dma address is required
7504 	 * to be 16-byte aligned.  Also align the virtual memory as each
7505 	 * maibox is copied into the bmbx mailbox region before issuing the
7506 	 * command to the port.
7507 	 */
7508 	phba->sli4_hba.bmbx.dmabuf = dmabuf;
7509 	phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
7510 
7511 	phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
7512 					      LPFC_ALIGN_16_BYTE);
7513 	phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
7514 					      LPFC_ALIGN_16_BYTE);
7515 
7516 	/*
7517 	 * Set the high and low physical addresses now.  The SLI4 alignment
7518 	 * requirement is 16 bytes and the mailbox is posted to the port
7519 	 * as two 30-bit addresses.  The other data is a bit marking whether
7520 	 * the 30-bit address is the high or low address.
7521 	 * Upcast bmbx aphys to 64bits so shift instruction compiles
7522 	 * clean on 32 bit machines.
7523 	 */
7524 	dma_address = &phba->sli4_hba.bmbx.dma_address;
7525 	phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
7526 	pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
7527 	dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
7528 					   LPFC_BMBX_BIT1_ADDR_HI);
7529 
7530 	pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
7531 	dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
7532 					   LPFC_BMBX_BIT1_ADDR_LO);
7533 	return 0;
7534 }
7535 
7536 /**
7537  * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
7538  * @phba: pointer to lpfc hba data structure.
7539  *
7540  * This routine is invoked to teardown the bootstrap mailbox
7541  * region and release all host resources. This routine requires
7542  * the caller to ensure all mailbox commands recovered, no
7543  * additional mailbox comands are sent, and interrupts are disabled
7544  * before calling this routine.
7545  *
7546  **/
7547 static void
7548 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
7549 {
7550 	dma_free_coherent(&phba->pcidev->dev,
7551 			  phba->sli4_hba.bmbx.bmbx_size,
7552 			  phba->sli4_hba.bmbx.dmabuf->virt,
7553 			  phba->sli4_hba.bmbx.dmabuf->phys);
7554 
7555 	kfree(phba->sli4_hba.bmbx.dmabuf);
7556 	memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
7557 }
7558 
7559 /**
7560  * lpfc_sli4_read_config - Get the config parameters.
7561  * @phba: pointer to lpfc hba data structure.
7562  *
7563  * This routine is invoked to read the configuration parameters from the HBA.
7564  * The configuration parameters are used to set the base and maximum values
7565  * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
7566  * allocation for the port.
7567  *
7568  * Return codes
7569  * 	0 - successful
7570  * 	-ENOMEM - No available memory
7571  *      -EIO - The mailbox failed to complete successfully.
7572  **/
7573 int
7574 lpfc_sli4_read_config(struct lpfc_hba *phba)
7575 {
7576 	LPFC_MBOXQ_t *pmb;
7577 	struct lpfc_mbx_read_config *rd_config;
7578 	union  lpfc_sli4_cfg_shdr *shdr;
7579 	uint32_t shdr_status, shdr_add_status;
7580 	struct lpfc_mbx_get_func_cfg *get_func_cfg;
7581 	struct lpfc_rsrc_desc_fcfcoe *desc;
7582 	char *pdesc_0;
7583 	uint16_t forced_link_speed;
7584 	uint32_t if_type;
7585 	int length, i, rc = 0, rc2;
7586 
7587 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7588 	if (!pmb) {
7589 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7590 				"2011 Unable to allocate memory for issuing "
7591 				"SLI_CONFIG_SPECIAL mailbox command\n");
7592 		return -ENOMEM;
7593 	}
7594 
7595 	lpfc_read_config(phba, pmb);
7596 
7597 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7598 	if (rc != MBX_SUCCESS) {
7599 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7600 			"2012 Mailbox failed , mbxCmd x%x "
7601 			"READ_CONFIG, mbxStatus x%x\n",
7602 			bf_get(lpfc_mqe_command, &pmb->u.mqe),
7603 			bf_get(lpfc_mqe_status, &pmb->u.mqe));
7604 		rc = -EIO;
7605 	} else {
7606 		rd_config = &pmb->u.mqe.un.rd_config;
7607 		if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
7608 			phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
7609 			phba->sli4_hba.lnk_info.lnk_tp =
7610 				bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
7611 			phba->sli4_hba.lnk_info.lnk_no =
7612 				bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
7613 			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7614 					"3081 lnk_type:%d, lnk_numb:%d\n",
7615 					phba->sli4_hba.lnk_info.lnk_tp,
7616 					phba->sli4_hba.lnk_info.lnk_no);
7617 		} else
7618 			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7619 					"3082 Mailbox (x%x) returned ldv:x0\n",
7620 					bf_get(lpfc_mqe_command, &pmb->u.mqe));
7621 		if (bf_get(lpfc_mbx_rd_conf_bbscn_def, rd_config)) {
7622 			phba->bbcredit_support = 1;
7623 			phba->sli4_hba.bbscn_params.word0 = rd_config->word8;
7624 		}
7625 
7626 		phba->sli4_hba.extents_in_use =
7627 			bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
7628 		phba->sli4_hba.max_cfg_param.max_xri =
7629 			bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
7630 		phba->sli4_hba.max_cfg_param.xri_base =
7631 			bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
7632 		phba->sli4_hba.max_cfg_param.max_vpi =
7633 			bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
7634 		phba->sli4_hba.max_cfg_param.vpi_base =
7635 			bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
7636 		phba->sli4_hba.max_cfg_param.max_rpi =
7637 			bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
7638 		phba->sli4_hba.max_cfg_param.rpi_base =
7639 			bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
7640 		phba->sli4_hba.max_cfg_param.max_vfi =
7641 			bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
7642 		phba->sli4_hba.max_cfg_param.vfi_base =
7643 			bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
7644 		phba->sli4_hba.max_cfg_param.max_fcfi =
7645 			bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
7646 		phba->sli4_hba.max_cfg_param.max_eq =
7647 			bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
7648 		phba->sli4_hba.max_cfg_param.max_rq =
7649 			bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
7650 		phba->sli4_hba.max_cfg_param.max_wq =
7651 			bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
7652 		phba->sli4_hba.max_cfg_param.max_cq =
7653 			bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
7654 		phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
7655 		phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
7656 		phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
7657 		phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
7658 		phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
7659 				(phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
7660 		phba->max_vports = phba->max_vpi;
7661 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7662 				"2003 cfg params Extents? %d "
7663 				"XRI(B:%d M:%d), "
7664 				"VPI(B:%d M:%d) "
7665 				"VFI(B:%d M:%d) "
7666 				"RPI(B:%d M:%d) "
7667 				"FCFI:%d EQ:%d CQ:%d WQ:%d RQ:%d\n",
7668 				phba->sli4_hba.extents_in_use,
7669 				phba->sli4_hba.max_cfg_param.xri_base,
7670 				phba->sli4_hba.max_cfg_param.max_xri,
7671 				phba->sli4_hba.max_cfg_param.vpi_base,
7672 				phba->sli4_hba.max_cfg_param.max_vpi,
7673 				phba->sli4_hba.max_cfg_param.vfi_base,
7674 				phba->sli4_hba.max_cfg_param.max_vfi,
7675 				phba->sli4_hba.max_cfg_param.rpi_base,
7676 				phba->sli4_hba.max_cfg_param.max_rpi,
7677 				phba->sli4_hba.max_cfg_param.max_fcfi,
7678 				phba->sli4_hba.max_cfg_param.max_eq,
7679 				phba->sli4_hba.max_cfg_param.max_cq,
7680 				phba->sli4_hba.max_cfg_param.max_wq,
7681 				phba->sli4_hba.max_cfg_param.max_rq);
7682 
7683 	}
7684 
7685 	if (rc)
7686 		goto read_cfg_out;
7687 
7688 	/* Update link speed if forced link speed is supported */
7689 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7690 	if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7691 		forced_link_speed =
7692 			bf_get(lpfc_mbx_rd_conf_link_speed, rd_config);
7693 		if (forced_link_speed) {
7694 			phba->hba_flag |= HBA_FORCED_LINK_SPEED;
7695 
7696 			switch (forced_link_speed) {
7697 			case LINK_SPEED_1G:
7698 				phba->cfg_link_speed =
7699 					LPFC_USER_LINK_SPEED_1G;
7700 				break;
7701 			case LINK_SPEED_2G:
7702 				phba->cfg_link_speed =
7703 					LPFC_USER_LINK_SPEED_2G;
7704 				break;
7705 			case LINK_SPEED_4G:
7706 				phba->cfg_link_speed =
7707 					LPFC_USER_LINK_SPEED_4G;
7708 				break;
7709 			case LINK_SPEED_8G:
7710 				phba->cfg_link_speed =
7711 					LPFC_USER_LINK_SPEED_8G;
7712 				break;
7713 			case LINK_SPEED_10G:
7714 				phba->cfg_link_speed =
7715 					LPFC_USER_LINK_SPEED_10G;
7716 				break;
7717 			case LINK_SPEED_16G:
7718 				phba->cfg_link_speed =
7719 					LPFC_USER_LINK_SPEED_16G;
7720 				break;
7721 			case LINK_SPEED_32G:
7722 				phba->cfg_link_speed =
7723 					LPFC_USER_LINK_SPEED_32G;
7724 				break;
7725 			case 0xffff:
7726 				phba->cfg_link_speed =
7727 					LPFC_USER_LINK_SPEED_AUTO;
7728 				break;
7729 			default:
7730 				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7731 						"0047 Unrecognized link "
7732 						"speed : %d\n",
7733 						forced_link_speed);
7734 				phba->cfg_link_speed =
7735 					LPFC_USER_LINK_SPEED_AUTO;
7736 			}
7737 		}
7738 	}
7739 
7740 	/* Reset the DFT_HBA_Q_DEPTH to the max xri  */
7741 	length = phba->sli4_hba.max_cfg_param.max_xri -
7742 			lpfc_sli4_get_els_iocb_cnt(phba);
7743 	if (phba->cfg_hba_queue_depth > length) {
7744 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7745 				"3361 HBA queue depth changed from %d to %d\n",
7746 				phba->cfg_hba_queue_depth, length);
7747 		phba->cfg_hba_queue_depth = length;
7748 	}
7749 
7750 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
7751 	    LPFC_SLI_INTF_IF_TYPE_2)
7752 		goto read_cfg_out;
7753 
7754 	/* get the pf# and vf# for SLI4 if_type 2 port */
7755 	length = (sizeof(struct lpfc_mbx_get_func_cfg) -
7756 		  sizeof(struct lpfc_sli4_cfg_mhdr));
7757 	lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
7758 			 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
7759 			 length, LPFC_SLI4_MBX_EMBED);
7760 
7761 	rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7762 	shdr = (union lpfc_sli4_cfg_shdr *)
7763 				&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7764 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7765 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7766 	if (rc2 || shdr_status || shdr_add_status) {
7767 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7768 				"3026 Mailbox failed , mbxCmd x%x "
7769 				"GET_FUNCTION_CONFIG, mbxStatus x%x\n",
7770 				bf_get(lpfc_mqe_command, &pmb->u.mqe),
7771 				bf_get(lpfc_mqe_status, &pmb->u.mqe));
7772 		goto read_cfg_out;
7773 	}
7774 
7775 	/* search for fc_fcoe resrouce descriptor */
7776 	get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
7777 
7778 	pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
7779 	desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
7780 	length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
7781 	if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
7782 		length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
7783 	else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
7784 		goto read_cfg_out;
7785 
7786 	for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
7787 		desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
7788 		if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
7789 		    bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
7790 			phba->sli4_hba.iov.pf_number =
7791 				bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
7792 			phba->sli4_hba.iov.vf_number =
7793 				bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
7794 			break;
7795 		}
7796 	}
7797 
7798 	if (i < LPFC_RSRC_DESC_MAX_NUM)
7799 		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7800 				"3027 GET_FUNCTION_CONFIG: pf_number:%d, "
7801 				"vf_number:%d\n", phba->sli4_hba.iov.pf_number,
7802 				phba->sli4_hba.iov.vf_number);
7803 	else
7804 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7805 				"3028 GET_FUNCTION_CONFIG: failed to find "
7806 				"Resrouce Descriptor:x%x\n",
7807 				LPFC_RSRC_DESC_TYPE_FCFCOE);
7808 
7809 read_cfg_out:
7810 	mempool_free(pmb, phba->mbox_mem_pool);
7811 	return rc;
7812 }
7813 
7814 /**
7815  * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
7816  * @phba: pointer to lpfc hba data structure.
7817  *
7818  * This routine is invoked to setup the port-side endian order when
7819  * the port if_type is 0.  This routine has no function for other
7820  * if_types.
7821  *
7822  * Return codes
7823  * 	0 - successful
7824  * 	-ENOMEM - No available memory
7825  *      -EIO - The mailbox failed to complete successfully.
7826  **/
7827 static int
7828 lpfc_setup_endian_order(struct lpfc_hba *phba)
7829 {
7830 	LPFC_MBOXQ_t *mboxq;
7831 	uint32_t if_type, rc = 0;
7832 	uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
7833 				      HOST_ENDIAN_HIGH_WORD1};
7834 
7835 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7836 	switch (if_type) {
7837 	case LPFC_SLI_INTF_IF_TYPE_0:
7838 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7839 						       GFP_KERNEL);
7840 		if (!mboxq) {
7841 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7842 					"0492 Unable to allocate memory for "
7843 					"issuing SLI_CONFIG_SPECIAL mailbox "
7844 					"command\n");
7845 			return -ENOMEM;
7846 		}
7847 
7848 		/*
7849 		 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
7850 		 * two words to contain special data values and no other data.
7851 		 */
7852 		memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
7853 		memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
7854 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7855 		if (rc != MBX_SUCCESS) {
7856 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7857 					"0493 SLI_CONFIG_SPECIAL mailbox "
7858 					"failed with status x%x\n",
7859 					rc);
7860 			rc = -EIO;
7861 		}
7862 		mempool_free(mboxq, phba->mbox_mem_pool);
7863 		break;
7864 	case LPFC_SLI_INTF_IF_TYPE_2:
7865 	case LPFC_SLI_INTF_IF_TYPE_1:
7866 	default:
7867 		break;
7868 	}
7869 	return rc;
7870 }
7871 
7872 /**
7873  * lpfc_sli4_queue_verify - Verify and update EQ counts
7874  * @phba: pointer to lpfc hba data structure.
7875  *
7876  * This routine is invoked to check the user settable queue counts for EQs.
7877  * After this routine is called the counts will be set to valid values that
7878  * adhere to the constraints of the system's interrupt vectors and the port's
7879  * queue resources.
7880  *
7881  * Return codes
7882  *      0 - successful
7883  *      -ENOMEM - No available memory
7884  **/
7885 static int
7886 lpfc_sli4_queue_verify(struct lpfc_hba *phba)
7887 {
7888 	int io_channel;
7889 	int fof_vectors = phba->cfg_fof ? 1 : 0;
7890 
7891 	/*
7892 	 * Sanity check for configured queue parameters against the run-time
7893 	 * device parameters
7894 	 */
7895 
7896 	/* Sanity check on HBA EQ parameters */
7897 	io_channel = phba->io_channel_irqs;
7898 
7899 	if (phba->sli4_hba.num_online_cpu < io_channel) {
7900 		lpfc_printf_log(phba,
7901 				KERN_ERR, LOG_INIT,
7902 				"3188 Reducing IO channels to match number of "
7903 				"online CPUs: from %d to %d\n",
7904 				io_channel, phba->sli4_hba.num_online_cpu);
7905 		io_channel = phba->sli4_hba.num_online_cpu;
7906 	}
7907 
7908 	if (io_channel + fof_vectors > phba->sli4_hba.max_cfg_param.max_eq) {
7909 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7910 				"2575 Reducing IO channels to match number of "
7911 				"available EQs: from %d to %d\n",
7912 				io_channel,
7913 				phba->sli4_hba.max_cfg_param.max_eq);
7914 		io_channel = phba->sli4_hba.max_cfg_param.max_eq - fof_vectors;
7915 	}
7916 
7917 	/* The actual number of FCP / NVME event queues adopted */
7918 	if (io_channel != phba->io_channel_irqs)
7919 		phba->io_channel_irqs = io_channel;
7920 	if (phba->cfg_fcp_io_channel > io_channel)
7921 		phba->cfg_fcp_io_channel = io_channel;
7922 	if (phba->cfg_nvme_io_channel > io_channel)
7923 		phba->cfg_nvme_io_channel = io_channel;
7924 	if (phba->cfg_nvme_io_channel < phba->cfg_nvmet_mrq)
7925 		phba->cfg_nvmet_mrq = phba->cfg_nvme_io_channel;
7926 
7927 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7928 			"2574 IO channels: irqs %d fcp %d nvme %d MRQ: %d\n",
7929 			phba->io_channel_irqs, phba->cfg_fcp_io_channel,
7930 			phba->cfg_nvme_io_channel, phba->cfg_nvmet_mrq);
7931 
7932 	/* Get EQ depth from module parameter, fake the default for now */
7933 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7934 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7935 
7936 	/* Get CQ depth from module parameter, fake the default for now */
7937 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7938 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
7939 	return 0;
7940 }
7941 
7942 static int
7943 lpfc_alloc_nvme_wq_cq(struct lpfc_hba *phba, int wqidx)
7944 {
7945 	struct lpfc_queue *qdesc;
7946 	int cnt;
7947 
7948 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7949 					    phba->sli4_hba.cq_ecount);
7950 	if (!qdesc) {
7951 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7952 				"0508 Failed allocate fast-path NVME CQ (%d)\n",
7953 				wqidx);
7954 		return 1;
7955 	}
7956 	phba->sli4_hba.nvme_cq[wqidx] = qdesc;
7957 
7958 	cnt = LPFC_NVME_WQSIZE;
7959 	qdesc = lpfc_sli4_queue_alloc(phba, LPFC_WQE128_SIZE, cnt);
7960 	if (!qdesc) {
7961 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7962 				"0509 Failed allocate fast-path NVME WQ (%d)\n",
7963 				wqidx);
7964 		return 1;
7965 	}
7966 	phba->sli4_hba.nvme_wq[wqidx] = qdesc;
7967 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7968 	return 0;
7969 }
7970 
7971 static int
7972 lpfc_alloc_fcp_wq_cq(struct lpfc_hba *phba, int wqidx)
7973 {
7974 	struct lpfc_queue *qdesc;
7975 	uint32_t wqesize;
7976 
7977 	/* Create Fast Path FCP CQs */
7978 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7979 					phba->sli4_hba.cq_ecount);
7980 	if (!qdesc) {
7981 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7982 			"0499 Failed allocate fast-path FCP CQ (%d)\n", wqidx);
7983 		return 1;
7984 	}
7985 	phba->sli4_hba.fcp_cq[wqidx] = qdesc;
7986 
7987 	/* Create Fast Path FCP WQs */
7988 	wqesize = (phba->fcp_embed_io) ?
7989 		LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
7990 	qdesc = lpfc_sli4_queue_alloc(phba, wqesize, phba->sli4_hba.wq_ecount);
7991 	if (!qdesc) {
7992 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7993 				"0503 Failed allocate fast-path FCP WQ (%d)\n",
7994 				wqidx);
7995 		return 1;
7996 	}
7997 	phba->sli4_hba.fcp_wq[wqidx] = qdesc;
7998 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
7999 	return 0;
8000 }
8001 
8002 /**
8003  * lpfc_sli4_queue_create - Create all the SLI4 queues
8004  * @phba: pointer to lpfc hba data structure.
8005  *
8006  * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
8007  * operation. For each SLI4 queue type, the parameters such as queue entry
8008  * count (queue depth) shall be taken from the module parameter. For now,
8009  * we just use some constant number as place holder.
8010  *
8011  * Return codes
8012  *      0 - successful
8013  *      -ENOMEM - No availble memory
8014  *      -EIO - The mailbox failed to complete successfully.
8015  **/
8016 int
8017 lpfc_sli4_queue_create(struct lpfc_hba *phba)
8018 {
8019 	struct lpfc_queue *qdesc;
8020 	int idx, io_channel;
8021 
8022 	/*
8023 	 * Create HBA Record arrays.
8024 	 * Both NVME and FCP will share that same vectors / EQs
8025 	 */
8026 	io_channel = phba->io_channel_irqs;
8027 	if (!io_channel)
8028 		return -ERANGE;
8029 
8030 	phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
8031 	phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
8032 	phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
8033 	phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
8034 	phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
8035 	phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
8036 	phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
8037 	phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
8038 	phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
8039 	phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
8040 
8041 	phba->sli4_hba.hba_eq =  kcalloc(io_channel,
8042 					sizeof(struct lpfc_queue *),
8043 					GFP_KERNEL);
8044 	if (!phba->sli4_hba.hba_eq) {
8045 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8046 			"2576 Failed allocate memory for "
8047 			"fast-path EQ record array\n");
8048 		goto out_error;
8049 	}
8050 
8051 	if (phba->cfg_fcp_io_channel) {
8052 		phba->sli4_hba.fcp_cq = kcalloc(phba->cfg_fcp_io_channel,
8053 						sizeof(struct lpfc_queue *),
8054 						GFP_KERNEL);
8055 		if (!phba->sli4_hba.fcp_cq) {
8056 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8057 					"2577 Failed allocate memory for "
8058 					"fast-path CQ record array\n");
8059 			goto out_error;
8060 		}
8061 		phba->sli4_hba.fcp_wq = kcalloc(phba->cfg_fcp_io_channel,
8062 						sizeof(struct lpfc_queue *),
8063 						GFP_KERNEL);
8064 		if (!phba->sli4_hba.fcp_wq) {
8065 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8066 					"2578 Failed allocate memory for "
8067 					"fast-path FCP WQ record array\n");
8068 			goto out_error;
8069 		}
8070 		/*
8071 		 * Since the first EQ can have multiple CQs associated with it,
8072 		 * this array is used to quickly see if we have a FCP fast-path
8073 		 * CQ match.
8074 		 */
8075 		phba->sli4_hba.fcp_cq_map = kcalloc(phba->cfg_fcp_io_channel,
8076 							sizeof(uint16_t),
8077 							GFP_KERNEL);
8078 		if (!phba->sli4_hba.fcp_cq_map) {
8079 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8080 					"2545 Failed allocate memory for "
8081 					"fast-path CQ map\n");
8082 			goto out_error;
8083 		}
8084 	}
8085 
8086 	if (phba->cfg_nvme_io_channel) {
8087 		phba->sli4_hba.nvme_cq = kcalloc(phba->cfg_nvme_io_channel,
8088 						sizeof(struct lpfc_queue *),
8089 						GFP_KERNEL);
8090 		if (!phba->sli4_hba.nvme_cq) {
8091 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8092 					"6077 Failed allocate memory for "
8093 					"fast-path CQ record array\n");
8094 			goto out_error;
8095 		}
8096 
8097 		phba->sli4_hba.nvme_wq = kcalloc(phba->cfg_nvme_io_channel,
8098 						sizeof(struct lpfc_queue *),
8099 						GFP_KERNEL);
8100 		if (!phba->sli4_hba.nvme_wq) {
8101 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8102 					"2581 Failed allocate memory for "
8103 					"fast-path NVME WQ record array\n");
8104 			goto out_error;
8105 		}
8106 
8107 		/*
8108 		 * Since the first EQ can have multiple CQs associated with it,
8109 		 * this array is used to quickly see if we have a NVME fast-path
8110 		 * CQ match.
8111 		 */
8112 		phba->sli4_hba.nvme_cq_map = kcalloc(phba->cfg_nvme_io_channel,
8113 							sizeof(uint16_t),
8114 							GFP_KERNEL);
8115 		if (!phba->sli4_hba.nvme_cq_map) {
8116 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8117 					"6078 Failed allocate memory for "
8118 					"fast-path CQ map\n");
8119 			goto out_error;
8120 		}
8121 
8122 		if (phba->nvmet_support) {
8123 			phba->sli4_hba.nvmet_cqset = kcalloc(
8124 					phba->cfg_nvmet_mrq,
8125 					sizeof(struct lpfc_queue *),
8126 					GFP_KERNEL);
8127 			if (!phba->sli4_hba.nvmet_cqset) {
8128 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8129 					"3121 Fail allocate memory for "
8130 					"fast-path CQ set array\n");
8131 				goto out_error;
8132 			}
8133 			phba->sli4_hba.nvmet_mrq_hdr = kcalloc(
8134 					phba->cfg_nvmet_mrq,
8135 					sizeof(struct lpfc_queue *),
8136 					GFP_KERNEL);
8137 			if (!phba->sli4_hba.nvmet_mrq_hdr) {
8138 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8139 					"3122 Fail allocate memory for "
8140 					"fast-path RQ set hdr array\n");
8141 				goto out_error;
8142 			}
8143 			phba->sli4_hba.nvmet_mrq_data = kcalloc(
8144 					phba->cfg_nvmet_mrq,
8145 					sizeof(struct lpfc_queue *),
8146 					GFP_KERNEL);
8147 			if (!phba->sli4_hba.nvmet_mrq_data) {
8148 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8149 					"3124 Fail allocate memory for "
8150 					"fast-path RQ set data array\n");
8151 				goto out_error;
8152 			}
8153 		}
8154 	}
8155 
8156 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8157 
8158 	/* Create HBA Event Queues (EQs) */
8159 	for (idx = 0; idx < io_channel; idx++) {
8160 		/* Create EQs */
8161 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
8162 					      phba->sli4_hba.eq_ecount);
8163 		if (!qdesc) {
8164 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8165 					"0497 Failed allocate EQ (%d)\n", idx);
8166 			goto out_error;
8167 		}
8168 		phba->sli4_hba.hba_eq[idx] = qdesc;
8169 	}
8170 
8171 	/* FCP and NVME io channels are not required to be balanced */
8172 
8173 	for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8174 		if (lpfc_alloc_fcp_wq_cq(phba, idx))
8175 			goto out_error;
8176 
8177 	for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
8178 		if (lpfc_alloc_nvme_wq_cq(phba, idx))
8179 			goto out_error;
8180 
8181 	if (phba->nvmet_support) {
8182 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8183 			qdesc = lpfc_sli4_queue_alloc(phba,
8184 					phba->sli4_hba.cq_esize,
8185 					phba->sli4_hba.cq_ecount);
8186 			if (!qdesc) {
8187 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8188 					"3142 Failed allocate NVME "
8189 					"CQ Set (%d)\n", idx);
8190 				goto out_error;
8191 			}
8192 			phba->sli4_hba.nvmet_cqset[idx] = qdesc;
8193 		}
8194 	}
8195 
8196 	/*
8197 	 * Create Slow Path Completion Queues (CQs)
8198 	 */
8199 
8200 	/* Create slow-path Mailbox Command Complete Queue */
8201 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8202 				      phba->sli4_hba.cq_ecount);
8203 	if (!qdesc) {
8204 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8205 				"0500 Failed allocate slow-path mailbox CQ\n");
8206 		goto out_error;
8207 	}
8208 	phba->sli4_hba.mbx_cq = qdesc;
8209 
8210 	/* Create slow-path ELS Complete Queue */
8211 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8212 				      phba->sli4_hba.cq_ecount);
8213 	if (!qdesc) {
8214 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8215 				"0501 Failed allocate slow-path ELS CQ\n");
8216 		goto out_error;
8217 	}
8218 	phba->sli4_hba.els_cq = qdesc;
8219 
8220 
8221 	/*
8222 	 * Create Slow Path Work Queues (WQs)
8223 	 */
8224 
8225 	/* Create Mailbox Command Queue */
8226 
8227 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
8228 				      phba->sli4_hba.mq_ecount);
8229 	if (!qdesc) {
8230 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8231 				"0505 Failed allocate slow-path MQ\n");
8232 		goto out_error;
8233 	}
8234 	phba->sli4_hba.mbx_wq = qdesc;
8235 
8236 	/*
8237 	 * Create ELS Work Queues
8238 	 */
8239 
8240 	/* Create slow-path ELS Work Queue */
8241 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8242 				      phba->sli4_hba.wq_ecount);
8243 	if (!qdesc) {
8244 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8245 				"0504 Failed allocate slow-path ELS WQ\n");
8246 		goto out_error;
8247 	}
8248 	phba->sli4_hba.els_wq = qdesc;
8249 	list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8250 
8251 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
8252 		/* Create NVME LS Complete Queue */
8253 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
8254 					      phba->sli4_hba.cq_ecount);
8255 		if (!qdesc) {
8256 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8257 					"6079 Failed allocate NVME LS CQ\n");
8258 			goto out_error;
8259 		}
8260 		phba->sli4_hba.nvmels_cq = qdesc;
8261 
8262 		/* Create NVME LS Work Queue */
8263 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
8264 					      phba->sli4_hba.wq_ecount);
8265 		if (!qdesc) {
8266 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8267 					"6080 Failed allocate NVME LS WQ\n");
8268 			goto out_error;
8269 		}
8270 		phba->sli4_hba.nvmels_wq = qdesc;
8271 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
8272 	}
8273 
8274 	/*
8275 	 * Create Receive Queue (RQ)
8276 	 */
8277 
8278 	/* Create Receive Queue for header */
8279 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8280 				      phba->sli4_hba.rq_ecount);
8281 	if (!qdesc) {
8282 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8283 				"0506 Failed allocate receive HRQ\n");
8284 		goto out_error;
8285 	}
8286 	phba->sli4_hba.hdr_rq = qdesc;
8287 
8288 	/* Create Receive Queue for data */
8289 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
8290 				      phba->sli4_hba.rq_ecount);
8291 	if (!qdesc) {
8292 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8293 				"0507 Failed allocate receive DRQ\n");
8294 		goto out_error;
8295 	}
8296 	phba->sli4_hba.dat_rq = qdesc;
8297 
8298 	if (phba->nvmet_support) {
8299 		for (idx = 0; idx < phba->cfg_nvmet_mrq; idx++) {
8300 			/* Create NVMET Receive Queue for header */
8301 			qdesc = lpfc_sli4_queue_alloc(phba,
8302 						      phba->sli4_hba.rq_esize,
8303 						      LPFC_NVMET_RQE_DEF_COUNT);
8304 			if (!qdesc) {
8305 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8306 						"3146 Failed allocate "
8307 						"receive HRQ\n");
8308 				goto out_error;
8309 			}
8310 			phba->sli4_hba.nvmet_mrq_hdr[idx] = qdesc;
8311 
8312 			/* Only needed for header of RQ pair */
8313 			qdesc->rqbp = kzalloc(sizeof(struct lpfc_rqb),
8314 					      GFP_KERNEL);
8315 			if (qdesc->rqbp == NULL) {
8316 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8317 						"6131 Failed allocate "
8318 						"Header RQBP\n");
8319 				goto out_error;
8320 			}
8321 
8322 			/* Put list in known state in case driver load fails. */
8323 			INIT_LIST_HEAD(&qdesc->rqbp->rqb_buffer_list);
8324 
8325 			/* Create NVMET Receive Queue for data */
8326 			qdesc = lpfc_sli4_queue_alloc(phba,
8327 						      phba->sli4_hba.rq_esize,
8328 						      LPFC_NVMET_RQE_DEF_COUNT);
8329 			if (!qdesc) {
8330 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8331 						"3156 Failed allocate "
8332 						"receive DRQ\n");
8333 				goto out_error;
8334 			}
8335 			phba->sli4_hba.nvmet_mrq_data[idx] = qdesc;
8336 		}
8337 	}
8338 
8339 	/* Create the Queues needed for Flash Optimized Fabric operations */
8340 	if (phba->cfg_fof)
8341 		lpfc_fof_queue_create(phba);
8342 	return 0;
8343 
8344 out_error:
8345 	lpfc_sli4_queue_destroy(phba);
8346 	return -ENOMEM;
8347 }
8348 
8349 static inline void
8350 __lpfc_sli4_release_queue(struct lpfc_queue **qp)
8351 {
8352 	if (*qp != NULL) {
8353 		lpfc_sli4_queue_free(*qp);
8354 		*qp = NULL;
8355 	}
8356 }
8357 
8358 static inline void
8359 lpfc_sli4_release_queues(struct lpfc_queue ***qs, int max)
8360 {
8361 	int idx;
8362 
8363 	if (*qs == NULL)
8364 		return;
8365 
8366 	for (idx = 0; idx < max; idx++)
8367 		__lpfc_sli4_release_queue(&(*qs)[idx]);
8368 
8369 	kfree(*qs);
8370 	*qs = NULL;
8371 }
8372 
8373 static inline void
8374 lpfc_sli4_release_queue_map(uint16_t **qmap)
8375 {
8376 	if (*qmap != NULL) {
8377 		kfree(*qmap);
8378 		*qmap = NULL;
8379 	}
8380 }
8381 
8382 /**
8383  * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
8384  * @phba: pointer to lpfc hba data structure.
8385  *
8386  * This routine is invoked to release all the SLI4 queues with the FCoE HBA
8387  * operation.
8388  *
8389  * Return codes
8390  *      0 - successful
8391  *      -ENOMEM - No available memory
8392  *      -EIO - The mailbox failed to complete successfully.
8393  **/
8394 void
8395 lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
8396 {
8397 	if (phba->cfg_fof)
8398 		lpfc_fof_queue_destroy(phba);
8399 
8400 	/* Release HBA eqs */
8401 	lpfc_sli4_release_queues(&phba->sli4_hba.hba_eq, phba->io_channel_irqs);
8402 
8403 	/* Release FCP cqs */
8404 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_cq,
8405 				 phba->cfg_fcp_io_channel);
8406 
8407 	/* Release FCP wqs */
8408 	lpfc_sli4_release_queues(&phba->sli4_hba.fcp_wq,
8409 				 phba->cfg_fcp_io_channel);
8410 
8411 	/* Release FCP CQ mapping array */
8412 	lpfc_sli4_release_queue_map(&phba->sli4_hba.fcp_cq_map);
8413 
8414 	/* Release NVME cqs */
8415 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_cq,
8416 					phba->cfg_nvme_io_channel);
8417 
8418 	/* Release NVME wqs */
8419 	lpfc_sli4_release_queues(&phba->sli4_hba.nvme_wq,
8420 					phba->cfg_nvme_io_channel);
8421 
8422 	/* Release NVME CQ mapping array */
8423 	lpfc_sli4_release_queue_map(&phba->sli4_hba.nvme_cq_map);
8424 
8425 	lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_cqset,
8426 					phba->cfg_nvmet_mrq);
8427 
8428 	lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_hdr,
8429 					phba->cfg_nvmet_mrq);
8430 	lpfc_sli4_release_queues(&phba->sli4_hba.nvmet_mrq_data,
8431 					phba->cfg_nvmet_mrq);
8432 
8433 	/* Release mailbox command work queue */
8434 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_wq);
8435 
8436 	/* Release ELS work queue */
8437 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_wq);
8438 
8439 	/* Release ELS work queue */
8440 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_wq);
8441 
8442 	/* Release unsolicited receive queue */
8443 	__lpfc_sli4_release_queue(&phba->sli4_hba.hdr_rq);
8444 	__lpfc_sli4_release_queue(&phba->sli4_hba.dat_rq);
8445 
8446 	/* Release ELS complete queue */
8447 	__lpfc_sli4_release_queue(&phba->sli4_hba.els_cq);
8448 
8449 	/* Release NVME LS complete queue */
8450 	__lpfc_sli4_release_queue(&phba->sli4_hba.nvmels_cq);
8451 
8452 	/* Release mailbox command complete queue */
8453 	__lpfc_sli4_release_queue(&phba->sli4_hba.mbx_cq);
8454 
8455 	/* Everything on this list has been freed */
8456 	INIT_LIST_HEAD(&phba->sli4_hba.lpfc_wq_list);
8457 }
8458 
8459 int
8460 lpfc_free_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *rq)
8461 {
8462 	struct lpfc_rqb *rqbp;
8463 	struct lpfc_dmabuf *h_buf;
8464 	struct rqb_dmabuf *rqb_buffer;
8465 
8466 	rqbp = rq->rqbp;
8467 	while (!list_empty(&rqbp->rqb_buffer_list)) {
8468 		list_remove_head(&rqbp->rqb_buffer_list, h_buf,
8469 				 struct lpfc_dmabuf, list);
8470 
8471 		rqb_buffer = container_of(h_buf, struct rqb_dmabuf, hbuf);
8472 		(rqbp->rqb_free_buffer)(phba, rqb_buffer);
8473 		rqbp->buffer_count--;
8474 	}
8475 	return 1;
8476 }
8477 
8478 static int
8479 lpfc_create_wq_cq(struct lpfc_hba *phba, struct lpfc_queue *eq,
8480 	struct lpfc_queue *cq, struct lpfc_queue *wq, uint16_t *cq_map,
8481 	int qidx, uint32_t qtype)
8482 {
8483 	struct lpfc_sli_ring *pring;
8484 	int rc;
8485 
8486 	if (!eq || !cq || !wq) {
8487 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8488 			"6085 Fast-path %s (%d) not allocated\n",
8489 			((eq) ? ((cq) ? "WQ" : "CQ") : "EQ"), qidx);
8490 		return -ENOMEM;
8491 	}
8492 
8493 	/* create the Cq first */
8494 	rc = lpfc_cq_create(phba, cq, eq,
8495 			(qtype == LPFC_MBOX) ? LPFC_MCQ : LPFC_WCQ, qtype);
8496 	if (rc) {
8497 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8498 			"6086 Failed setup of CQ (%d), rc = 0x%x\n",
8499 			qidx, (uint32_t)rc);
8500 		return rc;
8501 	}
8502 
8503 	if (qtype != LPFC_MBOX) {
8504 		/* Setup nvme_cq_map for fast lookup */
8505 		if (cq_map)
8506 			*cq_map = cq->queue_id;
8507 
8508 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8509 			"6087 CQ setup: cq[%d]-id=%d, parent eq[%d]-id=%d\n",
8510 			qidx, cq->queue_id, qidx, eq->queue_id);
8511 
8512 		/* create the wq */
8513 		rc = lpfc_wq_create(phba, wq, cq, qtype);
8514 		if (rc) {
8515 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8516 				"6123 Fail setup fastpath WQ (%d), rc = 0x%x\n",
8517 				qidx, (uint32_t)rc);
8518 			/* no need to tear down cq - caller will do so */
8519 			return rc;
8520 		}
8521 
8522 		/* Bind this CQ/WQ to the NVME ring */
8523 		pring = wq->pring;
8524 		pring->sli.sli4.wqp = (void *)wq;
8525 		cq->pring = pring;
8526 
8527 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8528 			"2593 WQ setup: wq[%d]-id=%d assoc=%d, cq[%d]-id=%d\n",
8529 			qidx, wq->queue_id, wq->assoc_qid, qidx, cq->queue_id);
8530 	} else {
8531 		rc = lpfc_mq_create(phba, wq, cq, LPFC_MBOX);
8532 		if (rc) {
8533 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8534 				"0539 Failed setup of slow-path MQ: "
8535 				"rc = 0x%x\n", rc);
8536 			/* no need to tear down cq - caller will do so */
8537 			return rc;
8538 		}
8539 
8540 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8541 			"2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
8542 			phba->sli4_hba.mbx_wq->queue_id,
8543 			phba->sli4_hba.mbx_cq->queue_id);
8544 	}
8545 
8546 	return 0;
8547 }
8548 
8549 /**
8550  * lpfc_sli4_queue_setup - Set up all the SLI4 queues
8551  * @phba: pointer to lpfc hba data structure.
8552  *
8553  * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
8554  * operation.
8555  *
8556  * Return codes
8557  *      0 - successful
8558  *      -ENOMEM - No available memory
8559  *      -EIO - The mailbox failed to complete successfully.
8560  **/
8561 int
8562 lpfc_sli4_queue_setup(struct lpfc_hba *phba)
8563 {
8564 	uint32_t shdr_status, shdr_add_status;
8565 	union lpfc_sli4_cfg_shdr *shdr;
8566 	LPFC_MBOXQ_t *mboxq;
8567 	int qidx;
8568 	uint32_t length, io_channel;
8569 	int rc = -ENOMEM;
8570 
8571 	/* Check for dual-ULP support */
8572 	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8573 	if (!mboxq) {
8574 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8575 				"3249 Unable to allocate memory for "
8576 				"QUERY_FW_CFG mailbox command\n");
8577 		return -ENOMEM;
8578 	}
8579 	length = (sizeof(struct lpfc_mbx_query_fw_config) -
8580 		  sizeof(struct lpfc_sli4_cfg_mhdr));
8581 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8582 			 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
8583 			 length, LPFC_SLI4_MBX_EMBED);
8584 
8585 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8586 
8587 	shdr = (union lpfc_sli4_cfg_shdr *)
8588 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
8589 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
8590 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
8591 	if (shdr_status || shdr_add_status || rc) {
8592 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8593 				"3250 QUERY_FW_CFG mailbox failed with status "
8594 				"x%x add_status x%x, mbx status x%x\n",
8595 				shdr_status, shdr_add_status, rc);
8596 		if (rc != MBX_TIMEOUT)
8597 			mempool_free(mboxq, phba->mbox_mem_pool);
8598 		rc = -ENXIO;
8599 		goto out_error;
8600 	}
8601 
8602 	phba->sli4_hba.fw_func_mode =
8603 			mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
8604 	phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
8605 	phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
8606 	phba->sli4_hba.physical_port =
8607 			mboxq->u.mqe.un.query_fw_cfg.rsp.physical_port;
8608 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8609 			"3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
8610 			"ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
8611 			phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
8612 
8613 	if (rc != MBX_TIMEOUT)
8614 		mempool_free(mboxq, phba->mbox_mem_pool);
8615 
8616 	/*
8617 	 * Set up HBA Event Queues (EQs)
8618 	 */
8619 	io_channel = phba->io_channel_irqs;
8620 
8621 	/* Set up HBA event queue */
8622 	if (io_channel && !phba->sli4_hba.hba_eq) {
8623 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8624 				"3147 Fast-path EQs not allocated\n");
8625 		rc = -ENOMEM;
8626 		goto out_error;
8627 	}
8628 	for (qidx = 0; qidx < io_channel; qidx++) {
8629 		if (!phba->sli4_hba.hba_eq[qidx]) {
8630 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8631 					"0522 Fast-path EQ (%d) not "
8632 					"allocated\n", qidx);
8633 			rc = -ENOMEM;
8634 			goto out_destroy;
8635 		}
8636 		rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[qidx],
8637 						phba->cfg_fcp_imax);
8638 		if (rc) {
8639 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8640 					"0523 Failed setup of fast-path EQ "
8641 					"(%d), rc = 0x%x\n", qidx,
8642 					(uint32_t)rc);
8643 			goto out_destroy;
8644 		}
8645 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8646 				"2584 HBA EQ setup: queue[%d]-id=%d\n",
8647 				qidx, phba->sli4_hba.hba_eq[qidx]->queue_id);
8648 	}
8649 
8650 	if (phba->cfg_nvme_io_channel) {
8651 		if (!phba->sli4_hba.nvme_cq || !phba->sli4_hba.nvme_wq) {
8652 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8653 				"6084 Fast-path NVME %s array not allocated\n",
8654 				(phba->sli4_hba.nvme_cq) ? "CQ" : "WQ");
8655 			rc = -ENOMEM;
8656 			goto out_destroy;
8657 		}
8658 
8659 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
8660 			rc = lpfc_create_wq_cq(phba,
8661 					phba->sli4_hba.hba_eq[
8662 						qidx % io_channel],
8663 					phba->sli4_hba.nvme_cq[qidx],
8664 					phba->sli4_hba.nvme_wq[qidx],
8665 					&phba->sli4_hba.nvme_cq_map[qidx],
8666 					qidx, LPFC_NVME);
8667 			if (rc) {
8668 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8669 					"6123 Failed to setup fastpath "
8670 					"NVME WQ/CQ (%d), rc = 0x%x\n",
8671 					qidx, (uint32_t)rc);
8672 				goto out_destroy;
8673 			}
8674 		}
8675 	}
8676 
8677 	if (phba->cfg_fcp_io_channel) {
8678 		/* Set up fast-path FCP Response Complete Queue */
8679 		if (!phba->sli4_hba.fcp_cq || !phba->sli4_hba.fcp_wq) {
8680 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8681 				"3148 Fast-path FCP %s array not allocated\n",
8682 				phba->sli4_hba.fcp_cq ? "WQ" : "CQ");
8683 			rc = -ENOMEM;
8684 			goto out_destroy;
8685 		}
8686 
8687 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
8688 			rc = lpfc_create_wq_cq(phba,
8689 					phba->sli4_hba.hba_eq[
8690 						qidx % io_channel],
8691 					phba->sli4_hba.fcp_cq[qidx],
8692 					phba->sli4_hba.fcp_wq[qidx],
8693 					&phba->sli4_hba.fcp_cq_map[qidx],
8694 					qidx, LPFC_FCP);
8695 			if (rc) {
8696 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8697 					"0535 Failed to setup fastpath "
8698 					"FCP WQ/CQ (%d), rc = 0x%x\n",
8699 					qidx, (uint32_t)rc);
8700 				goto out_destroy;
8701 			}
8702 		}
8703 	}
8704 
8705 	/*
8706 	 * Set up Slow Path Complete Queues (CQs)
8707 	 */
8708 
8709 	/* Set up slow-path MBOX CQ/MQ */
8710 
8711 	if (!phba->sli4_hba.mbx_cq || !phba->sli4_hba.mbx_wq) {
8712 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8713 				"0528 %s not allocated\n",
8714 				phba->sli4_hba.mbx_cq ?
8715 				"Mailbox WQ" : "Mailbox CQ");
8716 		rc = -ENOMEM;
8717 		goto out_destroy;
8718 	}
8719 
8720 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8721 			       phba->sli4_hba.mbx_cq,
8722 			       phba->sli4_hba.mbx_wq,
8723 			       NULL, 0, LPFC_MBOX);
8724 	if (rc) {
8725 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8726 			"0529 Failed setup of mailbox WQ/CQ: rc = 0x%x\n",
8727 			(uint32_t)rc);
8728 		goto out_destroy;
8729 	}
8730 	if (phba->nvmet_support) {
8731 		if (!phba->sli4_hba.nvmet_cqset) {
8732 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8733 					"3165 Fast-path NVME CQ Set "
8734 					"array not allocated\n");
8735 			rc = -ENOMEM;
8736 			goto out_destroy;
8737 		}
8738 		if (phba->cfg_nvmet_mrq > 1) {
8739 			rc = lpfc_cq_create_set(phba,
8740 					phba->sli4_hba.nvmet_cqset,
8741 					phba->sli4_hba.hba_eq,
8742 					LPFC_WCQ, LPFC_NVMET);
8743 			if (rc) {
8744 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8745 						"3164 Failed setup of NVME CQ "
8746 						"Set, rc = 0x%x\n",
8747 						(uint32_t)rc);
8748 				goto out_destroy;
8749 			}
8750 		} else {
8751 			/* Set up NVMET Receive Complete Queue */
8752 			rc = lpfc_cq_create(phba, phba->sli4_hba.nvmet_cqset[0],
8753 					    phba->sli4_hba.hba_eq[0],
8754 					    LPFC_WCQ, LPFC_NVMET);
8755 			if (rc) {
8756 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8757 						"6089 Failed setup NVMET CQ: "
8758 						"rc = 0x%x\n", (uint32_t)rc);
8759 				goto out_destroy;
8760 			}
8761 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8762 					"6090 NVMET CQ setup: cq-id=%d, "
8763 					"parent eq-id=%d\n",
8764 					phba->sli4_hba.nvmet_cqset[0]->queue_id,
8765 					phba->sli4_hba.hba_eq[0]->queue_id);
8766 		}
8767 	}
8768 
8769 	/* Set up slow-path ELS WQ/CQ */
8770 	if (!phba->sli4_hba.els_cq || !phba->sli4_hba.els_wq) {
8771 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8772 				"0530 ELS %s not allocated\n",
8773 				phba->sli4_hba.els_cq ? "WQ" : "CQ");
8774 		rc = -ENOMEM;
8775 		goto out_destroy;
8776 	}
8777 	rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8778 					phba->sli4_hba.els_cq,
8779 					phba->sli4_hba.els_wq,
8780 					NULL, 0, LPFC_ELS);
8781 	if (rc) {
8782 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8783 			"0529 Failed setup of ELS WQ/CQ: rc = 0x%x\n",
8784 			(uint32_t)rc);
8785 		goto out_destroy;
8786 	}
8787 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8788 			"2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
8789 			phba->sli4_hba.els_wq->queue_id,
8790 			phba->sli4_hba.els_cq->queue_id);
8791 
8792 	if (phba->cfg_nvme_io_channel) {
8793 		/* Set up NVME LS Complete Queue */
8794 		if (!phba->sli4_hba.nvmels_cq || !phba->sli4_hba.nvmels_wq) {
8795 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8796 					"6091 LS %s not allocated\n",
8797 					phba->sli4_hba.nvmels_cq ? "WQ" : "CQ");
8798 			rc = -ENOMEM;
8799 			goto out_destroy;
8800 		}
8801 		rc = lpfc_create_wq_cq(phba, phba->sli4_hba.hba_eq[0],
8802 					phba->sli4_hba.nvmels_cq,
8803 					phba->sli4_hba.nvmels_wq,
8804 					NULL, 0, LPFC_NVME_LS);
8805 		if (rc) {
8806 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8807 				"0529 Failed setup of NVVME LS WQ/CQ: "
8808 				"rc = 0x%x\n", (uint32_t)rc);
8809 			goto out_destroy;
8810 		}
8811 
8812 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8813 				"6096 ELS WQ setup: wq-id=%d, "
8814 				"parent cq-id=%d\n",
8815 				phba->sli4_hba.nvmels_wq->queue_id,
8816 				phba->sli4_hba.nvmels_cq->queue_id);
8817 	}
8818 
8819 	/*
8820 	 * Create NVMET Receive Queue (RQ)
8821 	 */
8822 	if (phba->nvmet_support) {
8823 		if ((!phba->sli4_hba.nvmet_cqset) ||
8824 		    (!phba->sli4_hba.nvmet_mrq_hdr) ||
8825 		    (!phba->sli4_hba.nvmet_mrq_data)) {
8826 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8827 					"6130 MRQ CQ Queues not "
8828 					"allocated\n");
8829 			rc = -ENOMEM;
8830 			goto out_destroy;
8831 		}
8832 		if (phba->cfg_nvmet_mrq > 1) {
8833 			rc = lpfc_mrq_create(phba,
8834 					     phba->sli4_hba.nvmet_mrq_hdr,
8835 					     phba->sli4_hba.nvmet_mrq_data,
8836 					     phba->sli4_hba.nvmet_cqset,
8837 					     LPFC_NVMET);
8838 			if (rc) {
8839 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8840 						"6098 Failed setup of NVMET "
8841 						"MRQ: rc = 0x%x\n",
8842 						(uint32_t)rc);
8843 				goto out_destroy;
8844 			}
8845 
8846 		} else {
8847 			rc = lpfc_rq_create(phba,
8848 					    phba->sli4_hba.nvmet_mrq_hdr[0],
8849 					    phba->sli4_hba.nvmet_mrq_data[0],
8850 					    phba->sli4_hba.nvmet_cqset[0],
8851 					    LPFC_NVMET);
8852 			if (rc) {
8853 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8854 						"6057 Failed setup of NVMET "
8855 						"Receive Queue: rc = 0x%x\n",
8856 						(uint32_t)rc);
8857 				goto out_destroy;
8858 			}
8859 
8860 			lpfc_printf_log(
8861 				phba, KERN_INFO, LOG_INIT,
8862 				"6099 NVMET RQ setup: hdr-rq-id=%d, "
8863 				"dat-rq-id=%d parent cq-id=%d\n",
8864 				phba->sli4_hba.nvmet_mrq_hdr[0]->queue_id,
8865 				phba->sli4_hba.nvmet_mrq_data[0]->queue_id,
8866 				phba->sli4_hba.nvmet_cqset[0]->queue_id);
8867 
8868 		}
8869 	}
8870 
8871 	if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
8872 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8873 				"0540 Receive Queue not allocated\n");
8874 		rc = -ENOMEM;
8875 		goto out_destroy;
8876 	}
8877 
8878 	rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
8879 			    phba->sli4_hba.els_cq, LPFC_USOL);
8880 	if (rc) {
8881 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8882 				"0541 Failed setup of Receive Queue: "
8883 				"rc = 0x%x\n", (uint32_t)rc);
8884 		goto out_destroy;
8885 	}
8886 
8887 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8888 			"2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
8889 			"parent cq-id=%d\n",
8890 			phba->sli4_hba.hdr_rq->queue_id,
8891 			phba->sli4_hba.dat_rq->queue_id,
8892 			phba->sli4_hba.els_cq->queue_id);
8893 
8894 	if (phba->cfg_fof) {
8895 		rc = lpfc_fof_queue_setup(phba);
8896 		if (rc) {
8897 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8898 					"0549 Failed setup of FOF Queues: "
8899 					"rc = 0x%x\n", rc);
8900 			goto out_destroy;
8901 		}
8902 	}
8903 
8904 	for (qidx = 0; qidx < io_channel; qidx += LPFC_MAX_EQ_DELAY_EQID_CNT)
8905 		lpfc_modify_hba_eq_delay(phba, qidx, LPFC_MAX_EQ_DELAY_EQID_CNT,
8906 					 phba->cfg_fcp_imax);
8907 
8908 	return 0;
8909 
8910 out_destroy:
8911 	lpfc_sli4_queue_unset(phba);
8912 out_error:
8913 	return rc;
8914 }
8915 
8916 /**
8917  * lpfc_sli4_queue_unset - Unset all the SLI4 queues
8918  * @phba: pointer to lpfc hba data structure.
8919  *
8920  * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
8921  * operation.
8922  *
8923  * Return codes
8924  *      0 - successful
8925  *      -ENOMEM - No available memory
8926  *      -EIO - The mailbox failed to complete successfully.
8927  **/
8928 void
8929 lpfc_sli4_queue_unset(struct lpfc_hba *phba)
8930 {
8931 	int qidx;
8932 
8933 	/* Unset the queues created for Flash Optimized Fabric operations */
8934 	if (phba->cfg_fof)
8935 		lpfc_fof_queue_destroy(phba);
8936 
8937 	/* Unset mailbox command work queue */
8938 	if (phba->sli4_hba.mbx_wq)
8939 		lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
8940 
8941 	/* Unset NVME LS work queue */
8942 	if (phba->sli4_hba.nvmels_wq)
8943 		lpfc_wq_destroy(phba, phba->sli4_hba.nvmels_wq);
8944 
8945 	/* Unset ELS work queue */
8946 	if (phba->sli4_hba.els_wq)
8947 		lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
8948 
8949 	/* Unset unsolicited receive queue */
8950 	if (phba->sli4_hba.hdr_rq)
8951 		lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq,
8952 				phba->sli4_hba.dat_rq);
8953 
8954 	/* Unset FCP work queue */
8955 	if (phba->sli4_hba.fcp_wq)
8956 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
8957 			lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[qidx]);
8958 
8959 	/* Unset NVME work queue */
8960 	if (phba->sli4_hba.nvme_wq) {
8961 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8962 			lpfc_wq_destroy(phba, phba->sli4_hba.nvme_wq[qidx]);
8963 	}
8964 
8965 	/* Unset mailbox command complete queue */
8966 	if (phba->sli4_hba.mbx_cq)
8967 		lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
8968 
8969 	/* Unset ELS complete queue */
8970 	if (phba->sli4_hba.els_cq)
8971 		lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
8972 
8973 	/* Unset NVME LS complete queue */
8974 	if (phba->sli4_hba.nvmels_cq)
8975 		lpfc_cq_destroy(phba, phba->sli4_hba.nvmels_cq);
8976 
8977 	/* Unset NVME response complete queue */
8978 	if (phba->sli4_hba.nvme_cq)
8979 		for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
8980 			lpfc_cq_destroy(phba, phba->sli4_hba.nvme_cq[qidx]);
8981 
8982 	/* Unset NVMET MRQ queue */
8983 	if (phba->sli4_hba.nvmet_mrq_hdr) {
8984 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8985 			lpfc_rq_destroy(phba,
8986 					phba->sli4_hba.nvmet_mrq_hdr[qidx],
8987 					phba->sli4_hba.nvmet_mrq_data[qidx]);
8988 	}
8989 
8990 	/* Unset NVMET CQ Set complete queue */
8991 	if (phba->sli4_hba.nvmet_cqset) {
8992 		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++)
8993 			lpfc_cq_destroy(phba,
8994 					phba->sli4_hba.nvmet_cqset[qidx]);
8995 	}
8996 
8997 	/* Unset FCP response complete queue */
8998 	if (phba->sli4_hba.fcp_cq)
8999 		for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
9000 			lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[qidx]);
9001 
9002 	/* Unset fast-path event queue */
9003 	if (phba->sli4_hba.hba_eq)
9004 		for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
9005 			lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[qidx]);
9006 }
9007 
9008 /**
9009  * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
9010  * @phba: pointer to lpfc hba data structure.
9011  *
9012  * This routine is invoked to allocate and set up a pool of completion queue
9013  * events. The body of the completion queue event is a completion queue entry
9014  * CQE. For now, this pool is used for the interrupt service routine to queue
9015  * the following HBA completion queue events for the worker thread to process:
9016  *   - Mailbox asynchronous events
9017  *   - Receive queue completion unsolicited events
9018  * Later, this can be used for all the slow-path events.
9019  *
9020  * Return codes
9021  *      0 - successful
9022  *      -ENOMEM - No available memory
9023  **/
9024 static int
9025 lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
9026 {
9027 	struct lpfc_cq_event *cq_event;
9028 	int i;
9029 
9030 	for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
9031 		cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
9032 		if (!cq_event)
9033 			goto out_pool_create_fail;
9034 		list_add_tail(&cq_event->list,
9035 			      &phba->sli4_hba.sp_cqe_event_pool);
9036 	}
9037 	return 0;
9038 
9039 out_pool_create_fail:
9040 	lpfc_sli4_cq_event_pool_destroy(phba);
9041 	return -ENOMEM;
9042 }
9043 
9044 /**
9045  * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
9046  * @phba: pointer to lpfc hba data structure.
9047  *
9048  * This routine is invoked to free the pool of completion queue events at
9049  * driver unload time. Note that, it is the responsibility of the driver
9050  * cleanup routine to free all the outstanding completion-queue events
9051  * allocated from this pool back into the pool before invoking this routine
9052  * to destroy the pool.
9053  **/
9054 static void
9055 lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
9056 {
9057 	struct lpfc_cq_event *cq_event, *next_cq_event;
9058 
9059 	list_for_each_entry_safe(cq_event, next_cq_event,
9060 				 &phba->sli4_hba.sp_cqe_event_pool, list) {
9061 		list_del(&cq_event->list);
9062 		kfree(cq_event);
9063 	}
9064 }
9065 
9066 /**
9067  * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9068  * @phba: pointer to lpfc hba data structure.
9069  *
9070  * This routine is the lock free version of the API invoked to allocate a
9071  * completion-queue event from the free pool.
9072  *
9073  * Return: Pointer to the newly allocated completion-queue event if successful
9074  *         NULL otherwise.
9075  **/
9076 struct lpfc_cq_event *
9077 __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9078 {
9079 	struct lpfc_cq_event *cq_event = NULL;
9080 
9081 	list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
9082 			 struct lpfc_cq_event, list);
9083 	return cq_event;
9084 }
9085 
9086 /**
9087  * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
9088  * @phba: pointer to lpfc hba data structure.
9089  *
9090  * This routine is the lock version of the API invoked to allocate a
9091  * completion-queue event from the free pool.
9092  *
9093  * Return: Pointer to the newly allocated completion-queue event if successful
9094  *         NULL otherwise.
9095  **/
9096 struct lpfc_cq_event *
9097 lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
9098 {
9099 	struct lpfc_cq_event *cq_event;
9100 	unsigned long iflags;
9101 
9102 	spin_lock_irqsave(&phba->hbalock, iflags);
9103 	cq_event = __lpfc_sli4_cq_event_alloc(phba);
9104 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9105 	return cq_event;
9106 }
9107 
9108 /**
9109  * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9110  * @phba: pointer to lpfc hba data structure.
9111  * @cq_event: pointer to the completion queue event to be freed.
9112  *
9113  * This routine is the lock free version of the API invoked to release a
9114  * completion-queue event back into the free pool.
9115  **/
9116 void
9117 __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9118 			     struct lpfc_cq_event *cq_event)
9119 {
9120 	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
9121 }
9122 
9123 /**
9124  * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
9125  * @phba: pointer to lpfc hba data structure.
9126  * @cq_event: pointer to the completion queue event to be freed.
9127  *
9128  * This routine is the lock version of the API invoked to release a
9129  * completion-queue event back into the free pool.
9130  **/
9131 void
9132 lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
9133 			   struct lpfc_cq_event *cq_event)
9134 {
9135 	unsigned long iflags;
9136 	spin_lock_irqsave(&phba->hbalock, iflags);
9137 	__lpfc_sli4_cq_event_release(phba, cq_event);
9138 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9139 }
9140 
9141 /**
9142  * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
9143  * @phba: pointer to lpfc hba data structure.
9144  *
9145  * This routine is to free all the pending completion-queue events to the
9146  * back into the free pool for device reset.
9147  **/
9148 static void
9149 lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
9150 {
9151 	LIST_HEAD(cqelist);
9152 	struct lpfc_cq_event *cqe;
9153 	unsigned long iflags;
9154 
9155 	/* Retrieve all the pending WCQEs from pending WCQE lists */
9156 	spin_lock_irqsave(&phba->hbalock, iflags);
9157 	/* Pending FCP XRI abort events */
9158 	list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9159 			 &cqelist);
9160 	/* Pending ELS XRI abort events */
9161 	list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9162 			 &cqelist);
9163 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
9164 		/* Pending NVME XRI abort events */
9165 		list_splice_init(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
9166 				 &cqelist);
9167 	}
9168 	/* Pending asynnc events */
9169 	list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
9170 			 &cqelist);
9171 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9172 
9173 	while (!list_empty(&cqelist)) {
9174 		list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
9175 		lpfc_sli4_cq_event_release(phba, cqe);
9176 	}
9177 }
9178 
9179 /**
9180  * lpfc_pci_function_reset - Reset pci function.
9181  * @phba: pointer to lpfc hba data structure.
9182  *
9183  * This routine is invoked to request a PCI function reset. It will destroys
9184  * all resources assigned to the PCI function which originates this request.
9185  *
9186  * Return codes
9187  *      0 - successful
9188  *      -ENOMEM - No available memory
9189  *      -EIO - The mailbox failed to complete successfully.
9190  **/
9191 int
9192 lpfc_pci_function_reset(struct lpfc_hba *phba)
9193 {
9194 	LPFC_MBOXQ_t *mboxq;
9195 	uint32_t rc = 0, if_type;
9196 	uint32_t shdr_status, shdr_add_status;
9197 	uint32_t rdy_chk;
9198 	uint32_t port_reset = 0;
9199 	union lpfc_sli4_cfg_shdr *shdr;
9200 	struct lpfc_register reg_data;
9201 	uint16_t devid;
9202 
9203 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9204 	switch (if_type) {
9205 	case LPFC_SLI_INTF_IF_TYPE_0:
9206 		mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
9207 						       GFP_KERNEL);
9208 		if (!mboxq) {
9209 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9210 					"0494 Unable to allocate memory for "
9211 					"issuing SLI_FUNCTION_RESET mailbox "
9212 					"command\n");
9213 			return -ENOMEM;
9214 		}
9215 
9216 		/* Setup PCI function reset mailbox-ioctl command */
9217 		lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9218 				 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
9219 				 LPFC_SLI4_MBX_EMBED);
9220 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9221 		shdr = (union lpfc_sli4_cfg_shdr *)
9222 			&mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
9223 		shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9224 		shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
9225 					 &shdr->response);
9226 		if (rc != MBX_TIMEOUT)
9227 			mempool_free(mboxq, phba->mbox_mem_pool);
9228 		if (shdr_status || shdr_add_status || rc) {
9229 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9230 					"0495 SLI_FUNCTION_RESET mailbox "
9231 					"failed with status x%x add_status x%x,"
9232 					" mbx status x%x\n",
9233 					shdr_status, shdr_add_status, rc);
9234 			rc = -ENXIO;
9235 		}
9236 		break;
9237 	case LPFC_SLI_INTF_IF_TYPE_2:
9238 wait:
9239 		/*
9240 		 * Poll the Port Status Register and wait for RDY for
9241 		 * up to 30 seconds. If the port doesn't respond, treat
9242 		 * it as an error.
9243 		 */
9244 		for (rdy_chk = 0; rdy_chk < 1500; rdy_chk++) {
9245 			if (lpfc_readl(phba->sli4_hba.u.if_type2.
9246 				STATUSregaddr, &reg_data.word0)) {
9247 				rc = -ENODEV;
9248 				goto out;
9249 			}
9250 			if (bf_get(lpfc_sliport_status_rdy, &reg_data))
9251 				break;
9252 			msleep(20);
9253 		}
9254 
9255 		if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
9256 			phba->work_status[0] = readl(
9257 				phba->sli4_hba.u.if_type2.ERR1regaddr);
9258 			phba->work_status[1] = readl(
9259 				phba->sli4_hba.u.if_type2.ERR2regaddr);
9260 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9261 					"2890 Port not ready, port status reg "
9262 					"0x%x error 1=0x%x, error 2=0x%x\n",
9263 					reg_data.word0,
9264 					phba->work_status[0],
9265 					phba->work_status[1]);
9266 			rc = -ENODEV;
9267 			goto out;
9268 		}
9269 
9270 		if (!port_reset) {
9271 			/*
9272 			 * Reset the port now
9273 			 */
9274 			reg_data.word0 = 0;
9275 			bf_set(lpfc_sliport_ctrl_end, &reg_data,
9276 			       LPFC_SLIPORT_LITTLE_ENDIAN);
9277 			bf_set(lpfc_sliport_ctrl_ip, &reg_data,
9278 			       LPFC_SLIPORT_INIT_PORT);
9279 			writel(reg_data.word0, phba->sli4_hba.u.if_type2.
9280 			       CTRLregaddr);
9281 			/* flush */
9282 			pci_read_config_word(phba->pcidev,
9283 					     PCI_DEVICE_ID, &devid);
9284 
9285 			port_reset = 1;
9286 			msleep(20);
9287 			goto wait;
9288 		} else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
9289 			rc = -ENODEV;
9290 			goto out;
9291 		}
9292 		break;
9293 
9294 	case LPFC_SLI_INTF_IF_TYPE_1:
9295 	default:
9296 		break;
9297 	}
9298 
9299 out:
9300 	/* Catch the not-ready port failure after a port reset. */
9301 	if (rc) {
9302 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9303 				"3317 HBA not functional: IP Reset Failed "
9304 				"try: echo fw_reset > board_mode\n");
9305 		rc = -ENODEV;
9306 	}
9307 
9308 	return rc;
9309 }
9310 
9311 /**
9312  * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
9313  * @phba: pointer to lpfc hba data structure.
9314  *
9315  * This routine is invoked to set up the PCI device memory space for device
9316  * with SLI-4 interface spec.
9317  *
9318  * Return codes
9319  * 	0 - successful
9320  * 	other values - error
9321  **/
9322 static int
9323 lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
9324 {
9325 	struct pci_dev *pdev;
9326 	unsigned long bar0map_len, bar1map_len, bar2map_len;
9327 	int error = -ENODEV;
9328 	uint32_t if_type;
9329 
9330 	/* Obtain PCI device reference */
9331 	if (!phba->pcidev)
9332 		return error;
9333 	else
9334 		pdev = phba->pcidev;
9335 
9336 	/* Set the device DMA mask size */
9337 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
9338 	 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
9339 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
9340 		 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
9341 			return error;
9342 		}
9343 	}
9344 
9345 	/*
9346 	 * The BARs and register set definitions and offset locations are
9347 	 * dependent on the if_type.
9348 	 */
9349 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
9350 				  &phba->sli4_hba.sli_intf.word0)) {
9351 		return error;
9352 	}
9353 
9354 	/* There is no SLI3 failback for SLI4 devices. */
9355 	if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
9356 	    LPFC_SLI_INTF_VALID) {
9357 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9358 				"2894 SLI_INTF reg contents invalid "
9359 				"sli_intf reg 0x%x\n",
9360 				phba->sli4_hba.sli_intf.word0);
9361 		return error;
9362 	}
9363 
9364 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9365 	/*
9366 	 * Get the bus address of SLI4 device Bar regions and the
9367 	 * number of bytes required by each mapping. The mapping of the
9368 	 * particular PCI BARs regions is dependent on the type of
9369 	 * SLI4 device.
9370 	 */
9371 	if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
9372 		phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
9373 		bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
9374 
9375 		/*
9376 		 * Map SLI4 PCI Config Space Register base to a kernel virtual
9377 		 * addr
9378 		 */
9379 		phba->sli4_hba.conf_regs_memmap_p =
9380 			ioremap(phba->pci_bar0_map, bar0map_len);
9381 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9382 			dev_printk(KERN_ERR, &pdev->dev,
9383 				   "ioremap failed for SLI4 PCI config "
9384 				   "registers.\n");
9385 			goto out;
9386 		}
9387 		phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
9388 		/* Set up BAR0 PCI config space register memory map */
9389 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9390 	} else {
9391 		phba->pci_bar0_map = pci_resource_start(pdev, 1);
9392 		bar0map_len = pci_resource_len(pdev, 1);
9393 		if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9394 			dev_printk(KERN_ERR, &pdev->dev,
9395 			   "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
9396 			goto out;
9397 		}
9398 		phba->sli4_hba.conf_regs_memmap_p =
9399 				ioremap(phba->pci_bar0_map, bar0map_len);
9400 		if (!phba->sli4_hba.conf_regs_memmap_p) {
9401 			dev_printk(KERN_ERR, &pdev->dev,
9402 				"ioremap failed for SLI4 PCI config "
9403 				"registers.\n");
9404 				goto out;
9405 		}
9406 		lpfc_sli4_bar0_register_memmap(phba, if_type);
9407 	}
9408 
9409 	if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
9410 	    (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
9411 		/*
9412 		 * Map SLI4 if type 0 HBA Control Register base to a kernel
9413 		 * virtual address and setup the registers.
9414 		 */
9415 		phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
9416 		bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
9417 		phba->sli4_hba.ctrl_regs_memmap_p =
9418 				ioremap(phba->pci_bar1_map, bar1map_len);
9419 		if (!phba->sli4_hba.ctrl_regs_memmap_p) {
9420 			dev_printk(KERN_ERR, &pdev->dev,
9421 			   "ioremap failed for SLI4 HBA control registers.\n");
9422 			goto out_iounmap_conf;
9423 		}
9424 		phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
9425 		lpfc_sli4_bar1_register_memmap(phba);
9426 	}
9427 
9428 	if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
9429 	    (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
9430 		/*
9431 		 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
9432 		 * virtual address and setup the registers.
9433 		 */
9434 		phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
9435 		bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
9436 		phba->sli4_hba.drbl_regs_memmap_p =
9437 				ioremap(phba->pci_bar2_map, bar2map_len);
9438 		if (!phba->sli4_hba.drbl_regs_memmap_p) {
9439 			dev_printk(KERN_ERR, &pdev->dev,
9440 			   "ioremap failed for SLI4 HBA doorbell registers.\n");
9441 			goto out_iounmap_ctrl;
9442 		}
9443 		phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
9444 		error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
9445 		if (error)
9446 			goto out_iounmap_all;
9447 	}
9448 
9449 	return 0;
9450 
9451 out_iounmap_all:
9452 	iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9453 out_iounmap_ctrl:
9454 	iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9455 out_iounmap_conf:
9456 	iounmap(phba->sli4_hba.conf_regs_memmap_p);
9457 out:
9458 	return error;
9459 }
9460 
9461 /**
9462  * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
9463  * @phba: pointer to lpfc hba data structure.
9464  *
9465  * This routine is invoked to unset the PCI device memory space for device
9466  * with SLI-4 interface spec.
9467  **/
9468 static void
9469 lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
9470 {
9471 	uint32_t if_type;
9472 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9473 
9474 	switch (if_type) {
9475 	case LPFC_SLI_INTF_IF_TYPE_0:
9476 		iounmap(phba->sli4_hba.drbl_regs_memmap_p);
9477 		iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
9478 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9479 		break;
9480 	case LPFC_SLI_INTF_IF_TYPE_2:
9481 		iounmap(phba->sli4_hba.conf_regs_memmap_p);
9482 		break;
9483 	case LPFC_SLI_INTF_IF_TYPE_1:
9484 	default:
9485 		dev_printk(KERN_ERR, &phba->pcidev->dev,
9486 			   "FATAL - unsupported SLI4 interface type - %d\n",
9487 			   if_type);
9488 		break;
9489 	}
9490 }
9491 
9492 /**
9493  * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
9494  * @phba: pointer to lpfc hba data structure.
9495  *
9496  * This routine is invoked to enable the MSI-X interrupt vectors to device
9497  * with SLI-3 interface specs.
9498  *
9499  * Return codes
9500  *   0 - successful
9501  *   other values - error
9502  **/
9503 static int
9504 lpfc_sli_enable_msix(struct lpfc_hba *phba)
9505 {
9506 	int rc;
9507 	LPFC_MBOXQ_t *pmb;
9508 
9509 	/* Set up MSI-X multi-message vectors */
9510 	rc = pci_alloc_irq_vectors(phba->pcidev,
9511 			LPFC_MSIX_VECTORS, LPFC_MSIX_VECTORS, PCI_IRQ_MSIX);
9512 	if (rc < 0) {
9513 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9514 				"0420 PCI enable MSI-X failed (%d)\n", rc);
9515 		goto vec_fail_out;
9516 	}
9517 
9518 	/*
9519 	 * Assign MSI-X vectors to interrupt handlers
9520 	 */
9521 
9522 	/* vector-0 is associated to slow-path handler */
9523 	rc = request_irq(pci_irq_vector(phba->pcidev, 0),
9524 			 &lpfc_sli_sp_intr_handler, 0,
9525 			 LPFC_SP_DRIVER_HANDLER_NAME, phba);
9526 	if (rc) {
9527 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9528 				"0421 MSI-X slow-path request_irq failed "
9529 				"(%d)\n", rc);
9530 		goto msi_fail_out;
9531 	}
9532 
9533 	/* vector-1 is associated to fast-path handler */
9534 	rc = request_irq(pci_irq_vector(phba->pcidev, 1),
9535 			 &lpfc_sli_fp_intr_handler, 0,
9536 			 LPFC_FP_DRIVER_HANDLER_NAME, phba);
9537 
9538 	if (rc) {
9539 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9540 				"0429 MSI-X fast-path request_irq failed "
9541 				"(%d)\n", rc);
9542 		goto irq_fail_out;
9543 	}
9544 
9545 	/*
9546 	 * Configure HBA MSI-X attention conditions to messages
9547 	 */
9548 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9549 
9550 	if (!pmb) {
9551 		rc = -ENOMEM;
9552 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9553 				"0474 Unable to allocate memory for issuing "
9554 				"MBOX_CONFIG_MSI command\n");
9555 		goto mem_fail_out;
9556 	}
9557 	rc = lpfc_config_msi(phba, pmb);
9558 	if (rc)
9559 		goto mbx_fail_out;
9560 	rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
9561 	if (rc != MBX_SUCCESS) {
9562 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
9563 				"0351 Config MSI mailbox command failed, "
9564 				"mbxCmd x%x, mbxStatus x%x\n",
9565 				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
9566 		goto mbx_fail_out;
9567 	}
9568 
9569 	/* Free memory allocated for mailbox command */
9570 	mempool_free(pmb, phba->mbox_mem_pool);
9571 	return rc;
9572 
9573 mbx_fail_out:
9574 	/* Free memory allocated for mailbox command */
9575 	mempool_free(pmb, phba->mbox_mem_pool);
9576 
9577 mem_fail_out:
9578 	/* free the irq already requested */
9579 	free_irq(pci_irq_vector(phba->pcidev, 1), phba);
9580 
9581 irq_fail_out:
9582 	/* free the irq already requested */
9583 	free_irq(pci_irq_vector(phba->pcidev, 0), phba);
9584 
9585 msi_fail_out:
9586 	/* Unconfigure MSI-X capability structure */
9587 	pci_free_irq_vectors(phba->pcidev);
9588 
9589 vec_fail_out:
9590 	return rc;
9591 }
9592 
9593 /**
9594  * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
9595  * @phba: pointer to lpfc hba data structure.
9596  *
9597  * This routine is invoked to enable the MSI interrupt mode to device with
9598  * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
9599  * enable the MSI vector. The device driver is responsible for calling the
9600  * request_irq() to register MSI vector with a interrupt the handler, which
9601  * is done in this function.
9602  *
9603  * Return codes
9604  * 	0 - successful
9605  * 	other values - error
9606  */
9607 static int
9608 lpfc_sli_enable_msi(struct lpfc_hba *phba)
9609 {
9610 	int rc;
9611 
9612 	rc = pci_enable_msi(phba->pcidev);
9613 	if (!rc)
9614 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9615 				"0462 PCI enable MSI mode success.\n");
9616 	else {
9617 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9618 				"0471 PCI enable MSI mode failed (%d)\n", rc);
9619 		return rc;
9620 	}
9621 
9622 	rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9623 			 0, LPFC_DRIVER_NAME, phba);
9624 	if (rc) {
9625 		pci_disable_msi(phba->pcidev);
9626 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9627 				"0478 MSI request_irq failed (%d)\n", rc);
9628 	}
9629 	return rc;
9630 }
9631 
9632 /**
9633  * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
9634  * @phba: pointer to lpfc hba data structure.
9635  *
9636  * This routine is invoked to enable device interrupt and associate driver's
9637  * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
9638  * spec. Depends on the interrupt mode configured to the driver, the driver
9639  * will try to fallback from the configured interrupt mode to an interrupt
9640  * mode which is supported by the platform, kernel, and device in the order
9641  * of:
9642  * MSI-X -> MSI -> IRQ.
9643  *
9644  * Return codes
9645  *   0 - successful
9646  *   other values - error
9647  **/
9648 static uint32_t
9649 lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9650 {
9651 	uint32_t intr_mode = LPFC_INTR_ERROR;
9652 	int retval;
9653 
9654 	if (cfg_mode == 2) {
9655 		/* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
9656 		retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
9657 		if (!retval) {
9658 			/* Now, try to enable MSI-X interrupt mode */
9659 			retval = lpfc_sli_enable_msix(phba);
9660 			if (!retval) {
9661 				/* Indicate initialization to MSI-X mode */
9662 				phba->intr_type = MSIX;
9663 				intr_mode = 2;
9664 			}
9665 		}
9666 	}
9667 
9668 	/* Fallback to MSI if MSI-X initialization failed */
9669 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
9670 		retval = lpfc_sli_enable_msi(phba);
9671 		if (!retval) {
9672 			/* Indicate initialization to MSI mode */
9673 			phba->intr_type = MSI;
9674 			intr_mode = 1;
9675 		}
9676 	}
9677 
9678 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
9679 	if (phba->intr_type == NONE) {
9680 		retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
9681 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9682 		if (!retval) {
9683 			/* Indicate initialization to INTx mode */
9684 			phba->intr_type = INTx;
9685 			intr_mode = 0;
9686 		}
9687 	}
9688 	return intr_mode;
9689 }
9690 
9691 /**
9692  * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
9693  * @phba: pointer to lpfc hba data structure.
9694  *
9695  * This routine is invoked to disable device interrupt and disassociate the
9696  * driver's interrupt handler(s) from interrupt vector(s) to device with
9697  * SLI-3 interface spec. Depending on the interrupt mode, the driver will
9698  * release the interrupt vector(s) for the message signaled interrupt.
9699  **/
9700 static void
9701 lpfc_sli_disable_intr(struct lpfc_hba *phba)
9702 {
9703 	int nr_irqs, i;
9704 
9705 	if (phba->intr_type == MSIX)
9706 		nr_irqs = LPFC_MSIX_VECTORS;
9707 	else
9708 		nr_irqs = 1;
9709 
9710 	for (i = 0; i < nr_irqs; i++)
9711 		free_irq(pci_irq_vector(phba->pcidev, i), phba);
9712 	pci_free_irq_vectors(phba->pcidev);
9713 
9714 	/* Reset interrupt management states */
9715 	phba->intr_type = NONE;
9716 	phba->sli.slistat.sli_intr = 0;
9717 }
9718 
9719 /**
9720  * lpfc_cpu_affinity_check - Check vector CPU affinity mappings
9721  * @phba: pointer to lpfc hba data structure.
9722  * @vectors: number of msix vectors allocated.
9723  *
9724  * The routine will figure out the CPU affinity assignment for every
9725  * MSI-X vector allocated for the HBA.  The hba_eq_hdl will be updated
9726  * with a pointer to the CPU mask that defines ALL the CPUs this vector
9727  * can be associated with. If the vector can be unquely associated with
9728  * a single CPU, that CPU will be recorded in hba_eq_hdl[index].cpu.
9729  * In addition, the CPU to IO channel mapping will be calculated
9730  * and the phba->sli4_hba.cpu_map array will reflect this.
9731  */
9732 static void
9733 lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
9734 {
9735 	struct lpfc_vector_map_info *cpup;
9736 	int index = 0;
9737 	int vec = 0;
9738 	int cpu;
9739 #ifdef CONFIG_X86
9740 	struct cpuinfo_x86 *cpuinfo;
9741 #endif
9742 
9743 	/* Init cpu_map array */
9744 	memset(phba->sli4_hba.cpu_map, 0xff,
9745 	       (sizeof(struct lpfc_vector_map_info) *
9746 	       phba->sli4_hba.num_present_cpu));
9747 
9748 	/* Update CPU map with physical id and core id of each CPU */
9749 	cpup = phba->sli4_hba.cpu_map;
9750 	for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
9751 #ifdef CONFIG_X86
9752 		cpuinfo = &cpu_data(cpu);
9753 		cpup->phys_id = cpuinfo->phys_proc_id;
9754 		cpup->core_id = cpuinfo->cpu_core_id;
9755 #else
9756 		/* No distinction between CPUs for other platforms */
9757 		cpup->phys_id = 0;
9758 		cpup->core_id = 0;
9759 #endif
9760 		cpup->channel_id = index;  /* For now round robin */
9761 		cpup->irq = pci_irq_vector(phba->pcidev, vec);
9762 		vec++;
9763 		if (vec >= vectors)
9764 			vec = 0;
9765 		index++;
9766 		if (index >= phba->cfg_fcp_io_channel)
9767 			index = 0;
9768 		cpup++;
9769 	}
9770 }
9771 
9772 
9773 /**
9774  * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
9775  * @phba: pointer to lpfc hba data structure.
9776  *
9777  * This routine is invoked to enable the MSI-X interrupt vectors to device
9778  * with SLI-4 interface spec.
9779  *
9780  * Return codes
9781  * 0 - successful
9782  * other values - error
9783  **/
9784 static int
9785 lpfc_sli4_enable_msix(struct lpfc_hba *phba)
9786 {
9787 	int vectors, rc, index;
9788 	char *name;
9789 
9790 	/* Set up MSI-X multi-message vectors */
9791 	vectors = phba->io_channel_irqs;
9792 	if (phba->cfg_fof)
9793 		vectors++;
9794 
9795 	rc = pci_alloc_irq_vectors(phba->pcidev,
9796 				(phba->nvmet_support) ? 1 : 2,
9797 				vectors, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
9798 	if (rc < 0) {
9799 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9800 				"0484 PCI enable MSI-X failed (%d)\n", rc);
9801 		goto vec_fail_out;
9802 	}
9803 	vectors = rc;
9804 
9805 	/* Assign MSI-X vectors to interrupt handlers */
9806 	for (index = 0; index < vectors; index++) {
9807 		name = phba->sli4_hba.hba_eq_hdl[index].handler_name;
9808 		memset(name, 0, LPFC_SLI4_HANDLER_NAME_SZ);
9809 		snprintf(name, LPFC_SLI4_HANDLER_NAME_SZ,
9810 			 LPFC_DRIVER_HANDLER_NAME"%d", index);
9811 
9812 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
9813 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9814 		atomic_set(&phba->sli4_hba.hba_eq_hdl[index].hba_eq_in_use, 1);
9815 		if (phba->cfg_fof && (index == (vectors - 1)))
9816 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
9817 				 &lpfc_sli4_fof_intr_handler, 0,
9818 				 name,
9819 				 &phba->sli4_hba.hba_eq_hdl[index]);
9820 		else
9821 			rc = request_irq(pci_irq_vector(phba->pcidev, index),
9822 				 &lpfc_sli4_hba_intr_handler, 0,
9823 				 name,
9824 				 &phba->sli4_hba.hba_eq_hdl[index]);
9825 		if (rc) {
9826 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9827 					"0486 MSI-X fast-path (%d) "
9828 					"request_irq failed (%d)\n", index, rc);
9829 			goto cfg_fail_out;
9830 		}
9831 	}
9832 
9833 	if (phba->cfg_fof)
9834 		vectors--;
9835 
9836 	if (vectors != phba->io_channel_irqs) {
9837 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9838 				"3238 Reducing IO channels to match number of "
9839 				"MSI-X vectors, requested %d got %d\n",
9840 				phba->io_channel_irqs, vectors);
9841 		if (phba->cfg_fcp_io_channel > vectors)
9842 			phba->cfg_fcp_io_channel = vectors;
9843 		if (phba->cfg_nvme_io_channel > vectors)
9844 			phba->cfg_nvme_io_channel = vectors;
9845 		if (phba->cfg_fcp_io_channel > phba->cfg_nvme_io_channel)
9846 			phba->io_channel_irqs = phba->cfg_fcp_io_channel;
9847 		else
9848 			phba->io_channel_irqs = phba->cfg_nvme_io_channel;
9849 	}
9850 	lpfc_cpu_affinity_check(phba, vectors);
9851 
9852 	return rc;
9853 
9854 cfg_fail_out:
9855 	/* free the irq already requested */
9856 	for (--index; index >= 0; index--)
9857 		free_irq(pci_irq_vector(phba->pcidev, index),
9858 				&phba->sli4_hba.hba_eq_hdl[index]);
9859 
9860 	/* Unconfigure MSI-X capability structure */
9861 	pci_free_irq_vectors(phba->pcidev);
9862 
9863 vec_fail_out:
9864 	return rc;
9865 }
9866 
9867 /**
9868  * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
9869  * @phba: pointer to lpfc hba data structure.
9870  *
9871  * This routine is invoked to enable the MSI interrupt mode to device with
9872  * SLI-4 interface spec. The kernel function pci_enable_msi() is called
9873  * to enable the MSI vector. The device driver is responsible for calling
9874  * the request_irq() to register MSI vector with a interrupt the handler,
9875  * which is done in this function.
9876  *
9877  * Return codes
9878  * 	0 - successful
9879  * 	other values - error
9880  **/
9881 static int
9882 lpfc_sli4_enable_msi(struct lpfc_hba *phba)
9883 {
9884 	int rc, index;
9885 
9886 	rc = pci_enable_msi(phba->pcidev);
9887 	if (!rc)
9888 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9889 				"0487 PCI enable MSI mode success.\n");
9890 	else {
9891 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9892 				"0488 PCI enable MSI mode failed (%d)\n", rc);
9893 		return rc;
9894 	}
9895 
9896 	rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9897 			 0, LPFC_DRIVER_NAME, phba);
9898 	if (rc) {
9899 		pci_disable_msi(phba->pcidev);
9900 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9901 				"0490 MSI request_irq failed (%d)\n", rc);
9902 		return rc;
9903 	}
9904 
9905 	for (index = 0; index < phba->io_channel_irqs; index++) {
9906 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
9907 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9908 	}
9909 
9910 	if (phba->cfg_fof) {
9911 		phba->sli4_hba.hba_eq_hdl[index].idx = index;
9912 		phba->sli4_hba.hba_eq_hdl[index].phba = phba;
9913 	}
9914 	return 0;
9915 }
9916 
9917 /**
9918  * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
9919  * @phba: pointer to lpfc hba data structure.
9920  *
9921  * This routine is invoked to enable device interrupt and associate driver's
9922  * interrupt handler(s) to interrupt vector(s) to device with SLI-4
9923  * interface spec. Depends on the interrupt mode configured to the driver,
9924  * the driver will try to fallback from the configured interrupt mode to an
9925  * interrupt mode which is supported by the platform, kernel, and device in
9926  * the order of:
9927  * MSI-X -> MSI -> IRQ.
9928  *
9929  * Return codes
9930  * 	0 - successful
9931  * 	other values - error
9932  **/
9933 static uint32_t
9934 lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
9935 {
9936 	uint32_t intr_mode = LPFC_INTR_ERROR;
9937 	int retval, idx;
9938 
9939 	if (cfg_mode == 2) {
9940 		/* Preparation before conf_msi mbox cmd */
9941 		retval = 0;
9942 		if (!retval) {
9943 			/* Now, try to enable MSI-X interrupt mode */
9944 			retval = lpfc_sli4_enable_msix(phba);
9945 			if (!retval) {
9946 				/* Indicate initialization to MSI-X mode */
9947 				phba->intr_type = MSIX;
9948 				intr_mode = 2;
9949 			}
9950 		}
9951 	}
9952 
9953 	/* Fallback to MSI if MSI-X initialization failed */
9954 	if (cfg_mode >= 1 && phba->intr_type == NONE) {
9955 		retval = lpfc_sli4_enable_msi(phba);
9956 		if (!retval) {
9957 			/* Indicate initialization to MSI mode */
9958 			phba->intr_type = MSI;
9959 			intr_mode = 1;
9960 		}
9961 	}
9962 
9963 	/* Fallback to INTx if both MSI-X/MSI initalization failed */
9964 	if (phba->intr_type == NONE) {
9965 		retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9966 				     IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9967 		if (!retval) {
9968 			struct lpfc_hba_eq_hdl *eqhdl;
9969 
9970 			/* Indicate initialization to INTx mode */
9971 			phba->intr_type = INTx;
9972 			intr_mode = 0;
9973 
9974 			for (idx = 0; idx < phba->io_channel_irqs; idx++) {
9975 				eqhdl = &phba->sli4_hba.hba_eq_hdl[idx];
9976 				eqhdl->idx = idx;
9977 				eqhdl->phba = phba;
9978 				atomic_set(&eqhdl->hba_eq_in_use, 1);
9979 			}
9980 			if (phba->cfg_fof) {
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 		}
9987 	}
9988 	return intr_mode;
9989 }
9990 
9991 /**
9992  * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9993  * @phba: pointer to lpfc hba data structure.
9994  *
9995  * This routine is invoked to disable device interrupt and disassociate
9996  * the driver's interrupt handler(s) from interrupt vector(s) to device
9997  * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9998  * will release the interrupt vector(s) for the message signaled interrupt.
9999  **/
10000 static void
10001 lpfc_sli4_disable_intr(struct lpfc_hba *phba)
10002 {
10003 	/* Disable the currently initialized interrupt mode */
10004 	if (phba->intr_type == MSIX) {
10005 		int index;
10006 
10007 		/* Free up MSI-X multi-message vectors */
10008 		for (index = 0; index < phba->io_channel_irqs; index++)
10009 			free_irq(pci_irq_vector(phba->pcidev, index),
10010 					&phba->sli4_hba.hba_eq_hdl[index]);
10011 
10012 		if (phba->cfg_fof)
10013 			free_irq(pci_irq_vector(phba->pcidev, index),
10014 					&phba->sli4_hba.hba_eq_hdl[index]);
10015 	} else {
10016 		free_irq(phba->pcidev->irq, phba);
10017 	}
10018 
10019 	pci_free_irq_vectors(phba->pcidev);
10020 
10021 	/* Reset interrupt management states */
10022 	phba->intr_type = NONE;
10023 	phba->sli.slistat.sli_intr = 0;
10024 }
10025 
10026 /**
10027  * lpfc_unset_hba - Unset SLI3 hba device initialization
10028  * @phba: pointer to lpfc hba data structure.
10029  *
10030  * This routine is invoked to unset the HBA device initialization steps to
10031  * a device with SLI-3 interface spec.
10032  **/
10033 static void
10034 lpfc_unset_hba(struct lpfc_hba *phba)
10035 {
10036 	struct lpfc_vport *vport = phba->pport;
10037 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10038 
10039 	spin_lock_irq(shost->host_lock);
10040 	vport->load_flag |= FC_UNLOADING;
10041 	spin_unlock_irq(shost->host_lock);
10042 
10043 	kfree(phba->vpi_bmask);
10044 	kfree(phba->vpi_ids);
10045 
10046 	lpfc_stop_hba_timers(phba);
10047 
10048 	phba->pport->work_port_events = 0;
10049 
10050 	lpfc_sli_hba_down(phba);
10051 
10052 	lpfc_sli_brdrestart(phba);
10053 
10054 	lpfc_sli_disable_intr(phba);
10055 
10056 	return;
10057 }
10058 
10059 /**
10060  * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
10061  * @phba: Pointer to HBA context object.
10062  *
10063  * This function is called in the SLI4 code path to wait for completion
10064  * of device's XRIs exchange busy. It will check the XRI exchange busy
10065  * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
10066  * that, it will check the XRI exchange busy on outstanding FCP and ELS
10067  * I/Os every 30 seconds, log error message, and wait forever. Only when
10068  * all XRI exchange busy complete, the driver unload shall proceed with
10069  * invoking the function reset ioctl mailbox command to the CNA and the
10070  * the rest of the driver unload resource release.
10071  **/
10072 static void
10073 lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
10074 {
10075 	int wait_time = 0;
10076 	int nvme_xri_cmpl = 1;
10077 	int nvmet_xri_cmpl = 1;
10078 	int fcp_xri_cmpl = 1;
10079 	int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10080 
10081 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10082 		fcp_xri_cmpl =
10083 			list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10084 	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10085 		nvme_xri_cmpl =
10086 			list_empty(&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10087 		nvmet_xri_cmpl =
10088 			list_empty(&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10089 	}
10090 
10091 	while (!fcp_xri_cmpl || !els_xri_cmpl || !nvme_xri_cmpl ||
10092 	       !nvmet_xri_cmpl) {
10093 		if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
10094 			if (!nvme_xri_cmpl)
10095 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10096 						"6100 NVME XRI exchange busy "
10097 						"wait time: %d seconds.\n",
10098 						wait_time/1000);
10099 			if (!fcp_xri_cmpl)
10100 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10101 						"2877 FCP XRI exchange busy "
10102 						"wait time: %d seconds.\n",
10103 						wait_time/1000);
10104 			if (!els_xri_cmpl)
10105 				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10106 						"2878 ELS XRI exchange busy "
10107 						"wait time: %d seconds.\n",
10108 						wait_time/1000);
10109 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
10110 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
10111 		} else {
10112 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
10113 			wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
10114 		}
10115 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10116 			nvme_xri_cmpl = list_empty(
10117 				&phba->sli4_hba.lpfc_abts_nvme_buf_list);
10118 			nvmet_xri_cmpl = list_empty(
10119 				&phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
10120 		}
10121 
10122 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
10123 			fcp_xri_cmpl = list_empty(
10124 				&phba->sli4_hba.lpfc_abts_scsi_buf_list);
10125 
10126 		els_xri_cmpl =
10127 			list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
10128 
10129 	}
10130 }
10131 
10132 /**
10133  * lpfc_sli4_hba_unset - Unset the fcoe hba
10134  * @phba: Pointer to HBA context object.
10135  *
10136  * This function is called in the SLI4 code path to reset the HBA's FCoE
10137  * function. The caller is not required to hold any lock. This routine
10138  * issues PCI function reset mailbox command to reset the FCoE function.
10139  * At the end of the function, it calls lpfc_hba_down_post function to
10140  * free any pending commands.
10141  **/
10142 static void
10143 lpfc_sli4_hba_unset(struct lpfc_hba *phba)
10144 {
10145 	int wait_cnt = 0;
10146 	LPFC_MBOXQ_t *mboxq;
10147 	struct pci_dev *pdev = phba->pcidev;
10148 
10149 	lpfc_stop_hba_timers(phba);
10150 	phba->sli4_hba.intr_enable = 0;
10151 
10152 	/*
10153 	 * Gracefully wait out the potential current outstanding asynchronous
10154 	 * mailbox command.
10155 	 */
10156 
10157 	/* First, block any pending async mailbox command from posted */
10158 	spin_lock_irq(&phba->hbalock);
10159 	phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10160 	spin_unlock_irq(&phba->hbalock);
10161 	/* Now, trying to wait it out if we can */
10162 	while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10163 		msleep(10);
10164 		if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
10165 			break;
10166 	}
10167 	/* Forcefully release the outstanding mailbox command if timed out */
10168 	if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10169 		spin_lock_irq(&phba->hbalock);
10170 		mboxq = phba->sli.mbox_active;
10171 		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10172 		__lpfc_mbox_cmpl_put(phba, mboxq);
10173 		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10174 		phba->sli.mbox_active = NULL;
10175 		spin_unlock_irq(&phba->hbalock);
10176 	}
10177 
10178 	/* Abort all iocbs associated with the hba */
10179 	lpfc_sli_hba_iocb_abort(phba);
10180 
10181 	/* Wait for completion of device XRI exchange busy */
10182 	lpfc_sli4_xri_exchange_busy_wait(phba);
10183 
10184 	/* Disable PCI subsystem interrupt */
10185 	lpfc_sli4_disable_intr(phba);
10186 
10187 	/* Disable SR-IOV if enabled */
10188 	if (phba->cfg_sriov_nr_virtfn)
10189 		pci_disable_sriov(pdev);
10190 
10191 	/* Stop kthread signal shall trigger work_done one more time */
10192 	kthread_stop(phba->worker_thread);
10193 
10194 	/* Unset the queues shared with the hardware then release all
10195 	 * allocated resources.
10196 	 */
10197 	lpfc_sli4_queue_unset(phba);
10198 	lpfc_sli4_queue_destroy(phba);
10199 
10200 	/* Reset SLI4 HBA FCoE function */
10201 	lpfc_pci_function_reset(phba);
10202 
10203 	/* Stop the SLI4 device port */
10204 	phba->pport->work_port_events = 0;
10205 }
10206 
10207  /**
10208  * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
10209  * @phba: Pointer to HBA context object.
10210  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10211  *
10212  * This function is called in the SLI4 code path to read the port's
10213  * sli4 capabilities.
10214  *
10215  * This function may be be called from any context that can block-wait
10216  * for the completion.  The expectation is that this routine is called
10217  * typically from probe_one or from the online routine.
10218  **/
10219 int
10220 lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10221 {
10222 	int rc;
10223 	struct lpfc_mqe *mqe;
10224 	struct lpfc_pc_sli4_params *sli4_params;
10225 	uint32_t mbox_tmo;
10226 
10227 	rc = 0;
10228 	mqe = &mboxq->u.mqe;
10229 
10230 	/* Read the port's SLI4 Parameters port capabilities */
10231 	lpfc_pc_sli4_params(mboxq);
10232 	if (!phba->sli4_hba.intr_enable)
10233 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10234 	else {
10235 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10236 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10237 	}
10238 
10239 	if (unlikely(rc))
10240 		return 1;
10241 
10242 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10243 	sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
10244 	sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
10245 	sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
10246 	sli4_params->featurelevel_1 = bf_get(featurelevel_1,
10247 					     &mqe->un.sli4_params);
10248 	sli4_params->featurelevel_2 = bf_get(featurelevel_2,
10249 					     &mqe->un.sli4_params);
10250 	sli4_params->proto_types = mqe->un.sli4_params.word3;
10251 	sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
10252 	sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
10253 	sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
10254 	sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
10255 	sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
10256 	sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
10257 	sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
10258 	sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
10259 	sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
10260 	sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
10261 	sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
10262 	sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
10263 	sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
10264 	sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
10265 	sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
10266 	sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
10267 	sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
10268 	sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
10269 	sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
10270 	sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
10271 
10272 	/* Make sure that sge_supp_len can be handled by the driver */
10273 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10274 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10275 
10276 	return rc;
10277 }
10278 
10279 /**
10280  * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
10281  * @phba: Pointer to HBA context object.
10282  * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
10283  *
10284  * This function is called in the SLI4 code path to read the port's
10285  * sli4 capabilities.
10286  *
10287  * This function may be be called from any context that can block-wait
10288  * for the completion.  The expectation is that this routine is called
10289  * typically from probe_one or from the online routine.
10290  **/
10291 int
10292 lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
10293 {
10294 	int rc;
10295 	struct lpfc_mqe *mqe = &mboxq->u.mqe;
10296 	struct lpfc_pc_sli4_params *sli4_params;
10297 	uint32_t mbox_tmo;
10298 	int length;
10299 	struct lpfc_sli4_parameters *mbx_sli4_parameters;
10300 
10301 	/*
10302 	 * By default, the driver assumes the SLI4 port requires RPI
10303 	 * header postings.  The SLI4_PARAM response will correct this
10304 	 * assumption.
10305 	 */
10306 	phba->sli4_hba.rpi_hdrs_in_use = 1;
10307 
10308 	/* Read the port's SLI4 Config Parameters */
10309 	length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
10310 		  sizeof(struct lpfc_sli4_cfg_mhdr));
10311 	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
10312 			 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
10313 			 length, LPFC_SLI4_MBX_EMBED);
10314 	if (!phba->sli4_hba.intr_enable)
10315 		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
10316 	else {
10317 		mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
10318 		rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
10319 	}
10320 	if (unlikely(rc))
10321 		return rc;
10322 	sli4_params = &phba->sli4_hba.pc_sli4_params;
10323 	mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
10324 	sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
10325 	sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
10326 	sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
10327 	sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
10328 					     mbx_sli4_parameters);
10329 	sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
10330 					     mbx_sli4_parameters);
10331 	if (bf_get(cfg_phwq, mbx_sli4_parameters))
10332 		phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
10333 	else
10334 		phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
10335 	sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
10336 	sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
10337 	sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
10338 	sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
10339 	sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
10340 	sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
10341 	sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
10342 	sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
10343 	sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
10344 					    mbx_sli4_parameters);
10345 	sli4_params->wqpcnt = bf_get(cfg_wqpcnt, mbx_sli4_parameters);
10346 	sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
10347 					   mbx_sli4_parameters);
10348 	phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
10349 	phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
10350 	phba->nvme_support = (bf_get(cfg_nvme, mbx_sli4_parameters) &&
10351 			      bf_get(cfg_xib, mbx_sli4_parameters));
10352 
10353 	if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP) ||
10354 	    !phba->nvme_support) {
10355 		phba->nvme_support = 0;
10356 		phba->nvmet_support = 0;
10357 		phba->cfg_nvmet_mrq = 0;
10358 		phba->cfg_nvme_io_channel = 0;
10359 		phba->io_channel_irqs = phba->cfg_fcp_io_channel;
10360 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_NVME,
10361 				"6101 Disabling NVME support: "
10362 				"Not supported by firmware: %d %d\n",
10363 				bf_get(cfg_nvme, mbx_sli4_parameters),
10364 				bf_get(cfg_xib, mbx_sli4_parameters));
10365 
10366 		/* If firmware doesn't support NVME, just use SCSI support */
10367 		if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
10368 			return -ENODEV;
10369 		phba->cfg_enable_fc4_type = LPFC_ENABLE_FCP;
10370 	}
10371 
10372 	if (bf_get(cfg_xib, mbx_sli4_parameters) && phba->cfg_suppress_rsp)
10373 		phba->sli.sli_flag |= LPFC_SLI_SUPPRESS_RSP;
10374 
10375 	if (bf_get(cfg_eqdr, mbx_sli4_parameters))
10376 		phba->sli.sli_flag |= LPFC_SLI_USE_EQDR;
10377 
10378 	/* Make sure that sge_supp_len can be handled by the driver */
10379 	if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
10380 		sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
10381 
10382 	/*
10383 	 * Issue IOs with CDB embedded in WQE to minimized the number
10384 	 * of DMAs the firmware has to do. Setting this to 1 also forces
10385 	 * the driver to use 128 bytes WQEs for FCP IOs.
10386 	 */
10387 	if (bf_get(cfg_ext_embed_cb, mbx_sli4_parameters))
10388 		phba->fcp_embed_io = 1;
10389 	else
10390 		phba->fcp_embed_io = 0;
10391 
10392 	/*
10393 	 * Check if the SLI port supports MDS Diagnostics
10394 	 */
10395 	if (bf_get(cfg_mds_diags, mbx_sli4_parameters))
10396 		phba->mds_diags_support = 1;
10397 	else
10398 		phba->mds_diags_support = 0;
10399 	return 0;
10400 }
10401 
10402 /**
10403  * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
10404  * @pdev: pointer to PCI device
10405  * @pid: pointer to PCI device identifier
10406  *
10407  * This routine is to be called to attach a device with SLI-3 interface spec
10408  * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10409  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10410  * information of the device and driver to see if the driver state that it can
10411  * support this kind of device. If the match is successful, the driver core
10412  * invokes this routine. If this routine determines it can claim the HBA, it
10413  * does all the initialization that it needs to do to handle the HBA properly.
10414  *
10415  * Return code
10416  * 	0 - driver can claim the device
10417  * 	negative value - driver can not claim the device
10418  **/
10419 static int
10420 lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
10421 {
10422 	struct lpfc_hba   *phba;
10423 	struct lpfc_vport *vport = NULL;
10424 	struct Scsi_Host  *shost = NULL;
10425 	int error;
10426 	uint32_t cfg_mode, intr_mode;
10427 
10428 	/* Allocate memory for HBA structure */
10429 	phba = lpfc_hba_alloc(pdev);
10430 	if (!phba)
10431 		return -ENOMEM;
10432 
10433 	/* Perform generic PCI device enabling operation */
10434 	error = lpfc_enable_pci_dev(phba);
10435 	if (error)
10436 		goto out_free_phba;
10437 
10438 	/* Set up SLI API function jump table for PCI-device group-0 HBAs */
10439 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
10440 	if (error)
10441 		goto out_disable_pci_dev;
10442 
10443 	/* Set up SLI-3 specific device PCI memory space */
10444 	error = lpfc_sli_pci_mem_setup(phba);
10445 	if (error) {
10446 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10447 				"1402 Failed to set up pci memory space.\n");
10448 		goto out_disable_pci_dev;
10449 	}
10450 
10451 	/* Set up SLI-3 specific device driver resources */
10452 	error = lpfc_sli_driver_resource_setup(phba);
10453 	if (error) {
10454 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10455 				"1404 Failed to set up driver resource.\n");
10456 		goto out_unset_pci_mem_s3;
10457 	}
10458 
10459 	/* Initialize and populate the iocb list per host */
10460 
10461 	error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
10462 	if (error) {
10463 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10464 				"1405 Failed to initialize iocb list.\n");
10465 		goto out_unset_driver_resource_s3;
10466 	}
10467 
10468 	/* Set up common device driver resources */
10469 	error = lpfc_setup_driver_resource_phase2(phba);
10470 	if (error) {
10471 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10472 				"1406 Failed to set up driver resource.\n");
10473 		goto out_free_iocb_list;
10474 	}
10475 
10476 	/* Get the default values for Model Name and Description */
10477 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10478 
10479 	/* Create SCSI host to the physical port */
10480 	error = lpfc_create_shost(phba);
10481 	if (error) {
10482 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10483 				"1407 Failed to create scsi host.\n");
10484 		goto out_unset_driver_resource;
10485 	}
10486 
10487 	/* Configure sysfs attributes */
10488 	vport = phba->pport;
10489 	error = lpfc_alloc_sysfs_attr(vport);
10490 	if (error) {
10491 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10492 				"1476 Failed to allocate sysfs attr\n");
10493 		goto out_destroy_shost;
10494 	}
10495 
10496 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
10497 	/* Now, trying to enable interrupt and bring up the device */
10498 	cfg_mode = phba->cfg_use_msi;
10499 	while (true) {
10500 		/* Put device to a known state before enabling interrupt */
10501 		lpfc_stop_port(phba);
10502 		/* Configure and enable interrupt */
10503 		intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
10504 		if (intr_mode == LPFC_INTR_ERROR) {
10505 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10506 					"0431 Failed to enable interrupt.\n");
10507 			error = -ENODEV;
10508 			goto out_free_sysfs_attr;
10509 		}
10510 		/* SLI-3 HBA setup */
10511 		if (lpfc_sli_hba_setup(phba)) {
10512 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10513 					"1477 Failed to set up hba\n");
10514 			error = -ENODEV;
10515 			goto out_remove_device;
10516 		}
10517 
10518 		/* Wait 50ms for the interrupts of previous mailbox commands */
10519 		msleep(50);
10520 		/* Check active interrupts on message signaled interrupts */
10521 		if (intr_mode == 0 ||
10522 		    phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
10523 			/* Log the current active interrupt mode */
10524 			phba->intr_mode = intr_mode;
10525 			lpfc_log_intr_mode(phba, intr_mode);
10526 			break;
10527 		} else {
10528 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10529 					"0447 Configure interrupt mode (%d) "
10530 					"failed active interrupt test.\n",
10531 					intr_mode);
10532 			/* Disable the current interrupt mode */
10533 			lpfc_sli_disable_intr(phba);
10534 			/* Try next level of interrupt mode */
10535 			cfg_mode = --intr_mode;
10536 		}
10537 	}
10538 
10539 	/* Perform post initialization setup */
10540 	lpfc_post_init_setup(phba);
10541 
10542 	/* Check if there are static vports to be created. */
10543 	lpfc_create_static_vport(phba);
10544 
10545 	return 0;
10546 
10547 out_remove_device:
10548 	lpfc_unset_hba(phba);
10549 out_free_sysfs_attr:
10550 	lpfc_free_sysfs_attr(vport);
10551 out_destroy_shost:
10552 	lpfc_destroy_shost(phba);
10553 out_unset_driver_resource:
10554 	lpfc_unset_driver_resource_phase2(phba);
10555 out_free_iocb_list:
10556 	lpfc_free_iocb_list(phba);
10557 out_unset_driver_resource_s3:
10558 	lpfc_sli_driver_resource_unset(phba);
10559 out_unset_pci_mem_s3:
10560 	lpfc_sli_pci_mem_unset(phba);
10561 out_disable_pci_dev:
10562 	lpfc_disable_pci_dev(phba);
10563 	if (shost)
10564 		scsi_host_put(shost);
10565 out_free_phba:
10566 	lpfc_hba_free(phba);
10567 	return error;
10568 }
10569 
10570 /**
10571  * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
10572  * @pdev: pointer to PCI device
10573  *
10574  * This routine is to be called to disattach a device with SLI-3 interface
10575  * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
10576  * removed from PCI bus, it performs all the necessary cleanup for the HBA
10577  * device to be removed from the PCI subsystem properly.
10578  **/
10579 static void
10580 lpfc_pci_remove_one_s3(struct pci_dev *pdev)
10581 {
10582 	struct Scsi_Host  *shost = pci_get_drvdata(pdev);
10583 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10584 	struct lpfc_vport **vports;
10585 	struct lpfc_hba   *phba = vport->phba;
10586 	int i;
10587 
10588 	spin_lock_irq(&phba->hbalock);
10589 	vport->load_flag |= FC_UNLOADING;
10590 	spin_unlock_irq(&phba->hbalock);
10591 
10592 	lpfc_free_sysfs_attr(vport);
10593 
10594 	/* Release all the vports against this physical port */
10595 	vports = lpfc_create_vport_work_array(phba);
10596 	if (vports != NULL)
10597 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10598 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10599 				continue;
10600 			fc_vport_terminate(vports[i]->fc_vport);
10601 		}
10602 	lpfc_destroy_vport_work_array(phba, vports);
10603 
10604 	/* Remove FC host and then SCSI host with the physical port */
10605 	fc_remove_host(shost);
10606 	scsi_remove_host(shost);
10607 
10608 	lpfc_cleanup(vport);
10609 
10610 	/*
10611 	 * Bring down the SLI Layer. This step disable all interrupts,
10612 	 * clears the rings, discards all mailbox commands, and resets
10613 	 * the HBA.
10614 	 */
10615 
10616 	/* HBA interrupt will be disabled after this call */
10617 	lpfc_sli_hba_down(phba);
10618 	/* Stop kthread signal shall trigger work_done one more time */
10619 	kthread_stop(phba->worker_thread);
10620 	/* Final cleanup of txcmplq and reset the HBA */
10621 	lpfc_sli_brdrestart(phba);
10622 
10623 	kfree(phba->vpi_bmask);
10624 	kfree(phba->vpi_ids);
10625 
10626 	lpfc_stop_hba_timers(phba);
10627 	spin_lock_irq(&phba->hbalock);
10628 	list_del_init(&vport->listentry);
10629 	spin_unlock_irq(&phba->hbalock);
10630 
10631 	lpfc_debugfs_terminate(vport);
10632 
10633 	/* Disable SR-IOV if enabled */
10634 	if (phba->cfg_sriov_nr_virtfn)
10635 		pci_disable_sriov(pdev);
10636 
10637 	/* Disable interrupt */
10638 	lpfc_sli_disable_intr(phba);
10639 
10640 	scsi_host_put(shost);
10641 
10642 	/*
10643 	 * Call scsi_free before mem_free since scsi bufs are released to their
10644 	 * corresponding pools here.
10645 	 */
10646 	lpfc_scsi_free(phba);
10647 	lpfc_mem_free_all(phba);
10648 
10649 	dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
10650 			  phba->hbqslimp.virt, phba->hbqslimp.phys);
10651 
10652 	/* Free resources associated with SLI2 interface */
10653 	dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
10654 			  phba->slim2p.virt, phba->slim2p.phys);
10655 
10656 	/* unmap adapter SLIM and Control Registers */
10657 	iounmap(phba->ctrl_regs_memmap_p);
10658 	iounmap(phba->slim_memmap_p);
10659 
10660 	lpfc_hba_free(phba);
10661 
10662 	pci_release_mem_regions(pdev);
10663 	pci_disable_device(pdev);
10664 }
10665 
10666 /**
10667  * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
10668  * @pdev: pointer to PCI device
10669  * @msg: power management message
10670  *
10671  * This routine is to be called from the kernel's PCI subsystem to support
10672  * system Power Management (PM) to device with SLI-3 interface spec. When
10673  * PM invokes this method, it quiesces the device by stopping the driver's
10674  * worker thread for the device, turning off device's interrupt and DMA,
10675  * and bring the device offline. Note that as the driver implements the
10676  * minimum PM requirements to a power-aware driver's PM support for the
10677  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10678  * to the suspend() method call will be treated as SUSPEND and the driver will
10679  * fully reinitialize its device during resume() method call, the driver will
10680  * set device to PCI_D3hot state in PCI config space instead of setting it
10681  * according to the @msg provided by the PM.
10682  *
10683  * Return code
10684  * 	0 - driver suspended the device
10685  * 	Error otherwise
10686  **/
10687 static int
10688 lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
10689 {
10690 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10691 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10692 
10693 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10694 			"0473 PCI device Power Management suspend.\n");
10695 
10696 	/* Bring down the device */
10697 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
10698 	lpfc_offline(phba);
10699 	kthread_stop(phba->worker_thread);
10700 
10701 	/* Disable interrupt from device */
10702 	lpfc_sli_disable_intr(phba);
10703 
10704 	/* Save device state to PCI config space */
10705 	pci_save_state(pdev);
10706 	pci_set_power_state(pdev, PCI_D3hot);
10707 
10708 	return 0;
10709 }
10710 
10711 /**
10712  * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
10713  * @pdev: pointer to PCI device
10714  *
10715  * This routine is to be called from the kernel's PCI subsystem to support
10716  * system Power Management (PM) to device with SLI-3 interface spec. When PM
10717  * invokes this method, it restores the device's PCI config space state and
10718  * fully reinitializes the device and brings it online. Note that as the
10719  * driver implements the minimum PM requirements to a power-aware driver's
10720  * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
10721  * FREEZE) to the suspend() method call will be treated as SUSPEND and the
10722  * driver will fully reinitialize its device during resume() method call,
10723  * the device will be set to PCI_D0 directly in PCI config space before
10724  * restoring the state.
10725  *
10726  * Return code
10727  * 	0 - driver suspended the device
10728  * 	Error otherwise
10729  **/
10730 static int
10731 lpfc_pci_resume_one_s3(struct pci_dev *pdev)
10732 {
10733 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10734 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10735 	uint32_t intr_mode;
10736 	int error;
10737 
10738 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10739 			"0452 PCI device Power Management resume.\n");
10740 
10741 	/* Restore device state from PCI config space */
10742 	pci_set_power_state(pdev, PCI_D0);
10743 	pci_restore_state(pdev);
10744 
10745 	/*
10746 	 * As the new kernel behavior of pci_restore_state() API call clears
10747 	 * device saved_state flag, need to save the restored state again.
10748 	 */
10749 	pci_save_state(pdev);
10750 
10751 	if (pdev->is_busmaster)
10752 		pci_set_master(pdev);
10753 
10754 	/* Startup the kernel thread for this host adapter. */
10755 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
10756 					"lpfc_worker_%d", phba->brd_no);
10757 	if (IS_ERR(phba->worker_thread)) {
10758 		error = PTR_ERR(phba->worker_thread);
10759 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10760 				"0434 PM resume failed to start worker "
10761 				"thread: error=x%x.\n", error);
10762 		return error;
10763 	}
10764 
10765 	/* Configure and enable interrupt */
10766 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
10767 	if (intr_mode == LPFC_INTR_ERROR) {
10768 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10769 				"0430 PM resume Failed to enable interrupt\n");
10770 		return -EIO;
10771 	} else
10772 		phba->intr_mode = intr_mode;
10773 
10774 	/* Restart HBA and bring it online */
10775 	lpfc_sli_brdrestart(phba);
10776 	lpfc_online(phba);
10777 
10778 	/* Log the current active interrupt mode */
10779 	lpfc_log_intr_mode(phba, phba->intr_mode);
10780 
10781 	return 0;
10782 }
10783 
10784 /**
10785  * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
10786  * @phba: pointer to lpfc hba data structure.
10787  *
10788  * This routine is called to prepare the SLI3 device for PCI slot recover. It
10789  * aborts all the outstanding SCSI I/Os to the pci device.
10790  **/
10791 static void
10792 lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
10793 {
10794 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10795 			"2723 PCI channel I/O abort preparing for recovery\n");
10796 
10797 	/*
10798 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10799 	 * and let the SCSI mid-layer to retry them to recover.
10800 	 */
10801 	lpfc_sli_abort_fcp_rings(phba);
10802 }
10803 
10804 /**
10805  * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
10806  * @phba: pointer to lpfc hba data structure.
10807  *
10808  * This routine is called to prepare the SLI3 device for PCI slot reset. It
10809  * disables the device interrupt and pci device, and aborts the internal FCP
10810  * pending I/Os.
10811  **/
10812 static void
10813 lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
10814 {
10815 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10816 			"2710 PCI channel disable preparing for reset\n");
10817 
10818 	/* Block any management I/Os to the device */
10819 	lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
10820 
10821 	/* Block all SCSI devices' I/Os on the host */
10822 	lpfc_scsi_dev_block(phba);
10823 
10824 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
10825 	lpfc_sli_flush_fcp_rings(phba);
10826 
10827 	/* stop all timers */
10828 	lpfc_stop_hba_timers(phba);
10829 
10830 	/* Disable interrupt and pci device */
10831 	lpfc_sli_disable_intr(phba);
10832 	pci_disable_device(phba->pcidev);
10833 }
10834 
10835 /**
10836  * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
10837  * @phba: pointer to lpfc hba data structure.
10838  *
10839  * This routine is called to prepare the SLI3 device for PCI slot permanently
10840  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10841  * pending I/Os.
10842  **/
10843 static void
10844 lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10845 {
10846 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10847 			"2711 PCI channel permanent disable for failure\n");
10848 	/* Block all SCSI devices' I/Os on the host */
10849 	lpfc_scsi_dev_block(phba);
10850 
10851 	/* stop all timers */
10852 	lpfc_stop_hba_timers(phba);
10853 
10854 	/* Clean up all driver's outstanding SCSI I/Os */
10855 	lpfc_sli_flush_fcp_rings(phba);
10856 }
10857 
10858 /**
10859  * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
10860  * @pdev: pointer to PCI device.
10861  * @state: the current PCI connection state.
10862  *
10863  * This routine is called from the PCI subsystem for I/O error handling to
10864  * device with SLI-3 interface spec. This function is called by the PCI
10865  * subsystem after a PCI bus error affecting this device has been detected.
10866  * When this function is invoked, it will need to stop all the I/Os and
10867  * interrupt(s) to the device. Once that is done, it will return
10868  * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
10869  * as desired.
10870  *
10871  * Return codes
10872  * 	PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
10873  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10874  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10875  **/
10876 static pci_ers_result_t
10877 lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
10878 {
10879 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10880 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10881 
10882 	switch (state) {
10883 	case pci_channel_io_normal:
10884 		/* Non-fatal error, prepare for recovery */
10885 		lpfc_sli_prep_dev_for_recover(phba);
10886 		return PCI_ERS_RESULT_CAN_RECOVER;
10887 	case pci_channel_io_frozen:
10888 		/* Fatal error, prepare for slot reset */
10889 		lpfc_sli_prep_dev_for_reset(phba);
10890 		return PCI_ERS_RESULT_NEED_RESET;
10891 	case pci_channel_io_perm_failure:
10892 		/* Permanent failure, prepare for device down */
10893 		lpfc_sli_prep_dev_for_perm_failure(phba);
10894 		return PCI_ERS_RESULT_DISCONNECT;
10895 	default:
10896 		/* Unknown state, prepare and request slot reset */
10897 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10898 				"0472 Unknown PCI error state: x%x\n", state);
10899 		lpfc_sli_prep_dev_for_reset(phba);
10900 		return PCI_ERS_RESULT_NEED_RESET;
10901 	}
10902 }
10903 
10904 /**
10905  * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
10906  * @pdev: pointer to PCI device.
10907  *
10908  * This routine is called from the PCI subsystem for error handling to
10909  * device with SLI-3 interface spec. This is called after PCI bus has been
10910  * reset to restart the PCI card from scratch, as if from a cold-boot.
10911  * During the PCI subsystem error recovery, after driver returns
10912  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10913  * recovery and then call this routine before calling the .resume method
10914  * to recover the device. This function will initialize the HBA device,
10915  * enable the interrupt, but it will just put the HBA to offline state
10916  * without passing any I/O traffic.
10917  *
10918  * Return codes
10919  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
10920  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10921  */
10922 static pci_ers_result_t
10923 lpfc_io_slot_reset_s3(struct pci_dev *pdev)
10924 {
10925 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10926 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10927 	struct lpfc_sli *psli = &phba->sli;
10928 	uint32_t intr_mode;
10929 
10930 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10931 	if (pci_enable_device_mem(pdev)) {
10932 		printk(KERN_ERR "lpfc: Cannot re-enable "
10933 			"PCI device after reset.\n");
10934 		return PCI_ERS_RESULT_DISCONNECT;
10935 	}
10936 
10937 	pci_restore_state(pdev);
10938 
10939 	/*
10940 	 * As the new kernel behavior of pci_restore_state() API call clears
10941 	 * device saved_state flag, need to save the restored state again.
10942 	 */
10943 	pci_save_state(pdev);
10944 
10945 	if (pdev->is_busmaster)
10946 		pci_set_master(pdev);
10947 
10948 	spin_lock_irq(&phba->hbalock);
10949 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10950 	spin_unlock_irq(&phba->hbalock);
10951 
10952 	/* Configure and enable interrupt */
10953 	intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
10954 	if (intr_mode == LPFC_INTR_ERROR) {
10955 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10956 				"0427 Cannot re-enable interrupt after "
10957 				"slot reset.\n");
10958 		return PCI_ERS_RESULT_DISCONNECT;
10959 	} else
10960 		phba->intr_mode = intr_mode;
10961 
10962 	/* Take device offline, it will perform cleanup */
10963 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
10964 	lpfc_offline(phba);
10965 	lpfc_sli_brdrestart(phba);
10966 
10967 	/* Log the current active interrupt mode */
10968 	lpfc_log_intr_mode(phba, phba->intr_mode);
10969 
10970 	return PCI_ERS_RESULT_RECOVERED;
10971 }
10972 
10973 /**
10974  * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
10975  * @pdev: pointer to PCI device
10976  *
10977  * This routine is called from the PCI subsystem for error handling to device
10978  * with SLI-3 interface spec. It is called when kernel error recovery tells
10979  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10980  * error recovery. After this call, traffic can start to flow from this device
10981  * again.
10982  */
10983 static void
10984 lpfc_io_resume_s3(struct pci_dev *pdev)
10985 {
10986 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
10987 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10988 
10989 	/* Bring device online, it will be no-op for non-fatal error resume */
10990 	lpfc_online(phba);
10991 
10992 	/* Clean up Advanced Error Reporting (AER) if needed */
10993 	if (phba->hba_flag & HBA_AER_ENABLED)
10994 		pci_cleanup_aer_uncorrect_error_status(pdev);
10995 }
10996 
10997 /**
10998  * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
10999  * @phba: pointer to lpfc hba data structure.
11000  *
11001  * returns the number of ELS/CT IOCBs to reserve
11002  **/
11003 int
11004 lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
11005 {
11006 	int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
11007 
11008 	if (phba->sli_rev == LPFC_SLI_REV4) {
11009 		if (max_xri <= 100)
11010 			return 10;
11011 		else if (max_xri <= 256)
11012 			return 25;
11013 		else if (max_xri <= 512)
11014 			return 50;
11015 		else if (max_xri <= 1024)
11016 			return 100;
11017 		else if (max_xri <= 1536)
11018 			return 150;
11019 		else if (max_xri <= 2048)
11020 			return 200;
11021 		else
11022 			return 250;
11023 	} else
11024 		return 0;
11025 }
11026 
11027 /**
11028  * lpfc_sli4_get_iocb_cnt - Calculate the # of total IOCBs to reserve
11029  * @phba: pointer to lpfc hba data structure.
11030  *
11031  * returns the number of ELS/CT + NVMET IOCBs to reserve
11032  **/
11033 int
11034 lpfc_sli4_get_iocb_cnt(struct lpfc_hba *phba)
11035 {
11036 	int max_xri = lpfc_sli4_get_els_iocb_cnt(phba);
11037 
11038 	if (phba->nvmet_support)
11039 		max_xri += LPFC_NVMET_BUF_POST;
11040 	return max_xri;
11041 }
11042 
11043 
11044 /**
11045  * lpfc_write_firmware - attempt to write a firmware image to the port
11046  * @fw: pointer to firmware image returned from request_firmware.
11047  * @phba: pointer to lpfc hba data structure.
11048  *
11049  **/
11050 static void
11051 lpfc_write_firmware(const struct firmware *fw, void *context)
11052 {
11053 	struct lpfc_hba *phba = (struct lpfc_hba *)context;
11054 	char fwrev[FW_REV_STR_SIZE];
11055 	struct lpfc_grp_hdr *image;
11056 	struct list_head dma_buffer_list;
11057 	int i, rc = 0;
11058 	struct lpfc_dmabuf *dmabuf, *next;
11059 	uint32_t offset = 0, temp_offset = 0;
11060 	uint32_t magic_number, ftype, fid, fsize;
11061 
11062 	/* It can be null in no-wait mode, sanity check */
11063 	if (!fw) {
11064 		rc = -ENXIO;
11065 		goto out;
11066 	}
11067 	image = (struct lpfc_grp_hdr *)fw->data;
11068 
11069 	magic_number = be32_to_cpu(image->magic_number);
11070 	ftype = bf_get_be32(lpfc_grp_hdr_file_type, image);
11071 	fid = bf_get_be32(lpfc_grp_hdr_id, image),
11072 	fsize = be32_to_cpu(image->size);
11073 
11074 	INIT_LIST_HEAD(&dma_buffer_list);
11075 	if ((magic_number != LPFC_GROUP_OJECT_MAGIC_G5 &&
11076 	     magic_number != LPFC_GROUP_OJECT_MAGIC_G6) ||
11077 	    ftype != LPFC_FILE_TYPE_GROUP || fsize != fw->size) {
11078 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11079 				"3022 Invalid FW image found. "
11080 				"Magic:%x Type:%x ID:%x Size %d %zd\n",
11081 				magic_number, ftype, fid, fsize, fw->size);
11082 		rc = -EINVAL;
11083 		goto release_out;
11084 	}
11085 	lpfc_decode_firmware_rev(phba, fwrev, 1);
11086 	if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
11087 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11088 				"3023 Updating Firmware, Current Version:%s "
11089 				"New Version:%s\n",
11090 				fwrev, image->revision);
11091 		for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
11092 			dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
11093 					 GFP_KERNEL);
11094 			if (!dmabuf) {
11095 				rc = -ENOMEM;
11096 				goto release_out;
11097 			}
11098 			dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11099 							  SLI4_PAGE_SIZE,
11100 							  &dmabuf->phys,
11101 							  GFP_KERNEL);
11102 			if (!dmabuf->virt) {
11103 				kfree(dmabuf);
11104 				rc = -ENOMEM;
11105 				goto release_out;
11106 			}
11107 			list_add_tail(&dmabuf->list, &dma_buffer_list);
11108 		}
11109 		while (offset < fw->size) {
11110 			temp_offset = offset;
11111 			list_for_each_entry(dmabuf, &dma_buffer_list, list) {
11112 				if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
11113 					memcpy(dmabuf->virt,
11114 					       fw->data + temp_offset,
11115 					       fw->size - temp_offset);
11116 					temp_offset = fw->size;
11117 					break;
11118 				}
11119 				memcpy(dmabuf->virt, fw->data + temp_offset,
11120 				       SLI4_PAGE_SIZE);
11121 				temp_offset += SLI4_PAGE_SIZE;
11122 			}
11123 			rc = lpfc_wr_object(phba, &dma_buffer_list,
11124 				    (fw->size - offset), &offset);
11125 			if (rc)
11126 				goto release_out;
11127 		}
11128 		rc = offset;
11129 	}
11130 
11131 release_out:
11132 	list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
11133 		list_del(&dmabuf->list);
11134 		dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
11135 				  dmabuf->virt, dmabuf->phys);
11136 		kfree(dmabuf);
11137 	}
11138 	release_firmware(fw);
11139 out:
11140 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11141 			"3024 Firmware update done: %d.\n", rc);
11142 	return;
11143 }
11144 
11145 /**
11146  * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
11147  * @phba: pointer to lpfc hba data structure.
11148  *
11149  * This routine is called to perform Linux generic firmware upgrade on device
11150  * that supports such feature.
11151  **/
11152 int
11153 lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
11154 {
11155 	uint8_t file_name[ELX_MODEL_NAME_SIZE];
11156 	int ret;
11157 	const struct firmware *fw;
11158 
11159 	/* Only supported on SLI4 interface type 2 for now */
11160 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
11161 	    LPFC_SLI_INTF_IF_TYPE_2)
11162 		return -EPERM;
11163 
11164 	snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
11165 
11166 	if (fw_upgrade == INT_FW_UPGRADE) {
11167 		ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
11168 					file_name, &phba->pcidev->dev,
11169 					GFP_KERNEL, (void *)phba,
11170 					lpfc_write_firmware);
11171 	} else if (fw_upgrade == RUN_FW_UPGRADE) {
11172 		ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
11173 		if (!ret)
11174 			lpfc_write_firmware(fw, (void *)phba);
11175 	} else {
11176 		ret = -EINVAL;
11177 	}
11178 
11179 	return ret;
11180 }
11181 
11182 /**
11183  * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
11184  * @pdev: pointer to PCI device
11185  * @pid: pointer to PCI device identifier
11186  *
11187  * This routine is called from the kernel's PCI subsystem to device with
11188  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11189  * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
11190  * information of the device and driver to see if the driver state that it
11191  * can support this kind of device. If the match is successful, the driver
11192  * core invokes this routine. If this routine determines it can claim the HBA,
11193  * it does all the initialization that it needs to do to handle the HBA
11194  * properly.
11195  *
11196  * Return code
11197  * 	0 - driver can claim the device
11198  * 	negative value - driver can not claim the device
11199  **/
11200 static int
11201 lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
11202 {
11203 	struct lpfc_hba   *phba;
11204 	struct lpfc_vport *vport = NULL;
11205 	struct Scsi_Host  *shost = NULL;
11206 	int error;
11207 	uint32_t cfg_mode, intr_mode;
11208 
11209 	/* Allocate memory for HBA structure */
11210 	phba = lpfc_hba_alloc(pdev);
11211 	if (!phba)
11212 		return -ENOMEM;
11213 
11214 	/* Perform generic PCI device enabling operation */
11215 	error = lpfc_enable_pci_dev(phba);
11216 	if (error)
11217 		goto out_free_phba;
11218 
11219 	/* Set up SLI API function jump table for PCI-device group-1 HBAs */
11220 	error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
11221 	if (error)
11222 		goto out_disable_pci_dev;
11223 
11224 	/* Set up SLI-4 specific device PCI memory space */
11225 	error = lpfc_sli4_pci_mem_setup(phba);
11226 	if (error) {
11227 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11228 				"1410 Failed to set up pci memory space.\n");
11229 		goto out_disable_pci_dev;
11230 	}
11231 
11232 	/* Set up SLI-4 Specific device driver resources */
11233 	error = lpfc_sli4_driver_resource_setup(phba);
11234 	if (error) {
11235 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11236 				"1412 Failed to set up driver resource.\n");
11237 		goto out_unset_pci_mem_s4;
11238 	}
11239 
11240 	INIT_LIST_HEAD(&phba->active_rrq_list);
11241 	INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
11242 
11243 	/* Set up common device driver resources */
11244 	error = lpfc_setup_driver_resource_phase2(phba);
11245 	if (error) {
11246 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11247 				"1414 Failed to set up driver resource.\n");
11248 		goto out_unset_driver_resource_s4;
11249 	}
11250 
11251 	/* Get the default values for Model Name and Description */
11252 	lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
11253 
11254 	/* Create SCSI host to the physical port */
11255 	error = lpfc_create_shost(phba);
11256 	if (error) {
11257 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11258 				"1415 Failed to create scsi host.\n");
11259 		goto out_unset_driver_resource;
11260 	}
11261 
11262 	/* Configure sysfs attributes */
11263 	vport = phba->pport;
11264 	error = lpfc_alloc_sysfs_attr(vport);
11265 	if (error) {
11266 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11267 				"1416 Failed to allocate sysfs attr\n");
11268 		goto out_destroy_shost;
11269 	}
11270 
11271 	shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
11272 	/* Now, trying to enable interrupt and bring up the device */
11273 	cfg_mode = phba->cfg_use_msi;
11274 
11275 	/* Put device to a known state before enabling interrupt */
11276 	lpfc_stop_port(phba);
11277 
11278 	/* Configure and enable interrupt */
11279 	intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
11280 	if (intr_mode == LPFC_INTR_ERROR) {
11281 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11282 				"0426 Failed to enable interrupt.\n");
11283 		error = -ENODEV;
11284 		goto out_free_sysfs_attr;
11285 	}
11286 	/* Default to single EQ for non-MSI-X */
11287 	if (phba->intr_type != MSIX) {
11288 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP)
11289 			phba->cfg_fcp_io_channel = 1;
11290 		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11291 			phba->cfg_nvme_io_channel = 1;
11292 			if (phba->nvmet_support)
11293 				phba->cfg_nvmet_mrq = 1;
11294 		}
11295 		phba->io_channel_irqs = 1;
11296 	}
11297 
11298 	/* Set up SLI-4 HBA */
11299 	if (lpfc_sli4_hba_setup(phba)) {
11300 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11301 				"1421 Failed to set up hba\n");
11302 		error = -ENODEV;
11303 		goto out_disable_intr;
11304 	}
11305 
11306 	/* Log the current active interrupt mode */
11307 	phba->intr_mode = intr_mode;
11308 	lpfc_log_intr_mode(phba, intr_mode);
11309 
11310 	/* Perform post initialization setup */
11311 	lpfc_post_init_setup(phba);
11312 
11313 	/* NVME support in FW earlier in the driver load corrects the
11314 	 * FC4 type making a check for nvme_support unnecessary.
11315 	 */
11316 	if ((phba->nvmet_support == 0) &&
11317 	    (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
11318 		/* Create NVME binding with nvme_fc_transport. This
11319 		 * ensures the vport is initialized.  If the localport
11320 		 * create fails, it should not unload the driver to
11321 		 * support field issues.
11322 		 */
11323 		error = lpfc_nvme_create_localport(vport);
11324 		if (error) {
11325 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11326 					"6004 NVME registration failed, "
11327 					"error x%x\n",
11328 					error);
11329 		}
11330 	}
11331 
11332 	/* check for firmware upgrade or downgrade */
11333 	if (phba->cfg_request_firmware_upgrade)
11334 		lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
11335 
11336 	/* Check if there are static vports to be created. */
11337 	lpfc_create_static_vport(phba);
11338 	return 0;
11339 
11340 out_disable_intr:
11341 	lpfc_sli4_disable_intr(phba);
11342 out_free_sysfs_attr:
11343 	lpfc_free_sysfs_attr(vport);
11344 out_destroy_shost:
11345 	lpfc_destroy_shost(phba);
11346 out_unset_driver_resource:
11347 	lpfc_unset_driver_resource_phase2(phba);
11348 out_unset_driver_resource_s4:
11349 	lpfc_sli4_driver_resource_unset(phba);
11350 out_unset_pci_mem_s4:
11351 	lpfc_sli4_pci_mem_unset(phba);
11352 out_disable_pci_dev:
11353 	lpfc_disable_pci_dev(phba);
11354 	if (shost)
11355 		scsi_host_put(shost);
11356 out_free_phba:
11357 	lpfc_hba_free(phba);
11358 	return error;
11359 }
11360 
11361 /**
11362  * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
11363  * @pdev: pointer to PCI device
11364  *
11365  * This routine is called from the kernel's PCI subsystem to device with
11366  * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
11367  * removed from PCI bus, it performs all the necessary cleanup for the HBA
11368  * device to be removed from the PCI subsystem properly.
11369  **/
11370 static void
11371 lpfc_pci_remove_one_s4(struct pci_dev *pdev)
11372 {
11373 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11374 	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
11375 	struct lpfc_vport **vports;
11376 	struct lpfc_hba *phba = vport->phba;
11377 	int i;
11378 
11379 	/* Mark the device unloading flag */
11380 	spin_lock_irq(&phba->hbalock);
11381 	vport->load_flag |= FC_UNLOADING;
11382 	spin_unlock_irq(&phba->hbalock);
11383 
11384 	/* Free the HBA sysfs attributes */
11385 	lpfc_free_sysfs_attr(vport);
11386 
11387 	/* Release all the vports against this physical port */
11388 	vports = lpfc_create_vport_work_array(phba);
11389 	if (vports != NULL)
11390 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11391 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
11392 				continue;
11393 			fc_vport_terminate(vports[i]->fc_vport);
11394 		}
11395 	lpfc_destroy_vport_work_array(phba, vports);
11396 
11397 	/* Remove FC host and then SCSI host with the physical port */
11398 	fc_remove_host(shost);
11399 	scsi_remove_host(shost);
11400 
11401 	/* Perform ndlp cleanup on the physical port.  The nvme and nvmet
11402 	 * localports are destroyed after to cleanup all transport memory.
11403 	 */
11404 	lpfc_cleanup(vport);
11405 	lpfc_nvmet_destroy_targetport(phba);
11406 	lpfc_nvme_destroy_localport(vport);
11407 
11408 	/*
11409 	 * Bring down the SLI Layer. This step disables all interrupts,
11410 	 * clears the rings, discards all mailbox commands, and resets
11411 	 * the HBA FCoE function.
11412 	 */
11413 	lpfc_debugfs_terminate(vport);
11414 	lpfc_sli4_hba_unset(phba);
11415 
11416 	spin_lock_irq(&phba->hbalock);
11417 	list_del_init(&vport->listentry);
11418 	spin_unlock_irq(&phba->hbalock);
11419 
11420 	/* Perform scsi free before driver resource_unset since scsi
11421 	 * buffers are released to their corresponding pools here.
11422 	 */
11423 	lpfc_scsi_free(phba);
11424 	lpfc_nvme_free(phba);
11425 	lpfc_free_iocb_list(phba);
11426 
11427 	lpfc_sli4_driver_resource_unset(phba);
11428 
11429 	/* Unmap adapter Control and Doorbell registers */
11430 	lpfc_sli4_pci_mem_unset(phba);
11431 
11432 	/* Release PCI resources and disable device's PCI function */
11433 	scsi_host_put(shost);
11434 	lpfc_disable_pci_dev(phba);
11435 
11436 	/* Finally, free the driver's device data structure */
11437 	lpfc_hba_free(phba);
11438 
11439 	return;
11440 }
11441 
11442 /**
11443  * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
11444  * @pdev: pointer to PCI device
11445  * @msg: power management message
11446  *
11447  * This routine is called from the kernel's PCI subsystem to support system
11448  * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
11449  * this method, it quiesces the device by stopping the driver's worker
11450  * thread for the device, turning off device's interrupt and DMA, and bring
11451  * the device offline. Note that as the driver implements the minimum PM
11452  * requirements to a power-aware driver's PM support for suspend/resume -- all
11453  * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
11454  * method call will be treated as SUSPEND and the driver will fully
11455  * reinitialize its device during resume() method call, the driver will set
11456  * device to PCI_D3hot state in PCI config space instead of setting it
11457  * according to the @msg provided by the PM.
11458  *
11459  * Return code
11460  * 	0 - driver suspended the device
11461  * 	Error otherwise
11462  **/
11463 static int
11464 lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
11465 {
11466 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11467 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11468 
11469 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11470 			"2843 PCI device Power Management suspend.\n");
11471 
11472 	/* Bring down the device */
11473 	lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11474 	lpfc_offline(phba);
11475 	kthread_stop(phba->worker_thread);
11476 
11477 	/* Disable interrupt from device */
11478 	lpfc_sli4_disable_intr(phba);
11479 	lpfc_sli4_queue_destroy(phba);
11480 
11481 	/* Save device state to PCI config space */
11482 	pci_save_state(pdev);
11483 	pci_set_power_state(pdev, PCI_D3hot);
11484 
11485 	return 0;
11486 }
11487 
11488 /**
11489  * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
11490  * @pdev: pointer to PCI device
11491  *
11492  * This routine is called from the kernel's PCI subsystem to support system
11493  * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
11494  * this method, it restores the device's PCI config space state and fully
11495  * reinitializes the device and brings it online. Note that as the driver
11496  * implements the minimum PM requirements to a power-aware driver's PM for
11497  * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
11498  * to the suspend() method call will be treated as SUSPEND and the driver
11499  * will fully reinitialize its device during resume() method call, the device
11500  * will be set to PCI_D0 directly in PCI config space before restoring the
11501  * state.
11502  *
11503  * Return code
11504  * 	0 - driver suspended the device
11505  * 	Error otherwise
11506  **/
11507 static int
11508 lpfc_pci_resume_one_s4(struct pci_dev *pdev)
11509 {
11510 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11511 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11512 	uint32_t intr_mode;
11513 	int error;
11514 
11515 	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11516 			"0292 PCI device Power Management resume.\n");
11517 
11518 	/* Restore device state from PCI config space */
11519 	pci_set_power_state(pdev, PCI_D0);
11520 	pci_restore_state(pdev);
11521 
11522 	/*
11523 	 * As the new kernel behavior of pci_restore_state() API call clears
11524 	 * device saved_state flag, need to save the restored state again.
11525 	 */
11526 	pci_save_state(pdev);
11527 
11528 	if (pdev->is_busmaster)
11529 		pci_set_master(pdev);
11530 
11531 	 /* Startup the kernel thread for this host adapter. */
11532 	phba->worker_thread = kthread_run(lpfc_do_work, phba,
11533 					"lpfc_worker_%d", phba->brd_no);
11534 	if (IS_ERR(phba->worker_thread)) {
11535 		error = PTR_ERR(phba->worker_thread);
11536 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11537 				"0293 PM resume failed to start worker "
11538 				"thread: error=x%x.\n", error);
11539 		return error;
11540 	}
11541 
11542 	/* Configure and enable interrupt */
11543 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11544 	if (intr_mode == LPFC_INTR_ERROR) {
11545 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11546 				"0294 PM resume Failed to enable interrupt\n");
11547 		return -EIO;
11548 	} else
11549 		phba->intr_mode = intr_mode;
11550 
11551 	/* Restart HBA and bring it online */
11552 	lpfc_sli_brdrestart(phba);
11553 	lpfc_online(phba);
11554 
11555 	/* Log the current active interrupt mode */
11556 	lpfc_log_intr_mode(phba, phba->intr_mode);
11557 
11558 	return 0;
11559 }
11560 
11561 /**
11562  * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
11563  * @phba: pointer to lpfc hba data structure.
11564  *
11565  * This routine is called to prepare the SLI4 device for PCI slot recover. It
11566  * aborts all the outstanding SCSI I/Os to the pci device.
11567  **/
11568 static void
11569 lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
11570 {
11571 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11572 			"2828 PCI channel I/O abort preparing for recovery\n");
11573 	/*
11574 	 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
11575 	 * and let the SCSI mid-layer to retry them to recover.
11576 	 */
11577 	lpfc_sli_abort_fcp_rings(phba);
11578 }
11579 
11580 /**
11581  * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
11582  * @phba: pointer to lpfc hba data structure.
11583  *
11584  * This routine is called to prepare the SLI4 device for PCI slot reset. It
11585  * disables the device interrupt and pci device, and aborts the internal FCP
11586  * pending I/Os.
11587  **/
11588 static void
11589 lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
11590 {
11591 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11592 			"2826 PCI channel disable preparing for reset\n");
11593 
11594 	/* Block any management I/Os to the device */
11595 	lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
11596 
11597 	/* Block all SCSI devices' I/Os on the host */
11598 	lpfc_scsi_dev_block(phba);
11599 
11600 	/* Flush all driver's outstanding SCSI I/Os as we are to reset */
11601 	lpfc_sli_flush_fcp_rings(phba);
11602 
11603 	/* stop all timers */
11604 	lpfc_stop_hba_timers(phba);
11605 
11606 	/* Disable interrupt and pci device */
11607 	lpfc_sli4_disable_intr(phba);
11608 	lpfc_sli4_queue_destroy(phba);
11609 	pci_disable_device(phba->pcidev);
11610 }
11611 
11612 /**
11613  * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
11614  * @phba: pointer to lpfc hba data structure.
11615  *
11616  * This routine is called to prepare the SLI4 device for PCI slot permanently
11617  * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
11618  * pending I/Os.
11619  **/
11620 static void
11621 lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
11622 {
11623 	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11624 			"2827 PCI channel permanent disable for failure\n");
11625 
11626 	/* Block all SCSI devices' I/Os on the host */
11627 	lpfc_scsi_dev_block(phba);
11628 
11629 	/* stop all timers */
11630 	lpfc_stop_hba_timers(phba);
11631 
11632 	/* Clean up all driver's outstanding SCSI I/Os */
11633 	lpfc_sli_flush_fcp_rings(phba);
11634 }
11635 
11636 /**
11637  * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
11638  * @pdev: pointer to PCI device.
11639  * @state: the current PCI connection state.
11640  *
11641  * This routine is called from the PCI subsystem for error handling to device
11642  * with SLI-4 interface spec. This function is called by the PCI subsystem
11643  * after a PCI bus error affecting this device has been detected. When this
11644  * function is invoked, it will need to stop all the I/Os and interrupt(s)
11645  * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
11646  * for the PCI subsystem to perform proper recovery as desired.
11647  *
11648  * Return codes
11649  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11650  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11651  **/
11652 static pci_ers_result_t
11653 lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
11654 {
11655 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11656 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11657 
11658 	switch (state) {
11659 	case pci_channel_io_normal:
11660 		/* Non-fatal error, prepare for recovery */
11661 		lpfc_sli4_prep_dev_for_recover(phba);
11662 		return PCI_ERS_RESULT_CAN_RECOVER;
11663 	case pci_channel_io_frozen:
11664 		/* Fatal error, prepare for slot reset */
11665 		lpfc_sli4_prep_dev_for_reset(phba);
11666 		return PCI_ERS_RESULT_NEED_RESET;
11667 	case pci_channel_io_perm_failure:
11668 		/* Permanent failure, prepare for device down */
11669 		lpfc_sli4_prep_dev_for_perm_failure(phba);
11670 		return PCI_ERS_RESULT_DISCONNECT;
11671 	default:
11672 		/* Unknown state, prepare and request slot reset */
11673 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11674 				"2825 Unknown PCI error state: x%x\n", state);
11675 		lpfc_sli4_prep_dev_for_reset(phba);
11676 		return PCI_ERS_RESULT_NEED_RESET;
11677 	}
11678 }
11679 
11680 /**
11681  * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
11682  * @pdev: pointer to PCI device.
11683  *
11684  * This routine is called from the PCI subsystem for error handling to device
11685  * with SLI-4 interface spec. It is called after PCI bus has been reset to
11686  * restart the PCI card from scratch, as if from a cold-boot. During the
11687  * PCI subsystem error recovery, after the driver returns
11688  * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
11689  * recovery and then call this routine before calling the .resume method to
11690  * recover the device. This function will initialize the HBA device, enable
11691  * the interrupt, but it will just put the HBA to offline state without
11692  * passing any I/O traffic.
11693  *
11694  * Return codes
11695  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
11696  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11697  */
11698 static pci_ers_result_t
11699 lpfc_io_slot_reset_s4(struct pci_dev *pdev)
11700 {
11701 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11702 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11703 	struct lpfc_sli *psli = &phba->sli;
11704 	uint32_t intr_mode;
11705 
11706 	dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
11707 	if (pci_enable_device_mem(pdev)) {
11708 		printk(KERN_ERR "lpfc: Cannot re-enable "
11709 			"PCI device after reset.\n");
11710 		return PCI_ERS_RESULT_DISCONNECT;
11711 	}
11712 
11713 	pci_restore_state(pdev);
11714 
11715 	/*
11716 	 * As the new kernel behavior of pci_restore_state() API call clears
11717 	 * device saved_state flag, need to save the restored state again.
11718 	 */
11719 	pci_save_state(pdev);
11720 
11721 	if (pdev->is_busmaster)
11722 		pci_set_master(pdev);
11723 
11724 	spin_lock_irq(&phba->hbalock);
11725 	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
11726 	spin_unlock_irq(&phba->hbalock);
11727 
11728 	/* Configure and enable interrupt */
11729 	intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
11730 	if (intr_mode == LPFC_INTR_ERROR) {
11731 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11732 				"2824 Cannot re-enable interrupt after "
11733 				"slot reset.\n");
11734 		return PCI_ERS_RESULT_DISCONNECT;
11735 	} else
11736 		phba->intr_mode = intr_mode;
11737 
11738 	/* Log the current active interrupt mode */
11739 	lpfc_log_intr_mode(phba, phba->intr_mode);
11740 
11741 	return PCI_ERS_RESULT_RECOVERED;
11742 }
11743 
11744 /**
11745  * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
11746  * @pdev: pointer to PCI device
11747  *
11748  * This routine is called from the PCI subsystem for error handling to device
11749  * with SLI-4 interface spec. It is called when kernel error recovery tells
11750  * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
11751  * error recovery. After this call, traffic can start to flow from this device
11752  * again.
11753  **/
11754 static void
11755 lpfc_io_resume_s4(struct pci_dev *pdev)
11756 {
11757 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11758 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11759 
11760 	/*
11761 	 * In case of slot reset, as function reset is performed through
11762 	 * mailbox command which needs DMA to be enabled, this operation
11763 	 * has to be moved to the io resume phase. Taking device offline
11764 	 * will perform the necessary cleanup.
11765 	 */
11766 	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
11767 		/* Perform device reset */
11768 		lpfc_offline_prep(phba, LPFC_MBX_WAIT);
11769 		lpfc_offline(phba);
11770 		lpfc_sli_brdrestart(phba);
11771 		/* Bring the device back online */
11772 		lpfc_online(phba);
11773 	}
11774 
11775 	/* Clean up Advanced Error Reporting (AER) if needed */
11776 	if (phba->hba_flag & HBA_AER_ENABLED)
11777 		pci_cleanup_aer_uncorrect_error_status(pdev);
11778 }
11779 
11780 /**
11781  * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
11782  * @pdev: pointer to PCI device
11783  * @pid: pointer to PCI device identifier
11784  *
11785  * This routine is to be registered to the kernel's PCI subsystem. When an
11786  * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
11787  * at PCI device-specific information of the device and driver to see if the
11788  * driver state that it can support this kind of device. If the match is
11789  * successful, the driver core invokes this routine. This routine dispatches
11790  * the action to the proper SLI-3 or SLI-4 device probing routine, which will
11791  * do all the initialization that it needs to do to handle the HBA device
11792  * properly.
11793  *
11794  * Return code
11795  * 	0 - driver can claim the device
11796  * 	negative value - driver can not claim the device
11797  **/
11798 static int
11799 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
11800 {
11801 	int rc;
11802 	struct lpfc_sli_intf intf;
11803 
11804 	if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
11805 		return -ENODEV;
11806 
11807 	if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
11808 	    (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
11809 		rc = lpfc_pci_probe_one_s4(pdev, pid);
11810 	else
11811 		rc = lpfc_pci_probe_one_s3(pdev, pid);
11812 
11813 	return rc;
11814 }
11815 
11816 /**
11817  * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
11818  * @pdev: pointer to PCI device
11819  *
11820  * This routine is to be registered to the kernel's PCI subsystem. When an
11821  * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
11822  * This routine dispatches the action to the proper SLI-3 or SLI-4 device
11823  * remove routine, which will perform all the necessary cleanup for the
11824  * device to be removed from the PCI subsystem properly.
11825  **/
11826 static void
11827 lpfc_pci_remove_one(struct pci_dev *pdev)
11828 {
11829 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11830 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11831 
11832 	switch (phba->pci_dev_grp) {
11833 	case LPFC_PCI_DEV_LP:
11834 		lpfc_pci_remove_one_s3(pdev);
11835 		break;
11836 	case LPFC_PCI_DEV_OC:
11837 		lpfc_pci_remove_one_s4(pdev);
11838 		break;
11839 	default:
11840 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11841 				"1424 Invalid PCI device group: 0x%x\n",
11842 				phba->pci_dev_grp);
11843 		break;
11844 	}
11845 	return;
11846 }
11847 
11848 /**
11849  * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
11850  * @pdev: pointer to PCI device
11851  * @msg: power management message
11852  *
11853  * This routine is to be registered to the kernel's PCI subsystem to support
11854  * system Power Management (PM). When PM invokes this method, it dispatches
11855  * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
11856  * suspend the device.
11857  *
11858  * Return code
11859  * 	0 - driver suspended the device
11860  * 	Error otherwise
11861  **/
11862 static int
11863 lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
11864 {
11865 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11866 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11867 	int rc = -ENODEV;
11868 
11869 	switch (phba->pci_dev_grp) {
11870 	case LPFC_PCI_DEV_LP:
11871 		rc = lpfc_pci_suspend_one_s3(pdev, msg);
11872 		break;
11873 	case LPFC_PCI_DEV_OC:
11874 		rc = lpfc_pci_suspend_one_s4(pdev, msg);
11875 		break;
11876 	default:
11877 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11878 				"1425 Invalid PCI device group: 0x%x\n",
11879 				phba->pci_dev_grp);
11880 		break;
11881 	}
11882 	return rc;
11883 }
11884 
11885 /**
11886  * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
11887  * @pdev: pointer to PCI device
11888  *
11889  * This routine is to be registered to the kernel's PCI subsystem to support
11890  * system Power Management (PM). When PM invokes this method, it dispatches
11891  * the action to the proper SLI-3 or SLI-4 device resume routine, which will
11892  * resume the device.
11893  *
11894  * Return code
11895  * 	0 - driver suspended the device
11896  * 	Error otherwise
11897  **/
11898 static int
11899 lpfc_pci_resume_one(struct pci_dev *pdev)
11900 {
11901 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11902 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11903 	int rc = -ENODEV;
11904 
11905 	switch (phba->pci_dev_grp) {
11906 	case LPFC_PCI_DEV_LP:
11907 		rc = lpfc_pci_resume_one_s3(pdev);
11908 		break;
11909 	case LPFC_PCI_DEV_OC:
11910 		rc = lpfc_pci_resume_one_s4(pdev);
11911 		break;
11912 	default:
11913 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11914 				"1426 Invalid PCI device group: 0x%x\n",
11915 				phba->pci_dev_grp);
11916 		break;
11917 	}
11918 	return rc;
11919 }
11920 
11921 /**
11922  * lpfc_io_error_detected - lpfc method for handling PCI I/O error
11923  * @pdev: pointer to PCI device.
11924  * @state: the current PCI connection state.
11925  *
11926  * This routine is registered to the PCI subsystem for error handling. This
11927  * function is called by the PCI subsystem after a PCI bus error affecting
11928  * this device has been detected. When this routine is invoked, it dispatches
11929  * the action to the proper SLI-3 or SLI-4 device error detected handling
11930  * routine, which will perform the proper error detected operation.
11931  *
11932  * Return codes
11933  * 	PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
11934  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11935  **/
11936 static pci_ers_result_t
11937 lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
11938 {
11939 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11940 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11941 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11942 
11943 	switch (phba->pci_dev_grp) {
11944 	case LPFC_PCI_DEV_LP:
11945 		rc = lpfc_io_error_detected_s3(pdev, state);
11946 		break;
11947 	case LPFC_PCI_DEV_OC:
11948 		rc = lpfc_io_error_detected_s4(pdev, state);
11949 		break;
11950 	default:
11951 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11952 				"1427 Invalid PCI device group: 0x%x\n",
11953 				phba->pci_dev_grp);
11954 		break;
11955 	}
11956 	return rc;
11957 }
11958 
11959 /**
11960  * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
11961  * @pdev: pointer to PCI device.
11962  *
11963  * This routine is registered to the PCI subsystem for error handling. This
11964  * function is called after PCI bus has been reset to restart the PCI card
11965  * from scratch, as if from a cold-boot. When this routine is invoked, it
11966  * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
11967  * routine, which will perform the proper device reset.
11968  *
11969  * Return codes
11970  * 	PCI_ERS_RESULT_RECOVERED - the device has been recovered
11971  * 	PCI_ERS_RESULT_DISCONNECT - device could not be recovered
11972  **/
11973 static pci_ers_result_t
11974 lpfc_io_slot_reset(struct pci_dev *pdev)
11975 {
11976 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
11977 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
11978 	pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
11979 
11980 	switch (phba->pci_dev_grp) {
11981 	case LPFC_PCI_DEV_LP:
11982 		rc = lpfc_io_slot_reset_s3(pdev);
11983 		break;
11984 	case LPFC_PCI_DEV_OC:
11985 		rc = lpfc_io_slot_reset_s4(pdev);
11986 		break;
11987 	default:
11988 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11989 				"1428 Invalid PCI device group: 0x%x\n",
11990 				phba->pci_dev_grp);
11991 		break;
11992 	}
11993 	return rc;
11994 }
11995 
11996 /**
11997  * lpfc_io_resume - lpfc method for resuming PCI I/O operation
11998  * @pdev: pointer to PCI device
11999  *
12000  * This routine is registered to the PCI subsystem for error handling. It
12001  * is called when kernel error recovery tells the lpfc driver that it is
12002  * OK to resume normal PCI operation after PCI bus error recovery. When
12003  * this routine is invoked, it dispatches the action to the proper SLI-3
12004  * or SLI-4 device io_resume routine, which will resume the device operation.
12005  **/
12006 static void
12007 lpfc_io_resume(struct pci_dev *pdev)
12008 {
12009 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
12010 	struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
12011 
12012 	switch (phba->pci_dev_grp) {
12013 	case LPFC_PCI_DEV_LP:
12014 		lpfc_io_resume_s3(pdev);
12015 		break;
12016 	case LPFC_PCI_DEV_OC:
12017 		lpfc_io_resume_s4(pdev);
12018 		break;
12019 	default:
12020 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12021 				"1429 Invalid PCI device group: 0x%x\n",
12022 				phba->pci_dev_grp);
12023 		break;
12024 	}
12025 	return;
12026 }
12027 
12028 /**
12029  * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
12030  * @phba: pointer to lpfc hba data structure.
12031  *
12032  * This routine checks to see if OAS is supported for this adapter. If
12033  * supported, the configure Flash Optimized Fabric flag is set.  Otherwise,
12034  * the enable oas flag is cleared and the pool created for OAS device data
12035  * is destroyed.
12036  *
12037  **/
12038 void
12039 lpfc_sli4_oas_verify(struct lpfc_hba *phba)
12040 {
12041 
12042 	if (!phba->cfg_EnableXLane)
12043 		return;
12044 
12045 	if (phba->sli4_hba.pc_sli4_params.oas_supported) {
12046 		phba->cfg_fof = 1;
12047 	} else {
12048 		phba->cfg_fof = 0;
12049 		if (phba->device_data_mem_pool)
12050 			mempool_destroy(phba->device_data_mem_pool);
12051 		phba->device_data_mem_pool = NULL;
12052 	}
12053 
12054 	return;
12055 }
12056 
12057 /**
12058  * lpfc_fof_queue_setup - Set up all the fof queues
12059  * @phba: pointer to lpfc hba data structure.
12060  *
12061  * This routine is invoked to set up all the fof queues for the FC HBA
12062  * operation.
12063  *
12064  * Return codes
12065  *      0 - successful
12066  *      -ENOMEM - No available memory
12067  **/
12068 int
12069 lpfc_fof_queue_setup(struct lpfc_hba *phba)
12070 {
12071 	struct lpfc_sli_ring *pring;
12072 	int rc;
12073 
12074 	rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
12075 	if (rc)
12076 		return -ENOMEM;
12077 
12078 	if (phba->cfg_fof) {
12079 
12080 		rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
12081 				    phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
12082 		if (rc)
12083 			goto out_oas_cq;
12084 
12085 		rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
12086 				    phba->sli4_hba.oas_cq, LPFC_FCP);
12087 		if (rc)
12088 			goto out_oas_wq;
12089 
12090 		/* Bind this CQ/WQ to the NVME ring */
12091 		pring = phba->sli4_hba.oas_wq->pring;
12092 		pring->sli.sli4.wqp =
12093 			(void *)phba->sli4_hba.oas_wq;
12094 		phba->sli4_hba.oas_cq->pring = pring;
12095 	}
12096 
12097 	return 0;
12098 
12099 out_oas_wq:
12100 	lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
12101 out_oas_cq:
12102 	lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
12103 	return rc;
12104 
12105 }
12106 
12107 /**
12108  * lpfc_fof_queue_create - Create all the fof queues
12109  * @phba: pointer to lpfc hba data structure.
12110  *
12111  * This routine is invoked to allocate all the fof queues for the FC HBA
12112  * operation. For each SLI4 queue type, the parameters such as queue entry
12113  * count (queue depth) shall be taken from the module parameter. For now,
12114  * we just use some constant number as place holder.
12115  *
12116  * Return codes
12117  *      0 - successful
12118  *      -ENOMEM - No availble memory
12119  *      -EIO - The mailbox failed to complete successfully.
12120  **/
12121 int
12122 lpfc_fof_queue_create(struct lpfc_hba *phba)
12123 {
12124 	struct lpfc_queue *qdesc;
12125 	uint32_t wqesize;
12126 
12127 	/* Create FOF EQ */
12128 	qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
12129 				      phba->sli4_hba.eq_ecount);
12130 	if (!qdesc)
12131 		goto out_error;
12132 
12133 	phba->sli4_hba.fof_eq = qdesc;
12134 
12135 	if (phba->cfg_fof) {
12136 
12137 		/* Create OAS CQ */
12138 		qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
12139 						      phba->sli4_hba.cq_ecount);
12140 		if (!qdesc)
12141 			goto out_error;
12142 
12143 		phba->sli4_hba.oas_cq = qdesc;
12144 
12145 		/* Create OAS WQ */
12146 		wqesize = (phba->fcp_embed_io) ?
12147 				LPFC_WQE128_SIZE : phba->sli4_hba.wq_esize;
12148 		qdesc = lpfc_sli4_queue_alloc(phba, wqesize,
12149 					      phba->sli4_hba.wq_ecount);
12150 
12151 		if (!qdesc)
12152 			goto out_error;
12153 
12154 		phba->sli4_hba.oas_wq = qdesc;
12155 		list_add_tail(&qdesc->wq_list, &phba->sli4_hba.lpfc_wq_list);
12156 
12157 	}
12158 	return 0;
12159 
12160 out_error:
12161 	lpfc_fof_queue_destroy(phba);
12162 	return -ENOMEM;
12163 }
12164 
12165 /**
12166  * lpfc_fof_queue_destroy - Destroy all the fof queues
12167  * @phba: pointer to lpfc hba data structure.
12168  *
12169  * This routine is invoked to release all the SLI4 queues with the FC HBA
12170  * operation.
12171  *
12172  * Return codes
12173  *      0 - successful
12174  **/
12175 int
12176 lpfc_fof_queue_destroy(struct lpfc_hba *phba)
12177 {
12178 	/* Release FOF Event queue */
12179 	if (phba->sli4_hba.fof_eq != NULL) {
12180 		lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
12181 		phba->sli4_hba.fof_eq = NULL;
12182 	}
12183 
12184 	/* Release OAS Completion queue */
12185 	if (phba->sli4_hba.oas_cq != NULL) {
12186 		lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
12187 		phba->sli4_hba.oas_cq = NULL;
12188 	}
12189 
12190 	/* Release OAS Work queue */
12191 	if (phba->sli4_hba.oas_wq != NULL) {
12192 		lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
12193 		phba->sli4_hba.oas_wq = NULL;
12194 	}
12195 	return 0;
12196 }
12197 
12198 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
12199 
12200 static const struct pci_error_handlers lpfc_err_handler = {
12201 	.error_detected = lpfc_io_error_detected,
12202 	.slot_reset = lpfc_io_slot_reset,
12203 	.resume = lpfc_io_resume,
12204 };
12205 
12206 static struct pci_driver lpfc_driver = {
12207 	.name		= LPFC_DRIVER_NAME,
12208 	.id_table	= lpfc_id_table,
12209 	.probe		= lpfc_pci_probe_one,
12210 	.remove		= lpfc_pci_remove_one,
12211 	.shutdown	= lpfc_pci_remove_one,
12212 	.suspend        = lpfc_pci_suspend_one,
12213 	.resume		= lpfc_pci_resume_one,
12214 	.err_handler    = &lpfc_err_handler,
12215 };
12216 
12217 static const struct file_operations lpfc_mgmt_fop = {
12218 	.owner = THIS_MODULE,
12219 };
12220 
12221 static struct miscdevice lpfc_mgmt_dev = {
12222 	.minor = MISC_DYNAMIC_MINOR,
12223 	.name = "lpfcmgmt",
12224 	.fops = &lpfc_mgmt_fop,
12225 };
12226 
12227 /**
12228  * lpfc_init - lpfc module initialization routine
12229  *
12230  * This routine is to be invoked when the lpfc module is loaded into the
12231  * kernel. The special kernel macro module_init() is used to indicate the
12232  * role of this routine to the kernel as lpfc module entry point.
12233  *
12234  * Return codes
12235  *   0 - successful
12236  *   -ENOMEM - FC attach transport failed
12237  *   all others - failed
12238  */
12239 static int __init
12240 lpfc_init(void)
12241 {
12242 	int error = 0;
12243 
12244 	printk(LPFC_MODULE_DESC "\n");
12245 	printk(LPFC_COPYRIGHT "\n");
12246 
12247 	error = misc_register(&lpfc_mgmt_dev);
12248 	if (error)
12249 		printk(KERN_ERR "Could not register lpfcmgmt device, "
12250 			"misc_register returned with status %d", error);
12251 
12252 	lpfc_transport_functions.vport_create = lpfc_vport_create;
12253 	lpfc_transport_functions.vport_delete = lpfc_vport_delete;
12254 	lpfc_transport_template =
12255 				fc_attach_transport(&lpfc_transport_functions);
12256 	if (lpfc_transport_template == NULL)
12257 		return -ENOMEM;
12258 	lpfc_vport_transport_template =
12259 		fc_attach_transport(&lpfc_vport_transport_functions);
12260 	if (lpfc_vport_transport_template == NULL) {
12261 		fc_release_transport(lpfc_transport_template);
12262 		return -ENOMEM;
12263 	}
12264 
12265 	/* Initialize in case vector mapping is needed */
12266 	lpfc_used_cpu = NULL;
12267 	lpfc_present_cpu = num_present_cpus();
12268 
12269 	error = pci_register_driver(&lpfc_driver);
12270 	if (error) {
12271 		fc_release_transport(lpfc_transport_template);
12272 		fc_release_transport(lpfc_vport_transport_template);
12273 	}
12274 
12275 	return error;
12276 }
12277 
12278 /**
12279  * lpfc_exit - lpfc module removal routine
12280  *
12281  * This routine is invoked when the lpfc module is removed from the kernel.
12282  * The special kernel macro module_exit() is used to indicate the role of
12283  * this routine to the kernel as lpfc module exit point.
12284  */
12285 static void __exit
12286 lpfc_exit(void)
12287 {
12288 	misc_deregister(&lpfc_mgmt_dev);
12289 	pci_unregister_driver(&lpfc_driver);
12290 	fc_release_transport(lpfc_transport_template);
12291 	fc_release_transport(lpfc_vport_transport_template);
12292 	if (_dump_buf_data) {
12293 		printk(KERN_ERR	"9062 BLKGRD: freeing %lu pages for "
12294 				"_dump_buf_data at 0x%p\n",
12295 				(1L << _dump_buf_data_order), _dump_buf_data);
12296 		free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
12297 	}
12298 
12299 	if (_dump_buf_dif) {
12300 		printk(KERN_ERR	"9049 BLKGRD: freeing %lu pages for "
12301 				"_dump_buf_dif at 0x%p\n",
12302 				(1L << _dump_buf_dif_order), _dump_buf_dif);
12303 		free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
12304 	}
12305 	kfree(lpfc_used_cpu);
12306 	idr_destroy(&lpfc_hba_index);
12307 }
12308 
12309 module_init(lpfc_init);
12310 module_exit(lpfc_exit);
12311 MODULE_LICENSE("GPL");
12312 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
12313 MODULE_AUTHOR("Broadcom");
12314 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
12315