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