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