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