linux/drivers/scsi/fcoe/fcoe_ctlr.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2008-2009 Cisco Systems, Inc.  All rights reserved.
   3 * Copyright (c) 2009 Intel Corporation.  All rights reserved.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms and conditions of the GNU General Public License,
   7 * version 2, as published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 * You should have received a copy of the GNU General Public License along with
  15 * this program; if not, write to the Free Software Foundation, Inc.,
  16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17 *
  18 * Maintained at www.Open-FCoE.org
  19 */
  20
  21#include <linux/types.h>
  22#include <linux/module.h>
  23#include <linux/kernel.h>
  24#include <linux/list.h>
  25#include <linux/spinlock.h>
  26#include <linux/timer.h>
  27#include <linux/netdevice.h>
  28#include <linux/etherdevice.h>
  29#include <linux/ethtool.h>
  30#include <linux/if_ether.h>
  31#include <linux/if_vlan.h>
  32#include <linux/errno.h>
  33#include <linux/bitops.h>
  34#include <linux/slab.h>
  35#include <net/rtnetlink.h>
  36
  37#include <scsi/fc/fc_els.h>
  38#include <scsi/fc/fc_fs.h>
  39#include <scsi/fc/fc_fip.h>
  40#include <scsi/fc/fc_encaps.h>
  41#include <scsi/fc/fc_fcoe.h>
  42#include <scsi/fc/fc_fcp.h>
  43
  44#include <scsi/libfc.h>
  45#include <scsi/libfcoe.h>
  46
  47#include "libfcoe.h"
  48
  49#define FCOE_CTLR_MIN_FKA       500             /* min keep alive (mS) */
  50#define FCOE_CTLR_DEF_FKA       FIP_DEF_FKA     /* default keep alive (mS) */
  51
  52static void fcoe_ctlr_timeout(unsigned long);
  53static void fcoe_ctlr_timer_work(struct work_struct *);
  54static void fcoe_ctlr_recv_work(struct work_struct *);
  55static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
  56
  57static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
  58static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
  59static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
  60static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
  61
  62static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
  63
  64static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
  65static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
  66static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
  67static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
  68
  69static const char * const fcoe_ctlr_states[] = {
  70        [FIP_ST_DISABLED] =     "DISABLED",
  71        [FIP_ST_LINK_WAIT] =    "LINK_WAIT",
  72        [FIP_ST_AUTO] =         "AUTO",
  73        [FIP_ST_NON_FIP] =      "NON_FIP",
  74        [FIP_ST_ENABLED] =      "ENABLED",
  75        [FIP_ST_VNMP_START] =   "VNMP_START",
  76        [FIP_ST_VNMP_PROBE1] =  "VNMP_PROBE1",
  77        [FIP_ST_VNMP_PROBE2] =  "VNMP_PROBE2",
  78        [FIP_ST_VNMP_CLAIM] =   "VNMP_CLAIM",
  79        [FIP_ST_VNMP_UP] =      "VNMP_UP",
  80};
  81
  82static const char *fcoe_ctlr_state(enum fip_state state)
  83{
  84        const char *cp = "unknown";
  85
  86        if (state < ARRAY_SIZE(fcoe_ctlr_states))
  87                cp = fcoe_ctlr_states[state];
  88        if (!cp)
  89                cp = "unknown";
  90        return cp;
  91}
  92
  93/**
  94 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
  95 * @fip: The FCoE controller
  96 * @state: The new state
  97 */
  98static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
  99{
 100        if (state == fip->state)
 101                return;
 102        if (fip->lp)
 103                LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
 104                        fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
 105        fip->state = state;
 106}
 107
 108/**
 109 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
 110 * @fcf: The FCF to check
 111 *
 112 * Return non-zero if FCF fcoe_size has been validated.
 113 */
 114static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
 115{
 116        return (fcf->flags & FIP_FL_SOL) != 0;
 117}
 118
 119/**
 120 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
 121 * @fcf: The FCF to check
 122 *
 123 * Return non-zero if the FCF is usable.
 124 */
 125static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
 126{
 127        u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
 128
 129        return (fcf->flags & flags) == flags;
 130}
 131
 132/**
 133 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
 134 * @fip: The FCoE controller
 135 */
 136static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
 137{
 138        if (fip->mode == FIP_MODE_VN2VN)
 139                hton24(fip->dest_addr, FIP_VN_FC_MAP);
 140        else
 141                hton24(fip->dest_addr, FIP_DEF_FC_MAP);
 142        hton24(fip->dest_addr + 3, 0);
 143        fip->map_dest = 1;
 144}
 145
 146/**
 147 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
 148 * @fip: The FCoE controller to initialize
 149 */
 150void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
 151{
 152        fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
 153        fip->mode = mode;
 154        fip->fip_resp = false;
 155        INIT_LIST_HEAD(&fip->fcfs);
 156        mutex_init(&fip->ctlr_mutex);
 157        spin_lock_init(&fip->ctlr_lock);
 158        fip->flogi_oxid = FC_XID_UNKNOWN;
 159        setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
 160        INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
 161        INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
 162        skb_queue_head_init(&fip->fip_recv_list);
 163}
 164EXPORT_SYMBOL(fcoe_ctlr_init);
 165
 166/**
 167 * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
 168 * @new: The newly discovered FCF
 169 *
 170 * Called with fip->ctlr_mutex held
 171 */
 172static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
 173{
 174        struct fcoe_ctlr *fip = new->fip;
 175        struct fcoe_ctlr_device *ctlr_dev;
 176        struct fcoe_fcf_device *temp, *fcf_dev;
 177        int rc = -ENOMEM;
 178
 179        LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
 180                        new->fabric_name, new->fcf_mac);
 181
 182        temp = kzalloc(sizeof(*temp), GFP_KERNEL);
 183        if (!temp)
 184                goto out;
 185
 186        temp->fabric_name = new->fabric_name;
 187        temp->switch_name = new->switch_name;
 188        temp->fc_map = new->fc_map;
 189        temp->vfid = new->vfid;
 190        memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
 191        temp->priority = new->pri;
 192        temp->fka_period = new->fka_period;
 193        temp->selected = 0; /* default to unselected */
 194
 195        /*
 196         * If ctlr_dev doesn't exist then it means we're a libfcoe user
 197         * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
 198         * fnic would be an example of a driver with this behavior. In this
 199         * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
 200         * don't want to make sysfs changes.
 201         */
 202
 203        ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
 204        if (ctlr_dev) {
 205                mutex_lock(&ctlr_dev->lock);
 206                fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
 207                if (unlikely(!fcf_dev)) {
 208                        rc = -ENOMEM;
 209                        mutex_unlock(&ctlr_dev->lock);
 210                        goto out;
 211                }
 212
 213                /*
 214                 * The fcoe_sysfs layer can return a CONNECTED fcf that
 215                 * has a priv (fcf was never deleted) or a CONNECTED fcf
 216                 * that doesn't have a priv (fcf was deleted). However,
 217                 * libfcoe will always delete FCFs before trying to add
 218                 * them. This is ensured because both recv_adv and
 219                 * age_fcfs are protected by the the fcoe_ctlr's mutex.
 220                 * This means that we should never get a FCF with a
 221                 * non-NULL priv pointer.
 222                 */
 223                BUG_ON(fcf_dev->priv);
 224
 225                fcf_dev->priv = new;
 226                new->fcf_dev = fcf_dev;
 227                mutex_unlock(&ctlr_dev->lock);
 228        }
 229
 230        list_add(&new->list, &fip->fcfs);
 231        fip->fcf_count++;
 232        rc = 0;
 233
 234out:
 235        kfree(temp);
 236        return rc;
 237}
 238
 239/**
 240 * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
 241 * @new: The FCF to be removed
 242 *
 243 * Called with fip->ctlr_mutex held
 244 */
 245static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
 246{
 247        struct fcoe_ctlr *fip = new->fip;
 248        struct fcoe_ctlr_device *cdev;
 249        struct fcoe_fcf_device *fcf_dev;
 250
 251        list_del(&new->list);
 252        fip->fcf_count--;
 253
 254        /*
 255         * If ctlr_dev doesn't exist then it means we're a libfcoe user
 256         * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
 257         * or a fcoe_fcf_device.
 258         *
 259         * fnic would be an example of a driver with this behavior. In this
 260         * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
 261         * but we don't want to make sysfs changes.
 262         */
 263        cdev = fcoe_ctlr_to_ctlr_dev(fip);
 264        if (cdev) {
 265                mutex_lock(&cdev->lock);
 266                fcf_dev = fcoe_fcf_to_fcf_dev(new);
 267                WARN_ON(!fcf_dev);
 268                new->fcf_dev = NULL;
 269                fcoe_fcf_device_delete(fcf_dev);
 270                kfree(new);
 271                mutex_unlock(&cdev->lock);
 272        }
 273}
 274
 275/**
 276 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
 277 * @fip: The FCoE controller whose FCFs are to be reset
 278 *
 279 * Called with &fcoe_ctlr lock held.
 280 */
 281static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
 282{
 283        struct fcoe_fcf *fcf;
 284        struct fcoe_fcf *next;
 285
 286        fip->sel_fcf = NULL;
 287        list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
 288                fcoe_sysfs_fcf_del(fcf);
 289        }
 290        WARN_ON(fip->fcf_count);
 291
 292        fip->sel_time = 0;
 293}
 294
 295/**
 296 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
 297 * @fip: The FCoE controller to tear down
 298 *
 299 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
 300 *
 301 * The receive handler will have been deleted before this to guarantee
 302 * that no more recv_work will be scheduled.
 303 *
 304 * The timer routine will simply return once we set FIP_ST_DISABLED.
 305 * This guarantees that no further timeouts or work will be scheduled.
 306 */
 307void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
 308{
 309        cancel_work_sync(&fip->recv_work);
 310        skb_queue_purge(&fip->fip_recv_list);
 311
 312        mutex_lock(&fip->ctlr_mutex);
 313        fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
 314        fcoe_ctlr_reset_fcfs(fip);
 315        mutex_unlock(&fip->ctlr_mutex);
 316        del_timer_sync(&fip->timer);
 317        cancel_work_sync(&fip->timer_work);
 318}
 319EXPORT_SYMBOL(fcoe_ctlr_destroy);
 320
 321/**
 322 * fcoe_ctlr_announce() - announce new FCF selection
 323 * @fip: The FCoE controller
 324 *
 325 * Also sets the destination MAC for FCoE and control packets
 326 *
 327 * Called with neither ctlr_mutex nor ctlr_lock held.
 328 */
 329static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
 330{
 331        struct fcoe_fcf *sel;
 332        struct fcoe_fcf *fcf;
 333
 334        mutex_lock(&fip->ctlr_mutex);
 335        spin_lock_bh(&fip->ctlr_lock);
 336
 337        kfree_skb(fip->flogi_req);
 338        fip->flogi_req = NULL;
 339        list_for_each_entry(fcf, &fip->fcfs, list)
 340                fcf->flogi_sent = 0;
 341
 342        spin_unlock_bh(&fip->ctlr_lock);
 343        sel = fip->sel_fcf;
 344
 345        if (sel && ether_addr_equal(sel->fcf_mac, fip->dest_addr))
 346                goto unlock;
 347        if (!is_zero_ether_addr(fip->dest_addr)) {
 348                printk(KERN_NOTICE "libfcoe: host%d: "
 349                       "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
 350                       fip->lp->host->host_no, fip->dest_addr);
 351                memset(fip->dest_addr, 0, ETH_ALEN);
 352        }
 353        if (sel) {
 354                printk(KERN_INFO "libfcoe: host%d: FIP selected "
 355                       "Fibre-Channel Forwarder MAC %pM\n",
 356                       fip->lp->host->host_no, sel->fcf_mac);
 357                memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
 358                fip->map_dest = 0;
 359        }
 360unlock:
 361        mutex_unlock(&fip->ctlr_mutex);
 362}
 363
 364/**
 365 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
 366 * @fip: The FCoE controller to get the maximum FCoE size from
 367 *
 368 * Returns the maximum packet size including the FCoE header and trailer,
 369 * but not including any Ethernet or VLAN headers.
 370 */
 371static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
 372{
 373        /*
 374         * Determine the max FCoE frame size allowed, including
 375         * FCoE header and trailer.
 376         * Note:  lp->mfs is currently the payload size, not the frame size.
 377         */
 378        return fip->lp->mfs + sizeof(struct fc_frame_header) +
 379                sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
 380}
 381
 382/**
 383 * fcoe_ctlr_solicit() - Send a FIP solicitation
 384 * @fip: The FCoE controller to send the solicitation on
 385 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
 386 */
 387static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
 388{
 389        struct sk_buff *skb;
 390        struct fip_sol {
 391                struct ethhdr eth;
 392                struct fip_header fip;
 393                struct {
 394                        struct fip_mac_desc mac;
 395                        struct fip_wwn_desc wwnn;
 396                        struct fip_size_desc size;
 397                } __packed desc;
 398        }  __packed * sol;
 399        u32 fcoe_size;
 400
 401        skb = dev_alloc_skb(sizeof(*sol));
 402        if (!skb)
 403                return;
 404
 405        sol = (struct fip_sol *)skb->data;
 406
 407        memset(sol, 0, sizeof(*sol));
 408        memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
 409        memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
 410        sol->eth.h_proto = htons(ETH_P_FIP);
 411
 412        sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 413        sol->fip.fip_op = htons(FIP_OP_DISC);
 414        sol->fip.fip_subcode = FIP_SC_SOL;
 415        sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
 416        sol->fip.fip_flags = htons(FIP_FL_FPMA);
 417        if (fip->spma)
 418                sol->fip.fip_flags |= htons(FIP_FL_SPMA);
 419
 420        sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
 421        sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
 422        memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
 423
 424        sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
 425        sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
 426        put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
 427
 428        fcoe_size = fcoe_ctlr_fcoe_size(fip);
 429        sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
 430        sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
 431        sol->desc.size.fd_size = htons(fcoe_size);
 432
 433        skb_put(skb, sizeof(*sol));
 434        skb->protocol = htons(ETH_P_FIP);
 435        skb->priority = fip->priority;
 436        skb_reset_mac_header(skb);
 437        skb_reset_network_header(skb);
 438        fip->send(fip, skb);
 439
 440        if (!fcf)
 441                fip->sol_time = jiffies;
 442}
 443
 444/**
 445 * fcoe_ctlr_link_up() - Start FCoE controller
 446 * @fip: The FCoE controller to start
 447 *
 448 * Called from the LLD when the network link is ready.
 449 */
 450void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
 451{
 452        mutex_lock(&fip->ctlr_mutex);
 453        if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
 454                mutex_unlock(&fip->ctlr_mutex);
 455                fc_linkup(fip->lp);
 456        } else if (fip->state == FIP_ST_LINK_WAIT) {
 457                fcoe_ctlr_set_state(fip, fip->mode);
 458                switch (fip->mode) {
 459                default:
 460                        LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
 461                        /* fall-through */
 462                case FIP_MODE_AUTO:
 463                        LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
 464                        /* fall-through */
 465                case FIP_MODE_FABRIC:
 466                case FIP_MODE_NON_FIP:
 467                        mutex_unlock(&fip->ctlr_mutex);
 468                        fc_linkup(fip->lp);
 469                        fcoe_ctlr_solicit(fip, NULL);
 470                        break;
 471                case FIP_MODE_VN2VN:
 472                        fcoe_ctlr_vn_start(fip);
 473                        mutex_unlock(&fip->ctlr_mutex);
 474                        fc_linkup(fip->lp);
 475                        break;
 476                }
 477        } else
 478                mutex_unlock(&fip->ctlr_mutex);
 479}
 480EXPORT_SYMBOL(fcoe_ctlr_link_up);
 481
 482/**
 483 * fcoe_ctlr_reset() - Reset a FCoE controller
 484 * @fip:       The FCoE controller to reset
 485 */
 486static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
 487{
 488        fcoe_ctlr_reset_fcfs(fip);
 489        del_timer(&fip->timer);
 490        fip->ctlr_ka_time = 0;
 491        fip->port_ka_time = 0;
 492        fip->sol_time = 0;
 493        fip->flogi_oxid = FC_XID_UNKNOWN;
 494        fcoe_ctlr_map_dest(fip);
 495}
 496
 497/**
 498 * fcoe_ctlr_link_down() - Stop a FCoE controller
 499 * @fip: The FCoE controller to be stopped
 500 *
 501 * Returns non-zero if the link was up and now isn't.
 502 *
 503 * Called from the LLD when the network link is not ready.
 504 * There may be multiple calls while the link is down.
 505 */
 506int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
 507{
 508        int link_dropped;
 509
 510        LIBFCOE_FIP_DBG(fip, "link down.\n");
 511        mutex_lock(&fip->ctlr_mutex);
 512        fcoe_ctlr_reset(fip);
 513        link_dropped = fip->state != FIP_ST_LINK_WAIT;
 514        fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
 515        mutex_unlock(&fip->ctlr_mutex);
 516
 517        if (link_dropped)
 518                fc_linkdown(fip->lp);
 519        return link_dropped;
 520}
 521EXPORT_SYMBOL(fcoe_ctlr_link_down);
 522
 523/**
 524 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
 525 * @fip:   The FCoE controller to send the FKA on
 526 * @lport: libfc fc_lport to send from
 527 * @ports: 0 for controller keep-alive, 1 for port keep-alive
 528 * @sa:    The source MAC address
 529 *
 530 * A controller keep-alive is sent every fka_period (typically 8 seconds).
 531 * The source MAC is the native MAC address.
 532 *
 533 * A port keep-alive is sent every 90 seconds while logged in.
 534 * The source MAC is the assigned mapped source address.
 535 * The destination is the FCF's F-port.
 536 */
 537static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
 538                                      struct fc_lport *lport,
 539                                      int ports, u8 *sa)
 540{
 541        struct sk_buff *skb;
 542        struct fip_kal {
 543                struct ethhdr eth;
 544                struct fip_header fip;
 545                struct fip_mac_desc mac;
 546        } __packed * kal;
 547        struct fip_vn_desc *vn;
 548        u32 len;
 549        struct fc_lport *lp;
 550        struct fcoe_fcf *fcf;
 551
 552        fcf = fip->sel_fcf;
 553        lp = fip->lp;
 554        if (!fcf || (ports && !lp->port_id))
 555                return;
 556
 557        len = sizeof(*kal) + ports * sizeof(*vn);
 558        skb = dev_alloc_skb(len);
 559        if (!skb)
 560                return;
 561
 562        kal = (struct fip_kal *)skb->data;
 563        memset(kal, 0, len);
 564        memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
 565        memcpy(kal->eth.h_source, sa, ETH_ALEN);
 566        kal->eth.h_proto = htons(ETH_P_FIP);
 567
 568        kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 569        kal->fip.fip_op = htons(FIP_OP_CTRL);
 570        kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
 571        kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
 572                                     ports * sizeof(*vn)) / FIP_BPW);
 573        kal->fip.fip_flags = htons(FIP_FL_FPMA);
 574        if (fip->spma)
 575                kal->fip.fip_flags |= htons(FIP_FL_SPMA);
 576
 577        kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
 578        kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
 579        memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
 580        if (ports) {
 581                vn = (struct fip_vn_desc *)(kal + 1);
 582                vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
 583                vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
 584                memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
 585                hton24(vn->fd_fc_id, lport->port_id);
 586                put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
 587        }
 588        skb_put(skb, len);
 589        skb->protocol = htons(ETH_P_FIP);
 590        skb->priority = fip->priority;
 591        skb_reset_mac_header(skb);
 592        skb_reset_network_header(skb);
 593        fip->send(fip, skb);
 594}
 595
 596/**
 597 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
 598 * @fip:   The FCoE controller for the ELS frame
 599 * @dtype: The FIP descriptor type for the frame
 600 * @skb:   The FCoE ELS frame including FC header but no FCoE headers
 601 * @d_id:  The destination port ID.
 602 *
 603 * Returns non-zero error code on failure.
 604 *
 605 * The caller must check that the length is a multiple of 4.
 606 *
 607 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
 608 * Headroom includes the FIP encapsulation description, FIP header, and
 609 * Ethernet header.  The tailroom is for the FIP MAC descriptor.
 610 */
 611static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
 612                            u8 dtype, struct sk_buff *skb, u32 d_id)
 613{
 614        struct fip_encaps_head {
 615                struct ethhdr eth;
 616                struct fip_header fip;
 617                struct fip_encaps encaps;
 618        } __packed * cap;
 619        struct fc_frame_header *fh;
 620        struct fip_mac_desc *mac;
 621        struct fcoe_fcf *fcf;
 622        size_t dlen;
 623        u16 fip_flags;
 624        u8 op;
 625
 626        fh = (struct fc_frame_header *)skb->data;
 627        op = *(u8 *)(fh + 1);
 628        dlen = sizeof(struct fip_encaps) + skb->len;    /* len before push */
 629        cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
 630        memset(cap, 0, sizeof(*cap));
 631
 632        if (lport->point_to_multipoint) {
 633                if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
 634                        return -ENODEV;
 635                fip_flags = 0;
 636        } else {
 637                fcf = fip->sel_fcf;
 638                if (!fcf)
 639                        return -ENODEV;
 640                fip_flags = fcf->flags;
 641                fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
 642                                         FIP_FL_FPMA;
 643                if (!fip_flags)
 644                        return -ENODEV;
 645                memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
 646        }
 647        memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
 648        cap->eth.h_proto = htons(ETH_P_FIP);
 649
 650        cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 651        cap->fip.fip_op = htons(FIP_OP_LS);
 652        if (op == ELS_LS_ACC || op == ELS_LS_RJT)
 653                cap->fip.fip_subcode = FIP_SC_REP;
 654        else
 655                cap->fip.fip_subcode = FIP_SC_REQ;
 656        cap->fip.fip_flags = htons(fip_flags);
 657
 658        cap->encaps.fd_desc.fip_dtype = dtype;
 659        cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
 660
 661        if (op != ELS_LS_RJT) {
 662                dlen += sizeof(*mac);
 663                mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
 664                memset(mac, 0, sizeof(*mac));
 665                mac->fd_desc.fip_dtype = FIP_DT_MAC;
 666                mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
 667                if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
 668                        memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
 669                } else if (fip->mode == FIP_MODE_VN2VN) {
 670                        hton24(mac->fd_mac, FIP_VN_FC_MAP);
 671                        hton24(mac->fd_mac + 3, fip->port_id);
 672                } else if (fip_flags & FIP_FL_SPMA) {
 673                        LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
 674                        memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
 675                } else {
 676                        LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
 677                        /* FPMA only FLOGI.  Must leave the MAC desc zeroed. */
 678                }
 679        }
 680        cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
 681
 682        skb->protocol = htons(ETH_P_FIP);
 683        skb->priority = fip->priority;
 684        skb_reset_mac_header(skb);
 685        skb_reset_network_header(skb);
 686        return 0;
 687}
 688
 689/**
 690 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
 691 * @fip:        FCoE controller.
 692 * @lport:      libfc fc_lport to send from
 693 * @skb:        FCoE ELS frame including FC header but no FCoE headers.
 694 *
 695 * Returns a non-zero error code if the frame should not be sent.
 696 * Returns zero if the caller should send the frame with FCoE encapsulation.
 697 *
 698 * The caller must check that the length is a multiple of 4.
 699 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
 700 * The the skb must also be an fc_frame.
 701 *
 702 * This is called from the lower-level driver with spinlocks held,
 703 * so we must not take a mutex here.
 704 */
 705int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
 706                       struct sk_buff *skb)
 707{
 708        struct fc_frame *fp;
 709        struct fc_frame_header *fh;
 710        u16 old_xid;
 711        u8 op;
 712        u8 mac[ETH_ALEN];
 713
 714        fp = container_of(skb, struct fc_frame, skb);
 715        fh = (struct fc_frame_header *)skb->data;
 716        op = *(u8 *)(fh + 1);
 717
 718        if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
 719                old_xid = fip->flogi_oxid;
 720                fip->flogi_oxid = ntohs(fh->fh_ox_id);
 721                if (fip->state == FIP_ST_AUTO) {
 722                        if (old_xid == FC_XID_UNKNOWN)
 723                                fip->flogi_count = 0;
 724                        fip->flogi_count++;
 725                        if (fip->flogi_count < 3)
 726                                goto drop;
 727                        fcoe_ctlr_map_dest(fip);
 728                        return 0;
 729                }
 730                if (fip->state == FIP_ST_NON_FIP)
 731                        fcoe_ctlr_map_dest(fip);
 732        }
 733
 734        if (fip->state == FIP_ST_NON_FIP)
 735                return 0;
 736        if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
 737                goto drop;
 738        switch (op) {
 739        case ELS_FLOGI:
 740                op = FIP_DT_FLOGI;
 741                if (fip->mode == FIP_MODE_VN2VN)
 742                        break;
 743                spin_lock_bh(&fip->ctlr_lock);
 744                kfree_skb(fip->flogi_req);
 745                fip->flogi_req = skb;
 746                fip->flogi_req_send = 1;
 747                spin_unlock_bh(&fip->ctlr_lock);
 748                schedule_work(&fip->timer_work);
 749                return -EINPROGRESS;
 750        case ELS_FDISC:
 751                if (ntoh24(fh->fh_s_id))
 752                        return 0;
 753                op = FIP_DT_FDISC;
 754                break;
 755        case ELS_LOGO:
 756                if (fip->mode == FIP_MODE_VN2VN) {
 757                        if (fip->state != FIP_ST_VNMP_UP)
 758                                return -EINVAL;
 759                        if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
 760                                return -EINVAL;
 761                } else {
 762                        if (fip->state != FIP_ST_ENABLED)
 763                                return 0;
 764                        if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
 765                                return 0;
 766                }
 767                op = FIP_DT_LOGO;
 768                break;
 769        case ELS_LS_ACC:
 770                /*
 771                 * If non-FIP, we may have gotten an SID by accepting an FLOGI
 772                 * from a point-to-point connection.  Switch to using
 773                 * the source mac based on the SID.  The destination
 774                 * MAC in this case would have been set by receiving the
 775                 * FLOGI.
 776                 */
 777                if (fip->state == FIP_ST_NON_FIP) {
 778                        if (fip->flogi_oxid == FC_XID_UNKNOWN)
 779                                return 0;
 780                        fip->flogi_oxid = FC_XID_UNKNOWN;
 781                        fc_fcoe_set_mac(mac, fh->fh_d_id);
 782                        fip->update_mac(lport, mac);
 783                }
 784                /* fall through */
 785        case ELS_LS_RJT:
 786                op = fr_encaps(fp);
 787                if (op)
 788                        break;
 789                return 0;
 790        default:
 791                if (fip->state != FIP_ST_ENABLED &&
 792                    fip->state != FIP_ST_VNMP_UP)
 793                        goto drop;
 794                return 0;
 795        }
 796        LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
 797                        op, ntoh24(fh->fh_d_id));
 798        if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
 799                goto drop;
 800        fip->send(fip, skb);
 801        return -EINPROGRESS;
 802drop:
 803        kfree_skb(skb);
 804        return -EINVAL;
 805}
 806EXPORT_SYMBOL(fcoe_ctlr_els_send);
 807
 808/**
 809 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
 810 * @fip: The FCoE controller to free FCFs on
 811 *
 812 * Called with lock held and preemption disabled.
 813 *
 814 * An FCF is considered old if we have missed two advertisements.
 815 * That is, there have been no valid advertisement from it for 2.5
 816 * times its keep-alive period.
 817 *
 818 * In addition, determine the time when an FCF selection can occur.
 819 *
 820 * Also, increment the MissDiscAdvCount when no advertisement is received
 821 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
 822 *
 823 * Returns the time in jiffies for the next call.
 824 */
 825static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
 826{
 827        struct fcoe_fcf *fcf;
 828        struct fcoe_fcf *next;
 829        unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
 830        unsigned long deadline;
 831        unsigned long sel_time = 0;
 832        struct list_head del_list;
 833        struct fc_stats *stats;
 834
 835        INIT_LIST_HEAD(&del_list);
 836
 837        stats = per_cpu_ptr(fip->lp->stats, get_cpu());
 838
 839        list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
 840                deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
 841                if (fip->sel_fcf == fcf) {
 842                        if (time_after(jiffies, deadline)) {
 843                                stats->MissDiscAdvCount++;
 844                                printk(KERN_INFO "libfcoe: host%d: "
 845                                       "Missing Discovery Advertisement "
 846                                       "for fab %16.16llx count %lld\n",
 847                                       fip->lp->host->host_no, fcf->fabric_name,
 848                                       stats->MissDiscAdvCount);
 849                        } else if (time_after(next_timer, deadline))
 850                                next_timer = deadline;
 851                }
 852
 853                deadline += fcf->fka_period;
 854                if (time_after_eq(jiffies, deadline)) {
 855                        if (fip->sel_fcf == fcf)
 856                                fip->sel_fcf = NULL;
 857                        /*
 858                         * Move to delete list so we can call
 859                         * fcoe_sysfs_fcf_del (which can sleep)
 860                         * after the put_cpu().
 861                         */
 862                        list_del(&fcf->list);
 863                        list_add(&fcf->list, &del_list);
 864                        stats->VLinkFailureCount++;
 865                } else {
 866                        if (time_after(next_timer, deadline))
 867                                next_timer = deadline;
 868                        if (fcoe_ctlr_mtu_valid(fcf) &&
 869                            (!sel_time || time_before(sel_time, fcf->time)))
 870                                sel_time = fcf->time;
 871                }
 872        }
 873        put_cpu();
 874
 875        list_for_each_entry_safe(fcf, next, &del_list, list) {
 876                /* Removes fcf from current list */
 877                fcoe_sysfs_fcf_del(fcf);
 878        }
 879
 880        if (sel_time && !fip->sel_fcf && !fip->sel_time) {
 881                sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
 882                fip->sel_time = sel_time;
 883        }
 884
 885        return next_timer;
 886}
 887
 888/**
 889 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
 890 * @fip: The FCoE controller receiving the advertisement
 891 * @skb: The received FIP advertisement frame
 892 * @fcf: The resulting FCF entry
 893 *
 894 * Returns zero on a valid parsed advertisement,
 895 * otherwise returns non zero value.
 896 */
 897static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
 898                               struct sk_buff *skb, struct fcoe_fcf *fcf)
 899{
 900        struct fip_header *fiph;
 901        struct fip_desc *desc = NULL;
 902        struct fip_wwn_desc *wwn;
 903        struct fip_fab_desc *fab;
 904        struct fip_fka_desc *fka;
 905        unsigned long t;
 906        size_t rlen;
 907        size_t dlen;
 908        u32 desc_mask;
 909
 910        memset(fcf, 0, sizeof(*fcf));
 911        fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
 912
 913        fiph = (struct fip_header *)skb->data;
 914        fcf->flags = ntohs(fiph->fip_flags);
 915
 916        /*
 917         * mask of required descriptors. validating each one clears its bit.
 918         */
 919        desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
 920                        BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
 921
 922        rlen = ntohs(fiph->fip_dl_len) * 4;
 923        if (rlen + sizeof(*fiph) > skb->len)
 924                return -EINVAL;
 925
 926        desc = (struct fip_desc *)(fiph + 1);
 927        while (rlen > 0) {
 928                dlen = desc->fip_dlen * FIP_BPW;
 929                if (dlen < sizeof(*desc) || dlen > rlen)
 930                        return -EINVAL;
 931                /* Drop Adv if there are duplicate critical descriptors */
 932                if ((desc->fip_dtype < 32) &&
 933                    !(desc_mask & 1U << desc->fip_dtype)) {
 934                        LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
 935                                        "Descriptors in FIP adv\n");
 936                        return -EINVAL;
 937                }
 938                switch (desc->fip_dtype) {
 939                case FIP_DT_PRI:
 940                        if (dlen != sizeof(struct fip_pri_desc))
 941                                goto len_err;
 942                        fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
 943                        desc_mask &= ~BIT(FIP_DT_PRI);
 944                        break;
 945                case FIP_DT_MAC:
 946                        if (dlen != sizeof(struct fip_mac_desc))
 947                                goto len_err;
 948                        memcpy(fcf->fcf_mac,
 949                               ((struct fip_mac_desc *)desc)->fd_mac,
 950                               ETH_ALEN);
 951                        memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
 952                        if (!is_valid_ether_addr(fcf->fcf_mac)) {
 953                                LIBFCOE_FIP_DBG(fip,
 954                                        "Invalid MAC addr %pM in FIP adv\n",
 955                                        fcf->fcf_mac);
 956                                return -EINVAL;
 957                        }
 958                        desc_mask &= ~BIT(FIP_DT_MAC);
 959                        break;
 960                case FIP_DT_NAME:
 961                        if (dlen != sizeof(struct fip_wwn_desc))
 962                                goto len_err;
 963                        wwn = (struct fip_wwn_desc *)desc;
 964                        fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
 965                        desc_mask &= ~BIT(FIP_DT_NAME);
 966                        break;
 967                case FIP_DT_FAB:
 968                        if (dlen != sizeof(struct fip_fab_desc))
 969                                goto len_err;
 970                        fab = (struct fip_fab_desc *)desc;
 971                        fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
 972                        fcf->vfid = ntohs(fab->fd_vfid);
 973                        fcf->fc_map = ntoh24(fab->fd_map);
 974                        desc_mask &= ~BIT(FIP_DT_FAB);
 975                        break;
 976                case FIP_DT_FKA:
 977                        if (dlen != sizeof(struct fip_fka_desc))
 978                                goto len_err;
 979                        fka = (struct fip_fka_desc *)desc;
 980                        if (fka->fd_flags & FIP_FKA_ADV_D)
 981                                fcf->fd_flags = 1;
 982                        t = ntohl(fka->fd_fka_period);
 983                        if (t >= FCOE_CTLR_MIN_FKA)
 984                                fcf->fka_period = msecs_to_jiffies(t);
 985                        desc_mask &= ~BIT(FIP_DT_FKA);
 986                        break;
 987                case FIP_DT_MAP_OUI:
 988                case FIP_DT_FCOE_SIZE:
 989                case FIP_DT_FLOGI:
 990                case FIP_DT_FDISC:
 991                case FIP_DT_LOGO:
 992                case FIP_DT_ELP:
 993                default:
 994                        LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
 995                                        "in FIP adv\n", desc->fip_dtype);
 996                        /* standard says ignore unknown descriptors >= 128 */
 997                        if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
 998                                return -EINVAL;
 999                        break;
1000                }
1001                desc = (struct fip_desc *)((char *)desc + dlen);
1002                rlen -= dlen;
1003        }
1004        if (!fcf->fc_map || (fcf->fc_map & 0x10000))
1005                return -EINVAL;
1006        if (!fcf->switch_name)
1007                return -EINVAL;
1008        if (desc_mask) {
1009                LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
1010                                desc_mask);
1011                return -EINVAL;
1012        }
1013        return 0;
1014
1015len_err:
1016        LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1017                        desc->fip_dtype, dlen);
1018        return -EINVAL;
1019}
1020
1021/**
1022 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
1023 * @fip: The FCoE controller receiving the advertisement
1024 * @skb: The received FIP packet
1025 */
1026static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1027{
1028        struct fcoe_fcf *fcf;
1029        struct fcoe_fcf new;
1030        unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
1031        int first = 0;
1032        int mtu_valid;
1033        int found = 0;
1034        int rc = 0;
1035
1036        if (fcoe_ctlr_parse_adv(fip, skb, &new))
1037                return;
1038
1039        mutex_lock(&fip->ctlr_mutex);
1040        first = list_empty(&fip->fcfs);
1041        list_for_each_entry(fcf, &fip->fcfs, list) {
1042                if (fcf->switch_name == new.switch_name &&
1043                    fcf->fabric_name == new.fabric_name &&
1044                    fcf->fc_map == new.fc_map &&
1045                    ether_addr_equal(fcf->fcf_mac, new.fcf_mac)) {
1046                        found = 1;
1047                        break;
1048                }
1049        }
1050        if (!found) {
1051                if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1052                        goto out;
1053
1054                fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1055                if (!fcf)
1056                        goto out;
1057
1058                memcpy(fcf, &new, sizeof(new));
1059                fcf->fip = fip;
1060                rc = fcoe_sysfs_fcf_add(fcf);
1061                if (rc) {
1062                        printk(KERN_ERR "Failed to allocate sysfs instance "
1063                               "for FCF, fab %16.16llx mac %pM\n",
1064                               new.fabric_name, new.fcf_mac);
1065                        kfree(fcf);
1066                        goto out;
1067                }
1068        } else {
1069                /*
1070                 * Update the FCF's keep-alive descriptor flags.
1071                 * Other flag changes from new advertisements are
1072                 * ignored after a solicited advertisement is
1073                 * received and the FCF is selectable (usable).
1074                 */
1075                fcf->fd_flags = new.fd_flags;
1076                if (!fcoe_ctlr_fcf_usable(fcf))
1077                        fcf->flags = new.flags;
1078
1079                if (fcf == fip->sel_fcf && !fcf->fd_flags) {
1080                        fip->ctlr_ka_time -= fcf->fka_period;
1081                        fip->ctlr_ka_time += new.fka_period;
1082                        if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1083                                mod_timer(&fip->timer, fip->ctlr_ka_time);
1084                }
1085                fcf->fka_period = new.fka_period;
1086                memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1087        }
1088
1089        mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1090        fcf->time = jiffies;
1091        if (!found)
1092                LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1093                                fcf->fabric_name, fcf->fcf_mac);
1094
1095        /*
1096         * If this advertisement is not solicited and our max receive size
1097         * hasn't been verified, send a solicited advertisement.
1098         */
1099        if (!mtu_valid)
1100                fcoe_ctlr_solicit(fip, fcf);
1101
1102        /*
1103         * If its been a while since we did a solicit, and this is
1104         * the first advertisement we've received, do a multicast
1105         * solicitation to gather as many advertisements as we can
1106         * before selection occurs.
1107         */
1108        if (first && time_after(jiffies, fip->sol_time + sol_tov))
1109                fcoe_ctlr_solicit(fip, NULL);
1110
1111        /*
1112         * Put this FCF at the head of the list for priority among equals.
1113         * This helps in the case of an NPV switch which insists we use
1114         * the FCF that answers multicast solicitations, not the others that
1115         * are sending periodic multicast advertisements.
1116         */
1117        if (mtu_valid)
1118                list_move(&fcf->list, &fip->fcfs);
1119
1120        /*
1121         * If this is the first validated FCF, note the time and
1122         * set a timer to trigger selection.
1123         */
1124        if (mtu_valid && !fip->sel_fcf && !fip->sel_time &&
1125            fcoe_ctlr_fcf_usable(fcf)) {
1126                fip->sel_time = jiffies +
1127                        msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1128                if (!timer_pending(&fip->timer) ||
1129                    time_before(fip->sel_time, fip->timer.expires))
1130                        mod_timer(&fip->timer, fip->sel_time);
1131        }
1132
1133out:
1134        mutex_unlock(&fip->ctlr_mutex);
1135}
1136
1137/**
1138 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1139 * @fip: The FCoE controller which received the packet
1140 * @skb: The received FIP packet
1141 */
1142static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1143{
1144        struct fc_lport *lport = fip->lp;
1145        struct fip_header *fiph;
1146        struct fc_frame *fp = (struct fc_frame *)skb;
1147        struct fc_frame_header *fh = NULL;
1148        struct fip_desc *desc;
1149        struct fip_encaps *els;
1150        struct fcoe_fcf *sel;
1151        struct fc_stats *stats;
1152        enum fip_desc_type els_dtype = 0;
1153        u8 els_op;
1154        u8 sub;
1155        u8 granted_mac[ETH_ALEN] = { 0 };
1156        size_t els_len = 0;
1157        size_t rlen;
1158        size_t dlen;
1159        u32 desc_mask = 0;
1160        u32 desc_cnt = 0;
1161
1162        fiph = (struct fip_header *)skb->data;
1163        sub = fiph->fip_subcode;
1164        if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1165                goto drop;
1166
1167        rlen = ntohs(fiph->fip_dl_len) * 4;
1168        if (rlen + sizeof(*fiph) > skb->len)
1169                goto drop;
1170
1171        desc = (struct fip_desc *)(fiph + 1);
1172        while (rlen > 0) {
1173                desc_cnt++;
1174                dlen = desc->fip_dlen * FIP_BPW;
1175                if (dlen < sizeof(*desc) || dlen > rlen)
1176                        goto drop;
1177                /* Drop ELS if there are duplicate critical descriptors */
1178                if (desc->fip_dtype < 32) {
1179                        if ((desc->fip_dtype != FIP_DT_MAC) &&
1180                            (desc_mask & 1U << desc->fip_dtype)) {
1181                                LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1182                                                "Descriptors in FIP ELS\n");
1183                                goto drop;
1184                        }
1185                        desc_mask |= (1 << desc->fip_dtype);
1186                }
1187                switch (desc->fip_dtype) {
1188                case FIP_DT_MAC:
1189                        sel = fip->sel_fcf;
1190                        if (desc_cnt == 1) {
1191                                LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1192                                                "received out of order\n");
1193                                goto drop;
1194                        }
1195                        /*
1196                         * Some switch implementations send two MAC descriptors,
1197                         * with first MAC(granted_mac) being the FPMA, and the
1198                         * second one(fcoe_mac) is used as destination address
1199                         * for sending/receiving FCoE packets. FIP traffic is
1200                         * sent using fip_mac. For regular switches, both
1201                         * fip_mac and fcoe_mac would be the same.
1202                         */
1203                        if (desc_cnt == 2)
1204                                memcpy(granted_mac,
1205                                       ((struct fip_mac_desc *)desc)->fd_mac,
1206                                       ETH_ALEN);
1207
1208                        if (dlen != sizeof(struct fip_mac_desc))
1209                                goto len_err;
1210
1211                        if ((desc_cnt == 3) && (sel))
1212                                memcpy(sel->fcoe_mac,
1213                                       ((struct fip_mac_desc *)desc)->fd_mac,
1214                                       ETH_ALEN);
1215                        break;
1216                case FIP_DT_FLOGI:
1217                case FIP_DT_FDISC:
1218                case FIP_DT_LOGO:
1219                case FIP_DT_ELP:
1220                        if (desc_cnt != 1) {
1221                                LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1222                                                "received out of order\n");
1223                                goto drop;
1224                        }
1225                        if (fh)
1226                                goto drop;
1227                        if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1228                                goto len_err;
1229                        els_len = dlen - sizeof(*els);
1230                        els = (struct fip_encaps *)desc;
1231                        fh = (struct fc_frame_header *)(els + 1);
1232                        els_dtype = desc->fip_dtype;
1233                        break;
1234                default:
1235                        LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1236                                        "in FIP adv\n", desc->fip_dtype);
1237                        /* standard says ignore unknown descriptors >= 128 */
1238                        if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1239                                goto drop;
1240                        if (desc_cnt <= 2) {
1241                                LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1242                                                "received out of order\n");
1243                                goto drop;
1244                        }
1245                        break;
1246                }
1247                desc = (struct fip_desc *)((char *)desc + dlen);
1248                rlen -= dlen;
1249        }
1250
1251        if (!fh)
1252                goto drop;
1253        els_op = *(u8 *)(fh + 1);
1254
1255        if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1256            sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1257                if (els_op == ELS_LS_ACC) {
1258                        if (!is_valid_ether_addr(granted_mac)) {
1259                                LIBFCOE_FIP_DBG(fip,
1260                                        "Invalid MAC address %pM in FIP ELS\n",
1261                                        granted_mac);
1262                                goto drop;
1263                        }
1264                        memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1265
1266                        if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1267                                fip->flogi_oxid = FC_XID_UNKNOWN;
1268                                if (els_dtype == FIP_DT_FLOGI)
1269                                        fcoe_ctlr_announce(fip);
1270                        }
1271                } else if (els_dtype == FIP_DT_FLOGI &&
1272                           !fcoe_ctlr_flogi_retry(fip))
1273                        goto drop;      /* retrying FLOGI so drop reject */
1274        }
1275
1276        if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1277            (!(1U << FIP_DT_MAC & desc_mask)))) {
1278                LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1279                                "in FIP ELS\n");
1280                goto drop;
1281        }
1282
1283        /*
1284         * Convert skb into an fc_frame containing only the ELS.
1285         */
1286        skb_pull(skb, (u8 *)fh - skb->data);
1287        skb_trim(skb, els_len);
1288        fp = (struct fc_frame *)skb;
1289        fc_frame_init(fp);
1290        fr_sof(fp) = FC_SOF_I3;
1291        fr_eof(fp) = FC_EOF_T;
1292        fr_dev(fp) = lport;
1293        fr_encaps(fp) = els_dtype;
1294
1295        stats = per_cpu_ptr(lport->stats, get_cpu());
1296        stats->RxFrames++;
1297        stats->RxWords += skb->len / FIP_BPW;
1298        put_cpu();
1299
1300        fc_exch_recv(lport, fp);
1301        return;
1302
1303len_err:
1304        LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1305                        desc->fip_dtype, dlen);
1306drop:
1307        kfree_skb(skb);
1308}
1309
1310/**
1311 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1312 * @fip: The FCoE controller that received the frame
1313 * @fh:  The received FIP header
1314 *
1315 * There may be multiple VN_Port descriptors.
1316 * The overall length has already been checked.
1317 */
1318static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1319                                     struct fip_header *fh)
1320{
1321        struct fip_desc *desc;
1322        struct fip_mac_desc *mp;
1323        struct fip_wwn_desc *wp;
1324        struct fip_vn_desc *vp;
1325        size_t rlen;
1326        size_t dlen;
1327        struct fcoe_fcf *fcf = fip->sel_fcf;
1328        struct fc_lport *lport = fip->lp;
1329        struct fc_lport *vn_port = NULL;
1330        u32 desc_mask;
1331        int num_vlink_desc;
1332        int reset_phys_port = 0;
1333        struct fip_vn_desc **vlink_desc_arr = NULL;
1334
1335        LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1336
1337        if (!fcf || !lport->port_id) {
1338                /*
1339                 * We are yet to select best FCF, but we got CVL in the
1340                 * meantime. reset the ctlr and let it rediscover the FCF
1341                 */
1342                mutex_lock(&fip->ctlr_mutex);
1343                fcoe_ctlr_reset(fip);
1344                mutex_unlock(&fip->ctlr_mutex);
1345                return;
1346        }
1347
1348        /*
1349         * mask of required descriptors.  Validating each one clears its bit.
1350         */
1351        desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1352
1353        rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1354        desc = (struct fip_desc *)(fh + 1);
1355
1356        /*
1357         * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1358         * before determining max Vx_Port descriptor but a buggy FCF could have
1359         * omited either or both MAC Address and Name Identifier descriptors
1360         */
1361        num_vlink_desc = rlen / sizeof(*vp);
1362        if (num_vlink_desc)
1363                vlink_desc_arr = kmalloc(sizeof(vp) * num_vlink_desc,
1364                                         GFP_ATOMIC);
1365        if (!vlink_desc_arr)
1366                return;
1367        num_vlink_desc = 0;
1368
1369        while (rlen >= sizeof(*desc)) {
1370                dlen = desc->fip_dlen * FIP_BPW;
1371                if (dlen > rlen)
1372                        goto err;
1373                /* Drop CVL if there are duplicate critical descriptors */
1374                if ((desc->fip_dtype < 32) &&
1375                    (desc->fip_dtype != FIP_DT_VN_ID) &&
1376                    !(desc_mask & 1U << desc->fip_dtype)) {
1377                        LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1378                                        "Descriptors in FIP CVL\n");
1379                        goto err;
1380                }
1381                switch (desc->fip_dtype) {
1382                case FIP_DT_MAC:
1383                        mp = (struct fip_mac_desc *)desc;
1384                        if (dlen < sizeof(*mp))
1385                                goto err;
1386                        if (!ether_addr_equal(mp->fd_mac, fcf->fcf_mac))
1387                                goto err;
1388                        desc_mask &= ~BIT(FIP_DT_MAC);
1389                        break;
1390                case FIP_DT_NAME:
1391                        wp = (struct fip_wwn_desc *)desc;
1392                        if (dlen < sizeof(*wp))
1393                                goto err;
1394                        if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1395                                goto err;
1396                        desc_mask &= ~BIT(FIP_DT_NAME);
1397                        break;
1398                case FIP_DT_VN_ID:
1399                        vp = (struct fip_vn_desc *)desc;
1400                        if (dlen < sizeof(*vp))
1401                                goto err;
1402                        vlink_desc_arr[num_vlink_desc++] = vp;
1403                        vn_port = fc_vport_id_lookup(lport,
1404                                                      ntoh24(vp->fd_fc_id));
1405                        if (vn_port && (vn_port == lport)) {
1406                                mutex_lock(&fip->ctlr_mutex);
1407                                per_cpu_ptr(lport->stats,
1408                                            get_cpu())->VLinkFailureCount++;
1409                                put_cpu();
1410                                fcoe_ctlr_reset(fip);
1411                                mutex_unlock(&fip->ctlr_mutex);
1412                        }
1413                        break;
1414                default:
1415                        /* standard says ignore unknown descriptors >= 128 */
1416                        if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1417                                goto err;
1418                        break;
1419                }
1420                desc = (struct fip_desc *)((char *)desc + dlen);
1421                rlen -= dlen;
1422        }
1423
1424        /*
1425         * reset only if all required descriptors were present and valid.
1426         */
1427        if (desc_mask)
1428                LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1429                                desc_mask);
1430        else if (!num_vlink_desc) {
1431                LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1432                /*
1433                 * No Vx_Port description. Clear all NPIV ports,
1434                 * followed by physical port
1435                 */
1436                mutex_lock(&fip->ctlr_mutex);
1437                per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++;
1438                put_cpu();
1439                fcoe_ctlr_reset(fip);
1440                mutex_unlock(&fip->ctlr_mutex);
1441
1442                mutex_lock(&lport->lp_mutex);
1443                list_for_each_entry(vn_port, &lport->vports, list)
1444                        fc_lport_reset(vn_port);
1445                mutex_unlock(&lport->lp_mutex);
1446
1447                fc_lport_reset(fip->lp);
1448                fcoe_ctlr_solicit(fip, NULL);
1449        } else {
1450                int i;
1451
1452                LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1453                for (i = 0; i < num_vlink_desc; i++) {
1454                        vp = vlink_desc_arr[i];
1455                        vn_port = fc_vport_id_lookup(lport,
1456                                                     ntoh24(vp->fd_fc_id));
1457                        if (!vn_port)
1458                                continue;
1459
1460                        /*
1461                         * 'port_id' is already validated, check MAC address and
1462                         * wwpn
1463                         */
1464                        if (!ether_addr_equal(fip->get_src_addr(vn_port),
1465                                              vp->fd_mac) ||
1466                                get_unaligned_be64(&vp->fd_wwpn) !=
1467                                                        vn_port->wwpn)
1468                                continue;
1469
1470                        if (vn_port == lport)
1471                                /*
1472                                 * Physical port, defer processing till all
1473                                 * listed NPIV ports are cleared
1474                                 */
1475                                reset_phys_port = 1;
1476                        else    /* NPIV port */
1477                                fc_lport_reset(vn_port);
1478                }
1479
1480                if (reset_phys_port) {
1481                        fc_lport_reset(fip->lp);
1482                        fcoe_ctlr_solicit(fip, NULL);
1483                }
1484        }
1485
1486err:
1487        kfree(vlink_desc_arr);
1488}
1489
1490/**
1491 * fcoe_ctlr_recv() - Receive a FIP packet
1492 * @fip: The FCoE controller that received the packet
1493 * @skb: The received FIP packet
1494 *
1495 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1496 */
1497void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1498{
1499        skb = skb_share_check(skb, GFP_ATOMIC);
1500        if (!skb)
1501                return;
1502        skb_queue_tail(&fip->fip_recv_list, skb);
1503        schedule_work(&fip->recv_work);
1504}
1505EXPORT_SYMBOL(fcoe_ctlr_recv);
1506
1507/**
1508 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1509 * @fip: The FCoE controller that received the frame
1510 * @skb: The received FIP frame
1511 *
1512 * Returns non-zero if the frame is dropped.
1513 */
1514static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1515{
1516        struct fip_header *fiph;
1517        struct ethhdr *eh;
1518        enum fip_state state;
1519        bool fip_vlan_resp = false;
1520        u16 op;
1521        u8 sub;
1522
1523        if (skb_linearize(skb))
1524                goto drop;
1525        if (skb->len < sizeof(*fiph))
1526                goto drop;
1527        eh = eth_hdr(skb);
1528        if (fip->mode == FIP_MODE_VN2VN) {
1529                if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1530                    !ether_addr_equal(eh->h_dest, fcoe_all_vn2vn) &&
1531                    !ether_addr_equal(eh->h_dest, fcoe_all_p2p))
1532                        goto drop;
1533        } else if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1534                   !ether_addr_equal(eh->h_dest, fcoe_all_enode))
1535                goto drop;
1536        fiph = (struct fip_header *)skb->data;
1537        op = ntohs(fiph->fip_op);
1538        sub = fiph->fip_subcode;
1539
1540        if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1541                goto drop;
1542        if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1543                goto drop;
1544
1545        mutex_lock(&fip->ctlr_mutex);
1546        state = fip->state;
1547        if (state == FIP_ST_AUTO) {
1548                fip->map_dest = 0;
1549                fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1550                state = FIP_ST_ENABLED;
1551                LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1552        }
1553        fip_vlan_resp = fip->fip_resp;
1554        mutex_unlock(&fip->ctlr_mutex);
1555
1556        if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1557                return fcoe_ctlr_vn_recv(fip, skb);
1558
1559        if (fip_vlan_resp && op == FIP_OP_VLAN) {
1560                LIBFCOE_FIP_DBG(fip, "fip vlan discovery\n");
1561                return fcoe_ctlr_vlan_recv(fip, skb);
1562        }
1563
1564        if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1565            state != FIP_ST_VNMP_CLAIM)
1566                goto drop;
1567
1568        if (op == FIP_OP_LS) {
1569                fcoe_ctlr_recv_els(fip, skb);   /* consumes skb */
1570                return 0;
1571        }
1572
1573        if (state != FIP_ST_ENABLED)
1574                goto drop;
1575
1576        if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1577                fcoe_ctlr_recv_adv(fip, skb);
1578        else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1579                fcoe_ctlr_recv_clr_vlink(fip, fiph);
1580        kfree_skb(skb);
1581        return 0;
1582drop:
1583        kfree_skb(skb);
1584        return -1;
1585}
1586
1587/**
1588 * fcoe_ctlr_select() - Select the best FCF (if possible)
1589 * @fip: The FCoE controller
1590 *
1591 * Returns the selected FCF, or NULL if none are usable.
1592 *
1593 * If there are conflicting advertisements, no FCF can be chosen.
1594 *
1595 * If there is already a selected FCF, this will choose a better one or
1596 * an equivalent one that hasn't already been sent a FLOGI.
1597 *
1598 * Called with lock held.
1599 */
1600static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1601{
1602        struct fcoe_fcf *fcf;
1603        struct fcoe_fcf *best = fip->sel_fcf;
1604
1605        list_for_each_entry(fcf, &fip->fcfs, list) {
1606                LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1607                                "VFID %d mac %pM map %x val %d "
1608                                "sent %u pri %u\n",
1609                                fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1610                                fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1611                                fcf->flogi_sent, fcf->pri);
1612                if (!fcoe_ctlr_fcf_usable(fcf)) {
1613                        LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1614                                        "map %x %svalid %savailable\n",
1615                                        fcf->fabric_name, fcf->fc_map,
1616                                        (fcf->flags & FIP_FL_SOL) ? "" : "in",
1617                                        (fcf->flags & FIP_FL_AVAIL) ?
1618                                        "" : "un");
1619                        continue;
1620                }
1621                if (!best || fcf->pri < best->pri || best->flogi_sent)
1622                        best = fcf;
1623                if (fcf->fabric_name != best->fabric_name ||
1624                    fcf->vfid != best->vfid ||
1625                    fcf->fc_map != best->fc_map) {
1626                        LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1627                                        "or FC-MAP\n");
1628                        return NULL;
1629                }
1630        }
1631        fip->sel_fcf = best;
1632        if (best) {
1633                LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1634                fip->port_ka_time = jiffies +
1635                        msecs_to_jiffies(FIP_VN_KA_PERIOD);
1636                fip->ctlr_ka_time = jiffies + best->fka_period;
1637                if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1638                        mod_timer(&fip->timer, fip->ctlr_ka_time);
1639        }
1640        return best;
1641}
1642
1643/**
1644 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1645 * @fip: The FCoE controller
1646 *
1647 * Returns non-zero error if it could not be sent.
1648 *
1649 * Called with ctlr_mutex and ctlr_lock held.
1650 * Caller must verify that fip->sel_fcf is not NULL.
1651 */
1652static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1653{
1654        struct sk_buff *skb;
1655        struct sk_buff *skb_orig;
1656        struct fc_frame_header *fh;
1657        int error;
1658
1659        skb_orig = fip->flogi_req;
1660        if (!skb_orig)
1661                return -EINVAL;
1662
1663        /*
1664         * Clone and send the FLOGI request.  If clone fails, use original.
1665         */
1666        skb = skb_clone(skb_orig, GFP_ATOMIC);
1667        if (!skb) {
1668                skb = skb_orig;
1669                fip->flogi_req = NULL;
1670        }
1671        fh = (struct fc_frame_header *)skb->data;
1672        error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1673                                 ntoh24(fh->fh_d_id));
1674        if (error) {
1675                kfree_skb(skb);
1676                return error;
1677        }
1678        fip->send(fip, skb);
1679        fip->sel_fcf->flogi_sent = 1;
1680        return 0;
1681}
1682
1683/**
1684 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1685 * @fip: The FCoE controller
1686 *
1687 * Returns non-zero error code if there's no FLOGI request to retry or
1688 * no alternate FCF available.
1689 */
1690static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1691{
1692        struct fcoe_fcf *fcf;
1693        int error;
1694
1695        mutex_lock(&fip->ctlr_mutex);
1696        spin_lock_bh(&fip->ctlr_lock);
1697        LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1698        fcf = fcoe_ctlr_select(fip);
1699        if (!fcf || fcf->flogi_sent) {
1700                kfree_skb(fip->flogi_req);
1701                fip->flogi_req = NULL;
1702                error = -ENOENT;
1703        } else {
1704                fcoe_ctlr_solicit(fip, NULL);
1705                error = fcoe_ctlr_flogi_send_locked(fip);
1706        }
1707        spin_unlock_bh(&fip->ctlr_lock);
1708        mutex_unlock(&fip->ctlr_mutex);
1709        return error;
1710}
1711
1712
1713/**
1714 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1715 * @fip: The FCoE controller that timed out
1716 *
1717 * Done here because fcoe_ctlr_els_send() can't get mutex.
1718 *
1719 * Called with ctlr_mutex held.  The caller must not hold ctlr_lock.
1720 */
1721static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1722{
1723        struct fcoe_fcf *fcf;
1724
1725        spin_lock_bh(&fip->ctlr_lock);
1726        fcf = fip->sel_fcf;
1727        if (!fcf || !fip->flogi_req_send)
1728                goto unlock;
1729
1730        LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1731
1732        /*
1733         * If this FLOGI is being sent due to a timeout retry
1734         * to the same FCF as before, select a different FCF if possible.
1735         */
1736        if (fcf->flogi_sent) {
1737                LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1738                fcf = fcoe_ctlr_select(fip);
1739                if (!fcf || fcf->flogi_sent) {
1740                        LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1741                        list_for_each_entry(fcf, &fip->fcfs, list)
1742                                fcf->flogi_sent = 0;
1743                        fcf = fcoe_ctlr_select(fip);
1744                }
1745        }
1746        if (fcf) {
1747                fcoe_ctlr_flogi_send_locked(fip);
1748                fip->flogi_req_send = 0;
1749        } else /* XXX */
1750                LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1751unlock:
1752        spin_unlock_bh(&fip->ctlr_lock);
1753}
1754
1755/**
1756 * fcoe_ctlr_timeout() - FIP timeout handler
1757 * @arg: The FCoE controller that timed out
1758 */
1759static void fcoe_ctlr_timeout(unsigned long arg)
1760{
1761        struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1762
1763        schedule_work(&fip->timer_work);
1764}
1765
1766/**
1767 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1768 * @work: Handle to a FCoE controller
1769 *
1770 * Ages FCFs.  Triggers FCF selection if possible.
1771 * Sends keep-alives and resets.
1772 */
1773static void fcoe_ctlr_timer_work(struct work_struct *work)
1774{
1775        struct fcoe_ctlr *fip;
1776        struct fc_lport *vport;
1777        u8 *mac;
1778        u8 reset = 0;
1779        u8 send_ctlr_ka = 0;
1780        u8 send_port_ka = 0;
1781        struct fcoe_fcf *sel;
1782        struct fcoe_fcf *fcf;
1783        unsigned long next_timer;
1784
1785        fip = container_of(work, struct fcoe_ctlr, timer_work);
1786        if (fip->mode == FIP_MODE_VN2VN)
1787                return fcoe_ctlr_vn_timeout(fip);
1788        mutex_lock(&fip->ctlr_mutex);
1789        if (fip->state == FIP_ST_DISABLED) {
1790                mutex_unlock(&fip->ctlr_mutex);
1791                return;
1792        }
1793
1794        fcf = fip->sel_fcf;
1795        next_timer = fcoe_ctlr_age_fcfs(fip);
1796
1797        sel = fip->sel_fcf;
1798        if (!sel && fip->sel_time) {
1799                if (time_after_eq(jiffies, fip->sel_time)) {
1800                        sel = fcoe_ctlr_select(fip);
1801                        fip->sel_time = 0;
1802                } else if (time_after(next_timer, fip->sel_time))
1803                        next_timer = fip->sel_time;
1804        }
1805
1806        if (sel && fip->flogi_req_send)
1807                fcoe_ctlr_flogi_send(fip);
1808        else if (!sel && fcf)
1809                reset = 1;
1810
1811        if (sel && !sel->fd_flags) {
1812                if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1813                        fip->ctlr_ka_time = jiffies + sel->fka_period;
1814                        send_ctlr_ka = 1;
1815                }
1816                if (time_after(next_timer, fip->ctlr_ka_time))
1817                        next_timer = fip->ctlr_ka_time;
1818
1819                if (time_after_eq(jiffies, fip->port_ka_time)) {
1820                        fip->port_ka_time = jiffies +
1821                                msecs_to_jiffies(FIP_VN_KA_PERIOD);
1822                        send_port_ka = 1;
1823                }
1824                if (time_after(next_timer, fip->port_ka_time))
1825                        next_timer = fip->port_ka_time;
1826        }
1827        if (!list_empty(&fip->fcfs))
1828                mod_timer(&fip->timer, next_timer);
1829        mutex_unlock(&fip->ctlr_mutex);
1830
1831        if (reset) {
1832                fc_lport_reset(fip->lp);
1833                /* restart things with a solicitation */
1834                fcoe_ctlr_solicit(fip, NULL);
1835        }
1836
1837        if (send_ctlr_ka)
1838                fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1839
1840        if (send_port_ka) {
1841                mutex_lock(&fip->lp->lp_mutex);
1842                mac = fip->get_src_addr(fip->lp);
1843                fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1844                list_for_each_entry(vport, &fip->lp->vports, list) {
1845                        mac = fip->get_src_addr(vport);
1846                        fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1847                }
1848                mutex_unlock(&fip->lp->lp_mutex);
1849        }
1850}
1851
1852/**
1853 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1854 * @recv_work: Handle to a FCoE controller
1855 */
1856static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1857{
1858        struct fcoe_ctlr *fip;
1859        struct sk_buff *skb;
1860
1861        fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1862        while ((skb = skb_dequeue(&fip->fip_recv_list)))
1863                fcoe_ctlr_recv_handler(fip, skb);
1864}
1865
1866/**
1867 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1868 * @fip: The FCoE controller
1869 * @fp:  The FC frame to snoop
1870 *
1871 * Snoop potential response to FLOGI or even incoming FLOGI.
1872 *
1873 * The caller has checked that we are waiting for login as indicated
1874 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1875 *
1876 * The caller is responsible for freeing the frame.
1877 * Fill in the granted_mac address.
1878 *
1879 * Return non-zero if the frame should not be delivered to libfc.
1880 */
1881int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1882                         struct fc_frame *fp)
1883{
1884        struct fc_frame_header *fh;
1885        u8 op;
1886        u8 *sa;
1887
1888        sa = eth_hdr(&fp->skb)->h_source;
1889        fh = fc_frame_header_get(fp);
1890        if (fh->fh_type != FC_TYPE_ELS)
1891                return 0;
1892
1893        op = fc_frame_payload_op(fp);
1894        if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1895            fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1896
1897                mutex_lock(&fip->ctlr_mutex);
1898                if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1899                        mutex_unlock(&fip->ctlr_mutex);
1900                        return -EINVAL;
1901                }
1902                fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1903                LIBFCOE_FIP_DBG(fip,
1904                                "received FLOGI LS_ACC using non-FIP mode\n");
1905
1906                /*
1907                 * FLOGI accepted.
1908                 * If the src mac addr is FC_OUI-based, then we mark the
1909                 * address_mode flag to use FC_OUI-based Ethernet DA.
1910                 * Otherwise we use the FCoE gateway addr
1911                 */
1912                if (ether_addr_equal(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1913                        fcoe_ctlr_map_dest(fip);
1914                } else {
1915                        memcpy(fip->dest_addr, sa, ETH_ALEN);
1916                        fip->map_dest = 0;
1917                }
1918                fip->flogi_oxid = FC_XID_UNKNOWN;
1919                mutex_unlock(&fip->ctlr_mutex);
1920                fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1921        } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1922                /*
1923                 * Save source MAC for point-to-point responses.
1924                 */
1925                mutex_lock(&fip->ctlr_mutex);
1926                if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1927                        memcpy(fip->dest_addr, sa, ETH_ALEN);
1928                        fip->map_dest = 0;
1929                        if (fip->state == FIP_ST_AUTO)
1930                                LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1931                                                "Setting non-FIP mode\n");
1932                        fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1933                }
1934                mutex_unlock(&fip->ctlr_mutex);
1935        }
1936        return 0;
1937}
1938EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1939
1940/**
1941 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1942 * @mac:    The MAC address to convert
1943 * @scheme: The scheme to use when converting
1944 * @port:   The port indicator for converting
1945 *
1946 * Returns: u64 fc world wide name
1947 */
1948u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1949                      unsigned int scheme, unsigned int port)
1950{
1951        u64 wwn;
1952        u64 host_mac;
1953
1954        /* The MAC is in NO, so flip only the low 48 bits */
1955        host_mac = ((u64) mac[0] << 40) |
1956                ((u64) mac[1] << 32) |
1957                ((u64) mac[2] << 24) |
1958                ((u64) mac[3] << 16) |
1959                ((u64) mac[4] << 8) |
1960                (u64) mac[5];
1961
1962        WARN_ON(host_mac >= (1ULL << 48));
1963        wwn = host_mac | ((u64) scheme << 60);
1964        switch (scheme) {
1965        case 1:
1966                WARN_ON(port != 0);
1967                break;
1968        case 2:
1969                WARN_ON(port >= 0xfff);
1970                wwn |= (u64) port << 48;
1971                break;
1972        default:
1973                WARN_ON(1);
1974                break;
1975        }
1976
1977        return wwn;
1978}
1979EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1980
1981/**
1982 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1983 * @rdata: libfc remote port
1984 */
1985static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1986{
1987        return (struct fcoe_rport *)(rdata + 1);
1988}
1989
1990/**
1991 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1992 * @fip: The FCoE controller
1993 * @sub: sub-opcode for probe request, reply, or advertisement.
1994 * @dest: The destination Ethernet MAC address
1995 * @min_len: minimum size of the Ethernet payload to be sent
1996 */
1997static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1998                              enum fip_vn2vn_subcode sub,
1999                              const u8 *dest, size_t min_len)
2000{
2001        struct sk_buff *skb;
2002        struct fip_vn2vn_probe_frame {
2003                struct ethhdr eth;
2004                struct fip_header fip;
2005                struct fip_mac_desc mac;
2006                struct fip_wwn_desc wwnn;
2007                struct fip_vn_desc vn;
2008        } __packed * frame;
2009        struct fip_fc4_feat *ff;
2010        struct fip_size_desc *size;
2011        u32 fcp_feat;
2012        size_t len;
2013        size_t dlen;
2014
2015        len = sizeof(*frame);
2016        dlen = 0;
2017        if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2018                dlen = sizeof(struct fip_fc4_feat) +
2019                       sizeof(struct fip_size_desc);
2020                len += dlen;
2021        }
2022        dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
2023        len = max(len, min_len + sizeof(struct ethhdr));
2024
2025        skb = dev_alloc_skb(len);
2026        if (!skb)
2027                return;
2028
2029        frame = (struct fip_vn2vn_probe_frame *)skb->data;
2030        memset(frame, 0, len);
2031        memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2032
2033        if (sub == FIP_SC_VN_BEACON) {
2034                hton24(frame->eth.h_source, FIP_VN_FC_MAP);
2035                hton24(frame->eth.h_source + 3, fip->port_id);
2036        } else {
2037                memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2038        }
2039        frame->eth.h_proto = htons(ETH_P_FIP);
2040
2041        frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2042        frame->fip.fip_op = htons(FIP_OP_VN2VN);
2043        frame->fip.fip_subcode = sub;
2044        frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2045
2046        frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2047        frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2048        memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2049
2050        frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2051        frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2052        put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2053
2054        frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2055        frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2056        hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2057        hton24(frame->vn.fd_mac + 3, fip->port_id);
2058        hton24(frame->vn.fd_fc_id, fip->port_id);
2059        put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2060
2061        /*
2062         * For claims, add FC-4 features.
2063         * TBD: Add interface to get fc-4 types and features from libfc.
2064         */
2065        if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2066                ff = (struct fip_fc4_feat *)(frame + 1);
2067                ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2068                ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2069                ff->fd_fts = fip->lp->fcts;
2070
2071                fcp_feat = 0;
2072                if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2073                        fcp_feat |= FCP_FEAT_INIT;
2074                if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2075                        fcp_feat |= FCP_FEAT_TARG;
2076                fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2077                ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2078
2079                size = (struct fip_size_desc *)(ff + 1);
2080                size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2081                size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2082                size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2083        }
2084
2085        skb_put(skb, len);
2086        skb->protocol = htons(ETH_P_FIP);
2087        skb->priority = fip->priority;
2088        skb_reset_mac_header(skb);
2089        skb_reset_network_header(skb);
2090
2091        fip->send(fip, skb);
2092}
2093
2094/**
2095 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2096 * @lport: The lport which is receiving the event
2097 * @rdata: remote port private data
2098 * @event: The event that occurred
2099 *
2100 * Locking Note:  The rport lock must not be held when calling this function.
2101 */
2102static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2103                                        struct fc_rport_priv *rdata,
2104                                        enum fc_rport_event event)
2105{
2106        struct fcoe_ctlr *fip = lport->disc.priv;
2107        struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2108
2109        LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2110                        rdata->ids.port_id, event);
2111
2112        mutex_lock(&fip->ctlr_mutex);
2113        switch (event) {
2114        case RPORT_EV_READY:
2115                frport->login_count = 0;
2116                break;
2117        case RPORT_EV_LOGO:
2118        case RPORT_EV_FAILED:
2119        case RPORT_EV_STOP:
2120                frport->login_count++;
2121                if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2122                        LIBFCOE_FIP_DBG(fip,
2123                                        "rport FLOGI limited port_id %6.6x\n",
2124                                        rdata->ids.port_id);
2125                        lport->tt.rport_logoff(rdata);
2126                }
2127                break;
2128        default:
2129                break;
2130        }
2131        mutex_unlock(&fip->ctlr_mutex);
2132}
2133
2134static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2135        .event_callback = fcoe_ctlr_vn_rport_callback,
2136};
2137
2138/**
2139 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2140 * @fip: The FCoE controller
2141 *
2142 * Called with ctlr_mutex held.
2143 */
2144static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2145{
2146        struct fc_rport_priv *rdata;
2147
2148        mutex_lock(&lport->disc.disc_mutex);
2149        list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
2150                lport->tt.rport_logoff(rdata);
2151        lport->disc.disc_callback = NULL;
2152        mutex_unlock(&lport->disc.disc_mutex);
2153}
2154
2155/**
2156 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2157 * @fip: The FCoE controller
2158 *
2159 * Called through the local port template for discovery.
2160 * Called without the ctlr_mutex held.
2161 */
2162static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2163{
2164        struct fcoe_ctlr *fip = lport->disc.priv;
2165
2166        mutex_lock(&fip->ctlr_mutex);
2167        fcoe_ctlr_disc_stop_locked(lport);
2168        mutex_unlock(&fip->ctlr_mutex);
2169}
2170
2171/**
2172 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2173 * @fip: The FCoE controller
2174 *
2175 * Called through the local port template for discovery.
2176 * Called without the ctlr_mutex held.
2177 */
2178static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2179{
2180        fcoe_ctlr_disc_stop(lport);
2181        lport->tt.rport_flush_queue();
2182        synchronize_rcu();
2183}
2184
2185/**
2186 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2187 * @fip: The FCoE controller
2188 *
2189 * Called with fcoe_ctlr lock held.
2190 */
2191static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2192{
2193        unsigned long wait;
2194        u32 port_id;
2195
2196        fcoe_ctlr_disc_stop_locked(fip->lp);
2197
2198        /*
2199         * Get proposed port ID.
2200         * If this is the first try after link up, use any previous port_id.
2201         * If there was none, use the low bits of the port_name.
2202         * On subsequent tries, get the next random one.
2203         * Don't use reserved IDs, use another non-zero value, just as random.
2204         */
2205        port_id = fip->port_id;
2206        if (fip->probe_tries)
2207                port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
2208        else if (!port_id)
2209                port_id = fip->lp->wwpn & 0xffff;
2210        if (!port_id || port_id == 0xffff)
2211                port_id = 1;
2212        fip->port_id = port_id;
2213
2214        if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2215                fip->probe_tries++;
2216                wait = prandom_u32() % FIP_VN_PROBE_WAIT;
2217        } else
2218                wait = FIP_VN_RLIM_INT;
2219        mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2220        fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2221}
2222
2223/**
2224 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2225 * @fip: The FCoE controller
2226 *
2227 * Called with fcoe_ctlr lock held.
2228 */
2229static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2230{
2231        fip->probe_tries = 0;
2232        prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
2233        fcoe_ctlr_vn_restart(fip);
2234}
2235
2236/**
2237 * fcoe_ctlr_vn_parse - parse probe request or response
2238 * @fip: The FCoE controller
2239 * @skb: incoming packet
2240 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2241 *
2242 * Returns non-zero error number on error.
2243 * Does not consume the packet.
2244 */
2245static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2246                              struct sk_buff *skb,
2247                              struct fc_rport_priv *rdata)
2248{
2249        struct fip_header *fiph;
2250        struct fip_desc *desc = NULL;
2251        struct fip_mac_desc *macd = NULL;
2252        struct fip_wwn_desc *wwn = NULL;
2253        struct fip_vn_desc *vn = NULL;
2254        struct fip_size_desc *size = NULL;
2255        struct fcoe_rport *frport;
2256        size_t rlen;
2257        size_t dlen;
2258        u32 desc_mask = 0;
2259        u32 dtype;
2260        u8 sub;
2261
2262        memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2263        frport = fcoe_ctlr_rport(rdata);
2264
2265        fiph = (struct fip_header *)skb->data;
2266        frport->flags = ntohs(fiph->fip_flags);
2267
2268        sub = fiph->fip_subcode;
2269        switch (sub) {
2270        case FIP_SC_VN_PROBE_REQ:
2271        case FIP_SC_VN_PROBE_REP:
2272        case FIP_SC_VN_BEACON:
2273                desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2274                            BIT(FIP_DT_VN_ID);
2275                break;
2276        case FIP_SC_VN_CLAIM_NOTIFY:
2277        case FIP_SC_VN_CLAIM_REP:
2278                desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2279                            BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2280                            BIT(FIP_DT_FCOE_SIZE);
2281                break;
2282        default:
2283                LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2284                return -EINVAL;
2285        }
2286
2287        rlen = ntohs(fiph->fip_dl_len) * 4;
2288        if (rlen + sizeof(*fiph) > skb->len)
2289                return -EINVAL;
2290
2291        desc = (struct fip_desc *)(fiph + 1);
2292        while (rlen > 0) {
2293                dlen = desc->fip_dlen * FIP_BPW;
2294                if (dlen < sizeof(*desc) || dlen > rlen)
2295                        return -EINVAL;
2296
2297                dtype = desc->fip_dtype;
2298                if (dtype < 32) {
2299                        if (!(desc_mask & BIT(dtype))) {
2300                                LIBFCOE_FIP_DBG(fip,
2301                                                "unexpected or duplicated desc "
2302                                                "desc type %u in "
2303                                                "FIP VN2VN subtype %u\n",
2304                                                dtype, sub);
2305                                return -EINVAL;
2306                        }
2307                        desc_mask &= ~BIT(dtype);
2308                }
2309
2310                switch (dtype) {
2311                case FIP_DT_MAC:
2312                        if (dlen != sizeof(struct fip_mac_desc))
2313                                goto len_err;
2314                        macd = (struct fip_mac_desc *)desc;
2315                        if (!is_valid_ether_addr(macd->fd_mac)) {
2316                                LIBFCOE_FIP_DBG(fip,
2317                                        "Invalid MAC addr %pM in FIP VN2VN\n",
2318                                         macd->fd_mac);
2319                                return -EINVAL;
2320                        }
2321                        memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2322                        break;
2323                case FIP_DT_NAME:
2324                        if (dlen != sizeof(struct fip_wwn_desc))
2325                                goto len_err;
2326                        wwn = (struct fip_wwn_desc *)desc;
2327                        rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2328                        break;
2329                case FIP_DT_VN_ID:
2330                        if (dlen != sizeof(struct fip_vn_desc))
2331                                goto len_err;
2332                        vn = (struct fip_vn_desc *)desc;
2333                        memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2334                        rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2335                        rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2336                        break;
2337                case FIP_DT_FC4F:
2338                        if (dlen != sizeof(struct fip_fc4_feat))
2339                                goto len_err;
2340                        break;
2341                case FIP_DT_FCOE_SIZE:
2342                        if (dlen != sizeof(struct fip_size_desc))
2343                                goto len_err;
2344                        size = (struct fip_size_desc *)desc;
2345                        frport->fcoe_len = ntohs(size->fd_size);
2346                        break;
2347                default:
2348                        LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2349                                        "in FIP probe\n", dtype);
2350                        /* standard says ignore unknown descriptors >= 128 */
2351                        if (dtype < FIP_DT_NON_CRITICAL)
2352                                return -EINVAL;
2353                        break;
2354                }
2355                desc = (struct fip_desc *)((char *)desc + dlen);
2356                rlen -= dlen;
2357        }
2358        return 0;
2359
2360len_err:
2361        LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2362                        dtype, dlen);
2363        return -EINVAL;
2364}
2365
2366/**
2367 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2368 * @fip: The FCoE controller
2369 *
2370 * Called with ctlr_mutex held.
2371 */
2372static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2373{
2374        fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2375        fip->sol_time = jiffies;
2376}
2377
2378/**
2379 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2380 * @fip: The FCoE controller
2381 * @rdata: parsed remote port with frport from the probe request
2382 *
2383 * Called with ctlr_mutex held.
2384 */
2385static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2386                                   struct fc_rport_priv *rdata)
2387{
2388        struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2389
2390        if (rdata->ids.port_id != fip->port_id)
2391                return;
2392
2393        switch (fip->state) {
2394        case FIP_ST_VNMP_CLAIM:
2395        case FIP_ST_VNMP_UP:
2396                fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2397                                  frport->enode_mac, 0);
2398                break;
2399        case FIP_ST_VNMP_PROBE1:
2400        case FIP_ST_VNMP_PROBE2:
2401                /*
2402                 * Decide whether to reply to the Probe.
2403                 * Our selected address is never a "recorded" one, so
2404                 * only reply if our WWPN is greater and the
2405                 * Probe's REC bit is not set.
2406                 * If we don't reply, we will change our address.
2407                 */
2408                if (fip->lp->wwpn > rdata->ids.port_name &&
2409                    !(frport->flags & FIP_FL_REC_OR_P2P)) {
2410                        fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2411                                          frport->enode_mac, 0);
2412                        break;
2413                }
2414                /* fall through */
2415        case FIP_ST_VNMP_START:
2416                fcoe_ctlr_vn_restart(fip);
2417                break;
2418        default:
2419                break;
2420        }
2421}
2422
2423/**
2424 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2425 * @fip: The FCoE controller
2426 * @rdata: parsed remote port with frport from the probe request
2427 *
2428 * Called with ctlr_mutex held.
2429 */
2430static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2431                                   struct fc_rport_priv *rdata)
2432{
2433        if (rdata->ids.port_id != fip->port_id)
2434                return;
2435        switch (fip->state) {
2436        case FIP_ST_VNMP_START:
2437        case FIP_ST_VNMP_PROBE1:
2438        case FIP_ST_VNMP_PROBE2:
2439        case FIP_ST_VNMP_CLAIM:
2440                fcoe_ctlr_vn_restart(fip);
2441                break;
2442        case FIP_ST_VNMP_UP:
2443                fcoe_ctlr_vn_send_claim(fip);
2444                break;
2445        default:
2446                break;
2447        }
2448}
2449
2450/**
2451 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2452 * @fip: The FCoE controller
2453 * @new: newly-parsed remote port with frport as a template for new rdata
2454 *
2455 * Called with ctlr_mutex held.
2456 */
2457static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2458{
2459        struct fc_lport *lport = fip->lp;
2460        struct fc_rport_priv *rdata;
2461        struct fc_rport_identifiers *ids;
2462        struct fcoe_rport *frport;
2463        u32 port_id;
2464
2465        port_id = new->ids.port_id;
2466        if (port_id == fip->port_id)
2467                return;
2468
2469        mutex_lock(&lport->disc.disc_mutex);
2470        rdata = lport->tt.rport_create(lport, port_id);
2471        if (!rdata) {
2472                mutex_unlock(&lport->disc.disc_mutex);
2473                return;
2474        }
2475
2476        rdata->ops = &fcoe_ctlr_vn_rport_ops;
2477        rdata->disc_id = lport->disc.disc_id;
2478
2479        ids = &rdata->ids;
2480        if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2481            (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2482                lport->tt.rport_logoff(rdata);
2483        ids->port_name = new->ids.port_name;
2484        ids->node_name = new->ids.node_name;
2485        mutex_unlock(&lport->disc.disc_mutex);
2486
2487        frport = fcoe_ctlr_rport(rdata);
2488        LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2489                        port_id, frport->fcoe_len ? "old" : "new");
2490        *frport = *fcoe_ctlr_rport(new);
2491        frport->time = 0;
2492}
2493
2494/**
2495 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2496 * @fip: The FCoE controller
2497 * @port_id:  The port_id of the remote VN_node
2498 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2499 *
2500 * Returns non-zero error if no remote port found.
2501 */
2502static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2503{
2504        struct fc_lport *lport = fip->lp;
2505        struct fc_rport_priv *rdata;
2506        struct fcoe_rport *frport;
2507        int ret = -1;
2508
2509        rdata = lport->tt.rport_lookup(lport, port_id);
2510        if (rdata) {
2511                frport = fcoe_ctlr_rport(rdata);
2512                memcpy(mac, frport->enode_mac, ETH_ALEN);
2513                ret = 0;
2514                kref_put(&rdata->kref, lport->tt.rport_destroy);
2515        }
2516        return ret;
2517}
2518
2519/**
2520 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2521 * @fip: The FCoE controller
2522 * @new: newly-parsed remote port with frport as a template for new rdata
2523 *
2524 * Called with ctlr_mutex held.
2525 */
2526static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2527                                      struct fc_rport_priv *new)
2528{
2529        struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2530
2531        if (frport->flags & FIP_FL_REC_OR_P2P) {
2532                fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2533                return;
2534        }
2535        switch (fip->state) {
2536        case FIP_ST_VNMP_START:
2537        case FIP_ST_VNMP_PROBE1:
2538        case FIP_ST_VNMP_PROBE2:
2539                if (new->ids.port_id == fip->port_id)
2540                        fcoe_ctlr_vn_restart(fip);
2541                break;
2542        case FIP_ST_VNMP_CLAIM:
2543        case FIP_ST_VNMP_UP:
2544                if (new->ids.port_id == fip->port_id) {
2545                        if (new->ids.port_name > fip->lp->wwpn) {
2546                                fcoe_ctlr_vn_restart(fip);
2547                                break;
2548                        }
2549                        fcoe_ctlr_vn_send_claim(fip);
2550                        break;
2551                }
2552                fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2553                                  min((u32)frport->fcoe_len,
2554                                      fcoe_ctlr_fcoe_size(fip)));
2555                fcoe_ctlr_vn_add(fip, new);
2556                break;
2557        default:
2558                break;
2559        }
2560}
2561
2562/**
2563 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2564 * @fip: The FCoE controller that received the frame
2565 * @new: newly-parsed remote port with frport from the Claim Response
2566 *
2567 * Called with ctlr_mutex held.
2568 */
2569static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2570                                    struct fc_rport_priv *new)
2571{
2572        LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2573                        new->ids.port_id, fcoe_ctlr_state(fip->state));
2574        if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2575                fcoe_ctlr_vn_add(fip, new);
2576}
2577
2578/**
2579 * fcoe_ctlr_vn_beacon() - handle received beacon.
2580 * @fip: The FCoE controller that received the frame
2581 * @new: newly-parsed remote port with frport from the Beacon
2582 *
2583 * Called with ctlr_mutex held.
2584 */
2585static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2586                                struct fc_rport_priv *new)
2587{
2588        struct fc_lport *lport = fip->lp;
2589        struct fc_rport_priv *rdata;
2590        struct fcoe_rport *frport;
2591
2592        frport = fcoe_ctlr_rport(new);
2593        if (frport->flags & FIP_FL_REC_OR_P2P) {
2594                fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2595                return;
2596        }
2597        rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2598        if (rdata) {
2599                if (rdata->ids.node_name == new->ids.node_name &&
2600                    rdata->ids.port_name == new->ids.port_name) {
2601                        frport = fcoe_ctlr_rport(rdata);
2602                        if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2603                                lport->tt.rport_login(rdata);
2604                        frport->time = jiffies;
2605                }
2606                kref_put(&rdata->kref, lport->tt.rport_destroy);
2607                return;
2608        }
2609        if (fip->state != FIP_ST_VNMP_UP)
2610                return;
2611
2612        /*
2613         * Beacon from a new neighbor.
2614         * Send a claim notify if one hasn't been sent recently.
2615         * Don't add the neighbor yet.
2616         */
2617        LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2618                        new->ids.port_id);
2619        if (time_after(jiffies,
2620                       fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2621                fcoe_ctlr_vn_send_claim(fip);
2622}
2623
2624/**
2625 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2626 * @fip: The FCoE controller
2627 *
2628 * Called with ctlr_mutex held.
2629 * Called only in state FIP_ST_VNMP_UP.
2630 * Returns the soonest time for next age-out or a time far in the future.
2631 */
2632static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2633{
2634        struct fc_lport *lport = fip->lp;
2635        struct fc_rport_priv *rdata;
2636        struct fcoe_rport *frport;
2637        unsigned long next_time;
2638        unsigned long deadline;
2639
2640        next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2641        mutex_lock(&lport->disc.disc_mutex);
2642        list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2643                frport = fcoe_ctlr_rport(rdata);
2644                if (!frport->time)
2645                        continue;
2646                deadline = frport->time +
2647                           msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2648                if (time_after_eq(jiffies, deadline)) {
2649                        frport->time = 0;
2650                        LIBFCOE_FIP_DBG(fip,
2651                                "port %16.16llx fc_id %6.6x beacon expired\n",
2652                                rdata->ids.port_name, rdata->ids.port_id);
2653                        lport->tt.rport_logoff(rdata);
2654                } else if (time_before(deadline, next_time))
2655                        next_time = deadline;
2656        }
2657        mutex_unlock(&lport->disc.disc_mutex);
2658        return next_time;
2659}
2660
2661/**
2662 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2663 * @fip: The FCoE controller that received the frame
2664 * @skb: The received FIP frame
2665 *
2666 * Returns non-zero if the frame is dropped.
2667 * Always consumes the frame.
2668 */
2669static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2670{
2671        struct fip_header *fiph;
2672        enum fip_vn2vn_subcode sub;
2673        struct {
2674                struct fc_rport_priv rdata;
2675                struct fcoe_rport frport;
2676        } buf;
2677        int rc;
2678
2679        fiph = (struct fip_header *)skb->data;
2680        sub = fiph->fip_subcode;
2681
2682        rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2683        if (rc) {
2684                LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2685                goto drop;
2686        }
2687
2688        mutex_lock(&fip->ctlr_mutex);
2689        switch (sub) {
2690        case FIP_SC_VN_PROBE_REQ:
2691                fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2692                break;
2693        case FIP_SC_VN_PROBE_REP:
2694                fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2695                break;
2696        case FIP_SC_VN_CLAIM_NOTIFY:
2697                fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2698                break;
2699        case FIP_SC_VN_CLAIM_REP:
2700                fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2701                break;
2702        case FIP_SC_VN_BEACON:
2703                fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2704                break;
2705        default:
2706                LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2707                rc = -1;
2708                break;
2709        }
2710        mutex_unlock(&fip->ctlr_mutex);
2711drop:
2712        kfree_skb(skb);
2713        return rc;
2714}
2715
2716/**
2717 * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2718 * @fip: The FCoE controller
2719 * @skb: incoming packet
2720 * @rdata: buffer for resulting parsed VLAN entry plus fcoe_rport
2721 *
2722 * Returns non-zero error number on error.
2723 * Does not consume the packet.
2724 */
2725static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2726                              struct sk_buff *skb,
2727                              struct fc_rport_priv *rdata)
2728{
2729        struct fip_header *fiph;
2730        struct fip_desc *desc = NULL;
2731        struct fip_mac_desc *macd = NULL;
2732        struct fip_wwn_desc *wwn = NULL;
2733        struct fcoe_rport *frport;
2734        size_t rlen;
2735        size_t dlen;
2736        u32 desc_mask = 0;
2737        u32 dtype;
2738        u8 sub;
2739
2740        memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2741        frport = fcoe_ctlr_rport(rdata);
2742
2743        fiph = (struct fip_header *)skb->data;
2744        frport->flags = ntohs(fiph->fip_flags);
2745
2746        sub = fiph->fip_subcode;
2747        switch (sub) {
2748        case FIP_SC_VL_REQ:
2749                desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2750                break;
2751        default:
2752                LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2753                return -EINVAL;
2754        }
2755
2756        rlen = ntohs(fiph->fip_dl_len) * 4;
2757        if (rlen + sizeof(*fiph) > skb->len)
2758                return -EINVAL;
2759
2760        desc = (struct fip_desc *)(fiph + 1);
2761        while (rlen > 0) {
2762                dlen = desc->fip_dlen * FIP_BPW;
2763                if (dlen < sizeof(*desc) || dlen > rlen)
2764                        return -EINVAL;
2765
2766                dtype = desc->fip_dtype;
2767                if (dtype < 32) {
2768                        if (!(desc_mask & BIT(dtype))) {
2769                                LIBFCOE_FIP_DBG(fip,
2770                                                "unexpected or duplicated desc "
2771                                                "desc type %u in "
2772                                                "FIP VN2VN subtype %u\n",
2773                                                dtype, sub);
2774                                return -EINVAL;
2775                        }
2776                        desc_mask &= ~BIT(dtype);
2777                }
2778
2779                switch (dtype) {
2780                case FIP_DT_MAC:
2781                        if (dlen != sizeof(struct fip_mac_desc))
2782                                goto len_err;
2783                        macd = (struct fip_mac_desc *)desc;
2784                        if (!is_valid_ether_addr(macd->fd_mac)) {
2785                                LIBFCOE_FIP_DBG(fip,
2786                                        "Invalid MAC addr %pM in FIP VN2VN\n",
2787                                         macd->fd_mac);
2788                                return -EINVAL;
2789                        }
2790                        memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2791                        break;
2792                case FIP_DT_NAME:
2793                        if (dlen != sizeof(struct fip_wwn_desc))
2794                                goto len_err;
2795                        wwn = (struct fip_wwn_desc *)desc;
2796                        rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2797                        break;
2798                default:
2799                        LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2800                                        "in FIP probe\n", dtype);
2801                        /* standard says ignore unknown descriptors >= 128 */
2802                        if (dtype < FIP_DT_NON_CRITICAL)
2803                                return -EINVAL;
2804                        break;
2805                }
2806                desc = (struct fip_desc *)((char *)desc + dlen);
2807                rlen -= dlen;
2808        }
2809        return 0;
2810
2811len_err:
2812        LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2813                        dtype, dlen);
2814        return -EINVAL;
2815}
2816
2817/**
2818 * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2819 * @fip: The FCoE controller
2820 * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2821 * @dest: The destination Ethernet MAC address
2822 * @min_len: minimum size of the Ethernet payload to be sent
2823 */
2824static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2825                              enum fip_vlan_subcode sub,
2826                              const u8 *dest)
2827{
2828        struct sk_buff *skb;
2829        struct fip_vlan_notify_frame {
2830                struct ethhdr eth;
2831                struct fip_header fip;
2832                struct fip_mac_desc mac;
2833                struct fip_vlan_desc vlan;
2834        } __packed * frame;
2835        size_t len;
2836        size_t dlen;
2837
2838        len = sizeof(*frame);
2839        dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2840        len = max(len, sizeof(struct ethhdr));
2841
2842        skb = dev_alloc_skb(len);
2843        if (!skb)
2844                return;
2845
2846        LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2847                        fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2848                        fip->lp->vlan);
2849
2850        frame = (struct fip_vlan_notify_frame *)skb->data;
2851        memset(frame, 0, len);
2852        memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2853
2854        memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2855        frame->eth.h_proto = htons(ETH_P_FIP);
2856
2857        frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2858        frame->fip.fip_op = htons(FIP_OP_VLAN);
2859        frame->fip.fip_subcode = sub;
2860        frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2861
2862        frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2863        frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2864        memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2865
2866        frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2867        frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2868        put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2869
2870        skb_put(skb, len);
2871        skb->protocol = htons(ETH_P_FIP);
2872        skb->priority = fip->priority;
2873        skb_reset_mac_header(skb);
2874        skb_reset_network_header(skb);
2875
2876        fip->send(fip, skb);
2877}
2878
2879/**
2880 * fcoe_ctlr_vlan_disk_reply() - send FIP VLAN Discovery Notification.
2881 * @fip: The FCoE controller
2882 *
2883 * Called with ctlr_mutex held.
2884 */
2885static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2886                                      struct fc_rport_priv *rdata)
2887{
2888        struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2889        enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2890
2891        if (fip->mode == FIP_MODE_VN2VN)
2892                sub = FIP_SC_VL_VN2VN_NOTE;
2893
2894        fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2895}
2896
2897/**
2898 * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2899 * @lport: The local port
2900 * @fp: The received frame
2901 *
2902 */
2903static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2904{
2905        struct fip_header *fiph;
2906        enum fip_vlan_subcode sub;
2907        struct {
2908                struct fc_rport_priv rdata;
2909                struct fcoe_rport frport;
2910        } buf;
2911        int rc;
2912
2913        fiph = (struct fip_header *)skb->data;
2914        sub = fiph->fip_subcode;
2915        rc = fcoe_ctlr_vlan_parse(fip, skb, &buf.rdata);
2916        if (rc) {
2917                LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
2918                goto drop;
2919        }
2920        mutex_lock(&fip->ctlr_mutex);
2921        if (sub == FIP_SC_VL_REQ)
2922                fcoe_ctlr_vlan_disc_reply(fip, &buf.rdata);
2923        mutex_unlock(&fip->ctlr_mutex);
2924
2925drop:
2926        kfree_skb(skb);
2927        return rc;
2928}
2929
2930/**
2931 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2932 * @lport: The local port
2933 * @fp: The received frame
2934 *
2935 * This should never be called since we don't see RSCNs or other
2936 * fabric-generated ELSes.
2937 */
2938static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
2939{
2940        struct fc_seq_els_data rjt_data;
2941
2942        rjt_data.reason = ELS_RJT_UNSUP;
2943        rjt_data.explan = ELS_EXPL_NONE;
2944        lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
2945        fc_frame_free(fp);
2946}
2947
2948/**
2949 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2950 * @fip: The FCoE controller
2951 *
2952 * This sets a flag indicating that remote ports should be created
2953 * and started for the peers we discover.  We use the disc_callback
2954 * pointer as that flag.  Peers already discovered are created here.
2955 *
2956 * The lport lock is held during this call. The callback must be done
2957 * later, without holding either the lport or discovery locks.
2958 * The fcoe_ctlr lock may also be held during this call.
2959 */
2960static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2961                                                  enum fc_disc_event),
2962                                 struct fc_lport *lport)
2963{
2964        struct fc_disc *disc = &lport->disc;
2965        struct fcoe_ctlr *fip = disc->priv;
2966
2967        mutex_lock(&disc->disc_mutex);
2968        disc->disc_callback = callback;
2969        disc->disc_id = (disc->disc_id + 2) | 1;
2970        disc->pending = 1;
2971        schedule_work(&fip->timer_work);
2972        mutex_unlock(&disc->disc_mutex);
2973}
2974
2975/**
2976 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2977 * @fip: The FCoE controller
2978 *
2979 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2980 * we've received at least one beacon.
2981 * Performs the discovery complete callback.
2982 */
2983static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2984{
2985        struct fc_lport *lport = fip->lp;
2986        struct fc_disc *disc = &lport->disc;
2987        struct fc_rport_priv *rdata;
2988        struct fcoe_rport *frport;
2989        void (*callback)(struct fc_lport *, enum fc_disc_event);
2990
2991        mutex_lock(&disc->disc_mutex);
2992        callback = disc->pending ? disc->disc_callback : NULL;
2993        disc->pending = 0;
2994        list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2995                frport = fcoe_ctlr_rport(rdata);
2996                if (frport->time)
2997                        lport->tt.rport_login(rdata);
2998        }
2999        mutex_unlock(&disc->disc_mutex);
3000        if (callback)
3001                callback(lport, DISC_EV_SUCCESS);
3002}
3003
3004/**
3005 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3006 * @fip: The FCoE controller
3007 */
3008static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3009{
3010        unsigned long next_time;
3011        u8 mac[ETH_ALEN];
3012        u32 new_port_id = 0;
3013
3014        mutex_lock(&fip->ctlr_mutex);
3015        switch (fip->state) {
3016        case FIP_ST_VNMP_START:
3017                fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
3018                fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3019                next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3020                break;
3021        case FIP_ST_VNMP_PROBE1:
3022                fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
3023                fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3024                next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3025                break;
3026        case FIP_ST_VNMP_PROBE2:
3027                fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3028                new_port_id = fip->port_id;
3029                hton24(mac, FIP_VN_FC_MAP);
3030                hton24(mac + 3, new_port_id);
3031                fcoe_ctlr_map_dest(fip);
3032                fip->update_mac(fip->lp, mac);
3033                fcoe_ctlr_vn_send_claim(fip);
3034                next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3035                break;
3036        case FIP_ST_VNMP_CLAIM:
3037                /*
3038                 * This may be invoked either by starting discovery so don't
3039                 * go to the next state unless it's been long enough.
3040                 */
3041                next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3042                if (time_after_eq(jiffies, next_time)) {
3043                        fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
3044                        fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3045                                          fcoe_all_vn2vn, 0);
3046                        next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3047                        fip->port_ka_time = next_time;
3048                }
3049                fcoe_ctlr_vn_disc(fip);
3050                break;
3051        case FIP_ST_VNMP_UP:
3052                next_time = fcoe_ctlr_vn_age(fip);
3053                if (time_after_eq(jiffies, fip->port_ka_time)) {
3054                        fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3055                                          fcoe_all_vn2vn, 0);
3056                        fip->port_ka_time = jiffies +
3057                                 msecs_to_jiffies(FIP_VN_BEACON_INT +
3058                                        (prandom_u32() % FIP_VN_BEACON_FUZZ));
3059                }
3060                if (time_before(fip->port_ka_time, next_time))
3061                        next_time = fip->port_ka_time;
3062                break;
3063        case FIP_ST_LINK_WAIT:
3064                goto unlock;
3065        default:
3066                WARN(1, "unexpected state %d\n", fip->state);
3067                goto unlock;
3068        }
3069        mod_timer(&fip->timer, next_time);
3070unlock:
3071        mutex_unlock(&fip->ctlr_mutex);
3072
3073        /* If port ID is new, notify local port after dropping ctlr_mutex */
3074        if (new_port_id)
3075                fc_lport_set_local_id(fip->lp, new_port_id);
3076}
3077
3078/**
3079 * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3080 * @lport: The local port to be (re)configured
3081 * @fip:   The FCoE controller whose mode is changing
3082 * @fip_mode: The new fip mode
3083 *
3084 * Note that the we shouldn't be changing the libfc discovery settings
3085 * (fc_disc_config) while an lport is going through the libfc state
3086 * machine. The mode can only be changed when a fcoe_ctlr device is
3087 * disabled, so that should ensure that this routine is only called
3088 * when nothing is happening.
3089 */
3090static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
3091                               enum fip_mode fip_mode)
3092{
3093        void *priv;
3094
3095        WARN_ON(lport->state != LPORT_ST_RESET &&
3096                lport->state != LPORT_ST_DISABLED);
3097
3098        if (fip_mode == FIP_MODE_VN2VN) {
3099                lport->rport_priv_size = sizeof(struct fcoe_rport);
3100                lport->point_to_multipoint = 1;
3101                lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3102                lport->tt.disc_start = fcoe_ctlr_disc_start;
3103                lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3104                lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3105                priv = fip;
3106        } else {
3107                lport->rport_priv_size = 0;
3108                lport->point_to_multipoint = 0;
3109                lport->tt.disc_recv_req = NULL;
3110                lport->tt.disc_start = NULL;
3111                lport->tt.disc_stop = NULL;
3112                lport->tt.disc_stop_final = NULL;
3113                priv = lport;
3114        }
3115
3116        fc_disc_config(lport, priv);
3117}
3118
3119/**
3120 * fcoe_libfc_config() - Sets up libfc related properties for local port
3121 * @lport:    The local port to configure libfc for
3122 * @fip:      The FCoE controller in use by the local port
3123 * @tt:       The libfc function template
3124 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
3125 *
3126 * Returns : 0 for success
3127 */
3128int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3129                      const struct libfc_function_template *tt, int init_fcp)
3130{
3131        /* Set the function pointers set by the LLDD */
3132        memcpy(&lport->tt, tt, sizeof(*tt));
3133        if (init_fcp && fc_fcp_init(lport))
3134                return -ENOMEM;
3135        fc_exch_init(lport);
3136        fc_elsct_init(lport);
3137        fc_lport_init(lport);
3138        fc_rport_init(lport);
3139        fc_disc_init(lport);
3140        fcoe_ctlr_mode_set(lport, fip, fip->mode);
3141        return 0;
3142}
3143EXPORT_SYMBOL_GPL(fcoe_libfc_config);
3144
3145void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3146{
3147        struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3148        struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3149        struct fcoe_fcf *fcf;
3150
3151        mutex_lock(&fip->ctlr_mutex);
3152        mutex_lock(&ctlr_dev->lock);
3153
3154        fcf = fcoe_fcf_device_priv(fcf_dev);
3155        if (fcf)
3156                fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3157        else
3158                fcf_dev->selected = 0;
3159
3160        mutex_unlock(&ctlr_dev->lock);
3161        mutex_unlock(&fip->ctlr_mutex);
3162}
3163EXPORT_SYMBOL(fcoe_fcf_get_selected);
3164
3165void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
3166{
3167        struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
3168        struct fc_lport *lport = ctlr->lp;
3169
3170        mutex_lock(&ctlr->ctlr_mutex);
3171        switch (ctlr_dev->mode) {
3172        case FIP_CONN_TYPE_VN2VN:
3173                ctlr->mode = FIP_MODE_VN2VN;
3174                break;
3175        case FIP_CONN_TYPE_FABRIC:
3176        default:
3177                ctlr->mode = FIP_MODE_FABRIC;
3178                break;
3179        }
3180
3181        mutex_unlock(&ctlr->ctlr_mutex);
3182
3183        fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
3184}
3185EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);
3186