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