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