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