File Index
Files and directories
Sign in
FILE

lpfc_hbadisc.c

File preview with syntax highlighting.

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