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