linux/drivers/net/bonding/bond_3ad.c
<<
>>
Prefs
   1/*
   2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License as published by the Free
   6 * Software Foundation; either version 2 of the License, or (at your option)
   7 * any later version.
   8 *
   9 * This program is distributed in the hope that 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., 59
  16 * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17 *
  18 * The full GNU General Public License is included in this distribution in the
  19 * file called LICENSE.
  20 *
  21 */
  22
  23#include <linux/skbuff.h>
  24#include <linux/if_ether.h>
  25#include <linux/netdevice.h>
  26#include <linux/spinlock.h>
  27#include <linux/ethtool.h>
  28#include <linux/etherdevice.h>
  29#include <linux/if_bonding.h>
  30#include <linux/pkt_sched.h>
  31#include <net/net_namespace.h>
  32#include <net/bonding.h>
  33#include <net/bond_3ad.h>
  34
  35/* General definitions */
  36#define AD_SHORT_TIMEOUT           1
  37#define AD_LONG_TIMEOUT            0
  38#define AD_STANDBY                 0x2
  39#define AD_MAX_TX_IN_SECOND        3
  40#define AD_COLLECTOR_MAX_DELAY     0
  41
  42/* Timer definitions (43.4.4 in the 802.3ad standard) */
  43#define AD_FAST_PERIODIC_TIME      1
  44#define AD_SLOW_PERIODIC_TIME      30
  45#define AD_SHORT_TIMEOUT_TIME      (3*AD_FAST_PERIODIC_TIME)
  46#define AD_LONG_TIMEOUT_TIME       (3*AD_SLOW_PERIODIC_TIME)
  47#define AD_CHURN_DETECTION_TIME    60
  48#define AD_AGGREGATE_WAIT_TIME     2
  49
  50/* Port state definitions (43.4.2.2 in the 802.3ad standard) */
  51#define AD_STATE_LACP_ACTIVITY   0x1
  52#define AD_STATE_LACP_TIMEOUT    0x2
  53#define AD_STATE_AGGREGATION     0x4
  54#define AD_STATE_SYNCHRONIZATION 0x8
  55#define AD_STATE_COLLECTING      0x10
  56#define AD_STATE_DISTRIBUTING    0x20
  57#define AD_STATE_DEFAULTED       0x40
  58#define AD_STATE_EXPIRED         0x80
  59
  60/* Port Variables definitions used by the State Machines (43.4.7 in the
  61 * 802.3ad standard)
  62 */
  63#define AD_PORT_BEGIN           0x1
  64#define AD_PORT_LACP_ENABLED    0x2
  65#define AD_PORT_ACTOR_CHURN     0x4
  66#define AD_PORT_PARTNER_CHURN   0x8
  67#define AD_PORT_READY           0x10
  68#define AD_PORT_READY_N         0x20
  69#define AD_PORT_MATCHED         0x40
  70#define AD_PORT_STANDBY         0x80
  71#define AD_PORT_SELECTED        0x100
  72#define AD_PORT_MOVED           0x200
  73#define AD_PORT_CHURNED         (AD_PORT_ACTOR_CHURN | AD_PORT_PARTNER_CHURN)
  74
  75/* Port Key definitions
  76 * key is determined according to the link speed, duplex and
  77 * user key (which is yet not supported)
  78 *           --------------------------------------------------------------
  79 * Port key  | User key (10 bits)           | Speed (5 bits)      | Duplex|
  80 *           --------------------------------------------------------------
  81 *           |15                           6|5                   1|0
  82 */
  83#define  AD_DUPLEX_KEY_MASKS    0x1
  84#define  AD_SPEED_KEY_MASKS     0x3E
  85#define  AD_USER_KEY_MASKS      0xFFC0
  86
  87enum ad_link_speed_type {
  88        AD_LINK_SPEED_1MBPS = 1,
  89        AD_LINK_SPEED_10MBPS,
  90        AD_LINK_SPEED_100MBPS,
  91        AD_LINK_SPEED_1000MBPS,
  92        AD_LINK_SPEED_2500MBPS,
  93        AD_LINK_SPEED_5000MBPS,
  94        AD_LINK_SPEED_10000MBPS,
  95        AD_LINK_SPEED_14000MBPS,
  96        AD_LINK_SPEED_20000MBPS,
  97        AD_LINK_SPEED_25000MBPS,
  98        AD_LINK_SPEED_40000MBPS,
  99        AD_LINK_SPEED_50000MBPS,
 100        AD_LINK_SPEED_56000MBPS,
 101        AD_LINK_SPEED_100000MBPS,
 102};
 103
 104/* compare MAC addresses */
 105#define MAC_ADDRESS_EQUAL(A, B) \
 106        ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
 107
 108static const u8 null_mac_addr[ETH_ALEN + 2] __long_aligned = {
 109        0, 0, 0, 0, 0, 0
 110};
 111static u16 ad_ticks_per_sec;
 112static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
 113
 114static const u8 lacpdu_mcast_addr[ETH_ALEN + 2] __long_aligned =
 115        MULTICAST_LACPDU_ADDR;
 116
 117/* ================= main 802.3ad protocol functions ================== */
 118static int ad_lacpdu_send(struct port *port);
 119static int ad_marker_send(struct port *port, struct bond_marker *marker);
 120static void ad_mux_machine(struct port *port, bool *update_slave_arr);
 121static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
 122static void ad_tx_machine(struct port *port);
 123static void ad_periodic_machine(struct port *port);
 124static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
 125static void ad_agg_selection_logic(struct aggregator *aggregator,
 126                                   bool *update_slave_arr);
 127static void ad_clear_agg(struct aggregator *aggregator);
 128static void ad_initialize_agg(struct aggregator *aggregator);
 129static void ad_initialize_port(struct port *port, int lacp_fast);
 130static void ad_enable_collecting_distributing(struct port *port,
 131                                              bool *update_slave_arr);
 132static void ad_disable_collecting_distributing(struct port *port,
 133                                               bool *update_slave_arr);
 134static void ad_marker_info_received(struct bond_marker *marker_info,
 135                                    struct port *port);
 136static void ad_marker_response_received(struct bond_marker *marker,
 137                                        struct port *port);
 138static void ad_update_actor_keys(struct port *port, bool reset);
 139
 140
 141/* ================= api to bonding and kernel code ================== */
 142
 143/**
 144 * __get_bond_by_port - get the port's bonding struct
 145 * @port: the port we're looking at
 146 *
 147 * Return @port's bonding struct, or %NULL if it can't be found.
 148 */
 149static inline struct bonding *__get_bond_by_port(struct port *port)
 150{
 151        if (port->slave == NULL)
 152                return NULL;
 153
 154        return bond_get_bond_by_slave(port->slave);
 155}
 156
 157/**
 158 * __get_first_agg - get the first aggregator in the bond
 159 * @bond: the bond we're looking at
 160 *
 161 * Return the aggregator of the first slave in @bond, or %NULL if it can't be
 162 * found.
 163 * The caller must hold RCU or RTNL lock.
 164 */
 165static inline struct aggregator *__get_first_agg(struct port *port)
 166{
 167        struct bonding *bond = __get_bond_by_port(port);
 168        struct slave *first_slave;
 169        struct aggregator *agg;
 170
 171        /* If there's no bond for this port, or bond has no slaves */
 172        if (bond == NULL)
 173                return NULL;
 174
 175        rcu_read_lock();
 176        first_slave = bond_first_slave_rcu(bond);
 177        agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
 178        rcu_read_unlock();
 179
 180        return agg;
 181}
 182
 183/**
 184 * __agg_has_partner - see if we have a partner
 185 * @agg: the agregator we're looking at
 186 *
 187 * Return nonzero if aggregator has a partner (denoted by a non-zero ether
 188 * address for the partner). Return 0 if not.
 189 */
 190static inline int __agg_has_partner(struct aggregator *agg)
 191{
 192        return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
 193}
 194
 195/**
 196 * __disable_port - disable the port's slave
 197 * @port: the port we're looking at
 198 */
 199static inline void __disable_port(struct port *port)
 200{
 201        bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
 202}
 203
 204/**
 205 * __enable_port - enable the port's slave, if it's up
 206 * @port: the port we're looking at
 207 */
 208static inline void __enable_port(struct port *port)
 209{
 210        struct slave *slave = port->slave;
 211
 212        if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
 213                bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
 214}
 215
 216/**
 217 * __port_is_enabled - check if the port's slave is in active state
 218 * @port: the port we're looking at
 219 */
 220static inline int __port_is_enabled(struct port *port)
 221{
 222        return bond_is_active_slave(port->slave);
 223}
 224
 225/**
 226 * __get_agg_selection_mode - get the aggregator selection mode
 227 * @port: the port we're looking at
 228 *
 229 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
 230 */
 231static inline u32 __get_agg_selection_mode(struct port *port)
 232{
 233        struct bonding *bond = __get_bond_by_port(port);
 234
 235        if (bond == NULL)
 236                return BOND_AD_STABLE;
 237
 238        return bond->params.ad_select;
 239}
 240
 241/**
 242 * __check_agg_selection_timer - check if the selection timer has expired
 243 * @port: the port we're looking at
 244 */
 245static inline int __check_agg_selection_timer(struct port *port)
 246{
 247        struct bonding *bond = __get_bond_by_port(port);
 248
 249        if (bond == NULL)
 250                return 0;
 251
 252        return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
 253}
 254
 255/**
 256 * __get_link_speed - get a port's speed
 257 * @port: the port we're looking at
 258 *
 259 * Return @port's speed in 802.3ad enum format. i.e. one of:
 260 *     0,
 261 *     %AD_LINK_SPEED_10MBPS,
 262 *     %AD_LINK_SPEED_100MBPS,
 263 *     %AD_LINK_SPEED_1000MBPS,
 264 *     %AD_LINK_SPEED_2500MBPS,
 265 *     %AD_LINK_SPEED_5000MBPS,
 266 *     %AD_LINK_SPEED_10000MBPS
 267 *     %AD_LINK_SPEED_14000MBPS,
 268 *     %AD_LINK_SPEED_20000MBPS
 269 *     %AD_LINK_SPEED_25000MBPS
 270 *     %AD_LINK_SPEED_40000MBPS
 271 *     %AD_LINK_SPEED_50000MBPS
 272 *     %AD_LINK_SPEED_56000MBPS
 273 *     %AD_LINK_SPEED_100000MBPS
 274 */
 275static u16 __get_link_speed(struct port *port)
 276{
 277        struct slave *slave = port->slave;
 278        u16 speed;
 279
 280        /* this if covers only a special case: when the configuration starts
 281         * with link down, it sets the speed to 0.
 282         * This is done in spite of the fact that the e100 driver reports 0
 283         * to be compatible with MVT in the future.
 284         */
 285        if (slave->link != BOND_LINK_UP)
 286                speed = 0;
 287        else {
 288                switch (slave->speed) {
 289                case SPEED_10:
 290                        speed = AD_LINK_SPEED_10MBPS;
 291                        break;
 292
 293                case SPEED_100:
 294                        speed = AD_LINK_SPEED_100MBPS;
 295                        break;
 296
 297                case SPEED_1000:
 298                        speed = AD_LINK_SPEED_1000MBPS;
 299                        break;
 300
 301                case SPEED_2500:
 302                        speed = AD_LINK_SPEED_2500MBPS;
 303                        break;
 304
 305                case SPEED_5000:
 306                        speed = AD_LINK_SPEED_5000MBPS;
 307                        break;
 308
 309                case SPEED_10000:
 310                        speed = AD_LINK_SPEED_10000MBPS;
 311                        break;
 312
 313                case SPEED_14000:
 314                        speed = AD_LINK_SPEED_14000MBPS;
 315                        break;
 316
 317                case SPEED_20000:
 318                        speed = AD_LINK_SPEED_20000MBPS;
 319                        break;
 320
 321                case SPEED_25000:
 322                        speed = AD_LINK_SPEED_25000MBPS;
 323                        break;
 324
 325                case SPEED_40000:
 326                        speed = AD_LINK_SPEED_40000MBPS;
 327                        break;
 328
 329                case SPEED_50000:
 330                        speed = AD_LINK_SPEED_50000MBPS;
 331                        break;
 332
 333                case SPEED_56000:
 334                        speed = AD_LINK_SPEED_56000MBPS;
 335                        break;
 336
 337                case SPEED_100000:
 338                        speed = AD_LINK_SPEED_100000MBPS;
 339                        break;
 340
 341                default:
 342                        /* unknown speed value from ethtool. shouldn't happen */
 343                        if (slave->speed != SPEED_UNKNOWN)
 344                                pr_warn_once("%s: unknown ethtool speed (%d) for port %d (set it to 0)\n",
 345                                             slave->bond->dev->name,
 346                                             slave->speed,
 347                                             port->actor_port_number);
 348                        speed = 0;
 349                        break;
 350                }
 351        }
 352
 353        netdev_dbg(slave->bond->dev, "Port %d Received link speed %d update from adapter\n",
 354                   port->actor_port_number, speed);
 355        return speed;
 356}
 357
 358/**
 359 * __get_duplex - get a port's duplex
 360 * @port: the port we're looking at
 361 *
 362 * Return @port's duplex in 802.3ad bitmask format. i.e.:
 363 *     0x01 if in full duplex
 364 *     0x00 otherwise
 365 */
 366static u8 __get_duplex(struct port *port)
 367{
 368        struct slave *slave = port->slave;
 369        u8 retval = 0x0;
 370
 371        /* handling a special case: when the configuration starts with
 372         * link down, it sets the duplex to 0.
 373         */
 374        if (slave->link == BOND_LINK_UP) {
 375                switch (slave->duplex) {
 376                case DUPLEX_FULL:
 377                        retval = 0x1;
 378                        netdev_dbg(slave->bond->dev, "Port %d Received status full duplex update from adapter\n",
 379                                   port->actor_port_number);
 380                        break;
 381                case DUPLEX_HALF:
 382                default:
 383                        retval = 0x0;
 384                        netdev_dbg(slave->bond->dev, "Port %d Received status NOT full duplex update from adapter\n",
 385                                   port->actor_port_number);
 386                        break;
 387                }
 388        }
 389        return retval;
 390}
 391
 392/* Conversions */
 393
 394/**
 395 * __ad_timer_to_ticks - convert a given timer type to AD module ticks
 396 * @timer_type: which timer to operate
 397 * @par: timer parameter. see below
 398 *
 399 * If @timer_type is %current_while_timer, @par indicates long/short timer.
 400 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
 401 *                                                   %SLOW_PERIODIC_TIME.
 402 */
 403static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
 404{
 405        u16 retval = 0; /* to silence the compiler */
 406
 407        switch (timer_type) {
 408        case AD_CURRENT_WHILE_TIMER:    /* for rx machine usage */
 409                if (par)
 410                        retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
 411                else
 412                        retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
 413                break;
 414        case AD_ACTOR_CHURN_TIMER:      /* for local churn machine */
 415                retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
 416                break;
 417        case AD_PERIODIC_TIMER:         /* for periodic machine */
 418                retval = (par*ad_ticks_per_sec); /* long timeout */
 419                break;
 420        case AD_PARTNER_CHURN_TIMER:    /* for remote churn machine */
 421                retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
 422                break;
 423        case AD_WAIT_WHILE_TIMER:       /* for selection machine */
 424                retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
 425                break;
 426        }
 427
 428        return retval;
 429}
 430
 431
 432/* ================= ad_rx_machine helper functions ================== */
 433
 434/**
 435 * __choose_matched - update a port's matched variable from a received lacpdu
 436 * @lacpdu: the lacpdu we've received
 437 * @port: the port we're looking at
 438 *
 439 * Update the value of the matched variable, using parameter values from a
 440 * newly received lacpdu. Parameter values for the partner carried in the
 441 * received PDU are compared with the corresponding operational parameter
 442 * values for the actor. Matched is set to TRUE if all of these parameters
 443 * match and the PDU parameter partner_state.aggregation has the same value as
 444 * actor_oper_port_state.aggregation and lacp will actively maintain the link
 445 * in the aggregation. Matched is also set to TRUE if the value of
 446 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
 447 * an individual link and lacp will actively maintain the link. Otherwise,
 448 * matched is set to FALSE. LACP is considered to be actively maintaining the
 449 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
 450 * the actor's actor_oper_port_state.lacp_activity and the PDU's
 451 * partner_state.lacp_activity variables are TRUE.
 452 *
 453 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
 454 * used here to implement the language from 802.3ad 43.4.9 that requires
 455 * recordPDU to "match" the LACPDU parameters to the stored values.
 456 */
 457static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
 458{
 459        /* check if all parameters are alike
 460         * or this is individual link(aggregation == FALSE)
 461         * then update the state machine Matched variable.
 462         */
 463        if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
 464             (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
 465             MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
 466             (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
 467             (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
 468             ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
 469            ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
 470                ) {
 471                port->sm_vars |= AD_PORT_MATCHED;
 472        } else {
 473                port->sm_vars &= ~AD_PORT_MATCHED;
 474        }
 475}
 476
 477/**
 478 * __record_pdu - record parameters from a received lacpdu
 479 * @lacpdu: the lacpdu we've received
 480 * @port: the port we're looking at
 481 *
 482 * Record the parameter values for the Actor carried in a received lacpdu as
 483 * the current partner operational parameter values and sets
 484 * actor_oper_port_state.defaulted to FALSE.
 485 */
 486static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
 487{
 488        if (lacpdu && port) {
 489                struct port_params *partner = &port->partner_oper;
 490
 491                __choose_matched(lacpdu, port);
 492                /* record the new parameter values for the partner
 493                 * operational
 494                 */
 495                partner->port_number = ntohs(lacpdu->actor_port);
 496                partner->port_priority = ntohs(lacpdu->actor_port_priority);
 497                partner->system = lacpdu->actor_system;
 498                partner->system_priority = ntohs(lacpdu->actor_system_priority);
 499                partner->key = ntohs(lacpdu->actor_key);
 500                partner->port_state = lacpdu->actor_state;
 501
 502                /* set actor_oper_port_state.defaulted to FALSE */
 503                port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
 504
 505                /* set the partner sync. to on if the partner is sync,
 506                 * and the port is matched
 507                 */
 508                if ((port->sm_vars & AD_PORT_MATCHED) &&
 509                    (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
 510                        partner->port_state |= AD_STATE_SYNCHRONIZATION;
 511                        pr_debug("%s partner sync=1\n", port->slave->dev->name);
 512                } else {
 513                        partner->port_state &= ~AD_STATE_SYNCHRONIZATION;
 514                        pr_debug("%s partner sync=0\n", port->slave->dev->name);
 515                }
 516        }
 517}
 518
 519/**
 520 * __record_default - record default parameters
 521 * @port: the port we're looking at
 522 *
 523 * This function records the default parameter values for the partner carried
 524 * in the Partner Admin parameters as the current partner operational parameter
 525 * values and sets actor_oper_port_state.defaulted to TRUE.
 526 */
 527static void __record_default(struct port *port)
 528{
 529        if (port) {
 530                /* record the partner admin parameters */
 531                memcpy(&port->partner_oper, &port->partner_admin,
 532                       sizeof(struct port_params));
 533
 534                /* set actor_oper_port_state.defaulted to true */
 535                port->actor_oper_port_state |= AD_STATE_DEFAULTED;
 536        }
 537}
 538
 539/**
 540 * __update_selected - update a port's Selected variable from a received lacpdu
 541 * @lacpdu: the lacpdu we've received
 542 * @port: the port we're looking at
 543 *
 544 * Update the value of the selected variable, using parameter values from a
 545 * newly received lacpdu. The parameter values for the Actor carried in the
 546 * received PDU are compared with the corresponding operational parameter
 547 * values for the ports partner. If one or more of the comparisons shows that
 548 * the value(s) received in the PDU differ from the current operational values,
 549 * then selected is set to FALSE and actor_oper_port_state.synchronization is
 550 * set to out_of_sync. Otherwise, selected remains unchanged.
 551 */
 552static void __update_selected(struct lacpdu *lacpdu, struct port *port)
 553{
 554        if (lacpdu && port) {
 555                const struct port_params *partner = &port->partner_oper;
 556
 557                /* check if any parameter is different then
 558                 * update the state machine selected variable.
 559                 */
 560                if (ntohs(lacpdu->actor_port) != partner->port_number ||
 561                    ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
 562                    !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
 563                    ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
 564                    ntohs(lacpdu->actor_key) != partner->key ||
 565                    (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) {
 566                        port->sm_vars &= ~AD_PORT_SELECTED;
 567                }
 568        }
 569}
 570
 571/**
 572 * __update_default_selected - update a port's Selected variable from Partner
 573 * @port: the port we're looking at
 574 *
 575 * This function updates the value of the selected variable, using the partner
 576 * administrative parameter values. The administrative values are compared with
 577 * the corresponding operational parameter values for the partner. If one or
 578 * more of the comparisons shows that the administrative value(s) differ from
 579 * the current operational values, then Selected is set to FALSE and
 580 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
 581 * Selected remains unchanged.
 582 */
 583static void __update_default_selected(struct port *port)
 584{
 585        if (port) {
 586                const struct port_params *admin = &port->partner_admin;
 587                const struct port_params *oper = &port->partner_oper;
 588
 589                /* check if any parameter is different then
 590                 * update the state machine selected variable.
 591                 */
 592                if (admin->port_number != oper->port_number ||
 593                    admin->port_priority != oper->port_priority ||
 594                    !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
 595                    admin->system_priority != oper->system_priority ||
 596                    admin->key != oper->key ||
 597                    (admin->port_state & AD_STATE_AGGREGATION)
 598                        != (oper->port_state & AD_STATE_AGGREGATION)) {
 599                        port->sm_vars &= ~AD_PORT_SELECTED;
 600                }
 601        }
 602}
 603
 604/**
 605 * __update_ntt - update a port's ntt variable from a received lacpdu
 606 * @lacpdu: the lacpdu we've received
 607 * @port: the port we're looking at
 608 *
 609 * Updates the value of the ntt variable, using parameter values from a newly
 610 * received lacpdu. The parameter values for the partner carried in the
 611 * received PDU are compared with the corresponding operational parameter
 612 * values for the Actor. If one or more of the comparisons shows that the
 613 * value(s) received in the PDU differ from the current operational values,
 614 * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
 615 */
 616static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
 617{
 618        /* validate lacpdu and port */
 619        if (lacpdu && port) {
 620                /* check if any parameter is different then
 621                 * update the port->ntt.
 622                 */
 623                if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
 624                    (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
 625                    !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
 626                    (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
 627                    (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
 628                    ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
 629                    ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
 630                    ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
 631                    ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
 632                   ) {
 633                        port->ntt = true;
 634                }
 635        }
 636}
 637
 638/**
 639 * __agg_ports_are_ready - check if all ports in an aggregator are ready
 640 * @aggregator: the aggregator we're looking at
 641 *
 642 */
 643static int __agg_ports_are_ready(struct aggregator *aggregator)
 644{
 645        struct port *port;
 646        int retval = 1;
 647
 648        if (aggregator) {
 649                /* scan all ports in this aggregator to verfy if they are
 650                 * all ready.
 651                 */
 652                for (port = aggregator->lag_ports;
 653                     port;
 654                     port = port->next_port_in_aggregator) {
 655                        if (!(port->sm_vars & AD_PORT_READY_N)) {
 656                                retval = 0;
 657                                break;
 658                        }
 659                }
 660        }
 661
 662        return retval;
 663}
 664
 665/**
 666 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
 667 * @aggregator: the aggregator we're looking at
 668 * @val: Should the ports' ready bit be set on or off
 669 *
 670 */
 671static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
 672{
 673        struct port *port;
 674
 675        for (port = aggregator->lag_ports; port;
 676             port = port->next_port_in_aggregator) {
 677                if (val)
 678                        port->sm_vars |= AD_PORT_READY;
 679                else
 680                        port->sm_vars &= ~AD_PORT_READY;
 681        }
 682}
 683
 684static int __agg_active_ports(struct aggregator *agg)
 685{
 686        struct port *port;
 687        int active = 0;
 688
 689        for (port = agg->lag_ports; port;
 690             port = port->next_port_in_aggregator) {
 691                if (port->is_enabled)
 692                        active++;
 693        }
 694
 695        return active;
 696}
 697
 698/**
 699 * __get_agg_bandwidth - get the total bandwidth of an aggregator
 700 * @aggregator: the aggregator we're looking at
 701 *
 702 */
 703static u32 __get_agg_bandwidth(struct aggregator *aggregator)
 704{
 705        int nports = __agg_active_ports(aggregator);
 706        u32 bandwidth = 0;
 707
 708        if (nports) {
 709                switch (__get_link_speed(aggregator->lag_ports)) {
 710                case AD_LINK_SPEED_1MBPS:
 711                        bandwidth = nports;
 712                        break;
 713                case AD_LINK_SPEED_10MBPS:
 714                        bandwidth = nports * 10;
 715                        break;
 716                case AD_LINK_SPEED_100MBPS:
 717                        bandwidth = nports * 100;
 718                        break;
 719                case AD_LINK_SPEED_1000MBPS:
 720                        bandwidth = nports * 1000;
 721                        break;
 722                case AD_LINK_SPEED_2500MBPS:
 723                        bandwidth = nports * 2500;
 724                        break;
 725                case AD_LINK_SPEED_5000MBPS:
 726                        bandwidth = nports * 5000;
 727                        break;
 728                case AD_LINK_SPEED_10000MBPS:
 729                        bandwidth = nports * 10000;
 730                        break;
 731                case AD_LINK_SPEED_14000MBPS:
 732                        bandwidth = nports * 14000;
 733                        break;
 734                case AD_LINK_SPEED_20000MBPS:
 735                        bandwidth = nports * 20000;
 736                        break;
 737                case AD_LINK_SPEED_25000MBPS:
 738                        bandwidth = nports * 25000;
 739                        break;
 740                case AD_LINK_SPEED_40000MBPS:
 741                        bandwidth = nports * 40000;
 742                        break;
 743                case AD_LINK_SPEED_50000MBPS:
 744                        bandwidth = nports * 50000;
 745                        break;
 746                case AD_LINK_SPEED_56000MBPS:
 747                        bandwidth = nports * 56000;
 748                        break;
 749                case AD_LINK_SPEED_100000MBPS:
 750                        bandwidth = nports * 100000;
 751                        break;
 752                default:
 753                        bandwidth = 0; /* to silence the compiler */
 754                }
 755        }
 756        return bandwidth;
 757}
 758
 759/**
 760 * __get_active_agg - get the current active aggregator
 761 * @aggregator: the aggregator we're looking at
 762 *
 763 * Caller must hold RCU lock.
 764 */
 765static struct aggregator *__get_active_agg(struct aggregator *aggregator)
 766{
 767        struct bonding *bond = aggregator->slave->bond;
 768        struct list_head *iter;
 769        struct slave *slave;
 770
 771        bond_for_each_slave_rcu(bond, slave, iter)
 772                if (SLAVE_AD_INFO(slave)->aggregator.is_active)
 773                        return &(SLAVE_AD_INFO(slave)->aggregator);
 774
 775        return NULL;
 776}
 777
 778/**
 779 * __update_lacpdu_from_port - update a port's lacpdu fields
 780 * @port: the port we're looking at
 781 */
 782static inline void __update_lacpdu_from_port(struct port *port)
 783{
 784        struct lacpdu *lacpdu = &port->lacpdu;
 785        const struct port_params *partner = &port->partner_oper;
 786
 787        /* update current actual Actor parameters
 788         * lacpdu->subtype                   initialized
 789         * lacpdu->version_number            initialized
 790         * lacpdu->tlv_type_actor_info       initialized
 791         * lacpdu->actor_information_length  initialized
 792         */
 793
 794        lacpdu->actor_system_priority = htons(port->actor_system_priority);
 795        lacpdu->actor_system = port->actor_system;
 796        lacpdu->actor_key = htons(port->actor_oper_port_key);
 797        lacpdu->actor_port_priority = htons(port->actor_port_priority);
 798        lacpdu->actor_port = htons(port->actor_port_number);
 799        lacpdu->actor_state = port->actor_oper_port_state;
 800        pr_debug("update lacpdu: %s, actor port state %x\n",
 801                 port->slave->dev->name, port->actor_oper_port_state);
 802
 803        /* lacpdu->reserved_3_1              initialized
 804         * lacpdu->tlv_type_partner_info     initialized
 805         * lacpdu->partner_information_length initialized
 806         */
 807
 808        lacpdu->partner_system_priority = htons(partner->system_priority);
 809        lacpdu->partner_system = partner->system;
 810        lacpdu->partner_key = htons(partner->key);
 811        lacpdu->partner_port_priority = htons(partner->port_priority);
 812        lacpdu->partner_port = htons(partner->port_number);
 813        lacpdu->partner_state = partner->port_state;
 814
 815        /* lacpdu->reserved_3_2              initialized
 816         * lacpdu->tlv_type_collector_info   initialized
 817         * lacpdu->collector_information_length initialized
 818         * collector_max_delay                initialized
 819         * reserved_12[12]                   initialized
 820         * tlv_type_terminator               initialized
 821         * terminator_length                 initialized
 822         * reserved_50[50]                   initialized
 823         */
 824}
 825
 826/* ================= main 802.3ad protocol code ========================= */
 827
 828/**
 829 * ad_lacpdu_send - send out a lacpdu packet on a given port
 830 * @port: the port we're looking at
 831 *
 832 * Returns:   0 on success
 833 *          < 0 on error
 834 */
 835static int ad_lacpdu_send(struct port *port)
 836{
 837        struct slave *slave = port->slave;
 838        struct sk_buff *skb;
 839        struct lacpdu_header *lacpdu_header;
 840        int length = sizeof(struct lacpdu_header);
 841
 842        skb = dev_alloc_skb(length);
 843        if (!skb)
 844                return -ENOMEM;
 845
 846        skb->dev = slave->dev;
 847        skb_reset_mac_header(skb);
 848        skb->network_header = skb->mac_header + ETH_HLEN;
 849        skb->protocol = PKT_TYPE_LACPDU;
 850        skb->priority = TC_PRIO_CONTROL;
 851
 852        lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
 853
 854        ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
 855        /* Note: source address is set to be the member's PERMANENT address,
 856         * because we use it to identify loopback lacpdus in receive.
 857         */
 858        ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
 859        lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
 860
 861        lacpdu_header->lacpdu = port->lacpdu;
 862
 863        dev_queue_xmit(skb);
 864
 865        return 0;
 866}
 867
 868/**
 869 * ad_marker_send - send marker information/response on a given port
 870 * @port: the port we're looking at
 871 * @marker: marker data to send
 872 *
 873 * Returns:   0 on success
 874 *          < 0 on error
 875 */
 876static int ad_marker_send(struct port *port, struct bond_marker *marker)
 877{
 878        struct slave *slave = port->slave;
 879        struct sk_buff *skb;
 880        struct bond_marker_header *marker_header;
 881        int length = sizeof(struct bond_marker_header);
 882
 883        skb = dev_alloc_skb(length + 16);
 884        if (!skb)
 885                return -ENOMEM;
 886
 887        skb_reserve(skb, 16);
 888
 889        skb->dev = slave->dev;
 890        skb_reset_mac_header(skb);
 891        skb->network_header = skb->mac_header + ETH_HLEN;
 892        skb->protocol = PKT_TYPE_LACPDU;
 893
 894        marker_header = (struct bond_marker_header *)skb_put(skb, length);
 895
 896        ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
 897        /* Note: source address is set to be the member's PERMANENT address,
 898         * because we use it to identify loopback MARKERs in receive.
 899         */
 900        ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
 901        marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
 902
 903        marker_header->marker = *marker;
 904
 905        dev_queue_xmit(skb);
 906
 907        return 0;
 908}
 909
 910/**
 911 * ad_mux_machine - handle a port's mux state machine
 912 * @port: the port we're looking at
 913 * @update_slave_arr: Does slave array need update?
 914 */
 915static void ad_mux_machine(struct port *port, bool *update_slave_arr)
 916{
 917        mux_states_t last_state;
 918
 919        /* keep current State Machine state to compare later if it was
 920         * changed
 921         */
 922        last_state = port->sm_mux_state;
 923
 924        if (port->sm_vars & AD_PORT_BEGIN) {
 925                port->sm_mux_state = AD_MUX_DETACHED;
 926        } else {
 927                switch (port->sm_mux_state) {
 928                case AD_MUX_DETACHED:
 929                        if ((port->sm_vars & AD_PORT_SELECTED)
 930                            || (port->sm_vars & AD_PORT_STANDBY))
 931                                /* if SELECTED or STANDBY */
 932                                port->sm_mux_state = AD_MUX_WAITING;
 933                        break;
 934                case AD_MUX_WAITING:
 935                        /* if SELECTED == FALSE return to DETACH state */
 936                        if (!(port->sm_vars & AD_PORT_SELECTED)) {
 937                                port->sm_vars &= ~AD_PORT_READY_N;
 938                                /* in order to withhold the Selection Logic to
 939                                 * check all ports READY_N value every callback
 940                                 * cycle to update ready variable, we check
 941                                 * READY_N and update READY here
 942                                 */
 943                                __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
 944                                port->sm_mux_state = AD_MUX_DETACHED;
 945                                break;
 946                        }
 947
 948                        /* check if the wait_while_timer expired */
 949                        if (port->sm_mux_timer_counter
 950                            && !(--port->sm_mux_timer_counter))
 951                                port->sm_vars |= AD_PORT_READY_N;
 952
 953                        /* in order to withhold the selection logic to check
 954                         * all ports READY_N value every callback cycle to
 955                         * update ready variable, we check READY_N and update
 956                         * READY here
 957                         */
 958                        __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
 959
 960                        /* if the wait_while_timer expired, and the port is
 961                         * in READY state, move to ATTACHED state
 962                         */
 963                        if ((port->sm_vars & AD_PORT_READY)
 964                            && !port->sm_mux_timer_counter)
 965                                port->sm_mux_state = AD_MUX_ATTACHED;
 966                        break;
 967                case AD_MUX_ATTACHED:
 968                        /* check also if agg_select_timer expired (so the
 969                         * edable port will take place only after this timer)
 970                         */
 971                        if ((port->sm_vars & AD_PORT_SELECTED) &&
 972                            (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) &&
 973                            !__check_agg_selection_timer(port)) {
 974                                if (port->aggregator->is_active)
 975                                        port->sm_mux_state =
 976                                            AD_MUX_COLLECTING_DISTRIBUTING;
 977                        } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
 978                                   (port->sm_vars & AD_PORT_STANDBY)) {
 979                                /* if UNSELECTED or STANDBY */
 980                                port->sm_vars &= ~AD_PORT_READY_N;
 981                                /* in order to withhold the selection logic to
 982                                 * check all ports READY_N value every callback
 983                                 * cycle to update ready variable, we check
 984                                 * READY_N and update READY here
 985                                 */
 986                                __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
 987                                port->sm_mux_state = AD_MUX_DETACHED;
 988                        } else if (port->aggregator->is_active) {
 989                                port->actor_oper_port_state |=
 990                                    AD_STATE_SYNCHRONIZATION;
 991                        }
 992                        break;
 993                case AD_MUX_COLLECTING_DISTRIBUTING:
 994                        if (!(port->sm_vars & AD_PORT_SELECTED) ||
 995                            (port->sm_vars & AD_PORT_STANDBY) ||
 996                            !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) ||
 997                            !(port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) {
 998                                port->sm_mux_state = AD_MUX_ATTACHED;
 999                        } else {
1000                                /* if port state hasn't changed make
1001                                 * sure that a collecting distributing
1002                                 * port in an active aggregator is enabled
1003                                 */
1004                                if (port->aggregator &&
1005                                    port->aggregator->is_active &&
1006                                    !__port_is_enabled(port)) {
1007
1008                                        __enable_port(port);
1009                                }
1010                        }
1011                        break;
1012                default:
1013                        break;
1014                }
1015        }
1016
1017        /* check if the state machine was changed */
1018        if (port->sm_mux_state != last_state) {
1019                pr_debug("Mux Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1020                         port->actor_port_number,
1021                         port->slave->dev->name,
1022                         last_state,
1023                         port->sm_mux_state);
1024                switch (port->sm_mux_state) {
1025                case AD_MUX_DETACHED:
1026                        port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
1027                        ad_disable_collecting_distributing(port,
1028                                                           update_slave_arr);
1029                        port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1030                        port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
1031                        port->ntt = true;
1032                        break;
1033                case AD_MUX_WAITING:
1034                        port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
1035                        break;
1036                case AD_MUX_ATTACHED:
1037                        if (port->aggregator->is_active)
1038                                port->actor_oper_port_state |=
1039                                    AD_STATE_SYNCHRONIZATION;
1040                        else
1041                                port->actor_oper_port_state &=
1042                                    ~AD_STATE_SYNCHRONIZATION;
1043                        port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
1044                        port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
1045                        ad_disable_collecting_distributing(port,
1046                                                           update_slave_arr);
1047                        port->ntt = true;
1048                        break;
1049                case AD_MUX_COLLECTING_DISTRIBUTING:
1050                        port->actor_oper_port_state |= AD_STATE_COLLECTING;
1051                        port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
1052                        port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
1053                        ad_enable_collecting_distributing(port,
1054                                                          update_slave_arr);
1055                        port->ntt = true;
1056                        break;
1057                default:
1058                        break;
1059                }
1060        }
1061}
1062
1063/**
1064 * ad_rx_machine - handle a port's rx State Machine
1065 * @lacpdu: the lacpdu we've received
1066 * @port: the port we're looking at
1067 *
1068 * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1069 * CURRENT. If timer expired set the state machine in the proper state.
1070 * In other cases, this function checks if we need to switch to other state.
1071 */
1072static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1073{
1074        rx_states_t last_state;
1075
1076        /* keep current State Machine state to compare later if it was
1077         * changed
1078         */
1079        last_state = port->sm_rx_state;
1080
1081        /* check if state machine should change state */
1082
1083        /* first, check if port was reinitialized */
1084        if (port->sm_vars & AD_PORT_BEGIN) {
1085                port->sm_rx_state = AD_RX_INITIALIZE;
1086                port->sm_vars |= AD_PORT_CHURNED;
1087        /* check if port is not enabled */
1088        } else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled)
1089                port->sm_rx_state = AD_RX_PORT_DISABLED;
1090        /* check if new lacpdu arrived */
1091        else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
1092                 (port->sm_rx_state == AD_RX_DEFAULTED) ||
1093                 (port->sm_rx_state == AD_RX_CURRENT))) {
1094                if (port->sm_rx_state != AD_RX_CURRENT)
1095                        port->sm_vars |= AD_PORT_CHURNED;
1096                port->sm_rx_timer_counter = 0;
1097                port->sm_rx_state = AD_RX_CURRENT;
1098        } else {
1099                /* if timer is on, and if it is expired */
1100                if (port->sm_rx_timer_counter &&
1101                    !(--port->sm_rx_timer_counter)) {
1102                        switch (port->sm_rx_state) {
1103                        case AD_RX_EXPIRED:
1104                                port->sm_rx_state = AD_RX_DEFAULTED;
1105                                break;
1106                        case AD_RX_CURRENT:
1107                                port->sm_rx_state = AD_RX_EXPIRED;
1108                                break;
1109                        default:
1110                                break;
1111                        }
1112                } else {
1113                        /* if no lacpdu arrived and no timer is on */
1114                        switch (port->sm_rx_state) {
1115                        case AD_RX_PORT_DISABLED:
1116                                if (port->is_enabled &&
1117                                    (port->sm_vars & AD_PORT_LACP_ENABLED))
1118                                        port->sm_rx_state = AD_RX_EXPIRED;
1119                                else if (port->is_enabled
1120                                         && ((port->sm_vars
1121                                              & AD_PORT_LACP_ENABLED) == 0))
1122                                        port->sm_rx_state = AD_RX_LACP_DISABLED;
1123                                break;
1124                        default:
1125                                break;
1126
1127                        }
1128                }
1129        }
1130
1131        /* check if the State machine was changed or new lacpdu arrived */
1132        if ((port->sm_rx_state != last_state) || (lacpdu)) {
1133                pr_debug("Rx Machine: Port=%d (%s), Last State=%d, Curr State=%d\n",
1134                         port->actor_port_number,
1135                         port->slave->dev->name,
1136                         last_state,
1137                         port->sm_rx_state);
1138                switch (port->sm_rx_state) {
1139                case AD_RX_INITIALIZE:
1140                        if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
1141                                port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1142                        else
1143                                port->sm_vars |= AD_PORT_LACP_ENABLED;
1144                        port->sm_vars &= ~AD_PORT_SELECTED;
1145                        __record_default(port);
1146                        port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1147                        port->sm_rx_state = AD_RX_PORT_DISABLED;
1148
1149                        /* Fall Through */
1150                case AD_RX_PORT_DISABLED:
1151                        port->sm_vars &= ~AD_PORT_MATCHED;
1152                        break;
1153                case AD_RX_LACP_DISABLED:
1154                        port->sm_vars &= ~AD_PORT_SELECTED;
1155                        __record_default(port);
1156                        port->partner_oper.port_state &= ~AD_STATE_AGGREGATION;
1157                        port->sm_vars |= AD_PORT_MATCHED;
1158                        port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1159                        break;
1160                case AD_RX_EXPIRED:
1161                        /* Reset of the Synchronization flag (Standard 43.4.12)
1162                         * This reset cause to disable this port in the
1163                         * COLLECTING_DISTRIBUTING state of the mux machine in
1164                         * case of EXPIRED even if LINK_DOWN didn't arrive for
1165                         * the port.
1166                         */
1167                        port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION;
1168                        port->sm_vars &= ~AD_PORT_MATCHED;
1169                        port->partner_oper.port_state |= AD_STATE_LACP_TIMEOUT;
1170                        port->partner_oper.port_state |= AD_STATE_LACP_ACTIVITY;
1171                        port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1172                        port->actor_oper_port_state |= AD_STATE_EXPIRED;
1173                        port->sm_vars |= AD_PORT_CHURNED;
1174                        break;
1175                case AD_RX_DEFAULTED:
1176                        __update_default_selected(port);
1177                        __record_default(port);
1178                        port->sm_vars |= AD_PORT_MATCHED;
1179                        port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1180                        break;
1181                case AD_RX_CURRENT:
1182                        /* detect loopback situation */
1183                        if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1184                                              &(port->actor_system))) {
1185                                netdev_err(port->slave->bond->dev, "An illegal loopback occurred on adapter (%s)\n"
1186                                       "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
1187                                       port->slave->dev->name);
1188                                return;
1189                        }
1190                        __update_selected(lacpdu, port);
1191                        __update_ntt(lacpdu, port);
1192                        __record_pdu(lacpdu, port);
1193                        port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
1194                        port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
1195                        break;
1196                default:
1197                        break;
1198                }
1199        }
1200}
1201
1202/**
1203 * ad_churn_machine - handle port churn's state machine
1204 * @port: the port we're looking at
1205 *
1206 */
1207static void ad_churn_machine(struct port *port)
1208{
1209        if (port->sm_vars & AD_PORT_CHURNED) {
1210                port->sm_vars &= ~AD_PORT_CHURNED;
1211                port->sm_churn_actor_state = AD_CHURN_MONITOR;
1212                port->sm_churn_partner_state = AD_CHURN_MONITOR;
1213                port->sm_churn_actor_timer_counter =
1214                        __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER, 0);
1215                 port->sm_churn_partner_timer_counter =
1216                         __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER, 0);
1217                return;
1218        }
1219        if (port->sm_churn_actor_timer_counter &&
1220            !(--port->sm_churn_actor_timer_counter) &&
1221            port->sm_churn_actor_state == AD_CHURN_MONITOR) {
1222                if (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION) {
1223                        port->sm_churn_actor_state = AD_NO_CHURN;
1224                } else {
1225                        port->churn_actor_count++;
1226                        port->sm_churn_actor_state = AD_CHURN;
1227                }
1228        }
1229        if (port->sm_churn_partner_timer_counter &&
1230            !(--port->sm_churn_partner_timer_counter) &&
1231            port->sm_churn_partner_state == AD_CHURN_MONITOR) {
1232                if (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) {
1233                        port->sm_churn_partner_state = AD_NO_CHURN;
1234                } else {
1235                        port->churn_partner_count++;
1236                        port->sm_churn_partner_state = AD_CHURN;
1237                }
1238        }
1239}
1240
1241/**
1242 * ad_tx_machine - handle a port's tx state machine
1243 * @port: the port we're looking at
1244 */
1245static void ad_tx_machine(struct port *port)
1246{
1247        /* check if tx timer expired, to verify that we do not send more than
1248         * 3 packets per second
1249         */
1250        if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1251                /* check if there is something to send */
1252                if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1253                        __update_lacpdu_from_port(port);
1254
1255                        if (ad_lacpdu_send(port) >= 0) {
1256                                pr_debug("Sent LACPDU on port %d\n",
1257                                         port->actor_port_number);
1258
1259                                /* mark ntt as false, so it will not be sent
1260                                 * again until demanded
1261                                 */
1262                                port->ntt = false;
1263                        }
1264                }
1265                /* restart tx timer(to verify that we will not exceed
1266                 * AD_MAX_TX_IN_SECOND
1267                 */
1268                port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1269        }
1270}
1271
1272/**
1273 * ad_periodic_machine - handle a port's periodic state machine
1274 * @port: the port we're looking at
1275 *
1276 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1277 */
1278static void ad_periodic_machine(struct port *port)
1279{
1280        periodic_states_t last_state;
1281
1282        /* keep current state machine state to compare later if it was changed */
1283        last_state = port->sm_periodic_state;
1284
1285        /* check if port was reinitialized */
1286        if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1287            (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY))
1288           ) {
1289                port->sm_periodic_state = AD_NO_PERIODIC;
1290        }
1291        /* check if state machine should change state */
1292        else if (port->sm_periodic_timer_counter) {
1293                /* check if periodic state machine expired */
1294                if (!(--port->sm_periodic_timer_counter)) {
1295                        /* if expired then do tx */
1296                        port->sm_periodic_state = AD_PERIODIC_TX;
1297                } else {
1298                        /* If not expired, check if there is some new timeout
1299                         * parameter from the partner state
1300                         */
1301                        switch (port->sm_periodic_state) {
1302                        case AD_FAST_PERIODIC:
1303                                if (!(port->partner_oper.port_state
1304                                      & AD_STATE_LACP_TIMEOUT))
1305                                        port->sm_periodic_state = AD_SLOW_PERIODIC;
1306                                break;
1307                        case AD_SLOW_PERIODIC:
1308                                if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) {
1309                                        port->sm_periodic_timer_counter = 0;
1310                                        port->sm_periodic_state = AD_PERIODIC_TX;
1311                                }
1312                                break;
1313                        default:
1314                                break;
1315                        }
1316                }
1317        } else {
1318                switch (port->sm_periodic_state) {
1319                case AD_NO_PERIODIC:
1320                        port->sm_periodic_state = AD_FAST_PERIODIC;
1321                        break;
1322                case AD_PERIODIC_TX:
1323                        if (!(port->partner_oper.port_state &
1324                            AD_STATE_LACP_TIMEOUT))
1325                                port->sm_periodic_state = AD_SLOW_PERIODIC;
1326                        else
1327                                port->sm_periodic_state = AD_FAST_PERIODIC;
1328                        break;
1329                default:
1330                        break;
1331                }
1332        }
1333
1334        /* check if the state machine was changed */
1335        if (port->sm_periodic_state != last_state) {
1336                pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1337                         port->actor_port_number, last_state,
1338                         port->sm_periodic_state);
1339                switch (port->sm_periodic_state) {
1340                case AD_NO_PERIODIC:
1341                        port->sm_periodic_timer_counter = 0;
1342                        break;
1343                case AD_FAST_PERIODIC:
1344                        /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1345                        port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1;
1346                        break;
1347                case AD_SLOW_PERIODIC:
1348                        /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1349                        port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1;
1350                        break;
1351                case AD_PERIODIC_TX:
1352                        port->ntt = true;
1353                        break;
1354                default:
1355                        break;
1356                }
1357        }
1358}
1359
1360/**
1361 * ad_port_selection_logic - select aggregation groups
1362 * @port: the port we're looking at
1363 * @update_slave_arr: Does slave array need update?
1364 *
1365 * Select aggregation groups, and assign each port for it's aggregetor. The
1366 * selection logic is called in the inititalization (after all the handshkes),
1367 * and after every lacpdu receive (if selected is off).
1368 */
1369static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
1370{
1371        struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1372        struct port *last_port = NULL, *curr_port;
1373        struct list_head *iter;
1374        struct bonding *bond;
1375        struct slave *slave;
1376        int found = 0;
1377
1378        /* if the port is already Selected, do nothing */
1379        if (port->sm_vars & AD_PORT_SELECTED)
1380                return;
1381
1382        bond = __get_bond_by_port(port);
1383
1384        /* if the port is connected to other aggregator, detach it */
1385        if (port->aggregator) {
1386                /* detach the port from its former aggregator */
1387                temp_aggregator = port->aggregator;
1388                for (curr_port = temp_aggregator->lag_ports; curr_port;
1389                     last_port = curr_port,
1390                     curr_port = curr_port->next_port_in_aggregator) {
1391                        if (curr_port == port) {
1392                                temp_aggregator->num_of_ports--;
1393                                /* if it is the first port attached to the
1394                                 * aggregator
1395                                 */
1396                                if (!last_port) {
1397                                        temp_aggregator->lag_ports =
1398                                                port->next_port_in_aggregator;
1399                                } else {
1400                                        /* not the first port attached to the
1401                                         * aggregator
1402                                         */
1403                                        last_port->next_port_in_aggregator =
1404                                                port->next_port_in_aggregator;
1405                                }
1406
1407                                /* clear the port's relations to this
1408                                 * aggregator
1409                                 */
1410                                port->aggregator = NULL;
1411                                port->next_port_in_aggregator = NULL;
1412                                port->actor_port_aggregator_identifier = 0;
1413
1414                                netdev_dbg(bond->dev, "Port %d left LAG %d\n",
1415                                           port->actor_port_number,
1416                                           temp_aggregator->aggregator_identifier);
1417                                /* if the aggregator is empty, clear its
1418                                 * parameters, and set it ready to be attached
1419                                 */
1420                                if (!temp_aggregator->lag_ports)
1421                                        ad_clear_agg(temp_aggregator);
1422                                break;
1423                        }
1424                }
1425                if (!curr_port) {
1426                        /* meaning: the port was related to an aggregator
1427                         * but was not on the aggregator port list
1428                         */
1429                        net_warn_ratelimited("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1430                                             port->slave->bond->dev->name,
1431                                             port->actor_port_number,
1432                                             port->slave->dev->name,
1433                                             port->aggregator->aggregator_identifier);
1434                }
1435        }
1436        /* search on all aggregators for a suitable aggregator for this port */
1437        bond_for_each_slave(bond, slave, iter) {
1438                aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1439
1440                /* keep a free aggregator for later use(if needed) */
1441                if (!aggregator->lag_ports) {
1442                        if (!free_aggregator)
1443                                free_aggregator = aggregator;
1444                        continue;
1445                }
1446                /* check if current aggregator suits us */
1447                if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1448                     MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1449                     (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1450                     (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1451                    ) &&
1452                    ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1453                      !aggregator->is_individual)  /* but is not individual OR */
1454                    )
1455                   ) {
1456                        /* attach to the founded aggregator */
1457                        port->aggregator = aggregator;
1458                        port->actor_port_aggregator_identifier =
1459                                port->aggregator->aggregator_identifier;
1460                        port->next_port_in_aggregator = aggregator->lag_ports;
1461                        port->aggregator->num_of_ports++;
1462                        aggregator->lag_ports = port;
1463                        netdev_dbg(bond->dev, "Port %d joined LAG %d(existing LAG)\n",
1464                                   port->actor_port_number,
1465                                   port->aggregator->aggregator_identifier);
1466
1467                        /* mark this port as selected */
1468                        port->sm_vars |= AD_PORT_SELECTED;
1469                        found = 1;
1470                        break;
1471                }
1472        }
1473
1474        /* the port couldn't find an aggregator - attach it to a new
1475         * aggregator
1476         */
1477        if (!found) {
1478                if (free_aggregator) {
1479                        /* assign port a new aggregator */
1480                        port->aggregator = free_aggregator;
1481                        port->actor_port_aggregator_identifier =
1482                                port->aggregator->aggregator_identifier;
1483
1484                        /* update the new aggregator's parameters
1485                         * if port was responsed from the end-user
1486                         */
1487                        if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
1488                                /* if port is full duplex */
1489                                port->aggregator->is_individual = false;
1490                        else
1491                                port->aggregator->is_individual = true;
1492
1493                        port->aggregator->actor_admin_aggregator_key =
1494                                port->actor_admin_port_key;
1495                        port->aggregator->actor_oper_aggregator_key =
1496                                port->actor_oper_port_key;
1497                        port->aggregator->partner_system =
1498                                port->partner_oper.system;
1499                        port->aggregator->partner_system_priority =
1500                                port->partner_oper.system_priority;
1501                        port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1502                        port->aggregator->receive_state = 1;
1503                        port->aggregator->transmit_state = 1;
1504                        port->aggregator->lag_ports = port;
1505                        port->aggregator->num_of_ports++;
1506
1507                        /* mark this port as selected */
1508                        port->sm_vars |= AD_PORT_SELECTED;
1509
1510                        netdev_dbg(bond->dev, "Port %d joined LAG %d(new LAG)\n",
1511                                   port->actor_port_number,
1512                                   port->aggregator->aggregator_identifier);
1513                } else {
1514                        netdev_err(bond->dev, "Port %d (on %s) did not find a suitable aggregator\n",
1515                               port->actor_port_number, port->slave->dev->name);
1516                }
1517        }
1518        /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1519         * in all aggregator's ports, else set ready=FALSE in all
1520         * aggregator's ports
1521         */
1522        __set_agg_ports_ready(port->aggregator,
1523                              __agg_ports_are_ready(port->aggregator));
1524
1525        aggregator = __get_first_agg(port);
1526        ad_agg_selection_logic(aggregator, update_slave_arr);
1527
1528        if (!port->aggregator->is_active)
1529                port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
1530}
1531
1532/* Decide if "agg" is a better choice for the new active aggregator that
1533 * the current best, according to the ad_select policy.
1534 */
1535static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1536                                                struct aggregator *curr)
1537{
1538        /* 0. If no best, select current.
1539         *
1540         * 1. If the current agg is not individual, and the best is
1541         *    individual, select current.
1542         *
1543         * 2. If current agg is individual and the best is not, keep best.
1544         *
1545         * 3. Therefore, current and best are both individual or both not
1546         *    individual, so:
1547         *
1548         * 3a. If current agg partner replied, and best agg partner did not,
1549         *     select current.
1550         *
1551         * 3b. If current agg partner did not reply and best agg partner
1552         *     did reply, keep best.
1553         *
1554         * 4.  Therefore, current and best both have partner replies or
1555         *     both do not, so perform selection policy:
1556         *
1557         * BOND_AD_COUNT: Select by count of ports.  If count is equal,
1558         *     select by bandwidth.
1559         *
1560         * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1561         */
1562        if (!best)
1563                return curr;
1564
1565        if (!curr->is_individual && best->is_individual)
1566                return curr;
1567
1568        if (curr->is_individual && !best->is_individual)
1569                return best;
1570
1571        if (__agg_has_partner(curr) && !__agg_has_partner(best))
1572                return curr;
1573
1574        if (!__agg_has_partner(curr) && __agg_has_partner(best))
1575                return best;
1576
1577        switch (__get_agg_selection_mode(curr->lag_ports)) {
1578        case BOND_AD_COUNT:
1579                if (__agg_active_ports(curr) > __agg_active_ports(best))
1580                        return curr;
1581
1582                if (__agg_active_ports(curr) < __agg_active_ports(best))
1583                        return best;
1584
1585                /*FALLTHROUGH*/
1586        case BOND_AD_STABLE:
1587        case BOND_AD_BANDWIDTH:
1588                if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1589                        return curr;
1590
1591                break;
1592
1593        default:
1594                net_warn_ratelimited("%s: Impossible agg select mode %d\n",
1595                                     curr->slave->bond->dev->name,
1596                                     __get_agg_selection_mode(curr->lag_ports));
1597                break;
1598        }
1599
1600        return best;
1601}
1602
1603static int agg_device_up(const struct aggregator *agg)
1604{
1605        struct port *port = agg->lag_ports;
1606
1607        if (!port)
1608                return 0;
1609
1610        for (port = agg->lag_ports; port;
1611             port = port->next_port_in_aggregator) {
1612                if (netif_running(port->slave->dev) &&
1613                    netif_carrier_ok(port->slave->dev))
1614                        return 1;
1615        }
1616
1617        return 0;
1618}
1619
1620/**
1621 * ad_agg_selection_logic - select an aggregation group for a team
1622 * @aggregator: the aggregator we're looking at
1623 * @update_slave_arr: Does slave array need update?
1624 *
1625 * It is assumed that only one aggregator may be selected for a team.
1626 *
1627 * The logic of this function is to select the aggregator according to
1628 * the ad_select policy:
1629 *
1630 * BOND_AD_STABLE: select the aggregator with the most ports attached to
1631 * it, and to reselect the active aggregator only if the previous
1632 * aggregator has no more ports related to it.
1633 *
1634 * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1635 * bandwidth, and reselect whenever a link state change takes place or the
1636 * set of slaves in the bond changes.
1637 *
1638 * BOND_AD_COUNT: select the aggregator with largest number of ports
1639 * (slaves), and reselect whenever a link state change takes place or the
1640 * set of slaves in the bond changes.
1641 *
1642 * FIXME: this function MUST be called with the first agg in the bond, or
1643 * __get_active_agg() won't work correctly. This function should be better
1644 * called with the bond itself, and retrieve the first agg from it.
1645 */
1646static void ad_agg_selection_logic(struct aggregator *agg,
1647                                   bool *update_slave_arr)
1648{
1649        struct aggregator *best, *active, *origin;
1650        struct bonding *bond = agg->slave->bond;
1651        struct list_head *iter;
1652        struct slave *slave;
1653        struct port *port;
1654
1655        rcu_read_lock();
1656        origin = agg;
1657        active = __get_active_agg(agg);
1658        best = (active && agg_device_up(active)) ? active : NULL;
1659
1660        bond_for_each_slave_rcu(bond, slave, iter) {
1661                agg = &(SLAVE_AD_INFO(slave)->aggregator);
1662
1663                agg->is_active = 0;
1664
1665                if (__agg_active_ports(agg) && agg_device_up(agg))
1666                        best = ad_agg_selection_test(best, agg);
1667        }
1668
1669        if (best &&
1670            __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1671                /* For the STABLE policy, don't replace the old active
1672                 * aggregator if it's still active (it has an answering
1673                 * partner) or if both the best and active don't have an
1674                 * answering partner.
1675                 */
1676                if (active && active->lag_ports &&
1677                    __agg_active_ports(active) &&
1678                    (__agg_has_partner(active) ||
1679                     (!__agg_has_partner(active) &&
1680                     !__agg_has_partner(best)))) {
1681                        if (!(!active->actor_oper_aggregator_key &&
1682                              best->actor_oper_aggregator_key)) {
1683                                best = NULL;
1684                                active->is_active = 1;
1685                        }
1686                }
1687        }
1688
1689        if (best && (best == active)) {
1690                best = NULL;
1691                active->is_active = 1;
1692        }
1693
1694        /* if there is new best aggregator, activate it */
1695        if (best) {
1696                netdev_dbg(bond->dev, "best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1697                           best->aggregator_identifier, best->num_of_ports,
1698                           best->actor_oper_aggregator_key,
1699                           best->partner_oper_aggregator_key,
1700                           best->is_individual, best->is_active);
1701                netdev_dbg(bond->dev, "best ports %p slave %p %s\n",
1702                           best->lag_ports, best->slave,
1703                           best->slave ? best->slave->dev->name : "NULL");
1704
1705                bond_for_each_slave_rcu(bond, slave, iter) {
1706                        agg = &(SLAVE_AD_INFO(slave)->aggregator);
1707
1708                        netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1709                                   agg->aggregator_identifier, agg->num_of_ports,
1710                                   agg->actor_oper_aggregator_key,
1711                                   agg->partner_oper_aggregator_key,
1712                                   agg->is_individual, agg->is_active);
1713                }
1714
1715                /* check if any partner replys */
1716                if (best->is_individual) {
1717                        net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1718                                             best->slave ?
1719                                             best->slave->bond->dev->name : "NULL");
1720                }
1721
1722                best->is_active = 1;
1723                netdev_dbg(bond->dev, "LAG %d chosen as the active LAG\n",
1724                           best->aggregator_identifier);
1725                netdev_dbg(bond->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1726                           best->aggregator_identifier, best->num_of_ports,
1727                           best->actor_oper_aggregator_key,
1728                           best->partner_oper_aggregator_key,
1729                           best->is_individual, best->is_active);
1730
1731                /* disable the ports that were related to the former
1732                 * active_aggregator
1733                 */
1734                if (active) {
1735                        for (port = active->lag_ports; port;
1736                             port = port->next_port_in_aggregator) {
1737                                __disable_port(port);
1738                        }
1739                }
1740                /* Slave array needs update. */
1741                *update_slave_arr = true;
1742        }
1743
1744        /* if the selected aggregator is of join individuals
1745         * (partner_system is NULL), enable their ports
1746         */
1747        active = __get_active_agg(origin);
1748
1749        if (active) {
1750                if (!__agg_has_partner(active)) {
1751                        for (port = active->lag_ports; port;
1752                             port = port->next_port_in_aggregator) {
1753                                __enable_port(port);
1754                        }
1755                }
1756        }
1757
1758        rcu_read_unlock();
1759
1760        bond_3ad_set_carrier(bond);
1761}
1762
1763/**
1764 * ad_clear_agg - clear a given aggregator's parameters
1765 * @aggregator: the aggregator we're looking at
1766 */
1767static void ad_clear_agg(struct aggregator *aggregator)
1768{
1769        if (aggregator) {
1770                aggregator->is_individual = false;
1771                aggregator->actor_admin_aggregator_key = 0;
1772                aggregator->actor_oper_aggregator_key = 0;
1773                eth_zero_addr(aggregator->partner_system.mac_addr_value);
1774                aggregator->partner_system_priority = 0;
1775                aggregator->partner_oper_aggregator_key = 0;
1776                aggregator->receive_state = 0;
1777                aggregator->transmit_state = 0;
1778                aggregator->lag_ports = NULL;
1779                aggregator->is_active = 0;
1780                aggregator->num_of_ports = 0;
1781                pr_debug("LAG %d was cleared\n",
1782                         aggregator->aggregator_identifier);
1783        }
1784}
1785
1786/**
1787 * ad_initialize_agg - initialize a given aggregator's parameters
1788 * @aggregator: the aggregator we're looking at
1789 */
1790static void ad_initialize_agg(struct aggregator *aggregator)
1791{
1792        if (aggregator) {
1793                ad_clear_agg(aggregator);
1794
1795                eth_zero_addr(aggregator->aggregator_mac_address.mac_addr_value);
1796                aggregator->aggregator_identifier = 0;
1797                aggregator->slave = NULL;
1798        }
1799}
1800
1801/**
1802 * ad_initialize_port - initialize a given port's parameters
1803 * @aggregator: the aggregator we're looking at
1804 * @lacp_fast: boolean. whether fast periodic should be used
1805 */
1806static void ad_initialize_port(struct port *port, int lacp_fast)
1807{
1808        static const struct port_params tmpl = {
1809                .system_priority = 0xffff,
1810                .key             = 1,
1811                .port_number     = 1,
1812                .port_priority   = 0xff,
1813                .port_state      = 1,
1814        };
1815        static const struct lacpdu lacpdu = {
1816                .subtype                = 0x01,
1817                .version_number = 0x01,
1818                .tlv_type_actor_info = 0x01,
1819                .actor_information_length = 0x14,
1820                .tlv_type_partner_info = 0x02,
1821                .partner_information_length = 0x14,
1822                .tlv_type_collector_info = 0x03,
1823                .collector_information_length = 0x10,
1824                .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1825        };
1826
1827        if (port) {
1828                port->actor_port_priority = 0xff;
1829                port->actor_port_aggregator_identifier = 0;
1830                port->ntt = false;
1831                port->actor_admin_port_state = AD_STATE_AGGREGATION |
1832                                               AD_STATE_LACP_ACTIVITY;
1833                port->actor_oper_port_state  = AD_STATE_AGGREGATION |
1834                                               AD_STATE_LACP_ACTIVITY;
1835
1836                if (lacp_fast)
1837                        port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
1838
1839                memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1840                memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1841
1842                port->is_enabled = true;
1843                /* private parameters */
1844                port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED;
1845                port->sm_rx_state = 0;
1846                port->sm_rx_timer_counter = 0;
1847                port->sm_periodic_state = 0;
1848                port->sm_periodic_timer_counter = 0;
1849                port->sm_mux_state = 0;
1850                port->sm_mux_timer_counter = 0;
1851                port->sm_tx_state = 0;
1852                port->aggregator = NULL;
1853                port->next_port_in_aggregator = NULL;
1854                port->transaction_id = 0;
1855
1856                port->sm_churn_actor_timer_counter = 0;
1857                port->sm_churn_actor_state = 0;
1858                port->churn_actor_count = 0;
1859                port->sm_churn_partner_timer_counter = 0;
1860                port->sm_churn_partner_state = 0;
1861                port->churn_partner_count = 0;
1862
1863                memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1864        }
1865}
1866
1867/**
1868 * ad_enable_collecting_distributing - enable a port's transmit/receive
1869 * @port: the port we're looking at
1870 * @update_slave_arr: Does slave array need update?
1871 *
1872 * Enable @port if it's in an active aggregator
1873 */
1874static void ad_enable_collecting_distributing(struct port *port,
1875                                              bool *update_slave_arr)
1876{
1877        if (port->aggregator->is_active) {
1878                pr_debug("Enabling port %d(LAG %d)\n",
1879                         port->actor_port_number,
1880                         port->aggregator->aggregator_identifier);
1881                __enable_port(port);
1882                /* Slave array needs update */
1883                *update_slave_arr = true;
1884        }
1885}
1886
1887/**
1888 * ad_disable_collecting_distributing - disable a port's transmit/receive
1889 * @port: the port we're looking at
1890 * @update_slave_arr: Does slave array need update?
1891 */
1892static void ad_disable_collecting_distributing(struct port *port,
1893                                               bool *update_slave_arr)
1894{
1895        if (port->aggregator &&
1896            !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1897                               &(null_mac_addr))) {
1898                pr_debug("Disabling port %d(LAG %d)\n",
1899                         port->actor_port_number,
1900                         port->aggregator->aggregator_identifier);
1901                __disable_port(port);
1902                /* Slave array needs an update */
1903                *update_slave_arr = true;
1904        }
1905}
1906
1907/**
1908 * ad_marker_info_received - handle receive of a Marker information frame
1909 * @marker_info: Marker info received
1910 * @port: the port we're looking at
1911 */
1912static void ad_marker_info_received(struct bond_marker *marker_info,
1913        struct port *port)
1914{
1915        struct bond_marker marker;
1916
1917        /* copy the received marker data to the response marker */
1918        memcpy(&marker, marker_info, sizeof(struct bond_marker));
1919        /* change the marker subtype to marker response */
1920        marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
1921
1922        /* send the marker response */
1923        if (ad_marker_send(port, &marker) >= 0) {
1924                pr_debug("Sent Marker Response on port %d\n",
1925                         port->actor_port_number);
1926        }
1927}
1928
1929/**
1930 * ad_marker_response_received - handle receive of a marker response frame
1931 * @marker: marker PDU received
1932 * @port: the port we're looking at
1933 *
1934 * This function does nothing since we decided not to implement send and handle
1935 * response for marker PDU's, in this stage, but only to respond to marker
1936 * information.
1937 */
1938static void ad_marker_response_received(struct bond_marker *marker,
1939                                        struct port *port)
1940{
1941        /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1942}
1943
1944/* ========= AD exported functions to the main bonding code ========= */
1945
1946/* Check aggregators status in team every T seconds */
1947#define AD_AGGREGATOR_SELECTION_TIMER  8
1948
1949/**
1950 * bond_3ad_initiate_agg_selection - initate aggregator selection
1951 * @bond: bonding struct
1952 *
1953 * Set the aggregation selection timer, to initiate an agg selection in
1954 * the very near future.  Called during first initialization, and during
1955 * any down to up transitions of the bond.
1956 */
1957void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1958{
1959        BOND_AD_INFO(bond).agg_select_timer = timeout;
1960}
1961
1962/**
1963 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
1964 * @bond: bonding struct to work on
1965 * @tick_resolution: tick duration (millisecond resolution)
1966 *
1967 * Can be called only after the mac address of the bond is set.
1968 */
1969void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution)
1970{
1971        /* check that the bond is not initialized yet */
1972        if (!MAC_ADDRESS_EQUAL(&(BOND_AD_INFO(bond).system.sys_mac_addr),
1973                                bond->dev->dev_addr)) {
1974
1975                BOND_AD_INFO(bond).aggregator_identifier = 0;
1976
1977                BOND_AD_INFO(bond).system.sys_priority =
1978                        bond->params.ad_actor_sys_prio;
1979                if (is_zero_ether_addr(bond->params.ad_actor_system))
1980                        BOND_AD_INFO(bond).system.sys_mac_addr =
1981                            *((struct mac_addr *)bond->dev->dev_addr);
1982                else
1983                        BOND_AD_INFO(bond).system.sys_mac_addr =
1984                            *((struct mac_addr *)bond->params.ad_actor_system);
1985
1986                /* initialize how many times this module is called in one
1987                 * second (should be about every 100ms)
1988                 */
1989                ad_ticks_per_sec = tick_resolution;
1990
1991                bond_3ad_initiate_agg_selection(bond,
1992                                                AD_AGGREGATOR_SELECTION_TIMER *
1993                                                ad_ticks_per_sec);
1994        }
1995}
1996
1997/**
1998 * bond_3ad_bind_slave - initialize a slave's port
1999 * @slave: slave struct to work on
2000 *
2001 * Returns:   0 on success
2002 *          < 0 on error
2003 */
2004void bond_3ad_bind_slave(struct slave *slave)
2005{
2006        struct bonding *bond = bond_get_bond_by_slave(slave);
2007        struct port *port;
2008        struct aggregator *aggregator;
2009
2010        /* check that the slave has not been initialized yet. */
2011        if (SLAVE_AD_INFO(slave)->port.slave != slave) {
2012
2013                /* port initialization */
2014                port = &(SLAVE_AD_INFO(slave)->port);
2015
2016                ad_initialize_port(port, bond->params.lacp_fast);
2017
2018                port->slave = slave;
2019                port->actor_port_number = SLAVE_AD_INFO(slave)->id;
2020                /* key is determined according to the link speed, duplex and
2021                 * user key
2022                 */
2023                port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
2024                ad_update_actor_keys(port, false);
2025                /* actor system is the bond's system */
2026                port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
2027                port->actor_system_priority =
2028                    BOND_AD_INFO(bond).system.sys_priority;
2029                /* tx timer(to verify that no more than MAX_TX_IN_SECOND
2030                 * lacpdu's are sent in one second)
2031                 */
2032                port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
2033
2034                __disable_port(port);
2035
2036                /* aggregator initialization */
2037                aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2038
2039                ad_initialize_agg(aggregator);
2040
2041                aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
2042                aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
2043                aggregator->slave = slave;
2044                aggregator->is_active = 0;
2045                aggregator->num_of_ports = 0;
2046        }
2047}
2048
2049/**
2050 * bond_3ad_unbind_slave - deinitialize a slave's port
2051 * @slave: slave struct to work on
2052 *
2053 * Search for the aggregator that is related to this port, remove the
2054 * aggregator and assign another aggregator for other port related to it
2055 * (if any), and remove the port.
2056 */
2057void bond_3ad_unbind_slave(struct slave *slave)
2058{
2059        struct port *port, *prev_port, *temp_port;
2060        struct aggregator *aggregator, *new_aggregator = NULL, *temp_aggregator;
2061        int select_new_active_agg = 0;
2062        struct bonding *bond = slave->bond;
2063        struct slave *slave_iter;
2064        struct list_head *iter;
2065        bool dummy_slave_update; /* Ignore this value as caller updates array */
2066
2067        /* Sync against bond_3ad_state_machine_handler() */
2068        spin_lock_bh(&bond->mode_lock);
2069        aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2070        port = &(SLAVE_AD_INFO(slave)->port);
2071
2072        /* if slave is null, the whole port is not initialized */
2073        if (!port->slave) {
2074                netdev_warn(bond->dev, "Trying to unbind an uninitialized port on %s\n",
2075                            slave->dev->name);
2076                goto out;
2077        }
2078
2079        netdev_dbg(bond->dev, "Unbinding Link Aggregation Group %d\n",
2080                   aggregator->aggregator_identifier);
2081
2082        /* Tell the partner that this port is not suitable for aggregation */
2083        port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
2084        __update_lacpdu_from_port(port);
2085        ad_lacpdu_send(port);
2086
2087        /* check if this aggregator is occupied */
2088        if (aggregator->lag_ports) {
2089                /* check if there are other ports related to this aggregator
2090                 * except the port related to this slave(thats ensure us that
2091                 * there is a reason to search for new aggregator, and that we
2092                 * will find one
2093                 */
2094                if ((aggregator->lag_ports != port) ||
2095                    (aggregator->lag_ports->next_port_in_aggregator)) {
2096                        /* find new aggregator for the related port(s) */
2097                        bond_for_each_slave(bond, slave_iter, iter) {
2098                                new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2099                                /* if the new aggregator is empty, or it is
2100                                 * connected to our port only
2101                                 */
2102                                if (!new_aggregator->lag_ports ||
2103                                    ((new_aggregator->lag_ports == port) &&
2104                                     !new_aggregator->lag_ports->next_port_in_aggregator))
2105                                        break;
2106                        }
2107                        if (!slave_iter)
2108                                new_aggregator = NULL;
2109
2110                        /* if new aggregator found, copy the aggregator's
2111                         * parameters and connect the related lag_ports to the
2112                         * new aggregator
2113                         */
2114                        if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2115                                netdev_dbg(bond->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2116                                           aggregator->aggregator_identifier,
2117                                           new_aggregator->aggregator_identifier);
2118
2119                                if ((new_aggregator->lag_ports == port) &&
2120                                    new_aggregator->is_active) {
2121                                        netdev_info(bond->dev, "Removing an active aggregator\n");
2122                                         select_new_active_agg = 1;
2123                                }
2124
2125                                new_aggregator->is_individual = aggregator->is_individual;
2126                                new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2127                                new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2128                                new_aggregator->partner_system = aggregator->partner_system;
2129                                new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2130                                new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2131                                new_aggregator->receive_state = aggregator->receive_state;
2132                                new_aggregator->transmit_state = aggregator->transmit_state;
2133                                new_aggregator->lag_ports = aggregator->lag_ports;
2134                                new_aggregator->is_active = aggregator->is_active;
2135                                new_aggregator->num_of_ports = aggregator->num_of_ports;
2136
2137                                /* update the information that is written on
2138                                 * the ports about the aggregator
2139                                 */
2140                                for (temp_port = aggregator->lag_ports; temp_port;
2141                                     temp_port = temp_port->next_port_in_aggregator) {
2142                                        temp_port->aggregator = new_aggregator;
2143                                        temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2144                                }
2145
2146                                ad_clear_agg(aggregator);
2147
2148                                if (select_new_active_agg)
2149                                        ad_agg_selection_logic(__get_first_agg(port),
2150                                                               &dummy_slave_update);
2151                        } else {
2152                                netdev_warn(bond->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
2153                        }
2154                } else {
2155                        /* in case that the only port related to this
2156                         * aggregator is the one we want to remove
2157                         */
2158                        select_new_active_agg = aggregator->is_active;
2159                        ad_clear_agg(aggregator);
2160                        if (select_new_active_agg) {
2161                                netdev_info(bond->dev, "Removing an active aggregator\n");
2162                                /* select new active aggregator */
2163                                temp_aggregator = __get_first_agg(port);
2164                                if (temp_aggregator)
2165                                        ad_agg_selection_logic(temp_aggregator,
2166                                                               &dummy_slave_update);
2167                        }
2168                }
2169        }
2170
2171        netdev_dbg(bond->dev, "Unbinding port %d\n", port->actor_port_number);
2172
2173        /* find the aggregator that this port is connected to */
2174        bond_for_each_slave(bond, slave_iter, iter) {
2175                temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2176                prev_port = NULL;
2177                /* search the port in the aggregator's related ports */
2178                for (temp_port = temp_aggregator->lag_ports; temp_port;
2179                     prev_port = temp_port,
2180                     temp_port = temp_port->next_port_in_aggregator) {
2181                        if (temp_port == port) {
2182                                /* the aggregator found - detach the port from
2183                                 * this aggregator
2184                                 */
2185                                if (prev_port)
2186                                        prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2187                                else
2188                                        temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2189                                temp_aggregator->num_of_ports--;
2190                                if (__agg_active_ports(temp_aggregator) == 0) {
2191                                        select_new_active_agg = temp_aggregator->is_active;
2192                                        ad_clear_agg(temp_aggregator);
2193                                        if (select_new_active_agg) {
2194                                                netdev_info(bond->dev, "Removing an active aggregator\n");
2195                                                /* select new active aggregator */
2196                                                ad_agg_selection_logic(__get_first_agg(port),
2197                                                                       &dummy_slave_update);
2198                                        }
2199                                }
2200                                break;
2201                        }
2202                }
2203        }
2204        port->slave = NULL;
2205
2206out:
2207        spin_unlock_bh(&bond->mode_lock);
2208}
2209
2210/**
2211 * bond_3ad_state_machine_handler - handle state machines timeout
2212 * @bond: bonding struct to work on
2213 *
2214 * The state machine handling concept in this module is to check every tick
2215 * which state machine should operate any function. The execution order is
2216 * round robin, so when we have an interaction between state machines, the
2217 * reply of one to each other might be delayed until next tick.
2218 *
2219 * This function also complete the initialization when the agg_select_timer
2220 * times out, and it selects an aggregator for the ports that are yet not
2221 * related to any aggregator, and selects the active aggregator for a bond.
2222 */
2223void bond_3ad_state_machine_handler(struct work_struct *work)
2224{
2225        struct bonding *bond = container_of(work, struct bonding,
2226                                            ad_work.work);
2227        struct aggregator *aggregator;
2228        struct list_head *iter;
2229        struct slave *slave;
2230        struct port *port;
2231        bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
2232        bool update_slave_arr = false;
2233
2234        /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2235         * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2236         * concurrently due to incoming LACPDU as well.
2237         */
2238        spin_lock_bh(&bond->mode_lock);
2239        rcu_read_lock();
2240
2241        /* check if there are any slaves */
2242        if (!bond_has_slaves(bond))
2243                goto re_arm;
2244
2245        /* check if agg_select_timer timer after initialize is timed out */
2246        if (BOND_AD_INFO(bond).agg_select_timer &&
2247            !(--BOND_AD_INFO(bond).agg_select_timer)) {
2248                slave = bond_first_slave_rcu(bond);
2249                port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
2250
2251                /* select the active aggregator for the bond */
2252                if (port) {
2253                        if (!port->slave) {
2254                                net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2255                                                     bond->dev->name);
2256                                goto re_arm;
2257                        }
2258
2259                        aggregator = __get_first_agg(port);
2260                        ad_agg_selection_logic(aggregator, &update_slave_arr);
2261                }
2262                bond_3ad_set_carrier(bond);
2263        }
2264
2265        /* for each port run the state machines */
2266        bond_for_each_slave_rcu(bond, slave, iter) {
2267                port = &(SLAVE_AD_INFO(slave)->port);
2268                if (!port->slave) {
2269                        net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
2270                                            bond->dev->name);
2271                        goto re_arm;
2272                }
2273
2274                ad_rx_machine(NULL, port);
2275                ad_periodic_machine(port);
2276                ad_port_selection_logic(port, &update_slave_arr);
2277                ad_mux_machine(port, &update_slave_arr);
2278                ad_tx_machine(port);
2279                ad_churn_machine(port);
2280
2281                /* turn off the BEGIN bit, since we already handled it */
2282                if (port->sm_vars & AD_PORT_BEGIN)
2283                        port->sm_vars &= ~AD_PORT_BEGIN;
2284        }
2285
2286re_arm:
2287        bond_for_each_slave_rcu(bond, slave, iter) {
2288                if (slave->should_notify) {
2289                        should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2290                        break;
2291                }
2292        }
2293        rcu_read_unlock();
2294        spin_unlock_bh(&bond->mode_lock);
2295
2296        if (update_slave_arr)
2297                bond_slave_arr_work_rearm(bond, 0);
2298
2299        if (should_notify_rtnl && rtnl_trylock()) {
2300                bond_slave_state_notify(bond);
2301                rtnl_unlock();
2302        }
2303        queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2304}
2305
2306/**
2307 * bond_3ad_rx_indication - handle a received frame
2308 * @lacpdu: received lacpdu
2309 * @slave: slave struct to work on
2310 * @length: length of the data received
2311 *
2312 * It is assumed that frames that were sent on this NIC don't returned as new
2313 * received frames (loopback). Since only the payload is given to this
2314 * function, it check for loopback.
2315 */
2316static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave,
2317                                  u16 length)
2318{
2319        struct port *port;
2320        int ret = RX_HANDLER_ANOTHER;
2321
2322        if (length >= sizeof(struct lacpdu)) {
2323
2324                port = &(SLAVE_AD_INFO(slave)->port);
2325
2326                if (!port->slave) {
2327                        net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2328                                             slave->dev->name, slave->bond->dev->name);
2329                        return ret;
2330                }
2331
2332                switch (lacpdu->subtype) {
2333                case AD_TYPE_LACPDU:
2334                        ret = RX_HANDLER_CONSUMED;
2335                        netdev_dbg(slave->bond->dev,
2336                                   "Received LACPDU on port %d slave %s\n",
2337                                   port->actor_port_number,
2338                                   slave->dev->name);
2339                        /* Protect against concurrent state machines */
2340                        spin_lock(&slave->bond->mode_lock);
2341                        ad_rx_machine(lacpdu, port);
2342                        spin_unlock(&slave->bond->mode_lock);
2343                        break;
2344
2345                case AD_TYPE_MARKER:
2346                        ret = RX_HANDLER_CONSUMED;
2347                        /* No need to convert fields to Little Endian since we
2348                         * don't use the marker's fields.
2349                         */
2350
2351                        switch (((struct bond_marker *)lacpdu)->tlv_type) {
2352                        case AD_MARKER_INFORMATION_SUBTYPE:
2353                                netdev_dbg(slave->bond->dev, "Received Marker Information on port %d\n",
2354                                           port->actor_port_number);
2355                                ad_marker_info_received((struct bond_marker *)lacpdu, port);
2356                                break;
2357
2358                        case AD_MARKER_RESPONSE_SUBTYPE:
2359                                netdev_dbg(slave->bond->dev, "Received Marker Response on port %d\n",
2360                                           port->actor_port_number);
2361                                ad_marker_response_received((struct bond_marker *)lacpdu, port);
2362                                break;
2363
2364                        default:
2365                                netdev_dbg(slave->bond->dev, "Received an unknown Marker subtype on slot %d\n",
2366                                           port->actor_port_number);
2367                        }
2368                }
2369        }
2370        return ret;
2371}
2372
2373/**
2374 * ad_update_actor_keys - Update the oper / admin keys for a port based on
2375 * its current speed and duplex settings.
2376 *
2377 * @port: the port we'are looking at
2378 * @reset: Boolean to just reset the speed and the duplex part of the key
2379 *
2380 * The logic to change the oper / admin keys is:
2381 * (a) A full duplex port can participate in LACP with partner.
2382 * (b) When the speed is changed, LACP need to be reinitiated.
2383 */
2384static void ad_update_actor_keys(struct port *port, bool reset)
2385{
2386        u8 duplex = 0;
2387        u16 ospeed = 0, speed = 0;
2388        u16 old_oper_key = port->actor_oper_port_key;
2389
2390        port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2391        if (!reset) {
2392                speed = __get_link_speed(port);
2393                ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2394                duplex = __get_duplex(port);
2395                port->actor_admin_port_key |= (speed << 1) | duplex;
2396        }
2397        port->actor_oper_port_key = port->actor_admin_port_key;
2398
2399        if (old_oper_key != port->actor_oper_port_key) {
2400                /* Only 'duplex' port participates in LACP */
2401                if (duplex)
2402                        port->sm_vars |= AD_PORT_LACP_ENABLED;
2403                else
2404                        port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2405
2406                if (!reset) {
2407                        if (!speed) {
2408                                netdev_err(port->slave->dev,
2409                                           "speed changed to 0 for port %s",
2410                                           port->slave->dev->name);
2411                        } else if (duplex && ospeed != speed) {
2412                                /* Speed change restarts LACP state-machine */
2413                                port->sm_vars |= AD_PORT_BEGIN;
2414                        }
2415                }
2416        }
2417}
2418
2419/**
2420 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2421 * change indication
2422 *
2423 * @slave: slave struct to work on
2424 *
2425 * Handle reselection of aggregator (if needed) for this port.
2426 */
2427void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
2428{
2429        struct port *port;
2430
2431        port = &(SLAVE_AD_INFO(slave)->port);
2432
2433        /* if slave is null, the whole port is not initialized */
2434        if (!port->slave) {
2435                netdev_warn(slave->bond->dev,
2436                            "speed/duplex changed for uninitialized port %s\n",
2437                            slave->dev->name);
2438                return;
2439        }
2440
2441        spin_lock_bh(&slave->bond->mode_lock);
2442        ad_update_actor_keys(port, false);
2443        spin_unlock_bh(&slave->bond->mode_lock);
2444        netdev_dbg(slave->bond->dev, "Port %d slave %s changed speed/duplex\n",
2445                   port->actor_port_number, slave->dev->name);
2446}
2447
2448/**
2449 * bond_3ad_handle_link_change - handle a slave's link status change indication
2450 * @slave: slave struct to work on
2451 * @status: whether the link is now up or down
2452 *
2453 * Handle reselection of aggregator (if needed) for this port.
2454 */
2455void bond_3ad_handle_link_change(struct slave *slave, char link)
2456{
2457        struct aggregator *agg;
2458        struct port *port;
2459        bool dummy;
2460
2461        port = &(SLAVE_AD_INFO(slave)->port);
2462
2463        /* if slave is null, the whole port is not initialized */
2464        if (!port->slave) {
2465                netdev_warn(slave->bond->dev, "link status changed for uninitialized port on %s\n",
2466                            slave->dev->name);
2467                return;
2468        }
2469
2470        spin_lock_bh(&slave->bond->mode_lock);
2471        /* on link down we are zeroing duplex and speed since
2472         * some of the adaptors(ce1000.lan) report full duplex/speed
2473         * instead of N/A(duplex) / 0(speed).
2474         *
2475         * on link up we are forcing recheck on the duplex and speed since
2476         * some of he adaptors(ce1000.lan) report.
2477         */
2478        if (link == BOND_LINK_UP) {
2479                port->is_enabled = true;
2480                ad_update_actor_keys(port, false);
2481        } else {
2482                /* link has failed */
2483                port->is_enabled = false;
2484                ad_update_actor_keys(port, true);
2485        }
2486        agg = __get_first_agg(port);
2487        ad_agg_selection_logic(agg, &dummy);
2488
2489        spin_unlock_bh(&slave->bond->mode_lock);
2490
2491        netdev_dbg(slave->bond->dev, "Port %d changed link status to %s\n",
2492                   port->actor_port_number,
2493                   link == BOND_LINK_UP ? "UP" : "DOWN");
2494
2495        /* RTNL is held and mode_lock is released so it's safe
2496         * to update slave_array here.
2497         */
2498        bond_update_slave_arr(slave->bond, NULL);
2499}
2500
2501/**
2502 * bond_3ad_set_carrier - set link state for bonding master
2503 * @bond - bonding structure
2504 *
2505 * if we have an active aggregator, we're up, if not, we're down.
2506 * Presumes that we cannot have an active aggregator if there are
2507 * no slaves with link up.
2508 *
2509 * This behavior complies with IEEE 802.3 section 43.3.9.
2510 *
2511 * Called by bond_set_carrier(). Return zero if carrier state does not
2512 * change, nonzero if it does.
2513 */
2514int bond_3ad_set_carrier(struct bonding *bond)
2515{
2516        struct aggregator *active;
2517        struct slave *first_slave;
2518        int ret = 1;
2519
2520        rcu_read_lock();
2521        first_slave = bond_first_slave_rcu(bond);
2522        if (!first_slave) {
2523                ret = 0;
2524                goto out;
2525        }
2526        active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
2527        if (active) {
2528                /* are enough slaves available to consider link up? */
2529                if (__agg_active_ports(active) < bond->params.min_links) {
2530                        if (netif_carrier_ok(bond->dev)) {
2531                                netif_carrier_off(bond->dev);
2532                                goto out;
2533                        }
2534                } else if (!netif_carrier_ok(bond->dev)) {
2535                        netif_carrier_on(bond->dev);
2536                        goto out;
2537                }
2538        } else if (netif_carrier_ok(bond->dev)) {
2539                netif_carrier_off(bond->dev);
2540        }
2541out:
2542        rcu_read_unlock();
2543        return ret;
2544}
2545
2546/**
2547 * __bond_3ad_get_active_agg_info - get information of the active aggregator
2548 * @bond: bonding struct to work on
2549 * @ad_info: ad_info struct to fill with the bond's info
2550 *
2551 * Returns:   0 on success
2552 *          < 0 on error
2553 */
2554int __bond_3ad_get_active_agg_info(struct bonding *bond,
2555                                   struct ad_info *ad_info)
2556{
2557        struct aggregator *aggregator = NULL;
2558        struct list_head *iter;
2559        struct slave *slave;
2560        struct port *port;
2561
2562        bond_for_each_slave_rcu(bond, slave, iter) {
2563                port = &(SLAVE_AD_INFO(slave)->port);
2564                if (port->aggregator && port->aggregator->is_active) {
2565                        aggregator = port->aggregator;
2566                        break;
2567                }
2568        }
2569
2570        if (!aggregator)
2571                return -1;
2572
2573        ad_info->aggregator_id = aggregator->aggregator_identifier;
2574        ad_info->ports = __agg_active_ports(aggregator);
2575        ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2576        ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2577        ether_addr_copy(ad_info->partner_system,
2578                        aggregator->partner_system.mac_addr_value);
2579        return 0;
2580}
2581
2582int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2583{
2584        int ret;
2585
2586        rcu_read_lock();
2587        ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2588        rcu_read_unlock();
2589
2590        return ret;
2591}
2592
2593int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2594                         struct slave *slave)
2595{
2596        struct lacpdu *lacpdu, _lacpdu;
2597
2598        if (skb->protocol != PKT_TYPE_LACPDU)
2599                return RX_HANDLER_ANOTHER;
2600
2601        if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2602                return RX_HANDLER_ANOTHER;
2603
2604        lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2605        if (!lacpdu)
2606                return RX_HANDLER_ANOTHER;
2607
2608        return bond_3ad_rx_indication(lacpdu, slave, skb->len);
2609}
2610
2611/**
2612 * bond_3ad_update_lacp_rate - change the lacp rate
2613 * @bond - bonding struct
2614 *
2615 * When modify lacp_rate parameter via sysfs,
2616 * update actor_oper_port_state of each port.
2617 *
2618 * Hold bond->mode_lock,
2619 * so we can modify port->actor_oper_port_state,
2620 * no matter bond is up or down.
2621 */
2622void bond_3ad_update_lacp_rate(struct bonding *bond)
2623{
2624        struct port *port = NULL;
2625        struct list_head *iter;
2626        struct slave *slave;
2627        int lacp_fast;
2628
2629        lacp_fast = bond->params.lacp_fast;
2630        spin_lock_bh(&bond->mode_lock);
2631        bond_for_each_slave(bond, slave, iter) {
2632                port = &(SLAVE_AD_INFO(slave)->port);
2633                if (lacp_fast)
2634                        port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
2635                else
2636                        port->actor_oper_port_state &= ~AD_STATE_LACP_TIMEOUT;
2637        }
2638        spin_unlock_bh(&bond->mode_lock);
2639}
2640