linux/drivers/scsi/lpfc/lpfc_hbadisc.c
<<
>>
Prefs
   1/*******************************************************************
   2 * This file is part of the Emulex Linux Device Driver for         *
   3 * Fibre Channel Host Bus Adapters.                                *
   4 * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
   5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
   6 * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
   7 * EMULEX and SLI are trademarks of Emulex.                        *
   8 * www.broadcom.com                                                *
   9 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
  10 *                                                                 *
  11 * This program is free software; you can redistribute it and/or   *
  12 * modify it under the terms of version 2 of the GNU General       *
  13 * Public License as published by the Free Software Foundation.    *
  14 * This program is distributed in the hope that it will be useful. *
  15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
  16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
  17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
  18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
  20 * more details, a copy of which can be found in the file COPYING  *
  21 * included with this package.                                     *
  22 *******************************************************************/
  23
  24#include <linux/blkdev.h>
  25#include <linux/delay.h>
  26#include <linux/slab.h>
  27#include <linux/pci.h>
  28#include <linux/kthread.h>
  29#include <linux/interrupt.h>
  30#include <linux/lockdep.h>
  31#include <linux/utsname.h>
  32
  33#include <scsi/scsi.h>
  34#include <scsi/scsi_device.h>
  35#include <scsi/scsi_host.h>
  36#include <scsi/scsi_transport_fc.h>
  37#include <scsi/fc/fc_fs.h>
  38
  39#include "lpfc_hw4.h"
  40#include "lpfc_hw.h"
  41#include "lpfc_nl.h"
  42#include "lpfc_disc.h"
  43#include "lpfc_sli.h"
  44#include "lpfc_sli4.h"
  45#include "lpfc.h"
  46#include "lpfc_scsi.h"
  47#include "lpfc_nvme.h"
  48#include "lpfc_logmsg.h"
  49#include "lpfc_crtn.h"
  50#include "lpfc_vport.h"
  51#include "lpfc_debugfs.h"
  52
  53/* AlpaArray for assignment of scsid for scan-down and bind_method */
  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        rdata = rport->dd_data;
 104        if (!rdata) {
 105                pr_err("**** %s: NULL dd_data on rport x%px SID x%x\n",
 106                       __func__, rport, rport->scsi_target_id);
 107                return -EINVAL;
 108        }
 109
 110        ndlp = rdata->pnode;
 111        if (!rdata->pnode) {
 112                pr_err("**** %s: NULL ndlp on rport x%px SID x%x\n",
 113                       __func__, rport, rport->scsi_target_id);
 114                return -EINVAL;
 115        }
 116
 117        if (!ndlp->vport) {
 118                pr_err("**** %s: Null vport on ndlp x%px, DID x%x rport x%px "
 119                       "SID x%x\n", __func__, ndlp, ndlp->nlp_DID, rport,
 120                       rport->scsi_target_id);
 121                return -EINVAL;
 122        }
 123        return 0;
 124}
 125
 126void
 127lpfc_terminate_rport_io(struct fc_rport *rport)
 128{
 129        struct lpfc_rport_data *rdata;
 130        struct lpfc_nodelist *ndlp;
 131        struct lpfc_vport *vport;
 132
 133        if (lpfc_rport_invalid(rport))
 134                return;
 135
 136        rdata = rport->dd_data;
 137        ndlp = rdata->pnode;
 138        vport = ndlp->vport;
 139        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
 140                              "rport terminate: sid:x%x did:x%x flg:x%x",
 141                              ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
 142
 143        if (ndlp->nlp_sid != NLP_NO_SID)
 144                lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
 145}
 146
 147/*
 148 * This function will be called when dev_loss_tmo fire.
 149 */
 150void
 151lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
 152{
 153        struct lpfc_nodelist *ndlp;
 154        struct lpfc_vport *vport;
 155        struct lpfc_hba   *phba;
 156        struct lpfc_work_evt *evtp;
 157        unsigned long iflags;
 158
 159        ndlp = ((struct lpfc_rport_data *)rport->dd_data)->pnode;
 160        if (!ndlp)
 161                return;
 162
 163        vport = ndlp->vport;
 164        phba  = vport->phba;
 165
 166        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
 167                "rport devlosscb: sid:x%x did:x%x flg:x%x",
 168                ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
 169
 170        lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
 171                         "3181 dev_loss_callbk x%06x, rport x%px flg x%x "
 172                         "load_flag x%x refcnt %d\n",
 173                         ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag,
 174                         vport->load_flag, kref_read(&ndlp->kref));
 175
 176        /* Don't schedule a worker thread event if the vport is going down.
 177         * The teardown process cleans up the node via lpfc_drop_node.
 178         */
 179        if (vport->load_flag & FC_UNLOADING) {
 180                ((struct lpfc_rport_data *)rport->dd_data)->pnode = NULL;
 181                ndlp->rport = NULL;
 182
 183                ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
 184
 185                /* Remove the node reference from remote_port_add now.
 186                 * The driver will not call remote_port_delete.
 187                 */
 188                lpfc_nlp_put(ndlp);
 189                return;
 190        }
 191
 192        if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
 193                return;
 194
 195        if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
 196                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 197                                 "6789 rport name %llx != node port name %llx",
 198                                 rport->port_name,
 199                                 wwn_to_u64(ndlp->nlp_portname.u.wwn));
 200
 201        evtp = &ndlp->dev_loss_evt;
 202
 203        if (!list_empty(&evtp->evt_listp)) {
 204                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 205                                 "6790 rport name %llx dev_loss_evt pending\n",
 206                                 rport->port_name);
 207                return;
 208        }
 209
 210        spin_lock_irqsave(&ndlp->lock, iflags);
 211        ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
 212        ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
 213
 214        /*
 215         * The backend does not expect any more calls associated with this
 216         * rport. Remove the association between rport and ndlp.
 217         */
 218        ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
 219        ((struct lpfc_rport_data *)rport->dd_data)->pnode = NULL;
 220        ndlp->rport = NULL;
 221        spin_unlock_irqrestore(&ndlp->lock, iflags);
 222
 223        /* We need to hold the node by incrementing the reference
 224         * count until this queued work is done
 225         */
 226        evtp->evt_arg1 = lpfc_nlp_get(ndlp);
 227
 228        spin_lock_irqsave(&phba->hbalock, iflags);
 229        if (evtp->evt_arg1) {
 230                evtp->evt = LPFC_EVT_DEV_LOSS;
 231                list_add_tail(&evtp->evt_listp, &phba->work_list);
 232                lpfc_worker_wake_up(phba);
 233        }
 234        spin_unlock_irqrestore(&phba->hbalock, iflags);
 235
 236        return;
 237}
 238
 239/**
 240 * lpfc_check_inactive_vmid_one - VMID inactivity checker for a vport
 241 * @vport: Pointer to vport context object.
 242 *
 243 * This function checks for idle VMID entries related to a particular vport. If
 244 * found unused/idle, free them accordingly.
 245 **/
 246static void lpfc_check_inactive_vmid_one(struct lpfc_vport *vport)
 247{
 248        u16 keep;
 249        u32 difftime = 0, r, bucket;
 250        u64 *lta;
 251        int cpu;
 252        struct lpfc_vmid *vmp;
 253
 254        write_lock(&vport->vmid_lock);
 255
 256        if (!vport->cur_vmid_cnt)
 257                goto out;
 258
 259        /* iterate through the table */
 260        hash_for_each(vport->hash_table, bucket, vmp, hnode) {
 261                keep = 0;
 262                if (vmp->flag & LPFC_VMID_REGISTERED) {
 263                        /* check if the particular VMID is in use */
 264                        /* for all available per cpu variable */
 265                        for_each_possible_cpu(cpu) {
 266                                /* if last access time is less than timeout */
 267                                lta = per_cpu_ptr(vmp->last_io_time, cpu);
 268                                if (!lta)
 269                                        continue;
 270                                difftime = (jiffies) - (*lta);
 271                                if ((vport->vmid_inactivity_timeout *
 272                                     JIFFIES_PER_HR) > difftime) {
 273                                        keep = 1;
 274                                        break;
 275                                }
 276                        }
 277
 278                        /* if none of the cpus have been used by the vm, */
 279                        /*  remove the entry if already registered */
 280                        if (!keep) {
 281                                /* mark the entry for deregistration */
 282                                vmp->flag = LPFC_VMID_DE_REGISTER;
 283                                write_unlock(&vport->vmid_lock);
 284                                if (vport->vmid_priority_tagging)
 285                                        r = lpfc_vmid_uvem(vport, vmp, false);
 286                                else
 287                                        r = lpfc_vmid_cmd(vport,
 288                                                          SLI_CTAS_DAPP_IDENT,
 289                                                          vmp);
 290
 291                                /* decrement number of active vms and mark */
 292                                /* entry in slot as free */
 293                                write_lock(&vport->vmid_lock);
 294                                if (!r) {
 295                                        struct lpfc_vmid *ht = vmp;
 296
 297                                        vport->cur_vmid_cnt--;
 298                                        ht->flag = LPFC_VMID_SLOT_FREE;
 299                                        free_percpu(ht->last_io_time);
 300                                        ht->last_io_time = NULL;
 301                                        hash_del(&ht->hnode);
 302                                }
 303                        }
 304                }
 305        }
 306 out:
 307        write_unlock(&vport->vmid_lock);
 308}
 309
 310/**
 311 * lpfc_check_inactive_vmid - VMID inactivity checker
 312 * @phba: Pointer to hba context object.
 313 *
 314 * This function is called from the worker thread to determine if an entry in
 315 * the VMID table can be released since there was no I/O activity seen from that
 316 * particular VM for the specified time. When this happens, the entry in the
 317 * table is released and also the resources on the switch cleared.
 318 **/
 319
 320static void lpfc_check_inactive_vmid(struct lpfc_hba *phba)
 321{
 322        struct lpfc_vport *vport;
 323        struct lpfc_vport **vports;
 324        int i;
 325
 326        vports = lpfc_create_vport_work_array(phba);
 327        if (!vports)
 328                return;
 329
 330        for (i = 0; i <= phba->max_vports; i++) {
 331                if ((!vports[i]) && (i == 0))
 332                        vport = phba->pport;
 333                else
 334                        vport = vports[i];
 335                if (!vport)
 336                        break;
 337
 338                lpfc_check_inactive_vmid_one(vport);
 339        }
 340        lpfc_destroy_vport_work_array(phba, vports);
 341}
 342
 343/**
 344 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
 345 * @ndlp: Pointer to remote node object.
 346 *
 347 * This function is called from the worker thread when devloss timeout timer
 348 * expires. For SLI4 host, this routine shall return 1 when at lease one
 349 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
 350 * routine shall return 0 when there is no remote node is still in use of FCF
 351 * when devloss timeout happened to this @ndlp.
 352 **/
 353static int
 354lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
 355{
 356        struct lpfc_vport *vport;
 357        struct lpfc_hba   *phba;
 358        uint8_t *name;
 359        int warn_on = 0;
 360        int fcf_inuse = 0;
 361        unsigned long iflags;
 362
 363        vport = ndlp->vport;
 364        name = (uint8_t *)&ndlp->nlp_portname;
 365        phba = vport->phba;
 366
 367        spin_lock_irqsave(&ndlp->lock, iflags);
 368        ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
 369        spin_unlock_irqrestore(&ndlp->lock, iflags);
 370
 371        if (phba->sli_rev == LPFC_SLI_REV4)
 372                fcf_inuse = lpfc_fcf_inuse(phba);
 373
 374        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
 375                              "rport devlosstmo:did:x%x type:x%x id:x%x",
 376                              ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_sid);
 377
 378        lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
 379                         "3182 %s x%06x, nflag x%x xflags x%x refcnt %d\n",
 380                         __func__, ndlp->nlp_DID, ndlp->nlp_flag,
 381                         ndlp->fc4_xpt_flags, kref_read(&ndlp->kref));
 382
 383        /* If the driver is recovering the rport, ignore devloss. */
 384        if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
 385                lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
 386                                 "0284 Devloss timeout Ignored on "
 387                                 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
 388                                 "NPort x%x\n",
 389                                 *name, *(name+1), *(name+2), *(name+3),
 390                                 *(name+4), *(name+5), *(name+6), *(name+7),
 391                                 ndlp->nlp_DID);
 392                return fcf_inuse;
 393        }
 394
 395        /* Fabric nodes are done. */
 396        if (ndlp->nlp_type & NLP_FABRIC) {
 397                lpfc_nlp_put(ndlp);
 398                return fcf_inuse;
 399        }
 400
 401        if (ndlp->nlp_sid != NLP_NO_SID) {
 402                warn_on = 1;
 403                lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
 404        }
 405
 406        if (warn_on) {
 407                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
 408                                 "0203 Devloss timeout on "
 409                                 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
 410                                 "NPort x%06x Data: x%x x%x x%x\n",
 411                                 *name, *(name+1), *(name+2), *(name+3),
 412                                 *(name+4), *(name+5), *(name+6), *(name+7),
 413                                 ndlp->nlp_DID, ndlp->nlp_flag,
 414                                 ndlp->nlp_state, ndlp->nlp_rpi);
 415        } else {
 416                lpfc_printf_vlog(vport, KERN_INFO, LOG_TRACE_EVENT,
 417                                 "0204 Devloss timeout on "
 418                                 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
 419                                 "NPort x%06x Data: x%x x%x x%x\n",
 420                                 *name, *(name+1), *(name+2), *(name+3),
 421                                 *(name+4), *(name+5), *(name+6), *(name+7),
 422                                 ndlp->nlp_DID, ndlp->nlp_flag,
 423                                 ndlp->nlp_state, ndlp->nlp_rpi);
 424        }
 425
 426        if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD))
 427                lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
 428
 429        return fcf_inuse;
 430}
 431
 432static void lpfc_check_vmid_qfpa_issue(struct lpfc_hba *phba)
 433{
 434        struct lpfc_vport *vport;
 435        struct lpfc_vport **vports;
 436        int i;
 437
 438        vports = lpfc_create_vport_work_array(phba);
 439        if (!vports)
 440                return;
 441
 442        for (i = 0; i <= phba->max_vports; i++) {
 443                if ((!vports[i]) && (i == 0))
 444                        vport = phba->pport;
 445                else
 446                        vport = vports[i];
 447                if (!vport)
 448                        break;
 449
 450                if (vport->vmid_flag & LPFC_VMID_ISSUE_QFPA) {
 451                        if (!lpfc_issue_els_qfpa(vport))
 452                                vport->vmid_flag &= ~LPFC_VMID_ISSUE_QFPA;
 453                }
 454        }
 455        lpfc_destroy_vport_work_array(phba, vports);
 456}
 457
 458/**
 459 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
 460 * @phba: Pointer to hba context object.
 461 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
 462 * @nlp_did: remote node identifer with devloss timeout.
 463 *
 464 * This function is called from the worker thread after invoking devloss
 465 * timeout handler and releasing the reference count for the ndlp with
 466 * which the devloss timeout was handled for SLI4 host. For the devloss
 467 * timeout of the last remote node which had been in use of FCF, when this
 468 * routine is invoked, it shall be guaranteed that none of the remote are
 469 * in-use of FCF. When devloss timeout to the last remote using the FCF,
 470 * if the FIP engine is neither in FCF table scan process nor roundrobin
 471 * failover process, the in-use FCF shall be unregistered. If the FIP
 472 * engine is in FCF discovery process, the devloss timeout state shall
 473 * be set for either the FCF table scan process or roundrobin failover
 474 * process to unregister the in-use FCF.
 475 **/
 476static void
 477lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
 478                                    uint32_t nlp_did)
 479{
 480        /* If devloss timeout happened to a remote node when FCF had no
 481         * longer been in-use, do nothing.
 482         */
 483        if (!fcf_inuse)
 484                return;
 485
 486        if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
 487                spin_lock_irq(&phba->hbalock);
 488                if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
 489                        if (phba->hba_flag & HBA_DEVLOSS_TMO) {
 490                                spin_unlock_irq(&phba->hbalock);
 491                                return;
 492                        }
 493                        phba->hba_flag |= HBA_DEVLOSS_TMO;
 494                        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 495                                        "2847 Last remote node (x%x) using "
 496                                        "FCF devloss tmo\n", nlp_did);
 497                }
 498                if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
 499                        spin_unlock_irq(&phba->hbalock);
 500                        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 501                                        "2868 Devloss tmo to FCF rediscovery "
 502                                        "in progress\n");
 503                        return;
 504                }
 505                if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
 506                        spin_unlock_irq(&phba->hbalock);
 507                        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 508                                        "2869 Devloss tmo to idle FIP engine, "
 509                                        "unreg in-use FCF and rescan.\n");
 510                        /* Unregister in-use FCF and rescan */
 511                        lpfc_unregister_fcf_rescan(phba);
 512                        return;
 513                }
 514                spin_unlock_irq(&phba->hbalock);
 515                if (phba->hba_flag & FCF_TS_INPROG)
 516                        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 517                                        "2870 FCF table scan in progress\n");
 518                if (phba->hba_flag & FCF_RR_INPROG)
 519                        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
 520                                        "2871 FLOGI roundrobin FCF failover "
 521                                        "in progress\n");
 522        }
 523        lpfc_unregister_unused_fcf(phba);
 524}
 525
 526/**
 527 * lpfc_alloc_fast_evt - Allocates data structure for posting event
 528 * @phba: Pointer to hba context object.
 529 *
 530 * This function is called from the functions which need to post
 531 * events from interrupt context. This function allocates data
 532 * structure required for posting event. It also keeps track of
 533 * number of events pending and prevent event storm when there are
 534 * too many events.
 535 **/
 536struct lpfc_fast_path_event *
 537lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
 538        struct lpfc_fast_path_event *ret;
 539
 540        /* If there are lot of fast event do not exhaust memory due to this */
 541        if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
 542                return NULL;
 543
 544        ret = kzalloc(sizeof(struct lpfc_fast_path_event),
 545                        GFP_ATOMIC);
 546        if (ret) {
 547                atomic_inc(&phba->fast_event_count);
 548                INIT_LIST_HEAD(&ret->work_evt.evt_listp);
 549                ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
 550        }
 551        return ret;
 552}
 553
 554/**
 555 * lpfc_free_fast_evt - Frees event data structure
 556 * @phba: Pointer to hba context object.
 557 * @evt:  Event object which need to be freed.
 558 *
 559 * This function frees the data structure required for posting
 560 * events.
 561 **/
 562void
 563lpfc_free_fast_evt(struct lpfc_hba *phba,
 564                struct lpfc_fast_path_event *evt) {
 565
 566        atomic_dec(&phba->fast_event_count);
 567        kfree(evt);
 568}
 569
 570/**
 571 * lpfc_send_fastpath_evt - Posts events generated from fast path
 572 * @phba: Pointer to hba context object.
 573 * @evtp: Event data structure.
 574 *
 575 * This function is called from worker thread, when the interrupt
 576 * context need to post an event. This function posts the event
 577 * to fc transport netlink interface.
 578 **/
 579static void
 580lpfc_send_fastpath_evt(struct lpfc_hba *phba,
 581                struct lpfc_work_evt *evtp)
 582{
 583        unsigned long evt_category, evt_sub_category;
 584        struct lpfc_fast_path_event *fast_evt_data;
 585        char *evt_data;
 586        uint32_t evt_data_size;
 587        struct Scsi_Host *shost;
 588
 589        fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
 590                work_evt);
 591
 592        evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
 593        evt_sub_category = (unsigned long) fast_evt_data->un.
 594                        fabric_evt.subcategory;
 595        shost = lpfc_shost_from_vport(fast_evt_data->vport);
 596        if (evt_category == FC_REG_FABRIC_EVENT) {
 597                if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
 598                        evt_data = (char *) &fast_evt_data->un.read_check_error;
 599                        evt_data_size = sizeof(fast_evt_data->un.
 600                                read_check_error);
 601                } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
 602                        (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
 603                        evt_data = (char *) &fast_evt_data->un.fabric_evt;
 604                        evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
 605                } else {
 606                        lpfc_free_fast_evt(phba, fast_evt_data);
 607                        return;
 608                }
 609        } else if (evt_category == FC_REG_SCSI_EVENT) {
 610                switch (evt_sub_category) {
 611                case LPFC_EVENT_QFULL:
 612                case LPFC_EVENT_DEVBSY:
 613                        evt_data = (char *) &fast_evt_data->un.scsi_evt;
 614                        evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
 615                        break;
 616                case LPFC_EVENT_CHECK_COND:
 617                        evt_data = (char *) &fast_evt_data->un.check_cond_evt;
 618                        evt_data_size =  sizeof(fast_evt_data->un.
 619                                check_cond_evt);
 620                        break;
 621                case LPFC_EVENT_VARQUEDEPTH:
 622                        evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
 623                        evt_data_size = sizeof(fast_evt_data->un.
 624                                queue_depth_evt);
 625                        break;
 626                default:
 627                        lpfc_free_fast_evt(phba, fast_evt_data);
 628                        return;
 629                }
 630        } else {
 631                lpfc_free_fast_evt(phba, fast_evt_data);
 632                return;
 633        }
 634
 635        if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
 636                fc_host_post_vendor_event(shost,
 637                        fc_get_event_number(),
 638                        evt_data_size,
 639                        evt_data,
 640                        LPFC_NL_VENDOR_ID);
 641
 642        lpfc_free_fast_evt(phba, fast_evt_data);
 643        return;
 644}
 645
 646static void
 647lpfc_work_list_done(struct lpfc_hba *phba)
 648{
 649        struct lpfc_work_evt  *evtp = NULL;
 650        struct lpfc_nodelist  *ndlp;
 651        int free_evt;
 652        int fcf_inuse;
 653        uint32_t nlp_did;
 654
 655        spin_lock_irq(&phba->hbalock);
 656        while (!list_empty(&phba->work_list)) {
 657                list_remove_head((&phba->work_list), evtp, typeof(*evtp),
 658                                 evt_listp);
 659                spin_unlock_irq(&phba->hbalock);
 660                free_evt = 1;
 661                switch (evtp->evt) {
 662                case LPFC_EVT_ELS_RETRY:
 663                        ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
 664                        lpfc_els_retry_delay_handler(ndlp);
 665                        free_evt = 0; /* evt is part of ndlp */
 666                        /* decrement the node reference count held
 667                         * for this queued work
 668                         */
 669                        lpfc_nlp_put(ndlp);
 670                        break;
 671                case LPFC_EVT_DEV_LOSS:
 672                        ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
 673                        fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
 674                        free_evt = 0;
 675                        /* decrement the node reference count held for
 676                         * this queued work
 677                         */
 678                        nlp_did = ndlp->nlp_DID;
 679                        lpfc_nlp_put(ndlp);
 680                        if (phba->sli_rev == LPFC_SLI_REV4)
 681                                lpfc_sli4_post_dev_loss_tmo_handler(phba,
 682                                                                    fcf_inuse,
 683                                                                    nlp_did);
 684                        break;
 685                case LPFC_EVT_RECOVER_PORT:
 686                        ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
 687                        lpfc_sli_abts_recover_port(ndlp->vport, ndlp);
 688                        free_evt = 0;
 689                        /* decrement the node reference count held for
 690                         * this queued work
 691                         */
 692                        lpfc_nlp_put(ndlp);
 693                        break;
 694                case LPFC_EVT_ONLINE:
 695                        if (phba->link_state < LPFC_LINK_DOWN)
 696                                *(int *) (evtp->evt_arg1) = lpfc_online(phba);
 697                        else
 698                                *(int *) (evtp->evt_arg1) = 0;
 699                        complete((struct completion *)(evtp->evt_arg2));
 700                        break;
 701                case LPFC_EVT_OFFLINE_PREP:
 702                        if (phba->link_state >= LPFC_LINK_DOWN)
 703                                lpfc_offline_prep(phba, LPFC_MBX_WAIT);
 704                        *(int *)(evtp->evt_arg1) = 0;
 705                        complete((struct completion *)(evtp->evt_arg2));
 706                        break;
 707                case LPFC_EVT_OFFLINE:
 708                        lpfc_offline(phba);
 709                        lpfc_sli_brdrestart(phba);
 710                        *(int *)(evtp->evt_arg1) =
 711                                lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
 712                        lpfc_unblock_mgmt_io(phba);
 713                        complete((struct completion *)(evtp->evt_arg2));
 714                        break;
 715                case LPFC_EVT_WARM_START:
 716                        lpfc_offline(phba);
 717                        lpfc_reset_barrier(phba);
 718                        lpfc_sli_brdreset(phba);
 719                        lpfc_hba_down_post(phba);
 720                        *(int *)(evtp->evt_arg1) =
 721                                lpfc_sli_brdready(phba, HS_MBRDY);
 722                        lpfc_unblock_mgmt_io(phba);
 723                        complete((struct completion *)(evtp->evt_arg2));
 724                        break;
 725                case LPFC_EVT_KILL:
 726                        lpfc_offline(phba);
 727                        *(int *)(evtp->evt_arg1)
 728                                = (phba->pport->stopped)
 729                                        ? 0 : lpfc_sli_brdkill(phba);
 730                        lpfc_unblock_mgmt_io(phba);
 731                        complete((struct completion *)(evtp->evt_arg2));
 732                        break;
 733                case LPFC_EVT_FASTPATH_MGMT_EVT:
 734                        lpfc_send_fastpath_evt(phba, evtp);
 735                        free_evt = 0;
 736                        break;
 737                case LPFC_EVT_RESET_HBA:
 738                        if (!(phba->pport->load_flag & FC_UNLOADING))
 739                                lpfc_reset_hba(phba);
 740                        break;
 741                }
 742                if (free_evt)
 743                        kfree(evtp);
 744                spin_lock_irq(&phba->hbalock);
 745        }
 746        spin_unlock_irq(&phba->hbalock);
 747
 748}
 749
 750static void
 751lpfc_work_done(struct lpfc_hba *phba)
 752{
 753        struct lpfc_sli_ring *pring;
 754        uint32_t ha_copy, status, control, work_port_events;
 755        struct lpfc_vport **vports;
 756        struct lpfc_vport *vport;
 757        int i;
 758
 759        spin_lock_irq(&phba->hbalock);
 760        ha_copy = phba->work_ha;
 761        phba->work_ha = 0;
 762        spin_unlock_irq(&phba->hbalock);
 763
 764        /* First, try to post the next mailbox command to SLI4 device */
 765        if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
 766                lpfc_sli4_post_async_mbox(phba);
 767
 768        if (ha_copy & HA_ERATT)
 769                /* Handle the error attention event */
 770                lpfc_handle_eratt(phba);
 771
 772        if (ha_copy & HA_MBATT)
 773                lpfc_sli_handle_mb_event(phba);
 774
 775        if (ha_copy & HA_LATT)
 776                lpfc_handle_latt(phba);
 777
 778        /* Handle VMID Events */
 779        if (lpfc_is_vmid_enabled(phba)) {
 780                if (phba->pport->work_port_events &
 781                    WORKER_CHECK_VMID_ISSUE_QFPA) {
 782                        lpfc_check_vmid_qfpa_issue(phba);
 783                        phba->pport->work_port_events &=
 784                                ~WORKER_CHECK_VMID_ISSUE_QFPA;
 785                }
 786                if (phba->pport->work_port_events &
 787                    WORKER_CHECK_INACTIVE_VMID) {
 788                        lpfc_check_inactive_vmid(phba);
 789                        phba->pport->work_port_events &=
 790                            ~WORKER_CHECK_INACTIVE_VMID;
 791                }
 792        }
 793
 794        /* Process SLI4 events */
 795        if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
 796                if (phba->hba_flag & HBA_RRQ_ACTIVE)
 797                        lpfc_handle_rrq_active(phba);
 798                if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
 799                        lpfc_sli4_els_xri_abort_event_proc(phba);
 800                if (phba->hba_flag & ASYNC_EVENT)
 801                        lpfc_sli4_async_event_proc(phba);
 802                if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
 803                        spin_lock_irq(&phba->hbalock);
 804                        phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
 805                        spin_unlock_irq(&phba->hbalock);
 806                        lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
 807                }
 808                if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
 809                        lpfc_sli4_fcf_redisc_event_proc(phba);
 810        }
 811
 812        vports = lpfc_create_vport_work_array(phba);
 813        if (vports != NULL)
 814                for (i = 0; i <= phba->max_vports; i++) {
 815                        /*
 816                         * We could have no vports in array if unloading, so if
 817                         * this happens then just use the pport
 818                         */
 819                        if (vports[i] == NULL && i == 0)
 820                                vport = phba->pport;
 821                        else
 822                                vport = vports[i];
 823                        if (vport == NULL)
 824                                break;
 825                        spin_lock_irq(&vport->work_port_lock);
 826                        work_port_events = vport->work_port_events;
 827                        vport->work_port_events &= ~work_port_events;
 828                        spin_unlock_irq(&vport->work_port_lock);
 829                        if (work_port_events & WORKER_DISC_TMO)
 830                                lpfc_disc_timeout_handler(vport);
 831                        if (work_port_events & WORKER_ELS_TMO)
 832                                lpfc_els_timeout_handler(vport);
 833                        if (work_port_events & WORKER_HB_TMO)
 834                                lpfc_hb_timeout_handler(phba);
 835                        if (work_port_events & WORKER_MBOX_TMO)
 836                                lpfc_mbox_timeout_handler(phba);
 837                        if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
 838                                lpfc_unblock_fabric_iocbs(phba);
 839                        if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
 840                                lpfc_ramp_down_queue_handler(phba);
 841                        if (work_port_events & WORKER_DELAYED_DISC_TMO)
 842                                lpfc_delayed_disc_timeout_handler(vport);
 843                }
 844        lpfc_destroy_vport_work_array(phba, vports);
 845
 846        pring = lpfc_phba_elsring(phba);
 847        status = (ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
 848        status >>= (4*LPFC_ELS_RING);
 849        if (pring && (status & HA_RXMASK ||
 850                      pring->flag & LPFC_DEFERRED_RING_EVENT ||
 851                      phba->hba_flag & HBA_SP_QUEUE_EVT)) {
 852                if (pring->flag & LPFC_STOP_IOCB_EVENT) {
 853                        pring->flag |= LPFC_DEFERRED_RING_EVENT;
 854                        /* Preserve legacy behavior. */
 855                        if (!(phba->hba_flag & HBA_SP_QUEUE_EVT))
 856                                set_bit(LPFC_DATA_READY, &phba->data_flags);
 857                } else {
 858                        /* Driver could have abort request completed in queue
 859                         * when link goes down.  Allow for this transition.
 860                         */
 861                        if (phba->link_state >= LPFC_LINK_DOWN ||
 862                            phba->link_flag & LS_MDS_LOOPBACK) {
 863                                pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
 864                                lpfc_sli_handle_slow_ring_event(phba, pring,
 865                                                                (status &
 866                                                                HA_RXMASK));
 867                        }
 868                }
 869                if (phba->sli_rev == LPFC_SLI_REV4)
 870                        lpfc_drain_txq(phba);
 871                /*
 872                 * Turn on Ring interrupts
 873                 */
 874                if (phba->sli_rev <= LPFC_SLI_REV3) {
 875                        spin_lock_irq(&phba->hbalock);
 876                        control = readl(phba->HCregaddr);
 877                        if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
 878                                lpfc_debugfs_slow_ring_trc(phba,
 879                                        "WRK Enable ring: cntl:x%x hacopy:x%x",
 880                                        control, ha_copy, 0);
 881
 882                                control |= (HC_R0INT_ENA << LPFC_ELS_RING);
 883                                writel(control, phba->HCregaddr);
 884                                readl(phba->HCregaddr); /* flush */
 885                        } else {
 886                                lpfc_debugfs_slow_ring_trc(phba,
 887                                        "WRK Ring ok:     cntl:x%x hacopy:x%x",
 888                                        control, ha_copy, 0);
 889                        }
 890                        spin_unlock_irq(&phba->hbalock);
 891                }
 892        }
 893        lpfc_work_list_done(phba);
 894}
 895
 896int
 897lpfc_do_work(void *p)
 898{
 899        struct lpfc_hba *phba = p;
 900        int rc;
 901
 902        set_user_nice(current, MIN_NICE);
 903        current->flags |= PF_NOFREEZE;
 904        phba->data_flags = 0;
 905
 906        while (!kthread_should_stop()) {
 907                /* wait and check worker queue activities */
 908                rc = wait_event_interruptible(phba->work_waitq,
 909                                        (test_and_clear_bit(LPFC_DATA_READY,
 910                                                            &phba->data_flags)
 911                                         || kthread_should_stop()));
 912                /* Signal wakeup shall terminate the worker thread */
 913                if (rc) {
 914                        lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 915                                        "0433 Wakeup on signal: rc=x%x\n", rc);
 916                        break;
 917                }
 918
 919                /* Attend pending lpfc data processing */
 920                lpfc_work_done(phba);
 921        }
 922        phba->worker_thread = NULL;
 923        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
 924                        "0432 Worker thread stopped.\n");
 925        return 0;
 926}
 927
 928/*
 929 * This is only called to handle FC worker events. Since this a rare
 930 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
 931 * embedding it in the IOCB.
 932 */
 933int
 934lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
 935                      uint32_t evt)
 936{
 937        struct lpfc_work_evt  *evtp;
 938        unsigned long flags;
 939
 940        /*
 941         * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
 942         * be queued to worker thread for processing
 943         */
 944        evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
 945        if (!evtp)
 946                return 0;
 947
 948        evtp->evt_arg1  = arg1;
 949        evtp->evt_arg2  = arg2;
 950        evtp->evt       = evt;
 951
 952        spin_lock_irqsave(&phba->hbalock, flags);
 953        list_add_tail(&evtp->evt_listp, &phba->work_list);
 954        spin_unlock_irqrestore(&phba->hbalock, flags);
 955
 956        lpfc_worker_wake_up(phba);
 957
 958        return 1;
 959}
 960
 961void
 962lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
 963{
 964        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
 965        struct lpfc_hba  *phba = vport->phba;
 966        struct lpfc_nodelist *ndlp, *next_ndlp;
 967
 968        list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
 969                if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
 970                        continue;
 971
 972                if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
 973                    ((vport->port_type == LPFC_NPIV_PORT) &&
 974                     ((ndlp->nlp_DID == NameServer_DID) ||
 975                      (ndlp->nlp_DID == FDMI_DID) ||
 976                      (ndlp->nlp_DID == Fabric_Cntl_DID))))
 977                        lpfc_unreg_rpi(vport, ndlp);
 978
 979                /* Leave Fabric nodes alone on link down */
 980                if ((phba->sli_rev < LPFC_SLI_REV4) &&
 981                    (!remove && ndlp->nlp_type & NLP_FABRIC))
 982                        continue;
 983
 984                /* Notify transport of connectivity loss to trigger cleanup. */
 985                if (phba->nvmet_support &&
 986                    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
 987                        lpfc_nvmet_invalidate_host(phba, ndlp);
 988
 989                lpfc_disc_state_machine(vport, ndlp, NULL,
 990                                        remove
 991                                        ? NLP_EVT_DEVICE_RM
 992                                        : NLP_EVT_DEVICE_RECOVERY);
 993        }
 994        if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
 995                if (phba->sli_rev == LPFC_SLI_REV4)
 996                        lpfc_sli4_unreg_all_rpis(vport);
 997                lpfc_mbx_unreg_vpi(vport);
 998                spin_lock_irq(shost->host_lock);
 999                vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1000                spin_unlock_irq(shost->host_lock);
1001        }
1002}
1003
1004void
1005lpfc_port_link_failure(struct lpfc_vport *vport)
1006{
1007        lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
1008
1009        /* Cleanup any outstanding received buffers */
1010        lpfc_cleanup_rcv_buffers(vport);
1011
1012        /* Cleanup any outstanding RSCN activity */
1013        lpfc_els_flush_rscn(vport);
1014
1015        /* Cleanup any outstanding ELS commands */
1016        lpfc_els_flush_cmd(vport);
1017
1018        lpfc_cleanup_rpis(vport, 0);
1019
1020        /* Turn off discovery timer if its running */
1021        lpfc_can_disctmo(vport);
1022}
1023
1024void
1025lpfc_linkdown_port(struct lpfc_vport *vport)
1026{
1027        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1028
1029        if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1030                fc_host_post_event(shost, fc_get_event_number(),
1031                                   FCH_EVT_LINKDOWN, 0);
1032
1033        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1034                "Link Down:       state:x%x rtry:x%x flg:x%x",
1035                vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1036
1037        lpfc_port_link_failure(vport);
1038
1039        /* Stop delayed Nport discovery */
1040        spin_lock_irq(shost->host_lock);
1041        vport->fc_flag &= ~FC_DISC_DELAYED;
1042        spin_unlock_irq(shost->host_lock);
1043        del_timer_sync(&vport->delayed_disc_tmo);
1044}
1045
1046int
1047lpfc_linkdown(struct lpfc_hba *phba)
1048{
1049        struct lpfc_vport *vport = phba->pport;
1050        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1051        struct lpfc_vport **vports;
1052        LPFC_MBOXQ_t          *mb;
1053        int i;
1054
1055        if (phba->link_state == LPFC_LINK_DOWN)
1056                return 0;
1057
1058        /* Block all SCSI stack I/Os */
1059        lpfc_scsi_dev_block(phba);
1060
1061        phba->defer_flogi_acc_flag = false;
1062
1063        spin_lock_irq(&phba->hbalock);
1064        phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1065        spin_unlock_irq(&phba->hbalock);
1066        if (phba->link_state > LPFC_LINK_DOWN) {
1067                phba->link_state = LPFC_LINK_DOWN;
1068                if (phba->sli4_hba.conf_trunk) {
1069                        phba->trunk_link.link0.state = 0;
1070                        phba->trunk_link.link1.state = 0;
1071                        phba->trunk_link.link2.state = 0;
1072                        phba->trunk_link.link3.state = 0;
1073                        phba->sli4_hba.link_state.logical_speed =
1074                                                LPFC_LINK_SPEED_UNKNOWN;
1075                }
1076                spin_lock_irq(shost->host_lock);
1077                phba->pport->fc_flag &= ~FC_LBIT;
1078                spin_unlock_irq(shost->host_lock);
1079        }
1080        vports = lpfc_create_vport_work_array(phba);
1081        if (vports != NULL) {
1082                for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1083                        /* Issue a LINK DOWN event to all nodes */
1084                        lpfc_linkdown_port(vports[i]);
1085
1086                        vports[i]->fc_myDID = 0;
1087
1088                        if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1089                            (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1090                                if (phba->nvmet_support)
1091                                        lpfc_nvmet_update_targetport(phba);
1092                                else
1093                                        lpfc_nvme_update_localport(vports[i]);
1094                        }
1095                }
1096        }
1097        lpfc_destroy_vport_work_array(phba, vports);
1098
1099        /* Clean up any SLI3 firmware default rpi's */
1100        if (phba->sli_rev > LPFC_SLI_REV3)
1101                goto skip_unreg_did;
1102
1103        mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1104        if (mb) {
1105                lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
1106                mb->vport = vport;
1107                mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1108                if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
1109                    == MBX_NOT_FINISHED) {
1110                        mempool_free(mb, phba->mbox_mem_pool);
1111                }
1112        }
1113
1114 skip_unreg_did:
1115        /* Setup myDID for link up if we are in pt2pt mode */
1116        if (phba->pport->fc_flag & FC_PT2PT) {
1117                mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1118                if (mb) {
1119                        lpfc_config_link(phba, mb);
1120                        mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1121                        mb->vport = vport;
1122                        if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
1123                            == MBX_NOT_FINISHED) {
1124                                mempool_free(mb, phba->mbox_mem_pool);
1125                        }
1126                }
1127                spin_lock_irq(shost->host_lock);
1128                phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
1129                phba->pport->rcv_flogi_cnt = 0;
1130                spin_unlock_irq(shost->host_lock);
1131        }
1132        return 0;
1133}
1134
1135static void
1136lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
1137{
1138        struct lpfc_nodelist *ndlp;
1139
1140        list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1141                ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
1142
1143                if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1144                        continue;
1145                if (ndlp->nlp_type & NLP_FABRIC) {
1146                        /* On Linkup its safe to clean up the ndlp
1147                         * from Fabric connections.
1148                         */
1149                        if (ndlp->nlp_DID != Fabric_DID)
1150                                lpfc_unreg_rpi(vport, ndlp);
1151                        lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1152                } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
1153                        /* Fail outstanding IO now since device is
1154                         * marked for PLOGI.
1155                         */
1156                        lpfc_unreg_rpi(vport, ndlp);
1157                }
1158        }
1159}
1160
1161static void
1162lpfc_linkup_port(struct lpfc_vport *vport)
1163{
1164        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1165        struct lpfc_hba  *phba = vport->phba;
1166
1167        if ((vport->load_flag & FC_UNLOADING) != 0)
1168                return;
1169
1170        lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1171                "Link Up:         top:x%x speed:x%x flg:x%x",
1172                phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
1173
1174        /* If NPIV is not enabled, only bring the physical port up */
1175        if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1176                (vport != phba->pport))
1177                return;
1178
1179        if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1180                fc_host_post_event(shost, fc_get_event_number(),
1181                                   FCH_EVT_LINKUP, 0);
1182
1183        spin_lock_irq(shost->host_lock);
1184        vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1185                            FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1186        vport->fc_flag |= FC_NDISC_ACTIVE;
1187        vport->fc_ns_retry = 0;
1188        spin_unlock_irq(shost->host_lock);
1189
1190        lpfc_linkup_cleanup_nodes(vport);
1191}
1192
1193static int
1194lpfc_linkup(struct lpfc_hba *phba)
1195{
1196        struct lpfc_vport **vports;
1197        int i;
1198        struct Scsi_Host  *shost = lpfc_shost_from_vport(phba->pport);
1199
1200        phba->link_state = LPFC_LINK_UP;
1201
1202        /* Unblock fabric iocbs if they are blocked */
1203        clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1204        del_timer_sync(&phba->fabric_block_timer);
1205
1206        vports = lpfc_create_vport_work_array(phba);
1207        if (vports != NULL)
1208                for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1209                        lpfc_linkup_port(vports[i]);
1210        lpfc_destroy_vport_work_array(phba, vports);
1211
1212        /* Clear the pport flogi counter in case the link down was
1213         * absorbed without an ACQE. No lock here - in worker thread
1214         * and discovery is synchronized.
1215         */
1216        spin_lock_irq(shost->host_lock);
1217        phba->pport->rcv_flogi_cnt = 0;
1218        spin_unlock_irq(shost->host_lock);
1219
1220        /* reinitialize initial FLOGI flag */
1221        phba->hba_flag &= ~(HBA_FLOGI_ISSUED);
1222        phba->defer_flogi_acc_flag = false;
1223
1224        return 0;
1225}
1226
1227/*
1228 * This routine handles processing a CLEAR_LA mailbox
1229 * command upon completion. It is setup in the LPFC_MBOXQ
1230 * as the completion routine when the command is
1231 * handed off to the SLI layer. SLI3 only.
1232 */
1233static void
1234lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1235{
1236        struct lpfc_vport *vport = pmb->vport;
1237        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1238        struct lpfc_sli   *psli = &phba->sli;
1239        MAILBOX_t *mb = &pmb->u.mb;
1240        uint32_t control;
1241
1242        /* Since we don't do discovery right now, turn these off here */
1243        psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1244        psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1245
1246        /* Check for error */
1247        if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
1248                /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
1249                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1250                                 "0320 CLEAR_LA mbxStatus error x%x hba "
1251                                 "state x%x\n",
1252                                 mb->mbxStatus, vport->port_state);
1253                phba->link_state = LPFC_HBA_ERROR;
1254                goto out;
1255        }
1256
1257        if (vport->port_type == LPFC_PHYSICAL_PORT)
1258                phba->link_state = LPFC_HBA_READY;
1259
1260        spin_lock_irq(&phba->hbalock);
1261        psli->sli_flag |= LPFC_PROCESS_LA;
1262        control = readl(phba->HCregaddr);
1263        control |= HC_LAINT_ENA;
1264        writel(control, phba->HCregaddr);
1265        readl(phba->HCregaddr); /* flush */
1266        spin_unlock_irq(&phba->hbalock);
1267        mempool_free(pmb, phba->mbox_mem_pool);
1268        return;
1269
1270out:
1271        /* Device Discovery completes */
1272        lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1273                         "0225 Device Discovery completes\n");
1274        mempool_free(pmb, phba->mbox_mem_pool);
1275
1276        spin_lock_irq(shost->host_lock);
1277        vport->fc_flag &= ~FC_ABORT_DISCOVERY;
1278        spin_unlock_irq(shost->host_lock);
1279
1280        lpfc_can_disctmo(vport);
1281
1282        /* turn on Link Attention interrupts */
1283
1284        spin_lock_irq(&phba->hbalock);
1285        psli->sli_flag |= LPFC_PROCESS_LA;
1286        control = readl(phba->HCregaddr);
1287        control |= HC_LAINT_ENA;
1288        writel(control, phba->HCregaddr);
1289        readl(phba->HCregaddr); /* flush */
1290        spin_unlock_irq(&phba->hbalock);
1291
1292        return;
1293}
1294
1295void
1296lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1297{
1298        struct lpfc_vport *vport = pmb->vport;
1299        LPFC_MBOXQ_t *sparam_mb;
1300        struct lpfc_dmabuf *sparam_mp;
1301        u16 status = pmb->u.mb.mbxStatus;
1302        int rc;
1303
1304        mempool_free(pmb, phba->mbox_mem_pool);
1305
1306        if (status)
1307                goto out;
1308
1309        /* don't perform discovery for SLI4 loopback diagnostic test */
1310        if ((phba->sli_rev == LPFC_SLI_REV4) &&
1311            !(phba->hba_flag & HBA_FCOE_MODE) &&
1312            (phba->link_flag & LS_LOOPBACK_MODE))
1313                return;
1314
1315        if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
1316            vport->fc_flag & FC_PUBLIC_LOOP &&
1317            !(vport->fc_flag & FC_LBIT)) {
1318                        /* Need to wait for FAN - use discovery timer
1319                         * for timeout.  port_state is identically
1320                         * LPFC_LOCAL_CFG_LINK while waiting for FAN
1321                         */
1322                        lpfc_set_disctmo(vport);
1323                        return;
1324        }
1325
1326        /* Start discovery by sending a FLOGI. port_state is identically
1327         * LPFC_FLOGI while waiting for FLOGI cmpl.
1328         */
1329        if (vport->port_state != LPFC_FLOGI) {
1330                /* Issue MBX_READ_SPARAM to update CSPs before FLOGI if
1331                 * bb-credit recovery is in place.
1332                 */
1333                if (phba->bbcredit_support && phba->cfg_enable_bbcr &&
1334                    !(phba->link_flag & LS_LOOPBACK_MODE)) {
1335                        sparam_mb = mempool_alloc(phba->mbox_mem_pool,
1336                                                  GFP_KERNEL);
1337                        if (!sparam_mb)
1338                                goto sparam_out;
1339
1340                        rc = lpfc_read_sparam(phba, sparam_mb, 0);
1341                        if (rc) {
1342                                mempool_free(sparam_mb, phba->mbox_mem_pool);
1343                                goto sparam_out;
1344                        }
1345                        sparam_mb->vport = vport;
1346                        sparam_mb->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
1347                        rc = lpfc_sli_issue_mbox(phba, sparam_mb, MBX_NOWAIT);
1348                        if (rc == MBX_NOT_FINISHED) {
1349                                sparam_mp = (struct lpfc_dmabuf *)
1350                                                sparam_mb->ctx_buf;
1351                                lpfc_mbuf_free(phba, sparam_mp->virt,
1352                                               sparam_mp->phys);
1353                                kfree(sparam_mp);
1354                                sparam_mb->ctx_buf = NULL;
1355                                mempool_free(sparam_mb, phba->mbox_mem_pool);
1356                                goto sparam_out;
1357                        }
1358
1359                        phba->hba_flag |= HBA_DEFER_FLOGI;
1360                }  else {
1361                        lpfc_initial_flogi(vport);
1362                }
1363        } else {
1364                if (vport->fc_flag & FC_PT2PT)
1365                        lpfc_disc_start(vport);
1366        }
1367        return;
1368
1369out:
1370        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1371                         "0306 CONFIG_LINK mbxStatus error x%x HBA state x%x\n",
1372                         status, vport->port_state);
1373
1374sparam_out:
1375        lpfc_linkdown(phba);
1376
1377        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1378                         "0200 CONFIG_LINK bad hba state x%x\n",
1379                         vport->port_state);
1380
1381        lpfc_issue_clear_la(phba, vport);
1382        return;
1383}
1384
1385/**
1386 * lpfc_sli4_clear_fcf_rr_bmask
1387 * @phba: pointer to the struct lpfc_hba for this port.
1388 * This fucnction resets the round robin bit mask and clears the
1389 * fcf priority list. The list deletions are done while holding the
1390 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1391 * from the lpfc_fcf_pri record.
1392 **/
1393void
1394lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1395{
1396        struct lpfc_fcf_pri *fcf_pri;
1397        struct lpfc_fcf_pri *next_fcf_pri;
1398        memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1399        spin_lock_irq(&phba->hbalock);
1400        list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1401                                &phba->fcf.fcf_pri_list, list) {
1402                list_del_init(&fcf_pri->list);
1403                fcf_pri->fcf_rec.flag = 0;
1404        }
1405        spin_unlock_irq(&phba->hbalock);
1406}
1407static void
1408lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1409{
1410        struct lpfc_vport *vport = mboxq->vport;
1411
1412        if (mboxq->u.mb.mbxStatus) {
1413                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1414                                 "2017 REG_FCFI mbxStatus error x%x "
1415                                 "HBA state x%x\n", mboxq->u.mb.mbxStatus,
1416                                 vport->port_state);
1417                goto fail_out;
1418        }
1419
1420        /* Start FCoE discovery by sending a FLOGI. */
1421        phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1422        /* Set the FCFI registered flag */
1423        spin_lock_irq(&phba->hbalock);
1424        phba->fcf.fcf_flag |= FCF_REGISTERED;
1425        spin_unlock_irq(&phba->hbalock);
1426
1427        /* If there is a pending FCoE event, restart FCF table scan. */
1428        if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1429                lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
1430                goto fail_out;
1431
1432        /* Mark successful completion of FCF table scan */
1433        spin_lock_irq(&phba->hbalock);
1434        phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1435        phba->hba_flag &= ~FCF_TS_INPROG;
1436        if (vport->port_state != LPFC_FLOGI) {
1437                phba->hba_flag |= FCF_RR_INPROG;
1438                spin_unlock_irq(&phba->hbalock);
1439                lpfc_issue_init_vfi(vport);
1440                goto out;
1441        }
1442        spin_unlock_irq(&phba->hbalock);
1443        goto out;
1444
1445fail_out:
1446        spin_lock_irq(&phba->hbalock);
1447        phba->hba_flag &= ~FCF_RR_INPROG;
1448        spin_unlock_irq(&phba->hbalock);
1449out:
1450        mempool_free(mboxq, phba->mbox_mem_pool);
1451}
1452
1453/**
1454 * lpfc_fab_name_match - Check if the fcf fabric name match.
1455 * @fab_name: pointer to fabric name.
1456 * @new_fcf_record: pointer to fcf record.
1457 *
1458 * This routine compare the fcf record's fabric name with provided
1459 * fabric name. If the fabric name are identical this function
1460 * returns 1 else return 0.
1461 **/
1462static uint32_t
1463lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1464{
1465        if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
1466                return 0;
1467        if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1468                return 0;
1469        if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1470                return 0;
1471        if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1472                return 0;
1473        if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1474                return 0;
1475        if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1476                return 0;
1477        if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1478                return 0;
1479        if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1480                return 0;
1481        return 1;
1482}
1483
1484/**
1485 * lpfc_sw_name_match - Check if the fcf switch name match.
1486 * @sw_name: pointer to switch name.
1487 * @new_fcf_record: pointer to fcf record.
1488 *
1489 * This routine compare the fcf record's switch name with provided
1490 * switch name. If the switch name are identical this function
1491 * returns 1 else return 0.
1492 **/
1493static uint32_t
1494lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1495{
1496        if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
1497                return 0;
1498        if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1499                return 0;
1500        if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1501                return 0;
1502        if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1503                return 0;
1504        if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1505                return 0;
1506        if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1507                return 0;
1508        if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1509                return 0;
1510        if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1511                return 0;
1512        return 1;
1513}
1514
1515/**
1516 * lpfc_mac_addr_match - Check if the fcf mac address match.
1517 * @mac_addr: pointer to mac address.
1518 * @new_fcf_record: pointer to fcf record.
1519 *
1520 * This routine compare the fcf record's mac address with HBA's
1521 * FCF mac address. If the mac addresses are identical this function
1522 * returns 1 else return 0.
1523 **/
1524static uint32_t
1525lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
1526{
1527        if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
1528                return 0;
1529        if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1530                return 0;
1531        if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1532                return 0;
1533        if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1534                return 0;
1535        if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1536                return 0;
1537        if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1538                return 0;
1539        return 1;
1540}
1541
1542static bool
1543lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1544{
1545        return (curr_vlan_id == new_vlan_id);
1546}
1547
1548/**
1549 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1550 * @phba: pointer to lpfc hba data structure.
1551 * @fcf_index: Index for the lpfc_fcf_record.
1552 * @new_fcf_record: pointer to hba fcf record.
1553 *
1554 * This routine updates the driver FCF priority record from the new HBA FCF
1555 * record. The hbalock is asserted held in the code path calling this
1556 * routine.
1557 **/
1558static void
1559__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1560                                 struct fcf_record *new_fcf_record
1561                                 )
1562{
1563        struct lpfc_fcf_pri *fcf_pri;
1564
1565        fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1566        fcf_pri->fcf_rec.fcf_index = fcf_index;
1567        /* FCF record priority */
1568        fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1569
1570}
1571
1572/**
1573 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1574 * @fcf_rec: pointer to driver fcf record.
1575 * @new_fcf_record: pointer to fcf record.
1576 *
1577 * This routine copies the FCF information from the FCF
1578 * record to lpfc_hba data structure.
1579 **/
1580static void
1581lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1582                     struct fcf_record *new_fcf_record)
1583{
1584        /* Fabric name */
1585        fcf_rec->fabric_name[0] =
1586                bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1587        fcf_rec->fabric_name[1] =
1588                bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1589        fcf_rec->fabric_name[2] =
1590                bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1591        fcf_rec->fabric_name[3] =
1592                bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1593        fcf_rec->fabric_name[4] =
1594                bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1595        fcf_rec->fabric_name[5] =
1596                bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1597        fcf_rec->fabric_name[6] =
1598                bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1599        fcf_rec->fabric_name[7] =
1600                bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1601        /* Mac address */
1602        fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1603        fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1604        fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1605        fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1606        fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1607        fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1608        /* FCF record index */
1609        fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1610        /* FCF record priority */
1611        fcf_rec->priority = new_fcf_record->fip_priority;
1612        /* Switch name */
1613        fcf_rec->switch_name[0] =
1614                bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1615        fcf_rec->switch_name[1] =
1616                bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1617        fcf_rec->switch_name[2] =
1618                bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1619        fcf_rec->switch_name[3] =
1620                bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1621        fcf_rec->switch_name[4] =
1622                bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1623        fcf_rec->switch_name[5] =
1624                bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1625        fcf_rec->switch_name[6] =
1626                bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1627        fcf_rec->switch_name[7] =
1628                bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
1629}
1630
1631/**
1632 * __lpfc_update_fcf_record - Update driver fcf record
1633 * @phba: pointer to lpfc hba data structure.
1634 * @fcf_rec: pointer to driver fcf record.
1635 * @new_fcf_record: pointer to hba fcf record.
1636 * @addr_mode: address mode to be set to the driver fcf record.
1637 * @vlan_id: vlan tag to be set to the driver fcf record.
1638 * @flag: flag bits to be set to the driver fcf record.
1639 *
1640 * This routine updates the driver FCF record from the new HBA FCF record
1641 * together with the address mode, vlan_id, and other informations. This
1642 * routine is called with the hbalock held.
1643 **/
1644static void
1645__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1646                       struct fcf_record *new_fcf_record, uint32_t addr_mode,
1647                       uint16_t vlan_id, uint32_t flag)
1648{
1649        lockdep_assert_held(&phba->hbalock);
1650
1651        /* Copy the fields from the HBA's FCF record */
1652        lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1653        /* Update other fields of driver FCF record */
1654        fcf_rec->addr_mode = addr_mode;
1655        fcf_rec->vlan_id = vlan_id;
1656        fcf_rec->flag |= (flag | RECORD_VALID);
1657        __lpfc_update_fcf_record_pri(phba,
1658                bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1659                                 new_fcf_record);
1660}
1661
1662/**
1663 * lpfc_register_fcf - Register the FCF with hba.
1664 * @phba: pointer to lpfc hba data structure.
1665 *
1666 * This routine issues a register fcfi mailbox command to register
1667 * the fcf with HBA.
1668 **/
1669static void
1670lpfc_register_fcf(struct lpfc_hba *phba)
1671{
1672        LPFC_MBOXQ_t *fcf_mbxq;
1673        int rc;
1674
1675        spin_lock_irq(&phba->hbalock);
1676        /* If the FCF is not available do nothing. */
1677        if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1678                phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1679                spin_unlock_irq(&phba->hbalock);
1680                return;
1681        }
1682
1683        /* The FCF is already registered, start discovery */
1684        if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1685                phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1686                phba->hba_flag &= ~FCF_TS_INPROG;
1687                if (phba->pport->port_state != LPFC_FLOGI &&
1688                    phba->pport->fc_flag & FC_FABRIC) {
1689                        phba->hba_flag |= FCF_RR_INPROG;
1690                        spin_unlock_irq(&phba->hbalock);
1691                        lpfc_initial_flogi(phba->pport);
1692                        return;
1693                }
1694                spin_unlock_irq(&phba->hbalock);
1695                return;
1696        }
1697        spin_unlock_irq(&phba->hbalock);
1698
1699        fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1700        if (!fcf_mbxq) {
1701                spin_lock_irq(&phba->hbalock);
1702                phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1703                spin_unlock_irq(&phba->hbalock);
1704                return;
1705        }
1706
1707        lpfc_reg_fcfi(phba, fcf_mbxq);
1708        fcf_mbxq->vport = phba->pport;
1709        fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1710        rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1711        if (rc == MBX_NOT_FINISHED) {
1712                spin_lock_irq(&phba->hbalock);
1713                phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1714                spin_unlock_irq(&phba->hbalock);
1715                mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1716        }
1717
1718        return;
1719}
1720
1721/**
1722 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1723 * @phba: pointer to lpfc hba data structure.
1724 * @new_fcf_record: pointer to fcf record.
1725 * @boot_flag: Indicates if this record used by boot bios.
1726 * @addr_mode: The address mode to be used by this FCF
1727 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
1728 *
1729 * This routine compare the fcf record with connect list obtained from the
1730 * config region to decide if this FCF can be used for SAN discovery. It returns
1731 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1732 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1733 * is used by boot bios and addr_mode will indicate the addressing mode to be
1734 * used for this FCF when the function returns.
1735 * If the FCF record need to be used with a particular vlan id, the vlan is
1736 * set in the vlan_id on return of the function. If not VLAN tagging need to
1737 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
1738 **/
1739static int
1740lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1741                        struct fcf_record *new_fcf_record,
1742                        uint32_t *boot_flag, uint32_t *addr_mode,
1743                        uint16_t *vlan_id)
1744{
1745        struct lpfc_fcf_conn_entry *conn_entry;
1746        int i, j, fcf_vlan_id = 0;
1747
1748        /* Find the lowest VLAN id in the FCF record */
1749        for (i = 0; i < 512; i++) {
1750                if (new_fcf_record->vlan_bitmap[i]) {
1751                        fcf_vlan_id = i * 8;
1752                        j = 0;
1753                        while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1754                                j++;
1755                                fcf_vlan_id++;
1756                        }
1757                        break;
1758                }
1759        }
1760
1761        /* FCF not valid/available or solicitation in progress */
1762        if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1763            !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1764            bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
1765                return 0;
1766
1767        if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
1768                *boot_flag = 0;
1769                *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1770                                new_fcf_record);
1771                if (phba->valid_vlan)
1772                        *vlan_id = phba->vlan_id;
1773                else
1774                        *vlan_id = LPFC_FCOE_NULL_VID;
1775                return 1;
1776        }
1777
1778        /*
1779         * If there are no FCF connection table entry, driver connect to all
1780         * FCFs.
1781         */
1782        if (list_empty(&phba->fcf_conn_rec_list)) {
1783                *boot_flag = 0;
1784                *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1785                        new_fcf_record);
1786
1787                /*
1788                 * When there are no FCF connect entries, use driver's default
1789                 * addressing mode - FPMA.
1790                 */
1791                if (*addr_mode & LPFC_FCF_FPMA)
1792                        *addr_mode = LPFC_FCF_FPMA;
1793
1794                /* If FCF record report a vlan id use that vlan id */
1795                if (fcf_vlan_id)
1796                        *vlan_id = fcf_vlan_id;
1797                else
1798                        *vlan_id = LPFC_FCOE_NULL_VID;
1799                return 1;
1800        }
1801
1802        list_for_each_entry(conn_entry,
1803                            &phba->fcf_conn_rec_list, list) {
1804                if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1805                        continue;
1806
1807                if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1808                        !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
1809                                             new_fcf_record))
1810                        continue;
1811                if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1812                        !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1813                                            new_fcf_record))
1814                        continue;
1815                if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1816                        /*
1817                         * If the vlan bit map does not have the bit set for the
1818                         * vlan id to be used, then it is not a match.
1819                         */
1820                        if (!(new_fcf_record->vlan_bitmap
1821                                [conn_entry->conn_rec.vlan_tag / 8] &
1822                                (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1823                                continue;
1824                }
1825
1826                /*
1827                 * If connection record does not support any addressing mode,
1828                 * skip the FCF record.
1829                 */
1830                if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1831                        & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1832                        continue;
1833
1834                /*
1835                 * Check if the connection record specifies a required
1836                 * addressing mode.
1837                 */
1838                if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1839                        !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1840
1841                        /*
1842                         * If SPMA required but FCF not support this continue.
1843                         */
1844                        if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1845                                !(bf_get(lpfc_fcf_record_mac_addr_prov,
1846                                        new_fcf_record) & LPFC_FCF_SPMA))
1847                                continue;
1848
1849                        /*
1850                         * If FPMA required but FCF not support this continue.
1851                         */
1852                        if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1853                                !(bf_get(lpfc_fcf_record_mac_addr_prov,
1854                                new_fcf_record) & LPFC_FCF_FPMA))
1855                                continue;
1856                }
1857
1858                /*
1859                 * This fcf record matches filtering criteria.
1860                 */
1861                if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1862                        *boot_flag = 1;
1863                else
1864                        *boot_flag = 0;
1865
1866                /*
1867                 * If user did not specify any addressing mode, or if the
1868                 * preferred addressing mode specified by user is not supported
1869                 * by FCF, allow fabric to pick the addressing mode.
1870                 */
1871                *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1872                                new_fcf_record);
1873                /*
1874                 * If the user specified a required address mode, assign that
1875                 * address mode
1876                 */
1877                if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1878                        (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1879                        *addr_mode = (conn_entry->conn_rec.flags &
1880                                FCFCNCT_AM_SPMA) ?
1881                                LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1882                /*
1883                 * If the user specified a preferred address mode, use the
1884                 * addr mode only if FCF support the addr_mode.
1885                 */
1886                else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1887                        (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1888                        (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1889                        (*addr_mode & LPFC_FCF_SPMA))
1890                                *addr_mode = LPFC_FCF_SPMA;
1891                else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1892                        (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1893                        !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1894                        (*addr_mode & LPFC_FCF_FPMA))
1895                                *addr_mode = LPFC_FCF_FPMA;
1896
1897                /* If matching connect list has a vlan id, use it */
1898                if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1899                        *vlan_id = conn_entry->conn_rec.vlan_tag;
1900                /*
1901                 * If no vlan id is specified in connect list, use the vlan id
1902                 * in the FCF record
1903                 */
1904                else if (fcf_vlan_id)
1905                        *vlan_id = fcf_vlan_id;
1906                else
1907                        *vlan_id = LPFC_FCOE_NULL_VID;
1908
1909                return 1;
1910        }
1911
1912        return 0;
1913}
1914
1915/**
1916 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1917 * @phba: pointer to lpfc hba data structure.
1918 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1919 *
1920 * This function check if there is any fcoe event pending while driver
1921 * scan FCF entries. If there is any pending event, it will restart the
1922 * FCF saning and return 1 else return 0.
1923 */
1924int
1925lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1926{
1927        /*
1928         * If the Link is up and no FCoE events while in the
1929         * FCF discovery, no need to restart FCF discovery.
1930         */
1931        if ((phba->link_state  >= LPFC_LINK_UP) &&
1932            (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1933                return 0;
1934
1935        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1936                        "2768 Pending link or FCF event during current "
1937                        "handling of the previous event: link_state:x%x, "
1938                        "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1939                        phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1940                        phba->fcoe_eventtag);
1941
1942        spin_lock_irq(&phba->hbalock);
1943        phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1944        spin_unlock_irq(&phba->hbalock);
1945
1946        if (phba->link_state >= LPFC_LINK_UP) {
1947                lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1948                                "2780 Restart FCF table scan due to "
1949                                "pending FCF event:evt_tag_at_scan:x%x, "
1950                                "evt_tag_current:x%x\n",
1951                                phba->fcoe_eventtag_at_fcf_scan,
1952                                phba->fcoe_eventtag);
1953                lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1954        } else {
1955                /*
1956                 * Do not continue FCF discovery and clear FCF_TS_INPROG
1957                 * flag
1958                 */
1959                lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1960                                "2833 Stop FCF discovery process due to link "
1961                                "state change (x%x)\n", phba->link_state);
1962                spin_lock_irq(&phba->hbalock);
1963                phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1964                phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
1965                spin_unlock_irq(&phba->hbalock);
1966        }
1967
1968        /* Unregister the currently registered FCF if required */
1969        if (unreg_fcf) {
1970                spin_lock_irq(&phba->hbalock);
1971                phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1972                spin_unlock_irq(&phba->hbalock);
1973                lpfc_sli4_unregister_fcf(phba);
1974        }
1975        return 1;
1976}
1977
1978/**
1979 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1980 * @phba: pointer to lpfc hba data structure.
1981 * @fcf_cnt: number of eligible fcf record seen so far.
1982 *
1983 * This function makes an running random selection decision on FCF record to
1984 * use through a sequence of @fcf_cnt eligible FCF records with equal
1985 * probability. To perform integer manunipulation of random numbers with
1986 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1987 * from prandom_u32() are taken as the random random number generated.
1988 *
1989 * Returns true when outcome is for the newly read FCF record should be
1990 * chosen; otherwise, return false when outcome is for keeping the previously
1991 * chosen FCF record.
1992 **/
1993static bool
1994lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1995{
1996        uint32_t rand_num;
1997
1998        /* Get 16-bit uniform random number */
1999        rand_num = 0xFFFF & prandom_u32();
2000
2001        /* Decision with probability 1/fcf_cnt */
2002        if ((fcf_cnt * rand_num) < 0xFFFF)
2003                return true;
2004        else
2005                return false;
2006}
2007
2008/**
2009 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
2010 * @phba: pointer to lpfc hba data structure.
2011 * @mboxq: pointer to mailbox object.
2012 * @next_fcf_index: pointer to holder of next fcf index.
2013 *
2014 * This routine parses the non-embedded fcf mailbox command by performing the
2015 * necessarily error checking, non-embedded read FCF record mailbox command
2016 * SGE parsing, and endianness swapping.
2017 *
2018 * Returns the pointer to the new FCF record in the non-embedded mailbox
2019 * command DMA memory if successfully, other NULL.
2020 */
2021static struct fcf_record *
2022lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
2023                             uint16_t *next_fcf_index)
2024{
2025        void *virt_addr;
2026        struct lpfc_mbx_sge sge;
2027        struct lpfc_mbx_read_fcf_tbl *read_fcf;
2028        uint32_t shdr_status, shdr_add_status, if_type;
2029        union lpfc_sli4_cfg_shdr *shdr;
2030        struct fcf_record *new_fcf_record;
2031
2032        /* Get the first SGE entry from the non-embedded DMA memory. This
2033         * routine only uses a single SGE.
2034         */
2035        lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
2036        if (unlikely(!mboxq->sge_array)) {
2037                lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2038                                "2524 Failed to get the non-embedded SGE "
2039                                "virtual address\n");
2040                return NULL;
2041        }
2042        virt_addr = mboxq->sge_array->addr[0];
2043
2044        shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
2045        lpfc_sli_pcimem_bcopy(shdr, shdr,
2046                              sizeof(union lpfc_sli4_cfg_shdr));
2047        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2048        if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
2049        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2050        if (shdr_status || shdr_add_status) {
2051                if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
2052                                        if_type == LPFC_SLI_INTF_IF_TYPE_2)
2053                        lpfc_printf_log(phba, KERN_ERR,
2054                                        LOG_TRACE_EVENT,
2055                                        "2726 READ_FCF_RECORD Indicates empty "
2056                                        "FCF table.\n");
2057                else
2058                        lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2059                                        "2521 READ_FCF_RECORD mailbox failed "
2060                                        "with status x%x add_status x%x, "
2061                                        "mbx\n", shdr_status, shdr_add_status);
2062                return NULL;
2063        }
2064
2065        /* Interpreting the returned information of the FCF record */
2066        read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
2067        lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
2068                              sizeof(struct lpfc_mbx_read_fcf_tbl));
2069        *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
2070        new_fcf_record = (struct fcf_record *)(virt_addr +
2071                          sizeof(struct lpfc_mbx_read_fcf_tbl));
2072        lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
2073                                offsetof(struct fcf_record, vlan_bitmap));
2074        new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
2075        new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
2076
2077        return new_fcf_record;
2078}
2079
2080/**
2081 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
2082 * @phba: pointer to lpfc hba data structure.
2083 * @fcf_record: pointer to the fcf record.
2084 * @vlan_id: the lowest vlan identifier associated to this fcf record.
2085 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
2086 *
2087 * This routine logs the detailed FCF record if the LOG_FIP loggin is
2088 * enabled.
2089 **/
2090static void
2091lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
2092                              struct fcf_record *fcf_record,
2093                              uint16_t vlan_id,
2094                              uint16_t next_fcf_index)
2095{
2096        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2097                        "2764 READ_FCF_RECORD:\n"
2098                        "\tFCF_Index     : x%x\n"
2099                        "\tFCF_Avail     : x%x\n"
2100                        "\tFCF_Valid     : x%x\n"
2101                        "\tFCF_SOL       : x%x\n"
2102                        "\tFIP_Priority  : x%x\n"
2103                        "\tMAC_Provider  : x%x\n"
2104                        "\tLowest VLANID : x%x\n"
2105                        "\tFCF_MAC Addr  : x%x:%x:%x:%x:%x:%x\n"
2106                        "\tFabric_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
2107                        "\tSwitch_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
2108                        "\tNext_FCF_Index: x%x\n",
2109                        bf_get(lpfc_fcf_record_fcf_index, fcf_record),
2110                        bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
2111                        bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
2112                        bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
2113                        fcf_record->fip_priority,
2114                        bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
2115                        vlan_id,
2116                        bf_get(lpfc_fcf_record_mac_0, fcf_record),
2117                        bf_get(lpfc_fcf_record_mac_1, fcf_record),
2118                        bf_get(lpfc_fcf_record_mac_2, fcf_record),
2119                        bf_get(lpfc_fcf_record_mac_3, fcf_record),
2120                        bf_get(lpfc_fcf_record_mac_4, fcf_record),
2121                        bf_get(lpfc_fcf_record_mac_5, fcf_record),
2122                        bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
2123                        bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
2124                        bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
2125                        bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
2126                        bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
2127                        bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
2128                        bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
2129                        bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
2130                        bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
2131                        bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
2132                        bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
2133                        bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
2134                        bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
2135                        bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
2136                        bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
2137                        bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
2138                        next_fcf_index);
2139}
2140
2141/**
2142 * lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
2143 * @phba: pointer to lpfc hba data structure.
2144 * @fcf_rec: pointer to an existing FCF record.
2145 * @new_fcf_record: pointer to a new FCF record.
2146 * @new_vlan_id: vlan id from the new FCF record.
2147 *
2148 * This function performs matching test of a new FCF record against an existing
2149 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
2150 * will not be used as part of the FCF record matching criteria.
2151 *
2152 * Returns true if all the fields matching, otherwise returns false.
2153 */
2154static bool
2155lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
2156                           struct lpfc_fcf_rec *fcf_rec,
2157                           struct fcf_record *new_fcf_record,
2158                           uint16_t new_vlan_id)
2159{
2160        if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
2161                if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
2162                        return false;
2163        if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
2164                return false;
2165        if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
2166                return false;
2167        if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
2168                return false;
2169        if (fcf_rec->priority != new_fcf_record->fip_priority)
2170                return false;
2171        return true;
2172}
2173
2174/**
2175 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
2176 * @vport: Pointer to vport object.
2177 * @fcf_index: index to next fcf.
2178 *
2179 * This function processing the roundrobin fcf failover to next fcf index.
2180 * When this function is invoked, there will be a current fcf registered
2181 * for flogi.
2182 * Return: 0 for continue retrying flogi on currently registered fcf;
2183 *         1 for stop flogi on currently registered fcf;
2184 */
2185int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
2186{
2187        struct lpfc_hba *phba = vport->phba;
2188        int rc;
2189
2190        if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
2191                spin_lock_irq(&phba->hbalock);
2192                if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2193                        spin_unlock_irq(&phba->hbalock);
2194                        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2195                                        "2872 Devloss tmo with no eligible "
2196                                        "FCF, unregister in-use FCF (x%x) "
2197                                        "and rescan FCF table\n",
2198                                        phba->fcf.current_rec.fcf_indx);
2199                        lpfc_unregister_fcf_rescan(phba);
2200                        goto stop_flogi_current_fcf;
2201                }
2202                /* Mark the end to FLOGI roundrobin failover */
2203                phba->hba_flag &= ~FCF_RR_INPROG;
2204                /* Allow action to new fcf asynchronous event */
2205                phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2206                spin_unlock_irq(&phba->hbalock);
2207                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2208                                "2865 No FCF available, stop roundrobin FCF "
2209                                "failover and change port state:x%x/x%x\n",
2210                                phba->pport->port_state, LPFC_VPORT_UNKNOWN);
2211                phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2212
2213                if (!phba->fcf.fcf_redisc_attempted) {
2214                        lpfc_unregister_fcf(phba);
2215
2216                        rc = lpfc_sli4_redisc_fcf_table(phba);
2217                        if (!rc) {
2218                                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2219                                                "3195 Rediscover FCF table\n");
2220                                phba->fcf.fcf_redisc_attempted = 1;
2221                                lpfc_sli4_clear_fcf_rr_bmask(phba);
2222                        } else {
2223                                lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2224                                                "3196 Rediscover FCF table "
2225                                                "failed. Status:x%x\n", rc);
2226                        }
2227                } else {
2228                        lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2229                                        "3197 Already rediscover FCF table "
2230                                        "attempted. No more retry\n");
2231                }
2232                goto stop_flogi_current_fcf;
2233        } else {
2234                lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
2235                                "2794 Try FLOGI roundrobin FCF failover to "
2236                                "(x%x)\n", fcf_index);
2237                rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
2238                if (rc)
2239                        lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
2240                                        "2761 FLOGI roundrobin FCF failover "
2241                                        "failed (rc:x%x) to read FCF (x%x)\n",
2242                                        rc, phba->fcf.current_rec.fcf_indx);
2243                else
2244                        goto stop_flogi_current_fcf;
2245        }
2246        return 0;
2247
2248stop_flogi_current_fcf:
2249        lpfc_can_disctmo(vport);
2250        return 1;
2251}
2252
2253/**
2254 * lpfc_sli4_fcf_pri_list_del
2255 * @phba: pointer to lpfc hba data structure.
2256 * @fcf_index: the index of the fcf record to delete
2257 * This routine checks the on list flag of the fcf_index to be deleted.
2258 * If it is one the list then it is removed from the list, and the flag
2259 * is cleared. This routine grab the hbalock before removing the fcf
2260 * record from the list.
2261 **/
2262static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2263                        uint16_t fcf_index)
2264{
2265        struct lpfc_fcf_pri *new_fcf_pri;
2266
2267        new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2268        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2269                "3058 deleting idx x%x pri x%x flg x%x\n",
2270                fcf_index, new_fcf_pri->fcf_rec.priority,
2271                 new_fcf_pri->fcf_rec.flag);
2272        spin_lock_irq(&phba->hbalock);
2273        if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2274                if (phba->fcf.current_rec.priority ==
2275                                new_fcf_pri->fcf_rec.priority)
2276                        phba->fcf.eligible_fcf_cnt--;
2277                list_del_init(&new_fcf_pri->list);
2278                new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2279        }
2280        spin_unlock_irq(&phba->hbalock);
2281}
2282
2283/**
2284 * lpfc_sli4_set_fcf_flogi_fail
2285 * @phba: pointer to lpfc hba data structure.
2286 * @fcf_index: the index of the fcf record to update
2287 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2288 * flag so the the round robin slection for the particular priority level
2289 * will try a different fcf record that does not have this bit set.
2290 * If the fcf record is re-read for any reason this flag is cleared brfore
2291 * adding it to the priority list.
2292 **/
2293void
2294lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2295{
2296        struct lpfc_fcf_pri *new_fcf_pri;
2297        new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2298        spin_lock_irq(&phba->hbalock);
2299        new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2300        spin_unlock_irq(&phba->hbalock);
2301}
2302
2303/**
2304 * lpfc_sli4_fcf_pri_list_add
2305 * @phba: pointer to lpfc hba data structure.
2306 * @fcf_index: the index of the fcf record to add
2307 * @new_fcf_record: pointer to a new FCF record.
2308 * This routine checks the priority of the fcf_index to be added.
2309 * If it is a lower priority than the current head of the fcf_pri list
2310 * then it is added to the list in the right order.
2311 * If it is the same priority as the current head of the list then it
2312 * is added to the head of the list and its bit in the rr_bmask is set.
2313 * If the fcf_index to be added is of a higher priority than the current
2314 * head of the list then the rr_bmask is cleared, its bit is set in the
2315 * rr_bmask and it is added to the head of the list.
2316 * returns:
2317 * 0=success 1=failure
2318 **/
2319static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2320        uint16_t fcf_index,
2321        struct fcf_record *new_fcf_record)
2322{
2323        uint16_t current_fcf_pri;
2324        uint16_t last_index;
2325        struct lpfc_fcf_pri *fcf_pri;
2326        struct lpfc_fcf_pri *next_fcf_pri;
2327        struct lpfc_fcf_pri *new_fcf_pri;
2328        int ret;
2329
2330        new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2331        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2332                "3059 adding idx x%x pri x%x flg x%x\n",
2333                fcf_index, new_fcf_record->fip_priority,
2334                 new_fcf_pri->fcf_rec.flag);
2335        spin_lock_irq(&phba->hbalock);
2336        if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2337                list_del_init(&new_fcf_pri->list);
2338        new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2339        new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2340        if (list_empty(&phba->fcf.fcf_pri_list)) {
2341                list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2342                ret = lpfc_sli4_fcf_rr_index_set(phba,
2343                                new_fcf_pri->fcf_rec.fcf_index);
2344                goto out;
2345        }
2346
2347        last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2348                                LPFC_SLI4_FCF_TBL_INDX_MAX);
2349        if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2350                ret = 0; /* Empty rr list */
2351                goto out;
2352        }
2353        current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2354        if (new_fcf_pri->fcf_rec.priority <=  current_fcf_pri) {
2355                list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2356                if (new_fcf_pri->fcf_rec.priority <  current_fcf_pri) {
2357                        memset(phba->fcf.fcf_rr_bmask, 0,
2358                                sizeof(*phba->fcf.fcf_rr_bmask));
2359                        /* fcfs_at_this_priority_level = 1; */
2360                        phba->fcf.eligible_fcf_cnt = 1;
2361                } else
2362                        /* fcfs_at_this_priority_level++; */
2363                        phba->fcf.eligible_fcf_cnt++;
2364                ret = lpfc_sli4_fcf_rr_index_set(phba,
2365                                new_fcf_pri->fcf_rec.fcf_index);
2366                goto out;
2367        }
2368
2369        list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2370                                &phba->fcf.fcf_pri_list, list) {
2371                if (new_fcf_pri->fcf_rec.priority <=
2372                                fcf_pri->fcf_rec.priority) {
2373                        if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2374                                list_add(&new_fcf_pri->list,
2375                                                &phba->fcf.fcf_pri_list);
2376                        else
2377                                list_add(&new_fcf_pri->list,
2378                                         &((struct lpfc_fcf_pri *)
2379                                        fcf_pri->list.prev)->list);
2380                        ret = 0;
2381                        goto out;
2382                } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2383                        || new_fcf_pri->fcf_rec.priority <
2384                                next_fcf_pri->fcf_rec.priority) {
2385                        list_add(&new_fcf_pri->list, &fcf_pri->list);
2386                        ret = 0;
2387                        goto out;
2388                }
2389                if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2390                        continue;
2391
2392        }
2393        ret = 1;
2394out:
2395        /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2396        new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2397        spin_unlock_irq(&phba->hbalock);
2398        return ret;
2399}
2400
2401/**
2402 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2403 * @phba: pointer to lpfc hba data structure.
2404 * @mboxq: pointer to mailbox object.
2405 *
2406 * This function iterates through all the fcf records available in
2407 * HBA and chooses the optimal FCF record for discovery. After finding
2408 * the FCF for discovery it registers the FCF record and kicks start
2409 * discovery.
2410 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2411 * use an FCF record which matches fabric name and mac address of the
2412 * currently used FCF record.
2413 * If the driver supports only one FCF, it will try to use the FCF record
2414 * used by BOOT_BIOS.
2415 */
2416void
2417lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2418{
2419        struct fcf_record *new_fcf_record;
2420        uint32_t boot_flag, addr_mode;
2421        uint16_t fcf_index, next_fcf_index;
2422        struct lpfc_fcf_rec *fcf_rec = NULL;
2423        uint16_t vlan_id = LPFC_FCOE_NULL_VID;
2424        bool select_new_fcf;
2425        int rc;
2426
2427        /* If there is pending FCoE event restart FCF table scan */
2428        if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
2429                lpfc_sli4_mbox_cmd_free(phba, mboxq);
2430                return;
2431        }
2432
2433        /* Parse the FCF record from the non-embedded mailbox command */
2434        new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2435                                                      &next_fcf_index);
2436        if (!new_fcf_record) {
2437                lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2438                                "2765 Mailbox command READ_FCF_RECORD "
2439                                "failed to retrieve a FCF record.\n");
2440                /* Let next new FCF event trigger fast failover */
2441                spin_lock_irq(&phba->hbalock);
2442                phba->hba_flag &= ~FCF_TS_INPROG;
2443                spin_unlock_irq(&phba->hbalock);
2444                lpfc_sli4_mbox_cmd_free(phba, mboxq);
2445                return;
2446        }
2447
2448        /* Check the FCF record against the connection list */
2449        rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2450                                      &addr_mode, &vlan_id);
2451
2452        /* Log the FCF record information if turned on */
2453        lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2454                                      next_fcf_index);
2455
2456        /*
2457         * If the fcf record does not match with connect list entries
2458         * read the next entry; otherwise, this is an eligible FCF
2459         * record for roundrobin FCF failover.
2460         */
2461        if (!rc) {
2462                lpfc_sli4_fcf_pri_list_del(phba,
2463                                        bf_get(lpfc_fcf_record_fcf_index,
2464                                               new_fcf_record));
2465                lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2466                                "2781 FCF (x%x) failed connection "
2467                                "list check: (x%x/x%x/%x)\n",
2468                                bf_get(lpfc_fcf_record_fcf_index,
2469                                       new_fcf_record),
2470                                bf_get(lpfc_fcf_record_fcf_avail,
2471                                       new_fcf_record),
2472                                bf_get(lpfc_fcf_record_fcf_valid,
2473                                       new_fcf_record),
2474                                bf_get(lpfc_fcf_record_fcf_sol,
2475                                       new_fcf_record));
2476                if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2477                    lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2478                    new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
2479                        if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2480                            phba->fcf.current_rec.fcf_indx) {
2481                                lpfc_printf_log(phba, KERN_ERR,
2482                                                LOG_TRACE_EVENT,
2483                                        "2862 FCF (x%x) matches property "
2484                                        "of in-use FCF (x%x)\n",
2485                                        bf_get(lpfc_fcf_record_fcf_index,
2486                                               new_fcf_record),
2487                                        phba->fcf.current_rec.fcf_indx);
2488                                goto read_next_fcf;
2489                        }
2490                        /*
2491                         * In case the current in-use FCF record becomes
2492                         * invalid/unavailable during FCF discovery that
2493                         * was not triggered by fast FCF failover process,
2494                         * treat it as fast FCF failover.
2495                         */
2496                        if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2497                            !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2498                                lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2499                                                "2835 Invalid in-use FCF "
2500                                                "(x%x), enter FCF failover "
2501                                                "table scan.\n",
2502                                                phba->fcf.current_rec.fcf_indx);
2503                                spin_lock_irq(&phba->hbalock);
2504                                phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2505                                spin_unlock_irq(&phba->hbalock);
2506                                lpfc_sli4_mbox_cmd_free(phba, mboxq);
2507                                lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2508                                                LPFC_FCOE_FCF_GET_FIRST);
2509                                return;
2510                        }
2511                }
2512                goto read_next_fcf;
2513        } else {
2514                fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2515                rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2516                                                        new_fcf_record);
2517                if (rc)
2518                        goto read_next_fcf;
2519        }
2520
2521        /*
2522         * If this is not the first FCF discovery of the HBA, use last
2523         * FCF record for the discovery. The condition that a rescan
2524         * matches the in-use FCF record: fabric name, switch name, mac
2525         * address, and vlan_id.
2526         */
2527        spin_lock_irq(&phba->hbalock);
2528        if (phba->fcf.fcf_flag & FCF_IN_USE) {
2529                if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2530                        lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2531                    new_fcf_record, vlan_id)) {
2532                        if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2533                            phba->fcf.current_rec.fcf_indx) {
2534                                phba->fcf.fcf_flag |= FCF_AVAILABLE;
2535                                if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2536                                        /* Stop FCF redisc wait timer */
2537                                        __lpfc_sli4_stop_fcf_redisc_wait_timer(
2538                                                                        phba);
2539                                else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2540                                        /* Fast failover, mark completed */
2541                                        phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2542                                spin_unlock_irq(&phba->hbalock);
2543                                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2544                                                "2836 New FCF matches in-use "
2545                                                "FCF (x%x), port_state:x%x, "
2546                                                "fc_flag:x%x\n",
2547                                                phba->fcf.current_rec.fcf_indx,
2548                                                phba->pport->port_state,
2549                                                phba->pport->fc_flag);
2550                                goto out;
2551                        } else
2552                                lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2553                                        "2863 New FCF (x%x) matches "
2554                                        "property of in-use FCF (x%x)\n",
2555                                        bf_get(lpfc_fcf_record_fcf_index,
2556                                               new_fcf_record),
2557                                        phba->fcf.current_rec.fcf_indx);
2558                }
2559                /*
2560                 * Read next FCF record from HBA searching for the matching
2561                 * with in-use record only if not during the fast failover
2562                 * period. In case of fast failover period, it shall try to
2563                 * determine whether the FCF record just read should be the
2564                 * next candidate.
2565                 */
2566                if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2567                        spin_unlock_irq(&phba->hbalock);
2568                        goto read_next_fcf;
2569                }
2570        }
2571        /*
2572         * Update on failover FCF record only if it's in FCF fast-failover
2573         * period; otherwise, update on current FCF record.
2574         */
2575        if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2576                fcf_rec = &phba->fcf.failover_rec;
2577        else
2578                fcf_rec = &phba->fcf.current_rec;
2579
2580        if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2581                /*
2582                 * If the driver FCF record does not have boot flag
2583                 * set and new hba fcf record has boot flag set, use
2584                 * the new hba fcf record.
2585                 */
2586                if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2587                        /* Choose this FCF record */
2588                        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2589                                        "2837 Update current FCF record "
2590                                        "(x%x) with new FCF record (x%x)\n",
2591                                        fcf_rec->fcf_indx,
2592                                        bf_get(lpfc_fcf_record_fcf_index,
2593                                        new_fcf_record));
2594                        __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2595                                        addr_mode, vlan_id, BOOT_ENABLE);
2596                        spin_unlock_irq(&phba->hbalock);
2597                        goto read_next_fcf;
2598                }
2599                /*
2600                 * If the driver FCF record has boot flag set and the
2601                 * new hba FCF record does not have boot flag, read
2602                 * the next FCF record.
2603                 */
2604                if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
2605                        spin_unlock_irq(&phba->hbalock);
2606                        goto read_next_fcf;
2607                }
2608                /*
2609                 * If the new hba FCF record has lower priority value
2610                 * than the driver FCF record, use the new record.
2611                 */
2612                if (new_fcf_record->fip_priority < fcf_rec->priority) {
2613                        /* Choose the new FCF record with lower priority */
2614                        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2615                                        "2838 Update current FCF record "
2616                                        "(x%x) with new FCF record (x%x)\n",
2617                                        fcf_rec->fcf_indx,
2618                                        bf_get(lpfc_fcf_record_fcf_index,
2619                                               new_fcf_record));
2620                        __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2621                                        addr_mode, vlan_id, 0);
2622                        /* Reset running random FCF selection count */
2623                        phba->fcf.eligible_fcf_cnt = 1;
2624                } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2625                        /* Update running random FCF selection count */
2626                        phba->fcf.eligible_fcf_cnt++;
2627                        select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2628                                                phba->fcf.eligible_fcf_cnt);
2629                        if (select_new_fcf) {
2630                                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2631                                        "2839 Update current FCF record "
2632                                        "(x%x) with new FCF record (x%x)\n",
2633                                        fcf_rec->fcf_indx,
2634                                        bf_get(lpfc_fcf_record_fcf_index,
2635                                               new_fcf_record));
2636                                /* Choose the new FCF by random selection */
2637                                __lpfc_update_fcf_record(phba, fcf_rec,
2638                                                         new_fcf_record,
2639                                                         addr_mode, vlan_id, 0);
2640                        }
2641                }
2642                spin_unlock_irq(&phba->hbalock);
2643                goto read_next_fcf;
2644        }
2645        /*
2646         * This is the first suitable FCF record, choose this record for
2647         * initial best-fit FCF.
2648         */
2649        if (fcf_rec) {
2650                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2651                                "2840 Update initial FCF candidate "
2652                                "with FCF (x%x)\n",
2653                                bf_get(lpfc_fcf_record_fcf_index,
2654                                       new_fcf_record));
2655                __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2656                                         addr_mode, vlan_id, (boot_flag ?
2657                                         BOOT_ENABLE : 0));
2658                phba->fcf.fcf_flag |= FCF_AVAILABLE;
2659                /* Setup initial running random FCF selection count */
2660                phba->fcf.eligible_fcf_cnt = 1;
2661        }
2662        spin_unlock_irq(&phba->hbalock);
2663        goto read_next_fcf;
2664
2665read_next_fcf:
2666        lpfc_sli4_mbox_cmd_free(phba, mboxq);
2667        if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2668                if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2669                        /*
2670                         * Case of FCF fast failover scan
2671                         */
2672
2673                        /*
2674                         * It has not found any suitable FCF record, cancel
2675                         * FCF scan inprogress, and do nothing
2676                         */
2677                        if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
2678                                lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2679                                               "2782 No suitable FCF found: "
2680                                               "(x%x/x%x)\n",
2681                                               phba->fcoe_eventtag_at_fcf_scan,
2682                                               bf_get(lpfc_fcf_record_fcf_index,
2683                                                      new_fcf_record));
2684                                spin_lock_irq(&phba->hbalock);
2685                                if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2686                                        phba->hba_flag &= ~FCF_TS_INPROG;
2687                                        spin_unlock_irq(&phba->hbalock);
2688                                        /* Unregister in-use FCF and rescan */
2689                                        lpfc_printf_log(phba, KERN_INFO,
2690                                                        LOG_FIP,
2691                                                        "2864 On devloss tmo "
2692                                                        "unreg in-use FCF and "
2693                                                        "rescan FCF table\n");
2694                                        lpfc_unregister_fcf_rescan(phba);
2695                                        return;
2696                                }
2697                                /*
2698                                 * Let next new FCF event trigger fast failover
2699                                 */
2700                                phba->hba_flag &= ~FCF_TS_INPROG;
2701                                spin_unlock_irq(&phba->hbalock);
2702                                return;
2703                        }
2704                        /*
2705                         * It has found a suitable FCF record that is not
2706                         * the same as in-use FCF record, unregister the
2707                         * in-use FCF record, replace the in-use FCF record
2708                         * with the new FCF record, mark FCF fast failover
2709                         * completed, and then start register the new FCF
2710                         * record.
2711                         */
2712
2713                        /* Unregister the current in-use FCF record */
2714                        lpfc_unregister_fcf(phba);
2715
2716                        /* Replace in-use record with the new record */
2717                        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2718                                        "2842 Replace in-use FCF (x%x) "
2719                                        "with failover FCF (x%x)\n",
2720                                        phba->fcf.current_rec.fcf_indx,
2721                                        phba->fcf.failover_rec.fcf_indx);
2722                        memcpy(&phba->fcf.current_rec,
2723                               &phba->fcf.failover_rec,
2724                               sizeof(struct lpfc_fcf_rec));
2725                        /*
2726                         * Mark the fast FCF failover rediscovery completed
2727                         * and the start of the first round of the roundrobin
2728                         * FCF failover.
2729                         */
2730                        spin_lock_irq(&phba->hbalock);
2731                        phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2732                        spin_unlock_irq(&phba->hbalock);
2733                        /* Register to the new FCF record */
2734                        lpfc_register_fcf(phba);
2735                } else {
2736                        /*
2737                         * In case of transaction period to fast FCF failover,
2738                         * do nothing when search to the end of the FCF table.
2739                         */
2740                        if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2741                            (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2742                                return;
2743
2744                        if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2745                                phba->fcf.fcf_flag & FCF_IN_USE) {
2746                                /*
2747                                 * In case the current in-use FCF record no
2748                                 * longer existed during FCF discovery that
2749                                 * was not triggered by fast FCF failover
2750                                 * process, treat it as fast FCF failover.
2751                                 */
2752                                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2753                                                "2841 In-use FCF record (x%x) "
2754                                                "not reported, entering fast "
2755                                                "FCF failover mode scanning.\n",
2756                                                phba->fcf.current_rec.fcf_indx);
2757                                spin_lock_irq(&phba->hbalock);
2758                                phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2759                                spin_unlock_irq(&phba->hbalock);
2760                                lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2761                                                LPFC_FCOE_FCF_GET_FIRST);
2762                                return;
2763                        }
2764                        /* Register to the new FCF record */
2765                        lpfc_register_fcf(phba);
2766                }
2767        } else
2768                lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
2769        return;
2770
2771out:
2772        lpfc_sli4_mbox_cmd_free(phba, mboxq);
2773        lpfc_register_fcf(phba);
2774
2775        return;
2776}
2777
2778/**
2779 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
2780 * @phba: pointer to lpfc hba data structure.
2781 * @mboxq: pointer to mailbox object.
2782 *
2783 * This is the callback function for FLOGI failure roundrobin FCF failover
2784 * read FCF record mailbox command from the eligible FCF record bmask for
2785 * performing the failover. If the FCF read back is not valid/available, it
2786 * fails through to retrying FLOGI to the currently registered FCF again.
2787 * Otherwise, if the FCF read back is valid and available, it will set the
2788 * newly read FCF record to the failover FCF record, unregister currently
2789 * registered FCF record, copy the failover FCF record to the current
2790 * FCF record, and then register the current FCF record before proceeding
2791 * to trying FLOGI on the new failover FCF.
2792 */
2793void
2794lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2795{
2796        struct fcf_record *new_fcf_record;
2797        uint32_t boot_flag, addr_mode;
2798        uint16_t next_fcf_index, fcf_index;
2799        uint16_t current_fcf_index;
2800        uint16_t vlan_id;
2801        int rc;
2802
2803        /* If link state is not up, stop the roundrobin failover process */
2804        if (phba->link_state < LPFC_LINK_UP) {
2805                spin_lock_irq(&phba->hbalock);
2806                phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
2807                phba->hba_flag &= ~FCF_RR_INPROG;
2808                spin_unlock_irq(&phba->hbalock);
2809                goto out;
2810        }
2811
2812        /* Parse the FCF record from the non-embedded mailbox command */
2813        new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2814                                                      &next_fcf_index);
2815        if (!new_fcf_record) {
2816                lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2817                                "2766 Mailbox command READ_FCF_RECORD "
2818                                "failed to retrieve a FCF record. "
2819                                "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2820                                phba->fcf.fcf_flag);
2821                lpfc_unregister_fcf_rescan(phba);
2822                goto out;
2823        }
2824
2825        /* Get the needed parameters from FCF record */
2826        rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2827                                      &addr_mode, &vlan_id);
2828
2829        /* Log the FCF record information if turned on */
2830        lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2831                                      next_fcf_index);
2832
2833        fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2834        if (!rc) {
2835                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2836                                "2848 Remove ineligible FCF (x%x) from "
2837                                "from roundrobin bmask\n", fcf_index);
2838                /* Clear roundrobin bmask bit for ineligible FCF */
2839                lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2840                /* Perform next round of roundrobin FCF failover */
2841                fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2842                rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2843                if (rc)
2844                        goto out;
2845                goto error_out;
2846        }
2847
2848        if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2849                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2850                                "2760 Perform FLOGI roundrobin FCF failover: "
2851                                "FCF (x%x) back to FCF (x%x)\n",
2852                                phba->fcf.current_rec.fcf_indx, fcf_index);
2853                /* Wait 500 ms before retrying FLOGI to current FCF */
2854                msleep(500);
2855                lpfc_issue_init_vfi(phba->pport);
2856                goto out;
2857        }
2858
2859        /* Upload new FCF record to the failover FCF record */
2860        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2861                        "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2862                        phba->fcf.failover_rec.fcf_indx, fcf_index);
2863        spin_lock_irq(&phba->hbalock);
2864        __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2865                                 new_fcf_record, addr_mode, vlan_id,
2866                                 (boot_flag ? BOOT_ENABLE : 0));
2867        spin_unlock_irq(&phba->hbalock);
2868
2869        current_fcf_index = phba->fcf.current_rec.fcf_indx;
2870
2871        /* Unregister the current in-use FCF record */
2872        lpfc_unregister_fcf(phba);
2873
2874        /* Replace in-use record with the new record */
2875        memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2876               sizeof(struct lpfc_fcf_rec));
2877
2878        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2879                        "2783 Perform FLOGI roundrobin FCF failover: FCF "
2880                        "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
2881
2882error_out:
2883        lpfc_register_fcf(phba);
2884out:
2885        lpfc_sli4_mbox_cmd_free(phba, mboxq);
2886}
2887
2888/**
2889 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2890 * @phba: pointer to lpfc hba data structure.
2891 * @mboxq: pointer to mailbox object.
2892 *
2893 * This is the callback function of read FCF record mailbox command for
2894 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
2895 * failover when a new FCF event happened. If the FCF read back is
2896 * valid/available and it passes the connection list check, it updates
2897 * the bmask for the eligible FCF record for roundrobin failover.
2898 */
2899void
2900lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2901{
2902        struct fcf_record *new_fcf_record;
2903        uint32_t boot_flag, addr_mode;
2904        uint16_t fcf_index, next_fcf_index;
2905        uint16_t vlan_id;
2906        int rc;
2907
2908        /* If link state is not up, no need to proceed */
2909        if (phba->link_state < LPFC_LINK_UP)
2910                goto out;
2911
2912        /* If FCF discovery period is over, no need to proceed */
2913        if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
2914                goto out;
2915
2916        /* Parse the FCF record from the non-embedded mailbox command */
2917        new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2918                                                      &next_fcf_index);
2919        if (!new_fcf_record) {
2920                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2921                                "2767 Mailbox command READ_FCF_RECORD "
2922                                "failed to retrieve a FCF record.\n");
2923                goto out;
2924        }
2925
2926        /* Check the connection list for eligibility */
2927        rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2928                                      &addr_mode, &vlan_id);
2929
2930        /* Log the FCF record information if turned on */
2931        lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2932                                      next_fcf_index);
2933
2934        if (!rc)
2935                goto out;
2936
2937        /* Update the eligible FCF record index bmask */
2938        fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2939
2940        rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
2941
2942out:
2943        lpfc_sli4_mbox_cmd_free(phba, mboxq);
2944}
2945
2946/**
2947 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2948 * @phba: pointer to lpfc hba data structure.
2949 * @mboxq: pointer to mailbox data structure.
2950 *
2951 * This function handles completion of init vfi mailbox command.
2952 */
2953static void
2954lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2955{
2956        struct lpfc_vport *vport = mboxq->vport;
2957
2958        /*
2959         * VFI not supported on interface type 0, just do the flogi
2960         * Also continue if the VFI is in use - just use the same one.
2961         */
2962        if (mboxq->u.mb.mbxStatus &&
2963            (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2964                        LPFC_SLI_INTF_IF_TYPE_0) &&
2965            mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
2966                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2967                                 "2891 Init VFI mailbox failed 0x%x\n",
2968                                 mboxq->u.mb.mbxStatus);
2969                mempool_free(mboxq, phba->mbox_mem_pool);
2970                lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2971                return;
2972        }
2973
2974        lpfc_initial_flogi(vport);
2975        mempool_free(mboxq, phba->mbox_mem_pool);
2976        return;
2977}
2978
2979/**
2980 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2981 * @vport: pointer to lpfc_vport data structure.
2982 *
2983 * This function issue a init_vfi mailbox command to initialize the VFI and
2984 * VPI for the physical port.
2985 */
2986void
2987lpfc_issue_init_vfi(struct lpfc_vport *vport)
2988{
2989        LPFC_MBOXQ_t *mboxq;
2990        int rc;
2991        struct lpfc_hba *phba = vport->phba;
2992
2993        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2994        if (!mboxq) {
2995                lpfc_printf_vlog(vport, KERN_ERR,
2996                        LOG_TRACE_EVENT, "2892 Failed to allocate "
2997                        "init_vfi mailbox\n");
2998                return;
2999        }
3000        lpfc_init_vfi(mboxq, vport);
3001        mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
3002        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
3003        if (rc == MBX_NOT_FINISHED) {
3004                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3005                                 "2893 Failed to issue init_vfi mailbox\n");
3006                mempool_free(mboxq, vport->phba->mbox_mem_pool);
3007        }
3008}
3009
3010/**
3011 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
3012 * @phba: pointer to lpfc hba data structure.
3013 * @mboxq: pointer to mailbox data structure.
3014 *
3015 * This function handles completion of init vpi mailbox command.
3016 */
3017void
3018lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3019{
3020        struct lpfc_vport *vport = mboxq->vport;
3021        struct lpfc_nodelist *ndlp;
3022        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3023
3024        if (mboxq->u.mb.mbxStatus) {
3025                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3026                                 "2609 Init VPI mailbox failed 0x%x\n",
3027                                 mboxq->u.mb.mbxStatus);
3028                mempool_free(mboxq, phba->mbox_mem_pool);
3029                lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3030                return;
3031        }
3032        spin_lock_irq(shost->host_lock);
3033        vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
3034        spin_unlock_irq(shost->host_lock);
3035
3036        /* If this port is physical port or FDISC is done, do reg_vpi */
3037        if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
3038                        ndlp = lpfc_findnode_did(vport, Fabric_DID);
3039                        if (!ndlp)
3040                                lpfc_printf_vlog(vport, KERN_ERR,
3041                                        LOG_TRACE_EVENT,
3042                                        "2731 Cannot find fabric "
3043                                        "controller node\n");
3044                        else
3045                                lpfc_register_new_vport(phba, vport, ndlp);
3046                        mempool_free(mboxq, phba->mbox_mem_pool);
3047                        return;
3048        }
3049
3050        if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
3051                lpfc_initial_fdisc(vport);
3052        else {
3053                lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
3054                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3055                                 "2606 No NPIV Fabric support\n");
3056        }
3057        mempool_free(mboxq, phba->mbox_mem_pool);
3058        return;
3059}
3060
3061/**
3062 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
3063 * @vport: pointer to lpfc_vport data structure.
3064 *
3065 * This function issue a init_vpi mailbox command to initialize
3066 * VPI for the vport.
3067 */
3068void
3069lpfc_issue_init_vpi(struct lpfc_vport *vport)
3070{
3071        LPFC_MBOXQ_t *mboxq;
3072        int rc, vpi;
3073
3074        if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
3075                vpi = lpfc_alloc_vpi(vport->phba);
3076                if (!vpi) {
3077                        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3078                                         "3303 Failed to obtain vport vpi\n");
3079                        lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3080                        return;
3081                }
3082                vport->vpi = vpi;
3083        }
3084
3085        mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
3086        if (!mboxq) {
3087                lpfc_printf_vlog(vport, KERN_ERR,
3088                        LOG_TRACE_EVENT, "2607 Failed to allocate "
3089                        "init_vpi mailbox\n");
3090                return;
3091        }
3092        lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
3093        mboxq->vport = vport;
3094        mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
3095        rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
3096        if (rc == MBX_NOT_FINISHED) {
3097                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3098                                 "2608 Failed to issue init_vpi mailbox\n");
3099                mempool_free(mboxq, vport->phba->mbox_mem_pool);
3100        }
3101}
3102
3103/**
3104 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
3105 * @phba: pointer to lpfc hba data structure.
3106 *
3107 * This function loops through the list of vports on the @phba and issues an
3108 * FDISC if possible.
3109 */
3110void
3111lpfc_start_fdiscs(struct lpfc_hba *phba)
3112{
3113        struct lpfc_vport **vports;
3114        int i;
3115
3116        vports = lpfc_create_vport_work_array(phba);
3117        if (vports != NULL) {
3118                for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3119                        if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
3120                                continue;
3121                        /* There are no vpi for this vport */
3122                        if (vports[i]->vpi > phba->max_vpi) {
3123                                lpfc_vport_set_state(vports[i],
3124                                                     FC_VPORT_FAILED);
3125                                continue;
3126                        }
3127                        if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3128                                lpfc_vport_set_state(vports[i],
3129                                                     FC_VPORT_LINKDOWN);
3130                                continue;
3131                        }
3132                        if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
3133                                lpfc_issue_init_vpi(vports[i]);
3134                                continue;
3135                        }
3136                        if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
3137                                lpfc_initial_fdisc(vports[i]);
3138                        else {
3139                                lpfc_vport_set_state(vports[i],
3140                                                     FC_VPORT_NO_FABRIC_SUPP);
3141                                lpfc_printf_vlog(vports[i], KERN_ERR,
3142                                                 LOG_TRACE_EVENT,
3143                                                 "0259 No NPIV "
3144                                                 "Fabric support\n");
3145                        }
3146                }
3147        }
3148        lpfc_destroy_vport_work_array(phba, vports);
3149}
3150
3151void
3152lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3153{
3154        struct lpfc_dmabuf *dmabuf = mboxq->ctx_buf;
3155        struct lpfc_vport *vport = mboxq->vport;
3156        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3157
3158        /*
3159         * VFI not supported for interface type 0, so ignore any mailbox
3160         * error (except VFI in use) and continue with the discovery.
3161         */
3162        if (mboxq->u.mb.mbxStatus &&
3163            (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3164                        LPFC_SLI_INTF_IF_TYPE_0) &&
3165            mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
3166                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3167                                 "2018 REG_VFI mbxStatus error x%x "
3168                                 "HBA state x%x\n",
3169                                 mboxq->u.mb.mbxStatus, vport->port_state);
3170                if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3171                        /* FLOGI failed, use loop map to make discovery list */
3172                        lpfc_disc_list_loopmap(vport);
3173                        /* Start discovery */
3174                        lpfc_disc_start(vport);
3175                        goto out_free_mem;
3176                }
3177                lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3178                goto out_free_mem;
3179        }
3180
3181        /* If the VFI is already registered, there is nothing else to do
3182         * Unless this was a VFI update and we are in PT2PT mode, then
3183         * we should drop through to set the port state to ready.
3184         */
3185        if (vport->fc_flag & FC_VFI_REGISTERED)
3186                if (!(phba->sli_rev == LPFC_SLI_REV4 &&
3187                      vport->fc_flag & FC_PT2PT))
3188                        goto out_free_mem;
3189
3190        /* The VPI is implicitly registered when the VFI is registered */
3191        spin_lock_irq(shost->host_lock);
3192        vport->vpi_state |= LPFC_VPI_REGISTERED;
3193        vport->fc_flag |= FC_VFI_REGISTERED;
3194        vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
3195        vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
3196        spin_unlock_irq(shost->host_lock);
3197
3198        /* In case SLI4 FC loopback test, we are ready */
3199        if ((phba->sli_rev == LPFC_SLI_REV4) &&
3200            (phba->link_flag & LS_LOOPBACK_MODE)) {
3201                phba->link_state = LPFC_HBA_READY;
3202                goto out_free_mem;
3203        }
3204
3205        lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3206                         "3313 cmpl reg vfi  port_state:%x fc_flag:%x myDid:%x "
3207                         "alpacnt:%d LinkState:%x topology:%x\n",
3208                         vport->port_state, vport->fc_flag, vport->fc_myDID,
3209                         vport->phba->alpa_map[0],
3210                         phba->link_state, phba->fc_topology);
3211
3212        if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
3213                /*
3214                 * For private loop or for NPort pt2pt,
3215                 * just start discovery and we are done.
3216                 */
3217                if ((vport->fc_flag & FC_PT2PT) ||
3218                    ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
3219                    !(vport->fc_flag & FC_PUBLIC_LOOP))) {
3220
3221                        /* Use loop map to make discovery list */
3222                        lpfc_disc_list_loopmap(vport);
3223                        /* Start discovery */
3224                        if (vport->fc_flag & FC_PT2PT)
3225                                vport->port_state = LPFC_VPORT_READY;
3226                        else
3227                                lpfc_disc_start(vport);
3228                } else {
3229                        lpfc_start_fdiscs(phba);
3230                        lpfc_do_scr_ns_plogi(phba, vport);
3231                }
3232        }
3233
3234out_free_mem:
3235        mempool_free(mboxq, phba->mbox_mem_pool);
3236        if (dmabuf) {
3237                lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
3238                kfree(dmabuf);
3239        }
3240        return;
3241}
3242
3243static void
3244lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3245{
3246        MAILBOX_t *mb = &pmb->u.mb;
3247        struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3248        struct lpfc_vport  *vport = pmb->vport;
3249        struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3250        struct serv_parm *sp = &vport->fc_sparam;
3251        uint32_t ed_tov;
3252
3253        /* Check for error */
3254        if (mb->mbxStatus) {
3255                /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
3256                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3257                                 "0319 READ_SPARAM mbxStatus error x%x "
3258                                 "hba state x%x>\n",
3259                                 mb->mbxStatus, vport->port_state);
3260                lpfc_linkdown(phba);
3261                goto out;
3262        }
3263
3264        memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
3265               sizeof (struct serv_parm));
3266
3267        ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3268        if (sp->cmn.edtovResolution)    /* E_D_TOV ticks are in nanoseconds */
3269                ed_tov = (ed_tov + 999999) / 1000000;
3270
3271        phba->fc_edtov = ed_tov;
3272        phba->fc_ratov = (2 * ed_tov) / 1000;
3273        if (phba->fc_ratov < FF_DEF_RATOV) {
3274                /* RA_TOV should be atleast 10sec for initial flogi */
3275                phba->fc_ratov = FF_DEF_RATOV;
3276        }
3277
3278        lpfc_update_vport_wwn(vport);
3279        fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
3280        if (vport->port_type == LPFC_PHYSICAL_PORT) {
3281                memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3282                memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3283        }
3284
3285        lpfc_mbuf_free(phba, mp->virt, mp->phys);
3286        kfree(mp);
3287        mempool_free(pmb, phba->mbox_mem_pool);
3288
3289        /* Check if sending the FLOGI is being deferred to after we get
3290         * up to date CSPs from MBX_READ_SPARAM.
3291         */
3292        if (phba->hba_flag & HBA_DEFER_FLOGI) {
3293                lpfc_initial_flogi(vport);
3294                phba->hba_flag &= ~HBA_DEFER_FLOGI;
3295        }
3296        return;
3297
3298out:
3299        pmb->ctx_buf = NULL;
3300        lpfc_mbuf_free(phba, mp->virt, mp->phys);
3301        kfree(mp);
3302        lpfc_issue_clear_la(phba, vport);
3303        mempool_free(pmb, phba->mbox_mem_pool);
3304        return;
3305}
3306
3307static void
3308lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
3309{
3310        struct lpfc_vport *vport = phba->pport;
3311        LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
3312        struct Scsi_Host *shost;
3313        int i;
3314        struct lpfc_dmabuf *mp;
3315        int rc;
3316        struct fcf_record *fcf_record;
3317        uint32_t fc_flags = 0;
3318        unsigned long iflags;
3319
3320        spin_lock_irqsave(&phba->hbalock, iflags);
3321        phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3322
3323        if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3324                switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3325                case LPFC_LINK_SPEED_1GHZ:
3326                case LPFC_LINK_SPEED_2GHZ:
3327                case LPFC_LINK_SPEED_4GHZ:
3328                case LPFC_LINK_SPEED_8GHZ:
3329                case LPFC_LINK_SPEED_10GHZ:
3330                case LPFC_LINK_SPEED_16GHZ:
3331                case LPFC_LINK_SPEED_32GHZ:
3332                case LPFC_LINK_SPEED_64GHZ:
3333                case LPFC_LINK_SPEED_128GHZ:
3334                        break;
3335                default:
3336                        phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3337                        break;
3338                }
3339        }
3340
3341        if (phba->fc_topology &&
3342            phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3343                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3344                                "3314 Toplogy changed was 0x%x is 0x%x\n",
3345                                phba->fc_topology,
3346                                bf_get(lpfc_mbx_read_top_topology, la));
3347                phba->fc_topology_changed = 1;
3348        }
3349
3350        phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
3351        phba->link_flag &= ~(LS_NPIV_FAB_SUPPORTED | LS_CT_VEN_RPA);
3352
3353        shost = lpfc_shost_from_vport(vport);
3354        if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3355                phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
3356
3357                /* if npiv is enabled and this adapter supports npiv log
3358                 * a message that npiv is not supported in this topology
3359                 */
3360                if (phba->cfg_enable_npiv && phba->max_vpi)
3361                        lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3362                                "1309 Link Up Event npiv not supported in loop "
3363                                "topology\n");
3364                                /* Get Loop Map information */
3365                if (bf_get(lpfc_mbx_read_top_il, la))
3366                        fc_flags |= FC_LBIT;
3367
3368                vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3369                i = la->lilpBde64.tus.f.bdeSize;
3370
3371                if (i == 0) {
3372                        phba->alpa_map[0] = 0;
3373                } else {
3374                        if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
3375                                int numalpa, j, k;
3376                                union {
3377                                        uint8_t pamap[16];
3378                                        struct {
3379                                                uint32_t wd1;
3380                                                uint32_t wd2;
3381                                                uint32_t wd3;
3382                                                uint32_t wd4;
3383                                        } pa;
3384                                } un;
3385                                numalpa = phba->alpa_map[0];
3386                                j = 0;
3387                                while (j < numalpa) {
3388                                        memset(un.pamap, 0, 16);
3389                                        for (k = 1; j < numalpa; k++) {
3390                                                un.pamap[k - 1] =
3391                                                        phba->alpa_map[j + 1];
3392                                                j++;
3393                                                if (k == 16)
3394                                                        break;
3395                                        }
3396                                        /* Link Up Event ALPA map */
3397                                        lpfc_printf_log(phba,
3398                                                        KERN_WARNING,
3399                                                        LOG_LINK_EVENT,
3400                                                        "1304 Link Up Event "
3401                                                        "ALPA map Data: x%x "
3402                                                        "x%x x%x x%x\n",
3403                                                        un.pa.wd1, un.pa.wd2,
3404                                                        un.pa.wd3, un.pa.wd4);
3405                                }
3406                        }
3407                }
3408        } else {
3409                if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
3410                        if (phba->max_vpi && phba->cfg_enable_npiv &&
3411                           (phba->sli_rev >= LPFC_SLI_REV3))
3412                                phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3413                }
3414                vport->fc_myDID = phba->fc_pref_DID;
3415                fc_flags |= FC_LBIT;
3416        }
3417        spin_unlock_irqrestore(&phba->hbalock, iflags);
3418
3419        if (fc_flags) {
3420                spin_lock_irqsave(shost->host_lock, iflags);
3421                vport->fc_flag |= fc_flags;
3422                spin_unlock_irqrestore(shost->host_lock, iflags);
3423        }
3424
3425        lpfc_linkup(phba);
3426        sparam_mbox = NULL;
3427
3428        sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3429        if (!sparam_mbox)
3430                goto out;
3431
3432        rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3433        if (rc) {
3434                mempool_free(sparam_mbox, phba->mbox_mem_pool);
3435                goto out;
3436        }
3437        sparam_mbox->vport = vport;
3438        sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3439        rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3440        if (rc == MBX_NOT_FINISHED) {
3441                mp = (struct lpfc_dmabuf *)sparam_mbox->ctx_buf;
3442                lpfc_mbuf_free(phba, mp->virt, mp->phys);
3443                kfree(mp);
3444                mempool_free(sparam_mbox, phba->mbox_mem_pool);
3445                goto out;
3446        }
3447
3448        if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3449                cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3450                if (!cfglink_mbox)
3451                        goto out;
3452                vport->port_state = LPFC_LOCAL_CFG_LINK;
3453                lpfc_config_link(phba, cfglink_mbox);
3454                cfglink_mbox->vport = vport;
3455                cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
3456                rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
3457                if (rc == MBX_NOT_FINISHED) {
3458                        mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3459                        goto out;
3460                }
3461        } else {
3462                vport->port_state = LPFC_VPORT_UNKNOWN;
3463                /*
3464                 * Add the driver's default FCF record at FCF index 0 now. This
3465                 * is phase 1 implementation that support FCF index 0 and driver
3466                 * defaults.
3467                 */
3468                if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
3469                        fcf_record = kzalloc(sizeof(struct fcf_record),
3470                                        GFP_KERNEL);
3471                        if (unlikely(!fcf_record)) {
3472                                lpfc_printf_log(phba, KERN_ERR,
3473                                        LOG_TRACE_EVENT,
3474                                        "2554 Could not allocate memory for "
3475                                        "fcf record\n");
3476                                rc = -ENODEV;
3477                                goto out;
3478                        }
3479
3480                        lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3481                                                LPFC_FCOE_FCF_DEF_INDEX);
3482                        rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3483                        if (unlikely(rc)) {
3484                                lpfc_printf_log(phba, KERN_ERR,
3485                                        LOG_TRACE_EVENT,
3486                                        "2013 Could not manually add FCF "
3487                                        "record 0, status %d\n", rc);
3488                                rc = -ENODEV;
3489                                kfree(fcf_record);
3490                                goto out;
3491                        }
3492                        kfree(fcf_record);
3493                }
3494                /*
3495                 * The driver is expected to do FIP/FCF. Call the port
3496                 * and get the FCF Table.
3497                 */
3498                spin_lock_irqsave(&phba->hbalock, iflags);
3499                if (phba->hba_flag & FCF_TS_INPROG) {
3500                        spin_unlock_irqrestore(&phba->hbalock, iflags);
3501                        return;
3502                }
3503                /* This is the initial FCF discovery scan */
3504                phba->fcf.fcf_flag |= FCF_INIT_DISC;
3505                spin_unlock_irqrestore(&phba->hbalock, iflags);
3506                lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3507                                "2778 Start FCF table scan at linkup\n");
3508                rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3509                                                     LPFC_FCOE_FCF_GET_FIRST);
3510                if (rc) {
3511                        spin_lock_irqsave(&phba->hbalock, iflags);
3512                        phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3513                        spin_unlock_irqrestore(&phba->hbalock, iflags);
3514                        goto out;
3515                }
3516                /* Reset FCF roundrobin bmask for new discovery */
3517                lpfc_sli4_clear_fcf_rr_bmask(phba);
3518        }
3519
3520        /* Prepare for LINK up registrations */
3521        memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
3522        scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
3523                  init_utsname()->nodename);
3524        return;
3525out:
3526        lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3527        lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3528                         "0263 Discovery Mailbox error: state: 0x%x : x%px x%px\n",
3529                         vport->port_state, sparam_mbox, cfglink_mbox);
3530        lpfc_issue_clear_la(phba, vport);
3531        return;
3532}
3533
3534static void
3535lpfc_enable_la(struct lpfc_hba *phba)
3536{
3537        uint32_t control;
3538        struct lpfc_sli *psli = &phba->sli;
3539        spin_lock_irq(&phba->hbalock);
3540        psli->sli_flag |= LPFC_PROCESS_LA;
3541        if (phba->sli_rev <= LPFC_SLI_REV3) {
3542                control = readl(phba->HCregaddr);
3543                control |= HC_LAINT_ENA;
3544                writel(control, phba->HCregaddr);
3545                readl(phba->HCregaddr); /* flush */
3546        }
3547        spin_unlock_irq(&phba->hbalock);
3548}
3549
3550static void
3551lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3552{
3553        lpfc_linkdown(phba);
3554        lpfc_enable_la(phba);
3555        lpfc_unregister_unused_fcf(phba);
3556        /* turn on Link Attention interrupts - no CLEAR_LA needed */
3557}
3558
3559
3560/*
3561 * This routine handles processing a READ_TOPOLOGY mailbox
3562 * command upon completion. It is setup in the LPFC_MBOXQ
3563 * as the completion routine when the command is
3564 * handed off to the SLI layer. SLI4 only.
3565 */
3566void
3567lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3568{
3569        struct lpfc_vport *vport = pmb->vport;
3570        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3571        struct lpfc_mbx_read_top *la;
3572        struct lpfc_sli_ring *pring;
3573        MAILBOX_t *mb = &pmb->u.mb;
3574        struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3575        uint8_t attn_type;
3576        unsigned long iflags;
3577
3578        /* Unblock ELS traffic */
3579        pring = lpfc_phba_elsring(phba);
3580        if (pring)
3581                pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3582
3583        /* Check for error */
3584        if (mb->mbxStatus) {
3585                lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3586                                "1307 READ_LA mbox error x%x state x%x\n",
3587                                mb->mbxStatus, vport->port_state);
3588                lpfc_mbx_issue_link_down(phba);
3589                phba->link_state = LPFC_HBA_ERROR;
3590                goto lpfc_mbx_cmpl_read_topology_free_mbuf;
3591        }
3592
3593        la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3594        attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
3595
3596        memcpy(&phba->alpa_map[0], mp->virt, 128);
3597
3598        spin_lock_irqsave(shost->host_lock, iflags);
3599        if (bf_get(lpfc_mbx_read_top_pb, la))
3600                vport->fc_flag |= FC_BYPASSED_MODE;
3601        else
3602                vport->fc_flag &= ~FC_BYPASSED_MODE;
3603        spin_unlock_irqrestore(shost->host_lock, iflags);
3604
3605        if (phba->fc_eventTag <= la->eventTag) {
3606                phba->fc_stat.LinkMultiEvent++;
3607                if (attn_type == LPFC_ATT_LINK_UP)
3608                        if (phba->fc_eventTag != 0)
3609                                lpfc_linkdown(phba);
3610        }
3611
3612        phba->fc_eventTag = la->eventTag;
3613        if (phba->sli_rev < LPFC_SLI_REV4) {
3614                spin_lock_irqsave(&phba->hbalock, iflags);
3615                if (bf_get(lpfc_mbx_read_top_mm, la))
3616                        phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3617                else
3618                        phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3619                spin_unlock_irqrestore(&phba->hbalock, iflags);
3620        }
3621
3622        phba->link_events++;
3623        if ((attn_type == LPFC_ATT_LINK_UP) &&
3624            !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
3625                phba->fc_stat.LinkUp++;
3626                if (phba->link_flag & LS_LOOPBACK_MODE) {
3627                        lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3628                                        "1306 Link Up Event in loop back mode "
3629                                        "x%x received Data: x%x x%x x%x x%x\n",
3630                                        la->eventTag, phba->fc_eventTag,
3631                                        bf_get(lpfc_mbx_read_top_alpa_granted,
3632                                               la),
3633                                        bf_get(lpfc_mbx_read_top_link_spd, la),
3634                                        phba->alpa_map[0]);
3635                } else {
3636                        lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3637                                        "1303 Link Up Event x%x received "
3638                                        "Data: x%x x%x x%x x%x x%x x%x %d\n",
3639                                        la->eventTag, phba->fc_eventTag,
3640                                        bf_get(lpfc_mbx_read_top_alpa_granted,
3641                                               la),
3642                                        bf_get(lpfc_mbx_read_top_link_spd, la),
3643                                        phba->alpa_map[0],
3644                                        bf_get(lpfc_mbx_read_top_mm, la),
3645                                        bf_get(lpfc_mbx_read_top_fa, la),
3646                                        phba->wait_4_mlo_maint_flg);
3647                }
3648                lpfc_mbx_process_link_up(phba, la);
3649        } else if (attn_type == LPFC_ATT_LINK_DOWN ||
3650                   attn_type == LPFC_ATT_UNEXP_WWPN) {
3651                phba->fc_stat.LinkDown++;
3652                if (phba->link_flag & LS_LOOPBACK_MODE)
3653                        lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3654                                "1308 Link Down Event in loop back mode "
3655                                "x%x received "
3656                                "Data: x%x x%x x%x\n",
3657                                la->eventTag, phba->fc_eventTag,
3658                                phba->pport->port_state, vport->fc_flag);
3659                else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3660                        lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3661                                "1313 Link Down Unexpected FA WWPN Event x%x "
3662                                "received Data: x%x x%x x%x x%x x%x\n",
3663                                la->eventTag, phba->fc_eventTag,
3664                                phba->pport->port_state, vport->fc_flag,
3665                                bf_get(lpfc_mbx_read_top_mm, la),
3666                                bf_get(lpfc_mbx_read_top_fa, la));
3667                else
3668                        lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3669                                "1305 Link Down Event x%x received "
3670                                "Data: x%x x%x x%x x%x x%x\n",
3671                                la->eventTag, phba->fc_eventTag,
3672                                phba->pport->port_state, vport->fc_flag,
3673                                bf_get(lpfc_mbx_read_top_mm, la),
3674                                bf_get(lpfc_mbx_read_top_fa, la));
3675                lpfc_mbx_issue_link_down(phba);
3676        }
3677        if (phba->sli.sli_flag & LPFC_MENLO_MAINT &&
3678            attn_type == LPFC_ATT_LINK_UP) {
3679                if (phba->link_state != LPFC_LINK_DOWN) {
3680                        phba->fc_stat.LinkDown++;
3681                        lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3682                                "1312 Link Down Event x%x received "
3683                                "Data: x%x x%x x%x\n",
3684                                la->eventTag, phba->fc_eventTag,
3685                                phba->pport->port_state, vport->fc_flag);
3686                        lpfc_mbx_issue_link_down(phba);
3687                } else
3688                        lpfc_enable_la(phba);
3689
3690                lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3691                                "1310 Menlo Maint Mode Link up Event x%x rcvd "
3692                                "Data: x%x x%x x%x\n",
3693                                la->eventTag, phba->fc_eventTag,
3694                                phba->pport->port_state, vport->fc_flag);
3695                /*
3696                 * The cmnd that triggered this will be waiting for this
3697                 * signal.
3698                 */
3699                /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3700                if (phba->wait_4_mlo_maint_flg) {
3701                        phba->wait_4_mlo_maint_flg = 0;
3702                        wake_up_interruptible(&phba->wait_4_mlo_m_q);
3703                }
3704        }
3705
3706        if ((phba->sli_rev < LPFC_SLI_REV4) &&
3707            bf_get(lpfc_mbx_read_top_fa, la)) {
3708                if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
3709                        lpfc_issue_clear_la(phba, vport);
3710                lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3711                                "1311 fa %d\n",
3712                                bf_get(lpfc_mbx_read_top_fa, la));
3713        }
3714
3715lpfc_mbx_cmpl_read_topology_free_mbuf:
3716        lpfc_mbuf_free(phba, mp->virt, mp->phys);
3717        kfree(mp);
3718        mempool_free(pmb, phba->mbox_mem_pool);
3719        return;
3720}
3721
3722/*
3723 * This routine handles processing a REG_LOGIN mailbox
3724 * command upon completion. It is setup in the LPFC_MBOXQ
3725 * as the completion routine when the command is
3726 * handed off to the SLI layer.
3727 */
3728void
3729lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3730{
3731        struct lpfc_vport  *vport = pmb->vport;
3732        struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3733        struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3734
3735        pmb->ctx_buf = NULL;
3736        pmb->ctx_ndlp = NULL;
3737
3738        lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NODE | LOG_DISCOVERY,
3739                         "0002 rpi:%x DID:%x flg:%x %d x%px\n",
3740                         ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3741                         kref_read(&ndlp->kref),
3742                         ndlp);
3743        if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3744                ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3745
3746        if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3747            ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
3748                /* We rcvd a rscn after issuing this
3749                 * mbox reg login, we may have cycled
3750                 * back through the state and be
3751                 * back at reg login state so this
3752                 * mbox needs to be ignored becase
3753                 * there is another reg login in
3754                 * process.
3755                 */
3756                spin_lock_irq(&ndlp->lock);
3757                ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3758                spin_unlock_irq(&ndlp->lock);
3759
3760                /*
3761                 * We cannot leave the RPI registered because
3762                 * if we go thru discovery again for this ndlp
3763                 * a subsequent REG_RPI will fail.
3764                 */
3765                ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3766                lpfc_unreg_rpi(vport, ndlp);
3767        }
3768
3769        /* Call state machine */
3770        lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
3771
3772        lpfc_mbuf_free(phba, mp->virt, mp->phys);
3773        kfree(mp);
3774        mempool_free(pmb, phba->mbox_mem_pool);
3775        /* decrement the node reference count held for this callback
3776         * function.
3777         */
3778        lpfc_nlp_put(ndlp);
3779
3780        return;
3781}
3782
3783static void
3784lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3785{
3786        MAILBOX_t *mb = &pmb->u.mb;
3787        struct lpfc_vport *vport = pmb->vport;
3788        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3789
3790        switch (mb->mbxStatus) {
3791        case 0x0011:
3792        case 0x0020:
3793                lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3794                                 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3795                                 mb->mbxStatus);
3796                break;
3797        /* If VPI is busy, reset the HBA */
3798        case 0x9700:
3799                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3800                        "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3801                        vport->vpi, mb->mbxStatus);
3802                if (!(phba->pport->load_flag & FC_UNLOADING))
3803                        lpfc_workq_post_event(phba, NULL, NULL,
3804                                LPFC_EVT_RESET_HBA);
3805        }
3806        spin_lock_irq(shost->host_lock);
3807        vport->vpi_state &= ~LPFC_VPI_REGISTERED;
3808        vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3809        spin_unlock_irq(shost->host_lock);
3810        vport->unreg_vpi_cmpl = VPORT_OK;
3811        mempool_free(pmb, phba->mbox_mem_pool);
3812        lpfc_cleanup_vports_rrqs(vport, NULL);
3813        /*
3814         * This shost reference might have been taken at the beginning of
3815         * lpfc_vport_delete()
3816         */
3817        if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
3818                scsi_host_put(shost);
3819}
3820
3821int
3822lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3823{
3824        struct lpfc_hba  *phba = vport->phba;
3825        LPFC_MBOXQ_t *mbox;
3826        int rc;
3827
3828        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3829        if (!mbox)
3830                return 1;
3831
3832        lpfc_unreg_vpi(phba, vport->vpi, mbox);
3833        mbox->vport = vport;
3834        mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
3835        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3836        if (rc == MBX_NOT_FINISHED) {
3837                lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3838                                 "1800 Could not issue unreg_vpi\n");
3839                mempool_free(mbox, phba->mbox_mem_pool);
3840                vport->unreg_vpi_cmpl = VPORT_ERROR;
3841                return rc;
3842        }
3843        return 0;
3844}
3845
3846static void
3847lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3848{
3849        struct lpfc_vport *vport = pmb->vport;
3850        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3851        MAILBOX_t *mb = &pmb->u.mb;
3852
3853        switch (mb->mbxStatus) {
3854        case 0x0011:
3855        case 0x9601:
3856        case 0x9602:
3857                lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3858                                 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3859                                 mb->mbxStatus);
3860                lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3861                spin_lock_irq(shost->host_lock);
3862                vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3863                spin_unlock_irq(shost->host_lock);
3864                vport->fc_myDID = 0;
3865
3866                if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3867                    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3868                        if (phba->nvmet_support)
3869                                lpfc_nvmet_update_targetport(phba);
3870                        else
3871                                lpfc_nvme_update_l