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