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