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