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