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