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