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