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