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