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