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