xref: /linux/drivers/scsi/lpfc/lpfc_els.c (revision f3449bf31d352f70c80a7993c272a7854ae98086)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 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 #include "lpfc_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_sli4.h"
36 #include "lpfc_nl.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43 #include "lpfc_debugfs.h"
44 
45 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
46 			  struct lpfc_iocbq *);
47 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
48 			struct lpfc_iocbq *);
49 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
50 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
51 				struct lpfc_nodelist *ndlp, uint8_t retry);
52 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
53 				  struct lpfc_iocbq *iocb);
54 
55 static int lpfc_max_els_tries = 3;
56 
57 /**
58  * lpfc_els_chk_latt - Check host link attention event for a vport
59  * @vport: pointer to a host virtual N_Port data structure.
60  *
61  * This routine checks whether there is an outstanding host link
62  * attention event during the discovery process with the @vport. It is done
63  * by reading the HBA's Host Attention (HA) register. If there is any host
64  * link attention events during this @vport's discovery process, the @vport
65  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
66  * be issued if the link state is not already in host link cleared state,
67  * and a return code shall indicate whether the host link attention event
68  * had happened.
69  *
70  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
71  * state in LPFC_VPORT_READY, the request for checking host link attention
72  * event will be ignored and a return code shall indicate no host link
73  * attention event had happened.
74  *
75  * Return codes
76  *   0 - no host link attention event happened
77  *   1 - host link attention event happened
78  **/
79 int
80 lpfc_els_chk_latt(struct lpfc_vport *vport)
81 {
82 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
83 	struct lpfc_hba  *phba = vport->phba;
84 	uint32_t ha_copy;
85 
86 	if (vport->port_state >= LPFC_VPORT_READY ||
87 	    phba->link_state == LPFC_LINK_DOWN ||
88 	    phba->sli_rev > LPFC_SLI_REV3)
89 		return 0;
90 
91 	/* Read the HBA Host Attention Register */
92 	ha_copy = readl(phba->HAregaddr);
93 
94 	if (!(ha_copy & HA_LATT))
95 		return 0;
96 
97 	/* Pending Link Event during Discovery */
98 	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
99 			 "0237 Pending Link Event during "
100 			 "Discovery: State x%x\n",
101 			 phba->pport->port_state);
102 
103 	/* CLEAR_LA should re-enable link attention events and
104 	 * we should then imediately take a LATT event. The
105 	 * LATT processing should call lpfc_linkdown() which
106 	 * will cleanup any left over in-progress discovery
107 	 * events.
108 	 */
109 	spin_lock_irq(shost->host_lock);
110 	vport->fc_flag |= FC_ABORT_DISCOVERY;
111 	spin_unlock_irq(shost->host_lock);
112 
113 	if (phba->link_state != LPFC_CLEAR_LA)
114 		lpfc_issue_clear_la(phba, vport);
115 
116 	return 1;
117 }
118 
119 /**
120  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
121  * @vport: pointer to a host virtual N_Port data structure.
122  * @expectRsp: flag indicating whether response is expected.
123  * @cmdSize: size of the ELS command.
124  * @retry: number of retries to the command IOCB when it fails.
125  * @ndlp: pointer to a node-list data structure.
126  * @did: destination identifier.
127  * @elscmd: the ELS command code.
128  *
129  * This routine is used for allocating a lpfc-IOCB data structure from
130  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
131  * passed into the routine for discovery state machine to issue an Extended
132  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
133  * and preparation routine that is used by all the discovery state machine
134  * routines and the ELS command-specific fields will be later set up by
135  * the individual discovery machine routines after calling this routine
136  * allocating and preparing a generic IOCB data structure. It fills in the
137  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
138  * payload and response payload (if expected). The reference count on the
139  * ndlp is incremented by 1 and the reference to the ndlp is put into
140  * context1 of the IOCB data structure for this IOCB to hold the ndlp
141  * reference for the command's callback function to access later.
142  *
143  * Return code
144  *   Pointer to the newly allocated/prepared els iocb data structure
145  *   NULL - when els iocb data structure allocation/preparation failed
146  **/
147 struct lpfc_iocbq *
148 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
149 		   uint16_t cmdSize, uint8_t retry,
150 		   struct lpfc_nodelist *ndlp, uint32_t did,
151 		   uint32_t elscmd)
152 {
153 	struct lpfc_hba  *phba = vport->phba;
154 	struct lpfc_iocbq *elsiocb;
155 	struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
156 	struct ulp_bde64 *bpl;
157 	IOCB_t *icmd;
158 
159 
160 	if (!lpfc_is_link_up(phba))
161 		return NULL;
162 
163 	/* Allocate buffer for  command iocb */
164 	elsiocb = lpfc_sli_get_iocbq(phba);
165 
166 	if (elsiocb == NULL)
167 		return NULL;
168 
169 	/*
170 	 * If this command is for fabric controller and HBA running
171 	 * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
172 	 */
173 	if ((did == Fabric_DID) &&
174 		(phba->hba_flag & HBA_FIP_SUPPORT) &&
175 		((elscmd == ELS_CMD_FLOGI) ||
176 		 (elscmd == ELS_CMD_FDISC) ||
177 		 (elscmd == ELS_CMD_LOGO)))
178 		switch (elscmd) {
179 		case ELS_CMD_FLOGI:
180 		elsiocb->iocb_flag |=
181 			((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
182 					& LPFC_FIP_ELS_ID_MASK);
183 		break;
184 		case ELS_CMD_FDISC:
185 		elsiocb->iocb_flag |=
186 			((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
187 					& LPFC_FIP_ELS_ID_MASK);
188 		break;
189 		case ELS_CMD_LOGO:
190 		elsiocb->iocb_flag |=
191 			((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
192 					& LPFC_FIP_ELS_ID_MASK);
193 		break;
194 		}
195 	else
196 		elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
197 
198 	icmd = &elsiocb->iocb;
199 
200 	/* fill in BDEs for command */
201 	/* Allocate buffer for command payload */
202 	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
203 	if (pcmd)
204 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
205 	if (!pcmd || !pcmd->virt)
206 		goto els_iocb_free_pcmb_exit;
207 
208 	INIT_LIST_HEAD(&pcmd->list);
209 
210 	/* Allocate buffer for response payload */
211 	if (expectRsp) {
212 		prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
213 		if (prsp)
214 			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
215 						     &prsp->phys);
216 		if (!prsp || !prsp->virt)
217 			goto els_iocb_free_prsp_exit;
218 		INIT_LIST_HEAD(&prsp->list);
219 	} else
220 		prsp = NULL;
221 
222 	/* Allocate buffer for Buffer ptr list */
223 	pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
224 	if (pbuflist)
225 		pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
226 						 &pbuflist->phys);
227 	if (!pbuflist || !pbuflist->virt)
228 		goto els_iocb_free_pbuf_exit;
229 
230 	INIT_LIST_HEAD(&pbuflist->list);
231 
232 	icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
233 	icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
234 	icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
235 	icmd->un.elsreq64.remoteID = did;	/* DID */
236 	if (expectRsp) {
237 		icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
238 		icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
239 		icmd->ulpTimeout = phba->fc_ratov * 2;
240 	} else {
241 		icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
242 		icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
243 	}
244 	icmd->ulpBdeCount = 1;
245 	icmd->ulpLe = 1;
246 	icmd->ulpClass = CLASS3;
247 
248 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
249 		icmd->un.elsreq64.myID = vport->fc_myDID;
250 
251 		/* For ELS_REQUEST64_CR, use the VPI by default */
252 		icmd->ulpContext = vport->vpi + phba->vpi_base;
253 		icmd->ulpCt_h = 0;
254 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
255 		if (elscmd == ELS_CMD_ECHO)
256 			icmd->ulpCt_l = 0; /* context = invalid RPI */
257 		else
258 			icmd->ulpCt_l = 1; /* context = VPI */
259 	}
260 
261 	bpl = (struct ulp_bde64 *) pbuflist->virt;
262 	bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
263 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
264 	bpl->tus.f.bdeSize = cmdSize;
265 	bpl->tus.f.bdeFlags = 0;
266 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
267 
268 	if (expectRsp) {
269 		bpl++;
270 		bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
271 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
272 		bpl->tus.f.bdeSize = FCELSSIZE;
273 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
274 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
275 	}
276 
277 	/* prevent preparing iocb with NULL ndlp reference */
278 	elsiocb->context1 = lpfc_nlp_get(ndlp);
279 	if (!elsiocb->context1)
280 		goto els_iocb_free_pbuf_exit;
281 	elsiocb->context2 = pcmd;
282 	elsiocb->context3 = pbuflist;
283 	elsiocb->retry = retry;
284 	elsiocb->vport = vport;
285 	elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
286 
287 	if (prsp) {
288 		list_add(&prsp->list, &pcmd->list);
289 	}
290 	if (expectRsp) {
291 		/* Xmit ELS command <elsCmd> to remote NPORT <did> */
292 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
293 				 "0116 Xmit ELS command x%x to remote "
294 				 "NPORT x%x I/O tag: x%x, port state: x%x\n",
295 				 elscmd, did, elsiocb->iotag,
296 				 vport->port_state);
297 	} else {
298 		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
299 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
300 				 "0117 Xmit ELS response x%x to remote "
301 				 "NPORT x%x I/O tag: x%x, size: x%x\n",
302 				 elscmd, ndlp->nlp_DID, elsiocb->iotag,
303 				 cmdSize);
304 	}
305 	return elsiocb;
306 
307 els_iocb_free_pbuf_exit:
308 	if (expectRsp)
309 		lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
310 	kfree(pbuflist);
311 
312 els_iocb_free_prsp_exit:
313 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
314 	kfree(prsp);
315 
316 els_iocb_free_pcmb_exit:
317 	kfree(pcmd);
318 	lpfc_sli_release_iocbq(phba, elsiocb);
319 	return NULL;
320 }
321 
322 /**
323  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
324  * @vport: pointer to a host virtual N_Port data structure.
325  *
326  * This routine issues a fabric registration login for a @vport. An
327  * active ndlp node with Fabric_DID must already exist for this @vport.
328  * The routine invokes two mailbox commands to carry out fabric registration
329  * login through the HBA firmware: the first mailbox command requests the
330  * HBA to perform link configuration for the @vport; and the second mailbox
331  * command requests the HBA to perform the actual fabric registration login
332  * with the @vport.
333  *
334  * Return code
335  *   0 - successfully issued fabric registration login for @vport
336  *   -ENXIO -- failed to issue fabric registration login for @vport
337  **/
338 int
339 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
340 {
341 	struct lpfc_hba  *phba = vport->phba;
342 	LPFC_MBOXQ_t *mbox;
343 	struct lpfc_dmabuf *mp;
344 	struct lpfc_nodelist *ndlp;
345 	struct serv_parm *sp;
346 	int rc;
347 	int err = 0;
348 
349 	sp = &phba->fc_fabparam;
350 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
351 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
352 		err = 1;
353 		goto fail;
354 	}
355 
356 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
357 	if (!mbox) {
358 		err = 2;
359 		goto fail;
360 	}
361 
362 	vport->port_state = LPFC_FABRIC_CFG_LINK;
363 	lpfc_config_link(phba, mbox);
364 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
365 	mbox->vport = vport;
366 
367 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
368 	if (rc == MBX_NOT_FINISHED) {
369 		err = 3;
370 		goto fail_free_mbox;
371 	}
372 
373 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
374 	if (!mbox) {
375 		err = 4;
376 		goto fail;
377 	}
378 	rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox, 0);
379 	if (rc) {
380 		err = 5;
381 		goto fail_free_mbox;
382 	}
383 
384 	mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
385 	mbox->vport = vport;
386 	/* increment the reference count on ndlp to hold reference
387 	 * for the callback routine.
388 	 */
389 	mbox->context2 = lpfc_nlp_get(ndlp);
390 
391 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
392 	if (rc == MBX_NOT_FINISHED) {
393 		err = 6;
394 		goto fail_issue_reg_login;
395 	}
396 
397 	return 0;
398 
399 fail_issue_reg_login:
400 	/* decrement the reference count on ndlp just incremented
401 	 * for the failed mbox command.
402 	 */
403 	lpfc_nlp_put(ndlp);
404 	mp = (struct lpfc_dmabuf *) mbox->context1;
405 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
406 	kfree(mp);
407 fail_free_mbox:
408 	mempool_free(mbox, phba->mbox_mem_pool);
409 
410 fail:
411 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
412 	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
413 		"0249 Cannot issue Register Fabric login: Err %d\n", err);
414 	return -ENXIO;
415 }
416 
417 /**
418  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
419  * @vport: pointer to a host virtual N_Port data structure.
420  *
421  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
422  * the @vport. This mailbox command is necessary for FCoE only.
423  *
424  * Return code
425  *   0 - successfully issued REG_VFI for @vport
426  *   A failure code otherwise.
427  **/
428 static int
429 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
430 {
431 	struct lpfc_hba  *phba = vport->phba;
432 	LPFC_MBOXQ_t *mboxq;
433 	struct lpfc_nodelist *ndlp;
434 	struct serv_parm *sp;
435 	struct lpfc_dmabuf *dmabuf;
436 	int rc = 0;
437 
438 	sp = &phba->fc_fabparam;
439 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
440 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
441 		rc = -ENODEV;
442 		goto fail;
443 	}
444 
445 	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
446 	if (!dmabuf) {
447 		rc = -ENOMEM;
448 		goto fail;
449 	}
450 	dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
451 	if (!dmabuf->virt) {
452 		rc = -ENOMEM;
453 		goto fail_free_dmabuf;
454 	}
455 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
456 	if (!mboxq) {
457 		rc = -ENOMEM;
458 		goto fail_free_coherent;
459 	}
460 	vport->port_state = LPFC_FABRIC_CFG_LINK;
461 	memcpy(dmabuf->virt, &phba->fc_fabparam, sizeof(vport->fc_sparam));
462 	lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
463 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
464 	mboxq->vport = vport;
465 	mboxq->context1 = dmabuf;
466 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
467 	if (rc == MBX_NOT_FINISHED) {
468 		rc = -ENXIO;
469 		goto fail_free_mbox;
470 	}
471 	return 0;
472 
473 fail_free_mbox:
474 	mempool_free(mboxq, phba->mbox_mem_pool);
475 fail_free_coherent:
476 	lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
477 fail_free_dmabuf:
478 	kfree(dmabuf);
479 fail:
480 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
481 	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
482 		"0289 Issue Register VFI failed: Err %d\n", rc);
483 	return rc;
484 }
485 
486 /**
487  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
488  * @vport: pointer to a host virtual N_Port data structure.
489  * @ndlp: pointer to a node-list data structure.
490  * @sp: pointer to service parameter data structure.
491  * @irsp: pointer to the IOCB within the lpfc response IOCB.
492  *
493  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
494  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
495  * port in a fabric topology. It properly sets up the parameters to the @ndlp
496  * from the IOCB response. It also check the newly assigned N_Port ID to the
497  * @vport against the previously assigned N_Port ID. If it is different from
498  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
499  * is invoked on all the remaining nodes with the @vport to unregister the
500  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
501  * is invoked to register login to the fabric.
502  *
503  * Return code
504  *   0 - Success (currently, always return 0)
505  **/
506 static int
507 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
508 			   struct serv_parm *sp, IOCB_t *irsp)
509 {
510 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
511 	struct lpfc_hba  *phba = vport->phba;
512 	struct lpfc_nodelist *np;
513 	struct lpfc_nodelist *next_np;
514 
515 	spin_lock_irq(shost->host_lock);
516 	vport->fc_flag |= FC_FABRIC;
517 	spin_unlock_irq(shost->host_lock);
518 
519 	phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
520 	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
521 		phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
522 
523 	phba->fc_edtovResol = sp->cmn.edtovResolution;
524 	phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
525 
526 	if (phba->fc_topology == TOPOLOGY_LOOP) {
527 		spin_lock_irq(shost->host_lock);
528 		vport->fc_flag |= FC_PUBLIC_LOOP;
529 		spin_unlock_irq(shost->host_lock);
530 	}
531 
532 	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
533 	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
534 	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
535 	ndlp->nlp_class_sup = 0;
536 	if (sp->cls1.classValid)
537 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
538 	if (sp->cls2.classValid)
539 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
540 	if (sp->cls3.classValid)
541 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
542 	if (sp->cls4.classValid)
543 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
544 	ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
545 				sp->cmn.bbRcvSizeLsb;
546 	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
547 
548 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
549 		if (sp->cmn.response_multiple_NPort) {
550 			lpfc_printf_vlog(vport, KERN_WARNING,
551 					 LOG_ELS | LOG_VPORT,
552 					 "1816 FLOGI NPIV supported, "
553 					 "response data 0x%x\n",
554 					 sp->cmn.response_multiple_NPort);
555 			phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
556 		} else {
557 			/* Because we asked f/w for NPIV it still expects us
558 			to call reg_vnpid atleast for the physcial host */
559 			lpfc_printf_vlog(vport, KERN_WARNING,
560 					 LOG_ELS | LOG_VPORT,
561 					 "1817 Fabric does not support NPIV "
562 					 "- configuring single port mode.\n");
563 			phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
564 		}
565 	}
566 
567 	if ((vport->fc_prevDID != vport->fc_myDID) &&
568 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
569 
570 		/* If our NportID changed, we need to ensure all
571 		 * remaining NPORTs get unreg_login'ed.
572 		 */
573 		list_for_each_entry_safe(np, next_np,
574 					&vport->fc_nodes, nlp_listp) {
575 			if (!NLP_CHK_NODE_ACT(np))
576 				continue;
577 			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
578 				   !(np->nlp_flag & NLP_NPR_ADISC))
579 				continue;
580 			spin_lock_irq(shost->host_lock);
581 			np->nlp_flag &= ~NLP_NPR_ADISC;
582 			spin_unlock_irq(shost->host_lock);
583 			lpfc_unreg_rpi(vport, np);
584 		}
585 		lpfc_cleanup_pending_mbox(vport);
586 
587 		if (phba->sli_rev == LPFC_SLI_REV4)
588 			lpfc_sli4_unreg_all_rpis(vport);
589 
590 		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
591 			lpfc_mbx_unreg_vpi(vport);
592 			spin_lock_irq(shost->host_lock);
593 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
594 			spin_unlock_irq(shost->host_lock);
595 		}
596 		/*
597 		 * If VPI is unreged, driver need to do INIT_VPI
598 		 * before re-registering
599 		 */
600 		if (phba->sli_rev == LPFC_SLI_REV4) {
601 			spin_lock_irq(shost->host_lock);
602 			vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
603 			spin_unlock_irq(shost->host_lock);
604 		}
605 	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
606 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
607 			/*
608 			 * Driver needs to re-reg VPI in order for f/w
609 			 * to update the MAC address.
610 			 */
611 			lpfc_register_new_vport(phba, vport, ndlp);
612 			return 0;
613 	}
614 
615 	if (phba->sli_rev < LPFC_SLI_REV4) {
616 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
617 		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
618 		    vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
619 			lpfc_register_new_vport(phba, vport, ndlp);
620 		else
621 			lpfc_issue_fabric_reglogin(vport);
622 	} else {
623 		ndlp->nlp_type |= NLP_FABRIC;
624 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
625 		if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
626 			(vport->vpi_state & LPFC_VPI_REGISTERED)) {
627 			lpfc_start_fdiscs(phba);
628 			lpfc_do_scr_ns_plogi(phba, vport);
629 		} else if (vport->fc_flag & FC_VFI_REGISTERED)
630 			lpfc_issue_init_vpi(vport);
631 		else
632 			lpfc_issue_reg_vfi(vport);
633 	}
634 	return 0;
635 }
636 /**
637  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
638  * @vport: pointer to a host virtual N_Port data structure.
639  * @ndlp: pointer to a node-list data structure.
640  * @sp: pointer to service parameter data structure.
641  *
642  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
643  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
644  * in a point-to-point topology. First, the @vport's N_Port Name is compared
645  * with the received N_Port Name: if the @vport's N_Port Name is greater than
646  * the received N_Port Name lexicographically, this node shall assign local
647  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
648  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
649  * this node shall just wait for the remote node to issue PLOGI and assign
650  * N_Port IDs.
651  *
652  * Return code
653  *   0 - Success
654  *   -ENXIO - Fail
655  **/
656 static int
657 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
658 			  struct serv_parm *sp)
659 {
660 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
661 	struct lpfc_hba  *phba = vport->phba;
662 	LPFC_MBOXQ_t *mbox;
663 	int rc;
664 
665 	spin_lock_irq(shost->host_lock);
666 	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
667 	spin_unlock_irq(shost->host_lock);
668 
669 	phba->fc_edtov = FF_DEF_EDTOV;
670 	phba->fc_ratov = FF_DEF_RATOV;
671 	rc = memcmp(&vport->fc_portname, &sp->portName,
672 		    sizeof(vport->fc_portname));
673 	if (rc >= 0) {
674 		/* This side will initiate the PLOGI */
675 		spin_lock_irq(shost->host_lock);
676 		vport->fc_flag |= FC_PT2PT_PLOGI;
677 		spin_unlock_irq(shost->host_lock);
678 
679 		/*
680 		 * N_Port ID cannot be 0, set our to LocalID the other
681 		 * side will be RemoteID.
682 		 */
683 
684 		/* not equal */
685 		if (rc)
686 			vport->fc_myDID = PT2PT_LocalID;
687 
688 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
689 		if (!mbox)
690 			goto fail;
691 
692 		lpfc_config_link(phba, mbox);
693 
694 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
695 		mbox->vport = vport;
696 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
697 		if (rc == MBX_NOT_FINISHED) {
698 			mempool_free(mbox, phba->mbox_mem_pool);
699 			goto fail;
700 		}
701 		/* Decrement ndlp reference count indicating that ndlp can be
702 		 * safely released when other references to it are done.
703 		 */
704 		lpfc_nlp_put(ndlp);
705 
706 		ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
707 		if (!ndlp) {
708 			/*
709 			 * Cannot find existing Fabric ndlp, so allocate a
710 			 * new one
711 			 */
712 			ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
713 			if (!ndlp)
714 				goto fail;
715 			lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
716 		} else if (!NLP_CHK_NODE_ACT(ndlp)) {
717 			ndlp = lpfc_enable_node(vport, ndlp,
718 						NLP_STE_UNUSED_NODE);
719 			if(!ndlp)
720 				goto fail;
721 		}
722 
723 		memcpy(&ndlp->nlp_portname, &sp->portName,
724 		       sizeof(struct lpfc_name));
725 		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
726 		       sizeof(struct lpfc_name));
727 		/* Set state will put ndlp onto node list if not already done */
728 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
729 		spin_lock_irq(shost->host_lock);
730 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
731 		spin_unlock_irq(shost->host_lock);
732 	} else
733 		/* This side will wait for the PLOGI, decrement ndlp reference
734 		 * count indicating that ndlp can be released when other
735 		 * references to it are done.
736 		 */
737 		lpfc_nlp_put(ndlp);
738 
739 	/* If we are pt2pt with another NPort, force NPIV off! */
740 	phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
741 
742 	spin_lock_irq(shost->host_lock);
743 	vport->fc_flag |= FC_PT2PT;
744 	spin_unlock_irq(shost->host_lock);
745 
746 	/* Start discovery - this should just do CLEAR_LA */
747 	lpfc_disc_start(vport);
748 	return 0;
749 fail:
750 	return -ENXIO;
751 }
752 
753 /**
754  * lpfc_cmpl_els_flogi - Completion callback function for flogi
755  * @phba: pointer to lpfc hba data structure.
756  * @cmdiocb: pointer to lpfc command iocb data structure.
757  * @rspiocb: pointer to lpfc response iocb data structure.
758  *
759  * This routine is the top-level completion callback function for issuing
760  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
761  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
762  * retry has been made (either immediately or delayed with lpfc_els_retry()
763  * returning 1), the command IOCB will be released and function returned.
764  * If the retry attempt has been given up (possibly reach the maximum
765  * number of retries), one additional decrement of ndlp reference shall be
766  * invoked before going out after releasing the command IOCB. This will
767  * actually release the remote node (Note, lpfc_els_free_iocb() will also
768  * invoke one decrement of ndlp reference count). If no error reported in
769  * the IOCB status, the command Port ID field is used to determine whether
770  * this is a point-to-point topology or a fabric topology: if the Port ID
771  * field is assigned, it is a fabric topology; otherwise, it is a
772  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
773  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
774  * specific topology completion conditions.
775  **/
776 static void
777 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
778 		    struct lpfc_iocbq *rspiocb)
779 {
780 	struct lpfc_vport *vport = cmdiocb->vport;
781 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
782 	IOCB_t *irsp = &rspiocb->iocb;
783 	struct lpfc_nodelist *ndlp = cmdiocb->context1;
784 	struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
785 	struct serv_parm *sp;
786 	uint16_t fcf_index;
787 	int rc;
788 
789 	/* Check to see if link went down during discovery */
790 	if (lpfc_els_chk_latt(vport)) {
791 		/* One additional decrement on node reference count to
792 		 * trigger the release of the node
793 		 */
794 		lpfc_nlp_put(ndlp);
795 		goto out;
796 	}
797 
798 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
799 		"FLOGI cmpl:      status:x%x/x%x state:x%x",
800 		irsp->ulpStatus, irsp->un.ulpWord[4],
801 		vport->port_state);
802 
803 	if (irsp->ulpStatus) {
804 		/*
805 		 * In case of FIP mode, perform roundrobin FCF failover
806 		 * due to new FCF discovery
807 		 */
808 		if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
809 		    (phba->fcf.fcf_flag & FCF_DISCOVERY) &&
810 		    (irsp->ulpStatus != IOSTAT_LOCAL_REJECT) &&
811 		    (irsp->un.ulpWord[4] != IOERR_SLI_ABORTED)) {
812 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
813 					"2611 FLOGI failed on FCF (x%x), "
814 					"status:x%x/x%x, tmo:x%x, perform "
815 					"roundrobin FCF failover\n",
816 					phba->fcf.current_rec.fcf_indx,
817 					irsp->ulpStatus, irsp->un.ulpWord[4],
818 					irsp->ulpTimeout);
819 			fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
820 			rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
821 			if (rc)
822 				goto out;
823 		}
824 
825 		/* FLOGI failure */
826 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
827 				"2858 FLOGI failure Status:x%x/x%x TMO:x%x\n",
828 				irsp->ulpStatus, irsp->un.ulpWord[4],
829 				irsp->ulpTimeout);
830 
831 		/* Check for retry */
832 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
833 			goto out;
834 
835 		/* FLOGI failed, so there is no fabric */
836 		spin_lock_irq(shost->host_lock);
837 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
838 		spin_unlock_irq(shost->host_lock);
839 
840 		/* If private loop, then allow max outstanding els to be
841 		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
842 		 * alpa map would take too long otherwise.
843 		 */
844 		if (phba->alpa_map[0] == 0) {
845 			vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
846 		}
847 
848 		/* FLOGI failure */
849 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
850 				 "0100 FLOGI failure Status:x%x/x%x TMO:x%x\n",
851 				 irsp->ulpStatus, irsp->un.ulpWord[4],
852 				 irsp->ulpTimeout);
853 		goto flogifail;
854 	}
855 	spin_lock_irq(shost->host_lock);
856 	vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
857 	vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
858 	spin_unlock_irq(shost->host_lock);
859 
860 	/*
861 	 * The FLogI succeeded.  Sync the data for the CPU before
862 	 * accessing it.
863 	 */
864 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
865 
866 	sp = prsp->virt + sizeof(uint32_t);
867 
868 	/* FLOGI completes successfully */
869 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
870 			 "0101 FLOGI completes successfully "
871 			 "Data: x%x x%x x%x x%x\n",
872 			 irsp->un.ulpWord[4], sp->cmn.e_d_tov,
873 			 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
874 
875 	if (vport->port_state == LPFC_FLOGI) {
876 		/*
877 		 * If Common Service Parameters indicate Nport
878 		 * we are point to point, if Fport we are Fabric.
879 		 */
880 		if (sp->cmn.fPort)
881 			rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
882 		else if (!(phba->hba_flag & HBA_FCOE_SUPPORT))
883 			rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
884 		else {
885 			lpfc_printf_vlog(vport, KERN_ERR,
886 				LOG_FIP | LOG_ELS,
887 				"2831 FLOGI response with cleared Fabric "
888 				"bit fcf_index 0x%x "
889 				"Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
890 				"Fabric Name "
891 				"%02x%02x%02x%02x%02x%02x%02x%02x\n",
892 				phba->fcf.current_rec.fcf_indx,
893 				phba->fcf.current_rec.switch_name[0],
894 				phba->fcf.current_rec.switch_name[1],
895 				phba->fcf.current_rec.switch_name[2],
896 				phba->fcf.current_rec.switch_name[3],
897 				phba->fcf.current_rec.switch_name[4],
898 				phba->fcf.current_rec.switch_name[5],
899 				phba->fcf.current_rec.switch_name[6],
900 				phba->fcf.current_rec.switch_name[7],
901 				phba->fcf.current_rec.fabric_name[0],
902 				phba->fcf.current_rec.fabric_name[1],
903 				phba->fcf.current_rec.fabric_name[2],
904 				phba->fcf.current_rec.fabric_name[3],
905 				phba->fcf.current_rec.fabric_name[4],
906 				phba->fcf.current_rec.fabric_name[5],
907 				phba->fcf.current_rec.fabric_name[6],
908 				phba->fcf.current_rec.fabric_name[7]);
909 			lpfc_nlp_put(ndlp);
910 			spin_lock_irq(&phba->hbalock);
911 			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
912 			phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
913 			spin_unlock_irq(&phba->hbalock);
914 			goto out;
915 		}
916 		if (!rc) {
917 			/* Mark the FCF discovery process done */
918 			if (phba->hba_flag & HBA_FIP_SUPPORT)
919 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
920 						LOG_ELS,
921 						"2769 FLOGI to FCF (x%x) "
922 						"completed successfully\n",
923 						phba->fcf.current_rec.fcf_indx);
924 			spin_lock_irq(&phba->hbalock);
925 			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
926 			phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
927 			spin_unlock_irq(&phba->hbalock);
928 			goto out;
929 		}
930 	}
931 
932 flogifail:
933 	lpfc_nlp_put(ndlp);
934 
935 	if (!lpfc_error_lost_link(irsp)) {
936 		/* FLOGI failed, so just use loop map to make discovery list */
937 		lpfc_disc_list_loopmap(vport);
938 
939 		/* Start discovery */
940 		lpfc_disc_start(vport);
941 	} else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
942 			((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
943 			(irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
944 			(phba->link_state != LPFC_CLEAR_LA)) {
945 		/* If FLOGI failed enable link interrupt. */
946 		lpfc_issue_clear_la(phba, vport);
947 	}
948 out:
949 	lpfc_els_free_iocb(phba, cmdiocb);
950 }
951 
952 /**
953  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
954  * @vport: pointer to a host virtual N_Port data structure.
955  * @ndlp: pointer to a node-list data structure.
956  * @retry: number of retries to the command IOCB.
957  *
958  * This routine issues a Fabric Login (FLOGI) Request ELS command
959  * for a @vport. The initiator service parameters are put into the payload
960  * of the FLOGI Request IOCB and the top-level callback function pointer
961  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
962  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
963  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
964  *
965  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
966  * will be incremented by 1 for holding the ndlp and the reference to ndlp
967  * will be stored into the context1 field of the IOCB for the completion
968  * callback function to the FLOGI ELS command.
969  *
970  * Return code
971  *   0 - successfully issued flogi iocb for @vport
972  *   1 - failed to issue flogi iocb for @vport
973  **/
974 static int
975 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
976 		     uint8_t retry)
977 {
978 	struct lpfc_hba  *phba = vport->phba;
979 	struct serv_parm *sp;
980 	IOCB_t *icmd;
981 	struct lpfc_iocbq *elsiocb;
982 	struct lpfc_sli_ring *pring;
983 	uint8_t *pcmd;
984 	uint16_t cmdsize;
985 	uint32_t tmo;
986 	int rc;
987 
988 	pring = &phba->sli.ring[LPFC_ELS_RING];
989 
990 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
991 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
992 				     ndlp->nlp_DID, ELS_CMD_FLOGI);
993 
994 	if (!elsiocb)
995 		return 1;
996 
997 	icmd = &elsiocb->iocb;
998 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
999 
1000 	/* For FLOGI request, remainder of payload is service parameters */
1001 	*((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1002 	pcmd += sizeof(uint32_t);
1003 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1004 	sp = (struct serv_parm *) pcmd;
1005 
1006 	/* Setup CSPs accordingly for Fabric */
1007 	sp->cmn.e_d_tov = 0;
1008 	sp->cmn.w2.r_a_tov = 0;
1009 	sp->cls1.classValid = 0;
1010 	sp->cls2.seqDelivery = 1;
1011 	sp->cls3.seqDelivery = 1;
1012 	if (sp->cmn.fcphLow < FC_PH3)
1013 		sp->cmn.fcphLow = FC_PH3;
1014 	if (sp->cmn.fcphHigh < FC_PH3)
1015 		sp->cmn.fcphHigh = FC_PH3;
1016 
1017 	if  (phba->sli_rev == LPFC_SLI_REV4) {
1018 		elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1019 		elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1020 		/* FLOGI needs to be 3 for WQE FCFI */
1021 		/* Set the fcfi to the fcfi we registered with */
1022 		elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1023 	} else if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1024 		sp->cmn.request_multiple_Nport = 1;
1025 		/* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1026 		icmd->ulpCt_h = 1;
1027 		icmd->ulpCt_l = 0;
1028 	}
1029 
1030 	if (phba->fc_topology != TOPOLOGY_LOOP) {
1031 		icmd->un.elsreq64.myID = 0;
1032 		icmd->un.elsreq64.fl = 1;
1033 	}
1034 
1035 	tmo = phba->fc_ratov;
1036 	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1037 	lpfc_set_disctmo(vport);
1038 	phba->fc_ratov = tmo;
1039 
1040 	phba->fc_stat.elsXmitFLOGI++;
1041 	elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1042 
1043 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1044 		"Issue FLOGI:     opt:x%x",
1045 		phba->sli3_options, 0, 0);
1046 
1047 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1048 	if (rc == IOCB_ERROR) {
1049 		lpfc_els_free_iocb(phba, elsiocb);
1050 		return 1;
1051 	}
1052 	return 0;
1053 }
1054 
1055 /**
1056  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1057  * @phba: pointer to lpfc hba data structure.
1058  *
1059  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1060  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1061  * list and issues an abort IOCB commond on each outstanding IOCB that
1062  * contains a active Fabric_DID ndlp. Note that this function is to issue
1063  * the abort IOCB command on all the outstanding IOCBs, thus when this
1064  * function returns, it does not guarantee all the IOCBs are actually aborted.
1065  *
1066  * Return code
1067  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1068  **/
1069 int
1070 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1071 {
1072 	struct lpfc_sli_ring *pring;
1073 	struct lpfc_iocbq *iocb, *next_iocb;
1074 	struct lpfc_nodelist *ndlp;
1075 	IOCB_t *icmd;
1076 
1077 	/* Abort outstanding I/O on NPort <nlp_DID> */
1078 	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1079 			"0201 Abort outstanding I/O on NPort x%x\n",
1080 			Fabric_DID);
1081 
1082 	pring = &phba->sli.ring[LPFC_ELS_RING];
1083 
1084 	/*
1085 	 * Check the txcmplq for an iocb that matches the nport the driver is
1086 	 * searching for.
1087 	 */
1088 	spin_lock_irq(&phba->hbalock);
1089 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1090 		icmd = &iocb->iocb;
1091 		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
1092 		    icmd->un.elsreq64.bdl.ulpIoTag32) {
1093 			ndlp = (struct lpfc_nodelist *)(iocb->context1);
1094 			if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1095 			    (ndlp->nlp_DID == Fabric_DID))
1096 				lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1097 		}
1098 	}
1099 	spin_unlock_irq(&phba->hbalock);
1100 
1101 	return 0;
1102 }
1103 
1104 /**
1105  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1106  * @vport: pointer to a host virtual N_Port data structure.
1107  *
1108  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1109  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1110  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1111  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1112  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1113  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1114  * @vport.
1115  *
1116  * Return code
1117  *   0 - failed to issue initial flogi for @vport
1118  *   1 - successfully issued initial flogi for @vport
1119  **/
1120 int
1121 lpfc_initial_flogi(struct lpfc_vport *vport)
1122 {
1123 	struct lpfc_hba *phba = vport->phba;
1124 	struct lpfc_nodelist *ndlp;
1125 
1126 	vport->port_state = LPFC_FLOGI;
1127 	lpfc_set_disctmo(vport);
1128 
1129 	/* First look for the Fabric ndlp */
1130 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
1131 	if (!ndlp) {
1132 		/* Cannot find existing Fabric ndlp, so allocate a new one */
1133 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1134 		if (!ndlp)
1135 			return 0;
1136 		lpfc_nlp_init(vport, ndlp, Fabric_DID);
1137 		/* Set the node type */
1138 		ndlp->nlp_type |= NLP_FABRIC;
1139 		/* Put ndlp onto node list */
1140 		lpfc_enqueue_node(vport, ndlp);
1141 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
1142 		/* re-setup ndlp without removing from node list */
1143 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1144 		if (!ndlp)
1145 			return 0;
1146 	}
1147 
1148 	if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1149 		/* This decrement of reference count to node shall kick off
1150 		 * the release of the node.
1151 		 */
1152 		lpfc_nlp_put(ndlp);
1153 		return 0;
1154 	}
1155 	return 1;
1156 }
1157 
1158 /**
1159  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1160  * @vport: pointer to a host virtual N_Port data structure.
1161  *
1162  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1163  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1164  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1165  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1166  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1167  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1168  * @vport.
1169  *
1170  * Return code
1171  *   0 - failed to issue initial fdisc for @vport
1172  *   1 - successfully issued initial fdisc for @vport
1173  **/
1174 int
1175 lpfc_initial_fdisc(struct lpfc_vport *vport)
1176 {
1177 	struct lpfc_hba *phba = vport->phba;
1178 	struct lpfc_nodelist *ndlp;
1179 
1180 	/* First look for the Fabric ndlp */
1181 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
1182 	if (!ndlp) {
1183 		/* Cannot find existing Fabric ndlp, so allocate a new one */
1184 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1185 		if (!ndlp)
1186 			return 0;
1187 		lpfc_nlp_init(vport, ndlp, Fabric_DID);
1188 		/* Put ndlp onto node list */
1189 		lpfc_enqueue_node(vport, ndlp);
1190 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
1191 		/* re-setup ndlp without removing from node list */
1192 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1193 		if (!ndlp)
1194 			return 0;
1195 	}
1196 
1197 	if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1198 		/* decrement node reference count to trigger the release of
1199 		 * the node.
1200 		 */
1201 		lpfc_nlp_put(ndlp);
1202 		return 0;
1203 	}
1204 	return 1;
1205 }
1206 
1207 /**
1208  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1209  * @vport: pointer to a host virtual N_Port data structure.
1210  *
1211  * This routine checks whether there are more remaining Port Logins
1212  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1213  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1214  * to issue ELS PLOGIs up to the configured discover threads with the
1215  * @vport (@vport->cfg_discovery_threads). The function also decrement
1216  * the @vport's num_disc_node by 1 if it is not already 0.
1217  **/
1218 void
1219 lpfc_more_plogi(struct lpfc_vport *vport)
1220 {
1221 	int sentplogi;
1222 
1223 	if (vport->num_disc_nodes)
1224 		vport->num_disc_nodes--;
1225 
1226 	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
1227 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1228 			 "0232 Continue discovery with %d PLOGIs to go "
1229 			 "Data: x%x x%x x%x\n",
1230 			 vport->num_disc_nodes, vport->fc_plogi_cnt,
1231 			 vport->fc_flag, vport->port_state);
1232 	/* Check to see if there are more PLOGIs to be sent */
1233 	if (vport->fc_flag & FC_NLP_MORE)
1234 		/* go thru NPR nodes and issue any remaining ELS PLOGIs */
1235 		sentplogi = lpfc_els_disc_plogi(vport);
1236 
1237 	return;
1238 }
1239 
1240 /**
1241  * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1242  * @phba: pointer to lpfc hba data structure.
1243  * @prsp: pointer to response IOCB payload.
1244  * @ndlp: pointer to a node-list data structure.
1245  *
1246  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1247  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1248  * The following cases are considered N_Port confirmed:
1249  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1250  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1251  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1252  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1253  * 1) if there is a node on vport list other than the @ndlp with the same
1254  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1255  * on that node to release the RPI associated with the node; 2) if there is
1256  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1257  * into, a new node shall be allocated (or activated). In either case, the
1258  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1259  * be released and the new_ndlp shall be put on to the vport node list and
1260  * its pointer returned as the confirmed node.
1261  *
1262  * Note that before the @ndlp got "released", the keepDID from not-matching
1263  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1264  * of the @ndlp. This is because the release of @ndlp is actually to put it
1265  * into an inactive state on the vport node list and the vport node list
1266  * management algorithm does not allow two node with a same DID.
1267  *
1268  * Return code
1269  *   pointer to the PLOGI N_Port @ndlp
1270  **/
1271 static struct lpfc_nodelist *
1272 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1273 			 struct lpfc_nodelist *ndlp)
1274 {
1275 	struct lpfc_vport    *vport = ndlp->vport;
1276 	struct lpfc_nodelist *new_ndlp;
1277 	struct lpfc_rport_data *rdata;
1278 	struct fc_rport *rport;
1279 	struct serv_parm *sp;
1280 	uint8_t  name[sizeof(struct lpfc_name)];
1281 	uint32_t rc, keepDID = 0;
1282 	int  put_node;
1283 	int  put_rport;
1284 
1285 	/* Fabric nodes can have the same WWPN so we don't bother searching
1286 	 * by WWPN.  Just return the ndlp that was given to us.
1287 	 */
1288 	if (ndlp->nlp_type & NLP_FABRIC)
1289 		return ndlp;
1290 
1291 	sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1292 	memset(name, 0, sizeof(struct lpfc_name));
1293 
1294 	/* Now we find out if the NPort we are logging into, matches the WWPN
1295 	 * we have for that ndlp. If not, we have some work to do.
1296 	 */
1297 	new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1298 
1299 	if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1300 		return ndlp;
1301 
1302 	if (!new_ndlp) {
1303 		rc = memcmp(&ndlp->nlp_portname, name,
1304 			    sizeof(struct lpfc_name));
1305 		if (!rc)
1306 			return ndlp;
1307 		new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1308 		if (!new_ndlp)
1309 			return ndlp;
1310 		lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
1311 	} else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1312 		rc = memcmp(&ndlp->nlp_portname, name,
1313 			    sizeof(struct lpfc_name));
1314 		if (!rc)
1315 			return ndlp;
1316 		new_ndlp = lpfc_enable_node(vport, new_ndlp,
1317 						NLP_STE_UNUSED_NODE);
1318 		if (!new_ndlp)
1319 			return ndlp;
1320 		keepDID = new_ndlp->nlp_DID;
1321 	} else
1322 		keepDID = new_ndlp->nlp_DID;
1323 
1324 	lpfc_unreg_rpi(vport, new_ndlp);
1325 	new_ndlp->nlp_DID = ndlp->nlp_DID;
1326 	new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1327 
1328 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
1329 		new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1330 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1331 
1332 	/* Set state will put new_ndlp on to node list if not already done */
1333 	lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1334 
1335 	/* Move this back to NPR state */
1336 	if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1337 		/* The new_ndlp is replacing ndlp totally, so we need
1338 		 * to put ndlp on UNUSED list and try to free it.
1339 		 */
1340 
1341 		/* Fix up the rport accordingly */
1342 		rport =  ndlp->rport;
1343 		if (rport) {
1344 			rdata = rport->dd_data;
1345 			if (rdata->pnode == ndlp) {
1346 				lpfc_nlp_put(ndlp);
1347 				ndlp->rport = NULL;
1348 				rdata->pnode = lpfc_nlp_get(new_ndlp);
1349 				new_ndlp->rport = rport;
1350 			}
1351 			new_ndlp->nlp_type = ndlp->nlp_type;
1352 		}
1353 		/* We shall actually free the ndlp with both nlp_DID and
1354 		 * nlp_portname fields equals 0 to avoid any ndlp on the
1355 		 * nodelist never to be used.
1356 		 */
1357 		if (ndlp->nlp_DID == 0) {
1358 			spin_lock_irq(&phba->ndlp_lock);
1359 			NLP_SET_FREE_REQ(ndlp);
1360 			spin_unlock_irq(&phba->ndlp_lock);
1361 		}
1362 
1363 		/* Two ndlps cannot have the same did on the nodelist */
1364 		ndlp->nlp_DID = keepDID;
1365 		lpfc_drop_node(vport, ndlp);
1366 	}
1367 	else {
1368 		lpfc_unreg_rpi(vport, ndlp);
1369 		/* Two ndlps cannot have the same did */
1370 		ndlp->nlp_DID = keepDID;
1371 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1372 		/* Since we are swapping the ndlp passed in with the new one
1373 		 * and the did has already been swapped, copy over the
1374 		 * state and names.
1375 		 */
1376 		memcpy(&new_ndlp->nlp_portname, &ndlp->nlp_portname,
1377 			sizeof(struct lpfc_name));
1378 		memcpy(&new_ndlp->nlp_nodename, &ndlp->nlp_nodename,
1379 			sizeof(struct lpfc_name));
1380 		new_ndlp->nlp_state = ndlp->nlp_state;
1381 		/* Fix up the rport accordingly */
1382 		rport = ndlp->rport;
1383 		if (rport) {
1384 			rdata = rport->dd_data;
1385 			put_node = rdata->pnode != NULL;
1386 			put_rport = ndlp->rport != NULL;
1387 			rdata->pnode = NULL;
1388 			ndlp->rport = NULL;
1389 			if (put_node)
1390 				lpfc_nlp_put(ndlp);
1391 			if (put_rport)
1392 				put_device(&rport->dev);
1393 		}
1394 	}
1395 	return new_ndlp;
1396 }
1397 
1398 /**
1399  * lpfc_end_rscn - Check and handle more rscn for a vport
1400  * @vport: pointer to a host virtual N_Port data structure.
1401  *
1402  * This routine checks whether more Registration State Change
1403  * Notifications (RSCNs) came in while the discovery state machine was in
1404  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1405  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1406  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1407  * handling the RSCNs.
1408  **/
1409 void
1410 lpfc_end_rscn(struct lpfc_vport *vport)
1411 {
1412 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1413 
1414 	if (vport->fc_flag & FC_RSCN_MODE) {
1415 		/*
1416 		 * Check to see if more RSCNs came in while we were
1417 		 * processing this one.
1418 		 */
1419 		if (vport->fc_rscn_id_cnt ||
1420 		    (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1421 			lpfc_els_handle_rscn(vport);
1422 		else {
1423 			spin_lock_irq(shost->host_lock);
1424 			vport->fc_flag &= ~FC_RSCN_MODE;
1425 			spin_unlock_irq(shost->host_lock);
1426 		}
1427 	}
1428 }
1429 
1430 /**
1431  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1432  * @phba: pointer to lpfc hba data structure.
1433  * @cmdiocb: pointer to lpfc command iocb data structure.
1434  * @rspiocb: pointer to lpfc response iocb data structure.
1435  *
1436  * This routine is the completion callback function for issuing the Port
1437  * Login (PLOGI) command. For PLOGI completion, there must be an active
1438  * ndlp on the vport node list that matches the remote node ID from the
1439  * PLOGI reponse IOCB. If such ndlp does not exist, the PLOGI is simply
1440  * ignored and command IOCB released. The PLOGI response IOCB status is
1441  * checked for error conditons. If there is error status reported, PLOGI
1442  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1443  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1444  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1445  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1446  * there are additional N_Port nodes with the vport that need to perform
1447  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1448  * PLOGIs.
1449  **/
1450 static void
1451 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1452 		    struct lpfc_iocbq *rspiocb)
1453 {
1454 	struct lpfc_vport *vport = cmdiocb->vport;
1455 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1456 	IOCB_t *irsp;
1457 	struct lpfc_nodelist *ndlp;
1458 	struct lpfc_dmabuf *prsp;
1459 	int disc, rc, did, type;
1460 
1461 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1462 	cmdiocb->context_un.rsp_iocb = rspiocb;
1463 
1464 	irsp = &rspiocb->iocb;
1465 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1466 		"PLOGI cmpl:      status:x%x/x%x did:x%x",
1467 		irsp->ulpStatus, irsp->un.ulpWord[4],
1468 		irsp->un.elsreq64.remoteID);
1469 
1470 	ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1471 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1472 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1473 				 "0136 PLOGI completes to NPort x%x "
1474 				 "with no ndlp. Data: x%x x%x x%x\n",
1475 				 irsp->un.elsreq64.remoteID,
1476 				 irsp->ulpStatus, irsp->un.ulpWord[4],
1477 				 irsp->ulpIoTag);
1478 		goto out;
1479 	}
1480 
1481 	/* Since ndlp can be freed in the disc state machine, note if this node
1482 	 * is being used during discovery.
1483 	 */
1484 	spin_lock_irq(shost->host_lock);
1485 	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1486 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1487 	spin_unlock_irq(shost->host_lock);
1488 	rc   = 0;
1489 
1490 	/* PLOGI completes to NPort <nlp_DID> */
1491 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1492 			 "0102 PLOGI completes to NPort x%x "
1493 			 "Data: x%x x%x x%x x%x x%x\n",
1494 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1495 			 irsp->ulpTimeout, disc, vport->num_disc_nodes);
1496 	/* Check to see if link went down during discovery */
1497 	if (lpfc_els_chk_latt(vport)) {
1498 		spin_lock_irq(shost->host_lock);
1499 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1500 		spin_unlock_irq(shost->host_lock);
1501 		goto out;
1502 	}
1503 
1504 	/* ndlp could be freed in DSM, save these values now */
1505 	type = ndlp->nlp_type;
1506 	did = ndlp->nlp_DID;
1507 
1508 	if (irsp->ulpStatus) {
1509 		/* Check for retry */
1510 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1511 			/* ELS command is being retried */
1512 			if (disc) {
1513 				spin_lock_irq(shost->host_lock);
1514 				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1515 				spin_unlock_irq(shost->host_lock);
1516 			}
1517 			goto out;
1518 		}
1519 		/* PLOGI failed Don't print the vport to vport rjts */
1520 		if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1521 			(((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1522 			((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1523 			(phba)->pport->cfg_log_verbose & LOG_ELS)
1524 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1525 				 "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
1526 				 ndlp->nlp_DID, irsp->ulpStatus,
1527 				 irsp->un.ulpWord[4]);
1528 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1529 		if (lpfc_error_lost_link(irsp))
1530 			rc = NLP_STE_FREED_NODE;
1531 		else
1532 			rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1533 						     NLP_EVT_CMPL_PLOGI);
1534 	} else {
1535 		/* Good status, call state machine */
1536 		prsp = list_entry(((struct lpfc_dmabuf *)
1537 				   cmdiocb->context2)->list.next,
1538 				  struct lpfc_dmabuf, list);
1539 		ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1540 		rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1541 					     NLP_EVT_CMPL_PLOGI);
1542 	}
1543 
1544 	if (disc && vport->num_disc_nodes) {
1545 		/* Check to see if there are more PLOGIs to be sent */
1546 		lpfc_more_plogi(vport);
1547 
1548 		if (vport->num_disc_nodes == 0) {
1549 			spin_lock_irq(shost->host_lock);
1550 			vport->fc_flag &= ~FC_NDISC_ACTIVE;
1551 			spin_unlock_irq(shost->host_lock);
1552 
1553 			lpfc_can_disctmo(vport);
1554 			lpfc_end_rscn(vport);
1555 		}
1556 	}
1557 
1558 out:
1559 	lpfc_els_free_iocb(phba, cmdiocb);
1560 	return;
1561 }
1562 
1563 /**
1564  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
1565  * @vport: pointer to a host virtual N_Port data structure.
1566  * @did: destination port identifier.
1567  * @retry: number of retries to the command IOCB.
1568  *
1569  * This routine issues a Port Login (PLOGI) command to a remote N_Port
1570  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1571  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1572  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1573  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1574  *
1575  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1576  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1577  * will be stored into the context1 field of the IOCB for the completion
1578  * callback function to the PLOGI ELS command.
1579  *
1580  * Return code
1581  *   0 - Successfully issued a plogi for @vport
1582  *   1 - failed to issue a plogi for @vport
1583  **/
1584 int
1585 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1586 {
1587 	struct lpfc_hba  *phba = vport->phba;
1588 	struct serv_parm *sp;
1589 	IOCB_t *icmd;
1590 	struct lpfc_nodelist *ndlp;
1591 	struct lpfc_iocbq *elsiocb;
1592 	struct lpfc_sli *psli;
1593 	uint8_t *pcmd;
1594 	uint16_t cmdsize;
1595 	int ret;
1596 
1597 	psli = &phba->sli;
1598 
1599 	ndlp = lpfc_findnode_did(vport, did);
1600 	if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1601 		ndlp = NULL;
1602 
1603 	/* If ndlp is not NULL, we will bump the reference count on it */
1604 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1605 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
1606 				     ELS_CMD_PLOGI);
1607 	if (!elsiocb)
1608 		return 1;
1609 
1610 	icmd = &elsiocb->iocb;
1611 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1612 
1613 	/* For PLOGI request, remainder of payload is service parameters */
1614 	*((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
1615 	pcmd += sizeof(uint32_t);
1616 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1617 	sp = (struct serv_parm *) pcmd;
1618 
1619 	/*
1620 	 * If we are a N-port connected to a Fabric, fix-up paramm's so logins
1621 	 * to device on remote loops work.
1622 	 */
1623 	if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
1624 		sp->cmn.altBbCredit = 1;
1625 
1626 	if (sp->cmn.fcphLow < FC_PH_4_3)
1627 		sp->cmn.fcphLow = FC_PH_4_3;
1628 
1629 	if (sp->cmn.fcphHigh < FC_PH3)
1630 		sp->cmn.fcphHigh = FC_PH3;
1631 
1632 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1633 		"Issue PLOGI:     did:x%x",
1634 		did, 0, 0);
1635 
1636 	phba->fc_stat.elsXmitPLOGI++;
1637 	elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1638 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
1639 
1640 	if (ret == IOCB_ERROR) {
1641 		lpfc_els_free_iocb(phba, elsiocb);
1642 		return 1;
1643 	}
1644 	return 0;
1645 }
1646 
1647 /**
1648  * lpfc_cmpl_els_prli - Completion callback function for prli
1649  * @phba: pointer to lpfc hba data structure.
1650  * @cmdiocb: pointer to lpfc command iocb data structure.
1651  * @rspiocb: pointer to lpfc response iocb data structure.
1652  *
1653  * This routine is the completion callback function for a Process Login
1654  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
1655  * status. If there is error status reported, PRLI retry shall be attempted
1656  * by invoking the lpfc_els_retry() routine. Otherwise, the state
1657  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
1658  * ndlp to mark the PRLI completion.
1659  **/
1660 static void
1661 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1662 		   struct lpfc_iocbq *rspiocb)
1663 {
1664 	struct lpfc_vport *vport = cmdiocb->vport;
1665 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1666 	IOCB_t *irsp;
1667 	struct lpfc_sli *psli;
1668 	struct lpfc_nodelist *ndlp;
1669 
1670 	psli = &phba->sli;
1671 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1672 	cmdiocb->context_un.rsp_iocb = rspiocb;
1673 
1674 	irsp = &(rspiocb->iocb);
1675 	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1676 	spin_lock_irq(shost->host_lock);
1677 	ndlp->nlp_flag &= ~NLP_PRLI_SND;
1678 	spin_unlock_irq(shost->host_lock);
1679 
1680 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1681 		"PRLI cmpl:       status:x%x/x%x did:x%x",
1682 		irsp->ulpStatus, irsp->un.ulpWord[4],
1683 		ndlp->nlp_DID);
1684 	/* PRLI completes to NPort <nlp_DID> */
1685 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1686 			 "0103 PRLI completes to NPort x%x "
1687 			 "Data: x%x x%x x%x x%x\n",
1688 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1689 			 irsp->ulpTimeout, vport->num_disc_nodes);
1690 
1691 	vport->fc_prli_sent--;
1692 	/* Check to see if link went down during discovery */
1693 	if (lpfc_els_chk_latt(vport))
1694 		goto out;
1695 
1696 	if (irsp->ulpStatus) {
1697 		/* Check for retry */
1698 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1699 			/* ELS command is being retried */
1700 			goto out;
1701 		}
1702 		/* PRLI failed */
1703 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1704 				 "2754 PRLI failure DID:%06X Status:x%x/x%x\n",
1705 				 ndlp->nlp_DID, irsp->ulpStatus,
1706 				 irsp->un.ulpWord[4]);
1707 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1708 		if (lpfc_error_lost_link(irsp))
1709 			goto out;
1710 		else
1711 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1712 						NLP_EVT_CMPL_PRLI);
1713 	} else
1714 		/* Good status, call state machine */
1715 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1716 					NLP_EVT_CMPL_PRLI);
1717 out:
1718 	lpfc_els_free_iocb(phba, cmdiocb);
1719 	return;
1720 }
1721 
1722 /**
1723  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
1724  * @vport: pointer to a host virtual N_Port data structure.
1725  * @ndlp: pointer to a node-list data structure.
1726  * @retry: number of retries to the command IOCB.
1727  *
1728  * This routine issues a Process Login (PRLI) ELS command for the
1729  * @vport. The PRLI service parameters are set up in the payload of the
1730  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
1731  * is put to the IOCB completion callback func field before invoking the
1732  * routine lpfc_sli_issue_iocb() to send out PRLI command.
1733  *
1734  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1735  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1736  * will be stored into the context1 field of the IOCB for the completion
1737  * callback function to the PRLI ELS command.
1738  *
1739  * Return code
1740  *   0 - successfully issued prli iocb command for @vport
1741  *   1 - failed to issue prli iocb command for @vport
1742  **/
1743 int
1744 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1745 		    uint8_t retry)
1746 {
1747 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1748 	struct lpfc_hba *phba = vport->phba;
1749 	PRLI *npr;
1750 	IOCB_t *icmd;
1751 	struct lpfc_iocbq *elsiocb;
1752 	uint8_t *pcmd;
1753 	uint16_t cmdsize;
1754 
1755 	cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1756 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1757 				     ndlp->nlp_DID, ELS_CMD_PRLI);
1758 	if (!elsiocb)
1759 		return 1;
1760 
1761 	icmd = &elsiocb->iocb;
1762 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1763 
1764 	/* For PRLI request, remainder of payload is service parameters */
1765 	memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1766 	*((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1767 	pcmd += sizeof(uint32_t);
1768 
1769 	/* For PRLI, remainder of payload is PRLI parameter page */
1770 	npr = (PRLI *) pcmd;
1771 	/*
1772 	 * If our firmware version is 3.20 or later,
1773 	 * set the following bits for FC-TAPE support.
1774 	 */
1775 	if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1776 		npr->ConfmComplAllowed = 1;
1777 		npr->Retry = 1;
1778 		npr->TaskRetryIdReq = 1;
1779 	}
1780 	npr->estabImagePair = 1;
1781 	npr->readXferRdyDis = 1;
1782 
1783 	/* For FCP support */
1784 	npr->prliType = PRLI_FCP_TYPE;
1785 	npr->initiatorFunc = 1;
1786 
1787 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1788 		"Issue PRLI:      did:x%x",
1789 		ndlp->nlp_DID, 0, 0);
1790 
1791 	phba->fc_stat.elsXmitPRLI++;
1792 	elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1793 	spin_lock_irq(shost->host_lock);
1794 	ndlp->nlp_flag |= NLP_PRLI_SND;
1795 	spin_unlock_irq(shost->host_lock);
1796 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
1797 	    IOCB_ERROR) {
1798 		spin_lock_irq(shost->host_lock);
1799 		ndlp->nlp_flag &= ~NLP_PRLI_SND;
1800 		spin_unlock_irq(shost->host_lock);
1801 		lpfc_els_free_iocb(phba, elsiocb);
1802 		return 1;
1803 	}
1804 	vport->fc_prli_sent++;
1805 	return 0;
1806 }
1807 
1808 /**
1809  * lpfc_rscn_disc - Perform rscn discovery for a vport
1810  * @vport: pointer to a host virtual N_Port data structure.
1811  *
1812  * This routine performs Registration State Change Notification (RSCN)
1813  * discovery for a @vport. If the @vport's node port recovery count is not
1814  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
1815  * the nodes that need recovery. If none of the PLOGI were needed through
1816  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
1817  * invoked to check and handle possible more RSCN came in during the period
1818  * of processing the current ones.
1819  **/
1820 static void
1821 lpfc_rscn_disc(struct lpfc_vport *vport)
1822 {
1823 	lpfc_can_disctmo(vport);
1824 
1825 	/* RSCN discovery */
1826 	/* go thru NPR nodes and issue ELS PLOGIs */
1827 	if (vport->fc_npr_cnt)
1828 		if (lpfc_els_disc_plogi(vport))
1829 			return;
1830 
1831 	lpfc_end_rscn(vport);
1832 }
1833 
1834 /**
1835  * lpfc_adisc_done - Complete the adisc phase of discovery
1836  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
1837  *
1838  * This function is called when the final ADISC is completed during discovery.
1839  * This function handles clearing link attention or issuing reg_vpi depending
1840  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
1841  * discovery.
1842  * This function is called with no locks held.
1843  **/
1844 static void
1845 lpfc_adisc_done(struct lpfc_vport *vport)
1846 {
1847 	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1848 	struct lpfc_hba   *phba = vport->phba;
1849 
1850 	/*
1851 	 * For NPIV, cmpl_reg_vpi will set port_state to READY,
1852 	 * and continue discovery.
1853 	 */
1854 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1855 	    !(vport->fc_flag & FC_RSCN_MODE) &&
1856 	    (phba->sli_rev < LPFC_SLI_REV4)) {
1857 		lpfc_issue_reg_vpi(phba, vport);
1858 		return;
1859 	}
1860 	/*
1861 	* For SLI2, we need to set port_state to READY
1862 	* and continue discovery.
1863 	*/
1864 	if (vport->port_state < LPFC_VPORT_READY) {
1865 		/* If we get here, there is nothing to ADISC */
1866 		if (vport->port_type == LPFC_PHYSICAL_PORT)
1867 			lpfc_issue_clear_la(phba, vport);
1868 		if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1869 			vport->num_disc_nodes = 0;
1870 			/* go thru NPR list, issue ELS PLOGIs */
1871 			if (vport->fc_npr_cnt)
1872 				lpfc_els_disc_plogi(vport);
1873 			if (!vport->num_disc_nodes) {
1874 				spin_lock_irq(shost->host_lock);
1875 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
1876 				spin_unlock_irq(shost->host_lock);
1877 				lpfc_can_disctmo(vport);
1878 				lpfc_end_rscn(vport);
1879 			}
1880 		}
1881 		vport->port_state = LPFC_VPORT_READY;
1882 	} else
1883 		lpfc_rscn_disc(vport);
1884 }
1885 
1886 /**
1887  * lpfc_more_adisc - Issue more adisc as needed
1888  * @vport: pointer to a host virtual N_Port data structure.
1889  *
1890  * This routine determines whether there are more ndlps on a @vport
1891  * node list need to have Address Discover (ADISC) issued. If so, it will
1892  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
1893  * remaining nodes which need to have ADISC sent.
1894  **/
1895 void
1896 lpfc_more_adisc(struct lpfc_vport *vport)
1897 {
1898 	int sentadisc;
1899 
1900 	if (vport->num_disc_nodes)
1901 		vport->num_disc_nodes--;
1902 	/* Continue discovery with <num_disc_nodes> ADISCs to go */
1903 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1904 			 "0210 Continue discovery with %d ADISCs to go "
1905 			 "Data: x%x x%x x%x\n",
1906 			 vport->num_disc_nodes, vport->fc_adisc_cnt,
1907 			 vport->fc_flag, vport->port_state);
1908 	/* Check to see if there are more ADISCs to be sent */
1909 	if (vport->fc_flag & FC_NLP_MORE) {
1910 		lpfc_set_disctmo(vport);
1911 		/* go thru NPR nodes and issue any remaining ELS ADISCs */
1912 		sentadisc = lpfc_els_disc_adisc(vport);
1913 	}
1914 	if (!vport->num_disc_nodes)
1915 		lpfc_adisc_done(vport);
1916 	return;
1917 }
1918 
1919 /**
1920  * lpfc_cmpl_els_adisc - Completion callback function for adisc
1921  * @phba: pointer to lpfc hba data structure.
1922  * @cmdiocb: pointer to lpfc command iocb data structure.
1923  * @rspiocb: pointer to lpfc response iocb data structure.
1924  *
1925  * This routine is the completion function for issuing the Address Discover
1926  * (ADISC) command. It first checks to see whether link went down during
1927  * the discovery process. If so, the node will be marked as node port
1928  * recovery for issuing discover IOCB by the link attention handler and
1929  * exit. Otherwise, the response status is checked. If error was reported
1930  * in the response status, the ADISC command shall be retried by invoking
1931  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
1932  * the response status, the state machine is invoked to set transition
1933  * with respect to NLP_EVT_CMPL_ADISC event.
1934  **/
1935 static void
1936 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1937 		    struct lpfc_iocbq *rspiocb)
1938 {
1939 	struct lpfc_vport *vport = cmdiocb->vport;
1940 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1941 	IOCB_t *irsp;
1942 	struct lpfc_nodelist *ndlp;
1943 	int  disc;
1944 
1945 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1946 	cmdiocb->context_un.rsp_iocb = rspiocb;
1947 
1948 	irsp = &(rspiocb->iocb);
1949 	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1950 
1951 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1952 		"ADISC cmpl:      status:x%x/x%x did:x%x",
1953 		irsp->ulpStatus, irsp->un.ulpWord[4],
1954 		ndlp->nlp_DID);
1955 
1956 	/* Since ndlp can be freed in the disc state machine, note if this node
1957 	 * is being used during discovery.
1958 	 */
1959 	spin_lock_irq(shost->host_lock);
1960 	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1961 	ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1962 	spin_unlock_irq(shost->host_lock);
1963 	/* ADISC completes to NPort <nlp_DID> */
1964 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1965 			 "0104 ADISC completes to NPort x%x "
1966 			 "Data: x%x x%x x%x x%x x%x\n",
1967 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1968 			 irsp->ulpTimeout, disc, vport->num_disc_nodes);
1969 	/* Check to see if link went down during discovery */
1970 	if (lpfc_els_chk_latt(vport)) {
1971 		spin_lock_irq(shost->host_lock);
1972 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1973 		spin_unlock_irq(shost->host_lock);
1974 		goto out;
1975 	}
1976 
1977 	if (irsp->ulpStatus) {
1978 		/* Check for retry */
1979 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1980 			/* ELS command is being retried */
1981 			if (disc) {
1982 				spin_lock_irq(shost->host_lock);
1983 				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1984 				spin_unlock_irq(shost->host_lock);
1985 				lpfc_set_disctmo(vport);
1986 			}
1987 			goto out;
1988 		}
1989 		/* ADISC failed */
1990 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1991 				 "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
1992 				 ndlp->nlp_DID, irsp->ulpStatus,
1993 				 irsp->un.ulpWord[4]);
1994 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1995 		if (!lpfc_error_lost_link(irsp))
1996 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1997 						NLP_EVT_CMPL_ADISC);
1998 	} else
1999 		/* Good status, call state machine */
2000 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2001 					NLP_EVT_CMPL_ADISC);
2002 
2003 	/* Check to see if there are more ADISCs to be sent */
2004 	if (disc && vport->num_disc_nodes)
2005 		lpfc_more_adisc(vport);
2006 out:
2007 	lpfc_els_free_iocb(phba, cmdiocb);
2008 	return;
2009 }
2010 
2011 /**
2012  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2013  * @vport: pointer to a virtual N_Port data structure.
2014  * @ndlp: pointer to a node-list data structure.
2015  * @retry: number of retries to the command IOCB.
2016  *
2017  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2018  * @vport. It prepares the payload of the ADISC ELS command, updates the
2019  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2020  * to issue the ADISC ELS command.
2021  *
2022  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2023  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2024  * will be stored into the context1 field of the IOCB for the completion
2025  * callback function to the ADISC ELS command.
2026  *
2027  * Return code
2028  *   0 - successfully issued adisc
2029  *   1 - failed to issue adisc
2030  **/
2031 int
2032 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2033 		     uint8_t retry)
2034 {
2035 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2036 	struct lpfc_hba  *phba = vport->phba;
2037 	ADISC *ap;
2038 	IOCB_t *icmd;
2039 	struct lpfc_iocbq *elsiocb;
2040 	uint8_t *pcmd;
2041 	uint16_t cmdsize;
2042 
2043 	cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2044 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2045 				     ndlp->nlp_DID, ELS_CMD_ADISC);
2046 	if (!elsiocb)
2047 		return 1;
2048 
2049 	icmd = &elsiocb->iocb;
2050 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2051 
2052 	/* For ADISC request, remainder of payload is service parameters */
2053 	*((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2054 	pcmd += sizeof(uint32_t);
2055 
2056 	/* Fill in ADISC payload */
2057 	ap = (ADISC *) pcmd;
2058 	ap->hardAL_PA = phba->fc_pref_ALPA;
2059 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2060 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2061 	ap->DID = be32_to_cpu(vport->fc_myDID);
2062 
2063 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2064 		"Issue ADISC:     did:x%x",
2065 		ndlp->nlp_DID, 0, 0);
2066 
2067 	phba->fc_stat.elsXmitADISC++;
2068 	elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2069 	spin_lock_irq(shost->host_lock);
2070 	ndlp->nlp_flag |= NLP_ADISC_SND;
2071 	spin_unlock_irq(shost->host_lock);
2072 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2073 	    IOCB_ERROR) {
2074 		spin_lock_irq(shost->host_lock);
2075 		ndlp->nlp_flag &= ~NLP_ADISC_SND;
2076 		spin_unlock_irq(shost->host_lock);
2077 		lpfc_els_free_iocb(phba, elsiocb);
2078 		return 1;
2079 	}
2080 	return 0;
2081 }
2082 
2083 /**
2084  * lpfc_cmpl_els_logo - Completion callback function for logo
2085  * @phba: pointer to lpfc hba data structure.
2086  * @cmdiocb: pointer to lpfc command iocb data structure.
2087  * @rspiocb: pointer to lpfc response iocb data structure.
2088  *
2089  * This routine is the completion function for issuing the ELS Logout (LOGO)
2090  * command. If no error status was reported from the LOGO response, the
2091  * state machine of the associated ndlp shall be invoked for transition with
2092  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2093  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2094  **/
2095 static void
2096 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2097 		   struct lpfc_iocbq *rspiocb)
2098 {
2099 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2100 	struct lpfc_vport *vport = ndlp->vport;
2101 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2102 	IOCB_t *irsp;
2103 	struct lpfc_sli *psli;
2104 
2105 	psli = &phba->sli;
2106 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2107 	cmdiocb->context_un.rsp_iocb = rspiocb;
2108 
2109 	irsp = &(rspiocb->iocb);
2110 	spin_lock_irq(shost->host_lock);
2111 	ndlp->nlp_flag &= ~NLP_LOGO_SND;
2112 	spin_unlock_irq(shost->host_lock);
2113 
2114 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2115 		"LOGO cmpl:       status:x%x/x%x did:x%x",
2116 		irsp->ulpStatus, irsp->un.ulpWord[4],
2117 		ndlp->nlp_DID);
2118 	/* LOGO completes to NPort <nlp_DID> */
2119 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2120 			 "0105 LOGO completes to NPort x%x "
2121 			 "Data: x%x x%x x%x x%x\n",
2122 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2123 			 irsp->ulpTimeout, vport->num_disc_nodes);
2124 	/* Check to see if link went down during discovery */
2125 	if (lpfc_els_chk_latt(vport))
2126 		goto out;
2127 
2128 	if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2129 	        /* NLP_EVT_DEVICE_RM should unregister the RPI
2130 		 * which should abort all outstanding IOs.
2131 		 */
2132 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2133 					NLP_EVT_DEVICE_RM);
2134 		goto out;
2135 	}
2136 
2137 	if (irsp->ulpStatus) {
2138 		/* Check for retry */
2139 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
2140 			/* ELS command is being retried */
2141 			goto out;
2142 		/* LOGO failed */
2143 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2144 				 "2756 LOGO failure DID:%06X Status:x%x/x%x\n",
2145 				 ndlp->nlp_DID, irsp->ulpStatus,
2146 				 irsp->un.ulpWord[4]);
2147 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2148 		if (lpfc_error_lost_link(irsp))
2149 			goto out;
2150 		else
2151 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2152 						NLP_EVT_CMPL_LOGO);
2153 	} else
2154 		/* Good status, call state machine.
2155 		 * This will unregister the rpi if needed.
2156 		 */
2157 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2158 					NLP_EVT_CMPL_LOGO);
2159 out:
2160 	lpfc_els_free_iocb(phba, cmdiocb);
2161 	return;
2162 }
2163 
2164 /**
2165  * lpfc_issue_els_logo - Issue a logo to an node on a vport
2166  * @vport: pointer to a virtual N_Port data structure.
2167  * @ndlp: pointer to a node-list data structure.
2168  * @retry: number of retries to the command IOCB.
2169  *
2170  * This routine constructs and issues an ELS Logout (LOGO) iocb command
2171  * to a remote node, referred by an @ndlp on a @vport. It constructs the
2172  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2173  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2174  *
2175  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2176  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2177  * will be stored into the context1 field of the IOCB for the completion
2178  * callback function to the LOGO ELS command.
2179  *
2180  * Return code
2181  *   0 - successfully issued logo
2182  *   1 - failed to issue logo
2183  **/
2184 int
2185 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2186 		    uint8_t retry)
2187 {
2188 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2189 	struct lpfc_hba  *phba = vport->phba;
2190 	IOCB_t *icmd;
2191 	struct lpfc_iocbq *elsiocb;
2192 	uint8_t *pcmd;
2193 	uint16_t cmdsize;
2194 	int rc;
2195 
2196 	spin_lock_irq(shost->host_lock);
2197 	if (ndlp->nlp_flag & NLP_LOGO_SND) {
2198 		spin_unlock_irq(shost->host_lock);
2199 		return 0;
2200 	}
2201 	spin_unlock_irq(shost->host_lock);
2202 
2203 	cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2204 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2205 				     ndlp->nlp_DID, ELS_CMD_LOGO);
2206 	if (!elsiocb)
2207 		return 1;
2208 
2209 	icmd = &elsiocb->iocb;
2210 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2211 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
2212 	pcmd += sizeof(uint32_t);
2213 
2214 	/* Fill in LOGO payload */
2215 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
2216 	pcmd += sizeof(uint32_t);
2217 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
2218 
2219 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2220 		"Issue LOGO:      did:x%x",
2221 		ndlp->nlp_DID, 0, 0);
2222 
2223 	phba->fc_stat.elsXmitLOGO++;
2224 	elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2225 	spin_lock_irq(shost->host_lock);
2226 	ndlp->nlp_flag |= NLP_LOGO_SND;
2227 	spin_unlock_irq(shost->host_lock);
2228 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2229 
2230 	if (rc == IOCB_ERROR) {
2231 		spin_lock_irq(shost->host_lock);
2232 		ndlp->nlp_flag &= ~NLP_LOGO_SND;
2233 		spin_unlock_irq(shost->host_lock);
2234 		lpfc_els_free_iocb(phba, elsiocb);
2235 		return 1;
2236 	}
2237 	return 0;
2238 }
2239 
2240 /**
2241  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
2242  * @phba: pointer to lpfc hba data structure.
2243  * @cmdiocb: pointer to lpfc command iocb data structure.
2244  * @rspiocb: pointer to lpfc response iocb data structure.
2245  *
2246  * This routine is a generic completion callback function for ELS commands.
2247  * Specifically, it is the callback function which does not need to perform
2248  * any command specific operations. It is currently used by the ELS command
2249  * issuing routines for the ELS State Change  Request (SCR),
2250  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2251  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2252  * certain debug loggings, this callback function simply invokes the
2253  * lpfc_els_chk_latt() routine to check whether link went down during the
2254  * discovery process.
2255  **/
2256 static void
2257 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2258 		  struct lpfc_iocbq *rspiocb)
2259 {
2260 	struct lpfc_vport *vport = cmdiocb->vport;
2261 	IOCB_t *irsp;
2262 
2263 	irsp = &rspiocb->iocb;
2264 
2265 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2266 		"ELS cmd cmpl:    status:x%x/x%x did:x%x",
2267 		irsp->ulpStatus, irsp->un.ulpWord[4],
2268 		irsp->un.elsreq64.remoteID);
2269 	/* ELS cmd tag <ulpIoTag> completes */
2270 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2271 			 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2272 			 irsp->ulpIoTag, irsp->ulpStatus,
2273 			 irsp->un.ulpWord[4], irsp->ulpTimeout);
2274 	/* Check to see if link went down during discovery */
2275 	lpfc_els_chk_latt(vport);
2276 	lpfc_els_free_iocb(phba, cmdiocb);
2277 	return;
2278 }
2279 
2280 /**
2281  * lpfc_issue_els_scr - Issue a scr to an node on a vport
2282  * @vport: pointer to a host virtual N_Port data structure.
2283  * @nportid: N_Port identifier to the remote node.
2284  * @retry: number of retries to the command IOCB.
2285  *
2286  * This routine issues a State Change Request (SCR) to a fabric node
2287  * on a @vport. The remote node @nportid is passed into the function. It
2288  * first search the @vport node list to find the matching ndlp. If no such
2289  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2290  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2291  * routine is invoked to send the SCR IOCB.
2292  *
2293  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2294  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2295  * will be stored into the context1 field of the IOCB for the completion
2296  * callback function to the SCR ELS command.
2297  *
2298  * Return code
2299  *   0 - Successfully issued scr command
2300  *   1 - Failed to issue scr command
2301  **/
2302 int
2303 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2304 {
2305 	struct lpfc_hba  *phba = vport->phba;
2306 	IOCB_t *icmd;
2307 	struct lpfc_iocbq *elsiocb;
2308 	struct lpfc_sli *psli;
2309 	uint8_t *pcmd;
2310 	uint16_t cmdsize;
2311 	struct lpfc_nodelist *ndlp;
2312 
2313 	psli = &phba->sli;
2314 	cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2315 
2316 	ndlp = lpfc_findnode_did(vport, nportid);
2317 	if (!ndlp) {
2318 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2319 		if (!ndlp)
2320 			return 1;
2321 		lpfc_nlp_init(vport, ndlp, nportid);
2322 		lpfc_enqueue_node(vport, ndlp);
2323 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
2324 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2325 		if (!ndlp)
2326 			return 1;
2327 	}
2328 
2329 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2330 				     ndlp->nlp_DID, ELS_CMD_SCR);
2331 
2332 	if (!elsiocb) {
2333 		/* This will trigger the release of the node just
2334 		 * allocated
2335 		 */
2336 		lpfc_nlp_put(ndlp);
2337 		return 1;
2338 	}
2339 
2340 	icmd = &elsiocb->iocb;
2341 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2342 
2343 	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2344 	pcmd += sizeof(uint32_t);
2345 
2346 	/* For SCR, remainder of payload is SCR parameter page */
2347 	memset(pcmd, 0, sizeof(SCR));
2348 	((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2349 
2350 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2351 		"Issue SCR:       did:x%x",
2352 		ndlp->nlp_DID, 0, 0);
2353 
2354 	phba->fc_stat.elsXmitSCR++;
2355 	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2356 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2357 	    IOCB_ERROR) {
2358 		/* The additional lpfc_nlp_put will cause the following
2359 		 * lpfc_els_free_iocb routine to trigger the rlease of
2360 		 * the node.
2361 		 */
2362 		lpfc_nlp_put(ndlp);
2363 		lpfc_els_free_iocb(phba, elsiocb);
2364 		return 1;
2365 	}
2366 	/* This will cause the callback-function lpfc_cmpl_els_cmd to
2367 	 * trigger the release of node.
2368 	 */
2369 	lpfc_nlp_put(ndlp);
2370 	return 0;
2371 }
2372 
2373 /**
2374  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
2375  * @vport: pointer to a host virtual N_Port data structure.
2376  * @nportid: N_Port identifier to the remote node.
2377  * @retry: number of retries to the command IOCB.
2378  *
2379  * This routine issues a Fibre Channel Address Resolution Response
2380  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2381  * is passed into the function. It first search the @vport node list to find
2382  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2383  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2384  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2385  *
2386  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2387  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2388  * will be stored into the context1 field of the IOCB for the completion
2389  * callback function to the PARPR ELS command.
2390  *
2391  * Return code
2392  *   0 - Successfully issued farpr command
2393  *   1 - Failed to issue farpr command
2394  **/
2395 static int
2396 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2397 {
2398 	struct lpfc_hba  *phba = vport->phba;
2399 	IOCB_t *icmd;
2400 	struct lpfc_iocbq *elsiocb;
2401 	struct lpfc_sli *psli;
2402 	FARP *fp;
2403 	uint8_t *pcmd;
2404 	uint32_t *lp;
2405 	uint16_t cmdsize;
2406 	struct lpfc_nodelist *ondlp;
2407 	struct lpfc_nodelist *ndlp;
2408 
2409 	psli = &phba->sli;
2410 	cmdsize = (sizeof(uint32_t) + sizeof(FARP));
2411 
2412 	ndlp = lpfc_findnode_did(vport, nportid);
2413 	if (!ndlp) {
2414 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2415 		if (!ndlp)
2416 			return 1;
2417 		lpfc_nlp_init(vport, ndlp, nportid);
2418 		lpfc_enqueue_node(vport, ndlp);
2419 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
2420 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2421 		if (!ndlp)
2422 			return 1;
2423 	}
2424 
2425 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2426 				     ndlp->nlp_DID, ELS_CMD_RNID);
2427 	if (!elsiocb) {
2428 		/* This will trigger the release of the node just
2429 		 * allocated
2430 		 */
2431 		lpfc_nlp_put(ndlp);
2432 		return 1;
2433 	}
2434 
2435 	icmd = &elsiocb->iocb;
2436 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2437 
2438 	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
2439 	pcmd += sizeof(uint32_t);
2440 
2441 	/* Fill in FARPR payload */
2442 	fp = (FARP *) (pcmd);
2443 	memset(fp, 0, sizeof(FARP));
2444 	lp = (uint32_t *) pcmd;
2445 	*lp++ = be32_to_cpu(nportid);
2446 	*lp++ = be32_to_cpu(vport->fc_myDID);
2447 	fp->Rflags = 0;
2448 	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2449 
2450 	memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2451 	memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2452 	ondlp = lpfc_findnode_did(vport, nportid);
2453 	if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
2454 		memcpy(&fp->OportName, &ondlp->nlp_portname,
2455 		       sizeof(struct lpfc_name));
2456 		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
2457 		       sizeof(struct lpfc_name));
2458 	}
2459 
2460 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2461 		"Issue FARPR:     did:x%x",
2462 		ndlp->nlp_DID, 0, 0);
2463 
2464 	phba->fc_stat.elsXmitFARPR++;
2465 	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2466 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2467 	    IOCB_ERROR) {
2468 		/* The additional lpfc_nlp_put will cause the following
2469 		 * lpfc_els_free_iocb routine to trigger the release of
2470 		 * the node.
2471 		 */
2472 		lpfc_nlp_put(ndlp);
2473 		lpfc_els_free_iocb(phba, elsiocb);
2474 		return 1;
2475 	}
2476 	/* This will cause the callback-function lpfc_cmpl_els_cmd to
2477 	 * trigger the release of the node.
2478 	 */
2479 	lpfc_nlp_put(ndlp);
2480 	return 0;
2481 }
2482 
2483 /**
2484  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
2485  * @vport: pointer to a host virtual N_Port data structure.
2486  * @nlp: pointer to a node-list data structure.
2487  *
2488  * This routine cancels the timer with a delayed IOCB-command retry for
2489  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2490  * removes the ELS retry event if it presents. In addition, if the
2491  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2492  * commands are sent for the @vport's nodes that require issuing discovery
2493  * ADISC.
2494  **/
2495 void
2496 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
2497 {
2498 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2499 	struct lpfc_work_evt *evtp;
2500 
2501 	if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2502 		return;
2503 	spin_lock_irq(shost->host_lock);
2504 	nlp->nlp_flag &= ~NLP_DELAY_TMO;
2505 	spin_unlock_irq(shost->host_lock);
2506 	del_timer_sync(&nlp->nlp_delayfunc);
2507 	nlp->nlp_last_elscmd = 0;
2508 	if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
2509 		list_del_init(&nlp->els_retry_evt.evt_listp);
2510 		/* Decrement nlp reference count held for the delayed retry */
2511 		evtp = &nlp->els_retry_evt;
2512 		lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2513 	}
2514 	if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2515 		spin_lock_irq(shost->host_lock);
2516 		nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2517 		spin_unlock_irq(shost->host_lock);
2518 		if (vport->num_disc_nodes) {
2519 			if (vport->port_state < LPFC_VPORT_READY) {
2520 				/* Check if there are more ADISCs to be sent */
2521 				lpfc_more_adisc(vport);
2522 			} else {
2523 				/* Check if there are more PLOGIs to be sent */
2524 				lpfc_more_plogi(vport);
2525 				if (vport->num_disc_nodes == 0) {
2526 					spin_lock_irq(shost->host_lock);
2527 					vport->fc_flag &= ~FC_NDISC_ACTIVE;
2528 					spin_unlock_irq(shost->host_lock);
2529 					lpfc_can_disctmo(vport);
2530 					lpfc_end_rscn(vport);
2531 				}
2532 			}
2533 		}
2534 	}
2535 	return;
2536 }
2537 
2538 /**
2539  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
2540  * @ptr: holder for the pointer to the timer function associated data (ndlp).
2541  *
2542  * This routine is invoked by the ndlp delayed-function timer to check
2543  * whether there is any pending ELS retry event(s) with the node. If not, it
2544  * simply returns. Otherwise, if there is at least one ELS delayed event, it
2545  * adds the delayed events to the HBA work list and invokes the
2546  * lpfc_worker_wake_up() routine to wake up worker thread to process the
2547  * event. Note that lpfc_nlp_get() is called before posting the event to
2548  * the work list to hold reference count of ndlp so that it guarantees the
2549  * reference to ndlp will still be available when the worker thread gets
2550  * to the event associated with the ndlp.
2551  **/
2552 void
2553 lpfc_els_retry_delay(unsigned long ptr)
2554 {
2555 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2556 	struct lpfc_vport *vport = ndlp->vport;
2557 	struct lpfc_hba   *phba = vport->phba;
2558 	unsigned long flags;
2559 	struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
2560 
2561 	spin_lock_irqsave(&phba->hbalock, flags);
2562 	if (!list_empty(&evtp->evt_listp)) {
2563 		spin_unlock_irqrestore(&phba->hbalock, flags);
2564 		return;
2565 	}
2566 
2567 	/* We need to hold the node by incrementing the reference
2568 	 * count until the queued work is done
2569 	 */
2570 	evtp->evt_arg1  = lpfc_nlp_get(ndlp);
2571 	if (evtp->evt_arg1) {
2572 		evtp->evt = LPFC_EVT_ELS_RETRY;
2573 		list_add_tail(&evtp->evt_listp, &phba->work_list);
2574 		lpfc_worker_wake_up(phba);
2575 	}
2576 	spin_unlock_irqrestore(&phba->hbalock, flags);
2577 	return;
2578 }
2579 
2580 /**
2581  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
2582  * @ndlp: pointer to a node-list data structure.
2583  *
2584  * This routine is the worker-thread handler for processing the @ndlp delayed
2585  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2586  * the last ELS command from the associated ndlp and invokes the proper ELS
2587  * function according to the delayed ELS command to retry the command.
2588  **/
2589 void
2590 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
2591 {
2592 	struct lpfc_vport *vport = ndlp->vport;
2593 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2594 	uint32_t cmd, did, retry;
2595 
2596 	spin_lock_irq(shost->host_lock);
2597 	did = ndlp->nlp_DID;
2598 	cmd = ndlp->nlp_last_elscmd;
2599 	ndlp->nlp_last_elscmd = 0;
2600 
2601 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2602 		spin_unlock_irq(shost->host_lock);
2603 		return;
2604 	}
2605 
2606 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2607 	spin_unlock_irq(shost->host_lock);
2608 	/*
2609 	 * If a discovery event readded nlp_delayfunc after timer
2610 	 * firing and before processing the timer, cancel the
2611 	 * nlp_delayfunc.
2612 	 */
2613 	del_timer_sync(&ndlp->nlp_delayfunc);
2614 	retry = ndlp->nlp_retry;
2615 	ndlp->nlp_retry = 0;
2616 
2617 	switch (cmd) {
2618 	case ELS_CMD_FLOGI:
2619 		lpfc_issue_els_flogi(vport, ndlp, retry);
2620 		break;
2621 	case ELS_CMD_PLOGI:
2622 		if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
2623 			ndlp->nlp_prev_state = ndlp->nlp_state;
2624 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2625 		}
2626 		break;
2627 	case ELS_CMD_ADISC:
2628 		if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
2629 			ndlp->nlp_prev_state = ndlp->nlp_state;
2630 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2631 		}
2632 		break;
2633 	case ELS_CMD_PRLI:
2634 		if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
2635 			ndlp->nlp_prev_state = ndlp->nlp_state;
2636 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2637 		}
2638 		break;
2639 	case ELS_CMD_LOGO:
2640 		if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
2641 			ndlp->nlp_prev_state = ndlp->nlp_state;
2642 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2643 		}
2644 		break;
2645 	case ELS_CMD_FDISC:
2646 		lpfc_issue_els_fdisc(vport, ndlp, retry);
2647 		break;
2648 	}
2649 	return;
2650 }
2651 
2652 /**
2653  * lpfc_els_retry - Make retry decision on an els command iocb
2654  * @phba: pointer to lpfc hba data structure.
2655  * @cmdiocb: pointer to lpfc command iocb data structure.
2656  * @rspiocb: pointer to lpfc response iocb data structure.
2657  *
2658  * This routine makes a retry decision on an ELS command IOCB, which has
2659  * failed. The following ELS IOCBs use this function for retrying the command
2660  * when previously issued command responsed with error status: FLOGI, PLOGI,
2661  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
2662  * returned error status, it makes the decision whether a retry shall be
2663  * issued for the command, and whether a retry shall be made immediately or
2664  * delayed. In the former case, the corresponding ELS command issuing-function
2665  * is called to retry the command. In the later case, the ELS command shall
2666  * be posted to the ndlp delayed event and delayed function timer set to the
2667  * ndlp for the delayed command issusing.
2668  *
2669  * Return code
2670  *   0 - No retry of els command is made
2671  *   1 - Immediate or delayed retry of els command is made
2672  **/
2673 static int
2674 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2675 	       struct lpfc_iocbq *rspiocb)
2676 {
2677 	struct lpfc_vport *vport = cmdiocb->vport;
2678 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2679 	IOCB_t *irsp = &rspiocb->iocb;
2680 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2681 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2682 	uint32_t *elscmd;
2683 	struct ls_rjt stat;
2684 	int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
2685 	int logerr = 0;
2686 	uint32_t cmd = 0;
2687 	uint32_t did;
2688 
2689 
2690 	/* Note: context2 may be 0 for internal driver abort
2691 	 * of delays ELS command.
2692 	 */
2693 
2694 	if (pcmd && pcmd->virt) {
2695 		elscmd = (uint32_t *) (pcmd->virt);
2696 		cmd = *elscmd++;
2697 	}
2698 
2699 	if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2700 		did = ndlp->nlp_DID;
2701 	else {
2702 		/* We should only hit this case for retrying PLOGI */
2703 		did = irsp->un.elsreq64.remoteID;
2704 		ndlp = lpfc_findnode_did(vport, did);
2705 		if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2706 		    && (cmd != ELS_CMD_PLOGI))
2707 			return 1;
2708 	}
2709 
2710 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2711 		"Retry ELS:       wd7:x%x wd4:x%x did:x%x",
2712 		*(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
2713 
2714 	switch (irsp->ulpStatus) {
2715 	case IOSTAT_FCP_RSP_ERROR:
2716 	case IOSTAT_REMOTE_STOP:
2717 		break;
2718 
2719 	case IOSTAT_LOCAL_REJECT:
2720 		switch ((irsp->un.ulpWord[4] & 0xff)) {
2721 		case IOERR_LOOP_OPEN_FAILURE:
2722 			if (cmd == ELS_CMD_FLOGI) {
2723 				if (PCI_DEVICE_ID_HORNET ==
2724 					phba->pcidev->device) {
2725 					phba->fc_topology = TOPOLOGY_LOOP;
2726 					phba->pport->fc_myDID = 0;
2727 					phba->alpa_map[0] = 0;
2728 					phba->alpa_map[1] = 0;
2729 				}
2730 			}
2731 			if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
2732 				delay = 1000;
2733 			retry = 1;
2734 			break;
2735 
2736 		case IOERR_ILLEGAL_COMMAND:
2737 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2738 					 "0124 Retry illegal cmd x%x "
2739 					 "retry:x%x delay:x%x\n",
2740 					 cmd, cmdiocb->retry, delay);
2741 			retry = 1;
2742 			/* All command's retry policy */
2743 			maxretry = 8;
2744 			if (cmdiocb->retry > 2)
2745 				delay = 1000;
2746 			break;
2747 
2748 		case IOERR_NO_RESOURCES:
2749 			logerr = 1; /* HBA out of resources */
2750 			retry = 1;
2751 			if (cmdiocb->retry > 100)
2752 				delay = 100;
2753 			maxretry = 250;
2754 			break;
2755 
2756 		case IOERR_ILLEGAL_FRAME:
2757 			delay = 100;
2758 			retry = 1;
2759 			break;
2760 
2761 		case IOERR_SEQUENCE_TIMEOUT:
2762 		case IOERR_INVALID_RPI:
2763 			retry = 1;
2764 			break;
2765 		}
2766 		break;
2767 
2768 	case IOSTAT_NPORT_RJT:
2769 	case IOSTAT_FABRIC_RJT:
2770 		if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
2771 			retry = 1;
2772 			break;
2773 		}
2774 		break;
2775 
2776 	case IOSTAT_NPORT_BSY:
2777 	case IOSTAT_FABRIC_BSY:
2778 		logerr = 1; /* Fabric / Remote NPort out of resources */
2779 		retry = 1;
2780 		break;
2781 
2782 	case IOSTAT_LS_RJT:
2783 		stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
2784 		/* Added for Vendor specifc support
2785 		 * Just keep retrying for these Rsn / Exp codes
2786 		 */
2787 		switch (stat.un.b.lsRjtRsnCode) {
2788 		case LSRJT_UNABLE_TPC:
2789 			if (stat.un.b.lsRjtRsnCodeExp ==
2790 			    LSEXP_CMD_IN_PROGRESS) {
2791 				if (cmd == ELS_CMD_PLOGI) {
2792 					delay = 1000;
2793 					maxretry = 48;
2794 				}
2795 				retry = 1;
2796 				break;
2797 			}
2798 			if (stat.un.b.lsRjtRsnCodeExp ==
2799 			    LSEXP_CANT_GIVE_DATA) {
2800 				if (cmd == ELS_CMD_PLOGI) {
2801 					delay = 1000;
2802 					maxretry = 48;
2803 				}
2804 				retry = 1;
2805 				break;
2806 			}
2807 			if (cmd == ELS_CMD_PLOGI) {
2808 				delay = 1000;
2809 				maxretry = lpfc_max_els_tries + 1;
2810 				retry = 1;
2811 				break;
2812 			}
2813 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2814 			  (cmd == ELS_CMD_FDISC) &&
2815 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
2816 				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2817 						 "0125 FDISC Failed (x%x). "
2818 						 "Fabric out of resources\n",
2819 						 stat.un.lsRjtError);
2820 				lpfc_vport_set_state(vport,
2821 						     FC_VPORT_NO_FABRIC_RSCS);
2822 			}
2823 			break;
2824 
2825 		case LSRJT_LOGICAL_BSY:
2826 			if ((cmd == ELS_CMD_PLOGI) ||
2827 			    (cmd == ELS_CMD_PRLI)) {
2828 				delay = 1000;
2829 				maxretry = 48;
2830 			} else if (cmd == ELS_CMD_FDISC) {
2831 				/* FDISC retry policy */
2832 				maxretry = 48;
2833 				if (cmdiocb->retry >= 32)
2834 					delay = 1000;
2835 			}
2836 			retry = 1;
2837 			break;
2838 
2839 		case LSRJT_LOGICAL_ERR:
2840 			/* There are some cases where switches return this
2841 			 * error when they are not ready and should be returning
2842 			 * Logical Busy. We should delay every time.
2843 			 */
2844 			if (cmd == ELS_CMD_FDISC &&
2845 			    stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
2846 				maxretry = 3;
2847 				delay = 1000;
2848 				retry = 1;
2849 				break;
2850 			}
2851 		case LSRJT_PROTOCOL_ERR:
2852 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2853 			  (cmd == ELS_CMD_FDISC) &&
2854 			  ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
2855 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
2856 			  ) {
2857 				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2858 						 "0122 FDISC Failed (x%x). "
2859 						 "Fabric Detected Bad WWN\n",
2860 						 stat.un.lsRjtError);
2861 				lpfc_vport_set_state(vport,
2862 						     FC_VPORT_FABRIC_REJ_WWN);
2863 			}
2864 			break;
2865 		}
2866 		break;
2867 
2868 	case IOSTAT_INTERMED_RSP:
2869 	case IOSTAT_BA_RJT:
2870 		break;
2871 
2872 	default:
2873 		break;
2874 	}
2875 
2876 	if (did == FDMI_DID)
2877 		retry = 1;
2878 
2879 	if (((cmd == ELS_CMD_FLOGI) || (cmd == ELS_CMD_FDISC)) &&
2880 	    (phba->fc_topology != TOPOLOGY_LOOP) &&
2881 	    !lpfc_error_lost_link(irsp)) {
2882 		/* FLOGI retry policy */
2883 		retry = 1;
2884 		/* retry forever */
2885 		maxretry = 0;
2886 		if (cmdiocb->retry >= 100)
2887 			delay = 5000;
2888 		else if (cmdiocb->retry >= 32)
2889 			delay = 1000;
2890 	}
2891 
2892 	cmdiocb->retry++;
2893 	if (maxretry && (cmdiocb->retry >= maxretry)) {
2894 		phba->fc_stat.elsRetryExceeded++;
2895 		retry = 0;
2896 	}
2897 
2898 	if ((vport->load_flag & FC_UNLOADING) != 0)
2899 		retry = 0;
2900 
2901 	if (retry) {
2902 		if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
2903 			/* Stop retrying PLOGI and FDISC if in FCF discovery */
2904 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
2905 				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2906 						 "2849 Stop retry ELS command "
2907 						 "x%x to remote NPORT x%x, "
2908 						 "Data: x%x x%x\n", cmd, did,
2909 						 cmdiocb->retry, delay);
2910 				return 0;
2911 			}
2912 		}
2913 
2914 		/* Retry ELS command <elsCmd> to remote NPORT <did> */
2915 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2916 				 "0107 Retry ELS command x%x to remote "
2917 				 "NPORT x%x Data: x%x x%x\n",
2918 				 cmd, did, cmdiocb->retry, delay);
2919 
2920 		if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
2921 			((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
2922 			((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
2923 			/* Don't reset timer for no resources */
2924 
2925 			/* If discovery / RSCN timer is running, reset it */
2926 			if (timer_pending(&vport->fc_disctmo) ||
2927 			    (vport->fc_flag & FC_RSCN_MODE))
2928 				lpfc_set_disctmo(vport);
2929 		}
2930 
2931 		phba->fc_stat.elsXmitRetry++;
2932 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
2933 			phba->fc_stat.elsDelayRetry++;
2934 			ndlp->nlp_retry = cmdiocb->retry;
2935 
2936 			/* delay is specified in milliseconds */
2937 			mod_timer(&ndlp->nlp_delayfunc,
2938 				jiffies + msecs_to_jiffies(delay));
2939 			spin_lock_irq(shost->host_lock);
2940 			ndlp->nlp_flag |= NLP_DELAY_TMO;
2941 			spin_unlock_irq(shost->host_lock);
2942 
2943 			ndlp->nlp_prev_state = ndlp->nlp_state;
2944 			if (cmd == ELS_CMD_PRLI)
2945 				lpfc_nlp_set_state(vport, ndlp,
2946 					NLP_STE_REG_LOGIN_ISSUE);
2947 			else
2948 				lpfc_nlp_set_state(vport, ndlp,
2949 					NLP_STE_NPR_NODE);
2950 			ndlp->nlp_last_elscmd = cmd;
2951 
2952 			return 1;
2953 		}
2954 		switch (cmd) {
2955 		case ELS_CMD_FLOGI:
2956 			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2957 			return 1;
2958 		case ELS_CMD_FDISC:
2959 			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
2960 			return 1;
2961 		case ELS_CMD_PLOGI:
2962 			if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2963 				ndlp->nlp_prev_state = ndlp->nlp_state;
2964 				lpfc_nlp_set_state(vport, ndlp,
2965 						   NLP_STE_PLOGI_ISSUE);
2966 			}
2967 			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2968 			return 1;
2969 		case ELS_CMD_ADISC:
2970 			ndlp->nlp_prev_state = ndlp->nlp_state;
2971 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2972 			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2973 			return 1;
2974 		case ELS_CMD_PRLI:
2975 			ndlp->nlp_prev_state = ndlp->nlp_state;
2976 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2977 			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2978 			return 1;
2979 		case ELS_CMD_LOGO:
2980 			ndlp->nlp_prev_state = ndlp->nlp_state;
2981 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2982 			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2983 			return 1;
2984 		}
2985 	}
2986 	/* No retry ELS command <elsCmd> to remote NPORT <did> */
2987 	if (logerr) {
2988 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2989 			 "0137 No retry ELS command x%x to remote "
2990 			 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
2991 			 cmd, did, irsp->ulpStatus,
2992 			 irsp->un.ulpWord[4]);
2993 	}
2994 	else {
2995 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2996 			 "0108 No retry ELS command x%x to remote "
2997 			 "NPORT x%x Retried:%d Error:x%x/%x\n",
2998 			 cmd, did, cmdiocb->retry, irsp->ulpStatus,
2999 			 irsp->un.ulpWord[4]);
3000 	}
3001 	return 0;
3002 }
3003 
3004 /**
3005  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
3006  * @phba: pointer to lpfc hba data structure.
3007  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
3008  *
3009  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
3010  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
3011  * checks to see whether there is a lpfc DMA buffer associated with the
3012  * response of the command IOCB. If so, it will be released before releasing
3013  * the lpfc DMA buffer associated with the IOCB itself.
3014  *
3015  * Return code
3016  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
3017  **/
3018 static int
3019 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
3020 {
3021 	struct lpfc_dmabuf *buf_ptr;
3022 
3023 	/* Free the response before processing the command. */
3024 	if (!list_empty(&buf_ptr1->list)) {
3025 		list_remove_head(&buf_ptr1->list, buf_ptr,
3026 				 struct lpfc_dmabuf,
3027 				 list);
3028 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3029 		kfree(buf_ptr);
3030 	}
3031 	lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
3032 	kfree(buf_ptr1);
3033 	return 0;
3034 }
3035 
3036 /**
3037  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
3038  * @phba: pointer to lpfc hba data structure.
3039  * @buf_ptr: pointer to the lpfc dma buffer data structure.
3040  *
3041  * This routine releases the lpfc Direct Memory Access (DMA) buffer
3042  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
3043  * pool.
3044  *
3045  * Return code
3046  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
3047  **/
3048 static int
3049 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
3050 {
3051 	lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3052 	kfree(buf_ptr);
3053 	return 0;
3054 }
3055 
3056 /**
3057  * lpfc_els_free_iocb - Free a command iocb and its associated resources
3058  * @phba: pointer to lpfc hba data structure.
3059  * @elsiocb: pointer to lpfc els command iocb data structure.
3060  *
3061  * This routine frees a command IOCB and its associated resources. The
3062  * command IOCB data structure contains the reference to various associated
3063  * resources, these fields must be set to NULL if the associated reference
3064  * not present:
3065  *   context1 - reference to ndlp
3066  *   context2 - reference to cmd
3067  *   context2->next - reference to rsp
3068  *   context3 - reference to bpl
3069  *
3070  * It first properly decrements the reference count held on ndlp for the
3071  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
3072  * set, it invokes the lpfc_els_free_data() routine to release the Direct
3073  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
3074  * adds the DMA buffer the @phba data structure for the delayed release.
3075  * If reference to the Buffer Pointer List (BPL) is present, the
3076  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
3077  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
3078  * invoked to release the IOCB data structure back to @phba IOCBQ list.
3079  *
3080  * Return code
3081  *   0 - Success (currently, always return 0)
3082  **/
3083 int
3084 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
3085 {
3086 	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
3087 	struct lpfc_nodelist *ndlp;
3088 
3089 	ndlp = (struct lpfc_nodelist *)elsiocb->context1;
3090 	if (ndlp) {
3091 		if (ndlp->nlp_flag & NLP_DEFER_RM) {
3092 			lpfc_nlp_put(ndlp);
3093 
3094 			/* If the ndlp is not being used by another discovery
3095 			 * thread, free it.
3096 			 */
3097 			if (!lpfc_nlp_not_used(ndlp)) {
3098 				/* If ndlp is being used by another discovery
3099 				 * thread, just clear NLP_DEFER_RM
3100 				 */
3101 				ndlp->nlp_flag &= ~NLP_DEFER_RM;
3102 			}
3103 		}
3104 		else
3105 			lpfc_nlp_put(ndlp);
3106 		elsiocb->context1 = NULL;
3107 	}
3108 	/* context2  = cmd,  context2->next = rsp, context3 = bpl */
3109 	if (elsiocb->context2) {
3110 		if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
3111 			/* Firmware could still be in progress of DMAing
3112 			 * payload, so don't free data buffer till after
3113 			 * a hbeat.
3114 			 */
3115 			elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
3116 			buf_ptr = elsiocb->context2;
3117 			elsiocb->context2 = NULL;
3118 			if (buf_ptr) {
3119 				buf_ptr1 = NULL;
3120 				spin_lock_irq(&phba->hbalock);
3121 				if (!list_empty(&buf_ptr->list)) {
3122 					list_remove_head(&buf_ptr->list,
3123 						buf_ptr1, struct lpfc_dmabuf,
3124 						list);
3125 					INIT_LIST_HEAD(&buf_ptr1->list);
3126 					list_add_tail(&buf_ptr1->list,
3127 						&phba->elsbuf);
3128 					phba->elsbuf_cnt++;
3129 				}
3130 				INIT_LIST_HEAD(&buf_ptr->list);
3131 				list_add_tail(&buf_ptr->list, &phba->elsbuf);
3132 				phba->elsbuf_cnt++;
3133 				spin_unlock_irq(&phba->hbalock);
3134 			}
3135 		} else {
3136 			buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
3137 			lpfc_els_free_data(phba, buf_ptr1);
3138 		}
3139 	}
3140 
3141 	if (elsiocb->context3) {
3142 		buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
3143 		lpfc_els_free_bpl(phba, buf_ptr);
3144 	}
3145 	lpfc_sli_release_iocbq(phba, elsiocb);
3146 	return 0;
3147 }
3148 
3149 /**
3150  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
3151  * @phba: pointer to lpfc hba data structure.
3152  * @cmdiocb: pointer to lpfc command iocb data structure.
3153  * @rspiocb: pointer to lpfc response iocb data structure.
3154  *
3155  * This routine is the completion callback function to the Logout (LOGO)
3156  * Accept (ACC) Response ELS command. This routine is invoked to indicate
3157  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
3158  * release the ndlp if it has the last reference remaining (reference count
3159  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
3160  * field to NULL to inform the following lpfc_els_free_iocb() routine no
3161  * ndlp reference count needs to be decremented. Otherwise, the ndlp
3162  * reference use-count shall be decremented by the lpfc_els_free_iocb()
3163  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
3164  * IOCB data structure.
3165  **/
3166 static void
3167 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3168 		       struct lpfc_iocbq *rspiocb)
3169 {
3170 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3171 	struct lpfc_vport *vport = cmdiocb->vport;
3172 	IOCB_t *irsp;
3173 
3174 	irsp = &rspiocb->iocb;
3175 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3176 		"ACC LOGO cmpl:   status:x%x/x%x did:x%x",
3177 		irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
3178 	/* ACC to LOGO completes to NPort <nlp_DID> */
3179 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3180 			 "0109 ACC to LOGO completes to NPort x%x "
3181 			 "Data: x%x x%x x%x\n",
3182 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3183 			 ndlp->nlp_rpi);
3184 
3185 	if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
3186 		/* NPort Recovery mode or node is just allocated */
3187 		if (!lpfc_nlp_not_used(ndlp)) {
3188 			/* If the ndlp is being used by another discovery
3189 			 * thread, just unregister the RPI.
3190 			 */
3191 			lpfc_unreg_rpi(vport, ndlp);
3192 		} else {
3193 			/* Indicate the node has already released, should
3194 			 * not reference to it from within lpfc_els_free_iocb.
3195 			 */
3196 			cmdiocb->context1 = NULL;
3197 		}
3198 	}
3199 	lpfc_els_free_iocb(phba, cmdiocb);
3200 	return;
3201 }
3202 
3203 /**
3204  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
3205  * @phba: pointer to lpfc hba data structure.
3206  * @pmb: pointer to the driver internal queue element for mailbox command.
3207  *
3208  * This routine is the completion callback function for unregister default
3209  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
3210  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
3211  * decrements the ndlp reference count held for this completion callback
3212  * function. After that, it invokes the lpfc_nlp_not_used() to check
3213  * whether there is only one reference left on the ndlp. If so, it will
3214  * perform one more decrement and trigger the release of the ndlp.
3215  **/
3216 void
3217 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3218 {
3219 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3220 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3221 
3222 	/*
3223 	 * This routine is used to register and unregister in previous SLI
3224 	 * modes.
3225 	 */
3226 	if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
3227 	    (phba->sli_rev == LPFC_SLI_REV4))
3228 		lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
3229 
3230 	pmb->context1 = NULL;
3231 	pmb->context2 = NULL;
3232 
3233 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3234 	kfree(mp);
3235 	mempool_free(pmb, phba->mbox_mem_pool);
3236 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3237 		lpfc_nlp_put(ndlp);
3238 		/* This is the end of the default RPI cleanup logic for this
3239 		 * ndlp. If no other discovery threads are using this ndlp.
3240 		 * we should free all resources associated with it.
3241 		 */
3242 		lpfc_nlp_not_used(ndlp);
3243 	}
3244 
3245 	return;
3246 }
3247 
3248 /**
3249  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
3250  * @phba: pointer to lpfc hba data structure.
3251  * @cmdiocb: pointer to lpfc command iocb data structure.
3252  * @rspiocb: pointer to lpfc response iocb data structure.
3253  *
3254  * This routine is the completion callback function for ELS Response IOCB
3255  * command. In normal case, this callback function just properly sets the
3256  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
3257  * field in the command IOCB is not NULL, the referred mailbox command will
3258  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
3259  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
3260  * link down event occurred during the discovery, the lpfc_nlp_not_used()
3261  * routine shall be invoked trying to release the ndlp if no other threads
3262  * are currently referring it.
3263  **/
3264 static void
3265 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3266 		  struct lpfc_iocbq *rspiocb)
3267 {
3268 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3269 	struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
3270 	struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
3271 	IOCB_t  *irsp;
3272 	uint8_t *pcmd;
3273 	LPFC_MBOXQ_t *mbox = NULL;
3274 	struct lpfc_dmabuf *mp = NULL;
3275 	uint32_t ls_rjt = 0;
3276 
3277 	irsp = &rspiocb->iocb;
3278 
3279 	if (cmdiocb->context_un.mbox)
3280 		mbox = cmdiocb->context_un.mbox;
3281 
3282 	/* First determine if this is a LS_RJT cmpl. Note, this callback
3283 	 * function can have cmdiocb->contest1 (ndlp) field set to NULL.
3284 	 */
3285 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
3286 	if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3287 	    (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
3288 		/* A LS_RJT associated with Default RPI cleanup has its own
3289 		 * separate code path.
3290 		 */
3291 		if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3292 			ls_rjt = 1;
3293 	}
3294 
3295 	/* Check to see if link went down during discovery */
3296 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
3297 		if (mbox) {
3298 			mp = (struct lpfc_dmabuf *) mbox->context1;
3299 			if (mp) {
3300 				lpfc_mbuf_free(phba, mp->virt, mp->phys);
3301 				kfree(mp);
3302 			}
3303 			mempool_free(mbox, phba->mbox_mem_pool);
3304 		}
3305 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3306 		    (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3307 			if (lpfc_nlp_not_used(ndlp)) {
3308 				ndlp = NULL;
3309 				/* Indicate the node has already released,
3310 				 * should not reference to it from within
3311 				 * the routine lpfc_els_free_iocb.
3312 				 */
3313 				cmdiocb->context1 = NULL;
3314 			}
3315 		goto out;
3316 	}
3317 
3318 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3319 		"ELS rsp cmpl:    status:x%x/x%x did:x%x",
3320 		irsp->ulpStatus, irsp->un.ulpWord[4],
3321 		cmdiocb->iocb.un.elsreq64.remoteID);
3322 	/* ELS response tag <ulpIoTag> completes */
3323 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3324 			 "0110 ELS response tag x%x completes "
3325 			 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3326 			 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3327 			 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3328 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3329 			 ndlp->nlp_rpi);
3330 	if (mbox) {
3331 		if ((rspiocb->iocb.ulpStatus == 0)
3332 		    && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
3333 			lpfc_unreg_rpi(vport, ndlp);
3334 			/* Increment reference count to ndlp to hold the
3335 			 * reference to ndlp for the callback function.
3336 			 */
3337 			mbox->context2 = lpfc_nlp_get(ndlp);
3338 			mbox->vport = vport;
3339 			if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3340 				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3341 				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3342 			}
3343 			else {
3344 				mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3345 				ndlp->nlp_prev_state = ndlp->nlp_state;
3346 				lpfc_nlp_set_state(vport, ndlp,
3347 					   NLP_STE_REG_LOGIN_ISSUE);
3348 			}
3349 			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
3350 			    != MBX_NOT_FINISHED)
3351 				goto out;
3352 			else
3353 				/* Decrement the ndlp reference count we
3354 				 * set for this failed mailbox command.
3355 				 */
3356 				lpfc_nlp_put(ndlp);
3357 
3358 			/* ELS rsp: Cannot issue reg_login for <NPortid> */
3359 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3360 				"0138 ELS rsp: Cannot issue reg_login for x%x "
3361 				"Data: x%x x%x x%x\n",
3362 				ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3363 				ndlp->nlp_rpi);
3364 
3365 			if (lpfc_nlp_not_used(ndlp)) {
3366 				ndlp = NULL;
3367 				/* Indicate node has already been released,
3368 				 * should not reference to it from within
3369 				 * the routine lpfc_els_free_iocb.
3370 				 */
3371 				cmdiocb->context1 = NULL;
3372 			}
3373 		} else {
3374 			/* Do not drop node for lpfc_els_abort'ed ELS cmds */
3375 			if (!lpfc_error_lost_link(irsp) &&
3376 			    ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
3377 				if (lpfc_nlp_not_used(ndlp)) {
3378 					ndlp = NULL;
3379 					/* Indicate node has already been
3380 					 * released, should not reference
3381 					 * to it from within the routine
3382 					 * lpfc_els_free_iocb.
3383 					 */
3384 					cmdiocb->context1 = NULL;
3385 				}
3386 			}
3387 		}
3388 		mp = (struct lpfc_dmabuf *) mbox->context1;
3389 		if (mp) {
3390 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
3391 			kfree(mp);
3392 		}
3393 		mempool_free(mbox, phba->mbox_mem_pool);
3394 	}
3395 out:
3396 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3397 		spin_lock_irq(shost->host_lock);
3398 		ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
3399 		spin_unlock_irq(shost->host_lock);
3400 
3401 		/* If the node is not being used by another discovery thread,
3402 		 * and we are sending a reject, we are done with it.
3403 		 * Release driver reference count here and free associated
3404 		 * resources.
3405 		 */
3406 		if (ls_rjt)
3407 			if (lpfc_nlp_not_used(ndlp))
3408 				/* Indicate node has already been released,
3409 				 * should not reference to it from within
3410 				 * the routine lpfc_els_free_iocb.
3411 				 */
3412 				cmdiocb->context1 = NULL;
3413 	}
3414 
3415 	lpfc_els_free_iocb(phba, cmdiocb);
3416 	return;
3417 }
3418 
3419 /**
3420  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
3421  * @vport: pointer to a host virtual N_Port data structure.
3422  * @flag: the els command code to be accepted.
3423  * @oldiocb: pointer to the original lpfc command iocb data structure.
3424  * @ndlp: pointer to a node-list data structure.
3425  * @mbox: pointer to the driver internal queue element for mailbox command.
3426  *
3427  * This routine prepares and issues an Accept (ACC) response IOCB
3428  * command. It uses the @flag to properly set up the IOCB field for the
3429  * specific ACC response command to be issued and invokes the
3430  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3431  * @mbox pointer is passed in, it will be put into the context_un.mbox
3432  * field of the IOCB for the completion callback function to issue the
3433  * mailbox command to the HBA later when callback is invoked.
3434  *
3435  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3436  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3437  * will be stored into the context1 field of the IOCB for the completion
3438  * callback function to the corresponding response ELS IOCB command.
3439  *
3440  * Return code
3441  *   0 - Successfully issued acc response
3442  *   1 - Failed to issue acc response
3443  **/
3444 int
3445 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3446 		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3447 		 LPFC_MBOXQ_t *mbox)
3448 {
3449 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3450 	struct lpfc_hba  *phba = vport->phba;
3451 	IOCB_t *icmd;
3452 	IOCB_t *oldcmd;
3453 	struct lpfc_iocbq *elsiocb;
3454 	struct lpfc_sli *psli;
3455 	uint8_t *pcmd;
3456 	uint16_t cmdsize;
3457 	int rc;
3458 	ELS_PKT *els_pkt_ptr;
3459 
3460 	psli = &phba->sli;
3461 	oldcmd = &oldiocb->iocb;
3462 
3463 	switch (flag) {
3464 	case ELS_CMD_ACC:
3465 		cmdsize = sizeof(uint32_t);
3466 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3467 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3468 		if (!elsiocb) {
3469 			spin_lock_irq(shost->host_lock);
3470 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3471 			spin_unlock_irq(shost->host_lock);
3472 			return 1;
3473 		}
3474 
3475 		icmd = &elsiocb->iocb;
3476 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3477 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3478 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3479 		pcmd += sizeof(uint32_t);
3480 
3481 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3482 			"Issue ACC:       did:x%x flg:x%x",
3483 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
3484 		break;
3485 	case ELS_CMD_PLOGI:
3486 		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
3487 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3488 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3489 		if (!elsiocb)
3490 			return 1;
3491 
3492 		icmd = &elsiocb->iocb;
3493 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3494 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3495 
3496 		if (mbox)
3497 			elsiocb->context_un.mbox = mbox;
3498 
3499 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3500 		pcmd += sizeof(uint32_t);
3501 		memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
3502 
3503 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3504 			"Issue ACC PLOGI: did:x%x flg:x%x",
3505 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
3506 		break;
3507 	case ELS_CMD_PRLO:
3508 		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
3509 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3510 					     ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3511 		if (!elsiocb)
3512 			return 1;
3513 
3514 		icmd = &elsiocb->iocb;
3515 		icmd->ulpContext = oldcmd->ulpContext; /* Xri */
3516 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3517 
3518 		memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
3519 		       sizeof(uint32_t) + sizeof(PRLO));
3520 		*((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3521 		els_pkt_ptr = (ELS_PKT *) pcmd;
3522 		els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
3523 
3524 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3525 			"Issue ACC PRLO:  did:x%x flg:x%x",
3526 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
3527 		break;
3528 	default:
3529 		return 1;
3530 	}
3531 	/* Xmit ELS ACC response tag <ulpIoTag> */
3532 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3533 			 "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
3534 			 "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
3535 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
3536 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3537 			 ndlp->nlp_rpi);
3538 	if (ndlp->nlp_flag & NLP_LOGO_ACC) {
3539 		spin_lock_irq(shost->host_lock);
3540 		ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3541 		spin_unlock_irq(shost->host_lock);
3542 		elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
3543 	} else {
3544 		elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3545 	}
3546 
3547 	phba->fc_stat.elsXmitACC++;
3548 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3549 	if (rc == IOCB_ERROR) {
3550 		lpfc_els_free_iocb(phba, elsiocb);
3551 		return 1;
3552 	}
3553 	return 0;
3554 }
3555 
3556 /**
3557  * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
3558  * @vport: pointer to a virtual N_Port data structure.
3559  * @rejectError:
3560  * @oldiocb: pointer to the original lpfc command iocb data structure.
3561  * @ndlp: pointer to a node-list data structure.
3562  * @mbox: pointer to the driver internal queue element for mailbox command.
3563  *
3564  * This routine prepares and issue an Reject (RJT) response IOCB
3565  * command. If a @mbox pointer is passed in, it will be put into the
3566  * context_un.mbox field of the IOCB for the completion callback function
3567  * to issue to the HBA later.
3568  *
3569  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3570  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3571  * will be stored into the context1 field of the IOCB for the completion
3572  * callback function to the reject response ELS IOCB command.
3573  *
3574  * Return code
3575  *   0 - Successfully issued reject response
3576  *   1 - Failed to issue reject response
3577  **/
3578 int
3579 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
3580 		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3581 		    LPFC_MBOXQ_t *mbox)
3582 {
3583 	struct lpfc_hba  *phba = vport->phba;
3584 	IOCB_t *icmd;
3585 	IOCB_t *oldcmd;
3586 	struct lpfc_iocbq *elsiocb;
3587 	struct lpfc_sli *psli;
3588 	uint8_t *pcmd;
3589 	uint16_t cmdsize;
3590 	int rc;
3591 
3592 	psli = &phba->sli;
3593 	cmdsize = 2 * sizeof(uint32_t);
3594 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3595 				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
3596 	if (!elsiocb)
3597 		return 1;
3598 
3599 	icmd = &elsiocb->iocb;
3600 	oldcmd = &oldiocb->iocb;
3601 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3602 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3603 
3604 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
3605 	pcmd += sizeof(uint32_t);
3606 	*((uint32_t *) (pcmd)) = rejectError;
3607 
3608 	if (mbox)
3609 		elsiocb->context_un.mbox = mbox;
3610 
3611 	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
3612 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3613 			 "0129 Xmit ELS RJT x%x response tag x%x "
3614 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3615 			 "rpi x%x\n",
3616 			 rejectError, elsiocb->iotag,
3617 			 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
3618 			 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
3619 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3620 		"Issue LS_RJT:    did:x%x flg:x%x err:x%x",
3621 		ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
3622 
3623 	phba->fc_stat.elsXmitLSRJT++;
3624 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3625 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3626 
3627 	if (rc == IOCB_ERROR) {
3628 		lpfc_els_free_iocb(phba, elsiocb);
3629 		return 1;
3630 	}
3631 	return 0;
3632 }
3633 
3634 /**
3635  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
3636  * @vport: pointer to a virtual N_Port data structure.
3637  * @oldiocb: pointer to the original lpfc command iocb data structure.
3638  * @ndlp: pointer to a node-list data structure.
3639  *
3640  * This routine prepares and issues an Accept (ACC) response to Address
3641  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
3642  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3643  *
3644  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3645  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3646  * will be stored into the context1 field of the IOCB for the completion
3647  * callback function to the ADISC Accept response ELS IOCB command.
3648  *
3649  * Return code
3650  *   0 - Successfully issued acc adisc response
3651  *   1 - Failed to issue adisc acc response
3652  **/
3653 int
3654 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3655 		       struct lpfc_nodelist *ndlp)
3656 {
3657 	struct lpfc_hba  *phba = vport->phba;
3658 	ADISC *ap;
3659 	IOCB_t *icmd, *oldcmd;
3660 	struct lpfc_iocbq *elsiocb;
3661 	uint8_t *pcmd;
3662 	uint16_t cmdsize;
3663 	int rc;
3664 
3665 	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
3666 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3667 				     ndlp->nlp_DID, ELS_CMD_ACC);
3668 	if (!elsiocb)
3669 		return 1;
3670 
3671 	icmd = &elsiocb->iocb;
3672 	oldcmd = &oldiocb->iocb;
3673 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3674 
3675 	/* Xmit ADISC ACC response tag <ulpIoTag> */
3676 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3677 			 "0130 Xmit ADISC ACC response iotag x%x xri: "
3678 			 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
3679 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
3680 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3681 			 ndlp->nlp_rpi);
3682 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3683 
3684 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3685 	pcmd += sizeof(uint32_t);
3686 
3687 	ap = (ADISC *) (pcmd);
3688 	ap->hardAL_PA = phba->fc_pref_ALPA;
3689 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3690 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3691 	ap->DID = be32_to_cpu(vport->fc_myDID);
3692 
3693 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3694 		"Issue ACC ADISC: did:x%x flg:x%x",
3695 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
3696 
3697 	phba->fc_stat.elsXmitACC++;
3698 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3699 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3700 	if (rc == IOCB_ERROR) {
3701 		lpfc_els_free_iocb(phba, elsiocb);
3702 		return 1;
3703 	}
3704 	return 0;
3705 }
3706 
3707 /**
3708  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
3709  * @vport: pointer to a virtual N_Port data structure.
3710  * @oldiocb: pointer to the original lpfc command iocb data structure.
3711  * @ndlp: pointer to a node-list data structure.
3712  *
3713  * This routine prepares and issues an Accept (ACC) response to Process
3714  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
3715  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3716  *
3717  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3718  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3719  * will be stored into the context1 field of the IOCB for the completion
3720  * callback function to the PRLI Accept response ELS IOCB command.
3721  *
3722  * Return code
3723  *   0 - Successfully issued acc prli response
3724  *   1 - Failed to issue acc prli response
3725  **/
3726 int
3727 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3728 		      struct lpfc_nodelist *ndlp)
3729 {
3730 	struct lpfc_hba  *phba = vport->phba;
3731 	PRLI *npr;
3732 	lpfc_vpd_t *vpd;
3733 	IOCB_t *icmd;
3734 	IOCB_t *oldcmd;
3735 	struct lpfc_iocbq *elsiocb;
3736 	struct lpfc_sli *psli;
3737 	uint8_t *pcmd;
3738 	uint16_t cmdsize;
3739 	int rc;
3740 
3741 	psli = &phba->sli;
3742 
3743 	cmdsize = sizeof(uint32_t) + sizeof(PRLI);
3744 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3745 		ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
3746 	if (!elsiocb)
3747 		return 1;
3748 
3749 	icmd = &elsiocb->iocb;
3750 	oldcmd = &oldiocb->iocb;
3751 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3752 	/* Xmit PRLI ACC response tag <ulpIoTag> */
3753 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3754 			 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
3755 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3756 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
3757 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3758 			 ndlp->nlp_rpi);
3759 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3760 
3761 	*((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
3762 	pcmd += sizeof(uint32_t);
3763 
3764 	/* For PRLI, remainder of payload is PRLI parameter page */
3765 	memset(pcmd, 0, sizeof(PRLI));
3766 
3767 	npr = (PRLI *) pcmd;
3768 	vpd = &phba->vpd;
3769 	/*
3770 	 * If the remote port is a target and our firmware version is 3.20 or
3771 	 * later, set the following bits for FC-TAPE support.
3772 	 */
3773 	if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3774 	    (vpd->rev.feaLevelHigh >= 0x02)) {
3775 		npr->ConfmComplAllowed = 1;
3776 		npr->Retry = 1;
3777 		npr->TaskRetryIdReq = 1;
3778 	}
3779 
3780 	npr->acceptRspCode = PRLI_REQ_EXECUTED;
3781 	npr->estabImagePair = 1;
3782 	npr->readXferRdyDis = 1;
3783 	npr->ConfmComplAllowed = 1;
3784 
3785 	npr->prliType = PRLI_FCP_TYPE;
3786 	npr->initiatorFunc = 1;
3787 
3788 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3789 		"Issue ACC PRLI:  did:x%x flg:x%x",
3790 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
3791 
3792 	phba->fc_stat.elsXmitACC++;
3793 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3794 
3795 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3796 	if (rc == IOCB_ERROR) {
3797 		lpfc_els_free_iocb(phba, elsiocb);
3798 		return 1;
3799 	}
3800 	return 0;
3801 }
3802 
3803 /**
3804  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
3805  * @vport: pointer to a virtual N_Port data structure.
3806  * @format: rnid command format.
3807  * @oldiocb: pointer to the original lpfc command iocb data structure.
3808  * @ndlp: pointer to a node-list data structure.
3809  *
3810  * This routine issues a Request Node Identification Data (RNID) Accept
3811  * (ACC) response. It constructs the RNID ACC response command according to
3812  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
3813  * issue the response. Note that this command does not need to hold the ndlp
3814  * reference count for the callback. So, the ndlp reference count taken by
3815  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
3816  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
3817  * there is no ndlp reference available.
3818  *
3819  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3820  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3821  * will be stored into the context1 field of the IOCB for the completion
3822  * callback function. However, for the RNID Accept Response ELS command,
3823  * this is undone later by this routine after the IOCB is allocated.
3824  *
3825  * Return code
3826  *   0 - Successfully issued acc rnid response
3827  *   1 - Failed to issue acc rnid response
3828  **/
3829 static int
3830 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
3831 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3832 {
3833 	struct lpfc_hba  *phba = vport->phba;
3834 	RNID *rn;
3835 	IOCB_t *icmd, *oldcmd;
3836 	struct lpfc_iocbq *elsiocb;
3837 	struct lpfc_sli *psli;
3838 	uint8_t *pcmd;
3839 	uint16_t cmdsize;
3840 	int rc;
3841 
3842 	psli = &phba->sli;
3843 	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
3844 					+ (2 * sizeof(struct lpfc_name));
3845 	if (format)
3846 		cmdsize += sizeof(RNID_TOP_DISC);
3847 
3848 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3849 				     ndlp->nlp_DID, ELS_CMD_ACC);
3850 	if (!elsiocb)
3851 		return 1;
3852 
3853 	icmd = &elsiocb->iocb;
3854 	oldcmd = &oldiocb->iocb;
3855 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3856 	/* Xmit RNID ACC response tag <ulpIoTag> */
3857 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3858 			 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
3859 			 elsiocb->iotag, elsiocb->iocb.ulpContext);
3860 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3861 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3862 	pcmd += sizeof(uint32_t);
3863 
3864 	memset(pcmd, 0, sizeof(RNID));
3865 	rn = (RNID *) (pcmd);
3866 	rn->Format = format;
3867 	rn->CommonLen = (2 * sizeof(struct lpfc_name));
3868 	memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3869 	memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3870 	switch (format) {
3871 	case 0:
3872 		rn->SpecificLen = 0;
3873 		break;
3874 	case RNID_TOPOLOGY_DISC:
3875 		rn->SpecificLen = sizeof(RNID_TOP_DISC);
3876 		memcpy(&rn->un.topologyDisc.portName,
3877 		       &vport->fc_portname, sizeof(struct lpfc_name));
3878 		rn->un.topologyDisc.unitType = RNID_HBA;
3879 		rn->un.topologyDisc.physPort = 0;
3880 		rn->un.topologyDisc.attachedNodes = 0;
3881 		break;
3882 	default:
3883 		rn->CommonLen = 0;
3884 		rn->SpecificLen = 0;
3885 		break;
3886 	}
3887 
3888 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3889 		"Issue ACC RNID:  did:x%x flg:x%x",
3890 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
3891 
3892 	phba->fc_stat.elsXmitACC++;
3893 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3894 	lpfc_nlp_put(ndlp);
3895 	elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
3896 				    * it could be freed */
3897 
3898 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3899 	if (rc == IOCB_ERROR) {
3900 		lpfc_els_free_iocb(phba, elsiocb);
3901 		return 1;
3902 	}
3903 	return 0;
3904 }
3905 
3906 /**
3907  * lpfc_els_rsp_echo_acc - Issue echo acc response
3908  * @vport: pointer to a virtual N_Port data structure.
3909  * @data: pointer to echo data to return in the accept.
3910  * @oldiocb: pointer to the original lpfc command iocb data structure.
3911  * @ndlp: pointer to a node-list data structure.
3912  *
3913  * Return code
3914  *   0 - Successfully issued acc echo response
3915  *   1 - Failed to issue acc echo response
3916  **/
3917 static int
3918 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
3919 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3920 {
3921 	struct lpfc_hba  *phba = vport->phba;
3922 	struct lpfc_iocbq *elsiocb;
3923 	struct lpfc_sli *psli;
3924 	uint8_t *pcmd;
3925 	uint16_t cmdsize;
3926 	int rc;
3927 
3928 	psli = &phba->sli;
3929 	cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
3930 
3931 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3932 				     ndlp->nlp_DID, ELS_CMD_ACC);
3933 	if (!elsiocb)
3934 		return 1;
3935 
3936 	elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext;	/* Xri */
3937 	/* Xmit ECHO ACC response tag <ulpIoTag> */
3938 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3939 			 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
3940 			 elsiocb->iotag, elsiocb->iocb.ulpContext);
3941 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3942 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3943 	pcmd += sizeof(uint32_t);
3944 	memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
3945 
3946 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3947 		"Issue ACC ECHO:  did:x%x flg:x%x",
3948 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
3949 
3950 	phba->fc_stat.elsXmitACC++;
3951 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3952 	lpfc_nlp_put(ndlp);
3953 	elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
3954 				    * it could be freed */
3955 
3956 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3957 	if (rc == IOCB_ERROR) {
3958 		lpfc_els_free_iocb(phba, elsiocb);
3959 		return 1;
3960 	}
3961 	return 0;
3962 }
3963 
3964 /**
3965  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
3966  * @vport: pointer to a host virtual N_Port data structure.
3967  *
3968  * This routine issues Address Discover (ADISC) ELS commands to those
3969  * N_Ports which are in node port recovery state and ADISC has not been issued
3970  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
3971  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
3972  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
3973  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
3974  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
3975  * IOCBs quit for later pick up. On the other hand, after walking through
3976  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
3977  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
3978  * no more ADISC need to be sent.
3979  *
3980  * Return code
3981  *    The number of N_Ports with adisc issued.
3982  **/
3983 int
3984 lpfc_els_disc_adisc(struct lpfc_vport *vport)
3985 {
3986 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3987 	struct lpfc_nodelist *ndlp, *next_ndlp;
3988 	int sentadisc = 0;
3989 
3990 	/* go thru NPR nodes and issue any remaining ELS ADISCs */
3991 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3992 		if (!NLP_CHK_NODE_ACT(ndlp))
3993 			continue;
3994 		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3995 		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3996 		    (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
3997 			spin_lock_irq(shost->host_lock);
3998 			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3999 			spin_unlock_irq(shost->host_lock);
4000 			ndlp->nlp_prev_state = ndlp->nlp_state;
4001 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4002 			lpfc_issue_els_adisc(vport, ndlp, 0);
4003 			sentadisc++;
4004 			vport->num_disc_nodes++;
4005 			if (vport->num_disc_nodes >=
4006 			    vport->cfg_discovery_threads) {
4007 				spin_lock_irq(shost->host_lock);
4008 				vport->fc_flag |= FC_NLP_MORE;
4009 				spin_unlock_irq(shost->host_lock);
4010 				break;
4011 			}
4012 		}
4013 	}
4014 	if (sentadisc == 0) {
4015 		spin_lock_irq(shost->host_lock);
4016 		vport->fc_flag &= ~FC_NLP_MORE;
4017 		spin_unlock_irq(shost->host_lock);
4018 	}
4019 	return sentadisc;
4020 }
4021 
4022 /**
4023  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
4024  * @vport: pointer to a host virtual N_Port data structure.
4025  *
4026  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
4027  * which are in node port recovery state, with a @vport. Each time an ELS
4028  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
4029  * the per @vport number of discover count (num_disc_nodes) shall be
4030  * incremented. If the num_disc_nodes reaches a pre-configured threshold
4031  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
4032  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
4033  * later pick up. On the other hand, after walking through all the ndlps with
4034  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
4035  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
4036  * PLOGI need to be sent.
4037  *
4038  * Return code
4039  *   The number of N_Ports with plogi issued.
4040  **/
4041 int
4042 lpfc_els_disc_plogi(struct lpfc_vport *vport)
4043 {
4044 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4045 	struct lpfc_nodelist *ndlp, *next_ndlp;
4046 	int sentplogi = 0;
4047 
4048 	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
4049 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
4050 		if (!NLP_CHK_NODE_ACT(ndlp))
4051 			continue;
4052 		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4053 		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4054 		    (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
4055 		    (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
4056 			ndlp->nlp_prev_state = ndlp->nlp_state;
4057 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4058 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4059 			sentplogi++;
4060 			vport->num_disc_nodes++;
4061 			if (vport->num_disc_nodes >=
4062 			    vport->cfg_discovery_threads) {
4063 				spin_lock_irq(shost->host_lock);
4064 				vport->fc_flag |= FC_NLP_MORE;
4065 				spin_unlock_irq(shost->host_lock);
4066 				break;
4067 			}
4068 		}
4069 	}
4070 	if (sentplogi) {
4071 		lpfc_set_disctmo(vport);
4072 	}
4073 	else {
4074 		spin_lock_irq(shost->host_lock);
4075 		vport->fc_flag &= ~FC_NLP_MORE;
4076 		spin_unlock_irq(shost->host_lock);
4077 	}
4078 	return sentplogi;
4079 }
4080 
4081 /**
4082  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
4083  * @vport: pointer to a host virtual N_Port data structure.
4084  *
4085  * This routine cleans up any Registration State Change Notification
4086  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
4087  * @vport together with the host_lock is used to prevent multiple thread
4088  * trying to access the RSCN array on a same @vport at the same time.
4089  **/
4090 void
4091 lpfc_els_flush_rscn(struct lpfc_vport *vport)
4092 {
4093 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4094 	struct lpfc_hba  *phba = vport->phba;
4095 	int i;
4096 
4097 	spin_lock_irq(shost->host_lock);
4098 	if (vport->fc_rscn_flush) {
4099 		/* Another thread is walking fc_rscn_id_list on this vport */
4100 		spin_unlock_irq(shost->host_lock);
4101 		return;
4102 	}
4103 	/* Indicate we are walking lpfc_els_flush_rscn on this vport */
4104 	vport->fc_rscn_flush = 1;
4105 	spin_unlock_irq(shost->host_lock);
4106 
4107 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
4108 		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
4109 		vport->fc_rscn_id_list[i] = NULL;
4110 	}
4111 	spin_lock_irq(shost->host_lock);
4112 	vport->fc_rscn_id_cnt = 0;
4113 	vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
4114 	spin_unlock_irq(shost->host_lock);
4115 	lpfc_can_disctmo(vport);
4116 	/* Indicate we are done walking this fc_rscn_id_list */
4117 	vport->fc_rscn_flush = 0;
4118 }
4119 
4120 /**
4121  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
4122  * @vport: pointer to a host virtual N_Port data structure.
4123  * @did: remote destination port identifier.
4124  *
4125  * This routine checks whether there is any pending Registration State
4126  * Configuration Notification (RSCN) to a @did on @vport.
4127  *
4128  * Return code
4129  *   None zero - The @did matched with a pending rscn
4130  *   0 - not able to match @did with a pending rscn
4131  **/
4132 int
4133 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
4134 {
4135 	D_ID ns_did;
4136 	D_ID rscn_did;
4137 	uint32_t *lp;
4138 	uint32_t payload_len, i;
4139 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4140 
4141 	ns_did.un.word = did;
4142 
4143 	/* Never match fabric nodes for RSCNs */
4144 	if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
4145 		return 0;
4146 
4147 	/* If we are doing a FULL RSCN rediscovery, match everything */
4148 	if (vport->fc_flag & FC_RSCN_DISCOVERY)
4149 		return did;
4150 
4151 	spin_lock_irq(shost->host_lock);
4152 	if (vport->fc_rscn_flush) {
4153 		/* Another thread is walking fc_rscn_id_list on this vport */
4154 		spin_unlock_irq(shost->host_lock);
4155 		return 0;
4156 	}
4157 	/* Indicate we are walking fc_rscn_id_list on this vport */
4158 	vport->fc_rscn_flush = 1;
4159 	spin_unlock_irq(shost->host_lock);
4160 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
4161 		lp = vport->fc_rscn_id_list[i]->virt;
4162 		payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4163 		payload_len -= sizeof(uint32_t);	/* take off word 0 */
4164 		while (payload_len) {
4165 			rscn_did.un.word = be32_to_cpu(*lp++);
4166 			payload_len -= sizeof(uint32_t);
4167 			switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
4168 			case RSCN_ADDRESS_FORMAT_PORT:
4169 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4170 				    && (ns_did.un.b.area == rscn_did.un.b.area)
4171 				    && (ns_did.un.b.id == rscn_did.un.b.id))
4172 					goto return_did_out;
4173 				break;
4174 			case RSCN_ADDRESS_FORMAT_AREA:
4175 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4176 				    && (ns_did.un.b.area == rscn_did.un.b.area))
4177 					goto return_did_out;
4178 				break;
4179 			case RSCN_ADDRESS_FORMAT_DOMAIN:
4180 				if (ns_did.un.b.domain == rscn_did.un.b.domain)
4181 					goto return_did_out;
4182 				break;
4183 			case RSCN_ADDRESS_FORMAT_FABRIC:
4184 				goto return_did_out;
4185 			}
4186 		}
4187 	}
4188 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
4189 	vport->fc_rscn_flush = 0;
4190 	return 0;
4191 return_did_out:
4192 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
4193 	vport->fc_rscn_flush = 0;
4194 	return did;
4195 }
4196 
4197 /**
4198  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
4199  * @vport: pointer to a host virtual N_Port data structure.
4200  *
4201  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
4202  * state machine for a @vport's nodes that are with pending RSCN (Registration
4203  * State Change Notification).
4204  *
4205  * Return code
4206  *   0 - Successful (currently alway return 0)
4207  **/
4208 static int
4209 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
4210 {
4211 	struct lpfc_nodelist *ndlp = NULL;
4212 
4213 	/* Move all affected nodes by pending RSCNs to NPR state. */
4214 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4215 		if (!NLP_CHK_NODE_ACT(ndlp) ||
4216 		    (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
4217 		    !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
4218 			continue;
4219 		lpfc_disc_state_machine(vport, ndlp, NULL,
4220 					NLP_EVT_DEVICE_RECOVERY);
4221 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
4222 	}
4223 	return 0;
4224 }
4225 
4226 /**
4227  * lpfc_send_rscn_event - Send an RSCN event to management application
4228  * @vport: pointer to a host virtual N_Port data structure.
4229  * @cmdiocb: pointer to lpfc command iocb data structure.
4230  *
4231  * lpfc_send_rscn_event sends an RSCN netlink event to management
4232  * applications.
4233  */
4234 static void
4235 lpfc_send_rscn_event(struct lpfc_vport *vport,
4236 		struct lpfc_iocbq *cmdiocb)
4237 {
4238 	struct lpfc_dmabuf *pcmd;
4239 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4240 	uint32_t *payload_ptr;
4241 	uint32_t payload_len;
4242 	struct lpfc_rscn_event_header *rscn_event_data;
4243 
4244 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4245 	payload_ptr = (uint32_t *) pcmd->virt;
4246 	payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
4247 
4248 	rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
4249 		payload_len, GFP_KERNEL);
4250 	if (!rscn_event_data) {
4251 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4252 			"0147 Failed to allocate memory for RSCN event\n");
4253 		return;
4254 	}
4255 	rscn_event_data->event_type = FC_REG_RSCN_EVENT;
4256 	rscn_event_data->payload_length = payload_len;
4257 	memcpy(rscn_event_data->rscn_payload, payload_ptr,
4258 		payload_len);
4259 
4260 	fc_host_post_vendor_event(shost,
4261 		fc_get_event_number(),
4262 		sizeof(struct lpfc_els_event_header) + payload_len,
4263 		(char *)rscn_event_data,
4264 		LPFC_NL_VENDOR_ID);
4265 
4266 	kfree(rscn_event_data);
4267 }
4268 
4269 /**
4270  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
4271  * @vport: pointer to a host virtual N_Port data structure.
4272  * @cmdiocb: pointer to lpfc command iocb data structure.
4273  * @ndlp: pointer to a node-list data structure.
4274  *
4275  * This routine processes an unsolicited RSCN (Registration State Change
4276  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
4277  * to invoke fc_host_post_event() routine to the FC transport layer. If the
4278  * discover state machine is about to begin discovery, it just accepts the
4279  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
4280  * contains N_Port IDs for other vports on this HBA, it just accepts the
4281  * RSCN and ignore processing it. If the state machine is in the recovery
4282  * state, the fc_rscn_id_list of this @vport is walked and the
4283  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
4284  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
4285  * routine is invoked to handle the RSCN event.
4286  *
4287  * Return code
4288  *   0 - Just sent the acc response
4289  *   1 - Sent the acc response and waited for name server completion
4290  **/
4291 static int
4292 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4293 		  struct lpfc_nodelist *ndlp)
4294 {
4295 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4296 	struct lpfc_hba  *phba = vport->phba;
4297 	struct lpfc_dmabuf *pcmd;
4298 	uint32_t *lp, *datap;
4299 	IOCB_t *icmd;
4300 	uint32_t payload_len, length, nportid, *cmd;
4301 	int rscn_cnt;
4302 	int rscn_id = 0, hba_id = 0;
4303 	int i;
4304 
4305 	icmd = &cmdiocb->iocb;
4306 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4307 	lp = (uint32_t *) pcmd->virt;
4308 
4309 	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4310 	payload_len -= sizeof(uint32_t);	/* take off word 0 */
4311 	/* RSCN received */
4312 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4313 			 "0214 RSCN received Data: x%x x%x x%x x%x\n",
4314 			 vport->fc_flag, payload_len, *lp,
4315 			 vport->fc_rscn_id_cnt);
4316 
4317 	/* Send an RSCN event to the management application */
4318 	lpfc_send_rscn_event(vport, cmdiocb);
4319 
4320 	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
4321 		fc_host_post_event(shost, fc_get_event_number(),
4322 			FCH_EVT_RSCN, lp[i]);
4323 
4324 	/* If we are about to begin discovery, just ACC the RSCN.
4325 	 * Discovery processing will satisfy it.
4326 	 */
4327 	if (vport->port_state <= LPFC_NS_QRY) {
4328 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4329 			"RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
4330 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4331 
4332 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4333 		return 0;
4334 	}
4335 
4336 	/* If this RSCN just contains NPortIDs for other vports on this HBA,
4337 	 * just ACC and ignore it.
4338 	 */
4339 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4340 		!(vport->cfg_peer_port_login)) {
4341 		i = payload_len;
4342 		datap = lp;
4343 		while (i > 0) {
4344 			nportid = *datap++;
4345 			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
4346 			i -= sizeof(uint32_t);
4347 			rscn_id++;
4348 			if (lpfc_find_vport_by_did(phba, nportid))
4349 				hba_id++;
4350 		}
4351 		if (rscn_id == hba_id) {
4352 			/* ALL NPortIDs in RSCN are on HBA */
4353 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4354 					 "0219 Ignore RSCN "
4355 					 "Data: x%x x%x x%x x%x\n",
4356 					 vport->fc_flag, payload_len,
4357 					 *lp, vport->fc_rscn_id_cnt);
4358 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4359 				"RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
4360 				ndlp->nlp_DID, vport->port_state,
4361 				ndlp->nlp_flag);
4362 
4363 			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
4364 				ndlp, NULL);
4365 			return 0;
4366 		}
4367 	}
4368 
4369 	spin_lock_irq(shost->host_lock);
4370 	if (vport->fc_rscn_flush) {
4371 		/* Another thread is walking fc_rscn_id_list on this vport */
4372 		vport->fc_flag |= FC_RSCN_DISCOVERY;
4373 		spin_unlock_irq(shost->host_lock);
4374 		/* Send back ACC */
4375 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4376 		return 0;
4377 	}
4378 	/* Indicate we are walking fc_rscn_id_list on this vport */
4379 	vport->fc_rscn_flush = 1;
4380 	spin_unlock_irq(shost->host_lock);
4381 	/* Get the array count after successfully have the token */
4382 	rscn_cnt = vport->fc_rscn_id_cnt;
4383 	/* If we are already processing an RSCN, save the received
4384 	 * RSCN payload buffer, cmdiocb->context2 to process later.
4385 	 */
4386 	if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
4387 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4388 			"RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
4389 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4390 
4391 		spin_lock_irq(shost->host_lock);
4392 		vport->fc_flag |= FC_RSCN_DEFERRED;
4393 		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
4394 		    !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
4395 			vport->fc_flag |= FC_RSCN_MODE;
4396 			spin_unlock_irq(shost->host_lock);
4397 			if (rscn_cnt) {
4398 				cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4399 				length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4400 			}
4401 			if ((rscn_cnt) &&
4402 			    (payload_len + length <= LPFC_BPL_SIZE)) {
4403 				*cmd &= ELS_CMD_MASK;
4404 				*cmd |= cpu_to_be32(payload_len + length);
4405 				memcpy(((uint8_t *)cmd) + length, lp,
4406 				       payload_len);
4407 			} else {
4408 				vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4409 				vport->fc_rscn_id_cnt++;
4410 				/* If we zero, cmdiocb->context2, the calling
4411 				 * routine will not try to free it.
4412 				 */
4413 				cmdiocb->context2 = NULL;
4414 			}
4415 			/* Deferred RSCN */
4416 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4417 					 "0235 Deferred RSCN "
4418 					 "Data: x%x x%x x%x\n",
4419 					 vport->fc_rscn_id_cnt, vport->fc_flag,
4420 					 vport->port_state);
4421 		} else {
4422 			vport->fc_flag |= FC_RSCN_DISCOVERY;
4423 			spin_unlock_irq(shost->host_lock);
4424 			/* ReDiscovery RSCN */
4425 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4426 					 "0234 ReDiscovery RSCN "
4427 					 "Data: x%x x%x x%x\n",
4428 					 vport->fc_rscn_id_cnt, vport->fc_flag,
4429 					 vport->port_state);
4430 		}
4431 		/* Indicate we are done walking fc_rscn_id_list on this vport */
4432 		vport->fc_rscn_flush = 0;
4433 		/* Send back ACC */
4434 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4435 		/* send RECOVERY event for ALL nodes that match RSCN payload */
4436 		lpfc_rscn_recovery_check(vport);
4437 		spin_lock_irq(shost->host_lock);
4438 		vport->fc_flag &= ~FC_RSCN_DEFERRED;
4439 		spin_unlock_irq(shost->host_lock);
4440 		return 0;
4441 	}
4442 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4443 		"RCV RSCN:        did:x%x/ste:x%x flg:x%x",
4444 		ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4445 
4446 	spin_lock_irq(shost->host_lock);
4447 	vport->fc_flag |= FC_RSCN_MODE;
4448 	spin_unlock_irq(shost->host_lock);
4449 	vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
4450 	/* Indicate we are done walking fc_rscn_id_list on this vport */
4451 	vport->fc_rscn_flush = 0;
4452 	/*
4453 	 * If we zero, cmdiocb->context2, the calling routine will
4454 	 * not try to free it.
4455 	 */
4456 	cmdiocb->context2 = NULL;
4457 	lpfc_set_disctmo(vport);
4458 	/* Send back ACC */
4459 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4460 	/* send RECOVERY event for ALL nodes that match RSCN payload */
4461 	lpfc_rscn_recovery_check(vport);
4462 	return lpfc_els_handle_rscn(vport);
4463 }
4464 
4465 /**
4466  * lpfc_els_handle_rscn - Handle rscn for a vport
4467  * @vport: pointer to a host virtual N_Port data structure.
4468  *
4469  * This routine handles the Registration State Configuration Notification
4470  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
4471  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
4472  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
4473  * NameServer shall be issued. If CT command to the NameServer fails to be
4474  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
4475  * RSCN activities with the @vport.
4476  *
4477  * Return code
4478  *   0 - Cleaned up rscn on the @vport
4479  *   1 - Wait for plogi to name server before proceed
4480  **/
4481 int
4482 lpfc_els_handle_rscn(struct lpfc_vport *vport)
4483 {
4484 	struct lpfc_nodelist *ndlp;
4485 	struct lpfc_hba *phba = vport->phba;
4486 
4487 	/* Ignore RSCN if the port is being torn down. */
4488 	if (vport->load_flag & FC_UNLOADING) {
4489 		lpfc_els_flush_rscn(vport);
4490 		return 0;
4491 	}
4492 
4493 	/* Start timer for RSCN processing */
4494 	lpfc_set_disctmo(vport);
4495 
4496 	/* RSCN processed */
4497 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4498 			 "0215 RSCN processed Data: x%x x%x x%x x%x\n",
4499 			 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
4500 			 vport->port_state);
4501 
4502 	/* To process RSCN, first compare RSCN data with NameServer */
4503 	vport->fc_ns_retry = 0;
4504 	vport->num_disc_nodes = 0;
4505 
4506 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
4507 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)
4508 	    && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
4509 		/* Good ndlp, issue CT Request to NameServer */
4510 		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
4511 			/* Wait for NameServer query cmpl before we can
4512 			   continue */
4513 			return 1;
4514 	} else {
4515 		/* If login to NameServer does not exist, issue one */
4516 		/* Good status, issue PLOGI to NameServer */
4517 		ndlp = lpfc_findnode_did(vport, NameServer_DID);
4518 		if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4519 			/* Wait for NameServer login cmpl before we can
4520 			   continue */
4521 			return 1;
4522 
4523 		if (ndlp) {
4524 			ndlp = lpfc_enable_node(vport, ndlp,
4525 						NLP_STE_PLOGI_ISSUE);
4526 			if (!ndlp) {
4527 				lpfc_els_flush_rscn(vport);
4528 				return 0;
4529 			}
4530 			ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
4531 		} else {
4532 			ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4533 			if (!ndlp) {
4534 				lpfc_els_flush_rscn(vport);
4535 				return 0;
4536 			}
4537 			lpfc_nlp_init(vport, ndlp, NameServer_DID);
4538 			ndlp->nlp_prev_state = ndlp->nlp_state;
4539 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4540 		}
4541 		ndlp->nlp_type |= NLP_FABRIC;
4542 		lpfc_issue_els_plogi(vport, NameServer_DID, 0);
4543 		/* Wait for NameServer login cmpl before we can
4544 		 * continue
4545 		 */
4546 		return 1;
4547 	}
4548 
4549 	lpfc_els_flush_rscn(vport);
4550 	return 0;
4551 }
4552 
4553 /**
4554  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
4555  * @vport: pointer to a host virtual N_Port data structure.
4556  * @cmdiocb: pointer to lpfc command iocb data structure.
4557  * @ndlp: pointer to a node-list data structure.
4558  *
4559  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
4560  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
4561  * point topology. As an unsolicited FLOGI should not be received in a loop
4562  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
4563  * lpfc_check_sparm() routine is invoked to check the parameters in the
4564  * unsolicited FLOGI. If parameters validation failed, the routine
4565  * lpfc_els_rsp_reject() shall be called with reject reason code set to
4566  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
4567  * FLOGI shall be compared with the Port WWN of the @vport to determine who
4568  * will initiate PLOGI. The higher lexicographical value party shall has
4569  * higher priority (as the winning port) and will initiate PLOGI and
4570  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
4571  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
4572  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
4573  *
4574  * Return code
4575  *   0 - Successfully processed the unsolicited flogi
4576  *   1 - Failed to process the unsolicited flogi
4577  **/
4578 static int
4579 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4580 		   struct lpfc_nodelist *ndlp)
4581 {
4582 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4583 	struct lpfc_hba  *phba = vport->phba;
4584 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4585 	uint32_t *lp = (uint32_t *) pcmd->virt;
4586 	IOCB_t *icmd = &cmdiocb->iocb;
4587 	struct serv_parm *sp;
4588 	LPFC_MBOXQ_t *mbox;
4589 	struct ls_rjt stat;
4590 	uint32_t cmd, did;
4591 	int rc;
4592 
4593 	cmd = *lp++;
4594 	sp = (struct serv_parm *) lp;
4595 
4596 	/* FLOGI received */
4597 
4598 	lpfc_set_disctmo(vport);
4599 
4600 	if (phba->fc_topology == TOPOLOGY_LOOP) {
4601 		/* We should never receive a FLOGI in loop mode, ignore it */
4602 		did = icmd->un.elsreq64.remoteID;
4603 
4604 		/* An FLOGI ELS command <elsCmd> was received from DID <did> in
4605 		   Loop Mode */
4606 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4607 				 "0113 An FLOGI ELS command x%x was "
4608 				 "received from DID x%x in Loop Mode\n",
4609 				 cmd, did);
4610 		return 1;
4611 	}
4612 
4613 	did = Fabric_DID;
4614 
4615 	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1))) {
4616 		/* For a FLOGI we accept, then if our portname is greater
4617 		 * then the remote portname we initiate Nport login.
4618 		 */
4619 
4620 		rc = memcmp(&vport->fc_portname, &sp->portName,
4621 			    sizeof(struct lpfc_name));
4622 
4623 		if (!rc) {
4624 			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4625 			if (!mbox)
4626 				return 1;
4627 
4628 			lpfc_linkdown(phba);
4629 			lpfc_init_link(phba, mbox,
4630 				       phba->cfg_topology,
4631 				       phba->cfg_link_speed);
4632 			mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
4633 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4634 			mbox->vport = vport;
4635 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4636 			lpfc_set_loopback_flag(phba);
4637 			if (rc == MBX_NOT_FINISHED) {
4638 				mempool_free(mbox, phba->mbox_mem_pool);
4639 			}
4640 			return 1;
4641 		} else if (rc > 0) {	/* greater than */
4642 			spin_lock_irq(shost->host_lock);
4643 			vport->fc_flag |= FC_PT2PT_PLOGI;
4644 			spin_unlock_irq(shost->host_lock);
4645 		}
4646 		spin_lock_irq(shost->host_lock);
4647 		vport->fc_flag |= FC_PT2PT;
4648 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4649 		spin_unlock_irq(shost->host_lock);
4650 	} else {
4651 		/* Reject this request because invalid parameters */
4652 		stat.un.b.lsRjtRsvd0 = 0;
4653 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4654 		stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
4655 		stat.un.b.vendorUnique = 0;
4656 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4657 			NULL);
4658 		return 1;
4659 	}
4660 
4661 	/* Send back ACC */
4662 	lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
4663 
4664 	return 0;
4665 }
4666 
4667 /**
4668  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
4669  * @vport: pointer to a host virtual N_Port data structure.
4670  * @cmdiocb: pointer to lpfc command iocb data structure.
4671  * @ndlp: pointer to a node-list data structure.
4672  *
4673  * This routine processes Request Node Identification Data (RNID) IOCB
4674  * received as an ELS unsolicited event. Only when the RNID specified format
4675  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
4676  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
4677  * Accept (ACC) the RNID ELS command. All the other RNID formats are
4678  * rejected by invoking the lpfc_els_rsp_reject() routine.
4679  *
4680  * Return code
4681  *   0 - Successfully processed rnid iocb (currently always return 0)
4682  **/
4683 static int
4684 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4685 		  struct lpfc_nodelist *ndlp)
4686 {
4687 	struct lpfc_dmabuf *pcmd;
4688 	uint32_t *lp;
4689 	IOCB_t *icmd;
4690 	RNID *rn;
4691 	struct ls_rjt stat;
4692 	uint32_t cmd, did;
4693 
4694 	icmd = &cmdiocb->iocb;
4695 	did = icmd->un.elsreq64.remoteID;
4696 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4697 	lp = (uint32_t *) pcmd->virt;
4698 
4699 	cmd = *lp++;
4700 	rn = (RNID *) lp;
4701 
4702 	/* RNID received */
4703 
4704 	switch (rn->Format) {
4705 	case 0:
4706 	case RNID_TOPOLOGY_DISC:
4707 		/* Send back ACC */
4708 		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
4709 		break;
4710 	default:
4711 		/* Reject this request because format not supported */
4712 		stat.un.b.lsRjtRsvd0 = 0;
4713 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4714 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4715 		stat.un.b.vendorUnique = 0;
4716 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4717 			NULL);
4718 	}
4719 	return 0;
4720 }
4721 
4722 /**
4723  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
4724  * @vport: pointer to a host virtual N_Port data structure.
4725  * @cmdiocb: pointer to lpfc command iocb data structure.
4726  * @ndlp: pointer to a node-list data structure.
4727  *
4728  * Return code
4729  *   0 - Successfully processed echo iocb (currently always return 0)
4730  **/
4731 static int
4732 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4733 		  struct lpfc_nodelist *ndlp)
4734 {
4735 	uint8_t *pcmd;
4736 
4737 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
4738 
4739 	/* skip over first word of echo command to find echo data */
4740 	pcmd += sizeof(uint32_t);
4741 
4742 	lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
4743 	return 0;
4744 }
4745 
4746 /**
4747  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
4748  * @vport: pointer to a host virtual N_Port data structure.
4749  * @cmdiocb: pointer to lpfc command iocb data structure.
4750  * @ndlp: pointer to a node-list data structure.
4751  *
4752  * This routine processes a Link Incident Report Registration(LIRR) IOCB
4753  * received as an ELS unsolicited event. Currently, this function just invokes
4754  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
4755  *
4756  * Return code
4757  *   0 - Successfully processed lirr iocb (currently always return 0)
4758  **/
4759 static int
4760 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4761 		  struct lpfc_nodelist *ndlp)
4762 {
4763 	struct ls_rjt stat;
4764 
4765 	/* For now, unconditionally reject this command */
4766 	stat.un.b.lsRjtRsvd0 = 0;
4767 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4768 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4769 	stat.un.b.vendorUnique = 0;
4770 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4771 	return 0;
4772 }
4773 
4774 /**
4775  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
4776  * @vport: pointer to a host virtual N_Port data structure.
4777  * @cmdiocb: pointer to lpfc command iocb data structure.
4778  * @ndlp: pointer to a node-list data structure.
4779  *
4780  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
4781  * received as an ELS unsolicited event. A request to RRQ shall only
4782  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
4783  * Nx_Port N_Port_ID of the target Exchange is the same as the
4784  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
4785  * not accepted, an LS_RJT with reason code "Unable to perform
4786  * command request" and reason code explanation "Invalid Originator
4787  * S_ID" shall be returned. For now, we just unconditionally accept
4788  * RRQ from the target.
4789  **/
4790 static void
4791 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4792 		 struct lpfc_nodelist *ndlp)
4793 {
4794 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4795 }
4796 
4797 /**
4798  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
4799  * @phba: pointer to lpfc hba data structure.
4800  * @pmb: pointer to the driver internal queue element for mailbox command.
4801  *
4802  * This routine is the completion callback function for the MBX_READ_LNK_STAT
4803  * mailbox command. This callback function is to actually send the Accept
4804  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
4805  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
4806  * mailbox command, constructs the RPS response with the link statistics
4807  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
4808  * response to the RPS.
4809  *
4810  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4811  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4812  * will be stored into the context1 field of the IOCB for the completion
4813  * callback function to the RPS Accept Response ELS IOCB command.
4814  *
4815  **/
4816 static void
4817 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4818 {
4819 	MAILBOX_t *mb;
4820 	IOCB_t *icmd;
4821 	struct RLS_RSP *rls_rsp;
4822 	uint8_t *pcmd;
4823 	struct lpfc_iocbq *elsiocb;
4824 	struct lpfc_nodelist *ndlp;
4825 	uint16_t xri;
4826 	uint32_t cmdsize;
4827 
4828 	mb = &pmb->u.mb;
4829 
4830 	ndlp = (struct lpfc_nodelist *) pmb->context2;
4831 	xri = (uint16_t) ((unsigned long)(pmb->context1));
4832 	pmb->context1 = NULL;
4833 	pmb->context2 = NULL;
4834 
4835 	if (mb->mbxStatus) {
4836 		mempool_free(pmb, phba->mbox_mem_pool);
4837 		return;
4838 	}
4839 
4840 	cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
4841 	mempool_free(pmb, phba->mbox_mem_pool);
4842 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
4843 				     lpfc_max_els_tries, ndlp,
4844 				     ndlp->nlp_DID, ELS_CMD_ACC);
4845 
4846 	/* Decrement the ndlp reference count from previous mbox command */
4847 	lpfc_nlp_put(ndlp);
4848 
4849 	if (!elsiocb)
4850 		return;
4851 
4852 	icmd = &elsiocb->iocb;
4853 	icmd->ulpContext = xri;
4854 
4855 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4856 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4857 	pcmd += sizeof(uint32_t); /* Skip past command */
4858 	rls_rsp = (struct RLS_RSP *)pcmd;
4859 
4860 	rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
4861 	rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
4862 	rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
4863 	rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
4864 	rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
4865 	rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
4866 
4867 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
4868 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
4869 			 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
4870 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4871 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
4872 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4873 			 ndlp->nlp_rpi);
4874 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4875 	phba->fc_stat.elsXmitACC++;
4876 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
4877 		lpfc_els_free_iocb(phba, elsiocb);
4878 }
4879 
4880 /**
4881  * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
4882  * @phba: pointer to lpfc hba data structure.
4883  * @pmb: pointer to the driver internal queue element for mailbox command.
4884  *
4885  * This routine is the completion callback function for the MBX_READ_LNK_STAT
4886  * mailbox command. This callback function is to actually send the Accept
4887  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
4888  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
4889  * mailbox command, constructs the RPS response with the link statistics
4890  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
4891  * response to the RPS.
4892  *
4893  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4894  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4895  * will be stored into the context1 field of the IOCB for the completion
4896  * callback function to the RPS Accept Response ELS IOCB command.
4897  *
4898  **/
4899 static void
4900 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4901 {
4902 	MAILBOX_t *mb;
4903 	IOCB_t *icmd;
4904 	RPS_RSP *rps_rsp;
4905 	uint8_t *pcmd;
4906 	struct lpfc_iocbq *elsiocb;
4907 	struct lpfc_nodelist *ndlp;
4908 	uint16_t xri, status;
4909 	uint32_t cmdsize;
4910 
4911 	mb = &pmb->u.mb;
4912 
4913 	ndlp = (struct lpfc_nodelist *) pmb->context2;
4914 	xri = (uint16_t) ((unsigned long)(pmb->context1));
4915 	pmb->context1 = NULL;
4916 	pmb->context2 = NULL;
4917 
4918 	if (mb->mbxStatus) {
4919 		mempool_free(pmb, phba->mbox_mem_pool);
4920 		return;
4921 	}
4922 
4923 	cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
4924 	mempool_free(pmb, phba->mbox_mem_pool);
4925 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
4926 				     lpfc_max_els_tries, ndlp,
4927 				     ndlp->nlp_DID, ELS_CMD_ACC);
4928 
4929 	/* Decrement the ndlp reference count from previous mbox command */
4930 	lpfc_nlp_put(ndlp);
4931 
4932 	if (!elsiocb)
4933 		return;
4934 
4935 	icmd = &elsiocb->iocb;
4936 	icmd->ulpContext = xri;
4937 
4938 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4939 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4940 	pcmd += sizeof(uint32_t); /* Skip past command */
4941 	rps_rsp = (RPS_RSP *)pcmd;
4942 
4943 	if (phba->fc_topology != TOPOLOGY_LOOP)
4944 		status = 0x10;
4945 	else
4946 		status = 0x8;
4947 	if (phba->pport->fc_flag & FC_FABRIC)
4948 		status |= 0x4;
4949 
4950 	rps_rsp->rsvd1 = 0;
4951 	rps_rsp->portStatus = cpu_to_be16(status);
4952 	rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
4953 	rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
4954 	rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
4955 	rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
4956 	rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
4957 	rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
4958 	/* Xmit ELS RPS ACC response tag <ulpIoTag> */
4959 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
4960 			 "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
4961 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4962 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
4963 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4964 			 ndlp->nlp_rpi);
4965 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4966 	phba->fc_stat.elsXmitACC++;
4967 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
4968 		lpfc_els_free_iocb(phba, elsiocb);
4969 	return;
4970 }
4971 
4972 /**
4973  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
4974  * @vport: pointer to a host virtual N_Port data structure.
4975  * @cmdiocb: pointer to lpfc command iocb data structure.
4976  * @ndlp: pointer to a node-list data structure.
4977  *
4978  * This routine processes Read Port Status (RPL) IOCB received as an
4979  * ELS unsolicited event. It first checks the remote port state. If the
4980  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
4981  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
4982  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
4983  * for reading the HBA link statistics. It is for the callback function,
4984  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
4985  * to actually sending out RPL Accept (ACC) response.
4986  *
4987  * Return codes
4988  *   0 - Successfully processed rls iocb (currently always return 0)
4989  **/
4990 static int
4991 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4992 		 struct lpfc_nodelist *ndlp)
4993 {
4994 	struct lpfc_hba *phba = vport->phba;
4995 	LPFC_MBOXQ_t *mbox;
4996 	struct lpfc_dmabuf *pcmd;
4997 	struct ls_rjt stat;
4998 
4999 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5000 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5001 		/* reject the unsolicited RPS request and done with it */
5002 		goto reject_out;
5003 
5004 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5005 
5006 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5007 	if (mbox) {
5008 		lpfc_read_lnk_stat(phba, mbox);
5009 		mbox->context1 =
5010 		    (void *)((unsigned long) cmdiocb->iocb.ulpContext);
5011 		mbox->context2 = lpfc_nlp_get(ndlp);
5012 		mbox->vport = vport;
5013 		mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
5014 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5015 			!= MBX_NOT_FINISHED)
5016 			/* Mbox completion will send ELS Response */
5017 			return 0;
5018 		/* Decrement reference count used for the failed mbox
5019 		 * command.
5020 		 */
5021 		lpfc_nlp_put(ndlp);
5022 		mempool_free(mbox, phba->mbox_mem_pool);
5023 	}
5024 reject_out:
5025 	/* issue rejection response */
5026 	stat.un.b.lsRjtRsvd0 = 0;
5027 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5028 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5029 	stat.un.b.vendorUnique = 0;
5030 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5031 	return 0;
5032 }
5033 
5034 /**
5035  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
5036  * @vport: pointer to a host virtual N_Port data structure.
5037  * @cmdiocb: pointer to lpfc command iocb data structure.
5038  * @ndlp: pointer to a node-list data structure.
5039  *
5040  * This routine processes Read Timout Value (RTV) IOCB received as an
5041  * ELS unsolicited event. It first checks the remote port state. If the
5042  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5043  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
5044  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
5045  * Value (RTV) unsolicited IOCB event.
5046  *
5047  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5048  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5049  * will be stored into the context1 field of the IOCB for the completion
5050  * callback function to the RPS Accept Response ELS IOCB command.
5051  *
5052  * Return codes
5053  *   0 - Successfully processed rtv iocb (currently always return 0)
5054  **/
5055 static int
5056 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5057 		 struct lpfc_nodelist *ndlp)
5058 {
5059 	struct lpfc_hba *phba = vport->phba;
5060 	struct ls_rjt stat;
5061 	struct RTV_RSP *rtv_rsp;
5062 	uint8_t *pcmd;
5063 	struct lpfc_iocbq *elsiocb;
5064 	uint32_t cmdsize;
5065 
5066 
5067 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5068 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5069 		/* reject the unsolicited RPS request and done with it */
5070 		goto reject_out;
5071 
5072 	cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
5073 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5074 				     lpfc_max_els_tries, ndlp,
5075 				     ndlp->nlp_DID, ELS_CMD_ACC);
5076 
5077 	if (!elsiocb)
5078 		return 1;
5079 
5080 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5081 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5082 	pcmd += sizeof(uint32_t); /* Skip past command */
5083 
5084 	/* use the command's xri in the response */
5085 	elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;
5086 
5087 	rtv_rsp = (struct RTV_RSP *)pcmd;
5088 
5089 	/* populate RTV payload */
5090 	rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
5091 	rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
5092 	bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
5093 	bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
5094 	rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
5095 
5096 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
5097 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5098 			 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
5099 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
5100 			 "Data: x%x x%x x%x\n",
5101 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
5102 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5103 			 ndlp->nlp_rpi,
5104 			rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
5105 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5106 	phba->fc_stat.elsXmitACC++;
5107 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
5108 		lpfc_els_free_iocb(phba, elsiocb);
5109 	return 0;
5110 
5111 reject_out:
5112 	/* issue rejection response */
5113 	stat.un.b.lsRjtRsvd0 = 0;
5114 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5115 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5116 	stat.un.b.vendorUnique = 0;
5117 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5118 	return 0;
5119 }
5120 
5121 /* lpfc_els_rcv_rps - Process an unsolicited rps iocb
5122  * @vport: pointer to a host virtual N_Port data structure.
5123  * @cmdiocb: pointer to lpfc command iocb data structure.
5124  * @ndlp: pointer to a node-list data structure.
5125  *
5126  * This routine processes Read Port Status (RPS) IOCB received as an
5127  * ELS unsolicited event. It first checks the remote port state. If the
5128  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5129  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
5130  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
5131  * for reading the HBA link statistics. It is for the callback function,
5132  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
5133  * to actually sending out RPS Accept (ACC) response.
5134  *
5135  * Return codes
5136  *   0 - Successfully processed rps iocb (currently always return 0)
5137  **/
5138 static int
5139 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5140 		 struct lpfc_nodelist *ndlp)
5141 {
5142 	struct lpfc_hba *phba = vport->phba;
5143 	uint32_t *lp;
5144 	uint8_t flag;
5145 	LPFC_MBOXQ_t *mbox;
5146 	struct lpfc_dmabuf *pcmd;
5147 	RPS *rps;
5148 	struct ls_rjt stat;
5149 
5150 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5151 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5152 		/* reject the unsolicited RPS request and done with it */
5153 		goto reject_out;
5154 
5155 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5156 	lp = (uint32_t *) pcmd->virt;
5157 	flag = (be32_to_cpu(*lp++) & 0xf);
5158 	rps = (RPS *) lp;
5159 
5160 	if ((flag == 0) ||
5161 	    ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
5162 	    ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
5163 				    sizeof(struct lpfc_name)) == 0))) {
5164 
5165 		printk("Fix me....\n");
5166 		dump_stack();
5167 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5168 		if (mbox) {
5169 			lpfc_read_lnk_stat(phba, mbox);
5170 			mbox->context1 =
5171 			    (void *)((unsigned long) cmdiocb->iocb.ulpContext);
5172 			mbox->context2 = lpfc_nlp_get(ndlp);
5173 			mbox->vport = vport;
5174 			mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
5175 			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5176 				!= MBX_NOT_FINISHED)
5177 				/* Mbox completion will send ELS Response */
5178 				return 0;
5179 			/* Decrement reference count used for the failed mbox
5180 			 * command.
5181 			 */
5182 			lpfc_nlp_put(ndlp);
5183 			mempool_free(mbox, phba->mbox_mem_pool);
5184 		}
5185 	}
5186 
5187 reject_out:
5188 	/* issue rejection response */
5189 	stat.un.b.lsRjtRsvd0 = 0;
5190 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5191 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5192 	stat.un.b.vendorUnique = 0;
5193 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5194 	return 0;
5195 }
5196 
5197 /**
5198  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
5199  * @vport: pointer to a host virtual N_Port data structure.
5200  * @cmdsize: size of the ELS command.
5201  * @oldiocb: pointer to the original lpfc command iocb data structure.
5202  * @ndlp: pointer to a node-list data structure.
5203  *
5204  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
5205  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
5206  *
5207  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5208  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5209  * will be stored into the context1 field of the IOCB for the completion
5210  * callback function to the RPL Accept Response ELS command.
5211  *
5212  * Return code
5213  *   0 - Successfully issued ACC RPL ELS command
5214  *   1 - Failed to issue ACC RPL ELS command
5215  **/
5216 static int
5217 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
5218 		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
5219 {
5220 	struct lpfc_hba *phba = vport->phba;
5221 	IOCB_t *icmd, *oldcmd;
5222 	RPL_RSP rpl_rsp;
5223 	struct lpfc_iocbq *elsiocb;
5224 	uint8_t *pcmd;
5225 
5226 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5227 				     ndlp->nlp_DID, ELS_CMD_ACC);
5228 
5229 	if (!elsiocb)
5230 		return 1;
5231 
5232 	icmd = &elsiocb->iocb;
5233 	oldcmd = &oldiocb->iocb;
5234 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
5235 
5236 	pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5237 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5238 	pcmd += sizeof(uint16_t);
5239 	*((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
5240 	pcmd += sizeof(uint16_t);
5241 
5242 	/* Setup the RPL ACC payload */
5243 	rpl_rsp.listLen = be32_to_cpu(1);
5244 	rpl_rsp.index = 0;
5245 	rpl_rsp.port_num_blk.portNum = 0;
5246 	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
5247 	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
5248 	    sizeof(struct lpfc_name));
5249 	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
5250 	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
5251 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5252 			 "0120 Xmit ELS RPL ACC response tag x%x "
5253 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
5254 			 "rpi x%x\n",
5255 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
5256 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5257 			 ndlp->nlp_rpi);
5258 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5259 	phba->fc_stat.elsXmitACC++;
5260 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
5261 	    IOCB_ERROR) {
5262 		lpfc_els_free_iocb(phba, elsiocb);
5263 		return 1;
5264 	}
5265 	return 0;
5266 }
5267 
5268 /**
5269  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
5270  * @vport: pointer to a host virtual N_Port data structure.
5271  * @cmdiocb: pointer to lpfc command iocb data structure.
5272  * @ndlp: pointer to a node-list data structure.
5273  *
5274  * This routine processes Read Port List (RPL) IOCB received as an ELS
5275  * unsolicited event. It first checks the remote port state. If the remote
5276  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
5277  * invokes the lpfc_els_rsp_reject() routine to send reject response.
5278  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
5279  * to accept the RPL.
5280  *
5281  * Return code
5282  *   0 - Successfully processed rpl iocb (currently always return 0)
5283  **/
5284 static int
5285 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5286 		 struct lpfc_nodelist *ndlp)
5287 {
5288 	struct lpfc_dmabuf *pcmd;
5289 	uint32_t *lp;
5290 	uint32_t maxsize;
5291 	uint16_t cmdsize;
5292 	RPL *rpl;
5293 	struct ls_rjt stat;
5294 
5295 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5296 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
5297 		/* issue rejection response */
5298 		stat.un.b.lsRjtRsvd0 = 0;
5299 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5300 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5301 		stat.un.b.vendorUnique = 0;
5302 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
5303 			NULL);
5304 		/* rejected the unsolicited RPL request and done with it */
5305 		return 0;
5306 	}
5307 
5308 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5309 	lp = (uint32_t *) pcmd->virt;
5310 	rpl = (RPL *) (lp + 1);
5311 	maxsize = be32_to_cpu(rpl->maxsize);
5312 
5313 	/* We support only one port */
5314 	if ((rpl->index == 0) &&
5315 	    ((maxsize == 0) ||
5316 	     ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
5317 		cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
5318 	} else {
5319 		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
5320 	}
5321 	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
5322 
5323 	return 0;
5324 }
5325 
5326 /**
5327  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
5328  * @vport: pointer to a virtual N_Port data structure.
5329  * @cmdiocb: pointer to lpfc command iocb data structure.
5330  * @ndlp: pointer to a node-list data structure.
5331  *
5332  * This routine processes Fibre Channel Address Resolution Protocol
5333  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
5334  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
5335  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
5336  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
5337  * remote PortName is compared against the FC PortName stored in the @vport
5338  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
5339  * compared against the FC NodeName stored in the @vport data structure.
5340  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
5341  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
5342  * invoked to send out FARP Response to the remote node. Before sending the
5343  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
5344  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
5345  * routine is invoked to log into the remote port first.
5346  *
5347  * Return code
5348  *   0 - Either the FARP Match Mode not supported or successfully processed
5349  **/
5350 static int
5351 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5352 		  struct lpfc_nodelist *ndlp)
5353 {
5354 	struct lpfc_dmabuf *pcmd;
5355 	uint32_t *lp;
5356 	IOCB_t *icmd;
5357 	FARP *fp;
5358 	uint32_t cmd, cnt, did;
5359 
5360 	icmd = &cmdiocb->iocb;
5361 	did = icmd->un.elsreq64.remoteID;
5362 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5363 	lp = (uint32_t *) pcmd->virt;
5364 
5365 	cmd = *lp++;
5366 	fp = (FARP *) lp;
5367 	/* FARP-REQ received from DID <did> */
5368 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5369 			 "0601 FARP-REQ received from DID x%x\n", did);
5370 	/* We will only support match on WWPN or WWNN */
5371 	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
5372 		return 0;
5373 	}
5374 
5375 	cnt = 0;
5376 	/* If this FARP command is searching for my portname */
5377 	if (fp->Mflags & FARP_MATCH_PORT) {
5378 		if (memcmp(&fp->RportName, &vport->fc_portname,
5379 			   sizeof(struct lpfc_name)) == 0)
5380 			cnt = 1;
5381 	}
5382 
5383 	/* If this FARP command is searching for my nodename */
5384 	if (fp->Mflags & FARP_MATCH_NODE) {
5385 		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
5386 			   sizeof(struct lpfc_name)) == 0)
5387 			cnt = 1;
5388 	}
5389 
5390 	if (cnt) {
5391 		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
5392 		   (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
5393 			/* Log back into the node before sending the FARP. */
5394 			if (fp->Rflags & FARP_REQUEST_PLOGI) {
5395 				ndlp->nlp_prev_state = ndlp->nlp_state;
5396 				lpfc_nlp_set_state(vport, ndlp,
5397 						   NLP_STE_PLOGI_ISSUE);
5398 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5399 			}
5400 
5401 			/* Send a FARP response to that node */
5402 			if (fp->Rflags & FARP_REQUEST_FARPR)
5403 				lpfc_issue_els_farpr(vport, did, 0);
5404 		}
5405 	}
5406 	return 0;
5407 }
5408 
5409 /**
5410  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
5411  * @vport: pointer to a host virtual N_Port data structure.
5412  * @cmdiocb: pointer to lpfc command iocb data structure.
5413  * @ndlp: pointer to a node-list data structure.
5414  *
5415  * This routine processes Fibre Channel Address Resolution Protocol
5416  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
5417  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
5418  * the FARP response request.
5419  *
5420  * Return code
5421  *   0 - Successfully processed FARPR IOCB (currently always return 0)
5422  **/
5423 static int
5424 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5425 		   struct lpfc_nodelist  *ndlp)
5426 {
5427 	struct lpfc_dmabuf *pcmd;
5428 	uint32_t *lp;
5429 	IOCB_t *icmd;
5430 	uint32_t cmd, did;
5431 
5432 	icmd = &cmdiocb->iocb;
5433 	did = icmd->un.elsreq64.remoteID;
5434 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5435 	lp = (uint32_t *) pcmd->virt;
5436 
5437 	cmd = *lp++;
5438 	/* FARP-RSP received from DID <did> */
5439 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5440 			 "0600 FARP-RSP received from DID x%x\n", did);
5441 	/* ACCEPT the Farp resp request */
5442 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
5443 
5444 	return 0;
5445 }
5446 
5447 /**
5448  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
5449  * @vport: pointer to a host virtual N_Port data structure.
5450  * @cmdiocb: pointer to lpfc command iocb data structure.
5451  * @fan_ndlp: pointer to a node-list data structure.
5452  *
5453  * This routine processes a Fabric Address Notification (FAN) IOCB
5454  * command received as an ELS unsolicited event. The FAN ELS command will
5455  * only be processed on a physical port (i.e., the @vport represents the
5456  * physical port). The fabric NodeName and PortName from the FAN IOCB are
5457  * compared against those in the phba data structure. If any of those is
5458  * different, the lpfc_initial_flogi() routine is invoked to initialize
5459  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
5460  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
5461  * is invoked to register login to the fabric.
5462  *
5463  * Return code
5464  *   0 - Successfully processed fan iocb (currently always return 0).
5465  **/
5466 static int
5467 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5468 		 struct lpfc_nodelist *fan_ndlp)
5469 {
5470 	struct lpfc_hba *phba = vport->phba;
5471 	uint32_t *lp;
5472 	FAN *fp;
5473 
5474 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
5475 	lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
5476 	fp = (FAN *) ++lp;
5477 	/* FAN received; Fan does not have a reply sequence */
5478 	if ((vport == phba->pport) &&
5479 	    (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
5480 		if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
5481 			    sizeof(struct lpfc_name))) ||
5482 		    (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
5483 			    sizeof(struct lpfc_name)))) {
5484 			/* This port has switched fabrics. FLOGI is required */
5485 			lpfc_initial_flogi(vport);
5486 		} else {
5487 			/* FAN verified - skip FLOGI */
5488 			vport->fc_myDID = vport->fc_prevDID;
5489 			if (phba->sli_rev < LPFC_SLI_REV4)
5490 				lpfc_issue_fabric_reglogin(vport);
5491 			else
5492 				lpfc_issue_reg_vfi(vport);
5493 		}
5494 	}
5495 	return 0;
5496 }
5497 
5498 /**
5499  * lpfc_els_timeout - Handler funciton to the els timer
5500  * @ptr: holder for the timer function associated data.
5501  *
5502  * This routine is invoked by the ELS timer after timeout. It posts the ELS
5503  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
5504  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
5505  * up the worker thread. It is for the worker thread to invoke the routine
5506  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
5507  **/
5508 void
5509 lpfc_els_timeout(unsigned long ptr)
5510 {
5511 	struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5512 	struct lpfc_hba   *phba = vport->phba;
5513 	uint32_t tmo_posted;
5514 	unsigned long iflag;
5515 
5516 	spin_lock_irqsave(&vport->work_port_lock, iflag);
5517 	tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
5518 	if (!tmo_posted)
5519 		vport->work_port_events |= WORKER_ELS_TMO;
5520 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
5521 
5522 	if (!tmo_posted)
5523 		lpfc_worker_wake_up(phba);
5524 	return;
5525 }
5526 
5527 
5528 /**
5529  * lpfc_els_timeout_handler - Process an els timeout event
5530  * @vport: pointer to a virtual N_Port data structure.
5531  *
5532  * This routine is the actual handler function that processes an ELS timeout
5533  * event. It walks the ELS ring to get and abort all the IOCBs (except the
5534  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
5535  * invoking the lpfc_sli_issue_abort_iotag() routine.
5536  **/
5537 void
5538 lpfc_els_timeout_handler(struct lpfc_vport *vport)
5539 {
5540 	struct lpfc_hba  *phba = vport->phba;
5541 	struct lpfc_sli_ring *pring;
5542 	struct lpfc_iocbq *tmp_iocb, *piocb;
5543 	IOCB_t *cmd = NULL;
5544 	struct lpfc_dmabuf *pcmd;
5545 	uint32_t els_command = 0;
5546 	uint32_t timeout;
5547 	uint32_t remote_ID = 0xffffffff;
5548 	LIST_HEAD(txcmplq_completions);
5549 	LIST_HEAD(abort_list);
5550 
5551 
5552 	timeout = (uint32_t)(phba->fc_ratov << 1);
5553 
5554 	pring = &phba->sli.ring[LPFC_ELS_RING];
5555 
5556 	spin_lock_irq(&phba->hbalock);
5557 	list_splice_init(&pring->txcmplq, &txcmplq_completions);
5558 	spin_unlock_irq(&phba->hbalock);
5559 
5560 	list_for_each_entry_safe(piocb, tmp_iocb, &txcmplq_completions, list) {
5561 		cmd = &piocb->iocb;
5562 
5563 		if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
5564 		    piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
5565 		    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
5566 			continue;
5567 
5568 		if (piocb->vport != vport)
5569 			continue;
5570 
5571 		pcmd = (struct lpfc_dmabuf *) piocb->context2;
5572 		if (pcmd)
5573 			els_command = *(uint32_t *) (pcmd->virt);
5574 
5575 		if (els_command == ELS_CMD_FARP ||
5576 		    els_command == ELS_CMD_FARPR ||
5577 		    els_command == ELS_CMD_FDISC)
5578 			continue;
5579 
5580 		if (piocb->drvrTimeout > 0) {
5581 			if (piocb->drvrTimeout >= timeout)
5582 				piocb->drvrTimeout -= timeout;
5583 			else
5584 				piocb->drvrTimeout = 0;
5585 			continue;
5586 		}
5587 
5588 		remote_ID = 0xffffffff;
5589 		if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
5590 			remote_ID = cmd->un.elsreq64.remoteID;
5591 		else {
5592 			struct lpfc_nodelist *ndlp;
5593 			ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
5594 			if (ndlp && NLP_CHK_NODE_ACT(ndlp))
5595 				remote_ID = ndlp->nlp_DID;
5596 		}
5597 		list_add_tail(&piocb->dlist, &abort_list);
5598 	}
5599 	spin_lock_irq(&phba->hbalock);
5600 	list_splice(&txcmplq_completions, &pring->txcmplq);
5601 	spin_unlock_irq(&phba->hbalock);
5602 
5603 	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
5604 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5605 			 "0127 ELS timeout Data: x%x x%x x%x "
5606 			 "x%x\n", els_command,
5607 			 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
5608 		spin_lock_irq(&phba->hbalock);
5609 		list_del_init(&piocb->dlist);
5610 		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5611 		spin_unlock_irq(&phba->hbalock);
5612 	}
5613 
5614 	if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
5615 		mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
5616 }
5617 
5618 /**
5619  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
5620  * @vport: pointer to a host virtual N_Port data structure.
5621  *
5622  * This routine is used to clean up all the outstanding ELS commands on a
5623  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
5624  * routine. After that, it walks the ELS transmit queue to remove all the
5625  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
5626  * the IOCBs with a non-NULL completion callback function, the callback
5627  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5628  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
5629  * callback function, the IOCB will simply be released. Finally, it walks
5630  * the ELS transmit completion queue to issue an abort IOCB to any transmit
5631  * completion queue IOCB that is associated with the @vport and is not
5632  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
5633  * part of the discovery state machine) out to HBA by invoking the
5634  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
5635  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
5636  * the IOCBs are aborted when this function returns.
5637  **/
5638 void
5639 lpfc_els_flush_cmd(struct lpfc_vport *vport)
5640 {
5641 	LIST_HEAD(completions);
5642 	struct lpfc_hba  *phba = vport->phba;
5643 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5644 	struct lpfc_iocbq *tmp_iocb, *piocb;
5645 	IOCB_t *cmd = NULL;
5646 
5647 	lpfc_fabric_abort_vport(vport);
5648 
5649 	spin_lock_irq(&phba->hbalock);
5650 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5651 		cmd = &piocb->iocb;
5652 
5653 		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5654 			continue;
5655 		}
5656 
5657 		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5658 		if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5659 		    cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5660 		    cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5661 		    cmd->ulpCommand == CMD_ABORT_XRI_CN)
5662 			continue;
5663 
5664 		if (piocb->vport != vport)
5665 			continue;
5666 
5667 		list_move_tail(&piocb->list, &completions);
5668 		pring->txq_cnt--;
5669 	}
5670 
5671 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5672 		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5673 			continue;
5674 		}
5675 
5676 		if (piocb->vport != vport)
5677 			continue;
5678 
5679 		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5680 	}
5681 	spin_unlock_irq(&phba->hbalock);
5682 
5683 	/* Cancell all the IOCBs from the completions list */
5684 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5685 			      IOERR_SLI_ABORTED);
5686 
5687 	return;
5688 }
5689 
5690 /**
5691  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
5692  * @phba: pointer to lpfc hba data structure.
5693  *
5694  * This routine is used to clean up all the outstanding ELS commands on a
5695  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
5696  * routine. After that, it walks the ELS transmit queue to remove all the
5697  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
5698  * the IOCBs with the completion callback function associated, the callback
5699  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5700  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
5701  * callback function associated, the IOCB will simply be released. Finally,
5702  * it walks the ELS transmit completion queue to issue an abort IOCB to any
5703  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
5704  * management plane IOCBs that are not part of the discovery state machine)
5705  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
5706  **/
5707 void
5708 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
5709 {
5710 	LIST_HEAD(completions);
5711 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5712 	struct lpfc_iocbq *tmp_iocb, *piocb;
5713 	IOCB_t *cmd = NULL;
5714 
5715 	lpfc_fabric_abort_hba(phba);
5716 	spin_lock_irq(&phba->hbalock);
5717 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5718 		cmd = &piocb->iocb;
5719 		if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5720 			continue;
5721 		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5722 		if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5723 		    cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5724 		    cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5725 		    cmd->ulpCommand == CMD_ABORT_XRI_CN)
5726 			continue;
5727 		list_move_tail(&piocb->list, &completions);
5728 		pring->txq_cnt--;
5729 	}
5730 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5731 		if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5732 			continue;
5733 		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5734 	}
5735 	spin_unlock_irq(&phba->hbalock);
5736 
5737 	/* Cancel all the IOCBs from the completions list */
5738 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5739 			      IOERR_SLI_ABORTED);
5740 
5741 	return;
5742 }
5743 
5744 /**
5745  * lpfc_send_els_failure_event - Posts an ELS command failure event
5746  * @phba: Pointer to hba context object.
5747  * @cmdiocbp: Pointer to command iocb which reported error.
5748  * @rspiocbp: Pointer to response iocb which reported error.
5749  *
5750  * This function sends an event when there is an ELS command
5751  * failure.
5752  **/
5753 void
5754 lpfc_send_els_failure_event(struct lpfc_hba *phba,
5755 			struct lpfc_iocbq *cmdiocbp,
5756 			struct lpfc_iocbq *rspiocbp)
5757 {
5758 	struct lpfc_vport *vport = cmdiocbp->vport;
5759 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5760 	struct lpfc_lsrjt_event lsrjt_event;
5761 	struct lpfc_fabric_event_header fabric_event;
5762 	struct ls_rjt stat;
5763 	struct lpfc_nodelist *ndlp;
5764 	uint32_t *pcmd;
5765 
5766 	ndlp = cmdiocbp->context1;
5767 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
5768 		return;
5769 
5770 	if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
5771 		lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
5772 		lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
5773 		memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
5774 			sizeof(struct lpfc_name));
5775 		memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
5776 			sizeof(struct lpfc_name));
5777 		pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
5778 			cmdiocbp->context2)->virt);
5779 		lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
5780 		stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
5781 		lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
5782 		lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
5783 		fc_host_post_vendor_event(shost,
5784 			fc_get_event_number(),
5785 			sizeof(lsrjt_event),
5786 			(char *)&lsrjt_event,
5787 			LPFC_NL_VENDOR_ID);
5788 		return;
5789 	}
5790 	if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
5791 		(rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
5792 		fabric_event.event_type = FC_REG_FABRIC_EVENT;
5793 		if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
5794 			fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
5795 		else
5796 			fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
5797 		memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
5798 			sizeof(struct lpfc_name));
5799 		memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
5800 			sizeof(struct lpfc_name));
5801 		fc_host_post_vendor_event(shost,
5802 			fc_get_event_number(),
5803 			sizeof(fabric_event),
5804 			(char *)&fabric_event,
5805 			LPFC_NL_VENDOR_ID);
5806 		return;
5807 	}
5808 
5809 }
5810 
5811 /**
5812  * lpfc_send_els_event - Posts unsolicited els event
5813  * @vport: Pointer to vport object.
5814  * @ndlp: Pointer FC node object.
5815  * @cmd: ELS command code.
5816  *
5817  * This function posts an event when there is an incoming
5818  * unsolicited ELS command.
5819  **/
5820 static void
5821 lpfc_send_els_event(struct lpfc_vport *vport,
5822 		    struct lpfc_nodelist *ndlp,
5823 		    uint32_t *payload)
5824 {
5825 	struct lpfc_els_event_header *els_data = NULL;
5826 	struct lpfc_logo_event *logo_data = NULL;
5827 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5828 
5829 	if (*payload == ELS_CMD_LOGO) {
5830 		logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
5831 		if (!logo_data) {
5832 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5833 				"0148 Failed to allocate memory "
5834 				"for LOGO event\n");
5835 			return;
5836 		}
5837 		els_data = &logo_data->header;
5838 	} else {
5839 		els_data = kmalloc(sizeof(struct lpfc_els_event_header),
5840 			GFP_KERNEL);
5841 		if (!els_data) {
5842 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5843 				"0149 Failed to allocate memory "
5844 				"for ELS event\n");
5845 			return;
5846 		}
5847 	}
5848 	els_data->event_type = FC_REG_ELS_EVENT;
5849 	switch (*payload) {
5850 	case ELS_CMD_PLOGI:
5851 		els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
5852 		break;
5853 	case ELS_CMD_PRLO:
5854 		els_data->subcategory = LPFC_EVENT_PRLO_RCV;
5855 		break;
5856 	case ELS_CMD_ADISC:
5857 		els_data->subcategory = LPFC_EVENT_ADISC_RCV;
5858 		break;
5859 	case ELS_CMD_LOGO:
5860 		els_data->subcategory = LPFC_EVENT_LOGO_RCV;
5861 		/* Copy the WWPN in the LOGO payload */
5862 		memcpy(logo_data->logo_wwpn, &payload[2],
5863 			sizeof(struct lpfc_name));
5864 		break;
5865 	default:
5866 		kfree(els_data);
5867 		return;
5868 	}
5869 	memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
5870 	memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
5871 	if (*payload == ELS_CMD_LOGO) {
5872 		fc_host_post_vendor_event(shost,
5873 			fc_get_event_number(),
5874 			sizeof(struct lpfc_logo_event),
5875 			(char *)logo_data,
5876 			LPFC_NL_VENDOR_ID);
5877 		kfree(logo_data);
5878 	} else {
5879 		fc_host_post_vendor_event(shost,
5880 			fc_get_event_number(),
5881 			sizeof(struct lpfc_els_event_header),
5882 			(char *)els_data,
5883 			LPFC_NL_VENDOR_ID);
5884 		kfree(els_data);
5885 	}
5886 
5887 	return;
5888 }
5889 
5890 
5891 /**
5892  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
5893  * @phba: pointer to lpfc hba data structure.
5894  * @pring: pointer to a SLI ring.
5895  * @vport: pointer to a host virtual N_Port data structure.
5896  * @elsiocb: pointer to lpfc els command iocb data structure.
5897  *
5898  * This routine is used for processing the IOCB associated with a unsolicited
5899  * event. It first determines whether there is an existing ndlp that matches
5900  * the DID from the unsolicited IOCB. If not, it will create a new one with
5901  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
5902  * IOCB is then used to invoke the proper routine and to set up proper state
5903  * of the discovery state machine.
5904  **/
5905 static void
5906 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5907 		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
5908 {
5909 	struct Scsi_Host  *shost;
5910 	struct lpfc_nodelist *ndlp;
5911 	struct ls_rjt stat;
5912 	uint32_t *payload;
5913 	uint32_t cmd, did, newnode, rjt_err = 0;
5914 	IOCB_t *icmd = &elsiocb->iocb;
5915 
5916 	if (!vport || !(elsiocb->context2))
5917 		goto dropit;
5918 
5919 	newnode = 0;
5920 	payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
5921 	cmd = *payload;
5922 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
5923 		lpfc_post_buffer(phba, pring, 1);
5924 
5925 	did = icmd->un.rcvels.remoteID;
5926 	if (icmd->ulpStatus) {
5927 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5928 			"RCV Unsol ELS:  status:x%x/x%x did:x%x",
5929 			icmd->ulpStatus, icmd->un.ulpWord[4], did);
5930 		goto dropit;
5931 	}
5932 
5933 	/* Check to see if link went down during discovery */
5934 	if (lpfc_els_chk_latt(vport))
5935 		goto dropit;
5936 
5937 	/* Ignore traffic received during vport shutdown. */
5938 	if (vport->load_flag & FC_UNLOADING)
5939 		goto dropit;
5940 
5941 	ndlp = lpfc_findnode_did(vport, did);
5942 	if (!ndlp) {
5943 		/* Cannot find existing Fabric ndlp, so allocate a new one */
5944 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5945 		if (!ndlp)
5946 			goto dropit;
5947 
5948 		lpfc_nlp_init(vport, ndlp, did);
5949 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5950 		newnode = 1;
5951 		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5952 			ndlp->nlp_type |= NLP_FABRIC;
5953 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
5954 		ndlp = lpfc_enable_node(vport, ndlp,
5955 					NLP_STE_UNUSED_NODE);
5956 		if (!ndlp)
5957 			goto dropit;
5958 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5959 		newnode = 1;
5960 		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5961 			ndlp->nlp_type |= NLP_FABRIC;
5962 	} else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
5963 		/* This is similar to the new node path */
5964 		ndlp = lpfc_nlp_get(ndlp);
5965 		if (!ndlp)
5966 			goto dropit;
5967 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5968 		newnode = 1;
5969 	}
5970 
5971 	phba->fc_stat.elsRcvFrame++;
5972 
5973 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5974 	elsiocb->vport = vport;
5975 
5976 	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
5977 		cmd &= ELS_CMD_MASK;
5978 	}
5979 	/* ELS command <elsCmd> received from NPORT <did> */
5980 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5981 			 "0112 ELS command x%x received from NPORT x%x "
5982 			 "Data: x%x\n", cmd, did, vport->port_state);
5983 	switch (cmd) {
5984 	case ELS_CMD_PLOGI:
5985 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5986 			"RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
5987 			did, vport->port_state, ndlp->nlp_flag);
5988 
5989 		phba->fc_stat.elsRcvPLOGI++;
5990 		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
5991 
5992 		lpfc_send_els_event(vport, ndlp, payload);
5993 		if (vport->port_state < LPFC_DISC_AUTH) {
5994 			if (!(phba->pport->fc_flag & FC_PT2PT) ||
5995 				(phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
5996 				rjt_err = LSRJT_UNABLE_TPC;
5997 				break;
5998 			}
5999 			/* We get here, and drop thru, if we are PT2PT with
6000 			 * another NPort and the other side has initiated
6001 			 * the PLOGI before responding to our FLOGI.
6002 			 */
6003 		}
6004 
6005 		shost = lpfc_shost_from_vport(vport);
6006 		spin_lock_irq(shost->host_lock);
6007 		ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
6008 		spin_unlock_irq(shost->host_lock);
6009 
6010 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
6011 					NLP_EVT_RCV_PLOGI);
6012 
6013 		break;
6014 	case ELS_CMD_FLOGI:
6015 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6016 			"RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
6017 			did, vport->port_state, ndlp->nlp_flag);
6018 
6019 		phba->fc_stat.elsRcvFLOGI++;
6020 		lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
6021 		if (newnode)
6022 			lpfc_nlp_put(ndlp);
6023 		break;
6024 	case ELS_CMD_LOGO:
6025 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6026 			"RCV LOGO:        did:x%x/ste:x%x flg:x%x",
6027 			did, vport->port_state, ndlp->nlp_flag);
6028 
6029 		phba->fc_stat.elsRcvLOGO++;
6030 		lpfc_send_els_event(vport, ndlp, payload);
6031 		if (vport->port_state < LPFC_DISC_AUTH) {
6032 			rjt_err = LSRJT_UNABLE_TPC;
6033 			break;
6034 		}
6035 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
6036 		break;
6037 	case ELS_CMD_PRLO:
6038 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6039 			"RCV PRLO:        did:x%x/ste:x%x flg:x%x",
6040 			did, vport->port_state, ndlp->nlp_flag);
6041 
6042 		phba->fc_stat.elsRcvPRLO++;
6043 		lpfc_send_els_event(vport, ndlp, payload);
6044 		if (vport->port_state < LPFC_DISC_AUTH) {
6045 			rjt_err = LSRJT_UNABLE_TPC;
6046 			break;
6047 		}
6048 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
6049 		break;
6050 	case ELS_CMD_RSCN:
6051 		phba->fc_stat.elsRcvRSCN++;
6052 		lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
6053 		if (newnode)
6054 			lpfc_nlp_put(ndlp);
6055 		break;
6056 	case ELS_CMD_ADISC:
6057 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6058 			"RCV ADISC:       did:x%x/ste:x%x flg:x%x",
6059 			did, vport->port_state, ndlp->nlp_flag);
6060 
6061 		lpfc_send_els_event(vport, ndlp, payload);
6062 		phba->fc_stat.elsRcvADISC++;
6063 		if (vport->port_state < LPFC_DISC_AUTH) {
6064 			rjt_err = LSRJT_UNABLE_TPC;
6065 			break;
6066 		}
6067 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
6068 					NLP_EVT_RCV_ADISC);
6069 		break;
6070 	case ELS_CMD_PDISC:
6071 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6072 			"RCV PDISC:       did:x%x/ste:x%x flg:x%x",
6073 			did, vport->port_state, ndlp->nlp_flag);
6074 
6075 		phba->fc_stat.elsRcvPDISC++;
6076 		if (vport->port_state < LPFC_DISC_AUTH) {
6077 			rjt_err = LSRJT_UNABLE_TPC;
6078 			break;
6079 		}
6080 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
6081 					NLP_EVT_RCV_PDISC);
6082 		break;
6083 	case ELS_CMD_FARPR:
6084 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6085 			"RCV FARPR:       did:x%x/ste:x%x flg:x%x",
6086 			did, vport->port_state, ndlp->nlp_flag);
6087 
6088 		phba->fc_stat.elsRcvFARPR++;
6089 		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
6090 		break;
6091 	case ELS_CMD_FARP:
6092 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6093 			"RCV FARP:        did:x%x/ste:x%x flg:x%x",
6094 			did, vport->port_state, ndlp->nlp_flag);
6095 
6096 		phba->fc_stat.elsRcvFARP++;
6097 		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
6098 		break;
6099 	case ELS_CMD_FAN:
6100 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6101 			"RCV FAN:         did:x%x/ste:x%x flg:x%x",
6102 			did, vport->port_state, ndlp->nlp_flag);
6103 
6104 		phba->fc_stat.elsRcvFAN++;
6105 		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
6106 		break;
6107 	case ELS_CMD_PRLI:
6108 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6109 			"RCV PRLI:        did:x%x/ste:x%x flg:x%x",
6110 			did, vport->port_state, ndlp->nlp_flag);
6111 
6112 		phba->fc_stat.elsRcvPRLI++;
6113 		if (vport->port_state < LPFC_DISC_AUTH) {
6114 			rjt_err = LSRJT_UNABLE_TPC;
6115 			break;
6116 		}
6117 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
6118 		break;
6119 	case ELS_CMD_LIRR:
6120 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6121 			"RCV LIRR:        did:x%x/ste:x%x flg:x%x",
6122 			did, vport->port_state, ndlp->nlp_flag);
6123 
6124 		phba->fc_stat.elsRcvLIRR++;
6125 		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
6126 		if (newnode)
6127 			lpfc_nlp_put(ndlp);
6128 		break;
6129 	case ELS_CMD_RLS:
6130 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6131 			"RCV RLS:         did:x%x/ste:x%x flg:x%x",
6132 			did, vport->port_state, ndlp->nlp_flag);
6133 
6134 		phba->fc_stat.elsRcvRLS++;
6135 		lpfc_els_rcv_rls(vport, elsiocb, ndlp);
6136 		if (newnode)
6137 			lpfc_nlp_put(ndlp);
6138 		break;
6139 	case ELS_CMD_RPS:
6140 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6141 			"RCV RPS:         did:x%x/ste:x%x flg:x%x",
6142 			did, vport->port_state, ndlp->nlp_flag);
6143 
6144 		phba->fc_stat.elsRcvRPS++;
6145 		lpfc_els_rcv_rps(vport, elsiocb, ndlp);
6146 		if (newnode)
6147 			lpfc_nlp_put(ndlp);
6148 		break;
6149 	case ELS_CMD_RPL:
6150 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6151 			"RCV RPL:         did:x%x/ste:x%x flg:x%x",
6152 			did, vport->port_state, ndlp->nlp_flag);
6153 
6154 		phba->fc_stat.elsRcvRPL++;
6155 		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
6156 		if (newnode)
6157 			lpfc_nlp_put(ndlp);
6158 		break;
6159 	case ELS_CMD_RNID:
6160 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6161 			"RCV RNID:        did:x%x/ste:x%x flg:x%x",
6162 			did, vport->port_state, ndlp->nlp_flag);
6163 
6164 		phba->fc_stat.elsRcvRNID++;
6165 		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
6166 		if (newnode)
6167 			lpfc_nlp_put(ndlp);
6168 		break;
6169 	case ELS_CMD_RTV:
6170 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6171 			"RCV RTV:        did:x%x/ste:x%x flg:x%x",
6172 			did, vport->port_state, ndlp->nlp_flag);
6173 		phba->fc_stat.elsRcvRTV++;
6174 		lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
6175 		if (newnode)
6176 			lpfc_nlp_put(ndlp);
6177 		break;
6178 	case ELS_CMD_RRQ:
6179 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6180 			"RCV RRQ:         did:x%x/ste:x%x flg:x%x",
6181 			did, vport->port_state, ndlp->nlp_flag);
6182 
6183 		phba->fc_stat.elsRcvRRQ++;
6184 		lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
6185 		if (newnode)
6186 			lpfc_nlp_put(ndlp);
6187 		break;
6188 	case ELS_CMD_ECHO:
6189 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6190 			"RCV ECHO:        did:x%x/ste:x%x flg:x%x",
6191 			did, vport->port_state, ndlp->nlp_flag);
6192 
6193 		phba->fc_stat.elsRcvECHO++;
6194 		lpfc_els_rcv_echo(vport, elsiocb, ndlp);
6195 		if (newnode)
6196 			lpfc_nlp_put(ndlp);
6197 		break;
6198 	default:
6199 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6200 			"RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
6201 			cmd, did, vport->port_state);
6202 
6203 		/* Unsupported ELS command, reject */
6204 		rjt_err = LSRJT_INVALID_CMD;
6205 
6206 		/* Unknown ELS command <elsCmd> received from NPORT <did> */
6207 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6208 				 "0115 Unknown ELS command x%x "
6209 				 "received from NPORT x%x\n", cmd, did);
6210 		if (newnode)
6211 			lpfc_nlp_put(ndlp);
6212 		break;
6213 	}
6214 
6215 	/* check if need to LS_RJT received ELS cmd */
6216 	if (rjt_err) {
6217 		memset(&stat, 0, sizeof(stat));
6218 		stat.un.b.lsRjtRsnCode = rjt_err;
6219 		stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
6220 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
6221 			NULL);
6222 	}
6223 
6224 	lpfc_nlp_put(elsiocb->context1);
6225 	elsiocb->context1 = NULL;
6226 	return;
6227 
6228 dropit:
6229 	if (vport && !(vport->load_flag & FC_UNLOADING))
6230 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6231 			"0111 Dropping received ELS cmd "
6232 			"Data: x%x x%x x%x\n",
6233 			icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
6234 	phba->fc_stat.elsRcvDrop++;
6235 }
6236 
6237 /**
6238  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6239  * @phba: pointer to lpfc hba data structure.
6240  * @vpi: host virtual N_Port identifier.
6241  *
6242  * This routine finds a vport on a HBA (referred by @phba) through a
6243  * @vpi. The function walks the HBA's vport list and returns the address
6244  * of the vport with the matching @vpi.
6245  *
6246  * Return code
6247  *    NULL - No vport with the matching @vpi found
6248  *    Otherwise - Address to the vport with the matching @vpi.
6249  **/
6250 struct lpfc_vport *
6251 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6252 {
6253 	struct lpfc_vport *vport;
6254 	unsigned long flags;
6255 
6256 	spin_lock_irqsave(&phba->hbalock, flags);
6257 	list_for_each_entry(vport, &phba->port_list, listentry) {
6258 		if (vport->vpi == vpi) {
6259 			spin_unlock_irqrestore(&phba->hbalock, flags);
6260 			return vport;
6261 		}
6262 	}
6263 	spin_unlock_irqrestore(&phba->hbalock, flags);
6264 	return NULL;
6265 }
6266 
6267 /**
6268  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
6269  * @phba: pointer to lpfc hba data structure.
6270  * @pring: pointer to a SLI ring.
6271  * @elsiocb: pointer to lpfc els iocb data structure.
6272  *
6273  * This routine is used to process an unsolicited event received from a SLI
6274  * (Service Level Interface) ring. The actual processing of the data buffer
6275  * associated with the unsolicited event is done by invoking the routine
6276  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
6277  * SLI ring on which the unsolicited event was received.
6278  **/
6279 void
6280 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6281 		     struct lpfc_iocbq *elsiocb)
6282 {
6283 	struct lpfc_vport *vport = phba->pport;
6284 	IOCB_t *icmd = &elsiocb->iocb;
6285 	dma_addr_t paddr;
6286 	struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
6287 	struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
6288 
6289 	elsiocb->context1 = NULL;
6290 	elsiocb->context2 = NULL;
6291 	elsiocb->context3 = NULL;
6292 
6293 	if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
6294 		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
6295 	} else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
6296 	    (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
6297 		phba->fc_stat.NoRcvBuf++;
6298 		/* Not enough posted buffers; Try posting more buffers */
6299 		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
6300 			lpfc_post_buffer(phba, pring, 0);
6301 		return;
6302 	}
6303 
6304 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
6305 	    (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
6306 	     icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6307 		if (icmd->unsli3.rcvsli3.vpi == 0xffff)
6308 			vport = phba->pport;
6309 		else
6310 			vport = lpfc_find_vport_by_vpid(phba,
6311 				icmd->unsli3.rcvsli3.vpi - phba->vpi_base);
6312 	}
6313 	/* If there are no BDEs associated
6314 	 * with this IOCB, there is nothing to do.
6315 	 */
6316 	if (icmd->ulpBdeCount == 0)
6317 		return;
6318 
6319 	/* type of ELS cmd is first 32bit word
6320 	 * in packet
6321 	 */
6322 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
6323 		elsiocb->context2 = bdeBuf1;
6324 	} else {
6325 		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
6326 				 icmd->un.cont64[0].addrLow);
6327 		elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
6328 							     paddr);
6329 	}
6330 
6331 	lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
6332 	/*
6333 	 * The different unsolicited event handlers would tell us
6334 	 * if they are done with "mp" by setting context2 to NULL.
6335 	 */
6336 	if (elsiocb->context2) {
6337 		lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
6338 		elsiocb->context2 = NULL;
6339 	}
6340 
6341 	/* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
6342 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
6343 	    icmd->ulpBdeCount == 2) {
6344 		elsiocb->context2 = bdeBuf2;
6345 		lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
6346 		/* free mp if we are done with it */
6347 		if (elsiocb->context2) {
6348 			lpfc_in_buf_free(phba, elsiocb->context2);
6349 			elsiocb->context2 = NULL;
6350 		}
6351 	}
6352 }
6353 
6354 /**
6355  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
6356  * @phba: pointer to lpfc hba data structure.
6357  * @vport: pointer to a virtual N_Port data structure.
6358  *
6359  * This routine issues a Port Login (PLOGI) to the Name Server with
6360  * State Change Request (SCR) for a @vport. This routine will create an
6361  * ndlp for the Name Server associated to the @vport if such node does
6362  * not already exist. The PLOGI to Name Server is issued by invoking the
6363  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
6364  * (FDMI) is configured to the @vport, a FDMI node will be created and
6365  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
6366  **/
6367 void
6368 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
6369 {
6370 	struct lpfc_nodelist *ndlp, *ndlp_fdmi;
6371 
6372 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
6373 	if (!ndlp) {
6374 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
6375 		if (!ndlp) {
6376 			if (phba->fc_topology == TOPOLOGY_LOOP) {
6377 				lpfc_disc_start(vport);
6378 				return;
6379 			}
6380 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6381 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6382 					 "0251 NameServer login: no memory\n");
6383 			return;
6384 		}
6385 		lpfc_nlp_init(vport, ndlp, NameServer_DID);
6386 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
6387 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
6388 		if (!ndlp) {
6389 			if (phba->fc_topology == TOPOLOGY_LOOP) {
6390 				lpfc_disc_start(vport);
6391 				return;
6392 			}
6393 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6394 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6395 					"0348 NameServer login: node freed\n");
6396 			return;
6397 		}
6398 	}
6399 	ndlp->nlp_type |= NLP_FABRIC;
6400 
6401 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6402 
6403 	if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
6404 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6405 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6406 				 "0252 Cannot issue NameServer login\n");
6407 		return;
6408 	}
6409 
6410 	if (vport->cfg_fdmi_on) {
6411 		ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
6412 					  GFP_KERNEL);
6413 		if (ndlp_fdmi) {
6414 			lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
6415 			ndlp_fdmi->nlp_type |= NLP_FABRIC;
6416 			lpfc_nlp_set_state(vport, ndlp_fdmi,
6417 				NLP_STE_PLOGI_ISSUE);
6418 			lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
6419 					     0);
6420 		}
6421 	}
6422 	return;
6423 }
6424 
6425 /**
6426  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
6427  * @phba: pointer to lpfc hba data structure.
6428  * @pmb: pointer to the driver internal queue element for mailbox command.
6429  *
6430  * This routine is the completion callback function to register new vport
6431  * mailbox command. If the new vport mailbox command completes successfully,
6432  * the fabric registration login shall be performed on physical port (the
6433  * new vport created is actually a physical port, with VPI 0) or the port
6434  * login to Name Server for State Change Request (SCR) will be performed
6435  * on virtual port (real virtual port, with VPI greater than 0).
6436  **/
6437 static void
6438 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6439 {
6440 	struct lpfc_vport *vport = pmb->vport;
6441 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
6442 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
6443 	MAILBOX_t *mb = &pmb->u.mb;
6444 	int rc;
6445 
6446 	spin_lock_irq(shost->host_lock);
6447 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
6448 	spin_unlock_irq(shost->host_lock);
6449 
6450 	if (mb->mbxStatus) {
6451 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6452 				"0915 Register VPI failed : Status: x%x"
6453 				" upd bit: x%x \n", mb->mbxStatus,
6454 				 mb->un.varRegVpi.upd);
6455 		if (phba->sli_rev == LPFC_SLI_REV4 &&
6456 			mb->un.varRegVpi.upd)
6457 			goto mbox_err_exit ;
6458 
6459 		switch (mb->mbxStatus) {
6460 		case 0x11:	/* unsupported feature */
6461 		case 0x9603:	/* max_vpi exceeded */
6462 		case 0x9602:	/* Link event since CLEAR_LA */
6463 			/* giving up on vport registration */
6464 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6465 			spin_lock_irq(shost->host_lock);
6466 			vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
6467 			spin_unlock_irq(shost->host_lock);
6468 			lpfc_can_disctmo(vport);
6469 			break;
6470 		/* If reg_vpi fail with invalid VPI status, re-init VPI */
6471 		case 0x20:
6472 			spin_lock_irq(shost->host_lock);
6473 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6474 			spin_unlock_irq(shost->host_lock);
6475 			lpfc_init_vpi(phba, pmb, vport->vpi);
6476 			pmb->vport = vport;
6477 			pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
6478 			rc = lpfc_sli_issue_mbox(phba, pmb,
6479 				MBX_NOWAIT);
6480 			if (rc == MBX_NOT_FINISHED) {
6481 				lpfc_printf_vlog(vport,
6482 					KERN_ERR, LOG_MBOX,
6483 					"2732 Failed to issue INIT_VPI"
6484 					" mailbox command\n");
6485 			} else {
6486 				lpfc_nlp_put(ndlp);
6487 				return;
6488 			}
6489 
6490 		default:
6491 			/* Try to recover from this error */
6492 			if (phba->sli_rev == LPFC_SLI_REV4)
6493 				lpfc_sli4_unreg_all_rpis(vport);
6494 			lpfc_mbx_unreg_vpi(vport);
6495 			spin_lock_irq(shost->host_lock);
6496 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6497 			spin_unlock_irq(shost->host_lock);
6498 			if (vport->port_type == LPFC_PHYSICAL_PORT
6499 				&& !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
6500 				lpfc_initial_flogi(vport);
6501 			else
6502 				lpfc_initial_fdisc(vport);
6503 			break;
6504 		}
6505 	} else {
6506 		spin_lock_irq(shost->host_lock);
6507 		vport->vpi_state |= LPFC_VPI_REGISTERED;
6508 		spin_unlock_irq(shost->host_lock);
6509 		if (vport == phba->pport) {
6510 			if (phba->sli_rev < LPFC_SLI_REV4)
6511 				lpfc_issue_fabric_reglogin(vport);
6512 			else {
6513 				/*
6514 				 * If the physical port is instantiated using
6515 				 * FDISC, do not start vport discovery.
6516 				 */
6517 				if (vport->port_state != LPFC_FDISC)
6518 					lpfc_start_fdiscs(phba);
6519 				lpfc_do_scr_ns_plogi(phba, vport);
6520 			}
6521 		} else
6522 			lpfc_do_scr_ns_plogi(phba, vport);
6523 	}
6524 mbox_err_exit:
6525 	/* Now, we decrement the ndlp reference count held for this
6526 	 * callback function
6527 	 */
6528 	lpfc_nlp_put(ndlp);
6529 
6530 	mempool_free(pmb, phba->mbox_mem_pool);
6531 	return;
6532 }
6533 
6534 /**
6535  * lpfc_register_new_vport - Register a new vport with a HBA
6536  * @phba: pointer to lpfc hba data structure.
6537  * @vport: pointer to a host virtual N_Port data structure.
6538  * @ndlp: pointer to a node-list data structure.
6539  *
6540  * This routine registers the @vport as a new virtual port with a HBA.
6541  * It is done through a registering vpi mailbox command.
6542  **/
6543 void
6544 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
6545 			struct lpfc_nodelist *ndlp)
6546 {
6547 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6548 	LPFC_MBOXQ_t *mbox;
6549 
6550 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6551 	if (mbox) {
6552 		lpfc_reg_vpi(vport, mbox);
6553 		mbox->vport = vport;
6554 		mbox->context2 = lpfc_nlp_get(ndlp);
6555 		mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
6556 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
6557 		    == MBX_NOT_FINISHED) {
6558 			/* mailbox command not success, decrement ndlp
6559 			 * reference count for this command
6560 			 */
6561 			lpfc_nlp_put(ndlp);
6562 			mempool_free(mbox, phba->mbox_mem_pool);
6563 
6564 			lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6565 				"0253 Register VPI: Can't send mbox\n");
6566 			goto mbox_err_exit;
6567 		}
6568 	} else {
6569 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6570 				 "0254 Register VPI: no memory\n");
6571 		goto mbox_err_exit;
6572 	}
6573 	return;
6574 
6575 mbox_err_exit:
6576 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6577 	spin_lock_irq(shost->host_lock);
6578 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
6579 	spin_unlock_irq(shost->host_lock);
6580 	return;
6581 }
6582 
6583 /**
6584  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
6585  * @phba: pointer to lpfc hba data structure.
6586  *
6587  * This routine cancels the retry delay timers to all the vports.
6588  **/
6589 void
6590 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
6591 {
6592 	struct lpfc_vport **vports;
6593 	struct lpfc_nodelist *ndlp;
6594 	uint32_t link_state;
6595 	int i;
6596 
6597 	/* Treat this failure as linkdown for all vports */
6598 	link_state = phba->link_state;
6599 	lpfc_linkdown(phba);
6600 	phba->link_state = link_state;
6601 
6602 	vports = lpfc_create_vport_work_array(phba);
6603 
6604 	if (vports) {
6605 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6606 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6607 			if (ndlp)
6608 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6609 			lpfc_els_flush_cmd(vports[i]);
6610 		}
6611 		lpfc_destroy_vport_work_array(phba, vports);
6612 	}
6613 }
6614 
6615 /**
6616  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
6617  * @phba: pointer to lpfc hba data structure.
6618  *
6619  * This routine abort all pending discovery commands and
6620  * start a timer to retry FLOGI for the physical port
6621  * discovery.
6622  **/
6623 void
6624 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
6625 {
6626 	struct lpfc_nodelist *ndlp;
6627 	struct Scsi_Host  *shost;
6628 
6629 	/* Cancel the all vports retry delay retry timers */
6630 	lpfc_cancel_all_vport_retry_delay_timer(phba);
6631 
6632 	/* If fabric require FLOGI, then re-instantiate physical login */
6633 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6634 	if (!ndlp)
6635 		return;
6636 
6637 	shost = lpfc_shost_from_vport(phba->pport);
6638 	mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
6639 	spin_lock_irq(shost->host_lock);
6640 	ndlp->nlp_flag |= NLP_DELAY_TMO;
6641 	spin_unlock_irq(shost->host_lock);
6642 	ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
6643 	phba->pport->port_state = LPFC_FLOGI;
6644 	return;
6645 }
6646 
6647 /**
6648  * lpfc_fabric_login_reqd - Check if FLOGI required.
6649  * @phba: pointer to lpfc hba data structure.
6650  * @cmdiocb: pointer to FDISC command iocb.
6651  * @rspiocb: pointer to FDISC response iocb.
6652  *
6653  * This routine checks if a FLOGI is reguired for FDISC
6654  * to succeed.
6655  **/
6656 static int
6657 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
6658 		struct lpfc_iocbq *cmdiocb,
6659 		struct lpfc_iocbq *rspiocb)
6660 {
6661 
6662 	if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
6663 		(rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
6664 		return 0;
6665 	else
6666 		return 1;
6667 }
6668 
6669 /**
6670  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
6671  * @phba: pointer to lpfc hba data structure.
6672  * @cmdiocb: pointer to lpfc command iocb data structure.
6673  * @rspiocb: pointer to lpfc response iocb data structure.
6674  *
6675  * This routine is the completion callback function to a Fabric Discover
6676  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
6677  * single threaded, each FDISC completion callback function will reset
6678  * the discovery timer for all vports such that the timers will not get
6679  * unnecessary timeout. The function checks the FDISC IOCB status. If error
6680  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
6681  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
6682  * assigned to the vport has been changed with the completion of the FDISC
6683  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
6684  * are unregistered from the HBA, and then the lpfc_register_new_vport()
6685  * routine is invoked to register new vport with the HBA. Otherwise, the
6686  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
6687  * Server for State Change Request (SCR).
6688  **/
6689 static void
6690 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6691 		    struct lpfc_iocbq *rspiocb)
6692 {
6693 	struct lpfc_vport *vport = cmdiocb->vport;
6694 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
6695 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
6696 	struct lpfc_nodelist *np;
6697 	struct lpfc_nodelist *next_np;
6698 	IOCB_t *irsp = &rspiocb->iocb;
6699 	struct lpfc_iocbq *piocb;
6700 
6701 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6702 			 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
6703 			 irsp->ulpStatus, irsp->un.ulpWord[4],
6704 			 vport->fc_prevDID);
6705 	/* Since all FDISCs are being single threaded, we
6706 	 * must reset the discovery timer for ALL vports
6707 	 * waiting to send FDISC when one completes.
6708 	 */
6709 	list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
6710 		lpfc_set_disctmo(piocb->vport);
6711 	}
6712 
6713 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6714 		"FDISC cmpl:      status:x%x/x%x prevdid:x%x",
6715 		irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
6716 
6717 	if (irsp->ulpStatus) {
6718 
6719 		if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
6720 			lpfc_retry_pport_discovery(phba);
6721 			goto out;
6722 		}
6723 
6724 		/* Check for retry */
6725 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
6726 			goto out;
6727 		/* FDISC failed */
6728 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6729 				 "0126 FDISC failed. (%d/%d)\n",
6730 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
6731 		goto fdisc_failed;
6732 	}
6733 	spin_lock_irq(shost->host_lock);
6734 	vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
6735 	vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
6736 	vport->fc_flag |= FC_FABRIC;
6737 	if (vport->phba->fc_topology == TOPOLOGY_LOOP)
6738 		vport->fc_flag |=  FC_PUBLIC_LOOP;
6739 	spin_unlock_irq(shost->host_lock);
6740 
6741 	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
6742 	lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
6743 	if ((vport->fc_prevDID != vport->fc_myDID) &&
6744 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
6745 		/* If our NportID changed, we need to ensure all
6746 		 * remaining NPORTs get unreg_login'ed so we can
6747 		 * issue unreg_vpi.
6748 		 */
6749 		list_for_each_entry_safe(np, next_np,
6750 			&vport->fc_nodes, nlp_listp) {
6751 			if (!NLP_CHK_NODE_ACT(ndlp) ||
6752 			    (np->nlp_state != NLP_STE_NPR_NODE) ||
6753 			    !(np->nlp_flag & NLP_NPR_ADISC))
6754 				continue;
6755 			spin_lock_irq(shost->host_lock);
6756 			np->nlp_flag &= ~NLP_NPR_ADISC;
6757 			spin_unlock_irq(shost->host_lock);
6758 			lpfc_unreg_rpi(vport, np);
6759 		}
6760 		lpfc_cleanup_pending_mbox(vport);
6761 
6762 		if (phba->sli_rev == LPFC_SLI_REV4)
6763 			lpfc_sli4_unreg_all_rpis(vport);
6764 
6765 		lpfc_mbx_unreg_vpi(vport);
6766 		spin_lock_irq(shost->host_lock);
6767 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6768 		if (phba->sli_rev == LPFC_SLI_REV4)
6769 			vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6770 		else
6771 			vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
6772 		spin_unlock_irq(shost->host_lock);
6773 	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
6774 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
6775 		/*
6776 		 * Driver needs to re-reg VPI in order for f/w
6777 		 * to update the MAC address.
6778 		 */
6779 		lpfc_register_new_vport(phba, vport, ndlp);
6780 		goto out;
6781 	}
6782 
6783 	if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
6784 		lpfc_issue_init_vpi(vport);
6785 	else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
6786 		lpfc_register_new_vport(phba, vport, ndlp);
6787 	else
6788 		lpfc_do_scr_ns_plogi(phba, vport);
6789 	goto out;
6790 fdisc_failed:
6791 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6792 	/* Cancel discovery timer */
6793 	lpfc_can_disctmo(vport);
6794 	lpfc_nlp_put(ndlp);
6795 out:
6796 	lpfc_els_free_iocb(phba, cmdiocb);
6797 }
6798 
6799 /**
6800  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
6801  * @vport: pointer to a virtual N_Port data structure.
6802  * @ndlp: pointer to a node-list data structure.
6803  * @retry: number of retries to the command IOCB.
6804  *
6805  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
6806  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
6807  * routine to issue the IOCB, which makes sure only one outstanding fabric
6808  * IOCB will be sent off HBA at any given time.
6809  *
6810  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6811  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6812  * will be stored into the context1 field of the IOCB for the completion
6813  * callback function to the FDISC ELS command.
6814  *
6815  * Return code
6816  *   0 - Successfully issued fdisc iocb command
6817  *   1 - Failed to issue fdisc iocb command
6818  **/
6819 static int
6820 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
6821 		     uint8_t retry)
6822 {
6823 	struct lpfc_hba *phba = vport->phba;
6824 	IOCB_t *icmd;
6825 	struct lpfc_iocbq *elsiocb;
6826 	struct serv_parm *sp;
6827 	uint8_t *pcmd;
6828 	uint16_t cmdsize;
6829 	int did = ndlp->nlp_DID;
6830 	int rc;
6831 
6832 	vport->port_state = LPFC_FDISC;
6833 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
6834 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
6835 				     ELS_CMD_FDISC);
6836 	if (!elsiocb) {
6837 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6838 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6839 				 "0255 Issue FDISC: no IOCB\n");
6840 		return 1;
6841 	}
6842 
6843 	icmd = &elsiocb->iocb;
6844 	icmd->un.elsreq64.myID = 0;
6845 	icmd->un.elsreq64.fl = 1;
6846 
6847 	if  (phba->sli_rev == LPFC_SLI_REV4) {
6848 		/* FDISC needs to be 1 for WQE VPI */
6849 		elsiocb->iocb.ulpCt_h = (SLI4_CT_VPI >> 1) & 1;
6850 		elsiocb->iocb.ulpCt_l = SLI4_CT_VPI & 1 ;
6851 		/* Set the ulpContext to the vpi */
6852 		elsiocb->iocb.ulpContext = vport->vpi + phba->vpi_base;
6853 	} else {
6854 		/* For FDISC, Let FDISC rsp set the NPortID for this VPI */
6855 		icmd->ulpCt_h = 1;
6856 		icmd->ulpCt_l = 0;
6857 	}
6858 
6859 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6860 	*((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
6861 	pcmd += sizeof(uint32_t); /* CSP Word 1 */
6862 	memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
6863 	sp = (struct serv_parm *) pcmd;
6864 	/* Setup CSPs accordingly for Fabric */
6865 	sp->cmn.e_d_tov = 0;
6866 	sp->cmn.w2.r_a_tov = 0;
6867 	sp->cls1.classValid = 0;
6868 	sp->cls2.seqDelivery = 1;
6869 	sp->cls3.seqDelivery = 1;
6870 
6871 	pcmd += sizeof(uint32_t); /* CSP Word 2 */
6872 	pcmd += sizeof(uint32_t); /* CSP Word 3 */
6873 	pcmd += sizeof(uint32_t); /* CSP Word 4 */
6874 	pcmd += sizeof(uint32_t); /* Port Name */
6875 	memcpy(pcmd, &vport->fc_portname, 8);
6876 	pcmd += sizeof(uint32_t); /* Node Name */
6877 	pcmd += sizeof(uint32_t); /* Node Name */
6878 	memcpy(pcmd, &vport->fc_nodename, 8);
6879 
6880 	lpfc_set_disctmo(vport);
6881 
6882 	phba->fc_stat.elsXmitFDISC++;
6883 	elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
6884 
6885 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6886 		"Issue FDISC:     did:x%x",
6887 		did, 0, 0);
6888 
6889 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
6890 	if (rc == IOCB_ERROR) {
6891 		lpfc_els_free_iocb(phba, elsiocb);
6892 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6893 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6894 				 "0256 Issue FDISC: Cannot send IOCB\n");
6895 		return 1;
6896 	}
6897 	lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
6898 	return 0;
6899 }
6900 
6901 /**
6902  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
6903  * @phba: pointer to lpfc hba data structure.
6904  * @cmdiocb: pointer to lpfc command iocb data structure.
6905  * @rspiocb: pointer to lpfc response iocb data structure.
6906  *
6907  * This routine is the completion callback function to the issuing of a LOGO
6908  * ELS command off a vport. It frees the command IOCB and then decrement the
6909  * reference count held on ndlp for this completion function, indicating that
6910  * the reference to the ndlp is no long needed. Note that the
6911  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
6912  * callback function and an additional explicit ndlp reference decrementation
6913  * will trigger the actual release of the ndlp.
6914  **/
6915 static void
6916 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6917 			struct lpfc_iocbq *rspiocb)
6918 {
6919 	struct lpfc_vport *vport = cmdiocb->vport;
6920 	IOCB_t *irsp;
6921 	struct lpfc_nodelist *ndlp;
6922 	ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
6923 
6924 	irsp = &rspiocb->iocb;
6925 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6926 		"LOGO npiv cmpl:  status:x%x/x%x did:x%x",
6927 		irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
6928 
6929 	lpfc_els_free_iocb(phba, cmdiocb);
6930 	vport->unreg_vpi_cmpl = VPORT_ERROR;
6931 
6932 	/* Trigger the release of the ndlp after logo */
6933 	lpfc_nlp_put(ndlp);
6934 }
6935 
6936 /**
6937  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
6938  * @vport: pointer to a virtual N_Port data structure.
6939  * @ndlp: pointer to a node-list data structure.
6940  *
6941  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
6942  *
6943  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6944  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6945  * will be stored into the context1 field of the IOCB for the completion
6946  * callback function to the LOGO ELS command.
6947  *
6948  * Return codes
6949  *   0 - Successfully issued logo off the @vport
6950  *   1 - Failed to issue logo off the @vport
6951  **/
6952 int
6953 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
6954 {
6955 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6956 	struct lpfc_hba  *phba = vport->phba;
6957 	IOCB_t *icmd;
6958 	struct lpfc_iocbq *elsiocb;
6959 	uint8_t *pcmd;
6960 	uint16_t cmdsize;
6961 
6962 	cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
6963 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
6964 				     ELS_CMD_LOGO);
6965 	if (!elsiocb)
6966 		return 1;
6967 
6968 	icmd = &elsiocb->iocb;
6969 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6970 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
6971 	pcmd += sizeof(uint32_t);
6972 
6973 	/* Fill in LOGO payload */
6974 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
6975 	pcmd += sizeof(uint32_t);
6976 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
6977 
6978 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6979 		"Issue LOGO npiv  did:x%x flg:x%x",
6980 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
6981 
6982 	elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
6983 	spin_lock_irq(shost->host_lock);
6984 	ndlp->nlp_flag |= NLP_LOGO_SND;
6985 	spin_unlock_irq(shost->host_lock);
6986 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
6987 	    IOCB_ERROR) {
6988 		spin_lock_irq(shost->host_lock);
6989 		ndlp->nlp_flag &= ~NLP_LOGO_SND;
6990 		spin_unlock_irq(shost->host_lock);
6991 		lpfc_els_free_iocb(phba, elsiocb);
6992 		return 1;
6993 	}
6994 	return 0;
6995 }
6996 
6997 /**
6998  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
6999  * @ptr: holder for the timer function associated data.
7000  *
7001  * This routine is invoked by the fabric iocb block timer after
7002  * timeout. It posts the fabric iocb block timeout event by setting the
7003  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
7004  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
7005  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
7006  * posted event WORKER_FABRIC_BLOCK_TMO.
7007  **/
7008 void
7009 lpfc_fabric_block_timeout(unsigned long ptr)
7010 {
7011 	struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
7012 	unsigned long iflags;
7013 	uint32_t tmo_posted;
7014 
7015 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
7016 	tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
7017 	if (!tmo_posted)
7018 		phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
7019 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
7020 
7021 	if (!tmo_posted)
7022 		lpfc_worker_wake_up(phba);
7023 	return;
7024 }
7025 
7026 /**
7027  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
7028  * @phba: pointer to lpfc hba data structure.
7029  *
7030  * This routine issues one fabric iocb from the driver internal list to
7031  * the HBA. It first checks whether it's ready to issue one fabric iocb to
7032  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
7033  * remove one pending fabric iocb from the driver internal list and invokes
7034  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
7035  **/
7036 static void
7037 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
7038 {
7039 	struct lpfc_iocbq *iocb;
7040 	unsigned long iflags;
7041 	int ret;
7042 	IOCB_t *cmd;
7043 
7044 repeat:
7045 	iocb = NULL;
7046 	spin_lock_irqsave(&phba->hbalock, iflags);
7047 	/* Post any pending iocb to the SLI layer */
7048 	if (atomic_read(&phba->fabric_iocb_count) == 0) {
7049 		list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
7050 				 list);
7051 		if (iocb)
7052 			/* Increment fabric iocb count to hold the position */
7053 			atomic_inc(&phba->fabric_iocb_count);
7054 	}
7055 	spin_unlock_irqrestore(&phba->hbalock, iflags);
7056 	if (iocb) {
7057 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
7058 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
7059 		iocb->iocb_flag |= LPFC_IO_FABRIC;
7060 
7061 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
7062 			"Fabric sched1:   ste:x%x",
7063 			iocb->vport->port_state, 0, 0);
7064 
7065 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
7066 
7067 		if (ret == IOCB_ERROR) {
7068 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
7069 			iocb->fabric_iocb_cmpl = NULL;
7070 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
7071 			cmd = &iocb->iocb;
7072 			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
7073 			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
7074 			iocb->iocb_cmpl(phba, iocb, iocb);
7075 
7076 			atomic_dec(&phba->fabric_iocb_count);
7077 			goto repeat;
7078 		}
7079 	}
7080 
7081 	return;
7082 }
7083 
7084 /**
7085  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
7086  * @phba: pointer to lpfc hba data structure.
7087  *
7088  * This routine unblocks the  issuing fabric iocb command. The function
7089  * will clear the fabric iocb block bit and then invoke the routine
7090  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
7091  * from the driver internal fabric iocb list.
7092  **/
7093 void
7094 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
7095 {
7096 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7097 
7098 	lpfc_resume_fabric_iocbs(phba);
7099 	return;
7100 }
7101 
7102 /**
7103  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
7104  * @phba: pointer to lpfc hba data structure.
7105  *
7106  * This routine blocks the issuing fabric iocb for a specified amount of
7107  * time (currently 100 ms). This is done by set the fabric iocb block bit
7108  * and set up a timeout timer for 100ms. When the block bit is set, no more
7109  * fabric iocb will be issued out of the HBA.
7110  **/
7111 static void
7112 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
7113 {
7114 	int blocked;
7115 
7116 	blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7117 	/* Start a timer to unblock fabric iocbs after 100ms */
7118 	if (!blocked)
7119 		mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
7120 
7121 	return;
7122 }
7123 
7124 /**
7125  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
7126  * @phba: pointer to lpfc hba data structure.
7127  * @cmdiocb: pointer to lpfc command iocb data structure.
7128  * @rspiocb: pointer to lpfc response iocb data structure.
7129  *
7130  * This routine is the callback function that is put to the fabric iocb's
7131  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
7132  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
7133  * function first restores and invokes the original iocb's callback function
7134  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
7135  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
7136  **/
7137 static void
7138 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7139 	struct lpfc_iocbq *rspiocb)
7140 {
7141 	struct ls_rjt stat;
7142 
7143 	if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
7144 		BUG();
7145 
7146 	switch (rspiocb->iocb.ulpStatus) {
7147 		case IOSTAT_NPORT_RJT:
7148 		case IOSTAT_FABRIC_RJT:
7149 			if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
7150 				lpfc_block_fabric_iocbs(phba);
7151 			}
7152 			break;
7153 
7154 		case IOSTAT_NPORT_BSY:
7155 		case IOSTAT_FABRIC_BSY:
7156 			lpfc_block_fabric_iocbs(phba);
7157 			break;
7158 
7159 		case IOSTAT_LS_RJT:
7160 			stat.un.lsRjtError =
7161 				be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
7162 			if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
7163 				(stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
7164 				lpfc_block_fabric_iocbs(phba);
7165 			break;
7166 	}
7167 
7168 	if (atomic_read(&phba->fabric_iocb_count) == 0)
7169 		BUG();
7170 
7171 	cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
7172 	cmdiocb->fabric_iocb_cmpl = NULL;
7173 	cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
7174 	cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
7175 
7176 	atomic_dec(&phba->fabric_iocb_count);
7177 	if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
7178 		/* Post any pending iocbs to HBA */
7179 		lpfc_resume_fabric_iocbs(phba);
7180 	}
7181 }
7182 
7183 /**
7184  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
7185  * @phba: pointer to lpfc hba data structure.
7186  * @iocb: pointer to lpfc command iocb data structure.
7187  *
7188  * This routine is used as the top-level API for issuing a fabric iocb command
7189  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
7190  * function makes sure that only one fabric bound iocb will be outstanding at
7191  * any given time. As such, this function will first check to see whether there
7192  * is already an outstanding fabric iocb on the wire. If so, it will put the
7193  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
7194  * issued later. Otherwise, it will issue the iocb on the wire and update the
7195  * fabric iocb count it indicate that there is one fabric iocb on the wire.
7196  *
7197  * Note, this implementation has a potential sending out fabric IOCBs out of
7198  * order. The problem is caused by the construction of the "ready" boolen does
7199  * not include the condition that the internal fabric IOCB list is empty. As
7200  * such, it is possible a fabric IOCB issued by this routine might be "jump"
7201  * ahead of the fabric IOCBs in the internal list.
7202  *
7203  * Return code
7204  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
7205  *   IOCB_ERROR - failed to issue fabric iocb
7206  **/
7207 static int
7208 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
7209 {
7210 	unsigned long iflags;
7211 	int ready;
7212 	int ret;
7213 
7214 	if (atomic_read(&phba->fabric_iocb_count) > 1)
7215 		BUG();
7216 
7217 	spin_lock_irqsave(&phba->hbalock, iflags);
7218 	ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
7219 		!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7220 
7221 	if (ready)
7222 		/* Increment fabric iocb count to hold the position */
7223 		atomic_inc(&phba->fabric_iocb_count);
7224 	spin_unlock_irqrestore(&phba->hbalock, iflags);
7225 	if (ready) {
7226 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
7227 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
7228 		iocb->iocb_flag |= LPFC_IO_FABRIC;
7229 
7230 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
7231 			"Fabric sched2:   ste:x%x",
7232 			iocb->vport->port_state, 0, 0);
7233 
7234 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
7235 
7236 		if (ret == IOCB_ERROR) {
7237 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
7238 			iocb->fabric_iocb_cmpl = NULL;
7239 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
7240 			atomic_dec(&phba->fabric_iocb_count);
7241 		}
7242 	} else {
7243 		spin_lock_irqsave(&phba->hbalock, iflags);
7244 		list_add_tail(&iocb->list, &phba->fabric_iocb_list);
7245 		spin_unlock_irqrestore(&phba->hbalock, iflags);
7246 		ret = IOCB_SUCCESS;
7247 	}
7248 	return ret;
7249 }
7250 
7251 /**
7252  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
7253  * @vport: pointer to a virtual N_Port data structure.
7254  *
7255  * This routine aborts all the IOCBs associated with a @vport from the
7256  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
7257  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
7258  * list, removes each IOCB associated with the @vport off the list, set the
7259  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
7260  * associated with the IOCB.
7261  **/
7262 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
7263 {
7264 	LIST_HEAD(completions);
7265 	struct lpfc_hba  *phba = vport->phba;
7266 	struct lpfc_iocbq *tmp_iocb, *piocb;
7267 
7268 	spin_lock_irq(&phba->hbalock);
7269 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
7270 				 list) {
7271 
7272 		if (piocb->vport != vport)
7273 			continue;
7274 
7275 		list_move_tail(&piocb->list, &completions);
7276 	}
7277 	spin_unlock_irq(&phba->hbalock);
7278 
7279 	/* Cancel all the IOCBs from the completions list */
7280 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7281 			      IOERR_SLI_ABORTED);
7282 }
7283 
7284 /**
7285  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
7286  * @ndlp: pointer to a node-list data structure.
7287  *
7288  * This routine aborts all the IOCBs associated with an @ndlp from the
7289  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
7290  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
7291  * list, removes each IOCB associated with the @ndlp off the list, set the
7292  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
7293  * associated with the IOCB.
7294  **/
7295 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
7296 {
7297 	LIST_HEAD(completions);
7298 	struct lpfc_hba  *phba = ndlp->phba;
7299 	struct lpfc_iocbq *tmp_iocb, *piocb;
7300 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7301 
7302 	spin_lock_irq(&phba->hbalock);
7303 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
7304 				 list) {
7305 		if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
7306 
7307 			list_move_tail(&piocb->list, &completions);
7308 		}
7309 	}
7310 	spin_unlock_irq(&phba->hbalock);
7311 
7312 	/* Cancel all the IOCBs from the completions list */
7313 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7314 			      IOERR_SLI_ABORTED);
7315 }
7316 
7317 /**
7318  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
7319  * @phba: pointer to lpfc hba data structure.
7320  *
7321  * This routine aborts all the IOCBs currently on the driver internal
7322  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
7323  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
7324  * list, removes IOCBs off the list, set the status feild to
7325  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
7326  * the IOCB.
7327  **/
7328 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
7329 {
7330 	LIST_HEAD(completions);
7331 
7332 	spin_lock_irq(&phba->hbalock);
7333 	list_splice_init(&phba->fabric_iocb_list, &completions);
7334 	spin_unlock_irq(&phba->hbalock);
7335 
7336 	/* Cancel all the IOCBs from the completions list */
7337 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7338 			      IOERR_SLI_ABORTED);
7339 }
7340 
7341 /**
7342  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
7343  * @phba: pointer to lpfc hba data structure.
7344  * @axri: pointer to the els xri abort wcqe structure.
7345  *
7346  * This routine is invoked by the worker thread to process a SLI4 slow-path
7347  * ELS aborted xri.
7348  **/
7349 void
7350 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
7351 			  struct sli4_wcqe_xri_aborted *axri)
7352 {
7353 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
7354 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7355 	unsigned long iflag = 0;
7356 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7357 
7358 	spin_lock_irqsave(&phba->hbalock, iflag);
7359 	spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
7360 	list_for_each_entry_safe(sglq_entry, sglq_next,
7361 			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
7362 		if (sglq_entry->sli4_xritag == xri) {
7363 			list_del(&sglq_entry->list);
7364 			list_add_tail(&sglq_entry->list,
7365 				&phba->sli4_hba.lpfc_sgl_list);
7366 			sglq_entry->state = SGL_FREED;
7367 			spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
7368 			spin_unlock_irqrestore(&phba->hbalock, iflag);
7369 
7370 			/* Check if TXQ queue needs to be serviced */
7371 			if (pring->txq_cnt)
7372 				lpfc_worker_wake_up(phba);
7373 			return;
7374 		}
7375 	}
7376 	spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
7377 	sglq_entry = __lpfc_get_active_sglq(phba, xri);
7378 	if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
7379 		spin_unlock_irqrestore(&phba->hbalock, iflag);
7380 		return;
7381 	}
7382 	sglq_entry->state = SGL_XRI_ABORTED;
7383 	spin_unlock_irqrestore(&phba->hbalock, iflag);
7384 	return;
7385 }
7386