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