xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_ct.c (revision babbdf5b)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. 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  *                                                                 *
10  * This program is free software; you can redistribute it and/or   *
11  * modify it under the terms of version 2 of the GNU General       *
12  * Public License as published by the Free Software Foundation.    *
13  * This program is distributed in the hope that it will be useful. *
14  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
15  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
16  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
17  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
18  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
19  * more details, a copy of which can be found in the file COPYING  *
20  * included with this package.                                     *
21  *******************************************************************/
22 
23 /*
24  * Fibre Channel SCSI LAN Device Driver CT support: FC Generic Services FC-GS
25  */
26 
27 #include <linux/blkdev.h>
28 #include <linux/pci.h>
29 #include <linux/interrupt.h>
30 #include <linux/slab.h>
31 #include <linux/utsname.h>
32 
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/fc/fc_fs.h>
38 
39 #include "lpfc_hw4.h"
40 #include "lpfc_hw.h"
41 #include "lpfc_sli.h"
42 #include "lpfc_sli4.h"
43 #include "lpfc_nl.h"
44 #include "lpfc_disc.h"
45 #include "lpfc.h"
46 #include "lpfc_scsi.h"
47 #include "lpfc_logmsg.h"
48 #include "lpfc_crtn.h"
49 #include "lpfc_version.h"
50 #include "lpfc_vport.h"
51 #include "lpfc_debugfs.h"
52 
53 /* FDMI Port Speed definitions - FC-GS-7 */
54 #define HBA_PORTSPEED_1GFC		0x00000001	/* 1G FC */
55 #define HBA_PORTSPEED_2GFC		0x00000002	/* 2G FC */
56 #define HBA_PORTSPEED_4GFC		0x00000008	/* 4G FC */
57 #define HBA_PORTSPEED_10GFC		0x00000004	/* 10G FC */
58 #define HBA_PORTSPEED_8GFC		0x00000010	/* 8G FC */
59 #define HBA_PORTSPEED_16GFC		0x00000020	/* 16G FC */
60 #define HBA_PORTSPEED_32GFC		0x00000040	/* 32G FC */
61 #define HBA_PORTSPEED_20GFC		0x00000080	/* 20G FC */
62 #define HBA_PORTSPEED_40GFC		0x00000100	/* 40G FC */
63 #define HBA_PORTSPEED_128GFC		0x00000200	/* 128G FC */
64 #define HBA_PORTSPEED_64GFC		0x00000400	/* 64G FC */
65 #define HBA_PORTSPEED_256GFC		0x00000800	/* 256G FC */
66 #define HBA_PORTSPEED_UNKNOWN		0x00008000	/* Unknown */
67 #define HBA_PORTSPEED_10GE		0x00010000	/* 10G E */
68 #define HBA_PORTSPEED_40GE		0x00020000	/* 40G E */
69 #define HBA_PORTSPEED_100GE		0x00040000	/* 100G E */
70 #define HBA_PORTSPEED_25GE		0x00080000	/* 25G E */
71 #define HBA_PORTSPEED_50GE		0x00100000	/* 50G E */
72 #define HBA_PORTSPEED_400GE		0x00200000	/* 400G E */
73 
74 #define FOURBYTES	4
75 
76 
77 static char *lpfc_release_version = LPFC_DRIVER_VERSION;
78 
79 static void
80 lpfc_ct_ignore_hbq_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
81 			  struct lpfc_dmabuf *mp, uint32_t size)
82 {
83 	if (!mp) {
84 		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
85 				"0146 Ignoring unsolicited CT No HBQ "
86 				"status = x%x\n",
87 				piocbq->iocb.ulpStatus);
88 	}
89 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
90 			"0145 Ignoring unsolicted CT HBQ Size:%d "
91 			"status = x%x\n",
92 			size, piocbq->iocb.ulpStatus);
93 }
94 
95 static void
96 lpfc_ct_unsol_buffer(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
97 		     struct lpfc_dmabuf *mp, uint32_t size)
98 {
99 	lpfc_ct_ignore_hbq_buffer(phba, piocbq, mp, size);
100 }
101 
102 /**
103  * lpfc_ct_unsol_cmpl : Completion callback function for unsol ct commands
104  * @phba : pointer to lpfc hba data structure.
105  * @cmdiocb : pointer to lpfc command iocb data structure.
106  * @rspiocb : pointer to lpfc response iocb data structure.
107  *
108  * This routine is the callback function for issuing unsol ct reject command.
109  * The memory allocated in the reject command path is freed up here.
110  **/
111 static void
112 lpfc_ct_unsol_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
113 		   struct lpfc_iocbq *rspiocb)
114 {
115 	struct lpfc_nodelist *ndlp;
116 	struct lpfc_dmabuf *mp, *bmp;
117 
118 	ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
119 	if (ndlp)
120 		lpfc_nlp_put(ndlp);
121 
122 	mp = cmdiocb->context2;
123 	bmp = cmdiocb->context3;
124 	if (mp) {
125 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
126 		kfree(mp);
127 		cmdiocb->context2 = NULL;
128 	}
129 
130 	if (bmp) {
131 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
132 		kfree(bmp);
133 		cmdiocb->context3 = NULL;
134 	}
135 
136 	lpfc_sli_release_iocbq(phba, cmdiocb);
137 }
138 
139 /**
140  * lpfc_ct_reject_event - Issue reject for unhandled CT MIB commands
141  * @ndlp: pointer to a node-list data structure.
142  * @ct_req: pointer to the CT request data structure.
143  * @rx_id: rx_id of the received UNSOL CT command
144  * @ox_id: ox_id of the UNSOL CT command
145  *
146  * This routine is invoked by the lpfc_ct_handle_mibreq routine for sending
147  * a reject response. Reject response is sent for the unhandled commands.
148  **/
149 static void
150 lpfc_ct_reject_event(struct lpfc_nodelist *ndlp,
151 		     struct lpfc_sli_ct_request *ct_req,
152 		     u16 rx_id, u16 ox_id)
153 {
154 	struct lpfc_vport *vport = ndlp->vport;
155 	struct lpfc_hba *phba = vport->phba;
156 	struct lpfc_sli_ct_request *ct_rsp;
157 	struct lpfc_iocbq *cmdiocbq = NULL;
158 	struct lpfc_dmabuf *bmp = NULL;
159 	struct lpfc_dmabuf *mp = NULL;
160 	struct ulp_bde64 *bpl;
161 	IOCB_t *icmd;
162 	u8 rc = 0;
163 
164 	/* fill in BDEs for command */
165 	mp = kmalloc(sizeof(*mp), GFP_KERNEL);
166 	if (!mp) {
167 		rc = 1;
168 		goto ct_exit;
169 	}
170 
171 	mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp->phys);
172 	if (!mp->virt) {
173 		rc = 2;
174 		goto ct_free_mp;
175 	}
176 
177 	/* Allocate buffer for Buffer ptr list */
178 	bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
179 	if (!bmp) {
180 		rc = 3;
181 		goto ct_free_mpvirt;
182 	}
183 
184 	bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &bmp->phys);
185 	if (!bmp->virt) {
186 		rc = 4;
187 		goto ct_free_bmp;
188 	}
189 
190 	INIT_LIST_HEAD(&mp->list);
191 	INIT_LIST_HEAD(&bmp->list);
192 
193 	bpl = (struct ulp_bde64 *)bmp->virt;
194 	memset(bpl, 0, sizeof(struct ulp_bde64));
195 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
196 	bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
197 	bpl->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
198 	bpl->tus.f.bdeSize = (LPFC_CT_PREAMBLE - 4);
199 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
200 
201 	ct_rsp = (struct lpfc_sli_ct_request *)mp->virt;
202 	memset(ct_rsp, 0, sizeof(struct lpfc_sli_ct_request));
203 
204 	ct_rsp->RevisionId.bits.Revision = SLI_CT_REVISION;
205 	ct_rsp->RevisionId.bits.InId = 0;
206 	ct_rsp->FsType = ct_req->FsType;
207 	ct_rsp->FsSubType = ct_req->FsSubType;
208 	ct_rsp->CommandResponse.bits.Size = 0;
209 	ct_rsp->CommandResponse.bits.CmdRsp =
210 		cpu_to_be16(SLI_CT_RESPONSE_FS_RJT);
211 	ct_rsp->ReasonCode = SLI_CT_REQ_NOT_SUPPORTED;
212 	ct_rsp->Explanation = SLI_CT_NO_ADDITIONAL_EXPL;
213 
214 	cmdiocbq = lpfc_sli_get_iocbq(phba);
215 	if (!cmdiocbq) {
216 		rc = 5;
217 		goto ct_free_bmpvirt;
218 	}
219 
220 	icmd = &cmdiocbq->iocb;
221 	icmd->un.genreq64.bdl.ulpIoTag32 = 0;
222 	icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
223 	icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
224 	icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
225 	icmd->un.genreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
226 	icmd->un.genreq64.w5.hcsw.Fctl = (LS | LA);
227 	icmd->un.genreq64.w5.hcsw.Dfctl = 0;
228 	icmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
229 	icmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
230 	icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
231 	icmd->ulpBdeCount = 1;
232 	icmd->ulpLe = 1;
233 	icmd->ulpClass = CLASS3;
234 
235 	/* Save for completion so we can release these resources */
236 	cmdiocbq->context1 = lpfc_nlp_get(ndlp);
237 	cmdiocbq->context2 = (uint8_t *)mp;
238 	cmdiocbq->context3 = (uint8_t *)bmp;
239 	cmdiocbq->iocb_cmpl = lpfc_ct_unsol_cmpl;
240 	icmd->ulpContext = rx_id;  /* Xri / rx_id */
241 	icmd->unsli3.rcvsli3.ox_id = ox_id;
242 	icmd->un.ulpWord[3] =
243 		phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
244 	icmd->ulpTimeout = (3 * phba->fc_ratov);
245 
246 	cmdiocbq->retry = 0;
247 	cmdiocbq->vport = vport;
248 	cmdiocbq->context_un.ndlp = NULL;
249 	cmdiocbq->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
250 
251 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
252 	if (!rc)
253 		return;
254 
255 	rc = 6;
256 	lpfc_nlp_put(ndlp);
257 	lpfc_sli_release_iocbq(phba, cmdiocbq);
258 ct_free_bmpvirt:
259 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
260 ct_free_bmp:
261 	kfree(bmp);
262 ct_free_mpvirt:
263 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
264 ct_free_mp:
265 	kfree(mp);
266 ct_exit:
267 	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
268 			 "6440 Unsol CT: Rsp err %d Data: x%x\n",
269 			 rc, vport->fc_flag);
270 }
271 
272 /**
273  * lpfc_ct_handle_mibreq - Process an unsolicited CT MIB request data buffer
274  * @phba: pointer to lpfc hba data structure.
275  * @ctiocbq: pointer to lpfc CT command iocb data structure.
276  *
277  * This routine is used for processing the IOCB associated with a unsolicited
278  * CT MIB request. It first determines whether there is an existing ndlp that
279  * matches the DID from the unsolicited IOCB. If not, it will return.
280  **/
281 static void
282 lpfc_ct_handle_mibreq(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocbq)
283 {
284 	struct lpfc_sli_ct_request *ct_req;
285 	struct lpfc_nodelist *ndlp = NULL;
286 	struct lpfc_vport *vport = NULL;
287 	IOCB_t *icmd = &ctiocbq->iocb;
288 	u32 mi_cmd, vpi;
289 	u32 did = 0;
290 
291 	vpi = ctiocbq->iocb.unsli3.rcvsli3.vpi;
292 	vport = lpfc_find_vport_by_vpid(phba, vpi);
293 	if (!vport) {
294 		lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
295 				"6437 Unsol CT: VPORT NULL vpi : x%x\n",
296 				vpi);
297 		return;
298 	}
299 
300 	did = ctiocbq->iocb.un.rcvels.remoteID;
301 	if (icmd->ulpStatus) {
302 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
303 				 "6438 Unsol CT: status:x%x/x%x did : x%x\n",
304 				 icmd->ulpStatus, icmd->un.ulpWord[4], did);
305 		return;
306 	}
307 
308 	/* Ignore traffic received during vport shutdown */
309 	if (vport->fc_flag & FC_UNLOADING)
310 		return;
311 
312 	ndlp = lpfc_findnode_did(vport, did);
313 	if (!ndlp) {
314 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
315 				 "6439 Unsol CT: NDLP Not Found for DID : x%x",
316 				 did);
317 		return;
318 	}
319 
320 	ct_req = ((struct lpfc_sli_ct_request *)
321 		 (((struct lpfc_dmabuf *)ctiocbq->context2)->virt));
322 
323 	mi_cmd = ct_req->CommandResponse.bits.CmdRsp;
324 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
325 			 "6442 : MI Cmd : x%x Not Supported\n", mi_cmd);
326 	lpfc_ct_reject_event(ndlp, ct_req,
327 			     ctiocbq->iocb.ulpContext,
328 			     ctiocbq->iocb.unsli3.rcvsli3.ox_id);
329 }
330 
331 /**
332  * lpfc_ct_unsol_event - Process an unsolicited event from a ct sli ring
333  * @phba: pointer to lpfc hba data structure.
334  * @pring: pointer to a SLI ring.
335  * @ctiocbq: pointer to lpfc ct iocb data structure.
336  *
337  * This routine is used to process an unsolicited event received from a SLI
338  * (Service Level Interface) ring. The actual processing of the data buffer
339  * associated with the unsolicited event is done by invoking appropriate routine
340  * after properly set up the iocb buffer from the SLI ring on which the
341  * unsolicited event was received.
342  **/
343 void
344 lpfc_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
345 		    struct lpfc_iocbq *ctiocbq)
346 {
347 	struct lpfc_dmabuf *mp = NULL;
348 	IOCB_t *icmd = &ctiocbq->iocb;
349 	int i;
350 	struct lpfc_iocbq *iocbq;
351 	dma_addr_t dma_addr;
352 	uint32_t size;
353 	struct list_head head;
354 	struct lpfc_sli_ct_request *ct_req;
355 	struct lpfc_dmabuf *bdeBuf1 = ctiocbq->context2;
356 	struct lpfc_dmabuf *bdeBuf2 = ctiocbq->context3;
357 
358 	ctiocbq->context1 = NULL;
359 	ctiocbq->context2 = NULL;
360 	ctiocbq->context3 = NULL;
361 
362 	if (unlikely(icmd->ulpStatus == IOSTAT_NEED_BUFFER)) {
363 		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
364 	} else if ((icmd->ulpStatus == IOSTAT_LOCAL_REJECT) &&
365 		   ((icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
366 		   IOERR_RCV_BUFFER_WAITING)) {
367 		/* Not enough posted buffers; Try posting more buffers */
368 		phba->fc_stat.NoRcvBuf++;
369 		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
370 			lpfc_post_buffer(phba, pring, 2);
371 		return;
372 	}
373 
374 	/* If there are no BDEs associated
375 	 * with this IOCB, there is nothing to do.
376 	 */
377 	if (icmd->ulpBdeCount == 0)
378 		return;
379 
380 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
381 		ctiocbq->context2 = bdeBuf1;
382 		if (icmd->ulpBdeCount == 2)
383 			ctiocbq->context3 = bdeBuf2;
384 	} else {
385 		dma_addr = getPaddr(icmd->un.cont64[0].addrHigh,
386 				    icmd->un.cont64[0].addrLow);
387 		ctiocbq->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
388 							     dma_addr);
389 		if (icmd->ulpBdeCount == 2) {
390 			dma_addr = getPaddr(icmd->un.cont64[1].addrHigh,
391 					    icmd->un.cont64[1].addrLow);
392 			ctiocbq->context3 = lpfc_sli_ringpostbuf_get(phba,
393 								     pring,
394 								     dma_addr);
395 		}
396 	}
397 
398 	ct_req = ((struct lpfc_sli_ct_request *)
399 		 (((struct lpfc_dmabuf *)ctiocbq->context2)->virt));
400 
401 	if (ct_req->FsType == SLI_CT_MANAGEMENT_SERVICE &&
402 	    ct_req->FsSubType == SLI_CT_MIB_Subtypes) {
403 		lpfc_ct_handle_mibreq(phba, ctiocbq);
404 	} else {
405 		if (!lpfc_bsg_ct_unsol_event(phba, pring, ctiocbq))
406 			return;
407 	}
408 
409 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
410 		INIT_LIST_HEAD(&head);
411 		list_add_tail(&head, &ctiocbq->list);
412 		list_for_each_entry(iocbq, &head, list) {
413 			icmd = &iocbq->iocb;
414 			if (icmd->ulpBdeCount == 0)
415 				continue;
416 			bdeBuf1 = iocbq->context2;
417 			iocbq->context2 = NULL;
418 			size  = icmd->un.cont64[0].tus.f.bdeSize;
419 			lpfc_ct_unsol_buffer(phba, ctiocbq, bdeBuf1, size);
420 			lpfc_in_buf_free(phba, bdeBuf1);
421 			if (icmd->ulpBdeCount == 2) {
422 				bdeBuf2 = iocbq->context3;
423 				iocbq->context3 = NULL;
424 				size  = icmd->unsli3.rcvsli3.bde2.tus.f.bdeSize;
425 				lpfc_ct_unsol_buffer(phba, ctiocbq, bdeBuf2,
426 						     size);
427 				lpfc_in_buf_free(phba, bdeBuf2);
428 			}
429 		}
430 		list_del(&head);
431 	} else {
432 		INIT_LIST_HEAD(&head);
433 		list_add_tail(&head, &ctiocbq->list);
434 		list_for_each_entry(iocbq, &head, list) {
435 			icmd = &iocbq->iocb;
436 			if (icmd->ulpBdeCount == 0)
437 				lpfc_ct_unsol_buffer(phba, iocbq, NULL, 0);
438 			for (i = 0; i < icmd->ulpBdeCount; i++) {
439 				dma_addr = getPaddr(icmd->un.cont64[i].addrHigh,
440 						    icmd->un.cont64[i].addrLow);
441 				mp = lpfc_sli_ringpostbuf_get(phba, pring,
442 							      dma_addr);
443 				size = icmd->un.cont64[i].tus.f.bdeSize;
444 				lpfc_ct_unsol_buffer(phba, iocbq, mp, size);
445 				lpfc_in_buf_free(phba, mp);
446 			}
447 			lpfc_post_buffer(phba, pring, i);
448 		}
449 		list_del(&head);
450 	}
451 }
452 
453 /**
454  * lpfc_ct_handle_unsol_abort - ct upper level protocol abort handler
455  * @phba: Pointer to HBA context object.
456  * @dmabuf: pointer to a dmabuf that describes the FC sequence
457  *
458  * This function serves as the upper level protocol abort handler for CT
459  * protocol.
460  *
461  * Return 1 if abort has been handled, 0 otherwise.
462  **/
463 int
464 lpfc_ct_handle_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
465 {
466 	int handled;
467 
468 	/* CT upper level goes through BSG */
469 	handled = lpfc_bsg_ct_unsol_abort(phba, dmabuf);
470 
471 	return handled;
472 }
473 
474 static void
475 lpfc_free_ct_rsp(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
476 {
477 	struct lpfc_dmabuf *mlast, *next_mlast;
478 
479 	list_for_each_entry_safe(mlast, next_mlast, &mlist->list, list) {
480 		lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
481 		list_del(&mlast->list);
482 		kfree(mlast);
483 	}
484 	lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
485 	kfree(mlist);
486 	return;
487 }
488 
489 static struct lpfc_dmabuf *
490 lpfc_alloc_ct_rsp(struct lpfc_hba *phba, int cmdcode, struct ulp_bde64 *bpl,
491 		  uint32_t size, int *entries)
492 {
493 	struct lpfc_dmabuf *mlist = NULL;
494 	struct lpfc_dmabuf *mp;
495 	int cnt, i = 0;
496 
497 	/* We get chunks of FCELSSIZE */
498 	cnt = size > FCELSSIZE ? FCELSSIZE: size;
499 
500 	while (size) {
501 		/* Allocate buffer for rsp payload */
502 		mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
503 		if (!mp) {
504 			if (mlist)
505 				lpfc_free_ct_rsp(phba, mlist);
506 			return NULL;
507 		}
508 
509 		INIT_LIST_HEAD(&mp->list);
510 
511 		if (cmdcode == be16_to_cpu(SLI_CTNS_GID_FT) ||
512 		    cmdcode == be16_to_cpu(SLI_CTNS_GFF_ID))
513 			mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
514 		else
515 			mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
516 
517 		if (!mp->virt) {
518 			kfree(mp);
519 			if (mlist)
520 				lpfc_free_ct_rsp(phba, mlist);
521 			return NULL;
522 		}
523 
524 		/* Queue it to a linked list */
525 		if (!mlist)
526 			mlist = mp;
527 		else
528 			list_add_tail(&mp->list, &mlist->list);
529 
530 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
531 		/* build buffer ptr list for IOCB */
532 		bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
533 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
534 		bpl->tus.f.bdeSize = (uint16_t) cnt;
535 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
536 		bpl++;
537 
538 		i++;
539 		size -= cnt;
540 	}
541 
542 	*entries = i;
543 	return mlist;
544 }
545 
546 int
547 lpfc_ct_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *ctiocb)
548 {
549 	struct lpfc_dmabuf *buf_ptr;
550 
551 	/* I/O job is complete so context is now invalid*/
552 	ctiocb->context_un.ndlp = NULL;
553 	if (ctiocb->context1) {
554 		buf_ptr = (struct lpfc_dmabuf *) ctiocb->context1;
555 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
556 		kfree(buf_ptr);
557 		ctiocb->context1 = NULL;
558 	}
559 	if (ctiocb->context2) {
560 		lpfc_free_ct_rsp(phba, (struct lpfc_dmabuf *) ctiocb->context2);
561 		ctiocb->context2 = NULL;
562 	}
563 
564 	if (ctiocb->context3) {
565 		buf_ptr = (struct lpfc_dmabuf *) ctiocb->context3;
566 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
567 		kfree(buf_ptr);
568 		ctiocb->context3 = NULL;
569 	}
570 	lpfc_sli_release_iocbq(phba, ctiocb);
571 	return 0;
572 }
573 
574 /*
575  * lpfc_gen_req - Build and issue a GEN_REQUEST command  to the SLI Layer
576  * @vport: pointer to a host virtual N_Port data structure.
577  * @bmp: Pointer to BPL for SLI command
578  * @inp: Pointer to data buffer for response data.
579  * @outp: Pointer to data buffer that hold the CT command.
580  * @cmpl: completion routine to call when command completes
581  * @ndlp: Destination NPort nodelist entry
582  *
583  * This function as the final part for issuing a CT command.
584  */
585 static int
586 lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
587 	     struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
588 	     void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
589 		     struct lpfc_iocbq *),
590 	     struct lpfc_nodelist *ndlp, uint32_t usr_flg, uint32_t num_entry,
591 	     uint32_t tmo, uint8_t retry)
592 {
593 	struct lpfc_hba  *phba = vport->phba;
594 	IOCB_t *icmd;
595 	struct lpfc_iocbq *geniocb;
596 	int rc;
597 
598 	/* Allocate buffer for  command iocb */
599 	geniocb = lpfc_sli_get_iocbq(phba);
600 
601 	if (geniocb == NULL)
602 		return 1;
603 
604 	icmd = &geniocb->iocb;
605 	icmd->un.genreq64.bdl.ulpIoTag32 = 0;
606 	icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
607 	icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
608 	icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
609 	icmd->un.genreq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
610 
611 	if (usr_flg)
612 		geniocb->context3 = NULL;
613 	else
614 		geniocb->context3 = (uint8_t *) bmp;
615 
616 	/* Save for completion so we can release these resources */
617 	geniocb->context1 = (uint8_t *) inp;
618 	geniocb->context2 = (uint8_t *) outp;
619 
620 	/* Fill in payload, bp points to frame payload */
621 	icmd->ulpCommand = CMD_GEN_REQUEST64_CR;
622 
623 	/* Fill in rest of iocb */
624 	icmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
625 	icmd->un.genreq64.w5.hcsw.Dfctl = 0;
626 	icmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
627 	icmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
628 
629 	if (!tmo) {
630 		 /* FC spec states we need 3 * ratov for CT requests */
631 		tmo = (3 * phba->fc_ratov);
632 	}
633 	icmd->ulpTimeout = tmo;
634 	icmd->ulpBdeCount = 1;
635 	icmd->ulpLe = 1;
636 	icmd->ulpClass = CLASS3;
637 	icmd->ulpContext = ndlp->nlp_rpi;
638 	if (phba->sli_rev == LPFC_SLI_REV4)
639 		icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
640 
641 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
642 		/* For GEN_REQUEST64_CR, use the RPI */
643 		icmd->ulpCt_h = 0;
644 		icmd->ulpCt_l = 0;
645 	}
646 
647 	/* Issue GEN REQ IOCB for NPORT <did> */
648 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
649 			 "0119 Issue GEN REQ IOCB to NPORT x%x "
650 			 "Data: x%x x%x\n",
651 			 ndlp->nlp_DID, icmd->ulpIoTag,
652 			 vport->port_state);
653 	geniocb->iocb_cmpl = cmpl;
654 	geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
655 	geniocb->vport = vport;
656 	geniocb->retry = retry;
657 	geniocb->context_un.ndlp = lpfc_nlp_get(ndlp);
658 	if (!geniocb->context_un.ndlp)
659 		goto out;
660 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, geniocb, 0);
661 
662 	if (rc == IOCB_ERROR) {
663 		geniocb->context_un.ndlp = NULL;
664 		lpfc_nlp_put(ndlp);
665 		goto out;
666 	}
667 
668 	return 0;
669 out:
670 	lpfc_sli_release_iocbq(phba, geniocb);
671 	return 1;
672 }
673 
674 /*
675  * lpfc_ct_cmd - Build and issue a CT command
676  * @vport: pointer to a host virtual N_Port data structure.
677  * @inmp: Pointer to data buffer for response data.
678  * @bmp: Pointer to BPL for SLI command
679  * @ndlp: Destination NPort nodelist entry
680  * @cmpl: completion routine to call when command completes
681  *
682  * This function is called for issuing a CT command.
683  */
684 static int
685 lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
686 	    struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
687 	    void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
688 			  struct lpfc_iocbq *),
689 	    uint32_t rsp_size, uint8_t retry)
690 {
691 	struct lpfc_hba  *phba = vport->phba;
692 	struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
693 	struct lpfc_dmabuf *outmp;
694 	int cnt = 0, status;
695 	int cmdcode = ((struct lpfc_sli_ct_request *) inmp->virt)->
696 		CommandResponse.bits.CmdRsp;
697 
698 	bpl++;			/* Skip past ct request */
699 
700 	/* Put buffer(s) for ct rsp in bpl */
701 	outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
702 	if (!outmp)
703 		return -ENOMEM;
704 	/*
705 	 * Form the CT IOCB.  The total number of BDEs in this IOCB
706 	 * is the single command plus response count from
707 	 * lpfc_alloc_ct_rsp.
708 	 */
709 	cnt += 1;
710 	status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp, 0,
711 			      cnt, 0, retry);
712 	if (status) {
713 		lpfc_free_ct_rsp(phba, outmp);
714 		return -ENOMEM;
715 	}
716 	return 0;
717 }
718 
719 struct lpfc_vport *
720 lpfc_find_vport_by_did(struct lpfc_hba *phba, uint32_t did) {
721 	struct lpfc_vport *vport_curr;
722 	unsigned long flags;
723 
724 	spin_lock_irqsave(&phba->port_list_lock, flags);
725 	list_for_each_entry(vport_curr, &phba->port_list, listentry) {
726 		if ((vport_curr->fc_myDID) && (vport_curr->fc_myDID == did)) {
727 			spin_unlock_irqrestore(&phba->port_list_lock, flags);
728 			return vport_curr;
729 		}
730 	}
731 	spin_unlock_irqrestore(&phba->port_list_lock, flags);
732 	return NULL;
733 }
734 
735 static void
736 lpfc_prep_node_fc4type(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
737 {
738 	struct lpfc_nodelist *ndlp;
739 
740 	if ((vport->port_type != LPFC_NPIV_PORT) ||
741 	    !(vport->ct_flags & FC_CT_RFF_ID) || !vport->cfg_restrict_login) {
742 
743 		ndlp = lpfc_setup_disc_node(vport, Did);
744 
745 		if (ndlp) {
746 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
747 				"Parse GID_FTrsp: did:x%x flg:x%x x%x",
748 				Did, ndlp->nlp_flag, vport->fc_flag);
749 
750 			/* By default, the driver expects to support FCP FC4 */
751 			if (fc4_type == FC_TYPE_FCP)
752 				ndlp->nlp_fc4_type |= NLP_FC4_FCP;
753 
754 			if (fc4_type == FC_TYPE_NVME)
755 				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
756 
757 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
758 					 "0238 Process x%06x NameServer Rsp "
759 					 "Data: x%x x%x x%x x%x x%x\n", Did,
760 					 ndlp->nlp_flag, ndlp->nlp_fc4_type,
761 					 ndlp->nlp_state, vport->fc_flag,
762 					 vport->fc_rscn_id_cnt);
763 
764 			/* if ndlp needs to be discovered and prior
765 			 * state of ndlp hit devloss, change state to
766 			 * allow rediscovery.
767 			 */
768 			if (ndlp->nlp_flag & NLP_NPR_2B_DISC &&
769 			    ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
770 				lpfc_nlp_set_state(vport, ndlp,
771 						   NLP_STE_NPR_NODE);
772 			}
773 		} else {
774 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
775 				"Skip1 GID_FTrsp: did:x%x flg:x%x cnt:%d",
776 				Did, vport->fc_flag, vport->fc_rscn_id_cnt);
777 
778 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
779 					 "0239 Skip x%06x NameServer Rsp "
780 					 "Data: x%x x%x x%px\n",
781 					 Did, vport->fc_flag,
782 					 vport->fc_rscn_id_cnt, ndlp);
783 		}
784 	} else {
785 		if (!(vport->fc_flag & FC_RSCN_MODE) ||
786 		    lpfc_rscn_payload_check(vport, Did)) {
787 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
788 				"Query GID_FTrsp: did:x%x flg:x%x cnt:%d",
789 				Did, vport->fc_flag, vport->fc_rscn_id_cnt);
790 
791 			/*
792 			 * This NPortID was previously a FCP/NVMe target,
793 			 * Don't even bother to send GFF_ID.
794 			 */
795 			ndlp = lpfc_findnode_did(vport, Did);
796 			if (ndlp &&
797 			    (ndlp->nlp_type &
798 			    (NLP_FCP_TARGET | NLP_NVME_TARGET))) {
799 				if (fc4_type == FC_TYPE_FCP)
800 					ndlp->nlp_fc4_type |= NLP_FC4_FCP;
801 				if (fc4_type == FC_TYPE_NVME)
802 					ndlp->nlp_fc4_type |= NLP_FC4_NVME;
803 				lpfc_setup_disc_node(vport, Did);
804 			} else if (lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
805 				   0, Did) == 0)
806 				vport->num_disc_nodes++;
807 			else
808 				lpfc_setup_disc_node(vport, Did);
809 		} else {
810 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
811 				"Skip2 GID_FTrsp: did:x%x flg:x%x cnt:%d",
812 				Did, vport->fc_flag, vport->fc_rscn_id_cnt);
813 
814 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
815 					 "0245 Skip x%06x NameServer Rsp "
816 					 "Data: x%x x%x\n", Did,
817 					 vport->fc_flag,
818 					 vport->fc_rscn_id_cnt);
819 		}
820 	}
821 }
822 
823 static void
824 lpfc_ns_rsp_audit_did(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
825 {
826 	struct lpfc_hba *phba = vport->phba;
827 	struct lpfc_nodelist *ndlp = NULL;
828 	char *str;
829 
830 	if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT)
831 		str = "GID_FT";
832 	else
833 		str = "GID_PT";
834 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
835 			 "6430 Process %s rsp for %08x type %x %s %s\n",
836 			 str, Did, fc4_type,
837 			 (fc4_type == FC_TYPE_FCP) ?  "FCP" : " ",
838 			 (fc4_type == FC_TYPE_NVME) ?  "NVME" : " ");
839 	/*
840 	 * To conserve rpi's, filter out addresses for other
841 	 * vports on the same physical HBAs.
842 	 */
843 	if (Did != vport->fc_myDID &&
844 	    (!lpfc_find_vport_by_did(phba, Did) ||
845 	     vport->cfg_peer_port_login)) {
846 		if (!phba->nvmet_support) {
847 			/* FCPI/NVMEI path. Process Did */
848 			lpfc_prep_node_fc4type(vport, Did, fc4_type);
849 			return;
850 		}
851 		/* NVMET path.  NVMET only cares about NVMEI nodes. */
852 		list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
853 			if (ndlp->nlp_type != NLP_NVME_INITIATOR ||
854 			    ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
855 				continue;
856 			spin_lock_irq(&ndlp->lock);
857 			if (ndlp->nlp_DID == Did)
858 				ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
859 			else
860 				ndlp->nlp_flag |= NLP_NVMET_RECOV;
861 			spin_unlock_irq(&ndlp->lock);
862 		}
863 	}
864 }
865 
866 static int
867 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint8_t fc4_type,
868 	    uint32_t Size)
869 {
870 	struct lpfc_sli_ct_request *Response =
871 		(struct lpfc_sli_ct_request *) mp->virt;
872 	struct lpfc_dmabuf *mlast, *next_mp;
873 	uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
874 	uint32_t Did, CTentry;
875 	int Cnt;
876 	struct list_head head;
877 	struct lpfc_nodelist *ndlp = NULL;
878 
879 	lpfc_set_disctmo(vport);
880 	vport->num_disc_nodes = 0;
881 	vport->fc_ns_retry = 0;
882 
883 
884 	list_add_tail(&head, &mp->list);
885 	list_for_each_entry_safe(mp, next_mp, &head, list) {
886 		mlast = mp;
887 
888 		Cnt = Size  > FCELSSIZE ? FCELSSIZE : Size;
889 
890 		Size -= Cnt;
891 
892 		if (!ctptr) {
893 			ctptr = (uint32_t *) mlast->virt;
894 		} else
895 			Cnt -= 16;	/* subtract length of CT header */
896 
897 		/* Loop through entire NameServer list of DIDs */
898 		while (Cnt >= sizeof(uint32_t)) {
899 			/* Get next DID from NameServer List */
900 			CTentry = *ctptr++;
901 			Did = ((be32_to_cpu(CTentry)) & Mask_DID);
902 			lpfc_ns_rsp_audit_did(vport, Did, fc4_type);
903 			if (CTentry & (cpu_to_be32(SLI_CT_LAST_ENTRY)))
904 				goto nsout1;
905 
906 			Cnt -= sizeof(uint32_t);
907 		}
908 		ctptr = NULL;
909 
910 	}
911 
912 	/* All GID_FT entries processed.  If the driver is running in
913 	 * in target mode, put impacted nodes into recovery and drop
914 	 * the RPI to flush outstanding IO.
915 	 */
916 	if (vport->phba->nvmet_support) {
917 		list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
918 			if (!(ndlp->nlp_flag & NLP_NVMET_RECOV))
919 				continue;
920 			lpfc_disc_state_machine(vport, ndlp, NULL,
921 						NLP_EVT_DEVICE_RECOVERY);
922 			spin_lock_irq(&ndlp->lock);
923 			ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
924 			spin_unlock_irq(&ndlp->lock);
925 		}
926 	}
927 
928 nsout1:
929 	list_del(&head);
930 	return 0;
931 }
932 
933 static void
934 lpfc_cmpl_ct_cmd_gid_ft(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
935 			struct lpfc_iocbq *rspiocb)
936 {
937 	struct lpfc_vport *vport = cmdiocb->vport;
938 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
939 	IOCB_t *irsp;
940 	struct lpfc_dmabuf *outp;
941 	struct lpfc_dmabuf *inp;
942 	struct lpfc_sli_ct_request *CTrsp;
943 	struct lpfc_sli_ct_request *CTreq;
944 	struct lpfc_nodelist *ndlp;
945 	int rc, type;
946 
947 	/* First save ndlp, before we overwrite it */
948 	ndlp = cmdiocb->context_un.ndlp;
949 
950 	/* we pass cmdiocb to state machine which needs rspiocb as well */
951 	cmdiocb->context_un.rsp_iocb = rspiocb;
952 	inp = (struct lpfc_dmabuf *) cmdiocb->context1;
953 	outp = (struct lpfc_dmabuf *) cmdiocb->context2;
954 	irsp = &rspiocb->iocb;
955 
956 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
957 		 "GID_FT cmpl:     status:x%x/x%x rtry:%d",
958 		irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_ns_retry);
959 
960 	/* Don't bother processing response if vport is being torn down. */
961 	if (vport->load_flag & FC_UNLOADING) {
962 		if (vport->fc_flag & FC_RSCN_MODE)
963 			lpfc_els_flush_rscn(vport);
964 		goto out;
965 	}
966 
967 	if (lpfc_els_chk_latt(vport)) {
968 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
969 				 "0216 Link event during NS query\n");
970 		if (vport->fc_flag & FC_RSCN_MODE)
971 			lpfc_els_flush_rscn(vport);
972 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
973 		goto out;
974 	}
975 	if (lpfc_error_lost_link(irsp)) {
976 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
977 				 "0226 NS query failed due to link event\n");
978 		if (vport->fc_flag & FC_RSCN_MODE)
979 			lpfc_els_flush_rscn(vport);
980 		goto out;
981 	}
982 
983 	spin_lock_irq(shost->host_lock);
984 	if (vport->fc_flag & FC_RSCN_DEFERRED) {
985 		vport->fc_flag &= ~FC_RSCN_DEFERRED;
986 		spin_unlock_irq(shost->host_lock);
987 
988 		/* This is a GID_FT completing so the gidft_inp counter was
989 		 * incremented before the GID_FT was issued to the wire.
990 		 */
991 		if (vport->gidft_inp)
992 			vport->gidft_inp--;
993 
994 		/*
995 		 * Skip processing the NS response
996 		 * Re-issue the NS cmd
997 		 */
998 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
999 				 "0151 Process Deferred RSCN Data: x%x x%x\n",
1000 				 vport->fc_flag, vport->fc_rscn_id_cnt);
1001 		lpfc_els_handle_rscn(vport);
1002 
1003 		goto out;
1004 	}
1005 	spin_unlock_irq(shost->host_lock);
1006 
1007 	if (irsp->ulpStatus) {
1008 		/* Check for retry */
1009 		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
1010 			if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
1011 			    (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1012 			    IOERR_NO_RESOURCES)
1013 				vport->fc_ns_retry++;
1014 
1015 			type = lpfc_get_gidft_type(vport, cmdiocb);
1016 			if (type == 0)
1017 				goto out;
1018 
1019 			/* CT command is being retried */
1020 			rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
1021 					 vport->fc_ns_retry, type);
1022 			if (rc == 0)
1023 				goto out;
1024 			else { /* Unable to send NS cmd */
1025 				if (vport->gidft_inp)
1026 					vport->gidft_inp--;
1027 			}
1028 		}
1029 		if (vport->fc_flag & FC_RSCN_MODE)
1030 			lpfc_els_flush_rscn(vport);
1031 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1032 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1033 				 "0257 GID_FT Query error: 0x%x 0x%x\n",
1034 				 irsp->ulpStatus, vport->fc_ns_retry);
1035 	} else {
1036 		/* Good status, continue checking */
1037 		CTreq = (struct lpfc_sli_ct_request *) inp->virt;
1038 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1039 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1040 		    cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
1041 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1042 					 "0208 NameServer Rsp Data: x%x x%x "
1043 					 "x%x x%x sz x%x\n",
1044 					 vport->fc_flag,
1045 					 CTreq->un.gid.Fc4Type,
1046 					 vport->num_disc_nodes,
1047 					 vport->gidft_inp,
1048 					 irsp->un.genreq64.bdl.bdeSize);
1049 
1050 			lpfc_ns_rsp(vport,
1051 				    outp,
1052 				    CTreq->un.gid.Fc4Type,
1053 				    (uint32_t) (irsp->un.genreq64.bdl.bdeSize));
1054 		} else if (CTrsp->CommandResponse.bits.CmdRsp ==
1055 			   be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
1056 			/* NameServer Rsp Error */
1057 			if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
1058 			    && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
1059 				lpfc_printf_vlog(vport, KERN_INFO,
1060 					LOG_DISCOVERY,
1061 					"0269 No NameServer Entries "
1062 					"Data: x%x x%x x%x x%x\n",
1063 					CTrsp->CommandResponse.bits.CmdRsp,
1064 					(uint32_t) CTrsp->ReasonCode,
1065 					(uint32_t) CTrsp->Explanation,
1066 					vport->fc_flag);
1067 
1068 				lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1069 				"GID_FT no entry  cmd:x%x rsn:x%x exp:x%x",
1070 				(uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1071 				(uint32_t) CTrsp->ReasonCode,
1072 				(uint32_t) CTrsp->Explanation);
1073 			} else {
1074 				lpfc_printf_vlog(vport, KERN_INFO,
1075 					LOG_DISCOVERY,
1076 					"0240 NameServer Rsp Error "
1077 					"Data: x%x x%x x%x x%x\n",
1078 					CTrsp->CommandResponse.bits.CmdRsp,
1079 					(uint32_t) CTrsp->ReasonCode,
1080 					(uint32_t) CTrsp->Explanation,
1081 					vport->fc_flag);
1082 
1083 				lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1084 				"GID_FT rsp err1  cmd:x%x rsn:x%x exp:x%x",
1085 				(uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1086 				(uint32_t) CTrsp->ReasonCode,
1087 				(uint32_t) CTrsp->Explanation);
1088 			}
1089 
1090 
1091 		} else {
1092 			/* NameServer Rsp Error */
1093 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1094 					"0241 NameServer Rsp Error "
1095 					"Data: x%x x%x x%x x%x\n",
1096 					CTrsp->CommandResponse.bits.CmdRsp,
1097 					(uint32_t) CTrsp->ReasonCode,
1098 					(uint32_t) CTrsp->Explanation,
1099 					vport->fc_flag);
1100 
1101 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1102 				"GID_FT rsp err2  cmd:x%x rsn:x%x exp:x%x",
1103 				(uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1104 				(uint32_t) CTrsp->ReasonCode,
1105 				(uint32_t) CTrsp->Explanation);
1106 		}
1107 		if (vport->gidft_inp)
1108 			vport->gidft_inp--;
1109 	}
1110 
1111 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1112 			 "4216 GID_FT cmpl inp %d disc %d\n",
1113 			 vport->gidft_inp, vport->num_disc_nodes);
1114 
1115 	/* Link up / RSCN discovery */
1116 	if ((vport->num_disc_nodes == 0) &&
1117 	    (vport->gidft_inp == 0)) {
1118 		/*
1119 		 * The driver has cycled through all Nports in the RSCN payload.
1120 		 * Complete the handling by cleaning up and marking the
1121 		 * current driver state.
1122 		 */
1123 		if (vport->port_state >= LPFC_DISC_AUTH) {
1124 			if (vport->fc_flag & FC_RSCN_MODE) {
1125 				lpfc_els_flush_rscn(vport);
1126 				spin_lock_irq(shost->host_lock);
1127 				vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1128 				spin_unlock_irq(shost->host_lock);
1129 			}
1130 			else
1131 				lpfc_els_flush_rscn(vport);
1132 		}
1133 
1134 		lpfc_disc_start(vport);
1135 	}
1136 out:
1137 	lpfc_ct_free_iocb(phba, cmdiocb);
1138 	lpfc_nlp_put(ndlp);
1139 	return;
1140 }
1141 
1142 static void
1143 lpfc_cmpl_ct_cmd_gid_pt(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1144 			struct lpfc_iocbq *rspiocb)
1145 {
1146 	struct lpfc_vport *vport = cmdiocb->vport;
1147 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1148 	IOCB_t *irsp;
1149 	struct lpfc_dmabuf *outp;
1150 	struct lpfc_dmabuf *inp;
1151 	struct lpfc_sli_ct_request *CTrsp;
1152 	struct lpfc_sli_ct_request *CTreq;
1153 	struct lpfc_nodelist *ndlp;
1154 	int rc;
1155 
1156 	/* First save ndlp, before we overwrite it */
1157 	ndlp = cmdiocb->context_un.ndlp;
1158 
1159 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1160 	cmdiocb->context_un.rsp_iocb = rspiocb;
1161 	inp = (struct lpfc_dmabuf *)cmdiocb->context1;
1162 	outp = (struct lpfc_dmabuf *)cmdiocb->context2;
1163 	irsp = &rspiocb->iocb;
1164 
1165 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1166 			      "GID_PT cmpl:     status:x%x/x%x rtry:%d",
1167 			      irsp->ulpStatus, irsp->un.ulpWord[4],
1168 			      vport->fc_ns_retry);
1169 
1170 	/* Don't bother processing response if vport is being torn down. */
1171 	if (vport->load_flag & FC_UNLOADING) {
1172 		if (vport->fc_flag & FC_RSCN_MODE)
1173 			lpfc_els_flush_rscn(vport);
1174 		goto out;
1175 	}
1176 
1177 	if (lpfc_els_chk_latt(vport)) {
1178 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1179 				 "4108 Link event during NS query\n");
1180 		if (vport->fc_flag & FC_RSCN_MODE)
1181 			lpfc_els_flush_rscn(vport);
1182 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1183 		goto out;
1184 	}
1185 	if (lpfc_error_lost_link(irsp)) {
1186 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1187 				 "4166 NS query failed due to link event\n");
1188 		if (vport->fc_flag & FC_RSCN_MODE)
1189 			lpfc_els_flush_rscn(vport);
1190 		goto out;
1191 	}
1192 
1193 	spin_lock_irq(shost->host_lock);
1194 	if (vport->fc_flag & FC_RSCN_DEFERRED) {
1195 		vport->fc_flag &= ~FC_RSCN_DEFERRED;
1196 		spin_unlock_irq(shost->host_lock);
1197 
1198 		/* This is a GID_PT completing so the gidft_inp counter was
1199 		 * incremented before the GID_PT was issued to the wire.
1200 		 */
1201 		if (vport->gidft_inp)
1202 			vport->gidft_inp--;
1203 
1204 		/*
1205 		 * Skip processing the NS response
1206 		 * Re-issue the NS cmd
1207 		 */
1208 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1209 				 "4167 Process Deferred RSCN Data: x%x x%x\n",
1210 				 vport->fc_flag, vport->fc_rscn_id_cnt);
1211 		lpfc_els_handle_rscn(vport);
1212 
1213 		goto out;
1214 	}
1215 	spin_unlock_irq(shost->host_lock);
1216 
1217 	if (irsp->ulpStatus) {
1218 		/* Check for retry */
1219 		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
1220 			if (irsp->ulpStatus != IOSTAT_LOCAL_REJECT ||
1221 			    (irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1222 			    IOERR_NO_RESOURCES)
1223 				vport->fc_ns_retry++;
1224 
1225 			/* CT command is being retried */
1226 			rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_PT,
1227 					 vport->fc_ns_retry, GID_PT_N_PORT);
1228 			if (rc == 0)
1229 				goto out;
1230 			else { /* Unable to send NS cmd */
1231 				if (vport->gidft_inp)
1232 					vport->gidft_inp--;
1233 			}
1234 		}
1235 		if (vport->fc_flag & FC_RSCN_MODE)
1236 			lpfc_els_flush_rscn(vport);
1237 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1238 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1239 				 "4103 GID_FT Query error: 0x%x 0x%x\n",
1240 				 irsp->ulpStatus, vport->fc_ns_retry);
1241 	} else {
1242 		/* Good status, continue checking */
1243 		CTreq = (struct lpfc_sli_ct_request *)inp->virt;
1244 		CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
1245 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1246 		    cpu_to_be16(SLI_CT_RESPONSE_FS_ACC)) {
1247 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1248 					 "4105 NameServer Rsp Data: x%x x%x "
1249 					 "x%x x%x sz x%x\n",
1250 					 vport->fc_flag,
1251 					 CTreq->un.gid.Fc4Type,
1252 					 vport->num_disc_nodes,
1253 					 vport->gidft_inp,
1254 					 irsp->un.genreq64.bdl.bdeSize);
1255 
1256 			lpfc_ns_rsp(vport,
1257 				    outp,
1258 				    CTreq->un.gid.Fc4Type,
1259 				    (uint32_t)(irsp->un.genreq64.bdl.bdeSize));
1260 		} else if (CTrsp->CommandResponse.bits.CmdRsp ==
1261 			   be16_to_cpu(SLI_CT_RESPONSE_FS_RJT)) {
1262 			/* NameServer Rsp Error */
1263 			if ((CTrsp->ReasonCode == SLI_CT_UNABLE_TO_PERFORM_REQ)
1264 			    && (CTrsp->Explanation == SLI_CT_NO_FC4_TYPES)) {
1265 				lpfc_printf_vlog(
1266 					vport, KERN_INFO, LOG_DISCOVERY,
1267 					"4106 No NameServer Entries "
1268 					"Data: x%x x%x x%x x%x\n",
1269 					CTrsp->CommandResponse.bits.CmdRsp,
1270 					(uint32_t)CTrsp->ReasonCode,
1271 					(uint32_t)CTrsp->Explanation,
1272 					vport->fc_flag);
1273 
1274 				lpfc_debugfs_disc_trc(
1275 				vport, LPFC_DISC_TRC_CT,
1276 				"GID_PT no entry  cmd:x%x rsn:x%x exp:x%x",
1277 				(uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1278 				(uint32_t)CTrsp->ReasonCode,
1279 				(uint32_t)CTrsp->Explanation);
1280 			} else {
1281 				lpfc_printf_vlog(
1282 					vport, KERN_INFO, LOG_DISCOVERY,
1283 					"4107 NameServer Rsp Error "
1284 					"Data: x%x x%x x%x x%x\n",
1285 					CTrsp->CommandResponse.bits.CmdRsp,
1286 					(uint32_t)CTrsp->ReasonCode,
1287 					(uint32_t)CTrsp->Explanation,
1288 					vport->fc_flag);
1289 
1290 				lpfc_debugfs_disc_trc(
1291 				vport, LPFC_DISC_TRC_CT,
1292 				"GID_PT rsp err1  cmd:x%x rsn:x%x exp:x%x",
1293 				(uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1294 				(uint32_t)CTrsp->ReasonCode,
1295 				(uint32_t)CTrsp->Explanation);
1296 			}
1297 		} else {
1298 			/* NameServer Rsp Error */
1299 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1300 					 "4109 NameServer Rsp Error "
1301 					 "Data: x%x x%x x%x x%x\n",
1302 					 CTrsp->CommandResponse.bits.CmdRsp,
1303 					 (uint32_t)CTrsp->ReasonCode,
1304 					 (uint32_t)CTrsp->Explanation,
1305 					 vport->fc_flag);
1306 
1307 			lpfc_debugfs_disc_trc(
1308 				vport, LPFC_DISC_TRC_CT,
1309 				"GID_PT rsp err2  cmd:x%x rsn:x%x exp:x%x",
1310 				(uint32_t)CTrsp->CommandResponse.bits.CmdRsp,
1311 				(uint32_t)CTrsp->ReasonCode,
1312 				(uint32_t)CTrsp->Explanation);
1313 		}
1314 		if (vport->gidft_inp)
1315 			vport->gidft_inp--;
1316 	}
1317 
1318 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1319 			 "6450 GID_PT cmpl inp %d disc %d\n",
1320 			 vport->gidft_inp, vport->num_disc_nodes);
1321 
1322 	/* Link up / RSCN discovery */
1323 	if ((vport->num_disc_nodes == 0) &&
1324 	    (vport->gidft_inp == 0)) {
1325 		/*
1326 		 * The driver has cycled through all Nports in the RSCN payload.
1327 		 * Complete the handling by cleaning up and marking the
1328 		 * current driver state.
1329 		 */
1330 		if (vport->port_state >= LPFC_DISC_AUTH) {
1331 			if (vport->fc_flag & FC_RSCN_MODE) {
1332 				lpfc_els_flush_rscn(vport);
1333 				spin_lock_irq(shost->host_lock);
1334 				vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1335 				spin_unlock_irq(shost->host_lock);
1336 			} else {
1337 				lpfc_els_flush_rscn(vport);
1338 			}
1339 		}
1340 
1341 		lpfc_disc_start(vport);
1342 	}
1343 out:
1344 	lpfc_ct_free_iocb(phba, cmdiocb);
1345 	lpfc_nlp_put(ndlp);
1346 }
1347 
1348 static void
1349 lpfc_cmpl_ct_cmd_gff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1350 			struct lpfc_iocbq *rspiocb)
1351 {
1352 	struct lpfc_vport *vport = cmdiocb->vport;
1353 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1354 	IOCB_t *irsp = &rspiocb->iocb;
1355 	struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1356 	struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1357 	struct lpfc_sli_ct_request *CTrsp;
1358 	int did, rc, retry;
1359 	uint8_t fbits;
1360 	struct lpfc_nodelist *ndlp = NULL, *free_ndlp = NULL;
1361 
1362 	did = ((struct lpfc_sli_ct_request *) inp->virt)->un.gff.PortId;
1363 	did = be32_to_cpu(did);
1364 
1365 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1366 		"GFF_ID cmpl:     status:x%x/x%x did:x%x",
1367 		irsp->ulpStatus, irsp->un.ulpWord[4], did);
1368 
1369 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1370 		/* Good status, continue checking */
1371 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1372 		fbits = CTrsp->un.gff_acc.fbits[FCP_TYPE_FEATURE_OFFSET];
1373 
1374 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1375 				 "6431 Process GFF_ID rsp for %08x "
1376 				 "fbits %02x %s %s\n",
1377 				 did, fbits,
1378 				 (fbits & FC4_FEATURE_INIT) ? "Initiator" : " ",
1379 				 (fbits & FC4_FEATURE_TARGET) ? "Target" : " ");
1380 
1381 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1382 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC)) {
1383 			if ((fbits & FC4_FEATURE_INIT) &&
1384 			    !(fbits & FC4_FEATURE_TARGET)) {
1385 				lpfc_printf_vlog(vport, KERN_INFO,
1386 						 LOG_DISCOVERY,
1387 						 "0270 Skip x%x GFF "
1388 						 "NameServer Rsp Data: (init) "
1389 						 "x%x x%x\n", did, fbits,
1390 						 vport->fc_rscn_id_cnt);
1391 				goto out;
1392 			}
1393 		}
1394 	}
1395 	else {
1396 		/* Check for retry */
1397 		if (cmdiocb->retry < LPFC_MAX_NS_RETRY) {
1398 			retry = 1;
1399 			if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1400 				switch ((irsp->un.ulpWord[4] &
1401 					IOERR_PARAM_MASK)) {
1402 
1403 				case IOERR_NO_RESOURCES:
1404 					/* We don't increment the retry
1405 					 * count for this case.
1406 					 */
1407 					break;
1408 				case IOERR_LINK_DOWN:
1409 				case IOERR_SLI_ABORTED:
1410 				case IOERR_SLI_DOWN:
1411 					retry = 0;
1412 					break;
1413 				default:
1414 					cmdiocb->retry++;
1415 				}
1416 			}
1417 			else
1418 				cmdiocb->retry++;
1419 
1420 			if (retry) {
1421 				/* CT command is being retried */
1422 				rc = lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
1423 					 cmdiocb->retry, did);
1424 				if (rc == 0) {
1425 					/* success */
1426 					free_ndlp = cmdiocb->context_un.ndlp;
1427 					lpfc_ct_free_iocb(phba, cmdiocb);
1428 					lpfc_nlp_put(free_ndlp);
1429 					return;
1430 				}
1431 			}
1432 		}
1433 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1434 				 "0267 NameServer GFF Rsp "
1435 				 "x%x Error (%d %d) Data: x%x x%x\n",
1436 				 did, irsp->ulpStatus, irsp->un.ulpWord[4],
1437 				 vport->fc_flag, vport->fc_rscn_id_cnt);
1438 	}
1439 
1440 	/* This is a target port, unregistered port, or the GFF_ID failed */
1441 	ndlp = lpfc_setup_disc_node(vport, did);
1442 	if (ndlp) {
1443 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1444 				 "0242 Process x%x GFF "
1445 				 "NameServer Rsp Data: x%x x%x x%x\n",
1446 				 did, ndlp->nlp_flag, vport->fc_flag,
1447 				 vport->fc_rscn_id_cnt);
1448 	} else {
1449 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1450 				 "0243 Skip x%x GFF "
1451 				 "NameServer Rsp Data: x%x x%x\n", did,
1452 				 vport->fc_flag, vport->fc_rscn_id_cnt);
1453 	}
1454 out:
1455 	/* Link up / RSCN discovery */
1456 	if (vport->num_disc_nodes)
1457 		vport->num_disc_nodes--;
1458 
1459 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1460 			 "6451 GFF_ID cmpl inp %d disc %d\n",
1461 			 vport->gidft_inp, vport->num_disc_nodes);
1462 
1463 	if (vport->num_disc_nodes == 0) {
1464 		/*
1465 		 * The driver has cycled through all Nports in the RSCN payload.
1466 		 * Complete the handling by cleaning up and marking the
1467 		 * current driver state.
1468 		 */
1469 		if (vport->port_state >= LPFC_DISC_AUTH) {
1470 			if (vport->fc_flag & FC_RSCN_MODE) {
1471 				lpfc_els_flush_rscn(vport);
1472 				spin_lock_irq(shost->host_lock);
1473 				vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1474 				spin_unlock_irq(shost->host_lock);
1475 			}
1476 			else
1477 				lpfc_els_flush_rscn(vport);
1478 		}
1479 		lpfc_disc_start(vport);
1480 	}
1481 
1482 	free_ndlp = cmdiocb->context_un.ndlp;
1483 	lpfc_ct_free_iocb(phba, cmdiocb);
1484 	lpfc_nlp_put(free_ndlp);
1485 	return;
1486 }
1487 
1488 static void
1489 lpfc_cmpl_ct_cmd_gft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1490 				struct lpfc_iocbq *rspiocb)
1491 {
1492 	struct lpfc_vport *vport = cmdiocb->vport;
1493 	IOCB_t *irsp = &rspiocb->iocb;
1494 	struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *)cmdiocb->context1;
1495 	struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *)cmdiocb->context2;
1496 	struct lpfc_sli_ct_request *CTrsp;
1497 	int did;
1498 	struct lpfc_nodelist *ndlp = NULL;
1499 	struct lpfc_nodelist *ns_ndlp = NULL;
1500 	uint32_t fc4_data_0, fc4_data_1;
1501 
1502 	did = ((struct lpfc_sli_ct_request *)inp->virt)->un.gft.PortId;
1503 	did = be32_to_cpu(did);
1504 
1505 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1506 			      "GFT_ID cmpl: status:x%x/x%x did:x%x",
1507 			      irsp->ulpStatus, irsp->un.ulpWord[4], did);
1508 
1509 	/* Preserve the nameserver node to release the reference. */
1510 	ns_ndlp = cmdiocb->context_un.ndlp;
1511 
1512 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1513 		/* Good status, continue checking */
1514 		CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
1515 		fc4_data_0 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[0]);
1516 		fc4_data_1 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[1]);
1517 
1518 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1519 				 "6432 Process GFT_ID rsp for %08x "
1520 				 "Data %08x %08x %s %s\n",
1521 				 did, fc4_data_0, fc4_data_1,
1522 				 (fc4_data_0 & LPFC_FC4_TYPE_BITMASK) ?
1523 				  "FCP" : " ",
1524 				 (fc4_data_1 & LPFC_FC4_TYPE_BITMASK) ?
1525 				  "NVME" : " ");
1526 
1527 		/* Lookup the NPort_ID queried in the GFT_ID and find the
1528 		 * driver's local node.  It's an error if the driver
1529 		 * doesn't have one.
1530 		 */
1531 		ndlp = lpfc_findnode_did(vport, did);
1532 		if (ndlp) {
1533 			/* The bitmask value for FCP and NVME FCP types is
1534 			 * the same because they are 32 bits distant from
1535 			 * each other in word0 and word0.
1536 			 */
1537 			if (fc4_data_0 & LPFC_FC4_TYPE_BITMASK)
1538 				ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1539 			if (fc4_data_1 &  LPFC_FC4_TYPE_BITMASK)
1540 				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1541 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1542 					 "3064 Setting ndlp x%px, DID x%06x "
1543 					 "with FC4 x%08x, Data: x%08x x%08x "
1544 					 "%d\n",
1545 					 ndlp, did, ndlp->nlp_fc4_type,
1546 					 FC_TYPE_FCP, FC_TYPE_NVME,
1547 					 ndlp->nlp_state);
1548 
1549 			if (ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE &&
1550 			    ndlp->nlp_fc4_type) {
1551 				ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1552 
1553 				lpfc_nlp_set_state(vport, ndlp,
1554 						   NLP_STE_PRLI_ISSUE);
1555 				lpfc_issue_els_prli(vport, ndlp, 0);
1556 			} else if (!ndlp->nlp_fc4_type) {
1557 				/* If fc4 type is still unknown, then LOGO */
1558 				lpfc_printf_vlog(vport, KERN_INFO,
1559 						 LOG_DISCOVERY,
1560 						 "6443 Sending LOGO ndlp x%px,"
1561 						 "DID x%06x with fc4_type: "
1562 						 "x%08x, state: %d\n",
1563 						 ndlp, did, ndlp->nlp_fc4_type,
1564 						 ndlp->nlp_state);
1565 				lpfc_issue_els_logo(vport, ndlp, 0);
1566 				ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1567 				lpfc_nlp_set_state(vport, ndlp,
1568 						   NLP_STE_NPR_NODE);
1569 			}
1570 		}
1571 	} else
1572 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1573 				 "3065 GFT_ID failed x%08x\n", irsp->ulpStatus);
1574 
1575 	lpfc_ct_free_iocb(phba, cmdiocb);
1576 	lpfc_nlp_put(ns_ndlp);
1577 }
1578 
1579 static void
1580 lpfc_cmpl_ct(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1581 	     struct lpfc_iocbq *rspiocb)
1582 {
1583 	struct lpfc_vport *vport = cmdiocb->vport;
1584 	struct lpfc_dmabuf *inp;
1585 	struct lpfc_dmabuf *outp;
1586 	IOCB_t *irsp;
1587 	struct lpfc_sli_ct_request *CTrsp;
1588 	struct lpfc_nodelist *ndlp;
1589 	int cmdcode, rc;
1590 	uint8_t retry;
1591 	uint32_t latt;
1592 
1593 	/* First save ndlp, before we overwrite it */
1594 	ndlp = cmdiocb->context_un.ndlp;
1595 
1596 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1597 	cmdiocb->context_un.rsp_iocb = rspiocb;
1598 
1599 	inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1600 	outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1601 	irsp = &rspiocb->iocb;
1602 
1603 	cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
1604 					CommandResponse.bits.CmdRsp);
1605 	CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1606 
1607 	latt = lpfc_els_chk_latt(vport);
1608 
1609 	/* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
1610 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1611 			 "0209 CT Request completes, latt %d, "
1612 			 "ulpStatus x%x CmdRsp x%x, Context x%x, Tag x%x\n",
1613 			 latt, irsp->ulpStatus,
1614 			 CTrsp->CommandResponse.bits.CmdRsp,
1615 			 cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
1616 
1617 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1618 		"CT cmd cmpl:     status:x%x/x%x cmd:x%x",
1619 		irsp->ulpStatus, irsp->un.ulpWord[4], cmdcode);
1620 
1621 	if (irsp->ulpStatus) {
1622 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1623 				 "0268 NS cmd x%x Error (x%x x%x)\n",
1624 				 cmdcode, irsp->ulpStatus, irsp->un.ulpWord[4]);
1625 
1626 		if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1627 			(((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1628 			  IOERR_SLI_DOWN) ||
1629 			 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1630 			  IOERR_SLI_ABORTED)))
1631 			goto out;
1632 
1633 		retry = cmdiocb->retry;
1634 		if (retry >= LPFC_MAX_NS_RETRY)
1635 			goto out;
1636 
1637 		retry++;
1638 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1639 				 "0250 Retrying NS cmd %x\n", cmdcode);
1640 		rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
1641 		if (rc == 0)
1642 			goto out;
1643 	}
1644 
1645 out:
1646 	lpfc_ct_free_iocb(phba, cmdiocb);
1647 	lpfc_nlp_put(ndlp);
1648 	return;
1649 }
1650 
1651 static void
1652 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1653 			struct lpfc_iocbq *rspiocb)
1654 {
1655 	IOCB_t *irsp = &rspiocb->iocb;
1656 	struct lpfc_vport *vport = cmdiocb->vport;
1657 
1658 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1659 		struct lpfc_dmabuf *outp;
1660 		struct lpfc_sli_ct_request *CTrsp;
1661 
1662 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1663 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1664 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1665 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1666 			vport->ct_flags |= FC_CT_RFT_ID;
1667 	}
1668 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1669 	return;
1670 }
1671 
1672 static void
1673 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1674 			struct lpfc_iocbq *rspiocb)
1675 {
1676 	IOCB_t *irsp = &rspiocb->iocb;
1677 	struct lpfc_vport *vport = cmdiocb->vport;
1678 
1679 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1680 		struct lpfc_dmabuf *outp;
1681 		struct lpfc_sli_ct_request *CTrsp;
1682 
1683 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1684 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1685 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1686 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1687 			vport->ct_flags |= FC_CT_RNN_ID;
1688 	}
1689 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1690 	return;
1691 }
1692 
1693 static void
1694 lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1695 			 struct lpfc_iocbq *rspiocb)
1696 {
1697 	IOCB_t *irsp = &rspiocb->iocb;
1698 	struct lpfc_vport *vport = cmdiocb->vport;
1699 
1700 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1701 		struct lpfc_dmabuf *outp;
1702 		struct lpfc_sli_ct_request *CTrsp;
1703 
1704 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1705 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1706 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1707 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1708 			vport->ct_flags |= FC_CT_RSPN_ID;
1709 	}
1710 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1711 	return;
1712 }
1713 
1714 static void
1715 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1716 			 struct lpfc_iocbq *rspiocb)
1717 {
1718 	IOCB_t *irsp = &rspiocb->iocb;
1719 	struct lpfc_vport *vport = cmdiocb->vport;
1720 
1721 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1722 		struct lpfc_dmabuf *outp;
1723 		struct lpfc_sli_ct_request *CTrsp;
1724 
1725 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1726 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1727 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1728 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1729 			vport->ct_flags |= FC_CT_RSNN_NN;
1730 	}
1731 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1732 	return;
1733 }
1734 
1735 static void
1736 lpfc_cmpl_ct_cmd_da_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1737  struct lpfc_iocbq *rspiocb)
1738 {
1739 	struct lpfc_vport *vport = cmdiocb->vport;
1740 
1741 	/* even if it fails we will act as though it succeeded. */
1742 	vport->ct_flags = 0;
1743 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1744 	return;
1745 }
1746 
1747 static void
1748 lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1749 			struct lpfc_iocbq *rspiocb)
1750 {
1751 	IOCB_t *irsp = &rspiocb->iocb;
1752 	struct lpfc_vport *vport = cmdiocb->vport;
1753 
1754 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1755 		struct lpfc_dmabuf *outp;
1756 		struct lpfc_sli_ct_request *CTrsp;
1757 
1758 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1759 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1760 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1761 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1762 			vport->ct_flags |= FC_CT_RFF_ID;
1763 	}
1764 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1765 	return;
1766 }
1767 
1768 /*
1769  * Although the symbolic port name is thought to be an integer
1770  * as of January 18, 2016, leave it as a string until more of
1771  * the record state becomes defined.
1772  */
1773 int
1774 lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
1775 	size_t size)
1776 {
1777 	int n;
1778 
1779 	/*
1780 	 * Use the lpfc board number as the Symbolic Port
1781 	 * Name object.  NPIV is not in play so this integer
1782 	 * value is sufficient and unique per FC-ID.
1783 	 */
1784 	n = scnprintf(symbol, size, "%d", vport->phba->brd_no);
1785 	return n;
1786 }
1787 
1788 
1789 int
1790 lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
1791 	size_t size)
1792 {
1793 	char fwrev[FW_REV_STR_SIZE] = {0};
1794 	char tmp[MAXHOSTNAMELEN] = {0};
1795 
1796 	memset(symbol, 0, size);
1797 
1798 	scnprintf(tmp, sizeof(tmp), "Emulex %s", vport->phba->ModelName);
1799 	if (strlcat(symbol, tmp, size) >= size)
1800 		goto buffer_done;
1801 
1802 	lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
1803 	scnprintf(tmp, sizeof(tmp), " FV%s", fwrev);
1804 	if (strlcat(symbol, tmp, size) >= size)
1805 		goto buffer_done;
1806 
1807 	scnprintf(tmp, sizeof(tmp), " DV%s", lpfc_release_version);
1808 	if (strlcat(symbol, tmp, size) >= size)
1809 		goto buffer_done;
1810 
1811 	scnprintf(tmp, sizeof(tmp), " HN:%s", vport->phba->os_host_name);
1812 	if (strlcat(symbol, tmp, size) >= size)
1813 		goto buffer_done;
1814 
1815 	/* Note :- OS name is "Linux" */
1816 	scnprintf(tmp, sizeof(tmp), " OS:%s", init_utsname()->sysname);
1817 	strlcat(symbol, tmp, size);
1818 
1819 buffer_done:
1820 	return strnlen(symbol, size);
1821 
1822 }
1823 
1824 static uint32_t
1825 lpfc_find_map_node(struct lpfc_vport *vport)
1826 {
1827 	struct lpfc_nodelist *ndlp, *next_ndlp;
1828 	struct Scsi_Host  *shost;
1829 	uint32_t cnt = 0;
1830 
1831 	shost = lpfc_shost_from_vport(vport);
1832 	spin_lock_irq(shost->host_lock);
1833 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1834 		if (ndlp->nlp_type & NLP_FABRIC)
1835 			continue;
1836 		if ((ndlp->nlp_state == NLP_STE_MAPPED_NODE) ||
1837 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE))
1838 			cnt++;
1839 	}
1840 	spin_unlock_irq(shost->host_lock);
1841 	return cnt;
1842 }
1843 
1844 /*
1845  * This routine will return the FC4 Type associated with the CT
1846  * GID_FT command.
1847  */
1848 int
1849 lpfc_get_gidft_type(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb)
1850 {
1851 	struct lpfc_sli_ct_request *CtReq;
1852 	struct lpfc_dmabuf *mp;
1853 	uint32_t type;
1854 
1855 	mp = cmdiocb->context1;
1856 	if (mp == NULL)
1857 		return 0;
1858 	CtReq = (struct lpfc_sli_ct_request *)mp->virt;
1859 	type = (uint32_t)CtReq->un.gid.Fc4Type;
1860 	if ((type != SLI_CTPT_FCP) && (type != SLI_CTPT_NVME))
1861 		return 0;
1862 	return type;
1863 }
1864 
1865 /*
1866  * lpfc_ns_cmd
1867  * Description:
1868  *    Issue Cmd to NameServer
1869  *       SLI_CTNS_GID_FT
1870  *       LI_CTNS_RFT_ID
1871  */
1872 int
1873 lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
1874 	    uint8_t retry, uint32_t context)
1875 {
1876 	struct lpfc_nodelist * ndlp;
1877 	struct lpfc_hba *phba = vport->phba;
1878 	struct lpfc_dmabuf *mp, *bmp;
1879 	struct lpfc_sli_ct_request *CtReq;
1880 	struct ulp_bde64 *bpl;
1881 	void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
1882 		      struct lpfc_iocbq *) = NULL;
1883 	uint32_t *ptr;
1884 	uint32_t rsp_size = 1024;
1885 	size_t   size;
1886 	int rc = 0;
1887 
1888 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
1889 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) {
1890 		rc=1;
1891 		goto ns_cmd_exit;
1892 	}
1893 
1894 	/* fill in BDEs for command */
1895 	/* Allocate buffer for command payload */
1896 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1897 	if (!mp) {
1898 		rc=2;
1899 		goto ns_cmd_exit;
1900 	}
1901 
1902 	INIT_LIST_HEAD(&mp->list);
1903 	mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
1904 	if (!mp->virt) {
1905 		rc=3;
1906 		goto ns_cmd_free_mp;
1907 	}
1908 
1909 	/* Allocate buffer for Buffer ptr list */
1910 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1911 	if (!bmp) {
1912 		rc=4;
1913 		goto ns_cmd_free_mpvirt;
1914 	}
1915 
1916 	INIT_LIST_HEAD(&bmp->list);
1917 	bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
1918 	if (!bmp->virt) {
1919 		rc=5;
1920 		goto ns_cmd_free_bmp;
1921 	}
1922 
1923 	/* NameServer Req */
1924 	lpfc_printf_vlog(vport, KERN_INFO ,LOG_DISCOVERY,
1925 			 "0236 NameServer Req Data: x%x x%x x%x x%x\n",
1926 			 cmdcode, vport->fc_flag, vport->fc_rscn_id_cnt,
1927 			 context);
1928 
1929 	bpl = (struct ulp_bde64 *) bmp->virt;
1930 	memset(bpl, 0, sizeof(struct ulp_bde64));
1931 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
1932 	bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
1933 	bpl->tus.f.bdeFlags = 0;
1934 	if (cmdcode == SLI_CTNS_GID_FT)
1935 		bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1936 	else if (cmdcode == SLI_CTNS_GID_PT)
1937 		bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1938 	else if (cmdcode == SLI_CTNS_GFF_ID)
1939 		bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
1940 	else if (cmdcode == SLI_CTNS_GFT_ID)
1941 		bpl->tus.f.bdeSize = GFT_REQUEST_SZ;
1942 	else if (cmdcode == SLI_CTNS_RFT_ID)
1943 		bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
1944 	else if (cmdcode == SLI_CTNS_RNN_ID)
1945 		bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
1946 	else if (cmdcode == SLI_CTNS_RSPN_ID)
1947 		bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
1948 	else if (cmdcode == SLI_CTNS_RSNN_NN)
1949 		bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
1950 	else if (cmdcode == SLI_CTNS_DA_ID)
1951 		bpl->tus.f.bdeSize = DA_ID_REQUEST_SZ;
1952 	else if (cmdcode == SLI_CTNS_RFF_ID)
1953 		bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
1954 	else
1955 		bpl->tus.f.bdeSize = 0;
1956 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
1957 
1958 	CtReq = (struct lpfc_sli_ct_request *) mp->virt;
1959 	memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
1960 	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
1961 	CtReq->RevisionId.bits.InId = 0;
1962 	CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
1963 	CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
1964 	CtReq->CommandResponse.bits.Size = 0;
1965 	switch (cmdcode) {
1966 	case SLI_CTNS_GID_FT:
1967 		CtReq->CommandResponse.bits.CmdRsp =
1968 		    cpu_to_be16(SLI_CTNS_GID_FT);
1969 		CtReq->un.gid.Fc4Type = context;
1970 
1971 		if (vport->port_state < LPFC_NS_QRY)
1972 			vport->port_state = LPFC_NS_QRY;
1973 		lpfc_set_disctmo(vport);
1974 		cmpl = lpfc_cmpl_ct_cmd_gid_ft;
1975 		rsp_size = FC_MAX_NS_RSP;
1976 		break;
1977 
1978 	case SLI_CTNS_GID_PT:
1979 		CtReq->CommandResponse.bits.CmdRsp =
1980 		    cpu_to_be16(SLI_CTNS_GID_PT);
1981 		CtReq->un.gid.PortType = context;
1982 
1983 		if (vport->port_state < LPFC_NS_QRY)
1984 			vport->port_state = LPFC_NS_QRY;
1985 		lpfc_set_disctmo(vport);
1986 		cmpl = lpfc_cmpl_ct_cmd_gid_pt;
1987 		rsp_size = FC_MAX_NS_RSP;
1988 		break;
1989 
1990 	case SLI_CTNS_GFF_ID:
1991 		CtReq->CommandResponse.bits.CmdRsp =
1992 			cpu_to_be16(SLI_CTNS_GFF_ID);
1993 		CtReq->un.gff.PortId = cpu_to_be32(context);
1994 		cmpl = lpfc_cmpl_ct_cmd_gff_id;
1995 		break;
1996 
1997 	case SLI_CTNS_GFT_ID:
1998 		CtReq->CommandResponse.bits.CmdRsp =
1999 			cpu_to_be16(SLI_CTNS_GFT_ID);
2000 		CtReq->un.gft.PortId = cpu_to_be32(context);
2001 		cmpl = lpfc_cmpl_ct_cmd_gft_id;
2002 		break;
2003 
2004 	case SLI_CTNS_RFT_ID:
2005 		vport->ct_flags &= ~FC_CT_RFT_ID;
2006 		CtReq->CommandResponse.bits.CmdRsp =
2007 		    cpu_to_be16(SLI_CTNS_RFT_ID);
2008 		CtReq->un.rft.PortId = cpu_to_be32(vport->fc_myDID);
2009 
2010 		/* Register FC4 FCP type if enabled.  */
2011 		if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
2012 		    vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)
2013 			CtReq->un.rft.fcpReg = 1;
2014 
2015 		/* Register NVME type if enabled.  Defined LE and swapped.
2016 		 * rsvd[0] is used as word1 because of the hard-coded
2017 		 * word0 usage in the ct_request data structure.
2018 		 */
2019 		if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
2020 		    vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
2021 			CtReq->un.rft.rsvd[0] =
2022 				cpu_to_be32(LPFC_FC4_TYPE_BITMASK);
2023 
2024 		ptr = (uint32_t *)CtReq;
2025 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2026 				 "6433 Issue RFT (%s %s): %08x %08x %08x %08x "
2027 				 "%08x %08x %08x %08x\n",
2028 				 CtReq->un.rft.fcpReg ? "FCP" : " ",
2029 				 CtReq->un.rft.rsvd[0] ? "NVME" : " ",
2030 				 *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
2031 				 *(ptr + 4), *(ptr + 5),
2032 				 *(ptr + 6), *(ptr + 7));
2033 		cmpl = lpfc_cmpl_ct_cmd_rft_id;
2034 		break;
2035 
2036 	case SLI_CTNS_RNN_ID:
2037 		vport->ct_flags &= ~FC_CT_RNN_ID;
2038 		CtReq->CommandResponse.bits.CmdRsp =
2039 		    cpu_to_be16(SLI_CTNS_RNN_ID);
2040 		CtReq->un.rnn.PortId = cpu_to_be32(vport->fc_myDID);
2041 		memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
2042 		       sizeof(struct lpfc_name));
2043 		cmpl = lpfc_cmpl_ct_cmd_rnn_id;
2044 		break;
2045 
2046 	case SLI_CTNS_RSPN_ID:
2047 		vport->ct_flags &= ~FC_CT_RSPN_ID;
2048 		CtReq->CommandResponse.bits.CmdRsp =
2049 		    cpu_to_be16(SLI_CTNS_RSPN_ID);
2050 		CtReq->un.rspn.PortId = cpu_to_be32(vport->fc_myDID);
2051 		size = sizeof(CtReq->un.rspn.symbname);
2052 		CtReq->un.rspn.len =
2053 			lpfc_vport_symbolic_port_name(vport,
2054 			CtReq->un.rspn.symbname, size);
2055 		cmpl = lpfc_cmpl_ct_cmd_rspn_id;
2056 		break;
2057 	case SLI_CTNS_RSNN_NN:
2058 		vport->ct_flags &= ~FC_CT_RSNN_NN;
2059 		CtReq->CommandResponse.bits.CmdRsp =
2060 		    cpu_to_be16(SLI_CTNS_RSNN_NN);
2061 		memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
2062 		       sizeof(struct lpfc_name));
2063 		size = sizeof(CtReq->un.rsnn.symbname);
2064 		CtReq->un.rsnn.len =
2065 			lpfc_vport_symbolic_node_name(vport,
2066 			CtReq->un.rsnn.symbname, size);
2067 		cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
2068 		break;
2069 	case SLI_CTNS_DA_ID:
2070 		/* Implement DA_ID Nameserver request */
2071 		CtReq->CommandResponse.bits.CmdRsp =
2072 			cpu_to_be16(SLI_CTNS_DA_ID);
2073 		CtReq->un.da_id.port_id = cpu_to_be32(vport->fc_myDID);
2074 		cmpl = lpfc_cmpl_ct_cmd_da_id;
2075 		break;
2076 	case SLI_CTNS_RFF_ID:
2077 		vport->ct_flags &= ~FC_CT_RFF_ID;
2078 		CtReq->CommandResponse.bits.CmdRsp =
2079 		    cpu_to_be16(SLI_CTNS_RFF_ID);
2080 		CtReq->un.rff.PortId = cpu_to_be32(vport->fc_myDID);
2081 		CtReq->un.rff.fbits = FC4_FEATURE_INIT;
2082 
2083 		/* The driver always supports FC_TYPE_FCP.  However, the
2084 		 * caller can specify NVME (type x28) as well.  But only
2085 		 * these that FC4 type is supported.
2086 		 */
2087 		if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2088 		     (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
2089 		    (context == FC_TYPE_NVME)) {
2090 			if ((vport == phba->pport) && phba->nvmet_support) {
2091 				CtReq->un.rff.fbits = (FC4_FEATURE_TARGET |
2092 					FC4_FEATURE_NVME_DISC);
2093 				lpfc_nvmet_update_targetport(phba);
2094 			} else {
2095 				lpfc_nvme_update_localport(vport);
2096 			}
2097 			CtReq->un.rff.type_code = context;
2098 
2099 		} else if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2100 			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) &&
2101 			   (context == FC_TYPE_FCP))
2102 			CtReq->un.rff.type_code = context;
2103 
2104 		else
2105 			goto ns_cmd_free_bmpvirt;
2106 
2107 		ptr = (uint32_t *)CtReq;
2108 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2109 				 "6434 Issue RFF (%s): %08x %08x %08x %08x "
2110 				 "%08x %08x %08x %08x\n",
2111 				 (context == FC_TYPE_NVME) ? "NVME" : "FCP",
2112 				 *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
2113 				 *(ptr + 4), *(ptr + 5),
2114 				 *(ptr + 6), *(ptr + 7));
2115 		cmpl = lpfc_cmpl_ct_cmd_rff_id;
2116 		break;
2117 	}
2118 	/* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
2119 	 * to hold ndlp reference for the corresponding callback function.
2120 	 */
2121 	if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry)) {
2122 		/* On success, The cmpl function will free the buffers */
2123 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
2124 			"Issue CT cmd:    cmd:x%x did:x%x",
2125 			cmdcode, ndlp->nlp_DID, 0);
2126 		return 0;
2127 	}
2128 	rc=6;
2129 
2130 ns_cmd_free_bmpvirt:
2131 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
2132 ns_cmd_free_bmp:
2133 	kfree(bmp);
2134 ns_cmd_free_mpvirt:
2135 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2136 ns_cmd_free_mp:
2137 	kfree(mp);
2138 ns_cmd_exit:
2139 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2140 			 "0266 Issue NameServer Req x%x err %d Data: x%x x%x\n",
2141 			 cmdcode, rc, vport->fc_flag, vport->fc_rscn_id_cnt);
2142 	return 1;
2143 }
2144 
2145 /**
2146  * lpfc_cmpl_ct_disc_fdmi - Handle a discovery FDMI completion
2147  * @phba: Pointer to HBA context object.
2148  * @cmdiocb: Pointer to the command IOCBQ.
2149  * @rspiocb: Pointer to the response IOCBQ.
2150  *
2151  * This function to handle the completion of a driver initiated FDMI
2152  * CT command issued during discovery.
2153  */
2154 static void
2155 lpfc_cmpl_ct_disc_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2156 		       struct lpfc_iocbq *rspiocb)
2157 {
2158 	struct lpfc_vport *vport = cmdiocb->vport;
2159 	struct lpfc_dmabuf *inp = cmdiocb->context1;
2160 	struct lpfc_dmabuf *outp = cmdiocb->context2;
2161 	struct lpfc_sli_ct_request *CTcmd = inp->virt;
2162 	struct lpfc_sli_ct_request *CTrsp = outp->virt;
2163 	uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
2164 	uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
2165 	IOCB_t *irsp = &rspiocb->iocb;
2166 	struct lpfc_nodelist *ndlp, *free_ndlp = NULL;
2167 	uint32_t latt, cmd, err;
2168 
2169 	latt = lpfc_els_chk_latt(vport);
2170 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
2171 		"FDMI cmpl:       status:x%x/x%x latt:%d",
2172 		irsp->ulpStatus, irsp->un.ulpWord[4], latt);
2173 
2174 	if (latt || irsp->ulpStatus) {
2175 
2176 		/* Look for a retryable error */
2177 		if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
2178 			switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
2179 			case IOERR_SLI_ABORTED:
2180 			case IOERR_SLI_DOWN:
2181 				/* Driver aborted this IO.  No retry as error
2182 				 * is likely Offline->Online or some adapter
2183 				 * error.  Recovery will try again.
2184 				 */
2185 				break;
2186 			case IOERR_ABORT_IN_PROGRESS:
2187 			case IOERR_SEQUENCE_TIMEOUT:
2188 			case IOERR_ILLEGAL_FRAME:
2189 			case IOERR_NO_RESOURCES:
2190 			case IOERR_ILLEGAL_COMMAND:
2191 				cmdiocb->retry++;
2192 				if (cmdiocb->retry >= LPFC_FDMI_MAX_RETRY)
2193 					break;
2194 
2195 				/* Retry the same FDMI command */
2196 				err = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING,
2197 							  cmdiocb, 0);
2198 				if (err == IOCB_ERROR)
2199 					break;
2200 				return;
2201 			default:
2202 				break;
2203 			}
2204 		}
2205 
2206 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2207 				 "0229 FDMI cmd %04x failed, latt = %d "
2208 				 "ulpStatus: x%x, rid x%x\n",
2209 				 be16_to_cpu(fdmi_cmd), latt, irsp->ulpStatus,
2210 				 irsp->un.ulpWord[4]);
2211 	}
2212 
2213 	free_ndlp = cmdiocb->context_un.ndlp;
2214 	lpfc_ct_free_iocb(phba, cmdiocb);
2215 	lpfc_nlp_put(free_ndlp);
2216 
2217 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
2218 	if (!ndlp)
2219 		return;
2220 
2221 	/* Check for a CT LS_RJT response */
2222 	cmd =  be16_to_cpu(fdmi_cmd);
2223 	if (fdmi_rsp == cpu_to_be16(SLI_CT_RESPONSE_FS_RJT)) {
2224 		/* FDMI rsp failed */
2225 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2226 				 "0220 FDMI cmd failed FS_RJT Data: x%x", cmd);
2227 
2228 		/* Should we fallback to FDMI-2 / FDMI-1 ? */
2229 		switch (cmd) {
2230 		case SLI_MGMT_RHBA:
2231 			if (vport->fdmi_hba_mask == LPFC_FDMI2_HBA_ATTR) {
2232 				/* Fallback to FDMI-1 */
2233 				vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
2234 				vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2235 				/* Start over */
2236 				lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2237 			}
2238 			return;
2239 
2240 		case SLI_MGMT_RPRT:
2241 			if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
2242 				/* Fallback to FDMI-1 */
2243 				vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2244 				/* Start over */
2245 				lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2246 				return;
2247 			}
2248 			if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
2249 				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
2250 				/* Retry the same command */
2251 				lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2252 			}
2253 			return;
2254 
2255 		case SLI_MGMT_RPA:
2256 			/* No retry on Vendor, RPA only done on physical port */
2257 			if (phba->link_flag & LS_CT_VEN_RPA) {
2258 				phba->link_flag &= ~LS_CT_VEN_RPA;
2259 				lpfc_printf_log(phba, KERN_ERR,
2260 						LOG_DISCOVERY | LOG_ELS,
2261 						"6460 VEN FDMI RPA failure\n");
2262 				return;
2263 			}
2264 			if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
2265 				/* Fallback to FDMI-1 */
2266 				vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
2267 				vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2268 				/* Start over */
2269 				lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2270 				return;
2271 			}
2272 			if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
2273 				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
2274 				/* Retry the same command */
2275 				lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2276 			}
2277 			return;
2278 		}
2279 	}
2280 
2281 	/*
2282 	 * On success, need to cycle thru FDMI registration for discovery
2283 	 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2284 	 * DPRT -> RPRT (vports)
2285 	 */
2286 	switch (cmd) {
2287 	case SLI_MGMT_RHBA:
2288 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA, 0);
2289 		break;
2290 
2291 	case SLI_MGMT_DHBA:
2292 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2293 		break;
2294 
2295 	case SLI_MGMT_DPRT:
2296 		if (vport->port_type == LPFC_PHYSICAL_PORT)
2297 			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA, 0);
2298 		else
2299 			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT, 0);
2300 		break;
2301 	case SLI_MGMT_RPA:
2302 		if (vport->port_type == LPFC_PHYSICAL_PORT &&
2303 		    phba->cfg_enable_mi &&
2304 		    phba->sli4_hba.pc_sli4_params.mi_ver > LPFC_MIB1_SUPPORT) {
2305 			/* mi is only for the phyical port, no vports */
2306 			if (phba->link_flag & LS_CT_VEN_RPA) {
2307 				lpfc_printf_vlog(vport, KERN_INFO,
2308 						 LOG_DISCOVERY | LOG_ELS,
2309 						 "6449 VEN RPA FDMI Success\n");
2310 				phba->link_flag &= ~LS_CT_VEN_RPA;
2311 				break;
2312 			}
2313 
2314 			if (lpfc_fdmi_cmd(vport, ndlp, cmd,
2315 					  LPFC_FDMI_VENDOR_ATTR_mi) == 0)
2316 				phba->link_flag |= LS_CT_VEN_RPA;
2317 			lpfc_printf_log(phba, KERN_INFO,
2318 					LOG_DISCOVERY | LOG_ELS,
2319 					"6458 Send MI FDMI:%x Flag x%x\n",
2320 					phba->sli4_hba.pc_sli4_params.mi_value,
2321 					phba->link_flag);
2322 		} else {
2323 			lpfc_printf_log(phba, KERN_INFO,
2324 					LOG_DISCOVERY | LOG_ELS,
2325 					"6459 No FDMI VEN MI support - "
2326 					"RPA Success\n");
2327 		}
2328 		break;
2329 	}
2330 	return;
2331 }
2332 
2333 
2334 /**
2335  * lpfc_fdmi_change_check - Check for changed FDMI parameters
2336  * @vport: pointer to a host virtual N_Port data structure.
2337  *
2338  * Check how many mapped NPorts we are connected to
2339  * Check if our hostname changed
2340  * Called from hbeat timeout routine to check if any FDMI parameters
2341  * changed. If so, re-register those Attributes.
2342  */
2343 void
2344 lpfc_fdmi_change_check(struct lpfc_vport *vport)
2345 {
2346 	struct lpfc_hba *phba = vport->phba;
2347 	struct lpfc_nodelist *ndlp;
2348 	uint16_t cnt;
2349 
2350 	if (!lpfc_is_link_up(phba))
2351 		return;
2352 
2353 	/* Must be connected to a Fabric */
2354 	if (!(vport->fc_flag & FC_FABRIC))
2355 		return;
2356 
2357 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
2358 	if (!ndlp)
2359 		return;
2360 
2361 	/* Check if system hostname changed */
2362 	if (strcmp(phba->os_host_name, init_utsname()->nodename)) {
2363 		memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
2364 		scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
2365 			  init_utsname()->nodename);
2366 		lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2367 
2368 		/* Since this effects multiple HBA and PORT attributes, we need
2369 		 * de-register and go thru the whole FDMI registration cycle.
2370 		 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2371 		 * DPRT -> RPRT (vports)
2372 		 */
2373 		if (vport->port_type == LPFC_PHYSICAL_PORT) {
2374 			/* For extra Vendor RPA */
2375 			phba->link_flag &= ~LS_CT_VEN_RPA;
2376 			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2377 		} else {
2378 			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2379 		}
2380 
2381 		/* Since this code path registers all the port attributes
2382 		 * we can just return without further checking.
2383 		 */
2384 		return;
2385 	}
2386 
2387 	if (!(vport->fdmi_port_mask & LPFC_FDMI_PORT_ATTR_num_disc))
2388 		return;
2389 
2390 	/* Check if the number of mapped NPorts changed */
2391 	cnt = lpfc_find_map_node(vport);
2392 	if (cnt == vport->fdmi_num_disc)
2393 		return;
2394 
2395 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
2396 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA,
2397 			      LPFC_FDMI_PORT_ATTR_num_disc);
2398 	} else {
2399 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT,
2400 			      LPFC_FDMI_PORT_ATTR_num_disc);
2401 	}
2402 }
2403 
2404 /* Routines for all individual HBA attributes */
2405 static int
2406 lpfc_fdmi_hba_attr_wwnn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2407 {
2408 	struct lpfc_fdmi_attr_entry *ae;
2409 	uint32_t size;
2410 
2411 	ae = &ad->AttrValue;
2412 	memset(ae, 0, sizeof(*ae));
2413 
2414 	memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2415 	       sizeof(struct lpfc_name));
2416 	size = FOURBYTES + sizeof(struct lpfc_name);
2417 	ad->AttrLen = cpu_to_be16(size);
2418 	ad->AttrType = cpu_to_be16(RHBA_NODENAME);
2419 	return size;
2420 }
2421 static int
2422 lpfc_fdmi_hba_attr_manufacturer(struct lpfc_vport *vport,
2423 				struct lpfc_fdmi_attr_def *ad)
2424 {
2425 	struct lpfc_fdmi_attr_entry *ae;
2426 	uint32_t len, size;
2427 
2428 	ae = &ad->AttrValue;
2429 	memset(ae, 0, sizeof(*ae));
2430 
2431 	/* This string MUST be consistent with other FC platforms
2432 	 * supported by Broadcom.
2433 	 */
2434 	strncpy(ae->un.AttrString,
2435 		"Emulex Corporation",
2436 		       sizeof(ae->un.AttrString));
2437 	len = strnlen(ae->un.AttrString,
2438 			  sizeof(ae->un.AttrString));
2439 	len += (len & 3) ? (4 - (len & 3)) : 4;
2440 	size = FOURBYTES + len;
2441 	ad->AttrLen = cpu_to_be16(size);
2442 	ad->AttrType = cpu_to_be16(RHBA_MANUFACTURER);
2443 	return size;
2444 }
2445 
2446 static int
2447 lpfc_fdmi_hba_attr_sn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2448 {
2449 	struct lpfc_hba *phba = vport->phba;
2450 	struct lpfc_fdmi_attr_entry *ae;
2451 	uint32_t len, size;
2452 
2453 	ae = &ad->AttrValue;
2454 	memset(ae, 0, sizeof(*ae));
2455 
2456 	strncpy(ae->un.AttrString, phba->SerialNumber,
2457 		sizeof(ae->un.AttrString));
2458 	len = strnlen(ae->un.AttrString,
2459 			  sizeof(ae->un.AttrString));
2460 	len += (len & 3) ? (4 - (len & 3)) : 4;
2461 	size = FOURBYTES + len;
2462 	ad->AttrLen = cpu_to_be16(size);
2463 	ad->AttrType = cpu_to_be16(RHBA_SERIAL_NUMBER);
2464 	return size;
2465 }
2466 
2467 static int
2468 lpfc_fdmi_hba_attr_model(struct lpfc_vport *vport,
2469 			 struct lpfc_fdmi_attr_def *ad)
2470 {
2471 	struct lpfc_hba *phba = vport->phba;
2472 	struct lpfc_fdmi_attr_entry *ae;
2473 	uint32_t len, size;
2474 
2475 	ae = &ad->AttrValue;
2476 	memset(ae, 0, sizeof(*ae));
2477 
2478 	strncpy(ae->un.AttrString, phba->ModelName,
2479 		sizeof(ae->un.AttrString));
2480 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2481 	len += (len & 3) ? (4 - (len & 3)) : 4;
2482 	size = FOURBYTES + len;
2483 	ad->AttrLen = cpu_to_be16(size);
2484 	ad->AttrType = cpu_to_be16(RHBA_MODEL);
2485 	return size;
2486 }
2487 
2488 static int
2489 lpfc_fdmi_hba_attr_description(struct lpfc_vport *vport,
2490 			       struct lpfc_fdmi_attr_def *ad)
2491 {
2492 	struct lpfc_hba *phba = vport->phba;
2493 	struct lpfc_fdmi_attr_entry *ae;
2494 	uint32_t len, size;
2495 
2496 	ae = &ad->AttrValue;
2497 	memset(ae, 0, sizeof(*ae));
2498 
2499 	strncpy(ae->un.AttrString, phba->ModelDesc,
2500 		sizeof(ae->un.AttrString));
2501 	len = strnlen(ae->un.AttrString,
2502 				  sizeof(ae->un.AttrString));
2503 	len += (len & 3) ? (4 - (len & 3)) : 4;
2504 	size = FOURBYTES + len;
2505 	ad->AttrLen = cpu_to_be16(size);
2506 	ad->AttrType = cpu_to_be16(RHBA_MODEL_DESCRIPTION);
2507 	return size;
2508 }
2509 
2510 static int
2511 lpfc_fdmi_hba_attr_hdw_ver(struct lpfc_vport *vport,
2512 			   struct lpfc_fdmi_attr_def *ad)
2513 {
2514 	struct lpfc_hba *phba = vport->phba;
2515 	lpfc_vpd_t *vp = &phba->vpd;
2516 	struct lpfc_fdmi_attr_entry *ae;
2517 	uint32_t i, j, incr, size;
2518 
2519 	ae = &ad->AttrValue;
2520 	memset(ae, 0, sizeof(*ae));
2521 
2522 	/* Convert JEDEC ID to ascii for hardware version */
2523 	incr = vp->rev.biuRev;
2524 	for (i = 0; i < 8; i++) {
2525 		j = (incr & 0xf);
2526 		if (j <= 9)
2527 			ae->un.AttrString[7 - i] =
2528 			    (char)((uint8_t) 0x30 +
2529 				   (uint8_t) j);
2530 		else
2531 			ae->un.AttrString[7 - i] =
2532 			    (char)((uint8_t) 0x61 +
2533 				   (uint8_t) (j - 10));
2534 		incr = (incr >> 4);
2535 	}
2536 	size = FOURBYTES + 8;
2537 	ad->AttrLen = cpu_to_be16(size);
2538 	ad->AttrType = cpu_to_be16(RHBA_HARDWARE_VERSION);
2539 	return size;
2540 }
2541 
2542 static int
2543 lpfc_fdmi_hba_attr_drvr_ver(struct lpfc_vport *vport,
2544 			    struct lpfc_fdmi_attr_def *ad)
2545 {
2546 	struct lpfc_fdmi_attr_entry *ae;
2547 	uint32_t len, size;
2548 
2549 	ae = &ad->AttrValue;
2550 	memset(ae, 0, sizeof(*ae));
2551 
2552 	strncpy(ae->un.AttrString, lpfc_release_version,
2553 		sizeof(ae->un.AttrString));
2554 	len = strnlen(ae->un.AttrString,
2555 			  sizeof(ae->un.AttrString));
2556 	len += (len & 3) ? (4 - (len & 3)) : 4;
2557 	size = FOURBYTES + len;
2558 	ad->AttrLen = cpu_to_be16(size);
2559 	ad->AttrType = cpu_to_be16(RHBA_DRIVER_VERSION);
2560 	return size;
2561 }
2562 
2563 static int
2564 lpfc_fdmi_hba_attr_rom_ver(struct lpfc_vport *vport,
2565 			   struct lpfc_fdmi_attr_def *ad)
2566 {
2567 	struct lpfc_hba *phba = vport->phba;
2568 	struct lpfc_fdmi_attr_entry *ae;
2569 	uint32_t len, size;
2570 
2571 	ae = &ad->AttrValue;
2572 	memset(ae, 0, sizeof(*ae));
2573 
2574 	if (phba->sli_rev == LPFC_SLI_REV4)
2575 		lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2576 	else
2577 		strncpy(ae->un.AttrString, phba->OptionROMVersion,
2578 			sizeof(ae->un.AttrString));
2579 	len = strnlen(ae->un.AttrString,
2580 			  sizeof(ae->un.AttrString));
2581 	len += (len & 3) ? (4 - (len & 3)) : 4;
2582 	size = FOURBYTES + len;
2583 	ad->AttrLen = cpu_to_be16(size);
2584 	ad->AttrType = cpu_to_be16(RHBA_OPTION_ROM_VERSION);
2585 	return size;
2586 }
2587 
2588 static int
2589 lpfc_fdmi_hba_attr_fmw_ver(struct lpfc_vport *vport,
2590 			   struct lpfc_fdmi_attr_def *ad)
2591 {
2592 	struct lpfc_hba *phba = vport->phba;
2593 	struct lpfc_fdmi_attr_entry *ae;
2594 	uint32_t len, size;
2595 
2596 	ae = &ad->AttrValue;
2597 	memset(ae, 0, sizeof(*ae));
2598 
2599 	lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2600 	len = strnlen(ae->un.AttrString,
2601 			  sizeof(ae->un.AttrString));
2602 	len += (len & 3) ? (4 - (len & 3)) : 4;
2603 	size = FOURBYTES + len;
2604 	ad->AttrLen = cpu_to_be16(size);
2605 	ad->AttrType = cpu_to_be16(RHBA_FIRMWARE_VERSION);
2606 	return size;
2607 }
2608 
2609 static int
2610 lpfc_fdmi_hba_attr_os_ver(struct lpfc_vport *vport,
2611 			  struct lpfc_fdmi_attr_def *ad)
2612 {
2613 	struct lpfc_fdmi_attr_entry *ae;
2614 	uint32_t len, size;
2615 
2616 	ae = &ad->AttrValue;
2617 	memset(ae, 0, sizeof(*ae));
2618 
2619 	snprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s %s %s",
2620 		 init_utsname()->sysname,
2621 		 init_utsname()->release,
2622 		 init_utsname()->version);
2623 
2624 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2625 	len += (len & 3) ? (4 - (len & 3)) : 4;
2626 	size = FOURBYTES + len;
2627 	ad->AttrLen = cpu_to_be16(size);
2628 	ad->AttrType = cpu_to_be16(RHBA_OS_NAME_VERSION);
2629 	return size;
2630 }
2631 
2632 static int
2633 lpfc_fdmi_hba_attr_ct_len(struct lpfc_vport *vport,
2634 			  struct lpfc_fdmi_attr_def *ad)
2635 {
2636 	struct lpfc_fdmi_attr_entry *ae;
2637 	uint32_t size;
2638 
2639 	ae = &ad->AttrValue;
2640 
2641 	ae->un.AttrInt =  cpu_to_be32(LPFC_MAX_CT_SIZE);
2642 	size = FOURBYTES + sizeof(uint32_t);
2643 	ad->AttrLen = cpu_to_be16(size);
2644 	ad->AttrType = cpu_to_be16(RHBA_MAX_CT_PAYLOAD_LEN);
2645 	return size;
2646 }
2647 
2648 static int
2649 lpfc_fdmi_hba_attr_symbolic_name(struct lpfc_vport *vport,
2650 				 struct lpfc_fdmi_attr_def *ad)
2651 {
2652 	struct lpfc_fdmi_attr_entry *ae;
2653 	uint32_t len, size;
2654 
2655 	ae = &ad->AttrValue;
2656 	memset(ae, 0, sizeof(*ae));
2657 
2658 	len = lpfc_vport_symbolic_node_name(vport,
2659 				ae->un.AttrString, 256);
2660 	len += (len & 3) ? (4 - (len & 3)) : 4;
2661 	size = FOURBYTES + len;
2662 	ad->AttrLen = cpu_to_be16(size);
2663 	ad->AttrType = cpu_to_be16(RHBA_SYM_NODENAME);
2664 	return size;
2665 }
2666 
2667 static int
2668 lpfc_fdmi_hba_attr_vendor_info(struct lpfc_vport *vport,
2669 			       struct lpfc_fdmi_attr_def *ad)
2670 {
2671 	struct lpfc_fdmi_attr_entry *ae;
2672 	uint32_t size;
2673 
2674 	ae = &ad->AttrValue;
2675 
2676 	/* Nothing is defined for this currently */
2677 	ae->un.AttrInt =  cpu_to_be32(0);
2678 	size = FOURBYTES + sizeof(uint32_t);
2679 	ad->AttrLen = cpu_to_be16(size);
2680 	ad->AttrType = cpu_to_be16(RHBA_VENDOR_INFO);
2681 	return size;
2682 }
2683 
2684 static int
2685 lpfc_fdmi_hba_attr_num_ports(struct lpfc_vport *vport,
2686 			     struct lpfc_fdmi_attr_def *ad)
2687 {
2688 	struct lpfc_fdmi_attr_entry *ae;
2689 	uint32_t size;
2690 
2691 	ae = &ad->AttrValue;
2692 
2693 	/* Each driver instance corresponds to a single port */
2694 	ae->un.AttrInt =  cpu_to_be32(1);
2695 	size = FOURBYTES + sizeof(uint32_t);
2696 	ad->AttrLen = cpu_to_be16(size);
2697 	ad->AttrType = cpu_to_be16(RHBA_NUM_PORTS);
2698 	return size;
2699 }
2700 
2701 static int
2702 lpfc_fdmi_hba_attr_fabric_wwnn(struct lpfc_vport *vport,
2703 			       struct lpfc_fdmi_attr_def *ad)
2704 {
2705 	struct lpfc_fdmi_attr_entry *ae;
2706 	uint32_t size;
2707 
2708 	ae = &ad->AttrValue;
2709 	memset(ae, 0, sizeof(*ae));
2710 
2711 	memcpy(&ae->un.AttrWWN, &vport->fabric_nodename,
2712 	       sizeof(struct lpfc_name));
2713 	size = FOURBYTES + sizeof(struct lpfc_name);
2714 	ad->AttrLen = cpu_to_be16(size);
2715 	ad->AttrType = cpu_to_be16(RHBA_FABRIC_WWNN);
2716 	return size;
2717 }
2718 
2719 static int
2720 lpfc_fdmi_hba_attr_bios_ver(struct lpfc_vport *vport,
2721 			    struct lpfc_fdmi_attr_def *ad)
2722 {
2723 	struct lpfc_hba *phba = vport->phba;
2724 	struct lpfc_fdmi_attr_entry *ae;
2725 	uint32_t len, size;
2726 
2727 	ae = &ad->AttrValue;
2728 	memset(ae, 0, sizeof(*ae));
2729 
2730 	strlcat(ae->un.AttrString, phba->BIOSVersion,
2731 		sizeof(ae->un.AttrString));
2732 	len = strnlen(ae->un.AttrString,
2733 			  sizeof(ae->un.AttrString));
2734 	len += (len & 3) ? (4 - (len & 3)) : 4;
2735 	size = FOURBYTES + len;
2736 	ad->AttrLen = cpu_to_be16(size);
2737 	ad->AttrType = cpu_to_be16(RHBA_BIOS_VERSION);
2738 	return size;
2739 }
2740 
2741 static int
2742 lpfc_fdmi_hba_attr_bios_state(struct lpfc_vport *vport,
2743 			      struct lpfc_fdmi_attr_def *ad)
2744 {
2745 	struct lpfc_fdmi_attr_entry *ae;
2746 	uint32_t size;
2747 
2748 	ae = &ad->AttrValue;
2749 
2750 	/* Driver doesn't have access to this information */
2751 	ae->un.AttrInt =  cpu_to_be32(0);
2752 	size = FOURBYTES + sizeof(uint32_t);
2753 	ad->AttrLen = cpu_to_be16(size);
2754 	ad->AttrType = cpu_to_be16(RHBA_BIOS_STATE);
2755 	return size;
2756 }
2757 
2758 static int
2759 lpfc_fdmi_hba_attr_vendor_id(struct lpfc_vport *vport,
2760 			     struct lpfc_fdmi_attr_def *ad)
2761 {
2762 	struct lpfc_fdmi_attr_entry *ae;
2763 	uint32_t len, size;
2764 
2765 	ae = &ad->AttrValue;
2766 	memset(ae, 0, sizeof(*ae));
2767 
2768 	strncpy(ae->un.AttrString, "EMULEX",
2769 		sizeof(ae->un.AttrString));
2770 	len = strnlen(ae->un.AttrString,
2771 			  sizeof(ae->un.AttrString));
2772 	len += (len & 3) ? (4 - (len & 3)) : 4;
2773 	size = FOURBYTES + len;
2774 	ad->AttrLen = cpu_to_be16(size);
2775 	ad->AttrType = cpu_to_be16(RHBA_VENDOR_ID);
2776 	return size;
2777 }
2778 
2779 /* Routines for all individual PORT attributes */
2780 static int
2781 lpfc_fdmi_port_attr_fc4type(struct lpfc_vport *vport,
2782 			    struct lpfc_fdmi_attr_def *ad)
2783 {
2784 	struct lpfc_hba   *phba = vport->phba;
2785 	struct lpfc_fdmi_attr_entry *ae;
2786 	uint32_t size;
2787 
2788 	ae = &ad->AttrValue;
2789 	memset(ae, 0, sizeof(*ae));
2790 
2791 	ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
2792 	ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
2793 
2794 	/* Check to see if Firmware supports NVME and on physical port */
2795 	if ((phba->sli_rev == LPFC_SLI_REV4) && (vport == phba->pport) &&
2796 	    phba->sli4_hba.pc_sli4_params.nvme)
2797 		ae->un.AttrTypes[6] = 0x01; /* Type 0x28 - NVME */
2798 
2799 	size = FOURBYTES + 32;
2800 	ad->AttrLen = cpu_to_be16(size);
2801 	ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_FC4_TYPES);
2802 	return size;
2803 }
2804 
2805 static int
2806 lpfc_fdmi_port_attr_support_speed(struct lpfc_vport *vport,
2807 				  struct lpfc_fdmi_attr_def *ad)
2808 {
2809 	struct lpfc_hba   *phba = vport->phba;
2810 	struct lpfc_fdmi_attr_entry *ae;
2811 	uint32_t size;
2812 
2813 	ae = &ad->AttrValue;
2814 
2815 	ae->un.AttrInt = 0;
2816 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2817 		if (phba->lmt & LMT_128Gb)
2818 			ae->un.AttrInt |= HBA_PORTSPEED_128GFC;
2819 		if (phba->lmt & LMT_64Gb)
2820 			ae->un.AttrInt |= HBA_PORTSPEED_64GFC;
2821 		if (phba->lmt & LMT_32Gb)
2822 			ae->un.AttrInt |= HBA_PORTSPEED_32GFC;
2823 		if (phba->lmt & LMT_16Gb)
2824 			ae->un.AttrInt |= HBA_PORTSPEED_16GFC;
2825 		if (phba->lmt & LMT_10Gb)
2826 			ae->un.AttrInt |= HBA_PORTSPEED_10GFC;
2827 		if (phba->lmt & LMT_8Gb)
2828 			ae->un.AttrInt |= HBA_PORTSPEED_8GFC;
2829 		if (phba->lmt & LMT_4Gb)
2830 			ae->un.AttrInt |= HBA_PORTSPEED_4GFC;
2831 		if (phba->lmt & LMT_2Gb)
2832 			ae->un.AttrInt |= HBA_PORTSPEED_2GFC;
2833 		if (phba->lmt & LMT_1Gb)
2834 			ae->un.AttrInt |= HBA_PORTSPEED_1GFC;
2835 	} else {
2836 		/* FCoE links support only one speed */
2837 		switch (phba->fc_linkspeed) {
2838 		case LPFC_ASYNC_LINK_SPEED_10GBPS:
2839 			ae->un.AttrInt = HBA_PORTSPEED_10GE;
2840 			break;
2841 		case LPFC_ASYNC_LINK_SPEED_25GBPS:
2842 			ae->un.AttrInt = HBA_PORTSPEED_25GE;
2843 			break;
2844 		case LPFC_ASYNC_LINK_SPEED_40GBPS:
2845 			ae->un.AttrInt = HBA_PORTSPEED_40GE;
2846 			break;
2847 		case LPFC_ASYNC_LINK_SPEED_100GBPS:
2848 			ae->un.AttrInt = HBA_PORTSPEED_100GE;
2849 			break;
2850 		}
2851 	}
2852 	ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2853 	size = FOURBYTES + sizeof(uint32_t);
2854 	ad->AttrLen = cpu_to_be16(size);
2855 	ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_SPEED);
2856 	return size;
2857 }
2858 
2859 static int
2860 lpfc_fdmi_port_attr_speed(struct lpfc_vport *vport,
2861 			  struct lpfc_fdmi_attr_def *ad)
2862 {
2863 	struct lpfc_hba   *phba = vport->phba;
2864 	struct lpfc_fdmi_attr_entry *ae;
2865 	uint32_t size;
2866 
2867 	ae = &ad->AttrValue;
2868 
2869 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2870 		switch (phba->fc_linkspeed) {
2871 		case LPFC_LINK_SPEED_1GHZ:
2872 			ae->un.AttrInt = HBA_PORTSPEED_1GFC;
2873 			break;
2874 		case LPFC_LINK_SPEED_2GHZ:
2875 			ae->un.AttrInt = HBA_PORTSPEED_2GFC;
2876 			break;
2877 		case LPFC_LINK_SPEED_4GHZ:
2878 			ae->un.AttrInt = HBA_PORTSPEED_4GFC;
2879 			break;
2880 		case LPFC_LINK_SPEED_8GHZ:
2881 			ae->un.AttrInt = HBA_PORTSPEED_8GFC;
2882 			break;
2883 		case LPFC_LINK_SPEED_10GHZ:
2884 			ae->un.AttrInt = HBA_PORTSPEED_10GFC;
2885 			break;
2886 		case LPFC_LINK_SPEED_16GHZ:
2887 			ae->un.AttrInt = HBA_PORTSPEED_16GFC;
2888 			break;
2889 		case LPFC_LINK_SPEED_32GHZ:
2890 			ae->un.AttrInt = HBA_PORTSPEED_32GFC;
2891 			break;
2892 		case LPFC_LINK_SPEED_64GHZ:
2893 			ae->un.AttrInt = HBA_PORTSPEED_64GFC;
2894 			break;
2895 		case LPFC_LINK_SPEED_128GHZ:
2896 			ae->un.AttrInt = HBA_PORTSPEED_128GFC;
2897 			break;
2898 		default:
2899 			ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2900 			break;
2901 		}
2902 	} else {
2903 		switch (phba->fc_linkspeed) {
2904 		case LPFC_ASYNC_LINK_SPEED_10GBPS:
2905 			ae->un.AttrInt = HBA_PORTSPEED_10GE;
2906 			break;
2907 		case LPFC_ASYNC_LINK_SPEED_25GBPS:
2908 			ae->un.AttrInt = HBA_PORTSPEED_25GE;
2909 			break;
2910 		case LPFC_ASYNC_LINK_SPEED_40GBPS:
2911 			ae->un.AttrInt = HBA_PORTSPEED_40GE;
2912 			break;
2913 		case LPFC_ASYNC_LINK_SPEED_100GBPS:
2914 			ae->un.AttrInt = HBA_PORTSPEED_100GE;
2915 			break;
2916 		default:
2917 			ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2918 			break;
2919 		}
2920 	}
2921 
2922 	ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2923 	size = FOURBYTES + sizeof(uint32_t);
2924 	ad->AttrLen = cpu_to_be16(size);
2925 	ad->AttrType = cpu_to_be16(RPRT_PORT_SPEED);
2926 	return size;
2927 }
2928 
2929 static int
2930 lpfc_fdmi_port_attr_max_frame(struct lpfc_vport *vport,
2931 			      struct lpfc_fdmi_attr_def *ad)
2932 {
2933 	struct serv_parm *hsp;
2934 	struct lpfc_fdmi_attr_entry *ae;
2935 	uint32_t size;
2936 
2937 	ae = &ad->AttrValue;
2938 
2939 	hsp = (struct serv_parm *)&vport->fc_sparam;
2940 	ae->un.AttrInt = (((uint32_t) hsp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
2941 			  (uint32_t) hsp->cmn.bbRcvSizeLsb;
2942 	ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2943 	size = FOURBYTES + sizeof(uint32_t);
2944 	ad->AttrLen = cpu_to_be16(size);
2945 	ad->AttrType = cpu_to_be16(RPRT_MAX_FRAME_SIZE);
2946 	return size;
2947 }
2948 
2949 static int
2950 lpfc_fdmi_port_attr_os_devname(struct lpfc_vport *vport,
2951 			       struct lpfc_fdmi_attr_def *ad)
2952 {
2953 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2954 	struct lpfc_fdmi_attr_entry *ae;
2955 	uint32_t len, size;
2956 
2957 	ae = &ad->AttrValue;
2958 	memset(ae, 0, sizeof(*ae));
2959 
2960 	snprintf(ae->un.AttrString, sizeof(ae->un.AttrString),
2961 		 "/sys/class/scsi_host/host%d", shost->host_no);
2962 	len = strnlen((char *)ae->un.AttrString,
2963 			  sizeof(ae->un.AttrString));
2964 	len += (len & 3) ? (4 - (len & 3)) : 4;
2965 	size = FOURBYTES + len;
2966 	ad->AttrLen = cpu_to_be16(size);
2967 	ad->AttrType = cpu_to_be16(RPRT_OS_DEVICE_NAME);
2968 	return size;
2969 }
2970 
2971 static int
2972 lpfc_fdmi_port_attr_host_name(struct lpfc_vport *vport,
2973 			      struct lpfc_fdmi_attr_def *ad)
2974 {
2975 	struct lpfc_fdmi_attr_entry *ae;
2976 	uint32_t len, size;
2977 
2978 	ae = &ad->AttrValue;
2979 	memset(ae, 0, sizeof(*ae));
2980 
2981 	scnprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s",
2982 		  vport->phba->os_host_name);
2983 
2984 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2985 	len += (len & 3) ? (4 - (len & 3)) : 4;
2986 	size = FOURBYTES + len;
2987 	ad->AttrLen = cpu_to_be16(size);
2988 	ad->AttrType = cpu_to_be16(RPRT_HOST_NAME);
2989 	return size;
2990 }
2991 
2992 static int
2993 lpfc_fdmi_port_attr_wwnn(struct lpfc_vport *vport,
2994 			 struct lpfc_fdmi_attr_def *ad)
2995 {
2996 	struct lpfc_fdmi_attr_entry *ae;
2997 	uint32_t size;
2998 
2999 	ae = &ad->AttrValue;
3000 	memset(ae, 0, sizeof(*ae));
3001 
3002 	memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
3003 	       sizeof(struct lpfc_name));
3004 	size = FOURBYTES + sizeof(struct lpfc_name);
3005 	ad->AttrLen = cpu_to_be16(size);
3006 	ad->AttrType = cpu_to_be16(RPRT_NODENAME);
3007 	return size;
3008 }
3009 
3010 static int
3011 lpfc_fdmi_port_attr_wwpn(struct lpfc_vport *vport,
3012 			 struct lpfc_fdmi_attr_def *ad)
3013 {
3014 	struct lpfc_fdmi_attr_entry *ae;
3015 	uint32_t size;
3016 
3017 	ae = &ad->AttrValue;
3018 	memset(ae, 0, sizeof(*ae));
3019 
3020 	memcpy(&ae->un.AttrWWN, &vport->fc_sparam.portName,
3021 	       sizeof(struct lpfc_name));
3022 	size = FOURBYTES + sizeof(struct lpfc_name);
3023 	ad->AttrLen = cpu_to_be16(size);
3024 	ad->AttrType = cpu_to_be16(RPRT_PORTNAME);
3025 	return size;
3026 }
3027 
3028 static int
3029 lpfc_fdmi_port_attr_symbolic_name(struct lpfc_vport *vport,
3030 				  struct lpfc_fdmi_attr_def *ad)
3031 {
3032 	struct lpfc_fdmi_attr_entry *ae;
3033 	uint32_t len, size;
3034 
3035 	ae = &ad->AttrValue;
3036 	memset(ae, 0, sizeof(*ae));
3037 
3038 	len = lpfc_vport_symbolic_port_name(vport, ae->un.AttrString, 256);
3039 	len += (len & 3) ? (4 - (len & 3)) : 4;
3040 	size = FOURBYTES + len;
3041 	ad->AttrLen = cpu_to_be16(size);
3042 	ad->AttrType = cpu_to_be16(RPRT_SYM_PORTNAME);
3043 	return size;
3044 }
3045 
3046 static int
3047 lpfc_fdmi_port_attr_port_type(struct lpfc_vport *vport,
3048 			      struct lpfc_fdmi_attr_def *ad)
3049 {
3050 	struct lpfc_hba *phba = vport->phba;
3051 	struct lpfc_fdmi_attr_entry *ae;
3052 	uint32_t size;
3053 
3054 	ae = &ad->AttrValue;
3055 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
3056 		ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NLPORT);
3057 	else
3058 		ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NPORT);
3059 	size = FOURBYTES + sizeof(uint32_t);
3060 	ad->AttrLen = cpu_to_be16(size);
3061 	ad->AttrType = cpu_to_be16(RPRT_PORT_TYPE);
3062 	return size;
3063 }
3064 
3065 static int
3066 lpfc_fdmi_port_attr_class(struct lpfc_vport *vport,
3067 			  struct lpfc_fdmi_attr_def *ad)
3068 {
3069 	struct lpfc_fdmi_attr_entry *ae;
3070 	uint32_t size;
3071 
3072 	ae = &ad->AttrValue;
3073 	ae->un.AttrInt = cpu_to_be32(FC_COS_CLASS2 | FC_COS_CLASS3);
3074 	size = FOURBYTES + sizeof(uint32_t);
3075 	ad->AttrLen = cpu_to_be16(size);
3076 	ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_CLASS);
3077 	return size;
3078 }
3079 
3080 static int
3081 lpfc_fdmi_port_attr_fabric_wwpn(struct lpfc_vport *vport,
3082 				struct lpfc_fdmi_attr_def *ad)
3083 {
3084 	struct lpfc_fdmi_attr_entry *ae;
3085 	uint32_t size;
3086 
3087 	ae = &ad->AttrValue;
3088 	memset(ae, 0, sizeof(*ae));
3089 
3090 	memcpy(&ae->un.AttrWWN, &vport->fabric_portname,
3091 	       sizeof(struct lpfc_name));
3092 	size = FOURBYTES + sizeof(struct lpfc_name);
3093 	ad->AttrLen = cpu_to_be16(size);
3094 	ad->AttrType = cpu_to_be16(RPRT_FABRICNAME);
3095 	return size;
3096 }
3097 
3098 static int
3099 lpfc_fdmi_port_attr_active_fc4type(struct lpfc_vport *vport,
3100 				   struct lpfc_fdmi_attr_def *ad)
3101 {
3102 	struct lpfc_fdmi_attr_entry *ae;
3103 	uint32_t size;
3104 
3105 	ae = &ad->AttrValue;
3106 	memset(ae, 0, sizeof(*ae));
3107 
3108 	ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
3109 	ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
3110 
3111 	/* Check to see if NVME is configured or not */
3112 	if (vport->phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3113 		ae->un.AttrTypes[6] = 0x1; /* Type 0x28 - NVME */
3114 
3115 	size = FOURBYTES + 32;
3116 	ad->AttrLen = cpu_to_be16(size);
3117 	ad->AttrType = cpu_to_be16(RPRT_ACTIVE_FC4_TYPES);
3118 	return size;
3119 }
3120 
3121 static int
3122 lpfc_fdmi_port_attr_port_state(struct lpfc_vport *vport,
3123 			       struct lpfc_fdmi_attr_def *ad)
3124 {
3125 	struct lpfc_fdmi_attr_entry *ae;
3126 	uint32_t size;
3127 
3128 	ae = &ad->AttrValue;
3129 	/* Link Up - operational */
3130 	ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTSTATE_ONLINE);
3131 	size = FOURBYTES + sizeof(uint32_t);
3132 	ad->AttrLen = cpu_to_be16(size);
3133 	ad->AttrType = cpu_to_be16(RPRT_PORT_STATE);
3134 	return size;
3135 }
3136 
3137 static int
3138 lpfc_fdmi_port_attr_num_disc(struct lpfc_vport *vport,
3139 			     struct lpfc_fdmi_attr_def *ad)
3140 {
3141 	struct lpfc_fdmi_attr_entry *ae;
3142 	uint32_t size;
3143 
3144 	ae = &ad->AttrValue;
3145 	vport->fdmi_num_disc = lpfc_find_map_node(vport);
3146 	ae->un.AttrInt = cpu_to_be32(vport->fdmi_num_disc);
3147 	size = FOURBYTES + sizeof(uint32_t);
3148 	ad->AttrLen = cpu_to_be16(size);
3149 	ad->AttrType = cpu_to_be16(RPRT_DISC_PORT);
3150 	return size;
3151 }
3152 
3153 static int
3154 lpfc_fdmi_port_attr_nportid(struct lpfc_vport *vport,
3155 			    struct lpfc_fdmi_attr_def *ad)
3156 {
3157 	struct lpfc_fdmi_attr_entry *ae;
3158 	uint32_t size;
3159 
3160 	ae = &ad->AttrValue;
3161 	ae->un.AttrInt =  cpu_to_be32(vport->fc_myDID);
3162 	size = FOURBYTES + sizeof(uint32_t);
3163 	ad->AttrLen = cpu_to_be16(size);
3164 	ad->AttrType = cpu_to_be16(RPRT_PORT_ID);
3165 	return size;
3166 }
3167 
3168 static int
3169 lpfc_fdmi_smart_attr_service(struct lpfc_vport *vport,
3170 			     struct lpfc_fdmi_attr_def *ad)
3171 {
3172 	struct lpfc_fdmi_attr_entry *ae;
3173 	uint32_t len, size;
3174 
3175 	ae = &ad->AttrValue;
3176 	memset(ae, 0, sizeof(*ae));
3177 
3178 	strncpy(ae->un.AttrString, "Smart SAN Initiator",
3179 		sizeof(ae->un.AttrString));
3180 	len = strnlen(ae->un.AttrString,
3181 			  sizeof(ae->un.AttrString));
3182 	len += (len & 3) ? (4 - (len & 3)) : 4;
3183 	size = FOURBYTES + len;
3184 	ad->AttrLen = cpu_to_be16(size);
3185 	ad->AttrType = cpu_to_be16(RPRT_SMART_SERVICE);
3186 	return size;
3187 }
3188 
3189 static int
3190 lpfc_fdmi_smart_attr_guid(struct lpfc_vport *vport,
3191 			  struct lpfc_fdmi_attr_def *ad)
3192 {
3193 	struct lpfc_fdmi_attr_entry *ae;
3194 	uint32_t size;
3195 
3196 	ae = &ad->AttrValue;
3197 	memset(ae, 0, sizeof(*ae));
3198 
3199 	memcpy(&ae->un.AttrString, &vport->fc_sparam.nodeName,
3200 	       sizeof(struct lpfc_name));
3201 	memcpy((((uint8_t *)&ae->un.AttrString) +
3202 		sizeof(struct lpfc_name)),
3203 		&vport->fc_sparam.portName, sizeof(struct lpfc_name));
3204 	size = FOURBYTES + (2 * sizeof(struct lpfc_name));
3205 	ad->AttrLen =  cpu_to_be16(size);
3206 	ad->AttrType = cpu_to_be16(RPRT_SMART_GUID);
3207 	return size;
3208 }
3209 
3210 static int
3211 lpfc_fdmi_smart_attr_version(struct lpfc_vport *vport,
3212 			     struct lpfc_fdmi_attr_def *ad)
3213 {
3214 	struct lpfc_fdmi_attr_entry *ae;
3215 	uint32_t len, size;
3216 
3217 	ae = &ad->AttrValue;
3218 	memset(ae, 0, sizeof(*ae));
3219 
3220 	strncpy(ae->un.AttrString, "Smart SAN Version 2.0",
3221 		sizeof(ae->un.AttrString));
3222 	len = strnlen(ae->un.AttrString,
3223 			  sizeof(ae->un.AttrString));
3224 	len += (len & 3) ? (4 - (len & 3)) : 4;
3225 	size = FOURBYTES + len;
3226 	ad->AttrLen =  cpu_to_be16(size);
3227 	ad->AttrType = cpu_to_be16(RPRT_SMART_VERSION);
3228 	return size;
3229 }
3230 
3231 static int
3232 lpfc_fdmi_smart_attr_model(struct lpfc_vport *vport,
3233 			   struct lpfc_fdmi_attr_def *ad)
3234 {
3235 	struct lpfc_hba *phba = vport->phba;
3236 	struct lpfc_fdmi_attr_entry *ae;
3237 	uint32_t len, size;
3238 
3239 	ae = &ad->AttrValue;
3240 	memset(ae, 0, sizeof(*ae));
3241 
3242 	strncpy(ae->un.AttrString, phba->ModelName,
3243 		sizeof(ae->un.AttrString));
3244 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3245 	len += (len & 3) ? (4 - (len & 3)) : 4;
3246 	size = FOURBYTES + len;
3247 	ad->AttrLen = cpu_to_be16(size);
3248 	ad->AttrType = cpu_to_be16(RPRT_SMART_MODEL);
3249 	return size;
3250 }
3251 
3252 static int
3253 lpfc_fdmi_smart_attr_port_info(struct lpfc_vport *vport,
3254 			       struct lpfc_fdmi_attr_def *ad)
3255 {
3256 	struct lpfc_fdmi_attr_entry *ae;
3257 	uint32_t size;
3258 
3259 	ae = &ad->AttrValue;
3260 
3261 	/* SRIOV (type 3) is not supported */
3262 	if (vport->vpi)
3263 		ae->un.AttrInt =  cpu_to_be32(2);  /* NPIV */
3264 	else
3265 		ae->un.AttrInt =  cpu_to_be32(1);  /* Physical */
3266 	size = FOURBYTES + sizeof(uint32_t);
3267 	ad->AttrLen = cpu_to_be16(size);
3268 	ad->AttrType = cpu_to_be16(RPRT_SMART_PORT_INFO);
3269 	return size;
3270 }
3271 
3272 static int
3273 lpfc_fdmi_smart_attr_qos(struct lpfc_vport *vport,
3274 			 struct lpfc_fdmi_attr_def *ad)
3275 {
3276 	struct lpfc_fdmi_attr_entry *ae;
3277 	uint32_t size;
3278 
3279 	ae = &ad->AttrValue;
3280 	ae->un.AttrInt =  cpu_to_be32(0);
3281 	size = FOURBYTES + sizeof(uint32_t);
3282 	ad->AttrLen = cpu_to_be16(size);
3283 	ad->AttrType = cpu_to_be16(RPRT_SMART_QOS);
3284 	return size;
3285 }
3286 
3287 static int
3288 lpfc_fdmi_smart_attr_security(struct lpfc_vport *vport,
3289 			      struct lpfc_fdmi_attr_def *ad)
3290 {
3291 	struct lpfc_fdmi_attr_entry *ae;
3292 	uint32_t size;
3293 
3294 	ae = &ad->AttrValue;
3295 	ae->un.AttrInt =  cpu_to_be32(1);
3296 	size = FOURBYTES + sizeof(uint32_t);
3297 	ad->AttrLen = cpu_to_be16(size);
3298 	ad->AttrType = cpu_to_be16(RPRT_SMART_SECURITY);
3299 	return size;
3300 }
3301 
3302 static int
3303 lpfc_fdmi_vendor_attr_mi(struct lpfc_vport *vport,
3304 			  struct lpfc_fdmi_attr_def *ad)
3305 {
3306 	struct lpfc_hba *phba = vport->phba;
3307 	struct lpfc_fdmi_attr_entry *ae;
3308 	uint32_t len, size;
3309 	char mibrevision[16];
3310 
3311 	ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
3312 	memset(ae, 0, 256);
3313 	sprintf(mibrevision, "ELXE2EM:%04d",
3314 		phba->sli4_hba.pc_sli4_params.mi_value);
3315 	strncpy(ae->un.AttrString, &mibrevision[0], sizeof(ae->un.AttrString));
3316 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3317 	len += (len & 3) ? (4 - (len & 3)) : 4;
3318 	size = FOURBYTES + len;
3319 	ad->AttrLen = cpu_to_be16(size);
3320 	ad->AttrType = cpu_to_be16(RPRT_VENDOR_MI);
3321 	return size;
3322 }
3323 
3324 /* RHBA attribute jump table */
3325 int (*lpfc_fdmi_hba_action[])
3326 	(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
3327 	/* Action routine                 Mask bit     Attribute type */
3328 	lpfc_fdmi_hba_attr_wwnn,	  /* bit0     RHBA_NODENAME           */
3329 	lpfc_fdmi_hba_attr_manufacturer,  /* bit1     RHBA_MANUFACTURER       */
3330 	lpfc_fdmi_hba_attr_sn,		  /* bit2     RHBA_SERIAL_NUMBER      */
3331 	lpfc_fdmi_hba_attr_model,	  /* bit3     RHBA_MODEL              */
3332 	lpfc_fdmi_hba_attr_description,	  /* bit4     RHBA_MODEL_DESCRIPTION  */
3333 	lpfc_fdmi_hba_attr_hdw_ver,	  /* bit5     RHBA_HARDWARE_VERSION   */
3334 	lpfc_fdmi_hba_attr_drvr_ver,	  /* bit6     RHBA_DRIVER_VERSION     */
3335 	lpfc_fdmi_hba_attr_rom_ver,	  /* bit7     RHBA_OPTION_ROM_VERSION */
3336 	lpfc_fdmi_hba_attr_fmw_ver,	  /* bit8     RHBA_FIRMWARE_VERSION   */
3337 	lpfc_fdmi_hba_attr_os_ver,	  /* bit9     RHBA_OS_NAME_VERSION    */
3338 	lpfc_fdmi_hba_attr_ct_len,	  /* bit10    RHBA_MAX_CT_PAYLOAD_LEN */
3339 	lpfc_fdmi_hba_attr_symbolic_name, /* bit11    RHBA_SYM_NODENAME       */
3340 	lpfc_fdmi_hba_attr_vendor_info,	  /* bit12    RHBA_VENDOR_INFO        */
3341 	lpfc_fdmi_hba_attr_num_ports,	  /* bit13    RHBA_NUM_PORTS          */
3342 	lpfc_fdmi_hba_attr_fabric_wwnn,	  /* bit14    RHBA_FABRIC_WWNN        */
3343 	lpfc_fdmi_hba_attr_bios_ver,	  /* bit15    RHBA_BIOS_VERSION       */
3344 	lpfc_fdmi_hba_attr_bios_state,	  /* bit16    RHBA_BIOS_STATE         */
3345 	lpfc_fdmi_hba_attr_vendor_id,	  /* bit17    RHBA_VENDOR_ID          */
3346 };
3347 
3348 /* RPA / RPRT attribute jump table */
3349 int (*lpfc_fdmi_port_action[])
3350 	(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
3351 	/* Action routine                   Mask bit   Attribute type */
3352 	lpfc_fdmi_port_attr_fc4type,        /* bit0   RPRT_SUPPORT_FC4_TYPES  */
3353 	lpfc_fdmi_port_attr_support_speed,  /* bit1   RPRT_SUPPORTED_SPEED    */
3354 	lpfc_fdmi_port_attr_speed,          /* bit2   RPRT_PORT_SPEED         */
3355 	lpfc_fdmi_port_attr_max_frame,      /* bit3   RPRT_MAX_FRAME_SIZE     */
3356 	lpfc_fdmi_port_attr_os_devname,     /* bit4   RPRT_OS_DEVICE_NAME     */
3357 	lpfc_fdmi_port_attr_host_name,      /* bit5   RPRT_HOST_NAME          */
3358 	lpfc_fdmi_port_attr_wwnn,           /* bit6   RPRT_NODENAME           */
3359 	lpfc_fdmi_port_attr_wwpn,           /* bit7   RPRT_PORTNAME           */
3360 	lpfc_fdmi_port_attr_symbolic_name,  /* bit8   RPRT_SYM_PORTNAME       */
3361 	lpfc_fdmi_port_attr_port_type,      /* bit9   RPRT_PORT_TYPE          */
3362 	lpfc_fdmi_port_attr_class,          /* bit10  RPRT_SUPPORTED_CLASS    */
3363 	lpfc_fdmi_port_attr_fabric_wwpn,    /* bit11  RPRT_FABRICNAME         */
3364 	lpfc_fdmi_port_attr_active_fc4type, /* bit12  RPRT_ACTIVE_FC4_TYPES   */
3365 	lpfc_fdmi_port_attr_port_state,     /* bit13  RPRT_PORT_STATE         */
3366 	lpfc_fdmi_port_attr_num_disc,       /* bit14  RPRT_DISC_PORT          */
3367 	lpfc_fdmi_port_attr_nportid,        /* bit15  RPRT_PORT_ID            */
3368 	lpfc_fdmi_smart_attr_service,       /* bit16  RPRT_SMART_SERVICE      */
3369 	lpfc_fdmi_smart_attr_guid,          /* bit17  RPRT_SMART_GUID         */
3370 	lpfc_fdmi_smart_attr_version,       /* bit18  RPRT_SMART_VERSION      */
3371 	lpfc_fdmi_smart_attr_model,         /* bit19  RPRT_SMART_MODEL        */
3372 	lpfc_fdmi_smart_attr_port_info,     /* bit20  RPRT_SMART_PORT_INFO    */
3373 	lpfc_fdmi_smart_attr_qos,           /* bit21  RPRT_SMART_QOS          */
3374 	lpfc_fdmi_smart_attr_security,      /* bit22  RPRT_SMART_SECURITY     */
3375 	lpfc_fdmi_vendor_attr_mi,           /* bit23  RPRT_VENDOR_MI          */
3376 };
3377 
3378 /**
3379  * lpfc_fdmi_cmd - Build and send a FDMI cmd to the specified NPort
3380  * @vport: pointer to a host virtual N_Port data structure.
3381  * @ndlp: ndlp to send FDMI cmd to (if NULL use FDMI_DID)
3382  * @cmdcode: FDMI command to send
3383  * @new_mask: Mask of HBA or PORT Attributes to send
3384  *
3385  * Builds and sends a FDMI command using the CT subsystem.
3386  */
3387 int
3388 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3389 	      int cmdcode, uint32_t new_mask)
3390 {
3391 	struct lpfc_hba *phba = vport->phba;
3392 	struct lpfc_dmabuf *mp, *bmp;
3393 	struct lpfc_sli_ct_request *CtReq;
3394 	struct ulp_bde64 *bpl;
3395 	uint32_t bit_pos;
3396 	uint32_t size;
3397 	uint32_t rsp_size;
3398 	uint32_t mask;
3399 	struct lpfc_fdmi_reg_hba *rh;
3400 	struct lpfc_fdmi_port_entry *pe;
3401 	struct lpfc_fdmi_reg_portattr *pab = NULL;
3402 	struct lpfc_fdmi_attr_block *ab = NULL;
3403 	int  (*func)(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad);
3404 	void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
3405 		     struct lpfc_iocbq *);
3406 
3407 	if (!ndlp)
3408 		return 0;
3409 
3410 	cmpl = lpfc_cmpl_ct_disc_fdmi; /* called from discovery */
3411 
3412 	/* fill in BDEs for command */
3413 	/* Allocate buffer for command payload */
3414 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3415 	if (!mp)
3416 		goto fdmi_cmd_exit;
3417 
3418 	mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
3419 	if (!mp->virt)
3420 		goto fdmi_cmd_free_mp;
3421 
3422 	/* Allocate buffer for Buffer ptr list */
3423 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3424 	if (!bmp)
3425 		goto fdmi_cmd_free_mpvirt;
3426 
3427 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
3428 	if (!bmp->virt)
3429 		goto fdmi_cmd_free_bmp;
3430 
3431 	INIT_LIST_HEAD(&mp->list);
3432 	INIT_LIST_HEAD(&bmp->list);
3433 
3434 	/* FDMI request */
3435 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3436 			 "0218 FDMI Request Data: x%x x%x x%x\n",
3437 			 vport->fc_flag, vport->port_state, cmdcode);
3438 	CtReq = (struct lpfc_sli_ct_request *)mp->virt;
3439 
3440 	/* First populate the CT_IU preamble */
3441 	memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
3442 	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
3443 	CtReq->RevisionId.bits.InId = 0;
3444 
3445 	CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
3446 	CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
3447 
3448 	CtReq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
3449 	rsp_size = LPFC_BPL_SIZE;
3450 	size = 0;
3451 
3452 	/* Next fill in the specific FDMI cmd information */
3453 	switch (cmdcode) {
3454 	case SLI_MGMT_RHAT:
3455 	case SLI_MGMT_RHBA:
3456 		rh = (struct lpfc_fdmi_reg_hba *)&CtReq->un.PortID;
3457 		/* HBA Identifier */
3458 		memcpy(&rh->hi.PortName, &phba->pport->fc_sparam.portName,
3459 		       sizeof(struct lpfc_name));
3460 
3461 		if (cmdcode == SLI_MGMT_RHBA) {
3462 			/* Registered Port List */
3463 			/* One entry (port) per adapter */
3464 			rh->rpl.EntryCnt = cpu_to_be32(1);
3465 			memcpy(&rh->rpl.pe.PortName,
3466 			       &phba->pport->fc_sparam.portName,
3467 			       sizeof(struct lpfc_name));
3468 
3469 			/* point to the HBA attribute block */
3470 			size = 2 * sizeof(struct lpfc_name) +
3471 				FOURBYTES;
3472 		} else {
3473 			size = sizeof(struct lpfc_name);
3474 		}
3475 		ab = (struct lpfc_fdmi_attr_block *)((uint8_t *)rh + size);
3476 		ab->EntryCnt = 0;
3477 		size += FOURBYTES;
3478 		bit_pos = 0;
3479 		if (new_mask)
3480 			mask = new_mask;
3481 		else
3482 			mask = vport->fdmi_hba_mask;
3483 
3484 		/* Mask will dictate what attributes to build in the request */
3485 		while (mask) {
3486 			if (mask & 0x1) {
3487 				func = lpfc_fdmi_hba_action[bit_pos];
3488 				size += func(vport,
3489 					     (struct lpfc_fdmi_attr_def *)
3490 					     ((uint8_t *)rh + size));
3491 				ab->EntryCnt++;
3492 				if ((size + 256) >
3493 				    (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3494 					goto hba_out;
3495 			}
3496 			mask = mask >> 1;
3497 			bit_pos++;
3498 		}
3499 hba_out:
3500 		ab->EntryCnt = cpu_to_be32(ab->EntryCnt);
3501 		/* Total size */
3502 		size = GID_REQUEST_SZ - 4 + size;
3503 		break;
3504 
3505 	case SLI_MGMT_RPRT:
3506 	case SLI_MGMT_RPA:
3507 		pab = (struct lpfc_fdmi_reg_portattr *)&CtReq->un.PortID;
3508 		if (cmdcode == SLI_MGMT_RPRT) {
3509 			rh = (struct lpfc_fdmi_reg_hba *)pab;
3510 			/* HBA Identifier */
3511 			memcpy(&rh->hi.PortName,
3512 			       &phba->pport->fc_sparam.portName,
3513 			       sizeof(struct lpfc_name));
3514 			pab = (struct lpfc_fdmi_reg_portattr *)
3515 				((uint8_t *)pab +  sizeof(struct lpfc_name));
3516 		}
3517 
3518 		memcpy((uint8_t *)&pab->PortName,
3519 		       (uint8_t *)&vport->fc_sparam.portName,
3520 		       sizeof(struct lpfc_name));
3521 		size += sizeof(struct lpfc_name) + FOURBYTES;
3522 		pab->ab.EntryCnt = 0;
3523 		bit_pos = 0;
3524 		if (new_mask)
3525 			mask = new_mask;
3526 		else
3527 			mask = vport->fdmi_port_mask;
3528 
3529 		/* Mask will dictate what attributes to build in the request */
3530 		while (mask) {
3531 			if (mask & 0x1) {
3532 				func = lpfc_fdmi_port_action[bit_pos];
3533 				size += func(vport,
3534 					     (struct lpfc_fdmi_attr_def *)
3535 					     ((uint8_t *)pab + size));
3536 				pab->ab.EntryCnt++;
3537 				if ((size + 256) >
3538 				    (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3539 					goto port_out;
3540 			}
3541 			mask = mask >> 1;
3542 			bit_pos++;
3543 		}
3544 port_out:
3545 		pab->ab.EntryCnt = cpu_to_be32(pab->ab.EntryCnt);
3546 		/* Total size */
3547 		if (cmdcode == SLI_MGMT_RPRT)
3548 			size += sizeof(struct lpfc_name);
3549 		size = GID_REQUEST_SZ - 4 + size;
3550 		break;
3551 
3552 	case SLI_MGMT_GHAT:
3553 	case SLI_MGMT_GRPL:
3554 		rsp_size = FC_MAX_NS_RSP;
3555 		fallthrough;
3556 	case SLI_MGMT_DHBA:
3557 	case SLI_MGMT_DHAT:
3558 		pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3559 		memcpy((uint8_t *)&pe->PortName,
3560 		       (uint8_t *)&vport->fc_sparam.portName,
3561 		       sizeof(struct lpfc_name));
3562 		size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3563 		break;
3564 
3565 	case SLI_MGMT_GPAT:
3566 	case SLI_MGMT_GPAS:
3567 		rsp_size = FC_MAX_NS_RSP;
3568 		fallthrough;
3569 	case SLI_MGMT_DPRT:
3570 	case SLI_MGMT_DPA:
3571 		pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3572 		memcpy((uint8_t *)&pe->PortName,
3573 		       (uint8_t *)&vport->fc_sparam.portName,
3574 		       sizeof(struct lpfc_name));
3575 		size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3576 		break;
3577 	case SLI_MGMT_GRHL:
3578 		size = GID_REQUEST_SZ - 4;
3579 		break;
3580 	default:
3581 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3582 				 "0298 FDMI cmdcode x%x not supported\n",
3583 				 cmdcode);
3584 		goto fdmi_cmd_free_bmpvirt;
3585 	}
3586 	CtReq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);
3587 
3588 	bpl = (struct ulp_bde64 *)bmp->virt;
3589 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
3590 	bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
3591 	bpl->tus.f.bdeFlags = 0;
3592 	bpl->tus.f.bdeSize = size;
3593 
3594 	/*
3595 	 * The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
3596 	 * to hold ndlp reference for the corresponding callback function.
3597 	 */
3598 	if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, 0))
3599 		return 0;
3600 
3601 fdmi_cmd_free_bmpvirt:
3602 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3603 fdmi_cmd_free_bmp:
3604 	kfree(bmp);
3605 fdmi_cmd_free_mpvirt:
3606 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3607 fdmi_cmd_free_mp:
3608 	kfree(mp);
3609 fdmi_cmd_exit:
3610 	/* Issue FDMI request failed */
3611 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3612 			 "0244 Issue FDMI request failed Data: x%x\n",
3613 			 cmdcode);
3614 	return 1;
3615 }
3616 
3617 /**
3618  * lpfc_delayed_disc_tmo - Timeout handler for delayed discovery timer.
3619  * @t: Context object of the timer.
3620  *
3621  * This function set the WORKER_DELAYED_DISC_TMO flag and wake up
3622  * the worker thread.
3623  **/
3624 void
3625 lpfc_delayed_disc_tmo(struct timer_list *t)
3626 {
3627 	struct lpfc_vport *vport = from_timer(vport, t, delayed_disc_tmo);
3628 	struct lpfc_hba   *phba = vport->phba;
3629 	uint32_t tmo_posted;
3630 	unsigned long iflag;
3631 
3632 	spin_lock_irqsave(&vport->work_port_lock, iflag);
3633 	tmo_posted = vport->work_port_events & WORKER_DELAYED_DISC_TMO;
3634 	if (!tmo_posted)
3635 		vport->work_port_events |= WORKER_DELAYED_DISC_TMO;
3636 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3637 
3638 	if (!tmo_posted)
3639 		lpfc_worker_wake_up(phba);
3640 	return;
3641 }
3642 
3643 /**
3644  * lpfc_delayed_disc_timeout_handler - Function called by worker thread to
3645  *      handle delayed discovery.
3646  * @vport: pointer to a host virtual N_Port data structure.
3647  *
3648  * This function start nport discovery of the vport.
3649  **/
3650 void
3651 lpfc_delayed_disc_timeout_handler(struct lpfc_vport *vport)
3652 {
3653 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3654 
3655 	spin_lock_irq(shost->host_lock);
3656 	if (!(vport->fc_flag & FC_DISC_DELAYED)) {
3657 		spin_unlock_irq(shost->host_lock);
3658 		return;
3659 	}
3660 	vport->fc_flag &= ~FC_DISC_DELAYED;
3661 	spin_unlock_irq(shost->host_lock);
3662 
3663 	lpfc_do_scr_ns_plogi(vport->phba, vport);
3664 }
3665 
3666 void
3667 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
3668 {
3669 	struct lpfc_sli *psli = &phba->sli;
3670 	lpfc_vpd_t *vp = &phba->vpd;
3671 	uint32_t b1, b2, b3, b4, i, rev;
3672 	char c;
3673 	uint32_t *ptr, str[4];
3674 	uint8_t *fwname;
3675 
3676 	if (phba->sli_rev == LPFC_SLI_REV4)
3677 		snprintf(fwrevision, FW_REV_STR_SIZE, "%s", vp->rev.opFwName);
3678 	else if (vp->rev.rBit) {
3679 		if (psli->sli_flag & LPFC_SLI_ACTIVE)
3680 			rev = vp->rev.sli2FwRev;
3681 		else
3682 			rev = vp->rev.sli1FwRev;
3683 
3684 		b1 = (rev & 0x0000f000) >> 12;
3685 		b2 = (rev & 0x00000f00) >> 8;
3686 		b3 = (rev & 0x000000c0) >> 6;
3687 		b4 = (rev & 0x00000030) >> 4;
3688 
3689 		switch (b4) {
3690 		case 0:
3691 			c = 'N';
3692 			break;
3693 		case 1:
3694 			c = 'A';
3695 			break;
3696 		case 2:
3697 			c = 'B';
3698 			break;
3699 		case 3:
3700 			c = 'X';
3701 			break;
3702 		default:
3703 			c = 0;
3704 			break;
3705 		}
3706 		b4 = (rev & 0x0000000f);
3707 
3708 		if (psli->sli_flag & LPFC_SLI_ACTIVE)
3709 			fwname = vp->rev.sli2FwName;
3710 		else
3711 			fwname = vp->rev.sli1FwName;
3712 
3713 		for (i = 0; i < 16; i++)
3714 			if (fwname[i] == 0x20)
3715 				fwname[i] = 0;
3716 
3717 		ptr = (uint32_t*)fwname;
3718 
3719 		for (i = 0; i < 3; i++)
3720 			str[i] = be32_to_cpu(*ptr++);
3721 
3722 		if (c == 0) {
3723 			if (flag)
3724 				sprintf(fwrevision, "%d.%d%d (%s)",
3725 					b1, b2, b3, (char *)str);
3726 			else
3727 				sprintf(fwrevision, "%d.%d%d", b1,
3728 					b2, b3);
3729 		} else {
3730 			if (flag)
3731 				sprintf(fwrevision, "%d.%d%d%c%d (%s)",
3732 					b1, b2, b3, c,
3733 					b4, (char *)str);
3734 			else
3735 				sprintf(fwrevision, "%d.%d%d%c%d",
3736 					b1, b2, b3, c, b4);
3737 		}
3738 	} else {
3739 		rev = vp->rev.smFwRev;
3740 
3741 		b1 = (rev & 0xff000000) >> 24;
3742 		b2 = (rev & 0x00f00000) >> 20;
3743 		b3 = (rev & 0x000f0000) >> 16;
3744 		c  = (rev & 0x0000ff00) >> 8;
3745 		b4 = (rev & 0x000000ff);
3746 
3747 		sprintf(fwrevision, "%d.%d%d%c%d", b1, b2, b3, c, b4);
3748 	}
3749 	return;
3750 }
3751