xref: /linux/drivers/scsi/lpfc/lpfc_ct.c (revision 94a0dfcf7d33ea96bf3eb0c33e4239942a4ff087)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2020 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  * @ctiocb: 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 	if (ctiocb->context_un.ndlp) {
552 		lpfc_nlp_put(ctiocb->context_un.ndlp);
553 		ctiocb->context_un.ndlp = NULL;
554 	}
555 	if (ctiocb->context1) {
556 		buf_ptr = (struct lpfc_dmabuf *) ctiocb->context1;
557 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
558 		kfree(buf_ptr);
559 		ctiocb->context1 = NULL;
560 	}
561 	if (ctiocb->context2) {
562 		lpfc_free_ct_rsp(phba, (struct lpfc_dmabuf *) ctiocb->context2);
563 		ctiocb->context2 = NULL;
564 	}
565 
566 	if (ctiocb->context3) {
567 		buf_ptr = (struct lpfc_dmabuf *) ctiocb->context3;
568 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
569 		kfree(buf_ptr);
570 		ctiocb->context3 = NULL;
571 	}
572 	lpfc_sli_release_iocbq(phba, ctiocb);
573 	return 0;
574 }
575 
576 /*
577  * lpfc_gen_req - Build and issue a GEN_REQUEST command  to the SLI Layer
578  * @vport: pointer to a host virtual N_Port data structure.
579  * @bmp: Pointer to BPL for SLI command
580  * @inp: Pointer to data buffer for response data.
581  * @outp: Pointer to data buffer that hold the CT command.
582  * @cmpl: completion routine to call when command completes
583  * @ndlp: Destination NPort nodelist entry
584  *
585  * This function as the final part for issuing a CT command.
586  */
587 static int
588 lpfc_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
589 	     struct lpfc_dmabuf *inp, struct lpfc_dmabuf *outp,
590 	     void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
591 		     struct lpfc_iocbq *),
592 	     struct lpfc_nodelist *ndlp, uint32_t usr_flg, uint32_t num_entry,
593 	     uint32_t tmo, uint8_t retry)
594 {
595 	struct lpfc_hba  *phba = vport->phba;
596 	IOCB_t *icmd;
597 	struct lpfc_iocbq *geniocb;
598 	int rc;
599 
600 	/* Allocate buffer for  command iocb */
601 	geniocb = lpfc_sli_get_iocbq(phba);
602 
603 	if (geniocb == NULL)
604 		return 1;
605 
606 	icmd = &geniocb->iocb;
607 	icmd->un.genreq64.bdl.ulpIoTag32 = 0;
608 	icmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
609 	icmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
610 	icmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
611 	icmd->un.genreq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
612 
613 	if (usr_flg)
614 		geniocb->context3 = NULL;
615 	else
616 		geniocb->context3 = (uint8_t *) bmp;
617 
618 	/* Save for completion so we can release these resources */
619 	geniocb->context1 = (uint8_t *) inp;
620 	geniocb->context2 = (uint8_t *) outp;
621 	geniocb->context_un.ndlp = lpfc_nlp_get(ndlp);
622 
623 	/* Fill in payload, bp points to frame payload */
624 	icmd->ulpCommand = CMD_GEN_REQUEST64_CR;
625 
626 	/* Fill in rest of iocb */
627 	icmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
628 	icmd->un.genreq64.w5.hcsw.Dfctl = 0;
629 	icmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
630 	icmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
631 
632 	if (!tmo) {
633 		 /* FC spec states we need 3 * ratov for CT requests */
634 		tmo = (3 * phba->fc_ratov);
635 	}
636 	icmd->ulpTimeout = tmo;
637 	icmd->ulpBdeCount = 1;
638 	icmd->ulpLe = 1;
639 	icmd->ulpClass = CLASS3;
640 	icmd->ulpContext = ndlp->nlp_rpi;
641 	if (phba->sli_rev == LPFC_SLI_REV4)
642 		icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
643 
644 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
645 		/* For GEN_REQUEST64_CR, use the RPI */
646 		icmd->ulpCt_h = 0;
647 		icmd->ulpCt_l = 0;
648 	}
649 
650 	/* Issue GEN REQ IOCB for NPORT <did> */
651 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
652 			 "0119 Issue GEN REQ IOCB to NPORT x%x "
653 			 "Data: x%x x%x\n",
654 			 ndlp->nlp_DID, icmd->ulpIoTag,
655 			 vport->port_state);
656 	geniocb->iocb_cmpl = cmpl;
657 	geniocb->drvrTimeout = icmd->ulpTimeout + LPFC_DRVR_TIMEOUT;
658 	geniocb->vport = vport;
659 	geniocb->retry = retry;
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 		lpfc_sli_release_iocbq(phba, geniocb);
666 		return 1;
667 	}
668 
669 	return 0;
670 }
671 
672 /*
673  * lpfc_ct_cmd - Build and issue a CT command
674  * @vport: pointer to a host virtual N_Port data structure.
675  * @inmp: Pointer to data buffer for response data.
676  * @bmp: Pointer to BPL for SLI command
677  * @ndlp: Destination NPort nodelist entry
678  * @cmpl: completion routine to call when command completes
679  *
680  * This function is called for issuing a CT command.
681  */
682 static int
683 lpfc_ct_cmd(struct lpfc_vport *vport, struct lpfc_dmabuf *inmp,
684 	    struct lpfc_dmabuf *bmp, struct lpfc_nodelist *ndlp,
685 	    void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
686 			  struct lpfc_iocbq *),
687 	    uint32_t rsp_size, uint8_t retry)
688 {
689 	struct lpfc_hba  *phba = vport->phba;
690 	struct ulp_bde64 *bpl = (struct ulp_bde64 *) bmp->virt;
691 	struct lpfc_dmabuf *outmp;
692 	int cnt = 0, status;
693 	int cmdcode = ((struct lpfc_sli_ct_request *) inmp->virt)->
694 		CommandResponse.bits.CmdRsp;
695 
696 	bpl++;			/* Skip past ct request */
697 
698 	/* Put buffer(s) for ct rsp in bpl */
699 	outmp = lpfc_alloc_ct_rsp(phba, cmdcode, bpl, rsp_size, &cnt);
700 	if (!outmp)
701 		return -ENOMEM;
702 	/*
703 	 * Form the CT IOCB.  The total number of BDEs in this IOCB
704 	 * is the single command plus response count from
705 	 * lpfc_alloc_ct_rsp.
706 	 */
707 	cnt += 1;
708 	status = lpfc_gen_req(vport, bmp, inmp, outmp, cmpl, ndlp, 0,
709 			      cnt, 0, retry);
710 	if (status) {
711 		lpfc_free_ct_rsp(phba, outmp);
712 		return -ENOMEM;
713 	}
714 	return 0;
715 }
716 
717 struct lpfc_vport *
718 lpfc_find_vport_by_did(struct lpfc_hba *phba, uint32_t did) {
719 	struct lpfc_vport *vport_curr;
720 	unsigned long flags;
721 
722 	spin_lock_irqsave(&phba->port_list_lock, flags);
723 	list_for_each_entry(vport_curr, &phba->port_list, listentry) {
724 		if ((vport_curr->fc_myDID) && (vport_curr->fc_myDID == did)) {
725 			spin_unlock_irqrestore(&phba->port_list_lock, flags);
726 			return vport_curr;
727 		}
728 	}
729 	spin_unlock_irqrestore(&phba->port_list_lock, flags);
730 	return NULL;
731 }
732 
733 static void
734 lpfc_prep_node_fc4type(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
735 {
736 	struct lpfc_nodelist *ndlp;
737 
738 	if ((vport->port_type != LPFC_NPIV_PORT) ||
739 	    !(vport->ct_flags & FC_CT_RFF_ID) || !vport->cfg_restrict_login) {
740 
741 		ndlp = lpfc_setup_disc_node(vport, Did);
742 
743 		if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
744 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
745 				"Parse GID_FTrsp: did:x%x flg:x%x x%x",
746 				Did, ndlp->nlp_flag, vport->fc_flag);
747 
748 			/* By default, the driver expects to support FCP FC4 */
749 			if (fc4_type == FC_TYPE_FCP)
750 				ndlp->nlp_fc4_type |= NLP_FC4_FCP;
751 
752 			if (fc4_type == FC_TYPE_NVME)
753 				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
754 
755 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
756 					 "0238 Process x%06x NameServer Rsp "
757 					 "Data: x%x x%x x%x x%x x%x\n", Did,
758 					 ndlp->nlp_flag, ndlp->nlp_fc4_type,
759 					 ndlp->nlp_state, vport->fc_flag,
760 					 vport->fc_rscn_id_cnt);
761 
762 			/* if ndlp needs to be discovered and prior
763 			 * state of ndlp hit devloss, change state to
764 			 * allow rediscovery.
765 			 */
766 			if (ndlp->nlp_flag & NLP_NPR_2B_DISC &&
767 			    ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
768 				lpfc_nlp_set_state(vport, ndlp,
769 						   NLP_STE_NPR_NODE);
770 			}
771 		} else {
772 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
773 				"Skip1 GID_FTrsp: did:x%x flg:x%x cnt:%d",
774 				Did, vport->fc_flag, vport->fc_rscn_id_cnt);
775 
776 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
777 					 "0239 Skip x%06x NameServer Rsp "
778 					 "Data: x%x x%x %p\n",
779 					 Did, vport->fc_flag,
780 					 vport->fc_rscn_id_cnt, ndlp);
781 		}
782 	} else {
783 		if (!(vport->fc_flag & FC_RSCN_MODE) ||
784 		    lpfc_rscn_payload_check(vport, Did)) {
785 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
786 				"Query GID_FTrsp: did:x%x flg:x%x cnt:%d",
787 				Did, vport->fc_flag, vport->fc_rscn_id_cnt);
788 
789 			/*
790 			 * This NPortID was previously a FCP/NVMe target,
791 			 * Don't even bother to send GFF_ID.
792 			 */
793 			ndlp = lpfc_findnode_did(vport, Did);
794 			if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
795 			    (ndlp->nlp_type &
796 			    (NLP_FCP_TARGET | NLP_NVME_TARGET))) {
797 				if (fc4_type == FC_TYPE_FCP)
798 					ndlp->nlp_fc4_type |= NLP_FC4_FCP;
799 				if (fc4_type == FC_TYPE_NVME)
800 					ndlp->nlp_fc4_type |= NLP_FC4_NVME;
801 				lpfc_setup_disc_node(vport, Did);
802 			} else if (lpfc_ns_cmd(vport, SLI_CTNS_GFF_ID,
803 				   0, Did) == 0)
804 				vport->num_disc_nodes++;
805 			else
806 				lpfc_setup_disc_node(vport, Did);
807 		} else {
808 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
809 				"Skip2 GID_FTrsp: did:x%x flg:x%x cnt:%d",
810 				Did, vport->fc_flag, vport->fc_rscn_id_cnt);
811 
812 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
813 					 "0245 Skip x%06x NameServer Rsp "
814 					 "Data: x%x x%x\n", Did,
815 					 vport->fc_flag,
816 					 vport->fc_rscn_id_cnt);
817 		}
818 	}
819 }
820 
821 static void
822 lpfc_ns_rsp_audit_did(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
823 {
824 	struct lpfc_hba *phba = vport->phba;
825 	struct lpfc_nodelist *ndlp = NULL;
826 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
827 	char *str;
828 
829 	if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT)
830 		str = "GID_FT";
831 	else
832 		str = "GID_PT";
833 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
834 			 "6430 Process %s rsp for %08x type %x %s %s\n",
835 			 str, Did, fc4_type,
836 			 (fc4_type == FC_TYPE_FCP) ?  "FCP" : " ",
837 			 (fc4_type == FC_TYPE_NVME) ?  "NVME" : " ");
838 	/*
839 	 * To conserve rpi's, filter out addresses for other
840 	 * vports on the same physical HBAs.
841 	 */
842 	if (Did != vport->fc_myDID &&
843 	    (!lpfc_find_vport_by_did(phba, Did) ||
844 	     vport->cfg_peer_port_login)) {
845 		if (!phba->nvmet_support) {
846 			/* FCPI/NVMEI path. Process Did */
847 			lpfc_prep_node_fc4type(vport, Did, fc4_type);
848 			return;
849 		}
850 		/* NVMET path.  NVMET only cares about NVMEI nodes. */
851 		list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
852 			if (ndlp->nlp_type != NLP_NVME_INITIATOR ||
853 			    ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
854 				continue;
855 			spin_lock_irq(shost->host_lock);
856 			if (ndlp->nlp_DID == Did)
857 				ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
858 			else
859 				ndlp->nlp_flag |= NLP_NVMET_RECOV;
860 			spin_unlock_irq(shost->host_lock);
861 		}
862 	}
863 }
864 
865 static int
866 lpfc_ns_rsp(struct lpfc_vport *vport, struct lpfc_dmabuf *mp, uint8_t fc4_type,
867 	    uint32_t Size)
868 {
869 	struct lpfc_sli_ct_request *Response =
870 		(struct lpfc_sli_ct_request *) mp->virt;
871 	struct lpfc_dmabuf *mlast, *next_mp;
872 	uint32_t *ctptr = (uint32_t *) & Response->un.gid.PortType;
873 	uint32_t Did, CTentry;
874 	int Cnt;
875 	struct list_head head;
876 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
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(shost->host_lock);
923 			ndlp->nlp_flag &= ~NLP_NVMET_RECOV;
924 			spin_unlock_irq(shost->host_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 	cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
1138 	lpfc_ct_free_iocb(phba, cmdiocb);
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 	cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
1345 	lpfc_ct_free_iocb(phba, cmdiocb);
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;
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 					lpfc_ct_free_iocb(phba, cmdiocb);
1427 					return;
1428 				}
1429 			}
1430 		}
1431 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1432 				 "0267 NameServer GFF Rsp "
1433 				 "x%x Error (%d %d) Data: x%x x%x\n",
1434 				 did, irsp->ulpStatus, irsp->un.ulpWord[4],
1435 				 vport->fc_flag, vport->fc_rscn_id_cnt);
1436 	}
1437 
1438 	/* This is a target port, unregistered port, or the GFF_ID failed */
1439 	ndlp = lpfc_setup_disc_node(vport, did);
1440 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
1441 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1442 				 "0242 Process x%x GFF "
1443 				 "NameServer Rsp Data: x%x x%x x%x\n",
1444 				 did, ndlp->nlp_flag, vport->fc_flag,
1445 				 vport->fc_rscn_id_cnt);
1446 	} else {
1447 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1448 				 "0243 Skip x%x GFF "
1449 				 "NameServer Rsp Data: x%x x%x\n", did,
1450 				 vport->fc_flag, vport->fc_rscn_id_cnt);
1451 	}
1452 out:
1453 	/* Link up / RSCN discovery */
1454 	if (vport->num_disc_nodes)
1455 		vport->num_disc_nodes--;
1456 
1457 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1458 			 "6451 GFF_ID cmpl inp %d disc %d\n",
1459 			 vport->gidft_inp, vport->num_disc_nodes);
1460 
1461 	if (vport->num_disc_nodes == 0) {
1462 		/*
1463 		 * The driver has cycled through all Nports in the RSCN payload.
1464 		 * Complete the handling by cleaning up and marking the
1465 		 * current driver state.
1466 		 */
1467 		if (vport->port_state >= LPFC_DISC_AUTH) {
1468 			if (vport->fc_flag & FC_RSCN_MODE) {
1469 				lpfc_els_flush_rscn(vport);
1470 				spin_lock_irq(shost->host_lock);
1471 				vport->fc_flag |= FC_RSCN_MODE; /* RSCN still */
1472 				spin_unlock_irq(shost->host_lock);
1473 			}
1474 			else
1475 				lpfc_els_flush_rscn(vport);
1476 		}
1477 		lpfc_disc_start(vport);
1478 	}
1479 	lpfc_ct_free_iocb(phba, cmdiocb);
1480 	return;
1481 }
1482 
1483 static void
1484 lpfc_cmpl_ct_cmd_gft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1485 				struct lpfc_iocbq *rspiocb)
1486 {
1487 	struct lpfc_vport *vport = cmdiocb->vport;
1488 	IOCB_t *irsp = &rspiocb->iocb;
1489 	struct lpfc_dmabuf *inp = (struct lpfc_dmabuf *)cmdiocb->context1;
1490 	struct lpfc_dmabuf *outp = (struct lpfc_dmabuf *)cmdiocb->context2;
1491 	struct lpfc_sli_ct_request *CTrsp;
1492 	int did;
1493 	struct lpfc_nodelist *ndlp;
1494 	uint32_t fc4_data_0, fc4_data_1;
1495 
1496 	did = ((struct lpfc_sli_ct_request *)inp->virt)->un.gft.PortId;
1497 	did = be32_to_cpu(did);
1498 
1499 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1500 			      "GFT_ID cmpl: status:x%x/x%x did:x%x",
1501 			      irsp->ulpStatus, irsp->un.ulpWord[4], did);
1502 
1503 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1504 		/* Good status, continue checking */
1505 		CTrsp = (struct lpfc_sli_ct_request *)outp->virt;
1506 		fc4_data_0 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[0]);
1507 		fc4_data_1 = be32_to_cpu(CTrsp->un.gft_acc.fc4_types[1]);
1508 
1509 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1510 				 "6432 Process GFT_ID rsp for %08x "
1511 				 "Data %08x %08x %s %s\n",
1512 				 did, fc4_data_0, fc4_data_1,
1513 				 (fc4_data_0 & LPFC_FC4_TYPE_BITMASK) ?
1514 				  "FCP" : " ",
1515 				 (fc4_data_1 & LPFC_FC4_TYPE_BITMASK) ?
1516 				  "NVME" : " ");
1517 
1518 		ndlp = lpfc_findnode_did(vport, did);
1519 		if (ndlp) {
1520 			/* The bitmask value for FCP and NVME FCP types is
1521 			 * the same because they are 32 bits distant from
1522 			 * each other in word0 and word0.
1523 			 */
1524 			if (fc4_data_0 & LPFC_FC4_TYPE_BITMASK)
1525 				ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1526 			if (fc4_data_1 &  LPFC_FC4_TYPE_BITMASK)
1527 				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1528 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1529 					 "3064 Setting ndlp x%px, DID x%06x "
1530 					 "with FC4 x%08x, Data: x%08x x%08x "
1531 					 "%d\n",
1532 					 ndlp, did, ndlp->nlp_fc4_type,
1533 					 FC_TYPE_FCP, FC_TYPE_NVME,
1534 					 ndlp->nlp_state);
1535 
1536 			if (ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE &&
1537 			    ndlp->nlp_fc4_type) {
1538 				ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1539 
1540 				lpfc_nlp_set_state(vport, ndlp,
1541 						   NLP_STE_PRLI_ISSUE);
1542 				lpfc_issue_els_prli(vport, ndlp, 0);
1543 			} else if (!ndlp->nlp_fc4_type) {
1544 				/* If fc4 type is still unknown, then LOGO */
1545 				lpfc_printf_vlog(vport, KERN_INFO,
1546 						 LOG_DISCOVERY,
1547 						 "6443 Sending LOGO ndlp x%px,"
1548 						 "DID x%06x with fc4_type: "
1549 						 "x%08x, state: %d\n",
1550 						 ndlp, did, ndlp->nlp_fc4_type,
1551 						 ndlp->nlp_state);
1552 				lpfc_issue_els_logo(vport, ndlp, 0);
1553 				ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1554 				lpfc_nlp_set_state(vport, ndlp,
1555 						   NLP_STE_NPR_NODE);
1556 			}
1557 		}
1558 	} else
1559 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1560 				 "3065 GFT_ID failed x%08x\n", irsp->ulpStatus);
1561 
1562 	lpfc_ct_free_iocb(phba, cmdiocb);
1563 }
1564 
1565 static void
1566 lpfc_cmpl_ct(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1567 	     struct lpfc_iocbq *rspiocb)
1568 {
1569 	struct lpfc_vport *vport = cmdiocb->vport;
1570 	struct lpfc_dmabuf *inp;
1571 	struct lpfc_dmabuf *outp;
1572 	IOCB_t *irsp;
1573 	struct lpfc_sli_ct_request *CTrsp;
1574 	struct lpfc_nodelist *ndlp;
1575 	int cmdcode, rc;
1576 	uint8_t retry;
1577 	uint32_t latt;
1578 
1579 	/* First save ndlp, before we overwrite it */
1580 	ndlp = cmdiocb->context_un.ndlp;
1581 
1582 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1583 	cmdiocb->context_un.rsp_iocb = rspiocb;
1584 
1585 	inp = (struct lpfc_dmabuf *) cmdiocb->context1;
1586 	outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1587 	irsp = &rspiocb->iocb;
1588 
1589 	cmdcode = be16_to_cpu(((struct lpfc_sli_ct_request *) inp->virt)->
1590 					CommandResponse.bits.CmdRsp);
1591 	CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1592 
1593 	latt = lpfc_els_chk_latt(vport);
1594 
1595 	/* RFT request completes status <ulpStatus> CmdRsp <CmdRsp> */
1596 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1597 			 "0209 CT Request completes, latt %d, "
1598 			 "ulpStatus x%x CmdRsp x%x, Context x%x, Tag x%x\n",
1599 			 latt, irsp->ulpStatus,
1600 			 CTrsp->CommandResponse.bits.CmdRsp,
1601 			 cmdiocb->iocb.ulpContext, cmdiocb->iocb.ulpIoTag);
1602 
1603 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
1604 		"CT cmd cmpl:     status:x%x/x%x cmd:x%x",
1605 		irsp->ulpStatus, irsp->un.ulpWord[4], cmdcode);
1606 
1607 	if (irsp->ulpStatus) {
1608 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1609 				 "0268 NS cmd x%x Error (x%x x%x)\n",
1610 				 cmdcode, irsp->ulpStatus, irsp->un.ulpWord[4]);
1611 
1612 		if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1613 			(((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1614 			  IOERR_SLI_DOWN) ||
1615 			 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1616 			  IOERR_SLI_ABORTED)))
1617 			goto out;
1618 
1619 		retry = cmdiocb->retry;
1620 		if (retry >= LPFC_MAX_NS_RETRY)
1621 			goto out;
1622 
1623 		retry++;
1624 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1625 				 "0250 Retrying NS cmd %x\n", cmdcode);
1626 		rc = lpfc_ns_cmd(vport, cmdcode, retry, 0);
1627 		if (rc == 0)
1628 			goto out;
1629 	}
1630 
1631 out:
1632 	cmdiocb->context_un.ndlp = ndlp; /* Now restore ndlp for free */
1633 	lpfc_ct_free_iocb(phba, cmdiocb);
1634 	return;
1635 }
1636 
1637 static void
1638 lpfc_cmpl_ct_cmd_rft_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1639 			struct lpfc_iocbq *rspiocb)
1640 {
1641 	IOCB_t *irsp = &rspiocb->iocb;
1642 	struct lpfc_vport *vport = cmdiocb->vport;
1643 
1644 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1645 		struct lpfc_dmabuf *outp;
1646 		struct lpfc_sli_ct_request *CTrsp;
1647 
1648 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1649 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1650 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1651 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1652 			vport->ct_flags |= FC_CT_RFT_ID;
1653 	}
1654 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1655 	return;
1656 }
1657 
1658 static void
1659 lpfc_cmpl_ct_cmd_rnn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1660 			struct lpfc_iocbq *rspiocb)
1661 {
1662 	IOCB_t *irsp = &rspiocb->iocb;
1663 	struct lpfc_vport *vport = cmdiocb->vport;
1664 
1665 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1666 		struct lpfc_dmabuf *outp;
1667 		struct lpfc_sli_ct_request *CTrsp;
1668 
1669 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1670 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1671 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1672 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1673 			vport->ct_flags |= FC_CT_RNN_ID;
1674 	}
1675 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1676 	return;
1677 }
1678 
1679 static void
1680 lpfc_cmpl_ct_cmd_rspn_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1681 			 struct lpfc_iocbq *rspiocb)
1682 {
1683 	IOCB_t *irsp = &rspiocb->iocb;
1684 	struct lpfc_vport *vport = cmdiocb->vport;
1685 
1686 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1687 		struct lpfc_dmabuf *outp;
1688 		struct lpfc_sli_ct_request *CTrsp;
1689 
1690 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1691 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1692 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1693 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1694 			vport->ct_flags |= FC_CT_RSPN_ID;
1695 	}
1696 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1697 	return;
1698 }
1699 
1700 static void
1701 lpfc_cmpl_ct_cmd_rsnn_nn(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1702 			 struct lpfc_iocbq *rspiocb)
1703 {
1704 	IOCB_t *irsp = &rspiocb->iocb;
1705 	struct lpfc_vport *vport = cmdiocb->vport;
1706 
1707 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1708 		struct lpfc_dmabuf *outp;
1709 		struct lpfc_sli_ct_request *CTrsp;
1710 
1711 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1712 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1713 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1714 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1715 			vport->ct_flags |= FC_CT_RSNN_NN;
1716 	}
1717 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1718 	return;
1719 }
1720 
1721 static void
1722 lpfc_cmpl_ct_cmd_da_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1723  struct lpfc_iocbq *rspiocb)
1724 {
1725 	struct lpfc_vport *vport = cmdiocb->vport;
1726 
1727 	/* even if it fails we will act as though it succeeded. */
1728 	vport->ct_flags = 0;
1729 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1730 	return;
1731 }
1732 
1733 static void
1734 lpfc_cmpl_ct_cmd_rff_id(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1735 			struct lpfc_iocbq *rspiocb)
1736 {
1737 	IOCB_t *irsp = &rspiocb->iocb;
1738 	struct lpfc_vport *vport = cmdiocb->vport;
1739 
1740 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
1741 		struct lpfc_dmabuf *outp;
1742 		struct lpfc_sli_ct_request *CTrsp;
1743 
1744 		outp = (struct lpfc_dmabuf *) cmdiocb->context2;
1745 		CTrsp = (struct lpfc_sli_ct_request *) outp->virt;
1746 		if (CTrsp->CommandResponse.bits.CmdRsp ==
1747 		    be16_to_cpu(SLI_CT_RESPONSE_FS_ACC))
1748 			vport->ct_flags |= FC_CT_RFF_ID;
1749 	}
1750 	lpfc_cmpl_ct(phba, cmdiocb, rspiocb);
1751 	return;
1752 }
1753 
1754 /*
1755  * Although the symbolic port name is thought to be an integer
1756  * as of January 18, 2016, leave it as a string until more of
1757  * the record state becomes defined.
1758  */
1759 int
1760 lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
1761 	size_t size)
1762 {
1763 	int n;
1764 
1765 	/*
1766 	 * Use the lpfc board number as the Symbolic Port
1767 	 * Name object.  NPIV is not in play so this integer
1768 	 * value is sufficient and unique per FC-ID.
1769 	 */
1770 	n = scnprintf(symbol, size, "%d", vport->phba->brd_no);
1771 	return n;
1772 }
1773 
1774 
1775 int
1776 lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
1777 	size_t size)
1778 {
1779 	char fwrev[FW_REV_STR_SIZE] = {0};
1780 	char tmp[MAXHOSTNAMELEN] = {0};
1781 
1782 	memset(symbol, 0, size);
1783 
1784 	scnprintf(tmp, sizeof(tmp), "Emulex %s", vport->phba->ModelName);
1785 	if (strlcat(symbol, tmp, size) >= size)
1786 		goto buffer_done;
1787 
1788 	lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
1789 	scnprintf(tmp, sizeof(tmp), " FV%s", fwrev);
1790 	if (strlcat(symbol, tmp, size) >= size)
1791 		goto buffer_done;
1792 
1793 	scnprintf(tmp, sizeof(tmp), " DV%s", lpfc_release_version);
1794 	if (strlcat(symbol, tmp, size) >= size)
1795 		goto buffer_done;
1796 
1797 	scnprintf(tmp, sizeof(tmp), " HN:%s", vport->phba->os_host_name);
1798 	if (strlcat(symbol, tmp, size) >= size)
1799 		goto buffer_done;
1800 
1801 	/* Note :- OS name is "Linux" */
1802 	scnprintf(tmp, sizeof(tmp), " OS:%s", init_utsname()->sysname);
1803 	strlcat(symbol, tmp, size);
1804 
1805 buffer_done:
1806 	return strnlen(symbol, size);
1807 
1808 }
1809 
1810 static uint32_t
1811 lpfc_find_map_node(struct lpfc_vport *vport)
1812 {
1813 	struct lpfc_nodelist *ndlp, *next_ndlp;
1814 	struct Scsi_Host  *shost;
1815 	uint32_t cnt = 0;
1816 
1817 	shost = lpfc_shost_from_vport(vport);
1818 	spin_lock_irq(shost->host_lock);
1819 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1820 		if (ndlp->nlp_type & NLP_FABRIC)
1821 			continue;
1822 		if ((ndlp->nlp_state == NLP_STE_MAPPED_NODE) ||
1823 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE))
1824 			cnt++;
1825 	}
1826 	spin_unlock_irq(shost->host_lock);
1827 	return cnt;
1828 }
1829 
1830 /*
1831  * This routine will return the FC4 Type associated with the CT
1832  * GID_FT command.
1833  */
1834 int
1835 lpfc_get_gidft_type(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb)
1836 {
1837 	struct lpfc_sli_ct_request *CtReq;
1838 	struct lpfc_dmabuf *mp;
1839 	uint32_t type;
1840 
1841 	mp = cmdiocb->context1;
1842 	if (mp == NULL)
1843 		return 0;
1844 	CtReq = (struct lpfc_sli_ct_request *)mp->virt;
1845 	type = (uint32_t)CtReq->un.gid.Fc4Type;
1846 	if ((type != SLI_CTPT_FCP) && (type != SLI_CTPT_NVME))
1847 		return 0;
1848 	return type;
1849 }
1850 
1851 /*
1852  * lpfc_ns_cmd
1853  * Description:
1854  *    Issue Cmd to NameServer
1855  *       SLI_CTNS_GID_FT
1856  *       LI_CTNS_RFT_ID
1857  */
1858 int
1859 lpfc_ns_cmd(struct lpfc_vport *vport, int cmdcode,
1860 	    uint8_t retry, uint32_t context)
1861 {
1862 	struct lpfc_nodelist * ndlp;
1863 	struct lpfc_hba *phba = vport->phba;
1864 	struct lpfc_dmabuf *mp, *bmp;
1865 	struct lpfc_sli_ct_request *CtReq;
1866 	struct ulp_bde64 *bpl;
1867 	void (*cmpl) (struct lpfc_hba *, struct lpfc_iocbq *,
1868 		      struct lpfc_iocbq *) = NULL;
1869 	uint32_t *ptr;
1870 	uint32_t rsp_size = 1024;
1871 	size_t   size;
1872 	int rc = 0;
1873 
1874 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
1875 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)
1876 	    || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) {
1877 		rc=1;
1878 		goto ns_cmd_exit;
1879 	}
1880 
1881 	/* fill in BDEs for command */
1882 	/* Allocate buffer for command payload */
1883 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1884 	if (!mp) {
1885 		rc=2;
1886 		goto ns_cmd_exit;
1887 	}
1888 
1889 	INIT_LIST_HEAD(&mp->list);
1890 	mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
1891 	if (!mp->virt) {
1892 		rc=3;
1893 		goto ns_cmd_free_mp;
1894 	}
1895 
1896 	/* Allocate buffer for Buffer ptr list */
1897 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1898 	if (!bmp) {
1899 		rc=4;
1900 		goto ns_cmd_free_mpvirt;
1901 	}
1902 
1903 	INIT_LIST_HEAD(&bmp->list);
1904 	bmp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(bmp->phys));
1905 	if (!bmp->virt) {
1906 		rc=5;
1907 		goto ns_cmd_free_bmp;
1908 	}
1909 
1910 	/* NameServer Req */
1911 	lpfc_printf_vlog(vport, KERN_INFO ,LOG_DISCOVERY,
1912 			 "0236 NameServer Req Data: x%x x%x x%x x%x\n",
1913 			 cmdcode, vport->fc_flag, vport->fc_rscn_id_cnt,
1914 			 context);
1915 
1916 	bpl = (struct ulp_bde64 *) bmp->virt;
1917 	memset(bpl, 0, sizeof(struct ulp_bde64));
1918 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys) );
1919 	bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys) );
1920 	bpl->tus.f.bdeFlags = 0;
1921 	if (cmdcode == SLI_CTNS_GID_FT)
1922 		bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1923 	else if (cmdcode == SLI_CTNS_GID_PT)
1924 		bpl->tus.f.bdeSize = GID_REQUEST_SZ;
1925 	else if (cmdcode == SLI_CTNS_GFF_ID)
1926 		bpl->tus.f.bdeSize = GFF_REQUEST_SZ;
1927 	else if (cmdcode == SLI_CTNS_GFT_ID)
1928 		bpl->tus.f.bdeSize = GFT_REQUEST_SZ;
1929 	else if (cmdcode == SLI_CTNS_RFT_ID)
1930 		bpl->tus.f.bdeSize = RFT_REQUEST_SZ;
1931 	else if (cmdcode == SLI_CTNS_RNN_ID)
1932 		bpl->tus.f.bdeSize = RNN_REQUEST_SZ;
1933 	else if (cmdcode == SLI_CTNS_RSPN_ID)
1934 		bpl->tus.f.bdeSize = RSPN_REQUEST_SZ;
1935 	else if (cmdcode == SLI_CTNS_RSNN_NN)
1936 		bpl->tus.f.bdeSize = RSNN_REQUEST_SZ;
1937 	else if (cmdcode == SLI_CTNS_DA_ID)
1938 		bpl->tus.f.bdeSize = DA_ID_REQUEST_SZ;
1939 	else if (cmdcode == SLI_CTNS_RFF_ID)
1940 		bpl->tus.f.bdeSize = RFF_REQUEST_SZ;
1941 	else
1942 		bpl->tus.f.bdeSize = 0;
1943 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
1944 
1945 	CtReq = (struct lpfc_sli_ct_request *) mp->virt;
1946 	memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
1947 	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
1948 	CtReq->RevisionId.bits.InId = 0;
1949 	CtReq->FsType = SLI_CT_DIRECTORY_SERVICE;
1950 	CtReq->FsSubType = SLI_CT_DIRECTORY_NAME_SERVER;
1951 	CtReq->CommandResponse.bits.Size = 0;
1952 	switch (cmdcode) {
1953 	case SLI_CTNS_GID_FT:
1954 		CtReq->CommandResponse.bits.CmdRsp =
1955 		    cpu_to_be16(SLI_CTNS_GID_FT);
1956 		CtReq->un.gid.Fc4Type = context;
1957 
1958 		if (vport->port_state < LPFC_NS_QRY)
1959 			vport->port_state = LPFC_NS_QRY;
1960 		lpfc_set_disctmo(vport);
1961 		cmpl = lpfc_cmpl_ct_cmd_gid_ft;
1962 		rsp_size = FC_MAX_NS_RSP;
1963 		break;
1964 
1965 	case SLI_CTNS_GID_PT:
1966 		CtReq->CommandResponse.bits.CmdRsp =
1967 		    cpu_to_be16(SLI_CTNS_GID_PT);
1968 		CtReq->un.gid.PortType = context;
1969 
1970 		if (vport->port_state < LPFC_NS_QRY)
1971 			vport->port_state = LPFC_NS_QRY;
1972 		lpfc_set_disctmo(vport);
1973 		cmpl = lpfc_cmpl_ct_cmd_gid_pt;
1974 		rsp_size = FC_MAX_NS_RSP;
1975 		break;
1976 
1977 	case SLI_CTNS_GFF_ID:
1978 		CtReq->CommandResponse.bits.CmdRsp =
1979 			cpu_to_be16(SLI_CTNS_GFF_ID);
1980 		CtReq->un.gff.PortId = cpu_to_be32(context);
1981 		cmpl = lpfc_cmpl_ct_cmd_gff_id;
1982 		break;
1983 
1984 	case SLI_CTNS_GFT_ID:
1985 		CtReq->CommandResponse.bits.CmdRsp =
1986 			cpu_to_be16(SLI_CTNS_GFT_ID);
1987 		CtReq->un.gft.PortId = cpu_to_be32(context);
1988 		cmpl = lpfc_cmpl_ct_cmd_gft_id;
1989 		break;
1990 
1991 	case SLI_CTNS_RFT_ID:
1992 		vport->ct_flags &= ~FC_CT_RFT_ID;
1993 		CtReq->CommandResponse.bits.CmdRsp =
1994 		    cpu_to_be16(SLI_CTNS_RFT_ID);
1995 		CtReq->un.rft.PortId = cpu_to_be32(vport->fc_myDID);
1996 
1997 		/* Register FC4 FCP type if enabled.  */
1998 		if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
1999 		    vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)
2000 			CtReq->un.rft.fcpReg = 1;
2001 
2002 		/* Register NVME type if enabled.  Defined LE and swapped.
2003 		 * rsvd[0] is used as word1 because of the hard-coded
2004 		 * word0 usage in the ct_request data structure.
2005 		 */
2006 		if (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
2007 		    vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
2008 			CtReq->un.rft.rsvd[0] =
2009 				cpu_to_be32(LPFC_FC4_TYPE_BITMASK);
2010 
2011 		ptr = (uint32_t *)CtReq;
2012 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2013 				 "6433 Issue RFT (%s %s): %08x %08x %08x %08x "
2014 				 "%08x %08x %08x %08x\n",
2015 				 CtReq->un.rft.fcpReg ? "FCP" : " ",
2016 				 CtReq->un.rft.rsvd[0] ? "NVME" : " ",
2017 				 *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
2018 				 *(ptr + 4), *(ptr + 5),
2019 				 *(ptr + 6), *(ptr + 7));
2020 		cmpl = lpfc_cmpl_ct_cmd_rft_id;
2021 		break;
2022 
2023 	case SLI_CTNS_RNN_ID:
2024 		vport->ct_flags &= ~FC_CT_RNN_ID;
2025 		CtReq->CommandResponse.bits.CmdRsp =
2026 		    cpu_to_be16(SLI_CTNS_RNN_ID);
2027 		CtReq->un.rnn.PortId = cpu_to_be32(vport->fc_myDID);
2028 		memcpy(CtReq->un.rnn.wwnn,  &vport->fc_nodename,
2029 		       sizeof(struct lpfc_name));
2030 		cmpl = lpfc_cmpl_ct_cmd_rnn_id;
2031 		break;
2032 
2033 	case SLI_CTNS_RSPN_ID:
2034 		vport->ct_flags &= ~FC_CT_RSPN_ID;
2035 		CtReq->CommandResponse.bits.CmdRsp =
2036 		    cpu_to_be16(SLI_CTNS_RSPN_ID);
2037 		CtReq->un.rspn.PortId = cpu_to_be32(vport->fc_myDID);
2038 		size = sizeof(CtReq->un.rspn.symbname);
2039 		CtReq->un.rspn.len =
2040 			lpfc_vport_symbolic_port_name(vport,
2041 			CtReq->un.rspn.symbname, size);
2042 		cmpl = lpfc_cmpl_ct_cmd_rspn_id;
2043 		break;
2044 	case SLI_CTNS_RSNN_NN:
2045 		vport->ct_flags &= ~FC_CT_RSNN_NN;
2046 		CtReq->CommandResponse.bits.CmdRsp =
2047 		    cpu_to_be16(SLI_CTNS_RSNN_NN);
2048 		memcpy(CtReq->un.rsnn.wwnn, &vport->fc_nodename,
2049 		       sizeof(struct lpfc_name));
2050 		size = sizeof(CtReq->un.rsnn.symbname);
2051 		CtReq->un.rsnn.len =
2052 			lpfc_vport_symbolic_node_name(vport,
2053 			CtReq->un.rsnn.symbname, size);
2054 		cmpl = lpfc_cmpl_ct_cmd_rsnn_nn;
2055 		break;
2056 	case SLI_CTNS_DA_ID:
2057 		/* Implement DA_ID Nameserver request */
2058 		CtReq->CommandResponse.bits.CmdRsp =
2059 			cpu_to_be16(SLI_CTNS_DA_ID);
2060 		CtReq->un.da_id.port_id = cpu_to_be32(vport->fc_myDID);
2061 		cmpl = lpfc_cmpl_ct_cmd_da_id;
2062 		break;
2063 	case SLI_CTNS_RFF_ID:
2064 		vport->ct_flags &= ~FC_CT_RFF_ID;
2065 		CtReq->CommandResponse.bits.CmdRsp =
2066 		    cpu_to_be16(SLI_CTNS_RFF_ID);
2067 		CtReq->un.rff.PortId = cpu_to_be32(vport->fc_myDID);
2068 		CtReq->un.rff.fbits = FC4_FEATURE_INIT;
2069 
2070 		/* The driver always supports FC_TYPE_FCP.  However, the
2071 		 * caller can specify NVME (type x28) as well.  But only
2072 		 * these that FC4 type is supported.
2073 		 */
2074 		if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2075 		     (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
2076 		    (context == FC_TYPE_NVME)) {
2077 			if ((vport == phba->pport) && phba->nvmet_support) {
2078 				CtReq->un.rff.fbits = (FC4_FEATURE_TARGET |
2079 					FC4_FEATURE_NVME_DISC);
2080 				lpfc_nvmet_update_targetport(phba);
2081 			} else {
2082 				lpfc_nvme_update_localport(vport);
2083 			}
2084 			CtReq->un.rff.type_code = context;
2085 
2086 		} else if (((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2087 			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) &&
2088 			   (context == FC_TYPE_FCP))
2089 			CtReq->un.rff.type_code = context;
2090 
2091 		else
2092 			goto ns_cmd_free_bmpvirt;
2093 
2094 		ptr = (uint32_t *)CtReq;
2095 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2096 				 "6434 Issue RFF (%s): %08x %08x %08x %08x "
2097 				 "%08x %08x %08x %08x\n",
2098 				 (context == FC_TYPE_NVME) ? "NVME" : "FCP",
2099 				 *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
2100 				 *(ptr + 4), *(ptr + 5),
2101 				 *(ptr + 6), *(ptr + 7));
2102 		cmpl = lpfc_cmpl_ct_cmd_rff_id;
2103 		break;
2104 	}
2105 	/* The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
2106 	 * to hold ndlp reference for the corresponding callback function.
2107 	 */
2108 	if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, retry)) {
2109 		/* On success, The cmpl function will free the buffers */
2110 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
2111 			"Issue CT cmd:    cmd:x%x did:x%x",
2112 			cmdcode, ndlp->nlp_DID, 0);
2113 		return 0;
2114 	}
2115 	rc=6;
2116 
2117 	/* Decrement ndlp reference count to release ndlp reference held
2118 	 * for the failed command's callback function.
2119 	 */
2120 	lpfc_nlp_put(ndlp);
2121 
2122 ns_cmd_free_bmpvirt:
2123 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
2124 ns_cmd_free_bmp:
2125 	kfree(bmp);
2126 ns_cmd_free_mpvirt:
2127 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2128 ns_cmd_free_mp:
2129 	kfree(mp);
2130 ns_cmd_exit:
2131 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2132 			 "0266 Issue NameServer Req x%x err %d Data: x%x x%x\n",
2133 			 cmdcode, rc, vport->fc_flag, vport->fc_rscn_id_cnt);
2134 	return 1;
2135 }
2136 
2137 /**
2138  * lpfc_cmpl_ct_disc_fdmi - Handle a discovery FDMI completion
2139  * @phba: Pointer to HBA context object.
2140  * @cmdiocb: Pointer to the command IOCBQ.
2141  * @rspiocb: Pointer to the response IOCBQ.
2142  *
2143  * This function to handle the completion of a driver initiated FDMI
2144  * CT command issued during discovery.
2145  */
2146 static void
2147 lpfc_cmpl_ct_disc_fdmi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2148 		       struct lpfc_iocbq *rspiocb)
2149 {
2150 	struct lpfc_vport *vport = cmdiocb->vport;
2151 	struct lpfc_dmabuf *inp = cmdiocb->context1;
2152 	struct lpfc_dmabuf *outp = cmdiocb->context2;
2153 	struct lpfc_sli_ct_request *CTcmd = inp->virt;
2154 	struct lpfc_sli_ct_request *CTrsp = outp->virt;
2155 	uint16_t fdmi_cmd = CTcmd->CommandResponse.bits.CmdRsp;
2156 	uint16_t fdmi_rsp = CTrsp->CommandResponse.bits.CmdRsp;
2157 	IOCB_t *irsp = &rspiocb->iocb;
2158 	struct lpfc_nodelist *ndlp;
2159 	uint32_t latt, cmd, err;
2160 
2161 	latt = lpfc_els_chk_latt(vport);
2162 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_CT,
2163 		"FDMI cmpl:       status:x%x/x%x latt:%d",
2164 		irsp->ulpStatus, irsp->un.ulpWord[4], latt);
2165 
2166 	if (latt || irsp->ulpStatus) {
2167 
2168 		/* Look for a retryable error */
2169 		if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
2170 			switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
2171 			case IOERR_SLI_ABORTED:
2172 			case IOERR_SLI_DOWN:
2173 				/* Driver aborted this IO.  No retry as error
2174 				 * is likely Offline->Online or some adapter
2175 				 * error.  Recovery will try again.
2176 				 */
2177 				break;
2178 			case IOERR_ABORT_IN_PROGRESS:
2179 			case IOERR_SEQUENCE_TIMEOUT:
2180 			case IOERR_ILLEGAL_FRAME:
2181 			case IOERR_NO_RESOURCES:
2182 			case IOERR_ILLEGAL_COMMAND:
2183 				cmdiocb->retry++;
2184 				if (cmdiocb->retry >= LPFC_FDMI_MAX_RETRY)
2185 					break;
2186 
2187 				/* Retry the same FDMI command */
2188 				err = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING,
2189 							  cmdiocb, 0);
2190 				if (err == IOCB_ERROR)
2191 					break;
2192 				return;
2193 			default:
2194 				break;
2195 			}
2196 		}
2197 
2198 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2199 				 "0229 FDMI cmd %04x failed, latt = %d "
2200 				 "ulpStatus: x%x, rid x%x\n",
2201 				 be16_to_cpu(fdmi_cmd), latt, irsp->ulpStatus,
2202 				 irsp->un.ulpWord[4]);
2203 	}
2204 	lpfc_ct_free_iocb(phba, cmdiocb);
2205 
2206 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
2207 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2208 		return;
2209 
2210 	/* Check for a CT LS_RJT response */
2211 	cmd =  be16_to_cpu(fdmi_cmd);
2212 	if (fdmi_rsp == cpu_to_be16(SLI_CT_RESPONSE_FS_RJT)) {
2213 		/* FDMI rsp failed */
2214 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2215 				 "0220 FDMI cmd failed FS_RJT Data: x%x", cmd);
2216 
2217 		/* Should we fallback to FDMI-2 / FDMI-1 ? */
2218 		switch (cmd) {
2219 		case SLI_MGMT_RHBA:
2220 			if (vport->fdmi_hba_mask == LPFC_FDMI2_HBA_ATTR) {
2221 				/* Fallback to FDMI-1 */
2222 				vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
2223 				vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2224 				/* Start over */
2225 				lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2226 			}
2227 			return;
2228 
2229 		case SLI_MGMT_RPRT:
2230 			if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
2231 				/* Fallback to FDMI-1 */
2232 				vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2233 				/* Start over */
2234 				lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2235 				return;
2236 			}
2237 			if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
2238 				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
2239 				/* Retry the same command */
2240 				lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2241 			}
2242 			return;
2243 
2244 		case SLI_MGMT_RPA:
2245 			/* No retry on Vendor RPA */
2246 			if (phba->link_flag & LS_CT_VEN_RPA) {
2247 				lpfc_printf_vlog(vport, KERN_ERR,
2248 						 LOG_DISCOVERY | LOG_ELS,
2249 						 "6460 VEN FDMI RPA failure\n");
2250 				phba->link_flag &= ~LS_CT_VEN_RPA;
2251 				return;
2252 			}
2253 			if (vport->fdmi_port_mask == LPFC_FDMI2_PORT_ATTR) {
2254 				/* Fallback to FDMI-1 */
2255 				vport->fdmi_hba_mask = LPFC_FDMI1_HBA_ATTR;
2256 				vport->fdmi_port_mask = LPFC_FDMI1_PORT_ATTR;
2257 				/* Start over */
2258 				lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2259 				return;
2260 			}
2261 			if (vport->fdmi_port_mask == LPFC_FDMI2_SMART_ATTR) {
2262 				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
2263 				/* Retry the same command */
2264 				lpfc_fdmi_cmd(vport, ndlp, cmd, 0);
2265 			}
2266 			return;
2267 		}
2268 	}
2269 
2270 	/*
2271 	 * On success, need to cycle thru FDMI registration for discovery
2272 	 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2273 	 * DPRT -> RPRT (vports)
2274 	 */
2275 	switch (cmd) {
2276 	case SLI_MGMT_RHBA:
2277 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA, 0);
2278 		break;
2279 
2280 	case SLI_MGMT_DHBA:
2281 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2282 		break;
2283 
2284 	case SLI_MGMT_DPRT:
2285 		if (vport->port_type == LPFC_PHYSICAL_PORT)
2286 			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RHBA, 0);
2287 		else
2288 			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT, 0);
2289 		break;
2290 	case SLI_MGMT_RPA:
2291 		if (vport->port_type == LPFC_PHYSICAL_PORT &&
2292 		    phba->cfg_enable_mi &&
2293 		    phba->sli4_hba.pc_sli4_params.mi_ver > LPFC_MIB1_SUPPORT) {
2294 			/* mi is only for the phyical port, no vports */
2295 			if (phba->link_flag & LS_CT_VEN_RPA) {
2296 				lpfc_printf_vlog(vport, KERN_INFO,
2297 						 LOG_DISCOVERY | LOG_ELS,
2298 						 "6449 VEN RPA Success\n");
2299 				break;
2300 			}
2301 
2302 			if (lpfc_fdmi_cmd(vport, ndlp, cmd,
2303 					  LPFC_FDMI_VENDOR_ATTR_mi) == 0)
2304 				phba->link_flag |= LS_CT_VEN_RPA;
2305 			lpfc_printf_vlog(vport, KERN_INFO,
2306 					LOG_DISCOVERY | LOG_ELS,
2307 					"6458 Send MI FDMI:%x Flag x%x\n",
2308 					phba->sli4_hba.pc_sli4_params.mi_value,
2309 					phba->link_flag);
2310 		} else {
2311 			lpfc_printf_vlog(vport, KERN_INFO,
2312 					 LOG_DISCOVERY | LOG_ELS,
2313 					 "6459 No FDMI VEN MI support - "
2314 					 "RPA Success\n");
2315 		}
2316 		break;
2317 	}
2318 	return;
2319 }
2320 
2321 
2322 /**
2323  * lpfc_fdmi_change_check - Check for changed FDMI parameters
2324  * @vport: pointer to a host virtual N_Port data structure.
2325  *
2326  * Check how many mapped NPorts we are connected to
2327  * Check if our hostname changed
2328  * Called from hbeat timeout routine to check if any FDMI parameters
2329  * changed. If so, re-register those Attributes.
2330  */
2331 void
2332 lpfc_fdmi_change_check(struct lpfc_vport *vport)
2333 {
2334 	struct lpfc_hba *phba = vport->phba;
2335 	struct lpfc_nodelist *ndlp;
2336 	uint16_t cnt;
2337 
2338 	if (!lpfc_is_link_up(phba))
2339 		return;
2340 
2341 	/* Must be connected to a Fabric */
2342 	if (!(vport->fc_flag & FC_FABRIC))
2343 		return;
2344 
2345 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
2346 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2347 		return;
2348 
2349 	/* Check if system hostname changed */
2350 	if (strcmp(phba->os_host_name, init_utsname()->nodename)) {
2351 		memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
2352 		scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
2353 			  init_utsname()->nodename);
2354 		lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2355 
2356 		/* Since this effects multiple HBA and PORT attributes, we need
2357 		 * de-register and go thru the whole FDMI registration cycle.
2358 		 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
2359 		 * DPRT -> RPRT (vports)
2360 		 */
2361 		if (vport->port_type == LPFC_PHYSICAL_PORT)
2362 			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
2363 		else
2364 			lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
2365 
2366 		/* Since this code path registers all the port attributes
2367 		 * we can just return without further checking.
2368 		 */
2369 		return;
2370 	}
2371 
2372 	if (!(vport->fdmi_port_mask & LPFC_FDMI_PORT_ATTR_num_disc))
2373 		return;
2374 
2375 	/* Check if the number of mapped NPorts changed */
2376 	cnt = lpfc_find_map_node(vport);
2377 	if (cnt == vport->fdmi_num_disc)
2378 		return;
2379 
2380 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
2381 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPA,
2382 			      LPFC_FDMI_PORT_ATTR_num_disc);
2383 	} else {
2384 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_RPRT,
2385 			      LPFC_FDMI_PORT_ATTR_num_disc);
2386 	}
2387 }
2388 
2389 /* Routines for all individual HBA attributes */
2390 static int
2391 lpfc_fdmi_hba_attr_wwnn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2392 {
2393 	struct lpfc_fdmi_attr_entry *ae;
2394 	uint32_t size;
2395 
2396 	ae = &ad->AttrValue;
2397 	memset(ae, 0, sizeof(*ae));
2398 
2399 	memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2400 	       sizeof(struct lpfc_name));
2401 	size = FOURBYTES + sizeof(struct lpfc_name);
2402 	ad->AttrLen = cpu_to_be16(size);
2403 	ad->AttrType = cpu_to_be16(RHBA_NODENAME);
2404 	return size;
2405 }
2406 static int
2407 lpfc_fdmi_hba_attr_manufacturer(struct lpfc_vport *vport,
2408 				struct lpfc_fdmi_attr_def *ad)
2409 {
2410 	struct lpfc_fdmi_attr_entry *ae;
2411 	uint32_t len, size;
2412 
2413 	ae = &ad->AttrValue;
2414 	memset(ae, 0, sizeof(*ae));
2415 
2416 	/* This string MUST be consistent with other FC platforms
2417 	 * supported by Broadcom.
2418 	 */
2419 	strncpy(ae->un.AttrString,
2420 		"Emulex Corporation",
2421 		       sizeof(ae->un.AttrString));
2422 	len = strnlen(ae->un.AttrString,
2423 			  sizeof(ae->un.AttrString));
2424 	len += (len & 3) ? (4 - (len & 3)) : 4;
2425 	size = FOURBYTES + len;
2426 	ad->AttrLen = cpu_to_be16(size);
2427 	ad->AttrType = cpu_to_be16(RHBA_MANUFACTURER);
2428 	return size;
2429 }
2430 
2431 static int
2432 lpfc_fdmi_hba_attr_sn(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad)
2433 {
2434 	struct lpfc_hba *phba = vport->phba;
2435 	struct lpfc_fdmi_attr_entry *ae;
2436 	uint32_t len, size;
2437 
2438 	ae = &ad->AttrValue;
2439 	memset(ae, 0, sizeof(*ae));
2440 
2441 	strncpy(ae->un.AttrString, phba->SerialNumber,
2442 		sizeof(ae->un.AttrString));
2443 	len = strnlen(ae->un.AttrString,
2444 			  sizeof(ae->un.AttrString));
2445 	len += (len & 3) ? (4 - (len & 3)) : 4;
2446 	size = FOURBYTES + len;
2447 	ad->AttrLen = cpu_to_be16(size);
2448 	ad->AttrType = cpu_to_be16(RHBA_SERIAL_NUMBER);
2449 	return size;
2450 }
2451 
2452 static int
2453 lpfc_fdmi_hba_attr_model(struct lpfc_vport *vport,
2454 			 struct lpfc_fdmi_attr_def *ad)
2455 {
2456 	struct lpfc_hba *phba = vport->phba;
2457 	struct lpfc_fdmi_attr_entry *ae;
2458 	uint32_t len, size;
2459 
2460 	ae = &ad->AttrValue;
2461 	memset(ae, 0, sizeof(*ae));
2462 
2463 	strncpy(ae->un.AttrString, phba->ModelName,
2464 		sizeof(ae->un.AttrString));
2465 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2466 	len += (len & 3) ? (4 - (len & 3)) : 4;
2467 	size = FOURBYTES + len;
2468 	ad->AttrLen = cpu_to_be16(size);
2469 	ad->AttrType = cpu_to_be16(RHBA_MODEL);
2470 	return size;
2471 }
2472 
2473 static int
2474 lpfc_fdmi_hba_attr_description(struct lpfc_vport *vport,
2475 			       struct lpfc_fdmi_attr_def *ad)
2476 {
2477 	struct lpfc_hba *phba = vport->phba;
2478 	struct lpfc_fdmi_attr_entry *ae;
2479 	uint32_t len, size;
2480 
2481 	ae = &ad->AttrValue;
2482 	memset(ae, 0, sizeof(*ae));
2483 
2484 	strncpy(ae->un.AttrString, phba->ModelDesc,
2485 		sizeof(ae->un.AttrString));
2486 	len = strnlen(ae->un.AttrString,
2487 				  sizeof(ae->un.AttrString));
2488 	len += (len & 3) ? (4 - (len & 3)) : 4;
2489 	size = FOURBYTES + len;
2490 	ad->AttrLen = cpu_to_be16(size);
2491 	ad->AttrType = cpu_to_be16(RHBA_MODEL_DESCRIPTION);
2492 	return size;
2493 }
2494 
2495 static int
2496 lpfc_fdmi_hba_attr_hdw_ver(struct lpfc_vport *vport,
2497 			   struct lpfc_fdmi_attr_def *ad)
2498 {
2499 	struct lpfc_hba *phba = vport->phba;
2500 	lpfc_vpd_t *vp = &phba->vpd;
2501 	struct lpfc_fdmi_attr_entry *ae;
2502 	uint32_t i, j, incr, size;
2503 
2504 	ae = &ad->AttrValue;
2505 	memset(ae, 0, sizeof(*ae));
2506 
2507 	/* Convert JEDEC ID to ascii for hardware version */
2508 	incr = vp->rev.biuRev;
2509 	for (i = 0; i < 8; i++) {
2510 		j = (incr & 0xf);
2511 		if (j <= 9)
2512 			ae->un.AttrString[7 - i] =
2513 			    (char)((uint8_t) 0x30 +
2514 				   (uint8_t) j);
2515 		else
2516 			ae->un.AttrString[7 - i] =
2517 			    (char)((uint8_t) 0x61 +
2518 				   (uint8_t) (j - 10));
2519 		incr = (incr >> 4);
2520 	}
2521 	size = FOURBYTES + 8;
2522 	ad->AttrLen = cpu_to_be16(size);
2523 	ad->AttrType = cpu_to_be16(RHBA_HARDWARE_VERSION);
2524 	return size;
2525 }
2526 
2527 static int
2528 lpfc_fdmi_hba_attr_drvr_ver(struct lpfc_vport *vport,
2529 			    struct lpfc_fdmi_attr_def *ad)
2530 {
2531 	struct lpfc_fdmi_attr_entry *ae;
2532 	uint32_t len, size;
2533 
2534 	ae = &ad->AttrValue;
2535 	memset(ae, 0, sizeof(*ae));
2536 
2537 	strncpy(ae->un.AttrString, lpfc_release_version,
2538 		sizeof(ae->un.AttrString));
2539 	len = strnlen(ae->un.AttrString,
2540 			  sizeof(ae->un.AttrString));
2541 	len += (len & 3) ? (4 - (len & 3)) : 4;
2542 	size = FOURBYTES + len;
2543 	ad->AttrLen = cpu_to_be16(size);
2544 	ad->AttrType = cpu_to_be16(RHBA_DRIVER_VERSION);
2545 	return size;
2546 }
2547 
2548 static int
2549 lpfc_fdmi_hba_attr_rom_ver(struct lpfc_vport *vport,
2550 			   struct lpfc_fdmi_attr_def *ad)
2551 {
2552 	struct lpfc_hba *phba = vport->phba;
2553 	struct lpfc_fdmi_attr_entry *ae;
2554 	uint32_t len, size;
2555 
2556 	ae = &ad->AttrValue;
2557 	memset(ae, 0, sizeof(*ae));
2558 
2559 	if (phba->sli_rev == LPFC_SLI_REV4)
2560 		lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2561 	else
2562 		strncpy(ae->un.AttrString, phba->OptionROMVersion,
2563 			sizeof(ae->un.AttrString));
2564 	len = strnlen(ae->un.AttrString,
2565 			  sizeof(ae->un.AttrString));
2566 	len += (len & 3) ? (4 - (len & 3)) : 4;
2567 	size = FOURBYTES + len;
2568 	ad->AttrLen = cpu_to_be16(size);
2569 	ad->AttrType = cpu_to_be16(RHBA_OPTION_ROM_VERSION);
2570 	return size;
2571 }
2572 
2573 static int
2574 lpfc_fdmi_hba_attr_fmw_ver(struct lpfc_vport *vport,
2575 			   struct lpfc_fdmi_attr_def *ad)
2576 {
2577 	struct lpfc_hba *phba = vport->phba;
2578 	struct lpfc_fdmi_attr_entry *ae;
2579 	uint32_t len, size;
2580 
2581 	ae = &ad->AttrValue;
2582 	memset(ae, 0, sizeof(*ae));
2583 
2584 	lpfc_decode_firmware_rev(phba, ae->un.AttrString, 1);
2585 	len = strnlen(ae->un.AttrString,
2586 			  sizeof(ae->un.AttrString));
2587 	len += (len & 3) ? (4 - (len & 3)) : 4;
2588 	size = FOURBYTES + len;
2589 	ad->AttrLen = cpu_to_be16(size);
2590 	ad->AttrType = cpu_to_be16(RHBA_FIRMWARE_VERSION);
2591 	return size;
2592 }
2593 
2594 static int
2595 lpfc_fdmi_hba_attr_os_ver(struct lpfc_vport *vport,
2596 			  struct lpfc_fdmi_attr_def *ad)
2597 {
2598 	struct lpfc_fdmi_attr_entry *ae;
2599 	uint32_t len, size;
2600 
2601 	ae = &ad->AttrValue;
2602 	memset(ae, 0, sizeof(*ae));
2603 
2604 	snprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s %s %s",
2605 		 init_utsname()->sysname,
2606 		 init_utsname()->release,
2607 		 init_utsname()->version);
2608 
2609 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2610 	len += (len & 3) ? (4 - (len & 3)) : 4;
2611 	size = FOURBYTES + len;
2612 	ad->AttrLen = cpu_to_be16(size);
2613 	ad->AttrType = cpu_to_be16(RHBA_OS_NAME_VERSION);
2614 	return size;
2615 }
2616 
2617 static int
2618 lpfc_fdmi_hba_attr_ct_len(struct lpfc_vport *vport,
2619 			  struct lpfc_fdmi_attr_def *ad)
2620 {
2621 	struct lpfc_fdmi_attr_entry *ae;
2622 	uint32_t size;
2623 
2624 	ae = &ad->AttrValue;
2625 
2626 	ae->un.AttrInt =  cpu_to_be32(LPFC_MAX_CT_SIZE);
2627 	size = FOURBYTES + sizeof(uint32_t);
2628 	ad->AttrLen = cpu_to_be16(size);
2629 	ad->AttrType = cpu_to_be16(RHBA_MAX_CT_PAYLOAD_LEN);
2630 	return size;
2631 }
2632 
2633 static int
2634 lpfc_fdmi_hba_attr_symbolic_name(struct lpfc_vport *vport,
2635 				 struct lpfc_fdmi_attr_def *ad)
2636 {
2637 	struct lpfc_fdmi_attr_entry *ae;
2638 	uint32_t len, size;
2639 
2640 	ae = &ad->AttrValue;
2641 	memset(ae, 0, sizeof(*ae));
2642 
2643 	len = lpfc_vport_symbolic_node_name(vport,
2644 				ae->un.AttrString, 256);
2645 	len += (len & 3) ? (4 - (len & 3)) : 4;
2646 	size = FOURBYTES + len;
2647 	ad->AttrLen = cpu_to_be16(size);
2648 	ad->AttrType = cpu_to_be16(RHBA_SYM_NODENAME);
2649 	return size;
2650 }
2651 
2652 static int
2653 lpfc_fdmi_hba_attr_vendor_info(struct lpfc_vport *vport,
2654 			       struct lpfc_fdmi_attr_def *ad)
2655 {
2656 	struct lpfc_fdmi_attr_entry *ae;
2657 	uint32_t size;
2658 
2659 	ae = &ad->AttrValue;
2660 
2661 	/* Nothing is defined for this currently */
2662 	ae->un.AttrInt =  cpu_to_be32(0);
2663 	size = FOURBYTES + sizeof(uint32_t);
2664 	ad->AttrLen = cpu_to_be16(size);
2665 	ad->AttrType = cpu_to_be16(RHBA_VENDOR_INFO);
2666 	return size;
2667 }
2668 
2669 static int
2670 lpfc_fdmi_hba_attr_num_ports(struct lpfc_vport *vport,
2671 			     struct lpfc_fdmi_attr_def *ad)
2672 {
2673 	struct lpfc_fdmi_attr_entry *ae;
2674 	uint32_t size;
2675 
2676 	ae = &ad->AttrValue;
2677 
2678 	/* Each driver instance corresponds to a single port */
2679 	ae->un.AttrInt =  cpu_to_be32(1);
2680 	size = FOURBYTES + sizeof(uint32_t);
2681 	ad->AttrLen = cpu_to_be16(size);
2682 	ad->AttrType = cpu_to_be16(RHBA_NUM_PORTS);
2683 	return size;
2684 }
2685 
2686 static int
2687 lpfc_fdmi_hba_attr_fabric_wwnn(struct lpfc_vport *vport,
2688 			       struct lpfc_fdmi_attr_def *ad)
2689 {
2690 	struct lpfc_fdmi_attr_entry *ae;
2691 	uint32_t size;
2692 
2693 	ae = &ad->AttrValue;
2694 	memset(ae, 0, sizeof(*ae));
2695 
2696 	memcpy(&ae->un.AttrWWN, &vport->fabric_nodename,
2697 	       sizeof(struct lpfc_name));
2698 	size = FOURBYTES + sizeof(struct lpfc_name);
2699 	ad->AttrLen = cpu_to_be16(size);
2700 	ad->AttrType = cpu_to_be16(RHBA_FABRIC_WWNN);
2701 	return size;
2702 }
2703 
2704 static int
2705 lpfc_fdmi_hba_attr_bios_ver(struct lpfc_vport *vport,
2706 			    struct lpfc_fdmi_attr_def *ad)
2707 {
2708 	struct lpfc_hba *phba = vport->phba;
2709 	struct lpfc_fdmi_attr_entry *ae;
2710 	uint32_t len, size;
2711 
2712 	ae = &ad->AttrValue;
2713 	memset(ae, 0, sizeof(*ae));
2714 
2715 	strlcat(ae->un.AttrString, phba->BIOSVersion,
2716 		sizeof(ae->un.AttrString));
2717 	len = strnlen(ae->un.AttrString,
2718 			  sizeof(ae->un.AttrString));
2719 	len += (len & 3) ? (4 - (len & 3)) : 4;
2720 	size = FOURBYTES + len;
2721 	ad->AttrLen = cpu_to_be16(size);
2722 	ad->AttrType = cpu_to_be16(RHBA_BIOS_VERSION);
2723 	return size;
2724 }
2725 
2726 static int
2727 lpfc_fdmi_hba_attr_bios_state(struct lpfc_vport *vport,
2728 			      struct lpfc_fdmi_attr_def *ad)
2729 {
2730 	struct lpfc_fdmi_attr_entry *ae;
2731 	uint32_t size;
2732 
2733 	ae = &ad->AttrValue;
2734 
2735 	/* Driver doesn't have access to this information */
2736 	ae->un.AttrInt =  cpu_to_be32(0);
2737 	size = FOURBYTES + sizeof(uint32_t);
2738 	ad->AttrLen = cpu_to_be16(size);
2739 	ad->AttrType = cpu_to_be16(RHBA_BIOS_STATE);
2740 	return size;
2741 }
2742 
2743 static int
2744 lpfc_fdmi_hba_attr_vendor_id(struct lpfc_vport *vport,
2745 			     struct lpfc_fdmi_attr_def *ad)
2746 {
2747 	struct lpfc_fdmi_attr_entry *ae;
2748 	uint32_t len, size;
2749 
2750 	ae = &ad->AttrValue;
2751 	memset(ae, 0, sizeof(*ae));
2752 
2753 	strncpy(ae->un.AttrString, "EMULEX",
2754 		sizeof(ae->un.AttrString));
2755 	len = strnlen(ae->un.AttrString,
2756 			  sizeof(ae->un.AttrString));
2757 	len += (len & 3) ? (4 - (len & 3)) : 4;
2758 	size = FOURBYTES + len;
2759 	ad->AttrLen = cpu_to_be16(size);
2760 	ad->AttrType = cpu_to_be16(RHBA_VENDOR_ID);
2761 	return size;
2762 }
2763 
2764 /* Routines for all individual PORT attributes */
2765 static int
2766 lpfc_fdmi_port_attr_fc4type(struct lpfc_vport *vport,
2767 			    struct lpfc_fdmi_attr_def *ad)
2768 {
2769 	struct lpfc_hba   *phba = vport->phba;
2770 	struct lpfc_fdmi_attr_entry *ae;
2771 	uint32_t size;
2772 
2773 	ae = &ad->AttrValue;
2774 	memset(ae, 0, sizeof(*ae));
2775 
2776 	ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
2777 	ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
2778 
2779 	/* Check to see if Firmware supports NVME and on physical port */
2780 	if ((phba->sli_rev == LPFC_SLI_REV4) && (vport == phba->pport) &&
2781 	    phba->sli4_hba.pc_sli4_params.nvme)
2782 		ae->un.AttrTypes[6] = 0x01; /* Type 0x28 - NVME */
2783 
2784 	size = FOURBYTES + 32;
2785 	ad->AttrLen = cpu_to_be16(size);
2786 	ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_FC4_TYPES);
2787 	return size;
2788 }
2789 
2790 static int
2791 lpfc_fdmi_port_attr_support_speed(struct lpfc_vport *vport,
2792 				  struct lpfc_fdmi_attr_def *ad)
2793 {
2794 	struct lpfc_hba   *phba = vport->phba;
2795 	struct lpfc_fdmi_attr_entry *ae;
2796 	uint32_t size;
2797 
2798 	ae = &ad->AttrValue;
2799 
2800 	ae->un.AttrInt = 0;
2801 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2802 		if (phba->lmt & LMT_128Gb)
2803 			ae->un.AttrInt |= HBA_PORTSPEED_128GFC;
2804 		if (phba->lmt & LMT_64Gb)
2805 			ae->un.AttrInt |= HBA_PORTSPEED_64GFC;
2806 		if (phba->lmt & LMT_32Gb)
2807 			ae->un.AttrInt |= HBA_PORTSPEED_32GFC;
2808 		if (phba->lmt & LMT_16Gb)
2809 			ae->un.AttrInt |= HBA_PORTSPEED_16GFC;
2810 		if (phba->lmt & LMT_10Gb)
2811 			ae->un.AttrInt |= HBA_PORTSPEED_10GFC;
2812 		if (phba->lmt & LMT_8Gb)
2813 			ae->un.AttrInt |= HBA_PORTSPEED_8GFC;
2814 		if (phba->lmt & LMT_4Gb)
2815 			ae->un.AttrInt |= HBA_PORTSPEED_4GFC;
2816 		if (phba->lmt & LMT_2Gb)
2817 			ae->un.AttrInt |= HBA_PORTSPEED_2GFC;
2818 		if (phba->lmt & LMT_1Gb)
2819 			ae->un.AttrInt |= HBA_PORTSPEED_1GFC;
2820 	} else {
2821 		/* FCoE links support only one speed */
2822 		switch (phba->fc_linkspeed) {
2823 		case LPFC_ASYNC_LINK_SPEED_10GBPS:
2824 			ae->un.AttrInt = HBA_PORTSPEED_10GE;
2825 			break;
2826 		case LPFC_ASYNC_LINK_SPEED_25GBPS:
2827 			ae->un.AttrInt = HBA_PORTSPEED_25GE;
2828 			break;
2829 		case LPFC_ASYNC_LINK_SPEED_40GBPS:
2830 			ae->un.AttrInt = HBA_PORTSPEED_40GE;
2831 			break;
2832 		case LPFC_ASYNC_LINK_SPEED_100GBPS:
2833 			ae->un.AttrInt = HBA_PORTSPEED_100GE;
2834 			break;
2835 		}
2836 	}
2837 	ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2838 	size = FOURBYTES + sizeof(uint32_t);
2839 	ad->AttrLen = cpu_to_be16(size);
2840 	ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_SPEED);
2841 	return size;
2842 }
2843 
2844 static int
2845 lpfc_fdmi_port_attr_speed(struct lpfc_vport *vport,
2846 			  struct lpfc_fdmi_attr_def *ad)
2847 {
2848 	struct lpfc_hba   *phba = vport->phba;
2849 	struct lpfc_fdmi_attr_entry *ae;
2850 	uint32_t size;
2851 
2852 	ae = &ad->AttrValue;
2853 
2854 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2855 		switch (phba->fc_linkspeed) {
2856 		case LPFC_LINK_SPEED_1GHZ:
2857 			ae->un.AttrInt = HBA_PORTSPEED_1GFC;
2858 			break;
2859 		case LPFC_LINK_SPEED_2GHZ:
2860 			ae->un.AttrInt = HBA_PORTSPEED_2GFC;
2861 			break;
2862 		case LPFC_LINK_SPEED_4GHZ:
2863 			ae->un.AttrInt = HBA_PORTSPEED_4GFC;
2864 			break;
2865 		case LPFC_LINK_SPEED_8GHZ:
2866 			ae->un.AttrInt = HBA_PORTSPEED_8GFC;
2867 			break;
2868 		case LPFC_LINK_SPEED_10GHZ:
2869 			ae->un.AttrInt = HBA_PORTSPEED_10GFC;
2870 			break;
2871 		case LPFC_LINK_SPEED_16GHZ:
2872 			ae->un.AttrInt = HBA_PORTSPEED_16GFC;
2873 			break;
2874 		case LPFC_LINK_SPEED_32GHZ:
2875 			ae->un.AttrInt = HBA_PORTSPEED_32GFC;
2876 			break;
2877 		case LPFC_LINK_SPEED_64GHZ:
2878 			ae->un.AttrInt = HBA_PORTSPEED_64GFC;
2879 			break;
2880 		case LPFC_LINK_SPEED_128GHZ:
2881 			ae->un.AttrInt = HBA_PORTSPEED_128GFC;
2882 			break;
2883 		default:
2884 			ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2885 			break;
2886 		}
2887 	} else {
2888 		switch (phba->fc_linkspeed) {
2889 		case LPFC_ASYNC_LINK_SPEED_10GBPS:
2890 			ae->un.AttrInt = HBA_PORTSPEED_10GE;
2891 			break;
2892 		case LPFC_ASYNC_LINK_SPEED_25GBPS:
2893 			ae->un.AttrInt = HBA_PORTSPEED_25GE;
2894 			break;
2895 		case LPFC_ASYNC_LINK_SPEED_40GBPS:
2896 			ae->un.AttrInt = HBA_PORTSPEED_40GE;
2897 			break;
2898 		case LPFC_ASYNC_LINK_SPEED_100GBPS:
2899 			ae->un.AttrInt = HBA_PORTSPEED_100GE;
2900 			break;
2901 		default:
2902 			ae->un.AttrInt = HBA_PORTSPEED_UNKNOWN;
2903 			break;
2904 		}
2905 	}
2906 
2907 	ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2908 	size = FOURBYTES + sizeof(uint32_t);
2909 	ad->AttrLen = cpu_to_be16(size);
2910 	ad->AttrType = cpu_to_be16(RPRT_PORT_SPEED);
2911 	return size;
2912 }
2913 
2914 static int
2915 lpfc_fdmi_port_attr_max_frame(struct lpfc_vport *vport,
2916 			      struct lpfc_fdmi_attr_def *ad)
2917 {
2918 	struct serv_parm *hsp;
2919 	struct lpfc_fdmi_attr_entry *ae;
2920 	uint32_t size;
2921 
2922 	ae = &ad->AttrValue;
2923 
2924 	hsp = (struct serv_parm *)&vport->fc_sparam;
2925 	ae->un.AttrInt = (((uint32_t) hsp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
2926 			  (uint32_t) hsp->cmn.bbRcvSizeLsb;
2927 	ae->un.AttrInt = cpu_to_be32(ae->un.AttrInt);
2928 	size = FOURBYTES + sizeof(uint32_t);
2929 	ad->AttrLen = cpu_to_be16(size);
2930 	ad->AttrType = cpu_to_be16(RPRT_MAX_FRAME_SIZE);
2931 	return size;
2932 }
2933 
2934 static int
2935 lpfc_fdmi_port_attr_os_devname(struct lpfc_vport *vport,
2936 			       struct lpfc_fdmi_attr_def *ad)
2937 {
2938 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2939 	struct lpfc_fdmi_attr_entry *ae;
2940 	uint32_t len, size;
2941 
2942 	ae = &ad->AttrValue;
2943 	memset(ae, 0, sizeof(*ae));
2944 
2945 	snprintf(ae->un.AttrString, sizeof(ae->un.AttrString),
2946 		 "/sys/class/scsi_host/host%d", shost->host_no);
2947 	len = strnlen((char *)ae->un.AttrString,
2948 			  sizeof(ae->un.AttrString));
2949 	len += (len & 3) ? (4 - (len & 3)) : 4;
2950 	size = FOURBYTES + len;
2951 	ad->AttrLen = cpu_to_be16(size);
2952 	ad->AttrType = cpu_to_be16(RPRT_OS_DEVICE_NAME);
2953 	return size;
2954 }
2955 
2956 static int
2957 lpfc_fdmi_port_attr_host_name(struct lpfc_vport *vport,
2958 			      struct lpfc_fdmi_attr_def *ad)
2959 {
2960 	struct lpfc_fdmi_attr_entry *ae;
2961 	uint32_t len, size;
2962 
2963 	ae = &ad->AttrValue;
2964 	memset(ae, 0, sizeof(*ae));
2965 
2966 	scnprintf(ae->un.AttrString, sizeof(ae->un.AttrString), "%s",
2967 		  vport->phba->os_host_name);
2968 
2969 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
2970 	len += (len & 3) ? (4 - (len & 3)) : 4;
2971 	size = FOURBYTES + len;
2972 	ad->AttrLen = cpu_to_be16(size);
2973 	ad->AttrType = cpu_to_be16(RPRT_HOST_NAME);
2974 	return size;
2975 }
2976 
2977 static int
2978 lpfc_fdmi_port_attr_wwnn(struct lpfc_vport *vport,
2979 			 struct lpfc_fdmi_attr_def *ad)
2980 {
2981 	struct lpfc_fdmi_attr_entry *ae;
2982 	uint32_t size;
2983 
2984 	ae = &ad->AttrValue;
2985 	memset(ae, 0, sizeof(*ae));
2986 
2987 	memcpy(&ae->un.AttrWWN, &vport->fc_sparam.nodeName,
2988 	       sizeof(struct lpfc_name));
2989 	size = FOURBYTES + sizeof(struct lpfc_name);
2990 	ad->AttrLen = cpu_to_be16(size);
2991 	ad->AttrType = cpu_to_be16(RPRT_NODENAME);
2992 	return size;
2993 }
2994 
2995 static int
2996 lpfc_fdmi_port_attr_wwpn(struct lpfc_vport *vport,
2997 			 struct lpfc_fdmi_attr_def *ad)
2998 {
2999 	struct lpfc_fdmi_attr_entry *ae;
3000 	uint32_t size;
3001 
3002 	ae = &ad->AttrValue;
3003 	memset(ae, 0, sizeof(*ae));
3004 
3005 	memcpy(&ae->un.AttrWWN, &vport->fc_sparam.portName,
3006 	       sizeof(struct lpfc_name));
3007 	size = FOURBYTES + sizeof(struct lpfc_name);
3008 	ad->AttrLen = cpu_to_be16(size);
3009 	ad->AttrType = cpu_to_be16(RPRT_PORTNAME);
3010 	return size;
3011 }
3012 
3013 static int
3014 lpfc_fdmi_port_attr_symbolic_name(struct lpfc_vport *vport,
3015 				  struct lpfc_fdmi_attr_def *ad)
3016 {
3017 	struct lpfc_fdmi_attr_entry *ae;
3018 	uint32_t len, size;
3019 
3020 	ae = &ad->AttrValue;
3021 	memset(ae, 0, sizeof(*ae));
3022 
3023 	len = lpfc_vport_symbolic_port_name(vport, ae->un.AttrString, 256);
3024 	len += (len & 3) ? (4 - (len & 3)) : 4;
3025 	size = FOURBYTES + len;
3026 	ad->AttrLen = cpu_to_be16(size);
3027 	ad->AttrType = cpu_to_be16(RPRT_SYM_PORTNAME);
3028 	return size;
3029 }
3030 
3031 static int
3032 lpfc_fdmi_port_attr_port_type(struct lpfc_vport *vport,
3033 			      struct lpfc_fdmi_attr_def *ad)
3034 {
3035 	struct lpfc_hba *phba = vport->phba;
3036 	struct lpfc_fdmi_attr_entry *ae;
3037 	uint32_t size;
3038 
3039 	ae = &ad->AttrValue;
3040 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
3041 		ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NLPORT);
3042 	else
3043 		ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTTYPE_NPORT);
3044 	size = FOURBYTES + sizeof(uint32_t);
3045 	ad->AttrLen = cpu_to_be16(size);
3046 	ad->AttrType = cpu_to_be16(RPRT_PORT_TYPE);
3047 	return size;
3048 }
3049 
3050 static int
3051 lpfc_fdmi_port_attr_class(struct lpfc_vport *vport,
3052 			  struct lpfc_fdmi_attr_def *ad)
3053 {
3054 	struct lpfc_fdmi_attr_entry *ae;
3055 	uint32_t size;
3056 
3057 	ae = &ad->AttrValue;
3058 	ae->un.AttrInt = cpu_to_be32(FC_COS_CLASS2 | FC_COS_CLASS3);
3059 	size = FOURBYTES + sizeof(uint32_t);
3060 	ad->AttrLen = cpu_to_be16(size);
3061 	ad->AttrType = cpu_to_be16(RPRT_SUPPORTED_CLASS);
3062 	return size;
3063 }
3064 
3065 static int
3066 lpfc_fdmi_port_attr_fabric_wwpn(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 	memset(ae, 0, sizeof(*ae));
3074 
3075 	memcpy(&ae->un.AttrWWN, &vport->fabric_portname,
3076 	       sizeof(struct lpfc_name));
3077 	size = FOURBYTES + sizeof(struct lpfc_name);
3078 	ad->AttrLen = cpu_to_be16(size);
3079 	ad->AttrType = cpu_to_be16(RPRT_FABRICNAME);
3080 	return size;
3081 }
3082 
3083 static int
3084 lpfc_fdmi_port_attr_active_fc4type(struct lpfc_vport *vport,
3085 				   struct lpfc_fdmi_attr_def *ad)
3086 {
3087 	struct lpfc_fdmi_attr_entry *ae;
3088 	uint32_t size;
3089 
3090 	ae = &ad->AttrValue;
3091 	memset(ae, 0, sizeof(*ae));
3092 
3093 	ae->un.AttrTypes[2] = 0x01; /* Type 0x8 - FCP */
3094 	ae->un.AttrTypes[7] = 0x01; /* Type 0x20 - CT */
3095 
3096 	/* Check to see if NVME is configured or not */
3097 	if (vport->phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)
3098 		ae->un.AttrTypes[6] = 0x1; /* Type 0x28 - NVME */
3099 
3100 	size = FOURBYTES + 32;
3101 	ad->AttrLen = cpu_to_be16(size);
3102 	ad->AttrType = cpu_to_be16(RPRT_ACTIVE_FC4_TYPES);
3103 	return size;
3104 }
3105 
3106 static int
3107 lpfc_fdmi_port_attr_port_state(struct lpfc_vport *vport,
3108 			       struct lpfc_fdmi_attr_def *ad)
3109 {
3110 	struct lpfc_fdmi_attr_entry *ae;
3111 	uint32_t size;
3112 
3113 	ae = &ad->AttrValue;
3114 	/* Link Up - operational */
3115 	ae->un.AttrInt =  cpu_to_be32(LPFC_FDMI_PORTSTATE_ONLINE);
3116 	size = FOURBYTES + sizeof(uint32_t);
3117 	ad->AttrLen = cpu_to_be16(size);
3118 	ad->AttrType = cpu_to_be16(RPRT_PORT_STATE);
3119 	return size;
3120 }
3121 
3122 static int
3123 lpfc_fdmi_port_attr_num_disc(struct lpfc_vport *vport,
3124 			     struct lpfc_fdmi_attr_def *ad)
3125 {
3126 	struct lpfc_fdmi_attr_entry *ae;
3127 	uint32_t size;
3128 
3129 	ae = &ad->AttrValue;
3130 	vport->fdmi_num_disc = lpfc_find_map_node(vport);
3131 	ae->un.AttrInt = cpu_to_be32(vport->fdmi_num_disc);
3132 	size = FOURBYTES + sizeof(uint32_t);
3133 	ad->AttrLen = cpu_to_be16(size);
3134 	ad->AttrType = cpu_to_be16(RPRT_DISC_PORT);
3135 	return size;
3136 }
3137 
3138 static int
3139 lpfc_fdmi_port_attr_nportid(struct lpfc_vport *vport,
3140 			    struct lpfc_fdmi_attr_def *ad)
3141 {
3142 	struct lpfc_fdmi_attr_entry *ae;
3143 	uint32_t size;
3144 
3145 	ae = &ad->AttrValue;
3146 	ae->un.AttrInt =  cpu_to_be32(vport->fc_myDID);
3147 	size = FOURBYTES + sizeof(uint32_t);
3148 	ad->AttrLen = cpu_to_be16(size);
3149 	ad->AttrType = cpu_to_be16(RPRT_PORT_ID);
3150 	return size;
3151 }
3152 
3153 static int
3154 lpfc_fdmi_smart_attr_service(struct lpfc_vport *vport,
3155 			     struct lpfc_fdmi_attr_def *ad)
3156 {
3157 	struct lpfc_fdmi_attr_entry *ae;
3158 	uint32_t len, size;
3159 
3160 	ae = &ad->AttrValue;
3161 	memset(ae, 0, sizeof(*ae));
3162 
3163 	strncpy(ae->un.AttrString, "Smart SAN Initiator",
3164 		sizeof(ae->un.AttrString));
3165 	len = strnlen(ae->un.AttrString,
3166 			  sizeof(ae->un.AttrString));
3167 	len += (len & 3) ? (4 - (len & 3)) : 4;
3168 	size = FOURBYTES + len;
3169 	ad->AttrLen = cpu_to_be16(size);
3170 	ad->AttrType = cpu_to_be16(RPRT_SMART_SERVICE);
3171 	return size;
3172 }
3173 
3174 static int
3175 lpfc_fdmi_smart_attr_guid(struct lpfc_vport *vport,
3176 			  struct lpfc_fdmi_attr_def *ad)
3177 {
3178 	struct lpfc_fdmi_attr_entry *ae;
3179 	uint32_t size;
3180 
3181 	ae = &ad->AttrValue;
3182 	memset(ae, 0, sizeof(*ae));
3183 
3184 	memcpy(&ae->un.AttrString, &vport->fc_sparam.nodeName,
3185 	       sizeof(struct lpfc_name));
3186 	memcpy((((uint8_t *)&ae->un.AttrString) +
3187 		sizeof(struct lpfc_name)),
3188 		&vport->fc_sparam.portName, sizeof(struct lpfc_name));
3189 	size = FOURBYTES + (2 * sizeof(struct lpfc_name));
3190 	ad->AttrLen =  cpu_to_be16(size);
3191 	ad->AttrType = cpu_to_be16(RPRT_SMART_GUID);
3192 	return size;
3193 }
3194 
3195 static int
3196 lpfc_fdmi_smart_attr_version(struct lpfc_vport *vport,
3197 			     struct lpfc_fdmi_attr_def *ad)
3198 {
3199 	struct lpfc_fdmi_attr_entry *ae;
3200 	uint32_t len, size;
3201 
3202 	ae = &ad->AttrValue;
3203 	memset(ae, 0, sizeof(*ae));
3204 
3205 	strncpy(ae->un.AttrString, "Smart SAN Version 2.0",
3206 		sizeof(ae->un.AttrString));
3207 	len = strnlen(ae->un.AttrString,
3208 			  sizeof(ae->un.AttrString));
3209 	len += (len & 3) ? (4 - (len & 3)) : 4;
3210 	size = FOURBYTES + len;
3211 	ad->AttrLen =  cpu_to_be16(size);
3212 	ad->AttrType = cpu_to_be16(RPRT_SMART_VERSION);
3213 	return size;
3214 }
3215 
3216 static int
3217 lpfc_fdmi_smart_attr_model(struct lpfc_vport *vport,
3218 			   struct lpfc_fdmi_attr_def *ad)
3219 {
3220 	struct lpfc_hba *phba = vport->phba;
3221 	struct lpfc_fdmi_attr_entry *ae;
3222 	uint32_t len, size;
3223 
3224 	ae = &ad->AttrValue;
3225 	memset(ae, 0, sizeof(*ae));
3226 
3227 	strncpy(ae->un.AttrString, phba->ModelName,
3228 		sizeof(ae->un.AttrString));
3229 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3230 	len += (len & 3) ? (4 - (len & 3)) : 4;
3231 	size = FOURBYTES + len;
3232 	ad->AttrLen = cpu_to_be16(size);
3233 	ad->AttrType = cpu_to_be16(RPRT_SMART_MODEL);
3234 	return size;
3235 }
3236 
3237 static int
3238 lpfc_fdmi_smart_attr_port_info(struct lpfc_vport *vport,
3239 			       struct lpfc_fdmi_attr_def *ad)
3240 {
3241 	struct lpfc_fdmi_attr_entry *ae;
3242 	uint32_t size;
3243 
3244 	ae = &ad->AttrValue;
3245 
3246 	/* SRIOV (type 3) is not supported */
3247 	if (vport->vpi)
3248 		ae->un.AttrInt =  cpu_to_be32(2);  /* NPIV */
3249 	else
3250 		ae->un.AttrInt =  cpu_to_be32(1);  /* Physical */
3251 	size = FOURBYTES + sizeof(uint32_t);
3252 	ad->AttrLen = cpu_to_be16(size);
3253 	ad->AttrType = cpu_to_be16(RPRT_SMART_PORT_INFO);
3254 	return size;
3255 }
3256 
3257 static int
3258 lpfc_fdmi_smart_attr_qos(struct lpfc_vport *vport,
3259 			 struct lpfc_fdmi_attr_def *ad)
3260 {
3261 	struct lpfc_fdmi_attr_entry *ae;
3262 	uint32_t size;
3263 
3264 	ae = &ad->AttrValue;
3265 	ae->un.AttrInt =  cpu_to_be32(0);
3266 	size = FOURBYTES + sizeof(uint32_t);
3267 	ad->AttrLen = cpu_to_be16(size);
3268 	ad->AttrType = cpu_to_be16(RPRT_SMART_QOS);
3269 	return size;
3270 }
3271 
3272 static int
3273 lpfc_fdmi_smart_attr_security(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(1);
3281 	size = FOURBYTES + sizeof(uint32_t);
3282 	ad->AttrLen = cpu_to_be16(size);
3283 	ad->AttrType = cpu_to_be16(RPRT_SMART_SECURITY);
3284 	return size;
3285 }
3286 
3287 int
3288 lpfc_fdmi_vendor_attr_mi(struct lpfc_vport *vport,
3289 			  struct lpfc_fdmi_attr_def *ad)
3290 {
3291 	struct lpfc_hba *phba = vport->phba;
3292 	struct lpfc_fdmi_attr_entry *ae;
3293 	uint32_t len, size;
3294 	char mibrevision[16];
3295 
3296 	ae = (struct lpfc_fdmi_attr_entry *)&ad->AttrValue;
3297 	memset(ae, 0, 256);
3298 	sprintf(mibrevision, "ELXE2EM:%04d",
3299 		phba->sli4_hba.pc_sli4_params.mi_value);
3300 	strncpy(ae->un.AttrString, &mibrevision[0], sizeof(ae->un.AttrString));
3301 	len = strnlen(ae->un.AttrString, sizeof(ae->un.AttrString));
3302 	len += (len & 3) ? (4 - (len & 3)) : 4;
3303 	size = FOURBYTES + len;
3304 	ad->AttrLen = cpu_to_be16(size);
3305 	ad->AttrType = cpu_to_be16(RPRT_VENDOR_MI);
3306 	return size;
3307 }
3308 
3309 /* RHBA attribute jump table */
3310 int (*lpfc_fdmi_hba_action[])
3311 	(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
3312 	/* Action routine                 Mask bit     Attribute type */
3313 	lpfc_fdmi_hba_attr_wwnn,	  /* bit0     RHBA_NODENAME           */
3314 	lpfc_fdmi_hba_attr_manufacturer,  /* bit1     RHBA_MANUFACTURER       */
3315 	lpfc_fdmi_hba_attr_sn,		  /* bit2     RHBA_SERIAL_NUMBER      */
3316 	lpfc_fdmi_hba_attr_model,	  /* bit3     RHBA_MODEL              */
3317 	lpfc_fdmi_hba_attr_description,	  /* bit4     RHBA_MODEL_DESCRIPTION  */
3318 	lpfc_fdmi_hba_attr_hdw_ver,	  /* bit5     RHBA_HARDWARE_VERSION   */
3319 	lpfc_fdmi_hba_attr_drvr_ver,	  /* bit6     RHBA_DRIVER_VERSION     */
3320 	lpfc_fdmi_hba_attr_rom_ver,	  /* bit7     RHBA_OPTION_ROM_VERSION */
3321 	lpfc_fdmi_hba_attr_fmw_ver,	  /* bit8     RHBA_FIRMWARE_VERSION   */
3322 	lpfc_fdmi_hba_attr_os_ver,	  /* bit9     RHBA_OS_NAME_VERSION    */
3323 	lpfc_fdmi_hba_attr_ct_len,	  /* bit10    RHBA_MAX_CT_PAYLOAD_LEN */
3324 	lpfc_fdmi_hba_attr_symbolic_name, /* bit11    RHBA_SYM_NODENAME       */
3325 	lpfc_fdmi_hba_attr_vendor_info,	  /* bit12    RHBA_VENDOR_INFO        */
3326 	lpfc_fdmi_hba_attr_num_ports,	  /* bit13    RHBA_NUM_PORTS          */
3327 	lpfc_fdmi_hba_attr_fabric_wwnn,	  /* bit14    RHBA_FABRIC_WWNN        */
3328 	lpfc_fdmi_hba_attr_bios_ver,	  /* bit15    RHBA_BIOS_VERSION       */
3329 	lpfc_fdmi_hba_attr_bios_state,	  /* bit16    RHBA_BIOS_STATE         */
3330 	lpfc_fdmi_hba_attr_vendor_id,	  /* bit17    RHBA_VENDOR_ID          */
3331 };
3332 
3333 /* RPA / RPRT attribute jump table */
3334 int (*lpfc_fdmi_port_action[])
3335 	(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad) = {
3336 	/* Action routine                   Mask bit   Attribute type */
3337 	lpfc_fdmi_port_attr_fc4type,        /* bit0   RPRT_SUPPORT_FC4_TYPES  */
3338 	lpfc_fdmi_port_attr_support_speed,  /* bit1   RPRT_SUPPORTED_SPEED    */
3339 	lpfc_fdmi_port_attr_speed,          /* bit2   RPRT_PORT_SPEED         */
3340 	lpfc_fdmi_port_attr_max_frame,      /* bit3   RPRT_MAX_FRAME_SIZE     */
3341 	lpfc_fdmi_port_attr_os_devname,     /* bit4   RPRT_OS_DEVICE_NAME     */
3342 	lpfc_fdmi_port_attr_host_name,      /* bit5   RPRT_HOST_NAME          */
3343 	lpfc_fdmi_port_attr_wwnn,           /* bit6   RPRT_NODENAME           */
3344 	lpfc_fdmi_port_attr_wwpn,           /* bit7   RPRT_PORTNAME           */
3345 	lpfc_fdmi_port_attr_symbolic_name,  /* bit8   RPRT_SYM_PORTNAME       */
3346 	lpfc_fdmi_port_attr_port_type,      /* bit9   RPRT_PORT_TYPE          */
3347 	lpfc_fdmi_port_attr_class,          /* bit10  RPRT_SUPPORTED_CLASS    */
3348 	lpfc_fdmi_port_attr_fabric_wwpn,    /* bit11  RPRT_FABRICNAME         */
3349 	lpfc_fdmi_port_attr_active_fc4type, /* bit12  RPRT_ACTIVE_FC4_TYPES   */
3350 	lpfc_fdmi_port_attr_port_state,     /* bit13  RPRT_PORT_STATE         */
3351 	lpfc_fdmi_port_attr_num_disc,       /* bit14  RPRT_DISC_PORT          */
3352 	lpfc_fdmi_port_attr_nportid,        /* bit15  RPRT_PORT_ID            */
3353 	lpfc_fdmi_smart_attr_service,       /* bit16  RPRT_SMART_SERVICE      */
3354 	lpfc_fdmi_smart_attr_guid,          /* bit17  RPRT_SMART_GUID         */
3355 	lpfc_fdmi_smart_attr_version,       /* bit18  RPRT_SMART_VERSION      */
3356 	lpfc_fdmi_smart_attr_model,         /* bit19  RPRT_SMART_MODEL        */
3357 	lpfc_fdmi_smart_attr_port_info,     /* bit20  RPRT_SMART_PORT_INFO    */
3358 	lpfc_fdmi_smart_attr_qos,           /* bit21  RPRT_SMART_QOS          */
3359 	lpfc_fdmi_smart_attr_security,      /* bit22  RPRT_SMART_SECURITY     */
3360 	lpfc_fdmi_vendor_attr_mi,           /* bit23  RPRT_VENDOR_MI          */
3361 };
3362 
3363 /**
3364  * lpfc_fdmi_cmd - Build and send a FDMI cmd to the specified NPort
3365  * @vport: pointer to a host virtual N_Port data structure.
3366  * @ndlp: ndlp to send FDMI cmd to (if NULL use FDMI_DID)
3367  * @cmdcode: FDMI command to send
3368  * @new_mask: Mask of HBA or PORT Attributes to send
3369  *
3370  * Builds and sends a FDMI command using the CT subsystem.
3371  */
3372 int
3373 lpfc_fdmi_cmd(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3374 	      int cmdcode, uint32_t new_mask)
3375 {
3376 	struct lpfc_hba *phba = vport->phba;
3377 	struct lpfc_dmabuf *mp, *bmp;
3378 	struct lpfc_sli_ct_request *CtReq;
3379 	struct ulp_bde64 *bpl;
3380 	uint32_t bit_pos;
3381 	uint32_t size;
3382 	uint32_t rsp_size;
3383 	uint32_t mask;
3384 	struct lpfc_fdmi_reg_hba *rh;
3385 	struct lpfc_fdmi_port_entry *pe;
3386 	struct lpfc_fdmi_reg_portattr *pab = NULL;
3387 	struct lpfc_fdmi_attr_block *ab = NULL;
3388 	int  (*func)(struct lpfc_vport *vport, struct lpfc_fdmi_attr_def *ad);
3389 	void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
3390 		     struct lpfc_iocbq *);
3391 
3392 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3393 		return 0;
3394 
3395 	cmpl = lpfc_cmpl_ct_disc_fdmi; /* called from discovery */
3396 
3397 	/* fill in BDEs for command */
3398 	/* Allocate buffer for command payload */
3399 	mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3400 	if (!mp)
3401 		goto fdmi_cmd_exit;
3402 
3403 	mp->virt = lpfc_mbuf_alloc(phba, 0, &(mp->phys));
3404 	if (!mp->virt)
3405 		goto fdmi_cmd_free_mp;
3406 
3407 	/* Allocate buffer for Buffer ptr list */
3408 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3409 	if (!bmp)
3410 		goto fdmi_cmd_free_mpvirt;
3411 
3412 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &(bmp->phys));
3413 	if (!bmp->virt)
3414 		goto fdmi_cmd_free_bmp;
3415 
3416 	INIT_LIST_HEAD(&mp->list);
3417 	INIT_LIST_HEAD(&bmp->list);
3418 
3419 	/* FDMI request */
3420 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3421 			 "0218 FDMI Request Data: x%x x%x x%x\n",
3422 			 vport->fc_flag, vport->port_state, cmdcode);
3423 	CtReq = (struct lpfc_sli_ct_request *)mp->virt;
3424 
3425 	/* First populate the CT_IU preamble */
3426 	memset(CtReq, 0, sizeof(struct lpfc_sli_ct_request));
3427 	CtReq->RevisionId.bits.Revision = SLI_CT_REVISION;
3428 	CtReq->RevisionId.bits.InId = 0;
3429 
3430 	CtReq->FsType = SLI_CT_MANAGEMENT_SERVICE;
3431 	CtReq->FsSubType = SLI_CT_FDMI_Subtypes;
3432 
3433 	CtReq->CommandResponse.bits.CmdRsp = cpu_to_be16(cmdcode);
3434 	rsp_size = LPFC_BPL_SIZE;
3435 	size = 0;
3436 
3437 	/* Next fill in the specific FDMI cmd information */
3438 	switch (cmdcode) {
3439 	case SLI_MGMT_RHAT:
3440 	case SLI_MGMT_RHBA:
3441 		rh = (struct lpfc_fdmi_reg_hba *)&CtReq->un.PortID;
3442 		/* HBA Identifier */
3443 		memcpy(&rh->hi.PortName, &phba->pport->fc_sparam.portName,
3444 		       sizeof(struct lpfc_name));
3445 
3446 		if (cmdcode == SLI_MGMT_RHBA) {
3447 			/* Registered Port List */
3448 			/* One entry (port) per adapter */
3449 			rh->rpl.EntryCnt = cpu_to_be32(1);
3450 			memcpy(&rh->rpl.pe.PortName,
3451 			       &phba->pport->fc_sparam.portName,
3452 			       sizeof(struct lpfc_name));
3453 
3454 			/* point to the HBA attribute block */
3455 			size = 2 * sizeof(struct lpfc_name) +
3456 				FOURBYTES;
3457 		} else {
3458 			size = sizeof(struct lpfc_name);
3459 		}
3460 		ab = (struct lpfc_fdmi_attr_block *)((uint8_t *)rh + size);
3461 		ab->EntryCnt = 0;
3462 		size += FOURBYTES;
3463 		bit_pos = 0;
3464 		if (new_mask)
3465 			mask = new_mask;
3466 		else
3467 			mask = vport->fdmi_hba_mask;
3468 
3469 		/* Mask will dictate what attributes to build in the request */
3470 		while (mask) {
3471 			if (mask & 0x1) {
3472 				func = lpfc_fdmi_hba_action[bit_pos];
3473 				size += func(vport,
3474 					     (struct lpfc_fdmi_attr_def *)
3475 					     ((uint8_t *)rh + size));
3476 				ab->EntryCnt++;
3477 				if ((size + 256) >
3478 				    (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3479 					goto hba_out;
3480 			}
3481 			mask = mask >> 1;
3482 			bit_pos++;
3483 		}
3484 hba_out:
3485 		ab->EntryCnt = cpu_to_be32(ab->EntryCnt);
3486 		/* Total size */
3487 		size = GID_REQUEST_SZ - 4 + size;
3488 		break;
3489 
3490 	case SLI_MGMT_RPRT:
3491 	case SLI_MGMT_RPA:
3492 		pab = (struct lpfc_fdmi_reg_portattr *)&CtReq->un.PortID;
3493 		if (cmdcode == SLI_MGMT_RPRT) {
3494 			rh = (struct lpfc_fdmi_reg_hba *)pab;
3495 			/* HBA Identifier */
3496 			memcpy(&rh->hi.PortName,
3497 			       &phba->pport->fc_sparam.portName,
3498 			       sizeof(struct lpfc_name));
3499 			pab = (struct lpfc_fdmi_reg_portattr *)
3500 				((uint8_t *)pab +  sizeof(struct lpfc_name));
3501 		}
3502 
3503 		memcpy((uint8_t *)&pab->PortName,
3504 		       (uint8_t *)&vport->fc_sparam.portName,
3505 		       sizeof(struct lpfc_name));
3506 		size += sizeof(struct lpfc_name) + FOURBYTES;
3507 		pab->ab.EntryCnt = 0;
3508 		bit_pos = 0;
3509 		if (new_mask)
3510 			mask = new_mask;
3511 		else
3512 			mask = vport->fdmi_port_mask;
3513 
3514 		/* Mask will dictate what attributes to build in the request */
3515 		while (mask) {
3516 			if (mask & 0x1) {
3517 				func = lpfc_fdmi_port_action[bit_pos];
3518 				size += func(vport,
3519 					     (struct lpfc_fdmi_attr_def *)
3520 					     ((uint8_t *)pab + size));
3521 				pab->ab.EntryCnt++;
3522 				if ((size + 256) >
3523 				    (LPFC_BPL_SIZE - LPFC_CT_PREAMBLE))
3524 					goto port_out;
3525 			}
3526 			mask = mask >> 1;
3527 			bit_pos++;
3528 		}
3529 port_out:
3530 		pab->ab.EntryCnt = cpu_to_be32(pab->ab.EntryCnt);
3531 		/* Total size */
3532 		if (cmdcode == SLI_MGMT_RPRT)
3533 			size += sizeof(struct lpfc_name);
3534 		size = GID_REQUEST_SZ - 4 + size;
3535 		break;
3536 
3537 	case SLI_MGMT_GHAT:
3538 	case SLI_MGMT_GRPL:
3539 		rsp_size = FC_MAX_NS_RSP;
3540 		fallthrough;
3541 	case SLI_MGMT_DHBA:
3542 	case SLI_MGMT_DHAT:
3543 		pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3544 		memcpy((uint8_t *)&pe->PortName,
3545 		       (uint8_t *)&vport->fc_sparam.portName,
3546 		       sizeof(struct lpfc_name));
3547 		size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3548 		break;
3549 
3550 	case SLI_MGMT_GPAT:
3551 	case SLI_MGMT_GPAS:
3552 		rsp_size = FC_MAX_NS_RSP;
3553 		fallthrough;
3554 	case SLI_MGMT_DPRT:
3555 	case SLI_MGMT_DPA:
3556 		pe = (struct lpfc_fdmi_port_entry *)&CtReq->un.PortID;
3557 		memcpy((uint8_t *)&pe->PortName,
3558 		       (uint8_t *)&vport->fc_sparam.portName,
3559 		       sizeof(struct lpfc_name));
3560 		size = GID_REQUEST_SZ - 4 + sizeof(struct lpfc_name);
3561 		break;
3562 	case SLI_MGMT_GRHL:
3563 		size = GID_REQUEST_SZ - 4;
3564 		break;
3565 	default:
3566 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3567 				 "0298 FDMI cmdcode x%x not supported\n",
3568 				 cmdcode);
3569 		goto fdmi_cmd_free_bmpvirt;
3570 	}
3571 	CtReq->CommandResponse.bits.Size = cpu_to_be16(rsp_size);
3572 
3573 	bpl = (struct ulp_bde64 *)bmp->virt;
3574 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
3575 	bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
3576 	bpl->tus.f.bdeFlags = 0;
3577 	bpl->tus.f.bdeSize = size;
3578 
3579 	/*
3580 	 * The lpfc_ct_cmd/lpfc_get_req shall increment ndlp reference count
3581 	 * to hold ndlp reference for the corresponding callback function.
3582 	 */
3583 	if (!lpfc_ct_cmd(vport, mp, bmp, ndlp, cmpl, rsp_size, 0))
3584 		return 0;
3585 
3586 	/*
3587 	 * Decrement ndlp reference count to release ndlp reference held
3588 	 * for the failed command's callback function.
3589 	 */
3590 	lpfc_nlp_put(ndlp);
3591 
3592 fdmi_cmd_free_bmpvirt:
3593 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3594 fdmi_cmd_free_bmp:
3595 	kfree(bmp);
3596 fdmi_cmd_free_mpvirt:
3597 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3598 fdmi_cmd_free_mp:
3599 	kfree(mp);
3600 fdmi_cmd_exit:
3601 	/* Issue FDMI request failed */
3602 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3603 			 "0244 Issue FDMI request failed Data: x%x\n",
3604 			 cmdcode);
3605 	return 1;
3606 }
3607 
3608 /**
3609  * lpfc_delayed_disc_tmo - Timeout handler for delayed discovery timer.
3610  * @t: Context object of the timer.
3611  *
3612  * This function set the WORKER_DELAYED_DISC_TMO flag and wake up
3613  * the worker thread.
3614  **/
3615 void
3616 lpfc_delayed_disc_tmo(struct timer_list *t)
3617 {
3618 	struct lpfc_vport *vport = from_timer(vport, t, delayed_disc_tmo);
3619 	struct lpfc_hba   *phba = vport->phba;
3620 	uint32_t tmo_posted;
3621 	unsigned long iflag;
3622 
3623 	spin_lock_irqsave(&vport->work_port_lock, iflag);
3624 	tmo_posted = vport->work_port_events & WORKER_DELAYED_DISC_TMO;
3625 	if (!tmo_posted)
3626 		vport->work_port_events |= WORKER_DELAYED_DISC_TMO;
3627 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
3628 
3629 	if (!tmo_posted)
3630 		lpfc_worker_wake_up(phba);
3631 	return;
3632 }
3633 
3634 /**
3635  * lpfc_delayed_disc_timeout_handler - Function called by worker thread to
3636  *      handle delayed discovery.
3637  * @vport: pointer to a host virtual N_Port data structure.
3638  *
3639  * This function start nport discovery of the vport.
3640  **/
3641 void
3642 lpfc_delayed_disc_timeout_handler(struct lpfc_vport *vport)
3643 {
3644 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3645 
3646 	spin_lock_irq(shost->host_lock);
3647 	if (!(vport->fc_flag & FC_DISC_DELAYED)) {
3648 		spin_unlock_irq(shost->host_lock);
3649 		return;
3650 	}
3651 	vport->fc_flag &= ~FC_DISC_DELAYED;
3652 	spin_unlock_irq(shost->host_lock);
3653 
3654 	lpfc_do_scr_ns_plogi(vport->phba, vport);
3655 }
3656 
3657 void
3658 lpfc_decode_firmware_rev(struct lpfc_hba *phba, char *fwrevision, int flag)
3659 {
3660 	struct lpfc_sli *psli = &phba->sli;
3661 	lpfc_vpd_t *vp = &phba->vpd;
3662 	uint32_t b1, b2, b3, b4, i, rev;
3663 	char c;
3664 	uint32_t *ptr, str[4];
3665 	uint8_t *fwname;
3666 
3667 	if (phba->sli_rev == LPFC_SLI_REV4)
3668 		snprintf(fwrevision, FW_REV_STR_SIZE, "%s", vp->rev.opFwName);
3669 	else if (vp->rev.rBit) {
3670 		if (psli->sli_flag & LPFC_SLI_ACTIVE)
3671 			rev = vp->rev.sli2FwRev;
3672 		else
3673 			rev = vp->rev.sli1FwRev;
3674 
3675 		b1 = (rev & 0x0000f000) >> 12;
3676 		b2 = (rev & 0x00000f00) >> 8;
3677 		b3 = (rev & 0x000000c0) >> 6;
3678 		b4 = (rev & 0x00000030) >> 4;
3679 
3680 		switch (b4) {
3681 		case 0:
3682 			c = 'N';
3683 			break;
3684 		case 1:
3685 			c = 'A';
3686 			break;
3687 		case 2:
3688 			c = 'B';
3689 			break;
3690 		case 3:
3691 			c = 'X';
3692 			break;
3693 		default:
3694 			c = 0;
3695 			break;
3696 		}
3697 		b4 = (rev & 0x0000000f);
3698 
3699 		if (psli->sli_flag & LPFC_SLI_ACTIVE)
3700 			fwname = vp->rev.sli2FwName;
3701 		else
3702 			fwname = vp->rev.sli1FwName;
3703 
3704 		for (i = 0; i < 16; i++)
3705 			if (fwname[i] == 0x20)
3706 				fwname[i] = 0;
3707 
3708 		ptr = (uint32_t*)fwname;
3709 
3710 		for (i = 0; i < 3; i++)
3711 			str[i] = be32_to_cpu(*ptr++);
3712 
3713 		if (c == 0) {
3714 			if (flag)
3715 				sprintf(fwrevision, "%d.%d%d (%s)",
3716 					b1, b2, b3, (char *)str);
3717 			else
3718 				sprintf(fwrevision, "%d.%d%d", b1,
3719 					b2, b3);
3720 		} else {
3721 			if (flag)
3722 				sprintf(fwrevision, "%d.%d%d%c%d (%s)",
3723 					b1, b2, b3, c,
3724 					b4, (char *)str);
3725 			else
3726 				sprintf(fwrevision, "%d.%d%d%c%d",
3727 					b1, b2, b3, c, b4);
3728 		}
3729 	} else {
3730 		rev = vp->rev.smFwRev;
3731 
3732 		b1 = (rev & 0xff000000) >> 24;
3733 		b2 = (rev & 0x00f00000) >> 20;
3734 		b3 = (rev & 0x000f0000) >> 16;
3735 		c  = (rev & 0x0000ff00) >> 8;
3736 		b4 = (rev & 0x000000ff);
3737 
3738 		sprintf(fwrevision, "%d.%d%d%c%d", b1, b2, b3, c, b4);
3739 	}
3740 	return;
3741 }
3742