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