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