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