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