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