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