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