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