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