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