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