xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_els.c (revision 76a4f7cc)
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->upcall_flags & NLP_WAIT_FOR_LOGO) {
2909 		wake_up_waiter = 1;
2910 		ndlp->upcall_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 				be32_to_cpu(pcgd->xmt_signal_frequency.count),
4025 				be32_to_cpu(pcgd->xmt_signal_frequency.units),
4026 				be32_to_cpu(pcgd->rcv_signal_capability),
4027 				be32_to_cpu(pcgd->rcv_signal_frequency.count),
4028 				be32_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 		}
4581 		break;
4582 
4583 	case IOSTAT_NPORT_RJT:
4584 	case IOSTAT_FABRIC_RJT:
4585 		if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
4586 			retry = 1;
4587 			break;
4588 		}
4589 		break;
4590 
4591 	case IOSTAT_NPORT_BSY:
4592 	case IOSTAT_FABRIC_BSY:
4593 		logerr = 1; /* Fabric / Remote NPort out of resources */
4594 		retry = 1;
4595 		break;
4596 
4597 	case IOSTAT_LS_RJT:
4598 		stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
4599 		/* Added for Vendor specifc support
4600 		 * Just keep retrying for these Rsn / Exp codes
4601 		 */
4602 		switch (stat.un.b.lsRjtRsnCode) {
4603 		case LSRJT_UNABLE_TPC:
4604 			/* The driver has a VALID PLOGI but the rport has
4605 			 * rejected the PRLI - can't do it now.  Delay
4606 			 * for 1 second and try again.
4607 			 *
4608 			 * However, if explanation is REQ_UNSUPPORTED there's
4609 			 * no point to retry PRLI.
4610 			 */
4611 			if ((cmd == ELS_CMD_PRLI || cmd == ELS_CMD_NVMEPRLI) &&
4612 			    stat.un.b.lsRjtRsnCodeExp !=
4613 			    LSEXP_REQ_UNSUPPORTED) {
4614 				delay = 1000;
4615 				maxretry = lpfc_max_els_tries + 1;
4616 				retry = 1;
4617 				break;
4618 			}
4619 
4620 			/* Legacy bug fix code for targets with PLOGI delays. */
4621 			if (stat.un.b.lsRjtRsnCodeExp ==
4622 			    LSEXP_CMD_IN_PROGRESS) {
4623 				if (cmd == ELS_CMD_PLOGI) {
4624 					delay = 1000;
4625 					maxretry = 48;
4626 				}
4627 				retry = 1;
4628 				break;
4629 			}
4630 			if (stat.un.b.lsRjtRsnCodeExp ==
4631 			    LSEXP_CANT_GIVE_DATA) {
4632 				if (cmd == ELS_CMD_PLOGI) {
4633 					delay = 1000;
4634 					maxretry = 48;
4635 				}
4636 				retry = 1;
4637 				break;
4638 			}
4639 			if (cmd == ELS_CMD_PLOGI) {
4640 				delay = 1000;
4641 				maxretry = lpfc_max_els_tries + 1;
4642 				retry = 1;
4643 				break;
4644 			}
4645 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4646 			  (cmd == ELS_CMD_FDISC) &&
4647 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
4648 				lpfc_printf_vlog(vport, KERN_ERR,
4649 						 LOG_TRACE_EVENT,
4650 						 "0125 FDISC Failed (x%x). "
4651 						 "Fabric out of resources\n",
4652 						 stat.un.lsRjtError);
4653 				lpfc_vport_set_state(vport,
4654 						     FC_VPORT_NO_FABRIC_RSCS);
4655 			}
4656 			break;
4657 
4658 		case LSRJT_LOGICAL_BSY:
4659 			if ((cmd == ELS_CMD_PLOGI) ||
4660 			    (cmd == ELS_CMD_PRLI) ||
4661 			    (cmd == ELS_CMD_NVMEPRLI)) {
4662 				delay = 1000;
4663 				maxretry = 48;
4664 			} else if (cmd == ELS_CMD_FDISC) {
4665 				/* FDISC retry policy */
4666 				maxretry = 48;
4667 				if (cmdiocb->retry >= 32)
4668 					delay = 1000;
4669 			}
4670 			retry = 1;
4671 			break;
4672 
4673 		case LSRJT_LOGICAL_ERR:
4674 			/* There are some cases where switches return this
4675 			 * error when they are not ready and should be returning
4676 			 * Logical Busy. We should delay every time.
4677 			 */
4678 			if (cmd == ELS_CMD_FDISC &&
4679 			    stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
4680 				maxretry = 3;
4681 				delay = 1000;
4682 				retry = 1;
4683 			} else if (cmd == ELS_CMD_FLOGI &&
4684 				   stat.un.b.lsRjtRsnCodeExp ==
4685 						LSEXP_NOTHING_MORE) {
4686 				vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
4687 				retry = 1;
4688 				lpfc_printf_vlog(vport, KERN_ERR,
4689 						 LOG_TRACE_EVENT,
4690 						 "0820 FLOGI Failed (x%x). "
4691 						 "BBCredit Not Supported\n",
4692 						 stat.un.lsRjtError);
4693 			}
4694 			break;
4695 
4696 		case LSRJT_PROTOCOL_ERR:
4697 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4698 			  (cmd == ELS_CMD_FDISC) &&
4699 			  ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
4700 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
4701 			  ) {
4702 				lpfc_printf_vlog(vport, KERN_ERR,
4703 						 LOG_TRACE_EVENT,
4704 						 "0122 FDISC Failed (x%x). "
4705 						 "Fabric Detected Bad WWN\n",
4706 						 stat.un.lsRjtError);
4707 				lpfc_vport_set_state(vport,
4708 						     FC_VPORT_FABRIC_REJ_WWN);
4709 			}
4710 			break;
4711 		case LSRJT_VENDOR_UNIQUE:
4712 			if ((stat.un.b.vendorUnique == 0x45) &&
4713 			    (cmd == ELS_CMD_FLOGI)) {
4714 				goto out_retry;
4715 			}
4716 			break;
4717 		case LSRJT_CMD_UNSUPPORTED:
4718 			/* lpfc nvmet returns this type of LS_RJT when it
4719 			 * receives an FCP PRLI because lpfc nvmet only
4720 			 * support NVME.  ELS request is terminated for FCP4
4721 			 * on this rport.
4722 			 */
4723 			if (stat.un.b.lsRjtRsnCodeExp ==
4724 			    LSEXP_REQ_UNSUPPORTED && cmd == ELS_CMD_PRLI) {
4725 				spin_lock_irq(&ndlp->lock);
4726 				ndlp->nlp_flag |= NLP_FCP_PRLI_RJT;
4727 				spin_unlock_irq(&ndlp->lock);
4728 				retry = 0;
4729 				goto out_retry;
4730 			}
4731 			break;
4732 		}
4733 		break;
4734 
4735 	case IOSTAT_INTERMED_RSP:
4736 	case IOSTAT_BA_RJT:
4737 		break;
4738 
4739 	default:
4740 		break;
4741 	}
4742 
4743 	if (link_reset) {
4744 		rc = lpfc_link_reset(vport);
4745 		if (rc) {
4746 			/* Do not give up. Retry PLOGI one more time and attempt
4747 			 * link reset if PLOGI fails again.
4748 			 */
4749 			retry = 1;
4750 			delay = 100;
4751 			goto out_retry;
4752 		}
4753 		return 1;
4754 	}
4755 
4756 	if (did == FDMI_DID)
4757 		retry = 1;
4758 
4759 	if ((cmd == ELS_CMD_FLOGI) &&
4760 	    (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
4761 	    !lpfc_error_lost_link(irsp)) {
4762 		/* FLOGI retry policy */
4763 		retry = 1;
4764 		/* retry FLOGI forever */
4765 		if (phba->link_flag != LS_LOOPBACK_MODE)
4766 			maxretry = 0;
4767 		else
4768 			maxretry = 2;
4769 
4770 		if (cmdiocb->retry >= 100)
4771 			delay = 5000;
4772 		else if (cmdiocb->retry >= 32)
4773 			delay = 1000;
4774 	} else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
4775 		/* retry FDISCs every second up to devloss */
4776 		retry = 1;
4777 		maxretry = vport->cfg_devloss_tmo;
4778 		delay = 1000;
4779 	}
4780 
4781 	cmdiocb->retry++;
4782 	if (maxretry && (cmdiocb->retry >= maxretry)) {
4783 		phba->fc_stat.elsRetryExceeded++;
4784 		retry = 0;
4785 	}
4786 
4787 	if ((vport->load_flag & FC_UNLOADING) != 0)
4788 		retry = 0;
4789 
4790 out_retry:
4791 	if (retry) {
4792 		if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
4793 			/* Stop retrying PLOGI and FDISC if in FCF discovery */
4794 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4795 				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4796 						 "2849 Stop retry ELS command "
4797 						 "x%x to remote NPORT x%x, "
4798 						 "Data: x%x x%x\n", cmd, did,
4799 						 cmdiocb->retry, delay);
4800 				return 0;
4801 			}
4802 		}
4803 
4804 		/* Retry ELS command <elsCmd> to remote NPORT <did> */
4805 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4806 				 "0107 Retry ELS command x%x to remote "
4807 				 "NPORT x%x Data: x%x x%x\n",
4808 				 cmd, did, cmdiocb->retry, delay);
4809 
4810 		if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
4811 			((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
4812 			((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
4813 			IOERR_NO_RESOURCES))) {
4814 			/* Don't reset timer for no resources */
4815 
4816 			/* If discovery / RSCN timer is running, reset it */
4817 			if (timer_pending(&vport->fc_disctmo) ||
4818 			    (vport->fc_flag & FC_RSCN_MODE))
4819 				lpfc_set_disctmo(vport);
4820 		}
4821 
4822 		phba->fc_stat.elsXmitRetry++;
4823 		if (ndlp && delay) {
4824 			phba->fc_stat.elsDelayRetry++;
4825 			ndlp->nlp_retry = cmdiocb->retry;
4826 
4827 			/* delay is specified in milliseconds */
4828 			mod_timer(&ndlp->nlp_delayfunc,
4829 				jiffies + msecs_to_jiffies(delay));
4830 			spin_lock_irq(&ndlp->lock);
4831 			ndlp->nlp_flag |= NLP_DELAY_TMO;
4832 			spin_unlock_irq(&ndlp->lock);
4833 
4834 			ndlp->nlp_prev_state = ndlp->nlp_state;
4835 			if ((cmd == ELS_CMD_PRLI) ||
4836 			    (cmd == ELS_CMD_NVMEPRLI))
4837 				lpfc_nlp_set_state(vport, ndlp,
4838 					NLP_STE_PRLI_ISSUE);
4839 			else if (cmd != ELS_CMD_ADISC)
4840 				lpfc_nlp_set_state(vport, ndlp,
4841 					NLP_STE_NPR_NODE);
4842 			ndlp->nlp_last_elscmd = cmd;
4843 
4844 			return 1;
4845 		}
4846 		switch (cmd) {
4847 		case ELS_CMD_FLOGI:
4848 			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
4849 			return 1;
4850 		case ELS_CMD_FDISC:
4851 			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
4852 			return 1;
4853 		case ELS_CMD_PLOGI:
4854 			if (ndlp) {
4855 				ndlp->nlp_prev_state = ndlp->nlp_state;
4856 				lpfc_nlp_set_state(vport, ndlp,
4857 						   NLP_STE_PLOGI_ISSUE);
4858 			}
4859 			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
4860 			return 1;
4861 		case ELS_CMD_ADISC:
4862 			ndlp->nlp_prev_state = ndlp->nlp_state;
4863 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4864 			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
4865 			return 1;
4866 		case ELS_CMD_PRLI:
4867 		case ELS_CMD_NVMEPRLI:
4868 			ndlp->nlp_prev_state = ndlp->nlp_state;
4869 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
4870 			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
4871 			return 1;
4872 		case ELS_CMD_LOGO:
4873 			ndlp->nlp_prev_state = ndlp->nlp_state;
4874 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4875 			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
4876 			return 1;
4877 		}
4878 	}
4879 	/* No retry ELS command <elsCmd> to remote NPORT <did> */
4880 	if (logerr) {
4881 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4882 			 "0137 No retry ELS command x%x to remote "
4883 			 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
4884 			 cmd, did, irsp->ulpStatus,
4885 			 irsp->un.ulpWord[4]);
4886 	}
4887 	else {
4888 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4889 			 "0108 No retry ELS command x%x to remote "
4890 			 "NPORT x%x Retried:%d Error:x%x/%x\n",
4891 			 cmd, did, cmdiocb->retry, irsp->ulpStatus,
4892 			 irsp->un.ulpWord[4]);
4893 	}
4894 	return 0;
4895 }
4896 
4897 /**
4898  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
4899  * @phba: pointer to lpfc hba data structure.
4900  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
4901  *
4902  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
4903  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
4904  * checks to see whether there is a lpfc DMA buffer associated with the
4905  * response of the command IOCB. If so, it will be released before releasing
4906  * the lpfc DMA buffer associated with the IOCB itself.
4907  *
4908  * Return code
4909  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4910  **/
4911 static int
4912 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
4913 {
4914 	struct lpfc_dmabuf *buf_ptr;
4915 
4916 	/* Free the response before processing the command. */
4917 	if (!list_empty(&buf_ptr1->list)) {
4918 		list_remove_head(&buf_ptr1->list, buf_ptr,
4919 				 struct lpfc_dmabuf,
4920 				 list);
4921 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4922 		kfree(buf_ptr);
4923 	}
4924 	lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
4925 	kfree(buf_ptr1);
4926 	return 0;
4927 }
4928 
4929 /**
4930  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
4931  * @phba: pointer to lpfc hba data structure.
4932  * @buf_ptr: pointer to the lpfc dma buffer data structure.
4933  *
4934  * This routine releases the lpfc Direct Memory Access (DMA) buffer
4935  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
4936  * pool.
4937  *
4938  * Return code
4939  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4940  **/
4941 static int
4942 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
4943 {
4944 	lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4945 	kfree(buf_ptr);
4946 	return 0;
4947 }
4948 
4949 /**
4950  * lpfc_els_free_iocb - Free a command iocb and its associated resources
4951  * @phba: pointer to lpfc hba data structure.
4952  * @elsiocb: pointer to lpfc els command iocb data structure.
4953  *
4954  * This routine frees a command IOCB and its associated resources. The
4955  * command IOCB data structure contains the reference to various associated
4956  * resources, these fields must be set to NULL if the associated reference
4957  * not present:
4958  *   context1 - reference to ndlp
4959  *   context2 - reference to cmd
4960  *   context2->next - reference to rsp
4961  *   context3 - reference to bpl
4962  *
4963  * It first properly decrements the reference count held on ndlp for the
4964  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
4965  * set, it invokes the lpfc_els_free_data() routine to release the Direct
4966  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
4967  * adds the DMA buffer the @phba data structure for the delayed release.
4968  * If reference to the Buffer Pointer List (BPL) is present, the
4969  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
4970  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
4971  * invoked to release the IOCB data structure back to @phba IOCBQ list.
4972  *
4973  * Return code
4974  *   0 - Success (currently, always return 0)
4975  **/
4976 int
4977 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
4978 {
4979 	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
4980 
4981 	/* The I/O iocb is complete.  Clear the context1 data. */
4982 	elsiocb->context1 = NULL;
4983 
4984 	/* context2  = cmd,  context2->next = rsp, context3 = bpl */
4985 	if (elsiocb->context2) {
4986 		if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
4987 			/* Firmware could still be in progress of DMAing
4988 			 * payload, so don't free data buffer till after
4989 			 * a hbeat.
4990 			 */
4991 			elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
4992 			buf_ptr = elsiocb->context2;
4993 			elsiocb->context2 = NULL;
4994 			if (buf_ptr) {
4995 				buf_ptr1 = NULL;
4996 				spin_lock_irq(&phba->hbalock);
4997 				if (!list_empty(&buf_ptr->list)) {
4998 					list_remove_head(&buf_ptr->list,
4999 						buf_ptr1, struct lpfc_dmabuf,
5000 						list);
5001 					INIT_LIST_HEAD(&buf_ptr1->list);
5002 					list_add_tail(&buf_ptr1->list,
5003 						&phba->elsbuf);
5004 					phba->elsbuf_cnt++;
5005 				}
5006 				INIT_LIST_HEAD(&buf_ptr->list);
5007 				list_add_tail(&buf_ptr->list, &phba->elsbuf);
5008 				phba->elsbuf_cnt++;
5009 				spin_unlock_irq(&phba->hbalock);
5010 			}
5011 		} else {
5012 			buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
5013 			lpfc_els_free_data(phba, buf_ptr1);
5014 			elsiocb->context2 = NULL;
5015 		}
5016 	}
5017 
5018 	if (elsiocb->context3) {
5019 		buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
5020 		lpfc_els_free_bpl(phba, buf_ptr);
5021 		elsiocb->context3 = NULL;
5022 	}
5023 	lpfc_sli_release_iocbq(phba, elsiocb);
5024 	return 0;
5025 }
5026 
5027 /**
5028  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
5029  * @phba: pointer to lpfc hba data structure.
5030  * @cmdiocb: pointer to lpfc command iocb data structure.
5031  * @rspiocb: pointer to lpfc response iocb data structure.
5032  *
5033  * This routine is the completion callback function to the Logout (LOGO)
5034  * Accept (ACC) Response ELS command. This routine is invoked to indicate
5035  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
5036  * release the ndlp if it has the last reference remaining (reference count
5037  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
5038  * field to NULL to inform the following lpfc_els_free_iocb() routine no
5039  * ndlp reference count needs to be decremented. Otherwise, the ndlp
5040  * reference use-count shall be decremented by the lpfc_els_free_iocb()
5041  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
5042  * IOCB data structure.
5043  **/
5044 static void
5045 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5046 		       struct lpfc_iocbq *rspiocb)
5047 {
5048 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
5049 	struct lpfc_vport *vport = cmdiocb->vport;
5050 	IOCB_t *irsp;
5051 
5052 	irsp = &rspiocb->iocb;
5053 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5054 		"ACC LOGO cmpl:   status:x%x/x%x did:x%x",
5055 		irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
5056 	/* ACC to LOGO completes to NPort <nlp_DID> */
5057 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5058 			 "0109 ACC to LOGO completes to NPort x%x refcnt %d "
5059 			 "Data: x%x x%x x%x\n",
5060 			 ndlp->nlp_DID, kref_read(&ndlp->kref), ndlp->nlp_flag,
5061 			 ndlp->nlp_state, ndlp->nlp_rpi);
5062 
5063 	/* This clause allows the LOGO ACC to complete and free resources
5064 	 * for the Fabric Domain Controller.  It does deliberately skip
5065 	 * the unreg_rpi and release rpi because some fabrics send RDP
5066 	 * requests after logging out from the initiator.
5067 	 */
5068 	if (ndlp->nlp_type & NLP_FABRIC &&
5069 	    ((ndlp->nlp_DID & WELL_KNOWN_DID_MASK) != WELL_KNOWN_DID_MASK))
5070 		goto out;
5071 
5072 	if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
5073 
5074 		/* If PLOGI is being retried, PLOGI completion will cleanup the
5075 		 * node. The NLP_NPR_2B_DISC flag needs to be retained to make
5076 		 * progress on nodes discovered from last RSCN.
5077 		 */
5078 		if ((ndlp->nlp_flag & NLP_DELAY_TMO) &&
5079 		    (ndlp->nlp_last_elscmd == ELS_CMD_PLOGI))
5080 			goto out;
5081 
5082 		/* NPort Recovery mode or node is just allocated */
5083 		if (!lpfc_nlp_not_used(ndlp)) {
5084 			/* A LOGO is completing and the node is in NPR state.
5085 			 * If this a fabric node that cleared its transport
5086 			 * registration, release the rpi.
5087 			 */
5088 			spin_lock_irq(&ndlp->lock);
5089 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
5090 			if (phba->sli_rev == LPFC_SLI_REV4)
5091 				ndlp->nlp_flag |= NLP_RELEASE_RPI;
5092 			spin_unlock_irq(&ndlp->lock);
5093 			lpfc_unreg_rpi(vport, ndlp);
5094 		} else {
5095 			/* Indicate the node has already released, should
5096 			 * not reference to it from within lpfc_els_free_iocb.
5097 			 */
5098 			cmdiocb->context1 = NULL;
5099 		}
5100 	}
5101  out:
5102 	/*
5103 	 * The driver received a LOGO from the rport and has ACK'd it.
5104 	 * At this point, the driver is done so release the IOCB
5105 	 */
5106 	lpfc_els_free_iocb(phba, cmdiocb);
5107 	lpfc_nlp_put(ndlp);
5108 }
5109 
5110 /**
5111  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
5112  * @phba: pointer to lpfc hba data structure.
5113  * @pmb: pointer to the driver internal queue element for mailbox command.
5114  *
5115  * This routine is the completion callback function for unregister default
5116  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
5117  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
5118  * decrements the ndlp reference count held for this completion callback
5119  * function. After that, it invokes the lpfc_nlp_not_used() to check
5120  * whether there is only one reference left on the ndlp. If so, it will
5121  * perform one more decrement and trigger the release of the ndlp.
5122  **/
5123 void
5124 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5125 {
5126 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
5127 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
5128 	u32 mbx_flag = pmb->mbox_flag;
5129 	u32 mbx_cmd = pmb->u.mb.mbxCommand;
5130 
5131 	pmb->ctx_buf = NULL;
5132 	pmb->ctx_ndlp = NULL;
5133 
5134 	if (ndlp) {
5135 		lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
5136 				 "0006 rpi x%x DID:%x flg:%x %d x%px "
5137 				 "mbx_cmd x%x mbx_flag x%x x%px\n",
5138 				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5139 				 kref_read(&ndlp->kref), ndlp, mbx_cmd,
5140 				 mbx_flag, pmb);
5141 
5142 		/* This ends the default/temporary RPI cleanup logic for this
5143 		 * ndlp and the node and rpi needs to be released. Free the rpi
5144 		 * first on an UNREG_LOGIN and then release the final
5145 		 * references.
5146 		 */
5147 		spin_lock_irq(&ndlp->lock);
5148 		ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
5149 		if (mbx_cmd == MBX_UNREG_LOGIN)
5150 			ndlp->nlp_flag &= ~NLP_UNREG_INP;
5151 		spin_unlock_irq(&ndlp->lock);
5152 		lpfc_nlp_put(ndlp);
5153 		lpfc_drop_node(ndlp->vport, ndlp);
5154 	}
5155 
5156 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
5157 	kfree(mp);
5158 	mempool_free(pmb, phba->mbox_mem_pool);
5159 	return;
5160 }
5161 
5162 /**
5163  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
5164  * @phba: pointer to lpfc hba data structure.
5165  * @cmdiocb: pointer to lpfc command iocb data structure.
5166  * @rspiocb: pointer to lpfc response iocb data structure.
5167  *
5168  * This routine is the completion callback function for ELS Response IOCB
5169  * command. In normal case, this callback function just properly sets the
5170  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
5171  * field in the command IOCB is not NULL, the referred mailbox command will
5172  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
5173  * the IOCB.
5174  **/
5175 static void
5176 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5177 		  struct lpfc_iocbq *rspiocb)
5178 {
5179 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
5180 	struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
5181 	struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
5182 	IOCB_t  *irsp;
5183 	LPFC_MBOXQ_t *mbox = NULL;
5184 	struct lpfc_dmabuf *mp = NULL;
5185 
5186 	irsp = &rspiocb->iocb;
5187 
5188 	if (!vport) {
5189 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5190 				"3177 ELS response failed\n");
5191 		goto out;
5192 	}
5193 	if (cmdiocb->context_un.mbox)
5194 		mbox = cmdiocb->context_un.mbox;
5195 
5196 	/* Check to see if link went down during discovery */
5197 	if (!ndlp || lpfc_els_chk_latt(vport)) {
5198 		if (mbox) {
5199 			mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
5200 			if (mp) {
5201 				lpfc_mbuf_free(phba, mp->virt, mp->phys);
5202 				kfree(mp);
5203 			}
5204 			mempool_free(mbox, phba->mbox_mem_pool);
5205 		}
5206 		goto out;
5207 	}
5208 
5209 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5210 		"ELS rsp cmpl:    status:x%x/x%x did:x%x",
5211 		irsp->ulpStatus, irsp->un.ulpWord[4],
5212 		cmdiocb->iocb.un.elsreq64.remoteID);
5213 	/* ELS response tag <ulpIoTag> completes */
5214 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5215 			 "0110 ELS response tag x%x completes "
5216 			 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%px\n",
5217 			 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
5218 			 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
5219 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5220 			 ndlp->nlp_rpi, kref_read(&ndlp->kref), mbox);
5221 	if (mbox) {
5222 		if ((rspiocb->iocb.ulpStatus == 0) &&
5223 		    (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
5224 			if (!lpfc_unreg_rpi(vport, ndlp) &&
5225 			    (!(vport->fc_flag & FC_PT2PT))) {
5226 				if (ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE) {
5227 					lpfc_printf_vlog(vport, KERN_INFO,
5228 							 LOG_DISCOVERY,
5229 							 "0314 PLOGI recov "
5230 							 "DID x%x "
5231 							 "Data: x%x x%x x%x\n",
5232 							 ndlp->nlp_DID,
5233 							 ndlp->nlp_state,
5234 							 ndlp->nlp_rpi,
5235 							 ndlp->nlp_flag);
5236 					mp = mbox->ctx_buf;
5237 					if (mp) {
5238 						lpfc_mbuf_free(phba, mp->virt,
5239 							       mp->phys);
5240 						kfree(mp);
5241 					}
5242 					mempool_free(mbox, phba->mbox_mem_pool);
5243 					goto out;
5244 				}
5245 			}
5246 
5247 			/* Increment reference count to ndlp to hold the
5248 			 * reference to ndlp for the callback function.
5249 			 */
5250 			mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
5251 			if (!mbox->ctx_ndlp)
5252 				goto out;
5253 
5254 			mbox->vport = vport;
5255 			if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
5256 				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5257 				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5258 			}
5259 			else {
5260 				mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
5261 				ndlp->nlp_prev_state = ndlp->nlp_state;
5262 				lpfc_nlp_set_state(vport, ndlp,
5263 					   NLP_STE_REG_LOGIN_ISSUE);
5264 			}
5265 
5266 			ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
5267 			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5268 			    != MBX_NOT_FINISHED)
5269 				goto out;
5270 
5271 			/* Decrement the ndlp reference count we
5272 			 * set for this failed mailbox command.
5273 			 */
5274 			lpfc_nlp_put(ndlp);
5275 			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
5276 
5277 			/* ELS rsp: Cannot issue reg_login for <NPortid> */
5278 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5279 				"0138 ELS rsp: Cannot issue reg_login for x%x "
5280 				"Data: x%x x%x x%x\n",
5281 				ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5282 				ndlp->nlp_rpi);
5283 		}
5284 		mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
5285 		if (mp) {
5286 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
5287 			kfree(mp);
5288 		}
5289 		mempool_free(mbox, phba->mbox_mem_pool);
5290 	}
5291 out:
5292 	if (ndlp && shost) {
5293 		spin_lock_irq(&ndlp->lock);
5294 		if (mbox)
5295 			ndlp->nlp_flag &= ~NLP_ACC_REGLOGIN;
5296 		ndlp->nlp_flag &= ~NLP_RM_DFLT_RPI;
5297 		spin_unlock_irq(&ndlp->lock);
5298 	}
5299 
5300 	/* An SLI4 NPIV instance wants to drop the node at this point under
5301 	 * these conditions and release the RPI.
5302 	 */
5303 	if (phba->sli_rev == LPFC_SLI_REV4 &&
5304 	    (vport && vport->port_type == LPFC_NPIV_PORT) &&
5305 	    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD) &&
5306 	    ndlp->nlp_flag & NLP_RELEASE_RPI) {
5307 		lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
5308 		spin_lock_irq(&ndlp->lock);
5309 		ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5310 		ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5311 		spin_unlock_irq(&ndlp->lock);
5312 		lpfc_drop_node(vport, ndlp);
5313 	}
5314 
5315 	/* Release the originating I/O reference. */
5316 	lpfc_els_free_iocb(phba, cmdiocb);
5317 	lpfc_nlp_put(ndlp);
5318 	return;
5319 }
5320 
5321 /**
5322  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
5323  * @vport: pointer to a host virtual N_Port data structure.
5324  * @flag: the els command code to be accepted.
5325  * @oldiocb: pointer to the original lpfc command iocb data structure.
5326  * @ndlp: pointer to a node-list data structure.
5327  * @mbox: pointer to the driver internal queue element for mailbox command.
5328  *
5329  * This routine prepares and issues an Accept (ACC) response IOCB
5330  * command. It uses the @flag to properly set up the IOCB field for the
5331  * specific ACC response command to be issued and invokes the
5332  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
5333  * @mbox pointer is passed in, it will be put into the context_un.mbox
5334  * field of the IOCB for the completion callback function to issue the
5335  * mailbox command to the HBA later when callback is invoked.
5336  *
5337  * Note that the ndlp reference count will be incremented by 1 for holding the
5338  * ndlp and the reference to ndlp will be stored into the context1 field of
5339  * the IOCB for the completion callback function to the corresponding
5340  * response ELS IOCB command.
5341  *
5342  * Return code
5343  *   0 - Successfully issued acc response
5344  *   1 - Failed to issue acc response
5345  **/
5346 int
5347 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
5348 		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5349 		 LPFC_MBOXQ_t *mbox)
5350 {
5351 	struct lpfc_hba  *phba = vport->phba;
5352 	IOCB_t *icmd;
5353 	IOCB_t *oldcmd;
5354 	struct lpfc_iocbq *elsiocb;
5355 	uint8_t *pcmd;
5356 	struct serv_parm *sp;
5357 	uint16_t cmdsize;
5358 	int rc;
5359 	ELS_PKT *els_pkt_ptr;
5360 	struct fc_els_rdf_resp *rdf_resp;
5361 
5362 	oldcmd = &oldiocb->iocb;
5363 
5364 	switch (flag) {
5365 	case ELS_CMD_ACC:
5366 		cmdsize = sizeof(uint32_t);
5367 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5368 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5369 		if (!elsiocb) {
5370 			spin_lock_irq(&ndlp->lock);
5371 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5372 			spin_unlock_irq(&ndlp->lock);
5373 			return 1;
5374 		}
5375 
5376 		icmd = &elsiocb->iocb;
5377 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5378 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5379 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5380 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5381 		pcmd += sizeof(uint32_t);
5382 
5383 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5384 			"Issue ACC:       did:x%x flg:x%x",
5385 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
5386 		break;
5387 	case ELS_CMD_FLOGI:
5388 	case ELS_CMD_PLOGI:
5389 		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
5390 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5391 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5392 		if (!elsiocb)
5393 			return 1;
5394 
5395 		icmd = &elsiocb->iocb;
5396 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5397 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5398 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5399 
5400 		if (mbox)
5401 			elsiocb->context_un.mbox = mbox;
5402 
5403 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5404 		pcmd += sizeof(uint32_t);
5405 		sp = (struct serv_parm *)pcmd;
5406 
5407 		if (flag == ELS_CMD_FLOGI) {
5408 			/* Copy the received service parameters back */
5409 			memcpy(sp, &phba->fc_fabparam,
5410 			       sizeof(struct serv_parm));
5411 
5412 			/* Clear the F_Port bit */
5413 			sp->cmn.fPort = 0;
5414 
5415 			/* Mark all class service parameters as invalid */
5416 			sp->cls1.classValid = 0;
5417 			sp->cls2.classValid = 0;
5418 			sp->cls3.classValid = 0;
5419 			sp->cls4.classValid = 0;
5420 
5421 			/* Copy our worldwide names */
5422 			memcpy(&sp->portName, &vport->fc_sparam.portName,
5423 			       sizeof(struct lpfc_name));
5424 			memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
5425 			       sizeof(struct lpfc_name));
5426 		} else {
5427 			memcpy(pcmd, &vport->fc_sparam,
5428 			       sizeof(struct serv_parm));
5429 
5430 			sp->cmn.valid_vendor_ver_level = 0;
5431 			memset(sp->un.vendorVersion, 0,
5432 			       sizeof(sp->un.vendorVersion));
5433 			sp->cmn.bbRcvSizeMsb &= 0xF;
5434 
5435 			/* If our firmware supports this feature, convey that
5436 			 * info to the target using the vendor specific field.
5437 			 */
5438 			if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
5439 				sp->cmn.valid_vendor_ver_level = 1;
5440 				sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
5441 				sp->un.vv.flags =
5442 					cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
5443 			}
5444 		}
5445 
5446 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5447 			"Issue ACC FLOGI/PLOGI: did:x%x flg:x%x",
5448 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
5449 		break;
5450 	case ELS_CMD_PRLO:
5451 		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
5452 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5453 					     ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
5454 		if (!elsiocb)
5455 			return 1;
5456 
5457 		icmd = &elsiocb->iocb;
5458 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5459 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5460 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5461 
5462 		memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
5463 		       sizeof(uint32_t) + sizeof(PRLO));
5464 		*((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
5465 		els_pkt_ptr = (ELS_PKT *) pcmd;
5466 		els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
5467 
5468 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5469 			"Issue ACC PRLO:  did:x%x flg:x%x",
5470 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
5471 		break;
5472 	case ELS_CMD_RDF:
5473 		cmdsize = sizeof(*rdf_resp);
5474 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5475 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5476 		if (!elsiocb)
5477 			return 1;
5478 
5479 		icmd = &elsiocb->iocb;
5480 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5481 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5482 		pcmd = (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5483 		rdf_resp = (struct fc_els_rdf_resp *)pcmd;
5484 		memset(rdf_resp, 0, sizeof(*rdf_resp));
5485 		rdf_resp->acc_hdr.la_cmd = ELS_LS_ACC;
5486 
5487 		/* FC-LS-5 specifies desc_list_len shall be set to 12 */
5488 		rdf_resp->desc_list_len = cpu_to_be32(12);
5489 
5490 		/* FC-LS-5 specifies LS REQ Information descriptor */
5491 		rdf_resp->lsri.desc_tag = cpu_to_be32(1);
5492 		rdf_resp->lsri.desc_len = cpu_to_be32(sizeof(u32));
5493 		rdf_resp->lsri.rqst_w0.cmd = ELS_RDF;
5494 		break;
5495 	default:
5496 		return 1;
5497 	}
5498 	if (ndlp->nlp_flag & NLP_LOGO_ACC) {
5499 		spin_lock_irq(&ndlp->lock);
5500 		if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED ||
5501 			ndlp->nlp_flag & NLP_REG_LOGIN_SEND))
5502 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5503 		spin_unlock_irq(&ndlp->lock);
5504 		elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
5505 	} else {
5506 		elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5507 	}
5508 
5509 	phba->fc_stat.elsXmitACC++;
5510 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5511 	if (!elsiocb->context1) {
5512 		lpfc_els_free_iocb(phba, elsiocb);
5513 		return 1;
5514 	}
5515 
5516 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5517 	if (rc == IOCB_ERROR) {
5518 		lpfc_els_free_iocb(phba, elsiocb);
5519 		lpfc_nlp_put(ndlp);
5520 		return 1;
5521 	}
5522 
5523 	/* Xmit ELS ACC response tag <ulpIoTag> */
5524 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5525 			 "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
5526 			 "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
5527 			 "RPI: x%x, fc_flag x%x refcnt %d\n",
5528 			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
5529 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5530 			 ndlp->nlp_rpi, vport->fc_flag, kref_read(&ndlp->kref));
5531 	return 0;
5532 }
5533 
5534 /**
5535  * lpfc_els_rsp_reject - Prepare and issue a rjt response iocb command
5536  * @vport: pointer to a virtual N_Port data structure.
5537  * @rejectError: reject response to issue
5538  * @oldiocb: pointer to the original lpfc command iocb data structure.
5539  * @ndlp: pointer to a node-list data structure.
5540  * @mbox: pointer to the driver internal queue element for mailbox command.
5541  *
5542  * This routine prepares and issue an Reject (RJT) response IOCB
5543  * command. If a @mbox pointer is passed in, it will be put into the
5544  * context_un.mbox field of the IOCB for the completion callback function
5545  * to issue to the HBA later.
5546  *
5547  * Note that the ndlp reference count will be incremented by 1 for holding the
5548  * ndlp and the reference to ndlp will be stored into the context1 field of
5549  * the IOCB for the completion callback function to the reject response
5550  * ELS IOCB command.
5551  *
5552  * Return code
5553  *   0 - Successfully issued reject response
5554  *   1 - Failed to issue reject response
5555  **/
5556 int
5557 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
5558 		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5559 		    LPFC_MBOXQ_t *mbox)
5560 {
5561 	int rc;
5562 	struct lpfc_hba  *phba = vport->phba;
5563 	IOCB_t *icmd;
5564 	IOCB_t *oldcmd;
5565 	struct lpfc_iocbq *elsiocb;
5566 	uint8_t *pcmd;
5567 	uint16_t cmdsize;
5568 
5569 	cmdsize = 2 * sizeof(uint32_t);
5570 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5571 				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
5572 	if (!elsiocb)
5573 		return 1;
5574 
5575 	icmd = &elsiocb->iocb;
5576 	oldcmd = &oldiocb->iocb;
5577 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5578 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5579 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5580 
5581 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
5582 	pcmd += sizeof(uint32_t);
5583 	*((uint32_t *) (pcmd)) = rejectError;
5584 
5585 	if (mbox)
5586 		elsiocb->context_un.mbox = mbox;
5587 
5588 	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
5589 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5590 			 "0129 Xmit ELS RJT x%x response tag x%x "
5591 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
5592 			 "rpi x%x\n",
5593 			 rejectError, elsiocb->iotag,
5594 			 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
5595 			 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
5596 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5597 		"Issue LS_RJT:    did:x%x flg:x%x err:x%x",
5598 		ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
5599 
5600 	phba->fc_stat.elsXmitLSRJT++;
5601 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5602 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5603 	if (!elsiocb->context1) {
5604 		lpfc_els_free_iocb(phba, elsiocb);
5605 		return 1;
5606 	}
5607 
5608 	/* The NPIV instance is rejecting this unsolicited ELS. Make sure the
5609 	 * node's assigned RPI gets released provided this node is not already
5610 	 * registered with the transport.
5611 	 */
5612 	if (phba->sli_rev == LPFC_SLI_REV4 &&
5613 	    vport->port_type == LPFC_NPIV_PORT &&
5614 	    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
5615 		spin_lock_irq(&ndlp->lock);
5616 		ndlp->nlp_flag |= NLP_RELEASE_RPI;
5617 		spin_unlock_irq(&ndlp->lock);
5618 	}
5619 
5620 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5621 	if (rc == IOCB_ERROR) {
5622 		lpfc_els_free_iocb(phba, elsiocb);
5623 		lpfc_nlp_put(ndlp);
5624 		return 1;
5625 	}
5626 
5627 	return 0;
5628 }
5629 
5630  /**
5631   * lpfc_issue_els_edc_rsp - Exchange Diagnostic Capabilities with the fabric.
5632   * @vport: pointer to a host virtual N_Port data structure.
5633   * @cmdiocb: pointer to the original lpfc command iocb data structure.
5634   * @ndlp: NPort to where rsp is directed
5635   *
5636   * This routine issues an EDC ACC RSP to the F-Port Controller to communicate
5637   * this N_Port's support of hardware signals in its Congestion
5638   * Capabilities Descriptor.
5639   *
5640   * Return code
5641   *   0 - Successfully issued edc rsp command
5642   *   1 - Failed to issue edc rsp command
5643   **/
5644 static int
5645 lpfc_issue_els_edc_rsp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5646 		       struct lpfc_nodelist *ndlp)
5647 {
5648 	struct lpfc_hba  *phba = vport->phba;
5649 	struct lpfc_els_edc_rsp *edc_rsp;
5650 	struct lpfc_iocbq *elsiocb;
5651 	IOCB_t *icmd, *cmd;
5652 	uint8_t *pcmd;
5653 	int cmdsize, rc;
5654 
5655 	cmdsize = sizeof(struct lpfc_els_edc_rsp);
5656 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, cmdiocb->retry,
5657 				     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5658 	if (!elsiocb)
5659 		return 1;
5660 
5661 	icmd = &elsiocb->iocb;
5662 	cmd = &cmdiocb->iocb;
5663 	icmd->ulpContext = cmd->ulpContext;     /* Xri / rx_id */
5664 	icmd->unsli3.rcvsli3.ox_id = cmd->unsli3.rcvsli3.ox_id;
5665 	pcmd = (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5666 	memset(pcmd, 0, cmdsize);
5667 
5668 	edc_rsp = (struct lpfc_els_edc_rsp *)pcmd;
5669 	edc_rsp->edc_rsp.acc_hdr.la_cmd = ELS_LS_ACC;
5670 	edc_rsp->edc_rsp.desc_list_len = cpu_to_be32(
5671 		FC_TLV_DESC_LENGTH_FROM_SZ(struct lpfc_els_edc_rsp));
5672 	edc_rsp->edc_rsp.lsri.desc_tag = cpu_to_be32(ELS_DTAG_LS_REQ_INFO);
5673 	edc_rsp->edc_rsp.lsri.desc_len = cpu_to_be32(
5674 		FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_els_lsri_desc));
5675 	edc_rsp->edc_rsp.lsri.rqst_w0.cmd = ELS_EDC;
5676 	lpfc_format_edc_cgn_desc(phba, &edc_rsp->cgn_desc);
5677 
5678 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5679 			      "Issue EDC ACC:      did:x%x flg:x%x refcnt %d",
5680 			      ndlp->nlp_DID, ndlp->nlp_flag,
5681 			      kref_read(&ndlp->kref));
5682 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5683 
5684 	phba->fc_stat.elsXmitACC++;
5685 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5686 	if (!elsiocb->context1) {
5687 		lpfc_els_free_iocb(phba, elsiocb);
5688 		return 1;
5689 	}
5690 
5691 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5692 	if (rc == IOCB_ERROR) {
5693 		lpfc_els_free_iocb(phba, elsiocb);
5694 		lpfc_nlp_put(ndlp);
5695 		return 1;
5696 	}
5697 
5698 	/* Xmit ELS ACC response tag <ulpIoTag> */
5699 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5700 			 "0152 Xmit EDC ACC response Status: x%x, IoTag: x%x, "
5701 			 "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
5702 			 "RPI: x%x, fc_flag x%x\n",
5703 			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
5704 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5705 			 ndlp->nlp_rpi, vport->fc_flag);
5706 
5707 	return 0;
5708 }
5709 
5710 /**
5711  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
5712  * @vport: pointer to a virtual N_Port data structure.
5713  * @oldiocb: pointer to the original lpfc command iocb data structure.
5714  * @ndlp: pointer to a node-list data structure.
5715  *
5716  * This routine prepares and issues an Accept (ACC) response to Address
5717  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
5718  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
5719  *
5720  * Note that the ndlp reference count will be incremented by 1 for holding the
5721  * ndlp and the reference to ndlp will be stored into the context1 field of
5722  * the IOCB for the completion callback function to the ADISC Accept response
5723  * ELS IOCB command.
5724  *
5725  * Return code
5726  *   0 - Successfully issued acc adisc response
5727  *   1 - Failed to issue adisc acc response
5728  **/
5729 int
5730 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5731 		       struct lpfc_nodelist *ndlp)
5732 {
5733 	struct lpfc_hba  *phba = vport->phba;
5734 	ADISC *ap;
5735 	IOCB_t *icmd, *oldcmd;
5736 	struct lpfc_iocbq *elsiocb;
5737 	uint8_t *pcmd;
5738 	uint16_t cmdsize;
5739 	int rc;
5740 
5741 	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
5742 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5743 				     ndlp->nlp_DID, ELS_CMD_ACC);
5744 	if (!elsiocb)
5745 		return 1;
5746 
5747 	icmd = &elsiocb->iocb;
5748 	oldcmd = &oldiocb->iocb;
5749 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5750 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5751 
5752 	/* Xmit ADISC ACC response tag <ulpIoTag> */
5753 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5754 			 "0130 Xmit ADISC ACC response iotag x%x xri: "
5755 			 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
5756 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
5757 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5758 			 ndlp->nlp_rpi);
5759 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5760 
5761 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5762 	pcmd += sizeof(uint32_t);
5763 
5764 	ap = (ADISC *) (pcmd);
5765 	ap->hardAL_PA = phba->fc_pref_ALPA;
5766 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
5767 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
5768 	ap->DID = be32_to_cpu(vport->fc_myDID);
5769 
5770 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5771 		      "Issue ACC ADISC: did:x%x flg:x%x refcnt %d",
5772 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
5773 
5774 	phba->fc_stat.elsXmitACC++;
5775 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5776 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5777 	if (!elsiocb->context1) {
5778 		lpfc_els_free_iocb(phba, elsiocb);
5779 		return 1;
5780 	}
5781 
5782 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5783 	if (rc == IOCB_ERROR) {
5784 		lpfc_els_free_iocb(phba, elsiocb);
5785 		lpfc_nlp_put(ndlp);
5786 		return 1;
5787 	}
5788 
5789 	/* Xmit ELS ACC response tag <ulpIoTag> */
5790 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5791 			 "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
5792 			 "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
5793 			 "RPI: x%x, fc_flag x%x\n",
5794 			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
5795 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5796 			 ndlp->nlp_rpi, vport->fc_flag);
5797 	return 0;
5798 }
5799 
5800 /**
5801  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
5802  * @vport: pointer to a virtual N_Port data structure.
5803  * @oldiocb: pointer to the original lpfc command iocb data structure.
5804  * @ndlp: pointer to a node-list data structure.
5805  *
5806  * This routine prepares and issues an Accept (ACC) response to Process
5807  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
5808  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
5809  *
5810  * Note that the ndlp reference count will be incremented by 1 for holding the
5811  * ndlp and the reference to ndlp will be stored into the context1 field of
5812  * the IOCB for the completion callback function to the PRLI Accept response
5813  * ELS IOCB command.
5814  *
5815  * Return code
5816  *   0 - Successfully issued acc prli response
5817  *   1 - Failed to issue acc prli response
5818  **/
5819 int
5820 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5821 		      struct lpfc_nodelist *ndlp)
5822 {
5823 	struct lpfc_hba  *phba = vport->phba;
5824 	PRLI *npr;
5825 	struct lpfc_nvme_prli *npr_nvme;
5826 	lpfc_vpd_t *vpd;
5827 	IOCB_t *icmd;
5828 	IOCB_t *oldcmd;
5829 	struct lpfc_iocbq *elsiocb;
5830 	uint8_t *pcmd;
5831 	uint16_t cmdsize;
5832 	uint32_t prli_fc4_req, *req_payload;
5833 	struct lpfc_dmabuf *req_buf;
5834 	int rc;
5835 	u32 elsrspcmd;
5836 
5837 	/* Need the incoming PRLI payload to determine if the ACC is for an
5838 	 * FC4 or NVME PRLI type.  The PRLI type is at word 1.
5839 	 */
5840 	req_buf = (struct lpfc_dmabuf *)oldiocb->context2;
5841 	req_payload = (((uint32_t *)req_buf->virt) + 1);
5842 
5843 	/* PRLI type payload is at byte 3 for FCP or NVME. */
5844 	prli_fc4_req = be32_to_cpu(*req_payload);
5845 	prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
5846 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5847 			 "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
5848 			 prli_fc4_req, *((uint32_t *)req_payload));
5849 
5850 	if (prli_fc4_req == PRLI_FCP_TYPE) {
5851 		cmdsize = sizeof(uint32_t) + sizeof(PRLI);
5852 		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
5853 	} else if (prli_fc4_req & PRLI_NVME_TYPE) {
5854 		cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
5855 		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
5856 	} else {
5857 		return 1;
5858 	}
5859 
5860 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5861 		ndlp->nlp_DID, elsrspcmd);
5862 	if (!elsiocb)
5863 		return 1;
5864 
5865 	icmd = &elsiocb->iocb;
5866 	oldcmd = &oldiocb->iocb;
5867 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5868 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5869 
5870 	/* Xmit PRLI ACC response tag <ulpIoTag> */
5871 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5872 			 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
5873 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
5874 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
5875 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5876 			 ndlp->nlp_rpi);
5877 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5878 	memset(pcmd, 0, cmdsize);
5879 
5880 	*((uint32_t *)(pcmd)) = elsrspcmd;
5881 	pcmd += sizeof(uint32_t);
5882 
5883 	/* For PRLI, remainder of payload is PRLI parameter page */
5884 	vpd = &phba->vpd;
5885 
5886 	if (prli_fc4_req == PRLI_FCP_TYPE) {
5887 		/*
5888 		 * If the remote port is a target and our firmware version
5889 		 * is 3.20 or later, set the following bits for FC-TAPE
5890 		 * support.
5891 		 */
5892 		npr = (PRLI *) pcmd;
5893 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
5894 		    (vpd->rev.feaLevelHigh >= 0x02)) {
5895 			npr->ConfmComplAllowed = 1;
5896 			npr->Retry = 1;
5897 			npr->TaskRetryIdReq = 1;
5898 		}
5899 		npr->acceptRspCode = PRLI_REQ_EXECUTED;
5900 		npr->estabImagePair = 1;
5901 		npr->readXferRdyDis = 1;
5902 		npr->ConfmComplAllowed = 1;
5903 		npr->prliType = PRLI_FCP_TYPE;
5904 		npr->initiatorFunc = 1;
5905 	} else if (prli_fc4_req & PRLI_NVME_TYPE) {
5906 		/* Respond with an NVME PRLI Type */
5907 		npr_nvme = (struct lpfc_nvme_prli *) pcmd;
5908 		bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
5909 		bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
5910 		bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
5911 		if (phba->nvmet_support) {
5912 			bf_set(prli_tgt, npr_nvme, 1);
5913 			bf_set(prli_disc, npr_nvme, 1);
5914 			if (phba->cfg_nvme_enable_fb) {
5915 				bf_set(prli_fba, npr_nvme, 1);
5916 
5917 				/* TBD.  Target mode needs to post buffers
5918 				 * that support the configured first burst
5919 				 * byte size.
5920 				 */
5921 				bf_set(prli_fb_sz, npr_nvme,
5922 				       phba->cfg_nvmet_fb_size);
5923 			}
5924 		} else {
5925 			bf_set(prli_init, npr_nvme, 1);
5926 		}
5927 
5928 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
5929 				 "6015 NVME issue PRLI ACC word1 x%08x "
5930 				 "word4 x%08x word5 x%08x flag x%x, "
5931 				 "fcp_info x%x nlp_type x%x\n",
5932 				 npr_nvme->word1, npr_nvme->word4,
5933 				 npr_nvme->word5, ndlp->nlp_flag,
5934 				 ndlp->nlp_fcp_info, ndlp->nlp_type);
5935 		npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
5936 		npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
5937 		npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
5938 	} else
5939 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5940 				 "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
5941 				 prli_fc4_req, ndlp->nlp_fc4_type,
5942 				 ndlp->nlp_DID);
5943 
5944 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5945 		      "Issue ACC PRLI:  did:x%x flg:x%x",
5946 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
5947 
5948 	phba->fc_stat.elsXmitACC++;
5949 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5950 	elsiocb->context1 =  lpfc_nlp_get(ndlp);
5951 	if (!elsiocb->context1) {
5952 		lpfc_els_free_iocb(phba, elsiocb);
5953 		return 1;
5954 	}
5955 
5956 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5957 	if (rc == IOCB_ERROR) {
5958 		lpfc_els_free_iocb(phba, elsiocb);
5959 		lpfc_nlp_put(ndlp);
5960 		return 1;
5961 	}
5962 
5963 	return 0;
5964 }
5965 
5966 /**
5967  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
5968  * @vport: pointer to a virtual N_Port data structure.
5969  * @format: rnid command format.
5970  * @oldiocb: pointer to the original lpfc command iocb data structure.
5971  * @ndlp: pointer to a node-list data structure.
5972  *
5973  * This routine issues a Request Node Identification Data (RNID) Accept
5974  * (ACC) response. It constructs the RNID ACC response command according to
5975  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
5976  * issue the response.
5977  *
5978  * Note that the ndlp reference count will be incremented by 1 for holding the
5979  * ndlp and the reference to ndlp will be stored into the context1 field of
5980  * the IOCB for the completion callback function.
5981  *
5982  * Return code
5983  *   0 - Successfully issued acc rnid response
5984  *   1 - Failed to issue acc rnid response
5985  **/
5986 static int
5987 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
5988 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
5989 {
5990 	struct lpfc_hba  *phba = vport->phba;
5991 	RNID *rn;
5992 	IOCB_t *icmd, *oldcmd;
5993 	struct lpfc_iocbq *elsiocb;
5994 	uint8_t *pcmd;
5995 	uint16_t cmdsize;
5996 	int rc;
5997 
5998 	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
5999 					+ (2 * sizeof(struct lpfc_name));
6000 	if (format)
6001 		cmdsize += sizeof(RNID_TOP_DISC);
6002 
6003 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6004 				     ndlp->nlp_DID, ELS_CMD_ACC);
6005 	if (!elsiocb)
6006 		return 1;
6007 
6008 	icmd = &elsiocb->iocb;
6009 	oldcmd = &oldiocb->iocb;
6010 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
6011 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
6012 
6013 	/* Xmit RNID ACC response tag <ulpIoTag> */
6014 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6015 			 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
6016 			 elsiocb->iotag, elsiocb->iocb.ulpContext);
6017 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6018 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6019 	pcmd += sizeof(uint32_t);
6020 
6021 	memset(pcmd, 0, sizeof(RNID));
6022 	rn = (RNID *) (pcmd);
6023 	rn->Format = format;
6024 	rn->CommonLen = (2 * sizeof(struct lpfc_name));
6025 	memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
6026 	memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
6027 	switch (format) {
6028 	case 0:
6029 		rn->SpecificLen = 0;
6030 		break;
6031 	case RNID_TOPOLOGY_DISC:
6032 		rn->SpecificLen = sizeof(RNID_TOP_DISC);
6033 		memcpy(&rn->un.topologyDisc.portName,
6034 		       &vport->fc_portname, sizeof(struct lpfc_name));
6035 		rn->un.topologyDisc.unitType = RNID_HBA;
6036 		rn->un.topologyDisc.physPort = 0;
6037 		rn->un.topologyDisc.attachedNodes = 0;
6038 		break;
6039 	default:
6040 		rn->CommonLen = 0;
6041 		rn->SpecificLen = 0;
6042 		break;
6043 	}
6044 
6045 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6046 		      "Issue ACC RNID:  did:x%x flg:x%x refcnt %d",
6047 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6048 
6049 	phba->fc_stat.elsXmitACC++;
6050 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6051 	elsiocb->context1 = lpfc_nlp_get(ndlp);
6052 	if (!elsiocb->context1) {
6053 		lpfc_els_free_iocb(phba, elsiocb);
6054 		return 1;
6055 	}
6056 
6057 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6058 	if (rc == IOCB_ERROR) {
6059 		lpfc_els_free_iocb(phba, elsiocb);
6060 		lpfc_nlp_put(ndlp);
6061 		return 1;
6062 	}
6063 
6064 	return 0;
6065 }
6066 
6067 /**
6068  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
6069  * @vport: pointer to a virtual N_Port data structure.
6070  * @iocb: pointer to the lpfc command iocb data structure.
6071  * @ndlp: pointer to a node-list data structure.
6072  *
6073  * Return
6074  **/
6075 static void
6076 lpfc_els_clear_rrq(struct lpfc_vport *vport,
6077 		   struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
6078 {
6079 	struct lpfc_hba  *phba = vport->phba;
6080 	uint8_t *pcmd;
6081 	struct RRQ *rrq;
6082 	uint16_t rxid;
6083 	uint16_t xri;
6084 	struct lpfc_node_rrq *prrq;
6085 
6086 
6087 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
6088 	pcmd += sizeof(uint32_t);
6089 	rrq = (struct RRQ *)pcmd;
6090 	rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
6091 	rxid = bf_get(rrq_rxid, rrq);
6092 
6093 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6094 			"2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
6095 			" x%x x%x\n",
6096 			be32_to_cpu(bf_get(rrq_did, rrq)),
6097 			bf_get(rrq_oxid, rrq),
6098 			rxid,
6099 			iocb->iotag, iocb->iocb.ulpContext);
6100 
6101 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6102 		"Clear RRQ:  did:x%x flg:x%x exchg:x%.08x",
6103 		ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
6104 	if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
6105 		xri = bf_get(rrq_oxid, rrq);
6106 	else
6107 		xri = rxid;
6108 	prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
6109 	if (prrq)
6110 		lpfc_clr_rrq_active(phba, xri, prrq);
6111 	return;
6112 }
6113 
6114 /**
6115  * lpfc_els_rsp_echo_acc - Issue echo acc response
6116  * @vport: pointer to a virtual N_Port data structure.
6117  * @data: pointer to echo data to return in the accept.
6118  * @oldiocb: pointer to the original lpfc command iocb data structure.
6119  * @ndlp: pointer to a node-list data structure.
6120  *
6121  * Return code
6122  *   0 - Successfully issued acc echo response
6123  *   1 - Failed to issue acc echo response
6124  **/
6125 static int
6126 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
6127 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6128 {
6129 	struct lpfc_hba  *phba = vport->phba;
6130 	struct lpfc_iocbq *elsiocb;
6131 	uint8_t *pcmd;
6132 	uint16_t cmdsize;
6133 	int rc;
6134 
6135 	cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
6136 
6137 	/* The accumulated length can exceed the BPL_SIZE.  For
6138 	 * now, use this as the limit
6139 	 */
6140 	if (cmdsize > LPFC_BPL_SIZE)
6141 		cmdsize = LPFC_BPL_SIZE;
6142 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6143 				     ndlp->nlp_DID, ELS_CMD_ACC);
6144 	if (!elsiocb)
6145 		return 1;
6146 
6147 	elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext;  /* Xri / rx_id */
6148 	elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
6149 
6150 	/* Xmit ECHO ACC response tag <ulpIoTag> */
6151 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6152 			 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
6153 			 elsiocb->iotag, elsiocb->iocb.ulpContext);
6154 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6155 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6156 	pcmd += sizeof(uint32_t);
6157 	memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
6158 
6159 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6160 		      "Issue ACC ECHO:  did:x%x flg:x%x refcnt %d",
6161 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6162 
6163 	phba->fc_stat.elsXmitACC++;
6164 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6165 	elsiocb->context1 =  lpfc_nlp_get(ndlp);
6166 	if (!elsiocb->context1) {
6167 		lpfc_els_free_iocb(phba, elsiocb);
6168 		return 1;
6169 	}
6170 
6171 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6172 	if (rc == IOCB_ERROR) {
6173 		lpfc_els_free_iocb(phba, elsiocb);
6174 		lpfc_nlp_put(ndlp);
6175 		return 1;
6176 	}
6177 
6178 	return 0;
6179 }
6180 
6181 /**
6182  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
6183  * @vport: pointer to a host virtual N_Port data structure.
6184  *
6185  * This routine issues Address Discover (ADISC) ELS commands to those
6186  * N_Ports which are in node port recovery state and ADISC has not been issued
6187  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
6188  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
6189  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
6190  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
6191  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
6192  * IOCBs quit for later pick up. On the other hand, after walking through
6193  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
6194  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
6195  * no more ADISC need to be sent.
6196  *
6197  * Return code
6198  *    The number of N_Ports with adisc issued.
6199  **/
6200 int
6201 lpfc_els_disc_adisc(struct lpfc_vport *vport)
6202 {
6203 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6204 	struct lpfc_nodelist *ndlp, *next_ndlp;
6205 	int sentadisc = 0;
6206 
6207 	/* go thru NPR nodes and issue any remaining ELS ADISCs */
6208 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6209 
6210 		if (ndlp->nlp_state != NLP_STE_NPR_NODE ||
6211 		    !(ndlp->nlp_flag & NLP_NPR_ADISC))
6212 			continue;
6213 
6214 		spin_lock_irq(&ndlp->lock);
6215 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
6216 		spin_unlock_irq(&ndlp->lock);
6217 
6218 		if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
6219 			/* This node was marked for ADISC but was not picked
6220 			 * for discovery. This is possible if the node was
6221 			 * missing in gidft response.
6222 			 *
6223 			 * At time of marking node for ADISC, we skipped unreg
6224 			 * from backend
6225 			 */
6226 			lpfc_nlp_unreg_node(vport, ndlp);
6227 			lpfc_unreg_rpi(vport, ndlp);
6228 			continue;
6229 		}
6230 
6231 		ndlp->nlp_prev_state = ndlp->nlp_state;
6232 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
6233 		lpfc_issue_els_adisc(vport, ndlp, 0);
6234 		sentadisc++;
6235 		vport->num_disc_nodes++;
6236 		if (vport->num_disc_nodes >=
6237 				vport->cfg_discovery_threads) {
6238 			spin_lock_irq(shost->host_lock);
6239 			vport->fc_flag |= FC_NLP_MORE;
6240 			spin_unlock_irq(shost->host_lock);
6241 			break;
6242 		}
6243 
6244 	}
6245 	if (sentadisc == 0) {
6246 		spin_lock_irq(shost->host_lock);
6247 		vport->fc_flag &= ~FC_NLP_MORE;
6248 		spin_unlock_irq(shost->host_lock);
6249 	}
6250 	return sentadisc;
6251 }
6252 
6253 /**
6254  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
6255  * @vport: pointer to a host virtual N_Port data structure.
6256  *
6257  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
6258  * which are in node port recovery state, with a @vport. Each time an ELS
6259  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
6260  * the per @vport number of discover count (num_disc_nodes) shall be
6261  * incremented. If the num_disc_nodes reaches a pre-configured threshold
6262  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
6263  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
6264  * later pick up. On the other hand, after walking through all the ndlps with
6265  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
6266  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
6267  * PLOGI need to be sent.
6268  *
6269  * Return code
6270  *   The number of N_Ports with plogi issued.
6271  **/
6272 int
6273 lpfc_els_disc_plogi(struct lpfc_vport *vport)
6274 {
6275 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6276 	struct lpfc_nodelist *ndlp, *next_ndlp;
6277 	int sentplogi = 0;
6278 
6279 	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
6280 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6281 		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
6282 				(ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
6283 				(ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
6284 				(ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
6285 			ndlp->nlp_prev_state = ndlp->nlp_state;
6286 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6287 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
6288 			sentplogi++;
6289 			vport->num_disc_nodes++;
6290 			if (vport->num_disc_nodes >=
6291 					vport->cfg_discovery_threads) {
6292 				spin_lock_irq(shost->host_lock);
6293 				vport->fc_flag |= FC_NLP_MORE;
6294 				spin_unlock_irq(shost->host_lock);
6295 				break;
6296 			}
6297 		}
6298 	}
6299 
6300 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6301 			 "6452 Discover PLOGI %d flag x%x\n",
6302 			 sentplogi, vport->fc_flag);
6303 
6304 	if (sentplogi) {
6305 		lpfc_set_disctmo(vport);
6306 	}
6307 	else {
6308 		spin_lock_irq(shost->host_lock);
6309 		vport->fc_flag &= ~FC_NLP_MORE;
6310 		spin_unlock_irq(shost->host_lock);
6311 	}
6312 	return sentplogi;
6313 }
6314 
6315 static uint32_t
6316 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
6317 		uint32_t word0)
6318 {
6319 
6320 	desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
6321 	desc->payload.els_req = word0;
6322 	desc->length = cpu_to_be32(sizeof(desc->payload));
6323 
6324 	return sizeof(struct fc_rdp_link_service_desc);
6325 }
6326 
6327 static uint32_t
6328 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
6329 		uint8_t *page_a0, uint8_t *page_a2)
6330 {
6331 	uint16_t wavelength;
6332 	uint16_t temperature;
6333 	uint16_t rx_power;
6334 	uint16_t tx_bias;
6335 	uint16_t tx_power;
6336 	uint16_t vcc;
6337 	uint16_t flag = 0;
6338 	struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
6339 	struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
6340 
6341 	desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
6342 
6343 	trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
6344 			&page_a0[SSF_TRANSCEIVER_CODE_B4];
6345 	trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
6346 			&page_a0[SSF_TRANSCEIVER_CODE_B5];
6347 
6348 	if ((trasn_code_byte4->fc_sw_laser) ||
6349 	    (trasn_code_byte5->fc_sw_laser_sl) ||
6350 	    (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
6351 		flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
6352 	} else if (trasn_code_byte4->fc_lw_laser) {
6353 		wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
6354 			page_a0[SSF_WAVELENGTH_B0];
6355 		if (wavelength == SFP_WAVELENGTH_LC1310)
6356 			flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
6357 		if (wavelength == SFP_WAVELENGTH_LL1550)
6358 			flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
6359 	}
6360 	/* check if its SFP+ */
6361 	flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
6362 			SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
6363 					<< SFP_FLAG_CT_SHIFT;
6364 
6365 	/* check if its OPTICAL */
6366 	flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
6367 			SFP_FLAG_IS_OPTICAL_PORT : 0)
6368 					<< SFP_FLAG_IS_OPTICAL_SHIFT;
6369 
6370 	temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
6371 		page_a2[SFF_TEMPERATURE_B0]);
6372 	vcc = (page_a2[SFF_VCC_B1] << 8 |
6373 		page_a2[SFF_VCC_B0]);
6374 	tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
6375 		page_a2[SFF_TXPOWER_B0]);
6376 	tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
6377 		page_a2[SFF_TX_BIAS_CURRENT_B0]);
6378 	rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
6379 		page_a2[SFF_RXPOWER_B0]);
6380 	desc->sfp_info.temperature = cpu_to_be16(temperature);
6381 	desc->sfp_info.rx_power = cpu_to_be16(rx_power);
6382 	desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
6383 	desc->sfp_info.tx_power = cpu_to_be16(tx_power);
6384 	desc->sfp_info.vcc = cpu_to_be16(vcc);
6385 
6386 	desc->sfp_info.flags = cpu_to_be16(flag);
6387 	desc->length = cpu_to_be32(sizeof(desc->sfp_info));
6388 
6389 	return sizeof(struct fc_rdp_sfp_desc);
6390 }
6391 
6392 static uint32_t
6393 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
6394 		READ_LNK_VAR *stat)
6395 {
6396 	uint32_t type;
6397 
6398 	desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
6399 
6400 	type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
6401 
6402 	desc->info.port_type = cpu_to_be32(type);
6403 
6404 	desc->info.link_status.link_failure_cnt =
6405 		cpu_to_be32(stat->linkFailureCnt);
6406 	desc->info.link_status.loss_of_synch_cnt =
6407 		cpu_to_be32(stat->lossSyncCnt);
6408 	desc->info.link_status.loss_of_signal_cnt =
6409 		cpu_to_be32(stat->lossSignalCnt);
6410 	desc->info.link_status.primitive_seq_proto_err =
6411 		cpu_to_be32(stat->primSeqErrCnt);
6412 	desc->info.link_status.invalid_trans_word =
6413 		cpu_to_be32(stat->invalidXmitWord);
6414 	desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
6415 
6416 	desc->length = cpu_to_be32(sizeof(desc->info));
6417 
6418 	return sizeof(struct fc_rdp_link_error_status_desc);
6419 }
6420 
6421 static uint32_t
6422 lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
6423 		      struct lpfc_vport *vport)
6424 {
6425 	uint32_t bbCredit;
6426 
6427 	desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
6428 
6429 	bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
6430 			(vport->fc_sparam.cmn.bbCreditMsb << 8);
6431 	desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
6432 	if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
6433 		bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
6434 			(vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
6435 		desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
6436 	} else {
6437 		desc->bbc_info.attached_port_bbc = 0;
6438 	}
6439 
6440 	desc->bbc_info.rtt = 0;
6441 	desc->length = cpu_to_be32(sizeof(desc->bbc_info));
6442 
6443 	return sizeof(struct fc_rdp_bbc_desc);
6444 }
6445 
6446 static uint32_t
6447 lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
6448 			   struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
6449 {
6450 	uint32_t flags = 0;
6451 
6452 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6453 
6454 	desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
6455 	desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
6456 	desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
6457 	desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
6458 
6459 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6460 		flags |= RDP_OET_HIGH_ALARM;
6461 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6462 		flags |= RDP_OET_LOW_ALARM;
6463 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6464 		flags |= RDP_OET_HIGH_WARNING;
6465 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6466 		flags |= RDP_OET_LOW_WARNING;
6467 
6468 	flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
6469 	desc->oed_info.function_flags = cpu_to_be32(flags);
6470 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6471 	return sizeof(struct fc_rdp_oed_sfp_desc);
6472 }
6473 
6474 static uint32_t
6475 lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
6476 			      struct fc_rdp_oed_sfp_desc *desc,
6477 			      uint8_t *page_a2)
6478 {
6479 	uint32_t flags = 0;
6480 
6481 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6482 
6483 	desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
6484 	desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
6485 	desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
6486 	desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
6487 
6488 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6489 		flags |= RDP_OET_HIGH_ALARM;
6490 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6491 		flags |= RDP_OET_LOW_ALARM;
6492 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6493 		flags |= RDP_OET_HIGH_WARNING;
6494 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6495 		flags |= RDP_OET_LOW_WARNING;
6496 
6497 	flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
6498 	desc->oed_info.function_flags = cpu_to_be32(flags);
6499 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6500 	return sizeof(struct fc_rdp_oed_sfp_desc);
6501 }
6502 
6503 static uint32_t
6504 lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
6505 			     struct fc_rdp_oed_sfp_desc *desc,
6506 			     uint8_t *page_a2)
6507 {
6508 	uint32_t flags = 0;
6509 
6510 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6511 
6512 	desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
6513 	desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
6514 	desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
6515 	desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
6516 
6517 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6518 		flags |= RDP_OET_HIGH_ALARM;
6519 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
6520 		flags |= RDP_OET_LOW_ALARM;
6521 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6522 		flags |= RDP_OET_HIGH_WARNING;
6523 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
6524 		flags |= RDP_OET_LOW_WARNING;
6525 
6526 	flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
6527 	desc->oed_info.function_flags = cpu_to_be32(flags);
6528 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6529 	return sizeof(struct fc_rdp_oed_sfp_desc);
6530 }
6531 
6532 static uint32_t
6533 lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
6534 			      struct fc_rdp_oed_sfp_desc *desc,
6535 			      uint8_t *page_a2)
6536 {
6537 	uint32_t flags = 0;
6538 
6539 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6540 
6541 	desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
6542 	desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
6543 	desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
6544 	desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
6545 
6546 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6547 		flags |= RDP_OET_HIGH_ALARM;
6548 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
6549 		flags |= RDP_OET_LOW_ALARM;
6550 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6551 		flags |= RDP_OET_HIGH_WARNING;
6552 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
6553 		flags |= RDP_OET_LOW_WARNING;
6554 
6555 	flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
6556 	desc->oed_info.function_flags = cpu_to_be32(flags);
6557 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6558 	return sizeof(struct fc_rdp_oed_sfp_desc);
6559 }
6560 
6561 
6562 static uint32_t
6563 lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
6564 			      struct fc_rdp_oed_sfp_desc *desc,
6565 			      uint8_t *page_a2)
6566 {
6567 	uint32_t flags = 0;
6568 
6569 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6570 
6571 	desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
6572 	desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
6573 	desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
6574 	desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
6575 
6576 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6577 		flags |= RDP_OET_HIGH_ALARM;
6578 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
6579 		flags |= RDP_OET_LOW_ALARM;
6580 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6581 		flags |= RDP_OET_HIGH_WARNING;
6582 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
6583 		flags |= RDP_OET_LOW_WARNING;
6584 
6585 	flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
6586 	desc->oed_info.function_flags = cpu_to_be32(flags);
6587 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6588 	return sizeof(struct fc_rdp_oed_sfp_desc);
6589 }
6590 
6591 static uint32_t
6592 lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
6593 		      uint8_t *page_a0, struct lpfc_vport *vport)
6594 {
6595 	desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
6596 	memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
6597 	memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
6598 	memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
6599 	memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
6600 	memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
6601 	desc->length = cpu_to_be32(sizeof(desc->opd_info));
6602 	return sizeof(struct fc_rdp_opd_sfp_desc);
6603 }
6604 
6605 static uint32_t
6606 lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
6607 {
6608 	if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
6609 		return 0;
6610 	desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
6611 
6612 	desc->info.CorrectedBlocks =
6613 		cpu_to_be32(stat->fecCorrBlkCount);
6614 	desc->info.UncorrectableBlocks =
6615 		cpu_to_be32(stat->fecUncorrBlkCount);
6616 
6617 	desc->length = cpu_to_be32(sizeof(desc->info));
6618 
6619 	return sizeof(struct fc_fec_rdp_desc);
6620 }
6621 
6622 static uint32_t
6623 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
6624 {
6625 	uint16_t rdp_cap = 0;
6626 	uint16_t rdp_speed;
6627 
6628 	desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
6629 
6630 	switch (phba->fc_linkspeed) {
6631 	case LPFC_LINK_SPEED_1GHZ:
6632 		rdp_speed = RDP_PS_1GB;
6633 		break;
6634 	case LPFC_LINK_SPEED_2GHZ:
6635 		rdp_speed = RDP_PS_2GB;
6636 		break;
6637 	case LPFC_LINK_SPEED_4GHZ:
6638 		rdp_speed = RDP_PS_4GB;
6639 		break;
6640 	case LPFC_LINK_SPEED_8GHZ:
6641 		rdp_speed = RDP_PS_8GB;
6642 		break;
6643 	case LPFC_LINK_SPEED_10GHZ:
6644 		rdp_speed = RDP_PS_10GB;
6645 		break;
6646 	case LPFC_LINK_SPEED_16GHZ:
6647 		rdp_speed = RDP_PS_16GB;
6648 		break;
6649 	case LPFC_LINK_SPEED_32GHZ:
6650 		rdp_speed = RDP_PS_32GB;
6651 		break;
6652 	case LPFC_LINK_SPEED_64GHZ:
6653 		rdp_speed = RDP_PS_64GB;
6654 		break;
6655 	case LPFC_LINK_SPEED_128GHZ:
6656 		rdp_speed = RDP_PS_128GB;
6657 		break;
6658 	case LPFC_LINK_SPEED_256GHZ:
6659 		rdp_speed = RDP_PS_256GB;
6660 		break;
6661 	default:
6662 		rdp_speed = RDP_PS_UNKNOWN;
6663 		break;
6664 	}
6665 
6666 	desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
6667 
6668 	if (phba->lmt & LMT_256Gb)
6669 		rdp_cap |= RDP_PS_256GB;
6670 	if (phba->lmt & LMT_128Gb)
6671 		rdp_cap |= RDP_PS_128GB;
6672 	if (phba->lmt & LMT_64Gb)
6673 		rdp_cap |= RDP_PS_64GB;
6674 	if (phba->lmt & LMT_32Gb)
6675 		rdp_cap |= RDP_PS_32GB;
6676 	if (phba->lmt & LMT_16Gb)
6677 		rdp_cap |= RDP_PS_16GB;
6678 	if (phba->lmt & LMT_10Gb)
6679 		rdp_cap |= RDP_PS_10GB;
6680 	if (phba->lmt & LMT_8Gb)
6681 		rdp_cap |= RDP_PS_8GB;
6682 	if (phba->lmt & LMT_4Gb)
6683 		rdp_cap |= RDP_PS_4GB;
6684 	if (phba->lmt & LMT_2Gb)
6685 		rdp_cap |= RDP_PS_2GB;
6686 	if (phba->lmt & LMT_1Gb)
6687 		rdp_cap |= RDP_PS_1GB;
6688 
6689 	if (rdp_cap == 0)
6690 		rdp_cap = RDP_CAP_UNKNOWN;
6691 	if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
6692 		rdp_cap |= RDP_CAP_USER_CONFIGURED;
6693 
6694 	desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
6695 	desc->length = cpu_to_be32(sizeof(desc->info));
6696 	return sizeof(struct fc_rdp_port_speed_desc);
6697 }
6698 
6699 static uint32_t
6700 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
6701 		struct lpfc_vport *vport)
6702 {
6703 
6704 	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
6705 
6706 	memcpy(desc->port_names.wwnn, &vport->fc_nodename,
6707 			sizeof(desc->port_names.wwnn));
6708 
6709 	memcpy(desc->port_names.wwpn, &vport->fc_portname,
6710 			sizeof(desc->port_names.wwpn));
6711 
6712 	desc->length = cpu_to_be32(sizeof(desc->port_names));
6713 	return sizeof(struct fc_rdp_port_name_desc);
6714 }
6715 
6716 static uint32_t
6717 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
6718 		struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
6719 {
6720 
6721 	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
6722 	if (vport->fc_flag & FC_FABRIC) {
6723 		memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
6724 		       sizeof(desc->port_names.wwnn));
6725 
6726 		memcpy(desc->port_names.wwpn, &vport->fabric_portname,
6727 		       sizeof(desc->port_names.wwpn));
6728 	} else {  /* Point to Point */
6729 		memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
6730 		       sizeof(desc->port_names.wwnn));
6731 
6732 		memcpy(desc->port_names.wwpn, &ndlp->nlp_portname,
6733 		       sizeof(desc->port_names.wwpn));
6734 	}
6735 
6736 	desc->length = cpu_to_be32(sizeof(desc->port_names));
6737 	return sizeof(struct fc_rdp_port_name_desc);
6738 }
6739 
6740 static void
6741 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
6742 		int status)
6743 {
6744 	struct lpfc_nodelist *ndlp = rdp_context->ndlp;
6745 	struct lpfc_vport *vport = ndlp->vport;
6746 	struct lpfc_iocbq *elsiocb;
6747 	struct ulp_bde64 *bpl;
6748 	IOCB_t *icmd;
6749 	uint8_t *pcmd;
6750 	struct ls_rjt *stat;
6751 	struct fc_rdp_res_frame *rdp_res;
6752 	uint32_t cmdsize, len;
6753 	uint16_t *flag_ptr;
6754 	int rc;
6755 
6756 	if (status != SUCCESS)
6757 		goto error;
6758 
6759 	/* This will change once we know the true size of the RDP payload */
6760 	cmdsize = sizeof(struct fc_rdp_res_frame);
6761 
6762 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
6763 			lpfc_max_els_tries, rdp_context->ndlp,
6764 			rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
6765 	if (!elsiocb)
6766 		goto free_rdp_context;
6767 
6768 	icmd = &elsiocb->iocb;
6769 	icmd->ulpContext = rdp_context->rx_id;
6770 	icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
6771 
6772 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6773 			"2171 Xmit RDP response tag x%x xri x%x, "
6774 			"did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x",
6775 			elsiocb->iotag, elsiocb->iocb.ulpContext,
6776 			ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6777 			ndlp->nlp_rpi);
6778 	rdp_res = (struct fc_rdp_res_frame *)
6779 		(((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6780 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6781 	memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
6782 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6783 
6784 	/* Update Alarm and Warning */
6785 	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
6786 	phba->sfp_alarm |= *flag_ptr;
6787 	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
6788 	phba->sfp_warning |= *flag_ptr;
6789 
6790 	/* For RDP payload */
6791 	len = 8;
6792 	len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
6793 					 (len + pcmd), ELS_CMD_RDP);
6794 
6795 	len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
6796 			rdp_context->page_a0, rdp_context->page_a2);
6797 	len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
6798 				  phba);
6799 	len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
6800 				       (len + pcmd), &rdp_context->link_stat);
6801 	len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
6802 					     (len + pcmd), vport);
6803 	len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
6804 					(len + pcmd), vport, ndlp);
6805 	len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
6806 			&rdp_context->link_stat);
6807 	len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
6808 				     &rdp_context->link_stat, vport);
6809 	len += lpfc_rdp_res_oed_temp_desc(phba,
6810 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6811 				rdp_context->page_a2);
6812 	len += lpfc_rdp_res_oed_voltage_desc(phba,
6813 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6814 				rdp_context->page_a2);
6815 	len += lpfc_rdp_res_oed_txbias_desc(phba,
6816 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6817 				rdp_context->page_a2);
6818 	len += lpfc_rdp_res_oed_txpower_desc(phba,
6819 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6820 				rdp_context->page_a2);
6821 	len += lpfc_rdp_res_oed_rxpower_desc(phba,
6822 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6823 				rdp_context->page_a2);
6824 	len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
6825 				     rdp_context->page_a0, vport);
6826 
6827 	rdp_res->length = cpu_to_be32(len - 8);
6828 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6829 
6830 	/* Now that we know the true size of the payload, update the BPL */
6831 	bpl = (struct ulp_bde64 *)
6832 		(((struct lpfc_dmabuf *)(elsiocb->context3))->virt);
6833 	bpl->tus.f.bdeSize = len;
6834 	bpl->tus.f.bdeFlags = 0;
6835 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
6836 
6837 	phba->fc_stat.elsXmitACC++;
6838 	elsiocb->context1 = lpfc_nlp_get(ndlp);
6839 	if (!elsiocb->context1) {
6840 		lpfc_els_free_iocb(phba, elsiocb);
6841 		goto free_rdp_context;
6842 	}
6843 
6844 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6845 	if (rc == IOCB_ERROR) {
6846 		lpfc_els_free_iocb(phba, elsiocb);
6847 		lpfc_nlp_put(ndlp);
6848 	}
6849 
6850 	goto free_rdp_context;
6851 
6852 error:
6853 	cmdsize = 2 * sizeof(uint32_t);
6854 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
6855 			ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
6856 	if (!elsiocb)
6857 		goto free_rdp_context;
6858 
6859 	icmd = &elsiocb->iocb;
6860 	icmd->ulpContext = rdp_context->rx_id;
6861 	icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
6862 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6863 
6864 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
6865 	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
6866 	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6867 
6868 	phba->fc_stat.elsXmitLSRJT++;
6869 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6870 	elsiocb->context1 = lpfc_nlp_get(ndlp);
6871 	if (!elsiocb->context1) {
6872 		lpfc_els_free_iocb(phba, elsiocb);
6873 		goto free_rdp_context;
6874 	}
6875 
6876 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6877 	if (rc == IOCB_ERROR) {
6878 		lpfc_els_free_iocb(phba, elsiocb);
6879 		lpfc_nlp_put(ndlp);
6880 	}
6881 
6882 free_rdp_context:
6883 	/* This reference put is for the original unsolicited RDP. If the
6884 	 * iocb prep failed, there is no reference to remove.
6885 	 */
6886 	lpfc_nlp_put(ndlp);
6887 	kfree(rdp_context);
6888 }
6889 
6890 static int
6891 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
6892 {
6893 	LPFC_MBOXQ_t *mbox = NULL;
6894 	int rc;
6895 
6896 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6897 	if (!mbox) {
6898 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
6899 				"7105 failed to allocate mailbox memory");
6900 		return 1;
6901 	}
6902 
6903 	if (lpfc_sli4_dump_page_a0(phba, mbox))
6904 		goto prep_mbox_fail;
6905 	mbox->vport = rdp_context->ndlp->vport;
6906 	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
6907 	mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
6908 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6909 	if (rc == MBX_NOT_FINISHED)
6910 		goto issue_mbox_fail;
6911 
6912 	return 0;
6913 
6914 prep_mbox_fail:
6915 issue_mbox_fail:
6916 	mempool_free(mbox, phba->mbox_mem_pool);
6917 	return 1;
6918 }
6919 
6920 /*
6921  * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
6922  * @vport: pointer to a host virtual N_Port data structure.
6923  * @cmdiocb: pointer to lpfc command iocb data structure.
6924  * @ndlp: pointer to a node-list data structure.
6925  *
6926  * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
6927  * IOCB. First, the payload of the unsolicited RDP is checked.
6928  * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
6929  * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
6930  * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
6931  * gather all data and send RDP response.
6932  *
6933  * Return code
6934  *   0 - Sent the acc response
6935  *   1 - Sent the reject response.
6936  */
6937 static int
6938 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6939 		struct lpfc_nodelist *ndlp)
6940 {
6941 	struct lpfc_hba *phba = vport->phba;
6942 	struct lpfc_dmabuf *pcmd;
6943 	uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
6944 	struct fc_rdp_req_frame *rdp_req;
6945 	struct lpfc_rdp_context *rdp_context;
6946 	IOCB_t *cmd = NULL;
6947 	struct ls_rjt stat;
6948 
6949 	if (phba->sli_rev < LPFC_SLI_REV4 ||
6950 	    bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
6951 						LPFC_SLI_INTF_IF_TYPE_2) {
6952 		rjt_err = LSRJT_UNABLE_TPC;
6953 		rjt_expl = LSEXP_REQ_UNSUPPORTED;
6954 		goto error;
6955 	}
6956 
6957 	if (phba->sli_rev < LPFC_SLI_REV4 || (phba->hba_flag & HBA_FCOE_MODE)) {
6958 		rjt_err = LSRJT_UNABLE_TPC;
6959 		rjt_expl = LSEXP_REQ_UNSUPPORTED;
6960 		goto error;
6961 	}
6962 
6963 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6964 	rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
6965 
6966 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6967 			 "2422 ELS RDP Request "
6968 			 "dec len %d tag x%x port_id %d len %d\n",
6969 			 be32_to_cpu(rdp_req->rdp_des_length),
6970 			 be32_to_cpu(rdp_req->nport_id_desc.tag),
6971 			 be32_to_cpu(rdp_req->nport_id_desc.nport_id),
6972 			 be32_to_cpu(rdp_req->nport_id_desc.length));
6973 
6974 	if (sizeof(struct fc_rdp_nport_desc) !=
6975 			be32_to_cpu(rdp_req->rdp_des_length))
6976 		goto rjt_logerr;
6977 	if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
6978 		goto rjt_logerr;
6979 	if (RDP_NPORT_ID_SIZE !=
6980 			be32_to_cpu(rdp_req->nport_id_desc.length))
6981 		goto rjt_logerr;
6982 	rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
6983 	if (!rdp_context) {
6984 		rjt_err = LSRJT_UNABLE_TPC;
6985 		goto error;
6986 	}
6987 
6988 	cmd = &cmdiocb->iocb;
6989 	rdp_context->ndlp = lpfc_nlp_get(ndlp);
6990 	if (!rdp_context->ndlp) {
6991 		kfree(rdp_context);
6992 		rjt_err = LSRJT_UNABLE_TPC;
6993 		goto error;
6994 	}
6995 	rdp_context->ox_id = cmd->unsli3.rcvsli3.ox_id;
6996 	rdp_context->rx_id = cmd->ulpContext;
6997 	rdp_context->cmpl = lpfc_els_rdp_cmpl;
6998 	if (lpfc_get_rdp_info(phba, rdp_context)) {
6999 		lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
7000 				 "2423 Unable to send mailbox");
7001 		kfree(rdp_context);
7002 		rjt_err = LSRJT_UNABLE_TPC;
7003 		lpfc_nlp_put(ndlp);
7004 		goto error;
7005 	}
7006 
7007 	return 0;
7008 
7009 rjt_logerr:
7010 	rjt_err = LSRJT_LOGICAL_ERR;
7011 
7012 error:
7013 	memset(&stat, 0, sizeof(stat));
7014 	stat.un.b.lsRjtRsnCode = rjt_err;
7015 	stat.un.b.lsRjtRsnCodeExp = rjt_expl;
7016 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7017 	return 1;
7018 }
7019 
7020 
7021 static void
7022 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7023 {
7024 	MAILBOX_t *mb;
7025 	IOCB_t *icmd;
7026 	uint8_t *pcmd;
7027 	struct lpfc_iocbq *elsiocb;
7028 	struct lpfc_nodelist *ndlp;
7029 	struct ls_rjt *stat;
7030 	union lpfc_sli4_cfg_shdr *shdr;
7031 	struct lpfc_lcb_context *lcb_context;
7032 	struct fc_lcb_res_frame *lcb_res;
7033 	uint32_t cmdsize, shdr_status, shdr_add_status;
7034 	int rc;
7035 
7036 	mb = &pmb->u.mb;
7037 	lcb_context = (struct lpfc_lcb_context *)pmb->ctx_ndlp;
7038 	ndlp = lcb_context->ndlp;
7039 	pmb->ctx_ndlp = NULL;
7040 	pmb->ctx_buf = NULL;
7041 
7042 	shdr = (union lpfc_sli4_cfg_shdr *)
7043 			&pmb->u.mqe.un.beacon_config.header.cfg_shdr;
7044 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7045 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7046 
7047 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
7048 				"0194 SET_BEACON_CONFIG mailbox "
7049 				"completed with status x%x add_status x%x,"
7050 				" mbx status x%x\n",
7051 				shdr_status, shdr_add_status, mb->mbxStatus);
7052 
7053 	if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
7054 	    (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
7055 	    (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
7056 		mempool_free(pmb, phba->mbox_mem_pool);
7057 		goto error;
7058 	}
7059 
7060 	mempool_free(pmb, phba->mbox_mem_pool);
7061 	cmdsize = sizeof(struct fc_lcb_res_frame);
7062 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7063 			lpfc_max_els_tries, ndlp,
7064 			ndlp->nlp_DID, ELS_CMD_ACC);
7065 
7066 	/* Decrement the ndlp reference count from previous mbox command */
7067 	lpfc_nlp_put(ndlp);
7068 
7069 	if (!elsiocb)
7070 		goto free_lcb_context;
7071 
7072 	lcb_res = (struct fc_lcb_res_frame *)
7073 		(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
7074 
7075 	memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
7076 	icmd = &elsiocb->iocb;
7077 	icmd->ulpContext = lcb_context->rx_id;
7078 	icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7079 
7080 	pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
7081 	*((uint32_t *)(pcmd)) = ELS_CMD_ACC;
7082 	lcb_res->lcb_sub_command = lcb_context->sub_command;
7083 	lcb_res->lcb_type = lcb_context->type;
7084 	lcb_res->capability = lcb_context->capability;
7085 	lcb_res->lcb_frequency = lcb_context->frequency;
7086 	lcb_res->lcb_duration = lcb_context->duration;
7087 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7088 	phba->fc_stat.elsXmitACC++;
7089 
7090 	elsiocb->context1 = lpfc_nlp_get(ndlp);
7091 	if (!elsiocb->context1) {
7092 		lpfc_els_free_iocb(phba, elsiocb);
7093 		goto out;
7094 	}
7095 
7096 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7097 	if (rc == IOCB_ERROR) {
7098 		lpfc_els_free_iocb(phba, elsiocb);
7099 		lpfc_nlp_put(ndlp);
7100 	}
7101  out:
7102 	kfree(lcb_context);
7103 	return;
7104 
7105 error:
7106 	cmdsize = sizeof(struct fc_lcb_res_frame);
7107 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7108 			lpfc_max_els_tries, ndlp,
7109 			ndlp->nlp_DID, ELS_CMD_LS_RJT);
7110 	lpfc_nlp_put(ndlp);
7111 	if (!elsiocb)
7112 		goto free_lcb_context;
7113 
7114 	icmd = &elsiocb->iocb;
7115 	icmd->ulpContext = lcb_context->rx_id;
7116 	icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7117 	pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
7118 
7119 	*((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
7120 	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
7121 	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7122 
7123 	if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
7124 		stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
7125 
7126 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7127 	phba->fc_stat.elsXmitLSRJT++;
7128 	elsiocb->context1 = lpfc_nlp_get(ndlp);
7129 	if (!elsiocb->context1) {
7130 		lpfc_els_free_iocb(phba, elsiocb);
7131 		goto free_lcb_context;
7132 	}
7133 
7134 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7135 	if (rc == IOCB_ERROR) {
7136 		lpfc_els_free_iocb(phba, elsiocb);
7137 		lpfc_nlp_put(ndlp);
7138 	}
7139 free_lcb_context:
7140 	kfree(lcb_context);
7141 }
7142 
7143 static int
7144 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
7145 		     struct lpfc_lcb_context *lcb_context,
7146 		     uint32_t beacon_state)
7147 {
7148 	struct lpfc_hba *phba = vport->phba;
7149 	union lpfc_sli4_cfg_shdr *cfg_shdr;
7150 	LPFC_MBOXQ_t *mbox = NULL;
7151 	uint32_t len;
7152 	int rc;
7153 
7154 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7155 	if (!mbox)
7156 		return 1;
7157 
7158 	cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
7159 	len = sizeof(struct lpfc_mbx_set_beacon_config) -
7160 		sizeof(struct lpfc_sli4_cfg_mhdr);
7161 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7162 			 LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
7163 			 LPFC_SLI4_MBX_EMBED);
7164 	mbox->ctx_ndlp = (void *)lcb_context;
7165 	mbox->vport = phba->pport;
7166 	mbox->mbox_cmpl = lpfc_els_lcb_rsp;
7167 	bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
7168 	       phba->sli4_hba.physical_port);
7169 	bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
7170 	       beacon_state);
7171 	mbox->u.mqe.un.beacon_config.word5 = 0;		/* Reserved */
7172 
7173 	/*
7174 	 *	Check bv1s bit before issuing the mailbox
7175 	 *	if bv1s == 1, LCB V1 supported
7176 	 *	else, LCB V0 supported
7177 	 */
7178 
7179 	if (phba->sli4_hba.pc_sli4_params.bv1s) {
7180 		/* COMMON_SET_BEACON_CONFIG_V1 */
7181 		cfg_shdr->request.word9 = BEACON_VERSION_V1;
7182 		lcb_context->capability |= LCB_CAPABILITY_DURATION;
7183 		bf_set(lpfc_mbx_set_beacon_port_type,
7184 		       &mbox->u.mqe.un.beacon_config, 0);
7185 		bf_set(lpfc_mbx_set_beacon_duration_v1,
7186 		       &mbox->u.mqe.un.beacon_config,
7187 		       be16_to_cpu(lcb_context->duration));
7188 	} else {
7189 		/* COMMON_SET_BEACON_CONFIG_V0 */
7190 		if (be16_to_cpu(lcb_context->duration) != 0) {
7191 			mempool_free(mbox, phba->mbox_mem_pool);
7192 			return 1;
7193 		}
7194 		cfg_shdr->request.word9 = BEACON_VERSION_V0;
7195 		lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
7196 		bf_set(lpfc_mbx_set_beacon_state,
7197 		       &mbox->u.mqe.un.beacon_config, beacon_state);
7198 		bf_set(lpfc_mbx_set_beacon_port_type,
7199 		       &mbox->u.mqe.un.beacon_config, 1);
7200 		bf_set(lpfc_mbx_set_beacon_duration,
7201 		       &mbox->u.mqe.un.beacon_config,
7202 		       be16_to_cpu(lcb_context->duration));
7203 	}
7204 
7205 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7206 	if (rc == MBX_NOT_FINISHED) {
7207 		mempool_free(mbox, phba->mbox_mem_pool);
7208 		return 1;
7209 	}
7210 
7211 	return 0;
7212 }
7213 
7214 
7215 /**
7216  * lpfc_els_rcv_lcb - Process an unsolicited LCB
7217  * @vport: pointer to a host virtual N_Port data structure.
7218  * @cmdiocb: pointer to lpfc command iocb data structure.
7219  * @ndlp: pointer to a node-list data structure.
7220  *
7221  * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
7222  * First, the payload of the unsolicited LCB is checked.
7223  * Then based on Subcommand beacon will either turn on or off.
7224  *
7225  * Return code
7226  * 0 - Sent the acc response
7227  * 1 - Sent the reject response.
7228  **/
7229 static int
7230 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7231 		 struct lpfc_nodelist *ndlp)
7232 {
7233 	struct lpfc_hba *phba = vport->phba;
7234 	struct lpfc_dmabuf *pcmd;
7235 	uint8_t *lp;
7236 	struct fc_lcb_request_frame *beacon;
7237 	struct lpfc_lcb_context *lcb_context;
7238 	u8 state, rjt_err = 0;
7239 	struct ls_rjt stat;
7240 
7241 	pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
7242 	lp = (uint8_t *)pcmd->virt;
7243 	beacon = (struct fc_lcb_request_frame *)pcmd->virt;
7244 
7245 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7246 			"0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
7247 			"type x%x frequency %x duration x%x\n",
7248 			lp[0], lp[1], lp[2],
7249 			beacon->lcb_command,
7250 			beacon->lcb_sub_command,
7251 			beacon->lcb_type,
7252 			beacon->lcb_frequency,
7253 			be16_to_cpu(beacon->lcb_duration));
7254 
7255 	if (beacon->lcb_sub_command != LPFC_LCB_ON &&
7256 	    beacon->lcb_sub_command != LPFC_LCB_OFF) {
7257 		rjt_err = LSRJT_CMD_UNSUPPORTED;
7258 		goto rjt;
7259 	}
7260 
7261 	if (phba->sli_rev < LPFC_SLI_REV4  ||
7262 	    phba->hba_flag & HBA_FCOE_MODE ||
7263 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7264 	    LPFC_SLI_INTF_IF_TYPE_2)) {
7265 		rjt_err = LSRJT_CMD_UNSUPPORTED;
7266 		goto rjt;
7267 	}
7268 
7269 	lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
7270 	if (!lcb_context) {
7271 		rjt_err = LSRJT_UNABLE_TPC;
7272 		goto rjt;
7273 	}
7274 
7275 	state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
7276 	lcb_context->sub_command = beacon->lcb_sub_command;
7277 	lcb_context->capability	= 0;
7278 	lcb_context->type = beacon->lcb_type;
7279 	lcb_context->frequency = beacon->lcb_frequency;
7280 	lcb_context->duration = beacon->lcb_duration;
7281 	lcb_context->ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
7282 	lcb_context->rx_id = cmdiocb->iocb.ulpContext;
7283 	lcb_context->ndlp = lpfc_nlp_get(ndlp);
7284 	if (!lcb_context->ndlp) {
7285 		rjt_err = LSRJT_UNABLE_TPC;
7286 		goto rjt_free;
7287 	}
7288 
7289 	if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
7290 		lpfc_printf_vlog(ndlp->vport, KERN_ERR, LOG_TRACE_EVENT,
7291 				 "0193 failed to send mail box");
7292 		lpfc_nlp_put(ndlp);
7293 		rjt_err = LSRJT_UNABLE_TPC;
7294 		goto rjt_free;
7295 	}
7296 	return 0;
7297 
7298 rjt_free:
7299 	kfree(lcb_context);
7300 rjt:
7301 	memset(&stat, 0, sizeof(stat));
7302 	stat.un.b.lsRjtRsnCode = rjt_err;
7303 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7304 	return 1;
7305 }
7306 
7307 
7308 /**
7309  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
7310  * @vport: pointer to a host virtual N_Port data structure.
7311  *
7312  * This routine cleans up any Registration State Change Notification
7313  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
7314  * @vport together with the host_lock is used to prevent multiple thread
7315  * trying to access the RSCN array on a same @vport at the same time.
7316  **/
7317 void
7318 lpfc_els_flush_rscn(struct lpfc_vport *vport)
7319 {
7320 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7321 	struct lpfc_hba  *phba = vport->phba;
7322 	int i;
7323 
7324 	spin_lock_irq(shost->host_lock);
7325 	if (vport->fc_rscn_flush) {
7326 		/* Another thread is walking fc_rscn_id_list on this vport */
7327 		spin_unlock_irq(shost->host_lock);
7328 		return;
7329 	}
7330 	/* Indicate we are walking lpfc_els_flush_rscn on this vport */
7331 	vport->fc_rscn_flush = 1;
7332 	spin_unlock_irq(shost->host_lock);
7333 
7334 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7335 		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
7336 		vport->fc_rscn_id_list[i] = NULL;
7337 	}
7338 	spin_lock_irq(shost->host_lock);
7339 	vport->fc_rscn_id_cnt = 0;
7340 	vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
7341 	spin_unlock_irq(shost->host_lock);
7342 	lpfc_can_disctmo(vport);
7343 	/* Indicate we are done walking this fc_rscn_id_list */
7344 	vport->fc_rscn_flush = 0;
7345 }
7346 
7347 /**
7348  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
7349  * @vport: pointer to a host virtual N_Port data structure.
7350  * @did: remote destination port identifier.
7351  *
7352  * This routine checks whether there is any pending Registration State
7353  * Configuration Notification (RSCN) to a @did on @vport.
7354  *
7355  * Return code
7356  *   None zero - The @did matched with a pending rscn
7357  *   0 - not able to match @did with a pending rscn
7358  **/
7359 int
7360 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
7361 {
7362 	D_ID ns_did;
7363 	D_ID rscn_did;
7364 	uint32_t *lp;
7365 	uint32_t payload_len, i;
7366 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7367 
7368 	ns_did.un.word = did;
7369 
7370 	/* Never match fabric nodes for RSCNs */
7371 	if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7372 		return 0;
7373 
7374 	/* If we are doing a FULL RSCN rediscovery, match everything */
7375 	if (vport->fc_flag & FC_RSCN_DISCOVERY)
7376 		return did;
7377 
7378 	spin_lock_irq(shost->host_lock);
7379 	if (vport->fc_rscn_flush) {
7380 		/* Another thread is walking fc_rscn_id_list on this vport */
7381 		spin_unlock_irq(shost->host_lock);
7382 		return 0;
7383 	}
7384 	/* Indicate we are walking fc_rscn_id_list on this vport */
7385 	vport->fc_rscn_flush = 1;
7386 	spin_unlock_irq(shost->host_lock);
7387 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7388 		lp = vport->fc_rscn_id_list[i]->virt;
7389 		payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
7390 		payload_len -= sizeof(uint32_t);	/* take off word 0 */
7391 		while (payload_len) {
7392 			rscn_did.un.word = be32_to_cpu(*lp++);
7393 			payload_len -= sizeof(uint32_t);
7394 			switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
7395 			case RSCN_ADDRESS_FORMAT_PORT:
7396 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7397 				    && (ns_did.un.b.area == rscn_did.un.b.area)
7398 				    && (ns_did.un.b.id == rscn_did.un.b.id))
7399 					goto return_did_out;
7400 				break;
7401 			case RSCN_ADDRESS_FORMAT_AREA:
7402 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7403 				    && (ns_did.un.b.area == rscn_did.un.b.area))
7404 					goto return_did_out;
7405 				break;
7406 			case RSCN_ADDRESS_FORMAT_DOMAIN:
7407 				if (ns_did.un.b.domain == rscn_did.un.b.domain)
7408 					goto return_did_out;
7409 				break;
7410 			case RSCN_ADDRESS_FORMAT_FABRIC:
7411 				goto return_did_out;
7412 			}
7413 		}
7414 	}
7415 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
7416 	vport->fc_rscn_flush = 0;
7417 	return 0;
7418 return_did_out:
7419 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
7420 	vport->fc_rscn_flush = 0;
7421 	return did;
7422 }
7423 
7424 /**
7425  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
7426  * @vport: pointer to a host virtual N_Port data structure.
7427  *
7428  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
7429  * state machine for a @vport's nodes that are with pending RSCN (Registration
7430  * State Change Notification).
7431  *
7432  * Return code
7433  *   0 - Successful (currently alway return 0)
7434  **/
7435 static int
7436 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
7437 {
7438 	struct lpfc_nodelist *ndlp = NULL;
7439 
7440 	/* Move all affected nodes by pending RSCNs to NPR state. */
7441 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
7442 		if ((ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
7443 		    !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
7444 			continue;
7445 
7446 		/* NVME Target mode does not do RSCN Recovery. */
7447 		if (vport->phba->nvmet_support)
7448 			continue;
7449 
7450 		/* If we are in the process of doing discovery on this
7451 		 * NPort, let it continue on its own.
7452 		 */
7453 		switch (ndlp->nlp_state) {
7454 		case  NLP_STE_PLOGI_ISSUE:
7455 		case  NLP_STE_ADISC_ISSUE:
7456 		case  NLP_STE_REG_LOGIN_ISSUE:
7457 		case  NLP_STE_PRLI_ISSUE:
7458 		case  NLP_STE_LOGO_ISSUE:
7459 			continue;
7460 		}
7461 
7462 		lpfc_disc_state_machine(vport, ndlp, NULL,
7463 					NLP_EVT_DEVICE_RECOVERY);
7464 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
7465 	}
7466 	return 0;
7467 }
7468 
7469 /**
7470  * lpfc_send_rscn_event - Send an RSCN event to management application
7471  * @vport: pointer to a host virtual N_Port data structure.
7472  * @cmdiocb: pointer to lpfc command iocb data structure.
7473  *
7474  * lpfc_send_rscn_event sends an RSCN netlink event to management
7475  * applications.
7476  */
7477 static void
7478 lpfc_send_rscn_event(struct lpfc_vport *vport,
7479 		struct lpfc_iocbq *cmdiocb)
7480 {
7481 	struct lpfc_dmabuf *pcmd;
7482 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7483 	uint32_t *payload_ptr;
7484 	uint32_t payload_len;
7485 	struct lpfc_rscn_event_header *rscn_event_data;
7486 
7487 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7488 	payload_ptr = (uint32_t *) pcmd->virt;
7489 	payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
7490 
7491 	rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
7492 		payload_len, GFP_KERNEL);
7493 	if (!rscn_event_data) {
7494 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
7495 			"0147 Failed to allocate memory for RSCN event\n");
7496 		return;
7497 	}
7498 	rscn_event_data->event_type = FC_REG_RSCN_EVENT;
7499 	rscn_event_data->payload_length = payload_len;
7500 	memcpy(rscn_event_data->rscn_payload, payload_ptr,
7501 		payload_len);
7502 
7503 	fc_host_post_vendor_event(shost,
7504 		fc_get_event_number(),
7505 		sizeof(struct lpfc_rscn_event_header) + payload_len,
7506 		(char *)rscn_event_data,
7507 		LPFC_NL_VENDOR_ID);
7508 
7509 	kfree(rscn_event_data);
7510 }
7511 
7512 /**
7513  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
7514  * @vport: pointer to a host virtual N_Port data structure.
7515  * @cmdiocb: pointer to lpfc command iocb data structure.
7516  * @ndlp: pointer to a node-list data structure.
7517  *
7518  * This routine processes an unsolicited RSCN (Registration State Change
7519  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
7520  * to invoke fc_host_post_event() routine to the FC transport layer. If the
7521  * discover state machine is about to begin discovery, it just accepts the
7522  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
7523  * contains N_Port IDs for other vports on this HBA, it just accepts the
7524  * RSCN and ignore processing it. If the state machine is in the recovery
7525  * state, the fc_rscn_id_list of this @vport is walked and the
7526  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
7527  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
7528  * routine is invoked to handle the RSCN event.
7529  *
7530  * Return code
7531  *   0 - Just sent the acc response
7532  *   1 - Sent the acc response and waited for name server completion
7533  **/
7534 static int
7535 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7536 		  struct lpfc_nodelist *ndlp)
7537 {
7538 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7539 	struct lpfc_hba  *phba = vport->phba;
7540 	struct lpfc_dmabuf *pcmd;
7541 	uint32_t *lp, *datap;
7542 	uint32_t payload_len, length, nportid, *cmd;
7543 	int rscn_cnt;
7544 	int rscn_id = 0, hba_id = 0;
7545 	int i, tmo;
7546 
7547 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7548 	lp = (uint32_t *) pcmd->virt;
7549 
7550 	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
7551 	payload_len -= sizeof(uint32_t);	/* take off word 0 */
7552 	/* RSCN received */
7553 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7554 			 "0214 RSCN received Data: x%x x%x x%x x%x\n",
7555 			 vport->fc_flag, payload_len, *lp,
7556 			 vport->fc_rscn_id_cnt);
7557 
7558 	/* Send an RSCN event to the management application */
7559 	lpfc_send_rscn_event(vport, cmdiocb);
7560 
7561 	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
7562 		fc_host_post_event(shost, fc_get_event_number(),
7563 			FCH_EVT_RSCN, lp[i]);
7564 
7565 	/* Check if RSCN is coming from a direct-connected remote NPort */
7566 	if (vport->fc_flag & FC_PT2PT) {
7567 		/* If so, just ACC it, no other action needed for now */
7568 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7569 				 "2024 pt2pt RSCN %08x Data: x%x x%x\n",
7570 				 *lp, vport->fc_flag, payload_len);
7571 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7572 
7573 		/* Check to see if we need to NVME rescan this target
7574 		 * remoteport.
7575 		 */
7576 		if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
7577 		    ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
7578 			lpfc_nvme_rescan_port(vport, ndlp);
7579 		return 0;
7580 	}
7581 
7582 	/* If we are about to begin discovery, just ACC the RSCN.
7583 	 * Discovery processing will satisfy it.
7584 	 */
7585 	if (vport->port_state <= LPFC_NS_QRY) {
7586 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7587 			"RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
7588 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
7589 
7590 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7591 		return 0;
7592 	}
7593 
7594 	/* If this RSCN just contains NPortIDs for other vports on this HBA,
7595 	 * just ACC and ignore it.
7596 	 */
7597 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
7598 		!(vport->cfg_peer_port_login)) {
7599 		i = payload_len;
7600 		datap = lp;
7601 		while (i > 0) {
7602 			nportid = *datap++;
7603 			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
7604 			i -= sizeof(uint32_t);
7605 			rscn_id++;
7606 			if (lpfc_find_vport_by_did(phba, nportid))
7607 				hba_id++;
7608 		}
7609 		if (rscn_id == hba_id) {
7610 			/* ALL NPortIDs in RSCN are on HBA */
7611 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7612 					 "0219 Ignore RSCN "
7613 					 "Data: x%x x%x x%x x%x\n",
7614 					 vport->fc_flag, payload_len,
7615 					 *lp, vport->fc_rscn_id_cnt);
7616 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7617 				"RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
7618 				ndlp->nlp_DID, vport->port_state,
7619 				ndlp->nlp_flag);
7620 
7621 			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
7622 				ndlp, NULL);
7623 			return 0;
7624 		}
7625 	}
7626 
7627 	spin_lock_irq(shost->host_lock);
7628 	if (vport->fc_rscn_flush) {
7629 		/* Another thread is walking fc_rscn_id_list on this vport */
7630 		vport->fc_flag |= FC_RSCN_DISCOVERY;
7631 		spin_unlock_irq(shost->host_lock);
7632 		/* Send back ACC */
7633 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7634 		return 0;
7635 	}
7636 	/* Indicate we are walking fc_rscn_id_list on this vport */
7637 	vport->fc_rscn_flush = 1;
7638 	spin_unlock_irq(shost->host_lock);
7639 	/* Get the array count after successfully have the token */
7640 	rscn_cnt = vport->fc_rscn_id_cnt;
7641 	/* If we are already processing an RSCN, save the received
7642 	 * RSCN payload buffer, cmdiocb->context2 to process later.
7643 	 */
7644 	if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
7645 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7646 			"RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
7647 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
7648 
7649 		spin_lock_irq(shost->host_lock);
7650 		vport->fc_flag |= FC_RSCN_DEFERRED;
7651 
7652 		/* Restart disctmo if its already running */
7653 		if (vport->fc_flag & FC_DISC_TMO) {
7654 			tmo = ((phba->fc_ratov * 3) + 3);
7655 			mod_timer(&vport->fc_disctmo,
7656 				  jiffies + msecs_to_jiffies(1000 * tmo));
7657 		}
7658 		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
7659 		    !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
7660 			vport->fc_flag |= FC_RSCN_MODE;
7661 			spin_unlock_irq(shost->host_lock);
7662 			if (rscn_cnt) {
7663 				cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
7664 				length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
7665 			}
7666 			if ((rscn_cnt) &&
7667 			    (payload_len + length <= LPFC_BPL_SIZE)) {
7668 				*cmd &= ELS_CMD_MASK;
7669 				*cmd |= cpu_to_be32(payload_len + length);
7670 				memcpy(((uint8_t *)cmd) + length, lp,
7671 				       payload_len);
7672 			} else {
7673 				vport->fc_rscn_id_list[rscn_cnt] = pcmd;
7674 				vport->fc_rscn_id_cnt++;
7675 				/* If we zero, cmdiocb->context2, the calling
7676 				 * routine will not try to free it.
7677 				 */
7678 				cmdiocb->context2 = NULL;
7679 			}
7680 			/* Deferred RSCN */
7681 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7682 					 "0235 Deferred RSCN "
7683 					 "Data: x%x x%x x%x\n",
7684 					 vport->fc_rscn_id_cnt, vport->fc_flag,
7685 					 vport->port_state);
7686 		} else {
7687 			vport->fc_flag |= FC_RSCN_DISCOVERY;
7688 			spin_unlock_irq(shost->host_lock);
7689 			/* ReDiscovery RSCN */
7690 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7691 					 "0234 ReDiscovery RSCN "
7692 					 "Data: x%x x%x x%x\n",
7693 					 vport->fc_rscn_id_cnt, vport->fc_flag,
7694 					 vport->port_state);
7695 		}
7696 		/* Indicate we are done walking fc_rscn_id_list on this vport */
7697 		vport->fc_rscn_flush = 0;
7698 		/* Send back ACC */
7699 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7700 		/* send RECOVERY event for ALL nodes that match RSCN payload */
7701 		lpfc_rscn_recovery_check(vport);
7702 		return 0;
7703 	}
7704 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
7705 		"RCV RSCN:        did:x%x/ste:x%x flg:x%x",
7706 		ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
7707 
7708 	spin_lock_irq(shost->host_lock);
7709 	vport->fc_flag |= FC_RSCN_MODE;
7710 	spin_unlock_irq(shost->host_lock);
7711 	vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
7712 	/* Indicate we are done walking fc_rscn_id_list on this vport */
7713 	vport->fc_rscn_flush = 0;
7714 	/*
7715 	 * If we zero, cmdiocb->context2, the calling routine will
7716 	 * not try to free it.
7717 	 */
7718 	cmdiocb->context2 = NULL;
7719 	lpfc_set_disctmo(vport);
7720 	/* Send back ACC */
7721 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7722 	/* send RECOVERY event for ALL nodes that match RSCN payload */
7723 	lpfc_rscn_recovery_check(vport);
7724 	return lpfc_els_handle_rscn(vport);
7725 }
7726 
7727 /**
7728  * lpfc_els_handle_rscn - Handle rscn for a vport
7729  * @vport: pointer to a host virtual N_Port data structure.
7730  *
7731  * This routine handles the Registration State Configuration Notification
7732  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
7733  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
7734  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
7735  * NameServer shall be issued. If CT command to the NameServer fails to be
7736  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
7737  * RSCN activities with the @vport.
7738  *
7739  * Return code
7740  *   0 - Cleaned up rscn on the @vport
7741  *   1 - Wait for plogi to name server before proceed
7742  **/
7743 int
7744 lpfc_els_handle_rscn(struct lpfc_vport *vport)
7745 {
7746 	struct lpfc_nodelist *ndlp;
7747 	struct lpfc_hba  *phba = vport->phba;
7748 
7749 	/* Ignore RSCN if the port is being torn down. */
7750 	if (vport->load_flag & FC_UNLOADING) {
7751 		lpfc_els_flush_rscn(vport);
7752 		return 0;
7753 	}
7754 
7755 	/* Start timer for RSCN processing */
7756 	lpfc_set_disctmo(vport);
7757 
7758 	/* RSCN processed */
7759 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
7760 			 "0215 RSCN processed Data: x%x x%x x%x x%x x%x x%x\n",
7761 			 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
7762 			 vport->port_state, vport->num_disc_nodes,
7763 			 vport->gidft_inp);
7764 
7765 	/* To process RSCN, first compare RSCN data with NameServer */
7766 	vport->fc_ns_retry = 0;
7767 	vport->num_disc_nodes = 0;
7768 
7769 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
7770 	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
7771 		/* Good ndlp, issue CT Request to NameServer.  Need to
7772 		 * know how many gidfts were issued.  If none, then just
7773 		 * flush the RSCN.  Otherwise, the outstanding requests
7774 		 * need to complete.
7775 		 */
7776 		if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
7777 			if (lpfc_issue_gidft(vport) > 0)
7778 				return 1;
7779 		} else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
7780 			if (lpfc_issue_gidpt(vport) > 0)
7781 				return 1;
7782 		} else {
7783 			return 1;
7784 		}
7785 	} else {
7786 		/* Nameserver login in question.  Revalidate. */
7787 		if (ndlp) {
7788 			ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
7789 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
7790 		} else {
7791 			ndlp = lpfc_nlp_init(vport, NameServer_DID);
7792 			if (!ndlp) {
7793 				lpfc_els_flush_rscn(vport);
7794 				return 0;
7795 			}
7796 			ndlp->nlp_prev_state = ndlp->nlp_state;
7797 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
7798 		}
7799 		ndlp->nlp_type |= NLP_FABRIC;
7800 		lpfc_issue_els_plogi(vport, NameServer_DID, 0);
7801 		/* Wait for NameServer login cmpl before we can
7802 		 * continue
7803 		 */
7804 		return 1;
7805 	}
7806 
7807 	lpfc_els_flush_rscn(vport);
7808 	return 0;
7809 }
7810 
7811 /**
7812  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
7813  * @vport: pointer to a host virtual N_Port data structure.
7814  * @cmdiocb: pointer to lpfc command iocb data structure.
7815  * @ndlp: pointer to a node-list data structure.
7816  *
7817  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
7818  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
7819  * point topology. As an unsolicited FLOGI should not be received in a loop
7820  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
7821  * lpfc_check_sparm() routine is invoked to check the parameters in the
7822  * unsolicited FLOGI. If parameters validation failed, the routine
7823  * lpfc_els_rsp_reject() shall be called with reject reason code set to
7824  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
7825  * FLOGI shall be compared with the Port WWN of the @vport to determine who
7826  * will initiate PLOGI. The higher lexicographical value party shall has
7827  * higher priority (as the winning port) and will initiate PLOGI and
7828  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
7829  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
7830  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
7831  *
7832  * Return code
7833  *   0 - Successfully processed the unsolicited flogi
7834  *   1 - Failed to process the unsolicited flogi
7835  **/
7836 static int
7837 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7838 		   struct lpfc_nodelist *ndlp)
7839 {
7840 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7841 	struct lpfc_hba  *phba = vport->phba;
7842 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7843 	uint32_t *lp = (uint32_t *) pcmd->virt;
7844 	IOCB_t *icmd = &cmdiocb->iocb;
7845 	struct serv_parm *sp;
7846 	LPFC_MBOXQ_t *mbox;
7847 	uint32_t cmd, did;
7848 	int rc;
7849 	uint32_t fc_flag = 0;
7850 	uint32_t port_state = 0;
7851 
7852 	cmd = *lp++;
7853 	sp = (struct serv_parm *) lp;
7854 
7855 	/* FLOGI received */
7856 
7857 	lpfc_set_disctmo(vport);
7858 
7859 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7860 		/* We should never receive a FLOGI in loop mode, ignore it */
7861 		did = icmd->un.elsreq64.remoteID;
7862 
7863 		/* An FLOGI ELS command <elsCmd> was received from DID <did> in
7864 		   Loop Mode */
7865 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
7866 				 "0113 An FLOGI ELS command x%x was "
7867 				 "received from DID x%x in Loop Mode\n",
7868 				 cmd, did);
7869 		return 1;
7870 	}
7871 
7872 	(void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
7873 
7874 	/*
7875 	 * If our portname is greater than the remote portname,
7876 	 * then we initiate Nport login.
7877 	 */
7878 
7879 	rc = memcmp(&vport->fc_portname, &sp->portName,
7880 		    sizeof(struct lpfc_name));
7881 
7882 	if (!rc) {
7883 		if (phba->sli_rev < LPFC_SLI_REV4) {
7884 			mbox = mempool_alloc(phba->mbox_mem_pool,
7885 					     GFP_KERNEL);
7886 			if (!mbox)
7887 				return 1;
7888 			lpfc_linkdown(phba);
7889 			lpfc_init_link(phba, mbox,
7890 				       phba->cfg_topology,
7891 				       phba->cfg_link_speed);
7892 			mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
7893 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7894 			mbox->vport = vport;
7895 			rc = lpfc_sli_issue_mbox(phba, mbox,
7896 						 MBX_NOWAIT);
7897 			lpfc_set_loopback_flag(phba);
7898 			if (rc == MBX_NOT_FINISHED)
7899 				mempool_free(mbox, phba->mbox_mem_pool);
7900 			return 1;
7901 		}
7902 
7903 		/* abort the flogi coming back to ourselves
7904 		 * due to external loopback on the port.
7905 		 */
7906 		lpfc_els_abort_flogi(phba);
7907 		return 0;
7908 
7909 	} else if (rc > 0) {	/* greater than */
7910 		spin_lock_irq(shost->host_lock);
7911 		vport->fc_flag |= FC_PT2PT_PLOGI;
7912 		spin_unlock_irq(shost->host_lock);
7913 
7914 		/* If we have the high WWPN we can assign our own
7915 		 * myDID; otherwise, we have to WAIT for a PLOGI
7916 		 * from the remote NPort to find out what it
7917 		 * will be.
7918 		 */
7919 		vport->fc_myDID = PT2PT_LocalID;
7920 	} else {
7921 		vport->fc_myDID = PT2PT_RemoteID;
7922 	}
7923 
7924 	/*
7925 	 * The vport state should go to LPFC_FLOGI only
7926 	 * AFTER we issue a FLOGI, not receive one.
7927 	 */
7928 	spin_lock_irq(shost->host_lock);
7929 	fc_flag = vport->fc_flag;
7930 	port_state = vport->port_state;
7931 	vport->fc_flag |= FC_PT2PT;
7932 	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
7933 
7934 	/* Acking an unsol FLOGI.  Count 1 for link bounce
7935 	 * work-around.
7936 	 */
7937 	vport->rcv_flogi_cnt++;
7938 	spin_unlock_irq(shost->host_lock);
7939 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7940 			 "3311 Rcv Flogi PS x%x new PS x%x "
7941 			 "fc_flag x%x new fc_flag x%x\n",
7942 			 port_state, vport->port_state,
7943 			 fc_flag, vport->fc_flag);
7944 
7945 	/*
7946 	 * We temporarily set fc_myDID to make it look like we are
7947 	 * a Fabric. This is done just so we end up with the right
7948 	 * did / sid on the FLOGI ACC rsp.
7949 	 */
7950 	did = vport->fc_myDID;
7951 	vport->fc_myDID = Fabric_DID;
7952 
7953 	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
7954 
7955 	/* Defer ACC response until AFTER we issue a FLOGI */
7956 	if (!(phba->hba_flag & HBA_FLOGI_ISSUED)) {
7957 		phba->defer_flogi_acc_rx_id = cmdiocb->iocb.ulpContext;
7958 		phba->defer_flogi_acc_ox_id =
7959 					cmdiocb->iocb.unsli3.rcvsli3.ox_id;
7960 
7961 		vport->fc_myDID = did;
7962 
7963 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7964 				 "3344 Deferring FLOGI ACC: rx_id: x%x,"
7965 				 " ox_id: x%x, hba_flag x%x\n",
7966 				 phba->defer_flogi_acc_rx_id,
7967 				 phba->defer_flogi_acc_ox_id, phba->hba_flag);
7968 
7969 		phba->defer_flogi_acc_flag = true;
7970 
7971 		return 0;
7972 	}
7973 
7974 	/* Send back ACC */
7975 	lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
7976 
7977 	/* Now lets put fc_myDID back to what its supposed to be */
7978 	vport->fc_myDID = did;
7979 
7980 	return 0;
7981 }
7982 
7983 /**
7984  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
7985  * @vport: pointer to a host virtual N_Port data structure.
7986  * @cmdiocb: pointer to lpfc command iocb data structure.
7987  * @ndlp: pointer to a node-list data structure.
7988  *
7989  * This routine processes Request Node Identification Data (RNID) IOCB
7990  * received as an ELS unsolicited event. Only when the RNID specified format
7991  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
7992  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
7993  * Accept (ACC) the RNID ELS command. All the other RNID formats are
7994  * rejected by invoking the lpfc_els_rsp_reject() routine.
7995  *
7996  * Return code
7997  *   0 - Successfully processed rnid iocb (currently always return 0)
7998  **/
7999 static int
8000 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8001 		  struct lpfc_nodelist *ndlp)
8002 {
8003 	struct lpfc_dmabuf *pcmd;
8004 	uint32_t *lp;
8005 	RNID *rn;
8006 	struct ls_rjt stat;
8007 
8008 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
8009 	lp = (uint32_t *) pcmd->virt;
8010 
8011 	lp++;
8012 	rn = (RNID *) lp;
8013 
8014 	/* RNID received */
8015 
8016 	switch (rn->Format) {
8017 	case 0:
8018 	case RNID_TOPOLOGY_DISC:
8019 		/* Send back ACC */
8020 		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
8021 		break;
8022 	default:
8023 		/* Reject this request because format not supported */
8024 		stat.un.b.lsRjtRsvd0 = 0;
8025 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8026 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8027 		stat.un.b.vendorUnique = 0;
8028 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
8029 			NULL);
8030 	}
8031 	return 0;
8032 }
8033 
8034 /**
8035  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
8036  * @vport: pointer to a host virtual N_Port data structure.
8037  * @cmdiocb: pointer to lpfc command iocb data structure.
8038  * @ndlp: pointer to a node-list data structure.
8039  *
8040  * Return code
8041  *   0 - Successfully processed echo iocb (currently always return 0)
8042  **/
8043 static int
8044 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8045 		  struct lpfc_nodelist *ndlp)
8046 {
8047 	uint8_t *pcmd;
8048 
8049 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
8050 
8051 	/* skip over first word of echo command to find echo data */
8052 	pcmd += sizeof(uint32_t);
8053 
8054 	lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
8055 	return 0;
8056 }
8057 
8058 /**
8059  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
8060  * @vport: pointer to a host virtual N_Port data structure.
8061  * @cmdiocb: pointer to lpfc command iocb data structure.
8062  * @ndlp: pointer to a node-list data structure.
8063  *
8064  * This routine processes a Link Incident Report Registration(LIRR) IOCB
8065  * received as an ELS unsolicited event. Currently, this function just invokes
8066  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
8067  *
8068  * Return code
8069  *   0 - Successfully processed lirr iocb (currently always return 0)
8070  **/
8071 static int
8072 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8073 		  struct lpfc_nodelist *ndlp)
8074 {
8075 	struct ls_rjt stat;
8076 
8077 	/* For now, unconditionally reject this command */
8078 	stat.un.b.lsRjtRsvd0 = 0;
8079 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8080 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8081 	stat.un.b.vendorUnique = 0;
8082 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8083 	return 0;
8084 }
8085 
8086 /**
8087  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
8088  * @vport: pointer to a host virtual N_Port data structure.
8089  * @cmdiocb: pointer to lpfc command iocb data structure.
8090  * @ndlp: pointer to a node-list data structure.
8091  *
8092  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
8093  * received as an ELS unsolicited event. A request to RRQ shall only
8094  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
8095  * Nx_Port N_Port_ID of the target Exchange is the same as the
8096  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
8097  * not accepted, an LS_RJT with reason code "Unable to perform
8098  * command request" and reason code explanation "Invalid Originator
8099  * S_ID" shall be returned. For now, we just unconditionally accept
8100  * RRQ from the target.
8101  **/
8102 static void
8103 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8104 		 struct lpfc_nodelist *ndlp)
8105 {
8106 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8107 	if (vport->phba->sli_rev == LPFC_SLI_REV4)
8108 		lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
8109 }
8110 
8111 /**
8112  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
8113  * @phba: pointer to lpfc hba data structure.
8114  * @pmb: pointer to the driver internal queue element for mailbox command.
8115  *
8116  * This routine is the completion callback function for the MBX_READ_LNK_STAT
8117  * mailbox command. This callback function is to actually send the Accept
8118  * (ACC) response to a Read Link Status (RLS) unsolicited IOCB event. It
8119  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
8120  * mailbox command, constructs the RLS response with the link statistics
8121  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
8122  * response to the RLS.
8123  *
8124  * Note that the ndlp reference count will be incremented by 1 for holding the
8125  * ndlp and the reference to ndlp will be stored into the context1 field of
8126  * the IOCB for the completion callback function to the RLS Accept Response
8127  * ELS IOCB command.
8128  *
8129  **/
8130 static void
8131 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
8132 {
8133 	int rc = 0;
8134 	MAILBOX_t *mb;
8135 	IOCB_t *icmd;
8136 	struct RLS_RSP *rls_rsp;
8137 	uint8_t *pcmd;
8138 	struct lpfc_iocbq *elsiocb;
8139 	struct lpfc_nodelist *ndlp;
8140 	uint16_t oxid;
8141 	uint16_t rxid;
8142 	uint32_t cmdsize;
8143 
8144 	mb = &pmb->u.mb;
8145 
8146 	ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
8147 	rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
8148 	oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
8149 	pmb->ctx_buf = NULL;
8150 	pmb->ctx_ndlp = NULL;
8151 
8152 	if (mb->mbxStatus) {
8153 		mempool_free(pmb, phba->mbox_mem_pool);
8154 		return;
8155 	}
8156 
8157 	cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
8158 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8159 				     lpfc_max_els_tries, ndlp,
8160 				     ndlp->nlp_DID, ELS_CMD_ACC);
8161 
8162 	/* Decrement the ndlp reference count from previous mbox command */
8163 	lpfc_nlp_put(ndlp);
8164 
8165 	if (!elsiocb) {
8166 		mempool_free(pmb, phba->mbox_mem_pool);
8167 		return;
8168 	}
8169 
8170 	icmd = &elsiocb->iocb;
8171 	icmd->ulpContext = rxid;
8172 	icmd->unsli3.rcvsli3.ox_id = oxid;
8173 
8174 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8175 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8176 	pcmd += sizeof(uint32_t); /* Skip past command */
8177 	rls_rsp = (struct RLS_RSP *)pcmd;
8178 
8179 	rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
8180 	rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
8181 	rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
8182 	rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
8183 	rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
8184 	rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
8185 	mempool_free(pmb, phba->mbox_mem_pool);
8186 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
8187 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8188 			 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
8189 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
8190 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
8191 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8192 			 ndlp->nlp_rpi);
8193 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
8194 	phba->fc_stat.elsXmitACC++;
8195 	elsiocb->context1 = lpfc_nlp_get(ndlp);
8196 	if (!elsiocb->context1) {
8197 		lpfc_els_free_iocb(phba, elsiocb);
8198 		return;
8199 	}
8200 
8201 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8202 	if (rc == IOCB_ERROR) {
8203 		lpfc_els_free_iocb(phba, elsiocb);
8204 		lpfc_nlp_put(ndlp);
8205 	}
8206 	return;
8207 }
8208 
8209 /**
8210  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
8211  * @vport: pointer to a host virtual N_Port data structure.
8212  * @cmdiocb: pointer to lpfc command iocb data structure.
8213  * @ndlp: pointer to a node-list data structure.
8214  *
8215  * This routine processes Read Link Status (RLS) IOCB received as an
8216  * ELS unsolicited event. It first checks the remote port state. If the
8217  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8218  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8219  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
8220  * for reading the HBA link statistics. It is for the callback function,
8221  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
8222  * to actually sending out RPL Accept (ACC) response.
8223  *
8224  * Return codes
8225  *   0 - Successfully processed rls iocb (currently always return 0)
8226  **/
8227 static int
8228 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8229 		 struct lpfc_nodelist *ndlp)
8230 {
8231 	struct lpfc_hba *phba = vport->phba;
8232 	LPFC_MBOXQ_t *mbox;
8233 	struct ls_rjt stat;
8234 
8235 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8236 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8237 		/* reject the unsolicited RLS request and done with it */
8238 		goto reject_out;
8239 
8240 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
8241 	if (mbox) {
8242 		lpfc_read_lnk_stat(phba, mbox);
8243 		mbox->ctx_buf = (void *)((unsigned long)
8244 			((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
8245 			cmdiocb->iocb.ulpContext)); /* rx_id */
8246 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
8247 		if (!mbox->ctx_ndlp)
8248 			goto node_err;
8249 		mbox->vport = vport;
8250 		mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
8251 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
8252 			!= MBX_NOT_FINISHED)
8253 			/* Mbox completion will send ELS Response */
8254 			return 0;
8255 		/* Decrement reference count used for the failed mbox
8256 		 * command.
8257 		 */
8258 		lpfc_nlp_put(ndlp);
8259 node_err:
8260 		mempool_free(mbox, phba->mbox_mem_pool);
8261 	}
8262 reject_out:
8263 	/* issue rejection response */
8264 	stat.un.b.lsRjtRsvd0 = 0;
8265 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8266 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8267 	stat.un.b.vendorUnique = 0;
8268 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8269 	return 0;
8270 }
8271 
8272 /**
8273  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
8274  * @vport: pointer to a host virtual N_Port data structure.
8275  * @cmdiocb: pointer to lpfc command iocb data structure.
8276  * @ndlp: pointer to a node-list data structure.
8277  *
8278  * This routine processes Read Timout Value (RTV) IOCB received as an
8279  * ELS unsolicited event. It first checks the remote port state. If the
8280  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8281  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8282  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
8283  * Value (RTV) unsolicited IOCB event.
8284  *
8285  * Note that the ndlp reference count will be incremented by 1 for holding the
8286  * ndlp and the reference to ndlp will be stored into the context1 field of
8287  * the IOCB for the completion callback function to the RTV Accept Response
8288  * ELS IOCB command.
8289  *
8290  * Return codes
8291  *   0 - Successfully processed rtv iocb (currently always return 0)
8292  **/
8293 static int
8294 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8295 		 struct lpfc_nodelist *ndlp)
8296 {
8297 	int rc = 0;
8298 	struct lpfc_hba *phba = vport->phba;
8299 	struct ls_rjt stat;
8300 	struct RTV_RSP *rtv_rsp;
8301 	uint8_t *pcmd;
8302 	struct lpfc_iocbq *elsiocb;
8303 	uint32_t cmdsize;
8304 
8305 
8306 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8307 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8308 		/* reject the unsolicited RTV request and done with it */
8309 		goto reject_out;
8310 
8311 	cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
8312 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8313 				     lpfc_max_els_tries, ndlp,
8314 				     ndlp->nlp_DID, ELS_CMD_ACC);
8315 
8316 	if (!elsiocb)
8317 		return 1;
8318 
8319 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8320 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8321 	pcmd += sizeof(uint32_t); /* Skip past command */
8322 
8323 	/* use the command's xri in the response */
8324 	elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;  /* Xri / rx_id */
8325 	elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
8326 
8327 	rtv_rsp = (struct RTV_RSP *)pcmd;
8328 
8329 	/* populate RTV payload */
8330 	rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
8331 	rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
8332 	bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
8333 	bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
8334 	rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
8335 
8336 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
8337 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8338 			 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
8339 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
8340 			 "Data: x%x x%x x%x\n",
8341 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
8342 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8343 			 ndlp->nlp_rpi,
8344 			rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
8345 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
8346 	phba->fc_stat.elsXmitACC++;
8347 	elsiocb->context1 = lpfc_nlp_get(ndlp);
8348 	if (!elsiocb->context1) {
8349 		lpfc_els_free_iocb(phba, elsiocb);
8350 		return 0;
8351 	}
8352 
8353 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8354 	if (rc == IOCB_ERROR) {
8355 		lpfc_els_free_iocb(phba, elsiocb);
8356 		lpfc_nlp_put(ndlp);
8357 	}
8358 	return 0;
8359 
8360 reject_out:
8361 	/* issue rejection response */
8362 	stat.un.b.lsRjtRsvd0 = 0;
8363 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8364 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8365 	stat.un.b.vendorUnique = 0;
8366 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8367 	return 0;
8368 }
8369 
8370 /* lpfc_issue_els_rrq - Process an unsolicited rrq iocb
8371  * @vport: pointer to a host virtual N_Port data structure.
8372  * @ndlp: pointer to a node-list data structure.
8373  * @did: DID of the target.
8374  * @rrq: Pointer to the rrq struct.
8375  *
8376  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
8377  * Successful the the completion handler will clear the RRQ.
8378  *
8379  * Return codes
8380  *   0 - Successfully sent rrq els iocb.
8381  *   1 - Failed to send rrq els iocb.
8382  **/
8383 static int
8384 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
8385 			uint32_t did, struct lpfc_node_rrq *rrq)
8386 {
8387 	struct lpfc_hba  *phba = vport->phba;
8388 	struct RRQ *els_rrq;
8389 	struct lpfc_iocbq *elsiocb;
8390 	uint8_t *pcmd;
8391 	uint16_t cmdsize;
8392 	int ret;
8393 
8394 	if (!ndlp)
8395 		return 1;
8396 
8397 	/* If ndlp is not NULL, we will bump the reference count on it */
8398 	cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
8399 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
8400 				     ELS_CMD_RRQ);
8401 	if (!elsiocb)
8402 		return 1;
8403 
8404 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8405 
8406 	/* For RRQ request, remainder of payload is Exchange IDs */
8407 	*((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
8408 	pcmd += sizeof(uint32_t);
8409 	els_rrq = (struct RRQ *) pcmd;
8410 
8411 	bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
8412 	bf_set(rrq_rxid, els_rrq, rrq->rxid);
8413 	bf_set(rrq_did, els_rrq, vport->fc_myDID);
8414 	els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
8415 	els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
8416 
8417 
8418 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8419 		"Issue RRQ:     did:x%x",
8420 		did, rrq->xritag, rrq->rxid);
8421 	elsiocb->context_un.rrq = rrq;
8422 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
8423 
8424 	lpfc_nlp_get(ndlp);
8425 	elsiocb->context1 = ndlp;
8426 
8427 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8428 	if (ret == IOCB_ERROR)
8429 		goto io_err;
8430 	return 0;
8431 
8432  io_err:
8433 	lpfc_els_free_iocb(phba, elsiocb);
8434 	lpfc_nlp_put(ndlp);
8435 	return 1;
8436 }
8437 
8438 /**
8439  * lpfc_send_rrq - Sends ELS RRQ if needed.
8440  * @phba: pointer to lpfc hba data structure.
8441  * @rrq: pointer to the active rrq.
8442  *
8443  * This routine will call the lpfc_issue_els_rrq if the rrq is
8444  * still active for the xri. If this function returns a failure then
8445  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
8446  *
8447  * Returns 0 Success.
8448  *         1 Failure.
8449  **/
8450 int
8451 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
8452 {
8453 	struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
8454 						       rrq->nlp_DID);
8455 	if (!ndlp)
8456 		return 1;
8457 
8458 	if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
8459 		return lpfc_issue_els_rrq(rrq->vport, ndlp,
8460 					 rrq->nlp_DID, rrq);
8461 	else
8462 		return 1;
8463 }
8464 
8465 /**
8466  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
8467  * @vport: pointer to a host virtual N_Port data structure.
8468  * @cmdsize: size of the ELS command.
8469  * @oldiocb: pointer to the original lpfc command iocb data structure.
8470  * @ndlp: pointer to a node-list data structure.
8471  *
8472  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
8473  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
8474  *
8475  * Note that the ndlp reference count will be incremented by 1 for holding the
8476  * ndlp and the reference to ndlp will be stored into the context1 field of
8477  * the IOCB for the completion callback function to the RPL Accept Response
8478  * ELS command.
8479  *
8480  * Return code
8481  *   0 - Successfully issued ACC RPL ELS command
8482  *   1 - Failed to issue ACC RPL ELS command
8483  **/
8484 static int
8485 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
8486 		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
8487 {
8488 	int rc = 0;
8489 	struct lpfc_hba *phba = vport->phba;
8490 	IOCB_t *icmd, *oldcmd;
8491 	RPL_RSP rpl_rsp;
8492 	struct lpfc_iocbq *elsiocb;
8493 	uint8_t *pcmd;
8494 
8495 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
8496 				     ndlp->nlp_DID, ELS_CMD_ACC);
8497 
8498 	if (!elsiocb)
8499 		return 1;
8500 
8501 	icmd = &elsiocb->iocb;
8502 	oldcmd = &oldiocb->iocb;
8503 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
8504 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
8505 
8506 	pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8507 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8508 	pcmd += sizeof(uint16_t);
8509 	*((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
8510 	pcmd += sizeof(uint16_t);
8511 
8512 	/* Setup the RPL ACC payload */
8513 	rpl_rsp.listLen = be32_to_cpu(1);
8514 	rpl_rsp.index = 0;
8515 	rpl_rsp.port_num_blk.portNum = 0;
8516 	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
8517 	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
8518 	    sizeof(struct lpfc_name));
8519 	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
8520 	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
8521 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8522 			 "0120 Xmit ELS RPL ACC response tag x%x "
8523 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
8524 			 "rpi x%x\n",
8525 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
8526 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8527 			 ndlp->nlp_rpi);
8528 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
8529 	phba->fc_stat.elsXmitACC++;
8530 	elsiocb->context1 = lpfc_nlp_get(ndlp);
8531 	if (!elsiocb->context1) {
8532 		lpfc_els_free_iocb(phba, elsiocb);
8533 		return 1;
8534 	}
8535 
8536 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8537 	if (rc == IOCB_ERROR) {
8538 		lpfc_els_free_iocb(phba, elsiocb);
8539 		lpfc_nlp_put(ndlp);
8540 		return 1;
8541 	}
8542 
8543 	return 0;
8544 }
8545 
8546 /**
8547  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
8548  * @vport: pointer to a host virtual N_Port data structure.
8549  * @cmdiocb: pointer to lpfc command iocb data structure.
8550  * @ndlp: pointer to a node-list data structure.
8551  *
8552  * This routine processes Read Port List (RPL) IOCB received as an ELS
8553  * unsolicited event. It first checks the remote port state. If the remote
8554  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
8555  * invokes the lpfc_els_rsp_reject() routine to send reject response.
8556  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
8557  * to accept the RPL.
8558  *
8559  * Return code
8560  *   0 - Successfully processed rpl iocb (currently always return 0)
8561  **/
8562 static int
8563 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8564 		 struct lpfc_nodelist *ndlp)
8565 {
8566 	struct lpfc_dmabuf *pcmd;
8567 	uint32_t *lp;
8568 	uint32_t maxsize;
8569 	uint16_t cmdsize;
8570 	RPL *rpl;
8571 	struct ls_rjt stat;
8572 
8573 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8574 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
8575 		/* issue rejection response */
8576 		stat.un.b.lsRjtRsvd0 = 0;
8577 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8578 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8579 		stat.un.b.vendorUnique = 0;
8580 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
8581 			NULL);
8582 		/* rejected the unsolicited RPL request and done with it */
8583 		return 0;
8584 	}
8585 
8586 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
8587 	lp = (uint32_t *) pcmd->virt;
8588 	rpl = (RPL *) (lp + 1);
8589 	maxsize = be32_to_cpu(rpl->maxsize);
8590 
8591 	/* We support only one port */
8592 	if ((rpl->index == 0) &&
8593 	    ((maxsize == 0) ||
8594 	     ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
8595 		cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
8596 	} else {
8597 		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
8598 	}
8599 	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
8600 
8601 	return 0;
8602 }
8603 
8604 /**
8605  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
8606  * @vport: pointer to a virtual N_Port data structure.
8607  * @cmdiocb: pointer to lpfc command iocb data structure.
8608  * @ndlp: pointer to a node-list data structure.
8609  *
8610  * This routine processes Fibre Channel Address Resolution Protocol
8611  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
8612  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
8613  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
8614  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
8615  * remote PortName is compared against the FC PortName stored in the @vport
8616  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
8617  * compared against the FC NodeName stored in the @vport data structure.
8618  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
8619  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
8620  * invoked to send out FARP Response to the remote node. Before sending the
8621  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
8622  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
8623  * routine is invoked to log into the remote port first.
8624  *
8625  * Return code
8626  *   0 - Either the FARP Match Mode not supported or successfully processed
8627  **/
8628 static int
8629 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8630 		  struct lpfc_nodelist *ndlp)
8631 {
8632 	struct lpfc_dmabuf *pcmd;
8633 	uint32_t *lp;
8634 	IOCB_t *icmd;
8635 	FARP *fp;
8636 	uint32_t cnt, did;
8637 
8638 	icmd = &cmdiocb->iocb;
8639 	did = icmd->un.elsreq64.remoteID;
8640 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
8641 	lp = (uint32_t *) pcmd->virt;
8642 
8643 	lp++;
8644 	fp = (FARP *) lp;
8645 	/* FARP-REQ received from DID <did> */
8646 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8647 			 "0601 FARP-REQ received from DID x%x\n", did);
8648 	/* We will only support match on WWPN or WWNN */
8649 	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
8650 		return 0;
8651 	}
8652 
8653 	cnt = 0;
8654 	/* If this FARP command is searching for my portname */
8655 	if (fp->Mflags & FARP_MATCH_PORT) {
8656 		if (memcmp(&fp->RportName, &vport->fc_portname,
8657 			   sizeof(struct lpfc_name)) == 0)
8658 			cnt = 1;
8659 	}
8660 
8661 	/* If this FARP command is searching for my nodename */
8662 	if (fp->Mflags & FARP_MATCH_NODE) {
8663 		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
8664 			   sizeof(struct lpfc_name)) == 0)
8665 			cnt = 1;
8666 	}
8667 
8668 	if (cnt) {
8669 		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
8670 		   (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
8671 			/* Log back into the node before sending the FARP. */
8672 			if (fp->Rflags & FARP_REQUEST_PLOGI) {
8673 				ndlp->nlp_prev_state = ndlp->nlp_state;
8674 				lpfc_nlp_set_state(vport, ndlp,
8675 						   NLP_STE_PLOGI_ISSUE);
8676 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
8677 			}
8678 
8679 			/* Send a FARP response to that node */
8680 			if (fp->Rflags & FARP_REQUEST_FARPR)
8681 				lpfc_issue_els_farpr(vport, did, 0);
8682 		}
8683 	}
8684 	return 0;
8685 }
8686 
8687 /**
8688  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
8689  * @vport: pointer to a host virtual N_Port data structure.
8690  * @cmdiocb: pointer to lpfc command iocb data structure.
8691  * @ndlp: pointer to a node-list data structure.
8692  *
8693  * This routine processes Fibre Channel Address Resolution Protocol
8694  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
8695  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
8696  * the FARP response request.
8697  *
8698  * Return code
8699  *   0 - Successfully processed FARPR IOCB (currently always return 0)
8700  **/
8701 static int
8702 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8703 		   struct lpfc_nodelist  *ndlp)
8704 {
8705 	struct lpfc_dmabuf *pcmd;
8706 	uint32_t *lp;
8707 	IOCB_t *icmd;
8708 	uint32_t did;
8709 
8710 	icmd = &cmdiocb->iocb;
8711 	did = icmd->un.elsreq64.remoteID;
8712 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
8713 	lp = (uint32_t *) pcmd->virt;
8714 
8715 	lp++;
8716 	/* FARP-RSP received from DID <did> */
8717 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8718 			 "0600 FARP-RSP received from DID x%x\n", did);
8719 	/* ACCEPT the Farp resp request */
8720 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8721 
8722 	return 0;
8723 }
8724 
8725 /**
8726  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
8727  * @vport: pointer to a host virtual N_Port data structure.
8728  * @cmdiocb: pointer to lpfc command iocb data structure.
8729  * @fan_ndlp: pointer to a node-list data structure.
8730  *
8731  * This routine processes a Fabric Address Notification (FAN) IOCB
8732  * command received as an ELS unsolicited event. The FAN ELS command will
8733  * only be processed on a physical port (i.e., the @vport represents the
8734  * physical port). The fabric NodeName and PortName from the FAN IOCB are
8735  * compared against those in the phba data structure. If any of those is
8736  * different, the lpfc_initial_flogi() routine is invoked to initialize
8737  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
8738  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
8739  * is invoked to register login to the fabric.
8740  *
8741  * Return code
8742  *   0 - Successfully processed fan iocb (currently always return 0).
8743  **/
8744 static int
8745 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8746 		 struct lpfc_nodelist *fan_ndlp)
8747 {
8748 	struct lpfc_hba *phba = vport->phba;
8749 	uint32_t *lp;
8750 	FAN *fp;
8751 
8752 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
8753 	lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
8754 	fp = (FAN *) ++lp;
8755 	/* FAN received; Fan does not have a reply sequence */
8756 	if ((vport == phba->pport) &&
8757 	    (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
8758 		if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
8759 			    sizeof(struct lpfc_name))) ||
8760 		    (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
8761 			    sizeof(struct lpfc_name)))) {
8762 			/* This port has switched fabrics. FLOGI is required */
8763 			lpfc_issue_init_vfi(vport);
8764 		} else {
8765 			/* FAN verified - skip FLOGI */
8766 			vport->fc_myDID = vport->fc_prevDID;
8767 			if (phba->sli_rev < LPFC_SLI_REV4)
8768 				lpfc_issue_fabric_reglogin(vport);
8769 			else {
8770 				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8771 					"3138 Need register VFI: (x%x/%x)\n",
8772 					vport->fc_prevDID, vport->fc_myDID);
8773 				lpfc_issue_reg_vfi(vport);
8774 			}
8775 		}
8776 	}
8777 	return 0;
8778 }
8779 
8780 /**
8781  * lpfc_els_rcv_edc - Process an unsolicited EDC iocb
8782  * @vport: pointer to a host virtual N_Port data structure.
8783  * @cmdiocb: pointer to lpfc command iocb data structure.
8784  * @ndlp: pointer to a node-list data structure.
8785  *
8786  * Return code
8787  *   0 - Successfully processed echo iocb (currently always return 0)
8788  **/
8789 static int
8790 lpfc_els_rcv_edc(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8791 		 struct lpfc_nodelist *ndlp)
8792 {
8793 	struct lpfc_hba  *phba = vport->phba;
8794 	struct fc_els_edc *edc_req;
8795 	struct fc_tlv_desc *tlv;
8796 	uint8_t *payload;
8797 	uint32_t *ptr, dtag;
8798 	const char *dtag_nm;
8799 	int desc_cnt = 0, bytes_remain;
8800 	bool rcv_cap_desc = false;
8801 
8802 	payload = ((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
8803 
8804 	edc_req = (struct fc_els_edc *)payload;
8805 	bytes_remain = be32_to_cpu(edc_req->desc_len);
8806 
8807 	ptr = (uint32_t *)payload;
8808 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
8809 			 "3319 Rcv EDC payload len %d: x%x x%x x%x\n",
8810 			 bytes_remain, be32_to_cpu(*ptr),
8811 			 be32_to_cpu(*(ptr + 1)), be32_to_cpu(*(ptr + 2)));
8812 
8813 	/* No signal support unless there is a congestion descriptor */
8814 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
8815 	phba->cgn_sig_freq = 0;
8816 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
8817 
8818 	if (bytes_remain <= 0)
8819 		goto out;
8820 
8821 	tlv = edc_req->desc;
8822 
8823 	/*
8824 	 * cycle through EDC diagnostic descriptors to find the
8825 	 * congestion signaling capability descriptor
8826 	 */
8827 	while (bytes_remain && !rcv_cap_desc) {
8828 		if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
8829 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8830 					"6464 Truncated TLV hdr on "
8831 					"Diagnostic descriptor[%d]\n",
8832 					desc_cnt);
8833 			goto out;
8834 		}
8835 
8836 		dtag = be32_to_cpu(tlv->desc_tag);
8837 		switch (dtag) {
8838 		case ELS_DTAG_LNK_FAULT_CAP:
8839 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
8840 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
8841 				sizeof(struct fc_diag_lnkflt_desc)) {
8842 				lpfc_printf_log(
8843 					phba, KERN_WARNING, LOG_CGN_MGMT,
8844 					"6465 Truncated Link Fault Diagnostic "
8845 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
8846 					desc_cnt, bytes_remain,
8847 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
8848 					sizeof(struct fc_diag_cg_sig_desc));
8849 				goto out;
8850 			}
8851 			/* No action for Link Fault descriptor for now */
8852 			break;
8853 		case ELS_DTAG_CG_SIGNAL_CAP:
8854 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
8855 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
8856 				sizeof(struct fc_diag_cg_sig_desc)) {
8857 				lpfc_printf_log(
8858 					phba, KERN_WARNING, LOG_CGN_MGMT,
8859 					"6466 Truncated cgn signal Diagnostic "
8860 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
8861 					desc_cnt, bytes_remain,
8862 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
8863 					sizeof(struct fc_diag_cg_sig_desc));
8864 				goto out;
8865 			}
8866 
8867 			phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
8868 			phba->cgn_reg_signal = phba->cgn_init_reg_signal;
8869 
8870 			/* We start negotiation with lpfc_fabric_cgn_frequency.
8871 			 * When we process the EDC, we will settle on the
8872 			 * higher frequency.
8873 			 */
8874 			phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
8875 
8876 			lpfc_least_capable_settings(
8877 				phba, (struct fc_diag_cg_sig_desc *)tlv);
8878 			rcv_cap_desc = true;
8879 			break;
8880 		default:
8881 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
8882 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8883 					"6467 unknown Diagnostic "
8884 					"Descriptor[%d]: tag x%x (%s)\n",
8885 					desc_cnt, dtag, dtag_nm);
8886 		}
8887 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
8888 		tlv = fc_tlv_next_desc(tlv);
8889 		desc_cnt++;
8890 	}
8891 out:
8892 	/* Need to send back an ACC */
8893 	lpfc_issue_els_edc_rsp(vport, cmdiocb, ndlp);
8894 
8895 	lpfc_config_cgn_signal(phba);
8896 	return 0;
8897 }
8898 
8899 /**
8900  * lpfc_els_timeout - Handler funciton to the els timer
8901  * @t: timer context used to obtain the vport.
8902  *
8903  * This routine is invoked by the ELS timer after timeout. It posts the ELS
8904  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
8905  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
8906  * up the worker thread. It is for the worker thread to invoke the routine
8907  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
8908  **/
8909 void
8910 lpfc_els_timeout(struct timer_list *t)
8911 {
8912 	struct lpfc_vport *vport = from_timer(vport, t, els_tmofunc);
8913 	struct lpfc_hba   *phba = vport->phba;
8914 	uint32_t tmo_posted;
8915 	unsigned long iflag;
8916 
8917 	spin_lock_irqsave(&vport->work_port_lock, iflag);
8918 	tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
8919 	if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
8920 		vport->work_port_events |= WORKER_ELS_TMO;
8921 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
8922 
8923 	if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
8924 		lpfc_worker_wake_up(phba);
8925 	return;
8926 }
8927 
8928 
8929 /**
8930  * lpfc_els_timeout_handler - Process an els timeout event
8931  * @vport: pointer to a virtual N_Port data structure.
8932  *
8933  * This routine is the actual handler function that processes an ELS timeout
8934  * event. It walks the ELS ring to get and abort all the IOCBs (except the
8935  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
8936  * invoking the lpfc_sli_issue_abort_iotag() routine.
8937  **/
8938 void
8939 lpfc_els_timeout_handler(struct lpfc_vport *vport)
8940 {
8941 	struct lpfc_hba  *phba = vport->phba;
8942 	struct lpfc_sli_ring *pring;
8943 	struct lpfc_iocbq *tmp_iocb, *piocb;
8944 	IOCB_t *cmd = NULL;
8945 	struct lpfc_dmabuf *pcmd;
8946 	uint32_t els_command = 0;
8947 	uint32_t timeout;
8948 	uint32_t remote_ID = 0xffffffff;
8949 	LIST_HEAD(abort_list);
8950 
8951 
8952 	timeout = (uint32_t)(phba->fc_ratov << 1);
8953 
8954 	pring = lpfc_phba_elsring(phba);
8955 	if (unlikely(!pring))
8956 		return;
8957 
8958 	if (phba->pport->load_flag & FC_UNLOADING)
8959 		return;
8960 
8961 	spin_lock_irq(&phba->hbalock);
8962 	if (phba->sli_rev == LPFC_SLI_REV4)
8963 		spin_lock(&pring->ring_lock);
8964 
8965 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
8966 		cmd = &piocb->iocb;
8967 
8968 		if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
8969 		    piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
8970 		    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
8971 			continue;
8972 
8973 		if (piocb->vport != vport)
8974 			continue;
8975 
8976 		pcmd = (struct lpfc_dmabuf *) piocb->context2;
8977 		if (pcmd)
8978 			els_command = *(uint32_t *) (pcmd->virt);
8979 
8980 		if (els_command == ELS_CMD_FARP ||
8981 		    els_command == ELS_CMD_FARPR ||
8982 		    els_command == ELS_CMD_FDISC)
8983 			continue;
8984 
8985 		if (piocb->drvrTimeout > 0) {
8986 			if (piocb->drvrTimeout >= timeout)
8987 				piocb->drvrTimeout -= timeout;
8988 			else
8989 				piocb->drvrTimeout = 0;
8990 			continue;
8991 		}
8992 
8993 		remote_ID = 0xffffffff;
8994 		if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
8995 			remote_ID = cmd->un.elsreq64.remoteID;
8996 		else {
8997 			struct lpfc_nodelist *ndlp;
8998 			ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
8999 			if (ndlp)
9000 				remote_ID = ndlp->nlp_DID;
9001 		}
9002 		list_add_tail(&piocb->dlist, &abort_list);
9003 	}
9004 	if (phba->sli_rev == LPFC_SLI_REV4)
9005 		spin_unlock(&pring->ring_lock);
9006 	spin_unlock_irq(&phba->hbalock);
9007 
9008 	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9009 		cmd = &piocb->iocb;
9010 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9011 			 "0127 ELS timeout Data: x%x x%x x%x "
9012 			 "x%x\n", els_command,
9013 			 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
9014 		spin_lock_irq(&phba->hbalock);
9015 		list_del_init(&piocb->dlist);
9016 		lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9017 		spin_unlock_irq(&phba->hbalock);
9018 	}
9019 
9020 	/* Make sure HBA is alive */
9021 	lpfc_issue_hb_tmo(phba);
9022 
9023 	if (!list_empty(&pring->txcmplq))
9024 		if (!(phba->pport->load_flag & FC_UNLOADING))
9025 			mod_timer(&vport->els_tmofunc,
9026 				  jiffies + msecs_to_jiffies(1000 * timeout));
9027 }
9028 
9029 /**
9030  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
9031  * @vport: pointer to a host virtual N_Port data structure.
9032  *
9033  * This routine is used to clean up all the outstanding ELS commands on a
9034  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
9035  * routine. After that, it walks the ELS transmit queue to remove all the
9036  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
9037  * the IOCBs with a non-NULL completion callback function, the callback
9038  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9039  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
9040  * callback function, the IOCB will simply be released. Finally, it walks
9041  * the ELS transmit completion queue to issue an abort IOCB to any transmit
9042  * completion queue IOCB that is associated with the @vport and is not
9043  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
9044  * part of the discovery state machine) out to HBA by invoking the
9045  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
9046  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
9047  * the IOCBs are aborted when this function returns.
9048  **/
9049 void
9050 lpfc_els_flush_cmd(struct lpfc_vport *vport)
9051 {
9052 	LIST_HEAD(abort_list);
9053 	struct lpfc_hba  *phba = vport->phba;
9054 	struct lpfc_sli_ring *pring;
9055 	struct lpfc_iocbq *tmp_iocb, *piocb;
9056 	IOCB_t *cmd = NULL;
9057 	unsigned long iflags = 0;
9058 
9059 	lpfc_fabric_abort_vport(vport);
9060 
9061 	/*
9062 	 * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
9063 	 * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
9064 	 * ultimately grabs the ring_lock, the driver must splice the list into
9065 	 * a working list and release the locks before calling the abort.
9066 	 */
9067 	spin_lock_irqsave(&phba->hbalock, iflags);
9068 	pring = lpfc_phba_elsring(phba);
9069 
9070 	/* Bail out if we've no ELS wq, like in PCI error recovery case. */
9071 	if (unlikely(!pring)) {
9072 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9073 		return;
9074 	}
9075 
9076 	if (phba->sli_rev == LPFC_SLI_REV4)
9077 		spin_lock(&pring->ring_lock);
9078 
9079 	/* First we need to issue aborts to outstanding cmds on txcmpl */
9080 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
9081 		if (piocb->iocb_flag & LPFC_IO_LIBDFC)
9082 			continue;
9083 
9084 		if (piocb->vport != vport)
9085 			continue;
9086 
9087 		if (piocb->iocb_flag & LPFC_DRIVER_ABORTED)
9088 			continue;
9089 
9090 		/* On the ELS ring we can have ELS_REQUESTs or
9091 		 * GEN_REQUESTs waiting for a response.
9092 		 */
9093 		cmd = &piocb->iocb;
9094 		if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
9095 			list_add_tail(&piocb->dlist, &abort_list);
9096 
9097 			/* If the link is down when flushing ELS commands
9098 			 * the firmware will not complete them till after
9099 			 * the link comes back up. This may confuse
9100 			 * discovery for the new link up, so we need to
9101 			 * change the compl routine to just clean up the iocb
9102 			 * and avoid any retry logic.
9103 			 */
9104 			if (phba->link_state == LPFC_LINK_DOWN)
9105 				piocb->iocb_cmpl = lpfc_cmpl_els_link_down;
9106 		}
9107 		if (cmd->ulpCommand == CMD_GEN_REQUEST64_CR)
9108 			list_add_tail(&piocb->dlist, &abort_list);
9109 	}
9110 
9111 	if (phba->sli_rev == LPFC_SLI_REV4)
9112 		spin_unlock(&pring->ring_lock);
9113 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9114 
9115 	/* Abort each txcmpl iocb on aborted list and remove the dlist links. */
9116 	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9117 		spin_lock_irqsave(&phba->hbalock, iflags);
9118 		list_del_init(&piocb->dlist);
9119 		lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9120 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9121 	}
9122 	/* Make sure HBA is alive */
9123 	lpfc_issue_hb_tmo(phba);
9124 
9125 	if (!list_empty(&abort_list))
9126 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9127 				 "3387 abort list for txq not empty\n");
9128 	INIT_LIST_HEAD(&abort_list);
9129 
9130 	spin_lock_irqsave(&phba->hbalock, iflags);
9131 	if (phba->sli_rev == LPFC_SLI_REV4)
9132 		spin_lock(&pring->ring_lock);
9133 
9134 	/* No need to abort the txq list,
9135 	 * just queue them up for lpfc_sli_cancel_iocbs
9136 	 */
9137 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
9138 		cmd = &piocb->iocb;
9139 
9140 		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
9141 			continue;
9142 		}
9143 
9144 		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
9145 		if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
9146 		    cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
9147 		    cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9148 		    cmd->ulpCommand == CMD_ABORT_XRI_CN)
9149 			continue;
9150 
9151 		if (piocb->vport != vport)
9152 			continue;
9153 
9154 		list_del_init(&piocb->list);
9155 		list_add_tail(&piocb->list, &abort_list);
9156 	}
9157 
9158 	/* The same holds true for any FLOGI/FDISC on the fabric_iocb_list */
9159 	if (vport == phba->pport) {
9160 		list_for_each_entry_safe(piocb, tmp_iocb,
9161 					 &phba->fabric_iocb_list, list) {
9162 			cmd = &piocb->iocb;
9163 			list_del_init(&piocb->list);
9164 			list_add_tail(&piocb->list, &abort_list);
9165 		}
9166 	}
9167 
9168 	if (phba->sli_rev == LPFC_SLI_REV4)
9169 		spin_unlock(&pring->ring_lock);
9170 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9171 
9172 	/* Cancel all the IOCBs from the completions list */
9173 	lpfc_sli_cancel_iocbs(phba, &abort_list,
9174 			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
9175 
9176 	return;
9177 }
9178 
9179 /**
9180  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
9181  * @phba: pointer to lpfc hba data structure.
9182  *
9183  * This routine is used to clean up all the outstanding ELS commands on a
9184  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
9185  * routine. After that, it walks the ELS transmit queue to remove all the
9186  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
9187  * the IOCBs with the completion callback function associated, the callback
9188  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9189  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
9190  * callback function associated, the IOCB will simply be released. Finally,
9191  * it walks the ELS transmit completion queue to issue an abort IOCB to any
9192  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
9193  * management plane IOCBs that are not part of the discovery state machine)
9194  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
9195  **/
9196 void
9197 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
9198 {
9199 	struct lpfc_vport *vport;
9200 
9201 	spin_lock_irq(&phba->port_list_lock);
9202 	list_for_each_entry(vport, &phba->port_list, listentry)
9203 		lpfc_els_flush_cmd(vport);
9204 	spin_unlock_irq(&phba->port_list_lock);
9205 
9206 	return;
9207 }
9208 
9209 /**
9210  * lpfc_send_els_failure_event - Posts an ELS command failure event
9211  * @phba: Pointer to hba context object.
9212  * @cmdiocbp: Pointer to command iocb which reported error.
9213  * @rspiocbp: Pointer to response iocb which reported error.
9214  *
9215  * This function sends an event when there is an ELS command
9216  * failure.
9217  **/
9218 void
9219 lpfc_send_els_failure_event(struct lpfc_hba *phba,
9220 			struct lpfc_iocbq *cmdiocbp,
9221 			struct lpfc_iocbq *rspiocbp)
9222 {
9223 	struct lpfc_vport *vport = cmdiocbp->vport;
9224 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9225 	struct lpfc_lsrjt_event lsrjt_event;
9226 	struct lpfc_fabric_event_header fabric_event;
9227 	struct ls_rjt stat;
9228 	struct lpfc_nodelist *ndlp;
9229 	uint32_t *pcmd;
9230 
9231 	ndlp = cmdiocbp->context1;
9232 	if (!ndlp)
9233 		return;
9234 
9235 	if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
9236 		lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
9237 		lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
9238 		memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
9239 			sizeof(struct lpfc_name));
9240 		memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
9241 			sizeof(struct lpfc_name));
9242 		pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9243 			cmdiocbp->context2)->virt);
9244 		lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
9245 		stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
9246 		lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
9247 		lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
9248 		fc_host_post_vendor_event(shost,
9249 			fc_get_event_number(),
9250 			sizeof(lsrjt_event),
9251 			(char *)&lsrjt_event,
9252 			LPFC_NL_VENDOR_ID);
9253 		return;
9254 	}
9255 	if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
9256 		(rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
9257 		fabric_event.event_type = FC_REG_FABRIC_EVENT;
9258 		if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
9259 			fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
9260 		else
9261 			fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
9262 		memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
9263 			sizeof(struct lpfc_name));
9264 		memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
9265 			sizeof(struct lpfc_name));
9266 		fc_host_post_vendor_event(shost,
9267 			fc_get_event_number(),
9268 			sizeof(fabric_event),
9269 			(char *)&fabric_event,
9270 			LPFC_NL_VENDOR_ID);
9271 		return;
9272 	}
9273 
9274 }
9275 
9276 /**
9277  * lpfc_send_els_event - Posts unsolicited els event
9278  * @vport: Pointer to vport object.
9279  * @ndlp: Pointer FC node object.
9280  * @payload: ELS command code type.
9281  *
9282  * This function posts an event when there is an incoming
9283  * unsolicited ELS command.
9284  **/
9285 static void
9286 lpfc_send_els_event(struct lpfc_vport *vport,
9287 		    struct lpfc_nodelist *ndlp,
9288 		    uint32_t *payload)
9289 {
9290 	struct lpfc_els_event_header *els_data = NULL;
9291 	struct lpfc_logo_event *logo_data = NULL;
9292 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9293 
9294 	if (*payload == ELS_CMD_LOGO) {
9295 		logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
9296 		if (!logo_data) {
9297 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9298 				"0148 Failed to allocate memory "
9299 				"for LOGO event\n");
9300 			return;
9301 		}
9302 		els_data = &logo_data->header;
9303 	} else {
9304 		els_data = kmalloc(sizeof(struct lpfc_els_event_header),
9305 			GFP_KERNEL);
9306 		if (!els_data) {
9307 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9308 				"0149 Failed to allocate memory "
9309 				"for ELS event\n");
9310 			return;
9311 		}
9312 	}
9313 	els_data->event_type = FC_REG_ELS_EVENT;
9314 	switch (*payload) {
9315 	case ELS_CMD_PLOGI:
9316 		els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
9317 		break;
9318 	case ELS_CMD_PRLO:
9319 		els_data->subcategory = LPFC_EVENT_PRLO_RCV;
9320 		break;
9321 	case ELS_CMD_ADISC:
9322 		els_data->subcategory = LPFC_EVENT_ADISC_RCV;
9323 		break;
9324 	case ELS_CMD_LOGO:
9325 		els_data->subcategory = LPFC_EVENT_LOGO_RCV;
9326 		/* Copy the WWPN in the LOGO payload */
9327 		memcpy(logo_data->logo_wwpn, &payload[2],
9328 			sizeof(struct lpfc_name));
9329 		break;
9330 	default:
9331 		kfree(els_data);
9332 		return;
9333 	}
9334 	memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
9335 	memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
9336 	if (*payload == ELS_CMD_LOGO) {
9337 		fc_host_post_vendor_event(shost,
9338 			fc_get_event_number(),
9339 			sizeof(struct lpfc_logo_event),
9340 			(char *)logo_data,
9341 			LPFC_NL_VENDOR_ID);
9342 		kfree(logo_data);
9343 	} else {
9344 		fc_host_post_vendor_event(shost,
9345 			fc_get_event_number(),
9346 			sizeof(struct lpfc_els_event_header),
9347 			(char *)els_data,
9348 			LPFC_NL_VENDOR_ID);
9349 		kfree(els_data);
9350 	}
9351 
9352 	return;
9353 }
9354 
9355 
9356 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_li_event_nm, fc_fpin_li_event_types,
9357 			FC_FPIN_LI_EVT_TYPES_INIT);
9358 
9359 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_deli_event_nm, fc_fpin_deli_event_types,
9360 			FC_FPIN_DELI_EVT_TYPES_INIT);
9361 
9362 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_event_nm, fc_fpin_congn_event_types,
9363 			FC_FPIN_CONGN_EVT_TYPES_INIT);
9364 
9365 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_severity_nm,
9366 			fc_fpin_congn_severity_types,
9367 			FC_FPIN_CONGN_SEVERITY_INIT);
9368 
9369 
9370 /**
9371  * lpfc_display_fpin_wwpn - Display WWPNs accessible by the attached port
9372  * @phba: Pointer to phba object.
9373  * @wwnlist: Pointer to list of WWPNs in FPIN payload
9374  * @cnt: count of WWPNs in FPIN payload
9375  *
9376  * This routine is called by LI and PC descriptors.
9377  * Limit the number of WWPNs displayed to 6 log messages, 6 per log message
9378  */
9379 static void
9380 lpfc_display_fpin_wwpn(struct lpfc_hba *phba, __be64 *wwnlist, u32 cnt)
9381 {
9382 	char buf[LPFC_FPIN_WWPN_LINE_SZ];
9383 	__be64 wwn;
9384 	u64 wwpn;
9385 	int i, len;
9386 	int line = 0;
9387 	int wcnt = 0;
9388 	bool endit = false;
9389 
9390 	len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ, "Accessible WWPNs:");
9391 	for (i = 0; i < cnt; i++) {
9392 		/* Are we on the last WWPN */
9393 		if (i == (cnt - 1))
9394 			endit = true;
9395 
9396 		/* Extract the next WWPN from the payload */
9397 		wwn = *wwnlist++;
9398 		wwpn = be64_to_cpu(wwn);
9399 		len += scnprintf(buf + len, LPFC_FPIN_WWPN_LINE_SZ,
9400 				 " %016llx", wwpn);
9401 
9402 		/* Log a message if we are on the last WWPN
9403 		 * or if we hit the max allowed per message.
9404 		 */
9405 		wcnt++;
9406 		if (wcnt == LPFC_FPIN_WWPN_LINE_CNT || endit) {
9407 			buf[len] = 0;
9408 			lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9409 					"4686 %s\n", buf);
9410 
9411 			/* Check if we reached the last WWPN */
9412 			if (endit)
9413 				return;
9414 
9415 			/* Limit the number of log message displayed per FPIN */
9416 			line++;
9417 			if (line == LPFC_FPIN_WWPN_NUM_LINE) {
9418 				lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9419 						"4687 %d WWPNs Truncated\n",
9420 						cnt - i - 1);
9421 				return;
9422 			}
9423 
9424 			/* Start over with next log message */
9425 			wcnt = 0;
9426 			len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ,
9427 					"Additional WWPNs:");
9428 		}
9429 	}
9430 }
9431 
9432 /**
9433  * lpfc_els_rcv_fpin_li - Process an FPIN Link Integrity Event.
9434  * @phba: Pointer to phba object.
9435  * @tlv:  Pointer to the Link Integrity Notification Descriptor.
9436  *
9437  * This function processes a Link Integrity FPIN event by logging a message.
9438  **/
9439 static void
9440 lpfc_els_rcv_fpin_li(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9441 {
9442 	struct fc_fn_li_desc *li = (struct fc_fn_li_desc *)tlv;
9443 	const char *li_evt_str;
9444 	u32 li_evt, cnt;
9445 
9446 	li_evt = be16_to_cpu(li->event_type);
9447 	li_evt_str = lpfc_get_fpin_li_event_nm(li_evt);
9448 	cnt = be32_to_cpu(li->pname_count);
9449 
9450 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9451 			"4680 FPIN Link Integrity %s (x%x) "
9452 			"Detecting PN x%016llx Attached PN x%016llx "
9453 			"Duration %d mSecs Count %d Port Cnt %d\n",
9454 			li_evt_str, li_evt,
9455 			be64_to_cpu(li->detecting_wwpn),
9456 			be64_to_cpu(li->attached_wwpn),
9457 			be32_to_cpu(li->event_threshold),
9458 			be32_to_cpu(li->event_count), cnt);
9459 
9460 	lpfc_display_fpin_wwpn(phba, (__be64 *)&li->pname_list, cnt);
9461 }
9462 
9463 /**
9464  * lpfc_els_rcv_fpin_del - Process an FPIN Delivery Event.
9465  * @phba: Pointer to hba object.
9466  * @tlv:  Pointer to the Delivery Notification Descriptor TLV
9467  *
9468  * This function processes a Delivery FPIN event by logging a message.
9469  **/
9470 static void
9471 lpfc_els_rcv_fpin_del(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9472 {
9473 	struct fc_fn_deli_desc *del = (struct fc_fn_deli_desc *)tlv;
9474 	const char *del_rsn_str;
9475 	u32 del_rsn;
9476 	__be32 *frame;
9477 
9478 	del_rsn = be16_to_cpu(del->deli_reason_code);
9479 	del_rsn_str = lpfc_get_fpin_deli_event_nm(del_rsn);
9480 
9481 	/* Skip over desc_tag/desc_len header to payload */
9482 	frame = (__be32 *)(del + 1);
9483 
9484 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9485 			"4681 FPIN Delivery %s (x%x) "
9486 			"Detecting PN x%016llx Attached PN x%016llx "
9487 			"DiscHdr0  x%08x "
9488 			"DiscHdr1 x%08x DiscHdr2 x%08x DiscHdr3 x%08x "
9489 			"DiscHdr4 x%08x DiscHdr5 x%08x\n",
9490 			del_rsn_str, del_rsn,
9491 			be64_to_cpu(del->detecting_wwpn),
9492 			be64_to_cpu(del->attached_wwpn),
9493 			be32_to_cpu(frame[0]),
9494 			be32_to_cpu(frame[1]),
9495 			be32_to_cpu(frame[2]),
9496 			be32_to_cpu(frame[3]),
9497 			be32_to_cpu(frame[4]),
9498 			be32_to_cpu(frame[5]));
9499 }
9500 
9501 /**
9502  * lpfc_els_rcv_fpin_peer_cgn - Process a FPIN Peer Congestion Event.
9503  * @phba: Pointer to hba object.
9504  * @tlv:  Pointer to the Peer Congestion Notification Descriptor TLV
9505  *
9506  * This function processes a Peer Congestion FPIN event by logging a message.
9507  **/
9508 static void
9509 lpfc_els_rcv_fpin_peer_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9510 {
9511 	struct fc_fn_peer_congn_desc *pc = (struct fc_fn_peer_congn_desc *)tlv;
9512 	const char *pc_evt_str;
9513 	u32 pc_evt, cnt;
9514 
9515 	pc_evt = be16_to_cpu(pc->event_type);
9516 	pc_evt_str = lpfc_get_fpin_congn_event_nm(pc_evt);
9517 	cnt = be32_to_cpu(pc->pname_count);
9518 
9519 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_ELS,
9520 			"4684 FPIN Peer Congestion %s (x%x) "
9521 			"Duration %d mSecs "
9522 			"Detecting PN x%016llx Attached PN x%016llx "
9523 			"Impacted Port Cnt %d\n",
9524 			pc_evt_str, pc_evt,
9525 			be32_to_cpu(pc->event_period),
9526 			be64_to_cpu(pc->detecting_wwpn),
9527 			be64_to_cpu(pc->attached_wwpn),
9528 			cnt);
9529 
9530 	lpfc_display_fpin_wwpn(phba, (__be64 *)&pc->pname_list, cnt);
9531 }
9532 
9533 /**
9534  * lpfc_els_rcv_fpin_cgn - Process an FPIN Congestion notification
9535  * @phba: Pointer to hba object.
9536  * @tlv:  Pointer to the Congestion Notification Descriptor TLV
9537  *
9538  * This function processes an FPIN Congestion Notifiction.  The notification
9539  * could be an Alarm or Warning.  This routine feeds that data into driver's
9540  * running congestion algorithm. It also processes the FPIN by
9541  * logging a message. It returns 1 to indicate deliver this message
9542  * to the upper layer or 0 to indicate don't deliver it.
9543  **/
9544 static int
9545 lpfc_els_rcv_fpin_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
9546 {
9547 	struct lpfc_cgn_info *cp;
9548 	struct fc_fn_congn_desc *cgn = (struct fc_fn_congn_desc *)tlv;
9549 	const char *cgn_evt_str;
9550 	u32 cgn_evt;
9551 	const char *cgn_sev_str;
9552 	u32 cgn_sev;
9553 	uint16_t value;
9554 	u32 crc;
9555 	bool nm_log = false;
9556 	int rc = 1;
9557 
9558 	cgn_evt = be16_to_cpu(cgn->event_type);
9559 	cgn_evt_str = lpfc_get_fpin_congn_event_nm(cgn_evt);
9560 	cgn_sev = cgn->severity;
9561 	cgn_sev_str = lpfc_get_fpin_congn_severity_nm(cgn_sev);
9562 
9563 	/* The driver only takes action on a Credit Stall or Oversubscription
9564 	 * event type to engage the IO algorithm.  The driver prints an
9565 	 * unmaskable message only for Lost Credit and Credit Stall.
9566 	 * TODO: Still need to have definition of host action on clear,
9567 	 *       lost credit and device specific event types.
9568 	 */
9569 	switch (cgn_evt) {
9570 	case FPIN_CONGN_LOST_CREDIT:
9571 		nm_log = true;
9572 		break;
9573 	case FPIN_CONGN_CREDIT_STALL:
9574 		nm_log = true;
9575 		fallthrough;
9576 	case FPIN_CONGN_OVERSUBSCRIPTION:
9577 		if (cgn_evt == FPIN_CONGN_OVERSUBSCRIPTION)
9578 			nm_log = false;
9579 		switch (cgn_sev) {
9580 		case FPIN_CONGN_SEVERITY_ERROR:
9581 			/* Take action here for an Alarm event */
9582 			if (phba->cmf_active_mode != LPFC_CFG_OFF) {
9583 				if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_ALARM) {
9584 					/* Track of alarm cnt for cgn_info */
9585 					atomic_inc(&phba->cgn_fabric_alarm_cnt);
9586 					/* Track of alarm cnt for SYNC_WQE */
9587 					atomic_inc(&phba->cgn_sync_alarm_cnt);
9588 				}
9589 				goto cleanup;
9590 			}
9591 			break;
9592 		case FPIN_CONGN_SEVERITY_WARNING:
9593 			/* Take action here for a Warning event */
9594 			if (phba->cmf_active_mode != LPFC_CFG_OFF) {
9595 				if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_WARN) {
9596 					/* Track of warning cnt for cgn_info */
9597 					atomic_inc(&phba->cgn_fabric_warn_cnt);
9598 					/* Track of warning cnt for SYNC_WQE */
9599 					atomic_inc(&phba->cgn_sync_warn_cnt);
9600 				}
9601 cleanup:
9602 				/* Save frequency in ms */
9603 				phba->cgn_fpin_frequency =
9604 					be32_to_cpu(cgn->event_period);
9605 				value = phba->cgn_fpin_frequency;
9606 				if (phba->cgn_i) {
9607 					cp = (struct lpfc_cgn_info *)
9608 						phba->cgn_i->virt;
9609 					if (phba->cgn_reg_fpin &
9610 						LPFC_CGN_FPIN_ALARM)
9611 						cp->cgn_alarm_freq =
9612 							cpu_to_le16(value);
9613 					if (phba->cgn_reg_fpin &
9614 						LPFC_CGN_FPIN_WARN)
9615 						cp->cgn_warn_freq =
9616 							cpu_to_le16(value);
9617 					crc = lpfc_cgn_calc_crc32
9618 						(cp,
9619 						LPFC_CGN_INFO_SZ,
9620 						LPFC_CGN_CRC32_SEED);
9621 					cp->cgn_info_crc = cpu_to_le32(crc);
9622 				}
9623 
9624 				/* Don't deliver to upper layer since
9625 				 * driver took action on this tlv.
9626 				 */
9627 				rc = 0;
9628 			}
9629 			break;
9630 		}
9631 		break;
9632 	}
9633 
9634 	/* Change the log level to unmaskable for the following event types. */
9635 	lpfc_printf_log(phba, (nm_log ? KERN_WARNING : KERN_INFO),
9636 			LOG_CGN_MGMT | LOG_ELS,
9637 			"4683 FPIN CONGESTION %s type %s (x%x) Event "
9638 			"Duration %d mSecs\n",
9639 			cgn_sev_str, cgn_evt_str, cgn_evt,
9640 			be32_to_cpu(cgn->event_period));
9641 	return rc;
9642 }
9643 
9644 void
9645 lpfc_els_rcv_fpin(struct lpfc_vport *vport, void *p, u32 fpin_length)
9646 {
9647 	struct lpfc_hba *phba = vport->phba;
9648 	struct fc_els_fpin *fpin = (struct fc_els_fpin *)p;
9649 	struct fc_tlv_desc *tlv, *first_tlv, *current_tlv;
9650 	const char *dtag_nm;
9651 	int desc_cnt = 0, bytes_remain, cnt;
9652 	u32 dtag, deliver = 0;
9653 	int len;
9654 
9655 	/* FPINs handled only if we are in the right discovery state */
9656 	if (vport->port_state < LPFC_DISC_AUTH)
9657 		return;
9658 
9659 	/* make sure there is the full fpin header */
9660 	if (fpin_length < sizeof(struct fc_els_fpin))
9661 		return;
9662 
9663 	/* Sanity check descriptor length. The desc_len value does not
9664 	 * include space for the ELS command and the desc_len fields.
9665 	 */
9666 	len = be32_to_cpu(fpin->desc_len);
9667 	if (fpin_length < len + sizeof(struct fc_els_fpin)) {
9668 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
9669 				"4671 Bad ELS FPIN length %d: %d\n",
9670 				len, fpin_length);
9671 		return;
9672 	}
9673 
9674 	tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
9675 	first_tlv = tlv;
9676 	bytes_remain = fpin_length - offsetof(struct fc_els_fpin, fpin_desc);
9677 	bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
9678 
9679 	/* process each descriptor separately */
9680 	while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
9681 	       bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
9682 		dtag = be32_to_cpu(tlv->desc_tag);
9683 		switch (dtag) {
9684 		case ELS_DTAG_LNK_INTEGRITY:
9685 			lpfc_els_rcv_fpin_li(phba, tlv);
9686 			deliver = 1;
9687 			break;
9688 		case ELS_DTAG_DELIVERY:
9689 			lpfc_els_rcv_fpin_del(phba, tlv);
9690 			deliver = 1;
9691 			break;
9692 		case ELS_DTAG_PEER_CONGEST:
9693 			lpfc_els_rcv_fpin_peer_cgn(phba, tlv);
9694 			deliver = 1;
9695 			break;
9696 		case ELS_DTAG_CONGESTION:
9697 			deliver = lpfc_els_rcv_fpin_cgn(phba, tlv);
9698 			break;
9699 		default:
9700 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
9701 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
9702 					"4678 unknown FPIN descriptor[%d]: "
9703 					"tag x%x (%s)\n",
9704 					desc_cnt, dtag, dtag_nm);
9705 
9706 			/* If descriptor is bad, drop the rest of the data */
9707 			return;
9708 		}
9709 		lpfc_cgn_update_stat(phba, dtag);
9710 		cnt = be32_to_cpu(tlv->desc_len);
9711 
9712 		/* Sanity check descriptor length. The desc_len value does not
9713 		 * include space for the desc_tag and the desc_len fields.
9714 		 */
9715 		len -= (cnt + sizeof(struct fc_tlv_desc));
9716 		if (len < 0) {
9717 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
9718 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
9719 					"4672 Bad FPIN descriptor TLV length "
9720 					"%d: %d %d %s\n",
9721 					cnt, len, fpin_length, dtag_nm);
9722 			return;
9723 		}
9724 
9725 		current_tlv = tlv;
9726 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
9727 		tlv = fc_tlv_next_desc(tlv);
9728 
9729 		/* Format payload such that the FPIN delivered to the
9730 		 * upper layer is a single descriptor FPIN.
9731 		 */
9732 		if (desc_cnt)
9733 			memcpy(first_tlv, current_tlv,
9734 			       (cnt + sizeof(struct fc_els_fpin)));
9735 
9736 		/* Adjust the length so that it only reflects a
9737 		 * single descriptor FPIN.
9738 		 */
9739 		fpin_length = cnt + sizeof(struct fc_els_fpin);
9740 		fpin->desc_len = cpu_to_be32(fpin_length);
9741 		fpin_length += sizeof(struct fc_els_fpin); /* the entire FPIN */
9742 
9743 		/* Send every descriptor individually to the upper layer */
9744 		if (deliver)
9745 			fc_host_fpin_rcv(lpfc_shost_from_vport(vport),
9746 					 fpin_length, (char *)fpin);
9747 		desc_cnt++;
9748 	}
9749 }
9750 
9751 /**
9752  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
9753  * @phba: pointer to lpfc hba data structure.
9754  * @pring: pointer to a SLI ring.
9755  * @vport: pointer to a host virtual N_Port data structure.
9756  * @elsiocb: pointer to lpfc els command iocb data structure.
9757  *
9758  * This routine is used for processing the IOCB associated with a unsolicited
9759  * event. It first determines whether there is an existing ndlp that matches
9760  * the DID from the unsolicited IOCB. If not, it will create a new one with
9761  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
9762  * IOCB is then used to invoke the proper routine and to set up proper state
9763  * of the discovery state machine.
9764  **/
9765 static void
9766 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9767 		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
9768 {
9769 	struct lpfc_nodelist *ndlp;
9770 	struct ls_rjt stat;
9771 	uint32_t *payload, payload_len;
9772 	uint32_t cmd, did, newnode;
9773 	uint8_t rjt_exp, rjt_err = 0, init_link = 0;
9774 	IOCB_t *icmd = &elsiocb->iocb;
9775 	LPFC_MBOXQ_t *mbox;
9776 
9777 	if (!vport || !(elsiocb->context2))
9778 		goto dropit;
9779 
9780 	newnode = 0;
9781 	payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
9782 	payload_len = elsiocb->iocb.unsli3.rcvsli3.acc_len;
9783 	cmd = *payload;
9784 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
9785 		lpfc_post_buffer(phba, pring, 1);
9786 
9787 	did = icmd->un.rcvels.remoteID;
9788 	if (icmd->ulpStatus) {
9789 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9790 			"RCV Unsol ELS:  status:x%x/x%x did:x%x",
9791 			icmd->ulpStatus, icmd->un.ulpWord[4], did);
9792 		goto dropit;
9793 	}
9794 
9795 	/* Check to see if link went down during discovery */
9796 	if (lpfc_els_chk_latt(vport))
9797 		goto dropit;
9798 
9799 	/* Ignore traffic received during vport shutdown. */
9800 	if (vport->load_flag & FC_UNLOADING)
9801 		goto dropit;
9802 
9803 	/* If NPort discovery is delayed drop incoming ELS */
9804 	if ((vport->fc_flag & FC_DISC_DELAYED) &&
9805 			(cmd != ELS_CMD_PLOGI))
9806 		goto dropit;
9807 
9808 	ndlp = lpfc_findnode_did(vport, did);
9809 	if (!ndlp) {
9810 		/* Cannot find existing Fabric ndlp, so allocate a new one */
9811 		ndlp = lpfc_nlp_init(vport, did);
9812 		if (!ndlp)
9813 			goto dropit;
9814 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
9815 		newnode = 1;
9816 		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
9817 			ndlp->nlp_type |= NLP_FABRIC;
9818 	} else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
9819 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
9820 		newnode = 1;
9821 	}
9822 
9823 	phba->fc_stat.elsRcvFrame++;
9824 
9825 	/*
9826 	 * Do not process any unsolicited ELS commands
9827 	 * if the ndlp is in DEV_LOSS
9828 	 */
9829 	spin_lock_irq(&ndlp->lock);
9830 	if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
9831 		spin_unlock_irq(&ndlp->lock);
9832 		if (newnode)
9833 			lpfc_nlp_put(ndlp);
9834 		goto dropit;
9835 	}
9836 	spin_unlock_irq(&ndlp->lock);
9837 
9838 	elsiocb->context1 = lpfc_nlp_get(ndlp);
9839 	if (!elsiocb->context1)
9840 		goto dropit;
9841 	elsiocb->vport = vport;
9842 
9843 	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
9844 		cmd &= ELS_CMD_MASK;
9845 	}
9846 	/* ELS command <elsCmd> received from NPORT <did> */
9847 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9848 			 "0112 ELS command x%x received from NPORT x%x "
9849 			 "refcnt %d Data: x%x x%x x%x x%x\n",
9850 			 cmd, did, kref_read(&ndlp->kref), vport->port_state,
9851 			 vport->fc_flag, vport->fc_myDID, vport->fc_prevDID);
9852 
9853 	/* reject till our FLOGI completes or PLOGI assigned DID via PT2PT */
9854 	if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
9855 	    (cmd != ELS_CMD_FLOGI) &&
9856 	    !((cmd == ELS_CMD_PLOGI) && (vport->fc_flag & FC_PT2PT))) {
9857 		rjt_err = LSRJT_LOGICAL_BSY;
9858 		rjt_exp = LSEXP_NOTHING_MORE;
9859 		goto lsrjt;
9860 	}
9861 
9862 	switch (cmd) {
9863 	case ELS_CMD_PLOGI:
9864 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9865 			"RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
9866 			did, vport->port_state, ndlp->nlp_flag);
9867 
9868 		phba->fc_stat.elsRcvPLOGI++;
9869 		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
9870 		if (phba->sli_rev == LPFC_SLI_REV4 &&
9871 		    (phba->pport->fc_flag & FC_PT2PT)) {
9872 			vport->fc_prevDID = vport->fc_myDID;
9873 			/* Our DID needs to be updated before registering
9874 			 * the vfi. This is done in lpfc_rcv_plogi but
9875 			 * that is called after the reg_vfi.
9876 			 */
9877 			vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
9878 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9879 					 "3312 Remote port assigned DID x%x "
9880 					 "%x\n", vport->fc_myDID,
9881 					 vport->fc_prevDID);
9882 		}
9883 
9884 		lpfc_send_els_event(vport, ndlp, payload);
9885 
9886 		/* If Nport discovery is delayed, reject PLOGIs */
9887 		if (vport->fc_flag & FC_DISC_DELAYED) {
9888 			rjt_err = LSRJT_UNABLE_TPC;
9889 			rjt_exp = LSEXP_NOTHING_MORE;
9890 			break;
9891 		}
9892 
9893 		if (vport->port_state < LPFC_DISC_AUTH) {
9894 			if (!(phba->pport->fc_flag & FC_PT2PT) ||
9895 				(phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
9896 				rjt_err = LSRJT_UNABLE_TPC;
9897 				rjt_exp = LSEXP_NOTHING_MORE;
9898 				break;
9899 			}
9900 		}
9901 
9902 		spin_lock_irq(&ndlp->lock);
9903 		ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
9904 		spin_unlock_irq(&ndlp->lock);
9905 
9906 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
9907 					NLP_EVT_RCV_PLOGI);
9908 
9909 		break;
9910 	case ELS_CMD_FLOGI:
9911 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9912 			"RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
9913 			did, vport->port_state, ndlp->nlp_flag);
9914 
9915 		phba->fc_stat.elsRcvFLOGI++;
9916 
9917 		/* If the driver believes fabric discovery is done and is ready,
9918 		 * bounce the link.  There is some descrepancy.
9919 		 */
9920 		if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
9921 		    vport->fc_flag & FC_PT2PT &&
9922 		    vport->rcv_flogi_cnt >= 1) {
9923 			rjt_err = LSRJT_LOGICAL_BSY;
9924 			rjt_exp = LSEXP_NOTHING_MORE;
9925 			init_link++;
9926 			goto lsrjt;
9927 		}
9928 
9929 		lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
9930 		if (newnode)
9931 			lpfc_disc_state_machine(vport, ndlp, NULL,
9932 					NLP_EVT_DEVICE_RM);
9933 		break;
9934 	case ELS_CMD_LOGO:
9935 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9936 			"RCV LOGO:        did:x%x/ste:x%x flg:x%x",
9937 			did, vport->port_state, ndlp->nlp_flag);
9938 
9939 		phba->fc_stat.elsRcvLOGO++;
9940 		lpfc_send_els_event(vport, ndlp, payload);
9941 		if (vport->port_state < LPFC_DISC_AUTH) {
9942 			rjt_err = LSRJT_UNABLE_TPC;
9943 			rjt_exp = LSEXP_NOTHING_MORE;
9944 			break;
9945 		}
9946 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
9947 		if (newnode)
9948 			lpfc_disc_state_machine(vport, ndlp, NULL,
9949 					NLP_EVT_DEVICE_RM);
9950 		break;
9951 	case ELS_CMD_PRLO:
9952 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9953 			"RCV PRLO:        did:x%x/ste:x%x flg:x%x",
9954 			did, vport->port_state, ndlp->nlp_flag);
9955 
9956 		phba->fc_stat.elsRcvPRLO++;
9957 		lpfc_send_els_event(vport, ndlp, payload);
9958 		if (vport->port_state < LPFC_DISC_AUTH) {
9959 			rjt_err = LSRJT_UNABLE_TPC;
9960 			rjt_exp = LSEXP_NOTHING_MORE;
9961 			break;
9962 		}
9963 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
9964 		break;
9965 	case ELS_CMD_LCB:
9966 		phba->fc_stat.elsRcvLCB++;
9967 		lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
9968 		break;
9969 	case ELS_CMD_RDP:
9970 		phba->fc_stat.elsRcvRDP++;
9971 		lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
9972 		break;
9973 	case ELS_CMD_RSCN:
9974 		phba->fc_stat.elsRcvRSCN++;
9975 		lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
9976 		if (newnode)
9977 			lpfc_disc_state_machine(vport, ndlp, NULL,
9978 					NLP_EVT_DEVICE_RM);
9979 		break;
9980 	case ELS_CMD_ADISC:
9981 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9982 			"RCV ADISC:       did:x%x/ste:x%x flg:x%x",
9983 			did, vport->port_state, ndlp->nlp_flag);
9984 
9985 		lpfc_send_els_event(vport, ndlp, payload);
9986 		phba->fc_stat.elsRcvADISC++;
9987 		if (vport->port_state < LPFC_DISC_AUTH) {
9988 			rjt_err = LSRJT_UNABLE_TPC;
9989 			rjt_exp = LSEXP_NOTHING_MORE;
9990 			break;
9991 		}
9992 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
9993 					NLP_EVT_RCV_ADISC);
9994 		break;
9995 	case ELS_CMD_PDISC:
9996 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
9997 			"RCV PDISC:       did:x%x/ste:x%x flg:x%x",
9998 			did, vport->port_state, ndlp->nlp_flag);
9999 
10000 		phba->fc_stat.elsRcvPDISC++;
10001 		if (vport->port_state < LPFC_DISC_AUTH) {
10002 			rjt_err = LSRJT_UNABLE_TPC;
10003 			rjt_exp = LSEXP_NOTHING_MORE;
10004 			break;
10005 		}
10006 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
10007 					NLP_EVT_RCV_PDISC);
10008 		break;
10009 	case ELS_CMD_FARPR:
10010 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10011 			"RCV FARPR:       did:x%x/ste:x%x flg:x%x",
10012 			did, vport->port_state, ndlp->nlp_flag);
10013 
10014 		phba->fc_stat.elsRcvFARPR++;
10015 		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
10016 		break;
10017 	case ELS_CMD_FARP:
10018 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10019 			"RCV FARP:        did:x%x/ste:x%x flg:x%x",
10020 			did, vport->port_state, ndlp->nlp_flag);
10021 
10022 		phba->fc_stat.elsRcvFARP++;
10023 		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
10024 		break;
10025 	case ELS_CMD_FAN:
10026 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10027 			"RCV FAN:         did:x%x/ste:x%x flg:x%x",
10028 			did, vport->port_state, ndlp->nlp_flag);
10029 
10030 		phba->fc_stat.elsRcvFAN++;
10031 		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
10032 		break;
10033 	case ELS_CMD_PRLI:
10034 	case ELS_CMD_NVMEPRLI:
10035 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10036 			"RCV PRLI:        did:x%x/ste:x%x flg:x%x",
10037 			did, vport->port_state, ndlp->nlp_flag);
10038 
10039 		phba->fc_stat.elsRcvPRLI++;
10040 		if ((vport->port_state < LPFC_DISC_AUTH) &&
10041 		    (vport->fc_flag & FC_FABRIC)) {
10042 			rjt_err = LSRJT_UNABLE_TPC;
10043 			rjt_exp = LSEXP_NOTHING_MORE;
10044 			break;
10045 		}
10046 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
10047 		break;
10048 	case ELS_CMD_LIRR:
10049 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10050 			"RCV LIRR:        did:x%x/ste:x%x flg:x%x",
10051 			did, vport->port_state, ndlp->nlp_flag);
10052 
10053 		phba->fc_stat.elsRcvLIRR++;
10054 		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
10055 		if (newnode)
10056 			lpfc_disc_state_machine(vport, ndlp, NULL,
10057 					NLP_EVT_DEVICE_RM);
10058 		break;
10059 	case ELS_CMD_RLS:
10060 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10061 			"RCV RLS:         did:x%x/ste:x%x flg:x%x",
10062 			did, vport->port_state, ndlp->nlp_flag);
10063 
10064 		phba->fc_stat.elsRcvRLS++;
10065 		lpfc_els_rcv_rls(vport, elsiocb, ndlp);
10066 		if (newnode)
10067 			lpfc_disc_state_machine(vport, ndlp, NULL,
10068 					NLP_EVT_DEVICE_RM);
10069 		break;
10070 	case ELS_CMD_RPL:
10071 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10072 			"RCV RPL:         did:x%x/ste:x%x flg:x%x",
10073 			did, vport->port_state, ndlp->nlp_flag);
10074 
10075 		phba->fc_stat.elsRcvRPL++;
10076 		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
10077 		if (newnode)
10078 			lpfc_disc_state_machine(vport, ndlp, NULL,
10079 					NLP_EVT_DEVICE_RM);
10080 		break;
10081 	case ELS_CMD_RNID:
10082 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10083 			"RCV RNID:        did:x%x/ste:x%x flg:x%x",
10084 			did, vport->port_state, ndlp->nlp_flag);
10085 
10086 		phba->fc_stat.elsRcvRNID++;
10087 		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
10088 		if (newnode)
10089 			lpfc_disc_state_machine(vport, ndlp, NULL,
10090 					NLP_EVT_DEVICE_RM);
10091 		break;
10092 	case ELS_CMD_RTV:
10093 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10094 			"RCV RTV:        did:x%x/ste:x%x flg:x%x",
10095 			did, vport->port_state, ndlp->nlp_flag);
10096 		phba->fc_stat.elsRcvRTV++;
10097 		lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
10098 		if (newnode)
10099 			lpfc_disc_state_machine(vport, ndlp, NULL,
10100 					NLP_EVT_DEVICE_RM);
10101 		break;
10102 	case ELS_CMD_RRQ:
10103 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10104 			"RCV RRQ:         did:x%x/ste:x%x flg:x%x",
10105 			did, vport->port_state, ndlp->nlp_flag);
10106 
10107 		phba->fc_stat.elsRcvRRQ++;
10108 		lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
10109 		if (newnode)
10110 			lpfc_disc_state_machine(vport, ndlp, NULL,
10111 					NLP_EVT_DEVICE_RM);
10112 		break;
10113 	case ELS_CMD_ECHO:
10114 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10115 			"RCV ECHO:        did:x%x/ste:x%x flg:x%x",
10116 			did, vport->port_state, ndlp->nlp_flag);
10117 
10118 		phba->fc_stat.elsRcvECHO++;
10119 		lpfc_els_rcv_echo(vport, elsiocb, ndlp);
10120 		if (newnode)
10121 			lpfc_disc_state_machine(vport, ndlp, NULL,
10122 					NLP_EVT_DEVICE_RM);
10123 		break;
10124 	case ELS_CMD_REC:
10125 		/* receive this due to exchange closed */
10126 		rjt_err = LSRJT_UNABLE_TPC;
10127 		rjt_exp = LSEXP_INVALID_OX_RX;
10128 		break;
10129 	case ELS_CMD_FPIN:
10130 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10131 				      "RCV FPIN:       did:x%x/ste:x%x flg:x%x",
10132 				      did, vport->port_state, ndlp->nlp_flag);
10133 
10134 		lpfc_els_rcv_fpin(vport, (struct fc_els_fpin *)payload,
10135 				  payload_len);
10136 
10137 		/* There are no replies, so no rjt codes */
10138 		break;
10139 	case ELS_CMD_EDC:
10140 		lpfc_els_rcv_edc(vport, elsiocb, ndlp);
10141 		break;
10142 	case ELS_CMD_RDF:
10143 		phba->fc_stat.elsRcvRDF++;
10144 		/* Accept RDF only from fabric controller */
10145 		if (did != Fabric_Cntl_DID) {
10146 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
10147 					 "1115 Received RDF from invalid DID "
10148 					 "x%x\n", did);
10149 			rjt_err = LSRJT_PROTOCOL_ERR;
10150 			rjt_exp = LSEXP_NOTHING_MORE;
10151 			goto lsrjt;
10152 		}
10153 
10154 		lpfc_els_rcv_rdf(vport, elsiocb, ndlp);
10155 		break;
10156 	default:
10157 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10158 			"RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
10159 			cmd, did, vport->port_state);
10160 
10161 		/* Unsupported ELS command, reject */
10162 		rjt_err = LSRJT_CMD_UNSUPPORTED;
10163 		rjt_exp = LSEXP_NOTHING_MORE;
10164 
10165 		/* Unknown ELS command <elsCmd> received from NPORT <did> */
10166 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10167 				 "0115 Unknown ELS command x%x "
10168 				 "received from NPORT x%x\n", cmd, did);
10169 		if (newnode)
10170 			lpfc_disc_state_machine(vport, ndlp, NULL,
10171 					NLP_EVT_DEVICE_RM);
10172 		break;
10173 	}
10174 
10175 lsrjt:
10176 	/* check if need to LS_RJT received ELS cmd */
10177 	if (rjt_err) {
10178 		memset(&stat, 0, sizeof(stat));
10179 		stat.un.b.lsRjtRsnCode = rjt_err;
10180 		stat.un.b.lsRjtRsnCodeExp = rjt_exp;
10181 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
10182 				    NULL);
10183 		/* Remove the reference from above for new nodes. */
10184 		if (newnode)
10185 			lpfc_disc_state_machine(vport, ndlp, NULL,
10186 					NLP_EVT_DEVICE_RM);
10187 	}
10188 
10189 	/* Release the reference on this elsiocb, not the ndlp. */
10190 	lpfc_nlp_put(elsiocb->context1);
10191 	elsiocb->context1 = NULL;
10192 
10193 	/* Special case.  Driver received an unsolicited command that
10194 	 * unsupportable given the driver's current state.  Reset the
10195 	 * link and start over.
10196 	 */
10197 	if (init_link) {
10198 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10199 		if (!mbox)
10200 			return;
10201 		lpfc_linkdown(phba);
10202 		lpfc_init_link(phba, mbox,
10203 			       phba->cfg_topology,
10204 			       phba->cfg_link_speed);
10205 		mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
10206 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10207 		mbox->vport = vport;
10208 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
10209 		    MBX_NOT_FINISHED)
10210 			mempool_free(mbox, phba->mbox_mem_pool);
10211 	}
10212 
10213 	return;
10214 
10215 dropit:
10216 	if (vport && !(vport->load_flag & FC_UNLOADING))
10217 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10218 			"0111 Dropping received ELS cmd "
10219 			"Data: x%x x%x x%x\n",
10220 			icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
10221 	phba->fc_stat.elsRcvDrop++;
10222 }
10223 
10224 /**
10225  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
10226  * @phba: pointer to lpfc hba data structure.
10227  * @pring: pointer to a SLI ring.
10228  * @elsiocb: pointer to lpfc els iocb data structure.
10229  *
10230  * This routine is used to process an unsolicited event received from a SLI
10231  * (Service Level Interface) ring. The actual processing of the data buffer
10232  * associated with the unsolicited event is done by invoking the routine
10233  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
10234  * SLI ring on which the unsolicited event was received.
10235  **/
10236 void
10237 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10238 		     struct lpfc_iocbq *elsiocb)
10239 {
10240 	struct lpfc_vport *vport = phba->pport;
10241 	IOCB_t *icmd = &elsiocb->iocb;
10242 	dma_addr_t paddr;
10243 	struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
10244 	struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
10245 
10246 	elsiocb->context1 = NULL;
10247 	elsiocb->context2 = NULL;
10248 	elsiocb->context3 = NULL;
10249 
10250 	if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
10251 		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
10252 	} else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
10253 		   (icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
10254 		   IOERR_RCV_BUFFER_WAITING) {
10255 		phba->fc_stat.NoRcvBuf++;
10256 		/* Not enough posted buffers; Try posting more buffers */
10257 		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
10258 			lpfc_post_buffer(phba, pring, 0);
10259 		return;
10260 	}
10261 
10262 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
10263 	    (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
10264 	     icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
10265 		if (icmd->unsli3.rcvsli3.vpi == 0xffff)
10266 			vport = phba->pport;
10267 		else
10268 			vport = lpfc_find_vport_by_vpid(phba,
10269 						icmd->unsli3.rcvsli3.vpi);
10270 	}
10271 
10272 	/* If there are no BDEs associated
10273 	 * with this IOCB, there is nothing to do.
10274 	 */
10275 	if (icmd->ulpBdeCount == 0)
10276 		return;
10277 
10278 	/* type of ELS cmd is first 32bit word
10279 	 * in packet
10280 	 */
10281 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
10282 		elsiocb->context2 = bdeBuf1;
10283 	} else {
10284 		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
10285 				 icmd->un.cont64[0].addrLow);
10286 		elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
10287 							     paddr);
10288 	}
10289 
10290 	lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
10291 	/*
10292 	 * The different unsolicited event handlers would tell us
10293 	 * if they are done with "mp" by setting context2 to NULL.
10294 	 */
10295 	if (elsiocb->context2) {
10296 		lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
10297 		elsiocb->context2 = NULL;
10298 	}
10299 
10300 	/* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
10301 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
10302 	    icmd->ulpBdeCount == 2) {
10303 		elsiocb->context2 = bdeBuf2;
10304 		lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
10305 		/* free mp if we are done with it */
10306 		if (elsiocb->context2) {
10307 			lpfc_in_buf_free(phba, elsiocb->context2);
10308 			elsiocb->context2 = NULL;
10309 		}
10310 	}
10311 }
10312 
10313 static void
10314 lpfc_start_fdmi(struct lpfc_vport *vport)
10315 {
10316 	struct lpfc_nodelist *ndlp;
10317 
10318 	/* If this is the first time, allocate an ndlp and initialize
10319 	 * it. Otherwise, make sure the node is enabled and then do the
10320 	 * login.
10321 	 */
10322 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
10323 	if (!ndlp) {
10324 		ndlp = lpfc_nlp_init(vport, FDMI_DID);
10325 		if (ndlp) {
10326 			ndlp->nlp_type |= NLP_FABRIC;
10327 		} else {
10328 			return;
10329 		}
10330 	}
10331 
10332 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
10333 	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
10334 }
10335 
10336 /**
10337  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
10338  * @phba: pointer to lpfc hba data structure.
10339  * @vport: pointer to a virtual N_Port data structure.
10340  *
10341  * This routine issues a Port Login (PLOGI) to the Name Server with
10342  * State Change Request (SCR) for a @vport. This routine will create an
10343  * ndlp for the Name Server associated to the @vport if such node does
10344  * not already exist. The PLOGI to Name Server is issued by invoking the
10345  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
10346  * (FDMI) is configured to the @vport, a FDMI node will be created and
10347  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
10348  **/
10349 void
10350 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
10351 {
10352 	struct lpfc_nodelist *ndlp;
10353 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10354 
10355 	/*
10356 	 * If lpfc_delay_discovery parameter is set and the clean address
10357 	 * bit is cleared and fc fabric parameters chenged, delay FC NPort
10358 	 * discovery.
10359 	 */
10360 	spin_lock_irq(shost->host_lock);
10361 	if (vport->fc_flag & FC_DISC_DELAYED) {
10362 		spin_unlock_irq(shost->host_lock);
10363 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10364 				 "3334 Delay fc port discovery for %d secs\n",
10365 				 phba->fc_ratov);
10366 		mod_timer(&vport->delayed_disc_tmo,
10367 			jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
10368 		return;
10369 	}
10370 	spin_unlock_irq(shost->host_lock);
10371 
10372 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
10373 	if (!ndlp) {
10374 		ndlp = lpfc_nlp_init(vport, NameServer_DID);
10375 		if (!ndlp) {
10376 			if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
10377 				lpfc_disc_start(vport);
10378 				return;
10379 			}
10380 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10381 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10382 					 "0251 NameServer login: no memory\n");
10383 			return;
10384 		}
10385 	}
10386 
10387 	ndlp->nlp_type |= NLP_FABRIC;
10388 
10389 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
10390 
10391 	if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
10392 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10393 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10394 				 "0252 Cannot issue NameServer login\n");
10395 		return;
10396 	}
10397 
10398 	if ((phba->cfg_enable_SmartSAN ||
10399 	     (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) &&
10400 	     (vport->load_flag & FC_ALLOW_FDMI))
10401 		lpfc_start_fdmi(vport);
10402 }
10403 
10404 /**
10405  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
10406  * @phba: pointer to lpfc hba data structure.
10407  * @pmb: pointer to the driver internal queue element for mailbox command.
10408  *
10409  * This routine is the completion callback function to register new vport
10410  * mailbox command. If the new vport mailbox command completes successfully,
10411  * the fabric registration login shall be performed on physical port (the
10412  * new vport created is actually a physical port, with VPI 0) or the port
10413  * login to Name Server for State Change Request (SCR) will be performed
10414  * on virtual port (real virtual port, with VPI greater than 0).
10415  **/
10416 static void
10417 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
10418 {
10419 	struct lpfc_vport *vport = pmb->vport;
10420 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10421 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
10422 	MAILBOX_t *mb = &pmb->u.mb;
10423 	int rc;
10424 
10425 	spin_lock_irq(shost->host_lock);
10426 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
10427 	spin_unlock_irq(shost->host_lock);
10428 
10429 	if (mb->mbxStatus) {
10430 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10431 				"0915 Register VPI failed : Status: x%x"
10432 				" upd bit: x%x \n", mb->mbxStatus,
10433 				 mb->un.varRegVpi.upd);
10434 		if (phba->sli_rev == LPFC_SLI_REV4 &&
10435 			mb->un.varRegVpi.upd)
10436 			goto mbox_err_exit ;
10437 
10438 		switch (mb->mbxStatus) {
10439 		case 0x11:	/* unsupported feature */
10440 		case 0x9603:	/* max_vpi exceeded */
10441 		case 0x9602:	/* Link event since CLEAR_LA */
10442 			/* giving up on vport registration */
10443 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10444 			spin_lock_irq(shost->host_lock);
10445 			vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
10446 			spin_unlock_irq(shost->host_lock);
10447 			lpfc_can_disctmo(vport);
10448 			break;
10449 		/* If reg_vpi fail with invalid VPI status, re-init VPI */
10450 		case 0x20:
10451 			spin_lock_irq(shost->host_lock);
10452 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
10453 			spin_unlock_irq(shost->host_lock);
10454 			lpfc_init_vpi(phba, pmb, vport->vpi);
10455 			pmb->vport = vport;
10456 			pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
10457 			rc = lpfc_sli_issue_mbox(phba, pmb,
10458 				MBX_NOWAIT);
10459 			if (rc == MBX_NOT_FINISHED) {
10460 				lpfc_printf_vlog(vport, KERN_ERR,
10461 						 LOG_TRACE_EVENT,
10462 					"2732 Failed to issue INIT_VPI"
10463 					" mailbox command\n");
10464 			} else {
10465 				lpfc_nlp_put(ndlp);
10466 				return;
10467 			}
10468 			fallthrough;
10469 		default:
10470 			/* Try to recover from this error */
10471 			if (phba->sli_rev == LPFC_SLI_REV4)
10472 				lpfc_sli4_unreg_all_rpis(vport);
10473 			lpfc_mbx_unreg_vpi(vport);
10474 			spin_lock_irq(shost->host_lock);
10475 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
10476 			spin_unlock_irq(shost->host_lock);
10477 			if (mb->mbxStatus == MBX_NOT_FINISHED)
10478 				break;
10479 			if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
10480 			    !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG)) {
10481 				if (phba->sli_rev == LPFC_SLI_REV4)
10482 					lpfc_issue_init_vfi(vport);
10483 				else
10484 					lpfc_initial_flogi(vport);
10485 			} else {
10486 				lpfc_initial_fdisc(vport);
10487 			}
10488 			break;
10489 		}
10490 	} else {
10491 		spin_lock_irq(shost->host_lock);
10492 		vport->vpi_state |= LPFC_VPI_REGISTERED;
10493 		spin_unlock_irq(shost->host_lock);
10494 		if (vport == phba->pport) {
10495 			if (phba->sli_rev < LPFC_SLI_REV4)
10496 				lpfc_issue_fabric_reglogin(vport);
10497 			else {
10498 				/*
10499 				 * If the physical port is instantiated using
10500 				 * FDISC, do not start vport discovery.
10501 				 */
10502 				if (vport->port_state != LPFC_FDISC)
10503 					lpfc_start_fdiscs(phba);
10504 				lpfc_do_scr_ns_plogi(phba, vport);
10505 			}
10506 		} else {
10507 			lpfc_do_scr_ns_plogi(phba, vport);
10508 		}
10509 	}
10510 mbox_err_exit:
10511 	/* Now, we decrement the ndlp reference count held for this
10512 	 * callback function
10513 	 */
10514 	lpfc_nlp_put(ndlp);
10515 
10516 	mempool_free(pmb, phba->mbox_mem_pool);
10517 	return;
10518 }
10519 
10520 /**
10521  * lpfc_register_new_vport - Register a new vport with a HBA
10522  * @phba: pointer to lpfc hba data structure.
10523  * @vport: pointer to a host virtual N_Port data structure.
10524  * @ndlp: pointer to a node-list data structure.
10525  *
10526  * This routine registers the @vport as a new virtual port with a HBA.
10527  * It is done through a registering vpi mailbox command.
10528  **/
10529 void
10530 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
10531 			struct lpfc_nodelist *ndlp)
10532 {
10533 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10534 	LPFC_MBOXQ_t *mbox;
10535 
10536 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10537 	if (mbox) {
10538 		lpfc_reg_vpi(vport, mbox);
10539 		mbox->vport = vport;
10540 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
10541 		if (!mbox->ctx_ndlp) {
10542 			mempool_free(mbox, phba->mbox_mem_pool);
10543 			goto mbox_err_exit;
10544 		}
10545 
10546 		mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
10547 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
10548 		    == MBX_NOT_FINISHED) {
10549 			/* mailbox command not success, decrement ndlp
10550 			 * reference count for this command
10551 			 */
10552 			lpfc_nlp_put(ndlp);
10553 			mempool_free(mbox, phba->mbox_mem_pool);
10554 
10555 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10556 				"0253 Register VPI: Can't send mbox\n");
10557 			goto mbox_err_exit;
10558 		}
10559 	} else {
10560 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10561 				 "0254 Register VPI: no memory\n");
10562 		goto mbox_err_exit;
10563 	}
10564 	return;
10565 
10566 mbox_err_exit:
10567 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10568 	spin_lock_irq(shost->host_lock);
10569 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
10570 	spin_unlock_irq(shost->host_lock);
10571 	return;
10572 }
10573 
10574 /**
10575  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
10576  * @phba: pointer to lpfc hba data structure.
10577  *
10578  * This routine cancels the retry delay timers to all the vports.
10579  **/
10580 void
10581 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
10582 {
10583 	struct lpfc_vport **vports;
10584 	struct lpfc_nodelist *ndlp;
10585 	uint32_t link_state;
10586 	int i;
10587 
10588 	/* Treat this failure as linkdown for all vports */
10589 	link_state = phba->link_state;
10590 	lpfc_linkdown(phba);
10591 	phba->link_state = link_state;
10592 
10593 	vports = lpfc_create_vport_work_array(phba);
10594 
10595 	if (vports) {
10596 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10597 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
10598 			if (ndlp)
10599 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
10600 			lpfc_els_flush_cmd(vports[i]);
10601 		}
10602 		lpfc_destroy_vport_work_array(phba, vports);
10603 	}
10604 }
10605 
10606 /**
10607  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
10608  * @phba: pointer to lpfc hba data structure.
10609  *
10610  * This routine abort all pending discovery commands and
10611  * start a timer to retry FLOGI for the physical port
10612  * discovery.
10613  **/
10614 void
10615 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
10616 {
10617 	struct lpfc_nodelist *ndlp;
10618 
10619 	/* Cancel the all vports retry delay retry timers */
10620 	lpfc_cancel_all_vport_retry_delay_timer(phba);
10621 
10622 	/* If fabric require FLOGI, then re-instantiate physical login */
10623 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
10624 	if (!ndlp)
10625 		return;
10626 
10627 	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
10628 	spin_lock_irq(&ndlp->lock);
10629 	ndlp->nlp_flag |= NLP_DELAY_TMO;
10630 	spin_unlock_irq(&ndlp->lock);
10631 	ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
10632 	phba->pport->port_state = LPFC_FLOGI;
10633 	return;
10634 }
10635 
10636 /**
10637  * lpfc_fabric_login_reqd - Check if FLOGI required.
10638  * @phba: pointer to lpfc hba data structure.
10639  * @cmdiocb: pointer to FDISC command iocb.
10640  * @rspiocb: pointer to FDISC response iocb.
10641  *
10642  * This routine checks if a FLOGI is reguired for FDISC
10643  * to succeed.
10644  **/
10645 static int
10646 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
10647 		struct lpfc_iocbq *cmdiocb,
10648 		struct lpfc_iocbq *rspiocb)
10649 {
10650 
10651 	if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
10652 		(rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
10653 		return 0;
10654 	else
10655 		return 1;
10656 }
10657 
10658 /**
10659  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
10660  * @phba: pointer to lpfc hba data structure.
10661  * @cmdiocb: pointer to lpfc command iocb data structure.
10662  * @rspiocb: pointer to lpfc response iocb data structure.
10663  *
10664  * This routine is the completion callback function to a Fabric Discover
10665  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
10666  * single threaded, each FDISC completion callback function will reset
10667  * the discovery timer for all vports such that the timers will not get
10668  * unnecessary timeout. The function checks the FDISC IOCB status. If error
10669  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
10670  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
10671  * assigned to the vport has been changed with the completion of the FDISC
10672  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
10673  * are unregistered from the HBA, and then the lpfc_register_new_vport()
10674  * routine is invoked to register new vport with the HBA. Otherwise, the
10675  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
10676  * Server for State Change Request (SCR).
10677  **/
10678 static void
10679 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10680 		    struct lpfc_iocbq *rspiocb)
10681 {
10682 	struct lpfc_vport *vport = cmdiocb->vport;
10683 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
10684 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
10685 	struct lpfc_nodelist *np;
10686 	struct lpfc_nodelist *next_np;
10687 	IOCB_t *irsp = &rspiocb->iocb;
10688 	struct lpfc_iocbq *piocb;
10689 	struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
10690 	struct serv_parm *sp;
10691 	uint8_t fabric_param_changed;
10692 
10693 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10694 			 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
10695 			 irsp->ulpStatus, irsp->un.ulpWord[4],
10696 			 vport->fc_prevDID);
10697 	/* Since all FDISCs are being single threaded, we
10698 	 * must reset the discovery timer for ALL vports
10699 	 * waiting to send FDISC when one completes.
10700 	 */
10701 	list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
10702 		lpfc_set_disctmo(piocb->vport);
10703 	}
10704 
10705 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
10706 		"FDISC cmpl:      status:x%x/x%x prevdid:x%x",
10707 		irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
10708 
10709 	if (irsp->ulpStatus) {
10710 
10711 		if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
10712 			lpfc_retry_pport_discovery(phba);
10713 			goto out;
10714 		}
10715 
10716 		/* Check for retry */
10717 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
10718 			goto out;
10719 		/* FDISC failed */
10720 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10721 				 "0126 FDISC failed. (x%x/x%x)\n",
10722 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
10723 		goto fdisc_failed;
10724 	}
10725 	spin_lock_irq(shost->host_lock);
10726 	vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
10727 	vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
10728 	vport->fc_flag |= FC_FABRIC;
10729 	if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
10730 		vport->fc_flag |=  FC_PUBLIC_LOOP;
10731 	spin_unlock_irq(shost->host_lock);
10732 
10733 	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
10734 	lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
10735 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
10736 	if (!prsp)
10737 		goto out;
10738 	sp = prsp->virt + sizeof(uint32_t);
10739 	fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
10740 	memcpy(&vport->fabric_portname, &sp->portName,
10741 		sizeof(struct lpfc_name));
10742 	memcpy(&vport->fabric_nodename, &sp->nodeName,
10743 		sizeof(struct lpfc_name));
10744 	if (fabric_param_changed &&
10745 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
10746 		/* If our NportID changed, we need to ensure all
10747 		 * remaining NPORTs get unreg_login'ed so we can
10748 		 * issue unreg_vpi.
10749 		 */
10750 		list_for_each_entry_safe(np, next_np,
10751 			&vport->fc_nodes, nlp_listp) {
10752 			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
10753 			    !(np->nlp_flag & NLP_NPR_ADISC))
10754 				continue;
10755 			spin_lock_irq(&ndlp->lock);
10756 			np->nlp_flag &= ~NLP_NPR_ADISC;
10757 			spin_unlock_irq(&ndlp->lock);
10758 			lpfc_unreg_rpi(vport, np);
10759 		}
10760 		lpfc_cleanup_pending_mbox(vport);
10761 
10762 		if (phba->sli_rev == LPFC_SLI_REV4)
10763 			lpfc_sli4_unreg_all_rpis(vport);
10764 
10765 		lpfc_mbx_unreg_vpi(vport);
10766 		spin_lock_irq(shost->host_lock);
10767 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
10768 		if (phba->sli_rev == LPFC_SLI_REV4)
10769 			vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
10770 		else
10771 			vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
10772 		spin_unlock_irq(shost->host_lock);
10773 	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
10774 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
10775 		/*
10776 		 * Driver needs to re-reg VPI in order for f/w
10777 		 * to update the MAC address.
10778 		 */
10779 		lpfc_register_new_vport(phba, vport, ndlp);
10780 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
10781 		goto out;
10782 	}
10783 
10784 	if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
10785 		lpfc_issue_init_vpi(vport);
10786 	else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
10787 		lpfc_register_new_vport(phba, vport, ndlp);
10788 	else
10789 		lpfc_do_scr_ns_plogi(phba, vport);
10790 
10791 	/* The FDISC completed successfully. Move the fabric ndlp to
10792 	 * UNMAPPED state and register with the transport.
10793 	 */
10794 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
10795 	goto out;
10796 
10797 fdisc_failed:
10798 	if (vport->fc_vport &&
10799 	    (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
10800 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10801 	/* Cancel discovery timer */
10802 	lpfc_can_disctmo(vport);
10803 out:
10804 	lpfc_els_free_iocb(phba, cmdiocb);
10805 	lpfc_nlp_put(ndlp);
10806 }
10807 
10808 /**
10809  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
10810  * @vport: pointer to a virtual N_Port data structure.
10811  * @ndlp: pointer to a node-list data structure.
10812  * @retry: number of retries to the command IOCB.
10813  *
10814  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
10815  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
10816  * routine to issue the IOCB, which makes sure only one outstanding fabric
10817  * IOCB will be sent off HBA at any given time.
10818  *
10819  * Note that the ndlp reference count will be incremented by 1 for holding the
10820  * ndlp and the reference to ndlp will be stored into the context1 field of
10821  * the IOCB for the completion callback function to the FDISC ELS command.
10822  *
10823  * Return code
10824  *   0 - Successfully issued fdisc iocb command
10825  *   1 - Failed to issue fdisc iocb command
10826  **/
10827 static int
10828 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
10829 		     uint8_t retry)
10830 {
10831 	struct lpfc_hba *phba = vport->phba;
10832 	IOCB_t *icmd;
10833 	struct lpfc_iocbq *elsiocb;
10834 	struct serv_parm *sp;
10835 	uint8_t *pcmd;
10836 	uint16_t cmdsize;
10837 	int did = ndlp->nlp_DID;
10838 	int rc;
10839 
10840 	vport->port_state = LPFC_FDISC;
10841 	vport->fc_myDID = 0;
10842 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
10843 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
10844 				     ELS_CMD_FDISC);
10845 	if (!elsiocb) {
10846 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10847 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10848 				 "0255 Issue FDISC: no IOCB\n");
10849 		return 1;
10850 	}
10851 
10852 	icmd = &elsiocb->iocb;
10853 	icmd->un.elsreq64.myID = 0;
10854 	icmd->un.elsreq64.fl = 1;
10855 
10856 	/*
10857 	 * SLI3 ports require a different context type value than SLI4.
10858 	 * Catch SLI3 ports here and override the prep.
10859 	 */
10860 	if (phba->sli_rev == LPFC_SLI_REV3) {
10861 		icmd->ulpCt_h = 1;
10862 		icmd->ulpCt_l = 0;
10863 	}
10864 
10865 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
10866 	*((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
10867 	pcmd += sizeof(uint32_t); /* CSP Word 1 */
10868 	memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
10869 	sp = (struct serv_parm *) pcmd;
10870 	/* Setup CSPs accordingly for Fabric */
10871 	sp->cmn.e_d_tov = 0;
10872 	sp->cmn.w2.r_a_tov = 0;
10873 	sp->cmn.virtual_fabric_support = 0;
10874 	sp->cls1.classValid = 0;
10875 	sp->cls2.seqDelivery = 1;
10876 	sp->cls3.seqDelivery = 1;
10877 
10878 	pcmd += sizeof(uint32_t); /* CSP Word 2 */
10879 	pcmd += sizeof(uint32_t); /* CSP Word 3 */
10880 	pcmd += sizeof(uint32_t); /* CSP Word 4 */
10881 	pcmd += sizeof(uint32_t); /* Port Name */
10882 	memcpy(pcmd, &vport->fc_portname, 8);
10883 	pcmd += sizeof(uint32_t); /* Node Name */
10884 	pcmd += sizeof(uint32_t); /* Node Name */
10885 	memcpy(pcmd, &vport->fc_nodename, 8);
10886 	sp->cmn.valid_vendor_ver_level = 0;
10887 	memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
10888 	lpfc_set_disctmo(vport);
10889 
10890 	phba->fc_stat.elsXmitFDISC++;
10891 	elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
10892 
10893 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
10894 		"Issue FDISC:     did:x%x",
10895 		did, 0, 0);
10896 
10897 	elsiocb->context1 = lpfc_nlp_get(ndlp);
10898 	if (!elsiocb->context1) {
10899 		lpfc_els_free_iocb(phba, elsiocb);
10900 		goto err_out;
10901 	}
10902 
10903 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
10904 	if (rc == IOCB_ERROR) {
10905 		lpfc_els_free_iocb(phba, elsiocb);
10906 		lpfc_nlp_put(ndlp);
10907 		goto err_out;
10908 	}
10909 
10910 	lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
10911 	return 0;
10912 
10913  err_out:
10914 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
10915 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10916 			 "0256 Issue FDISC: Cannot send IOCB\n");
10917 	return 1;
10918 }
10919 
10920 /**
10921  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
10922  * @phba: pointer to lpfc hba data structure.
10923  * @cmdiocb: pointer to lpfc command iocb data structure.
10924  * @rspiocb: pointer to lpfc response iocb data structure.
10925  *
10926  * This routine is the completion callback function to the issuing of a LOGO
10927  * ELS command off a vport. It frees the command IOCB and then decrement the
10928  * reference count held on ndlp for this completion function, indicating that
10929  * the reference to the ndlp is no long needed. Note that the
10930  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
10931  * callback function and an additional explicit ndlp reference decrementation
10932  * will trigger the actual release of the ndlp.
10933  **/
10934 static void
10935 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10936 			struct lpfc_iocbq *rspiocb)
10937 {
10938 	struct lpfc_vport *vport = cmdiocb->vport;
10939 	IOCB_t *irsp;
10940 	struct lpfc_nodelist *ndlp;
10941 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
10942 
10943 	ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
10944 	irsp = &rspiocb->iocb;
10945 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
10946 		"LOGO npiv cmpl:  status:x%x/x%x did:x%x",
10947 		irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
10948 
10949 	/* NPIV LOGO completes to NPort <nlp_DID> */
10950 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10951 			 "2928 NPIV LOGO completes to NPort x%x "
10952 			 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
10953 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
10954 			 irsp->ulpTimeout, vport->num_disc_nodes,
10955 			 kref_read(&ndlp->kref), ndlp->nlp_flag,
10956 			 ndlp->fc4_xpt_flags);
10957 
10958 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
10959 		spin_lock_irq(shost->host_lock);
10960 		vport->fc_flag &= ~FC_NDISC_ACTIVE;
10961 		vport->fc_flag &= ~FC_FABRIC;
10962 		spin_unlock_irq(shost->host_lock);
10963 		lpfc_can_disctmo(vport);
10964 	}
10965 
10966 	/* Safe to release resources now. */
10967 	lpfc_els_free_iocb(phba, cmdiocb);
10968 	lpfc_nlp_put(ndlp);
10969 	vport->unreg_vpi_cmpl = VPORT_ERROR;
10970 }
10971 
10972 /**
10973  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
10974  * @vport: pointer to a virtual N_Port data structure.
10975  * @ndlp: pointer to a node-list data structure.
10976  *
10977  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
10978  *
10979  * Note that the ndlp reference count will be incremented by 1 for holding the
10980  * ndlp and the reference to ndlp will be stored into the context1 field of
10981  * the IOCB for the completion callback function to the LOGO ELS command.
10982  *
10983  * Return codes
10984  *   0 - Successfully issued logo off the @vport
10985  *   1 - Failed to issue logo off the @vport
10986  **/
10987 int
10988 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
10989 {
10990 	int rc = 0;
10991 	struct lpfc_hba  *phba = vport->phba;
10992 	struct lpfc_iocbq *elsiocb;
10993 	uint8_t *pcmd;
10994 	uint16_t cmdsize;
10995 
10996 	cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
10997 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
10998 				     ELS_CMD_LOGO);
10999 	if (!elsiocb)
11000 		return 1;
11001 
11002 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
11003 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
11004 	pcmd += sizeof(uint32_t);
11005 
11006 	/* Fill in LOGO payload */
11007 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
11008 	pcmd += sizeof(uint32_t);
11009 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
11010 
11011 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11012 		"Issue LOGO npiv  did:x%x flg:x%x",
11013 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
11014 
11015 	elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
11016 	spin_lock_irq(&ndlp->lock);
11017 	ndlp->nlp_flag |= NLP_LOGO_SND;
11018 	spin_unlock_irq(&ndlp->lock);
11019 	elsiocb->context1 = lpfc_nlp_get(ndlp);
11020 	if (!elsiocb->context1) {
11021 		lpfc_els_free_iocb(phba, elsiocb);
11022 		goto err;
11023 	}
11024 
11025 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
11026 	if (rc == IOCB_ERROR) {
11027 		lpfc_els_free_iocb(phba, elsiocb);
11028 		lpfc_nlp_put(ndlp);
11029 		goto err;
11030 	}
11031 	return 0;
11032 
11033 err:
11034 	spin_lock_irq(&ndlp->lock);
11035 	ndlp->nlp_flag &= ~NLP_LOGO_SND;
11036 	spin_unlock_irq(&ndlp->lock);
11037 	return 1;
11038 }
11039 
11040 /**
11041  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
11042  * @t: timer context used to obtain the lpfc hba.
11043  *
11044  * This routine is invoked by the fabric iocb block timer after
11045  * timeout. It posts the fabric iocb block timeout event by setting the
11046  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
11047  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
11048  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
11049  * posted event WORKER_FABRIC_BLOCK_TMO.
11050  **/
11051 void
11052 lpfc_fabric_block_timeout(struct timer_list *t)
11053 {
11054 	struct lpfc_hba  *phba = from_timer(phba, t, fabric_block_timer);
11055 	unsigned long iflags;
11056 	uint32_t tmo_posted;
11057 
11058 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11059 	tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
11060 	if (!tmo_posted)
11061 		phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
11062 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11063 
11064 	if (!tmo_posted)
11065 		lpfc_worker_wake_up(phba);
11066 	return;
11067 }
11068 
11069 /**
11070  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
11071  * @phba: pointer to lpfc hba data structure.
11072  *
11073  * This routine issues one fabric iocb from the driver internal list to
11074  * the HBA. It first checks whether it's ready to issue one fabric iocb to
11075  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
11076  * remove one pending fabric iocb from the driver internal list and invokes
11077  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
11078  **/
11079 static void
11080 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
11081 {
11082 	struct lpfc_iocbq *iocb;
11083 	unsigned long iflags;
11084 	int ret;
11085 	IOCB_t *cmd;
11086 
11087 repeat:
11088 	iocb = NULL;
11089 	spin_lock_irqsave(&phba->hbalock, iflags);
11090 	/* Post any pending iocb to the SLI layer */
11091 	if (atomic_read(&phba->fabric_iocb_count) == 0) {
11092 		list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
11093 				 list);
11094 		if (iocb)
11095 			/* Increment fabric iocb count to hold the position */
11096 			atomic_inc(&phba->fabric_iocb_count);
11097 	}
11098 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11099 	if (iocb) {
11100 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
11101 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
11102 		iocb->iocb_flag |= LPFC_IO_FABRIC;
11103 
11104 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11105 			"Fabric sched1:   ste:x%x",
11106 			iocb->vport->port_state, 0, 0);
11107 
11108 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11109 
11110 		if (ret == IOCB_ERROR) {
11111 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
11112 			iocb->fabric_iocb_cmpl = NULL;
11113 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
11114 			cmd = &iocb->iocb;
11115 			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
11116 			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
11117 			iocb->iocb_cmpl(phba, iocb, iocb);
11118 
11119 			atomic_dec(&phba->fabric_iocb_count);
11120 			goto repeat;
11121 		}
11122 	}
11123 }
11124 
11125 /**
11126  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
11127  * @phba: pointer to lpfc hba data structure.
11128  *
11129  * This routine unblocks the  issuing fabric iocb command. The function
11130  * will clear the fabric iocb block bit and then invoke the routine
11131  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
11132  * from the driver internal fabric iocb list.
11133  **/
11134 void
11135 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
11136 {
11137 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11138 
11139 	lpfc_resume_fabric_iocbs(phba);
11140 	return;
11141 }
11142 
11143 /**
11144  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
11145  * @phba: pointer to lpfc hba data structure.
11146  *
11147  * This routine blocks the issuing fabric iocb for a specified amount of
11148  * time (currently 100 ms). This is done by set the fabric iocb block bit
11149  * and set up a timeout timer for 100ms. When the block bit is set, no more
11150  * fabric iocb will be issued out of the HBA.
11151  **/
11152 static void
11153 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
11154 {
11155 	int blocked;
11156 
11157 	blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11158 	/* Start a timer to unblock fabric iocbs after 100ms */
11159 	if (!blocked)
11160 		mod_timer(&phba->fabric_block_timer,
11161 			  jiffies + msecs_to_jiffies(100));
11162 
11163 	return;
11164 }
11165 
11166 /**
11167  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
11168  * @phba: pointer to lpfc hba data structure.
11169  * @cmdiocb: pointer to lpfc command iocb data structure.
11170  * @rspiocb: pointer to lpfc response iocb data structure.
11171  *
11172  * This routine is the callback function that is put to the fabric iocb's
11173  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
11174  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
11175  * function first restores and invokes the original iocb's callback function
11176  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
11177  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
11178  **/
11179 static void
11180 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11181 	struct lpfc_iocbq *rspiocb)
11182 {
11183 	struct ls_rjt stat;
11184 
11185 	BUG_ON((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
11186 
11187 	switch (rspiocb->iocb.ulpStatus) {
11188 		case IOSTAT_NPORT_RJT:
11189 		case IOSTAT_FABRIC_RJT:
11190 			if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
11191 				lpfc_block_fabric_iocbs(phba);
11192 			}
11193 			break;
11194 
11195 		case IOSTAT_NPORT_BSY:
11196 		case IOSTAT_FABRIC_BSY:
11197 			lpfc_block_fabric_iocbs(phba);
11198 			break;
11199 
11200 		case IOSTAT_LS_RJT:
11201 			stat.un.lsRjtError =
11202 				be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
11203 			if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
11204 				(stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
11205 				lpfc_block_fabric_iocbs(phba);
11206 			break;
11207 	}
11208 
11209 	BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
11210 
11211 	cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
11212 	cmdiocb->fabric_iocb_cmpl = NULL;
11213 	cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
11214 	cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
11215 
11216 	atomic_dec(&phba->fabric_iocb_count);
11217 	if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
11218 		/* Post any pending iocbs to HBA */
11219 		lpfc_resume_fabric_iocbs(phba);
11220 	}
11221 }
11222 
11223 /**
11224  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
11225  * @phba: pointer to lpfc hba data structure.
11226  * @iocb: pointer to lpfc command iocb data structure.
11227  *
11228  * This routine is used as the top-level API for issuing a fabric iocb command
11229  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
11230  * function makes sure that only one fabric bound iocb will be outstanding at
11231  * any given time. As such, this function will first check to see whether there
11232  * is already an outstanding fabric iocb on the wire. If so, it will put the
11233  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
11234  * issued later. Otherwise, it will issue the iocb on the wire and update the
11235  * fabric iocb count it indicate that there is one fabric iocb on the wire.
11236  *
11237  * Note, this implementation has a potential sending out fabric IOCBs out of
11238  * order. The problem is caused by the construction of the "ready" boolen does
11239  * not include the condition that the internal fabric IOCB list is empty. As
11240  * such, it is possible a fabric IOCB issued by this routine might be "jump"
11241  * ahead of the fabric IOCBs in the internal list.
11242  *
11243  * Return code
11244  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
11245  *   IOCB_ERROR - failed to issue fabric iocb
11246  **/
11247 static int
11248 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
11249 {
11250 	unsigned long iflags;
11251 	int ready;
11252 	int ret;
11253 
11254 	BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
11255 
11256 	spin_lock_irqsave(&phba->hbalock, iflags);
11257 	ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
11258 		!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11259 
11260 	if (ready)
11261 		/* Increment fabric iocb count to hold the position */
11262 		atomic_inc(&phba->fabric_iocb_count);
11263 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11264 	if (ready) {
11265 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
11266 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
11267 		iocb->iocb_flag |= LPFC_IO_FABRIC;
11268 
11269 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11270 			"Fabric sched2:   ste:x%x",
11271 			iocb->vport->port_state, 0, 0);
11272 
11273 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11274 
11275 		if (ret == IOCB_ERROR) {
11276 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
11277 			iocb->fabric_iocb_cmpl = NULL;
11278 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
11279 			atomic_dec(&phba->fabric_iocb_count);
11280 		}
11281 	} else {
11282 		spin_lock_irqsave(&phba->hbalock, iflags);
11283 		list_add_tail(&iocb->list, &phba->fabric_iocb_list);
11284 		spin_unlock_irqrestore(&phba->hbalock, iflags);
11285 		ret = IOCB_SUCCESS;
11286 	}
11287 	return ret;
11288 }
11289 
11290 /**
11291  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
11292  * @vport: pointer to a virtual N_Port data structure.
11293  *
11294  * This routine aborts all the IOCBs associated with a @vport from the
11295  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11296  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11297  * list, removes each IOCB associated with the @vport off the list, set the
11298  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11299  * associated with the IOCB.
11300  **/
11301 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
11302 {
11303 	LIST_HEAD(completions);
11304 	struct lpfc_hba  *phba = vport->phba;
11305 	struct lpfc_iocbq *tmp_iocb, *piocb;
11306 
11307 	spin_lock_irq(&phba->hbalock);
11308 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
11309 				 list) {
11310 
11311 		if (piocb->vport != vport)
11312 			continue;
11313 
11314 		list_move_tail(&piocb->list, &completions);
11315 	}
11316 	spin_unlock_irq(&phba->hbalock);
11317 
11318 	/* Cancel all the IOCBs from the completions list */
11319 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11320 			      IOERR_SLI_ABORTED);
11321 }
11322 
11323 /**
11324  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
11325  * @ndlp: pointer to a node-list data structure.
11326  *
11327  * This routine aborts all the IOCBs associated with an @ndlp from the
11328  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
11329  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
11330  * list, removes each IOCB associated with the @ndlp off the list, set the
11331  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
11332  * associated with the IOCB.
11333  **/
11334 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
11335 {
11336 	LIST_HEAD(completions);
11337 	struct lpfc_hba  *phba = ndlp->phba;
11338 	struct lpfc_iocbq *tmp_iocb, *piocb;
11339 	struct lpfc_sli_ring *pring;
11340 
11341 	pring = lpfc_phba_elsring(phba);
11342 
11343 	if (unlikely(!pring))
11344 		return;
11345 
11346 	spin_lock_irq(&phba->hbalock);
11347 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
11348 				 list) {
11349 		if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
11350 
11351 			list_move_tail(&piocb->list, &completions);
11352 		}
11353 	}
11354 	spin_unlock_irq(&phba->hbalock);
11355 
11356 	/* Cancel all the IOCBs from the completions list */
11357 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11358 			      IOERR_SLI_ABORTED);
11359 }
11360 
11361 /**
11362  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
11363  * @phba: pointer to lpfc hba data structure.
11364  *
11365  * This routine aborts all the IOCBs currently on the driver internal
11366  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
11367  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
11368  * list, removes IOCBs off the list, set the status field to
11369  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
11370  * the IOCB.
11371  **/
11372 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
11373 {
11374 	LIST_HEAD(completions);
11375 
11376 	spin_lock_irq(&phba->hbalock);
11377 	list_splice_init(&phba->fabric_iocb_list, &completions);
11378 	spin_unlock_irq(&phba->hbalock);
11379 
11380 	/* Cancel all the IOCBs from the completions list */
11381 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11382 			      IOERR_SLI_ABORTED);
11383 }
11384 
11385 /**
11386  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
11387  * @vport: pointer to lpfc vport data structure.
11388  *
11389  * This routine is invoked by the vport cleanup for deletions and the cleanup
11390  * for an ndlp on removal.
11391  **/
11392 void
11393 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
11394 {
11395 	struct lpfc_hba *phba = vport->phba;
11396 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
11397 	struct lpfc_nodelist *ndlp = NULL;
11398 	unsigned long iflag = 0;
11399 
11400 	spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
11401 	list_for_each_entry_safe(sglq_entry, sglq_next,
11402 			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
11403 		if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport) {
11404 			lpfc_nlp_put(sglq_entry->ndlp);
11405 			ndlp = sglq_entry->ndlp;
11406 			sglq_entry->ndlp = NULL;
11407 
11408 			/* If the xri on the abts_els_sgl list is for the Fport
11409 			 * node and the vport is unloading, the xri aborted wcqe
11410 			 * likely isn't coming back.  Just release the sgl.
11411 			 */
11412 			if ((vport->load_flag & FC_UNLOADING) &&
11413 			    ndlp->nlp_DID == Fabric_DID) {
11414 				list_del(&sglq_entry->list);
11415 				sglq_entry->state = SGL_FREED;
11416 				list_add_tail(&sglq_entry->list,
11417 					&phba->sli4_hba.lpfc_els_sgl_list);
11418 			}
11419 		}
11420 	}
11421 	spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
11422 	return;
11423 }
11424 
11425 /**
11426  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
11427  * @phba: pointer to lpfc hba data structure.
11428  * @axri: pointer to the els xri abort wcqe structure.
11429  *
11430  * This routine is invoked by the worker thread to process a SLI4 slow-path
11431  * ELS aborted xri.
11432  **/
11433 void
11434 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
11435 			  struct sli4_wcqe_xri_aborted *axri)
11436 {
11437 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
11438 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
11439 	uint16_t lxri = 0;
11440 
11441 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
11442 	unsigned long iflag = 0;
11443 	struct lpfc_nodelist *ndlp;
11444 	struct lpfc_sli_ring *pring;
11445 
11446 	pring = lpfc_phba_elsring(phba);
11447 
11448 	spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
11449 	list_for_each_entry_safe(sglq_entry, sglq_next,
11450 			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
11451 		if (sglq_entry->sli4_xritag == xri) {
11452 			list_del(&sglq_entry->list);
11453 			ndlp = sglq_entry->ndlp;
11454 			sglq_entry->ndlp = NULL;
11455 			list_add_tail(&sglq_entry->list,
11456 				&phba->sli4_hba.lpfc_els_sgl_list);
11457 			sglq_entry->state = SGL_FREED;
11458 			spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock,
11459 					       iflag);
11460 
11461 			if (ndlp) {
11462 				lpfc_set_rrq_active(phba, ndlp,
11463 					sglq_entry->sli4_lxritag,
11464 					rxid, 1);
11465 				lpfc_nlp_put(ndlp);
11466 			}
11467 
11468 			/* Check if TXQ queue needs to be serviced */
11469 			if (pring && !list_empty(&pring->txq))
11470 				lpfc_worker_wake_up(phba);
11471 			return;
11472 		}
11473 	}
11474 	spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
11475 	lxri = lpfc_sli4_xri_inrange(phba, xri);
11476 	if (lxri == NO_XRI)
11477 		return;
11478 
11479 	spin_lock_irqsave(&phba->hbalock, iflag);
11480 	sglq_entry = __lpfc_get_active_sglq(phba, lxri);
11481 	if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
11482 		spin_unlock_irqrestore(&phba->hbalock, iflag);
11483 		return;
11484 	}
11485 	sglq_entry->state = SGL_XRI_ABORTED;
11486 	spin_unlock_irqrestore(&phba->hbalock, iflag);
11487 	return;
11488 }
11489 
11490 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
11491  * @vport: pointer to virtual port object.
11492  * @ndlp: nodelist pointer for the impacted node.
11493  *
11494  * The driver calls this routine in response to an SLI4 XRI ABORT CQE
11495  * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
11496  * the driver is required to send a LOGO to the remote node before it
11497  * attempts to recover its login to the remote node.
11498  */
11499 void
11500 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
11501 			   struct lpfc_nodelist *ndlp)
11502 {
11503 	struct Scsi_Host *shost;
11504 	struct lpfc_hba *phba;
11505 	unsigned long flags = 0;
11506 
11507 	shost = lpfc_shost_from_vport(vport);
11508 	phba = vport->phba;
11509 	if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
11510 		lpfc_printf_log(phba, KERN_INFO,
11511 				LOG_SLI, "3093 No rport recovery needed. "
11512 				"rport in state 0x%x\n", ndlp->nlp_state);
11513 		return;
11514 	}
11515 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11516 			"3094 Start rport recovery on shost id 0x%x "
11517 			"fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
11518 			"flags 0x%x\n",
11519 			shost->host_no, ndlp->nlp_DID,
11520 			vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
11521 			ndlp->nlp_flag);
11522 	/*
11523 	 * The rport is not responding.  Remove the FCP-2 flag to prevent
11524 	 * an ADISC in the follow-up recovery code.
11525 	 */
11526 	spin_lock_irqsave(&ndlp->lock, flags);
11527 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
11528 	ndlp->nlp_flag |= NLP_ISSUE_LOGO;
11529 	spin_unlock_irqrestore(&ndlp->lock, flags);
11530 	lpfc_unreg_rpi(vport, ndlp);
11531 }
11532 
11533 static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport)
11534 {
11535 	bitmap_zero(vport->vmid_priority_range, LPFC_VMID_MAX_PRIORITY_RANGE);
11536 }
11537 
11538 static void
11539 lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max)
11540 {
11541 	u32 i;
11542 
11543 	if ((min > max) || (max > LPFC_VMID_MAX_PRIORITY_RANGE))
11544 		return;
11545 
11546 	for (i = min; i <= max; i++)
11547 		set_bit(i, vport->vmid_priority_range);
11548 }
11549 
11550 static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid)
11551 {
11552 	set_bit(ctcl_vmid, vport->vmid_priority_range);
11553 }
11554 
11555 u32 lpfc_vmid_get_cs_ctl(struct lpfc_vport *vport)
11556 {
11557 	u32 i;
11558 
11559 	i = find_first_bit(vport->vmid_priority_range,
11560 			   LPFC_VMID_MAX_PRIORITY_RANGE);
11561 
11562 	if (i == LPFC_VMID_MAX_PRIORITY_RANGE)
11563 		return 0;
11564 
11565 	clear_bit(i, vport->vmid_priority_range);
11566 	return i;
11567 }
11568 
11569 #define MAX_PRIORITY_DESC	255
11570 
11571 static void
11572 lpfc_cmpl_els_qfpa(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11573 		   struct lpfc_iocbq *rspiocb)
11574 {
11575 	struct lpfc_vport *vport = cmdiocb->vport;
11576 	struct priority_range_desc *desc;
11577 	struct lpfc_dmabuf *prsp = NULL;
11578 	struct lpfc_vmid_priority_range *vmid_range = NULL;
11579 	u32 *data;
11580 	struct lpfc_dmabuf *dmabuf = cmdiocb->context2;
11581 	IOCB_t *irsp = &rspiocb->iocb;
11582 	u8 *pcmd, max_desc;
11583 	u32 len, i;
11584 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
11585 
11586 	prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
11587 	if (!prsp)
11588 		goto out;
11589 
11590 	pcmd = prsp->virt;
11591 	data = (u32 *)pcmd;
11592 	if (data[0] == ELS_CMD_LS_RJT) {
11593 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
11594 				 "3277 QFPA LS_RJT x%x  x%x\n",
11595 				 data[0], data[1]);
11596 		goto out;
11597 	}
11598 	if (irsp->ulpStatus) {
11599 		lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
11600 				 "6529 QFPA failed with status x%x  x%x\n",
11601 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
11602 		goto out;
11603 	}
11604 
11605 	if (!vport->qfpa_res) {
11606 		max_desc = FCELSSIZE / sizeof(*vport->qfpa_res);
11607 		vport->qfpa_res = kcalloc(max_desc, sizeof(*vport->qfpa_res),
11608 					  GFP_KERNEL);
11609 		if (!vport->qfpa_res)
11610 			goto out;
11611 	}
11612 
11613 	len = *((u32 *)(pcmd + 4));
11614 	len = be32_to_cpu(len);
11615 	memcpy(vport->qfpa_res, pcmd, len + 8);
11616 	len = len / LPFC_PRIORITY_RANGE_DESC_SIZE;
11617 
11618 	desc = (struct priority_range_desc *)(pcmd + 8);
11619 	vmid_range = vport->vmid_priority.vmid_range;
11620 	if (!vmid_range) {
11621 		vmid_range = kcalloc(MAX_PRIORITY_DESC, sizeof(*vmid_range),
11622 				     GFP_KERNEL);
11623 		if (!vmid_range) {
11624 			kfree(vport->qfpa_res);
11625 			goto out;
11626 		}
11627 		vport->vmid_priority.vmid_range = vmid_range;
11628 	}
11629 	vport->vmid_priority.num_descriptors = len;
11630 
11631 	for (i = 0; i < len; i++, vmid_range++, desc++) {
11632 		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
11633 				 "6539 vmid values low=%d, high=%d, qos=%d, "
11634 				 "local ve id=%d\n", desc->lo_range,
11635 				 desc->hi_range, desc->qos_priority,
11636 				 desc->local_ve_id);
11637 
11638 		vmid_range->low = desc->lo_range << 1;
11639 		if (desc->local_ve_id == QFPA_ODD_ONLY)
11640 			vmid_range->low++;
11641 		if (desc->qos_priority)
11642 			vport->vmid_flag |= LPFC_VMID_QOS_ENABLED;
11643 		vmid_range->qos = desc->qos_priority;
11644 
11645 		vmid_range->high = desc->hi_range << 1;
11646 		if ((desc->local_ve_id == QFPA_ODD_ONLY) ||
11647 		    (desc->local_ve_id == QFPA_EVEN_ODD))
11648 			vmid_range->high++;
11649 	}
11650 	lpfc_init_cs_ctl_bitmap(vport);
11651 	for (i = 0; i < vport->vmid_priority.num_descriptors; i++) {
11652 		lpfc_vmid_set_cs_ctl_range(vport,
11653 				vport->vmid_priority.vmid_range[i].low,
11654 				vport->vmid_priority.vmid_range[i].high);
11655 	}
11656 
11657 	vport->vmid_flag |= LPFC_VMID_QFPA_CMPL;
11658  out:
11659 	lpfc_els_free_iocb(phba, cmdiocb);
11660 	lpfc_nlp_put(ndlp);
11661 }
11662 
11663 int lpfc_issue_els_qfpa(struct lpfc_vport *vport)
11664 {
11665 	struct lpfc_hba *phba = vport->phba;
11666 	struct lpfc_nodelist *ndlp;
11667 	struct lpfc_iocbq *elsiocb;
11668 	u8 *pcmd;
11669 	int ret;
11670 
11671 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
11672 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
11673 		return -ENXIO;
11674 
11675 	elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_QFPA_SIZE, 2, ndlp,
11676 				     ndlp->nlp_DID, ELS_CMD_QFPA);
11677 	if (!elsiocb)
11678 		return -ENOMEM;
11679 
11680 	pcmd = (u8 *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
11681 
11682 	*((u32 *)(pcmd)) = ELS_CMD_QFPA;
11683 	pcmd += 4;
11684 
11685 	elsiocb->iocb_cmpl = lpfc_cmpl_els_qfpa;
11686 
11687 	elsiocb->context1 = lpfc_nlp_get(ndlp);
11688 	if (!elsiocb->context1) {
11689 		lpfc_els_free_iocb(vport->phba, elsiocb);
11690 		return -ENXIO;
11691 	}
11692 
11693 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 2);
11694 	if (ret != IOCB_SUCCESS) {
11695 		lpfc_els_free_iocb(phba, elsiocb);
11696 		lpfc_nlp_put(ndlp);
11697 		return -EIO;
11698 	}
11699 	vport->vmid_flag &= ~LPFC_VMID_QOS_ENABLED;
11700 	return 0;
11701 }
11702 
11703 int
11704 lpfc_vmid_uvem(struct lpfc_vport *vport,
11705 	       struct lpfc_vmid *vmid, bool instantiated)
11706 {
11707 	struct lpfc_vem_id_desc *vem_id_desc;
11708 	struct lpfc_nodelist *ndlp;
11709 	struct lpfc_iocbq *elsiocb;
11710 	struct instantiated_ve_desc *inst_desc;
11711 	struct lpfc_vmid_context *vmid_context;
11712 	u8 *pcmd;
11713 	u32 *len;
11714 	int ret = 0;
11715 
11716 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
11717 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
11718 		return -ENXIO;
11719 
11720 	vmid_context = kmalloc(sizeof(*vmid_context), GFP_KERNEL);
11721 	if (!vmid_context)
11722 		return -ENOMEM;
11723 	elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_UVEM_SIZE, 2,
11724 				     ndlp, Fabric_DID, ELS_CMD_UVEM);
11725 	if (!elsiocb)
11726 		goto out;
11727 
11728 	lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
11729 			 "3427 Host vmid %s %d\n",
11730 			 vmid->host_vmid, instantiated);
11731 	vmid_context->vmp = vmid;
11732 	vmid_context->nlp = ndlp;
11733 	vmid_context->instantiated = instantiated;
11734 	elsiocb->vmid_tag.vmid_context = vmid_context;
11735 	pcmd = (u8 *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
11736 
11737 	if (uuid_is_null((uuid_t *)vport->lpfc_vmid_host_uuid))
11738 		memcpy(vport->lpfc_vmid_host_uuid, vmid->host_vmid,
11739 		       LPFC_COMPRESS_VMID_SIZE);
11740 
11741 	*((u32 *)(pcmd)) = ELS_CMD_UVEM;
11742 	len = (u32 *)(pcmd + 4);
11743 	*len = cpu_to_be32(LPFC_UVEM_SIZE - 8);
11744 
11745 	vem_id_desc = (struct lpfc_vem_id_desc *)(pcmd + 8);
11746 	vem_id_desc->tag = be32_to_cpu(VEM_ID_DESC_TAG);
11747 	vem_id_desc->length = be32_to_cpu(LPFC_UVEM_VEM_ID_DESC_SIZE);
11748 	memcpy(vem_id_desc->vem_id, vport->lpfc_vmid_host_uuid,
11749 	       LPFC_COMPRESS_VMID_SIZE);
11750 
11751 	inst_desc = (struct instantiated_ve_desc *)(pcmd + 32);
11752 	inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
11753 	inst_desc->length = be32_to_cpu(LPFC_UVEM_VE_MAP_DESC_SIZE);
11754 	memcpy(inst_desc->global_vem_id, vmid->host_vmid,
11755 	       LPFC_COMPRESS_VMID_SIZE);
11756 
11757 	bf_set(lpfc_instantiated_nport_id, inst_desc, vport->fc_myDID);
11758 	bf_set(lpfc_instantiated_local_id, inst_desc,
11759 	       vmid->un.cs_ctl_vmid);
11760 	if (instantiated) {
11761 		inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
11762 	} else {
11763 		inst_desc->tag = be32_to_cpu(DEINSTANTIATED_VE_DESC_TAG);
11764 		lpfc_vmid_put_cs_ctl(vport, vmid->un.cs_ctl_vmid);
11765 	}
11766 	inst_desc->word6 = cpu_to_be32(inst_desc->word6);
11767 
11768 	elsiocb->iocb_cmpl = lpfc_cmpl_els_uvem;
11769 
11770 	elsiocb->context1 = lpfc_nlp_get(ndlp);
11771 	if (!elsiocb->context1) {
11772 		lpfc_els_free_iocb(vport->phba, elsiocb);
11773 		goto out;
11774 	}
11775 
11776 	ret = lpfc_sli_issue_iocb(vport->phba, LPFC_ELS_RING, elsiocb, 0);
11777 	if (ret != IOCB_SUCCESS) {
11778 		lpfc_els_free_iocb(vport->phba, elsiocb);
11779 		lpfc_nlp_put(ndlp);
11780 		goto out;
11781 	}
11782 
11783 	return 0;
11784  out:
11785 	kfree(vmid_context);
11786 	return -EIO;
11787 }
11788 
11789 static void
11790 lpfc_cmpl_els_uvem(struct lpfc_hba *phba, struct lpfc_iocbq *icmdiocb,
11791 		   struct lpfc_iocbq *rspiocb)
11792 {
11793 	struct lpfc_vport *vport = icmdiocb->vport;
11794 	struct lpfc_dmabuf *prsp = NULL;
11795 	struct lpfc_vmid_context *vmid_context =
11796 	    icmdiocb->vmid_tag.vmid_context;
11797 	struct lpfc_nodelist *ndlp = icmdiocb->context1;
11798 	u8 *pcmd;
11799 	u32 *data;
11800 	IOCB_t *irsp = &rspiocb->iocb;
11801 	struct lpfc_dmabuf *dmabuf = icmdiocb->context2;
11802 	struct lpfc_vmid *vmid;
11803 
11804 	vmid = vmid_context->vmp;
11805 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
11806 		ndlp = NULL;
11807 
11808 	prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
11809 	if (!prsp)
11810 		goto out;
11811 	pcmd = prsp->virt;
11812 	data = (u32 *)pcmd;
11813 	if (data[0] == ELS_CMD_LS_RJT) {
11814 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
11815 				 "4532 UVEM LS_RJT %x %x\n", data[0], data[1]);
11816 		goto out;
11817 	}
11818 	if (irsp->ulpStatus) {
11819 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
11820 				 "4533 UVEM error status %x: %x\n",
11821 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
11822 		goto out;
11823 	}
11824 	spin_lock(&phba->hbalock);
11825 	/* Set IN USE flag */
11826 	vport->vmid_flag |= LPFC_VMID_IN_USE;
11827 	phba->pport->vmid_flag |= LPFC_VMID_IN_USE;
11828 	spin_unlock(&phba->hbalock);
11829 
11830 	if (vmid_context->instantiated) {
11831 		write_lock(&vport->vmid_lock);
11832 		vmid->flag |= LPFC_VMID_REGISTERED;
11833 		vmid->flag &= ~LPFC_VMID_REQ_REGISTER;
11834 		write_unlock(&vport->vmid_lock);
11835 	}
11836 
11837  out:
11838 	kfree(vmid_context);
11839 	lpfc_els_free_iocb(phba, icmdiocb);
11840 	lpfc_nlp_put(ndlp);
11841 }
11842