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