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