xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_hbadisc.c (revision 826f328e)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2020 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/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pci.h>
28 #include <linux/kthread.h>
29 #include <linux/interrupt.h>
30 #include <linux/lockdep.h>
31 #include <linux/utsname.h>
32 
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/fc/fc_fs.h>
38 
39 #include "lpfc_hw4.h"
40 #include "lpfc_hw.h"
41 #include "lpfc_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_sli.h"
44 #include "lpfc_sli4.h"
45 #include "lpfc.h"
46 #include "lpfc_scsi.h"
47 #include "lpfc_nvme.h"
48 #include "lpfc_logmsg.h"
49 #include "lpfc_crtn.h"
50 #include "lpfc_vport.h"
51 #include "lpfc_debugfs.h"
52 
53 /* AlpaArray for assignment of scsid for scan-down and bind_method */
54 static uint8_t lpfcAlpaArray[] = {
55 	0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
56 	0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
57 	0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
58 	0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
59 	0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
60 	0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
61 	0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
62 	0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
63 	0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
64 	0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
65 	0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
66 	0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
67 	0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
68 };
69 
70 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
71 static void lpfc_disc_flush_list(struct lpfc_vport *vport);
72 static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
73 static int lpfc_fcf_inuse(struct lpfc_hba *);
74 static void lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *, LPFC_MBOXQ_t *);
75 
76 /* The source of a terminate rport I/O is either a dev_loss_tmo
77  * event or a call to fc_remove_host.  While the rport should be
78  * valid during these downcalls, the transport can call twice
79  * in a single event.  This routine provides somoe protection
80  * as the NDLP isn't really free, just released to the pool.
81  */
82 static int
83 lpfc_rport_invalid(struct fc_rport *rport)
84 {
85 	struct lpfc_rport_data *rdata;
86 	struct lpfc_nodelist *ndlp;
87 
88 	if (!rport) {
89 		pr_err("**** %s: NULL rport, exit.\n", __func__);
90 		return -EINVAL;
91 	}
92 
93 	rdata = rport->dd_data;
94 	if (!rdata) {
95 		pr_err("**** %s: NULL dd_data on rport %p SID x%x\n",
96 		       __func__, rport, rport->scsi_target_id);
97 		return -EINVAL;
98 	}
99 
100 	ndlp = rdata->pnode;
101 	if (!rdata->pnode) {
102 		pr_err("**** %s: NULL ndlp on rport %p SID x%x\n",
103 		       __func__, rport, rport->scsi_target_id);
104 		return -EINVAL;
105 	}
106 
107 	if (!ndlp->vport) {
108 		pr_err("**** %s: Null vport on ndlp %p, DID x%x rport %p "
109 		       "SID x%x\n", __func__, ndlp, ndlp->nlp_DID, rport,
110 		       rport->scsi_target_id);
111 		return -EINVAL;
112 	}
113 	return 0;
114 }
115 
116 void
117 lpfc_terminate_rport_io(struct fc_rport *rport)
118 {
119 	struct lpfc_rport_data *rdata;
120 	struct lpfc_nodelist *ndlp;
121 	struct lpfc_vport *vport;
122 
123 	if (lpfc_rport_invalid(rport))
124 		return;
125 
126 	rdata = rport->dd_data;
127 	ndlp = rdata->pnode;
128 	vport = ndlp->vport;
129 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
130 			      "rport terminate: sid:x%x did:x%x flg:x%x",
131 			      ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
132 
133 	if (ndlp->nlp_sid != NLP_NO_SID) {
134 		lpfc_sli_abort_iocb(vport,
135 				    &vport->phba->sli.sli3_ring[LPFC_FCP_RING],
136 				    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
137 	}
138 }
139 
140 /*
141  * This function will be called when dev_loss_tmo fire.
142  */
143 void
144 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
145 {
146 	struct lpfc_nodelist *ndlp;
147 	struct lpfc_vport *vport;
148 	struct lpfc_hba   *phba;
149 	struct lpfc_work_evt *evtp;
150 	unsigned long iflags;
151 
152 	ndlp = ((struct lpfc_rport_data *)rport->dd_data)->pnode;
153 	if (!ndlp)
154 		return;
155 
156 	vport = ndlp->vport;
157 	phba  = vport->phba;
158 
159 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
160 		"rport devlosscb: sid:x%x did:x%x flg:x%x",
161 		ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
162 
163 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
164 			 "3181 dev_loss_callbk x%06x, rport %p flg x%x "
165 			 "load_flag x%x refcnt %d\n",
166 			 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag,
167 			 vport->load_flag, kref_read(&ndlp->kref));
168 
169 	/* Don't schedule a worker thread event if the vport is going down.
170 	 * The teardown process cleans up the node via lpfc_drop_node.
171 	 */
172 	if (vport->load_flag & FC_UNLOADING) {
173 		((struct lpfc_rport_data *)rport->dd_data)->pnode = NULL;
174 		ndlp->rport = NULL;
175 
176 		ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
177 
178 		/* Remove the node reference from remote_port_add now.
179 		 * The driver will not call remote_port_delete.
180 		 */
181 		lpfc_nlp_put(ndlp);
182 		return;
183 	}
184 
185 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
186 		return;
187 
188 	if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
189 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
190 				 "6789 rport name %llx != node port name %llx",
191 				 rport->port_name,
192 				 wwn_to_u64(ndlp->nlp_portname.u.wwn));
193 
194 	evtp = &ndlp->dev_loss_evt;
195 
196 	if (!list_empty(&evtp->evt_listp)) {
197 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
198 				 "6790 rport name %llx dev_loss_evt pending\n",
199 				 rport->port_name);
200 		return;
201 	}
202 
203 	spin_lock_irqsave(&ndlp->lock, iflags);
204 	ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
205 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
206 
207 	/*
208 	 * The backend does not expect any more calls associated with this
209 	 * rport. Remove the association between rport and ndlp.
210 	 */
211 	ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
212 	((struct lpfc_rport_data *)rport->dd_data)->pnode = NULL;
213 	ndlp->rport = NULL;
214 	spin_unlock_irqrestore(&ndlp->lock, iflags);
215 
216 	/* We need to hold the node by incrementing the reference
217 	 * count until this queued work is done
218 	 */
219 	evtp->evt_arg1 = lpfc_nlp_get(ndlp);
220 
221 	spin_lock_irqsave(&phba->hbalock, iflags);
222 	if (evtp->evt_arg1) {
223 		evtp->evt = LPFC_EVT_DEV_LOSS;
224 		list_add_tail(&evtp->evt_listp, &phba->work_list);
225 		lpfc_worker_wake_up(phba);
226 	}
227 	spin_unlock_irqrestore(&phba->hbalock, iflags);
228 
229 	return;
230 }
231 
232 /**
233  * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
234  * @ndlp: Pointer to remote node object.
235  *
236  * This function is called from the worker thread when devloss timeout timer
237  * expires. For SLI4 host, this routine shall return 1 when at lease one
238  * remote node, including this @ndlp, is still in use of FCF; otherwise, this
239  * routine shall return 0 when there is no remote node is still in use of FCF
240  * when devloss timeout happened to this @ndlp.
241  **/
242 static int
243 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
244 {
245 	struct lpfc_vport *vport;
246 	struct lpfc_hba   *phba;
247 	uint8_t *name;
248 	int warn_on = 0;
249 	int fcf_inuse = 0;
250 	unsigned long iflags;
251 
252 	vport = ndlp->vport;
253 	name = (uint8_t *)&ndlp->nlp_portname;
254 	phba = vport->phba;
255 
256 	spin_lock_irqsave(&ndlp->lock, iflags);
257 	ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
258 	spin_unlock_irqrestore(&ndlp->lock, iflags);
259 
260 	if (phba->sli_rev == LPFC_SLI_REV4)
261 		fcf_inuse = lpfc_fcf_inuse(phba);
262 
263 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
264 			      "rport devlosstmo:did:x%x type:x%x id:x%x",
265 			      ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_sid);
266 
267 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
268 			 "3182 %s x%06x, nflag x%x xflags x%x refcnt %d\n",
269 			 __func__, ndlp->nlp_DID, ndlp->nlp_flag,
270 			 ndlp->fc4_xpt_flags, kref_read(&ndlp->kref));
271 
272 	/* If the driver is recovering the rport, ignore devloss. */
273 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
274 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
275 				 "0284 Devloss timeout Ignored on "
276 				 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
277 				 "NPort x%x\n",
278 				 *name, *(name+1), *(name+2), *(name+3),
279 				 *(name+4), *(name+5), *(name+6), *(name+7),
280 				 ndlp->nlp_DID);
281 		return fcf_inuse;
282 	}
283 
284 	/* Fabric nodes are done. */
285 	if (ndlp->nlp_type & NLP_FABRIC) {
286 		lpfc_nlp_put(ndlp);
287 		return fcf_inuse;
288 	}
289 
290 	if (ndlp->nlp_sid != NLP_NO_SID) {
291 		warn_on = 1;
292 		lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
293 				    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
294 	}
295 
296 	if (warn_on) {
297 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
298 				 "0203 Devloss timeout on "
299 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
300 				 "NPort x%06x Data: x%x x%x x%x\n",
301 				 *name, *(name+1), *(name+2), *(name+3),
302 				 *(name+4), *(name+5), *(name+6), *(name+7),
303 				 ndlp->nlp_DID, ndlp->nlp_flag,
304 				 ndlp->nlp_state, ndlp->nlp_rpi);
305 	} else {
306 		lpfc_printf_vlog(vport, KERN_INFO, LOG_TRACE_EVENT,
307 				 "0204 Devloss timeout on "
308 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
309 				 "NPort x%06x Data: x%x x%x x%x\n",
310 				 *name, *(name+1), *(name+2), *(name+3),
311 				 *(name+4), *(name+5), *(name+6), *(name+7),
312 				 ndlp->nlp_DID, ndlp->nlp_flag,
313 				 ndlp->nlp_state, ndlp->nlp_rpi);
314 	}
315 
316 	if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD))
317 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
318 
319 	return fcf_inuse;
320 }
321 
322 /**
323  * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
324  * @phba: Pointer to hba context object.
325  * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
326  * @nlp_did: remote node identifer with devloss timeout.
327  *
328  * This function is called from the worker thread after invoking devloss
329  * timeout handler and releasing the reference count for the ndlp with
330  * which the devloss timeout was handled for SLI4 host. For the devloss
331  * timeout of the last remote node which had been in use of FCF, when this
332  * routine is invoked, it shall be guaranteed that none of the remote are
333  * in-use of FCF. When devloss timeout to the last remote using the FCF,
334  * if the FIP engine is neither in FCF table scan process nor roundrobin
335  * failover process, the in-use FCF shall be unregistered. If the FIP
336  * engine is in FCF discovery process, the devloss timeout state shall
337  * be set for either the FCF table scan process or roundrobin failover
338  * process to unregister the in-use FCF.
339  **/
340 static void
341 lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
342 				    uint32_t nlp_did)
343 {
344 	/* If devloss timeout happened to a remote node when FCF had no
345 	 * longer been in-use, do nothing.
346 	 */
347 	if (!fcf_inuse)
348 		return;
349 
350 	if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
351 		spin_lock_irq(&phba->hbalock);
352 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
353 			if (phba->hba_flag & HBA_DEVLOSS_TMO) {
354 				spin_unlock_irq(&phba->hbalock);
355 				return;
356 			}
357 			phba->hba_flag |= HBA_DEVLOSS_TMO;
358 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
359 					"2847 Last remote node (x%x) using "
360 					"FCF devloss tmo\n", nlp_did);
361 		}
362 		if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
363 			spin_unlock_irq(&phba->hbalock);
364 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
365 					"2868 Devloss tmo to FCF rediscovery "
366 					"in progress\n");
367 			return;
368 		}
369 		if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
370 			spin_unlock_irq(&phba->hbalock);
371 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
372 					"2869 Devloss tmo to idle FIP engine, "
373 					"unreg in-use FCF and rescan.\n");
374 			/* Unregister in-use FCF and rescan */
375 			lpfc_unregister_fcf_rescan(phba);
376 			return;
377 		}
378 		spin_unlock_irq(&phba->hbalock);
379 		if (phba->hba_flag & FCF_TS_INPROG)
380 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
381 					"2870 FCF table scan in progress\n");
382 		if (phba->hba_flag & FCF_RR_INPROG)
383 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
384 					"2871 FLOGI roundrobin FCF failover "
385 					"in progress\n");
386 	}
387 	lpfc_unregister_unused_fcf(phba);
388 }
389 
390 /**
391  * lpfc_alloc_fast_evt - Allocates data structure for posting event
392  * @phba: Pointer to hba context object.
393  *
394  * This function is called from the functions which need to post
395  * events from interrupt context. This function allocates data
396  * structure required for posting event. It also keeps track of
397  * number of events pending and prevent event storm when there are
398  * too many events.
399  **/
400 struct lpfc_fast_path_event *
401 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
402 	struct lpfc_fast_path_event *ret;
403 
404 	/* If there are lot of fast event do not exhaust memory due to this */
405 	if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
406 		return NULL;
407 
408 	ret = kzalloc(sizeof(struct lpfc_fast_path_event),
409 			GFP_ATOMIC);
410 	if (ret) {
411 		atomic_inc(&phba->fast_event_count);
412 		INIT_LIST_HEAD(&ret->work_evt.evt_listp);
413 		ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
414 	}
415 	return ret;
416 }
417 
418 /**
419  * lpfc_free_fast_evt - Frees event data structure
420  * @phba: Pointer to hba context object.
421  * @evt:  Event object which need to be freed.
422  *
423  * This function frees the data structure required for posting
424  * events.
425  **/
426 void
427 lpfc_free_fast_evt(struct lpfc_hba *phba,
428 		struct lpfc_fast_path_event *evt) {
429 
430 	atomic_dec(&phba->fast_event_count);
431 	kfree(evt);
432 }
433 
434 /**
435  * lpfc_send_fastpath_evt - Posts events generated from fast path
436  * @phba: Pointer to hba context object.
437  * @evtp: Event data structure.
438  *
439  * This function is called from worker thread, when the interrupt
440  * context need to post an event. This function posts the event
441  * to fc transport netlink interface.
442  **/
443 static void
444 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
445 		struct lpfc_work_evt *evtp)
446 {
447 	unsigned long evt_category, evt_sub_category;
448 	struct lpfc_fast_path_event *fast_evt_data;
449 	char *evt_data;
450 	uint32_t evt_data_size;
451 	struct Scsi_Host *shost;
452 
453 	fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
454 		work_evt);
455 
456 	evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
457 	evt_sub_category = (unsigned long) fast_evt_data->un.
458 			fabric_evt.subcategory;
459 	shost = lpfc_shost_from_vport(fast_evt_data->vport);
460 	if (evt_category == FC_REG_FABRIC_EVENT) {
461 		if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
462 			evt_data = (char *) &fast_evt_data->un.read_check_error;
463 			evt_data_size = sizeof(fast_evt_data->un.
464 				read_check_error);
465 		} else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
466 			(evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
467 			evt_data = (char *) &fast_evt_data->un.fabric_evt;
468 			evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
469 		} else {
470 			lpfc_free_fast_evt(phba, fast_evt_data);
471 			return;
472 		}
473 	} else if (evt_category == FC_REG_SCSI_EVENT) {
474 		switch (evt_sub_category) {
475 		case LPFC_EVENT_QFULL:
476 		case LPFC_EVENT_DEVBSY:
477 			evt_data = (char *) &fast_evt_data->un.scsi_evt;
478 			evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
479 			break;
480 		case LPFC_EVENT_CHECK_COND:
481 			evt_data = (char *) &fast_evt_data->un.check_cond_evt;
482 			evt_data_size =  sizeof(fast_evt_data->un.
483 				check_cond_evt);
484 			break;
485 		case LPFC_EVENT_VARQUEDEPTH:
486 			evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
487 			evt_data_size = sizeof(fast_evt_data->un.
488 				queue_depth_evt);
489 			break;
490 		default:
491 			lpfc_free_fast_evt(phba, fast_evt_data);
492 			return;
493 		}
494 	} else {
495 		lpfc_free_fast_evt(phba, fast_evt_data);
496 		return;
497 	}
498 
499 	if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
500 		fc_host_post_vendor_event(shost,
501 			fc_get_event_number(),
502 			evt_data_size,
503 			evt_data,
504 			LPFC_NL_VENDOR_ID);
505 
506 	lpfc_free_fast_evt(phba, fast_evt_data);
507 	return;
508 }
509 
510 static void
511 lpfc_work_list_done(struct lpfc_hba *phba)
512 {
513 	struct lpfc_work_evt  *evtp = NULL;
514 	struct lpfc_nodelist  *ndlp;
515 	int free_evt;
516 	int fcf_inuse;
517 	uint32_t nlp_did;
518 
519 	spin_lock_irq(&phba->hbalock);
520 	while (!list_empty(&phba->work_list)) {
521 		list_remove_head((&phba->work_list), evtp, typeof(*evtp),
522 				 evt_listp);
523 		spin_unlock_irq(&phba->hbalock);
524 		free_evt = 1;
525 		switch (evtp->evt) {
526 		case LPFC_EVT_ELS_RETRY:
527 			ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
528 			lpfc_els_retry_delay_handler(ndlp);
529 			free_evt = 0; /* evt is part of ndlp */
530 			/* decrement the node reference count held
531 			 * for this queued work
532 			 */
533 			lpfc_nlp_put(ndlp);
534 			break;
535 		case LPFC_EVT_DEV_LOSS:
536 			ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
537 			fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
538 			free_evt = 0;
539 			/* decrement the node reference count held for
540 			 * this queued work
541 			 */
542 			nlp_did = ndlp->nlp_DID;
543 			lpfc_nlp_put(ndlp);
544 			if (phba->sli_rev == LPFC_SLI_REV4)
545 				lpfc_sli4_post_dev_loss_tmo_handler(phba,
546 								    fcf_inuse,
547 								    nlp_did);
548 			break;
549 		case LPFC_EVT_RECOVER_PORT:
550 			ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
551 			lpfc_sli_abts_recover_port(ndlp->vport, ndlp);
552 			free_evt = 0;
553 			/* decrement the node reference count held for
554 			 * this queued work
555 			 */
556 			lpfc_nlp_put(ndlp);
557 			break;
558 		case LPFC_EVT_ONLINE:
559 			if (phba->link_state < LPFC_LINK_DOWN)
560 				*(int *) (evtp->evt_arg1) = lpfc_online(phba);
561 			else
562 				*(int *) (evtp->evt_arg1) = 0;
563 			complete((struct completion *)(evtp->evt_arg2));
564 			break;
565 		case LPFC_EVT_OFFLINE_PREP:
566 			if (phba->link_state >= LPFC_LINK_DOWN)
567 				lpfc_offline_prep(phba, LPFC_MBX_WAIT);
568 			*(int *)(evtp->evt_arg1) = 0;
569 			complete((struct completion *)(evtp->evt_arg2));
570 			break;
571 		case LPFC_EVT_OFFLINE:
572 			lpfc_offline(phba);
573 			lpfc_sli_brdrestart(phba);
574 			*(int *)(evtp->evt_arg1) =
575 				lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
576 			lpfc_unblock_mgmt_io(phba);
577 			complete((struct completion *)(evtp->evt_arg2));
578 			break;
579 		case LPFC_EVT_WARM_START:
580 			lpfc_offline(phba);
581 			lpfc_reset_barrier(phba);
582 			lpfc_sli_brdreset(phba);
583 			lpfc_hba_down_post(phba);
584 			*(int *)(evtp->evt_arg1) =
585 				lpfc_sli_brdready(phba, HS_MBRDY);
586 			lpfc_unblock_mgmt_io(phba);
587 			complete((struct completion *)(evtp->evt_arg2));
588 			break;
589 		case LPFC_EVT_KILL:
590 			lpfc_offline(phba);
591 			*(int *)(evtp->evt_arg1)
592 				= (phba->pport->stopped)
593 				        ? 0 : lpfc_sli_brdkill(phba);
594 			lpfc_unblock_mgmt_io(phba);
595 			complete((struct completion *)(evtp->evt_arg2));
596 			break;
597 		case LPFC_EVT_FASTPATH_MGMT_EVT:
598 			lpfc_send_fastpath_evt(phba, evtp);
599 			free_evt = 0;
600 			break;
601 		case LPFC_EVT_RESET_HBA:
602 			if (!(phba->pport->load_flag & FC_UNLOADING))
603 				lpfc_reset_hba(phba);
604 			break;
605 		}
606 		if (free_evt)
607 			kfree(evtp);
608 		spin_lock_irq(&phba->hbalock);
609 	}
610 	spin_unlock_irq(&phba->hbalock);
611 
612 }
613 
614 static void
615 lpfc_work_done(struct lpfc_hba *phba)
616 {
617 	struct lpfc_sli_ring *pring;
618 	uint32_t ha_copy, status, control, work_port_events;
619 	struct lpfc_vport **vports;
620 	struct lpfc_vport *vport;
621 	int i;
622 
623 	spin_lock_irq(&phba->hbalock);
624 	ha_copy = phba->work_ha;
625 	phba->work_ha = 0;
626 	spin_unlock_irq(&phba->hbalock);
627 
628 	/* First, try to post the next mailbox command to SLI4 device */
629 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
630 		lpfc_sli4_post_async_mbox(phba);
631 
632 	if (ha_copy & HA_ERATT)
633 		/* Handle the error attention event */
634 		lpfc_handle_eratt(phba);
635 
636 	if (ha_copy & HA_MBATT)
637 		lpfc_sli_handle_mb_event(phba);
638 
639 	if (ha_copy & HA_LATT)
640 		lpfc_handle_latt(phba);
641 
642 	/* Process SLI4 events */
643 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
644 		if (phba->hba_flag & HBA_RRQ_ACTIVE)
645 			lpfc_handle_rrq_active(phba);
646 		if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
647 			lpfc_sli4_els_xri_abort_event_proc(phba);
648 		if (phba->hba_flag & ASYNC_EVENT)
649 			lpfc_sli4_async_event_proc(phba);
650 		if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
651 			spin_lock_irq(&phba->hbalock);
652 			phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
653 			spin_unlock_irq(&phba->hbalock);
654 			lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
655 		}
656 		if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
657 			lpfc_sli4_fcf_redisc_event_proc(phba);
658 	}
659 
660 	vports = lpfc_create_vport_work_array(phba);
661 	if (vports != NULL)
662 		for (i = 0; i <= phba->max_vports; i++) {
663 			/*
664 			 * We could have no vports in array if unloading, so if
665 			 * this happens then just use the pport
666 			 */
667 			if (vports[i] == NULL && i == 0)
668 				vport = phba->pport;
669 			else
670 				vport = vports[i];
671 			if (vport == NULL)
672 				break;
673 			spin_lock_irq(&vport->work_port_lock);
674 			work_port_events = vport->work_port_events;
675 			vport->work_port_events &= ~work_port_events;
676 			spin_unlock_irq(&vport->work_port_lock);
677 			if (work_port_events & WORKER_DISC_TMO)
678 				lpfc_disc_timeout_handler(vport);
679 			if (work_port_events & WORKER_ELS_TMO)
680 				lpfc_els_timeout_handler(vport);
681 			if (work_port_events & WORKER_HB_TMO)
682 				lpfc_hb_timeout_handler(phba);
683 			if (work_port_events & WORKER_MBOX_TMO)
684 				lpfc_mbox_timeout_handler(phba);
685 			if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
686 				lpfc_unblock_fabric_iocbs(phba);
687 			if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
688 				lpfc_ramp_down_queue_handler(phba);
689 			if (work_port_events & WORKER_DELAYED_DISC_TMO)
690 				lpfc_delayed_disc_timeout_handler(vport);
691 		}
692 	lpfc_destroy_vport_work_array(phba, vports);
693 
694 	pring = lpfc_phba_elsring(phba);
695 	status = (ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
696 	status >>= (4*LPFC_ELS_RING);
697 	if (pring && (status & HA_RXMASK ||
698 		      pring->flag & LPFC_DEFERRED_RING_EVENT ||
699 		      phba->hba_flag & HBA_SP_QUEUE_EVT)) {
700 		if (pring->flag & LPFC_STOP_IOCB_EVENT) {
701 			pring->flag |= LPFC_DEFERRED_RING_EVENT;
702 			/* Preserve legacy behavior. */
703 			if (!(phba->hba_flag & HBA_SP_QUEUE_EVT))
704 				set_bit(LPFC_DATA_READY, &phba->data_flags);
705 		} else {
706 			/* Driver could have abort request completed in queue
707 			 * when link goes down.  Allow for this transition.
708 			 */
709 			if (phba->link_state >= LPFC_LINK_DOWN ||
710 			    phba->link_flag & LS_MDS_LOOPBACK) {
711 				pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
712 				lpfc_sli_handle_slow_ring_event(phba, pring,
713 								(status &
714 								HA_RXMASK));
715 			}
716 		}
717 		if (phba->sli_rev == LPFC_SLI_REV4)
718 			lpfc_drain_txq(phba);
719 		/*
720 		 * Turn on Ring interrupts
721 		 */
722 		if (phba->sli_rev <= LPFC_SLI_REV3) {
723 			spin_lock_irq(&phba->hbalock);
724 			control = readl(phba->HCregaddr);
725 			if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
726 				lpfc_debugfs_slow_ring_trc(phba,
727 					"WRK Enable ring: cntl:x%x hacopy:x%x",
728 					control, ha_copy, 0);
729 
730 				control |= (HC_R0INT_ENA << LPFC_ELS_RING);
731 				writel(control, phba->HCregaddr);
732 				readl(phba->HCregaddr); /* flush */
733 			} else {
734 				lpfc_debugfs_slow_ring_trc(phba,
735 					"WRK Ring ok:     cntl:x%x hacopy:x%x",
736 					control, ha_copy, 0);
737 			}
738 			spin_unlock_irq(&phba->hbalock);
739 		}
740 	}
741 	lpfc_work_list_done(phba);
742 }
743 
744 int
745 lpfc_do_work(void *p)
746 {
747 	struct lpfc_hba *phba = p;
748 	int rc;
749 
750 	set_user_nice(current, MIN_NICE);
751 	current->flags |= PF_NOFREEZE;
752 	phba->data_flags = 0;
753 
754 	while (!kthread_should_stop()) {
755 		/* wait and check worker queue activities */
756 		rc = wait_event_interruptible(phba->work_waitq,
757 					(test_and_clear_bit(LPFC_DATA_READY,
758 							    &phba->data_flags)
759 					 || kthread_should_stop()));
760 		/* Signal wakeup shall terminate the worker thread */
761 		if (rc) {
762 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
763 					"0433 Wakeup on signal: rc=x%x\n", rc);
764 			break;
765 		}
766 
767 		/* Attend pending lpfc data processing */
768 		lpfc_work_done(phba);
769 	}
770 	phba->worker_thread = NULL;
771 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
772 			"0432 Worker thread stopped.\n");
773 	return 0;
774 }
775 
776 /*
777  * This is only called to handle FC worker events. Since this a rare
778  * occurrence, we allocate a struct lpfc_work_evt structure here instead of
779  * embedding it in the IOCB.
780  */
781 int
782 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
783 		      uint32_t evt)
784 {
785 	struct lpfc_work_evt  *evtp;
786 	unsigned long flags;
787 
788 	/*
789 	 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
790 	 * be queued to worker thread for processing
791 	 */
792 	evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
793 	if (!evtp)
794 		return 0;
795 
796 	evtp->evt_arg1  = arg1;
797 	evtp->evt_arg2  = arg2;
798 	evtp->evt       = evt;
799 
800 	spin_lock_irqsave(&phba->hbalock, flags);
801 	list_add_tail(&evtp->evt_listp, &phba->work_list);
802 	spin_unlock_irqrestore(&phba->hbalock, flags);
803 
804 	lpfc_worker_wake_up(phba);
805 
806 	return 1;
807 }
808 
809 void
810 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
811 {
812 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
813 	struct lpfc_hba  *phba = vport->phba;
814 	struct lpfc_nodelist *ndlp, *next_ndlp;
815 
816 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
817 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
818 			continue;
819 
820 		if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
821 		    ((vport->port_type == LPFC_NPIV_PORT) &&
822 		     ((ndlp->nlp_DID == NameServer_DID) ||
823 		      (ndlp->nlp_DID == FDMI_DID))))
824 			lpfc_unreg_rpi(vport, ndlp);
825 
826 		/* Leave Fabric nodes alone on link down */
827 		if ((phba->sli_rev < LPFC_SLI_REV4) &&
828 		    (!remove && ndlp->nlp_type & NLP_FABRIC))
829 			continue;
830 
831 		/* Notify transport of connectivity loss to trigger cleanup. */
832 		if (phba->nvmet_support &&
833 		    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
834 			lpfc_nvmet_invalidate_host(phba, ndlp);
835 
836 		lpfc_disc_state_machine(vport, ndlp, NULL,
837 					remove
838 					? NLP_EVT_DEVICE_RM
839 					: NLP_EVT_DEVICE_RECOVERY);
840 	}
841 	if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
842 		if (phba->sli_rev == LPFC_SLI_REV4)
843 			lpfc_sli4_unreg_all_rpis(vport);
844 		lpfc_mbx_unreg_vpi(vport);
845 		spin_lock_irq(shost->host_lock);
846 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
847 		spin_unlock_irq(shost->host_lock);
848 	}
849 }
850 
851 void
852 lpfc_port_link_failure(struct lpfc_vport *vport)
853 {
854 	lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
855 
856 	/* Cleanup any outstanding received buffers */
857 	lpfc_cleanup_rcv_buffers(vport);
858 
859 	/* Cleanup any outstanding RSCN activity */
860 	lpfc_els_flush_rscn(vport);
861 
862 	/* Cleanup any outstanding ELS commands */
863 	lpfc_els_flush_cmd(vport);
864 
865 	lpfc_cleanup_rpis(vport, 0);
866 
867 	/* Turn off discovery timer if its running */
868 	lpfc_can_disctmo(vport);
869 }
870 
871 void
872 lpfc_linkdown_port(struct lpfc_vport *vport)
873 {
874 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
875 
876 	if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
877 		fc_host_post_event(shost, fc_get_event_number(),
878 				   FCH_EVT_LINKDOWN, 0);
879 
880 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
881 		"Link Down:       state:x%x rtry:x%x flg:x%x",
882 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
883 
884 	lpfc_port_link_failure(vport);
885 
886 	/* Stop delayed Nport discovery */
887 	spin_lock_irq(shost->host_lock);
888 	vport->fc_flag &= ~FC_DISC_DELAYED;
889 	spin_unlock_irq(shost->host_lock);
890 	del_timer_sync(&vport->delayed_disc_tmo);
891 }
892 
893 int
894 lpfc_linkdown(struct lpfc_hba *phba)
895 {
896 	struct lpfc_vport *vport = phba->pport;
897 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
898 	struct lpfc_vport **vports;
899 	LPFC_MBOXQ_t          *mb;
900 	int i;
901 
902 	if (phba->link_state == LPFC_LINK_DOWN)
903 		return 0;
904 
905 	/* Block all SCSI stack I/Os */
906 	lpfc_scsi_dev_block(phba);
907 
908 	phba->defer_flogi_acc_flag = false;
909 
910 	spin_lock_irq(&phba->hbalock);
911 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
912 	spin_unlock_irq(&phba->hbalock);
913 	if (phba->link_state > LPFC_LINK_DOWN) {
914 		phba->link_state = LPFC_LINK_DOWN;
915 		if (phba->sli4_hba.conf_trunk) {
916 			phba->trunk_link.link0.state = 0;
917 			phba->trunk_link.link1.state = 0;
918 			phba->trunk_link.link2.state = 0;
919 			phba->trunk_link.link3.state = 0;
920 			phba->sli4_hba.link_state.logical_speed =
921 						LPFC_LINK_SPEED_UNKNOWN;
922 		}
923 		spin_lock_irq(shost->host_lock);
924 		phba->pport->fc_flag &= ~FC_LBIT;
925 		spin_unlock_irq(shost->host_lock);
926 	}
927 	vports = lpfc_create_vport_work_array(phba);
928 	if (vports != NULL) {
929 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
930 			/* Issue a LINK DOWN event to all nodes */
931 			lpfc_linkdown_port(vports[i]);
932 
933 			vports[i]->fc_myDID = 0;
934 
935 			if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
936 			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
937 				if (phba->nvmet_support)
938 					lpfc_nvmet_update_targetport(phba);
939 				else
940 					lpfc_nvme_update_localport(vports[i]);
941 			}
942 		}
943 	}
944 	lpfc_destroy_vport_work_array(phba, vports);
945 
946 	/* Clean up any SLI3 firmware default rpi's */
947 	if (phba->sli_rev > LPFC_SLI_REV3)
948 		goto skip_unreg_did;
949 
950 	mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
951 	if (mb) {
952 		lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
953 		mb->vport = vport;
954 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
955 		if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
956 		    == MBX_NOT_FINISHED) {
957 			mempool_free(mb, phba->mbox_mem_pool);
958 		}
959 	}
960 
961  skip_unreg_did:
962 	/* Setup myDID for link up if we are in pt2pt mode */
963 	if (phba->pport->fc_flag & FC_PT2PT) {
964 		mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
965 		if (mb) {
966 			lpfc_config_link(phba, mb);
967 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
968 			mb->vport = vport;
969 			if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
970 			    == MBX_NOT_FINISHED) {
971 				mempool_free(mb, phba->mbox_mem_pool);
972 			}
973 		}
974 		spin_lock_irq(shost->host_lock);
975 		phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
976 		phba->pport->rcv_flogi_cnt = 0;
977 		spin_unlock_irq(shost->host_lock);
978 	}
979 	return 0;
980 }
981 
982 static void
983 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
984 {
985 	struct lpfc_nodelist *ndlp;
986 
987 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
988 		ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
989 
990 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
991 			continue;
992 		if (ndlp->nlp_type & NLP_FABRIC) {
993 			/* On Linkup its safe to clean up the ndlp
994 			 * from Fabric connections.
995 			 */
996 			if (ndlp->nlp_DID != Fabric_DID)
997 				lpfc_unreg_rpi(vport, ndlp);
998 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
999 		} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
1000 			/* Fail outstanding IO now since device is
1001 			 * marked for PLOGI.
1002 			 */
1003 			lpfc_unreg_rpi(vport, ndlp);
1004 		}
1005 	}
1006 }
1007 
1008 static void
1009 lpfc_linkup_port(struct lpfc_vport *vport)
1010 {
1011 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1012 	struct lpfc_hba  *phba = vport->phba;
1013 
1014 	if ((vport->load_flag & FC_UNLOADING) != 0)
1015 		return;
1016 
1017 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1018 		"Link Up:         top:x%x speed:x%x flg:x%x",
1019 		phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
1020 
1021 	/* If NPIV is not enabled, only bring the physical port up */
1022 	if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1023 		(vport != phba->pport))
1024 		return;
1025 
1026 	if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1027 		fc_host_post_event(shost, fc_get_event_number(),
1028 				   FCH_EVT_LINKUP, 0);
1029 
1030 	spin_lock_irq(shost->host_lock);
1031 	vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1032 			    FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1033 	vport->fc_flag |= FC_NDISC_ACTIVE;
1034 	vport->fc_ns_retry = 0;
1035 	spin_unlock_irq(shost->host_lock);
1036 
1037 	lpfc_linkup_cleanup_nodes(vport);
1038 }
1039 
1040 static int
1041 lpfc_linkup(struct lpfc_hba *phba)
1042 {
1043 	struct lpfc_vport **vports;
1044 	int i;
1045 	struct Scsi_Host  *shost = lpfc_shost_from_vport(phba->pport);
1046 
1047 	phba->link_state = LPFC_LINK_UP;
1048 
1049 	/* Unblock fabric iocbs if they are blocked */
1050 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1051 	del_timer_sync(&phba->fabric_block_timer);
1052 
1053 	vports = lpfc_create_vport_work_array(phba);
1054 	if (vports != NULL)
1055 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1056 			lpfc_linkup_port(vports[i]);
1057 	lpfc_destroy_vport_work_array(phba, vports);
1058 
1059 	/* Clear the pport flogi counter in case the link down was
1060 	 * absorbed without an ACQE. No lock here - in worker thread
1061 	 * and discovery is synchronized.
1062 	 */
1063 	spin_lock_irq(shost->host_lock);
1064 	phba->pport->rcv_flogi_cnt = 0;
1065 	spin_unlock_irq(shost->host_lock);
1066 
1067 	/* reinitialize initial FLOGI flag */
1068 	phba->hba_flag &= ~(HBA_FLOGI_ISSUED);
1069 	phba->defer_flogi_acc_flag = false;
1070 
1071 	return 0;
1072 }
1073 
1074 /*
1075  * This routine handles processing a CLEAR_LA mailbox
1076  * command upon completion. It is setup in the LPFC_MBOXQ
1077  * as the completion routine when the command is
1078  * handed off to the SLI layer. SLI3 only.
1079  */
1080 static void
1081 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1082 {
1083 	struct lpfc_vport *vport = pmb->vport;
1084 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1085 	struct lpfc_sli   *psli = &phba->sli;
1086 	MAILBOX_t *mb = &pmb->u.mb;
1087 	uint32_t control;
1088 
1089 	/* Since we don't do discovery right now, turn these off here */
1090 	psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1091 	psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1092 
1093 	/* Check for error */
1094 	if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
1095 		/* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
1096 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1097 				 "0320 CLEAR_LA mbxStatus error x%x hba "
1098 				 "state x%x\n",
1099 				 mb->mbxStatus, vport->port_state);
1100 		phba->link_state = LPFC_HBA_ERROR;
1101 		goto out;
1102 	}
1103 
1104 	if (vport->port_type == LPFC_PHYSICAL_PORT)
1105 		phba->link_state = LPFC_HBA_READY;
1106 
1107 	spin_lock_irq(&phba->hbalock);
1108 	psli->sli_flag |= LPFC_PROCESS_LA;
1109 	control = readl(phba->HCregaddr);
1110 	control |= HC_LAINT_ENA;
1111 	writel(control, phba->HCregaddr);
1112 	readl(phba->HCregaddr); /* flush */
1113 	spin_unlock_irq(&phba->hbalock);
1114 	mempool_free(pmb, phba->mbox_mem_pool);
1115 	return;
1116 
1117 out:
1118 	/* Device Discovery completes */
1119 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1120 			 "0225 Device Discovery completes\n");
1121 	mempool_free(pmb, phba->mbox_mem_pool);
1122 
1123 	spin_lock_irq(shost->host_lock);
1124 	vport->fc_flag &= ~FC_ABORT_DISCOVERY;
1125 	spin_unlock_irq(shost->host_lock);
1126 
1127 	lpfc_can_disctmo(vport);
1128 
1129 	/* turn on Link Attention interrupts */
1130 
1131 	spin_lock_irq(&phba->hbalock);
1132 	psli->sli_flag |= LPFC_PROCESS_LA;
1133 	control = readl(phba->HCregaddr);
1134 	control |= HC_LAINT_ENA;
1135 	writel(control, phba->HCregaddr);
1136 	readl(phba->HCregaddr); /* flush */
1137 	spin_unlock_irq(&phba->hbalock);
1138 
1139 	return;
1140 }
1141 
1142 void
1143 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1144 {
1145 	struct lpfc_vport *vport = pmb->vport;
1146 	LPFC_MBOXQ_t *sparam_mb;
1147 	struct lpfc_dmabuf *sparam_mp;
1148 	int rc;
1149 
1150 	if (pmb->u.mb.mbxStatus)
1151 		goto out;
1152 
1153 	mempool_free(pmb, phba->mbox_mem_pool);
1154 
1155 	/* don't perform discovery for SLI4 loopback diagnostic test */
1156 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
1157 	    !(phba->hba_flag & HBA_FCOE_MODE) &&
1158 	    (phba->link_flag & LS_LOOPBACK_MODE))
1159 		return;
1160 
1161 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
1162 	    vport->fc_flag & FC_PUBLIC_LOOP &&
1163 	    !(vport->fc_flag & FC_LBIT)) {
1164 			/* Need to wait for FAN - use discovery timer
1165 			 * for timeout.  port_state is identically
1166 			 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1167 			 */
1168 			lpfc_set_disctmo(vport);
1169 			return;
1170 	}
1171 
1172 	/* Start discovery by sending a FLOGI. port_state is identically
1173 	 * LPFC_FLOGI while waiting for FLOGI cmpl.
1174 	 */
1175 	if (vport->port_state != LPFC_FLOGI) {
1176 		/* Issue MBX_READ_SPARAM to update CSPs before FLOGI if
1177 		 * bb-credit recovery is in place.
1178 		 */
1179 		if (phba->bbcredit_support && phba->cfg_enable_bbcr &&
1180 		    !(phba->link_flag & LS_LOOPBACK_MODE)) {
1181 			sparam_mb = mempool_alloc(phba->mbox_mem_pool,
1182 						  GFP_KERNEL);
1183 			if (!sparam_mb)
1184 				goto sparam_out;
1185 
1186 			rc = lpfc_read_sparam(phba, sparam_mb, 0);
1187 			if (rc) {
1188 				mempool_free(sparam_mb, phba->mbox_mem_pool);
1189 				goto sparam_out;
1190 			}
1191 			sparam_mb->vport = vport;
1192 			sparam_mb->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
1193 			rc = lpfc_sli_issue_mbox(phba, sparam_mb, MBX_NOWAIT);
1194 			if (rc == MBX_NOT_FINISHED) {
1195 				sparam_mp = (struct lpfc_dmabuf *)
1196 						sparam_mb->ctx_buf;
1197 				lpfc_mbuf_free(phba, sparam_mp->virt,
1198 					       sparam_mp->phys);
1199 				kfree(sparam_mp);
1200 				sparam_mb->ctx_buf = NULL;
1201 				mempool_free(sparam_mb, phba->mbox_mem_pool);
1202 				goto sparam_out;
1203 			}
1204 
1205 			phba->hba_flag |= HBA_DEFER_FLOGI;
1206 		}  else {
1207 			lpfc_initial_flogi(vport);
1208 		}
1209 	} else {
1210 		if (vport->fc_flag & FC_PT2PT)
1211 			lpfc_disc_start(vport);
1212 	}
1213 	return;
1214 
1215 out:
1216 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1217 			 "0306 CONFIG_LINK mbxStatus error x%x "
1218 			 "HBA state x%x\n",
1219 			 pmb->u.mb.mbxStatus, vport->port_state);
1220 sparam_out:
1221 	mempool_free(pmb, phba->mbox_mem_pool);
1222 
1223 	lpfc_linkdown(phba);
1224 
1225 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1226 			 "0200 CONFIG_LINK bad hba state x%x\n",
1227 			 vport->port_state);
1228 
1229 	lpfc_issue_clear_la(phba, vport);
1230 	return;
1231 }
1232 
1233 /**
1234  * lpfc_sli4_clear_fcf_rr_bmask
1235  * @phba: pointer to the struct lpfc_hba for this port.
1236  * This fucnction resets the round robin bit mask and clears the
1237  * fcf priority list. The list deletions are done while holding the
1238  * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1239  * from the lpfc_fcf_pri record.
1240  **/
1241 void
1242 lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1243 {
1244 	struct lpfc_fcf_pri *fcf_pri;
1245 	struct lpfc_fcf_pri *next_fcf_pri;
1246 	memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1247 	spin_lock_irq(&phba->hbalock);
1248 	list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1249 				&phba->fcf.fcf_pri_list, list) {
1250 		list_del_init(&fcf_pri->list);
1251 		fcf_pri->fcf_rec.flag = 0;
1252 	}
1253 	spin_unlock_irq(&phba->hbalock);
1254 }
1255 static void
1256 lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1257 {
1258 	struct lpfc_vport *vport = mboxq->vport;
1259 
1260 	if (mboxq->u.mb.mbxStatus) {
1261 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1262 				 "2017 REG_FCFI mbxStatus error x%x "
1263 				 "HBA state x%x\n", mboxq->u.mb.mbxStatus,
1264 				 vport->port_state);
1265 		goto fail_out;
1266 	}
1267 
1268 	/* Start FCoE discovery by sending a FLOGI. */
1269 	phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1270 	/* Set the FCFI registered flag */
1271 	spin_lock_irq(&phba->hbalock);
1272 	phba->fcf.fcf_flag |= FCF_REGISTERED;
1273 	spin_unlock_irq(&phba->hbalock);
1274 
1275 	/* If there is a pending FCoE event, restart FCF table scan. */
1276 	if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1277 		lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
1278 		goto fail_out;
1279 
1280 	/* Mark successful completion of FCF table scan */
1281 	spin_lock_irq(&phba->hbalock);
1282 	phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1283 	phba->hba_flag &= ~FCF_TS_INPROG;
1284 	if (vport->port_state != LPFC_FLOGI) {
1285 		phba->hba_flag |= FCF_RR_INPROG;
1286 		spin_unlock_irq(&phba->hbalock);
1287 		lpfc_issue_init_vfi(vport);
1288 		goto out;
1289 	}
1290 	spin_unlock_irq(&phba->hbalock);
1291 	goto out;
1292 
1293 fail_out:
1294 	spin_lock_irq(&phba->hbalock);
1295 	phba->hba_flag &= ~FCF_RR_INPROG;
1296 	spin_unlock_irq(&phba->hbalock);
1297 out:
1298 	mempool_free(mboxq, phba->mbox_mem_pool);
1299 }
1300 
1301 /**
1302  * lpfc_fab_name_match - Check if the fcf fabric name match.
1303  * @fab_name: pointer to fabric name.
1304  * @new_fcf_record: pointer to fcf record.
1305  *
1306  * This routine compare the fcf record's fabric name with provided
1307  * fabric name. If the fabric name are identical this function
1308  * returns 1 else return 0.
1309  **/
1310 static uint32_t
1311 lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1312 {
1313 	if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
1314 		return 0;
1315 	if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1316 		return 0;
1317 	if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1318 		return 0;
1319 	if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1320 		return 0;
1321 	if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1322 		return 0;
1323 	if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1324 		return 0;
1325 	if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1326 		return 0;
1327 	if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1328 		return 0;
1329 	return 1;
1330 }
1331 
1332 /**
1333  * lpfc_sw_name_match - Check if the fcf switch name match.
1334  * @sw_name: pointer to switch name.
1335  * @new_fcf_record: pointer to fcf record.
1336  *
1337  * This routine compare the fcf record's switch name with provided
1338  * switch name. If the switch name are identical this function
1339  * returns 1 else return 0.
1340  **/
1341 static uint32_t
1342 lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1343 {
1344 	if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
1345 		return 0;
1346 	if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1347 		return 0;
1348 	if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1349 		return 0;
1350 	if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1351 		return 0;
1352 	if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1353 		return 0;
1354 	if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1355 		return 0;
1356 	if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1357 		return 0;
1358 	if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1359 		return 0;
1360 	return 1;
1361 }
1362 
1363 /**
1364  * lpfc_mac_addr_match - Check if the fcf mac address match.
1365  * @mac_addr: pointer to mac address.
1366  * @new_fcf_record: pointer to fcf record.
1367  *
1368  * This routine compare the fcf record's mac address with HBA's
1369  * FCF mac address. If the mac addresses are identical this function
1370  * returns 1 else return 0.
1371  **/
1372 static uint32_t
1373 lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
1374 {
1375 	if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
1376 		return 0;
1377 	if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1378 		return 0;
1379 	if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1380 		return 0;
1381 	if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1382 		return 0;
1383 	if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1384 		return 0;
1385 	if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1386 		return 0;
1387 	return 1;
1388 }
1389 
1390 static bool
1391 lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1392 {
1393 	return (curr_vlan_id == new_vlan_id);
1394 }
1395 
1396 /**
1397  * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1398  * @phba: pointer to lpfc hba data structure.
1399  * @fcf_index: Index for the lpfc_fcf_record.
1400  * @new_fcf_record: pointer to hba fcf record.
1401  *
1402  * This routine updates the driver FCF priority record from the new HBA FCF
1403  * record. The hbalock is asserted held in the code path calling this
1404  * routine.
1405  **/
1406 static void
1407 __lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1408 				 struct fcf_record *new_fcf_record
1409 				 )
1410 {
1411 	struct lpfc_fcf_pri *fcf_pri;
1412 
1413 	fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1414 	fcf_pri->fcf_rec.fcf_index = fcf_index;
1415 	/* FCF record priority */
1416 	fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1417 
1418 }
1419 
1420 /**
1421  * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1422  * @fcf_rec: pointer to driver fcf record.
1423  * @new_fcf_record: pointer to fcf record.
1424  *
1425  * This routine copies the FCF information from the FCF
1426  * record to lpfc_hba data structure.
1427  **/
1428 static void
1429 lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1430 		     struct fcf_record *new_fcf_record)
1431 {
1432 	/* Fabric name */
1433 	fcf_rec->fabric_name[0] =
1434 		bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1435 	fcf_rec->fabric_name[1] =
1436 		bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1437 	fcf_rec->fabric_name[2] =
1438 		bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1439 	fcf_rec->fabric_name[3] =
1440 		bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1441 	fcf_rec->fabric_name[4] =
1442 		bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1443 	fcf_rec->fabric_name[5] =
1444 		bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1445 	fcf_rec->fabric_name[6] =
1446 		bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1447 	fcf_rec->fabric_name[7] =
1448 		bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1449 	/* Mac address */
1450 	fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1451 	fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1452 	fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1453 	fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1454 	fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1455 	fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1456 	/* FCF record index */
1457 	fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1458 	/* FCF record priority */
1459 	fcf_rec->priority = new_fcf_record->fip_priority;
1460 	/* Switch name */
1461 	fcf_rec->switch_name[0] =
1462 		bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1463 	fcf_rec->switch_name[1] =
1464 		bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1465 	fcf_rec->switch_name[2] =
1466 		bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1467 	fcf_rec->switch_name[3] =
1468 		bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1469 	fcf_rec->switch_name[4] =
1470 		bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1471 	fcf_rec->switch_name[5] =
1472 		bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1473 	fcf_rec->switch_name[6] =
1474 		bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1475 	fcf_rec->switch_name[7] =
1476 		bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
1477 }
1478 
1479 /**
1480  * lpfc_update_fcf_record - Update driver fcf record
1481  * @phba: pointer to lpfc hba data structure.
1482  * @fcf_rec: pointer to driver fcf record.
1483  * @new_fcf_record: pointer to hba fcf record.
1484  * @addr_mode: address mode to be set to the driver fcf record.
1485  * @vlan_id: vlan tag to be set to the driver fcf record.
1486  * @flag: flag bits to be set to the driver fcf record.
1487  *
1488  * This routine updates the driver FCF record from the new HBA FCF record
1489  * together with the address mode, vlan_id, and other informations. This
1490  * routine is called with the hbalock held.
1491  **/
1492 static void
1493 __lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1494 		       struct fcf_record *new_fcf_record, uint32_t addr_mode,
1495 		       uint16_t vlan_id, uint32_t flag)
1496 {
1497 	lockdep_assert_held(&phba->hbalock);
1498 
1499 	/* Copy the fields from the HBA's FCF record */
1500 	lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1501 	/* Update other fields of driver FCF record */
1502 	fcf_rec->addr_mode = addr_mode;
1503 	fcf_rec->vlan_id = vlan_id;
1504 	fcf_rec->flag |= (flag | RECORD_VALID);
1505 	__lpfc_update_fcf_record_pri(phba,
1506 		bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1507 				 new_fcf_record);
1508 }
1509 
1510 /**
1511  * lpfc_register_fcf - Register the FCF with hba.
1512  * @phba: pointer to lpfc hba data structure.
1513  *
1514  * This routine issues a register fcfi mailbox command to register
1515  * the fcf with HBA.
1516  **/
1517 static void
1518 lpfc_register_fcf(struct lpfc_hba *phba)
1519 {
1520 	LPFC_MBOXQ_t *fcf_mbxq;
1521 	int rc;
1522 
1523 	spin_lock_irq(&phba->hbalock);
1524 	/* If the FCF is not available do nothing. */
1525 	if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1526 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1527 		spin_unlock_irq(&phba->hbalock);
1528 		return;
1529 	}
1530 
1531 	/* The FCF is already registered, start discovery */
1532 	if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1533 		phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1534 		phba->hba_flag &= ~FCF_TS_INPROG;
1535 		if (phba->pport->port_state != LPFC_FLOGI &&
1536 		    phba->pport->fc_flag & FC_FABRIC) {
1537 			phba->hba_flag |= FCF_RR_INPROG;
1538 			spin_unlock_irq(&phba->hbalock);
1539 			lpfc_initial_flogi(phba->pport);
1540 			return;
1541 		}
1542 		spin_unlock_irq(&phba->hbalock);
1543 		return;
1544 	}
1545 	spin_unlock_irq(&phba->hbalock);
1546 
1547 	fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1548 	if (!fcf_mbxq) {
1549 		spin_lock_irq(&phba->hbalock);
1550 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1551 		spin_unlock_irq(&phba->hbalock);
1552 		return;
1553 	}
1554 
1555 	lpfc_reg_fcfi(phba, fcf_mbxq);
1556 	fcf_mbxq->vport = phba->pport;
1557 	fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1558 	rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1559 	if (rc == MBX_NOT_FINISHED) {
1560 		spin_lock_irq(&phba->hbalock);
1561 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1562 		spin_unlock_irq(&phba->hbalock);
1563 		mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1564 	}
1565 
1566 	return;
1567 }
1568 
1569 /**
1570  * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1571  * @phba: pointer to lpfc hba data structure.
1572  * @new_fcf_record: pointer to fcf record.
1573  * @boot_flag: Indicates if this record used by boot bios.
1574  * @addr_mode: The address mode to be used by this FCF
1575  * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
1576  *
1577  * This routine compare the fcf record with connect list obtained from the
1578  * config region to decide if this FCF can be used for SAN discovery. It returns
1579  * 1 if this record can be used for SAN discovery else return zero. If this FCF
1580  * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1581  * is used by boot bios and addr_mode will indicate the addressing mode to be
1582  * used for this FCF when the function returns.
1583  * If the FCF record need to be used with a particular vlan id, the vlan is
1584  * set in the vlan_id on return of the function. If not VLAN tagging need to
1585  * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
1586  **/
1587 static int
1588 lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1589 			struct fcf_record *new_fcf_record,
1590 			uint32_t *boot_flag, uint32_t *addr_mode,
1591 			uint16_t *vlan_id)
1592 {
1593 	struct lpfc_fcf_conn_entry *conn_entry;
1594 	int i, j, fcf_vlan_id = 0;
1595 
1596 	/* Find the lowest VLAN id in the FCF record */
1597 	for (i = 0; i < 512; i++) {
1598 		if (new_fcf_record->vlan_bitmap[i]) {
1599 			fcf_vlan_id = i * 8;
1600 			j = 0;
1601 			while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1602 				j++;
1603 				fcf_vlan_id++;
1604 			}
1605 			break;
1606 		}
1607 	}
1608 
1609 	/* FCF not valid/available or solicitation in progress */
1610 	if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1611 	    !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1612 	    bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
1613 		return 0;
1614 
1615 	if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
1616 		*boot_flag = 0;
1617 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1618 				new_fcf_record);
1619 		if (phba->valid_vlan)
1620 			*vlan_id = phba->vlan_id;
1621 		else
1622 			*vlan_id = LPFC_FCOE_NULL_VID;
1623 		return 1;
1624 	}
1625 
1626 	/*
1627 	 * If there are no FCF connection table entry, driver connect to all
1628 	 * FCFs.
1629 	 */
1630 	if (list_empty(&phba->fcf_conn_rec_list)) {
1631 		*boot_flag = 0;
1632 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1633 			new_fcf_record);
1634 
1635 		/*
1636 		 * When there are no FCF connect entries, use driver's default
1637 		 * addressing mode - FPMA.
1638 		 */
1639 		if (*addr_mode & LPFC_FCF_FPMA)
1640 			*addr_mode = LPFC_FCF_FPMA;
1641 
1642 		/* If FCF record report a vlan id use that vlan id */
1643 		if (fcf_vlan_id)
1644 			*vlan_id = fcf_vlan_id;
1645 		else
1646 			*vlan_id = LPFC_FCOE_NULL_VID;
1647 		return 1;
1648 	}
1649 
1650 	list_for_each_entry(conn_entry,
1651 			    &phba->fcf_conn_rec_list, list) {
1652 		if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1653 			continue;
1654 
1655 		if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1656 			!lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
1657 					     new_fcf_record))
1658 			continue;
1659 		if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1660 			!lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1661 					    new_fcf_record))
1662 			continue;
1663 		if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1664 			/*
1665 			 * If the vlan bit map does not have the bit set for the
1666 			 * vlan id to be used, then it is not a match.
1667 			 */
1668 			if (!(new_fcf_record->vlan_bitmap
1669 				[conn_entry->conn_rec.vlan_tag / 8] &
1670 				(1 << (conn_entry->conn_rec.vlan_tag % 8))))
1671 				continue;
1672 		}
1673 
1674 		/*
1675 		 * If connection record does not support any addressing mode,
1676 		 * skip the FCF record.
1677 		 */
1678 		if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1679 			& (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1680 			continue;
1681 
1682 		/*
1683 		 * Check if the connection record specifies a required
1684 		 * addressing mode.
1685 		 */
1686 		if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1687 			!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1688 
1689 			/*
1690 			 * If SPMA required but FCF not support this continue.
1691 			 */
1692 			if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1693 				!(bf_get(lpfc_fcf_record_mac_addr_prov,
1694 					new_fcf_record) & LPFC_FCF_SPMA))
1695 				continue;
1696 
1697 			/*
1698 			 * If FPMA required but FCF not support this continue.
1699 			 */
1700 			if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1701 				!(bf_get(lpfc_fcf_record_mac_addr_prov,
1702 				new_fcf_record) & LPFC_FCF_FPMA))
1703 				continue;
1704 		}
1705 
1706 		/*
1707 		 * This fcf record matches filtering criteria.
1708 		 */
1709 		if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1710 			*boot_flag = 1;
1711 		else
1712 			*boot_flag = 0;
1713 
1714 		/*
1715 		 * If user did not specify any addressing mode, or if the
1716 		 * preferred addressing mode specified by user is not supported
1717 		 * by FCF, allow fabric to pick the addressing mode.
1718 		 */
1719 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1720 				new_fcf_record);
1721 		/*
1722 		 * If the user specified a required address mode, assign that
1723 		 * address mode
1724 		 */
1725 		if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1726 			(!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1727 			*addr_mode = (conn_entry->conn_rec.flags &
1728 				FCFCNCT_AM_SPMA) ?
1729 				LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1730 		/*
1731 		 * If the user specified a preferred address mode, use the
1732 		 * addr mode only if FCF support the addr_mode.
1733 		 */
1734 		else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1735 			(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1736 			(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1737 			(*addr_mode & LPFC_FCF_SPMA))
1738 				*addr_mode = LPFC_FCF_SPMA;
1739 		else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1740 			(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1741 			!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1742 			(*addr_mode & LPFC_FCF_FPMA))
1743 				*addr_mode = LPFC_FCF_FPMA;
1744 
1745 		/* If matching connect list has a vlan id, use it */
1746 		if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1747 			*vlan_id = conn_entry->conn_rec.vlan_tag;
1748 		/*
1749 		 * If no vlan id is specified in connect list, use the vlan id
1750 		 * in the FCF record
1751 		 */
1752 		else if (fcf_vlan_id)
1753 			*vlan_id = fcf_vlan_id;
1754 		else
1755 			*vlan_id = LPFC_FCOE_NULL_VID;
1756 
1757 		return 1;
1758 	}
1759 
1760 	return 0;
1761 }
1762 
1763 /**
1764  * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1765  * @phba: pointer to lpfc hba data structure.
1766  * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1767  *
1768  * This function check if there is any fcoe event pending while driver
1769  * scan FCF entries. If there is any pending event, it will restart the
1770  * FCF saning and return 1 else return 0.
1771  */
1772 int
1773 lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1774 {
1775 	/*
1776 	 * If the Link is up and no FCoE events while in the
1777 	 * FCF discovery, no need to restart FCF discovery.
1778 	 */
1779 	if ((phba->link_state  >= LPFC_LINK_UP) &&
1780 	    (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1781 		return 0;
1782 
1783 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1784 			"2768 Pending link or FCF event during current "
1785 			"handling of the previous event: link_state:x%x, "
1786 			"evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1787 			phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1788 			phba->fcoe_eventtag);
1789 
1790 	spin_lock_irq(&phba->hbalock);
1791 	phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1792 	spin_unlock_irq(&phba->hbalock);
1793 
1794 	if (phba->link_state >= LPFC_LINK_UP) {
1795 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1796 				"2780 Restart FCF table scan due to "
1797 				"pending FCF event:evt_tag_at_scan:x%x, "
1798 				"evt_tag_current:x%x\n",
1799 				phba->fcoe_eventtag_at_fcf_scan,
1800 				phba->fcoe_eventtag);
1801 		lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1802 	} else {
1803 		/*
1804 		 * Do not continue FCF discovery and clear FCF_TS_INPROG
1805 		 * flag
1806 		 */
1807 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1808 				"2833 Stop FCF discovery process due to link "
1809 				"state change (x%x)\n", phba->link_state);
1810 		spin_lock_irq(&phba->hbalock);
1811 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1812 		phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
1813 		spin_unlock_irq(&phba->hbalock);
1814 	}
1815 
1816 	/* Unregister the currently registered FCF if required */
1817 	if (unreg_fcf) {
1818 		spin_lock_irq(&phba->hbalock);
1819 		phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1820 		spin_unlock_irq(&phba->hbalock);
1821 		lpfc_sli4_unregister_fcf(phba);
1822 	}
1823 	return 1;
1824 }
1825 
1826 /**
1827  * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1828  * @phba: pointer to lpfc hba data structure.
1829  * @fcf_cnt: number of eligible fcf record seen so far.
1830  *
1831  * This function makes an running random selection decision on FCF record to
1832  * use through a sequence of @fcf_cnt eligible FCF records with equal
1833  * probability. To perform integer manunipulation of random numbers with
1834  * size unit32_t, the lower 16 bits of the 32-bit random number returned
1835  * from prandom_u32() are taken as the random random number generated.
1836  *
1837  * Returns true when outcome is for the newly read FCF record should be
1838  * chosen; otherwise, return false when outcome is for keeping the previously
1839  * chosen FCF record.
1840  **/
1841 static bool
1842 lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1843 {
1844 	uint32_t rand_num;
1845 
1846 	/* Get 16-bit uniform random number */
1847 	rand_num = 0xFFFF & prandom_u32();
1848 
1849 	/* Decision with probability 1/fcf_cnt */
1850 	if ((fcf_cnt * rand_num) < 0xFFFF)
1851 		return true;
1852 	else
1853 		return false;
1854 }
1855 
1856 /**
1857  * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
1858  * @phba: pointer to lpfc hba data structure.
1859  * @mboxq: pointer to mailbox object.
1860  * @next_fcf_index: pointer to holder of next fcf index.
1861  *
1862  * This routine parses the non-embedded fcf mailbox command by performing the
1863  * necessarily error checking, non-embedded read FCF record mailbox command
1864  * SGE parsing, and endianness swapping.
1865  *
1866  * Returns the pointer to the new FCF record in the non-embedded mailbox
1867  * command DMA memory if successfully, other NULL.
1868  */
1869 static struct fcf_record *
1870 lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1871 			     uint16_t *next_fcf_index)
1872 {
1873 	void *virt_addr;
1874 	struct lpfc_mbx_sge sge;
1875 	struct lpfc_mbx_read_fcf_tbl *read_fcf;
1876 	uint32_t shdr_status, shdr_add_status, if_type;
1877 	union lpfc_sli4_cfg_shdr *shdr;
1878 	struct fcf_record *new_fcf_record;
1879 
1880 	/* Get the first SGE entry from the non-embedded DMA memory. This
1881 	 * routine only uses a single SGE.
1882 	 */
1883 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1884 	if (unlikely(!mboxq->sge_array)) {
1885 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1886 				"2524 Failed to get the non-embedded SGE "
1887 				"virtual address\n");
1888 		return NULL;
1889 	}
1890 	virt_addr = mboxq->sge_array->addr[0];
1891 
1892 	shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1893 	lpfc_sli_pcimem_bcopy(shdr, shdr,
1894 			      sizeof(union lpfc_sli4_cfg_shdr));
1895 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1896 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1897 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
1898 	if (shdr_status || shdr_add_status) {
1899 		if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1900 					if_type == LPFC_SLI_INTF_IF_TYPE_2)
1901 			lpfc_printf_log(phba, KERN_ERR,
1902 					LOG_TRACE_EVENT,
1903 					"2726 READ_FCF_RECORD Indicates empty "
1904 					"FCF table.\n");
1905 		else
1906 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1907 					"2521 READ_FCF_RECORD mailbox failed "
1908 					"with status x%x add_status x%x, "
1909 					"mbx\n", shdr_status, shdr_add_status);
1910 		return NULL;
1911 	}
1912 
1913 	/* Interpreting the returned information of the FCF record */
1914 	read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1915 	lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1916 			      sizeof(struct lpfc_mbx_read_fcf_tbl));
1917 	*next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1918 	new_fcf_record = (struct fcf_record *)(virt_addr +
1919 			  sizeof(struct lpfc_mbx_read_fcf_tbl));
1920 	lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1921 				offsetof(struct fcf_record, vlan_bitmap));
1922 	new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1923 	new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
1924 
1925 	return new_fcf_record;
1926 }
1927 
1928 /**
1929  * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1930  * @phba: pointer to lpfc hba data structure.
1931  * @fcf_record: pointer to the fcf record.
1932  * @vlan_id: the lowest vlan identifier associated to this fcf record.
1933  * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1934  *
1935  * This routine logs the detailed FCF record if the LOG_FIP loggin is
1936  * enabled.
1937  **/
1938 static void
1939 lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1940 			      struct fcf_record *fcf_record,
1941 			      uint16_t vlan_id,
1942 			      uint16_t next_fcf_index)
1943 {
1944 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1945 			"2764 READ_FCF_RECORD:\n"
1946 			"\tFCF_Index     : x%x\n"
1947 			"\tFCF_Avail     : x%x\n"
1948 			"\tFCF_Valid     : x%x\n"
1949 			"\tFCF_SOL       : x%x\n"
1950 			"\tFIP_Priority  : x%x\n"
1951 			"\tMAC_Provider  : x%x\n"
1952 			"\tLowest VLANID : x%x\n"
1953 			"\tFCF_MAC Addr  : x%x:%x:%x:%x:%x:%x\n"
1954 			"\tFabric_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1955 			"\tSwitch_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1956 			"\tNext_FCF_Index: x%x\n",
1957 			bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1958 			bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1959 			bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1960 			bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
1961 			fcf_record->fip_priority,
1962 			bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1963 			vlan_id,
1964 			bf_get(lpfc_fcf_record_mac_0, fcf_record),
1965 			bf_get(lpfc_fcf_record_mac_1, fcf_record),
1966 			bf_get(lpfc_fcf_record_mac_2, fcf_record),
1967 			bf_get(lpfc_fcf_record_mac_3, fcf_record),
1968 			bf_get(lpfc_fcf_record_mac_4, fcf_record),
1969 			bf_get(lpfc_fcf_record_mac_5, fcf_record),
1970 			bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1971 			bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1972 			bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1973 			bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1974 			bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1975 			bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1976 			bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1977 			bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1978 			bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1979 			bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1980 			bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1981 			bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1982 			bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1983 			bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1984 			bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1985 			bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1986 			next_fcf_index);
1987 }
1988 
1989 /**
1990  * lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1991  * @phba: pointer to lpfc hba data structure.
1992  * @fcf_rec: pointer to an existing FCF record.
1993  * @new_fcf_record: pointer to a new FCF record.
1994  * @new_vlan_id: vlan id from the new FCF record.
1995  *
1996  * This function performs matching test of a new FCF record against an existing
1997  * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1998  * will not be used as part of the FCF record matching criteria.
1999  *
2000  * Returns true if all the fields matching, otherwise returns false.
2001  */
2002 static bool
2003 lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
2004 			   struct lpfc_fcf_rec *fcf_rec,
2005 			   struct fcf_record *new_fcf_record,
2006 			   uint16_t new_vlan_id)
2007 {
2008 	if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
2009 		if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
2010 			return false;
2011 	if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
2012 		return false;
2013 	if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
2014 		return false;
2015 	if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
2016 		return false;
2017 	if (fcf_rec->priority != new_fcf_record->fip_priority)
2018 		return false;
2019 	return true;
2020 }
2021 
2022 /**
2023  * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
2024  * @vport: Pointer to vport object.
2025  * @fcf_index: index to next fcf.
2026  *
2027  * This function processing the roundrobin fcf failover to next fcf index.
2028  * When this function is invoked, there will be a current fcf registered
2029  * for flogi.
2030  * Return: 0 for continue retrying flogi on currently registered fcf;
2031  *         1 for stop flogi on currently registered fcf;
2032  */
2033 int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
2034 {
2035 	struct lpfc_hba *phba = vport->phba;
2036 	int rc;
2037 
2038 	if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
2039 		spin_lock_irq(&phba->hbalock);
2040 		if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2041 			spin_unlock_irq(&phba->hbalock);
2042 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2043 					"2872 Devloss tmo with no eligible "
2044 					"FCF, unregister in-use FCF (x%x) "
2045 					"and rescan FCF table\n",
2046 					phba->fcf.current_rec.fcf_indx);
2047 			lpfc_unregister_fcf_rescan(phba);
2048 			goto stop_flogi_current_fcf;
2049 		}
2050 		/* Mark the end to FLOGI roundrobin failover */
2051 		phba->hba_flag &= ~FCF_RR_INPROG;
2052 		/* Allow action to new fcf asynchronous event */
2053 		phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2054 		spin_unlock_irq(&phba->hbalock);
2055 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2056 				"2865 No FCF available, stop roundrobin FCF "
2057 				"failover and change port state:x%x/x%x\n",
2058 				phba->pport->port_state, LPFC_VPORT_UNKNOWN);
2059 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2060 
2061 		if (!phba->fcf.fcf_redisc_attempted) {
2062 			lpfc_unregister_fcf(phba);
2063 
2064 			rc = lpfc_sli4_redisc_fcf_table(phba);
2065 			if (!rc) {
2066 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2067 						"3195 Rediscover FCF table\n");
2068 				phba->fcf.fcf_redisc_attempted = 1;
2069 				lpfc_sli4_clear_fcf_rr_bmask(phba);
2070 			} else {
2071 				lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2072 						"3196 Rediscover FCF table "
2073 						"failed. Status:x%x\n", rc);
2074 			}
2075 		} else {
2076 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2077 					"3197 Already rediscover FCF table "
2078 					"attempted. No more retry\n");
2079 		}
2080 		goto stop_flogi_current_fcf;
2081 	} else {
2082 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
2083 				"2794 Try FLOGI roundrobin FCF failover to "
2084 				"(x%x)\n", fcf_index);
2085 		rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
2086 		if (rc)
2087 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
2088 					"2761 FLOGI roundrobin FCF failover "
2089 					"failed (rc:x%x) to read FCF (x%x)\n",
2090 					rc, phba->fcf.current_rec.fcf_indx);
2091 		else
2092 			goto stop_flogi_current_fcf;
2093 	}
2094 	return 0;
2095 
2096 stop_flogi_current_fcf:
2097 	lpfc_can_disctmo(vport);
2098 	return 1;
2099 }
2100 
2101 /**
2102  * lpfc_sli4_fcf_pri_list_del
2103  * @phba: pointer to lpfc hba data structure.
2104  * @fcf_index: the index of the fcf record to delete
2105  * This routine checks the on list flag of the fcf_index to be deleted.
2106  * If it is one the list then it is removed from the list, and the flag
2107  * is cleared. This routine grab the hbalock before removing the fcf
2108  * record from the list.
2109  **/
2110 static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2111 			uint16_t fcf_index)
2112 {
2113 	struct lpfc_fcf_pri *new_fcf_pri;
2114 
2115 	new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2116 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2117 		"3058 deleting idx x%x pri x%x flg x%x\n",
2118 		fcf_index, new_fcf_pri->fcf_rec.priority,
2119 		 new_fcf_pri->fcf_rec.flag);
2120 	spin_lock_irq(&phba->hbalock);
2121 	if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2122 		if (phba->fcf.current_rec.priority ==
2123 				new_fcf_pri->fcf_rec.priority)
2124 			phba->fcf.eligible_fcf_cnt--;
2125 		list_del_init(&new_fcf_pri->list);
2126 		new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2127 	}
2128 	spin_unlock_irq(&phba->hbalock);
2129 }
2130 
2131 /**
2132  * lpfc_sli4_set_fcf_flogi_fail
2133  * @phba: pointer to lpfc hba data structure.
2134  * @fcf_index: the index of the fcf record to update
2135  * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2136  * flag so the the round robin slection for the particular priority level
2137  * will try a different fcf record that does not have this bit set.
2138  * If the fcf record is re-read for any reason this flag is cleared brfore
2139  * adding it to the priority list.
2140  **/
2141 void
2142 lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2143 {
2144 	struct lpfc_fcf_pri *new_fcf_pri;
2145 	new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2146 	spin_lock_irq(&phba->hbalock);
2147 	new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2148 	spin_unlock_irq(&phba->hbalock);
2149 }
2150 
2151 /**
2152  * lpfc_sli4_fcf_pri_list_add
2153  * @phba: pointer to lpfc hba data structure.
2154  * @fcf_index: the index of the fcf record to add
2155  * @new_fcf_record: pointer to a new FCF record.
2156  * This routine checks the priority of the fcf_index to be added.
2157  * If it is a lower priority than the current head of the fcf_pri list
2158  * then it is added to the list in the right order.
2159  * If it is the same priority as the current head of the list then it
2160  * is added to the head of the list and its bit in the rr_bmask is set.
2161  * If the fcf_index to be added is of a higher priority than the current
2162  * head of the list then the rr_bmask is cleared, its bit is set in the
2163  * rr_bmask and it is added to the head of the list.
2164  * returns:
2165  * 0=success 1=failure
2166  **/
2167 static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2168 	uint16_t fcf_index,
2169 	struct fcf_record *new_fcf_record)
2170 {
2171 	uint16_t current_fcf_pri;
2172 	uint16_t last_index;
2173 	struct lpfc_fcf_pri *fcf_pri;
2174 	struct lpfc_fcf_pri *next_fcf_pri;
2175 	struct lpfc_fcf_pri *new_fcf_pri;
2176 	int ret;
2177 
2178 	new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2179 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2180 		"3059 adding idx x%x pri x%x flg x%x\n",
2181 		fcf_index, new_fcf_record->fip_priority,
2182 		 new_fcf_pri->fcf_rec.flag);
2183 	spin_lock_irq(&phba->hbalock);
2184 	if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2185 		list_del_init(&new_fcf_pri->list);
2186 	new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2187 	new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2188 	if (list_empty(&phba->fcf.fcf_pri_list)) {
2189 		list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2190 		ret = lpfc_sli4_fcf_rr_index_set(phba,
2191 				new_fcf_pri->fcf_rec.fcf_index);
2192 		goto out;
2193 	}
2194 
2195 	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2196 				LPFC_SLI4_FCF_TBL_INDX_MAX);
2197 	if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2198 		ret = 0; /* Empty rr list */
2199 		goto out;
2200 	}
2201 	current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2202 	if (new_fcf_pri->fcf_rec.priority <=  current_fcf_pri) {
2203 		list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2204 		if (new_fcf_pri->fcf_rec.priority <  current_fcf_pri) {
2205 			memset(phba->fcf.fcf_rr_bmask, 0,
2206 				sizeof(*phba->fcf.fcf_rr_bmask));
2207 			/* fcfs_at_this_priority_level = 1; */
2208 			phba->fcf.eligible_fcf_cnt = 1;
2209 		} else
2210 			/* fcfs_at_this_priority_level++; */
2211 			phba->fcf.eligible_fcf_cnt++;
2212 		ret = lpfc_sli4_fcf_rr_index_set(phba,
2213 				new_fcf_pri->fcf_rec.fcf_index);
2214 		goto out;
2215 	}
2216 
2217 	list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2218 				&phba->fcf.fcf_pri_list, list) {
2219 		if (new_fcf_pri->fcf_rec.priority <=
2220 				fcf_pri->fcf_rec.priority) {
2221 			if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2222 				list_add(&new_fcf_pri->list,
2223 						&phba->fcf.fcf_pri_list);
2224 			else
2225 				list_add(&new_fcf_pri->list,
2226 					 &((struct lpfc_fcf_pri *)
2227 					fcf_pri->list.prev)->list);
2228 			ret = 0;
2229 			goto out;
2230 		} else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2231 			|| new_fcf_pri->fcf_rec.priority <
2232 				next_fcf_pri->fcf_rec.priority) {
2233 			list_add(&new_fcf_pri->list, &fcf_pri->list);
2234 			ret = 0;
2235 			goto out;
2236 		}
2237 		if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2238 			continue;
2239 
2240 	}
2241 	ret = 1;
2242 out:
2243 	/* we use = instead of |= to clear the FLOGI_FAILED flag. */
2244 	new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2245 	spin_unlock_irq(&phba->hbalock);
2246 	return ret;
2247 }
2248 
2249 /**
2250  * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2251  * @phba: pointer to lpfc hba data structure.
2252  * @mboxq: pointer to mailbox object.
2253  *
2254  * This function iterates through all the fcf records available in
2255  * HBA and chooses the optimal FCF record for discovery. After finding
2256  * the FCF for discovery it registers the FCF record and kicks start
2257  * discovery.
2258  * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2259  * use an FCF record which matches fabric name and mac address of the
2260  * currently used FCF record.
2261  * If the driver supports only one FCF, it will try to use the FCF record
2262  * used by BOOT_BIOS.
2263  */
2264 void
2265 lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2266 {
2267 	struct fcf_record *new_fcf_record;
2268 	uint32_t boot_flag, addr_mode;
2269 	uint16_t fcf_index, next_fcf_index;
2270 	struct lpfc_fcf_rec *fcf_rec = NULL;
2271 	uint16_t vlan_id = LPFC_FCOE_NULL_VID;
2272 	bool select_new_fcf;
2273 	int rc;
2274 
2275 	/* If there is pending FCoE event restart FCF table scan */
2276 	if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
2277 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
2278 		return;
2279 	}
2280 
2281 	/* Parse the FCF record from the non-embedded mailbox command */
2282 	new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2283 						      &next_fcf_index);
2284 	if (!new_fcf_record) {
2285 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2286 				"2765 Mailbox command READ_FCF_RECORD "
2287 				"failed to retrieve a FCF record.\n");
2288 		/* Let next new FCF event trigger fast failover */
2289 		spin_lock_irq(&phba->hbalock);
2290 		phba->hba_flag &= ~FCF_TS_INPROG;
2291 		spin_unlock_irq(&phba->hbalock);
2292 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
2293 		return;
2294 	}
2295 
2296 	/* Check the FCF record against the connection list */
2297 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2298 				      &addr_mode, &vlan_id);
2299 
2300 	/* Log the FCF record information if turned on */
2301 	lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2302 				      next_fcf_index);
2303 
2304 	/*
2305 	 * If the fcf record does not match with connect list entries
2306 	 * read the next entry; otherwise, this is an eligible FCF
2307 	 * record for roundrobin FCF failover.
2308 	 */
2309 	if (!rc) {
2310 		lpfc_sli4_fcf_pri_list_del(phba,
2311 					bf_get(lpfc_fcf_record_fcf_index,
2312 					       new_fcf_record));
2313 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2314 				"2781 FCF (x%x) failed connection "
2315 				"list check: (x%x/x%x/%x)\n",
2316 				bf_get(lpfc_fcf_record_fcf_index,
2317 				       new_fcf_record),
2318 				bf_get(lpfc_fcf_record_fcf_avail,
2319 				       new_fcf_record),
2320 				bf_get(lpfc_fcf_record_fcf_valid,
2321 				       new_fcf_record),
2322 				bf_get(lpfc_fcf_record_fcf_sol,
2323 				       new_fcf_record));
2324 		if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2325 		    lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2326 		    new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
2327 			if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2328 			    phba->fcf.current_rec.fcf_indx) {
2329 				lpfc_printf_log(phba, KERN_ERR,
2330 						LOG_TRACE_EVENT,
2331 					"2862 FCF (x%x) matches property "
2332 					"of in-use FCF (x%x)\n",
2333 					bf_get(lpfc_fcf_record_fcf_index,
2334 					       new_fcf_record),
2335 					phba->fcf.current_rec.fcf_indx);
2336 				goto read_next_fcf;
2337 			}
2338 			/*
2339 			 * In case the current in-use FCF record becomes
2340 			 * invalid/unavailable during FCF discovery that
2341 			 * was not triggered by fast FCF failover process,
2342 			 * treat it as fast FCF failover.
2343 			 */
2344 			if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2345 			    !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2346 				lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2347 						"2835 Invalid in-use FCF "
2348 						"(x%x), enter FCF failover "
2349 						"table scan.\n",
2350 						phba->fcf.current_rec.fcf_indx);
2351 				spin_lock_irq(&phba->hbalock);
2352 				phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2353 				spin_unlock_irq(&phba->hbalock);
2354 				lpfc_sli4_mbox_cmd_free(phba, mboxq);
2355 				lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2356 						LPFC_FCOE_FCF_GET_FIRST);
2357 				return;
2358 			}
2359 		}
2360 		goto read_next_fcf;
2361 	} else {
2362 		fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2363 		rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2364 							new_fcf_record);
2365 		if (rc)
2366 			goto read_next_fcf;
2367 	}
2368 
2369 	/*
2370 	 * If this is not the first FCF discovery of the HBA, use last
2371 	 * FCF record for the discovery. The condition that a rescan
2372 	 * matches the in-use FCF record: fabric name, switch name, mac
2373 	 * address, and vlan_id.
2374 	 */
2375 	spin_lock_irq(&phba->hbalock);
2376 	if (phba->fcf.fcf_flag & FCF_IN_USE) {
2377 		if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2378 			lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2379 		    new_fcf_record, vlan_id)) {
2380 			if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2381 			    phba->fcf.current_rec.fcf_indx) {
2382 				phba->fcf.fcf_flag |= FCF_AVAILABLE;
2383 				if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2384 					/* Stop FCF redisc wait timer */
2385 					__lpfc_sli4_stop_fcf_redisc_wait_timer(
2386 									phba);
2387 				else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2388 					/* Fast failover, mark completed */
2389 					phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2390 				spin_unlock_irq(&phba->hbalock);
2391 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2392 						"2836 New FCF matches in-use "
2393 						"FCF (x%x), port_state:x%x, "
2394 						"fc_flag:x%x\n",
2395 						phba->fcf.current_rec.fcf_indx,
2396 						phba->pport->port_state,
2397 						phba->pport->fc_flag);
2398 				goto out;
2399 			} else
2400 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2401 					"2863 New FCF (x%x) matches "
2402 					"property of in-use FCF (x%x)\n",
2403 					bf_get(lpfc_fcf_record_fcf_index,
2404 					       new_fcf_record),
2405 					phba->fcf.current_rec.fcf_indx);
2406 		}
2407 		/*
2408 		 * Read next FCF record from HBA searching for the matching
2409 		 * with in-use record only if not during the fast failover
2410 		 * period. In case of fast failover period, it shall try to
2411 		 * determine whether the FCF record just read should be the
2412 		 * next candidate.
2413 		 */
2414 		if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2415 			spin_unlock_irq(&phba->hbalock);
2416 			goto read_next_fcf;
2417 		}
2418 	}
2419 	/*
2420 	 * Update on failover FCF record only if it's in FCF fast-failover
2421 	 * period; otherwise, update on current FCF record.
2422 	 */
2423 	if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2424 		fcf_rec = &phba->fcf.failover_rec;
2425 	else
2426 		fcf_rec = &phba->fcf.current_rec;
2427 
2428 	if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2429 		/*
2430 		 * If the driver FCF record does not have boot flag
2431 		 * set and new hba fcf record has boot flag set, use
2432 		 * the new hba fcf record.
2433 		 */
2434 		if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2435 			/* Choose this FCF record */
2436 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2437 					"2837 Update current FCF record "
2438 					"(x%x) with new FCF record (x%x)\n",
2439 					fcf_rec->fcf_indx,
2440 					bf_get(lpfc_fcf_record_fcf_index,
2441 					new_fcf_record));
2442 			__lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2443 					addr_mode, vlan_id, BOOT_ENABLE);
2444 			spin_unlock_irq(&phba->hbalock);
2445 			goto read_next_fcf;
2446 		}
2447 		/*
2448 		 * If the driver FCF record has boot flag set and the
2449 		 * new hba FCF record does not have boot flag, read
2450 		 * the next FCF record.
2451 		 */
2452 		if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
2453 			spin_unlock_irq(&phba->hbalock);
2454 			goto read_next_fcf;
2455 		}
2456 		/*
2457 		 * If the new hba FCF record has lower priority value
2458 		 * than the driver FCF record, use the new record.
2459 		 */
2460 		if (new_fcf_record->fip_priority < fcf_rec->priority) {
2461 			/* Choose the new FCF record with lower priority */
2462 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2463 					"2838 Update current FCF record "
2464 					"(x%x) with new FCF record (x%x)\n",
2465 					fcf_rec->fcf_indx,
2466 					bf_get(lpfc_fcf_record_fcf_index,
2467 					       new_fcf_record));
2468 			__lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2469 					addr_mode, vlan_id, 0);
2470 			/* Reset running random FCF selection count */
2471 			phba->fcf.eligible_fcf_cnt = 1;
2472 		} else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2473 			/* Update running random FCF selection count */
2474 			phba->fcf.eligible_fcf_cnt++;
2475 			select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2476 						phba->fcf.eligible_fcf_cnt);
2477 			if (select_new_fcf) {
2478 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2479 					"2839 Update current FCF record "
2480 					"(x%x) with new FCF record (x%x)\n",
2481 					fcf_rec->fcf_indx,
2482 					bf_get(lpfc_fcf_record_fcf_index,
2483 					       new_fcf_record));
2484 				/* Choose the new FCF by random selection */
2485 				__lpfc_update_fcf_record(phba, fcf_rec,
2486 							 new_fcf_record,
2487 							 addr_mode, vlan_id, 0);
2488 			}
2489 		}
2490 		spin_unlock_irq(&phba->hbalock);
2491 		goto read_next_fcf;
2492 	}
2493 	/*
2494 	 * This is the first suitable FCF record, choose this record for
2495 	 * initial best-fit FCF.
2496 	 */
2497 	if (fcf_rec) {
2498 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2499 				"2840 Update initial FCF candidate "
2500 				"with FCF (x%x)\n",
2501 				bf_get(lpfc_fcf_record_fcf_index,
2502 				       new_fcf_record));
2503 		__lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2504 					 addr_mode, vlan_id, (boot_flag ?
2505 					 BOOT_ENABLE : 0));
2506 		phba->fcf.fcf_flag |= FCF_AVAILABLE;
2507 		/* Setup initial running random FCF selection count */
2508 		phba->fcf.eligible_fcf_cnt = 1;
2509 	}
2510 	spin_unlock_irq(&phba->hbalock);
2511 	goto read_next_fcf;
2512 
2513 read_next_fcf:
2514 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
2515 	if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2516 		if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2517 			/*
2518 			 * Case of FCF fast failover scan
2519 			 */
2520 
2521 			/*
2522 			 * It has not found any suitable FCF record, cancel
2523 			 * FCF scan inprogress, and do nothing
2524 			 */
2525 			if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
2526 				lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2527 					       "2782 No suitable FCF found: "
2528 					       "(x%x/x%x)\n",
2529 					       phba->fcoe_eventtag_at_fcf_scan,
2530 					       bf_get(lpfc_fcf_record_fcf_index,
2531 						      new_fcf_record));
2532 				spin_lock_irq(&phba->hbalock);
2533 				if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2534 					phba->hba_flag &= ~FCF_TS_INPROG;
2535 					spin_unlock_irq(&phba->hbalock);
2536 					/* Unregister in-use FCF and rescan */
2537 					lpfc_printf_log(phba, KERN_INFO,
2538 							LOG_FIP,
2539 							"2864 On devloss tmo "
2540 							"unreg in-use FCF and "
2541 							"rescan FCF table\n");
2542 					lpfc_unregister_fcf_rescan(phba);
2543 					return;
2544 				}
2545 				/*
2546 				 * Let next new FCF event trigger fast failover
2547 				 */
2548 				phba->hba_flag &= ~FCF_TS_INPROG;
2549 				spin_unlock_irq(&phba->hbalock);
2550 				return;
2551 			}
2552 			/*
2553 			 * It has found a suitable FCF record that is not
2554 			 * the same as in-use FCF record, unregister the
2555 			 * in-use FCF record, replace the in-use FCF record
2556 			 * with the new FCF record, mark FCF fast failover
2557 			 * completed, and then start register the new FCF
2558 			 * record.
2559 			 */
2560 
2561 			/* Unregister the current in-use FCF record */
2562 			lpfc_unregister_fcf(phba);
2563 
2564 			/* Replace in-use record with the new record */
2565 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2566 					"2842 Replace in-use FCF (x%x) "
2567 					"with failover FCF (x%x)\n",
2568 					phba->fcf.current_rec.fcf_indx,
2569 					phba->fcf.failover_rec.fcf_indx);
2570 			memcpy(&phba->fcf.current_rec,
2571 			       &phba->fcf.failover_rec,
2572 			       sizeof(struct lpfc_fcf_rec));
2573 			/*
2574 			 * Mark the fast FCF failover rediscovery completed
2575 			 * and the start of the first round of the roundrobin
2576 			 * FCF failover.
2577 			 */
2578 			spin_lock_irq(&phba->hbalock);
2579 			phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2580 			spin_unlock_irq(&phba->hbalock);
2581 			/* Register to the new FCF record */
2582 			lpfc_register_fcf(phba);
2583 		} else {
2584 			/*
2585 			 * In case of transaction period to fast FCF failover,
2586 			 * do nothing when search to the end of the FCF table.
2587 			 */
2588 			if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2589 			    (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2590 				return;
2591 
2592 			if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2593 				phba->fcf.fcf_flag & FCF_IN_USE) {
2594 				/*
2595 				 * In case the current in-use FCF record no
2596 				 * longer existed during FCF discovery that
2597 				 * was not triggered by fast FCF failover
2598 				 * process, treat it as fast FCF failover.
2599 				 */
2600 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2601 						"2841 In-use FCF record (x%x) "
2602 						"not reported, entering fast "
2603 						"FCF failover mode scanning.\n",
2604 						phba->fcf.current_rec.fcf_indx);
2605 				spin_lock_irq(&phba->hbalock);
2606 				phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2607 				spin_unlock_irq(&phba->hbalock);
2608 				lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2609 						LPFC_FCOE_FCF_GET_FIRST);
2610 				return;
2611 			}
2612 			/* Register to the new FCF record */
2613 			lpfc_register_fcf(phba);
2614 		}
2615 	} else
2616 		lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
2617 	return;
2618 
2619 out:
2620 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
2621 	lpfc_register_fcf(phba);
2622 
2623 	return;
2624 }
2625 
2626 /**
2627  * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
2628  * @phba: pointer to lpfc hba data structure.
2629  * @mboxq: pointer to mailbox object.
2630  *
2631  * This is the callback function for FLOGI failure roundrobin FCF failover
2632  * read FCF record mailbox command from the eligible FCF record bmask for
2633  * performing the failover. If the FCF read back is not valid/available, it
2634  * fails through to retrying FLOGI to the currently registered FCF again.
2635  * Otherwise, if the FCF read back is valid and available, it will set the
2636  * newly read FCF record to the failover FCF record, unregister currently
2637  * registered FCF record, copy the failover FCF record to the current
2638  * FCF record, and then register the current FCF record before proceeding
2639  * to trying FLOGI on the new failover FCF.
2640  */
2641 void
2642 lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2643 {
2644 	struct fcf_record *new_fcf_record;
2645 	uint32_t boot_flag, addr_mode;
2646 	uint16_t next_fcf_index, fcf_index;
2647 	uint16_t current_fcf_index;
2648 	uint16_t vlan_id;
2649 	int rc;
2650 
2651 	/* If link state is not up, stop the roundrobin failover process */
2652 	if (phba->link_state < LPFC_LINK_UP) {
2653 		spin_lock_irq(&phba->hbalock);
2654 		phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2655 		phba->hba_flag &= ~FCF_RR_INPROG;
2656 		spin_unlock_irq(&phba->hbalock);
2657 		goto out;
2658 	}
2659 
2660 	/* Parse the FCF record from the non-embedded mailbox command */
2661 	new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2662 						      &next_fcf_index);
2663 	if (!new_fcf_record) {
2664 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2665 				"2766 Mailbox command READ_FCF_RECORD "
2666 				"failed to retrieve a FCF record. "
2667 				"hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2668 				phba->fcf.fcf_flag);
2669 		lpfc_unregister_fcf_rescan(phba);
2670 		goto out;
2671 	}
2672 
2673 	/* Get the needed parameters from FCF record */
2674 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2675 				      &addr_mode, &vlan_id);
2676 
2677 	/* Log the FCF record information if turned on */
2678 	lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2679 				      next_fcf_index);
2680 
2681 	fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2682 	if (!rc) {
2683 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2684 				"2848 Remove ineligible FCF (x%x) from "
2685 				"from roundrobin bmask\n", fcf_index);
2686 		/* Clear roundrobin bmask bit for ineligible FCF */
2687 		lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2688 		/* Perform next round of roundrobin FCF failover */
2689 		fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2690 		rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2691 		if (rc)
2692 			goto out;
2693 		goto error_out;
2694 	}
2695 
2696 	if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2697 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2698 				"2760 Perform FLOGI roundrobin FCF failover: "
2699 				"FCF (x%x) back to FCF (x%x)\n",
2700 				phba->fcf.current_rec.fcf_indx, fcf_index);
2701 		/* Wait 500 ms before retrying FLOGI to current FCF */
2702 		msleep(500);
2703 		lpfc_issue_init_vfi(phba->pport);
2704 		goto out;
2705 	}
2706 
2707 	/* Upload new FCF record to the failover FCF record */
2708 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2709 			"2834 Update current FCF (x%x) with new FCF (x%x)\n",
2710 			phba->fcf.failover_rec.fcf_indx, fcf_index);
2711 	spin_lock_irq(&phba->hbalock);
2712 	__lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2713 				 new_fcf_record, addr_mode, vlan_id,
2714 				 (boot_flag ? BOOT_ENABLE : 0));
2715 	spin_unlock_irq(&phba->hbalock);
2716 
2717 	current_fcf_index = phba->fcf.current_rec.fcf_indx;
2718 
2719 	/* Unregister the current in-use FCF record */
2720 	lpfc_unregister_fcf(phba);
2721 
2722 	/* Replace in-use record with the new record */
2723 	memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2724 	       sizeof(struct lpfc_fcf_rec));
2725 
2726 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2727 			"2783 Perform FLOGI roundrobin FCF failover: FCF "
2728 			"(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
2729 
2730 error_out:
2731 	lpfc_register_fcf(phba);
2732 out:
2733 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
2734 }
2735 
2736 /**
2737  * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2738  * @phba: pointer to lpfc hba data structure.
2739  * @mboxq: pointer to mailbox object.
2740  *
2741  * This is the callback function of read FCF record mailbox command for
2742  * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
2743  * failover when a new FCF event happened. If the FCF read back is
2744  * valid/available and it passes the connection list check, it updates
2745  * the bmask for the eligible FCF record for roundrobin failover.
2746  */
2747 void
2748 lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2749 {
2750 	struct fcf_record *new_fcf_record;
2751 	uint32_t boot_flag, addr_mode;
2752 	uint16_t fcf_index, next_fcf_index;
2753 	uint16_t vlan_id;
2754 	int rc;
2755 
2756 	/* If link state is not up, no need to proceed */
2757 	if (phba->link_state < LPFC_LINK_UP)
2758 		goto out;
2759 
2760 	/* If FCF discovery period is over, no need to proceed */
2761 	if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
2762 		goto out;
2763 
2764 	/* Parse the FCF record from the non-embedded mailbox command */
2765 	new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2766 						      &next_fcf_index);
2767 	if (!new_fcf_record) {
2768 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2769 				"2767 Mailbox command READ_FCF_RECORD "
2770 				"failed to retrieve a FCF record.\n");
2771 		goto out;
2772 	}
2773 
2774 	/* Check the connection list for eligibility */
2775 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2776 				      &addr_mode, &vlan_id);
2777 
2778 	/* Log the FCF record information if turned on */
2779 	lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2780 				      next_fcf_index);
2781 
2782 	if (!rc)
2783 		goto out;
2784 
2785 	/* Update the eligible FCF record index bmask */
2786 	fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2787 
2788 	rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
2789 
2790 out:
2791 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
2792 }
2793 
2794 /**
2795  * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2796  * @phba: pointer to lpfc hba data structure.
2797  * @mboxq: pointer to mailbox data structure.
2798  *
2799  * This function handles completion of init vfi mailbox command.
2800  */
2801 static void
2802 lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2803 {
2804 	struct lpfc_vport *vport = mboxq->vport;
2805 
2806 	/*
2807 	 * VFI not supported on interface type 0, just do the flogi
2808 	 * Also continue if the VFI is in use - just use the same one.
2809 	 */
2810 	if (mboxq->u.mb.mbxStatus &&
2811 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2812 			LPFC_SLI_INTF_IF_TYPE_0) &&
2813 	    mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
2814 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2815 				 "2891 Init VFI mailbox failed 0x%x\n",
2816 				 mboxq->u.mb.mbxStatus);
2817 		mempool_free(mboxq, phba->mbox_mem_pool);
2818 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2819 		return;
2820 	}
2821 
2822 	lpfc_initial_flogi(vport);
2823 	mempool_free(mboxq, phba->mbox_mem_pool);
2824 	return;
2825 }
2826 
2827 /**
2828  * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2829  * @vport: pointer to lpfc_vport data structure.
2830  *
2831  * This function issue a init_vfi mailbox command to initialize the VFI and
2832  * VPI for the physical port.
2833  */
2834 void
2835 lpfc_issue_init_vfi(struct lpfc_vport *vport)
2836 {
2837 	LPFC_MBOXQ_t *mboxq;
2838 	int rc;
2839 	struct lpfc_hba *phba = vport->phba;
2840 
2841 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2842 	if (!mboxq) {
2843 		lpfc_printf_vlog(vport, KERN_ERR,
2844 			LOG_TRACE_EVENT, "2892 Failed to allocate "
2845 			"init_vfi mailbox\n");
2846 		return;
2847 	}
2848 	lpfc_init_vfi(mboxq, vport);
2849 	mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2850 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2851 	if (rc == MBX_NOT_FINISHED) {
2852 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2853 				 "2893 Failed to issue init_vfi mailbox\n");
2854 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
2855 	}
2856 }
2857 
2858 /**
2859  * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2860  * @phba: pointer to lpfc hba data structure.
2861  * @mboxq: pointer to mailbox data structure.
2862  *
2863  * This function handles completion of init vpi mailbox command.
2864  */
2865 void
2866 lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2867 {
2868 	struct lpfc_vport *vport = mboxq->vport;
2869 	struct lpfc_nodelist *ndlp;
2870 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2871 
2872 	if (mboxq->u.mb.mbxStatus) {
2873 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2874 				 "2609 Init VPI mailbox failed 0x%x\n",
2875 				 mboxq->u.mb.mbxStatus);
2876 		mempool_free(mboxq, phba->mbox_mem_pool);
2877 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2878 		return;
2879 	}
2880 	spin_lock_irq(shost->host_lock);
2881 	vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
2882 	spin_unlock_irq(shost->host_lock);
2883 
2884 	/* If this port is physical port or FDISC is done, do reg_vpi */
2885 	if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2886 			ndlp = lpfc_findnode_did(vport, Fabric_DID);
2887 			if (!ndlp)
2888 				lpfc_printf_vlog(vport, KERN_ERR,
2889 					LOG_TRACE_EVENT,
2890 					"2731 Cannot find fabric "
2891 					"controller node\n");
2892 			else
2893 				lpfc_register_new_vport(phba, vport, ndlp);
2894 			mempool_free(mboxq, phba->mbox_mem_pool);
2895 			return;
2896 	}
2897 
2898 	if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2899 		lpfc_initial_fdisc(vport);
2900 	else {
2901 		lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
2902 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2903 				 "2606 No NPIV Fabric support\n");
2904 	}
2905 	mempool_free(mboxq, phba->mbox_mem_pool);
2906 	return;
2907 }
2908 
2909 /**
2910  * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2911  * @vport: pointer to lpfc_vport data structure.
2912  *
2913  * This function issue a init_vpi mailbox command to initialize
2914  * VPI for the vport.
2915  */
2916 void
2917 lpfc_issue_init_vpi(struct lpfc_vport *vport)
2918 {
2919 	LPFC_MBOXQ_t *mboxq;
2920 	int rc, vpi;
2921 
2922 	if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2923 		vpi = lpfc_alloc_vpi(vport->phba);
2924 		if (!vpi) {
2925 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2926 					 "3303 Failed to obtain vport vpi\n");
2927 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2928 			return;
2929 		}
2930 		vport->vpi = vpi;
2931 	}
2932 
2933 	mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2934 	if (!mboxq) {
2935 		lpfc_printf_vlog(vport, KERN_ERR,
2936 			LOG_TRACE_EVENT, "2607 Failed to allocate "
2937 			"init_vpi mailbox\n");
2938 		return;
2939 	}
2940 	lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2941 	mboxq->vport = vport;
2942 	mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2943 	rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2944 	if (rc == MBX_NOT_FINISHED) {
2945 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2946 				 "2608 Failed to issue init_vpi mailbox\n");
2947 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
2948 	}
2949 }
2950 
2951 /**
2952  * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2953  * @phba: pointer to lpfc hba data structure.
2954  *
2955  * This function loops through the list of vports on the @phba and issues an
2956  * FDISC if possible.
2957  */
2958 void
2959 lpfc_start_fdiscs(struct lpfc_hba *phba)
2960 {
2961 	struct lpfc_vport **vports;
2962 	int i;
2963 
2964 	vports = lpfc_create_vport_work_array(phba);
2965 	if (vports != NULL) {
2966 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2967 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2968 				continue;
2969 			/* There are no vpi for this vport */
2970 			if (vports[i]->vpi > phba->max_vpi) {
2971 				lpfc_vport_set_state(vports[i],
2972 						     FC_VPORT_FAILED);
2973 				continue;
2974 			}
2975 			if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
2976 				lpfc_vport_set_state(vports[i],
2977 						     FC_VPORT_LINKDOWN);
2978 				continue;
2979 			}
2980 			if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
2981 				lpfc_issue_init_vpi(vports[i]);
2982 				continue;
2983 			}
2984 			if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2985 				lpfc_initial_fdisc(vports[i]);
2986 			else {
2987 				lpfc_vport_set_state(vports[i],
2988 						     FC_VPORT_NO_FABRIC_SUPP);
2989 				lpfc_printf_vlog(vports[i], KERN_ERR,
2990 						 LOG_TRACE_EVENT,
2991 						 "0259 No NPIV "
2992 						 "Fabric support\n");
2993 			}
2994 		}
2995 	}
2996 	lpfc_destroy_vport_work_array(phba, vports);
2997 }
2998 
2999 void
3000 lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3001 {
3002 	struct lpfc_dmabuf *dmabuf = mboxq->ctx_buf;
3003 	struct lpfc_vport *vport = mboxq->vport;
3004 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3005 
3006 	/*
3007 	 * VFI not supported for interface type 0, so ignore any mailbox
3008 	 * error (except VFI in use) and continue with the discovery.
3009 	 */
3010 	if (mboxq->u.mb.mbxStatus &&
3011 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3012 			LPFC_SLI_INTF_IF_TYPE_0) &&
3013 	    mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
3014 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3015 				 "2018 REG_VFI mbxStatus error x%x "
3016 				 "HBA state x%x\n",
3017 				 mboxq->u.mb.mbxStatus, vport->port_state);
3018 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3019 			/* FLOGI failed, use loop map to make discovery list */
3020 			lpfc_disc_list_loopmap(vport);
3021 			/* Start discovery */
3022 			lpfc_disc_start(vport);
3023 			goto out_free_mem;
3024 		}
3025 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3026 		goto out_free_mem;
3027 	}
3028 
3029 	/* If the VFI is already registered, there is nothing else to do
3030 	 * Unless this was a VFI update and we are in PT2PT mode, then
3031 	 * we should drop through to set the port state to ready.
3032 	 */
3033 	if (vport->fc_flag & FC_VFI_REGISTERED)
3034 		if (!(phba->sli_rev == LPFC_SLI_REV4 &&
3035 		      vport->fc_flag & FC_PT2PT))
3036 			goto out_free_mem;
3037 
3038 	/* The VPI is implicitly registered when the VFI is registered */
3039 	spin_lock_irq(shost->host_lock);
3040 	vport->vpi_state |= LPFC_VPI_REGISTERED;
3041 	vport->fc_flag |= FC_VFI_REGISTERED;
3042 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
3043 	vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
3044 	spin_unlock_irq(shost->host_lock);
3045 
3046 	/* In case SLI4 FC loopback test, we are ready */
3047 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
3048 	    (phba->link_flag & LS_LOOPBACK_MODE)) {
3049 		phba->link_state = LPFC_HBA_READY;
3050 		goto out_free_mem;
3051 	}
3052 
3053 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3054 			 "3313 cmpl reg vfi  port_state:%x fc_flag:%x myDid:%x "
3055 			 "alpacnt:%d LinkState:%x topology:%x\n",
3056 			 vport->port_state, vport->fc_flag, vport->fc_myDID,
3057 			 vport->phba->alpa_map[0],
3058 			 phba->link_state, phba->fc_topology);
3059 
3060 	if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
3061 		/*
3062 		 * For private loop or for NPort pt2pt,
3063 		 * just start discovery and we are done.
3064 		 */
3065 		if ((vport->fc_flag & FC_PT2PT) ||
3066 		    ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
3067 		    !(vport->fc_flag & FC_PUBLIC_LOOP))) {
3068 
3069 			/* Use loop map to make discovery list */
3070 			lpfc_disc_list_loopmap(vport);
3071 			/* Start discovery */
3072 			if (vport->fc_flag & FC_PT2PT)
3073 				vport->port_state = LPFC_VPORT_READY;
3074 			else
3075 				lpfc_disc_start(vport);
3076 		} else {
3077 			lpfc_start_fdiscs(phba);
3078 			lpfc_do_scr_ns_plogi(phba, vport);
3079 		}
3080 	}
3081 
3082 out_free_mem:
3083 	mempool_free(mboxq, phba->mbox_mem_pool);
3084 	if (dmabuf) {
3085 		lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
3086 		kfree(dmabuf);
3087 	}
3088 	return;
3089 }
3090 
3091 static void
3092 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3093 {
3094 	MAILBOX_t *mb = &pmb->u.mb;
3095 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3096 	struct lpfc_vport  *vport = pmb->vport;
3097 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3098 	struct serv_parm *sp = &vport->fc_sparam;
3099 	uint32_t ed_tov;
3100 
3101 	/* Check for error */
3102 	if (mb->mbxStatus) {
3103 		/* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
3104 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3105 				 "0319 READ_SPARAM mbxStatus error x%x "
3106 				 "hba state x%x>\n",
3107 				 mb->mbxStatus, vport->port_state);
3108 		lpfc_linkdown(phba);
3109 		goto out;
3110 	}
3111 
3112 	memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
3113 	       sizeof (struct serv_parm));
3114 
3115 	ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3116 	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
3117 		ed_tov = (ed_tov + 999999) / 1000000;
3118 
3119 	phba->fc_edtov = ed_tov;
3120 	phba->fc_ratov = (2 * ed_tov) / 1000;
3121 	if (phba->fc_ratov < FF_DEF_RATOV) {
3122 		/* RA_TOV should be atleast 10sec for initial flogi */
3123 		phba->fc_ratov = FF_DEF_RATOV;
3124 	}
3125 
3126 	lpfc_update_vport_wwn(vport);
3127 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
3128 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
3129 		memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3130 		memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3131 	}
3132 
3133 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3134 	kfree(mp);
3135 	mempool_free(pmb, phba->mbox_mem_pool);
3136 
3137 	/* Check if sending the FLOGI is being deferred to after we get
3138 	 * up to date CSPs from MBX_READ_SPARAM.
3139 	 */
3140 	if (phba->hba_flag & HBA_DEFER_FLOGI) {
3141 		lpfc_initial_flogi(vport);
3142 		phba->hba_flag &= ~HBA_DEFER_FLOGI;
3143 	}
3144 	return;
3145 
3146 out:
3147 	pmb->ctx_buf = NULL;
3148 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3149 	kfree(mp);
3150 	lpfc_issue_clear_la(phba, vport);
3151 	mempool_free(pmb, phba->mbox_mem_pool);
3152 	return;
3153 }
3154 
3155 static void
3156 lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
3157 {
3158 	struct lpfc_vport *vport = phba->pport;
3159 	LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
3160 	struct Scsi_Host *shost;
3161 	int i;
3162 	struct lpfc_dmabuf *mp;
3163 	int rc;
3164 	struct fcf_record *fcf_record;
3165 	uint32_t fc_flags = 0;
3166 	unsigned long iflags;
3167 
3168 	spin_lock_irqsave(&phba->hbalock, iflags);
3169 	phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3170 
3171 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3172 		switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3173 		case LPFC_LINK_SPEED_1GHZ:
3174 		case LPFC_LINK_SPEED_2GHZ:
3175 		case LPFC_LINK_SPEED_4GHZ:
3176 		case LPFC_LINK_SPEED_8GHZ:
3177 		case LPFC_LINK_SPEED_10GHZ:
3178 		case LPFC_LINK_SPEED_16GHZ:
3179 		case LPFC_LINK_SPEED_32GHZ:
3180 		case LPFC_LINK_SPEED_64GHZ:
3181 		case LPFC_LINK_SPEED_128GHZ:
3182 			break;
3183 		default:
3184 			phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3185 			break;
3186 		}
3187 	}
3188 
3189 	if (phba->fc_topology &&
3190 	    phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3191 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3192 				"3314 Toplogy changed was 0x%x is 0x%x\n",
3193 				phba->fc_topology,
3194 				bf_get(lpfc_mbx_read_top_topology, la));
3195 		phba->fc_topology_changed = 1;
3196 	}
3197 
3198 	phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
3199 	phba->link_flag &= ~(LS_NPIV_FAB_SUPPORTED | LS_CT_VEN_RPA);
3200 
3201 	shost = lpfc_shost_from_vport(vport);
3202 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3203 		phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
3204 
3205 		/* if npiv is enabled and this adapter supports npiv log
3206 		 * a message that npiv is not supported in this topology
3207 		 */
3208 		if (phba->cfg_enable_npiv && phba->max_vpi)
3209 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3210 				"1309 Link Up Event npiv not supported in loop "
3211 				"topology\n");
3212 				/* Get Loop Map information */
3213 		if (bf_get(lpfc_mbx_read_top_il, la))
3214 			fc_flags |= FC_LBIT;
3215 
3216 		vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3217 		i = la->lilpBde64.tus.f.bdeSize;
3218 
3219 		if (i == 0) {
3220 			phba->alpa_map[0] = 0;
3221 		} else {
3222 			if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
3223 				int numalpa, j, k;
3224 				union {
3225 					uint8_t pamap[16];
3226 					struct {
3227 						uint32_t wd1;
3228 						uint32_t wd2;
3229 						uint32_t wd3;
3230 						uint32_t wd4;
3231 					} pa;
3232 				} un;
3233 				numalpa = phba->alpa_map[0];
3234 				j = 0;
3235 				while (j < numalpa) {
3236 					memset(un.pamap, 0, 16);
3237 					for (k = 1; j < numalpa; k++) {
3238 						un.pamap[k - 1] =
3239 							phba->alpa_map[j + 1];
3240 						j++;
3241 						if (k == 16)
3242 							break;
3243 					}
3244 					/* Link Up Event ALPA map */
3245 					lpfc_printf_log(phba,
3246 							KERN_WARNING,
3247 							LOG_LINK_EVENT,
3248 							"1304 Link Up Event "
3249 							"ALPA map Data: x%x "
3250 							"x%x x%x x%x\n",
3251 							un.pa.wd1, un.pa.wd2,
3252 							un.pa.wd3, un.pa.wd4);
3253 				}
3254 			}
3255 		}
3256 	} else {
3257 		if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
3258 			if (phba->max_vpi && phba->cfg_enable_npiv &&
3259 			   (phba->sli_rev >= LPFC_SLI_REV3))
3260 				phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3261 		}
3262 		vport->fc_myDID = phba->fc_pref_DID;
3263 		fc_flags |= FC_LBIT;
3264 	}
3265 	spin_unlock_irqrestore(&phba->hbalock, iflags);
3266 
3267 	if (fc_flags) {
3268 		spin_lock_irqsave(shost->host_lock, iflags);
3269 		vport->fc_flag |= fc_flags;
3270 		spin_unlock_irqrestore(shost->host_lock, iflags);
3271 	}
3272 
3273 	lpfc_linkup(phba);
3274 	sparam_mbox = NULL;
3275 
3276 	sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3277 	if (!sparam_mbox)
3278 		goto out;
3279 
3280 	rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3281 	if (rc) {
3282 		mempool_free(sparam_mbox, phba->mbox_mem_pool);
3283 		goto out;
3284 	}
3285 	sparam_mbox->vport = vport;
3286 	sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3287 	rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3288 	if (rc == MBX_NOT_FINISHED) {
3289 		mp = (struct lpfc_dmabuf *)sparam_mbox->ctx_buf;
3290 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
3291 		kfree(mp);
3292 		mempool_free(sparam_mbox, phba->mbox_mem_pool);
3293 		goto out;
3294 	}
3295 
3296 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3297 		cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3298 		if (!cfglink_mbox)
3299 			goto out;
3300 		vport->port_state = LPFC_LOCAL_CFG_LINK;
3301 		lpfc_config_link(phba, cfglink_mbox);
3302 		cfglink_mbox->vport = vport;
3303 		cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
3304 		rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
3305 		if (rc == MBX_NOT_FINISHED) {
3306 			mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3307 			goto out;
3308 		}
3309 	} else {
3310 		vport->port_state = LPFC_VPORT_UNKNOWN;
3311 		/*
3312 		 * Add the driver's default FCF record at FCF index 0 now. This
3313 		 * is phase 1 implementation that support FCF index 0 and driver
3314 		 * defaults.
3315 		 */
3316 		if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
3317 			fcf_record = kzalloc(sizeof(struct fcf_record),
3318 					GFP_KERNEL);
3319 			if (unlikely(!fcf_record)) {
3320 				lpfc_printf_log(phba, KERN_ERR,
3321 					LOG_TRACE_EVENT,
3322 					"2554 Could not allocate memory for "
3323 					"fcf record\n");
3324 				rc = -ENODEV;
3325 				goto out;
3326 			}
3327 
3328 			lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3329 						LPFC_FCOE_FCF_DEF_INDEX);
3330 			rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3331 			if (unlikely(rc)) {
3332 				lpfc_printf_log(phba, KERN_ERR,
3333 					LOG_TRACE_EVENT,
3334 					"2013 Could not manually add FCF "
3335 					"record 0, status %d\n", rc);
3336 				rc = -ENODEV;
3337 				kfree(fcf_record);
3338 				goto out;
3339 			}
3340 			kfree(fcf_record);
3341 		}
3342 		/*
3343 		 * The driver is expected to do FIP/FCF. Call the port
3344 		 * and get the FCF Table.
3345 		 */
3346 		spin_lock_irqsave(&phba->hbalock, iflags);
3347 		if (phba->hba_flag & FCF_TS_INPROG) {
3348 			spin_unlock_irqrestore(&phba->hbalock, iflags);
3349 			return;
3350 		}
3351 		/* This is the initial FCF discovery scan */
3352 		phba->fcf.fcf_flag |= FCF_INIT_DISC;
3353 		spin_unlock_irqrestore(&phba->hbalock, iflags);
3354 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3355 				"2778 Start FCF table scan at linkup\n");
3356 		rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3357 						     LPFC_FCOE_FCF_GET_FIRST);
3358 		if (rc) {
3359 			spin_lock_irqsave(&phba->hbalock, iflags);
3360 			phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3361 			spin_unlock_irqrestore(&phba->hbalock, iflags);
3362 			goto out;
3363 		}
3364 		/* Reset FCF roundrobin bmask for new discovery */
3365 		lpfc_sli4_clear_fcf_rr_bmask(phba);
3366 	}
3367 
3368 	/* Prepare for LINK up registrations */
3369 	memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
3370 	scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
3371 		  init_utsname()->nodename);
3372 	return;
3373 out:
3374 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3375 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3376 			 "0263 Discovery Mailbox error: state: 0x%x : x%px x%px\n",
3377 			 vport->port_state, sparam_mbox, cfglink_mbox);
3378 	lpfc_issue_clear_la(phba, vport);
3379 	return;
3380 }
3381 
3382 static void
3383 lpfc_enable_la(struct lpfc_hba *phba)
3384 {
3385 	uint32_t control;
3386 	struct lpfc_sli *psli = &phba->sli;
3387 	spin_lock_irq(&phba->hbalock);
3388 	psli->sli_flag |= LPFC_PROCESS_LA;
3389 	if (phba->sli_rev <= LPFC_SLI_REV3) {
3390 		control = readl(phba->HCregaddr);
3391 		control |= HC_LAINT_ENA;
3392 		writel(control, phba->HCregaddr);
3393 		readl(phba->HCregaddr); /* flush */
3394 	}
3395 	spin_unlock_irq(&phba->hbalock);
3396 }
3397 
3398 static void
3399 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3400 {
3401 	lpfc_linkdown(phba);
3402 	lpfc_enable_la(phba);
3403 	lpfc_unregister_unused_fcf(phba);
3404 	/* turn on Link Attention interrupts - no CLEAR_LA needed */
3405 }
3406 
3407 
3408 /*
3409  * This routine handles processing a READ_TOPOLOGY mailbox
3410  * command upon completion. It is setup in the LPFC_MBOXQ
3411  * as the completion routine when the command is
3412  * handed off to the SLI layer. SLI4 only.
3413  */
3414 void
3415 lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3416 {
3417 	struct lpfc_vport *vport = pmb->vport;
3418 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3419 	struct lpfc_mbx_read_top *la;
3420 	struct lpfc_sli_ring *pring;
3421 	MAILBOX_t *mb = &pmb->u.mb;
3422 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3423 	uint8_t attn_type;
3424 	unsigned long iflags;
3425 
3426 	/* Unblock ELS traffic */
3427 	pring = lpfc_phba_elsring(phba);
3428 	if (pring)
3429 		pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3430 
3431 	/* Check for error */
3432 	if (mb->mbxStatus) {
3433 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3434 				"1307 READ_LA mbox error x%x state x%x\n",
3435 				mb->mbxStatus, vport->port_state);
3436 		lpfc_mbx_issue_link_down(phba);
3437 		phba->link_state = LPFC_HBA_ERROR;
3438 		goto lpfc_mbx_cmpl_read_topology_free_mbuf;
3439 	}
3440 
3441 	la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3442 	attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
3443 
3444 	memcpy(&phba->alpa_map[0], mp->virt, 128);
3445 
3446 	spin_lock_irqsave(shost->host_lock, iflags);
3447 	if (bf_get(lpfc_mbx_read_top_pb, la))
3448 		vport->fc_flag |= FC_BYPASSED_MODE;
3449 	else
3450 		vport->fc_flag &= ~FC_BYPASSED_MODE;
3451 	spin_unlock_irqrestore(shost->host_lock, iflags);
3452 
3453 	if (phba->fc_eventTag <= la->eventTag) {
3454 		phba->fc_stat.LinkMultiEvent++;
3455 		if (attn_type == LPFC_ATT_LINK_UP)
3456 			if (phba->fc_eventTag != 0)
3457 				lpfc_linkdown(phba);
3458 	}
3459 
3460 	phba->fc_eventTag = la->eventTag;
3461 	if (phba->sli_rev < LPFC_SLI_REV4) {
3462 		spin_lock_irqsave(&phba->hbalock, iflags);
3463 		if (bf_get(lpfc_mbx_read_top_mm, la))
3464 			phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3465 		else
3466 			phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3467 		spin_unlock_irqrestore(&phba->hbalock, iflags);
3468 	}
3469 
3470 	phba->link_events++;
3471 	if ((attn_type == LPFC_ATT_LINK_UP) &&
3472 	    !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
3473 		phba->fc_stat.LinkUp++;
3474 		if (phba->link_flag & LS_LOOPBACK_MODE) {
3475 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3476 					"1306 Link Up Event in loop back mode "
3477 					"x%x received Data: x%x x%x x%x x%x\n",
3478 					la->eventTag, phba->fc_eventTag,
3479 					bf_get(lpfc_mbx_read_top_alpa_granted,
3480 					       la),
3481 					bf_get(lpfc_mbx_read_top_link_spd, la),
3482 					phba->alpa_map[0]);
3483 		} else {
3484 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3485 					"1303 Link Up Event x%x received "
3486 					"Data: x%x x%x x%x x%x x%x x%x %d\n",
3487 					la->eventTag, phba->fc_eventTag,
3488 					bf_get(lpfc_mbx_read_top_alpa_granted,
3489 					       la),
3490 					bf_get(lpfc_mbx_read_top_link_spd, la),
3491 					phba->alpa_map[0],
3492 					bf_get(lpfc_mbx_read_top_mm, la),
3493 					bf_get(lpfc_mbx_read_top_fa, la),
3494 					phba->wait_4_mlo_maint_flg);
3495 		}
3496 		lpfc_mbx_process_link_up(phba, la);
3497 	} else if (attn_type == LPFC_ATT_LINK_DOWN ||
3498 		   attn_type == LPFC_ATT_UNEXP_WWPN) {
3499 		phba->fc_stat.LinkDown++;
3500 		if (phba->link_flag & LS_LOOPBACK_MODE)
3501 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3502 				"1308 Link Down Event in loop back mode "
3503 				"x%x received "
3504 				"Data: x%x x%x x%x\n",
3505 				la->eventTag, phba->fc_eventTag,
3506 				phba->pport->port_state, vport->fc_flag);
3507 		else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3508 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3509 				"1313 Link Down Unexpected FA WWPN Event x%x "
3510 				"received Data: x%x x%x x%x x%x x%x\n",
3511 				la->eventTag, phba->fc_eventTag,
3512 				phba->pport->port_state, vport->fc_flag,
3513 				bf_get(lpfc_mbx_read_top_mm, la),
3514 				bf_get(lpfc_mbx_read_top_fa, la));
3515 		else
3516 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3517 				"1305 Link Down Event x%x received "
3518 				"Data: x%x x%x x%x x%x x%x\n",
3519 				la->eventTag, phba->fc_eventTag,
3520 				phba->pport->port_state, vport->fc_flag,
3521 				bf_get(lpfc_mbx_read_top_mm, la),
3522 				bf_get(lpfc_mbx_read_top_fa, la));
3523 		lpfc_mbx_issue_link_down(phba);
3524 	}
3525 	if (phba->sli.sli_flag & LPFC_MENLO_MAINT &&
3526 	    attn_type == LPFC_ATT_LINK_UP) {
3527 		if (phba->link_state != LPFC_LINK_DOWN) {
3528 			phba->fc_stat.LinkDown++;
3529 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3530 				"1312 Link Down Event x%x received "
3531 				"Data: x%x x%x x%x\n",
3532 				la->eventTag, phba->fc_eventTag,
3533 				phba->pport->port_state, vport->fc_flag);
3534 			lpfc_mbx_issue_link_down(phba);
3535 		} else
3536 			lpfc_enable_la(phba);
3537 
3538 		lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3539 				"1310 Menlo Maint Mode Link up Event x%x rcvd "
3540 				"Data: x%x x%x x%x\n",
3541 				la->eventTag, phba->fc_eventTag,
3542 				phba->pport->port_state, vport->fc_flag);
3543 		/*
3544 		 * The cmnd that triggered this will be waiting for this
3545 		 * signal.
3546 		 */
3547 		/* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3548 		if (phba->wait_4_mlo_maint_flg) {
3549 			phba->wait_4_mlo_maint_flg = 0;
3550 			wake_up_interruptible(&phba->wait_4_mlo_m_q);
3551 		}
3552 	}
3553 
3554 	if ((phba->sli_rev < LPFC_SLI_REV4) &&
3555 	    bf_get(lpfc_mbx_read_top_fa, la)) {
3556 		if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
3557 			lpfc_issue_clear_la(phba, vport);
3558 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3559 				"1311 fa %d\n",
3560 				bf_get(lpfc_mbx_read_top_fa, la));
3561 	}
3562 
3563 lpfc_mbx_cmpl_read_topology_free_mbuf:
3564 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3565 	kfree(mp);
3566 	mempool_free(pmb, phba->mbox_mem_pool);
3567 	return;
3568 }
3569 
3570 /*
3571  * This routine handles processing a REG_LOGIN mailbox
3572  * command upon completion. It is setup in the LPFC_MBOXQ
3573  * as the completion routine when the command is
3574  * handed off to the SLI layer.
3575  */
3576 void
3577 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3578 {
3579 	struct lpfc_vport  *vport = pmb->vport;
3580 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3581 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3582 
3583 	pmb->ctx_buf = NULL;
3584 	pmb->ctx_ndlp = NULL;
3585 
3586 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NODE | LOG_DISCOVERY,
3587 			 "0002 rpi:%x DID:%x flg:%x %d x%px\n",
3588 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3589 			 kref_read(&ndlp->kref),
3590 			 ndlp);
3591 	if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3592 		ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3593 
3594 	if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3595 	    ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
3596 		/* We rcvd a rscn after issuing this
3597 		 * mbox reg login, we may have cycled
3598 		 * back through the state and be
3599 		 * back at reg login state so this
3600 		 * mbox needs to be ignored becase
3601 		 * there is another reg login in
3602 		 * process.
3603 		 */
3604 		spin_lock_irq(&ndlp->lock);
3605 		ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3606 		spin_unlock_irq(&ndlp->lock);
3607 
3608 		/*
3609 		 * We cannot leave the RPI registered because
3610 		 * if we go thru discovery again for this ndlp
3611 		 * a subsequent REG_RPI will fail.
3612 		 */
3613 		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3614 		lpfc_unreg_rpi(vport, ndlp);
3615 	}
3616 
3617 	/* Call state machine */
3618 	lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
3619 
3620 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3621 	kfree(mp);
3622 	mempool_free(pmb, phba->mbox_mem_pool);
3623 	/* decrement the node reference count held for this callback
3624 	 * function.
3625 	 */
3626 	lpfc_nlp_put(ndlp);
3627 
3628 	return;
3629 }
3630 
3631 static void
3632 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3633 {
3634 	MAILBOX_t *mb = &pmb->u.mb;
3635 	struct lpfc_vport *vport = pmb->vport;
3636 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3637 
3638 	switch (mb->mbxStatus) {
3639 	case 0x0011:
3640 	case 0x0020:
3641 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3642 				 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3643 				 mb->mbxStatus);
3644 		break;
3645 	/* If VPI is busy, reset the HBA */
3646 	case 0x9700:
3647 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3648 			"2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3649 			vport->vpi, mb->mbxStatus);
3650 		if (!(phba->pport->load_flag & FC_UNLOADING))
3651 			lpfc_workq_post_event(phba, NULL, NULL,
3652 				LPFC_EVT_RESET_HBA);
3653 	}
3654 	spin_lock_irq(shost->host_lock);
3655 	vport->vpi_state &= ~LPFC_VPI_REGISTERED;
3656 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3657 	spin_unlock_irq(shost->host_lock);
3658 	vport->unreg_vpi_cmpl = VPORT_OK;
3659 	mempool_free(pmb, phba->mbox_mem_pool);
3660 	lpfc_cleanup_vports_rrqs(vport, NULL);
3661 	/*
3662 	 * This shost reference might have been taken at the beginning of
3663 	 * lpfc_vport_delete()
3664 	 */
3665 	if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
3666 		scsi_host_put(shost);
3667 }
3668 
3669 int
3670 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3671 {
3672 	struct lpfc_hba  *phba = vport->phba;
3673 	LPFC_MBOXQ_t *mbox;
3674 	int rc;
3675 
3676 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3677 	if (!mbox)
3678 		return 1;
3679 
3680 	lpfc_unreg_vpi(phba, vport->vpi, mbox);
3681 	mbox->vport = vport;
3682 	mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
3683 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3684 	if (rc == MBX_NOT_FINISHED) {
3685 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3686 				 "1800 Could not issue unreg_vpi\n");
3687 		mempool_free(mbox, phba->mbox_mem_pool);
3688 		vport->unreg_vpi_cmpl = VPORT_ERROR;
3689 		return rc;
3690 	}
3691 	return 0;
3692 }
3693 
3694 static void
3695 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3696 {
3697 	struct lpfc_vport *vport = pmb->vport;
3698 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3699 	MAILBOX_t *mb = &pmb->u.mb;
3700 
3701 	switch (mb->mbxStatus) {
3702 	case 0x0011:
3703 	case 0x9601:
3704 	case 0x9602:
3705 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3706 				 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3707 				 mb->mbxStatus);
3708 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3709 		spin_lock_irq(shost->host_lock);
3710 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3711 		spin_unlock_irq(shost->host_lock);
3712 		vport->fc_myDID = 0;
3713 
3714 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3715 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3716 			if (phba->nvmet_support)
3717 				lpfc_nvmet_update_targetport(phba);
3718 			else
3719 				lpfc_nvme_update_localport(vport);
3720 		}
3721 		goto out;
3722 	}
3723 
3724 	spin_lock_irq(shost->host_lock);
3725 	vport->vpi_state |= LPFC_VPI_REGISTERED;
3726 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
3727 	spin_unlock_irq(shost->host_lock);
3728 	vport->num_disc_nodes = 0;
3729 	/* go thru NPR list and issue ELS PLOGIs */
3730 	if (vport->fc_npr_cnt)
3731 		lpfc_els_disc_plogi(vport);
3732 
3733 	if (!vport->num_disc_nodes) {
3734 		spin_lock_irq(shost->host_lock);
3735 		vport->fc_flag &= ~FC_NDISC_ACTIVE;
3736 		spin_unlock_irq(shost->host_lock);
3737 		lpfc_can_disctmo(vport);
3738 	}
3739 	vport->port_state = LPFC_VPORT_READY;
3740 
3741 out:
3742 	mempool_free(pmb, phba->mbox_mem_pool);
3743 	return;
3744 }
3745 
3746 /**
3747  * lpfc_create_static_vport - Read HBA config region to create static vports.
3748  * @phba: pointer to lpfc hba data structure.
3749  *
3750  * This routine issue a DUMP mailbox command for config region 22 to get
3751  * the list of static vports to be created. The function create vports
3752  * based on the information returned from the HBA.
3753  **/
3754 void
3755 lpfc_create_static_vport(struct lpfc_hba *phba)
3756 {
3757 	LPFC_MBOXQ_t *pmb = NULL;
3758 	MAILBOX_t *mb;
3759 	struct static_vport_info *vport_info;
3760 	int mbx_wait_rc = 0, i;
3761 	struct fc_vport_identifiers vport_id;
3762 	struct fc_vport *new_fc_vport;
3763 	struct Scsi_Host *shost;
3764 	struct lpfc_vport *vport;
3765 	uint16_t offset = 0;
3766 	uint8_t *vport_buff;
3767 	struct lpfc_dmabuf *mp;
3768 	uint32_t byte_count = 0;
3769 
3770 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3771 	if (!pmb) {
3772 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3773 				"0542 lpfc_create_static_vport failed to"
3774 				" allocate mailbox memory\n");
3775 		return;
3776 	}
3777 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
3778 	mb = &pmb->u.mb;
3779 
3780 	vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3781 	if (!vport_info) {
3782 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3783 				"0543 lpfc_create_static_vport failed to"
3784 				" allocate vport_info\n");
3785 		mempool_free(pmb, phba->mbox_mem_pool);
3786 		return;
3787 	}
3788 
3789 	vport_buff = (uint8_t *) vport_info;
3790 	do {
3791 		/* free dma buffer from previous round */
3792 		if (pmb->ctx_buf) {
3793 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3794 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
3795 			kfree(mp);
3796 		}
3797 		if (lpfc_dump_static_vport(phba, pmb, offset))
3798 			goto out;
3799 
3800 		pmb->vport = phba->pport;
3801 		mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3802 							LPFC_MBOX_TMO);
3803 
3804 		if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
3805 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3806 				"0544 lpfc_create_static_vport failed to"
3807 				" issue dump mailbox command ret 0x%x "
3808 				"status 0x%x\n",
3809 				mbx_wait_rc, mb->mbxStatus);
3810 			goto out;
3811 		}
3812 
3813 		if (phba->sli_rev == LPFC_SLI_REV4) {
3814 			byte_count = pmb->u.mqe.un.mb_words[5];
3815 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3816 			if (byte_count > sizeof(struct static_vport_info) -
3817 					offset)
3818 				byte_count = sizeof(struct static_vport_info)
3819 					- offset;
3820 			memcpy(vport_buff + offset, mp->virt, byte_count);
3821 			offset += byte_count;
3822 		} else {
3823 			if (mb->un.varDmp.word_cnt >
3824 				sizeof(struct static_vport_info) - offset)
3825 				mb->un.varDmp.word_cnt =
3826 					sizeof(struct static_vport_info)
3827 						- offset;
3828 			byte_count = mb->un.varDmp.word_cnt;
3829 			lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3830 				vport_buff + offset,
3831 				byte_count);
3832 
3833 			offset += byte_count;
3834 		}
3835 
3836 	} while (byte_count &&
3837 		offset < sizeof(struct static_vport_info));
3838 
3839 
3840 	if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3841 		((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3842 			!= VPORT_INFO_REV)) {
3843 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3844 				"0545 lpfc_create_static_vport bad"
3845 				" information header 0x%x 0x%x\n",
3846 				le32_to_cpu(vport_info->signature),
3847 				le32_to_cpu(vport_info->rev) &
3848 				VPORT_INFO_REV_MASK);
3849 
3850 		goto out;
3851 	}
3852 
3853 	shost = lpfc_shost_from_vport(phba->pport);
3854 
3855 	for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3856 		memset(&vport_id, 0, sizeof(vport_id));
3857 		vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3858 		vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3859 		if (!vport_id.port_name || !vport_id.node_name)
3860 			continue;
3861 
3862 		vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3863 		vport_id.vport_type = FC_PORTTYPE_NPIV;
3864 		vport_id.disable = false;
3865 		new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3866 
3867 		if (!new_fc_vport) {
3868 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3869 				"0546 lpfc_create_static_vport failed to"
3870 				" create vport\n");
3871 			continue;
3872 		}
3873 
3874 		vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3875 		vport->vport_flag |= STATIC_VPORT;
3876 	}
3877 
3878 out:
3879 	kfree(vport_info);
3880 	if (mbx_wait_rc != MBX_TIMEOUT) {
3881 		if (pmb->ctx_buf) {
3882 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3883 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
3884 			kfree(mp);
3885 		}
3886 		mempool_free(pmb, phba->mbox_mem_pool);
3887 	}
3888 
3889 	return;
3890 }
3891 
3892 /*
3893  * This routine handles processing a Fabric REG_LOGIN mailbox
3894  * command upon completion. It is setup in the LPFC_MBOXQ
3895  * as the completion routine when the command is
3896  * handed off to the SLI layer.
3897  */
3898 void
3899 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3900 {
3901 	struct lpfc_vport *vport = pmb->vport;
3902 	MAILBOX_t *mb = &pmb->u.mb;
3903 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3904 	struct lpfc_nodelist *ndlp;
3905 	struct Scsi_Host *shost;
3906 
3907 	ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3908 	pmb->ctx_ndlp = NULL;
3909 	pmb->ctx_buf = NULL;
3910 
3911 	if (mb->mbxStatus) {
3912 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3913 				 "0258 Register Fabric login error: 0x%x\n",
3914 				 mb->mbxStatus);
3915 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
3916 		kfree(mp);
3917 		mempool_free(pmb, phba->mbox_mem_pool);
3918 
3919 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3920 			/* FLOGI failed, use loop map to make discovery list */
3921 			lpfc_disc_list_loopmap(vport);
3922 
3923 			/* Start discovery */
3924 			lpfc_disc_start(vport);
3925 			/* Decrement the reference count to ndlp after the
3926 			 * reference to the ndlp are done.
3927 			 */
3928 			lpfc_nlp_put(ndlp);
3929 			return;
3930 		}
3931 
3932 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3933 		/* Decrement the reference count to ndlp after the reference
3934 		 * to the ndlp are done.
3935 		 */
3936 		lpfc_nlp_put(ndlp);
3937 		return;
3938 	}
3939 
3940 	if (phba->sli_rev < LPFC_SLI_REV4)
3941 		ndlp->nlp_rpi = mb->un.varWords[0];
3942 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3943 	ndlp->nlp_type |= NLP_FABRIC;
3944 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
3945 
3946 	if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
3947 		/* when physical port receive logo donot start
3948 		 * vport discovery */
3949 		if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3950 			lpfc_start_fdiscs(phba);
3951 		else {
3952 			shost = lpfc_shost_from_vport(vport);
3953 			spin_lock_irq(shost->host_lock);
3954 			vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
3955 			spin_unlock_irq(shost->host_lock);
3956 		}
3957 		lpfc_do_scr_ns_plogi(phba, vport);
3958 	}
3959 
3960 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3961 	kfree(mp);
3962 	mempool_free(pmb, phba->mbox_mem_pool);
3963 
3964 	/* Drop the reference count from the mbox at the end after
3965 	 * all the current reference to the ndlp have been done.
3966 	 */
3967 	lpfc_nlp_put(ndlp);
3968 	return;
3969 }
3970 
3971  /*
3972   * This routine will issue a GID_FT for each FC4 Type supported
3973   * by the driver. ALL GID_FTs must complete before discovery is started.
3974   */
3975 int
3976 lpfc_issue_gidft(struct lpfc_vport *vport)
3977 {
3978 	/* Good status, issue CT Request to NameServer */
3979 	if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3980 	    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3981 		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3982 			/* Cannot issue NameServer FCP Query, so finish up
3983 			 * discovery
3984 			 */
3985 			lpfc_printf_vlog(vport, KERN_ERR,
3986 					 LOG_TRACE_EVENT,
3987 					 "0604 %s FC TYPE %x %s\n",
3988 					 "Failed to issue GID_FT to ",
3989 					 FC_TYPE_FCP,
3990 					 "Finishing discovery.");
3991 			return 0;
3992 		}
3993 		vport->gidft_inp++;
3994 	}
3995 
3996 	if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3997 	    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3998 		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3999 			/* Cannot issue NameServer NVME Query, so finish up
4000 			 * discovery
4001 			 */
4002 			lpfc_printf_vlog(vport, KERN_ERR,
4003 					 LOG_TRACE_EVENT,
4004 					 "0605 %s FC_TYPE %x %s %d\n",
4005 					 "Failed to issue GID_FT to ",
4006 					 FC_TYPE_NVME,
4007 					 "Finishing discovery: gidftinp ",
4008 					 vport->gidft_inp);
4009 			if (vport->gidft_inp == 0)
4010 				return 0;
4011 		} else
4012 			vport->gidft_inp++;
4013 	}
4014 	return vport->gidft_inp;
4015 }
4016 
4017 /**
4018  * lpfc_issue_gidpt - issue a GID_PT for all N_Ports
4019  * @vport: The virtual port for which this call is being executed.
4020  *
4021  * This routine will issue a GID_PT to get a list of all N_Ports
4022  *
4023  * Return value :
4024  *   0 - Failure to issue a GID_PT
4025  *   1 - GID_PT issued
4026  **/
4027 int
4028 lpfc_issue_gidpt(struct lpfc_vport *vport)
4029 {
4030 	/* Good status, issue CT Request to NameServer */
4031 	if (lpfc_ns_cmd(vport, SLI_CTNS_GID_PT, 0, GID_PT_N_PORT)) {
4032 		/* Cannot issue NameServer FCP Query, so finish up
4033 		 * discovery
4034 		 */
4035 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4036 				 "0606 %s Port TYPE %x %s\n",
4037 				 "Failed to issue GID_PT to ",
4038 				 GID_PT_N_PORT,
4039 				 "Finishing discovery.");
4040 		return 0;
4041 	}
4042 	vport->gidft_inp++;
4043 	return 1;
4044 }
4045 
4046 /*
4047  * This routine handles processing a NameServer REG_LOGIN mailbox
4048  * command upon completion. It is setup in the LPFC_MBOXQ
4049  * as the completion routine when the command is
4050  * handed off to the SLI layer.
4051  */
4052 void
4053 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4054 {
4055 	MAILBOX_t *mb = &pmb->u.mb;
4056 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4057 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4058 	struct lpfc_vport *vport = pmb->vport;
4059 
4060 	pmb->ctx_buf = NULL;
4061 	pmb->ctx_ndlp = NULL;
4062 	vport->gidft_inp = 0;
4063 
4064 	if (mb->mbxStatus) {
4065 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4066 				 "0260 Register NameServer error: 0x%x\n",
4067 				 mb->mbxStatus);
4068 
4069 out:
4070 		/* decrement the node reference count held for this
4071 		 * callback function.
4072 		 */
4073 		lpfc_nlp_put(ndlp);
4074 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
4075 		kfree(mp);
4076 		mempool_free(pmb, phba->mbox_mem_pool);
4077 
4078 		/* If the node is not registered with the scsi or nvme
4079 		 * transport, remove the fabric node.  The failed reg_login
4080 		 * is terminal.
4081 		 */
4082 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
4083 			spin_lock_irq(&ndlp->lock);
4084 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
4085 			spin_unlock_irq(&ndlp->lock);
4086 			lpfc_nlp_not_used(ndlp);
4087 		}
4088 
4089 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4090 			/*
4091 			 * RegLogin failed, use loop map to make discovery
4092 			 * list
4093 			 */
4094 			lpfc_disc_list_loopmap(vport);
4095 
4096 			/* Start discovery */
4097 			lpfc_disc_start(vport);
4098 			return;
4099 		}
4100 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4101 		return;
4102 	}
4103 
4104 	if (phba->sli_rev < LPFC_SLI_REV4)
4105 		ndlp->nlp_rpi = mb->un.varWords[0];
4106 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4107 	ndlp->nlp_type |= NLP_FABRIC;
4108 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4109 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
4110 			 "0003 rpi:%x DID:%x flg:%x %d x%px\n",
4111 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4112 			 kref_read(&ndlp->kref),
4113 			 ndlp);
4114 
4115 	if (vport->port_state < LPFC_VPORT_READY) {
4116 		/* Link up discovery requires Fabric registration. */
4117 		lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
4118 		lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
4119 		lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
4120 		lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
4121 
4122 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4123 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
4124 			lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
4125 
4126 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4127 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
4128 			lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
4129 				    FC_TYPE_NVME);
4130 
4131 		/* Issue SCR just before NameServer GID_FT Query */
4132 		lpfc_issue_els_scr(vport, 0);
4133 
4134 		if (!phba->cfg_enable_mi ||
4135 		    phba->sli4_hba.pc_sli4_params.mi_ver < LPFC_MIB3_SUPPORT)
4136 			lpfc_issue_els_rdf(vport, 0);
4137 	}
4138 
4139 	vport->fc_ns_retry = 0;
4140 	if (lpfc_issue_gidft(vport) == 0)
4141 		goto out;
4142 
4143 	/*
4144 	 * At this point in time we may need to wait for multiple
4145 	 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
4146 	 *
4147 	 * decrement the node reference count held for this
4148 	 * callback function.
4149 	 */
4150 	lpfc_nlp_put(ndlp);
4151 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
4152 	kfree(mp);
4153 	mempool_free(pmb, phba->mbox_mem_pool);
4154 
4155 	return;
4156 }
4157 
4158 static void
4159 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4160 {
4161 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4162 	struct fc_rport  *rport;
4163 	struct lpfc_rport_data *rdata;
4164 	struct fc_rport_identifiers rport_ids;
4165 	struct lpfc_hba  *phba = vport->phba;
4166 	unsigned long flags;
4167 
4168 	if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4169 		return;
4170 
4171 	/* Remote port has reappeared. Re-register w/ FC transport */
4172 	rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4173 	rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
4174 	rport_ids.port_id = ndlp->nlp_DID;
4175 	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
4176 
4177 
4178 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4179 			      "rport add:       did:x%x flg:x%x type x%x",
4180 			      ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4181 
4182 	/* Don't add the remote port if unloading. */
4183 	if (vport->load_flag & FC_UNLOADING)
4184 		return;
4185 
4186 	/*
4187 	 * Disassociate any older association between this ndlp and rport
4188 	 */
4189 	if (ndlp->rport) {
4190 		rdata = ndlp->rport->dd_data;
4191 		rdata->pnode = NULL;
4192 	}
4193 
4194 	ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
4195 	if (!rport) {
4196 		dev_printk(KERN_WARNING, &phba->pcidev->dev,
4197 			   "Warning: fc_remote_port_add failed\n");
4198 		return;
4199 	}
4200 
4201 	/* Successful port add.  Complete initializing node data */
4202 	rport->maxframe_size = ndlp->nlp_maxframe;
4203 	rport->supported_classes = ndlp->nlp_class_sup;
4204 	rdata = rport->dd_data;
4205 	rdata->pnode = lpfc_nlp_get(ndlp);
4206 	if (!rdata->pnode) {
4207 		dev_warn(&phba->pcidev->dev,
4208 			 "Warning - node ref failed. Unreg rport\n");
4209 		fc_remote_port_delete(rport);
4210 		ndlp->rport = NULL;
4211 		return;
4212 	}
4213 
4214 	spin_lock_irqsave(&ndlp->lock, flags);
4215 	ndlp->fc4_xpt_flags |= SCSI_XPT_REGD;
4216 	spin_unlock_irqrestore(&ndlp->lock, flags);
4217 
4218 	if (ndlp->nlp_type & NLP_FCP_TARGET)
4219 		rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
4220 	if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4221 		rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4222 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
4223 		rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
4224 	if (ndlp->nlp_type & NLP_NVME_TARGET)
4225 		rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
4226 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
4227 		rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
4228 
4229 	if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
4230 		fc_remote_port_rolechg(rport, rport_ids.roles);
4231 
4232 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4233 			 "3183 %s rport x%px DID x%x, role x%x\n",
4234 			 __func__, rport, rport->port_id, rport->roles);
4235 
4236 	if ((rport->scsi_target_id != -1) &&
4237 	    (rport->scsi_target_id < LPFC_MAX_TARGET)) {
4238 		ndlp->nlp_sid = rport->scsi_target_id;
4239 	}
4240 
4241 	return;
4242 }
4243 
4244 static void
4245 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
4246 {
4247 	struct fc_rport *rport = ndlp->rport;
4248 	struct lpfc_vport *vport = ndlp->vport;
4249 
4250 	if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4251 		return;
4252 
4253 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4254 		"rport delete:    did:x%x flg:x%x type x%x",
4255 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4256 
4257 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4258 			 "3184 rport unregister x%06x, rport x%px "
4259 			 "xptflg x%x\n",
4260 			 ndlp->nlp_DID, rport, ndlp->fc4_xpt_flags);
4261 
4262 	fc_remote_port_delete(rport);
4263 	lpfc_nlp_put(ndlp);
4264 }
4265 
4266 static void
4267 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
4268 {
4269 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4270 	unsigned long iflags;
4271 
4272 	spin_lock_irqsave(shost->host_lock, iflags);
4273 	switch (state) {
4274 	case NLP_STE_UNUSED_NODE:
4275 		vport->fc_unused_cnt += count;
4276 		break;
4277 	case NLP_STE_PLOGI_ISSUE:
4278 		vport->fc_plogi_cnt += count;
4279 		break;
4280 	case NLP_STE_ADISC_ISSUE:
4281 		vport->fc_adisc_cnt += count;
4282 		break;
4283 	case NLP_STE_REG_LOGIN_ISSUE:
4284 		vport->fc_reglogin_cnt += count;
4285 		break;
4286 	case NLP_STE_PRLI_ISSUE:
4287 		vport->fc_prli_cnt += count;
4288 		break;
4289 	case NLP_STE_UNMAPPED_NODE:
4290 		vport->fc_unmap_cnt += count;
4291 		break;
4292 	case NLP_STE_MAPPED_NODE:
4293 		vport->fc_map_cnt += count;
4294 		break;
4295 	case NLP_STE_NPR_NODE:
4296 		if (vport->fc_npr_cnt == 0 && count == -1)
4297 			vport->fc_npr_cnt = 0;
4298 		else
4299 			vport->fc_npr_cnt += count;
4300 		break;
4301 	}
4302 	spin_unlock_irqrestore(shost->host_lock, iflags);
4303 }
4304 
4305 static void
4306 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4307 		       int old_state, int new_state)
4308 {
4309 	if (new_state == NLP_STE_UNMAPPED_NODE) {
4310 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4311 		ndlp->nlp_type |= NLP_FC_NODE;
4312 	}
4313 	if (new_state == NLP_STE_MAPPED_NODE)
4314 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4315 	if (new_state == NLP_STE_NPR_NODE)
4316 		ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4317 
4318 	/* FCP and NVME Transport interface */
4319 	if ((old_state == NLP_STE_MAPPED_NODE ||
4320 	     old_state == NLP_STE_UNMAPPED_NODE)) {
4321 		if (ndlp->rport) {
4322 			vport->phba->nport_event_cnt++;
4323 			lpfc_unregister_remote_port(ndlp);
4324 		}
4325 
4326 		if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4327 			vport->phba->nport_event_cnt++;
4328 			if (vport->phba->nvmet_support == 0) {
4329 				/* Start devloss if target. */
4330 				if (ndlp->nlp_type & NLP_NVME_TARGET)
4331 					lpfc_nvme_unregister_port(vport, ndlp);
4332 			} else {
4333 				/* NVMET has no upcall. */
4334 				lpfc_nlp_put(ndlp);
4335 			}
4336 		}
4337 	}
4338 
4339 	/* FCP and NVME Transport interfaces */
4340 
4341 	if (new_state ==  NLP_STE_MAPPED_NODE ||
4342 	    new_state == NLP_STE_UNMAPPED_NODE) {
4343 		if (ndlp->nlp_fc4_type ||
4344 		    ndlp->nlp_DID == Fabric_DID ||
4345 		    ndlp->nlp_DID == NameServer_DID ||
4346 		    ndlp->nlp_DID == FDMI_DID) {
4347 			vport->phba->nport_event_cnt++;
4348 			/*
4349 			 * Tell the fc transport about the port, if we haven't
4350 			 * already. If we have, and it's a scsi entity, be
4351 			 */
4352 			lpfc_register_remote_port(vport, ndlp);
4353 		}
4354 		/* Notify the NVME transport of this new rport. */
4355 		if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4356 		    ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4357 			if (vport->phba->nvmet_support == 0) {
4358 				/* Register this rport with the transport.
4359 				 * Only NVME Target Rports are registered with
4360 				 * the transport.
4361 				 */
4362 				if (ndlp->nlp_type & NLP_NVME_TARGET) {
4363 					vport->phba->nport_event_cnt++;
4364 					lpfc_nvme_register_port(vport, ndlp);
4365 				}
4366 			} else {
4367 				/* Just take an NDLP ref count since the
4368 				 * target does not register rports.
4369 				 */
4370 				lpfc_nlp_get(ndlp);
4371 			}
4372 		}
4373 	}
4374 
4375 	if ((new_state ==  NLP_STE_MAPPED_NODE) &&
4376 		(vport->stat_data_enabled)) {
4377 		/*
4378 		 * A new target is discovered, if there is no buffer for
4379 		 * statistical data collection allocate buffer.
4380 		 */
4381 		ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4382 					 sizeof(struct lpfc_scsicmd_bkt),
4383 					 GFP_KERNEL);
4384 
4385 		if (!ndlp->lat_data)
4386 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4387 				"0286 lpfc_nlp_state_cleanup failed to "
4388 				"allocate statistical data buffer DID "
4389 				"0x%x\n", ndlp->nlp_DID);
4390 	}
4391 	/*
4392 	 * If the node just added to Mapped list was an FCP target,
4393 	 * but the remote port registration failed or assigned a target
4394 	 * id outside the presentable range - move the node to the
4395 	 * Unmapped List.
4396 	 */
4397 	if ((new_state == NLP_STE_MAPPED_NODE) &&
4398 	    (ndlp->nlp_type & NLP_FCP_TARGET) &&
4399 	    (!ndlp->rport ||
4400 	     ndlp->rport->scsi_target_id == -1 ||
4401 	     ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
4402 		spin_lock_irq(&ndlp->lock);
4403 		ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
4404 		spin_unlock_irq(&ndlp->lock);
4405 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4406 	}
4407 }
4408 
4409 static char *
4410 lpfc_nlp_state_name(char *buffer, size_t size, int state)
4411 {
4412 	static char *states[] = {
4413 		[NLP_STE_UNUSED_NODE] = "UNUSED",
4414 		[NLP_STE_PLOGI_ISSUE] = "PLOGI",
4415 		[NLP_STE_ADISC_ISSUE] = "ADISC",
4416 		[NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4417 		[NLP_STE_PRLI_ISSUE] = "PRLI",
4418 		[NLP_STE_LOGO_ISSUE] = "LOGO",
4419 		[NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4420 		[NLP_STE_MAPPED_NODE] = "MAPPED",
4421 		[NLP_STE_NPR_NODE] = "NPR",
4422 	};
4423 
4424 	if (state < NLP_STE_MAX_STATE && states[state])
4425 		strlcpy(buffer, states[state], size);
4426 	else
4427 		snprintf(buffer, size, "unknown (%d)", state);
4428 	return buffer;
4429 }
4430 
4431 void
4432 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4433 		   int state)
4434 {
4435 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4436 	int  old_state = ndlp->nlp_state;
4437 	int node_dropped = ndlp->nlp_flag & NLP_DROPPED;
4438 	char name1[16], name2[16];
4439 
4440 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4441 			 "0904 NPort state transition x%06x, %s -> %s\n",
4442 			 ndlp->nlp_DID,
4443 			 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4444 			 lpfc_nlp_state_name(name2, sizeof(name2), state));
4445 
4446 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4447 		"node statechg    did:x%x old:%d ste:%d",
4448 		ndlp->nlp_DID, old_state, state);
4449 
4450 	if (node_dropped && old_state == NLP_STE_UNUSED_NODE &&
4451 	    state != NLP_STE_UNUSED_NODE) {
4452 		ndlp->nlp_flag &= ~NLP_DROPPED;
4453 		lpfc_nlp_get(ndlp);
4454 	}
4455 
4456 	if (old_state == NLP_STE_NPR_NODE &&
4457 	    state != NLP_STE_NPR_NODE)
4458 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
4459 	if (old_state == NLP_STE_UNMAPPED_NODE) {
4460 		ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4461 		ndlp->nlp_type &= ~NLP_FC_NODE;
4462 	}
4463 
4464 	if (list_empty(&ndlp->nlp_listp)) {
4465 		spin_lock_irq(shost->host_lock);
4466 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4467 		spin_unlock_irq(shost->host_lock);
4468 	} else if (old_state)
4469 		lpfc_nlp_counters(vport, old_state, -1);
4470 
4471 	ndlp->nlp_state = state;
4472 	lpfc_nlp_counters(vport, state, 1);
4473 	lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
4474 }
4475 
4476 void
4477 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4478 {
4479 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4480 
4481 	if (list_empty(&ndlp->nlp_listp)) {
4482 		spin_lock_irq(shost->host_lock);
4483 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4484 		spin_unlock_irq(shost->host_lock);
4485 	}
4486 }
4487 
4488 void
4489 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4490 {
4491 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4492 
4493 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
4494 	if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4495 		lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4496 	spin_lock_irq(shost->host_lock);
4497 	list_del_init(&ndlp->nlp_listp);
4498 	spin_unlock_irq(shost->host_lock);
4499 	lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4500 				NLP_STE_UNUSED_NODE);
4501 }
4502 
4503 /**
4504  * lpfc_initialize_node - Initialize all fields of node object
4505  * @vport: Pointer to Virtual Port object.
4506  * @ndlp: Pointer to FC node object.
4507  * @did: FC_ID of the node.
4508  *
4509  * This function is always called when node object need to be initialized.
4510  * It initializes all the fields of the node object. Although the reference
4511  * to phba from @ndlp can be obtained indirectly through it's reference to
4512  * @vport, a direct reference to phba is taken here by @ndlp. This is due
4513  * to the life-span of the @ndlp might go beyond the existence of @vport as
4514  * the final release of ndlp is determined by its reference count. And, the
4515  * operation on @ndlp needs the reference to phba.
4516  **/
4517 static inline void
4518 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4519 	uint32_t did)
4520 {
4521 	INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4522 	INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4523 	timer_setup(&ndlp->nlp_delayfunc, lpfc_els_retry_delay, 0);
4524 	INIT_LIST_HEAD(&ndlp->recovery_evt.evt_listp);
4525 
4526 	ndlp->nlp_DID = did;
4527 	ndlp->vport = vport;
4528 	ndlp->phba = vport->phba;
4529 	ndlp->nlp_sid = NLP_NO_SID;
4530 	ndlp->nlp_fc4_type = NLP_FC4_NONE;
4531 	kref_init(&ndlp->kref);
4532 	atomic_set(&ndlp->cmd_pending, 0);
4533 	ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4534 	ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4535 }
4536 
4537 void
4538 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4539 {
4540 	/*
4541 	 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
4542 	 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
4543 	 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4544 	 * until ALL other outstanding threads have completed. We check
4545 	 * that the ndlp not already in the UNUSED state before we proceed.
4546 	 */
4547 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4548 		return;
4549 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
4550 	ndlp->nlp_flag |= NLP_DROPPED;
4551 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4552 		lpfc_cleanup_vports_rrqs(vport, ndlp);
4553 		lpfc_unreg_rpi(vport, ndlp);
4554 	}
4555 
4556 	lpfc_nlp_put(ndlp);
4557 	return;
4558 }
4559 
4560 /*
4561  * Start / ReStart rescue timer for Discovery / RSCN handling
4562  */
4563 void
4564 lpfc_set_disctmo(struct lpfc_vport *vport)
4565 {
4566 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4567 	struct lpfc_hba  *phba = vport->phba;
4568 	uint32_t tmo;
4569 
4570 	if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
4571 		/* For FAN, timeout should be greater than edtov */
4572 		tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4573 	} else {
4574 		/* Normal discovery timeout should be > than ELS/CT timeout
4575 		 * FC spec states we need 3 * ratov for CT requests
4576 		 */
4577 		tmo = ((phba->fc_ratov * 3) + 3);
4578 	}
4579 
4580 
4581 	if (!timer_pending(&vport->fc_disctmo)) {
4582 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4583 			"set disc timer:  tmo:x%x state:x%x flg:x%x",
4584 			tmo, vport->port_state, vport->fc_flag);
4585 	}
4586 
4587 	mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
4588 	spin_lock_irq(shost->host_lock);
4589 	vport->fc_flag |= FC_DISC_TMO;
4590 	spin_unlock_irq(shost->host_lock);
4591 
4592 	/* Start Discovery Timer state <hba_state> */
4593 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4594 			 "0247 Start Discovery Timer state x%x "
4595 			 "Data: x%x x%lx x%x x%x\n",
4596 			 vport->port_state, tmo,
4597 			 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4598 			 vport->fc_adisc_cnt);
4599 
4600 	return;
4601 }
4602 
4603 /*
4604  * Cancel rescue timer for Discovery / RSCN handling
4605  */
4606 int
4607 lpfc_can_disctmo(struct lpfc_vport *vport)
4608 {
4609 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4610 	unsigned long iflags;
4611 
4612 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4613 		"can disc timer:  state:x%x rtry:x%x flg:x%x",
4614 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4615 
4616 	/* Turn off discovery timer if its running */
4617 	if (vport->fc_flag & FC_DISC_TMO) {
4618 		spin_lock_irqsave(shost->host_lock, iflags);
4619 		vport->fc_flag &= ~FC_DISC_TMO;
4620 		spin_unlock_irqrestore(shost->host_lock, iflags);
4621 		del_timer_sync(&vport->fc_disctmo);
4622 		spin_lock_irqsave(&vport->work_port_lock, iflags);
4623 		vport->work_port_events &= ~WORKER_DISC_TMO;
4624 		spin_unlock_irqrestore(&vport->work_port_lock, iflags);
4625 	}
4626 
4627 	/* Cancel Discovery Timer state <hba_state> */
4628 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4629 			 "0248 Cancel Discovery Timer state x%x "
4630 			 "Data: x%x x%x x%x\n",
4631 			 vport->port_state, vport->fc_flag,
4632 			 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
4633 	return 0;
4634 }
4635 
4636 /*
4637  * Check specified ring for outstanding IOCB on the SLI queue
4638  * Return true if iocb matches the specified nport
4639  */
4640 int
4641 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4642 		    struct lpfc_sli_ring *pring,
4643 		    struct lpfc_iocbq *iocb,
4644 		    struct lpfc_nodelist *ndlp)
4645 {
4646 	IOCB_t *icmd = &iocb->iocb;
4647 	struct lpfc_vport    *vport = ndlp->vport;
4648 
4649 	if (iocb->vport != vport)
4650 		return 0;
4651 
4652 	if (pring->ringno == LPFC_ELS_RING) {
4653 		switch (icmd->ulpCommand) {
4654 		case CMD_GEN_REQUEST64_CR:
4655 			if (iocb->context_un.ndlp == ndlp)
4656 				return 1;
4657 			fallthrough;
4658 		case CMD_ELS_REQUEST64_CR:
4659 			if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4660 				return 1;
4661 			fallthrough;
4662 		case CMD_XMIT_ELS_RSP64_CX:
4663 			if (iocb->context1 == (uint8_t *) ndlp)
4664 				return 1;
4665 		}
4666 	} else if (pring->ringno == LPFC_FCP_RING) {
4667 		/* Skip match check if waiting to relogin to FCP target */
4668 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4669 		    (ndlp->nlp_flag & NLP_DELAY_TMO)) {
4670 			return 0;
4671 		}
4672 		if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
4673 			return 1;
4674 		}
4675 	}
4676 	return 0;
4677 }
4678 
4679 static void
4680 __lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4681 		struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4682 		struct list_head *dequeue_list)
4683 {
4684 	struct lpfc_iocbq *iocb, *next_iocb;
4685 
4686 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4687 		/* Check to see if iocb matches the nport */
4688 		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4689 			/* match, dequeue */
4690 			list_move_tail(&iocb->list, dequeue_list);
4691 	}
4692 }
4693 
4694 static void
4695 lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4696 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4697 {
4698 	struct lpfc_sli *psli = &phba->sli;
4699 	uint32_t i;
4700 
4701 	spin_lock_irq(&phba->hbalock);
4702 	for (i = 0; i < psli->num_rings; i++)
4703 		__lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4704 						dequeue_list);
4705 	spin_unlock_irq(&phba->hbalock);
4706 }
4707 
4708 static void
4709 lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4710 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4711 {
4712 	struct lpfc_sli_ring *pring;
4713 	struct lpfc_queue *qp = NULL;
4714 
4715 	spin_lock_irq(&phba->hbalock);
4716 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4717 		pring = qp->pring;
4718 		if (!pring)
4719 			continue;
4720 		spin_lock(&pring->ring_lock);
4721 		__lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
4722 		spin_unlock(&pring->ring_lock);
4723 	}
4724 	spin_unlock_irq(&phba->hbalock);
4725 }
4726 
4727 /*
4728  * Free resources / clean up outstanding I/Os
4729  * associated with nlp_rpi in the LPFC_NODELIST entry.
4730  */
4731 static int
4732 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
4733 {
4734 	LIST_HEAD(completions);
4735 
4736 	lpfc_fabric_abort_nport(ndlp);
4737 
4738 	/*
4739 	 * Everything that matches on txcmplq will be returned
4740 	 * by firmware with a no rpi error.
4741 	 */
4742 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
4743 		if (phba->sli_rev != LPFC_SLI_REV4)
4744 			lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4745 		else
4746 			lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
4747 	}
4748 
4749 	/* Cancel all the IOCBs from the completions list */
4750 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4751 			      IOERR_SLI_ABORTED);
4752 
4753 	return 0;
4754 }
4755 
4756 /**
4757  * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4758  * @phba: Pointer to HBA context object.
4759  * @pmb: Pointer to mailbox object.
4760  *
4761  * This function will issue an ELS LOGO command after completing
4762  * the UNREG_RPI.
4763  **/
4764 static void
4765 lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4766 {
4767 	struct lpfc_vport  *vport = pmb->vport;
4768 	struct lpfc_nodelist *ndlp;
4769 
4770 	ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
4771 	if (!ndlp)
4772 		return;
4773 	lpfc_issue_els_logo(vport, ndlp, 0);
4774 	mempool_free(pmb, phba->mbox_mem_pool);
4775 
4776 	/* Check to see if there are any deferred events to process */
4777 	if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
4778 	    (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
4779 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4780 				 "1434 UNREG cmpl deferred logo x%x "
4781 				 "on NPort x%x Data: x%x x%px\n",
4782 				 ndlp->nlp_rpi, ndlp->nlp_DID,
4783 				 ndlp->nlp_defer_did, ndlp);
4784 
4785 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
4786 		ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4787 		lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4788 	} else {
4789 		if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
4790 			lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
4791 			ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
4792 			ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
4793 		}
4794 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
4795 	}
4796 }
4797 
4798 /*
4799  * Sets the mailbox completion handler to be used for the
4800  * unreg_rpi command. The handler varies based on the state of
4801  * the port and what will be happening to the rpi next.
4802  */
4803 static void
4804 lpfc_set_unreg_login_mbx_cmpl(struct lpfc_hba *phba, struct lpfc_vport *vport,
4805 	struct lpfc_nodelist *ndlp, LPFC_MBOXQ_t *mbox)
4806 {
4807 	unsigned long iflags;
4808 
4809 	/* Driver always gets a reference on the mailbox job
4810 	 * in support of async jobs.
4811 	 */
4812 	mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
4813 	if (!mbox->ctx_ndlp)
4814 		return;
4815 
4816 	if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4817 		mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4818 
4819 	} else if (phba->sli_rev == LPFC_SLI_REV4 &&
4820 		   (!(vport->load_flag & FC_UNLOADING)) &&
4821 		    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
4822 				      LPFC_SLI_INTF_IF_TYPE_2) &&
4823 		    (kref_read(&ndlp->kref) > 0)) {
4824 		mbox->mbox_cmpl = lpfc_sli4_unreg_rpi_cmpl_clr;
4825 	} else {
4826 		if (vport->load_flag & FC_UNLOADING) {
4827 			if (phba->sli_rev == LPFC_SLI_REV4) {
4828 				spin_lock_irqsave(&ndlp->lock, iflags);
4829 				ndlp->nlp_flag |= NLP_RELEASE_RPI;
4830 				spin_unlock_irqrestore(&ndlp->lock, iflags);
4831 			}
4832 		}
4833 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4834 	}
4835 }
4836 
4837 /*
4838  * Free rpi associated with LPFC_NODELIST entry.
4839  * This routine is called from lpfc_freenode(), when we are removing
4840  * a LPFC_NODELIST entry. It is also called if the driver initiates a
4841  * LOGO that completes successfully, and we are waiting to PLOGI back
4842  * to the remote NPort. In addition, it is called after we receive
4843  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4844  * we are waiting to PLOGI back to the remote NPort.
4845  */
4846 int
4847 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4848 {
4849 	struct lpfc_hba *phba = vport->phba;
4850 	LPFC_MBOXQ_t    *mbox;
4851 	int rc, acc_plogi = 1;
4852 	uint16_t rpi;
4853 
4854 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4855 	    ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4856 		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4857 			lpfc_printf_vlog(vport, KERN_INFO,
4858 					 LOG_NODE | LOG_DISCOVERY,
4859 					 "3366 RPI x%x needs to be "
4860 					 "unregistered nlp_flag x%x "
4861 					 "did x%x\n",
4862 					 ndlp->nlp_rpi, ndlp->nlp_flag,
4863 					 ndlp->nlp_DID);
4864 
4865 		/* If there is already an UNREG in progress for this ndlp,
4866 		 * no need to queue up another one.
4867 		 */
4868 		if (ndlp->nlp_flag & NLP_UNREG_INP) {
4869 			lpfc_printf_vlog(vport, KERN_INFO,
4870 					 LOG_NODE | LOG_DISCOVERY,
4871 					 "1436 unreg_rpi SKIP UNREG x%x on "
4872 					 "NPort x%x deferred x%x  flg x%x "
4873 					 "Data: x%px\n",
4874 					 ndlp->nlp_rpi, ndlp->nlp_DID,
4875 					 ndlp->nlp_defer_did,
4876 					 ndlp->nlp_flag, ndlp);
4877 			goto out;
4878 		}
4879 
4880 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4881 		if (mbox) {
4882 			/* SLI4 ports require the physical rpi value. */
4883 			rpi = ndlp->nlp_rpi;
4884 			if (phba->sli_rev == LPFC_SLI_REV4)
4885 				rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4886 
4887 			lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
4888 			mbox->vport = vport;
4889 			lpfc_set_unreg_login_mbx_cmpl(phba, vport, ndlp, mbox);
4890 			if (!mbox->ctx_ndlp) {
4891 				mempool_free(mbox, phba->mbox_mem_pool);
4892 				return 1;
4893 			}
4894 
4895 			if (mbox->mbox_cmpl == lpfc_sli4_unreg_rpi_cmpl_clr)
4896 				/*
4897 				 * accept PLOGIs after unreg_rpi_cmpl
4898 				 */
4899 				acc_plogi = 0;
4900 			if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
4901 			    Fabric_DID_MASK) &&
4902 			    (!(vport->fc_flag & FC_OFFLINE_MODE)))
4903 				ndlp->nlp_flag |= NLP_UNREG_INP;
4904 
4905 			lpfc_printf_vlog(vport, KERN_INFO,
4906 					 LOG_NODE | LOG_DISCOVERY,
4907 					 "1433 unreg_rpi UNREG x%x on "
4908 					 "NPort x%x deferred flg x%x "
4909 					 "Data:x%px\n",
4910 					 ndlp->nlp_rpi, ndlp->nlp_DID,
4911 					 ndlp->nlp_flag, ndlp);
4912 
4913 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4914 			if (rc == MBX_NOT_FINISHED) {
4915 				mempool_free(mbox, phba->mbox_mem_pool);
4916 				acc_plogi = 1;
4917 			}
4918 		} else {
4919 			lpfc_printf_vlog(vport, KERN_INFO,
4920 					 LOG_NODE | LOG_DISCOVERY,
4921 					 "1444 Failed to allocate mempool "
4922 					 "unreg_rpi UNREG x%x, "
4923 					 "DID x%x, flag x%x, "
4924 					 "ndlp x%px\n",
4925 					 ndlp->nlp_rpi, ndlp->nlp_DID,
4926 					 ndlp->nlp_flag, ndlp);
4927 
4928 			/* Because mempool_alloc failed, we
4929 			 * will issue a LOGO here and keep the rpi alive if
4930 			 * not unloading.
4931 			 */
4932 			if (!(vport->load_flag & FC_UNLOADING)) {
4933 				ndlp->nlp_flag &= ~NLP_UNREG_INP;
4934 				lpfc_issue_els_logo(vport, ndlp, 0);
4935 				ndlp->nlp_prev_state = ndlp->nlp_state;
4936 				lpfc_nlp_set_state(vport, ndlp,
4937 						   NLP_STE_NPR_NODE);
4938 			}
4939 
4940 			return 1;
4941 		}
4942 		lpfc_no_rpi(phba, ndlp);
4943 out:
4944 		if (phba->sli_rev != LPFC_SLI_REV4)
4945 			ndlp->nlp_rpi = 0;
4946 		ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
4947 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
4948 		if (acc_plogi)
4949 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4950 		return 1;
4951 	}
4952 	ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4953 	return 0;
4954 }
4955 
4956 /**
4957  * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4958  * @phba: pointer to lpfc hba data structure.
4959  *
4960  * This routine is invoked to unregister all the currently registered RPIs
4961  * to the HBA.
4962  **/
4963 void
4964 lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4965 {
4966 	struct lpfc_vport **vports;
4967 	struct lpfc_nodelist *ndlp;
4968 	struct Scsi_Host *shost;
4969 	int i;
4970 
4971 	vports = lpfc_create_vport_work_array(phba);
4972 	if (!vports) {
4973 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4974 				"2884 Vport array allocation failed \n");
4975 		return;
4976 	}
4977 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4978 		shost = lpfc_shost_from_vport(vports[i]);
4979 		spin_lock_irq(shost->host_lock);
4980 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4981 			if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
4982 				/* The mempool_alloc might sleep */
4983 				spin_unlock_irq(shost->host_lock);
4984 				lpfc_unreg_rpi(vports[i], ndlp);
4985 				spin_lock_irq(shost->host_lock);
4986 			}
4987 		}
4988 		spin_unlock_irq(shost->host_lock);
4989 	}
4990 	lpfc_destroy_vport_work_array(phba, vports);
4991 }
4992 
4993 void
4994 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4995 {
4996 	struct lpfc_hba  *phba  = vport->phba;
4997 	LPFC_MBOXQ_t     *mbox;
4998 	int rc;
4999 
5000 	if (phba->sli_rev == LPFC_SLI_REV4) {
5001 		lpfc_sli4_unreg_all_rpis(vport);
5002 		return;
5003 	}
5004 
5005 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5006 	if (mbox) {
5007 		lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
5008 				 mbox);
5009 		mbox->vport = vport;
5010 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5011 		mbox->ctx_ndlp = NULL;
5012 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5013 		if (rc != MBX_TIMEOUT)
5014 			mempool_free(mbox, phba->mbox_mem_pool);
5015 
5016 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5017 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5018 					 "1836 Could not issue "
5019 					 "unreg_login(all_rpis) status %d\n",
5020 					 rc);
5021 	}
5022 }
5023 
5024 void
5025 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
5026 {
5027 	struct lpfc_hba  *phba  = vport->phba;
5028 	LPFC_MBOXQ_t     *mbox;
5029 	int rc;
5030 
5031 	/* Unreg DID is an SLI3 operation. */
5032 	if (phba->sli_rev > LPFC_SLI_REV3)
5033 		return;
5034 
5035 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5036 	if (mbox) {
5037 		lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
5038 			       mbox);
5039 		mbox->vport = vport;
5040 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5041 		mbox->ctx_ndlp = NULL;
5042 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5043 		if (rc != MBX_TIMEOUT)
5044 			mempool_free(mbox, phba->mbox_mem_pool);
5045 
5046 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5047 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5048 					 "1815 Could not issue "
5049 					 "unreg_did (default rpis) status %d\n",
5050 					 rc);
5051 	}
5052 }
5053 
5054 /*
5055  * Free resources associated with LPFC_NODELIST entry
5056  * so it can be freed.
5057  */
5058 static int
5059 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5060 {
5061 	struct lpfc_hba  *phba = vport->phba;
5062 	LPFC_MBOXQ_t *mb, *nextmb;
5063 	struct lpfc_dmabuf *mp;
5064 
5065 	/* Cleanup node for NPort <nlp_DID> */
5066 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5067 			 "0900 Cleanup node for NPort x%x "
5068 			 "Data: x%x x%x x%x\n",
5069 			 ndlp->nlp_DID, ndlp->nlp_flag,
5070 			 ndlp->nlp_state, ndlp->nlp_rpi);
5071 	lpfc_dequeue_node(vport, ndlp);
5072 
5073 	/* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5074 
5075 	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5076 	if ((mb = phba->sli.mbox_active)) {
5077 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5078 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5079 		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5080 			mb->ctx_ndlp = NULL;
5081 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5082 		}
5083 	}
5084 
5085 	spin_lock_irq(&phba->hbalock);
5086 	/* Cleanup REG_LOGIN completions which are not yet processed */
5087 	list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5088 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
5089 			(mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
5090 			(ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
5091 			continue;
5092 
5093 		mb->ctx_ndlp = NULL;
5094 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5095 	}
5096 
5097 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
5098 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5099 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5100 		    (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5101 			mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
5102 			if (mp) {
5103 				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
5104 				kfree(mp);
5105 			}
5106 			list_del(&mb->list);
5107 			mempool_free(mb, phba->mbox_mem_pool);
5108 			/* We shall not invoke the lpfc_nlp_put to decrement
5109 			 * the ndlp reference count as we are in the process
5110 			 * of lpfc_nlp_release.
5111 			 */
5112 		}
5113 	}
5114 	spin_unlock_irq(&phba->hbalock);
5115 
5116 	lpfc_els_abort(phba, ndlp);
5117 
5118 	spin_lock_irq(&ndlp->lock);
5119 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
5120 	spin_unlock_irq(&ndlp->lock);
5121 
5122 	ndlp->nlp_last_elscmd = 0;
5123 	del_timer_sync(&ndlp->nlp_delayfunc);
5124 
5125 	list_del_init(&ndlp->els_retry_evt.evt_listp);
5126 	list_del_init(&ndlp->dev_loss_evt.evt_listp);
5127 	list_del_init(&ndlp->recovery_evt.evt_listp);
5128 	lpfc_cleanup_vports_rrqs(vport, ndlp);
5129 	if (phba->sli_rev == LPFC_SLI_REV4)
5130 		ndlp->nlp_flag |= NLP_RELEASE_RPI;
5131 	return 0;
5132 }
5133 
5134 static int
5135 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5136 	      uint32_t did)
5137 {
5138 	D_ID mydid, ndlpdid, matchdid;
5139 
5140 	if (did == Bcast_DID)
5141 		return 0;
5142 
5143 	/* First check for Direct match */
5144 	if (ndlp->nlp_DID == did)
5145 		return 1;
5146 
5147 	/* Next check for area/domain identically equals 0 match */
5148 	mydid.un.word = vport->fc_myDID;
5149 	if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
5150 		return 0;
5151 	}
5152 
5153 	matchdid.un.word = did;
5154 	ndlpdid.un.word = ndlp->nlp_DID;
5155 	if (matchdid.un.b.id == ndlpdid.un.b.id) {
5156 		if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5157 		    (mydid.un.b.area == matchdid.un.b.area)) {
5158 			/* This code is supposed to match the ID
5159 			 * for a private loop device that is
5160 			 * connect to fl_port. But we need to
5161 			 * check that the port did not just go
5162 			 * from pt2pt to fabric or we could end
5163 			 * up matching ndlp->nlp_DID 000001 to
5164 			 * fabric DID 0x20101
5165 			 */
5166 			if ((ndlpdid.un.b.domain == 0) &&
5167 			    (ndlpdid.un.b.area == 0)) {
5168 				if (ndlpdid.un.b.id &&
5169 				    vport->phba->fc_topology ==
5170 				    LPFC_TOPOLOGY_LOOP)
5171 					return 1;
5172 			}
5173 			return 0;
5174 		}
5175 
5176 		matchdid.un.word = ndlp->nlp_DID;
5177 		if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5178 		    (mydid.un.b.area == ndlpdid.un.b.area)) {
5179 			if ((matchdid.un.b.domain == 0) &&
5180 			    (matchdid.un.b.area == 0)) {
5181 				if (matchdid.un.b.id)
5182 					return 1;
5183 			}
5184 		}
5185 	}
5186 	return 0;
5187 }
5188 
5189 /* Search for a nodelist entry */
5190 static struct lpfc_nodelist *
5191 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5192 {
5193 	struct lpfc_nodelist *ndlp;
5194 	uint32_t data1;
5195 
5196 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5197 		if (lpfc_matchdid(vport, ndlp, did)) {
5198 			data1 = (((uint32_t)ndlp->nlp_state << 24) |
5199 				 ((uint32_t)ndlp->nlp_xri << 16) |
5200 				 ((uint32_t)ndlp->nlp_type << 8)
5201 				 );
5202 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5203 					 "0929 FIND node DID "
5204 					 "Data: x%px x%x x%x x%x x%x x%px\n",
5205 					 ndlp, ndlp->nlp_DID,
5206 					 ndlp->nlp_flag, data1, ndlp->nlp_rpi,
5207 					 ndlp->active_rrqs_xri_bitmap);
5208 			return ndlp;
5209 		}
5210 	}
5211 
5212 	/* FIND node did <did> NOT FOUND */
5213 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5214 			 "0932 FIND node did x%x NOT FOUND.\n", did);
5215 	return NULL;
5216 }
5217 
5218 struct lpfc_nodelist *
5219 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5220 {
5221 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5222 	struct lpfc_nodelist *ndlp;
5223 	unsigned long iflags;
5224 
5225 	spin_lock_irqsave(shost->host_lock, iflags);
5226 	ndlp = __lpfc_findnode_did(vport, did);
5227 	spin_unlock_irqrestore(shost->host_lock, iflags);
5228 	return ndlp;
5229 }
5230 
5231 struct lpfc_nodelist *
5232 lpfc_findnode_mapped(struct lpfc_vport *vport)
5233 {
5234 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5235 	struct lpfc_nodelist *ndlp;
5236 	uint32_t data1;
5237 	unsigned long iflags;
5238 
5239 	spin_lock_irqsave(shost->host_lock, iflags);
5240 
5241 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5242 		if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
5243 		    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
5244 			data1 = (((uint32_t)ndlp->nlp_state << 24) |
5245 				 ((uint32_t)ndlp->nlp_xri << 16) |
5246 				 ((uint32_t)ndlp->nlp_type << 8) |
5247 				 ((uint32_t)ndlp->nlp_rpi & 0xff));
5248 			spin_unlock_irqrestore(shost->host_lock, iflags);
5249 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5250 					 "2025 FIND node DID "
5251 					 "Data: x%px x%x x%x x%x x%px\n",
5252 					 ndlp, ndlp->nlp_DID,
5253 					 ndlp->nlp_flag, data1,
5254 					 ndlp->active_rrqs_xri_bitmap);
5255 			return ndlp;
5256 		}
5257 	}
5258 	spin_unlock_irqrestore(shost->host_lock, iflags);
5259 
5260 	/* FIND node did <did> NOT FOUND */
5261 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5262 			 "2026 FIND mapped did NOT FOUND.\n");
5263 	return NULL;
5264 }
5265 
5266 struct lpfc_nodelist *
5267 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5268 {
5269 	struct lpfc_nodelist *ndlp;
5270 
5271 	ndlp = lpfc_findnode_did(vport, did);
5272 	if (!ndlp) {
5273 		if (vport->phba->nvmet_support)
5274 			return NULL;
5275 		if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5276 		    lpfc_rscn_payload_check(vport, did) == 0)
5277 			return NULL;
5278 		ndlp = lpfc_nlp_init(vport, did);
5279 		if (!ndlp)
5280 			return NULL;
5281 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5282 
5283 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5284 				 "6453 Setup New Node 2B_DISC x%x "
5285 				 "Data:x%x x%x x%x\n",
5286 				 ndlp->nlp_DID, ndlp->nlp_flag,
5287 				 ndlp->nlp_state, vport->fc_flag);
5288 
5289 		spin_lock_irq(&ndlp->lock);
5290 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5291 		spin_unlock_irq(&ndlp->lock);
5292 		return ndlp;
5293 	}
5294 
5295 	/* The NVME Target does not want to actively manage an rport.
5296 	 * The goal is to allow the target to reset its state and clear
5297 	 * pending IO in preparation for the initiator to recover.
5298 	 */
5299 	if ((vport->fc_flag & FC_RSCN_MODE) &&
5300 	    !(vport->fc_flag & FC_NDISC_ACTIVE)) {
5301 		if (lpfc_rscn_payload_check(vport, did)) {
5302 
5303 			/* Since this node is marked for discovery,
5304 			 * delay timeout is not needed.
5305 			 */
5306 			lpfc_cancel_retry_delay_tmo(vport, ndlp);
5307 
5308 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5309 					 "6455 Setup RSCN Node 2B_DISC x%x "
5310 					 "Data:x%x x%x x%x\n",
5311 					 ndlp->nlp_DID, ndlp->nlp_flag,
5312 					 ndlp->nlp_state, vport->fc_flag);
5313 
5314 			/* NVME Target mode waits until rport is known to be
5315 			 * impacted by the RSCN before it transitions.  No
5316 			 * active management - just go to NPR provided the
5317 			 * node had a valid login.
5318 			 */
5319 			if (vport->phba->nvmet_support)
5320 				return ndlp;
5321 
5322 			/* If we've already received a PLOGI from this NPort
5323 			 * we don't need to try to discover it again.
5324 			 */
5325 			if (ndlp->nlp_flag & NLP_RCV_PLOGI &&
5326 			    !(ndlp->nlp_type &
5327 			     (NLP_FCP_TARGET | NLP_NVME_TARGET)))
5328 				return NULL;
5329 
5330 			ndlp->nlp_prev_state = ndlp->nlp_state;
5331 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5332 
5333 			spin_lock_irq(&ndlp->lock);
5334 			ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5335 			spin_unlock_irq(&ndlp->lock);
5336 		} else {
5337 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5338 					 "6456 Skip Setup RSCN Node x%x "
5339 					 "Data:x%x x%x x%x\n",
5340 					 ndlp->nlp_DID, ndlp->nlp_flag,
5341 					 ndlp->nlp_state, vport->fc_flag);
5342 			ndlp = NULL;
5343 		}
5344 	} else {
5345 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5346 				 "6457 Setup Active Node 2B_DISC x%x "
5347 				 "Data:x%x x%x x%x\n",
5348 				 ndlp->nlp_DID, ndlp->nlp_flag,
5349 				 ndlp->nlp_state, vport->fc_flag);
5350 
5351 		/* If the initiator received a PLOGI from this NPort or if the
5352 		 * initiator is already in the process of discovery on it,
5353 		 * there's no need to try to discover it again.
5354 		 */
5355 		if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
5356 		    ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5357 		    (!vport->phba->nvmet_support &&
5358 		     ndlp->nlp_flag & NLP_RCV_PLOGI))
5359 			return NULL;
5360 
5361 		if (vport->phba->nvmet_support)
5362 			return ndlp;
5363 
5364 		/* Moving to NPR state clears unsolicited flags and
5365 		 * allows for rediscovery
5366 		 */
5367 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5368 
5369 		spin_lock_irq(&ndlp->lock);
5370 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5371 		spin_unlock_irq(&ndlp->lock);
5372 	}
5373 	return ndlp;
5374 }
5375 
5376 /* Build a list of nodes to discover based on the loopmap */
5377 void
5378 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
5379 {
5380 	struct lpfc_hba  *phba = vport->phba;
5381 	int j;
5382 	uint32_t alpa, index;
5383 
5384 	if (!lpfc_is_link_up(phba))
5385 		return;
5386 
5387 	if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
5388 		return;
5389 
5390 	/* Check for loop map present or not */
5391 	if (phba->alpa_map[0]) {
5392 		for (j = 1; j <= phba->alpa_map[0]; j++) {
5393 			alpa = phba->alpa_map[j];
5394 			if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
5395 				continue;
5396 			lpfc_setup_disc_node(vport, alpa);
5397 		}
5398 	} else {
5399 		/* No alpamap, so try all alpa's */
5400 		for (j = 0; j < FC_MAXLOOP; j++) {
5401 			/* If cfg_scan_down is set, start from highest
5402 			 * ALPA (0xef) to lowest (0x1).
5403 			 */
5404 			if (vport->cfg_scan_down)
5405 				index = j;
5406 			else
5407 				index = FC_MAXLOOP - j - 1;
5408 			alpa = lpfcAlpaArray[index];
5409 			if ((vport->fc_myDID & 0xff) == alpa)
5410 				continue;
5411 			lpfc_setup_disc_node(vport, alpa);
5412 		}
5413 	}
5414 	return;
5415 }
5416 
5417 /* SLI3 only */
5418 void
5419 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
5420 {
5421 	LPFC_MBOXQ_t *mbox;
5422 	struct lpfc_sli *psli = &phba->sli;
5423 	struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5424 	struct lpfc_sli_ring *fcp_ring   = &psli->sli3_ring[LPFC_FCP_RING];
5425 	int  rc;
5426 
5427 	/*
5428 	 * if it's not a physical port or if we already send
5429 	 * clear_la then don't send it.
5430 	 */
5431 	if ((phba->link_state >= LPFC_CLEAR_LA) ||
5432 	    (vport->port_type != LPFC_PHYSICAL_PORT) ||
5433 		(phba->sli_rev == LPFC_SLI_REV4))
5434 		return;
5435 
5436 			/* Link up discovery */
5437 	if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5438 		phba->link_state = LPFC_CLEAR_LA;
5439 		lpfc_clear_la(phba, mbox);
5440 		mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5441 		mbox->vport = vport;
5442 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5443 		if (rc == MBX_NOT_FINISHED) {
5444 			mempool_free(mbox, phba->mbox_mem_pool);
5445 			lpfc_disc_flush_list(vport);
5446 			extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5447 			fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5448 			phba->link_state = LPFC_HBA_ERROR;
5449 		}
5450 	}
5451 }
5452 
5453 /* Reg_vpi to tell firmware to resume normal operations */
5454 void
5455 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5456 {
5457 	LPFC_MBOXQ_t *regvpimbox;
5458 
5459 	regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5460 	if (regvpimbox) {
5461 		lpfc_reg_vpi(vport, regvpimbox);
5462 		regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5463 		regvpimbox->vport = vport;
5464 		if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
5465 					== MBX_NOT_FINISHED) {
5466 			mempool_free(regvpimbox, phba->mbox_mem_pool);
5467 		}
5468 	}
5469 }
5470 
5471 /* Start Link up / RSCN discovery on NPR nodes */
5472 void
5473 lpfc_disc_start(struct lpfc_vport *vport)
5474 {
5475 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5476 	struct lpfc_hba  *phba = vport->phba;
5477 	uint32_t num_sent;
5478 	uint32_t clear_la_pending;
5479 
5480 	if (!lpfc_is_link_up(phba)) {
5481 		lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5482 				 "3315 Link is not up %x\n",
5483 				 phba->link_state);
5484 		return;
5485 	}
5486 
5487 	if (phba->link_state == LPFC_CLEAR_LA)
5488 		clear_la_pending = 1;
5489 	else
5490 		clear_la_pending = 0;
5491 
5492 	if (vport->port_state < LPFC_VPORT_READY)
5493 		vport->port_state = LPFC_DISC_AUTH;
5494 
5495 	lpfc_set_disctmo(vport);
5496 
5497 	vport->fc_prevDID = vport->fc_myDID;
5498 	vport->num_disc_nodes = 0;
5499 
5500 	/* Start Discovery state <hba_state> */
5501 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5502 			 "0202 Start Discovery port state x%x "
5503 			 "flg x%x Data: x%x x%x x%x\n",
5504 			 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5505 			 vport->fc_adisc_cnt, vport->fc_npr_cnt);
5506 
5507 	/* First do ADISCs - if any */
5508 	num_sent = lpfc_els_disc_adisc(vport);
5509 
5510 	if (num_sent)
5511 		return;
5512 
5513 	/* Register the VPI for SLI3, NPIV only. */
5514 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5515 	    !(vport->fc_flag & FC_PT2PT) &&
5516 	    !(vport->fc_flag & FC_RSCN_MODE) &&
5517 	    (phba->sli_rev < LPFC_SLI_REV4)) {
5518 		lpfc_issue_clear_la(phba, vport);
5519 		lpfc_issue_reg_vpi(phba, vport);
5520 		return;
5521 	}
5522 
5523 	/*
5524 	 * For SLI2, we need to set port_state to READY and continue
5525 	 * discovery.
5526 	 */
5527 	if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5528 		/* If we get here, there is nothing to ADISC */
5529 		lpfc_issue_clear_la(phba, vport);
5530 
5531 		if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
5532 			vport->num_disc_nodes = 0;
5533 			/* go thru NPR nodes and issue ELS PLOGIs */
5534 			if (vport->fc_npr_cnt)
5535 				lpfc_els_disc_plogi(vport);
5536 
5537 			if (!vport->num_disc_nodes) {
5538 				spin_lock_irq(shost->host_lock);
5539 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
5540 				spin_unlock_irq(shost->host_lock);
5541 				lpfc_can_disctmo(vport);
5542 			}
5543 		}
5544 		vport->port_state = LPFC_VPORT_READY;
5545 	} else {
5546 		/* Next do PLOGIs - if any */
5547 		num_sent = lpfc_els_disc_plogi(vport);
5548 
5549 		if (num_sent)
5550 			return;
5551 
5552 		if (vport->fc_flag & FC_RSCN_MODE) {
5553 			/* Check to see if more RSCNs came in while we
5554 			 * were processing this one.
5555 			 */
5556 			if ((vport->fc_rscn_id_cnt == 0) &&
5557 			    (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5558 				spin_lock_irq(shost->host_lock);
5559 				vport->fc_flag &= ~FC_RSCN_MODE;
5560 				spin_unlock_irq(shost->host_lock);
5561 				lpfc_can_disctmo(vport);
5562 			} else
5563 				lpfc_els_handle_rscn(vport);
5564 		}
5565 	}
5566 	return;
5567 }
5568 
5569 /*
5570  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5571  *  ring the match the sppecified nodelist.
5572  */
5573 static void
5574 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
5575 {
5576 	LIST_HEAD(completions);
5577 	IOCB_t     *icmd;
5578 	struct lpfc_iocbq    *iocb, *next_iocb;
5579 	struct lpfc_sli_ring *pring;
5580 
5581 	pring = lpfc_phba_elsring(phba);
5582 	if (unlikely(!pring))
5583 		return;
5584 
5585 	/* Error matching iocb on txq or txcmplq
5586 	 * First check the txq.
5587 	 */
5588 	spin_lock_irq(&phba->hbalock);
5589 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5590 		if (iocb->context1 != ndlp) {
5591 			continue;
5592 		}
5593 		icmd = &iocb->iocb;
5594 		if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5595 		    (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5596 
5597 			list_move_tail(&iocb->list, &completions);
5598 		}
5599 	}
5600 
5601 	/* Next check the txcmplq */
5602 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5603 		if (iocb->context1 != ndlp) {
5604 			continue;
5605 		}
5606 		icmd = &iocb->iocb;
5607 		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5608 		    icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
5609 			lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
5610 		}
5611 	}
5612 	spin_unlock_irq(&phba->hbalock);
5613 
5614 	/* Cancel all the IOCBs from the completions list */
5615 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5616 			      IOERR_SLI_ABORTED);
5617 }
5618 
5619 static void
5620 lpfc_disc_flush_list(struct lpfc_vport *vport)
5621 {
5622 	struct lpfc_nodelist *ndlp, *next_ndlp;
5623 	struct lpfc_hba *phba = vport->phba;
5624 
5625 	if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5626 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
5627 					 nlp_listp) {
5628 			if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5629 			    ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5630 				lpfc_free_tx(phba, ndlp);
5631 			}
5632 		}
5633 	}
5634 }
5635 
5636 void
5637 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5638 {
5639 	lpfc_els_flush_rscn(vport);
5640 	lpfc_els_flush_cmd(vport);
5641 	lpfc_disc_flush_list(vport);
5642 }
5643 
5644 /*****************************************************************************/
5645 /*
5646  * NAME:     lpfc_disc_timeout
5647  *
5648  * FUNCTION: Fibre Channel driver discovery timeout routine.
5649  *
5650  * EXECUTION ENVIRONMENT: interrupt only
5651  *
5652  * CALLED FROM:
5653  *      Timer function
5654  *
5655  * RETURNS:
5656  *      none
5657  */
5658 /*****************************************************************************/
5659 void
5660 lpfc_disc_timeout(struct timer_list *t)
5661 {
5662 	struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
5663 	struct lpfc_hba   *phba = vport->phba;
5664 	uint32_t tmo_posted;
5665 	unsigned long flags = 0;
5666 
5667 	if (unlikely(!phba))
5668 		return;
5669 
5670 	spin_lock_irqsave(&vport->work_port_lock, flags);
5671 	tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5672 	if (!tmo_posted)
5673 		vport->work_port_events |= WORKER_DISC_TMO;
5674 	spin_unlock_irqrestore(&vport->work_port_lock, flags);
5675 
5676 	if (!tmo_posted)
5677 		lpfc_worker_wake_up(phba);
5678 	return;
5679 }
5680 
5681 static void
5682 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
5683 {
5684 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5685 	struct lpfc_hba  *phba = vport->phba;
5686 	struct lpfc_sli  *psli = &phba->sli;
5687 	struct lpfc_nodelist *ndlp, *next_ndlp;
5688 	LPFC_MBOXQ_t *initlinkmbox;
5689 	int rc, clrlaerr = 0;
5690 
5691 	if (!(vport->fc_flag & FC_DISC_TMO))
5692 		return;
5693 
5694 	spin_lock_irq(shost->host_lock);
5695 	vport->fc_flag &= ~FC_DISC_TMO;
5696 	spin_unlock_irq(shost->host_lock);
5697 
5698 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5699 		"disc timeout:    state:x%x rtry:x%x flg:x%x",
5700 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5701 
5702 	switch (vport->port_state) {
5703 
5704 	case LPFC_LOCAL_CFG_LINK:
5705 		/*
5706 		 * port_state is identically  LPFC_LOCAL_CFG_LINK while
5707 		 * waiting for FAN timeout
5708 		 */
5709 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5710 				 "0221 FAN timeout\n");
5711 
5712 		/* Start discovery by sending FLOGI, clean up old rpis */
5713 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
5714 					 nlp_listp) {
5715 			if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5716 				continue;
5717 			if (ndlp->nlp_type & NLP_FABRIC) {
5718 				/* Clean up the ndlp on Fabric connections */
5719 				lpfc_drop_node(vport, ndlp);
5720 
5721 			} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
5722 				/* Fail outstanding IO now since device
5723 				 * is marked for PLOGI.
5724 				 */
5725 				lpfc_unreg_rpi(vport, ndlp);
5726 			}
5727 		}
5728 		if (vport->port_state != LPFC_FLOGI) {
5729 			if (phba->sli_rev <= LPFC_SLI_REV3)
5730 				lpfc_initial_flogi(vport);
5731 			else
5732 				lpfc_issue_init_vfi(vport);
5733 			return;
5734 		}
5735 		break;
5736 
5737 	case LPFC_FDISC:
5738 	case LPFC_FLOGI:
5739 	/* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
5740 		/* Initial FLOGI timeout */
5741 		lpfc_printf_vlog(vport, KERN_ERR,
5742 				 LOG_TRACE_EVENT,
5743 				 "0222 Initial %s timeout\n",
5744 				 vport->vpi ? "FDISC" : "FLOGI");
5745 
5746 		/* Assume no Fabric and go on with discovery.
5747 		 * Check for outstanding ELS FLOGI to abort.
5748 		 */
5749 
5750 		/* FLOGI failed, so just use loop map to make discovery list */
5751 		lpfc_disc_list_loopmap(vport);
5752 
5753 		/* Start discovery */
5754 		lpfc_disc_start(vport);
5755 		break;
5756 
5757 	case LPFC_FABRIC_CFG_LINK:
5758 	/* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5759 	   NameServer login */
5760 		lpfc_printf_vlog(vport, KERN_ERR,
5761 				 LOG_TRACE_EVENT,
5762 				 "0223 Timeout while waiting for "
5763 				 "NameServer login\n");
5764 		/* Next look for NameServer ndlp */
5765 		ndlp = lpfc_findnode_did(vport, NameServer_DID);
5766 		if (ndlp)
5767 			lpfc_els_abort(phba, ndlp);
5768 
5769 		/* ReStart discovery */
5770 		goto restart_disc;
5771 
5772 	case LPFC_NS_QRY:
5773 	/* Check for wait for NameServer Rsp timeout */
5774 		lpfc_printf_vlog(vport, KERN_ERR,
5775 				 LOG_TRACE_EVENT,
5776 				 "0224 NameServer Query timeout "
5777 				 "Data: x%x x%x\n",
5778 				 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
5779 
5780 		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5781 			/* Try it one more time */
5782 			vport->fc_ns_retry++;
5783 			vport->gidft_inp = 0;
5784 			rc = lpfc_issue_gidft(vport);
5785 			if (rc == 0)
5786 				break;
5787 		}
5788 		vport->fc_ns_retry = 0;
5789 
5790 restart_disc:
5791 		/*
5792 		 * Discovery is over.
5793 		 * set port_state to PORT_READY if SLI2.
5794 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
5795 		 */
5796 		if (phba->sli_rev < LPFC_SLI_REV4) {
5797 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5798 				lpfc_issue_reg_vpi(phba, vport);
5799 			else  {
5800 				lpfc_issue_clear_la(phba, vport);
5801 				vport->port_state = LPFC_VPORT_READY;
5802 			}
5803 		}
5804 
5805 		/* Setup and issue mailbox INITIALIZE LINK command */
5806 		initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5807 		if (!initlinkmbox) {
5808 			lpfc_printf_vlog(vport, KERN_ERR,
5809 					 LOG_TRACE_EVENT,
5810 					 "0206 Device Discovery "
5811 					 "completion error\n");
5812 			phba->link_state = LPFC_HBA_ERROR;
5813 			break;
5814 		}
5815 
5816 		lpfc_linkdown(phba);
5817 		lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5818 			       phba->cfg_link_speed);
5819 		initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
5820 		initlinkmbox->vport = vport;
5821 		initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5822 		rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
5823 		lpfc_set_loopback_flag(phba);
5824 		if (rc == MBX_NOT_FINISHED)
5825 			mempool_free(initlinkmbox, phba->mbox_mem_pool);
5826 
5827 		break;
5828 
5829 	case LPFC_DISC_AUTH:
5830 	/* Node Authentication timeout */
5831 		lpfc_printf_vlog(vport, KERN_ERR,
5832 				 LOG_TRACE_EVENT,
5833 				 "0227 Node Authentication timeout\n");
5834 		lpfc_disc_flush_list(vport);
5835 
5836 		/*
5837 		 * set port_state to PORT_READY if SLI2.
5838 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
5839 		 */
5840 		if (phba->sli_rev < LPFC_SLI_REV4) {
5841 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5842 				lpfc_issue_reg_vpi(phba, vport);
5843 			else  {	/* NPIV Not enabled */
5844 				lpfc_issue_clear_la(phba, vport);
5845 				vport->port_state = LPFC_VPORT_READY;
5846 			}
5847 		}
5848 		break;
5849 
5850 	case LPFC_VPORT_READY:
5851 		if (vport->fc_flag & FC_RSCN_MODE) {
5852 			lpfc_printf_vlog(vport, KERN_ERR,
5853 					 LOG_TRACE_EVENT,
5854 					 "0231 RSCN timeout Data: x%x "
5855 					 "x%x\n",
5856 					 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
5857 
5858 			/* Cleanup any outstanding ELS commands */
5859 			lpfc_els_flush_cmd(vport);
5860 
5861 			lpfc_els_flush_rscn(vport);
5862 			lpfc_disc_flush_list(vport);
5863 		}
5864 		break;
5865 
5866 	default:
5867 		lpfc_printf_vlog(vport, KERN_ERR,
5868 				 LOG_TRACE_EVENT,
5869 				 "0273 Unexpected discovery timeout, "
5870 				 "vport State x%x\n", vport->port_state);
5871 		break;
5872 	}
5873 
5874 	switch (phba->link_state) {
5875 	case LPFC_CLEAR_LA:
5876 				/* CLEAR LA timeout */
5877 		lpfc_printf_vlog(vport, KERN_ERR,
5878 				 LOG_TRACE_EVENT,
5879 				 "0228 CLEAR LA timeout\n");
5880 		clrlaerr = 1;
5881 		break;
5882 
5883 	case LPFC_LINK_UP:
5884 		lpfc_issue_clear_la(phba, vport);
5885 		fallthrough;
5886 	case LPFC_LINK_UNKNOWN:
5887 	case LPFC_WARM_START:
5888 	case LPFC_INIT_START:
5889 	case LPFC_INIT_MBX_CMDS:
5890 	case LPFC_LINK_DOWN:
5891 	case LPFC_HBA_ERROR:
5892 		lpfc_printf_vlog(vport, KERN_ERR,
5893 				 LOG_TRACE_EVENT,
5894 				 "0230 Unexpected timeout, hba link "
5895 				 "state x%x\n", phba->link_state);
5896 		clrlaerr = 1;
5897 		break;
5898 
5899 	case LPFC_HBA_READY:
5900 		break;
5901 	}
5902 
5903 	if (clrlaerr) {
5904 		lpfc_disc_flush_list(vport);
5905 		if (phba->sli_rev != LPFC_SLI_REV4) {
5906 			psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5907 				~LPFC_STOP_IOCB_EVENT;
5908 			psli->sli3_ring[LPFC_FCP_RING].flag &=
5909 				~LPFC_STOP_IOCB_EVENT;
5910 		}
5911 		vport->port_state = LPFC_VPORT_READY;
5912 	}
5913 	return;
5914 }
5915 
5916 /*
5917  * This routine handles processing a NameServer REG_LOGIN mailbox
5918  * command upon completion. It is setup in the LPFC_MBOXQ
5919  * as the completion routine when the command is
5920  * handed off to the SLI layer.
5921  */
5922 void
5923 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5924 {
5925 	MAILBOX_t *mb = &pmb->u.mb;
5926 	struct lpfc_dmabuf   *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
5927 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
5928 	struct lpfc_vport    *vport = pmb->vport;
5929 
5930 	pmb->ctx_buf = NULL;
5931 	pmb->ctx_ndlp = NULL;
5932 
5933 	if (phba->sli_rev < LPFC_SLI_REV4)
5934 		ndlp->nlp_rpi = mb->un.varWords[0];
5935 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
5936 	ndlp->nlp_type |= NLP_FABRIC;
5937 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
5938 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
5939 			 "0004 rpi:%x DID:%x flg:%x %d x%px\n",
5940 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5941 			 kref_read(&ndlp->kref),
5942 			 ndlp);
5943 	/*
5944 	 * Start issuing Fabric-Device Management Interface (FDMI) command to
5945 	 * 0xfffffa (FDMI well known port).
5946 	 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
5947 	 * DPRT -> RPRT (vports)
5948 	 */
5949 	if (vport->port_type == LPFC_PHYSICAL_PORT)
5950 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
5951 	else
5952 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5953 
5954 
5955 	/* decrement the node reference count held for this callback
5956 	 * function.
5957 	 */
5958 	lpfc_nlp_put(ndlp);
5959 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
5960 	kfree(mp);
5961 	mempool_free(pmb, phba->mbox_mem_pool);
5962 
5963 	return;
5964 }
5965 
5966 static int
5967 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5968 {
5969 	uint16_t *rpi = param;
5970 
5971 	return ndlp->nlp_rpi == *rpi;
5972 }
5973 
5974 static int
5975 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5976 {
5977 	return memcmp(&ndlp->nlp_portname, param,
5978 		      sizeof(ndlp->nlp_portname)) == 0;
5979 }
5980 
5981 static struct lpfc_nodelist *
5982 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
5983 {
5984 	struct lpfc_nodelist *ndlp;
5985 
5986 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5987 		if (filter(ndlp, param)) {
5988 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5989 					 "3185 FIND node filter %ps DID "
5990 					 "ndlp x%px did x%x flg x%x st x%x "
5991 					 "xri x%x type x%x rpi x%x\n",
5992 					 filter, ndlp, ndlp->nlp_DID,
5993 					 ndlp->nlp_flag, ndlp->nlp_state,
5994 					 ndlp->nlp_xri, ndlp->nlp_type,
5995 					 ndlp->nlp_rpi);
5996 			return ndlp;
5997 		}
5998 	}
5999 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6000 			 "3186 FIND node filter %ps NOT FOUND.\n", filter);
6001 	return NULL;
6002 }
6003 
6004 /*
6005  * This routine looks up the ndlp lists for the given RPI. If rpi found it
6006  * returns the node list element pointer else return NULL.
6007  */
6008 struct lpfc_nodelist *
6009 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6010 {
6011 	return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
6012 }
6013 
6014 /*
6015  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
6016  * returns the node element list pointer else return NULL.
6017  */
6018 struct lpfc_nodelist *
6019 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
6020 {
6021 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6022 	struct lpfc_nodelist *ndlp;
6023 
6024 	spin_lock_irq(shost->host_lock);
6025 	ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
6026 	spin_unlock_irq(shost->host_lock);
6027 	return ndlp;
6028 }
6029 
6030 /*
6031  * This routine looks up the ndlp lists for the given RPI. If the rpi
6032  * is found, the routine returns the node element list pointer else
6033  * return NULL.
6034  */
6035 struct lpfc_nodelist *
6036 lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6037 {
6038 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6039 	struct lpfc_nodelist *ndlp;
6040 	unsigned long flags;
6041 
6042 	spin_lock_irqsave(shost->host_lock, flags);
6043 	ndlp = __lpfc_findnode_rpi(vport, rpi);
6044 	spin_unlock_irqrestore(shost->host_lock, flags);
6045 	return ndlp;
6046 }
6047 
6048 /**
6049  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6050  * @phba: pointer to lpfc hba data structure.
6051  * @vpi: the physical host virtual N_Port identifier.
6052  *
6053  * This routine finds a vport on a HBA (referred by @phba) through a
6054  * @vpi. The function walks the HBA's vport list and returns the address
6055  * of the vport with the matching @vpi.
6056  *
6057  * Return code
6058  *    NULL - No vport with the matching @vpi found
6059  *    Otherwise - Address to the vport with the matching @vpi.
6060  **/
6061 struct lpfc_vport *
6062 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6063 {
6064 	struct lpfc_vport *vport;
6065 	unsigned long flags;
6066 	int i = 0;
6067 
6068 	/* The physical ports are always vpi 0 - translate is unnecessary. */
6069 	if (vpi > 0) {
6070 		/*
6071 		 * Translate the physical vpi to the logical vpi.  The
6072 		 * vport stores the logical vpi.
6073 		 */
6074 		for (i = 0; i < phba->max_vpi; i++) {
6075 			if (vpi == phba->vpi_ids[i])
6076 				break;
6077 		}
6078 
6079 		if (i >= phba->max_vpi) {
6080 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6081 					"2936 Could not find Vport mapped "
6082 					"to vpi %d\n", vpi);
6083 			return NULL;
6084 		}
6085 	}
6086 
6087 	spin_lock_irqsave(&phba->port_list_lock, flags);
6088 	list_for_each_entry(vport, &phba->port_list, listentry) {
6089 		if (vport->vpi == i) {
6090 			spin_unlock_irqrestore(&phba->port_list_lock, flags);
6091 			return vport;
6092 		}
6093 	}
6094 	spin_unlock_irqrestore(&phba->port_list_lock, flags);
6095 	return NULL;
6096 }
6097 
6098 struct lpfc_nodelist *
6099 lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
6100 {
6101 	struct lpfc_nodelist *ndlp;
6102 	int rpi = LPFC_RPI_ALLOC_ERROR;
6103 
6104 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6105 		rpi = lpfc_sli4_alloc_rpi(vport->phba);
6106 		if (rpi == LPFC_RPI_ALLOC_ERROR)
6107 			return NULL;
6108 	}
6109 
6110 	ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6111 	if (!ndlp) {
6112 		if (vport->phba->sli_rev == LPFC_SLI_REV4)
6113 			lpfc_sli4_free_rpi(vport->phba, rpi);
6114 		return NULL;
6115 	}
6116 
6117 	memset(ndlp, 0, sizeof (struct lpfc_nodelist));
6118 
6119 	spin_lock_init(&ndlp->lock);
6120 
6121 	lpfc_initialize_node(vport, ndlp, did);
6122 	INIT_LIST_HEAD(&ndlp->nlp_listp);
6123 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6124 		ndlp->nlp_rpi = rpi;
6125 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6126 				 "0007 Init New ndlp x%px, rpi:x%x DID:%x "
6127 				 "flg:x%x refcnt:%d\n",
6128 				 ndlp, ndlp->nlp_rpi, ndlp->nlp_DID,
6129 				 ndlp->nlp_flag, kref_read(&ndlp->kref));
6130 
6131 		ndlp->active_rrqs_xri_bitmap =
6132 				mempool_alloc(vport->phba->active_rrq_pool,
6133 					      GFP_KERNEL);
6134 		if (ndlp->active_rrqs_xri_bitmap)
6135 			memset(ndlp->active_rrqs_xri_bitmap, 0,
6136 			       ndlp->phba->cfg_rrq_xri_bitmap_sz);
6137 	}
6138 
6139 
6140 
6141 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6142 		"node init:       did:x%x",
6143 		ndlp->nlp_DID, 0, 0);
6144 
6145 	return ndlp;
6146 }
6147 
6148 /* This routine releases all resources associated with a specifc NPort's ndlp
6149  * and mempool_free's the nodelist.
6150  */
6151 static void
6152 lpfc_nlp_release(struct kref *kref)
6153 {
6154 	struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6155 						  kref);
6156 	struct lpfc_vport *vport = ndlp->vport;
6157 
6158 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6159 		"node release:    did:x%x flg:x%x type:x%x",
6160 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6161 
6162 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6163 			"0279 %s: ndlp:%p did %x refcnt:%d rpi:%x\n",
6164 			 __func__, ndlp, ndlp->nlp_DID,
6165 			 kref_read(&ndlp->kref), ndlp->nlp_rpi);
6166 
6167 	/* remove ndlp from action. */
6168 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
6169 	lpfc_cleanup_node(vport, ndlp);
6170 
6171 	/* Clear Node key fields to give other threads notice
6172 	 * that this node memory is not valid anymore.
6173 	 */
6174 	ndlp->vport = NULL;
6175 	ndlp->nlp_state = NLP_STE_FREED_NODE;
6176 	ndlp->nlp_flag = 0;
6177 	ndlp->fc4_xpt_flags = 0;
6178 
6179 	/* free ndlp memory for final ndlp release */
6180 	kfree(ndlp->lat_data);
6181 	if (ndlp->phba->sli_rev == LPFC_SLI_REV4)
6182 		mempool_free(ndlp->active_rrqs_xri_bitmap,
6183 				ndlp->phba->active_rrq_pool);
6184 	mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
6185 }
6186 
6187 /* This routine bumps the reference count for a ndlp structure to ensure
6188  * that one discovery thread won't free a ndlp while another discovery thread
6189  * is using it.
6190  */
6191 struct lpfc_nodelist *
6192 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6193 {
6194 	unsigned long flags;
6195 
6196 	if (ndlp) {
6197 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6198 			"node get:        did:x%x flg:x%x refcnt:x%x",
6199 			ndlp->nlp_DID, ndlp->nlp_flag,
6200 			kref_read(&ndlp->kref));
6201 
6202 		/* The check of ndlp usage to prevent incrementing the
6203 		 * ndlp reference count that is in the process of being
6204 		 * released.
6205 		 */
6206 		spin_lock_irqsave(&ndlp->lock, flags);
6207 		if (!kref_get_unless_zero(&ndlp->kref)) {
6208 			spin_unlock_irqrestore(&ndlp->lock, flags);
6209 			lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6210 				"0276 %s: ndlp:x%px refcnt:%d\n",
6211 				__func__, (void *)ndlp, kref_read(&ndlp->kref));
6212 			return NULL;
6213 		}
6214 		spin_unlock_irqrestore(&ndlp->lock, flags);
6215 	} else {
6216 		WARN_ONCE(!ndlp, "**** %s, get ref on NULL ndlp!", __func__);
6217 	}
6218 
6219 	return ndlp;
6220 }
6221 
6222 /* This routine decrements the reference count for a ndlp structure. If the
6223  * count goes to 0, this indicates the associated nodelist should be freed.
6224  */
6225 int
6226 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6227 {
6228 	if (ndlp) {
6229 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6230 				"node put:        did:x%x flg:x%x refcnt:x%x",
6231 				ndlp->nlp_DID, ndlp->nlp_flag,
6232 				kref_read(&ndlp->kref));
6233 	} else {
6234 		WARN_ONCE(!ndlp, "**** %s, put ref on NULL ndlp!", __func__);
6235 	}
6236 
6237 	return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
6238 }
6239 
6240 /* This routine free's the specified nodelist if it is not in use
6241  * by any other discovery thread. This routine returns 1 if the
6242  * ndlp has been freed. A return value of 0 indicates the ndlp is
6243  * not yet been released.
6244  */
6245 int
6246 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6247 {
6248 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6249 		"node not used:   did:x%x flg:x%x refcnt:x%x",
6250 		ndlp->nlp_DID, ndlp->nlp_flag,
6251 		kref_read(&ndlp->kref));
6252 	if (kref_read(&ndlp->kref) == 1)
6253 		if (lpfc_nlp_put(ndlp))
6254 			return 1;
6255 	return 0;
6256 }
6257 
6258 /**
6259  * lpfc_fcf_inuse - Check if FCF can be unregistered.
6260  * @phba: Pointer to hba context object.
6261  *
6262  * This function iterate through all FC nodes associated
6263  * will all vports to check if there is any node with
6264  * fc_rports associated with it. If there is an fc_rport
6265  * associated with the node, then the node is either in
6266  * discovered state or its devloss_timer is pending.
6267  */
6268 static int
6269 lpfc_fcf_inuse(struct lpfc_hba *phba)
6270 {
6271 	struct lpfc_vport **vports;
6272 	int i, ret = 0;
6273 	struct lpfc_nodelist *ndlp;
6274 	struct Scsi_Host  *shost;
6275 
6276 	vports = lpfc_create_vport_work_array(phba);
6277 
6278 	/* If driver cannot allocate memory, indicate fcf is in use */
6279 	if (!vports)
6280 		return 1;
6281 
6282 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6283 		shost = lpfc_shost_from_vport(vports[i]);
6284 		spin_lock_irq(shost->host_lock);
6285 		/*
6286 		 * IF the CVL_RCVD bit is not set then we have sent the
6287 		 * flogi.
6288 		 * If dev_loss fires while we are waiting we do not want to
6289 		 * unreg the fcf.
6290 		 */
6291 		if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6292 			spin_unlock_irq(shost->host_lock);
6293 			ret =  1;
6294 			goto out;
6295 		}
6296 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6297 			if (ndlp->rport &&
6298 			  (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6299 				ret = 1;
6300 				spin_unlock_irq(shost->host_lock);
6301 				goto out;
6302 			} else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6303 				ret = 1;
6304 				lpfc_printf_log(phba, KERN_INFO,
6305 						LOG_NODE | LOG_DISCOVERY,
6306 						"2624 RPI %x DID %x flag %x "
6307 						"still logged in\n",
6308 						ndlp->nlp_rpi, ndlp->nlp_DID,
6309 						ndlp->nlp_flag);
6310 			}
6311 		}
6312 		spin_unlock_irq(shost->host_lock);
6313 	}
6314 out:
6315 	lpfc_destroy_vport_work_array(phba, vports);
6316 	return ret;
6317 }
6318 
6319 /**
6320  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6321  * @phba: Pointer to hba context object.
6322  * @mboxq: Pointer to mailbox object.
6323  *
6324  * This function frees memory associated with the mailbox command.
6325  */
6326 void
6327 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6328 {
6329 	struct lpfc_vport *vport = mboxq->vport;
6330 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6331 
6332 	if (mboxq->u.mb.mbxStatus) {
6333 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6334 				"2555 UNREG_VFI mbxStatus error x%x "
6335 				"HBA state x%x\n",
6336 				mboxq->u.mb.mbxStatus, vport->port_state);
6337 	}
6338 	spin_lock_irq(shost->host_lock);
6339 	phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6340 	spin_unlock_irq(shost->host_lock);
6341 	mempool_free(mboxq, phba->mbox_mem_pool);
6342 	return;
6343 }
6344 
6345 /**
6346  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6347  * @phba: Pointer to hba context object.
6348  * @mboxq: Pointer to mailbox object.
6349  *
6350  * This function frees memory associated with the mailbox command.
6351  */
6352 static void
6353 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6354 {
6355 	struct lpfc_vport *vport = mboxq->vport;
6356 
6357 	if (mboxq->u.mb.mbxStatus) {
6358 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6359 				"2550 UNREG_FCFI mbxStatus error x%x "
6360 				"HBA state x%x\n",
6361 				mboxq->u.mb.mbxStatus, vport->port_state);
6362 	}
6363 	mempool_free(mboxq, phba->mbox_mem_pool);
6364 	return;
6365 }
6366 
6367 /**
6368  * lpfc_unregister_fcf_prep - Unregister fcf record preparation
6369  * @phba: Pointer to hba context object.
6370  *
6371  * This function prepare the HBA for unregistering the currently registered
6372  * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6373  * VFIs.
6374  */
6375 int
6376 lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
6377 {
6378 	struct lpfc_vport **vports;
6379 	struct lpfc_nodelist *ndlp;
6380 	struct Scsi_Host *shost;
6381 	int i = 0, rc;
6382 
6383 	/* Unregister RPIs */
6384 	if (lpfc_fcf_inuse(phba))
6385 		lpfc_unreg_hba_rpis(phba);
6386 
6387 	/* At this point, all discovery is aborted */
6388 	phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6389 
6390 	/* Unregister VPIs */
6391 	vports = lpfc_create_vport_work_array(phba);
6392 	if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
6393 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6394 			/* Stop FLOGI/FDISC retries */
6395 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6396 			if (ndlp)
6397 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6398 			lpfc_cleanup_pending_mbox(vports[i]);
6399 			if (phba->sli_rev == LPFC_SLI_REV4)
6400 				lpfc_sli4_unreg_all_rpis(vports[i]);
6401 			lpfc_mbx_unreg_vpi(vports[i]);
6402 			shost = lpfc_shost_from_vport(vports[i]);
6403 			spin_lock_irq(shost->host_lock);
6404 			vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6405 			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
6406 			spin_unlock_irq(shost->host_lock);
6407 		}
6408 	lpfc_destroy_vport_work_array(phba, vports);
6409 	if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6410 		ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6411 		if (ndlp)
6412 			lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6413 		lpfc_cleanup_pending_mbox(phba->pport);
6414 		if (phba->sli_rev == LPFC_SLI_REV4)
6415 			lpfc_sli4_unreg_all_rpis(phba->pport);
6416 		lpfc_mbx_unreg_vpi(phba->pport);
6417 		shost = lpfc_shost_from_vport(phba->pport);
6418 		spin_lock_irq(shost->host_lock);
6419 		phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6420 		phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6421 		spin_unlock_irq(shost->host_lock);
6422 	}
6423 
6424 	/* Cleanup any outstanding ELS commands */
6425 	lpfc_els_flush_all_cmd(phba);
6426 
6427 	/* Unregister the physical port VFI */
6428 	rc = lpfc_issue_unreg_vfi(phba->pport);
6429 	return rc;
6430 }
6431 
6432 /**
6433  * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6434  * @phba: Pointer to hba context object.
6435  *
6436  * This function issues synchronous unregister FCF mailbox command to HBA to
6437  * unregister the currently registered FCF record. The driver does not reset
6438  * the driver FCF usage state flags.
6439  *
6440  * Return 0 if successfully issued, none-zero otherwise.
6441  */
6442 int
6443 lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6444 {
6445 	LPFC_MBOXQ_t *mbox;
6446 	int rc;
6447 
6448 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6449 	if (!mbox) {
6450 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6451 				"2551 UNREG_FCFI mbox allocation failed"
6452 				"HBA state x%x\n", phba->pport->port_state);
6453 		return -ENOMEM;
6454 	}
6455 	lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6456 	mbox->vport = phba->pport;
6457 	mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6458 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6459 
6460 	if (rc == MBX_NOT_FINISHED) {
6461 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6462 				"2552 Unregister FCFI command failed rc x%x "
6463 				"HBA state x%x\n",
6464 				rc, phba->pport->port_state);
6465 		return -EINVAL;
6466 	}
6467 	return 0;
6468 }
6469 
6470 /**
6471  * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6472  * @phba: Pointer to hba context object.
6473  *
6474  * This function unregisters the currently reigstered FCF. This function
6475  * also tries to find another FCF for discovery by rescan the HBA FCF table.
6476  */
6477 void
6478 lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6479 {
6480 	int rc;
6481 
6482 	/* Preparation for unregistering fcf */
6483 	rc = lpfc_unregister_fcf_prep(phba);
6484 	if (rc) {
6485 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6486 				"2748 Failed to prepare for unregistering "
6487 				"HBA's FCF record: rc=%d\n", rc);
6488 		return;
6489 	}
6490 
6491 	/* Now, unregister FCF record and reset HBA FCF state */
6492 	rc = lpfc_sli4_unregister_fcf(phba);
6493 	if (rc)
6494 		return;
6495 	/* Reset HBA FCF states after successful unregister FCF */
6496 	phba->fcf.fcf_flag = 0;
6497 	phba->fcf.current_rec.flag = 0;
6498 
6499 	/*
6500 	 * If driver is not unloading, check if there is any other
6501 	 * FCF record that can be used for discovery.
6502 	 */
6503 	if ((phba->pport->load_flag & FC_UNLOADING) ||
6504 	    (phba->link_state < LPFC_LINK_UP))
6505 		return;
6506 
6507 	/* This is considered as the initial FCF discovery scan */
6508 	spin_lock_irq(&phba->hbalock);
6509 	phba->fcf.fcf_flag |= FCF_INIT_DISC;
6510 	spin_unlock_irq(&phba->hbalock);
6511 
6512 	/* Reset FCF roundrobin bmask for new discovery */
6513 	lpfc_sli4_clear_fcf_rr_bmask(phba);
6514 
6515 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
6516 
6517 	if (rc) {
6518 		spin_lock_irq(&phba->hbalock);
6519 		phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6520 		spin_unlock_irq(&phba->hbalock);
6521 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6522 				"2553 lpfc_unregister_unused_fcf failed "
6523 				"to read FCF record HBA state x%x\n",
6524 				phba->pport->port_state);
6525 	}
6526 }
6527 
6528 /**
6529  * lpfc_unregister_fcf - Unregister the currently registered fcf record
6530  * @phba: Pointer to hba context object.
6531  *
6532  * This function just unregisters the currently reigstered FCF. It does not
6533  * try to find another FCF for discovery.
6534  */
6535 void
6536 lpfc_unregister_fcf(struct lpfc_hba *phba)
6537 {
6538 	int rc;
6539 
6540 	/* Preparation for unregistering fcf */
6541 	rc = lpfc_unregister_fcf_prep(phba);
6542 	if (rc) {
6543 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6544 				"2749 Failed to prepare for unregistering "
6545 				"HBA's FCF record: rc=%d\n", rc);
6546 		return;
6547 	}
6548 
6549 	/* Now, unregister FCF record and reset HBA FCF state */
6550 	rc = lpfc_sli4_unregister_fcf(phba);
6551 	if (rc)
6552 		return;
6553 	/* Set proper HBA FCF states after successful unregister FCF */
6554 	spin_lock_irq(&phba->hbalock);
6555 	phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6556 	spin_unlock_irq(&phba->hbalock);
6557 }
6558 
6559 /**
6560  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6561  * @phba: Pointer to hba context object.
6562  *
6563  * This function check if there are any connected remote port for the FCF and
6564  * if all the devices are disconnected, this function unregister FCFI.
6565  * This function also tries to use another FCF for discovery.
6566  */
6567 void
6568 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6569 {
6570 	/*
6571 	 * If HBA is not running in FIP mode, if HBA does not support
6572 	 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6573 	 * registered, do nothing.
6574 	 */
6575 	spin_lock_irq(&phba->hbalock);
6576 	if (!(phba->hba_flag & HBA_FCOE_MODE) ||
6577 	    !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
6578 	    !(phba->hba_flag & HBA_FIP_SUPPORT) ||
6579 	    (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
6580 	    (phba->pport->port_state == LPFC_FLOGI)) {
6581 		spin_unlock_irq(&phba->hbalock);
6582 		return;
6583 	}
6584 	spin_unlock_irq(&phba->hbalock);
6585 
6586 	if (lpfc_fcf_inuse(phba))
6587 		return;
6588 
6589 	lpfc_unregister_fcf_rescan(phba);
6590 }
6591 
6592 /**
6593  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6594  * @phba: Pointer to hba context object.
6595  * @buff: Buffer containing the FCF connection table as in the config
6596  *         region.
6597  * This function create driver data structure for the FCF connection
6598  * record table read from config region 23.
6599  */
6600 static void
6601 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6602 	uint8_t *buff)
6603 {
6604 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6605 	struct lpfc_fcf_conn_hdr *conn_hdr;
6606 	struct lpfc_fcf_conn_rec *conn_rec;
6607 	uint32_t record_count;
6608 	int i;
6609 
6610 	/* Free the current connect table */
6611 	list_for_each_entry_safe(conn_entry, next_conn_entry,
6612 		&phba->fcf_conn_rec_list, list) {
6613 		list_del_init(&conn_entry->list);
6614 		kfree(conn_entry);
6615 	}
6616 
6617 	conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6618 	record_count = conn_hdr->length * sizeof(uint32_t)/
6619 		sizeof(struct lpfc_fcf_conn_rec);
6620 
6621 	conn_rec = (struct lpfc_fcf_conn_rec *)
6622 		(buff + sizeof(struct lpfc_fcf_conn_hdr));
6623 
6624 	for (i = 0; i < record_count; i++) {
6625 		if (!(conn_rec[i].flags & FCFCNCT_VALID))
6626 			continue;
6627 		conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6628 			GFP_KERNEL);
6629 		if (!conn_entry) {
6630 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6631 					"2566 Failed to allocate connection"
6632 					" table entry\n");
6633 			return;
6634 		}
6635 
6636 		memcpy(&conn_entry->conn_rec, &conn_rec[i],
6637 			sizeof(struct lpfc_fcf_conn_rec));
6638 		list_add_tail(&conn_entry->list,
6639 			&phba->fcf_conn_rec_list);
6640 	}
6641 
6642 	if (!list_empty(&phba->fcf_conn_rec_list)) {
6643 		i = 0;
6644 		list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6645 				    list) {
6646 			conn_rec = &conn_entry->conn_rec;
6647 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6648 					"3345 FCF connection list rec[%02d]: "
6649 					"flags:x%04x, vtag:x%04x, "
6650 					"fabric_name:x%02x:%02x:%02x:%02x:"
6651 					"%02x:%02x:%02x:%02x, "
6652 					"switch_name:x%02x:%02x:%02x:%02x:"
6653 					"%02x:%02x:%02x:%02x\n", i++,
6654 					conn_rec->flags, conn_rec->vlan_tag,
6655 					conn_rec->fabric_name[0],
6656 					conn_rec->fabric_name[1],
6657 					conn_rec->fabric_name[2],
6658 					conn_rec->fabric_name[3],
6659 					conn_rec->fabric_name[4],
6660 					conn_rec->fabric_name[5],
6661 					conn_rec->fabric_name[6],
6662 					conn_rec->fabric_name[7],
6663 					conn_rec->switch_name[0],
6664 					conn_rec->switch_name[1],
6665 					conn_rec->switch_name[2],
6666 					conn_rec->switch_name[3],
6667 					conn_rec->switch_name[4],
6668 					conn_rec->switch_name[5],
6669 					conn_rec->switch_name[6],
6670 					conn_rec->switch_name[7]);
6671 		}
6672 	}
6673 }
6674 
6675 /**
6676  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6677  * @phba: Pointer to hba context object.
6678  * @buff: Buffer containing the FCoE parameter data structure.
6679  *
6680  *  This function update driver data structure with config
6681  *  parameters read from config region 23.
6682  */
6683 static void
6684 lpfc_read_fcoe_param(struct lpfc_hba *phba,
6685 			uint8_t *buff)
6686 {
6687 	struct lpfc_fip_param_hdr *fcoe_param_hdr;
6688 	struct lpfc_fcoe_params *fcoe_param;
6689 
6690 	fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6691 		buff;
6692 	fcoe_param = (struct lpfc_fcoe_params *)
6693 		(buff + sizeof(struct lpfc_fip_param_hdr));
6694 
6695 	if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6696 		(fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6697 		return;
6698 
6699 	if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6700 		phba->valid_vlan = 1;
6701 		phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6702 			0xFFF;
6703 	}
6704 
6705 	phba->fc_map[0] = fcoe_param->fc_map[0];
6706 	phba->fc_map[1] = fcoe_param->fc_map[1];
6707 	phba->fc_map[2] = fcoe_param->fc_map[2];
6708 	return;
6709 }
6710 
6711 /**
6712  * lpfc_get_rec_conf23 - Get a record type in config region data.
6713  * @buff: Buffer containing config region 23 data.
6714  * @size: Size of the data buffer.
6715  * @rec_type: Record type to be searched.
6716  *
6717  * This function searches config region data to find the beginning
6718  * of the record specified by record_type. If record found, this
6719  * function return pointer to the record else return NULL.
6720  */
6721 static uint8_t *
6722 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6723 {
6724 	uint32_t offset = 0, rec_length;
6725 
6726 	if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6727 		(size < sizeof(uint32_t)))
6728 		return NULL;
6729 
6730 	rec_length = buff[offset + 1];
6731 
6732 	/*
6733 	 * One TLV record has one word header and number of data words
6734 	 * specified in the rec_length field of the record header.
6735 	 */
6736 	while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6737 		<= size) {
6738 		if (buff[offset] == rec_type)
6739 			return &buff[offset];
6740 
6741 		if (buff[offset] == LPFC_REGION23_LAST_REC)
6742 			return NULL;
6743 
6744 		offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6745 		rec_length = buff[offset + 1];
6746 	}
6747 	return NULL;
6748 }
6749 
6750 /**
6751  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6752  * @phba: Pointer to lpfc_hba data structure.
6753  * @buff: Buffer containing config region 23 data.
6754  * @size: Size of the data buffer.
6755  *
6756  * This function parses the FCoE config parameters in config region 23 and
6757  * populate driver data structure with the parameters.
6758  */
6759 void
6760 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6761 		uint8_t *buff,
6762 		uint32_t size)
6763 {
6764 	uint32_t offset = 0;
6765 	uint8_t *rec_ptr;
6766 
6767 	/*
6768 	 * If data size is less than 2 words signature and version cannot be
6769 	 * verified.
6770 	 */
6771 	if (size < 2*sizeof(uint32_t))
6772 		return;
6773 
6774 	/* Check the region signature first */
6775 	if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6776 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6777 			"2567 Config region 23 has bad signature\n");
6778 		return;
6779 	}
6780 
6781 	offset += 4;
6782 
6783 	/* Check the data structure version */
6784 	if (buff[offset] != LPFC_REGION23_VERSION) {
6785 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6786 				"2568 Config region 23 has bad version\n");
6787 		return;
6788 	}
6789 	offset += 4;
6790 
6791 	/* Read FCoE param record */
6792 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6793 			size - offset, FCOE_PARAM_TYPE);
6794 	if (rec_ptr)
6795 		lpfc_read_fcoe_param(phba, rec_ptr);
6796 
6797 	/* Read FCF connection table */
6798 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6799 		size - offset, FCOE_CONN_TBL_TYPE);
6800 	if (rec_ptr)
6801 		lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6802 
6803 }
6804