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