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