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