• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 
35 #include "lpfc_hw4.h"
36 #include "lpfc_hw.h"
37 #include "lpfc_sli.h"
38 #include "lpfc_sli4.h"
39 #include "lpfc_nl.h"
40 #include "lpfc_disc.h"
41 #include "lpfc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc_nvme.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_vport.h"
47 #include "lpfc_debugfs.h"
48 
49 
50 /* Called to verify a rcv'ed ADISC was intended for us. */
51 static int
lpfc_check_adisc(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct lpfc_name * nn,struct lpfc_name * pn)52 lpfc_check_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
53 		 struct lpfc_name *nn, struct lpfc_name *pn)
54 {
55 	/* First, we MUST have a RPI registered */
56 	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED))
57 		return 0;
58 
59 	/* Compare the ADISC rsp WWNN / WWPN matches our internal node
60 	 * table entry for that node.
61 	 */
62 	if (memcmp(nn, &ndlp->nlp_nodename, sizeof (struct lpfc_name)))
63 		return 0;
64 
65 	if (memcmp(pn, &ndlp->nlp_portname, sizeof (struct lpfc_name)))
66 		return 0;
67 
68 	/* we match, return success */
69 	return 1;
70 }
71 
72 int
lpfc_check_sparm(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct serv_parm * sp,uint32_t class,int flogi)73 lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
74 		 struct serv_parm *sp, uint32_t class, int flogi)
75 {
76 	volatile struct serv_parm *hsp = &vport->fc_sparam;
77 	uint16_t hsp_value, ssp_value = 0;
78 
79 	/*
80 	 * The receive data field size and buffer-to-buffer receive data field
81 	 * size entries are 16 bits but are represented as two 8-bit fields in
82 	 * the driver data structure to account for rsvd bits and other control
83 	 * bits.  Reconstruct and compare the fields as a 16-bit values before
84 	 * correcting the byte values.
85 	 */
86 	if (sp->cls1.classValid) {
87 		if (!flogi) {
88 			hsp_value = ((hsp->cls1.rcvDataSizeMsb << 8) |
89 				     hsp->cls1.rcvDataSizeLsb);
90 			ssp_value = ((sp->cls1.rcvDataSizeMsb << 8) |
91 				     sp->cls1.rcvDataSizeLsb);
92 			if (!ssp_value)
93 				goto bad_service_param;
94 			if (ssp_value > hsp_value) {
95 				sp->cls1.rcvDataSizeLsb =
96 					hsp->cls1.rcvDataSizeLsb;
97 				sp->cls1.rcvDataSizeMsb =
98 					hsp->cls1.rcvDataSizeMsb;
99 			}
100 		}
101 	} else if (class == CLASS1)
102 		goto bad_service_param;
103 	if (sp->cls2.classValid) {
104 		if (!flogi) {
105 			hsp_value = ((hsp->cls2.rcvDataSizeMsb << 8) |
106 				     hsp->cls2.rcvDataSizeLsb);
107 			ssp_value = ((sp->cls2.rcvDataSizeMsb << 8) |
108 				     sp->cls2.rcvDataSizeLsb);
109 			if (!ssp_value)
110 				goto bad_service_param;
111 			if (ssp_value > hsp_value) {
112 				sp->cls2.rcvDataSizeLsb =
113 					hsp->cls2.rcvDataSizeLsb;
114 				sp->cls2.rcvDataSizeMsb =
115 					hsp->cls2.rcvDataSizeMsb;
116 			}
117 		}
118 	} else if (class == CLASS2)
119 		goto bad_service_param;
120 	if (sp->cls3.classValid) {
121 		if (!flogi) {
122 			hsp_value = ((hsp->cls3.rcvDataSizeMsb << 8) |
123 				     hsp->cls3.rcvDataSizeLsb);
124 			ssp_value = ((sp->cls3.rcvDataSizeMsb << 8) |
125 				     sp->cls3.rcvDataSizeLsb);
126 			if (!ssp_value)
127 				goto bad_service_param;
128 			if (ssp_value > hsp_value) {
129 				sp->cls3.rcvDataSizeLsb =
130 					hsp->cls3.rcvDataSizeLsb;
131 				sp->cls3.rcvDataSizeMsb =
132 					hsp->cls3.rcvDataSizeMsb;
133 			}
134 		}
135 	} else if (class == CLASS3)
136 		goto bad_service_param;
137 
138 	/*
139 	 * Preserve the upper four bits of the MSB from the PLOGI response.
140 	 * These bits contain the Buffer-to-Buffer State Change Number
141 	 * from the target and need to be passed to the FW.
142 	 */
143 	hsp_value = (hsp->cmn.bbRcvSizeMsb << 8) | hsp->cmn.bbRcvSizeLsb;
144 	ssp_value = (sp->cmn.bbRcvSizeMsb << 8) | sp->cmn.bbRcvSizeLsb;
145 	if (ssp_value > hsp_value) {
146 		sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
147 		sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
148 				       (hsp->cmn.bbRcvSizeMsb & 0x0F);
149 	}
150 
151 	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
152 	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
153 	return 1;
154 bad_service_param:
155 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
156 			 "0207 Device %x "
157 			 "(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x) sent "
158 			 "invalid service parameters.  Ignoring device.\n",
159 			 ndlp->nlp_DID,
160 			 sp->nodeName.u.wwn[0], sp->nodeName.u.wwn[1],
161 			 sp->nodeName.u.wwn[2], sp->nodeName.u.wwn[3],
162 			 sp->nodeName.u.wwn[4], sp->nodeName.u.wwn[5],
163 			 sp->nodeName.u.wwn[6], sp->nodeName.u.wwn[7]);
164 	return 0;
165 }
166 
167 static void *
lpfc_check_elscmpl_iocb(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)168 lpfc_check_elscmpl_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
169 			struct lpfc_iocbq *rspiocb)
170 {
171 	struct lpfc_dmabuf *pcmd, *prsp;
172 	uint32_t *lp;
173 	void     *ptr = NULL;
174 	IOCB_t   *irsp;
175 
176 	irsp = &rspiocb->iocb;
177 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
178 
179 	/* For lpfc_els_abort, context2 could be zero'ed to delay
180 	 * freeing associated memory till after ABTS completes.
181 	 */
182 	if (pcmd) {
183 		prsp =  list_get_first(&pcmd->list, struct lpfc_dmabuf,
184 				       list);
185 		if (prsp) {
186 			lp = (uint32_t *) prsp->virt;
187 			ptr = (void *)((uint8_t *)lp + sizeof(uint32_t));
188 		}
189 	} else {
190 		/* Force ulpStatus error since we are returning NULL ptr */
191 		if (!(irsp->ulpStatus)) {
192 			irsp->ulpStatus = IOSTAT_LOCAL_REJECT;
193 			irsp->un.ulpWord[4] = IOERR_SLI_ABORTED;
194 		}
195 		ptr = NULL;
196 	}
197 	return ptr;
198 }
199 
200 
201 
202 /*
203  * Free resources / clean up outstanding I/Os
204  * associated with a LPFC_NODELIST entry. This
205  * routine effectively results in a "software abort".
206  */
207 void
lpfc_els_abort(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp)208 lpfc_els_abort(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
209 {
210 	LIST_HEAD(abort_list);
211 	struct lpfc_sli_ring *pring;
212 	struct lpfc_iocbq *iocb, *next_iocb;
213 
214 	pring = lpfc_phba_elsring(phba);
215 
216 	/* In case of error recovery path, we might have a NULL pring here */
217 	if (unlikely(!pring))
218 		return;
219 
220 	/* Abort outstanding I/O on NPort <nlp_DID> */
221 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_DISCOVERY,
222 			 "2819 Abort outstanding I/O on NPort x%x "
223 			 "Data: x%x x%x x%x\n",
224 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
225 			 ndlp->nlp_rpi);
226 	/* Clean up all fabric IOs first.*/
227 	lpfc_fabric_abort_nport(ndlp);
228 
229 	/*
230 	 * Lock the ELS ring txcmplq for SLI3/SLI4 and build a local list
231 	 * of all ELS IOs that need an ABTS.  The IOs need to stay on the
232 	 * txcmplq so that the abort operation completes them successfully.
233 	 */
234 	spin_lock_irq(&phba->hbalock);
235 	if (phba->sli_rev == LPFC_SLI_REV4)
236 		spin_lock(&pring->ring_lock);
237 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
238 	/* Add to abort_list on on NDLP match. */
239 		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
240 			list_add_tail(&iocb->dlist, &abort_list);
241 	}
242 	if (phba->sli_rev == LPFC_SLI_REV4)
243 		spin_unlock(&pring->ring_lock);
244 	spin_unlock_irq(&phba->hbalock);
245 
246 	/* Abort the targeted IOs and remove them from the abort list. */
247 	list_for_each_entry_safe(iocb, next_iocb, &abort_list, dlist) {
248 			spin_lock_irq(&phba->hbalock);
249 			list_del_init(&iocb->dlist);
250 			lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
251 			spin_unlock_irq(&phba->hbalock);
252 	}
253 	/* Make sure HBA is alive */
254 	lpfc_issue_hb_tmo(phba);
255 
256 	INIT_LIST_HEAD(&abort_list);
257 
258 	/* Now process the txq */
259 	spin_lock_irq(&phba->hbalock);
260 	if (phba->sli_rev == LPFC_SLI_REV4)
261 		spin_lock(&pring->ring_lock);
262 
263 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
264 		/* Check to see if iocb matches the nport we are looking for */
265 		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
266 			list_del_init(&iocb->list);
267 			list_add_tail(&iocb->list, &abort_list);
268 		}
269 	}
270 
271 	if (phba->sli_rev == LPFC_SLI_REV4)
272 		spin_unlock(&pring->ring_lock);
273 	spin_unlock_irq(&phba->hbalock);
274 
275 	/* Cancel all the IOCBs from the completions list */
276 	lpfc_sli_cancel_iocbs(phba, &abort_list,
277 			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
278 
279 	lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
280 }
281 
282 /* lpfc_defer_plogi_acc - Issue PLOGI ACC after reg_login completes
283  * @phba: pointer to lpfc hba data structure.
284  * @login_mbox: pointer to REG_RPI mailbox object
285  *
286  * The ACC for a rcv'ed PLOGI is deferred until AFTER the REG_RPI completes
287  */
288 static void
lpfc_defer_plogi_acc(struct lpfc_hba * phba,LPFC_MBOXQ_t * login_mbox)289 lpfc_defer_plogi_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *login_mbox)
290 {
291 	struct lpfc_iocbq *save_iocb;
292 	struct lpfc_nodelist *ndlp;
293 	MAILBOX_t *mb = &login_mbox->u.mb;
294 
295 	int rc;
296 
297 	ndlp = login_mbox->ctx_ndlp;
298 	save_iocb = login_mbox->context3;
299 
300 	if (mb->mbxStatus == MBX_SUCCESS) {
301 		/* Now that REG_RPI completed successfully,
302 		 * we can now proceed with sending the PLOGI ACC.
303 		 */
304 		rc = lpfc_els_rsp_acc(login_mbox->vport, ELS_CMD_PLOGI,
305 				      save_iocb, ndlp, NULL);
306 		if (rc) {
307 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
308 					"4576 PLOGI ACC fails pt2pt discovery: "
309 					"DID %x Data: %x\n", ndlp->nlp_DID, rc);
310 		}
311 	}
312 
313 	/* Now process the REG_RPI cmpl */
314 	lpfc_mbx_cmpl_reg_login(phba, login_mbox);
315 	ndlp->nlp_flag &= ~NLP_ACC_REGLOGIN;
316 	kfree(save_iocb);
317 }
318 
319 static int
lpfc_rcv_plogi(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct lpfc_iocbq * cmdiocb)320 lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
321 	       struct lpfc_iocbq *cmdiocb)
322 {
323 	struct lpfc_hba    *phba = vport->phba;
324 	struct lpfc_dmabuf *pcmd;
325 	struct lpfc_dmabuf *mp;
326 	uint64_t nlp_portwwn = 0;
327 	uint32_t *lp;
328 	IOCB_t *icmd;
329 	struct serv_parm *sp;
330 	uint32_t ed_tov;
331 	LPFC_MBOXQ_t *link_mbox;
332 	LPFC_MBOXQ_t *login_mbox;
333 	struct lpfc_iocbq *save_iocb;
334 	struct ls_rjt stat;
335 	uint32_t vid, flag;
336 	int rc;
337 
338 	memset(&stat, 0, sizeof (struct ls_rjt));
339 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
340 	lp = (uint32_t *) pcmd->virt;
341 	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
342 	if (wwn_to_u64(sp->portName.u.wwn) == 0) {
343 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
344 				 "0140 PLOGI Reject: invalid pname\n");
345 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
346 		stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_PNAME;
347 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
348 			NULL);
349 		return 0;
350 	}
351 	if (wwn_to_u64(sp->nodeName.u.wwn) == 0) {
352 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
353 				 "0141 PLOGI Reject: invalid nname\n");
354 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
355 		stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_NNAME;
356 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
357 			NULL);
358 		return 0;
359 	}
360 
361 	nlp_portwwn = wwn_to_u64(ndlp->nlp_portname.u.wwn);
362 	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0) == 0)) {
363 		/* Reject this request because invalid parameters */
364 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
365 		stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
366 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
367 			NULL);
368 		return 0;
369 	}
370 	icmd = &cmdiocb->iocb;
371 
372 	/* PLOGI chkparm OK */
373 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
374 			 "0114 PLOGI chkparm OK Data: x%x x%x x%x "
375 			 "x%x x%x x%x\n",
376 			 ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
377 			 ndlp->nlp_rpi, vport->port_state,
378 			 vport->fc_flag);
379 
380 	if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
381 		ndlp->nlp_fcp_info |= CLASS2;
382 	else
383 		ndlp->nlp_fcp_info |= CLASS3;
384 
385 	ndlp->nlp_class_sup = 0;
386 	if (sp->cls1.classValid)
387 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
388 	if (sp->cls2.classValid)
389 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
390 	if (sp->cls3.classValid)
391 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
392 	if (sp->cls4.classValid)
393 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
394 	ndlp->nlp_maxframe =
395 		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
396 	/* if already logged in, do implicit logout */
397 	switch (ndlp->nlp_state) {
398 	case  NLP_STE_NPR_NODE:
399 		if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
400 			break;
401 		fallthrough;
402 	case  NLP_STE_REG_LOGIN_ISSUE:
403 	case  NLP_STE_PRLI_ISSUE:
404 	case  NLP_STE_UNMAPPED_NODE:
405 	case  NLP_STE_MAPPED_NODE:
406 		/* For initiators, lpfc_plogi_confirm_nport skips fabric did.
407 		 * For target mode, execute implicit logo.
408 		 * Fabric nodes go into NPR.
409 		 */
410 		if (!(ndlp->nlp_type & NLP_FABRIC) &&
411 		    !(phba->nvmet_support)) {
412 			/* Clear ndlp info, since follow up PRLI may have
413 			 * updated ndlp information
414 			 */
415 			ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
416 			ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
417 			ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
418 			ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;
419 			ndlp->nlp_flag &= ~NLP_FIRSTBURST;
420 
421 			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
422 					 ndlp, NULL);
423 			return 1;
424 		}
425 		if (nlp_portwwn != 0 &&
426 		    nlp_portwwn != wwn_to_u64(sp->portName.u.wwn))
427 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
428 					 "0143 PLOGI recv'd from DID: x%x "
429 					 "WWPN changed: old %llx new %llx\n",
430 					 ndlp->nlp_DID,
431 					 (unsigned long long)nlp_portwwn,
432 					 (unsigned long long)
433 					 wwn_to_u64(sp->portName.u.wwn));
434 
435 		/* Notify transport of connectivity loss to trigger cleanup. */
436 		if (phba->nvmet_support &&
437 		    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
438 			lpfc_nvmet_invalidate_host(phba, ndlp);
439 
440 		ndlp->nlp_prev_state = ndlp->nlp_state;
441 		/* rport needs to be unregistered first */
442 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
443 		break;
444 	}
445 
446 	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
447 	ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
448 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
449 	ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;
450 	ndlp->nlp_flag &= ~NLP_FIRSTBURST;
451 
452 	login_mbox = NULL;
453 	link_mbox = NULL;
454 	save_iocb = NULL;
455 
456 	/* Check for Nport to NPort pt2pt protocol */
457 	if ((vport->fc_flag & FC_PT2PT) &&
458 	    !(vport->fc_flag & FC_PT2PT_PLOGI)) {
459 		/* rcv'ed PLOGI decides what our NPortId will be */
460 		vport->fc_myDID = icmd->un.rcvels.parmRo;
461 
462 		/* If there is an outstanding FLOGI, abort it now.
463 		 * The remote NPort is not going to ACC our FLOGI
464 		 * if its already issuing a PLOGI for pt2pt mode.
465 		 * This indicates our FLOGI was dropped; however, we
466 		 * must have ACCed the remote NPorts FLOGI to us
467 		 * to make it here.
468 		 */
469 		if (phba->hba_flag & HBA_FLOGI_OUTSTANDING)
470 			lpfc_els_abort_flogi(phba);
471 
472 		ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
473 		if (sp->cmn.edtovResolution) {
474 			/* E_D_TOV ticks are in nanoseconds */
475 			ed_tov = (phba->fc_edtov + 999999) / 1000000;
476 		}
477 
478 		/*
479 		 * For pt-to-pt, use the larger EDTOV
480 		 * RATOV = 2 * EDTOV
481 		 */
482 		if (ed_tov > phba->fc_edtov)
483 			phba->fc_edtov = ed_tov;
484 		phba->fc_ratov = (2 * phba->fc_edtov) / 1000;
485 
486 		memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
487 
488 		/* Issue CONFIG_LINK for SLI3 or REG_VFI for SLI4,
489 		 * to account for updated TOV's / parameters
490 		 */
491 		if (phba->sli_rev == LPFC_SLI_REV4)
492 			lpfc_issue_reg_vfi(vport);
493 		else {
494 			link_mbox = mempool_alloc(phba->mbox_mem_pool,
495 						  GFP_KERNEL);
496 			if (!link_mbox)
497 				goto out;
498 			lpfc_config_link(phba, link_mbox);
499 			link_mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
500 			link_mbox->vport = vport;
501 			link_mbox->ctx_ndlp = ndlp;
502 
503 			rc = lpfc_sli_issue_mbox(phba, link_mbox, MBX_NOWAIT);
504 			if (rc == MBX_NOT_FINISHED) {
505 				mempool_free(link_mbox, phba->mbox_mem_pool);
506 				goto out;
507 			}
508 		}
509 
510 		lpfc_can_disctmo(vport);
511 	}
512 
513 	ndlp->nlp_flag &= ~NLP_SUPPRESS_RSP;
514 	if ((phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) &&
515 	    sp->cmn.valid_vendor_ver_level) {
516 		vid = be32_to_cpu(sp->un.vv.vid);
517 		flag = be32_to_cpu(sp->un.vv.flags);
518 		if ((vid == LPFC_VV_EMLX_ID) && (flag & LPFC_VV_SUPPRESS_RSP))
519 			ndlp->nlp_flag |= NLP_SUPPRESS_RSP;
520 	}
521 
522 	login_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
523 	if (!login_mbox)
524 		goto out;
525 
526 	save_iocb = kzalloc(sizeof(*save_iocb), GFP_KERNEL);
527 	if (!save_iocb)
528 		goto out;
529 
530 	/* Save info from cmd IOCB to be used in rsp after all mbox completes */
531 	memcpy((uint8_t *)save_iocb, (uint8_t *)cmdiocb,
532 	       sizeof(struct lpfc_iocbq));
533 
534 	/* Registering an existing RPI behaves differently for SLI3 vs SLI4 */
535 	if (phba->sli_rev == LPFC_SLI_REV4)
536 		lpfc_unreg_rpi(vport, ndlp);
537 
538 	/* Issue REG_LOGIN first, before ACCing the PLOGI, thus we will
539 	 * always be deferring the ACC.
540 	 */
541 	rc = lpfc_reg_rpi(phba, vport->vpi, icmd->un.rcvels.remoteID,
542 			    (uint8_t *)sp, login_mbox, ndlp->nlp_rpi);
543 	if (rc)
544 		goto out;
545 
546 	login_mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
547 	login_mbox->vport = vport;
548 
549 	/*
550 	 * If there is an outstanding PLOGI issued, abort it before
551 	 * sending ACC rsp for received PLOGI. If pending plogi
552 	 * is not canceled here, the plogi will be rejected by
553 	 * remote port and will be retried. On a configuration with
554 	 * single discovery thread, this will cause a huge delay in
555 	 * discovery. Also this will cause multiple state machines
556 	 * running in parallel for this node.
557 	 * This only applies to a fabric environment.
558 	 */
559 	if ((ndlp->nlp_state == NLP_STE_PLOGI_ISSUE) &&
560 	    (vport->fc_flag & FC_FABRIC)) {
561 		/* software abort outstanding PLOGI */
562 		lpfc_els_abort(phba, ndlp);
563 	}
564 
565 	if ((vport->port_type == LPFC_NPIV_PORT &&
566 	     vport->cfg_restrict_login)) {
567 
568 		/* no deferred ACC */
569 		kfree(save_iocb);
570 
571 		/* This is an NPIV SLI4 instance that does not need to register
572 		 * a default RPI.
573 		 */
574 		if (phba->sli_rev == LPFC_SLI_REV4) {
575 			mp = (struct lpfc_dmabuf *)login_mbox->ctx_buf;
576 			if (mp) {
577 				lpfc_mbuf_free(phba, mp->virt, mp->phys);
578 				kfree(mp);
579 			}
580 			mempool_free(login_mbox, phba->mbox_mem_pool);
581 			login_mbox = NULL;
582 		} else {
583 			/* In order to preserve RPIs, we want to cleanup
584 			 * the default RPI the firmware created to rcv
585 			 * this ELS request. The only way to do this is
586 			 * to register, then unregister the RPI.
587 			 */
588 			spin_lock_irq(&ndlp->lock);
589 			ndlp->nlp_flag |= (NLP_RM_DFLT_RPI | NLP_ACC_REGLOGIN |
590 					   NLP_RCV_PLOGI);
591 			spin_unlock_irq(&ndlp->lock);
592 		}
593 
594 		stat.un.b.lsRjtRsnCode = LSRJT_INVALID_CMD;
595 		stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
596 		rc = lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
597 			ndlp, login_mbox);
598 		if (rc)
599 			mempool_free(login_mbox, phba->mbox_mem_pool);
600 		return 1;
601 	}
602 
603 	/* So the order here should be:
604 	 * SLI3 pt2pt
605 	 *   Issue CONFIG_LINK mbox
606 	 *   CONFIG_LINK cmpl
607 	 * SLI4 pt2pt
608 	 *   Issue REG_VFI mbox
609 	 *   REG_VFI cmpl
610 	 * SLI4
611 	 *   Issue UNREG RPI mbx
612 	 *   UNREG RPI cmpl
613 	 * Issue REG_RPI mbox
614 	 * REG RPI cmpl
615 	 * Issue PLOGI ACC
616 	 * PLOGI ACC cmpl
617 	 */
618 	login_mbox->mbox_cmpl = lpfc_defer_plogi_acc;
619 	login_mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
620 	login_mbox->context3 = save_iocb; /* For PLOGI ACC */
621 
622 	spin_lock_irq(&ndlp->lock);
623 	ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
624 	spin_unlock_irq(&ndlp->lock);
625 
626 	/* Start the ball rolling by issuing REG_LOGIN here */
627 	rc = lpfc_sli_issue_mbox(phba, login_mbox, MBX_NOWAIT);
628 	if (rc == MBX_NOT_FINISHED)
629 		goto out;
630 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
631 
632 	return 1;
633 out:
634 	kfree(save_iocb);
635 	if (login_mbox)
636 		mempool_free(login_mbox, phba->mbox_mem_pool);
637 
638 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
639 	stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
640 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
641 	return 0;
642 }
643 
644 /**
645  * lpfc_mbx_cmpl_resume_rpi - Resume RPI completion routine
646  * @phba: pointer to lpfc hba data structure.
647  * @mboxq: pointer to mailbox object
648  *
649  * This routine is invoked to issue a completion to a rcv'ed
650  * ADISC or PDISC after the paused RPI has been resumed.
651  **/
652 static void
lpfc_mbx_cmpl_resume_rpi(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)653 lpfc_mbx_cmpl_resume_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
654 {
655 	struct lpfc_vport *vport;
656 	struct lpfc_iocbq *elsiocb;
657 	struct lpfc_nodelist *ndlp;
658 	uint32_t cmd;
659 
660 	elsiocb = (struct lpfc_iocbq *)mboxq->ctx_buf;
661 	ndlp = (struct lpfc_nodelist *)mboxq->ctx_ndlp;
662 	vport = mboxq->vport;
663 	cmd = elsiocb->drvrTimeout;
664 
665 	if (cmd == ELS_CMD_ADISC) {
666 		lpfc_els_rsp_adisc_acc(vport, elsiocb, ndlp);
667 	} else {
668 		lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, elsiocb,
669 			ndlp, NULL);
670 	}
671 
672 	/* This nlp_put pairs with lpfc_sli4_resume_rpi */
673 	lpfc_nlp_put(ndlp);
674 
675 	kfree(elsiocb);
676 	mempool_free(mboxq, phba->mbox_mem_pool);
677 }
678 
679 static int
lpfc_rcv_padisc(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct lpfc_iocbq * cmdiocb)680 lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
681 		struct lpfc_iocbq *cmdiocb)
682 {
683 	struct lpfc_iocbq  *elsiocb;
684 	struct lpfc_dmabuf *pcmd;
685 	struct serv_parm   *sp;
686 	struct lpfc_name   *pnn, *ppn;
687 	struct ls_rjt stat;
688 	ADISC *ap;
689 	IOCB_t *icmd;
690 	uint32_t *lp;
691 	uint32_t cmd;
692 
693 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
694 	lp = (uint32_t *) pcmd->virt;
695 
696 	cmd = *lp++;
697 	if (cmd == ELS_CMD_ADISC) {
698 		ap = (ADISC *) lp;
699 		pnn = (struct lpfc_name *) & ap->nodeName;
700 		ppn = (struct lpfc_name *) & ap->portName;
701 	} else {
702 		sp = (struct serv_parm *) lp;
703 		pnn = (struct lpfc_name *) & sp->nodeName;
704 		ppn = (struct lpfc_name *) & sp->portName;
705 	}
706 
707 	icmd = &cmdiocb->iocb;
708 	if (icmd->ulpStatus == 0 && lpfc_check_adisc(vport, ndlp, pnn, ppn)) {
709 
710 		/*
711 		 * As soon as  we send ACC, the remote NPort can
712 		 * start sending us data. Thus, for SLI4 we must
713 		 * resume the RPI before the ACC goes out.
714 		 */
715 		if (vport->phba->sli_rev == LPFC_SLI_REV4) {
716 			elsiocb = kmalloc(sizeof(struct lpfc_iocbq),
717 				GFP_KERNEL);
718 			if (elsiocb) {
719 
720 				/* Save info from cmd IOCB used in rsp */
721 				memcpy((uint8_t *)elsiocb, (uint8_t *)cmdiocb,
722 					sizeof(struct lpfc_iocbq));
723 
724 				/* Save the ELS cmd */
725 				elsiocb->drvrTimeout = cmd;
726 
727 				lpfc_sli4_resume_rpi(ndlp,
728 					lpfc_mbx_cmpl_resume_rpi, elsiocb);
729 				goto out;
730 			}
731 		}
732 
733 		if (cmd == ELS_CMD_ADISC) {
734 			lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
735 		} else {
736 			lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
737 				ndlp, NULL);
738 		}
739 out:
740 		/* If we are authenticated, move to the proper state.
741 		 * It is possible an ADISC arrived and the remote nport
742 		 * is already in MAPPED or UNMAPPED state.  Catch this
743 		 * condition and don't set the nlp_state again because
744 		 * it causes an unnecessary transport unregister/register.
745 		 *
746 		 * Nodes marked for ADISC will move MAPPED or UNMAPPED state
747 		 * after issuing ADISC
748 		 */
749 		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET)) {
750 			if ((ndlp->nlp_state != NLP_STE_MAPPED_NODE) &&
751 			    !(ndlp->nlp_flag & NLP_NPR_ADISC))
752 				lpfc_nlp_set_state(vport, ndlp,
753 						   NLP_STE_MAPPED_NODE);
754 		}
755 
756 		return 1;
757 	}
758 	/* Reject this request because invalid parameters */
759 	stat.un.b.lsRjtRsvd0 = 0;
760 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
761 	stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
762 	stat.un.b.vendorUnique = 0;
763 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
764 
765 	/* 1 sec timeout */
766 	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
767 
768 	spin_lock_irq(&ndlp->lock);
769 	ndlp->nlp_flag |= NLP_DELAY_TMO;
770 	spin_unlock_irq(&ndlp->lock);
771 	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
772 	ndlp->nlp_prev_state = ndlp->nlp_state;
773 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
774 	return 0;
775 }
776 
777 static int
lpfc_rcv_logo(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct lpfc_iocbq * cmdiocb,uint32_t els_cmd)778 lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
779 	      struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
780 {
781 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
782 	struct lpfc_hba    *phba = vport->phba;
783 	struct lpfc_vport **vports;
784 	int i, active_vlink_present = 0 ;
785 
786 	/* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
787 	/* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
788 	 * PLOGIs during LOGO storms from a device.
789 	 */
790 	spin_lock_irq(&ndlp->lock);
791 	ndlp->nlp_flag |= NLP_LOGO_ACC;
792 	spin_unlock_irq(&ndlp->lock);
793 	if (els_cmd == ELS_CMD_PRLO)
794 		lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
795 	else
796 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
797 
798 	/* This clause allows the initiator to ACC the LOGO back to the
799 	 * Fabric Domain Controller.  It does deliberately skip all other
800 	 * steps because some fabrics send RDP requests after logging out
801 	 * from the initiator.
802 	 */
803 	if (ndlp->nlp_type & NLP_FABRIC &&
804 	    ((ndlp->nlp_DID & WELL_KNOWN_DID_MASK) != WELL_KNOWN_DID_MASK))
805 		return 0;
806 
807 	/* Notify transport of connectivity loss to trigger cleanup. */
808 	if (phba->nvmet_support &&
809 	    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
810 		lpfc_nvmet_invalidate_host(phba, ndlp);
811 
812 	if (ndlp->nlp_DID == Fabric_DID) {
813 		if (vport->port_state <= LPFC_FDISC ||
814 		    vport->fc_flag & FC_PT2PT)
815 			goto out;
816 		lpfc_linkdown_port(vport);
817 		spin_lock_irq(shost->host_lock);
818 		vport->fc_flag |= FC_VPORT_LOGO_RCVD;
819 		spin_unlock_irq(shost->host_lock);
820 		vports = lpfc_create_vport_work_array(phba);
821 		if (vports) {
822 			for (i = 0; i <= phba->max_vports && vports[i] != NULL;
823 					i++) {
824 				if ((!(vports[i]->fc_flag &
825 					FC_VPORT_LOGO_RCVD)) &&
826 					(vports[i]->port_state > LPFC_FDISC)) {
827 					active_vlink_present = 1;
828 					break;
829 				}
830 			}
831 			lpfc_destroy_vport_work_array(phba, vports);
832 		}
833 
834 		/*
835 		 * Don't re-instantiate if vport is marked for deletion.
836 		 * If we are here first then vport_delete is going to wait
837 		 * for discovery to complete.
838 		 */
839 		if (!(vport->load_flag & FC_UNLOADING) &&
840 					active_vlink_present) {
841 			/*
842 			 * If there are other active VLinks present,
843 			 * re-instantiate the Vlink using FDISC.
844 			 */
845 			mod_timer(&ndlp->nlp_delayfunc,
846 				  jiffies + msecs_to_jiffies(1000));
847 			spin_lock_irq(&ndlp->lock);
848 			ndlp->nlp_flag |= NLP_DELAY_TMO;
849 			spin_unlock_irq(&ndlp->lock);
850 			ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
851 			vport->port_state = LPFC_FDISC;
852 		} else {
853 			spin_lock_irq(shost->host_lock);
854 			phba->pport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG;
855 			spin_unlock_irq(shost->host_lock);
856 			lpfc_retry_pport_discovery(phba);
857 		}
858 	} else if ((!(ndlp->nlp_type & NLP_FABRIC) &&
859 		((ndlp->nlp_type & NLP_FCP_TARGET) ||
860 		(ndlp->nlp_type & NLP_NVME_TARGET) ||
861 		(vport->fc_flag & FC_PT2PT))) ||
862 		(ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
863 		/* Only try to re-login if this is NOT a Fabric Node
864 		 * AND the remote NPORT is a FCP/NVME Target or we
865 		 * are in pt2pt mode. NLP_STE_ADISC_ISSUE is a special
866 		 * case for LOGO as a response to ADISC behavior.
867 		 */
868 		mod_timer(&ndlp->nlp_delayfunc,
869 			  jiffies + msecs_to_jiffies(1000 * 1));
870 		spin_lock_irq(&ndlp->lock);
871 		ndlp->nlp_flag |= NLP_DELAY_TMO;
872 		spin_unlock_irq(&ndlp->lock);
873 
874 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
875 	}
876 out:
877 	/* Unregister from backend, could have been skipped due to ADISC */
878 	lpfc_nlp_unreg_node(vport, ndlp);
879 
880 	ndlp->nlp_prev_state = ndlp->nlp_state;
881 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
882 
883 	spin_lock_irq(&ndlp->lock);
884 	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
885 	spin_unlock_irq(&ndlp->lock);
886 	/* The driver has to wait until the ACC completes before it continues
887 	 * processing the LOGO.  The action will resume in
888 	 * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
889 	 * unreg_login, the driver waits so the ACC does not get aborted.
890 	 */
891 	return 0;
892 }
893 
894 static uint32_t
lpfc_rcv_prli_support_check(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct lpfc_iocbq * cmdiocb)895 lpfc_rcv_prli_support_check(struct lpfc_vport *vport,
896 			    struct lpfc_nodelist *ndlp,
897 			    struct lpfc_iocbq *cmdiocb)
898 {
899 	struct ls_rjt stat;
900 	uint32_t *payload;
901 	uint32_t cmd;
902 
903 	payload = ((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
904 	cmd = *payload;
905 	if (vport->phba->nvmet_support) {
906 		/* Must be a NVME PRLI */
907 		if (cmd ==  ELS_CMD_PRLI)
908 			goto out;
909 	} else {
910 		/* Initiator mode. */
911 		if (!vport->nvmei_support && (cmd == ELS_CMD_NVMEPRLI))
912 			goto out;
913 	}
914 	return 1;
915 out:
916 	lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME_DISC,
917 			 "6115 Rcv PRLI (%x) check failed: ndlp rpi %d "
918 			 "state x%x flags x%x\n",
919 			 cmd, ndlp->nlp_rpi, ndlp->nlp_state,
920 			 ndlp->nlp_flag);
921 	memset(&stat, 0, sizeof(struct ls_rjt));
922 	stat.un.b.lsRjtRsnCode = LSRJT_CMD_UNSUPPORTED;
923 	stat.un.b.lsRjtRsnCodeExp = LSEXP_REQ_UNSUPPORTED;
924 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
925 			    ndlp, NULL);
926 	return 0;
927 }
928 
929 static void
lpfc_rcv_prli(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct lpfc_iocbq * cmdiocb)930 lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
931 	      struct lpfc_iocbq *cmdiocb)
932 {
933 	struct lpfc_hba  *phba = vport->phba;
934 	struct lpfc_dmabuf *pcmd;
935 	uint32_t *lp;
936 	PRLI *npr;
937 	struct fc_rport *rport = ndlp->rport;
938 	u32 roles;
939 
940 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
941 	lp = (uint32_t *) pcmd->virt;
942 	npr = (PRLI *) ((uint8_t *) lp + sizeof (uint32_t));
943 
944 	if ((npr->prliType == PRLI_FCP_TYPE) ||
945 	    (npr->prliType == PRLI_NVME_TYPE)) {
946 		if (npr->initiatorFunc) {
947 			if (npr->prliType == PRLI_FCP_TYPE)
948 				ndlp->nlp_type |= NLP_FCP_INITIATOR;
949 			if (npr->prliType == PRLI_NVME_TYPE)
950 				ndlp->nlp_type |= NLP_NVME_INITIATOR;
951 		}
952 		if (npr->targetFunc) {
953 			if (npr->prliType == PRLI_FCP_TYPE)
954 				ndlp->nlp_type |= NLP_FCP_TARGET;
955 			if (npr->prliType == PRLI_NVME_TYPE)
956 				ndlp->nlp_type |= NLP_NVME_TARGET;
957 			if (npr->writeXferRdyDis)
958 				ndlp->nlp_flag |= NLP_FIRSTBURST;
959 		}
960 		if (npr->Retry && ndlp->nlp_type &
961 					(NLP_FCP_INITIATOR | NLP_FCP_TARGET))
962 			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
963 
964 		if (npr->Retry && phba->nsler &&
965 		    ndlp->nlp_type & (NLP_NVME_INITIATOR | NLP_NVME_TARGET))
966 			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
967 
968 
969 		/* If this driver is in nvme target mode, set the ndlp's fc4
970 		 * type to NVME provided the PRLI response claims NVME FC4
971 		 * type.  Target mode does not issue gft_id so doesn't get
972 		 * the fc4 type set until now.
973 		 */
974 		if (phba->nvmet_support && (npr->prliType == PRLI_NVME_TYPE)) {
975 			ndlp->nlp_fc4_type |= NLP_FC4_NVME;
976 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
977 		}
978 
979 		/* Fabric Controllers send FCP PRLI as an initiator but should
980 		 * not get recognized as FCP type and registered with transport.
981 		 */
982 		if (npr->prliType == PRLI_FCP_TYPE &&
983 		    !(ndlp->nlp_type & NLP_FABRIC))
984 			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
985 	}
986 	if (rport) {
987 		/* We need to update the rport role values */
988 		roles = FC_RPORT_ROLE_UNKNOWN;
989 		if (ndlp->nlp_type & NLP_FCP_INITIATOR)
990 			roles |= FC_RPORT_ROLE_FCP_INITIATOR;
991 		if (ndlp->nlp_type & NLP_FCP_TARGET)
992 			roles |= FC_RPORT_ROLE_FCP_TARGET;
993 
994 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
995 			"rport rolechg:   role:x%x did:x%x flg:x%x",
996 			roles, ndlp->nlp_DID, ndlp->nlp_flag);
997 
998 		if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
999 			fc_remote_port_rolechg(rport, roles);
1000 	}
1001 }
1002 
1003 static uint32_t
lpfc_disc_set_adisc(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)1004 lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1005 {
1006 	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
1007 		spin_lock_irq(&ndlp->lock);
1008 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1009 		spin_unlock_irq(&ndlp->lock);
1010 		return 0;
1011 	}
1012 
1013 	if (!(vport->fc_flag & FC_PT2PT)) {
1014 		/* Check config parameter use-adisc or FCP-2 */
1015 		if (vport->cfg_use_adisc && ((vport->fc_flag & FC_RSCN_MODE) ||
1016 		    ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
1017 		     (ndlp->nlp_type & NLP_FCP_TARGET)))) {
1018 			spin_lock_irq(&ndlp->lock);
1019 			ndlp->nlp_flag |= NLP_NPR_ADISC;
1020 			spin_unlock_irq(&ndlp->lock);
1021 			return 1;
1022 		}
1023 	}
1024 
1025 	spin_lock_irq(&ndlp->lock);
1026 	ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1027 	spin_unlock_irq(&ndlp->lock);
1028 	lpfc_unreg_rpi(vport, ndlp);
1029 	return 0;
1030 }
1031 
1032 /**
1033  * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
1034  * @phba : Pointer to lpfc_hba structure.
1035  * @vport: Pointer to lpfc_vport structure.
1036  * @ndlp: Pointer to lpfc_nodelist structure.
1037  * @rpi  : rpi to be release.
1038  *
1039  * This function will send a unreg_login mailbox command to the firmware
1040  * to release a rpi.
1041  **/
1042 static void
lpfc_release_rpi(struct lpfc_hba * phba,struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint16_t rpi)1043 lpfc_release_rpi(struct lpfc_hba *phba, struct lpfc_vport *vport,
1044 		 struct lpfc_nodelist *ndlp, uint16_t rpi)
1045 {
1046 	LPFC_MBOXQ_t *pmb;
1047 	int rc;
1048 
1049 	/* If there is already an UNREG in progress for this ndlp,
1050 	 * no need to queue up another one.
1051 	 */
1052 	if (ndlp->nlp_flag & NLP_UNREG_INP) {
1053 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1054 				 "1435 release_rpi SKIP UNREG x%x on "
1055 				 "NPort x%x deferred x%x  flg x%x "
1056 				 "Data: x%px\n",
1057 				 ndlp->nlp_rpi, ndlp->nlp_DID,
1058 				 ndlp->nlp_defer_did,
1059 				 ndlp->nlp_flag, ndlp);
1060 		return;
1061 	}
1062 
1063 	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
1064 			GFP_KERNEL);
1065 	if (!pmb)
1066 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1067 				 "2796 mailbox memory allocation failed \n");
1068 	else {
1069 		lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
1070 		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1071 		pmb->vport = vport;
1072 		pmb->ctx_ndlp = lpfc_nlp_get(ndlp);
1073 		if (!pmb->ctx_ndlp) {
1074 			mempool_free(pmb, phba->mbox_mem_pool);
1075 			return;
1076 		}
1077 
1078 		if (((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
1079 		    (!(vport->fc_flag & FC_OFFLINE_MODE)))
1080 			ndlp->nlp_flag |= NLP_UNREG_INP;
1081 
1082 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1083 				 "1437 release_rpi UNREG x%x "
1084 				 "on NPort x%x flg x%x\n",
1085 				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag);
1086 
1087 		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1088 		if (rc == MBX_NOT_FINISHED)
1089 			mempool_free(pmb, phba->mbox_mem_pool);
1090 	}
1091 }
1092 
1093 static uint32_t
lpfc_disc_illegal(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1094 lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1095 		  void *arg, uint32_t evt)
1096 {
1097 	struct lpfc_hba *phba;
1098 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1099 	uint16_t rpi;
1100 
1101 	phba = vport->phba;
1102 	/* Release the RPI if reglogin completing */
1103 	if (!(phba->pport->load_flag & FC_UNLOADING) &&
1104 		(evt == NLP_EVT_CMPL_REG_LOGIN) &&
1105 		(!pmb->u.mb.mbxStatus)) {
1106 		rpi = pmb->u.mb.un.varWords[0];
1107 		lpfc_release_rpi(phba, vport, ndlp, rpi);
1108 	}
1109 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1110 			 "0271 Illegal State Transition: node x%x "
1111 			 "event x%x, state x%x Data: x%x x%x\n",
1112 			 ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
1113 			 ndlp->nlp_flag);
1114 	return ndlp->nlp_state;
1115 }
1116 
1117 static uint32_t
lpfc_cmpl_plogi_illegal(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1118 lpfc_cmpl_plogi_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1119 		  void *arg, uint32_t evt)
1120 {
1121 	/* This transition is only legal if we previously
1122 	 * rcv'ed a PLOGI. Since we don't want 2 discovery threads
1123 	 * working on the same NPortID, do nothing for this thread
1124 	 * to stop it.
1125 	 */
1126 	if (!(ndlp->nlp_flag & NLP_RCV_PLOGI)) {
1127 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1128 				 "0272 Illegal State Transition: node x%x "
1129 				 "event x%x, state x%x Data: x%x x%x\n",
1130 				  ndlp->nlp_DID, evt, ndlp->nlp_state,
1131 				  ndlp->nlp_rpi, ndlp->nlp_flag);
1132 	}
1133 	return ndlp->nlp_state;
1134 }
1135 
1136 /* Start of Discovery State Machine routines */
1137 
1138 static uint32_t
lpfc_rcv_plogi_unused_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1139 lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1140 			   void *arg, uint32_t evt)
1141 {
1142 	struct lpfc_iocbq *cmdiocb;
1143 
1144 	cmdiocb = (struct lpfc_iocbq *) arg;
1145 
1146 	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1147 		return ndlp->nlp_state;
1148 	}
1149 	return NLP_STE_FREED_NODE;
1150 }
1151 
1152 static uint32_t
lpfc_rcv_els_unused_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1153 lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1154 			 void *arg, uint32_t evt)
1155 {
1156 	lpfc_issue_els_logo(vport, ndlp, 0);
1157 	return ndlp->nlp_state;
1158 }
1159 
1160 static uint32_t
lpfc_rcv_logo_unused_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1161 lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1162 			  void *arg, uint32_t evt)
1163 {
1164 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1165 
1166 	spin_lock_irq(&ndlp->lock);
1167 	ndlp->nlp_flag |= NLP_LOGO_ACC;
1168 	spin_unlock_irq(&ndlp->lock);
1169 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1170 
1171 	return ndlp->nlp_state;
1172 }
1173 
1174 static uint32_t
lpfc_cmpl_logo_unused_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1175 lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1176 			   void *arg, uint32_t evt)
1177 {
1178 	return NLP_STE_FREED_NODE;
1179 }
1180 
1181 static uint32_t
lpfc_device_rm_unused_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1182 lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1183 			   void *arg, uint32_t evt)
1184 {
1185 	return NLP_STE_FREED_NODE;
1186 }
1187 
1188 static uint32_t
lpfc_device_recov_unused_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1189 lpfc_device_recov_unused_node(struct lpfc_vport *vport,
1190 			struct lpfc_nodelist *ndlp,
1191 			   void *arg, uint32_t evt)
1192 {
1193 	return ndlp->nlp_state;
1194 }
1195 
1196 static uint32_t
lpfc_rcv_plogi_plogi_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1197 lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1198 			   void *arg, uint32_t evt)
1199 {
1200 	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1201 	struct lpfc_hba   *phba = vport->phba;
1202 	struct lpfc_iocbq *cmdiocb = arg;
1203 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
1204 	uint32_t *lp = (uint32_t *) pcmd->virt;
1205 	struct serv_parm *sp = (struct serv_parm *) (lp + 1);
1206 	struct ls_rjt stat;
1207 	int port_cmp;
1208 
1209 	memset(&stat, 0, sizeof (struct ls_rjt));
1210 
1211 	/* For a PLOGI, we only accept if our portname is less
1212 	 * than the remote portname.
1213 	 */
1214 	phba->fc_stat.elsLogiCol++;
1215 	port_cmp = memcmp(&vport->fc_portname, &sp->portName,
1216 			  sizeof(struct lpfc_name));
1217 
1218 	if (port_cmp >= 0) {
1219 		/* Reject this request because the remote node will accept
1220 		   ours */
1221 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1222 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
1223 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
1224 			NULL);
1225 	} else {
1226 		if (lpfc_rcv_plogi(vport, ndlp, cmdiocb) &&
1227 		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
1228 		    (vport->num_disc_nodes)) {
1229 			spin_lock_irq(&ndlp->lock);
1230 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1231 			spin_unlock_irq(&ndlp->lock);
1232 			/* Check if there are more PLOGIs to be sent */
1233 			lpfc_more_plogi(vport);
1234 			if (vport->num_disc_nodes == 0) {
1235 				spin_lock_irq(shost->host_lock);
1236 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
1237 				spin_unlock_irq(shost->host_lock);
1238 				lpfc_can_disctmo(vport);
1239 				lpfc_end_rscn(vport);
1240 			}
1241 		}
1242 	} /* If our portname was less */
1243 
1244 	return ndlp->nlp_state;
1245 }
1246 
1247 static uint32_t
lpfc_rcv_prli_plogi_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1248 lpfc_rcv_prli_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1249 			  void *arg, uint32_t evt)
1250 {
1251 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1252 	struct ls_rjt     stat;
1253 
1254 	memset(&stat, 0, sizeof (struct ls_rjt));
1255 	stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
1256 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1257 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1258 	return ndlp->nlp_state;
1259 }
1260 
1261 static uint32_t
lpfc_rcv_logo_plogi_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1262 lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1263 			  void *arg, uint32_t evt)
1264 {
1265 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1266 
1267 	/* Retrieve RPI from LOGO IOCB. RPI is used for CMD_ABORT_XRI_CN */
1268 	if (vport->phba->sli_rev == LPFC_SLI_REV3)
1269 		ndlp->nlp_rpi = cmdiocb->iocb.ulpIoTag;
1270 				/* software abort outstanding PLOGI */
1271 	lpfc_els_abort(vport->phba, ndlp);
1272 
1273 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1274 	return ndlp->nlp_state;
1275 }
1276 
1277 static uint32_t
lpfc_rcv_els_plogi_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1278 lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1279 			 void *arg, uint32_t evt)
1280 {
1281 	struct lpfc_hba   *phba = vport->phba;
1282 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1283 
1284 	/* software abort outstanding PLOGI */
1285 	lpfc_els_abort(phba, ndlp);
1286 
1287 	if (evt == NLP_EVT_RCV_LOGO) {
1288 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1289 	} else {
1290 		lpfc_issue_els_logo(vport, ndlp, 0);
1291 	}
1292 
1293 	/* Put ndlp in npr state set plogi timer for 1 sec */
1294 	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000 * 1));
1295 	spin_lock_irq(&ndlp->lock);
1296 	ndlp->nlp_flag |= NLP_DELAY_TMO;
1297 	spin_unlock_irq(&ndlp->lock);
1298 	ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1299 	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1300 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1301 
1302 	return ndlp->nlp_state;
1303 }
1304 
1305 static uint32_t
lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1306 lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
1307 			    struct lpfc_nodelist *ndlp,
1308 			    void *arg,
1309 			    uint32_t evt)
1310 {
1311 	struct lpfc_hba    *phba = vport->phba;
1312 	struct lpfc_iocbq  *cmdiocb, *rspiocb;
1313 	struct lpfc_dmabuf *pcmd, *prsp, *mp;
1314 	uint32_t *lp;
1315 	uint32_t vid, flag;
1316 	IOCB_t *irsp;
1317 	struct serv_parm *sp;
1318 	uint32_t ed_tov;
1319 	LPFC_MBOXQ_t *mbox;
1320 	int rc;
1321 
1322 	cmdiocb = (struct lpfc_iocbq *) arg;
1323 	rspiocb = cmdiocb->context_un.rsp_iocb;
1324 
1325 	if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
1326 		/* Recovery from PLOGI collision logic */
1327 		return ndlp->nlp_state;
1328 	}
1329 
1330 	irsp = &rspiocb->iocb;
1331 
1332 	if (irsp->ulpStatus)
1333 		goto out;
1334 
1335 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
1336 
1337 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1338 	if (!prsp)
1339 		goto out;
1340 
1341 	lp = (uint32_t *) prsp->virt;
1342 	sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
1343 
1344 	/* Some switches have FDMI servers returning 0 for WWN */
1345 	if ((ndlp->nlp_DID != FDMI_DID) &&
1346 		(wwn_to_u64(sp->portName.u.wwn) == 0 ||
1347 		wwn_to_u64(sp->nodeName.u.wwn) == 0)) {
1348 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1349 				 "0142 PLOGI RSP: Invalid WWN.\n");
1350 		goto out;
1351 	}
1352 	if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
1353 		goto out;
1354 	/* PLOGI chkparm OK */
1355 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1356 			 "0121 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
1357 			 ndlp->nlp_DID, ndlp->nlp_state,
1358 			 ndlp->nlp_flag, ndlp->nlp_rpi);
1359 	if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
1360 		ndlp->nlp_fcp_info |= CLASS2;
1361 	else
1362 		ndlp->nlp_fcp_info |= CLASS3;
1363 
1364 	ndlp->nlp_class_sup = 0;
1365 	if (sp->cls1.classValid)
1366 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
1367 	if (sp->cls2.classValid)
1368 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
1369 	if (sp->cls3.classValid)
1370 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
1371 	if (sp->cls4.classValid)
1372 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
1373 	ndlp->nlp_maxframe =
1374 		((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
1375 
1376 	if ((vport->fc_flag & FC_PT2PT) &&
1377 	    (vport->fc_flag & FC_PT2PT_PLOGI)) {
1378 		ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
1379 		if (sp->cmn.edtovResolution) {
1380 			/* E_D_TOV ticks are in nanoseconds */
1381 			ed_tov = (phba->fc_edtov + 999999) / 1000000;
1382 		}
1383 
1384 		ndlp->nlp_flag &= ~NLP_SUPPRESS_RSP;
1385 		if ((phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) &&
1386 		    sp->cmn.valid_vendor_ver_level) {
1387 			vid = be32_to_cpu(sp->un.vv.vid);
1388 			flag = be32_to_cpu(sp->un.vv.flags);
1389 			if ((vid == LPFC_VV_EMLX_ID) &&
1390 			    (flag & LPFC_VV_SUPPRESS_RSP))
1391 				ndlp->nlp_flag |= NLP_SUPPRESS_RSP;
1392 		}
1393 
1394 		/*
1395 		 * Use the larger EDTOV
1396 		 * RATOV = 2 * EDTOV for pt-to-pt
1397 		 */
1398 		if (ed_tov > phba->fc_edtov)
1399 			phba->fc_edtov = ed_tov;
1400 		phba->fc_ratov = (2 * phba->fc_edtov) / 1000;
1401 
1402 		memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
1403 
1404 		/* Issue config_link / reg_vfi to account for updated TOV's */
1405 		if (phba->sli_rev == LPFC_SLI_REV4) {
1406 			lpfc_issue_reg_vfi(vport);
1407 		} else {
1408 			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1409 			if (!mbox) {
1410 				lpfc_printf_vlog(vport, KERN_ERR,
1411 						 LOG_TRACE_EVENT,
1412 						 "0133 PLOGI: no memory "
1413 						 "for config_link "
1414 						 "Data: x%x x%x x%x x%x\n",
1415 						 ndlp->nlp_DID, ndlp->nlp_state,
1416 						 ndlp->nlp_flag, ndlp->nlp_rpi);
1417 				goto out;
1418 			}
1419 
1420 			lpfc_config_link(phba, mbox);
1421 
1422 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1423 			mbox->vport = vport;
1424 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1425 			if (rc == MBX_NOT_FINISHED) {
1426 				mempool_free(mbox, phba->mbox_mem_pool);
1427 				goto out;
1428 			}
1429 		}
1430 	}
1431 
1432 	lpfc_unreg_rpi(vport, ndlp);
1433 
1434 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1435 	if (!mbox) {
1436 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1437 				 "0018 PLOGI: no memory for reg_login "
1438 				 "Data: x%x x%x x%x x%x\n",
1439 				 ndlp->nlp_DID, ndlp->nlp_state,
1440 				 ndlp->nlp_flag, ndlp->nlp_rpi);
1441 		goto out;
1442 	}
1443 
1444 	if (lpfc_reg_rpi(phba, vport->vpi, irsp->un.elsreq64.remoteID,
1445 			 (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
1446 		switch (ndlp->nlp_DID) {
1447 		case NameServer_DID:
1448 			mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
1449 			/* Fabric Controller Node needs these parameters. */
1450 			memcpy(&ndlp->fc_sparam, sp, sizeof(struct serv_parm));
1451 			break;
1452 		case FDMI_DID:
1453 			mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
1454 			break;
1455 		default:
1456 			ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
1457 			mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
1458 		}
1459 
1460 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
1461 		if (!mbox->ctx_ndlp)
1462 			goto out;
1463 
1464 		mbox->vport = vport;
1465 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
1466 		    != MBX_NOT_FINISHED) {
1467 			lpfc_nlp_set_state(vport, ndlp,
1468 					   NLP_STE_REG_LOGIN_ISSUE);
1469 			return ndlp->nlp_state;
1470 		}
1471 		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
1472 			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1473 		/* decrement node reference count to the failed mbox
1474 		 * command
1475 		 */
1476 		lpfc_nlp_put(ndlp);
1477 		mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
1478 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
1479 		kfree(mp);
1480 		mempool_free(mbox, phba->mbox_mem_pool);
1481 
1482 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1483 				 "0134 PLOGI: cannot issue reg_login "
1484 				 "Data: x%x x%x x%x x%x\n",
1485 				 ndlp->nlp_DID, ndlp->nlp_state,
1486 				 ndlp->nlp_flag, ndlp->nlp_rpi);
1487 	} else {
1488 		mempool_free(mbox, phba->mbox_mem_pool);
1489 
1490 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1491 				 "0135 PLOGI: cannot format reg_login "
1492 				 "Data: x%x x%x x%x x%x\n",
1493 				 ndlp->nlp_DID, ndlp->nlp_state,
1494 				 ndlp->nlp_flag, ndlp->nlp_rpi);
1495 	}
1496 
1497 
1498 out:
1499 	if (ndlp->nlp_DID == NameServer_DID) {
1500 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1501 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1502 				 "0261 Cannot Register NameServer login\n");
1503 	}
1504 
1505 	/*
1506 	** In case the node reference counter does not go to zero, ensure that
1507 	** the stale state for the node is not processed.
1508 	*/
1509 
1510 	ndlp->nlp_prev_state = ndlp->nlp_state;
1511 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1512 	return NLP_STE_FREED_NODE;
1513 }
1514 
1515 static uint32_t
lpfc_cmpl_logo_plogi_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1516 lpfc_cmpl_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1517 			   void *arg, uint32_t evt)
1518 {
1519 	return ndlp->nlp_state;
1520 }
1521 
1522 static uint32_t
lpfc_cmpl_reglogin_plogi_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1523 lpfc_cmpl_reglogin_plogi_issue(struct lpfc_vport *vport,
1524 	struct lpfc_nodelist *ndlp, void *arg, uint32_t evt)
1525 {
1526 	struct lpfc_hba *phba;
1527 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1528 	MAILBOX_t *mb = &pmb->u.mb;
1529 	uint16_t rpi;
1530 
1531 	phba = vport->phba;
1532 	/* Release the RPI */
1533 	if (!(phba->pport->load_flag & FC_UNLOADING) &&
1534 		!mb->mbxStatus) {
1535 		rpi = pmb->u.mb.un.varWords[0];
1536 		lpfc_release_rpi(phba, vport, ndlp, rpi);
1537 	}
1538 	return ndlp->nlp_state;
1539 }
1540 
1541 static uint32_t
lpfc_device_rm_plogi_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1542 lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1543 			   void *arg, uint32_t evt)
1544 {
1545 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1546 		spin_lock_irq(&ndlp->lock);
1547 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1548 		spin_unlock_irq(&ndlp->lock);
1549 		return ndlp->nlp_state;
1550 	} else {
1551 		/* software abort outstanding PLOGI */
1552 		lpfc_els_abort(vport->phba, ndlp);
1553 
1554 		lpfc_drop_node(vport, ndlp);
1555 		return NLP_STE_FREED_NODE;
1556 	}
1557 }
1558 
1559 static uint32_t
lpfc_device_recov_plogi_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1560 lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
1561 			      struct lpfc_nodelist *ndlp,
1562 			      void *arg,
1563 			      uint32_t evt)
1564 {
1565 	struct lpfc_hba  *phba = vport->phba;
1566 
1567 	/* Don't do anything that will mess up processing of the
1568 	 * previous RSCN.
1569 	 */
1570 	if (vport->fc_flag & FC_RSCN_DEFERRED)
1571 		return ndlp->nlp_state;
1572 
1573 	/* software abort outstanding PLOGI */
1574 	lpfc_els_abort(phba, ndlp);
1575 
1576 	ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1577 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1578 	spin_lock_irq(&ndlp->lock);
1579 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1580 	spin_unlock_irq(&ndlp->lock);
1581 
1582 	return ndlp->nlp_state;
1583 }
1584 
1585 static uint32_t
lpfc_rcv_plogi_adisc_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1586 lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1587 			   void *arg, uint32_t evt)
1588 {
1589 	struct lpfc_hba   *phba = vport->phba;
1590 	struct lpfc_iocbq *cmdiocb;
1591 
1592 	/* software abort outstanding ADISC */
1593 	lpfc_els_abort(phba, ndlp);
1594 
1595 	cmdiocb = (struct lpfc_iocbq *) arg;
1596 
1597 	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1598 		if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1599 			spin_lock_irq(&ndlp->lock);
1600 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1601 			spin_unlock_irq(&ndlp->lock);
1602 			if (vport->num_disc_nodes)
1603 				lpfc_more_adisc(vport);
1604 		}
1605 		return ndlp->nlp_state;
1606 	}
1607 	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1608 	lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
1609 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1610 
1611 	return ndlp->nlp_state;
1612 }
1613 
1614 static uint32_t
lpfc_rcv_prli_adisc_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1615 lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1616 			  void *arg, uint32_t evt)
1617 {
1618 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1619 
1620 	if (lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
1621 		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1622 	return ndlp->nlp_state;
1623 }
1624 
1625 static uint32_t
lpfc_rcv_logo_adisc_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1626 lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1627 			  void *arg, uint32_t evt)
1628 {
1629 	struct lpfc_hba *phba = vport->phba;
1630 	struct lpfc_iocbq *cmdiocb;
1631 
1632 	cmdiocb = (struct lpfc_iocbq *) arg;
1633 
1634 	/* software abort outstanding ADISC */
1635 	lpfc_els_abort(phba, ndlp);
1636 
1637 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1638 	return ndlp->nlp_state;
1639 }
1640 
1641 static uint32_t
lpfc_rcv_padisc_adisc_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1642 lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
1643 			    struct lpfc_nodelist *ndlp,
1644 			    void *arg, uint32_t evt)
1645 {
1646 	struct lpfc_iocbq *cmdiocb;
1647 
1648 	cmdiocb = (struct lpfc_iocbq *) arg;
1649 
1650 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1651 	return ndlp->nlp_state;
1652 }
1653 
1654 static uint32_t
lpfc_rcv_prlo_adisc_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1655 lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1656 			  void *arg, uint32_t evt)
1657 {
1658 	struct lpfc_iocbq *cmdiocb;
1659 
1660 	cmdiocb = (struct lpfc_iocbq *) arg;
1661 
1662 	/* Treat like rcv logo */
1663 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1664 	return ndlp->nlp_state;
1665 }
1666 
1667 static uint32_t
lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1668 lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
1669 			    struct lpfc_nodelist *ndlp,
1670 			    void *arg, uint32_t evt)
1671 {
1672 	struct lpfc_hba   *phba = vport->phba;
1673 	struct lpfc_iocbq *cmdiocb, *rspiocb;
1674 	IOCB_t *irsp;
1675 	ADISC *ap;
1676 	int rc;
1677 
1678 	cmdiocb = (struct lpfc_iocbq *) arg;
1679 	rspiocb = cmdiocb->context_un.rsp_iocb;
1680 
1681 	ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1682 	irsp = &rspiocb->iocb;
1683 
1684 	if ((irsp->ulpStatus) ||
1685 	    (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
1686 		/* 1 sec timeout */
1687 		mod_timer(&ndlp->nlp_delayfunc,
1688 			  jiffies + msecs_to_jiffies(1000));
1689 		spin_lock_irq(&ndlp->lock);
1690 		ndlp->nlp_flag |= NLP_DELAY_TMO;
1691 		spin_unlock_irq(&ndlp->lock);
1692 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1693 
1694 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1695 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1696 		lpfc_unreg_rpi(vport, ndlp);
1697 		return ndlp->nlp_state;
1698 	}
1699 
1700 	if (phba->sli_rev == LPFC_SLI_REV4) {
1701 		rc = lpfc_sli4_resume_rpi(ndlp, NULL, NULL);
1702 		if (rc) {
1703 			/* Stay in state and retry. */
1704 			ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1705 			return ndlp->nlp_state;
1706 		}
1707 	}
1708 
1709 	if (ndlp->nlp_type & NLP_FCP_TARGET)
1710 		ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1711 
1712 	if (ndlp->nlp_type & NLP_NVME_TARGET)
1713 		ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1714 
1715 	if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET)) {
1716 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1717 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1718 	} else {
1719 		ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1720 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1721 	}
1722 
1723 	return ndlp->nlp_state;
1724 }
1725 
1726 static uint32_t
lpfc_device_rm_adisc_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1727 lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1728 			   void *arg, uint32_t evt)
1729 {
1730 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1731 		spin_lock_irq(&ndlp->lock);
1732 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1733 		spin_unlock_irq(&ndlp->lock);
1734 		return ndlp->nlp_state;
1735 	} else {
1736 		/* software abort outstanding ADISC */
1737 		lpfc_els_abort(vport->phba, ndlp);
1738 
1739 		lpfc_drop_node(vport, ndlp);
1740 		return NLP_STE_FREED_NODE;
1741 	}
1742 }
1743 
1744 static uint32_t
lpfc_device_recov_adisc_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1745 lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
1746 			      struct lpfc_nodelist *ndlp,
1747 			      void *arg,
1748 			      uint32_t evt)
1749 {
1750 	struct lpfc_hba  *phba = vport->phba;
1751 
1752 	/* Don't do anything that will mess up processing of the
1753 	 * previous RSCN.
1754 	 */
1755 	if (vport->fc_flag & FC_RSCN_DEFERRED)
1756 		return ndlp->nlp_state;
1757 
1758 	/* software abort outstanding ADISC */
1759 	lpfc_els_abort(phba, ndlp);
1760 
1761 	ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1762 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1763 	spin_lock_irq(&ndlp->lock);
1764 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1765 	spin_unlock_irq(&ndlp->lock);
1766 	lpfc_disc_set_adisc(vport, ndlp);
1767 	return ndlp->nlp_state;
1768 }
1769 
1770 static uint32_t
lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1771 lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport *vport,
1772 			      struct lpfc_nodelist *ndlp,
1773 			      void *arg,
1774 			      uint32_t evt)
1775 {
1776 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1777 
1778 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1779 	return ndlp->nlp_state;
1780 }
1781 
1782 static uint32_t
lpfc_rcv_prli_reglogin_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1783 lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
1784 			     struct lpfc_nodelist *ndlp,
1785 			     void *arg,
1786 			     uint32_t evt)
1787 {
1788 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1789 	struct ls_rjt     stat;
1790 
1791 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb)) {
1792 		return ndlp->nlp_state;
1793 	}
1794 	if (vport->phba->nvmet_support) {
1795 		/* NVME Target mode.  Handle and respond to the PRLI and
1796 		 * transition to UNMAPPED provided the RPI has completed
1797 		 * registration.
1798 		 */
1799 		if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
1800 			lpfc_rcv_prli(vport, ndlp, cmdiocb);
1801 			lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1802 		} else {
1803 			/* RPI registration has not completed. Reject the PRLI
1804 			 * to prevent an illegal state transition when the
1805 			 * rpi registration does complete.
1806 			 */
1807 			memset(&stat, 0, sizeof(struct ls_rjt));
1808 			stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
1809 			stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1810 			lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
1811 					    ndlp, NULL);
1812 			return ndlp->nlp_state;
1813 		}
1814 	} else {
1815 		/* Initiator mode. */
1816 		lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1817 	}
1818 	return ndlp->nlp_state;
1819 }
1820 
1821 static uint32_t
lpfc_rcv_logo_reglogin_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1822 lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
1823 			     struct lpfc_nodelist *ndlp,
1824 			     void *arg,
1825 			     uint32_t evt)
1826 {
1827 	struct lpfc_hba   *phba = vport->phba;
1828 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1829 	LPFC_MBOXQ_t	  *mb;
1830 	LPFC_MBOXQ_t	  *nextmb;
1831 	struct lpfc_dmabuf *mp;
1832 	struct lpfc_nodelist *ns_ndlp;
1833 
1834 	cmdiocb = (struct lpfc_iocbq *) arg;
1835 
1836 	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1837 	if ((mb = phba->sli.mbox_active)) {
1838 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1839 		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
1840 			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1841 			lpfc_nlp_put(ndlp);
1842 			mb->ctx_ndlp = NULL;
1843 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1844 		}
1845 	}
1846 
1847 	spin_lock_irq(&phba->hbalock);
1848 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1849 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1850 		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
1851 			mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
1852 			if (mp) {
1853 				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
1854 				kfree(mp);
1855 			}
1856 			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1857 			lpfc_nlp_put(ndlp);
1858 			list_del(&mb->list);
1859 			phba->sli.mboxq_cnt--;
1860 			mempool_free(mb, phba->mbox_mem_pool);
1861 		}
1862 	}
1863 	spin_unlock_irq(&phba->hbalock);
1864 
1865 	/* software abort if any GID_FT is outstanding */
1866 	if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
1867 		ns_ndlp = lpfc_findnode_did(vport, NameServer_DID);
1868 		if (ns_ndlp)
1869 			lpfc_els_abort(phba, ns_ndlp);
1870 	}
1871 
1872 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1873 	return ndlp->nlp_state;
1874 }
1875 
1876 static uint32_t
lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1877 lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
1878 			       struct lpfc_nodelist *ndlp,
1879 			       void *arg,
1880 			       uint32_t evt)
1881 {
1882 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1883 
1884 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1885 	return ndlp->nlp_state;
1886 }
1887 
1888 static uint32_t
lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1889 lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
1890 			     struct lpfc_nodelist *ndlp,
1891 			     void *arg,
1892 			     uint32_t evt)
1893 {
1894 	struct lpfc_iocbq *cmdiocb;
1895 
1896 	cmdiocb = (struct lpfc_iocbq *) arg;
1897 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1898 	return ndlp->nlp_state;
1899 }
1900 
1901 static uint32_t
lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)1902 lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
1903 				  struct lpfc_nodelist *ndlp,
1904 				  void *arg,
1905 				  uint32_t evt)
1906 {
1907 	struct lpfc_hba *phba = vport->phba;
1908 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1909 	MAILBOX_t *mb = &pmb->u.mb;
1910 	uint32_t did  = mb->un.varWords[1];
1911 
1912 	if (mb->mbxStatus) {
1913 		/* RegLogin failed */
1914 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1915 				 "0246 RegLogin failed Data: x%x x%x x%x x%x "
1916 				 "x%x\n",
1917 				 did, mb->mbxStatus, vport->port_state,
1918 				 mb->un.varRegLogin.vpi,
1919 				 mb->un.varRegLogin.rpi);
1920 		/*
1921 		 * If RegLogin failed due to lack of HBA resources do not
1922 		 * retry discovery.
1923 		 */
1924 		if (mb->mbxStatus == MBXERR_RPI_FULL) {
1925 			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1926 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1927 			return ndlp->nlp_state;
1928 		}
1929 
1930 		/* Put ndlp in npr state set plogi timer for 1 sec */
1931 		mod_timer(&ndlp->nlp_delayfunc,
1932 			  jiffies + msecs_to_jiffies(1000 * 1));
1933 		spin_lock_irq(&ndlp->lock);
1934 		ndlp->nlp_flag |= NLP_DELAY_TMO;
1935 		spin_unlock_irq(&ndlp->lock);
1936 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1937 
1938 		lpfc_issue_els_logo(vport, ndlp, 0);
1939 		return ndlp->nlp_state;
1940 	}
1941 
1942 	/* SLI4 ports have preallocated logical rpis. */
1943 	if (phba->sli_rev < LPFC_SLI_REV4)
1944 		ndlp->nlp_rpi = mb->un.varWords[0];
1945 
1946 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1947 
1948 	/* Only if we are not a fabric nport do we issue PRLI */
1949 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1950 			 "3066 RegLogin Complete on x%x x%x x%x\n",
1951 			 did, ndlp->nlp_type, ndlp->nlp_fc4_type);
1952 	if (!(ndlp->nlp_type & NLP_FABRIC) &&
1953 	    (phba->nvmet_support == 0)) {
1954 		/* The driver supports FCP and NVME concurrently.  If the
1955 		 * ndlp's nlp_fc4_type is still zero, the driver doesn't
1956 		 * know what PRLI to send yet.  Figure that out now and
1957 		 * call PRLI depending on the outcome.
1958 		 */
1959 		if (vport->fc_flag & FC_PT2PT) {
1960 			/* If we are pt2pt, there is no Fabric to determine
1961 			 * the FC4 type of the remote nport. So if NVME
1962 			 * is configured try it.
1963 			 */
1964 			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1965 			if ((!(vport->fc_flag & FC_PT2PT_NO_NVME)) &&
1966 			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH ||
1967 			    vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1968 				ndlp->nlp_fc4_type |= NLP_FC4_NVME;
1969 				/* We need to update the localport also */
1970 				lpfc_nvme_update_localport(vport);
1971 			}
1972 
1973 		} else if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
1974 			ndlp->nlp_fc4_type |= NLP_FC4_FCP;
1975 
1976 		} else if (ndlp->nlp_fc4_type == 0) {
1977 			/* If we are only configured for FCP, the driver
1978 			 * should just issue PRLI for FCP. Otherwise issue
1979 			 * GFT_ID to determine if remote port supports NVME.
1980 			 */
1981 			if (vport->cfg_enable_fc4_type != LPFC_ENABLE_FCP) {
1982 				lpfc_ns_cmd(vport, SLI_CTNS_GFT_ID, 0,
1983 					    ndlp->nlp_DID);
1984 				return ndlp->nlp_state;
1985 			}
1986 			ndlp->nlp_fc4_type = NLP_FC4_FCP;
1987 		}
1988 
1989 		ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1990 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1991 		if (lpfc_issue_els_prli(vport, ndlp, 0)) {
1992 			lpfc_issue_els_logo(vport, ndlp, 0);
1993 			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1994 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1995 		}
1996 	} else {
1997 		if ((vport->fc_flag & FC_PT2PT) && phba->nvmet_support)
1998 			phba->targetport->port_id = vport->fc_myDID;
1999 
2000 		/* Only Fabric ports should transition. NVME target
2001 		 * must complete PRLI.
2002 		 */
2003 		if (ndlp->nlp_type & NLP_FABRIC) {
2004 			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2005 			ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2006 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2007 		}
2008 	}
2009 	return ndlp->nlp_state;
2010 }
2011 
2012 static uint32_t
lpfc_device_rm_reglogin_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2013 lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
2014 			      struct lpfc_nodelist *ndlp,
2015 			      void *arg,
2016 			      uint32_t evt)
2017 {
2018 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2019 		spin_lock_irq(&ndlp->lock);
2020 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
2021 		spin_unlock_irq(&ndlp->lock);
2022 		return ndlp->nlp_state;
2023 	} else {
2024 		lpfc_drop_node(vport, ndlp);
2025 		return NLP_STE_FREED_NODE;
2026 	}
2027 }
2028 
2029 static uint32_t
lpfc_device_recov_reglogin_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2030 lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
2031 				 struct lpfc_nodelist *ndlp,
2032 				 void *arg,
2033 				 uint32_t evt)
2034 {
2035 	/* Don't do anything that will mess up processing of the
2036 	 * previous RSCN.
2037 	 */
2038 	if (vport->fc_flag & FC_RSCN_DEFERRED)
2039 		return ndlp->nlp_state;
2040 
2041 	ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
2042 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2043 	spin_lock_irq(&ndlp->lock);
2044 
2045 	/* If we are a target we won't immediately transition into PRLI,
2046 	 * so if REG_LOGIN already completed we don't need to ignore it.
2047 	 */
2048 	if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED) ||
2049 	    !vport->phba->nvmet_support)
2050 		ndlp->nlp_flag |= NLP_IGNR_REG_CMPL;
2051 
2052 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2053 	spin_unlock_irq(&ndlp->lock);
2054 	lpfc_disc_set_adisc(vport, ndlp);
2055 	return ndlp->nlp_state;
2056 }
2057 
2058 static uint32_t
lpfc_rcv_plogi_prli_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2059 lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2060 			  void *arg, uint32_t evt)
2061 {
2062 	struct lpfc_iocbq *cmdiocb;
2063 
2064 	cmdiocb = (struct lpfc_iocbq *) arg;
2065 
2066 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2067 	return ndlp->nlp_state;
2068 }
2069 
2070 static uint32_t
lpfc_rcv_prli_prli_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2071 lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2072 			 void *arg, uint32_t evt)
2073 {
2074 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2075 
2076 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
2077 		return ndlp->nlp_state;
2078 	lpfc_rcv_prli(vport, ndlp, cmdiocb);
2079 	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2080 	return ndlp->nlp_state;
2081 }
2082 
2083 static uint32_t
lpfc_rcv_logo_prli_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2084 lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2085 			 void *arg, uint32_t evt)
2086 {
2087 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2088 
2089 	/* Software abort outstanding PRLI before sending acc */
2090 	lpfc_els_abort(vport->phba, ndlp);
2091 
2092 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2093 	return ndlp->nlp_state;
2094 }
2095 
2096 static uint32_t
lpfc_rcv_padisc_prli_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2097 lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2098 			   void *arg, uint32_t evt)
2099 {
2100 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2101 
2102 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2103 	return ndlp->nlp_state;
2104 }
2105 
2106 /* This routine is envoked when we rcv a PRLO request from a nport
2107  * we are logged into.  We should send back a PRLO rsp setting the
2108  * appropriate bits.
2109  * NEXT STATE = PRLI_ISSUE
2110  */
2111 static uint32_t
lpfc_rcv_prlo_prli_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2112 lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2113 			 void *arg, uint32_t evt)
2114 {
2115 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2116 
2117 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2118 	return ndlp->nlp_state;
2119 }
2120 
2121 static uint32_t
lpfc_cmpl_prli_prli_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2122 lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2123 			  void *arg, uint32_t evt)
2124 {
2125 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2126 	struct lpfc_hba   *phba = vport->phba;
2127 	IOCB_t *irsp;
2128 	PRLI *npr;
2129 	struct lpfc_nvme_prli *nvpr;
2130 	void *temp_ptr;
2131 
2132 	cmdiocb = (struct lpfc_iocbq *) arg;
2133 	rspiocb = cmdiocb->context_un.rsp_iocb;
2134 
2135 	/* A solicited PRLI is either FCP or NVME.  The PRLI cmd/rsp
2136 	 * format is different so NULL the two PRLI types so that the
2137 	 * driver correctly gets the correct context.
2138 	 */
2139 	npr = NULL;
2140 	nvpr = NULL;
2141 	temp_ptr = lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
2142 	if (cmdiocb->iocb_flag & LPFC_PRLI_FCP_REQ)
2143 		npr = (PRLI *) temp_ptr;
2144 	else if (cmdiocb->iocb_flag & LPFC_PRLI_NVME_REQ)
2145 		nvpr = (struct lpfc_nvme_prli *) temp_ptr;
2146 
2147 	irsp = &rspiocb->iocb;
2148 	if (irsp->ulpStatus) {
2149 		if ((vport->port_type == LPFC_NPIV_PORT) &&
2150 		    vport->cfg_restrict_login) {
2151 			goto out;
2152 		}
2153 
2154 		/* Adjust the nlp_type accordingly if the PRLI failed */
2155 		if (npr)
2156 			ndlp->nlp_fc4_type &= ~NLP_FC4_FCP;
2157 		if (nvpr)
2158 			ndlp->nlp_fc4_type &= ~NLP_FC4_NVME;
2159 
2160 		/* We can't set the DSM state till BOTH PRLIs complete */
2161 		goto out_err;
2162 	}
2163 
2164 	if (npr && (npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
2165 	    (npr->prliType == PRLI_FCP_TYPE)) {
2166 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2167 				 "6028 FCP NPR PRLI Cmpl Init %d Target %d\n",
2168 				 npr->initiatorFunc,
2169 				 npr->targetFunc);
2170 		if (npr->initiatorFunc)
2171 			ndlp->nlp_type |= NLP_FCP_INITIATOR;
2172 		if (npr->targetFunc) {
2173 			ndlp->nlp_type |= NLP_FCP_TARGET;
2174 			if (npr->writeXferRdyDis)
2175 				ndlp->nlp_flag |= NLP_FIRSTBURST;
2176 		}
2177 		if (npr->Retry)
2178 			ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
2179 
2180 	} else if (nvpr &&
2181 		   (bf_get_be32(prli_acc_rsp_code, nvpr) ==
2182 		    PRLI_REQ_EXECUTED) &&
2183 		   (bf_get_be32(prli_type_code, nvpr) ==
2184 		    PRLI_NVME_TYPE)) {
2185 
2186 		/* Complete setting up the remote ndlp personality. */
2187 		if (bf_get_be32(prli_init, nvpr))
2188 			ndlp->nlp_type |= NLP_NVME_INITIATOR;
2189 
2190 		if (phba->nsler && bf_get_be32(prli_nsler, nvpr) &&
2191 		    bf_get_be32(prli_conf, nvpr))
2192 
2193 			ndlp->nlp_nvme_info |= NLP_NVME_NSLER;
2194 		else
2195 			ndlp->nlp_nvme_info &= ~NLP_NVME_NSLER;
2196 
2197 		/* Target driver cannot solicit NVME FB. */
2198 		if (bf_get_be32(prli_tgt, nvpr)) {
2199 			/* Complete the nvme target roles.  The transport
2200 			 * needs to know if the rport is capable of
2201 			 * discovery in addition to its role.
2202 			 */
2203 			ndlp->nlp_type |= NLP_NVME_TARGET;
2204 			if (bf_get_be32(prli_disc, nvpr))
2205 				ndlp->nlp_type |= NLP_NVME_DISCOVERY;
2206 
2207 			/*
2208 			 * If prli_fba is set, the Target supports FirstBurst.
2209 			 * If prli_fb_sz is 0, the FirstBurst size is unlimited,
2210 			 * otherwise it defines the actual size supported by
2211 			 * the NVME Target.
2212 			 */
2213 			if ((bf_get_be32(prli_fba, nvpr) == 1) &&
2214 			    (phba->cfg_nvme_enable_fb) &&
2215 			    (!phba->nvmet_support)) {
2216 				/* Both sides support FB. The target's first
2217 				 * burst size is a 512 byte encoded value.
2218 				 */
2219 				ndlp->nlp_flag |= NLP_FIRSTBURST;
2220 				ndlp->nvme_fb_size = bf_get_be32(prli_fb_sz,
2221 								 nvpr);
2222 
2223 				/* Expressed in units of 512 bytes */
2224 				if (ndlp->nvme_fb_size)
2225 					ndlp->nvme_fb_size <<=
2226 						LPFC_NVME_FB_SHIFT;
2227 				else
2228 					ndlp->nvme_fb_size = LPFC_NVME_MAX_FB;
2229 			}
2230 		}
2231 
2232 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2233 				 "6029 NVME PRLI Cmpl w1 x%08x "
2234 				 "w4 x%08x w5 x%08x flag x%x, "
2235 				 "fcp_info x%x nlp_type x%x\n",
2236 				 be32_to_cpu(nvpr->word1),
2237 				 be32_to_cpu(nvpr->word4),
2238 				 be32_to_cpu(nvpr->word5),
2239 				 ndlp->nlp_flag, ndlp->nlp_fcp_info,
2240 				 ndlp->nlp_type);
2241 	}
2242 	if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
2243 	    (vport->port_type == LPFC_NPIV_PORT) &&
2244 	     vport->cfg_restrict_login) {
2245 out:
2246 		spin_lock_irq(&ndlp->lock);
2247 		ndlp->nlp_flag |= NLP_TARGET_REMOVE;
2248 		spin_unlock_irq(&ndlp->lock);
2249 		lpfc_issue_els_logo(vport, ndlp, 0);
2250 
2251 		ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
2252 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2253 		return ndlp->nlp_state;
2254 	}
2255 
2256 out_err:
2257 	/* The ndlp state cannot move to MAPPED or UNMAPPED before all PRLIs
2258 	 * are complete.
2259 	 */
2260 	if (ndlp->fc4_prli_sent == 0) {
2261 		ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
2262 		if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET))
2263 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
2264 		else if (ndlp->nlp_type &
2265 			 (NLP_FCP_INITIATOR | NLP_NVME_INITIATOR))
2266 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2267 	} else
2268 		lpfc_printf_vlog(vport,
2269 				 KERN_INFO, LOG_ELS,
2270 				 "3067 PRLI's still outstanding "
2271 				 "on x%06x - count %d, Pend Node Mode "
2272 				 "transition...\n",
2273 				 ndlp->nlp_DID, ndlp->fc4_prli_sent);
2274 
2275 	return ndlp->nlp_state;
2276 }
2277 
2278 /*! lpfc_device_rm_prli_issue
2279  *
2280  * \pre
2281  * \post
2282  * \param   phba
2283  * \param   ndlp
2284  * \param   arg
2285  * \param   evt
2286  * \return  uint32_t
2287  *
2288  * \b Description:
2289  *    This routine is envoked when we a request to remove a nport we are in the
2290  *    process of PRLIing. We should software abort outstanding prli, unreg
2291  *    login, send a logout. We will change node state to UNUSED_NODE, put it
2292  *    on plogi list so it can be freed when LOGO completes.
2293  *
2294  */
2295 
2296 static uint32_t
lpfc_device_rm_prli_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2297 lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2298 			  void *arg, uint32_t evt)
2299 {
2300 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2301 		spin_lock_irq(&ndlp->lock);
2302 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
2303 		spin_unlock_irq(&ndlp->lock);
2304 		return ndlp->nlp_state;
2305 	} else {
2306 		/* software abort outstanding PLOGI */
2307 		lpfc_els_abort(vport->phba, ndlp);
2308 
2309 		lpfc_drop_node(vport, ndlp);
2310 		return NLP_STE_FREED_NODE;
2311 	}
2312 }
2313 
2314 
2315 /*! lpfc_device_recov_prli_issue
2316  *
2317  * \pre
2318  * \post
2319  * \param   phba
2320  * \param   ndlp
2321  * \param   arg
2322  * \param   evt
2323  * \return  uint32_t
2324  *
2325  * \b Description:
2326  *    The routine is envoked when the state of a device is unknown, like
2327  *    during a link down. We should remove the nodelist entry from the
2328  *    unmapped list, issue a UNREG_LOGIN, do a software abort of the
2329  *    outstanding PRLI command, then free the node entry.
2330  */
2331 static uint32_t
lpfc_device_recov_prli_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2332 lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
2333 			     struct lpfc_nodelist *ndlp,
2334 			     void *arg,
2335 			     uint32_t evt)
2336 {
2337 	struct lpfc_hba  *phba = vport->phba;
2338 
2339 	/* Don't do anything that will mess up processing of the
2340 	 * previous RSCN.
2341 	 */
2342 	if (vport->fc_flag & FC_RSCN_DEFERRED)
2343 		return ndlp->nlp_state;
2344 
2345 	/* software abort outstanding PRLI */
2346 	lpfc_els_abort(phba, ndlp);
2347 
2348 	ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
2349 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2350 	spin_lock_irq(&ndlp->lock);
2351 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2352 	spin_unlock_irq(&ndlp->lock);
2353 	lpfc_disc_set_adisc(vport, ndlp);
2354 	return ndlp->nlp_state;
2355 }
2356 
2357 static uint32_t
lpfc_rcv_plogi_logo_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2358 lpfc_rcv_plogi_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2359 			  void *arg, uint32_t evt)
2360 {
2361 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2362 	struct ls_rjt     stat;
2363 
2364 	memset(&stat, 0, sizeof(struct ls_rjt));
2365 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2366 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2367 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2368 	return ndlp->nlp_state;
2369 }
2370 
2371 static uint32_t
lpfc_rcv_prli_logo_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2372 lpfc_rcv_prli_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2373 			 void *arg, uint32_t evt)
2374 {
2375 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2376 	struct ls_rjt     stat;
2377 
2378 	memset(&stat, 0, sizeof(struct ls_rjt));
2379 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2380 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2381 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2382 	return ndlp->nlp_state;
2383 }
2384 
2385 static uint32_t
lpfc_rcv_logo_logo_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2386 lpfc_rcv_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2387 			 void *arg, uint32_t evt)
2388 {
2389 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2390 
2391 	spin_lock_irq(&ndlp->lock);
2392 	ndlp->nlp_flag |= NLP_LOGO_ACC;
2393 	spin_unlock_irq(&ndlp->lock);
2394 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2395 	return ndlp->nlp_state;
2396 }
2397 
2398 static uint32_t
lpfc_rcv_padisc_logo_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2399 lpfc_rcv_padisc_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2400 			   void *arg, uint32_t evt)
2401 {
2402 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2403 	struct ls_rjt     stat;
2404 
2405 	memset(&stat, 0, sizeof(struct ls_rjt));
2406 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2407 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2408 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2409 	return ndlp->nlp_state;
2410 }
2411 
2412 static uint32_t
lpfc_rcv_prlo_logo_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2413 lpfc_rcv_prlo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2414 			 void *arg, uint32_t evt)
2415 {
2416 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
2417 	struct ls_rjt     stat;
2418 
2419 	memset(&stat, 0, sizeof(struct ls_rjt));
2420 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2421 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2422 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2423 	return ndlp->nlp_state;
2424 }
2425 
2426 static uint32_t
lpfc_cmpl_logo_logo_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2427 lpfc_cmpl_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2428 			  void *arg, uint32_t evt)
2429 {
2430 	ndlp->nlp_prev_state = NLP_STE_LOGO_ISSUE;
2431 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2432 	spin_lock_irq(&ndlp->lock);
2433 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2434 	spin_unlock_irq(&ndlp->lock);
2435 	lpfc_disc_set_adisc(vport, ndlp);
2436 	return ndlp->nlp_state;
2437 }
2438 
2439 static uint32_t
lpfc_device_rm_logo_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2440 lpfc_device_rm_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2441 			  void *arg, uint32_t evt)
2442 {
2443 	/*
2444 	 * DevLoss has timed out and is calling for Device Remove.
2445 	 * In this case, abort the LOGO and cleanup the ndlp
2446 	 */
2447 
2448 	lpfc_unreg_rpi(vport, ndlp);
2449 	/* software abort outstanding PLOGI */
2450 	lpfc_els_abort(vport->phba, ndlp);
2451 	lpfc_drop_node(vport, ndlp);
2452 	return NLP_STE_FREED_NODE;
2453 }
2454 
2455 static uint32_t
lpfc_device_recov_logo_issue(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2456 lpfc_device_recov_logo_issue(struct lpfc_vport *vport,
2457 			     struct lpfc_nodelist *ndlp,
2458 			     void *arg, uint32_t evt)
2459 {
2460 	/*
2461 	 * Device Recovery events have no meaning for a node with a LOGO
2462 	 * outstanding.  The LOGO has to complete first and handle the
2463 	 * node from that point.
2464 	 */
2465 	return ndlp->nlp_state;
2466 }
2467 
2468 static uint32_t
lpfc_rcv_plogi_unmap_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2469 lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2470 			  void *arg, uint32_t evt)
2471 {
2472 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2473 
2474 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2475 	return ndlp->nlp_state;
2476 }
2477 
2478 static uint32_t
lpfc_rcv_prli_unmap_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2479 lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2480 			 void *arg, uint32_t evt)
2481 {
2482 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2483 
2484 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
2485 		return ndlp->nlp_state;
2486 
2487 	lpfc_rcv_prli(vport, ndlp, cmdiocb);
2488 	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2489 	return ndlp->nlp_state;
2490 }
2491 
2492 static uint32_t
lpfc_rcv_logo_unmap_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2493 lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2494 			 void *arg, uint32_t evt)
2495 {
2496 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2497 
2498 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2499 	return ndlp->nlp_state;
2500 }
2501 
2502 static uint32_t
lpfc_rcv_padisc_unmap_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2503 lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2504 			   void *arg, uint32_t evt)
2505 {
2506 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2507 
2508 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2509 	return ndlp->nlp_state;
2510 }
2511 
2512 static uint32_t
lpfc_rcv_prlo_unmap_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2513 lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2514 			 void *arg, uint32_t evt)
2515 {
2516 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2517 
2518 	lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
2519 	return ndlp->nlp_state;
2520 }
2521 
2522 static uint32_t
lpfc_device_rm_unmap_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2523 lpfc_device_rm_unmap_node(struct lpfc_vport *vport,
2524 			  struct lpfc_nodelist *ndlp,
2525 			  void *arg,
2526 			  uint32_t evt)
2527 {
2528 	lpfc_drop_node(vport, ndlp);
2529 	return NLP_STE_FREED_NODE;
2530 }
2531 
2532 static uint32_t
lpfc_device_recov_unmap_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2533 lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
2534 			     struct lpfc_nodelist *ndlp,
2535 			     void *arg,
2536 			     uint32_t evt)
2537 {
2538 	ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
2539 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2540 	spin_lock_irq(&ndlp->lock);
2541 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2542 	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
2543 	spin_unlock_irq(&ndlp->lock);
2544 	lpfc_disc_set_adisc(vport, ndlp);
2545 
2546 	return ndlp->nlp_state;
2547 }
2548 
2549 static uint32_t
lpfc_rcv_plogi_mapped_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2550 lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2551 			   void *arg, uint32_t evt)
2552 {
2553 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2554 
2555 	lpfc_rcv_plogi(vport, ndlp, cmdiocb);
2556 	return ndlp->nlp_state;
2557 }
2558 
2559 static uint32_t
lpfc_rcv_prli_mapped_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2560 lpfc_rcv_prli_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2561 			  void *arg, uint32_t evt)
2562 {
2563 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2564 
2565 	if (!lpfc_rcv_prli_support_check(vport, ndlp, cmdiocb))
2566 		return ndlp->nlp_state;
2567 	lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
2568 	return ndlp->nlp_state;
2569 }
2570 
2571 static uint32_t
lpfc_rcv_logo_mapped_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2572 lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2573 			  void *arg, uint32_t evt)
2574 {
2575 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2576 
2577 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2578 	return ndlp->nlp_state;
2579 }
2580 
2581 static uint32_t
lpfc_rcv_padisc_mapped_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2582 lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
2583 			    struct lpfc_nodelist *ndlp,
2584 			    void *arg, uint32_t evt)
2585 {
2586 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2587 
2588 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2589 	return ndlp->nlp_state;
2590 }
2591 
2592 static uint32_t
lpfc_rcv_prlo_mapped_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2593 lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2594 			  void *arg, uint32_t evt)
2595 {
2596 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2597 
2598 	/* flush the target */
2599 	lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
2600 
2601 	/* Treat like rcv logo */
2602 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
2603 	return ndlp->nlp_state;
2604 }
2605 
2606 static uint32_t
lpfc_device_recov_mapped_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2607 lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
2608 			      struct lpfc_nodelist *ndlp,
2609 			      void *arg,
2610 			      uint32_t evt)
2611 {
2612 	lpfc_disc_set_adisc(vport, ndlp);
2613 
2614 	ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
2615 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2616 	spin_lock_irq(&ndlp->lock);
2617 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2618 	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
2619 	spin_unlock_irq(&ndlp->lock);
2620 	return ndlp->nlp_state;
2621 }
2622 
2623 static uint32_t
lpfc_rcv_plogi_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2624 lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2625 			void *arg, uint32_t evt)
2626 {
2627 	struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
2628 
2629 	/* Ignore PLOGI if we have an outstanding LOGO */
2630 	if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
2631 		return ndlp->nlp_state;
2632 	if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
2633 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
2634 		spin_lock_irq(&ndlp->lock);
2635 		ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
2636 		spin_unlock_irq(&ndlp->lock);
2637 	} else if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
2638 		/* send PLOGI immediately, move to PLOGI issue state */
2639 		if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2640 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2641 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2642 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2643 		}
2644 	}
2645 	return ndlp->nlp_state;
2646 }
2647 
2648 static uint32_t
lpfc_rcv_prli_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2649 lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2650 		       void *arg, uint32_t evt)
2651 {
2652 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2653 	struct ls_rjt     stat;
2654 
2655 	memset(&stat, 0, sizeof (struct ls_rjt));
2656 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2657 	stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2658 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2659 
2660 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2661 		/*
2662 		 * ADISC nodes will be handled in regular discovery path after
2663 		 * receiving response from NS.
2664 		 *
2665 		 * For other nodes, Send PLOGI to trigger an implicit LOGO.
2666 		 */
2667 		if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2668 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2669 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2670 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2671 		}
2672 	}
2673 	return ndlp->nlp_state;
2674 }
2675 
2676 static uint32_t
lpfc_rcv_logo_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2677 lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
2678 		       void *arg, uint32_t evt)
2679 {
2680 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2681 
2682 	lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2683 	return ndlp->nlp_state;
2684 }
2685 
2686 static uint32_t
lpfc_rcv_padisc_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2687 lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2688 			 void *arg, uint32_t evt)
2689 {
2690 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2691 
2692 	lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2693 	/*
2694 	 * Do not start discovery if discovery is about to start
2695 	 * or discovery in progress for this node. Starting discovery
2696 	 * here will affect the counting of discovery threads.
2697 	 */
2698 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
2699 	    !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
2700 		/*
2701 		 * ADISC nodes will be handled in regular discovery path after
2702 		 * receiving response from NS.
2703 		 *
2704 		 * For other nodes, Send PLOGI to trigger an implicit LOGO.
2705 		 */
2706 		if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2707 			ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2708 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2709 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2710 		}
2711 	}
2712 	return ndlp->nlp_state;
2713 }
2714 
2715 static uint32_t
lpfc_rcv_prlo_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2716 lpfc_rcv_prlo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2717 		       void *arg, uint32_t evt)
2718 {
2719 	struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2720 
2721 	spin_lock_irq(&ndlp->lock);
2722 	ndlp->nlp_flag |= NLP_LOGO_ACC;
2723 	spin_unlock_irq(&ndlp->lock);
2724 
2725 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2726 
2727 	if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
2728 		mod_timer(&ndlp->nlp_delayfunc,
2729 			  jiffies + msecs_to_jiffies(1000 * 1));
2730 		spin_lock_irq(&ndlp->lock);
2731 		ndlp->nlp_flag |= NLP_DELAY_TMO;
2732 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2733 		spin_unlock_irq(&ndlp->lock);
2734 		ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
2735 	} else {
2736 		spin_lock_irq(&ndlp->lock);
2737 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2738 		spin_unlock_irq(&ndlp->lock);
2739 	}
2740 	return ndlp->nlp_state;
2741 }
2742 
2743 static uint32_t
lpfc_cmpl_plogi_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2744 lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2745 			 void *arg, uint32_t evt)
2746 {
2747 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2748 	IOCB_t *irsp;
2749 
2750 	cmdiocb = (struct lpfc_iocbq *) arg;
2751 	rspiocb = cmdiocb->context_un.rsp_iocb;
2752 
2753 	irsp = &rspiocb->iocb;
2754 	if (irsp->ulpStatus) {
2755 		return NLP_STE_FREED_NODE;
2756 	}
2757 	return ndlp->nlp_state;
2758 }
2759 
2760 static uint32_t
lpfc_cmpl_prli_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2761 lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2762 			void *arg, uint32_t evt)
2763 {
2764 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2765 	IOCB_t *irsp;
2766 
2767 	cmdiocb = (struct lpfc_iocbq *) arg;
2768 	rspiocb = cmdiocb->context_un.rsp_iocb;
2769 
2770 	irsp = &rspiocb->iocb;
2771 	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
2772 		lpfc_drop_node(vport, ndlp);
2773 		return NLP_STE_FREED_NODE;
2774 	}
2775 	return ndlp->nlp_state;
2776 }
2777 
2778 static uint32_t
lpfc_cmpl_logo_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2779 lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2780 			void *arg, uint32_t evt)
2781 {
2782 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2783 
2784 	/* For the fabric port just clear the fc flags. */
2785 	if (ndlp->nlp_DID == Fabric_DID) {
2786 		spin_lock_irq(shost->host_lock);
2787 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2788 		spin_unlock_irq(shost->host_lock);
2789 	}
2790 	lpfc_unreg_rpi(vport, ndlp);
2791 	return ndlp->nlp_state;
2792 }
2793 
2794 static uint32_t
lpfc_cmpl_adisc_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2795 lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2796 			 void *arg, uint32_t evt)
2797 {
2798 	struct lpfc_iocbq *cmdiocb, *rspiocb;
2799 	IOCB_t *irsp;
2800 
2801 	cmdiocb = (struct lpfc_iocbq *) arg;
2802 	rspiocb = cmdiocb->context_un.rsp_iocb;
2803 
2804 	irsp = &rspiocb->iocb;
2805 	if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
2806 		lpfc_drop_node(vport, ndlp);
2807 		return NLP_STE_FREED_NODE;
2808 	}
2809 	return ndlp->nlp_state;
2810 }
2811 
2812 static uint32_t
lpfc_cmpl_reglogin_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2813 lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
2814 			    struct lpfc_nodelist *ndlp,
2815 			    void *arg, uint32_t evt)
2816 {
2817 	LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2818 	MAILBOX_t    *mb = &pmb->u.mb;
2819 
2820 	if (!mb->mbxStatus) {
2821 		/* SLI4 ports have preallocated logical rpis. */
2822 		if (vport->phba->sli_rev < LPFC_SLI_REV4)
2823 			ndlp->nlp_rpi = mb->un.varWords[0];
2824 		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
2825 		if (ndlp->nlp_flag & NLP_LOGO_ACC) {
2826 			lpfc_unreg_rpi(vport, ndlp);
2827 		}
2828 	} else {
2829 		if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
2830 			lpfc_drop_node(vport, ndlp);
2831 			return NLP_STE_FREED_NODE;
2832 		}
2833 	}
2834 	return ndlp->nlp_state;
2835 }
2836 
2837 static uint32_t
lpfc_device_rm_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2838 lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2839 			void *arg, uint32_t evt)
2840 {
2841 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2842 		spin_lock_irq(&ndlp->lock);
2843 		ndlp->nlp_flag |= NLP_NODEV_REMOVE;
2844 		spin_unlock_irq(&ndlp->lock);
2845 		return ndlp->nlp_state;
2846 	}
2847 	lpfc_drop_node(vport, ndlp);
2848 	return NLP_STE_FREED_NODE;
2849 }
2850 
2851 static uint32_t
lpfc_device_recov_npr_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)2852 lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2853 			   void *arg, uint32_t evt)
2854 {
2855 	/* Don't do anything that will mess up processing of the
2856 	 * previous RSCN.
2857 	 */
2858 	if (vport->fc_flag & FC_RSCN_DEFERRED)
2859 		return ndlp->nlp_state;
2860 
2861 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
2862 	spin_lock_irq(&ndlp->lock);
2863 	ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2864 	ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
2865 	spin_unlock_irq(&ndlp->lock);
2866 	return ndlp->nlp_state;
2867 }
2868 
2869 
2870 /* This next section defines the NPort Discovery State Machine */
2871 
2872 /* There are 4 different double linked lists nodelist entries can reside on.
2873  * The plogi list and adisc list are used when Link Up discovery or RSCN
2874  * processing is needed. Each list holds the nodes that we will send PLOGI
2875  * or ADISC on. These lists will keep track of what nodes will be effected
2876  * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
2877  * The unmapped_list will contain all nodes that we have successfully logged
2878  * into at the Fibre Channel level. The mapped_list will contain all nodes
2879  * that are mapped FCP targets.
2880  */
2881 /*
2882  * The bind list is a list of undiscovered (potentially non-existent) nodes
2883  * that we have saved binding information on. This information is used when
2884  * nodes transition from the unmapped to the mapped list.
2885  */
2886 /* For UNUSED_NODE state, the node has just been allocated .
2887  * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
2888  * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
2889  * and put on the unmapped list. For ADISC processing, the node is taken off
2890  * the ADISC list and placed on either the mapped or unmapped list (depending
2891  * on its previous state). Once on the unmapped list, a PRLI is issued and the
2892  * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
2893  * changed to UNMAPPED_NODE. If the completion indicates a mapped
2894  * node, the node is taken off the unmapped list. The binding list is checked
2895  * for a valid binding, or a binding is automatically assigned. If binding
2896  * assignment is unsuccessful, the node is left on the unmapped list. If
2897  * binding assignment is successful, the associated binding list entry (if
2898  * any) is removed, and the node is placed on the mapped list.
2899  */
2900 /*
2901  * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
2902  * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
2903  * expire, all effected nodes will receive a DEVICE_RM event.
2904  */
2905 /*
2906  * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
2907  * to either the ADISC or PLOGI list.  After a Nameserver query or ALPA loopmap
2908  * check, additional nodes may be added or removed (via DEVICE_RM) to / from
2909  * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
2910  * we will first process the ADISC list.  32 entries are processed initially and
2911  * ADISC is initited for each one.  Completions / Events for each node are
2912  * funnelled thru the state machine.  As each node finishes ADISC processing, it
2913  * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
2914  * waiting, and the ADISC list count is identically 0, then we are done. For
2915  * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
2916  * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
2917  * list.  32 entries are processed initially and PLOGI is initited for each one.
2918  * Completions / Events for each node are funnelled thru the state machine.  As
2919  * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
2920  * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
2921  * indentically 0, then we are done. We have now completed discovery / RSCN
2922  * handling. Upon completion, ALL nodes should be on either the mapped or
2923  * unmapped lists.
2924  */
2925 
2926 static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
2927      (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
2928 	/* Action routine                  Event       Current State  */
2929 	lpfc_rcv_plogi_unused_node,	/* RCV_PLOGI   UNUSED_NODE    */
2930 	lpfc_rcv_els_unused_node,	/* RCV_PRLI        */
2931 	lpfc_rcv_logo_unused_node,	/* RCV_LOGO        */
2932 	lpfc_rcv_els_unused_node,	/* RCV_ADISC       */
2933 	lpfc_rcv_els_unused_node,	/* RCV_PDISC       */
2934 	lpfc_rcv_els_unused_node,	/* RCV_PRLO        */
2935 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
2936 	lpfc_disc_illegal,		/* CMPL_PRLI       */
2937 	lpfc_cmpl_logo_unused_node,	/* CMPL_LOGO       */
2938 	lpfc_disc_illegal,		/* CMPL_ADISC      */
2939 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
2940 	lpfc_device_rm_unused_node,	/* DEVICE_RM       */
2941 	lpfc_device_recov_unused_node,	/* DEVICE_RECOVERY */
2942 
2943 	lpfc_rcv_plogi_plogi_issue,	/* RCV_PLOGI   PLOGI_ISSUE    */
2944 	lpfc_rcv_prli_plogi_issue,	/* RCV_PRLI        */
2945 	lpfc_rcv_logo_plogi_issue,	/* RCV_LOGO        */
2946 	lpfc_rcv_els_plogi_issue,	/* RCV_ADISC       */
2947 	lpfc_rcv_els_plogi_issue,	/* RCV_PDISC       */
2948 	lpfc_rcv_els_plogi_issue,	/* RCV_PRLO        */
2949 	lpfc_cmpl_plogi_plogi_issue,	/* CMPL_PLOGI      */
2950 	lpfc_disc_illegal,		/* CMPL_PRLI       */
2951 	lpfc_cmpl_logo_plogi_issue,	/* CMPL_LOGO       */
2952 	lpfc_disc_illegal,		/* CMPL_ADISC      */
2953 	lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
2954 	lpfc_device_rm_plogi_issue,	/* DEVICE_RM       */
2955 	lpfc_device_recov_plogi_issue,	/* DEVICE_RECOVERY */
2956 
2957 	lpfc_rcv_plogi_adisc_issue,	/* RCV_PLOGI   ADISC_ISSUE    */
2958 	lpfc_rcv_prli_adisc_issue,	/* RCV_PRLI        */
2959 	lpfc_rcv_logo_adisc_issue,	/* RCV_LOGO        */
2960 	lpfc_rcv_padisc_adisc_issue,	/* RCV_ADISC       */
2961 	lpfc_rcv_padisc_adisc_issue,	/* RCV_PDISC       */
2962 	lpfc_rcv_prlo_adisc_issue,	/* RCV_PRLO        */
2963 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
2964 	lpfc_disc_illegal,		/* CMPL_PRLI       */
2965 	lpfc_disc_illegal,		/* CMPL_LOGO       */
2966 	lpfc_cmpl_adisc_adisc_issue,	/* CMPL_ADISC      */
2967 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
2968 	lpfc_device_rm_adisc_issue,	/* DEVICE_RM       */
2969 	lpfc_device_recov_adisc_issue,	/* DEVICE_RECOVERY */
2970 
2971 	lpfc_rcv_plogi_reglogin_issue,	/* RCV_PLOGI  REG_LOGIN_ISSUE */
2972 	lpfc_rcv_prli_reglogin_issue,	/* RCV_PLOGI       */
2973 	lpfc_rcv_logo_reglogin_issue,	/* RCV_LOGO        */
2974 	lpfc_rcv_padisc_reglogin_issue,	/* RCV_ADISC       */
2975 	lpfc_rcv_padisc_reglogin_issue,	/* RCV_PDISC       */
2976 	lpfc_rcv_prlo_reglogin_issue,	/* RCV_PRLO        */
2977 	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
2978 	lpfc_disc_illegal,		/* CMPL_PRLI       */
2979 	lpfc_disc_illegal,		/* CMPL_LOGO       */
2980 	lpfc_disc_illegal,		/* CMPL_ADISC      */
2981 	lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN  */
2982 	lpfc_device_rm_reglogin_issue,	/* DEVICE_RM       */
2983 	lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */
2984 
2985 	lpfc_rcv_plogi_prli_issue,	/* RCV_PLOGI   PRLI_ISSUE     */
2986 	lpfc_rcv_prli_prli_issue,	/* RCV_PRLI        */
2987 	lpfc_rcv_logo_prli_issue,	/* RCV_LOGO        */
2988 	lpfc_rcv_padisc_prli_issue,	/* RCV_ADISC       */
2989 	lpfc_rcv_padisc_prli_issue,	/* RCV_PDISC       */
2990 	lpfc_rcv_prlo_prli_issue,	/* RCV_PRLO        */
2991 	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
2992 	lpfc_cmpl_prli_prli_issue,	/* CMPL_PRLI       */
2993 	lpfc_disc_illegal,		/* CMPL_LOGO       */
2994 	lpfc_disc_illegal,		/* CMPL_ADISC      */
2995 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
2996 	lpfc_device_rm_prli_issue,	/* DEVICE_RM       */
2997 	lpfc_device_recov_prli_issue,	/* DEVICE_RECOVERY */
2998 
2999 	lpfc_rcv_plogi_logo_issue,	/* RCV_PLOGI   LOGO_ISSUE     */
3000 	lpfc_rcv_prli_logo_issue,	/* RCV_PRLI        */
3001 	lpfc_rcv_logo_logo_issue,	/* RCV_LOGO        */
3002 	lpfc_rcv_padisc_logo_issue,	/* RCV_ADISC       */
3003 	lpfc_rcv_padisc_logo_issue,	/* RCV_PDISC       */
3004 	lpfc_rcv_prlo_logo_issue,	/* RCV_PRLO        */
3005 	lpfc_cmpl_plogi_illegal,	/* CMPL_PLOGI      */
3006 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3007 	lpfc_cmpl_logo_logo_issue,	/* CMPL_LOGO       */
3008 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3009 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3010 	lpfc_device_rm_logo_issue,	/* DEVICE_RM       */
3011 	lpfc_device_recov_logo_issue,	/* DEVICE_RECOVERY */
3012 
3013 	lpfc_rcv_plogi_unmap_node,	/* RCV_PLOGI   UNMAPPED_NODE  */
3014 	lpfc_rcv_prli_unmap_node,	/* RCV_PRLI        */
3015 	lpfc_rcv_logo_unmap_node,	/* RCV_LOGO        */
3016 	lpfc_rcv_padisc_unmap_node,	/* RCV_ADISC       */
3017 	lpfc_rcv_padisc_unmap_node,	/* RCV_PDISC       */
3018 	lpfc_rcv_prlo_unmap_node,	/* RCV_PRLO        */
3019 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
3020 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3021 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3022 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3023 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3024 	lpfc_device_rm_unmap_node,	/* DEVICE_RM       */
3025 	lpfc_device_recov_unmap_node,	/* DEVICE_RECOVERY */
3026 
3027 	lpfc_rcv_plogi_mapped_node,	/* RCV_PLOGI   MAPPED_NODE    */
3028 	lpfc_rcv_prli_mapped_node,	/* RCV_PRLI        */
3029 	lpfc_rcv_logo_mapped_node,	/* RCV_LOGO        */
3030 	lpfc_rcv_padisc_mapped_node,	/* RCV_ADISC       */
3031 	lpfc_rcv_padisc_mapped_node,	/* RCV_PDISC       */
3032 	lpfc_rcv_prlo_mapped_node,	/* RCV_PRLO        */
3033 	lpfc_disc_illegal,		/* CMPL_PLOGI      */
3034 	lpfc_disc_illegal,		/* CMPL_PRLI       */
3035 	lpfc_disc_illegal,		/* CMPL_LOGO       */
3036 	lpfc_disc_illegal,		/* CMPL_ADISC      */
3037 	lpfc_disc_illegal,		/* CMPL_REG_LOGIN  */
3038 	lpfc_disc_illegal,		/* DEVICE_RM       */
3039 	lpfc_device_recov_mapped_node,	/* DEVICE_RECOVERY */
3040 
3041 	lpfc_rcv_plogi_npr_node,        /* RCV_PLOGI   NPR_NODE    */
3042 	lpfc_rcv_prli_npr_node,         /* RCV_PRLI        */
3043 	lpfc_rcv_logo_npr_node,         /* RCV_LOGO        */
3044 	lpfc_rcv_padisc_npr_node,       /* RCV_ADISC       */
3045 	lpfc_rcv_padisc_npr_node,       /* RCV_PDISC       */
3046 	lpfc_rcv_prlo_npr_node,         /* RCV_PRLO        */
3047 	lpfc_cmpl_plogi_npr_node,	/* CMPL_PLOGI      */
3048 	lpfc_cmpl_prli_npr_node,	/* CMPL_PRLI       */
3049 	lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
3050 	lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
3051 	lpfc_cmpl_reglogin_npr_node,    /* CMPL_REG_LOGIN  */
3052 	lpfc_device_rm_npr_node,        /* DEVICE_RM       */
3053 	lpfc_device_recov_npr_node,     /* DEVICE_RECOVERY */
3054 };
3055 
3056 int
lpfc_disc_state_machine(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,void * arg,uint32_t evt)3057 lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3058 			void *arg, uint32_t evt)
3059 {
3060 	uint32_t cur_state, rc;
3061 	uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
3062 			 uint32_t);
3063 	uint32_t got_ndlp = 0;
3064 	uint32_t data1;
3065 
3066 	if (lpfc_nlp_get(ndlp))
3067 		got_ndlp = 1;
3068 
3069 	cur_state = ndlp->nlp_state;
3070 
3071 	data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
3072 		((uint32_t)ndlp->nlp_type));
3073 	/* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
3074 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3075 			 "0211 DSM in event x%x on NPort x%x in "
3076 			 "state %d rpi x%x Data: x%x x%x\n",
3077 			 evt, ndlp->nlp_DID, cur_state, ndlp->nlp_rpi,
3078 			 ndlp->nlp_flag, data1);
3079 
3080 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
3081 		 "DSM in:          evt:%d ste:%d did:x%x",
3082 		evt, cur_state, ndlp->nlp_DID);
3083 
3084 	func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
3085 	rc = (func) (vport, ndlp, arg, evt);
3086 
3087 	/* DSM out state <rc> on NPort <nlp_DID> */
3088 	if (got_ndlp) {
3089 		data1 = (((uint32_t)ndlp->nlp_fc4_type << 16) |
3090 			((uint32_t)ndlp->nlp_type));
3091 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3092 			 "0212 DSM out state %d on NPort x%x "
3093 			 "rpi x%x Data: x%x x%x\n",
3094 			 rc, ndlp->nlp_DID, ndlp->nlp_rpi, ndlp->nlp_flag,
3095 			 data1);
3096 
3097 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
3098 			"DSM out:         ste:%d did:x%x flg:x%x",
3099 			rc, ndlp->nlp_DID, ndlp->nlp_flag);
3100 		/* Decrement the ndlp reference count held for this function */
3101 		lpfc_nlp_put(ndlp);
3102 	} else {
3103 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3104 			"0213 DSM out state %d on NPort free\n", rc);
3105 
3106 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
3107 			"DSM out:         ste:%d did:x%x flg:x%x",
3108 			rc, 0, 0);
3109 	}
3110 
3111 	return rc;
3112 }
3113