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