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