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