xref: /linux/drivers/scsi/lpfc/lpfc_els.c (revision e2dacf8e5e33e9d268874882ad1e0d307f693db0)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2025 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 /* See Fibre Channel protocol T11 FC-LS for details */
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include <uapi/scsi/fc/fc_fs.h>
35 #include <uapi/scsi/fc/fc_els.h>
36 
37 #include "lpfc_hw4.h"
38 #include "lpfc_hw.h"
39 #include "lpfc_sli.h"
40 #include "lpfc_sli4.h"
41 #include "lpfc_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_scsi.h"
44 #include "lpfc.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_crtn.h"
47 #include "lpfc_vport.h"
48 #include "lpfc_debugfs.h"
49 
50 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
51 			  struct lpfc_iocbq *);
52 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
53 			struct lpfc_iocbq *);
54 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
55 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
56 				struct lpfc_nodelist *ndlp, uint8_t retry);
57 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
58 				  struct lpfc_iocbq *iocb);
59 static void lpfc_cmpl_els_edc(struct lpfc_hba *phba,
60 			      struct lpfc_iocbq *cmdiocb,
61 			      struct lpfc_iocbq *rspiocb);
62 static void lpfc_cmpl_els_uvem(struct lpfc_hba *, struct lpfc_iocbq *,
63 			       struct lpfc_iocbq *);
64 
65 static int lpfc_max_els_tries = 3;
66 
67 static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport);
68 static void lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max);
69 static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid);
70 
71 /**
72  * lpfc_els_chk_latt - Check host link attention event for a vport
73  * @vport: pointer to a host virtual N_Port data structure.
74  *
75  * This routine checks whether there is an outstanding host link
76  * attention event during the discovery process with the @vport. It is done
77  * by reading the HBA's Host Attention (HA) register. If there is any host
78  * link attention events during this @vport's discovery process, the @vport
79  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
80  * be issued if the link state is not already in host link cleared state,
81  * and a return code shall indicate whether the host link attention event
82  * had happened.
83  *
84  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
85  * state in LPFC_VPORT_READY, the request for checking host link attention
86  * event will be ignored and a return code shall indicate no host link
87  * attention event had happened.
88  *
89  * Return codes
90  *   0 - no host link attention event happened
91  *   1 - host link attention event happened
92  **/
93 int
94 lpfc_els_chk_latt(struct lpfc_vport *vport)
95 {
96 	struct lpfc_hba  *phba = vport->phba;
97 	uint32_t ha_copy;
98 
99 	if (vport->port_state >= LPFC_VPORT_READY ||
100 	    phba->link_state == LPFC_LINK_DOWN ||
101 	    phba->sli_rev > LPFC_SLI_REV3)
102 		return 0;
103 
104 	/* Read the HBA Host Attention Register */
105 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
106 		return 1;
107 
108 	if (!(ha_copy & HA_LATT))
109 		return 0;
110 
111 	/* Pending Link Event during Discovery */
112 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
113 			 "0237 Pending Link Event during "
114 			 "Discovery: State x%x\n",
115 			 phba->pport->port_state);
116 
117 	/* CLEAR_LA should re-enable link attention events and
118 	 * we should then immediately take a LATT event. The
119 	 * LATT processing should call lpfc_linkdown() which
120 	 * will cleanup any left over in-progress discovery
121 	 * events.
122 	 */
123 	set_bit(FC_ABORT_DISCOVERY, &vport->fc_flag);
124 
125 	if (phba->link_state != LPFC_CLEAR_LA)
126 		lpfc_issue_clear_la(phba, vport);
127 
128 	return 1;
129 }
130 
131 static bool lpfc_is_els_acc_rsp(struct lpfc_dmabuf *buf)
132 {
133 	struct fc_els_ls_acc *rsp = buf->virt;
134 
135 	if (rsp && rsp->la_cmd == ELS_LS_ACC)
136 		return true;
137 	return false;
138 }
139 
140 /**
141  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
142  * @vport: pointer to a host virtual N_Port data structure.
143  * @expect_rsp: flag indicating whether response is expected.
144  * @cmd_size: size of the ELS command.
145  * @retry: number of retries to the command when it fails.
146  * @ndlp: pointer to a node-list data structure.
147  * @did: destination identifier.
148  * @elscmd: the ELS command code.
149  *
150  * This routine is used for allocating a lpfc-IOCB data structure from
151  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
152  * passed into the routine for discovery state machine to issue an Extended
153  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
154  * and preparation routine that is used by all the discovery state machine
155  * routines and the ELS command-specific fields will be later set up by
156  * the individual discovery machine routines after calling this routine
157  * allocating and preparing a generic IOCB data structure. It fills in the
158  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
159  * payload and response payload (if expected). The reference count on the
160  * ndlp is incremented by 1 and the reference to the ndlp is put into
161  * ndlp of the IOCB data structure for this IOCB to hold the ndlp
162  * reference for the command's callback function to access later.
163  *
164  * Return code
165  *   Pointer to the newly allocated/prepared els iocb data structure
166  *   NULL - when els iocb data structure allocation/preparation failed
167  **/
168 struct lpfc_iocbq *
169 lpfc_prep_els_iocb(struct lpfc_vport *vport, u8 expect_rsp,
170 		   u16 cmd_size, u8 retry,
171 		   struct lpfc_nodelist *ndlp, u32 did,
172 		   u32 elscmd)
173 {
174 	struct lpfc_hba  *phba = vport->phba;
175 	struct lpfc_iocbq *elsiocb;
176 	struct lpfc_dmabuf *pcmd, *prsp, *pbuflist, *bmp;
177 	struct ulp_bde64_le *bpl;
178 	u32 timeout = 0;
179 
180 	if (!lpfc_is_link_up(phba))
181 		return NULL;
182 
183 	/* Allocate buffer for  command iocb */
184 	elsiocb = lpfc_sli_get_iocbq(phba);
185 	if (!elsiocb)
186 		return NULL;
187 
188 	/*
189 	 * If this command is for fabric controller and HBA running
190 	 * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
191 	 */
192 	if (did == Fabric_DID &&
193 	    test_bit(HBA_FIP_SUPPORT, &phba->hba_flag) &&
194 	    (elscmd == ELS_CMD_FLOGI ||
195 	     elscmd == ELS_CMD_FDISC ||
196 	     elscmd == ELS_CMD_LOGO))
197 		switch (elscmd) {
198 		case ELS_CMD_FLOGI:
199 			elsiocb->cmd_flag |=
200 				((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
201 				 & LPFC_FIP_ELS_ID_MASK);
202 			break;
203 		case ELS_CMD_FDISC:
204 			elsiocb->cmd_flag |=
205 				((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
206 				 & LPFC_FIP_ELS_ID_MASK);
207 			break;
208 		case ELS_CMD_LOGO:
209 			elsiocb->cmd_flag |=
210 				((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
211 				 & LPFC_FIP_ELS_ID_MASK);
212 			break;
213 		}
214 	else
215 		elsiocb->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
216 
217 	/* fill in BDEs for command */
218 	/* Allocate buffer for command payload */
219 	pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
220 	if (pcmd)
221 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
222 	if (!pcmd || !pcmd->virt)
223 		goto els_iocb_free_pcmb_exit;
224 
225 	INIT_LIST_HEAD(&pcmd->list);
226 
227 	/* Allocate buffer for response payload */
228 	if (expect_rsp) {
229 		prsp = kmalloc(sizeof(*prsp), GFP_KERNEL);
230 		if (prsp)
231 			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
232 						     &prsp->phys);
233 		if (!prsp || !prsp->virt)
234 			goto els_iocb_free_prsp_exit;
235 		INIT_LIST_HEAD(&prsp->list);
236 	} else {
237 		prsp = NULL;
238 	}
239 
240 	/* Allocate buffer for Buffer ptr list */
241 	pbuflist = kmalloc(sizeof(*pbuflist), GFP_KERNEL);
242 	if (pbuflist)
243 		pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
244 						 &pbuflist->phys);
245 	if (!pbuflist || !pbuflist->virt)
246 		goto els_iocb_free_pbuf_exit;
247 
248 	INIT_LIST_HEAD(&pbuflist->list);
249 
250 	if (expect_rsp) {
251 		switch (elscmd) {
252 		case ELS_CMD_FLOGI:
253 			timeout = FF_DEF_RATOV * 2;
254 			break;
255 		case ELS_CMD_LOGO:
256 			timeout = phba->fc_ratov;
257 			break;
258 		default:
259 			timeout = phba->fc_ratov * 2;
260 		}
261 
262 		/* Fill SGE for the num bde count */
263 		elsiocb->num_bdes = 2;
264 	}
265 
266 	if (phba->sli_rev == LPFC_SLI_REV4)
267 		bmp = pcmd;
268 	else
269 		bmp = pbuflist;
270 
271 	lpfc_sli_prep_els_req_rsp(phba, elsiocb, vport, bmp, cmd_size, did,
272 				  elscmd, timeout, expect_rsp);
273 
274 	bpl = (struct ulp_bde64_le *)pbuflist->virt;
275 	bpl->addr_low = cpu_to_le32(putPaddrLow(pcmd->phys));
276 	bpl->addr_high = cpu_to_le32(putPaddrHigh(pcmd->phys));
277 	bpl->type_size = cpu_to_le32(cmd_size);
278 	bpl->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
279 
280 	if (expect_rsp) {
281 		bpl++;
282 		bpl->addr_low = cpu_to_le32(putPaddrLow(prsp->phys));
283 		bpl->addr_high = cpu_to_le32(putPaddrHigh(prsp->phys));
284 		bpl->type_size = cpu_to_le32(FCELSSIZE);
285 		bpl->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
286 	}
287 
288 	elsiocb->cmd_dmabuf = pcmd;
289 	elsiocb->bpl_dmabuf = pbuflist;
290 	elsiocb->retry = retry;
291 	elsiocb->vport = vport;
292 	elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
293 
294 	if (prsp)
295 		list_add(&prsp->list, &pcmd->list);
296 	if (expect_rsp) {
297 		/* Xmit ELS command <elsCmd> to remote NPORT <did> */
298 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
299 				 "0116 Xmit ELS command x%x to remote "
300 				 "NPORT x%x I/O tag: x%x, port state:x%x "
301 				 "rpi x%x fc_flag:x%lx\n",
302 				 elscmd, did, elsiocb->iotag,
303 				 vport->port_state, ndlp->nlp_rpi,
304 				 vport->fc_flag);
305 	} else {
306 		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
307 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
308 				 "0117 Xmit ELS response x%x to remote "
309 				 "NPORT x%x I/O tag: x%x, size: x%x "
310 				 "port_state x%x  rpi x%x fc_flag x%lx\n",
311 				 elscmd, ndlp->nlp_DID, elsiocb->iotag,
312 				 cmd_size, vport->port_state,
313 				 ndlp->nlp_rpi, vport->fc_flag);
314 	}
315 
316 	return elsiocb;
317 
318 els_iocb_free_pbuf_exit:
319 	if (expect_rsp)
320 		lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
321 	kfree(pbuflist);
322 
323 els_iocb_free_prsp_exit:
324 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
325 	kfree(prsp);
326 
327 els_iocb_free_pcmb_exit:
328 	kfree(pcmd);
329 	lpfc_sli_release_iocbq(phba, elsiocb);
330 	return NULL;
331 }
332 
333 /**
334  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
335  * @vport: pointer to a host virtual N_Port data structure.
336  *
337  * This routine issues a fabric registration login for a @vport. An
338  * active ndlp node with Fabric_DID must already exist for this @vport.
339  * The routine invokes two mailbox commands to carry out fabric registration
340  * login through the HBA firmware: the first mailbox command requests the
341  * HBA to perform link configuration for the @vport; and the second mailbox
342  * command requests the HBA to perform the actual fabric registration login
343  * with the @vport.
344  *
345  * Return code
346  *   0 - successfully issued fabric registration login for @vport
347  *   -ENXIO -- failed to issue fabric registration login for @vport
348  **/
349 int
350 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
351 {
352 	struct lpfc_hba  *phba = vport->phba;
353 	LPFC_MBOXQ_t *mbox;
354 	struct lpfc_nodelist *ndlp;
355 	struct serv_parm *sp;
356 	int rc;
357 	int err = 0;
358 
359 	sp = &phba->fc_fabparam;
360 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
361 	if (!ndlp) {
362 		err = 1;
363 		goto fail;
364 	}
365 
366 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
367 	if (!mbox) {
368 		err = 2;
369 		goto fail;
370 	}
371 
372 	vport->port_state = LPFC_FABRIC_CFG_LINK;
373 	lpfc_config_link(phba, mbox);
374 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
375 	mbox->vport = vport;
376 
377 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
378 	if (rc == MBX_NOT_FINISHED) {
379 		err = 3;
380 		goto fail_free_mbox;
381 	}
382 
383 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
384 	if (!mbox) {
385 		err = 4;
386 		goto fail;
387 	}
388 	rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
389 			  ndlp->nlp_rpi);
390 	if (rc) {
391 		err = 5;
392 		goto fail_free_mbox;
393 	}
394 
395 	mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
396 	mbox->vport = vport;
397 	/* increment the reference count on ndlp to hold reference
398 	 * for the callback routine.
399 	 */
400 	mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
401 	if (!mbox->ctx_ndlp) {
402 		err = 6;
403 		goto fail_free_mbox;
404 	}
405 
406 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
407 	if (rc == MBX_NOT_FINISHED) {
408 		err = 7;
409 		goto fail_issue_reg_login;
410 	}
411 
412 	return 0;
413 
414 fail_issue_reg_login:
415 	/* decrement the reference count on ndlp just incremented
416 	 * for the failed mbox command.
417 	 */
418 	lpfc_nlp_put(ndlp);
419 fail_free_mbox:
420 	lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
421 fail:
422 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
423 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
424 			 "0249 Cannot issue Register Fabric login: Err %d\n",
425 			 err);
426 	return -ENXIO;
427 }
428 
429 /**
430  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
431  * @vport: pointer to a host virtual N_Port data structure.
432  *
433  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
434  * the @vport. This mailbox command is necessary for SLI4 port only.
435  *
436  * Return code
437  *   0 - successfully issued REG_VFI for @vport
438  *   A failure code otherwise.
439  **/
440 int
441 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
442 {
443 	struct lpfc_hba  *phba = vport->phba;
444 	LPFC_MBOXQ_t *mboxq = NULL;
445 	struct lpfc_nodelist *ndlp;
446 	struct lpfc_dmabuf *dmabuf = NULL;
447 	int rc = 0;
448 
449 	/* move forward in case of SLI4 FC port loopback test and pt2pt mode */
450 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
451 	    !(phba->link_flag & LS_LOOPBACK_MODE) &&
452 	    !test_bit(FC_PT2PT, &vport->fc_flag)) {
453 		ndlp = lpfc_findnode_did(vport, Fabric_DID);
454 		if (!ndlp) {
455 			rc = -ENODEV;
456 			goto fail;
457 		}
458 	}
459 
460 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
461 	if (!mboxq) {
462 		rc = -ENOMEM;
463 		goto fail;
464 	}
465 
466 	/* Supply CSP's only if we are fabric connect or pt-to-pt connect */
467 	if (test_bit(FC_FABRIC, &vport->fc_flag) ||
468 	    test_bit(FC_PT2PT, &vport->fc_flag)) {
469 		rc = lpfc_mbox_rsrc_prep(phba, mboxq);
470 		if (rc) {
471 			rc = -ENOMEM;
472 			goto fail_mbox;
473 		}
474 		dmabuf = mboxq->ctx_buf;
475 		memcpy(dmabuf->virt, &phba->fc_fabparam,
476 		       sizeof(struct serv_parm));
477 	}
478 
479 	vport->port_state = LPFC_FABRIC_CFG_LINK;
480 	if (dmabuf) {
481 		lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
482 		/* lpfc_reg_vfi memsets the mailbox.  Restore the ctx_buf. */
483 		mboxq->ctx_buf = dmabuf;
484 	} else {
485 		lpfc_reg_vfi(mboxq, vport, 0);
486 	}
487 
488 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
489 	mboxq->vport = vport;
490 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
491 	if (rc == MBX_NOT_FINISHED) {
492 		rc = -ENXIO;
493 		goto fail_mbox;
494 	}
495 	return 0;
496 
497 fail_mbox:
498 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
499 fail:
500 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
501 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
502 			 "0289 Issue Register VFI failed: Err %d\n", rc);
503 	return rc;
504 }
505 
506 /**
507  * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
508  * @vport: pointer to a host virtual N_Port data structure.
509  *
510  * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
511  * the @vport. This mailbox command is necessary for SLI4 port only.
512  *
513  * Return code
514  *   0 - successfully issued REG_VFI for @vport
515  *   A failure code otherwise.
516  **/
517 int
518 lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
519 {
520 	struct lpfc_hba *phba = vport->phba;
521 	LPFC_MBOXQ_t *mboxq;
522 	int rc;
523 
524 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
525 	if (!mboxq) {
526 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
527 				"2556 UNREG_VFI mbox allocation failed"
528 				"HBA state x%x\n", phba->pport->port_state);
529 		return -ENOMEM;
530 	}
531 
532 	lpfc_unreg_vfi(mboxq, vport);
533 	mboxq->vport = vport;
534 	mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
535 
536 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
537 	if (rc == MBX_NOT_FINISHED) {
538 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
539 				"2557 UNREG_VFI issue mbox failed rc x%x "
540 				"HBA state x%x\n",
541 				rc, phba->pport->port_state);
542 		mempool_free(mboxq, phba->mbox_mem_pool);
543 		return -EIO;
544 	}
545 
546 	clear_bit(FC_VFI_REGISTERED, &vport->fc_flag);
547 	return 0;
548 }
549 
550 /**
551  * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
552  * @vport: pointer to a host virtual N_Port data structure.
553  * @sp: pointer to service parameter data structure.
554  *
555  * This routine is called from FLOGI/FDISC completion handler functions.
556  * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
557  * node nodename is changed in the completion service parameter else return
558  * 0. This function also set flag in the vport data structure to delay
559  * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
560  * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
561  * node nodename is changed in the completion service parameter.
562  *
563  * Return code
564  *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
565  *   1 - FCID or Fabric Nodename or Fabric portname is changed.
566  *
567  **/
568 static uint8_t
569 lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
570 		struct serv_parm *sp)
571 {
572 	struct lpfc_hba *phba = vport->phba;
573 	uint8_t fabric_param_changed = 0;
574 
575 	if ((vport->fc_prevDID != vport->fc_myDID) ||
576 		memcmp(&vport->fabric_portname, &sp->portName,
577 			sizeof(struct lpfc_name)) ||
578 		memcmp(&vport->fabric_nodename, &sp->nodeName,
579 			sizeof(struct lpfc_name)) ||
580 		(vport->vport_flag & FAWWPN_PARAM_CHG)) {
581 		fabric_param_changed = 1;
582 		vport->vport_flag &= ~FAWWPN_PARAM_CHG;
583 	}
584 	/*
585 	 * Word 1 Bit 31 in common service parameter is overloaded.
586 	 * Word 1 Bit 31 in FLOGI request is multiple NPort request
587 	 * Word 1 Bit 31 in FLOGI response is clean address bit
588 	 *
589 	 * If fabric parameter is changed and clean address bit is
590 	 * cleared delay nport discovery if
591 	 * - vport->fc_prevDID != 0 (not initial discovery) OR
592 	 * - lpfc_delay_discovery module parameter is set.
593 	 */
594 	if (fabric_param_changed && !sp->cmn.clean_address_bit &&
595 	    (vport->fc_prevDID || phba->cfg_delay_discovery))
596 		set_bit(FC_DISC_DELAYED, &vport->fc_flag);
597 
598 	return fabric_param_changed;
599 }
600 
601 
602 /**
603  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
604  * @vport: pointer to a host virtual N_Port data structure.
605  * @ndlp: pointer to a node-list data structure.
606  * @sp: pointer to service parameter data structure.
607  * @ulp_word4: command response value
608  *
609  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
610  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
611  * port in a fabric topology. It properly sets up the parameters to the @ndlp
612  * from the IOCB response. It also check the newly assigned N_Port ID to the
613  * @vport against the previously assigned N_Port ID. If it is different from
614  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
615  * is invoked on all the remaining nodes with the @vport to unregister the
616  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
617  * is invoked to register login to the fabric.
618  *
619  * Return code
620  *   0 - Success (currently, always return 0)
621  **/
622 static int
623 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
624 			   struct serv_parm *sp, uint32_t ulp_word4)
625 {
626 	struct lpfc_hba  *phba = vport->phba;
627 	struct lpfc_nodelist *np;
628 	struct lpfc_nodelist *next_np;
629 	uint8_t fabric_param_changed;
630 
631 	set_bit(FC_FABRIC, &vport->fc_flag);
632 
633 	phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
634 	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
635 		phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
636 
637 	phba->fc_edtovResol = sp->cmn.edtovResolution;
638 	phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
639 
640 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP)
641 		set_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
642 
643 	vport->fc_myDID = ulp_word4 & Mask_DID;
644 	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
645 	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
646 	ndlp->nlp_class_sup = 0;
647 	if (sp->cls1.classValid)
648 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
649 	if (sp->cls2.classValid)
650 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
651 	if (sp->cls3.classValid)
652 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
653 	ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
654 				sp->cmn.bbRcvSizeLsb;
655 
656 	fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
657 	if (fabric_param_changed) {
658 		/* Reset FDMI attribute masks based on config parameter */
659 		if (phba->cfg_enable_SmartSAN ||
660 		    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
661 			/* Setup appropriate attribute masks */
662 			vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
663 			if (phba->cfg_enable_SmartSAN)
664 				vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
665 			else
666 				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
667 		} else {
668 			vport->fdmi_hba_mask = 0;
669 			vport->fdmi_port_mask = 0;
670 		}
671 
672 	}
673 	memcpy(&vport->fabric_portname, &sp->portName,
674 			sizeof(struct lpfc_name));
675 	memcpy(&vport->fabric_nodename, &sp->nodeName,
676 			sizeof(struct lpfc_name));
677 	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
678 
679 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
680 		if (sp->cmn.response_multiple_NPort) {
681 			lpfc_printf_vlog(vport, KERN_WARNING,
682 					 LOG_ELS | LOG_VPORT,
683 					 "1816 FLOGI NPIV supported, "
684 					 "response data 0x%x\n",
685 					 sp->cmn.response_multiple_NPort);
686 			spin_lock_irq(&phba->hbalock);
687 			phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
688 			spin_unlock_irq(&phba->hbalock);
689 		} else {
690 			/* Because we asked f/w for NPIV it still expects us
691 			to call reg_vnpid at least for the physical host */
692 			lpfc_printf_vlog(vport, KERN_WARNING,
693 					 LOG_ELS | LOG_VPORT,
694 					 "1817 Fabric does not support NPIV "
695 					 "- configuring single port mode.\n");
696 			spin_lock_irq(&phba->hbalock);
697 			phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
698 			spin_unlock_irq(&phba->hbalock);
699 		}
700 	}
701 
702 	/*
703 	 * For FC we need to do some special processing because of the SLI
704 	 * Port's default settings of the Common Service Parameters.
705 	 */
706 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
707 	    (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
708 		/* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
709 		if (fabric_param_changed)
710 			lpfc_unregister_fcf_prep(phba);
711 
712 		/* This should just update the VFI CSPs*/
713 		if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag))
714 			lpfc_issue_reg_vfi(vport);
715 	}
716 
717 	if (fabric_param_changed &&
718 		!test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
719 
720 		/* If our NportID changed, we need to ensure all
721 		 * remaining NPORTs get unreg_login'ed.
722 		 */
723 		list_for_each_entry_safe(np, next_np,
724 					&vport->fc_nodes, nlp_listp) {
725 			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
726 			    !test_bit(NLP_NPR_ADISC, &np->nlp_flag))
727 				continue;
728 			clear_bit(NLP_NPR_ADISC, &np->nlp_flag);
729 			lpfc_unreg_rpi(vport, np);
730 		}
731 		lpfc_cleanup_pending_mbox(vport);
732 
733 		if (phba->sli_rev == LPFC_SLI_REV4) {
734 			lpfc_sli4_unreg_all_rpis(vport);
735 			lpfc_mbx_unreg_vpi(vport);
736 			set_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
737 		}
738 
739 		/*
740 		 * For SLI3 and SLI4, the VPI needs to be reregistered in
741 		 * response to this fabric parameter change event.
742 		 */
743 		set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
744 	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
745 		   !test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
746 			/*
747 			 * Driver needs to re-reg VPI in order for f/w
748 			 * to update the MAC address.
749 			 */
750 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
751 			lpfc_register_new_vport(phba, vport, ndlp);
752 			return 0;
753 	}
754 
755 	if (phba->sli_rev < LPFC_SLI_REV4) {
756 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
757 		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
758 		    test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag))
759 			lpfc_register_new_vport(phba, vport, ndlp);
760 		else
761 			lpfc_issue_fabric_reglogin(vport);
762 	} else {
763 		ndlp->nlp_type |= NLP_FABRIC;
764 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
765 		if ((!test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) &&
766 		    (vport->vpi_state & LPFC_VPI_REGISTERED)) {
767 			lpfc_start_fdiscs(phba);
768 			lpfc_do_scr_ns_plogi(phba, vport);
769 		} else if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag))
770 			lpfc_issue_init_vpi(vport);
771 		else {
772 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
773 					"3135 Need register VFI: (x%x/%x)\n",
774 					vport->fc_prevDID, vport->fc_myDID);
775 			lpfc_issue_reg_vfi(vport);
776 		}
777 	}
778 	return 0;
779 }
780 
781 /**
782  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
783  * @vport: pointer to a host virtual N_Port data structure.
784  * @ndlp: pointer to a node-list data structure.
785  * @sp: pointer to service parameter data structure.
786  *
787  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
788  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
789  * in a point-to-point topology. First, the @vport's N_Port Name is compared
790  * with the received N_Port Name: if the @vport's N_Port Name is greater than
791  * the received N_Port Name lexicographically, this node shall assign local
792  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
793  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
794  * this node shall just wait for the remote node to issue PLOGI and assign
795  * N_Port IDs.
796  *
797  * Return code
798  *   0 - Success
799  *   -ENXIO - Fail
800  **/
801 static int
802 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
803 			  struct serv_parm *sp)
804 {
805 	struct lpfc_hba  *phba = vport->phba;
806 	LPFC_MBOXQ_t *mbox;
807 	int rc;
808 
809 	clear_bit(FC_FABRIC, &vport->fc_flag);
810 	clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
811 	set_bit(FC_PT2PT, &vport->fc_flag);
812 
813 	/* If we are pt2pt with another NPort, force NPIV off! */
814 	phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
815 
816 	/* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
817 	if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
818 		lpfc_unregister_fcf_prep(phba);
819 		clear_bit(FC_VFI_REGISTERED, &vport->fc_flag);
820 		phba->fc_topology_changed = 0;
821 	}
822 
823 	rc = memcmp(&vport->fc_portname, &sp->portName,
824 		    sizeof(vport->fc_portname));
825 
826 	if (rc >= 0) {
827 		/* This side will initiate the PLOGI */
828 		set_bit(FC_PT2PT_PLOGI, &vport->fc_flag);
829 
830 		/*
831 		 * N_Port ID cannot be 0, set our Id to LocalID
832 		 * the other side will be RemoteID.
833 		 */
834 
835 		/* not equal */
836 		if (rc)
837 			vport->fc_myDID = PT2PT_LocalID;
838 
839 		/* If not registered with a transport, decrement ndlp reference
840 		 * count indicating that ndlp can be safely released when other
841 		 * references are removed.
842 		 */
843 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)))
844 			lpfc_nlp_put(ndlp);
845 
846 		ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
847 		if (!ndlp) {
848 			/*
849 			 * Cannot find existing Fabric ndlp, so allocate a
850 			 * new one
851 			 */
852 			ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
853 			if (!ndlp)
854 				goto fail;
855 		}
856 
857 		memcpy(&ndlp->nlp_portname, &sp->portName,
858 		       sizeof(struct lpfc_name));
859 		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
860 		       sizeof(struct lpfc_name));
861 		/* Set state will put ndlp onto node list if not already done */
862 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
863 		set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
864 
865 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
866 		if (!mbox)
867 			goto fail;
868 
869 		lpfc_config_link(phba, mbox);
870 
871 		mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
872 		mbox->vport = vport;
873 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
874 		if (rc == MBX_NOT_FINISHED) {
875 			mempool_free(mbox, phba->mbox_mem_pool);
876 			goto fail;
877 		}
878 	} else {
879 		/* This side will wait for the PLOGI. If not registered with
880 		 * a transport, decrement node reference count indicating that
881 		 * ndlp can be released when other references are removed.
882 		 */
883 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)))
884 			lpfc_nlp_put(ndlp);
885 
886 		/* Start discovery - this should just do CLEAR_LA */
887 		lpfc_disc_start(vport);
888 	}
889 
890 	return 0;
891 fail:
892 	return -ENXIO;
893 }
894 
895 /**
896  * lpfc_cmpl_els_flogi - Completion callback function for flogi
897  * @phba: pointer to lpfc hba data structure.
898  * @cmdiocb: pointer to lpfc command iocb data structure.
899  * @rspiocb: pointer to lpfc response iocb data structure.
900  *
901  * This routine is the top-level completion callback function for issuing
902  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
903  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
904  * retry has been made (either immediately or delayed with lpfc_els_retry()
905  * returning 1), the command IOCB will be released and function returned.
906  * If the retry attempt has been given up (possibly reach the maximum
907  * number of retries), one additional decrement of ndlp reference shall be
908  * invoked before going out after releasing the command IOCB. This will
909  * actually release the remote node (Note, lpfc_els_free_iocb() will also
910  * invoke one decrement of ndlp reference count). If no error reported in
911  * the IOCB status, the command Port ID field is used to determine whether
912  * this is a point-to-point topology or a fabric topology: if the Port ID
913  * field is assigned, it is a fabric topology; otherwise, it is a
914  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
915  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
916  * specific topology completion conditions.
917  **/
918 static void
919 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
920 		    struct lpfc_iocbq *rspiocb)
921 {
922 	struct lpfc_vport *vport = cmdiocb->vport;
923 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
924 	IOCB_t *irsp;
925 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf, *prsp;
926 	struct serv_parm *sp;
927 	uint16_t fcf_index;
928 	int rc;
929 	u32 ulp_status, ulp_word4, tmo;
930 	bool flogi_in_retry = false;
931 
932 	/* Check to see if link went down during discovery */
933 	if (lpfc_els_chk_latt(vport)) {
934 		/* One additional decrement on node reference count to
935 		 * trigger the release of the node.  Make sure the ndlp
936 		 * is marked NLP_DROPPED.
937 		 */
938 		if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag) &&
939 		    !test_bit(NLP_DROPPED, &ndlp->nlp_flag) &&
940 		    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
941 			set_bit(NLP_DROPPED, &ndlp->nlp_flag);
942 			lpfc_nlp_put(ndlp);
943 		}
944 		goto out;
945 	}
946 
947 	ulp_status = get_job_ulpstatus(phba, rspiocb);
948 	ulp_word4 = get_job_word4(phba, rspiocb);
949 
950 	if (phba->sli_rev == LPFC_SLI_REV4) {
951 		tmo = get_wqe_tmo(cmdiocb);
952 	} else {
953 		irsp = &rspiocb->iocb;
954 		tmo = irsp->ulpTimeout;
955 	}
956 
957 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
958 		"FLOGI cmpl:      status:x%x/x%x state:x%x",
959 		ulp_status, ulp_word4,
960 		vport->port_state);
961 
962 	if (ulp_status) {
963 		/*
964 		 * In case of FIP mode, perform roundrobin FCF failover
965 		 * due to new FCF discovery
966 		 */
967 		if (test_bit(HBA_FIP_SUPPORT, &phba->hba_flag) &&
968 		    (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
969 			if (phba->link_state < LPFC_LINK_UP)
970 				goto stop_rr_fcf_flogi;
971 			if ((phba->fcoe_cvl_eventtag_attn ==
972 			     phba->fcoe_cvl_eventtag) &&
973 			    (ulp_status == IOSTAT_LOCAL_REJECT) &&
974 			    ((ulp_word4 & IOERR_PARAM_MASK) ==
975 			    IOERR_SLI_ABORTED))
976 				goto stop_rr_fcf_flogi;
977 			else
978 				phba->fcoe_cvl_eventtag_attn =
979 					phba->fcoe_cvl_eventtag;
980 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
981 					"2611 FLOGI FCF (x%x), "
982 					"status:x%x/x%x, tmo:x%x, perform "
983 					"roundrobin FCF failover\n",
984 					phba->fcf.current_rec.fcf_indx,
985 					ulp_status, ulp_word4, tmo);
986 			lpfc_sli4_set_fcf_flogi_fail(phba,
987 					phba->fcf.current_rec.fcf_indx);
988 			fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
989 			rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
990 			if (rc)
991 				goto out;
992 		}
993 
994 stop_rr_fcf_flogi:
995 		/* FLOGI failure */
996 		if (!(ulp_status == IOSTAT_LOCAL_REJECT &&
997 		      ((ulp_word4 & IOERR_PARAM_MASK) ==
998 					IOERR_LOOP_OPEN_FAILURE)))
999 			lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
1000 				      "2858 FLOGI Status:x%x/x%x TMO"
1001 				      ":x%x Data x%lx x%x x%lx x%x\n",
1002 				      ulp_status, ulp_word4, tmo,
1003 				      phba->hba_flag, phba->fcf.fcf_flag,
1004 				      ndlp->nlp_flag, ndlp->fc4_xpt_flags);
1005 
1006 		/* Check for retry */
1007 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1008 			/* Address a timing race with dev_loss.  If dev_loss
1009 			 * is active on this FPort node, put the initial ref
1010 			 * count back to stop premature node release actions.
1011 			 */
1012 			lpfc_check_nlp_post_devloss(vport, ndlp);
1013 			flogi_in_retry = true;
1014 			goto out;
1015 		}
1016 
1017 		/* The FLOGI will not be retried.  If the FPort node is not
1018 		 * registered with the SCSI transport, remove the initial
1019 		 * reference to trigger node release.
1020 		 */
1021 		if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag) &&
1022 		    !test_bit(NLP_DROPPED, &ndlp->nlp_flag) &&
1023 		    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
1024 			set_bit(NLP_DROPPED, &ndlp->nlp_flag);
1025 			lpfc_nlp_put(ndlp);
1026 		}
1027 
1028 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
1029 				 "0150 FLOGI Status:x%x/x%x "
1030 				 "xri x%x iotag x%x TMO:x%x refcnt %d\n",
1031 				 ulp_status, ulp_word4, cmdiocb->sli4_xritag,
1032 				 cmdiocb->iotag, tmo, kref_read(&ndlp->kref));
1033 
1034 		/* If this is not a loop open failure, bail out */
1035 		if (!(ulp_status == IOSTAT_LOCAL_REJECT &&
1036 		      ((ulp_word4 & IOERR_PARAM_MASK) ==
1037 					IOERR_LOOP_OPEN_FAILURE))) {
1038 			/* Warn FLOGI status */
1039 			lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
1040 				      "0100 FLOGI Status:x%x/x%x "
1041 				      "TMO:x%x\n",
1042 				      ulp_status, ulp_word4, tmo);
1043 			goto flogifail;
1044 		}
1045 
1046 		/* FLOGI failed, so there is no fabric */
1047 		clear_bit(FC_FABRIC, &vport->fc_flag);
1048 		clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
1049 		clear_bit(FC_PT2PT_NO_NVME, &vport->fc_flag);
1050 
1051 		/* If private loop, then allow max outstanding els to be
1052 		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1053 		 * alpa map would take too long otherwise.
1054 		 */
1055 		if (phba->alpa_map[0] == 0)
1056 			vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
1057 		if ((phba->sli_rev == LPFC_SLI_REV4) &&
1058 		    (!test_bit(FC_VFI_REGISTERED, &vport->fc_flag) ||
1059 		     (vport->fc_prevDID != vport->fc_myDID) ||
1060 			phba->fc_topology_changed)) {
1061 			if (test_bit(FC_VFI_REGISTERED, &vport->fc_flag)) {
1062 				if (phba->fc_topology_changed) {
1063 					lpfc_unregister_fcf_prep(phba);
1064 					clear_bit(FC_VFI_REGISTERED,
1065 						  &vport->fc_flag);
1066 					phba->fc_topology_changed = 0;
1067 				} else {
1068 					lpfc_sli4_unreg_all_rpis(vport);
1069 				}
1070 			}
1071 
1072 			/* Do not register VFI if the driver aborted FLOGI */
1073 			if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4))
1074 				lpfc_issue_reg_vfi(vport);
1075 
1076 			goto out;
1077 		}
1078 		goto flogifail;
1079 	}
1080 	clear_bit(FC_VPORT_CVL_RCVD, &vport->fc_flag);
1081 	clear_bit(FC_VPORT_LOGO_RCVD, &vport->fc_flag);
1082 
1083 	/*
1084 	 * The FLOGI succeeded.  Sync the data for the CPU before
1085 	 * accessing it.
1086 	 */
1087 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1088 	if (!prsp)
1089 		goto out;
1090 	if (!lpfc_is_els_acc_rsp(prsp))
1091 		goto out;
1092 	sp = prsp->virt + sizeof(uint32_t);
1093 
1094 	/* FLOGI completes successfully */
1095 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1096 			 "0101 FLOGI completes successfully, I/O tag:x%x "
1097 			 "xri x%x Data: x%x x%x x%x x%x x%x x%lx x%x %d\n",
1098 			 cmdiocb->iotag, cmdiocb->sli4_xritag,
1099 			 ulp_word4, sp->cmn.e_d_tov,
1100 			 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1101 			 vport->port_state, vport->fc_flag,
1102 			 sp->cmn.priority_tagging, kref_read(&ndlp->kref));
1103 
1104 	/* reinitialize the VMID datastructure before returning */
1105 	if (lpfc_is_vmid_enabled(phba)) {
1106 		lpfc_reinit_vmid(vport);
1107 		vport->vmid_flag = 0;
1108 	}
1109 	if (sp->cmn.priority_tagging)
1110 		vport->phba->pport->vmid_flag |= (LPFC_VMID_ISSUE_QFPA |
1111 						  LPFC_VMID_TYPE_PRIO);
1112 
1113 	/*
1114 	 * Address a timing race with dev_loss.  If dev_loss is active on
1115 	 * this FPort node, put the initial ref count back to stop premature
1116 	 * node release actions.
1117 	 */
1118 	lpfc_check_nlp_post_devloss(vport, ndlp);
1119 	if (vport->port_state == LPFC_FLOGI) {
1120 		/*
1121 		 * If Common Service Parameters indicate Nport
1122 		 * we are point to point, if Fport we are Fabric.
1123 		 */
1124 		if (sp->cmn.fPort)
1125 			rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp,
1126 							ulp_word4);
1127 		else if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag))
1128 			rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1129 		else {
1130 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1131 				"2831 FLOGI response with cleared Fabric "
1132 				"bit fcf_index 0x%x "
1133 				"Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1134 				"Fabric Name "
1135 				"%02x%02x%02x%02x%02x%02x%02x%02x\n",
1136 				phba->fcf.current_rec.fcf_indx,
1137 				phba->fcf.current_rec.switch_name[0],
1138 				phba->fcf.current_rec.switch_name[1],
1139 				phba->fcf.current_rec.switch_name[2],
1140 				phba->fcf.current_rec.switch_name[3],
1141 				phba->fcf.current_rec.switch_name[4],
1142 				phba->fcf.current_rec.switch_name[5],
1143 				phba->fcf.current_rec.switch_name[6],
1144 				phba->fcf.current_rec.switch_name[7],
1145 				phba->fcf.current_rec.fabric_name[0],
1146 				phba->fcf.current_rec.fabric_name[1],
1147 				phba->fcf.current_rec.fabric_name[2],
1148 				phba->fcf.current_rec.fabric_name[3],
1149 				phba->fcf.current_rec.fabric_name[4],
1150 				phba->fcf.current_rec.fabric_name[5],
1151 				phba->fcf.current_rec.fabric_name[6],
1152 				phba->fcf.current_rec.fabric_name[7]);
1153 
1154 			lpfc_nlp_put(ndlp);
1155 			spin_lock_irq(&phba->hbalock);
1156 			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1157 			spin_unlock_irq(&phba->hbalock);
1158 			clear_bit(FCF_RR_INPROG, &phba->hba_flag);
1159 			clear_bit(HBA_DEVLOSS_TMO, &phba->hba_flag);
1160 			phba->fcf.fcf_redisc_attempted = 0; /* reset */
1161 			goto out;
1162 		}
1163 		if (!rc) {
1164 			/* Mark the FCF discovery process done */
1165 			if (test_bit(HBA_FIP_SUPPORT, &phba->hba_flag))
1166 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1167 						LOG_ELS,
1168 						"2769 FLOGI to FCF (x%x) "
1169 						"completed successfully\n",
1170 						phba->fcf.current_rec.fcf_indx);
1171 			spin_lock_irq(&phba->hbalock);
1172 			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1173 			spin_unlock_irq(&phba->hbalock);
1174 			clear_bit(FCF_RR_INPROG, &phba->hba_flag);
1175 			clear_bit(HBA_DEVLOSS_TMO, &phba->hba_flag);
1176 			phba->fcf.fcf_redisc_attempted = 0; /* reset */
1177 			goto out;
1178 		}
1179 	} else if (vport->port_state > LPFC_FLOGI &&
1180 		   test_bit(FC_PT2PT, &vport->fc_flag)) {
1181 		/*
1182 		 * In a p2p topology, it is possible that discovery has
1183 		 * already progressed, and this completion can be ignored.
1184 		 * Recheck the indicated topology.
1185 		 */
1186 		if (!sp->cmn.fPort)
1187 			goto out;
1188 	}
1189 
1190 flogifail:
1191 	spin_lock_irq(&phba->hbalock);
1192 	phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1193 	spin_unlock_irq(&phba->hbalock);
1194 
1195 	if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
1196 		/* FLOGI failed, so just use loop map to make discovery list */
1197 		lpfc_disc_list_loopmap(vport);
1198 
1199 		/* Start discovery */
1200 		lpfc_disc_start(vport);
1201 	} else if (((ulp_status != IOSTAT_LOCAL_REJECT) ||
1202 			(((ulp_word4 & IOERR_PARAM_MASK) !=
1203 			 IOERR_SLI_ABORTED) &&
1204 			((ulp_word4 & IOERR_PARAM_MASK) !=
1205 			 IOERR_SLI_DOWN))) &&
1206 			(phba->link_state != LPFC_CLEAR_LA)) {
1207 		/* If FLOGI failed enable link interrupt. */
1208 		lpfc_issue_clear_la(phba, vport);
1209 	}
1210 out:
1211 	if (!flogi_in_retry)
1212 		clear_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag);
1213 
1214 	lpfc_els_free_iocb(phba, cmdiocb);
1215 	lpfc_nlp_put(ndlp);
1216 }
1217 
1218 /**
1219  * lpfc_cmpl_els_link_down - Completion callback function for ELS command
1220  *                           aborted during a link down
1221  * @phba: pointer to lpfc hba data structure.
1222  * @cmdiocb: pointer to lpfc command iocb data structure.
1223  * @rspiocb: pointer to lpfc response iocb data structure.
1224  *
1225  */
1226 static void
1227 lpfc_cmpl_els_link_down(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1228 			struct lpfc_iocbq *rspiocb)
1229 {
1230 	uint32_t *pcmd;
1231 	uint32_t cmd;
1232 	u32 ulp_status, ulp_word4;
1233 
1234 	pcmd = (uint32_t *)cmdiocb->cmd_dmabuf->virt;
1235 	cmd = *pcmd;
1236 
1237 	ulp_status = get_job_ulpstatus(phba, rspiocb);
1238 	ulp_word4 = get_job_word4(phba, rspiocb);
1239 
1240 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1241 			"6445 ELS completes after LINK_DOWN: "
1242 			" Status %x/%x cmd x%x flg x%x iotag x%x\n",
1243 			ulp_status, ulp_word4, cmd,
1244 			cmdiocb->cmd_flag, cmdiocb->iotag);
1245 
1246 	if (cmdiocb->cmd_flag & LPFC_IO_FABRIC) {
1247 		cmdiocb->cmd_flag &= ~LPFC_IO_FABRIC;
1248 		atomic_dec(&phba->fabric_iocb_count);
1249 	}
1250 	lpfc_els_free_iocb(phba, cmdiocb);
1251 }
1252 
1253 /**
1254  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1255  * @vport: pointer to a host virtual N_Port data structure.
1256  * @ndlp: pointer to a node-list data structure.
1257  * @retry: number of retries to the command IOCB.
1258  *
1259  * This routine issues a Fabric Login (FLOGI) Request ELS command
1260  * for a @vport. The initiator service parameters are put into the payload
1261  * of the FLOGI Request IOCB and the top-level callback function pointer
1262  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1263  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1264  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1265  *
1266  * Note that the ndlp reference count will be incremented by 1 for holding the
1267  * ndlp and the reference to ndlp will be stored into the ndlp field of
1268  * the IOCB for the completion callback function to the FLOGI ELS command.
1269  *
1270  * Return code
1271  *   0 - successfully issued flogi iocb for @vport
1272  *   1 - failed to issue flogi iocb for @vport
1273  **/
1274 static int
1275 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1276 		     uint8_t retry)
1277 {
1278 	struct lpfc_hba  *phba = vport->phba;
1279 	struct serv_parm *sp;
1280 	union lpfc_wqe128 *wqe = NULL;
1281 	IOCB_t *icmd = NULL;
1282 	struct lpfc_iocbq *elsiocb;
1283 	struct lpfc_iocbq defer_flogi_acc;
1284 	u8 *pcmd, ct;
1285 	uint16_t cmdsize;
1286 	uint32_t tmo, did;
1287 	int rc;
1288 
1289 	/* It's possible for lpfc to reissue a FLOGI on an ndlp that is marked
1290 	 * NLP_DROPPED.  This happens when the FLOGI completed with the XB bit
1291 	 * set causing lpfc to reference the ndlp until the XRI_ABORTED CQE is
1292 	 * issued. The time window for the XRI_ABORTED CQE can be as much as
1293 	 * 2*2*RA_TOV allowing for ndlp reuse of this type when the link is
1294 	 * cycling quickly.  When true, restore the initial reference and remove
1295 	 * the NLP_DROPPED flag as lpfc is retrying.
1296 	 */
1297 	if (test_and_clear_bit(NLP_DROPPED, &ndlp->nlp_flag)) {
1298 		if (!lpfc_nlp_get(ndlp))
1299 			return 1;
1300 	}
1301 
1302 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1303 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1304 				     ndlp->nlp_DID, ELS_CMD_FLOGI);
1305 
1306 	if (!elsiocb)
1307 		return 1;
1308 
1309 	wqe = &elsiocb->wqe;
1310 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
1311 	icmd = &elsiocb->iocb;
1312 
1313 	/* For FLOGI request, remainder of payload is service parameters */
1314 	*((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1315 	pcmd += sizeof(uint32_t);
1316 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1317 	sp = (struct serv_parm *) pcmd;
1318 
1319 	/* Setup CSPs accordingly for Fabric */
1320 	sp->cmn.e_d_tov = 0;
1321 	sp->cmn.w2.r_a_tov = 0;
1322 	sp->cmn.virtual_fabric_support = 0;
1323 	sp->cls1.classValid = 0;
1324 	if (sp->cmn.fcphLow < FC_PH3)
1325 		sp->cmn.fcphLow = FC_PH3;
1326 	if (sp->cmn.fcphHigh < FC_PH3)
1327 		sp->cmn.fcphHigh = FC_PH3;
1328 
1329 	/* Determine if switch supports priority tagging */
1330 	if (phba->cfg_vmid_priority_tagging) {
1331 		sp->cmn.priority_tagging = 1;
1332 		/* lpfc_vmid_host_uuid is combination of wwpn and wwnn */
1333 		if (!memchr_inv(vport->lpfc_vmid_host_uuid, 0,
1334 				sizeof(vport->lpfc_vmid_host_uuid))) {
1335 			memcpy(vport->lpfc_vmid_host_uuid, phba->wwpn,
1336 			       sizeof(phba->wwpn));
1337 			memcpy(&vport->lpfc_vmid_host_uuid[8], phba->wwnn,
1338 			       sizeof(phba->wwnn));
1339 		}
1340 	}
1341 
1342 	if  (phba->sli_rev == LPFC_SLI_REV4) {
1343 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1344 		    LPFC_SLI_INTF_IF_TYPE_0) {
1345 			/* FLOGI needs to be 3 for WQE FCFI */
1346 			ct = SLI4_CT_FCFI;
1347 			bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
1348 
1349 			/* Set the fcfi to the fcfi we registered with */
1350 			bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
1351 			       phba->fcf.fcfi);
1352 		}
1353 
1354 		/* Can't do SLI4 class2 without support sequence coalescing */
1355 		sp->cls2.classValid = 0;
1356 		sp->cls2.seqDelivery = 0;
1357 
1358 		/* Fill out Auxiliary Parameter Data */
1359 		if (phba->pni) {
1360 			sp->aux.flags =
1361 				AUX_PARM_DATA_VALID | AUX_PARM_PNI_VALID;
1362 			sp->aux.pni = cpu_to_be64(phba->pni);
1363 			sp->aux.npiv_cnt = cpu_to_be16(phba->max_vpi - 1);
1364 		}
1365 	} else {
1366 		/* Historical, setting sequential-delivery bit for SLI3 */
1367 		sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1368 		sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1369 		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1370 			sp->cmn.request_multiple_Nport = 1;
1371 			/* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1372 			icmd->ulpCt_h = 1;
1373 			icmd->ulpCt_l = 0;
1374 		} else {
1375 			sp->cmn.request_multiple_Nport = 0;
1376 		}
1377 
1378 		if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1379 			icmd->un.elsreq64.myID = 0;
1380 			icmd->un.elsreq64.fl = 1;
1381 		}
1382 	}
1383 
1384 	tmo = phba->fc_ratov;
1385 	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1386 	lpfc_set_disctmo(vport);
1387 	phba->fc_ratov = tmo;
1388 
1389 	phba->fc_stat.elsXmitFLOGI++;
1390 	elsiocb->cmd_cmpl = lpfc_cmpl_els_flogi;
1391 
1392 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1393 		"Issue FLOGI:     opt:x%x",
1394 		phba->sli3_options, 0, 0);
1395 
1396 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
1397 	if (!elsiocb->ndlp) {
1398 		lpfc_els_free_iocb(phba, elsiocb);
1399 		return 1;
1400 	}
1401 
1402 	/* Avoid race with FLOGI completion and hba_flags. */
1403 	set_bit(HBA_FLOGI_ISSUED, &phba->hba_flag);
1404 	set_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag);
1405 
1406 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1407 	if (rc == IOCB_ERROR) {
1408 		clear_bit(HBA_FLOGI_ISSUED, &phba->hba_flag);
1409 		clear_bit(HBA_FLOGI_OUTSTANDING, &phba->hba_flag);
1410 		lpfc_els_free_iocb(phba, elsiocb);
1411 		lpfc_nlp_put(ndlp);
1412 		return 1;
1413 	}
1414 
1415 	/* Clear external loopback plug detected flag */
1416 	phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
1417 
1418 	/* Check for a deferred FLOGI ACC condition */
1419 	if (phba->defer_flogi_acc.flag) {
1420 		/* lookup ndlp for received FLOGI */
1421 		ndlp = lpfc_findnode_did(vport, 0);
1422 		if (!ndlp)
1423 			return 0;
1424 
1425 		did = vport->fc_myDID;
1426 		vport->fc_myDID = Fabric_DID;
1427 
1428 		memset(&defer_flogi_acc, 0, sizeof(struct lpfc_iocbq));
1429 
1430 		if (phba->sli_rev == LPFC_SLI_REV4) {
1431 			bf_set(wqe_ctxt_tag,
1432 			       &defer_flogi_acc.wqe.xmit_els_rsp.wqe_com,
1433 			       phba->defer_flogi_acc.rx_id);
1434 			bf_set(wqe_rcvoxid,
1435 			       &defer_flogi_acc.wqe.xmit_els_rsp.wqe_com,
1436 			       phba->defer_flogi_acc.ox_id);
1437 		} else {
1438 			icmd = &defer_flogi_acc.iocb;
1439 			icmd->ulpContext = phba->defer_flogi_acc.rx_id;
1440 			icmd->unsli3.rcvsli3.ox_id =
1441 				phba->defer_flogi_acc.ox_id;
1442 		}
1443 
1444 		/* The LS_ACC completion needs to drop the initial reference.
1445 		 * This is a special case for Pt2Pt because both FLOGIs need
1446 		 * to complete and lpfc defers the LS_ACC when the remote
1447 		 * FLOGI arrives before the driver's FLOGI.
1448 		 */
1449 		set_bit(NLP_FLOGI_DFR_ACC, &ndlp->nlp_flag);
1450 
1451 		/* Send deferred FLOGI ACC */
1452 		lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, &defer_flogi_acc,
1453 				 ndlp, NULL);
1454 
1455 		phba->defer_flogi_acc.flag = false;
1456 
1457 		/* Decrement the held ndlp that was incremented when the
1458 		 * deferred flogi acc flag was set.
1459 		 */
1460 		if (phba->defer_flogi_acc.ndlp) {
1461 			lpfc_nlp_put(phba->defer_flogi_acc.ndlp);
1462 			phba->defer_flogi_acc.ndlp = NULL;
1463 		}
1464 
1465 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1466 				 "3354 Xmit deferred FLOGI ACC: rx_id: x%x,"
1467 				 " ox_id: x%x, ndlp x%px hba_flag x%lx\n",
1468 				 phba->defer_flogi_acc.rx_id,
1469 				 phba->defer_flogi_acc.ox_id,
1470 				 phba->defer_flogi_acc.ndlp,
1471 				 phba->hba_flag);
1472 
1473 		vport->fc_myDID = did;
1474 	}
1475 
1476 	return 0;
1477 }
1478 
1479 /**
1480  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1481  * @phba: pointer to lpfc hba data structure.
1482  *
1483  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1484  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1485  * list and issues an abort IOCB commond on each outstanding IOCB that
1486  * contains a active Fabric_DID ndlp. Note that this function is to issue
1487  * the abort IOCB command on all the outstanding IOCBs, thus when this
1488  * function returns, it does not guarantee all the IOCBs are actually aborted.
1489  *
1490  * Return code
1491  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1492  **/
1493 int
1494 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1495 {
1496 	struct lpfc_sli_ring *pring;
1497 	struct lpfc_iocbq *iocb, *next_iocb;
1498 	struct lpfc_nodelist *ndlp;
1499 	u32 ulp_command;
1500 
1501 	/* Abort outstanding I/O on NPort <nlp_DID> */
1502 	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1503 			"0201 Abort outstanding I/O on NPort x%x\n",
1504 			Fabric_DID);
1505 
1506 	pring = lpfc_phba_elsring(phba);
1507 	if (unlikely(!pring))
1508 		return -EIO;
1509 
1510 	/*
1511 	 * Check the txcmplq for an iocb that matches the nport the driver is
1512 	 * searching for.
1513 	 */
1514 	spin_lock_irq(&phba->hbalock);
1515 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1516 		ulp_command = get_job_cmnd(phba, iocb);
1517 		if (ulp_command == CMD_ELS_REQUEST64_CR) {
1518 			ndlp = iocb->ndlp;
1519 			if (ndlp && ndlp->nlp_DID == Fabric_DID) {
1520 				if (test_bit(FC_PT2PT, &phba->pport->fc_flag) &&
1521 				    !test_bit(FC_PT2PT_PLOGI,
1522 					      &phba->pport->fc_flag))
1523 					iocb->fabric_cmd_cmpl =
1524 						lpfc_ignore_els_cmpl;
1525 				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
1526 							   NULL);
1527 			}
1528 		}
1529 	}
1530 	/* Make sure HBA is alive */
1531 	lpfc_issue_hb_tmo(phba);
1532 
1533 	spin_unlock_irq(&phba->hbalock);
1534 
1535 	return 0;
1536 }
1537 
1538 /**
1539  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1540  * @vport: pointer to a host virtual N_Port data structure.
1541  *
1542  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1543  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1544  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1545  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1546  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1547  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1548  * @vport.
1549  *
1550  * Return code
1551  *   0 - failed to issue initial flogi for @vport
1552  *   1 - successfully issued initial flogi for @vport
1553  **/
1554 int
1555 lpfc_initial_flogi(struct lpfc_vport *vport)
1556 {
1557 	struct lpfc_nodelist *ndlp;
1558 
1559 	vport->port_state = LPFC_FLOGI;
1560 	lpfc_set_disctmo(vport);
1561 
1562 	/* First look for the Fabric ndlp */
1563 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
1564 	if (!ndlp) {
1565 		/* Cannot find existing Fabric ndlp, so allocate a new one */
1566 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
1567 		if (!ndlp)
1568 			return 0;
1569 		/* Set the node type */
1570 		ndlp->nlp_type |= NLP_FABRIC;
1571 
1572 		/* Put ndlp onto node list */
1573 		lpfc_enqueue_node(vport, ndlp);
1574 	}
1575 
1576 	/* Reset the Fabric flag, topology change may have happened */
1577 	clear_bit(FC_FABRIC, &vport->fc_flag);
1578 	if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1579 		/* A node reference should be retained while registered with a
1580 		 * transport or dev-loss-evt work is pending.
1581 		 * Otherwise, decrement node reference to trigger release.
1582 		 */
1583 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
1584 		    !test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
1585 			lpfc_nlp_put(ndlp);
1586 		return 0;
1587 	}
1588 	return 1;
1589 }
1590 
1591 /**
1592  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1593  * @vport: pointer to a host virtual N_Port data structure.
1594  *
1595  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1596  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1597  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1598  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1599  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1600  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1601  * @vport.
1602  *
1603  * Return code
1604  *   0 - failed to issue initial fdisc for @vport
1605  *   1 - successfully issued initial fdisc for @vport
1606  **/
1607 int
1608 lpfc_initial_fdisc(struct lpfc_vport *vport)
1609 {
1610 	struct lpfc_nodelist *ndlp;
1611 
1612 	/* First look for the Fabric ndlp */
1613 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
1614 	if (!ndlp) {
1615 		/* Cannot find existing Fabric ndlp, so allocate a new one */
1616 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
1617 		if (!ndlp)
1618 			return 0;
1619 
1620 		/* NPIV is only supported in Fabrics. */
1621 		ndlp->nlp_type |= NLP_FABRIC;
1622 
1623 		/* Put ndlp onto node list */
1624 		lpfc_enqueue_node(vport, ndlp);
1625 	}
1626 
1627 	if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1628 		/* A node reference should be retained while registered with a
1629 		 * transport or dev-loss-evt work is pending.
1630 		 * Otherwise, decrement node reference to trigger release.
1631 		 */
1632 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
1633 		    !test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
1634 			lpfc_nlp_put(ndlp);
1635 		return 0;
1636 	}
1637 	return 1;
1638 }
1639 
1640 /**
1641  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1642  * @vport: pointer to a host virtual N_Port data structure.
1643  *
1644  * This routine checks whether there are more remaining Port Logins
1645  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1646  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1647  * to issue ELS PLOGIs up to the configured discover threads with the
1648  * @vport (@vport->cfg_discovery_threads). The function also decrement
1649  * the @vport's num_disc_node by 1 if it is not already 0.
1650  **/
1651 void
1652 lpfc_more_plogi(struct lpfc_vport *vport)
1653 {
1654 	if (vport->num_disc_nodes)
1655 		vport->num_disc_nodes--;
1656 
1657 	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
1658 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1659 			 "0232 Continue discovery with %d PLOGIs to go "
1660 			 "Data: x%x x%lx x%x\n",
1661 			 vport->num_disc_nodes,
1662 			 atomic_read(&vport->fc_plogi_cnt),
1663 			 vport->fc_flag, vport->port_state);
1664 	/* Check to see if there are more PLOGIs to be sent */
1665 	if (test_bit(FC_NLP_MORE, &vport->fc_flag))
1666 		/* go thru NPR nodes and issue any remaining ELS PLOGIs */
1667 		lpfc_els_disc_plogi(vport);
1668 
1669 	return;
1670 }
1671 
1672 /**
1673  * lpfc_plogi_confirm_nport - Confirm plogi wwpn matches stored ndlp
1674  * @phba: pointer to lpfc hba data structure.
1675  * @prsp: pointer to response IOCB payload.
1676  * @ndlp: pointer to a node-list data structure.
1677  *
1678  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1679  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1680  * The following cases are considered N_Port confirmed:
1681  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1682  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1683  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1684  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1685  * 1) if there is a node on vport list other than the @ndlp with the same
1686  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1687  * on that node to release the RPI associated with the node; 2) if there is
1688  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1689  * into, a new node shall be allocated (or activated). In either case, the
1690  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1691  * be released and the new_ndlp shall be put on to the vport node list and
1692  * its pointer returned as the confirmed node.
1693  *
1694  * Note that before the @ndlp got "released", the keepDID from not-matching
1695  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1696  * of the @ndlp. This is because the release of @ndlp is actually to put it
1697  * into an inactive state on the vport node list and the vport node list
1698  * management algorithm does not allow two node with a same DID.
1699  *
1700  * Return code
1701  *   pointer to the PLOGI N_Port @ndlp
1702  **/
1703 static struct lpfc_nodelist *
1704 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1705 			 struct lpfc_nodelist *ndlp)
1706 {
1707 	struct lpfc_vport *vport = ndlp->vport;
1708 	struct lpfc_nodelist *new_ndlp;
1709 	struct serv_parm *sp;
1710 	uint8_t  name[sizeof(struct lpfc_name)];
1711 	uint32_t keepDID = 0;
1712 	int rc;
1713 	unsigned long keep_nlp_flag = 0, keep_new_nlp_flag = 0;
1714 	uint16_t keep_nlp_state;
1715 	u32 keep_nlp_fc4_type = 0;
1716 	struct lpfc_nvme_rport *keep_nrport = NULL;
1717 	unsigned long *active_rrqs_xri_bitmap = NULL;
1718 
1719 	sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1720 	memset(name, 0, sizeof(struct lpfc_name));
1721 
1722 	/* Now we find out if the NPort we are logging into, matches the WWPN
1723 	 * we have for that ndlp. If not, we have some work to do.
1724 	 */
1725 	new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1726 
1727 	/* return immediately if the WWPN matches ndlp */
1728 	if (new_ndlp == ndlp)
1729 		return ndlp;
1730 
1731 	if (phba->sli_rev == LPFC_SLI_REV4) {
1732 		active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1733 						       GFP_KERNEL);
1734 		if (active_rrqs_xri_bitmap)
1735 			memset(active_rrqs_xri_bitmap, 0,
1736 			       phba->cfg_rrq_xri_bitmap_sz);
1737 	}
1738 
1739 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1740 			 "3178 PLOGI confirm: ndlp x%x x%lx x%x: "
1741 			 "new_ndlp x%x x%lx x%x\n",
1742 			 ndlp->nlp_DID, ndlp->nlp_flag,  ndlp->nlp_fc4_type,
1743 			 (new_ndlp ? new_ndlp->nlp_DID : 0),
1744 			 (new_ndlp ? new_ndlp->nlp_flag : 0),
1745 			 (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
1746 
1747 	if (!new_ndlp) {
1748 		rc = memcmp(&ndlp->nlp_portname, name,
1749 			    sizeof(struct lpfc_name));
1750 		if (!rc) {
1751 			if (active_rrqs_xri_bitmap)
1752 				mempool_free(active_rrqs_xri_bitmap,
1753 					     phba->active_rrq_pool);
1754 			return ndlp;
1755 		}
1756 		new_ndlp = lpfc_nlp_init(vport, ndlp->nlp_DID);
1757 		if (!new_ndlp) {
1758 			if (active_rrqs_xri_bitmap)
1759 				mempool_free(active_rrqs_xri_bitmap,
1760 					     phba->active_rrq_pool);
1761 			return ndlp;
1762 		}
1763 	} else {
1764 		if (phba->sli_rev == LPFC_SLI_REV4 &&
1765 		    active_rrqs_xri_bitmap)
1766 			memcpy(active_rrqs_xri_bitmap,
1767 			       new_ndlp->active_rrqs_xri_bitmap,
1768 			       phba->cfg_rrq_xri_bitmap_sz);
1769 
1770 		/*
1771 		 * Unregister from backend if not done yet. Could have been
1772 		 * skipped due to ADISC
1773 		 */
1774 		lpfc_nlp_unreg_node(vport, new_ndlp);
1775 	}
1776 
1777 	keepDID = new_ndlp->nlp_DID;
1778 
1779 	/* At this point in this routine, we know new_ndlp will be
1780 	 * returned. however, any previous GID_FTs that were done
1781 	 * would have updated nlp_fc4_type in ndlp, so we must ensure
1782 	 * new_ndlp has the right value.
1783 	 */
1784 	if (test_bit(FC_FABRIC, &vport->fc_flag)) {
1785 		keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
1786 		new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
1787 	}
1788 
1789 	lpfc_unreg_rpi(vport, new_ndlp);
1790 	new_ndlp->nlp_DID = ndlp->nlp_DID;
1791 	new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1792 	if (phba->sli_rev == LPFC_SLI_REV4)
1793 		memcpy(new_ndlp->active_rrqs_xri_bitmap,
1794 		       ndlp->active_rrqs_xri_bitmap,
1795 		       phba->cfg_rrq_xri_bitmap_sz);
1796 
1797 	/* Lock both ndlps */
1798 	spin_lock_irq(&ndlp->lock);
1799 	spin_lock_irq(&new_ndlp->lock);
1800 	keep_new_nlp_flag = new_ndlp->nlp_flag;
1801 	keep_nlp_flag = ndlp->nlp_flag;
1802 	new_ndlp->nlp_flag = ndlp->nlp_flag;
1803 
1804 	/* if new_ndlp had NLP_UNREG_INP set, keep it */
1805 	if (test_bit(NLP_UNREG_INP, &keep_new_nlp_flag))
1806 		set_bit(NLP_UNREG_INP, &new_ndlp->nlp_flag);
1807 	else
1808 		clear_bit(NLP_UNREG_INP, &new_ndlp->nlp_flag);
1809 
1810 	/* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
1811 	if (test_bit(NLP_RPI_REGISTERED, &keep_new_nlp_flag))
1812 		set_bit(NLP_RPI_REGISTERED, &new_ndlp->nlp_flag);
1813 	else
1814 		clear_bit(NLP_RPI_REGISTERED, &new_ndlp->nlp_flag);
1815 
1816 	/*
1817 	 * Retain the DROPPED flag. This will take care of the init
1818 	 * refcount when affecting the state change
1819 	 */
1820 	if (test_bit(NLP_DROPPED, &keep_new_nlp_flag))
1821 		set_bit(NLP_DROPPED, &new_ndlp->nlp_flag);
1822 	else
1823 		clear_bit(NLP_DROPPED, &new_ndlp->nlp_flag);
1824 
1825 	ndlp->nlp_flag = keep_new_nlp_flag;
1826 
1827 	/* if ndlp had NLP_UNREG_INP set, keep it */
1828 	if (test_bit(NLP_UNREG_INP, &keep_nlp_flag))
1829 		set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
1830 	else
1831 		clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
1832 
1833 	/* if ndlp had NLP_RPI_REGISTERED set, keep it */
1834 	if (test_bit(NLP_RPI_REGISTERED, &keep_nlp_flag))
1835 		set_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
1836 	else
1837 		clear_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
1838 
1839 	/*
1840 	 * Retain the DROPPED flag. This will take care of the init
1841 	 * refcount when affecting the state change
1842 	 */
1843 	if (test_bit(NLP_DROPPED, &keep_nlp_flag))
1844 		set_bit(NLP_DROPPED, &ndlp->nlp_flag);
1845 	else
1846 		clear_bit(NLP_DROPPED, &ndlp->nlp_flag);
1847 
1848 	spin_unlock_irq(&new_ndlp->lock);
1849 	spin_unlock_irq(&ndlp->lock);
1850 
1851 	/* Set nlp_states accordingly */
1852 	keep_nlp_state = new_ndlp->nlp_state;
1853 	lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1854 
1855 	/* interchange the nvme remoteport structs */
1856 	keep_nrport = new_ndlp->nrport;
1857 	new_ndlp->nrport = ndlp->nrport;
1858 
1859 	/* Move this back to NPR state */
1860 	if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1861 		/* The ndlp doesn't have a portname yet, but does have an
1862 		 * NPort ID.  The new_ndlp portname matches the Rport's
1863 		 * portname.  Reinstantiate the new_ndlp and reset the ndlp.
1864 		 */
1865 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1866 			 "3179 PLOGI confirm NEW: %x %x\n",
1867 			 new_ndlp->nlp_DID, keepDID);
1868 
1869 		/* Two ndlps cannot have the same did on the nodelist.
1870 		 * The KeepDID and keep_nlp_fc4_type need to be swapped
1871 		 * because ndlp is inflight with no WWPN.
1872 		 */
1873 		ndlp->nlp_DID = keepDID;
1874 		ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1875 		lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1876 		if (phba->sli_rev == LPFC_SLI_REV4 &&
1877 		    active_rrqs_xri_bitmap)
1878 			memcpy(ndlp->active_rrqs_xri_bitmap,
1879 			       active_rrqs_xri_bitmap,
1880 			       phba->cfg_rrq_xri_bitmap_sz);
1881 
1882 	} else {
1883 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1884 			 "3180 PLOGI confirm SWAP: %x %x\n",
1885 			 new_ndlp->nlp_DID, keepDID);
1886 
1887 		lpfc_unreg_rpi(vport, ndlp);
1888 
1889 		/* The ndlp and new_ndlp both have WWPNs but are swapping
1890 		 * NPort Ids and attributes.
1891 		 */
1892 		ndlp->nlp_DID = keepDID;
1893 		ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1894 
1895 		if (phba->sli_rev == LPFC_SLI_REV4 &&
1896 		    active_rrqs_xri_bitmap)
1897 			memcpy(ndlp->active_rrqs_xri_bitmap,
1898 			       active_rrqs_xri_bitmap,
1899 			       phba->cfg_rrq_xri_bitmap_sz);
1900 
1901 		/* Since we are switching over to the new_ndlp,
1902 		 * reset the old ndlp state
1903 		 */
1904 		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1905 		    (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1906 			keep_nlp_state = NLP_STE_NPR_NODE;
1907 		lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1908 		ndlp->nrport = keep_nrport;
1909 	}
1910 
1911 	/*
1912 	 * If ndlp is not associated with any rport we can drop it here else
1913 	 * let dev_loss_tmo_callbk trigger DEVICE_RM event
1914 	 */
1915 	if (!ndlp->rport && (ndlp->nlp_state == NLP_STE_NPR_NODE))
1916 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
1917 
1918 	if (phba->sli_rev == LPFC_SLI_REV4 &&
1919 	    active_rrqs_xri_bitmap)
1920 		mempool_free(active_rrqs_xri_bitmap,
1921 			     phba->active_rrq_pool);
1922 
1923 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1924 			 "3173 PLOGI confirm exit: new_ndlp x%x x%lx x%x\n",
1925 			 new_ndlp->nlp_DID, new_ndlp->nlp_flag,
1926 			 new_ndlp->nlp_fc4_type);
1927 
1928 	return new_ndlp;
1929 }
1930 
1931 /**
1932  * lpfc_end_rscn - Check and handle more rscn for a vport
1933  * @vport: pointer to a host virtual N_Port data structure.
1934  *
1935  * This routine checks whether more Registration State Change
1936  * Notifications (RSCNs) came in while the discovery state machine was in
1937  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1938  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1939  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1940  * handling the RSCNs.
1941  **/
1942 void
1943 lpfc_end_rscn(struct lpfc_vport *vport)
1944 {
1945 
1946 	if (test_bit(FC_RSCN_MODE, &vport->fc_flag)) {
1947 		/*
1948 		 * Check to see if more RSCNs came in while we were
1949 		 * processing this one.
1950 		 */
1951 		if (vport->fc_rscn_id_cnt ||
1952 		    test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag))
1953 			lpfc_els_handle_rscn(vport);
1954 		else
1955 			clear_bit(FC_RSCN_MODE, &vport->fc_flag);
1956 	}
1957 }
1958 
1959 /**
1960  * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1961  * @phba: pointer to lpfc hba data structure.
1962  * @cmdiocb: pointer to lpfc command iocb data structure.
1963  * @rspiocb: pointer to lpfc response iocb data structure.
1964  *
1965  * This routine will call the clear rrq function to free the rrq and
1966  * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1967  * exist then the clear_rrq is still called because the rrq needs to
1968  * be freed.
1969  **/
1970 
1971 static void
1972 lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1973 		  struct lpfc_iocbq *rspiocb)
1974 {
1975 	struct lpfc_vport *vport = cmdiocb->vport;
1976 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
1977 	struct lpfc_node_rrq *rrq;
1978 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
1979 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
1980 
1981 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1982 	rrq = cmdiocb->context_un.rrq;
1983 	cmdiocb->rsp_iocb = rspiocb;
1984 
1985 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1986 		"RRQ cmpl:      status:x%x/x%x did:x%x",
1987 		ulp_status, ulp_word4,
1988 		get_job_els_rsp64_did(phba, cmdiocb));
1989 
1990 
1991 	/* rrq completes to NPort <nlp_DID> */
1992 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1993 			 "2880 RRQ completes to DID x%x "
1994 			 "Data: x%x x%x x%x x%x x%x\n",
1995 			 ndlp->nlp_DID, ulp_status, ulp_word4,
1996 			 get_wqe_tmo(cmdiocb), rrq->xritag, rrq->rxid);
1997 
1998 	if (ulp_status) {
1999 		/* Check for retry */
2000 		/* Warn RRQ status Don't print the vport to vport rjts */
2001 		if (ulp_status != IOSTAT_LS_RJT ||
2002 		    (((ulp_word4) >> 16 != LSRJT_INVALID_CMD) &&
2003 		     ((ulp_word4) >> 16 != LSRJT_UNABLE_TPC)) ||
2004 		    (phba)->pport->cfg_log_verbose & LOG_ELS)
2005 			lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
2006 				      "2881 RRQ DID:%06X Status:"
2007 				      "x%x/x%x\n",
2008 				      ndlp->nlp_DID, ulp_status,
2009 				      ulp_word4);
2010 	}
2011 
2012 	lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
2013 	lpfc_els_free_iocb(phba, cmdiocb);
2014 	lpfc_nlp_put(ndlp);
2015 	return;
2016 }
2017 
2018 /**
2019  * lpfc_check_encryption - Reports an ndlp's encryption information
2020  * @phba: pointer to lpfc hba data structure.
2021  * @ndlp: pointer to a node-list data structure.
2022  * @cmdiocb: pointer to lpfc command iocbq data structure.
2023  * @rspiocb: pointer to lpfc response iocbq data structure.
2024  *
2025  * This routine is called in the completion callback function for issuing
2026  * or receiving a Port Login (PLOGI) command. In a PLOGI completion, if FEDIF
2027  * is supported, encryption information will be provided in completion status
2028  * data. If @phba supports FEDIF, a log message containing encryption
2029  * information will be logged. Encryption status is also saved for encryption
2030  * reporting with upper layer through the rport encryption attribute.
2031  **/
2032 static void
2033 lpfc_check_encryption(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
2034 		      struct lpfc_iocbq *cmdiocb, struct lpfc_iocbq *rspiocb)
2035 {
2036 	struct lpfc_vport *vport = cmdiocb->vport;
2037 	u32 did = ndlp->nlp_DID;
2038 	struct lpfc_enc_info *nlp_enc_info = &ndlp->nlp_enc_info;
2039 	char enc_status[FC_RPORT_ENCRYPTION_STATUS_MAX_LEN] = {0};
2040 	char enc_level[8] = "N/A";
2041 	u8 encryption;
2042 
2043 	if (phba->sli4_hba.encryption_support &&
2044 	    ((did & Fabric_DID_MASK) != Fabric_DID_MASK)) {
2045 		encryption = bf_get(lpfc_wcqe_c_enc,
2046 				    &rspiocb->wcqe_cmpl);
2047 		nlp_enc_info->status = encryption;
2048 
2049 		strscpy(enc_status, encryption ? "Encrypted" : "Unencrypted",
2050 			sizeof(enc_status));
2051 
2052 		if (encryption) {
2053 			nlp_enc_info->level = bf_get(lpfc_wcqe_c_enc_lvl,
2054 						     &rspiocb->wcqe_cmpl);
2055 			strscpy(enc_level, nlp_enc_info->level ? "CNSA2.0" :
2056 								 "CNSA1.0",
2057 				sizeof(enc_level));
2058 		}
2059 
2060 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ENCRYPTION,
2061 				 "0924 DID:x%06x %s Session "
2062 				 "Established, Encryption Level:%s "
2063 				 "rpi:x%x\n",
2064 				 ndlp->nlp_DID, enc_status, enc_level,
2065 				 ndlp->nlp_rpi);
2066 	}
2067 }
2068 
2069 /**
2070  * lpfc_cmpl_els_plogi - Completion callback function for plogi
2071  * @phba: pointer to lpfc hba data structure.
2072  * @cmdiocb: pointer to lpfc command iocb data structure.
2073  * @rspiocb: pointer to lpfc response iocb data structure.
2074  *
2075  * This routine is the completion callback function for issuing the Port
2076  * Login (PLOGI) command. For PLOGI completion, there must be an active
2077  * ndlp on the vport node list that matches the remote node ID from the
2078  * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
2079  * ignored and command IOCB released. The PLOGI response IOCB status is
2080  * checked for error conditions. If there is error status reported, PLOGI
2081  * retry shall be attempted by invoking the lpfc_els_retry() routine.
2082  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
2083  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
2084  * (DSM) is set for this PLOGI completion. Finally, it checks whether
2085  * there are additional N_Port nodes with the vport that need to perform
2086  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
2087  * PLOGIs.
2088  **/
2089 static void
2090 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2091 		    struct lpfc_iocbq *rspiocb)
2092 {
2093 	struct lpfc_vport *vport = cmdiocb->vport;
2094 	IOCB_t *irsp;
2095 	struct lpfc_nodelist *ndlp, *free_ndlp;
2096 	struct lpfc_dmabuf *prsp;
2097 	bool disc;
2098 	struct serv_parm *sp = NULL;
2099 	u32 ulp_status, ulp_word4, did, iotag;
2100 	bool release_node = false;
2101 
2102 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2103 	cmdiocb->rsp_iocb = rspiocb;
2104 
2105 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2106 	ulp_word4 = get_job_word4(phba, rspiocb);
2107 	did = get_job_els_rsp64_did(phba, cmdiocb);
2108 
2109 	if (phba->sli_rev == LPFC_SLI_REV4) {
2110 		iotag = get_wqe_reqtag(cmdiocb);
2111 	} else {
2112 		irsp = &rspiocb->iocb;
2113 		iotag = irsp->ulpIoTag;
2114 	}
2115 
2116 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2117 		"PLOGI cmpl:      status:x%x/x%x did:x%x",
2118 		ulp_status, ulp_word4, did);
2119 
2120 	ndlp = lpfc_findnode_did(vport, did);
2121 	if (!ndlp) {
2122 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2123 				 "0136 PLOGI completes to NPort x%x "
2124 				 "with no ndlp. Data: x%x x%x x%x\n",
2125 				 did, ulp_status, ulp_word4, iotag);
2126 		goto out_freeiocb;
2127 	}
2128 
2129 	/* Since ndlp can be freed in the disc state machine, note if this node
2130 	 * is being used during discovery.
2131 	 */
2132 	disc = test_and_clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2133 
2134 	/* PLOGI completes to NPort <nlp_DID> */
2135 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2136 			 "0102 PLOGI completes to NPort x%06x "
2137 			 "IoTag x%x Data: x%x x%x x%x x%x x%x\n",
2138 			 ndlp->nlp_DID, iotag,
2139 			 ndlp->nlp_fc4_type,
2140 			 ulp_status, ulp_word4,
2141 			 disc, vport->num_disc_nodes);
2142 
2143 	/* Check to see if link went down during discovery */
2144 	if (lpfc_els_chk_latt(vport)) {
2145 		set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2146 		goto out;
2147 	}
2148 
2149 	if (ulp_status) {
2150 		/* Check for retry */
2151 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2152 			/* ELS command is being retried */
2153 			if (disc)
2154 				set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2155 			goto out;
2156 		}
2157 		/* Warn PLOGI status Don't print the vport to vport rjts */
2158 		if (ulp_status != IOSTAT_LS_RJT ||
2159 		    (((ulp_word4) >> 16 != LSRJT_INVALID_CMD) &&
2160 		     ((ulp_word4) >> 16 != LSRJT_UNABLE_TPC)) ||
2161 		    (phba)->pport->cfg_log_verbose & LOG_ELS)
2162 			lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
2163 				      "2753 PLOGI DID:%06X "
2164 				      "Status:x%x/x%x\n",
2165 				      ndlp->nlp_DID, ulp_status,
2166 				      ulp_word4);
2167 
2168 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2169 		if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4))
2170 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2171 						NLP_EVT_CMPL_PLOGI);
2172 
2173 		/* If a PLOGI collision occurred, the node needs to continue
2174 		 * with the reglogin process.
2175 		 */
2176 		spin_lock_irq(&ndlp->lock);
2177 		if ((test_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag) ||
2178 		     test_bit(NLP_RCV_PLOGI, &ndlp->nlp_flag)) &&
2179 		    ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE) {
2180 			spin_unlock_irq(&ndlp->lock);
2181 			goto out;
2182 		}
2183 
2184 		/* No PLOGI collision and the node is not registered with the
2185 		 * scsi or nvme transport. It is no longer an active node. Just
2186 		 * start the device remove process.
2187 		 */
2188 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2189 			clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2190 			if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
2191 				release_node = true;
2192 		}
2193 		spin_unlock_irq(&ndlp->lock);
2194 
2195 		if (release_node)
2196 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2197 						NLP_EVT_DEVICE_RM);
2198 	} else {
2199 		/* Good status, call state machine */
2200 		prsp = list_get_first(&cmdiocb->cmd_dmabuf->list,
2201 				      struct lpfc_dmabuf, list);
2202 		if (!prsp)
2203 			goto out;
2204 		if (!lpfc_is_els_acc_rsp(prsp))
2205 			goto out;
2206 		ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2207 
2208 		lpfc_check_encryption(phba, ndlp, cmdiocb, rspiocb);
2209 
2210 		sp = (struct serv_parm *)((u8 *)prsp->virt +
2211 					  sizeof(u32));
2212 
2213 		ndlp->vmid_support = 0;
2214 		if ((phba->cfg_vmid_app_header && sp->cmn.app_hdr_support) ||
2215 		    (phba->cfg_vmid_priority_tagging &&
2216 		     sp->cmn.priority_tagging)) {
2217 			lpfc_printf_log(phba, KERN_DEBUG, LOG_ELS,
2218 					"4018 app_hdr_support %d tagging %d DID x%x\n",
2219 					sp->cmn.app_hdr_support,
2220 					sp->cmn.priority_tagging,
2221 					ndlp->nlp_DID);
2222 			/* if the dest port supports VMID, mark it in ndlp */
2223 			ndlp->vmid_support = 1;
2224 		}
2225 
2226 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2227 					NLP_EVT_CMPL_PLOGI);
2228 	}
2229 
2230 	if (disc && vport->num_disc_nodes) {
2231 		/* Check to see if there are more PLOGIs to be sent */
2232 		lpfc_more_plogi(vport);
2233 
2234 		if (vport->num_disc_nodes == 0) {
2235 			clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
2236 
2237 			lpfc_can_disctmo(vport);
2238 			lpfc_end_rscn(vport);
2239 		}
2240 	}
2241 
2242 out:
2243 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2244 			      "PLOGI Cmpl PUT:     did:x%x refcnt %d",
2245 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2246 
2247 out_freeiocb:
2248 	/* Release the reference on the original I/O request. */
2249 	free_ndlp = cmdiocb->ndlp;
2250 
2251 	lpfc_els_free_iocb(phba, cmdiocb);
2252 	lpfc_nlp_put(free_ndlp);
2253 	return;
2254 }
2255 
2256 /**
2257  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
2258  * @vport: pointer to a host virtual N_Port data structure.
2259  * @did: destination port identifier.
2260  * @retry: number of retries to the command IOCB.
2261  *
2262  * This routine issues a Port Login (PLOGI) command to a remote N_Port
2263  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
2264  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
2265  * This routine constructs the proper fields of the PLOGI IOCB and invokes
2266  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
2267  *
2268  * Note that the ndlp reference count will be incremented by 1 for holding
2269  * the ndlp and the reference to ndlp will be stored into the ndlp field
2270  * of the IOCB for the completion callback function to the PLOGI ELS command.
2271  *
2272  * Return code
2273  *   0 - Successfully issued a plogi for @vport
2274  *   1 - failed to issue a plogi for @vport
2275  **/
2276 int
2277 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
2278 {
2279 	struct lpfc_hba  *phba = vport->phba;
2280 	struct serv_parm *sp;
2281 	struct lpfc_nodelist *ndlp;
2282 	struct lpfc_iocbq *elsiocb;
2283 	uint8_t *pcmd;
2284 	uint16_t cmdsize;
2285 	int ret;
2286 
2287 	ndlp = lpfc_findnode_did(vport, did);
2288 	if (!ndlp)
2289 		return 1;
2290 
2291 	/* Defer the processing of the issue PLOGI until after the
2292 	 * outstanding UNREG_RPI mbox command completes, unless we
2293 	 * are going offline. This logic does not apply for Fabric DIDs
2294 	 */
2295 	if ((test_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag) ||
2296 	     test_bit(NLP_UNREG_INP, &ndlp->nlp_flag)) &&
2297 	    ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
2298 	    !test_bit(FC_OFFLINE_MODE, &vport->fc_flag)) {
2299 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2300 				 "4110 Issue PLOGI x%x deferred "
2301 				 "on NPort x%x rpi x%x flg x%lx Data:"
2302 				 " x%px\n",
2303 				 ndlp->nlp_defer_did, ndlp->nlp_DID,
2304 				 ndlp->nlp_rpi, ndlp->nlp_flag, ndlp);
2305 
2306 		/* We can only defer 1st PLOGI */
2307 		if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
2308 			ndlp->nlp_defer_did = did;
2309 		return 0;
2310 	}
2311 
2312 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2313 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2314 				     ELS_CMD_PLOGI);
2315 	if (!elsiocb)
2316 		return 1;
2317 
2318 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
2319 
2320 	/* For PLOGI request, remainder of payload is service parameters */
2321 	*((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
2322 	pcmd += sizeof(uint32_t);
2323 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2324 	sp = (struct serv_parm *) pcmd;
2325 
2326 	/*
2327 	 * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2328 	 * to device on remote loops work.
2329 	 */
2330 	if (test_bit(FC_FABRIC, &vport->fc_flag) &&
2331 	    !test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))
2332 		sp->cmn.altBbCredit = 1;
2333 
2334 	if (sp->cmn.fcphLow < FC_PH_4_3)
2335 		sp->cmn.fcphLow = FC_PH_4_3;
2336 
2337 	if (sp->cmn.fcphHigh < FC_PH3)
2338 		sp->cmn.fcphHigh = FC_PH3;
2339 
2340 	sp->cmn.valid_vendor_ver_level = 0;
2341 	memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
2342 	if (!test_bit(FC_PT2PT, &vport->fc_flag))
2343 		sp->cmn.bbRcvSizeMsb &= 0xF;
2344 
2345 	/* Check if the destination port supports VMID */
2346 	ndlp->vmid_support = 0;
2347 	if (vport->vmid_priority_tagging)
2348 		sp->cmn.priority_tagging = 1;
2349 	else if (phba->cfg_vmid_app_header &&
2350 		 bf_get(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags))
2351 		sp->cmn.app_hdr_support = 1;
2352 
2353 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2354 		"Issue PLOGI:     did:x%x",
2355 		did, 0, 0);
2356 
2357 	/* If our firmware supports this feature, convey that
2358 	 * information to the target using the vendor specific field.
2359 	 */
2360 	if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2361 		sp->cmn.valid_vendor_ver_level = 1;
2362 		sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2363 		sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2364 	}
2365 
2366 	phba->fc_stat.elsXmitPLOGI++;
2367 	elsiocb->cmd_cmpl = lpfc_cmpl_els_plogi;
2368 
2369 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2370 			      "Issue PLOGI:     did:x%x refcnt %d",
2371 			      did, kref_read(&ndlp->kref), 0);
2372 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
2373 	if (!elsiocb->ndlp) {
2374 		lpfc_els_free_iocb(phba, elsiocb);
2375 		return 1;
2376 	}
2377 
2378 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2379 	if (ret) {
2380 		lpfc_els_free_iocb(phba, elsiocb);
2381 		lpfc_nlp_put(ndlp);
2382 		return 1;
2383 	}
2384 
2385 	return 0;
2386 }
2387 
2388 /**
2389  * lpfc_cmpl_els_prli - Completion callback function for prli
2390  * @phba: pointer to lpfc hba data structure.
2391  * @cmdiocb: pointer to lpfc command iocb data structure.
2392  * @rspiocb: pointer to lpfc response iocb data structure.
2393  *
2394  * This routine is the completion callback function for a Process Login
2395  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2396  * status. If there is error status reported, PRLI retry shall be attempted
2397  * by invoking the lpfc_els_retry() routine. Otherwise, the state
2398  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2399  * ndlp to mark the PRLI completion.
2400  **/
2401 static void
2402 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2403 		   struct lpfc_iocbq *rspiocb)
2404 {
2405 	struct lpfc_vport *vport = cmdiocb->vport;
2406 	struct lpfc_nodelist *ndlp;
2407 	char *mode;
2408 	u32 ulp_status;
2409 	u32 ulp_word4;
2410 	bool release_node = false;
2411 
2412 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2413 	cmdiocb->rsp_iocb = rspiocb;
2414 
2415 	ndlp = cmdiocb->ndlp;
2416 
2417 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2418 	ulp_word4 = get_job_word4(phba, rspiocb);
2419 
2420 	clear_bit(NLP_PRLI_SND, &ndlp->nlp_flag);
2421 
2422 	/* Driver supports multiple FC4 types.  Counters matter. */
2423 	spin_lock_irq(&ndlp->lock);
2424 	vport->fc_prli_sent--;
2425 	ndlp->fc4_prli_sent--;
2426 	spin_unlock_irq(&ndlp->lock);
2427 
2428 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2429 		"PRLI cmpl:       status:x%x/x%x did:x%x",
2430 		ulp_status, ulp_word4,
2431 		ndlp->nlp_DID);
2432 
2433 	/* PRLI completes to NPort <nlp_DID> */
2434 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2435 			 "0103 PRLI completes to NPort x%06x "
2436 			 "Data: x%x x%x x%x x%x x%x\n",
2437 			 ndlp->nlp_DID, ulp_status, ulp_word4,
2438 			 vport->num_disc_nodes, ndlp->fc4_prli_sent,
2439 			 ndlp->fc4_xpt_flags);
2440 
2441 	/* Check to see if link went down during discovery */
2442 	if (lpfc_els_chk_latt(vport))
2443 		goto out;
2444 
2445 	if (ulp_status) {
2446 		/* Check for retry */
2447 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2448 			/* ELS command is being retried */
2449 			goto out;
2450 		}
2451 
2452 		/* If we don't send GFT_ID to Fabric, a PRLI error
2453 		 * could be expected.
2454 		 */
2455 		if (test_bit(FC_FABRIC, &vport->fc_flag) ||
2456 		    vport->cfg_enable_fc4_type != LPFC_ENABLE_BOTH)
2457 			mode = KERN_WARNING;
2458 		else
2459 			mode = KERN_INFO;
2460 
2461 		/* Warn PRLI status */
2462 		lpfc_vlog_msg(vport, mode, LOG_ELS,
2463 				 "2754 PRLI DID:%06X Status:x%x/x%x, "
2464 				 "data: x%x x%x x%lx\n",
2465 				 ndlp->nlp_DID, ulp_status,
2466 				 ulp_word4, ndlp->nlp_state,
2467 				 ndlp->fc4_prli_sent, ndlp->nlp_flag);
2468 
2469 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2470 		if (!lpfc_error_lost_link(vport, ulp_status, ulp_word4))
2471 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2472 						NLP_EVT_CMPL_PRLI);
2473 
2474 		/* The following condition catches an inflight transition
2475 		 * mismatch typically caused by an RSCN. Skip any
2476 		 * processing to allow recovery.
2477 		 */
2478 		if ((ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
2479 		     ndlp->nlp_state <= NLP_STE_REG_LOGIN_ISSUE) ||
2480 		    (ndlp->nlp_state == NLP_STE_NPR_NODE &&
2481 		     test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag))) {
2482 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2483 					 "2784 PRLI cmpl: Allow Node recovery "
2484 					 "DID x%06x nstate x%x nflag x%lx\n",
2485 					 ndlp->nlp_DID, ndlp->nlp_state,
2486 					 ndlp->nlp_flag);
2487 			goto out;
2488 		}
2489 
2490 		/*
2491 		 * For P2P topology, retain the node so that PLOGI can be
2492 		 * attempted on it again.
2493 		 */
2494 		if (test_bit(FC_PT2PT, &vport->fc_flag))
2495 			goto out;
2496 
2497 		/* As long as this node is not registered with the SCSI
2498 		 * or NVMe transport and no other PRLIs are outstanding,
2499 		 * it is no longer an active node.  Otherwise devloss
2500 		 * handles the final cleanup.
2501 		 */
2502 		spin_lock_irq(&ndlp->lock);
2503 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD)) &&
2504 		    !ndlp->fc4_prli_sent) {
2505 			clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2506 			if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
2507 				release_node = true;
2508 		}
2509 		spin_unlock_irq(&ndlp->lock);
2510 
2511 		if (release_node)
2512 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2513 						NLP_EVT_DEVICE_RM);
2514 	} else {
2515 		/* Good status, call state machine.  However, if another
2516 		 * PRLI is outstanding, don't call the state machine
2517 		 * because final disposition to Mapped or Unmapped is
2518 		 * completed there.
2519 		 */
2520 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2521 					NLP_EVT_CMPL_PRLI);
2522 	}
2523 
2524 out:
2525 	lpfc_els_free_iocb(phba, cmdiocb);
2526 	lpfc_nlp_put(ndlp);
2527 	return;
2528 }
2529 
2530 /**
2531  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2532  * @vport: pointer to a host virtual N_Port data structure.
2533  * @ndlp: pointer to a node-list data structure.
2534  * @retry: number of retries to the command IOCB.
2535  *
2536  * This routine issues a Process Login (PRLI) ELS command for the
2537  * @vport. The PRLI service parameters are set up in the payload of the
2538  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2539  * is put to the IOCB completion callback func field before invoking the
2540  * routine lpfc_sli_issue_iocb() to send out PRLI command.
2541  *
2542  * Note that the ndlp reference count will be incremented by 1 for holding the
2543  * ndlp and the reference to ndlp will be stored into the ndlp field of
2544  * the IOCB for the completion callback function to the PRLI ELS command.
2545  *
2546  * Return code
2547  *   0 - successfully issued prli iocb command for @vport
2548  *   1 - failed to issue prli iocb command for @vport
2549  **/
2550 int
2551 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2552 		    uint8_t retry)
2553 {
2554 	int rc = 0;
2555 	struct lpfc_hba *phba = vport->phba;
2556 	PRLI *npr;
2557 	struct lpfc_nvme_prli *npr_nvme;
2558 	struct lpfc_iocbq *elsiocb;
2559 	uint8_t *pcmd;
2560 	uint16_t cmdsize;
2561 	u32 local_nlp_type, elscmd;
2562 
2563 	/*
2564 	 * If we are in RSCN mode, the FC4 types supported from a
2565 	 * previous GFT_ID command may not be accurate. So, if we
2566 	 * are a NVME Initiator, always look for the possibility of
2567 	 * the remote NPort beng a NVME Target.
2568 	 */
2569 	if (phba->sli_rev == LPFC_SLI_REV4 &&
2570 	    test_bit(FC_RSCN_MODE, &vport->fc_flag) &&
2571 	    vport->nvmei_support)
2572 		ndlp->nlp_fc4_type |= NLP_FC4_NVME;
2573 	local_nlp_type = ndlp->nlp_fc4_type;
2574 
2575 	/* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2576 	 * fields here before any of them can complete.
2577 	 */
2578 	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2579 	ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2580 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
2581 	clear_bit(NLP_FIRSTBURST, &ndlp->nlp_flag);
2582 	clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2583 	ndlp->nvme_fb_size = 0;
2584 
2585  send_next_prli:
2586 	if (local_nlp_type & NLP_FC4_FCP) {
2587 		/* Payload is 4 + 16 = 20 x14 bytes. */
2588 		cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2589 		elscmd = ELS_CMD_PRLI;
2590 	} else if (local_nlp_type & NLP_FC4_NVME) {
2591 		/* Payload is 4 + 20 = 24 x18 bytes. */
2592 		cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2593 		elscmd = ELS_CMD_NVMEPRLI;
2594 	} else {
2595 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2596 				 "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2597 				 ndlp->nlp_fc4_type, ndlp->nlp_DID);
2598 		return 1;
2599 	}
2600 
2601 	/* SLI3 ports don't support NVME.  If this rport is a strict NVME
2602 	 * FC4 type, implicitly LOGO.
2603 	 */
2604 	if (phba->sli_rev == LPFC_SLI_REV3 &&
2605 	    ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2606 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2607 				 "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2608 				 ndlp->nlp_type);
2609 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2610 		return 1;
2611 	}
2612 
2613 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2614 				     ndlp->nlp_DID, elscmd);
2615 	if (!elsiocb)
2616 		return 1;
2617 
2618 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
2619 
2620 	/* For PRLI request, remainder of payload is service parameters */
2621 	memset(pcmd, 0, cmdsize);
2622 
2623 	if (local_nlp_type & NLP_FC4_FCP) {
2624 		/* Remainder of payload is FCP PRLI parameter page.
2625 		 * Note: this data structure is defined as
2626 		 * BE/LE in the structure definition so no
2627 		 * byte swap call is made.
2628 		 */
2629 		*((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2630 		pcmd += sizeof(uint32_t);
2631 		npr = (PRLI *)pcmd;
2632 
2633 		/*
2634 		 * If our firmware version is 3.20 or later,
2635 		 * set the following bits for FC-TAPE support.
2636 		 */
2637 		if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2638 			npr->ConfmComplAllowed = 1;
2639 			npr->Retry = 1;
2640 			npr->TaskRetryIdReq = 1;
2641 		}
2642 		npr->estabImagePair = 1;
2643 		npr->readXferRdyDis = 1;
2644 		if (vport->cfg_first_burst_size)
2645 			npr->writeXferRdyDis = 1;
2646 
2647 		/* For FCP support */
2648 		npr->prliType = PRLI_FCP_TYPE;
2649 		npr->initiatorFunc = 1;
2650 		elsiocb->cmd_flag |= LPFC_PRLI_FCP_REQ;
2651 
2652 		/* Remove FCP type - processed. */
2653 		local_nlp_type &= ~NLP_FC4_FCP;
2654 	} else if (local_nlp_type & NLP_FC4_NVME) {
2655 		/* Remainder of payload is NVME PRLI parameter page.
2656 		 * This data structure is the newer definition that
2657 		 * uses bf macros so a byte swap is required.
2658 		 */
2659 		*((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2660 		pcmd += sizeof(uint32_t);
2661 		npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2662 		bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2663 		bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
2664 		if (phba->nsler) {
2665 			bf_set(prli_nsler, npr_nvme, 1);
2666 			bf_set(prli_conf, npr_nvme, 1);
2667 		}
2668 
2669 		/* Only initiators request first burst. */
2670 		if ((phba->cfg_nvme_enable_fb) &&
2671 		    !phba->nvmet_support)
2672 			bf_set(prli_fba, npr_nvme, 1);
2673 
2674 		if (phba->nvmet_support) {
2675 			bf_set(prli_tgt, npr_nvme, 1);
2676 			bf_set(prli_disc, npr_nvme, 1);
2677 		} else {
2678 			bf_set(prli_init, npr_nvme, 1);
2679 			bf_set(prli_conf, npr_nvme, 1);
2680 		}
2681 
2682 		npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2683 		npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2684 		elsiocb->cmd_flag |= LPFC_PRLI_NVME_REQ;
2685 
2686 		/* Remove NVME type - processed. */
2687 		local_nlp_type &= ~NLP_FC4_NVME;
2688 	}
2689 
2690 	phba->fc_stat.elsXmitPRLI++;
2691 	elsiocb->cmd_cmpl = lpfc_cmpl_els_prli;
2692 
2693 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2694 			      "Issue PRLI:  did:x%x refcnt %d",
2695 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
2696 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
2697 	if (!elsiocb->ndlp) {
2698 		lpfc_els_free_iocb(phba, elsiocb);
2699 		return 1;
2700 	}
2701 
2702 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2703 	if (rc == IOCB_ERROR) {
2704 		lpfc_els_free_iocb(phba, elsiocb);
2705 		lpfc_nlp_put(ndlp);
2706 		return 1;
2707 	}
2708 
2709 	/* The vport counters are used for lpfc_scan_finished, but
2710 	 * the ndlp is used to track outstanding PRLIs for different
2711 	 * FC4 types.
2712 	 */
2713 	set_bit(NLP_PRLI_SND, &ndlp->nlp_flag);
2714 	spin_lock_irq(&ndlp->lock);
2715 	vport->fc_prli_sent++;
2716 	ndlp->fc4_prli_sent++;
2717 	spin_unlock_irq(&ndlp->lock);
2718 
2719 	/* The driver supports 2 FC4 types.  Make sure
2720 	 * a PRLI is issued for all types before exiting.
2721 	 */
2722 	if (phba->sli_rev == LPFC_SLI_REV4 &&
2723 	    local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
2724 		goto send_next_prli;
2725 	else
2726 		return 0;
2727 }
2728 
2729 /**
2730  * lpfc_rscn_disc - Perform rscn discovery for a vport
2731  * @vport: pointer to a host virtual N_Port data structure.
2732  *
2733  * This routine performs Registration State Change Notification (RSCN)
2734  * discovery for a @vport. If the @vport's node port recovery count is not
2735  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2736  * the nodes that need recovery. If none of the PLOGI were needed through
2737  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2738  * invoked to check and handle possible more RSCN came in during the period
2739  * of processing the current ones.
2740  **/
2741 static void
2742 lpfc_rscn_disc(struct lpfc_vport *vport)
2743 {
2744 	lpfc_can_disctmo(vport);
2745 
2746 	/* RSCN discovery */
2747 	/* go thru NPR nodes and issue ELS PLOGIs */
2748 	if (atomic_read(&vport->fc_npr_cnt))
2749 		if (lpfc_els_disc_plogi(vport))
2750 			return;
2751 
2752 	lpfc_end_rscn(vport);
2753 }
2754 
2755 /**
2756  * lpfc_adisc_done - Complete the adisc phase of discovery
2757  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2758  *
2759  * This function is called when the final ADISC is completed during discovery.
2760  * This function handles clearing link attention or issuing reg_vpi depending
2761  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2762  * discovery.
2763  * This function is called with no locks held.
2764  **/
2765 static void
2766 lpfc_adisc_done(struct lpfc_vport *vport)
2767 {
2768 	struct lpfc_hba   *phba = vport->phba;
2769 
2770 	/*
2771 	 * For NPIV, cmpl_reg_vpi will set port_state to READY,
2772 	 * and continue discovery.
2773 	 */
2774 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2775 	    !test_bit(FC_RSCN_MODE, &vport->fc_flag) &&
2776 	    (phba->sli_rev < LPFC_SLI_REV4)) {
2777 
2778 		/*
2779 		 * If link is down, clear_la and reg_vpi will be done after
2780 		 * flogi following a link up event
2781 		 */
2782 		if (!lpfc_is_link_up(phba))
2783 			return;
2784 
2785 		/* The ADISCs are complete.  Doesn't matter if they
2786 		 * succeeded or failed because the ADISC completion
2787 		 * routine guarantees to call the state machine and
2788 		 * the RPI is either unregistered (failed ADISC response)
2789 		 * or the RPI is still valid and the node is marked
2790 		 * mapped for a target.  The exchanges should be in the
2791 		 * correct state. This code is specific to SLI3.
2792 		 */
2793 		lpfc_issue_clear_la(phba, vport);
2794 		lpfc_issue_reg_vpi(phba, vport);
2795 		return;
2796 	}
2797 	/*
2798 	* For SLI2, we need to set port_state to READY
2799 	* and continue discovery.
2800 	*/
2801 	if (vport->port_state < LPFC_VPORT_READY) {
2802 		/* If we get here, there is nothing to ADISC */
2803 		lpfc_issue_clear_la(phba, vport);
2804 		if (!test_bit(FC_ABORT_DISCOVERY, &vport->fc_flag)) {
2805 			vport->num_disc_nodes = 0;
2806 			/* go thru NPR list, issue ELS PLOGIs */
2807 			if (atomic_read(&vport->fc_npr_cnt))
2808 				lpfc_els_disc_plogi(vport);
2809 			if (!vport->num_disc_nodes) {
2810 				clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
2811 				lpfc_can_disctmo(vport);
2812 				lpfc_end_rscn(vport);
2813 			}
2814 		}
2815 		vport->port_state = LPFC_VPORT_READY;
2816 	} else
2817 		lpfc_rscn_disc(vport);
2818 }
2819 
2820 /**
2821  * lpfc_more_adisc - Issue more adisc as needed
2822  * @vport: pointer to a host virtual N_Port data structure.
2823  *
2824  * This routine determines whether there are more ndlps on a @vport
2825  * node list need to have Address Discover (ADISC) issued. If so, it will
2826  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2827  * remaining nodes which need to have ADISC sent.
2828  **/
2829 void
2830 lpfc_more_adisc(struct lpfc_vport *vport)
2831 {
2832 	if (vport->num_disc_nodes)
2833 		vport->num_disc_nodes--;
2834 	/* Continue discovery with <num_disc_nodes> ADISCs to go */
2835 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2836 			 "0210 Continue discovery with %d ADISCs to go "
2837 			 "Data: x%x x%lx x%x\n",
2838 			 vport->num_disc_nodes,
2839 			 atomic_read(&vport->fc_adisc_cnt),
2840 			 vport->fc_flag, vport->port_state);
2841 	/* Check to see if there are more ADISCs to be sent */
2842 	if (test_bit(FC_NLP_MORE, &vport->fc_flag)) {
2843 		lpfc_set_disctmo(vport);
2844 		/* go thru NPR nodes and issue any remaining ELS ADISCs */
2845 		lpfc_els_disc_adisc(vport);
2846 	}
2847 	if (!vport->num_disc_nodes)
2848 		lpfc_adisc_done(vport);
2849 	return;
2850 }
2851 
2852 /**
2853  * lpfc_cmpl_els_adisc - Completion callback function for adisc
2854  * @phba: pointer to lpfc hba data structure.
2855  * @cmdiocb: pointer to lpfc command iocb data structure.
2856  * @rspiocb: pointer to lpfc response iocb data structure.
2857  *
2858  * This routine is the completion function for issuing the Address Discover
2859  * (ADISC) command. It first checks to see whether link went down during
2860  * the discovery process. If so, the node will be marked as node port
2861  * recovery for issuing discover IOCB by the link attention handler and
2862  * exit. Otherwise, the response status is checked. If error was reported
2863  * in the response status, the ADISC command shall be retried by invoking
2864  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2865  * the response status, the state machine is invoked to set transition
2866  * with respect to NLP_EVT_CMPL_ADISC event.
2867  **/
2868 static void
2869 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2870 		    struct lpfc_iocbq *rspiocb)
2871 {
2872 	struct lpfc_vport *vport = cmdiocb->vport;
2873 	IOCB_t *irsp;
2874 	struct lpfc_nodelist *ndlp;
2875 	bool  disc;
2876 	u32 ulp_status, ulp_word4, tmo, iotag;
2877 	bool release_node = false;
2878 
2879 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2880 	cmdiocb->rsp_iocb = rspiocb;
2881 
2882 	ndlp = cmdiocb->ndlp;
2883 
2884 	ulp_status = get_job_ulpstatus(phba, rspiocb);
2885 	ulp_word4 = get_job_word4(phba, rspiocb);
2886 
2887 	if (phba->sli_rev == LPFC_SLI_REV4) {
2888 		tmo = get_wqe_tmo(cmdiocb);
2889 		iotag = get_wqe_reqtag(cmdiocb);
2890 	} else {
2891 		irsp = &rspiocb->iocb;
2892 		tmo = irsp->ulpTimeout;
2893 		iotag = irsp->ulpIoTag;
2894 	}
2895 
2896 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2897 		"ADISC cmpl:      status:x%x/x%x did:x%x",
2898 		ulp_status, ulp_word4,
2899 		ndlp->nlp_DID);
2900 
2901 	/* Since ndlp can be freed in the disc state machine, note if this node
2902 	 * is being used during discovery.
2903 	 */
2904 	disc = test_and_clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2905 	clear_bit(NLP_ADISC_SND, &ndlp->nlp_flag);
2906 	/* ADISC completes to NPort <nlp_DID> */
2907 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2908 			 "0104 ADISC completes to NPort x%x "
2909 			 "IoTag x%x Data: x%x x%x x%x x%x x%x\n",
2910 			 ndlp->nlp_DID, iotag,
2911 			 ulp_status, ulp_word4,
2912 			 tmo, disc, vport->num_disc_nodes);
2913 
2914 	/* Check to see if link went down during discovery */
2915 	if (lpfc_els_chk_latt(vport)) {
2916 		set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2917 		goto out;
2918 	}
2919 
2920 	if (ulp_status) {
2921 		/* Check for retry */
2922 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2923 			/* ELS command is being retried */
2924 			if (disc) {
2925 				set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2926 				lpfc_set_disctmo(vport);
2927 			}
2928 			goto out;
2929 		}
2930 		/* Warn ADISC status */
2931 		lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
2932 			      "2755 ADISC DID:%06X Status:x%x/x%x\n",
2933 			      ndlp->nlp_DID, ulp_status,
2934 			      ulp_word4);
2935 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2936 					NLP_EVT_CMPL_ADISC);
2937 
2938 		/* As long as this node is not registered with the SCSI or NVMe
2939 		 * transport, it is no longer an active node. Otherwise
2940 		 * devloss handles the final cleanup.
2941 		 */
2942 		spin_lock_irq(&ndlp->lock);
2943 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
2944 			clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
2945 			if (!test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
2946 				release_node = true;
2947 		}
2948 		spin_unlock_irq(&ndlp->lock);
2949 
2950 		if (release_node)
2951 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2952 						NLP_EVT_DEVICE_RM);
2953 	} else
2954 		/* Good status, call state machine */
2955 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2956 					NLP_EVT_CMPL_ADISC);
2957 
2958 	/* Check to see if there are more ADISCs to be sent */
2959 	if (disc && vport->num_disc_nodes)
2960 		lpfc_more_adisc(vport);
2961 out:
2962 	lpfc_els_free_iocb(phba, cmdiocb);
2963 	lpfc_nlp_put(ndlp);
2964 	return;
2965 }
2966 
2967 /**
2968  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2969  * @vport: pointer to a virtual N_Port data structure.
2970  * @ndlp: pointer to a node-list data structure.
2971  * @retry: number of retries to the command IOCB.
2972  *
2973  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2974  * @vport. It prepares the payload of the ADISC ELS command, updates the
2975  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2976  * to issue the ADISC ELS command.
2977  *
2978  * Note that the ndlp reference count will be incremented by 1 for holding the
2979  * ndlp and the reference to ndlp will be stored into the ndlp field of
2980  * the IOCB for the completion callback function to the ADISC ELS command.
2981  *
2982  * Return code
2983  *   0 - successfully issued adisc
2984  *   1 - failed to issue adisc
2985  **/
2986 int
2987 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2988 		     uint8_t retry)
2989 {
2990 	int rc = 0;
2991 	struct lpfc_hba  *phba = vport->phba;
2992 	ADISC *ap;
2993 	struct lpfc_iocbq *elsiocb;
2994 	uint8_t *pcmd;
2995 	uint16_t cmdsize;
2996 
2997 	cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2998 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2999 				     ndlp->nlp_DID, ELS_CMD_ADISC);
3000 	if (!elsiocb)
3001 		return 1;
3002 
3003 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3004 
3005 	/* For ADISC request, remainder of payload is service parameters */
3006 	*((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
3007 	pcmd += sizeof(uint32_t);
3008 
3009 	/* Fill in ADISC payload */
3010 	ap = (ADISC *) pcmd;
3011 	ap->hardAL_PA = phba->fc_pref_ALPA;
3012 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3013 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3014 	ap->DID = be32_to_cpu(vport->fc_myDID);
3015 
3016 	phba->fc_stat.elsXmitADISC++;
3017 	elsiocb->cmd_cmpl = lpfc_cmpl_els_adisc;
3018 	set_bit(NLP_ADISC_SND, &ndlp->nlp_flag);
3019 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
3020 	if (!elsiocb->ndlp) {
3021 		lpfc_els_free_iocb(phba, elsiocb);
3022 		goto err;
3023 	}
3024 
3025 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3026 			      "Issue ADISC:   did:x%x refcnt %d",
3027 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3028 
3029 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3030 	if (rc == IOCB_ERROR) {
3031 		lpfc_els_free_iocb(phba, elsiocb);
3032 		lpfc_nlp_put(ndlp);
3033 		goto err;
3034 	}
3035 
3036 	return 0;
3037 
3038 err:
3039 	clear_bit(NLP_ADISC_SND, &ndlp->nlp_flag);
3040 	return 1;
3041 }
3042 
3043 /**
3044  * lpfc_cmpl_els_logo - Completion callback function for logo
3045  * @phba: pointer to lpfc hba data structure.
3046  * @cmdiocb: pointer to lpfc command iocb data structure.
3047  * @rspiocb: pointer to lpfc response iocb data structure.
3048  *
3049  * This routine is the completion function for issuing the ELS Logout (LOGO)
3050  * command. If no error status was reported from the LOGO response, the
3051  * state machine of the associated ndlp shall be invoked for transition with
3052  * respect to NLP_EVT_CMPL_LOGO event.
3053  **/
3054 static void
3055 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3056 		   struct lpfc_iocbq *rspiocb)
3057 {
3058 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
3059 	struct lpfc_vport *vport = ndlp->vport;
3060 	IOCB_t *irsp;
3061 	uint32_t skip_recovery = 0;
3062 	int wake_up_waiter = 0;
3063 	u32 ulp_status;
3064 	u32 ulp_word4;
3065 	u32 tmo, iotag;
3066 
3067 	/* we pass cmdiocb to state machine which needs rspiocb as well */
3068 	cmdiocb->rsp_iocb = rspiocb;
3069 
3070 	ulp_status = get_job_ulpstatus(phba, rspiocb);
3071 	ulp_word4 = get_job_word4(phba, rspiocb);
3072 
3073 	if (phba->sli_rev == LPFC_SLI_REV4) {
3074 		tmo = get_wqe_tmo(cmdiocb);
3075 		iotag = get_wqe_reqtag(cmdiocb);
3076 	} else {
3077 		irsp = &rspiocb->iocb;
3078 		tmo = irsp->ulpTimeout;
3079 		iotag = irsp->ulpIoTag;
3080 	}
3081 
3082 	clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
3083 	if (test_and_clear_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags))
3084 		wake_up_waiter = 1;
3085 
3086 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3087 		"LOGO cmpl:       status:x%x/x%x did:x%x",
3088 		ulp_status, ulp_word4,
3089 		ndlp->nlp_DID);
3090 
3091 	/* LOGO completes to NPort <nlp_DID> */
3092 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3093 			 "0105 LOGO completes to NPort x%x "
3094 			 "IoTag x%x refcnt %d nflags x%lx xflags x%x "
3095 			 "Data: x%x x%x x%x x%x\n",
3096 			 ndlp->nlp_DID, iotag,
3097 			 kref_read(&ndlp->kref), ndlp->nlp_flag,
3098 			 ndlp->fc4_xpt_flags, ulp_status, ulp_word4,
3099 			 tmo, vport->num_disc_nodes);
3100 
3101 	if (lpfc_els_chk_latt(vport)) {
3102 		skip_recovery = 1;
3103 		goto out;
3104 	}
3105 
3106 	/* The LOGO will not be retried on failure.  A LOGO was
3107 	 * issued to the remote rport and a ACC or RJT or no Answer are
3108 	 * all acceptable.  Note the failure and move forward with
3109 	 * discovery.  The PLOGI will retry.
3110 	 */
3111 	if (ulp_status) {
3112 		/* Warn LOGO status */
3113 		lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
3114 			      "2756 LOGO, No Retry DID:%06X "
3115 			      "Status:x%x/x%x\n",
3116 			      ndlp->nlp_DID, ulp_status,
3117 			      ulp_word4);
3118 
3119 		/* Call NLP_EVT_DEVICE_RM if link is down or LOGO is aborted */
3120 		if (lpfc_error_lost_link(vport, ulp_status, ulp_word4))
3121 			skip_recovery = 1;
3122 	}
3123 
3124 	/* Call state machine. This will unregister the rpi if needed. */
3125 	lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
3126 
3127 out:
3128 	/* At this point, the LOGO processing is complete. NOTE: For a
3129 	 * pt2pt topology, we are assuming the NPortID will only change
3130 	 * on link up processing. For a LOGO / PLOGI initiated by the
3131 	 * Initiator, we are assuming the NPortID is not going to change.
3132 	 */
3133 
3134 	if (wake_up_waiter && ndlp->logo_waitq)
3135 		wake_up(ndlp->logo_waitq);
3136 	/*
3137 	 * If the node is a target, the handling attempts to recover the port.
3138 	 * For any other port type, the rpi is unregistered as an implicit
3139 	 * LOGO.
3140 	 */
3141 	if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
3142 	    skip_recovery == 0) {
3143 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
3144 		set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
3145 
3146 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3147 				 "3187 LOGO completes to NPort x%x: Start "
3148 				 "Recovery Data: x%x x%x x%x x%x\n",
3149 				 ndlp->nlp_DID, ulp_status,
3150 				 ulp_word4, tmo,
3151 				 vport->num_disc_nodes);
3152 
3153 		lpfc_els_free_iocb(phba, cmdiocb);
3154 		lpfc_nlp_put(ndlp);
3155 
3156 		lpfc_disc_start(vport);
3157 		return;
3158 	}
3159 
3160 	/* Cleanup path for failed REG_RPI handling. If REG_RPI fails, the
3161 	 * driver sends a LOGO to the rport to cleanup.  For fabric and
3162 	 * initiator ports cleanup the node as long as it the node is not
3163 	 * register with the transport.
3164 	 */
3165 	if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
3166 		clear_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
3167 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
3168 					NLP_EVT_DEVICE_RM);
3169 	}
3170 
3171 	/* Driver is done with the I/O. */
3172 	lpfc_els_free_iocb(phba, cmdiocb);
3173 	lpfc_nlp_put(ndlp);
3174 }
3175 
3176 /**
3177  * lpfc_issue_els_logo - Issue a logo to an node on a vport
3178  * @vport: pointer to a virtual N_Port data structure.
3179  * @ndlp: pointer to a node-list data structure.
3180  * @retry: number of retries to the command IOCB.
3181  *
3182  * This routine constructs and issues an ELS Logout (LOGO) iocb command
3183  * to a remote node, referred by an @ndlp on a @vport. It constructs the
3184  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
3185  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
3186  *
3187  * Note that the ndlp reference count will be incremented by 1 for holding the
3188  * ndlp and the reference to ndlp will be stored into the ndlp field of
3189  * the IOCB for the completion callback function to the LOGO ELS command.
3190  *
3191  * Callers of this routine are expected to unregister the RPI first
3192  *
3193  * Return code
3194  *   0 - successfully issued logo
3195  *   1 - failed to issue logo
3196  **/
3197 int
3198 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3199 		    uint8_t retry)
3200 {
3201 	struct lpfc_hba  *phba = vport->phba;
3202 	struct lpfc_iocbq *elsiocb;
3203 	uint8_t *pcmd;
3204 	uint16_t cmdsize;
3205 	int rc;
3206 
3207 	if (test_bit(NLP_LOGO_SND, &ndlp->nlp_flag))
3208 		return 0;
3209 
3210 	cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
3211 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3212 				     ndlp->nlp_DID, ELS_CMD_LOGO);
3213 	if (!elsiocb)
3214 		return 1;
3215 
3216 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3217 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
3218 	pcmd += sizeof(uint32_t);
3219 
3220 	/* Fill in LOGO payload */
3221 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
3222 	pcmd += sizeof(uint32_t);
3223 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
3224 
3225 	phba->fc_stat.elsXmitLOGO++;
3226 	elsiocb->cmd_cmpl = lpfc_cmpl_els_logo;
3227 	set_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
3228 	clear_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
3229 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
3230 	if (!elsiocb->ndlp) {
3231 		lpfc_els_free_iocb(phba, elsiocb);
3232 		goto err;
3233 	}
3234 
3235 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3236 			      "Issue LOGO:      did:x%x refcnt %d",
3237 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3238 
3239 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3240 	if (rc == IOCB_ERROR) {
3241 		lpfc_els_free_iocb(phba, elsiocb);
3242 		lpfc_nlp_put(ndlp);
3243 		goto err;
3244 	}
3245 
3246 	spin_lock_irq(&ndlp->lock);
3247 	ndlp->nlp_prev_state = ndlp->nlp_state;
3248 	spin_unlock_irq(&ndlp->lock);
3249 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3250 	return 0;
3251 
3252 err:
3253 	clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
3254 	return 1;
3255 }
3256 
3257 /**
3258  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
3259  * @phba: pointer to lpfc hba data structure.
3260  * @cmdiocb: pointer to lpfc command iocb data structure.
3261  * @rspiocb: pointer to lpfc response iocb data structure.
3262  *
3263  * This routine is a generic completion callback function for ELS commands.
3264  * Specifically, it is the callback function which does not need to perform
3265  * any command specific operations. It is currently used by the ELS command
3266  * issuing routines for RSCN, lpfc_issue_els_rscn, and the ELS Fibre Channel
3267  * Address Resolution Protocol Response (FARPR) routine, lpfc_issue_els_farpr().
3268  * Other than certain debug loggings, this callback function simply invokes the
3269  * lpfc_els_chk_latt() routine to check whether link went down during the
3270  * discovery process.
3271  **/
3272 static void
3273 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3274 		  struct lpfc_iocbq *rspiocb)
3275 {
3276 	struct lpfc_vport *vport = cmdiocb->vport;
3277 	struct lpfc_nodelist *free_ndlp;
3278 	IOCB_t *irsp;
3279 	u32 ulp_status, ulp_word4, tmo, did, iotag;
3280 
3281 	ulp_status = get_job_ulpstatus(phba, rspiocb);
3282 	ulp_word4 = get_job_word4(phba, rspiocb);
3283 	did = get_job_els_rsp64_did(phba, cmdiocb);
3284 
3285 	if (phba->sli_rev == LPFC_SLI_REV4) {
3286 		tmo = get_wqe_tmo(cmdiocb);
3287 		iotag = get_wqe_reqtag(cmdiocb);
3288 	} else {
3289 		irsp = &rspiocb->iocb;
3290 		tmo = irsp->ulpTimeout;
3291 		iotag = irsp->ulpIoTag;
3292 	}
3293 
3294 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3295 			      "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3296 			      ulp_status, ulp_word4, did);
3297 
3298 	/* ELS cmd tag <ulpIoTag> completes */
3299 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3300 			 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
3301 			 iotag, ulp_status, ulp_word4, tmo);
3302 
3303 	/* Check to see if link went down during discovery */
3304 	lpfc_els_chk_latt(vport);
3305 
3306 	free_ndlp = cmdiocb->ndlp;
3307 
3308 	lpfc_els_free_iocb(phba, cmdiocb);
3309 	lpfc_nlp_put(free_ndlp);
3310 }
3311 
3312 /**
3313  * lpfc_reg_fab_ctrl_node - RPI register the fabric controller node.
3314  * @vport: pointer to lpfc_vport data structure.
3315  * @fc_ndlp: pointer to the fabric controller (0xfffffd) node.
3316  *
3317  * This routine registers the rpi assigned to the fabric controller
3318  * NPort_ID (0xfffffd) with the port and moves the node to UNMAPPED
3319  * state triggering a registration with the SCSI transport.
3320  *
3321  * This routine is single out because the fabric controller node
3322  * does not receive a PLOGI.  This routine is consumed by the
3323  * SCR and RDF ELS commands.  Callers are expected to qualify
3324  * with SLI4 first.
3325  **/
3326 static int
3327 lpfc_reg_fab_ctrl_node(struct lpfc_vport *vport, struct lpfc_nodelist *fc_ndlp)
3328 {
3329 	int rc;
3330 	struct lpfc_hba *phba = vport->phba;
3331 	struct lpfc_nodelist *ns_ndlp;
3332 	LPFC_MBOXQ_t *mbox;
3333 
3334 	if (test_bit(NLP_RPI_REGISTERED, &fc_ndlp->nlp_flag))
3335 		return 0;
3336 
3337 	ns_ndlp = lpfc_findnode_did(vport, NameServer_DID);
3338 	if (!ns_ndlp)
3339 		return -ENODEV;
3340 
3341 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3342 			 "0935 %s: Reg FC RPI x%x on FC DID x%x NSSte: x%x\n",
3343 			 __func__, fc_ndlp->nlp_rpi, fc_ndlp->nlp_DID,
3344 			 ns_ndlp->nlp_state);
3345 	if (ns_ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
3346 		return -ENODEV;
3347 
3348 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3349 	if (!mbox) {
3350 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3351 				 "0936 %s: no memory for reg_login "
3352 				 "Data: x%x x%x x%lx x%x\n", __func__,
3353 				 fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3354 				 fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3355 		return -ENOMEM;
3356 	}
3357 	rc = lpfc_reg_rpi(phba, vport->vpi, fc_ndlp->nlp_DID,
3358 			  (u8 *)&ns_ndlp->fc_sparam, mbox, fc_ndlp->nlp_rpi);
3359 	if (rc) {
3360 		rc = -EACCES;
3361 		goto out;
3362 	}
3363 
3364 	set_bit(NLP_REG_LOGIN_SEND, &fc_ndlp->nlp_flag);
3365 	mbox->mbox_cmpl = lpfc_mbx_cmpl_fc_reg_login;
3366 	mbox->ctx_ndlp = lpfc_nlp_get(fc_ndlp);
3367 	if (!mbox->ctx_ndlp) {
3368 		rc = -ENOMEM;
3369 		goto out;
3370 	}
3371 
3372 	mbox->vport = vport;
3373 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3374 	if (rc == MBX_NOT_FINISHED) {
3375 		rc = -ENODEV;
3376 		lpfc_nlp_put(fc_ndlp);
3377 		goto out;
3378 	}
3379 	/* Success path. Exit. */
3380 	lpfc_nlp_set_state(vport, fc_ndlp,
3381 			   NLP_STE_REG_LOGIN_ISSUE);
3382 	return 0;
3383 
3384  out:
3385 	lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
3386 	lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3387 			 "0938 %s: failed to format reg_login "
3388 			 "Data: x%x x%x x%lx x%x\n", __func__,
3389 			 fc_ndlp->nlp_DID, fc_ndlp->nlp_state,
3390 			 fc_ndlp->nlp_flag, fc_ndlp->nlp_rpi);
3391 	return rc;
3392 }
3393 
3394 /**
3395  * lpfc_cmpl_els_disc_cmd - Completion callback function for Discovery ELS cmd
3396  * @phba: pointer to lpfc hba data structure.
3397  * @cmdiocb: pointer to lpfc command iocb data structure.
3398  * @rspiocb: pointer to lpfc response iocb data structure.
3399  *
3400  * This routine is a generic completion callback function for Discovery ELS cmd.
3401  * Currently used by the ELS command issuing routines for the ELS State Change
3402  * Request (SCR), lpfc_issue_els_scr(), Exchange Diagnostic Capabilities (EDC),
3403  * lpfc_issue_els_edc()  and the ELS RDF, lpfc_issue_els_rdf().
3404  * These commands will be retried once only for ELS timeout errors.
3405  **/
3406 static void
3407 lpfc_cmpl_els_disc_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3408 		       struct lpfc_iocbq *rspiocb)
3409 {
3410 	struct lpfc_vport *vport = cmdiocb->vport;
3411 	IOCB_t *irsp;
3412 	struct lpfc_els_rdf_rsp *prdf;
3413 	struct lpfc_dmabuf *pcmd, *prsp;
3414 	u32 *pdata;
3415 	u32 cmd;
3416 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
3417 	u32 ulp_status, ulp_word4, tmo, did, iotag;
3418 
3419 	ulp_status = get_job_ulpstatus(phba, rspiocb);
3420 	ulp_word4 = get_job_word4(phba, rspiocb);
3421 	did = get_job_els_rsp64_did(phba, cmdiocb);
3422 
3423 	if (phba->sli_rev == LPFC_SLI_REV4) {
3424 		tmo = get_wqe_tmo(cmdiocb);
3425 		iotag = get_wqe_reqtag(cmdiocb);
3426 	} else {
3427 		irsp = &rspiocb->iocb;
3428 		tmo = irsp->ulpTimeout;
3429 		iotag = irsp->ulpIoTag;
3430 	}
3431 
3432 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3433 		"ELS cmd cmpl:    status:x%x/x%x did:x%x",
3434 		ulp_status, ulp_word4, did);
3435 
3436 	/* ELS cmd tag <ulpIoTag> completes */
3437 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3438 			 "0217 ELS cmd tag x%x completes Data: x%x x%x x%x x%x\n",
3439 			 iotag, ulp_status, ulp_word4, tmo, cmdiocb->retry);
3440 
3441 	pcmd = cmdiocb->cmd_dmabuf;
3442 	if (!pcmd)
3443 		goto out;
3444 
3445 	pdata = (u32 *)pcmd->virt;
3446 	if (!pdata)
3447 		goto out;
3448 	cmd = *pdata;
3449 
3450 	/* Only 1 retry for ELS Timeout only */
3451 	if (ulp_status == IOSTAT_LOCAL_REJECT &&
3452 	    ((ulp_word4 & IOERR_PARAM_MASK) ==
3453 	    IOERR_SEQUENCE_TIMEOUT)) {
3454 		cmdiocb->retry++;
3455 		if (cmdiocb->retry <= 1) {
3456 			switch (cmd) {
3457 			case ELS_CMD_SCR:
3458 				lpfc_issue_els_scr(vport, cmdiocb->retry);
3459 				break;
3460 			case ELS_CMD_EDC:
3461 				lpfc_issue_els_edc(vport, cmdiocb->retry);
3462 				break;
3463 			case ELS_CMD_RDF:
3464 				lpfc_issue_els_rdf(vport, cmdiocb->retry);
3465 				break;
3466 			}
3467 			goto out;
3468 		}
3469 		phba->fc_stat.elsRetryExceeded++;
3470 	}
3471 	if (cmd == ELS_CMD_EDC) {
3472 		/* must be called before checking uplStatus and returning */
3473 		lpfc_cmpl_els_edc(phba, cmdiocb, rspiocb);
3474 		return;
3475 	}
3476 
3477 	if (ulp_status) {
3478 		/* ELS discovery cmd completes with error */
3479 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS | LOG_CGN_MGMT,
3480 				 "4203 ELS cmd x%x error: x%x x%X\n", cmd,
3481 				 ulp_status, ulp_word4);
3482 
3483 		/* In the case where the ELS cmd completes with an error and
3484 		 * the node does not have RPI registered, the node is
3485 		 * outstanding and should put its initial reference.
3486 		 */
3487 		if ((cmd == ELS_CMD_SCR || cmd == ELS_CMD_RDF) &&
3488 		    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD) &&
3489 		    !test_and_set_bit(NLP_DROPPED, &ndlp->nlp_flag))
3490 			lpfc_nlp_put(ndlp);
3491 		goto out;
3492 	}
3493 
3494 	/* The RDF response doesn't have any impact on the running driver
3495 	 * but the notification descriptors are dumped here for support.
3496 	 */
3497 	if (cmd == ELS_CMD_RDF) {
3498 		int i;
3499 
3500 		prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
3501 		if (!prsp)
3502 			goto out;
3503 
3504 		prdf = (struct lpfc_els_rdf_rsp *)prsp->virt;
3505 		if (!prdf)
3506 			goto out;
3507 		if (!lpfc_is_els_acc_rsp(prsp))
3508 			goto out;
3509 
3510 		for (i = 0; i < ELS_RDF_REG_TAG_CNT &&
3511 			    i < be32_to_cpu(prdf->reg_d1.reg_desc.count); i++)
3512 			lpfc_printf_vlog(vport, KERN_INFO,
3513 					 LOG_ELS | LOG_CGN_MGMT,
3514 					 "4677 Fabric RDF Notification Grant "
3515 					 "Data: 0x%08x Reg: %x %x\n",
3516 					 be32_to_cpu(
3517 						 prdf->reg_d1.desc_tags[i]),
3518 					 phba->cgn_reg_signal,
3519 					 phba->cgn_reg_fpin);
3520 	}
3521 
3522 out:
3523 	/* Check to see if link went down during discovery */
3524 	lpfc_els_chk_latt(vport);
3525 	lpfc_els_free_iocb(phba, cmdiocb);
3526 	lpfc_nlp_put(ndlp);
3527 	return;
3528 }
3529 
3530 /**
3531  * lpfc_issue_els_scr - Issue a scr to an node on a vport
3532  * @vport: pointer to a host virtual N_Port data structure.
3533  * @retry: retry counter for the command IOCB.
3534  *
3535  * This routine issues a State Change Request (SCR) to a fabric node
3536  * on a @vport. The remote node is Fabric Controller (0xfffffd). It
3537  * first search the @vport node list to find the matching ndlp. If no such
3538  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
3539  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
3540  * routine is invoked to send the SCR IOCB.
3541  *
3542  * Note that the ndlp reference count will be incremented by 1 for holding the
3543  * ndlp and the reference to ndlp will be stored into the ndlp field of
3544  * the IOCB for the completion callback function to the SCR ELS command.
3545  *
3546  * Return code
3547  *   0 - Successfully issued scr command
3548  *   1 - Failed to issue scr command
3549  **/
3550 int
3551 lpfc_issue_els_scr(struct lpfc_vport *vport, uint8_t retry)
3552 {
3553 	int rc = 0;
3554 	struct lpfc_hba  *phba = vport->phba;
3555 	struct lpfc_iocbq *elsiocb;
3556 	uint8_t *pcmd;
3557 	uint16_t cmdsize;
3558 	struct lpfc_nodelist *ndlp;
3559 	bool node_created = false;
3560 
3561 	cmdsize = (sizeof(uint32_t) + sizeof(SCR));
3562 
3563 	ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3564 	if (!ndlp) {
3565 		ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3566 		if (!ndlp)
3567 			return 1;
3568 		lpfc_enqueue_node(vport, ndlp);
3569 		node_created = true;
3570 	}
3571 
3572 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3573 				     ndlp->nlp_DID, ELS_CMD_SCR);
3574 	if (!elsiocb)
3575 		goto out_node_created;
3576 
3577 	if (phba->sli_rev == LPFC_SLI_REV4) {
3578 		rc = lpfc_reg_fab_ctrl_node(vport, ndlp);
3579 		if (rc) {
3580 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3581 					 "0937 %s: Failed to reg fc node, rc %d\n",
3582 					 __func__, rc);
3583 			goto out_free_iocb;
3584 		}
3585 	}
3586 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3587 
3588 	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
3589 	pcmd += sizeof(uint32_t);
3590 
3591 	/* For SCR, remainder of payload is SCR parameter page */
3592 	memset(pcmd, 0, sizeof(SCR));
3593 	((SCR *) pcmd)->Function = SCR_FUNC_FULL;
3594 
3595 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3596 		"Issue SCR:       did:x%x",
3597 		ndlp->nlp_DID, 0, 0);
3598 
3599 	phba->fc_stat.elsXmitSCR++;
3600 	elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
3601 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
3602 	if (!elsiocb->ndlp)
3603 		goto out_free_iocb;
3604 
3605 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3606 			      "Issue SCR:     did:x%x refcnt %d",
3607 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3608 
3609 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3610 	if (rc == IOCB_ERROR)
3611 		goto out_iocb_error;
3612 
3613 	return 0;
3614 
3615 out_iocb_error:
3616 	lpfc_nlp_put(ndlp);
3617 out_free_iocb:
3618 	lpfc_els_free_iocb(phba, elsiocb);
3619 out_node_created:
3620 	if (node_created)
3621 		lpfc_nlp_put(ndlp);
3622 	return 1;
3623 }
3624 
3625 /**
3626  * lpfc_issue_els_rscn - Issue an RSCN to the Fabric Controller (Fabric)
3627  *   or the other nport (pt2pt).
3628  * @vport: pointer to a host virtual N_Port data structure.
3629  * @retry: number of retries to the command IOCB.
3630  *
3631  * This routine issues a RSCN to the Fabric Controller (DID 0xFFFFFD)
3632  *  when connected to a fabric, or to the remote port when connected
3633  *  in point-to-point mode. When sent to the Fabric Controller, it will
3634  *  replay the RSCN to registered recipients.
3635  *
3636  * Note that the ndlp reference count will be incremented by 1 for holding the
3637  * ndlp and the reference to ndlp will be stored into the ndlp field of
3638  * the IOCB for the completion callback function to the RSCN ELS command.
3639  *
3640  * Return code
3641  *   0 - Successfully issued RSCN command
3642  *   1 - Failed to issue RSCN command
3643  **/
3644 int
3645 lpfc_issue_els_rscn(struct lpfc_vport *vport, uint8_t retry)
3646 {
3647 	int rc = 0;
3648 	struct lpfc_hba *phba = vport->phba;
3649 	struct lpfc_iocbq *elsiocb;
3650 	struct lpfc_nodelist *ndlp;
3651 	struct {
3652 		struct fc_els_rscn rscn;
3653 		struct fc_els_rscn_page portid;
3654 	} *event;
3655 	uint32_t nportid;
3656 	uint16_t cmdsize = sizeof(*event);
3657 
3658 	/* Not supported for private loop */
3659 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
3660 	    !test_bit(FC_PUBLIC_LOOP, &vport->fc_flag))
3661 		return 1;
3662 
3663 	if (test_bit(FC_PT2PT, &vport->fc_flag)) {
3664 		/* find any mapped nport - that would be the other nport */
3665 		ndlp = lpfc_findnode_mapped(vport);
3666 		if (!ndlp)
3667 			return 1;
3668 	} else {
3669 		nportid = FC_FID_FCTRL;
3670 		/* find the fabric controller node */
3671 		ndlp = lpfc_findnode_did(vport, nportid);
3672 		if (!ndlp) {
3673 			/* if one didn't exist, make one */
3674 			ndlp = lpfc_nlp_init(vport, nportid);
3675 			if (!ndlp)
3676 				return 1;
3677 			lpfc_enqueue_node(vport, ndlp);
3678 		}
3679 	}
3680 
3681 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3682 				     ndlp->nlp_DID, ELS_CMD_RSCN_XMT);
3683 
3684 	if (!elsiocb)
3685 		return 1;
3686 
3687 	event = elsiocb->cmd_dmabuf->virt;
3688 
3689 	event->rscn.rscn_cmd = ELS_RSCN;
3690 	event->rscn.rscn_page_len = sizeof(struct fc_els_rscn_page);
3691 	event->rscn.rscn_plen = cpu_to_be16(cmdsize);
3692 
3693 	nportid = vport->fc_myDID;
3694 	/* appears that page flags must be 0 for fabric to broadcast RSCN */
3695 	event->portid.rscn_page_flags = 0;
3696 	event->portid.rscn_fid[0] = (nportid & 0x00FF0000) >> 16;
3697 	event->portid.rscn_fid[1] = (nportid & 0x0000FF00) >> 8;
3698 	event->portid.rscn_fid[2] = nportid & 0x000000FF;
3699 
3700 	phba->fc_stat.elsXmitRSCN++;
3701 	elsiocb->cmd_cmpl = lpfc_cmpl_els_cmd;
3702 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
3703 	if (!elsiocb->ndlp) {
3704 		lpfc_els_free_iocb(phba, elsiocb);
3705 		return 1;
3706 	}
3707 
3708 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3709 			      "Issue RSCN:   did:x%x refcnt %d",
3710 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3711 
3712 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3713 	if (rc == IOCB_ERROR) {
3714 		lpfc_els_free_iocb(phba, elsiocb);
3715 		lpfc_nlp_put(ndlp);
3716 		return 1;
3717 	}
3718 
3719 	return 0;
3720 }
3721 
3722 /**
3723  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
3724  * @vport: pointer to a host virtual N_Port data structure.
3725  * @nportid: N_Port identifier to the remote node.
3726  * @retry: number of retries to the command IOCB.
3727  *
3728  * This routine issues a Fibre Channel Address Resolution Response
3729  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
3730  * is passed into the function. It first search the @vport node list to find
3731  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
3732  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
3733  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
3734  *
3735  * Note that the ndlp reference count will be incremented by 1 for holding the
3736  * ndlp and the reference to ndlp will be stored into the ndlp field of
3737  * the IOCB for the completion callback function to the FARPR ELS command.
3738  *
3739  * Return code
3740  *   0 - Successfully issued farpr command
3741  *   1 - Failed to issue farpr command
3742  **/
3743 static int
3744 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3745 {
3746 	int rc = 0;
3747 	struct lpfc_hba  *phba = vport->phba;
3748 	struct lpfc_iocbq *elsiocb;
3749 	FARP *fp;
3750 	uint8_t *pcmd;
3751 	uint32_t *lp;
3752 	uint16_t cmdsize;
3753 	struct lpfc_nodelist *ondlp;
3754 	struct lpfc_nodelist *ndlp;
3755 
3756 	cmdsize = (sizeof(uint32_t) + sizeof(FARP));
3757 
3758 	ndlp = lpfc_findnode_did(vport, nportid);
3759 	if (!ndlp) {
3760 		ndlp = lpfc_nlp_init(vport, nportid);
3761 		if (!ndlp)
3762 			return 1;
3763 		lpfc_enqueue_node(vport, ndlp);
3764 	}
3765 
3766 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3767 				     ndlp->nlp_DID, ELS_CMD_FARPR);
3768 	if (!elsiocb)
3769 		return 1;
3770 
3771 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
3772 
3773 	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
3774 	pcmd += sizeof(uint32_t);
3775 
3776 	/* Fill in FARPR payload */
3777 	fp = (FARP *) (pcmd);
3778 	memset(fp, 0, sizeof(FARP));
3779 	lp = (uint32_t *) pcmd;
3780 	*lp++ = be32_to_cpu(nportid);
3781 	*lp++ = be32_to_cpu(vport->fc_myDID);
3782 	fp->Rflags = 0;
3783 	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3784 
3785 	memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3786 	memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3787 	ondlp = lpfc_findnode_did(vport, nportid);
3788 	if (ondlp) {
3789 		memcpy(&fp->OportName, &ondlp->nlp_portname,
3790 		       sizeof(struct lpfc_name));
3791 		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
3792 		       sizeof(struct lpfc_name));
3793 	}
3794 
3795 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3796 		"Issue FARPR:     did:x%x",
3797 		ndlp->nlp_DID, 0, 0);
3798 
3799 	phba->fc_stat.elsXmitFARPR++;
3800 	elsiocb->cmd_cmpl = lpfc_cmpl_els_cmd;
3801 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
3802 	if (!elsiocb->ndlp) {
3803 		lpfc_els_free_iocb(phba, elsiocb);
3804 		return 1;
3805 	}
3806 
3807 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3808 	if (rc == IOCB_ERROR) {
3809 		/* The additional lpfc_nlp_put will cause the following
3810 		 * lpfc_els_free_iocb routine to trigger the release of
3811 		 * the node.
3812 		 */
3813 		lpfc_els_free_iocb(phba, elsiocb);
3814 		lpfc_nlp_put(ndlp);
3815 		return 1;
3816 	}
3817 
3818 	return 0;
3819 }
3820 
3821 /**
3822  * lpfc_issue_els_rdf - Register for diagnostic functions from the fabric.
3823  * @vport: pointer to a host virtual N_Port data structure.
3824  * @retry: retry counter for the command IOCB.
3825  *
3826  * This routine issues an ELS RDF to the Fabric Controller to register
3827  * for diagnostic functions.
3828  *
3829  * Note that the ndlp reference count will be incremented by 1 for holding the
3830  * ndlp and the reference to ndlp will be stored into the ndlp field of
3831  * the IOCB for the completion callback function to the RDF ELS command.
3832  *
3833  * Return code
3834  *   0 - Successfully issued rdf command
3835  *   < 0 - Failed to issue rdf command
3836  *   -EACCES - RDF not required for NPIV_PORT
3837  *   -ENODEV - No fabric controller device available
3838  *   -ENOMEM - No available memory
3839  *   -EIO - The mailbox failed to complete successfully.
3840  *
3841  **/
3842 int
3843 lpfc_issue_els_rdf(struct lpfc_vport *vport, uint8_t retry)
3844 {
3845 	struct lpfc_hba *phba = vport->phba;
3846 	struct lpfc_iocbq *elsiocb;
3847 	struct lpfc_els_rdf_req *prdf;
3848 	struct lpfc_nodelist *ndlp;
3849 	uint16_t cmdsize;
3850 	int rc;
3851 	bool node_created = false;
3852 	int err;
3853 
3854 	cmdsize = sizeof(*prdf);
3855 
3856 	/* RDF ELS is not required on an NPIV VN_Port. */
3857 	if (vport->port_type == LPFC_NPIV_PORT)
3858 		return -EACCES;
3859 
3860 	ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3861 	if (!ndlp) {
3862 		ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3863 		if (!ndlp)
3864 			return -ENODEV;
3865 		lpfc_enqueue_node(vport, ndlp);
3866 		node_created = true;
3867 	}
3868 
3869 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3870 				     ndlp->nlp_DID, ELS_CMD_RDF);
3871 	if (!elsiocb) {
3872 		err = -ENOMEM;
3873 		goto out_node_created;
3874 	}
3875 
3876 	/* Configure the payload for the supported FPIN events. */
3877 	prdf = (struct lpfc_els_rdf_req *)elsiocb->cmd_dmabuf->virt;
3878 	memset(prdf, 0, cmdsize);
3879 	prdf->rdf.fpin_cmd = ELS_RDF;
3880 	prdf->rdf.desc_len = cpu_to_be32(sizeof(struct lpfc_els_rdf_req) -
3881 					 sizeof(struct fc_els_rdf_hdr));
3882 	prdf->reg_d1.reg_desc.desc_tag = cpu_to_be32(ELS_DTAG_FPIN_REGISTER);
3883 	prdf->reg_d1.reg_desc.desc_len = cpu_to_be32(
3884 				FC_TLV_DESC_LENGTH_FROM_SZ(prdf->reg_d1));
3885 	prdf->reg_d1.reg_desc.count = cpu_to_be32(ELS_RDF_REG_TAG_CNT);
3886 	prdf->reg_d1.desc_tags[0] = cpu_to_be32(ELS_DTAG_LNK_INTEGRITY);
3887 	prdf->reg_d1.desc_tags[1] = cpu_to_be32(ELS_DTAG_DELIVERY);
3888 	prdf->reg_d1.desc_tags[2] = cpu_to_be32(ELS_DTAG_PEER_CONGEST);
3889 	prdf->reg_d1.desc_tags[3] = cpu_to_be32(ELS_DTAG_CONGESTION);
3890 
3891 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3892 			 "6444 Xmit RDF to remote NPORT x%x Reg: %x %x\n",
3893 			 ndlp->nlp_DID, phba->cgn_reg_signal,
3894 			 phba->cgn_reg_fpin);
3895 
3896 	phba->cgn_fpin_frequency = LPFC_FPIN_INIT_FREQ;
3897 	elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
3898 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
3899 	if (!elsiocb->ndlp) {
3900 		err = -EIO;
3901 		goto out_free_iocb;
3902 	}
3903 
3904 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3905 			      "Issue RDF:     did:x%x refcnt %d",
3906 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
3907 
3908 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3909 	if (rc == IOCB_ERROR) {
3910 		err = -EIO;
3911 		goto out_iocb_error;
3912 	}
3913 	return 0;
3914 
3915 out_iocb_error:
3916 	lpfc_nlp_put(ndlp);
3917 out_free_iocb:
3918 	lpfc_els_free_iocb(phba, elsiocb);
3919 out_node_created:
3920 	if (node_created)
3921 		lpfc_nlp_put(ndlp);
3922 	return err;
3923 }
3924 
3925  /**
3926   * lpfc_els_rcv_rdf - Receive RDF ELS request from the fabric.
3927   * @vport: pointer to a host virtual N_Port data structure.
3928   * @cmdiocb: pointer to lpfc command iocb data structure.
3929   * @ndlp: pointer to a node-list data structure.
3930   *
3931   * A received RDF implies a possible change to fabric supported diagnostic
3932   * functions.  This routine sends LS_ACC and then has the Nx_Port issue a new
3933   * RDF request to reregister for supported diagnostic functions.
3934   *
3935   * Return code
3936   *   0 - Success
3937   *   -EIO - Failed to process received RDF
3938   **/
3939 static int
3940 lpfc_els_rcv_rdf(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3941 		 struct lpfc_nodelist *ndlp)
3942 {
3943 	int rc;
3944 
3945 	rc = lpfc_els_rsp_acc(vport, ELS_CMD_RDF, cmdiocb, ndlp, NULL);
3946 	/* Send LS_ACC */
3947 	if (rc) {
3948 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3949 				 "1623 Failed to RDF_ACC from x%x for x%x Data: %d\n",
3950 				 ndlp->nlp_DID, vport->fc_myDID, rc);
3951 		return -EIO;
3952 	}
3953 
3954 	rc = lpfc_issue_els_rdf(vport, 0);
3955 	/* Issue new RDF for reregistering */
3956 	if (rc) {
3957 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
3958 				 "2623 Failed to re register RDF for x%x Data: %d\n",
3959 				 vport->fc_myDID, rc);
3960 		return -EIO;
3961 	}
3962 
3963 	return 0;
3964 }
3965 
3966 /**
3967  * lpfc_least_capable_settings - helper function for EDC rsp processing
3968  * @phba: pointer to lpfc hba data structure.
3969  * @pcgd: pointer to congestion detection descriptor in EDC rsp.
3970  *
3971  * This helper routine determines the least capable setting for
3972  * congestion signals, signal freq, including scale, from the
3973  * congestion detection descriptor in the EDC rsp.  The routine
3974  * sets @phba values in preparation for a set_featues mailbox.
3975  **/
3976 static void
3977 lpfc_least_capable_settings(struct lpfc_hba *phba,
3978 			    struct fc_diag_cg_sig_desc *pcgd)
3979 {
3980 	u32 rsp_sig_cap = 0, drv_sig_cap = 0;
3981 	u32 rsp_sig_freq_cyc = 0, rsp_sig_freq_scale = 0;
3982 
3983 	/* Get rsp signal and frequency capabilities.  */
3984 	rsp_sig_cap = be32_to_cpu(pcgd->xmt_signal_capability);
3985 	rsp_sig_freq_cyc = be16_to_cpu(pcgd->xmt_signal_frequency.count);
3986 	rsp_sig_freq_scale = be16_to_cpu(pcgd->xmt_signal_frequency.units);
3987 
3988 	/* If the Fport does not support signals. Set FPIN only */
3989 	if (rsp_sig_cap == EDC_CG_SIG_NOTSUPPORTED)
3990 		goto out_no_support;
3991 
3992 	/* Apply the xmt scale to the xmt cycle to get the correct frequency.
3993 	 * Adapter default is 100 millisSeconds.  Convert all xmt cycle values
3994 	 * to milliSeconds.
3995 	 */
3996 	switch (rsp_sig_freq_scale) {
3997 	case EDC_CG_SIGFREQ_SEC:
3998 		rsp_sig_freq_cyc *= MSEC_PER_SEC;
3999 		break;
4000 	case EDC_CG_SIGFREQ_MSEC:
4001 		rsp_sig_freq_cyc = 1;
4002 		break;
4003 	default:
4004 		goto out_no_support;
4005 	}
4006 
4007 	/* Convenient shorthand. */
4008 	drv_sig_cap = phba->cgn_reg_signal;
4009 
4010 	/* Choose the least capable frequency. */
4011 	if (rsp_sig_freq_cyc > phba->cgn_sig_freq)
4012 		phba->cgn_sig_freq = rsp_sig_freq_cyc;
4013 
4014 	/* Should be some common signals support. Settle on least capable
4015 	 * signal and adjust FPIN values. Initialize defaults to ease the
4016 	 * decision.
4017 	 */
4018 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
4019 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4020 	if (rsp_sig_cap == EDC_CG_SIG_WARN_ONLY &&
4021 	    (drv_sig_cap == EDC_CG_SIG_WARN_ONLY ||
4022 	     drv_sig_cap == EDC_CG_SIG_WARN_ALARM)) {
4023 		phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
4024 		phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
4025 	}
4026 	if (rsp_sig_cap == EDC_CG_SIG_WARN_ALARM) {
4027 		if (drv_sig_cap == EDC_CG_SIG_WARN_ALARM) {
4028 			phba->cgn_reg_signal = EDC_CG_SIG_WARN_ALARM;
4029 			phba->cgn_reg_fpin = LPFC_CGN_FPIN_NONE;
4030 		}
4031 		if (drv_sig_cap == EDC_CG_SIG_WARN_ONLY) {
4032 			phba->cgn_reg_signal = EDC_CG_SIG_WARN_ONLY;
4033 			phba->cgn_reg_fpin &= ~LPFC_CGN_FPIN_WARN;
4034 		}
4035 	}
4036 
4037 	/* We are NOT recording signal frequency in congestion info buffer */
4038 	return;
4039 
4040 out_no_support:
4041 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4042 	phba->cgn_sig_freq = 0;
4043 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
4044 }
4045 
4046 DECLARE_ENUM2STR_LOOKUP(lpfc_get_tlv_dtag_nm, fc_ls_tlv_dtag,
4047 			FC_LS_TLV_DTAG_INIT);
4048 
4049 /**
4050  * lpfc_cmpl_els_edc - Completion callback function for EDC
4051  * @phba: pointer to lpfc hba data structure.
4052  * @cmdiocb: pointer to lpfc command iocb data structure.
4053  * @rspiocb: pointer to lpfc response iocb data structure.
4054  *
4055  * This routine is the completion callback function for issuing the Exchange
4056  * Diagnostic Capabilities (EDC) command. The driver issues an EDC to
4057  * notify the FPort of its Congestion and Link Fault capabilities.  This
4058  * routine parses the FPort's response and decides on the least common
4059  * values applicable to both FPort and NPort for Warnings and Alarms that
4060  * are communicated via hardware signals.
4061  **/
4062 static void
4063 lpfc_cmpl_els_edc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4064 		  struct lpfc_iocbq *rspiocb)
4065 {
4066 	IOCB_t *irsp_iocb;
4067 	struct fc_els_edc_resp *edc_rsp;
4068 	struct fc_tlv_desc *tlv;
4069 	struct fc_diag_cg_sig_desc *pcgd;
4070 	struct fc_diag_lnkflt_desc *plnkflt;
4071 	struct lpfc_dmabuf *pcmd, *prsp;
4072 	const char *dtag_nm;
4073 	u32 *pdata, dtag;
4074 	int desc_cnt = 0, bytes_remain;
4075 	bool rcv_cap_desc = false;
4076 	struct lpfc_nodelist *ndlp;
4077 	u32 ulp_status, ulp_word4, tmo, did, iotag;
4078 
4079 	ndlp = cmdiocb->ndlp;
4080 
4081 	ulp_status = get_job_ulpstatus(phba, rspiocb);
4082 	ulp_word4 = get_job_word4(phba, rspiocb);
4083 	did = get_job_els_rsp64_did(phba, rspiocb);
4084 
4085 	if (phba->sli_rev == LPFC_SLI_REV4) {
4086 		tmo = get_wqe_tmo(rspiocb);
4087 		iotag = get_wqe_reqtag(rspiocb);
4088 	} else {
4089 		irsp_iocb = &rspiocb->iocb;
4090 		tmo = irsp_iocb->ulpTimeout;
4091 		iotag = irsp_iocb->ulpIoTag;
4092 	}
4093 
4094 	lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
4095 			      "EDC cmpl:    status:x%x/x%x did:x%x",
4096 			      ulp_status, ulp_word4, did);
4097 
4098 	/* ELS cmd tag <ulpIoTag> completes */
4099 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
4100 			"4201 EDC cmd tag x%x completes Data: x%x x%x x%x\n",
4101 			iotag, ulp_status, ulp_word4, tmo);
4102 
4103 	pcmd = cmdiocb->cmd_dmabuf;
4104 	if (!pcmd)
4105 		goto out;
4106 
4107 	pdata = (u32 *)pcmd->virt;
4108 	if (!pdata)
4109 		goto out;
4110 
4111 	/* Need to clear signal values, send features MB and RDF with FPIN. */
4112 	if (ulp_status)
4113 		goto out;
4114 
4115 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
4116 	if (!prsp)
4117 		goto out;
4118 
4119 	edc_rsp = prsp->virt;
4120 	if (!edc_rsp)
4121 		goto out;
4122 
4123 	/* ELS cmd tag <ulpIoTag> completes */
4124 	lpfc_printf_log(phba, KERN_INFO,
4125 			LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
4126 			"4676 Fabric EDC Rsp: "
4127 			"0x%02x, 0x%08x\n",
4128 			edc_rsp->acc_hdr.la_cmd,
4129 			be32_to_cpu(edc_rsp->desc_list_len));
4130 
4131 	if (!lpfc_is_els_acc_rsp(prsp))
4132 		goto out;
4133 
4134 	/*
4135 	 * Payload length in bytes is the response descriptor list
4136 	 * length minus the 12 bytes of Link Service Request
4137 	 * Information descriptor in the reply.
4138 	 */
4139 	bytes_remain = be32_to_cpu(edc_rsp->desc_list_len) -
4140 				   sizeof(struct fc_els_lsri_desc);
4141 	if (bytes_remain <= 0)
4142 		goto out;
4143 
4144 	tlv = edc_rsp->desc;
4145 
4146 	/*
4147 	 * cycle through EDC diagnostic descriptors to find the
4148 	 * congestion signaling capability descriptor
4149 	 */
4150 	while (bytes_remain) {
4151 		if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
4152 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
4153 					"6461 Truncated TLV hdr on "
4154 					"Diagnostic descriptor[%d]\n",
4155 					desc_cnt);
4156 			goto out;
4157 		}
4158 
4159 		dtag = be32_to_cpu(tlv->desc_tag);
4160 		switch (dtag) {
4161 		case ELS_DTAG_LNK_FAULT_CAP:
4162 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
4163 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
4164 					sizeof(struct fc_diag_lnkflt_desc)) {
4165 				lpfc_printf_log(phba, KERN_WARNING,
4166 					LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
4167 					"6462 Truncated Link Fault Diagnostic "
4168 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
4169 					desc_cnt, bytes_remain,
4170 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
4171 					sizeof(struct fc_diag_lnkflt_desc));
4172 				goto out;
4173 			}
4174 			plnkflt = (struct fc_diag_lnkflt_desc *)tlv;
4175 			lpfc_printf_log(phba, KERN_INFO,
4176 				LOG_ELS | LOG_LDS_EVENT,
4177 				"4617 Link Fault Desc Data: 0x%08x 0x%08x "
4178 				"0x%08x 0x%08x 0x%08x\n",
4179 				be32_to_cpu(plnkflt->desc_tag),
4180 				be32_to_cpu(plnkflt->desc_len),
4181 				be32_to_cpu(
4182 					plnkflt->degrade_activate_threshold),
4183 				be32_to_cpu(
4184 					plnkflt->degrade_deactivate_threshold),
4185 				be32_to_cpu(plnkflt->fec_degrade_interval));
4186 			break;
4187 		case ELS_DTAG_CG_SIGNAL_CAP:
4188 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
4189 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
4190 					sizeof(struct fc_diag_cg_sig_desc)) {
4191 				lpfc_printf_log(
4192 					phba, KERN_WARNING, LOG_CGN_MGMT,
4193 					"6463 Truncated Cgn Signal Diagnostic "
4194 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
4195 					desc_cnt, bytes_remain,
4196 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
4197 					sizeof(struct fc_diag_cg_sig_desc));
4198 				goto out;
4199 			}
4200 
4201 			pcgd = (struct fc_diag_cg_sig_desc *)tlv;
4202 			lpfc_printf_log(
4203 				phba, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
4204 				"4616 CGN Desc Data: 0x%08x 0x%08x "
4205 				"0x%08x 0x%04x 0x%04x 0x%08x 0x%04x 0x%04x\n",
4206 				be32_to_cpu(pcgd->desc_tag),
4207 				be32_to_cpu(pcgd->desc_len),
4208 				be32_to_cpu(pcgd->xmt_signal_capability),
4209 				be16_to_cpu(pcgd->xmt_signal_frequency.count),
4210 				be16_to_cpu(pcgd->xmt_signal_frequency.units),
4211 				be32_to_cpu(pcgd->rcv_signal_capability),
4212 				be16_to_cpu(pcgd->rcv_signal_frequency.count),
4213 				be16_to_cpu(pcgd->rcv_signal_frequency.units));
4214 
4215 			/* Compare driver and Fport capabilities and choose
4216 			 * least common.
4217 			 */
4218 			lpfc_least_capable_settings(phba, pcgd);
4219 			rcv_cap_desc = true;
4220 			break;
4221 		default:
4222 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
4223 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
4224 					"4919 unknown Diagnostic "
4225 					"Descriptor[%d]: tag x%x (%s)\n",
4226 					desc_cnt, dtag, dtag_nm);
4227 		}
4228 
4229 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
4230 		tlv = fc_tlv_next_desc(tlv);
4231 		desc_cnt++;
4232 	}
4233 
4234 out:
4235 	if (!rcv_cap_desc) {
4236 		phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
4237 		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4238 		phba->cgn_sig_freq = 0;
4239 		lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_CGN_MGMT,
4240 				"4202 EDC rsp error - sending RDF "
4241 				"for FPIN only.\n");
4242 	}
4243 
4244 	lpfc_config_cgn_signal(phba);
4245 
4246 	/* Check to see if link went down during discovery */
4247 	lpfc_els_chk_latt(phba->pport);
4248 	lpfc_debugfs_disc_trc(phba->pport, LPFC_DISC_TRC_ELS_CMD,
4249 			      "EDC Cmpl:     did:x%x refcnt %d",
4250 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
4251 	lpfc_els_free_iocb(phba, cmdiocb);
4252 	lpfc_nlp_put(ndlp);
4253 }
4254 
4255 static void
4256 lpfc_format_edc_lft_desc(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
4257 {
4258 	struct fc_diag_lnkflt_desc *lft = (struct fc_diag_lnkflt_desc *)tlv;
4259 
4260 	lft->desc_tag = cpu_to_be32(ELS_DTAG_LNK_FAULT_CAP);
4261 	lft->desc_len = cpu_to_be32(
4262 		FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_diag_lnkflt_desc));
4263 
4264 	lft->degrade_activate_threshold =
4265 		cpu_to_be32(phba->degrade_activate_threshold);
4266 	lft->degrade_deactivate_threshold =
4267 		cpu_to_be32(phba->degrade_deactivate_threshold);
4268 	lft->fec_degrade_interval = cpu_to_be32(phba->fec_degrade_interval);
4269 }
4270 
4271 static void
4272 lpfc_format_edc_cgn_desc(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
4273 {
4274 	struct fc_diag_cg_sig_desc *cgd = (struct fc_diag_cg_sig_desc *)tlv;
4275 
4276 	/* We are assuming cgd was zero'ed before calling this routine */
4277 
4278 	/* Configure the congestion detection capability */
4279 	cgd->desc_tag = cpu_to_be32(ELS_DTAG_CG_SIGNAL_CAP);
4280 
4281 	/* Descriptor len doesn't include the tag or len fields. */
4282 	cgd->desc_len = cpu_to_be32(
4283 		FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_diag_cg_sig_desc));
4284 
4285 	/* xmt_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
4286 	 * xmt_signal_frequency.count already set to 0.
4287 	 * xmt_signal_frequency.units already set to 0.
4288 	 */
4289 
4290 	if (phba->cmf_active_mode == LPFC_CFG_OFF) {
4291 		/* rcv_signal_capability already set to EDC_CG_SIG_NOTSUPPORTED.
4292 		 * rcv_signal_frequency.count already set to 0.
4293 		 * rcv_signal_frequency.units already set to 0.
4294 		 */
4295 		phba->cgn_sig_freq = 0;
4296 		return;
4297 	}
4298 	switch (phba->cgn_reg_signal) {
4299 	case EDC_CG_SIG_WARN_ONLY:
4300 		cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ONLY);
4301 		break;
4302 	case EDC_CG_SIG_WARN_ALARM:
4303 		cgd->rcv_signal_capability = cpu_to_be32(EDC_CG_SIG_WARN_ALARM);
4304 		break;
4305 	default:
4306 		/* rcv_signal_capability left 0 thus no support */
4307 		break;
4308 	}
4309 
4310 	/* We start negotiation with lpfc_fabric_cgn_frequency, after
4311 	 * the completion we settle on the higher frequency.
4312 	 */
4313 	cgd->rcv_signal_frequency.count =
4314 		cpu_to_be16(lpfc_fabric_cgn_frequency);
4315 	cgd->rcv_signal_frequency.units =
4316 		cpu_to_be16(EDC_CG_SIGFREQ_MSEC);
4317 }
4318 
4319 static bool
4320 lpfc_link_is_lds_capable(struct lpfc_hba *phba)
4321 {
4322 	if (!(phba->lmt & LMT_64Gb))
4323 		return false;
4324 	if (phba->sli_rev != LPFC_SLI_REV4)
4325 		return false;
4326 
4327 	if (phba->sli4_hba.conf_trunk) {
4328 		if (phba->trunk_link.phy_lnk_speed == LPFC_USER_LINK_SPEED_64G)
4329 			return true;
4330 	} else if (phba->fc_linkspeed == LPFC_LINK_SPEED_64GHZ) {
4331 		return true;
4332 	}
4333 	return false;
4334 }
4335 
4336  /**
4337   * lpfc_issue_els_edc - Exchange Diagnostic Capabilities with the fabric.
4338   * @vport: pointer to a host virtual N_Port data structure.
4339   * @retry: retry counter for the command iocb.
4340   *
4341   * This routine issues an ELS EDC to the F-Port Controller to communicate
4342   * this N_Port's support of hardware signals in its Congestion
4343   * Capabilities Descriptor.
4344   *
4345   * Note: This routine does not check if one or more signals are
4346   * set in the cgn_reg_signal parameter.  The caller makes the
4347   * decision to enforce cgn_reg_signal as nonzero or zero depending
4348   * on the conditions.  During Fabric requests, the driver
4349   * requires cgn_reg_signals to be nonzero.  But a dynamic request
4350   * to set the congestion mode to OFF from Monitor or Manage
4351   * would correctly issue an EDC with no signals enabled to
4352   * turn off switch functionality and then update the FW.
4353   *
4354   * Return code
4355   *   0 - Successfully issued edc command
4356   *   1 - Failed to issue edc command
4357   **/
4358 int
4359 lpfc_issue_els_edc(struct lpfc_vport *vport, uint8_t retry)
4360 {
4361 	struct lpfc_hba  *phba = vport->phba;
4362 	struct lpfc_iocbq *elsiocb;
4363 	struct fc_els_edc *edc_req;
4364 	struct fc_tlv_desc *tlv;
4365 	u16 cmdsize;
4366 	struct lpfc_nodelist *ndlp;
4367 	u8 *pcmd = NULL;
4368 	u32 cgn_desc_size, lft_desc_size;
4369 	int rc;
4370 
4371 	if (vport->port_type == LPFC_NPIV_PORT)
4372 		return -EACCES;
4373 
4374 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
4375 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
4376 		return -ENODEV;
4377 
4378 	cgn_desc_size = (phba->cgn_init_reg_signal) ?
4379 				sizeof(struct fc_diag_cg_sig_desc) : 0;
4380 	lft_desc_size = (lpfc_link_is_lds_capable(phba)) ?
4381 				sizeof(struct fc_diag_lnkflt_desc) : 0;
4382 	cmdsize = cgn_desc_size + lft_desc_size;
4383 
4384 	/* Skip EDC if no applicable descriptors */
4385 	if (!cmdsize)
4386 		goto try_rdf;
4387 
4388 	cmdsize += sizeof(struct fc_els_edc);
4389 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
4390 				     ndlp->nlp_DID, ELS_CMD_EDC);
4391 	if (!elsiocb)
4392 		goto try_rdf;
4393 
4394 	/* Configure the payload for the supported Diagnostics capabilities. */
4395 	pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
4396 	memset(pcmd, 0, cmdsize);
4397 	edc_req = (struct fc_els_edc *)pcmd;
4398 	edc_req->desc_len = cpu_to_be32(cgn_desc_size + lft_desc_size);
4399 	edc_req->edc_cmd = ELS_EDC;
4400 	tlv = edc_req->desc;
4401 
4402 	if (cgn_desc_size) {
4403 		lpfc_format_edc_cgn_desc(phba, tlv);
4404 		phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
4405 		tlv = fc_tlv_next_desc(tlv);
4406 	}
4407 
4408 	if (lft_desc_size)
4409 		lpfc_format_edc_lft_desc(phba, tlv);
4410 
4411 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_CGN_MGMT,
4412 			 "4623 Xmit EDC to remote "
4413 			 "NPORT x%x reg_sig x%x reg_fpin:x%x\n",
4414 			 ndlp->nlp_DID, phba->cgn_reg_signal,
4415 			 phba->cgn_reg_fpin);
4416 
4417 	elsiocb->cmd_cmpl = lpfc_cmpl_els_disc_cmd;
4418 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
4419 	if (!elsiocb->ndlp) {
4420 		lpfc_els_free_iocb(phba, elsiocb);
4421 		return -EIO;
4422 	}
4423 
4424 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4425 			      "Issue EDC:     did:x%x refcnt %d",
4426 			      ndlp->nlp_DID, kref_read(&ndlp->kref), 0);
4427 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4428 	if (rc == IOCB_ERROR) {
4429 		/* The additional lpfc_nlp_put will cause the following
4430 		 * lpfc_els_free_iocb routine to trigger the release of
4431 		 * the node.
4432 		 */
4433 		lpfc_els_free_iocb(phba, elsiocb);
4434 		lpfc_nlp_put(ndlp);
4435 		goto try_rdf;
4436 	}
4437 	return 0;
4438 try_rdf:
4439 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
4440 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
4441 	rc = lpfc_issue_els_rdf(vport, 0);
4442 	return rc;
4443 }
4444 
4445 /**
4446  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
4447  * @vport: pointer to a host virtual N_Port data structure.
4448  * @nlp: pointer to a node-list data structure.
4449  *
4450  * This routine cancels the timer with a delayed IOCB-command retry for
4451  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
4452  * removes the ELS retry event if it presents. In addition, if the
4453  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
4454  * commands are sent for the @vport's nodes that require issuing discovery
4455  * ADISC.
4456  **/
4457 void
4458 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
4459 {
4460 	struct lpfc_work_evt *evtp;
4461 
4462 	if (!test_and_clear_bit(NLP_DELAY_TMO, &nlp->nlp_flag))
4463 		return;
4464 	timer_delete_sync(&nlp->nlp_delayfunc);
4465 	nlp->nlp_last_elscmd = 0;
4466 	if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
4467 		list_del_init(&nlp->els_retry_evt.evt_listp);
4468 		/* Decrement nlp reference count held for the delayed retry */
4469 		evtp = &nlp->els_retry_evt;
4470 		lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
4471 	}
4472 	if (test_and_clear_bit(NLP_NPR_2B_DISC, &nlp->nlp_flag)) {
4473 		if (vport->num_disc_nodes) {
4474 			if (vport->port_state < LPFC_VPORT_READY) {
4475 				/* Check if there are more ADISCs to be sent */
4476 				lpfc_more_adisc(vport);
4477 			} else {
4478 				/* Check if there are more PLOGIs to be sent */
4479 				lpfc_more_plogi(vport);
4480 				if (vport->num_disc_nodes == 0) {
4481 					clear_bit(FC_NDISC_ACTIVE,
4482 						  &vport->fc_flag);
4483 					lpfc_can_disctmo(vport);
4484 					lpfc_end_rscn(vport);
4485 				}
4486 			}
4487 		}
4488 	}
4489 	return;
4490 }
4491 
4492 /**
4493  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
4494  * @t: pointer to the timer function associated data (ndlp).
4495  *
4496  * This routine is invoked by the ndlp delayed-function timer to check
4497  * whether there is any pending ELS retry event(s) with the node. If not, it
4498  * simply returns. Otherwise, if there is at least one ELS delayed event, it
4499  * adds the delayed events to the HBA work list and invokes the
4500  * lpfc_worker_wake_up() routine to wake up worker thread to process the
4501  * event. Note that lpfc_nlp_get() is called before posting the event to
4502  * the work list to hold reference count of ndlp so that it guarantees the
4503  * reference to ndlp will still be available when the worker thread gets
4504  * to the event associated with the ndlp.
4505  **/
4506 void
4507 lpfc_els_retry_delay(struct timer_list *t)
4508 {
4509 	struct lpfc_nodelist *ndlp = timer_container_of(ndlp, t,
4510 							nlp_delayfunc);
4511 	struct lpfc_vport *vport = ndlp->vport;
4512 	struct lpfc_hba   *phba = vport->phba;
4513 	unsigned long flags;
4514 	struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
4515 
4516 	/* Hold a node reference for outstanding queued work */
4517 	if (!lpfc_nlp_get(ndlp))
4518 		return;
4519 
4520 	spin_lock_irqsave(&phba->hbalock, flags);
4521 	if (!list_empty(&evtp->evt_listp)) {
4522 		spin_unlock_irqrestore(&phba->hbalock, flags);
4523 		lpfc_nlp_put(ndlp);
4524 		return;
4525 	}
4526 
4527 	evtp->evt_arg1 = ndlp;
4528 	evtp->evt = LPFC_EVT_ELS_RETRY;
4529 	list_add_tail(&evtp->evt_listp, &phba->work_list);
4530 	spin_unlock_irqrestore(&phba->hbalock, flags);
4531 
4532 	lpfc_worker_wake_up(phba);
4533 }
4534 
4535 /**
4536  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
4537  * @ndlp: pointer to a node-list data structure.
4538  *
4539  * This routine is the worker-thread handler for processing the @ndlp delayed
4540  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
4541  * the last ELS command from the associated ndlp and invokes the proper ELS
4542  * function according to the delayed ELS command to retry the command.
4543  **/
4544 void
4545 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
4546 {
4547 	struct lpfc_vport *vport = ndlp->vport;
4548 	uint32_t cmd, retry;
4549 
4550 	spin_lock_irq(&ndlp->lock);
4551 	cmd = ndlp->nlp_last_elscmd;
4552 	ndlp->nlp_last_elscmd = 0;
4553 	spin_unlock_irq(&ndlp->lock);
4554 
4555 	if (!test_and_clear_bit(NLP_DELAY_TMO, &ndlp->nlp_flag))
4556 		return;
4557 
4558 	/*
4559 	 * If a discovery event readded nlp_delayfunc after timer
4560 	 * firing and before processing the timer, cancel the
4561 	 * nlp_delayfunc.
4562 	 */
4563 	timer_delete_sync(&ndlp->nlp_delayfunc);
4564 	retry = ndlp->nlp_retry;
4565 	ndlp->nlp_retry = 0;
4566 
4567 	switch (cmd) {
4568 	case ELS_CMD_FLOGI:
4569 		lpfc_issue_els_flogi(vport, ndlp, retry);
4570 		break;
4571 	case ELS_CMD_PLOGI:
4572 		if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
4573 			ndlp->nlp_prev_state = ndlp->nlp_state;
4574 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4575 		}
4576 		break;
4577 	case ELS_CMD_ADISC:
4578 		if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
4579 			ndlp->nlp_prev_state = ndlp->nlp_state;
4580 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4581 		}
4582 		break;
4583 	case ELS_CMD_PRLI:
4584 	case ELS_CMD_NVMEPRLI:
4585 		if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
4586 			ndlp->nlp_prev_state = ndlp->nlp_state;
4587 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
4588 		}
4589 		break;
4590 	case ELS_CMD_LOGO:
4591 		if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
4592 			ndlp->nlp_prev_state = ndlp->nlp_state;
4593 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4594 		}
4595 		break;
4596 	case ELS_CMD_FDISC:
4597 		if (!test_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag))
4598 			lpfc_issue_els_fdisc(vport, ndlp, retry);
4599 		break;
4600 	}
4601 	return;
4602 }
4603 
4604 /**
4605  * lpfc_link_reset - Issue link reset
4606  * @vport: pointer to a virtual N_Port data structure.
4607  *
4608  * This routine performs link reset by sending INIT_LINK mailbox command.
4609  * For SLI-3 adapter, link attention interrupt is enabled before issuing
4610  * INIT_LINK mailbox command.
4611  *
4612  * Return code
4613  *   0 - Link reset initiated successfully
4614  *   1 - Failed to initiate link reset
4615  **/
4616 int
4617 lpfc_link_reset(struct lpfc_vport *vport)
4618 {
4619 	struct lpfc_hba *phba = vport->phba;
4620 	LPFC_MBOXQ_t *mbox;
4621 	uint32_t control;
4622 	int rc;
4623 
4624 	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4625 			 "2851 Attempt link reset\n");
4626 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4627 	if (!mbox) {
4628 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4629 				"2852 Failed to allocate mbox memory");
4630 		return 1;
4631 	}
4632 
4633 	/* Enable Link attention interrupts */
4634 	if (phba->sli_rev <= LPFC_SLI_REV3) {
4635 		spin_lock_irq(&phba->hbalock);
4636 		phba->sli.sli_flag |= LPFC_PROCESS_LA;
4637 		control = readl(phba->HCregaddr);
4638 		control |= HC_LAINT_ENA;
4639 		writel(control, phba->HCregaddr);
4640 		readl(phba->HCregaddr); /* flush */
4641 		spin_unlock_irq(&phba->hbalock);
4642 	}
4643 
4644 	lpfc_init_link(phba, mbox, phba->cfg_topology,
4645 		       phba->cfg_link_speed);
4646 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4647 	mbox->vport = vport;
4648 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4649 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
4650 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4651 				"2853 Failed to issue INIT_LINK "
4652 				"mbox command, rc:x%x\n", rc);
4653 		mempool_free(mbox, phba->mbox_mem_pool);
4654 		return 1;
4655 	}
4656 
4657 	return 0;
4658 }
4659 
4660 /**
4661  * lpfc_els_retry - Make retry decision on an els command iocb
4662  * @phba: pointer to lpfc hba data structure.
4663  * @cmdiocb: pointer to lpfc command iocb data structure.
4664  * @rspiocb: pointer to lpfc response iocb data structure.
4665  *
4666  * This routine makes a retry decision on an ELS command IOCB, which has
4667  * failed. The following ELS IOCBs use this function for retrying the command
4668  * when previously issued command responsed with error status: FLOGI, PLOGI,
4669  * PRLI, ADISC and FDISC. Based on the ELS command type and the
4670  * returned error status, it makes the decision whether a retry shall be
4671  * issued for the command, and whether a retry shall be made immediately or
4672  * delayed. In the former case, the corresponding ELS command issuing-function
4673  * is called to retry the command. In the later case, the ELS command shall
4674  * be posted to the ndlp delayed event and delayed function timer set to the
4675  * ndlp for the delayed command issusing.
4676  *
4677  * Return code
4678  *   0 - No retry of els command is made
4679  *   1 - Immediate or delayed retry of els command is made
4680  **/
4681 static int
4682 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4683 	       struct lpfc_iocbq *rspiocb)
4684 {
4685 	struct lpfc_vport *vport = cmdiocb->vport;
4686 	union lpfc_wqe128 *irsp = &rspiocb->wqe;
4687 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
4688 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
4689 	uint32_t *elscmd;
4690 	struct ls_rjt stat;
4691 	int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
4692 	int logerr = 0;
4693 	uint32_t cmd = 0;
4694 	uint32_t did;
4695 	int link_reset = 0, rc;
4696 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
4697 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
4698 	u8 rsn_code_exp = 0;
4699 
4700 
4701 	/* Note: cmd_dmabuf may be 0 for internal driver abort
4702 	 * of delays ELS command.
4703 	 */
4704 
4705 	if (pcmd && pcmd->virt) {
4706 		elscmd = (uint32_t *) (pcmd->virt);
4707 		cmd = *elscmd++;
4708 	}
4709 
4710 	if (ndlp)
4711 		did = ndlp->nlp_DID;
4712 	else {
4713 		/* We should only hit this case for retrying PLOGI */
4714 		did = get_job_els_rsp64_did(phba, rspiocb);
4715 		ndlp = lpfc_findnode_did(vport, did);
4716 		if (!ndlp && (cmd != ELS_CMD_PLOGI))
4717 			return 0;
4718 	}
4719 
4720 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4721 		"Retry ELS:       wd7:x%x wd4:x%x did:x%x",
4722 		*(((uint32_t *)irsp) + 7), ulp_word4, did);
4723 
4724 	switch (ulp_status) {
4725 	case IOSTAT_FCP_RSP_ERROR:
4726 		break;
4727 	case IOSTAT_REMOTE_STOP:
4728 		if (phba->sli_rev == LPFC_SLI_REV4) {
4729 			/* This IO was aborted by the target, we don't
4730 			 * know the rxid and because we did not send the
4731 			 * ABTS we cannot generate and RRQ.
4732 			 */
4733 			lpfc_set_rrq_active(phba, ndlp,
4734 					 cmdiocb->sli4_lxritag, 0, 0);
4735 		}
4736 		break;
4737 	case IOSTAT_LOCAL_REJECT:
4738 		switch ((ulp_word4 & IOERR_PARAM_MASK)) {
4739 		case IOERR_LOOP_OPEN_FAILURE:
4740 			if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
4741 				delay = 1000;
4742 			retry = 1;
4743 			break;
4744 
4745 		case IOERR_ILLEGAL_COMMAND:
4746 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4747 					 "0124 Retry illegal cmd x%x "
4748 					 "retry:x%x delay:x%x\n",
4749 					 cmd, cmdiocb->retry, delay);
4750 			retry = 1;
4751 			/* All command's retry policy */
4752 			maxretry = 8;
4753 			if (cmdiocb->retry > 2)
4754 				delay = 1000;
4755 			break;
4756 
4757 		case IOERR_NO_RESOURCES:
4758 			logerr = 1; /* HBA out of resources */
4759 			retry = 1;
4760 			if (cmdiocb->retry > 100)
4761 				delay = 100;
4762 			maxretry = 250;
4763 			break;
4764 
4765 		case IOERR_ILLEGAL_FRAME:
4766 			delay = 100;
4767 			retry = 1;
4768 			break;
4769 
4770 		case IOERR_INVALID_RPI:
4771 			if (cmd == ELS_CMD_PLOGI &&
4772 			    did == NameServer_DID) {
4773 				/* Continue forever if plogi to */
4774 				/* the nameserver fails */
4775 				maxretry = 0;
4776 				delay = 100;
4777 			} else if (cmd == ELS_CMD_PRLI &&
4778 				   ndlp->nlp_state != NLP_STE_PRLI_ISSUE) {
4779 				/* State-command disagreement.  The PRLI was
4780 				 * failed with an invalid rpi meaning there
4781 				 * some unexpected state change.  Don't retry.
4782 				 */
4783 				maxretry = 0;
4784 				retry = 0;
4785 				break;
4786 			}
4787 			retry = 1;
4788 			break;
4789 
4790 		case IOERR_SEQUENCE_TIMEOUT:
4791 			if (cmd == ELS_CMD_PLOGI &&
4792 			    did == NameServer_DID &&
4793 			    (cmdiocb->retry + 1) == maxretry) {
4794 				/* Reset the Link */
4795 				link_reset = 1;
4796 				break;
4797 			}
4798 			retry = 1;
4799 			delay = 100;
4800 			break;
4801 		case IOERR_SLI_ABORTED:
4802 			/* Retry ELS PLOGI command?
4803 			 * Possibly the rport just wasn't ready.
4804 			 */
4805 			if (cmd == ELS_CMD_PLOGI) {
4806 				/* No retry if state change */
4807 				if (ndlp &&
4808 				    ndlp->nlp_state != NLP_STE_PLOGI_ISSUE)
4809 					goto out_retry;
4810 				retry = 1;
4811 				maxretry = 2;
4812 			}
4813 			break;
4814 		}
4815 		break;
4816 
4817 	case IOSTAT_NPORT_RJT:
4818 	case IOSTAT_FABRIC_RJT:
4819 		if (ulp_word4 & RJT_UNAVAIL_TEMP) {
4820 			retry = 1;
4821 			break;
4822 		}
4823 		break;
4824 
4825 	case IOSTAT_NPORT_BSY:
4826 	case IOSTAT_FABRIC_BSY:
4827 		logerr = 1; /* Fabric / Remote NPort out of resources */
4828 		retry = 1;
4829 		break;
4830 
4831 	case IOSTAT_LS_RJT:
4832 		stat.un.ls_rjt_error_be = cpu_to_be32(ulp_word4);
4833 		/* Added for Vendor specifc support
4834 		 * Just keep retrying for these Rsn / Exp codes
4835 		 */
4836 		if (test_bit(FC_PT2PT, &vport->fc_flag) &&
4837 		    cmd == ELS_CMD_NVMEPRLI) {
4838 			switch (stat.un.b.lsRjtRsnCode) {
4839 			case LSRJT_UNABLE_TPC:
4840 			case LSRJT_INVALID_CMD:
4841 			case LSRJT_LOGICAL_ERR:
4842 			case LSRJT_CMD_UNSUPPORTED:
4843 				lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
4844 						 "0168 NVME PRLI LS_RJT "
4845 						 "reason %x port doesn't "
4846 						 "support NVME, disabling NVME\n",
4847 						 stat.un.b.lsRjtRsnCode);
4848 				retry = 0;
4849 				set_bit(FC_PT2PT_NO_NVME, &vport->fc_flag);
4850 				goto out_retry;
4851 			}
4852 		}
4853 		switch (stat.un.b.lsRjtRsnCode) {
4854 		case LSRJT_UNABLE_TPC:
4855 			/* Special case for PRLI LS_RJTs. Recall that lpfc
4856 			 * uses a single routine to issue both PRLI FC4 types.
4857 			 * If the PRLI is rejected because that FC4 type
4858 			 * isn't really supported, don't retry and cause
4859 			 * multiple transport registrations.  Otherwise, parse
4860 			 * the reason code/reason code explanation and take the
4861 			 * appropriate action.
4862 			 */
4863 			lpfc_printf_vlog(vport, KERN_INFO,
4864 					 LOG_DISCOVERY | LOG_ELS | LOG_NODE,
4865 					 "0153 ELS cmd x%x LS_RJT by x%x. "
4866 					 "RsnCode x%x RsnCodeExp x%x\n",
4867 					 cmd, did, stat.un.b.lsRjtRsnCode,
4868 					 stat.un.b.lsRjtRsnCodeExp);
4869 
4870 			switch (stat.un.b.lsRjtRsnCodeExp) {
4871 			case LSEXP_CANT_GIVE_DATA:
4872 			case LSEXP_CMD_IN_PROGRESS:
4873 				if (cmd == ELS_CMD_PLOGI) {
4874 					delay = 1000;
4875 					maxretry = 48;
4876 				}
4877 				retry = 1;
4878 				break;
4879 			case LSEXP_REQ_UNSUPPORTED:
4880 			case LSEXP_NO_RSRC_ASSIGN:
4881 				/* These explanation codes get no retry. */
4882 				if (cmd == ELS_CMD_PRLI ||
4883 				    cmd == ELS_CMD_NVMEPRLI)
4884 					break;
4885 				fallthrough;
4886 			default:
4887 				/* Limit the delay and retry action to a limited
4888 				 * cmd set.  There are other ELS commands where
4889 				 * a retry is not expected.
4890 				 */
4891 				if (cmd == ELS_CMD_PLOGI ||
4892 				    cmd == ELS_CMD_PRLI ||
4893 				    cmd == ELS_CMD_NVMEPRLI) {
4894 					delay = 1000;
4895 					maxretry = lpfc_max_els_tries + 1;
4896 					retry = 1;
4897 				}
4898 				break;
4899 			}
4900 
4901 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4902 			  (cmd == ELS_CMD_FDISC) &&
4903 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
4904 				lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
4905 					      "0125 FDISC (x%x). "
4906 					      "Fabric out of resources\n",
4907 					      stat.un.lsRjtError);
4908 				lpfc_vport_set_state(vport,
4909 						     FC_VPORT_NO_FABRIC_RSCS);
4910 			}
4911 			break;
4912 
4913 		case LSRJT_LOGICAL_BSY:
4914 			rsn_code_exp = stat.un.b.lsRjtRsnCodeExp;
4915 			if ((cmd == ELS_CMD_PLOGI) ||
4916 			    (cmd == ELS_CMD_PRLI) ||
4917 			    (cmd == ELS_CMD_NVMEPRLI)) {
4918 				delay = 1000;
4919 				maxretry = 48;
4920 
4921 				/* An authentication LS_RJT reason code
4922 				 * explanation means some error in the
4923 				 * security settings end-to-end.  Reduce
4924 				 * the retry count to allow lpfc to clear
4925 				 * RSCN mode and not race with dev_loss.
4926 				 */
4927 				if (cmd == ELS_CMD_PLOGI &&
4928 				    rsn_code_exp == LSEXP_AUTH_REQ)
4929 					maxretry = 8;
4930 			} else if (cmd == ELS_CMD_FDISC) {
4931 				/* FDISC retry policy */
4932 				maxretry = 48;
4933 				if (cmdiocb->retry >= 32)
4934 					delay = 1000;
4935 			}
4936 			retry = 1;
4937 			break;
4938 
4939 		case LSRJT_LOGICAL_ERR:
4940 			/* There are some cases where switches return this
4941 			 * error when they are not ready and should be returning
4942 			 * Logical Busy. We should delay every time.
4943 			 */
4944 			if (cmd == ELS_CMD_FDISC &&
4945 			    stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
4946 				maxretry = 3;
4947 				delay = 1000;
4948 				retry = 1;
4949 			} else if (cmd == ELS_CMD_FLOGI &&
4950 				   stat.un.b.lsRjtRsnCodeExp ==
4951 						LSEXP_NOTHING_MORE) {
4952 				vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
4953 				retry = 1;
4954 				lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
4955 					      "0820 FLOGI (x%x). "
4956 					      "BBCredit Not Supported\n",
4957 					      stat.un.lsRjtError);
4958 			} else if (cmd == ELS_CMD_PLOGI) {
4959 				rsn_code_exp = stat.un.b.lsRjtRsnCodeExp;
4960 
4961 				/* An authentication LS_RJT reason code
4962 				 * explanation means some error in the
4963 				 * security settings end-to-end.  Reduce
4964 				 * the retry count to allow lpfc to clear
4965 				 * RSCN mode and not race with dev_loss.
4966 				 */
4967 				if (rsn_code_exp == LSEXP_AUTH_REQ) {
4968 					delay = 1000;
4969 					retry = 1;
4970 					maxretry = 8;
4971 				}
4972 			}
4973 			break;
4974 
4975 		case LSRJT_PROTOCOL_ERR:
4976 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4977 			  (cmd == ELS_CMD_FDISC) &&
4978 			  ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
4979 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
4980 			  ) {
4981 				lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
4982 					      "0122 FDISC (x%x). "
4983 					      "Fabric Detected Bad WWN\n",
4984 					      stat.un.lsRjtError);
4985 				lpfc_vport_set_state(vport,
4986 						     FC_VPORT_FABRIC_REJ_WWN);
4987 			}
4988 			break;
4989 		case LSRJT_VENDOR_UNIQUE:
4990 			if ((stat.un.b.vendorUnique == 0x45) &&
4991 			    (cmd == ELS_CMD_FLOGI)) {
4992 				goto out_retry;
4993 			}
4994 			break;
4995 		case LSRJT_CMD_UNSUPPORTED:
4996 			/* lpfc nvmet returns this type of LS_RJT when it
4997 			 * receives an FCP PRLI because lpfc nvmet only
4998 			 * support NVME.  ELS request is terminated for FCP4
4999 			 * on this rport.
5000 			 */
5001 			if (stat.un.b.lsRjtRsnCodeExp ==
5002 			    LSEXP_REQ_UNSUPPORTED) {
5003 				if (cmd == ELS_CMD_PRLI)
5004 					goto out_retry;
5005 			}
5006 			break;
5007 		}
5008 		break;
5009 
5010 	case IOSTAT_INTERMED_RSP:
5011 	case IOSTAT_BA_RJT:
5012 		break;
5013 
5014 	default:
5015 		break;
5016 	}
5017 
5018 	if (link_reset) {
5019 		rc = lpfc_link_reset(vport);
5020 		if (rc) {
5021 			/* Do not give up. Retry PLOGI one more time and attempt
5022 			 * link reset if PLOGI fails again.
5023 			 */
5024 			retry = 1;
5025 			delay = 100;
5026 			goto out_retry;
5027 		}
5028 		return 1;
5029 	}
5030 
5031 	if (did == FDMI_DID)
5032 		retry = 1;
5033 
5034 	if ((cmd == ELS_CMD_FLOGI) &&
5035 	    (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
5036 	    !lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
5037 		/* FLOGI retry policy */
5038 		retry = 1;
5039 		/* retry FLOGI forever */
5040 		if (phba->link_flag != LS_LOOPBACK_MODE)
5041 			maxretry = 0;
5042 		else
5043 			maxretry = 2;
5044 
5045 		if (cmdiocb->retry >= 100)
5046 			delay = 5000;
5047 		else if (cmdiocb->retry >= 32)
5048 			delay = 1000;
5049 	} else if ((cmd == ELS_CMD_FDISC) &&
5050 	    !lpfc_error_lost_link(vport, ulp_status, ulp_word4)) {
5051 		/* retry FDISCs every second up to devloss */
5052 		retry = 1;
5053 		maxretry = vport->cfg_devloss_tmo;
5054 		delay = 1000;
5055 	}
5056 
5057 	cmdiocb->retry++;
5058 	if (maxretry && (cmdiocb->retry >= maxretry)) {
5059 		phba->fc_stat.elsRetryExceeded++;
5060 		retry = 0;
5061 	}
5062 
5063 	if (test_bit(FC_UNLOADING, &vport->load_flag))
5064 		retry = 0;
5065 
5066 out_retry:
5067 	if (retry) {
5068 		if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
5069 			/* Stop retrying PLOGI and FDISC if in FCF discovery */
5070 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
5071 				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5072 						 "2849 Stop retry ELS command "
5073 						 "x%x to remote NPORT x%x, "
5074 						 "Data: x%x x%x\n", cmd, did,
5075 						 cmdiocb->retry, delay);
5076 				return 0;
5077 			}
5078 		}
5079 
5080 		/* Retry ELS command <elsCmd> to remote NPORT <did> */
5081 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5082 				 "0107 Retry ELS command x%x to remote "
5083 				 "NPORT x%x Data: x%x x%x\n",
5084 				 cmd, did, cmdiocb->retry, delay);
5085 
5086 		if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
5087 			((ulp_status != IOSTAT_LOCAL_REJECT) ||
5088 			((ulp_word4 & IOERR_PARAM_MASK) !=
5089 			IOERR_NO_RESOURCES))) {
5090 			/* Don't reset timer for no resources */
5091 
5092 			/* If discovery / RSCN timer is running, reset it */
5093 			if (timer_pending(&vport->fc_disctmo) ||
5094 			    test_bit(FC_RSCN_MODE, &vport->fc_flag))
5095 				lpfc_set_disctmo(vport);
5096 		}
5097 
5098 		phba->fc_stat.elsXmitRetry++;
5099 		if (ndlp && delay) {
5100 			phba->fc_stat.elsDelayRetry++;
5101 			ndlp->nlp_retry = cmdiocb->retry;
5102 
5103 			/* delay is specified in milliseconds */
5104 			mod_timer(&ndlp->nlp_delayfunc,
5105 				jiffies + msecs_to_jiffies(delay));
5106 			set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
5107 
5108 			ndlp->nlp_prev_state = ndlp->nlp_state;
5109 			if ((cmd == ELS_CMD_PRLI) ||
5110 			    (cmd == ELS_CMD_NVMEPRLI))
5111 				lpfc_nlp_set_state(vport, ndlp,
5112 					NLP_STE_PRLI_ISSUE);
5113 			else if (cmd != ELS_CMD_ADISC)
5114 				lpfc_nlp_set_state(vport, ndlp,
5115 					NLP_STE_NPR_NODE);
5116 			ndlp->nlp_last_elscmd = cmd;
5117 
5118 			return 1;
5119 		}
5120 		switch (cmd) {
5121 		case ELS_CMD_FLOGI:
5122 			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
5123 			return 1;
5124 		case ELS_CMD_FDISC:
5125 			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
5126 			return 1;
5127 		case ELS_CMD_PLOGI:
5128 			if (ndlp) {
5129 				ndlp->nlp_prev_state = ndlp->nlp_state;
5130 				lpfc_nlp_set_state(vport, ndlp,
5131 						   NLP_STE_PLOGI_ISSUE);
5132 			}
5133 			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
5134 			return 1;
5135 		case ELS_CMD_ADISC:
5136 			ndlp->nlp_prev_state = ndlp->nlp_state;
5137 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
5138 			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
5139 			return 1;
5140 		case ELS_CMD_PRLI:
5141 		case ELS_CMD_NVMEPRLI:
5142 			ndlp->nlp_prev_state = ndlp->nlp_state;
5143 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
5144 			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
5145 			return 1;
5146 		case ELS_CMD_LOGO:
5147 			ndlp->nlp_prev_state = ndlp->nlp_state;
5148 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
5149 			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
5150 			return 1;
5151 		}
5152 	}
5153 	/* No retry ELS command <elsCmd> to remote NPORT <did> */
5154 	if (logerr) {
5155 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5156 			 "0137 No retry ELS command x%x to remote "
5157 			 "NPORT x%x: Out of Resources: Error:x%x/%x "
5158 			 "IoTag x%x\n",
5159 			 cmd, did, ulp_status, ulp_word4,
5160 			 cmdiocb->iotag);
5161 	}
5162 	else {
5163 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5164 				 "0108 No retry ELS command x%x to remote "
5165 				 "NPORT x%x Retried:%d Error:x%x/%x "
5166 				 "IoTag x%x nflags x%lx\n",
5167 				 cmd, did, cmdiocb->retry, ulp_status,
5168 				 ulp_word4, cmdiocb->iotag,
5169 				 (ndlp ? ndlp->nlp_flag : 0));
5170 	}
5171 	return 0;
5172 }
5173 
5174 /**
5175  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
5176  * @phba: pointer to lpfc hba data structure.
5177  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
5178  *
5179  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
5180  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
5181  * checks to see whether there is a lpfc DMA buffer associated with the
5182  * response of the command IOCB. If so, it will be released before releasing
5183  * the lpfc DMA buffer associated with the IOCB itself.
5184  *
5185  * Return code
5186  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
5187  **/
5188 static int
5189 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
5190 {
5191 	struct lpfc_dmabuf *buf_ptr;
5192 
5193 	/* Free the response before processing the command. */
5194 	if (!list_empty(&buf_ptr1->list)) {
5195 		list_remove_head(&buf_ptr1->list, buf_ptr,
5196 				 struct lpfc_dmabuf,
5197 				 list);
5198 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
5199 		kfree(buf_ptr);
5200 	}
5201 	lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
5202 	kfree(buf_ptr1);
5203 	return 0;
5204 }
5205 
5206 /**
5207  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
5208  * @phba: pointer to lpfc hba data structure.
5209  * @buf_ptr: pointer to the lpfc dma buffer data structure.
5210  *
5211  * This routine releases the lpfc Direct Memory Access (DMA) buffer
5212  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
5213  * pool.
5214  *
5215  * Return code
5216  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
5217  **/
5218 static int
5219 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
5220 {
5221 	lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
5222 	kfree(buf_ptr);
5223 	return 0;
5224 }
5225 
5226 /**
5227  * lpfc_els_free_iocb - Free a command iocb and its associated resources
5228  * @phba: pointer to lpfc hba data structure.
5229  * @elsiocb: pointer to lpfc els command iocb data structure.
5230  *
5231  * This routine frees a command IOCB and its associated resources. The
5232  * command IOCB data structure contains the reference to various associated
5233  * resources, these fields must be set to NULL if the associated reference
5234  * not present:
5235  *   cmd_dmabuf - reference to cmd.
5236  *   cmd_dmabuf->next - reference to rsp
5237  *   rsp_dmabuf - unused
5238  *   bpl_dmabuf - reference to bpl
5239  *
5240  * It first properly decrements the reference count held on ndlp for the
5241  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
5242  * set, it invokes the lpfc_els_free_data() routine to release the Direct
5243  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
5244  * adds the DMA buffer the @phba data structure for the delayed release.
5245  * If reference to the Buffer Pointer List (BPL) is present, the
5246  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
5247  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
5248  * invoked to release the IOCB data structure back to @phba IOCBQ list.
5249  *
5250  * Return code
5251  *   0 - Success (currently, always return 0)
5252  **/
5253 int
5254 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
5255 {
5256 	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
5257 
5258 	/* The I/O iocb is complete.  Clear the node and first dmabuf */
5259 	elsiocb->ndlp = NULL;
5260 
5261 	/* cmd_dmabuf = cmd,  cmd_dmabuf->next = rsp, bpl_dmabuf = bpl */
5262 	if (elsiocb->cmd_dmabuf) {
5263 		if (elsiocb->cmd_flag & LPFC_DELAY_MEM_FREE) {
5264 			/* Firmware could still be in progress of DMAing
5265 			 * payload, so don't free data buffer till after
5266 			 * a hbeat.
5267 			 */
5268 			elsiocb->cmd_flag &= ~LPFC_DELAY_MEM_FREE;
5269 			buf_ptr = elsiocb->cmd_dmabuf;
5270 			elsiocb->cmd_dmabuf = NULL;
5271 			if (buf_ptr) {
5272 				buf_ptr1 = NULL;
5273 				spin_lock_irq(&phba->hbalock);
5274 				if (!list_empty(&buf_ptr->list)) {
5275 					list_remove_head(&buf_ptr->list,
5276 						buf_ptr1, struct lpfc_dmabuf,
5277 						list);
5278 					INIT_LIST_HEAD(&buf_ptr1->list);
5279 					list_add_tail(&buf_ptr1->list,
5280 						&phba->elsbuf);
5281 					phba->elsbuf_cnt++;
5282 				}
5283 				INIT_LIST_HEAD(&buf_ptr->list);
5284 				list_add_tail(&buf_ptr->list, &phba->elsbuf);
5285 				phba->elsbuf_cnt++;
5286 				spin_unlock_irq(&phba->hbalock);
5287 			}
5288 		} else {
5289 			buf_ptr1 = elsiocb->cmd_dmabuf;
5290 			lpfc_els_free_data(phba, buf_ptr1);
5291 		}
5292 	}
5293 
5294 	if (elsiocb->bpl_dmabuf) {
5295 		buf_ptr = elsiocb->bpl_dmabuf;
5296 		lpfc_els_free_bpl(phba, buf_ptr);
5297 	}
5298 	lpfc_sli_release_iocbq(phba, elsiocb);
5299 	return 0;
5300 }
5301 
5302 /**
5303  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
5304  * @phba: pointer to lpfc hba data structure.
5305  * @cmdiocb: pointer to lpfc command iocb data structure.
5306  * @rspiocb: pointer to lpfc response iocb data structure.
5307  *
5308  * This routine is the completion callback function to the Logout (LOGO)
5309  * Accept (ACC) Response ELS command. This routine is invoked to indicate
5310  * the completion of the LOGO process. If the node has transitioned to NPR,
5311  * this routine unregisters the RPI if it is still registered. The
5312  * lpfc_els_free_iocb() is invoked to release the IOCB data structure.
5313  **/
5314 static void
5315 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5316 		       struct lpfc_iocbq *rspiocb)
5317 {
5318 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
5319 	struct lpfc_vport *vport = cmdiocb->vport;
5320 	u32 ulp_status, ulp_word4;
5321 
5322 	ulp_status = get_job_ulpstatus(phba, rspiocb);
5323 	ulp_word4 = get_job_word4(phba, rspiocb);
5324 
5325 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5326 		"ACC LOGO cmpl:   status:x%x/x%x did:x%x",
5327 		ulp_status, ulp_word4, ndlp->nlp_DID);
5328 	/* ACC to LOGO completes to NPort <nlp_DID> */
5329 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5330 			 "0109 ACC to LOGO completes to NPort x%x refcnt %d "
5331 			 "last els x%x Data: x%lx x%x x%x\n",
5332 			 ndlp->nlp_DID, kref_read(&ndlp->kref),
5333 			 ndlp->nlp_last_elscmd, ndlp->nlp_flag, ndlp->nlp_state,
5334 			 ndlp->nlp_rpi);
5335 
5336 	/* This clause allows the LOGO ACC to complete and free resources
5337 	 * for the Fabric Domain Controller.  It does deliberately skip
5338 	 * the unreg_rpi and release rpi because some fabrics send RDP
5339 	 * requests after logging out from the initiator.
5340 	 */
5341 	if (ndlp->nlp_type & NLP_FABRIC &&
5342 	    ((ndlp->nlp_DID & WELL_KNOWN_DID_MASK) != WELL_KNOWN_DID_MASK))
5343 		goto out;
5344 
5345 	if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
5346 		if (test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag))
5347 			lpfc_unreg_rpi(vport, ndlp);
5348 
5349 		/* If came from PRLO, then PRLO_ACC is done.
5350 		 * Start rediscovery now.
5351 		 */
5352 		if (ndlp->nlp_last_elscmd == ELS_CMD_PRLO) {
5353 			set_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag);
5354 			ndlp->nlp_prev_state = ndlp->nlp_state;
5355 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5356 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5357 		}
5358 	}
5359 
5360  out:
5361 	/*
5362 	 * The driver received a LOGO from the rport and has ACK'd it.
5363 	 * At this point, the driver is done so release the IOCB
5364 	 */
5365 	lpfc_els_free_iocb(phba, cmdiocb);
5366 	lpfc_nlp_put(ndlp);
5367 }
5368 
5369 /**
5370  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
5371  * @phba: pointer to lpfc hba data structure.
5372  * @pmb: pointer to the driver internal queue element for mailbox command.
5373  *
5374  * This routine is the completion callback function for unregister default
5375  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
5376  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
5377  * decrements the ndlp reference count held for this completion callback
5378  * function. After that, it invokes the lpfc_drop_node to check
5379  * whether it is appropriate to release the node.
5380  **/
5381 void
5382 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5383 {
5384 	struct lpfc_nodelist *ndlp = pmb->ctx_ndlp;
5385 	u32 mbx_flag = pmb->mbox_flag;
5386 	u32 mbx_cmd = pmb->u.mb.mbxCommand;
5387 
5388 	if (ndlp) {
5389 		lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
5390 				 "0006 rpi x%x DID:%x flg:%lx %d x%px "
5391 				 "mbx_cmd x%x mbx_flag x%x x%px\n",
5392 				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5393 				 kref_read(&ndlp->kref), ndlp, mbx_cmd,
5394 				 mbx_flag, pmb);
5395 
5396 		/* This ends the default/temporary RPI cleanup logic for this
5397 		 * ndlp and the node and rpi needs to be released. Free the rpi
5398 		 * first on an UNREG_LOGIN and then release the final
5399 		 * references.
5400 		 */
5401 		clear_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
5402 		if (mbx_cmd == MBX_UNREG_LOGIN)
5403 			clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
5404 		lpfc_nlp_put(ndlp);
5405 		lpfc_drop_node(ndlp->vport, ndlp);
5406 	}
5407 
5408 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
5409 }
5410 
5411 /**
5412  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
5413  * @phba: pointer to lpfc hba data structure.
5414  * @cmdiocb: pointer to lpfc command iocb data structure.
5415  * @rspiocb: pointer to lpfc response iocb data structure.
5416  *
5417  * This routine is the completion callback function for ELS Response IOCB
5418  * command. In normal case, this callback function just properly sets the
5419  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
5420  * field in the command IOCB is not NULL, the referred mailbox command will
5421  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
5422  * the IOCB.
5423  **/
5424 static void
5425 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5426 		  struct lpfc_iocbq *rspiocb)
5427 {
5428 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
5429 	struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
5430 	struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
5431 	IOCB_t  *irsp;
5432 	LPFC_MBOXQ_t *mbox = NULL;
5433 	u32 ulp_status, ulp_word4, tmo, did, iotag;
5434 	u32 cmd;
5435 
5436 	if (!vport) {
5437 		lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
5438 				"3177 null vport in ELS rsp\n");
5439 		goto release;
5440 	}
5441 	if (cmdiocb->context_un.mbox)
5442 		mbox = cmdiocb->context_un.mbox;
5443 
5444 	ulp_status = get_job_ulpstatus(phba, rspiocb);
5445 	ulp_word4 = get_job_word4(phba, rspiocb);
5446 	did = get_job_els_rsp64_did(phba, cmdiocb);
5447 
5448 	if (phba->sli_rev == LPFC_SLI_REV4) {
5449 		tmo = get_wqe_tmo(cmdiocb);
5450 		iotag = get_wqe_reqtag(cmdiocb);
5451 	} else {
5452 		irsp = &rspiocb->iocb;
5453 		tmo = irsp->ulpTimeout;
5454 		iotag = irsp->ulpIoTag;
5455 	}
5456 
5457 	/* Check to see if link went down during discovery */
5458 	if (!ndlp || lpfc_els_chk_latt(vport)) {
5459 		if (mbox)
5460 			lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
5461 		goto out;
5462 	}
5463 
5464 	if (!ulp_status && test_bit(NLP_RCV_PLOGI, &ndlp->nlp_flag))
5465 		lpfc_check_encryption(phba, ndlp, cmdiocb, rspiocb);
5466 
5467 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5468 		"ELS rsp cmpl:    status:x%x/x%x did:x%x",
5469 		ulp_status, ulp_word4, did);
5470 	/* ELS response tag <ulpIoTag> completes */
5471 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5472 			 "0110 ELS response tag x%x completes fc_flag x%lx"
5473 			 "Data: x%x x%x x%x x%x x%lx x%x x%x x%x %p %p\n",
5474 			 iotag, vport->fc_flag, ulp_status, ulp_word4, tmo,
5475 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5476 			 ndlp->nlp_rpi, kref_read(&ndlp->kref), mbox, ndlp);
5477 	if (mbox && !test_bit(FC_PT2PT, &vport->fc_flag)) {
5478 		if (ulp_status == 0 &&
5479 		    test_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag)) {
5480 			if (!lpfc_unreg_rpi(vport, ndlp) &&
5481 			    !test_bit(FC_PT2PT, &vport->fc_flag)) {
5482 				if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5483 				    ndlp->nlp_state ==
5484 				     NLP_STE_REG_LOGIN_ISSUE) {
5485 					lpfc_printf_vlog(vport, KERN_INFO,
5486 							 LOG_DISCOVERY,
5487 							 "0314 PLOGI recov "
5488 							 "DID x%x "
5489 							 "Data: x%x x%x x%lx\n",
5490 							 ndlp->nlp_DID,
5491 							 ndlp->nlp_state,
5492 							 ndlp->nlp_rpi,
5493 							 ndlp->nlp_flag);
5494 					goto out_free_mbox;
5495 				}
5496 			}
5497 
5498 			/* Increment reference count to ndlp to hold the
5499 			 * reference to ndlp for the callback function.
5500 			 */
5501 			mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
5502 			if (!mbox->ctx_ndlp)
5503 				goto out_free_mbox;
5504 
5505 			mbox->vport = vport;
5506 			if (test_bit(NLP_RM_DFLT_RPI, &ndlp->nlp_flag)) {
5507 				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5508 				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5509 			} else {
5510 				mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
5511 				ndlp->nlp_prev_state = ndlp->nlp_state;
5512 				lpfc_nlp_set_state(vport, ndlp,
5513 					   NLP_STE_REG_LOGIN_ISSUE);
5514 			}
5515 
5516 			set_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
5517 			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5518 			    != MBX_NOT_FINISHED)
5519 				goto out;
5520 
5521 			/* Decrement the ndlp reference count we
5522 			 * set for this failed mailbox command.
5523 			 */
5524 			lpfc_nlp_put(ndlp);
5525 			clear_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag);
5526 
5527 			/* ELS rsp: Cannot issue reg_login for <NPortid> */
5528 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5529 				"0138 ELS rsp: Cannot issue reg_login for x%x "
5530 				"Data: x%lx x%x x%x\n",
5531 				ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5532 				ndlp->nlp_rpi);
5533 		}
5534 out_free_mbox:
5535 		lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
5536 	} else if (mbox && test_bit(FC_PT2PT, &vport->fc_flag) &&
5537 		   test_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag)) {
5538 		lpfc_mbx_cmpl_reg_login(phba, mbox);
5539 		clear_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag);
5540 	}
5541 out:
5542 	if (ndlp && shost) {
5543 		if (mbox)
5544 			clear_bit(NLP_ACC_REGLOGIN, &ndlp->nlp_flag);
5545 		clear_bit(NLP_RM_DFLT_RPI, &ndlp->nlp_flag);
5546 	}
5547 
5548 	/* An SLI4 NPIV instance wants to drop the node at this point under
5549 	 * these conditions because it doesn't need the login.
5550 	 */
5551 	if (phba->sli_rev == LPFC_SLI_REV4 &&
5552 	    vport->port_type == LPFC_NPIV_PORT &&
5553 	    !(ndlp->fc4_xpt_flags & SCSI_XPT_REGD)) {
5554 		if (ndlp->nlp_state != NLP_STE_PLOGI_ISSUE &&
5555 		    ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE &&
5556 		    ndlp->nlp_state != NLP_STE_PRLI_ISSUE) {
5557 			/* Drop ndlp if there is no planned or outstanding
5558 			 * issued PRLI.
5559 			 *
5560 			 * In cases when the ndlp is acting as both an initiator
5561 			 * and target function, let our issued PRLI determine
5562 			 * the final ndlp kref drop.
5563 			 */
5564 			lpfc_drop_node(vport, ndlp);
5565 		}
5566 	}
5567 
5568 	/* The driver's unsolicited deferred FLOGI ACC in Pt2Pt needs to
5569 	 * release the initial reference because the put after the free_iocb
5570 	 * call removes only the reference from the defer logic. This FLOGI
5571 	 * is never registered with the SCSI transport.
5572 	 */
5573 	if (test_bit(FC_PT2PT, &vport->fc_flag) &&
5574 	    test_and_clear_bit(NLP_FLOGI_DFR_ACC, &ndlp->nlp_flag)) {
5575 		lpfc_printf_vlog(vport, KERN_INFO,
5576 				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
5577 				 "3357 Pt2Pt Defer FLOGI ACC ndlp x%px, "
5578 				 "nflags x%lx, fc_flag x%lx\n",
5579 				 ndlp, ndlp->nlp_flag,
5580 				 vport->fc_flag);
5581 		cmd = *((u32 *)cmdiocb->cmd_dmabuf->virt);
5582 		if (cmd == ELS_CMD_ACC) {
5583 			if (!test_and_set_bit(NLP_DROPPED, &ndlp->nlp_flag))
5584 				lpfc_nlp_put(ndlp);
5585 		}
5586 	}
5587 
5588 release:
5589 	/* Release the originating I/O reference. */
5590 	lpfc_els_free_iocb(phba, cmdiocb);
5591 	lpfc_nlp_put(ndlp);
5592 	return;
5593 }
5594 
5595 /**
5596  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
5597  * @vport: pointer to a host virtual N_Port data structure.
5598  * @flag: the els command code to be accepted.
5599  * @oldiocb: pointer to the original lpfc command iocb data structure.
5600  * @ndlp: pointer to a node-list data structure.
5601  * @mbox: pointer to the driver internal queue element for mailbox command.
5602  *
5603  * This routine prepares and issues an Accept (ACC) response IOCB
5604  * command. It uses the @flag to properly set up the IOCB field for the
5605  * specific ACC response command to be issued and invokes the
5606  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
5607  * @mbox pointer is passed in, it will be put into the context_un.mbox
5608  * field of the IOCB for the completion callback function to issue the
5609  * mailbox command to the HBA later when callback is invoked.
5610  *
5611  * Note that the ndlp reference count will be incremented by 1 for holding the
5612  * ndlp and the reference to ndlp will be stored into the ndlp field of
5613  * the IOCB for the completion callback function to the corresponding
5614  * response ELS IOCB command.
5615  *
5616  * Return code
5617  *   0 - Successfully issued acc response
5618  *   1 - Failed to issue acc response
5619  **/
5620 int
5621 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
5622 		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5623 		 LPFC_MBOXQ_t *mbox)
5624 {
5625 	struct lpfc_hba  *phba = vport->phba;
5626 	IOCB_t *icmd;
5627 	IOCB_t *oldcmd;
5628 	union lpfc_wqe128 *wqe;
5629 	union lpfc_wqe128 *oldwqe = &oldiocb->wqe;
5630 	struct lpfc_iocbq *elsiocb;
5631 	uint8_t *pcmd;
5632 	struct serv_parm *sp;
5633 	uint16_t cmdsize;
5634 	int rc;
5635 	ELS_PKT *els_pkt_ptr;
5636 	struct fc_els_rdf_resp *rdf_resp;
5637 
5638 	switch (flag) {
5639 	case ELS_CMD_ACC:
5640 		cmdsize = sizeof(uint32_t);
5641 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5642 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5643 		if (!elsiocb) {
5644 			clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
5645 			return 1;
5646 		}
5647 
5648 		if (phba->sli_rev == LPFC_SLI_REV4) {
5649 			wqe = &elsiocb->wqe;
5650 			/* XRI / rx_id */
5651 			bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5652 			       bf_get(wqe_ctxt_tag,
5653 				      &oldwqe->xmit_els_rsp.wqe_com));
5654 
5655 			/* oxid */
5656 			bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5657 			       bf_get(wqe_rcvoxid,
5658 				      &oldwqe->xmit_els_rsp.wqe_com));
5659 		} else {
5660 			icmd = &elsiocb->iocb;
5661 			oldcmd = &oldiocb->iocb;
5662 			icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5663 			icmd->unsli3.rcvsli3.ox_id =
5664 				oldcmd->unsli3.rcvsli3.ox_id;
5665 		}
5666 
5667 		pcmd = elsiocb->cmd_dmabuf->virt;
5668 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5669 		pcmd += sizeof(uint32_t);
5670 
5671 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5672 			"Issue ACC:       did:x%x flg:x%lx",
5673 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
5674 		break;
5675 	case ELS_CMD_FLOGI:
5676 	case ELS_CMD_PLOGI:
5677 		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
5678 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5679 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5680 		if (!elsiocb)
5681 			return 1;
5682 
5683 		if (phba->sli_rev == LPFC_SLI_REV4) {
5684 			wqe = &elsiocb->wqe;
5685 			/* XRI / rx_id */
5686 			bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5687 			       bf_get(wqe_ctxt_tag,
5688 				      &oldwqe->xmit_els_rsp.wqe_com));
5689 
5690 			/* oxid */
5691 			bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5692 			       bf_get(wqe_rcvoxid,
5693 				      &oldwqe->xmit_els_rsp.wqe_com));
5694 		} else {
5695 			icmd = &elsiocb->iocb;
5696 			oldcmd = &oldiocb->iocb;
5697 			icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5698 			icmd->unsli3.rcvsli3.ox_id =
5699 				oldcmd->unsli3.rcvsli3.ox_id;
5700 		}
5701 
5702 		pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
5703 
5704 		if (mbox)
5705 			elsiocb->context_un.mbox = mbox;
5706 
5707 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5708 		pcmd += sizeof(uint32_t);
5709 		sp = (struct serv_parm *)pcmd;
5710 
5711 		if (flag == ELS_CMD_FLOGI) {
5712 			/* Copy the received service parameters back */
5713 			memcpy(sp, &phba->fc_fabparam,
5714 			       sizeof(struct serv_parm));
5715 
5716 			/* Clear the F_Port bit */
5717 			sp->cmn.fPort = 0;
5718 
5719 			/* Mark all class service parameters as invalid */
5720 			sp->cls1.classValid = 0;
5721 			sp->cls2.classValid = 0;
5722 			sp->cls3.classValid = 0;
5723 
5724 			/* Copy our worldwide names */
5725 			memcpy(&sp->portName, &vport->fc_sparam.portName,
5726 			       sizeof(struct lpfc_name));
5727 			memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
5728 			       sizeof(struct lpfc_name));
5729 		} else {
5730 			memcpy(pcmd, &vport->fc_sparam,
5731 			       sizeof(struct serv_parm));
5732 
5733 			sp->cmn.valid_vendor_ver_level = 0;
5734 			memset(sp->un.vendorVersion, 0,
5735 			       sizeof(sp->un.vendorVersion));
5736 			if (!test_bit(FC_PT2PT, &vport->fc_flag))
5737 				sp->cmn.bbRcvSizeMsb &= 0xF;
5738 
5739 			/* If our firmware supports this feature, convey that
5740 			 * info to the target using the vendor specific field.
5741 			 */
5742 			if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
5743 				sp->cmn.valid_vendor_ver_level = 1;
5744 				sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
5745 				sp->un.vv.flags =
5746 					cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
5747 			}
5748 		}
5749 
5750 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5751 			"Issue ACC FLOGI/PLOGI: did:x%x flg:x%lx",
5752 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
5753 		break;
5754 	case ELS_CMD_PRLO:
5755 		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
5756 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5757 					     ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
5758 		if (!elsiocb)
5759 			return 1;
5760 
5761 		if (phba->sli_rev == LPFC_SLI_REV4) {
5762 			wqe = &elsiocb->wqe;
5763 			/* XRI / rx_id */
5764 			bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5765 			       bf_get(wqe_ctxt_tag,
5766 				      &oldwqe->xmit_els_rsp.wqe_com));
5767 
5768 			/* oxid */
5769 			bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5770 			       bf_get(wqe_rcvoxid,
5771 				      &oldwqe->xmit_els_rsp.wqe_com));
5772 		} else {
5773 			icmd = &elsiocb->iocb;
5774 			oldcmd = &oldiocb->iocb;
5775 			icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5776 			icmd->unsli3.rcvsli3.ox_id =
5777 				oldcmd->unsli3.rcvsli3.ox_id;
5778 		}
5779 
5780 		pcmd = (u8 *) elsiocb->cmd_dmabuf->virt;
5781 
5782 		memcpy(pcmd, oldiocb->cmd_dmabuf->virt,
5783 		       sizeof(uint32_t) + sizeof(PRLO));
5784 		*((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
5785 		els_pkt_ptr = (ELS_PKT *) pcmd;
5786 		els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
5787 
5788 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5789 			"Issue ACC PRLO:  did:x%x flg:x%lx",
5790 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
5791 		break;
5792 	case ELS_CMD_RDF:
5793 		cmdsize = sizeof(*rdf_resp);
5794 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
5795 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
5796 		if (!elsiocb)
5797 			return 1;
5798 
5799 		if (phba->sli_rev == LPFC_SLI_REV4) {
5800 			wqe = &elsiocb->wqe;
5801 			/* XRI / rx_id */
5802 			bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
5803 			       bf_get(wqe_ctxt_tag,
5804 				      &oldwqe->xmit_els_rsp.wqe_com));
5805 
5806 			/* oxid */
5807 			bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5808 			       bf_get(wqe_rcvoxid,
5809 				      &oldwqe->xmit_els_rsp.wqe_com));
5810 		} else {
5811 			icmd = &elsiocb->iocb;
5812 			oldcmd = &oldiocb->iocb;
5813 			icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5814 			icmd->unsli3.rcvsli3.ox_id =
5815 				oldcmd->unsli3.rcvsli3.ox_id;
5816 		}
5817 
5818 		pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
5819 		rdf_resp = (struct fc_els_rdf_resp *)pcmd;
5820 		memset(rdf_resp, 0, sizeof(*rdf_resp));
5821 		rdf_resp->acc_hdr.la_cmd = ELS_LS_ACC;
5822 
5823 		/* FC-LS-5 specifies desc_list_len shall be set to 12 */
5824 		rdf_resp->desc_list_len = cpu_to_be32(12);
5825 
5826 		/* FC-LS-5 specifies LS REQ Information descriptor */
5827 		rdf_resp->lsri.desc_tag = cpu_to_be32(1);
5828 		rdf_resp->lsri.desc_len = cpu_to_be32(sizeof(u32));
5829 		rdf_resp->lsri.rqst_w0.cmd = ELS_RDF;
5830 		break;
5831 	default:
5832 		return 1;
5833 	}
5834 	if (test_bit(NLP_LOGO_ACC, &ndlp->nlp_flag)) {
5835 		if (!test_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag) &&
5836 		    !test_bit(NLP_REG_LOGIN_SEND, &ndlp->nlp_flag))
5837 			clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
5838 		elsiocb->cmd_cmpl = lpfc_cmpl_els_logo_acc;
5839 	} else {
5840 		elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5841 	}
5842 
5843 	phba->fc_stat.elsXmitACC++;
5844 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
5845 	if (!elsiocb->ndlp) {
5846 		lpfc_els_free_iocb(phba, elsiocb);
5847 		return 1;
5848 	}
5849 
5850 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5851 	if (rc == IOCB_ERROR) {
5852 		lpfc_els_free_iocb(phba, elsiocb);
5853 		lpfc_nlp_put(ndlp);
5854 		return 1;
5855 	}
5856 
5857 	/* Xmit ELS ACC response tag <ulpIoTag> */
5858 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5859 			 "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
5860 			 "XRI: x%x, DID: x%x, nlp_flag: x%lx nlp_state: x%x "
5861 			 "RPI: x%x, fc_flag x%lx refcnt %d\n",
5862 			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
5863 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5864 			 ndlp->nlp_rpi, vport->fc_flag, kref_read(&ndlp->kref));
5865 	return 0;
5866 }
5867 
5868 /**
5869  * lpfc_els_rsp_reject - Prepare and issue a rjt response iocb command
5870  * @vport: pointer to a virtual N_Port data structure.
5871  * @rejectError: reject response to issue
5872  * @oldiocb: pointer to the original lpfc command iocb data structure.
5873  * @ndlp: pointer to a node-list data structure.
5874  * @mbox: pointer to the driver internal queue element for mailbox command.
5875  *
5876  * This routine prepares and issue an Reject (RJT) response IOCB
5877  * command. If a @mbox pointer is passed in, it will be put into the
5878  * context_un.mbox field of the IOCB for the completion callback function
5879  * to issue to the HBA later.
5880  *
5881  * Note that the ndlp reference count will be incremented by 1 for holding the
5882  * ndlp and the reference to ndlp will be stored into the ndlp field of
5883  * the IOCB for the completion callback function to the reject response
5884  * ELS IOCB command.
5885  *
5886  * Return code
5887  *   0 - Successfully issued reject response
5888  *   1 - Failed to issue reject response
5889  **/
5890 int
5891 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
5892 		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
5893 		    LPFC_MBOXQ_t *mbox)
5894 {
5895 	int rc;
5896 	struct lpfc_hba  *phba = vport->phba;
5897 	IOCB_t *icmd;
5898 	IOCB_t *oldcmd;
5899 	union lpfc_wqe128 *wqe;
5900 	struct lpfc_iocbq *elsiocb;
5901 	uint8_t *pcmd;
5902 	uint16_t cmdsize;
5903 
5904 	cmdsize = 2 * sizeof(uint32_t);
5905 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5906 				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
5907 	if (!elsiocb)
5908 		return 1;
5909 
5910 	if (phba->sli_rev == LPFC_SLI_REV4) {
5911 		wqe = &elsiocb->wqe;
5912 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
5913 		       get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
5914 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
5915 		       get_job_rcvoxid(phba, oldiocb));
5916 	} else {
5917 		icmd = &elsiocb->iocb;
5918 		oldcmd = &oldiocb->iocb;
5919 		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5920 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5921 	}
5922 
5923 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
5924 
5925 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
5926 	pcmd += sizeof(uint32_t);
5927 	*((uint32_t *) (pcmd)) = rejectError;
5928 
5929 	if (mbox)
5930 		elsiocb->context_un.mbox = mbox;
5931 
5932 	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
5933 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5934 			 "0129 Xmit ELS RJT x%x response tag x%x "
5935 			 "xri x%x, did x%x, nlp_flag x%lx, nlp_state x%x, "
5936 			 "rpi x%x\n",
5937 			 rejectError, elsiocb->iotag,
5938 			 get_job_ulpcontext(phba, elsiocb), ndlp->nlp_DID,
5939 			 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
5940 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5941 		"Issue LS_RJT:    did:x%x flg:x%lx err:x%x",
5942 		ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
5943 
5944 	phba->fc_stat.elsXmitLSRJT++;
5945 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
5946 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
5947 	if (!elsiocb->ndlp) {
5948 		lpfc_els_free_iocb(phba, elsiocb);
5949 		return 1;
5950 	}
5951 
5952 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5953 	if (rc == IOCB_ERROR) {
5954 		lpfc_els_free_iocb(phba, elsiocb);
5955 		lpfc_nlp_put(ndlp);
5956 		return 1;
5957 	}
5958 
5959 	return 0;
5960 }
5961 
5962  /**
5963   * lpfc_issue_els_edc_rsp - Exchange Diagnostic Capabilities with the fabric.
5964   * @vport: pointer to a host virtual N_Port data structure.
5965   * @cmdiocb: pointer to the original lpfc command iocb data structure.
5966   * @ndlp: NPort to where rsp is directed
5967   *
5968   * This routine issues an EDC ACC RSP to the F-Port Controller to communicate
5969   * this N_Port's support of hardware signals in its Congestion
5970   * Capabilities Descriptor.
5971   *
5972   * Return code
5973   *   0 - Successfully issued edc rsp command
5974   *   1 - Failed to issue edc rsp command
5975   **/
5976 static int
5977 lpfc_issue_els_edc_rsp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5978 		       struct lpfc_nodelist *ndlp)
5979 {
5980 	struct lpfc_hba  *phba = vport->phba;
5981 	struct fc_els_edc_resp *edc_rsp;
5982 	struct fc_tlv_desc *tlv;
5983 	struct lpfc_iocbq *elsiocb;
5984 	IOCB_t *icmd, *cmd;
5985 	union lpfc_wqe128 *wqe;
5986 	u32 cgn_desc_size, lft_desc_size;
5987 	u16 cmdsize;
5988 	uint8_t *pcmd;
5989 	int rc;
5990 
5991 	cmdsize = sizeof(struct fc_els_edc_resp);
5992 	cgn_desc_size = sizeof(struct fc_diag_cg_sig_desc);
5993 	lft_desc_size = (lpfc_link_is_lds_capable(phba)) ?
5994 				sizeof(struct fc_diag_lnkflt_desc) : 0;
5995 	cmdsize += cgn_desc_size + lft_desc_size;
5996 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, cmdiocb->retry,
5997 				     ndlp, 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 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6004 		       get_job_ulpcontext(phba, cmdiocb)); /* Xri / rx_id */
6005 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6006 		       get_job_rcvoxid(phba, cmdiocb));
6007 	} else {
6008 		icmd = &elsiocb->iocb;
6009 		cmd = &cmdiocb->iocb;
6010 		icmd->ulpContext = cmd->ulpContext; /* Xri / rx_id */
6011 		icmd->unsli3.rcvsli3.ox_id = cmd->unsli3.rcvsli3.ox_id;
6012 	}
6013 
6014 	pcmd = elsiocb->cmd_dmabuf->virt;
6015 	memset(pcmd, 0, cmdsize);
6016 
6017 	edc_rsp = (struct fc_els_edc_resp *)pcmd;
6018 	edc_rsp->acc_hdr.la_cmd = ELS_LS_ACC;
6019 	edc_rsp->desc_list_len = cpu_to_be32(sizeof(struct fc_els_lsri_desc) +
6020 						cgn_desc_size + lft_desc_size);
6021 	edc_rsp->lsri.desc_tag = cpu_to_be32(ELS_DTAG_LS_REQ_INFO);
6022 	edc_rsp->lsri.desc_len = cpu_to_be32(
6023 		FC_TLV_DESC_LENGTH_FROM_SZ(struct fc_els_lsri_desc));
6024 	edc_rsp->lsri.rqst_w0.cmd = ELS_EDC;
6025 	tlv = edc_rsp->desc;
6026 	lpfc_format_edc_cgn_desc(phba, tlv);
6027 	tlv = fc_tlv_next_desc(tlv);
6028 	if (lft_desc_size)
6029 		lpfc_format_edc_lft_desc(phba, tlv);
6030 
6031 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6032 			      "Issue EDC ACC:      did:x%x flg:x%lx refcnt %d",
6033 			      ndlp->nlp_DID, ndlp->nlp_flag,
6034 			      kref_read(&ndlp->kref));
6035 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6036 
6037 	phba->fc_stat.elsXmitACC++;
6038 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
6039 	if (!elsiocb->ndlp) {
6040 		lpfc_els_free_iocb(phba, elsiocb);
6041 		return 1;
6042 	}
6043 
6044 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6045 	if (rc == IOCB_ERROR) {
6046 		lpfc_els_free_iocb(phba, elsiocb);
6047 		lpfc_nlp_put(ndlp);
6048 		return 1;
6049 	}
6050 
6051 	/* Xmit ELS ACC response tag <ulpIoTag> */
6052 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6053 			 "0152 Xmit EDC ACC response Status: x%x, IoTag: x%x, "
6054 			 "XRI: x%x, DID: x%x, nlp_flag: x%lx nlp_state: x%x "
6055 			 "RPI: x%x, fc_flag x%lx\n",
6056 			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
6057 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6058 			 ndlp->nlp_rpi, vport->fc_flag);
6059 
6060 	return 0;
6061 }
6062 
6063 /**
6064  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
6065  * @vport: pointer to a virtual N_Port data structure.
6066  * @oldiocb: pointer to the original lpfc command iocb data structure.
6067  * @ndlp: pointer to a node-list data structure.
6068  *
6069  * This routine prepares and issues an Accept (ACC) response to Address
6070  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
6071  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
6072  *
6073  * Note that the ndlp reference count will be incremented by 1 for holding the
6074  * ndlp and the reference to ndlp will be stored into the ndlp field of
6075  * the IOCB for the completion callback function to the ADISC Accept response
6076  * ELS IOCB command.
6077  *
6078  * Return code
6079  *   0 - Successfully issued acc adisc response
6080  *   1 - Failed to issue adisc acc response
6081  **/
6082 int
6083 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
6084 		       struct lpfc_nodelist *ndlp)
6085 {
6086 	struct lpfc_hba  *phba = vport->phba;
6087 	ADISC *ap;
6088 	IOCB_t *icmd, *oldcmd;
6089 	union lpfc_wqe128 *wqe;
6090 	struct lpfc_iocbq *elsiocb;
6091 	uint8_t *pcmd;
6092 	uint16_t cmdsize;
6093 	int rc;
6094 	u32 ulp_context;
6095 
6096 	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
6097 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6098 				     ndlp->nlp_DID, ELS_CMD_ACC);
6099 	if (!elsiocb)
6100 		return 1;
6101 
6102 	if (phba->sli_rev == LPFC_SLI_REV4) {
6103 		wqe = &elsiocb->wqe;
6104 		/* XRI / rx_id */
6105 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6106 		       get_job_ulpcontext(phba, oldiocb));
6107 		ulp_context = get_job_ulpcontext(phba, elsiocb);
6108 		/* oxid */
6109 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6110 		       get_job_rcvoxid(phba, oldiocb));
6111 	} else {
6112 		icmd = &elsiocb->iocb;
6113 		oldcmd = &oldiocb->iocb;
6114 		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6115 		ulp_context = elsiocb->iocb.ulpContext;
6116 		icmd->unsli3.rcvsli3.ox_id =
6117 			oldcmd->unsli3.rcvsli3.ox_id;
6118 	}
6119 
6120 	/* Xmit ADISC ACC response tag <ulpIoTag> */
6121 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6122 			 "0130 Xmit ADISC ACC response iotag x%x xri: "
6123 			 "x%x, did x%x, nlp_flag x%lx, nlp_state x%x rpi x%x\n",
6124 			 elsiocb->iotag, ulp_context,
6125 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6126 			 ndlp->nlp_rpi);
6127 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6128 
6129 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6130 	pcmd += sizeof(uint32_t);
6131 
6132 	ap = (ADISC *) (pcmd);
6133 	ap->hardAL_PA = phba->fc_pref_ALPA;
6134 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
6135 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
6136 	ap->DID = be32_to_cpu(vport->fc_myDID);
6137 
6138 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6139 		      "Issue ACC ADISC: did:x%x flg:x%lx refcnt %d",
6140 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6141 
6142 	phba->fc_stat.elsXmitACC++;
6143 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6144 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
6145 	if (!elsiocb->ndlp) {
6146 		lpfc_els_free_iocb(phba, elsiocb);
6147 		return 1;
6148 	}
6149 
6150 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6151 	if (rc == IOCB_ERROR) {
6152 		lpfc_els_free_iocb(phba, elsiocb);
6153 		lpfc_nlp_put(ndlp);
6154 		return 1;
6155 	}
6156 
6157 	return 0;
6158 }
6159 
6160 /**
6161  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
6162  * @vport: pointer to a virtual N_Port data structure.
6163  * @oldiocb: pointer to the original lpfc command iocb data structure.
6164  * @ndlp: pointer to a node-list data structure.
6165  *
6166  * This routine prepares and issues an Accept (ACC) response to Process
6167  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
6168  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
6169  *
6170  * Note that the ndlp reference count will be incremented by 1 for holding the
6171  * ndlp and the reference to ndlp will be stored into the ndlp field of
6172  * the IOCB for the completion callback function to the PRLI Accept response
6173  * ELS IOCB command.
6174  *
6175  * Return code
6176  *   0 - Successfully issued acc prli response
6177  *   1 - Failed to issue acc prli response
6178  **/
6179 int
6180 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
6181 		      struct lpfc_nodelist *ndlp)
6182 {
6183 	struct lpfc_hba  *phba = vport->phba;
6184 	PRLI *npr;
6185 	struct lpfc_nvme_prli *npr_nvme;
6186 	lpfc_vpd_t *vpd;
6187 	IOCB_t *icmd;
6188 	IOCB_t *oldcmd;
6189 	union lpfc_wqe128 *wqe;
6190 	struct lpfc_iocbq *elsiocb;
6191 	uint8_t *pcmd;
6192 	uint16_t cmdsize;
6193 	uint32_t prli_fc4_req, *req_payload;
6194 	struct lpfc_dmabuf *req_buf;
6195 	int rc;
6196 	u32 elsrspcmd, ulp_context;
6197 
6198 	/* Need the incoming PRLI payload to determine if the ACC is for an
6199 	 * FC4 or NVME PRLI type.  The PRLI type is at word 1.
6200 	 */
6201 	req_buf = oldiocb->cmd_dmabuf;
6202 	req_payload = (((uint32_t *)req_buf->virt) + 1);
6203 
6204 	/* PRLI type payload is at byte 3 for FCP or NVME. */
6205 	prli_fc4_req = be32_to_cpu(*req_payload);
6206 	prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
6207 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6208 			 "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
6209 			 prli_fc4_req, *((uint32_t *)req_payload));
6210 
6211 	if (prli_fc4_req == PRLI_FCP_TYPE) {
6212 		cmdsize = sizeof(uint32_t) + sizeof(PRLI);
6213 		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
6214 	} else if (prli_fc4_req == PRLI_NVME_TYPE) {
6215 		cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
6216 		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
6217 	} else {
6218 		return 1;
6219 	}
6220 
6221 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6222 				     ndlp->nlp_DID, elsrspcmd);
6223 	if (!elsiocb)
6224 		return 1;
6225 
6226 	if (phba->sli_rev == LPFC_SLI_REV4) {
6227 		wqe = &elsiocb->wqe;
6228 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6229 		       get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
6230 		ulp_context = get_job_ulpcontext(phba, elsiocb);
6231 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6232 		       get_job_rcvoxid(phba, oldiocb));
6233 	} else {
6234 		icmd = &elsiocb->iocb;
6235 		oldcmd = &oldiocb->iocb;
6236 		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6237 		ulp_context = elsiocb->iocb.ulpContext;
6238 		icmd->unsli3.rcvsli3.ox_id =
6239 			oldcmd->unsli3.rcvsli3.ox_id;
6240 	}
6241 
6242 	/* Xmit PRLI ACC response tag <ulpIoTag> */
6243 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6244 			 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
6245 			 "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x\n",
6246 			 elsiocb->iotag, ulp_context,
6247 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6248 			 ndlp->nlp_rpi);
6249 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6250 	memset(pcmd, 0, cmdsize);
6251 
6252 	*((uint32_t *)(pcmd)) = elsrspcmd;
6253 	pcmd += sizeof(uint32_t);
6254 
6255 	/* For PRLI, remainder of payload is PRLI parameter page */
6256 	vpd = &phba->vpd;
6257 
6258 	if (prli_fc4_req == PRLI_FCP_TYPE) {
6259 		/*
6260 		 * If the remote port is a target and our firmware version
6261 		 * is 3.20 or later, set the following bits for FC-TAPE
6262 		 * support.
6263 		 */
6264 		npr = (PRLI *) pcmd;
6265 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
6266 		    (vpd->rev.feaLevelHigh >= 0x02)) {
6267 			npr->ConfmComplAllowed = 1;
6268 			npr->Retry = 1;
6269 			npr->TaskRetryIdReq = 1;
6270 		}
6271 		npr->acceptRspCode = PRLI_REQ_EXECUTED;
6272 
6273 		/* Set image pair for complementary pairs only. */
6274 		if (ndlp->nlp_type & NLP_FCP_TARGET)
6275 			npr->estabImagePair = 1;
6276 		else
6277 			npr->estabImagePair = 0;
6278 		npr->readXferRdyDis = 1;
6279 		npr->ConfmComplAllowed = 1;
6280 		npr->prliType = PRLI_FCP_TYPE;
6281 		npr->initiatorFunc = 1;
6282 
6283 		/* Xmit PRLI ACC response tag <ulpIoTag> */
6284 		lpfc_printf_vlog(vport, KERN_INFO,
6285 				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
6286 				 "6014 FCP issue PRLI ACC imgpair %d "
6287 				 "retry %d task %d\n",
6288 				 npr->estabImagePair,
6289 				 npr->Retry, npr->TaskRetryIdReq);
6290 
6291 	} else if (prli_fc4_req == PRLI_NVME_TYPE) {
6292 		/* Respond with an NVME PRLI Type */
6293 		npr_nvme = (struct lpfc_nvme_prli *) pcmd;
6294 		bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
6295 		bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
6296 		bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
6297 		if (phba->nvmet_support) {
6298 			bf_set(prli_tgt, npr_nvme, 1);
6299 			bf_set(prli_disc, npr_nvme, 1);
6300 			if (phba->cfg_nvme_enable_fb) {
6301 				bf_set(prli_fba, npr_nvme, 1);
6302 
6303 				/* TBD.  Target mode needs to post buffers
6304 				 * that support the configured first burst
6305 				 * byte size.
6306 				 */
6307 				bf_set(prli_fb_sz, npr_nvme,
6308 				       phba->cfg_nvmet_fb_size);
6309 			}
6310 		} else {
6311 			bf_set(prli_init, npr_nvme, 1);
6312 		}
6313 
6314 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
6315 				 "6015 NVME issue PRLI ACC word1 x%08x "
6316 				 "word4 x%08x word5 x%08x flag x%lx, "
6317 				 "fcp_info x%x nlp_type x%x\n",
6318 				 npr_nvme->word1, npr_nvme->word4,
6319 				 npr_nvme->word5, ndlp->nlp_flag,
6320 				 ndlp->nlp_fcp_info, ndlp->nlp_type);
6321 		npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
6322 		npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
6323 		npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
6324 	} else
6325 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6326 				 "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
6327 				 prli_fc4_req, ndlp->nlp_fc4_type,
6328 				 ndlp->nlp_DID);
6329 
6330 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6331 		      "Issue ACC PRLI:  did:x%x flg:x%lx",
6332 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6333 
6334 	phba->fc_stat.elsXmitACC++;
6335 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6336 	elsiocb->ndlp =  lpfc_nlp_get(ndlp);
6337 	if (!elsiocb->ndlp) {
6338 		lpfc_els_free_iocb(phba, elsiocb);
6339 		return 1;
6340 	}
6341 
6342 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6343 	if (rc == IOCB_ERROR) {
6344 		lpfc_els_free_iocb(phba, elsiocb);
6345 		lpfc_nlp_put(ndlp);
6346 		return 1;
6347 	}
6348 
6349 	return 0;
6350 }
6351 
6352 /**
6353  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
6354  * @vport: pointer to a virtual N_Port data structure.
6355  * @format: rnid command format.
6356  * @oldiocb: pointer to the original lpfc command iocb data structure.
6357  * @ndlp: pointer to a node-list data structure.
6358  *
6359  * This routine issues a Request Node Identification Data (RNID) Accept
6360  * (ACC) response. It constructs the RNID ACC response command according to
6361  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
6362  * issue the response.
6363  *
6364  * Note that the ndlp reference count will be incremented by 1 for holding the
6365  * ndlp and the reference to ndlp will be stored into the ndlp field of
6366  * the IOCB for the completion callback function.
6367  *
6368  * Return code
6369  *   0 - Successfully issued acc rnid response
6370  *   1 - Failed to issue acc rnid response
6371  **/
6372 static int
6373 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
6374 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6375 {
6376 	struct lpfc_hba  *phba = vport->phba;
6377 	RNID *rn;
6378 	IOCB_t *icmd, *oldcmd;
6379 	union lpfc_wqe128 *wqe;
6380 	struct lpfc_iocbq *elsiocb;
6381 	uint8_t *pcmd;
6382 	uint16_t cmdsize;
6383 	int rc;
6384 	u32 ulp_context;
6385 
6386 	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
6387 					+ (2 * sizeof(struct lpfc_name));
6388 	if (format)
6389 		cmdsize += sizeof(RNID_TOP_DISC);
6390 
6391 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6392 				     ndlp->nlp_DID, ELS_CMD_ACC);
6393 	if (!elsiocb)
6394 		return 1;
6395 
6396 	if (phba->sli_rev == LPFC_SLI_REV4) {
6397 		wqe = &elsiocb->wqe;
6398 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6399 		       get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
6400 		ulp_context = get_job_ulpcontext(phba, elsiocb);
6401 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6402 		       get_job_rcvoxid(phba, oldiocb));
6403 	} else {
6404 		icmd = &elsiocb->iocb;
6405 		oldcmd = &oldiocb->iocb;
6406 		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6407 		ulp_context = elsiocb->iocb.ulpContext;
6408 		icmd->unsli3.rcvsli3.ox_id =
6409 			oldcmd->unsli3.rcvsli3.ox_id;
6410 	}
6411 
6412 	/* Xmit RNID ACC response tag <ulpIoTag> */
6413 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6414 			 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
6415 			 elsiocb->iotag, ulp_context);
6416 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6417 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6418 	pcmd += sizeof(uint32_t);
6419 
6420 	memset(pcmd, 0, sizeof(RNID));
6421 	rn = (RNID *) (pcmd);
6422 	rn->Format = format;
6423 	rn->CommonLen = (2 * sizeof(struct lpfc_name));
6424 	memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
6425 	memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
6426 	switch (format) {
6427 	case 0:
6428 		rn->SpecificLen = 0;
6429 		break;
6430 	case RNID_TOPOLOGY_DISC:
6431 		rn->SpecificLen = sizeof(RNID_TOP_DISC);
6432 		memcpy(&rn->un.topologyDisc.portName,
6433 		       &vport->fc_portname, sizeof(struct lpfc_name));
6434 		rn->un.topologyDisc.unitType = RNID_HBA;
6435 		rn->un.topologyDisc.physPort = 0;
6436 		rn->un.topologyDisc.attachedNodes = 0;
6437 		break;
6438 	default:
6439 		rn->CommonLen = 0;
6440 		rn->SpecificLen = 0;
6441 		break;
6442 	}
6443 
6444 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6445 		      "Issue ACC RNID:  did:x%x flg:x%lx refcnt %d",
6446 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6447 
6448 	phba->fc_stat.elsXmitACC++;
6449 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6450 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
6451 	if (!elsiocb->ndlp) {
6452 		lpfc_els_free_iocb(phba, elsiocb);
6453 		return 1;
6454 	}
6455 
6456 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6457 	if (rc == IOCB_ERROR) {
6458 		lpfc_els_free_iocb(phba, elsiocb);
6459 		lpfc_nlp_put(ndlp);
6460 		return 1;
6461 	}
6462 
6463 	return 0;
6464 }
6465 
6466 /**
6467  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
6468  * @vport: pointer to a virtual N_Port data structure.
6469  * @iocb: pointer to the lpfc command iocb data structure.
6470  * @ndlp: pointer to a node-list data structure.
6471  *
6472  * Return
6473  **/
6474 static void
6475 lpfc_els_clear_rrq(struct lpfc_vport *vport,
6476 		   struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
6477 {
6478 	struct lpfc_hba  *phba = vport->phba;
6479 	uint8_t *pcmd;
6480 	struct RRQ *rrq;
6481 	uint16_t rxid;
6482 	uint16_t xri;
6483 	struct lpfc_node_rrq *prrq;
6484 
6485 
6486 	pcmd = (uint8_t *)iocb->cmd_dmabuf->virt;
6487 	pcmd += sizeof(uint32_t);
6488 	rrq = (struct RRQ *)pcmd;
6489 	rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
6490 	rxid = bf_get(rrq_rxid, rrq);
6491 
6492 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6493 			"2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
6494 			" x%x x%x\n",
6495 			be32_to_cpu(bf_get(rrq_did, rrq)),
6496 			bf_get(rrq_oxid, rrq),
6497 			rxid,
6498 			get_wqe_reqtag(iocb),
6499 			get_job_ulpcontext(phba, iocb));
6500 
6501 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6502 		"Clear RRQ:  did:x%x flg:x%lx exchg:x%.08x",
6503 		ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
6504 	if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
6505 		xri = bf_get(rrq_oxid, rrq);
6506 	else
6507 		xri = rxid;
6508 	prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
6509 	if (prrq)
6510 		lpfc_clr_rrq_active(phba, xri, prrq);
6511 	return;
6512 }
6513 
6514 /**
6515  * lpfc_els_rsp_echo_acc - Issue echo acc response
6516  * @vport: pointer to a virtual N_Port data structure.
6517  * @data: pointer to echo data to return in the accept.
6518  * @oldiocb: pointer to the original lpfc command iocb data structure.
6519  * @ndlp: pointer to a node-list data structure.
6520  *
6521  * Return code
6522  *   0 - Successfully issued acc echo response
6523  *   1 - Failed to issue acc echo response
6524  **/
6525 static int
6526 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
6527 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
6528 {
6529 	struct lpfc_hba  *phba = vport->phba;
6530 	IOCB_t *icmd, *oldcmd;
6531 	union lpfc_wqe128 *wqe;
6532 	struct lpfc_iocbq *elsiocb;
6533 	uint8_t *pcmd;
6534 	uint16_t cmdsize;
6535 	int rc;
6536 	u32 ulp_context;
6537 
6538 	if (phba->sli_rev == LPFC_SLI_REV4)
6539 		cmdsize = oldiocb->wcqe_cmpl.total_data_placed;
6540 	else
6541 		cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
6542 
6543 	/* The accumulated length can exceed the BPL_SIZE.  For
6544 	 * now, use this as the limit
6545 	 */
6546 	if (cmdsize > LPFC_BPL_SIZE)
6547 		cmdsize = LPFC_BPL_SIZE;
6548 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
6549 				     ndlp->nlp_DID, ELS_CMD_ACC);
6550 	if (!elsiocb)
6551 		return 1;
6552 
6553 	if (phba->sli_rev == LPFC_SLI_REV4) {
6554 		wqe = &elsiocb->wqe;
6555 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
6556 		       get_job_ulpcontext(phba, oldiocb)); /* Xri / rx_id */
6557 		ulp_context = get_job_ulpcontext(phba, elsiocb);
6558 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6559 		       get_job_rcvoxid(phba, oldiocb));
6560 	} else {
6561 		icmd = &elsiocb->iocb;
6562 		oldcmd = &oldiocb->iocb;
6563 		icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
6564 		ulp_context = elsiocb->iocb.ulpContext;
6565 		icmd->unsli3.rcvsli3.ox_id =
6566 			oldcmd->unsli3.rcvsli3.ox_id;
6567 	}
6568 
6569 	/* Xmit ECHO ACC response tag <ulpIoTag> */
6570 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6571 			 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
6572 			 elsiocb->iotag, ulp_context);
6573 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
6574 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6575 	pcmd += sizeof(uint32_t);
6576 	memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
6577 
6578 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
6579 		      "Issue ACC ECHO:  did:x%x flg:x%lx refcnt %d",
6580 		      ndlp->nlp_DID, ndlp->nlp_flag, kref_read(&ndlp->kref));
6581 
6582 	phba->fc_stat.elsXmitACC++;
6583 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
6584 	elsiocb->ndlp =  lpfc_nlp_get(ndlp);
6585 	if (!elsiocb->ndlp) {
6586 		lpfc_els_free_iocb(phba, elsiocb);
6587 		return 1;
6588 	}
6589 
6590 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6591 	if (rc == IOCB_ERROR) {
6592 		lpfc_els_free_iocb(phba, elsiocb);
6593 		lpfc_nlp_put(ndlp);
6594 		return 1;
6595 	}
6596 
6597 	return 0;
6598 }
6599 
6600 /**
6601  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
6602  * @vport: pointer to a host virtual N_Port data structure.
6603  *
6604  * This routine issues Address Discover (ADISC) ELS commands to those
6605  * N_Ports which are in node port recovery state and ADISC has not been issued
6606  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
6607  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
6608  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
6609  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
6610  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
6611  * IOCBs quit for later pick up. On the other hand, after walking through
6612  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
6613  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
6614  * no more ADISC need to be sent.
6615  *
6616  * Return code
6617  *    The number of N_Ports with adisc issued.
6618  **/
6619 int
6620 lpfc_els_disc_adisc(struct lpfc_vport *vport)
6621 {
6622 	struct lpfc_nodelist *ndlp, *next_ndlp;
6623 	int sentadisc = 0;
6624 
6625 	/* go thru NPR nodes and issue any remaining ELS ADISCs */
6626 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6627 
6628 		if (ndlp->nlp_state != NLP_STE_NPR_NODE ||
6629 		    !test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag))
6630 			continue;
6631 
6632 		clear_bit(NLP_NPR_ADISC, &ndlp->nlp_flag);
6633 
6634 		if (!test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag)) {
6635 			/* This node was marked for ADISC but was not picked
6636 			 * for discovery. This is possible if the node was
6637 			 * missing in gidft response.
6638 			 *
6639 			 * At time of marking node for ADISC, we skipped unreg
6640 			 * from backend
6641 			 */
6642 			lpfc_nlp_unreg_node(vport, ndlp);
6643 			lpfc_unreg_rpi(vport, ndlp);
6644 			continue;
6645 		}
6646 
6647 		ndlp->nlp_prev_state = ndlp->nlp_state;
6648 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
6649 		lpfc_issue_els_adisc(vport, ndlp, 0);
6650 		sentadisc++;
6651 		vport->num_disc_nodes++;
6652 		if (vport->num_disc_nodes >=
6653 				vport->cfg_discovery_threads) {
6654 			set_bit(FC_NLP_MORE, &vport->fc_flag);
6655 			break;
6656 		}
6657 
6658 	}
6659 	if (sentadisc == 0)
6660 		clear_bit(FC_NLP_MORE, &vport->fc_flag);
6661 	return sentadisc;
6662 }
6663 
6664 /**
6665  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
6666  * @vport: pointer to a host virtual N_Port data structure.
6667  *
6668  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
6669  * which are in node port recovery state, with a @vport. Each time an ELS
6670  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
6671  * the per @vport number of discover count (num_disc_nodes) shall be
6672  * incremented. If the num_disc_nodes reaches a pre-configured threshold
6673  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
6674  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
6675  * later pick up. On the other hand, after walking through all the ndlps with
6676  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
6677  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
6678  * PLOGI need to be sent.
6679  *
6680  * Return code
6681  *   The number of N_Ports with plogi issued.
6682  **/
6683 int
6684 lpfc_els_disc_plogi(struct lpfc_vport *vport)
6685 {
6686 	struct lpfc_nodelist *ndlp, *next_ndlp;
6687 	int sentplogi = 0;
6688 
6689 	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
6690 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
6691 		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
6692 		    test_bit(NLP_NPR_2B_DISC, &ndlp->nlp_flag) &&
6693 		    !test_bit(NLP_DELAY_TMO, &ndlp->nlp_flag) &&
6694 		    !test_bit(NLP_NPR_ADISC, &ndlp->nlp_flag)) {
6695 			ndlp->nlp_prev_state = ndlp->nlp_state;
6696 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6697 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
6698 			sentplogi++;
6699 			vport->num_disc_nodes++;
6700 			if (vport->num_disc_nodes >=
6701 					vport->cfg_discovery_threads) {
6702 				set_bit(FC_NLP_MORE, &vport->fc_flag);
6703 				break;
6704 			}
6705 		}
6706 	}
6707 
6708 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6709 			 "6452 Discover PLOGI %d flag x%lx\n",
6710 			 sentplogi, vport->fc_flag);
6711 
6712 	if (sentplogi)
6713 		lpfc_set_disctmo(vport);
6714 	else
6715 		clear_bit(FC_NLP_MORE, &vport->fc_flag);
6716 	return sentplogi;
6717 }
6718 
6719 static uint32_t
6720 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
6721 		uint32_t word0)
6722 {
6723 
6724 	desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
6725 	desc->payload.els_req = word0;
6726 	desc->length = cpu_to_be32(sizeof(desc->payload));
6727 
6728 	return sizeof(struct fc_rdp_link_service_desc);
6729 }
6730 
6731 static uint32_t
6732 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
6733 		uint8_t *page_a0, uint8_t *page_a2)
6734 {
6735 	uint16_t wavelength;
6736 	uint16_t temperature;
6737 	uint16_t rx_power;
6738 	uint16_t tx_bias;
6739 	uint16_t tx_power;
6740 	uint16_t vcc;
6741 	uint16_t flag = 0;
6742 	struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
6743 	struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
6744 
6745 	desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
6746 
6747 	trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
6748 			&page_a0[SSF_TRANSCEIVER_CODE_B4];
6749 	trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
6750 			&page_a0[SSF_TRANSCEIVER_CODE_B5];
6751 
6752 	if ((trasn_code_byte4->fc_sw_laser) ||
6753 	    (trasn_code_byte5->fc_sw_laser_sl) ||
6754 	    (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
6755 		flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
6756 	} else if (trasn_code_byte4->fc_lw_laser) {
6757 		wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
6758 			page_a0[SSF_WAVELENGTH_B0];
6759 		if (wavelength == SFP_WAVELENGTH_LC1310)
6760 			flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
6761 		if (wavelength == SFP_WAVELENGTH_LL1550)
6762 			flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
6763 	}
6764 	/* check if its SFP+ */
6765 	flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
6766 			SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
6767 					<< SFP_FLAG_CT_SHIFT;
6768 
6769 	/* check if its OPTICAL */
6770 	flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
6771 			SFP_FLAG_IS_OPTICAL_PORT : 0)
6772 					<< SFP_FLAG_IS_OPTICAL_SHIFT;
6773 
6774 	temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
6775 		page_a2[SFF_TEMPERATURE_B0]);
6776 	vcc = (page_a2[SFF_VCC_B1] << 8 |
6777 		page_a2[SFF_VCC_B0]);
6778 	tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
6779 		page_a2[SFF_TXPOWER_B0]);
6780 	tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
6781 		page_a2[SFF_TX_BIAS_CURRENT_B0]);
6782 	rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
6783 		page_a2[SFF_RXPOWER_B0]);
6784 	desc->sfp_info.temperature = cpu_to_be16(temperature);
6785 	desc->sfp_info.rx_power = cpu_to_be16(rx_power);
6786 	desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
6787 	desc->sfp_info.tx_power = cpu_to_be16(tx_power);
6788 	desc->sfp_info.vcc = cpu_to_be16(vcc);
6789 
6790 	desc->sfp_info.flags = cpu_to_be16(flag);
6791 	desc->length = cpu_to_be32(sizeof(desc->sfp_info));
6792 
6793 	return sizeof(struct fc_rdp_sfp_desc);
6794 }
6795 
6796 static uint32_t
6797 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
6798 		READ_LNK_VAR *stat)
6799 {
6800 	uint32_t type;
6801 
6802 	desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
6803 
6804 	type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
6805 
6806 	desc->info.port_type = cpu_to_be32(type);
6807 
6808 	desc->info.link_status.link_failure_cnt =
6809 		cpu_to_be32(stat->linkFailureCnt);
6810 	desc->info.link_status.loss_of_synch_cnt =
6811 		cpu_to_be32(stat->lossSyncCnt);
6812 	desc->info.link_status.loss_of_signal_cnt =
6813 		cpu_to_be32(stat->lossSignalCnt);
6814 	desc->info.link_status.primitive_seq_proto_err =
6815 		cpu_to_be32(stat->primSeqErrCnt);
6816 	desc->info.link_status.invalid_trans_word =
6817 		cpu_to_be32(stat->invalidXmitWord);
6818 	desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
6819 
6820 	desc->length = cpu_to_be32(sizeof(desc->info));
6821 
6822 	return sizeof(struct fc_rdp_link_error_status_desc);
6823 }
6824 
6825 static uint32_t
6826 lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
6827 		      struct lpfc_vport *vport)
6828 {
6829 	uint32_t bbCredit;
6830 
6831 	desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
6832 
6833 	bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
6834 			(vport->fc_sparam.cmn.bbCreditMsb << 8);
6835 	desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
6836 	if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
6837 		bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
6838 			(vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
6839 		desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
6840 	} else {
6841 		desc->bbc_info.attached_port_bbc = 0;
6842 	}
6843 
6844 	desc->bbc_info.rtt = 0;
6845 	desc->length = cpu_to_be32(sizeof(desc->bbc_info));
6846 
6847 	return sizeof(struct fc_rdp_bbc_desc);
6848 }
6849 
6850 static uint32_t
6851 lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
6852 			   struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
6853 {
6854 	uint32_t flags = 0;
6855 
6856 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6857 
6858 	desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
6859 	desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
6860 	desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
6861 	desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
6862 
6863 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6864 		flags |= RDP_OET_HIGH_ALARM;
6865 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6866 		flags |= RDP_OET_LOW_ALARM;
6867 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
6868 		flags |= RDP_OET_HIGH_WARNING;
6869 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
6870 		flags |= RDP_OET_LOW_WARNING;
6871 
6872 	flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
6873 	desc->oed_info.function_flags = cpu_to_be32(flags);
6874 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6875 	return sizeof(struct fc_rdp_oed_sfp_desc);
6876 }
6877 
6878 static uint32_t
6879 lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
6880 			      struct fc_rdp_oed_sfp_desc *desc,
6881 			      uint8_t *page_a2)
6882 {
6883 	uint32_t flags = 0;
6884 
6885 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6886 
6887 	desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
6888 	desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
6889 	desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
6890 	desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
6891 
6892 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6893 		flags |= RDP_OET_HIGH_ALARM;
6894 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6895 		flags |= RDP_OET_LOW_ALARM;
6896 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
6897 		flags |= RDP_OET_HIGH_WARNING;
6898 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
6899 		flags |= RDP_OET_LOW_WARNING;
6900 
6901 	flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
6902 	desc->oed_info.function_flags = cpu_to_be32(flags);
6903 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6904 	return sizeof(struct fc_rdp_oed_sfp_desc);
6905 }
6906 
6907 static uint32_t
6908 lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
6909 			     struct fc_rdp_oed_sfp_desc *desc,
6910 			     uint8_t *page_a2)
6911 {
6912 	uint32_t flags = 0;
6913 
6914 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6915 
6916 	desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
6917 	desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
6918 	desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
6919 	desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
6920 
6921 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6922 		flags |= RDP_OET_HIGH_ALARM;
6923 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
6924 		flags |= RDP_OET_LOW_ALARM;
6925 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
6926 		flags |= RDP_OET_HIGH_WARNING;
6927 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
6928 		flags |= RDP_OET_LOW_WARNING;
6929 
6930 	flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
6931 	desc->oed_info.function_flags = cpu_to_be32(flags);
6932 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6933 	return sizeof(struct fc_rdp_oed_sfp_desc);
6934 }
6935 
6936 static uint32_t
6937 lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
6938 			      struct fc_rdp_oed_sfp_desc *desc,
6939 			      uint8_t *page_a2)
6940 {
6941 	uint32_t flags = 0;
6942 
6943 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6944 
6945 	desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
6946 	desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
6947 	desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
6948 	desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
6949 
6950 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6951 		flags |= RDP_OET_HIGH_ALARM;
6952 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
6953 		flags |= RDP_OET_LOW_ALARM;
6954 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
6955 		flags |= RDP_OET_HIGH_WARNING;
6956 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
6957 		flags |= RDP_OET_LOW_WARNING;
6958 
6959 	flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
6960 	desc->oed_info.function_flags = cpu_to_be32(flags);
6961 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6962 	return sizeof(struct fc_rdp_oed_sfp_desc);
6963 }
6964 
6965 
6966 static uint32_t
6967 lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
6968 			      struct fc_rdp_oed_sfp_desc *desc,
6969 			      uint8_t *page_a2)
6970 {
6971 	uint32_t flags = 0;
6972 
6973 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
6974 
6975 	desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
6976 	desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
6977 	desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
6978 	desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
6979 
6980 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6981 		flags |= RDP_OET_HIGH_ALARM;
6982 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
6983 		flags |= RDP_OET_LOW_ALARM;
6984 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
6985 		flags |= RDP_OET_HIGH_WARNING;
6986 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
6987 		flags |= RDP_OET_LOW_WARNING;
6988 
6989 	flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
6990 	desc->oed_info.function_flags = cpu_to_be32(flags);
6991 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
6992 	return sizeof(struct fc_rdp_oed_sfp_desc);
6993 }
6994 
6995 static uint32_t
6996 lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
6997 		      uint8_t *page_a0, struct lpfc_vport *vport)
6998 {
6999 	desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
7000 	memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
7001 	memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
7002 	memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
7003 	memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
7004 	memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
7005 	desc->length = cpu_to_be32(sizeof(desc->opd_info));
7006 	return sizeof(struct fc_rdp_opd_sfp_desc);
7007 }
7008 
7009 static uint32_t
7010 lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
7011 {
7012 	if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
7013 		return 0;
7014 	desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
7015 
7016 	desc->info.CorrectedBlocks =
7017 		cpu_to_be32(stat->fecCorrBlkCount);
7018 	desc->info.UncorrectableBlocks =
7019 		cpu_to_be32(stat->fecUncorrBlkCount);
7020 
7021 	desc->length = cpu_to_be32(sizeof(desc->info));
7022 
7023 	return sizeof(struct fc_fec_rdp_desc);
7024 }
7025 
7026 static uint32_t
7027 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
7028 {
7029 	uint16_t rdp_cap = 0;
7030 	uint16_t rdp_speed;
7031 
7032 	desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
7033 
7034 	switch (phba->fc_linkspeed) {
7035 	case LPFC_LINK_SPEED_1GHZ:
7036 		rdp_speed = RDP_PS_1GB;
7037 		break;
7038 	case LPFC_LINK_SPEED_2GHZ:
7039 		rdp_speed = RDP_PS_2GB;
7040 		break;
7041 	case LPFC_LINK_SPEED_4GHZ:
7042 		rdp_speed = RDP_PS_4GB;
7043 		break;
7044 	case LPFC_LINK_SPEED_8GHZ:
7045 		rdp_speed = RDP_PS_8GB;
7046 		break;
7047 	case LPFC_LINK_SPEED_10GHZ:
7048 		rdp_speed = RDP_PS_10GB;
7049 		break;
7050 	case LPFC_LINK_SPEED_16GHZ:
7051 		rdp_speed = RDP_PS_16GB;
7052 		break;
7053 	case LPFC_LINK_SPEED_32GHZ:
7054 		rdp_speed = RDP_PS_32GB;
7055 		break;
7056 	case LPFC_LINK_SPEED_64GHZ:
7057 		rdp_speed = RDP_PS_64GB;
7058 		break;
7059 	case LPFC_LINK_SPEED_128GHZ:
7060 		rdp_speed = RDP_PS_128GB;
7061 		break;
7062 	case LPFC_LINK_SPEED_256GHZ:
7063 		rdp_speed = RDP_PS_256GB;
7064 		break;
7065 	default:
7066 		rdp_speed = RDP_PS_UNKNOWN;
7067 		break;
7068 	}
7069 
7070 	desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
7071 
7072 	if (phba->lmt & LMT_256Gb)
7073 		rdp_cap |= RDP_PS_256GB;
7074 	if (phba->lmt & LMT_128Gb)
7075 		rdp_cap |= RDP_PS_128GB;
7076 	if (phba->lmt & LMT_64Gb)
7077 		rdp_cap |= RDP_PS_64GB;
7078 	if (phba->lmt & LMT_32Gb)
7079 		rdp_cap |= RDP_PS_32GB;
7080 	if (phba->lmt & LMT_16Gb)
7081 		rdp_cap |= RDP_PS_16GB;
7082 	if (phba->lmt & LMT_10Gb)
7083 		rdp_cap |= RDP_PS_10GB;
7084 	if (phba->lmt & LMT_8Gb)
7085 		rdp_cap |= RDP_PS_8GB;
7086 	if (phba->lmt & LMT_4Gb)
7087 		rdp_cap |= RDP_PS_4GB;
7088 	if (phba->lmt & LMT_2Gb)
7089 		rdp_cap |= RDP_PS_2GB;
7090 	if (phba->lmt & LMT_1Gb)
7091 		rdp_cap |= RDP_PS_1GB;
7092 
7093 	if (rdp_cap == 0)
7094 		rdp_cap = RDP_CAP_UNKNOWN;
7095 	if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
7096 		rdp_cap |= RDP_CAP_USER_CONFIGURED;
7097 
7098 	desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
7099 	desc->length = cpu_to_be32(sizeof(desc->info));
7100 	return sizeof(struct fc_rdp_port_speed_desc);
7101 }
7102 
7103 static uint32_t
7104 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
7105 		struct lpfc_vport *vport)
7106 {
7107 
7108 	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
7109 
7110 	memcpy(desc->port_names.wwnn, &vport->fc_nodename,
7111 			sizeof(desc->port_names.wwnn));
7112 
7113 	memcpy(desc->port_names.wwpn, &vport->fc_portname,
7114 			sizeof(desc->port_names.wwpn));
7115 
7116 	desc->length = cpu_to_be32(sizeof(desc->port_names));
7117 	return sizeof(struct fc_rdp_port_name_desc);
7118 }
7119 
7120 static uint32_t
7121 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
7122 		struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
7123 {
7124 
7125 	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
7126 	if (test_bit(FC_FABRIC, &vport->fc_flag)) {
7127 		memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
7128 		       sizeof(desc->port_names.wwnn));
7129 
7130 		memcpy(desc->port_names.wwpn, &vport->fabric_portname,
7131 		       sizeof(desc->port_names.wwpn));
7132 	} else {  /* Point to Point */
7133 		memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
7134 		       sizeof(desc->port_names.wwnn));
7135 
7136 		memcpy(desc->port_names.wwpn, &ndlp->nlp_portname,
7137 		       sizeof(desc->port_names.wwpn));
7138 	}
7139 
7140 	desc->length = cpu_to_be32(sizeof(desc->port_names));
7141 	return sizeof(struct fc_rdp_port_name_desc);
7142 }
7143 
7144 static void
7145 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
7146 		int status)
7147 {
7148 	struct lpfc_nodelist *ndlp = rdp_context->ndlp;
7149 	struct lpfc_vport *vport = ndlp->vport;
7150 	struct lpfc_iocbq *elsiocb;
7151 	struct ulp_bde64 *bpl;
7152 	IOCB_t *icmd;
7153 	union lpfc_wqe128 *wqe;
7154 	uint8_t *pcmd;
7155 	struct ls_rjt *stat;
7156 	struct fc_rdp_res_frame *rdp_res;
7157 	uint32_t cmdsize, len;
7158 	uint16_t *flag_ptr;
7159 	int rc;
7160 	u32 ulp_context;
7161 
7162 	if (status != SUCCESS)
7163 		goto error;
7164 
7165 	/* This will change once we know the true size of the RDP payload */
7166 	cmdsize = sizeof(struct fc_rdp_res_frame);
7167 
7168 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
7169 				lpfc_max_els_tries, rdp_context->ndlp,
7170 				rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
7171 	if (!elsiocb)
7172 		goto free_rdp_context;
7173 
7174 	ulp_context = get_job_ulpcontext(phba, elsiocb);
7175 	if (phba->sli_rev == LPFC_SLI_REV4) {
7176 		wqe = &elsiocb->wqe;
7177 		/* ox-id of the frame */
7178 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7179 		       rdp_context->ox_id);
7180 		bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
7181 		       rdp_context->rx_id);
7182 	} else {
7183 		icmd = &elsiocb->iocb;
7184 		icmd->ulpContext = rdp_context->rx_id;
7185 		icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
7186 	}
7187 
7188 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7189 			"2171 Xmit RDP response tag x%x xri x%x, "
7190 			"did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x",
7191 			elsiocb->iotag, ulp_context,
7192 			ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7193 			ndlp->nlp_rpi);
7194 	rdp_res = (struct fc_rdp_res_frame *)elsiocb->cmd_dmabuf->virt;
7195 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7196 	memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
7197 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7198 
7199 	/* Update Alarm and Warning */
7200 	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
7201 	phba->sfp_alarm |= *flag_ptr;
7202 	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
7203 	phba->sfp_warning |= *flag_ptr;
7204 
7205 	/* For RDP payload */
7206 	len = 8;
7207 	len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
7208 					 (len + pcmd), ELS_CMD_RDP);
7209 
7210 	len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
7211 			rdp_context->page_a0, rdp_context->page_a2);
7212 	len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
7213 				  phba);
7214 	len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
7215 				       (len + pcmd), &rdp_context->link_stat);
7216 	len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
7217 					     (len + pcmd), vport);
7218 	len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
7219 					(len + pcmd), vport, ndlp);
7220 	len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
7221 			&rdp_context->link_stat);
7222 	len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
7223 				     &rdp_context->link_stat, vport);
7224 	len += lpfc_rdp_res_oed_temp_desc(phba,
7225 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7226 				rdp_context->page_a2);
7227 	len += lpfc_rdp_res_oed_voltage_desc(phba,
7228 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7229 				rdp_context->page_a2);
7230 	len += lpfc_rdp_res_oed_txbias_desc(phba,
7231 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7232 				rdp_context->page_a2);
7233 	len += lpfc_rdp_res_oed_txpower_desc(phba,
7234 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7235 				rdp_context->page_a2);
7236 	len += lpfc_rdp_res_oed_rxpower_desc(phba,
7237 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
7238 				rdp_context->page_a2);
7239 	len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
7240 				     rdp_context->page_a0, vport);
7241 
7242 	rdp_res->length = cpu_to_be32(len - 8);
7243 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7244 
7245 	/* Now that we know the true size of the payload, update the BPL */
7246 	bpl = (struct ulp_bde64 *)elsiocb->bpl_dmabuf->virt;
7247 	bpl->tus.f.bdeSize = len;
7248 	bpl->tus.f.bdeFlags = 0;
7249 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
7250 
7251 	phba->fc_stat.elsXmitACC++;
7252 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
7253 	if (!elsiocb->ndlp) {
7254 		lpfc_els_free_iocb(phba, elsiocb);
7255 		goto free_rdp_context;
7256 	}
7257 
7258 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7259 	if (rc == IOCB_ERROR) {
7260 		lpfc_els_free_iocb(phba, elsiocb);
7261 		lpfc_nlp_put(ndlp);
7262 	}
7263 
7264 	goto free_rdp_context;
7265 
7266 error:
7267 	cmdsize = 2 * sizeof(uint32_t);
7268 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
7269 			ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
7270 	if (!elsiocb)
7271 		goto free_rdp_context;
7272 
7273 	if (phba->sli_rev == LPFC_SLI_REV4) {
7274 		wqe = &elsiocb->wqe;
7275 		/* ox-id of the frame */
7276 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7277 		       rdp_context->ox_id);
7278 		bf_set(wqe_ctxt_tag,
7279 		       &wqe->xmit_els_rsp.wqe_com,
7280 		       rdp_context->rx_id);
7281 	} else {
7282 		icmd = &elsiocb->iocb;
7283 		icmd->ulpContext = rdp_context->rx_id;
7284 		icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
7285 	}
7286 
7287 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7288 
7289 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
7290 	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
7291 	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7292 
7293 	phba->fc_stat.elsXmitLSRJT++;
7294 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7295 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
7296 	if (!elsiocb->ndlp) {
7297 		lpfc_els_free_iocb(phba, elsiocb);
7298 		goto free_rdp_context;
7299 	}
7300 
7301 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7302 	if (rc == IOCB_ERROR) {
7303 		lpfc_els_free_iocb(phba, elsiocb);
7304 		lpfc_nlp_put(ndlp);
7305 	}
7306 
7307 free_rdp_context:
7308 	/* This reference put is for the original unsolicited RDP. If the
7309 	 * prep failed, there is no reference to remove.
7310 	 */
7311 	lpfc_nlp_put(ndlp);
7312 	kfree(rdp_context);
7313 }
7314 
7315 static int
7316 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
7317 {
7318 	LPFC_MBOXQ_t *mbox = NULL;
7319 	int rc;
7320 
7321 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7322 	if (!mbox) {
7323 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
7324 				"7105 failed to allocate mailbox memory");
7325 		return 1;
7326 	}
7327 
7328 	if (lpfc_sli4_dump_page_a0(phba, mbox))
7329 		goto rdp_fail;
7330 	mbox->vport = rdp_context->ndlp->vport;
7331 	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
7332 	mbox->ctx_u.rdp = rdp_context;
7333 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7334 	if (rc == MBX_NOT_FINISHED) {
7335 		lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
7336 		return 1;
7337 	}
7338 
7339 	return 0;
7340 
7341 rdp_fail:
7342 	mempool_free(mbox, phba->mbox_mem_pool);
7343 	return 1;
7344 }
7345 
7346 int lpfc_get_sfp_info_wait(struct lpfc_hba *phba,
7347 			   struct lpfc_rdp_context *rdp_context)
7348 {
7349 	LPFC_MBOXQ_t *mbox = NULL;
7350 	int rc;
7351 	struct lpfc_dmabuf *mp;
7352 	struct lpfc_dmabuf *mpsave;
7353 	void *virt;
7354 	MAILBOX_t *mb;
7355 
7356 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7357 	if (!mbox) {
7358 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
7359 				"7205 failed to allocate mailbox memory");
7360 		return 1;
7361 	}
7362 
7363 	if (lpfc_sli4_dump_page_a0(phba, mbox))
7364 		goto sfp_fail;
7365 	mp = mbox->ctx_buf;
7366 	mpsave = mp;
7367 	virt = mp->virt;
7368 	if (phba->sli_rev < LPFC_SLI_REV4) {
7369 		mb = &mbox->u.mb;
7370 		mb->un.varDmp.cv = 1;
7371 		mb->un.varDmp.co = 1;
7372 		mb->un.varWords[2] = 0;
7373 		mb->un.varWords[3] = DMP_SFF_PAGE_A0_SIZE / 4;
7374 		mb->un.varWords[4] = 0;
7375 		mb->un.varWords[5] = 0;
7376 		mb->un.varWords[6] = 0;
7377 		mb->un.varWords[7] = 0;
7378 		mb->un.varWords[8] = 0;
7379 		mb->un.varWords[9] = 0;
7380 		mb->un.varWords[10] = 0;
7381 		mbox->in_ext_byte_len = DMP_SFF_PAGE_A0_SIZE;
7382 		mbox->out_ext_byte_len = DMP_SFF_PAGE_A0_SIZE;
7383 		mbox->mbox_offset_word = 5;
7384 		mbox->ext_buf = virt;
7385 	} else {
7386 		bf_set(lpfc_mbx_memory_dump_type3_length,
7387 		       &mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A0_SIZE);
7388 		mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
7389 		mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
7390 	}
7391 	mbox->vport = phba->pport;
7392 	rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_SLI4_CONFIG_TMO);
7393 	if (rc == MBX_NOT_FINISHED) {
7394 		rc = 1;
7395 		goto error;
7396 	}
7397 	if (rc == MBX_TIMEOUT)
7398 		goto error;
7399 	if (phba->sli_rev == LPFC_SLI_REV4)
7400 		mp = mbox->ctx_buf;
7401 	else
7402 		mp = mpsave;
7403 
7404 	if (bf_get(lpfc_mqe_status, &mbox->u.mqe)) {
7405 		rc = 1;
7406 		goto error;
7407 	}
7408 
7409 	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a0,
7410 			     DMP_SFF_PAGE_A0_SIZE);
7411 
7412 	memset(mbox, 0, sizeof(*mbox));
7413 	memset(mp->virt, 0, DMP_SFF_PAGE_A2_SIZE);
7414 	INIT_LIST_HEAD(&mp->list);
7415 
7416 	/* save address for completion */
7417 	mbox->ctx_buf = mp;
7418 	mbox->vport = phba->pport;
7419 
7420 	bf_set(lpfc_mqe_command, &mbox->u.mqe, MBX_DUMP_MEMORY);
7421 	bf_set(lpfc_mbx_memory_dump_type3_type,
7422 	       &mbox->u.mqe.un.mem_dump_type3, DMP_LMSD);
7423 	bf_set(lpfc_mbx_memory_dump_type3_link,
7424 	       &mbox->u.mqe.un.mem_dump_type3, phba->sli4_hba.physical_port);
7425 	bf_set(lpfc_mbx_memory_dump_type3_page_no,
7426 	       &mbox->u.mqe.un.mem_dump_type3, DMP_PAGE_A2);
7427 	if (phba->sli_rev < LPFC_SLI_REV4) {
7428 		mb = &mbox->u.mb;
7429 		mb->un.varDmp.cv = 1;
7430 		mb->un.varDmp.co = 1;
7431 		mb->un.varWords[2] = 0;
7432 		mb->un.varWords[3] = DMP_SFF_PAGE_A2_SIZE / 4;
7433 		mb->un.varWords[4] = 0;
7434 		mb->un.varWords[5] = 0;
7435 		mb->un.varWords[6] = 0;
7436 		mb->un.varWords[7] = 0;
7437 		mb->un.varWords[8] = 0;
7438 		mb->un.varWords[9] = 0;
7439 		mb->un.varWords[10] = 0;
7440 		mbox->in_ext_byte_len = DMP_SFF_PAGE_A2_SIZE;
7441 		mbox->out_ext_byte_len = DMP_SFF_PAGE_A2_SIZE;
7442 		mbox->mbox_offset_word = 5;
7443 		mbox->ext_buf = virt;
7444 	} else {
7445 		bf_set(lpfc_mbx_memory_dump_type3_length,
7446 		       &mbox->u.mqe.un.mem_dump_type3, DMP_SFF_PAGE_A2_SIZE);
7447 		mbox->u.mqe.un.mem_dump_type3.addr_lo = putPaddrLow(mp->phys);
7448 		mbox->u.mqe.un.mem_dump_type3.addr_hi = putPaddrHigh(mp->phys);
7449 	}
7450 
7451 	rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_SLI4_CONFIG_TMO);
7452 
7453 	if (rc == MBX_TIMEOUT)
7454 		goto error;
7455 	if (bf_get(lpfc_mqe_status, &mbox->u.mqe)) {
7456 		rc = 1;
7457 		goto error;
7458 	}
7459 	rc = 0;
7460 
7461 	lpfc_sli_bemem_bcopy(mp->virt, &rdp_context->page_a2,
7462 			     DMP_SFF_PAGE_A2_SIZE);
7463 
7464 error:
7465 	if (mbox->mbox_flag & LPFC_MBX_WAKE) {
7466 		mbox->ctx_buf = mpsave;
7467 		lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
7468 	}
7469 
7470 	return rc;
7471 
7472 sfp_fail:
7473 	mempool_free(mbox, phba->mbox_mem_pool);
7474 	return 1;
7475 }
7476 
7477 /*
7478  * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
7479  * @vport: pointer to a host virtual N_Port data structure.
7480  * @cmdiocb: pointer to lpfc command iocb data structure.
7481  * @ndlp: pointer to a node-list data structure.
7482  *
7483  * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
7484  * IOCB. First, the payload of the unsolicited RDP is checked.
7485  * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
7486  * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
7487  * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
7488  * gather all data and send RDP response.
7489  *
7490  * Return code
7491  *   0 - Sent the acc response
7492  *   1 - Sent the reject response.
7493  */
7494 static int
7495 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7496 		struct lpfc_nodelist *ndlp)
7497 {
7498 	struct lpfc_hba *phba = vport->phba;
7499 	struct lpfc_dmabuf *pcmd;
7500 	uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
7501 	struct fc_rdp_req_frame *rdp_req;
7502 	struct lpfc_rdp_context *rdp_context;
7503 	union lpfc_wqe128 *cmd = NULL;
7504 	struct ls_rjt stat;
7505 
7506 	if (phba->sli_rev < LPFC_SLI_REV4 ||
7507 	    bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7508 						LPFC_SLI_INTF_IF_TYPE_2) {
7509 		rjt_err = LSRJT_UNABLE_TPC;
7510 		rjt_expl = LSEXP_REQ_UNSUPPORTED;
7511 		goto error;
7512 	}
7513 
7514 	if (phba->sli_rev < LPFC_SLI_REV4 ||
7515 	    test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
7516 		rjt_err = LSRJT_UNABLE_TPC;
7517 		rjt_expl = LSEXP_REQ_UNSUPPORTED;
7518 		goto error;
7519 	}
7520 
7521 	pcmd = cmdiocb->cmd_dmabuf;
7522 	rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
7523 
7524 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7525 			 "2422 ELS RDP Request "
7526 			 "dec len %d tag x%x port_id %d len %d\n",
7527 			 be32_to_cpu(rdp_req->rdp_des_length),
7528 			 be32_to_cpu(rdp_req->nport_id_desc.tag),
7529 			 be32_to_cpu(rdp_req->nport_id_desc.nport_id),
7530 			 be32_to_cpu(rdp_req->nport_id_desc.length));
7531 
7532 	if (sizeof(struct fc_rdp_nport_desc) !=
7533 			be32_to_cpu(rdp_req->rdp_des_length))
7534 		goto rjt_logerr;
7535 	if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
7536 		goto rjt_logerr;
7537 	if (RDP_NPORT_ID_SIZE !=
7538 			be32_to_cpu(rdp_req->nport_id_desc.length))
7539 		goto rjt_logerr;
7540 	rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
7541 	if (!rdp_context) {
7542 		rjt_err = LSRJT_UNABLE_TPC;
7543 		goto error;
7544 	}
7545 
7546 	cmd = &cmdiocb->wqe;
7547 	rdp_context->ndlp = lpfc_nlp_get(ndlp);
7548 	if (!rdp_context->ndlp) {
7549 		kfree(rdp_context);
7550 		rjt_err = LSRJT_UNABLE_TPC;
7551 		goto error;
7552 	}
7553 	rdp_context->ox_id = bf_get(wqe_rcvoxid,
7554 				    &cmd->xmit_els_rsp.wqe_com);
7555 	rdp_context->rx_id = bf_get(wqe_ctxt_tag,
7556 				    &cmd->xmit_els_rsp.wqe_com);
7557 	rdp_context->cmpl = lpfc_els_rdp_cmpl;
7558 	if (lpfc_get_rdp_info(phba, rdp_context)) {
7559 		lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
7560 				 "2423 Unable to send mailbox");
7561 		kfree(rdp_context);
7562 		rjt_err = LSRJT_UNABLE_TPC;
7563 		lpfc_nlp_put(ndlp);
7564 		goto error;
7565 	}
7566 
7567 	return 0;
7568 
7569 rjt_logerr:
7570 	rjt_err = LSRJT_LOGICAL_ERR;
7571 
7572 error:
7573 	memset(&stat, 0, sizeof(stat));
7574 	stat.un.b.lsRjtRsnCode = rjt_err;
7575 	stat.un.b.lsRjtRsnCodeExp = rjt_expl;
7576 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7577 	return 1;
7578 }
7579 
7580 
7581 static void
7582 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7583 {
7584 	MAILBOX_t *mb;
7585 	IOCB_t *icmd;
7586 	union lpfc_wqe128 *wqe;
7587 	uint8_t *pcmd;
7588 	struct lpfc_iocbq *elsiocb;
7589 	struct lpfc_nodelist *ndlp;
7590 	struct ls_rjt *stat;
7591 	union lpfc_sli4_cfg_shdr *shdr;
7592 	struct lpfc_lcb_context *lcb_context;
7593 	struct fc_lcb_res_frame *lcb_res;
7594 	uint32_t cmdsize, shdr_status, shdr_add_status;
7595 	int rc;
7596 
7597 	mb = &pmb->u.mb;
7598 	lcb_context = pmb->ctx_u.lcb;
7599 	ndlp = lcb_context->ndlp;
7600 	memset(&pmb->ctx_u, 0, sizeof(pmb->ctx_u));
7601 	pmb->ctx_buf = NULL;
7602 
7603 	shdr = (union lpfc_sli4_cfg_shdr *)
7604 			&pmb->u.mqe.un.beacon_config.header.cfg_shdr;
7605 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7606 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7607 
7608 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
7609 				"0194 SET_BEACON_CONFIG mailbox "
7610 				"completed with status x%x add_status x%x,"
7611 				" mbx status x%x\n",
7612 				shdr_status, shdr_add_status, mb->mbxStatus);
7613 
7614 	if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
7615 	    (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
7616 	    (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
7617 		mempool_free(pmb, phba->mbox_mem_pool);
7618 		goto error;
7619 	}
7620 
7621 	mempool_free(pmb, phba->mbox_mem_pool);
7622 	cmdsize = sizeof(struct fc_lcb_res_frame);
7623 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7624 			lpfc_max_els_tries, ndlp,
7625 			ndlp->nlp_DID, ELS_CMD_ACC);
7626 
7627 	/* Decrement the ndlp reference count from previous mbox command */
7628 	lpfc_nlp_put(ndlp);
7629 
7630 	if (!elsiocb)
7631 		goto free_lcb_context;
7632 
7633 	lcb_res = (struct fc_lcb_res_frame *)elsiocb->cmd_dmabuf->virt;
7634 
7635 	memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
7636 
7637 	if (phba->sli_rev == LPFC_SLI_REV4) {
7638 		wqe = &elsiocb->wqe;
7639 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, lcb_context->rx_id);
7640 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7641 		       lcb_context->ox_id);
7642 	} else {
7643 		icmd = &elsiocb->iocb;
7644 		icmd->ulpContext = lcb_context->rx_id;
7645 		icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7646 	}
7647 
7648 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7649 	*((uint32_t *)(pcmd)) = ELS_CMD_ACC;
7650 	lcb_res->lcb_sub_command = lcb_context->sub_command;
7651 	lcb_res->lcb_type = lcb_context->type;
7652 	lcb_res->capability = lcb_context->capability;
7653 	lcb_res->lcb_frequency = lcb_context->frequency;
7654 	lcb_res->lcb_duration = lcb_context->duration;
7655 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7656 	phba->fc_stat.elsXmitACC++;
7657 
7658 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
7659 	if (!elsiocb->ndlp) {
7660 		lpfc_els_free_iocb(phba, elsiocb);
7661 		goto out;
7662 	}
7663 
7664 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7665 	if (rc == IOCB_ERROR) {
7666 		lpfc_els_free_iocb(phba, elsiocb);
7667 		lpfc_nlp_put(ndlp);
7668 	}
7669  out:
7670 	kfree(lcb_context);
7671 	return;
7672 
7673 error:
7674 	cmdsize = sizeof(struct fc_lcb_res_frame);
7675 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7676 				     lpfc_max_els_tries, ndlp,
7677 				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
7678 	lpfc_nlp_put(ndlp);
7679 	if (!elsiocb)
7680 		goto free_lcb_context;
7681 
7682 	if (phba->sli_rev == LPFC_SLI_REV4) {
7683 		wqe = &elsiocb->wqe;
7684 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, lcb_context->rx_id);
7685 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7686 		       lcb_context->ox_id);
7687 	} else {
7688 		icmd = &elsiocb->iocb;
7689 		icmd->ulpContext = lcb_context->rx_id;
7690 		icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
7691 	}
7692 
7693 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
7694 
7695 	*((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
7696 	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
7697 	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7698 
7699 	if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
7700 		stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
7701 
7702 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
7703 	phba->fc_stat.elsXmitLSRJT++;
7704 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
7705 	if (!elsiocb->ndlp) {
7706 		lpfc_els_free_iocb(phba, elsiocb);
7707 		goto free_lcb_context;
7708 	}
7709 
7710 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7711 	if (rc == IOCB_ERROR) {
7712 		lpfc_els_free_iocb(phba, elsiocb);
7713 		lpfc_nlp_put(ndlp);
7714 	}
7715 free_lcb_context:
7716 	kfree(lcb_context);
7717 }
7718 
7719 static int
7720 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
7721 		     struct lpfc_lcb_context *lcb_context,
7722 		     uint32_t beacon_state)
7723 {
7724 	struct lpfc_hba *phba = vport->phba;
7725 	union lpfc_sli4_cfg_shdr *cfg_shdr;
7726 	LPFC_MBOXQ_t *mbox = NULL;
7727 	uint32_t len;
7728 	int rc;
7729 
7730 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7731 	if (!mbox)
7732 		return 1;
7733 
7734 	cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
7735 	len = sizeof(struct lpfc_mbx_set_beacon_config) -
7736 		sizeof(struct lpfc_sli4_cfg_mhdr);
7737 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7738 			 LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
7739 			 LPFC_SLI4_MBX_EMBED);
7740 	mbox->ctx_u.lcb = lcb_context;
7741 	mbox->vport = phba->pport;
7742 	mbox->mbox_cmpl = lpfc_els_lcb_rsp;
7743 	bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
7744 	       phba->sli4_hba.physical_port);
7745 	bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
7746 	       beacon_state);
7747 	mbox->u.mqe.un.beacon_config.word5 = 0;		/* Reserved */
7748 
7749 	/*
7750 	 *	Check bv1s bit before issuing the mailbox
7751 	 *	if bv1s == 1, LCB V1 supported
7752 	 *	else, LCB V0 supported
7753 	 */
7754 
7755 	if (phba->sli4_hba.pc_sli4_params.bv1s) {
7756 		/* COMMON_SET_BEACON_CONFIG_V1 */
7757 		cfg_shdr->request.word9 = BEACON_VERSION_V1;
7758 		lcb_context->capability |= LCB_CAPABILITY_DURATION;
7759 		bf_set(lpfc_mbx_set_beacon_port_type,
7760 		       &mbox->u.mqe.un.beacon_config, 0);
7761 		bf_set(lpfc_mbx_set_beacon_duration_v1,
7762 		       &mbox->u.mqe.un.beacon_config,
7763 		       be16_to_cpu(lcb_context->duration));
7764 	} else {
7765 		/* COMMON_SET_BEACON_CONFIG_V0 */
7766 		if (be16_to_cpu(lcb_context->duration) != 0) {
7767 			mempool_free(mbox, phba->mbox_mem_pool);
7768 			return 1;
7769 		}
7770 		cfg_shdr->request.word9 = BEACON_VERSION_V0;
7771 		lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
7772 		bf_set(lpfc_mbx_set_beacon_state,
7773 		       &mbox->u.mqe.un.beacon_config, beacon_state);
7774 		bf_set(lpfc_mbx_set_beacon_port_type,
7775 		       &mbox->u.mqe.un.beacon_config, 1);
7776 		bf_set(lpfc_mbx_set_beacon_duration,
7777 		       &mbox->u.mqe.un.beacon_config,
7778 		       be16_to_cpu(lcb_context->duration));
7779 	}
7780 
7781 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7782 	if (rc == MBX_NOT_FINISHED) {
7783 		mempool_free(mbox, phba->mbox_mem_pool);
7784 		return 1;
7785 	}
7786 
7787 	return 0;
7788 }
7789 
7790 
7791 /**
7792  * lpfc_els_rcv_lcb - Process an unsolicited LCB
7793  * @vport: pointer to a host virtual N_Port data structure.
7794  * @cmdiocb: pointer to lpfc command iocb data structure.
7795  * @ndlp: pointer to a node-list data structure.
7796  *
7797  * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
7798  * First, the payload of the unsolicited LCB is checked.
7799  * Then based on Subcommand beacon will either turn on or off.
7800  *
7801  * Return code
7802  * 0 - Sent the acc response
7803  * 1 - Sent the reject response.
7804  **/
7805 static int
7806 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7807 		 struct lpfc_nodelist *ndlp)
7808 {
7809 	struct lpfc_hba *phba = vport->phba;
7810 	struct lpfc_dmabuf *pcmd;
7811 	uint8_t *lp;
7812 	struct fc_lcb_request_frame *beacon;
7813 	struct lpfc_lcb_context *lcb_context;
7814 	u8 state, rjt_err = 0;
7815 	struct ls_rjt stat;
7816 
7817 	pcmd = cmdiocb->cmd_dmabuf;
7818 	lp = (uint8_t *)pcmd->virt;
7819 	beacon = (struct fc_lcb_request_frame *)pcmd->virt;
7820 
7821 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7822 			"0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
7823 			"type x%x frequency %x duration x%x\n",
7824 			lp[0], lp[1], lp[2],
7825 			beacon->lcb_command,
7826 			beacon->lcb_sub_command,
7827 			beacon->lcb_type,
7828 			beacon->lcb_frequency,
7829 			be16_to_cpu(beacon->lcb_duration));
7830 
7831 	if (beacon->lcb_sub_command != LPFC_LCB_ON &&
7832 	    beacon->lcb_sub_command != LPFC_LCB_OFF) {
7833 		rjt_err = LSRJT_CMD_UNSUPPORTED;
7834 		goto rjt;
7835 	}
7836 
7837 	if (phba->sli_rev < LPFC_SLI_REV4  ||
7838 	    test_bit(HBA_FCOE_MODE, &phba->hba_flag) ||
7839 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
7840 	    LPFC_SLI_INTF_IF_TYPE_2)) {
7841 		rjt_err = LSRJT_CMD_UNSUPPORTED;
7842 		goto rjt;
7843 	}
7844 
7845 	lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
7846 	if (!lcb_context) {
7847 		rjt_err = LSRJT_UNABLE_TPC;
7848 		goto rjt;
7849 	}
7850 
7851 	state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
7852 	lcb_context->sub_command = beacon->lcb_sub_command;
7853 	lcb_context->capability	= 0;
7854 	lcb_context->type = beacon->lcb_type;
7855 	lcb_context->frequency = beacon->lcb_frequency;
7856 	lcb_context->duration = beacon->lcb_duration;
7857 	lcb_context->ox_id = get_job_rcvoxid(phba, cmdiocb);
7858 	lcb_context->rx_id = get_job_ulpcontext(phba, cmdiocb);
7859 	lcb_context->ndlp = lpfc_nlp_get(ndlp);
7860 	if (!lcb_context->ndlp) {
7861 		rjt_err = LSRJT_UNABLE_TPC;
7862 		goto rjt_free;
7863 	}
7864 
7865 	if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
7866 		lpfc_printf_vlog(ndlp->vport, KERN_ERR, LOG_TRACE_EVENT,
7867 				 "0193 failed to send mail box");
7868 		lpfc_nlp_put(ndlp);
7869 		rjt_err = LSRJT_UNABLE_TPC;
7870 		goto rjt_free;
7871 	}
7872 	return 0;
7873 
7874 rjt_free:
7875 	kfree(lcb_context);
7876 rjt:
7877 	memset(&stat, 0, sizeof(stat));
7878 	stat.un.b.lsRjtRsnCode = rjt_err;
7879 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7880 	return 1;
7881 }
7882 
7883 
7884 /**
7885  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
7886  * @vport: pointer to a host virtual N_Port data structure.
7887  *
7888  * This routine cleans up any Registration State Change Notification
7889  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
7890  * @vport together with the host_lock is used to prevent multiple thread
7891  * trying to access the RSCN array on a same @vport at the same time.
7892  **/
7893 void
7894 lpfc_els_flush_rscn(struct lpfc_vport *vport)
7895 {
7896 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7897 	struct lpfc_hba  *phba = vport->phba;
7898 	int i;
7899 
7900 	spin_lock_irq(shost->host_lock);
7901 	if (vport->fc_rscn_flush) {
7902 		/* Another thread is walking fc_rscn_id_list on this vport */
7903 		spin_unlock_irq(shost->host_lock);
7904 		return;
7905 	}
7906 	/* Indicate we are walking lpfc_els_flush_rscn on this vport */
7907 	vport->fc_rscn_flush = 1;
7908 	spin_unlock_irq(shost->host_lock);
7909 
7910 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7911 		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
7912 		vport->fc_rscn_id_list[i] = NULL;
7913 	}
7914 	clear_bit(FC_RSCN_MODE, &vport->fc_flag);
7915 	clear_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
7916 	spin_lock_irq(shost->host_lock);
7917 	vport->fc_rscn_id_cnt = 0;
7918 	spin_unlock_irq(shost->host_lock);
7919 	lpfc_can_disctmo(vport);
7920 	/* Indicate we are done walking this fc_rscn_id_list */
7921 	vport->fc_rscn_flush = 0;
7922 }
7923 
7924 /**
7925  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
7926  * @vport: pointer to a host virtual N_Port data structure.
7927  * @did: remote destination port identifier.
7928  *
7929  * This routine checks whether there is any pending Registration State
7930  * Configuration Notification (RSCN) to a @did on @vport.
7931  *
7932  * Return code
7933  *   None zero - The @did matched with a pending rscn
7934  *   0 - not able to match @did with a pending rscn
7935  **/
7936 int
7937 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
7938 {
7939 	D_ID ns_did;
7940 	D_ID rscn_did;
7941 	uint32_t *lp;
7942 	uint32_t payload_len, i;
7943 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7944 
7945 	ns_did.un.word = did;
7946 
7947 	/* Never match fabric nodes for RSCNs */
7948 	if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
7949 		return 0;
7950 
7951 	/* If we are doing a FULL RSCN rediscovery, match everything */
7952 	if (test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag))
7953 		return did;
7954 
7955 	spin_lock_irq(shost->host_lock);
7956 	if (vport->fc_rscn_flush) {
7957 		/* Another thread is walking fc_rscn_id_list on this vport */
7958 		spin_unlock_irq(shost->host_lock);
7959 		return 0;
7960 	}
7961 	/* Indicate we are walking fc_rscn_id_list on this vport */
7962 	vport->fc_rscn_flush = 1;
7963 	spin_unlock_irq(shost->host_lock);
7964 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
7965 		lp = vport->fc_rscn_id_list[i]->virt;
7966 		payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
7967 		payload_len -= sizeof(uint32_t);	/* take off word 0 */
7968 		while (payload_len) {
7969 			rscn_did.un.word = be32_to_cpu(*lp++);
7970 			payload_len -= sizeof(uint32_t);
7971 			switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
7972 			case RSCN_ADDRESS_FORMAT_PORT:
7973 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7974 				    && (ns_did.un.b.area == rscn_did.un.b.area)
7975 				    && (ns_did.un.b.id == rscn_did.un.b.id))
7976 					goto return_did_out;
7977 				break;
7978 			case RSCN_ADDRESS_FORMAT_AREA:
7979 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
7980 				    && (ns_did.un.b.area == rscn_did.un.b.area))
7981 					goto return_did_out;
7982 				break;
7983 			case RSCN_ADDRESS_FORMAT_DOMAIN:
7984 				if (ns_did.un.b.domain == rscn_did.un.b.domain)
7985 					goto return_did_out;
7986 				break;
7987 			case RSCN_ADDRESS_FORMAT_FABRIC:
7988 				goto return_did_out;
7989 			}
7990 		}
7991 	}
7992 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
7993 	vport->fc_rscn_flush = 0;
7994 	return 0;
7995 return_did_out:
7996 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
7997 	vport->fc_rscn_flush = 0;
7998 	return did;
7999 }
8000 
8001 /**
8002  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
8003  * @vport: pointer to a host virtual N_Port data structure.
8004  *
8005  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
8006  * state machine for a @vport's nodes that are with pending RSCN (Registration
8007  * State Change Notification).
8008  *
8009  * Return code
8010  *   0 - Successful (currently alway return 0)
8011  **/
8012 static int
8013 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
8014 {
8015 	struct lpfc_nodelist *ndlp = NULL, *n;
8016 
8017 	/* Move all affected nodes by pending RSCNs to NPR state. */
8018 	list_for_each_entry_safe(ndlp, n, &vport->fc_nodes, nlp_listp) {
8019 		if (test_bit(FC_UNLOADING, &vport->load_flag)) {
8020 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8021 					 "1000 %s Unloading set\n",
8022 					 __func__);
8023 			return 0;
8024 		}
8025 
8026 		if ((ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
8027 		    !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
8028 			continue;
8029 
8030 		/* NVME Target mode does not do RSCN Recovery. */
8031 		if (vport->phba->nvmet_support)
8032 			continue;
8033 
8034 		/* If we are in the process of doing discovery on this
8035 		 * NPort, let it continue on its own.
8036 		 */
8037 		switch (ndlp->nlp_state) {
8038 		case  NLP_STE_PLOGI_ISSUE:
8039 		case  NLP_STE_ADISC_ISSUE:
8040 		case  NLP_STE_REG_LOGIN_ISSUE:
8041 		case  NLP_STE_PRLI_ISSUE:
8042 		case  NLP_STE_LOGO_ISSUE:
8043 			continue;
8044 		}
8045 
8046 		lpfc_disc_state_machine(vport, ndlp, NULL,
8047 					NLP_EVT_DEVICE_RECOVERY);
8048 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
8049 	}
8050 	return 0;
8051 }
8052 
8053 /**
8054  * lpfc_send_rscn_event - Send an RSCN event to management application
8055  * @vport: pointer to a host virtual N_Port data structure.
8056  * @cmdiocb: pointer to lpfc command iocb data structure.
8057  *
8058  * lpfc_send_rscn_event sends an RSCN netlink event to management
8059  * applications.
8060  */
8061 static void
8062 lpfc_send_rscn_event(struct lpfc_vport *vport,
8063 		struct lpfc_iocbq *cmdiocb)
8064 {
8065 	struct lpfc_dmabuf *pcmd;
8066 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8067 	uint32_t *payload_ptr;
8068 	uint32_t payload_len;
8069 	struct lpfc_rscn_event_header *rscn_event_data;
8070 
8071 	pcmd = cmdiocb->cmd_dmabuf;
8072 	payload_ptr = (uint32_t *) pcmd->virt;
8073 	payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
8074 
8075 	rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
8076 		payload_len, GFP_KERNEL);
8077 	if (!rscn_event_data) {
8078 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8079 			"0147 Failed to allocate memory for RSCN event\n");
8080 		return;
8081 	}
8082 	rscn_event_data->event_type = FC_REG_RSCN_EVENT;
8083 	rscn_event_data->payload_length = payload_len;
8084 	memcpy(rscn_event_data->rscn_payload, payload_ptr,
8085 		payload_len);
8086 
8087 	fc_host_post_vendor_event(shost,
8088 		fc_get_event_number(),
8089 		sizeof(struct lpfc_rscn_event_header) + payload_len,
8090 		(char *)rscn_event_data,
8091 		LPFC_NL_VENDOR_ID);
8092 
8093 	kfree(rscn_event_data);
8094 }
8095 
8096 /**
8097  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
8098  * @vport: pointer to a host virtual N_Port data structure.
8099  * @cmdiocb: pointer to lpfc command iocb data structure.
8100  * @ndlp: pointer to a node-list data structure.
8101  *
8102  * This routine processes an unsolicited RSCN (Registration State Change
8103  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
8104  * to invoke fc_host_post_event() routine to the FC transport layer. If the
8105  * discover state machine is about to begin discovery, it just accepts the
8106  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
8107  * contains N_Port IDs for other vports on this HBA, it just accepts the
8108  * RSCN and ignore processing it. If the state machine is in the recovery
8109  * state, the fc_rscn_id_list of this @vport is walked and the
8110  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
8111  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
8112  * routine is invoked to handle the RSCN event.
8113  *
8114  * Return code
8115  *   0 - Just sent the acc response
8116  *   1 - Sent the acc response and waited for name server completion
8117  **/
8118 static int
8119 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8120 		  struct lpfc_nodelist *ndlp)
8121 {
8122 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8123 	struct lpfc_hba  *phba = vport->phba;
8124 	struct lpfc_dmabuf *pcmd;
8125 	uint32_t *lp, *datap;
8126 	uint32_t payload_len, length, nportid, *cmd;
8127 	int rscn_cnt;
8128 	int rscn_id = 0, hba_id = 0;
8129 	int i, tmo;
8130 
8131 	pcmd = cmdiocb->cmd_dmabuf;
8132 	lp = (uint32_t *) pcmd->virt;
8133 
8134 	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
8135 	payload_len -= sizeof(uint32_t);	/* take off word 0 */
8136 	/* RSCN received */
8137 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8138 			 "0214 RSCN received Data: x%lx x%x x%x x%x\n",
8139 			 vport->fc_flag, payload_len, *lp,
8140 			 vport->fc_rscn_id_cnt);
8141 
8142 	/* Send an RSCN event to the management application */
8143 	lpfc_send_rscn_event(vport, cmdiocb);
8144 
8145 	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
8146 		fc_host_post_event(shost, fc_get_event_number(),
8147 			FCH_EVT_RSCN, lp[i]);
8148 
8149 	/* Check if RSCN is coming from a direct-connected remote NPort */
8150 	if (test_bit(FC_PT2PT, &vport->fc_flag)) {
8151 		/* If so, just ACC it, no other action needed for now */
8152 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8153 				 "2024 pt2pt RSCN %08x Data: x%lx x%x\n",
8154 				 *lp, vport->fc_flag, payload_len);
8155 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8156 
8157 		/* Check to see if we need to NVME rescan this target
8158 		 * remoteport.
8159 		 */
8160 		if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
8161 		    ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
8162 			lpfc_nvme_rescan_port(vport, ndlp);
8163 		return 0;
8164 	}
8165 
8166 	/* If we are about to begin discovery, just ACC the RSCN.
8167 	 * Discovery processing will satisfy it.
8168 	 */
8169 	if (vport->port_state <= LPFC_NS_QRY) {
8170 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8171 			"RCV RSCN ignore: did:x%x/ste:x%x flg:x%lx",
8172 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
8173 
8174 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8175 		return 0;
8176 	}
8177 
8178 	/* If this RSCN just contains NPortIDs for other vports on this HBA,
8179 	 * just ACC and ignore it.
8180 	 */
8181 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
8182 		!(vport->cfg_peer_port_login)) {
8183 		i = payload_len;
8184 		datap = lp;
8185 		while (i > 0) {
8186 			nportid = *datap++;
8187 			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
8188 			i -= sizeof(uint32_t);
8189 			rscn_id++;
8190 			if (lpfc_find_vport_by_did(phba, nportid))
8191 				hba_id++;
8192 		}
8193 		if (rscn_id == hba_id) {
8194 			/* ALL NPortIDs in RSCN are on HBA */
8195 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8196 					 "0219 Ignore RSCN "
8197 					 "Data: x%lx x%x x%x x%x\n",
8198 					 vport->fc_flag, payload_len,
8199 					 *lp, vport->fc_rscn_id_cnt);
8200 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8201 				"RCV RSCN vport:  did:x%x/ste:x%x flg:x%lx",
8202 				ndlp->nlp_DID, vport->port_state,
8203 				ndlp->nlp_flag);
8204 
8205 			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
8206 				ndlp, NULL);
8207 			/* Restart disctmo if its already running */
8208 			if (test_bit(FC_DISC_TMO, &vport->fc_flag)) {
8209 				tmo = ((phba->fc_ratov * 3) + 3);
8210 				mod_timer(&vport->fc_disctmo,
8211 					  jiffies + secs_to_jiffies(tmo));
8212 			}
8213 			return 0;
8214 		}
8215 	}
8216 
8217 	spin_lock_irq(shost->host_lock);
8218 	if (vport->fc_rscn_flush) {
8219 		/* Another thread is walking fc_rscn_id_list on this vport */
8220 		spin_unlock_irq(shost->host_lock);
8221 		set_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
8222 		/* Send back ACC */
8223 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8224 		return 0;
8225 	}
8226 	/* Indicate we are walking fc_rscn_id_list on this vport */
8227 	vport->fc_rscn_flush = 1;
8228 	spin_unlock_irq(shost->host_lock);
8229 	/* Get the array count after successfully have the token */
8230 	rscn_cnt = vport->fc_rscn_id_cnt;
8231 	/* If we are already processing an RSCN, save the received
8232 	 * RSCN payload buffer, cmdiocb->cmd_dmabuf to process later.
8233 	 */
8234 	if (test_bit(FC_RSCN_MODE, &vport->fc_flag) ||
8235 	    test_bit(FC_NDISC_ACTIVE, &vport->fc_flag)) {
8236 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8237 			"RCV RSCN defer:  did:x%x/ste:x%x flg:x%lx",
8238 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
8239 
8240 		set_bit(FC_RSCN_DEFERRED, &vport->fc_flag);
8241 
8242 		/* Restart disctmo if its already running */
8243 		if (test_bit(FC_DISC_TMO, &vport->fc_flag)) {
8244 			tmo = ((phba->fc_ratov * 3) + 3);
8245 			mod_timer(&vport->fc_disctmo,
8246 				  jiffies + secs_to_jiffies(tmo));
8247 		}
8248 		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
8249 		    !test_bit(FC_RSCN_DISCOVERY, &vport->fc_flag)) {
8250 			set_bit(FC_RSCN_MODE, &vport->fc_flag);
8251 			if (rscn_cnt) {
8252 				cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
8253 				length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
8254 			}
8255 			if ((rscn_cnt) &&
8256 			    (payload_len + length <= LPFC_BPL_SIZE)) {
8257 				*cmd &= ELS_CMD_MASK;
8258 				*cmd |= cpu_to_be32(payload_len + length);
8259 				memcpy(((uint8_t *)cmd) + length, lp,
8260 				       payload_len);
8261 			} else {
8262 				vport->fc_rscn_id_list[rscn_cnt] = pcmd;
8263 				vport->fc_rscn_id_cnt++;
8264 				/* If we zero, cmdiocb->cmd_dmabuf, the calling
8265 				 * routine will not try to free it.
8266 				 */
8267 				cmdiocb->cmd_dmabuf = NULL;
8268 			}
8269 			/* Deferred RSCN */
8270 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8271 					 "0235 Deferred RSCN "
8272 					 "Data: x%x x%lx x%x\n",
8273 					 vport->fc_rscn_id_cnt, vport->fc_flag,
8274 					 vport->port_state);
8275 		} else {
8276 			set_bit(FC_RSCN_DISCOVERY, &vport->fc_flag);
8277 			/* ReDiscovery RSCN */
8278 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8279 					 "0234 ReDiscovery RSCN "
8280 					 "Data: x%x x%lx x%x\n",
8281 					 vport->fc_rscn_id_cnt, vport->fc_flag,
8282 					 vport->port_state);
8283 		}
8284 		/* Indicate we are done walking fc_rscn_id_list on this vport */
8285 		vport->fc_rscn_flush = 0;
8286 		/* Send back ACC */
8287 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8288 		/* send RECOVERY event for ALL nodes that match RSCN payload */
8289 		lpfc_rscn_recovery_check(vport);
8290 		return 0;
8291 	}
8292 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8293 		"RCV RSCN:        did:x%x/ste:x%x flg:x%lx",
8294 		ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
8295 
8296 	set_bit(FC_RSCN_MODE, &vport->fc_flag);
8297 	vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
8298 	/* Indicate we are done walking fc_rscn_id_list on this vport */
8299 	vport->fc_rscn_flush = 0;
8300 	/*
8301 	 * If we zero, cmdiocb->cmd_dmabuf, the calling routine will
8302 	 * not try to free it.
8303 	 */
8304 	cmdiocb->cmd_dmabuf = NULL;
8305 	lpfc_set_disctmo(vport);
8306 	/* Send back ACC */
8307 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8308 	/* send RECOVERY event for ALL nodes that match RSCN payload */
8309 	lpfc_rscn_recovery_check(vport);
8310 	return lpfc_els_handle_rscn(vport);
8311 }
8312 
8313 /**
8314  * lpfc_els_handle_rscn - Handle rscn for a vport
8315  * @vport: pointer to a host virtual N_Port data structure.
8316  *
8317  * This routine handles the Registration State Configuration Notification
8318  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
8319  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
8320  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
8321  * NameServer shall be issued. If CT command to the NameServer fails to be
8322  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
8323  * RSCN activities with the @vport.
8324  *
8325  * Return code
8326  *   0 - Cleaned up rscn on the @vport
8327  *   1 - Wait for plogi to name server before proceed
8328  **/
8329 int
8330 lpfc_els_handle_rscn(struct lpfc_vport *vport)
8331 {
8332 	struct lpfc_nodelist *ndlp;
8333 	struct lpfc_hba  *phba = vport->phba;
8334 
8335 	/* Ignore RSCN if the port is being torn down. */
8336 	if (test_bit(FC_UNLOADING, &vport->load_flag)) {
8337 		lpfc_els_flush_rscn(vport);
8338 		return 0;
8339 	}
8340 
8341 	/* Start timer for RSCN processing */
8342 	lpfc_set_disctmo(vport);
8343 
8344 	/* RSCN processed */
8345 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
8346 			 "0215 RSCN processed Data: x%lx x%x x%x x%x x%x x%x\n",
8347 			 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
8348 			 vport->port_state, vport->num_disc_nodes,
8349 			 vport->gidft_inp);
8350 
8351 	/* To process RSCN, first compare RSCN data with NameServer */
8352 	vport->fc_ns_retry = 0;
8353 	vport->num_disc_nodes = 0;
8354 
8355 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
8356 	if (ndlp && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
8357 		/* Good ndlp, issue CT Request to NameServer.  Need to
8358 		 * know how many gidfts were issued.  If none, then just
8359 		 * flush the RSCN.  Otherwise, the outstanding requests
8360 		 * need to complete.
8361 		 */
8362 		if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
8363 			if (lpfc_issue_gidft(vport) > 0)
8364 				return 1;
8365 		} else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
8366 			if (lpfc_issue_gidpt(vport) > 0)
8367 				return 1;
8368 		} else {
8369 			return 1;
8370 		}
8371 	} else {
8372 		/* Nameserver login in question.  Revalidate. */
8373 		if (ndlp) {
8374 			ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
8375 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8376 		} else {
8377 			ndlp = lpfc_nlp_init(vport, NameServer_DID);
8378 			if (!ndlp) {
8379 				lpfc_els_flush_rscn(vport);
8380 				return 0;
8381 			}
8382 			ndlp->nlp_prev_state = ndlp->nlp_state;
8383 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8384 		}
8385 		ndlp->nlp_type |= NLP_FABRIC;
8386 		lpfc_issue_els_plogi(vport, NameServer_DID, 0);
8387 		/* Wait for NameServer login cmpl before we can
8388 		 * continue
8389 		 */
8390 		return 1;
8391 	}
8392 
8393 	lpfc_els_flush_rscn(vport);
8394 	return 0;
8395 }
8396 
8397 /**
8398  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
8399  * @vport: pointer to a host virtual N_Port data structure.
8400  * @cmdiocb: pointer to lpfc command iocb data structure.
8401  * @ndlp: pointer to a node-list data structure.
8402  *
8403  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
8404  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
8405  * point topology. As an unsolicited FLOGI should not be received in a loop
8406  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
8407  * lpfc_check_sparm() routine is invoked to check the parameters in the
8408  * unsolicited FLOGI. If parameters validation failed, the routine
8409  * lpfc_els_rsp_reject() shall be called with reject reason code set to
8410  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
8411  * FLOGI shall be compared with the Port WWN of the @vport to determine who
8412  * will initiate PLOGI. The higher lexicographical value party shall has
8413  * higher priority (as the winning port) and will initiate PLOGI and
8414  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
8415  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
8416  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
8417  *
8418  * Return code
8419  *   0 - Successfully processed the unsolicited flogi
8420  *   1 - Failed to process the unsolicited flogi
8421  **/
8422 static int
8423 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8424 		   struct lpfc_nodelist *ndlp)
8425 {
8426 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8427 	struct lpfc_hba  *phba = vport->phba;
8428 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
8429 	uint32_t *lp = (uint32_t *) pcmd->virt;
8430 	union lpfc_wqe128 *wqe = &cmdiocb->wqe;
8431 	struct serv_parm *sp;
8432 	LPFC_MBOXQ_t *mbox;
8433 	uint32_t cmd, did;
8434 	int rc;
8435 	unsigned long fc_flag = 0;
8436 	uint32_t port_state = 0;
8437 
8438 	/* Clear external loopback plug detected flag */
8439 	phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
8440 
8441 	cmd = *lp++;
8442 	sp = (struct serv_parm *) lp;
8443 
8444 	/* FLOGI received */
8445 
8446 	lpfc_set_disctmo(vport);
8447 
8448 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8449 		/* We should never receive a FLOGI in loop mode, ignore it */
8450 		did =  bf_get(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest);
8451 
8452 		/* An FLOGI ELS command <elsCmd> was received from DID <did> in
8453 		   Loop Mode */
8454 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8455 				 "0113 An FLOGI ELS command x%x was "
8456 				 "received from DID x%x in Loop Mode\n",
8457 				 cmd, did);
8458 		return 1;
8459 	}
8460 
8461 	(void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
8462 
8463 	/*
8464 	 * If our portname is greater than the remote portname,
8465 	 * then we initiate Nport login.
8466 	 */
8467 
8468 	rc = memcmp(&vport->fc_portname, &sp->portName,
8469 		    sizeof(struct lpfc_name));
8470 
8471 	if (!rc) {
8472 		if (phba->sli_rev < LPFC_SLI_REV4) {
8473 			mbox = mempool_alloc(phba->mbox_mem_pool,
8474 					     GFP_KERNEL);
8475 			if (!mbox)
8476 				return 1;
8477 			lpfc_linkdown(phba);
8478 			lpfc_init_link(phba, mbox,
8479 				       phba->cfg_topology,
8480 				       phba->cfg_link_speed);
8481 			mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
8482 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8483 			mbox->vport = vport;
8484 			rc = lpfc_sli_issue_mbox(phba, mbox,
8485 						 MBX_NOWAIT);
8486 			lpfc_set_loopback_flag(phba);
8487 			if (rc == MBX_NOT_FINISHED)
8488 				mempool_free(mbox, phba->mbox_mem_pool);
8489 			return 1;
8490 		}
8491 
8492 		/* External loopback plug insertion detected */
8493 		phba->link_flag |= LS_EXTERNAL_LOOPBACK;
8494 
8495 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_LIBDFC,
8496 				 "1119 External Loopback plug detected\n");
8497 
8498 		/* abort the flogi coming back to ourselves
8499 		 * due to external loopback on the port.
8500 		 */
8501 		lpfc_els_abort_flogi(phba);
8502 		return 0;
8503 
8504 	} else if (rc > 0) {	/* greater than */
8505 		set_bit(FC_PT2PT_PLOGI, &vport->fc_flag);
8506 
8507 		/* If we have the high WWPN we can assign our own
8508 		 * myDID; otherwise, we have to WAIT for a PLOGI
8509 		 * from the remote NPort to find out what it
8510 		 * will be.
8511 		 */
8512 		vport->fc_myDID = PT2PT_LocalID;
8513 	} else {
8514 		vport->fc_myDID = PT2PT_RemoteID;
8515 	}
8516 
8517 	/*
8518 	 * The vport state should go to LPFC_FLOGI only
8519 	 * AFTER we issue a FLOGI, not receive one.
8520 	 */
8521 	spin_lock_irq(shost->host_lock);
8522 	fc_flag = vport->fc_flag;
8523 	port_state = vport->port_state;
8524 	/* Acking an unsol FLOGI.  Count 1 for link bounce
8525 	 * work-around.
8526 	 */
8527 	vport->rcv_flogi_cnt++;
8528 	spin_unlock_irq(shost->host_lock);
8529 	set_bit(FC_PT2PT, &vport->fc_flag);
8530 	clear_bit(FC_FABRIC, &vport->fc_flag);
8531 	clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
8532 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8533 			 "3311 Rcv Flogi PS x%x new PS x%x "
8534 			 "fc_flag x%lx new fc_flag x%lx, hba_flag x%lx\n",
8535 			 port_state, vport->port_state,
8536 			 fc_flag, vport->fc_flag, phba->hba_flag);
8537 
8538 	/*
8539 	 * We temporarily set fc_myDID to make it look like we are
8540 	 * a Fabric. This is done just so we end up with the right
8541 	 * did / sid on the FLOGI ACC rsp.
8542 	 */
8543 	did = vport->fc_myDID;
8544 	vport->fc_myDID = Fabric_DID;
8545 
8546 	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
8547 
8548 	/* Defer ACC response until AFTER we issue a FLOGI */
8549 	if (!test_bit(HBA_FLOGI_ISSUED, &phba->hba_flag)) {
8550 		phba->defer_flogi_acc.rx_id = bf_get(wqe_ctxt_tag,
8551 						     &wqe->xmit_els_rsp.wqe_com);
8552 		phba->defer_flogi_acc.ox_id = bf_get(wqe_rcvoxid,
8553 						     &wqe->xmit_els_rsp.wqe_com);
8554 
8555 		vport->fc_myDID = did;
8556 		phba->defer_flogi_acc.flag = true;
8557 
8558 		/* This nlp_get is paired with nlp_puts that reset the
8559 		 * defer_flogi_acc.flag back to false.  We need to retain
8560 		 * a kref on the ndlp until the deferred FLOGI ACC is
8561 		 * processed or cancelled.
8562 		 */
8563 		phba->defer_flogi_acc.ndlp = lpfc_nlp_get(ndlp);
8564 
8565 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8566 				 "3344 Deferring FLOGI ACC: rx_id: x%x,"
8567 				 " ox_id: x%x, ndlp x%px, hba_flag x%lx\n",
8568 				 phba->defer_flogi_acc.rx_id,
8569 				 phba->defer_flogi_acc.ox_id,
8570 				 phba->defer_flogi_acc.ndlp,
8571 				 phba->hba_flag);
8572 		return 0;
8573 	}
8574 
8575 	/* Send back ACC */
8576 	lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
8577 
8578 	/* Now lets put fc_myDID back to what its supposed to be */
8579 	vport->fc_myDID = did;
8580 
8581 	return 0;
8582 }
8583 
8584 /**
8585  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
8586  * @vport: pointer to a host virtual N_Port data structure.
8587  * @cmdiocb: pointer to lpfc command iocb data structure.
8588  * @ndlp: pointer to a node-list data structure.
8589  *
8590  * This routine processes Request Node Identification Data (RNID) IOCB
8591  * received as an ELS unsolicited event. Only when the RNID specified format
8592  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
8593  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
8594  * Accept (ACC) the RNID ELS command. All the other RNID formats are
8595  * rejected by invoking the lpfc_els_rsp_reject() routine.
8596  *
8597  * Return code
8598  *   0 - Successfully processed rnid iocb (currently always return 0)
8599  **/
8600 static int
8601 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8602 		  struct lpfc_nodelist *ndlp)
8603 {
8604 	struct lpfc_dmabuf *pcmd;
8605 	uint32_t *lp;
8606 	RNID *rn;
8607 	struct ls_rjt stat;
8608 
8609 	pcmd = cmdiocb->cmd_dmabuf;
8610 	lp = (uint32_t *) pcmd->virt;
8611 
8612 	lp++;
8613 	rn = (RNID *) lp;
8614 
8615 	/* RNID received */
8616 
8617 	switch (rn->Format) {
8618 	case 0:
8619 	case RNID_TOPOLOGY_DISC:
8620 		/* Send back ACC */
8621 		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
8622 		break;
8623 	default:
8624 		/* Reject this request because format not supported */
8625 		stat.un.b.lsRjtRsvd0 = 0;
8626 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8627 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8628 		stat.un.b.vendorUnique = 0;
8629 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
8630 			NULL);
8631 	}
8632 	return 0;
8633 }
8634 
8635 /**
8636  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
8637  * @vport: pointer to a host virtual N_Port data structure.
8638  * @cmdiocb: pointer to lpfc command iocb data structure.
8639  * @ndlp: pointer to a node-list data structure.
8640  *
8641  * Return code
8642  *   0 - Successfully processed echo iocb (currently always return 0)
8643  **/
8644 static int
8645 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8646 		  struct lpfc_nodelist *ndlp)
8647 {
8648 	uint8_t *pcmd;
8649 
8650 	pcmd = (uint8_t *)cmdiocb->cmd_dmabuf->virt;
8651 
8652 	/* skip over first word of echo command to find echo data */
8653 	pcmd += sizeof(uint32_t);
8654 
8655 	lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
8656 	return 0;
8657 }
8658 
8659 /**
8660  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
8661  * @vport: pointer to a host virtual N_Port data structure.
8662  * @cmdiocb: pointer to lpfc command iocb data structure.
8663  * @ndlp: pointer to a node-list data structure.
8664  *
8665  * This routine processes a Link Incident Report Registration(LIRR) IOCB
8666  * received as an ELS unsolicited event. Currently, this function just invokes
8667  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
8668  *
8669  * Return code
8670  *   0 - Successfully processed lirr iocb (currently always return 0)
8671  **/
8672 static int
8673 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8674 		  struct lpfc_nodelist *ndlp)
8675 {
8676 	struct ls_rjt stat;
8677 
8678 	/* For now, unconditionally reject this command */
8679 	stat.un.b.lsRjtRsvd0 = 0;
8680 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8681 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8682 	stat.un.b.vendorUnique = 0;
8683 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8684 	return 0;
8685 }
8686 
8687 /**
8688  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
8689  * @vport: pointer to a host virtual N_Port data structure.
8690  * @cmdiocb: pointer to lpfc command iocb data structure.
8691  * @ndlp: pointer to a node-list data structure.
8692  *
8693  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
8694  * received as an ELS unsolicited event. A request to RRQ shall only
8695  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
8696  * Nx_Port N_Port_ID of the target Exchange is the same as the
8697  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
8698  * not accepted, an LS_RJT with reason code "Unable to perform
8699  * command request" and reason code explanation "Invalid Originator
8700  * S_ID" shall be returned. For now, we just unconditionally accept
8701  * RRQ from the target.
8702  **/
8703 static void
8704 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8705 		 struct lpfc_nodelist *ndlp)
8706 {
8707 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
8708 	if (vport->phba->sli_rev == LPFC_SLI_REV4)
8709 		lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
8710 }
8711 
8712 /**
8713  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
8714  * @phba: pointer to lpfc hba data structure.
8715  * @pmb: pointer to the driver internal queue element for mailbox command.
8716  *
8717  * This routine is the completion callback function for the MBX_READ_LNK_STAT
8718  * mailbox command. This callback function is to actually send the Accept
8719  * (ACC) response to a Read Link Status (RLS) unsolicited IOCB event. It
8720  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
8721  * mailbox command, constructs the RLS response with the link statistics
8722  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
8723  * response to the RLS.
8724  *
8725  * Note that the ndlp reference count will be incremented by 1 for holding the
8726  * ndlp and the reference to ndlp will be stored into the ndlp field of
8727  * the IOCB for the completion callback function to the RLS Accept Response
8728  * ELS IOCB command.
8729  *
8730  **/
8731 static void
8732 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
8733 {
8734 	int rc = 0;
8735 	MAILBOX_t *mb;
8736 	IOCB_t *icmd;
8737 	union lpfc_wqe128 *wqe;
8738 	struct RLS_RSP *rls_rsp;
8739 	uint8_t *pcmd;
8740 	struct lpfc_iocbq *elsiocb;
8741 	struct lpfc_nodelist *ndlp;
8742 	uint16_t oxid;
8743 	uint16_t rxid;
8744 	uint32_t cmdsize;
8745 	u32 ulp_context;
8746 
8747 	mb = &pmb->u.mb;
8748 
8749 	ndlp = pmb->ctx_ndlp;
8750 	rxid = (uint16_t)(pmb->ctx_u.ox_rx_id & 0xffff);
8751 	oxid = (uint16_t)((pmb->ctx_u.ox_rx_id >> 16) & 0xffff);
8752 	memset(&pmb->ctx_u, 0, sizeof(pmb->ctx_u));
8753 	pmb->ctx_ndlp = NULL;
8754 
8755 	if (mb->mbxStatus) {
8756 		mempool_free(pmb, phba->mbox_mem_pool);
8757 		return;
8758 	}
8759 
8760 	cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
8761 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8762 				     lpfc_max_els_tries, ndlp,
8763 				     ndlp->nlp_DID, ELS_CMD_ACC);
8764 
8765 	/* Decrement the ndlp reference count from previous mbox command */
8766 	lpfc_nlp_put(ndlp);
8767 
8768 	if (!elsiocb) {
8769 		mempool_free(pmb, phba->mbox_mem_pool);
8770 		return;
8771 	}
8772 
8773 	ulp_context = get_job_ulpcontext(phba, elsiocb);
8774 	if (phba->sli_rev == LPFC_SLI_REV4) {
8775 		wqe = &elsiocb->wqe;
8776 		/* Xri / rx_id */
8777 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, rxid);
8778 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com, oxid);
8779 	} else {
8780 		icmd = &elsiocb->iocb;
8781 		icmd->ulpContext = rxid;
8782 		icmd->unsli3.rcvsli3.ox_id = oxid;
8783 	}
8784 
8785 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
8786 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8787 	pcmd += sizeof(uint32_t); /* Skip past command */
8788 	rls_rsp = (struct RLS_RSP *)pcmd;
8789 
8790 	rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
8791 	rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
8792 	rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
8793 	rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
8794 	rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
8795 	rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
8796 	mempool_free(pmb, phba->mbox_mem_pool);
8797 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
8798 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8799 			 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
8800 			 "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x\n",
8801 			 elsiocb->iotag, ulp_context,
8802 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8803 			 ndlp->nlp_rpi);
8804 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
8805 	phba->fc_stat.elsXmitACC++;
8806 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
8807 	if (!elsiocb->ndlp) {
8808 		lpfc_els_free_iocb(phba, elsiocb);
8809 		return;
8810 	}
8811 
8812 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8813 	if (rc == IOCB_ERROR) {
8814 		lpfc_els_free_iocb(phba, elsiocb);
8815 		lpfc_nlp_put(ndlp);
8816 	}
8817 	return;
8818 }
8819 
8820 /**
8821  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
8822  * @vport: pointer to a host virtual N_Port data structure.
8823  * @cmdiocb: pointer to lpfc command iocb data structure.
8824  * @ndlp: pointer to a node-list data structure.
8825  *
8826  * This routine processes Read Link Status (RLS) IOCB received as an
8827  * ELS unsolicited event. It first checks the remote port state. If the
8828  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8829  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8830  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
8831  * for reading the HBA link statistics. It is for the callback function,
8832  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
8833  * to actually sending out RPL Accept (ACC) response.
8834  *
8835  * Return codes
8836  *   0 - Successfully processed rls iocb (currently always return 0)
8837  **/
8838 static int
8839 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8840 		 struct lpfc_nodelist *ndlp)
8841 {
8842 	struct lpfc_hba *phba = vport->phba;
8843 	LPFC_MBOXQ_t *mbox;
8844 	struct ls_rjt stat;
8845 	u32 ctx = get_job_ulpcontext(phba, cmdiocb);
8846 	u32 ox_id = get_job_rcvoxid(phba, cmdiocb);
8847 
8848 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8849 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8850 		/* reject the unsolicited RLS request and done with it */
8851 		goto reject_out;
8852 
8853 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
8854 	if (mbox) {
8855 		lpfc_read_lnk_stat(phba, mbox);
8856 		mbox->ctx_u.ox_rx_id = ox_id << 16 | ctx;
8857 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
8858 		if (!mbox->ctx_ndlp)
8859 			goto node_err;
8860 		mbox->vport = vport;
8861 		mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
8862 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
8863 			!= MBX_NOT_FINISHED)
8864 			/* Mbox completion will send ELS Response */
8865 			return 0;
8866 		/* Decrement reference count used for the failed mbox
8867 		 * command.
8868 		 */
8869 		lpfc_nlp_put(ndlp);
8870 node_err:
8871 		mempool_free(mbox, phba->mbox_mem_pool);
8872 	}
8873 reject_out:
8874 	/* issue rejection response */
8875 	stat.un.b.lsRjtRsvd0 = 0;
8876 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8877 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8878 	stat.un.b.vendorUnique = 0;
8879 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8880 	return 0;
8881 }
8882 
8883 /**
8884  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
8885  * @vport: pointer to a host virtual N_Port data structure.
8886  * @cmdiocb: pointer to lpfc command iocb data structure.
8887  * @ndlp: pointer to a node-list data structure.
8888  *
8889  * This routine processes Read Timeout Value (RTV) IOCB received as an
8890  * ELS unsolicited event. It first checks the remote port state. If the
8891  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
8892  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
8893  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
8894  * Value (RTV) unsolicited IOCB event.
8895  *
8896  * Note that the ndlp reference count will be incremented by 1 for holding the
8897  * ndlp and the reference to ndlp will be stored into the ndlp field of
8898  * the IOCB for the completion callback function to the RTV Accept Response
8899  * ELS IOCB command.
8900  *
8901  * Return codes
8902  *   0 - Successfully processed rtv iocb (currently always return 0)
8903  **/
8904 static int
8905 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
8906 		 struct lpfc_nodelist *ndlp)
8907 {
8908 	int rc = 0;
8909 	IOCB_t *icmd;
8910 	union lpfc_wqe128 *wqe;
8911 	struct lpfc_hba *phba = vport->phba;
8912 	struct ls_rjt stat;
8913 	struct RTV_RSP *rtv_rsp;
8914 	uint8_t *pcmd;
8915 	struct lpfc_iocbq *elsiocb;
8916 	uint32_t cmdsize;
8917 	u32 ulp_context;
8918 
8919 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
8920 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
8921 		/* reject the unsolicited RTV request and done with it */
8922 		goto reject_out;
8923 
8924 	cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
8925 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
8926 				     lpfc_max_els_tries, ndlp,
8927 				     ndlp->nlp_DID, ELS_CMD_ACC);
8928 
8929 	if (!elsiocb)
8930 		return 1;
8931 
8932 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
8933 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
8934 	pcmd += sizeof(uint32_t); /* Skip past command */
8935 
8936 	ulp_context = get_job_ulpcontext(phba, elsiocb);
8937 	/* use the command's xri in the response */
8938 	if (phba->sli_rev == LPFC_SLI_REV4) {
8939 		wqe = &elsiocb->wqe;
8940 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
8941 		       get_job_ulpcontext(phba, cmdiocb));
8942 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8943 		       get_job_rcvoxid(phba, cmdiocb));
8944 	} else {
8945 		icmd = &elsiocb->iocb;
8946 		icmd->ulpContext = get_job_ulpcontext(phba, cmdiocb);
8947 		icmd->unsli3.rcvsli3.ox_id = get_job_rcvoxid(phba, cmdiocb);
8948 	}
8949 
8950 	rtv_rsp = (struct RTV_RSP *)pcmd;
8951 
8952 	/* populate RTV payload */
8953 	rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
8954 	rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
8955 	bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
8956 	bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
8957 	rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
8958 
8959 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
8960 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
8961 			 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
8962 			 "did x%x, nlp_flag x%lx, nlp_state x%x, rpi x%x, "
8963 			 "Data: x%x x%x x%x\n",
8964 			 elsiocb->iotag, ulp_context,
8965 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
8966 			 ndlp->nlp_rpi,
8967 			rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
8968 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
8969 	phba->fc_stat.elsXmitACC++;
8970 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
8971 	if (!elsiocb->ndlp) {
8972 		lpfc_els_free_iocb(phba, elsiocb);
8973 		return 0;
8974 	}
8975 
8976 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
8977 	if (rc == IOCB_ERROR) {
8978 		lpfc_els_free_iocb(phba, elsiocb);
8979 		lpfc_nlp_put(ndlp);
8980 	}
8981 	return 0;
8982 
8983 reject_out:
8984 	/* issue rejection response */
8985 	stat.un.b.lsRjtRsvd0 = 0;
8986 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
8987 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
8988 	stat.un.b.vendorUnique = 0;
8989 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
8990 	return 0;
8991 }
8992 
8993 /* lpfc_issue_els_rrq - Process an unsolicited rrq iocb
8994  * @vport: pointer to a host virtual N_Port data structure.
8995  * @ndlp: pointer to a node-list data structure.
8996  * @did: DID of the target.
8997  * @rrq: Pointer to the rrq struct.
8998  *
8999  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
9000  * successful, the completion handler will clear the RRQ.
9001  *
9002  * Return codes
9003  *   0 - Successfully sent rrq els iocb.
9004  *   1 - Failed to send rrq els iocb.
9005  **/
9006 static int
9007 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
9008 			uint32_t did, struct lpfc_node_rrq *rrq)
9009 {
9010 	struct lpfc_hba  *phba = vport->phba;
9011 	struct RRQ *els_rrq;
9012 	struct lpfc_iocbq *elsiocb;
9013 	uint8_t *pcmd;
9014 	uint16_t cmdsize;
9015 	int ret;
9016 
9017 	if (!ndlp)
9018 		return 1;
9019 
9020 	/* If ndlp is not NULL, we will bump the reference count on it */
9021 	cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
9022 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
9023 				     ELS_CMD_RRQ);
9024 	if (!elsiocb)
9025 		return 1;
9026 
9027 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
9028 
9029 	/* For RRQ request, remainder of payload is Exchange IDs */
9030 	*((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
9031 	pcmd += sizeof(uint32_t);
9032 	els_rrq = (struct RRQ *) pcmd;
9033 
9034 	bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
9035 	bf_set(rrq_rxid, els_rrq, rrq->rxid);
9036 	bf_set(rrq_did, els_rrq, vport->fc_myDID);
9037 	els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
9038 	els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
9039 
9040 
9041 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9042 		"Issue RRQ:     did:x%x",
9043 		did, rrq->xritag, rrq->rxid);
9044 	elsiocb->context_un.rrq = rrq;
9045 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rrq;
9046 
9047 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
9048 	if (!elsiocb->ndlp)
9049 		goto io_err;
9050 
9051 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
9052 	if (ret == IOCB_ERROR) {
9053 		lpfc_nlp_put(ndlp);
9054 		goto io_err;
9055 	}
9056 	return 0;
9057 
9058  io_err:
9059 	lpfc_els_free_iocb(phba, elsiocb);
9060 	return 1;
9061 }
9062 
9063 /**
9064  * lpfc_send_rrq - Sends ELS RRQ if needed.
9065  * @phba: pointer to lpfc hba data structure.
9066  * @rrq: pointer to the active rrq.
9067  *
9068  * This routine will call the lpfc_issue_els_rrq if the rrq is
9069  * still active for the xri. If this function returns a failure then
9070  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
9071  *
9072  * Returns 0 Success.
9073  *         1 Failure.
9074  **/
9075 int
9076 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
9077 {
9078 	struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
9079 						       rrq->nlp_DID);
9080 	if (!ndlp)
9081 		return 1;
9082 
9083 	if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
9084 		return lpfc_issue_els_rrq(rrq->vport, ndlp,
9085 					 rrq->nlp_DID, rrq);
9086 	else
9087 		return 1;
9088 }
9089 
9090 /**
9091  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
9092  * @vport: pointer to a host virtual N_Port data structure.
9093  * @cmdsize: size of the ELS command.
9094  * @oldiocb: pointer to the original lpfc command iocb data structure.
9095  * @ndlp: pointer to a node-list data structure.
9096  *
9097  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
9098  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
9099  *
9100  * Note that the ndlp reference count will be incremented by 1 for holding the
9101  * ndlp and the reference to ndlp will be stored into the ndlp field of
9102  * the IOCB for the completion callback function to the RPL Accept Response
9103  * ELS command.
9104  *
9105  * Return code
9106  *   0 - Successfully issued ACC RPL ELS command
9107  *   1 - Failed to issue ACC RPL ELS command
9108  **/
9109 static int
9110 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
9111 		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
9112 {
9113 	int rc = 0;
9114 	struct lpfc_hba *phba = vport->phba;
9115 	IOCB_t *icmd;
9116 	union lpfc_wqe128 *wqe;
9117 	RPL_RSP rpl_rsp;
9118 	struct lpfc_iocbq *elsiocb;
9119 	uint8_t *pcmd;
9120 	u32 ulp_context;
9121 
9122 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
9123 				     ndlp->nlp_DID, ELS_CMD_ACC);
9124 
9125 	if (!elsiocb)
9126 		return 1;
9127 
9128 	ulp_context = get_job_ulpcontext(phba, elsiocb);
9129 	if (phba->sli_rev == LPFC_SLI_REV4) {
9130 		wqe = &elsiocb->wqe;
9131 		/* Xri / rx_id */
9132 		bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
9133 		       get_job_ulpcontext(phba, oldiocb));
9134 		bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
9135 		       get_job_rcvoxid(phba, oldiocb));
9136 	} else {
9137 		icmd = &elsiocb->iocb;
9138 		icmd->ulpContext = get_job_ulpcontext(phba, oldiocb);
9139 		icmd->unsli3.rcvsli3.ox_id = get_job_rcvoxid(phba, oldiocb);
9140 	}
9141 
9142 	pcmd = elsiocb->cmd_dmabuf->virt;
9143 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
9144 	pcmd += sizeof(uint16_t);
9145 	*((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
9146 	pcmd += sizeof(uint16_t);
9147 
9148 	/* Setup the RPL ACC payload */
9149 	rpl_rsp.listLen = be32_to_cpu(1);
9150 	rpl_rsp.index = 0;
9151 	rpl_rsp.port_num_blk.portNum = 0;
9152 	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
9153 	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
9154 	    sizeof(struct lpfc_name));
9155 	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
9156 	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
9157 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9158 			 "0120 Xmit ELS RPL ACC response tag x%x "
9159 			 "xri x%x, did x%x, nlp_flag x%lx, nlp_state x%x, "
9160 			 "rpi x%x\n",
9161 			 elsiocb->iotag, ulp_context,
9162 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
9163 			 ndlp->nlp_rpi);
9164 	elsiocb->cmd_cmpl = lpfc_cmpl_els_rsp;
9165 	phba->fc_stat.elsXmitACC++;
9166 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
9167 	if (!elsiocb->ndlp) {
9168 		lpfc_els_free_iocb(phba, elsiocb);
9169 		return 1;
9170 	}
9171 
9172 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
9173 	if (rc == IOCB_ERROR) {
9174 		lpfc_els_free_iocb(phba, elsiocb);
9175 		lpfc_nlp_put(ndlp);
9176 		return 1;
9177 	}
9178 
9179 	return 0;
9180 }
9181 
9182 /**
9183  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
9184  * @vport: pointer to a host virtual N_Port data structure.
9185  * @cmdiocb: pointer to lpfc command iocb data structure.
9186  * @ndlp: pointer to a node-list data structure.
9187  *
9188  * This routine processes Read Port List (RPL) IOCB received as an ELS
9189  * unsolicited event. It first checks the remote port state. If the remote
9190  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
9191  * invokes the lpfc_els_rsp_reject() routine to send reject response.
9192  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
9193  * to accept the RPL.
9194  *
9195  * Return code
9196  *   0 - Successfully processed rpl iocb (currently always return 0)
9197  **/
9198 static int
9199 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9200 		 struct lpfc_nodelist *ndlp)
9201 {
9202 	struct lpfc_dmabuf *pcmd;
9203 	uint32_t *lp;
9204 	uint32_t maxsize;
9205 	uint16_t cmdsize;
9206 	RPL *rpl;
9207 	struct ls_rjt stat;
9208 
9209 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
9210 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
9211 		/* issue rejection response */
9212 		stat.un.b.lsRjtRsvd0 = 0;
9213 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
9214 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
9215 		stat.un.b.vendorUnique = 0;
9216 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
9217 			NULL);
9218 		/* rejected the unsolicited RPL request and done with it */
9219 		return 0;
9220 	}
9221 
9222 	pcmd = cmdiocb->cmd_dmabuf;
9223 	lp = (uint32_t *) pcmd->virt;
9224 	rpl = (RPL *) (lp + 1);
9225 	maxsize = be32_to_cpu(rpl->maxsize);
9226 
9227 	/* We support only one port */
9228 	if ((rpl->index == 0) &&
9229 	    ((maxsize == 0) ||
9230 	     ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
9231 		cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
9232 	} else {
9233 		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
9234 	}
9235 	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
9236 
9237 	return 0;
9238 }
9239 
9240 /**
9241  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
9242  * @vport: pointer to a virtual N_Port data structure.
9243  * @cmdiocb: pointer to lpfc command iocb data structure.
9244  * @ndlp: pointer to a node-list data structure.
9245  *
9246  * This routine processes Fibre Channel Address Resolution Protocol
9247  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
9248  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
9249  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
9250  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
9251  * remote PortName is compared against the FC PortName stored in the @vport
9252  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
9253  * compared against the FC NodeName stored in the @vport data structure.
9254  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
9255  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
9256  * invoked to send out FARP Response to the remote node. Before sending the
9257  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
9258  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
9259  * routine is invoked to log into the remote port first.
9260  *
9261  * Return code
9262  *   0 - Either the FARP Match Mode not supported or successfully processed
9263  **/
9264 static int
9265 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9266 		  struct lpfc_nodelist *ndlp)
9267 {
9268 	struct lpfc_dmabuf *pcmd;
9269 	uint32_t *lp;
9270 	FARP *fp;
9271 	uint32_t cnt, did;
9272 
9273 	did = get_job_els_rsp64_did(vport->phba, cmdiocb);
9274 	pcmd = cmdiocb->cmd_dmabuf;
9275 	lp = (uint32_t *) pcmd->virt;
9276 
9277 	lp++;
9278 	fp = (FARP *) lp;
9279 	/* FARP-REQ received from DID <did> */
9280 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9281 			 "0601 FARP-REQ received from DID x%x\n", did);
9282 	/* We will only support match on WWPN or WWNN */
9283 	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
9284 		return 0;
9285 	}
9286 
9287 	cnt = 0;
9288 	/* If this FARP command is searching for my portname */
9289 	if (fp->Mflags & FARP_MATCH_PORT) {
9290 		if (memcmp(&fp->RportName, &vport->fc_portname,
9291 			   sizeof(struct lpfc_name)) == 0)
9292 			cnt = 1;
9293 	}
9294 
9295 	/* If this FARP command is searching for my nodename */
9296 	if (fp->Mflags & FARP_MATCH_NODE) {
9297 		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
9298 			   sizeof(struct lpfc_name)) == 0)
9299 			cnt = 1;
9300 	}
9301 
9302 	if (cnt) {
9303 		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
9304 		   (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
9305 			/* Log back into the node before sending the FARP. */
9306 			if (fp->Rflags & FARP_REQUEST_PLOGI) {
9307 				ndlp->nlp_prev_state = ndlp->nlp_state;
9308 				lpfc_nlp_set_state(vport, ndlp,
9309 						   NLP_STE_PLOGI_ISSUE);
9310 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
9311 			}
9312 
9313 			/* Send a FARP response to that node */
9314 			if (fp->Rflags & FARP_REQUEST_FARPR)
9315 				lpfc_issue_els_farpr(vport, did, 0);
9316 		}
9317 	}
9318 	return 0;
9319 }
9320 
9321 /**
9322  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
9323  * @vport: pointer to a host virtual N_Port data structure.
9324  * @cmdiocb: pointer to lpfc command iocb data structure.
9325  * @ndlp: pointer to a node-list data structure.
9326  *
9327  * This routine processes Fibre Channel Address Resolution Protocol
9328  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
9329  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
9330  * the FARP response request.
9331  *
9332  * Return code
9333  *   0 - Successfully processed FARPR IOCB (currently always return 0)
9334  **/
9335 static int
9336 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9337 		   struct lpfc_nodelist  *ndlp)
9338 {
9339 	uint32_t did;
9340 
9341 	did = get_job_els_rsp64_did(vport->phba, cmdiocb);
9342 
9343 	/* FARP-RSP received from DID <did> */
9344 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9345 			 "0600 FARP-RSP received from DID x%x\n", did);
9346 	/* ACCEPT the Farp resp request */
9347 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
9348 
9349 	return 0;
9350 }
9351 
9352 /**
9353  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
9354  * @vport: pointer to a host virtual N_Port data structure.
9355  * @cmdiocb: pointer to lpfc command iocb data structure.
9356  * @fan_ndlp: pointer to a node-list data structure.
9357  *
9358  * This routine processes a Fabric Address Notification (FAN) IOCB
9359  * command received as an ELS unsolicited event. The FAN ELS command will
9360  * only be processed on a physical port (i.e., the @vport represents the
9361  * physical port). The fabric NodeName and PortName from the FAN IOCB are
9362  * compared against those in the phba data structure. If any of those is
9363  * different, the lpfc_initial_flogi() routine is invoked to initialize
9364  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
9365  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
9366  * is invoked to register login to the fabric.
9367  *
9368  * Return code
9369  *   0 - Successfully processed fan iocb (currently always return 0).
9370  **/
9371 static int
9372 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9373 		 struct lpfc_nodelist *fan_ndlp)
9374 {
9375 	struct lpfc_hba *phba = vport->phba;
9376 	uint32_t *lp;
9377 	FAN *fp;
9378 
9379 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
9380 	lp = (uint32_t *)cmdiocb->cmd_dmabuf->virt;
9381 	fp = (FAN *) ++lp;
9382 	/* FAN received; Fan does not have a reply sequence */
9383 	if ((vport == phba->pport) &&
9384 	    (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
9385 		if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
9386 			    sizeof(struct lpfc_name))) ||
9387 		    (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
9388 			    sizeof(struct lpfc_name)))) {
9389 			/* This port has switched fabrics. FLOGI is required */
9390 			lpfc_issue_init_vfi(vport);
9391 		} else {
9392 			/* FAN verified - skip FLOGI */
9393 			vport->fc_myDID = vport->fc_prevDID;
9394 			if (phba->sli_rev < LPFC_SLI_REV4)
9395 				lpfc_issue_fabric_reglogin(vport);
9396 			else {
9397 				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9398 					"3138 Need register VFI: (x%x/%x)\n",
9399 					vport->fc_prevDID, vport->fc_myDID);
9400 				lpfc_issue_reg_vfi(vport);
9401 			}
9402 		}
9403 	}
9404 	return 0;
9405 }
9406 
9407 /**
9408  * lpfc_els_rcv_edc - Process an unsolicited EDC iocb
9409  * @vport: pointer to a host virtual N_Port data structure.
9410  * @cmdiocb: pointer to lpfc command iocb data structure.
9411  * @ndlp: pointer to a node-list data structure.
9412  *
9413  * Return code
9414  *   0 - Successfully processed echo iocb (currently always return 0)
9415  **/
9416 static int
9417 lpfc_els_rcv_edc(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
9418 		 struct lpfc_nodelist *ndlp)
9419 {
9420 	struct lpfc_hba  *phba = vport->phba;
9421 	struct fc_els_edc *edc_req;
9422 	struct fc_tlv_desc *tlv;
9423 	uint8_t *payload;
9424 	uint32_t *ptr, dtag;
9425 	const char *dtag_nm;
9426 	int desc_cnt = 0, bytes_remain;
9427 	struct fc_diag_lnkflt_desc *plnkflt;
9428 
9429 	payload = cmdiocb->cmd_dmabuf->virt;
9430 
9431 	edc_req = (struct fc_els_edc *)payload;
9432 	bytes_remain = be32_to_cpu(edc_req->desc_len);
9433 
9434 	ptr = (uint32_t *)payload;
9435 	lpfc_printf_vlog(vport, KERN_INFO,
9436 			 LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
9437 			 "3319 Rcv EDC payload len %d: x%x x%x x%x\n",
9438 			 bytes_remain, be32_to_cpu(*ptr),
9439 			 be32_to_cpu(*(ptr + 1)), be32_to_cpu(*(ptr + 2)));
9440 
9441 	/* No signal support unless there is a congestion descriptor */
9442 	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
9443 	phba->cgn_sig_freq = 0;
9444 	phba->cgn_reg_fpin = LPFC_CGN_FPIN_ALARM | LPFC_CGN_FPIN_WARN;
9445 
9446 	if (bytes_remain <= 0)
9447 		goto out;
9448 
9449 	tlv = edc_req->desc;
9450 
9451 	/*
9452 	 * cycle through EDC diagnostic descriptors to find the
9453 	 * congestion signaling capability descriptor
9454 	 */
9455 	while (bytes_remain) {
9456 		if (bytes_remain < FC_TLV_DESC_HDR_SZ) {
9457 			lpfc_printf_log(phba, KERN_WARNING,
9458 					LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
9459 					"6464 Truncated TLV hdr on "
9460 					"Diagnostic descriptor[%d]\n",
9461 					desc_cnt);
9462 			goto out;
9463 		}
9464 
9465 		dtag = be32_to_cpu(tlv->desc_tag);
9466 		switch (dtag) {
9467 		case ELS_DTAG_LNK_FAULT_CAP:
9468 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
9469 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
9470 				sizeof(struct fc_diag_lnkflt_desc)) {
9471 				lpfc_printf_log(phba, KERN_WARNING,
9472 					LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
9473 					"6465 Truncated Link Fault Diagnostic "
9474 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
9475 					desc_cnt, bytes_remain,
9476 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
9477 					sizeof(struct fc_diag_lnkflt_desc));
9478 				goto out;
9479 			}
9480 			plnkflt = (struct fc_diag_lnkflt_desc *)tlv;
9481 			lpfc_printf_log(phba, KERN_INFO,
9482 				LOG_ELS | LOG_LDS_EVENT,
9483 				"4626 Link Fault Desc Data: x%08x len x%x "
9484 				"da x%x dd x%x interval x%x\n",
9485 				be32_to_cpu(plnkflt->desc_tag),
9486 				be32_to_cpu(plnkflt->desc_len),
9487 				be32_to_cpu(
9488 					plnkflt->degrade_activate_threshold),
9489 				be32_to_cpu(
9490 					plnkflt->degrade_deactivate_threshold),
9491 				be32_to_cpu(plnkflt->fec_degrade_interval));
9492 			break;
9493 		case ELS_DTAG_CG_SIGNAL_CAP:
9494 			if (bytes_remain < FC_TLV_DESC_SZ_FROM_LENGTH(tlv) ||
9495 			    FC_TLV_DESC_SZ_FROM_LENGTH(tlv) !=
9496 				sizeof(struct fc_diag_cg_sig_desc)) {
9497 				lpfc_printf_log(
9498 					phba, KERN_WARNING, LOG_CGN_MGMT,
9499 					"6466 Truncated cgn signal Diagnostic "
9500 					"descriptor[%d]: %d vs 0x%zx 0x%zx\n",
9501 					desc_cnt, bytes_remain,
9502 					FC_TLV_DESC_SZ_FROM_LENGTH(tlv),
9503 					sizeof(struct fc_diag_cg_sig_desc));
9504 				goto out;
9505 			}
9506 
9507 			phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
9508 			phba->cgn_reg_signal = phba->cgn_init_reg_signal;
9509 
9510 			/* We start negotiation with lpfc_fabric_cgn_frequency.
9511 			 * When we process the EDC, we will settle on the
9512 			 * higher frequency.
9513 			 */
9514 			phba->cgn_sig_freq = lpfc_fabric_cgn_frequency;
9515 
9516 			lpfc_least_capable_settings(
9517 				phba, (struct fc_diag_cg_sig_desc *)tlv);
9518 			break;
9519 		default:
9520 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
9521 			lpfc_printf_log(phba, KERN_WARNING,
9522 					LOG_ELS | LOG_CGN_MGMT | LOG_LDS_EVENT,
9523 					"6467 unknown Diagnostic "
9524 					"Descriptor[%d]: tag x%x (%s)\n",
9525 					desc_cnt, dtag, dtag_nm);
9526 		}
9527 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
9528 		tlv = fc_tlv_next_desc(tlv);
9529 		desc_cnt++;
9530 	}
9531 out:
9532 	/* Need to send back an ACC */
9533 	lpfc_issue_els_edc_rsp(vport, cmdiocb, ndlp);
9534 
9535 	lpfc_config_cgn_signal(phba);
9536 	return 0;
9537 }
9538 
9539 /**
9540  * lpfc_els_timeout - Handler funciton to the els timer
9541  * @t: timer context used to obtain the vport.
9542  *
9543  * This routine is invoked by the ELS timer after timeout. It posts the ELS
9544  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
9545  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
9546  * up the worker thread. It is for the worker thread to invoke the routine
9547  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
9548  **/
9549 void
9550 lpfc_els_timeout(struct timer_list *t)
9551 {
9552 	struct lpfc_vport *vport = timer_container_of(vport, t, els_tmofunc);
9553 	struct lpfc_hba   *phba = vport->phba;
9554 	uint32_t tmo_posted;
9555 	unsigned long iflag;
9556 
9557 	spin_lock_irqsave(&vport->work_port_lock, iflag);
9558 	tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
9559 	if (!tmo_posted && !test_bit(FC_UNLOADING, &vport->load_flag))
9560 		vport->work_port_events |= WORKER_ELS_TMO;
9561 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
9562 
9563 	if (!tmo_posted && !test_bit(FC_UNLOADING, &vport->load_flag))
9564 		lpfc_worker_wake_up(phba);
9565 	return;
9566 }
9567 
9568 
9569 /**
9570  * lpfc_els_timeout_handler - Process an els timeout event
9571  * @vport: pointer to a virtual N_Port data structure.
9572  *
9573  * This routine is the actual handler function that processes an ELS timeout
9574  * event. It walks the ELS ring to get and abort all the IOCBs (except the
9575  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
9576  * invoking the lpfc_sli_issue_abort_iotag() routine.
9577  **/
9578 void
9579 lpfc_els_timeout_handler(struct lpfc_vport *vport)
9580 {
9581 	struct lpfc_hba  *phba = vport->phba;
9582 	struct lpfc_sli_ring *pring;
9583 	struct lpfc_iocbq *tmp_iocb, *piocb;
9584 	IOCB_t *cmd = NULL;
9585 	struct lpfc_dmabuf *pcmd;
9586 	uint32_t els_command = 0;
9587 	uint32_t timeout;
9588 	uint32_t remote_ID = 0xffffffff;
9589 	LIST_HEAD(abort_list);
9590 	u32 ulp_command = 0, ulp_context = 0, did = 0, iotag = 0;
9591 
9592 
9593 	timeout = (uint32_t)(phba->fc_ratov << 1);
9594 
9595 	pring = lpfc_phba_elsring(phba);
9596 	if (unlikely(!pring))
9597 		return;
9598 
9599 	if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
9600 		return;
9601 
9602 	spin_lock_irq(&phba->hbalock);
9603 	if (phba->sli_rev == LPFC_SLI_REV4)
9604 		spin_lock(&pring->ring_lock);
9605 
9606 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
9607 		ulp_command = get_job_cmnd(phba, piocb);
9608 		ulp_context = get_job_ulpcontext(phba, piocb);
9609 		did = get_job_els_rsp64_did(phba, piocb);
9610 
9611 		if (phba->sli_rev == LPFC_SLI_REV4) {
9612 			iotag = get_wqe_reqtag(piocb);
9613 		} else {
9614 			cmd = &piocb->iocb;
9615 			iotag = cmd->ulpIoTag;
9616 		}
9617 
9618 		if ((piocb->cmd_flag & LPFC_IO_LIBDFC) != 0 ||
9619 		    ulp_command == CMD_ABORT_XRI_CX ||
9620 		    ulp_command == CMD_ABORT_XRI_CN ||
9621 		    ulp_command == CMD_CLOSE_XRI_CN)
9622 			continue;
9623 
9624 		if (piocb->vport != vport)
9625 			continue;
9626 
9627 		pcmd = piocb->cmd_dmabuf;
9628 		if (pcmd)
9629 			els_command = *(uint32_t *) (pcmd->virt);
9630 
9631 		if (els_command == ELS_CMD_FARP ||
9632 		    els_command == ELS_CMD_FARPR ||
9633 		    els_command == ELS_CMD_FDISC)
9634 			continue;
9635 
9636 		if (piocb->drvrTimeout > 0) {
9637 			if (piocb->drvrTimeout >= timeout)
9638 				piocb->drvrTimeout -= timeout;
9639 			else
9640 				piocb->drvrTimeout = 0;
9641 			continue;
9642 		}
9643 
9644 		remote_ID = 0xffffffff;
9645 		if (ulp_command != CMD_GEN_REQUEST64_CR) {
9646 			remote_ID = did;
9647 		} else {
9648 			struct lpfc_nodelist *ndlp;
9649 			ndlp = __lpfc_findnode_rpi(vport, ulp_context);
9650 			if (ndlp)
9651 				remote_ID = ndlp->nlp_DID;
9652 		}
9653 		list_add_tail(&piocb->dlist, &abort_list);
9654 	}
9655 	if (phba->sli_rev == LPFC_SLI_REV4)
9656 		spin_unlock(&pring->ring_lock);
9657 	spin_unlock_irq(&phba->hbalock);
9658 
9659 	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9660 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9661 			 "0127 ELS timeout Data: x%x x%x x%x "
9662 			 "x%x\n", els_command,
9663 			 remote_ID, ulp_command, iotag);
9664 
9665 		spin_lock_irq(&phba->hbalock);
9666 		list_del_init(&piocb->dlist);
9667 		lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9668 		spin_unlock_irq(&phba->hbalock);
9669 	}
9670 
9671 	/* Make sure HBA is alive */
9672 	lpfc_issue_hb_tmo(phba);
9673 
9674 	if (!list_empty(&pring->txcmplq))
9675 		if (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
9676 			mod_timer(&vport->els_tmofunc,
9677 				  jiffies + secs_to_jiffies(timeout));
9678 }
9679 
9680 /**
9681  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
9682  * @vport: pointer to a host virtual N_Port data structure.
9683  *
9684  * This routine is used to clean up all the outstanding ELS commands on a
9685  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
9686  * routine. After that, it walks the ELS transmit queue to remove all the
9687  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
9688  * the IOCBs with a non-NULL completion callback function, the callback
9689  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9690  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
9691  * callback function, the IOCB will simply be released. Finally, it walks
9692  * the ELS transmit completion queue to issue an abort IOCB to any transmit
9693  * completion queue IOCB that is associated with the @vport and is not
9694  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
9695  * part of the discovery state machine) out to HBA by invoking the
9696  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
9697  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
9698  * the IOCBs are aborted when this function returns.
9699  **/
9700 void
9701 lpfc_els_flush_cmd(struct lpfc_vport *vport)
9702 {
9703 	LIST_HEAD(abort_list);
9704 	LIST_HEAD(cancel_list);
9705 	struct lpfc_hba  *phba = vport->phba;
9706 	struct lpfc_sli_ring *pring;
9707 	struct lpfc_iocbq *tmp_iocb, *piocb;
9708 	u32 ulp_command;
9709 	unsigned long iflags = 0;
9710 	bool mbx_tmo_err;
9711 
9712 	lpfc_fabric_abort_vport(vport);
9713 
9714 	/*
9715 	 * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
9716 	 * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
9717 	 * ultimately grabs the ring_lock, the driver must splice the list into
9718 	 * a working list and release the locks before calling the abort.
9719 	 */
9720 	spin_lock_irqsave(&phba->hbalock, iflags);
9721 	pring = lpfc_phba_elsring(phba);
9722 
9723 	/* Bail out if we've no ELS wq, like in PCI error recovery case. */
9724 	if (unlikely(!pring)) {
9725 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9726 		return;
9727 	}
9728 
9729 	if (phba->sli_rev == LPFC_SLI_REV4)
9730 		spin_lock(&pring->ring_lock);
9731 
9732 	mbx_tmo_err = test_bit(MBX_TMO_ERR, &phba->bit_flags);
9733 	/* First we need to issue aborts to outstanding cmds on txcmpl */
9734 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
9735 		if (piocb->vport != vport)
9736 			continue;
9737 
9738 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9739 				 "2243 iotag = 0x%x cmd_flag = 0x%x "
9740 				 "ulp_command = 0x%x sli_flag = 0x%x\n",
9741 				 piocb->iotag, piocb->cmd_flag,
9742 				 get_job_cmnd(phba, piocb),
9743 				 phba->sli.sli_flag);
9744 
9745 		if ((phba->sli.sli_flag & LPFC_SLI_ACTIVE) && !mbx_tmo_err) {
9746 			if (piocb->cmd_flag & LPFC_IO_LIBDFC)
9747 				continue;
9748 			if (piocb->cmd_flag & LPFC_DRIVER_ABORTED)
9749 				continue;
9750 		}
9751 
9752 		/* On the ELS ring we can have ELS_REQUESTs, ELS_RSPs,
9753 		 * or GEN_REQUESTs waiting for a CQE response.
9754 		 */
9755 		ulp_command = get_job_cmnd(phba, piocb);
9756 		if (ulp_command == CMD_ELS_REQUEST64_WQE ||
9757 		    ulp_command == CMD_XMIT_ELS_RSP64_WQE) {
9758 			list_add_tail(&piocb->dlist, &abort_list);
9759 
9760 			/* If the link is down when flushing ELS commands
9761 			 * the firmware will not complete them till after
9762 			 * the link comes back up. This may confuse
9763 			 * discovery for the new link up, so we need to
9764 			 * change the compl routine to just clean up the iocb
9765 			 * and avoid any retry logic.
9766 			 */
9767 			if (phba->link_state == LPFC_LINK_DOWN)
9768 				piocb->cmd_cmpl = lpfc_cmpl_els_link_down;
9769 		} else if (ulp_command == CMD_GEN_REQUEST64_CR ||
9770 			   mbx_tmo_err)
9771 			list_add_tail(&piocb->dlist, &abort_list);
9772 	}
9773 
9774 	if (phba->sli_rev == LPFC_SLI_REV4)
9775 		spin_unlock(&pring->ring_lock);
9776 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9777 
9778 	/* Abort each txcmpl iocb on aborted list and remove the dlist links. */
9779 	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
9780 		spin_lock_irqsave(&phba->hbalock, iflags);
9781 		list_del_init(&piocb->dlist);
9782 		if (mbx_tmo_err || !(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
9783 			list_move_tail(&piocb->list, &cancel_list);
9784 		else
9785 			lpfc_sli_issue_abort_iotag(phba, pring, piocb, NULL);
9786 
9787 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9788 	}
9789 	if (!list_empty(&cancel_list))
9790 		lpfc_sli_cancel_iocbs(phba, &cancel_list, IOSTAT_LOCAL_REJECT,
9791 				      IOERR_SLI_ABORTED);
9792 	else
9793 		/* Make sure HBA is alive */
9794 		lpfc_issue_hb_tmo(phba);
9795 
9796 	if (!list_empty(&abort_list))
9797 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9798 				 "3387 abort list for txq not empty\n");
9799 	INIT_LIST_HEAD(&abort_list);
9800 
9801 	spin_lock_irqsave(&phba->hbalock, iflags);
9802 	if (phba->sli_rev == LPFC_SLI_REV4)
9803 		spin_lock(&pring->ring_lock);
9804 
9805 	/* No need to abort the txq list,
9806 	 * just queue them up for lpfc_sli_cancel_iocbs
9807 	 */
9808 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
9809 		ulp_command = get_job_cmnd(phba, piocb);
9810 
9811 		if (piocb->cmd_flag & LPFC_IO_LIBDFC)
9812 			continue;
9813 
9814 		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
9815 		if (ulp_command == CMD_QUE_RING_BUF_CN ||
9816 		    ulp_command == CMD_QUE_RING_BUF64_CN ||
9817 		    ulp_command == CMD_CLOSE_XRI_CN ||
9818 		    ulp_command == CMD_ABORT_XRI_CN ||
9819 		    ulp_command == CMD_ABORT_XRI_CX)
9820 			continue;
9821 
9822 		if (piocb->vport != vport)
9823 			continue;
9824 
9825 		list_del_init(&piocb->list);
9826 		list_add_tail(&piocb->list, &abort_list);
9827 	}
9828 
9829 	/* The same holds true for any FLOGI/FDISC on the fabric_iocb_list */
9830 	if (vport == phba->pport) {
9831 		list_for_each_entry_safe(piocb, tmp_iocb,
9832 					 &phba->fabric_iocb_list, list) {
9833 			list_del_init(&piocb->list);
9834 			list_add_tail(&piocb->list, &abort_list);
9835 		}
9836 	}
9837 
9838 	if (phba->sli_rev == LPFC_SLI_REV4)
9839 		spin_unlock(&pring->ring_lock);
9840 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9841 
9842 	/* Cancel all the IOCBs from the completions list */
9843 	lpfc_sli_cancel_iocbs(phba, &abort_list,
9844 			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
9845 
9846 	return;
9847 }
9848 
9849 /**
9850  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
9851  * @phba: pointer to lpfc hba data structure.
9852  *
9853  * This routine is used to clean up all the outstanding ELS commands on a
9854  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
9855  * routine. After that, it walks the ELS transmit queue to remove all the
9856  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
9857  * the IOCBs with the completion callback function associated, the callback
9858  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
9859  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
9860  * callback function associated, the IOCB will simply be released. Finally,
9861  * it walks the ELS transmit completion queue to issue an abort IOCB to any
9862  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
9863  * management plane IOCBs that are not part of the discovery state machine)
9864  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
9865  **/
9866 void
9867 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
9868 {
9869 	struct lpfc_vport *vport;
9870 
9871 	spin_lock_irq(&phba->port_list_lock);
9872 	list_for_each_entry(vport, &phba->port_list, listentry)
9873 		lpfc_els_flush_cmd(vport);
9874 	spin_unlock_irq(&phba->port_list_lock);
9875 
9876 	return;
9877 }
9878 
9879 /**
9880  * lpfc_send_els_failure_event - Posts an ELS command failure event
9881  * @phba: Pointer to hba context object.
9882  * @cmdiocbp: Pointer to command iocb which reported error.
9883  * @rspiocbp: Pointer to response iocb which reported error.
9884  *
9885  * This function sends an event when there is an ELS command
9886  * failure.
9887  **/
9888 void
9889 lpfc_send_els_failure_event(struct lpfc_hba *phba,
9890 			struct lpfc_iocbq *cmdiocbp,
9891 			struct lpfc_iocbq *rspiocbp)
9892 {
9893 	struct lpfc_vport *vport = cmdiocbp->vport;
9894 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9895 	struct lpfc_lsrjt_event lsrjt_event;
9896 	struct lpfc_fabric_event_header fabric_event;
9897 	struct ls_rjt stat;
9898 	struct lpfc_nodelist *ndlp;
9899 	uint32_t *pcmd;
9900 	u32 ulp_status, ulp_word4;
9901 
9902 	ndlp = cmdiocbp->ndlp;
9903 	if (!ndlp)
9904 		return;
9905 
9906 	ulp_status = get_job_ulpstatus(phba, rspiocbp);
9907 	ulp_word4 = get_job_word4(phba, rspiocbp);
9908 
9909 	if (ulp_status == IOSTAT_LS_RJT) {
9910 		lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
9911 		lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
9912 		memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
9913 			sizeof(struct lpfc_name));
9914 		memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
9915 			sizeof(struct lpfc_name));
9916 		pcmd = (uint32_t *)cmdiocbp->cmd_dmabuf->virt;
9917 		lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
9918 		stat.un.ls_rjt_error_be = cpu_to_be32(ulp_word4);
9919 		lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
9920 		lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
9921 		fc_host_post_vendor_event(shost,
9922 			fc_get_event_number(),
9923 			sizeof(lsrjt_event),
9924 			(char *)&lsrjt_event,
9925 			LPFC_NL_VENDOR_ID);
9926 		return;
9927 	}
9928 	if (ulp_status == IOSTAT_NPORT_BSY ||
9929 	    ulp_status == IOSTAT_FABRIC_BSY) {
9930 		fabric_event.event_type = FC_REG_FABRIC_EVENT;
9931 		if (ulp_status == IOSTAT_NPORT_BSY)
9932 			fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
9933 		else
9934 			fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
9935 		memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
9936 			sizeof(struct lpfc_name));
9937 		memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
9938 			sizeof(struct lpfc_name));
9939 		fc_host_post_vendor_event(shost,
9940 			fc_get_event_number(),
9941 			sizeof(fabric_event),
9942 			(char *)&fabric_event,
9943 			LPFC_NL_VENDOR_ID);
9944 		return;
9945 	}
9946 
9947 }
9948 
9949 /**
9950  * lpfc_send_els_event - Posts unsolicited els event
9951  * @vport: Pointer to vport object.
9952  * @ndlp: Pointer FC node object.
9953  * @payload: ELS command code type.
9954  *
9955  * This function posts an event when there is an incoming
9956  * unsolicited ELS command.
9957  **/
9958 static void
9959 lpfc_send_els_event(struct lpfc_vport *vport,
9960 		    struct lpfc_nodelist *ndlp,
9961 		    uint32_t *payload)
9962 {
9963 	struct lpfc_els_event_header *els_data = NULL;
9964 	struct lpfc_logo_event *logo_data = NULL;
9965 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9966 
9967 	if (*payload == ELS_CMD_LOGO) {
9968 		logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
9969 		if (!logo_data) {
9970 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9971 				"0148 Failed to allocate memory "
9972 				"for LOGO event\n");
9973 			return;
9974 		}
9975 		els_data = &logo_data->header;
9976 	} else {
9977 		els_data = kmalloc(sizeof(struct lpfc_els_event_header),
9978 			GFP_KERNEL);
9979 		if (!els_data) {
9980 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9981 				"0149 Failed to allocate memory "
9982 				"for ELS event\n");
9983 			return;
9984 		}
9985 	}
9986 	els_data->event_type = FC_REG_ELS_EVENT;
9987 	switch (*payload) {
9988 	case ELS_CMD_PLOGI:
9989 		els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
9990 		break;
9991 	case ELS_CMD_PRLO:
9992 		els_data->subcategory = LPFC_EVENT_PRLO_RCV;
9993 		break;
9994 	case ELS_CMD_ADISC:
9995 		els_data->subcategory = LPFC_EVENT_ADISC_RCV;
9996 		break;
9997 	case ELS_CMD_LOGO:
9998 		els_data->subcategory = LPFC_EVENT_LOGO_RCV;
9999 		/* Copy the WWPN in the LOGO payload */
10000 		memcpy(logo_data->logo_wwpn, &payload[2],
10001 			sizeof(struct lpfc_name));
10002 		break;
10003 	default:
10004 		kfree(els_data);
10005 		return;
10006 	}
10007 	memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
10008 	memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
10009 	if (*payload == ELS_CMD_LOGO) {
10010 		fc_host_post_vendor_event(shost,
10011 			fc_get_event_number(),
10012 			sizeof(struct lpfc_logo_event),
10013 			(char *)logo_data,
10014 			LPFC_NL_VENDOR_ID);
10015 		kfree(logo_data);
10016 	} else {
10017 		fc_host_post_vendor_event(shost,
10018 			fc_get_event_number(),
10019 			sizeof(struct lpfc_els_event_header),
10020 			(char *)els_data,
10021 			LPFC_NL_VENDOR_ID);
10022 		kfree(els_data);
10023 	}
10024 
10025 	return;
10026 }
10027 
10028 
10029 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_li_event_nm, fc_fpin_li_event_types,
10030 			FC_FPIN_LI_EVT_TYPES_INIT);
10031 
10032 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_deli_event_nm, fc_fpin_deli_event_types,
10033 			FC_FPIN_DELI_EVT_TYPES_INIT);
10034 
10035 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_event_nm, fc_fpin_congn_event_types,
10036 			FC_FPIN_CONGN_EVT_TYPES_INIT);
10037 
10038 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_congn_severity_nm,
10039 			fc_fpin_congn_severity_types,
10040 			FC_FPIN_CONGN_SEVERITY_INIT);
10041 
10042 
10043 /**
10044  * lpfc_display_fpin_wwpn - Display WWPNs accessible by the attached port
10045  * @phba: Pointer to phba object.
10046  * @wwnlist: Pointer to list of WWPNs in FPIN payload
10047  * @cnt: count of WWPNs in FPIN payload
10048  *
10049  * This routine is called by LI and PC descriptors.
10050  * Limit the number of WWPNs displayed to 6 log messages, 6 per log message
10051  */
10052 static void
10053 lpfc_display_fpin_wwpn(struct lpfc_hba *phba, __be64 *wwnlist, u32 cnt)
10054 {
10055 	char buf[LPFC_FPIN_WWPN_LINE_SZ];
10056 	__be64 wwn;
10057 	u64 wwpn;
10058 	int i, len;
10059 	int line = 0;
10060 	int wcnt = 0;
10061 	bool endit = false;
10062 
10063 	len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ, "Accessible WWPNs:");
10064 	for (i = 0; i < cnt; i++) {
10065 		/* Are we on the last WWPN */
10066 		if (i == (cnt - 1))
10067 			endit = true;
10068 
10069 		/* Extract the next WWPN from the payload */
10070 		wwn = *wwnlist++;
10071 		wwpn = be64_to_cpu(wwn);
10072 		len += scnprintf(buf + len, LPFC_FPIN_WWPN_LINE_SZ - len,
10073 				 " %016llx", wwpn);
10074 
10075 		/* Log a message if we are on the last WWPN
10076 		 * or if we hit the max allowed per message.
10077 		 */
10078 		wcnt++;
10079 		if (wcnt == LPFC_FPIN_WWPN_LINE_CNT || endit) {
10080 			buf[len] = 0;
10081 			lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10082 					"4686 %s\n", buf);
10083 
10084 			/* Check if we reached the last WWPN */
10085 			if (endit)
10086 				return;
10087 
10088 			/* Limit the number of log message displayed per FPIN */
10089 			line++;
10090 			if (line == LPFC_FPIN_WWPN_NUM_LINE) {
10091 				lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10092 						"4687 %d WWPNs Truncated\n",
10093 						cnt - i - 1);
10094 				return;
10095 			}
10096 
10097 			/* Start over with next log message */
10098 			wcnt = 0;
10099 			len = scnprintf(buf, LPFC_FPIN_WWPN_LINE_SZ,
10100 					"Additional WWPNs:");
10101 		}
10102 	}
10103 }
10104 
10105 /**
10106  * lpfc_els_rcv_fpin_li - Process an FPIN Link Integrity Event.
10107  * @phba: Pointer to phba object.
10108  * @tlv:  Pointer to the Link Integrity Notification Descriptor.
10109  *
10110  * This function processes a Link Integrity FPIN event by logging a message.
10111  **/
10112 static void
10113 lpfc_els_rcv_fpin_li(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10114 {
10115 	struct fc_fn_li_desc *li = (struct fc_fn_li_desc *)tlv;
10116 	const char *li_evt_str;
10117 	u32 li_evt, cnt;
10118 
10119 	li_evt = be16_to_cpu(li->event_type);
10120 	li_evt_str = lpfc_get_fpin_li_event_nm(li_evt);
10121 	cnt = be32_to_cpu(li->pname_count);
10122 
10123 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10124 			"4680 FPIN Link Integrity %s (x%x) "
10125 			"Detecting PN x%016llx Attached PN x%016llx "
10126 			"Duration %d mSecs Count %d Port Cnt %d\n",
10127 			li_evt_str, li_evt,
10128 			be64_to_cpu(li->detecting_wwpn),
10129 			be64_to_cpu(li->attached_wwpn),
10130 			be32_to_cpu(li->event_threshold),
10131 			be32_to_cpu(li->event_count), cnt);
10132 
10133 	lpfc_display_fpin_wwpn(phba, (__be64 *)&li->pname_list, cnt);
10134 }
10135 
10136 /**
10137  * lpfc_els_rcv_fpin_del - Process an FPIN Delivery Event.
10138  * @phba: Pointer to hba object.
10139  * @tlv:  Pointer to the Delivery Notification Descriptor TLV
10140  *
10141  * This function processes a Delivery FPIN event by logging a message.
10142  **/
10143 static void
10144 lpfc_els_rcv_fpin_del(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10145 {
10146 	struct fc_fn_deli_desc *del = (struct fc_fn_deli_desc *)tlv;
10147 	const char *del_rsn_str;
10148 	u32 del_rsn;
10149 	__be32 *frame;
10150 
10151 	del_rsn = be16_to_cpu(del->deli_reason_code);
10152 	del_rsn_str = lpfc_get_fpin_deli_event_nm(del_rsn);
10153 
10154 	/* Skip over desc_tag/desc_len header to payload */
10155 	frame = (__be32 *)(del + 1);
10156 
10157 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10158 			"4681 FPIN Delivery %s (x%x) "
10159 			"Detecting PN x%016llx Attached PN x%016llx "
10160 			"DiscHdr0  x%08x "
10161 			"DiscHdr1 x%08x DiscHdr2 x%08x DiscHdr3 x%08x "
10162 			"DiscHdr4 x%08x DiscHdr5 x%08x\n",
10163 			del_rsn_str, del_rsn,
10164 			be64_to_cpu(del->detecting_wwpn),
10165 			be64_to_cpu(del->attached_wwpn),
10166 			be32_to_cpu(frame[0]),
10167 			be32_to_cpu(frame[1]),
10168 			be32_to_cpu(frame[2]),
10169 			be32_to_cpu(frame[3]),
10170 			be32_to_cpu(frame[4]),
10171 			be32_to_cpu(frame[5]));
10172 }
10173 
10174 /**
10175  * lpfc_els_rcv_fpin_peer_cgn - Process a FPIN Peer Congestion Event.
10176  * @phba: Pointer to hba object.
10177  * @tlv:  Pointer to the Peer Congestion Notification Descriptor TLV
10178  *
10179  * This function processes a Peer Congestion FPIN event by logging a message.
10180  **/
10181 static void
10182 lpfc_els_rcv_fpin_peer_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10183 {
10184 	struct fc_fn_peer_congn_desc *pc = (struct fc_fn_peer_congn_desc *)tlv;
10185 	const char *pc_evt_str;
10186 	u32 pc_evt, cnt;
10187 
10188 	pc_evt = be16_to_cpu(pc->event_type);
10189 	pc_evt_str = lpfc_get_fpin_congn_event_nm(pc_evt);
10190 	cnt = be32_to_cpu(pc->pname_count);
10191 
10192 	/* Capture FPIN frequency */
10193 	phba->cgn_fpin_frequency = be32_to_cpu(pc->event_period);
10194 
10195 	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT | LOG_ELS,
10196 			"4684 FPIN Peer Congestion %s (x%x) "
10197 			"Duration %d mSecs "
10198 			"Detecting PN x%016llx Attached PN x%016llx "
10199 			"Impacted Port Cnt %d\n",
10200 			pc_evt_str, pc_evt,
10201 			be32_to_cpu(pc->event_period),
10202 			be64_to_cpu(pc->detecting_wwpn),
10203 			be64_to_cpu(pc->attached_wwpn),
10204 			cnt);
10205 
10206 	lpfc_display_fpin_wwpn(phba, (__be64 *)&pc->pname_list, cnt);
10207 }
10208 
10209 /**
10210  * lpfc_els_rcv_fpin_cgn - Process an FPIN Congestion notification
10211  * @phba: Pointer to hba object.
10212  * @tlv:  Pointer to the Congestion Notification Descriptor TLV
10213  *
10214  * This function processes an FPIN Congestion Notifiction.  The notification
10215  * could be an Alarm or Warning.  This routine feeds that data into driver's
10216  * running congestion algorithm. It also processes the FPIN by
10217  * logging a message. It returns 1 to indicate deliver this message
10218  * to the upper layer or 0 to indicate don't deliver it.
10219  **/
10220 static int
10221 lpfc_els_rcv_fpin_cgn(struct lpfc_hba *phba, struct fc_tlv_desc *tlv)
10222 {
10223 	struct lpfc_cgn_info *cp;
10224 	struct fc_fn_congn_desc *cgn = (struct fc_fn_congn_desc *)tlv;
10225 	const char *cgn_evt_str;
10226 	u32 cgn_evt;
10227 	const char *cgn_sev_str;
10228 	u32 cgn_sev;
10229 	uint16_t value;
10230 	u32 crc;
10231 	bool nm_log = false;
10232 	int rc = 1;
10233 
10234 	cgn_evt = be16_to_cpu(cgn->event_type);
10235 	cgn_evt_str = lpfc_get_fpin_congn_event_nm(cgn_evt);
10236 	cgn_sev = cgn->severity;
10237 	cgn_sev_str = lpfc_get_fpin_congn_severity_nm(cgn_sev);
10238 
10239 	/* The driver only takes action on a Credit Stall or Oversubscription
10240 	 * event type to engage the IO algorithm.  The driver prints an
10241 	 * unmaskable message only for Lost Credit and Credit Stall.
10242 	 * TODO: Still need to have definition of host action on clear,
10243 	 *       lost credit and device specific event types.
10244 	 */
10245 	switch (cgn_evt) {
10246 	case FPIN_CONGN_LOST_CREDIT:
10247 		nm_log = true;
10248 		break;
10249 	case FPIN_CONGN_CREDIT_STALL:
10250 		nm_log = true;
10251 		fallthrough;
10252 	case FPIN_CONGN_OVERSUBSCRIPTION:
10253 		if (cgn_evt == FPIN_CONGN_OVERSUBSCRIPTION)
10254 			nm_log = false;
10255 		switch (cgn_sev) {
10256 		case FPIN_CONGN_SEVERITY_ERROR:
10257 			/* Take action here for an Alarm event */
10258 			if (phba->cmf_active_mode != LPFC_CFG_OFF) {
10259 				if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_ALARM) {
10260 					/* Track of alarm cnt for SYNC_WQE */
10261 					atomic_inc(&phba->cgn_sync_alarm_cnt);
10262 				}
10263 				/* Track alarm cnt for cgn_info regardless
10264 				 * of whether CMF is configured for Signals
10265 				 * or FPINs.
10266 				 */
10267 				atomic_inc(&phba->cgn_fabric_alarm_cnt);
10268 				goto cleanup;
10269 			}
10270 			break;
10271 		case FPIN_CONGN_SEVERITY_WARNING:
10272 			/* Take action here for a Warning event */
10273 			if (phba->cmf_active_mode != LPFC_CFG_OFF) {
10274 				if (phba->cgn_reg_fpin & LPFC_CGN_FPIN_WARN) {
10275 					/* Track of warning cnt for SYNC_WQE */
10276 					atomic_inc(&phba->cgn_sync_warn_cnt);
10277 				}
10278 				/* Track warning cnt and freq for cgn_info
10279 				 * regardless of whether CMF is configured for
10280 				 * Signals or FPINs.
10281 				 */
10282 				atomic_inc(&phba->cgn_fabric_warn_cnt);
10283 cleanup:
10284 				/* Save frequency in ms */
10285 				phba->cgn_fpin_frequency =
10286 					be32_to_cpu(cgn->event_period);
10287 				value = phba->cgn_fpin_frequency;
10288 				if (phba->cgn_i) {
10289 					cp = (struct lpfc_cgn_info *)
10290 						phba->cgn_i->virt;
10291 					cp->cgn_alarm_freq =
10292 						cpu_to_le16(value);
10293 					cp->cgn_warn_freq =
10294 						cpu_to_le16(value);
10295 					crc = lpfc_cgn_calc_crc32
10296 						(cp,
10297 						LPFC_CGN_INFO_SZ,
10298 						LPFC_CGN_CRC32_SEED);
10299 					cp->cgn_info_crc = cpu_to_le32(crc);
10300 				}
10301 
10302 				/* Don't deliver to upper layer since
10303 				 * driver took action on this tlv.
10304 				 */
10305 				rc = 0;
10306 			}
10307 			break;
10308 		}
10309 		break;
10310 	}
10311 
10312 	/* Change the log level to unmaskable for the following event types. */
10313 	lpfc_printf_log(phba, (nm_log ? KERN_WARNING : KERN_INFO),
10314 			LOG_CGN_MGMT | LOG_ELS,
10315 			"4683 FPIN CONGESTION %s type %s (x%x) Event "
10316 			"Duration %d mSecs\n",
10317 			cgn_sev_str, cgn_evt_str, cgn_evt,
10318 			be32_to_cpu(cgn->event_period));
10319 	return rc;
10320 }
10321 
10322 void
10323 lpfc_els_rcv_fpin(struct lpfc_vport *vport, void *p, u32 fpin_length)
10324 {
10325 	struct lpfc_hba *phba = vport->phba;
10326 	struct fc_els_fpin *fpin = (struct fc_els_fpin *)p;
10327 	struct fc_tlv_desc *tlv, *first_tlv, *current_tlv;
10328 	const char *dtag_nm;
10329 	int desc_cnt = 0, bytes_remain, cnt;
10330 	u32 dtag, deliver = 0;
10331 	int len;
10332 
10333 	/* FPINs handled only if we are in the right discovery state */
10334 	if (vport->port_state < LPFC_DISC_AUTH)
10335 		return;
10336 
10337 	/* make sure there is the full fpin header */
10338 	if (fpin_length < sizeof(struct fc_els_fpin))
10339 		return;
10340 
10341 	/* Sanity check descriptor length. The desc_len value does not
10342 	 * include space for the ELS command and the desc_len fields.
10343 	 */
10344 	len = be32_to_cpu(fpin->desc_len);
10345 	if (fpin_length < len + sizeof(struct fc_els_fpin)) {
10346 		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10347 				"4671 Bad ELS FPIN length %d: %d\n",
10348 				len, fpin_length);
10349 		return;
10350 	}
10351 
10352 	tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
10353 	first_tlv = tlv;
10354 	bytes_remain = fpin_length - offsetof(struct fc_els_fpin, fpin_desc);
10355 	bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
10356 
10357 	/* process each descriptor separately */
10358 	while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
10359 	       bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
10360 		dtag = be32_to_cpu(tlv->desc_tag);
10361 		switch (dtag) {
10362 		case ELS_DTAG_LNK_INTEGRITY:
10363 			lpfc_els_rcv_fpin_li(phba, tlv);
10364 			deliver = 1;
10365 			break;
10366 		case ELS_DTAG_DELIVERY:
10367 			lpfc_els_rcv_fpin_del(phba, tlv);
10368 			deliver = 1;
10369 			break;
10370 		case ELS_DTAG_PEER_CONGEST:
10371 			lpfc_els_rcv_fpin_peer_cgn(phba, tlv);
10372 			deliver = 1;
10373 			break;
10374 		case ELS_DTAG_CONGESTION:
10375 			deliver = lpfc_els_rcv_fpin_cgn(phba, tlv);
10376 			break;
10377 		default:
10378 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
10379 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10380 					"4678 unknown FPIN descriptor[%d]: "
10381 					"tag x%x (%s)\n",
10382 					desc_cnt, dtag, dtag_nm);
10383 
10384 			/* If descriptor is bad, drop the rest of the data */
10385 			return;
10386 		}
10387 		lpfc_cgn_update_stat(phba, dtag);
10388 		cnt = be32_to_cpu(tlv->desc_len);
10389 
10390 		/* Sanity check descriptor length. The desc_len value does not
10391 		 * include space for the desc_tag and the desc_len fields.
10392 		 */
10393 		len -= (cnt + sizeof(struct fc_tlv_desc));
10394 		if (len < 0) {
10395 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
10396 			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
10397 					"4672 Bad FPIN descriptor TLV length "
10398 					"%d: %d %d %s\n",
10399 					cnt, len, fpin_length, dtag_nm);
10400 			return;
10401 		}
10402 
10403 		current_tlv = tlv;
10404 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
10405 		tlv = fc_tlv_next_desc(tlv);
10406 
10407 		/* Format payload such that the FPIN delivered to the
10408 		 * upper layer is a single descriptor FPIN.
10409 		 */
10410 		if (desc_cnt)
10411 			memcpy(first_tlv, current_tlv,
10412 			       (cnt + sizeof(struct fc_els_fpin)));
10413 
10414 		/* Adjust the length so that it only reflects a
10415 		 * single descriptor FPIN.
10416 		 */
10417 		fpin_length = cnt + sizeof(struct fc_els_fpin);
10418 		fpin->desc_len = cpu_to_be32(fpin_length);
10419 		fpin_length += sizeof(struct fc_els_fpin); /* the entire FPIN */
10420 
10421 		/* Send every descriptor individually to the upper layer */
10422 		if (deliver)
10423 			fc_host_fpin_rcv(lpfc_shost_from_vport(vport),
10424 					 fpin_length, (char *)fpin, 0);
10425 		desc_cnt++;
10426 	}
10427 }
10428 
10429 /**
10430  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
10431  * @phba: pointer to lpfc hba data structure.
10432  * @pring: pointer to a SLI ring.
10433  * @vport: pointer to a host virtual N_Port data structure.
10434  * @elsiocb: pointer to lpfc els command iocb data structure.
10435  *
10436  * This routine is used for processing the IOCB associated with a unsolicited
10437  * event. It first determines whether there is an existing ndlp that matches
10438  * the DID from the unsolicited IOCB. If not, it will create a new one with
10439  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
10440  * IOCB is then used to invoke the proper routine and to set up proper state
10441  * of the discovery state machine.
10442  **/
10443 static void
10444 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10445 		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
10446 {
10447 	struct lpfc_nodelist *ndlp;
10448 	struct ls_rjt stat;
10449 	u32 *payload, payload_len;
10450 	u32 cmd = 0, did = 0, newnode, status = 0;
10451 	uint8_t rjt_exp, rjt_err = 0, init_link = 0;
10452 	struct lpfc_wcqe_complete *wcqe_cmpl = NULL;
10453 	LPFC_MBOXQ_t *mbox;
10454 
10455 	if (!vport || !elsiocb->cmd_dmabuf)
10456 		goto dropit;
10457 
10458 	newnode = 0;
10459 	wcqe_cmpl = &elsiocb->wcqe_cmpl;
10460 	payload = elsiocb->cmd_dmabuf->virt;
10461 	if (phba->sli_rev == LPFC_SLI_REV4)
10462 		payload_len = wcqe_cmpl->total_data_placed;
10463 	else
10464 		payload_len = elsiocb->iocb.unsli3.rcvsli3.acc_len;
10465 	status = get_job_ulpstatus(phba, elsiocb);
10466 	cmd = *payload;
10467 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
10468 		lpfc_sli3_post_buffer(phba, pring, 1);
10469 
10470 	did = get_job_els_rsp64_did(phba, elsiocb);
10471 	if (status) {
10472 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10473 			"RCV Unsol ELS:  status:x%x/x%x did:x%x",
10474 			status, get_job_word4(phba, elsiocb), did);
10475 		goto dropit;
10476 	}
10477 
10478 	/* Check to see if link went down during discovery */
10479 	if (lpfc_els_chk_latt(vport))
10480 		goto dropit;
10481 
10482 	/* Ignore traffic received during vport shutdown. */
10483 	if (test_bit(FC_UNLOADING, &vport->load_flag))
10484 		goto dropit;
10485 
10486 	/* If NPort discovery is delayed drop incoming ELS */
10487 	if (test_bit(FC_DISC_DELAYED, &vport->fc_flag) &&
10488 	    cmd != ELS_CMD_PLOGI)
10489 		goto dropit;
10490 
10491 	ndlp = lpfc_findnode_did(vport, did);
10492 	if (!ndlp) {
10493 		/* Cannot find existing Fabric ndlp, so allocate a new one */
10494 		ndlp = lpfc_nlp_init(vport, did);
10495 		if (!ndlp)
10496 			goto dropit;
10497 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
10498 		newnode = 1;
10499 		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
10500 			ndlp->nlp_type |= NLP_FABRIC;
10501 	} else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
10502 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
10503 		newnode = 1;
10504 	}
10505 
10506 	phba->fc_stat.elsRcvFrame++;
10507 
10508 	/*
10509 	 * Do not process any unsolicited ELS commands
10510 	 * if the ndlp is in DEV_LOSS
10511 	 */
10512 	if (test_bit(NLP_IN_DEV_LOSS, &ndlp->nlp_flag))
10513 		goto dropit;
10514 
10515 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
10516 	if (!elsiocb->ndlp)
10517 		goto dropit;
10518 	elsiocb->vport = vport;
10519 
10520 	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
10521 		cmd &= ELS_CMD_MASK;
10522 	}
10523 	/* ELS command <elsCmd> received from NPORT <did> */
10524 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10525 			 "0112 ELS command x%x received from NPORT x%x "
10526 			 "refcnt %d Data: x%x x%lx x%x x%x\n",
10527 			 cmd, did, kref_read(&ndlp->kref), vport->port_state,
10528 			 vport->fc_flag, vport->fc_myDID, vport->fc_prevDID);
10529 
10530 	/* reject till our FLOGI completes or PLOGI assigned DID via PT2PT */
10531 	if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
10532 	    (cmd != ELS_CMD_FLOGI) &&
10533 	    !((cmd == ELS_CMD_PLOGI) && test_bit(FC_PT2PT, &vport->fc_flag))) {
10534 		rjt_err = LSRJT_LOGICAL_BSY;
10535 		rjt_exp = LSEXP_NOTHING_MORE;
10536 		goto lsrjt;
10537 	}
10538 
10539 	switch (cmd) {
10540 	case ELS_CMD_PLOGI:
10541 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10542 			"RCV PLOGI:       did:x%x/ste:x%x flg:x%lx",
10543 			did, vport->port_state, ndlp->nlp_flag);
10544 
10545 		phba->fc_stat.elsRcvPLOGI++;
10546 		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
10547 		if (phba->sli_rev == LPFC_SLI_REV4 &&
10548 		    test_bit(FC_PT2PT, &phba->pport->fc_flag)) {
10549 			vport->fc_prevDID = vport->fc_myDID;
10550 			/* Our DID needs to be updated before registering
10551 			 * the vfi. This is done in lpfc_rcv_plogi but
10552 			 * that is called after the reg_vfi.
10553 			 */
10554 			vport->fc_myDID =
10555 				bf_get(els_rsp64_sid,
10556 				       &elsiocb->wqe.xmit_els_rsp);
10557 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
10558 					 "3312 Remote port assigned DID x%x "
10559 					 "%x\n", vport->fc_myDID,
10560 					 vport->fc_prevDID);
10561 		}
10562 
10563 		lpfc_send_els_event(vport, ndlp, payload);
10564 
10565 		/* If Nport discovery is delayed, reject PLOGIs */
10566 		if (test_bit(FC_DISC_DELAYED, &vport->fc_flag)) {
10567 			rjt_err = LSRJT_UNABLE_TPC;
10568 			rjt_exp = LSEXP_NOTHING_MORE;
10569 			break;
10570 		}
10571 
10572 		if (vport->port_state < LPFC_DISC_AUTH) {
10573 			if (!test_bit(FC_PT2PT, &phba->pport->fc_flag) ||
10574 			    test_bit(FC_PT2PT_PLOGI, &phba->pport->fc_flag)) {
10575 				rjt_err = LSRJT_UNABLE_TPC;
10576 				rjt_exp = LSEXP_NOTHING_MORE;
10577 				break;
10578 			}
10579 		}
10580 
10581 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
10582 					NLP_EVT_RCV_PLOGI);
10583 
10584 		break;
10585 	case ELS_CMD_FLOGI:
10586 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10587 			"RCV FLOGI:       did:x%x/ste:x%x flg:x%lx",
10588 			did, vport->port_state, ndlp->nlp_flag);
10589 
10590 		phba->fc_stat.elsRcvFLOGI++;
10591 
10592 		/* If the driver believes fabric discovery is done and is ready,
10593 		 * bounce the link.  There is some descrepancy.
10594 		 */
10595 		if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
10596 		    test_bit(FC_PT2PT, &vport->fc_flag) &&
10597 		    vport->rcv_flogi_cnt >= 1) {
10598 			rjt_err = LSRJT_LOGICAL_BSY;
10599 			rjt_exp = LSEXP_NOTHING_MORE;
10600 			init_link++;
10601 			goto lsrjt;
10602 		}
10603 
10604 		lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
10605 		/* retain node if our response is deferred */
10606 		if (phba->defer_flogi_acc.flag)
10607 			break;
10608 		if (newnode)
10609 			lpfc_disc_state_machine(vport, ndlp, NULL,
10610 					NLP_EVT_DEVICE_RM);
10611 		break;
10612 	case ELS_CMD_LOGO:
10613 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10614 			"RCV LOGO:        did:x%x/ste:x%x flg:x%lx",
10615 			did, vport->port_state, ndlp->nlp_flag);
10616 
10617 		phba->fc_stat.elsRcvLOGO++;
10618 		lpfc_send_els_event(vport, ndlp, payload);
10619 		if (vport->port_state < LPFC_DISC_AUTH) {
10620 			rjt_err = LSRJT_UNABLE_TPC;
10621 			rjt_exp = LSEXP_NOTHING_MORE;
10622 			break;
10623 		}
10624 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
10625 		if (newnode)
10626 			lpfc_disc_state_machine(vport, ndlp, NULL,
10627 					NLP_EVT_DEVICE_RM);
10628 		break;
10629 	case ELS_CMD_PRLO:
10630 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10631 			"RCV PRLO:        did:x%x/ste:x%x flg:x%lx",
10632 			did, vport->port_state, ndlp->nlp_flag);
10633 
10634 		phba->fc_stat.elsRcvPRLO++;
10635 		lpfc_send_els_event(vport, ndlp, payload);
10636 		if (vport->port_state < LPFC_DISC_AUTH) {
10637 			rjt_err = LSRJT_UNABLE_TPC;
10638 			rjt_exp = LSEXP_NOTHING_MORE;
10639 			break;
10640 		}
10641 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
10642 		break;
10643 	case ELS_CMD_LCB:
10644 		phba->fc_stat.elsRcvLCB++;
10645 		lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
10646 		break;
10647 	case ELS_CMD_RDP:
10648 		phba->fc_stat.elsRcvRDP++;
10649 		lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
10650 		break;
10651 	case ELS_CMD_RSCN:
10652 		phba->fc_stat.elsRcvRSCN++;
10653 		lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
10654 		if (newnode)
10655 			lpfc_disc_state_machine(vport, ndlp, NULL,
10656 					NLP_EVT_DEVICE_RM);
10657 		break;
10658 	case ELS_CMD_ADISC:
10659 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10660 			"RCV ADISC:       did:x%x/ste:x%x flg:x%lx",
10661 			did, vport->port_state, ndlp->nlp_flag);
10662 
10663 		lpfc_send_els_event(vport, ndlp, payload);
10664 		phba->fc_stat.elsRcvADISC++;
10665 		if (vport->port_state < LPFC_DISC_AUTH) {
10666 			rjt_err = LSRJT_UNABLE_TPC;
10667 			rjt_exp = LSEXP_NOTHING_MORE;
10668 			break;
10669 		}
10670 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
10671 					NLP_EVT_RCV_ADISC);
10672 		break;
10673 	case ELS_CMD_PDISC:
10674 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10675 			"RCV PDISC:       did:x%x/ste:x%x flg:x%lx",
10676 			did, vport->port_state, ndlp->nlp_flag);
10677 
10678 		phba->fc_stat.elsRcvPDISC++;
10679 		if (vport->port_state < LPFC_DISC_AUTH) {
10680 			rjt_err = LSRJT_UNABLE_TPC;
10681 			rjt_exp = LSEXP_NOTHING_MORE;
10682 			break;
10683 		}
10684 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
10685 					NLP_EVT_RCV_PDISC);
10686 		break;
10687 	case ELS_CMD_FARPR:
10688 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10689 			"RCV FARPR:       did:x%x/ste:x%x flg:x%lx",
10690 			did, vport->port_state, ndlp->nlp_flag);
10691 
10692 		phba->fc_stat.elsRcvFARPR++;
10693 		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
10694 		break;
10695 	case ELS_CMD_FARP:
10696 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10697 			"RCV FARP:        did:x%x/ste:x%x flg:x%lx",
10698 			did, vport->port_state, ndlp->nlp_flag);
10699 
10700 		phba->fc_stat.elsRcvFARP++;
10701 		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
10702 		break;
10703 	case ELS_CMD_FAN:
10704 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10705 			"RCV FAN:         did:x%x/ste:x%x flg:x%lx",
10706 			did, vport->port_state, ndlp->nlp_flag);
10707 
10708 		phba->fc_stat.elsRcvFAN++;
10709 		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
10710 		break;
10711 	case ELS_CMD_PRLI:
10712 	case ELS_CMD_NVMEPRLI:
10713 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10714 			"RCV PRLI:        did:x%x/ste:x%x flg:x%lx",
10715 			did, vport->port_state, ndlp->nlp_flag);
10716 
10717 		phba->fc_stat.elsRcvPRLI++;
10718 		if ((vport->port_state < LPFC_DISC_AUTH) &&
10719 		    test_bit(FC_FABRIC, &vport->fc_flag)) {
10720 			rjt_err = LSRJT_UNABLE_TPC;
10721 			rjt_exp = LSEXP_NOTHING_MORE;
10722 			break;
10723 		}
10724 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
10725 		break;
10726 	case ELS_CMD_LIRR:
10727 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10728 			"RCV LIRR:        did:x%x/ste:x%x flg:x%lx",
10729 			did, vport->port_state, ndlp->nlp_flag);
10730 
10731 		phba->fc_stat.elsRcvLIRR++;
10732 		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
10733 		if (newnode)
10734 			lpfc_disc_state_machine(vport, ndlp, NULL,
10735 					NLP_EVT_DEVICE_RM);
10736 		break;
10737 	case ELS_CMD_RLS:
10738 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10739 			"RCV RLS:         did:x%x/ste:x%x flg:x%lx",
10740 			did, vport->port_state, ndlp->nlp_flag);
10741 
10742 		phba->fc_stat.elsRcvRLS++;
10743 		lpfc_els_rcv_rls(vport, elsiocb, ndlp);
10744 		if (newnode)
10745 			lpfc_disc_state_machine(vport, ndlp, NULL,
10746 					NLP_EVT_DEVICE_RM);
10747 		break;
10748 	case ELS_CMD_RPL:
10749 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10750 			"RCV RPL:         did:x%x/ste:x%x flg:x%lx",
10751 			did, vport->port_state, ndlp->nlp_flag);
10752 
10753 		phba->fc_stat.elsRcvRPL++;
10754 		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
10755 		if (newnode)
10756 			lpfc_disc_state_machine(vport, ndlp, NULL,
10757 					NLP_EVT_DEVICE_RM);
10758 		break;
10759 	case ELS_CMD_RNID:
10760 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10761 			"RCV RNID:        did:x%x/ste:x%x flg:x%lx",
10762 			did, vport->port_state, ndlp->nlp_flag);
10763 
10764 		phba->fc_stat.elsRcvRNID++;
10765 		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
10766 		if (newnode)
10767 			lpfc_disc_state_machine(vport, ndlp, NULL,
10768 					NLP_EVT_DEVICE_RM);
10769 		break;
10770 	case ELS_CMD_RTV:
10771 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10772 			"RCV RTV:        did:x%x/ste:x%x flg:x%lx",
10773 			did, vport->port_state, ndlp->nlp_flag);
10774 		phba->fc_stat.elsRcvRTV++;
10775 		lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
10776 		if (newnode)
10777 			lpfc_disc_state_machine(vport, ndlp, NULL,
10778 					NLP_EVT_DEVICE_RM);
10779 		break;
10780 	case ELS_CMD_RRQ:
10781 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10782 			"RCV RRQ:         did:x%x/ste:x%x flg:x%lx",
10783 			did, vport->port_state, ndlp->nlp_flag);
10784 
10785 		phba->fc_stat.elsRcvRRQ++;
10786 		lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
10787 		if (newnode)
10788 			lpfc_disc_state_machine(vport, ndlp, NULL,
10789 					NLP_EVT_DEVICE_RM);
10790 		break;
10791 	case ELS_CMD_ECHO:
10792 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10793 			"RCV ECHO:        did:x%x/ste:x%x flg:x%lx",
10794 			did, vport->port_state, ndlp->nlp_flag);
10795 
10796 		phba->fc_stat.elsRcvECHO++;
10797 		lpfc_els_rcv_echo(vport, elsiocb, ndlp);
10798 		if (newnode)
10799 			lpfc_disc_state_machine(vport, ndlp, NULL,
10800 					NLP_EVT_DEVICE_RM);
10801 		break;
10802 	case ELS_CMD_REC:
10803 		/* receive this due to exchange closed */
10804 		rjt_err = LSRJT_UNABLE_TPC;
10805 		rjt_exp = LSEXP_INVALID_OX_RX;
10806 		break;
10807 	case ELS_CMD_FPIN:
10808 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10809 				      "RCV FPIN:       did:x%x/ste:x%x "
10810 				      "flg:x%lx",
10811 				      did, vport->port_state, ndlp->nlp_flag);
10812 
10813 		lpfc_els_rcv_fpin(vport, (struct fc_els_fpin *)payload,
10814 				  payload_len);
10815 
10816 		/* There are no replies, so no rjt codes */
10817 		break;
10818 	case ELS_CMD_EDC:
10819 		lpfc_els_rcv_edc(vport, elsiocb, ndlp);
10820 		break;
10821 	case ELS_CMD_RDF:
10822 		phba->fc_stat.elsRcvRDF++;
10823 		/* Accept RDF only from fabric controller */
10824 		if (did != Fabric_Cntl_DID) {
10825 			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
10826 					 "1115 Received RDF from invalid DID "
10827 					 "x%x\n", did);
10828 			rjt_err = LSRJT_PROTOCOL_ERR;
10829 			rjt_exp = LSEXP_NOTHING_MORE;
10830 			goto lsrjt;
10831 		}
10832 
10833 		lpfc_els_rcv_rdf(vport, elsiocb, ndlp);
10834 		break;
10835 	default:
10836 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
10837 			"RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
10838 			cmd, did, vport->port_state);
10839 
10840 		/* Unsupported ELS command, reject */
10841 		rjt_err = LSRJT_CMD_UNSUPPORTED;
10842 		rjt_exp = LSEXP_NOTHING_MORE;
10843 
10844 		/* Unknown ELS command <elsCmd> received from NPORT <did> */
10845 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
10846 				 "0115 Unknown ELS command x%x "
10847 				 "received from NPORT x%x\n", cmd, did);
10848 		if (newnode)
10849 			lpfc_disc_state_machine(vport, ndlp, NULL,
10850 					NLP_EVT_DEVICE_RM);
10851 		break;
10852 	}
10853 
10854 lsrjt:
10855 	/* check if need to LS_RJT received ELS cmd */
10856 	if (rjt_err) {
10857 		memset(&stat, 0, sizeof(stat));
10858 		stat.un.b.lsRjtRsnCode = rjt_err;
10859 		stat.un.b.lsRjtRsnCodeExp = rjt_exp;
10860 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
10861 				    NULL);
10862 		/* Remove the reference from above for new nodes. */
10863 		if (newnode)
10864 			lpfc_disc_state_machine(vport, ndlp, NULL,
10865 					NLP_EVT_DEVICE_RM);
10866 	}
10867 
10868 	/* Release the reference on this elsiocb, not the ndlp. */
10869 	lpfc_nlp_put(elsiocb->ndlp);
10870 	elsiocb->ndlp = NULL;
10871 
10872 	/* Special case.  Driver received an unsolicited command that
10873 	 * unsupportable given the driver's current state.  Reset the
10874 	 * link and start over.
10875 	 */
10876 	if (init_link) {
10877 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10878 		if (!mbox)
10879 			return;
10880 		lpfc_linkdown(phba);
10881 		lpfc_init_link(phba, mbox,
10882 			       phba->cfg_topology,
10883 			       phba->cfg_link_speed);
10884 		mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
10885 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10886 		mbox->vport = vport;
10887 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
10888 		    MBX_NOT_FINISHED)
10889 			mempool_free(mbox, phba->mbox_mem_pool);
10890 	}
10891 
10892 	return;
10893 
10894 dropit:
10895 	if (vport && !test_bit(FC_UNLOADING, &vport->load_flag))
10896 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
10897 			"0111 Dropping received ELS cmd "
10898 			"Data: x%x x%x x%x x%x\n",
10899 			cmd, status, get_job_word4(phba, elsiocb), did);
10900 
10901 	phba->fc_stat.elsRcvDrop++;
10902 }
10903 
10904 /**
10905  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
10906  * @phba: pointer to lpfc hba data structure.
10907  * @pring: pointer to a SLI ring.
10908  * @elsiocb: pointer to lpfc els iocb data structure.
10909  *
10910  * This routine is used to process an unsolicited event received from a SLI
10911  * (Service Level Interface) ring. The actual processing of the data buffer
10912  * associated with the unsolicited event is done by invoking the routine
10913  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
10914  * SLI ring on which the unsolicited event was received.
10915  **/
10916 void
10917 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10918 		     struct lpfc_iocbq *elsiocb)
10919 {
10920 	struct lpfc_vport *vport = elsiocb->vport;
10921 	u32 ulp_command, status, parameter, bde_count = 0;
10922 	IOCB_t *icmd;
10923 	struct lpfc_wcqe_complete *wcqe_cmpl = NULL;
10924 	struct lpfc_dmabuf *bdeBuf1 = elsiocb->cmd_dmabuf;
10925 	struct lpfc_dmabuf *bdeBuf2 = elsiocb->bpl_dmabuf;
10926 	dma_addr_t paddr;
10927 
10928 	elsiocb->cmd_dmabuf = NULL;
10929 	elsiocb->rsp_dmabuf = NULL;
10930 	elsiocb->bpl_dmabuf = NULL;
10931 
10932 	wcqe_cmpl = &elsiocb->wcqe_cmpl;
10933 	ulp_command = get_job_cmnd(phba, elsiocb);
10934 	status = get_job_ulpstatus(phba, elsiocb);
10935 	parameter = get_job_word4(phba, elsiocb);
10936 	if (phba->sli_rev == LPFC_SLI_REV4)
10937 		bde_count = wcqe_cmpl->word3;
10938 	else
10939 		bde_count = elsiocb->iocb.ulpBdeCount;
10940 
10941 	if (status == IOSTAT_NEED_BUFFER) {
10942 		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
10943 	} else if (status == IOSTAT_LOCAL_REJECT &&
10944 		   (parameter & IOERR_PARAM_MASK) ==
10945 		   IOERR_RCV_BUFFER_WAITING) {
10946 		phba->fc_stat.NoRcvBuf++;
10947 		/* Not enough posted buffers; Try posting more buffers */
10948 		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
10949 			lpfc_sli3_post_buffer(phba, pring, 0);
10950 		return;
10951 	}
10952 
10953 	if (phba->sli_rev == LPFC_SLI_REV3) {
10954 		icmd = &elsiocb->iocb;
10955 		if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
10956 		    (ulp_command == CMD_IOCB_RCV_ELS64_CX ||
10957 		     ulp_command == CMD_IOCB_RCV_SEQ64_CX)) {
10958 			if (icmd->unsli3.rcvsli3.vpi == 0xffff)
10959 				vport = phba->pport;
10960 			else
10961 				vport = lpfc_find_vport_by_vpid(phba,
10962 						icmd->unsli3.rcvsli3.vpi);
10963 		}
10964 	}
10965 
10966 	/* If there are no BDEs associated
10967 	 * with this IOCB, there is nothing to do.
10968 	 */
10969 	if (bde_count == 0)
10970 		return;
10971 
10972 	/* Account for SLI2 or SLI3 and later unsolicited buffering */
10973 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
10974 		elsiocb->cmd_dmabuf = bdeBuf1;
10975 		if (bde_count == 2)
10976 			elsiocb->bpl_dmabuf = bdeBuf2;
10977 	} else {
10978 		icmd = &elsiocb->iocb;
10979 		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
10980 				 icmd->un.cont64[0].addrLow);
10981 		elsiocb->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,
10982 							       paddr);
10983 		if (bde_count == 2) {
10984 			paddr = getPaddr(icmd->un.cont64[1].addrHigh,
10985 					 icmd->un.cont64[1].addrLow);
10986 			elsiocb->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,
10987 									pring,
10988 									paddr);
10989 		}
10990 	}
10991 
10992 	lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
10993 	/*
10994 	 * The different unsolicited event handlers would tell us
10995 	 * if they are done with "mp" by setting cmd_dmabuf/bpl_dmabuf to NULL.
10996 	 */
10997 	if (elsiocb->cmd_dmabuf) {
10998 		lpfc_in_buf_free(phba, elsiocb->cmd_dmabuf);
10999 		elsiocb->cmd_dmabuf = NULL;
11000 	}
11001 
11002 	if (elsiocb->bpl_dmabuf) {
11003 		lpfc_in_buf_free(phba, elsiocb->bpl_dmabuf);
11004 		elsiocb->bpl_dmabuf = NULL;
11005 	}
11006 
11007 }
11008 
11009 static void
11010 lpfc_start_fdmi(struct lpfc_vport *vport)
11011 {
11012 	struct lpfc_nodelist *ndlp;
11013 
11014 	/* If this is the first time, allocate an ndlp and initialize
11015 	 * it. Otherwise, make sure the node is enabled and then do the
11016 	 * login.
11017 	 */
11018 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
11019 	if (!ndlp) {
11020 		ndlp = lpfc_nlp_init(vport, FDMI_DID);
11021 		if (ndlp) {
11022 			ndlp->nlp_type |= NLP_FABRIC;
11023 		} else {
11024 			return;
11025 		}
11026 	}
11027 
11028 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
11029 	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
11030 }
11031 
11032 /**
11033  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
11034  * @phba: pointer to lpfc hba data structure.
11035  * @vport: pointer to a virtual N_Port data structure.
11036  *
11037  * This routine issues a Port Login (PLOGI) to the Name Server with
11038  * State Change Request (SCR) for a @vport. This routine will create an
11039  * ndlp for the Name Server associated to the @vport if such node does
11040  * not already exist. The PLOGI to Name Server is issued by invoking the
11041  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
11042  * (FDMI) is configured to the @vport, a FDMI node will be created and
11043  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
11044  **/
11045 void
11046 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
11047 {
11048 	struct lpfc_nodelist *ndlp;
11049 
11050 	/*
11051 	 * If lpfc_delay_discovery parameter is set and the clean address
11052 	 * bit is cleared and fc fabric parameters chenged, delay FC NPort
11053 	 * discovery.
11054 	 */
11055 	if (test_bit(FC_DISC_DELAYED, &vport->fc_flag)) {
11056 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11057 				 "3334 Delay fc port discovery for %d secs\n",
11058 				 phba->fc_ratov);
11059 		mod_timer(&vport->delayed_disc_tmo,
11060 			jiffies + secs_to_jiffies(phba->fc_ratov));
11061 		return;
11062 	}
11063 
11064 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
11065 	if (!ndlp) {
11066 		ndlp = lpfc_nlp_init(vport, NameServer_DID);
11067 		if (!ndlp) {
11068 			if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
11069 				lpfc_disc_start(vport);
11070 				return;
11071 			}
11072 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11073 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11074 					 "0251 NameServer login: no memory\n");
11075 			return;
11076 		}
11077 	}
11078 
11079 	ndlp->nlp_type |= NLP_FABRIC;
11080 
11081 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
11082 
11083 	if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
11084 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11085 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11086 				 "0252 Cannot issue NameServer login\n");
11087 		return;
11088 	}
11089 
11090 	if ((phba->cfg_enable_SmartSAN ||
11091 	     phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT) &&
11092 	    test_bit(FC_ALLOW_FDMI, &vport->load_flag))
11093 		lpfc_start_fdmi(vport);
11094 }
11095 
11096 /**
11097  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
11098  * @phba: pointer to lpfc hba data structure.
11099  * @pmb: pointer to the driver internal queue element for mailbox command.
11100  *
11101  * This routine is the completion callback function to register new vport
11102  * mailbox command. If the new vport mailbox command completes successfully,
11103  * the fabric registration login shall be performed on physical port (the
11104  * new vport created is actually a physical port, with VPI 0) or the port
11105  * login to Name Server for State Change Request (SCR) will be performed
11106  * on virtual port (real virtual port, with VPI greater than 0).
11107  **/
11108 static void
11109 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
11110 {
11111 	struct lpfc_vport *vport = pmb->vport;
11112 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
11113 	struct lpfc_nodelist *ndlp = pmb->ctx_ndlp;
11114 	MAILBOX_t *mb = &pmb->u.mb;
11115 	int rc;
11116 
11117 	clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11118 
11119 	if (mb->mbxStatus) {
11120 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11121 				"0915 Register VPI failed : Status: x%x"
11122 				" upd bit: x%x \n", mb->mbxStatus,
11123 				 mb->un.varRegVpi.upd);
11124 		if (phba->sli_rev == LPFC_SLI_REV4 &&
11125 			mb->un.varRegVpi.upd)
11126 			goto mbox_err_exit ;
11127 
11128 		switch (mb->mbxStatus) {
11129 		case 0x11:	/* unsupported feature */
11130 		case 0x9603:	/* max_vpi exceeded */
11131 		case 0x9602:	/* Link event since CLEAR_LA */
11132 			/* giving up on vport registration */
11133 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11134 			clear_bit(FC_FABRIC, &vport->fc_flag);
11135 			clear_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
11136 			lpfc_can_disctmo(vport);
11137 			break;
11138 		/* If reg_vpi fail with invalid VPI status, re-init VPI */
11139 		case 0x20:
11140 			set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11141 			lpfc_init_vpi(phba, pmb, vport->vpi);
11142 			pmb->vport = vport;
11143 			pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
11144 			rc = lpfc_sli_issue_mbox(phba, pmb,
11145 				MBX_NOWAIT);
11146 			if (rc == MBX_NOT_FINISHED) {
11147 				lpfc_printf_vlog(vport, KERN_ERR,
11148 						 LOG_TRACE_EVENT,
11149 					"2732 Failed to issue INIT_VPI"
11150 					" mailbox command\n");
11151 			} else {
11152 				lpfc_nlp_put(ndlp);
11153 				return;
11154 			}
11155 			fallthrough;
11156 		default:
11157 			/* Try to recover from this error */
11158 			if (phba->sli_rev == LPFC_SLI_REV4)
11159 				lpfc_sli4_unreg_all_rpis(vport);
11160 			lpfc_mbx_unreg_vpi(vport);
11161 			set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11162 			if (mb->mbxStatus == MBX_NOT_FINISHED)
11163 				break;
11164 			if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
11165 			    !test_bit(FC_LOGO_RCVD_DID_CHNG, &vport->fc_flag)) {
11166 				if (phba->sli_rev == LPFC_SLI_REV4)
11167 					lpfc_issue_init_vfi(vport);
11168 				else
11169 					lpfc_initial_flogi(vport);
11170 			} else {
11171 				lpfc_initial_fdisc(vport);
11172 			}
11173 			break;
11174 		}
11175 	} else {
11176 		spin_lock_irq(shost->host_lock);
11177 		vport->vpi_state |= LPFC_VPI_REGISTERED;
11178 		spin_unlock_irq(shost->host_lock);
11179 		if (vport == phba->pport) {
11180 			if (phba->sli_rev < LPFC_SLI_REV4)
11181 				lpfc_issue_fabric_reglogin(vport);
11182 			else {
11183 				/*
11184 				 * If the physical port is instantiated using
11185 				 * FDISC, do not start vport discovery.
11186 				 */
11187 				if (vport->port_state != LPFC_FDISC)
11188 					lpfc_start_fdiscs(phba);
11189 				lpfc_do_scr_ns_plogi(phba, vport);
11190 			}
11191 		} else {
11192 			lpfc_do_scr_ns_plogi(phba, vport);
11193 		}
11194 	}
11195 mbox_err_exit:
11196 	/* Now, we decrement the ndlp reference count held for this
11197 	 * callback function
11198 	 */
11199 	lpfc_nlp_put(ndlp);
11200 
11201 	mempool_free(pmb, phba->mbox_mem_pool);
11202 
11203 	/* reinitialize the VMID datastructure before returning.
11204 	 * this is specifically for vport
11205 	 */
11206 	if (lpfc_is_vmid_enabled(phba))
11207 		lpfc_reinit_vmid(vport);
11208 	vport->vmid_flag = vport->phba->pport->vmid_flag;
11209 
11210 	return;
11211 }
11212 
11213 /**
11214  * lpfc_register_new_vport - Register a new vport with a HBA
11215  * @phba: pointer to lpfc hba data structure.
11216  * @vport: pointer to a host virtual N_Port data structure.
11217  * @ndlp: pointer to a node-list data structure.
11218  *
11219  * This routine registers the @vport as a new virtual port with a HBA.
11220  * It is done through a registering vpi mailbox command.
11221  **/
11222 void
11223 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
11224 			struct lpfc_nodelist *ndlp)
11225 {
11226 	LPFC_MBOXQ_t *mbox;
11227 
11228 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11229 	if (mbox) {
11230 		lpfc_reg_vpi(vport, mbox);
11231 		mbox->vport = vport;
11232 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
11233 		if (!mbox->ctx_ndlp) {
11234 			mempool_free(mbox, phba->mbox_mem_pool);
11235 			goto mbox_err_exit;
11236 		}
11237 
11238 		mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
11239 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
11240 		    == MBX_NOT_FINISHED) {
11241 			/* mailbox command not success, decrement ndlp
11242 			 * reference count for this command
11243 			 */
11244 			lpfc_nlp_put(ndlp);
11245 			mempool_free(mbox, phba->mbox_mem_pool);
11246 
11247 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11248 				"0253 Register VPI: Can't send mbox\n");
11249 			goto mbox_err_exit;
11250 		}
11251 	} else {
11252 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11253 				 "0254 Register VPI: no memory\n");
11254 		goto mbox_err_exit;
11255 	}
11256 	return;
11257 
11258 mbox_err_exit:
11259 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11260 	clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11261 	return;
11262 }
11263 
11264 /**
11265  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
11266  * @phba: pointer to lpfc hba data structure.
11267  *
11268  * This routine cancels the retry delay timers to all the vports.
11269  **/
11270 void
11271 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
11272 {
11273 	struct lpfc_vport **vports;
11274 	struct lpfc_nodelist *ndlp;
11275 	uint32_t link_state;
11276 	int i;
11277 
11278 	/* Treat this failure as linkdown for all vports */
11279 	link_state = phba->link_state;
11280 	lpfc_linkdown(phba);
11281 	phba->link_state = link_state;
11282 
11283 	vports = lpfc_create_vport_work_array(phba);
11284 
11285 	if (vports) {
11286 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
11287 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
11288 			if (ndlp)
11289 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
11290 			lpfc_els_flush_cmd(vports[i]);
11291 		}
11292 		lpfc_destroy_vport_work_array(phba, vports);
11293 	}
11294 }
11295 
11296 /**
11297  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
11298  * @phba: pointer to lpfc hba data structure.
11299  *
11300  * This routine abort all pending discovery commands and
11301  * start a timer to retry FLOGI for the physical port
11302  * discovery.
11303  **/
11304 void
11305 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
11306 {
11307 	struct lpfc_nodelist *ndlp;
11308 
11309 	/* Cancel the all vports retry delay retry timers */
11310 	lpfc_cancel_all_vport_retry_delay_timer(phba);
11311 
11312 	/* If fabric require FLOGI, then re-instantiate physical login */
11313 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
11314 	if (!ndlp)
11315 		return;
11316 
11317 	mod_timer(&ndlp->nlp_delayfunc, jiffies + secs_to_jiffies(1));
11318 	set_bit(NLP_DELAY_TMO, &ndlp->nlp_flag);
11319 	ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
11320 	phba->pport->port_state = LPFC_FLOGI;
11321 	return;
11322 }
11323 
11324 /**
11325  * lpfc_fabric_login_reqd - Check if FLOGI required.
11326  * @phba: pointer to lpfc hba data structure.
11327  * @cmdiocb: pointer to FDISC command iocb.
11328  * @rspiocb: pointer to FDISC response iocb.
11329  *
11330  * This routine checks if a FLOGI is reguired for FDISC
11331  * to succeed.
11332  **/
11333 static int
11334 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
11335 		struct lpfc_iocbq *cmdiocb,
11336 		struct lpfc_iocbq *rspiocb)
11337 {
11338 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
11339 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
11340 
11341 	if (ulp_status != IOSTAT_FABRIC_RJT ||
11342 	    ulp_word4 != RJT_LOGIN_REQUIRED)
11343 		return 0;
11344 	else
11345 		return 1;
11346 }
11347 
11348 /**
11349  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
11350  * @phba: pointer to lpfc hba data structure.
11351  * @cmdiocb: pointer to lpfc command iocb data structure.
11352  * @rspiocb: pointer to lpfc response iocb data structure.
11353  *
11354  * This routine is the completion callback function to a Fabric Discover
11355  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
11356  * single threaded, each FDISC completion callback function will reset
11357  * the discovery timer for all vports such that the timers will not get
11358  * unnecessary timeout. The function checks the FDISC IOCB status. If error
11359  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
11360  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
11361  * assigned to the vport has been changed with the completion of the FDISC
11362  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
11363  * are unregistered from the HBA, and then the lpfc_register_new_vport()
11364  * routine is invoked to register new vport with the HBA. Otherwise, the
11365  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
11366  * Server for State Change Request (SCR).
11367  **/
11368 static void
11369 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11370 		    struct lpfc_iocbq *rspiocb)
11371 {
11372 	struct lpfc_vport *vport = cmdiocb->vport;
11373 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
11374 	struct lpfc_nodelist *np;
11375 	struct lpfc_nodelist *next_np;
11376 	struct lpfc_iocbq *piocb;
11377 	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf, *prsp;
11378 	struct serv_parm *sp;
11379 	uint8_t fabric_param_changed;
11380 	u32 ulp_status, ulp_word4;
11381 
11382 	ulp_status = get_job_ulpstatus(phba, rspiocb);
11383 	ulp_word4 = get_job_word4(phba, rspiocb);
11384 
11385 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
11386 			 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
11387 			 ulp_status, ulp_word4,
11388 			 vport->fc_prevDID);
11389 	/* Since all FDISCs are being single threaded, we
11390 	 * must reset the discovery timer for ALL vports
11391 	 * waiting to send FDISC when one completes.
11392 	 */
11393 	list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
11394 		lpfc_set_disctmo(piocb->vport);
11395 	}
11396 
11397 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11398 		"FDISC cmpl:      status:x%x/x%x prevdid:x%x",
11399 		ulp_status, ulp_word4, vport->fc_prevDID);
11400 
11401 	if (ulp_status) {
11402 
11403 		if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
11404 			lpfc_retry_pport_discovery(phba);
11405 			goto out;
11406 		}
11407 
11408 		/* Check for retry */
11409 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
11410 			goto out;
11411 		/* Warn FDISC status */
11412 		lpfc_vlog_msg(vport, KERN_WARNING, LOG_ELS,
11413 			      "0126 FDISC cmpl status: x%x/x%x)\n",
11414 			      ulp_status, ulp_word4);
11415 
11416 		/* drop initial reference */
11417 		if (!test_and_set_bit(NLP_DROPPED, &ndlp->nlp_flag))
11418 			lpfc_nlp_put(ndlp);
11419 
11420 		goto fdisc_failed;
11421 	}
11422 
11423 	lpfc_check_nlp_post_devloss(vport, ndlp);
11424 
11425 	clear_bit(FC_VPORT_CVL_RCVD, &vport->fc_flag);
11426 	clear_bit(FC_VPORT_LOGO_RCVD, &vport->fc_flag);
11427 	set_bit(FC_FABRIC, &vport->fc_flag);
11428 	if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
11429 		set_bit(FC_PUBLIC_LOOP, &vport->fc_flag);
11430 
11431 	vport->fc_myDID = ulp_word4 & Mask_DID;
11432 	lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
11433 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
11434 	if (!prsp)
11435 		goto out;
11436 	if (!lpfc_is_els_acc_rsp(prsp))
11437 		goto out;
11438 
11439 	sp = prsp->virt + sizeof(uint32_t);
11440 	fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
11441 	memcpy(&vport->fabric_portname, &sp->portName,
11442 		sizeof(struct lpfc_name));
11443 	memcpy(&vport->fabric_nodename, &sp->nodeName,
11444 		sizeof(struct lpfc_name));
11445 	if (fabric_param_changed &&
11446 		!test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
11447 		/* If our NportID changed, we need to ensure all
11448 		 * remaining NPORTs get unreg_login'ed so we can
11449 		 * issue unreg_vpi.
11450 		 */
11451 		list_for_each_entry_safe(np, next_np,
11452 			&vport->fc_nodes, nlp_listp) {
11453 			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
11454 			    !test_bit(NLP_NPR_ADISC, &np->nlp_flag))
11455 				continue;
11456 			clear_bit(NLP_NPR_ADISC, &np->nlp_flag);
11457 			lpfc_unreg_rpi(vport, np);
11458 		}
11459 		lpfc_cleanup_pending_mbox(vport);
11460 
11461 		if (phba->sli_rev == LPFC_SLI_REV4)
11462 			lpfc_sli4_unreg_all_rpis(vport);
11463 
11464 		lpfc_mbx_unreg_vpi(vport);
11465 		set_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
11466 		if (phba->sli_rev == LPFC_SLI_REV4)
11467 			set_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag);
11468 		else
11469 			set_bit(FC_LOGO_RCVD_DID_CHNG, &vport->fc_flag);
11470 	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
11471 		   !test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag)) {
11472 		/*
11473 		 * Driver needs to re-reg VPI in order for f/w
11474 		 * to update the MAC address.
11475 		 */
11476 		lpfc_register_new_vport(phba, vport, ndlp);
11477 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
11478 		goto out;
11479 	}
11480 
11481 	if (test_bit(FC_VPORT_NEEDS_INIT_VPI, &vport->fc_flag))
11482 		lpfc_issue_init_vpi(vport);
11483 	else if (test_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag))
11484 		lpfc_register_new_vport(phba, vport, ndlp);
11485 	else
11486 		lpfc_do_scr_ns_plogi(phba, vport);
11487 
11488 	/* The FDISC completed successfully. Move the fabric ndlp to
11489 	 * UNMAPPED state and register with the transport.
11490 	 */
11491 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
11492 	goto out;
11493 
11494 fdisc_failed:
11495 	if (vport->fc_vport &&
11496 	    (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
11497 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11498 	/* Cancel discovery timer */
11499 	lpfc_can_disctmo(vport);
11500 out:
11501 	lpfc_els_free_iocb(phba, cmdiocb);
11502 	lpfc_nlp_put(ndlp);
11503 }
11504 
11505 /**
11506  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
11507  * @vport: pointer to a virtual N_Port data structure.
11508  * @ndlp: pointer to a node-list data structure.
11509  * @retry: number of retries to the command IOCB.
11510  *
11511  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
11512  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
11513  * routine to issue the IOCB, which makes sure only one outstanding fabric
11514  * IOCB will be sent off HBA at any given time.
11515  *
11516  * Note that the ndlp reference count will be incremented by 1 for holding the
11517  * ndlp and the reference to ndlp will be stored into the ndlp field of
11518  * the IOCB for the completion callback function to the FDISC ELS command.
11519  *
11520  * Return code
11521  *   0 - Successfully issued fdisc iocb command
11522  *   1 - Failed to issue fdisc iocb command
11523  **/
11524 static int
11525 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
11526 		     uint8_t retry)
11527 {
11528 	struct lpfc_hba *phba = vport->phba;
11529 	IOCB_t *icmd;
11530 	union lpfc_wqe128 *wqe = NULL;
11531 	struct lpfc_iocbq *elsiocb;
11532 	struct serv_parm *sp;
11533 	uint8_t *pcmd;
11534 	uint16_t cmdsize;
11535 	int did = ndlp->nlp_DID;
11536 	int rc;
11537 
11538 	vport->port_state = LPFC_FDISC;
11539 	vport->fc_myDID = 0;
11540 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
11541 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
11542 				     ELS_CMD_FDISC);
11543 	if (!elsiocb) {
11544 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11545 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11546 				 "0255 Issue FDISC: no IOCB\n");
11547 		return 1;
11548 	}
11549 
11550 	if (phba->sli_rev == LPFC_SLI_REV4) {
11551 		wqe = &elsiocb->wqe;
11552 		bf_set(els_req64_sid, &wqe->els_req, 0);
11553 		bf_set(els_req64_sp, &wqe->els_req, 1);
11554 	} else {
11555 		icmd = &elsiocb->iocb;
11556 		icmd->un.elsreq64.myID = 0;
11557 		icmd->un.elsreq64.fl = 1;
11558 		icmd->ulpCt_h = 1;
11559 		icmd->ulpCt_l = 0;
11560 	}
11561 
11562 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
11563 	*((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
11564 	pcmd += sizeof(uint32_t); /* CSP Word 1 */
11565 	memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
11566 	sp = (struct serv_parm *) pcmd;
11567 	/* Setup CSPs accordingly for Fabric */
11568 	sp->cmn.e_d_tov = 0;
11569 	sp->cmn.w2.r_a_tov = 0;
11570 	sp->cmn.virtual_fabric_support = 0;
11571 	sp->cls1.classValid = 0;
11572 	sp->cls2.seqDelivery = 1;
11573 	sp->cls3.seqDelivery = 1;
11574 
11575 	/* Fill out Auxiliary Parameter Data */
11576 	if (phba->pni) {
11577 		sp->aux.flags =
11578 			AUX_PARM_DATA_VALID | AUX_PARM_PNI_VALID;
11579 		sp->aux.pni = cpu_to_be64(phba->pni);
11580 	}
11581 
11582 	pcmd += sizeof(uint32_t); /* CSP Word 2 */
11583 	pcmd += sizeof(uint32_t); /* CSP Word 3 */
11584 	pcmd += sizeof(uint32_t); /* CSP Word 4 */
11585 	pcmd += sizeof(uint32_t); /* Port Name */
11586 	memcpy(pcmd, &vport->fc_portname, 8);
11587 	pcmd += sizeof(uint32_t); /* Node Name */
11588 	pcmd += sizeof(uint32_t); /* Node Name */
11589 	memcpy(pcmd, &vport->fc_nodename, 8);
11590 	sp->cmn.valid_vendor_ver_level = 0;
11591 	memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
11592 	lpfc_set_disctmo(vport);
11593 
11594 	phba->fc_stat.elsXmitFDISC++;
11595 	elsiocb->cmd_cmpl = lpfc_cmpl_els_fdisc;
11596 
11597 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11598 		"Issue FDISC:     did:x%x",
11599 		did, 0, 0);
11600 
11601 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
11602 	if (!elsiocb->ndlp)
11603 		goto err_out;
11604 
11605 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
11606 	if (rc == IOCB_ERROR) {
11607 		lpfc_nlp_put(ndlp);
11608 		goto err_out;
11609 	}
11610 
11611 	lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
11612 	return 0;
11613 
11614  err_out:
11615 	lpfc_els_free_iocb(phba, elsiocb);
11616 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
11617 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
11618 			 "0256 Issue FDISC: Cannot send IOCB\n");
11619 	return 1;
11620 }
11621 
11622 /**
11623  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
11624  * @phba: pointer to lpfc hba data structure.
11625  * @cmdiocb: pointer to lpfc command iocb data structure.
11626  * @rspiocb: pointer to lpfc response iocb data structure.
11627  *
11628  * This routine is the completion callback function to the issuing of a LOGO
11629  * ELS command off a vport. It frees the command IOCB and then decrement the
11630  * reference count held on ndlp for this completion function, indicating that
11631  * the reference to the ndlp is no long needed. Note that the
11632  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
11633  * callback function and an additional explicit ndlp reference decrementation
11634  * will trigger the actual release of the ndlp.
11635  **/
11636 static void
11637 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11638 			struct lpfc_iocbq *rspiocb)
11639 {
11640 	struct lpfc_vport *vport = cmdiocb->vport;
11641 	IOCB_t *irsp;
11642 	struct lpfc_nodelist *ndlp;
11643 	u32 ulp_status, ulp_word4, did, tmo;
11644 
11645 	ndlp = cmdiocb->ndlp;
11646 
11647 	ulp_status = get_job_ulpstatus(phba, rspiocb);
11648 	ulp_word4 = get_job_word4(phba, rspiocb);
11649 
11650 	if (phba->sli_rev == LPFC_SLI_REV4) {
11651 		did = get_job_els_rsp64_did(phba, cmdiocb);
11652 		tmo = get_wqe_tmo(cmdiocb);
11653 	} else {
11654 		irsp = &rspiocb->iocb;
11655 		did = get_job_els_rsp64_did(phba, rspiocb);
11656 		tmo = irsp->ulpTimeout;
11657 	}
11658 
11659 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11660 		"LOGO npiv cmpl:  status:x%x/x%x did:x%x",
11661 		ulp_status, ulp_word4, did);
11662 
11663 	/* NPIV LOGO completes to NPort <nlp_DID> */
11664 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
11665 			 "2928 NPIV LOGO completes to NPort x%x "
11666 			 "Data: x%x x%x x%x x%x x%x x%lx x%x\n",
11667 			 ndlp->nlp_DID, ulp_status, ulp_word4,
11668 			 tmo, vport->num_disc_nodes,
11669 			 kref_read(&ndlp->kref), ndlp->nlp_flag,
11670 			 ndlp->fc4_xpt_flags);
11671 
11672 	if (ulp_status == IOSTAT_SUCCESS) {
11673 		clear_bit(FC_NDISC_ACTIVE, &vport->fc_flag);
11674 		clear_bit(FC_FABRIC, &vport->fc_flag);
11675 		lpfc_can_disctmo(vport);
11676 	}
11677 
11678 	if (test_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags)) {
11679 		/* Wake up lpfc_vport_delete if waiting...*/
11680 		if (ndlp->logo_waitq)
11681 			wake_up(ndlp->logo_waitq);
11682 		clear_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
11683 		clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
11684 		clear_bit(NLP_WAIT_FOR_LOGO, &ndlp->save_flags);
11685 	}
11686 
11687 	/* Safe to release resources now. */
11688 	lpfc_els_free_iocb(phba, cmdiocb);
11689 	lpfc_nlp_put(ndlp);
11690 }
11691 
11692 /**
11693  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
11694  * @vport: pointer to a virtual N_Port data structure.
11695  * @ndlp: pointer to a node-list data structure.
11696  *
11697  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
11698  *
11699  * Note that the ndlp reference count will be incremented by 1 for holding the
11700  * ndlp and the reference to ndlp will be stored into the ndlp field of
11701  * the IOCB for the completion callback function to the LOGO ELS command.
11702  *
11703  * Return codes
11704  *   0 - Successfully issued logo off the @vport
11705  *   1 - Failed to issue logo off the @vport
11706  **/
11707 int
11708 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
11709 {
11710 	int rc = 0;
11711 	struct lpfc_hba  *phba = vport->phba;
11712 	struct lpfc_iocbq *elsiocb;
11713 	uint8_t *pcmd;
11714 	uint16_t cmdsize;
11715 
11716 	cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
11717 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
11718 				     ELS_CMD_LOGO);
11719 	if (!elsiocb)
11720 		return 1;
11721 
11722 	pcmd = (uint8_t *)elsiocb->cmd_dmabuf->virt;
11723 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
11724 	pcmd += sizeof(uint32_t);
11725 
11726 	/* Fill in LOGO payload */
11727 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
11728 	pcmd += sizeof(uint32_t);
11729 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
11730 
11731 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
11732 		"Issue LOGO npiv  did:x%x flg:x%lx",
11733 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
11734 
11735 	elsiocb->cmd_cmpl = lpfc_cmpl_els_npiv_logo;
11736 	set_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
11737 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
11738 	if (!elsiocb->ndlp) {
11739 		lpfc_els_free_iocb(phba, elsiocb);
11740 		goto err;
11741 	}
11742 
11743 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
11744 	if (rc == IOCB_ERROR) {
11745 		lpfc_els_free_iocb(phba, elsiocb);
11746 		lpfc_nlp_put(ndlp);
11747 		goto err;
11748 	}
11749 	return 0;
11750 
11751 err:
11752 	clear_bit(NLP_LOGO_SND, &ndlp->nlp_flag);
11753 	return 1;
11754 }
11755 
11756 /**
11757  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
11758  * @t: timer context used to obtain the lpfc hba.
11759  *
11760  * This routine is invoked by the fabric iocb block timer after
11761  * timeout. It posts the fabric iocb block timeout event by setting the
11762  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
11763  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
11764  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
11765  * posted event WORKER_FABRIC_BLOCK_TMO.
11766  **/
11767 void
11768 lpfc_fabric_block_timeout(struct timer_list *t)
11769 {
11770 	struct lpfc_hba  *phba = timer_container_of(phba, t,
11771 						    fabric_block_timer);
11772 	unsigned long iflags;
11773 	uint32_t tmo_posted;
11774 
11775 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11776 	tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
11777 	if (!tmo_posted)
11778 		phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
11779 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11780 
11781 	if (!tmo_posted)
11782 		lpfc_worker_wake_up(phba);
11783 	return;
11784 }
11785 
11786 /**
11787  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
11788  * @phba: pointer to lpfc hba data structure.
11789  *
11790  * This routine issues one fabric iocb from the driver internal list to
11791  * the HBA. It first checks whether it's ready to issue one fabric iocb to
11792  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
11793  * remove one pending fabric iocb from the driver internal list and invokes
11794  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
11795  **/
11796 static void
11797 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
11798 {
11799 	struct lpfc_iocbq *iocb;
11800 	unsigned long iflags;
11801 	int ret;
11802 
11803 repeat:
11804 	iocb = NULL;
11805 	spin_lock_irqsave(&phba->hbalock, iflags);
11806 	/* Post any pending iocb to the SLI layer */
11807 	if (atomic_read(&phba->fabric_iocb_count) == 0) {
11808 		list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
11809 				 list);
11810 		if (iocb)
11811 			/* Increment fabric iocb count to hold the position */
11812 			atomic_inc(&phba->fabric_iocb_count);
11813 	}
11814 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11815 	if (iocb) {
11816 		iocb->fabric_cmd_cmpl = iocb->cmd_cmpl;
11817 		iocb->cmd_cmpl = lpfc_cmpl_fabric_iocb;
11818 		iocb->cmd_flag |= LPFC_IO_FABRIC;
11819 
11820 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11821 				      "Fabric sched1:   ste:x%x",
11822 				      iocb->vport->port_state, 0, 0);
11823 
11824 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11825 
11826 		if (ret == IOCB_ERROR) {
11827 			iocb->cmd_cmpl = iocb->fabric_cmd_cmpl;
11828 			iocb->fabric_cmd_cmpl = NULL;
11829 			iocb->cmd_flag &= ~LPFC_IO_FABRIC;
11830 			set_job_ulpstatus(iocb, IOSTAT_LOCAL_REJECT);
11831 			iocb->wcqe_cmpl.parameter = IOERR_SLI_ABORTED;
11832 			iocb->cmd_cmpl(phba, iocb, iocb);
11833 
11834 			atomic_dec(&phba->fabric_iocb_count);
11835 			goto repeat;
11836 		}
11837 	}
11838 }
11839 
11840 /**
11841  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
11842  * @phba: pointer to lpfc hba data structure.
11843  *
11844  * This routine unblocks the  issuing fabric iocb command. The function
11845  * will clear the fabric iocb block bit and then invoke the routine
11846  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
11847  * from the driver internal fabric iocb list.
11848  **/
11849 void
11850 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
11851 {
11852 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11853 
11854 	lpfc_resume_fabric_iocbs(phba);
11855 	return;
11856 }
11857 
11858 /**
11859  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
11860  * @phba: pointer to lpfc hba data structure.
11861  *
11862  * This routine blocks the issuing fabric iocb for a specified amount of
11863  * time (currently 100 ms). This is done by set the fabric iocb block bit
11864  * and set up a timeout timer for 100ms. When the block bit is set, no more
11865  * fabric iocb will be issued out of the HBA.
11866  **/
11867 static void
11868 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
11869 {
11870 	int blocked;
11871 
11872 	blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11873 	/* Start a timer to unblock fabric iocbs after 100ms */
11874 	if (!blocked)
11875 		mod_timer(&phba->fabric_block_timer,
11876 			  jiffies + msecs_to_jiffies(100));
11877 
11878 	return;
11879 }
11880 
11881 /**
11882  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
11883  * @phba: pointer to lpfc hba data structure.
11884  * @cmdiocb: pointer to lpfc command iocb data structure.
11885  * @rspiocb: pointer to lpfc response iocb data structure.
11886  *
11887  * This routine is the callback function that is put to the fabric iocb's
11888  * callback function pointer (iocb->cmd_cmpl). The original iocb's callback
11889  * function pointer has been stored in iocb->fabric_cmd_cmpl. This callback
11890  * function first restores and invokes the original iocb's callback function
11891  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
11892  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
11893  **/
11894 static void
11895 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11896 		      struct lpfc_iocbq *rspiocb)
11897 {
11898 	struct ls_rjt stat;
11899 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
11900 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
11901 
11902 	WARN_ON((cmdiocb->cmd_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
11903 
11904 	switch (ulp_status) {
11905 		case IOSTAT_NPORT_RJT:
11906 		case IOSTAT_FABRIC_RJT:
11907 			if (ulp_word4 & RJT_UNAVAIL_TEMP)
11908 				lpfc_block_fabric_iocbs(phba);
11909 			break;
11910 
11911 		case IOSTAT_NPORT_BSY:
11912 		case IOSTAT_FABRIC_BSY:
11913 			lpfc_block_fabric_iocbs(phba);
11914 			break;
11915 
11916 		case IOSTAT_LS_RJT:
11917 			stat.un.ls_rjt_error_be =
11918 				cpu_to_be32(ulp_word4);
11919 			if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
11920 				(stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
11921 				lpfc_block_fabric_iocbs(phba);
11922 			break;
11923 	}
11924 
11925 	BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
11926 
11927 	cmdiocb->cmd_cmpl = cmdiocb->fabric_cmd_cmpl;
11928 	cmdiocb->fabric_cmd_cmpl = NULL;
11929 	cmdiocb->cmd_flag &= ~LPFC_IO_FABRIC;
11930 	cmdiocb->cmd_cmpl(phba, cmdiocb, rspiocb);
11931 
11932 	atomic_dec(&phba->fabric_iocb_count);
11933 	if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
11934 		/* Post any pending iocbs to HBA */
11935 		lpfc_resume_fabric_iocbs(phba);
11936 	}
11937 }
11938 
11939 /**
11940  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
11941  * @phba: pointer to lpfc hba data structure.
11942  * @iocb: pointer to lpfc command iocb data structure.
11943  *
11944  * This routine is used as the top-level API for issuing a fabric iocb command
11945  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
11946  * function makes sure that only one fabric bound iocb will be outstanding at
11947  * any given time. As such, this function will first check to see whether there
11948  * is already an outstanding fabric iocb on the wire. If so, it will put the
11949  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
11950  * issued later. Otherwise, it will issue the iocb on the wire and update the
11951  * fabric iocb count it indicate that there is one fabric iocb on the wire.
11952  *
11953  * Note, this implementation has a potential sending out fabric IOCBs out of
11954  * order. The problem is caused by the construction of the "ready" boolen does
11955  * not include the condition that the internal fabric IOCB list is empty. As
11956  * such, it is possible a fabric IOCB issued by this routine might be "jump"
11957  * ahead of the fabric IOCBs in the internal list.
11958  *
11959  * Return code
11960  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
11961  *   IOCB_ERROR - failed to issue fabric iocb
11962  **/
11963 static int
11964 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
11965 {
11966 	unsigned long iflags;
11967 	int ready;
11968 	int ret;
11969 
11970 	BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
11971 
11972 	spin_lock_irqsave(&phba->hbalock, iflags);
11973 	ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
11974 		!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
11975 
11976 	if (ready)
11977 		/* Increment fabric iocb count to hold the position */
11978 		atomic_inc(&phba->fabric_iocb_count);
11979 	spin_unlock_irqrestore(&phba->hbalock, iflags);
11980 	if (ready) {
11981 		iocb->fabric_cmd_cmpl = iocb->cmd_cmpl;
11982 		iocb->cmd_cmpl = lpfc_cmpl_fabric_iocb;
11983 		iocb->cmd_flag |= LPFC_IO_FABRIC;
11984 
11985 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
11986 				      "Fabric sched2:   ste:x%x",
11987 				      iocb->vport->port_state, 0, 0);
11988 
11989 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
11990 
11991 		if (ret == IOCB_ERROR) {
11992 			iocb->cmd_cmpl = iocb->fabric_cmd_cmpl;
11993 			iocb->fabric_cmd_cmpl = NULL;
11994 			iocb->cmd_flag &= ~LPFC_IO_FABRIC;
11995 			atomic_dec(&phba->fabric_iocb_count);
11996 		}
11997 	} else {
11998 		spin_lock_irqsave(&phba->hbalock, iflags);
11999 		list_add_tail(&iocb->list, &phba->fabric_iocb_list);
12000 		spin_unlock_irqrestore(&phba->hbalock, iflags);
12001 		ret = IOCB_SUCCESS;
12002 	}
12003 	return ret;
12004 }
12005 
12006 /**
12007  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
12008  * @vport: pointer to a virtual N_Port data structure.
12009  *
12010  * This routine aborts all the IOCBs associated with a @vport from the
12011  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
12012  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
12013  * list, removes each IOCB associated with the @vport off the list, set the
12014  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
12015  * associated with the IOCB.
12016  **/
12017 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
12018 {
12019 	LIST_HEAD(completions);
12020 	struct lpfc_hba  *phba = vport->phba;
12021 	struct lpfc_iocbq *tmp_iocb, *piocb;
12022 
12023 	spin_lock_irq(&phba->hbalock);
12024 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
12025 				 list) {
12026 
12027 		if (piocb->vport != vport)
12028 			continue;
12029 
12030 		list_move_tail(&piocb->list, &completions);
12031 	}
12032 	spin_unlock_irq(&phba->hbalock);
12033 
12034 	/* Cancel all the IOCBs from the completions list */
12035 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12036 			      IOERR_SLI_ABORTED);
12037 }
12038 
12039 /**
12040  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
12041  * @ndlp: pointer to a node-list data structure.
12042  *
12043  * This routine aborts all the IOCBs associated with an @ndlp from the
12044  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
12045  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
12046  * list, removes each IOCB associated with the @ndlp off the list, set the
12047  * status field to IOSTAT_LOCAL_REJECT, and invokes the callback function
12048  * associated with the IOCB.
12049  **/
12050 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
12051 {
12052 	LIST_HEAD(completions);
12053 	struct lpfc_hba  *phba = ndlp->phba;
12054 	struct lpfc_iocbq *tmp_iocb, *piocb;
12055 	struct lpfc_sli_ring *pring;
12056 
12057 	pring = lpfc_phba_elsring(phba);
12058 
12059 	if (unlikely(!pring))
12060 		return;
12061 
12062 	spin_lock_irq(&phba->hbalock);
12063 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
12064 				 list) {
12065 		if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
12066 
12067 			list_move_tail(&piocb->list, &completions);
12068 		}
12069 	}
12070 	spin_unlock_irq(&phba->hbalock);
12071 
12072 	/* Cancel all the IOCBs from the completions list */
12073 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12074 			      IOERR_SLI_ABORTED);
12075 }
12076 
12077 /**
12078  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
12079  * @phba: pointer to lpfc hba data structure.
12080  *
12081  * This routine aborts all the IOCBs currently on the driver internal
12082  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
12083  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
12084  * list, removes IOCBs off the list, set the status field to
12085  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
12086  * the IOCB.
12087  **/
12088 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
12089 {
12090 	LIST_HEAD(completions);
12091 
12092 	spin_lock_irq(&phba->hbalock);
12093 	list_splice_init(&phba->fabric_iocb_list, &completions);
12094 	spin_unlock_irq(&phba->hbalock);
12095 
12096 	/* Cancel all the IOCBs from the completions list */
12097 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12098 			      IOERR_SLI_ABORTED);
12099 }
12100 
12101 /**
12102  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
12103  * @vport: pointer to lpfc vport data structure.
12104  *
12105  * This routine is invoked by the vport cleanup for deletions and the cleanup
12106  * for an ndlp on removal.
12107  **/
12108 void
12109 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
12110 {
12111 	struct lpfc_hba *phba = vport->phba;
12112 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
12113 	struct lpfc_nodelist *ndlp = NULL;
12114 	unsigned long iflag = 0;
12115 
12116 	spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
12117 	list_for_each_entry_safe(sglq_entry, sglq_next,
12118 			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
12119 		if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport) {
12120 			lpfc_nlp_put(sglq_entry->ndlp);
12121 			ndlp = sglq_entry->ndlp;
12122 			sglq_entry->ndlp = NULL;
12123 
12124 			/* If the xri on the abts_els_sgl list is for the Fport
12125 			 * node and the vport is unloading, the xri aborted wcqe
12126 			 * likely isn't coming back.  Just release the sgl.
12127 			 */
12128 			if (test_bit(FC_UNLOADING, &vport->load_flag) &&
12129 			    ndlp->nlp_DID == Fabric_DID) {
12130 				list_del(&sglq_entry->list);
12131 				sglq_entry->state = SGL_FREED;
12132 				list_add_tail(&sglq_entry->list,
12133 					&phba->sli4_hba.lpfc_els_sgl_list);
12134 			}
12135 		}
12136 	}
12137 	spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
12138 	return;
12139 }
12140 
12141 /**
12142  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
12143  * @phba: pointer to lpfc hba data structure.
12144  * @axri: pointer to the els xri abort wcqe structure.
12145  *
12146  * This routine is invoked by the worker thread to process a SLI4 slow-path
12147  * ELS aborted xri.
12148  **/
12149 void
12150 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
12151 			  struct sli4_wcqe_xri_aborted *axri)
12152 {
12153 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
12154 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
12155 	uint16_t lxri = 0;
12156 
12157 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
12158 	unsigned long iflag = 0;
12159 	struct lpfc_nodelist *ndlp;
12160 	struct lpfc_sli_ring *pring;
12161 
12162 	pring = lpfc_phba_elsring(phba);
12163 
12164 	spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock, iflag);
12165 	list_for_each_entry_safe(sglq_entry, sglq_next,
12166 			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
12167 		if (sglq_entry->sli4_xritag == xri) {
12168 			list_del(&sglq_entry->list);
12169 			ndlp = sglq_entry->ndlp;
12170 			sglq_entry->ndlp = NULL;
12171 			list_add_tail(&sglq_entry->list,
12172 				&phba->sli4_hba.lpfc_els_sgl_list);
12173 			sglq_entry->state = SGL_FREED;
12174 			spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock,
12175 					       iflag);
12176 			lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI |
12177 					LOG_DISCOVERY | LOG_NODE,
12178 					"0732 ELS XRI ABORT on Node: ndlp=x%px "
12179 					"xri=x%x\n",
12180 					ndlp, xri);
12181 			if (ndlp) {
12182 				lpfc_set_rrq_active(phba, ndlp,
12183 					sglq_entry->sli4_lxritag,
12184 					rxid, 1);
12185 				lpfc_nlp_put(ndlp);
12186 			}
12187 
12188 			/* Check if TXQ queue needs to be serviced */
12189 			if (pring && !list_empty(&pring->txq))
12190 				lpfc_worker_wake_up(phba);
12191 			return;
12192 		}
12193 	}
12194 	spin_unlock_irqrestore(&phba->sli4_hba.sgl_list_lock, iflag);
12195 	lxri = lpfc_sli4_xri_inrange(phba, xri);
12196 	if (lxri == NO_XRI)
12197 		return;
12198 
12199 	spin_lock_irqsave(&phba->hbalock, iflag);
12200 	sglq_entry = __lpfc_get_active_sglq(phba, lxri);
12201 	if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
12202 		spin_unlock_irqrestore(&phba->hbalock, iflag);
12203 		return;
12204 	}
12205 	sglq_entry->state = SGL_XRI_ABORTED;
12206 	spin_unlock_irqrestore(&phba->hbalock, iflag);
12207 	return;
12208 }
12209 
12210 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
12211  * @vport: pointer to virtual port object.
12212  * @ndlp: nodelist pointer for the impacted node.
12213  *
12214  * The driver calls this routine in response to an SLI4 XRI ABORT CQE
12215  * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
12216  * the driver is required to send a LOGO to the remote node before it
12217  * attempts to recover its login to the remote node.
12218  */
12219 void
12220 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
12221 			   struct lpfc_nodelist *ndlp)
12222 {
12223 	struct Scsi_Host *shost;
12224 	struct lpfc_hba *phba;
12225 	unsigned long flags = 0;
12226 
12227 	shost = lpfc_shost_from_vport(vport);
12228 	phba = vport->phba;
12229 	if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
12230 		lpfc_printf_log(phba, KERN_INFO,
12231 				LOG_SLI, "3093 No rport recovery needed. "
12232 				"rport in state 0x%x\n", ndlp->nlp_state);
12233 		return;
12234 	}
12235 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12236 			"3094 Start rport recovery on shost id 0x%x "
12237 			"fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
12238 			"flag 0x%lx\n",
12239 			shost->host_no, ndlp->nlp_DID,
12240 			vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
12241 			ndlp->nlp_flag);
12242 	/*
12243 	 * The rport is not responding.  Remove the FCP-2 flag to prevent
12244 	 * an ADISC in the follow-up recovery code.
12245 	 */
12246 	spin_lock_irqsave(&ndlp->lock, flags);
12247 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
12248 	spin_unlock_irqrestore(&ndlp->lock, flags);
12249 	set_bit(NLP_ISSUE_LOGO, &ndlp->nlp_flag);
12250 	lpfc_unreg_rpi(vport, ndlp);
12251 }
12252 
12253 static void lpfc_init_cs_ctl_bitmap(struct lpfc_vport *vport)
12254 {
12255 	bitmap_zero(vport->vmid_priority_range, LPFC_VMID_MAX_PRIORITY_RANGE);
12256 }
12257 
12258 static void
12259 lpfc_vmid_set_cs_ctl_range(struct lpfc_vport *vport, u32 min, u32 max)
12260 {
12261 	u32 i;
12262 
12263 	if ((min > max) || (max > LPFC_VMID_MAX_PRIORITY_RANGE))
12264 		return;
12265 
12266 	for (i = min; i <= max; i++)
12267 		set_bit(i, vport->vmid_priority_range);
12268 }
12269 
12270 static void lpfc_vmid_put_cs_ctl(struct lpfc_vport *vport, u32 ctcl_vmid)
12271 {
12272 	set_bit(ctcl_vmid, vport->vmid_priority_range);
12273 }
12274 
12275 u32 lpfc_vmid_get_cs_ctl(struct lpfc_vport *vport)
12276 {
12277 	u32 i;
12278 
12279 	i = find_first_bit(vport->vmid_priority_range,
12280 			   LPFC_VMID_MAX_PRIORITY_RANGE);
12281 
12282 	if (i == LPFC_VMID_MAX_PRIORITY_RANGE)
12283 		return 0;
12284 
12285 	clear_bit(i, vport->vmid_priority_range);
12286 	return i;
12287 }
12288 
12289 #define MAX_PRIORITY_DESC	255
12290 
12291 static void
12292 lpfc_cmpl_els_qfpa(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12293 		   struct lpfc_iocbq *rspiocb)
12294 {
12295 	struct lpfc_vport *vport = cmdiocb->vport;
12296 	struct priority_range_desc *desc;
12297 	struct lpfc_dmabuf *prsp = NULL;
12298 	struct lpfc_vmid_priority_range *vmid_range = NULL;
12299 	u32 *data;
12300 	struct lpfc_dmabuf *dmabuf = cmdiocb->cmd_dmabuf;
12301 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12302 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12303 	u8 *pcmd, max_desc;
12304 	u32 len, i;
12305 	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
12306 
12307 	prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
12308 	if (!prsp)
12309 		goto out;
12310 
12311 	pcmd = prsp->virt;
12312 	data = (u32 *)pcmd;
12313 	if (data[0] == ELS_CMD_LS_RJT) {
12314 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12315 				 "3277 QFPA LS_RJT x%x  x%x\n",
12316 				 data[0], data[1]);
12317 		goto out;
12318 	}
12319 	if (ulp_status) {
12320 		lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
12321 				 "6529 QFPA failed with status x%x  x%x\n",
12322 				 ulp_status, ulp_word4);
12323 		goto out;
12324 	}
12325 
12326 	if (!vport->qfpa_res) {
12327 		max_desc = FCELSSIZE / sizeof(*vport->qfpa_res);
12328 		vport->qfpa_res = kcalloc(max_desc, sizeof(*vport->qfpa_res),
12329 					  GFP_KERNEL);
12330 		if (!vport->qfpa_res)
12331 			goto out;
12332 	}
12333 
12334 	len = *((u32 *)(pcmd + 4));
12335 	len = be32_to_cpu(len);
12336 	memcpy(vport->qfpa_res, pcmd, len + 8);
12337 	len = len / LPFC_PRIORITY_RANGE_DESC_SIZE;
12338 
12339 	desc = (struct priority_range_desc *)(pcmd + 8);
12340 	vmid_range = vport->vmid_priority.vmid_range;
12341 	if (!vmid_range) {
12342 		vmid_range = kcalloc(MAX_PRIORITY_DESC, sizeof(*vmid_range),
12343 				     GFP_KERNEL);
12344 		if (!vmid_range) {
12345 			kfree(vport->qfpa_res);
12346 			goto out;
12347 		}
12348 		vport->vmid_priority.vmid_range = vmid_range;
12349 	}
12350 	vport->vmid_priority.num_descriptors = len;
12351 
12352 	for (i = 0; i < len; i++, vmid_range++, desc++) {
12353 		lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
12354 				 "6539 vmid values low=%d, high=%d, qos=%d, "
12355 				 "local ve id=%d\n", desc->lo_range,
12356 				 desc->hi_range, desc->qos_priority,
12357 				 desc->local_ve_id);
12358 
12359 		vmid_range->low = desc->lo_range << 1;
12360 		if (desc->local_ve_id == QFPA_ODD_ONLY)
12361 			vmid_range->low++;
12362 		if (desc->qos_priority)
12363 			vport->vmid_flag |= LPFC_VMID_QOS_ENABLED;
12364 		vmid_range->qos = desc->qos_priority;
12365 
12366 		vmid_range->high = desc->hi_range << 1;
12367 		if ((desc->local_ve_id == QFPA_ODD_ONLY) ||
12368 		    (desc->local_ve_id == QFPA_EVEN_ODD))
12369 			vmid_range->high++;
12370 	}
12371 	lpfc_init_cs_ctl_bitmap(vport);
12372 	for (i = 0; i < vport->vmid_priority.num_descriptors; i++) {
12373 		lpfc_vmid_set_cs_ctl_range(vport,
12374 				vport->vmid_priority.vmid_range[i].low,
12375 				vport->vmid_priority.vmid_range[i].high);
12376 	}
12377 
12378 	vport->vmid_flag |= LPFC_VMID_QFPA_CMPL;
12379  out:
12380 	lpfc_els_free_iocb(phba, cmdiocb);
12381 	lpfc_nlp_put(ndlp);
12382 }
12383 
12384 int lpfc_issue_els_qfpa(struct lpfc_vport *vport)
12385 {
12386 	struct lpfc_hba *phba = vport->phba;
12387 	struct lpfc_nodelist *ndlp;
12388 	struct lpfc_iocbq *elsiocb;
12389 	u8 *pcmd;
12390 	int ret;
12391 
12392 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
12393 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12394 		return -ENXIO;
12395 
12396 	elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_QFPA_SIZE, 2, ndlp,
12397 				     ndlp->nlp_DID, ELS_CMD_QFPA);
12398 	if (!elsiocb)
12399 		return -ENOMEM;
12400 
12401 	pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
12402 
12403 	*((u32 *)(pcmd)) = ELS_CMD_QFPA;
12404 	pcmd += 4;
12405 
12406 	elsiocb->cmd_cmpl = lpfc_cmpl_els_qfpa;
12407 
12408 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
12409 	if (!elsiocb->ndlp) {
12410 		lpfc_els_free_iocb(vport->phba, elsiocb);
12411 		return -ENXIO;
12412 	}
12413 
12414 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 2);
12415 	if (ret != IOCB_SUCCESS) {
12416 		lpfc_els_free_iocb(phba, elsiocb);
12417 		lpfc_nlp_put(ndlp);
12418 		return -EIO;
12419 	}
12420 	vport->vmid_flag &= ~LPFC_VMID_QOS_ENABLED;
12421 	return 0;
12422 }
12423 
12424 int
12425 lpfc_vmid_uvem(struct lpfc_vport *vport,
12426 	       struct lpfc_vmid *vmid, bool instantiated)
12427 {
12428 	struct lpfc_vem_id_desc *vem_id_desc;
12429 	struct lpfc_nodelist *ndlp;
12430 	struct lpfc_iocbq *elsiocb;
12431 	struct instantiated_ve_desc *inst_desc;
12432 	struct lpfc_vmid_context *vmid_context;
12433 	u8 *pcmd;
12434 	u32 *len;
12435 	int ret = 0;
12436 
12437 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
12438 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12439 		return -ENXIO;
12440 
12441 	vmid_context = kmalloc(sizeof(*vmid_context), GFP_KERNEL);
12442 	if (!vmid_context)
12443 		return -ENOMEM;
12444 	elsiocb = lpfc_prep_els_iocb(vport, 1, LPFC_UVEM_SIZE, 2,
12445 				     ndlp, Fabric_DID, ELS_CMD_UVEM);
12446 	if (!elsiocb)
12447 		goto out;
12448 
12449 	lpfc_printf_vlog(vport, KERN_DEBUG, LOG_ELS,
12450 			 "3427 Host vmid %s %d\n",
12451 			 vmid->host_vmid, instantiated);
12452 	vmid_context->vmp = vmid;
12453 	vmid_context->nlp = ndlp;
12454 	vmid_context->instantiated = instantiated;
12455 	elsiocb->vmid_tag.vmid_context = vmid_context;
12456 	pcmd = (u8 *)elsiocb->cmd_dmabuf->virt;
12457 
12458 	if (!memchr_inv(vport->lpfc_vmid_host_uuid, 0,
12459 			sizeof(vport->lpfc_vmid_host_uuid)))
12460 		memcpy(vport->lpfc_vmid_host_uuid, vmid->host_vmid,
12461 		       sizeof(vport->lpfc_vmid_host_uuid));
12462 
12463 	*((u32 *)(pcmd)) = ELS_CMD_UVEM;
12464 	len = (u32 *)(pcmd + 4);
12465 	*len = cpu_to_be32(LPFC_UVEM_SIZE - 8);
12466 
12467 	vem_id_desc = (struct lpfc_vem_id_desc *)(pcmd + 8);
12468 	vem_id_desc->tag = be32_to_cpu(VEM_ID_DESC_TAG);
12469 	vem_id_desc->length = be32_to_cpu(LPFC_UVEM_VEM_ID_DESC_SIZE);
12470 	memcpy(vem_id_desc->vem_id, vport->lpfc_vmid_host_uuid,
12471 	       sizeof(vem_id_desc->vem_id));
12472 
12473 	inst_desc = (struct instantiated_ve_desc *)(pcmd + 32);
12474 	inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
12475 	inst_desc->length = be32_to_cpu(LPFC_UVEM_VE_MAP_DESC_SIZE);
12476 	memcpy(inst_desc->global_vem_id, vmid->host_vmid,
12477 	       sizeof(inst_desc->global_vem_id));
12478 
12479 	bf_set(lpfc_instantiated_nport_id, inst_desc, vport->fc_myDID);
12480 	bf_set(lpfc_instantiated_local_id, inst_desc,
12481 	       vmid->un.cs_ctl_vmid);
12482 	if (instantiated) {
12483 		inst_desc->tag = be32_to_cpu(INSTANTIATED_VE_DESC_TAG);
12484 	} else {
12485 		inst_desc->tag = be32_to_cpu(DEINSTANTIATED_VE_DESC_TAG);
12486 		lpfc_vmid_put_cs_ctl(vport, vmid->un.cs_ctl_vmid);
12487 	}
12488 	inst_desc->word6 = cpu_to_be32(inst_desc->word6);
12489 
12490 	elsiocb->cmd_cmpl = lpfc_cmpl_els_uvem;
12491 
12492 	elsiocb->ndlp = lpfc_nlp_get(ndlp);
12493 	if (!elsiocb->ndlp) {
12494 		lpfc_els_free_iocb(vport->phba, elsiocb);
12495 		goto out;
12496 	}
12497 
12498 	ret = lpfc_sli_issue_iocb(vport->phba, LPFC_ELS_RING, elsiocb, 0);
12499 	if (ret != IOCB_SUCCESS) {
12500 		lpfc_els_free_iocb(vport->phba, elsiocb);
12501 		lpfc_nlp_put(ndlp);
12502 		goto out;
12503 	}
12504 
12505 	return 0;
12506  out:
12507 	kfree(vmid_context);
12508 	return -EIO;
12509 }
12510 
12511 static void
12512 lpfc_cmpl_els_uvem(struct lpfc_hba *phba, struct lpfc_iocbq *icmdiocb,
12513 		   struct lpfc_iocbq *rspiocb)
12514 {
12515 	struct lpfc_vport *vport = icmdiocb->vport;
12516 	struct lpfc_dmabuf *prsp = NULL;
12517 	struct lpfc_vmid_context *vmid_context =
12518 	    icmdiocb->vmid_tag.vmid_context;
12519 	struct lpfc_nodelist *ndlp = icmdiocb->ndlp;
12520 	u8 *pcmd;
12521 	u32 *data;
12522 	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12523 	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12524 	struct lpfc_dmabuf *dmabuf = icmdiocb->cmd_dmabuf;
12525 	struct lpfc_vmid *vmid;
12526 
12527 	vmid = vmid_context->vmp;
12528 	if (!ndlp || ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
12529 		ndlp = NULL;
12530 
12531 	prsp = list_get_first(&dmabuf->list, struct lpfc_dmabuf, list);
12532 	if (!prsp)
12533 		goto out;
12534 	pcmd = prsp->virt;
12535 	data = (u32 *)pcmd;
12536 	if (data[0] == ELS_CMD_LS_RJT) {
12537 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12538 				 "4532 UVEM LS_RJT %x %x\n", data[0], data[1]);
12539 		goto out;
12540 	}
12541 	if (ulp_status) {
12542 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
12543 				 "4533 UVEM error status %x: %x\n",
12544 				 ulp_status, ulp_word4);
12545 		goto out;
12546 	}
12547 	spin_lock(&phba->hbalock);
12548 	/* Set IN USE flag */
12549 	vport->vmid_flag |= LPFC_VMID_IN_USE;
12550 	phba->pport->vmid_flag |= LPFC_VMID_IN_USE;
12551 	spin_unlock(&phba->hbalock);
12552 
12553 	if (vmid_context->instantiated) {
12554 		write_lock(&vport->vmid_lock);
12555 		vmid->flag |= LPFC_VMID_REGISTERED;
12556 		vmid->flag &= ~LPFC_VMID_REQ_REGISTER;
12557 		write_unlock(&vport->vmid_lock);
12558 	}
12559 
12560  out:
12561 	kfree(vmid_context);
12562 	lpfc_els_free_iocb(phba, icmdiocb);
12563 	lpfc_nlp_put(ndlp);
12564 }
12565