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