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