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