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