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