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