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