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