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