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