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