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