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