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