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