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