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