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