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