xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_els.c (revision 405db98b)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2021 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  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 /* See Fibre Channel protocol T11 FC-LS for details */
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include <uapi/scsi/fc/fc_fs.h>
35 #include <uapi/scsi/fc/fc_els.h>
36 
37 #include "lpfc_hw4.h"
38 #include "lpfc_hw.h"
39 #include "lpfc_sli.h"
40 #include "lpfc_sli4.h"
41 #include "lpfc_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_scsi.h"
44 #include "lpfc.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_crtn.h"
47 #include "lpfc_vport.h"
48 #include "lpfc_debugfs.h"
49 
50 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
51 			  struct lpfc_iocbq *);
52 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
53 			struct lpfc_iocbq *);
54 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
55 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
56 				struct lpfc_nodelist *ndlp, uint8_t retry);
57 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
58 				  struct lpfc_iocbq *iocb);
59 static void lpfc_cmpl_els_edc(struct lpfc_hba *phba,
60 			      struct lpfc_iocbq *cmdiocb,
61 			      struct lpfc_iocbq *rspiocb);
62 static void lpfc_cmpl_els_uvem(struct lpfc_hba *, struct lpfc_iocbq *,
63 			       struct lpfc_iocbq *);
64 
65 static int lpfc_max_els_tries = 3;
66 
67 static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport);
68 static void lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max);
69 static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid);
70 
71 /**
72  * lpfc_els_chk_latt - Check host link attention event for a vport
73  * @vport: pointer to a host virtual N_Port data structure.
74  *
75  * This routine checks whether there is an outstanding host link
76  * attention event during the discovery process with the @vport. It is done
77  * by reading the HBA's Host Attention (HA) register. If there is any host
78  * link attention events during this @vport's discovery process, the @vport
79  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
80  * be issued if the link state is not already in host link cleared state,
81  * and a return code shall indicate whether the host link attention event
82  * had happened.
83  *
84  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
85  * state in LPFC_VPORT_READY, the request for checking host link attention
86  * event will be ignored and a return code shall indicate no host link
87  * attention event had happened.
88  *
89  * Return codes
90  *   0 - no host link attention event happened
91  *   1 - host link attention event happened
92  **/
93 int
94 lpfc_els_chk_latt(struct lpfc_vport *vport)
95 {
96 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
97 	struct lpfc_hba  *phba = vport->phba;
98 	uint32_t ha_copy;
99 
100 	if (vport->port_state >= LPFC_VPORT_READY ||
101 	    phba->link_state == LPFC_LINK_DOWN ||
102 	    phba->sli_rev > LPFC_SLI_REV3)
103 		return 0;
104 
105 	/* Read the HBA Host Attention Register */
106 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
107 		return 1;
108 
109 	if (!(ha_copy & HA_LATT))
110 		return 0;
111 
112 	/* Pending Link Event during Discovery */
113 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
114 			 "0237 Pending Link Event during "
115 			 "Discovery: State x%x\n",
116 			 phba->pport->port_state);
117 
118 	/* CLEAR_LA should re-enable link attention events and
119 	 * we should then immediately take a LATT event. The
120 	 * LATT processing should call lpfc_linkdown() which
121 	 * will cleanup any left over in-progress discovery
122 	 * events.
123 	 */
124 	spin_lock_irq(shost->host_lock);
125 	vport->fc_flag |= FC_ABORT_DISCOVERY;
126 	spin_unlock_irq(shost->host_lock);
127 
128 	if (phba->link_state != LPFC_CLEAR_LA)
129 		lpfc_issue_clear_la(phba, vport);
130 
131 	return 1;
132 }
133 
134 /**
135  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
136  * @vport: pointer to a host virtual N_Port data structure.
137  * @expectRsp: flag indicating whether response is expected.
138  * @cmdSize: size of the ELS command.
139  * @retry: number of retries to the command IOCB when it fails.
140  * @ndlp: pointer to a node-list data structure.
141  * @did: destination identifier.
142  * @elscmd: the ELS command code.
143  *
144  * This routine is used for allocating a lpfc-IOCB data structure from
145  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
146  * passed into the routine for discovery state machine to issue an Extended
147  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
148  * and preparation routine that is used by all the discovery state machine
149  * routines and the ELS command-specific fields will be later set up by
150  * the individual discovery machine routines after calling this routine
151  * allocating and preparing a generic IOCB data structure. It fills in the
152  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
153  * payload and response payload (if expected). The reference count on the
154  * ndlp is incremented by 1 and the reference to the ndlp is put into
155  * context1 of the IOCB data structure for this IOCB to hold the ndlp
156  * reference for the command's callback function to access later.
157  *
158  * Return code
159  *   Pointer to the newly allocated/prepared els iocb data structure
160  *   NULL - when els iocb data structure allocation/preparation failed
161  **/
162 struct lpfc_iocbq *
163 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
164 		   uint16_t cmdSize, uint8_t retry,
165 		   struct lpfc_nodelist *ndlp, uint32_t did,
166 		   uint32_t elscmd)
167 {
168 	struct lpfc_hba  *phba = vport->phba;
169 	struct lpfc_iocbq *elsiocb;
170 	struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
171 	struct ulp_bde64 *bpl;
172 	IOCB_t *icmd;
173 
174 
175 	if (!lpfc_is_link_up(phba))
176 		return NULL;
177 
178 	/* Allocate buffer for  command iocb */
179 	elsiocb = lpfc_sli_get_iocbq(phba);
180 
181 	if (elsiocb == NULL)
182 		return NULL;
183 
184 	/*
185 	 * If this command is for fabric controller and HBA running
186 	 * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
187 	 */
188 	if ((did == Fabric_DID) &&
189 		(phba->hba_flag & HBA_FIP_SUPPORT) &&
190 		((elscmd == ELS_CMD_FLOGI) ||
191 		 (elscmd == ELS_CMD_FDISC) ||
192 		 (elscmd == ELS_CMD_LOGO)))
193 		switch (elscmd) {
194 		case ELS_CMD_FLOGI:
195 		elsiocb->iocb_flag |=
196 			((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
197 					& LPFC_FIP_ELS_ID_MASK);
198 		break;
199 		case ELS_CMD_FDISC:
200 		elsiocb->iocb_flag |=
201 			((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
202 					& LPFC_FIP_ELS_ID_MASK);
203 		break;
204 		case ELS_CMD_LOGO:
205 		elsiocb->iocb_flag |=
206 			((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
207 					& LPFC_FIP_ELS_ID_MASK);
208 		break;
209 		}
210 	else
211 		elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
212 
213 	icmd = &elsiocb->iocb;
214 
215 	/* fill in BDEs for command */
216 	/* Allocate buffer for command payload */
217 	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
218 	if (pcmd)
219 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
220 	if (!pcmd || !pcmd->virt)
221 		goto els_iocb_free_pcmb_exit;
222 
223 	INIT_LIST_HEAD(&pcmd->list);
224 
225 	/* Allocate buffer for response payload */
226 	if (expectRsp) {
227 		prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
228 		if (prsp)
229 			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
230 						     &prsp->phys);
231 		if (!prsp || !prsp->virt)
232 			goto els_iocb_free_prsp_exit;
233 		INIT_LIST_HEAD(&prsp->list);
234 	} else
235 		prsp = NULL;
236 
237 	/* Allocate buffer for Buffer ptr list */
238 	pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
239 	if (pbuflist)
240 		pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
241 						 &pbuflist->phys);
242 	if (!pbuflist || !pbuflist->virt)
243 		goto els_iocb_free_pbuf_exit;
244 
245 	INIT_LIST_HEAD(&pbuflist->list);
246 
247 	if (expectRsp) {
248 		icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
249 		icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
250 		icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
251 		icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
252 
253 		icmd->un.elsreq64.remoteID = did;		/* DID */
254 		icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
255 		if (elscmd == ELS_CMD_FLOGI)
256 			icmd->ulpTimeout = FF_DEF_RATOV * 2;
257 		else if (elscmd == ELS_CMD_LOGO)
258 			icmd->ulpTimeout = phba->fc_ratov;
259 		else
260 			icmd->ulpTimeout = phba->fc_ratov * 2;
261 	} else {
262 		icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
263 		icmd->un.xseq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
264 		icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
265 		icmd->un.xseq64.bdl.bdeSize = sizeof(struct ulp_bde64);
266 		icmd->un.xseq64.xmit_els_remoteID = did;	/* DID */
267 		icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
268 	}
269 	icmd->ulpBdeCount = 1;
270 	icmd->ulpLe = 1;
271 	icmd->ulpClass = CLASS3;
272 
273 	/*
274 	 * If we have NPIV enabled, we want to send ELS traffic by VPI.
275 	 * For SLI4, since the driver controls VPIs we also want to include
276 	 * all ELS pt2pt protocol traffic as well.
277 	 */
278 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
279 		((phba->sli_rev == LPFC_SLI_REV4) &&
280 		    (vport->fc_flag & FC_PT2PT))) {
281 
282 		if (expectRsp) {
283 			icmd->un.elsreq64.myID = vport->fc_myDID;
284 
285 			/* For ELS_REQUEST64_CR, use the VPI by default */
286 			icmd->ulpContext = phba->vpi_ids[vport->vpi];
287 		}
288 
289 		icmd->ulpCt_h = 0;
290 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
291 		if (elscmd == ELS_CMD_ECHO)
292 			icmd->ulpCt_l = 0; /* context = invalid RPI */
293 		else
294 			icmd->ulpCt_l = 1; /* context = VPI */
295 	}
296 
297 	bpl = (struct ulp_bde64 *) pbuflist->virt;
298 	bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
299 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
300 	bpl->tus.f.bdeSize = cmdSize;
301 	bpl->tus.f.bdeFlags = 0;
302 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
303 
304 	if (expectRsp) {
305 		bpl++;
306 		bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
307 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
308 		bpl->tus.f.bdeSize = FCELSSIZE;
309 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
310 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
311 	}
312 
313 	elsiocb->context2 = pcmd;
314 	elsiocb->context3 = pbuflist;
315 	elsiocb->retry = retry;
316 	elsiocb->vport = vport;
317 	elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
318 
319 	if (prsp) {
320 		list_add(&prsp->list, &pcmd->list);
321 	}
322 	if (expectRsp) {
323 		/* Xmit ELS command <elsCmd> to remote NPORT <did> */
324 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
325 				 "0116 Xmit ELS command x%x to remote "
326 				 "NPORT x%x I/O tag: x%x, port state:x%x "
327 				 "rpi x%x fc_flag:x%x nlp_flag:x%x vport:x%p\n",
328 				 elscmd, did, elsiocb->iotag,
329 				 vport->port_state, ndlp->nlp_rpi,
330 				 vport->fc_flag, ndlp->nlp_flag, vport);
331 	} else {
332 		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
333 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
334 				 "0117 Xmit ELS response x%x to remote "
335 				 "NPORT x%x I/O tag: x%x, size: x%x "
336 				 "port_state x%x  rpi x%x fc_flag x%x\n",
337 				 elscmd, ndlp->nlp_DID, elsiocb->iotag,
338 				 cmdSize, vport->port_state,
339 				 ndlp->nlp_rpi, vport->fc_flag);
340 	}
341 	return elsiocb;
342 
343 els_iocb_free_pbuf_exit:
344 	if (expectRsp)
345 		lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
346 	kfree(pbuflist);
347 
348 els_iocb_free_prsp_exit:
349 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
350 	kfree(prsp);
351 
352 els_iocb_free_pcmb_exit:
353 	kfree(pcmd);
354 	lpfc_sli_release_iocbq(phba, elsiocb);
355 	return NULL;
356 }
357 
358 /**
359  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
360  * @vport: pointer to a host virtual N_Port data structure.
361  *
362  * This routine issues a fabric registration login for a @vport. An
363  * active ndlp node with Fabric_DID must already exist for this @vport.
364  * The routine invokes two mailbox commands to carry out fabric registration
365  * login through the HBA firmware: the first mailbox command requests the
366  * HBA to perform link configuration for the @vport; and the second mailbox
367  * command requests the HBA to perform the actual fabric registration login
368  * with the @vport.
369  *
370  * Return code
371  *   0 - successfully issued fabric registration login for @vport
372  *   -ENXIO -- failed to issue fabric registration login for @vport
373  **/
374 int
375 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
376 {
377 	struct lpfc_hba  *phba = vport->phba;
378 	LPFC_MBOXQ_t *mbox;
379 	struct lpfc_dmabuf *mp;
380 	struct lpfc_nodelist *ndlp;
381 	struct serv_parm *sp;
382 	int rc;
383 	int err = 0;
384 
385 	sp = &phba->fc_fabparam;
386 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
387 	if (!ndlp) {
388 		err = 1;
389 		goto fail;
390 	}
391 
392 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
393 	if (!mbox) {
394 		err = 2;
395 		goto fail;
396 	}
397 
398 	vport->port_state = LPFC_FABRIC_CFG_LINK;
399 	lpfc_config_link(phba, mbox);
400 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
401 	mbox->vport = vport;
402 
403 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
404 	if (rc == MBX_NOT_FINISHED) {
405 		err = 3;
406 		goto fail_free_mbox;
407 	}
408 
409 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
410 	if (!mbox) {
411 		err = 4;
412 		goto fail;
413 	}
414 	rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
415 			  ndlp->nlp_rpi);
416 	if (rc) {
417 		err = 5;
418 		goto fail_free_mbox;
419 	}
420 
421 	mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
422 	mbox->vport = vport;
423 	/* increment the reference count on ndlp to hold reference
424 	 * for the callback routine.
425 	 */
426 	mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
427 	if (!mbox->ctx_ndlp) {
428 		err = 6;
429 		goto fail_no_ndlp;
430 	}
431 
432 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
433 	if (rc == MBX_NOT_FINISHED) {
434 		err = 7;
435 		goto fail_issue_reg_login;
436 	}
437 
438 	return 0;
439 
440 fail_issue_reg_login:
441 	/* decrement the reference count on ndlp just incremented
442 	 * for the failed mbox command.
443 	 */
444 	lpfc_nlp_put(ndlp);
445 fail_no_ndlp:
446 	mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
447 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
448 	kfree(mp);
449 fail_free_mbox:
450 	mempool_free(mbox, phba->mbox_mem_pool);
451 
452 fail:
453 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
454 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
455 			 "0249 Cannot issue Register Fabric login: Err %d\n",
456 			 err);
457 	return -ENXIO;
458 }
459 
460 /**
461  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
462  * @vport: pointer to a host virtual N_Port data structure.
463  *
464  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
465  * the @vport. This mailbox command is necessary for SLI4 port only.
466  *
467  * Return code
468  *   0 - successfully issued REG_VFI for @vport
469  *   A failure code otherwise.
470  **/
471 int
472 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
473 {
474 	struct lpfc_hba  *phba = vport->phba;
475 	LPFC_MBOXQ_t *mboxq = NULL;
476 	struct lpfc_nodelist *ndlp;
477 	struct lpfc_dmabuf *dmabuf = NULL;
478 	int rc = 0;
479 
480 	/* move forward in case of SLI4 FC port loopback test and pt2pt mode */
481 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
482 	    !(phba->link_flag & LS_LOOPBACK_MODE) &&
483 	    !(vport->fc_flag & FC_PT2PT)) {
484 		ndlp = lpfc_findnode_did(vport, Fabric_DID);
485 		if (!ndlp) {
486 			rc = -ENODEV;
487 			goto fail;
488 		}
489 	}
490 
491 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
492 	if (!mboxq) {
493 		rc = -ENOMEM;
494 		goto fail;
495 	}
496 
497 	/* Supply CSP's only if we are fabric connect or pt-to-pt connect */
498 	if ((vport->fc_flag & FC_FABRIC) || (vport->fc_flag & FC_PT2PT)) {
499 		dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
500 		if (!dmabuf) {
501 			rc = -ENOMEM;
502 			goto fail;
503 		}
504 		dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
505 		if (!dmabuf->virt) {
506 			rc = -ENOMEM;
507 			goto fail;
508 		}
509 		memcpy(dmabuf->virt, &phba->fc_fabparam,
510 		       sizeof(struct serv_parm));
511 	}
512 
513 	vport->port_state = LPFC_FABRIC_CFG_LINK;
514 	if (dmabuf)
515 		lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
516 	else
517 		lpfc_reg_vfi(mboxq, vport, 0);
518 
519 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
520 	mboxq->vport = vport;
521 	mboxq->ctx_buf = dmabuf;
522 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
523 	if (rc == MBX_NOT_FINISHED) {
524 		rc = -ENXIO;
525 		goto fail;
526 	}
527 	return 0;
528 
529 fail:
530 	if (mboxq)
531 		mempool_free(mboxq, phba->mbox_mem_pool);
532 	if (dmabuf) {
533 		if (dmabuf->virt)
534 			lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
535 		kfree(dmabuf);
536 	}
537 
538 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
539 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
540 			 "0289 Issue Register VFI failed: Err %d\n", rc);
541 	return rc;
542 }
543 
544 /**
545  * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
546  * @vport: pointer to a host virtual N_Port data structure.
547  *
548  * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
549  * the @vport. This mailbox command is necessary for SLI4 port only.
550  *
551  * Return code
552  *   0 - successfully issued REG_VFI for @vport
553  *   A failure code otherwise.
554  **/
555 int
556 lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
557 {
558 	struct lpfc_hba *phba = vport->phba;
559 	struct Scsi_Host *shost;
560 	LPFC_MBOXQ_t *mboxq;
561 	int rc;
562 
563 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
564 	if (!mboxq) {
565 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
566 				"2556 UNREG_VFI mbox allocation failed"
567 				"HBA state x%x\n", phba->pport->port_state);
568 		return -ENOMEM;
569 	}
570 
571 	lpfc_unreg_vfi(mboxq, vport);
572 	mboxq->vport = vport;
573 	mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
574 
575 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
576 	if (rc == MBX_NOT_FINISHED) {
577 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
578 				"2557 UNREG_VFI issue mbox failed rc x%x "
579 				"HBA state x%x\n",
580 				rc, phba->pport->port_state);
581 		mempool_free(mboxq, phba->mbox_mem_pool);
582 		return -EIO;
583 	}
584 
585 	shost = lpfc_shost_from_vport(vport);
586 	spin_lock_irq(shost->host_lock);
587 	vport->fc_flag &= ~FC_VFI_REGISTERED;
588 	spin_unlock_irq(shost->host_lock);
589 	return 0;
590 }
591 
592 /**
593  * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
594  * @vport: pointer to a host virtual N_Port data structure.
595  * @sp: pointer to service parameter data structure.
596  *
597  * This routine is called from FLOGI/FDISC completion handler functions.
598  * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
599  * node nodename is changed in the completion service parameter else return
600  * 0. This function also set flag in the vport data structure to delay
601  * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
602  * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
603  * node nodename is changed in the completion service parameter.
604  *
605  * Return code
606  *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
607  *   1 - FCID or Fabric Nodename or Fabric portname is changed.
608  *
609  **/
610 static uint8_t
611 lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
612 		struct serv_parm *sp)
613 {
614 	struct lpfc_hba *phba = vport->phba;
615 	uint8_t fabric_param_changed = 0;
616 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
617 
618 	if ((vport->fc_prevDID != vport->fc_myDID) ||
619 		memcmp(&vport->fabric_portname, &sp->portName,
620 			sizeof(struct lpfc_name)) ||
621 		memcmp(&vport->fabric_nodename, &sp->nodeName,
622 			sizeof(struct lpfc_name)) ||
623 		(vport->vport_flag & FAWWPN_PARAM_CHG)) {
624 		fabric_param_changed = 1;
625 		vport->vport_flag &= ~FAWWPN_PARAM_CHG;
626 	}
627 	/*
628 	 * Word 1 Bit 31 in common service parameter is overloaded.
629 	 * Word 1 Bit 31 in FLOGI request is multiple NPort request
630 	 * Word 1 Bit 31 in FLOGI response is clean address bit
631 	 *
632 	 * If fabric parameter is changed and clean address bit is
633 	 * cleared delay nport discovery if
634 	 * - vport->fc_prevDID != 0 (not initial discovery) OR
635 	 * - lpfc_delay_discovery module parameter is set.
636 	 */
637 	if (fabric_param_changed && !sp->cmn.clean_address_bit &&
638 	    (vport->fc_prevDID || phba->cfg_delay_discovery)) {
639 		spin_lock_irq(shost->host_lock);
640 		vport->fc_flag |= FC_DISC_DELAYED;
641 		spin_unlock_irq(shost->host_lock);
642 	}
643 
644 	return fabric_param_changed;
645 }
646 
647 
648 /**
649  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
650  * @vport: pointer to a host virtual N_Port data structure.
651  * @ndlp: pointer to a node-list data structure.
652  * @sp: pointer to service parameter data structure.
653  * @irsp: pointer to the IOCB within the lpfc response IOCB.
654  *
655  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
656  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
657  * port in a fabric topology. It properly sets up the parameters to the @ndlp
658  * from the IOCB response. It also check the newly assigned N_Port ID to the
659  * @vport against the previously assigned N_Port ID. If it is different from
660  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
661  * is invoked on all the remaining nodes with the @vport to unregister the
662  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
663  * is invoked to register login to the fabric.
664  *
665  * Return code
666  *   0 - Success (currently, always return 0)
667  **/
668 static int
669 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
670 			   struct serv_parm *sp, IOCB_t *irsp)
671 {
672 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
673 	struct lpfc_hba  *phba = vport->phba;
674 	struct lpfc_nodelist *np;
675 	struct lpfc_nodelist *next_np;
676 	uint8_t fabric_param_changed;
677 
678 	spin_lock_irq(shost->host_lock);
679 	vport->fc_flag |= FC_FABRIC;
680 	spin_unlock_irq(shost->host_lock);
681 
682 	phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
683 	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
684 		phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
685 
686 	phba->fc_edtovResol = sp->cmn.edtovResolution;
687 	phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
688 
689 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
690 		spin_lock_irq(shost->host_lock);
691 		vport->fc_flag |= FC_PUBLIC_LOOP;
692 		spin_unlock_irq(shost->host_lock);
693 	}
694 
695 	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
696 	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
697 	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
698 	ndlp->nlp_class_sup = 0;
699 	if (sp->cls1.classValid)
700 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
701 	if (sp->cls2.classValid)
702 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
703 	if (sp->cls3.classValid)
704 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
705 	if (sp->cls4.classValid)
706 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
707 	ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
708 				sp->cmn.bbRcvSizeLsb;
709 
710 	fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
711 	if (fabric_param_changed) {
712 		/* Reset FDMI attribute masks based on config parameter */
713 		if (phba->cfg_enable_SmartSAN ||
714 		    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
715 			/* Setup appropriate attribute masks */
716 			vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
717 			if (phba->cfg_enable_SmartSAN)
718 				vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
719 			else
720 				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
721 		} else {
722 			vport->fdmi_hba_mask = 0;
723 			vport->fdmi_port_mask = 0;
724 		}
725 
726 	}
727 	memcpy(&vport->fabric_portname, &sp->portName,
728 			sizeof(struct lpfc_name));
729 	memcpy(&vport->fabric_nodename, &sp->nodeName,
730 			sizeof(struct lpfc_name));
731 	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
732 
733 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
734 		if (sp->cmn.response_multiple_NPort) {
735 			lpfc_printf_vlog(vport, KERN_WARNING,
736 					 LOG_ELS | LOG_VPORT,
737 					 "1816 FLOGI NPIV supported, "
738 					 "response data 0x%x\n",
739 					 sp->cmn.response_multiple_NPort);
740 			spin_lock_irq(&phba->hbalock);
741 			phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
742 			spin_unlock_irq(&phba->hbalock);
743 		} else {
744 			/* Because we asked f/w for NPIV it still expects us
745 			to call reg_vnpid at least for the physical host */
746 			lpfc_printf_vlog(vport, KERN_WARNING,
747 					 LOG_ELS | LOG_VPORT,
748 					 "1817 Fabric does not support NPIV "
749 					 "- configuring single port mode.\n");
750 			spin_lock_irq(&phba->hbalock);
751 			phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
752 			spin_unlock_irq(&phba->hbalock);
753 		}
754 	}
755 
756 	/*
757 	 * For FC we need to do some special processing because of the SLI
758 	 * Port's default settings of the Common Service Parameters.
759 	 */
760 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
761 	    (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
762 		/* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
763 		if (fabric_param_changed)
764 			lpfc_unregister_fcf_prep(phba);
765 
766 		/* This should just update the VFI CSPs*/
767 		if (vport->fc_flag & FC_VFI_REGISTERED)
768 			lpfc_issue_reg_vfi(vport);
769 	}
770 
771 	if (fabric_param_changed &&
772 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
773 
774 		/* If our NportID changed, we need to ensure all
775 		 * remaining NPORTs get unreg_login'ed.
776 		 */
777 		list_for_each_entry_safe(np, next_np,
778 					&vport->fc_nodes, nlp_listp) {
779 			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
780 				   !(np->nlp_flag & NLP_NPR_ADISC))
781 				continue;
782 			spin_lock_irq(&np->lock);
783 			np->nlp_flag &= ~NLP_NPR_ADISC;
784 			spin_unlock_irq(&np->lock);
785 			lpfc_unreg_rpi(vport, np);
786 		}
787 		lpfc_cleanup_pending_mbox(vport);
788 
789 		if (phba->sli_rev == LPFC_SLI_REV4) {
790 			lpfc_sli4_unreg_all_rpis(vport);
791 			lpfc_mbx_unreg_vpi(vport);
792 			spin_lock_irq(shost->host_lock);
793 			vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
794 			spin_unlock_irq(shost->host_lock);
795 		}
796 
797 		/*
798 		 * For SLI3 and SLI4, the VPI needs to be reregistered in
799 		 * response to this fabric parameter change event.
800 		 */
801 		spin_lock_irq(shost->host_lock);
802 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
803 		spin_unlock_irq(shost->host_lock);
804 	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
805 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
806 			/*
807 			 * Driver needs to re-reg VPI in order for f/w
808 			 * to update the MAC address.
809 			 */
810 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
811 			lpfc_register_new_vport(phba, vport, ndlp);
812 			return 0;
813 	}
814 
815 	if (phba->sli_rev < LPFC_SLI_REV4) {
816 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
817 		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
818 		    vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
819 			lpfc_register_new_vport(phba, vport, ndlp);
820 		else
821 			lpfc_issue_fabric_reglogin(vport);
822 	} else {
823 		ndlp->nlp_type |= NLP_FABRIC;
824 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
825 		if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
826 			(vport->vpi_state & LPFC_VPI_REGISTERED)) {
827 			lpfc_start_fdiscs(phba);
828 			lpfc_do_scr_ns_plogi(phba, vport);
829 		} else if (vport->fc_flag & FC_VFI_REGISTERED)
830 			lpfc_issue_init_vpi(vport);
831 		else {
832 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
833 					"3135 Need register VFI: (x%x/%x)\n",
834 					vport->fc_prevDID, vport->fc_myDID);
835 			lpfc_issue_reg_vfi(vport);
836 		}
837 	}
838 	return 0;
839 }
840 
841 /**
842  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
843  * @vport: pointer to a host virtual N_Port data structure.
844  * @ndlp: pointer to a node-list data structure.
845  * @sp: pointer to service parameter data structure.
846  *
847  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
848  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
849  * in a point-to-point topology. First, the @vport's N_Port Name is compared
850  * with the received N_Port Name: if the @vport's N_Port Name is greater than
851  * the received N_Port Name lexicographically, this node shall assign local
852  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
853  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
854  * this node shall just wait for the remote node to issue PLOGI and assign
855  * N_Port IDs.
856  *
857  * Return code
858  *   0 - Success
859  *   -ENXIO - Fail
860  **/
861 static int
862 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
863 			  struct serv_parm *sp)
864 {
865 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
866 	struct lpfc_hba  *phba = vport->phba;
867 	LPFC_MBOXQ_t *mbox;
868 	int rc;
869 
870 	spin_lock_irq(shost->host_lock);
871 	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
872 	vport->fc_flag |= FC_PT2PT;
873 	spin_unlock_irq(shost->host_lock);
874 
875 	/* If we are pt2pt with another NPort, force NPIV off! */
876 	phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
877 
878 	/* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
879 	if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
880 		lpfc_unregister_fcf_prep(phba);
881 
882 		spin_lock_irq(shost->host_lock);
883 		vport->fc_flag &= ~FC_VFI_REGISTERED;
884 		spin_unlock_irq(shost->host_lock);
885 		phba->fc_topology_changed = 0;
886 	}
887 
888 	rc = memcmp(&vport->fc_portname, &sp->portName,
889 		    sizeof(vport->fc_portname));
890 
891 	if (rc >= 0) {
892 		/* This side will initiate the PLOGI */
893 		spin_lock_irq(shost->host_lock);
894 		vport->fc_flag |= FC_PT2PT_PLOGI;
895 		spin_unlock_irq(shost->host_lock);
896 
897 		/*
898 		 * N_Port ID cannot be 0, set our Id to LocalID
899 		 * the other side will be RemoteID.
900 		 */
901 
902 		/* not equal */
903 		if (rc)
904 			vport->fc_myDID = PT2PT_LocalID;
905 
906 		/* Decrement ndlp reference count indicating that ndlp can be
907 		 * safely released when other references to it are done.
908 		 */
909 		lpfc_nlp_put(ndlp);
910 
911 		ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
912 		if (!ndlp) {
913 			/*
914 			 * Cannot find existing Fabric ndlp, so allocate a
915 			 * new one
916 			 */
917 			ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
918 			if (!ndlp)
919 				goto fail;
920 		}
921 
922 		memcpy(&ndlp->nlp_portname, &sp->portName,
923 		       sizeof(struct lpfc_name));
924 		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
925 		       sizeof(struct lpfc_name));
926 		/* Set state will put ndlp onto node list if not already done */
927 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
928 		spin_lock_irq(&ndlp->lock);
929 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
930 		spin_unlock_irq(&ndlp->lock);
931 
932 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
933 		if (!mbox)
934 			goto fail;
935 
936 		lpfc_config_link(phba, mbox);
937 
938 		mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
939 		mbox->vport = vport;
940 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
941 		if (rc == MBX_NOT_FINISHED) {
942 			mempool_free(mbox, phba->mbox_mem_pool);
943 			goto fail;
944 		}
945 	} else {
946 		/* This side will wait for the PLOGI, decrement ndlp reference
947 		 * count indicating that ndlp can be released when other
948 		 * references to it are done.
949 		 */
950 		lpfc_nlp_put(ndlp);
951 
952 		/* Start discovery - this should just do CLEAR_LA */
953 		lpfc_disc_start(vport);
954 	}
955 
956 	return 0;
957 fail:
958 	return -ENXIO;
959 }
960 
961 /**
962  * lpfc_cmpl_els_flogi - Completion callback function for flogi
963  * @phba: pointer to lpfc hba data structure.
964  * @cmdiocb: pointer to lpfc command iocb data structure.
965  * @rspiocb: pointer to lpfc response iocb data structure.
966  *
967  * This routine is the top-level completion callback function for issuing
968  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
969  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
970  * retry has been made (either immediately or delayed with lpfc_els_retry()
971  * returning 1), the command IOCB will be released and function returned.
972  * If the retry attempt has been given up (possibly reach the maximum
973  * number of retries), one additional decrement of ndlp reference shall be
974  * invoked before going out after releasing the command IOCB. This will
975  * actually release the remote node (Note, lpfc_els_free_iocb() will also
976  * invoke one decrement of ndlp reference count). If no error reported in
977  * the IOCB status, the command Port ID field is used to determine whether
978  * this is a point-to-point topology or a fabric topology: if the Port ID
979  * field is assigned, it is a fabric topology; otherwise, it is a
980  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
981  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
982  * specific topology completion conditions.
983  **/
984 static void
985 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
986 		    struct lpfc_iocbq *rspiocb)
987 {
988 	struct lpfc_vport *vport = cmdiocb->vport;
989 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
990 	IOCB_t *irsp = &rspiocb->iocb;
991 	struct lpfc_nodelist *ndlp = cmdiocb->context1;
992 	struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
993 	struct serv_parm *sp;
994 	uint16_t fcf_index;
995 	int rc;
996 
997 	/* Check to see if link went down during discovery */
998 	if (lpfc_els_chk_latt(vport)) {
999 		/* One additional decrement on node reference count to
1000 		 * trigger the release of the node
1001 		 */
1002 		lpfc_nlp_put(ndlp);
1003 		goto out;
1004 	}
1005 
1006 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1007 		"FLOGI cmpl:      status:x%x/x%x state:x%x",
1008 		irsp->ulpStatus, irsp->un.ulpWord[4],
1009 		vport->port_state);
1010 
1011 	if (irsp->ulpStatus) {
1012 		/*
1013 		 * In case of FIP mode, perform roundrobin FCF failover
1014 		 * due to new FCF discovery
1015 		 */
1016 		if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
1017 		    (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
1018 			if (phba->link_state < LPFC_LINK_UP)
1019 				goto stop_rr_fcf_flogi;
1020 			if ((phba->fcoe_cvl_eventtag_attn ==
1021 			     phba->fcoe_cvl_eventtag) &&
1022 			    (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1023 			    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1024 			    IOERR_SLI_ABORTED))
1025 				goto stop_rr_fcf_flogi;
1026 			else
1027 				phba->fcoe_cvl_eventtag_attn =
1028 					phba->fcoe_cvl_eventtag;
1029 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1030 					"2611 FLOGI failed on FCF (x%x), "
1031 					"status:x%x/x%x, tmo:x%x, perform "
1032 					"roundrobin FCF failover\n",
1033 					phba->fcf.current_rec.fcf_indx,
1034 					irsp->ulpStatus, irsp->un.ulpWord[4],
1035 					irsp->ulpTimeout);
1036 			lpfc_sli4_set_fcf_flogi_fail(phba,
1037 					phba->fcf.current_rec.fcf_indx);
1038 			fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
1039 			rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
1040 			if (rc)
1041 				goto out;
1042 		}
1043 
1044 stop_rr_fcf_flogi:
1045 		/* FLOGI failure */
1046 		if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1047 		      ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1048 					IOERR_LOOP_OPEN_FAILURE)))
1049 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1050 					 "2858 FLOGI failure Status:x%x/x%x TMO"
1051 					 ":x%x Data x%x x%x\n",
1052 					 irsp->ulpStatus, irsp->un.ulpWord[4],
1053 					 irsp->ulpTimeout, phba->hba_flag,
1054 					 phba->fcf.fcf_flag);
1055 
1056 		/* Check for retry */
1057 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1058 			goto out;
1059 
1060 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_TRACE_EVENT,
1061 				 "0150 FLOGI failure Status:x%x/x%x "
1062 				 "xri x%x TMO:x%x refcnt %d\n",
1063 				 irsp->ulpStatus, irsp->un.ulpWord[4],
1064 				 cmdiocb->sli4_xritag, irsp->ulpTimeout,
1065 				 kref_read(&ndlp->kref));
1066 
1067 		/* If this is not a loop open failure, bail out */
1068 		if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1069 		      ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1070 					IOERR_LOOP_OPEN_FAILURE)))
1071 			goto flogifail;
1072 
1073 		/* FLOGI failed, so there is no fabric */
1074 		spin_lock_irq(shost->host_lock);
1075 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1076 		spin_unlock_irq(shost->host_lock);
1077 
1078 		/* If private loop, then allow max outstanding els to be
1079 		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1080 		 * alpa map would take too long otherwise.
1081 		 */
1082 		if (phba->alpa_map[0] == 0)
1083 			vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
1084 		if ((phba->sli_rev == LPFC_SLI_REV4) &&
1085 		    (!(vport->fc_flag & FC_VFI_REGISTERED) ||
1086 		     (vport->fc_prevDID != vport->fc_myDID) ||
1087 			phba->fc_topology_changed)) {
1088 			if (vport->fc_flag & FC_VFI_REGISTERED) {
1089 				if (phba->fc_topology_changed) {
1090 					lpfc_unregister_fcf_prep(phba);
1091 					spin_lock_irq(shost->host_lock);
1092 					vport->fc_flag &= ~FC_VFI_REGISTERED;
1093 					spin_unlock_irq(shost->host_lock);
1094 					phba->fc_topology_changed = 0;
1095 				} else {
1096 					lpfc_sli4_unreg_all_rpis(vport);
1097 				}
1098 			}
1099 
1100 			/* Do not register VFI if the driver aborted FLOGI */
1101 			if (!lpfc_error_lost_link(irsp))
1102 				lpfc_issue_reg_vfi(vport);
1103 
1104 			lpfc_nlp_put(ndlp);
1105 			goto out;
1106 		}
1107 		goto flogifail;
1108 	}
1109 	spin_lock_irq(shost->host_lock);
1110 	vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
1111 	vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
1112 	spin_unlock_irq(shost->host_lock);
1113 
1114 	/*
1115 	 * The FLogI succeeded.  Sync the data for the CPU before
1116 	 * accessing it.
1117 	 */
1118 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1119 	if (!prsp)
1120 		goto out;
1121 	sp = prsp->virt + sizeof(uint32_t);
1122 
1123 	/* FLOGI completes successfully */
1124 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1125 			 "0101 FLOGI completes successfully, I/O tag:x%x, "
1126 			 "xri x%x Data: x%x x%x x%x x%x x%x x%x x%x %d\n",
1127 			 cmdiocb->iotag, cmdiocb->sli4_xritag,
1128 			 irsp->un.ulpWord[4], sp->cmn.e_d_tov,
1129 			 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1130 			 vport->port_state, vport->fc_flag,
1131 			 sp->cmn.priority_tagging, kref_read(&ndlp->kref));
1132 
1133 	if (sp->cmn.priority_tagging)
1134 		vport->vmid_flag |= LPFC_VMID_ISSUE_QFPA;
1135 
1136 	if (vport->port_state == LPFC_FLOGI) {
1137 		/*
1138 		 * If Common Service Parameters indicate Nport
1139 		 * we are point to point, if Fport we are Fabric.
1140 		 */
1141 		if (sp->cmn.fPort)
1142 			rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
1143 		else if (!(phba->hba_flag & HBA_FCOE_MODE))
1144 			rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1145 		else {
1146 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1147 				"2831 FLOGI response with cleared Fabric "
1148 				"bit fcf_index 0x%x "
1149 				"Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1150 				"Fabric Name "
1151 				"%02x%02x%02x%02x%02x%02x%02x%02x\n",
1152 				phba->fcf.current_rec.fcf_indx,
1153 				phba->fcf.current_rec.switch_name[0],
1154 				phba->fcf.current_rec.switch_name[1],
1155 				phba->fcf.current_rec.switch_name[2],
1156 				phba->fcf.current_rec.switch_name[3],
1157 				phba->fcf.current_rec.switch_name[4],
1158 				phba->fcf.current_rec.switch_name[5],
1159 				phba->fcf.current_rec.switch_name[6],
1160 				phba->fcf.current_rec.switch_name[7],
1161 				phba->fcf.current_rec.fabric_name[0],
1162 				phba->fcf.current_rec.fabric_name[1],
1163 				phba->fcf.current_rec.fabric_name[2],
1164 				phba->fcf.current_rec.fabric_name[3],
1165 				phba->fcf.current_rec.fabric_name[4],
1166 				phba->fcf.current_rec.fabric_name[5],
1167 				phba->fcf.current_rec.fabric_name[6],
1168 				phba->fcf.current_rec.fabric_name[7]);
1169 
1170 			lpfc_nlp_put(ndlp);
1171 			spin_lock_irq(&phba->hbalock);
1172 			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1173 			phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1174 			spin_unlock_irq(&phba->hbalock);
1175 			phba->fcf.fcf_redisc_attempted = 0; /* reset */
1176 			goto out;
1177 		}
1178 		if (!rc) {
1179 			/* Mark the FCF discovery process done */
1180 			if (phba->hba_flag & HBA_FIP_SUPPORT)
1181 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1182 						LOG_ELS,
1183 						"2769 FLOGI to FCF (x%x) "
1184 						"completed successfully\n",
1185 						phba->fcf.current_rec.fcf_indx);
1186 			spin_lock_irq(&phba->hbalock);
1187 			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1188 			phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1189 			spin_unlock_irq(&phba->hbalock);
1190 			phba->fcf.fcf_redisc_attempted = 0; /* reset */
1191 			goto out;
1192 		}
1193 	} else if (vport->port_state > LPFC_FLOGI &&
1194 		   vport->fc_flag & FC_PT2PT) {
1195 		/*
1196 		 * In a p2p topology, it is possible that discovery has
1197 		 * already progressed, and this completion can be ignored.
1198 		 * Recheck the indicated topology.
1199 		 */
1200 		if (!sp->cmn.fPort)
1201 			goto out;
1202 	}
1203 
1204 flogifail:
1205 	spin_lock_irq(&phba->hbalock);
1206 	phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1207 	spin_unlock_irq(&phba->hbalock);
1208 
1209 	if (!lpfc_error_lost_link(irsp)) {
1210 		/* FLOGI failed, so just use loop map to make discovery list */
1211 		lpfc_disc_list_loopmap(vport);
1212 
1213 		/* Start discovery */
1214 		lpfc_disc_start(vport);
1215 	} else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1216 			(((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1217 			 IOERR_SLI_ABORTED) &&
1218 			((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1219 			 IOERR_SLI_DOWN))) &&
1220 			(phba->link_state != LPFC_CLEAR_LA)) {
1221 		/* If FLOGI failed enable link interrupt. */
1222 		lpfc_issue_clear_la(phba, vport);
1223 	}
1224 out:
1225 	phba->hba_flag &= ~HBA_FLOGI_OUTSTANDING;
1226 	lpfc_els_free_iocb(phba, cmdiocb);
1227 	lpfc_nlp_put(ndlp);
1228 }
1229 
1230 /**
1231  * lpfc_cmpl_els_link_down - Completion callback function for ELS command
1232  *                           aborted during a link down
1233  * @phba: pointer to lpfc hba data structure.
1234  * @cmdiocb: pointer to lpfc command iocb data structure.
1235  * @rspiocb: pointer to lpfc response iocb data structure.
1236  *
1237  */
1238 static void
1239 lpfc_cmpl_els_link_down(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1240 			struct lpfc_iocbq *rspiocb)
1241 {
1242 	IOCB_t *irsp;
1243 	uint32_t *pcmd;
1244 	uint32_t cmd;
1245 
1246 	pcmd = (uint32_t *)(((struct lpfc_dmabuf *)cmdiocb->context2)->virt);
1247 	cmd = *pcmd;
1248 	irsp = &rspiocb->iocb;
1249 
1250 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1251 			"6445 ELS completes after LINK_DOWN: "
1252 			" Status %x/%x cmd x%x flg x%x\n",
1253 			irsp->ulpStatus, irsp->un.ulpWord[4], cmd,
1254 			cmdiocb->iocb_flag);
1255 
1256 	if (cmdiocb->iocb_flag & LPFC_IO_FABRIC) {
1257 		cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
1258 		atomic_dec(&phba->fabric_iocb_count);
1259 	}
1260 	lpfc_els_free_iocb(phba, cmdiocb);
1261 }
1262 
1263 /**
1264  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1265  * @vport: pointer to a host virtual N_Port data structure.
1266  * @ndlp: pointer to a node-list data structure.
1267  * @retry: number of retries to the command IOCB.
1268  *
1269  * This routine issues a Fabric Login (FLOGI) Request ELS command
1270  * for a @vport. The initiator service parameters are put into the payload
1271  * of the FLOGI Request IOCB and the top-level callback function pointer
1272  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1273  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1274  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1275  *
1276  * Note that the ndlp reference count will be incremented by 1 for holding the
1277  * ndlp and the reference to ndlp will be stored into the context1 field of
1278  * the IOCB for the completion callback function to the FLOGI ELS command.
1279  *
1280  * Return code
1281  *   0 - successfully issued flogi iocb for @vport
1282  *   1 - failed to issue flogi iocb for @vport
1283  **/
1284 static int
1285 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1286 		     uint8_t retry)
1287 {
1288 	struct lpfc_hba  *phba = vport->phba;
1289 	struct serv_parm *sp;
1290 	IOCB_t *icmd;
1291 	struct lpfc_iocbq *elsiocb;
1292 	struct lpfc_iocbq defer_flogi_acc;
1293 	uint8_t *pcmd;
1294 	uint16_t cmdsize;
1295 	uint32_t tmo, did;
1296 	int rc;
1297 
1298 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1299 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1300 				     ndlp->nlp_DID, ELS_CMD_FLOGI);
1301 
1302 	if (!elsiocb)
1303 		return 1;
1304 
1305 	icmd = &elsiocb->iocb;
1306 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1307 
1308 	/* For FLOGI request, remainder of payload is service parameters */
1309 	*((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1310 	pcmd += sizeof(uint32_t);
1311 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1312 	sp = (struct serv_parm *) pcmd;
1313 
1314 	/* Setup CSPs accordingly for Fabric */
1315 	sp->cmn.e_d_tov = 0;
1316 	sp->cmn.w2.r_a_tov = 0;
1317 	sp->cmn.virtual_fabric_support = 0;
1318 	sp->cls1.classValid = 0;
1319 	if (sp->cmn.fcphLow < FC_PH3)
1320 		sp->cmn.fcphLow = FC_PH3;
1321 	if (sp->cmn.fcphHigh < FC_PH3)
1322 		sp->cmn.fcphHigh = FC_PH3;
1323 
1324 	/* Determine if switch supports priority tagging */
1325 	if (phba->cfg_vmid_priority_tagging) {
1326 		sp->cmn.priority_tagging = 1;
1327 		/* lpfc_vmid_host_uuid is combination of wwpn and wwnn */
1328 		if (uuid_is_null((uuid_t *)vport->lpfc_vmid_host_uuid)) {
1329 			memcpy(vport->lpfc_vmid_host_uuid, phba->wwpn,
1330 			       sizeof(phba->wwpn));
1331 			memcpy(&vport->lpfc_vmid_host_uuid[8], phba->wwnn,
1332 			       sizeof(phba->wwnn));
1333 		}
1334 	}
1335 
1336 	if  (phba->sli_rev == LPFC_SLI_REV4) {
1337 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1338 		    LPFC_SLI_INTF_IF_TYPE_0) {
1339 			elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1340 			elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1341 			/* FLOGI needs to be 3 for WQE FCFI */
1342 			/* Set the fcfi to the fcfi we registered with */
1343 			elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1344 		}
1345 		/* Can't do SLI4 class2 without support sequence coalescing */
1346 		sp->cls2.classValid = 0;
1347 		sp->cls2.seqDelivery = 0;
1348 	} else {
1349 		/* Historical, setting sequential-delivery bit for SLI3 */
1350 		sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1351 		sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1352 		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1353 			sp->cmn.request_multiple_Nport = 1;
1354 			/* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1355 			icmd->ulpCt_h = 1;
1356 			icmd->ulpCt_l = 0;
1357 		} else
1358 			sp->cmn.request_multiple_Nport = 0;
1359 	}
1360 
1361 	if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1362 		icmd->un.elsreq64.myID = 0;
1363 		icmd->un.elsreq64.fl = 1;
1364 	}
1365 
1366 	tmo = phba->fc_ratov;
1367 	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1368 	lpfc_set_disctmo(vport);
1369 	phba->fc_ratov = tmo;
1370 
1371 	phba->fc_stat.elsXmitFLOGI++;
1372 	elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1373 
1374 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1375 		"Issue FLOGI:     opt:x%x",
1376 		phba->sli3_options, 0, 0);
1377 
1378 	elsiocb->context1 = lpfc_nlp_get(ndlp);
1379 	if (!elsiocb->context1) {
1380 		lpfc_els_free_iocb(phba, elsiocb);
1381 		return 1;
1382 	}
1383 
1384 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1385 	if (rc == IOCB_ERROR) {
1386 		lpfc_els_free_iocb(phba, elsiocb);
1387 		lpfc_nlp_put(ndlp);
1388 		return 1;
1389 	}
1390 
1391 	phba->hba_flag |= (HBA_FLOGI_ISSUED | HBA_FLOGI_OUTSTANDING);
1392 
1393 	/* Check for a deferred FLOGI ACC condition */
1394 	if (phba->defer_flogi_acc_flag) {
1395 		did = vport->fc_myDID;
1396 		vport->fc_myDID = Fabric_DID;
1397 
1398 		memset(&defer_flogi_acc, 0, sizeof(struct lpfc_iocbq));
1399 
1400 		defer_flogi_acc.iocb.ulpContext = phba->defer_flogi_acc_rx_id;
1401 		defer_flogi_acc.iocb.unsli3.rcvsli3.ox_id =
1402 						phba->defer_flogi_acc_ox_id;
1403 
1404 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1405 				 "3354 Xmit deferred FLOGI ACC: rx_id: x%x,"
1406 				 " ox_id: x%x, hba_flag x%x\n",
1407 				 phba->defer_flogi_acc_rx_id,
1408 				 phba->defer_flogi_acc_ox_id, phba->hba_flag);
1409 
1410 		/* Send deferred FLOGI ACC */
1411 		lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, &defer_flogi_acc,
1412 				 ndlp, NULL);
1413 
1414 		phba->defer_flogi_acc_flag = false;
1415 
1416 		vport->fc_myDID = did;
1417 	}
1418 
1419 	return 0;
1420 }
1421 
1422 /**
1423  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1424  * @phba: pointer to lpfc hba data structure.
1425  *
1426  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1427  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1428  * list and issues an abort IOCB commond on each outstanding IOCB that
1429  * contains a active Fabric_DID ndlp. Note that this function is to issue
1430  * the abort IOCB command on all the outstanding IOCBs, thus when this
1431  * function returns, it does not guarantee all the IOCBs are actually aborted.
1432  *
1433  * Return code
1434  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1435  **/
1436 int
1437 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1438 {
1439 	struct lpfc_sli_ring *pring;
1440 	struct lpfc_iocbq *iocb, *next_iocb;
1441 	struct lpfc_nodelist *ndlp;
1442 	IOCB_t *icmd;
1443 
1444 	/* Abort outstanding I/O on NPort <nlp_DID> */
1445 	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1446 			"0201 Abort outstanding I/O on NPort x%x\n",
1447 			Fabric_DID);
1448 
1449 	pring = lpfc_phba_elsring(phba);
1450 	if (unlikely(!pring))
1451 		return -EIO;
1452 
1453 	/*
1454 	 * Check the txcmplq for an iocb that matches the nport the driver is
1455 	 * searching for.
1456 	 */
1457 	spin_lock_irq(&phba->hbalock);
1458 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1459 		icmd = &iocb->iocb;
1460 		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
1461 			ndlp = (struct lpfc_nodelist *)(iocb->context1);
1462 			if (ndlp && ndlp->nlp_DID == Fabric_DID) {
1463 				if ((phba->pport->fc_flag & FC_PT2PT) &&
1464 				    !(phba->pport->fc_flag & FC_PT2PT_PLOGI))
1465 					iocb->fabric_iocb_cmpl =
1466 						lpfc_ignore_els_cmpl;
1467 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
1468 							   NULL);
1469 			}
1470 		}
1471 	}
1472 	/* Make sure HBA is alive */
1473 	lpfc_issue_hb_tmo(phba);
1474 
1475 	spin_unlock_irq(&phba->hbalock);
1476 
1477 	return 0;
1478 }
1479 
1480 /**
1481  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1482  * @vport: pointer to a host virtual N_Port data structure.
1483  *
1484  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1485  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1486  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1487  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1488  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1489  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1490  * @vport.
1491  *
1492  * Return code
1493  *   0 - failed to issue initial flogi for @vport
1494  *   1 - successfully issued initial flogi for @vport
1495  **/
1496 int
1497 lpfc_initial_flogi(struct lpfc_vport *vport)
1498 {
1499 	struct lpfc_nodelist *ndlp;
1500 
1501 	vport->port_state = LPFC_FLOGI;
1502 	lpfc_set_disctmo(vport);
1503 
1504 	/* First look for the Fabric ndlp */
1505 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
1506 	if (!ndlp) {
1507 		/* Cannot find existing Fabric ndlp, so allocate a new one */
1508 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
1509 		if (!ndlp)
1510 			return 0;
1511 		/* Set the node type */
1512 		ndlp->nlp_type |= NLP_FABRIC;
1513 
1514 		/* Put ndlp onto node list */
1515 		lpfc_enqueue_node(vport, ndlp);
1516 	}
1517 
1518 	if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1519 		/* This decrement of reference count to node shall kick off
1520 		 * the release of the node.
1521 		 */
1522 		lpfc_nlp_put(ndlp);
1523 		return 0;
1524 	}
1525 	return 1;
1526 }
1527 
1528 /**
1529  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1530  * @vport: pointer to a host virtual N_Port data structure.
1531  *
1532  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1533  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1534  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1535  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1536  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1537  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1538  * @vport.
1539  *
1540  * Return code
1541  *   0 - failed to issue initial fdisc for @vport
1542  *   1 - successfully issued initial fdisc for @vport
1543  **/
1544 int
1545 lpfc_initial_fdisc(struct lpfc_vport *vport)
1546 {
1547 	struct lpfc_nodelist *ndlp;
1548 
1549 	/* First look for the Fabric ndlp */
1550 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
1551 	if (!ndlp) {
1552 		/* Cannot find existing Fabric ndlp, so allocate a new one */
1553 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
1554 		if (!ndlp)
1555 			return 0;
1556 
1557 		/* NPIV is only supported in Fabrics. */
1558 		ndlp->nlp_type |= NLP_FABRIC;
1559 
1560 		/* Put ndlp onto node list */
1561 		lpfc_enqueue_node(vport, ndlp);
1562 	}
1563 
1564 	if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1565 		/* decrement node reference count to trigger the release of
1566 		 * the node.
1567 		 */
1568 		lpfc_nlp_put(ndlp);
1569 		return 0;
1570 	}
1571 	return 1;
1572 }
1573 
1574 /**
1575  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1576  * @vport: pointer to a host virtual N_Port data structure.
1577  *
1578  * This routine checks whether there are more remaining Port Logins
1579  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1580  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1581  * to issue ELS PLOGIs up to the configured discover threads with the
1582  * @vport (@vport->cfg_discovery_threads). The function also decrement
1583  * the @vport's num_disc_node by 1 if it is not already 0.
1584  **/
1585 void
1586 lpfc_more_plogi(struct lpfc_vport *vport)
1587 {
1588 	if (vport->num_disc_nodes)
1589 		vport->num_disc_nodes--;
1590 
1591 	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
1592 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1593 			 "0232 Continue discovery with %d PLOGIs to go "
1594 			 "Data: x%x x%x x%x\n",
1595 			 vport->num_disc_nodes, vport->fc_plogi_cnt,
1596 			 vport->fc_flag, vport->port_state);
1597 	/* Check to see if there are more PLOGIs to be sent */
1598 	if (vport->fc_flag & FC_NLP_MORE)
1599 		/* go thru NPR nodes and issue any remaining ELS PLOGIs */
1600 		lpfc_els_disc_plogi(vport);
1601 
1602 	return;
1603 }
1604 
1605 /**
1606  * lpfc_plogi_confirm_nport - Confirm plogi wwpn matches stored ndlp
1607  * @phba: pointer to lpfc hba data structure.
1608  * @prsp: pointer to response IOCB payload.
1609  * @ndlp: pointer to a node-list data structure.
1610  *
1611  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1612  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1613  * The following cases are considered N_Port confirmed:
1614  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1615  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1616  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1617  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1618  * 1) if there is a node on vport list other than the @ndlp with the same
1619  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1620  * on that node to release the RPI associated with the node; 2) if there is
1621  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1622  * into, a new node shall be allocated (or activated). In either case, the
1623  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1624  * be released and the new_ndlp shall be put on to the vport node list and
1625  * its pointer returned as the confirmed node.
1626  *
1627  * Note that before the @ndlp got "released", the keepDID from not-matching
1628  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1629  * of the @ndlp. This is because the release of @ndlp is actually to put it
1630  * into an inactive state on the vport node list and the vport node list
1631  * management algorithm does not allow two node with a same DID.
1632  *
1633  * Return code
1634  *   pointer to the PLOGI N_Port @ndlp
1635  **/
1636 static struct lpfc_nodelist *
1637 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1638 			 struct lpfc_nodelist *ndlp)
1639 {
1640 	struct lpfc_vport *vport = ndlp->vport;
1641 	struct lpfc_nodelist *new_ndlp;
1642 	struct serv_parm *sp;
1643 	uint8_t  name[sizeof(struct lpfc_name)];
1644 	uint32_t keepDID = 0, keep_nlp_flag = 0;
1645 	uint32_t keep_new_nlp_flag = 0;
1646 	uint16_t keep_nlp_state;
1647 	u32 keep_nlp_fc4_type = 0;
1648 	struct lpfc_nvme_rport *keep_nrport = NULL;
1649 	unsigned long *active_rrqs_xri_bitmap = NULL;
1650 
1651 	/* Fabric nodes can have the same WWPN so we don't bother searching
1652 	 * by WWPN.  Just return the ndlp that was given to us.
1653 	 */
1654 	if (ndlp->nlp_type & NLP_FABRIC)
1655 		return ndlp;
1656 
1657 	sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1658 	memset(name, 0, sizeof(struct lpfc_name));
1659 
1660 	/* Now we find out if the NPort we are logging into, matches the WWPN
1661 	 * we have for that ndlp. If not, we have some work to do.
1662 	 */
1663 	new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1664 
1665 	/* return immediately if the WWPN matches ndlp */
1666 	if (!new_ndlp || (new_ndlp == ndlp))
1667 		return ndlp;
1668 
1669 	/*
1670 	 * Unregister from backend if not done yet. Could have been skipped
1671 	 * due to ADISC
1672 	 */
1673 	lpfc_nlp_unreg_node(vport, new_ndlp);
1674 
1675 	if (phba->sli_rev == LPFC_SLI_REV4) {
1676 		active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1677 						       GFP_KERNEL);
1678 		if (active_rrqs_xri_bitmap)
1679 			memset(active_rrqs_xri_bitmap, 0,
1680 			       phba->cfg_rrq_xri_bitmap_sz);
1681 	}
1682 
1683 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1684 			 "3178 PLOGI confirm: ndlp x%x x%x x%x: "
1685 			 "new_ndlp x%x x%x x%x\n",
1686 			 ndlp->nlp_DID, ndlp->nlp_flag,  ndlp->nlp_fc4_type,
1687 			 (new_ndlp ? new_ndlp->nlp_DID : 0),
1688 			 (new_ndlp ? new_ndlp->nlp_flag : 0),
1689 			 (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
1690 
1691 	keepDID = new_ndlp->nlp_DID;
1692 
1693 	if (phba->sli_rev == LPFC_SLI_REV4 && active_rrqs_xri_bitmap)
1694 		memcpy(active_rrqs_xri_bitmap, new_ndlp->active_rrqs_xri_bitmap,
1695 		       phba->cfg_rrq_xri_bitmap_sz);
1696 
1697 	/* At this point in this routine, we know new_ndlp will be
1698 	 * returned. however, any previous GID_FTs that were done
1699 	 * would have updated nlp_fc4_type in ndlp, so we must ensure
1700 	 * new_ndlp has the right value.
1701 	 */
1702 	if (vport->fc_flag & FC_FABRIC) {
1703 		keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
1704 		new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
1705 	}
1706 
1707 	lpfc_unreg_rpi(vport, new_ndlp);
1708 	new_ndlp->nlp_DID = ndlp->nlp_DID;
1709 	new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1710 	if (phba->sli_rev == LPFC_SLI_REV4)
1711 		memcpy(new_ndlp->active_rrqs_xri_bitmap,
1712 		       ndlp->active_rrqs_xri_bitmap,
1713 		       phba->cfg_rrq_xri_bitmap_sz);
1714 
1715 	/* Lock both ndlps */
1716 	spin_lock_irq(&ndlp->lock);
1717 	spin_lock_irq(&new_ndlp->lock);
1718 	keep_new_nlp_flag = new_ndlp->nlp_flag;
1719 	keep_nlp_flag = ndlp->nlp_flag;
1720 	new_ndlp->nlp_flag = ndlp->nlp_flag;
1721 
1722 	/* if new_ndlp had NLP_UNREG_INP set, keep it */
1723 	if (keep_new_nlp_flag & NLP_UNREG_INP)
1724 		new_ndlp->nlp_flag |= NLP_UNREG_INP;
1725 	else
1726 		new_ndlp->nlp_flag &= ~NLP_UNREG_INP;
1727 
1728 	/* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
1729 	if (keep_new_nlp_flag & NLP_RPI_REGISTERED)
1730 		new_ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1731 	else
1732 		new_ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1733 
1734 	/*
1735 	 * Retain the DROPPED flag. This will take care of the init
1736 	 * refcount when affecting the state change
1737 	 */
1738 	if (keep_new_nlp_flag & NLP_DROPPED)
1739 		new_ndlp->nlp_flag |= NLP_DROPPED;
1740 	else
1741 		new_ndlp->nlp_flag &= ~NLP_DROPPED;
1742 
1743 	ndlp->nlp_flag = keep_new_nlp_flag;
1744 
1745 	/* if ndlp had NLP_UNREG_INP set, keep it */
1746 	if (keep_nlp_flag & NLP_UNREG_INP)
1747 		ndlp->nlp_flag |= NLP_UNREG_INP;
1748 	else
1749 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
1750 
1751 	/* if ndlp had NLP_RPI_REGISTERED set, keep it */
1752 	if (keep_nlp_flag & NLP_RPI_REGISTERED)
1753 		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1754 	else
1755 		ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1756 
1757 	/*
1758 	 * Retain the DROPPED flag. This will take care of the init
1759 	 * refcount when affecting the state change
1760 	 */
1761 	if (keep_nlp_flag & NLP_DROPPED)
1762 		ndlp->nlp_flag |= NLP_DROPPED;
1763 	else
1764 		ndlp->nlp_flag &= ~NLP_DROPPED;
1765 
1766 	spin_unlock_irq(&new_ndlp->lock);
1767 	spin_unlock_irq(&ndlp->lock);
1768 
1769 	/* Set nlp_states accordingly */
1770 	keep_nlp_state = new_ndlp->nlp_state;
1771 	lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1772 
1773 	/* interchange the nvme remoteport structs */
1774 	keep_nrport = new_ndlp->nrport;
1775 	new_ndlp->nrport = ndlp->nrport;
1776 
1777 	/* Move this back to NPR state */
1778 	if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1779 		/* The new_ndlp is replacing ndlp totally, so we need
1780 		 * to put ndlp on UNUSED list and try to free it.
1781 		 */
1782 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1783 			 "3179 PLOGI confirm NEW: %x %x\n",
1784 			 new_ndlp->nlp_DID, keepDID);
1785 
1786 		/* Two ndlps cannot have the same did on the nodelist.
1787 		 * Note: for this case, ndlp has a NULL WWPN so setting
1788 		 * the nlp_fc4_type isn't required.
1789 		 */
1790 		ndlp->nlp_DID = keepDID;
1791 		lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1792 		if (phba->sli_rev == LPFC_SLI_REV4 &&
1793 		    active_rrqs_xri_bitmap)
1794 			memcpy(ndlp->active_rrqs_xri_bitmap,
1795 			       active_rrqs_xri_bitmap,
1796 			       phba->cfg_rrq_xri_bitmap_sz);
1797 
1798 	} else {
1799 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1800 			 "3180 PLOGI confirm SWAP: %x %x\n",
1801 			 new_ndlp->nlp_DID, keepDID);
1802 
1803 		lpfc_unreg_rpi(vport, ndlp);
1804 
1805 		/* Two ndlps cannot have the same did and the fc4
1806 		 * type must be transferred because the ndlp is in
1807 		 * flight.
1808 		 */
1809 		ndlp->nlp_DID = keepDID;
1810 		ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1811 
1812 		if (phba->sli_rev == LPFC_SLI_REV4 &&
1813 		    active_rrqs_xri_bitmap)
1814 			memcpy(ndlp->active_rrqs_xri_bitmap,
1815 			       active_rrqs_xri_bitmap,
1816 			       phba->cfg_rrq_xri_bitmap_sz);
1817 
1818 		/* Since we are switching over to the new_ndlp,
1819 		 * reset the old ndlp state
1820 		 */
1821 		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1822 		    (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1823 			keep_nlp_state = NLP_STE_NPR_NODE;
1824 		lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1825 		ndlp->nrport = keep_nrport;
1826 	}
1827 
1828 	/*
1829 	 * If ndlp is not associated with any rport we can drop it here else
1830 	 * let dev_loss_tmo_callbk trigger DEVICE_RM event
1831 	 */
1832 	if (!ndlp->rport && (ndlp->nlp_state == NLP_STE_NPR_NODE))
1833 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
1834 
1835 	if (phba->sli_rev == LPFC_SLI_REV4 &&
1836 	    active_rrqs_xri_bitmap)
1837 		mempool_free(active_rrqs_xri_bitmap,
1838 			     phba->active_rrq_pool);
1839 
1840 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1841 			 "3173 PLOGI confirm exit: new_ndlp x%x x%x x%x\n",
1842 			 new_ndlp->nlp_DID, new_ndlp->nlp_flag,
1843 			 new_ndlp->nlp_fc4_type);
1844 
1845 	return new_ndlp;
1846 }
1847 
1848 /**
1849  * lpfc_end_rscn - Check and handle more rscn for a vport
1850  * @vport: pointer to a host virtual N_Port data structure.
1851  *
1852  * This routine checks whether more Registration State Change
1853  * Notifications (RSCNs) came in while the discovery state machine was in
1854  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1855  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1856  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1857  * handling the RSCNs.
1858  **/
1859 void
1860 lpfc_end_rscn(struct lpfc_vport *vport)
1861 {
1862 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1863 
1864 	if (vport->fc_flag & FC_RSCN_MODE) {
1865 		/*
1866 		 * Check to see if more RSCNs came in while we were
1867 		 * processing this one.
1868 		 */
1869 		if (vport->fc_rscn_id_cnt ||
1870 		    (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1871 			lpfc_els_handle_rscn(vport);
1872 		else {
1873 			spin_lock_irq(shost->host_lock);
1874 			vport->fc_flag &= ~FC_RSCN_MODE;
1875 			spin_unlock_irq(shost->host_lock);
1876 		}
1877 	}
1878 }
1879 
1880 /**
1881  * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1882  * @phba: pointer to lpfc hba data structure.
1883  * @cmdiocb: pointer to lpfc command iocb data structure.
1884  * @rspiocb: pointer to lpfc response iocb data structure.
1885  *
1886  * This routine will call the clear rrq function to free the rrq and
1887  * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1888  * exist then the clear_rrq is still called because the rrq needs to
1889  * be freed.
1890  **/
1891 
1892 static void
1893 lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1894 		    struct lpfc_iocbq *rspiocb)
1895 {
1896 	struct lpfc_vport *vport = cmdiocb->vport;
1897 	IOCB_t *irsp;
1898 	struct lpfc_nodelist *ndlp = cmdiocb->context1;
1899 	struct lpfc_node_rrq *rrq;
1900 
1901 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1902 	rrq = cmdiocb->context_un.rrq;
1903 	cmdiocb->context_un.rsp_iocb = rspiocb;
1904 
1905 	irsp = &rspiocb->iocb;
1906 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1907 		"RRQ cmpl:      status:x%x/x%x did:x%x",
1908 		irsp->ulpStatus, irsp->un.ulpWord[4],
1909 		irsp->un.elsreq64.remoteID);
1910 
1911 	/* rrq completes to NPort <nlp_DID> */
1912 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1913 			 "2880 RRQ completes to DID x%x "
1914 			 "Data: x%x x%x x%x x%x x%x\n",
1915 			 irsp->un.elsreq64.remoteID,
1916 			 irsp->ulpStatus, irsp->un.ulpWord[4],
1917 			 irsp->ulpTimeout, rrq->xritag, rrq->rxid);
1918 
1919 	if (irsp->ulpStatus) {
1920 		/* Check for retry */
1921 		/* RRQ failed Don't print the vport to vport rjts */
1922 		if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1923 			(((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1924 			((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1925 			(phba)->pport->cfg_log_verbose & LOG_ELS)
1926 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1927 					 "2881 RRQ failure DID:%06X Status:"
1928 					 "x%x/x%x\n",
1929 					 ndlp->nlp_DID, irsp->ulpStatus,
1930 					 irsp->un.ulpWord[4]);
1931 	}
1932 
1933 	lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1934 	lpfc_els_free_iocb(phba, cmdiocb);
1935 	lpfc_nlp_put(ndlp);
1936 	return;
1937 }
1938 /**
1939  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1940  * @phba: pointer to lpfc hba data structure.
1941  * @cmdiocb: pointer to lpfc command iocb data structure.
1942  * @rspiocb: pointer to lpfc response iocb data structure.
1943  *
1944  * This routine is the completion callback function for issuing the Port
1945  * Login (PLOGI) command. For PLOGI completion, there must be an active
1946  * ndlp on the vport node list that matches the remote node ID from the
1947  * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
1948  * ignored and command IOCB released. The PLOGI response IOCB status is
1949  * checked for error conditions. If there is error status reported, PLOGI
1950  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1951  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1952  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1953  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1954  * there are additional N_Port nodes with the vport that need to perform
1955  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1956  * PLOGIs.
1957  **/
1958 static void
1959 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1960 		    struct lpfc_iocbq *rspiocb)
1961 {
1962 	struct lpfc_vport *vport = cmdiocb->vport;
1963 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1964 	IOCB_t *irsp;
1965 	struct lpfc_nodelist *ndlp, *free_ndlp;
1966 	struct lpfc_dmabuf *prsp;
1967 	int disc;
1968 	struct serv_parm *sp = NULL;
1969 
1970 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1971 	cmdiocb->context_un.rsp_iocb = rspiocb;
1972 
1973 	irsp = &rspiocb->iocb;
1974 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1975 		"PLOGI cmpl:      status:x%x/x%x did:x%x",
1976 		irsp->ulpStatus, irsp->un.ulpWord[4],
1977 		irsp->un.elsreq64.remoteID);
1978 
1979 	ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1980 	if (!ndlp) {
1981 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1982 				 "0136 PLOGI completes to NPort x%x "
1983 				 "with no ndlp. Data: x%x x%x x%x\n",
1984 				 irsp->un.elsreq64.remoteID,
1985 				 irsp->ulpStatus, irsp->un.ulpWord[4],
1986 				 irsp->ulpIoTag);
1987 		goto out_freeiocb;
1988 	}
1989 
1990 	/* Since ndlp can be freed in the disc state machine, note if this node
1991 	 * is being used during discovery.
1992 	 */
1993 	spin_lock_irq(&ndlp->lock);
1994 	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1995 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1996 	spin_unlock_irq(&ndlp->lock);
1997 
1998 	/* PLOGI completes to NPort <nlp_DID> */
1999 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2000 			 "0102 PLOGI completes to NPort x%06x "
2001 			 "Data: x%x x%x x%x x%x x%x\n",
2002 			 ndlp->nlp_DID, ndlp->nlp_fc4_type,
2003 			 irsp->ulpStatus, irsp->un.ulpWord[4],
2004 			 disc, vport->num_disc_nodes);
2005 
2006 	/* Check to see if link went down during discovery */
2007 	if (lpfc_els_chk_latt(vport)) {
2008 		spin_lock_irq(&ndlp->lock);
2009 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2010 		spin_unlock_irq(&ndlp->lock);
2011 		goto out;
2012 	}
2013 
2014 	if (irsp->ulpStatus) {
2015 		/* Check for retry */
2016 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2017 			/* ELS command is being retried */
2018 			if (disc) {
2019 				spin_lock_irq(&ndlp->lock);
2020 				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2021 				spin_unlock_irq(&ndlp->lock);
2022 			}
2023 			goto out;
2024 		}
2025 		/* PLOGI failed Don't print the vport to vport rjts */
2026 		if (irsp->ulpStatus != IOSTAT_LS_RJT ||
2027 			(((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
2028 			((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
2029 			(phba)->pport->cfg_log_verbose & LOG_ELS)
2030 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2031 				 "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
2032 				 ndlp->nlp_DID, irsp->ulpStatus,
2033 				 irsp->un.ulpWord[4]);
2034 
2035 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2036 		if (!lpfc_error_lost_link(irsp))
2037 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2038 						NLP_EVT_CMPL_PLOGI);
2039 
2040 		/* If a PLOGI collision occurred, the node needs to continue
2041 		 * with the reglogin process.
2042 		 */
2043 		spin_lock_irq(&ndlp->lock);
2044 		if ((ndlp->nlp_flag & (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI)) &&
2045 		    ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE) {
2046 			spin_unlock_irq(&ndlp->lock);
2047 			goto out;
2048 		}
2049 		spin_unlock_irq(&ndlp->lock);
2050 
2051 		/* No PLOGI collision and the node is not registered with the
2052 		 * scsi or nvme transport. It is no longer an active node. Just
2053 		 * start the device remove process.
2054 		 */
2055 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2056 			spin_lock_irq(&ndlp->lock);
2057 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2058 			spin_unlock_irq(&ndlp->lock);
2059 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2060 						NLP_EVT_DEVICE_RM);
2061 		}
2062 	} else {
2063 		/* Good status, call state machine */
2064 		prsp = list_entry(((struct lpfc_dmabuf *)
2065 				   cmdiocb->context2)->list.next,
2066 				  struct lpfc_dmabuf, list);
2067 		ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2068 
2069 		sp = (struct serv_parm *)((u8 *)prsp->virt +
2070 					  sizeof(u32));
2071 
2072 		ndlp->vmid_support = 0;
2073 		if ((phba->cfg_vmid_app_header && sp->cmn.app_hdr_support) ||
2074 		    (phba->cfg_vmid_priority_tagging &&
2075 		     sp->cmn.priority_tagging)) {
2076 			lpfc_printf_log(phba, KERN_DEBUG, LOG_ELS,
2077 					"4018 app_hdr_support %d tagging %d DID x%x\n",
2078 					sp->cmn.app_hdr_support,
2079 					sp->cmn.priority_tagging,
2080 					ndlp->nlp_DID);
2081 			/* if the dest port supports VMID, mark it in ndlp */
2082 			ndlp->vmid_support = 1;
2083 		}
2084 
2085 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2086 					NLP_EVT_CMPL_PLOGI);
2087 	}
2088 
2089 	if (disc && vport->num_disc_nodes) {
2090 		/* Check to see if there are more PLOGIs to be sent */
2091 		lpfc_more_plogi(vport);
2092 
2093 		if (vport->num_disc_nodes == 0) {
2094 			spin_lock_irq(shost->host_lock);
2095 			vport->fc_flag &= ~FC_NDISC_ACTIVE;
2096 			spin_unlock_irq(shost->host_lock);
2097 
2098 			lpfc_can_disctmo(vport);
2099 			lpfc_end_rscn(vport);
2100 		}
2101 	}
2102 
2103 out:
2104 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2105 			      "PLOGI Cmpl PUT:     did:x%x refcnt %d",
2106 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2107 
2108 out_freeiocb:
2109 	/* Release the reference on the original I/O request. */
2110 	free_ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
2111 
2112 	lpfc_els_free_iocb(phba, cmdiocb);
2113 	lpfc_nlp_put(free_ndlp);
2114 	return;
2115 }
2116 
2117 /**
2118  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
2119  * @vport: pointer to a host virtual N_Port data structure.
2120  * @did: destination port identifier.
2121  * @retry: number of retries to the command IOCB.
2122  *
2123  * This routine issues a Port Login (PLOGI) command to a remote N_Port
2124  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
2125  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
2126  * This routine constructs the proper fields of the PLOGI IOCB and invokes
2127  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
2128  *
2129  * Note that the ndlp reference count will be incremented by 1 for holding
2130  * the ndlp and the reference to ndlp will be stored into the context1 field
2131  * of the IOCB for the completion callback function to the PLOGI ELS command.
2132  *
2133  * Return code
2134  *   0 - Successfully issued a plogi for @vport
2135  *   1 - failed to issue a plogi for @vport
2136  **/
2137 int
2138 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
2139 {
2140 	struct lpfc_hba  *phba = vport->phba;
2141 	struct serv_parm *sp;
2142 	struct lpfc_nodelist *ndlp;
2143 	struct lpfc_iocbq *elsiocb;
2144 	uint8_t *pcmd;
2145 	uint16_t cmdsize;
2146 	int ret;
2147 
2148 	ndlp = lpfc_findnode_did(vport, did);
2149 	if (!ndlp)
2150 		return 1;
2151 
2152 	/* Defer the processing of the issue PLOGI until after the
2153 	 * outstanding UNREG_RPI mbox command completes, unless we
2154 	 * are going offline. This logic does not apply for Fabric DIDs
2155 	 */
2156 	if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2157 	    ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
2158 	    !(vport->fc_flag & FC_OFFLINE_MODE)) {
2159 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2160 				 "4110 Issue PLOGI x%x deferred "
2161 				 "on NPort x%x rpi x%x Data: x%px\n",
2162 				 ndlp->nlp_defer_did, ndlp->nlp_DID,
2163 				 ndlp->nlp_rpi, ndlp);
2164 
2165 		/* We can only defer 1st PLOGI */
2166 		if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
2167 			ndlp->nlp_defer_did = did;
2168 		return 0;
2169 	}
2170 
2171 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2172 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2173 				     ELS_CMD_PLOGI);
2174 	if (!elsiocb)
2175 		return 1;
2176 
2177 	spin_lock_irq(&ndlp->lock);
2178 	ndlp->nlp_flag &= ~NLP_FCP_PRLI_RJT;
2179 	spin_unlock_irq(&ndlp->lock);
2180 
2181 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2182 
2183 	/* For PLOGI request, remainder of payload is service parameters */
2184 	*((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
2185 	pcmd += sizeof(uint32_t);
2186 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2187 	sp = (struct serv_parm *) pcmd;
2188 
2189 	/*
2190 	 * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2191 	 * to device on remote loops work.
2192 	 */
2193 	if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
2194 		sp->cmn.altBbCredit = 1;
2195 
2196 	if (sp->cmn.fcphLow < FC_PH_4_3)
2197 		sp->cmn.fcphLow = FC_PH_4_3;
2198 
2199 	if (sp->cmn.fcphHigh < FC_PH3)
2200 		sp->cmn.fcphHigh = FC_PH3;
2201 
2202 	sp->cmn.valid_vendor_ver_level = 0;
2203 	memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
2204 	sp->cmn.bbRcvSizeMsb &= 0xF;
2205 
2206 	/* Check if the destination port supports VMID */
2207 	ndlp->vmid_support = 0;
2208 	if (vport->vmid_priority_tagging)
2209 		sp->cmn.priority_tagging = 1;
2210 	else if (phba->cfg_vmid_app_header &&
2211 		 bf_get(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags))
2212 		sp->cmn.app_hdr_support = 1;
2213 
2214 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2215 		"Issue PLOGI:     did:x%x",
2216 		did, 0, 0);
2217 
2218 	/* If our firmware supports this feature, convey that
2219 	 * information to the target using the vendor specific field.
2220 	 */
2221 	if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2222 		sp->cmn.valid_vendor_ver_level = 1;
2223 		sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2224 		sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2225 	}
2226 
2227 	phba->fc_stat.elsXmitPLOGI++;
2228 	elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
2229 
2230 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2231 			      "Issue PLOGI:     did:x%x refcnt %d",
2232 			      did, kref_read(&ndlp->kref), 0);
2233 	elsiocb->context1 = lpfc_nlp_get(ndlp);
2234 	if (!elsiocb->context1) {
2235 		lpfc_els_free_iocb(phba, elsiocb);
2236 		return 1;
2237 	}
2238 
2239 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2240 	if (ret) {
2241 		lpfc_els_free_iocb(phba, elsiocb);
2242 		lpfc_nlp_put(ndlp);
2243 		return 1;
2244 	}
2245 
2246 	return 0;
2247 }
2248 
2249 /**
2250  * lpfc_cmpl_els_prli - Completion callback function for prli
2251  * @phba: pointer to lpfc hba data structure.
2252  * @cmdiocb: pointer to lpfc command iocb data structure.
2253  * @rspiocb: pointer to lpfc response iocb data structure.
2254  *
2255  * This routine is the completion callback function for a Process Login
2256  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2257  * status. If there is error status reported, PRLI retry shall be attempted
2258  * by invoking the lpfc_els_retry() routine. Otherwise, the state
2259  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2260  * ndlp to mark the PRLI completion.
2261  **/
2262 static void
2263 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2264 		   struct lpfc_iocbq *rspiocb)
2265 {
2266 	struct lpfc_vport *vport = cmdiocb->vport;
2267 	IOCB_t *irsp;
2268 	struct lpfc_nodelist *ndlp;
2269 	char *mode;
2270 	u32 loglevel;
2271 
2272 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2273 	cmdiocb->context_un.rsp_iocb = rspiocb;
2274 
2275 	irsp = &(rspiocb->iocb);
2276 	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2277 	spin_lock_irq(&ndlp->lock);
2278 	ndlp->nlp_flag &= ~NLP_PRLI_SND;
2279 
2280 	/* Driver supports multiple FC4 types.  Counters matter. */
2281 	vport->fc_prli_sent--;
2282 	ndlp->fc4_prli_sent--;
2283 	spin_unlock_irq(&ndlp->lock);
2284 
2285 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2286 		"PRLI cmpl:       status:x%x/x%x did:x%x",
2287 		irsp->ulpStatus, irsp->un.ulpWord[4],
2288 		ndlp->nlp_DID);
2289 
2290 	/* PRLI completes to NPort <nlp_DID> */
2291 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2292 			 "0103 PRLI completes to NPort x%06x "
2293 			 "Data: x%x x%x x%x x%x\n",
2294 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2295 			 vport->num_disc_nodes, ndlp->fc4_prli_sent);
2296 
2297 	/* Check to see if link went down during discovery */
2298 	if (lpfc_els_chk_latt(vport))
2299 		goto out;
2300 
2301 	if (irsp->ulpStatus) {
2302 		/* Check for retry */
2303 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2304 			/* ELS command is being retried */
2305 			goto out;
2306 		}
2307 
2308 		/* If we don't send GFT_ID to Fabric, a PRLI error
2309 		 * could be expected.
2310 		 */
2311 		if ((vport->fc_flag & FC_FABRIC) ||
2312 		    (vport->cfg_enable_fc4_type != LPFC_ENABLE_BOTH)) {
2313 			mode = KERN_ERR;
2314 			loglevel =  LOG_TRACE_EVENT;
2315 		} else {
2316 			mode = KERN_INFO;
2317 			loglevel =  LOG_ELS;
2318 		}
2319 
2320 		/* PRLI failed */
2321 		lpfc_printf_vlog(vport, mode, loglevel,
2322 				 "2754 PRLI failure DID:%06X Status:x%x/x%x, "
2323 				 "data: x%x\n",
2324 				 ndlp->nlp_DID, irsp->ulpStatus,
2325 				 irsp->un.ulpWord[4], ndlp->fc4_prli_sent);
2326 
2327 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2328 		if (!lpfc_error_lost_link(irsp))
2329 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2330 						NLP_EVT_CMPL_PRLI);
2331 
2332 		/*
2333 		 * For P2P topology, retain the node so that PLOGI can be
2334 		 * attempted on it again.
2335 		 */
2336 		if (vport->fc_flag & FC_PT2PT)
2337 			goto out;
2338 
2339 		/* As long as this node is not registered with the SCSI
2340 		 * or NVMe transport and no other PRLIs are outstanding,
2341 		 * it is no longer an active node.  Otherwise devloss
2342 		 * handles the final cleanup.
2343 		 */
2344 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
2345 		    !ndlp->fc4_prli_sent) {
2346 			spin_lock_irq(&ndlp->lock);
2347 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2348 			spin_unlock_irq(&ndlp->lock);
2349 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2350 						NLP_EVT_DEVICE_RM);
2351 		}
2352 	} else {
2353 		/* Good status, call state machine.  However, if another
2354 		 * PRLI is outstanding, don't call the state machine
2355 		 * because final disposition to Mapped or Unmapped is
2356 		 * completed there.
2357 		 */
2358 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2359 					NLP_EVT_CMPL_PRLI);
2360 	}
2361 
2362 out:
2363 	lpfc_els_free_iocb(phba, cmdiocb);
2364 	lpfc_nlp_put(ndlp);
2365 	return;
2366 }
2367 
2368 /**
2369  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2370  * @vport: pointer to a host virtual N_Port data structure.
2371  * @ndlp: pointer to a node-list data structure.
2372  * @retry: number of retries to the command IOCB.
2373  *
2374  * This routine issues a Process Login (PRLI) ELS command for the
2375  * @vport. The PRLI service parameters are set up in the payload of the
2376  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2377  * is put to the IOCB completion callback func field before invoking the
2378  * routine lpfc_sli_issue_iocb() to send out PRLI command.
2379  *
2380  * Note that the ndlp reference count will be incremented by 1 for holding the
2381  * ndlp and the reference to ndlp will be stored into the context1 field of
2382  * the IOCB for the completion callback function to the PRLI ELS command.
2383  *
2384  * Return code
2385  *   0 - successfully issued prli iocb command for @vport
2386  *   1 - failed to issue prli iocb command for @vport
2387  **/
2388 int
2389 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2390 		    uint8_t retry)
2391 {
2392 	int rc = 0;
2393 	struct lpfc_hba *phba = vport->phba;
2394 	PRLI *npr;
2395 	struct lpfc_nvme_prli *npr_nvme;
2396 	struct lpfc_iocbq *elsiocb;
2397 	uint8_t *pcmd;
2398 	uint16_t cmdsize;
2399 	u32 local_nlp_type, elscmd;
2400 
2401 	/*
2402 	 * If we are in RSCN mode, the FC4 types supported from a
2403 	 * previous GFT_ID command may not be accurate. So, if we
2404 	 * are a NVME Initiator, always look for the possibility of
2405 	 * the remote NPort beng a NVME Target.
2406 	 */
2407 	if (phba->sli_rev == LPFC_SLI_REV4 &&
2408 	    vport->fc_flag & FC_RSCN_MODE &&
2409 	    vport->nvmei_support)
2410 		ndlp->nlp_fc4_type |= NLP_FC4_NVME;
2411 	local_nlp_type = ndlp->nlp_fc4_type;
2412 
2413 	/* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2414 	 * fields here before any of them can complete.
2415 	 */
2416 	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2417 	ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2418 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
2419 	ndlp->nlp_flag &= ~(NLP_FIRSTBURST | NLP_NPR_2B_DISC);
2420 	ndlp->nvme_fb_size = 0;
2421 
2422  send_next_prli:
2423 	if (local_nlp_type & NLP_FC4_FCP) {
2424 		/* Payload is 4 + 16 = 20 x14 bytes. */
2425 		cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2426 		elscmd = ELS_CMD_PRLI;
2427 	} else if (local_nlp_type & NLP_FC4_NVME) {
2428 		/* Payload is 4 + 20 = 24 x18 bytes. */
2429 		cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2430 		elscmd = ELS_CMD_NVMEPRLI;
2431 	} else {
2432 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2433 				 "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2434 				 ndlp->nlp_fc4_type, ndlp->nlp_DID);
2435 		return 1;
2436 	}
2437 
2438 	/* SLI3 ports don't support NVME.  If this rport is a strict NVME
2439 	 * FC4 type, implicitly LOGO.
2440 	 */
2441 	if (phba->sli_rev == LPFC_SLI_REV3 &&
2442 	    ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2443 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2444 				 "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2445 				 ndlp->nlp_type);
2446 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2447 		return 1;
2448 	}
2449 
2450 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2451 				     ndlp->nlp_DID, elscmd);
2452 	if (!elsiocb)
2453 		return 1;
2454 
2455 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2456 
2457 	/* For PRLI request, remainder of payload is service parameters */
2458 	memset(pcmd, 0, cmdsize);
2459 
2460 	if (local_nlp_type & NLP_FC4_FCP) {
2461 		/* Remainder of payload is FCP PRLI parameter page.
2462 		 * Note: this data structure is defined as
2463 		 * BE/LE in the structure definition so no
2464 		 * byte swap call is made.
2465 		 */
2466 		*((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2467 		pcmd += sizeof(uint32_t);
2468 		npr = (PRLI *)pcmd;
2469 
2470 		/*
2471 		 * If our firmware version is 3.20 or later,
2472 		 * set the following bits for FC-TAPE support.
2473 		 */
2474 		if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2475 			npr->ConfmComplAllowed = 1;
2476 			npr->Retry = 1;
2477 			npr->TaskRetryIdReq = 1;
2478 		}
2479 		npr->estabImagePair = 1;
2480 		npr->readXferRdyDis = 1;
2481 		if (vport->cfg_first_burst_size)
2482 			npr->writeXferRdyDis = 1;
2483 
2484 		/* For FCP support */
2485 		npr->prliType = PRLI_FCP_TYPE;
2486 		npr->initiatorFunc = 1;
2487 		elsiocb->iocb_flag |= LPFC_PRLI_FCP_REQ;
2488 
2489 		/* Remove FCP type - processed. */
2490 		local_nlp_type &= ~NLP_FC4_FCP;
2491 	} else if (local_nlp_type & NLP_FC4_NVME) {
2492 		/* Remainder of payload is NVME PRLI parameter page.
2493 		 * This data structure is the newer definition that
2494 		 * uses bf macros so a byte swap is required.
2495 		 */
2496 		*((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2497 		pcmd += sizeof(uint32_t);
2498 		npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2499 		bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2500 		bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
2501 		if (phba->nsler) {
2502 			bf_set(prli_nsler, npr_nvme, 1);
2503 			bf_set(prli_conf, npr_nvme, 1);
2504 		}
2505 
2506 		/* Only initiators request first burst. */
2507 		if ((phba->cfg_nvme_enable_fb) &&
2508 		    !phba->nvmet_support)
2509 			bf_set(prli_fba, npr_nvme, 1);
2510 
2511 		if (phba->nvmet_support) {
2512 			bf_set(prli_tgt, npr_nvme, 1);
2513 			bf_set(prli_disc, npr_nvme, 1);
2514 		} else {
2515 			bf_set(prli_init, npr_nvme, 1);
2516 			bf_set(prli_conf, npr_nvme, 1);
2517 		}
2518 
2519 		npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2520 		npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2521 		elsiocb->iocb_flag |= LPFC_PRLI_NVME_REQ;
2522 
2523 		/* Remove NVME type - processed. */
2524 		local_nlp_type &= ~NLP_FC4_NVME;
2525 	}
2526 
2527 	phba->fc_stat.elsXmitPRLI++;
2528 	elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
2529 	spin_lock_irq(&ndlp->lock);
2530 	ndlp->nlp_flag |= NLP_PRLI_SND;
2531 
2532 	/* The vport counters are used for lpfc_scan_finished, but
2533 	 * the ndlp is used to track outstanding PRLIs for different
2534 	 * FC4 types.
2535 	 */
2536 	vport->fc_prli_sent++;
2537 	ndlp->fc4_prli_sent++;
2538 	spin_unlock_irq(&ndlp->lock);
2539 
2540 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2541 			      "Issue PRLI:  did:x%x refcnt %d",
2542 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2543 	elsiocb->context1 = lpfc_nlp_get(ndlp);
2544 	if (!elsiocb->context1) {
2545 		lpfc_els_free_iocb(phba, elsiocb);
2546 		goto err;
2547 	}
2548 
2549 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2550 	if (rc == IOCB_ERROR) {
2551 		lpfc_els_free_iocb(phba, elsiocb);
2552 		lpfc_nlp_put(ndlp);
2553 		goto err;
2554 	}
2555 
2556 
2557 	/* The driver supports 2 FC4 types.  Make sure
2558 	 * a PRLI is issued for all types before exiting.
2559 	 */
2560 	if (phba->sli_rev == LPFC_SLI_REV4 &&
2561 	    local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
2562 		goto send_next_prli;
2563 	else
2564 		return 0;
2565 
2566 err:
2567 	spin_lock_irq(&ndlp->lock);
2568 	ndlp->nlp_flag &= ~NLP_PRLI_SND;
2569 	spin_unlock_irq(&ndlp->lock);
2570 	return 1;
2571 }
2572 
2573 /**
2574  * lpfc_rscn_disc - Perform rscn discovery for a vport
2575  * @vport: pointer to a host virtual N_Port data structure.
2576  *
2577  * This routine performs Registration State Change Notification (RSCN)
2578  * discovery for a @vport. If the @vport's node port recovery count is not
2579  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2580  * the nodes that need recovery. If none of the PLOGI were needed through
2581  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2582  * invoked to check and handle possible more RSCN came in during the period
2583  * of processing the current ones.
2584  **/
2585 static void
2586 lpfc_rscn_disc(struct lpfc_vport *vport)
2587 {
2588 	lpfc_can_disctmo(vport);
2589 
2590 	/* RSCN discovery */
2591 	/* go thru NPR nodes and issue ELS PLOGIs */
2592 	if (vport->fc_npr_cnt)
2593 		if (lpfc_els_disc_plogi(vport))
2594 			return;
2595 
2596 	lpfc_end_rscn(vport);
2597 }
2598 
2599 /**
2600  * lpfc_adisc_done - Complete the adisc phase of discovery
2601  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2602  *
2603  * This function is called when the final ADISC is completed during discovery.
2604  * This function handles clearing link attention or issuing reg_vpi depending
2605  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2606  * discovery.
2607  * This function is called with no locks held.
2608  **/
2609 static void
2610 lpfc_adisc_done(struct lpfc_vport *vport)
2611 {
2612 	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
2613 	struct lpfc_hba   *phba = vport->phba;
2614 
2615 	/*
2616 	 * For NPIV, cmpl_reg_vpi will set port_state to READY,
2617 	 * and continue discovery.
2618 	 */
2619 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2620 	    !(vport->fc_flag & FC_RSCN_MODE) &&
2621 	    (phba->sli_rev < LPFC_SLI_REV4)) {
2622 
2623 		/*
2624 		 * If link is down, clear_la and reg_vpi will be done after
2625 		 * flogi following a link up event
2626 		 */
2627 		if (!lpfc_is_link_up(phba))
2628 			return;
2629 
2630 		/* The ADISCs are complete.  Doesn't matter if they
2631 		 * succeeded or failed because the ADISC completion
2632 		 * routine guarantees to call the state machine and
2633 		 * the RPI is either unregistered (failed ADISC response)
2634 		 * or the RPI is still valid and the node is marked
2635 		 * mapped for a target.  The exchanges should be in the
2636 		 * correct state. This code is specific to SLI3.
2637 		 */
2638 		lpfc_issue_clear_la(phba, vport);
2639 		lpfc_issue_reg_vpi(phba, vport);
2640 		return;
2641 	}
2642 	/*
2643 	* For SLI2, we need to set port_state to READY
2644 	* and continue discovery.
2645 	*/
2646 	if (vport->port_state < LPFC_VPORT_READY) {
2647 		/* If we get here, there is nothing to ADISC */
2648 		lpfc_issue_clear_la(phba, vport);
2649 		if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2650 			vport->num_disc_nodes = 0;
2651 			/* go thru NPR list, issue ELS PLOGIs */
2652 			if (vport->fc_npr_cnt)
2653 				lpfc_els_disc_plogi(vport);
2654 			if (!vport->num_disc_nodes) {
2655 				spin_lock_irq(shost->host_lock);
2656 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
2657 				spin_unlock_irq(shost->host_lock);
2658 				lpfc_can_disctmo(vport);
2659 				lpfc_end_rscn(vport);
2660 			}
2661 		}
2662 		vport->port_state = LPFC_VPORT_READY;
2663 	} else
2664 		lpfc_rscn_disc(vport);
2665 }
2666 
2667 /**
2668  * lpfc_more_adisc - Issue more adisc as needed
2669  * @vport: pointer to a host virtual N_Port data structure.
2670  *
2671  * This routine determines whether there are more ndlps on a @vport
2672  * node list need to have Address Discover (ADISC) issued. If so, it will
2673  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2674  * remaining nodes which need to have ADISC sent.
2675  **/
2676 void
2677 lpfc_more_adisc(struct lpfc_vport *vport)
2678 {
2679 	if (vport->num_disc_nodes)
2680 		vport->num_disc_nodes--;
2681 	/* Continue discovery with <num_disc_nodes> ADISCs to go */
2682 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2683 			 "0210 Continue discovery with %d ADISCs to go "
2684 			 "Data: x%x x%x x%x\n",
2685 			 vport->num_disc_nodes, vport->fc_adisc_cnt,
2686 			 vport->fc_flag, vport->port_state);
2687 	/* Check to see if there are more ADISCs to be sent */
2688 	if (vport->fc_flag & FC_NLP_MORE) {
2689 		lpfc_set_disctmo(vport);
2690 		/* go thru NPR nodes and issue any remaining ELS ADISCs */
2691 		lpfc_els_disc_adisc(vport);
2692 	}
2693 	if (!vport->num_disc_nodes)
2694 		lpfc_adisc_done(vport);
2695 	return;
2696 }
2697 
2698 /**
2699  * lpfc_cmpl_els_adisc - Completion callback function for adisc
2700  * @phba: pointer to lpfc hba data structure.
2701  * @cmdiocb: pointer to lpfc command iocb data structure.
2702  * @rspiocb: pointer to lpfc response iocb data structure.
2703  *
2704  * This routine is the completion function for issuing the Address Discover
2705  * (ADISC) command. It first checks to see whether link went down during
2706  * the discovery process. If so, the node will be marked as node port
2707  * recovery for issuing discover IOCB by the link attention handler and
2708  * exit. Otherwise, the response status is checked. If error was reported
2709  * in the response status, the ADISC command shall be retried by invoking
2710  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2711  * the response status, the state machine is invoked to set transition
2712  * with respect to NLP_EVT_CMPL_ADISC event.
2713  **/
2714 static void
2715 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2716 		    struct lpfc_iocbq *rspiocb)
2717 {
2718 	struct lpfc_vport *vport = cmdiocb->vport;
2719 	IOCB_t *irsp;
2720 	struct lpfc_nodelist *ndlp;
2721 	int  disc;
2722 
2723 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2724 	cmdiocb->context_un.rsp_iocb = rspiocb;
2725 
2726 	irsp = &(rspiocb->iocb);
2727 	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2728 
2729 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2730 		"ADISC cmpl:      status:x%x/x%x did:x%x",
2731 		irsp->ulpStatus, irsp->un.ulpWord[4],
2732 		ndlp->nlp_DID);
2733 
2734 	/* Since ndlp can be freed in the disc state machine, note if this node
2735 	 * is being used during discovery.
2736 	 */
2737 	spin_lock_irq(&ndlp->lock);
2738 	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2739 	ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2740 	spin_unlock_irq(&ndlp->lock);
2741 	/* ADISC completes to NPort <nlp_DID> */
2742 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2743 			 "0104 ADISC completes to NPort x%x "
2744 			 "Data: x%x x%x x%x x%x x%x\n",
2745 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2746 			 irsp->ulpTimeout, disc, vport->num_disc_nodes);
2747 	/* Check to see if link went down during discovery */
2748 	if (lpfc_els_chk_latt(vport)) {
2749 		spin_lock_irq(&ndlp->lock);
2750 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2751 		spin_unlock_irq(&ndlp->lock);
2752 		goto out;
2753 	}
2754 
2755 	if (irsp->ulpStatus) {
2756 		/* Check for retry */
2757 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2758 			/* ELS command is being retried */
2759 			if (disc) {
2760 				spin_lock_irq(&ndlp->lock);
2761 				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2762 				spin_unlock_irq(&ndlp->lock);
2763 				lpfc_set_disctmo(vport);
2764 			}
2765 			goto out;
2766 		}
2767 		/* ADISC failed */
2768 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2769 				 "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2770 				 ndlp->nlp_DID, irsp->ulpStatus,
2771 				 irsp->un.ulpWord[4]);
2772 
2773 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2774 				NLP_EVT_CMPL_ADISC);
2775 
2776 		/* As long as this node is not registered with the SCSI or NVMe
2777 		 * transport, it is no longer an active node. Otherwise
2778 		 * devloss handles the final cleanup.
2779 		 */
2780 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2781 			spin_lock_irq(&ndlp->lock);
2782 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2783 			spin_unlock_irq(&ndlp->lock);
2784 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2785 						NLP_EVT_DEVICE_RM);
2786 		}
2787 	} else
2788 		/* Good status, call state machine */
2789 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2790 					NLP_EVT_CMPL_ADISC);
2791 
2792 	/* Check to see if there are more ADISCs to be sent */
2793 	if (disc && vport->num_disc_nodes)
2794 		lpfc_more_adisc(vport);
2795 out:
2796 	lpfc_els_free_iocb(phba, cmdiocb);
2797 	lpfc_nlp_put(ndlp);
2798 	return;
2799 }
2800 
2801 /**
2802  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2803  * @vport: pointer to a virtual N_Port data structure.
2804  * @ndlp: pointer to a node-list data structure.
2805  * @retry: number of retries to the command IOCB.
2806  *
2807  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2808  * @vport. It prepares the payload of the ADISC ELS command, updates the
2809  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2810  * to issue the ADISC ELS command.
2811  *
2812  * Note that the ndlp reference count will be incremented by 1 for holding the
2813  * ndlp and the reference to ndlp will be stored into the context1 field of
2814  * the IOCB for the completion callback function to the ADISC ELS command.
2815  *
2816  * Return code
2817  *   0 - successfully issued adisc
2818  *   1 - failed to issue adisc
2819  **/
2820 int
2821 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2822 		     uint8_t retry)
2823 {
2824 	int rc = 0;
2825 	struct lpfc_hba  *phba = vport->phba;
2826 	ADISC *ap;
2827 	struct lpfc_iocbq *elsiocb;
2828 	uint8_t *pcmd;
2829 	uint16_t cmdsize;
2830 
2831 	cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2832 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2833 				     ndlp->nlp_DID, ELS_CMD_ADISC);
2834 	if (!elsiocb)
2835 		return 1;
2836 
2837 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2838 
2839 	/* For ADISC request, remainder of payload is service parameters */
2840 	*((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2841 	pcmd += sizeof(uint32_t);
2842 
2843 	/* Fill in ADISC payload */
2844 	ap = (ADISC *) pcmd;
2845 	ap->hardAL_PA = phba->fc_pref_ALPA;
2846 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2847 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2848 	ap->DID = be32_to_cpu(vport->fc_myDID);
2849 
2850 	phba->fc_stat.elsXmitADISC++;
2851 	elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2852 	spin_lock_irq(&ndlp->lock);
2853 	ndlp->nlp_flag |= NLP_ADISC_SND;
2854 	spin_unlock_irq(&ndlp->lock);
2855 	elsiocb->context1 = lpfc_nlp_get(ndlp);
2856 	if (!elsiocb->context1) {
2857 		lpfc_els_free_iocb(phba, elsiocb);
2858 		goto err;
2859 	}
2860 
2861 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2862 			      "Issue ADISC:   did:x%x refcnt %d",
2863 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2864 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2865 	if (rc == IOCB_ERROR) {
2866 		lpfc_els_free_iocb(phba, elsiocb);
2867 		lpfc_nlp_put(ndlp);
2868 		goto err;
2869 	}
2870 
2871 	return 0;
2872 
2873 err:
2874 	spin_lock_irq(&ndlp->lock);
2875 	ndlp->nlp_flag &= ~NLP_ADISC_SND;
2876 	spin_unlock_irq(&ndlp->lock);
2877 	return 1;
2878 }
2879 
2880 /**
2881  * lpfc_cmpl_els_logo - Completion callback function for logo
2882  * @phba: pointer to lpfc hba data structure.
2883  * @cmdiocb: pointer to lpfc command iocb data structure.
2884  * @rspiocb: pointer to lpfc response iocb data structure.
2885  *
2886  * This routine is the completion function for issuing the ELS Logout (LOGO)
2887  * command. If no error status was reported from the LOGO response, the
2888  * state machine of the associated ndlp shall be invoked for transition with
2889  * respect to NLP_EVT_CMPL_LOGO event.
2890  **/
2891 static void
2892 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2893 		   struct lpfc_iocbq *rspiocb)
2894 {
2895 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2896 	struct lpfc_vport *vport = ndlp->vport;
2897 	IOCB_t *irsp;
2898 	unsigned long flags;
2899 	uint32_t skip_recovery = 0;
2900 	int wake_up_waiter = 0;
2901 
2902 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2903 	cmdiocb->context_un.rsp_iocb = rspiocb;
2904 
2905 	irsp = &(rspiocb->iocb);
2906 	spin_lock_irq(&ndlp->lock);
2907 	ndlp->nlp_flag &= ~NLP_LOGO_SND;
2908 	if (ndlp->save_flags & NLP_WAIT_FOR_LOGO) {
2909 		wake_up_waiter = 1;
2910 		ndlp->save_flags &= ~NLP_WAIT_FOR_LOGO;
2911 	}
2912 	spin_unlock_irq(&ndlp->lock);
2913 
2914 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2915 		"LOGO cmpl:       status:x%x/x%x did:x%x",
2916 		irsp->ulpStatus, irsp->un.ulpWord[4],
2917 		ndlp->nlp_DID);
2918 
2919 	/* LOGO completes to NPort <nlp_DID> */
2920 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2921 			 "0105 LOGO completes to NPort x%x "
2922 			 "refcnt %d nflags x%x Data: x%x x%x x%x x%x\n",
2923 			 ndlp->nlp_DID, kref_read(&ndlp->kref), ndlp->nlp_flag,
2924 			 irsp->ulpStatus, irsp->un.ulpWord[4],
2925 			 irsp->ulpTimeout, vport->num_disc_nodes);
2926 
2927 	if (lpfc_els_chk_latt(vport)) {
2928 		skip_recovery = 1;
2929 		goto out;
2930 	}
2931 
2932 	/* The LOGO will not be retried on failure.  A LOGO was
2933 	 * issued to the remote rport and a ACC or RJT or no Answer are
2934 	 * all acceptable.  Note the failure and move forward with
2935 	 * discovery.  The PLOGI will retry.
2936 	 */
2937 	if (irsp->ulpStatus) {
2938 		/* LOGO failed */
2939 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2940 				 "2756 LOGO failure, No Retry DID:%06X Status:x%x/x%x\n",
2941 				 ndlp->nlp_DID, irsp->ulpStatus,
2942 				 irsp->un.ulpWord[4]);
2943 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2944 		if (lpfc_error_lost_link(irsp)) {
2945 			skip_recovery = 1;
2946 			goto out;
2947 		}
2948 	}
2949 
2950 	/* Call state machine. This will unregister the rpi if needed. */
2951 	lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
2952 
2953 	/* The driver sets this flag for an NPIV instance that doesn't want to
2954 	 * log into the remote port.
2955 	 */
2956 	if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2957 		spin_lock_irq(&ndlp->lock);
2958 		if (phba->sli_rev == LPFC_SLI_REV4)
2959 			ndlp->nlp_flag |= NLP_RELEASE_RPI;
2960 		ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2961 		spin_unlock_irq(&ndlp->lock);
2962 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2963 					NLP_EVT_DEVICE_RM);
2964 		lpfc_els_free_iocb(phba, cmdiocb);
2965 		lpfc_nlp_put(ndlp);
2966 
2967 		/* Presume the node was released. */
2968 		return;
2969 	}
2970 
2971 out:
2972 	/* Driver is done with the IO.  */
2973 	lpfc_els_free_iocb(phba, cmdiocb);
2974 	lpfc_nlp_put(ndlp);
2975 
2976 	/* At this point, the LOGO processing is complete. NOTE: For a
2977 	 * pt2pt topology, we are assuming the NPortID will only change
2978 	 * on link up processing. For a LOGO / PLOGI initiated by the
2979 	 * Initiator, we are assuming the NPortID is not going to change.
2980 	 */
2981 
2982 	if (wake_up_waiter && ndlp->logo_waitq)
2983 		wake_up(ndlp->logo_waitq);
2984 	/*
2985 	 * If the node is a target, the handling attempts to recover the port.
2986 	 * For any other port type, the rpi is unregistered as an implicit
2987 	 * LOGO.
2988 	 */
2989 	if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
2990 	    skip_recovery == 0) {
2991 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
2992 		spin_lock_irqsave(&ndlp->lock, flags);
2993 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2994 		spin_unlock_irqrestore(&ndlp->lock, flags);
2995 
2996 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2997 				 "3187 LOGO completes to NPort x%x: Start "
2998 				 "Recovery Data: x%x x%x x%x x%x\n",
2999 				 ndlp->nlp_DID, irsp->ulpStatus,
3000 				 irsp->un.ulpWord[4], irsp->ulpTimeout,
3001 				 vport->num_disc_nodes);
3002 		lpfc_disc_start(vport);
3003 		return;
3004 	}
3005 
3006 	/* Cleanup path for failed REG_RPI handling. If REG_RPI fails, the
3007 	 * driver sends a LOGO to the rport to cleanup.  For fabric and
3008 	 * initiator ports cleanup the node as long as it the node is not
3009 	 * register with the transport.
3010 	 */
3011 	if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
3012 		spin_lock_irq(&ndlp->lock);
3013 		ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
3014 		spin_unlock_irq(&ndlp->lock);
3015 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
3016 					NLP_EVT_DEVICE_RM);
3017 	}
3018 }
3019 
3020 /**
3021  * lpfc_issue_els_logo - Issue a logo to an node on a vport
3022  * @vport: pointer to a virtual N_Port data structure.
3023  * @ndlp: pointer to a node-list data structure.
3024  * @retry: number of retries to the command IOCB.
3025  *
3026  * This routine constructs and issues an ELS Logout (LOGO) iocb command
3027  * to a remote node, referred by an @ndlp on a @vport. It constructs the
3028  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
3029  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
3030  *
3031  * Note that the ndlp reference count will be incremented by 1 for holding the
3032  * ndlp and the reference to ndlp will be stored into the context1 field of
3033  * the IOCB for the completion callback function to the LOGO ELS command.
3034  *
3035  * Callers of this routine are expected to unregister the RPI first
3036  *
3037  * Return code
3038  *   0 - successfully issued logo
3039  *   1 - failed to issue logo
3040  **/
3041 int
3042 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3043 		    uint8_t retry)
3044 {
3045 	struct lpfc_hba  *phba = vport->phba;
3046 	struct lpfc_iocbq *elsiocb;
3047 	uint8_t *pcmd;
3048 	uint16_t cmdsize;
3049 	int rc;
3050 
3051 	spin_lock_irq(&ndlp->lock);
3052 	if (ndlp->nlp_flag & NLP_LOGO_SND) {
3053 		spin_unlock_irq(&ndlp->lock);
3054 		return 0;
3055 	}
3056 	spin_unlock_irq(&ndlp->lock);
3057 
3058 	cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
3059 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3060 				     ndlp->nlp_DID, ELS_CMD_LOGO);
3061 	if (!elsiocb)
3062 		return 1;
3063 
3064 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3065 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
3066 	pcmd += sizeof(uint32_t);
3067 
3068 	/* Fill in LOGO payload */
3069 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
3070 	pcmd += sizeof(uint32_t);
3071 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
3072 
3073 	phba->fc_stat.elsXmitLOGO++;
3074 	elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
3075 	spin_lock_irq(&ndlp->lock);
3076 	ndlp->nlp_flag |= NLP_LOGO_SND;
3077 	ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
3078 	spin_unlock_irq(&ndlp->lock);
3079 	elsiocb->context1 = lpfc_nlp_get(ndlp);
3080 	if (!elsiocb->context1) {
3081 		lpfc_els_free_iocb(phba, elsiocb);
3082 		goto err;
3083 	}
3084 
3085 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3086 			      "Issue LOGO:      did:x%x refcnt %d",
3087 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3088 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3089 	if (rc == IOCB_ERROR) {
3090 		lpfc_els_free_iocb(phba, elsiocb);
3091 		lpfc_nlp_put(ndlp);
3092 		goto err;
3093 	}
3094 
3095 	spin_lock_irq(&ndlp->lock);
3096 	ndlp->nlp_prev_state = ndlp->nlp_state;
3097 	spin_unlock_irq(&ndlp->lock);
3098 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3099 	return 0;
3100 
3101 err:
3102 	spin_lock_irq(&ndlp->lock);
3103 	ndlp->nlp_flag &= ~NLP_LOGO_SND;
3104 	spin_unlock_irq(&ndlp->lock);
3105 	return 1;
3106 }
3107 
3108 /**
3109  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
3110  * @phba: pointer to lpfc hba data structure.
3111  * @cmdiocb: pointer to lpfc command iocb data structure.
3112  * @rspiocb: pointer to lpfc response iocb data structure.
3113  *
3114  * This routine is a generic completion callback function for ELS commands.
3115  * Specifically, it is the callback function which does not need to perform
3116  * any command specific operations. It is currently used by the ELS command
3117  * issuing routines for RSCN, lpfc_issue_els_rscn, and the ELS Fibre Channel
3118  * Address Resolution Protocol Response (FARPR) routine, lpfc_issue_els_farpr().
3119  * Other than certain debug loggings, this callback function simply invokes the
3120  * lpfc_els_chk_latt() routine to check whether link went down during the
3121  * discovery process.
3122  **/
3123 static void
3124 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3125 		  struct lpfc_iocbq *rspiocb)
3126 {
3127 	struct lpfc_vport *vport = cmdiocb->vport;
3128 	struct lpfc_nodelist *free_ndlp;
3129 	IOCB_t *irsp;
3130 
3131 	irsp = &rspiocb->iocb;
3132 
3133 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3134 			      "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3135 			      irsp->ulpStatus, irsp->un.ulpWord[4],
3136 			      irsp->un.elsreq64.remoteID);
3137 
3138 	/* ELS cmd tag <ulpIoTag> completes */
3139 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3140 			 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
3141 			 irsp->ulpIoTag, irsp->ulpStatus,
3142 			 irsp->un.ulpWord[4], irsp->ulpTimeout);
3143 
3144 	/* Check to see if link went down during discovery */
3145 	lpfc_els_chk_latt(vport);
3146 
3147 	free_ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
3148 
3149 	lpfc_els_free_iocb(phba, cmdiocb);
3150 	lpfc_nlp_put(free_ndlp);
3151 }
3152 
3153 /**
3154  * lpfc_reg_fab_ctrl_node - RPI register the fabric controller node.
3155  * @vport: pointer to lpfc_vport data structure.
3156  * @fc_ndlp: pointer to the fabric controller (0xfffffd) node.
3157  *
3158  * This routine registers the rpi assigned to the fabric controller
3159  * NPort_ID (0xfffffd) with the port and moves the node to UNMAPPED
3160  * state triggering a registration with the SCSI transport.
3161  *
3162  * This routine is single out because the fabric controller node
3163  * does not receive a PLOGI.  This routine is consumed by the
3164  * SCR and RDF ELS commands.  Callers are expected to qualify
3165  * with SLI4 first.
3166  **/
3167 static int
3168 lpfc_reg_fab_ctrl_node(struct lpfc_vport *vport, struct lpfc_nodelist *fc_ndlp)
3169 {
3170 	int rc = 0;
3171 	struct lpfc_hba *phba = vport->phba;
3172 	struct lpfc_nodelist *ns_ndlp;
3173 	LPFC_MBOXQ_t *mbox;
3174 	struct lpfc_dmabuf *mp;
3175 
3176 	if (fc_ndlp->nlp_flag & NLP_RPI_REGISTERED)
3177 		return rc;
3178 
3179 	ns_ndlp = lpfc_findnode_did(vport, NameServer_DID);
3180 	if (!ns_ndlp)
3181 		return -ENODEV;
3182 
3183 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3184 			 "0935 %s: Reg FC RPI x%x on FC DID x%x NSSte: x%x\n",
3185 			 __func__, fc_ndlp->nlp_rpi, fc_ndlp->nlp_DID,
3186 			 ns_ndlp->nlp_state);
3187 	if (ns_ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
3188 		return -ENODEV;
3189 
3190 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3191 	if (!mbox) {
3192 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3193 				 "0936 %s: no memory for reg_login "
3194 				 "Data: x%x x%x x%x x%x\n", __func__,
3195 				 fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3196 				 fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3197 		return -ENOMEM;
3198 	}
3199 	rc = lpfc_reg_rpi(phba, vport->vpi, fc_ndlp->nlp_DID,
3200 			  (u8 *)&vport->fc_sparam, mbox, fc_ndlp->nlp_rpi);
3201 	if (rc) {
3202 		rc = -EACCES;
3203 		goto out;
3204 	}
3205 
3206 	fc_ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
3207 	mbox->mbox_cmpl = lpfc_mbx_cmpl_fc_reg_login;
3208 	mbox->ctx_ndlp = lpfc_nlp_get(fc_ndlp);
3209 	if (!mbox->ctx_ndlp) {
3210 		rc = -ENOMEM;
3211 		goto out_mem;
3212 	}
3213 
3214 	mbox->vport = vport;
3215 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3216 	if (rc == MBX_NOT_FINISHED) {
3217 		rc = -ENODEV;
3218 		lpfc_nlp_put(fc_ndlp);
3219 		goto out_mem;
3220 	}
3221 	/* Success path. Exit. */
3222 	lpfc_nlp_set_state(vport, fc_ndlp,
3223 			   NLP_STE_REG_LOGIN_ISSUE);
3224 	return 0;
3225 
3226  out_mem:
3227 	fc_ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3228 	mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
3229 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3230 	kfree(mp);
3231 
3232  out:
3233 	mempool_free(mbox, phba->mbox_mem_pool);
3234 	lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3235 			 "0938 %s: failed to format reg_login "
3236 			 "Data: x%x x%x x%x x%x\n", __func__,
3237 			 fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3238 			 fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3239 	return rc;
3240 }
3241 
3242 /**
3243  * lpfc_cmpl_els_disc_cmd - Completion callback function for Discovery ELS cmd
3244  * @phba: pointer to lpfc hba data structure.
3245  * @cmdiocb: pointer to lpfc command iocb data structure.
3246  * @rspiocb: pointer to lpfc response iocb data structure.
3247  *
3248  * This routine is a generic completion callback function for Discovery ELS cmd.
3249  * Currently used by the ELS command issuing routines for the ELS State Change
3250  * Request (SCR), lpfc_issue_els_scr() and the ELS RDF, lpfc_issue_els_rdf().
3251  * These commands will be retried once only for ELS timeout errors.
3252  **/
3253 static void
3254 lpfc_cmpl_els_disc_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3255 		       struct lpfc_iocbq *rspiocb)
3256 {
3257 	struct lpfc_vport *vport = cmdiocb->vport;
3258 	IOCB_t *irsp;
3259 	struct lpfc_els_rdf_rsp *prdf;
3260 	struct lpfc_dmabuf *pcmd, *prsp;
3261 	u32 *pdata;
3262 	u32 cmd;
3263 	struct lpfc_nodelist *ndlp = cmdiocb->context1;
3264 
3265 	irsp = &rspiocb->iocb;
3266 
3267 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3268 		"ELS cmd cmpl:    status:x%x/x%x did:x%x",
3269 		irsp->ulpStatus, irsp->un.ulpWord[4],
3270 		irsp->un.elsreq64.remoteID);
3271 	/* ELS cmd tag <ulpIoTag> completes */
3272 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3273 			 "0217 ELS cmd tag x%x completes Data: x%x x%x x%x "
3274 			 "x%x\n",
3275 			 irsp->ulpIoTag, irsp->ulpStatus,
3276 			 irsp->un.ulpWord[4], irsp->ulpTimeout,
3277 			 cmdiocb->retry);
3278 
3279 	pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
3280 	if (!pcmd)
3281 		goto out;
3282 
3283 	pdata = (u32 *)pcmd->virt;
3284 	if (!pdata)
3285 		goto out;
3286 	cmd = *pdata;
3287 
3288 	/* Only 1 retry for ELS Timeout only */
3289 	if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
3290 	    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3291 	    IOERR_SEQUENCE_TIMEOUT)) {
3292 		cmdiocb->retry++;
3293 		if (cmdiocb->retry <= 1) {
3294 			switch (cmd) {
3295 			case ELS_CMD_SCR:
3296 				lpfc_issue_els_scr(vport, cmdiocb->retry);
3297 				break;
3298 			case ELS_CMD_EDC:
3299 				lpfc_issue_els_edc(vport, cmdiocb->retry);
3300 				break;
3301 			case ELS_CMD_RDF:
3302 				cmdiocb->context1 = NULL; /* save ndlp refcnt */
3303 				lpfc_issue_els_rdf(vport, cmdiocb->retry);
3304 				break;
3305 			}
3306 			goto out;
3307 		}
3308 		phba->fc_stat.elsRetryExceeded++;
3309 	}
3310 	if (cmd == ELS_CMD_EDC) {
3311 		/* must be called before checking uplStatus and returning */
3312 		lpfc_cmpl_els_edc(phba, cmdiocb, rspiocb);
3313 		return;
3314 	}
3315 	if (irsp->ulpStatus) {
3316 		/* ELS discovery cmd completes with error */
3317 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
3318 				 "4203 ELS cmd x%x error: x%x x%X\n", cmd,
3319 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
3320 		goto out;
3321 	}
3322 
3323 	/* The RDF response doesn't have any impact on the running driver
3324 	 * but the notification descriptors are dumped here for support.
3325 	 */
3326 	if (cmd == ELS_CMD_RDF) {
3327 		int i;
3328 
3329 		prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
3330 		if (!prsp)
3331 			goto out;
3332 
3333 		prdf = (struct lpfc_els_rdf_rsp *)prsp->virt;
3334 		if (!prdf)
3335 			goto out;
3336 
3337 		for (i = 0; i < ELS_RDF_REG_TAG_CNT &&
3338 			    i < be32_to_cpu(prdf->reg_d1.reg_desc.count); i++)
3339 			lpfc_printf_vlog(vport, KERN_INFO,
3340 					 LOG_ELS | LOG_CGN_MGMT,
3341 					 "4677 Fabric RDF Notification Grant "
3342 					 "Data: 0x%08x Reg: %x %x\n",
3343 					 be32_to_cpu(
3344 						prdf->reg_d1.desc_tags[i]),
3345 					 phba->cgn_reg_signal,
3346 					 phba->cgn_reg_fpin);
3347 	}
3348 
3349 out:
3350 	/* Check to see if link went down during discovery */
3351 	lpfc_els_chk_latt(vport);
3352 	lpfc_els_free_iocb(phba, cmdiocb);
3353 	lpfc_nlp_put(ndlp);
3354 	return;
3355 }
3356 
3357 /**
3358  * lpfc_issue_els_scr - Issue a scr to an node on a vport
3359  * @vport: pointer to a host virtual N_Port data structure.
3360  * @retry: retry counter for the command IOCB.
3361  *
3362  * This routine issues a State Change Request (SCR) to a fabric node
3363  * on a @vport. The remote node is Fabric Controller (0xfffffd). It
3364  * first search the @vport node list to find the matching ndlp. If no such
3365  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
3366  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
3367  * routine is invoked to send the SCR IOCB.
3368  *
3369  * Note that the ndlp reference count will be incremented by 1 for holding the
3370  * ndlp and the reference to ndlp will be stored into the context1 field of
3371  * the IOCB for the completion callback function to the SCR ELS command.
3372  *
3373  * Return code
3374  *   0 - Successfully issued scr command
3375  *   1 - Failed to issue scr command
3376  **/
3377 int
3378 lpfc_issue_els_scr(struct lpfc_vport *vport, uint8_t retry)
3379 {
3380 	int rc = 0;
3381 	struct lpfc_hba  *phba = vport->phba;
3382 	struct lpfc_iocbq *elsiocb;
3383 	uint8_t *pcmd;
3384 	uint16_t cmdsize;
3385 	struct lpfc_nodelist *ndlp;
3386 
3387 	cmdsize = (sizeof(uint32_t) + sizeof(SCR));
3388 
3389 	ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3390 	if (!ndlp) {
3391 		ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3392 		if (!ndlp)
3393 			return 1;
3394 		lpfc_enqueue_node(vport, ndlp);
3395 	}
3396 
3397 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3398 				     ndlp->nlp_DID, ELS_CMD_SCR);
3399 	if (!elsiocb)
3400 		return 1;
3401 
3402 	if (phba->sli_rev == LPFC_SLI_REV4) {
3403 		rc = lpfc_reg_fab_ctrl_node(vport, ndlp);
3404 		if (rc) {
3405 			lpfc_els_free_iocb(phba, elsiocb);
3406 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3407 					 "0937 %s: Failed to reg fc node, rc %d\n",
3408 					 __func__, rc);
3409 			return 1;
3410 		}
3411 	}
3412 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3413 
3414 	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
3415 	pcmd += sizeof(uint32_t);
3416 
3417 	/* For SCR, remainder of payload is SCR parameter page */
3418 	memset(pcmd, 0, sizeof(SCR));
3419 	((SCR *) pcmd)->Function = SCR_FUNC_FULL;
3420 
3421 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3422 		"Issue SCR:       did:x%x",
3423 		ndlp->nlp_DID, 0, 0);
3424 
3425 	phba->fc_stat.elsXmitSCR++;
3426 	elsiocb->iocb_cmpl = lpfc_cmpl_els_disc_cmd;
3427 	elsiocb->context1 = lpfc_nlp_get(ndlp);
3428 	if (!elsiocb->context1) {
3429 		lpfc_els_free_iocb(phba, elsiocb);
3430 		return 1;
3431 	}
3432 
3433 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3434 			      "Issue SCR:     did:x%x refcnt %d",
3435 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3436 
3437 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3438 	if (rc == IOCB_ERROR) {
3439 		lpfc_els_free_iocb(phba, elsiocb);
3440 		lpfc_nlp_put(ndlp);
3441 		return 1;
3442 	}
3443 
3444 	return 0;
3445 }
3446 
3447 /**
3448  * lpfc_issue_els_rscn - Issue an RSCN to the Fabric Controller (Fabric)
3449  *   or the other nport (pt2pt).
3450  * @vport: pointer to a host virtual N_Port data structure.
3451  * @retry: number of retries to the command IOCB.
3452  *
3453  * This routine issues a RSCN to the Fabric Controller (DID 0xFFFFFD)
3454  *  when connected to a fabric, or to the remote port when connected
3455  *  in point-to-point mode. When sent to the Fabric Controller, it will
3456  *  replay the RSCN to registered recipients.
3457  *
3458  * Note that the ndlp reference count will be incremented by 1 for holding the
3459  * ndlp and the reference to ndlp will be stored into the context1 field of
3460  * the IOCB for the completion callback function to the RSCN ELS command.
3461  *
3462  * Return code
3463  *   0 - Successfully issued RSCN command
3464  *   1 - Failed to issue RSCN command
3465  **/
3466 int
3467 lpfc_issue_els_rscn(struct lpfc_vport *vport, uint8_t retry)
3468 {
3469 	int rc = 0;
3470 	struct lpfc_hba *phba = vport->phba;
3471 	struct lpfc_iocbq *elsiocb;
3472 	struct lpfc_nodelist *ndlp;
3473 	struct {
3474 		struct fc_els_rscn rscn;
3475 		struct fc_els_rscn_page portid;
3476 	} *event;
3477 	uint32_t nportid;
3478 	uint16_t cmdsize = sizeof(*event);
3479 
3480 	/* Not supported for private loop */
3481 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
3482 	    !(vport->fc_flag & FC_PUBLIC_LOOP))
3483 		return 1;
3484 
3485 	if (vport->fc_flag & FC_PT2PT) {
3486 		/* find any mapped nport - that would be the other nport */
3487 		ndlp = lpfc_findnode_mapped(vport);
3488 		if (!ndlp)
3489 			return 1;
3490 	} else {
3491 		nportid = FC_FID_FCTRL;
3492 		/* find the fabric controller node */
3493 		ndlp = lpfc_findnode_did(vport, nportid);
3494 		if (!ndlp) {
3495 			/* if one didn't exist, make one */
3496 			ndlp = lpfc_nlp_init(vport, nportid);
3497 			if (!ndlp)
3498 				return 1;
3499 			lpfc_enqueue_node(vport, ndlp);
3500 		}
3501 	}
3502 
3503 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3504 				     ndlp->nlp_DID, ELS_CMD_RSCN_XMT);
3505 
3506 	if (!elsiocb)
3507 		return 1;
3508 
3509 	event = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
3510 
3511 	event->rscn.rscn_cmd = ELS_RSCN;
3512 	event->rscn.rscn_page_len = sizeof(struct fc_els_rscn_page);
3513 	event->rscn.rscn_plen = cpu_to_be16(cmdsize);
3514 
3515 	nportid = vport->fc_myDID;
3516 	/* appears that page flags must be 0 for fabric to broadcast RSCN */
3517 	event->portid.rscn_page_flags = 0;
3518 	event->portid.rscn_fid[0] = (nportid & 0x00FF0000) >> 16;
3519 	event->portid.rscn_fid[1] = (nportid & 0x0000FF00) >> 8;
3520 	event->portid.rscn_fid[2] = nportid & 0x000000FF;
3521 
3522 	phba->fc_stat.elsXmitRSCN++;
3523 	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3524 	elsiocb->context1 = lpfc_nlp_get(ndlp);
3525 	if (!elsiocb->context1) {
3526 		lpfc_els_free_iocb(phba, elsiocb);
3527 		return 1;
3528 	}
3529 
3530 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3531 			      "Issue RSCN:       did:x%x",
3532 			      ndlp->nlp_DID, 0, 0);
3533 
3534 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3535 	if (rc == IOCB_ERROR) {
3536 		lpfc_els_free_iocb(phba, elsiocb);
3537 		lpfc_nlp_put(ndlp);
3538 		return 1;
3539 	}
3540 
3541 	/* This will cause the callback-function lpfc_cmpl_els_cmd to
3542 	 * trigger the release of node.
3543 	 */
3544 	if (!(vport->fc_flag & FC_PT2PT))
3545 		lpfc_nlp_put(ndlp);
3546 	return 0;
3547 }
3548 
3549 /**
3550  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
3551  * @vport: pointer to a host virtual N_Port data structure.
3552  * @nportid: N_Port identifier to the remote node.
3553  * @retry: number of retries to the command IOCB.
3554  *
3555  * This routine issues a Fibre Channel Address Resolution Response
3556  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
3557  * is passed into the function. It first search the @vport node list to find
3558  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
3559  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
3560  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
3561  *
3562  * Note that the ndlp reference count will be incremented by 1 for holding the
3563  * ndlp and the reference to ndlp will be stored into the context1 field of
3564  * the IOCB for the completion callback function to the FARPR ELS command.
3565  *
3566  * Return code
3567  *   0 - Successfully issued farpr command
3568  *   1 - Failed to issue farpr command
3569  **/
3570 static int
3571 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3572 {
3573 	int rc = 0;
3574 	struct lpfc_hba  *phba = vport->phba;
3575 	struct lpfc_iocbq *elsiocb;
3576 	FARP *fp;
3577 	uint8_t *pcmd;
3578 	uint32_t *lp;
3579 	uint16_t cmdsize;
3580 	struct lpfc_nodelist *ondlp;
3581 	struct lpfc_nodelist *ndlp;
3582 
3583 	cmdsize = (sizeof(uint32_t) + sizeof(FARP));
3584 
3585 	ndlp = lpfc_findnode_did(vport, nportid);
3586 	if (!ndlp) {
3587 		ndlp = lpfc_nlp_init(vport, nportid);
3588 		if (!ndlp)
3589 			return 1;
3590 		lpfc_enqueue_node(vport, ndlp);
3591 	}
3592 
3593 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3594 				     ndlp->nlp_DID, ELS_CMD_RNID);
3595 	if (!elsiocb)
3596 		return 1;
3597 
3598 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3599 
3600 	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
3601 	pcmd += sizeof(uint32_t);
3602 
3603 	/* Fill in FARPR payload */
3604 	fp = (FARP *) (pcmd);
3605 	memset(fp, 0, sizeof(FARP));
3606 	lp = (uint32_t *) pcmd;
3607 	*lp++ = be32_to_cpu(nportid);
3608 	*lp++ = be32_to_cpu(vport->fc_myDID);
3609 	fp->Rflags = 0;
3610 	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3611 
3612 	memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3613 	memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3614 	ondlp = lpfc_findnode_did(vport, nportid);
3615 	if (ondlp) {
3616 		memcpy(&fp->OportName, &ondlp->nlp_portname,
3617 		       sizeof(struct lpfc_name));
3618 		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
3619 		       sizeof(struct lpfc_name));
3620 	}
3621 
3622 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3623 		"Issue FARPR:     did:x%x",
3624 		ndlp->nlp_DID, 0, 0);
3625 
3626 	phba->fc_stat.elsXmitFARPR++;
3627 	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3628 	elsiocb->context1 = lpfc_nlp_get(ndlp);
3629 	if (!elsiocb->context1) {
3630 		lpfc_els_free_iocb(phba, elsiocb);
3631 		return 1;
3632 	}
3633 
3634 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3635 	if (rc == IOCB_ERROR) {
3636 		/* The additional lpfc_nlp_put will cause the following
3637 		 * lpfc_els_free_iocb routine to trigger the release of
3638 		 * the node.
3639 		 */
3640 		lpfc_els_free_iocb(phba, elsiocb);
3641 		lpfc_nlp_put(ndlp);
3642 		return 1;
3643 	}
3644 	/* This will cause the callback-function lpfc_cmpl_els_cmd to
3645 	 * trigger the release of the node.
3646 	 */
3647 	/* Don't release reference count as RDF is likely outstanding */
3648 	return 0;
3649 }
3650 
3651 /**
3652  * lpfc_issue_els_rdf - Register for diagnostic functions from the fabric.
3653  * @vport: pointer to a host virtual N_Port data structure.
3654  * @retry: retry counter for the command IOCB.
3655  *
3656  * This routine issues an ELS RDF to the Fabric Controller to register
3657  * for diagnostic functions.
3658  *
3659  * Note that the ndlp reference count will be incremented by 1 for holding the
3660  * ndlp and the reference to ndlp will be stored into the context1 field of
3661  * the IOCB for the completion callback function to the RDF ELS command.
3662  *
3663  * Return code
3664  *   0 - Successfully issued rdf command
3665  *   1 - Failed to issue rdf command
3666  **/
3667 int
3668 lpfc_issue_els_rdf(struct lpfc_vport *vport, uint8_t retry)
3669 {
3670 	struct lpfc_hba *phba = vport->phba;
3671 	struct lpfc_iocbq *elsiocb;
3672 	struct lpfc_els_rdf_req *prdf;
3673 	struct lpfc_nodelist *ndlp;
3674 	uint16_t cmdsize;
3675 	int rc;
3676 
3677 	cmdsize = sizeof(*prdf);
3678 
3679 	ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3680 	if (!ndlp) {
3681 		ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3682 		if (!ndlp)
3683 			return -ENODEV;
3684 		lpfc_enqueue_node(vport, ndlp);
3685 	}
3686 
3687 	/* RDF ELS is not required on an NPIV VN_Port. */
3688 	if (vport->port_type == LPFC_NPIV_PORT)
3689 		return -EACCES;
3690 
3691 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3692 				     ndlp->nlp_DID, ELS_CMD_RDF);
3693 	if (!elsiocb)
3694 		return -ENOMEM;
3695 
3696 	/* Configure the payload for the supported FPIN events. */
3697 	prdf = (struct lpfc_els_rdf_req *)
3698 		(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
3699 	memset(prdf, 0, cmdsize);
3700 	prdf->rdf.fpin_cmd = ELS_RDF;
3701 	prdf->rdf.desc_len = cpu_to_be32(sizeof(struct lpfc_els_rdf_req) -
3702 					 sizeof(struct fc_els_rdf));
3703 	prdf->reg_d1.reg_desc.desc_tag = cpu_to_be32(ELS_DTAG_FPIN_REGISTER);
3704 	prdf->reg_d1.reg_desc.desc_len = cpu_to_be32(
3705 				FC_TLV_DESC_LENGTH_FROM_SZ(prdf->reg_d1));
3706 	prdf->reg_d1.reg_desc.count = cpu_to_be32(ELS_RDF_REG_TAG_CNT);
3707 	prdf->reg_d1.desc_tags[0] = cpu_to_be32(ELS_DTAG_LNK_INTEGRITY);
3708 	prdf->reg_d1.desc_tags[1] = cpu_to_be32(ELS_DTAG_DELIVERY);
3709 	prdf->reg_d1.desc_tags[2] = cpu_to_be32(ELS_DTAG_PEER_CONGEST);
3710 	prdf->reg_d1.desc_tags[3] = cpu_to_be32(ELS_DTAG_CONGESTION);
3711 
3712 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3713 			 "6444 Xmit RDF to remote NPORT x%x Reg: %x %x\n",
3714 			 ndlp->nlp_DID, phba->cgn_reg_signal,
3715 			 phba->cgn_reg_fpin);
3716 
3717 	phba->cgn_fpin_frequency = LPFC_FPIN_INIT_FREQ;
3718 	elsiocb->iocb_cmpl = lpfc_cmpl_els_disc_cmd;
3719 	elsiocb->context1 = lpfc_nlp_get(ndlp);
3720 	if (!elsiocb->context1) {
3721 		lpfc_els_free_iocb(phba, elsiocb);
3722 		return -EIO;
3723 	}
3724 
3725 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3726 			      "Issue RDF:     did:x%x refcnt %d",
3727 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3728 
3729 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3730 	if (rc == IOCB_ERROR) {
3731 		lpfc_els_free_iocb(phba, elsiocb);
3732 		lpfc_nlp_put(ndlp);
3733 		return -EIO;
3734 	}
3735 	return 0;
3736 }
3737 
3738  /**
3739   * lpfc_els_rcv_rdf - Receive RDF ELS request from the fabric.
3740   * @vport: pointer to a host virtual N_Port data structure.
3741   * @cmdiocb: pointer to lpfc command iocb data structure.
3742   * @ndlp: pointer to a node-list data structure.
3743   *
3744   * A received RDF implies a possible change to fabric supported diagnostic
3745   * functions.  This routine sends LS_ACC and then has the Nx_Port issue a new
3746   * RDF request to reregister for supported diagnostic functions.
3747   *
3748   * Return code
3749   *   0 - Success
3750   *   -EIO - Failed to process received RDF
3751   **/
3752 static int
3753 lpfc_els_rcv_rdf(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3754 		 struct lpfc_nodelist *ndlp)
3755 {
3756 	/* Send LS_ACC */
3757 	if (lpfc_els_rsp_acc(vport, ELS_CMD_RDF, cmdiocb, ndlp, NULL)) {
3758 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3759 				 "1623 Failed to RDF_ACC from x%x for x%x\n",
3760 				 ndlp->nlp_DID, vport->fc_myDID);
3761 		return -EIO;
3762 	}
3763 
3764 	/* Issue new RDF for reregistering */
3765 	if (lpfc_issue_els_rdf(vport, 0)) {
3766 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3767 				 "2623 Failed to re register RDF for x%x\n",
3768 				 vport->fc_myDID);
3769 		return -EIO;
3770 	}
3771 
3772 	return 0;
3773 }
3774 
3775 /**
3776  * lpfc_least_capable_settings - helper function for EDC rsp processing
3777  * @phba: pointer to lpfc hba data structure.
3778  * @pcgd: pointer to congestion detection descriptor in EDC rsp.
3779  *
3780  * This helper routine determines the least capable setting for
3781  * congestion signals, signal freq, including scale, from the
3782  * congestion detection descriptor in the EDC rsp.  The routine
3783  * sets @phba values in preparation for a set_featues mailbox.
3784  **/
3785 static void
3786 lpfc_least_capable_settings(struct lpfc_hba *phba,
3787 			    struct fc_diag_cg_sig_desc *pcgd)
3788 {
3789 	u32 rsp_sig_cap = 0, drv_sig_cap = 0;
3790 	u32 rsp_sig_freq_cyc = 0, rsp_sig_freq_scale = 0;
3791 	struct lpfc_cgn_info *cp;
3792 	u32 crc;
3793 	u16 sig_freq;
3794 
3795 	/* Get rsp signal and frequency capabilities.  */
3796 	rsp_sig_cap = be32_to_cpu(pcgd->xmt_signal_capability);
3797 	rsp_sig_freq_cyc = be16_to_cpu(pcgd->xmt_signal_frequency.count);
3798 	rsp_sig_freq_scale = be16_to_cpu(pcgd->xmt_signal_frequency.units);
3799 
3800 	/* If the Fport does not support signals. Set FPIN only */
3801 	if (rsp_sig_cap == EDC_CG_SIG_NOTSUPPORTED)
3802 		goto out_no_support;
3803 
3804 	/* Apply the xmt scale to the xmt cycle to get the correct frequency.
3805 	 * Adapter default is 100 millisSeconds.  Convert all xmt cycle values
3806 	 * to milliSeconds.
3807 	 */
3808 	switch (rsp_sig_freq_scale) {
3809 	case EDC_CG_SIGFREQ_SEC:
3810 		rsp_sig_freq_cyc *= MSEC_PER_SEC;
3811 		break;
3812 	case EDC_CG_SIGFREQ_MSEC:
3813 		rsp_sig_freq_cyc = 1;
3814 		break;
3815 	default:
3816 		goto out_no_support;
3817 	}
3818 
3819 	/* Convenient shorthand. */
3820 	drv_sig_cap = phba->cgn_reg_signal;
3821 
3822 	/* Choose the least capable frequency. */
3823 	if (rsp_sig_freq_cyc > phba->cgn_sig_freq)
3824 		phba->cgn_sig_freq = rsp_sig_freq_cyc;
3825 
3826 	/* Should be some common signals support. Settle on least capable
3827 	 * signal and adjust FPIN values. Initialize defaults to ease the
3828 	 * decision.
3829 	 */
3830 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
3831 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
3832 	if (rsp_sig_cap == EDC_CG_SIG_WARN_ONLY &&
3833 	    (drv_sig_cap == EDC_CG_SIG_WARN_ONLY ||
3834 	     drv_sig_cap == EDC_CG_SIG_WARN_ALARM)) {
3835 		phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
3836 		phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
3837 	}
3838 	if (rsp_sig_cap == EDC_CG_SIG_WARN_ALARM) {
3839 		if (drv_sig_cap == EDC_CG_SIG_WARN_ALARM) {
3840 			phba->cgn_reg_signal = EDC_CG_SIG_WARN_ALARM;
3841 			phba->cgn_reg_fpin = LPFC_CGN_FPIN_NONE;
3842 		}
3843 		if (drv_sig_cap == EDC_CG_SIG_WARN_ONLY) {
3844 			phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
3845 			phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
3846 		}
3847 	}
3848 
3849 	if (!phba->cgn_i)
3850 		return;
3851 
3852 	/* Update signal frequency in congestion info buffer */
3853 	cp = (struct lpfc_cgn_info *)phba->cgn_i->virt;
3854 
3855 	/* Frequency (in ms) Signal Warning/Signal Congestion Notifications
3856 	 * are received by the HBA
3857 	 */
3858 	sig_freq = phba->cgn_sig_freq;
3859 
3860 	if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
3861 		cp->cgn_warn_freq = cpu_to_le16(sig_freq);
3862 	if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
3863 		cp->cgn_alarm_freq = cpu_to_le16(sig_freq);
3864 		cp->cgn_warn_freq = cpu_to_le16(sig_freq);
3865 	}
3866 	crc = lpfc_cgn_calc_crc32(cp, LPFC_CGN_INFO_SZ, LPFC_CGN_CRC32_SEED);
3867 	cp->cgn_info_crc = cpu_to_le32(crc);
3868 	return;
3869 
3870 out_no_support:
3871 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
3872 	phba->cgn_sig_freq = 0;
3873 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
3874 }
3875 
3876 DECLARE_ENUM2STR_LOOKUP(lpfc_get_tlv_dtag_nm, fc_ls_tlv_dtag,
3877 			FC_LS_TLV_DTAG_INIT);
3878 
3879 /**
3880  * lpfc_cmpl_els_edc - Completion callback function for EDC
3881  * @phba: pointer to lpfc hba data structure.
3882  * @cmdiocb: pointer to lpfc command iocb data structure.
3883  * @rspiocb: pointer to lpfc response iocb data structure.
3884  *
3885  * This routine is the completion callback function for issuing the Exchange
3886  * Diagnostic Capabilities (EDC) command. The driver issues an EDC to
3887  * notify the FPort of its Congestion and Link Fault capabilities.  This
3888  * routine parses the FPort's response and decides on the least common
3889  * values applicable to both FPort and NPort for Warnings and Alarms that
3890  * are communicated via hardware signals.
3891  **/
3892 static void
3893 lpfc_cmpl_els_edc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3894 		  struct lpfc_iocbq *rspiocb)
3895 {
3896 	IOCB_t *irsp;
3897 	struct fc_els_edc_resp *edc_rsp;
3898 	struct fc_tlv_desc *tlv;
3899 	struct fc_diag_cg_sig_desc *pcgd;
3900 	struct fc_diag_lnkflt_desc *plnkflt;
3901 	struct lpfc_dmabuf *pcmd, *prsp;
3902 	const char *dtag_nm;
3903 	u32 *pdata, dtag;
3904 	int desc_cnt = 0, bytes_remain;
3905 	bool rcv_cap_desc = false;
3906 	struct lpfc_nodelist *ndlp;
3907 
3908 	irsp = &rspiocb->iocb;
3909 	ndlp = cmdiocb->context1;
3910 
3911 	lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
3912 			      "EDC cmpl:    status:x%x/x%x did:x%x",
3913 			      irsp->ulpStatus, irsp->un.ulpWord[4],
3914 			      irsp->un.elsreq64.remoteID);
3915 
3916 	/* ELS cmd tag <ulpIoTag> completes */
3917 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3918 			"4201 EDC cmd tag x%x completes Data: x%x x%x x%x\n",
3919 			irsp->ulpIoTag, irsp->ulpStatus,
3920 			irsp->un.ulpWord[4], irsp->ulpTimeout);
3921 
3922 	pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
3923 	if (!pcmd)
3924 		goto out;
3925 
3926 	pdata = (u32 *)pcmd->virt;
3927 	if (!pdata)
3928 		goto out;
3929 
3930 	/* Need to clear signal values, send features MB and RDF with FPIN. */
3931 	if (irsp->ulpStatus)
3932 		goto out;
3933 
3934 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
3935 	if (!prsp)
3936 		goto out;
3937 
3938 	edc_rsp = prsp->virt;
3939 	if (!edc_rsp)
3940 		goto out;
3941 
3942 	/* ELS cmd tag <ulpIoTag> completes */
3943 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3944 			"4676 Fabric EDC Rsp: "
3945 			"0x%02x, 0x%08x\n",
3946 			edc_rsp->acc_hdr.la_cmd,
3947 			be32_to_cpu(edc_rsp->desc_list_len));
3948 
3949 	/*
3950 	 * Payload length in bytes is the response descriptor list
3951 	 * length minus the 12 bytes of Link Service Request
3952 	 * Information descriptor in the reply.
3953 	 */
3954 	bytes_remain = be32_to_cpu(edc_rsp->desc_list_len) -
3955 				   sizeof(struct fc_els_lsri_desc);
3956 	if (bytes_remain <= 0)
3957 		goto out;
3958 
3959 	tlv = edc_rsp->desc;
3960 
3961 	/*
3962 	 * cycle through EDC diagnostic descriptors to find the
3963 	 * congestion signaling capability descriptor
3964 	 */
3965 	while (bytes_remain) {
3966 		if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
3967 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
3968 					"6461 Truncated TLV hdr on "
3969 					"Diagnostic descriptor[%d]\n",
3970 					desc_cnt);
3971 			goto out;
3972 		}
3973 
3974 		dtag = be32_to_cpu(tlv->desc_tag);
3975 		switch (dtag) {
3976 		case ELS_DTAG_LNK_FAULT_CAP:
3977 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
3978 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
3979 					sizeof(struct fc_diag_lnkflt_desc)) {
3980 				lpfc_printf_log(
3981 					phba, KERN_WARNING, LOG_CGN_MGMT,
3982 					"6462 Truncated Link Fault Diagnostic "
3983 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
3984 					desc_cnt, bytes_remain,
3985 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
3986 					sizeof(struct fc_diag_cg_sig_desc));
3987 				goto out;
3988 			}
3989 			plnkflt = (struct fc_diag_lnkflt_desc *)tlv;
3990 			lpfc_printf_log(
3991 				phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3992 				"4617 Link Fault Desc Data: 0x%08x 0x%08x "
3993 				"0x%08x 0x%08x 0x%08x\n",
3994 				be32_to_cpu(plnkflt->desc_tag),
3995 				be32_to_cpu(plnkflt->desc_len),
3996 				be32_to_cpu(
3997 					plnkflt->degrade_activate_threshold),
3998 				be32_to_cpu(
3999 					plnkflt->degrade_deactivate_threshold),
4000 				be32_to_cpu(plnkflt->fec_degrade_interval));
4001 			break;
4002 		case ELS_DTAG_CG_SIGNAL_CAP:
4003 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
4004 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
4005 					sizeof(struct fc_diag_cg_sig_desc)) {
4006 				lpfc_printf_log(
4007 					phba, KERN_WARNING, LOG_CGN_MGMT,
4008 					"6463 Truncated Cgn Signal Diagnostic "
4009 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
4010 					desc_cnt, bytes_remain,
4011 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
4012 					sizeof(struct fc_diag_cg_sig_desc));
4013 				goto out;
4014 			}
4015 
4016 			pcgd = (struct fc_diag_cg_sig_desc *)tlv;
4017 			lpfc_printf_log(
4018 				phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
4019 				"4616 CGN Desc Data: 0x%08x 0x%08x "
4020 				"0x%08x 0x%04x 0x%04x 0x%08x 0x%04x 0x%04x\n",
4021 				be32_to_cpu(pcgd->desc_tag),
4022 				be32_to_cpu(pcgd->desc_len),
4023 				be32_to_cpu(pcgd->xmt_signal_capability),
4024 				be16_to_cpu(pcgd->xmt_signal_frequency.count),
4025 				be16_to_cpu(pcgd->xmt_signal_frequency.units),
4026 				be32_to_cpu(pcgd->rcv_signal_capability),
4027 				be16_to_cpu(pcgd->rcv_signal_frequency.count),
4028 				be16_to_cpu(pcgd->rcv_signal_frequency.units));
4029 
4030 			/* Compare driver and Fport capabilities and choose
4031 			 * least common.
4032 			 */
4033 			lpfc_least_capable_settings(phba, pcgd);
4034 			rcv_cap_desc = true;
4035 			break;
4036 		default:
4037 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
4038 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
4039 					"4919 unknown Diagnostic "
4040 					"Descriptor[%d]: tag x%x (%s)\n",
4041 					desc_cnt, dtag, dtag_nm);
4042 		}
4043 
4044 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
4045 		tlv = fc_tlv_next_desc(tlv);
4046 		desc_cnt++;
4047 	}
4048 
4049 out:
4050 	if (!rcv_cap_desc) {
4051 		phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
4052 		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4053 		phba->cgn_sig_freq = 0;
4054 		lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_CGN_MGMT,
4055 				"4202 EDC rsp error - sending RDF "
4056 				"for FPIN only.\n");
4057 	}
4058 
4059 	lpfc_config_cgn_signal(phba);
4060 
4061 	/* Check to see if link went down during discovery */
4062 	lpfc_els_chk_latt(phba->pport);
4063 	lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
4064 			      "EDC Cmpl:     did:x%x refcnt %d",
4065 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
4066 	lpfc_els_free_iocb(phba, cmdiocb);
4067 	lpfc_nlp_put(ndlp);
4068 }
4069 
4070 static void
4071 lpfc_format_edc_cgn_desc(struct lpfc_hba *phba, struct fc_diag_cg_sig_desc *cgd)
4072 {
4073 	/* We are assuming cgd was zero'ed before calling this routine */
4074 
4075 	/* Configure the congestion detection capability */
4076 	cgd->desc_tag = cpu_to_be32(ELS_DTAG_CG_SIGNAL_CAP);
4077 
4078 	/* Descriptor len doesn't include the tag or len fields. */
4079 	cgd->desc_len = cpu_to_be32(
4080 		FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_diag_cg_sig_desc));
4081 
4082 	/* xmt_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
4083 	 * xmt_signal_frequency.count already set to 0.
4084 	 * xmt_signal_frequency.units already set to 0.
4085 	 */
4086 
4087 	if (phba->cmf_active_mode == LPFC_CFG_OFF) {
4088 		/* rcv_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
4089 		 * rcv_signal_frequency.count already set to 0.
4090 		 * rcv_signal_frequency.units already set to 0.
4091 		 */
4092 		phba->cgn_sig_freq = 0;
4093 		return;
4094 	}
4095 	switch (phba->cgn_reg_signal) {
4096 	case EDC_CG_SIG_WARN_ONLY:
4097 		cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ONLY);
4098 		break;
4099 	case EDC_CG_SIG_WARN_ALARM:
4100 		cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ALARM);
4101 		break;
4102 	default:
4103 		/* rcv_signal_capability left 0 thus no support */
4104 		break;
4105 	}
4106 
4107 	/* We start negotiation with lpfc_fabric_cgn_frequency, after
4108 	 * the completion we settle on the higher frequency.
4109 	 */
4110 	cgd->rcv_signal_frequency.count =
4111 		cpu_to_be16(lpfc_fabric_cgn_frequency);
4112 	cgd->rcv_signal_frequency.units =
4113 		cpu_to_be16(EDC_CG_SIGFREQ_MSEC);
4114 }
4115 
4116  /**
4117   * lpfc_issue_els_edc - Exchange Diagnostic Capabilities with the fabric.
4118   * @vport: pointer to a host virtual N_Port data structure.
4119   * @retry: retry counter for the command iocb.
4120   *
4121   * This routine issues an ELS EDC to the F-Port Controller to communicate
4122   * this N_Port's support of hardware signals in its Congestion
4123   * Capabilities Descriptor.
4124   *
4125   * Note: This routine does not check if one or more signals are
4126   * set in the cgn_reg_signal parameter.  The caller makes the
4127   * decision to enforce cgn_reg_signal as nonzero or zero depending
4128   * on the conditions.  During Fabric requests, the driver
4129   * requires cgn_reg_signals to be nonzero.  But a dynamic request
4130   * to set the congestion mode to OFF from Monitor or Manage
4131   * would correctly issue an EDC with no signals enabled to
4132   * turn off switch functionality and then update the FW.
4133   *
4134   * Return code
4135   *   0 - Successfully issued edc command
4136   *   1 - Failed to issue edc command
4137   **/
4138 int
4139 lpfc_issue_els_edc(struct lpfc_vport *vport, uint8_t retry)
4140 {
4141 	struct lpfc_hba  *phba = vport->phba;
4142 	struct lpfc_iocbq *elsiocb;
4143 	struct lpfc_els_edc_req *edc_req;
4144 	struct fc_diag_cg_sig_desc *cgn_desc;
4145 	u16 cmdsize;
4146 	struct lpfc_nodelist *ndlp;
4147 	u8 *pcmd = NULL;
4148 	u32 edc_req_size, cgn_desc_size;
4149 	int rc;
4150 
4151 	if (vport->port_type == LPFC_NPIV_PORT)
4152 		return -EACCES;
4153 
4154 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
4155 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
4156 		return -ENODEV;
4157 
4158 	/* If HBA doesn't support signals, drop into RDF */
4159 	if (!phba->cgn_init_reg_signal)
4160 		goto try_rdf;
4161 
4162 	edc_req_size = sizeof(struct fc_els_edc);
4163 	cgn_desc_size = sizeof(struct fc_diag_cg_sig_desc);
4164 	cmdsize = edc_req_size + cgn_desc_size;
4165 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
4166 				     ndlp->nlp_DID, ELS_CMD_EDC);
4167 	if (!elsiocb)
4168 		goto try_rdf;
4169 
4170 	/* Configure the payload for the supported Diagnostics capabilities. */
4171 	pcmd = (u8 *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
4172 	memset(pcmd, 0, cmdsize);
4173 	edc_req = (struct lpfc_els_edc_req *)pcmd;
4174 	edc_req->edc.desc_len = cpu_to_be32(cgn_desc_size);
4175 	edc_req->edc.edc_cmd = ELS_EDC;
4176 
4177 	cgn_desc = &edc_req->cgn_desc;
4178 
4179 	lpfc_format_edc_cgn_desc(phba, cgn_desc);
4180 
4181 	phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
4182 
4183 	lpfc_printf_vlog(vport, KERN_INFO, LOG_CGN_MGMT,
4184 			 "4623 Xmit EDC to remote "
4185 			 "NPORT x%x reg_sig x%x reg_fpin:x%x\n",
4186 			 ndlp->nlp_DID, phba->cgn_reg_signal,
4187 			 phba->cgn_reg_fpin);
4188 
4189 	elsiocb->iocb_cmpl = lpfc_cmpl_els_disc_cmd;
4190 	elsiocb->context1 = lpfc_nlp_get(ndlp);
4191 	if (!elsiocb->context1) {
4192 		lpfc_els_free_iocb(phba, elsiocb);
4193 		return -EIO;
4194 	}
4195 
4196 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4197 			      "Issue EDC:     did:x%x refcnt %d",
4198 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
4199 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4200 	if (rc == IOCB_ERROR) {
4201 		/* The additional lpfc_nlp_put will cause the following
4202 		 * lpfc_els_free_iocb routine to trigger the rlease of
4203 		 * the node.
4204 		 */
4205 		lpfc_els_free_iocb(phba, elsiocb);
4206 		lpfc_nlp_put(ndlp);
4207 		goto try_rdf;
4208 	}
4209 	return 0;
4210 try_rdf:
4211 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
4212 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4213 	rc = lpfc_issue_els_rdf(vport, 0);
4214 	return rc;
4215 }
4216 
4217 /**
4218  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
4219  * @vport: pointer to a host virtual N_Port data structure.
4220  * @nlp: pointer to a node-list data structure.
4221  *
4222  * This routine cancels the timer with a delayed IOCB-command retry for
4223  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
4224  * removes the ELS retry event if it presents. In addition, if the
4225  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
4226  * commands are sent for the @vport's nodes that require issuing discovery
4227  * ADISC.
4228  **/
4229 void
4230 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
4231 {
4232 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4233 	struct lpfc_work_evt *evtp;
4234 
4235 	if (!(nlp->nlp_flag & NLP_DELAY_TMO))
4236 		return;
4237 	spin_lock_irq(&nlp->lock);
4238 	nlp->nlp_flag &= ~NLP_DELAY_TMO;
4239 	spin_unlock_irq(&nlp->lock);
4240 	del_timer_sync(&nlp->nlp_delayfunc);
4241 	nlp->nlp_last_elscmd = 0;
4242 	if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
4243 		list_del_init(&nlp->els_retry_evt.evt_listp);
4244 		/* Decrement nlp reference count held for the delayed retry */
4245 		evtp = &nlp->els_retry_evt;
4246 		lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
4247 	}
4248 	if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
4249 		spin_lock_irq(&nlp->lock);
4250 		nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
4251 		spin_unlock_irq(&nlp->lock);
4252 		if (vport->num_disc_nodes) {
4253 			if (vport->port_state < LPFC_VPORT_READY) {
4254 				/* Check if there are more ADISCs to be sent */
4255 				lpfc_more_adisc(vport);
4256 			} else {
4257 				/* Check if there are more PLOGIs to be sent */
4258 				lpfc_more_plogi(vport);
4259 				if (vport->num_disc_nodes == 0) {
4260 					spin_lock_irq(shost->host_lock);
4261 					vport->fc_flag &= ~FC_NDISC_ACTIVE;
4262 					spin_unlock_irq(shost->host_lock);
4263 					lpfc_can_disctmo(vport);
4264 					lpfc_end_rscn(vport);
4265 				}
4266 			}
4267 		}
4268 	}
4269 	return;
4270 }
4271 
4272 /**
4273  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
4274  * @t: pointer to the timer function associated data (ndlp).
4275  *
4276  * This routine is invoked by the ndlp delayed-function timer to check
4277  * whether there is any pending ELS retry event(s) with the node. If not, it
4278  * simply returns. Otherwise, if there is at least one ELS delayed event, it
4279  * adds the delayed events to the HBA work list and invokes the
4280  * lpfc_worker_wake_up() routine to wake up worker thread to process the
4281  * event. Note that lpfc_nlp_get() is called before posting the event to
4282  * the work list to hold reference count of ndlp so that it guarantees the
4283  * reference to ndlp will still be available when the worker thread gets
4284  * to the event associated with the ndlp.
4285  **/
4286 void
4287 lpfc_els_retry_delay(struct timer_list *t)
4288 {
4289 	struct lpfc_nodelist *ndlp = from_timer(ndlp, t, nlp_delayfunc);
4290 	struct lpfc_vport *vport = ndlp->vport;
4291 	struct lpfc_hba   *phba = vport->phba;
4292 	unsigned long flags;
4293 	struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
4294 
4295 	spin_lock_irqsave(&phba->hbalock, flags);
4296 	if (!list_empty(&evtp->evt_listp)) {
4297 		spin_unlock_irqrestore(&phba->hbalock, flags);
4298 		return;
4299 	}
4300 
4301 	/* We need to hold the node by incrementing the reference
4302 	 * count until the queued work is done
4303 	 */
4304 	evtp->evt_arg1  = lpfc_nlp_get(ndlp);
4305 	if (evtp->evt_arg1) {
4306 		evtp->evt = LPFC_EVT_ELS_RETRY;
4307 		list_add_tail(&evtp->evt_listp, &phba->work_list);
4308 		lpfc_worker_wake_up(phba);
4309 	}
4310 	spin_unlock_irqrestore(&phba->hbalock, flags);
4311 	return;
4312 }
4313 
4314 /**
4315  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
4316  * @ndlp: pointer to a node-list data structure.
4317  *
4318  * This routine is the worker-thread handler for processing the @ndlp delayed
4319  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
4320  * the last ELS command from the associated ndlp and invokes the proper ELS
4321  * function according to the delayed ELS command to retry the command.
4322  **/
4323 void
4324 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
4325 {
4326 	struct lpfc_vport *vport = ndlp->vport;
4327 	uint32_t cmd, retry;
4328 
4329 	spin_lock_irq(&ndlp->lock);
4330 	cmd = ndlp->nlp_last_elscmd;
4331 	ndlp->nlp_last_elscmd = 0;
4332 
4333 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
4334 		spin_unlock_irq(&ndlp->lock);
4335 		return;
4336 	}
4337 
4338 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
4339 	spin_unlock_irq(&ndlp->lock);
4340 	/*
4341 	 * If a discovery event readded nlp_delayfunc after timer
4342 	 * firing and before processing the timer, cancel the
4343 	 * nlp_delayfunc.
4344 	 */
4345 	del_timer_sync(&ndlp->nlp_delayfunc);
4346 	retry = ndlp->nlp_retry;
4347 	ndlp->nlp_retry = 0;
4348 
4349 	switch (cmd) {
4350 	case ELS_CMD_FLOGI:
4351 		lpfc_issue_els_flogi(vport, ndlp, retry);
4352 		break;
4353 	case ELS_CMD_PLOGI:
4354 		if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
4355 			ndlp->nlp_prev_state = ndlp->nlp_state;
4356 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4357 		}
4358 		break;
4359 	case ELS_CMD_ADISC:
4360 		if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
4361 			ndlp->nlp_prev_state = ndlp->nlp_state;
4362 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4363 		}
4364 		break;
4365 	case ELS_CMD_PRLI:
4366 	case ELS_CMD_NVMEPRLI:
4367 		if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
4368 			ndlp->nlp_prev_state = ndlp->nlp_state;
4369 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
4370 		}
4371 		break;
4372 	case ELS_CMD_LOGO:
4373 		if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
4374 			ndlp->nlp_prev_state = ndlp->nlp_state;
4375 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4376 		}
4377 		break;
4378 	case ELS_CMD_FDISC:
4379 		if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
4380 			lpfc_issue_els_fdisc(vport, ndlp, retry);
4381 		break;
4382 	}
4383 	return;
4384 }
4385 
4386 /**
4387  * lpfc_link_reset - Issue link reset
4388  * @vport: pointer to a virtual N_Port data structure.
4389  *
4390  * This routine performs link reset by sending INIT_LINK mailbox command.
4391  * For SLI-3 adapter, link attention interrupt is enabled before issuing
4392  * INIT_LINK mailbox command.
4393  *
4394  * Return code
4395  *   0 - Link reset initiated successfully
4396  *   1 - Failed to initiate link reset
4397  **/
4398 int
4399 lpfc_link_reset(struct lpfc_vport *vport)
4400 {
4401 	struct lpfc_hba *phba = vport->phba;
4402 	LPFC_MBOXQ_t *mbox;
4403 	uint32_t control;
4404 	int rc;
4405 
4406 	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4407 			 "2851 Attempt link reset\n");
4408 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4409 	if (!mbox) {
4410 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4411 				"2852 Failed to allocate mbox memory");
4412 		return 1;
4413 	}
4414 
4415 	/* Enable Link attention interrupts */
4416 	if (phba->sli_rev <= LPFC_SLI_REV3) {
4417 		spin_lock_irq(&phba->hbalock);
4418 		phba->sli.sli_flag |= LPFC_PROCESS_LA;
4419 		control = readl(phba->HCregaddr);
4420 		control |= HC_LAINT_ENA;
4421 		writel(control, phba->HCregaddr);
4422 		readl(phba->HCregaddr); /* flush */
4423 		spin_unlock_irq(&phba->hbalock);
4424 	}
4425 
4426 	lpfc_init_link(phba, mbox, phba->cfg_topology,
4427 		       phba->cfg_link_speed);
4428 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4429 	mbox->vport = vport;
4430 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4431 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
4432 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4433 				"2853 Failed to issue INIT_LINK "
4434 				"mbox command, rc:x%x\n", rc);
4435 		mempool_free(mbox, phba->mbox_mem_pool);
4436 		return 1;
4437 	}
4438 
4439 	return 0;
4440 }
4441 
4442 /**
4443  * lpfc_els_retry - Make retry decision on an els command iocb
4444  * @phba: pointer to lpfc hba data structure.
4445  * @cmdiocb: pointer to lpfc command iocb data structure.
4446  * @rspiocb: pointer to lpfc response iocb data structure.
4447  *
4448  * This routine makes a retry decision on an ELS command IOCB, which has
4449  * failed. The following ELS IOCBs use this function for retrying the command
4450  * when previously issued command responsed with error status: FLOGI, PLOGI,
4451  * PRLI, ADISC and FDISC. Based on the ELS command type and the
4452  * returned error status, it makes the decision whether a retry shall be
4453  * issued for the command, and whether a retry shall be made immediately or
4454  * delayed. In the former case, the corresponding ELS command issuing-function
4455  * is called to retry the command. In the later case, the ELS command shall
4456  * be posted to the ndlp delayed event and delayed function timer set to the
4457  * ndlp for the delayed command issusing.
4458  *
4459  * Return code
4460  *   0 - No retry of els command is made
4461  *   1 - Immediate or delayed retry of els command is made
4462  **/
4463 static int
4464 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4465 	       struct lpfc_iocbq *rspiocb)
4466 {
4467 	struct lpfc_vport *vport = cmdiocb->vport;
4468 	IOCB_t *irsp = &rspiocb->iocb;
4469 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4470 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4471 	uint32_t *elscmd;
4472 	struct ls_rjt stat;
4473 	int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
4474 	int logerr = 0;
4475 	uint32_t cmd = 0;
4476 	uint32_t did;
4477 	int link_reset = 0, rc;
4478 
4479 
4480 	/* Note: context2 may be 0 for internal driver abort
4481 	 * of delays ELS command.
4482 	 */
4483 
4484 	if (pcmd && pcmd->virt) {
4485 		elscmd = (uint32_t *) (pcmd->virt);
4486 		cmd = *elscmd++;
4487 	}
4488 
4489 	if (ndlp)
4490 		did = ndlp->nlp_DID;
4491 	else {
4492 		/* We should only hit this case for retrying PLOGI */
4493 		did = irsp->un.elsreq64.remoteID;
4494 		ndlp = lpfc_findnode_did(vport, did);
4495 		if (!ndlp && (cmd != ELS_CMD_PLOGI))
4496 			return 0;
4497 	}
4498 
4499 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4500 		"Retry ELS:       wd7:x%x wd4:x%x did:x%x",
4501 		*(((uint32_t *)irsp) + 7), irsp->un.ulpWord[4], did);
4502 
4503 	switch (irsp->ulpStatus) {
4504 	case IOSTAT_FCP_RSP_ERROR:
4505 		break;
4506 	case IOSTAT_REMOTE_STOP:
4507 		if (phba->sli_rev == LPFC_SLI_REV4) {
4508 			/* This IO was aborted by the target, we don't
4509 			 * know the rxid and because we did not send the
4510 			 * ABTS we cannot generate and RRQ.
4511 			 */
4512 			lpfc_set_rrq_active(phba, ndlp,
4513 					 cmdiocb->sli4_lxritag, 0, 0);
4514 		}
4515 		break;
4516 	case IOSTAT_LOCAL_REJECT:
4517 		switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
4518 		case IOERR_LOOP_OPEN_FAILURE:
4519 			if (cmd == ELS_CMD_FLOGI) {
4520 				if (PCI_DEVICE_ID_HORNET ==
4521 					phba->pcidev->device) {
4522 					phba->fc_topology = LPFC_TOPOLOGY_LOOP;
4523 					phba->pport->fc_myDID = 0;
4524 					phba->alpa_map[0] = 0;
4525 					phba->alpa_map[1] = 0;
4526 				}
4527 			}
4528 			if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
4529 				delay = 1000;
4530 			retry = 1;
4531 			break;
4532 
4533 		case IOERR_ILLEGAL_COMMAND:
4534 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4535 					 "0124 Retry illegal cmd x%x "
4536 					 "retry:x%x delay:x%x\n",
4537 					 cmd, cmdiocb->retry, delay);
4538 			retry = 1;
4539 			/* All command's retry policy */
4540 			maxretry = 8;
4541 			if (cmdiocb->retry > 2)
4542 				delay = 1000;
4543 			break;
4544 
4545 		case IOERR_NO_RESOURCES:
4546 			logerr = 1; /* HBA out of resources */
4547 			retry = 1;
4548 			if (cmdiocb->retry > 100)
4549 				delay = 100;
4550 			maxretry = 250;
4551 			break;
4552 
4553 		case IOERR_ILLEGAL_FRAME:
4554 			delay = 100;
4555 			retry = 1;
4556 			break;
4557 
4558 		case IOERR_INVALID_RPI:
4559 			if (cmd == ELS_CMD_PLOGI &&
4560 			    did == NameServer_DID) {
4561 				/* Continue forever if plogi to */
4562 				/* the nameserver fails */
4563 				maxretry = 0;
4564 				delay = 100;
4565 			}
4566 			retry = 1;
4567 			break;
4568 
4569 		case IOERR_SEQUENCE_TIMEOUT:
4570 			if (cmd == ELS_CMD_PLOGI &&
4571 			    did == NameServer_DID &&
4572 			    (cmdiocb->retry + 1) == maxretry) {
4573 				/* Reset the Link */
4574 				link_reset = 1;
4575 				break;
4576 			}
4577 			retry = 1;
4578 			delay = 100;
4579 			break;
4580 		case IOERR_SLI_ABORTED:
4581 			/* Retry ELS PLOGI command?
4582 			 * Possibly the rport just wasn't ready.
4583 			 */
4584 			if (cmd == ELS_CMD_PLOGI) {
4585 				/* No retry if state change */
4586 				if (ndlp &&
4587 				    ndlp->nlp_state != NLP_STE_PLOGI_ISSUE)
4588 					goto out_retry;
4589 				retry = 1;
4590 				maxretry = 2;
4591 			}
4592 			break;
4593 		}
4594 		break;
4595 
4596 	case IOSTAT_NPORT_RJT:
4597 	case IOSTAT_FABRIC_RJT:
4598 		if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
4599 			retry = 1;
4600 			break;
4601 		}
4602 		break;
4603 
4604 	case IOSTAT_NPORT_BSY:
4605 	case IOSTAT_FABRIC_BSY:
4606 		logerr = 1; /* Fabric / Remote NPort out of resources */
4607 		retry = 1;
4608 		break;
4609 
4610 	case IOSTAT_LS_RJT:
4611 		stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
4612 		/* Added for Vendor specifc support
4613 		 * Just keep retrying for these Rsn / Exp codes
4614 		 */
4615 		switch (stat.un.b.lsRjtRsnCode) {
4616 		case LSRJT_UNABLE_TPC:
4617 			/* The driver has a VALID PLOGI but the rport has
4618 			 * rejected the PRLI - can't do it now.  Delay
4619 			 * for 1 second and try again.
4620 			 *
4621 			 * However, if explanation is REQ_UNSUPPORTED there's
4622 			 * no point to retry PRLI.
4623 			 */
4624 			if ((cmd == ELS_CMD_PRLI || cmd == ELS_CMD_NVMEPRLI) &&
4625 			    stat.un.b.lsRjtRsnCodeExp !=
4626 			    LSEXP_REQ_UNSUPPORTED) {
4627 				delay = 1000;
4628 				maxretry = lpfc_max_els_tries + 1;
4629 				retry = 1;
4630 				break;
4631 			}
4632 
4633 			/* Legacy bug fix code for targets with PLOGI delays. */
4634 			if (stat.un.b.lsRjtRsnCodeExp ==
4635 			    LSEXP_CMD_IN_PROGRESS) {
4636 				if (cmd == ELS_CMD_PLOGI) {
4637 					delay = 1000;
4638 					maxretry = 48;
4639 				}
4640 				retry = 1;
4641 				break;
4642 			}
4643 			if (stat.un.b.lsRjtRsnCodeExp ==
4644 			    LSEXP_CANT_GIVE_DATA) {
4645 				if (cmd == ELS_CMD_PLOGI) {
4646 					delay = 1000;
4647 					maxretry = 48;
4648 				}
4649 				retry = 1;
4650 				break;
4651 			}
4652 			if (cmd == ELS_CMD_PLOGI) {
4653 				delay = 1000;
4654 				maxretry = lpfc_max_els_tries + 1;
4655 				retry = 1;
4656 				break;
4657 			}
4658 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4659 			  (cmd == ELS_CMD_FDISC) &&
4660 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
4661 				lpfc_printf_vlog(vport, KERN_ERR,
4662 						 LOG_TRACE_EVENT,
4663 						 "0125 FDISC Failed (x%x). "
4664 						 "Fabric out of resources\n",
4665 						 stat.un.lsRjtError);
4666 				lpfc_vport_set_state(vport,
4667 						     FC_VPORT_NO_FABRIC_RSCS);
4668 			}
4669 			break;
4670 
4671 		case LSRJT_LOGICAL_BSY:
4672 			if ((cmd == ELS_CMD_PLOGI) ||
4673 			    (cmd == ELS_CMD_PRLI) ||
4674 			    (cmd == ELS_CMD_NVMEPRLI)) {
4675 				delay = 1000;
4676 				maxretry = 48;
4677 			} else if (cmd == ELS_CMD_FDISC) {
4678 				/* FDISC retry policy */
4679 				maxretry = 48;
4680 				if (cmdiocb->retry >= 32)
4681 					delay = 1000;
4682 			}
4683 			retry = 1;
4684 			break;
4685 
4686 		case LSRJT_LOGICAL_ERR:
4687 			/* There are some cases where switches return this
4688 			 * error when they are not ready and should be returning
4689 			 * Logical Busy. We should delay every time.
4690 			 */
4691 			if (cmd == ELS_CMD_FDISC &&
4692 			    stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
4693 				maxretry = 3;
4694 				delay = 1000;
4695 				retry = 1;
4696 			} else if (cmd == ELS_CMD_FLOGI &&
4697 				   stat.un.b.lsRjtRsnCodeExp ==
4698 						LSEXP_NOTHING_MORE) {
4699 				vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
4700 				retry = 1;
4701 				lpfc_printf_vlog(vport, KERN_ERR,
4702 						 LOG_TRACE_EVENT,
4703 						 "0820 FLOGI Failed (x%x). "
4704 						 "BBCredit Not Supported\n",
4705 						 stat.un.lsRjtError);
4706 			}
4707 			break;
4708 
4709 		case LSRJT_PROTOCOL_ERR:
4710 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4711 			  (cmd == ELS_CMD_FDISC) &&
4712 			  ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
4713 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
4714 			  ) {
4715 				lpfc_printf_vlog(vport, KERN_ERR,
4716 						 LOG_TRACE_EVENT,
4717 						 "0122 FDISC Failed (x%x). "
4718 						 "Fabric Detected Bad WWN\n",
4719 						 stat.un.lsRjtError);
4720 				lpfc_vport_set_state(vport,
4721 						     FC_VPORT_FABRIC_REJ_WWN);
4722 			}
4723 			break;
4724 		case LSRJT_VENDOR_UNIQUE:
4725 			if ((stat.un.b.vendorUnique == 0x45) &&
4726 			    (cmd == ELS_CMD_FLOGI)) {
4727 				goto out_retry;
4728 			}
4729 			break;
4730 		case LSRJT_CMD_UNSUPPORTED:
4731 			/* lpfc nvmet returns this type of LS_RJT when it
4732 			 * receives an FCP PRLI because lpfc nvmet only
4733 			 * support NVME.  ELS request is terminated for FCP4
4734 			 * on this rport.
4735 			 */
4736 			if (stat.un.b.lsRjtRsnCodeExp ==
4737 			    LSEXP_REQ_UNSUPPORTED && cmd == ELS_CMD_PRLI) {
4738 				spin_lock_irq(&ndlp->lock);
4739 				ndlp->nlp_flag |= NLP_FCP_PRLI_RJT;
4740 				spin_unlock_irq(&ndlp->lock);
4741 				retry = 0;
4742 				goto out_retry;
4743 			}
4744 			break;
4745 		}
4746 		break;
4747 
4748 	case IOSTAT_INTERMED_RSP:
4749 	case IOSTAT_BA_RJT:
4750 		break;
4751 
4752 	default:
4753 		break;
4754 	}
4755 
4756 	if (link_reset) {
4757 		rc = lpfc_link_reset(vport);
4758 		if (rc) {
4759 			/* Do not give up. Retry PLOGI one more time and attempt
4760 			 * link reset if PLOGI fails again.
4761 			 */
4762 			retry = 1;
4763 			delay = 100;
4764 			goto out_retry;
4765 		}
4766 		return 1;
4767 	}
4768 
4769 	if (did == FDMI_DID)
4770 		retry = 1;
4771 
4772 	if ((cmd == ELS_CMD_FLOGI) &&
4773 	    (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
4774 	    !lpfc_error_lost_link(irsp)) {
4775 		/* FLOGI retry policy */
4776 		retry = 1;
4777 		/* retry FLOGI forever */
4778 		if (phba->link_flag != LS_LOOPBACK_MODE)
4779 			maxretry = 0;
4780 		else
4781 			maxretry = 2;
4782 
4783 		if (cmdiocb->retry >= 100)
4784 			delay = 5000;
4785 		else if (cmdiocb->retry >= 32)
4786 			delay = 1000;
4787 	} else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
4788 		/* retry FDISCs every second up to devloss */
4789 		retry = 1;
4790 		maxretry = vport->cfg_devloss_tmo;
4791 		delay = 1000;
4792 	}
4793 
4794 	cmdiocb->retry++;
4795 	if (maxretry && (cmdiocb->retry >= maxretry)) {
4796 		phba->fc_stat.elsRetryExceeded++;
4797 		retry = 0;
4798 	}
4799 
4800 	if ((vport->load_flag & FC_UNLOADING) != 0)
4801 		retry = 0;
4802 
4803 out_retry:
4804 	if (retry) {
4805 		if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
4806 			/* Stop retrying PLOGI and FDISC if in FCF discovery */
4807 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4808 				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4809 						 "2849 Stop retry ELS command "
4810 						 "x%x to remote NPORT x%x, "
4811 						 "Data: x%x x%x\n", cmd, did,
4812 						 cmdiocb->retry, delay);
4813 				return 0;
4814 			}
4815 		}
4816 
4817 		/* Retry ELS command <elsCmd> to remote NPORT <did> */
4818 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4819 				 "0107 Retry ELS command x%x to remote "
4820 				 "NPORT x%x Data: x%x x%x\n",
4821 				 cmd, did, cmdiocb->retry, delay);
4822 
4823 		if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
4824 			((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
4825 			((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
4826 			IOERR_NO_RESOURCES))) {
4827 			/* Don't reset timer for no resources */
4828 
4829 			/* If discovery / RSCN timer is running, reset it */
4830 			if (timer_pending(&vport->fc_disctmo) ||
4831 			    (vport->fc_flag & FC_RSCN_MODE))
4832 				lpfc_set_disctmo(vport);
4833 		}
4834 
4835 		phba->fc_stat.elsXmitRetry++;
4836 		if (ndlp && delay) {
4837 			phba->fc_stat.elsDelayRetry++;
4838 			ndlp->nlp_retry = cmdiocb->retry;
4839 
4840 			/* delay is specified in milliseconds */
4841 			mod_timer(&ndlp->nlp_delayfunc,
4842 				jiffies + msecs_to_jiffies(delay));
4843 			spin_lock_irq(&ndlp->lock);
4844 			ndlp->nlp_flag |= NLP_DELAY_TMO;
4845 			spin_unlock_irq(&ndlp->lock);
4846 
4847 			ndlp->nlp_prev_state = ndlp->nlp_state;
4848 			if ((cmd == ELS_CMD_PRLI) ||
4849 			    (cmd == ELS_CMD_NVMEPRLI))
4850 				lpfc_nlp_set_state(vport, ndlp,
4851 					NLP_STE_PRLI_ISSUE);
4852 			else if (cmd != ELS_CMD_ADISC)
4853 				lpfc_nlp_set_state(vport, ndlp,
4854 					NLP_STE_NPR_NODE);
4855 			ndlp->nlp_last_elscmd = cmd;
4856 
4857 			return 1;
4858 		}
4859 		switch (cmd) {
4860 		case ELS_CMD_FLOGI:
4861 			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
4862 			return 1;
4863 		case ELS_CMD_FDISC:
4864 			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
4865 			return 1;
4866 		case ELS_CMD_PLOGI:
4867 			if (ndlp) {
4868 				ndlp->nlp_prev_state = ndlp->nlp_state;
4869 				lpfc_nlp_set_state(vport, ndlp,
4870 						   NLP_STE_PLOGI_ISSUE);
4871 			}
4872 			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
4873 			return 1;
4874 		case ELS_CMD_ADISC:
4875 			ndlp->nlp_prev_state = ndlp->nlp_state;
4876 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4877 			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
4878 			return 1;
4879 		case ELS_CMD_PRLI:
4880 		case ELS_CMD_NVMEPRLI:
4881 			ndlp->nlp_prev_state = ndlp->nlp_state;
4882 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
4883 			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
4884 			return 1;
4885 		case ELS_CMD_LOGO:
4886 			ndlp->nlp_prev_state = ndlp->nlp_state;
4887 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4888 			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
4889 			return 1;
4890 		}
4891 	}
4892 	/* No retry ELS command <elsCmd> to remote NPORT <did> */
4893 	if (logerr) {
4894 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4895 			 "0137 No retry ELS command x%x to remote "
4896 			 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
4897 			 cmd, did, irsp->ulpStatus,
4898 			 irsp->un.ulpWord[4]);
4899 	}
4900 	else {
4901 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4902 			 "0108 No retry ELS command x%x to remote "
4903 			 "NPORT x%x Retried:%d Error:x%x/%x\n",
4904 			 cmd, did, cmdiocb->retry, irsp->ulpStatus,
4905 			 irsp->un.ulpWord[4]);
4906 	}
4907 	return 0;
4908 }
4909 
4910 /**
4911  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
4912  * @phba: pointer to lpfc hba data structure.
4913  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
4914  *
4915  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
4916  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
4917  * checks to see whether there is a lpfc DMA buffer associated with the
4918  * response of the command IOCB. If so, it will be released before releasing
4919  * the lpfc DMA buffer associated with the IOCB itself.
4920  *
4921  * Return code
4922  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4923  **/
4924 static int
4925 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
4926 {
4927 	struct lpfc_dmabuf *buf_ptr;
4928 
4929 	/* Free the response before processing the command. */
4930 	if (!list_empty(&buf_ptr1->list)) {
4931 		list_remove_head(&buf_ptr1->list, buf_ptr,
4932 				 struct lpfc_dmabuf,
4933 				 list);
4934 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4935 		kfree(buf_ptr);
4936 	}
4937 	lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
4938 	kfree(buf_ptr1);
4939 	return 0;
4940 }
4941 
4942 /**
4943  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
4944  * @phba: pointer to lpfc hba data structure.
4945  * @buf_ptr: pointer to the lpfc dma buffer data structure.
4946  *
4947  * This routine releases the lpfc Direct Memory Access (DMA) buffer
4948  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
4949  * pool.
4950  *
4951  * Return code
4952  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4953  **/
4954 static int
4955 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
4956 {
4957 	lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4958 	kfree(buf_ptr);
4959 	return 0;
4960 }
4961 
4962 /**
4963  * lpfc_els_free_iocb - Free a command iocb and its associated resources
4964  * @phba: pointer to lpfc hba data structure.
4965  * @elsiocb: pointer to lpfc els command iocb data structure.
4966  *
4967  * This routine frees a command IOCB and its associated resources. The
4968  * command IOCB data structure contains the reference to various associated
4969  * resources, these fields must be set to NULL if the associated reference
4970  * not present:
4971  *   context1 - reference to ndlp
4972  *   context2 - reference to cmd
4973  *   context2->next - reference to rsp
4974  *   context3 - reference to bpl
4975  *
4976  * It first properly decrements the reference count held on ndlp for the
4977  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
4978  * set, it invokes the lpfc_els_free_data() routine to release the Direct
4979  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
4980  * adds the DMA buffer the @phba data structure for the delayed release.
4981  * If reference to the Buffer Pointer List (BPL) is present, the
4982  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
4983  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
4984  * invoked to release the IOCB data structure back to @phba IOCBQ list.
4985  *
4986  * Return code
4987  *   0 - Success (currently, always return 0)
4988  **/
4989 int
4990 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
4991 {
4992 	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
4993 
4994 	/* The I/O iocb is complete.  Clear the context1 data. */
4995 	elsiocb->context1 = NULL;
4996 
4997 	/* context2  = cmd,  context2->next = rsp, context3 = bpl */
4998 	if (elsiocb->context2) {
4999 		if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
5000 			/* Firmware could still be in progress of DMAing
5001 			 * payload, so don't free data buffer till after
5002 			 * a hbeat.
5003 			 */
5004 			elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
5005 			buf_ptr = elsiocb->context2;
5006 			elsiocb->context2 = NULL;
5007 			if (buf_ptr) {
5008 				buf_ptr1 = NULL;
5009 				spin_lock_irq(&phba->hbalock);
5010 				if (!list_empty(&buf_ptr->list)) {
5011 					list_remove_head(&buf_ptr->list,
5012 						buf_ptr1, struct lpfc_dmabuf,
5013 						list);
5014 					INIT_LIST_HEAD(&buf_ptr1->list);
5015 					list_add_tail(&buf_ptr1->list,
5016 						&phba->elsbuf);
5017 					phba->elsbuf_cnt++;
5018 				}
5019 				INIT_LIST_HEAD(&buf_ptr->list);
5020 				list_add_tail(&buf_ptr->list, &phba->elsbuf);
5021 				phba->elsbuf_cnt++;
5022 				spin_unlock_irq(&phba->hbalock);
5023 			}
5024 		} else {
5025 			buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
5026 			lpfc_els_free_data(phba, buf_ptr1);
5027 			elsiocb->context2 = NULL;
5028 		}
5029 	}
5030 
5031 	if (elsiocb->context3) {
5032 		buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
5033 		lpfc_els_free_bpl(phba, buf_ptr);
5034 		elsiocb->context3 = NULL;
5035 	}
5036 	lpfc_sli_release_iocbq(phba, elsiocb);
5037 	return 0;
5038 }
5039 
5040 /**
5041  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
5042  * @phba: pointer to lpfc hba data structure.
5043  * @cmdiocb: pointer to lpfc command iocb data structure.
5044  * @rspiocb: pointer to lpfc response iocb data structure.
5045  *
5046  * This routine is the completion callback function to the Logout (LOGO)
5047  * Accept (ACC) Response ELS command. This routine is invoked to indicate
5048  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
5049  * release the ndlp if it has the last reference remaining (reference count
5050  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
5051  * field to NULL to inform the following lpfc_els_free_iocb() routine no
5052  * ndlp reference count needs to be decremented. Otherwise, the ndlp
5053  * reference use-count shall be decremented by the lpfc_els_free_iocb()
5054  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
5055  * IOCB data structure.
5056  **/
5057 static void
5058 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5059 		       struct lpfc_iocbq *rspiocb)
5060 {
5061 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
5062 	struct lpfc_vport *vport = cmdiocb->vport;
5063 	IOCB_t *irsp;
5064 
5065 	irsp = &rspiocb->iocb;
5066 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5067 		"ACC LOGO cmpl:   status:x%x/x%x did:x%x",
5068 		irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
5069 	/* ACC to LOGO completes to NPort <nlp_DID> */
5070 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5071 			 "0109 ACC to LOGO completes to NPort x%x refcnt %d "
5072 			 "Data: x%x x%x x%x\n",
5073 			 ndlp->nlp_DID, kref_read(&ndlp->kref), ndlp->nlp_flag,
5074 			 ndlp->nlp_state, ndlp->nlp_rpi);
5075 
5076 	/* This clause allows the LOGO ACC to complete and free resources
5077 	 * for the Fabric Domain Controller.  It does deliberately skip
5078 	 * the unreg_rpi and release rpi because some fabrics send RDP
5079 	 * requests after logging out from the initiator.
5080 	 */
5081 	if (ndlp->nlp_type & NLP_FABRIC &&
5082 	    ((ndlp->nlp_DID & WELL_KNOWN_DID_MASK) != WELL_KNOWN_DID_MASK))
5083 		goto out;
5084 
5085 	if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
5086 
5087 		/* If PLOGI is being retried, PLOGI completion will cleanup the
5088 		 * node. The NLP_NPR_2B_DISC flag needs to be retained to make
5089 		 * progress on nodes discovered from last RSCN.
5090 		 */
5091 		if ((ndlp->nlp_flag & NLP_DELAY_TMO) &&
5092 		    (ndlp->nlp_last_elscmd == ELS_CMD_PLOGI))
5093 			goto out;
5094 
5095 		/* NPort Recovery mode or node is just allocated */
5096 		if (!lpfc_nlp_not_used(ndlp)) {
5097 			/* A LOGO is completing and the node is in NPR state.
5098 			 * If this a fabric node that cleared its transport
5099 			 * registration, release the rpi.
5100 			 */
5101 			spin_lock_irq(&ndlp->lock);
5102 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
5103 			if (phba->sli_rev == LPFC_SLI_REV4)
5104 				ndlp->nlp_flag |= NLP_RELEASE_RPI;
5105 			spin_unlock_irq(&ndlp->lock);
5106 			lpfc_unreg_rpi(vport, ndlp);
5107 		} else {
5108 			/* Indicate the node has already released, should
5109 			 * not reference to it from within lpfc_els_free_iocb.
5110 			 */
5111 			cmdiocb->context1 = NULL;
5112 		}
5113 	}
5114  out:
5115 	/*
5116 	 * The driver received a LOGO from the rport and has ACK'd it.
5117 	 * At this point, the driver is done so release the IOCB
5118 	 */
5119 	lpfc_els_free_iocb(phba, cmdiocb);
5120 	lpfc_nlp_put(ndlp);
5121 }
5122 
5123 /**
5124  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
5125  * @phba: pointer to lpfc hba data structure.
5126  * @pmb: pointer to the driver internal queue element for mailbox command.
5127  *
5128  * This routine is the completion callback function for unregister default
5129  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
5130  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
5131  * decrements the ndlp reference count held for this completion callback
5132  * function. After that, it invokes the lpfc_nlp_not_used() to check
5133  * whether there is only one reference left on the ndlp. If so, it will
5134  * perform one more decrement and trigger the release of the ndlp.
5135  **/
5136 void
5137 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5138 {
5139 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
5140 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
5141 	u32 mbx_flag = pmb->mbox_flag;
5142 	u32 mbx_cmd = pmb->u.mb.mbxCommand;
5143 
5144 	pmb->ctx_buf = NULL;
5145 	pmb->ctx_ndlp = NULL;
5146 
5147 	if (ndlp) {
5148 		lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
5149 				 "0006 rpi x%x DID:%x flg:%x %d x%px "
5150 				 "mbx_cmd x%x mbx_flag x%x x%px\n",
5151 				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5152 				 kref_read(&ndlp->kref), ndlp, mbx_cmd,
5153 				 mbx_flag, pmb);
5154 
5155 		/* This ends the default/temporary RPI cleanup logic for this
5156 		 * ndlp and the node and rpi needs to be released. Free the rpi
5157 		 * first on an UNREG_LOGIN and then release the final
5158 		 * references.
5159 		 */
5160 		spin_lock_irq(&ndlp->lock);
5161 		ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
5162 		if (mbx_cmd == MBX_UNREG_LOGIN)
5163 			ndlp->nlp_flag &= ~NLP_UNREG_INP;
5164 		spin_unlock_irq(&ndlp->lock);
5165 		lpfc_nlp_put(ndlp);
5166 		lpfc_drop_node(ndlp->vport, ndlp);
5167 	}
5168 
5169 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
5170 	kfree(mp);
5171 	mempool_free(pmb, phba->mbox_mem_pool);
5172 	return;
5173 }
5174 
5175 /**
5176  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
5177  * @phba: pointer to lpfc hba data structure.
5178  * @cmdiocb: pointer to lpfc command iocb data structure.
5179  * @rspiocb: pointer to lpfc response iocb data structure.
5180  *
5181  * This routine is the completion callback function for ELS Response IOCB
5182  * command. In normal case, this callback function just properly sets the
5183  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
5184  * field in the command IOCB is not NULL, the referred mailbox command will
5185  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
5186  * the IOCB.
5187  **/
5188 static void
5189 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5190 		  struct lpfc_iocbq *rspiocb)
5191 {
5192 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
5193 	struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
5194 	struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
5195 	IOCB_t  *irsp;
5196 	LPFC_MBOXQ_t *mbox = NULL;
5197 	struct lpfc_dmabuf *mp = NULL;
5198 
5199 	irsp = &rspiocb->iocb;
5200 
5201 	if (!vport) {
5202 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5203 				"3177 ELS response failed\n");
5204 		goto out;
5205 	}
5206 	if (cmdiocb->context_un.mbox)
5207 		mbox = cmdiocb->context_un.mbox;
5208 
5209 	/* Check to see if link went down during discovery */
5210 	if (!ndlp || lpfc_els_chk_latt(vport)) {
5211 		if (mbox) {
5212 			mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
5213 			if (mp) {
5214 				lpfc_mbuf_free(phba, mp->virt, mp->phys);
5215 				kfree(mp);
5216 			}
5217 			mempool_free(mbox, phba->mbox_mem_pool);
5218 		}
5219 		goto out;
5220 	}
5221 
5222 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5223 		"ELS rsp cmpl:    status:x%x/x%x did:x%x",
5224 		irsp->ulpStatus, irsp->un.ulpWord[4],
5225 		cmdiocb->iocb.un.elsreq64.remoteID);
5226 	/* ELS response tag <ulpIoTag> completes */
5227 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5228 			 "0110 ELS response tag x%x completes "
5229 			 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%px\n",
5230 			 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
5231 			 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
5232 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5233 			 ndlp->nlp_rpi, kref_read(&ndlp->kref), mbox);
5234 	if (mbox) {
5235 		if ((rspiocb->iocb.ulpStatus == 0) &&
5236 		    (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
5237 			if (!lpfc_unreg_rpi(vport, ndlp) &&
5238 			    (!(vport->fc_flag & FC_PT2PT))) {
5239 				if (ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE) {
5240 					lpfc_printf_vlog(vport, KERN_INFO,
5241 							 LOG_DISCOVERY,
5242 							 "0314 PLOGI recov "
5243 							 "DID x%x "
5244 							 "Data: x%x x%x x%x\n",
5245 							 ndlp->nlp_DID,
5246 							 ndlp->nlp_state,
5247 							 ndlp->nlp_rpi,
5248 							 ndlp->nlp_flag);
5249 					mp = mbox->ctx_buf;
5250 					if (mp) {
5251 						lpfc_mbuf_free(phba, mp->virt,
5252 							       mp->phys);
5253 						kfree(mp);
5254 					}
5255 					mempool_free(mbox, phba->mbox_mem_pool);
5256 					goto out;
5257 				}
5258 			}
5259 
5260 			/* Increment reference count to ndlp to hold the
5261 			 * reference to ndlp for the callback function.
5262 			 */
5263 			mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
5264 			if (!mbox->ctx_ndlp)
5265 				goto out;
5266 
5267 			mbox->vport = vport;
5268 			if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
5269 				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5270 				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5271 			}
5272 			else {
5273 				mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
5274 				ndlp->nlp_prev_state = ndlp->nlp_state;
5275 				lpfc_nlp_set_state(vport, ndlp,
5276 					   NLP_STE_REG_LOGIN_ISSUE);
5277 			}
5278 
5279 			ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
5280 			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5281 			    != MBX_NOT_FINISHED)
5282 				goto out;
5283 
5284 			/* Decrement the ndlp reference count we
5285 			 * set for this failed mailbox command.
5286 			 */
5287 			lpfc_nlp_put(ndlp);
5288 			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
5289 
5290 			/* ELS rsp: Cannot issue reg_login for <NPortid> */
5291 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5292 				"0138 ELS rsp: Cannot issue reg_login for x%x "
5293 				"Data: x%x x%x x%x\n",
5294 				ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5295 				ndlp->nlp_rpi);
5296 		}
5297 		mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
5298 		if (mp) {
5299 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
5300 			kfree(mp);
5301 		}
5302 		mempool_free(mbox, phba->mbox_mem_pool);
5303 	}
5304 out:
5305 	if (ndlp && shost) {
5306 		spin_lock_irq(&ndlp->lock);
5307 		if (mbox)
5308 			ndlp->nlp_flag &= ~NLP_ACC_REGLOGIN;
5309 		ndlp->nlp_flag &= ~NLP_RM_DFLT_RPI;
5310 		spin_unlock_irq(&ndlp->lock);
5311 	}
5312 
5313 	/* An SLI4 NPIV instance wants to drop the node at this point under
5314 	 * these conditions and release the RPI.
5315 	 */
5316 	if (phba->sli_rev == LPFC_SLI_REV4 &&
5317 	    (vport && vport->port_type == LPFC_NPIV_PORT) &&
5318 	    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD) &&
5319 	    ndlp->nlp_flag & NLP_RELEASE_RPI) {
5320 		lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
5321 		spin_lock_irq(&ndlp->lock);
5322 		ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5323 		ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5324 		spin_unlock_irq(&ndlp->lock);
5325 		lpfc_drop_node(vport, ndlp);
5326 	}
5327 
5328 	/* Release the originating I/O reference. */
5329 	lpfc_els_free_iocb(phba, cmdiocb);
5330 	lpfc_nlp_put(ndlp);
5331 	return;
5332 }
5333 
5334 /**
5335  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
5336  * @vport: pointer to a host virtual N_Port data structure.
5337  * @flag: the els command code to be accepted.
5338  * @oldiocb: pointer to the original lpfc command iocb data structure.
5339  * @ndlp: pointer to a node-list data structure.
5340  * @mbox: pointer to the driver internal queue element for mailbox command.
5341  *
5342  * This routine prepares and issues an Accept (ACC) response IOCB
5343  * command. It uses the @flag to properly set up the IOCB field for the
5344  * specific ACC response command to be issued and invokes the
5345  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
5346  * @mbox pointer is passed in, it will be put into the context_un.mbox
5347  * field of the IOCB for the completion callback function to issue the
5348  * mailbox command to the HBA later when callback is invoked.
5349  *
5350  * Note that the ndlp reference count will be incremented by 1 for holding the
5351  * ndlp and the reference to ndlp will be stored into the context1 field of
5352  * the IOCB for the completion callback function to the corresponding
5353  * response ELS IOCB command.
5354  *
5355  * Return code
5356  *   0 - Successfully issued acc response
5357  *   1 - Failed to issue acc response
5358  **/
5359 int
5360 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
5361 		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5362 		 LPFC_MBOXQ_t *mbox)
5363 {
5364 	struct lpfc_hba  *phba = vport->phba;
5365 	IOCB_t *icmd;
5366 	IOCB_t *oldcmd;
5367 	struct lpfc_iocbq *elsiocb;
5368 	uint8_t *pcmd;
5369 	struct serv_parm *sp;
5370 	uint16_t cmdsize;
5371 	int rc;
5372 	ELS_PKT *els_pkt_ptr;
5373 	struct fc_els_rdf_resp *rdf_resp;
5374 
5375 	oldcmd = &oldiocb->iocb;
5376 
5377 	switch (flag) {
5378 	case ELS_CMD_ACC:
5379 		cmdsize = sizeof(uint32_t);
5380 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5381 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5382 		if (!elsiocb) {
5383 			spin_lock_irq(&ndlp->lock);
5384 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5385 			spin_unlock_irq(&ndlp->lock);
5386 			return 1;
5387 		}
5388 
5389 		icmd = &elsiocb->iocb;
5390 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5391 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5392 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5393 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5394 		pcmd += sizeof(uint32_t);
5395 
5396 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5397 			"Issue ACC:       did:x%x flg:x%x",
5398 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
5399 		break;
5400 	case ELS_CMD_FLOGI:
5401 	case ELS_CMD_PLOGI:
5402 		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
5403 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5404 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5405 		if (!elsiocb)
5406 			return 1;
5407 
5408 		icmd = &elsiocb->iocb;
5409 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5410 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5411 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5412 
5413 		if (mbox)
5414 			elsiocb->context_un.mbox = mbox;
5415 
5416 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5417 		pcmd += sizeof(uint32_t);
5418 		sp = (struct serv_parm *)pcmd;
5419 
5420 		if (flag == ELS_CMD_FLOGI) {
5421 			/* Copy the received service parameters back */
5422 			memcpy(sp, &phba->fc_fabparam,
5423 			       sizeof(struct serv_parm));
5424 
5425 			/* Clear the F_Port bit */
5426 			sp->cmn.fPort = 0;
5427 
5428 			/* Mark all class service parameters as invalid */
5429 			sp->cls1.classValid = 0;
5430 			sp->cls2.classValid = 0;
5431 			sp->cls3.classValid = 0;
5432 			sp->cls4.classValid = 0;
5433 
5434 			/* Copy our worldwide names */
5435 			memcpy(&sp->portName, &vport->fc_sparam.portName,
5436 			       sizeof(struct lpfc_name));
5437 			memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
5438 			       sizeof(struct lpfc_name));
5439 		} else {
5440 			memcpy(pcmd, &vport->fc_sparam,
5441 			       sizeof(struct serv_parm));
5442 
5443 			sp->cmn.valid_vendor_ver_level = 0;
5444 			memset(sp->un.vendorVersion, 0,
5445 			       sizeof(sp->un.vendorVersion));
5446 			sp->cmn.bbRcvSizeMsb &= 0xF;
5447 
5448 			/* If our firmware supports this feature, convey that
5449 			 * info to the target using the vendor specific field.
5450 			 */
5451 			if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
5452 				sp->cmn.valid_vendor_ver_level = 1;
5453 				sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
5454 				sp->un.vv.flags =
5455 					cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
5456 			}
5457 		}
5458 
5459 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5460 			"Issue ACC FLOGI/PLOGI: did:x%x flg:x%x",
5461 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
5462 		break;
5463 	case ELS_CMD_PRLO:
5464 		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
5465 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5466 					     ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
5467 		if (!elsiocb)
5468 			return 1;
5469 
5470 		icmd = &elsiocb->iocb;
5471 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5472 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5473 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5474 
5475 		memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
5476 		       sizeof(uint32_t) + sizeof(PRLO));
5477 		*((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
5478 		els_pkt_ptr = (ELS_PKT *) pcmd;
5479 		els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
5480 
5481 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5482 			"Issue ACC PRLO:  did:x%x flg:x%x",
5483 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
5484 		break;
5485 	case ELS_CMD_RDF:
5486 		cmdsize = sizeof(*rdf_resp);
5487 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5488 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5489 		if (!elsiocb)
5490 			return 1;
5491 
5492 		icmd = &elsiocb->iocb;
5493 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5494 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5495 		pcmd = (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5496 		rdf_resp = (struct fc_els_rdf_resp *)pcmd;
5497 		memset(rdf_resp, 0, sizeof(*rdf_resp));
5498 		rdf_resp->acc_hdr.la_cmd = ELS_LS_ACC;
5499 
5500 		/* FC-LS-5 specifies desc_list_len shall be set to 12 */
5501 		rdf_resp->desc_list_len = cpu_to_be32(12);
5502 
5503 		/* FC-LS-5 specifies LS REQ Information descriptor */
5504 		rdf_resp->lsri.desc_tag = cpu_to_be32(1);
5505 		rdf_resp->lsri.desc_len = cpu_to_be32(sizeof(u32));
5506 		rdf_resp->lsri.rqst_w0.cmd = ELS_RDF;
5507 		break;
5508 	default:
5509 		return 1;
5510 	}
5511 	if (ndlp->nlp_flag & NLP_LOGO_ACC) {
5512 		spin_lock_irq(&ndlp->lock);
5513 		if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED ||
5514 			ndlp->nlp_flag & NLP_REG_LOGIN_SEND))
5515 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5516 		spin_unlock_irq(&ndlp->lock);
5517 		elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
5518 	} else {
5519 		elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5520 	}
5521 
5522 	phba->fc_stat.elsXmitACC++;
5523 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5524 	if (!elsiocb->context1) {
5525 		lpfc_els_free_iocb(phba, elsiocb);
5526 		return 1;
5527 	}
5528 
5529 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5530 	if (rc == IOCB_ERROR) {
5531 		lpfc_els_free_iocb(phba, elsiocb);
5532 		lpfc_nlp_put(ndlp);
5533 		return 1;
5534 	}
5535 
5536 	/* Xmit ELS ACC response tag <ulpIoTag> */
5537 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5538 			 "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
5539 			 "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
5540 			 "RPI: x%x, fc_flag x%x refcnt %d\n",
5541 			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
5542 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5543 			 ndlp->nlp_rpi, vport->fc_flag, kref_read(&ndlp->kref));
5544 	return 0;
5545 }
5546 
5547 /**
5548  * lpfc_els_rsp_reject - Prepare and issue a rjt response iocb command
5549  * @vport: pointer to a virtual N_Port data structure.
5550  * @rejectError: reject response to issue
5551  * @oldiocb: pointer to the original lpfc command iocb data structure.
5552  * @ndlp: pointer to a node-list data structure.
5553  * @mbox: pointer to the driver internal queue element for mailbox command.
5554  *
5555  * This routine prepares and issue an Reject (RJT) response IOCB
5556  * command. If a @mbox pointer is passed in, it will be put into the
5557  * context_un.mbox field of the IOCB for the completion callback function
5558  * to issue to the HBA later.
5559  *
5560  * Note that the ndlp reference count will be incremented by 1 for holding the
5561  * ndlp and the reference to ndlp will be stored into the context1 field of
5562  * the IOCB for the completion callback function to the reject response
5563  * ELS IOCB command.
5564  *
5565  * Return code
5566  *   0 - Successfully issued reject response
5567  *   1 - Failed to issue reject response
5568  **/
5569 int
5570 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
5571 		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5572 		    LPFC_MBOXQ_t *mbox)
5573 {
5574 	int rc;
5575 	struct lpfc_hba  *phba = vport->phba;
5576 	IOCB_t *icmd;
5577 	IOCB_t *oldcmd;
5578 	struct lpfc_iocbq *elsiocb;
5579 	uint8_t *pcmd;
5580 	uint16_t cmdsize;
5581 
5582 	cmdsize = 2 * sizeof(uint32_t);
5583 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5584 				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
5585 	if (!elsiocb)
5586 		return 1;
5587 
5588 	icmd = &elsiocb->iocb;
5589 	oldcmd = &oldiocb->iocb;
5590 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5591 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5592 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5593 
5594 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
5595 	pcmd += sizeof(uint32_t);
5596 	*((uint32_t *) (pcmd)) = rejectError;
5597 
5598 	if (mbox)
5599 		elsiocb->context_un.mbox = mbox;
5600 
5601 	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
5602 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5603 			 "0129 Xmit ELS RJT x%x response tag x%x "
5604 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
5605 			 "rpi x%x\n",
5606 			 rejectError, elsiocb->iotag,
5607 			 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
5608 			 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
5609 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5610 		"Issue LS_RJT:    did:x%x flg:x%x err:x%x",
5611 		ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
5612 
5613 	phba->fc_stat.elsXmitLSRJT++;
5614 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5615 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5616 	if (!elsiocb->context1) {
5617 		lpfc_els_free_iocb(phba, elsiocb);
5618 		return 1;
5619 	}
5620 
5621 	/* The NPIV instance is rejecting this unsolicited ELS. Make sure the
5622 	 * node's assigned RPI gets released provided this node is not already
5623 	 * registered with the transport.
5624 	 */
5625 	if (phba->sli_rev == LPFC_SLI_REV4 &&
5626 	    vport->port_type == LPFC_NPIV_PORT &&
5627 	    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
5628 		spin_lock_irq(&ndlp->lock);
5629 		ndlp->nlp_flag |= NLP_RELEASE_RPI;
5630 		spin_unlock_irq(&ndlp->lock);
5631 	}
5632 
5633 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5634 	if (rc == IOCB_ERROR) {
5635 		lpfc_els_free_iocb(phba, elsiocb);
5636 		lpfc_nlp_put(ndlp);
5637 		return 1;
5638 	}
5639 
5640 	return 0;
5641 }
5642 
5643  /**
5644   * lpfc_issue_els_edc_rsp - Exchange Diagnostic Capabilities with the fabric.
5645   * @vport: pointer to a host virtual N_Port data structure.
5646   * @cmdiocb: pointer to the original lpfc command iocb data structure.
5647   * @ndlp: NPort to where rsp is directed
5648   *
5649   * This routine issues an EDC ACC RSP to the F-Port Controller to communicate
5650   * this N_Port's support of hardware signals in its Congestion
5651   * Capabilities Descriptor.
5652   *
5653   * Return code
5654   *   0 - Successfully issued edc rsp command
5655   *   1 - Failed to issue edc rsp command
5656   **/
5657 static int
5658 lpfc_issue_els_edc_rsp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5659 		       struct lpfc_nodelist *ndlp)
5660 {
5661 	struct lpfc_hba  *phba = vport->phba;
5662 	struct lpfc_els_edc_rsp *edc_rsp;
5663 	struct lpfc_iocbq *elsiocb;
5664 	IOCB_t *icmd, *cmd;
5665 	uint8_t *pcmd;
5666 	int cmdsize, rc;
5667 
5668 	cmdsize = sizeof(struct lpfc_els_edc_rsp);
5669 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, cmdiocb->retry,
5670 				     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5671 	if (!elsiocb)
5672 		return 1;
5673 
5674 	icmd = &elsiocb->iocb;
5675 	cmd = &cmdiocb->iocb;
5676 	icmd->ulpContext = cmd->ulpContext;     /* Xri / rx_id */
5677 	icmd->unsli3.rcvsli3.ox_id = cmd->unsli3.rcvsli3.ox_id;
5678 	pcmd = (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5679 	memset(pcmd, 0, cmdsize);
5680 
5681 	edc_rsp = (struct lpfc_els_edc_rsp *)pcmd;
5682 	edc_rsp->edc_rsp.acc_hdr.la_cmd = ELS_LS_ACC;
5683 	edc_rsp->edc_rsp.desc_list_len = cpu_to_be32(
5684 		FC_TLV_DESC_LENGTH_FROM_SZ(struct lpfc_els_edc_rsp));
5685 	edc_rsp->edc_rsp.lsri.desc_tag = cpu_to_be32(ELS_DTAG_LS_REQ_INFO);
5686 	edc_rsp->edc_rsp.lsri.desc_len = cpu_to_be32(
5687 		FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_els_lsri_desc));
5688 	edc_rsp->edc_rsp.lsri.rqst_w0.cmd = ELS_EDC;
5689 	lpfc_format_edc_cgn_desc(phba, &edc_rsp->cgn_desc);
5690 
5691 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5692 			      "Issue EDC ACC:      did:x%x flg:x%x refcnt %d",
5693 			      ndlp->nlp_DID, ndlp->nlp_flag,
5694 			      kref_read(&ndlp->kref));
5695 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5696 
5697 	phba->fc_stat.elsXmitACC++;
5698 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5699 	if (!elsiocb->context1) {
5700 		lpfc_els_free_iocb(phba, elsiocb);
5701 		return 1;
5702 	}
5703 
5704 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5705 	if (rc == IOCB_ERROR) {
5706 		lpfc_els_free_iocb(phba, elsiocb);
5707 		lpfc_nlp_put(ndlp);
5708 		return 1;
5709 	}
5710 
5711 	/* Xmit ELS ACC response tag <ulpIoTag> */
5712 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5713 			 "0152 Xmit EDC ACC response Status: x%x, IoTag: x%x, "
5714 			 "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
5715 			 "RPI: x%x, fc_flag x%x\n",
5716 			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
5717 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5718 			 ndlp->nlp_rpi, vport->fc_flag);
5719 
5720 	return 0;
5721 }
5722 
5723 /**
5724  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
5725  * @vport: pointer to a virtual N_Port data structure.
5726  * @oldiocb: pointer to the original lpfc command iocb data structure.
5727  * @ndlp: pointer to a node-list data structure.
5728  *
5729  * This routine prepares and issues an Accept (ACC) response to Address
5730  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
5731  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
5732  *
5733  * Note that the ndlp reference count will be incremented by 1 for holding the
5734  * ndlp and the reference to ndlp will be stored into the context1 field of
5735  * the IOCB for the completion callback function to the ADISC Accept response
5736  * ELS IOCB command.
5737  *
5738  * Return code
5739  *   0 - Successfully issued acc adisc response
5740  *   1 - Failed to issue adisc acc response
5741  **/
5742 int
5743 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5744 		       struct lpfc_nodelist *ndlp)
5745 {
5746 	struct lpfc_hba  *phba = vport->phba;
5747 	ADISC *ap;
5748 	IOCB_t *icmd, *oldcmd;
5749 	struct lpfc_iocbq *elsiocb;
5750 	uint8_t *pcmd;
5751 	uint16_t cmdsize;
5752 	int rc;
5753 
5754 	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
5755 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5756 				     ndlp->nlp_DID, ELS_CMD_ACC);
5757 	if (!elsiocb)
5758 		return 1;
5759 
5760 	icmd = &elsiocb->iocb;
5761 	oldcmd = &oldiocb->iocb;
5762 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5763 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5764 
5765 	/* Xmit ADISC ACC response tag <ulpIoTag> */
5766 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5767 			 "0130 Xmit ADISC ACC response iotag x%x xri: "
5768 			 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
5769 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
5770 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5771 			 ndlp->nlp_rpi);
5772 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5773 
5774 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5775 	pcmd += sizeof(uint32_t);
5776 
5777 	ap = (ADISC *) (pcmd);
5778 	ap->hardAL_PA = phba->fc_pref_ALPA;
5779 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
5780 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
5781 	ap->DID = be32_to_cpu(vport->fc_myDID);
5782 
5783 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5784 		      "Issue ACC ADISC: did:x%x flg:x%x refcnt %d",
5785 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
5786 
5787 	phba->fc_stat.elsXmitACC++;
5788 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5789 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5790 	if (!elsiocb->context1) {
5791 		lpfc_els_free_iocb(phba, elsiocb);
5792 		return 1;
5793 	}
5794 
5795 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5796 	if (rc == IOCB_ERROR) {
5797 		lpfc_els_free_iocb(phba, elsiocb);
5798 		lpfc_nlp_put(ndlp);
5799 		return 1;
5800 	}
5801 
5802 	/* Xmit ELS ACC response tag <ulpIoTag> */
5803 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5804 			 "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
5805 			 "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
5806 			 "RPI: x%x, fc_flag x%x\n",
5807 			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
5808 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5809 			 ndlp->nlp_rpi, vport->fc_flag);
5810 	return 0;
5811 }
5812 
5813 /**
5814  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
5815  * @vport: pointer to a virtual N_Port data structure.
5816  * @oldiocb: pointer to the original lpfc command iocb data structure.
5817  * @ndlp: pointer to a node-list data structure.
5818  *
5819  * This routine prepares and issues an Accept (ACC) response to Process
5820  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
5821  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
5822  *
5823  * Note that the ndlp reference count will be incremented by 1 for holding the
5824  * ndlp and the reference to ndlp will be stored into the context1 field of
5825  * the IOCB for the completion callback function to the PRLI Accept response
5826  * ELS IOCB command.
5827  *
5828  * Return code
5829  *   0 - Successfully issued acc prli response
5830  *   1 - Failed to issue acc prli response
5831  **/
5832 int
5833 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5834 		      struct lpfc_nodelist *ndlp)
5835 {
5836 	struct lpfc_hba  *phba = vport->phba;
5837 	PRLI *npr;
5838 	struct lpfc_nvme_prli *npr_nvme;
5839 	lpfc_vpd_t *vpd;
5840 	IOCB_t *icmd;
5841 	IOCB_t *oldcmd;
5842 	struct lpfc_iocbq *elsiocb;
5843 	uint8_t *pcmd;
5844 	uint16_t cmdsize;
5845 	uint32_t prli_fc4_req, *req_payload;
5846 	struct lpfc_dmabuf *req_buf;
5847 	int rc;
5848 	u32 elsrspcmd;
5849 
5850 	/* Need the incoming PRLI payload to determine if the ACC is for an
5851 	 * FC4 or NVME PRLI type.  The PRLI type is at word 1.
5852 	 */
5853 	req_buf = (struct lpfc_dmabuf *)oldiocb->context2;
5854 	req_payload = (((uint32_t *)req_buf->virt) + 1);
5855 
5856 	/* PRLI type payload is at byte 3 for FCP or NVME. */
5857 	prli_fc4_req = be32_to_cpu(*req_payload);
5858 	prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
5859 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5860 			 "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
5861 			 prli_fc4_req, *((uint32_t *)req_payload));
5862 
5863 	if (prli_fc4_req == PRLI_FCP_TYPE) {
5864 		cmdsize = sizeof(uint32_t) + sizeof(PRLI);
5865 		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
5866 	} else if (prli_fc4_req & PRLI_NVME_TYPE) {
5867 		cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
5868 		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
5869 	} else {
5870 		return 1;
5871 	}
5872 
5873 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5874 		ndlp->nlp_DID, elsrspcmd);
5875 	if (!elsiocb)
5876 		return 1;
5877 
5878 	icmd = &elsiocb->iocb;
5879 	oldcmd = &oldiocb->iocb;
5880 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5881 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5882 
5883 	/* Xmit PRLI ACC response tag <ulpIoTag> */
5884 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5885 			 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
5886 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
5887 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
5888 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5889 			 ndlp->nlp_rpi);
5890 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5891 	memset(pcmd, 0, cmdsize);
5892 
5893 	*((uint32_t *)(pcmd)) = elsrspcmd;
5894 	pcmd += sizeof(uint32_t);
5895 
5896 	/* For PRLI, remainder of payload is PRLI parameter page */
5897 	vpd = &phba->vpd;
5898 
5899 	if (prli_fc4_req == PRLI_FCP_TYPE) {
5900 		/*
5901 		 * If the remote port is a target and our firmware version
5902 		 * is 3.20 or later, set the following bits for FC-TAPE
5903 		 * support.
5904 		 */
5905 		npr = (PRLI *) pcmd;
5906 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
5907 		    (vpd->rev.feaLevelHigh >= 0x02)) {
5908 			npr->ConfmComplAllowed = 1;
5909 			npr->Retry = 1;
5910 			npr->TaskRetryIdReq = 1;
5911 		}
5912 		npr->acceptRspCode = PRLI_REQ_EXECUTED;
5913 		npr->estabImagePair = 1;
5914 		npr->readXferRdyDis = 1;
5915 		npr->ConfmComplAllowed = 1;
5916 		npr->prliType = PRLI_FCP_TYPE;
5917 		npr->initiatorFunc = 1;
5918 	} else if (prli_fc4_req & PRLI_NVME_TYPE) {
5919 		/* Respond with an NVME PRLI Type */
5920 		npr_nvme = (struct lpfc_nvme_prli *) pcmd;
5921 		bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
5922 		bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
5923 		bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
5924 		if (phba->nvmet_support) {
5925 			bf_set(prli_tgt, npr_nvme, 1);
5926 			bf_set(prli_disc, npr_nvme, 1);
5927 			if (phba->cfg_nvme_enable_fb) {
5928 				bf_set(prli_fba, npr_nvme, 1);
5929 
5930 				/* TBD.  Target mode needs to post buffers
5931 				 * that support the configured first burst
5932 				 * byte size.
5933 				 */
5934 				bf_set(prli_fb_sz, npr_nvme,
5935 				       phba->cfg_nvmet_fb_size);
5936 			}
5937 		} else {
5938 			bf_set(prli_init, npr_nvme, 1);
5939 		}
5940 
5941 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
5942 				 "6015 NVME issue PRLI ACC word1 x%08x "
5943 				 "word4 x%08x word5 x%08x flag x%x, "
5944 				 "fcp_info x%x nlp_type x%x\n",
5945 				 npr_nvme->word1, npr_nvme->word4,
5946 				 npr_nvme->word5, ndlp->nlp_flag,
5947 				 ndlp->nlp_fcp_info, ndlp->nlp_type);
5948 		npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
5949 		npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
5950 		npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
5951 	} else
5952 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5953 				 "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
5954 				 prli_fc4_req, ndlp->nlp_fc4_type,
5955 				 ndlp->nlp_DID);
5956 
5957 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5958 		      "Issue ACC PRLI:  did:x%x flg:x%x",
5959 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
5960 
5961 	phba->fc_stat.elsXmitACC++;
5962 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5963 	elsiocb->context1 =  lpfc_nlp_get(ndlp);
5964 	if (!elsiocb->context1) {
5965 		lpfc_els_free_iocb(phba, elsiocb);
5966 		return 1;
5967 	}
5968 
5969 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5970 	if (rc == IOCB_ERROR) {
5971 		lpfc_els_free_iocb(phba, elsiocb);
5972 		lpfc_nlp_put(ndlp);
5973 		return 1;
5974 	}
5975 
5976 	return 0;
5977 }
5978 
5979 /**
5980  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
5981  * @vport: pointer to a virtual N_Port data structure.
5982  * @format: rnid command format.
5983  * @oldiocb: pointer to the original lpfc command iocb data structure.
5984  * @ndlp: pointer to a node-list data structure.
5985  *
5986  * This routine issues a Request Node Identification Data (RNID) Accept
5987  * (ACC) response. It constructs the RNID ACC response command according to
5988  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
5989  * issue the response.
5990  *
5991  * Note that the ndlp reference count will be incremented by 1 for holding the
5992  * ndlp and the reference to ndlp will be stored into the context1 field of
5993  * the IOCB for the completion callback function.
5994  *
5995  * Return code
5996  *   0 - Successfully issued acc rnid response
5997  *   1 - Failed to issue acc rnid response
5998  **/
5999 static int
6000 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
6001 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6002 {
6003 	struct lpfc_hba  *phba = vport->phba;
6004 	RNID *rn;
6005 	IOCB_t *icmd, *oldcmd;
6006 	struct lpfc_iocbq *elsiocb;
6007 	uint8_t *pcmd;
6008 	uint16_t cmdsize;
6009 	int rc;
6010 
6011 	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
6012 					+ (2 * sizeof(struct lpfc_name));
6013 	if (format)
6014 		cmdsize += sizeof(RNID_TOP_DISC);
6015 
6016 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6017 				     ndlp->nlp_DID, ELS_CMD_ACC);
6018 	if (!elsiocb)
6019 		return 1;
6020 
6021 	icmd = &elsiocb->iocb;
6022 	oldcmd = &oldiocb->iocb;
6023 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
6024 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
6025 
6026 	/* Xmit RNID ACC response tag <ulpIoTag> */
6027 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6028 			 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
6029 			 elsiocb->iotag, elsiocb->iocb.ulpContext);
6030 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6031 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6032 	pcmd += sizeof(uint32_t);
6033 
6034 	memset(pcmd, 0, sizeof(RNID));
6035 	rn = (RNID *) (pcmd);
6036 	rn->Format = format;
6037 	rn->CommonLen = (2 * sizeof(struct lpfc_name));
6038 	memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
6039 	memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
6040 	switch (format) {
6041 	case 0:
6042 		rn->SpecificLen = 0;
6043 		break;
6044 	case RNID_TOPOLOGY_DISC:
6045 		rn->SpecificLen = sizeof(RNID_TOP_DISC);
6046 		memcpy(&rn->un.topologyDisc.portName,
6047 		       &vport->fc_portname, sizeof(struct lpfc_name));
6048 		rn->un.topologyDisc.unitType = RNID_HBA;
6049 		rn->un.topologyDisc.physPort = 0;
6050 		rn->un.topologyDisc.attachedNodes = 0;
6051 		break;
6052 	default:
6053 		rn->CommonLen = 0;
6054 		rn->SpecificLen = 0;
6055 		break;
6056 	}
6057 
6058 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6059 		      "Issue ACC RNID:  did:x%x flg:x%x refcnt %d",
6060 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6061 
6062 	phba->fc_stat.elsXmitACC++;
6063 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6064 	elsiocb->context1 = lpfc_nlp_get(ndlp);
6065 	if (!elsiocb->context1) {
6066 		lpfc_els_free_iocb(phba, elsiocb);
6067 		return 1;
6068 	}
6069 
6070 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6071 	if (rc == IOCB_ERROR) {
6072 		lpfc_els_free_iocb(phba, elsiocb);
6073 		lpfc_nlp_put(ndlp);
6074 		return 1;
6075 	}
6076 
6077 	return 0;
6078 }
6079 
6080 /**
6081  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
6082  * @vport: pointer to a virtual N_Port data structure.
6083  * @iocb: pointer to the lpfc command iocb data structure.
6084  * @ndlp: pointer to a node-list data structure.
6085  *
6086  * Return
6087  **/
6088 static void
6089 lpfc_els_clear_rrq(struct lpfc_vport *vport,
6090 		   struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
6091 {
6092 	struct lpfc_hba  *phba = vport->phba;
6093 	uint8_t *pcmd;
6094 	struct RRQ *rrq;
6095 	uint16_t rxid;
6096 	uint16_t xri;
6097 	struct lpfc_node_rrq *prrq;
6098 
6099 
6100 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
6101 	pcmd += sizeof(uint32_t);
6102 	rrq = (struct RRQ *)pcmd;
6103 	rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
6104 	rxid = bf_get(rrq_rxid, rrq);
6105 
6106 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6107 			"2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
6108 			" x%x x%x\n",
6109 			be32_to_cpu(bf_get(rrq_did, rrq)),
6110 			bf_get(rrq_oxid, rrq),
6111 			rxid,
6112 			iocb->iotag, iocb->iocb.ulpContext);
6113 
6114 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6115 		"Clear RRQ:  did:x%x flg:x%x exchg:x%.08x",
6116 		ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
6117 	if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
6118 		xri = bf_get(rrq_oxid, rrq);
6119 	else
6120 		xri = rxid;
6121 	prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
6122 	if (prrq)
6123 		lpfc_clr_rrq_active(phba, xri, prrq);
6124 	return;
6125 }
6126 
6127 /**
6128  * lpfc_els_rsp_echo_acc - Issue echo acc response
6129  * @vport: pointer to a virtual N_Port data structure.
6130  * @data: pointer to echo data to return in the accept.
6131  * @oldiocb: pointer to the original lpfc command iocb data structure.
6132  * @ndlp: pointer to a node-list data structure.
6133  *
6134  * Return code
6135  *   0 - Successfully issued acc echo response
6136  *   1 - Failed to issue acc echo response
6137  **/
6138 static int
6139 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
6140 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6141 {
6142 	struct lpfc_hba  *phba = vport->phba;
6143 	struct lpfc_iocbq *elsiocb;
6144 	uint8_t *pcmd;
6145 	uint16_t cmdsize;
6146 	int rc;
6147 
6148 	cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
6149 
6150 	/* The accumulated length can exceed the BPL_SIZE.  For
6151 	 * now, use this as the limit
6152 	 */
6153 	if (cmdsize > LPFC_BPL_SIZE)
6154 		cmdsize = LPFC_BPL_SIZE;
6155 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6156 				     ndlp->nlp_DID, ELS_CMD_ACC);
6157 	if (!elsiocb)
6158 		return 1;
6159 
6160 	elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext;  /* Xri / rx_id */
6161 	elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
6162 
6163 	/* Xmit ECHO ACC response tag <ulpIoTag> */
6164 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6165 			 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
6166 			 elsiocb->iotag, elsiocb->iocb.ulpContext);
6167 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6168 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6169 	pcmd += sizeof(uint32_t);
6170 	memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
6171 
6172 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6173 		      "Issue ACC ECHO:  did:x%x flg:x%x refcnt %d",
6174 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6175 
6176 	phba->fc_stat.elsXmitACC++;
6177 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6178 	elsiocb->context1 =  lpfc_nlp_get(ndlp);
6179 	if (!elsiocb->context1) {
6180 		lpfc_els_free_iocb(phba, elsiocb);
6181 		return 1;
6182 	}
6183 
6184 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6185 	if (rc == IOCB_ERROR) {
6186 		lpfc_els_free_iocb(phba, elsiocb);
6187 		lpfc_nlp_put(ndlp);
6188 		return 1;
6189 	}
6190 
6191 	return 0;
6192 }
6193 
6194 /**
6195  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
6196  * @vport: pointer to a host virtual N_Port data structure.
6197  *
6198  * This routine issues Address Discover (ADISC) ELS commands to those
6199  * N_Ports which are in node port recovery state and ADISC has not been issued
6200  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
6201  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
6202  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
6203  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
6204  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
6205  * IOCBs quit for later pick up. On the other hand, after walking through
6206  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
6207  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
6208  * no more ADISC need to be sent.
6209  *
6210  * Return code
6211  *    The number of N_Ports with adisc issued.
6212  **/
6213 int
6214 lpfc_els_disc_adisc(struct lpfc_vport *vport)
6215 {
6216 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6217 	struct lpfc_nodelist *ndlp, *next_ndlp;
6218 	int sentadisc = 0;
6219 
6220 	/* go thru NPR nodes and issue any remaining ELS ADISCs */
6221 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6222 
6223 		if (ndlp->nlp_state != NLP_STE_NPR_NODE ||
6224 		    !(ndlp->nlp_flag & NLP_NPR_ADISC))
6225 			continue;
6226 
6227 		spin_lock_irq(&ndlp->lock);
6228 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
6229 		spin_unlock_irq(&ndlp->lock);
6230 
6231 		if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
6232 			/* This node was marked for ADISC but was not picked
6233 			 * for discovery. This is possible if the node was
6234 			 * missing in gidft response.
6235 			 *
6236 			 * At time of marking node for ADISC, we skipped unreg
6237 			 * from backend
6238 			 */
6239 			lpfc_nlp_unreg_node(vport, ndlp);
6240 			lpfc_unreg_rpi(vport, ndlp);
6241 			continue;
6242 		}
6243 
6244 		ndlp->nlp_prev_state = ndlp->nlp_state;
6245 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
6246 		lpfc_issue_els_adisc(vport, ndlp, 0);
6247 		sentadisc++;
6248 		vport->num_disc_nodes++;
6249 		if (vport->num_disc_nodes >=
6250 				vport->cfg_discovery_threads) {
6251 			spin_lock_irq(shost->host_lock);
6252 			vport->fc_flag |= FC_NLP_MORE;
6253 			spin_unlock_irq(shost->host_lock);
6254 			break;
6255 		}
6256 
6257 	}
6258 	if (sentadisc == 0) {
6259 		spin_lock_irq(shost->host_lock);
6260 		vport->fc_flag &= ~FC_NLP_MORE;
6261 		spin_unlock_irq(shost->host_lock);
6262 	}
6263 	return sentadisc;
6264 }
6265 
6266 /**
6267  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
6268  * @vport: pointer to a host virtual N_Port data structure.
6269  *
6270  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
6271  * which are in node port recovery state, with a @vport. Each time an ELS
6272  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
6273  * the per @vport number of discover count (num_disc_nodes) shall be
6274  * incremented. If the num_disc_nodes reaches a pre-configured threshold
6275  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
6276  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
6277  * later pick up. On the other hand, after walking through all the ndlps with
6278  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
6279  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
6280  * PLOGI need to be sent.
6281  *
6282  * Return code
6283  *   The number of N_Ports with plogi issued.
6284  **/
6285 int
6286 lpfc_els_disc_plogi(struct lpfc_vport *vport)
6287 {
6288 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6289 	struct lpfc_nodelist *ndlp, *next_ndlp;
6290 	int sentplogi = 0;
6291 
6292 	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
6293 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6294 		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
6295 				(ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
6296 				(ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
6297 				(ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
6298 			ndlp->nlp_prev_state = ndlp->nlp_state;
6299 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6300 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
6301 			sentplogi++;
6302 			vport->num_disc_nodes++;
6303 			if (vport->num_disc_nodes >=
6304 					vport->cfg_discovery_threads) {
6305 				spin_lock_irq(shost->host_lock);
6306 				vport->fc_flag |= FC_NLP_MORE;
6307 				spin_unlock_irq(shost->host_lock);
6308 				break;
6309 			}
6310 		}
6311 	}
6312 
6313 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6314 			 "6452 Discover PLOGI %d flag x%x\n",
6315 			 sentplogi, vport->fc_flag);
6316 
6317 	if (sentplogi) {
6318 		lpfc_set_disctmo(vport);
6319 	}
6320 	else {
6321 		spin_lock_irq(shost->host_lock);
6322 		vport->fc_flag &= ~FC_NLP_MORE;
6323 		spin_unlock_irq(shost->host_lock);
6324 	}
6325 	return sentplogi;
6326 }
6327 
6328 static uint32_t
6329 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
6330 		uint32_t word0)
6331 {
6332 
6333 	desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
6334 	desc->payload.els_req = word0;
6335 	desc->length = cpu_to_be32(sizeof(desc->payload));
6336 
6337 	return sizeof(struct fc_rdp_link_service_desc);
6338 }
6339 
6340 static uint32_t
6341 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
6342 		uint8_t *page_a0, uint8_t *page_a2)
6343 {
6344 	uint16_t wavelength;
6345 	uint16_t temperature;
6346 	uint16_t rx_power;
6347 	uint16_t tx_bias;
6348 	uint16_t tx_power;
6349 	uint16_t vcc;
6350 	uint16_t flag = 0;
6351 	struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
6352 	struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
6353 
6354 	desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
6355 
6356 	trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
6357 			&page_a0[SSF_TRANSCEIVER_CODE_B4];
6358 	trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
6359 			&page_a0[SSF_TRANSCEIVER_CODE_B5];
6360 
6361 	if ((trasn_code_byte4->fc_sw_laser) ||
6362 	    (trasn_code_byte5->fc_sw_laser_sl) ||
6363 	    (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
6364 		flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
6365 	} else if (trasn_code_byte4->fc_lw_laser) {
6366 		wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
6367 			page_a0[SSF_WAVELENGTH_B0];
6368 		if (wavelength == SFP_WAVELENGTH_LC1310)
6369 			flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
6370 		if (wavelength == SFP_WAVELENGTH_LL1550)
6371 			flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
6372 	}
6373 	/* check if its SFP+ */
6374 	flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
6375 			SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
6376 					<< SFP_FLAG_CT_SHIFT;
6377 
6378 	/* check if its OPTICAL */
6379 	flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
6380 			SFP_FLAG_IS_OPTICAL_PORT : 0)
6381 					<< SFP_FLAG_IS_OPTICAL_SHIFT;
6382 
6383 	temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
6384 		page_a2[SFF_TEMPERATURE_B0]);
6385 	vcc = (page_a2[SFF_VCC_B1] << 8 |
6386 		page_a2[SFF_VCC_B0]);
6387 	tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
6388 		page_a2[SFF_TXPOWER_B0]);
6389 	tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
6390 		page_a2[SFF_TX_BIAS_CURRENT_B0]);
6391 	rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
6392 		page_a2[SFF_RXPOWER_B0]);
6393 	desc->sfp_info.temperature = cpu_to_be16(temperature);
6394 	desc->sfp_info.rx_power = cpu_to_be16(rx_power);
6395 	desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
6396 	desc->sfp_info.tx_power = cpu_to_be16(tx_power);
6397 	desc->sfp_info.vcc = cpu_to_be16(vcc);
6398 
6399 	desc->sfp_info.flags = cpu_to_be16(flag);
6400 	desc->length = cpu_to_be32(sizeof(desc->sfp_info));
6401 
6402 	return sizeof(struct fc_rdp_sfp_desc);
6403 }
6404 
6405 static uint32_t
6406 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
6407 		READ_LNK_VAR *stat)
6408 {
6409 	uint32_t type;
6410 
6411 	desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
6412 
6413 	type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
6414 
6415 	desc->info.port_type = cpu_to_be32(type);
6416 
6417 	desc->info.link_status.link_failure_cnt =
6418 		cpu_to_be32(stat->linkFailureCnt);
6419 	desc->info.link_status.loss_of_synch_cnt =
6420 		cpu_to_be32(stat->lossSyncCnt);
6421 	desc->info.link_status.loss_of_signal_cnt =
6422 		cpu_to_be32(stat->lossSignalCnt);
6423 	desc->info.link_status.primitive_seq_proto_err =
6424 		cpu_to_be32(stat->primSeqErrCnt);
6425 	desc->info.link_status.invalid_trans_word =
6426 		cpu_to_be32(stat->invalidXmitWord);
6427 	desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
6428 
6429 	desc->length = cpu_to_be32(sizeof(desc->info));
6430 
6431 	return sizeof(struct fc_rdp_link_error_status_desc);
6432 }
6433 
6434 static uint32_t
6435 lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
6436 		      struct lpfc_vport *vport)
6437 {
6438 	uint32_t bbCredit;
6439 
6440 	desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
6441 
6442 	bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
6443 			(vport->fc_sparam.cmn.bbCreditMsb << 8);
6444 	desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
6445 	if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
6446 		bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
6447 			(vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
6448 		desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
6449 	} else {
6450 		desc->bbc_info.attached_port_bbc = 0;
6451 	}
6452 
6453 	desc->bbc_info.rtt = 0;
6454 	desc->length = cpu_to_be32(sizeof(desc->bbc_info));
6455 
6456 	return sizeof(struct fc_rdp_bbc_desc);
6457 }
6458 
6459 static uint32_t
6460 lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
6461 			   struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
6462 {
6463 	uint32_t flags = 0;
6464 
6465 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6466 
6467 	desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
6468 	desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
6469 	desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
6470 	desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
6471 
6472 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6473 		flags |= RDP_OET_HIGH_ALARM;
6474 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6475 		flags |= RDP_OET_LOW_ALARM;
6476 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6477 		flags |= RDP_OET_HIGH_WARNING;
6478 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6479 		flags |= RDP_OET_LOW_WARNING;
6480 
6481 	flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
6482 	desc->oed_info.function_flags = cpu_to_be32(flags);
6483 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6484 	return sizeof(struct fc_rdp_oed_sfp_desc);
6485 }
6486 
6487 static uint32_t
6488 lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
6489 			      struct fc_rdp_oed_sfp_desc *desc,
6490 			      uint8_t *page_a2)
6491 {
6492 	uint32_t flags = 0;
6493 
6494 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6495 
6496 	desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
6497 	desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
6498 	desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
6499 	desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
6500 
6501 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6502 		flags |= RDP_OET_HIGH_ALARM;
6503 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6504 		flags |= RDP_OET_LOW_ALARM;
6505 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6506 		flags |= RDP_OET_HIGH_WARNING;
6507 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6508 		flags |= RDP_OET_LOW_WARNING;
6509 
6510 	flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
6511 	desc->oed_info.function_flags = cpu_to_be32(flags);
6512 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6513 	return sizeof(struct fc_rdp_oed_sfp_desc);
6514 }
6515 
6516 static uint32_t
6517 lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
6518 			     struct fc_rdp_oed_sfp_desc *desc,
6519 			     uint8_t *page_a2)
6520 {
6521 	uint32_t flags = 0;
6522 
6523 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6524 
6525 	desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
6526 	desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
6527 	desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
6528 	desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
6529 
6530 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6531 		flags |= RDP_OET_HIGH_ALARM;
6532 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
6533 		flags |= RDP_OET_LOW_ALARM;
6534 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6535 		flags |= RDP_OET_HIGH_WARNING;
6536 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
6537 		flags |= RDP_OET_LOW_WARNING;
6538 
6539 	flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
6540 	desc->oed_info.function_flags = cpu_to_be32(flags);
6541 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6542 	return sizeof(struct fc_rdp_oed_sfp_desc);
6543 }
6544 
6545 static uint32_t
6546 lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
6547 			      struct fc_rdp_oed_sfp_desc *desc,
6548 			      uint8_t *page_a2)
6549 {
6550 	uint32_t flags = 0;
6551 
6552 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6553 
6554 	desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
6555 	desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
6556 	desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
6557 	desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
6558 
6559 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6560 		flags |= RDP_OET_HIGH_ALARM;
6561 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
6562 		flags |= RDP_OET_LOW_ALARM;
6563 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6564 		flags |= RDP_OET_HIGH_WARNING;
6565 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
6566 		flags |= RDP_OET_LOW_WARNING;
6567 
6568 	flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
6569 	desc->oed_info.function_flags = cpu_to_be32(flags);
6570 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6571 	return sizeof(struct fc_rdp_oed_sfp_desc);
6572 }
6573 
6574 
6575 static uint32_t
6576 lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
6577 			      struct fc_rdp_oed_sfp_desc *desc,
6578 			      uint8_t *page_a2)
6579 {
6580 	uint32_t flags = 0;
6581 
6582 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6583 
6584 	desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
6585 	desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
6586 	desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
6587 	desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
6588 
6589 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6590 		flags |= RDP_OET_HIGH_ALARM;
6591 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
6592 		flags |= RDP_OET_LOW_ALARM;
6593 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6594 		flags |= RDP_OET_HIGH_WARNING;
6595 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
6596 		flags |= RDP_OET_LOW_WARNING;
6597 
6598 	flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
6599 	desc->oed_info.function_flags = cpu_to_be32(flags);
6600 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6601 	return sizeof(struct fc_rdp_oed_sfp_desc);
6602 }
6603 
6604 static uint32_t
6605 lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
6606 		      uint8_t *page_a0, struct lpfc_vport *vport)
6607 {
6608 	desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
6609 	memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
6610 	memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
6611 	memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
6612 	memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
6613 	memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
6614 	desc->length = cpu_to_be32(sizeof(desc->opd_info));
6615 	return sizeof(struct fc_rdp_opd_sfp_desc);
6616 }
6617 
6618 static uint32_t
6619 lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
6620 {
6621 	if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
6622 		return 0;
6623 	desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
6624 
6625 	desc->info.CorrectedBlocks =
6626 		cpu_to_be32(stat->fecCorrBlkCount);
6627 	desc->info.UncorrectableBlocks =
6628 		cpu_to_be32(stat->fecUncorrBlkCount);
6629 
6630 	desc->length = cpu_to_be32(sizeof(desc->info));
6631 
6632 	return sizeof(struct fc_fec_rdp_desc);
6633 }
6634 
6635 static uint32_t
6636 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
6637 {
6638 	uint16_t rdp_cap = 0;
6639 	uint16_t rdp_speed;
6640 
6641 	desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
6642 
6643 	switch (phba->fc_linkspeed) {
6644 	case LPFC_LINK_SPEED_1GHZ:
6645 		rdp_speed = RDP_PS_1GB;
6646 		break;
6647 	case LPFC_LINK_SPEED_2GHZ:
6648 		rdp_speed = RDP_PS_2GB;
6649 		break;
6650 	case LPFC_LINK_SPEED_4GHZ:
6651 		rdp_speed = RDP_PS_4GB;
6652 		break;
6653 	case LPFC_LINK_SPEED_8GHZ:
6654 		rdp_speed = RDP_PS_8GB;
6655 		break;
6656 	case LPFC_LINK_SPEED_10GHZ:
6657 		rdp_speed = RDP_PS_10GB;
6658 		break;
6659 	case LPFC_LINK_SPEED_16GHZ:
6660 		rdp_speed = RDP_PS_16GB;
6661 		break;
6662 	case LPFC_LINK_SPEED_32GHZ:
6663 		rdp_speed = RDP_PS_32GB;
6664 		break;
6665 	case LPFC_LINK_SPEED_64GHZ:
6666 		rdp_speed = RDP_PS_64GB;
6667 		break;
6668 	case LPFC_LINK_SPEED_128GHZ:
6669 		rdp_speed = RDP_PS_128GB;
6670 		break;
6671 	case LPFC_LINK_SPEED_256GHZ:
6672 		rdp_speed = RDP_PS_256GB;
6673 		break;
6674 	default:
6675 		rdp_speed = RDP_PS_UNKNOWN;
6676 		break;
6677 	}
6678 
6679 	desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
6680 
6681 	if (phba->lmt & LMT_256Gb)
6682 		rdp_cap |= RDP_PS_256GB;
6683 	if (phba->lmt & LMT_128Gb)
6684 		rdp_cap |= RDP_PS_128GB;
6685 	if (phba->lmt & LMT_64Gb)
6686 		rdp_cap |= RDP_PS_64GB;
6687 	if (phba->lmt & LMT_32Gb)
6688 		rdp_cap |= RDP_PS_32GB;
6689 	if (phba->lmt & LMT_16Gb)
6690 		rdp_cap |= RDP_PS_16GB;
6691 	if (phba->lmt & LMT_10Gb)
6692 		rdp_cap |= RDP_PS_10GB;
6693 	if (phba->lmt & LMT_8Gb)
6694 		rdp_cap |= RDP_PS_8GB;
6695 	if (phba->lmt & LMT_4Gb)
6696 		rdp_cap |= RDP_PS_4GB;
6697 	if (phba->lmt & LMT_2Gb)
6698 		rdp_cap |= RDP_PS_2GB;
6699 	if (phba->lmt & LMT_1Gb)
6700 		rdp_cap |= RDP_PS_1GB;
6701 
6702 	if (rdp_cap == 0)
6703 		rdp_cap = RDP_CAP_UNKNOWN;
6704 	if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
6705 		rdp_cap |= RDP_CAP_USER_CONFIGURED;
6706 
6707 	desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
6708 	desc->length = cpu_to_be32(sizeof(desc->info));
6709 	return sizeof(struct fc_rdp_port_speed_desc);
6710 }
6711 
6712 static uint32_t
6713 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
6714 		struct lpfc_vport *vport)
6715 {
6716 
6717 	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
6718 
6719 	memcpy(desc->port_names.wwnn, &vport->fc_nodename,
6720 			sizeof(desc->port_names.wwnn));
6721 
6722 	memcpy(desc->port_names.wwpn, &vport->fc_portname,
6723 			sizeof(desc->port_names.wwpn));
6724 
6725 	desc->length = cpu_to_be32(sizeof(desc->port_names));
6726 	return sizeof(struct fc_rdp_port_name_desc);
6727 }
6728 
6729 static uint32_t
6730 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
6731 		struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
6732 {
6733 
6734 	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
6735 	if (vport->fc_flag & FC_FABRIC) {
6736 		memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
6737 		       sizeof(desc->port_names.wwnn));
6738 
6739 		memcpy(desc->port_names.wwpn, &vport->fabric_portname,
6740 		       sizeof(desc->port_names.wwpn));
6741 	} else {  /* Point to Point */
6742 		memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
6743 		       sizeof(desc->port_names.wwnn));
6744 
6745 		memcpy(desc->port_names.wwpn, &ndlp->nlp_portname,
6746 		       sizeof(desc->port_names.wwpn));
6747 	}
6748 
6749 	desc->length = cpu_to_be32(sizeof(desc->port_names));
6750 	return sizeof(struct fc_rdp_port_name_desc);
6751 }
6752 
6753 static void
6754 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
6755 		int status)
6756 {
6757 	struct lpfc_nodelist *ndlp = rdp_context->ndlp;
6758 	struct lpfc_vport *vport = ndlp->vport;
6759 	struct lpfc_iocbq *elsiocb;
6760 	struct ulp_bde64 *bpl;
6761 	IOCB_t *icmd;
6762 	uint8_t *pcmd;
6763 	struct ls_rjt *stat;
6764 	struct fc_rdp_res_frame *rdp_res;
6765 	uint32_t cmdsize, len;
6766 	uint16_t *flag_ptr;
6767 	int rc;
6768 
6769 	if (status != SUCCESS)
6770 		goto error;
6771 
6772 	/* This will change once we know the true size of the RDP payload */
6773 	cmdsize = sizeof(struct fc_rdp_res_frame);
6774 
6775 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
6776 			lpfc_max_els_tries, rdp_context->ndlp,
6777 			rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
6778 	if (!elsiocb)
6779 		goto free_rdp_context;
6780 
6781 	icmd = &elsiocb->iocb;
6782 	icmd->ulpContext = rdp_context->rx_id;
6783 	icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
6784 
6785 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6786 			"2171 Xmit RDP response tag x%x xri x%x, "
6787 			"did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x",
6788 			elsiocb->iotag, elsiocb->iocb.ulpContext,
6789 			ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6790 			ndlp->nlp_rpi);
6791 	rdp_res = (struct fc_rdp_res_frame *)
6792 		(((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6793 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6794 	memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
6795 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6796 
6797 	/* Update Alarm and Warning */
6798 	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
6799 	phba->sfp_alarm |= *flag_ptr;
6800 	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
6801 	phba->sfp_warning |= *flag_ptr;
6802 
6803 	/* For RDP payload */
6804 	len = 8;
6805 	len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
6806 					 (len + pcmd), ELS_CMD_RDP);
6807 
6808 	len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
6809 			rdp_context->page_a0, rdp_context->page_a2);
6810 	len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
6811 				  phba);
6812 	len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
6813 				       (len + pcmd), &rdp_context->link_stat);
6814 	len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
6815 					     (len + pcmd), vport);
6816 	len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
6817 					(len + pcmd), vport, ndlp);
6818 	len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
6819 			&rdp_context->link_stat);
6820 	len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
6821 				     &rdp_context->link_stat, vport);
6822 	len += lpfc_rdp_res_oed_temp_desc(phba,
6823 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6824 				rdp_context->page_a2);
6825 	len += lpfc_rdp_res_oed_voltage_desc(phba,
6826 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6827 				rdp_context->page_a2);
6828 	len += lpfc_rdp_res_oed_txbias_desc(phba,
6829 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6830 				rdp_context->page_a2);
6831 	len += lpfc_rdp_res_oed_txpower_desc(phba,
6832 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6833 				rdp_context->page_a2);
6834 	len += lpfc_rdp_res_oed_rxpower_desc(phba,
6835 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6836 				rdp_context->page_a2);
6837 	len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
6838 				     rdp_context->page_a0, vport);
6839 
6840 	rdp_res->length = cpu_to_be32(len - 8);
6841 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6842 
6843 	/* Now that we know the true size of the payload, update the BPL */
6844 	bpl = (struct ulp_bde64 *)
6845 		(((struct lpfc_dmabuf *)(elsiocb->context3))->virt);
6846 	bpl->tus.f.bdeSize = len;
6847 	bpl->tus.f.bdeFlags = 0;
6848 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
6849 
6850 	phba->fc_stat.elsXmitACC++;
6851 	elsiocb->context1 = lpfc_nlp_get(ndlp);
6852 	if (!elsiocb->context1) {
6853 		lpfc_els_free_iocb(phba, elsiocb);
6854 		goto free_rdp_context;
6855 	}
6856 
6857 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6858 	if (rc == IOCB_ERROR) {
6859 		lpfc_els_free_iocb(phba, elsiocb);
6860 		lpfc_nlp_put(ndlp);
6861 	}
6862 
6863 	goto free_rdp_context;
6864 
6865 error:
6866 	cmdsize = 2 * sizeof(uint32_t);
6867 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
6868 			ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
6869 	if (!elsiocb)
6870 		goto free_rdp_context;
6871 
6872 	icmd = &elsiocb->iocb;
6873 	icmd->ulpContext = rdp_context->rx_id;
6874 	icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
6875 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6876 
6877 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
6878 	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
6879 	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6880 
6881 	phba->fc_stat.elsXmitLSRJT++;
6882 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6883 	elsiocb->context1 = lpfc_nlp_get(ndlp);
6884 	if (!elsiocb->context1) {
6885 		lpfc_els_free_iocb(phba, elsiocb);
6886 		goto free_rdp_context;
6887 	}
6888 
6889 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6890 	if (rc == IOCB_ERROR) {
6891 		lpfc_els_free_iocb(phba, elsiocb);
6892 		lpfc_nlp_put(ndlp);
6893 	}
6894 
6895 free_rdp_context:
6896 	/* This reference put is for the original unsolicited RDP. If the
6897 	 * iocb prep failed, there is no reference to remove.
6898 	 */
6899 	lpfc_nlp_put(ndlp);
6900 	kfree(rdp_context);
6901 }
6902 
6903 static int
6904 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
6905 {
6906 	LPFC_MBOXQ_t *mbox = NULL;
6907 	int rc;
6908 
6909 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6910 	if (!mbox) {
6911 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
6912 				"7105 failed to allocate mailbox memory");
6913 		return 1;
6914 	}
6915 
6916 	if (lpfc_sli4_dump_page_a0(phba, mbox))
6917 		goto prep_mbox_fail;
6918 	mbox->vport = rdp_context->ndlp->vport;
6919 	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
6920 	mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
6921 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6922 	if (rc == MBX_NOT_FINISHED)
6923 		goto issue_mbox_fail;
6924 
6925 	return 0;
6926 
6927 prep_mbox_fail:
6928 issue_mbox_fail:
6929 	mempool_free(mbox, phba->mbox_mem_pool);
6930 	return 1;
6931 }
6932 
6933 /*
6934  * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
6935  * @vport: pointer to a host virtual N_Port data structure.
6936  * @cmdiocb: pointer to lpfc command iocb data structure.
6937  * @ndlp: pointer to a node-list data structure.
6938  *
6939  * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
6940  * IOCB. First, the payload of the unsolicited RDP is checked.
6941  * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
6942  * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
6943  * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
6944  * gather all data and send RDP response.
6945  *
6946  * Return code
6947  *   0 - Sent the acc response
6948  *   1 - Sent the reject response.
6949  */
6950 static int
6951 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6952 		struct lpfc_nodelist *ndlp)
6953 {
6954 	struct lpfc_hba *phba = vport->phba;
6955 	struct lpfc_dmabuf *pcmd;
6956 	uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
6957 	struct fc_rdp_req_frame *rdp_req;
6958 	struct lpfc_rdp_context *rdp_context;
6959 	IOCB_t *cmd = NULL;
6960 	struct ls_rjt stat;
6961 
6962 	if (phba->sli_rev < LPFC_SLI_REV4 ||
6963 	    bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
6964 						LPFC_SLI_INTF_IF_TYPE_2) {
6965 		rjt_err = LSRJT_UNABLE_TPC;
6966 		rjt_expl = LSEXP_REQ_UNSUPPORTED;
6967 		goto error;
6968 	}
6969 
6970 	if (phba->sli_rev < LPFC_SLI_REV4 || (phba->hba_flag & HBA_FCOE_MODE)) {
6971 		rjt_err = LSRJT_UNABLE_TPC;
6972 		rjt_expl = LSEXP_REQ_UNSUPPORTED;
6973 		goto error;
6974 	}
6975 
6976 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6977 	rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
6978 
6979 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6980 			 "2422 ELS RDP Request "
6981 			 "dec len %d tag x%x port_id %d len %d\n",
6982 			 be32_to_cpu(rdp_req->rdp_des_length),
6983 			 be32_to_cpu(rdp_req->nport_id_desc.tag),
6984 			 be32_to_cpu(rdp_req->nport_id_desc.nport_id),
6985 			 be32_to_cpu(rdp_req->nport_id_desc.length));
6986 
6987 	if (sizeof(struct fc_rdp_nport_desc) !=
6988 			be32_to_cpu(rdp_req->rdp_des_length))
6989 		goto rjt_logerr;
6990 	if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
6991 		goto rjt_logerr;
6992 	if (RDP_NPORT_ID_SIZE !=
6993 			be32_to_cpu(rdp_req->nport_id_desc.length))
6994 		goto rjt_logerr;
6995 	rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
6996 	if (!rdp_context) {
6997 		rjt_err = LSRJT_UNABLE_TPC;
6998 		goto error;
6999 	}
7000 
7001 	cmd = &cmdiocb->iocb;
7002 	rdp_context->ndlp = lpfc_nlp_get(ndlp);
7003 	if (!rdp_context->ndlp) {
7004 		kfree(rdp_context);
7005 		rjt_err = LSRJT_UNABLE_TPC;
7006 		goto error;
7007 	}
7008 	rdp_context->ox_id = cmd->unsli3.rcvsli3.ox_id;
7009 	rdp_context->rx_id = cmd->ulpContext;
7010 	rdp_context->cmpl = lpfc_els_rdp_cmpl;
7011 	if (lpfc_get_rdp_info(phba, rdp_context)) {
7012 		lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
7013 				 "2423 Unable to send mailbox");
7014 		kfree(rdp_context);
7015 		rjt_err = LSRJT_UNABLE_TPC;
7016 		lpfc_nlp_put(ndlp);
7017 		goto error;
7018 	}
7019 
7020 	return 0;
7021 
7022 rjt_logerr:
7023 	rjt_err = LSRJT_LOGICAL_ERR;
7024 
7025 error:
7026 	memset(&stat, 0, sizeof(stat));
7027 	stat.un.b.lsRjtRsnCode = rjt_err;
7028 	stat.un.b.lsRjtRsnCodeExp = rjt_expl;
7029 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7030 	return 1;
7031 }
7032 
7033 
7034 static void
7035 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7036 {
7037 	MAILBOX_t *mb;
7038 	IOCB_t *icmd;
7039 	uint8_t *pcmd;
7040 	struct lpfc_iocbq *elsiocb;
7041 	struct lpfc_nodelist *ndlp;
7042 	struct ls_rjt *stat;
7043 	union lpfc_sli4_cfg_shdr *shdr;
7044 	struct lpfc_lcb_context *lcb_context;
7045 	struct fc_lcb_res_frame *lcb_res;
7046 	uint32_t cmdsize, shdr_status, shdr_add_status;
7047 	int rc;
7048 
7049 	mb = &pmb->u.mb;
7050 	lcb_context = (struct lpfc_lcb_context *)pmb->ctx_ndlp;
7051 	ndlp = lcb_context->ndlp;
7052 	pmb->ctx_ndlp = NULL;
7053 	pmb->ctx_buf = NULL;
7054 
7055 	shdr = (union lpfc_sli4_cfg_shdr *)
7056 			&pmb->u.mqe.un.beacon_config.header.cfg_shdr;
7057 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7058 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7059 
7060 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
7061 				"0194 SET_BEACON_CONFIG mailbox "
7062 				"completed with status x%x add_status x%x,"
7063 				" mbx status x%x\n",
7064 				shdr_status, shdr_add_status, mb->mbxStatus);
7065 
7066 	if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
7067 	    (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
7068 	    (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
7069 		mempool_free(pmb, phba->mbox_mem_pool);
7070 		goto error;
7071 	}
7072 
7073 	mempool_free(pmb, phba->mbox_mem_pool);
7074 	cmdsize = sizeof(struct fc_lcb_res_frame);
7075 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7076 			lpfc_max_els_tries, ndlp,
7077 			ndlp->nlp_DID, ELS_CMD_ACC);
7078 
7079 	/* Decrement the ndlp reference count from previous mbox command */
7080 	lpfc_nlp_put(ndlp);
7081 
7082 	if (!elsiocb)
7083 		goto free_lcb_context;
7084 
7085 	lcb_res = (struct fc_lcb_res_frame *)
7086 		(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
7087 
7088 	memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
7089 	icmd = &elsiocb->iocb;
7090 	icmd->ulpContext = lcb_context->rx_id;
7091 	icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7092 
7093 	pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
7094 	*((uint32_t *)(pcmd)) = ELS_CMD_ACC;
7095 	lcb_res->lcb_sub_command = lcb_context->sub_command;
7096 	lcb_res->lcb_type = lcb_context->type;
7097 	lcb_res->capability = lcb_context->capability;
7098 	lcb_res->lcb_frequency = lcb_context->frequency;
7099 	lcb_res->lcb_duration = lcb_context->duration;
7100 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7101 	phba->fc_stat.elsXmitACC++;
7102 
7103 	elsiocb->context1 = lpfc_nlp_get(ndlp);
7104 	if (!elsiocb->context1) {
7105 		lpfc_els_free_iocb(phba, elsiocb);
7106 		goto out;
7107 	}
7108 
7109 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7110 	if (rc == IOCB_ERROR) {
7111 		lpfc_els_free_iocb(phba, elsiocb);
7112 		lpfc_nlp_put(ndlp);
7113 	}
7114  out:
7115 	kfree(lcb_context);
7116 	return;
7117 
7118 error:
7119 	cmdsize = sizeof(struct fc_lcb_res_frame);
7120 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7121 			lpfc_max_els_tries, ndlp,
7122 			ndlp->nlp_DID, ELS_CMD_LS_RJT);
7123 	lpfc_nlp_put(ndlp);
7124 	if (!elsiocb)
7125 		goto free_lcb_context;
7126 
7127 	icmd = &elsiocb->iocb;
7128 	icmd->ulpContext = lcb_context->rx_id;
7129 	icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7130 	pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
7131 
7132 	*((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
7133 	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
7134 	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7135 
7136 	if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
7137 		stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
7138 
7139 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7140 	phba->fc_stat.elsXmitLSRJT++;
7141 	elsiocb->context1 = lpfc_nlp_get(ndlp);
7142 	if (!elsiocb->context1) {
7143 		lpfc_els_free_iocb(phba, elsiocb);
7144 		goto free_lcb_context;
7145 	}
7146 
7147 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7148 	if (rc == IOCB_ERROR) {
7149 		lpfc_els_free_iocb(phba, elsiocb);
7150 		lpfc_nlp_put(ndlp);
7151 	}
7152 free_lcb_context:
7153 	kfree(lcb_context);
7154 }
7155 
7156 static int
7157 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
7158 		     struct lpfc_lcb_context *lcb_context,
7159 		     uint32_t beacon_state)
7160 {
7161 	struct lpfc_hba *phba = vport->phba;
7162 	union lpfc_sli4_cfg_shdr *cfg_shdr;
7163 	LPFC_MBOXQ_t *mbox = NULL;
7164 	uint32_t len;
7165 	int rc;
7166 
7167 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7168 	if (!mbox)
7169 		return 1;
7170 
7171 	cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
7172 	len = sizeof(struct lpfc_mbx_set_beacon_config) -
7173 		sizeof(struct lpfc_sli4_cfg_mhdr);
7174 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7175 			 LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
7176 			 LPFC_SLI4_MBX_EMBED);
7177 	mbox->ctx_ndlp = (void *)lcb_context;
7178 	mbox->vport = phba->pport;
7179 	mbox->mbox_cmpl = lpfc_els_lcb_rsp;
7180 	bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
7181 	       phba->sli4_hba.physical_port);
7182 	bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
7183 	       beacon_state);
7184 	mbox->u.mqe.un.beacon_config.word5 = 0;		/* Reserved */
7185 
7186 	/*
7187 	 *	Check bv1s bit before issuing the mailbox
7188 	 *	if bv1s == 1, LCB V1 supported
7189 	 *	else, LCB V0 supported
7190 	 */
7191 
7192 	if (phba->sli4_hba.pc_sli4_params.bv1s) {
7193 		/* COMMON_SET_BEACON_CONFIG_V1 */
7194 		cfg_shdr->request.word9 = BEACON_VERSION_V1;
7195 		lcb_context->capability |= LCB_CAPABILITY_DURATION;
7196 		bf_set(lpfc_mbx_set_beacon_port_type,
7197 		       &mbox->u.mqe.un.beacon_config, 0);
7198 		bf_set(lpfc_mbx_set_beacon_duration_v1,
7199 		       &mbox->u.mqe.un.beacon_config,
7200 		       be16_to_cpu(lcb_context->duration));
7201 	} else {
7202 		/* COMMON_SET_BEACON_CONFIG_V0 */
7203 		if (be16_to_cpu(lcb_context->duration) != 0) {
7204 			mempool_free(mbox, phba->mbox_mem_pool);
7205 			return 1;
7206 		}
7207 		cfg_shdr->request.word9 = BEACON_VERSION_V0;
7208 		lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
7209 		bf_set(lpfc_mbx_set_beacon_state,
7210 		       &mbox->u.mqe.un.beacon_config, beacon_state);
7211 		bf_set(lpfc_mbx_set_beacon_port_type,
7212 		       &mbox->u.mqe.un.beacon_config, 1);
7213 		bf_set(lpfc_mbx_set_beacon_duration,
7214 		       &mbox->u.mqe.un.beacon_config,
7215 		       be16_to_cpu(lcb_context->duration));
7216 	}
7217 
7218 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7219 	if (rc == MBX_NOT_FINISHED) {
7220 		mempool_free(mbox, phba->mbox_mem_pool);
7221 		return 1;
7222 	}
7223 
7224 	return 0;
7225 }
7226 
7227 
7228 /**
7229  * lpfc_els_rcv_lcb - Process an unsolicited LCB
7230  * @vport: pointer to a host virtual N_Port data structure.
7231  * @cmdiocb: pointer to lpfc command iocb data structure.
7232  * @ndlp: pointer to a node-list data structure.
7233  *
7234  * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
7235  * First, the payload of the unsolicited LCB is checked.
7236  * Then based on Subcommand beacon will either turn on or off.
7237  *
7238  * Return code
7239  * 0 - Sent the acc response
7240  * 1 - Sent the reject response.
7241  **/
7242 static int
7243 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7244 		 struct lpfc_nodelist *ndlp)
7245 {
7246 	struct lpfc_hba *phba = vport->phba;
7247 	struct lpfc_dmabuf *pcmd;
7248 	uint8_t *lp;
7249 	struct fc_lcb_request_frame *beacon;
7250 	struct lpfc_lcb_context *lcb_context;
7251 	u8 state, rjt_err = 0;
7252 	struct ls_rjt stat;
7253 
7254 	pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
7255 	lp = (uint8_t *)pcmd->virt;
7256 	beacon = (struct fc_lcb_request_frame *)pcmd->virt;
7257 
7258 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7259 			"0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
7260 			"type x%x frequency %x duration x%x\n",
7261 			lp[0], lp[1], lp[2],
7262 			beacon->lcb_command,
7263 			beacon->lcb_sub_command,
7264 			beacon->lcb_type,
7265 			beacon->lcb_frequency,
7266 			be16_to_cpu(beacon->lcb_duration));
7267 
7268 	if (beacon->lcb_sub_command != LPFC_LCB_ON &&
7269 	    beacon->lcb_sub_command != LPFC_LCB_OFF) {
7270 		rjt_err = LSRJT_CMD_UNSUPPORTED;
7271 		goto rjt;
7272 	}
7273 
7274 	if (phba->sli_rev < LPFC_SLI_REV4  ||
7275 	    phba->hba_flag & HBA_FCOE_MODE ||
7276 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7277 	    LPFC_SLI_INTF_IF_TYPE_2)) {
7278 		rjt_err = LSRJT_CMD_UNSUPPORTED;
7279 		goto rjt;
7280 	}
7281 
7282 	lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
7283 	if (!lcb_context) {
7284 		rjt_err = LSRJT_UNABLE_TPC;
7285 		goto rjt;
7286 	}
7287 
7288 	state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
7289 	lcb_context->sub_command = beacon->lcb_sub_command;
7290 	lcb_context->capability	= 0;
7291 	lcb_context->type = beacon->lcb_type;
7292 	lcb_context->frequency = beacon->lcb_frequency;
7293 	lcb_context->duration = beacon->lcb_duration;
7294 	lcb_context->ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
7295 	lcb_context->rx_id = cmdiocb->iocb.ulpContext;
7296 	lcb_context->ndlp = lpfc_nlp_get(ndlp);
7297 	if (!lcb_context->ndlp) {
7298 		rjt_err = LSRJT_UNABLE_TPC;
7299 		goto rjt_free;
7300 	}
7301 
7302 	if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
7303 		lpfc_printf_vlog(ndlp->vport, KERN_ERR, LOG_TRACE_EVENT,
7304 				 "0193 failed to send mail box");
7305 		lpfc_nlp_put(ndlp);
7306 		rjt_err = LSRJT_UNABLE_TPC;
7307 		goto rjt_free;
7308 	}
7309 	return 0;
7310 
7311 rjt_free:
7312 	kfree(lcb_context);
7313 rjt:
7314 	memset(&stat, 0, sizeof(stat));
7315 	stat.un.b.lsRjtRsnCode = rjt_err;
7316 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7317 	return 1;
7318 }
7319 
7320 
7321 /**
7322  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
7323  * @vport: pointer to a host virtual N_Port data structure.
7324  *
7325  * This routine cleans up any Registration State Change Notification
7326  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
7327  * @vport together with the host_lock is used to prevent multiple thread
7328  * trying to access the RSCN array on a same @vport at the same time.
7329  **/
7330 void
7331 lpfc_els_flush_rscn(struct lpfc_vport *vport)
7332 {
7333 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7334 	struct lpfc_hba  *phba = vport->phba;
7335 	int i;
7336 
7337 	spin_lock_irq(shost->host_lock);
7338 	if (vport->fc_rscn_flush) {
7339 		/* Another thread is walking fc_rscn_id_list on this vport */
7340 		spin_unlock_irq(shost->host_lock);
7341 		return;
7342 	}
7343 	/* Indicate we are walking lpfc_els_flush_rscn on this vport */
7344 	vport->fc_rscn_flush = 1;
7345 	spin_unlock_irq(shost->host_lock);
7346 
7347 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7348 		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
7349 		vport->fc_rscn_id_list[i] = NULL;
7350 	}
7351 	spin_lock_irq(shost->host_lock);
7352 	vport->fc_rscn_id_cnt = 0;
7353 	vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
7354 	spin_unlock_irq(shost->host_lock);
7355 	lpfc_can_disctmo(vport);
7356 	/* Indicate we are done walking this fc_rscn_id_list */
7357 	vport->fc_rscn_flush = 0;
7358 }
7359 
7360 /**
7361  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
7362  * @vport: pointer to a host virtual N_Port data structure.
7363  * @did: remote destination port identifier.
7364  *
7365  * This routine checks whether there is any pending Registration State
7366  * Configuration Notification (RSCN) to a @did on @vport.
7367  *
7368  * Return code
7369  *   None zero - The @did matched with a pending rscn
7370  *   0 - not able to match @did with a pending rscn
7371  **/
7372 int
7373 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
7374 {
7375 	D_ID ns_did;
7376 	D_ID rscn_did;
7377 	uint32_t *lp;
7378 	uint32_t payload_len, i;
7379 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7380 
7381 	ns_did.un.word = did;
7382 
7383 	/* Never match fabric nodes for RSCNs */
7384 	if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7385 		return 0;
7386 
7387 	/* If we are doing a FULL RSCN rediscovery, match everything */
7388 	if (vport->fc_flag & FC_RSCN_DISCOVERY)
7389 		return did;
7390 
7391 	spin_lock_irq(shost->host_lock);
7392 	if (vport->fc_rscn_flush) {
7393 		/* Another thread is walking fc_rscn_id_list on this vport */
7394 		spin_unlock_irq(shost->host_lock);
7395 		return 0;
7396 	}
7397 	/* Indicate we are walking fc_rscn_id_list on this vport */
7398 	vport->fc_rscn_flush = 1;
7399 	spin_unlock_irq(shost->host_lock);
7400 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7401 		lp = vport->fc_rscn_id_list[i]->virt;
7402 		payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
7403 		payload_len -= sizeof(uint32_t);	/* take off word 0 */
7404 		while (payload_len) {
7405 			rscn_did.un.word = be32_to_cpu(*lp++);
7406 			payload_len -= sizeof(uint32_t);
7407 			switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
7408 			case RSCN_ADDRESS_FORMAT_PORT:
7409 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7410 				    && (ns_did.un.b.area == rscn_did.un.b.area)
7411 				    && (ns_did.un.b.id == rscn_did.un.b.id))
7412 					goto return_did_out;
7413 				break;
7414 			case RSCN_ADDRESS_FORMAT_AREA:
7415 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7416 				    && (ns_did.un.b.area == rscn_did.un.b.area))
7417 					goto return_did_out;
7418 				break;
7419 			case RSCN_ADDRESS_FORMAT_DOMAIN:
7420 				if (ns_did.un.b.domain == rscn_did.un.b.domain)
7421 					goto return_did_out;
7422 				break;
7423 			case RSCN_ADDRESS_FORMAT_FABRIC:
7424 				goto return_did_out;
7425 			}
7426 		}
7427 	}
7428 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
7429 	vport->fc_rscn_flush = 0;
7430 	return 0;
7431 return_did_out:
7432 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
7433 	vport->fc_rscn_flush = 0;
7434 	return did;
7435 }
7436 
7437 /**
7438  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
7439  * @vport: pointer to a host virtual N_Port data structure.
7440  *
7441  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
7442  * state machine for a @vport's nodes that are with pending RSCN (Registration
7443  * State Change Notification).
7444  *
7445  * Return code
7446  *   0 - Successful (currently alway return 0)
7447  **/
7448 static int
7449 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
7450 {
7451 	struct lpfc_nodelist *ndlp = NULL;
7452 
7453 	/* Move all affected nodes by pending RSCNs to NPR state. */
7454 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
7455 		if ((ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
7456 		    !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
7457 			continue;
7458 
7459 		/* NVME Target mode does not do RSCN Recovery. */
7460 		if (vport->phba->nvmet_support)
7461 			continue;
7462 
7463 		/* If we are in the process of doing discovery on this
7464 		 * NPort, let it continue on its own.
7465 		 */
7466 		switch (ndlp->nlp_state) {
7467 		case  NLP_STE_PLOGI_ISSUE:
7468 		case  NLP_STE_ADISC_ISSUE:
7469 		case  NLP_STE_REG_LOGIN_ISSUE:
7470 		case  NLP_STE_PRLI_ISSUE:
7471 		case  NLP_STE_LOGO_ISSUE:
7472 			continue;
7473 		}
7474 
7475 		lpfc_disc_state_machine(vport, ndlp, NULL,
7476 					NLP_EVT_DEVICE_RECOVERY);
7477 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
7478 	}
7479 	return 0;
7480 }
7481 
7482 /**
7483  * lpfc_send_rscn_event - Send an RSCN event to management application
7484  * @vport: pointer to a host virtual N_Port data structure.
7485  * @cmdiocb: pointer to lpfc command iocb data structure.
7486  *
7487  * lpfc_send_rscn_event sends an RSCN netlink event to management
7488  * applications.
7489  */
7490 static void
7491 lpfc_send_rscn_event(struct lpfc_vport *vport,
7492 		struct lpfc_iocbq *cmdiocb)
7493 {
7494 	struct lpfc_dmabuf *pcmd;
7495 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7496 	uint32_t *payload_ptr;
7497 	uint32_t payload_len;
7498 	struct lpfc_rscn_event_header *rscn_event_data;
7499 
7500 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7501 	payload_ptr = (uint32_t *) pcmd->virt;
7502 	payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
7503 
7504 	rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
7505 		payload_len, GFP_KERNEL);
7506 	if (!rscn_event_data) {
7507 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
7508 			"0147 Failed to allocate memory for RSCN event\n");
7509 		return;
7510 	}
7511 	rscn_event_data->event_type = FC_REG_RSCN_EVENT;
7512 	rscn_event_data->payload_length = payload_len;
7513 	memcpy(rscn_event_data->rscn_payload, payload_ptr,
7514 		payload_len);
7515 
7516 	fc_host_post_vendor_event(shost,
7517 		fc_get_event_number(),
7518 		sizeof(struct lpfc_rscn_event_header) + payload_len,
7519 		(char *)rscn_event_data,
7520 		LPFC_NL_VENDOR_ID);
7521 
7522 	kfree(rscn_event_data);
7523 }
7524 
7525 /**
7526  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
7527  * @vport: pointer to a host virtual N_Port data structure.
7528  * @cmdiocb: pointer to lpfc command iocb data structure.
7529  * @ndlp: pointer to a node-list data structure.
7530  *
7531  * This routine processes an unsolicited RSCN (Registration State Change
7532  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
7533  * to invoke fc_host_post_event() routine to the FC transport layer. If the
7534  * discover state machine is about to begin discovery, it just accepts the
7535  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
7536  * contains N_Port IDs for other vports on this HBA, it just accepts the
7537  * RSCN and ignore processing it. If the state machine is in the recovery
7538  * state, the fc_rscn_id_list of this @vport is walked and the
7539  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
7540  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
7541  * routine is invoked to handle the RSCN event.
7542  *
7543  * Return code
7544  *   0 - Just sent the acc response
7545  *   1 - Sent the acc response and waited for name server completion
7546  **/
7547 static int
7548 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7549 		  struct lpfc_nodelist *ndlp)
7550 {
7551 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7552 	struct lpfc_hba  *phba = vport->phba;
7553 	struct lpfc_dmabuf *pcmd;
7554 	uint32_t *lp, *datap;
7555 	uint32_t payload_len, length, nportid, *cmd;
7556 	int rscn_cnt;
7557 	int rscn_id = 0, hba_id = 0;
7558 	int i, tmo;
7559 
7560 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7561 	lp = (uint32_t *) pcmd->virt;
7562 
7563 	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
7564 	payload_len -= sizeof(uint32_t);	/* take off word 0 */
7565 	/* RSCN received */
7566 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7567 			 "0214 RSCN received Data: x%x x%x x%x x%x\n",
7568 			 vport->fc_flag, payload_len, *lp,
7569 			 vport->fc_rscn_id_cnt);
7570 
7571 	/* Send an RSCN event to the management application */
7572 	lpfc_send_rscn_event(vport, cmdiocb);
7573 
7574 	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
7575 		fc_host_post_event(shost, fc_get_event_number(),
7576 			FCH_EVT_RSCN, lp[i]);
7577 
7578 	/* Check if RSCN is coming from a direct-connected remote NPort */
7579 	if (vport->fc_flag & FC_PT2PT) {
7580 		/* If so, just ACC it, no other action needed for now */
7581 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7582 				 "2024 pt2pt RSCN %08x Data: x%x x%x\n",
7583 				 *lp, vport->fc_flag, payload_len);
7584 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7585 
7586 		/* Check to see if we need to NVME rescan this target
7587 		 * remoteport.
7588 		 */
7589 		if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
7590 		    ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
7591 			lpfc_nvme_rescan_port(vport, ndlp);
7592 		return 0;
7593 	}
7594 
7595 	/* If we are about to begin discovery, just ACC the RSCN.
7596 	 * Discovery processing will satisfy it.
7597 	 */
7598 	if (vport->port_state <= LPFC_NS_QRY) {
7599 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7600 			"RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
7601 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
7602 
7603 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7604 		return 0;
7605 	}
7606 
7607 	/* If this RSCN just contains NPortIDs for other vports on this HBA,
7608 	 * just ACC and ignore it.
7609 	 */
7610 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
7611 		!(vport->cfg_peer_port_login)) {
7612 		i = payload_len;
7613 		datap = lp;
7614 		while (i > 0) {
7615 			nportid = *datap++;
7616 			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
7617 			i -= sizeof(uint32_t);
7618 			rscn_id++;
7619 			if (lpfc_find_vport_by_did(phba, nportid))
7620 				hba_id++;
7621 		}
7622 		if (rscn_id == hba_id) {
7623 			/* ALL NPortIDs in RSCN are on HBA */
7624 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7625 					 "0219 Ignore RSCN "
7626 					 "Data: x%x x%x x%x x%x\n",
7627 					 vport->fc_flag, payload_len,
7628 					 *lp, vport->fc_rscn_id_cnt);
7629 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7630 				"RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
7631 				ndlp->nlp_DID, vport->port_state,
7632 				ndlp->nlp_flag);
7633 
7634 			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
7635 				ndlp, NULL);
7636 			return 0;
7637 		}
7638 	}
7639 
7640 	spin_lock_irq(shost->host_lock);
7641 	if (vport->fc_rscn_flush) {
7642 		/* Another thread is walking fc_rscn_id_list on this vport */
7643 		vport->fc_flag |= FC_RSCN_DISCOVERY;
7644 		spin_unlock_irq(shost->host_lock);
7645 		/* Send back ACC */
7646 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7647 		return 0;
7648 	}
7649 	/* Indicate we are walking fc_rscn_id_list on this vport */
7650 	vport->fc_rscn_flush = 1;
7651 	spin_unlock_irq(shost->host_lock);
7652 	/* Get the array count after successfully have the token */
7653 	rscn_cnt = vport->fc_rscn_id_cnt;
7654 	/* If we are already processing an RSCN, save the received
7655 	 * RSCN payload buffer, cmdiocb->context2 to process later.
7656 	 */
7657 	if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
7658 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7659 			"RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
7660 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
7661 
7662 		spin_lock_irq(shost->host_lock);
7663 		vport->fc_flag |= FC_RSCN_DEFERRED;
7664 
7665 		/* Restart disctmo if its already running */
7666 		if (vport->fc_flag & FC_DISC_TMO) {
7667 			tmo = ((phba->fc_ratov * 3) + 3);
7668 			mod_timer(&vport->fc_disctmo,
7669 				  jiffies + msecs_to_jiffies(1000 * tmo));
7670 		}
7671 		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
7672 		    !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
7673 			vport->fc_flag |= FC_RSCN_MODE;
7674 			spin_unlock_irq(shost->host_lock);
7675 			if (rscn_cnt) {
7676 				cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
7677 				length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
7678 			}
7679 			if ((rscn_cnt) &&
7680 			    (payload_len + length <= LPFC_BPL_SIZE)) {
7681 				*cmd &= ELS_CMD_MASK;
7682 				*cmd |= cpu_to_be32(payload_len + length);
7683 				memcpy(((uint8_t *)cmd) + length, lp,
7684 				       payload_len);
7685 			} else {
7686 				vport->fc_rscn_id_list[rscn_cnt] = pcmd;
7687 				vport->fc_rscn_id_cnt++;
7688 				/* If we zero, cmdiocb->context2, the calling
7689 				 * routine will not try to free it.
7690 				 */
7691 				cmdiocb->context2 = NULL;
7692 			}
7693 			/* Deferred RSCN */
7694 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7695 					 "0235 Deferred RSCN "
7696 					 "Data: x%x x%x x%x\n",
7697 					 vport->fc_rscn_id_cnt, vport->fc_flag,
7698 					 vport->port_state);
7699 		} else {
7700 			vport->fc_flag |= FC_RSCN_DISCOVERY;
7701 			spin_unlock_irq(shost->host_lock);
7702 			/* ReDiscovery RSCN */
7703 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7704 					 "0234 ReDiscovery RSCN "
7705 					 "Data: x%x x%x x%x\n",
7706 					 vport->fc_rscn_id_cnt, vport->fc_flag,
7707 					 vport->port_state);
7708 		}
7709 		/* Indicate we are done walking fc_rscn_id_list on this vport */
7710 		vport->fc_rscn_flush = 0;
7711 		/* Send back ACC */
7712 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7713 		/* send RECOVERY event for ALL nodes that match RSCN payload */
7714 		lpfc_rscn_recovery_check(vport);
7715 		return 0;
7716 	}
7717 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7718 		"RCV RSCN:        did:x%x/ste:x%x flg:x%x",
7719 		ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
7720 
7721 	spin_lock_irq(shost->host_lock);
7722 	vport->fc_flag |= FC_RSCN_MODE;
7723 	spin_unlock_irq(shost->host_lock);
7724 	vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
7725 	/* Indicate we are done walking fc_rscn_id_list on this vport */
7726 	vport->fc_rscn_flush = 0;
7727 	/*
7728 	 * If we zero, cmdiocb->context2, the calling routine will
7729 	 * not try to free it.
7730 	 */
7731 	cmdiocb->context2 = NULL;
7732 	lpfc_set_disctmo(vport);
7733 	/* Send back ACC */
7734 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7735 	/* send RECOVERY event for ALL nodes that match RSCN payload */
7736 	lpfc_rscn_recovery_check(vport);
7737 	return lpfc_els_handle_rscn(vport);
7738 }
7739 
7740 /**
7741  * lpfc_els_handle_rscn - Handle rscn for a vport
7742  * @vport: pointer to a host virtual N_Port data structure.
7743  *
7744  * This routine handles the Registration State Configuration Notification
7745  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
7746  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
7747  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
7748  * NameServer shall be issued. If CT command to the NameServer fails to be
7749  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
7750  * RSCN activities with the @vport.
7751  *
7752  * Return code
7753  *   0 - Cleaned up rscn on the @vport
7754  *   1 - Wait for plogi to name server before proceed
7755  **/
7756 int
7757 lpfc_els_handle_rscn(struct lpfc_vport *vport)
7758 {
7759 	struct lpfc_nodelist *ndlp;
7760 	struct lpfc_hba  *phba = vport->phba;
7761 
7762 	/* Ignore RSCN if the port is being torn down. */
7763 	if (vport->load_flag & FC_UNLOADING) {
7764 		lpfc_els_flush_rscn(vport);
7765 		return 0;
7766 	}
7767 
7768 	/* Start timer for RSCN processing */
7769 	lpfc_set_disctmo(vport);
7770 
7771 	/* RSCN processed */
7772 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7773 			 "0215 RSCN processed Data: x%x x%x x%x x%x x%x x%x\n",
7774 			 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
7775 			 vport->port_state, vport->num_disc_nodes,
7776 			 vport->gidft_inp);
7777 
7778 	/* To process RSCN, first compare RSCN data with NameServer */
7779 	vport->fc_ns_retry = 0;
7780 	vport->num_disc_nodes = 0;
7781 
7782 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
7783 	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
7784 		/* Good ndlp, issue CT Request to NameServer.  Need to
7785 		 * know how many gidfts were issued.  If none, then just
7786 		 * flush the RSCN.  Otherwise, the outstanding requests
7787 		 * need to complete.
7788 		 */
7789 		if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
7790 			if (lpfc_issue_gidft(vport) > 0)
7791 				return 1;
7792 		} else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
7793 			if (lpfc_issue_gidpt(vport) > 0)
7794 				return 1;
7795 		} else {
7796 			return 1;
7797 		}
7798 	} else {
7799 		/* Nameserver login in question.  Revalidate. */
7800 		if (ndlp) {
7801 			ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
7802 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
7803 		} else {
7804 			ndlp = lpfc_nlp_init(vport, NameServer_DID);
7805 			if (!ndlp) {
7806 				lpfc_els_flush_rscn(vport);
7807 				return 0;
7808 			}
7809 			ndlp->nlp_prev_state = ndlp->nlp_state;
7810 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
7811 		}
7812 		ndlp->nlp_type |= NLP_FABRIC;
7813 		lpfc_issue_els_plogi(vport, NameServer_DID, 0);
7814 		/* Wait for NameServer login cmpl before we can
7815 		 * continue
7816 		 */
7817 		return 1;
7818 	}
7819 
7820 	lpfc_els_flush_rscn(vport);
7821 	return 0;
7822 }
7823 
7824 /**
7825  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
7826  * @vport: pointer to a host virtual N_Port data structure.
7827  * @cmdiocb: pointer to lpfc command iocb data structure.
7828  * @ndlp: pointer to a node-list data structure.
7829  *
7830  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
7831  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
7832  * point topology. As an unsolicited FLOGI should not be received in a loop
7833  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
7834  * lpfc_check_sparm() routine is invoked to check the parameters in the
7835  * unsolicited FLOGI. If parameters validation failed, the routine
7836  * lpfc_els_rsp_reject() shall be called with reject reason code set to
7837  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
7838  * FLOGI shall be compared with the Port WWN of the @vport to determine who
7839  * will initiate PLOGI. The higher lexicographical value party shall has
7840  * higher priority (as the winning port) and will initiate PLOGI and
7841  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
7842  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
7843  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
7844  *
7845  * Return code
7846  *   0 - Successfully processed the unsolicited flogi
7847  *   1 - Failed to process the unsolicited flogi
7848  **/
7849 static int
7850 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7851 		   struct lpfc_nodelist *ndlp)
7852 {
7853 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7854 	struct lpfc_hba  *phba = vport->phba;
7855 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7856 	uint32_t *lp = (uint32_t *) pcmd->virt;
7857 	IOCB_t *icmd = &cmdiocb->iocb;
7858 	struct serv_parm *sp;
7859 	LPFC_MBOXQ_t *mbox;
7860 	uint32_t cmd, did;
7861 	int rc;
7862 	uint32_t fc_flag = 0;
7863 	uint32_t port_state = 0;
7864 
7865 	cmd = *lp++;
7866 	sp = (struct serv_parm *) lp;
7867 
7868 	/* FLOGI received */
7869 
7870 	lpfc_set_disctmo(vport);
7871 
7872 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7873 		/* We should never receive a FLOGI in loop mode, ignore it */
7874 		did = icmd->un.elsreq64.remoteID;
7875 
7876 		/* An FLOGI ELS command <elsCmd> was received from DID <did> in
7877 		   Loop Mode */
7878 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
7879 				 "0113 An FLOGI ELS command x%x was "
7880 				 "received from DID x%x in Loop Mode\n",
7881 				 cmd, did);
7882 		return 1;
7883 	}
7884 
7885 	(void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
7886 
7887 	/*
7888 	 * If our portname is greater than the remote portname,
7889 	 * then we initiate Nport login.
7890 	 */
7891 
7892 	rc = memcmp(&vport->fc_portname, &sp->portName,
7893 		    sizeof(struct lpfc_name));
7894 
7895 	if (!rc) {
7896 		if (phba->sli_rev < LPFC_SLI_REV4) {
7897 			mbox = mempool_alloc(phba->mbox_mem_pool,
7898 					     GFP_KERNEL);
7899 			if (!mbox)
7900 				return 1;
7901 			lpfc_linkdown(phba);
7902 			lpfc_init_link(phba, mbox,
7903 				       phba->cfg_topology,
7904 				       phba->cfg_link_speed);
7905 			mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
7906 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7907 			mbox->vport = vport;
7908 			rc = lpfc_sli_issue_mbox(phba, mbox,
7909 						 MBX_NOWAIT);
7910 			lpfc_set_loopback_flag(phba);
7911 			if (rc == MBX_NOT_FINISHED)
7912 				mempool_free(mbox, phba->mbox_mem_pool);
7913 			return 1;
7914 		}
7915 
7916 		/* abort the flogi coming back to ourselves
7917 		 * due to external loopback on the port.
7918 		 */
7919 		lpfc_els_abort_flogi(phba);
7920 		return 0;
7921 
7922 	} else if (rc > 0) {	/* greater than */
7923 		spin_lock_irq(shost->host_lock);
7924 		vport->fc_flag |= FC_PT2PT_PLOGI;
7925 		spin_unlock_irq(shost->host_lock);
7926 
7927 		/* If we have the high WWPN we can assign our own
7928 		 * myDID; otherwise, we have to WAIT for a PLOGI
7929 		 * from the remote NPort to find out what it
7930 		 * will be.
7931 		 */
7932 		vport->fc_myDID = PT2PT_LocalID;
7933 	} else {
7934 		vport->fc_myDID = PT2PT_RemoteID;
7935 	}
7936 
7937 	/*
7938 	 * The vport state should go to LPFC_FLOGI only
7939 	 * AFTER we issue a FLOGI, not receive one.
7940 	 */
7941 	spin_lock_irq(shost->host_lock);
7942 	fc_flag = vport->fc_flag;
7943 	port_state = vport->port_state;
7944 	vport->fc_flag |= FC_PT2PT;
7945 	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
7946 
7947 	/* Acking an unsol FLOGI.  Count 1 for link bounce
7948 	 * work-around.
7949 	 */
7950 	vport->rcv_flogi_cnt++;
7951 	spin_unlock_irq(shost->host_lock);
7952 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7953 			 "3311 Rcv Flogi PS x%x new PS x%x "
7954 			 "fc_flag x%x new fc_flag x%x\n",
7955 			 port_state, vport->port_state,
7956 			 fc_flag, vport->fc_flag);
7957 
7958 	/*
7959 	 * We temporarily set fc_myDID to make it look like we are
7960 	 * a Fabric. This is done just so we end up with the right
7961 	 * did / sid on the FLOGI ACC rsp.
7962 	 */
7963 	did = vport->fc_myDID;
7964 	vport->fc_myDID = Fabric_DID;
7965 
7966 	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
7967 
7968 	/* Defer ACC response until AFTER we issue a FLOGI */
7969 	if (!(phba->hba_flag & HBA_FLOGI_ISSUED)) {
7970 		phba->defer_flogi_acc_rx_id = cmdiocb->iocb.ulpContext;
7971 		phba->defer_flogi_acc_ox_id =
7972 					cmdiocb->iocb.unsli3.rcvsli3.ox_id;
7973 
7974 		vport->fc_myDID = did;
7975 
7976 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7977 				 "3344 Deferring FLOGI ACC: rx_id: x%x,"
7978 				 " ox_id: x%x, hba_flag x%x\n",
7979 				 phba->defer_flogi_acc_rx_id,
7980 				 phba->defer_flogi_acc_ox_id, phba->hba_flag);
7981 
7982 		phba->defer_flogi_acc_flag = true;
7983 
7984 		return 0;
7985 	}
7986 
7987 	/* Send back ACC */
7988 	lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
7989 
7990 	/* Now lets put fc_myDID back to what its supposed to be */
7991 	vport->fc_myDID = did;
7992 
7993 	return 0;
7994 }
7995 
7996 /**
7997  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
7998  * @vport: pointer to a host virtual N_Port data structure.
7999  * @cmdiocb: pointer to lpfc command iocb data structure.
8000  * @ndlp: pointer to a node-list data structure.
8001  *
8002  * This routine processes Request Node Identification Data (RNID) IOCB
8003  * received as an ELS unsolicited event. Only when the RNID specified format
8004  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
8005  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
8006  * Accept (ACC) the RNID ELS command. All the other RNID formats are
8007  * rejected by invoking the lpfc_els_rsp_reject() routine.
8008  *
8009  * Return code
8010  *   0 - Successfully processed rnid iocb (currently always return 0)
8011  **/
8012 static int
8013 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8014 		  struct lpfc_nodelist *ndlp)
8015 {
8016 	struct lpfc_dmabuf *pcmd;
8017 	uint32_t *lp;
8018 	RNID *rn;
8019 	struct ls_rjt stat;
8020 
8021 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
8022 	lp = (uint32_t *) pcmd->virt;
8023 
8024 	lp++;
8025 	rn = (RNID *) lp;
8026 
8027 	/* RNID received */
8028 
8029 	switch (rn->Format) {
8030 	case 0:
8031 	case RNID_TOPOLOGY_DISC:
8032 		/* Send back ACC */
8033 		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
8034 		break;
8035 	default:
8036 		/* Reject this request because format not supported */
8037 		stat.un.b.lsRjtRsvd0 = 0;
8038 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8039 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8040 		stat.un.b.vendorUnique = 0;
8041 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
8042 			NULL);
8043 	}
8044 	return 0;
8045 }
8046 
8047 /**
8048  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
8049  * @vport: pointer to a host virtual N_Port data structure.
8050  * @cmdiocb: pointer to lpfc command iocb data structure.
8051  * @ndlp: pointer to a node-list data structure.
8052  *
8053  * Return code
8054  *   0 - Successfully processed echo iocb (currently always return 0)
8055  **/
8056 static int
8057 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8058 		  struct lpfc_nodelist *ndlp)
8059 {
8060 	uint8_t *pcmd;
8061 
8062 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
8063 
8064 	/* skip over first word of echo command to find echo data */
8065 	pcmd += sizeof(uint32_t);
8066 
8067 	lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
8068 	return 0;
8069 }
8070 
8071 /**
8072  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
8073  * @vport: pointer to a host virtual N_Port data structure.
8074  * @cmdiocb: pointer to lpfc command iocb data structure.
8075  * @ndlp: pointer to a node-list data structure.
8076  *
8077  * This routine processes a Link Incident Report Registration(LIRR) IOCB
8078  * received as an ELS unsolicited event. Currently, this function just invokes
8079  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
8080  *
8081  * Return code
8082  *   0 - Successfully processed lirr iocb (currently always return 0)
8083  **/
8084 static int
8085 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8086 		  struct lpfc_nodelist *ndlp)
8087 {
8088 	struct ls_rjt stat;
8089 
8090 	/* For now, unconditionally reject this command */
8091 	stat.un.b.lsRjtRsvd0 = 0;
8092 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8093 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8094 	stat.un.b.vendorUnique = 0;
8095 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8096 	return 0;
8097 }
8098 
8099 /**
8100  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
8101  * @vport: pointer to a host virtual N_Port data structure.
8102  * @cmdiocb: pointer to lpfc command iocb data structure.
8103  * @ndlp: pointer to a node-list data structure.
8104  *
8105  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
8106  * received as an ELS unsolicited event. A request to RRQ shall only
8107  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
8108  * Nx_Port N_Port_ID of the target Exchange is the same as the
8109  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
8110  * not accepted, an LS_RJT with reason code "Unable to perform
8111  * command request" and reason code explanation "Invalid Originator
8112  * S_ID" shall be returned. For now, we just unconditionally accept
8113  * RRQ from the target.
8114  **/
8115 static void
8116 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8117 		 struct lpfc_nodelist *ndlp)
8118 {
8119 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8120 	if (vport->phba->sli_rev == LPFC_SLI_REV4)
8121 		lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
8122 }
8123 
8124 /**
8125  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
8126  * @phba: pointer to lpfc hba data structure.
8127  * @pmb: pointer to the driver internal queue element for mailbox command.
8128  *
8129  * This routine is the completion callback function for the MBX_READ_LNK_STAT
8130  * mailbox command. This callback function is to actually send the Accept
8131  * (ACC) response to a Read Link Status (RLS) unsolicited IOCB event. It
8132  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
8133  * mailbox command, constructs the RLS response with the link statistics
8134  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
8135  * response to the RLS.
8136  *
8137  * Note that the ndlp reference count will be incremented by 1 for holding the
8138  * ndlp and the reference to ndlp will be stored into the context1 field of
8139  * the IOCB for the completion callback function to the RLS Accept Response
8140  * ELS IOCB command.
8141  *
8142  **/
8143 static void
8144 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
8145 {
8146 	int rc = 0;
8147 	MAILBOX_t *mb;
8148 	IOCB_t *icmd;
8149 	struct RLS_RSP *rls_rsp;
8150 	uint8_t *pcmd;
8151 	struct lpfc_iocbq *elsiocb;
8152 	struct lpfc_nodelist *ndlp;
8153 	uint16_t oxid;
8154 	uint16_t rxid;
8155 	uint32_t cmdsize;
8156 
8157 	mb = &pmb->u.mb;
8158 
8159 	ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
8160 	rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
8161 	oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
8162 	pmb->ctx_buf = NULL;
8163 	pmb->ctx_ndlp = NULL;
8164 
8165 	if (mb->mbxStatus) {
8166 		mempool_free(pmb, phba->mbox_mem_pool);
8167 		return;
8168 	}
8169 
8170 	cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
8171 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8172 				     lpfc_max_els_tries, ndlp,
8173 				     ndlp->nlp_DID, ELS_CMD_ACC);
8174 
8175 	/* Decrement the ndlp reference count from previous mbox command */
8176 	lpfc_nlp_put(ndlp);
8177 
8178 	if (!elsiocb) {
8179 		mempool_free(pmb, phba->mbox_mem_pool);
8180 		return;
8181 	}
8182 
8183 	icmd = &elsiocb->iocb;
8184 	icmd->ulpContext = rxid;
8185 	icmd->unsli3.rcvsli3.ox_id = oxid;
8186 
8187 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8188 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8189 	pcmd += sizeof(uint32_t); /* Skip past command */
8190 	rls_rsp = (struct RLS_RSP *)pcmd;
8191 
8192 	rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
8193 	rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
8194 	rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
8195 	rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
8196 	rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
8197 	rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
8198 	mempool_free(pmb, phba->mbox_mem_pool);
8199 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
8200 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8201 			 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
8202 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
8203 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
8204 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8205 			 ndlp->nlp_rpi);
8206 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
8207 	phba->fc_stat.elsXmitACC++;
8208 	elsiocb->context1 = lpfc_nlp_get(ndlp);
8209 	if (!elsiocb->context1) {
8210 		lpfc_els_free_iocb(phba, elsiocb);
8211 		return;
8212 	}
8213 
8214 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8215 	if (rc == IOCB_ERROR) {
8216 		lpfc_els_free_iocb(phba, elsiocb);
8217 		lpfc_nlp_put(ndlp);
8218 	}
8219 	return;
8220 }
8221 
8222 /**
8223  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
8224  * @vport: pointer to a host virtual N_Port data structure.
8225  * @cmdiocb: pointer to lpfc command iocb data structure.
8226  * @ndlp: pointer to a node-list data structure.
8227  *
8228  * This routine processes Read Link Status (RLS) IOCB received as an
8229  * ELS unsolicited event. It first checks the remote port state. If the
8230  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8231  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8232  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
8233  * for reading the HBA link statistics. It is for the callback function,
8234  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
8235  * to actually sending out RPL Accept (ACC) response.
8236  *
8237  * Return codes
8238  *   0 - Successfully processed rls iocb (currently always return 0)
8239  **/
8240 static int
8241 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8242 		 struct lpfc_nodelist *ndlp)
8243 {
8244 	struct lpfc_hba *phba = vport->phba;
8245 	LPFC_MBOXQ_t *mbox;
8246 	struct ls_rjt stat;
8247 
8248 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8249 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8250 		/* reject the unsolicited RLS request and done with it */
8251 		goto reject_out;
8252 
8253 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
8254 	if (mbox) {
8255 		lpfc_read_lnk_stat(phba, mbox);
8256 		mbox->ctx_buf = (void *)((unsigned long)
8257 			((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
8258 			cmdiocb->iocb.ulpContext)); /* rx_id */
8259 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
8260 		if (!mbox->ctx_ndlp)
8261 			goto node_err;
8262 		mbox->vport = vport;
8263 		mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
8264 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
8265 			!= MBX_NOT_FINISHED)
8266 			/* Mbox completion will send ELS Response */
8267 			return 0;
8268 		/* Decrement reference count used for the failed mbox
8269 		 * command.
8270 		 */
8271 		lpfc_nlp_put(ndlp);
8272 node_err:
8273 		mempool_free(mbox, phba->mbox_mem_pool);
8274 	}
8275 reject_out:
8276 	/* issue rejection response */
8277 	stat.un.b.lsRjtRsvd0 = 0;
8278 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8279 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8280 	stat.un.b.vendorUnique = 0;
8281 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8282 	return 0;
8283 }
8284 
8285 /**
8286  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
8287  * @vport: pointer to a host virtual N_Port data structure.
8288  * @cmdiocb: pointer to lpfc command iocb data structure.
8289  * @ndlp: pointer to a node-list data structure.
8290  *
8291  * This routine processes Read Timout Value (RTV) IOCB received as an
8292  * ELS unsolicited event. It first checks the remote port state. If the
8293  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8294  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8295  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
8296  * Value (RTV) unsolicited IOCB event.
8297  *
8298  * Note that the ndlp reference count will be incremented by 1 for holding the
8299  * ndlp and the reference to ndlp will be stored into the context1 field of
8300  * the IOCB for the completion callback function to the RTV Accept Response
8301  * ELS IOCB command.
8302  *
8303  * Return codes
8304  *   0 - Successfully processed rtv iocb (currently always return 0)
8305  **/
8306 static int
8307 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8308 		 struct lpfc_nodelist *ndlp)
8309 {
8310 	int rc = 0;
8311 	struct lpfc_hba *phba = vport->phba;
8312 	struct ls_rjt stat;
8313 	struct RTV_RSP *rtv_rsp;
8314 	uint8_t *pcmd;
8315 	struct lpfc_iocbq *elsiocb;
8316 	uint32_t cmdsize;
8317 
8318 
8319 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8320 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8321 		/* reject the unsolicited RTV request and done with it */
8322 		goto reject_out;
8323 
8324 	cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
8325 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8326 				     lpfc_max_els_tries, ndlp,
8327 				     ndlp->nlp_DID, ELS_CMD_ACC);
8328 
8329 	if (!elsiocb)
8330 		return 1;
8331 
8332 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8333 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8334 	pcmd += sizeof(uint32_t); /* Skip past command */
8335 
8336 	/* use the command's xri in the response */
8337 	elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;  /* Xri / rx_id */
8338 	elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
8339 
8340 	rtv_rsp = (struct RTV_RSP *)pcmd;
8341 
8342 	/* populate RTV payload */
8343 	rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
8344 	rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
8345 	bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
8346 	bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
8347 	rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
8348 
8349 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
8350 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8351 			 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
8352 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
8353 			 "Data: x%x x%x x%x\n",
8354 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
8355 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8356 			 ndlp->nlp_rpi,
8357 			rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
8358 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
8359 	phba->fc_stat.elsXmitACC++;
8360 	elsiocb->context1 = lpfc_nlp_get(ndlp);
8361 	if (!elsiocb->context1) {
8362 		lpfc_els_free_iocb(phba, elsiocb);
8363 		return 0;
8364 	}
8365 
8366 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8367 	if (rc == IOCB_ERROR) {
8368 		lpfc_els_free_iocb(phba, elsiocb);
8369 		lpfc_nlp_put(ndlp);
8370 	}
8371 	return 0;
8372 
8373 reject_out:
8374 	/* issue rejection response */
8375 	stat.un.b.lsRjtRsvd0 = 0;
8376 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8377 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8378 	stat.un.b.vendorUnique = 0;
8379 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8380 	return 0;
8381 }
8382 
8383 /* lpfc_issue_els_rrq - Process an unsolicited rrq iocb
8384  * @vport: pointer to a host virtual N_Port data structure.
8385  * @ndlp: pointer to a node-list data structure.
8386  * @did: DID of the target.
8387  * @rrq: Pointer to the rrq struct.
8388  *
8389  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
8390  * Successful the the completion handler will clear the RRQ.
8391  *
8392  * Return codes
8393  *   0 - Successfully sent rrq els iocb.
8394  *   1 - Failed to send rrq els iocb.
8395  **/
8396 static int
8397 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
8398 			uint32_t did, struct lpfc_node_rrq *rrq)
8399 {
8400 	struct lpfc_hba  *phba = vport->phba;
8401 	struct RRQ *els_rrq;
8402 	struct lpfc_iocbq *elsiocb;
8403 	uint8_t *pcmd;
8404 	uint16_t cmdsize;
8405 	int ret;
8406 
8407 	if (!ndlp)
8408 		return 1;
8409 
8410 	/* If ndlp is not NULL, we will bump the reference count on it */
8411 	cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
8412 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
8413 				     ELS_CMD_RRQ);
8414 	if (!elsiocb)
8415 		return 1;
8416 
8417 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8418 
8419 	/* For RRQ request, remainder of payload is Exchange IDs */
8420 	*((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
8421 	pcmd += sizeof(uint32_t);
8422 	els_rrq = (struct RRQ *) pcmd;
8423 
8424 	bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
8425 	bf_set(rrq_rxid, els_rrq, rrq->rxid);
8426 	bf_set(rrq_did, els_rrq, vport->fc_myDID);
8427 	els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
8428 	els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
8429 
8430 
8431 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8432 		"Issue RRQ:     did:x%x",
8433 		did, rrq->xritag, rrq->rxid);
8434 	elsiocb->context_un.rrq = rrq;
8435 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
8436 
8437 	lpfc_nlp_get(ndlp);
8438 	elsiocb->context1 = ndlp;
8439 
8440 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8441 	if (ret == IOCB_ERROR)
8442 		goto io_err;
8443 	return 0;
8444 
8445  io_err:
8446 	lpfc_els_free_iocb(phba, elsiocb);
8447 	lpfc_nlp_put(ndlp);
8448 	return 1;
8449 }
8450 
8451 /**
8452  * lpfc_send_rrq - Sends ELS RRQ if needed.
8453  * @phba: pointer to lpfc hba data structure.
8454  * @rrq: pointer to the active rrq.
8455  *
8456  * This routine will call the lpfc_issue_els_rrq if the rrq is
8457  * still active for the xri. If this function returns a failure then
8458  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
8459  *
8460  * Returns 0 Success.
8461  *         1 Failure.
8462  **/
8463 int
8464 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
8465 {
8466 	struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
8467 						       rrq->nlp_DID);
8468 	if (!ndlp)
8469 		return 1;
8470 
8471 	if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
8472 		return lpfc_issue_els_rrq(rrq->vport, ndlp,
8473 					 rrq->nlp_DID, rrq);
8474 	else
8475 		return 1;
8476 }
8477 
8478 /**
8479  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
8480  * @vport: pointer to a host virtual N_Port data structure.
8481  * @cmdsize: size of the ELS command.
8482  * @oldiocb: pointer to the original lpfc command iocb data structure.
8483  * @ndlp: pointer to a node-list data structure.
8484  *
8485  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
8486  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
8487  *
8488  * Note that the ndlp reference count will be incremented by 1 for holding the
8489  * ndlp and the reference to ndlp will be stored into the context1 field of
8490  * the IOCB for the completion callback function to the RPL Accept Response
8491  * ELS command.
8492  *
8493  * Return code
8494  *   0 - Successfully issued ACC RPL ELS command
8495  *   1 - Failed to issue ACC RPL ELS command
8496  **/
8497 static int
8498 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
8499 		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
8500 {
8501 	int rc = 0;
8502 	struct lpfc_hba *phba = vport->phba;
8503 	IOCB_t *icmd, *oldcmd;
8504 	RPL_RSP rpl_rsp;
8505 	struct lpfc_iocbq *elsiocb;
8506 	uint8_t *pcmd;
8507 
8508 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
8509 				     ndlp->nlp_DID, ELS_CMD_ACC);
8510 
8511 	if (!elsiocb)
8512 		return 1;
8513 
8514 	icmd = &elsiocb->iocb;
8515 	oldcmd = &oldiocb->iocb;
8516 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
8517 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
8518 
8519 	pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8520 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8521 	pcmd += sizeof(uint16_t);
8522 	*((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
8523 	pcmd += sizeof(uint16_t);
8524 
8525 	/* Setup the RPL ACC payload */
8526 	rpl_rsp.listLen = be32_to_cpu(1);
8527 	rpl_rsp.index = 0;
8528 	rpl_rsp.port_num_blk.portNum = 0;
8529 	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
8530 	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
8531 	    sizeof(struct lpfc_name));
8532 	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
8533 	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
8534 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8535 			 "0120 Xmit ELS RPL ACC response tag x%x "
8536 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
8537 			 "rpi x%x\n",
8538 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
8539 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8540 			 ndlp->nlp_rpi);
8541 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
8542 	phba->fc_stat.elsXmitACC++;
8543 	elsiocb->context1 = lpfc_nlp_get(ndlp);
8544 	if (!elsiocb->context1) {
8545 		lpfc_els_free_iocb(phba, elsiocb);
8546 		return 1;
8547 	}
8548 
8549 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8550 	if (rc == IOCB_ERROR) {
8551 		lpfc_els_free_iocb(phba, elsiocb);
8552 		lpfc_nlp_put(ndlp);
8553 		return 1;
8554 	}
8555 
8556 	return 0;
8557 }
8558 
8559 /**
8560  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
8561  * @vport: pointer to a host virtual N_Port data structure.
8562  * @cmdiocb: pointer to lpfc command iocb data structure.
8563  * @ndlp: pointer to a node-list data structure.
8564  *
8565  * This routine processes Read Port List (RPL) IOCB received as an ELS
8566  * unsolicited event. It first checks the remote port state. If the remote
8567  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
8568  * invokes the lpfc_els_rsp_reject() routine to send reject response.
8569  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
8570  * to accept the RPL.
8571  *
8572  * Return code
8573  *   0 - Successfully processed rpl iocb (currently always return 0)
8574  **/
8575 static int
8576 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8577 		 struct lpfc_nodelist *ndlp)
8578 {
8579 	struct lpfc_dmabuf *pcmd;
8580 	uint32_t *lp;
8581 	uint32_t maxsize;
8582 	uint16_t cmdsize;
8583 	RPL *rpl;
8584 	struct ls_rjt stat;
8585 
8586 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8587 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
8588 		/* issue rejection response */
8589 		stat.un.b.lsRjtRsvd0 = 0;
8590 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8591 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8592 		stat.un.b.vendorUnique = 0;
8593 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
8594 			NULL);
8595 		/* rejected the unsolicited RPL request and done with it */
8596 		return 0;
8597 	}
8598 
8599 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
8600 	lp = (uint32_t *) pcmd->virt;
8601 	rpl = (RPL *) (lp + 1);
8602 	maxsize = be32_to_cpu(rpl->maxsize);
8603 
8604 	/* We support only one port */
8605 	if ((rpl->index == 0) &&
8606 	    ((maxsize == 0) ||
8607 	     ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
8608 		cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
8609 	} else {
8610 		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
8611 	}
8612 	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
8613 
8614 	return 0;
8615 }
8616 
8617 /**
8618  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
8619  * @vport: pointer to a virtual N_Port data structure.
8620  * @cmdiocb: pointer to lpfc command iocb data structure.
8621  * @ndlp: pointer to a node-list data structure.
8622  *
8623  * This routine processes Fibre Channel Address Resolution Protocol
8624  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
8625  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
8626  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
8627  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
8628  * remote PortName is compared against the FC PortName stored in the @vport
8629  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
8630  * compared against the FC NodeName stored in the @vport data structure.
8631  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
8632  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
8633  * invoked to send out FARP Response to the remote node. Before sending the
8634  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
8635  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
8636  * routine is invoked to log into the remote port first.
8637  *
8638  * Return code
8639  *   0 - Either the FARP Match Mode not supported or successfully processed
8640  **/
8641 static int
8642 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8643 		  struct lpfc_nodelist *ndlp)
8644 {
8645 	struct lpfc_dmabuf *pcmd;
8646 	uint32_t *lp;
8647 	IOCB_t *icmd;
8648 	FARP *fp;
8649 	uint32_t cnt, did;
8650 
8651 	icmd = &cmdiocb->iocb;
8652 	did = icmd->un.elsreq64.remoteID;
8653 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
8654 	lp = (uint32_t *) pcmd->virt;
8655 
8656 	lp++;
8657 	fp = (FARP *) lp;
8658 	/* FARP-REQ received from DID <did> */
8659 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8660 			 "0601 FARP-REQ received from DID x%x\n", did);
8661 	/* We will only support match on WWPN or WWNN */
8662 	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
8663 		return 0;
8664 	}
8665 
8666 	cnt = 0;
8667 	/* If this FARP command is searching for my portname */
8668 	if (fp->Mflags & FARP_MATCH_PORT) {
8669 		if (memcmp(&fp->RportName, &vport->fc_portname,
8670 			   sizeof(struct lpfc_name)) == 0)
8671 			cnt = 1;
8672 	}
8673 
8674 	/* If this FARP command is searching for my nodename */
8675 	if (fp->Mflags & FARP_MATCH_NODE) {
8676 		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
8677 			   sizeof(struct lpfc_name)) == 0)
8678 			cnt = 1;
8679 	}
8680 
8681 	if (cnt) {
8682 		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
8683 		   (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
8684 			/* Log back into the node before sending the FARP. */
8685 			if (fp->Rflags & FARP_REQUEST_PLOGI) {
8686 				ndlp->nlp_prev_state = ndlp->nlp_state;
8687 				lpfc_nlp_set_state(vport, ndlp,
8688 						   NLP_STE_PLOGI_ISSUE);
8689 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
8690 			}
8691 
8692 			/* Send a FARP response to that node */
8693 			if (fp->Rflags & FARP_REQUEST_FARPR)
8694 				lpfc_issue_els_farpr(vport, did, 0);
8695 		}
8696 	}
8697 	return 0;
8698 }
8699 
8700 /**
8701  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
8702  * @vport: pointer to a host virtual N_Port data structure.
8703  * @cmdiocb: pointer to lpfc command iocb data structure.
8704  * @ndlp: pointer to a node-list data structure.
8705  *
8706  * This routine processes Fibre Channel Address Resolution Protocol
8707  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
8708  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
8709  * the FARP response request.
8710  *
8711  * Return code
8712  *   0 - Successfully processed FARPR IOCB (currently always return 0)
8713  **/
8714 static int
8715 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8716 		   struct lpfc_nodelist  *ndlp)
8717 {
8718 	struct lpfc_dmabuf *pcmd;
8719 	uint32_t *lp;
8720 	IOCB_t *icmd;
8721 	uint32_t did;
8722 
8723 	icmd = &cmdiocb->iocb;
8724 	did = icmd->un.elsreq64.remoteID;
8725 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
8726 	lp = (uint32_t *) pcmd->virt;
8727 
8728 	lp++;
8729 	/* FARP-RSP received from DID <did> */
8730 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8731 			 "0600 FARP-RSP received from DID x%x\n", did);
8732 	/* ACCEPT the Farp resp request */
8733 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8734 
8735 	return 0;
8736 }
8737 
8738 /**
8739  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
8740  * @vport: pointer to a host virtual N_Port data structure.
8741  * @cmdiocb: pointer to lpfc command iocb data structure.
8742  * @fan_ndlp: pointer to a node-list data structure.
8743  *
8744  * This routine processes a Fabric Address Notification (FAN) IOCB
8745  * command received as an ELS unsolicited event. The FAN ELS command will
8746  * only be processed on a physical port (i.e., the @vport represents the
8747  * physical port). The fabric NodeName and PortName from the FAN IOCB are
8748  * compared against those in the phba data structure. If any of those is
8749  * different, the lpfc_initial_flogi() routine is invoked to initialize
8750  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
8751  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
8752  * is invoked to register login to the fabric.
8753  *
8754  * Return code
8755  *   0 - Successfully processed fan iocb (currently always return 0).
8756  **/
8757 static int
8758 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8759 		 struct lpfc_nodelist *fan_ndlp)
8760 {
8761 	struct lpfc_hba *phba = vport->phba;
8762 	uint32_t *lp;
8763 	FAN *fp;
8764 
8765 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
8766 	lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
8767 	fp = (FAN *) ++lp;
8768 	/* FAN received; Fan does not have a reply sequence */
8769 	if ((vport == phba->pport) &&
8770 	    (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
8771 		if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
8772 			    sizeof(struct lpfc_name))) ||
8773 		    (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
8774 			    sizeof(struct lpfc_name)))) {
8775 			/* This port has switched fabrics. FLOGI is required */
8776 			lpfc_issue_init_vfi(vport);
8777 		} else {
8778 			/* FAN verified - skip FLOGI */
8779 			vport->fc_myDID = vport->fc_prevDID;
8780 			if (phba->sli_rev < LPFC_SLI_REV4)
8781 				lpfc_issue_fabric_reglogin(vport);
8782 			else {
8783 				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8784 					"3138 Need register VFI: (x%x/%x)\n",
8785 					vport->fc_prevDID, vport->fc_myDID);
8786 				lpfc_issue_reg_vfi(vport);
8787 			}
8788 		}
8789 	}
8790 	return 0;
8791 }
8792 
8793 /**
8794  * lpfc_els_rcv_edc - Process an unsolicited EDC iocb
8795  * @vport: pointer to a host virtual N_Port data structure.
8796  * @cmdiocb: pointer to lpfc command iocb data structure.
8797  * @ndlp: pointer to a node-list data structure.
8798  *
8799  * Return code
8800  *   0 - Successfully processed echo iocb (currently always return 0)
8801  **/
8802 static int
8803 lpfc_els_rcv_edc(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8804 		 struct lpfc_nodelist *ndlp)
8805 {
8806 	struct lpfc_hba  *phba = vport->phba;
8807 	struct fc_els_edc *edc_req;
8808 	struct fc_tlv_desc *tlv;
8809 	uint8_t *payload;
8810 	uint32_t *ptr, dtag;
8811 	const char *dtag_nm;
8812 	int desc_cnt = 0, bytes_remain;
8813 	bool rcv_cap_desc = false;
8814 
8815 	payload = ((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
8816 
8817 	edc_req = (struct fc_els_edc *)payload;
8818 	bytes_remain = be32_to_cpu(edc_req->desc_len);
8819 
8820 	ptr = (uint32_t *)payload;
8821 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
8822 			 "3319 Rcv EDC payload len %d: x%x x%x x%x\n",
8823 			 bytes_remain, be32_to_cpu(*ptr),
8824 			 be32_to_cpu(*(ptr + 1)), be32_to_cpu(*(ptr + 2)));
8825 
8826 	/* No signal support unless there is a congestion descriptor */
8827 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
8828 	phba->cgn_sig_freq = 0;
8829 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
8830 
8831 	if (bytes_remain <= 0)
8832 		goto out;
8833 
8834 	tlv = edc_req->desc;
8835 
8836 	/*
8837 	 * cycle through EDC diagnostic descriptors to find the
8838 	 * congestion signaling capability descriptor
8839 	 */
8840 	while (bytes_remain && !rcv_cap_desc) {
8841 		if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
8842 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8843 					"6464 Truncated TLV hdr on "
8844 					"Diagnostic descriptor[%d]\n",
8845 					desc_cnt);
8846 			goto out;
8847 		}
8848 
8849 		dtag = be32_to_cpu(tlv->desc_tag);
8850 		switch (dtag) {
8851 		case ELS_DTAG_LNK_FAULT_CAP:
8852 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
8853 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
8854 				sizeof(struct fc_diag_lnkflt_desc)) {
8855 				lpfc_printf_log(
8856 					phba, KERN_WARNING, LOG_CGN_MGMT,
8857 					"6465 Truncated Link Fault Diagnostic "
8858 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
8859 					desc_cnt, bytes_remain,
8860 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
8861 					sizeof(struct fc_diag_cg_sig_desc));
8862 				goto out;
8863 			}
8864 			/* No action for Link Fault descriptor for now */
8865 			break;
8866 		case ELS_DTAG_CG_SIGNAL_CAP:
8867 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
8868 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
8869 				sizeof(struct fc_diag_cg_sig_desc)) {
8870 				lpfc_printf_log(
8871 					phba, KERN_WARNING, LOG_CGN_MGMT,
8872 					"6466 Truncated cgn signal Diagnostic "
8873 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
8874 					desc_cnt, bytes_remain,
8875 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
8876 					sizeof(struct fc_diag_cg_sig_desc));
8877 				goto out;
8878 			}
8879 
8880 			phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
8881 			phba->cgn_reg_signal = phba->cgn_init_reg_signal;
8882 
8883 			/* We start negotiation with lpfc_fabric_cgn_frequency.
8884 			 * When we process the EDC, we will settle on the
8885 			 * higher frequency.
8886 			 */
8887 			phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
8888 
8889 			lpfc_least_capable_settings(
8890 				phba, (struct fc_diag_cg_sig_desc *)tlv);
8891 			rcv_cap_desc = true;
8892 			break;
8893 		default:
8894 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
8895 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8896 					"6467 unknown Diagnostic "
8897 					"Descriptor[%d]: tag x%x (%s)\n",
8898 					desc_cnt, dtag, dtag_nm);
8899 		}
8900 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
8901 		tlv = fc_tlv_next_desc(tlv);
8902 		desc_cnt++;
8903 	}
8904 out:
8905 	/* Need to send back an ACC */
8906 	lpfc_issue_els_edc_rsp(vport, cmdiocb, ndlp);
8907 
8908 	lpfc_config_cgn_signal(phba);
8909 	return 0;
8910 }
8911 
8912 /**
8913  * lpfc_els_timeout - Handler funciton to the els timer
8914  * @t: timer context used to obtain the vport.
8915  *
8916  * This routine is invoked by the ELS timer after timeout. It posts the ELS
8917  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
8918  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
8919  * up the worker thread. It is for the worker thread to invoke the routine
8920  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
8921  **/
8922 void
8923 lpfc_els_timeout(struct timer_list *t)
8924 {
8925 	struct lpfc_vport *vport = from_timer(vport, t, els_tmofunc);
8926 	struct lpfc_hba   *phba = vport->phba;
8927 	uint32_t tmo_posted;
8928 	unsigned long iflag;
8929 
8930 	spin_lock_irqsave(&vport->work_port_lock, iflag);
8931 	tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
8932 	if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
8933 		vport->work_port_events |= WORKER_ELS_TMO;
8934 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
8935 
8936 	if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
8937 		lpfc_worker_wake_up(phba);
8938 	return;
8939 }
8940 
8941 
8942 /**
8943  * lpfc_els_timeout_handler - Process an els timeout event
8944  * @vport: pointer to a virtual N_Port data structure.
8945  *
8946  * This routine is the actual handler function that processes an ELS timeout
8947  * event. It walks the ELS ring to get and abort all the IOCBs (except the
8948  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
8949  * invoking the lpfc_sli_issue_abort_iotag() routine.
8950  **/
8951 void
8952 lpfc_els_timeout_handler(struct lpfc_vport *vport)
8953 {
8954 	struct lpfc_hba  *phba = vport->phba;
8955 	struct lpfc_sli_ring *pring;
8956 	struct lpfc_iocbq *tmp_iocb, *piocb;
8957 	IOCB_t *cmd = NULL;
8958 	struct lpfc_dmabuf *pcmd;
8959 	uint32_t els_command = 0;
8960 	uint32_t timeout;
8961 	uint32_t remote_ID = 0xffffffff;
8962 	LIST_HEAD(abort_list);
8963 
8964 
8965 	timeout = (uint32_t)(phba->fc_ratov << 1);
8966 
8967 	pring = lpfc_phba_elsring(phba);
8968 	if (unlikely(!pring))
8969 		return;
8970 
8971 	if (phba->pport->load_flag & FC_UNLOADING)
8972 		return;
8973 
8974 	spin_lock_irq(&phba->hbalock);
8975 	if (phba->sli_rev == LPFC_SLI_REV4)
8976 		spin_lock(&pring->ring_lock);
8977 
8978 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
8979 		cmd = &piocb->iocb;
8980 
8981 		if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
8982 		    piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
8983 		    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
8984 			continue;
8985 
8986 		if (piocb->vport != vport)
8987 			continue;
8988 
8989 		pcmd = (struct lpfc_dmabuf *) piocb->context2;
8990 		if (pcmd)
8991 			els_command = *(uint32_t *) (pcmd->virt);
8992 
8993 		if (els_command == ELS_CMD_FARP ||
8994 		    els_command == ELS_CMD_FARPR ||
8995 		    els_command == ELS_CMD_FDISC)
8996 			continue;
8997 
8998 		if (piocb->drvrTimeout > 0) {
8999 			if (piocb->drvrTimeout >= timeout)
9000 				piocb->drvrTimeout -= timeout;
9001 			else
9002 				piocb->drvrTimeout = 0;
9003 			continue;
9004 		}
9005 
9006 		remote_ID = 0xffffffff;
9007 		if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
9008 			remote_ID = cmd->un.elsreq64.remoteID;
9009 		else {
9010 			struct lpfc_nodelist *ndlp;
9011 			ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
9012 			if (ndlp)
9013 				remote_ID = ndlp->nlp_DID;
9014 		}
9015 		list_add_tail(&piocb->dlist, &abort_list);
9016 	}
9017 	if (phba->sli_rev == LPFC_SLI_REV4)
9018 		spin_unlock(&pring->ring_lock);
9019 	spin_unlock_irq(&phba->hbalock);
9020 
9021 	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9022 		cmd = &piocb->iocb;
9023 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9024 			 "0127 ELS timeout Data: x%x x%x x%x "
9025 			 "x%x\n", els_command,
9026 			 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
9027 		spin_lock_irq(&phba->hbalock);
9028 		list_del_init(&piocb->dlist);
9029 		lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9030 		spin_unlock_irq(&phba->hbalock);
9031 	}
9032 
9033 	/* Make sure HBA is alive */
9034 	lpfc_issue_hb_tmo(phba);
9035 
9036 	if (!list_empty(&pring->txcmplq))
9037 		if (!(phba->pport->load_flag & FC_UNLOADING))
9038 			mod_timer(&vport->els_tmofunc,
9039 				  jiffies + msecs_to_jiffies(1000 * timeout));
9040 }
9041 
9042 /**
9043  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
9044  * @vport: pointer to a host virtual N_Port data structure.
9045  *
9046  * This routine is used to clean up all the outstanding ELS commands on a
9047  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
9048  * routine. After that, it walks the ELS transmit queue to remove all the
9049  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
9050  * the IOCBs with a non-NULL completion callback function, the callback
9051  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9052  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
9053  * callback function, the IOCB will simply be released. Finally, it walks
9054  * the ELS transmit completion queue to issue an abort IOCB to any transmit
9055  * completion queue IOCB that is associated with the @vport and is not
9056  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
9057  * part of the discovery state machine) out to HBA by invoking the
9058  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
9059  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
9060  * the IOCBs are aborted when this function returns.
9061  **/
9062 void
9063 lpfc_els_flush_cmd(struct lpfc_vport *vport)
9064 {
9065 	LIST_HEAD(abort_list);
9066 	struct lpfc_hba  *phba = vport->phba;
9067 	struct lpfc_sli_ring *pring;
9068 	struct lpfc_iocbq *tmp_iocb, *piocb;
9069 	IOCB_t *cmd = NULL;
9070 	unsigned long iflags = 0;
9071 
9072 	lpfc_fabric_abort_vport(vport);
9073 
9074 	/*
9075 	 * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
9076 	 * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
9077 	 * ultimately grabs the ring_lock, the driver must splice the list into
9078 	 * a working list and release the locks before calling the abort.
9079 	 */
9080 	spin_lock_irqsave(&phba->hbalock, iflags);
9081 	pring = lpfc_phba_elsring(phba);
9082 
9083 	/* Bail out if we've no ELS wq, like in PCI error recovery case. */
9084 	if (unlikely(!pring)) {
9085 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9086 		return;
9087 	}
9088 
9089 	if (phba->sli_rev == LPFC_SLI_REV4)
9090 		spin_lock(&pring->ring_lock);
9091 
9092 	/* First we need to issue aborts to outstanding cmds on txcmpl */
9093 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
9094 		if (piocb->iocb_flag & LPFC_IO_LIBDFC)
9095 			continue;
9096 
9097 		if (piocb->vport != vport)
9098 			continue;
9099 
9100 		if (piocb->iocb_flag & LPFC_DRIVER_ABORTED)
9101 			continue;
9102 
9103 		/* On the ELS ring we can have ELS_REQUESTs or
9104 		 * GEN_REQUESTs waiting for a response.
9105 		 */
9106 		cmd = &piocb->iocb;
9107 		if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
9108 			list_add_tail(&piocb->dlist, &abort_list);
9109 
9110 			/* If the link is down when flushing ELS commands
9111 			 * the firmware will not complete them till after
9112 			 * the link comes back up. This may confuse
9113 			 * discovery for the new link up, so we need to
9114 			 * change the compl routine to just clean up the iocb
9115 			 * and avoid any retry logic.
9116 			 */
9117 			if (phba->link_state == LPFC_LINK_DOWN)
9118 				piocb->iocb_cmpl = lpfc_cmpl_els_link_down;
9119 		}
9120 		if (cmd->ulpCommand == CMD_GEN_REQUEST64_CR)
9121 			list_add_tail(&piocb->dlist, &abort_list);
9122 	}
9123 
9124 	if (phba->sli_rev == LPFC_SLI_REV4)
9125 		spin_unlock(&pring->ring_lock);
9126 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9127 
9128 	/* Abort each txcmpl iocb on aborted list and remove the dlist links. */
9129 	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9130 		spin_lock_irqsave(&phba->hbalock, iflags);
9131 		list_del_init(&piocb->dlist);
9132 		lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9133 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9134 	}
9135 	/* Make sure HBA is alive */
9136 	lpfc_issue_hb_tmo(phba);
9137 
9138 	if (!list_empty(&abort_list))
9139 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9140 				 "3387 abort list for txq not empty\n");
9141 	INIT_LIST_HEAD(&abort_list);
9142 
9143 	spin_lock_irqsave(&phba->hbalock, iflags);
9144 	if (phba->sli_rev == LPFC_SLI_REV4)
9145 		spin_lock(&pring->ring_lock);
9146 
9147 	/* No need to abort the txq list,
9148 	 * just queue them up for lpfc_sli_cancel_iocbs
9149 	 */
9150 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
9151 		cmd = &piocb->iocb;
9152 
9153 		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
9154 			continue;
9155 		}
9156 
9157 		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
9158 		if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
9159 		    cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
9160 		    cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9161 		    cmd->ulpCommand == CMD_ABORT_XRI_CN)
9162 			continue;
9163 
9164 		if (piocb->vport != vport)
9165 			continue;
9166 
9167 		list_del_init(&piocb->list);
9168 		list_add_tail(&piocb->list, &abort_list);
9169 	}
9170 
9171 	/* The same holds true for any FLOGI/FDISC on the fabric_iocb_list */
9172 	if (vport == phba->pport) {
9173 		list_for_each_entry_safe(piocb, tmp_iocb,
9174 					 &phba->fabric_iocb_list, list) {
9175 			cmd = &piocb->iocb;
9176 			list_del_init(&piocb->list);
9177 			list_add_tail(&piocb->list, &abort_list);
9178 		}
9179 	}
9180 
9181 	if (phba->sli_rev == LPFC_SLI_REV4)
9182 		spin_unlock(&pring->ring_lock);
9183 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9184 
9185 	/* Cancel all the IOCBs from the completions list */
9186 	lpfc_sli_cancel_iocbs(phba, &abort_list,
9187 			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
9188 
9189 	return;
9190 }
9191 
9192 /**
9193  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
9194  * @phba: pointer to lpfc hba data structure.
9195  *
9196  * This routine is used to clean up all the outstanding ELS commands on a
9197  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
9198  * routine. After that, it walks the ELS transmit queue to remove all the
9199  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
9200  * the IOCBs with the completion callback function associated, the callback
9201  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9202  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
9203  * callback function associated, the IOCB will simply be released. Finally,
9204  * it walks the ELS transmit completion queue to issue an abort IOCB to any
9205  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
9206  * management plane IOCBs that are not part of the discovery state machine)
9207  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
9208  **/
9209 void
9210 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
9211 {
9212 	struct lpfc_vport *vport;
9213 
9214 	spin_lock_irq(&phba->port_list_lock);
9215 	list_for_each_entry(vport, &phba->port_list, listentry)
9216 		lpfc_els_flush_cmd(vport);
9217 	spin_unlock_irq(&phba->port_list_lock);
9218 
9219 	return;
9220 }
9221 
9222 /**
9223  * lpfc_send_els_failure_event - Posts an ELS command failure event
9224  * @phba: Pointer to hba context object.
9225  * @cmdiocbp: Pointer to command iocb which reported error.
9226  * @rspiocbp: Pointer to response iocb which reported error.
9227  *
9228  * This function sends an event when there is an ELS command
9229  * failure.
9230  **/
9231 void
9232 lpfc_send_els_failure_event(struct lpfc_hba *phba,
9233 			struct lpfc_iocbq *cmdiocbp,
9234 			struct lpfc_iocbq *rspiocbp)
9235 {
9236 	struct lpfc_vport *vport = cmdiocbp->vport;
9237 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9238 	struct lpfc_lsrjt_event lsrjt_event;
9239 	struct lpfc_fabric_event_header fabric_event;
9240 	struct ls_rjt stat;
9241 	struct lpfc_nodelist *ndlp;
9242 	uint32_t *pcmd;
9243 
9244 	ndlp = cmdiocbp->context1;
9245 	if (!ndlp)
9246 		return;
9247 
9248 	if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
9249 		lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
9250 		lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
9251 		memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
9252 			sizeof(struct lpfc_name));
9253 		memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
9254 			sizeof(struct lpfc_name));
9255 		pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9256 			cmdiocbp->context2)->virt);
9257 		lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
9258 		stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
9259 		lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
9260 		lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
9261 		fc_host_post_vendor_event(shost,
9262 			fc_get_event_number(),
9263 			sizeof(lsrjt_event),
9264 			(char *)&lsrjt_event,
9265 			LPFC_NL_VENDOR_ID);
9266 		return;
9267 	}
9268 	if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
9269 		(rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
9270 		fabric_event.event_type = FC_REG_FABRIC_EVENT;
9271 		if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
9272 			fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
9273 		else
9274 			fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
9275 		memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
9276 			sizeof(struct lpfc_name));
9277 		memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
9278 			sizeof(struct lpfc_name));
9279 		fc_host_post_vendor_event(shost,
9280 			fc_get_event_number(),
9281 			sizeof(fabric_event),
9282 			(char *)&fabric_event,
9283 			LPFC_NL_VENDOR_ID);
9284 		return;
9285 	}
9286 
9287 }
9288 
9289 /**
9290  * lpfc_send_els_event - Posts unsolicited els event
9291  * @vport: Pointer to vport object.
9292  * @ndlp: Pointer FC node object.
9293  * @payload: ELS command code type.
9294  *
9295  * This function posts an event when there is an incoming
9296  * unsolicited ELS command.
9297  **/
9298 static void
9299 lpfc_send_els_event(struct lpfc_vport *vport,
9300 		    struct lpfc_nodelist *ndlp,
9301 		    uint32_t *payload)
9302 {
9303 	struct lpfc_els_event_header *els_data = NULL;
9304 	struct lpfc_logo_event *logo_data = NULL;
9305 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9306 
9307 	if (*payload == ELS_CMD_LOGO) {
9308 		logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
9309 		if (!logo_data) {
9310 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9311 				"0148 Failed to allocate memory "
9312 				"for LOGO event\n");
9313 			return;
9314 		}
9315 		els_data = &logo_data->header;
9316 	} else {
9317 		els_data = kmalloc(sizeof(struct lpfc_els_event_header),
9318 			GFP_KERNEL);
9319 		if (!els_data) {
9320 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9321 				"0149 Failed to allocate memory "
9322 				"for ELS event\n");
9323 			return;
9324 		}
9325 	}
9326 	els_data->event_type = FC_REG_ELS_EVENT;
9327 	switch (*payload) {
9328 	case ELS_CMD_PLOGI:
9329 		els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
9330 		break;
9331 	case ELS_CMD_PRLO:
9332 		els_data->subcategory = LPFC_EVENT_PRLO_RCV;
9333 		break;
9334 	case ELS_CMD_ADISC:
9335 		els_data->subcategory = LPFC_EVENT_ADISC_RCV;
9336 		break;
9337 	case ELS_CMD_LOGO:
9338 		els_data->subcategory = LPFC_EVENT_LOGO_RCV;
9339 		/* Copy the WWPN in the LOGO payload */
9340 		memcpy(logo_data->logo_wwpn, &payload[2],
9341 			sizeof(struct lpfc_name));
9342 		break;
9343 	default:
9344 		kfree(els_data);
9345 		return;
9346 	}
9347 	memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
9348 	memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
9349 	if (*payload == ELS_CMD_LOGO) {
9350 		fc_host_post_vendor_event(shost,
9351 			fc_get_event_number(),
9352 			sizeof(struct lpfc_logo_event),
9353 			(char *)logo_data,
9354 			LPFC_NL_VENDOR_ID);
9355 		kfree(logo_data);
9356 	} else {
9357 		fc_host_post_vendor_event(shost,
9358 			fc_get_event_number(),
9359 			sizeof(struct lpfc_els_event_header),
9360 			(char *)els_data,
9361 			LPFC_NL_VENDOR_ID);
9362 		kfree(els_data);
9363 	}
9364 
9365 	return;
9366 }
9367 
9368 
9369 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_li_event_nm, fc_fpin_li_event_types,
9370 			FC_FPIN_LI_EVT_TYPES_INIT);
9371 
9372 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_deli_event_nm, fc_fpin_deli_event_types,
9373 			FC_FPIN_DELI_EVT_TYPES_INIT);
9374 
9375 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_event_nm, fc_fpin_congn_event_types,
9376 			FC_FPIN_CONGN_EVT_TYPES_INIT);
9377 
9378 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_severity_nm,
9379 			fc_fpin_congn_severity_types,
9380 			FC_FPIN_CONGN_SEVERITY_INIT);
9381 
9382 
9383 /**
9384  * lpfc_display_fpin_wwpn - Display WWPNs accessible by the attached port
9385  * @phba: Pointer to phba object.
9386  * @wwnlist: Pointer to list of WWPNs in FPIN payload
9387  * @cnt: count of WWPNs in FPIN payload
9388  *
9389  * This routine is called by LI and PC descriptors.
9390  * Limit the number of WWPNs displayed to 6 log messages, 6 per log message
9391  */
9392 static void
9393 lpfc_display_fpin_wwpn(struct lpfc_hba *phba, __be64 *wwnlist, u32 cnt)
9394 {
9395 	char buf[LPFC_FPIN_WWPN_LINE_SZ];
9396 	__be64 wwn;
9397 	u64 wwpn;
9398 	int i, len;
9399 	int line = 0;
9400 	int wcnt = 0;
9401 	bool endit = false;
9402 
9403 	len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ, "Accessible WWPNs:");
9404 	for (i = 0; i < cnt; i++) {
9405 		/* Are we on the last WWPN */
9406 		if (i == (cnt - 1))
9407 			endit = true;
9408 
9409 		/* Extract the next WWPN from the payload */
9410 		wwn = *wwnlist++;
9411 		wwpn = be64_to_cpu(wwn);
9412 		len += scnprintf(buf + len, LPFC_FPIN_WWPN_LINE_SZ - len,
9413 				 " %016llx", wwpn);
9414 
9415 		/* Log a message if we are on the last WWPN
9416 		 * or if we hit the max allowed per message.
9417 		 */
9418 		wcnt++;
9419 		if (wcnt == LPFC_FPIN_WWPN_LINE_CNT || endit) {
9420 			buf[len] = 0;
9421 			lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9422 					"4686 %s\n", buf);
9423 
9424 			/* Check if we reached the last WWPN */
9425 			if (endit)
9426 				return;
9427 
9428 			/* Limit the number of log message displayed per FPIN */
9429 			line++;
9430 			if (line == LPFC_FPIN_WWPN_NUM_LINE) {
9431 				lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9432 						"4687 %d WWPNs Truncated\n",
9433 						cnt - i - 1);
9434 				return;
9435 			}
9436 
9437 			/* Start over with next log message */
9438 			wcnt = 0;
9439 			len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ,
9440 					"Additional WWPNs:");
9441 		}
9442 	}
9443 }
9444 
9445 /**
9446  * lpfc_els_rcv_fpin_li - Process an FPIN Link Integrity Event.
9447  * @phba: Pointer to phba object.
9448  * @tlv:  Pointer to the Link Integrity Notification Descriptor.
9449  *
9450  * This function processes a Link Integrity FPIN event by logging a message.
9451  **/
9452 static void
9453 lpfc_els_rcv_fpin_li(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9454 {
9455 	struct fc_fn_li_desc *li = (struct fc_fn_li_desc *)tlv;
9456 	const char *li_evt_str;
9457 	u32 li_evt, cnt;
9458 
9459 	li_evt = be16_to_cpu(li->event_type);
9460 	li_evt_str = lpfc_get_fpin_li_event_nm(li_evt);
9461 	cnt = be32_to_cpu(li->pname_count);
9462 
9463 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9464 			"4680 FPIN Link Integrity %s (x%x) "
9465 			"Detecting PN x%016llx Attached PN x%016llx "
9466 			"Duration %d mSecs Count %d Port Cnt %d\n",
9467 			li_evt_str, li_evt,
9468 			be64_to_cpu(li->detecting_wwpn),
9469 			be64_to_cpu(li->attached_wwpn),
9470 			be32_to_cpu(li->event_threshold),
9471 			be32_to_cpu(li->event_count), cnt);
9472 
9473 	lpfc_display_fpin_wwpn(phba, (__be64 *)&li->pname_list, cnt);
9474 }
9475 
9476 /**
9477  * lpfc_els_rcv_fpin_del - Process an FPIN Delivery Event.
9478  * @phba: Pointer to hba object.
9479  * @tlv:  Pointer to the Delivery Notification Descriptor TLV
9480  *
9481  * This function processes a Delivery FPIN event by logging a message.
9482  **/
9483 static void
9484 lpfc_els_rcv_fpin_del(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9485 {
9486 	struct fc_fn_deli_desc *del = (struct fc_fn_deli_desc *)tlv;
9487 	const char *del_rsn_str;
9488 	u32 del_rsn;
9489 	__be32 *frame;
9490 
9491 	del_rsn = be16_to_cpu(del->deli_reason_code);
9492 	del_rsn_str = lpfc_get_fpin_deli_event_nm(del_rsn);
9493 
9494 	/* Skip over desc_tag/desc_len header to payload */
9495 	frame = (__be32 *)(del + 1);
9496 
9497 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9498 			"4681 FPIN Delivery %s (x%x) "
9499 			"Detecting PN x%016llx Attached PN x%016llx "
9500 			"DiscHdr0  x%08x "
9501 			"DiscHdr1 x%08x DiscHdr2 x%08x DiscHdr3 x%08x "
9502 			"DiscHdr4 x%08x DiscHdr5 x%08x\n",
9503 			del_rsn_str, del_rsn,
9504 			be64_to_cpu(del->detecting_wwpn),
9505 			be64_to_cpu(del->attached_wwpn),
9506 			be32_to_cpu(frame[0]),
9507 			be32_to_cpu(frame[1]),
9508 			be32_to_cpu(frame[2]),
9509 			be32_to_cpu(frame[3]),
9510 			be32_to_cpu(frame[4]),
9511 			be32_to_cpu(frame[5]));
9512 }
9513 
9514 /**
9515  * lpfc_els_rcv_fpin_peer_cgn - Process a FPIN Peer Congestion Event.
9516  * @phba: Pointer to hba object.
9517  * @tlv:  Pointer to the Peer Congestion Notification Descriptor TLV
9518  *
9519  * This function processes a Peer Congestion FPIN event by logging a message.
9520  **/
9521 static void
9522 lpfc_els_rcv_fpin_peer_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9523 {
9524 	struct fc_fn_peer_congn_desc *pc = (struct fc_fn_peer_congn_desc *)tlv;
9525 	const char *pc_evt_str;
9526 	u32 pc_evt, cnt;
9527 
9528 	pc_evt = be16_to_cpu(pc->event_type);
9529 	pc_evt_str = lpfc_get_fpin_congn_event_nm(pc_evt);
9530 	cnt = be32_to_cpu(pc->pname_count);
9531 
9532 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_ELS,
9533 			"4684 FPIN Peer Congestion %s (x%x) "
9534 			"Duration %d mSecs "
9535 			"Detecting PN x%016llx Attached PN x%016llx "
9536 			"Impacted Port Cnt %d\n",
9537 			pc_evt_str, pc_evt,
9538 			be32_to_cpu(pc->event_period),
9539 			be64_to_cpu(pc->detecting_wwpn),
9540 			be64_to_cpu(pc->attached_wwpn),
9541 			cnt);
9542 
9543 	lpfc_display_fpin_wwpn(phba, (__be64 *)&pc->pname_list, cnt);
9544 }
9545 
9546 /**
9547  * lpfc_els_rcv_fpin_cgn - Process an FPIN Congestion notification
9548  * @phba: Pointer to hba object.
9549  * @tlv:  Pointer to the Congestion Notification Descriptor TLV
9550  *
9551  * This function processes an FPIN Congestion Notifiction.  The notification
9552  * could be an Alarm or Warning.  This routine feeds that data into driver's
9553  * running congestion algorithm. It also processes the FPIN by
9554  * logging a message. It returns 1 to indicate deliver this message
9555  * to the upper layer or 0 to indicate don't deliver it.
9556  **/
9557 static int
9558 lpfc_els_rcv_fpin_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9559 {
9560 	struct lpfc_cgn_info *cp;
9561 	struct fc_fn_congn_desc *cgn = (struct fc_fn_congn_desc *)tlv;
9562 	const char *cgn_evt_str;
9563 	u32 cgn_evt;
9564 	const char *cgn_sev_str;
9565 	u32 cgn_sev;
9566 	uint16_t value;
9567 	u32 crc;
9568 	bool nm_log = false;
9569 	int rc = 1;
9570 
9571 	cgn_evt = be16_to_cpu(cgn->event_type);
9572 	cgn_evt_str = lpfc_get_fpin_congn_event_nm(cgn_evt);
9573 	cgn_sev = cgn->severity;
9574 	cgn_sev_str = lpfc_get_fpin_congn_severity_nm(cgn_sev);
9575 
9576 	/* The driver only takes action on a Credit Stall or Oversubscription
9577 	 * event type to engage the IO algorithm.  The driver prints an
9578 	 * unmaskable message only for Lost Credit and Credit Stall.
9579 	 * TODO: Still need to have definition of host action on clear,
9580 	 *       lost credit and device specific event types.
9581 	 */
9582 	switch (cgn_evt) {
9583 	case FPIN_CONGN_LOST_CREDIT:
9584 		nm_log = true;
9585 		break;
9586 	case FPIN_CONGN_CREDIT_STALL:
9587 		nm_log = true;
9588 		fallthrough;
9589 	case FPIN_CONGN_OVERSUBSCRIPTION:
9590 		if (cgn_evt == FPIN_CONGN_OVERSUBSCRIPTION)
9591 			nm_log = false;
9592 		switch (cgn_sev) {
9593 		case FPIN_CONGN_SEVERITY_ERROR:
9594 			/* Take action here for an Alarm event */
9595 			if (phba->cmf_active_mode != LPFC_CFG_OFF) {
9596 				if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_ALARM) {
9597 					/* Track of alarm cnt for cgn_info */
9598 					atomic_inc(&phba->cgn_fabric_alarm_cnt);
9599 					/* Track of alarm cnt for SYNC_WQE */
9600 					atomic_inc(&phba->cgn_sync_alarm_cnt);
9601 				}
9602 				goto cleanup;
9603 			}
9604 			break;
9605 		case FPIN_CONGN_SEVERITY_WARNING:
9606 			/* Take action here for a Warning event */
9607 			if (phba->cmf_active_mode != LPFC_CFG_OFF) {
9608 				if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_WARN) {
9609 					/* Track of warning cnt for cgn_info */
9610 					atomic_inc(&phba->cgn_fabric_warn_cnt);
9611 					/* Track of warning cnt for SYNC_WQE */
9612 					atomic_inc(&phba->cgn_sync_warn_cnt);
9613 				}
9614 cleanup:
9615 				/* Save frequency in ms */
9616 				phba->cgn_fpin_frequency =
9617 					be32_to_cpu(cgn->event_period);
9618 				value = phba->cgn_fpin_frequency;
9619 				if (phba->cgn_i) {
9620 					cp = (struct lpfc_cgn_info *)
9621 						phba->cgn_i->virt;
9622 					if (phba->cgn_reg_fpin &
9623 						LPFC_CGN_FPIN_ALARM)
9624 						cp->cgn_alarm_freq =
9625 							cpu_to_le16(value);
9626 					if (phba->cgn_reg_fpin &
9627 						LPFC_CGN_FPIN_WARN)
9628 						cp->cgn_warn_freq =
9629 							cpu_to_le16(value);
9630 					crc = lpfc_cgn_calc_crc32
9631 						(cp,
9632 						LPFC_CGN_INFO_SZ,
9633 						LPFC_CGN_CRC32_SEED);
9634 					cp->cgn_info_crc = cpu_to_le32(crc);
9635 				}
9636 
9637 				/* Don't deliver to upper layer since
9638 				 * driver took action on this tlv.
9639 				 */
9640 				rc = 0;
9641 			}
9642 			break;
9643 		}
9644 		break;
9645 	}
9646 
9647 	/* Change the log level to unmaskable for the following event types. */
9648 	lpfc_printf_log(phba, (nm_log ? KERN_WARNING : KERN_INFO),
9649 			LOG_CGN_MGMT | LOG_ELS,
9650 			"4683 FPIN CONGESTION %s type %s (x%x) Event "
9651 			"Duration %d mSecs\n",
9652 			cgn_sev_str, cgn_evt_str, cgn_evt,
9653 			be32_to_cpu(cgn->event_period));
9654 	return rc;
9655 }
9656 
9657 void
9658 lpfc_els_rcv_fpin(struct lpfc_vport *vport, void *p, u32 fpin_length)
9659 {
9660 	struct lpfc_hba *phba = vport->phba;
9661 	struct fc_els_fpin *fpin = (struct fc_els_fpin *)p;
9662 	struct fc_tlv_desc *tlv, *first_tlv, *current_tlv;
9663 	const char *dtag_nm;
9664 	int desc_cnt = 0, bytes_remain, cnt;
9665 	u32 dtag, deliver = 0;
9666 	int len;
9667 
9668 	/* FPINs handled only if we are in the right discovery state */
9669 	if (vport->port_state < LPFC_DISC_AUTH)
9670 		return;
9671 
9672 	/* make sure there is the full fpin header */
9673 	if (fpin_length < sizeof(struct fc_els_fpin))
9674 		return;
9675 
9676 	/* Sanity check descriptor length. The desc_len value does not
9677 	 * include space for the ELS command and the desc_len fields.
9678 	 */
9679 	len = be32_to_cpu(fpin->desc_len);
9680 	if (fpin_length < len + sizeof(struct fc_els_fpin)) {
9681 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
9682 				"4671 Bad ELS FPIN length %d: %d\n",
9683 				len, fpin_length);
9684 		return;
9685 	}
9686 
9687 	tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
9688 	first_tlv = tlv;
9689 	bytes_remain = fpin_length - offsetof(struct fc_els_fpin, fpin_desc);
9690 	bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
9691 
9692 	/* process each descriptor separately */
9693 	while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
9694 	       bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
9695 		dtag = be32_to_cpu(tlv->desc_tag);
9696 		switch (dtag) {
9697 		case ELS_DTAG_LNK_INTEGRITY:
9698 			lpfc_els_rcv_fpin_li(phba, tlv);
9699 			deliver = 1;
9700 			break;
9701 		case ELS_DTAG_DELIVERY:
9702 			lpfc_els_rcv_fpin_del(phba, tlv);
9703 			deliver = 1;
9704 			break;
9705 		case ELS_DTAG_PEER_CONGEST:
9706 			lpfc_els_rcv_fpin_peer_cgn(phba, tlv);
9707 			deliver = 1;
9708 			break;
9709 		case ELS_DTAG_CONGESTION:
9710 			deliver = lpfc_els_rcv_fpin_cgn(phba, tlv);
9711 			break;
9712 		default:
9713 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
9714 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
9715 					"4678 unknown FPIN descriptor[%d]: "
9716 					"tag x%x (%s)\n",
9717 					desc_cnt, dtag, dtag_nm);
9718 
9719 			/* If descriptor is bad, drop the rest of the data */
9720 			return;
9721 		}
9722 		lpfc_cgn_update_stat(phba, dtag);
9723 		cnt = be32_to_cpu(tlv->desc_len);
9724 
9725 		/* Sanity check descriptor length. The desc_len value does not
9726 		 * include space for the desc_tag and the desc_len fields.
9727 		 */
9728 		len -= (cnt + sizeof(struct fc_tlv_desc));
9729 		if (len < 0) {
9730 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
9731 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
9732 					"4672 Bad FPIN descriptor TLV length "
9733 					"%d: %d %d %s\n",
9734 					cnt, len, fpin_length, dtag_nm);
9735 			return;
9736 		}
9737 
9738 		current_tlv = tlv;
9739 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
9740 		tlv = fc_tlv_next_desc(tlv);
9741 
9742 		/* Format payload such that the FPIN delivered to the
9743 		 * upper layer is a single descriptor FPIN.
9744 		 */
9745 		if (desc_cnt)
9746 			memcpy(first_tlv, current_tlv,
9747 			       (cnt + sizeof(struct fc_els_fpin)));
9748 
9749 		/* Adjust the length so that it only reflects a
9750 		 * single descriptor FPIN.
9751 		 */
9752 		fpin_length = cnt + sizeof(struct fc_els_fpin);
9753 		fpin->desc_len = cpu_to_be32(fpin_length);
9754 		fpin_length += sizeof(struct fc_els_fpin); /* the entire FPIN */
9755 
9756 		/* Send every descriptor individually to the upper layer */
9757 		if (deliver)
9758 			fc_host_fpin_rcv(lpfc_shost_from_vport(vport),
9759 					 fpin_length, (char *)fpin);
9760 		desc_cnt++;
9761 	}
9762 }
9763 
9764 /**
9765  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
9766  * @phba: pointer to lpfc hba data structure.
9767  * @pring: pointer to a SLI ring.
9768  * @vport: pointer to a host virtual N_Port data structure.
9769  * @elsiocb: pointer to lpfc els command iocb data structure.
9770  *
9771  * This routine is used for processing the IOCB associated with a unsolicited
9772  * event. It first determines whether there is an existing ndlp that matches
9773  * the DID from the unsolicited IOCB. If not, it will create a new one with
9774  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
9775  * IOCB is then used to invoke the proper routine and to set up proper state
9776  * of the discovery state machine.
9777  **/
9778 static void
9779 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9780 		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
9781 {
9782 	struct lpfc_nodelist *ndlp;
9783 	struct ls_rjt stat;
9784 	uint32_t *payload, payload_len;
9785 	uint32_t cmd, did, newnode;
9786 	uint8_t rjt_exp, rjt_err = 0, init_link = 0;
9787 	IOCB_t *icmd = &elsiocb->iocb;
9788 	LPFC_MBOXQ_t *mbox;
9789 
9790 	if (!vport || !(elsiocb->context2))
9791 		goto dropit;
9792 
9793 	newnode = 0;
9794 	payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
9795 	payload_len = elsiocb->iocb.unsli3.rcvsli3.acc_len;
9796 	cmd = *payload;
9797 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
9798 		lpfc_post_buffer(phba, pring, 1);
9799 
9800 	did = icmd->un.rcvels.remoteID;
9801 	if (icmd->ulpStatus) {
9802 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9803 			"RCV Unsol ELS:  status:x%x/x%x did:x%x",
9804 			icmd->ulpStatus, icmd->un.ulpWord[4], did);
9805 		goto dropit;
9806 	}
9807 
9808 	/* Check to see if link went down during discovery */
9809 	if (lpfc_els_chk_latt(vport))
9810 		goto dropit;
9811 
9812 	/* Ignore traffic received during vport shutdown. */
9813 	if (vport->load_flag & FC_UNLOADING)
9814 		goto dropit;
9815 
9816 	/* If NPort discovery is delayed drop incoming ELS */
9817 	if ((vport->fc_flag & FC_DISC_DELAYED) &&
9818 			(cmd != ELS_CMD_PLOGI))
9819 		goto dropit;
9820 
9821 	ndlp = lpfc_findnode_did(vport, did);
9822 	if (!ndlp) {
9823 		/* Cannot find existing Fabric ndlp, so allocate a new one */
9824 		ndlp = lpfc_nlp_init(vport, did);
9825 		if (!ndlp)
9826 			goto dropit;
9827 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
9828 		newnode = 1;
9829 		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
9830 			ndlp->nlp_type |= NLP_FABRIC;
9831 	} else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
9832 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
9833 		newnode = 1;
9834 	}
9835 
9836 	phba->fc_stat.elsRcvFrame++;
9837 
9838 	/*
9839 	 * Do not process any unsolicited ELS commands
9840 	 * if the ndlp is in DEV_LOSS
9841 	 */
9842 	spin_lock_irq(&ndlp->lock);
9843 	if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
9844 		spin_unlock_irq(&ndlp->lock);
9845 		if (newnode)
9846 			lpfc_nlp_put(ndlp);
9847 		goto dropit;
9848 	}
9849 	spin_unlock_irq(&ndlp->lock);
9850 
9851 	elsiocb->context1 = lpfc_nlp_get(ndlp);
9852 	if (!elsiocb->context1)
9853 		goto dropit;
9854 	elsiocb->vport = vport;
9855 
9856 	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
9857 		cmd &= ELS_CMD_MASK;
9858 	}
9859 	/* ELS command <elsCmd> received from NPORT <did> */
9860 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9861 			 "0112 ELS command x%x received from NPORT x%x "
9862 			 "refcnt %d Data: x%x x%x x%x x%x\n",
9863 			 cmd, did, kref_read(&ndlp->kref), vport->port_state,
9864 			 vport->fc_flag, vport->fc_myDID, vport->fc_prevDID);
9865 
9866 	/* reject till our FLOGI completes or PLOGI assigned DID via PT2PT */
9867 	if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
9868 	    (cmd != ELS_CMD_FLOGI) &&
9869 	    !((cmd == ELS_CMD_PLOGI) && (vport->fc_flag & FC_PT2PT))) {
9870 		rjt_err = LSRJT_LOGICAL_BSY;
9871 		rjt_exp = LSEXP_NOTHING_MORE;
9872 		goto lsrjt;
9873 	}
9874 
9875 	switch (cmd) {
9876 	case ELS_CMD_PLOGI:
9877 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9878 			"RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
9879 			did, vport->port_state, ndlp->nlp_flag);
9880 
9881 		phba->fc_stat.elsRcvPLOGI++;
9882 		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
9883 		if (phba->sli_rev == LPFC_SLI_REV4 &&
9884 		    (phba->pport->fc_flag & FC_PT2PT)) {
9885 			vport->fc_prevDID = vport->fc_myDID;
9886 			/* Our DID needs to be updated before registering
9887 			 * the vfi. This is done in lpfc_rcv_plogi but
9888 			 * that is called after the reg_vfi.
9889 			 */
9890 			vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
9891 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9892 					 "3312 Remote port assigned DID x%x "
9893 					 "%x\n", vport->fc_myDID,
9894 					 vport->fc_prevDID);
9895 		}
9896 
9897 		lpfc_send_els_event(vport, ndlp, payload);
9898 
9899 		/* If Nport discovery is delayed, reject PLOGIs */
9900 		if (vport->fc_flag & FC_DISC_DELAYED) {
9901 			rjt_err = LSRJT_UNABLE_TPC;
9902 			rjt_exp = LSEXP_NOTHING_MORE;
9903 			break;
9904 		}
9905 
9906 		if (vport->port_state < LPFC_DISC_AUTH) {
9907 			if (!(phba->pport->fc_flag & FC_PT2PT) ||
9908 				(phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
9909 				rjt_err = LSRJT_UNABLE_TPC;
9910 				rjt_exp = LSEXP_NOTHING_MORE;
9911 				break;
9912 			}
9913 		}
9914 
9915 		spin_lock_irq(&ndlp->lock);
9916 		ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
9917 		spin_unlock_irq(&ndlp->lock);
9918 
9919 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
9920 					NLP_EVT_RCV_PLOGI);
9921 
9922 		break;
9923 	case ELS_CMD_FLOGI:
9924 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9925 			"RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
9926 			did, vport->port_state, ndlp->nlp_flag);
9927 
9928 		phba->fc_stat.elsRcvFLOGI++;
9929 
9930 		/* If the driver believes fabric discovery is done and is ready,
9931 		 * bounce the link.  There is some descrepancy.
9932 		 */
9933 		if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
9934 		    vport->fc_flag & FC_PT2PT &&
9935 		    vport->rcv_flogi_cnt >= 1) {
9936 			rjt_err = LSRJT_LOGICAL_BSY;
9937 			rjt_exp = LSEXP_NOTHING_MORE;
9938 			init_link++;
9939 			goto lsrjt;
9940 		}
9941 
9942 		lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
9943 		if (newnode)
9944 			lpfc_disc_state_machine(vport, ndlp, NULL,
9945 					NLP_EVT_DEVICE_RM);
9946 		break;
9947 	case ELS_CMD_LOGO:
9948 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9949 			"RCV LOGO:        did:x%x/ste:x%x flg:x%x",
9950 			did, vport->port_state, ndlp->nlp_flag);
9951 
9952 		phba->fc_stat.elsRcvLOGO++;
9953 		lpfc_send_els_event(vport, ndlp, payload);
9954 		if (vport->port_state < LPFC_DISC_AUTH) {
9955 			rjt_err = LSRJT_UNABLE_TPC;
9956 			rjt_exp = LSEXP_NOTHING_MORE;
9957 			break;
9958 		}
9959 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
9960 		if (newnode)
9961 			lpfc_disc_state_machine(vport, ndlp, NULL,
9962 					NLP_EVT_DEVICE_RM);
9963 		break;
9964 	case ELS_CMD_PRLO:
9965 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9966 			"RCV PRLO:        did:x%x/ste:x%x flg:x%x",
9967 			did, vport->port_state, ndlp->nlp_flag);
9968 
9969 		phba->fc_stat.elsRcvPRLO++;
9970 		lpfc_send_els_event(vport, ndlp, payload);
9971 		if (vport->port_state < LPFC_DISC_AUTH) {
9972 			rjt_err = LSRJT_UNABLE_TPC;
9973 			rjt_exp = LSEXP_NOTHING_MORE;
9974 			break;
9975 		}
9976 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
9977 		break;
9978 	case ELS_CMD_LCB:
9979 		phba->fc_stat.elsRcvLCB++;
9980 		lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
9981 		break;
9982 	case ELS_CMD_RDP:
9983 		phba->fc_stat.elsRcvRDP++;
9984 		lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
9985 		break;
9986 	case ELS_CMD_RSCN:
9987 		phba->fc_stat.elsRcvRSCN++;
9988 		lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
9989 		if (newnode)
9990 			lpfc_disc_state_machine(vport, ndlp, NULL,
9991 					NLP_EVT_DEVICE_RM);
9992 		break;
9993 	case ELS_CMD_ADISC:
9994 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9995 			"RCV ADISC:       did:x%x/ste:x%x flg:x%x",
9996 			did, vport->port_state, ndlp->nlp_flag);
9997 
9998 		lpfc_send_els_event(vport, ndlp, payload);
9999 		phba->fc_stat.elsRcvADISC++;
10000 		if (vport->port_state < LPFC_DISC_AUTH) {
10001 			rjt_err = LSRJT_UNABLE_TPC;
10002 			rjt_exp = LSEXP_NOTHING_MORE;
10003 			break;
10004 		}
10005 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
10006 					NLP_EVT_RCV_ADISC);
10007 		break;
10008 	case ELS_CMD_PDISC:
10009 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10010 			"RCV PDISC:       did:x%x/ste:x%x flg:x%x",
10011 			did, vport->port_state, ndlp->nlp_flag);
10012 
10013 		phba->fc_stat.elsRcvPDISC++;
10014 		if (vport->port_state < LPFC_DISC_AUTH) {
10015 			rjt_err = LSRJT_UNABLE_TPC;
10016 			rjt_exp = LSEXP_NOTHING_MORE;
10017 			break;
10018 		}
10019 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
10020 					NLP_EVT_RCV_PDISC);
10021 		break;
10022 	case ELS_CMD_FARPR:
10023 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10024 			"RCV FARPR:       did:x%x/ste:x%x flg:x%x",
10025 			did, vport->port_state, ndlp->nlp_flag);
10026 
10027 		phba->fc_stat.elsRcvFARPR++;
10028 		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
10029 		break;
10030 	case ELS_CMD_FARP:
10031 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10032 			"RCV FARP:        did:x%x/ste:x%x flg:x%x",
10033 			did, vport->port_state, ndlp->nlp_flag);
10034 
10035 		phba->fc_stat.elsRcvFARP++;
10036 		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
10037 		break;
10038 	case ELS_CMD_FAN:
10039 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10040 			"RCV FAN:         did:x%x/ste:x%x flg:x%x",
10041 			did, vport->port_state, ndlp->nlp_flag);
10042 
10043 		phba->fc_stat.elsRcvFAN++;
10044 		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
10045 		break;
10046 	case ELS_CMD_PRLI:
10047 	case ELS_CMD_NVMEPRLI:
10048 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10049 			"RCV PRLI:        did:x%x/ste:x%x flg:x%x",
10050 			did, vport->port_state, ndlp->nlp_flag);
10051 
10052 		phba->fc_stat.elsRcvPRLI++;
10053 		if ((vport->port_state < LPFC_DISC_AUTH) &&
10054 		    (vport->fc_flag & FC_FABRIC)) {
10055 			rjt_err = LSRJT_UNABLE_TPC;
10056 			rjt_exp = LSEXP_NOTHING_MORE;
10057 			break;
10058 		}
10059 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
10060 		break;
10061 	case ELS_CMD_LIRR:
10062 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10063 			"RCV LIRR:        did:x%x/ste:x%x flg:x%x",
10064 			did, vport->port_state, ndlp->nlp_flag);
10065 
10066 		phba->fc_stat.elsRcvLIRR++;
10067 		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
10068 		if (newnode)
10069 			lpfc_disc_state_machine(vport, ndlp, NULL,
10070 					NLP_EVT_DEVICE_RM);
10071 		break;
10072 	case ELS_CMD_RLS:
10073 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10074 			"RCV RLS:         did:x%x/ste:x%x flg:x%x",
10075 			did, vport->port_state, ndlp->nlp_flag);
10076 
10077 		phba->fc_stat.elsRcvRLS++;
10078 		lpfc_els_rcv_rls(vport, elsiocb, ndlp);
10079 		if (newnode)
10080 			lpfc_disc_state_machine(vport, ndlp, NULL,
10081 					NLP_EVT_DEVICE_RM);
10082 		break;
10083 	case ELS_CMD_RPL:
10084 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10085 			"RCV RPL:         did:x%x/ste:x%x flg:x%x",
10086 			did, vport->port_state, ndlp->nlp_flag);
10087 
10088 		phba->fc_stat.elsRcvRPL++;
10089 		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
10090 		if (newnode)
10091 			lpfc_disc_state_machine(vport, ndlp, NULL,
10092 					NLP_EVT_DEVICE_RM);
10093 		break;
10094 	case ELS_CMD_RNID:
10095 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10096 			"RCV RNID:        did:x%x/ste:x%x flg:x%x",
10097 			did, vport->port_state, ndlp->nlp_flag);
10098 
10099 		phba->fc_stat.elsRcvRNID++;
10100 		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
10101 		if (newnode)
10102 			lpfc_disc_state_machine(vport, ndlp, NULL,
10103 					NLP_EVT_DEVICE_RM);
10104 		break;
10105 	case ELS_CMD_RTV:
10106 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10107 			"RCV RTV:        did:x%x/ste:x%x flg:x%x",
10108 			did, vport->port_state, ndlp->nlp_flag);
10109 		phba->fc_stat.elsRcvRTV++;
10110 		lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
10111 		if (newnode)
10112 			lpfc_disc_state_machine(vport, ndlp, NULL,
10113 					NLP_EVT_DEVICE_RM);
10114 		break;
10115 	case ELS_CMD_RRQ:
10116 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10117 			"RCV RRQ:         did:x%x/ste:x%x flg:x%x",
10118 			did, vport->port_state, ndlp->nlp_flag);
10119 
10120 		phba->fc_stat.elsRcvRRQ++;
10121 		lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
10122 		if (newnode)
10123 			lpfc_disc_state_machine(vport, ndlp, NULL,
10124 					NLP_EVT_DEVICE_RM);
10125 		break;
10126 	case ELS_CMD_ECHO:
10127 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10128 			"RCV ECHO:        did:x%x/ste:x%x flg:x%x",
10129 			did, vport->port_state, ndlp->nlp_flag);
10130 
10131 		phba->fc_stat.elsRcvECHO++;
10132 		lpfc_els_rcv_echo(vport, elsiocb, ndlp);
10133 		if (newnode)
10134 			lpfc_disc_state_machine(vport, ndlp, NULL,
10135 					NLP_EVT_DEVICE_RM);
10136 		break;
10137 	case ELS_CMD_REC:
10138 		/* receive this due to exchange closed */
10139 		rjt_err = LSRJT_UNABLE_TPC;
10140 		rjt_exp = LSEXP_INVALID_OX_RX;
10141 		break;
10142 	case ELS_CMD_FPIN:
10143 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10144 				      "RCV FPIN:       did:x%x/ste:x%x flg:x%x",
10145 				      did, vport->port_state, ndlp->nlp_flag);
10146 
10147 		lpfc_els_rcv_fpin(vport, (struct fc_els_fpin *)payload,
10148 				  payload_len);
10149 
10150 		/* There are no replies, so no rjt codes */
10151 		break;
10152 	case ELS_CMD_EDC:
10153 		lpfc_els_rcv_edc(vport, elsiocb, ndlp);
10154 		break;
10155 	case ELS_CMD_RDF:
10156 		phba->fc_stat.elsRcvRDF++;
10157 		/* Accept RDF only from fabric controller */
10158 		if (did != Fabric_Cntl_DID) {
10159 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
10160 					 "1115 Received RDF from invalid DID "
10161 					 "x%x\n", did);
10162 			rjt_err = LSRJT_PROTOCOL_ERR;
10163 			rjt_exp = LSEXP_NOTHING_MORE;
10164 			goto lsrjt;
10165 		}
10166 
10167 		lpfc_els_rcv_rdf(vport, elsiocb, ndlp);
10168 		break;
10169 	default:
10170 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10171 			"RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
10172 			cmd, did, vport->port_state);
10173 
10174 		/* Unsupported ELS command, reject */
10175 		rjt_err = LSRJT_CMD_UNSUPPORTED;
10176 		rjt_exp = LSEXP_NOTHING_MORE;
10177 
10178 		/* Unknown ELS command <elsCmd> received from NPORT <did> */
10179 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10180 				 "0115 Unknown ELS command x%x "
10181 				 "received from NPORT x%x\n", cmd, did);
10182 		if (newnode)
10183 			lpfc_disc_state_machine(vport, ndlp, NULL,
10184 					NLP_EVT_DEVICE_RM);
10185 		break;
10186 	}
10187 
10188 lsrjt:
10189 	/* check if need to LS_RJT received ELS cmd */
10190 	if (rjt_err) {
10191 		memset(&stat, 0, sizeof(stat));
10192 		stat.un.b.lsRjtRsnCode = rjt_err;
10193 		stat.un.b.lsRjtRsnCodeExp = rjt_exp;
10194 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
10195 				    NULL);
10196 		/* Remove the reference from above for new nodes. */
10197 		if (newnode)
10198 			lpfc_disc_state_machine(vport, ndlp, NULL,
10199 					NLP_EVT_DEVICE_RM);
10200 	}
10201 
10202 	/* Release the reference on this elsiocb, not the ndlp. */
10203 	lpfc_nlp_put(elsiocb->context1);
10204 	elsiocb->context1 = NULL;
10205 
10206 	/* Special case.  Driver received an unsolicited command that
10207 	 * unsupportable given the driver's current state.  Reset the
10208 	 * link and start over.
10209 	 */
10210 	if (init_link) {
10211 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10212 		if (!mbox)
10213 			return;
10214 		lpfc_linkdown(phba);
10215 		lpfc_init_link(phba, mbox,
10216 			       phba->cfg_topology,
10217 			       phba->cfg_link_speed);
10218 		mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
10219 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10220 		mbox->vport = vport;
10221 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
10222 		    MBX_NOT_FINISHED)
10223 			mempool_free(mbox, phba->mbox_mem_pool);
10224 	}
10225 
10226 	return;
10227 
10228 dropit:
10229 	if (vport && !(vport->load_flag & FC_UNLOADING))
10230 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10231 			"0111 Dropping received ELS cmd "
10232 			"Data: x%x x%x x%x\n",
10233 			icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
10234 	phba->fc_stat.elsRcvDrop++;
10235 }
10236 
10237 /**
10238  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
10239  * @phba: pointer to lpfc hba data structure.
10240  * @pring: pointer to a SLI ring.
10241  * @elsiocb: pointer to lpfc els iocb data structure.
10242  *
10243  * This routine is used to process an unsolicited event received from a SLI
10244  * (Service Level Interface) ring. The actual processing of the data buffer
10245  * associated with the unsolicited event is done by invoking the routine
10246  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
10247  * SLI ring on which the unsolicited event was received.
10248  **/
10249 void
10250 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10251 		     struct lpfc_iocbq *elsiocb)
10252 {
10253 	struct lpfc_vport *vport = phba->pport;
10254 	IOCB_t *icmd = &elsiocb->iocb;
10255 	dma_addr_t paddr;
10256 	struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
10257 	struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
10258 
10259 	elsiocb->context1 = NULL;
10260 	elsiocb->context2 = NULL;
10261 	elsiocb->context3 = NULL;
10262 
10263 	if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
10264 		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
10265 	} else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
10266 		   (icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
10267 		   IOERR_RCV_BUFFER_WAITING) {
10268 		phba->fc_stat.NoRcvBuf++;
10269 		/* Not enough posted buffers; Try posting more buffers */
10270 		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
10271 			lpfc_post_buffer(phba, pring, 0);
10272 		return;
10273 	}
10274 
10275 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
10276 	    (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
10277 	     icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
10278 		if (icmd->unsli3.rcvsli3.vpi == 0xffff)
10279 			vport = phba->pport;
10280 		else
10281 			vport = lpfc_find_vport_by_vpid(phba,
10282 						icmd->unsli3.rcvsli3.vpi);
10283 	}
10284 
10285 	/* If there are no BDEs associated
10286 	 * with this IOCB, there is nothing to do.
10287 	 */
10288 	if (icmd->ulpBdeCount == 0)
10289 		return;
10290 
10291 	/* type of ELS cmd is first 32bit word
10292 	 * in packet
10293 	 */
10294 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
10295 		elsiocb->context2 = bdeBuf1;
10296 	} else {
10297 		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
10298 				 icmd->un.cont64[0].addrLow);
10299 		elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
10300 							     paddr);
10301 	}
10302 
10303 	lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
10304 	/*
10305 	 * The different unsolicited event handlers would tell us
10306 	 * if they are done with "mp" by setting context2 to NULL.
10307 	 */
10308 	if (elsiocb->context2) {
10309 		lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
10310 		elsiocb->context2 = NULL;
10311 	}
10312 
10313 	/* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
10314 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
10315 	    icmd->ulpBdeCount == 2) {
10316 		elsiocb->context2 = bdeBuf2;
10317 		lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
10318 		/* free mp if we are done with it */
10319 		if (elsiocb->context2) {
10320 			lpfc_in_buf_free(phba, elsiocb->context2);
10321 			elsiocb->context2 = NULL;
10322 		}
10323 	}
10324 }
10325 
10326 static void
10327 lpfc_start_fdmi(struct lpfc_vport *vport)
10328 {
10329 	struct lpfc_nodelist *ndlp;
10330 
10331 	/* If this is the first time, allocate an ndlp and initialize
10332 	 * it. Otherwise, make sure the node is enabled and then do the
10333 	 * login.
10334 	 */
10335 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
10336 	if (!ndlp) {
10337 		ndlp = lpfc_nlp_init(vport, FDMI_DID);
10338 		if (ndlp) {
10339 			ndlp->nlp_type |= NLP_FABRIC;
10340 		} else {
10341 			return;
10342 		}
10343 	}
10344 
10345 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
10346 	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
10347 }
10348 
10349 /**
10350  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
10351  * @phba: pointer to lpfc hba data structure.
10352  * @vport: pointer to a virtual N_Port data structure.
10353  *
10354  * This routine issues a Port Login (PLOGI) to the Name Server with
10355  * State Change Request (SCR) for a @vport. This routine will create an
10356  * ndlp for the Name Server associated to the @vport if such node does
10357  * not already exist. The PLOGI to Name Server is issued by invoking the
10358  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
10359  * (FDMI) is configured to the @vport, a FDMI node will be created and
10360  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
10361  **/
10362 void
10363 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
10364 {
10365 	struct lpfc_nodelist *ndlp;
10366 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10367 
10368 	/*
10369 	 * If lpfc_delay_discovery parameter is set and the clean address
10370 	 * bit is cleared and fc fabric parameters chenged, delay FC NPort
10371 	 * discovery.
10372 	 */
10373 	spin_lock_irq(shost->host_lock);
10374 	if (vport->fc_flag & FC_DISC_DELAYED) {
10375 		spin_unlock_irq(shost->host_lock);
10376 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10377 				 "3334 Delay fc port discovery for %d secs\n",
10378 				 phba->fc_ratov);
10379 		mod_timer(&vport->delayed_disc_tmo,
10380 			jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
10381 		return;
10382 	}
10383 	spin_unlock_irq(shost->host_lock);
10384 
10385 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
10386 	if (!ndlp) {
10387 		ndlp = lpfc_nlp_init(vport, NameServer_DID);
10388 		if (!ndlp) {
10389 			if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
10390 				lpfc_disc_start(vport);
10391 				return;
10392 			}
10393 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10394 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10395 					 "0251 NameServer login: no memory\n");
10396 			return;
10397 		}
10398 	}
10399 
10400 	ndlp->nlp_type |= NLP_FABRIC;
10401 
10402 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
10403 
10404 	if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
10405 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10406 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10407 				 "0252 Cannot issue NameServer login\n");
10408 		return;
10409 	}
10410 
10411 	if ((phba->cfg_enable_SmartSAN ||
10412 	     (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) &&
10413 	     (vport->load_flag & FC_ALLOW_FDMI))
10414 		lpfc_start_fdmi(vport);
10415 }
10416 
10417 /**
10418  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
10419  * @phba: pointer to lpfc hba data structure.
10420  * @pmb: pointer to the driver internal queue element for mailbox command.
10421  *
10422  * This routine is the completion callback function to register new vport
10423  * mailbox command. If the new vport mailbox command completes successfully,
10424  * the fabric registration login shall be performed on physical port (the
10425  * new vport created is actually a physical port, with VPI 0) or the port
10426  * login to Name Server for State Change Request (SCR) will be performed
10427  * on virtual port (real virtual port, with VPI greater than 0).
10428  **/
10429 static void
10430 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
10431 {
10432 	struct lpfc_vport *vport = pmb->vport;
10433 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10434 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
10435 	MAILBOX_t *mb = &pmb->u.mb;
10436 	int rc;
10437 
10438 	spin_lock_irq(shost->host_lock);
10439 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
10440 	spin_unlock_irq(shost->host_lock);
10441 
10442 	if (mb->mbxStatus) {
10443 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10444 				"0915 Register VPI failed : Status: x%x"
10445 				" upd bit: x%x \n", mb->mbxStatus,
10446 				 mb->un.varRegVpi.upd);
10447 		if (phba->sli_rev == LPFC_SLI_REV4 &&
10448 			mb->un.varRegVpi.upd)
10449 			goto mbox_err_exit ;
10450 
10451 		switch (mb->mbxStatus) {
10452 		case 0x11:	/* unsupported feature */
10453 		case 0x9603:	/* max_vpi exceeded */
10454 		case 0x9602:	/* Link event since CLEAR_LA */
10455 			/* giving up on vport registration */
10456 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10457 			spin_lock_irq(shost->host_lock);
10458 			vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
10459 			spin_unlock_irq(shost->host_lock);
10460 			lpfc_can_disctmo(vport);
10461 			break;
10462 		/* If reg_vpi fail with invalid VPI status, re-init VPI */
10463 		case 0x20:
10464 			spin_lock_irq(shost->host_lock);
10465 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
10466 			spin_unlock_irq(shost->host_lock);
10467 			lpfc_init_vpi(phba, pmb, vport->vpi);
10468 			pmb->vport = vport;
10469 			pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
10470 			rc = lpfc_sli_issue_mbox(phba, pmb,
10471 				MBX_NOWAIT);
10472 			if (rc == MBX_NOT_FINISHED) {
10473 				lpfc_printf_vlog(vport, KERN_ERR,
10474 						 LOG_TRACE_EVENT,
10475 					"2732 Failed to issue INIT_VPI"
10476 					" mailbox command\n");
10477 			} else {
10478 				lpfc_nlp_put(ndlp);
10479 				return;
10480 			}
10481 			fallthrough;
10482 		default:
10483 			/* Try to recover from this error */
10484 			if (phba->sli_rev == LPFC_SLI_REV4)
10485 				lpfc_sli4_unreg_all_rpis(vport);
10486 			lpfc_mbx_unreg_vpi(vport);
10487 			spin_lock_irq(shost->host_lock);
10488 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
10489 			spin_unlock_irq(shost->host_lock);
10490 			if (mb->mbxStatus == MBX_NOT_FINISHED)
10491 				break;
10492 			if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
10493 			    !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG)) {
10494 				if (phba->sli_rev == LPFC_SLI_REV4)
10495 					lpfc_issue_init_vfi(vport);
10496 				else
10497 					lpfc_initial_flogi(vport);
10498 			} else {
10499 				lpfc_initial_fdisc(vport);
10500 			}
10501 			break;
10502 		}
10503 	} else {
10504 		spin_lock_irq(shost->host_lock);
10505 		vport->vpi_state |= LPFC_VPI_REGISTERED;
10506 		spin_unlock_irq(shost->host_lock);
10507 		if (vport == phba->pport) {
10508 			if (phba->sli_rev < LPFC_SLI_REV4)
10509 				lpfc_issue_fabric_reglogin(vport);
10510 			else {
10511 				/*
10512 				 * If the physical port is instantiated using
10513 				 * FDISC, do not start vport discovery.
10514 				 */
10515 				if (vport->port_state != LPFC_FDISC)
10516 					lpfc_start_fdiscs(phba);
10517 				lpfc_do_scr_ns_plogi(phba, vport);
10518 			}
10519 		} else {
10520 			lpfc_do_scr_ns_plogi(phba, vport);
10521 		}
10522 	}
10523 mbox_err_exit:
10524 	/* Now, we decrement the ndlp reference count held for this
10525 	 * callback function
10526 	 */
10527 	lpfc_nlp_put(ndlp);
10528 
10529 	mempool_free(pmb, phba->mbox_mem_pool);
10530 	return;
10531 }
10532 
10533 /**
10534  * lpfc_register_new_vport - Register a new vport with a HBA
10535  * @phba: pointer to lpfc hba data structure.
10536  * @vport: pointer to a host virtual N_Port data structure.
10537  * @ndlp: pointer to a node-list data structure.
10538  *
10539  * This routine registers the @vport as a new virtual port with a HBA.
10540  * It is done through a registering vpi mailbox command.
10541  **/
10542 void
10543 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
10544 			struct lpfc_nodelist *ndlp)
10545 {
10546 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10547 	LPFC_MBOXQ_t *mbox;
10548 
10549 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10550 	if (mbox) {
10551 		lpfc_reg_vpi(vport, mbox);
10552 		mbox->vport = vport;
10553 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
10554 		if (!mbox->ctx_ndlp) {
10555 			mempool_free(mbox, phba->mbox_mem_pool);
10556 			goto mbox_err_exit;
10557 		}
10558 
10559 		mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
10560 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
10561 		    == MBX_NOT_FINISHED) {
10562 			/* mailbox command not success, decrement ndlp
10563 			 * reference count for this command
10564 			 */
10565 			lpfc_nlp_put(ndlp);
10566 			mempool_free(mbox, phba->mbox_mem_pool);
10567 
10568 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10569 				"0253 Register VPI: Can't send mbox\n");
10570 			goto mbox_err_exit;
10571 		}
10572 	} else {
10573 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10574 				 "0254 Register VPI: no memory\n");
10575 		goto mbox_err_exit;
10576 	}
10577 	return;
10578 
10579 mbox_err_exit:
10580 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10581 	spin_lock_irq(shost->host_lock);
10582 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
10583 	spin_unlock_irq(shost->host_lock);
10584 	return;
10585 }
10586 
10587 /**
10588  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
10589  * @phba: pointer to lpfc hba data structure.
10590  *
10591  * This routine cancels the retry delay timers to all the vports.
10592  **/
10593 void
10594 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
10595 {
10596 	struct lpfc_vport **vports;
10597 	struct lpfc_nodelist *ndlp;
10598 	uint32_t link_state;
10599 	int i;
10600 
10601 	/* Treat this failure as linkdown for all vports */
10602 	link_state = phba->link_state;
10603 	lpfc_linkdown(phba);
10604 	phba->link_state = link_state;
10605 
10606 	vports = lpfc_create_vport_work_array(phba);
10607 
10608 	if (vports) {
10609 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10610 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
10611 			if (ndlp)
10612 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
10613 			lpfc_els_flush_cmd(vports[i]);
10614 		}
10615 		lpfc_destroy_vport_work_array(phba, vports);
10616 	}
10617 }
10618 
10619 /**
10620  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
10621  * @phba: pointer to lpfc hba data structure.
10622  *
10623  * This routine abort all pending discovery commands and
10624  * start a timer to retry FLOGI for the physical port
10625  * discovery.
10626  **/
10627 void
10628 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
10629 {
10630 	struct lpfc_nodelist *ndlp;
10631 
10632 	/* Cancel the all vports retry delay retry timers */
10633 	lpfc_cancel_all_vport_retry_delay_timer(phba);
10634 
10635 	/* If fabric require FLOGI, then re-instantiate physical login */
10636 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
10637 	if (!ndlp)
10638 		return;
10639 
10640 	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
10641 	spin_lock_irq(&ndlp->lock);
10642 	ndlp->nlp_flag |= NLP_DELAY_TMO;
10643 	spin_unlock_irq(&ndlp->lock);
10644 	ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
10645 	phba->pport->port_state = LPFC_FLOGI;
10646 	return;
10647 }
10648 
10649 /**
10650  * lpfc_fabric_login_reqd - Check if FLOGI required.
10651  * @phba: pointer to lpfc hba data structure.
10652  * @cmdiocb: pointer to FDISC command iocb.
10653  * @rspiocb: pointer to FDISC response iocb.
10654  *
10655  * This routine checks if a FLOGI is reguired for FDISC
10656  * to succeed.
10657  **/
10658 static int
10659 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
10660 		struct lpfc_iocbq *cmdiocb,
10661 		struct lpfc_iocbq *rspiocb)
10662 {
10663 
10664 	if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
10665 		(rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
10666 		return 0;
10667 	else
10668 		return 1;
10669 }
10670 
10671 /**
10672  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
10673  * @phba: pointer to lpfc hba data structure.
10674  * @cmdiocb: pointer to lpfc command iocb data structure.
10675  * @rspiocb: pointer to lpfc response iocb data structure.
10676  *
10677  * This routine is the completion callback function to a Fabric Discover
10678  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
10679  * single threaded, each FDISC completion callback function will reset
10680  * the discovery timer for all vports such that the timers will not get
10681  * unnecessary timeout. The function checks the FDISC IOCB status. If error
10682  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
10683  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
10684  * assigned to the vport has been changed with the completion of the FDISC
10685  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
10686  * are unregistered from the HBA, and then the lpfc_register_new_vport()
10687  * routine is invoked to register new vport with the HBA. Otherwise, the
10688  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
10689  * Server for State Change Request (SCR).
10690  **/
10691 static void
10692 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10693 		    struct lpfc_iocbq *rspiocb)
10694 {
10695 	struct lpfc_vport *vport = cmdiocb->vport;
10696 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10697 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
10698 	struct lpfc_nodelist *np;
10699 	struct lpfc_nodelist *next_np;
10700 	IOCB_t *irsp = &rspiocb->iocb;
10701 	struct lpfc_iocbq *piocb;
10702 	struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
10703 	struct serv_parm *sp;
10704 	uint8_t fabric_param_changed;
10705 
10706 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10707 			 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
10708 			 irsp->ulpStatus, irsp->un.ulpWord[4],
10709 			 vport->fc_prevDID);
10710 	/* Since all FDISCs are being single threaded, we
10711 	 * must reset the discovery timer for ALL vports
10712 	 * waiting to send FDISC when one completes.
10713 	 */
10714 	list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
10715 		lpfc_set_disctmo(piocb->vport);
10716 	}
10717 
10718 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
10719 		"FDISC cmpl:      status:x%x/x%x prevdid:x%x",
10720 		irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
10721 
10722 	if (irsp->ulpStatus) {
10723 
10724 		if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
10725 			lpfc_retry_pport_discovery(phba);
10726 			goto out;
10727 		}
10728 
10729 		/* Check for retry */
10730 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
10731 			goto out;
10732 		/* FDISC failed */
10733 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10734 				 "0126 FDISC failed. (x%x/x%x)\n",
10735 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
10736 		goto fdisc_failed;
10737 	}
10738 
10739 	lpfc_check_nlp_post_devloss(vport, ndlp);
10740 
10741 	spin_lock_irq(shost->host_lock);
10742 	vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
10743 	vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
10744 	vport->fc_flag |= FC_FABRIC;
10745 	if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
10746 		vport->fc_flag |=  FC_PUBLIC_LOOP;
10747 	spin_unlock_irq(shost->host_lock);
10748 
10749 	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
10750 	lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
10751 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
10752 	if (!prsp)
10753 		goto out;
10754 	sp = prsp->virt + sizeof(uint32_t);
10755 	fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
10756 	memcpy(&vport->fabric_portname, &sp->portName,
10757 		sizeof(struct lpfc_name));
10758 	memcpy(&vport->fabric_nodename, &sp->nodeName,
10759 		sizeof(struct lpfc_name));
10760 	if (fabric_param_changed &&
10761 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
10762 		/* If our NportID changed, we need to ensure all
10763 		 * remaining NPORTs get unreg_login'ed so we can
10764 		 * issue unreg_vpi.
10765 		 */
10766 		list_for_each_entry_safe(np, next_np,
10767 			&vport->fc_nodes, nlp_listp) {
10768 			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
10769 			    !(np->nlp_flag & NLP_NPR_ADISC))
10770 				continue;
10771 			spin_lock_irq(&ndlp->lock);
10772 			np->nlp_flag &= ~NLP_NPR_ADISC;
10773 			spin_unlock_irq(&ndlp->lock);
10774 			lpfc_unreg_rpi(vport, np);
10775 		}
10776 		lpfc_cleanup_pending_mbox(vport);
10777 
10778 		if (phba->sli_rev == LPFC_SLI_REV4)
10779 			lpfc_sli4_unreg_all_rpis(vport);
10780 
10781 		lpfc_mbx_unreg_vpi(vport);
10782 		spin_lock_irq(shost->host_lock);
10783 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
10784 		if (phba->sli_rev == LPFC_SLI_REV4)
10785 			vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
10786 		else
10787 			vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
10788 		spin_unlock_irq(shost->host_lock);
10789 	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
10790 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
10791 		/*
10792 		 * Driver needs to re-reg VPI in order for f/w
10793 		 * to update the MAC address.
10794 		 */
10795 		lpfc_register_new_vport(phba, vport, ndlp);
10796 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
10797 		goto out;
10798 	}
10799 
10800 	if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
10801 		lpfc_issue_init_vpi(vport);
10802 	else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
10803 		lpfc_register_new_vport(phba, vport, ndlp);
10804 	else
10805 		lpfc_do_scr_ns_plogi(phba, vport);
10806 
10807 	/* The FDISC completed successfully. Move the fabric ndlp to
10808 	 * UNMAPPED state and register with the transport.
10809 	 */
10810 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
10811 	goto out;
10812 
10813 fdisc_failed:
10814 	if (vport->fc_vport &&
10815 	    (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
10816 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10817 	/* Cancel discovery timer */
10818 	lpfc_can_disctmo(vport);
10819 out:
10820 	lpfc_els_free_iocb(phba, cmdiocb);
10821 	lpfc_nlp_put(ndlp);
10822 }
10823 
10824 /**
10825  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
10826  * @vport: pointer to a virtual N_Port data structure.
10827  * @ndlp: pointer to a node-list data structure.
10828  * @retry: number of retries to the command IOCB.
10829  *
10830  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
10831  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
10832  * routine to issue the IOCB, which makes sure only one outstanding fabric
10833  * IOCB will be sent off HBA at any given time.
10834  *
10835  * Note that the ndlp reference count will be incremented by 1 for holding the
10836  * ndlp and the reference to ndlp will be stored into the context1 field of
10837  * the IOCB for the completion callback function to the FDISC ELS command.
10838  *
10839  * Return code
10840  *   0 - Successfully issued fdisc iocb command
10841  *   1 - Failed to issue fdisc iocb command
10842  **/
10843 static int
10844 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
10845 		     uint8_t retry)
10846 {
10847 	struct lpfc_hba *phba = vport->phba;
10848 	IOCB_t *icmd;
10849 	struct lpfc_iocbq *elsiocb;
10850 	struct serv_parm *sp;
10851 	uint8_t *pcmd;
10852 	uint16_t cmdsize;
10853 	int did = ndlp->nlp_DID;
10854 	int rc;
10855 
10856 	vport->port_state = LPFC_FDISC;
10857 	vport->fc_myDID = 0;
10858 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
10859 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
10860 				     ELS_CMD_FDISC);
10861 	if (!elsiocb) {
10862 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10863 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10864 				 "0255 Issue FDISC: no IOCB\n");
10865 		return 1;
10866 	}
10867 
10868 	icmd = &elsiocb->iocb;
10869 	icmd->un.elsreq64.myID = 0;
10870 	icmd->un.elsreq64.fl = 1;
10871 
10872 	/*
10873 	 * SLI3 ports require a different context type value than SLI4.
10874 	 * Catch SLI3 ports here and override the prep.
10875 	 */
10876 	if (phba->sli_rev == LPFC_SLI_REV3) {
10877 		icmd->ulpCt_h = 1;
10878 		icmd->ulpCt_l = 0;
10879 	}
10880 
10881 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
10882 	*((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
10883 	pcmd += sizeof(uint32_t); /* CSP Word 1 */
10884 	memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
10885 	sp = (struct serv_parm *) pcmd;
10886 	/* Setup CSPs accordingly for Fabric */
10887 	sp->cmn.e_d_tov = 0;
10888 	sp->cmn.w2.r_a_tov = 0;
10889 	sp->cmn.virtual_fabric_support = 0;
10890 	sp->cls1.classValid = 0;
10891 	sp->cls2.seqDelivery = 1;
10892 	sp->cls3.seqDelivery = 1;
10893 
10894 	pcmd += sizeof(uint32_t); /* CSP Word 2 */
10895 	pcmd += sizeof(uint32_t); /* CSP Word 3 */
10896 	pcmd += sizeof(uint32_t); /* CSP Word 4 */
10897 	pcmd += sizeof(uint32_t); /* Port Name */
10898 	memcpy(pcmd, &vport->fc_portname, 8);
10899 	pcmd += sizeof(uint32_t); /* Node Name */
10900 	pcmd += sizeof(uint32_t); /* Node Name */
10901 	memcpy(pcmd, &vport->fc_nodename, 8);
10902 	sp->cmn.valid_vendor_ver_level = 0;
10903 	memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
10904 	lpfc_set_disctmo(vport);
10905 
10906 	phba->fc_stat.elsXmitFDISC++;
10907 	elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
10908 
10909 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
10910 		"Issue FDISC:     did:x%x",
10911 		did, 0, 0);
10912 
10913 	elsiocb->context1 = lpfc_nlp_get(ndlp);
10914 	if (!elsiocb->context1) {
10915 		lpfc_els_free_iocb(phba, elsiocb);
10916 		goto err_out;
10917 	}
10918 
10919 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
10920 	if (rc == IOCB_ERROR) {
10921 		lpfc_els_free_iocb(phba, elsiocb);
10922 		lpfc_nlp_put(ndlp);
10923 		goto err_out;
10924 	}
10925 
10926 	lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
10927 	return 0;
10928 
10929  err_out:
10930 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10931 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10932 			 "0256 Issue FDISC: Cannot send IOCB\n");
10933 	return 1;
10934 }
10935 
10936 /**
10937  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
10938  * @phba: pointer to lpfc hba data structure.
10939  * @cmdiocb: pointer to lpfc command iocb data structure.
10940  * @rspiocb: pointer to lpfc response iocb data structure.
10941  *
10942  * This routine is the completion callback function to the issuing of a LOGO
10943  * ELS command off a vport. It frees the command IOCB and then decrement the
10944  * reference count held on ndlp for this completion function, indicating that
10945  * the reference to the ndlp is no long needed. Note that the
10946  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
10947  * callback function and an additional explicit ndlp reference decrementation
10948  * will trigger the actual release of the ndlp.
10949  **/
10950 static void
10951 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10952 			struct lpfc_iocbq *rspiocb)
10953 {
10954 	struct lpfc_vport *vport = cmdiocb->vport;
10955 	IOCB_t *irsp;
10956 	struct lpfc_nodelist *ndlp;
10957 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10958 
10959 	ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
10960 	irsp = &rspiocb->iocb;
10961 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
10962 		"LOGO npiv cmpl:  status:x%x/x%x did:x%x",
10963 		irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
10964 
10965 	/* NPIV LOGO completes to NPort <nlp_DID> */
10966 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10967 			 "2928 NPIV LOGO completes to NPort x%x "
10968 			 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
10969 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
10970 			 irsp->ulpTimeout, vport->num_disc_nodes,
10971 			 kref_read(&ndlp->kref), ndlp->nlp_flag,
10972 			 ndlp->fc4_xpt_flags);
10973 
10974 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
10975 		spin_lock_irq(shost->host_lock);
10976 		vport->fc_flag &= ~FC_NDISC_ACTIVE;
10977 		vport->fc_flag &= ~FC_FABRIC;
10978 		spin_unlock_irq(shost->host_lock);
10979 		lpfc_can_disctmo(vport);
10980 	}
10981 
10982 	/* Safe to release resources now. */
10983 	lpfc_els_free_iocb(phba, cmdiocb);
10984 	lpfc_nlp_put(ndlp);
10985 	vport->unreg_vpi_cmpl = VPORT_ERROR;
10986 }
10987 
10988 /**
10989  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
10990  * @vport: pointer to a virtual N_Port data structure.
10991  * @ndlp: pointer to a node-list data structure.
10992  *
10993  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
10994  *
10995  * Note that the ndlp reference count will be incremented by 1 for holding the
10996  * ndlp and the reference to ndlp will be stored into the context1 field of
10997  * the IOCB for the completion callback function to the LOGO ELS command.
10998  *
10999  * Return codes
11000  *   0 - Successfully issued logo off the @vport
11001  *   1 - Failed to issue logo off the @vport
11002  **/
11003 int
11004 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
11005 {
11006 	int rc = 0;
11007 	struct lpfc_hba  *phba = vport->phba;
11008 	struct lpfc_iocbq *elsiocb;
11009 	uint8_t *pcmd;
11010 	uint16_t cmdsize;
11011 
11012 	cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
11013 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
11014 				     ELS_CMD_LOGO);
11015 	if (!elsiocb)
11016 		return 1;
11017 
11018 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
11019 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
11020 	pcmd += sizeof(uint32_t);
11021 
11022 	/* Fill in LOGO payload */
11023 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
11024 	pcmd += sizeof(uint32_t);
11025 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
11026 
11027 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11028 		"Issue LOGO npiv  did:x%x flg:x%x",
11029 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
11030 
11031 	elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
11032 	spin_lock_irq(&ndlp->lock);
11033 	ndlp->nlp_flag |= NLP_LOGO_SND;
11034 	spin_unlock_irq(&ndlp->lock);
11035 	elsiocb->context1 = lpfc_nlp_get(ndlp);
11036 	if (!elsiocb->context1) {
11037 		lpfc_els_free_iocb(phba, elsiocb);
11038 		goto err;
11039 	}
11040 
11041 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
11042 	if (rc == IOCB_ERROR) {
11043 		lpfc_els_free_iocb(phba, elsiocb);
11044 		lpfc_nlp_put(ndlp);
11045 		goto err;
11046 	}
11047 	return 0;
11048 
11049 err:
11050 	spin_lock_irq(&ndlp->lock);
11051 	ndlp->nlp_flag &= ~NLP_LOGO_SND;
11052 	spin_unlock_irq(&ndlp->lock);
11053 	return 1;
11054 }
11055 
11056 /**
11057  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
11058  * @t: timer context used to obtain the lpfc hba.
11059  *
11060  * This routine is invoked by the fabric iocb block timer after
11061  * timeout. It posts the fabric iocb block timeout event by setting the
11062  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
11063  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
11064  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
11065  * posted event WORKER_FABRIC_BLOCK_TMO.
11066  **/
11067 void
11068 lpfc_fabric_block_timeout(struct timer_list *t)
11069 {
11070 	struct lpfc_hba  *phba = from_timer(phba, t, fabric_block_timer);
11071 	unsigned long iflags;
11072 	uint32_t tmo_posted;
11073 
11074 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11075 	tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
11076 	if (!tmo_posted)
11077 		phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
11078 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11079 
11080 	if (!tmo_posted)
11081 		lpfc_worker_wake_up(phba);
11082 	return;
11083 }
11084 
11085 /**
11086  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
11087  * @phba: pointer to lpfc hba data structure.
11088  *
11089  * This routine issues one fabric iocb from the driver internal list to
11090  * the HBA. It first checks whether it's ready to issue one fabric iocb to
11091  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
11092  * remove one pending fabric iocb from the driver internal list and invokes
11093  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
11094  **/
11095 static void
11096 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
11097 {
11098 	struct lpfc_iocbq *iocb;
11099 	unsigned long iflags;
11100 	int ret;
11101 	IOCB_t *cmd;
11102 
11103 repeat:
11104 	iocb = NULL;
11105 	spin_lock_irqsave(&phba->hbalock, iflags);
11106 	/* Post any pending iocb to the SLI layer */
11107 	if (atomic_read(&phba->fabric_iocb_count) == 0) {
11108 		list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
11109 				 list);
11110 		if (iocb)
11111 			/* Increment fabric iocb count to hold the position */
11112 			atomic_inc(&phba->fabric_iocb_count);
11113 	}
11114 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11115 	if (iocb) {
11116 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
11117 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
11118 		iocb->iocb_flag |= LPFC_IO_FABRIC;
11119 
11120 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11121 			"Fabric sched1:   ste:x%x",
11122 			iocb->vport->port_state, 0, 0);
11123 
11124 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11125 
11126 		if (ret == IOCB_ERROR) {
11127 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
11128 			iocb->fabric_iocb_cmpl = NULL;
11129 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
11130 			cmd = &iocb->iocb;
11131 			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
11132 			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
11133 			iocb->iocb_cmpl(phba, iocb, iocb);
11134 
11135 			atomic_dec(&phba->fabric_iocb_count);
11136 			goto repeat;
11137 		}
11138 	}
11139 }
11140 
11141 /**
11142  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
11143  * @phba: pointer to lpfc hba data structure.
11144  *
11145  * This routine unblocks the  issuing fabric iocb command. The function
11146  * will clear the fabric iocb block bit and then invoke the routine
11147  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
11148  * from the driver internal fabric iocb list.
11149  **/
11150 void
11151 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
11152 {
11153 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11154 
11155 	lpfc_resume_fabric_iocbs(phba);
11156 	return;
11157 }
11158 
11159 /**
11160  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
11161  * @phba: pointer to lpfc hba data structure.
11162  *
11163  * This routine blocks the issuing fabric iocb for a specified amount of
11164  * time (currently 100 ms). This is done by set the fabric iocb block bit
11165  * and set up a timeout timer for 100ms. When the block bit is set, no more
11166  * fabric iocb will be issued out of the HBA.
11167  **/
11168 static void
11169 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
11170 {
11171 	int blocked;
11172 
11173 	blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11174 	/* Start a timer to unblock fabric iocbs after 100ms */
11175 	if (!blocked)
11176 		mod_timer(&phba->fabric_block_timer,
11177 			  jiffies + msecs_to_jiffies(100));
11178 
11179 	return;
11180 }
11181 
11182 /**
11183  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
11184  * @phba: pointer to lpfc hba data structure.
11185  * @cmdiocb: pointer to lpfc command iocb data structure.
11186  * @rspiocb: pointer to lpfc response iocb data structure.
11187  *
11188  * This routine is the callback function that is put to the fabric iocb's
11189  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
11190  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
11191  * function first restores and invokes the original iocb's callback function
11192  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
11193  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
11194  **/
11195 static void
11196 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11197 	struct lpfc_iocbq *rspiocb)
11198 {
11199 	struct ls_rjt stat;
11200 
11201 	BUG_ON((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
11202 
11203 	switch (rspiocb->iocb.ulpStatus) {
11204 		case IOSTAT_NPORT_RJT:
11205 		case IOSTAT_FABRIC_RJT:
11206 			if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
11207 				lpfc_block_fabric_iocbs(phba);
11208 			}
11209 			break;
11210 
11211 		case IOSTAT_NPORT_BSY:
11212 		case IOSTAT_FABRIC_BSY:
11213 			lpfc_block_fabric_iocbs(phba);
11214 			break;
11215 
11216 		case IOSTAT_LS_RJT:
11217 			stat.un.lsRjtError =
11218 				be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
11219 			if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
11220 				(stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
11221 				lpfc_block_fabric_iocbs(phba);
11222 			break;
11223 	}
11224 
11225 	BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
11226 
11227 	cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
11228 	cmdiocb->fabric_iocb_cmpl = NULL;
11229 	cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
11230 	cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
11231 
11232 	atomic_dec(&phba->fabric_iocb_count);
11233 	if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
11234 		/* Post any pending iocbs to HBA */
11235 		lpfc_resume_fabric_iocbs(phba);
11236 	}
11237 }
11238 
11239 /**
11240  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
11241  * @phba: pointer to lpfc hba data structure.
11242  * @iocb: pointer to lpfc command iocb data structure.
11243  *
11244  * This routine is used as the top-level API for issuing a fabric iocb command
11245  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
11246  * function makes sure that only one fabric bound iocb will be outstanding at
11247  * any given time. As such, this function will first check to see whether there
11248  * is already an outstanding fabric iocb on the wire. If so, it will put the
11249  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
11250  * issued later. Otherwise, it will issue the iocb on the wire and update the
11251  * fabric iocb count it indicate that there is one fabric iocb on the wire.
11252  *
11253  * Note, this implementation has a potential sending out fabric IOCBs out of
11254  * order. The problem is caused by the construction of the "ready" boolen does
11255  * not include the condition that the internal fabric IOCB list is empty. As
11256  * such, it is possible a fabric IOCB issued by this routine might be "jump"
11257  * ahead of the fabric IOCBs in the internal list.
11258  *
11259  * Return code
11260  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
11261  *   IOCB_ERROR - failed to issue fabric iocb
11262  **/
11263 static int
11264 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
11265 {
11266 	unsigned long iflags;
11267 	int ready;
11268 	int ret;
11269 
11270 	BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
11271 
11272 	spin_lock_irqsave(&phba->hbalock, iflags);
11273 	ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
11274 		!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11275 
11276 	if (ready)
11277 		/* Increment fabric iocb count to hold the position */
11278 		atomic_inc(&phba->fabric_iocb_count);
11279 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11280 	if (ready) {
11281 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
11282 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
11283 		iocb->iocb_flag |= LPFC_IO_FABRIC;
11284 
11285 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11286 			"Fabric sched2:   ste:x%x",
11287 			iocb->vport->port_state, 0, 0);
11288 
11289 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11290 
11291 		if (ret == IOCB_ERROR) {
11292 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
11293 			iocb->fabric_iocb_cmpl = NULL;
11294 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
11295 			atomic_dec(&phba->fabric_iocb_count);
11296 		}
11297 	} else {
11298 		spin_lock_irqsave(&phba->hbalock, iflags);
11299 		list_add_tail(&iocb->list, &phba->fabric_iocb_list);
11300 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11301 		ret = IOCB_SUCCESS;
11302 	}
11303 	return ret;
11304 }
11305 
11306 /**
11307  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
11308  * @vport: pointer to a virtual N_Port data structure.
11309  *
11310  * This routine aborts all the IOCBs associated with a @vport from the
11311  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11312  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11313  * list, removes each IOCB associated with the @vport off the list, set the
11314  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11315  * associated with the IOCB.
11316  **/
11317 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
11318 {
11319 	LIST_HEAD(completions);
11320 	struct lpfc_hba  *phba = vport->phba;
11321 	struct lpfc_iocbq *tmp_iocb, *piocb;
11322 
11323 	spin_lock_irq(&phba->hbalock);
11324 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
11325 				 list) {
11326 
11327 		if (piocb->vport != vport)
11328 			continue;
11329 
11330 		list_move_tail(&piocb->list, &completions);
11331 	}
11332 	spin_unlock_irq(&phba->hbalock);
11333 
11334 	/* Cancel all the IOCBs from the completions list */
11335 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11336 			      IOERR_SLI_ABORTED);
11337 }
11338 
11339 /**
11340  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
11341  * @ndlp: pointer to a node-list data structure.
11342  *
11343  * This routine aborts all the IOCBs associated with an @ndlp from the
11344  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11345  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11346  * list, removes each IOCB associated with the @ndlp off the list, set the
11347  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11348  * associated with the IOCB.
11349  **/
11350 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
11351 {
11352 	LIST_HEAD(completions);
11353 	struct lpfc_hba  *phba = ndlp->phba;
11354 	struct lpfc_iocbq *tmp_iocb, *piocb;
11355 	struct lpfc_sli_ring *pring;
11356 
11357 	pring = lpfc_phba_elsring(phba);
11358 
11359 	if (unlikely(!pring))
11360 		return;
11361 
11362 	spin_lock_irq(&phba->hbalock);
11363 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
11364 				 list) {
11365 		if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
11366 
11367 			list_move_tail(&piocb->list, &completions);
11368 		}
11369 	}
11370 	spin_unlock_irq(&phba->hbalock);
11371 
11372 	/* Cancel all the IOCBs from the completions list */
11373 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11374 			      IOERR_SLI_ABORTED);
11375 }
11376 
11377 /**
11378  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
11379  * @phba: pointer to lpfc hba data structure.
11380  *
11381  * This routine aborts all the IOCBs currently on the driver internal
11382  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
11383  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
11384  * list, removes IOCBs off the list, set the status field to
11385  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
11386  * the IOCB.
11387  **/
11388 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
11389 {
11390 	LIST_HEAD(completions);
11391 
11392 	spin_lock_irq(&phba->hbalock);
11393 	list_splice_init(&phba->fabric_iocb_list, &completions);
11394 	spin_unlock_irq(&phba->hbalock);
11395 
11396 	/* Cancel all the IOCBs from the completions list */
11397 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11398 			      IOERR_SLI_ABORTED);
11399 }
11400 
11401 /**
11402  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
11403  * @vport: pointer to lpfc vport data structure.
11404  *
11405  * This routine is invoked by the vport cleanup for deletions and the cleanup
11406  * for an ndlp on removal.
11407  **/
11408 void
11409 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
11410 {
11411 	struct lpfc_hba *phba = vport->phba;
11412 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
11413 	struct lpfc_nodelist *ndlp = NULL;
11414 	unsigned long iflag = 0;
11415 
11416 	spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
11417 	list_for_each_entry_safe(sglq_entry, sglq_next,
11418 			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
11419 		if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport) {
11420 			lpfc_nlp_put(sglq_entry->ndlp);
11421 			ndlp = sglq_entry->ndlp;
11422 			sglq_entry->ndlp = NULL;
11423 
11424 			/* If the xri on the abts_els_sgl list is for the Fport
11425 			 * node and the vport is unloading, the xri aborted wcqe
11426 			 * likely isn't coming back.  Just release the sgl.
11427 			 */
11428 			if ((vport->load_flag & FC_UNLOADING) &&
11429 			    ndlp->nlp_DID == Fabric_DID) {
11430 				list_del(&sglq_entry->list);
11431 				sglq_entry->state = SGL_FREED;
11432 				list_add_tail(&sglq_entry->list,
11433 					&phba->sli4_hba.lpfc_els_sgl_list);
11434 			}
11435 		}
11436 	}
11437 	spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
11438 	return;
11439 }
11440 
11441 /**
11442  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
11443  * @phba: pointer to lpfc hba data structure.
11444  * @axri: pointer to the els xri abort wcqe structure.
11445  *
11446  * This routine is invoked by the worker thread to process a SLI4 slow-path
11447  * ELS aborted xri.
11448  **/
11449 void
11450 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
11451 			  struct sli4_wcqe_xri_aborted *axri)
11452 {
11453 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
11454 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
11455 	uint16_t lxri = 0;
11456 
11457 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
11458 	unsigned long iflag = 0;
11459 	struct lpfc_nodelist *ndlp;
11460 	struct lpfc_sli_ring *pring;
11461 
11462 	pring = lpfc_phba_elsring(phba);
11463 
11464 	spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
11465 	list_for_each_entry_safe(sglq_entry, sglq_next,
11466 			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
11467 		if (sglq_entry->sli4_xritag == xri) {
11468 			list_del(&sglq_entry->list);
11469 			ndlp = sglq_entry->ndlp;
11470 			sglq_entry->ndlp = NULL;
11471 			list_add_tail(&sglq_entry->list,
11472 				&phba->sli4_hba.lpfc_els_sgl_list);
11473 			sglq_entry->state = SGL_FREED;
11474 			spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock,
11475 					       iflag);
11476 
11477 			if (ndlp) {
11478 				lpfc_set_rrq_active(phba, ndlp,
11479 					sglq_entry->sli4_lxritag,
11480 					rxid, 1);
11481 				lpfc_nlp_put(ndlp);
11482 			}
11483 
11484 			/* Check if TXQ queue needs to be serviced */
11485 			if (pring && !list_empty(&pring->txq))
11486 				lpfc_worker_wake_up(phba);
11487 			return;
11488 		}
11489 	}
11490 	spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
11491 	lxri = lpfc_sli4_xri_inrange(phba, xri);
11492 	if (lxri == NO_XRI)
11493 		return;
11494 
11495 	spin_lock_irqsave(&phba->hbalock, iflag);
11496 	sglq_entry = __lpfc_get_active_sglq(phba, lxri);
11497 	if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
11498 		spin_unlock_irqrestore(&phba->hbalock, iflag);
11499 		return;
11500 	}
11501 	sglq_entry->state = SGL_XRI_ABORTED;
11502 	spin_unlock_irqrestore(&phba->hbalock, iflag);
11503 	return;
11504 }
11505 
11506 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
11507  * @vport: pointer to virtual port object.
11508  * @ndlp: nodelist pointer for the impacted node.
11509  *
11510  * The driver calls this routine in response to an SLI4 XRI ABORT CQE
11511  * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
11512  * the driver is required to send a LOGO to the remote node before it
11513  * attempts to recover its login to the remote node.
11514  */
11515 void
11516 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
11517 			   struct lpfc_nodelist *ndlp)
11518 {
11519 	struct Scsi_Host *shost;
11520 	struct lpfc_hba *phba;
11521 	unsigned long flags = 0;
11522 
11523 	shost = lpfc_shost_from_vport(vport);
11524 	phba = vport->phba;
11525 	if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
11526 		lpfc_printf_log(phba, KERN_INFO,
11527 				LOG_SLI, "3093 No rport recovery needed. "
11528 				"rport in state 0x%x\n", ndlp->nlp_state);
11529 		return;
11530 	}
11531 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11532 			"3094 Start rport recovery on shost id 0x%x "
11533 			"fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
11534 			"flags 0x%x\n",
11535 			shost->host_no, ndlp->nlp_DID,
11536 			vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
11537 			ndlp->nlp_flag);
11538 	/*
11539 	 * The rport is not responding.  Remove the FCP-2 flag to prevent
11540 	 * an ADISC in the follow-up recovery code.
11541 	 */
11542 	spin_lock_irqsave(&ndlp->lock, flags);
11543 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
11544 	ndlp->nlp_flag |= NLP_ISSUE_LOGO;
11545 	spin_unlock_irqrestore(&ndlp->lock, flags);
11546 	lpfc_unreg_rpi(vport, ndlp);
11547 }
11548 
11549 static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport)
11550 {
11551 	bitmap_zero(vport->vmid_priority_range, LPFC_VMID_MAX_PRIORITY_RANGE);
11552 }
11553 
11554 static void
11555 lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max)
11556 {
11557 	u32 i;
11558 
11559 	if ((min > max) || (max > LPFC_VMID_MAX_PRIORITY_RANGE))
11560 		return;
11561 
11562 	for (i = min; i <= max; i++)
11563 		set_bit(i, vport->vmid_priority_range);
11564 }
11565 
11566 static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid)
11567 {
11568 	set_bit(ctcl_vmid, vport->vmid_priority_range);
11569 }
11570 
11571 u32 lpfc_vmid_get_cs_ctl(struct lpfc_vport *vport)
11572 {
11573 	u32 i;
11574 
11575 	i = find_first_bit(vport->vmid_priority_range,
11576 			   LPFC_VMID_MAX_PRIORITY_RANGE);
11577 
11578 	if (i == LPFC_VMID_MAX_PRIORITY_RANGE)
11579 		return 0;
11580 
11581 	clear_bit(i, vport->vmid_priority_range);
11582 	return i;
11583 }
11584 
11585 #define MAX_PRIORITY_DESC	255
11586 
11587 static void
11588 lpfc_cmpl_els_qfpa(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11589 		   struct lpfc_iocbq *rspiocb)
11590 {
11591 	struct lpfc_vport *vport = cmdiocb->vport;
11592 	struct priority_range_desc *desc;
11593 	struct lpfc_dmabuf *prsp = NULL;
11594 	struct lpfc_vmid_priority_range *vmid_range = NULL;
11595 	u32 *data;
11596 	struct lpfc_dmabuf *dmabuf = cmdiocb->context2;
11597 	IOCB_t *irsp = &rspiocb->iocb;
11598 	u8 *pcmd, max_desc;
11599 	u32 len, i;
11600 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
11601 
11602 	prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
11603 	if (!prsp)
11604 		goto out;
11605 
11606 	pcmd = prsp->virt;
11607 	data = (u32 *)pcmd;
11608 	if (data[0] == ELS_CMD_LS_RJT) {
11609 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
11610 				 "3277 QFPA LS_RJT x%x  x%x\n",
11611 				 data[0], data[1]);
11612 		goto out;
11613 	}
11614 	if (irsp->ulpStatus) {
11615 		lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
11616 				 "6529 QFPA failed with status x%x  x%x\n",
11617 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
11618 		goto out;
11619 	}
11620 
11621 	if (!vport->qfpa_res) {
11622 		max_desc = FCELSSIZE / sizeof(*vport->qfpa_res);
11623 		vport->qfpa_res = kcalloc(max_desc, sizeof(*vport->qfpa_res),
11624 					  GFP_KERNEL);
11625 		if (!vport->qfpa_res)
11626 			goto out;
11627 	}
11628 
11629 	len = *((u32 *)(pcmd + 4));
11630 	len = be32_to_cpu(len);
11631 	memcpy(vport->qfpa_res, pcmd, len + 8);
11632 	len = len / LPFC_PRIORITY_RANGE_DESC_SIZE;
11633 
11634 	desc = (struct priority_range_desc *)(pcmd + 8);
11635 	vmid_range = vport->vmid_priority.vmid_range;
11636 	if (!vmid_range) {
11637 		vmid_range = kcalloc(MAX_PRIORITY_DESC, sizeof(*vmid_range),
11638 				     GFP_KERNEL);
11639 		if (!vmid_range) {
11640 			kfree(vport->qfpa_res);
11641 			goto out;
11642 		}
11643 		vport->vmid_priority.vmid_range = vmid_range;
11644 	}
11645 	vport->vmid_priority.num_descriptors = len;
11646 
11647 	for (i = 0; i < len; i++, vmid_range++, desc++) {
11648 		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
11649 				 "6539 vmid values low=%d, high=%d, qos=%d, "
11650 				 "local ve id=%d\n", desc->lo_range,
11651 				 desc->hi_range, desc->qos_priority,
11652 				 desc->local_ve_id);
11653 
11654 		vmid_range->low = desc->lo_range << 1;
11655 		if (desc->local_ve_id == QFPA_ODD_ONLY)
11656 			vmid_range->low++;
11657 		if (desc->qos_priority)
11658 			vport->vmid_flag |= LPFC_VMID_QOS_ENABLED;
11659 		vmid_range->qos = desc->qos_priority;
11660 
11661 		vmid_range->high = desc->hi_range << 1;
11662 		if ((desc->local_ve_id == QFPA_ODD_ONLY) ||
11663 		    (desc->local_ve_id == QFPA_EVEN_ODD))
11664 			vmid_range->high++;
11665 	}
11666 	lpfc_init_cs_ctl_bitmap(vport);
11667 	for (i = 0; i < vport->vmid_priority.num_descriptors; i++) {
11668 		lpfc_vmid_set_cs_ctl_range(vport,
11669 				vport->vmid_priority.vmid_range[i].low,
11670 				vport->vmid_priority.vmid_range[i].high);
11671 	}
11672 
11673 	vport->vmid_flag |= LPFC_VMID_QFPA_CMPL;
11674  out:
11675 	lpfc_els_free_iocb(phba, cmdiocb);
11676 	lpfc_nlp_put(ndlp);
11677 }
11678 
11679 int lpfc_issue_els_qfpa(struct lpfc_vport *vport)
11680 {
11681 	struct lpfc_hba *phba = vport->phba;
11682 	struct lpfc_nodelist *ndlp;
11683 	struct lpfc_iocbq *elsiocb;
11684 	u8 *pcmd;
11685 	int ret;
11686 
11687 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
11688 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
11689 		return -ENXIO;
11690 
11691 	elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_QFPA_SIZE, 2, ndlp,
11692 				     ndlp->nlp_DID, ELS_CMD_QFPA);
11693 	if (!elsiocb)
11694 		return -ENOMEM;
11695 
11696 	pcmd = (u8 *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
11697 
11698 	*((u32 *)(pcmd)) = ELS_CMD_QFPA;
11699 	pcmd += 4;
11700 
11701 	elsiocb->iocb_cmpl = lpfc_cmpl_els_qfpa;
11702 
11703 	elsiocb->context1 = lpfc_nlp_get(ndlp);
11704 	if (!elsiocb->context1) {
11705 		lpfc_els_free_iocb(vport->phba, elsiocb);
11706 		return -ENXIO;
11707 	}
11708 
11709 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 2);
11710 	if (ret != IOCB_SUCCESS) {
11711 		lpfc_els_free_iocb(phba, elsiocb);
11712 		lpfc_nlp_put(ndlp);
11713 		return -EIO;
11714 	}
11715 	vport->vmid_flag &= ~LPFC_VMID_QOS_ENABLED;
11716 	return 0;
11717 }
11718 
11719 int
11720 lpfc_vmid_uvem(struct lpfc_vport *vport,
11721 	       struct lpfc_vmid *vmid, bool instantiated)
11722 {
11723 	struct lpfc_vem_id_desc *vem_id_desc;
11724 	struct lpfc_nodelist *ndlp;
11725 	struct lpfc_iocbq *elsiocb;
11726 	struct instantiated_ve_desc *inst_desc;
11727 	struct lpfc_vmid_context *vmid_context;
11728 	u8 *pcmd;
11729 	u32 *len;
11730 	int ret = 0;
11731 
11732 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
11733 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
11734 		return -ENXIO;
11735 
11736 	vmid_context = kmalloc(sizeof(*vmid_context), GFP_KERNEL);
11737 	if (!vmid_context)
11738 		return -ENOMEM;
11739 	elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_UVEM_SIZE, 2,
11740 				     ndlp, Fabric_DID, ELS_CMD_UVEM);
11741 	if (!elsiocb)
11742 		goto out;
11743 
11744 	lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
11745 			 "3427 Host vmid %s %d\n",
11746 			 vmid->host_vmid, instantiated);
11747 	vmid_context->vmp = vmid;
11748 	vmid_context->nlp = ndlp;
11749 	vmid_context->instantiated = instantiated;
11750 	elsiocb->vmid_tag.vmid_context = vmid_context;
11751 	pcmd = (u8 *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
11752 
11753 	if (uuid_is_null((uuid_t *)vport->lpfc_vmid_host_uuid))
11754 		memcpy(vport->lpfc_vmid_host_uuid, vmid->host_vmid,
11755 		       LPFC_COMPRESS_VMID_SIZE);
11756 
11757 	*((u32 *)(pcmd)) = ELS_CMD_UVEM;
11758 	len = (u32 *)(pcmd + 4);
11759 	*len = cpu_to_be32(LPFC_UVEM_SIZE - 8);
11760 
11761 	vem_id_desc = (struct lpfc_vem_id_desc *)(pcmd + 8);
11762 	vem_id_desc->tag = be32_to_cpu(VEM_ID_DESC_TAG);
11763 	vem_id_desc->length = be32_to_cpu(LPFC_UVEM_VEM_ID_DESC_SIZE);
11764 	memcpy(vem_id_desc->vem_id, vport->lpfc_vmid_host_uuid,
11765 	       LPFC_COMPRESS_VMID_SIZE);
11766 
11767 	inst_desc = (struct instantiated_ve_desc *)(pcmd + 32);
11768 	inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
11769 	inst_desc->length = be32_to_cpu(LPFC_UVEM_VE_MAP_DESC_SIZE);
11770 	memcpy(inst_desc->global_vem_id, vmid->host_vmid,
11771 	       LPFC_COMPRESS_VMID_SIZE);
11772 
11773 	bf_set(lpfc_instantiated_nport_id, inst_desc, vport->fc_myDID);
11774 	bf_set(lpfc_instantiated_local_id, inst_desc,
11775 	       vmid->un.cs_ctl_vmid);
11776 	if (instantiated) {
11777 		inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
11778 	} else {
11779 		inst_desc->tag = be32_to_cpu(DEINSTANTIATED_VE_DESC_TAG);
11780 		lpfc_vmid_put_cs_ctl(vport, vmid->un.cs_ctl_vmid);
11781 	}
11782 	inst_desc->word6 = cpu_to_be32(inst_desc->word6);
11783 
11784 	elsiocb->iocb_cmpl = lpfc_cmpl_els_uvem;
11785 
11786 	elsiocb->context1 = lpfc_nlp_get(ndlp);
11787 	if (!elsiocb->context1) {
11788 		lpfc_els_free_iocb(vport->phba, elsiocb);
11789 		goto out;
11790 	}
11791 
11792 	ret = lpfc_sli_issue_iocb(vport->phba, LPFC_ELS_RING, elsiocb, 0);
11793 	if (ret != IOCB_SUCCESS) {
11794 		lpfc_els_free_iocb(vport->phba, elsiocb);
11795 		lpfc_nlp_put(ndlp);
11796 		goto out;
11797 	}
11798 
11799 	return 0;
11800  out:
11801 	kfree(vmid_context);
11802 	return -EIO;
11803 }
11804 
11805 static void
11806 lpfc_cmpl_els_uvem(struct lpfc_hba *phba, struct lpfc_iocbq *icmdiocb,
11807 		   struct lpfc_iocbq *rspiocb)
11808 {
11809 	struct lpfc_vport *vport = icmdiocb->vport;
11810 	struct lpfc_dmabuf *prsp = NULL;
11811 	struct lpfc_vmid_context *vmid_context =
11812 	    icmdiocb->vmid_tag.vmid_context;
11813 	struct lpfc_nodelist *ndlp = icmdiocb->context1;
11814 	u8 *pcmd;
11815 	u32 *data;
11816 	IOCB_t *irsp = &rspiocb->iocb;
11817 	struct lpfc_dmabuf *dmabuf = icmdiocb->context2;
11818 	struct lpfc_vmid *vmid;
11819 
11820 	vmid = vmid_context->vmp;
11821 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
11822 		ndlp = NULL;
11823 
11824 	prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
11825 	if (!prsp)
11826 		goto out;
11827 	pcmd = prsp->virt;
11828 	data = (u32 *)pcmd;
11829 	if (data[0] == ELS_CMD_LS_RJT) {
11830 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
11831 				 "4532 UVEM LS_RJT %x %x\n", data[0], data[1]);
11832 		goto out;
11833 	}
11834 	if (irsp->ulpStatus) {
11835 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
11836 				 "4533 UVEM error status %x: %x\n",
11837 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
11838 		goto out;
11839 	}
11840 	spin_lock(&phba->hbalock);
11841 	/* Set IN USE flag */
11842 	vport->vmid_flag |= LPFC_VMID_IN_USE;
11843 	phba->pport->vmid_flag |= LPFC_VMID_IN_USE;
11844 	spin_unlock(&phba->hbalock);
11845 
11846 	if (vmid_context->instantiated) {
11847 		write_lock(&vport->vmid_lock);
11848 		vmid->flag |= LPFC_VMID_REGISTERED;
11849 		vmid->flag &= ~LPFC_VMID_REQ_REGISTER;
11850 		write_unlock(&vport->vmid_lock);
11851 	}
11852 
11853  out:
11854 	kfree(vmid_context);
11855 	lpfc_els_free_iocb(phba, icmdiocb);
11856 	lpfc_nlp_put(ndlp);
11857 }
11858