xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_hbadisc.c (revision 31af04cd)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2018 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 
122 	rdata = rport->dd_data;
123 	ndlp = rdata->pnode;
124 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
125 		return;
126 
127 	vport = ndlp->vport;
128 	phba  = vport->phba;
129 
130 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
131 		"rport devlosscb: sid:x%x did:x%x flg:x%x",
132 		ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
133 
134 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
135 			 "3181 dev_loss_callbk x%06x, rport %p flg x%x\n",
136 			 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
137 
138 	/* Don't defer this if we are in the process of deleting the vport
139 	 * or unloading the driver. The unload will cleanup the node
140 	 * appropriately we just need to cleanup the ndlp rport info here.
141 	 */
142 	if (vport->load_flag & FC_UNLOADING) {
143 		put_node = rdata->pnode != NULL;
144 		put_rport = ndlp->rport != NULL;
145 		rdata->pnode = NULL;
146 		ndlp->rport = NULL;
147 		if (put_node)
148 			lpfc_nlp_put(ndlp);
149 		if (put_rport)
150 			put_device(&rport->dev);
151 		return;
152 	}
153 
154 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
155 		return;
156 
157 	if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
158 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
159 				"6789 rport name %llx != node port name %llx",
160 				rport->port_name,
161 				wwn_to_u64(ndlp->nlp_portname.u.wwn));
162 
163 	evtp = &ndlp->dev_loss_evt;
164 
165 	if (!list_empty(&evtp->evt_listp)) {
166 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
167 				"6790 rport name %llx dev_loss_evt pending",
168 				rport->port_name);
169 		return;
170 	}
171 
172 	shost = lpfc_shost_from_vport(vport);
173 	spin_lock_irq(shost->host_lock);
174 	ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
175 	spin_unlock_irq(shost->host_lock);
176 
177 	/* We need to hold the node by incrementing the reference
178 	 * count until this queued work is done
179 	 */
180 	evtp->evt_arg1  = lpfc_nlp_get(ndlp);
181 
182 	spin_lock_irq(&phba->hbalock);
183 	if (evtp->evt_arg1) {
184 		evtp->evt = LPFC_EVT_DEV_LOSS;
185 		list_add_tail(&evtp->evt_listp, &phba->work_list);
186 		lpfc_worker_wake_up(phba);
187 	}
188 	spin_unlock_irq(&phba->hbalock);
189 
190 	return;
191 }
192 
193 /**
194  * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
195  * @ndlp: Pointer to remote node object.
196  *
197  * This function is called from the worker thread when devloss timeout timer
198  * expires. For SLI4 host, this routine shall return 1 when at lease one
199  * remote node, including this @ndlp, is still in use of FCF; otherwise, this
200  * routine shall return 0 when there is no remote node is still in use of FCF
201  * when devloss timeout happened to this @ndlp.
202  **/
203 static int
204 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
205 {
206 	struct lpfc_rport_data *rdata;
207 	struct fc_rport   *rport;
208 	struct lpfc_vport *vport;
209 	struct lpfc_hba   *phba;
210 	struct Scsi_Host  *shost;
211 	uint8_t *name;
212 	int  put_node;
213 	int warn_on = 0;
214 	int fcf_inuse = 0;
215 
216 	rport = ndlp->rport;
217 	vport = ndlp->vport;
218 	shost = lpfc_shost_from_vport(vport);
219 
220 	spin_lock_irq(shost->host_lock);
221 	ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
222 	spin_unlock_irq(shost->host_lock);
223 
224 	if (!rport)
225 		return fcf_inuse;
226 
227 	name = (uint8_t *) &ndlp->nlp_portname;
228 	phba  = vport->phba;
229 
230 	if (phba->sli_rev == LPFC_SLI_REV4)
231 		fcf_inuse = lpfc_fcf_inuse(phba);
232 
233 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
234 		"rport devlosstmo:did:x%x type:x%x id:x%x",
235 		ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
236 
237 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
238 			 "3182 dev_loss_tmo_handler x%06x, rport %p flg x%x\n",
239 			 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
240 
241 	/*
242 	 * lpfc_nlp_remove if reached with dangling rport drops the
243 	 * reference. To make sure that does not happen clear rport
244 	 * pointer in ndlp before lpfc_nlp_put.
245 	 */
246 	rdata = rport->dd_data;
247 
248 	/* Don't defer this if we are in the process of deleting the vport
249 	 * or unloading the driver. The unload will cleanup the node
250 	 * appropriately we just need to cleanup the ndlp rport info here.
251 	 */
252 	if (vport->load_flag & FC_UNLOADING) {
253 		if (ndlp->nlp_sid != NLP_NO_SID) {
254 			/* flush the target */
255 			lpfc_sli_abort_iocb(vport,
256 					    &phba->sli.sli3_ring[LPFC_FCP_RING],
257 					    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
258 		}
259 		put_node = rdata->pnode != NULL;
260 		rdata->pnode = NULL;
261 		ndlp->rport = NULL;
262 		if (put_node)
263 			lpfc_nlp_put(ndlp);
264 		put_device(&rport->dev);
265 
266 		return fcf_inuse;
267 	}
268 
269 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
270 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
271 				 "0284 Devloss timeout Ignored on "
272 				 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
273 				 "NPort x%x\n",
274 				 *name, *(name+1), *(name+2), *(name+3),
275 				 *(name+4), *(name+5), *(name+6), *(name+7),
276 				 ndlp->nlp_DID);
277 		return fcf_inuse;
278 	}
279 
280 	put_node = rdata->pnode != NULL;
281 	rdata->pnode = NULL;
282 	ndlp->rport = NULL;
283 	if (put_node)
284 		lpfc_nlp_put(ndlp);
285 	put_device(&rport->dev);
286 
287 	if (ndlp->nlp_type & NLP_FABRIC)
288 		return fcf_inuse;
289 
290 	if (ndlp->nlp_sid != NLP_NO_SID) {
291 		warn_on = 1;
292 		lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
293 				    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
294 	}
295 
296 	if (warn_on) {
297 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
298 				 "0203 Devloss timeout on "
299 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
300 				 "NPort x%06x Data: x%x x%x x%x\n",
301 				 *name, *(name+1), *(name+2), *(name+3),
302 				 *(name+4), *(name+5), *(name+6), *(name+7),
303 				 ndlp->nlp_DID, ndlp->nlp_flag,
304 				 ndlp->nlp_state, ndlp->nlp_rpi);
305 	} else {
306 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
307 				 "0204 Devloss timeout on "
308 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
309 				 "NPort x%06x Data: x%x x%x x%x\n",
310 				 *name, *(name+1), *(name+2), *(name+3),
311 				 *(name+4), *(name+5), *(name+6), *(name+7),
312 				 ndlp->nlp_DID, ndlp->nlp_flag,
313 				 ndlp->nlp_state, ndlp->nlp_rpi);
314 	}
315 
316 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
317 	    !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
318 	    (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
319 	    (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
320 	    (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
321 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
322 
323 	return fcf_inuse;
324 }
325 
326 /**
327  * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
328  * @phba: Pointer to hba context object.
329  * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
330  * @nlp_did: remote node identifer with devloss timeout.
331  *
332  * This function is called from the worker thread after invoking devloss
333  * timeout handler and releasing the reference count for the ndlp with
334  * which the devloss timeout was handled for SLI4 host. For the devloss
335  * timeout of the last remote node which had been in use of FCF, when this
336  * routine is invoked, it shall be guaranteed that none of the remote are
337  * in-use of FCF. When devloss timeout to the last remote using the FCF,
338  * if the FIP engine is neither in FCF table scan process nor roundrobin
339  * failover process, the in-use FCF shall be unregistered. If the FIP
340  * engine is in FCF discovery process, the devloss timeout state shall
341  * be set for either the FCF table scan process or roundrobin failover
342  * process to unregister the in-use FCF.
343  **/
344 static void
345 lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
346 				    uint32_t nlp_did)
347 {
348 	/* If devloss timeout happened to a remote node when FCF had no
349 	 * longer been in-use, do nothing.
350 	 */
351 	if (!fcf_inuse)
352 		return;
353 
354 	if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
355 		spin_lock_irq(&phba->hbalock);
356 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
357 			if (phba->hba_flag & HBA_DEVLOSS_TMO) {
358 				spin_unlock_irq(&phba->hbalock);
359 				return;
360 			}
361 			phba->hba_flag |= HBA_DEVLOSS_TMO;
362 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
363 					"2847 Last remote node (x%x) using "
364 					"FCF devloss tmo\n", nlp_did);
365 		}
366 		if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
367 			spin_unlock_irq(&phba->hbalock);
368 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
369 					"2868 Devloss tmo to FCF rediscovery "
370 					"in progress\n");
371 			return;
372 		}
373 		if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
374 			spin_unlock_irq(&phba->hbalock);
375 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
376 					"2869 Devloss tmo to idle FIP engine, "
377 					"unreg in-use FCF and rescan.\n");
378 			/* Unregister in-use FCF and rescan */
379 			lpfc_unregister_fcf_rescan(phba);
380 			return;
381 		}
382 		spin_unlock_irq(&phba->hbalock);
383 		if (phba->hba_flag & FCF_TS_INPROG)
384 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
385 					"2870 FCF table scan in progress\n");
386 		if (phba->hba_flag & FCF_RR_INPROG)
387 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
388 					"2871 FLOGI roundrobin FCF failover "
389 					"in progress\n");
390 	}
391 	lpfc_unregister_unused_fcf(phba);
392 }
393 
394 /**
395  * lpfc_alloc_fast_evt - Allocates data structure for posting event
396  * @phba: Pointer to hba context object.
397  *
398  * This function is called from the functions which need to post
399  * events from interrupt context. This function allocates data
400  * structure required for posting event. It also keeps track of
401  * number of events pending and prevent event storm when there are
402  * too many events.
403  **/
404 struct lpfc_fast_path_event *
405 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
406 	struct lpfc_fast_path_event *ret;
407 
408 	/* If there are lot of fast event do not exhaust memory due to this */
409 	if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
410 		return NULL;
411 
412 	ret = kzalloc(sizeof(struct lpfc_fast_path_event),
413 			GFP_ATOMIC);
414 	if (ret) {
415 		atomic_inc(&phba->fast_event_count);
416 		INIT_LIST_HEAD(&ret->work_evt.evt_listp);
417 		ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
418 	}
419 	return ret;
420 }
421 
422 /**
423  * lpfc_free_fast_evt - Frees event data structure
424  * @phba: Pointer to hba context object.
425  * @evt:  Event object which need to be freed.
426  *
427  * This function frees the data structure required for posting
428  * events.
429  **/
430 void
431 lpfc_free_fast_evt(struct lpfc_hba *phba,
432 		struct lpfc_fast_path_event *evt) {
433 
434 	atomic_dec(&phba->fast_event_count);
435 	kfree(evt);
436 }
437 
438 /**
439  * lpfc_send_fastpath_evt - Posts events generated from fast path
440  * @phba: Pointer to hba context object.
441  * @evtp: Event data structure.
442  *
443  * This function is called from worker thread, when the interrupt
444  * context need to post an event. This function posts the event
445  * to fc transport netlink interface.
446  **/
447 static void
448 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
449 		struct lpfc_work_evt *evtp)
450 {
451 	unsigned long evt_category, evt_sub_category;
452 	struct lpfc_fast_path_event *fast_evt_data;
453 	char *evt_data;
454 	uint32_t evt_data_size;
455 	struct Scsi_Host *shost;
456 
457 	fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
458 		work_evt);
459 
460 	evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
461 	evt_sub_category = (unsigned long) fast_evt_data->un.
462 			fabric_evt.subcategory;
463 	shost = lpfc_shost_from_vport(fast_evt_data->vport);
464 	if (evt_category == FC_REG_FABRIC_EVENT) {
465 		if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
466 			evt_data = (char *) &fast_evt_data->un.read_check_error;
467 			evt_data_size = sizeof(fast_evt_data->un.
468 				read_check_error);
469 		} else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
470 			(evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
471 			evt_data = (char *) &fast_evt_data->un.fabric_evt;
472 			evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
473 		} else {
474 			lpfc_free_fast_evt(phba, fast_evt_data);
475 			return;
476 		}
477 	} else if (evt_category == FC_REG_SCSI_EVENT) {
478 		switch (evt_sub_category) {
479 		case LPFC_EVENT_QFULL:
480 		case LPFC_EVENT_DEVBSY:
481 			evt_data = (char *) &fast_evt_data->un.scsi_evt;
482 			evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
483 			break;
484 		case LPFC_EVENT_CHECK_COND:
485 			evt_data = (char *) &fast_evt_data->un.check_cond_evt;
486 			evt_data_size =  sizeof(fast_evt_data->un.
487 				check_cond_evt);
488 			break;
489 		case LPFC_EVENT_VARQUEDEPTH:
490 			evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
491 			evt_data_size = sizeof(fast_evt_data->un.
492 				queue_depth_evt);
493 			break;
494 		default:
495 			lpfc_free_fast_evt(phba, fast_evt_data);
496 			return;
497 		}
498 	} else {
499 		lpfc_free_fast_evt(phba, fast_evt_data);
500 		return;
501 	}
502 
503 	if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
504 		fc_host_post_vendor_event(shost,
505 			fc_get_event_number(),
506 			evt_data_size,
507 			evt_data,
508 			LPFC_NL_VENDOR_ID);
509 
510 	lpfc_free_fast_evt(phba, fast_evt_data);
511 	return;
512 }
513 
514 static void
515 lpfc_work_list_done(struct lpfc_hba *phba)
516 {
517 	struct lpfc_work_evt  *evtp = NULL;
518 	struct lpfc_nodelist  *ndlp;
519 	int free_evt;
520 	int fcf_inuse;
521 	uint32_t nlp_did;
522 
523 	spin_lock_irq(&phba->hbalock);
524 	while (!list_empty(&phba->work_list)) {
525 		list_remove_head((&phba->work_list), evtp, typeof(*evtp),
526 				 evt_listp);
527 		spin_unlock_irq(&phba->hbalock);
528 		free_evt = 1;
529 		switch (evtp->evt) {
530 		case LPFC_EVT_ELS_RETRY:
531 			ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
532 			lpfc_els_retry_delay_handler(ndlp);
533 			free_evt = 0; /* evt is part of ndlp */
534 			/* decrement the node reference count held
535 			 * for this queued work
536 			 */
537 			lpfc_nlp_put(ndlp);
538 			break;
539 		case LPFC_EVT_DEV_LOSS:
540 			ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
541 			fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
542 			free_evt = 0;
543 			/* decrement the node reference count held for
544 			 * this queued work
545 			 */
546 			nlp_did = ndlp->nlp_DID;
547 			lpfc_nlp_put(ndlp);
548 			if (phba->sli_rev == LPFC_SLI_REV4)
549 				lpfc_sli4_post_dev_loss_tmo_handler(phba,
550 								    fcf_inuse,
551 								    nlp_did);
552 			break;
553 		case LPFC_EVT_ONLINE:
554 			if (phba->link_state < LPFC_LINK_DOWN)
555 				*(int *) (evtp->evt_arg1) = lpfc_online(phba);
556 			else
557 				*(int *) (evtp->evt_arg1) = 0;
558 			complete((struct completion *)(evtp->evt_arg2));
559 			break;
560 		case LPFC_EVT_OFFLINE_PREP:
561 			if (phba->link_state >= LPFC_LINK_DOWN)
562 				lpfc_offline_prep(phba, LPFC_MBX_WAIT);
563 			*(int *)(evtp->evt_arg1) = 0;
564 			complete((struct completion *)(evtp->evt_arg2));
565 			break;
566 		case LPFC_EVT_OFFLINE:
567 			lpfc_offline(phba);
568 			lpfc_sli_brdrestart(phba);
569 			*(int *)(evtp->evt_arg1) =
570 				lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
571 			lpfc_unblock_mgmt_io(phba);
572 			complete((struct completion *)(evtp->evt_arg2));
573 			break;
574 		case LPFC_EVT_WARM_START:
575 			lpfc_offline(phba);
576 			lpfc_reset_barrier(phba);
577 			lpfc_sli_brdreset(phba);
578 			lpfc_hba_down_post(phba);
579 			*(int *)(evtp->evt_arg1) =
580 				lpfc_sli_brdready(phba, HS_MBRDY);
581 			lpfc_unblock_mgmt_io(phba);
582 			complete((struct completion *)(evtp->evt_arg2));
583 			break;
584 		case LPFC_EVT_KILL:
585 			lpfc_offline(phba);
586 			*(int *)(evtp->evt_arg1)
587 				= (phba->pport->stopped)
588 				        ? 0 : lpfc_sli_brdkill(phba);
589 			lpfc_unblock_mgmt_io(phba);
590 			complete((struct completion *)(evtp->evt_arg2));
591 			break;
592 		case LPFC_EVT_FASTPATH_MGMT_EVT:
593 			lpfc_send_fastpath_evt(phba, evtp);
594 			free_evt = 0;
595 			break;
596 		case LPFC_EVT_RESET_HBA:
597 			if (!(phba->pport->load_flag & FC_UNLOADING))
598 				lpfc_reset_hba(phba);
599 			break;
600 		}
601 		if (free_evt)
602 			kfree(evtp);
603 		spin_lock_irq(&phba->hbalock);
604 	}
605 	spin_unlock_irq(&phba->hbalock);
606 
607 }
608 
609 static void
610 lpfc_work_done(struct lpfc_hba *phba)
611 {
612 	struct lpfc_sli_ring *pring;
613 	uint32_t ha_copy, status, control, work_port_events;
614 	struct lpfc_vport **vports;
615 	struct lpfc_vport *vport;
616 	int i;
617 
618 	spin_lock_irq(&phba->hbalock);
619 	ha_copy = phba->work_ha;
620 	phba->work_ha = 0;
621 	spin_unlock_irq(&phba->hbalock);
622 
623 	/* First, try to post the next mailbox command to SLI4 device */
624 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
625 		lpfc_sli4_post_async_mbox(phba);
626 
627 	if (ha_copy & HA_ERATT)
628 		/* Handle the error attention event */
629 		lpfc_handle_eratt(phba);
630 
631 	if (ha_copy & HA_MBATT)
632 		lpfc_sli_handle_mb_event(phba);
633 
634 	if (ha_copy & HA_LATT)
635 		lpfc_handle_latt(phba);
636 
637 	/* Process SLI4 events */
638 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
639 		if (phba->hba_flag & HBA_RRQ_ACTIVE)
640 			lpfc_handle_rrq_active(phba);
641 		if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
642 			lpfc_sli4_fcp_xri_abort_event_proc(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 lpfc_hba  *phba = vport->phba;
863 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
864 
865 	if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
866 		fc_host_post_event(shost, fc_get_event_number(),
867 				   FCH_EVT_LINKDOWN, 0);
868 
869 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
870 		"Link Down:       state:x%x rtry:x%x flg:x%x",
871 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
872 
873 	lpfc_port_link_failure(vport);
874 
875 	/* Stop delayed Nport discovery */
876 	spin_lock_irq(shost->host_lock);
877 	vport->fc_flag &= ~FC_DISC_DELAYED;
878 	spin_unlock_irq(shost->host_lock);
879 	del_timer_sync(&vport->delayed_disc_tmo);
880 }
881 
882 int
883 lpfc_linkdown(struct lpfc_hba *phba)
884 {
885 	struct lpfc_vport *vport = phba->pport;
886 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
887 	struct lpfc_vport **vports;
888 	LPFC_MBOXQ_t          *mb;
889 	int i;
890 
891 	if (phba->link_state == LPFC_LINK_DOWN) {
892 		if (phba->sli4_hba.conf_trunk) {
893 			phba->trunk_link.link0.state = 0;
894 			phba->trunk_link.link1.state = 0;
895 			phba->trunk_link.link2.state = 0;
896 			phba->trunk_link.link3.state = 0;
897 		}
898 		return 0;
899 	}
900 	/* Block all SCSI stack I/Os */
901 	lpfc_scsi_dev_block(phba);
902 
903 	phba->defer_flogi_acc_flag = false;
904 
905 	spin_lock_irq(&phba->hbalock);
906 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
907 	spin_unlock_irq(&phba->hbalock);
908 	if (phba->link_state > LPFC_LINK_DOWN) {
909 		phba->link_state = LPFC_LINK_DOWN;
910 		if (phba->sli4_hba.conf_trunk) {
911 			phba->trunk_link.link0.state = 0;
912 			phba->trunk_link.link1.state = 0;
913 			phba->trunk_link.link2.state = 0;
914 			phba->trunk_link.link3.state = 0;
915 		}
916 		spin_lock_irq(shost->host_lock);
917 		phba->pport->fc_flag &= ~FC_LBIT;
918 		spin_unlock_irq(shost->host_lock);
919 	}
920 	vports = lpfc_create_vport_work_array(phba);
921 	if (vports != NULL) {
922 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
923 			/* Issue a LINK DOWN event to all nodes */
924 			lpfc_linkdown_port(vports[i]);
925 
926 			vports[i]->fc_myDID = 0;
927 
928 			if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
929 			    (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
930 				if (phba->nvmet_support)
931 					lpfc_nvmet_update_targetport(phba);
932 				else
933 					lpfc_nvme_update_localport(vports[i]);
934 			}
935 		}
936 	}
937 	lpfc_destroy_vport_work_array(phba, vports);
938 	/* Clean up any firmware default rpi's */
939 	mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
940 	if (mb) {
941 		lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
942 		mb->vport = vport;
943 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
944 		if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
945 		    == MBX_NOT_FINISHED) {
946 			mempool_free(mb, phba->mbox_mem_pool);
947 		}
948 	}
949 
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 (phba->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 
3123 	spin_lock_irq(&phba->hbalock);
3124 	phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3125 
3126 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3127 		switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3128 		case LPFC_LINK_SPEED_1GHZ:
3129 		case LPFC_LINK_SPEED_2GHZ:
3130 		case LPFC_LINK_SPEED_4GHZ:
3131 		case LPFC_LINK_SPEED_8GHZ:
3132 		case LPFC_LINK_SPEED_10GHZ:
3133 		case LPFC_LINK_SPEED_16GHZ:
3134 		case LPFC_LINK_SPEED_32GHZ:
3135 		case LPFC_LINK_SPEED_64GHZ:
3136 		case LPFC_LINK_SPEED_128GHZ:
3137 			break;
3138 		default:
3139 			phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3140 			break;
3141 		}
3142 	}
3143 
3144 	if (phba->fc_topology &&
3145 	    phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3146 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3147 				"3314 Toplogy changed was 0x%x is 0x%x\n",
3148 				phba->fc_topology,
3149 				bf_get(lpfc_mbx_read_top_topology, la));
3150 		phba->fc_topology_changed = 1;
3151 	}
3152 
3153 	phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
3154 	phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
3155 
3156 	shost = lpfc_shost_from_vport(vport);
3157 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3158 		phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
3159 
3160 		/* if npiv is enabled and this adapter supports npiv log
3161 		 * a message that npiv is not supported in this topology
3162 		 */
3163 		if (phba->cfg_enable_npiv && phba->max_vpi)
3164 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3165 				"1309 Link Up Event npiv not supported in loop "
3166 				"topology\n");
3167 				/* Get Loop Map information */
3168 		if (bf_get(lpfc_mbx_read_top_il, la))
3169 			fc_flags |= FC_LBIT;
3170 
3171 		vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3172 		i = la->lilpBde64.tus.f.bdeSize;
3173 
3174 		if (i == 0) {
3175 			phba->alpa_map[0] = 0;
3176 		} else {
3177 			if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
3178 				int numalpa, j, k;
3179 				union {
3180 					uint8_t pamap[16];
3181 					struct {
3182 						uint32_t wd1;
3183 						uint32_t wd2;
3184 						uint32_t wd3;
3185 						uint32_t wd4;
3186 					} pa;
3187 				} un;
3188 				numalpa = phba->alpa_map[0];
3189 				j = 0;
3190 				while (j < numalpa) {
3191 					memset(un.pamap, 0, 16);
3192 					for (k = 1; j < numalpa; k++) {
3193 						un.pamap[k - 1] =
3194 							phba->alpa_map[j + 1];
3195 						j++;
3196 						if (k == 16)
3197 							break;
3198 					}
3199 					/* Link Up Event ALPA map */
3200 					lpfc_printf_log(phba,
3201 							KERN_WARNING,
3202 							LOG_LINK_EVENT,
3203 							"1304 Link Up Event "
3204 							"ALPA map Data: x%x "
3205 							"x%x x%x x%x\n",
3206 							un.pa.wd1, un.pa.wd2,
3207 							un.pa.wd3, un.pa.wd4);
3208 				}
3209 			}
3210 		}
3211 	} else {
3212 		if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
3213 			if (phba->max_vpi && phba->cfg_enable_npiv &&
3214 			   (phba->sli_rev >= LPFC_SLI_REV3))
3215 				phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3216 		}
3217 		vport->fc_myDID = phba->fc_pref_DID;
3218 		fc_flags |= FC_LBIT;
3219 	}
3220 	spin_unlock_irq(&phba->hbalock);
3221 
3222 	if (fc_flags) {
3223 		spin_lock_irq(shost->host_lock);
3224 		vport->fc_flag |= fc_flags;
3225 		spin_unlock_irq(shost->host_lock);
3226 	}
3227 
3228 	lpfc_linkup(phba);
3229 	sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3230 	if (!sparam_mbox)
3231 		goto out;
3232 
3233 	rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3234 	if (rc) {
3235 		mempool_free(sparam_mbox, phba->mbox_mem_pool);
3236 		goto out;
3237 	}
3238 	sparam_mbox->vport = vport;
3239 	sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3240 	rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3241 	if (rc == MBX_NOT_FINISHED) {
3242 		mp = (struct lpfc_dmabuf *)sparam_mbox->ctx_buf;
3243 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
3244 		kfree(mp);
3245 		mempool_free(sparam_mbox, phba->mbox_mem_pool);
3246 		goto out;
3247 	}
3248 
3249 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3250 		cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3251 		if (!cfglink_mbox)
3252 			goto out;
3253 		vport->port_state = LPFC_LOCAL_CFG_LINK;
3254 		lpfc_config_link(phba, cfglink_mbox);
3255 		cfglink_mbox->vport = vport;
3256 		cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
3257 		rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
3258 		if (rc == MBX_NOT_FINISHED) {
3259 			mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3260 			goto out;
3261 		}
3262 	} else {
3263 		vport->port_state = LPFC_VPORT_UNKNOWN;
3264 		/*
3265 		 * Add the driver's default FCF record at FCF index 0 now. This
3266 		 * is phase 1 implementation that support FCF index 0 and driver
3267 		 * defaults.
3268 		 */
3269 		if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
3270 			fcf_record = kzalloc(sizeof(struct fcf_record),
3271 					GFP_KERNEL);
3272 			if (unlikely(!fcf_record)) {
3273 				lpfc_printf_log(phba, KERN_ERR,
3274 					LOG_MBOX | LOG_SLI,
3275 					"2554 Could not allocate memory for "
3276 					"fcf record\n");
3277 				rc = -ENODEV;
3278 				goto out;
3279 			}
3280 
3281 			lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3282 						LPFC_FCOE_FCF_DEF_INDEX);
3283 			rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3284 			if (unlikely(rc)) {
3285 				lpfc_printf_log(phba, KERN_ERR,
3286 					LOG_MBOX | LOG_SLI,
3287 					"2013 Could not manually add FCF "
3288 					"record 0, status %d\n", rc);
3289 				rc = -ENODEV;
3290 				kfree(fcf_record);
3291 				goto out;
3292 			}
3293 			kfree(fcf_record);
3294 		}
3295 		/*
3296 		 * The driver is expected to do FIP/FCF. Call the port
3297 		 * and get the FCF Table.
3298 		 */
3299 		spin_lock_irq(&phba->hbalock);
3300 		if (phba->hba_flag & FCF_TS_INPROG) {
3301 			spin_unlock_irq(&phba->hbalock);
3302 			return;
3303 		}
3304 		/* This is the initial FCF discovery scan */
3305 		phba->fcf.fcf_flag |= FCF_INIT_DISC;
3306 		spin_unlock_irq(&phba->hbalock);
3307 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3308 				"2778 Start FCF table scan at linkup\n");
3309 		rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3310 						     LPFC_FCOE_FCF_GET_FIRST);
3311 		if (rc) {
3312 			spin_lock_irq(&phba->hbalock);
3313 			phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3314 			spin_unlock_irq(&phba->hbalock);
3315 			goto out;
3316 		}
3317 		/* Reset FCF roundrobin bmask for new discovery */
3318 		lpfc_sli4_clear_fcf_rr_bmask(phba);
3319 	}
3320 
3321 	return;
3322 out:
3323 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3324 	lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3325 			 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3326 			 vport->port_state, sparam_mbox, cfglink_mbox);
3327 	lpfc_issue_clear_la(phba, vport);
3328 	return;
3329 }
3330 
3331 static void
3332 lpfc_enable_la(struct lpfc_hba *phba)
3333 {
3334 	uint32_t control;
3335 	struct lpfc_sli *psli = &phba->sli;
3336 	spin_lock_irq(&phba->hbalock);
3337 	psli->sli_flag |= LPFC_PROCESS_LA;
3338 	if (phba->sli_rev <= LPFC_SLI_REV3) {
3339 		control = readl(phba->HCregaddr);
3340 		control |= HC_LAINT_ENA;
3341 		writel(control, phba->HCregaddr);
3342 		readl(phba->HCregaddr); /* flush */
3343 	}
3344 	spin_unlock_irq(&phba->hbalock);
3345 }
3346 
3347 static void
3348 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3349 {
3350 	lpfc_linkdown(phba);
3351 	lpfc_enable_la(phba);
3352 	lpfc_unregister_unused_fcf(phba);
3353 	/* turn on Link Attention interrupts - no CLEAR_LA needed */
3354 }
3355 
3356 
3357 /*
3358  * This routine handles processing a READ_TOPOLOGY mailbox
3359  * command upon completion. It is setup in the LPFC_MBOXQ
3360  * as the completion routine when the command is
3361  * handed off to the SLI layer. SLI4 only.
3362  */
3363 void
3364 lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3365 {
3366 	struct lpfc_vport *vport = pmb->vport;
3367 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3368 	struct lpfc_mbx_read_top *la;
3369 	struct lpfc_sli_ring *pring;
3370 	MAILBOX_t *mb = &pmb->u.mb;
3371 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3372 	uint8_t attn_type;
3373 
3374 	/* Unblock ELS traffic */
3375 	pring = lpfc_phba_elsring(phba);
3376 	if (pring)
3377 		pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3378 
3379 	/* Check for error */
3380 	if (mb->mbxStatus) {
3381 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3382 				"1307 READ_LA mbox error x%x state x%x\n",
3383 				mb->mbxStatus, vport->port_state);
3384 		lpfc_mbx_issue_link_down(phba);
3385 		phba->link_state = LPFC_HBA_ERROR;
3386 		goto lpfc_mbx_cmpl_read_topology_free_mbuf;
3387 	}
3388 
3389 	la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3390 	attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
3391 
3392 	memcpy(&phba->alpa_map[0], mp->virt, 128);
3393 
3394 	spin_lock_irq(shost->host_lock);
3395 	if (bf_get(lpfc_mbx_read_top_pb, la))
3396 		vport->fc_flag |= FC_BYPASSED_MODE;
3397 	else
3398 		vport->fc_flag &= ~FC_BYPASSED_MODE;
3399 	spin_unlock_irq(shost->host_lock);
3400 
3401 	if (phba->fc_eventTag <= la->eventTag) {
3402 		phba->fc_stat.LinkMultiEvent++;
3403 		if (attn_type == LPFC_ATT_LINK_UP)
3404 			if (phba->fc_eventTag != 0)
3405 				lpfc_linkdown(phba);
3406 	}
3407 
3408 	phba->fc_eventTag = la->eventTag;
3409 	if (phba->sli_rev < LPFC_SLI_REV4) {
3410 		spin_lock_irq(&phba->hbalock);
3411 		if (bf_get(lpfc_mbx_read_top_mm, la))
3412 			phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3413 		else
3414 			phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3415 		spin_unlock_irq(&phba->hbalock);
3416 	}
3417 
3418 	phba->link_events++;
3419 	if ((attn_type == LPFC_ATT_LINK_UP) &&
3420 	    !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
3421 		phba->fc_stat.LinkUp++;
3422 		if (phba->link_flag & LS_LOOPBACK_MODE) {
3423 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3424 					"1306 Link Up Event in loop back mode "
3425 					"x%x received Data: x%x x%x x%x x%x\n",
3426 					la->eventTag, phba->fc_eventTag,
3427 					bf_get(lpfc_mbx_read_top_alpa_granted,
3428 					       la),
3429 					bf_get(lpfc_mbx_read_top_link_spd, la),
3430 					phba->alpa_map[0]);
3431 		} else {
3432 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3433 					"1303 Link Up Event x%x received "
3434 					"Data: x%x x%x x%x x%x x%x x%x %d\n",
3435 					la->eventTag, phba->fc_eventTag,
3436 					bf_get(lpfc_mbx_read_top_alpa_granted,
3437 					       la),
3438 					bf_get(lpfc_mbx_read_top_link_spd, la),
3439 					phba->alpa_map[0],
3440 					bf_get(lpfc_mbx_read_top_mm, la),
3441 					bf_get(lpfc_mbx_read_top_fa, la),
3442 					phba->wait_4_mlo_maint_flg);
3443 		}
3444 		lpfc_mbx_process_link_up(phba, la);
3445 	} else if (attn_type == LPFC_ATT_LINK_DOWN ||
3446 		   attn_type == LPFC_ATT_UNEXP_WWPN) {
3447 		phba->fc_stat.LinkDown++;
3448 		if (phba->link_flag & LS_LOOPBACK_MODE)
3449 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3450 				"1308 Link Down Event in loop back mode "
3451 				"x%x received "
3452 				"Data: x%x x%x x%x\n",
3453 				la->eventTag, phba->fc_eventTag,
3454 				phba->pport->port_state, vport->fc_flag);
3455 		else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3456 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3457 				"1313 Link Down UNEXP WWPN Event x%x received "
3458 				"Data: x%x x%x x%x x%x x%x\n",
3459 				la->eventTag, phba->fc_eventTag,
3460 				phba->pport->port_state, vport->fc_flag,
3461 				bf_get(lpfc_mbx_read_top_mm, la),
3462 				bf_get(lpfc_mbx_read_top_fa, la));
3463 		else
3464 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3465 				"1305 Link Down Event x%x received "
3466 				"Data: x%x x%x x%x x%x x%x\n",
3467 				la->eventTag, phba->fc_eventTag,
3468 				phba->pport->port_state, vport->fc_flag,
3469 				bf_get(lpfc_mbx_read_top_mm, la),
3470 				bf_get(lpfc_mbx_read_top_fa, la));
3471 		lpfc_mbx_issue_link_down(phba);
3472 	}
3473 	if (phba->sli.sli_flag & LPFC_MENLO_MAINT &&
3474 	    attn_type == LPFC_ATT_LINK_UP) {
3475 		if (phba->link_state != LPFC_LINK_DOWN) {
3476 			phba->fc_stat.LinkDown++;
3477 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3478 				"1312 Link Down Event x%x received "
3479 				"Data: x%x x%x x%x\n",
3480 				la->eventTag, phba->fc_eventTag,
3481 				phba->pport->port_state, vport->fc_flag);
3482 			lpfc_mbx_issue_link_down(phba);
3483 		} else
3484 			lpfc_enable_la(phba);
3485 
3486 		lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3487 				"1310 Menlo Maint Mode Link up Event x%x rcvd "
3488 				"Data: x%x x%x x%x\n",
3489 				la->eventTag, phba->fc_eventTag,
3490 				phba->pport->port_state, vport->fc_flag);
3491 		/*
3492 		 * The cmnd that triggered this will be waiting for this
3493 		 * signal.
3494 		 */
3495 		/* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3496 		if (phba->wait_4_mlo_maint_flg) {
3497 			phba->wait_4_mlo_maint_flg = 0;
3498 			wake_up_interruptible(&phba->wait_4_mlo_m_q);
3499 		}
3500 	}
3501 
3502 	if ((phba->sli_rev < LPFC_SLI_REV4) &&
3503 	    bf_get(lpfc_mbx_read_top_fa, la)) {
3504 		if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
3505 			lpfc_issue_clear_la(phba, vport);
3506 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3507 				"1311 fa %d\n",
3508 				bf_get(lpfc_mbx_read_top_fa, la));
3509 	}
3510 
3511 lpfc_mbx_cmpl_read_topology_free_mbuf:
3512 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3513 	kfree(mp);
3514 	mempool_free(pmb, phba->mbox_mem_pool);
3515 	return;
3516 }
3517 
3518 /*
3519  * This routine handles processing a REG_LOGIN mailbox
3520  * command upon completion. It is setup in the LPFC_MBOXQ
3521  * as the completion routine when the command is
3522  * handed off to the SLI layer.
3523  */
3524 void
3525 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3526 {
3527 	struct lpfc_vport  *vport = pmb->vport;
3528 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3529 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3530 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3531 
3532 	pmb->ctx_buf = NULL;
3533 	pmb->ctx_ndlp = NULL;
3534 
3535 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3536 			 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3537 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3538 			 kref_read(&ndlp->kref),
3539 			 ndlp->nlp_usg_map, ndlp);
3540 	if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3541 		ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3542 
3543 	if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3544 	    ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
3545 		/* We rcvd a rscn after issuing this
3546 		 * mbox reg login, we may have cycled
3547 		 * back through the state and be
3548 		 * back at reg login state so this
3549 		 * mbox needs to be ignored becase
3550 		 * there is another reg login in
3551 		 * process.
3552 		 */
3553 		spin_lock_irq(shost->host_lock);
3554 		ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3555 		spin_unlock_irq(shost->host_lock);
3556 
3557 		/*
3558 		 * We cannot leave the RPI registered because
3559 		 * if we go thru discovery again for this ndlp
3560 		 * a subsequent REG_RPI will fail.
3561 		 */
3562 		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3563 		lpfc_unreg_rpi(vport, ndlp);
3564 	}
3565 
3566 	/* Call state machine */
3567 	lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
3568 
3569 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3570 	kfree(mp);
3571 	mempool_free(pmb, phba->mbox_mem_pool);
3572 	/* decrement the node reference count held for this callback
3573 	 * function.
3574 	 */
3575 	lpfc_nlp_put(ndlp);
3576 
3577 	return;
3578 }
3579 
3580 static void
3581 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3582 {
3583 	MAILBOX_t *mb = &pmb->u.mb;
3584 	struct lpfc_vport *vport = pmb->vport;
3585 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3586 
3587 	switch (mb->mbxStatus) {
3588 	case 0x0011:
3589 	case 0x0020:
3590 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3591 				 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3592 				 mb->mbxStatus);
3593 		break;
3594 	/* If VPI is busy, reset the HBA */
3595 	case 0x9700:
3596 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3597 			"2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3598 			vport->vpi, mb->mbxStatus);
3599 		if (!(phba->pport->load_flag & FC_UNLOADING))
3600 			lpfc_workq_post_event(phba, NULL, NULL,
3601 				LPFC_EVT_RESET_HBA);
3602 	}
3603 	spin_lock_irq(shost->host_lock);
3604 	vport->vpi_state &= ~LPFC_VPI_REGISTERED;
3605 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3606 	spin_unlock_irq(shost->host_lock);
3607 	vport->unreg_vpi_cmpl = VPORT_OK;
3608 	mempool_free(pmb, phba->mbox_mem_pool);
3609 	lpfc_cleanup_vports_rrqs(vport, NULL);
3610 	/*
3611 	 * This shost reference might have been taken at the beginning of
3612 	 * lpfc_vport_delete()
3613 	 */
3614 	if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
3615 		scsi_host_put(shost);
3616 }
3617 
3618 int
3619 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3620 {
3621 	struct lpfc_hba  *phba = vport->phba;
3622 	LPFC_MBOXQ_t *mbox;
3623 	int rc;
3624 
3625 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3626 	if (!mbox)
3627 		return 1;
3628 
3629 	lpfc_unreg_vpi(phba, vport->vpi, mbox);
3630 	mbox->vport = vport;
3631 	mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
3632 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3633 	if (rc == MBX_NOT_FINISHED) {
3634 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3635 				 "1800 Could not issue unreg_vpi\n");
3636 		mempool_free(mbox, phba->mbox_mem_pool);
3637 		vport->unreg_vpi_cmpl = VPORT_ERROR;
3638 		return rc;
3639 	}
3640 	return 0;
3641 }
3642 
3643 static void
3644 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3645 {
3646 	struct lpfc_vport *vport = pmb->vport;
3647 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3648 	MAILBOX_t *mb = &pmb->u.mb;
3649 
3650 	switch (mb->mbxStatus) {
3651 	case 0x0011:
3652 	case 0x9601:
3653 	case 0x9602:
3654 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3655 				 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3656 				 mb->mbxStatus);
3657 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3658 		spin_lock_irq(shost->host_lock);
3659 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3660 		spin_unlock_irq(shost->host_lock);
3661 		vport->fc_myDID = 0;
3662 
3663 		if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3664 		    (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3665 			if (phba->nvmet_support)
3666 				lpfc_nvmet_update_targetport(phba);
3667 			else
3668 				lpfc_nvme_update_localport(vport);
3669 		}
3670 		goto out;
3671 	}
3672 
3673 	spin_lock_irq(shost->host_lock);
3674 	vport->vpi_state |= LPFC_VPI_REGISTERED;
3675 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
3676 	spin_unlock_irq(shost->host_lock);
3677 	vport->num_disc_nodes = 0;
3678 	/* go thru NPR list and issue ELS PLOGIs */
3679 	if (vport->fc_npr_cnt)
3680 		lpfc_els_disc_plogi(vport);
3681 
3682 	if (!vport->num_disc_nodes) {
3683 		spin_lock_irq(shost->host_lock);
3684 		vport->fc_flag &= ~FC_NDISC_ACTIVE;
3685 		spin_unlock_irq(shost->host_lock);
3686 		lpfc_can_disctmo(vport);
3687 	}
3688 	vport->port_state = LPFC_VPORT_READY;
3689 
3690 out:
3691 	mempool_free(pmb, phba->mbox_mem_pool);
3692 	return;
3693 }
3694 
3695 /**
3696  * lpfc_create_static_vport - Read HBA config region to create static vports.
3697  * @phba: pointer to lpfc hba data structure.
3698  *
3699  * This routine issue a DUMP mailbox command for config region 22 to get
3700  * the list of static vports to be created. The function create vports
3701  * based on the information returned from the HBA.
3702  **/
3703 void
3704 lpfc_create_static_vport(struct lpfc_hba *phba)
3705 {
3706 	LPFC_MBOXQ_t *pmb = NULL;
3707 	MAILBOX_t *mb;
3708 	struct static_vport_info *vport_info;
3709 	int mbx_wait_rc = 0, i;
3710 	struct fc_vport_identifiers vport_id;
3711 	struct fc_vport *new_fc_vport;
3712 	struct Scsi_Host *shost;
3713 	struct lpfc_vport *vport;
3714 	uint16_t offset = 0;
3715 	uint8_t *vport_buff;
3716 	struct lpfc_dmabuf *mp;
3717 	uint32_t byte_count = 0;
3718 
3719 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3720 	if (!pmb) {
3721 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3722 				"0542 lpfc_create_static_vport failed to"
3723 				" allocate mailbox memory\n");
3724 		return;
3725 	}
3726 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
3727 	mb = &pmb->u.mb;
3728 
3729 	vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3730 	if (!vport_info) {
3731 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3732 				"0543 lpfc_create_static_vport failed to"
3733 				" allocate vport_info\n");
3734 		mempool_free(pmb, phba->mbox_mem_pool);
3735 		return;
3736 	}
3737 
3738 	vport_buff = (uint8_t *) vport_info;
3739 	do {
3740 		/* free dma buffer from previous round */
3741 		if (pmb->ctx_buf) {
3742 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3743 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
3744 			kfree(mp);
3745 		}
3746 		if (lpfc_dump_static_vport(phba, pmb, offset))
3747 			goto out;
3748 
3749 		pmb->vport = phba->pport;
3750 		mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3751 							LPFC_MBOX_TMO);
3752 
3753 		if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
3754 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3755 				"0544 lpfc_create_static_vport failed to"
3756 				" issue dump mailbox command ret 0x%x "
3757 				"status 0x%x\n",
3758 				mbx_wait_rc, mb->mbxStatus);
3759 			goto out;
3760 		}
3761 
3762 		if (phba->sli_rev == LPFC_SLI_REV4) {
3763 			byte_count = pmb->u.mqe.un.mb_words[5];
3764 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3765 			if (byte_count > sizeof(struct static_vport_info) -
3766 					offset)
3767 				byte_count = sizeof(struct static_vport_info)
3768 					- offset;
3769 			memcpy(vport_buff + offset, mp->virt, byte_count);
3770 			offset += byte_count;
3771 		} else {
3772 			if (mb->un.varDmp.word_cnt >
3773 				sizeof(struct static_vport_info) - offset)
3774 				mb->un.varDmp.word_cnt =
3775 					sizeof(struct static_vport_info)
3776 						- offset;
3777 			byte_count = mb->un.varDmp.word_cnt;
3778 			lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3779 				vport_buff + offset,
3780 				byte_count);
3781 
3782 			offset += byte_count;
3783 		}
3784 
3785 	} while (byte_count &&
3786 		offset < sizeof(struct static_vport_info));
3787 
3788 
3789 	if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3790 		((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3791 			!= VPORT_INFO_REV)) {
3792 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3793 			"0545 lpfc_create_static_vport bad"
3794 			" information header 0x%x 0x%x\n",
3795 			le32_to_cpu(vport_info->signature),
3796 			le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3797 
3798 		goto out;
3799 	}
3800 
3801 	shost = lpfc_shost_from_vport(phba->pport);
3802 
3803 	for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3804 		memset(&vport_id, 0, sizeof(vport_id));
3805 		vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3806 		vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3807 		if (!vport_id.port_name || !vport_id.node_name)
3808 			continue;
3809 
3810 		vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3811 		vport_id.vport_type = FC_PORTTYPE_NPIV;
3812 		vport_id.disable = false;
3813 		new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3814 
3815 		if (!new_fc_vport) {
3816 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3817 				"0546 lpfc_create_static_vport failed to"
3818 				" create vport\n");
3819 			continue;
3820 		}
3821 
3822 		vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3823 		vport->vport_flag |= STATIC_VPORT;
3824 	}
3825 
3826 out:
3827 	kfree(vport_info);
3828 	if (mbx_wait_rc != MBX_TIMEOUT) {
3829 		if (pmb->ctx_buf) {
3830 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3831 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
3832 			kfree(mp);
3833 		}
3834 		mempool_free(pmb, phba->mbox_mem_pool);
3835 	}
3836 
3837 	return;
3838 }
3839 
3840 /*
3841  * This routine handles processing a Fabric REG_LOGIN mailbox
3842  * command upon completion. It is setup in the LPFC_MBOXQ
3843  * as the completion routine when the command is
3844  * handed off to the SLI layer.
3845  */
3846 void
3847 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3848 {
3849 	struct lpfc_vport *vport = pmb->vport;
3850 	MAILBOX_t *mb = &pmb->u.mb;
3851 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3852 	struct lpfc_nodelist *ndlp;
3853 	struct Scsi_Host *shost;
3854 
3855 	ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3856 	pmb->ctx_ndlp = NULL;
3857 	pmb->ctx_buf = NULL;
3858 
3859 	if (mb->mbxStatus) {
3860 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3861 				 "0258 Register Fabric login error: 0x%x\n",
3862 				 mb->mbxStatus);
3863 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
3864 		kfree(mp);
3865 		mempool_free(pmb, phba->mbox_mem_pool);
3866 
3867 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3868 			/* FLOGI failed, use loop map to make discovery list */
3869 			lpfc_disc_list_loopmap(vport);
3870 
3871 			/* Start discovery */
3872 			lpfc_disc_start(vport);
3873 			/* Decrement the reference count to ndlp after the
3874 			 * reference to the ndlp are done.
3875 			 */
3876 			lpfc_nlp_put(ndlp);
3877 			return;
3878 		}
3879 
3880 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3881 		/* Decrement the reference count to ndlp after the reference
3882 		 * to the ndlp are done.
3883 		 */
3884 		lpfc_nlp_put(ndlp);
3885 		return;
3886 	}
3887 
3888 	if (phba->sli_rev < LPFC_SLI_REV4)
3889 		ndlp->nlp_rpi = mb->un.varWords[0];
3890 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3891 	ndlp->nlp_type |= NLP_FABRIC;
3892 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
3893 
3894 	if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
3895 		/* when physical port receive logo donot start
3896 		 * vport discovery */
3897 		if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3898 			lpfc_start_fdiscs(phba);
3899 		else {
3900 			shost = lpfc_shost_from_vport(vport);
3901 			spin_lock_irq(shost->host_lock);
3902 			vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
3903 			spin_unlock_irq(shost->host_lock);
3904 		}
3905 		lpfc_do_scr_ns_plogi(phba, vport);
3906 	}
3907 
3908 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
3909 	kfree(mp);
3910 	mempool_free(pmb, phba->mbox_mem_pool);
3911 
3912 	/* Drop the reference count from the mbox at the end after
3913 	 * all the current reference to the ndlp have been done.
3914 	 */
3915 	lpfc_nlp_put(ndlp);
3916 	return;
3917 }
3918 
3919  /*
3920   * This routine will issue a GID_FT for each FC4 Type supported
3921   * by the driver. ALL GID_FTs must complete before discovery is started.
3922   */
3923 int
3924 lpfc_issue_gidft(struct lpfc_vport *vport)
3925 {
3926 	struct lpfc_hba *phba = vport->phba;
3927 
3928 	/* Good status, issue CT Request to NameServer */
3929 	if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3930 	    (phba->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 ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3946 	    (phba->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 %p\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 ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4063 		    (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
4064 			lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
4065 
4066 		if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4067 		    (phba->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 (phba->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_RPORT_ROLE_FCP_TARGET;
4156 	if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4157 		rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4158 
4159 	if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
4160 		fc_remote_port_rolechg(rport, rport_ids.roles);
4161 
4162 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4163 			 "3183 rport register x%06x, rport %p role x%x\n",
4164 			 ndlp->nlp_DID, rport, rport_ids.roles);
4165 
4166 	if ((rport->scsi_target_id != -1) &&
4167 	    (rport->scsi_target_id < LPFC_MAX_TARGET)) {
4168 		ndlp->nlp_sid = rport->scsi_target_id;
4169 	}
4170 	return;
4171 }
4172 
4173 static void
4174 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
4175 {
4176 	struct fc_rport *rport = ndlp->rport;
4177 	struct lpfc_vport *vport = ndlp->vport;
4178 	struct lpfc_hba  *phba = vport->phba;
4179 
4180 	if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4181 		return;
4182 
4183 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4184 		"rport delete:    did:x%x flg:x%x type x%x",
4185 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4186 
4187 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4188 			 "3184 rport unregister x%06x, rport %p\n",
4189 			 ndlp->nlp_DID, rport);
4190 
4191 	fc_remote_port_delete(rport);
4192 
4193 	return;
4194 }
4195 
4196 static void
4197 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
4198 {
4199 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4200 
4201 	spin_lock_irq(shost->host_lock);
4202 	switch (state) {
4203 	case NLP_STE_UNUSED_NODE:
4204 		vport->fc_unused_cnt += count;
4205 		break;
4206 	case NLP_STE_PLOGI_ISSUE:
4207 		vport->fc_plogi_cnt += count;
4208 		break;
4209 	case NLP_STE_ADISC_ISSUE:
4210 		vport->fc_adisc_cnt += count;
4211 		break;
4212 	case NLP_STE_REG_LOGIN_ISSUE:
4213 		vport->fc_reglogin_cnt += count;
4214 		break;
4215 	case NLP_STE_PRLI_ISSUE:
4216 		vport->fc_prli_cnt += count;
4217 		break;
4218 	case NLP_STE_UNMAPPED_NODE:
4219 		vport->fc_unmap_cnt += count;
4220 		break;
4221 	case NLP_STE_MAPPED_NODE:
4222 		vport->fc_map_cnt += count;
4223 		break;
4224 	case NLP_STE_NPR_NODE:
4225 		if (vport->fc_npr_cnt == 0 && count == -1)
4226 			vport->fc_npr_cnt = 0;
4227 		else
4228 			vport->fc_npr_cnt += count;
4229 		break;
4230 	}
4231 	spin_unlock_irq(shost->host_lock);
4232 }
4233 
4234 static void
4235 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4236 		       int old_state, int new_state)
4237 {
4238 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4239 
4240 	if (new_state == NLP_STE_UNMAPPED_NODE) {
4241 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4242 		ndlp->nlp_type |= NLP_FC_NODE;
4243 	}
4244 	if (new_state == NLP_STE_MAPPED_NODE)
4245 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4246 	if (new_state == NLP_STE_NPR_NODE)
4247 		ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4248 
4249 	/* FCP and NVME Transport interface */
4250 	if ((old_state == NLP_STE_MAPPED_NODE ||
4251 	     old_state == NLP_STE_UNMAPPED_NODE)) {
4252 		if (ndlp->rport) {
4253 			vport->phba->nport_event_cnt++;
4254 			lpfc_unregister_remote_port(ndlp);
4255 		}
4256 
4257 		if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4258 			vport->phba->nport_event_cnt++;
4259 			if (vport->phba->nvmet_support == 0) {
4260 				/* Start devloss if target. */
4261 				if (ndlp->nlp_type & NLP_NVME_TARGET)
4262 					lpfc_nvme_unregister_port(vport, ndlp);
4263 			} else {
4264 				/* NVMET has no upcall. */
4265 				lpfc_nlp_put(ndlp);
4266 			}
4267 		}
4268 	}
4269 
4270 	/* FCP and NVME Transport interfaces */
4271 
4272 	if (new_state ==  NLP_STE_MAPPED_NODE ||
4273 	    new_state == NLP_STE_UNMAPPED_NODE) {
4274 		if (ndlp->nlp_fc4_type ||
4275 		    ndlp->nlp_DID == Fabric_DID ||
4276 		    ndlp->nlp_DID == NameServer_DID ||
4277 		    ndlp->nlp_DID == FDMI_DID) {
4278 			vport->phba->nport_event_cnt++;
4279 			/*
4280 			 * Tell the fc transport about the port, if we haven't
4281 			 * already. If we have, and it's a scsi entity, be
4282 			 */
4283 			lpfc_register_remote_port(vport, ndlp);
4284 		}
4285 		/* Notify the NVME transport of this new rport. */
4286 		if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4287 		    ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4288 			if (vport->phba->nvmet_support == 0) {
4289 				/* Register this rport with the transport.
4290 				 * Only NVME Target Rports are registered with
4291 				 * the transport.
4292 				 */
4293 				if (ndlp->nlp_type & NLP_NVME_TARGET) {
4294 					vport->phba->nport_event_cnt++;
4295 					lpfc_nvme_register_port(vport, ndlp);
4296 				}
4297 			} else {
4298 				/* Just take an NDLP ref count since the
4299 				 * target does not register rports.
4300 				 */
4301 				lpfc_nlp_get(ndlp);
4302 			}
4303 		}
4304 	}
4305 
4306 	if ((new_state ==  NLP_STE_MAPPED_NODE) &&
4307 		(vport->stat_data_enabled)) {
4308 		/*
4309 		 * A new target is discovered, if there is no buffer for
4310 		 * statistical data collection allocate buffer.
4311 		 */
4312 		ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4313 					 sizeof(struct lpfc_scsicmd_bkt),
4314 					 GFP_KERNEL);
4315 
4316 		if (!ndlp->lat_data)
4317 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4318 				"0286 lpfc_nlp_state_cleanup failed to "
4319 				"allocate statistical data buffer DID "
4320 				"0x%x\n", ndlp->nlp_DID);
4321 	}
4322 	/*
4323 	 * If the node just added to Mapped list was an FCP target,
4324 	 * but the remote port registration failed or assigned a target
4325 	 * id outside the presentable range - move the node to the
4326 	 * Unmapped List.
4327 	 */
4328 	if ((new_state == NLP_STE_MAPPED_NODE) &&
4329 	    (ndlp->nlp_type & NLP_FCP_TARGET) &&
4330 	    (!ndlp->rport ||
4331 	     ndlp->rport->scsi_target_id == -1 ||
4332 	     ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
4333 		spin_lock_irq(shost->host_lock);
4334 		ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
4335 		spin_unlock_irq(shost->host_lock);
4336 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4337 	}
4338 }
4339 
4340 static char *
4341 lpfc_nlp_state_name(char *buffer, size_t size, int state)
4342 {
4343 	static char *states[] = {
4344 		[NLP_STE_UNUSED_NODE] = "UNUSED",
4345 		[NLP_STE_PLOGI_ISSUE] = "PLOGI",
4346 		[NLP_STE_ADISC_ISSUE] = "ADISC",
4347 		[NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4348 		[NLP_STE_PRLI_ISSUE] = "PRLI",
4349 		[NLP_STE_LOGO_ISSUE] = "LOGO",
4350 		[NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4351 		[NLP_STE_MAPPED_NODE] = "MAPPED",
4352 		[NLP_STE_NPR_NODE] = "NPR",
4353 	};
4354 
4355 	if (state < NLP_STE_MAX_STATE && states[state])
4356 		strlcpy(buffer, states[state], size);
4357 	else
4358 		snprintf(buffer, size, "unknown (%d)", state);
4359 	return buffer;
4360 }
4361 
4362 void
4363 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4364 		   int state)
4365 {
4366 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4367 	int  old_state = ndlp->nlp_state;
4368 	char name1[16], name2[16];
4369 
4370 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4371 			 "0904 NPort state transition x%06x, %s -> %s\n",
4372 			 ndlp->nlp_DID,
4373 			 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4374 			 lpfc_nlp_state_name(name2, sizeof(name2), state));
4375 
4376 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4377 		"node statechg    did:x%x old:%d ste:%d",
4378 		ndlp->nlp_DID, old_state, state);
4379 
4380 	if (old_state == NLP_STE_NPR_NODE &&
4381 	    state != NLP_STE_NPR_NODE)
4382 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
4383 	if (old_state == NLP_STE_UNMAPPED_NODE) {
4384 		ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4385 		ndlp->nlp_type &= ~NLP_FC_NODE;
4386 	}
4387 
4388 	if (list_empty(&ndlp->nlp_listp)) {
4389 		spin_lock_irq(shost->host_lock);
4390 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4391 		spin_unlock_irq(shost->host_lock);
4392 	} else if (old_state)
4393 		lpfc_nlp_counters(vport, old_state, -1);
4394 
4395 	ndlp->nlp_state = state;
4396 	lpfc_nlp_counters(vport, state, 1);
4397 	lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
4398 }
4399 
4400 void
4401 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4402 {
4403 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4404 
4405 	if (list_empty(&ndlp->nlp_listp)) {
4406 		spin_lock_irq(shost->host_lock);
4407 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4408 		spin_unlock_irq(shost->host_lock);
4409 	}
4410 }
4411 
4412 void
4413 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4414 {
4415 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4416 
4417 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
4418 	if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4419 		lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4420 	spin_lock_irq(shost->host_lock);
4421 	list_del_init(&ndlp->nlp_listp);
4422 	spin_unlock_irq(shost->host_lock);
4423 	lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4424 				NLP_STE_UNUSED_NODE);
4425 }
4426 
4427 static void
4428 lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4429 {
4430 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
4431 	if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4432 		lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4433 	lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4434 				NLP_STE_UNUSED_NODE);
4435 }
4436 /**
4437  * lpfc_initialize_node - Initialize all fields of node object
4438  * @vport: Pointer to Virtual Port object.
4439  * @ndlp: Pointer to FC node object.
4440  * @did: FC_ID of the node.
4441  *
4442  * This function is always called when node object need to be initialized.
4443  * It initializes all the fields of the node object. Although the reference
4444  * to phba from @ndlp can be obtained indirectly through it's reference to
4445  * @vport, a direct reference to phba is taken here by @ndlp. This is due
4446  * to the life-span of the @ndlp might go beyond the existence of @vport as
4447  * the final release of ndlp is determined by its reference count. And, the
4448  * operation on @ndlp needs the reference to phba.
4449  **/
4450 static inline void
4451 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4452 	uint32_t did)
4453 {
4454 	INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4455 	INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4456 	timer_setup(&ndlp->nlp_delayfunc, lpfc_els_retry_delay, 0);
4457 	ndlp->nlp_DID = did;
4458 	ndlp->vport = vport;
4459 	ndlp->phba = vport->phba;
4460 	ndlp->nlp_sid = NLP_NO_SID;
4461 	ndlp->nlp_fc4_type = NLP_FC4_NONE;
4462 	kref_init(&ndlp->kref);
4463 	NLP_INT_NODE_ACT(ndlp);
4464 	atomic_set(&ndlp->cmd_pending, 0);
4465 	ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4466 	ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4467 }
4468 
4469 struct lpfc_nodelist *
4470 lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4471 		 int state)
4472 {
4473 	struct lpfc_hba *phba = vport->phba;
4474 	uint32_t did, flag;
4475 	unsigned long flags;
4476 	unsigned long *active_rrqs_xri_bitmap = NULL;
4477 	int rpi = LPFC_RPI_ALLOC_ERROR;
4478 	uint32_t defer_did = 0;
4479 
4480 	if (!ndlp)
4481 		return NULL;
4482 
4483 	if (phba->sli_rev == LPFC_SLI_REV4) {
4484 		rpi = lpfc_sli4_alloc_rpi(vport->phba);
4485 		if (rpi == LPFC_RPI_ALLOC_ERROR)
4486 			return NULL;
4487 	}
4488 
4489 	spin_lock_irqsave(&phba->ndlp_lock, flags);
4490 	/* The ndlp should not be in memory free mode */
4491 	if (NLP_CHK_FREE_REQ(ndlp)) {
4492 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4493 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4494 				"0277 lpfc_enable_node: ndlp:x%p "
4495 				"usgmap:x%x refcnt:%d\n",
4496 				(void *)ndlp, ndlp->nlp_usg_map,
4497 				kref_read(&ndlp->kref));
4498 		goto free_rpi;
4499 	}
4500 	/* The ndlp should not already be in active mode */
4501 	if (NLP_CHK_NODE_ACT(ndlp)) {
4502 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4503 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4504 				"0278 lpfc_enable_node: ndlp:x%p "
4505 				"usgmap:x%x refcnt:%d\n",
4506 				(void *)ndlp, ndlp->nlp_usg_map,
4507 				kref_read(&ndlp->kref));
4508 		goto free_rpi;
4509 	}
4510 
4511 	/* First preserve the orginal DID, xri_bitmap and some flags */
4512 	did = ndlp->nlp_DID;
4513 	flag = (ndlp->nlp_flag & NLP_UNREG_INP);
4514 	if (flag & NLP_UNREG_INP)
4515 		defer_did = ndlp->nlp_defer_did;
4516 	if (phba->sli_rev == LPFC_SLI_REV4)
4517 		active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
4518 
4519 	/* Zero ndlp except of ndlp linked list pointer */
4520 	memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4521 		sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
4522 
4523 	/* Next reinitialize and restore saved objects */
4524 	lpfc_initialize_node(vport, ndlp, did);
4525 	ndlp->nlp_flag |= flag;
4526 	if (flag & NLP_UNREG_INP)
4527 		ndlp->nlp_defer_did = defer_did;
4528 	if (phba->sli_rev == LPFC_SLI_REV4)
4529 		ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4530 
4531 	spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4532 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4533 		ndlp->nlp_rpi = rpi;
4534 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4535 				 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4536 				 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4537 				 ndlp->nlp_flag,
4538 				 kref_read(&ndlp->kref),
4539 				 ndlp->nlp_usg_map, ndlp);
4540 	}
4541 
4542 
4543 	if (state != NLP_STE_UNUSED_NODE)
4544 		lpfc_nlp_set_state(vport, ndlp, state);
4545 
4546 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4547 		"node enable:       did:x%x",
4548 		ndlp->nlp_DID, 0, 0);
4549 	return ndlp;
4550 
4551 free_rpi:
4552 	if (phba->sli_rev == LPFC_SLI_REV4)
4553 		lpfc_sli4_free_rpi(vport->phba, rpi);
4554 	return NULL;
4555 }
4556 
4557 void
4558 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4559 {
4560 	/*
4561 	 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
4562 	 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
4563 	 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4564 	 * until ALL other outstanding threads have completed. We check
4565 	 * that the ndlp not already in the UNUSED state before we proceed.
4566 	 */
4567 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4568 		return;
4569 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
4570 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4571 		lpfc_cleanup_vports_rrqs(vport, ndlp);
4572 		lpfc_unreg_rpi(vport, ndlp);
4573 	}
4574 
4575 	lpfc_nlp_put(ndlp);
4576 	return;
4577 }
4578 
4579 /*
4580  * Start / ReStart rescue timer for Discovery / RSCN handling
4581  */
4582 void
4583 lpfc_set_disctmo(struct lpfc_vport *vport)
4584 {
4585 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4586 	struct lpfc_hba  *phba = vport->phba;
4587 	uint32_t tmo;
4588 
4589 	if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
4590 		/* For FAN, timeout should be greater than edtov */
4591 		tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4592 	} else {
4593 		/* Normal discovery timeout should be > than ELS/CT timeout
4594 		 * FC spec states we need 3 * ratov for CT requests
4595 		 */
4596 		tmo = ((phba->fc_ratov * 3) + 3);
4597 	}
4598 
4599 
4600 	if (!timer_pending(&vport->fc_disctmo)) {
4601 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4602 			"set disc timer:  tmo:x%x state:x%x flg:x%x",
4603 			tmo, vport->port_state, vport->fc_flag);
4604 	}
4605 
4606 	mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
4607 	spin_lock_irq(shost->host_lock);
4608 	vport->fc_flag |= FC_DISC_TMO;
4609 	spin_unlock_irq(shost->host_lock);
4610 
4611 	/* Start Discovery Timer state <hba_state> */
4612 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4613 			 "0247 Start Discovery Timer state x%x "
4614 			 "Data: x%x x%lx x%x x%x\n",
4615 			 vport->port_state, tmo,
4616 			 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4617 			 vport->fc_adisc_cnt);
4618 
4619 	return;
4620 }
4621 
4622 /*
4623  * Cancel rescue timer for Discovery / RSCN handling
4624  */
4625 int
4626 lpfc_can_disctmo(struct lpfc_vport *vport)
4627 {
4628 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4629 	unsigned long iflags;
4630 
4631 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4632 		"can disc timer:  state:x%x rtry:x%x flg:x%x",
4633 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4634 
4635 	/* Turn off discovery timer if its running */
4636 	if (vport->fc_flag & FC_DISC_TMO) {
4637 		spin_lock_irqsave(shost->host_lock, iflags);
4638 		vport->fc_flag &= ~FC_DISC_TMO;
4639 		spin_unlock_irqrestore(shost->host_lock, iflags);
4640 		del_timer_sync(&vport->fc_disctmo);
4641 		spin_lock_irqsave(&vport->work_port_lock, iflags);
4642 		vport->work_port_events &= ~WORKER_DISC_TMO;
4643 		spin_unlock_irqrestore(&vport->work_port_lock, iflags);
4644 	}
4645 
4646 	/* Cancel Discovery Timer state <hba_state> */
4647 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4648 			 "0248 Cancel Discovery Timer state x%x "
4649 			 "Data: x%x x%x x%x\n",
4650 			 vport->port_state, vport->fc_flag,
4651 			 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
4652 	return 0;
4653 }
4654 
4655 /*
4656  * Check specified ring for outstanding IOCB on the SLI queue
4657  * Return true if iocb matches the specified nport
4658  */
4659 int
4660 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4661 		    struct lpfc_sli_ring *pring,
4662 		    struct lpfc_iocbq *iocb,
4663 		    struct lpfc_nodelist *ndlp)
4664 {
4665 	IOCB_t *icmd = &iocb->iocb;
4666 	struct lpfc_vport    *vport = ndlp->vport;
4667 
4668 	if (iocb->vport != vport)
4669 		return 0;
4670 
4671 	if (pring->ringno == LPFC_ELS_RING) {
4672 		switch (icmd->ulpCommand) {
4673 		case CMD_GEN_REQUEST64_CR:
4674 			if (iocb->context_un.ndlp == ndlp)
4675 				return 1;
4676 		case CMD_ELS_REQUEST64_CR:
4677 			if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4678 				return 1;
4679 		case CMD_XMIT_ELS_RSP64_CX:
4680 			if (iocb->context1 == (uint8_t *) ndlp)
4681 				return 1;
4682 		}
4683 	} else if (pring->ringno == LPFC_FCP_RING) {
4684 		/* Skip match check if waiting to relogin to FCP target */
4685 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4686 		    (ndlp->nlp_flag & NLP_DELAY_TMO)) {
4687 			return 0;
4688 		}
4689 		if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
4690 			return 1;
4691 		}
4692 	}
4693 	return 0;
4694 }
4695 
4696 static void
4697 __lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4698 		struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4699 		struct list_head *dequeue_list)
4700 {
4701 	struct lpfc_iocbq *iocb, *next_iocb;
4702 
4703 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4704 		/* Check to see if iocb matches the nport */
4705 		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4706 			/* match, dequeue */
4707 			list_move_tail(&iocb->list, dequeue_list);
4708 	}
4709 }
4710 
4711 static void
4712 lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4713 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4714 {
4715 	struct lpfc_sli *psli = &phba->sli;
4716 	uint32_t i;
4717 
4718 	spin_lock_irq(&phba->hbalock);
4719 	for (i = 0; i < psli->num_rings; i++)
4720 		__lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4721 						dequeue_list);
4722 	spin_unlock_irq(&phba->hbalock);
4723 }
4724 
4725 static void
4726 lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4727 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4728 {
4729 	struct lpfc_sli_ring *pring;
4730 	struct lpfc_queue *qp = NULL;
4731 
4732 	spin_lock_irq(&phba->hbalock);
4733 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4734 		pring = qp->pring;
4735 		if (!pring)
4736 			continue;
4737 		spin_lock(&pring->ring_lock);
4738 		__lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
4739 		spin_unlock(&pring->ring_lock);
4740 	}
4741 	spin_unlock_irq(&phba->hbalock);
4742 }
4743 
4744 /*
4745  * Free resources / clean up outstanding I/Os
4746  * associated with nlp_rpi in the LPFC_NODELIST entry.
4747  */
4748 static int
4749 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
4750 {
4751 	LIST_HEAD(completions);
4752 
4753 	lpfc_fabric_abort_nport(ndlp);
4754 
4755 	/*
4756 	 * Everything that matches on txcmplq will be returned
4757 	 * by firmware with a no rpi error.
4758 	 */
4759 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
4760 		if (phba->sli_rev != LPFC_SLI_REV4)
4761 			lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4762 		else
4763 			lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
4764 	}
4765 
4766 	/* Cancel all the IOCBs from the completions list */
4767 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4768 			      IOERR_SLI_ABORTED);
4769 
4770 	return 0;
4771 }
4772 
4773 /**
4774  * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4775  * @phba: Pointer to HBA context object.
4776  * @pmb: Pointer to mailbox object.
4777  *
4778  * This function will issue an ELS LOGO command after completing
4779  * the UNREG_RPI.
4780  **/
4781 static void
4782 lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4783 {
4784 	struct lpfc_vport  *vport = pmb->vport;
4785 	struct lpfc_nodelist *ndlp;
4786 
4787 	ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
4788 	if (!ndlp)
4789 		return;
4790 	lpfc_issue_els_logo(vport, ndlp, 0);
4791 	mempool_free(pmb, phba->mbox_mem_pool);
4792 
4793 	/* Check to see if there are any deferred events to process */
4794 	if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
4795 	    (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
4796 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4797 				 "1434 UNREG cmpl deferred logo x%x "
4798 				 "on NPort x%x Data: x%x %p\n",
4799 				 ndlp->nlp_rpi, ndlp->nlp_DID,
4800 				 ndlp->nlp_defer_did, ndlp);
4801 
4802 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
4803 		ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4804 		lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4805 	} else {
4806 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
4807 	}
4808 }
4809 
4810 /*
4811  * Free rpi associated with LPFC_NODELIST entry.
4812  * This routine is called from lpfc_freenode(), when we are removing
4813  * a LPFC_NODELIST entry. It is also called if the driver initiates a
4814  * LOGO that completes successfully, and we are waiting to PLOGI back
4815  * to the remote NPort. In addition, it is called after we receive
4816  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4817  * we are waiting to PLOGI back to the remote NPort.
4818  */
4819 int
4820 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4821 {
4822 	struct lpfc_hba *phba = vport->phba;
4823 	LPFC_MBOXQ_t    *mbox;
4824 	int rc, acc_plogi = 1;
4825 	uint16_t rpi;
4826 
4827 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4828 	    ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4829 		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4830 			lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4831 					 "3366 RPI x%x needs to be "
4832 					 "unregistered nlp_flag x%x "
4833 					 "did x%x\n",
4834 					 ndlp->nlp_rpi, ndlp->nlp_flag,
4835 					 ndlp->nlp_DID);
4836 
4837 		/* If there is already an UNREG in progress for this ndlp,
4838 		 * no need to queue up another one.
4839 		 */
4840 		if (ndlp->nlp_flag & NLP_UNREG_INP) {
4841 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4842 					 "1436 unreg_rpi SKIP UNREG x%x on "
4843 					 "NPort x%x deferred x%x  flg x%x "
4844 					 "Data: %p\n",
4845 					 ndlp->nlp_rpi, ndlp->nlp_DID,
4846 					 ndlp->nlp_defer_did,
4847 					 ndlp->nlp_flag, ndlp);
4848 			goto out;
4849 		}
4850 
4851 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4852 		if (mbox) {
4853 			/* SLI4 ports require the physical rpi value. */
4854 			rpi = ndlp->nlp_rpi;
4855 			if (phba->sli_rev == LPFC_SLI_REV4)
4856 				rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4857 
4858 			lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
4859 			mbox->vport = vport;
4860 			if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4861 				mbox->ctx_ndlp = ndlp;
4862 				mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4863 			} else {
4864 				if (phba->sli_rev == LPFC_SLI_REV4 &&
4865 				    (!(vport->load_flag & FC_UNLOADING)) &&
4866 				    (bf_get(lpfc_sli_intf_if_type,
4867 				     &phba->sli4_hba.sli_intf) >=
4868 				      LPFC_SLI_INTF_IF_TYPE_2) &&
4869 				    (kref_read(&ndlp->kref) > 0)) {
4870 					mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
4871 					mbox->mbox_cmpl =
4872 						lpfc_sli4_unreg_rpi_cmpl_clr;
4873 					/*
4874 					 * accept PLOGIs after unreg_rpi_cmpl
4875 					 */
4876 					acc_plogi = 0;
4877 				} else {
4878 					mbox->ctx_ndlp = ndlp;
4879 					mbox->mbox_cmpl =
4880 						lpfc_sli_def_mbox_cmpl;
4881 				}
4882 			}
4883 			if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
4884 			    Fabric_DID_MASK) &&
4885 			    (!(vport->fc_flag & FC_OFFLINE_MODE)))
4886 				ndlp->nlp_flag |= NLP_UNREG_INP;
4887 
4888 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4889 					 "1433 unreg_rpi UNREG x%x on "
4890 					 "NPort x%x deferred flg x%x Data:%p\n",
4891 					 ndlp->nlp_rpi, ndlp->nlp_DID,
4892 					 ndlp->nlp_flag, ndlp);
4893 
4894 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4895 			if (rc == MBX_NOT_FINISHED) {
4896 				mempool_free(mbox, phba->mbox_mem_pool);
4897 				acc_plogi = 1;
4898 			}
4899 		}
4900 		lpfc_no_rpi(phba, ndlp);
4901 out:
4902 		if (phba->sli_rev != LPFC_SLI_REV4)
4903 			ndlp->nlp_rpi = 0;
4904 		ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
4905 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
4906 		if (acc_plogi)
4907 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4908 		return 1;
4909 	}
4910 	ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4911 	return 0;
4912 }
4913 
4914 /**
4915  * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4916  * @phba: pointer to lpfc hba data structure.
4917  *
4918  * This routine is invoked to unregister all the currently registered RPIs
4919  * to the HBA.
4920  **/
4921 void
4922 lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4923 {
4924 	struct lpfc_vport **vports;
4925 	struct lpfc_nodelist *ndlp;
4926 	struct Scsi_Host *shost;
4927 	int i;
4928 
4929 	vports = lpfc_create_vport_work_array(phba);
4930 	if (!vports) {
4931 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4932 			"2884 Vport array allocation failed \n");
4933 		return;
4934 	}
4935 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4936 		shost = lpfc_shost_from_vport(vports[i]);
4937 		spin_lock_irq(shost->host_lock);
4938 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4939 			if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
4940 				/* The mempool_alloc might sleep */
4941 				spin_unlock_irq(shost->host_lock);
4942 				lpfc_unreg_rpi(vports[i], ndlp);
4943 				spin_lock_irq(shost->host_lock);
4944 			}
4945 		}
4946 		spin_unlock_irq(shost->host_lock);
4947 	}
4948 	lpfc_destroy_vport_work_array(phba, vports);
4949 }
4950 
4951 void
4952 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4953 {
4954 	struct lpfc_hba  *phba  = vport->phba;
4955 	LPFC_MBOXQ_t     *mbox;
4956 	int rc;
4957 
4958 	if (phba->sli_rev == LPFC_SLI_REV4) {
4959 		lpfc_sli4_unreg_all_rpis(vport);
4960 		return;
4961 	}
4962 
4963 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4964 	if (mbox) {
4965 		lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4966 				 mbox);
4967 		mbox->vport = vport;
4968 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4969 		mbox->ctx_ndlp = NULL;
4970 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
4971 		if (rc != MBX_TIMEOUT)
4972 			mempool_free(mbox, phba->mbox_mem_pool);
4973 
4974 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4975 			lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4976 				"1836 Could not issue "
4977 				"unreg_login(all_rpis) status %d\n", rc);
4978 	}
4979 }
4980 
4981 void
4982 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4983 {
4984 	struct lpfc_hba  *phba  = vport->phba;
4985 	LPFC_MBOXQ_t     *mbox;
4986 	int rc;
4987 
4988 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4989 	if (mbox) {
4990 		lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4991 			       mbox);
4992 		mbox->vport = vport;
4993 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4994 		mbox->ctx_ndlp = NULL;
4995 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
4996 		if (rc != MBX_TIMEOUT)
4997 			mempool_free(mbox, phba->mbox_mem_pool);
4998 
4999 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5000 			lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
5001 					 "1815 Could not issue "
5002 					 "unreg_did (default rpis) status %d\n",
5003 					 rc);
5004 	}
5005 }
5006 
5007 /*
5008  * Free resources associated with LPFC_NODELIST entry
5009  * so it can be freed.
5010  */
5011 static int
5012 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5013 {
5014 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5015 	struct lpfc_hba  *phba = vport->phba;
5016 	LPFC_MBOXQ_t *mb, *nextmb;
5017 	struct lpfc_dmabuf *mp;
5018 
5019 	/* Cleanup node for NPort <nlp_DID> */
5020 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5021 			 "0900 Cleanup node for NPort x%x "
5022 			 "Data: x%x x%x x%x\n",
5023 			 ndlp->nlp_DID, ndlp->nlp_flag,
5024 			 ndlp->nlp_state, ndlp->nlp_rpi);
5025 	if (NLP_CHK_FREE_REQ(ndlp)) {
5026 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5027 				"0280 lpfc_cleanup_node: ndlp:x%p "
5028 				"usgmap:x%x refcnt:%d\n",
5029 				(void *)ndlp, ndlp->nlp_usg_map,
5030 				kref_read(&ndlp->kref));
5031 		lpfc_dequeue_node(vport, ndlp);
5032 	} else {
5033 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5034 				"0281 lpfc_cleanup_node: ndlp:x%p "
5035 				"usgmap:x%x refcnt:%d\n",
5036 				(void *)ndlp, ndlp->nlp_usg_map,
5037 				kref_read(&ndlp->kref));
5038 		lpfc_disable_node(vport, ndlp);
5039 	}
5040 
5041 
5042 	/* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5043 
5044 	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5045 	if ((mb = phba->sli.mbox_active)) {
5046 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5047 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5048 		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5049 			mb->ctx_ndlp = NULL;
5050 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5051 		}
5052 	}
5053 
5054 	spin_lock_irq(&phba->hbalock);
5055 	/* Cleanup REG_LOGIN completions which are not yet processed */
5056 	list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5057 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
5058 			(mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
5059 			(ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
5060 			continue;
5061 
5062 		mb->ctx_ndlp = NULL;
5063 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5064 	}
5065 
5066 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
5067 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5068 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5069 		    (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5070 			mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
5071 			if (mp) {
5072 				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
5073 				kfree(mp);
5074 			}
5075 			list_del(&mb->list);
5076 			mempool_free(mb, phba->mbox_mem_pool);
5077 			/* We shall not invoke the lpfc_nlp_put to decrement
5078 			 * the ndlp reference count as we are in the process
5079 			 * of lpfc_nlp_release.
5080 			 */
5081 		}
5082 	}
5083 	spin_unlock_irq(&phba->hbalock);
5084 
5085 	lpfc_els_abort(phba, ndlp);
5086 
5087 	spin_lock_irq(shost->host_lock);
5088 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
5089 	spin_unlock_irq(shost->host_lock);
5090 
5091 	ndlp->nlp_last_elscmd = 0;
5092 	del_timer_sync(&ndlp->nlp_delayfunc);
5093 
5094 	list_del_init(&ndlp->els_retry_evt.evt_listp);
5095 	list_del_init(&ndlp->dev_loss_evt.evt_listp);
5096 	lpfc_cleanup_vports_rrqs(vport, ndlp);
5097 	lpfc_unreg_rpi(vport, ndlp);
5098 
5099 	return 0;
5100 }
5101 
5102 /*
5103  * Check to see if we can free the nlp back to the freelist.
5104  * If we are in the middle of using the nlp in the discovery state
5105  * machine, defer the free till we reach the end of the state machine.
5106  */
5107 static void
5108 lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5109 {
5110 	struct lpfc_hba  *phba = vport->phba;
5111 	struct lpfc_rport_data *rdata;
5112 	struct fc_rport *rport;
5113 	LPFC_MBOXQ_t *mbox;
5114 	int rc;
5115 
5116 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
5117 	if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
5118 	    !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
5119 	    !(ndlp->nlp_flag & NLP_RPI_REGISTERED) &&
5120 	    phba->sli_rev != LPFC_SLI_REV4) {
5121 		/* For this case we need to cleanup the default rpi
5122 		 * allocated by the firmware.
5123 		 */
5124 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5125 				 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5126 				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5127 				 kref_read(&ndlp->kref),
5128 				 ndlp->nlp_usg_map, ndlp);
5129 		if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
5130 			!= NULL) {
5131 			rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
5132 			    (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
5133 			if (rc) {
5134 				mempool_free(mbox, phba->mbox_mem_pool);
5135 			}
5136 			else {
5137 				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5138 				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5139 				mbox->vport = vport;
5140 				mbox->ctx_ndlp = ndlp;
5141 				rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5142 				if (rc == MBX_NOT_FINISHED) {
5143 					mempool_free(mbox, phba->mbox_mem_pool);
5144 				}
5145 			}
5146 		}
5147 	}
5148 	lpfc_cleanup_node(vport, ndlp);
5149 
5150 	/*
5151 	 * ndlp->rport must be set to NULL before it reaches here
5152 	 * i.e. break rport/node link before doing lpfc_nlp_put for
5153 	 * registered rport and then drop the reference of rport.
5154 	 */
5155 	if (ndlp->rport) {
5156 		/*
5157 		 * extra lpfc_nlp_put dropped the reference of ndlp
5158 		 * for registered rport so need to cleanup rport
5159 		 */
5160 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5161 				"0940 removed node x%p DID x%x "
5162 				" rport not null %p\n",
5163 				ndlp, ndlp->nlp_DID, ndlp->rport);
5164 		rport = ndlp->rport;
5165 		rdata = rport->dd_data;
5166 		rdata->pnode = NULL;
5167 		ndlp->rport = NULL;
5168 		put_device(&rport->dev);
5169 	}
5170 }
5171 
5172 static int
5173 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5174 	      uint32_t did)
5175 {
5176 	D_ID mydid, ndlpdid, matchdid;
5177 
5178 	if (did == Bcast_DID)
5179 		return 0;
5180 
5181 	/* First check for Direct match */
5182 	if (ndlp->nlp_DID == did)
5183 		return 1;
5184 
5185 	/* Next check for area/domain identically equals 0 match */
5186 	mydid.un.word = vport->fc_myDID;
5187 	if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
5188 		return 0;
5189 	}
5190 
5191 	matchdid.un.word = did;
5192 	ndlpdid.un.word = ndlp->nlp_DID;
5193 	if (matchdid.un.b.id == ndlpdid.un.b.id) {
5194 		if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5195 		    (mydid.un.b.area == matchdid.un.b.area)) {
5196 			/* This code is supposed to match the ID
5197 			 * for a private loop device that is
5198 			 * connect to fl_port. But we need to
5199 			 * check that the port did not just go
5200 			 * from pt2pt to fabric or we could end
5201 			 * up matching ndlp->nlp_DID 000001 to
5202 			 * fabric DID 0x20101
5203 			 */
5204 			if ((ndlpdid.un.b.domain == 0) &&
5205 			    (ndlpdid.un.b.area == 0)) {
5206 				if (ndlpdid.un.b.id &&
5207 				    vport->phba->fc_topology ==
5208 				    LPFC_TOPOLOGY_LOOP)
5209 					return 1;
5210 			}
5211 			return 0;
5212 		}
5213 
5214 		matchdid.un.word = ndlp->nlp_DID;
5215 		if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5216 		    (mydid.un.b.area == ndlpdid.un.b.area)) {
5217 			if ((matchdid.un.b.domain == 0) &&
5218 			    (matchdid.un.b.area == 0)) {
5219 				if (matchdid.un.b.id)
5220 					return 1;
5221 			}
5222 		}
5223 	}
5224 	return 0;
5225 }
5226 
5227 /* Search for a nodelist entry */
5228 static struct lpfc_nodelist *
5229 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5230 {
5231 	struct lpfc_nodelist *ndlp;
5232 	uint32_t data1;
5233 
5234 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5235 		if (lpfc_matchdid(vport, ndlp, did)) {
5236 			data1 = (((uint32_t) ndlp->nlp_state << 24) |
5237 				 ((uint32_t) ndlp->nlp_xri << 16) |
5238 				 ((uint32_t) ndlp->nlp_type << 8) |
5239 				 ((uint32_t) ndlp->nlp_rpi & 0xff));
5240 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5241 					 "0929 FIND node DID "
5242 					 "Data: x%p x%x x%x x%x %p\n",
5243 					 ndlp, ndlp->nlp_DID,
5244 					 ndlp->nlp_flag, data1,
5245 					 ndlp->active_rrqs_xri_bitmap);
5246 			return ndlp;
5247 		}
5248 	}
5249 
5250 	/* FIND node did <did> NOT FOUND */
5251 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5252 			 "0932 FIND node did x%x NOT FOUND.\n", did);
5253 	return NULL;
5254 }
5255 
5256 struct lpfc_nodelist *
5257 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5258 {
5259 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5260 	struct lpfc_nodelist *ndlp;
5261 	unsigned long iflags;
5262 
5263 	spin_lock_irqsave(shost->host_lock, iflags);
5264 	ndlp = __lpfc_findnode_did(vport, did);
5265 	spin_unlock_irqrestore(shost->host_lock, iflags);
5266 	return ndlp;
5267 }
5268 
5269 struct lpfc_nodelist *
5270 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5271 {
5272 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5273 	struct lpfc_nodelist *ndlp;
5274 
5275 	ndlp = lpfc_findnode_did(vport, did);
5276 	if (!ndlp) {
5277 		if (vport->phba->nvmet_support)
5278 			return NULL;
5279 		if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5280 		    lpfc_rscn_payload_check(vport, did) == 0)
5281 			return NULL;
5282 		ndlp = lpfc_nlp_init(vport, did);
5283 		if (!ndlp)
5284 			return NULL;
5285 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5286 		spin_lock_irq(shost->host_lock);
5287 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5288 		spin_unlock_irq(shost->host_lock);
5289 		return ndlp;
5290 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
5291 		if (vport->phba->nvmet_support)
5292 			return NULL;
5293 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5294 		if (!ndlp)
5295 			return NULL;
5296 		spin_lock_irq(shost->host_lock);
5297 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5298 		spin_unlock_irq(shost->host_lock);
5299 		return ndlp;
5300 	}
5301 
5302 	/* The NVME Target does not want to actively manage an rport.
5303 	 * The goal is to allow the target to reset its state and clear
5304 	 * pending IO in preparation for the initiator to recover.
5305 	 */
5306 	if ((vport->fc_flag & FC_RSCN_MODE) &&
5307 	    !(vport->fc_flag & FC_NDISC_ACTIVE)) {
5308 		if (lpfc_rscn_payload_check(vport, did)) {
5309 
5310 			/* Since this node is marked for discovery,
5311 			 * delay timeout is not needed.
5312 			 */
5313 			lpfc_cancel_retry_delay_tmo(vport, ndlp);
5314 
5315 			/* NVME Target mode waits until rport is known to be
5316 			 * impacted by the RSCN before it transitions.  No
5317 			 * active management - just go to NPR provided the
5318 			 * node had a valid login.
5319 			 */
5320 			if (vport->phba->nvmet_support)
5321 				return ndlp;
5322 
5323 			/* If we've already received a PLOGI from this NPort
5324 			 * we don't need to try to discover it again.
5325 			 */
5326 			if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5327 				return NULL;
5328 
5329 			spin_lock_irq(shost->host_lock);
5330 			ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5331 			spin_unlock_irq(shost->host_lock);
5332 		} else
5333 			ndlp = NULL;
5334 	} else {
5335 		/* If the initiator received a PLOGI from this NPort or if the
5336 		 * initiator is already in the process of discovery on it,
5337 		 * there's no need to try to discover it again.
5338 		 */
5339 		if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
5340 		    ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5341 		    (!vport->phba->nvmet_support &&
5342 		     ndlp->nlp_flag & NLP_RCV_PLOGI))
5343 			return NULL;
5344 
5345 		if (vport->phba->nvmet_support)
5346 			return ndlp;
5347 
5348 		/* Moving to NPR state clears unsolicited flags and
5349 		 * allows for rediscovery
5350 		 */
5351 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5352 
5353 		spin_lock_irq(shost->host_lock);
5354 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5355 		spin_unlock_irq(shost->host_lock);
5356 	}
5357 	return ndlp;
5358 }
5359 
5360 /* Build a list of nodes to discover based on the loopmap */
5361 void
5362 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
5363 {
5364 	struct lpfc_hba  *phba = vport->phba;
5365 	int j;
5366 	uint32_t alpa, index;
5367 
5368 	if (!lpfc_is_link_up(phba))
5369 		return;
5370 
5371 	if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
5372 		return;
5373 
5374 	/* Check for loop map present or not */
5375 	if (phba->alpa_map[0]) {
5376 		for (j = 1; j <= phba->alpa_map[0]; j++) {
5377 			alpa = phba->alpa_map[j];
5378 			if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
5379 				continue;
5380 			lpfc_setup_disc_node(vport, alpa);
5381 		}
5382 	} else {
5383 		/* No alpamap, so try all alpa's */
5384 		for (j = 0; j < FC_MAXLOOP; j++) {
5385 			/* If cfg_scan_down is set, start from highest
5386 			 * ALPA (0xef) to lowest (0x1).
5387 			 */
5388 			if (vport->cfg_scan_down)
5389 				index = j;
5390 			else
5391 				index = FC_MAXLOOP - j - 1;
5392 			alpa = lpfcAlpaArray[index];
5393 			if ((vport->fc_myDID & 0xff) == alpa)
5394 				continue;
5395 			lpfc_setup_disc_node(vport, alpa);
5396 		}
5397 	}
5398 	return;
5399 }
5400 
5401 /* SLI3 only */
5402 void
5403 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
5404 {
5405 	LPFC_MBOXQ_t *mbox;
5406 	struct lpfc_sli *psli = &phba->sli;
5407 	struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5408 	struct lpfc_sli_ring *fcp_ring   = &psli->sli3_ring[LPFC_FCP_RING];
5409 	int  rc;
5410 
5411 	/*
5412 	 * if it's not a physical port or if we already send
5413 	 * clear_la then don't send it.
5414 	 */
5415 	if ((phba->link_state >= LPFC_CLEAR_LA) ||
5416 	    (vport->port_type != LPFC_PHYSICAL_PORT) ||
5417 		(phba->sli_rev == LPFC_SLI_REV4))
5418 		return;
5419 
5420 			/* Link up discovery */
5421 	if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5422 		phba->link_state = LPFC_CLEAR_LA;
5423 		lpfc_clear_la(phba, mbox);
5424 		mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5425 		mbox->vport = vport;
5426 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5427 		if (rc == MBX_NOT_FINISHED) {
5428 			mempool_free(mbox, phba->mbox_mem_pool);
5429 			lpfc_disc_flush_list(vport);
5430 			extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5431 			fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5432 			phba->link_state = LPFC_HBA_ERROR;
5433 		}
5434 	}
5435 }
5436 
5437 /* Reg_vpi to tell firmware to resume normal operations */
5438 void
5439 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5440 {
5441 	LPFC_MBOXQ_t *regvpimbox;
5442 
5443 	regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5444 	if (regvpimbox) {
5445 		lpfc_reg_vpi(vport, regvpimbox);
5446 		regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5447 		regvpimbox->vport = vport;
5448 		if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
5449 					== MBX_NOT_FINISHED) {
5450 			mempool_free(regvpimbox, phba->mbox_mem_pool);
5451 		}
5452 	}
5453 }
5454 
5455 /* Start Link up / RSCN discovery on NPR nodes */
5456 void
5457 lpfc_disc_start(struct lpfc_vport *vport)
5458 {
5459 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5460 	struct lpfc_hba  *phba = vport->phba;
5461 	uint32_t num_sent;
5462 	uint32_t clear_la_pending;
5463 
5464 	if (!lpfc_is_link_up(phba)) {
5465 		lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5466 				 "3315 Link is not up %x\n",
5467 				 phba->link_state);
5468 		return;
5469 	}
5470 
5471 	if (phba->link_state == LPFC_CLEAR_LA)
5472 		clear_la_pending = 1;
5473 	else
5474 		clear_la_pending = 0;
5475 
5476 	if (vport->port_state < LPFC_VPORT_READY)
5477 		vport->port_state = LPFC_DISC_AUTH;
5478 
5479 	lpfc_set_disctmo(vport);
5480 
5481 	vport->fc_prevDID = vport->fc_myDID;
5482 	vport->num_disc_nodes = 0;
5483 
5484 	/* Start Discovery state <hba_state> */
5485 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5486 			 "0202 Start Discovery hba state x%x "
5487 			 "Data: x%x x%x x%x\n",
5488 			 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5489 			 vport->fc_adisc_cnt);
5490 
5491 	/* First do ADISCs - if any */
5492 	num_sent = lpfc_els_disc_adisc(vport);
5493 
5494 	if (num_sent)
5495 		return;
5496 
5497 	/* Register the VPI for SLI3, NPIV only. */
5498 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5499 	    !(vport->fc_flag & FC_PT2PT) &&
5500 	    !(vport->fc_flag & FC_RSCN_MODE) &&
5501 	    (phba->sli_rev < LPFC_SLI_REV4)) {
5502 		lpfc_issue_clear_la(phba, vport);
5503 		lpfc_issue_reg_vpi(phba, vport);
5504 		return;
5505 	}
5506 
5507 	/*
5508 	 * For SLI2, we need to set port_state to READY and continue
5509 	 * discovery.
5510 	 */
5511 	if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5512 		/* If we get here, there is nothing to ADISC */
5513 		lpfc_issue_clear_la(phba, vport);
5514 
5515 		if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
5516 			vport->num_disc_nodes = 0;
5517 			/* go thru NPR nodes and issue ELS PLOGIs */
5518 			if (vport->fc_npr_cnt)
5519 				lpfc_els_disc_plogi(vport);
5520 
5521 			if (!vport->num_disc_nodes) {
5522 				spin_lock_irq(shost->host_lock);
5523 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
5524 				spin_unlock_irq(shost->host_lock);
5525 				lpfc_can_disctmo(vport);
5526 			}
5527 		}
5528 		vport->port_state = LPFC_VPORT_READY;
5529 	} else {
5530 		/* Next do PLOGIs - if any */
5531 		num_sent = lpfc_els_disc_plogi(vport);
5532 
5533 		if (num_sent)
5534 			return;
5535 
5536 		if (vport->fc_flag & FC_RSCN_MODE) {
5537 			/* Check to see if more RSCNs came in while we
5538 			 * were processing this one.
5539 			 */
5540 			if ((vport->fc_rscn_id_cnt == 0) &&
5541 			    (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5542 				spin_lock_irq(shost->host_lock);
5543 				vport->fc_flag &= ~FC_RSCN_MODE;
5544 				spin_unlock_irq(shost->host_lock);
5545 				lpfc_can_disctmo(vport);
5546 			} else
5547 				lpfc_els_handle_rscn(vport);
5548 		}
5549 	}
5550 	return;
5551 }
5552 
5553 /*
5554  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5555  *  ring the match the sppecified nodelist.
5556  */
5557 static void
5558 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
5559 {
5560 	LIST_HEAD(completions);
5561 	IOCB_t     *icmd;
5562 	struct lpfc_iocbq    *iocb, *next_iocb;
5563 	struct lpfc_sli_ring *pring;
5564 
5565 	pring = lpfc_phba_elsring(phba);
5566 	if (unlikely(!pring))
5567 		return;
5568 
5569 	/* Error matching iocb on txq or txcmplq
5570 	 * First check the txq.
5571 	 */
5572 	spin_lock_irq(&phba->hbalock);
5573 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5574 		if (iocb->context1 != ndlp) {
5575 			continue;
5576 		}
5577 		icmd = &iocb->iocb;
5578 		if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5579 		    (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5580 
5581 			list_move_tail(&iocb->list, &completions);
5582 		}
5583 	}
5584 
5585 	/* Next check the txcmplq */
5586 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5587 		if (iocb->context1 != ndlp) {
5588 			continue;
5589 		}
5590 		icmd = &iocb->iocb;
5591 		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5592 		    icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
5593 			lpfc_sli_issue_abort_iotag(phba, pring, iocb);
5594 		}
5595 	}
5596 	spin_unlock_irq(&phba->hbalock);
5597 
5598 	/* Cancel all the IOCBs from the completions list */
5599 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5600 			      IOERR_SLI_ABORTED);
5601 }
5602 
5603 static void
5604 lpfc_disc_flush_list(struct lpfc_vport *vport)
5605 {
5606 	struct lpfc_nodelist *ndlp, *next_ndlp;
5607 	struct lpfc_hba *phba = vport->phba;
5608 
5609 	if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5610 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
5611 					 nlp_listp) {
5612 			if (!NLP_CHK_NODE_ACT(ndlp))
5613 				continue;
5614 			if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5615 			    ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5616 				lpfc_free_tx(phba, ndlp);
5617 			}
5618 		}
5619 	}
5620 }
5621 
5622 void
5623 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5624 {
5625 	lpfc_els_flush_rscn(vport);
5626 	lpfc_els_flush_cmd(vport);
5627 	lpfc_disc_flush_list(vport);
5628 }
5629 
5630 /*****************************************************************************/
5631 /*
5632  * NAME:     lpfc_disc_timeout
5633  *
5634  * FUNCTION: Fibre Channel driver discovery timeout routine.
5635  *
5636  * EXECUTION ENVIRONMENT: interrupt only
5637  *
5638  * CALLED FROM:
5639  *      Timer function
5640  *
5641  * RETURNS:
5642  *      none
5643  */
5644 /*****************************************************************************/
5645 void
5646 lpfc_disc_timeout(struct timer_list *t)
5647 {
5648 	struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
5649 	struct lpfc_hba   *phba = vport->phba;
5650 	uint32_t tmo_posted;
5651 	unsigned long flags = 0;
5652 
5653 	if (unlikely(!phba))
5654 		return;
5655 
5656 	spin_lock_irqsave(&vport->work_port_lock, flags);
5657 	tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5658 	if (!tmo_posted)
5659 		vport->work_port_events |= WORKER_DISC_TMO;
5660 	spin_unlock_irqrestore(&vport->work_port_lock, flags);
5661 
5662 	if (!tmo_posted)
5663 		lpfc_worker_wake_up(phba);
5664 	return;
5665 }
5666 
5667 static void
5668 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
5669 {
5670 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5671 	struct lpfc_hba  *phba = vport->phba;
5672 	struct lpfc_sli  *psli = &phba->sli;
5673 	struct lpfc_nodelist *ndlp, *next_ndlp;
5674 	LPFC_MBOXQ_t *initlinkmbox;
5675 	int rc, clrlaerr = 0;
5676 
5677 	if (!(vport->fc_flag & FC_DISC_TMO))
5678 		return;
5679 
5680 	spin_lock_irq(shost->host_lock);
5681 	vport->fc_flag &= ~FC_DISC_TMO;
5682 	spin_unlock_irq(shost->host_lock);
5683 
5684 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5685 		"disc timeout:    state:x%x rtry:x%x flg:x%x",
5686 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5687 
5688 	switch (vport->port_state) {
5689 
5690 	case LPFC_LOCAL_CFG_LINK:
5691 		/*
5692 		 * port_state is identically  LPFC_LOCAL_CFG_LINK while
5693 		 * waiting for FAN timeout
5694 		 */
5695 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5696 				 "0221 FAN timeout\n");
5697 
5698 		/* Start discovery by sending FLOGI, clean up old rpis */
5699 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
5700 					 nlp_listp) {
5701 			if (!NLP_CHK_NODE_ACT(ndlp))
5702 				continue;
5703 			if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5704 				continue;
5705 			if (ndlp->nlp_type & NLP_FABRIC) {
5706 				/* Clean up the ndlp on Fabric connections */
5707 				lpfc_drop_node(vport, ndlp);
5708 
5709 			} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
5710 				/* Fail outstanding IO now since device
5711 				 * is marked for PLOGI.
5712 				 */
5713 				lpfc_unreg_rpi(vport, ndlp);
5714 			}
5715 		}
5716 		if (vport->port_state != LPFC_FLOGI) {
5717 			if (phba->sli_rev <= LPFC_SLI_REV3)
5718 				lpfc_initial_flogi(vport);
5719 			else
5720 				lpfc_issue_init_vfi(vport);
5721 			return;
5722 		}
5723 		break;
5724 
5725 	case LPFC_FDISC:
5726 	case LPFC_FLOGI:
5727 	/* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
5728 		/* Initial FLOGI timeout */
5729 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5730 				 "0222 Initial %s timeout\n",
5731 				 vport->vpi ? "FDISC" : "FLOGI");
5732 
5733 		/* Assume no Fabric and go on with discovery.
5734 		 * Check for outstanding ELS FLOGI to abort.
5735 		 */
5736 
5737 		/* FLOGI failed, so just use loop map to make discovery list */
5738 		lpfc_disc_list_loopmap(vport);
5739 
5740 		/* Start discovery */
5741 		lpfc_disc_start(vport);
5742 		break;
5743 
5744 	case LPFC_FABRIC_CFG_LINK:
5745 	/* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5746 	   NameServer login */
5747 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5748 				 "0223 Timeout while waiting for "
5749 				 "NameServer login\n");
5750 		/* Next look for NameServer ndlp */
5751 		ndlp = lpfc_findnode_did(vport, NameServer_DID);
5752 		if (ndlp && NLP_CHK_NODE_ACT(ndlp))
5753 			lpfc_els_abort(phba, ndlp);
5754 
5755 		/* ReStart discovery */
5756 		goto restart_disc;
5757 
5758 	case LPFC_NS_QRY:
5759 	/* Check for wait for NameServer Rsp timeout */
5760 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5761 				 "0224 NameServer Query timeout "
5762 				 "Data: x%x x%x\n",
5763 				 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
5764 
5765 		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5766 			/* Try it one more time */
5767 			vport->fc_ns_retry++;
5768 			vport->gidft_inp = 0;
5769 			rc = lpfc_issue_gidft(vport);
5770 			if (rc == 0)
5771 				break;
5772 		}
5773 		vport->fc_ns_retry = 0;
5774 
5775 restart_disc:
5776 		/*
5777 		 * Discovery is over.
5778 		 * set port_state to PORT_READY if SLI2.
5779 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
5780 		 */
5781 		if (phba->sli_rev < LPFC_SLI_REV4) {
5782 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5783 				lpfc_issue_reg_vpi(phba, vport);
5784 			else  {
5785 				lpfc_issue_clear_la(phba, vport);
5786 				vport->port_state = LPFC_VPORT_READY;
5787 			}
5788 		}
5789 
5790 		/* Setup and issue mailbox INITIALIZE LINK command */
5791 		initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5792 		if (!initlinkmbox) {
5793 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5794 					 "0206 Device Discovery "
5795 					 "completion error\n");
5796 			phba->link_state = LPFC_HBA_ERROR;
5797 			break;
5798 		}
5799 
5800 		lpfc_linkdown(phba);
5801 		lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5802 			       phba->cfg_link_speed);
5803 		initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
5804 		initlinkmbox->vport = vport;
5805 		initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5806 		rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
5807 		lpfc_set_loopback_flag(phba);
5808 		if (rc == MBX_NOT_FINISHED)
5809 			mempool_free(initlinkmbox, phba->mbox_mem_pool);
5810 
5811 		break;
5812 
5813 	case LPFC_DISC_AUTH:
5814 	/* Node Authentication timeout */
5815 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5816 				 "0227 Node Authentication timeout\n");
5817 		lpfc_disc_flush_list(vport);
5818 
5819 		/*
5820 		 * set port_state to PORT_READY if SLI2.
5821 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
5822 		 */
5823 		if (phba->sli_rev < LPFC_SLI_REV4) {
5824 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5825 				lpfc_issue_reg_vpi(phba, vport);
5826 			else  {	/* NPIV Not enabled */
5827 				lpfc_issue_clear_la(phba, vport);
5828 				vport->port_state = LPFC_VPORT_READY;
5829 			}
5830 		}
5831 		break;
5832 
5833 	case LPFC_VPORT_READY:
5834 		if (vport->fc_flag & FC_RSCN_MODE) {
5835 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5836 					 "0231 RSCN timeout Data: x%x "
5837 					 "x%x\n",
5838 					 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
5839 
5840 			/* Cleanup any outstanding ELS commands */
5841 			lpfc_els_flush_cmd(vport);
5842 
5843 			lpfc_els_flush_rscn(vport);
5844 			lpfc_disc_flush_list(vport);
5845 		}
5846 		break;
5847 
5848 	default:
5849 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5850 				 "0273 Unexpected discovery timeout, "
5851 				 "vport State x%x\n", vport->port_state);
5852 		break;
5853 	}
5854 
5855 	switch (phba->link_state) {
5856 	case LPFC_CLEAR_LA:
5857 				/* CLEAR LA timeout */
5858 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5859 				 "0228 CLEAR LA timeout\n");
5860 		clrlaerr = 1;
5861 		break;
5862 
5863 	case LPFC_LINK_UP:
5864 		lpfc_issue_clear_la(phba, vport);
5865 		/* Drop thru */
5866 	case LPFC_LINK_UNKNOWN:
5867 	case LPFC_WARM_START:
5868 	case LPFC_INIT_START:
5869 	case LPFC_INIT_MBX_CMDS:
5870 	case LPFC_LINK_DOWN:
5871 	case LPFC_HBA_ERROR:
5872 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5873 				 "0230 Unexpected timeout, hba link "
5874 				 "state x%x\n", phba->link_state);
5875 		clrlaerr = 1;
5876 		break;
5877 
5878 	case LPFC_HBA_READY:
5879 		break;
5880 	}
5881 
5882 	if (clrlaerr) {
5883 		lpfc_disc_flush_list(vport);
5884 		if (phba->sli_rev != LPFC_SLI_REV4) {
5885 			psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5886 				~LPFC_STOP_IOCB_EVENT;
5887 			psli->sli3_ring[LPFC_FCP_RING].flag &=
5888 				~LPFC_STOP_IOCB_EVENT;
5889 		}
5890 		vport->port_state = LPFC_VPORT_READY;
5891 	}
5892 	return;
5893 }
5894 
5895 /*
5896  * This routine handles processing a NameServer REG_LOGIN mailbox
5897  * command upon completion. It is setup in the LPFC_MBOXQ
5898  * as the completion routine when the command is
5899  * handed off to the SLI layer.
5900  */
5901 void
5902 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5903 {
5904 	MAILBOX_t *mb = &pmb->u.mb;
5905 	struct lpfc_dmabuf   *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
5906 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
5907 	struct lpfc_vport    *vport = pmb->vport;
5908 
5909 	pmb->ctx_buf = NULL;
5910 	pmb->ctx_ndlp = NULL;
5911 
5912 	if (phba->sli_rev < LPFC_SLI_REV4)
5913 		ndlp->nlp_rpi = mb->un.varWords[0];
5914 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
5915 	ndlp->nlp_type |= NLP_FABRIC;
5916 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
5917 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5918 			 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5919 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5920 			 kref_read(&ndlp->kref),
5921 			 ndlp->nlp_usg_map, ndlp);
5922 	/*
5923 	 * Start issuing Fabric-Device Management Interface (FDMI) command to
5924 	 * 0xfffffa (FDMI well known port).
5925 	 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
5926 	 * DPRT -> RPRT (vports)
5927 	 */
5928 	if (vport->port_type == LPFC_PHYSICAL_PORT)
5929 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
5930 	else
5931 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5932 
5933 
5934 	/* decrement the node reference count held for this callback
5935 	 * function.
5936 	 */
5937 	lpfc_nlp_put(ndlp);
5938 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
5939 	kfree(mp);
5940 	mempool_free(pmb, phba->mbox_mem_pool);
5941 
5942 	return;
5943 }
5944 
5945 static int
5946 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5947 {
5948 	uint16_t *rpi = param;
5949 
5950 	/* check for active node */
5951 	if (!NLP_CHK_NODE_ACT(ndlp))
5952 		return 0;
5953 
5954 	return ndlp->nlp_rpi == *rpi;
5955 }
5956 
5957 static int
5958 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5959 {
5960 	return memcmp(&ndlp->nlp_portname, param,
5961 		      sizeof(ndlp->nlp_portname)) == 0;
5962 }
5963 
5964 static struct lpfc_nodelist *
5965 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
5966 {
5967 	struct lpfc_nodelist *ndlp;
5968 
5969 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5970 		if (filter(ndlp, param)) {
5971 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5972 					 "3185 FIND node filter %p DID "
5973 					 "ndlp %p did x%x flg x%x st x%x "
5974 					 "xri x%x type x%x rpi x%x\n",
5975 					 filter, ndlp, ndlp->nlp_DID,
5976 					 ndlp->nlp_flag, ndlp->nlp_state,
5977 					 ndlp->nlp_xri, ndlp->nlp_type,
5978 					 ndlp->nlp_rpi);
5979 			return ndlp;
5980 		}
5981 	}
5982 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5983 			 "3186 FIND node filter %p NOT FOUND.\n", filter);
5984 	return NULL;
5985 }
5986 
5987 /*
5988  * This routine looks up the ndlp lists for the given RPI. If rpi found it
5989  * returns the node list element pointer else return NULL.
5990  */
5991 struct lpfc_nodelist *
5992 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5993 {
5994 	return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
5995 }
5996 
5997 /*
5998  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
5999  * returns the node element list pointer else return NULL.
6000  */
6001 struct lpfc_nodelist *
6002 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
6003 {
6004 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6005 	struct lpfc_nodelist *ndlp;
6006 
6007 	spin_lock_irq(shost->host_lock);
6008 	ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
6009 	spin_unlock_irq(shost->host_lock);
6010 	return ndlp;
6011 }
6012 
6013 /*
6014  * This routine looks up the ndlp lists for the given RPI. If the rpi
6015  * is found, the routine returns the node element list pointer else
6016  * return NULL.
6017  */
6018 struct lpfc_nodelist *
6019 lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6020 {
6021 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6022 	struct lpfc_nodelist *ndlp;
6023 
6024 	spin_lock_irq(shost->host_lock);
6025 	ndlp = __lpfc_findnode_rpi(vport, rpi);
6026 	spin_unlock_irq(shost->host_lock);
6027 	return ndlp;
6028 }
6029 
6030 /**
6031  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6032  * @phba: pointer to lpfc hba data structure.
6033  * @vpi: the physical host virtual N_Port identifier.
6034  *
6035  * This routine finds a vport on a HBA (referred by @phba) through a
6036  * @vpi. The function walks the HBA's vport list and returns the address
6037  * of the vport with the matching @vpi.
6038  *
6039  * Return code
6040  *    NULL - No vport with the matching @vpi found
6041  *    Otherwise - Address to the vport with the matching @vpi.
6042  **/
6043 struct lpfc_vport *
6044 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6045 {
6046 	struct lpfc_vport *vport;
6047 	unsigned long flags;
6048 	int i = 0;
6049 
6050 	/* The physical ports are always vpi 0 - translate is unnecessary. */
6051 	if (vpi > 0) {
6052 		/*
6053 		 * Translate the physical vpi to the logical vpi.  The
6054 		 * vport stores the logical vpi.
6055 		 */
6056 		for (i = 0; i < phba->max_vpi; i++) {
6057 			if (vpi == phba->vpi_ids[i])
6058 				break;
6059 		}
6060 
6061 		if (i >= phba->max_vpi) {
6062 			lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
6063 					 "2936 Could not find Vport mapped "
6064 					 "to vpi %d\n", vpi);
6065 			return NULL;
6066 		}
6067 	}
6068 
6069 	spin_lock_irqsave(&phba->port_list_lock, flags);
6070 	list_for_each_entry(vport, &phba->port_list, listentry) {
6071 		if (vport->vpi == i) {
6072 			spin_unlock_irqrestore(&phba->port_list_lock, flags);
6073 			return vport;
6074 		}
6075 	}
6076 	spin_unlock_irqrestore(&phba->port_list_lock, flags);
6077 	return NULL;
6078 }
6079 
6080 struct lpfc_nodelist *
6081 lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
6082 {
6083 	struct lpfc_nodelist *ndlp;
6084 	int rpi = LPFC_RPI_ALLOC_ERROR;
6085 
6086 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6087 		rpi = lpfc_sli4_alloc_rpi(vport->phba);
6088 		if (rpi == LPFC_RPI_ALLOC_ERROR)
6089 			return NULL;
6090 	}
6091 
6092 	ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6093 	if (!ndlp) {
6094 		if (vport->phba->sli_rev == LPFC_SLI_REV4)
6095 			lpfc_sli4_free_rpi(vport->phba, rpi);
6096 		return NULL;
6097 	}
6098 
6099 	memset(ndlp, 0, sizeof (struct lpfc_nodelist));
6100 
6101 	lpfc_initialize_node(vport, ndlp, did);
6102 	INIT_LIST_HEAD(&ndlp->nlp_listp);
6103 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6104 		ndlp->nlp_rpi = rpi;
6105 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6106 				 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
6107 				 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
6108 				 ndlp->nlp_flag,
6109 				 kref_read(&ndlp->kref),
6110 				 ndlp->nlp_usg_map, ndlp);
6111 
6112 		ndlp->active_rrqs_xri_bitmap =
6113 				mempool_alloc(vport->phba->active_rrq_pool,
6114 					      GFP_KERNEL);
6115 		if (ndlp->active_rrqs_xri_bitmap)
6116 			memset(ndlp->active_rrqs_xri_bitmap, 0,
6117 			       ndlp->phba->cfg_rrq_xri_bitmap_sz);
6118 	}
6119 
6120 
6121 
6122 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6123 		"node init:       did:x%x",
6124 		ndlp->nlp_DID, 0, 0);
6125 
6126 	return ndlp;
6127 }
6128 
6129 /* This routine releases all resources associated with a specifc NPort's ndlp
6130  * and mempool_free's the nodelist.
6131  */
6132 static void
6133 lpfc_nlp_release(struct kref *kref)
6134 {
6135 	struct lpfc_hba *phba;
6136 	unsigned long flags;
6137 	struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6138 						  kref);
6139 
6140 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6141 		"node release:    did:x%x flg:x%x type:x%x",
6142 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6143 
6144 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
6145 			"0279 lpfc_nlp_release: ndlp:x%p did %x "
6146 			"usgmap:x%x refcnt:%d rpi:%x\n",
6147 			(void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
6148 			kref_read(&ndlp->kref), ndlp->nlp_rpi);
6149 
6150 	/* remove ndlp from action. */
6151 	lpfc_nlp_remove(ndlp->vport, ndlp);
6152 
6153 	/* clear the ndlp active flag for all release cases */
6154 	phba = ndlp->phba;
6155 	spin_lock_irqsave(&phba->ndlp_lock, flags);
6156 	NLP_CLR_NODE_ACT(ndlp);
6157 	spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6158 	if (phba->sli_rev == LPFC_SLI_REV4)
6159 		lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
6160 
6161 	/* free ndlp memory for final ndlp release */
6162 	if (NLP_CHK_FREE_REQ(ndlp)) {
6163 		kfree(ndlp->lat_data);
6164 		if (phba->sli_rev == LPFC_SLI_REV4)
6165 			mempool_free(ndlp->active_rrqs_xri_bitmap,
6166 				     ndlp->phba->active_rrq_pool);
6167 		mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
6168 	}
6169 }
6170 
6171 /* This routine bumps the reference count for a ndlp structure to ensure
6172  * that one discovery thread won't free a ndlp while another discovery thread
6173  * is using it.
6174  */
6175 struct lpfc_nodelist *
6176 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6177 {
6178 	struct lpfc_hba *phba;
6179 	unsigned long flags;
6180 
6181 	if (ndlp) {
6182 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6183 			"node get:        did:x%x flg:x%x refcnt:x%x",
6184 			ndlp->nlp_DID, ndlp->nlp_flag,
6185 			kref_read(&ndlp->kref));
6186 		/* The check of ndlp usage to prevent incrementing the
6187 		 * ndlp reference count that is in the process of being
6188 		 * released.
6189 		 */
6190 		phba = ndlp->phba;
6191 		spin_lock_irqsave(&phba->ndlp_lock, flags);
6192 		if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
6193 			spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6194 			lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6195 				"0276 lpfc_nlp_get: ndlp:x%p "
6196 				"usgmap:x%x refcnt:%d\n",
6197 				(void *)ndlp, ndlp->nlp_usg_map,
6198 				kref_read(&ndlp->kref));
6199 			return NULL;
6200 		} else
6201 			kref_get(&ndlp->kref);
6202 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6203 	}
6204 	return ndlp;
6205 }
6206 
6207 /* This routine decrements the reference count for a ndlp structure. If the
6208  * count goes to 0, this indicates the the associated nodelist should be
6209  * freed. Returning 1 indicates the ndlp resource has been released; on the
6210  * other hand, returning 0 indicates the ndlp resource has not been released
6211  * yet.
6212  */
6213 int
6214 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6215 {
6216 	struct lpfc_hba *phba;
6217 	unsigned long flags;
6218 
6219 	if (!ndlp)
6220 		return 1;
6221 
6222 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6223 	"node put:        did:x%x flg:x%x refcnt:x%x",
6224 		ndlp->nlp_DID, ndlp->nlp_flag,
6225 		kref_read(&ndlp->kref));
6226 	phba = ndlp->phba;
6227 	spin_lock_irqsave(&phba->ndlp_lock, flags);
6228 	/* Check the ndlp memory free acknowledge flag to avoid the
6229 	 * possible race condition that kref_put got invoked again
6230 	 * after previous one has done ndlp memory free.
6231 	 */
6232 	if (NLP_CHK_FREE_ACK(ndlp)) {
6233 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6234 		lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6235 				"0274 lpfc_nlp_put: ndlp:x%p "
6236 				"usgmap:x%x refcnt:%d\n",
6237 				(void *)ndlp, ndlp->nlp_usg_map,
6238 				kref_read(&ndlp->kref));
6239 		return 1;
6240 	}
6241 	/* Check the ndlp inactivate log flag to avoid the possible
6242 	 * race condition that kref_put got invoked again after ndlp
6243 	 * is already in inactivating state.
6244 	 */
6245 	if (NLP_CHK_IACT_REQ(ndlp)) {
6246 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6247 		lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6248 				"0275 lpfc_nlp_put: ndlp:x%p "
6249 				"usgmap:x%x refcnt:%d\n",
6250 				(void *)ndlp, ndlp->nlp_usg_map,
6251 				kref_read(&ndlp->kref));
6252 		return 1;
6253 	}
6254 	/* For last put, mark the ndlp usage flags to make sure no
6255 	 * other kref_get and kref_put on the same ndlp shall get
6256 	 * in between the process when the final kref_put has been
6257 	 * invoked on this ndlp.
6258 	 */
6259 	if (kref_read(&ndlp->kref) == 1) {
6260 		/* Indicate ndlp is put to inactive state. */
6261 		NLP_SET_IACT_REQ(ndlp);
6262 		/* Acknowledge ndlp memory free has been seen. */
6263 		if (NLP_CHK_FREE_REQ(ndlp))
6264 			NLP_SET_FREE_ACK(ndlp);
6265 	}
6266 	spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6267 	/* Note, the kref_put returns 1 when decrementing a reference
6268 	 * count that was 1, it invokes the release callback function,
6269 	 * but it still left the reference count as 1 (not actually
6270 	 * performs the last decrementation). Otherwise, it actually
6271 	 * decrements the reference count and returns 0.
6272 	 */
6273 	return kref_put(&ndlp->kref, lpfc_nlp_release);
6274 }
6275 
6276 /* This routine free's the specified nodelist if it is not in use
6277  * by any other discovery thread. This routine returns 1 if the
6278  * ndlp has been freed. A return value of 0 indicates the ndlp is
6279  * not yet been released.
6280  */
6281 int
6282 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6283 {
6284 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6285 		"node not used:   did:x%x flg:x%x refcnt:x%x",
6286 		ndlp->nlp_DID, ndlp->nlp_flag,
6287 		kref_read(&ndlp->kref));
6288 	if (kref_read(&ndlp->kref) == 1)
6289 		if (lpfc_nlp_put(ndlp))
6290 			return 1;
6291 	return 0;
6292 }
6293 
6294 /**
6295  * lpfc_fcf_inuse - Check if FCF can be unregistered.
6296  * @phba: Pointer to hba context object.
6297  *
6298  * This function iterate through all FC nodes associated
6299  * will all vports to check if there is any node with
6300  * fc_rports associated with it. If there is an fc_rport
6301  * associated with the node, then the node is either in
6302  * discovered state or its devloss_timer is pending.
6303  */
6304 static int
6305 lpfc_fcf_inuse(struct lpfc_hba *phba)
6306 {
6307 	struct lpfc_vport **vports;
6308 	int i, ret = 0;
6309 	struct lpfc_nodelist *ndlp;
6310 	struct Scsi_Host  *shost;
6311 
6312 	vports = lpfc_create_vport_work_array(phba);
6313 
6314 	/* If driver cannot allocate memory, indicate fcf is in use */
6315 	if (!vports)
6316 		return 1;
6317 
6318 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6319 		shost = lpfc_shost_from_vport(vports[i]);
6320 		spin_lock_irq(shost->host_lock);
6321 		/*
6322 		 * IF the CVL_RCVD bit is not set then we have sent the
6323 		 * flogi.
6324 		 * If dev_loss fires while we are waiting we do not want to
6325 		 * unreg the fcf.
6326 		 */
6327 		if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6328 			spin_unlock_irq(shost->host_lock);
6329 			ret =  1;
6330 			goto out;
6331 		}
6332 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6333 			if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6334 			  (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6335 				ret = 1;
6336 				spin_unlock_irq(shost->host_lock);
6337 				goto out;
6338 			} else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6339 				ret = 1;
6340 				lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
6341 						"2624 RPI %x DID %x flag %x "
6342 						"still logged in\n",
6343 						ndlp->nlp_rpi, ndlp->nlp_DID,
6344 						ndlp->nlp_flag);
6345 			}
6346 		}
6347 		spin_unlock_irq(shost->host_lock);
6348 	}
6349 out:
6350 	lpfc_destroy_vport_work_array(phba, vports);
6351 	return ret;
6352 }
6353 
6354 /**
6355  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6356  * @phba: Pointer to hba context object.
6357  * @mboxq: Pointer to mailbox object.
6358  *
6359  * This function frees memory associated with the mailbox command.
6360  */
6361 void
6362 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6363 {
6364 	struct lpfc_vport *vport = mboxq->vport;
6365 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6366 
6367 	if (mboxq->u.mb.mbxStatus) {
6368 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6369 			"2555 UNREG_VFI mbxStatus error x%x "
6370 			"HBA state x%x\n",
6371 			mboxq->u.mb.mbxStatus, vport->port_state);
6372 	}
6373 	spin_lock_irq(shost->host_lock);
6374 	phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6375 	spin_unlock_irq(shost->host_lock);
6376 	mempool_free(mboxq, phba->mbox_mem_pool);
6377 	return;
6378 }
6379 
6380 /**
6381  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6382  * @phba: Pointer to hba context object.
6383  * @mboxq: Pointer to mailbox object.
6384  *
6385  * This function frees memory associated with the mailbox command.
6386  */
6387 static void
6388 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6389 {
6390 	struct lpfc_vport *vport = mboxq->vport;
6391 
6392 	if (mboxq->u.mb.mbxStatus) {
6393 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6394 			"2550 UNREG_FCFI mbxStatus error x%x "
6395 			"HBA state x%x\n",
6396 			mboxq->u.mb.mbxStatus, vport->port_state);
6397 	}
6398 	mempool_free(mboxq, phba->mbox_mem_pool);
6399 	return;
6400 }
6401 
6402 /**
6403  * lpfc_unregister_fcf_prep - Unregister fcf record preparation
6404  * @phba: Pointer to hba context object.
6405  *
6406  * This function prepare the HBA for unregistering the currently registered
6407  * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6408  * VFIs.
6409  */
6410 int
6411 lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
6412 {
6413 	struct lpfc_vport **vports;
6414 	struct lpfc_nodelist *ndlp;
6415 	struct Scsi_Host *shost;
6416 	int i = 0, rc;
6417 
6418 	/* Unregister RPIs */
6419 	if (lpfc_fcf_inuse(phba))
6420 		lpfc_unreg_hba_rpis(phba);
6421 
6422 	/* At this point, all discovery is aborted */
6423 	phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6424 
6425 	/* Unregister VPIs */
6426 	vports = lpfc_create_vport_work_array(phba);
6427 	if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
6428 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6429 			/* Stop FLOGI/FDISC retries */
6430 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6431 			if (ndlp)
6432 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6433 			lpfc_cleanup_pending_mbox(vports[i]);
6434 			if (phba->sli_rev == LPFC_SLI_REV4)
6435 				lpfc_sli4_unreg_all_rpis(vports[i]);
6436 			lpfc_mbx_unreg_vpi(vports[i]);
6437 			shost = lpfc_shost_from_vport(vports[i]);
6438 			spin_lock_irq(shost->host_lock);
6439 			vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6440 			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
6441 			spin_unlock_irq(shost->host_lock);
6442 		}
6443 	lpfc_destroy_vport_work_array(phba, vports);
6444 	if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6445 		ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6446 		if (ndlp)
6447 			lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6448 		lpfc_cleanup_pending_mbox(phba->pport);
6449 		if (phba->sli_rev == LPFC_SLI_REV4)
6450 			lpfc_sli4_unreg_all_rpis(phba->pport);
6451 		lpfc_mbx_unreg_vpi(phba->pport);
6452 		shost = lpfc_shost_from_vport(phba->pport);
6453 		spin_lock_irq(shost->host_lock);
6454 		phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6455 		phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6456 		spin_unlock_irq(shost->host_lock);
6457 	}
6458 
6459 	/* Cleanup any outstanding ELS commands */
6460 	lpfc_els_flush_all_cmd(phba);
6461 
6462 	/* Unregister the physical port VFI */
6463 	rc = lpfc_issue_unreg_vfi(phba->pport);
6464 	return rc;
6465 }
6466 
6467 /**
6468  * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6469  * @phba: Pointer to hba context object.
6470  *
6471  * This function issues synchronous unregister FCF mailbox command to HBA to
6472  * unregister the currently registered FCF record. The driver does not reset
6473  * the driver FCF usage state flags.
6474  *
6475  * Return 0 if successfully issued, none-zero otherwise.
6476  */
6477 int
6478 lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6479 {
6480 	LPFC_MBOXQ_t *mbox;
6481 	int rc;
6482 
6483 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6484 	if (!mbox) {
6485 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6486 				"2551 UNREG_FCFI mbox allocation failed"
6487 				"HBA state x%x\n", phba->pport->port_state);
6488 		return -ENOMEM;
6489 	}
6490 	lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6491 	mbox->vport = phba->pport;
6492 	mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6493 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6494 
6495 	if (rc == MBX_NOT_FINISHED) {
6496 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6497 				"2552 Unregister FCFI command failed rc x%x "
6498 				"HBA state x%x\n",
6499 				rc, phba->pport->port_state);
6500 		return -EINVAL;
6501 	}
6502 	return 0;
6503 }
6504 
6505 /**
6506  * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6507  * @phba: Pointer to hba context object.
6508  *
6509  * This function unregisters the currently reigstered FCF. This function
6510  * also tries to find another FCF for discovery by rescan the HBA FCF table.
6511  */
6512 void
6513 lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6514 {
6515 	int rc;
6516 
6517 	/* Preparation for unregistering fcf */
6518 	rc = lpfc_unregister_fcf_prep(phba);
6519 	if (rc) {
6520 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6521 				"2748 Failed to prepare for unregistering "
6522 				"HBA's FCF record: rc=%d\n", rc);
6523 		return;
6524 	}
6525 
6526 	/* Now, unregister FCF record and reset HBA FCF state */
6527 	rc = lpfc_sli4_unregister_fcf(phba);
6528 	if (rc)
6529 		return;
6530 	/* Reset HBA FCF states after successful unregister FCF */
6531 	phba->fcf.fcf_flag = 0;
6532 	phba->fcf.current_rec.flag = 0;
6533 
6534 	/*
6535 	 * If driver is not unloading, check if there is any other
6536 	 * FCF record that can be used for discovery.
6537 	 */
6538 	if ((phba->pport->load_flag & FC_UNLOADING) ||
6539 	    (phba->link_state < LPFC_LINK_UP))
6540 		return;
6541 
6542 	/* This is considered as the initial FCF discovery scan */
6543 	spin_lock_irq(&phba->hbalock);
6544 	phba->fcf.fcf_flag |= FCF_INIT_DISC;
6545 	spin_unlock_irq(&phba->hbalock);
6546 
6547 	/* Reset FCF roundrobin bmask for new discovery */
6548 	lpfc_sli4_clear_fcf_rr_bmask(phba);
6549 
6550 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
6551 
6552 	if (rc) {
6553 		spin_lock_irq(&phba->hbalock);
6554 		phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6555 		spin_unlock_irq(&phba->hbalock);
6556 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6557 				"2553 lpfc_unregister_unused_fcf failed "
6558 				"to read FCF record HBA state x%x\n",
6559 				phba->pport->port_state);
6560 	}
6561 }
6562 
6563 /**
6564  * lpfc_unregister_fcf - Unregister the currently registered fcf record
6565  * @phba: Pointer to hba context object.
6566  *
6567  * This function just unregisters the currently reigstered FCF. It does not
6568  * try to find another FCF for discovery.
6569  */
6570 void
6571 lpfc_unregister_fcf(struct lpfc_hba *phba)
6572 {
6573 	int rc;
6574 
6575 	/* Preparation for unregistering fcf */
6576 	rc = lpfc_unregister_fcf_prep(phba);
6577 	if (rc) {
6578 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6579 				"2749 Failed to prepare for unregistering "
6580 				"HBA's FCF record: rc=%d\n", rc);
6581 		return;
6582 	}
6583 
6584 	/* Now, unregister FCF record and reset HBA FCF state */
6585 	rc = lpfc_sli4_unregister_fcf(phba);
6586 	if (rc)
6587 		return;
6588 	/* Set proper HBA FCF states after successful unregister FCF */
6589 	spin_lock_irq(&phba->hbalock);
6590 	phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6591 	spin_unlock_irq(&phba->hbalock);
6592 }
6593 
6594 /**
6595  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6596  * @phba: Pointer to hba context object.
6597  *
6598  * This function check if there are any connected remote port for the FCF and
6599  * if all the devices are disconnected, this function unregister FCFI.
6600  * This function also tries to use another FCF for discovery.
6601  */
6602 void
6603 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6604 {
6605 	/*
6606 	 * If HBA is not running in FIP mode, if HBA does not support
6607 	 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6608 	 * registered, do nothing.
6609 	 */
6610 	spin_lock_irq(&phba->hbalock);
6611 	if (!(phba->hba_flag & HBA_FCOE_MODE) ||
6612 	    !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
6613 	    !(phba->hba_flag & HBA_FIP_SUPPORT) ||
6614 	    (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
6615 	    (phba->pport->port_state == LPFC_FLOGI)) {
6616 		spin_unlock_irq(&phba->hbalock);
6617 		return;
6618 	}
6619 	spin_unlock_irq(&phba->hbalock);
6620 
6621 	if (lpfc_fcf_inuse(phba))
6622 		return;
6623 
6624 	lpfc_unregister_fcf_rescan(phba);
6625 }
6626 
6627 /**
6628  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6629  * @phba: Pointer to hba context object.
6630  * @buff: Buffer containing the FCF connection table as in the config
6631  *         region.
6632  * This function create driver data structure for the FCF connection
6633  * record table read from config region 23.
6634  */
6635 static void
6636 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6637 	uint8_t *buff)
6638 {
6639 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6640 	struct lpfc_fcf_conn_hdr *conn_hdr;
6641 	struct lpfc_fcf_conn_rec *conn_rec;
6642 	uint32_t record_count;
6643 	int i;
6644 
6645 	/* Free the current connect table */
6646 	list_for_each_entry_safe(conn_entry, next_conn_entry,
6647 		&phba->fcf_conn_rec_list, list) {
6648 		list_del_init(&conn_entry->list);
6649 		kfree(conn_entry);
6650 	}
6651 
6652 	conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6653 	record_count = conn_hdr->length * sizeof(uint32_t)/
6654 		sizeof(struct lpfc_fcf_conn_rec);
6655 
6656 	conn_rec = (struct lpfc_fcf_conn_rec *)
6657 		(buff + sizeof(struct lpfc_fcf_conn_hdr));
6658 
6659 	for (i = 0; i < record_count; i++) {
6660 		if (!(conn_rec[i].flags & FCFCNCT_VALID))
6661 			continue;
6662 		conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6663 			GFP_KERNEL);
6664 		if (!conn_entry) {
6665 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6666 				"2566 Failed to allocate connection"
6667 				" table entry\n");
6668 			return;
6669 		}
6670 
6671 		memcpy(&conn_entry->conn_rec, &conn_rec[i],
6672 			sizeof(struct lpfc_fcf_conn_rec));
6673 		list_add_tail(&conn_entry->list,
6674 			&phba->fcf_conn_rec_list);
6675 	}
6676 
6677 	if (!list_empty(&phba->fcf_conn_rec_list)) {
6678 		i = 0;
6679 		list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6680 				    list) {
6681 			conn_rec = &conn_entry->conn_rec;
6682 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6683 					"3345 FCF connection list rec[%02d]: "
6684 					"flags:x%04x, vtag:x%04x, "
6685 					"fabric_name:x%02x:%02x:%02x:%02x:"
6686 					"%02x:%02x:%02x:%02x, "
6687 					"switch_name:x%02x:%02x:%02x:%02x:"
6688 					"%02x:%02x:%02x:%02x\n", i++,
6689 					conn_rec->flags, conn_rec->vlan_tag,
6690 					conn_rec->fabric_name[0],
6691 					conn_rec->fabric_name[1],
6692 					conn_rec->fabric_name[2],
6693 					conn_rec->fabric_name[3],
6694 					conn_rec->fabric_name[4],
6695 					conn_rec->fabric_name[5],
6696 					conn_rec->fabric_name[6],
6697 					conn_rec->fabric_name[7],
6698 					conn_rec->switch_name[0],
6699 					conn_rec->switch_name[1],
6700 					conn_rec->switch_name[2],
6701 					conn_rec->switch_name[3],
6702 					conn_rec->switch_name[4],
6703 					conn_rec->switch_name[5],
6704 					conn_rec->switch_name[6],
6705 					conn_rec->switch_name[7]);
6706 		}
6707 	}
6708 }
6709 
6710 /**
6711  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6712  * @phba: Pointer to hba context object.
6713  * @buff: Buffer containing the FCoE parameter data structure.
6714  *
6715  *  This function update driver data structure with config
6716  *  parameters read from config region 23.
6717  */
6718 static void
6719 lpfc_read_fcoe_param(struct lpfc_hba *phba,
6720 			uint8_t *buff)
6721 {
6722 	struct lpfc_fip_param_hdr *fcoe_param_hdr;
6723 	struct lpfc_fcoe_params *fcoe_param;
6724 
6725 	fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6726 		buff;
6727 	fcoe_param = (struct lpfc_fcoe_params *)
6728 		(buff + sizeof(struct lpfc_fip_param_hdr));
6729 
6730 	if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6731 		(fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6732 		return;
6733 
6734 	if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6735 		phba->valid_vlan = 1;
6736 		phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6737 			0xFFF;
6738 	}
6739 
6740 	phba->fc_map[0] = fcoe_param->fc_map[0];
6741 	phba->fc_map[1] = fcoe_param->fc_map[1];
6742 	phba->fc_map[2] = fcoe_param->fc_map[2];
6743 	return;
6744 }
6745 
6746 /**
6747  * lpfc_get_rec_conf23 - Get a record type in config region data.
6748  * @buff: Buffer containing config region 23 data.
6749  * @size: Size of the data buffer.
6750  * @rec_type: Record type to be searched.
6751  *
6752  * This function searches config region data to find the beginning
6753  * of the record specified by record_type. If record found, this
6754  * function return pointer to the record else return NULL.
6755  */
6756 static uint8_t *
6757 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6758 {
6759 	uint32_t offset = 0, rec_length;
6760 
6761 	if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6762 		(size < sizeof(uint32_t)))
6763 		return NULL;
6764 
6765 	rec_length = buff[offset + 1];
6766 
6767 	/*
6768 	 * One TLV record has one word header and number of data words
6769 	 * specified in the rec_length field of the record header.
6770 	 */
6771 	while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6772 		<= size) {
6773 		if (buff[offset] == rec_type)
6774 			return &buff[offset];
6775 
6776 		if (buff[offset] == LPFC_REGION23_LAST_REC)
6777 			return NULL;
6778 
6779 		offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6780 		rec_length = buff[offset + 1];
6781 	}
6782 	return NULL;
6783 }
6784 
6785 /**
6786  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6787  * @phba: Pointer to lpfc_hba data structure.
6788  * @buff: Buffer containing config region 23 data.
6789  * @size: Size of the data buffer.
6790  *
6791  * This function parses the FCoE config parameters in config region 23 and
6792  * populate driver data structure with the parameters.
6793  */
6794 void
6795 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6796 		uint8_t *buff,
6797 		uint32_t size)
6798 {
6799 	uint32_t offset = 0;
6800 	uint8_t *rec_ptr;
6801 
6802 	/*
6803 	 * If data size is less than 2 words signature and version cannot be
6804 	 * verified.
6805 	 */
6806 	if (size < 2*sizeof(uint32_t))
6807 		return;
6808 
6809 	/* Check the region signature first */
6810 	if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6811 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6812 			"2567 Config region 23 has bad signature\n");
6813 		return;
6814 	}
6815 
6816 	offset += 4;
6817 
6818 	/* Check the data structure version */
6819 	if (buff[offset] != LPFC_REGION23_VERSION) {
6820 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6821 			"2568 Config region 23 has bad version\n");
6822 		return;
6823 	}
6824 	offset += 4;
6825 
6826 	/* Read FCoE param record */
6827 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6828 			size - offset, FCOE_PARAM_TYPE);
6829 	if (rec_ptr)
6830 		lpfc_read_fcoe_param(phba, rec_ptr);
6831 
6832 	/* Read FCF connection table */
6833 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6834 		size - offset, FCOE_CONN_TBL_TYPE);
6835 	if (rec_ptr)
6836 		lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6837 
6838 }
6839