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