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