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