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