xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_hbadisc.c (revision a09d2831)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 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/pci.h>
24 #include <linux/kthread.h>
25 #include <linux/interrupt.h>
26 
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31 
32 #include "lpfc_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_nl.h"
35 #include "lpfc_disc.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43 #include "lpfc_debugfs.h"
44 
45 /* AlpaArray for assignment of scsid for scan-down and bind_method */
46 static uint8_t lpfcAlpaArray[] = {
47 	0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
48 	0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
49 	0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
50 	0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
51 	0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
52 	0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
53 	0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
54 	0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
55 	0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
56 	0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
57 	0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
58 	0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
59 	0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
60 };
61 
62 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
63 static void lpfc_disc_flush_list(struct lpfc_vport *vport);
64 static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
65 
66 void
67 lpfc_terminate_rport_io(struct fc_rport *rport)
68 {
69 	struct lpfc_rport_data *rdata;
70 	struct lpfc_nodelist * ndlp;
71 	struct lpfc_hba *phba;
72 
73 	rdata = rport->dd_data;
74 	ndlp = rdata->pnode;
75 
76 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
77 		if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
78 			printk(KERN_ERR "Cannot find remote node"
79 			" to terminate I/O Data x%x\n",
80 			rport->port_id);
81 		return;
82 	}
83 
84 	phba  = ndlp->phba;
85 
86 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
87 		"rport terminate: sid:x%x did:x%x flg:x%x",
88 		ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
89 
90 	if (ndlp->nlp_sid != NLP_NO_SID) {
91 		lpfc_sli_abort_iocb(ndlp->vport,
92 			&phba->sli.ring[phba->sli.fcp_ring],
93 			ndlp->nlp_sid, 0, LPFC_CTX_TGT);
94 	}
95 }
96 
97 /*
98  * This function will be called when dev_loss_tmo fire.
99  */
100 void
101 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
102 {
103 	struct lpfc_rport_data *rdata;
104 	struct lpfc_nodelist * ndlp;
105 	struct lpfc_vport *vport;
106 	struct lpfc_hba   *phba;
107 	struct lpfc_work_evt *evtp;
108 	int  put_node;
109 	int  put_rport;
110 
111 	rdata = rport->dd_data;
112 	ndlp = rdata->pnode;
113 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
114 		return;
115 
116 	vport = ndlp->vport;
117 	phba  = vport->phba;
118 
119 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
120 		"rport devlosscb: sid:x%x did:x%x flg:x%x",
121 		ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
122 
123 	/* Don't defer this if we are in the process of deleting the vport
124 	 * or unloading the driver. The unload will cleanup the node
125 	 * appropriately we just need to cleanup the ndlp rport info here.
126 	 */
127 	if (vport->load_flag & FC_UNLOADING) {
128 		put_node = rdata->pnode != NULL;
129 		put_rport = ndlp->rport != NULL;
130 		rdata->pnode = NULL;
131 		ndlp->rport = NULL;
132 		if (put_node)
133 			lpfc_nlp_put(ndlp);
134 		if (put_rport)
135 			put_device(&rport->dev);
136 		return;
137 	}
138 
139 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
140 		return;
141 
142 	evtp = &ndlp->dev_loss_evt;
143 
144 	if (!list_empty(&evtp->evt_listp))
145 		return;
146 
147 	spin_lock_irq(&phba->hbalock);
148 	/* We need to hold the node by incrementing the reference
149 	 * count until this queued work is done
150 	 */
151 	evtp->evt_arg1  = lpfc_nlp_get(ndlp);
152 	if (evtp->evt_arg1) {
153 		evtp->evt = LPFC_EVT_DEV_LOSS;
154 		list_add_tail(&evtp->evt_listp, &phba->work_list);
155 		lpfc_worker_wake_up(phba);
156 	}
157 	spin_unlock_irq(&phba->hbalock);
158 
159 	return;
160 }
161 
162 /*
163  * This function is called from the worker thread when dev_loss_tmo
164  * expire.
165  */
166 static void
167 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
168 {
169 	struct lpfc_rport_data *rdata;
170 	struct fc_rport   *rport;
171 	struct lpfc_vport *vport;
172 	struct lpfc_hba   *phba;
173 	uint8_t *name;
174 	int  put_node;
175 	int  put_rport;
176 	int warn_on = 0;
177 
178 	rport = ndlp->rport;
179 
180 	if (!rport)
181 		return;
182 
183 	rdata = rport->dd_data;
184 	name = (uint8_t *) &ndlp->nlp_portname;
185 	vport = ndlp->vport;
186 	phba  = vport->phba;
187 
188 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
189 		"rport devlosstmo:did:x%x type:x%x id:x%x",
190 		ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
191 
192 	/* Don't defer this if we are in the process of deleting the vport
193 	 * or unloading the driver. The unload will cleanup the node
194 	 * appropriately we just need to cleanup the ndlp rport info here.
195 	 */
196 	if (vport->load_flag & FC_UNLOADING) {
197 		if (ndlp->nlp_sid != NLP_NO_SID) {
198 			/* flush the target */
199 			lpfc_sli_abort_iocb(vport,
200 					&phba->sli.ring[phba->sli.fcp_ring],
201 					ndlp->nlp_sid, 0, LPFC_CTX_TGT);
202 		}
203 		put_node = rdata->pnode != NULL;
204 		put_rport = ndlp->rport != NULL;
205 		rdata->pnode = NULL;
206 		ndlp->rport = NULL;
207 		if (put_node)
208 			lpfc_nlp_put(ndlp);
209 		if (put_rport)
210 			put_device(&rport->dev);
211 		return;
212 	}
213 
214 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
215 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
216 				 "0284 Devloss timeout Ignored on "
217 				 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
218 				 "NPort x%x\n",
219 				 *name, *(name+1), *(name+2), *(name+3),
220 				 *(name+4), *(name+5), *(name+6), *(name+7),
221 				 ndlp->nlp_DID);
222 		return;
223 	}
224 
225 	if (ndlp->nlp_type & NLP_FABRIC) {
226 		/* We will clean up these Nodes in linkup */
227 		put_node = rdata->pnode != NULL;
228 		put_rport = ndlp->rport != NULL;
229 		rdata->pnode = NULL;
230 		ndlp->rport = NULL;
231 		if (put_node)
232 			lpfc_nlp_put(ndlp);
233 		if (put_rport)
234 			put_device(&rport->dev);
235 		return;
236 	}
237 
238 	if (ndlp->nlp_sid != NLP_NO_SID) {
239 		warn_on = 1;
240 		/* flush the target */
241 		lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
242 				    ndlp->nlp_sid, 0, LPFC_CTX_TGT);
243 	}
244 
245 	if (warn_on) {
246 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
247 				 "0203 Devloss timeout on "
248 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
249 				 "NPort x%06x Data: x%x x%x x%x\n",
250 				 *name, *(name+1), *(name+2), *(name+3),
251 				 *(name+4), *(name+5), *(name+6), *(name+7),
252 				 ndlp->nlp_DID, ndlp->nlp_flag,
253 				 ndlp->nlp_state, ndlp->nlp_rpi);
254 	} else {
255 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
256 				 "0204 Devloss timeout on "
257 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
258 				 "NPort x%06x Data: x%x x%x x%x\n",
259 				 *name, *(name+1), *(name+2), *(name+3),
260 				 *(name+4), *(name+5), *(name+6), *(name+7),
261 				 ndlp->nlp_DID, ndlp->nlp_flag,
262 				 ndlp->nlp_state, ndlp->nlp_rpi);
263 	}
264 
265 	put_node = rdata->pnode != NULL;
266 	put_rport = ndlp->rport != NULL;
267 	rdata->pnode = NULL;
268 	ndlp->rport = NULL;
269 	if (put_node)
270 		lpfc_nlp_put(ndlp);
271 	if (put_rport)
272 		put_device(&rport->dev);
273 
274 	if (!(vport->load_flag & FC_UNLOADING) &&
275 	    !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
276 	    !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
277 	    (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
278 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
279 
280 	lpfc_unregister_unused_fcf(phba);
281 }
282 
283 /**
284  * lpfc_alloc_fast_evt - Allocates data structure for posting event
285  * @phba: Pointer to hba context object.
286  *
287  * This function is called from the functions which need to post
288  * events from interrupt context. This function allocates data
289  * structure required for posting event. It also keeps track of
290  * number of events pending and prevent event storm when there are
291  * too many events.
292  **/
293 struct lpfc_fast_path_event *
294 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
295 	struct lpfc_fast_path_event *ret;
296 
297 	/* If there are lot of fast event do not exhaust memory due to this */
298 	if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
299 		return NULL;
300 
301 	ret = kzalloc(sizeof(struct lpfc_fast_path_event),
302 			GFP_ATOMIC);
303 	if (ret) {
304 		atomic_inc(&phba->fast_event_count);
305 		INIT_LIST_HEAD(&ret->work_evt.evt_listp);
306 		ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
307 	}
308 	return ret;
309 }
310 
311 /**
312  * lpfc_free_fast_evt - Frees event data structure
313  * @phba: Pointer to hba context object.
314  * @evt:  Event object which need to be freed.
315  *
316  * This function frees the data structure required for posting
317  * events.
318  **/
319 void
320 lpfc_free_fast_evt(struct lpfc_hba *phba,
321 		struct lpfc_fast_path_event *evt) {
322 
323 	atomic_dec(&phba->fast_event_count);
324 	kfree(evt);
325 }
326 
327 /**
328  * lpfc_send_fastpath_evt - Posts events generated from fast path
329  * @phba: Pointer to hba context object.
330  * @evtp: Event data structure.
331  *
332  * This function is called from worker thread, when the interrupt
333  * context need to post an event. This function posts the event
334  * to fc transport netlink interface.
335  **/
336 static void
337 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
338 		struct lpfc_work_evt *evtp)
339 {
340 	unsigned long evt_category, evt_sub_category;
341 	struct lpfc_fast_path_event *fast_evt_data;
342 	char *evt_data;
343 	uint32_t evt_data_size;
344 	struct Scsi_Host *shost;
345 
346 	fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
347 		work_evt);
348 
349 	evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
350 	evt_sub_category = (unsigned long) fast_evt_data->un.
351 			fabric_evt.subcategory;
352 	shost = lpfc_shost_from_vport(fast_evt_data->vport);
353 	if (evt_category == FC_REG_FABRIC_EVENT) {
354 		if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
355 			evt_data = (char *) &fast_evt_data->un.read_check_error;
356 			evt_data_size = sizeof(fast_evt_data->un.
357 				read_check_error);
358 		} else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
359 			(evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
360 			evt_data = (char *) &fast_evt_data->un.fabric_evt;
361 			evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
362 		} else {
363 			lpfc_free_fast_evt(phba, fast_evt_data);
364 			return;
365 		}
366 	} else if (evt_category == FC_REG_SCSI_EVENT) {
367 		switch (evt_sub_category) {
368 		case LPFC_EVENT_QFULL:
369 		case LPFC_EVENT_DEVBSY:
370 			evt_data = (char *) &fast_evt_data->un.scsi_evt;
371 			evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
372 			break;
373 		case LPFC_EVENT_CHECK_COND:
374 			evt_data = (char *) &fast_evt_data->un.check_cond_evt;
375 			evt_data_size =  sizeof(fast_evt_data->un.
376 				check_cond_evt);
377 			break;
378 		case LPFC_EVENT_VARQUEDEPTH:
379 			evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
380 			evt_data_size = sizeof(fast_evt_data->un.
381 				queue_depth_evt);
382 			break;
383 		default:
384 			lpfc_free_fast_evt(phba, fast_evt_data);
385 			return;
386 		}
387 	} else {
388 		lpfc_free_fast_evt(phba, fast_evt_data);
389 		return;
390 	}
391 
392 	fc_host_post_vendor_event(shost,
393 		fc_get_event_number(),
394 		evt_data_size,
395 		evt_data,
396 		LPFC_NL_VENDOR_ID);
397 
398 	lpfc_free_fast_evt(phba, fast_evt_data);
399 	return;
400 }
401 
402 static void
403 lpfc_work_list_done(struct lpfc_hba *phba)
404 {
405 	struct lpfc_work_evt  *evtp = NULL;
406 	struct lpfc_nodelist  *ndlp;
407 	int free_evt;
408 
409 	spin_lock_irq(&phba->hbalock);
410 	while (!list_empty(&phba->work_list)) {
411 		list_remove_head((&phba->work_list), evtp, typeof(*evtp),
412 				 evt_listp);
413 		spin_unlock_irq(&phba->hbalock);
414 		free_evt = 1;
415 		switch (evtp->evt) {
416 		case LPFC_EVT_ELS_RETRY:
417 			ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
418 			lpfc_els_retry_delay_handler(ndlp);
419 			free_evt = 0; /* evt is part of ndlp */
420 			/* decrement the node reference count held
421 			 * for this queued work
422 			 */
423 			lpfc_nlp_put(ndlp);
424 			break;
425 		case LPFC_EVT_DEV_LOSS:
426 			ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
427 			lpfc_dev_loss_tmo_handler(ndlp);
428 			free_evt = 0;
429 			/* decrement the node reference count held for
430 			 * this queued work
431 			 */
432 			lpfc_nlp_put(ndlp);
433 			break;
434 		case LPFC_EVT_ONLINE:
435 			if (phba->link_state < LPFC_LINK_DOWN)
436 				*(int *) (evtp->evt_arg1) = lpfc_online(phba);
437 			else
438 				*(int *) (evtp->evt_arg1) = 0;
439 			complete((struct completion *)(evtp->evt_arg2));
440 			break;
441 		case LPFC_EVT_OFFLINE_PREP:
442 			if (phba->link_state >= LPFC_LINK_DOWN)
443 				lpfc_offline_prep(phba);
444 			*(int *)(evtp->evt_arg1) = 0;
445 			complete((struct completion *)(evtp->evt_arg2));
446 			break;
447 		case LPFC_EVT_OFFLINE:
448 			lpfc_offline(phba);
449 			lpfc_sli_brdrestart(phba);
450 			*(int *)(evtp->evt_arg1) =
451 				lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
452 			lpfc_unblock_mgmt_io(phba);
453 			complete((struct completion *)(evtp->evt_arg2));
454 			break;
455 		case LPFC_EVT_WARM_START:
456 			lpfc_offline(phba);
457 			lpfc_reset_barrier(phba);
458 			lpfc_sli_brdreset(phba);
459 			lpfc_hba_down_post(phba);
460 			*(int *)(evtp->evt_arg1) =
461 				lpfc_sli_brdready(phba, HS_MBRDY);
462 			lpfc_unblock_mgmt_io(phba);
463 			complete((struct completion *)(evtp->evt_arg2));
464 			break;
465 		case LPFC_EVT_KILL:
466 			lpfc_offline(phba);
467 			*(int *)(evtp->evt_arg1)
468 				= (phba->pport->stopped)
469 				        ? 0 : lpfc_sli_brdkill(phba);
470 			lpfc_unblock_mgmt_io(phba);
471 			complete((struct completion *)(evtp->evt_arg2));
472 			break;
473 		case LPFC_EVT_FASTPATH_MGMT_EVT:
474 			lpfc_send_fastpath_evt(phba, evtp);
475 			free_evt = 0;
476 			break;
477 		}
478 		if (free_evt)
479 			kfree(evtp);
480 		spin_lock_irq(&phba->hbalock);
481 	}
482 	spin_unlock_irq(&phba->hbalock);
483 
484 }
485 
486 static void
487 lpfc_work_done(struct lpfc_hba *phba)
488 {
489 	struct lpfc_sli_ring *pring;
490 	uint32_t ha_copy, status, control, work_port_events;
491 	struct lpfc_vport **vports;
492 	struct lpfc_vport *vport;
493 	int i;
494 
495 	spin_lock_irq(&phba->hbalock);
496 	ha_copy = phba->work_ha;
497 	phba->work_ha = 0;
498 	spin_unlock_irq(&phba->hbalock);
499 
500 	/* First, try to post the next mailbox command to SLI4 device */
501 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
502 		lpfc_sli4_post_async_mbox(phba);
503 
504 	if (ha_copy & HA_ERATT)
505 		/* Handle the error attention event */
506 		lpfc_handle_eratt(phba);
507 
508 	if (ha_copy & HA_MBATT)
509 		lpfc_sli_handle_mb_event(phba);
510 
511 	if (ha_copy & HA_LATT)
512 		lpfc_handle_latt(phba);
513 
514 	/* Process SLI4 events */
515 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
516 		if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
517 			lpfc_sli4_fcp_xri_abort_event_proc(phba);
518 		if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
519 			lpfc_sli4_els_xri_abort_event_proc(phba);
520 		if (phba->hba_flag & ASYNC_EVENT)
521 			lpfc_sli4_async_event_proc(phba);
522 		if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
523 			spin_lock_irq(&phba->hbalock);
524 			phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
525 			spin_unlock_irq(&phba->hbalock);
526 			lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
527 		}
528 	}
529 
530 	vports = lpfc_create_vport_work_array(phba);
531 	if (vports != NULL)
532 		for (i = 0; i <= phba->max_vports; i++) {
533 			/*
534 			 * We could have no vports in array if unloading, so if
535 			 * this happens then just use the pport
536 			 */
537 			if (vports[i] == NULL && i == 0)
538 				vport = phba->pport;
539 			else
540 				vport = vports[i];
541 			if (vport == NULL)
542 				break;
543 			spin_lock_irq(&vport->work_port_lock);
544 			work_port_events = vport->work_port_events;
545 			vport->work_port_events &= ~work_port_events;
546 			spin_unlock_irq(&vport->work_port_lock);
547 			if (work_port_events & WORKER_DISC_TMO)
548 				lpfc_disc_timeout_handler(vport);
549 			if (work_port_events & WORKER_ELS_TMO)
550 				lpfc_els_timeout_handler(vport);
551 			if (work_port_events & WORKER_HB_TMO)
552 				lpfc_hb_timeout_handler(phba);
553 			if (work_port_events & WORKER_MBOX_TMO)
554 				lpfc_mbox_timeout_handler(phba);
555 			if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
556 				lpfc_unblock_fabric_iocbs(phba);
557 			if (work_port_events & WORKER_FDMI_TMO)
558 				lpfc_fdmi_timeout_handler(vport);
559 			if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
560 				lpfc_ramp_down_queue_handler(phba);
561 			if (work_port_events & WORKER_RAMP_UP_QUEUE)
562 				lpfc_ramp_up_queue_handler(phba);
563 		}
564 	lpfc_destroy_vport_work_array(phba, vports);
565 
566 	pring = &phba->sli.ring[LPFC_ELS_RING];
567 	status = (ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
568 	status >>= (4*LPFC_ELS_RING);
569 	if ((status & HA_RXMASK) ||
570 	    (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
571 	    (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
572 		if (pring->flag & LPFC_STOP_IOCB_EVENT) {
573 			pring->flag |= LPFC_DEFERRED_RING_EVENT;
574 			/* Set the lpfc data pending flag */
575 			set_bit(LPFC_DATA_READY, &phba->data_flags);
576 		} else {
577 			pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
578 			lpfc_sli_handle_slow_ring_event(phba, pring,
579 							(status &
580 							 HA_RXMASK));
581 		}
582 		/*
583 		 * Turn on Ring interrupts
584 		 */
585 		if (phba->sli_rev <= LPFC_SLI_REV3) {
586 			spin_lock_irq(&phba->hbalock);
587 			control = readl(phba->HCregaddr);
588 			if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
589 				lpfc_debugfs_slow_ring_trc(phba,
590 					"WRK Enable ring: cntl:x%x hacopy:x%x",
591 					control, ha_copy, 0);
592 
593 				control |= (HC_R0INT_ENA << LPFC_ELS_RING);
594 				writel(control, phba->HCregaddr);
595 				readl(phba->HCregaddr); /* flush */
596 			} else {
597 				lpfc_debugfs_slow_ring_trc(phba,
598 					"WRK Ring ok:     cntl:x%x hacopy:x%x",
599 					control, ha_copy, 0);
600 			}
601 			spin_unlock_irq(&phba->hbalock);
602 		}
603 	}
604 	lpfc_work_list_done(phba);
605 }
606 
607 int
608 lpfc_do_work(void *p)
609 {
610 	struct lpfc_hba *phba = p;
611 	int rc;
612 
613 	set_user_nice(current, -20);
614 	phba->data_flags = 0;
615 
616 	while (!kthread_should_stop()) {
617 		/* wait and check worker queue activities */
618 		rc = wait_event_interruptible(phba->work_waitq,
619 					(test_and_clear_bit(LPFC_DATA_READY,
620 							    &phba->data_flags)
621 					 || kthread_should_stop()));
622 		/* Signal wakeup shall terminate the worker thread */
623 		if (rc) {
624 			lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
625 					"0433 Wakeup on signal: rc=x%x\n", rc);
626 			break;
627 		}
628 
629 		/* Attend pending lpfc data processing */
630 		lpfc_work_done(phba);
631 	}
632 	phba->worker_thread = NULL;
633 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
634 			"0432 Worker thread stopped.\n");
635 	return 0;
636 }
637 
638 /*
639  * This is only called to handle FC worker events. Since this a rare
640  * occurance, we allocate a struct lpfc_work_evt structure here instead of
641  * embedding it in the IOCB.
642  */
643 int
644 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
645 		      uint32_t evt)
646 {
647 	struct lpfc_work_evt  *evtp;
648 	unsigned long flags;
649 
650 	/*
651 	 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
652 	 * be queued to worker thread for processing
653 	 */
654 	evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
655 	if (!evtp)
656 		return 0;
657 
658 	evtp->evt_arg1  = arg1;
659 	evtp->evt_arg2  = arg2;
660 	evtp->evt       = evt;
661 
662 	spin_lock_irqsave(&phba->hbalock, flags);
663 	list_add_tail(&evtp->evt_listp, &phba->work_list);
664 	spin_unlock_irqrestore(&phba->hbalock, flags);
665 
666 	lpfc_worker_wake_up(phba);
667 
668 	return 1;
669 }
670 
671 void
672 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
673 {
674 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
675 	struct lpfc_hba  *phba = vport->phba;
676 	struct lpfc_nodelist *ndlp, *next_ndlp;
677 	int  rc;
678 
679 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
680 		if (!NLP_CHK_NODE_ACT(ndlp))
681 			continue;
682 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
683 			continue;
684 		if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
685 			((vport->port_type == LPFC_NPIV_PORT) &&
686 			(ndlp->nlp_DID == NameServer_DID)))
687 			lpfc_unreg_rpi(vport, ndlp);
688 
689 		/* Leave Fabric nodes alone on link down */
690 		if ((phba->sli_rev < LPFC_SLI_REV4) &&
691 		    (!remove && ndlp->nlp_type & NLP_FABRIC))
692 			continue;
693 		rc = lpfc_disc_state_machine(vport, ndlp, NULL,
694 					     remove
695 					     ? NLP_EVT_DEVICE_RM
696 					     : NLP_EVT_DEVICE_RECOVERY);
697 	}
698 	if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
699 		lpfc_mbx_unreg_vpi(vport);
700 		spin_lock_irq(shost->host_lock);
701 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
702 		spin_unlock_irq(shost->host_lock);
703 	}
704 }
705 
706 void
707 lpfc_port_link_failure(struct lpfc_vport *vport)
708 {
709 	/* Cleanup any outstanding received buffers */
710 	lpfc_cleanup_rcv_buffers(vport);
711 
712 	/* Cleanup any outstanding RSCN activity */
713 	lpfc_els_flush_rscn(vport);
714 
715 	/* Cleanup any outstanding ELS commands */
716 	lpfc_els_flush_cmd(vport);
717 
718 	lpfc_cleanup_rpis(vport, 0);
719 
720 	/* Turn off discovery timer if its running */
721 	lpfc_can_disctmo(vport);
722 }
723 
724 void
725 lpfc_linkdown_port(struct lpfc_vport *vport)
726 {
727 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
728 
729 	fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
730 
731 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
732 		"Link Down:       state:x%x rtry:x%x flg:x%x",
733 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
734 
735 	lpfc_port_link_failure(vport);
736 
737 }
738 
739 int
740 lpfc_linkdown(struct lpfc_hba *phba)
741 {
742 	struct lpfc_vport *vport = phba->pport;
743 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
744 	struct lpfc_vport **vports;
745 	LPFC_MBOXQ_t          *mb;
746 	int i;
747 
748 	if (phba->link_state == LPFC_LINK_DOWN)
749 		return 0;
750 
751 	/* Block all SCSI stack I/Os */
752 	lpfc_scsi_dev_block(phba);
753 
754 	spin_lock_irq(&phba->hbalock);
755 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
756 	if (phba->link_state > LPFC_LINK_DOWN) {
757 		phba->link_state = LPFC_LINK_DOWN;
758 		phba->pport->fc_flag &= ~FC_LBIT;
759 	}
760 	spin_unlock_irq(&phba->hbalock);
761 	vports = lpfc_create_vport_work_array(phba);
762 	if (vports != NULL)
763 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
764 			/* Issue a LINK DOWN event to all nodes */
765 			lpfc_linkdown_port(vports[i]);
766 		}
767 	lpfc_destroy_vport_work_array(phba, vports);
768 	/* Clean up any firmware default rpi's */
769 	mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
770 	if (mb) {
771 		lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
772 		mb->vport = vport;
773 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
774 		if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
775 		    == MBX_NOT_FINISHED) {
776 			mempool_free(mb, phba->mbox_mem_pool);
777 		}
778 	}
779 
780 	/* Setup myDID for link up if we are in pt2pt mode */
781 	if (phba->pport->fc_flag & FC_PT2PT) {
782 		phba->pport->fc_myDID = 0;
783 		mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
784 		if (mb) {
785 			lpfc_config_link(phba, mb);
786 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
787 			mb->vport = vport;
788 			if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
789 			    == MBX_NOT_FINISHED) {
790 				mempool_free(mb, phba->mbox_mem_pool);
791 			}
792 		}
793 		spin_lock_irq(shost->host_lock);
794 		phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
795 		spin_unlock_irq(shost->host_lock);
796 	}
797 
798 	return 0;
799 }
800 
801 static void
802 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
803 {
804 	struct lpfc_nodelist *ndlp;
805 
806 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
807 		if (!NLP_CHK_NODE_ACT(ndlp))
808 			continue;
809 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
810 			continue;
811 		if (ndlp->nlp_type & NLP_FABRIC) {
812 			/* On Linkup its safe to clean up the ndlp
813 			 * from Fabric connections.
814 			 */
815 			if (ndlp->nlp_DID != Fabric_DID)
816 				lpfc_unreg_rpi(vport, ndlp);
817 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
818 		} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
819 			/* Fail outstanding IO now since device is
820 			 * marked for PLOGI.
821 			 */
822 			lpfc_unreg_rpi(vport, ndlp);
823 		}
824 	}
825 }
826 
827 static void
828 lpfc_linkup_port(struct lpfc_vport *vport)
829 {
830 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
831 	struct lpfc_hba  *phba = vport->phba;
832 
833 	if ((vport->load_flag & FC_UNLOADING) != 0)
834 		return;
835 
836 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
837 		"Link Up:         top:x%x speed:x%x flg:x%x",
838 		phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
839 
840 	/* If NPIV is not enabled, only bring the physical port up */
841 	if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
842 		(vport != phba->pport))
843 		return;
844 
845 	fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
846 
847 	spin_lock_irq(shost->host_lock);
848 	vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
849 			    FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
850 	vport->fc_flag |= FC_NDISC_ACTIVE;
851 	vport->fc_ns_retry = 0;
852 	spin_unlock_irq(shost->host_lock);
853 
854 	if (vport->fc_flag & FC_LBIT)
855 		lpfc_linkup_cleanup_nodes(vport);
856 
857 }
858 
859 static int
860 lpfc_linkup(struct lpfc_hba *phba)
861 {
862 	struct lpfc_vport **vports;
863 	int i;
864 
865 	phba->link_state = LPFC_LINK_UP;
866 
867 	/* Unblock fabric iocbs if they are blocked */
868 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
869 	del_timer_sync(&phba->fabric_block_timer);
870 
871 	vports = lpfc_create_vport_work_array(phba);
872 	if (vports != NULL)
873 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
874 			lpfc_linkup_port(vports[i]);
875 	lpfc_destroy_vport_work_array(phba, vports);
876 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
877 	    (phba->sli_rev < LPFC_SLI_REV4))
878 		lpfc_issue_clear_la(phba, phba->pport);
879 
880 	return 0;
881 }
882 
883 /*
884  * This routine handles processing a CLEAR_LA mailbox
885  * command upon completion. It is setup in the LPFC_MBOXQ
886  * as the completion routine when the command is
887  * handed off to the SLI layer.
888  */
889 static void
890 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
891 {
892 	struct lpfc_vport *vport = pmb->vport;
893 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
894 	struct lpfc_sli   *psli = &phba->sli;
895 	MAILBOX_t *mb = &pmb->u.mb;
896 	uint32_t control;
897 
898 	/* Since we don't do discovery right now, turn these off here */
899 	psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
900 	psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
901 	psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
902 
903 	/* Check for error */
904 	if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
905 		/* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
906 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
907 				 "0320 CLEAR_LA mbxStatus error x%x hba "
908 				 "state x%x\n",
909 				 mb->mbxStatus, vport->port_state);
910 		phba->link_state = LPFC_HBA_ERROR;
911 		goto out;
912 	}
913 
914 	if (vport->port_type == LPFC_PHYSICAL_PORT)
915 		phba->link_state = LPFC_HBA_READY;
916 
917 	spin_lock_irq(&phba->hbalock);
918 	psli->sli_flag |= LPFC_PROCESS_LA;
919 	control = readl(phba->HCregaddr);
920 	control |= HC_LAINT_ENA;
921 	writel(control, phba->HCregaddr);
922 	readl(phba->HCregaddr); /* flush */
923 	spin_unlock_irq(&phba->hbalock);
924 	mempool_free(pmb, phba->mbox_mem_pool);
925 	return;
926 
927 out:
928 	/* Device Discovery completes */
929 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
930 			 "0225 Device Discovery completes\n");
931 	mempool_free(pmb, phba->mbox_mem_pool);
932 
933 	spin_lock_irq(shost->host_lock);
934 	vport->fc_flag &= ~FC_ABORT_DISCOVERY;
935 	spin_unlock_irq(shost->host_lock);
936 
937 	lpfc_can_disctmo(vport);
938 
939 	/* turn on Link Attention interrupts */
940 
941 	spin_lock_irq(&phba->hbalock);
942 	psli->sli_flag |= LPFC_PROCESS_LA;
943 	control = readl(phba->HCregaddr);
944 	control |= HC_LAINT_ENA;
945 	writel(control, phba->HCregaddr);
946 	readl(phba->HCregaddr); /* flush */
947 	spin_unlock_irq(&phba->hbalock);
948 
949 	return;
950 }
951 
952 
953 static void
954 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
955 {
956 	struct lpfc_vport *vport = pmb->vport;
957 
958 	if (pmb->u.mb.mbxStatus)
959 		goto out;
960 
961 	mempool_free(pmb, phba->mbox_mem_pool);
962 
963 	if (phba->fc_topology == TOPOLOGY_LOOP &&
964 	    vport->fc_flag & FC_PUBLIC_LOOP &&
965 	    !(vport->fc_flag & FC_LBIT)) {
966 			/* Need to wait for FAN - use discovery timer
967 			 * for timeout.  port_state is identically
968 			 * LPFC_LOCAL_CFG_LINK while waiting for FAN
969 			 */
970 			lpfc_set_disctmo(vport);
971 			return;
972 	}
973 
974 	/* Start discovery by sending a FLOGI. port_state is identically
975 	 * LPFC_FLOGI while waiting for FLOGI cmpl
976 	 */
977 	if (vport->port_state != LPFC_FLOGI) {
978 		lpfc_initial_flogi(vport);
979 	}
980 	return;
981 
982 out:
983 	lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
984 			 "0306 CONFIG_LINK mbxStatus error x%x "
985 			 "HBA state x%x\n",
986 			 pmb->u.mb.mbxStatus, vport->port_state);
987 	mempool_free(pmb, phba->mbox_mem_pool);
988 
989 	lpfc_linkdown(phba);
990 
991 	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
992 			 "0200 CONFIG_LINK bad hba state x%x\n",
993 			 vport->port_state);
994 
995 	lpfc_issue_clear_la(phba, vport);
996 	return;
997 }
998 
999 static void
1000 lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1001 {
1002 	struct lpfc_vport *vport = mboxq->vport;
1003 	unsigned long flags;
1004 
1005 	if (mboxq->u.mb.mbxStatus) {
1006 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1007 			 "2017 REG_FCFI mbxStatus error x%x "
1008 			 "HBA state x%x\n",
1009 			 mboxq->u.mb.mbxStatus, vport->port_state);
1010 		mempool_free(mboxq, phba->mbox_mem_pool);
1011 		return;
1012 	}
1013 
1014 	/* Start FCoE discovery by sending a FLOGI. */
1015 	phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1016 	/* Set the FCFI registered flag */
1017 	spin_lock_irqsave(&phba->hbalock, flags);
1018 	phba->fcf.fcf_flag |= FCF_REGISTERED;
1019 	spin_unlock_irqrestore(&phba->hbalock, flags);
1020 	/* If there is a pending FCoE event, restart FCF table scan. */
1021 	if (lpfc_check_pending_fcoe_event(phba, 1)) {
1022 		mempool_free(mboxq, phba->mbox_mem_pool);
1023 		return;
1024 	}
1025 	spin_lock_irqsave(&phba->hbalock, flags);
1026 	phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1027 	phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1028 	spin_unlock_irqrestore(&phba->hbalock, flags);
1029 	if (vport->port_state != LPFC_FLOGI)
1030 		lpfc_initial_flogi(vport);
1031 
1032 	mempool_free(mboxq, phba->mbox_mem_pool);
1033 	return;
1034 }
1035 
1036 /**
1037  * lpfc_fab_name_match - Check if the fcf fabric name match.
1038  * @fab_name: pointer to fabric name.
1039  * @new_fcf_record: pointer to fcf record.
1040  *
1041  * This routine compare the fcf record's fabric name with provided
1042  * fabric name. If the fabric name are identical this function
1043  * returns 1 else return 0.
1044  **/
1045 static uint32_t
1046 lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1047 {
1048 	if ((fab_name[0] ==
1049 		bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
1050 	    (fab_name[1] ==
1051 		bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
1052 	    (fab_name[2] ==
1053 		bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
1054 	    (fab_name[3] ==
1055 		bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
1056 	    (fab_name[4] ==
1057 		bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
1058 	    (fab_name[5] ==
1059 		bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
1060 	    (fab_name[6] ==
1061 		bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
1062 	    (fab_name[7] ==
1063 		bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
1064 		return 1;
1065 	else
1066 		return 0;
1067 }
1068 
1069 /**
1070  * lpfc_sw_name_match - Check if the fcf switch name match.
1071  * @fab_name: pointer to fabric name.
1072  * @new_fcf_record: pointer to fcf record.
1073  *
1074  * This routine compare the fcf record's switch name with provided
1075  * switch name. If the switch name are identical this function
1076  * returns 1 else return 0.
1077  **/
1078 static uint32_t
1079 lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1080 {
1081 	if ((sw_name[0] ==
1082 		bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record)) &&
1083 	    (sw_name[1] ==
1084 		bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record)) &&
1085 	    (sw_name[2] ==
1086 		bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record)) &&
1087 	    (sw_name[3] ==
1088 		bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record)) &&
1089 	    (sw_name[4] ==
1090 		bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record)) &&
1091 	    (sw_name[5] ==
1092 		bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record)) &&
1093 	    (sw_name[6] ==
1094 		bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record)) &&
1095 	    (sw_name[7] ==
1096 		bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record)))
1097 		return 1;
1098 	else
1099 		return 0;
1100 }
1101 
1102 /**
1103  * lpfc_mac_addr_match - Check if the fcf mac address match.
1104  * @phba: pointer to lpfc hba data structure.
1105  * @new_fcf_record: pointer to fcf record.
1106  *
1107  * This routine compare the fcf record's mac address with HBA's
1108  * FCF mac address. If the mac addresses are identical this function
1109  * returns 1 else return 0.
1110  **/
1111 static uint32_t
1112 lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1113 {
1114 	if ((phba->fcf.mac_addr[0] ==
1115 		bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
1116 	    (phba->fcf.mac_addr[1] ==
1117 		bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
1118 	    (phba->fcf.mac_addr[2] ==
1119 		bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
1120 	    (phba->fcf.mac_addr[3] ==
1121 		bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
1122 	    (phba->fcf.mac_addr[4] ==
1123 		bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
1124 	    (phba->fcf.mac_addr[5] ==
1125 		bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
1126 		return 1;
1127 	else
1128 		return 0;
1129 }
1130 
1131 /**
1132  * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1133  * @phba: pointer to lpfc hba data structure.
1134  * @new_fcf_record: pointer to fcf record.
1135  *
1136  * This routine copies the FCF information from the FCF
1137  * record to lpfc_hba data structure.
1138  **/
1139 static void
1140 lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1141 {
1142 	phba->fcf.fabric_name[0] =
1143 		bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1144 	phba->fcf.fabric_name[1] =
1145 		bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1146 	phba->fcf.fabric_name[2] =
1147 		bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1148 	phba->fcf.fabric_name[3] =
1149 		bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1150 	phba->fcf.fabric_name[4] =
1151 		bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1152 	phba->fcf.fabric_name[5] =
1153 		bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1154 	phba->fcf.fabric_name[6] =
1155 		bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1156 	phba->fcf.fabric_name[7] =
1157 		bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1158 	phba->fcf.mac_addr[0] =
1159 		bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1160 	phba->fcf.mac_addr[1] =
1161 		bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1162 	phba->fcf.mac_addr[2] =
1163 		bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1164 	phba->fcf.mac_addr[3] =
1165 		bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1166 	phba->fcf.mac_addr[4] =
1167 		bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1168 	phba->fcf.mac_addr[5] =
1169 		bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1170 	phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1171 	phba->fcf.priority = new_fcf_record->fip_priority;
1172 	phba->fcf.switch_name[0] =
1173 		bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1174 	phba->fcf.switch_name[1] =
1175 		bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1176 	phba->fcf.switch_name[2] =
1177 		bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1178 	phba->fcf.switch_name[3] =
1179 		bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1180 	phba->fcf.switch_name[4] =
1181 		bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1182 	phba->fcf.switch_name[5] =
1183 		bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1184 	phba->fcf.switch_name[6] =
1185 		bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1186 	phba->fcf.switch_name[7] =
1187 		bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
1188 }
1189 
1190 /**
1191  * lpfc_register_fcf - Register the FCF with hba.
1192  * @phba: pointer to lpfc hba data structure.
1193  *
1194  * This routine issues a register fcfi mailbox command to register
1195  * the fcf with HBA.
1196  **/
1197 static void
1198 lpfc_register_fcf(struct lpfc_hba *phba)
1199 {
1200 	LPFC_MBOXQ_t *fcf_mbxq;
1201 	int rc;
1202 	unsigned long flags;
1203 
1204 	spin_lock_irqsave(&phba->hbalock, flags);
1205 
1206 	/* If the FCF is not availabe do nothing. */
1207 	if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1208 		phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1209 		spin_unlock_irqrestore(&phba->hbalock, flags);
1210 		return;
1211 	}
1212 
1213 	/* The FCF is already registered, start discovery */
1214 	if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1215 		phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1216 		phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1217 		spin_unlock_irqrestore(&phba->hbalock, flags);
1218 		if (phba->pport->port_state != LPFC_FLOGI)
1219 			lpfc_initial_flogi(phba->pport);
1220 		return;
1221 	}
1222 	spin_unlock_irqrestore(&phba->hbalock, flags);
1223 
1224 	fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1225 		GFP_KERNEL);
1226 	if (!fcf_mbxq) {
1227 		spin_lock_irqsave(&phba->hbalock, flags);
1228 		phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1229 		spin_unlock_irqrestore(&phba->hbalock, flags);
1230 		return;
1231 	}
1232 
1233 	lpfc_reg_fcfi(phba, fcf_mbxq);
1234 	fcf_mbxq->vport = phba->pport;
1235 	fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1236 	rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1237 	if (rc == MBX_NOT_FINISHED) {
1238 		spin_lock_irqsave(&phba->hbalock, flags);
1239 		phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1240 		spin_unlock_irqrestore(&phba->hbalock, flags);
1241 		mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1242 	}
1243 
1244 	return;
1245 }
1246 
1247 /**
1248  * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1249  * @phba: pointer to lpfc hba data structure.
1250  * @new_fcf_record: pointer to fcf record.
1251  * @boot_flag: Indicates if this record used by boot bios.
1252  * @addr_mode: The address mode to be used by this FCF
1253  *
1254  * This routine compare the fcf record with connect list obtained from the
1255  * config region to decide if this FCF can be used for SAN discovery. It returns
1256  * 1 if this record can be used for SAN discovery else return zero. If this FCF
1257  * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1258  * is used by boot bios and addr_mode will indicate the addressing mode to be
1259  * used for this FCF when the function returns.
1260  * If the FCF record need to be used with a particular vlan id, the vlan is
1261  * set in the vlan_id on return of the function. If not VLAN tagging need to
1262  * be used with the FCF vlan_id will be set to 0xFFFF;
1263  **/
1264 static int
1265 lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1266 			struct fcf_record *new_fcf_record,
1267 			uint32_t *boot_flag, uint32_t *addr_mode,
1268 			uint16_t *vlan_id)
1269 {
1270 	struct lpfc_fcf_conn_entry *conn_entry;
1271 	int i, j, fcf_vlan_id = 0;
1272 
1273 	/* Find the lowest VLAN id in the FCF record */
1274 	for (i = 0; i < 512; i++) {
1275 		if (new_fcf_record->vlan_bitmap[i]) {
1276 			fcf_vlan_id = i * 8;
1277 			j = 0;
1278 			while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1279 				j++;
1280 				fcf_vlan_id++;
1281 			}
1282 			break;
1283 		}
1284 	}
1285 
1286 	/* If FCF not available return 0 */
1287 	if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1288 		!bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1289 		return 0;
1290 
1291 	if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
1292 		*boot_flag = 0;
1293 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1294 				new_fcf_record);
1295 		if (phba->valid_vlan)
1296 			*vlan_id = phba->vlan_id;
1297 		else
1298 			*vlan_id = 0xFFFF;
1299 		return 1;
1300 	}
1301 
1302 	/*
1303 	 * If there are no FCF connection table entry, driver connect to all
1304 	 * FCFs.
1305 	 */
1306 	if (list_empty(&phba->fcf_conn_rec_list)) {
1307 		*boot_flag = 0;
1308 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1309 			new_fcf_record);
1310 
1311 		/*
1312 		 * When there are no FCF connect entries, use driver's default
1313 		 * addressing mode - FPMA.
1314 		 */
1315 		if (*addr_mode & LPFC_FCF_FPMA)
1316 			*addr_mode = LPFC_FCF_FPMA;
1317 
1318 		/* If FCF record report a vlan id use that vlan id */
1319 		if (fcf_vlan_id)
1320 			*vlan_id = fcf_vlan_id;
1321 		else
1322 			*vlan_id = 0xFFFF;
1323 		return 1;
1324 	}
1325 
1326 	list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
1327 		if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1328 			continue;
1329 
1330 		if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1331 			!lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
1332 					     new_fcf_record))
1333 			continue;
1334 		if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1335 			!lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1336 					    new_fcf_record))
1337 			continue;
1338 		if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1339 			/*
1340 			 * If the vlan bit map does not have the bit set for the
1341 			 * vlan id to be used, then it is not a match.
1342 			 */
1343 			if (!(new_fcf_record->vlan_bitmap
1344 				[conn_entry->conn_rec.vlan_tag / 8] &
1345 				(1 << (conn_entry->conn_rec.vlan_tag % 8))))
1346 				continue;
1347 		}
1348 
1349 		/*
1350 		 * If connection record does not support any addressing mode,
1351 		 * skip the FCF record.
1352 		 */
1353 		if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1354 			& (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1355 			continue;
1356 
1357 		/*
1358 		 * Check if the connection record specifies a required
1359 		 * addressing mode.
1360 		 */
1361 		if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1362 			!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1363 
1364 			/*
1365 			 * If SPMA required but FCF not support this continue.
1366 			 */
1367 			if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1368 				!(bf_get(lpfc_fcf_record_mac_addr_prov,
1369 					new_fcf_record) & LPFC_FCF_SPMA))
1370 				continue;
1371 
1372 			/*
1373 			 * If FPMA required but FCF not support this continue.
1374 			 */
1375 			if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1376 				!(bf_get(lpfc_fcf_record_mac_addr_prov,
1377 				new_fcf_record) & LPFC_FCF_FPMA))
1378 				continue;
1379 		}
1380 
1381 		/*
1382 		 * This fcf record matches filtering criteria.
1383 		 */
1384 		if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1385 			*boot_flag = 1;
1386 		else
1387 			*boot_flag = 0;
1388 
1389 		/*
1390 		 * If user did not specify any addressing mode, or if the
1391 		 * prefered addressing mode specified by user is not supported
1392 		 * by FCF, allow fabric to pick the addressing mode.
1393 		 */
1394 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1395 				new_fcf_record);
1396 		/*
1397 		 * If the user specified a required address mode, assign that
1398 		 * address mode
1399 		 */
1400 		if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1401 			(!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1402 			*addr_mode = (conn_entry->conn_rec.flags &
1403 				FCFCNCT_AM_SPMA) ?
1404 				LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1405 		/*
1406 		 * If the user specified a prefered address mode, use the
1407 		 * addr mode only if FCF support the addr_mode.
1408 		 */
1409 		else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1410 			(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1411 			(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1412 			(*addr_mode & LPFC_FCF_SPMA))
1413 				*addr_mode = LPFC_FCF_SPMA;
1414 		else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1415 			(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1416 			!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1417 			(*addr_mode & LPFC_FCF_FPMA))
1418 				*addr_mode = LPFC_FCF_FPMA;
1419 
1420 		/* If matching connect list has a vlan id, use it */
1421 		if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1422 			*vlan_id = conn_entry->conn_rec.vlan_tag;
1423 		/*
1424 		 * If no vlan id is specified in connect list, use the vlan id
1425 		 * in the FCF record
1426 		 */
1427 		else if (fcf_vlan_id)
1428 			*vlan_id = fcf_vlan_id;
1429 		else
1430 			*vlan_id = 0xFFFF;
1431 
1432 		return 1;
1433 	}
1434 
1435 	return 0;
1436 }
1437 
1438 /**
1439  * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1440  * @phba: pointer to lpfc hba data structure.
1441  * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1442  *
1443  * This function check if there is any fcoe event pending while driver
1444  * scan FCF entries. If there is any pending event, it will restart the
1445  * FCF saning and return 1 else return 0.
1446  */
1447 int
1448 lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1449 {
1450 	LPFC_MBOXQ_t *mbox;
1451 	int rc;
1452 	/*
1453 	 * If the Link is up and no FCoE events while in the
1454 	 * FCF discovery, no need to restart FCF discovery.
1455 	 */
1456 	if ((phba->link_state  >= LPFC_LINK_UP) &&
1457 		(phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1458 		return 0;
1459 
1460 	spin_lock_irq(&phba->hbalock);
1461 	phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1462 	spin_unlock_irq(&phba->hbalock);
1463 
1464 	if (phba->link_state >= LPFC_LINK_UP)
1465 		lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
1466 	else {
1467 		/*
1468 		 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1469 		 * flag
1470 		 */
1471 		spin_lock_irq(&phba->hbalock);
1472 		phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1473 		spin_unlock_irq(&phba->hbalock);
1474 	}
1475 
1476 	if (unreg_fcf) {
1477 		spin_lock_irq(&phba->hbalock);
1478 		phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1479 		spin_unlock_irq(&phba->hbalock);
1480 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1481 		if (!mbox) {
1482 			lpfc_printf_log(phba, KERN_ERR,
1483 				LOG_DISCOVERY|LOG_MBOX,
1484 				"2610 UNREG_FCFI mbox allocation failed\n");
1485 			return 1;
1486 		}
1487 		lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
1488 		mbox->vport = phba->pport;
1489 		mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
1490 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1491 		if (rc == MBX_NOT_FINISHED) {
1492 			lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
1493 				"2611 UNREG_FCFI issue mbox failed\n");
1494 			mempool_free(mbox, phba->mbox_mem_pool);
1495 		}
1496 	}
1497 
1498 	return 1;
1499 }
1500 
1501 /**
1502  * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1503  * @phba: pointer to lpfc hba data structure.
1504  * @mboxq: pointer to mailbox object.
1505  *
1506  * This function iterate through all the fcf records available in
1507  * HBA and choose the optimal FCF record for discovery. After finding
1508  * the FCF for discovery it register the FCF record and kick start
1509  * discovery.
1510  * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1511  * use a FCF record which match fabric name and mac address of the
1512  * currently used FCF record.
1513  * If the driver support only one FCF, it will try to use the FCF record
1514  * used by BOOT_BIOS.
1515  */
1516 void
1517 lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1518 {
1519 	void *virt_addr;
1520 	dma_addr_t phys_addr;
1521 	uint8_t *bytep;
1522 	struct lpfc_mbx_sge sge;
1523 	struct lpfc_mbx_read_fcf_tbl *read_fcf;
1524 	uint32_t shdr_status, shdr_add_status;
1525 	union lpfc_sli4_cfg_shdr *shdr;
1526 	struct fcf_record *new_fcf_record;
1527 	int rc;
1528 	uint32_t boot_flag, addr_mode;
1529 	uint32_t next_fcf_index;
1530 	unsigned long flags;
1531 	uint16_t vlan_id;
1532 
1533 	/* If there is pending FCoE event restart FCF table scan */
1534 	if (lpfc_check_pending_fcoe_event(phba, 0)) {
1535 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
1536 		return;
1537 	}
1538 
1539 	/* Get the first SGE entry from the non-embedded DMA memory. This
1540 	 * routine only uses a single SGE.
1541 	 */
1542 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1543 	phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1544 	if (unlikely(!mboxq->sge_array)) {
1545 		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1546 				"2524 Failed to get the non-embedded SGE "
1547 				"virtual address\n");
1548 		goto out;
1549 	}
1550 	virt_addr = mboxq->sge_array->addr[0];
1551 
1552 	shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1553 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1554 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1555 				 &shdr->response);
1556 	/*
1557 	 * The FCF Record was read and there is no reason for the driver
1558 	 * to maintain the FCF record data or memory. Instead, just need
1559 	 * to book keeping the FCFIs can be used.
1560 	 */
1561 	if (shdr_status || shdr_add_status) {
1562 		if (shdr_status == STATUS_FCF_TABLE_EMPTY) {
1563 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1564 					"2726 READ_FCF_RECORD Indicates empty "
1565 					"FCF table.\n");
1566 		} else {
1567 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1568 					"2521 READ_FCF_RECORD mailbox failed "
1569 					"with status x%x add_status x%x, mbx\n",
1570 					shdr_status, shdr_add_status);
1571 		}
1572 		goto out;
1573 	}
1574 	/* Interpreting the returned information of FCF records */
1575 	read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1576 	lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1577 			      sizeof(struct lpfc_mbx_read_fcf_tbl));
1578 	next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1579 
1580 	new_fcf_record = (struct fcf_record *)(virt_addr +
1581 			  sizeof(struct lpfc_mbx_read_fcf_tbl));
1582 	lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1583 			      sizeof(struct fcf_record));
1584 	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1585 
1586 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
1587 				      &boot_flag, &addr_mode,
1588 					&vlan_id);
1589 	/*
1590 	 * If the fcf record does not match with connect list entries
1591 	 * read the next entry.
1592 	 */
1593 	if (!rc)
1594 		goto read_next_fcf;
1595 	/*
1596 	 * If this is not the first FCF discovery of the HBA, use last
1597 	 * FCF record for the discovery.
1598 	 */
1599 	spin_lock_irqsave(&phba->hbalock, flags);
1600 	if (phba->fcf.fcf_flag & FCF_IN_USE) {
1601 		if (lpfc_fab_name_match(phba->fcf.fabric_name,
1602 					new_fcf_record) &&
1603 		    lpfc_sw_name_match(phba->fcf.switch_name,
1604 					new_fcf_record) &&
1605 		    lpfc_mac_addr_match(phba, new_fcf_record)) {
1606 			phba->fcf.fcf_flag |= FCF_AVAILABLE;
1607 			spin_unlock_irqrestore(&phba->hbalock, flags);
1608 			goto out;
1609 		}
1610 		spin_unlock_irqrestore(&phba->hbalock, flags);
1611 		goto read_next_fcf;
1612 	}
1613 	if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1614 		/*
1615 		 * If the current FCF record does not have boot flag
1616 		 * set and new fcf record has boot flag set, use the
1617 		 * new fcf record.
1618 		 */
1619 		if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1620 			/* Use this FCF record */
1621 			lpfc_copy_fcf_record(phba, new_fcf_record);
1622 			phba->fcf.addr_mode = addr_mode;
1623 			phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1624 			if (vlan_id != 0xFFFF) {
1625 				phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1626 				phba->fcf.vlan_id = vlan_id;
1627 			}
1628 			spin_unlock_irqrestore(&phba->hbalock, flags);
1629 			goto read_next_fcf;
1630 		}
1631 		/*
1632 		 * If the current FCF record has boot flag set and the
1633 		 * new FCF record does not have boot flag, read the next
1634 		 * FCF record.
1635 		 */
1636 		if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1637 			spin_unlock_irqrestore(&phba->hbalock, flags);
1638 			goto read_next_fcf;
1639 		}
1640 		/*
1641 		 * If there is a record with lower priority value for
1642 		 * the current FCF, use that record.
1643 		 */
1644 		if (lpfc_fab_name_match(phba->fcf.fabric_name,
1645 					new_fcf_record) &&
1646 		    (new_fcf_record->fip_priority < phba->fcf.priority)) {
1647 			/* Use this FCF record */
1648 			lpfc_copy_fcf_record(phba, new_fcf_record);
1649 			phba->fcf.addr_mode = addr_mode;
1650 			if (vlan_id != 0xFFFF) {
1651 				phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1652 				phba->fcf.vlan_id = vlan_id;
1653 			}
1654 			spin_unlock_irqrestore(&phba->hbalock, flags);
1655 			goto read_next_fcf;
1656 		}
1657 		spin_unlock_irqrestore(&phba->hbalock, flags);
1658 		goto read_next_fcf;
1659 	}
1660 	/*
1661 	 * This is the first available FCF record, use this
1662 	 * record.
1663 	 */
1664 	lpfc_copy_fcf_record(phba, new_fcf_record);
1665 	phba->fcf.addr_mode = addr_mode;
1666 	if (boot_flag)
1667 		phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1668 	phba->fcf.fcf_flag |= FCF_AVAILABLE;
1669 	if (vlan_id != 0xFFFF) {
1670 		phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1671 		phba->fcf.vlan_id = vlan_id;
1672 	}
1673 	spin_unlock_irqrestore(&phba->hbalock, flags);
1674 	goto read_next_fcf;
1675 
1676 read_next_fcf:
1677 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
1678 	if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
1679 		lpfc_register_fcf(phba);
1680 	else
1681 		lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1682 	return;
1683 
1684 out:
1685 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
1686 	lpfc_register_fcf(phba);
1687 
1688 	return;
1689 }
1690 
1691 /**
1692  * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
1693  * @phba: pointer to lpfc hba data structure.
1694  * @mboxq: pointer to mailbox data structure.
1695  *
1696  * This function handles completion of init vpi mailbox command.
1697  */
1698 static void
1699 lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1700 {
1701 	struct lpfc_vport *vport = mboxq->vport;
1702 	if (mboxq->u.mb.mbxStatus) {
1703 		lpfc_printf_vlog(vport, KERN_ERR,
1704 				LOG_MBOX,
1705 				"2609 Init VPI mailbox failed 0x%x\n",
1706 				mboxq->u.mb.mbxStatus);
1707 		mempool_free(mboxq, phba->mbox_mem_pool);
1708 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1709 		return;
1710 	}
1711 	spin_lock_irq(&phba->hbalock);
1712 	vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
1713 	spin_unlock_irq(&phba->hbalock);
1714 
1715 	if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1716 		lpfc_initial_fdisc(vport);
1717 	else {
1718 		lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
1719 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1720 				 "2606 No NPIV Fabric support\n");
1721 	}
1722 	return;
1723 }
1724 
1725 /**
1726  * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1727  * @phba: pointer to lpfc hba data structure.
1728  *
1729  * This function loops through the list of vports on the @phba and issues an
1730  * FDISC if possible.
1731  */
1732 void
1733 lpfc_start_fdiscs(struct lpfc_hba *phba)
1734 {
1735 	struct lpfc_vport **vports;
1736 	int i;
1737 	LPFC_MBOXQ_t *mboxq;
1738 	int rc;
1739 
1740 	vports = lpfc_create_vport_work_array(phba);
1741 	if (vports != NULL) {
1742 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1743 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1744 				continue;
1745 			/* There are no vpi for this vport */
1746 			if (vports[i]->vpi > phba->max_vpi) {
1747 				lpfc_vport_set_state(vports[i],
1748 						     FC_VPORT_FAILED);
1749 				continue;
1750 			}
1751 			if (phba->fc_topology == TOPOLOGY_LOOP) {
1752 				lpfc_vport_set_state(vports[i],
1753 						     FC_VPORT_LINKDOWN);
1754 				continue;
1755 			}
1756 			if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
1757 				mboxq = mempool_alloc(phba->mbox_mem_pool,
1758 					GFP_KERNEL);
1759 				if (!mboxq) {
1760 					lpfc_printf_vlog(vports[i], KERN_ERR,
1761 					LOG_MBOX, "2607 Failed to allocate "
1762 					"init_vpi mailbox\n");
1763 					continue;
1764 				}
1765 				lpfc_init_vpi(phba, mboxq, vports[i]->vpi);
1766 				mboxq->vport = vports[i];
1767 				mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
1768 				rc = lpfc_sli_issue_mbox(phba, mboxq,
1769 					MBX_NOWAIT);
1770 				if (rc == MBX_NOT_FINISHED) {
1771 					lpfc_printf_vlog(vports[i], KERN_ERR,
1772 					LOG_MBOX, "2608 Failed to issue "
1773 					"init_vpi mailbox\n");
1774 					mempool_free(mboxq,
1775 						phba->mbox_mem_pool);
1776 				}
1777 				continue;
1778 			}
1779 			if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1780 				lpfc_initial_fdisc(vports[i]);
1781 			else {
1782 				lpfc_vport_set_state(vports[i],
1783 						     FC_VPORT_NO_FABRIC_SUPP);
1784 				lpfc_printf_vlog(vports[i], KERN_ERR,
1785 						 LOG_ELS,
1786 						 "0259 No NPIV "
1787 						 "Fabric support\n");
1788 			}
1789 		}
1790 	}
1791 	lpfc_destroy_vport_work_array(phba, vports);
1792 }
1793 
1794 void
1795 lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1796 {
1797 	struct lpfc_dmabuf *dmabuf = mboxq->context1;
1798 	struct lpfc_vport *vport = mboxq->vport;
1799 
1800 	if (mboxq->u.mb.mbxStatus) {
1801 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1802 			 "2018 REG_VFI mbxStatus error x%x "
1803 			 "HBA state x%x\n",
1804 			 mboxq->u.mb.mbxStatus, vport->port_state);
1805 		if (phba->fc_topology == TOPOLOGY_LOOP) {
1806 			/* FLOGI failed, use loop map to make discovery list */
1807 			lpfc_disc_list_loopmap(vport);
1808 			/* Start discovery */
1809 			lpfc_disc_start(vport);
1810 			goto fail_free_mem;
1811 		}
1812 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1813 		goto fail_free_mem;
1814 	}
1815 	/* The VPI is implicitly registered when the VFI is registered */
1816 	vport->vpi_state |= LPFC_VPI_REGISTERED;
1817 
1818 	if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1819 		lpfc_start_fdiscs(phba);
1820 		lpfc_do_scr_ns_plogi(phba, vport);
1821 	}
1822 
1823 fail_free_mem:
1824 	mempool_free(mboxq, phba->mbox_mem_pool);
1825 	lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1826 	kfree(dmabuf);
1827 	return;
1828 }
1829 
1830 static void
1831 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1832 {
1833 	MAILBOX_t *mb = &pmb->u.mb;
1834 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
1835 	struct lpfc_vport  *vport = pmb->vport;
1836 
1837 
1838 	/* Check for error */
1839 	if (mb->mbxStatus) {
1840 		/* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
1841 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1842 				 "0319 READ_SPARAM mbxStatus error x%x "
1843 				 "hba state x%x>\n",
1844 				 mb->mbxStatus, vport->port_state);
1845 		lpfc_linkdown(phba);
1846 		goto out;
1847 	}
1848 
1849 	memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
1850 	       sizeof (struct serv_parm));
1851 	if (phba->cfg_soft_wwnn)
1852 		u64_to_wwn(phba->cfg_soft_wwnn,
1853 			   vport->fc_sparam.nodeName.u.wwn);
1854 	if (phba->cfg_soft_wwpn)
1855 		u64_to_wwn(phba->cfg_soft_wwpn,
1856 			   vport->fc_sparam.portName.u.wwn);
1857 	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1858 	       sizeof(vport->fc_nodename));
1859 	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1860 	       sizeof(vport->fc_portname));
1861 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
1862 		memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1863 		memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1864 	}
1865 
1866 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
1867 	kfree(mp);
1868 	mempool_free(pmb, phba->mbox_mem_pool);
1869 	return;
1870 
1871 out:
1872 	pmb->context1 = NULL;
1873 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
1874 	kfree(mp);
1875 	lpfc_issue_clear_la(phba, vport);
1876 	mempool_free(pmb, phba->mbox_mem_pool);
1877 	return;
1878 }
1879 
1880 static void
1881 lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
1882 {
1883 	struct lpfc_vport *vport = phba->pport;
1884 	LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
1885 	int i;
1886 	struct lpfc_dmabuf *mp;
1887 	int rc;
1888 	struct fcf_record *fcf_record;
1889 
1890 	sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1891 
1892 	spin_lock_irq(&phba->hbalock);
1893 	switch (la->UlnkSpeed) {
1894 	case LA_1GHZ_LINK:
1895 		phba->fc_linkspeed = LA_1GHZ_LINK;
1896 		break;
1897 	case LA_2GHZ_LINK:
1898 		phba->fc_linkspeed = LA_2GHZ_LINK;
1899 		break;
1900 	case LA_4GHZ_LINK:
1901 		phba->fc_linkspeed = LA_4GHZ_LINK;
1902 		break;
1903 	case LA_8GHZ_LINK:
1904 		phba->fc_linkspeed = LA_8GHZ_LINK;
1905 		break;
1906 	case LA_10GHZ_LINK:
1907 		phba->fc_linkspeed = LA_10GHZ_LINK;
1908 		break;
1909 	default:
1910 		phba->fc_linkspeed = LA_UNKNW_LINK;
1911 		break;
1912 	}
1913 
1914 	phba->fc_topology = la->topology;
1915 	phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
1916 
1917 	if (phba->fc_topology == TOPOLOGY_LOOP) {
1918 		phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
1919 
1920 		/* if npiv is enabled and this adapter supports npiv log
1921 		 * a message that npiv is not supported in this topology
1922 		 */
1923 		if (phba->cfg_enable_npiv && phba->max_vpi)
1924 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1925 				"1309 Link Up Event npiv not supported in loop "
1926 				"topology\n");
1927 				/* Get Loop Map information */
1928 		if (la->il)
1929 			vport->fc_flag |= FC_LBIT;
1930 
1931 		vport->fc_myDID = la->granted_AL_PA;
1932 		i = la->un.lilpBde64.tus.f.bdeSize;
1933 
1934 		if (i == 0) {
1935 			phba->alpa_map[0] = 0;
1936 		} else {
1937 			if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
1938 				int numalpa, j, k;
1939 				union {
1940 					uint8_t pamap[16];
1941 					struct {
1942 						uint32_t wd1;
1943 						uint32_t wd2;
1944 						uint32_t wd3;
1945 						uint32_t wd4;
1946 					} pa;
1947 				} un;
1948 				numalpa = phba->alpa_map[0];
1949 				j = 0;
1950 				while (j < numalpa) {
1951 					memset(un.pamap, 0, 16);
1952 					for (k = 1; j < numalpa; k++) {
1953 						un.pamap[k - 1] =
1954 							phba->alpa_map[j + 1];
1955 						j++;
1956 						if (k == 16)
1957 							break;
1958 					}
1959 					/* Link Up Event ALPA map */
1960 					lpfc_printf_log(phba,
1961 							KERN_WARNING,
1962 							LOG_LINK_EVENT,
1963 							"1304 Link Up Event "
1964 							"ALPA map Data: x%x "
1965 							"x%x x%x x%x\n",
1966 							un.pa.wd1, un.pa.wd2,
1967 							un.pa.wd3, un.pa.wd4);
1968 				}
1969 			}
1970 		}
1971 	} else {
1972 		if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
1973 			if (phba->max_vpi && phba->cfg_enable_npiv &&
1974 			   (phba->sli_rev == 3))
1975 				phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1976 		}
1977 		vport->fc_myDID = phba->fc_pref_DID;
1978 		vport->fc_flag |= FC_LBIT;
1979 	}
1980 	spin_unlock_irq(&phba->hbalock);
1981 
1982 	lpfc_linkup(phba);
1983 	if (sparam_mbox) {
1984 		lpfc_read_sparam(phba, sparam_mbox, 0);
1985 		sparam_mbox->vport = vport;
1986 		sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
1987 		rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
1988 		if (rc == MBX_NOT_FINISHED) {
1989 			mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1990 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
1991 			kfree(mp);
1992 			mempool_free(sparam_mbox, phba->mbox_mem_pool);
1993 			goto out;
1994 		}
1995 	}
1996 
1997 	if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
1998 		cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1999 		if (!cfglink_mbox)
2000 			goto out;
2001 		vport->port_state = LPFC_LOCAL_CFG_LINK;
2002 		lpfc_config_link(phba, cfglink_mbox);
2003 		cfglink_mbox->vport = vport;
2004 		cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
2005 		rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
2006 		if (rc == MBX_NOT_FINISHED) {
2007 			mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2008 			goto out;
2009 		}
2010 	} else {
2011 		vport->port_state = LPFC_VPORT_UNKNOWN;
2012 		/*
2013 		 * Add the driver's default FCF record at FCF index 0 now. This
2014 		 * is phase 1 implementation that support FCF index 0 and driver
2015 		 * defaults.
2016 		 */
2017 		if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
2018 			fcf_record = kzalloc(sizeof(struct fcf_record),
2019 					GFP_KERNEL);
2020 			if (unlikely(!fcf_record)) {
2021 				lpfc_printf_log(phba, KERN_ERR,
2022 					LOG_MBOX | LOG_SLI,
2023 					"2554 Could not allocate memmory for "
2024 					"fcf record\n");
2025 				rc = -ENODEV;
2026 				goto out;
2027 			}
2028 
2029 			lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2030 						LPFC_FCOE_FCF_DEF_INDEX);
2031 			rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2032 			if (unlikely(rc)) {
2033 				lpfc_printf_log(phba, KERN_ERR,
2034 					LOG_MBOX | LOG_SLI,
2035 					"2013 Could not manually add FCF "
2036 					"record 0, status %d\n", rc);
2037 				rc = -ENODEV;
2038 				kfree(fcf_record);
2039 				goto out;
2040 			}
2041 			kfree(fcf_record);
2042 		}
2043 		/*
2044 		 * The driver is expected to do FIP/FCF. Call the port
2045 		 * and get the FCF Table.
2046 		 */
2047 		spin_lock_irq(&phba->hbalock);
2048 		if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2049 			spin_unlock_irq(&phba->hbalock);
2050 			return;
2051 		}
2052 		spin_unlock_irq(&phba->hbalock);
2053 		rc = lpfc_sli4_read_fcf_record(phba,
2054 					LPFC_FCOE_FCF_GET_FIRST);
2055 		if (rc)
2056 			goto out;
2057 	}
2058 
2059 	return;
2060 out:
2061 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2062 	lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2063 			 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2064 			 vport->port_state, sparam_mbox, cfglink_mbox);
2065 	lpfc_issue_clear_la(phba, vport);
2066 	return;
2067 }
2068 
2069 static void
2070 lpfc_enable_la(struct lpfc_hba *phba)
2071 {
2072 	uint32_t control;
2073 	struct lpfc_sli *psli = &phba->sli;
2074 	spin_lock_irq(&phba->hbalock);
2075 	psli->sli_flag |= LPFC_PROCESS_LA;
2076 	if (phba->sli_rev <= LPFC_SLI_REV3) {
2077 		control = readl(phba->HCregaddr);
2078 		control |= HC_LAINT_ENA;
2079 		writel(control, phba->HCregaddr);
2080 		readl(phba->HCregaddr); /* flush */
2081 	}
2082 	spin_unlock_irq(&phba->hbalock);
2083 }
2084 
2085 static void
2086 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2087 {
2088 	lpfc_linkdown(phba);
2089 	lpfc_enable_la(phba);
2090 	lpfc_unregister_unused_fcf(phba);
2091 	/* turn on Link Attention interrupts - no CLEAR_LA needed */
2092 }
2093 
2094 
2095 /*
2096  * This routine handles processing a READ_LA mailbox
2097  * command upon completion. It is setup in the LPFC_MBOXQ
2098  * as the completion routine when the command is
2099  * handed off to the SLI layer.
2100  */
2101 void
2102 lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2103 {
2104 	struct lpfc_vport *vport = pmb->vport;
2105 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2106 	READ_LA_VAR *la;
2107 	MAILBOX_t *mb = &pmb->u.mb;
2108 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2109 
2110 	/* Unblock ELS traffic */
2111 	phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
2112 	/* Check for error */
2113 	if (mb->mbxStatus) {
2114 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2115 				"1307 READ_LA mbox error x%x state x%x\n",
2116 				mb->mbxStatus, vport->port_state);
2117 		lpfc_mbx_issue_link_down(phba);
2118 		phba->link_state = LPFC_HBA_ERROR;
2119 		goto lpfc_mbx_cmpl_read_la_free_mbuf;
2120 	}
2121 
2122 	la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
2123 
2124 	memcpy(&phba->alpa_map[0], mp->virt, 128);
2125 
2126 	spin_lock_irq(shost->host_lock);
2127 	if (la->pb)
2128 		vport->fc_flag |= FC_BYPASSED_MODE;
2129 	else
2130 		vport->fc_flag &= ~FC_BYPASSED_MODE;
2131 	spin_unlock_irq(shost->host_lock);
2132 
2133 	if ((phba->fc_eventTag  < la->eventTag) ||
2134 	    (phba->fc_eventTag == la->eventTag)) {
2135 		phba->fc_stat.LinkMultiEvent++;
2136 		if (la->attType == AT_LINK_UP)
2137 			if (phba->fc_eventTag != 0)
2138 				lpfc_linkdown(phba);
2139 	}
2140 
2141 	phba->fc_eventTag = la->eventTag;
2142 	if (la->mm)
2143 		phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2144 	else
2145 		phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
2146 
2147 	phba->link_events++;
2148 	if (la->attType == AT_LINK_UP && (!la->mm)) {
2149 		phba->fc_stat.LinkUp++;
2150 		if (phba->link_flag & LS_LOOPBACK_MODE) {
2151 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2152 					"1306 Link Up Event in loop back mode "
2153 					"x%x received Data: x%x x%x x%x x%x\n",
2154 					la->eventTag, phba->fc_eventTag,
2155 					la->granted_AL_PA, la->UlnkSpeed,
2156 					phba->alpa_map[0]);
2157 		} else {
2158 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2159 					"1303 Link Up Event x%x received "
2160 					"Data: x%x x%x x%x x%x x%x x%x %d\n",
2161 					la->eventTag, phba->fc_eventTag,
2162 					la->granted_AL_PA, la->UlnkSpeed,
2163 					phba->alpa_map[0],
2164 					la->mm, la->fa,
2165 					phba->wait_4_mlo_maint_flg);
2166 		}
2167 		lpfc_mbx_process_link_up(phba, la);
2168 	} else if (la->attType == AT_LINK_DOWN) {
2169 		phba->fc_stat.LinkDown++;
2170 		if (phba->link_flag & LS_LOOPBACK_MODE) {
2171 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2172 				"1308 Link Down Event in loop back mode "
2173 				"x%x received "
2174 				"Data: x%x x%x x%x\n",
2175 				la->eventTag, phba->fc_eventTag,
2176 				phba->pport->port_state, vport->fc_flag);
2177 		}
2178 		else {
2179 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2180 				"1305 Link Down Event x%x received "
2181 				"Data: x%x x%x x%x x%x x%x\n",
2182 				la->eventTag, phba->fc_eventTag,
2183 				phba->pport->port_state, vport->fc_flag,
2184 				la->mm, la->fa);
2185 		}
2186 		lpfc_mbx_issue_link_down(phba);
2187 	}
2188 	if (la->mm && la->attType == AT_LINK_UP) {
2189 		if (phba->link_state != LPFC_LINK_DOWN) {
2190 			phba->fc_stat.LinkDown++;
2191 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2192 				"1312 Link Down Event x%x received "
2193 				"Data: x%x x%x x%x\n",
2194 				la->eventTag, phba->fc_eventTag,
2195 				phba->pport->port_state, vport->fc_flag);
2196 			lpfc_mbx_issue_link_down(phba);
2197 		} else
2198 			lpfc_enable_la(phba);
2199 
2200 		lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2201 				"1310 Menlo Maint Mode Link up Event x%x rcvd "
2202 				"Data: x%x x%x x%x\n",
2203 				la->eventTag, phba->fc_eventTag,
2204 				phba->pport->port_state, vport->fc_flag);
2205 		/*
2206 		 * The cmnd that triggered this will be waiting for this
2207 		 * signal.
2208 		 */
2209 		/* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2210 		if (phba->wait_4_mlo_maint_flg) {
2211 			phba->wait_4_mlo_maint_flg = 0;
2212 			wake_up_interruptible(&phba->wait_4_mlo_m_q);
2213 		}
2214 	}
2215 
2216 	if (la->fa) {
2217 		if (la->mm)
2218 			lpfc_issue_clear_la(phba, vport);
2219 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2220 				"1311 fa %d\n", la->fa);
2221 	}
2222 
2223 lpfc_mbx_cmpl_read_la_free_mbuf:
2224 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2225 	kfree(mp);
2226 	mempool_free(pmb, phba->mbox_mem_pool);
2227 	return;
2228 }
2229 
2230 /*
2231  * This routine handles processing a REG_LOGIN mailbox
2232  * command upon completion. It is setup in the LPFC_MBOXQ
2233  * as the completion routine when the command is
2234  * handed off to the SLI layer.
2235  */
2236 void
2237 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2238 {
2239 	struct lpfc_vport  *vport = pmb->vport;
2240 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2241 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2242 
2243 	pmb->context1 = NULL;
2244 
2245 	/* Good status, call state machine */
2246 	lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
2247 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2248 	kfree(mp);
2249 	mempool_free(pmb, phba->mbox_mem_pool);
2250 	/* decrement the node reference count held for this callback
2251 	 * function.
2252 	 */
2253 	lpfc_nlp_put(ndlp);
2254 
2255 	return;
2256 }
2257 
2258 static void
2259 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2260 {
2261 	MAILBOX_t *mb = &pmb->u.mb;
2262 	struct lpfc_vport *vport = pmb->vport;
2263 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2264 
2265 	switch (mb->mbxStatus) {
2266 	case 0x0011:
2267 	case 0x0020:
2268 	case 0x9700:
2269 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2270 				 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2271 				 mb->mbxStatus);
2272 		break;
2273 	}
2274 	spin_lock_irq(&phba->hbalock);
2275 	vport->vpi_state &= ~LPFC_VPI_REGISTERED;
2276 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2277 	spin_unlock_irq(&phba->hbalock);
2278 	vport->unreg_vpi_cmpl = VPORT_OK;
2279 	mempool_free(pmb, phba->mbox_mem_pool);
2280 	/*
2281 	 * This shost reference might have been taken at the beginning of
2282 	 * lpfc_vport_delete()
2283 	 */
2284 	if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
2285 		scsi_host_put(shost);
2286 }
2287 
2288 int
2289 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2290 {
2291 	struct lpfc_hba  *phba = vport->phba;
2292 	LPFC_MBOXQ_t *mbox;
2293 	int rc;
2294 
2295 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2296 	if (!mbox)
2297 		return 1;
2298 
2299 	lpfc_unreg_vpi(phba, vport->vpi, mbox);
2300 	mbox->vport = vport;
2301 	mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
2302 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2303 	if (rc == MBX_NOT_FINISHED) {
2304 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2305 				 "1800 Could not issue unreg_vpi\n");
2306 		mempool_free(mbox, phba->mbox_mem_pool);
2307 		vport->unreg_vpi_cmpl = VPORT_ERROR;
2308 		return rc;
2309 	}
2310 	return 0;
2311 }
2312 
2313 static void
2314 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2315 {
2316 	struct lpfc_vport *vport = pmb->vport;
2317 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2318 	MAILBOX_t *mb = &pmb->u.mb;
2319 
2320 	switch (mb->mbxStatus) {
2321 	case 0x0011:
2322 	case 0x9601:
2323 	case 0x9602:
2324 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2325 				 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2326 				 mb->mbxStatus);
2327 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2328 		spin_lock_irq(shost->host_lock);
2329 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2330 		spin_unlock_irq(shost->host_lock);
2331 		vport->fc_myDID = 0;
2332 		goto out;
2333 	}
2334 
2335 	vport->vpi_state |= LPFC_VPI_REGISTERED;
2336 	vport->num_disc_nodes = 0;
2337 	/* go thru NPR list and issue ELS PLOGIs */
2338 	if (vport->fc_npr_cnt)
2339 		lpfc_els_disc_plogi(vport);
2340 
2341 	if (!vport->num_disc_nodes) {
2342 		spin_lock_irq(shost->host_lock);
2343 		vport->fc_flag &= ~FC_NDISC_ACTIVE;
2344 		spin_unlock_irq(shost->host_lock);
2345 		lpfc_can_disctmo(vport);
2346 	}
2347 	vport->port_state = LPFC_VPORT_READY;
2348 
2349 out:
2350 	mempool_free(pmb, phba->mbox_mem_pool);
2351 	return;
2352 }
2353 
2354 /**
2355  * lpfc_create_static_vport - Read HBA config region to create static vports.
2356  * @phba: pointer to lpfc hba data structure.
2357  *
2358  * This routine issue a DUMP mailbox command for config region 22 to get
2359  * the list of static vports to be created. The function create vports
2360  * based on the information returned from the HBA.
2361  **/
2362 void
2363 lpfc_create_static_vport(struct lpfc_hba *phba)
2364 {
2365 	LPFC_MBOXQ_t *pmb = NULL;
2366 	MAILBOX_t *mb;
2367 	struct static_vport_info *vport_info;
2368 	int rc = 0, i;
2369 	struct fc_vport_identifiers vport_id;
2370 	struct fc_vport *new_fc_vport;
2371 	struct Scsi_Host *shost;
2372 	struct lpfc_vport *vport;
2373 	uint16_t offset = 0;
2374 	uint8_t *vport_buff;
2375 	struct lpfc_dmabuf *mp;
2376 	uint32_t byte_count = 0;
2377 
2378 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2379 	if (!pmb) {
2380 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2381 				"0542 lpfc_create_static_vport failed to"
2382 				" allocate mailbox memory\n");
2383 		return;
2384 	}
2385 
2386 	mb = &pmb->u.mb;
2387 
2388 	vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2389 	if (!vport_info) {
2390 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2391 				"0543 lpfc_create_static_vport failed to"
2392 				" allocate vport_info\n");
2393 		mempool_free(pmb, phba->mbox_mem_pool);
2394 		return;
2395 	}
2396 
2397 	vport_buff = (uint8_t *) vport_info;
2398 	do {
2399 		if (lpfc_dump_static_vport(phba, pmb, offset))
2400 			goto out;
2401 
2402 		pmb->vport = phba->pport;
2403 		rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2404 
2405 		if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2406 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2407 				"0544 lpfc_create_static_vport failed to"
2408 				" issue dump mailbox command ret 0x%x "
2409 				"status 0x%x\n",
2410 				rc, mb->mbxStatus);
2411 			goto out;
2412 		}
2413 
2414 		if (phba->sli_rev == LPFC_SLI_REV4) {
2415 			byte_count = pmb->u.mqe.un.mb_words[5];
2416 			mp = (struct lpfc_dmabuf *) pmb->context2;
2417 			if (byte_count > sizeof(struct static_vport_info) -
2418 					offset)
2419 				byte_count = sizeof(struct static_vport_info)
2420 					- offset;
2421 			memcpy(vport_buff + offset, mp->virt, byte_count);
2422 			offset += byte_count;
2423 		} else {
2424 			if (mb->un.varDmp.word_cnt >
2425 				sizeof(struct static_vport_info) - offset)
2426 				mb->un.varDmp.word_cnt =
2427 					sizeof(struct static_vport_info)
2428 						- offset;
2429 			byte_count = mb->un.varDmp.word_cnt;
2430 			lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2431 				vport_buff + offset,
2432 				byte_count);
2433 
2434 			offset += byte_count;
2435 		}
2436 
2437 	} while (byte_count &&
2438 		offset < sizeof(struct static_vport_info));
2439 
2440 
2441 	if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2442 		((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2443 			!= VPORT_INFO_REV)) {
2444 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2445 			"0545 lpfc_create_static_vport bad"
2446 			" information header 0x%x 0x%x\n",
2447 			le32_to_cpu(vport_info->signature),
2448 			le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2449 
2450 		goto out;
2451 	}
2452 
2453 	shost = lpfc_shost_from_vport(phba->pport);
2454 
2455 	for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2456 		memset(&vport_id, 0, sizeof(vport_id));
2457 		vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2458 		vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2459 		if (!vport_id.port_name || !vport_id.node_name)
2460 			continue;
2461 
2462 		vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2463 		vport_id.vport_type = FC_PORTTYPE_NPIV;
2464 		vport_id.disable = false;
2465 		new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2466 
2467 		if (!new_fc_vport) {
2468 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2469 				"0546 lpfc_create_static_vport failed to"
2470 				" create vport\n");
2471 			continue;
2472 		}
2473 
2474 		vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2475 		vport->vport_flag |= STATIC_VPORT;
2476 	}
2477 
2478 out:
2479 	kfree(vport_info);
2480 	if (rc != MBX_TIMEOUT) {
2481 		if (pmb->context2) {
2482 			mp = (struct lpfc_dmabuf *) pmb->context2;
2483 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
2484 			kfree(mp);
2485 		}
2486 		mempool_free(pmb, phba->mbox_mem_pool);
2487 	}
2488 
2489 	return;
2490 }
2491 
2492 /*
2493  * This routine handles processing a Fabric REG_LOGIN mailbox
2494  * command upon completion. It is setup in the LPFC_MBOXQ
2495  * as the completion routine when the command is
2496  * handed off to the SLI layer.
2497  */
2498 void
2499 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2500 {
2501 	struct lpfc_vport *vport = pmb->vport;
2502 	MAILBOX_t *mb = &pmb->u.mb;
2503 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2504 	struct lpfc_nodelist *ndlp;
2505 
2506 	ndlp = (struct lpfc_nodelist *) pmb->context2;
2507 	pmb->context1 = NULL;
2508 	pmb->context2 = NULL;
2509 	if (mb->mbxStatus) {
2510 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2511 				 "0258 Register Fabric login error: 0x%x\n",
2512 				 mb->mbxStatus);
2513 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
2514 		kfree(mp);
2515 		mempool_free(pmb, phba->mbox_mem_pool);
2516 
2517 		if (phba->fc_topology == TOPOLOGY_LOOP) {
2518 			/* FLOGI failed, use loop map to make discovery list */
2519 			lpfc_disc_list_loopmap(vport);
2520 
2521 			/* Start discovery */
2522 			lpfc_disc_start(vport);
2523 			/* Decrement the reference count to ndlp after the
2524 			 * reference to the ndlp are done.
2525 			 */
2526 			lpfc_nlp_put(ndlp);
2527 			return;
2528 		}
2529 
2530 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2531 		/* Decrement the reference count to ndlp after the reference
2532 		 * to the ndlp are done.
2533 		 */
2534 		lpfc_nlp_put(ndlp);
2535 		return;
2536 	}
2537 
2538 	ndlp->nlp_rpi = mb->un.varWords[0];
2539 	ndlp->nlp_flag |= NLP_RPI_VALID;
2540 	ndlp->nlp_type |= NLP_FABRIC;
2541 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2542 
2543 	if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2544 		lpfc_start_fdiscs(phba);
2545 		lpfc_do_scr_ns_plogi(phba, vport);
2546 	}
2547 
2548 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2549 	kfree(mp);
2550 	mempool_free(pmb, phba->mbox_mem_pool);
2551 
2552 	/* Drop the reference count from the mbox at the end after
2553 	 * all the current reference to the ndlp have been done.
2554 	 */
2555 	lpfc_nlp_put(ndlp);
2556 	return;
2557 }
2558 
2559 /*
2560  * This routine handles processing a NameServer REG_LOGIN mailbox
2561  * command upon completion. It is setup in the LPFC_MBOXQ
2562  * as the completion routine when the command is
2563  * handed off to the SLI layer.
2564  */
2565 void
2566 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2567 {
2568 	MAILBOX_t *mb = &pmb->u.mb;
2569 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2570 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2571 	struct lpfc_vport *vport = pmb->vport;
2572 
2573 	if (mb->mbxStatus) {
2574 out:
2575 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2576 				 "0260 Register NameServer error: 0x%x\n",
2577 				 mb->mbxStatus);
2578 		/* decrement the node reference count held for this
2579 		 * callback function.
2580 		 */
2581 		lpfc_nlp_put(ndlp);
2582 		lpfc_mbuf_free(phba, mp->virt, mp->phys);
2583 		kfree(mp);
2584 		mempool_free(pmb, phba->mbox_mem_pool);
2585 
2586 		/* If no other thread is using the ndlp, free it */
2587 		lpfc_nlp_not_used(ndlp);
2588 
2589 		if (phba->fc_topology == TOPOLOGY_LOOP) {
2590 			/*
2591 			 * RegLogin failed, use loop map to make discovery
2592 			 * list
2593 			 */
2594 			lpfc_disc_list_loopmap(vport);
2595 
2596 			/* Start discovery */
2597 			lpfc_disc_start(vport);
2598 			return;
2599 		}
2600 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2601 		return;
2602 	}
2603 
2604 	pmb->context1 = NULL;
2605 
2606 	ndlp->nlp_rpi = mb->un.varWords[0];
2607 	ndlp->nlp_flag |= NLP_RPI_VALID;
2608 	ndlp->nlp_type |= NLP_FABRIC;
2609 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2610 
2611 	if (vport->port_state < LPFC_VPORT_READY) {
2612 		/* Link up discovery requires Fabric registration. */
2613 		lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2614 		lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2615 		lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2616 		lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2617 		lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2618 
2619 		/* Issue SCR just before NameServer GID_FT Query */
2620 		lpfc_issue_els_scr(vport, SCR_DID, 0);
2621 	}
2622 
2623 	vport->fc_ns_retry = 0;
2624 	/* Good status, issue CT Request to NameServer */
2625 	if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
2626 		/* Cannot issue NameServer Query, so finish up discovery */
2627 		goto out;
2628 	}
2629 
2630 	/* decrement the node reference count held for this
2631 	 * callback function.
2632 	 */
2633 	lpfc_nlp_put(ndlp);
2634 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2635 	kfree(mp);
2636 	mempool_free(pmb, phba->mbox_mem_pool);
2637 
2638 	return;
2639 }
2640 
2641 static void
2642 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2643 {
2644 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2645 	struct fc_rport  *rport;
2646 	struct lpfc_rport_data *rdata;
2647 	struct fc_rport_identifiers rport_ids;
2648 	struct lpfc_hba  *phba = vport->phba;
2649 
2650 	/* Remote port has reappeared. Re-register w/ FC transport */
2651 	rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2652 	rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2653 	rport_ids.port_id = ndlp->nlp_DID;
2654 	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
2655 
2656 	/*
2657 	 * We leave our node pointer in rport->dd_data when we unregister a
2658 	 * FCP target port.  But fc_remote_port_add zeros the space to which
2659 	 * rport->dd_data points.  So, if we're reusing a previously
2660 	 * registered port, drop the reference that we took the last time we
2661 	 * registered the port.
2662 	 */
2663 	if (ndlp->rport && ndlp->rport->dd_data &&
2664 	    ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
2665 		lpfc_nlp_put(ndlp);
2666 
2667 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2668 		"rport add:       did:x%x flg:x%x type x%x",
2669 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2670 
2671 	ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
2672 	if (!rport || !get_device(&rport->dev)) {
2673 		dev_printk(KERN_WARNING, &phba->pcidev->dev,
2674 			   "Warning: fc_remote_port_add failed\n");
2675 		return;
2676 	}
2677 
2678 	/* initialize static port data */
2679 	rport->maxframe_size = ndlp->nlp_maxframe;
2680 	rport->supported_classes = ndlp->nlp_class_sup;
2681 	rdata = rport->dd_data;
2682 	rdata->pnode = lpfc_nlp_get(ndlp);
2683 
2684 	if (ndlp->nlp_type & NLP_FCP_TARGET)
2685 		rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2686 	if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2687 		rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2688 
2689 
2690 	if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
2691 		fc_remote_port_rolechg(rport, rport_ids.roles);
2692 
2693 	if ((rport->scsi_target_id != -1) &&
2694 	    (rport->scsi_target_id < LPFC_MAX_TARGET)) {
2695 		ndlp->nlp_sid = rport->scsi_target_id;
2696 	}
2697 	return;
2698 }
2699 
2700 static void
2701 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
2702 {
2703 	struct fc_rport *rport = ndlp->rport;
2704 
2705 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2706 		"rport delete:    did:x%x flg:x%x type x%x",
2707 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2708 
2709 	fc_remote_port_delete(rport);
2710 
2711 	return;
2712 }
2713 
2714 static void
2715 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
2716 {
2717 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2718 
2719 	spin_lock_irq(shost->host_lock);
2720 	switch (state) {
2721 	case NLP_STE_UNUSED_NODE:
2722 		vport->fc_unused_cnt += count;
2723 		break;
2724 	case NLP_STE_PLOGI_ISSUE:
2725 		vport->fc_plogi_cnt += count;
2726 		break;
2727 	case NLP_STE_ADISC_ISSUE:
2728 		vport->fc_adisc_cnt += count;
2729 		break;
2730 	case NLP_STE_REG_LOGIN_ISSUE:
2731 		vport->fc_reglogin_cnt += count;
2732 		break;
2733 	case NLP_STE_PRLI_ISSUE:
2734 		vport->fc_prli_cnt += count;
2735 		break;
2736 	case NLP_STE_UNMAPPED_NODE:
2737 		vport->fc_unmap_cnt += count;
2738 		break;
2739 	case NLP_STE_MAPPED_NODE:
2740 		vport->fc_map_cnt += count;
2741 		break;
2742 	case NLP_STE_NPR_NODE:
2743 		vport->fc_npr_cnt += count;
2744 		break;
2745 	}
2746 	spin_unlock_irq(shost->host_lock);
2747 }
2748 
2749 static void
2750 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2751 		       int old_state, int new_state)
2752 {
2753 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2754 
2755 	if (new_state == NLP_STE_UNMAPPED_NODE) {
2756 		ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2757 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2758 		ndlp->nlp_type |= NLP_FC_NODE;
2759 	}
2760 	if (new_state == NLP_STE_MAPPED_NODE)
2761 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2762 	if (new_state == NLP_STE_NPR_NODE)
2763 		ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
2764 
2765 	/* Transport interface */
2766 	if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2767 			    old_state == NLP_STE_UNMAPPED_NODE)) {
2768 		vport->phba->nport_event_cnt++;
2769 		lpfc_unregister_remote_port(ndlp);
2770 	}
2771 
2772 	if (new_state ==  NLP_STE_MAPPED_NODE ||
2773 	    new_state == NLP_STE_UNMAPPED_NODE) {
2774 		vport->phba->nport_event_cnt++;
2775 		/*
2776 		 * Tell the fc transport about the port, if we haven't
2777 		 * already. If we have, and it's a scsi entity, be
2778 		 * sure to unblock any attached scsi devices
2779 		 */
2780 		lpfc_register_remote_port(vport, ndlp);
2781 	}
2782 	if ((new_state ==  NLP_STE_MAPPED_NODE) &&
2783 		(vport->stat_data_enabled)) {
2784 		/*
2785 		 * A new target is discovered, if there is no buffer for
2786 		 * statistical data collection allocate buffer.
2787 		 */
2788 		ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2789 					 sizeof(struct lpfc_scsicmd_bkt),
2790 					 GFP_KERNEL);
2791 
2792 		if (!ndlp->lat_data)
2793 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2794 				"0286 lpfc_nlp_state_cleanup failed to "
2795 				"allocate statistical data buffer DID "
2796 				"0x%x\n", ndlp->nlp_DID);
2797 	}
2798 	/*
2799 	 * if we added to Mapped list, but the remote port
2800 	 * registration failed or assigned a target id outside
2801 	 * our presentable range - move the node to the
2802 	 * Unmapped List
2803 	 */
2804 	if (new_state == NLP_STE_MAPPED_NODE &&
2805 	    (!ndlp->rport ||
2806 	     ndlp->rport->scsi_target_id == -1 ||
2807 	     ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
2808 		spin_lock_irq(shost->host_lock);
2809 		ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
2810 		spin_unlock_irq(shost->host_lock);
2811 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
2812 	}
2813 }
2814 
2815 static char *
2816 lpfc_nlp_state_name(char *buffer, size_t size, int state)
2817 {
2818 	static char *states[] = {
2819 		[NLP_STE_UNUSED_NODE] = "UNUSED",
2820 		[NLP_STE_PLOGI_ISSUE] = "PLOGI",
2821 		[NLP_STE_ADISC_ISSUE] = "ADISC",
2822 		[NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2823 		[NLP_STE_PRLI_ISSUE] = "PRLI",
2824 		[NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2825 		[NLP_STE_MAPPED_NODE] = "MAPPED",
2826 		[NLP_STE_NPR_NODE] = "NPR",
2827 	};
2828 
2829 	if (state < NLP_STE_MAX_STATE && states[state])
2830 		strlcpy(buffer, states[state], size);
2831 	else
2832 		snprintf(buffer, size, "unknown (%d)", state);
2833 	return buffer;
2834 }
2835 
2836 void
2837 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2838 		   int state)
2839 {
2840 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2841 	int  old_state = ndlp->nlp_state;
2842 	char name1[16], name2[16];
2843 
2844 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2845 			 "0904 NPort state transition x%06x, %s -> %s\n",
2846 			 ndlp->nlp_DID,
2847 			 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2848 			 lpfc_nlp_state_name(name2, sizeof(name2), state));
2849 
2850 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2851 		"node statechg    did:x%x old:%d ste:%d",
2852 		ndlp->nlp_DID, old_state, state);
2853 
2854 	if (old_state == NLP_STE_NPR_NODE &&
2855 	    state != NLP_STE_NPR_NODE)
2856 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
2857 	if (old_state == NLP_STE_UNMAPPED_NODE) {
2858 		ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2859 		ndlp->nlp_type &= ~NLP_FC_NODE;
2860 	}
2861 
2862 	if (list_empty(&ndlp->nlp_listp)) {
2863 		spin_lock_irq(shost->host_lock);
2864 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2865 		spin_unlock_irq(shost->host_lock);
2866 	} else if (old_state)
2867 		lpfc_nlp_counters(vport, old_state, -1);
2868 
2869 	ndlp->nlp_state = state;
2870 	lpfc_nlp_counters(vport, state, 1);
2871 	lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
2872 }
2873 
2874 void
2875 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2876 {
2877 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2878 
2879 	if (list_empty(&ndlp->nlp_listp)) {
2880 		spin_lock_irq(shost->host_lock);
2881 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2882 		spin_unlock_irq(shost->host_lock);
2883 	}
2884 }
2885 
2886 void
2887 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2888 {
2889 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2890 
2891 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
2892 	if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2893 		lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2894 	spin_lock_irq(shost->host_lock);
2895 	list_del_init(&ndlp->nlp_listp);
2896 	spin_unlock_irq(shost->host_lock);
2897 	lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2898 				NLP_STE_UNUSED_NODE);
2899 }
2900 
2901 static void
2902 lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2903 {
2904 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
2905 	if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2906 		lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2907 	lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2908 				NLP_STE_UNUSED_NODE);
2909 }
2910 /**
2911  * lpfc_initialize_node - Initialize all fields of node object
2912  * @vport: Pointer to Virtual Port object.
2913  * @ndlp: Pointer to FC node object.
2914  * @did: FC_ID of the node.
2915  *
2916  * This function is always called when node object need to be initialized.
2917  * It initializes all the fields of the node object. Although the reference
2918  * to phba from @ndlp can be obtained indirectly through it's reference to
2919  * @vport, a direct reference to phba is taken here by @ndlp. This is due
2920  * to the life-span of the @ndlp might go beyond the existence of @vport as
2921  * the final release of ndlp is determined by its reference count. And, the
2922  * operation on @ndlp needs the reference to phba.
2923  **/
2924 static inline void
2925 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2926 	uint32_t did)
2927 {
2928 	INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2929 	INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2930 	init_timer(&ndlp->nlp_delayfunc);
2931 	ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2932 	ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2933 	ndlp->nlp_DID = did;
2934 	ndlp->vport = vport;
2935 	ndlp->phba = vport->phba;
2936 	ndlp->nlp_sid = NLP_NO_SID;
2937 	kref_init(&ndlp->kref);
2938 	NLP_INT_NODE_ACT(ndlp);
2939 	atomic_set(&ndlp->cmd_pending, 0);
2940 	ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2941 }
2942 
2943 struct lpfc_nodelist *
2944 lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2945 		 int state)
2946 {
2947 	struct lpfc_hba *phba = vport->phba;
2948 	uint32_t did;
2949 	unsigned long flags;
2950 
2951 	if (!ndlp)
2952 		return NULL;
2953 
2954 	spin_lock_irqsave(&phba->ndlp_lock, flags);
2955 	/* The ndlp should not be in memory free mode */
2956 	if (NLP_CHK_FREE_REQ(ndlp)) {
2957 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2958 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2959 				"0277 lpfc_enable_node: ndlp:x%p "
2960 				"usgmap:x%x refcnt:%d\n",
2961 				(void *)ndlp, ndlp->nlp_usg_map,
2962 				atomic_read(&ndlp->kref.refcount));
2963 		return NULL;
2964 	}
2965 	/* The ndlp should not already be in active mode */
2966 	if (NLP_CHK_NODE_ACT(ndlp)) {
2967 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2968 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2969 				"0278 lpfc_enable_node: ndlp:x%p "
2970 				"usgmap:x%x refcnt:%d\n",
2971 				(void *)ndlp, ndlp->nlp_usg_map,
2972 				atomic_read(&ndlp->kref.refcount));
2973 		return NULL;
2974 	}
2975 
2976 	/* Keep the original DID */
2977 	did = ndlp->nlp_DID;
2978 
2979 	/* re-initialize ndlp except of ndlp linked list pointer */
2980 	memset((((char *)ndlp) + sizeof (struct list_head)), 0,
2981 		sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
2982 	lpfc_initialize_node(vport, ndlp, did);
2983 
2984 	spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2985 
2986 	if (state != NLP_STE_UNUSED_NODE)
2987 		lpfc_nlp_set_state(vport, ndlp, state);
2988 
2989 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2990 		"node enable:       did:x%x",
2991 		ndlp->nlp_DID, 0, 0);
2992 	return ndlp;
2993 }
2994 
2995 void
2996 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2997 {
2998 	/*
2999 	 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
3000 	 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
3001 	 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3002 	 * until ALL other outstanding threads have completed. We check
3003 	 * that the ndlp not already in the UNUSED state before we proceed.
3004 	 */
3005 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3006 		return;
3007 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
3008 	lpfc_nlp_put(ndlp);
3009 	return;
3010 }
3011 
3012 /*
3013  * Start / ReStart rescue timer for Discovery / RSCN handling
3014  */
3015 void
3016 lpfc_set_disctmo(struct lpfc_vport *vport)
3017 {
3018 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3019 	struct lpfc_hba  *phba = vport->phba;
3020 	uint32_t tmo;
3021 
3022 	if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
3023 		/* For FAN, timeout should be greater than edtov */
3024 		tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3025 	} else {
3026 		/* Normal discovery timeout should be > than ELS/CT timeout
3027 		 * FC spec states we need 3 * ratov for CT requests
3028 		 */
3029 		tmo = ((phba->fc_ratov * 3) + 3);
3030 	}
3031 
3032 
3033 	if (!timer_pending(&vport->fc_disctmo)) {
3034 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3035 			"set disc timer:  tmo:x%x state:x%x flg:x%x",
3036 			tmo, vport->port_state, vport->fc_flag);
3037 	}
3038 
3039 	mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3040 	spin_lock_irq(shost->host_lock);
3041 	vport->fc_flag |= FC_DISC_TMO;
3042 	spin_unlock_irq(shost->host_lock);
3043 
3044 	/* Start Discovery Timer state <hba_state> */
3045 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3046 			 "0247 Start Discovery Timer state x%x "
3047 			 "Data: x%x x%lx x%x x%x\n",
3048 			 vport->port_state, tmo,
3049 			 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3050 			 vport->fc_adisc_cnt);
3051 
3052 	return;
3053 }
3054 
3055 /*
3056  * Cancel rescue timer for Discovery / RSCN handling
3057  */
3058 int
3059 lpfc_can_disctmo(struct lpfc_vport *vport)
3060 {
3061 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3062 	unsigned long iflags;
3063 
3064 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3065 		"can disc timer:  state:x%x rtry:x%x flg:x%x",
3066 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3067 
3068 	/* Turn off discovery timer if its running */
3069 	if (vport->fc_flag & FC_DISC_TMO) {
3070 		spin_lock_irqsave(shost->host_lock, iflags);
3071 		vport->fc_flag &= ~FC_DISC_TMO;
3072 		spin_unlock_irqrestore(shost->host_lock, iflags);
3073 		del_timer_sync(&vport->fc_disctmo);
3074 		spin_lock_irqsave(&vport->work_port_lock, iflags);
3075 		vport->work_port_events &= ~WORKER_DISC_TMO;
3076 		spin_unlock_irqrestore(&vport->work_port_lock, iflags);
3077 	}
3078 
3079 	/* Cancel Discovery Timer state <hba_state> */
3080 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3081 			 "0248 Cancel Discovery Timer state x%x "
3082 			 "Data: x%x x%x x%x\n",
3083 			 vport->port_state, vport->fc_flag,
3084 			 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
3085 	return 0;
3086 }
3087 
3088 /*
3089  * Check specified ring for outstanding IOCB on the SLI queue
3090  * Return true if iocb matches the specified nport
3091  */
3092 int
3093 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3094 		    struct lpfc_sli_ring *pring,
3095 		    struct lpfc_iocbq *iocb,
3096 		    struct lpfc_nodelist *ndlp)
3097 {
3098 	struct lpfc_sli *psli = &phba->sli;
3099 	IOCB_t *icmd = &iocb->iocb;
3100 	struct lpfc_vport    *vport = ndlp->vport;
3101 
3102 	if (iocb->vport != vport)
3103 		return 0;
3104 
3105 	if (pring->ringno == LPFC_ELS_RING) {
3106 		switch (icmd->ulpCommand) {
3107 		case CMD_GEN_REQUEST64_CR:
3108 			if (iocb->context_un.ndlp == ndlp)
3109 				return 1;
3110 		case CMD_ELS_REQUEST64_CR:
3111 			if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3112 				return 1;
3113 		case CMD_XMIT_ELS_RSP64_CX:
3114 			if (iocb->context1 == (uint8_t *) ndlp)
3115 				return 1;
3116 		}
3117 	} else if (pring->ringno == psli->extra_ring) {
3118 
3119 	} else if (pring->ringno == psli->fcp_ring) {
3120 		/* Skip match check if waiting to relogin to FCP target */
3121 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3122 		    (ndlp->nlp_flag & NLP_DELAY_TMO)) {
3123 			return 0;
3124 		}
3125 		if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
3126 			return 1;
3127 		}
3128 	} else if (pring->ringno == psli->next_ring) {
3129 
3130 	}
3131 	return 0;
3132 }
3133 
3134 /*
3135  * Free resources / clean up outstanding I/Os
3136  * associated with nlp_rpi in the LPFC_NODELIST entry.
3137  */
3138 static int
3139 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
3140 {
3141 	LIST_HEAD(completions);
3142 	struct lpfc_sli *psli;
3143 	struct lpfc_sli_ring *pring;
3144 	struct lpfc_iocbq *iocb, *next_iocb;
3145 	uint32_t i;
3146 
3147 	lpfc_fabric_abort_nport(ndlp);
3148 
3149 	/*
3150 	 * Everything that matches on txcmplq will be returned
3151 	 * by firmware with a no rpi error.
3152 	 */
3153 	psli = &phba->sli;
3154 	if (ndlp->nlp_flag & NLP_RPI_VALID) {
3155 		/* Now process each ring */
3156 		for (i = 0; i < psli->num_rings; i++) {
3157 			pring = &psli->ring[i];
3158 
3159 			spin_lock_irq(&phba->hbalock);
3160 			list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
3161 						 list) {
3162 				/*
3163 				 * Check to see if iocb matches the nport we are
3164 				 * looking for
3165 				 */
3166 				if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3167 							 ndlp))) {
3168 					/* It matches, so deque and call compl
3169 					   with an error */
3170 					list_move_tail(&iocb->list,
3171 						       &completions);
3172 					pring->txq_cnt--;
3173 				}
3174 			}
3175 			spin_unlock_irq(&phba->hbalock);
3176 		}
3177 	}
3178 
3179 	/* Cancel all the IOCBs from the completions list */
3180 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3181 			      IOERR_SLI_ABORTED);
3182 
3183 	return 0;
3184 }
3185 
3186 /*
3187  * Free rpi associated with LPFC_NODELIST entry.
3188  * This routine is called from lpfc_freenode(), when we are removing
3189  * a LPFC_NODELIST entry. It is also called if the driver initiates a
3190  * LOGO that completes successfully, and we are waiting to PLOGI back
3191  * to the remote NPort. In addition, it is called after we receive
3192  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3193  * we are waiting to PLOGI back to the remote NPort.
3194  */
3195 int
3196 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3197 {
3198 	struct lpfc_hba *phba = vport->phba;
3199 	LPFC_MBOXQ_t    *mbox;
3200 	int rc;
3201 
3202 	if (ndlp->nlp_flag & NLP_RPI_VALID) {
3203 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3204 		if (mbox) {
3205 			lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
3206 			mbox->vport = vport;
3207 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3208 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3209 			if (rc == MBX_NOT_FINISHED)
3210 				mempool_free(mbox, phba->mbox_mem_pool);
3211 		}
3212 		lpfc_no_rpi(phba, ndlp);
3213 		ndlp->nlp_rpi = 0;
3214 		ndlp->nlp_flag &= ~NLP_RPI_VALID;
3215 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3216 		return 1;
3217 	}
3218 	return 0;
3219 }
3220 
3221 void
3222 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3223 {
3224 	struct lpfc_hba  *phba  = vport->phba;
3225 	LPFC_MBOXQ_t     *mbox;
3226 	int rc;
3227 
3228 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3229 	if (mbox) {
3230 		lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3231 		mbox->vport = vport;
3232 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3233 		mbox->context1 = NULL;
3234 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
3235 		if (rc != MBX_TIMEOUT)
3236 			mempool_free(mbox, phba->mbox_mem_pool);
3237 
3238 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3239 			lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3240 				"1836 Could not issue "
3241 				"unreg_login(all_rpis) status %d\n", rc);
3242 	}
3243 }
3244 
3245 void
3246 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3247 {
3248 	struct lpfc_hba  *phba  = vport->phba;
3249 	LPFC_MBOXQ_t     *mbox;
3250 	int rc;
3251 
3252 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3253 	if (mbox) {
3254 		lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3255 		mbox->vport = vport;
3256 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3257 		mbox->context1 = NULL;
3258 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
3259 		if (rc != MBX_TIMEOUT)
3260 			mempool_free(mbox, phba->mbox_mem_pool);
3261 
3262 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3263 			lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3264 					 "1815 Could not issue "
3265 					 "unreg_did (default rpis) status %d\n",
3266 					 rc);
3267 	}
3268 }
3269 
3270 /*
3271  * Free resources associated with LPFC_NODELIST entry
3272  * so it can be freed.
3273  */
3274 static int
3275 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3276 {
3277 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3278 	struct lpfc_hba  *phba = vport->phba;
3279 	LPFC_MBOXQ_t *mb, *nextmb;
3280 	struct lpfc_dmabuf *mp;
3281 
3282 	/* Cleanup node for NPort <nlp_DID> */
3283 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3284 			 "0900 Cleanup node for NPort x%x "
3285 			 "Data: x%x x%x x%x\n",
3286 			 ndlp->nlp_DID, ndlp->nlp_flag,
3287 			 ndlp->nlp_state, ndlp->nlp_rpi);
3288 	if (NLP_CHK_FREE_REQ(ndlp)) {
3289 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3290 				"0280 lpfc_cleanup_node: ndlp:x%p "
3291 				"usgmap:x%x refcnt:%d\n",
3292 				(void *)ndlp, ndlp->nlp_usg_map,
3293 				atomic_read(&ndlp->kref.refcount));
3294 		lpfc_dequeue_node(vport, ndlp);
3295 	} else {
3296 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3297 				"0281 lpfc_cleanup_node: ndlp:x%p "
3298 				"usgmap:x%x refcnt:%d\n",
3299 				(void *)ndlp, ndlp->nlp_usg_map,
3300 				atomic_read(&ndlp->kref.refcount));
3301 		lpfc_disable_node(vport, ndlp);
3302 	}
3303 
3304 	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3305 	if ((mb = phba->sli.mbox_active)) {
3306 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
3307 		   (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3308 			mb->context2 = NULL;
3309 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3310 		}
3311 	}
3312 
3313 	spin_lock_irq(&phba->hbalock);
3314 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
3315 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
3316 		    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3317 			mp = (struct lpfc_dmabuf *) (mb->context1);
3318 			if (mp) {
3319 				__lpfc_mbuf_free(phba, mp->virt, mp->phys);
3320 				kfree(mp);
3321 			}
3322 			list_del(&mb->list);
3323 			mempool_free(mb, phba->mbox_mem_pool);
3324 			/* We shall not invoke the lpfc_nlp_put to decrement
3325 			 * the ndlp reference count as we are in the process
3326 			 * of lpfc_nlp_release.
3327 			 */
3328 		}
3329 	}
3330 	spin_unlock_irq(&phba->hbalock);
3331 
3332 	lpfc_els_abort(phba, ndlp);
3333 
3334 	spin_lock_irq(shost->host_lock);
3335 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
3336 	spin_unlock_irq(shost->host_lock);
3337 
3338 	ndlp->nlp_last_elscmd = 0;
3339 	del_timer_sync(&ndlp->nlp_delayfunc);
3340 
3341 	list_del_init(&ndlp->els_retry_evt.evt_listp);
3342 	list_del_init(&ndlp->dev_loss_evt.evt_listp);
3343 
3344 	lpfc_unreg_rpi(vport, ndlp);
3345 
3346 	return 0;
3347 }
3348 
3349 /*
3350  * Check to see if we can free the nlp back to the freelist.
3351  * If we are in the middle of using the nlp in the discovery state
3352  * machine, defer the free till we reach the end of the state machine.
3353  */
3354 static void
3355 lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3356 {
3357 	struct lpfc_hba  *phba = vport->phba;
3358 	struct lpfc_rport_data *rdata;
3359 	LPFC_MBOXQ_t *mbox;
3360 	int rc;
3361 
3362 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
3363 	if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3364 	    !(ndlp->nlp_flag & NLP_RPI_VALID)) {
3365 		/* For this case we need to cleanup the default rpi
3366 		 * allocated by the firmware.
3367 		 */
3368 		if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3369 			!= NULL) {
3370 			rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
3371 			    (uint8_t *) &vport->fc_sparam, mbox, 0);
3372 			if (rc) {
3373 				mempool_free(mbox, phba->mbox_mem_pool);
3374 			}
3375 			else {
3376 				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3377 				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3378 				mbox->vport = vport;
3379 				mbox->context2 = NULL;
3380 				rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3381 				if (rc == MBX_NOT_FINISHED) {
3382 					mempool_free(mbox, phba->mbox_mem_pool);
3383 				}
3384 			}
3385 		}
3386 	}
3387 	lpfc_cleanup_node(vport, ndlp);
3388 
3389 	/*
3390 	 * We can get here with a non-NULL ndlp->rport because when we
3391 	 * unregister a rport we don't break the rport/node linkage.  So if we
3392 	 * do, make sure we don't leaving any dangling pointers behind.
3393 	 */
3394 	if (ndlp->rport) {
3395 		rdata = ndlp->rport->dd_data;
3396 		rdata->pnode = NULL;
3397 		ndlp->rport = NULL;
3398 	}
3399 }
3400 
3401 static int
3402 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3403 	      uint32_t did)
3404 {
3405 	D_ID mydid, ndlpdid, matchdid;
3406 
3407 	if (did == Bcast_DID)
3408 		return 0;
3409 
3410 	/* First check for Direct match */
3411 	if (ndlp->nlp_DID == did)
3412 		return 1;
3413 
3414 	/* Next check for area/domain identically equals 0 match */
3415 	mydid.un.word = vport->fc_myDID;
3416 	if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
3417 		return 0;
3418 	}
3419 
3420 	matchdid.un.word = did;
3421 	ndlpdid.un.word = ndlp->nlp_DID;
3422 	if (matchdid.un.b.id == ndlpdid.un.b.id) {
3423 		if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3424 		    (mydid.un.b.area == matchdid.un.b.area)) {
3425 			if ((ndlpdid.un.b.domain == 0) &&
3426 			    (ndlpdid.un.b.area == 0)) {
3427 				if (ndlpdid.un.b.id)
3428 					return 1;
3429 			}
3430 			return 0;
3431 		}
3432 
3433 		matchdid.un.word = ndlp->nlp_DID;
3434 		if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3435 		    (mydid.un.b.area == ndlpdid.un.b.area)) {
3436 			if ((matchdid.un.b.domain == 0) &&
3437 			    (matchdid.un.b.area == 0)) {
3438 				if (matchdid.un.b.id)
3439 					return 1;
3440 			}
3441 		}
3442 	}
3443 	return 0;
3444 }
3445 
3446 /* Search for a nodelist entry */
3447 static struct lpfc_nodelist *
3448 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
3449 {
3450 	struct lpfc_nodelist *ndlp;
3451 	uint32_t data1;
3452 
3453 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3454 		if (lpfc_matchdid(vport, ndlp, did)) {
3455 			data1 = (((uint32_t) ndlp->nlp_state << 24) |
3456 				 ((uint32_t) ndlp->nlp_xri << 16) |
3457 				 ((uint32_t) ndlp->nlp_type << 8) |
3458 				 ((uint32_t) ndlp->nlp_rpi & 0xff));
3459 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3460 					 "0929 FIND node DID "
3461 					 "Data: x%p x%x x%x x%x\n",
3462 					 ndlp, ndlp->nlp_DID,
3463 					 ndlp->nlp_flag, data1);
3464 			return ndlp;
3465 		}
3466 	}
3467 
3468 	/* FIND node did <did> NOT FOUND */
3469 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3470 			 "0932 FIND node did x%x NOT FOUND.\n", did);
3471 	return NULL;
3472 }
3473 
3474 struct lpfc_nodelist *
3475 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
3476 {
3477 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3478 	struct lpfc_nodelist *ndlp;
3479 
3480 	spin_lock_irq(shost->host_lock);
3481 	ndlp = __lpfc_findnode_did(vport, did);
3482 	spin_unlock_irq(shost->host_lock);
3483 	return ndlp;
3484 }
3485 
3486 struct lpfc_nodelist *
3487 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
3488 {
3489 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3490 	struct lpfc_nodelist *ndlp;
3491 
3492 	ndlp = lpfc_findnode_did(vport, did);
3493 	if (!ndlp) {
3494 		if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3495 		    lpfc_rscn_payload_check(vport, did) == 0)
3496 			return NULL;
3497 		ndlp = (struct lpfc_nodelist *)
3498 		     mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
3499 		if (!ndlp)
3500 			return NULL;
3501 		lpfc_nlp_init(vport, ndlp, did);
3502 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3503 		spin_lock_irq(shost->host_lock);
3504 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3505 		spin_unlock_irq(shost->host_lock);
3506 		return ndlp;
3507 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
3508 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3509 		if (!ndlp)
3510 			return NULL;
3511 		spin_lock_irq(shost->host_lock);
3512 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3513 		spin_unlock_irq(shost->host_lock);
3514 		return ndlp;
3515 	}
3516 
3517 	if ((vport->fc_flag & FC_RSCN_MODE) &&
3518 	    !(vport->fc_flag & FC_NDISC_ACTIVE)) {
3519 		if (lpfc_rscn_payload_check(vport, did)) {
3520 			/* If we've already recieved a PLOGI from this NPort
3521 			 * we don't need to try to discover it again.
3522 			 */
3523 			if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3524 				return NULL;
3525 
3526 			/* Since this node is marked for discovery,
3527 			 * delay timeout is not needed.
3528 			 */
3529 			lpfc_cancel_retry_delay_tmo(vport, ndlp);
3530 			spin_lock_irq(shost->host_lock);
3531 			ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3532 			spin_unlock_irq(shost->host_lock);
3533 		} else
3534 			ndlp = NULL;
3535 	} else {
3536 		/* If we've already recieved a PLOGI from this NPort,
3537 		 * or we are already in the process of discovery on it,
3538 		 * we don't need to try to discover it again.
3539 		 */
3540 		if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
3541 		    ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3542 		    ndlp->nlp_flag & NLP_RCV_PLOGI)
3543 			return NULL;
3544 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3545 		spin_lock_irq(shost->host_lock);
3546 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3547 		spin_unlock_irq(shost->host_lock);
3548 	}
3549 	return ndlp;
3550 }
3551 
3552 /* Build a list of nodes to discover based on the loopmap */
3553 void
3554 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
3555 {
3556 	struct lpfc_hba  *phba = vport->phba;
3557 	int j;
3558 	uint32_t alpa, index;
3559 
3560 	if (!lpfc_is_link_up(phba))
3561 		return;
3562 
3563 	if (phba->fc_topology != TOPOLOGY_LOOP)
3564 		return;
3565 
3566 	/* Check for loop map present or not */
3567 	if (phba->alpa_map[0]) {
3568 		for (j = 1; j <= phba->alpa_map[0]; j++) {
3569 			alpa = phba->alpa_map[j];
3570 			if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
3571 				continue;
3572 			lpfc_setup_disc_node(vport, alpa);
3573 		}
3574 	} else {
3575 		/* No alpamap, so try all alpa's */
3576 		for (j = 0; j < FC_MAXLOOP; j++) {
3577 			/* If cfg_scan_down is set, start from highest
3578 			 * ALPA (0xef) to lowest (0x1).
3579 			 */
3580 			if (vport->cfg_scan_down)
3581 				index = j;
3582 			else
3583 				index = FC_MAXLOOP - j - 1;
3584 			alpa = lpfcAlpaArray[index];
3585 			if ((vport->fc_myDID & 0xff) == alpa)
3586 				continue;
3587 			lpfc_setup_disc_node(vport, alpa);
3588 		}
3589 	}
3590 	return;
3591 }
3592 
3593 void
3594 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
3595 {
3596 	LPFC_MBOXQ_t *mbox;
3597 	struct lpfc_sli *psli = &phba->sli;
3598 	struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3599 	struct lpfc_sli_ring *fcp_ring   = &psli->ring[psli->fcp_ring];
3600 	struct lpfc_sli_ring *next_ring  = &psli->ring[psli->next_ring];
3601 	int  rc;
3602 
3603 	/*
3604 	 * if it's not a physical port or if we already send
3605 	 * clear_la then don't send it.
3606 	 */
3607 	if ((phba->link_state >= LPFC_CLEAR_LA) ||
3608 	    (vport->port_type != LPFC_PHYSICAL_PORT) ||
3609 		(phba->sli_rev == LPFC_SLI_REV4))
3610 		return;
3611 
3612 			/* Link up discovery */
3613 	if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3614 		phba->link_state = LPFC_CLEAR_LA;
3615 		lpfc_clear_la(phba, mbox);
3616 		mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3617 		mbox->vport = vport;
3618 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3619 		if (rc == MBX_NOT_FINISHED) {
3620 			mempool_free(mbox, phba->mbox_mem_pool);
3621 			lpfc_disc_flush_list(vport);
3622 			extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3623 			fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3624 			next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3625 			phba->link_state = LPFC_HBA_ERROR;
3626 		}
3627 	}
3628 }
3629 
3630 /* Reg_vpi to tell firmware to resume normal operations */
3631 void
3632 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3633 {
3634 	LPFC_MBOXQ_t *regvpimbox;
3635 
3636 	regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3637 	if (regvpimbox) {
3638 		lpfc_reg_vpi(vport, regvpimbox);
3639 		regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3640 		regvpimbox->vport = vport;
3641 		if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
3642 					== MBX_NOT_FINISHED) {
3643 			mempool_free(regvpimbox, phba->mbox_mem_pool);
3644 		}
3645 	}
3646 }
3647 
3648 /* Start Link up / RSCN discovery on NPR nodes */
3649 void
3650 lpfc_disc_start(struct lpfc_vport *vport)
3651 {
3652 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3653 	struct lpfc_hba  *phba = vport->phba;
3654 	uint32_t num_sent;
3655 	uint32_t clear_la_pending;
3656 	int did_changed;
3657 
3658 	if (!lpfc_is_link_up(phba))
3659 		return;
3660 
3661 	if (phba->link_state == LPFC_CLEAR_LA)
3662 		clear_la_pending = 1;
3663 	else
3664 		clear_la_pending = 0;
3665 
3666 	if (vport->port_state < LPFC_VPORT_READY)
3667 		vport->port_state = LPFC_DISC_AUTH;
3668 
3669 	lpfc_set_disctmo(vport);
3670 
3671 	if (vport->fc_prevDID == vport->fc_myDID)
3672 		did_changed = 0;
3673 	else
3674 		did_changed = 1;
3675 
3676 	vport->fc_prevDID = vport->fc_myDID;
3677 	vport->num_disc_nodes = 0;
3678 
3679 	/* Start Discovery state <hba_state> */
3680 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3681 			 "0202 Start Discovery hba state x%x "
3682 			 "Data: x%x x%x x%x\n",
3683 			 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3684 			 vport->fc_adisc_cnt);
3685 
3686 	/* First do ADISCs - if any */
3687 	num_sent = lpfc_els_disc_adisc(vport);
3688 
3689 	if (num_sent)
3690 		return;
3691 
3692 	/*
3693 	 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3694 	 * continue discovery.
3695 	 */
3696 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3697 	    !(vport->fc_flag & FC_PT2PT) &&
3698 	    !(vport->fc_flag & FC_RSCN_MODE) &&
3699 	    (phba->sli_rev < LPFC_SLI_REV4)) {
3700 		lpfc_issue_reg_vpi(phba, vport);
3701 		return;
3702 	}
3703 
3704 	/*
3705 	 * For SLI2, we need to set port_state to READY and continue
3706 	 * discovery.
3707 	 */
3708 	if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
3709 		/* If we get here, there is nothing to ADISC */
3710 		if (vport->port_type == LPFC_PHYSICAL_PORT)
3711 			lpfc_issue_clear_la(phba, vport);
3712 
3713 		if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
3714 			vport->num_disc_nodes = 0;
3715 			/* go thru NPR nodes and issue ELS PLOGIs */
3716 			if (vport->fc_npr_cnt)
3717 				lpfc_els_disc_plogi(vport);
3718 
3719 			if (!vport->num_disc_nodes) {
3720 				spin_lock_irq(shost->host_lock);
3721 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
3722 				spin_unlock_irq(shost->host_lock);
3723 				lpfc_can_disctmo(vport);
3724 			}
3725 		}
3726 		vport->port_state = LPFC_VPORT_READY;
3727 	} else {
3728 		/* Next do PLOGIs - if any */
3729 		num_sent = lpfc_els_disc_plogi(vport);
3730 
3731 		if (num_sent)
3732 			return;
3733 
3734 		if (vport->fc_flag & FC_RSCN_MODE) {
3735 			/* Check to see if more RSCNs came in while we
3736 			 * were processing this one.
3737 			 */
3738 			if ((vport->fc_rscn_id_cnt == 0) &&
3739 			    (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3740 				spin_lock_irq(shost->host_lock);
3741 				vport->fc_flag &= ~FC_RSCN_MODE;
3742 				spin_unlock_irq(shost->host_lock);
3743 				lpfc_can_disctmo(vport);
3744 			} else
3745 				lpfc_els_handle_rscn(vport);
3746 		}
3747 	}
3748 	return;
3749 }
3750 
3751 /*
3752  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3753  *  ring the match the sppecified nodelist.
3754  */
3755 static void
3756 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
3757 {
3758 	LIST_HEAD(completions);
3759 	struct lpfc_sli *psli;
3760 	IOCB_t     *icmd;
3761 	struct lpfc_iocbq    *iocb, *next_iocb;
3762 	struct lpfc_sli_ring *pring;
3763 
3764 	psli = &phba->sli;
3765 	pring = &psli->ring[LPFC_ELS_RING];
3766 
3767 	/* Error matching iocb on txq or txcmplq
3768 	 * First check the txq.
3769 	 */
3770 	spin_lock_irq(&phba->hbalock);
3771 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3772 		if (iocb->context1 != ndlp) {
3773 			continue;
3774 		}
3775 		icmd = &iocb->iocb;
3776 		if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3777 		    (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3778 
3779 			list_move_tail(&iocb->list, &completions);
3780 			pring->txq_cnt--;
3781 		}
3782 	}
3783 
3784 	/* Next check the txcmplq */
3785 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3786 		if (iocb->context1 != ndlp) {
3787 			continue;
3788 		}
3789 		icmd = &iocb->iocb;
3790 		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3791 		    icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
3792 			lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3793 		}
3794 	}
3795 	spin_unlock_irq(&phba->hbalock);
3796 
3797 	/* Cancel all the IOCBs from the completions list */
3798 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3799 			      IOERR_SLI_ABORTED);
3800 }
3801 
3802 static void
3803 lpfc_disc_flush_list(struct lpfc_vport *vport)
3804 {
3805 	struct lpfc_nodelist *ndlp, *next_ndlp;
3806 	struct lpfc_hba *phba = vport->phba;
3807 
3808 	if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3809 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3810 					 nlp_listp) {
3811 			if (!NLP_CHK_NODE_ACT(ndlp))
3812 				continue;
3813 			if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3814 			    ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3815 				lpfc_free_tx(phba, ndlp);
3816 			}
3817 		}
3818 	}
3819 }
3820 
3821 void
3822 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3823 {
3824 	lpfc_els_flush_rscn(vport);
3825 	lpfc_els_flush_cmd(vport);
3826 	lpfc_disc_flush_list(vport);
3827 }
3828 
3829 /*****************************************************************************/
3830 /*
3831  * NAME:     lpfc_disc_timeout
3832  *
3833  * FUNCTION: Fibre Channel driver discovery timeout routine.
3834  *
3835  * EXECUTION ENVIRONMENT: interrupt only
3836  *
3837  * CALLED FROM:
3838  *      Timer function
3839  *
3840  * RETURNS:
3841  *      none
3842  */
3843 /*****************************************************************************/
3844 void
3845 lpfc_disc_timeout(unsigned long ptr)
3846 {
3847 	struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3848 	struct lpfc_hba   *phba = vport->phba;
3849 	uint32_t tmo_posted;
3850 	unsigned long flags = 0;
3851 
3852 	if (unlikely(!phba))
3853 		return;
3854 
3855 	spin_lock_irqsave(&vport->work_port_lock, flags);
3856 	tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
3857 	if (!tmo_posted)
3858 		vport->work_port_events |= WORKER_DISC_TMO;
3859 	spin_unlock_irqrestore(&vport->work_port_lock, flags);
3860 
3861 	if (!tmo_posted)
3862 		lpfc_worker_wake_up(phba);
3863 	return;
3864 }
3865 
3866 static void
3867 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
3868 {
3869 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3870 	struct lpfc_hba  *phba = vport->phba;
3871 	struct lpfc_sli  *psli = &phba->sli;
3872 	struct lpfc_nodelist *ndlp, *next_ndlp;
3873 	LPFC_MBOXQ_t *initlinkmbox;
3874 	int rc, clrlaerr = 0;
3875 
3876 	if (!(vport->fc_flag & FC_DISC_TMO))
3877 		return;
3878 
3879 	spin_lock_irq(shost->host_lock);
3880 	vport->fc_flag &= ~FC_DISC_TMO;
3881 	spin_unlock_irq(shost->host_lock);
3882 
3883 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3884 		"disc timeout:    state:x%x rtry:x%x flg:x%x",
3885 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3886 
3887 	switch (vport->port_state) {
3888 
3889 	case LPFC_LOCAL_CFG_LINK:
3890 	/* port_state is identically  LPFC_LOCAL_CFG_LINK while waiting for
3891 	 * FAN
3892 	 */
3893 				/* FAN timeout */
3894 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3895 				 "0221 FAN timeout\n");
3896 		/* Start discovery by sending FLOGI, clean up old rpis */
3897 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
3898 					 nlp_listp) {
3899 			if (!NLP_CHK_NODE_ACT(ndlp))
3900 				continue;
3901 			if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3902 				continue;
3903 			if (ndlp->nlp_type & NLP_FABRIC) {
3904 				/* Clean up the ndlp on Fabric connections */
3905 				lpfc_drop_node(vport, ndlp);
3906 
3907 			} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
3908 				/* Fail outstanding IO now since device
3909 				 * is marked for PLOGI.
3910 				 */
3911 				lpfc_unreg_rpi(vport, ndlp);
3912 			}
3913 		}
3914 		if (vport->port_state != LPFC_FLOGI) {
3915 			lpfc_initial_flogi(vport);
3916 			return;
3917 		}
3918 		break;
3919 
3920 	case LPFC_FDISC:
3921 	case LPFC_FLOGI:
3922 	/* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
3923 		/* Initial FLOGI timeout */
3924 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3925 				 "0222 Initial %s timeout\n",
3926 				 vport->vpi ? "FDISC" : "FLOGI");
3927 
3928 		/* Assume no Fabric and go on with discovery.
3929 		 * Check for outstanding ELS FLOGI to abort.
3930 		 */
3931 
3932 		/* FLOGI failed, so just use loop map to make discovery list */
3933 		lpfc_disc_list_loopmap(vport);
3934 
3935 		/* Start discovery */
3936 		lpfc_disc_start(vport);
3937 		break;
3938 
3939 	case LPFC_FABRIC_CFG_LINK:
3940 	/* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
3941 	   NameServer login */
3942 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3943 				 "0223 Timeout while waiting for "
3944 				 "NameServer login\n");
3945 		/* Next look for NameServer ndlp */
3946 		ndlp = lpfc_findnode_did(vport, NameServer_DID);
3947 		if (ndlp && NLP_CHK_NODE_ACT(ndlp))
3948 			lpfc_els_abort(phba, ndlp);
3949 
3950 		/* ReStart discovery */
3951 		goto restart_disc;
3952 
3953 	case LPFC_NS_QRY:
3954 	/* Check for wait for NameServer Rsp timeout */
3955 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3956 				 "0224 NameServer Query timeout "
3957 				 "Data: x%x x%x\n",
3958 				 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
3959 
3960 		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
3961 			/* Try it one more time */
3962 			vport->fc_ns_retry++;
3963 			rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
3964 					 vport->fc_ns_retry, 0);
3965 			if (rc == 0)
3966 				break;
3967 		}
3968 		vport->fc_ns_retry = 0;
3969 
3970 restart_disc:
3971 		/*
3972 		 * Discovery is over.
3973 		 * set port_state to PORT_READY if SLI2.
3974 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
3975 		 */
3976 		if (phba->sli_rev < LPFC_SLI_REV4) {
3977 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3978 				lpfc_issue_reg_vpi(phba, vport);
3979 			else  {	/* NPIV Not enabled */
3980 				lpfc_issue_clear_la(phba, vport);
3981 				vport->port_state = LPFC_VPORT_READY;
3982 			}
3983 		}
3984 
3985 		/* Setup and issue mailbox INITIALIZE LINK command */
3986 		initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3987 		if (!initlinkmbox) {
3988 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3989 					 "0206 Device Discovery "
3990 					 "completion error\n");
3991 			phba->link_state = LPFC_HBA_ERROR;
3992 			break;
3993 		}
3994 
3995 		lpfc_linkdown(phba);
3996 		lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
3997 			       phba->cfg_link_speed);
3998 		initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
3999 		initlinkmbox->vport = vport;
4000 		initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4001 		rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
4002 		lpfc_set_loopback_flag(phba);
4003 		if (rc == MBX_NOT_FINISHED)
4004 			mempool_free(initlinkmbox, phba->mbox_mem_pool);
4005 
4006 		break;
4007 
4008 	case LPFC_DISC_AUTH:
4009 	/* Node Authentication timeout */
4010 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4011 				 "0227 Node Authentication timeout\n");
4012 		lpfc_disc_flush_list(vport);
4013 
4014 		/*
4015 		 * set port_state to PORT_READY if SLI2.
4016 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
4017 		 */
4018 		if (phba->sli_rev < LPFC_SLI_REV4) {
4019 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4020 				lpfc_issue_reg_vpi(phba, vport);
4021 			else  {	/* NPIV Not enabled */
4022 				lpfc_issue_clear_la(phba, vport);
4023 				vport->port_state = LPFC_VPORT_READY;
4024 			}
4025 		}
4026 		break;
4027 
4028 	case LPFC_VPORT_READY:
4029 		if (vport->fc_flag & FC_RSCN_MODE) {
4030 			lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4031 					 "0231 RSCN timeout Data: x%x "
4032 					 "x%x\n",
4033 					 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
4034 
4035 			/* Cleanup any outstanding ELS commands */
4036 			lpfc_els_flush_cmd(vport);
4037 
4038 			lpfc_els_flush_rscn(vport);
4039 			lpfc_disc_flush_list(vport);
4040 		}
4041 		break;
4042 
4043 	default:
4044 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4045 				 "0273 Unexpected discovery timeout, "
4046 				 "vport State x%x\n", vport->port_state);
4047 		break;
4048 	}
4049 
4050 	switch (phba->link_state) {
4051 	case LPFC_CLEAR_LA:
4052 				/* CLEAR LA timeout */
4053 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4054 				 "0228 CLEAR LA timeout\n");
4055 		clrlaerr = 1;
4056 		break;
4057 
4058 	case LPFC_LINK_UP:
4059 		lpfc_issue_clear_la(phba, vport);
4060 		/* Drop thru */
4061 	case LPFC_LINK_UNKNOWN:
4062 	case LPFC_WARM_START:
4063 	case LPFC_INIT_START:
4064 	case LPFC_INIT_MBX_CMDS:
4065 	case LPFC_LINK_DOWN:
4066 	case LPFC_HBA_ERROR:
4067 		lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4068 				 "0230 Unexpected timeout, hba link "
4069 				 "state x%x\n", phba->link_state);
4070 		clrlaerr = 1;
4071 		break;
4072 
4073 	case LPFC_HBA_READY:
4074 		break;
4075 	}
4076 
4077 	if (clrlaerr) {
4078 		lpfc_disc_flush_list(vport);
4079 		psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4080 		psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4081 		psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4082 		vport->port_state = LPFC_VPORT_READY;
4083 	}
4084 
4085 	return;
4086 }
4087 
4088 /*
4089  * This routine handles processing a NameServer REG_LOGIN mailbox
4090  * command upon completion. It is setup in the LPFC_MBOXQ
4091  * as the completion routine when the command is
4092  * handed off to the SLI layer.
4093  */
4094 void
4095 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4096 {
4097 	MAILBOX_t *mb = &pmb->u.mb;
4098 	struct lpfc_dmabuf   *mp = (struct lpfc_dmabuf *) (pmb->context1);
4099 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4100 	struct lpfc_vport    *vport = pmb->vport;
4101 
4102 	pmb->context1 = NULL;
4103 
4104 	ndlp->nlp_rpi = mb->un.varWords[0];
4105 	ndlp->nlp_flag |= NLP_RPI_VALID;
4106 	ndlp->nlp_type |= NLP_FABRIC;
4107 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4108 
4109 	/*
4110 	 * Start issuing Fabric-Device Management Interface (FDMI) command to
4111 	 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4112 	 * fdmi-on=2 (supporting RPA/hostnmae)
4113 	 */
4114 
4115 	if (vport->cfg_fdmi_on == 1)
4116 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4117 	else
4118 		mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
4119 
4120 	/* decrement the node reference count held for this callback
4121 	 * function.
4122 	 */
4123 	lpfc_nlp_put(ndlp);
4124 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
4125 	kfree(mp);
4126 	mempool_free(pmb, phba->mbox_mem_pool);
4127 
4128 	return;
4129 }
4130 
4131 static int
4132 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4133 {
4134 	uint16_t *rpi = param;
4135 
4136 	return ndlp->nlp_rpi == *rpi;
4137 }
4138 
4139 static int
4140 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4141 {
4142 	return memcmp(&ndlp->nlp_portname, param,
4143 		      sizeof(ndlp->nlp_portname)) == 0;
4144 }
4145 
4146 static struct lpfc_nodelist *
4147 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
4148 {
4149 	struct lpfc_nodelist *ndlp;
4150 
4151 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4152 		if (filter(ndlp, param))
4153 			return ndlp;
4154 	}
4155 	return NULL;
4156 }
4157 
4158 /*
4159  * This routine looks up the ndlp lists for the given RPI. If rpi found it
4160  * returns the node list element pointer else return NULL.
4161  */
4162 struct lpfc_nodelist *
4163 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
4164 {
4165 	return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
4166 }
4167 
4168 /*
4169  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
4170  * returns the node element list pointer else return NULL.
4171  */
4172 struct lpfc_nodelist *
4173 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
4174 {
4175 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4176 	struct lpfc_nodelist *ndlp;
4177 
4178 	spin_lock_irq(shost->host_lock);
4179 	ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4180 	spin_unlock_irq(shost->host_lock);
4181 	return ndlp;
4182 }
4183 
4184 void
4185 lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4186 	      uint32_t did)
4187 {
4188 	memset(ndlp, 0, sizeof (struct lpfc_nodelist));
4189 
4190 	lpfc_initialize_node(vport, ndlp, did);
4191 	INIT_LIST_HEAD(&ndlp->nlp_listp);
4192 
4193 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4194 		"node init:       did:x%x",
4195 		ndlp->nlp_DID, 0, 0);
4196 
4197 	return;
4198 }
4199 
4200 /* This routine releases all resources associated with a specifc NPort's ndlp
4201  * and mempool_free's the nodelist.
4202  */
4203 static void
4204 lpfc_nlp_release(struct kref *kref)
4205 {
4206 	struct lpfc_hba *phba;
4207 	unsigned long flags;
4208 	struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4209 						  kref);
4210 
4211 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4212 		"node release:    did:x%x flg:x%x type:x%x",
4213 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4214 
4215 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4216 			"0279 lpfc_nlp_release: ndlp:x%p "
4217 			"usgmap:x%x refcnt:%d\n",
4218 			(void *)ndlp, ndlp->nlp_usg_map,
4219 			atomic_read(&ndlp->kref.refcount));
4220 
4221 	/* remove ndlp from action. */
4222 	lpfc_nlp_remove(ndlp->vport, ndlp);
4223 
4224 	/* clear the ndlp active flag for all release cases */
4225 	phba = ndlp->phba;
4226 	spin_lock_irqsave(&phba->ndlp_lock, flags);
4227 	NLP_CLR_NODE_ACT(ndlp);
4228 	spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4229 
4230 	/* free ndlp memory for final ndlp release */
4231 	if (NLP_CHK_FREE_REQ(ndlp)) {
4232 		kfree(ndlp->lat_data);
4233 		mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
4234 	}
4235 }
4236 
4237 /* This routine bumps the reference count for a ndlp structure to ensure
4238  * that one discovery thread won't free a ndlp while another discovery thread
4239  * is using it.
4240  */
4241 struct lpfc_nodelist *
4242 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4243 {
4244 	struct lpfc_hba *phba;
4245 	unsigned long flags;
4246 
4247 	if (ndlp) {
4248 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4249 			"node get:        did:x%x flg:x%x refcnt:x%x",
4250 			ndlp->nlp_DID, ndlp->nlp_flag,
4251 			atomic_read(&ndlp->kref.refcount));
4252 		/* The check of ndlp usage to prevent incrementing the
4253 		 * ndlp reference count that is in the process of being
4254 		 * released.
4255 		 */
4256 		phba = ndlp->phba;
4257 		spin_lock_irqsave(&phba->ndlp_lock, flags);
4258 		if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4259 			spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4260 			lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4261 				"0276 lpfc_nlp_get: ndlp:x%p "
4262 				"usgmap:x%x refcnt:%d\n",
4263 				(void *)ndlp, ndlp->nlp_usg_map,
4264 				atomic_read(&ndlp->kref.refcount));
4265 			return NULL;
4266 		} else
4267 			kref_get(&ndlp->kref);
4268 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4269 	}
4270 	return ndlp;
4271 }
4272 
4273 /* This routine decrements the reference count for a ndlp structure. If the
4274  * count goes to 0, this indicates the the associated nodelist should be
4275  * freed. Returning 1 indicates the ndlp resource has been released; on the
4276  * other hand, returning 0 indicates the ndlp resource has not been released
4277  * yet.
4278  */
4279 int
4280 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4281 {
4282 	struct lpfc_hba *phba;
4283 	unsigned long flags;
4284 
4285 	if (!ndlp)
4286 		return 1;
4287 
4288 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4289 	"node put:        did:x%x flg:x%x refcnt:x%x",
4290 		ndlp->nlp_DID, ndlp->nlp_flag,
4291 		atomic_read(&ndlp->kref.refcount));
4292 	phba = ndlp->phba;
4293 	spin_lock_irqsave(&phba->ndlp_lock, flags);
4294 	/* Check the ndlp memory free acknowledge flag to avoid the
4295 	 * possible race condition that kref_put got invoked again
4296 	 * after previous one has done ndlp memory free.
4297 	 */
4298 	if (NLP_CHK_FREE_ACK(ndlp)) {
4299 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4300 		lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4301 				"0274 lpfc_nlp_put: ndlp:x%p "
4302 				"usgmap:x%x refcnt:%d\n",
4303 				(void *)ndlp, ndlp->nlp_usg_map,
4304 				atomic_read(&ndlp->kref.refcount));
4305 		return 1;
4306 	}
4307 	/* Check the ndlp inactivate log flag to avoid the possible
4308 	 * race condition that kref_put got invoked again after ndlp
4309 	 * is already in inactivating state.
4310 	 */
4311 	if (NLP_CHK_IACT_REQ(ndlp)) {
4312 		spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4313 		lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4314 				"0275 lpfc_nlp_put: ndlp:x%p "
4315 				"usgmap:x%x refcnt:%d\n",
4316 				(void *)ndlp, ndlp->nlp_usg_map,
4317 				atomic_read(&ndlp->kref.refcount));
4318 		return 1;
4319 	}
4320 	/* For last put, mark the ndlp usage flags to make sure no
4321 	 * other kref_get and kref_put on the same ndlp shall get
4322 	 * in between the process when the final kref_put has been
4323 	 * invoked on this ndlp.
4324 	 */
4325 	if (atomic_read(&ndlp->kref.refcount) == 1) {
4326 		/* Indicate ndlp is put to inactive state. */
4327 		NLP_SET_IACT_REQ(ndlp);
4328 		/* Acknowledge ndlp memory free has been seen. */
4329 		if (NLP_CHK_FREE_REQ(ndlp))
4330 			NLP_SET_FREE_ACK(ndlp);
4331 	}
4332 	spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4333 	/* Note, the kref_put returns 1 when decrementing a reference
4334 	 * count that was 1, it invokes the release callback function,
4335 	 * but it still left the reference count as 1 (not actually
4336 	 * performs the last decrementation). Otherwise, it actually
4337 	 * decrements the reference count and returns 0.
4338 	 */
4339 	return kref_put(&ndlp->kref, lpfc_nlp_release);
4340 }
4341 
4342 /* This routine free's the specified nodelist if it is not in use
4343  * by any other discovery thread. This routine returns 1 if the
4344  * ndlp has been freed. A return value of 0 indicates the ndlp is
4345  * not yet been released.
4346  */
4347 int
4348 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4349 {
4350 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4351 		"node not used:   did:x%x flg:x%x refcnt:x%x",
4352 		ndlp->nlp_DID, ndlp->nlp_flag,
4353 		atomic_read(&ndlp->kref.refcount));
4354 	if (atomic_read(&ndlp->kref.refcount) == 1)
4355 		if (lpfc_nlp_put(ndlp))
4356 			return 1;
4357 	return 0;
4358 }
4359 
4360 /**
4361  * lpfc_fcf_inuse - Check if FCF can be unregistered.
4362  * @phba: Pointer to hba context object.
4363  *
4364  * This function iterate through all FC nodes associated
4365  * will all vports to check if there is any node with
4366  * fc_rports associated with it. If there is an fc_rport
4367  * associated with the node, then the node is either in
4368  * discovered state or its devloss_timer is pending.
4369  */
4370 static int
4371 lpfc_fcf_inuse(struct lpfc_hba *phba)
4372 {
4373 	struct lpfc_vport **vports;
4374 	int i, ret = 0;
4375 	struct lpfc_nodelist *ndlp;
4376 	struct Scsi_Host  *shost;
4377 
4378 	vports = lpfc_create_vport_work_array(phba);
4379 
4380 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4381 		shost = lpfc_shost_from_vport(vports[i]);
4382 		spin_lock_irq(shost->host_lock);
4383 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4384 			if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4385 			  (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4386 				ret = 1;
4387 				spin_unlock_irq(shost->host_lock);
4388 				goto out;
4389 			} else {
4390 				lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4391 					"2624 RPI %x DID %x flg %x still "
4392 					"logged in\n",
4393 					ndlp->nlp_rpi, ndlp->nlp_DID,
4394 					ndlp->nlp_flag);
4395 				if (ndlp->nlp_flag & NLP_RPI_VALID)
4396 					ret = 1;
4397 			}
4398 		}
4399 		spin_unlock_irq(shost->host_lock);
4400 	}
4401 out:
4402 	lpfc_destroy_vport_work_array(phba, vports);
4403 	return ret;
4404 }
4405 
4406 /**
4407  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4408  * @phba: Pointer to hba context object.
4409  * @mboxq: Pointer to mailbox object.
4410  *
4411  * This function frees memory associated with the mailbox command.
4412  */
4413 static void
4414 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4415 {
4416 	struct lpfc_vport *vport = mboxq->vport;
4417 
4418 	if (mboxq->u.mb.mbxStatus) {
4419 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4420 			"2555 UNREG_VFI mbxStatus error x%x "
4421 			"HBA state x%x\n",
4422 			mboxq->u.mb.mbxStatus, vport->port_state);
4423 	}
4424 	mempool_free(mboxq, phba->mbox_mem_pool);
4425 	return;
4426 }
4427 
4428 /**
4429  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4430  * @phba: Pointer to hba context object.
4431  * @mboxq: Pointer to mailbox object.
4432  *
4433  * This function frees memory associated with the mailbox command.
4434  */
4435 static void
4436 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4437 {
4438 	struct lpfc_vport *vport = mboxq->vport;
4439 
4440 	if (mboxq->u.mb.mbxStatus) {
4441 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4442 			"2550 UNREG_FCFI mbxStatus error x%x "
4443 			"HBA state x%x\n",
4444 			mboxq->u.mb.mbxStatus, vport->port_state);
4445 	}
4446 	mempool_free(mboxq, phba->mbox_mem_pool);
4447 	return;
4448 }
4449 
4450 /**
4451  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4452  * @phba: Pointer to hba context object.
4453  *
4454  * This function check if there are any connected remote port for the FCF and
4455  * if all the devices are disconnected, this function unregister FCFI.
4456  * This function also tries to use another FCF for discovery.
4457  */
4458 void
4459 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4460 {
4461 	LPFC_MBOXQ_t *mbox;
4462 	int rc;
4463 	struct lpfc_vport **vports;
4464 	int i;
4465 
4466 	spin_lock_irq(&phba->hbalock);
4467 	/*
4468 	 * If HBA is not running in FIP mode or
4469 	 * If HBA does not support FCoE or
4470 	 * If FCF is not registered.
4471 	 * do nothing.
4472 	 */
4473 	if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4474 		!(phba->fcf.fcf_flag & FCF_REGISTERED) ||
4475 		(!(phba->hba_flag & HBA_FIP_SUPPORT))) {
4476 		spin_unlock_irq(&phba->hbalock);
4477 		return;
4478 	}
4479 	spin_unlock_irq(&phba->hbalock);
4480 
4481 	if (lpfc_fcf_inuse(phba))
4482 		return;
4483 
4484 	/* At this point, all discovery is aborted */
4485 	phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4486 
4487 	/* Unregister VPIs */
4488 	vports = lpfc_create_vport_work_array(phba);
4489 	if (vports &&
4490 		(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
4491 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4492 			lpfc_mbx_unreg_vpi(vports[i]);
4493 			spin_lock_irq(&phba->hbalock);
4494 			vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
4495 			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
4496 			spin_unlock_irq(&phba->hbalock);
4497 		}
4498 	lpfc_destroy_vport_work_array(phba, vports);
4499 
4500 	/* Unregister VFI */
4501 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4502 	if (!mbox) {
4503 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4504 			"2556 UNREG_VFI mbox allocation failed"
4505 			"HBA state x%x\n",
4506 			phba->pport->port_state);
4507 		return;
4508 	}
4509 
4510 	lpfc_unreg_vfi(mbox, phba->pport);
4511 	mbox->vport = phba->pport;
4512 	mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4513 
4514 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4515 	if (rc == MBX_NOT_FINISHED) {
4516 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4517 			"2557 UNREG_VFI issue mbox failed rc x%x "
4518 			"HBA state x%x\n",
4519 			rc, phba->pport->port_state);
4520 		mempool_free(mbox, phba->mbox_mem_pool);
4521 		return;
4522 	}
4523 
4524 	/* Unregister FCF */
4525 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4526 	if (!mbox) {
4527 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4528 			"2551 UNREG_FCFI mbox allocation failed"
4529 			"HBA state x%x\n",
4530 			phba->pport->port_state);
4531 		return;
4532 	}
4533 
4534 	lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4535 	mbox->vport = phba->pport;
4536 	mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4537 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4538 
4539 	if (rc == MBX_NOT_FINISHED) {
4540 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4541 			"2552 UNREG_FCFI issue mbox failed rc x%x "
4542 			"HBA state x%x\n",
4543 			rc, phba->pport->port_state);
4544 		mempool_free(mbox, phba->mbox_mem_pool);
4545 		return;
4546 	}
4547 
4548 	spin_lock_irq(&phba->hbalock);
4549 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
4550 		FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
4551 		FCF_VALID_VLAN);
4552 	spin_unlock_irq(&phba->hbalock);
4553 
4554 	/*
4555 	 * If driver is not unloading, check if there is any other
4556 	 * FCF record that can be used for discovery.
4557 	 */
4558 	if ((phba->pport->load_flag & FC_UNLOADING) ||
4559 		(phba->link_state < LPFC_LINK_UP))
4560 		return;
4561 
4562 	rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4563 
4564 	if (rc)
4565 		lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4566 			"2553 lpfc_unregister_unused_fcf failed to read FCF"
4567 			" record HBA state x%x\n",
4568 			phba->pport->port_state);
4569 }
4570 
4571 /**
4572  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4573  * @phba: Pointer to hba context object.
4574  * @buff: Buffer containing the FCF connection table as in the config
4575  *         region.
4576  * This function create driver data structure for the FCF connection
4577  * record table read from config region 23.
4578  */
4579 static void
4580 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4581 	uint8_t *buff)
4582 {
4583 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4584 	struct lpfc_fcf_conn_hdr *conn_hdr;
4585 	struct lpfc_fcf_conn_rec *conn_rec;
4586 	uint32_t record_count;
4587 	int i;
4588 
4589 	/* Free the current connect table */
4590 	list_for_each_entry_safe(conn_entry, next_conn_entry,
4591 		&phba->fcf_conn_rec_list, list) {
4592 		list_del_init(&conn_entry->list);
4593 		kfree(conn_entry);
4594 	}
4595 
4596 	conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4597 	record_count = conn_hdr->length * sizeof(uint32_t)/
4598 		sizeof(struct lpfc_fcf_conn_rec);
4599 
4600 	conn_rec = (struct lpfc_fcf_conn_rec *)
4601 		(buff + sizeof(struct lpfc_fcf_conn_hdr));
4602 
4603 	for (i = 0; i < record_count; i++) {
4604 		if (!(conn_rec[i].flags & FCFCNCT_VALID))
4605 			continue;
4606 		conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4607 			GFP_KERNEL);
4608 		if (!conn_entry) {
4609 			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4610 				"2566 Failed to allocate connection"
4611 				" table entry\n");
4612 			return;
4613 		}
4614 
4615 		memcpy(&conn_entry->conn_rec, &conn_rec[i],
4616 			sizeof(struct lpfc_fcf_conn_rec));
4617 		conn_entry->conn_rec.vlan_tag =
4618 			le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4619 		conn_entry->conn_rec.flags =
4620 			le16_to_cpu(conn_entry->conn_rec.flags);
4621 		list_add_tail(&conn_entry->list,
4622 			&phba->fcf_conn_rec_list);
4623 	}
4624 }
4625 
4626 /**
4627  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4628  * @phba: Pointer to hba context object.
4629  * @buff: Buffer containing the FCoE parameter data structure.
4630  *
4631  *  This function update driver data structure with config
4632  *  parameters read from config region 23.
4633  */
4634 static void
4635 lpfc_read_fcoe_param(struct lpfc_hba *phba,
4636 			uint8_t *buff)
4637 {
4638 	struct lpfc_fip_param_hdr *fcoe_param_hdr;
4639 	struct lpfc_fcoe_params *fcoe_param;
4640 
4641 	fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4642 		buff;
4643 	fcoe_param = (struct lpfc_fcoe_params *)
4644 		(buff + sizeof(struct lpfc_fip_param_hdr));
4645 
4646 	if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4647 		(fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4648 		return;
4649 
4650 	if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4651 		phba->valid_vlan = 1;
4652 		phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4653 			0xFFF;
4654 	}
4655 
4656 	phba->fc_map[0] = fcoe_param->fc_map[0];
4657 	phba->fc_map[1] = fcoe_param->fc_map[1];
4658 	phba->fc_map[2] = fcoe_param->fc_map[2];
4659 	return;
4660 }
4661 
4662 /**
4663  * lpfc_get_rec_conf23 - Get a record type in config region data.
4664  * @buff: Buffer containing config region 23 data.
4665  * @size: Size of the data buffer.
4666  * @rec_type: Record type to be searched.
4667  *
4668  * This function searches config region data to find the begining
4669  * of the record specified by record_type. If record found, this
4670  * function return pointer to the record else return NULL.
4671  */
4672 static uint8_t *
4673 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4674 {
4675 	uint32_t offset = 0, rec_length;
4676 
4677 	if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4678 		(size < sizeof(uint32_t)))
4679 		return NULL;
4680 
4681 	rec_length = buff[offset + 1];
4682 
4683 	/*
4684 	 * One TLV record has one word header and number of data words
4685 	 * specified in the rec_length field of the record header.
4686 	 */
4687 	while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4688 		<= size) {
4689 		if (buff[offset] == rec_type)
4690 			return &buff[offset];
4691 
4692 		if (buff[offset] == LPFC_REGION23_LAST_REC)
4693 			return NULL;
4694 
4695 		offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4696 		rec_length = buff[offset + 1];
4697 	}
4698 	return NULL;
4699 }
4700 
4701 /**
4702  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4703  * @phba: Pointer to lpfc_hba data structure.
4704  * @buff: Buffer containing config region 23 data.
4705  * @size: Size of the data buffer.
4706  *
4707  * This fuction parse the FCoE config parameters in config region 23 and
4708  * populate driver data structure with the parameters.
4709  */
4710 void
4711 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4712 		uint8_t *buff,
4713 		uint32_t size)
4714 {
4715 	uint32_t offset = 0, rec_length;
4716 	uint8_t *rec_ptr;
4717 
4718 	/*
4719 	 * If data size is less than 2 words signature and version cannot be
4720 	 * verified.
4721 	 */
4722 	if (size < 2*sizeof(uint32_t))
4723 		return;
4724 
4725 	/* Check the region signature first */
4726 	if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4727 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4728 			"2567 Config region 23 has bad signature\n");
4729 		return;
4730 	}
4731 
4732 	offset += 4;
4733 
4734 	/* Check the data structure version */
4735 	if (buff[offset] != LPFC_REGION23_VERSION) {
4736 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4737 			"2568 Config region 23 has bad version\n");
4738 		return;
4739 	}
4740 	offset += 4;
4741 
4742 	rec_length = buff[offset + 1];
4743 
4744 	/* Read FCoE param record */
4745 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4746 			size - offset, FCOE_PARAM_TYPE);
4747 	if (rec_ptr)
4748 		lpfc_read_fcoe_param(phba, rec_ptr);
4749 
4750 	/* Read FCF connection table */
4751 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4752 		size - offset, FCOE_CONN_TBL_TYPE);
4753 	if (rec_ptr)
4754 		lpfc_read_fcf_conn_tbl(phba, rec_ptr);
4755 
4756 }
4757