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