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